Browse Source

Updated C++# to the latest LLVM/Clang.

LLVM r245554 / 9a4e2cb3295f286dafc41b7e18619bc150880611, Clang r245562 / 41edf4ec7304ddfdf9225d225586804f394a5cf4.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>

Conflicts:
	docs/GettingStarted.md
pull/536/merge
Dimitar Dobrev 10 years ago
parent
commit
0e3b5f81a5
  1. 2
      build/Clang-commit
  2. 2
      build/LLVM-commit
  3. 128
      src/CppParser/ELFDumper.h
  4. 39
      src/CppParser/Parser.cpp

2
build/Clang-commit

@ -1 +1 @@ @@ -1 +1 @@
3457cd5516ac741fa106623d9578f5ac88593f4d
41edf4ec7304ddfdf9225d225586804f394a5cf4

2
build/LLVM-commit

@ -1 +1 @@ @@ -1 +1 @@
0e8abfa6ed986c892ec723236e32e78fd9c47b88
9a4e2cb3295f286dafc41b7e18619bc150880611

128
src/CppParser/ELFDumper.h

@ -0,0 +1,128 @@ @@ -0,0 +1,128 @@
/************************************************************************
*
* CppSharp
* Licensed under the simplified BSD license. All rights reserved.
*
************************************************************************/
#pragma once
#include <llvm/Object/ELF.h>
namespace CppSharp { namespace CppParser {
template<typename ELFT>
class ELFDumper {
public:
ELFDumper(const llvm::object::ELFFile<ELFT> *Obj);
std::vector<llvm::StringRef> ELFDumper<ELFT>::getNeededLibraries() const;
private:
typedef llvm::object::ELFFile<ELFT> ELFO;
typedef typename ELFO::Elf_Sym Elf_Sym;
typedef typename ELFO::Elf_Dyn Elf_Dyn;
typedef typename ELFO::Elf_Dyn_Range Elf_Dyn_Range;
typedef typename ELFO::Elf_Phdr Elf_Phdr;
typedef typename ELFO::uintX_t uintX_t;
/// \brief Represents a region described by entries in the .dynamic table.
struct DynRegionInfo {
DynRegionInfo() : Addr(nullptr), Size(0), EntSize(0) {}
/// \brief Address in current address space.
const void *Addr;
/// \brief Size in bytes of the region.
uintX_t Size;
/// \brief Size of each entity in the region.
uintX_t EntSize;
};
llvm::StringRef getDynamicString(uint64_t Offset) const;
const Elf_Dyn *dynamic_table_begin() const;
const Elf_Dyn *dynamic_table_end() const;
Elf_Dyn_Range dynamic_table() const {
return llvm::make_range(dynamic_table_begin(), dynamic_table_end());
}
const ELFO *Obj;
DynRegionInfo DynamicRegion;
llvm::StringRef DynamicStringTable;
};
template <typename ELFT>
ELFDumper<ELFT>::ELFDumper(const llvm::object::ELFFile<ELFT> *Obj) {
llvm::SmallVector<const Elf_Phdr *, 4> LoadSegments;
for (const Elf_Phdr &Phdr : Obj->program_headers()) {
if (Phdr.p_type == llvm::ELF::PT_DYNAMIC) {
DynamicRegion.Addr = Obj->base() + Phdr.p_offset;
uint64_t Size = Phdr.p_filesz;
if (Size % sizeof(Elf_Dyn))
llvm::report_fatal_error("Invalid dynamic table size");
DynamicRegion.Size = Phdr.p_filesz;
continue;
}
if (Phdr.p_type != llvm::ELF::PT_LOAD || Phdr.p_filesz == 0)
continue;
LoadSegments.push_back(&Phdr);
}
auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t *{
const Elf_Phdr **I = std::upper_bound(
LoadSegments.begin(), LoadSegments.end(), VAddr, llvm::object::compareAddr<ELFT>);
if (I == LoadSegments.begin())
llvm::report_fatal_error("Virtual address is not in any segment");
--I;
const Elf_Phdr &Phdr = **I;
uint64_t Delta = VAddr - Phdr.p_vaddr;
if (Delta >= Phdr.p_filesz)
llvm::report_fatal_error("Virtual address is not in any segment");
return Obj->base() + Phdr.p_offset + Delta;
};
const char *StringTableBegin = nullptr;
uint64_t StringTableSize = 0;
for (const Elf_Dyn &Dyn : dynamic_table()) {
switch (Dyn.d_tag) {
case llvm::ELF::DT_STRTAB:
StringTableBegin = (const char *)toMappedAddr(Dyn.getPtr());
break;
case llvm::ELF::DT_STRSZ:
StringTableSize = Dyn.getVal();
break;
}
}
if (StringTableBegin)
DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
}
template <typename ELFT>
const typename ELFDumper<ELFT>::Elf_Dyn *
ELFDumper<ELFT>::dynamic_table_begin() const {
return reinterpret_cast<const Elf_Dyn *>(DynamicRegion.Addr);
}
template <typename ELFT>
const typename ELFDumper<ELFT>::Elf_Dyn *
ELFDumper<ELFT>::dynamic_table_end() const {
uint64_t Size = DynamicRegion.Size;
return dynamic_table_begin() + Size / sizeof(Elf_Dyn);
}
template <class ELFT>
llvm::StringRef ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
return llvm::StringRef(DynamicStringTable.data() + Value);
}
template<class ELFT>
std::vector<llvm::StringRef> ELFDumper<ELFT>::getNeededLibraries() const {
std::vector<llvm::StringRef> Libs;
for (const auto &Entry : dynamic_table())
if (Entry.d_tag == llvm::ELF::DT_NEEDED)
Libs.push_back(getDynamicString(Entry.d_un.d_val));
return Libs;
}
} }

