PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/llvm-project/lld/ELF/InputFiles.cpp

https://github.com/freebsd/freebsd
C++ | 1311 lines | 886 code | 149 blank | 276 comment | 292 complexity | c966b72d70135f6fa5e56252e72e3d01 MD5 | raw file
  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. #include "InputFiles.h"
  9. #include "Driver.h"
  10. #include "InputSection.h"
  11. #include "LinkerScript.h"
  12. #include "SymbolTable.h"
  13. #include "Symbols.h"
  14. #include "SyntheticSections.h"
  15. #include "Target.h"
  16. #include "lld/Common/CommonLinkerContext.h"
  17. #include "lld/Common/DWARF.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/CodeGen/Analysis.h"
  20. #include "llvm/IR/LLVMContext.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/LTO/LTO.h"
  23. #include "llvm/MC/StringTableBuilder.h"
  24. #include "llvm/Object/ELFObjectFile.h"
  25. #include "llvm/Support/ARMAttributeParser.h"
  26. #include "llvm/Support/ARMBuildAttributes.h"
  27. #include "llvm/Support/Endian.h"
  28. #include "llvm/Support/Path.h"
  29. #include "llvm/Support/RISCVAttributeParser.h"
  30. #include "llvm/Support/TarWriter.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. using namespace llvm;
  33. using namespace llvm::ELF;
  34. using namespace llvm::object;
  35. using namespace llvm::sys;
  36. using namespace llvm::sys::fs;
  37. using namespace llvm::support::endian;
  38. using namespace lld;
  39. using namespace lld::elf;
  40. bool InputFile::isInGroup;
  41. uint32_t InputFile::nextGroupId;
  42. SmallVector<std::unique_ptr<MemoryBuffer>> elf::memoryBuffers;
  43. SmallVector<ArchiveFile *, 0> elf::archiveFiles;
  44. SmallVector<BinaryFile *, 0> elf::binaryFiles;
  45. SmallVector<BitcodeFile *, 0> elf::bitcodeFiles;
  46. SmallVector<BitcodeFile *, 0> elf::lazyBitcodeFiles;
  47. SmallVector<ELFFileBase *, 0> elf::objectFiles;
  48. SmallVector<SharedFile *, 0> elf::sharedFiles;
  49. std::unique_ptr<TarWriter> elf::tar;
  50. // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
  51. std::string lld::toString(const InputFile *f) {
  52. if (!f)
  53. return "<internal>";
  54. if (f->toStringCache.empty()) {
  55. if (f->archiveName.empty())
  56. f->toStringCache = f->getName();
  57. else
  58. (f->archiveName + "(" + f->getName() + ")").toVector(f->toStringCache);
  59. }
  60. return std::string(f->toStringCache);
  61. }
  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. auto report = [&](StringRef msg) {
  67. StringRef filename = mb.getBufferIdentifier();
  68. if (archiveName.empty())
  69. fatal(filename + ": " + msg);
  70. else
  71. fatal(archiveName + "(" + filename + "): " + msg);
  72. };
  73. if (!mb.getBuffer().startswith(ElfMagic))
  74. report("not an ELF file");
  75. if (endian != ELFDATA2LSB && endian != ELFDATA2MSB)
  76. report("corrupted ELF file: invalid data encoding");
  77. if (size != ELFCLASS32 && size != ELFCLASS64)
  78. report("corrupted ELF file: invalid file class");
  79. size_t bufSize = mb.getBuffer().size();
  80. if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) ||
  81. (size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr)))
  82. report("corrupted ELF file: file is too short");
  83. if (size == ELFCLASS32)
  84. return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
  85. return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
  86. }
  87. InputFile::InputFile(Kind k, MemoryBufferRef m)
  88. : mb(m), groupId(nextGroupId), fileKind(k) {
  89. // All files within the same --{start,end}-group get the same group ID.
  90. // Otherwise, a new file will get a new group ID.
  91. if (!isInGroup)
  92. ++nextGroupId;
  93. }
  94. Optional<MemoryBufferRef> elf::readFile(StringRef path) {
  95. llvm::TimeTraceScope timeScope("Load input files", path);
  96. // The --chroot option changes our virtual root directory.
  97. // This is useful when you are dealing with files created by --reproduce.
  98. if (!config->chroot.empty() && path.startswith("/"))
  99. path = saver().save(config->chroot + path);
  100. log(path);
  101. config->dependencyFiles.insert(llvm::CachedHashString(path));
  102. auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false,
  103. /*RequiresNullTerminator=*/false);
  104. if (auto ec = mbOrErr.getError()) {
  105. error("cannot open " + path + ": " + ec.message());
  106. return None;
  107. }
  108. MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef();
  109. memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership
  110. if (tar)
  111. tar->append(relativeToRoot(path), mbref.getBuffer());
  112. return mbref;
  113. }
  114. // All input object files must be for the same architecture
  115. // (e.g. it does not make sense to link x86 object files with
  116. // MIPS object files.) This function checks for that error.
  117. static bool isCompatible(InputFile *file) {
  118. if (!file->isElf() && !isa<BitcodeFile>(file))
  119. return true;
  120. if (file->ekind == config->ekind && file->emachine == config->emachine) {
  121. if (config->emachine != EM_MIPS)
  122. return true;
  123. if (isMipsN32Abi(file) == config->mipsN32Abi)
  124. return true;
  125. }
  126. StringRef target =
  127. !config->bfdname.empty() ? config->bfdname : config->emulation;
  128. if (!target.empty()) {
  129. error(toString(file) + " is incompatible with " + target);
  130. return false;
  131. }
  132. InputFile *existing = nullptr;
  133. if (!objectFiles.empty())
  134. existing = objectFiles[0];
  135. else if (!sharedFiles.empty())
  136. existing = sharedFiles[0];
  137. else if (!bitcodeFiles.empty())
  138. existing = bitcodeFiles[0];
  139. std::string with;
  140. if (existing)
  141. with = " with " + toString(existing);
  142. error(toString(file) + " is incompatible" + with);
  143. return false;
  144. }
  145. template <class ELFT> static void doParseFile(InputFile *file) {
  146. if (!isCompatible(file))
  147. return;
  148. // Binary file
  149. if (auto *f = dyn_cast<BinaryFile>(file)) {
  150. binaryFiles.push_back(f);
  151. f->parse();
  152. return;
  153. }
  154. // .a file
  155. if (auto *f = dyn_cast<ArchiveFile>(file)) {
  156. archiveFiles.push_back(f);
  157. f->parse();
  158. return;
  159. }
  160. // Lazy object file
  161. if (file->lazy) {
  162. if (auto *f = dyn_cast<BitcodeFile>(file)) {
  163. lazyBitcodeFiles.push_back(f);
  164. f->parseLazy();
  165. } else {
  166. cast<ObjFile<ELFT>>(file)->parseLazy();
  167. }
  168. return;
  169. }
  170. if (config->trace)
  171. message(toString(file));
  172. // .so file
  173. if (auto *f = dyn_cast<SharedFile>(file)) {
  174. f->parse<ELFT>();
  175. return;
  176. }
  177. // LLVM bitcode file
  178. if (auto *f = dyn_cast<BitcodeFile>(file)) {
  179. bitcodeFiles.push_back(f);
  180. f->parse<ELFT>();
  181. return;
  182. }
  183. // Regular object file
  184. objectFiles.push_back(cast<ELFFileBase>(file));
  185. cast<ObjFile<ELFT>>(file)->parse();
  186. }
  187. // Add symbols in File to the symbol table.
  188. void elf::parseFile(InputFile *file) { invokeELFT(doParseFile, file); }
  189. // Concatenates arguments to construct a string representing an error location.
  190. static std::string createFileLineMsg(StringRef path, unsigned line) {
  191. std::string filename = std::string(path::filename(path));
  192. std::string lineno = ":" + std::to_string(line);
  193. if (filename == path)
  194. return filename + lineno;
  195. return filename + lineno + " (" + path.str() + lineno + ")";
  196. }
  197. template <class ELFT>
  198. static std::string getSrcMsgAux(ObjFile<ELFT> &file, const Symbol &sym,
  199. InputSectionBase &sec, uint64_t offset) {
  200. // In DWARF, functions and variables are stored to different places.
  201. // First, lookup a function for a given offset.
  202. if (Optional<DILineInfo> info = file.getDILineInfo(&sec, offset))
  203. return createFileLineMsg(info->FileName, info->Line);
  204. // If it failed, lookup again as a variable.
  205. if (Optional<std::pair<std::string, unsigned>> fileLine =
  206. file.getVariableLoc(sym.getName()))
  207. return createFileLineMsg(fileLine->first, fileLine->second);
  208. // File.sourceFile contains STT_FILE symbol, and that is a last resort.
  209. return std::string(file.sourceFile);
  210. }
  211. std::string InputFile::getSrcMsg(const Symbol &sym, InputSectionBase &sec,
  212. uint64_t offset) {
  213. if (kind() != ObjKind)
  214. return "";
  215. switch (config->ekind) {
  216. default:
  217. llvm_unreachable("Invalid kind");
  218. case ELF32LEKind:
  219. return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), sym, sec, offset);
  220. case ELF32BEKind:
  221. return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), sym, sec, offset);
  222. case ELF64LEKind:
  223. return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), sym, sec, offset);
  224. case ELF64BEKind:
  225. return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), sym, sec, offset);
  226. }
  227. }
  228. StringRef InputFile::getNameForScript() const {
  229. if (archiveName.empty())
  230. return getName();
  231. if (nameForScriptCache.empty())
  232. nameForScriptCache = (archiveName + Twine(':') + getName()).str();
  233. return nameForScriptCache;
  234. }
  235. template <class ELFT> DWARFCache *ObjFile<ELFT>::getDwarf() {
  236. llvm::call_once(initDwarf, [this]() {
  237. dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(
  238. std::make_unique<LLDDwarfObj<ELFT>>(this), "",
  239. [&](Error err) { warn(getName() + ": " + toString(std::move(err))); },
  240. [&](Error warning) {
  241. warn(getName() + ": " + toString(std::move(warning)));
  242. }));
  243. });
  244. return dwarf.get();
  245. }
  246. // Returns the pair of file name and line number describing location of data
  247. // object (variable, array, etc) definition.
  248. template <class ELFT>
  249. Optional<std::pair<std::string, unsigned>>
  250. ObjFile<ELFT>::getVariableLoc(StringRef name) {
  251. return getDwarf()->getVariableLoc(name);
  252. }
  253. // Returns source line information for a given offset
  254. // using DWARF debug info.
  255. template <class ELFT>
  256. Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s,
  257. uint64_t offset) {
  258. // Detect SectionIndex for specified section.
  259. uint64_t sectionIndex = object::SectionedAddress::UndefSection;
  260. ArrayRef<InputSectionBase *> sections = s->file->getSections();
  261. for (uint64_t curIndex = 0; curIndex < sections.size(); ++curIndex) {
  262. if (s == sections[curIndex]) {
  263. sectionIndex = curIndex;
  264. break;
  265. }
  266. }
  267. return getDwarf()->getDILineInfo(offset, sectionIndex);
  268. }
  269. ELFFileBase::ELFFileBase(Kind k, MemoryBufferRef mb) : InputFile(k, mb) {
  270. ekind = getELFKind(mb, "");
  271. switch (ekind) {
  272. case ELF32LEKind:
  273. init<ELF32LE>();
  274. break;
  275. case ELF32BEKind:
  276. init<ELF32BE>();
  277. break;
  278. case ELF64LEKind:
  279. init<ELF64LE>();
  280. break;
  281. case ELF64BEKind:
  282. init<ELF64BE>();
  283. break;
  284. default:
  285. llvm_unreachable("getELFKind");
  286. }
  287. }
  288. template <typename Elf_Shdr>
  289. static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) {
  290. for (const Elf_Shdr &sec : sections)
  291. if (sec.sh_type == type)
  292. return &sec;
  293. return nullptr;
  294. }
  295. template <class ELFT> void ELFFileBase::init() {
  296. using Elf_Shdr = typename ELFT::Shdr;
  297. using Elf_Sym = typename ELFT::Sym;
  298. // Initialize trivial attributes.
  299. const ELFFile<ELFT> &obj = getObj<ELFT>();
  300. emachine = obj.getHeader().e_machine;
  301. osabi = obj.getHeader().e_ident[llvm::ELF::EI_OSABI];
  302. abiVersion = obj.getHeader().e_ident[llvm::ELF::EI_ABIVERSION];
  303. ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this);
  304. elfShdrs = sections.data();
  305. numELFShdrs = sections.size();
  306. // Find a symbol table.
  307. bool isDSO =
  308. (identify_magic(mb.getBuffer()) == file_magic::elf_shared_object);
  309. const Elf_Shdr *symtabSec =
  310. findSection(sections, isDSO ? SHT_DYNSYM : SHT_SYMTAB);
  311. if (!symtabSec)
  312. return;
  313. // Initialize members corresponding to a symbol table.
  314. firstGlobal = symtabSec->sh_info;
  315. ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(symtabSec), this);
  316. if (firstGlobal == 0 || firstGlobal > eSyms.size())
  317. fatal(toString(this) + ": invalid sh_info in symbol table");
  318. elfSyms = reinterpret_cast<const void *>(eSyms.data());
  319. numELFSyms = uint32_t(eSyms.size());
  320. stringTable = CHECK(obj.getStringTableForSymtab(*symtabSec, sections), this);
  321. }
  322. template <class ELFT>
  323. uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const {
  324. return CHECK(
  325. this->getObj().getSectionIndex(sym, getELFSyms<ELFT>(), shndxTable),
  326. this);
  327. }
  328. template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {
  329. object::ELFFile<ELFT> obj = this->getObj();
  330. // Read a section table. justSymbols is usually false.
  331. if (this->justSymbols)
  332. initializeJustSymbols();
  333. else
  334. initializeSections(ignoreComdats, obj);
  335. // Read a symbol table.
  336. initializeSymbols(obj);
  337. }
  338. // Sections with SHT_GROUP and comdat bits define comdat section groups.
  339. // They are identified and deduplicated by group name. This function
  340. // returns a group name.
  341. template <class ELFT>
  342. StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections,
  343. const Elf_Shdr &sec) {
  344. typename ELFT::SymRange symbols = this->getELFSyms<ELFT>();
  345. if (sec.sh_info >= symbols.size())
  346. fatal(toString(this) + ": invalid symbol index");
  347. const typename ELFT::Sym &sym = symbols[sec.sh_info];
  348. return CHECK(sym.getName(this->stringTable), this);
  349. }
  350. template <class ELFT>
  351. bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {
  352. // On a regular link we don't merge sections if -O0 (default is -O1). This
  353. // sometimes makes the linker significantly faster, although the output will
  354. // be bigger.
  355. //
  356. // Doing the same for -r would create a problem as it would combine sections
  357. // with different sh_entsize. One option would be to just copy every SHF_MERGE
  358. // section as is to the output. While this would produce a valid ELF file with
  359. // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
  360. // they see two .debug_str. We could have separate logic for combining
  361. // SHF_MERGE sections based both on their name and sh_entsize, but that seems
  362. // to be more trouble than it is worth. Instead, we just use the regular (-O1)
  363. // logic for -r.
  364. if (config->optimize == 0 && !config->relocatable)
  365. return false;
  366. // A mergeable section with size 0 is useless because they don't have
  367. // any data to merge. A mergeable string section with size 0 can be
  368. // argued as invalid because it doesn't end with a null character.
  369. // We'll avoid a mess by handling them as if they were non-mergeable.
  370. if (sec.sh_size == 0)
  371. return false;
  372. // Check for sh_entsize. The ELF spec is not clear about the zero
  373. // sh_entsize. It says that "the member [sh_entsize] contains 0 if
  374. // the section does not hold a table of fixed-size entries". We know
  375. // that Rust 1.13 produces a string mergeable section with a zero
  376. // sh_entsize. Here we just accept it rather than being picky about it.
  377. uint64_t entSize = sec.sh_entsize;
  378. if (entSize == 0)
  379. return false;
  380. if (sec.sh_size % entSize)
  381. fatal(toString(this) + ":(" + name + "): SHF_MERGE section size (" +
  382. Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" +
  383. Twine(entSize) + ")");
  384. if (sec.sh_flags & SHF_WRITE)
  385. fatal(toString(this) + ":(" + name +
  386. "): writable SHF_MERGE section is not supported");
  387. return true;
  388. }
  389. // This is for --just-symbols.
  390. //
  391. // --just-symbols is a very minor feature that allows you to link your
  392. // output against other existing program, so that if you load both your
  393. // program and the other program into memory, your output can refer the
  394. // other program's symbols.
  395. //
  396. // When the option is given, we link "just symbols". The section table is
  397. // initialized with null pointers.
  398. template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {
  399. sections.resize(numELFShdrs);
  400. }
  401. // An ELF object file may contain a `.deplibs` section. If it exists, the
  402. // section contains a list of library specifiers such as `m` for libm. This
  403. // function resolves a given name by finding the first matching library checking
  404. // the various ways that a library can be specified to LLD. This ELF extension
  405. // is a form of autolinking and is called `dependent libraries`. It is currently
  406. // unique to LLVM and lld.
  407. static void addDependentLibrary(StringRef specifier, const InputFile *f) {
  408. if (!config->dependentLibraries)
  409. return;
  410. if (Optional<std::string> s = searchLibraryBaseName(specifier))
  411. driver->addFile(*s, /*withLOption=*/true);
  412. else if (Optional<std::string> s = findFromSearchPaths(specifier))
  413. driver->addFile(*s, /*withLOption=*/true);
  414. else if (fs::exists(specifier))
  415. driver->addFile(specifier, /*withLOption=*/false);
  416. else
  417. error(toString(f) +
  418. ": unable to find library from dependent library specifier: " +
  419. specifier);
  420. }
  421. // Record the membership of a section group so that in the garbage collection
  422. // pass, section group members are kept or discarded as a unit.
  423. template <class ELFT>
  424. static void handleSectionGroup(ArrayRef<InputSectionBase *> sections,
  425. ArrayRef<typename ELFT::Word> entries) {
  426. bool hasAlloc = false;
  427. for (uint32_t index : entries.slice(1)) {
  428. if (index >= sections.size())
  429. return;
  430. if (InputSectionBase *s = sections[index])
  431. if (s != &InputSection::discarded && s->flags & SHF_ALLOC)
  432. hasAlloc = true;
  433. }
  434. // If any member has the SHF_ALLOC flag, the whole group is subject to garbage
  435. // collection. See the comment in markLive(). This rule retains .debug_types
  436. // and .rela.debug_types.
  437. if (!hasAlloc)
  438. return;
  439. // Connect the members in a circular doubly-linked list via
  440. // nextInSectionGroup.
  441. InputSectionBase *head;
  442. InputSectionBase *prev = nullptr;
  443. for (uint32_t index : entries.slice(1)) {
  444. InputSectionBase *s = sections[index];
  445. if (!s || s == &InputSection::discarded)
  446. continue;
  447. if (prev)
  448. prev->nextInSectionGroup = s;
  449. else
  450. head = s;
  451. prev = s;
  452. }
  453. if (prev)
  454. prev->nextInSectionGroup = head;
  455. }
  456. template <class ELFT>
  457. void ObjFile<ELFT>::initializeSections(bool ignoreComdats,
  458. const llvm::object::ELFFile<ELFT> &obj) {
  459. ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>();
  460. StringRef shstrtab = CHECK(obj.getSectionStringTable(objSections), this);
  461. uint64_t size = objSections.size();
  462. this->sections.resize(size);
  463. std::vector<ArrayRef<Elf_Word>> selectedGroups;
  464. for (size_t i = 0; i != size; ++i) {
  465. if (this->sections[i] == &InputSection::discarded)
  466. continue;
  467. const Elf_Shdr &sec = objSections[i];
  468. // SHF_EXCLUDE'ed sections are discarded by the linker. However,
  469. // if -r is given, we'll let the final link discard such sections.
  470. // This is compatible with GNU.
  471. if ((sec.sh_flags & SHF_EXCLUDE) && !config->relocatable) {
  472. if (sec.sh_type == SHT_LLVM_CALL_GRAPH_PROFILE)
  473. cgProfileSectionIndex = i;
  474. if (sec.sh_type == SHT_LLVM_ADDRSIG) {
  475. // We ignore the address-significance table if we know that the object
  476. // file was created by objcopy or ld -r. This is because these tools
  477. // will reorder the symbols in the symbol table, invalidating the data
  478. // in the address-significance table, which refers to symbols by index.
  479. if (sec.sh_link != 0)
  480. this->addrsigSec = &sec;
  481. else if (config->icf == ICFLevel::Safe)
  482. warn(toString(this) +
  483. ": --icf=safe conservatively ignores "
  484. "SHT_LLVM_ADDRSIG [index " +
  485. Twine(i) +
  486. "] with sh_link=0 "
  487. "(likely created using objcopy or ld -r)");
  488. }
  489. this->sections[i] = &InputSection::discarded;
  490. continue;
  491. }
  492. switch (sec.sh_type) {
  493. case SHT_GROUP: {
  494. // De-duplicate section groups by their signatures.
  495. StringRef signature = getShtGroupSignature(objSections, sec);
  496. this->sections[i] = &InputSection::discarded;
  497. ArrayRef<Elf_Word> entries =
  498. CHECK(obj.template getSectionContentsAsArray<Elf_Word>(sec), this);
  499. if (entries.empty())
  500. fatal(toString(this) + ": empty SHT_GROUP");
  501. Elf_Word flag = entries[0];
  502. if (flag && flag != GRP_COMDAT)
  503. fatal(toString(this) + ": unsupported SHT_GROUP format");
  504. bool keepGroup =
  505. (flag & GRP_COMDAT) == 0 || ignoreComdats ||
  506. symtab->comdatGroups.try_emplace(CachedHashStringRef(signature), this)
  507. .second;
  508. if (keepGroup) {
  509. if (config->relocatable)
  510. this->sections[i] = createInputSection(
  511. i, sec, check(obj.getSectionName(sec, shstrtab)));
  512. selectedGroups.push_back(entries);
  513. continue;
  514. }
  515. // Otherwise, discard group members.
  516. for (uint32_t secIndex : entries.slice(1)) {
  517. if (secIndex >= size)
  518. fatal(toString(this) +
  519. ": invalid section index in group: " + Twine(secIndex));
  520. this->sections[secIndex] = &InputSection::discarded;
  521. }
  522. break;
  523. }
  524. case SHT_SYMTAB_SHNDX:
  525. shndxTable = CHECK(obj.getSHNDXTable(sec, objSections), this);
  526. break;
  527. case SHT_SYMTAB:
  528. case SHT_STRTAB:
  529. case SHT_REL:
  530. case SHT_RELA:
  531. case SHT_NULL:
  532. break;
  533. default:
  534. this->sections[i] =
  535. createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));
  536. }
  537. }
  538. // We have a second loop. It is used to:
  539. // 1) handle SHF_LINK_ORDER sections.
  540. // 2) create SHT_REL[A] sections. In some cases the section header index of a
  541. // relocation section may be smaller than that of the relocated section. In
  542. // such cases, the relocation section would attempt to reference a target
  543. // section that has not yet been created. For simplicity, delay creation of
  544. // relocation sections until now.
  545. for (size_t i = 0; i != size; ++i) {
  546. if (this->sections[i] == &InputSection::discarded)
  547. continue;
  548. const Elf_Shdr &sec = objSections[i];
  549. if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) {
  550. // Find a relocation target section and associate this section with that.
  551. // Target may have been discarded if it is in a different section group
  552. // and the group is discarded, even though it's a violation of the spec.
  553. // We handle that situation gracefully by discarding dangling relocation
  554. // sections.
  555. const uint32_t info = sec.sh_info;
  556. InputSectionBase *s = getRelocTarget(i, sec, info);
  557. if (!s)
  558. continue;
  559. // ELF spec allows mergeable sections with relocations, but they are rare,
  560. // and it is in practice hard to merge such sections by contents, because
  561. // applying relocations at end of linking changes section contents. So, we
  562. // simply handle such sections as non-mergeable ones. Degrading like this
  563. // is acceptable because section merging is optional.
  564. if (auto *ms = dyn_cast<MergeInputSection>(s)) {
  565. s = make<InputSection>(ms->file, ms->flags, ms->type, ms->alignment,
  566. ms->data(), ms->name);
  567. sections[info] = s;
  568. }
  569. if (s->relSecIdx != 0)
  570. error(
  571. toString(s) +
  572. ": multiple relocation sections to one section are not supported");
  573. s->relSecIdx = i;
  574. // Relocation sections are usually removed from the output, so return
  575. // `nullptr` for the normal case. However, if -r or --emit-relocs is
  576. // specified, we need to copy them to the output. (Some post link analysis
  577. // tools specify --emit-relocs to obtain the information.)
  578. if (config->copyRelocs) {
  579. auto *isec = make<InputSection>(
  580. *this, sec, check(obj.getSectionName(sec, shstrtab)));
  581. // If the relocated section is discarded (due to /DISCARD/ or
  582. // --gc-sections), the relocation section should be discarded as well.
  583. s->dependentSections.push_back(isec);
  584. sections[i] = isec;
  585. }
  586. continue;
  587. }
  588. // A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have
  589. // the flag.
  590. if (!sec.sh_link || !(sec.sh_flags & SHF_LINK_ORDER))
  591. continue;
  592. InputSectionBase *linkSec = nullptr;
  593. if (sec.sh_link < size)
  594. linkSec = this->sections[sec.sh_link];
  595. if (!linkSec)
  596. fatal(toString(this) + ": invalid sh_link index: " + Twine(sec.sh_link));
  597. // A SHF_LINK_ORDER section is discarded if its linked-to section is
  598. // discarded.
  599. InputSection *isec = cast<InputSection>(this->sections[i]);
  600. linkSec->dependentSections.push_back(isec);
  601. if (!isa<InputSection>(linkSec))
  602. error("a section " + isec->name +
  603. " with SHF_LINK_ORDER should not refer a non-regular section: " +
  604. toString(linkSec));
  605. }
  606. for (ArrayRef<Elf_Word> entries : selectedGroups)
  607. handleSectionGroup<ELFT>(this->sections, entries);
  608. }
  609. // For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD
  610. // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
  611. // the input objects have been compiled.
  612. static void updateARMVFPArgs(const ARMAttributeParser &attributes,
  613. const InputFile *f) {
  614. Optional<unsigned> attr =
  615. attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);
  616. if (!attr.hasValue())
  617. // If an ABI tag isn't present then it is implicitly given the value of 0
  618. // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
  619. // including some in glibc that don't use FP args (and should have value 3)
  620. // don't have the attribute so we do not consider an implicit value of 0
  621. // as a clash.
  622. return;
  623. unsigned vfpArgs = attr.getValue();
  624. ARMVFPArgKind arg;
  625. switch (vfpArgs) {
  626. case ARMBuildAttrs::BaseAAPCS:
  627. arg = ARMVFPArgKind::Base;
  628. break;
  629. case ARMBuildAttrs::HardFPAAPCS:
  630. arg = ARMVFPArgKind::VFP;
  631. break;
  632. case ARMBuildAttrs::ToolChainFPPCS:
  633. // Tool chain specific convention that conforms to neither AAPCS variant.
  634. arg = ARMVFPArgKind::ToolChain;
  635. break;
  636. case ARMBuildAttrs::CompatibleFPAAPCS:
  637. // Object compatible with all conventions.
  638. return;
  639. default:
  640. error(toString(f) + ": unknown Tag_ABI_VFP_args value: " + Twine(vfpArgs));
  641. return;
  642. }
  643. // Follow ld.bfd and error if there is a mix of calling conventions.
  644. if (config->armVFPArgs != arg && config->armVFPArgs != ARMVFPArgKind::Default)
  645. error(toString(f) + ": incompatible Tag_ABI_VFP_args");
  646. else
  647. config->armVFPArgs = arg;
  648. }
  649. // The ARM support in lld makes some use of instructions that are not available
  650. // on all ARM architectures. Namely:
  651. // - Use of BLX instruction for interworking between ARM and Thumb state.
  652. // - Use of the extended Thumb branch encoding in relocation.
  653. // - Use of the MOVT/MOVW instructions in Thumb Thunks.
  654. // The ARM Attributes section contains information about the architecture chosen
  655. // at compile time. We follow the convention that if at least one input object
  656. // is compiled with an architecture that supports these features then lld is
  657. // permitted to use them.
  658. static void updateSupportedARMFeatures(const ARMAttributeParser &attributes) {
  659. Optional<unsigned> attr =
  660. attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
  661. if (!attr.hasValue())
  662. return;
  663. auto arch = attr.getValue();
  664. switch (arch) {
  665. case ARMBuildAttrs::Pre_v4:
  666. case ARMBuildAttrs::v4:
  667. case ARMBuildAttrs::v4T:
  668. // Architectures prior to v5 do not support BLX instruction
  669. break;
  670. case ARMBuildAttrs::v5T:
  671. case ARMBuildAttrs::v5TE:
  672. case ARMBuildAttrs::v5TEJ:
  673. case ARMBuildAttrs::v6:
  674. case ARMBuildAttrs::v6KZ:
  675. case ARMBuildAttrs::v6K:
  676. config->armHasBlx = true;
  677. // Architectures used in pre-Cortex processors do not support
  678. // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
  679. // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
  680. break;
  681. default:
  682. // All other Architectures have BLX and extended branch encoding
  683. config->armHasBlx = true;
  684. config->armJ1J2BranchEncoding = true;
  685. if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M)
  686. // All Architectures used in Cortex processors with the exception
  687. // of v6-M and v6S-M have the MOVT and MOVW instructions.
  688. config->armHasMovtMovw = true;
  689. break;
  690. }
  691. }
  692. // If a source file is compiled with x86 hardware-assisted call flow control
  693. // enabled, the generated object file contains feature flags indicating that
  694. // fact. This function reads the feature flags and returns it.
  695. //
  696. // Essentially we want to read a single 32-bit value in this function, but this
  697. // function is rather complicated because the value is buried deep inside a
  698. // .note.gnu.property section.
  699. //
  700. // The section consists of one or more NOTE records. Each NOTE record consists
  701. // of zero or more type-length-value fields. We want to find a field of a
  702. // certain type. It seems a bit too much to just store a 32-bit value, perhaps
  703. // the ABI is unnecessarily complicated.
  704. template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) {
  705. using Elf_Nhdr = typename ELFT::Nhdr;
  706. using Elf_Note = typename ELFT::Note;
  707. uint32_t featuresSet = 0;
  708. ArrayRef<uint8_t> data = sec.data();
  709. auto reportFatal = [&](const uint8_t *place, const char *msg) {
  710. fatal(toString(sec.file) + ":(" + sec.name + "+0x" +
  711. Twine::utohexstr(place - sec.data().data()) + "): " + msg);
  712. };
  713. while (!data.empty()) {
  714. // Read one NOTE record.
  715. auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data());
  716. if (data.size() < sizeof(Elf_Nhdr) || data.size() < nhdr->getSize())
  717. reportFatal(data.data(), "data is too short");
  718. Elf_Note note(*nhdr);
  719. if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") {
  720. data = data.slice(nhdr->getSize());
  721. continue;
  722. }
  723. uint32_t featureAndType = config->emachine == EM_AARCH64
  724. ? GNU_PROPERTY_AARCH64_FEATURE_1_AND
  725. : GNU_PROPERTY_X86_FEATURE_1_AND;
  726. // Read a body of a NOTE record, which consists of type-length-value fields.
  727. ArrayRef<uint8_t> desc = note.getDesc();
  728. while (!desc.empty()) {
  729. const uint8_t *place = desc.data();
  730. if (desc.size() < 8)
  731. reportFatal(place, "program property is too short");
  732. uint32_t type = read32<ELFT::TargetEndianness>(desc.data());
  733. uint32_t size = read32<ELFT::TargetEndianness>(desc.data() + 4);
  734. desc = desc.slice(8);
  735. if (desc.size() < size)
  736. reportFatal(place, "program property is too short");
  737. if (type == featureAndType) {
  738. // We found a FEATURE_1_AND field. There may be more than one of these
  739. // in a .note.gnu.property section, for a relocatable object we
  740. // accumulate the bits set.
  741. if (size < 4)
  742. reportFatal(place, "FEATURE_1_AND entry is too short");
  743. featuresSet |= read32<ELFT::TargetEndianness>(desc.data());
  744. }
  745. // Padding is present in the note descriptor, if necessary.
  746. desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));
  747. }
  748. // Go to next NOTE record to look for more FEATURE_1_AND descriptions.
  749. data = data.slice(nhdr->getSize());
  750. }
  751. return featuresSet;
  752. }
  753. template <class ELFT>
  754. InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx,
  755. const Elf_Shdr &sec,
  756. uint32_t info) {
  757. if (info < this->sections.size()) {
  758. InputSectionBase *target = this->sections[info];
  759. // Strictly speaking, a relocation section must be included in the
  760. // group of the section it relocates. However, LLVM 3.3 and earlier
  761. // would fail to do so, so we gracefully handle that case.
  762. if (target == &InputSection::discarded)
  763. return nullptr;
  764. if (target != nullptr)
  765. return target;
  766. }
  767. error(toString(this) + Twine(": relocation section (index ") + Twine(idx) +
  768. ") has invalid sh_info (" + Twine(info) + ")");
  769. return nullptr;
  770. }
  771. template <class ELFT>
  772. InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,
  773. const Elf_Shdr &sec,
  774. StringRef name) {
  775. if (sec.sh_type == SHT_ARM_ATTRIBUTES && config->emachine == EM_ARM) {
  776. ARMAttributeParser attributes;
  777. ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
  778. if (Error e = attributes.parse(contents, config->ekind == ELF32LEKind
  779. ? support::little
  780. : support::big)) {
  781. auto *isec = make<InputSection>(*this, sec, name);
  782. warn(toString(isec) + ": " + llvm::toString(std::move(e)));
  783. } else {
  784. updateSupportedARMFeatures(attributes);
  785. updateARMVFPArgs(attributes, this);
  786. // FIXME: Retain the first attribute section we see. The eglibc ARM
  787. // dynamic loaders require the presence of an attribute section for dlopen
  788. // to work. In a full implementation we would merge all attribute
  789. // sections.
  790. if (in.attributes == nullptr) {
  791. in.attributes = std::make_unique<InputSection>(*this, sec, name);
  792. return in.attributes.get();
  793. }
  794. return &InputSection::discarded;
  795. }
  796. }
  797. if (sec.sh_type == SHT_RISCV_ATTRIBUTES && config->emachine == EM_RISCV) {
  798. RISCVAttributeParser attributes;
  799. ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec));
  800. if (Error e = attributes.parse(contents, support::little)) {
  801. auto *isec = make<InputSection>(*this, sec, name);
  802. warn(toString(isec) + ": " + llvm::toString(std::move(e)));
  803. } else {
  804. // FIXME: Validate arch tag contains C if and only if EF_RISCV_RVC is
  805. // present.
  806. // FIXME: Retain the first attribute section we see. Tools such as
  807. // llvm-objdump make use of the attribute section to determine which
  808. // standard extensions to enable. In a full implementation we would merge
  809. // all attribute sections.
  810. if (in.attributes == nullptr) {
  811. in.attributes = std::make_unique<InputSection>(*this, sec, name);
  812. return in.attributes.get();
  813. }
  814. return &InputSection::discarded;
  815. }
  816. }
  817. if (sec.sh_type == SHT_LLVM_DEPENDENT_LIBRARIES && !config->relocatable) {
  818. ArrayRef<char> data =
  819. CHECK(this->getObj().template getSectionContentsAsArray<char>(sec), this);
  820. if (!data.empty() && data.back() != '\0') {
  821. error(toString(this) +
  822. ": corrupted dependent libraries section (unterminated string): " +
  823. name);
  824. return &InputSection::discarded;
  825. }
  826. for (const char *d = data.begin(), *e = data.end(); d < e;) {
  827. StringRef s(d);
  828. addDependentLibrary(s, this);
  829. d += s.size() + 1;
  830. }
  831. return &InputSection::discarded;
  832. }
  833. if (name.startswith(".n")) {
  834. // The GNU linker uses .note.GNU-stack section as a marker indicating
  835. // that the code in the object file does not expect that the stack is
  836. // executable (in terms of NX bit). If all input files have the marker,
  837. // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
  838. // make the stack non-executable. Most object files have this section as
  839. // of 2017.
  840. //
  841. // But making the stack non-executable is a norm today for security
  842. // reasons. Failure to do so may result in a serious security issue.
  843. // Therefore, we make LLD always add PT_GNU_STACK unless it is
  844. // explicitly told to do otherwise (by -z execstack). Because the stack
  845. // executable-ness is controlled solely by command line options,
  846. // .note.GNU-stack sections are simply ignored.
  847. if (name == ".note.GNU-stack")
  848. return &InputSection::discarded;
  849. // Object files that use processor features such as Intel Control-Flow
  850. // Enforcement (CET) or AArch64 Branch Target Identification BTI, use a
  851. // .note.gnu.property section containing a bitfield of feature bits like the
  852. // GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag.
  853. //
  854. // Since we merge bitmaps from multiple object files to create a new
  855. // .note.gnu.property containing a single AND'ed bitmap, we discard an input
  856. // file's .note.gnu.property section.
  857. if (name == ".note.gnu.property") {
  858. this->andFeatures = readAndFeatures<ELFT>(InputSection(*this, sec, name));
  859. return &InputSection::discarded;
  860. }
  861. // Split stacks is a feature to support a discontiguous stack,
  862. // commonly used in the programming language Go. For the details,
  863. // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
  864. // for split stack will include a .note.GNU-split-stack section.
  865. if (name == ".note.GNU-split-stack") {
  866. if (config->relocatable) {
  867. error(
  868. "cannot mix split-stack and non-split-stack in a relocatable link");
  869. return &InputSection::discarded;
  870. }
  871. this->splitStack = true;
  872. return &InputSection::discarded;
  873. }
  874. // An object file cmpiled for split stack, but where some of the
  875. // functions were compiled with the no_split_stack_attribute will
  876. // include a .note.GNU-no-split-stack section.
  877. if (name == ".note.GNU-no-split-stack") {
  878. this->someNoSplitStack = true;
  879. return &InputSection::discarded;
  880. }
  881. // Strip existing .note.gnu.build-id sections so that the output won't have
  882. // more than one build-id. This is not usually a problem because input
  883. // object files normally don't have .build-id sections, but you can create
  884. // such files by "ld.{bfd,gold,lld} -r --build-id", and we want to guard
  885. // against it.
  886. if (name == ".note.gnu.build-id")
  887. return &InputSection::discarded;
  888. }
  889. // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
  890. // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
  891. // sections. Drop those sections to avoid duplicate symbol errors.
  892. // FIXME: This is glibc PR20543, we should remove this hack once that has been
  893. // fixed for a while.
  894. if (name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" ||
  895. name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx")
  896. return &InputSection::discarded;
  897. // The linker merges EH (exception handling) frames and creates a
  898. // .eh_frame_hdr section for runtime. So we handle them with a special
  899. // class. For relocatable outputs, they are just passed through.
  900. if (name == ".eh_frame" && !config->relocatable)
  901. return make<EhInputSection>(*this, sec, name);
  902. if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name))
  903. return make<MergeInputSection>(*this, sec, name);
  904. return make<InputSection>(*this, sec, name);
  905. }
  906. // Initialize this->Symbols. this->Symbols is a parallel array as
  907. // its corresponding ELF symbol table.
  908. template <class ELFT>
  909. void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
  910. ArrayRef<InputSectionBase *> sections(this->sections);
  911. SymbolTable &symtab = *elf::symtab;
  912. ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
  913. symbols.resize(eSyms.size());
  914. SymbolUnion *locals =
  915. firstGlobal == 0
  916. ? nullptr
  917. : getSpecificAllocSingleton<SymbolUnion>().Allocate(firstGlobal);
  918. for (size_t i = 0, end = firstGlobal; i != end; ++i) {
  919. const Elf_Sym &eSym = eSyms[i];
  920. uint32_t secIdx = eSym.st_shndx;
  921. if (LLVM_UNLIKELY(secIdx == SHN_XINDEX))
  922. secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));
  923. else if (secIdx >= SHN_LORESERVE)
  924. secIdx = 0;
  925. if (LLVM_UNLIKELY(secIdx >= sections.size()))
  926. fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
  927. if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL))
  928. error(toString(this) + ": non-local symbol (" + Twine(i) +
  929. ") found at index < .symtab's sh_info (" + Twine(end) + ")");
  930. InputSectionBase *sec = sections[secIdx];
  931. uint8_t type = eSym.getType();
  932. if (type == STT_FILE)
  933. sourceFile = CHECK(eSym.getName(stringTable), this);
  934. if (LLVM_UNLIKELY(stringTable.size() <= eSym.st_name))
  935. fatal(toString(this) + ": invalid symbol name offset");
  936. StringRef name(stringTable.data() + eSym.st_name);
  937. symbols[i] = reinterpret_cast<Symbol *>(locals + i);
  938. if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded)
  939. new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type,
  940. /*discardedSecIdx=*/secIdx);
  941. else
  942. new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type,
  943. eSym.st_value, eSym.st_size, sec);
  944. }
  945. // Some entries have been filled by LazyObjFile.
  946. for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)
  947. if (!symbols[i])
  948. symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this));
  949. // Perform symbol resolution on non-local symbols.
  950. SmallVector<unsigned, 32> undefineds;
  951. for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {
  952. const Elf_Sym &eSym = eSyms[i];
  953. uint8_t binding = eSym.getBinding();
  954. if (LLVM_UNLIKELY(binding == STB_LOCAL)) {
  955. errorOrWarn(toString(this) + ": STB_LOCAL symbol (" + Twine(i) +
  956. ") found at index >= .symtab's sh_info (" +
  957. Twine(firstGlobal) + ")");
  958. continue;
  959. }
  960. uint32_t secIdx = eSym.st_shndx;
  961. if (LLVM_UNLIKELY(secIdx == SHN_XINDEX))
  962. secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));
  963. else if (secIdx >= SHN_LORESERVE)
  964. secIdx = 0;
  965. if (LLVM_UNLIKELY(secIdx >= sections.size()))
  966. fatal(toString(this) + ": invalid section index: " + Twine(secIdx));
  967. InputSectionBase *sec = sections[secIdx];
  968. uint8_t stOther = eSym.st_other;
  969. uint8_t type = eSym.getType();
  970. uint64_t value = eSym.st_value;
  971. uint64_t size = eSym.st_size;
  972. if (eSym.st_shndx == SHN_UNDEF) {
  973. undefineds.push_back(i);
  974. continue;
  975. }
  976. Symbol *sym = symbols[i];
  977. const StringRef name = sym->getName();
  978. if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) {
  979. if (value == 0 || value >= UINT32_MAX)
  980. fatal(toString(this) + ": common symbol '" + name +
  981. "' has invalid alignment: " + Twine(value));
  982. hasCommonSyms = true;
  983. sym->resolve(
  984. CommonSymbol{this, name, binding, stOther, type, value, size});
  985. continue;
  986. }
  987. // If a defined symbol is in a discarded section, handle it as if it
  988. // were an undefined symbol. Such symbol doesn't comply with the
  989. // standard, but in practice, a .eh_frame often directly refer
  990. // COMDAT member sections, and if a comdat group is discarded, some
  991. // defined symbol in a .eh_frame becomes dangling symbols.
  992. if (sec == &InputSection::discarded) {
  993. Undefined und{this, name, binding, stOther, type, secIdx};
  994. // !ArchiveFile::parsed or !LazyObjFile::lazy means that the file
  995. // containing this object has not finished processing, i.e. this symbol is
  996. // a result of a lazy symbol extract. We should demote the lazy symbol to
  997. // an Undefined so that any relocations outside of the group to it will
  998. // trigger a discarded section error.
  999. if ((sym->symbolKind == Symbol::LazyArchiveKind &&
  1000. !cast<ArchiveFile>(sym->file)->parsed) ||
  1001. (sym->symbolKind == Symbol::LazyObjectKind && !sym->file->lazy)) {
  1002. sym->replace(und);
  1003. // Prevent LTO from internalizing the symbol in case there is a
  1004. // reference to this symbol from this file.
  1005. sym->isUsedInRegularObj = true;
  1006. } else
  1007. sym->resolve(und);
  1008. continue;
  1009. }
  1010. // Handle global defined symbols.
  1011. if (binding == STB_GLOBAL || binding == STB_WEAK ||
  1012. binding == STB_GNU_UNIQUE) {
  1013. sym->resolve(
  1014. Defined{this, name, binding, stOther, type, value, size, sec});
  1015. continue;
  1016. }
  1017. fatal(toString(this) + ": unexpected binding: " + Twine((int)binding));
  1018. }
  1019. // Undefined symbols (excluding those defined relative to non-prevailing
  1020. // sections) can trigger recursive extract. Process defined symbols first so
  1021. // that the relative order between a defined symbol and an undefined symbol
  1022. // does not change the symbol resolution behavior. In addition, a set of
  1023. // interconnected symbols will all be resolved to the same file, instead of
  1024. // being resolved to different files.
  1025. for (unsigned i : undefineds) {
  1026. const Elf_Sym &eSym = eSyms[i];
  1027. Symbol *sym = symbols[i];
  1028. sym->resolve(Undefined{this, sym->getName(), eSym.getBinding(),
  1029. eSym.st_other, eSym.getType()});
  1030. sym->referenced = true;
  1031. }
  1032. }
  1033. ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&file)
  1034. : InputFile(ArchiveKind, file->getMemoryBufferRef()),
  1035. file(std::move(file)) {}
  1036. void ArchiveFile::parse() {
  1037. SymbolTable &symtab = *elf::symtab;
  1038. for (const Archive::Symbol &sym : file->symbols())
  1039. symtab.addSymbol(LazyArchive{*this, sym});
  1040. // Inform a future invocation of ObjFile<ELFT>::initializeSymbols() that this
  1041. // archive has been processed.
  1042. parsed = true;
  1043. }
  1044. // Returns a buffer pointing to a member file containing a given symbol.
  1045. void ArchiveFile::extract(const Archive::Symbol &sym) {
  1046. Archive::Child c =
  1047. CHECK(sym.getMember(), toString(this) +
  1048. ": could not get the member for symbol " +
  1049. toELFString(sym));
  1050. if (!seen.insert(c.getChildOffset()).second)
  1051. return;
  1052. MemoryBufferRef mb =
  1053. CHECK(c.getMemoryBufferRef(),
  1054. toString(this) +
  1055. ": could not get the buffer for the member defining symbol " +
  1056. toELFString(sym));
  1057. if (tar && c.getParent()->isThin())
  1058. tar->append(relativeToRoot(CHECK(c.getFullName(), this)), mb.getBuffer());
  1059. InputFile *file = createObjectFile(mb, getName(), c.getChildOffset());
  1060. file->groupId = groupId;
  1061. parseFile(file);
  1062. }
  1063. // The handling of tentative definitions (COMMON symbols) in archives is murky.
  1064. // A tentative definition will be promoted to a global definition if there are
  1065. // no non-tentative definitions to dominate it. When we hold a tentative
  1066. // definition to a symbol and are inspecting archive members for inclusion
  1067. // there are 2 ways we can proceed:
  1068. //
  1069. // 1) Consider the tentative definition a 'real' definition (ie promotion from
  1070. // tentative to real definition has already happened) and not inspect
  1071. // archive members for Global/Weak definitions to replace the tentative
  1072. // definition. An archive member would only be included if it satisfies some
  1073. // other undefined symbol. This is the behavior Gold uses.
  1074. //
  1075. // 2) Consider the tentative definition as still undefined (ie the promotion to
  1076. // a real definition happens only after all symbol resolution is done).
  1077. // The linker searches archive members for STB_GLOBAL definitions to
  1078. // replace the tentative definition with. This is the behavior used by
  1079. // GNU ld.
  1080. //
  1081. // The second behavior is inherited from SysVR4, which based it on the FORTRAN
  1082. // COMMON BLOCK model. This behavior is needed for proper initialization in old
  1083. // (pre F90) FORTRAN code that is packaged into an archive.
  1084. //
  1085. // The following functions search archive members for definitions to replace
  1086. // tentative definitions (implementing behavior 2).
  1087. static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName,
  1088. StringRef archiveName) {
  1089. IRSymtabFile symtabFile = check(readIRSymtab(mb));
  1090. for (const irsymtab::Reader::SymbolRef &sym :
  1091. symtabFile.TheReader.symbols()) {
  1092. if (sym.isGlobal() && sym.getName() == symName)
  1093. return !sym.isUndefined() && !sym.isWeak() && !sym.isCommon();
  1094. }
  1095. return false;
  1096. }
  1097. template <class ELFT>
  1098. static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
  1099. StringRef archiveName) {
  1100. ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(mb, archiveName);
  1101. StringRef stringtable = obj->getStringTable();
  1102. for (auto sym : obj->template getGlobalELFSyms<ELFT>()) {
  1103. Expected<StringRef> name = sym.getName(stringtable);
  1104. if (name && name.get() == symName)
  1105. return sym.isDefined() && sym.getBinding() == STB_GLOBAL &&
  1106. !sym.isCommon();
  1107. }
  1108. return false;
  1109. }
  1110. static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName,
  1111. StringRef archiveName) {
  1112. switch (getELFKind(mb, archiveName)) {
  1113. case ELF32LEKind:
  1114. return isNonCommonDef<ELF32LE>(mb, symName, archiveName);
  1115. case ELF32BEKind:
  1116. return isNonCommonDef<ELF32BE>(mb, symName, archiveName);
  1117. case ELF64LEKind:
  1118. return isNonCommonDef<ELF64LE>(mb, symName, archiveName);
  1119. case ELF64BEKind:
  1120. return isNonCommonDef<ELF64BE>(mb, symName, archiveName);
  1121. default:
  1122. llvm_unreachable("getELFKind");
  1123. }
  1124. }
  1125. bool ArchiveFile::shouldExtractForCommon(const Archive::Symbol &sym) {
  1126. Archive::Child c =
  1127. CHECK(sym.getMember(), toString(this) +
  1128. ": could not get the member for symbol " +
  1129. toELFString(sym));
  1130. MemoryBufferRef mb =
  1131. CHECK(c.getMemoryBufferRef(),
  1132. toString(this) +
  1133. ": could not get the buffer for the member defining symbol " +
  1134. toELFString(sym));
  1135. if (isBitcode(mb))
  1136. return isBitcodeNonCommonDef(mb, sym.getName(), getName());
  1137. return isNonCommonDef(mb, sym.getName(), getName());
  1138. }
  1139. size_t ArchiveFile::getMemberCount() const {
  1140. size_t count = 0;
  1141. Error err = Error::success();
  1142. for (const Archive::Child &c : file->children(err)) {
  1143. (void)c;
  1144. ++count;
  1145. }
  1146. // This function is used by --print-archive-stats=, where an error does not
  1147. // really matter.
  1148. consumeError(std::move(err));
  1149. return count;
  1150. }
  1151. unsigned SharedFile::vernauxNum;
  1152. // Parse the version definitions in the object file if present, and return a
  1153. // vector whose nth element contains a pointer to the Elf_Verdef for version
  1154. // identifier n. Version identifiers that are not definitions map to nullptr.
  1155. template <typename ELFT>
  1156. static SmallVector<const void *, 0>
  1157. parseVerdefs(const uint8_t *base, const typename ELFT::Shdr *sec) {
  1158. if (!sec)
  1159. return {};
  1160. // Build the Verdefs array by following the chain of Elf_Verdef objects
  1161. // from the start of the .gnu.version_d section.
  1162. SmallVector<const void *, 0> verd