diff options
| author | Peter Collingbourne <peter@pcc.me.uk> | 2017-04-14 02:55:06 +0000 |
|---|---|---|
| committer | Peter Collingbourne <peter@pcc.me.uk> | 2017-04-14 02:55:06 +0000 |
| commit | 8446f1fe6a5afd89c0f24e5500fce675390ce87b (patch) | |
| tree | 800cf0ab92411704f2b4e6b82f0ce7a799476045 | |
| parent | 2f72b19b05e86321b40c048ed7fdd0a9637d93d8 (diff) | |
| download | bcm5719-llvm-8446f1fe6a5afd89c0f24e5500fce675390ce87b.tar.gz bcm5719-llvm-8446f1fe6a5afd89c0f24e5500fce675390ce87b.zip | |
Object, LTO: Add target triple to irsymtab and LTO API.
Start using it in LLD to avoid needing to read bitcode again just to get the
target triple, and in llvm-lto2 to avoid printing symbol table information
that is inappropriate for the target.
Differential Revision: https://reviews.llvm.org/D32038
llvm-svn: 300300
| -rw-r--r-- | lld/COFF/InputFiles.cpp | 6 | ||||
| -rw-r--r-- | lld/ELF/InputFiles.cpp | 55 | ||||
| -rw-r--r-- | lld/ELF/InputFiles.h | 9 | ||||
| -rw-r--r-- | llvm/include/llvm/LTO/LTO.h | 5 | ||||
| -rw-r--r-- | llvm/include/llvm/Object/IRSymtab.h | 4 | ||||
| -rw-r--r-- | llvm/lib/LTO/LTO.cpp | 1 | ||||
| -rw-r--r-- | llvm/lib/Object/IRSymtab.cpp | 1 | ||||
| -rw-r--r-- | llvm/test/LTO/Resolution/X86/symtab-elf.ll | 15 | ||||
| -rw-r--r-- | llvm/test/LTO/Resolution/X86/symtab.ll | 3 | ||||
| -rw-r--r-- | llvm/tools/llvm-lto2/llvm-lto2.cpp | 9 |
10 files changed, 64 insertions, 44 deletions
diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp index f9befa3732a..cb56e13014d 100644 --- a/lld/COFF/InputFiles.cpp +++ b/lld/COFF/InputFiles.cpp @@ -19,7 +19,6 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Triple.h" #include "llvm/ADT/Twine.h" -#include "llvm/Bitcode/BitcodeReader.h" #include "llvm/Object/Binary.h" #include "llvm/Object/COFF.h" #include "llvm/Support/COFF.h" @@ -364,10 +363,7 @@ void BitcodeFile::parse() { } MachineTypes BitcodeFile::getMachineType() { - Expected<std::string> ET = getBitcodeTargetTriple(MB); - if (!ET) - return IMAGE_FILE_MACHINE_UNKNOWN; - switch (Triple(*ET).getArch()) { + switch (Triple(Obj->getTargetTriple()).getArch()) { case Triple::x86_64: return AMD64; case Triple::x86: diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 8c8905a2743..d651fbcad25 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -16,7 +16,6 @@ #include "Symbols.h" #include "SyntheticSections.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/Bitcode/BitcodeReader.h" #include "llvm/CodeGen/Analysis.h" #include "llvm/DebugInfo/DWARF/DWARFContext.h" #include "llvm/IR/LLVMContext.h" @@ -760,15 +759,13 @@ template <class ELFT> void SharedFile<ELFT>::parseRest() { } } -static ELFKind getBitcodeELFKind(MemoryBufferRef MB) { - Triple T(check(getBitcodeTargetTriple(MB), MB.getBufferIdentifier())); +static ELFKind getBitcodeELFKind(const Triple &T) { if (T.isLittleEndian()) return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind; return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind; } -static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) { - Triple T(check(getBitcodeTargetTriple(MB), MB.getBufferIdentifier())); +static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) { switch (T.getArch()) { case Triple::aarch64: return EM_AARCH64; @@ -789,15 +786,32 @@ static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) { case Triple::x86_64: return EM_X86_64; default: - fatal(MB.getBufferIdentifier() + - ": could not infer e_machine from bitcode target triple " + T.str()); + fatal(Path + ": could not infer e_machine from bitcode target triple " + + T.str()); } } -BitcodeFile::BitcodeFile(MemoryBufferRef MB, uint64_t OffsetInArchive) - : InputFile(BitcodeKind, MB), OffsetInArchive(OffsetInArchive) { - EKind = getBitcodeELFKind(MB); - EMachine = getBitcodeMachineKind(MB); +BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName, + uint64_t OffsetInArchive) + : InputFile(BitcodeKind, MB) { + this->ArchiveName = ArchiveName; + + // Here we pass a new MemoryBufferRef which is identified by ArchiveName + // (the fully resolved path of the archive) + member name + offset of the + // member in the archive. + // ThinLTO uses the MemoryBufferRef identifier to access its internal + // data structures and if two archives define two members with the same name, + // this causes a collision which result in only one of the objects being + // taken into consideration at LTO time (which very likely causes undefined + // symbols later in the link stage). + MemoryBufferRef MBRef(MB.getBuffer(), + Saver.save(ArchiveName + MB.getBufferIdentifier() + + utostr(OffsetInArchive))); + Obj = check(lto::InputFile::create(MBRef), toString(this)); + + Triple T(Obj->getTargetTriple()); + EKind = getBitcodeELFKind(T); + EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T); } static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) { @@ -845,20 +859,6 @@ static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats, template <class ELFT> void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) { - - // Here we pass a new MemoryBufferRef which is identified by ArchiveName - // (the fully resolved path of the archive) + member name + offset of the - // member in the archive. - // ThinLTO uses the MemoryBufferRef identifier to access its internal - // data structures and if two archives define two members with the same name, - // this causes a collision which result in only one of the objects being - // taken into consideration at LTO time (which very likely causes undefined - // symbols later in the link stage). - MemoryBufferRef MBRef(MB.getBuffer(), - Saver.save(ArchiveName + MB.getBufferIdentifier() + - utostr(OffsetInArchive))); - Obj = check(lto::InputFile::create(MBRef), toString(this)); - std::vector<bool> KeptComdats; for (StringRef S : Obj->getComdatTable()) KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second); @@ -931,8 +931,9 @@ static bool isBitcode(MemoryBufferRef MB) { InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName, uint64_t OffsetInArchive) { - InputFile *F = isBitcode(MB) ? make<BitcodeFile>(MB, OffsetInArchive) - : createELFFile<ObjectFile>(MB); + InputFile *F = isBitcode(MB) + ? make<BitcodeFile>(MB, ArchiveName, OffsetInArchive) + : createELFFile<ObjectFile>(MB); F->ArchiveName = ArchiveName; return F; } diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 975f9b4c9b5..40a8b23c5ef 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -260,19 +260,14 @@ private: class BitcodeFile : public InputFile { public: - BitcodeFile(MemoryBufferRef M, uint64_t OffsetInArchive); + BitcodeFile(MemoryBufferRef M, StringRef ArchiveName, + uint64_t OffsetInArchive); static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; } template <class ELFT> void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups); ArrayRef<Symbol *> getSymbols() { return Symbols; } std::unique_ptr<llvm::lto::InputFile> Obj; - // If this file is in an archive, the member contains the offset of - // the file in the archive. Otherwise, it's just zero. We store this - // field so that we can pass it to lib/LTO in order to disambiguate - // between objects. - uint64_t OffsetInArchive; - private: std::vector<Symbol *> Symbols; }; diff --git a/llvm/include/llvm/LTO/LTO.h b/llvm/include/llvm/LTO/LTO.h index 8c083947b84..3772592757b 100644 --- a/llvm/include/llvm/LTO/LTO.h +++ b/llvm/include/llvm/LTO/LTO.h @@ -97,7 +97,7 @@ private: // [begin, end) for each module std::vector<std::pair<size_t, size_t>> ModuleSymIndices; - StringRef SourceFileName, COFFLinkerOpts; + StringRef TargetTriple, SourceFileName, COFFLinkerOpts; std::vector<StringRef> ComdatTable; public: @@ -138,6 +138,9 @@ public: /// Returns the path to the InputFile. StringRef getName() const; + /// Returns the input file's target triple. + StringRef getTargetTriple() const { return TargetTriple; } + /// Returns the source file path specified at compile time. StringRef getSourceFileName() const { return SourceFileName; } diff --git a/llvm/include/llvm/Object/IRSymtab.h b/llvm/include/llvm/Object/IRSymtab.h index 98ef3ffcc97..cde6f3b0f65 100644 --- a/llvm/include/llvm/Object/IRSymtab.h +++ b/llvm/include/llvm/Object/IRSymtab.h @@ -116,7 +116,7 @@ struct Header { Range<Symbol> Symbols; Range<Uncommon> Uncommons; - Str SourceFileName; + Str TargetTriple, SourceFileName; /// COFF-specific: linker directives. Str COFFLinkerOpts; @@ -227,6 +227,8 @@ public: /// copied into an irsymtab::Symbol object. symbol_range module_symbols(unsigned I) const; + StringRef getTargetTriple() const { return str(header().TargetTriple); } + /// Returns the source file path specified at compile time. StringRef getSourceFileName() const { return str(header().SourceFileName); } diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index e6fc8cc8853..9782c898bf5 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -351,6 +351,7 @@ Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) { irsymtab::Reader R({Symtab.data(), Symtab.size()}, {File->Strtab.data(), File->Strtab.size()}); + File->TargetTriple = R.getTargetTriple(); File->SourceFileName = R.getSourceFileName(); File->COFFLinkerOpts = R.getCOFFLinkerOpts(); File->ComdatTable = R.getComdatTable(); diff --git a/llvm/lib/Object/IRSymtab.cpp b/llvm/lib/Object/IRSymtab.cpp index f78b84b388a..da1ef9946b5 100644 --- a/llvm/lib/Object/IRSymtab.cpp +++ b/llvm/lib/Object/IRSymtab.cpp @@ -190,6 +190,7 @@ Error Builder::build(ArrayRef<Module *> IRMods) { storage::Header Hdr; assert(!IRMods.empty()); + setStr(Hdr.TargetTriple, IRMods[0]->getTargetTriple()); setStr(Hdr.SourceFileName, IRMods[0]->getSourceFileName()); TT = Triple(IRMods[0]->getTargetTriple()); diff --git a/llvm/test/LTO/Resolution/X86/symtab-elf.ll b/llvm/test/LTO/Resolution/X86/symtab-elf.ll new file mode 100644 index 00000000000..1683b061c6d --- /dev/null +++ b/llvm/test/LTO/Resolution/X86/symtab-elf.ll @@ -0,0 +1,15 @@ +; RUN: llvm-as -o %t %s +; RUN: llvm-lto2 dump-symtab %t | FileCheck %s + +; CHECK: target triple: x86_64-unknown-linux-gnu +target triple = "x86_64-unknown-linux-gnu" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +; CHECK-NOT: linker opts: +!0 = !{i32 6, !"Linker Options", !{!{!"/include:foo"}}} +!llvm.module.flags = !{ !0 } + +@g1 = global i32 0 + +; CHECK-NOT: fallback g1 +@g2 = weak alias i32, i32* @g1 diff --git a/llvm/test/LTO/Resolution/X86/symtab.ll b/llvm/test/LTO/Resolution/X86/symtab.ll index e2729ac918a..b7bc1174901 100644 --- a/llvm/test/LTO/Resolution/X86/symtab.ll +++ b/llvm/test/LTO/Resolution/X86/symtab.ll @@ -1,13 +1,14 @@ ; RUN: llvm-as -o %t %s ; RUN: llvm-lto2 dump-symtab %t | FileCheck %s +; CHECK: target triple: i686-pc-windows-msvc18.0.0 target triple = "i686-pc-windows-msvc18.0.0" target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32" ; CHECK: source filename: src.c source_filename = "src.c" -; CHECK: linker opts (COFF only): /include:foo +; CHECK: linker opts: /include:foo !0 = !{i32 6, !"Linker Options", !{!{!"/include:foo"}}} !llvm.module.flags = !{ !0 } diff --git a/llvm/tools/llvm-lto2/llvm-lto2.cpp b/llvm/tools/llvm-lto2/llvm-lto2.cpp index 7a8d4055338..3d2643db85b 100644 --- a/llvm/tools/llvm-lto2/llvm-lto2.cpp +++ b/llvm/tools/llvm-lto2/llvm-lto2.cpp @@ -293,8 +293,13 @@ static int dumpSymtab(int argc, char **argv) { std::unique_ptr<InputFile> Input = check(InputFile::create(MB->getMemBufferRef()), F); + outs() << "target triple: " << Input->getTargetTriple() << '\n'; + Triple TT(Input->getTargetTriple()); + outs() << "source filename: " << Input->getSourceFileName() << '\n'; - outs() << "linker opts (COFF only): " << Input->getCOFFLinkerOpts() << '\n'; + + if (TT.isOSBinFormatCOFF()) + outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n'; std::vector<StringRef> ComdatTable = Input->getComdatTable(); for (const InputFile::Symbol &Sym : Input->symbols()) { @@ -328,7 +333,7 @@ static int dumpSymtab(int argc, char **argv) { if (Comdat != -1) outs() << " comdat " << ComdatTable[Comdat] << '\n'; - if (Sym.isWeak() && Sym.isIndirect()) + if (TT.isOSBinFormatCOFF() && Sym.isWeak() && Sym.isIndirect()) outs() << " fallback " << Sym.getCOFFWeakExternalFallback() << '\n'; } |

