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/CodeGen/Analysis.h"
20 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/LTO/LTO.h"
24 #include "llvm/MC/StringTableBuilder.h"
25 #include "llvm/Object/ELFObjectFile.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/TarWriter.h"
28 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm::ELF;
32 using namespace llvm::object;
33 using namespace llvm::sys::fs;
36 using namespace lld::elf;
40 InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
43 // In ELF object file all section addresses are zero. If we have multiple
44 // .text sections (when using -ffunction-section or comdat group) then
45 // LLVM DWARF parser will not be able to parse .debug_line correctly, unless
46 // we assign each section some unique address. This callback method assigns
47 // each section an address equal to its offset in ELF object file.
48 class ObjectInfo : public LoadedObjectInfo {
50 uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override {
51 return static_cast<const ELFSectionRef &>(Sec).getOffset();
53 std::unique_ptr<LoadedObjectInfo> clone() const override {
54 return std::unique_ptr<LoadedObjectInfo>();
59 Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
61 auto MBOrErr = MemoryBuffer::getFile(Path);
62 if (auto EC = MBOrErr.getError()) {
63 error("cannot open " + Path + ": " + EC.message());
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), toString(this));
81 DWARFContextInMemory Dwarf(*Obj, &ObjInfo);
82 DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs));
83 DataExtractor LineData(Dwarf.getLineSection().Data, Config->IsLE,
86 // The second parameter is offset in .debug_line section
87 // for compilation unit (CU) of interest. We have only one
88 // CU (object file), so offset is always 0.
89 DwarfLine->getOrParseLineTable(LineData, 0);
92 // Returns source line information for a given offset
93 // using DWARF debug info.
95 Optional<DILineInfo> elf::ObjectFile<ELFT>::getDILineInfo(InputSectionBase *S,
98 initializeDwarfLine();
100 // The offset to CU is 0.
101 const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
105 // Use fake address calcuated by adding section file offset and offset in
106 // section. See comments for ObjectInfo class.
108 Tbl->getFileLineInfoForAddress(
109 S->getOffsetInFile() + Offset, nullptr,
110 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
116 // Returns source line information for a given offset
117 // using DWARF debug info.
118 template <class ELFT>
119 std::string elf::ObjectFile<ELFT>::getLineInfo(InputSectionBase *S,
121 if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
122 return Info->FileName + ":" + std::to_string(Info->Line);
126 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
127 std::string lld::toString(const InputFile *F) {
131 if (F->ToStringCache.empty()) {
132 if (F->ArchiveName.empty())
133 F->ToStringCache = F->getName();
135 F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str();
137 return F->ToStringCache;
140 template <class ELFT> static ELFKind getELFKind() {
141 if (ELFT::TargetEndianness == support::little)
142 return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
143 return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
146 template <class ELFT>
147 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
148 EKind = getELFKind<ELFT>();
149 EMachine = getObj().getHeader()->e_machine;
150 OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
153 template <class ELFT>
154 typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalSymbols() {
155 return makeArrayRef(Symbols.begin() + FirstNonLocal, Symbols.end());
158 template <class ELFT>
159 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
160 return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX),
164 template <class ELFT>
165 void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
166 const Elf_Shdr *Symtab) {
167 FirstNonLocal = Symtab->sh_info;
168 Symbols = check(getObj().symbols(Symtab), toString(this));
169 if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size())
170 fatal(toString(this) + ": invalid sh_info in symbol table");
172 StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections),
176 template <class ELFT>
177 elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
178 : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
180 template <class ELFT>
181 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
182 if (this->SymbolBodies.empty())
183 return this->SymbolBodies;
184 return makeArrayRef(this->SymbolBodies).slice(1, this->FirstNonLocal - 1);
187 template <class ELFT>
188 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
189 if (this->SymbolBodies.empty())
190 return this->SymbolBodies;
191 return makeArrayRef(this->SymbolBodies).slice(1);
194 template <class ELFT>
195 void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
196 // Read section and symbol tables.
197 initializeSections(ComdatGroups);
201 // Sections with SHT_GROUP and comdat bits define comdat section groups.
202 // They are identified and deduplicated by group name. This function
203 // returns a group name.
204 template <class ELFT>
206 elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
207 const Elf_Shdr &Sec) {
208 if (this->Symbols.empty())
211 check(object::getSection<ELFT>(Sections, Sec.sh_link), toString(this)));
212 const Elf_Sym *Sym = check(
213 object::getSymbol<ELFT>(this->Symbols, Sec.sh_info), toString(this));
214 return check(Sym->getName(this->StringTable), toString(this));
217 template <class ELFT>
218 ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
219 elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
220 const ELFFile<ELFT> &Obj = this->getObj();
221 ArrayRef<Elf_Word> Entries = check(
222 Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), toString(this));
223 if (Entries.empty() || Entries[0] != GRP_COMDAT)
224 fatal(toString(this) + ": unsupported SHT_GROUP format");
225 return Entries.slice(1);
228 template <class ELFT>
229 bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
230 // We don't merge sections if -O0 (default is -O1). This makes sometimes
231 // the linker significantly faster, although the output will be bigger.
232 if (Config->Optimize == 0)
235 // Do not merge sections if generating a relocatable object. It makes
236 // the code simpler because we do not need to update relocation addends
237 // to reflect changes introduced by merging. Instead of that we write
238 // such "merge" sections into separate OutputSections and keep SHF_MERGE
239 // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
240 // later during a final linking.
241 if (Config->Relocatable)
244 // A mergeable section with size 0 is useless because they don't have
245 // any data to merge. A mergeable string section with size 0 can be
246 // argued as invalid because it doesn't end with a null character.
247 // We'll avoid a mess by handling them as if they were non-mergeable.
248 if (Sec.sh_size == 0)
251 // Check for sh_entsize. The ELF spec is not clear about the zero
252 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
253 // the section does not hold a table of fixed-size entries". We know
254 // that Rust 1.13 produces a string mergeable section with a zero
255 // sh_entsize. Here we just accept it rather than being picky about it.
256 uint64_t EntSize = Sec.sh_entsize;
259 if (Sec.sh_size % EntSize)
260 fatal(toString(this) +
261 ": SHF_MERGE section size must be a multiple of sh_entsize");
263 uint64_t Flags = Sec.sh_flags;
264 if (!(Flags & SHF_MERGE))
266 if (Flags & SHF_WRITE)
267 fatal(toString(this) + ": writable SHF_MERGE section is not supported");
269 // Don't try to merge if the alignment is larger than the sh_entsize and this
270 // is not SHF_STRINGS.
272 // Since this is not a SHF_STRINGS, we would need to pad after every entity.
273 // It would be equivalent for the producer of the .o to just set a larger
275 if (Flags & SHF_STRINGS)
278 return Sec.sh_addralign <= EntSize;
281 template <class ELFT>
282 void elf::ObjectFile<ELFT>::initializeSections(
283 DenseSet<CachedHashStringRef> &ComdatGroups) {
284 ArrayRef<Elf_Shdr> ObjSections =
285 check(this->getObj().sections(), toString(this));
286 const ELFFile<ELFT> &Obj = this->getObj();
287 uint64_t Size = ObjSections.size();
288 this->Sections.resize(Size);
290 StringRef SectionStringTable =
291 check(Obj.getSectionStringTable(ObjSections), toString(this));
292 for (const Elf_Shdr &Sec : ObjSections) {
294 if (this->Sections[I] == &InputSection::Discarded)
297 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
298 // if -r is given, we'll let the final link discard such sections.
299 // This is compatible with GNU.
300 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
301 this->Sections[I] = &InputSection::Discarded;
305 switch (Sec.sh_type) {
307 this->Sections[I] = &InputSection::Discarded;
310 CachedHashStringRef(getShtGroupSignature(ObjSections, Sec)))
313 for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
314 if (SecIndex >= Size)
315 fatal(toString(this) +
316 ": invalid section index in group: " + Twine(SecIndex));
317 this->Sections[SecIndex] = &InputSection::Discarded;
321 this->initSymtab(ObjSections, &Sec);
323 case SHT_SYMTAB_SHNDX:
325 check(Obj.getSHNDXTable(Sec, ObjSections), toString(this));
331 this->Sections[I] = createInputSection(Sec, SectionStringTable);
334 // .ARM.exidx sections have a reverse dependency on the InputSection they
335 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
336 if (Sec.sh_flags & SHF_LINK_ORDER) {
337 if (Sec.sh_link >= this->Sections.size())
338 fatal(toString(this) + ": invalid sh_link index: " +
340 this->Sections[Sec.sh_link]->DependentSections.push_back(
346 template <class ELFT>
347 InputSectionBase *elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
348 uint32_t Idx = Sec.sh_info;
349 if (Idx >= this->Sections.size())
350 fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
351 InputSectionBase *Target = this->Sections[Idx];
353 // Strictly speaking, a relocation section must be included in the
354 // group of the section it relocates. However, LLVM 3.3 and earlier
355 // would fail to do so, so we gracefully handle that case.
356 if (Target == &InputSection::Discarded)
360 fatal(toString(this) + ": unsupported relocation reference");
364 template <class ELFT>
366 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec,
367 StringRef SectionStringTable) {
368 StringRef Name = check(
369 this->getObj().getSectionName(&Sec, SectionStringTable), toString(this));
371 switch (Sec.sh_type) {
372 case SHT_ARM_ATTRIBUTES:
373 // FIXME: ARM meta-data section. Retain the first attribute section
374 // we see. The eglibc ARM dynamic loaders require the presence of an
375 // attribute section for dlopen to work.
376 // In a full implementation we would merge all attribute sections.
377 if (In<ELFT>::ARMAttributes == nullptr) {
378 In<ELFT>::ARMAttributes = make<InputSection>(this, &Sec, Name);
379 return In<ELFT>::ARMAttributes;
381 return &InputSection::Discarded;
384 // Find the relocation target section and associate this
385 // section with it. Target can be discarded, for example
386 // if it is a duplicated member of SHT_GROUP section, we
387 // do not create or proccess relocatable sections then.
388 InputSectionBase *Target = getRelocTarget(Sec);
392 // This section contains relocation information.
393 // If -r is given, we do not interpret or apply relocation
394 // but just copy relocation sections to output.
395 if (Config->Relocatable)
396 return make<InputSection>(this, &Sec, Name);
398 if (Target->FirstRelocation)
399 fatal(toString(this) +
400 ": multiple relocation sections to one section are not supported");
401 if (isa<MergeInputSection>(Target))
402 fatal(toString(this) +
403 ": relocations pointing to SHF_MERGE are not supported");
405 size_t NumRelocations;
406 if (Sec.sh_type == SHT_RELA) {
407 ArrayRef<Elf_Rela> Rels =
408 check(this->getObj().relas(&Sec), toString(this));
409 Target->FirstRelocation = Rels.begin();
410 NumRelocations = Rels.size();
411 Target->AreRelocsRela = true;
413 ArrayRef<Elf_Rel> Rels = check(this->getObj().rels(&Sec), toString(this));
414 Target->FirstRelocation = Rels.begin();
415 NumRelocations = Rels.size();
416 Target->AreRelocsRela = false;
418 assert(isUInt<31>(NumRelocations));
419 Target->NumRelocations = NumRelocations;
421 // Relocation sections processed by the linker are usually removed
422 // from the output, so returning `nullptr` for the normal case.
423 // However, if -emit-relocs is given, we need to leave them in the output.
424 // (Some post link analysis tools need this information.)
425 if (Config->EmitRelocs) {
426 InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
427 // We will not emit relocation section if target was discarded.
428 Target->DependentSections.push_back(RelocSec);
435 // The GNU linker uses .note.GNU-stack section as a marker indicating
436 // that the code in the object file does not expect that the stack is
437 // executable (in terms of NX bit). If all input files have the marker,
438 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
439 // make the stack non-executable. Most object files have this section as
442 // But making the stack non-executable is a norm today for security
443 // reasons. Failure to do so may result in a serious security issue.
444 // Therefore, we make LLD always add PT_GNU_STACK unless it is
445 // explicitly told to do otherwise (by -z execstack). Because the stack
446 // executable-ness is controlled solely by command line options,
447 // .note.GNU-stack sections are simply ignored.
448 if (Name == ".note.GNU-stack")
449 return &InputSection::Discarded;
451 // Split stacks is a feature to support a discontiguous stack. At least
452 // as of 2017, it seems that the feature is not being used widely.
453 // Only GNU gold supports that. We don't. For the details about that,
454 // see https://gcc.gnu.org/wiki/SplitStacks
455 if (Name == ".note.GNU-split-stack") {
456 error(toString(this) +
457 ": object file compiled with -fsplit-stack is not supported");
458 return &InputSection::Discarded;
461 if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
462 return &InputSection::Discarded;
464 // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
465 // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
466 // sections. Drop those sections to avoid duplicate symbol errors.
467 // FIXME: This is glibc PR20543, we should remove this hack once that has been
468 // fixed for a while.
469 if (Name.startswith(".gnu.linkonce."))
470 return &InputSection::Discarded;
472 // The linker merges EH (exception handling) frames and creates a
473 // .eh_frame_hdr section for runtime. So we handle them with a special
474 // class. For relocatable outputs, they are just passed through.
475 if (Name == ".eh_frame" && !Config->Relocatable)
476 return make<EhInputSection>(this, &Sec, Name);
478 if (shouldMerge(Sec))
479 return make<MergeInputSection>(this, &Sec, Name);
480 return make<InputSection>(this, &Sec, Name);
483 template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
484 SymbolBodies.reserve(this->Symbols.size());
485 for (const Elf_Sym &Sym : this->Symbols)
486 SymbolBodies.push_back(createSymbolBody(&Sym));
489 template <class ELFT>
490 InputSectionBase *elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
491 uint32_t Index = this->getSectionIndex(Sym);
492 if (Index >= this->Sections.size())
493 fatal(toString(this) + ": invalid section index: " + Twine(Index));
494 InputSectionBase *S = this->Sections[Index];
496 // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 could
497 // generate broken objects. STT_SECTION/STT_NOTYPE symbols can be
498 // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
499 // In this case it is fine for section to be null here as we do not
500 // allocate sections of these types.
502 if (Index == 0 || Sym.getType() == STT_SECTION ||
503 Sym.getType() == STT_NOTYPE)
505 fatal(toString(this) + ": invalid section index: " + Twine(Index));
508 if (S == &InputSection::Discarded)
513 template <class ELFT>
514 SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
515 int Binding = Sym->getBinding();
516 InputSectionBase *Sec = getSection(*Sym);
518 uint8_t StOther = Sym->st_other;
519 uint8_t Type = Sym->getType();
520 uint64_t Value = Sym->st_value;
521 uint64_t Size = Sym->st_size;
523 if (Binding == STB_LOCAL) {
524 if (Sym->getType() == STT_FILE)
525 SourceFile = check(Sym->getName(this->StringTable), toString(this));
527 if (this->StringTable.size() <= Sym->st_name)
528 fatal(toString(this) + ": invalid symbol name offset");
530 StringRefZ Name = this->StringTable.data() + Sym->st_name;
531 if (Sym->st_shndx == SHN_UNDEF)
532 return make<Undefined>(Name, /*IsLocal=*/true, StOther, Type, this);
534 return make<DefinedRegular>(Name, /*IsLocal=*/true, StOther, Type, Value,
538 StringRef Name = check(Sym->getName(this->StringTable), toString(this));
540 switch (Sym->st_shndx) {
542 return elf::Symtab<ELFT>::X
543 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type,
544 /*CanOmitFromDynSym=*/false, this)
547 if (Value == 0 || Value >= UINT32_MAX)
548 fatal(toString(this) + ": common symbol '" + Name +
549 "' has invalid alignment: " + Twine(Value));
550 return elf::Symtab<ELFT>::X
551 ->addCommon(Name, Size, Value, Binding, StOther, Type, this)
557 fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
561 if (Sec == &InputSection::Discarded)
562 return elf::Symtab<ELFT>::X
563 ->addUndefined(Name, /*IsLocal=*/false, Binding, StOther, Type,
564 /*CanOmitFromDynSym=*/false, this)
566 return elf::Symtab<ELFT>::X
567 ->addRegular(Name, StOther, Type, Value, Size, Binding, Sec, this)
572 template <class ELFT> void ArchiveFile::parse() {
573 File = check(Archive::create(MB),
574 MB.getBufferIdentifier() + ": failed to parse archive");
576 // Read the symbol table to construct Lazy objects.
577 for (const Archive::Symbol &Sym : File->symbols()) {
578 Symtab<ELFT>::X->addLazyArchive(this, Sym);
581 if (File->symbols().begin() == File->symbols().end())
582 Config->ArchiveWithoutSymbolsSeen = true;
585 // Returns a buffer pointing to a member file containing a given symbol.
586 std::pair<MemoryBufferRef, uint64_t>
587 ArchiveFile::getMember(const Archive::Symbol *Sym) {
589 check(Sym->getMember(), toString(this) +
590 ": could not get the member for symbol " +
593 if (!Seen.insert(C.getChildOffset()).second)
594 return {MemoryBufferRef(), 0};
596 MemoryBufferRef Ret =
597 check(C.getMemoryBufferRef(),
599 ": could not get the buffer for the member defining symbol " +
602 if (C.getParent()->isThin() && Tar)
603 Tar->append(relativeToRoot(check(C.getFullName(), toString(this))),
605 if (C.getParent()->isThin())
607 return {Ret, C.getChildOffset()};
610 template <class ELFT>
611 SharedFile<ELFT>::SharedFile(MemoryBufferRef M, StringRef DefaultSoName)
612 : ELFFileBase<ELFT>(Base::SharedKind, M), SoName(DefaultSoName),
613 AsNeeded(Config->AsNeeded) {}
615 template <class ELFT>
616 const typename ELFT::Shdr *
617 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
619 this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX),
623 // Partially parse the shared object file so that we can call
624 // getSoName on this object.
625 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
626 const Elf_Shdr *DynamicSec = nullptr;
627 const ELFFile<ELFT> Obj = this->getObj();
628 ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
630 // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
631 for (const Elf_Shdr &Sec : Sections) {
632 switch (Sec.sh_type) {
636 this->initSymtab(Sections, &Sec);
641 case SHT_SYMTAB_SHNDX:
643 check(Obj.getSHNDXTable(Sec, Sections), toString(this));
646 this->VersymSec = &Sec;
649 this->VerdefSec = &Sec;
654 if (this->VersymSec && this->Symbols.empty())
655 error("SHT_GNU_versym should be associated with symbol table");
657 // Search for a DT_SONAME tag to initialize this->SoName.
660 ArrayRef<Elf_Dyn> Arr =
661 check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
663 for (const Elf_Dyn &Dyn : Arr) {
664 if (Dyn.d_tag == DT_SONAME) {
665 uint64_t Val = Dyn.getVal();
666 if (Val >= this->StringTable.size())
667 fatal(toString(this) + ": invalid DT_SONAME entry");
668 SoName = StringRef(this->StringTable.data() + Val);
674 // Parse the version definitions in the object file if present. Returns a vector
675 // whose nth element contains a pointer to the Elf_Verdef for version identifier
676 // n. Version identifiers that are not definitions map to nullptr. The array
677 // always has at least length 1.
678 template <class ELFT>
679 std::vector<const typename ELFT::Verdef *>
680 SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
681 std::vector<const Elf_Verdef *> Verdefs(1);
682 // We only need to process symbol versions for this DSO if it has both a
683 // versym and a verdef section, which indicates that the DSO contains symbol
684 // version definitions.
685 if (!VersymSec || !VerdefSec)
688 // The location of the first global versym entry.
689 const char *Base = this->MB.getBuffer().data();
690 Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
693 // We cannot determine the largest verdef identifier without inspecting
694 // every Elf_Verdef, but both bfd and gold assign verdef identifiers
695 // sequentially starting from 1, so we predict that the largest identifier
696 // will be VerdefCount.
697 unsigned VerdefCount = VerdefSec->sh_info;
698 Verdefs.resize(VerdefCount + 1);
700 // Build the Verdefs array by following the chain of Elf_Verdef objects
701 // from the start of the .gnu.version_d section.
702 const char *Verdef = Base + VerdefSec->sh_offset;
703 for (unsigned I = 0; I != VerdefCount; ++I) {
704 auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
705 Verdef += CurVerdef->vd_next;
706 unsigned VerdefIndex = CurVerdef->vd_ndx;
707 if (Verdefs.size() <= VerdefIndex)
708 Verdefs.resize(VerdefIndex + 1);
709 Verdefs[VerdefIndex] = CurVerdef;
715 // Fully parse the shared object file. This must be called after parseSoName().
716 template <class ELFT> void SharedFile<ELFT>::parseRest() {
717 // Create mapping from version identifiers to Elf_Verdef entries.
718 const Elf_Versym *Versym = nullptr;
719 std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
721 Elf_Sym_Range Syms = this->getGlobalSymbols();
722 for (const Elf_Sym &Sym : Syms) {
723 unsigned VersymIndex = 0;
725 VersymIndex = Versym->vs_index;
728 bool Hidden = VersymIndex & VERSYM_HIDDEN;
729 VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
731 StringRef Name = check(Sym.getName(this->StringTable), toString(this));
732 if (Sym.isUndefined()) {
733 Undefs.push_back(Name);
737 // Ignore local symbols.
738 if (Versym && VersymIndex == VER_NDX_LOCAL)
741 const Elf_Verdef *V =
742 VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
745 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
747 // Also add the symbol with the versioned name to handle undefined symbols
748 // with explicit versions.
750 StringRef VerName = this->StringTable.data() + V->getAux()->vda_name;
751 Name = Saver.save(Twine(Name) + "@" + VerName);
752 elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
757 static ELFKind getBitcodeELFKind(const Triple &T) {
758 if (T.isLittleEndian())
759 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
760 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
763 static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
764 switch (T.getArch()) {
765 case Triple::aarch64:
773 case Triple::mips64el:
780 return T.isOSIAMCU() ? EM_IAMCU : EM_386;
784 fatal(Path + ": could not infer e_machine from bitcode target triple " +
789 BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
790 uint64_t OffsetInArchive)
791 : InputFile(BitcodeKind, MB) {
792 this->ArchiveName = ArchiveName;
794 // Here we pass a new MemoryBufferRef which is identified by ArchiveName
795 // (the fully resolved path of the archive) + member name + offset of the
796 // member in the archive.
797 // ThinLTO uses the MemoryBufferRef identifier to access its internal
798 // data structures and if two archives define two members with the same name,
799 // this causes a collision which result in only one of the objects being
800 // taken into consideration at LTO time (which very likely causes undefined
801 // symbols later in the link stage).
802 MemoryBufferRef MBRef(MB.getBuffer(),
803 Saver.save(ArchiveName + MB.getBufferIdentifier() +
804 utostr(OffsetInArchive)));
805 Obj = check(lto::InputFile::create(MBRef), toString(this));
807 Triple T(Obj->getTargetTriple());
808 EKind = getBitcodeELFKind(T);
809 EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T);
812 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
813 switch (GvVisibility) {
814 case GlobalValue::DefaultVisibility:
816 case GlobalValue::HiddenVisibility:
818 case GlobalValue::ProtectedVisibility:
819 return STV_PROTECTED;
821 llvm_unreachable("unknown visibility");
824 template <class ELFT>
825 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
826 const lto::InputFile::Symbol &ObjSym,
828 StringRef NameRef = Saver.save(ObjSym.getName());
829 uint32_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL;
831 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
832 uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
833 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
835 int C = ObjSym.getComdatIndex();
836 if (C != -1 && !KeptComdats[C])
837 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding,
838 Visibility, Type, CanOmitFromDynSym,
841 if (ObjSym.isUndefined())
842 return Symtab<ELFT>::X->addUndefined(NameRef, /*IsLocal=*/false, Binding,
843 Visibility, Type, CanOmitFromDynSym,
846 if (ObjSym.isCommon())
847 return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(),
848 ObjSym.getCommonAlignment(), Binding,
849 Visibility, STT_OBJECT, F);
851 return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
852 CanOmitFromDynSym, F);
855 template <class ELFT>
856 void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
857 std::vector<bool> KeptComdats;
858 for (StringRef S : Obj->getComdatTable())
859 KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
861 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
862 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this));
865 // Small bit of template meta programming to handle the SharedFile constructor
866 // being the only one with a DefaultSoName parameter.
867 template <template <class> class T, class E>
868 typename std::enable_if<std::is_same<T<E>, SharedFile<E>>::value,
870 createELFAux(MemoryBufferRef MB, StringRef DefaultSoName) {
871 return make<T<E>>(MB, DefaultSoName);
873 template <template <class> class T, class E>
874 typename std::enable_if<!std::is_same<T<E>, SharedFile<E>>::value,
876 createELFAux(MemoryBufferRef MB, StringRef DefaultSoName) {
877 return make<T<E>>(MB);
880 template <template <class> class T>
881 static InputFile *createELFFile(MemoryBufferRef MB, StringRef DefaultSoName) {
883 unsigned char Endian;
884 std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
885 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
886 fatal(MB.getBufferIdentifier() + ": invalid data encoding");
888 size_t BufSize = MB.getBuffer().size();
889 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
890 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
891 fatal(MB.getBufferIdentifier() + ": file is too short");
894 if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
895 Obj = createELFAux<T, ELF32LE>(MB, DefaultSoName);
896 else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
897 Obj = createELFAux<T, ELF32BE>(MB, DefaultSoName);
898 else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
899 Obj = createELFAux<T, ELF64LE>(MB, DefaultSoName);
900 else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
901 Obj = createELFAux<T, ELF64BE>(MB, DefaultSoName);
903 fatal(MB.getBufferIdentifier() + ": invalid file class");
905 if (!Config->FirstElf)
906 Config->FirstElf = Obj;
910 template <class ELFT> void BinaryFile::parse() {
911 StringRef Buf = MB.getBuffer();
912 ArrayRef<uint8_t> Data =
913 makeArrayRef<uint8_t>((const uint8_t *)Buf.data(), Buf.size());
915 std::string Filename = MB.getBufferIdentifier();
916 std::transform(Filename.begin(), Filename.end(), Filename.begin(),
917 [](char C) { return isalnum(C) ? C : '_'; });
918 Filename = "_binary_" + Filename;
919 StringRef StartName = Saver.save(Twine(Filename) + "_start");
920 StringRef EndName = Saver.save(Twine(Filename) + "_end");
921 StringRef SizeName = Saver.save(Twine(Filename) + "_size");
924 make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data");
925 Sections.push_back(Section);
927 elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, STT_OBJECT, 0, 0,
928 STB_GLOBAL, Section, nullptr);
929 elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, STT_OBJECT,
930 Data.size(), 0, STB_GLOBAL, Section,
932 elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, STT_OBJECT,
933 Data.size(), 0, STB_GLOBAL, nullptr,
937 static bool isBitcode(MemoryBufferRef MB) {
938 using namespace sys::fs;
939 return identify_magic(MB.getBuffer()) == file_magic::bitcode;
942 InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
943 uint64_t OffsetInArchive) {
944 InputFile *F = isBitcode(MB)
945 ? make<BitcodeFile>(MB, ArchiveName, OffsetInArchive)
946 : createELFFile<ObjectFile>(MB, "");
947 F->ArchiveName = ArchiveName;
951 InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
952 return createELFFile<SharedFile>(MB, DefaultSoName);
955 MemoryBufferRef LazyObjectFile::getBuffer() {
957 return MemoryBufferRef();
962 template <class ELFT> void LazyObjectFile::parse() {
963 for (StringRef Sym : getSymbols())
964 Symtab<ELFT>::X->addLazyObject(Sym, *this);
967 template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
968 typedef typename ELFT::Shdr Elf_Shdr;
969 typedef typename ELFT::Sym Elf_Sym;
970 typedef typename ELFT::SymRange Elf_Sym_Range;
972 const ELFFile<ELFT> Obj(this->MB.getBuffer());
973 ArrayRef<Elf_Shdr> Sections = check(Obj.sections(), toString(this));
974 for (const Elf_Shdr &Sec : Sections) {
975 if (Sec.sh_type != SHT_SYMTAB)
978 Elf_Sym_Range Syms = check(Obj.symbols(&Sec), toString(this));
979 uint32_t FirstNonLocal = Sec.sh_info;
980 StringRef StringTable =
981 check(Obj.getStringTableForSymtab(Sec, Sections), toString(this));
982 std::vector<StringRef> V;
984 for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
985 if (Sym.st_shndx != SHN_UNDEF)
986 V.push_back(check(Sym.getName(StringTable), toString(this)));
992 std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
993 std::unique_ptr<lto::InputFile> Obj =
994 check(lto::InputFile::create(this->MB), toString(this));
995 std::vector<StringRef> V;
996 for (const lto::InputFile::Symbol &Sym : Obj->symbols())
997 if (!Sym.isUndefined())
998 V.push_back(Saver.save(Sym.getName()));
1002 // Returns a vector of globally-visible defined symbol names.
1003 std::vector<StringRef> LazyObjectFile::getSymbols() {
1004 if (isBitcode(this->MB))
1005 return getBitcodeSymbols();
1008 unsigned char Endian;
1009 std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
1010 if (Size == ELFCLASS32) {
1011 if (Endian == ELFDATA2LSB)
1012 return getElfSymbols<ELF32LE>();
1013 return getElfSymbols<ELF32BE>();
1015 if (Endian == ELFDATA2LSB)
1016 return getElfSymbols<ELF64LE>();
1017 return getElfSymbols<ELF64BE>();
1020 template void ArchiveFile::parse<ELF32LE>();
1021 template void ArchiveFile::parse<ELF32BE>();
1022 template void ArchiveFile::parse<ELF64LE>();
1023 template void ArchiveFile::parse<ELF64BE>();
1025 template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
1026 template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
1027 template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
1028 template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
1030 template void LazyObjectFile::parse<ELF32LE>();
1031 template void LazyObjectFile::parse<ELF32BE>();
1032 template void LazyObjectFile::parse<ELF64LE>();
1033 template void LazyObjectFile::parse<ELF64BE>();
1035 template class elf::ELFFileBase<ELF32LE>;
1036 template class elf::ELFFileBase<ELF32BE>;
1037 template class elf::ELFFileBase<ELF64LE>;
1038 template class elf::ELFFileBase<ELF64BE>;
1040 template class elf::ObjectFile<ELF32LE>;
1041 template class elf::ObjectFile<ELF32BE>;
1042 template class elf::ObjectFile<ELF64LE>;
1043 template class elf::ObjectFile<ELF64BE>;
1045 template class elf::SharedFile<ELF32LE>;
1046 template class elf::SharedFile<ELF32BE>;
1047 template class elf::SharedFile<ELF64LE>;
1048 template class elf::SharedFile<ELF64BE>;
1050 template void BinaryFile::parse<ELF32LE>();
1051 template void BinaryFile::parse<ELF32BE>();
1052 template void BinaryFile::parse<ELF64LE>();
1053 template void BinaryFile::parse<ELF64BE>();