1 //===- InputFiles.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "InputFiles.h"
10 #include "Config.h"
11 #include "DWARF.h"
12 #include "Driver.h"
13 #include "InputSection.h"
14 #include "LinkerScript.h"
15 #include "SymbolTable.h"
16 #include "Symbols.h"
17 #include "SyntheticSections.h"
18 #include "Target.h"
19 #include "lld/Common/CommonLinkerContext.h"
20 #include "lld/Common/DWARF.h"
21 #include "llvm/ADT/CachedHashString.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/LTO/LTO.h"
24 #include "llvm/Object/IRObjectFile.h"
25 #include "llvm/Support/ARMAttributeParser.h"
26 #include "llvm/Support/ARMBuildAttributes.h"
27 #include "llvm/Support/Endian.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/RISCVAttributeParser.h"
31 #include "llvm/Support/TarWriter.h"
32 #include "llvm/Support/raw_ostream.h"
33
34 using namespace llvm;
35 using namespace llvm::ELF;
36 using namespace llvm::object;
37 using namespace llvm::sys;
38 using namespace llvm::sys::fs;
39 using namespace llvm::support::endian;
40 using namespace lld;
41 using namespace lld::elf;
42
43 bool InputFile::isInGroup;
44 uint32_t InputFile::nextGroupId;
45
46 std::unique_ptr<TarWriter> elf::tar;
47
48 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
toString(const InputFile * f)49 std::string lld::toString(const InputFile *f) {
50 if (!f)
51 return "<internal>";
52
53 if (f->toStringCache.empty()) {
54 if (f->archiveName.empty())
55 f->toStringCache = f->getName();
56 else
57 (f->archiveName + "(" + f->getName() + ")").toVector(f->toStringCache);
58 }
59 return std::string(f->toStringCache);
60 }
61
getELFKind(MemoryBufferRef mb,StringRef archiveName)62 static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) {
63 unsigned char size;
64 unsigned char endian;
65 std::tie(size, endian) = getElfArchType(mb.getBuffer());
66
67 auto report = [&](StringRef msg) {
68 StringRef filename = mb.getBufferIdentifier();
69 if (archiveName.empty())
70 fatal(filename + ": " + msg);
71 else
72 fatal(archiveName + "(" + filename + "): " + msg);
73 };
74
75 if (!mb.getBuffer().startswith(ElfMagic))
76 report("not an ELF file");
77 if (endian != ELFDATA2LSB && endian != ELFDATA2MSB)
78 report("corrupted ELF file: invalid data encoding");
79 if (size != ELFCLASS32 && size != ELFCLASS64)
80 report("corrupted ELF file: invalid file class");
81
82 size_t bufSize = mb.getBuffer().size();
83 if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) ||
84 (size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr)))
85 report("corrupted ELF file: file is too short");
86
87 if (size == ELFCLASS32)
88 return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
89 return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
90 }
91
InputFile(Kind k,MemoryBufferRef m)92 InputFile::InputFile(Kind k, MemoryBufferRef m)
93 : mb(m), groupId(nextGroupId), fileKind(k) {
94 // All files within the same --{start,end}-group get the same group ID.
95 // Otherwise, a new file will get a new group ID.
96 if (!isInGroup)
97 ++nextGroupId;
98 }
99
readFile(StringRef path)100 Optional<MemoryBufferRef> elf::readFile(StringRef path) {
101 llvm::TimeTraceScope timeScope("Load input files", path);
102
103 // The --chroot option changes our virtual root directory.
104 // This is useful when you are dealing with files created by --reproduce.
105 if (!config->chroot.empty() && path.startswith("/"))
106 path = saver().save(config->chroot + path);
107
108 log(path);
109 config->dependencyFiles.insert(llvm::CachedHashString(path));
110
111 auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false,
112 /*RequiresNullTerminator=*/false);
113 if (auto ec = mbOrErr.getError()) {
114 error("cannot open " + path + ": " + ec.message());
115 return None;
116 }
117
118 MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef();
119 ctx->memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership
120
121 if (tar)
122 tar->append(relativeToRoot(path), mbref.getBuffer());
123 return mbref;
124 }
125
126 // All input object files must be for the same architecture
127 // (e.g. it does not make sense to link x86 object files with
128 // MIPS object files.) This function checks for that error.
isCompatible(InputFile * file)129 static bool isCompatible(InputFile *file) {
130 if (!file->isElf() && !isa<BitcodeFile>(file))
131 return true;
132
133 if (file->ekind == config->ekind && file->emachine == config->emachine) {
134 if (config->emachine != EM_MIPS)
135 return true;
136 if (isMipsN32Abi(file) == config->mipsN32Abi)
137 return true;
138 }
139
140 StringRef target =
141 !config->bfdname.empty() ? config->bfdname : config->emulation;
142 if (!target.empty()) {
143 error(toString(file) + " is incompatible with " + target);
144 return false;
145 }
146
147 InputFile *existing = nullptr;
148 if (!ctx->objectFiles.empty())
149 existing = ctx->objectFiles[0];
150 else if (!ctx->sharedFiles.empty())
151 existing = ctx->sharedFiles[0];
152 else if (!ctx->bitcodeFiles.empty())
153 existing = ctx->bitcodeFiles[0];
154 std::string with;
155 if (existing)
156 with = " with " + toString(existing);
157 error(toString(file) + " is incompatible" + with);
158 return false;
159 }
160
doParseFile(InputFile * file)161 template <class ELFT> static void doParseFile(InputFile *file) {
162 if (!isCompatible(file))
163 return;
164
165 // Binary file
166 if (auto *f = dyn_cast<BinaryFile>(file)) {
167 ctx->binaryFiles.push_back(f);
168 f->parse();
169 return;
170 }
171
172 // Lazy object file
173 if (file->lazy) {
174 if (auto *f = dyn_cast<BitcodeFile>(file)) {
175 ctx->lazyBitcodeFiles.push_back(f);
176 f->parseLazy();
177 } else {
178 cast<ObjFile<ELFT>>(file)->parseLazy();
179 }
180 return;
181 }
182
183 if (config->trace)
184 message(toString(file));
185
186 // .so file
187 if (auto *f = dyn_cast<SharedFile>(file)) {
188 f->parse<ELFT>();
189 return;
190 }
191
192 // LLVM bitcode file
193 if (auto *f = dyn_cast<BitcodeFile>(file)) {
194 ctx->bitcodeFiles.push_back(f);
195 f->parse<ELFT>();
196 return;
197 }
198
199 // Regular object file
200 ctx->objectFiles.push_back(cast<ELFFileBase>(file));
201 cast<ObjFile<ELFT>>(file)->parse();
202 }
203
204 // Add symbols in File to the symbol table.
parseFile(InputFile * file)205 void elf::parseFile(InputFile *file) { invokeELFT(doParseFile, file); }
206
207 // Concatenates arguments to construct a string representing an error location.
createFileLineMsg(StringRef path,unsigned line)208 static std::string createFileLineMsg(StringRef path, unsigned line) {
209 std::string filename = std::string(path::filename(path));
210 std::string lineno = ":" + std::to_string(line);
211 if (filename == path)
212 return filename + lineno;
213 return filename + lineno + " (" + path.str() + lineno + ")";
214 }
215
216 template <class ELFT>
getSrcMsgAux(ObjFile<ELFT> & file,const Symbol & sym,InputSectionBase & sec,uint64_t offset)217 static std::string getSrcMsgAux(ObjFile<ELFT> &file, const Symbol &sym,
218 InputSectionBase &sec, uint64_t offset) {
219 // In DWARF, functions and variables are stored to different places.
220 // First, look up a function for a given offset.
221 if (Optional<DILineInfo> info = file.getDILineInfo(&sec, offset))
222 return createFileLineMsg(info->FileName, info->Line);
223
224 // If it failed, look up again as a variable.
225 if (Optional<std::pair<std::string, unsigned>> fileLine =
226 file.getVariableLoc(sym.getName()))
227 return createFileLineMsg(fileLine->first, fileLine->second);
228
229 // File.sourceFile contains STT_FILE symbol, and that is a last resort.
230 return std::string(file.sourceFile);
231 }
232
getSrcMsg(const Symbol & sym,InputSectionBase & sec,uint64_t offset)233 std::string InputFile::getSrcMsg(const Symbol &sym, InputSectionBase &sec,
234 uint64_t offset) {
235 if (kind() != ObjKind)
236 return "";
237 switch (config->ekind) {
238 default:
239 llvm_unreachable("Invalid kind");
240 case ELF32LEKind:
241 return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), sym, sec, offset);
242 case ELF32BEKind:
243 return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), sym, sec, offset);
244 case ELF64LEKind:
245 return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), sym, sec, offset);
246 case ELF64BEKind:
247 return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), sym, sec, offset);
248 }
249 }
250
getNameForScript() const251 StringRef InputFile::getNameForScript() const {
252 if (archiveName.empty())
253 return getName();
254
255 if (nameForScriptCache.empty())
256 nameForScriptCache = (archiveName + Twine(':') + getName()).str();
257
258 return nameForScriptCache;
259 }
260
getDwarf()261 template <class ELFT> DWARFCache *ObjFile<ELFT>::getDwarf() {
262 llvm::call_once(initDwarf, [this]() {
263 dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(
264 std::make_unique<LLDDwarfObj<ELFT>>(this), "",
265 [&](Error err) { warn(getName() + ": " + toString(std::move(err))); },
266 [&](Error warning) {
267 warn(getName() + ": " + toString(std::move(warning)));
268 }));
269 });
270
271 return dwarf.get();
272 }
273
274 // Returns the pair of file name and line number describing location of data
275 // object (variable, array, etc) definition.
276 template <class ELFT>
277 Optional<std::pair<std::string, unsigned>>
getVariableLoc(StringRef name)278 ObjFile<ELFT>::getVariableLoc(StringRef name) {
279 return getDwarf()->getVariableLoc(name);
280 }
281
282 // Returns source line information for a given offset
283 // using DWARF debug info.
284 template <class ELFT>
getDILineInfo(InputSectionBase * s,uint64_t offset)285 Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s,
286 uint64_t offset) {
287 // Detect SectionIndex for specified section.
288 uint64_t sectionIndex = object::SectionedAddress::UndefSection;
289 ArrayRef<InputSectionBase *> sections = s->file->getSections();
290 for (uint64_t curIndex = 0; curIndex < sections.size(); ++curIndex) {
291 if (s == sections[curIndex]) {
292 sectionIndex = curIndex;
293 break;
294 }
295 }
296
297 return getDwarf()->getDILineInfo(offset, sectionIndex);
298 }
299
ELFFileBase(Kind k,MemoryBufferRef mb)300 ELFFileBase::ELFFileBase(Kind k, MemoryBufferRef mb) : InputFile(k, mb) {
301 ekind = getELFKind(mb, "");
302
303 switch (ekind) {
304 case ELF32LEKind:
305 init<ELF32LE>();
306 break;
307 case ELF32BEKind:
308 init<ELF32BE>();
309 break;
310 case ELF64LEKind:
311 init<ELF64LE>();
312 break;
313 case ELF64BEKind:
314 init<ELF64BE>();
315 break;
316 default:
317 llvm_unreachable("getELFKind");
318 }
319 }
320
321 template <typename Elf_Shdr>
findSection(ArrayRef<Elf_Shdr> sections,uint32_t type)322 static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) {
323 for (const Elf_Shdr &sec : sections)
324 if (sec.sh_type == type)
325 return &sec;
326 return nullptr;
327 }
328
init()329 template <class ELFT> void ELFFileBase::init() {
330 using Elf_Shdr = typename ELFT::Shdr;
331 using Elf_Sym = typename ELFT::Sym;
332
333 // Initialize trivial attributes.
334 const ELFFile<ELFT> &obj = getObj<ELFT>();
335 emachine = obj.getHeader().e_machine;
336 osabi = obj.getHeader().e_ident[llvm::ELF::EI_OSABI];
337 abiVersion = obj.getHeader().e_ident[llvm::ELF::EI_ABIVERSION];
338
339 ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this);
340 elfShdrs = sections.data();
341 numELFShdrs = sections.size();
342
343 // Find a symbol table.
344 bool isDSO =
345 (identify_magic(mb.getBuffer()) == file_magic::elf_shared_object);
346 const Elf_Shdr *symtabSec =
347 findSection(sections, isDSO ? SHT_DYNSYM : SHT_SYMTAB);
348
349 if (!symtabSec)
350 return;
351
352 // Initialize members corresponding to a symbol table.
353 firstGlobal = symtabSec->sh_info;
354
355 ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(symtabSec), this);
356 if (firstGlobal == 0 || firstGlobal > eSyms.size())
357 fatal(toString(this) + ": invalid sh_info in symbol table");
358
359 elfSyms = reinterpret_cast<const void *>(eSyms.data());
360 numELFSyms = uint32_t(eSyms.size());
361 stringTable = CHECK(obj.getStringTableForSymtab(*symtabSec, sections), this);
362 }
363
364 template <class ELFT>
getSectionIndex(const Elf_Sym & sym) const365 uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const {
366 return CHECK(
367 this->getObj().getSectionIndex(sym, getELFSyms<ELFT>(), shndxTable),
368 this);
369 }
370
parse(bool ignoreComdats)371 template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
372 object::ELFFile<ELFT> obj = this->getObj();
373 // Read a section table. justSymbols is usually false.
374 if (this->justSymbols)
375 initializeJustSymbols();
376 else
377 initializeSections(ignoreComdats, obj);
378
379 // Read a symbol table.
380 initializeSymbols(obj);
381 }
382
383 // Sections with SHT_GROUP and comdat bits define comdat section groups.
384 // They are identified and deduplicated by group name. This function
385 // returns a group name.
386 template <class ELFT>
getShtGroupSignature(ArrayRef<Elf_Shdr> sections,const Elf_Shdr & sec)387 StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
388 const Elf_Shdr &sec) {
389 typename ELFT::SymRange symbols = this->getELFSyms<ELFT>();
390 if (sec.sh_info >= symbols.size())
391 fatal(toString(this) + ": invalid symbol index");
392 const typename ELFT::Sym &sym = symbols[sec.sh_info];
393 return CHECK(sym.getName(this->stringTable), this);
394 }
395
396 template <class ELFT>
shouldMerge(const Elf_Shdr & sec,StringRef name)397 bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {
398 // On a regular link we don't merge sections if -O0 (default is -O1). This
399 // sometimes makes the linker significantly faster, although the output will
400 // be bigger.
401 //
402 // Doing the same for -r would create a problem as it would combine sections
403 // with different sh_entsize. One option would be to just copy every SHF_MERGE
404 // section as is to the output. While this would produce a valid ELF file with
405 // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
406 // they see two .debug_str. We could have separate logic for combining
407 // SHF_MERGE sections based both on their name and sh_entsize, but that seems
408 // to be more trouble than it is worth. Instead, we just use the regular (-O1)
409 // logic for -r.
410 if (config->optimize == 0 && !config->relocatable)
411 return false;
412
413 // A mergeable section with size 0 is useless because they don't have
414 // any data to merge. A mergeable string section with size 0 can be
415 // argued as invalid because it doesn't end with a null character.
416 // We'll avoid a mess by handling them as if they were non-mergeable.
417 if (sec.sh_size == 0)
418 return false;
419
420 // Check for sh_entsize. The ELF spec is not clear about the zero
421 // sh_entsize. It says that "the member [sh_entsize] contains 0 if
422 // the section does not hold a table of fixed-size entries". We know
423 // that Rust 1.13 produces a string mergeable section with a zero
424 // sh_entsize. Here we just accept it rather than being picky about it.
425 uint64_t entSize = sec.sh_entsize;
426 if (entSize == 0)
427 return false;
428 if (sec.sh_size % entSize)
429 fatal(toString(this) + ":(" + name + "): SHF_MERGE section size (" +
430 Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" +
431 Twine(entSize) + ")");
432
433 if (sec.sh_flags & SHF_WRITE)
434 fatal(toString(this) + ":(" + name +
435 "): writable SHF_MERGE section is not supported");
436
437 return true;
438 }
439
440 // This is for --just-symbols.
441 //
442 // --just-symbols is a very minor feature that allows you to link your
443 // output against other existing program, so that if you load both your
444 // program and the other program into memory, your output can refer the
445 // other program's symbols.
446 //
447 // When the option is given, we link "just symbols". The section table is
448 // initialized with null pointers.
initializeJustSymbols()449 template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {
450 sections.resize(numELFShdrs);
451 }
452
453 // An ELF object file may contain a `.deplibs` section. If it exists, the
454 // section contains a list of library specifiers such as `m` for libm. This
455 // function resolves a given name by finding the first matching library checking
456 // the various ways that a library can be specified to LLD. This ELF extension
457 // is a form of autolinking and is called `dependent libraries`. It is currently
458 // unique to LLVM and lld.
addDependentLibrary(StringRef specifier,const InputFile * f)459 static void addDependentLibrary(StringRef specifier, const InputFile *f) {
460 if (!config->dependentLibraries)
461 return;
462 if (Optional<std::string> s = searchLibraryBaseName(specifier))
463 driver->addFile(saver().save(*s), /*withLOption=*/true);
464 else if (Optional<std::string> s = findFromSearchPaths(specifier))
465 driver->addFile(saver().save(*s), /*withLOption=*/true);
466 else if (fs::exists(specifier))
467 driver->addFile(specifier, /*withLOption=*/false);
468 else
469 error(toString(f) +
470 ": unable to find library from dependent library specifier: " +
471 specifier);
472 }
473
474 // Record the membership of a section group so that in the garbage collection
475 // pass, section group members are kept or discarded as a unit.
476 template <class ELFT>
handleSectionGroup(ArrayRef<InputSectionBase * > sections,ArrayRef<typename ELFT::Word> entries)477 static void handleSectionGroup(ArrayRef<InputSectionBase *> sections,
478 ArrayRef<typename ELFT::Word> entries) {
479 bool hasAlloc = false;
480 for (uint32_t index : entries.slice(1)) {
481 if (index >= sections.size())
482 return;
483 if (InputSectionBase *s = sections[index])
484 if (s != &InputSection::discarded && s->flags & SHF_ALLOC)
485 hasAlloc = true;
486 }
487
488 // If any member has the SHF_ALLOC flag, the whole group is subject to garbage
489 // collection. See the comment in markLive(). This rule retains .debug_types
490 // and .rela.debug_types.
491 if (!hasAlloc)
492 return;
493
494 // Connect the members in a circular doubly-linked list via
495 // nextInSectionGroup.
496 InputSectionBase *head;
497 InputSectionBase *prev = nullptr;
498 for (uint32_t index : entries.slice(1)) {
499 InputSectionBase *s = sections[index];
500 if (!s || s == &InputSection::discarded)
501 continue;
502 if (prev)
503 prev->nextInSectionGroup = s;
504 else
505 head = s;
506 prev = s;
507 }
508 if (prev)
509 prev->nextInSectionGroup = head;
510 }
511
512 template <class ELFT>
initializeSections(bool ignoreComdats,const llvm::object::ELFFile<ELFT> & obj)513 void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
514 const llvm::object::ELFFile<ELFT> &obj) {
515 ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>();
516 StringRef shstrtab = CHECK(obj.getSectionStringTable(objSections), this);
517 uint64_t size = objSections.size();
518 this->sections.resize(size);
519
520 std::vector<ArrayRef<Elf_Word>> selectedGroups;
521
522 for (size_t i = 0; i != size; ++i) {
523 if (this->sections[i] == &InputSection::discarded)
524 continue;
525 const Elf_Shdr &sec = objSections[i];
526
527 // SHF_EXCLUDE'ed sections are discarded by the linker. However,
528 // if -r is given, we'll let the final link discard such sections.
529 // This is compatible with GNU.
530 if ((sec.sh_flags & SHF_EXCLUDE) && !config->relocatable) {
531 if (sec.sh_type == SHT_LLVM_CALL_GRAPH_PROFILE)
532 cgProfileSectionIndex = i;
533 if (sec.sh_type == SHT_LLVM_ADDRSIG) {
534 // We ignore the address-significance table if we know that the object
535 // file was created by objcopy or ld -r. This is because these tools
536 // will reorder the symbols in the symbol table, invalidating the data
537 // in the address-significance table, which refers to symbols by index.
538 if (sec.sh_link != 0)
539 this->addrsigSec = &sec;
540 else if (config->icf == ICFLevel::Safe)
541 warn(toString(this) +
542 ": --icf=safe conservatively ignores "
543 "SHT_LLVM_ADDRSIG [index " +
544 Twine(i) +
545 "] with sh_link=0 "
546 "(likely created using objcopy or ld -r)");
547 }
548 this->sections[i] = &InputSection::discarded;
549 continue;
550 }
551
552 switch (sec.sh_type) {
553 case SHT_GROUP: {
554 // De-duplicate section groups by their signatures.
555 StringRef signature = getShtGroupSignature(objSections, sec);
556 this->sections[i] = &InputSection::discarded;
557
558 ArrayRef<Elf_Word> entries =
559 CHECK(obj.template getSectionContentsAsArray<Elf_Word>(sec), this);
560 if (entries.empty())
561 fatal(toString(this) + ": empty SHT_GROUP");
562
563 Elf_Word flag = entries[0];
564 if (flag && flag != GRP_COMDAT)
565 fatal(toString(this) + ": unsupported SHT_GROUP format");
566
567 bool keepGroup =
568 (flag & GRP_COMDAT) == 0 || ignoreComdats ||
569 symtab->comdatGroups.try_emplace(CachedHashStringRef(signature), this)
570 .second;
571 if (keepGroup) {
572 if (config->relocatable)
573 this->sections[i] = createInputSection(
574 i, sec, check(obj.getSectionName(sec, shstrtab)));
575 selectedGroups.push_back(entries);
576 continue;
577 }
578
579 // Otherwise, discard group members.
580 for (uint32_t secIndex : entries.slice(1)) {
581 if (secIndex >= size)
582 fatal(toString(this) +
583 ": invalid section index in group: " + Twine(secIndex));
584 this->sections[secIndex] = &InputSection::discarded;
585 }
586 break;
587 }
588 case SHT_SYMTAB_SHNDX:
589 shndxTable = CHECK(obj.getSHNDXTable(sec, objSections), this);
590 break;
591 case SHT_SYMTAB:
592 case SHT_STRTAB:
593 case SHT_REL:
594 case SHT_RELA:
595 case SHT_NULL:
596 break;
597 case SHT_LLVM_SYMPART:
598 ctx->hasSympart.store(true, std::memory_order_relaxed);
599 LLVM_FALLTHROUGH;
600 default:
601 this->sections[i] =
602 createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));
603 }
604 }
605
606 // We have a second loop. It is used to:
607 // 1) handle SHF_LINK_ORDER sections.
608 // 2) create SHT_REL[A] sections. In some cases the section header index of a
609 // relocation section may be smaller than that of the relocated section. In
610 // such cases, the relocation section would attempt to reference a target
611 // section that has not yet been created. For simplicity, delay creation of
612 // relocation sections until now.
613 for (size_t i = 0; i != size; ++i) {
614 if (this->sections[i] == &InputSection::discarded)
615 continue;
616 const Elf_Shdr &sec = objSections[i];
617
618 if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) {
619 // Find a relocation target section and associate this section with that.
620 // Target may have been discarded if it is in a different section group
621 // and the group is discarded, even though it's a violation of the spec.
622 // We handle that situation gracefully by discarding dangling relocation
623 // sections.
624 const uint32_t info = sec.sh_info;
625 InputSectionBase *s = getRelocTarget(i, sec, info);
626 if (!s)
627 continue;
628
629 // ELF spec allows mergeable sections with relocations, but they are rare,
630 // and it is in practice hard to merge such sections by contents, because
631 // applying relocations at end of linking changes section contents. So, we
632 // simply handle such sections as non-mergeable ones. Degrading like this
633 // is acceptable because section merging is optional.
634 if (auto *ms = dyn_cast<MergeInputSection>(s)) {
635 s = make<InputSection>(ms->file, ms->flags, ms->type, ms->alignment,
636 ms->data(), ms->name);
637 sections[info] = s;
638 }
639
640 if (s->relSecIdx != 0)
641 error(
642 toString(s) +
643 ": multiple relocation sections to one section are not supported");
644 s->relSecIdx = i;
645
646 // Relocation sections are usually removed from the output, so return
647 // `nullptr` for the normal case. However, if -r or --emit-relocs is
648 // specified, we need to copy them to the output. (Some post link analysis
649 // tools specify --emit-relocs to obtain the information.)
650 if (config->copyRelocs) {
651 auto *isec = make<InputSection>(
652 *this, sec, check(obj.getSectionName(sec, shstrtab)));
653 // If the relocated section is discarded (due to /DISCARD/ or
654 // --gc-sections), the relocation section should be discarded as well.
655 s->dependentSections.push_back(isec);
656 sections[i] = isec;
657 }
658 continue;
659 }
660
661 // A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have
662 // the flag.
663 if (!sec.sh_link || !(sec.sh_flags & SHF_LINK_ORDER))
664 continue;
665
666 InputSectionBase *linkSec = nullptr;
667 if (sec.sh_link < size)
668 linkSec = this->sections[sec.sh_link];
669 if (!linkSec)
670 fatal(toString(this) + ": invalid sh_link index: " + Twine(sec.sh_link));
671
672 // A SHF_LINK_ORDER section is discarded if its linked-to section is
673 // discarded.
674 InputSection *isec = cast<InputSection>(this->sections[i]);
675 linkSec->dependentSections.push_back(isec);
676 if (!isa<InputSection>(linkSec))
677 error("a section " + isec->name +
678 " with SHF_LINK_ORDER should not refer a non-regular section: " +
679 toString(linkSec));
680 }
681
682 for (ArrayRef<Elf_Word> entries : selectedGroups)
683 handleSectionGroup<ELFT>(this->sections, entries);
684 }
685
686 // For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD
687 // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
688 // the input objects have been compiled.
updateARMVFPArgs(const ARMAttributeParser & attributes,const InputFile * f)689 static void updateARMVFPArgs(const ARMAttributeParser &attributes,
690 const InputFile *f) {
691 Optional<unsigned> attr =
692 attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);
693 if (!attr)
694 // If an ABI tag isn't present then it is implicitly given the value of 0
695 // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
696 // including some in glibc that don't use FP args (and should have value 3)
697 // don't have the attribute so we do not consider an implicit value of 0
698 // as a clash.
699 return;
700
701 unsigned vfpArgs = *attr;
702 ARMVFPArgKind arg;
703 switch (vfpArgs) {
704 case ARMBuildAttrs::BaseAAPCS:
705 arg = ARMVFPArgKind::Base;
706 break;
707 case ARMBuildAttrs::HardFPAAPCS:
708 arg = ARMVFPArgKind::VFP;
709 break;
710 case ARMBuildAttrs::ToolChainFPPCS:
711 // Tool chain specific convention that conforms to neither AAPCS variant.
712 arg = ARMVFPArgKind::ToolChain;
713 break;
714 case ARMBuildAttrs::CompatibleFPAAPCS:
715 // Object compatible with all conventions.
716 return;
717 default:
718 error(toString(f) + ": unknown Tag_ABI_VFP_args value: " + Twine(vfpArgs));
719 return;
720 }
721 // Follow ld.bfd and error if there is a mix of calling conventions.
722 if (config->armVFPArgs != arg && config->armVFPArgs != ARMVFPArgKind::Default)
723 error(toString(f) + ": incompatible Tag_ABI_VFP_args");
724 else
725 config->armVFPArgs = arg;
726 }
727
728 // The ARM support in lld makes some use of instructions that are not available
729 // on all ARM architectures. Namely:
730 // - Use of BLX instruction for interworking between ARM and Thumb state.
731 // - Use of the extended Thumb branch encoding in relocation.
732 // - Use of the MOVT/MOVW instructions in Thumb Thunks.
733 // The ARM Attributes section contains information about the architecture chosen
734 // at compile time. We follow the convention that if at least one input object
735 // is compiled with an architecture that supports these features then lld is
736 // permitted to use them.
updateSupportedARMFeatures(const ARMAttributeParser & attributes)737 static void updateSupportedARMFeatures(const ARMAttributeParser &attributes) {
738 Optional<unsigned> attr =
739 attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
740 if (!attr)
741 return;
742 auto arch = attr.value();
743 switch (arch) {
744 case ARMBuildAttrs::Pre_v4:
745 case ARMBuildAttrs::v4:
746 case ARMBuildAttrs::v4T:
747 // Architectures prior to v5 do not support BLX instruction
748 break;
749 case ARMBuildAttrs::v5T:
750 case ARMBuildAttrs::v5TE:
751 case ARMBuildAttrs::v5TEJ:
752 case ARMBuildAttrs::v6:
753 case ARMBuildAttrs::v6KZ:
754 case ARMBuildAttrs::v6K:
755 config->armHasBlx = true;
756 // Architectures used in pre-Cortex processors do not support
757 // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
758 // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
759 break;
760 default:
761 // All other Architectures have BLX and extended branch encoding
762 config->armHasBlx = true;
763 config->armJ1J2BranchEncoding = true;
764 if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M)
765 // All Architectures used in Cortex processors with the exception
766 // of v6-M and v6S-M have the MOVT and MOVW instructions.
767 config->armHasMovtMovw = true;
768 break;
769 }
770 }
771
772 // If a source file is compiled with x86 hardware-assisted call flow control
773 // enabled, the generated object file contains feature flags indicating that
774 // fact. This function reads the feature flags and returns it.
775 //
776 // Essentially we want to read a single 32-bit value in this function, but this
777 // function is rather complicated because the value is buried deep inside a
778 // .note.gnu.property section.
779 //
780 // The section consists of one or more NOTE records. Each NOTE record consists
781 // of zero or more type-length-value fields. We want to find a field of a
782 // certain type. It seems a bit too much to just store a 32-bit value, perhaps
783 // the ABI is unnecessarily complicated.
readAndFeatures(const InputSection & sec)784 template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) {
785 using Elf_Nhdr = typename ELFT::Nhdr;
786 using Elf_Note = typename ELFT::Note;
787
788 uint32_t featuresSet = 0;
789 ArrayRef<uint8_t> data = sec.rawData;
790 auto reportFatal = [&](const uint8_t *place, const char *msg) {
791 fatal(toString(sec.file) + ":(" + sec.name + "+0x" +
792 Twine::utohexstr(place - sec.rawData.data()) + "): " + msg);
793 };
794 while (!data.empty()) {
795 // Read one NOTE record.
796 auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data());
797 if (data.size() < sizeof(Elf_Nhdr) || data.size() < nhdr->getSize())
798 reportFatal(data.data(), "data is too short");
799
800 Elf_Note note(*nhdr);
801 if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") {
802 data = data.slice(nhdr->getSize());
803 continue;
804 }
805
806 uint32_t featureAndType = config->emachine == EM_AARCH64
807 ? GNU_PROPERTY_AARCH64_FEATURE_1_AND
808 : GNU_PROPERTY_X86_FEATURE_1_AND;
809
810 // Read a body of a NOTE record, which consists of type-length-value fields.
811 ArrayRef<uint8_t> desc = note.getDesc();
812 while (!desc.empty()) {
813 const uint8_t *place = desc.data();
814 if (desc.size() < 8)
815 reportFatal(place, "program property is too short");
816 uint32_t type = read32<ELFT::TargetEndianness>(desc.data());
817 uint32_t size = read32<ELFT::TargetEndianness>(desc.data() + 4);
818 desc = desc.slice(8);
819 if (desc.size() < size)
820 reportFatal(place, "program property is too short");
821
822 if (type == featureAndType) {
823 // We found a FEATURE_1_AND field. There may be more than one of these
824 // in a .note.gnu.property section, for a relocatable object we
825 // accumulate the bits set.
826 if (size < 4)
827 reportFatal(place, "FEATURE_1_AND entry is too short");
828 featuresSet |= read32<ELFT::TargetEndianness>(desc.data());
829 }
830
831 // Padding is present in the note descriptor, if necessary.
832 desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));
833 }
834
835 // Go to next NOTE record to look for more FEATURE_1_AND descriptions.
836 data = data.slice(nhdr->getSize());
837 }
838
839 return featuresSet;
840 }
841
842 template <class ELFT>
getRelocTarget(uint32_t idx,const Elf_Shdr & sec,uint32_t info)843 InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx,
844 const Elf_Shdr &sec,
845 uint32_t info) {
846 if (info < this->sections.size()) {
847 InputSectionBase *target = this->sections[info];
848
849 // Strictly speaking, a relocation section must be included in the
850 // group of the section it relocates. However, LLVM 3.3 and earlier
851 // would fail to do so, so we gracefully handle that case.
852 if (target == &InputSection::discarded)
853 return nullptr;
854
855 if (target != nullptr)
856 return target;
857 }
858
859 error(toString(this) + Twine(": relocation section (index ") + Twine(idx) +
860 ") has invalid sh_info (" + Twine(info) + ")");
861 return nullptr;
862 }
863
864 template <class ELFT>
createInputSection(uint32_t idx,const Elf_Shdr & sec,StringRef name)865 InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
866 const Elf_Shdr &sec,
867 StringRef name) {
868 if (sec.sh_type == SHT_ARM_ATTRIBUTES && config->emachine == EM_ARM) {
869 ARMAttributeParser attributes;
870 ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
871 if (Error e = attributes.parse(contents, config->ekind == ELF32LEKind
872 ? support::little
873 : support::big)) {
874 auto *isec = make<InputSection>(*this, sec, name);
875 warn(toString(isec) + ": " + llvm::toString(std::move(e)));
876 } else {
877 updateSupportedARMFeatures(attributes);
878 updateARMVFPArgs(attributes, this);
879
880 // FIXME: Retain the first attribute section we see. The eglibc ARM
881 // dynamic loaders require the presence of an attribute section for dlopen
882 // to work. In a full implementation we would merge all attribute
883 // sections.
884 if (in.attributes == nullptr) {
885 in.attributes = std::make_unique<InputSection>(*this, sec, name);
886 return in.attributes.get();
887 }
888 return &InputSection::discarded;
889 }
890 }
891
892 if (sec.sh_type == SHT_RISCV_ATTRIBUTES && config->emachine == EM_RISCV) {
893 RISCVAttributeParser attributes;
894 ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
895 if (Error e = attributes.parse(contents, support::little)) {
896 auto *isec = make<InputSection>(*this, sec, name);
897 warn(toString(isec) + ": " + llvm::toString(std::move(e)));
898 } else {
899 // FIXME: Validate arch tag contains C if and only if EF_RISCV_RVC is
900 // present.
901
902 // FIXME: Retain the first attribute section we see. Tools such as
903 // llvm-objdump make use of the attribute section to determine which
904 // standard extensions to enable. In a full implementation we would merge
905 // all attribute sections.
906 if (in.attributes == nullptr) {
907 in.attributes = std::make_unique<InputSection>(*this, sec, name);
908 return in.attributes.get();
909 }
910 return &InputSection::discarded;
911 }
912 }
913
914 if (sec.sh_type == SHT_LLVM_DEPENDENT_LIBRARIES && !config->relocatable) {
915 ArrayRef<char> data =
916 CHECK(this->getObj().template getSectionContentsAsArray<char>(sec), this);
917 if (!data.empty() && data.back() != '\0') {
918 error(toString(this) +
919 ": corrupted dependent libraries section (unterminated string): " +
920 name);
921 return &InputSection::discarded;
922 }
923 for (const char *d = data.begin(), *e = data.end(); d < e;) {
924 StringRef s(d);
925 addDependentLibrary(s, this);
926 d += s.size() + 1;
927 }
928 return &InputSection::discarded;
929 }
930
931 if (name.startswith(".n")) {
932 // The GNU linker uses .note.GNU-stack section as a marker indicating
933 // that the code in the object file does not expect that the stack is
934 // executable (in terms of NX bit). If all input files have the marker,
935 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
936 // make the stack non-executable. Most object files have this section as
937 // of 2017.
938 //
939 // But making the stack non-executable is a norm today for security
940 // reasons. Failure to do so may result in a serious security issue.
941 // Therefore, we make LLD always add PT_GNU_STACK unless it is
942 // explicitly told to do otherwise (by -z execstack). Because the stack
943 // executable-ness is controlled solely by command line options,
944 // .note.GNU-stack sections are simply ignored.
945 if (name == ".note.GNU-stack")
946 return &InputSection::discarded;
947
948 // Object files that use processor features such as Intel Control-Flow
949 // Enforcement (CET) or AArch64 Branch Target Identification BTI, use a
950 // .note.gnu.property section containing a bitfield of feature bits like the
951 // GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag.
952 //
953 // Since we merge bitmaps from multiple object files to create a new
954 // .note.gnu.property containing a single AND'ed bitmap, we discard an input
955 // file's .note.gnu.property section.
956 if (name == ".note.gnu.property") {
957 this->andFeatures = readAndFeatures<ELFT>(InputSection(*this, sec, name));
958 return &InputSection::discarded;
959 }
960
961 // Split stacks is a feature to support a discontiguous stack,
962 // commonly used in the programming language Go. For the details,
963 // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
964 // for split stack will include a .note.GNU-split-stack section.
965 if (name == ".note.GNU-split-stack") {
966 if (config->relocatable) {
967 error(
968 "cannot mix split-stack and non-split-stack in a relocatable link");
969 return &InputSection::discarded;
970 }
971 this->splitStack = true;
972 return &InputSection::discarded;
973 }
974
975 // An object file cmpiled for split stack, but where some of the
976 // functions were compiled with the no_split_stack_attribute will
977 // include a .note.GNU-no-split-stack section.
978 if (name == ".note.GNU-no-split-stack") {
979 this->someNoSplitStack = true;
980 return &InputSection::discarded;
981 }
982
983 // Strip existing .note.gnu.build-id sections so that the output won't have
984 // more than one build-id. This is not usually a problem because input
985 // object files normally don't have .build-id sections, but you can create
986 // such files by "ld.{bfd,gold,lld} -r --build-id", and we want to guard
987 // against it.
988 if (name == ".note.gnu.build-id")
989 return &InputSection::discarded;
990 }
991
992 // The linker merges EH (exception handling) frames and creates a
993 // .eh_frame_hdr section for runtime. So we handle them with a special
994 // class. For relocatable outputs, they are just passed through.
995 if (name == ".eh_frame" && !config->relocatable)
996 return make<EhInputSection>(*this, sec, name);
997
998 if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name))
999 return make<MergeInputSection>(*this, sec, name);
1000 return make<InputSection>(*this, sec, name);
1001 }
1002
1003 // Initialize this->Symbols. this->Symbols is a parallel array as
1004 // its corresponding ELF symbol table.
1005 template <class ELFT>
initializeSymbols(const object::ELFFile<ELFT> & obj)1006 void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
1007 SymbolTable &symtab = *elf::symtab;
1008
1009 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
1010 symbols.resize(eSyms.size());
1011
1012 // Some entries have been filled by LazyObjFile.
1013 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)
1014 if (!symbols[i])
1015 symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this));
1016
1017 // Perform symbol resolution on non-local symbols.
1018 SmallVector<unsigned, 32> undefineds;
1019 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
1020 const Elf_Sym &eSym = eSyms[i];
1021 uint32_t secIdx = eSym.st_shndx;
1022 if (secIdx == SHN_UNDEF) {
1023 undefineds.push_back(i);
1024 continue;
1025 }
1026
1027 uint8_t binding = eSym.getBinding();
1028 uint8_t stOther = eSym.st_other;
1029 uint8_t type = eSym.getType();
1030 uint64_t value = eSym.st_value;
1031 uint64_t size = eSym.st_size;
1032
1033 Symbol *sym = symbols[i];
1034 sym->isUsedInRegularObj = true;
1035 if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) {
1036 if (value == 0 || value >= UINT32_MAX)
1037 fatal(toString(this) + ": common symbol '" + sym->getName() +
1038 "' has invalid alignment: " + Twine(value));
1039 hasCommonSyms = true;
1040 sym->resolve(
1041 CommonSymbol{this, StringRef(), binding, stOther, type, value, size});
1042 continue;
1043 }
1044
1045 // Handle global defined symbols. Defined::section will be set in postParse.
1046 sym->resolve(Defined{this, StringRef(), binding, stOther, type, value, size,
1047 nullptr});
1048 }
1049
1050 // Undefined symbols (excluding those defined relative to non-prevailing
1051 // sections) can trigger recursive extract. Process defined symbols first so
1052 // that the relative order between a defined symbol and an undefined symbol
1053 // does not change the symbol resolution behavior. In addition, a set of
1054 // interconnected symbols will all be resolved to the same file, instead of
1055 // being resolved to different files.
1056 for (unsigned i : undefineds) {
1057 const Elf_Sym &eSym = eSyms[i];
1058 Symbol *sym = symbols[i];
1059 sym->resolve(Undefined{this, StringRef(), eSym.getBinding(), eSym.st_other,
1060 eSym.getType()});
1061 sym->isUsedInRegularObj = true;
1062 sym->referenced = true;
1063 }
1064 }
1065
initializeLocalSymbols()1066 template <class ELFT> void ObjFile<ELFT>::initializeLocalSymbols() {
1067 if (!firstGlobal)
1068 return;
1069 localSymStorage = std::make_unique<SymbolUnion[]>(firstGlobal);
1070 SymbolUnion *locals = localSymStorage.get();
1071
1072 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
1073 for (size_t i = 0, end = firstGlobal; i != end; ++i) {
1074 const Elf_Sym &eSym = eSyms[i];
1075 uint32_t secIdx = eSym.st_shndx;
1076 if (LLVM_UNLIKELY(secIdx == SHN_XINDEX))
1077 secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));
1078 else if (secIdx >= SHN_LORESERVE)
1079 secIdx = 0;
1080 if (LLVM_UNLIKELY(secIdx >= sections.size()))
1081 fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
1082 if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL))
1083 error(toString(this) + ": non-local symbol (" + Twine(i) +
1084 ") found at index < .symtab's sh_info (" + Twine(end) + ")");
1085
1086 InputSectionBase *sec = sections[secIdx];
1087 uint8_t type = eSym.getType();
1088 if (type == STT_FILE)
1089 sourceFile = CHECK(eSym.getName(stringTable), this);
1090 if (LLVM_UNLIKELY(stringTable.size() <= eSym.st_name))
1091 fatal(toString(this) + ": invalid symbol name offset");
1092 StringRef name(stringTable.data() + eSym.st_name);
1093
1094 symbols[i] = reinterpret_cast<Symbol *>(locals + i);
1095 if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded)
1096 new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type,
1097 /*discardedSecIdx=*/secIdx);
1098 else
1099 new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type,
1100 eSym.st_value, eSym.st_size, sec);
1101 symbols[i]->isUsedInRegularObj = true;
1102 }
1103 }
1104
1105 // Called after all ObjFile::parse is called for all ObjFiles. This checks
1106 // duplicate symbols and may do symbol property merge in the future.
postParse()1107 template <class ELFT> void ObjFile<ELFT>::postParse() {
1108 static std::mutex mu;
1109 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
1110 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
1111 const Elf_Sym &eSym = eSyms[i];
1112 Symbol &sym = *symbols[i];
1113 uint32_t secIdx = eSym.st_shndx;
1114 uint8_t binding = eSym.getBinding();
1115 if (LLVM_UNLIKELY(binding != STB_GLOBAL && binding != STB_WEAK &&
1116 binding != STB_GNU_UNIQUE))
1117 errorOrWarn(toString(this) + ": symbol (" + Twine(i) +
1118 ") has invalid binding: " + Twine((int)binding));
1119
1120 // st_value of STT_TLS represents the assigned offset, not the actual
1121 // address which is used by STT_FUNC and STT_OBJECT. STT_TLS symbols can
1122 // only be referenced by special TLS relocations. It is usually an error if
1123 // a STT_TLS symbol is replaced by a non-STT_TLS symbol, vice versa.
1124 if (LLVM_UNLIKELY(sym.isTls()) && eSym.getType() != STT_TLS &&
1125 eSym.getType() != STT_NOTYPE)
1126 errorOrWarn("TLS attribute mismatch: " + toString(sym) + "\n>>> in " +
1127 toString(sym.file) + "\n>>> in " + toString(this));
1128
1129 // Handle non-COMMON defined symbol below. !sym.file allows a symbol
1130 // assignment to redefine a symbol without an error.
1131 if (!sym.file || !sym.isDefined() || secIdx == SHN_UNDEF ||
1132 secIdx == SHN_COMMON)
1133 continue;
1134
1135 if (LLVM_UNLIKELY(secIdx == SHN_XINDEX))
1136 secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));
1137 else if (secIdx >= SHN_LORESERVE)
1138 secIdx = 0;
1139 if (LLVM_UNLIKELY(secIdx >= sections.size()))
1140 fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
1141 InputSectionBase *sec = sections[secIdx];
1142 if (sec == &InputSection::discarded) {
1143 if (sym.traced) {
1144 printTraceSymbol(Undefined{this, sym.getName(), sym.binding,
1145 sym.stOther, sym.type, secIdx},
1146 sym.getName());
1147 }
1148 if (sym.file == this) {
1149 std::lock_guard<std::mutex> lock(mu);
1150 ctx->nonPrevailingSyms.emplace_back(&sym, secIdx);
1151 }
1152 continue;
1153 }
1154
1155 if (sym.file == this) {
1156 cast<Defined>(sym).section = sec;
1157 continue;
1158 }
1159
1160 if (sym.binding == STB_WEAK || binding == STB_WEAK)
1161 continue;
1162 std::lock_guard<std::mutex> lock(mu);
1163 ctx->duplicates.push_back({&sym, this, sec, eSym.st_value});
1164 }
1165 }
1166
1167 // The handling of tentative definitions (COMMON symbols) in archives is murky.
1168 // A tentative definition will be promoted to a global definition if there are
1169 // no non-tentative definitions to dominate it. When we hold a tentative
1170 // definition to a symbol and are inspecting archive members for inclusion
1171 // there are 2 ways we can proceed:
1172 //
1173 // 1) Consider the tentative definition a 'real' definition (ie promotion from
1174 // tentative to real definition has already happened) and not inspect
1175 // archive members for Global/Weak definitions to replace the tentative
1176 // definition. An archive member would only be included if it satisfies some
1177 // other undefined symbol. This is the behavior Gold uses.
1178 //
1179 // 2) Consider the tentative definition as still undefined (ie the promotion to
1180 // a real definition happens only after all symbol resolution is done).
1181 // The linker searches archive members for STB_GLOBAL definitions to
1182 // replace the tentative definition with. This is the behavior used by
1183 // GNU ld.
1184 //
1185 // The second behavior is inherited from SysVR4, which based it on the FORTRAN
1186 // COMMON BLOCK model. This behavior is needed for proper initialization in old
1187 // (pre F90) FORTRAN code that is packaged into an archive.
1188 //
1189 // The following functions search archive members for definitions to replace
1190 // tentative definitions (implementing behavior 2).
isBitcodeNonCommonDef(MemoryBufferRef mb,StringRef symName,StringRef archiveName)1191 static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName,
1192 StringRef archiveName) {
1193 IRSymtabFile symtabFile = check(readIRSymtab(mb));
1194 for (const irsymtab::Reader::SymbolRef &sym :
1195 symtabFile.TheReader.symbols()) {
1196 if (sym.isGlobal() && sym.getName() == symName)
1197 return !sym.isUndefined() && !sym.isWeak() && !sym.isCommon();
1198 }
1199 return false;
1200 }
1201
1202 template <class ELFT>
isNonCommonDef(MemoryBufferRef mb,StringRef symName,StringRef archiveName)1203 static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
1204 StringRef archiveName) {
1205 ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(mb, archiveName);
1206 StringRef stringtable = obj->getStringTable();
1207
1208 for (auto sym : obj->template getGlobalELFSyms<ELFT>()) {
1209 Expected<StringRef> name = sym.getName(stringtable);
1210 if (name && name.get() == symName)
1211 return sym.isDefined() && sym.getBinding() == STB_GLOBAL &&
1212 !sym.isCommon();
1213 }
1214 return false;
1215 }
1216
isNonCommonDef(MemoryBufferRef mb,StringRef symName,StringRef archiveName)1217 static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
1218 StringRef archiveName) {
1219 switch (getELFKind(mb, archiveName)) {
1220 case ELF32LEKind:
1221 return isNonCommonDef<ELF32LE>(mb, symName, archiveName);
1222 case ELF32BEKind:
1223 return isNonCommonDef<ELF32BE>(mb, symName, archiveName);
1224 case ELF64LEKind:
1225 return isNonCommonDef<ELF64LE>(mb, symName, archiveName);
1226 case ELF64BEKind:
1227 return isNonCommonDef<ELF64BE>(mb, symName, archiveName);
1228 default:
1229 llvm_unreachable("getELFKind");
1230 }
1231 }
1232
1233 unsigned SharedFile::vernauxNum;
1234
1235 // Parse the version definitions in the object file if present, and return a
1236 // vector whose nth element contains a pointer to the Elf_Verdef for version
1237 // identifier n. Version identifiers that are not definitions map to nullptr.
1238 template <typename ELFT>
1239 static SmallVector<const void *, 0>
parseVerdefs(const uint8_t * base,const typename ELFT::Shdr * sec)1240 parseVerdefs(const uint8_t *base, const typename ELFT::Shdr *sec) {
1241 if (!sec)
1242 return {};
1243
1244 // Build the Verdefs array by following the chain of Elf_Verdef objects
1245 // from the start of the .gnu.version_d section.
1246 SmallVector<const void *, 0> verdefs;
1247 const uint8_t *verdef = base + sec->sh_offset;
1248 for (unsigned i = 0, e = sec->sh_info; i != e; ++i) {
1249 auto *curVerdef = reinterpret_cast<const typename ELFT::Verdef *>(verdef);
1250 verdef += curVerdef->vd_next;
1251 unsigned verdefIndex = curVerdef->vd_ndx;
1252 if (verdefIndex >= verdefs.size())
1253 verdefs.resize(verdefIndex + 1);
1254 verdefs[verdefIndex] = curVerdef;
1255 }
1256 return verdefs;
1257 }
1258
1259 // Parse SHT_GNU_verneed to properly set the name of a versioned undefined
1260 // symbol. We detect fatal issues which would cause vulnerabilities, but do not
1261 // implement sophisticated error checking like in llvm-readobj because the value
1262 // of such diagnostics is low.
1263 template <typename ELFT>
parseVerneed(const ELFFile<ELFT> & obj,const typename ELFT::Shdr * sec)1264 std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj,
1265 const typename ELFT::Shdr *sec) {
1266 if (!sec)
1267 return {};
1268 std::vector<uint32_t> verneeds;
1269 ArrayRef<uint8_t> data = CHECK(obj.getSectionContents(*sec), this);
1270 const uint8_t *verneedBuf = data.begin();
1271 for (unsigned i = 0; i != sec->sh_info; ++i) {
1272 if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end())
1273 fatal(toString(this) + " has an invalid Verneed");
1274 auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf);
1275 const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux;
1276 for (unsigned j = 0; j != vn->vn_cnt; ++j) {
1277 if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end())
1278 fatal(toString(this) + " has an invalid Vernaux");
1279 auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf);
1280 if (aux->vna_name >= this->stringTable.size())
1281 fatal(toString(this) + " has a Vernaux with an invalid vna_name");
1282 uint16_t version = aux->vna_other & VERSYM_VERSION;
1283 if (version >= verneeds.size())
1284 verneeds.resize(version + 1);
1285 verneeds[version] = aux->vna_name;
1286 vernauxBuf += aux->vna_next;
1287 }
1288 verneedBuf += vn->vn_next;
1289 }
1290 return verneeds;
1291 }
1292
1293 // We do not usually care about alignments of data in shared object
1294 // files because the loader takes care of it. However, if we promote a
1295 // DSO symbol to point to .bss due to copy relocation, we need to keep
1296 // the original alignment requirements. We infer it in this function.
1297 template <typename ELFT>
getAlignment(ArrayRef<typename ELFT::Shdr> sections,const typename ELFT::Sym & sym)1298 static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> sections,
1299 const typename ELFT::Sym &sym) {
1300 uint64_t ret = UINT64_MAX;
1301 if (sym.st_value)
1302 ret = 1ULL << countTrailingZeros((uint64_t)sym.st_value);
1303 if (0 < sym.st_shndx && sym.st_shndx < sections.size())
1304 ret = std::min<uint64_t>(ret, sections[sym.st_shndx].sh_addralign);
1305 return (ret > UINT32_MAX) ? 0 : ret;
1306 }
1307
1308 // Fully parse the shared object file.
1309 //
1310 // This function parses symbol versions. If a DSO has version information,
1311 // the file has a ".gnu.version_d" section which contains symbol version
1312 // definitions. Each symbol is associated to one version through a table in
1313 // ".gnu.version" section. That table is a parallel array for the symbol
1314 // table, and each table entry contains an index in ".gnu.version_d".
1315 //
1316 // The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for
1317 // VER_NDX_GLOBAL. There's no table entry for these special versions in
1318 // ".gnu.version_d".
1319 //
1320 // The file format for symbol versioning is perhaps a bit more complicated
1321 // than necessary, but you can easily understand the code if you wrap your
1322 // head around the data structure described above.
parse()1323 template <class ELFT> void SharedFile::parse() {
1324 using Elf_Dyn = typename ELFT::Dyn;
1325 using Elf_Shdr = typename ELFT::Shdr;
1326 using Elf_Sym = typename ELFT::Sym;
1327 using Elf_Verdef = typename ELFT::Verdef;
1328 using Elf_Versym = typename ELFT::Versym;
1329
1330 ArrayRef<Elf_Dyn> dynamicTags;
1331 const ELFFile<ELFT> obj = this->getObj<ELFT>();
1332 ArrayRef<Elf_Shdr> sections = getELFShdrs<ELFT>();
1333
1334 const Elf_Shdr *versymSec = nullptr;
1335 const Elf_Shdr *verdefSec = nullptr;
1336 const Elf_Shdr *verneedSec = nullptr;
1337
1338 // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
1339 for (const Elf_Shdr &sec : sections) {
1340 switch (sec.sh_type) {
1341 default:
1342 continue;
1343 case SHT_DYNAMIC:
1344 dynamicTags =
1345 CHECK(obj.template getSectionContentsAsArray<Elf_Dyn>(sec), this);
1346 break;
1347 case SHT_GNU_versym:
1348 versymSec = &sec;
1349 break;
1350 case SHT_GNU_verdef:
1351 verdefSec = &sec;
1352 break;
1353 case SHT_GNU_verneed:
1354 verneedSec = &sec;
1355 break;
1356 }
1357 }
1358
1359 if (versymSec && numELFSyms == 0) {
1360 error("SHT_GNU_versym should be associated with symbol table");
1361 return;
1362 }
1363
1364 // Search for a DT_SONAME tag to initialize this->soName.
1365 for (const Elf_Dyn &dyn : dynamicTags) {
1366 if (dyn.d_tag == DT_NEEDED) {
1367 uint64_t val = dyn.getVal();
1368 if (val >= this->stringTable.size())
1369 fatal(toString(this) + ": invalid DT_NEEDED entry");
1370 dtNeeded.push_back(this->stringTable.data() + val);
1371 } else if (dyn.d_tag == DT_SONAME) {
1372 uint64_t val = dyn.getVal();
1373 if (val >= this->stringTable.size())
1374 fatal(toString(this) + ": invalid DT_SONAME entry");
1375 soName = this->stringTable.data() + val;
1376 }
1377 }
1378
1379 // DSOs are uniquified not by filename but by soname.
1380 DenseMap<CachedHashStringRef, SharedFile *>::iterator it;
1381 bool wasInserted;
1382 std::tie(it, wasInserted) =
1383 symtab->soNames.try_emplace(CachedHashStringRef(soName), this);
1384
1385 // If a DSO appears more than once on the command line with and without
1386 // --as-needed, --no-as-needed takes precedence over --as-needed because a
1387 // user can add an extra DSO with --no-as-needed to force it to be added to
1388 // the dependency list.
1389 it->second->isNeeded |= isNeeded;
1390 if (!wasInserted)
1391 return;
1392
1393 ctx->sharedFiles.push_back(this);
1394
1395 verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec);
1396 std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec);
1397
1398 // Parse ".gnu.version" section which is a parallel array for the symbol
1399 // table. If a given file doesn't have a ".gnu.version" section, we use
1400 // VER_NDX_GLOBAL.
1401 size_t size = numELFSyms - firstGlobal;
1402 std::vector<uint16_t> versyms(size, VER_NDX_GLOBAL);
1403 if (versymSec) {
1404 ArrayRef<Elf_Versym> versym =
1405 CHECK(obj.template getSectionContentsAsArray<Elf_Versym>(*versymSec),
1406 this)
1407 .slice(firstGlobal);
1408 for (size_t i = 0; i < size; ++i)
1409 versyms[i] = versym[i].vs_index;
1410 }
1411
1412 // System libraries can have a lot of symbols with versions. Using a
1413 // fixed buffer for computing the versions name (foo@ver) can save a
1414 // lot of allocations.
1415 SmallString<0> versionedNameBuffer;
1416
1417 // Add symbols to the symbol table.
1418 SymbolTable &symtab = *elf::symtab;
1419 ArrayRef<Elf_Sym> syms = this->getGlobalELFSyms<ELFT>();
1420 for (size_t i = 0, e = syms.size(); i != e; ++i) {
1421 const Elf_Sym &sym = syms[i];
1422
1423 // ELF spec requires that all local symbols precede weak or global
1424 // symbols in each symbol table, and the index of first non-local symbol
1425 // is stored to sh_info. If a local symbol appears after some non-local
1426 // symbol, that's a violation of the spec.
1427 StringRef name = CHECK(sym.getName(stringTable), this);
1428 if (sym.getBinding() == STB_LOCAL) {
1429 warn("found local symbol '" + name +
1430 "' in global part of symbol table in file " + toString(this));
1431 continue;
1432 }
1433
1434 uint16_t idx = versyms[i] & ~VERSYM_HIDDEN;
1435 if (sym.isUndefined()) {
1436 // For unversioned undefined symbols, VER_NDX_GLOBAL makes more sense but
1437 // as of binutils 2.34, GNU ld produces VER_NDX_LOCAL.
1438 if (idx != VER_NDX_LOCAL && idx != VER_NDX_GLOBAL) {
1439 if (idx >= verneeds.size()) {
1440 error("corrupt input file: version need index " + Twine(idx) +
1441 " for symbol " + name + " is out of bounds\n>>> defined in " +
1442 toString(this));
1443 continue;
1444 }
1445 StringRef verName = stringTable.data() + verneeds[idx];
1446 versionedNameBuffer.clear();
1447 name = saver().save(
1448 (name + "@" + verName).toStringRef(versionedNameBuffer));
1449 }
1450 Symbol *s = symtab.addSymbol(
1451 Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()});
1452 s->exportDynamic = true;
1453 if (s->isUndefined() && sym.getBinding() != STB_WEAK &&
1454 config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore)
1455 requiredSymbols.push_back(s);
1456 continue;
1457 }
1458
1459 // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly
1460 // assigns VER_NDX_LOCAL to this section global symbol. Here is a
1461 // workaround for this bug.
1462 if (config->emachine == EM_MIPS && idx == VER_NDX_LOCAL &&
1463 name == "_gp_disp")
1464 continue;
1465
1466 uint32_t alignment = getAlignment<ELFT>(sections, sym);
1467 if (!(versyms[i] & VERSYM_HIDDEN)) {
1468 auto *s = symtab.addSymbol(
1469 SharedSymbol{*this, name, sym.getBinding(), sym.st_other,
1470 sym.getType(), sym.st_value, sym.st_size, alignment});
1471 if (s->file == this)
1472 s->verdefIndex = idx;
1473 }
1474
1475 // Also add the symbol with the versioned name to handle undefined symbols
1476 // with explicit versions.
1477 if (idx == VER_NDX_GLOBAL)
1478 continue;
1479
1480 if (idx >= verdefs.size() || idx == VER_NDX_LOCAL) {
1481 error("corrupt input file: version definition index " + Twine(idx) +
1482 " for symbol " + name + " is out of bounds\n>>> defined in " +
1483 toString(this));
1484 continue;
1485 }
1486
1487 StringRef verName =
1488 stringTable.data() +
1489 reinterpret_cast<const Elf_Verdef *>(verdefs[idx])->getAux()->vda_name;
1490 versionedNameBuffer.clear();
1491 name = (name + "@" + verName).toStringRef(versionedNameBuffer);
1492 auto *s = symtab.addSymbol(
1493 SharedSymbol{*this, saver().save(name), sym.getBinding(), sym.st_other,
1494 sym.getType(), sym.st_value, sym.st_size, alignment});
1495 if (s->file == this)
1496 s->verdefIndex = idx;
1497 }
1498 }
1499
getBitcodeELFKind(const Triple & t)1500 static ELFKind getBitcodeELFKind(const Triple &t) {
1501 if (t.isLittleEndian())
1502 return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
1503 return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
1504 }
1505
getBitcodeMachineKind(StringRef path,const Triple & t)1506 static uint16_t getBitcodeMachineKind(StringRef path, const Triple &t) {
1507 switch (t.getArch()) {
1508 case Triple::aarch64:
1509 case Triple::aarch64_be:
1510 return EM_AARCH64;
1511 case Triple::amdgcn:
1512 case Triple::r600:
1513 return EM_AMDGPU;
1514 case Triple::arm:
1515 case Triple::thumb:
1516 return EM_ARM;
1517 case Triple::avr:
1518 return EM_AVR;
1519 case Triple::hexagon:
1520 return EM_HEXAGON;
1521 case Triple::mips:
1522 case Triple::mipsel:
1523 case Triple::mips64:
1524 case Triple::mips64el:
1525 return EM_MIPS;
1526 case Triple::msp430:
1527 return EM_MSP430;
1528 case Triple::ppc:
1529 case Triple::ppcle:
1530 return EM_PPC;
1531 case Triple::ppc64:
1532 case Triple::ppc64le:
1533 return EM_PPC64;
1534 case Triple::riscv32:
1535 case Triple::riscv64:
1536 return EM_RISCV;
1537 case Triple::x86:
1538 return t.isOSIAMCU() ? EM_IAMCU : EM_386;
1539 case Triple::x86_64:
1540 return EM_X86_64;
1541 default:
1542 error(path + ": could not infer e_machine from bitcode target triple " +
1543 t.str());
1544 return EM_NONE;
1545 }
1546 }
1547
getOsAbi(const Triple & t)1548 static uint8_t getOsAbi(const Triple &t) {
1549 switch (t.getOS()) {
1550 case Triple::AMDHSA:
1551 return ELF::ELFOSABI_AMDGPU_HSA;
1552 case Triple::AMDPAL:
1553 return ELF::ELFOSABI_AMDGPU_PAL;
1554 case Triple::Mesa3D:
1555 return ELF::ELFOSABI_AMDGPU_MESA3D;
1556 default:
1557 return ELF::ELFOSABI_NONE;
1558 }
1559 }
1560
BitcodeFile(MemoryBufferRef mb,StringRef archiveName,uint64_t offsetInArchive,bool lazy)1561 BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
1562 uint64_t offsetInArchive, bool lazy)
1563 : InputFile(BitcodeKind, mb) {
1564 this->archiveName = archiveName;
1565 this->lazy = lazy;
1566
1567 std::string path = mb.getBufferIdentifier().str();
1568 if (config->thinLTOIndexOnly)
1569 path = replaceThinLTOSuffix(mb.getBufferIdentifier());
1570
1571 // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1572 // name. If two archives define two members with the same name, this
1573 // causes a collision which result in only one of the objects being taken
1574 // into consideration at LTO time (which very likely causes undefined
1575 // symbols later in the link stage). So we append file offset to make
1576 // filename unique.
1577 StringRef name = archiveName.empty()
1578 ? saver().save(path)
1579 : saver().save(archiveName + "(" + path::filename(path) +
1580 " at " + utostr(offsetInArchive) + ")");
1581 MemoryBufferRef mbref(mb.getBuffer(), name);
1582
1583 obj = CHECK(lto::InputFile::create(mbref), this);
1584
1585 Triple t(obj->getTargetTriple());
1586 ekind = getBitcodeELFKind(t);
1587 emachine = getBitcodeMachineKind(mb.getBufferIdentifier(), t);
1588 osabi = getOsAbi(t);
1589 }
1590
mapVisibility(GlobalValue::VisibilityTypes gvVisibility)1591 static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {
1592 switch (gvVisibility) {
1593 case GlobalValue::DefaultVisibility:
1594 return STV_DEFAULT;
1595 case GlobalValue::HiddenVisibility:
1596 return STV_HIDDEN;
1597 case GlobalValue::ProtectedVisibility:
1598 return STV_PROTECTED;
1599 }
1600 llvm_unreachable("unknown visibility");
1601 }
1602
1603 template <class ELFT>
1604 static void
createBitcodeSymbol(Symbol * & sym,const std::vector<bool> & keptComdats,const lto::InputFile::Symbol & objSym,BitcodeFile & f)1605 createBitcodeSymbol(Symbol *&sym, const std::vector<bool> &keptComdats,
1606 const lto::InputFile::Symbol &objSym, BitcodeFile &f) {
1607 uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL;
1608 uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE;
1609 uint8_t visibility = mapVisibility(objSym.getVisibility());
1610
1611 if (!sym)
1612 sym = symtab->insert(saver().save(objSym.getName()));
1613
1614 int c = objSym.getComdatIndex();
1615 if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) {
1616 Undefined newSym(&f, StringRef(), binding, visibility, type);
1617 sym->resolve(newSym);
1618 sym->referenced = true;
1619 return;
1620 }
1621
1622 if (objSym.isCommon()) {
1623 sym->resolve(CommonSymbol{&f, StringRef(), binding, visibility, STT_OBJECT,
1624 objSym.getCommonAlignment(),
1625 objSym.getCommonSize()});
1626 } else {
1627 Defined newSym(&f, StringRef(), binding, visibility, type, 0, 0, nullptr);
1628 if (objSym.canBeOmittedFromSymbolTable())
1629 newSym.exportDynamic = false;
1630 sym->resolve(newSym);
1631 }
1632 }
1633
parse()1634 template <class ELFT> void BitcodeFile::parse() {
1635 for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable()) {
1636 keptComdats.push_back(
1637 s.second == Comdat::NoDeduplicate ||
1638 symtab->comdatGroups.try_emplace(CachedHashStringRef(s.first), this)
1639 .second);
1640 }
1641
1642 symbols.resize(obj->symbols().size());
1643 // Process defined symbols first. See the comment in
1644 // ObjFile<ELFT>::initializeSymbols.
1645 for (auto it : llvm::enumerate(obj->symbols()))
1646 if (!it.value().isUndefined()) {
1647 Symbol *&sym = symbols[it.index()];
1648 createBitcodeSymbol<ELFT>(sym, keptComdats, it.value(), *this);
1649 }
1650 for (auto it : llvm::enumerate(obj->symbols()))
1651 if (it.value().isUndefined()) {
1652 Symbol *&sym = symbols[it.index()];
1653 createBitcodeSymbol<ELFT>(sym, keptComdats, it.value(), *this);
1654 }
1655
1656 for (auto l : obj->getDependentLibraries())
1657 addDependentLibrary(l, this);
1658 }
1659
parseLazy()1660 void BitcodeFile::parseLazy() {
1661 SymbolTable &symtab = *elf::symtab;
1662 symbols.resize(obj->symbols().size());
1663 for (auto it : llvm::enumerate(obj->symbols()))
1664 if (!it.value().isUndefined()) {
1665 auto *sym = symtab.insert(saver().save(it.value().getName()));
1666 sym->resolve(LazyObject{*this});
1667 symbols[it.index()] = sym;
1668 }
1669 }
1670
postParse()1671 void BitcodeFile::postParse() {
1672 for (auto it : llvm::enumerate(obj->symbols())) {
1673 const Symbol &sym = *symbols[it.index()];
1674 const auto &objSym = it.value();
1675 if (sym.file == this || !sym.isDefined() || objSym.isUndefined() ||
1676 objSym.isCommon() || objSym.isWeak())
1677 continue;
1678 int c = objSym.getComdatIndex();
1679 if (c != -1 && !keptComdats[c])
1680 continue;
1681 reportDuplicate(sym, this, nullptr, 0);
1682 }
1683 }
1684
parse()1685 void BinaryFile::parse() {
1686 ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer());
1687 auto *section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
1688 8, data, ".data");
1689 sections.push_back(section);
1690
1691 // For each input file foo that is embedded to a result as a binary
1692 // blob, we define _binary_foo_{start,end,size} symbols, so that
1693 // user programs can access blobs by name. Non-alphanumeric
1694 // characters in a filename are replaced with underscore.
1695 std::string s = "_binary_" + mb.getBufferIdentifier().str();
1696 for (size_t i = 0; i < s.size(); ++i)
1697 if (!isAlnum(s[i]))
1698 s[i] = '_';
1699
1700 llvm::StringSaver &saver = lld::saver();
1701
1702 symtab->addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_start"),
1703 STB_GLOBAL, STV_DEFAULT, STT_OBJECT, 0,
1704 0, section});
1705 symtab->addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_end"),
1706 STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
1707 data.size(), 0, section});
1708 symtab->addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_size"),
1709 STB_GLOBAL, STV_DEFAULT, STT_OBJECT,
1710 data.size(), 0, nullptr});
1711 }
1712
createObjFile(MemoryBufferRef mb,StringRef archiveName,bool lazy)1713 ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName,
1714 bool lazy) {
1715 ELFFileBase *f;
1716 switch (getELFKind(mb, archiveName)) {
1717 case ELF32LEKind:
1718 f = make<ObjFile<ELF32LE>>(mb, archiveName);
1719 break;
1720 case ELF32BEKind:
1721 f = make<ObjFile<ELF32BE>>(mb, archiveName);
1722 break;
1723 case ELF64LEKind:
1724 f = make<ObjFile<ELF64LE>>(mb, archiveName);
1725 break;
1726 case ELF64BEKind:
1727 f = make<ObjFile<ELF64BE>>(mb, archiveName);
1728 break;
1729 default:
1730 llvm_unreachable("getELFKind");
1731 }
1732 f->lazy = lazy;
1733 return f;
1734 }
1735
parseLazy()1736 template <class ELFT> void ObjFile<ELFT>::parseLazy() {
1737 const ArrayRef<typename ELFT::Sym> eSyms = this->getELFSyms<ELFT>();
1738 SymbolTable &symtab = *elf::symtab;
1739
1740 symbols.resize(eSyms.size());
1741 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)
1742 if (eSyms[i].st_shndx != SHN_UNDEF)
1743 symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this));
1744
1745 // Replace existing symbols with LazyObject symbols.
1746 //
1747 // resolve() may trigger this->extract() if an existing symbol is an undefined
1748 // symbol. If that happens, this function has served its purpose, and we can
1749 // exit from the loop early.
1750 for (Symbol *sym : makeArrayRef(symbols).slice(firstGlobal))
1751 if (sym) {
1752 sym->resolve(LazyObject{*this});
1753 if (!lazy)
1754 return;
1755 }
1756 }
1757
shouldExtractForCommon(StringRef name)1758 bool InputFile::shouldExtractForCommon(StringRef name) {
1759 if (isa<BitcodeFile>(this))
1760 return isBitcodeNonCommonDef(mb, name, archiveName);
1761
1762 return isNonCommonDef(mb, name, archiveName);
1763 }
1764
replaceThinLTOSuffix(StringRef path)1765 std::string elf::replaceThinLTOSuffix(StringRef path) {
1766 StringRef suffix = config->thinLTOObjectSuffixReplace.first;
1767 StringRef repl = config->thinLTOObjectSuffixReplace.second;
1768
1769 if (path.consume_back(suffix))
1770 return (path + repl).str();
1771 return std::string(path);
1772 }
1773
1774 template void BitcodeFile::parse<ELF32LE>();
1775 template void BitcodeFile::parse<ELF32BE>();
1776 template void BitcodeFile::parse<ELF64LE>();
1777 template void BitcodeFile::parse<ELF64BE>();
1778
1779 template class elf::ObjFile<ELF32LE>;
1780 template class elf::ObjFile<ELF32BE>;
1781 template class elf::ObjFile<ELF64LE>;
1782 template class elf::ObjFile<ELF64BE>;
1783
1784 template void SharedFile::parse<ELF32LE>();
1785 template void SharedFile::parse<ELF32BE>();
1786 template void SharedFile::parse<ELF64LE>();
1787 template void SharedFile::parse<ELF64BE>();
1788