1 //===- InputFiles.cpp -----------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "InputFiles.h"
12 #include "InputSection.h"
13 #include "LinkerScript.h"
15 #include "SymbolTable.h"
17 #include "SyntheticSections.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Bitcode/BitcodeReader.h"
20 #include "llvm/CodeGen/Analysis.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/LTO/LTO.h"
25 #include "llvm/MC/StringTableBuilder.h"
26 #include "llvm/Object/ELFObjectFile.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/TarWriter.h"
29 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm::ELF;
33 using namespace llvm::object;
34 using namespace llvm::sys::fs;
37 using namespace lld::elf;
42 // In ELF object file all section addresses are zero. If we have multiple
43 // .text sections (when using -ffunction-section or comdat group) then
44 // LLVM DWARF parser will not be able to parse .debug_line correctly, unless
45 // we assign each section some unique address. This callback method assigns
46 // each section an address equal to its offset in ELF object file.
47 class ObjectInfo : public LoadedObjectInfo {
49 uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override {
50 return static_cast<const ELFSectionRef &>(Sec).getOffset();
52 std::unique_ptr<LoadedObjectInfo> clone() const override {
53 return std::unique_ptr<LoadedObjectInfo>();
58 Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
60 outs() << Path << "\n";
62 auto MBOrErr = MemoryBuffer::getFile(Path);
63 if (auto EC = MBOrErr.getError()) {
64 error(EC, "cannot open " + Path);
67 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
68 MemoryBufferRef MBRef = MB->getMemBufferRef();
69 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
72 Tar->append(relativeToRoot(Path), MBRef.getBuffer());
76 template <class ELFT> void elf::ObjectFile<ELFT>::initializeDwarfLine() {
77 std::unique_ptr<object::ObjectFile> Obj =
78 check(object::ObjectFile::createObjectFile(this->MB),
79 "createObjectFile failed");
82 DWARFContextInMemory Dwarf(*Obj, &ObjInfo);
83 DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs));
84 DataExtractor LineData(Dwarf.getLineSection().Data,
85 ELFT::TargetEndianness == support::little,
86 ELFT::Is64Bits ? 8 : 4);
88 // The second parameter is offset in .debug_line section
89 // for compilation unit (CU) of interest. We have only one
90 // CU (object file), so offset is always 0.
91 DwarfLine->getOrParseLineTable(LineData, 0);
94 // Returns source line information for a given offset
95 // using DWARF debug info.
97 std::string elf::ObjectFile<ELFT>::getLineInfo(InputSectionBase<ELFT> *S,
100 initializeDwarfLine();
102 // The offset to CU is 0.
103 const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
107 // Use fake address calcuated by adding section file offset and offset in
108 // section. See comments for ObjectInfo class.
110 Tbl->getFileLineInfoForAddress(
111 S->Offset + Offset, nullptr,
112 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
115 return Info.FileName + ":" + std::to_string(Info.Line);
118 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
119 std::string lld::toString(const InputFile *F) {
122 if (!F->ArchiveName.empty())
123 return (F->ArchiveName + "(" + F->getName() + ")").str();
127 template <class ELFT> static ELFKind getELFKind() {
128 if (ELFT::TargetEndianness == support::little)
129 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
130 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
133 template <class ELFT>
134 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
135 EKind = getELFKind<ELFT>();
136 EMachine = getObj().getHeader()->e_machine;
137 OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
140 template <class ELFT>
141 typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalSymbols() {
142 return makeArrayRef(Symbols.begin() + FirstNonLocal, Symbols.end());
145 template <class ELFT>
146 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
147 return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX));
150 template <class ELFT>
151 void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
152 const Elf_Shdr *Symtab) {
153 FirstNonLocal = Symtab->sh_info;
154 Symbols = check(getObj().symbols(Symtab));
155 if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size())
156 fatal(toString(this) + ": invalid sh_info in symbol table");
158 StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections));
161 template <class ELFT>
162 elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
163 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
165 template <class ELFT>
166 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
167 return makeArrayRef(this->SymbolBodies).slice(this->FirstNonLocal);
170 template <class ELFT>
171 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
172 if (this->SymbolBodies.empty())
173 return this->SymbolBodies;
174 return makeArrayRef(this->SymbolBodies).slice(1, this->FirstNonLocal - 1);
177 template <class ELFT>
178 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
179 if (this->SymbolBodies.empty())
180 return this->SymbolBodies;
181 return makeArrayRef(this->SymbolBodies).slice(1);
184 template <class ELFT>
185 void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
186 // Read section and symbol tables.
187 initializeSections(ComdatGroups);
191 // Sections with SHT_GROUP and comdat bits define comdat section groups.
192 // They are identified and deduplicated by group name. This function
193 // returns a group name.
194 template <class ELFT>
196 elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
197 const Elf_Shdr &Sec) {
198 if (this->Symbols.empty())
199 this->initSymtab(Sections,
200 check(object::getSection<ELFT>(Sections, Sec.sh_link)));
202 check(object::getSymbol<ELFT>(this->Symbols, Sec.sh_info));
203 return check(Sym->getName(this->StringTable));
206 template <class ELFT>
207 ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
208 elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
209 const ELFFile<ELFT> &Obj = this->getObj();
210 ArrayRef<Elf_Word> Entries =
211 check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
212 if (Entries.empty() || Entries[0] != GRP_COMDAT)
213 fatal(toString(this) + ": unsupported SHT_GROUP format");
214 return Entries.slice(1);
217 template <class ELFT>
218 bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
219 // We don't merge sections if -O0 (default is -O1). This makes sometimes
220 // the linker significantly faster, although the output will be bigger.
221 if (Config->Optimize == 0)
224 // Do not merge sections if generating a relocatable object. It makes
225 // the code simpler because we do not need to update relocation addends
226 // to reflect changes introduced by merging. Instead of that we write
227 // such "merge" sections into separate OutputSections and keep SHF_MERGE
228 // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
229 // later during a final linking.
230 if (Config->Relocatable)
233 // A mergeable section with size 0 is useless because they don't have
234 // any data to merge. A mergeable string section with size 0 can be
235 // argued as invalid because it doesn't end with a null character.
236 // We'll avoid a mess by handling them as if they were non-mergeable.
237 if (Sec.sh_size == 0)
240 // Check for sh_entsize. The ELF spec is not clear about the zero
241 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
242 // the section does not hold a table of fixed-size entries". We know
243 // that Rust 1.13 produces a string mergeable section with a zero
244 // sh_entsize. Here we just accept it rather than being picky about it.
245 uintX_t EntSize = Sec.sh_entsize;
248 if (Sec.sh_size % EntSize)
249 fatal(toString(this) +
250 ": SHF_MERGE section size must be a multiple of sh_entsize");
252 uintX_t Flags = Sec.sh_flags;
253 if (!(Flags & SHF_MERGE))
255 if (Flags & SHF_WRITE)
256 fatal(toString(this) + ": writable SHF_MERGE section is not supported");
258 // Don't try to merge if the alignment is larger than the sh_entsize and this
259 // is not SHF_STRINGS.
261 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
262 // It would be equivalent for the producer of the .o to just set a larger
264 if (Flags & SHF_STRINGS)
267 return Sec.sh_addralign <= EntSize;
270 template <class ELFT>
271 void elf::ObjectFile<ELFT>::initializeSections(
272 DenseSet<CachedHashStringRef> &ComdatGroups) {
273 ArrayRef<Elf_Shdr> ObjSections = check(this->getObj().sections());
274 const ELFFile<ELFT> &Obj = this->getObj();
275 uint64_t Size = ObjSections.size();
276 Sections.resize(Size);
278 StringRef SectionStringTable = check(Obj.getSectionStringTable(ObjSections));
279 for (const Elf_Shdr &Sec : ObjSections) {
281 if (Sections[I] == &InputSection<ELFT>::Discarded)
284 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
285 // if -r is given, we'll let the final link discard such sections.
286 // This is compatible with GNU.
287 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
288 Sections[I] = &InputSection<ELFT>::Discarded;
292 switch (Sec.sh_type) {
294 Sections[I] = &InputSection<ELFT>::Discarded;
295 if (ComdatGroups.insert(CachedHashStringRef(
296 getShtGroupSignature(ObjSections, Sec)))
299 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
300 if (SecIndex >= Size)
301 fatal(toString(this) + ": invalid section index in group: " +
303 Sections[SecIndex] = &InputSection<ELFT>::Discarded;
307 this->initSymtab(ObjSections, &Sec);
309 case SHT_SYMTAB_SHNDX:
310 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, ObjSections));
316 Sections[I] = createInputSection(Sec, SectionStringTable);
319 // .ARM.exidx sections have a reverse dependency on the InputSection they
320 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
321 if (Sec.sh_flags & SHF_LINK_ORDER) {
322 if (Sec.sh_link >= Sections.size())
323 fatal(toString(this) + ": invalid sh_link index: " +
325 auto *IS = cast<InputSection<ELFT>>(Sections[Sec.sh_link]);
326 IS->DependentSection = Sections[I];
331 template <class ELFT>
332 InputSectionBase<ELFT> *
333 elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
334 uint32_t Idx = Sec.sh_info;
335 if (Idx >= Sections.size())
336 fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
337 InputSectionBase<ELFT> *Target = Sections[Idx];
339 // Strictly speaking, a relocation section must be included in the
340 // group of the section it relocates. However, LLVM 3.3 and earlier
341 // would fail to do so, so we gracefully handle that case.
342 if (Target == &InputSection<ELFT>::Discarded)
346 fatal(toString(this) + ": unsupported relocation reference");
350 template <class ELFT>
351 InputSectionBase<ELFT> *
352 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec,
353 StringRef SectionStringTable) {
355 check(this->getObj().getSectionName(&Sec, SectionStringTable));
357 switch (Sec.sh_type) {
358 case SHT_ARM_ATTRIBUTES:
359 // FIXME: ARM meta-data section. Retain the first attribute section
360 // we see. The eglibc ARM dynamic loaders require the presence of an
361 // attribute section for dlopen to work.
362 // In a full implementation we would merge all attribute sections.
363 if (In<ELFT>::ARMAttributes == nullptr) {
364 In<ELFT>::ARMAttributes = make<InputSection<ELFT>>(this, &Sec, Name);
365 return In<ELFT>::ARMAttributes;
367 return &InputSection<ELFT>::Discarded;
370 // This section contains relocation information.
371 // If -r is given, we do not interpret or apply relocation
372 // but just copy relocation sections to output.
373 if (Config->Relocatable)
374 return make<InputSection<ELFT>>(this, &Sec, Name);
376 // Find the relocation target section and associate this
378 InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
381 if (Target->FirstRelocation)
382 fatal(toString(this) +
383 ": multiple relocation sections to one section are not supported");
384 if (!isa<InputSection<ELFT>>(Target) && !isa<EhInputSection<ELFT>>(Target))
385 fatal(toString(this) +
386 ": relocations pointing to SHF_MERGE are not supported");
388 size_t NumRelocations;
389 if (Sec.sh_type == SHT_RELA) {
390 ArrayRef<Elf_Rela> Rels = check(this->getObj().relas(&Sec));
391 Target->FirstRelocation = Rels.begin();
392 NumRelocations = Rels.size();
393 Target->AreRelocsRela = true;
395 ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec));
396 Target->FirstRelocation = Rels.begin();
397 NumRelocations = Rels.size();
398 Target->AreRelocsRela = false;
400 assert(isUInt<31>(NumRelocations));
401 Target->NumRelocations = NumRelocations;
406 // .note.GNU-stack is a marker section to control the presence of
407 // PT_GNU_STACK segment in outputs. Since the presence of the segment
408 // is controlled only by the command line option (-z execstack) in LLD,
409 // .note.GNU-stack is ignored.
410 if (Name == ".note.GNU-stack")
411 return &InputSection<ELFT>::Discarded;
413 if (Name == ".note.GNU-split-stack") {
414 error("objects using splitstacks are not supported");
415 return &InputSection<ELFT>::Discarded;
418 if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
419 return &InputSection<ELFT>::Discarded;
421 // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
422 // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
423 // sections. Drop those sections to avoid duplicate symbol errors.
424 // FIXME: This is glibc PR20543, we should remove this hack once that has been
425 // fixed for a while.
426 if (Name.startswith(".gnu.linkonce."))
427 return &InputSection<ELFT>::Discarded;
429 // The linker merges EH (exception handling) frames and creates a
430 // .eh_frame_hdr section for runtime. So we handle them with a special
431 // class. For relocatable outputs, they are just passed through.
432 if (Name == ".eh_frame" && !Config->Relocatable)
433 return make<EhInputSection<ELFT>>(this, &Sec, Name);
435 if (shouldMerge(Sec))
436 return make<MergeInputSection<ELFT>>(this, &Sec, Name);
437 return make<InputSection<ELFT>>(this, &Sec, Name);
440 template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
441 SymbolBodies.reserve(this->Symbols.size());
442 for (const Elf_Sym &Sym : this->Symbols)
443 SymbolBodies.push_back(createSymbolBody(&Sym));
446 template <class ELFT>
447 InputSectionBase<ELFT> *
448 elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
449 uint32_t Index = this->getSectionIndex(Sym);
450 if (Index >= Sections.size())
451 fatal(toString(this) + ": invalid section index: " + Twine(Index));
452 InputSectionBase<ELFT> *S = Sections[Index];
454 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
455 // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
456 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
457 // In this case it is fine for section to be null here as we do not
458 // allocate sections of these types.
460 if (Index == 0 || Sym.getType() == STT_SECTION ||
461 Sym.getType() == STT_NOTYPE)
463 fatal(toString(this) + ": invalid section index: " + Twine(Index));
466 if (S == &InputSection<ELFT>::Discarded)
471 template <class ELFT>
472 SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
473 int Binding = Sym->getBinding();
474 InputSectionBase<ELFT> *Sec = getSection(*Sym);
476 uint8_t StOther = Sym->st_other;
477 uint8_t Type = Sym->getType();
478 uintX_t Value = Sym->st_value;
479 uintX_t Size = Sym->st_size;
481 if (Binding == STB_LOCAL) {
482 if (Sym->getType() == STT_FILE)
483 SourceFile = check(Sym->getName(this->StringTable));
485 if (this->StringTable.size() <= Sym->st_name)
486 fatal(toString(this) + ": invalid symbol name offset");
488 StringRefZ Name = this->StringTable.data() + Sym->st_name;
489 if (Sym->st_shndx == SHN_UNDEF)
491 Undefined<ELFT>(Name, /*IsLocal=*/true, StOther, Type, this);
493 return new (BAlloc) DefinedRegular<ELFT>(Name, /*IsLocal=*/true, StOther,
494 Type, Value, Size, Sec, this);
497 StringRef Name = check(Sym->getName(this->StringTable));
499 switch (Sym->st_shndx) {
501 return elf::Symtab<ELFT>::X
502 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type,
503 /*CanOmitFromDynSym=*/false, this)
506 if (Value == 0 || Value >= UINT32_MAX)
507 fatal(toString(this) + ": common symbol '" + Name +
508 "' has invalid alignment: " + Twine(Value));
509 return elf::Symtab<ELFT>::X
510 ->addCommon(Name, Size, Value, Binding, StOther, Type, this)
516 fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
520 if (Sec == &InputSection<ELFT>::Discarded)
521 return elf::Symtab<ELFT>::X
522 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type,
523 /*CanOmitFromDynSym=*/false, this)
525 return elf::Symtab<ELFT>::X
526 ->addRegular(Name, StOther, Type, Value, Size, Binding, Sec, this)
531 template <class ELFT> void ArchiveFile::parse() {
532 File = check(Archive::create(MB),
533 MB.getBufferIdentifier() + ": failed to parse archive");
535 // Read the symbol table to construct Lazy objects.
536 for (const Archive::Symbol &Sym : File->symbols())
537 Symtab<ELFT>::X->addLazyArchive(this, Sym);
540 // Returns a buffer pointing to a member file containing a given symbol.
541 std::pair<MemoryBufferRef, uint64_t>
542 ArchiveFile::getMember(const Archive::Symbol *Sym) {
544 check(Sym->getMember(),
545 "could not get the member for symbol " + Sym->getName());
547 if (!Seen.insert(C.getChildOffset()).second)
548 return {MemoryBufferRef(), 0};
550 MemoryBufferRef Ret =
551 check(C.getMemoryBufferRef(),
552 "could not get the buffer for the member defining symbol " +
555 if (C.getParent()->isThin() && Tar)
556 Tar->append(relativeToRoot(check(C.getFullName())), Ret.getBuffer());
557 if (C.getParent()->isThin())
559 return {Ret, C.getChildOffset()};
562 template <class ELFT>
563 SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
564 : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
566 template <class ELFT>
567 const typename ELFT::Shdr *
568 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
570 this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX));
573 // Partially parse the shared object file so that we can call
574 // getSoName on this object.
575 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
576 const Elf_Shdr *DynamicSec = nullptr;
578 const ELFFile<ELFT> Obj = this->getObj();
579 ArrayRef<Elf_Shdr> Sections = check(Obj.sections());
580 for (const Elf_Shdr &Sec : Sections) {
581 switch (Sec.sh_type) {
585 this->initSymtab(Sections, &Sec);
590 case SHT_SYMTAB_SHNDX:
591 this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, Sections));
594 this->VersymSec = &Sec;
597 this->VerdefSec = &Sec;
602 if (this->VersymSec && this->Symbols.empty())
603 error("SHT_GNU_versym should be associated with symbol table");
605 // DSOs are identified by soname, and they usually contain
606 // DT_SONAME tag in their header. But if they are missing,
607 // filenames are used as default sonames.
608 SoName = sys::path::filename(this->getName());
613 ArrayRef<Elf_Dyn> Arr =
614 check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
615 toString(this) + ": getSectionContentsAsArray failed");
616 for (const Elf_Dyn &Dyn : Arr) {
617 if (Dyn.d_tag == DT_SONAME) {
618 uintX_t Val = Dyn.getVal();
619 if (Val >= this->StringTable.size())
620 fatal(toString(this) + ": invalid DT_SONAME entry");
621 SoName = StringRef(this->StringTable.data() + Val);
627 // Parse the version definitions in the object file if present. Returns a vector
628 // whose nth element contains a pointer to the Elf_Verdef for version identifier
629 // n. Version identifiers that are not definitions map to nullptr. The array
630 // always has at least length 1.
631 template <class ELFT>
632 std::vector<const typename ELFT::Verdef *>
633 SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
634 std::vector<const Elf_Verdef *> Verdefs(1);
635 // We only need to process symbol versions for this DSO if it has both a
636 // versym and a verdef section, which indicates that the DSO contains symbol
637 // version definitions.
638 if (!VersymSec || !VerdefSec)
641 // The location of the first global versym entry.
642 const char *Base = this->MB.getBuffer().data();
643 Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
646 // We cannot determine the largest verdef identifier without inspecting
647 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
648 // sequentially starting from 1, so we predict that the largest identifier
649 // will be VerdefCount.
650 unsigned VerdefCount = VerdefSec->sh_info;
651 Verdefs.resize(VerdefCount + 1);
653 // Build the Verdefs array by following the chain of Elf_Verdef objects
654 // from the start of the .gnu.version_d section.
655 const char *Verdef = Base + VerdefSec->sh_offset;
656 for (unsigned I = 0; I != VerdefCount; ++I) {
657 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
658 Verdef += CurVerdef->vd_next;
659 unsigned VerdefIndex = CurVerdef->vd_ndx;
660 if (Verdefs.size() <= VerdefIndex)
661 Verdefs.resize(VerdefIndex + 1);
662 Verdefs[VerdefIndex] = CurVerdef;
668 // Fully parse the shared object file. This must be called after parseSoName().
669 template <class ELFT> void SharedFile<ELFT>::parseRest() {
670 // Create mapping from version identifiers to Elf_Verdef entries.
671 const Elf_Versym *Versym = nullptr;
672 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
674 Elf_Sym_Range Syms = this->getGlobalSymbols();
675 for (const Elf_Sym &Sym : Syms) {
676 unsigned VersymIndex = 0;
678 VersymIndex = Versym->vs_index;
681 bool Hidden = VersymIndex & VERSYM_HIDDEN;
682 VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
684 StringRef Name = check(Sym.getName(this->StringTable));
685 if (Sym.isUndefined()) {
686 Undefs.push_back(Name);
690 // Ignore local symbols.
691 if (Versym && VersymIndex == VER_NDX_LOCAL)
694 const Elf_Verdef *V =
695 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
698 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
700 // Also add the symbol with the versioned name to handle undefined symbols
701 // with explicit versions.
703 StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
704 Name = Saver.save(Twine(Name) + "@" + VerName);
705 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
710 static ELFKind getBitcodeELFKind(MemoryBufferRef MB) {
711 Triple T(check(getBitcodeTargetTriple(MB)));
712 if (T.isLittleEndian())
713 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
714 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
717 static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) {
718 Triple T(check(getBitcodeTargetTriple(MB)));
719 switch (T.getArch()) {
720 case Triple::aarch64:
727 case Triple::mips64el:
734 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
738 fatal(MB.getBufferIdentifier() +
739 ": could not infer e_machine from bitcode target triple " + T.str());
743 BitcodeFile::BitcodeFile(MemoryBufferRef MB) : InputFile(BitcodeKind, MB) {
744 EKind = getBitcodeELFKind(MB);
745 EMachine = getBitcodeMachineKind(MB);
748 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
749 switch (GvVisibility) {
750 case GlobalValue::DefaultVisibility:
752 case GlobalValue::HiddenVisibility:
754 case GlobalValue::ProtectedVisibility:
755 return STV_PROTECTED;
757 llvm_unreachable("unknown visibility");
760 template <class ELFT>
761 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
762 const lto::InputFile::Symbol &ObjSym,
764 StringRef NameRef = Saver.save(ObjSym.getName());
765 uint32_t Flags = ObjSym.getFlags();
766 uint32_t Binding = (Flags & BasicSymbolRef::SF_Weak) ? STB_WEAK : STB_GLOBAL;
768 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
769 uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
770 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
772 int C = check(ObjSym.getComdatIndex());
773 if (C != -1 && !KeptComdats[C])
774 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding,
775 Visibility, Type, CanOmitFromDynSym,
778 if (Flags & BasicSymbolRef::SF_Undefined)
779 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding,
780 Visibility, Type, CanOmitFromDynSym,
783 if (Flags & BasicSymbolRef::SF_Common)
784 return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(),
785 ObjSym.getCommonAlignment(), Binding,
786 Visibility, STT_OBJECT, F);
788 return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
789 CanOmitFromDynSym, F);
792 template <class ELFT>
793 void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
795 // Here we pass a new MemoryBufferRef which is identified by ArchiveName
796 // (the fully resolved path of the archive) + member name + offset of the
797 // member in the archive.
798 // ThinLTO uses the MemoryBufferRef identifier to access its internal
799 // data structures and if two archives define two members with the same name,
800 // this causes a collision which result in only one of the objects being
801 // taken into consideration at LTO time (which very likely causes undefined
802 // symbols later in the link stage).
803 Obj = check(lto::InputFile::create(MemoryBufferRef(
804 MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier() +
805 utostr(OffsetInArchive)))));
807 std::vector<bool> KeptComdats;
808 for (StringRef S : Obj->getComdatTable()) {
809 StringRef N = Saver.save(S);
810 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(N)).second);
813 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
814 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this));
817 template <template <class> class T>
818 static InputFile *createELFFile(MemoryBufferRef MB) {
820 unsigned char Endian;
821 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
822 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
823 fatal(MB.getBufferIdentifier() + ": invalid data encoding");
825 size_t BufSize = MB.getBuffer().size();
826 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
827 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
828 fatal(MB.getBufferIdentifier() + ": file is too short");
831 if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
832 Obj = make<T<ELF32LE>>(MB);
833 else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
834 Obj = make<T<ELF32BE>>(MB);
835 else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
836 Obj = make<T<ELF64LE>>(MB);
837 else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
838 Obj = make<T<ELF64BE>>(MB);
840 fatal(MB.getBufferIdentifier() + ": invalid file class");
842 if (!Config->FirstElf)
843 Config->FirstElf = Obj;
847 template <class ELFT> void BinaryFile::parse() {
848 StringRef Buf = MB.getBuffer();
849 ArrayRef<uint8_t> Data =
850 makeArrayRef<uint8_t>((const uint8_t *)Buf.data(), Buf.size());
852 std::string Filename = MB.getBufferIdentifier();
853 std::transform(Filename.begin(), Filename.end(), Filename.begin(),
854 [](char C) { return isalnum(C) ? C : '_'; });
855 Filename = "_binary_" + Filename;
856 StringRef StartName = Saver.save(Twine(Filename) + "_start");
857 StringRef EndName = Saver.save(Twine(Filename) + "_end");
858 StringRef SizeName = Saver.save(Twine(Filename) + "_size");
860 auto *Section = make<InputSection<ELFT>>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
862 Sections.push_back(Section);
864 elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, STT_OBJECT, 0, 0,
865 STB_GLOBAL, Section, nullptr);
866 elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, STT_OBJECT,
867 Data.size(), 0, STB_GLOBAL, Section,
869 elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, STT_OBJECT,
870 Data.size(), 0, STB_GLOBAL, nullptr,
874 static bool isBitcode(MemoryBufferRef MB) {
875 using namespace sys::fs;
876 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
879 InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
880 uint64_t OffsetInArchive) {
882 isBitcode(MB) ? make<BitcodeFile>(MB) : createELFFile<ObjectFile>(MB);
883 F->ArchiveName = ArchiveName;
884 F->OffsetInArchive = OffsetInArchive;
888 InputFile *elf::createSharedFile(MemoryBufferRef MB) {
889 return createELFFile<SharedFile>(MB);
892 MemoryBufferRef LazyObjectFile::getBuffer() {
894 return MemoryBufferRef();
899 template <class ELFT> void LazyObjectFile::parse() {
900 for (StringRef Sym : getSymbols())
901 Symtab<ELFT>::X->addLazyObject(Sym, *this);
904 template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
905 typedef typename ELFT::Shdr Elf_Shdr;
906 typedef typename ELFT::Sym Elf_Sym;
907 typedef typename ELFT::SymRange Elf_Sym_Range;
909 const ELFFile<ELFT> Obj(this->MB.getBuffer());
910 ArrayRef<Elf_Shdr> Sections = check(Obj.sections());
911 for (const Elf_Shdr &Sec : Sections) {
912 if (Sec.sh_type != SHT_SYMTAB)
914 Elf_Sym_Range Syms = check(Obj.symbols(&Sec));
915 uint32_t FirstNonLocal = Sec.sh_info;
916 StringRef StringTable = check(Obj.getStringTableForSymtab(Sec, Sections));
917 std::vector<StringRef> V;
918 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
919 if (Sym.st_shndx != SHN_UNDEF)
920 V.push_back(check(Sym.getName(StringTable)));
926 std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
927 std::unique_ptr<lto::InputFile> Obj = check(lto::InputFile::create(this->MB));
928 std::vector<StringRef> V;
929 for (const lto::InputFile::Symbol &Sym : Obj->symbols())
930 if (!(Sym.getFlags() & BasicSymbolRef::SF_Undefined))
931 V.push_back(Saver.save(Sym.getName()));
935 // Returns a vector of globally-visible defined symbol names.
936 std::vector<StringRef> LazyObjectFile::getSymbols() {
937 if (isBitcode(this->MB))
938 return getBitcodeSymbols();
941 unsigned char Endian;
942 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
943 if (Size == ELFCLASS32) {
944 if (Endian == ELFDATA2LSB)
945 return getElfSymbols<ELF32LE>();
946 return getElfSymbols<ELF32BE>();
948 if (Endian == ELFDATA2LSB)
949 return getElfSymbols<ELF64LE>();
950 return getElfSymbols<ELF64BE>();
953 template void ArchiveFile::parse<ELF32LE>();
954 template void ArchiveFile::parse<ELF32BE>();
955 template void ArchiveFile::parse<ELF64LE>();
956 template void ArchiveFile::parse<ELF64BE>();
958 template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
959 template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
960 template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
961 template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
963 template void LazyObjectFile::parse<ELF32LE>();
964 template void LazyObjectFile::parse<ELF32BE>();
965 template void LazyObjectFile::parse<ELF64LE>();
966 template void LazyObjectFile::parse<ELF64BE>();
968 template class elf::ELFFileBase<ELF32LE>;
969 template class elf::ELFFileBase<ELF32BE>;
970 template class elf::ELFFileBase<ELF64LE>;
971 template class elf::ELFFileBase<ELF64BE>;
973 template class elf::ObjectFile<ELF32LE>;
974 template class elf::ObjectFile<ELF32BE>;
975 template class elf::ObjectFile<ELF64LE>;
976 template class elf::ObjectFile<ELF64BE>;
978 template class elf::SharedFile<ELF32LE>;
979 template class elf::SharedFile<ELF32BE>;
980 template class elf::SharedFile<ELF64LE>;
981 template class elf::SharedFile<ELF64BE>;
983 template void BinaryFile::parse<ELF32LE>();
984 template void BinaryFile::parse<ELF32BE>();
985 template void BinaryFile::parse<ELF64LE>();
986 template void BinaryFile::parse<ELF64BE>();