InputFiles.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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 "Chunks.h"
  10. #include "Config.h"
  11. #include "DebugTypes.h"
  12. #include "Driver.h"
  13. #include "SymbolTable.h"
  14. #include "Symbols.h"
  15. #include "lld/Common/ErrorHandler.h"
  16. #include "lld/Common/Memory.h"
  17. #include "llvm-c/lto.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/Triple.h"
  20. #include "llvm/ADT/Twine.h"
  21. #include "llvm/BinaryFormat/COFF.h"
  22. #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
  23. #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
  24. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  25. #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
  26. #include "llvm/Object/Binary.h"
  27. #include "llvm/Object/COFF.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/Endian.h"
  30. #include "llvm/Support/Error.h"
  31. #include "llvm/Support/ErrorOr.h"
  32. #include "llvm/Support/FileSystem.h"
  33. #include "llvm/Support/Path.h"
  34. #include "llvm/Target/TargetOptions.h"
  35. #include <cstring>
  36. #include <system_error>
  37. #include <utility>
  38. using namespace llvm;
  39. using namespace llvm::COFF;
  40. using namespace llvm::codeview;
  41. using namespace llvm::object;
  42. using namespace llvm::support::endian;
  43. using llvm::Triple;
  44. using llvm::support::ulittle32_t;
  45. namespace lld {
  46. // Returns the last element of a path, which is supposed to be a filename.
  47. static StringRef getBasename(StringRef path) {
  48. return sys::path::filename(path, sys::path::Style::windows);
  49. }
  50. // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
  51. std::string toString(const coff::InputFile *file) {
  52. if (!file)
  53. return "<internal>";
  54. if (file->parentName.empty() || file->kind() == coff::InputFile::ImportKind)
  55. return file->getName();
  56. return (getBasename(file->parentName) + "(" + getBasename(file->getName()) +
  57. ")")
  58. .str();
  59. }
  60. namespace coff {
  61. std::vector<ObjFile *> ObjFile::instances;
  62. std::vector<ImportFile *> ImportFile::instances;
  63. std::vector<BitcodeFile *> BitcodeFile::instances;
  64. /// Checks that Source is compatible with being a weak alias to Target.
  65. /// If Source is Undefined and has no weak alias set, makes it a weak
  66. /// alias to Target.
  67. static void checkAndSetWeakAlias(SymbolTable *symtab, InputFile *f,
  68. Symbol *source, Symbol *target) {
  69. if (auto *u = dyn_cast<Undefined>(source)) {
  70. if (u->weakAlias && u->weakAlias != target) {
  71. // Weak aliases as produced by GCC are named in the form
  72. // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
  73. // of another symbol emitted near the weak symbol.
  74. // Just use the definition from the first object file that defined
  75. // this weak symbol.
  76. if (config->mingw)
  77. return;
  78. symtab->reportDuplicate(source, f);
  79. }
  80. u->weakAlias = target;
  81. }
  82. }
  83. static bool ignoredSymbolName(StringRef name) {
  84. return name == "@feat.00" || name == "@comp.id";
  85. }
  86. ArchiveFile::ArchiveFile(MemoryBufferRef m) : InputFile(ArchiveKind, m) {}
  87. void ArchiveFile::parse() {
  88. // Parse a MemoryBufferRef as an archive file.
  89. file = CHECK(Archive::create(mb), this);
  90. // Read the symbol table to construct Lazy objects.
  91. for (const Archive::Symbol &sym : file->symbols())
  92. symtab->addLazyArchive(this, sym);
  93. }
  94. // Returns a buffer pointing to a member file containing a given symbol.
  95. void ArchiveFile::addMember(const Archive::Symbol &sym) {
  96. const Archive::Child &c =
  97. CHECK(sym.getMember(),
  98. "could not get the member for symbol " + toCOFFString(sym));
  99. // Return an empty buffer if we have already returned the same buffer.
  100. if (!seen.insert(c.getChildOffset()).second)
  101. return;
  102. driver->enqueueArchiveMember(c, sym, getName());
  103. }
  104. std::vector<MemoryBufferRef> getArchiveMembers(Archive *file) {
  105. std::vector<MemoryBufferRef> v;
  106. Error err = Error::success();
  107. for (const ErrorOr<Archive::Child> &cOrErr : file->children(err)) {
  108. Archive::Child c =
  109. CHECK(cOrErr,
  110. file->getFileName() + ": could not get the child of the archive");
  111. MemoryBufferRef mbref =
  112. CHECK(c.getMemoryBufferRef(),
  113. file->getFileName() +
  114. ": could not get the buffer for a child of the archive");
  115. v.push_back(mbref);
  116. }
  117. if (err)
  118. fatal(file->getFileName() +
  119. ": Archive::children failed: " + toString(std::move(err)));
  120. return v;
  121. }
  122. void LazyObjFile::fetch() {
  123. if (mb.getBuffer().empty())
  124. return;
  125. InputFile *file;
  126. if (isBitcode(mb))
  127. file = make<BitcodeFile>(mb, "", 0, std::move(symbols));
  128. else
  129. file = make<ObjFile>(mb, std::move(symbols));
  130. mb = {};
  131. symtab->addFile(file);
  132. }
  133. void LazyObjFile::parse() {
  134. if (isBitcode(this->mb)) {
  135. // Bitcode file.
  136. std::unique_ptr<lto::InputFile> obj =
  137. CHECK(lto::InputFile::create(this->mb), this);
  138. for (const lto::InputFile::Symbol &sym : obj->symbols()) {
  139. if (!sym.isUndefined())
  140. symtab->addLazyObject(this, sym.getName());
  141. }
  142. return;
  143. }
  144. // Native object file.
  145. std::unique_ptr<Binary> coffObjPtr = CHECK(createBinary(mb), this);
  146. COFFObjectFile *coffObj = cast<COFFObjectFile>(coffObjPtr.get());
  147. uint32_t numSymbols = coffObj->getNumberOfSymbols();
  148. for (uint32_t i = 0; i < numSymbols; ++i) {
  149. COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
  150. if (coffSym.isUndefined() || !coffSym.isExternal() ||
  151. coffSym.isWeakExternal())
  152. continue;
  153. StringRef name;
  154. coffObj->getSymbolName(coffSym, name);
  155. if (coffSym.isAbsolute() && ignoredSymbolName(name))
  156. continue;
  157. symtab->addLazyObject(this, name);
  158. i += coffSym.getNumberOfAuxSymbols();
  159. }
  160. }
  161. void ObjFile::parse() {
  162. // Parse a memory buffer as a COFF file.
  163. std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
  164. if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
  165. bin.release();
  166. coffObj.reset(obj);
  167. } else {
  168. fatal(toString(this) + " is not a COFF file");
  169. }
  170. // Read section and symbol tables.
  171. initializeChunks();
  172. initializeSymbols();
  173. initializeFlags();
  174. initializeDependencies();
  175. }
  176. const coff_section* ObjFile::getSection(uint32_t i) {
  177. const coff_section *sec;
  178. if (auto ec = coffObj->getSection(i, sec))
  179. fatal("getSection failed: #" + Twine(i) + ": " + ec.message());
  180. return sec;
  181. }
  182. // We set SectionChunk pointers in the SparseChunks vector to this value
  183. // temporarily to mark comdat sections as having an unknown resolution. As we
  184. // walk the object file's symbol table, once we visit either a leader symbol or
  185. // an associative section definition together with the parent comdat's leader,
  186. // we set the pointer to either nullptr (to mark the section as discarded) or a
  187. // valid SectionChunk for that section.
  188. static SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
  189. void ObjFile::initializeChunks() {
  190. uint32_t numSections = coffObj->getNumberOfSections();
  191. chunks.reserve(numSections);
  192. sparseChunks.resize(numSections + 1);
  193. for (uint32_t i = 1; i < numSections + 1; ++i) {
  194. const coff_section *sec = getSection(i);
  195. if (sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
  196. sparseChunks[i] = pendingComdat;
  197. else
  198. sparseChunks[i] = readSection(i, nullptr, "");
  199. }
  200. }
  201. SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
  202. const coff_aux_section_definition *def,
  203. StringRef leaderName) {
  204. const coff_section *sec = getSection(sectionNumber);
  205. StringRef name;
  206. if (Expected<StringRef> e = coffObj->getSectionName(sec))
  207. name = *e;
  208. else
  209. fatal("getSectionName failed: #" + Twine(sectionNumber) + ": " +
  210. toString(e.takeError()));
  211. if (name == ".drectve") {
  212. ArrayRef<uint8_t> data;
  213. cantFail(coffObj->getSectionContents(sec, data));
  214. directives = StringRef((const char *)data.data(), data.size());
  215. return nullptr;
  216. }
  217. if (name == ".llvm_addrsig") {
  218. addrsigSec = sec;
  219. return nullptr;
  220. }
  221. // Object files may have DWARF debug info or MS CodeView debug info
  222. // (or both).
  223. //
  224. // DWARF sections don't need any special handling from the perspective
  225. // of the linker; they are just a data section containing relocations.
  226. // We can just link them to complete debug info.
  227. //
  228. // CodeView needs linker support. We need to interpret debug info,
  229. // and then write it to a separate .pdb file.
  230. // Ignore DWARF debug info unless /debug is given.
  231. if (!config->debug && name.startswith(".debug_"))
  232. return nullptr;
  233. if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
  234. return nullptr;
  235. auto *c = make<SectionChunk>(this, sec);
  236. if (def)
  237. c->checksum = def->CheckSum;
  238. // CodeView sections are stored to a different vector because they are not
  239. // linked in the regular manner.
  240. if (c->isCodeView())
  241. debugChunks.push_back(c);
  242. else if (name == ".gfids$y")
  243. guardFidChunks.push_back(c);
  244. else if (name == ".gljmp$y")
  245. guardLJmpChunks.push_back(c);
  246. else if (name == ".sxdata")
  247. sXDataChunks.push_back(c);
  248. else if (config->tailMerge && sec->NumberOfRelocations == 0 &&
  249. name == ".rdata" && leaderName.startswith("??_C@"))
  250. // COFF sections that look like string literal sections (i.e. no
  251. // relocations, in .rdata, leader symbol name matches the MSVC name mangling
  252. // for string literals) are subject to string tail merging.
  253. MergeChunk::addSection(c);
  254. else if (name == ".rsrc" || name.startswith(".rsrc$"))
  255. resourceChunks.push_back(c);
  256. else
  257. chunks.push_back(c);
  258. return c;
  259. }
  260. void ObjFile::includeResourceChunks() {
  261. chunks.insert(chunks.end(), resourceChunks.begin(), resourceChunks.end());
  262. }
  263. void ObjFile::readAssociativeDefinition(
  264. COFFSymbolRef sym, const coff_aux_section_definition *def) {
  265. readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
  266. }
  267. void ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
  268. const coff_aux_section_definition *def,
  269. uint32_t parentIndex) {
  270. SectionChunk *parent = sparseChunks[parentIndex];
  271. int32_t sectionNumber = sym.getSectionNumber();
  272. auto diag = [&]() {
  273. StringRef name, parentName;
  274. coffObj->getSymbolName(sym, name);
  275. const coff_section *parentSec = getSection(parentIndex);
  276. if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
  277. parentName = *e;
  278. error(toString(this) + ": associative comdat " + name + " (sec " +
  279. Twine(sectionNumber) + ") has invalid reference to section " +
  280. parentName + " (sec " + Twine(parentIndex) + ")");
  281. };
  282. if (parent == pendingComdat) {
  283. // This can happen if an associative comdat refers to another associative
  284. // comdat that appears after it (invalid per COFF spec) or to a section
  285. // without any symbols.
  286. diag();
  287. return;
  288. }
  289. // Check whether the parent is prevailing. If it is, so are we, and we read
  290. // the section; otherwise mark it as discarded.
  291. if (parent) {
  292. SectionChunk *c = readSection(sectionNumber, def, "");
  293. sparseChunks[sectionNumber] = c;
  294. if (c) {
  295. c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
  296. parent->addAssociative(c);
  297. }
  298. } else {
  299. sparseChunks[sectionNumber] = nullptr;
  300. }
  301. }
  302. void ObjFile::recordPrevailingSymbolForMingw(
  303. COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
  304. // For comdat symbols in executable sections, where this is the copy
  305. // of the section chunk we actually include instead of discarding it,
  306. // add the symbol to a map to allow using it for implicitly
  307. // associating .[px]data$<func> sections to it.
  308. int32_t sectionNumber = sym.getSectionNumber();
  309. SectionChunk *sc = sparseChunks[sectionNumber];
  310. if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
  311. StringRef name;
  312. coffObj->getSymbolName(sym, name);
  313. if (getMachineType() == I386)
  314. name.consume_front("_");
  315. prevailingSectionMap[name] = sectionNumber;
  316. }
  317. }
  318. void ObjFile::maybeAssociateSEHForMingw(
  319. COFFSymbolRef sym, const coff_aux_section_definition *def,
  320. const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
  321. StringRef name;
  322. coffObj->getSymbolName(sym, name);
  323. if (name.consume_front(".pdata$") || name.consume_front(".xdata$") ||
  324. name.consume_front(".eh_frame$")) {
  325. // For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
  326. // associative to the symbol <func>.
  327. auto parentSym = prevailingSectionMap.find(name);
  328. if (parentSym != prevailingSectionMap.end())
  329. readAssociativeDefinition(sym, def, parentSym->second);
  330. }
  331. }
  332. Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
  333. SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
  334. if (sym.isExternal()) {
  335. StringRef name;
  336. coffObj->getSymbolName(sym, name);
  337. if (sc)
  338. return symtab->addRegular(this, name, sym.getGeneric(), sc,
  339. sym.getValue());
  340. // For MinGW symbols named .weak.* that point to a discarded section,
  341. // don't create an Undefined symbol. If nothing ever refers to the symbol,
  342. // everything should be fine. If something actually refers to the symbol
  343. // (e.g. the undefined weak alias), linking will fail due to undefined
  344. // references at the end.
  345. if (config->mingw && name.startswith(".weak."))
  346. return nullptr;
  347. return symtab->addUndefined(name, this, false);
  348. }
  349. if (sc)
  350. return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
  351. /*IsExternal*/ false, sym.getGeneric(), sc);
  352. return nullptr;
  353. }
  354. void ObjFile::initializeSymbols() {
  355. uint32_t numSymbols = coffObj->getNumberOfSymbols();
  356. symbols.resize(numSymbols);
  357. SmallVector<std::pair<Symbol *, uint32_t>, 8> weakAliases;
  358. std::vector<uint32_t> pendingIndexes;
  359. pendingIndexes.reserve(numSymbols);
  360. DenseMap<StringRef, uint32_t> prevailingSectionMap;
  361. std::vector<const coff_aux_section_definition *> comdatDefs(
  362. coffObj->getNumberOfSections() + 1);
  363. for (uint32_t i = 0; i < numSymbols; ++i) {
  364. COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
  365. bool prevailingComdat;
  366. if (coffSym.isUndefined()) {
  367. symbols[i] = createUndefined(coffSym);
  368. } else if (coffSym.isWeakExternal()) {
  369. symbols[i] = createUndefined(coffSym);
  370. uint32_t tagIndex = coffSym.getAux<coff_aux_weak_external>()->TagIndex;
  371. weakAliases.emplace_back(symbols[i], tagIndex);
  372. } else if (Optional<Symbol *> optSym =
  373. createDefined(coffSym, comdatDefs, prevailingComdat)) {
  374. symbols[i] = *optSym;
  375. if (config->mingw && prevailingComdat)
  376. recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
  377. } else {
  378. // createDefined() returns None if a symbol belongs to a section that
  379. // was pending at the point when the symbol was read. This can happen in
  380. // two cases:
  381. // 1) section definition symbol for a comdat leader;
  382. // 2) symbol belongs to a comdat section associated with another section.
  383. // In both of these cases, we can expect the section to be resolved by
  384. // the time we finish visiting the remaining symbols in the symbol
  385. // table. So we postpone the handling of this symbol until that time.
  386. pendingIndexes.push_back(i);
  387. }
  388. i += coffSym.getNumberOfAuxSymbols();
  389. }
  390. for (uint32_t i : pendingIndexes) {
  391. COFFSymbolRef sym = check(coffObj->getSymbol(i));
  392. if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
  393. if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
  394. readAssociativeDefinition(sym, def);
  395. else if (config->mingw)
  396. maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
  397. }
  398. if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
  399. StringRef name;
  400. coffObj->getSymbolName(sym, name);
  401. log("comdat section " + name +
  402. " without leader and unassociated, discarding");
  403. continue;
  404. }
  405. symbols[i] = createRegular(sym);
  406. }
  407. for (auto &kv : weakAliases) {
  408. Symbol *sym = kv.first;
  409. uint32_t idx = kv.second;
  410. checkAndSetWeakAlias(symtab, this, sym, symbols[idx]);
  411. }
  412. }
  413. Symbol *ObjFile::createUndefined(COFFSymbolRef sym) {
  414. StringRef name;
  415. coffObj->getSymbolName(sym, name);
  416. return symtab->addUndefined(name, this, sym.isWeakExternal());
  417. }
  418. void ObjFile::handleComdatSelection(COFFSymbolRef sym, COMDATType &selection,
  419. bool &prevailing, DefinedRegular *leader) {
  420. if (prevailing)
  421. return;
  422. // There's already an existing comdat for this symbol: `Leader`.
  423. // Use the comdats's selection field to determine if the new
  424. // symbol in `Sym` should be discarded, produce a duplicate symbol
  425. // error, etc.
  426. SectionChunk *leaderChunk = nullptr;
  427. COMDATType leaderSelection = IMAGE_COMDAT_SELECT_ANY;
  428. if (leader->data) {
  429. leaderChunk = leader->getChunk();
  430. leaderSelection = leaderChunk->selection;
  431. } else {
  432. // FIXME: comdats from LTO files don't know their selection; treat them
  433. // as "any".
  434. selection = leaderSelection;
  435. }
  436. if ((selection == IMAGE_COMDAT_SELECT_ANY &&
  437. leaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
  438. (selection == IMAGE_COMDAT_SELECT_LARGEST &&
  439. leaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
  440. // cl.exe picks "any" for vftables when building with /GR- and
  441. // "largest" when building with /GR. To be able to link object files
  442. // compiled with each flag, "any" and "largest" are merged as "largest".
  443. leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
  444. }
  445. // Other than that, comdat selections must match. This is a bit more
  446. // strict than link.exe which allows merging "any" and "largest" if "any"
  447. // is the first symbol the linker sees, and it allows merging "largest"
  448. // with everything (!) if "largest" is the first symbol the linker sees.
  449. // Making this symmetric independent of which selection is seen first
  450. // seems better though.
  451. // (This behavior matches ModuleLinker::getComdatResult().)
  452. if (selection != leaderSelection) {
  453. log(("conflicting comdat type for " + toString(*leader) + ": " +
  454. Twine((int)leaderSelection) + " in " + toString(leader->getFile()) +
  455. " and " + Twine((int)selection) + " in " + toString(this))
  456. .str());
  457. symtab->reportDuplicate(leader, this);
  458. return;
  459. }
  460. switch (selection) {
  461. case IMAGE_COMDAT_SELECT_NODUPLICATES:
  462. symtab->reportDuplicate(leader, this);
  463. break;
  464. case IMAGE_COMDAT_SELECT_ANY:
  465. // Nothing to do.
  466. break;
  467. case IMAGE_COMDAT_SELECT_SAME_SIZE:
  468. if (leaderChunk->getSize() != getSection(sym)->SizeOfRawData)
  469. symtab->reportDuplicate(leader, this);
  470. break;
  471. case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
  472. SectionChunk newChunk(this, getSection(sym));
  473. // link.exe only compares section contents here and doesn't complain
  474. // if the two comdat sections have e.g. different alignment.
  475. // Match that.
  476. if (leaderChunk->getContents() != newChunk.getContents())
  477. symtab->reportDuplicate(leader, this, &newChunk, sym.getValue());
  478. break;
  479. }
  480. case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
  481. // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
  482. // (This means lld-link doesn't produce duplicate symbol errors for
  483. // associative comdats while link.exe does, but associate comdats
  484. // are never extern in practice.)
  485. llvm_unreachable("createDefined not called for associative comdats");
  486. case IMAGE_COMDAT_SELECT_LARGEST:
  487. if (leaderChunk->getSize() < getSection(sym)->SizeOfRawData) {
  488. // Replace the existing comdat symbol with the new one.
  489. StringRef name;
  490. coffObj->getSymbolName(sym, name);
  491. // FIXME: This is incorrect: With /opt:noref, the previous sections
  492. // make it into the final executable as well. Correct handling would
  493. // be to undo reading of the whole old section that's being replaced,
  494. // or doing one pass that determines what the final largest comdat
  495. // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
  496. // only the largest one.
  497. replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
  498. /*IsExternal*/ true, sym.getGeneric(),
  499. nullptr);
  500. prevailing = true;
  501. }
  502. break;
  503. case IMAGE_COMDAT_SELECT_NEWEST:
  504. llvm_unreachable("should have been rejected earlier");
  505. }
  506. }
  507. Optional<Symbol *> ObjFile::createDefined(
  508. COFFSymbolRef sym,
  509. std::vector<const coff_aux_section_definition *> &comdatDefs,
  510. bool &prevailing) {
  511. prevailing = false;
  512. auto getName = [&]() {
  513. StringRef s;
  514. coffObj->getSymbolName(sym, s);
  515. return s;
  516. };
  517. if (sym.isCommon()) {
  518. auto *c = make<CommonChunk>(sym);
  519. chunks.push_back(c);
  520. return symtab->addCommon(this, getName(), sym.getValue(), sym.getGeneric(),
  521. c);
  522. }
  523. if (sym.isAbsolute()) {
  524. StringRef name = getName();
  525. if (name == "@feat.00")
  526. feat00Flags = sym.getValue();
  527. // Skip special symbols.
  528. if (ignoredSymbolName(name))
  529. return nullptr;
  530. if (sym.isExternal())
  531. return symtab->addAbsolute(name, sym);
  532. return make<DefinedAbsolute>(name, sym);
  533. }
  534. int32_t sectionNumber = sym.getSectionNumber();
  535. if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
  536. return nullptr;
  537. if (llvm::COFF::isReservedSectionNumber(sectionNumber))
  538. fatal(toString(this) + ": " + getName() +
  539. " should not refer to special section " + Twine(sectionNumber));
  540. if ((uint32_t)sectionNumber >= sparseChunks.size())
  541. fatal(toString(this) + ": " + getName() +
  542. " should not refer to non-existent section " + Twine(sectionNumber));
  543. // Comdat handling.
  544. // A comdat symbol consists of two symbol table entries.
  545. // The first symbol entry has the name of the section (e.g. .text), fixed
  546. // values for the other fields, and one auxiliary record.
  547. // The second symbol entry has the name of the comdat symbol, called the
  548. // "comdat leader".
  549. // When this function is called for the first symbol entry of a comdat,
  550. // it sets comdatDefs and returns None, and when it's called for the second
  551. // symbol entry it reads comdatDefs and then sets it back to nullptr.
  552. // Handle comdat leader.
  553. if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
  554. comdatDefs[sectionNumber] = nullptr;
  555. DefinedRegular *leader;
  556. if (sym.isExternal()) {
  557. std::tie(leader, prevailing) =
  558. symtab->addComdat(this, getName(), sym.getGeneric());
  559. } else {
  560. leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
  561. /*IsExternal*/ false, sym.getGeneric());
  562. prevailing = true;
  563. }
  564. if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
  565. // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
  566. // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
  567. def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
  568. fatal("unknown comdat type " + std::to_string((int)def->Selection) +
  569. " for " + getName() + " in " + toString(this));
  570. }
  571. COMDATType selection = (COMDATType)def->Selection;
  572. if (leader->isCOMDAT)
  573. handleComdatSelection(sym, selection, prevailing, leader);
  574. if (prevailing) {
  575. SectionChunk *c = readSection(sectionNumber, def, getName());
  576. sparseChunks[sectionNumber] = c;
  577. c->sym = cast<DefinedRegular>(leader);
  578. c->selection = selection;
  579. cast<DefinedRegular>(leader)->data = &c->repl;
  580. } else {
  581. sparseChunks[sectionNumber] = nullptr;
  582. }
  583. return leader;
  584. }
  585. // Prepare to handle the comdat leader symbol by setting the section's
  586. // ComdatDefs pointer if we encounter a non-associative comdat.
  587. if (sparseChunks[sectionNumber] == pendingComdat) {
  588. if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
  589. if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
  590. comdatDefs[sectionNumber] = def;
  591. }
  592. return None;
  593. }
  594. return createRegular(sym);
  595. }
  596. MachineTypes ObjFile::getMachineType() {
  597. if (coffObj)
  598. return static_cast<MachineTypes>(coffObj->getMachine());
  599. return IMAGE_FILE_MACHINE_UNKNOWN;
  600. }
  601. ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
  602. if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
  603. return sec->consumeDebugMagic();
  604. return {};
  605. }
  606. // OBJ files systematically store critical information in a .debug$S stream,
  607. // even if the TU was compiled with no debug info. At least two records are
  608. // always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
  609. // PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
  610. // currently used to initialize the hotPatchable member.
  611. void ObjFile::initializeFlags() {
  612. ArrayRef<uint8_t> data = getDebugSection(".debug$S");
  613. if (data.empty())
  614. return;
  615. DebugSubsectionArray subsections;
  616. BinaryStreamReader reader(data, support::little);
  617. ExitOnError exitOnErr;
  618. exitOnErr(reader.readArray(subsections, data.size()));
  619. for (const DebugSubsectionRecord &ss : subsections) {
  620. if (ss.kind() != DebugSubsectionKind::Symbols)
  621. continue;
  622. unsigned offset = 0;
  623. // Only parse the first two records. We are only looking for S_OBJNAME
  624. // and S_COMPILE3, and they usually appear at the beginning of the
  625. // stream.
  626. for (unsigned i = 0; i < 2; ++i) {
  627. Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
  628. if (!sym) {
  629. consumeError(sym.takeError());
  630. return;
  631. }
  632. if (sym->kind() == SymbolKind::S_COMPILE3) {
  633. auto cs =
  634. cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
  635. hotPatchable =
  636. (cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
  637. }
  638. if (sym->kind() == SymbolKind::S_OBJNAME) {
  639. auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
  640. sym.get()));
  641. pchSignature = objName.Signature;
  642. }
  643. offset += sym->length();
  644. }
  645. }
  646. }
  647. // Depending on the compilation flags, OBJs can refer to external files,
  648. // necessary to merge this OBJ into the final PDB. We currently support two
  649. // types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
  650. // And PDB type servers, when compiling with /Zi. This function extracts these
  651. // dependencies and makes them available as a TpiSource interface (see
  652. // DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
  653. // output even with /Yc and /Yu and with /Zi.
  654. void ObjFile::initializeDependencies() {
  655. if (!config->debug)
  656. return;
  657. bool isPCH = false;
  658. ArrayRef<uint8_t> data = getDebugSection(".debug$P");
  659. if (!data.empty())
  660. isPCH = true;
  661. else
  662. data = getDebugSection(".debug$T");
  663. if (data.empty())
  664. return;
  665. CVTypeArray types;
  666. BinaryStreamReader reader(data, support::little);
  667. cantFail(reader.readArray(types, reader.getLength()));
  668. CVTypeArray::Iterator firstType = types.begin();
  669. if (firstType == types.end())
  670. return;
  671. debugTypes.emplace(types);
  672. if (isPCH) {
  673. debugTypesObj = makePrecompSource(this);
  674. return;
  675. }
  676. if (firstType->kind() == LF_TYPESERVER2) {
  677. TypeServer2Record ts = cantFail(
  678. TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
  679. debugTypesObj = makeUseTypeServerSource(this, &ts);
  680. return;
  681. }
  682. if (firstType->kind() == LF_PRECOMP) {
  683. PrecompRecord precomp = cantFail(
  684. TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
  685. debugTypesObj = makeUsePrecompSource(this, &precomp);
  686. return;
  687. }
  688. debugTypesObj = makeTpiSource(this);
  689. }
  690. // Used only for DWARF debug info, which is not common (except in MinGW
  691. // environments). This returns an optional pair of file name and line
  692. // number for where the variable was defined.
  693. Optional<std::pair<StringRef, uint32_t>>
  694. ObjFile::getVariableLocation(StringRef var) {
  695. if (!dwarf) {
  696. dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
  697. if (!dwarf)
  698. return None;
  699. }
  700. if (config->machine == I386)
  701. var.consume_front("_");
  702. Optional<std::pair<std::string, unsigned>> ret = dwarf->getVariableLoc(var);
  703. if (!ret)
  704. return None;
  705. return std::make_pair(saver.save(ret->first), ret->second);
  706. }
  707. // Used only for DWARF debug info, which is not common (except in MinGW
  708. // environments).
  709. Optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
  710. uint32_t sectionIndex) {
  711. if (!dwarf) {
  712. dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
  713. if (!dwarf)
  714. return None;
  715. }
  716. return dwarf->getDILineInfo(offset, sectionIndex);
  717. }
  718. StringRef ltrim1(StringRef s, const char *chars) {
  719. if (!s.empty() && strchr(chars, s[0]))
  720. return s.substr(1);
  721. return s;
  722. }
  723. void ImportFile::parse() {
  724. const char *buf = mb.getBufferStart();
  725. const auto *hdr = reinterpret_cast<const coff_import_header *>(buf);
  726. // Check if the total size is valid.
  727. if (mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
  728. fatal("broken import library");
  729. // Read names and create an __imp_ symbol.
  730. StringRef name = saver.save(StringRef(buf + sizeof(*hdr)));
  731. StringRef impName = saver.save("__imp_" + name);
  732. const char *nameStart = buf + sizeof(coff_import_header) + name.size() + 1;
  733. dllName = StringRef(nameStart);
  734. StringRef extName;
  735. switch (hdr->getNameType()) {
  736. case IMPORT_ORDINAL:
  737. extName = "";
  738. break;
  739. case IMPORT_NAME:
  740. extName = name;
  741. break;
  742. case IMPORT_NAME_NOPREFIX:
  743. extName = ltrim1(name, "?@_");
  744. break;
  745. case IMPORT_NAME_UNDECORATE:
  746. extName = ltrim1(name, "?@_");
  747. extName = extName.substr(0, extName.find('@'));
  748. break;
  749. }
  750. this->hdr = hdr;
  751. externalName = extName;
  752. impSym = symtab->addImportData(impName, this);
  753. // If this was a duplicate, we logged an error but may continue;
  754. // in this case, impSym is nullptr.
  755. if (!impSym)
  756. return;
  757. if (hdr->getType() == llvm::COFF::IMPORT_CONST)
  758. static_cast<void>(symtab->addImportData(name, this));
  759. // If type is function, we need to create a thunk which jump to an
  760. // address pointed by the __imp_ symbol. (This allows you to call
  761. // DLL functions just like regular non-DLL functions.)
  762. if (hdr->getType() == llvm::COFF::IMPORT_CODE)
  763. thunkSym = symtab->addImportThunk(
  764. name, cast_or_null<DefinedImportData>(impSym), hdr->Machine);
  765. }
  766. BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
  767. uint64_t offsetInArchive,
  768. std::vector<Symbol *> &&symbols)
  769. : InputFile(BitcodeKind, mb), symbols(std::move(symbols)) {
  770. std::string path = mb.getBufferIdentifier().str();
  771. if (config->thinLTOIndexOnly)
  772. path = replaceThinLTOSuffix(mb.getBufferIdentifier());
  773. // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
  774. // name. If two archives define two members with the same name, this
  775. // causes a collision which result in only one of the objects being taken
  776. // into consideration at LTO time (which very likely causes undefined
  777. // symbols later in the link stage). So we append file offset to make
  778. // filename unique.
  779. MemoryBufferRef mbref(
  780. mb.getBuffer(),
  781. saver.save(archiveName + path +
  782. (archiveName.empty() ? "" : utostr(offsetInArchive))));
  783. obj = check(lto::InputFile::create(mbref));
  784. }
  785. void BitcodeFile::parse() {
  786. std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
  787. for (size_t i = 0; i != obj->getComdatTable().size(); ++i)
  788. // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
  789. // selection handling.
  790. comdat[i] = symtab->addComdat(this, saver.save(obj->getComdatTable()[i]));
  791. for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
  792. StringRef symName = saver.save(objSym.getName());
  793. int comdatIndex = objSym.getComdatIndex();
  794. Symbol *sym;
  795. if (objSym.isUndefined()) {
  796. sym = symtab->addUndefined(symName, this, false);
  797. } else if (objSym.isCommon()) {
  798. sym = symtab->addCommon(this, symName, objSym.getCommonSize());
  799. } else if (objSym.isWeak() && objSym.isIndirect()) {
  800. // Weak external.
  801. sym = symtab->addUndefined(symName, this, true);
  802. std::string fallback = objSym.getCOFFWeakExternalFallback();
  803. Symbol *alias = symtab->addUndefined(saver.save(fallback));
  804. checkAndSetWeakAlias(symtab, this, sym, alias);
  805. } else if (comdatIndex != -1) {
  806. if (symName == obj->getComdatTable()[comdatIndex])
  807. sym = comdat[comdatIndex].first;
  808. else if (comdat[comdatIndex].second)
  809. sym = symtab->addRegular(this, symName);
  810. else
  811. sym = symtab->addUndefined(symName, this, false);
  812. } else {
  813. sym = symtab->addRegular(this, symName);
  814. }
  815. symbols.push_back(sym);
  816. if (objSym.isUsed())
  817. config->gcroot.push_back(sym);
  818. }
  819. directives = obj->getCOFFLinkerOpts();
  820. }
  821. MachineTypes BitcodeFile::getMachineType() {
  822. switch (Triple(obj->getTargetTriple()).getArch()) {
  823. case Triple::x86_64:
  824. return AMD64;
  825. case Triple::x86:
  826. return I386;
  827. case Triple::arm:
  828. return ARMNT;
  829. case Triple::aarch64:
  830. return ARM64;
  831. default:
  832. return IMAGE_FILE_MACHINE_UNKNOWN;
  833. }
  834. }
  835. std::string replaceThinLTOSuffix(StringRef path) {
  836. StringRef suffix = config->thinLTOObjectSuffixReplace.first;
  837. StringRef repl = config->thinLTOObjectSuffixReplace.second;
  838. if (path.consume_back(suffix))
  839. return (path + repl).str();
  840. return path;
  841. }
  842. } // namespace coff
  843. } // namespace lld