InputFiles.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 "Config.h"
  10. #include "InputChunks.h"
  11. #include "InputEvent.h"
  12. #include "InputGlobal.h"
  13. #include "SymbolTable.h"
  14. #include "lld/Common/ErrorHandler.h"
  15. #include "lld/Common/Memory.h"
  16. #include "lld/Common/Reproduce.h"
  17. #include "llvm/Object/Binary.h"
  18. #include "llvm/Object/Wasm.h"
  19. #include "llvm/Support/TarWriter.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #define DEBUG_TYPE "lld"
  22. using namespace llvm;
  23. using namespace llvm::object;
  24. using namespace llvm::wasm;
  25. namespace lld {
  26. // Returns a string in the format of "foo.o" or "foo.a(bar.o)".
  27. std::string toString(const wasm::InputFile *file) {
  28. if (!file)
  29. return "<internal>";
  30. if (file->archiveName.empty())
  31. return file->getName();
  32. return (file->archiveName + "(" + file->getName() + ")").str();
  33. }
  34. namespace wasm {
  35. std::unique_ptr<llvm::TarWriter> tar;
  36. Optional<MemoryBufferRef> readFile(StringRef path) {
  37. log("Loading: " + path);
  38. auto mbOrErr = MemoryBuffer::getFile(path);
  39. if (auto ec = mbOrErr.getError()) {
  40. error("cannot open " + path + ": " + ec.message());
  41. return None;
  42. }
  43. std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
  44. MemoryBufferRef mbref = mb->getMemBufferRef();
  45. make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership
  46. if (tar)
  47. tar->append(relativeToRoot(path), mbref.getBuffer());
  48. return mbref;
  49. }
  50. InputFile *createObjectFile(MemoryBufferRef mb,
  51. StringRef archiveName) {
  52. file_magic magic = identify_magic(mb.getBuffer());
  53. if (magic == file_magic::wasm_object) {
  54. std::unique_ptr<Binary> bin =
  55. CHECK(createBinary(mb), mb.getBufferIdentifier());
  56. auto *obj = cast<WasmObjectFile>(bin.get());
  57. if (obj->isSharedObject())
  58. return make<SharedFile>(mb);
  59. return make<ObjFile>(mb, archiveName);
  60. }
  61. if (magic == file_magic::bitcode)
  62. return make<BitcodeFile>(mb, archiveName);
  63. fatal("unknown file type: " + mb.getBufferIdentifier());
  64. }
  65. void ObjFile::dumpInfo() const {
  66. log("info for: " + toString(this) +
  67. "\n Symbols : " + Twine(symbols.size()) +
  68. "\n Function Imports : " + Twine(wasmObj->getNumImportedFunctions()) +
  69. "\n Global Imports : " + Twine(wasmObj->getNumImportedGlobals()) +
  70. "\n Event Imports : " + Twine(wasmObj->getNumImportedEvents()));
  71. }
  72. // Relocations contain either symbol or type indices. This function takes a
  73. // relocation and returns relocated index (i.e. translates from the input
  74. // symbol/type space to the output symbol/type space).
  75. uint32_t ObjFile::calcNewIndex(const WasmRelocation &reloc) const {
  76. if (reloc.Type == R_WASM_TYPE_INDEX_LEB) {
  77. assert(typeIsUsed[reloc.Index]);
  78. return typeMap[reloc.Index];
  79. }
  80. const Symbol *sym = symbols[reloc.Index];
  81. if (auto *ss = dyn_cast<SectionSymbol>(sym))
  82. sym = ss->getOutputSectionSymbol();
  83. return sym->getOutputSymbolIndex();
  84. }
  85. // Relocations can contain addend for combined sections. This function takes a
  86. // relocation and returns updated addend by offset in the output section.
  87. uint32_t ObjFile::calcNewAddend(const WasmRelocation &reloc) const {
  88. switch (reloc.Type) {
  89. case R_WASM_MEMORY_ADDR_LEB:
  90. case R_WASM_MEMORY_ADDR_SLEB:
  91. case R_WASM_MEMORY_ADDR_REL_SLEB:
  92. case R_WASM_MEMORY_ADDR_I32:
  93. case R_WASM_FUNCTION_OFFSET_I32:
  94. return reloc.Addend;
  95. case R_WASM_SECTION_OFFSET_I32:
  96. return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend;
  97. default:
  98. llvm_unreachable("unexpected relocation type");
  99. }
  100. }
  101. // Calculate the value we expect to find at the relocation location.
  102. // This is used as a sanity check before applying a relocation to a given
  103. // location. It is useful for catching bugs in the compiler and linker.
  104. uint32_t ObjFile::calcExpectedValue(const WasmRelocation &reloc) const {
  105. switch (reloc.Type) {
  106. case R_WASM_TABLE_INDEX_I32:
  107. case R_WASM_TABLE_INDEX_SLEB:
  108. case R_WASM_TABLE_INDEX_REL_SLEB: {
  109. const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
  110. return tableEntries[sym.Info.ElementIndex];
  111. }
  112. case R_WASM_MEMORY_ADDR_SLEB:
  113. case R_WASM_MEMORY_ADDR_I32:
  114. case R_WASM_MEMORY_ADDR_LEB:
  115. case R_WASM_MEMORY_ADDR_REL_SLEB: {
  116. const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
  117. if (sym.isUndefined())
  118. return 0;
  119. const WasmSegment &segment =
  120. wasmObj->dataSegments()[sym.Info.DataRef.Segment];
  121. return segment.Data.Offset.Value.Int32 + sym.Info.DataRef.Offset +
  122. reloc.Addend;
  123. }
  124. case R_WASM_FUNCTION_OFFSET_I32: {
  125. const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
  126. InputFunction *f =
  127. functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()];
  128. return f->getFunctionInputOffset() + f->getFunctionCodeOffset() +
  129. reloc.Addend;
  130. }
  131. case R_WASM_SECTION_OFFSET_I32:
  132. return reloc.Addend;
  133. case R_WASM_TYPE_INDEX_LEB:
  134. return reloc.Index;
  135. case R_WASM_FUNCTION_INDEX_LEB:
  136. case R_WASM_GLOBAL_INDEX_LEB:
  137. case R_WASM_EVENT_INDEX_LEB: {
  138. const WasmSymbol &sym = wasmObj->syms()[reloc.Index];
  139. return sym.Info.ElementIndex;
  140. }
  141. default:
  142. llvm_unreachable("unknown relocation type");
  143. }
  144. }
  145. // Translate from the relocation's index into the final linked output value.
  146. uint32_t ObjFile::calcNewValue(const WasmRelocation &reloc) const {
  147. const Symbol* sym = nullptr;
  148. if (reloc.Type != R_WASM_TYPE_INDEX_LEB) {
  149. sym = symbols[reloc.Index];
  150. // We can end up with relocations against non-live symbols. For example
  151. // in debug sections.
  152. if ((isa<FunctionSymbol>(sym) || isa<DataSymbol>(sym)) && !sym->isLive())
  153. return 0;
  154. }
  155. switch (reloc.Type) {
  156. case R_WASM_TABLE_INDEX_I32:
  157. case R_WASM_TABLE_INDEX_SLEB:
  158. case R_WASM_TABLE_INDEX_REL_SLEB: {
  159. if (!getFunctionSymbol(reloc.Index)->hasTableIndex())
  160. return 0;
  161. uint32_t index = getFunctionSymbol(reloc.Index)->getTableIndex();
  162. if (reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB)
  163. index -= config->tableBase;
  164. return index;
  165. }
  166. case R_WASM_MEMORY_ADDR_SLEB:
  167. case R_WASM_MEMORY_ADDR_I32:
  168. case R_WASM_MEMORY_ADDR_LEB:
  169. case R_WASM_MEMORY_ADDR_REL_SLEB:
  170. if (isa<UndefinedData>(sym) || sym->isUndefWeak())
  171. return 0;
  172. return cast<DefinedData>(sym)->getVirtualAddress() + reloc.Addend;
  173. case R_WASM_TYPE_INDEX_LEB:
  174. return typeMap[reloc.Index];
  175. case R_WASM_FUNCTION_INDEX_LEB:
  176. return getFunctionSymbol(reloc.Index)->getFunctionIndex();
  177. case R_WASM_GLOBAL_INDEX_LEB:
  178. if (auto gs = dyn_cast<GlobalSymbol>(sym))
  179. return gs->getGlobalIndex();
  180. return sym->getGOTIndex();
  181. case R_WASM_EVENT_INDEX_LEB:
  182. return getEventSymbol(reloc.Index)->getEventIndex();
  183. case R_WASM_FUNCTION_OFFSET_I32: {
  184. auto *f = cast<DefinedFunction>(sym);
  185. return f->function->outputOffset + f->function->getFunctionCodeOffset() +
  186. reloc.Addend;
  187. }
  188. case R_WASM_SECTION_OFFSET_I32:
  189. return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend;
  190. default:
  191. llvm_unreachable("unknown relocation type");
  192. }
  193. }
  194. template <class T>
  195. static void setRelocs(const std::vector<T *> &chunks,
  196. const WasmSection *section) {
  197. if (!section)
  198. return;
  199. ArrayRef<WasmRelocation> relocs = section->Relocations;
  200. assert(std::is_sorted(relocs.begin(), relocs.end(),
  201. [](const WasmRelocation &r1, const WasmRelocation &r2) {
  202. return r1.Offset < r2.Offset;
  203. }));
  204. assert(std::is_sorted(
  205. chunks.begin(), chunks.end(), [](InputChunk *c1, InputChunk *c2) {
  206. return c1->getInputSectionOffset() < c2->getInputSectionOffset();
  207. }));
  208. auto relocsNext = relocs.begin();
  209. auto relocsEnd = relocs.end();
  210. auto relocLess = [](const WasmRelocation &r, uint32_t val) {
  211. return r.Offset < val;
  212. };
  213. for (InputChunk *c : chunks) {
  214. auto relocsStart = std::lower_bound(relocsNext, relocsEnd,
  215. c->getInputSectionOffset(), relocLess);
  216. relocsNext = std::lower_bound(
  217. relocsStart, relocsEnd, c->getInputSectionOffset() + c->getInputSize(),
  218. relocLess);
  219. c->setRelocations(ArrayRef<WasmRelocation>(relocsStart, relocsNext));
  220. }
  221. }
  222. void ObjFile::parse(bool ignoreComdats) {
  223. // Parse a memory buffer as a wasm file.
  224. LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n");
  225. std::unique_ptr<Binary> bin = CHECK(createBinary(mb), toString(this));
  226. auto *obj = dyn_cast<WasmObjectFile>(bin.get());
  227. if (!obj)
  228. fatal(toString(this) + ": not a wasm file");
  229. if (!obj->isRelocatableObject())
  230. fatal(toString(this) + ": not a relocatable wasm file");
  231. bin.release();
  232. wasmObj.reset(obj);
  233. // Build up a map of function indices to table indices for use when
  234. // verifying the existing table index relocations
  235. uint32_t totalFunctions =
  236. wasmObj->getNumImportedFunctions() + wasmObj->functions().size();
  237. tableEntries.resize(totalFunctions);
  238. for (const WasmElemSegment &seg : wasmObj->elements()) {
  239. if (seg.Offset.Opcode != WASM_OPCODE_I32_CONST)
  240. fatal(toString(this) + ": invalid table elements");
  241. uint32_t offset = seg.Offset.Value.Int32;
  242. for (uint32_t index = 0; index < seg.Functions.size(); index++) {
  243. uint32_t functionIndex = seg.Functions[index];
  244. tableEntries[functionIndex] = offset + index;
  245. }
  246. }
  247. uint32_t sectionIndex = 0;
  248. // Bool for each symbol, true if called directly. This allows us to implement
  249. // a weaker form of signature checking where undefined functions that are not
  250. // called directly (i.e. only address taken) don't have to match the defined
  251. // function's signature. We cannot do this for directly called functions
  252. // because those signatures are checked at validation times.
  253. // See https://bugs.llvm.org/show_bug.cgi?id=40412
  254. std::vector<bool> isCalledDirectly(wasmObj->getNumberOfSymbols(), false);
  255. for (const SectionRef &sec : wasmObj->sections()) {
  256. const WasmSection &section = wasmObj->getWasmSection(sec);
  257. // Wasm objects can have at most one code and one data section.
  258. if (section.Type == WASM_SEC_CODE) {
  259. assert(!codeSection);
  260. codeSection = &section;
  261. } else if (section.Type == WASM_SEC_DATA) {
  262. assert(!dataSection);
  263. dataSection = &section;
  264. } else if (section.Type == WASM_SEC_CUSTOM) {
  265. customSections.emplace_back(make<InputSection>(section, this));
  266. customSections.back()->setRelocations(section.Relocations);
  267. customSectionsByIndex[sectionIndex] = customSections.back();
  268. }
  269. sectionIndex++;
  270. // Scans relocations to dermine determine if a function symbol is called
  271. // directly
  272. for (const WasmRelocation &reloc : section.Relocations)
  273. if (reloc.Type == R_WASM_FUNCTION_INDEX_LEB)
  274. isCalledDirectly[reloc.Index] = true;
  275. }
  276. typeMap.resize(getWasmObj()->types().size());
  277. typeIsUsed.resize(getWasmObj()->types().size(), false);
  278. ArrayRef<StringRef> comdats = wasmObj->linkingData().Comdats;
  279. for (StringRef comdat : comdats) {
  280. bool isNew = ignoreComdats || symtab->addComdat(comdat);
  281. keptComdats.push_back(isNew);
  282. }
  283. // Populate `Segments`.
  284. for (const WasmSegment &s : wasmObj->dataSegments()) {
  285. auto* seg = make<InputSegment>(s, this);
  286. seg->discarded = isExcludedByComdat(seg);
  287. segments.emplace_back(seg);
  288. }
  289. setRelocs(segments, dataSection);
  290. // Populate `Functions`.
  291. ArrayRef<WasmFunction> funcs = wasmObj->functions();
  292. ArrayRef<uint32_t> funcTypes = wasmObj->functionTypes();
  293. ArrayRef<WasmSignature> types = wasmObj->types();
  294. functions.reserve(funcs.size());
  295. for (size_t i = 0, e = funcs.size(); i != e; ++i) {
  296. auto* func = make<InputFunction>(types[funcTypes[i]], &funcs[i], this);
  297. func->discarded = isExcludedByComdat(func);
  298. functions.emplace_back(func);
  299. }
  300. setRelocs(functions, codeSection);
  301. // Populate `Globals`.
  302. for (const WasmGlobal &g : wasmObj->globals())
  303. globals.emplace_back(make<InputGlobal>(g, this));
  304. // Populate `Events`.
  305. for (const WasmEvent &e : wasmObj->events())
  306. events.emplace_back(make<InputEvent>(types[e.Type.SigIndex], e, this));
  307. // Populate `Symbols` based on the symbols in the object.
  308. symbols.reserve(wasmObj->getNumberOfSymbols());
  309. for (const SymbolRef &sym : wasmObj->symbols()) {
  310. const WasmSymbol &wasmSym = wasmObj->getWasmSymbol(sym.getRawDataRefImpl());
  311. if (wasmSym.isDefined()) {
  312. // createDefined may fail if the symbol is comdat excluded in which case
  313. // we fall back to creating an undefined symbol
  314. if (Symbol *d = createDefined(wasmSym)) {
  315. symbols.push_back(d);
  316. continue;
  317. }
  318. }
  319. size_t idx = symbols.size();
  320. symbols.push_back(createUndefined(wasmSym, isCalledDirectly[idx]));
  321. }
  322. }
  323. bool ObjFile::isExcludedByComdat(InputChunk *chunk) const {
  324. uint32_t c = chunk->getComdat();
  325. if (c == UINT32_MAX)
  326. return false;
  327. return !keptComdats[c];
  328. }
  329. FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t index) const {
  330. return cast<FunctionSymbol>(symbols[index]);
  331. }
  332. GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t index) const {
  333. return cast<GlobalSymbol>(symbols[index]);
  334. }
  335. EventSymbol *ObjFile::getEventSymbol(uint32_t index) const {
  336. return cast<EventSymbol>(symbols[index]);
  337. }
  338. SectionSymbol *ObjFile::getSectionSymbol(uint32_t index) const {
  339. return cast<SectionSymbol>(symbols[index]);
  340. }
  341. DataSymbol *ObjFile::getDataSymbol(uint32_t index) const {
  342. return cast<DataSymbol>(symbols[index]);
  343. }
  344. Symbol *ObjFile::createDefined(const WasmSymbol &sym) {
  345. StringRef name = sym.Info.Name;
  346. uint32_t flags = sym.Info.Flags;
  347. switch (sym.Info.Kind) {
  348. case WASM_SYMBOL_TYPE_FUNCTION: {
  349. InputFunction *func =
  350. functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()];
  351. if (sym.isBindingLocal())
  352. return make<DefinedFunction>(name, flags, this, func);
  353. if (func->discarded)
  354. return nullptr;
  355. return symtab->addDefinedFunction(name, flags, this, func);
  356. }
  357. case WASM_SYMBOL_TYPE_DATA: {
  358. InputSegment *seg = segments[sym.Info.DataRef.Segment];
  359. uint32_t offset = sym.Info.DataRef.Offset;
  360. uint32_t size = sym.Info.DataRef.Size;
  361. if (sym.isBindingLocal())
  362. return make<DefinedData>(name, flags, this, seg, offset, size);
  363. if (seg->discarded)
  364. return nullptr;
  365. return symtab->addDefinedData(name, flags, this, seg, offset, size);
  366. }
  367. case WASM_SYMBOL_TYPE_GLOBAL: {
  368. InputGlobal *global =
  369. globals[sym.Info.ElementIndex - wasmObj->getNumImportedGlobals()];
  370. if (sym.isBindingLocal())
  371. return make<DefinedGlobal>(name, flags, this, global);
  372. return symtab->addDefinedGlobal(name, flags, this, global);
  373. }
  374. case WASM_SYMBOL_TYPE_SECTION: {
  375. InputSection *section = customSectionsByIndex[sym.Info.ElementIndex];
  376. assert(sym.isBindingLocal());
  377. return make<SectionSymbol>(flags, section, this);
  378. }
  379. case WASM_SYMBOL_TYPE_EVENT: {
  380. InputEvent *event =
  381. events[sym.Info.ElementIndex - wasmObj->getNumImportedEvents()];
  382. if (sym.isBindingLocal())
  383. return make<DefinedEvent>(name, flags, this, event);
  384. return symtab->addDefinedEvent(name, flags, this, event);
  385. }
  386. }
  387. llvm_unreachable("unknown symbol kind");
  388. }
  389. Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) {
  390. StringRef name = sym.Info.Name;
  391. uint32_t flags = sym.Info.Flags | WASM_SYMBOL_UNDEFINED;
  392. switch (sym.Info.Kind) {
  393. case WASM_SYMBOL_TYPE_FUNCTION:
  394. if (sym.isBindingLocal())
  395. return make<UndefinedFunction>(name, sym.Info.ImportName,
  396. sym.Info.ImportModule, flags, this,
  397. sym.Signature, isCalledDirectly);
  398. return symtab->addUndefinedFunction(name, sym.Info.ImportName,
  399. sym.Info.ImportModule, flags, this,
  400. sym.Signature, isCalledDirectly);
  401. case WASM_SYMBOL_TYPE_DATA:
  402. if (sym.isBindingLocal())
  403. return make<UndefinedData>(name, flags, this);
  404. return symtab->addUndefinedData(name, flags, this);
  405. case WASM_SYMBOL_TYPE_GLOBAL:
  406. if (sym.isBindingLocal())
  407. return make<UndefinedGlobal>(name, sym.Info.ImportName,
  408. sym.Info.ImportModule, flags, this,
  409. sym.GlobalType);
  410. return symtab->addUndefinedGlobal(name, sym.Info.ImportName,
  411. sym.Info.ImportModule, flags, this,
  412. sym.GlobalType);
  413. case WASM_SYMBOL_TYPE_SECTION:
  414. llvm_unreachable("section symbols cannot be undefined");
  415. }
  416. llvm_unreachable("unknown symbol kind");
  417. }
  418. void ArchiveFile::parse() {
  419. // Parse a MemoryBufferRef as an archive file.
  420. LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");
  421. file = CHECK(Archive::create(mb), toString(this));
  422. // Read the symbol table to construct Lazy symbols.
  423. int count = 0;
  424. for (const Archive::Symbol &sym : file->symbols()) {
  425. symtab->addLazy(this, &sym);
  426. ++count;
  427. }
  428. LLVM_DEBUG(dbgs() << "Read " << count << " symbols\n");
  429. }
  430. void ArchiveFile::addMember(const Archive::Symbol *sym) {
  431. const Archive::Child &c =
  432. CHECK(sym->getMember(),
  433. "could not get the member for symbol " + sym->getName());
  434. // Don't try to load the same member twice (this can happen when members
  435. // mutually reference each other).
  436. if (!seen.insert(c.getChildOffset()).second)
  437. return;
  438. LLVM_DEBUG(dbgs() << "loading lazy: " << sym->getName() << "\n");
  439. LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n");
  440. MemoryBufferRef mb =
  441. CHECK(c.getMemoryBufferRef(),
  442. "could not get the buffer for the member defining symbol " +
  443. sym->getName());
  444. InputFile *obj = createObjectFile(mb, getName());
  445. symtab->addFile(obj);
  446. }
  447. static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {
  448. switch (gvVisibility) {
  449. case GlobalValue::DefaultVisibility:
  450. return WASM_SYMBOL_VISIBILITY_DEFAULT;
  451. case GlobalValue::HiddenVisibility:
  452. case GlobalValue::ProtectedVisibility:
  453. return WASM_SYMBOL_VISIBILITY_HIDDEN;
  454. }
  455. llvm_unreachable("unknown visibility");
  456. }
  457. static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats,
  458. const lto::InputFile::Symbol &objSym,
  459. BitcodeFile &f) {
  460. StringRef name = saver.save(objSym.getName());
  461. uint32_t flags = objSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0;
  462. flags |= mapVisibility(objSym.getVisibility());
  463. int c = objSym.getComdatIndex();
  464. bool excludedByComdat = c != -1 && !keptComdats[c];
  465. if (objSym.isUndefined() || excludedByComdat) {
  466. flags |= WASM_SYMBOL_UNDEFINED;
  467. if (objSym.isExecutable())
  468. return symtab->addUndefinedFunction(name, "", "", flags, &f, nullptr,
  469. true);
  470. return symtab->addUndefinedData(name, flags, &f);
  471. }
  472. if (objSym.isExecutable())
  473. return symtab->addDefinedFunction(name, flags, &f, nullptr);
  474. return symtab->addDefinedData(name, flags, &f, nullptr, 0, 0);
  475. }
  476. void BitcodeFile::parse() {
  477. obj = check(lto::InputFile::create(MemoryBufferRef(
  478. mb.getBuffer(), saver.save(archiveName + mb.getBufferIdentifier()))));
  479. Triple t(obj->getTargetTriple());
  480. if (t.getArch() != Triple::wasm32) {
  481. error(toString(mb.getBufferIdentifier()) + ": machine type must be wasm32");
  482. return;
  483. }
  484. std::vector<bool> keptComdats;
  485. for (StringRef s : obj->getComdatTable())
  486. keptComdats.push_back(symtab->addComdat(s));
  487. for (const lto::InputFile::Symbol &objSym : obj->symbols())
  488. symbols.push_back(createBitcodeSymbol(keptComdats, objSym, *this));
  489. }
  490. } // namespace wasm
  491. } // namespace lld