PageRenderTime 71ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/llvm/tools/lld/ELF/InputFiles.cpp

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