39
src/CppParser/Parser.cpp

@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
#endif
#include "Parser.h"
#include "ELFDumper.h"
#include <llvm/Support/Host.h>
#include <llvm/Support/Path.h>
@ -284,7 +285,7 @@ void Parser::SetupHeader() @@ -284,7 +285,7 @@ void Parser::SetupHeader()
C->createPreprocessor(TU_Complete);
Preprocessor& PP = C->getPreprocessor();
PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
PP.getLangOpts());
C->createASTContext();
@ -2517,7 +2518,7 @@ PreprocessedEntity* Parser::WalkPreprocessedEntity( @@ -2517,7 +2518,7 @@ PreprocessedEntity* Parser::WalkPreprocessedEntity(
}
case clang::PreprocessedEntity::MacroDefinitionKind:
{
auto MD = cast<clang::MacroDefinition>(PPEntity);
auto MD = cast<clang::MacroDefinitionRecord>(PPEntity);
if (!IsValidDeclaration(MD->getLocation()))
break;
@ -3160,13 +3161,11 @@ ParserResult* Parser::ParseHeader(const std::string& File, ParserResult* res) @@ -3160,13 +3161,11 @@ ParserResult* Parser::ParseHeader(const std::string& File, ParserResult* res)
std::unique_ptr<llvm::Module> M(new llvm::Module("", Ctx));
M->setTargetTriple(AST->getTargetInfo().getTriple().getTriple());
M->setDataLayout(AST->getTargetInfo().getTargetDescription());
std::unique_ptr<llvm::DataLayout> TD(new llvm::DataLayout(AST->getTargetInfo()
.getTargetDescription()));
M->setDataLayout(AST->getTargetInfo().getDataLayoutString());
std::unique_ptr<clang::CodeGen::CodeGenModule> CGM(
new clang::CodeGen::CodeGenModule(C->getASTContext(), C->getCodeGenOpts(),
*M, *TD, C->getDiagnostics()));
new clang::CodeGen::CodeGenModule(C->getASTContext(), C->getHeaderSearchOpts(),
C->getPreprocessorOpts(), C->getCodeGenOpts(), *M, C->getDiagnostics()));
std::unique_ptr<clang::CodeGen::CodeGenTypes> CGT(
new clang::CodeGen::CodeGenTypes(*CGM.get()));
@ -3212,9 +3211,9 @@ static ArchType ConvertArchType(unsigned int archType) @@ -3212,9 +3211,9 @@ static ArchType ConvertArchType(unsigned int archType)
template<class ELFT>
static void ReadELFDependencies(const llvm::object::ELFFile<ELFT>* ELFFile, CppSharp::CppParser::NativeLibrary*& NativeLib)
{
for (const auto &Entry : ELFFile->dynamic_table())
if (Entry.d_tag == llvm::ELF::DT_NEEDED)
NativeLib->Dependencies.push_back(ELFFile->getDynamicString(Entry.d_un.d_val));
ELFDumper<ELFT> ELFDumper(ELFFile);
for (const auto &Dependency : ELFDumper.getNeededLibraries())
NativeLib->Dependencies.push_back(Dependency);
}
ParserResultKind Parser::ParseSharedLib(llvm::StringRef File,
@ -3228,8 +3227,8 @@ ParserResultKind Parser::ParseSharedLib(llvm::StringRef File, @@ -3228,8 +3227,8 @@ ParserResultKind Parser::ParseSharedLib(llvm::StringRef File,
if (ObjectFile->isELF())
{
auto IDyn = llvm::object::getELFDynamicSymbolIterators(ObjectFile);
for (auto it = IDyn.first; it != IDyn.second; ++it)
auto IDyn = llvm::cast<llvm::object::ELFObjectFileBase>(ObjectFile)->getDynamicSymbolIterators();
for (auto it = IDyn.begin(); it != IDyn.end(); ++it)
{
std::string Sym;
llvm::raw_string_ostream SymStream(Sym);
@ -3262,16 +3261,16 @@ ParserResultKind Parser::ParseSharedLib(llvm::StringRef File, @@ -3262,16 +3261,16 @@ ParserResultKind Parser::ParseSharedLib(llvm::StringRef File,
{
if (auto COFFObjectFile = llvm::dyn_cast<llvm::object::COFFObjectFile>(ObjectFile))
{
for (auto it = COFFObjectFile->export_directory_begin(); it != COFFObjectFile->export_directory_end(); ++it)
for (auto ExportedSymbol : COFFObjectFile->export_directories())
{
llvm::StringRef Symbol;
if (!it->getSymbolName(Symbol))
if (!ExportedSymbol.getSymbolName(Symbol))
NativeLib->Symbols.push_back(Symbol);
}
for (auto dep = COFFObjectFile->import_directory_begin(); dep != COFFObjectFile->import_directory_end(); ++dep)
for (auto ImportedSymbol : COFFObjectFile->import_directories())
{
llvm::StringRef Name;
if (!dep->getName(Name) && (Name.endswith(".dll") || Name.endswith(".DLL")))
if (!ImportedSymbol.getName(Name) && (Name.endswith(".dll") || Name.endswith(".DLL")))
NativeLib->Dependencies.push_back(Name);
}
}
@ -3422,13 +3421,11 @@ ParserTargetInfo* Parser::GetTargetInfo() @@ -3422,13 +3421,11 @@ ParserTargetInfo* Parser::GetTargetInfo()
std::unique_ptr<llvm::Module> M(new llvm::Module("", Ctx));
M->setTargetTriple(AST->getTargetInfo().getTriple().getTriple());
M->setDataLayout(AST->getTargetInfo().getTargetDescription());
std::unique_ptr<llvm::DataLayout> TD(new llvm::DataLayout(AST->getTargetInfo()
.getTargetDescription()));
M->setDataLayout(AST->getTargetInfo().getDataLayoutString());
std::unique_ptr<clang::CodeGen::CodeGenModule> CGM(
new clang::CodeGen::CodeGenModule(C->getASTContext(), C->getCodeGenOpts(),
*M, *TD, C->getDiagnostics()));
new clang::CodeGen::CodeGenModule(C->getASTContext(), C->getHeaderSearchOpts(),
C->getPreprocessorOpts(), C->getCodeGenOpts(), *M, C->getDiagnostics()));
std::unique_ptr<clang::CodeGen::CodeGenTypes> CGT(
new clang::CodeGen::CodeGenTypes(*CGM.get()));

Loading…
Cancel
Save