SymbolTable.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. //===- SymbolTable.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 "SymbolTable.h"
  9. #include "Config.h"
  10. #include "Driver.h"
  11. #include "LTO.h"
  12. #include "PDB.h"
  13. #include "Symbols.h"
  14. #include "lld/Common/ErrorHandler.h"
  15. #include "lld/Common/Memory.h"
  16. #include "lld/Common/Timer.h"
  17. #include "llvm/DebugInfo/Symbolize/Symbolize.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/Object/WindowsMachineFlag.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <utility>
  23. using namespace llvm;
  24. namespace lld {
  25. namespace coff {
  26. static Timer ltoTimer("LTO", Timer::root());
  27. SymbolTable *symtab;
  28. void SymbolTable::addFile(InputFile *file) {
  29. log("Reading " + toString(file));
  30. file->parse();
  31. MachineTypes mt = file->getMachineType();
  32. if (config->machine == IMAGE_FILE_MACHINE_UNKNOWN) {
  33. config->machine = mt;
  34. } else if (mt != IMAGE_FILE_MACHINE_UNKNOWN && config->machine != mt) {
  35. error(toString(file) + ": machine type " + machineToStr(mt) +
  36. " conflicts with " + machineToStr(config->machine));
  37. return;
  38. }
  39. if (auto *f = dyn_cast<ObjFile>(file)) {
  40. ObjFile::instances.push_back(f);
  41. } else if (auto *f = dyn_cast<BitcodeFile>(file)) {
  42. BitcodeFile::instances.push_back(f);
  43. } else if (auto *f = dyn_cast<ImportFile>(file)) {
  44. ImportFile::instances.push_back(f);
  45. }
  46. driver->parseDirectives(file);
  47. }
  48. static void errorOrWarn(const Twine &s) {
  49. if (config->forceUnresolved)
  50. warn(s);
  51. else
  52. error(s);
  53. }
  54. // Causes the file associated with a lazy symbol to be linked in.
  55. static void forceLazy(Symbol *s) {
  56. s->pendingArchiveLoad = true;
  57. switch (s->kind()) {
  58. case Symbol::Kind::LazyArchiveKind: {
  59. auto *l = cast<LazyArchive>(s);
  60. l->file->addMember(l->sym);
  61. break;
  62. }
  63. case Symbol::Kind::LazyObjectKind:
  64. cast<LazyObject>(s)->file->fetch();
  65. break;
  66. default:
  67. llvm_unreachable(
  68. "symbol passed to forceLazy is not a LazyArchive or LazyObject");
  69. }
  70. }
  71. // Returns the symbol in SC whose value is <= Addr that is closest to Addr.
  72. // This is generally the global variable or function whose definition contains
  73. // Addr.
  74. static Symbol *getSymbol(SectionChunk *sc, uint32_t addr) {
  75. DefinedRegular *candidate = nullptr;
  76. for (Symbol *s : sc->file->getSymbols()) {
  77. auto *d = dyn_cast_or_null<DefinedRegular>(s);
  78. if (!d || !d->data || d->file != sc->file || d->getChunk() != sc ||
  79. d->getValue() > addr ||
  80. (candidate && d->getValue() < candidate->getValue()))
  81. continue;
  82. candidate = d;
  83. }
  84. return candidate;
  85. }
  86. static std::vector<std::string> getSymbolLocations(BitcodeFile *file) {
  87. std::string res("\n>>> referenced by ");
  88. StringRef source = file->obj->getSourceFileName();
  89. if (!source.empty())
  90. res += source.str() + "\n>>> ";
  91. res += toString(file);
  92. return {res};
  93. }
  94. static Optional<std::pair<StringRef, uint32_t>>
  95. getFileLineDwarf(const SectionChunk *c, uint32_t addr) {
  96. Optional<DILineInfo> optionalLineInfo =
  97. c->file->getDILineInfo(addr, c->getSectionNumber() - 1);
  98. if (!optionalLineInfo)
  99. return None;
  100. const DILineInfo &lineInfo = *optionalLineInfo;
  101. if (lineInfo.FileName == DILineInfo::BadString)
  102. return None;
  103. return std::make_pair(saver.save(lineInfo.FileName), lineInfo.Line);
  104. }
  105. static Optional<std::pair<StringRef, uint32_t>>
  106. getFileLine(const SectionChunk *c, uint32_t addr) {
  107. // MinGW can optionally use codeview, even if the default is dwarf.
  108. Optional<std::pair<StringRef, uint32_t>> fileLine =
  109. getFileLineCodeView(c, addr);
  110. // If codeview didn't yield any result, check dwarf in MinGW mode.
  111. if (!fileLine && config->mingw)
  112. fileLine = getFileLineDwarf(c, addr);
  113. return fileLine;
  114. }
  115. // Given a file and the index of a symbol in that file, returns a description
  116. // of all references to that symbol from that file. If no debug information is
  117. // available, returns just the name of the file, else one string per actual
  118. // reference as described in the debug info.
  119. std::vector<std::string> getSymbolLocations(ObjFile *file, uint32_t symIndex) {
  120. struct Location {
  121. Symbol *sym;
  122. std::pair<StringRef, uint32_t> fileLine;
  123. };
  124. std::vector<Location> locations;
  125. for (Chunk *c : file->getChunks()) {
  126. auto *sc = dyn_cast<SectionChunk>(c);
  127. if (!sc)
  128. continue;
  129. for (const coff_relocation &r : sc->getRelocs()) {
  130. if (r.SymbolTableIndex != symIndex)
  131. continue;
  132. Optional<std::pair<StringRef, uint32_t>> fileLine =
  133. getFileLine(sc, r.VirtualAddress);
  134. Symbol *sym = getSymbol(sc, r.VirtualAddress);
  135. if (fileLine)
  136. locations.push_back({sym, *fileLine});
  137. else if (sym)
  138. locations.push_back({sym, {"", 0}});
  139. }
  140. }
  141. if (locations.empty())
  142. return std::vector<std::string>({"\n>>> referenced by " + toString(file)});
  143. std::vector<std::string> symbolLocations(locations.size());
  144. size_t i = 0;
  145. for (Location loc : locations) {
  146. llvm::raw_string_ostream os(symbolLocations[i++]);
  147. os << "\n>>> referenced by ";
  148. if (!loc.fileLine.first.empty())
  149. os << loc.fileLine.first << ":" << loc.fileLine.second
  150. << "\n>>> ";
  151. os << toString(file);
  152. if (loc.sym)
  153. os << ":(" << toString(*loc.sym) << ')';
  154. }
  155. return symbolLocations;
  156. }
  157. std::vector<std::string> getSymbolLocations(InputFile *file,
  158. uint32_t symIndex) {
  159. if (auto *o = dyn_cast<ObjFile>(file))
  160. return getSymbolLocations(o, symIndex);
  161. if (auto *b = dyn_cast<BitcodeFile>(file))
  162. return getSymbolLocations(b);
  163. llvm_unreachable("unsupported file type passed to getSymbolLocations");
  164. return {};
  165. }
  166. // For an undefined symbol, stores all files referencing it and the index of
  167. // the undefined symbol in each file.
  168. struct UndefinedDiag {
  169. Symbol *sym;
  170. struct File {
  171. InputFile *file;
  172. uint32_t symIndex;
  173. };
  174. std::vector<File> files;
  175. };
  176. static void reportUndefinedSymbol(const UndefinedDiag &undefDiag) {
  177. std::string out;
  178. llvm::raw_string_ostream os(out);
  179. os << "undefined symbol: " << toString(*undefDiag.sym);
  180. const size_t maxUndefReferences = 10;
  181. size_t i = 0, numRefs = 0;
  182. for (const UndefinedDiag::File &ref : undefDiag.files) {
  183. std::vector<std::string> symbolLocations =
  184. getSymbolLocations(ref.file, ref.symIndex);
  185. numRefs += symbolLocations.size();
  186. for (const std::string &s : symbolLocations) {
  187. if (i >= maxUndefReferences)
  188. break;
  189. os << s;
  190. i++;
  191. }
  192. }
  193. if (i < numRefs)
  194. os << "\n>>> referenced " << numRefs - i << " more times";
  195. errorOrWarn(os.str());
  196. }
  197. void SymbolTable::loadMinGWAutomaticImports() {
  198. for (auto &i : symMap) {
  199. Symbol *sym = i.second;
  200. auto *undef = dyn_cast<Undefined>(sym);
  201. if (!undef)
  202. continue;
  203. if (!sym->isUsedInRegularObj)
  204. continue;
  205. if (undef->getWeakAlias())
  206. continue;
  207. StringRef name = undef->getName();
  208. if (name.startswith("__imp_"))
  209. continue;
  210. // If we have an undefined symbol, but we have a lazy symbol we could
  211. // load, load it.
  212. Symbol *l = find(("__imp_" + name).str());
  213. if (!l || l->pendingArchiveLoad || !l->isLazy())
  214. continue;
  215. log("Loading lazy " + l->getName() + " from " + l->getFile()->getName() +
  216. " for automatic import");
  217. forceLazy(l);
  218. }
  219. }
  220. Defined *SymbolTable::impSymbol(StringRef name) {
  221. if (name.startswith("__imp_"))
  222. return nullptr;
  223. return dyn_cast_or_null<Defined>(find(("__imp_" + name).str()));
  224. }
  225. bool SymbolTable::handleMinGWAutomaticImport(Symbol *sym, StringRef name) {
  226. Defined *imp = impSymbol(name);
  227. if (!imp)
  228. return false;
  229. // Replace the reference directly to a variable with a reference
  230. // to the import address table instead. This obviously isn't right,
  231. // but we mark the symbol as isRuntimePseudoReloc, and a later pass
  232. // will add runtime pseudo relocations for every relocation against
  233. // this Symbol. The runtime pseudo relocation framework expects the
  234. // reference itself to point at the IAT entry.
  235. size_t impSize = 0;
  236. if (isa<DefinedImportData>(imp)) {
  237. log("Automatically importing " + name + " from " +
  238. cast<DefinedImportData>(imp)->getDLLName());
  239. impSize = sizeof(DefinedImportData);
  240. } else if (isa<DefinedRegular>(imp)) {
  241. log("Automatically importing " + name + " from " +
  242. toString(cast<DefinedRegular>(imp)->file));
  243. impSize = sizeof(DefinedRegular);
  244. } else {
  245. warn("unable to automatically import " + name + " from " + imp->getName() +
  246. " from " + toString(cast<DefinedRegular>(imp)->file) +
  247. "; unexpected symbol type");
  248. return false;
  249. }
  250. sym->replaceKeepingName(imp, impSize);
  251. sym->isRuntimePseudoReloc = true;
  252. // There may exist symbols named .refptr.<name> which only consist
  253. // of a single pointer to <name>. If it turns out <name> is
  254. // automatically imported, we don't need to keep the .refptr.<name>
  255. // pointer at all, but redirect all accesses to it to the IAT entry
  256. // for __imp_<name> instead, and drop the whole .refptr.<name> chunk.
  257. DefinedRegular *refptr =
  258. dyn_cast_or_null<DefinedRegular>(find((".refptr." + name).str()));
  259. if (refptr && refptr->getChunk()->getSize() == config->wordsize) {
  260. SectionChunk *sc = dyn_cast_or_null<SectionChunk>(refptr->getChunk());
  261. if (sc && sc->getRelocs().size() == 1 && *sc->symbols().begin() == sym) {
  262. log("Replacing .refptr." + name + " with " + imp->getName());
  263. refptr->getChunk()->live = false;
  264. refptr->replaceKeepingName(imp, impSize);
  265. }
  266. }
  267. return true;
  268. }
  269. /// Helper function for reportUnresolvable and resolveRemainingUndefines.
  270. /// This function emits an "undefined symbol" diagnostic for each symbol in
  271. /// undefs. If localImports is not nullptr, it also emits a "locally
  272. /// defined symbol imported" diagnostic for symbols in localImports.
  273. /// objFiles and bitcodeFiles (if not nullptr) are used to report where
  274. /// undefined symbols are referenced.
  275. static void
  276. reportProblemSymbols(const SmallPtrSetImpl<Symbol *> &undefs,
  277. const DenseMap<Symbol *, Symbol *> *localImports,
  278. const std::vector<ObjFile *> objFiles,
  279. const std::vector<BitcodeFile *> *bitcodeFiles) {
  280. // Return early if there is nothing to report (which should be
  281. // the common case).
  282. if (undefs.empty() && (!localImports || localImports->empty()))
  283. return;
  284. for (Symbol *b : config->gcroot) {
  285. if (undefs.count(b))
  286. errorOrWarn("<root>: undefined symbol: " + toString(*b));
  287. if (localImports)
  288. if (Symbol *imp = localImports->lookup(b))
  289. warn("<root>: locally defined symbol imported: " + toString(*imp) +
  290. " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
  291. }
  292. std::vector<UndefinedDiag> undefDiags;
  293. DenseMap<Symbol *, int> firstDiag;
  294. auto processFile = [&](InputFile *file, ArrayRef<Symbol *> symbols) {
  295. uint32_t symIndex = (uint32_t)-1;
  296. for (Symbol *sym : symbols) {
  297. ++symIndex;
  298. if (!sym)
  299. continue;
  300. if (undefs.count(sym)) {
  301. auto it = firstDiag.find(sym);
  302. if (it == firstDiag.end()) {
  303. firstDiag[sym] = undefDiags.size();
  304. undefDiags.push_back({sym, {{file, symIndex}}});
  305. } else {
  306. undefDiags[it->second].files.push_back({file, symIndex});
  307. }
  308. }
  309. if (localImports)
  310. if (Symbol *imp = localImports->lookup(sym))
  311. warn(toString(file) +
  312. ": locally defined symbol imported: " + toString(*imp) +
  313. " (defined in " + toString(imp->getFile()) + ") [LNK4217]");
  314. }
  315. };
  316. for (ObjFile *file : objFiles)
  317. processFile(file, file->getSymbols());
  318. if (bitcodeFiles)
  319. for (BitcodeFile *file : *bitcodeFiles)
  320. processFile(file, file->getSymbols());
  321. for (const UndefinedDiag &undefDiag : undefDiags)
  322. reportUndefinedSymbol(undefDiag);
  323. }
  324. void SymbolTable::reportUnresolvable() {
  325. SmallPtrSet<Symbol *, 8> undefs;
  326. for (auto &i : symMap) {
  327. Symbol *sym = i.second;
  328. auto *undef = dyn_cast<Undefined>(sym);
  329. if (!undef)
  330. continue;
  331. if (undef->getWeakAlias())
  332. continue;
  333. StringRef name = undef->getName();
  334. if (name.startswith("__imp_")) {
  335. Symbol *imp = find(name.substr(strlen("__imp_")));
  336. if (imp && isa<Defined>(imp))
  337. continue;
  338. }
  339. if (name.contains("_PchSym_"))
  340. continue;
  341. if (config->mingw && impSymbol(name))
  342. continue;
  343. undefs.insert(sym);
  344. }
  345. reportProblemSymbols(undefs,
  346. /* localImports */ nullptr, ObjFile::instances,
  347. &BitcodeFile::instances);
  348. }
  349. void SymbolTable::resolveRemainingUndefines() {
  350. SmallPtrSet<Symbol *, 8> undefs;
  351. DenseMap<Symbol *, Symbol *> localImports;
  352. for (auto &i : symMap) {
  353. Symbol *sym = i.second;
  354. auto *undef = dyn_cast<Undefined>(sym);
  355. if (!undef)
  356. continue;
  357. if (!sym->isUsedInRegularObj)
  358. continue;
  359. StringRef name = undef->getName();
  360. // A weak alias may have been resolved, so check for that.
  361. if (Defined *d = undef->getWeakAlias()) {
  362. // We want to replace Sym with D. However, we can't just blindly
  363. // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
  364. // internal symbol, and internal symbols are stored as "unparented"
  365. // Symbols. For that reason we need to check which type of symbol we
  366. // are dealing with and copy the correct number of bytes.
  367. if (isa<DefinedRegular>(d))
  368. memcpy(sym, d, sizeof(DefinedRegular));
  369. else if (isa<DefinedAbsolute>(d))
  370. memcpy(sym, d, sizeof(DefinedAbsolute));
  371. else
  372. memcpy(sym, d, sizeof(SymbolUnion));
  373. continue;
  374. }
  375. // If we can resolve a symbol by removing __imp_ prefix, do that.
  376. // This odd rule is for compatibility with MSVC linker.
  377. if (name.startswith("__imp_")) {
  378. Symbol *imp = find(name.substr(strlen("__imp_")));
  379. if (imp && isa<Defined>(imp)) {
  380. auto *d = cast<Defined>(imp);
  381. replaceSymbol<DefinedLocalImport>(sym, name, d);
  382. localImportChunks.push_back(cast<DefinedLocalImport>(sym)->getChunk());
  383. localImports[sym] = d;
  384. continue;
  385. }
  386. }
  387. // We don't want to report missing Microsoft precompiled headers symbols.
  388. // A proper message will be emitted instead in PDBLinker::aquirePrecompObj
  389. if (name.contains("_PchSym_"))
  390. continue;
  391. if (config->mingw && handleMinGWAutomaticImport(sym, name))
  392. continue;
  393. // Remaining undefined symbols are not fatal if /force is specified.
  394. // They are replaced with dummy defined symbols.
  395. if (config->forceUnresolved)
  396. replaceSymbol<DefinedAbsolute>(sym, name, 0);
  397. undefs.insert(sym);
  398. }
  399. reportProblemSymbols(
  400. undefs, config->warnLocallyDefinedImported ? &localImports : nullptr,
  401. ObjFile::instances, /* bitcode files no longer needed */ nullptr);
  402. }
  403. std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
  404. bool inserted = false;
  405. Symbol *&sym = symMap[CachedHashStringRef(name)];
  406. if (!sym) {
  407. sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
  408. sym->isUsedInRegularObj = false;
  409. sym->pendingArchiveLoad = false;
  410. inserted = true;
  411. }
  412. return {sym, inserted};
  413. }
  414. std::pair<Symbol *, bool> SymbolTable::insert(StringRef name, InputFile *file) {
  415. std::pair<Symbol *, bool> result = insert(name);
  416. if (!file || !isa<BitcodeFile>(file))
  417. result.first->isUsedInRegularObj = true;
  418. return result;
  419. }
  420. Symbol *SymbolTable::addUndefined(StringRef name, InputFile *f,
  421. bool isWeakAlias) {
  422. Symbol *s;
  423. bool wasInserted;
  424. std::tie(s, wasInserted) = insert(name, f);
  425. if (wasInserted || (s->isLazy() && isWeakAlias)) {
  426. replaceSymbol<Undefined>(s, name);
  427. return s;
  428. }
  429. if (s->isLazy())
  430. forceLazy(s);
  431. return s;
  432. }
  433. void SymbolTable::addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym) {
  434. StringRef name = sym.getName();
  435. Symbol *s;
  436. bool wasInserted;
  437. std::tie(s, wasInserted) = insert(name);
  438. if (wasInserted) {
  439. replaceSymbol<LazyArchive>(s, f, sym);
  440. return;
  441. }
  442. auto *u = dyn_cast<Undefined>(s);
  443. if (!u || u->weakAlias || s->pendingArchiveLoad)
  444. return;
  445. s->pendingArchiveLoad = true;
  446. f->addMember(sym);
  447. }
  448. void SymbolTable::addLazyObject(LazyObjFile *f, StringRef n) {
  449. Symbol *s;
  450. bool wasInserted;
  451. std::tie(s, wasInserted) = insert(n, f);
  452. if (wasInserted) {
  453. replaceSymbol<LazyObject>(s, f, n);
  454. return;
  455. }
  456. auto *u = dyn_cast<Undefined>(s);
  457. if (!u || u->weakAlias || s->pendingArchiveLoad)
  458. return;
  459. s->pendingArchiveLoad = true;
  460. f->fetch();
  461. }
  462. static std::string getSourceLocationBitcode(BitcodeFile *file) {
  463. std::string res("\n>>> defined at ");
  464. StringRef source = file->obj->getSourceFileName();
  465. if (!source.empty())
  466. res += source.str() + "\n>>> ";
  467. res += toString(file);
  468. return res;
  469. }
  470. static std::string getSourceLocationObj(ObjFile *file, SectionChunk *sc,
  471. uint32_t offset, StringRef name) {
  472. Optional<std::pair<StringRef, uint32_t>> fileLine;
  473. if (sc)
  474. fileLine = getFileLine(sc, offset);
  475. if (!fileLine)
  476. fileLine = file->getVariableLocation(name);
  477. std::string res;
  478. llvm::raw_string_ostream os(res);
  479. os << "\n>>> defined at ";
  480. if (fileLine)
  481. os << fileLine->first << ":" << fileLine->second << "\n>>> ";
  482. os << toString(file);
  483. return os.str();
  484. }
  485. static std::string getSourceLocation(InputFile *file, SectionChunk *sc,
  486. uint32_t offset, StringRef name) {
  487. if (auto *o = dyn_cast<ObjFile>(file))
  488. return getSourceLocationObj(o, sc, offset, name);
  489. if (auto *b = dyn_cast<BitcodeFile>(file))
  490. return getSourceLocationBitcode(b);
  491. return "\n>>> defined at " + toString(file);
  492. }
  493. // Construct and print an error message in the form of:
  494. //
  495. // lld-link: error: duplicate symbol: foo
  496. // >>> defined at bar.c:30
  497. // >>> bar.o
  498. // >>> defined at baz.c:563
  499. // >>> baz.o
  500. void SymbolTable::reportDuplicate(Symbol *existing, InputFile *newFile,
  501. SectionChunk *newSc,
  502. uint32_t newSectionOffset) {
  503. std::string msg;
  504. llvm::raw_string_ostream os(msg);
  505. os << "duplicate symbol: " << toString(*existing);
  506. DefinedRegular *d = cast<DefinedRegular>(existing);
  507. if (d && isa<ObjFile>(d->getFile())) {
  508. os << getSourceLocation(d->getFile(), d->getChunk(), d->getValue(),
  509. existing->getName());
  510. } else {
  511. os << getSourceLocation(existing->getFile(), nullptr, 0, "");
  512. }
  513. os << getSourceLocation(newFile, newSc, newSectionOffset,
  514. existing->getName());
  515. if (config->forceMultiple)
  516. warn(os.str());
  517. else
  518. error(os.str());
  519. }
  520. Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {
  521. Symbol *s;
  522. bool wasInserted;
  523. std::tie(s, wasInserted) = insert(n, nullptr);
  524. s->isUsedInRegularObj = true;
  525. if (wasInserted || isa<Undefined>(s) || s->isLazy())
  526. replaceSymbol<DefinedAbsolute>(s, n, sym);
  527. else if (!isa<DefinedCOFF>(s))
  528. reportDuplicate(s, nullptr);
  529. return s;
  530. }
  531. Symbol *SymbolTable::addAbsolute(StringRef n, uint64_t va) {
  532. Symbol *s;
  533. bool wasInserted;
  534. std::tie(s, wasInserted) = insert(n, nullptr);
  535. s->isUsedInRegularObj = true;
  536. if (wasInserted || isa<Undefined>(s) || s->isLazy())
  537. replaceSymbol<DefinedAbsolute>(s, n, va);
  538. else if (!isa<DefinedCOFF>(s))
  539. reportDuplicate(s, nullptr);
  540. return s;
  541. }
  542. Symbol *SymbolTable::addSynthetic(StringRef n, Chunk *c) {
  543. Symbol *s;
  544. bool wasInserted;
  545. std::tie(s, wasInserted) = insert(n, nullptr);
  546. s->isUsedInRegularObj = true;
  547. if (wasInserted || isa<Undefined>(s) || s->isLazy())
  548. replaceSymbol<DefinedSynthetic>(s, n, c);
  549. else if (!isa<DefinedCOFF>(s))
  550. reportDuplicate(s, nullptr);
  551. return s;
  552. }
  553. Symbol *SymbolTable::addRegular(InputFile *f, StringRef n,
  554. const coff_symbol_generic *sym, SectionChunk *c,
  555. uint32_t sectionOffset) {
  556. Symbol *s;
  557. bool wasInserted;
  558. std::tie(s, wasInserted) = insert(n, f);
  559. if (wasInserted || !isa<DefinedRegular>(s))
  560. replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ false,
  561. /*IsExternal*/ true, sym, c);
  562. else
  563. reportDuplicate(s, f, c, sectionOffset);
  564. return s;
  565. }
  566. std::pair<DefinedRegular *, bool>
  567. SymbolTable::addComdat(InputFile *f, StringRef n,
  568. const coff_symbol_generic *sym) {
  569. Symbol *s;
  570. bool wasInserted;
  571. std::tie(s, wasInserted) = insert(n, f);
  572. if (wasInserted || !isa<DefinedRegular>(s)) {
  573. replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ true,
  574. /*IsExternal*/ true, sym, nullptr);
  575. return {cast<DefinedRegular>(s), true};
  576. }
  577. auto *existingSymbol = cast<DefinedRegular>(s);
  578. if (!existingSymbol->isCOMDAT)
  579. reportDuplicate(s, f);
  580. return {existingSymbol, false};
  581. }
  582. Symbol *SymbolTable::addCommon(InputFile *f, StringRef n, uint64_t size,
  583. const coff_symbol_generic *sym, CommonChunk *c) {
  584. Symbol *s;
  585. bool wasInserted;
  586. std::tie(s, wasInserted) = insert(n, f);
  587. if (wasInserted || !isa<DefinedCOFF>(s))
  588. replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
  589. else if (auto *dc = dyn_cast<DefinedCommon>(s))
  590. if (size > dc->getSize())
  591. replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
  592. return s;
  593. }
  594. Symbol *SymbolTable::addImportData(StringRef n, ImportFile *f) {
  595. Symbol *s;
  596. bool wasInserted;
  597. std::tie(s, wasInserted) = insert(n, nullptr);
  598. s->isUsedInRegularObj = true;
  599. if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
  600. replaceSymbol<DefinedImportData>(s, n, f);
  601. return s;
  602. }
  603. reportDuplicate(s, f);
  604. return nullptr;
  605. }
  606. Symbol *SymbolTable::addImportThunk(StringRef name, DefinedImportData *id,
  607. uint16_t machine) {
  608. Symbol *s;
  609. bool wasInserted;
  610. std::tie(s, wasInserted) = insert(name, nullptr);
  611. s->isUsedInRegularObj = true;
  612. if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
  613. replaceSymbol<DefinedImportThunk>(s, name, id, machine);
  614. return s;
  615. }
  616. reportDuplicate(s, id->file);
  617. return nullptr;
  618. }
  619. void SymbolTable::addLibcall(StringRef name) {
  620. Symbol *sym = findUnderscore(name);
  621. if (!sym)
  622. return;
  623. if (auto *l = dyn_cast<LazyArchive>(sym)) {
  624. MemoryBufferRef mb = l->getMemberBuffer();
  625. if (isBitcode(mb))
  626. addUndefined(sym->getName());
  627. } else if (LazyObject *o = dyn_cast<LazyObject>(sym)) {
  628. if (isBitcode(o->file->mb))
  629. addUndefined(sym->getName());
  630. }
  631. }
  632. std::vector<Chunk *> SymbolTable::getChunks() {
  633. std::vector<Chunk *> res;
  634. for (ObjFile *file : ObjFile::instances) {
  635. ArrayRef<Chunk *> v = file->getChunks();
  636. res.insert(res.end(), v.begin(), v.end());
  637. }
  638. return res;
  639. }
  640. Symbol *SymbolTable::find(StringRef name) {
  641. return symMap.lookup(CachedHashStringRef(name));
  642. }
  643. Symbol *SymbolTable::findUnderscore(StringRef name) {
  644. if (config->machine == I386)
  645. return find(("_" + name).str());
  646. return find(name);
  647. }
  648. // Return all symbols that start with Prefix, possibly ignoring the first
  649. // character of Prefix or the first character symbol.
  650. std::vector<Symbol *> SymbolTable::getSymsWithPrefix(StringRef prefix) {
  651. std::vector<Symbol *> syms;
  652. for (auto pair : symMap) {
  653. StringRef name = pair.first.val();
  654. if (name.startswith(prefix) || name.startswith(prefix.drop_front()) ||
  655. name.drop_front().startswith(prefix) ||
  656. name.drop_front().startswith(prefix.drop_front())) {
  657. syms.push_back(pair.second);
  658. }
  659. }
  660. return syms;
  661. }
  662. Symbol *SymbolTable::findMangle(StringRef name) {
  663. if (Symbol *sym = find(name))
  664. if (!isa<Undefined>(sym))
  665. return sym;
  666. // Efficient fuzzy string lookup is impossible with a hash table, so iterate
  667. // the symbol table once and collect all possibly matching symbols into this
  668. // vector. Then compare each possibly matching symbol with each possible
  669. // mangling.
  670. std::vector<Symbol *> syms = getSymsWithPrefix(name);
  671. auto findByPrefix = [&syms](const Twine &t) -> Symbol * {
  672. std::string prefix = t.str();
  673. for (auto *s : syms)
  674. if (s->getName().startswith(prefix))
  675. return s;
  676. return nullptr;
  677. };
  678. // For non-x86, just look for C++ functions.
  679. if (config->machine != I386)
  680. return findByPrefix("?" + name + "@@Y");
  681. if (!name.startswith("_"))
  682. return nullptr;
  683. // Search for x86 stdcall function.
  684. if (Symbol *s = findByPrefix(name + "@"))
  685. return s;
  686. // Search for x86 fastcall function.
  687. if (Symbol *s = findByPrefix("@" + name.substr(1) + "@"))
  688. return s;
  689. // Search for x86 vectorcall function.
  690. if (Symbol *s = findByPrefix(name.substr(1) + "@@"))
  691. return s;
  692. // Search for x86 C++ non-member function.
  693. return findByPrefix("?" + name.substr(1) + "@@Y");
  694. }
  695. Symbol *SymbolTable::addUndefined(StringRef name) {
  696. return addUndefined(name, nullptr, false);
  697. }
  698. std::vector<StringRef> SymbolTable::compileBitcodeFiles() {
  699. lto.reset(new BitcodeCompiler);
  700. for (BitcodeFile *f : BitcodeFile::instances)
  701. lto->add(*f);
  702. return lto->compile();
  703. }
  704. void SymbolTable::addCombinedLTOObjects() {
  705. if (BitcodeFile::instances.empty())
  706. return;
  707. ScopedTimer t(ltoTimer);
  708. for (StringRef object : compileBitcodeFiles()) {
  709. auto *obj = make<ObjFile>(MemoryBufferRef(object, "lto.tmp"));
  710. obj->parse();
  711. ObjFile::instances.push_back(obj);
  712. }
  713. }
  714. } // namespace coff
  715. } // namespace lld