diff options
Diffstat (limited to 'lld/wasm/InputFiles.cpp')
-rw-r--r-- | lld/wasm/InputFiles.cpp | 562 |
1 files changed, 281 insertions, 281 deletions
diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp index b3e41a41b5f..b5233cf09ed 100644 --- a/lld/wasm/InputFiles.cpp +++ b/lld/wasm/InputFiles.cpp @@ -29,77 +29,77 @@ using namespace llvm; using namespace llvm::object; using namespace llvm::wasm; -std::unique_ptr<llvm::TarWriter> lld::wasm::Tar; +std::unique_ptr<llvm::TarWriter> lld::wasm::tar; -Optional<MemoryBufferRef> lld::wasm::readFile(StringRef Path) { - log("Loading: " + Path); +Optional<MemoryBufferRef> lld::wasm::readFile(StringRef path) { + log("Loading: " + path); - auto MBOrErr = MemoryBuffer::getFile(Path); - if (auto EC = MBOrErr.getError()) { - error("cannot open " + Path + ": " + EC.message()); + auto mbOrErr = MemoryBuffer::getFile(path); + if (auto ec = mbOrErr.getError()) { + error("cannot open " + path + ": " + ec.message()); return None; } - std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; - MemoryBufferRef MBRef = MB->getMemBufferRef(); - make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership + std::unique_ptr<MemoryBuffer> &mb = *mbOrErr; + MemoryBufferRef mbref = mb->getMemBufferRef(); + make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership - if (Tar) - Tar->append(relativeToRoot(Path), MBRef.getBuffer()); - return MBRef; + if (tar) + tar->append(relativeToRoot(path), mbref.getBuffer()); + return mbref; } -InputFile *lld::wasm::createObjectFile(MemoryBufferRef MB, - StringRef ArchiveName) { - file_magic Magic = identify_magic(MB.getBuffer()); - if (Magic == file_magic::wasm_object) { - std::unique_ptr<Binary> Bin = - CHECK(createBinary(MB), MB.getBufferIdentifier()); - auto *Obj = cast<WasmObjectFile>(Bin.get()); - if (Obj->isSharedObject()) - return make<SharedFile>(MB); - return make<ObjFile>(MB, ArchiveName); +InputFile *lld::wasm::createObjectFile(MemoryBufferRef mb, + StringRef archiveName) { + file_magic magic = identify_magic(mb.getBuffer()); + if (magic == file_magic::wasm_object) { + std::unique_ptr<Binary> bin = + CHECK(createBinary(mb), mb.getBufferIdentifier()); + auto *obj = cast<WasmObjectFile>(bin.get()); + if (obj->isSharedObject()) + return make<SharedFile>(mb); + return make<ObjFile>(mb, archiveName); } - if (Magic == file_magic::bitcode) - return make<BitcodeFile>(MB, ArchiveName); + if (magic == file_magic::bitcode) + return make<BitcodeFile>(mb, archiveName); - fatal("unknown file type: " + MB.getBufferIdentifier()); + fatal("unknown file type: " + mb.getBufferIdentifier()); } void ObjFile::dumpInfo() const { log("info for: " + toString(this) + - "\n Symbols : " + Twine(Symbols.size()) + - "\n Function Imports : " + Twine(WasmObj->getNumImportedFunctions()) + - "\n Global Imports : " + Twine(WasmObj->getNumImportedGlobals()) + - "\n Event Imports : " + Twine(WasmObj->getNumImportedEvents())); + "\n Symbols : " + Twine(symbols.size()) + + "\n Function Imports : " + Twine(wasmObj->getNumImportedFunctions()) + + "\n Global Imports : " + Twine(wasmObj->getNumImportedGlobals()) + + "\n Event Imports : " + Twine(wasmObj->getNumImportedEvents())); } // Relocations contain either symbol or type indices. This function takes a // relocation and returns relocated index (i.e. translates from the input // symbol/type space to the output symbol/type space). -uint32_t ObjFile::calcNewIndex(const WasmRelocation &Reloc) const { - if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) { - assert(TypeIsUsed[Reloc.Index]); - return TypeMap[Reloc.Index]; +uint32_t ObjFile::calcNewIndex(const WasmRelocation &reloc) const { + if (reloc.Type == R_WASM_TYPE_INDEX_LEB) { + assert(typeIsUsed[reloc.Index]); + return typeMap[reloc.Index]; } - const Symbol *Sym = Symbols[Reloc.Index]; - if (auto *SS = dyn_cast<SectionSymbol>(Sym)) - Sym = SS->getOutputSectionSymbol(); - return Sym->getOutputSymbolIndex(); + const Symbol *sym = symbols[reloc.Index]; + if (auto *ss = dyn_cast<SectionSymbol>(sym)) + sym = ss->getOutputSectionSymbol(); + return sym->getOutputSymbolIndex(); } // Relocations can contain addend for combined sections. This function takes a // relocation and returns updated addend by offset in the output section. -uint32_t ObjFile::calcNewAddend(const WasmRelocation &Reloc) const { - switch (Reloc.Type) { +uint32_t ObjFile::calcNewAddend(const WasmRelocation &reloc) const { + switch (reloc.Type) { case R_WASM_MEMORY_ADDR_LEB: case R_WASM_MEMORY_ADDR_SLEB: case R_WASM_MEMORY_ADDR_REL_SLEB: case R_WASM_MEMORY_ADDR_I32: case R_WASM_FUNCTION_OFFSET_I32: - return Reloc.Addend; + return reloc.Addend; case R_WASM_SECTION_OFFSET_I32: - return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend; + return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend; default: llvm_unreachable("unexpected relocation type"); } @@ -108,42 +108,42 @@ uint32_t ObjFile::calcNewAddend(const WasmRelocation &Reloc) const { // Calculate the value we expect to find at the relocation location. // This is used as a sanity check before applying a relocation to a given // location. It is useful for catching bugs in the compiler and linker. -uint32_t ObjFile::calcExpectedValue(const WasmRelocation &Reloc) const { - switch (Reloc.Type) { +uint32_t ObjFile::calcExpectedValue(const WasmRelocation &reloc) const { + switch (reloc.Type) { case R_WASM_TABLE_INDEX_I32: case R_WASM_TABLE_INDEX_SLEB: case R_WASM_TABLE_INDEX_REL_SLEB: { - const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; - return TableEntries[Sym.Info.ElementIndex]; + const WasmSymbol &sym = wasmObj->syms()[reloc.Index]; + return tableEntries[sym.Info.ElementIndex]; } case R_WASM_MEMORY_ADDR_SLEB: case R_WASM_MEMORY_ADDR_I32: case R_WASM_MEMORY_ADDR_LEB: case R_WASM_MEMORY_ADDR_REL_SLEB: { - const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; - if (Sym.isUndefined()) + const WasmSymbol &sym = wasmObj->syms()[reloc.Index]; + if (sym.isUndefined()) return 0; - const WasmSegment &Segment = - WasmObj->dataSegments()[Sym.Info.DataRef.Segment]; - return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset + - Reloc.Addend; + const WasmSegment &segment = + wasmObj->dataSegments()[sym.Info.DataRef.Segment]; + return segment.Data.Offset.Value.Int32 + sym.Info.DataRef.Offset + + reloc.Addend; } case R_WASM_FUNCTION_OFFSET_I32: { - const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; - InputFunction *F = - Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()]; - return F->getFunctionInputOffset() + F->getFunctionCodeOffset() + - Reloc.Addend; + const WasmSymbol &sym = wasmObj->syms()[reloc.Index]; + InputFunction *f = + functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()]; + return f->getFunctionInputOffset() + f->getFunctionCodeOffset() + + reloc.Addend; } case R_WASM_SECTION_OFFSET_I32: - return Reloc.Addend; + return reloc.Addend; case R_WASM_TYPE_INDEX_LEB: - return Reloc.Index; + return reloc.Index; case R_WASM_FUNCTION_INDEX_LEB: case R_WASM_GLOBAL_INDEX_LEB: case R_WASM_EVENT_INDEX_LEB: { - const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; - return Sym.Info.ElementIndex; + const WasmSymbol &sym = wasmObj->syms()[reloc.Index]; + return sym.Info.ElementIndex; } default: llvm_unreachable("unknown relocation type"); @@ -151,115 +151,115 @@ uint32_t ObjFile::calcExpectedValue(const WasmRelocation &Reloc) const { } // Translate from the relocation's index into the final linked output value. -uint32_t ObjFile::calcNewValue(const WasmRelocation &Reloc) const { - const Symbol* Sym = nullptr; - if (Reloc.Type != R_WASM_TYPE_INDEX_LEB) { - Sym = Symbols[Reloc.Index]; +uint32_t ObjFile::calcNewValue(const WasmRelocation &reloc) const { + const Symbol* sym = nullptr; + if (reloc.Type != R_WASM_TYPE_INDEX_LEB) { + sym = symbols[reloc.Index]; // We can end up with relocations against non-live symbols. For example // in debug sections. - if ((isa<FunctionSymbol>(Sym) || isa<DataSymbol>(Sym)) && !Sym->isLive()) + if ((isa<FunctionSymbol>(sym) || isa<DataSymbol>(sym)) && !sym->isLive()) return 0; } - switch (Reloc.Type) { + switch (reloc.Type) { case R_WASM_TABLE_INDEX_I32: case R_WASM_TABLE_INDEX_SLEB: case R_WASM_TABLE_INDEX_REL_SLEB: - if (Config->Pic && !getFunctionSymbol(Reloc.Index)->hasTableIndex()) + if (config->isPic && !getFunctionSymbol(reloc.Index)->hasTableIndex()) return 0; - return getFunctionSymbol(Reloc.Index)->getTableIndex(); + return getFunctionSymbol(reloc.Index)->getTableIndex(); case R_WASM_MEMORY_ADDR_SLEB: case R_WASM_MEMORY_ADDR_I32: case R_WASM_MEMORY_ADDR_LEB: case R_WASM_MEMORY_ADDR_REL_SLEB: - if (isa<UndefinedData>(Sym)) + if (isa<UndefinedData>(sym)) return 0; - return cast<DefinedData>(Sym)->getVirtualAddress() + Reloc.Addend; + return cast<DefinedData>(sym)->getVirtualAddress() + reloc.Addend; case R_WASM_TYPE_INDEX_LEB: - return TypeMap[Reloc.Index]; + return typeMap[reloc.Index]; case R_WASM_FUNCTION_INDEX_LEB: - return getFunctionSymbol(Reloc.Index)->getFunctionIndex(); + return getFunctionSymbol(reloc.Index)->getFunctionIndex(); case R_WASM_GLOBAL_INDEX_LEB: - if (auto GS = dyn_cast<GlobalSymbol>(Sym)) - return GS->getGlobalIndex(); - return Sym->getGOTIndex(); + if (auto gs = dyn_cast<GlobalSymbol>(sym)) + return gs->getGlobalIndex(); + return sym->getGOTIndex(); case R_WASM_EVENT_INDEX_LEB: - return getEventSymbol(Reloc.Index)->getEventIndex(); + return getEventSymbol(reloc.Index)->getEventIndex(); case R_WASM_FUNCTION_OFFSET_I32: { - auto *F = cast<DefinedFunction>(Sym); - return F->Function->OutputOffset + F->Function->getFunctionCodeOffset() + - Reloc.Addend; + auto *f = cast<DefinedFunction>(sym); + return f->function->outputOffset + f->function->getFunctionCodeOffset() + + reloc.Addend; } case R_WASM_SECTION_OFFSET_I32: - return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend; + return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend; default: llvm_unreachable("unknown relocation type"); } } template <class T> -static void setRelocs(const std::vector<T *> &Chunks, - const WasmSection *Section) { - if (!Section) +static void setRelocs(const std::vector<T *> &chunks, + const WasmSection *section) { + if (!section) return; - ArrayRef<WasmRelocation> Relocs = Section->Relocations; - assert(std::is_sorted(Relocs.begin(), Relocs.end(), - [](const WasmRelocation &R1, const WasmRelocation &R2) { - return R1.Offset < R2.Offset; + ArrayRef<WasmRelocation> relocs = section->Relocations; + assert(std::is_sorted(relocs.begin(), relocs.end(), + [](const WasmRelocation &r1, const WasmRelocation &r2) { + return r1.Offset < r2.Offset; })); assert(std::is_sorted( - Chunks.begin(), Chunks.end(), [](InputChunk *C1, InputChunk *C2) { - return C1->getInputSectionOffset() < C2->getInputSectionOffset(); + chunks.begin(), chunks.end(), [](InputChunk *c1, InputChunk *c2) { + return c1->getInputSectionOffset() < c2->getInputSectionOffset(); })); - auto RelocsNext = Relocs.begin(); - auto RelocsEnd = Relocs.end(); - auto RelocLess = [](const WasmRelocation &R, uint32_t Val) { - return R.Offset < Val; + auto relocsNext = relocs.begin(); + auto relocsEnd = relocs.end(); + auto relocLess = [](const WasmRelocation &r, uint32_t val) { + return r.Offset < val; }; - for (InputChunk *C : Chunks) { - auto RelocsStart = std::lower_bound(RelocsNext, RelocsEnd, - C->getInputSectionOffset(), RelocLess); - RelocsNext = std::lower_bound( - RelocsStart, RelocsEnd, C->getInputSectionOffset() + C->getInputSize(), - RelocLess); - C->setRelocations(ArrayRef<WasmRelocation>(RelocsStart, RelocsNext)); + for (InputChunk *c : chunks) { + auto relocsStart = std::lower_bound(relocsNext, relocsEnd, + c->getInputSectionOffset(), relocLess); + relocsNext = std::lower_bound( + relocsStart, relocsEnd, c->getInputSectionOffset() + c->getInputSize(), + relocLess); + c->setRelocations(ArrayRef<WasmRelocation>(relocsStart, relocsNext)); } } -void ObjFile::parse(bool IgnoreComdats) { +void ObjFile::parse(bool ignoreComdats) { // Parse a memory buffer as a wasm file. LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n"); - std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), toString(this)); + std::unique_ptr<Binary> bin = CHECK(createBinary(mb), toString(this)); - auto *Obj = dyn_cast<WasmObjectFile>(Bin.get()); - if (!Obj) + auto *obj = dyn_cast<WasmObjectFile>(bin.get()); + if (!obj) fatal(toString(this) + ": not a wasm file"); - if (!Obj->isRelocatableObject()) + if (!obj->isRelocatableObject()) fatal(toString(this) + ": not a relocatable wasm file"); - Bin.release(); - WasmObj.reset(Obj); + bin.release(); + wasmObj.reset(obj); // Build up a map of function indices to table indices for use when // verifying the existing table index relocations - uint32_t TotalFunctions = - WasmObj->getNumImportedFunctions() + WasmObj->functions().size(); - TableEntries.resize(TotalFunctions); - for (const WasmElemSegment &Seg : WasmObj->elements()) { - if (Seg.Offset.Opcode != WASM_OPCODE_I32_CONST) + uint32_t totalFunctions = + wasmObj->getNumImportedFunctions() + wasmObj->functions().size(); + tableEntries.resize(totalFunctions); + for (const WasmElemSegment &seg : wasmObj->elements()) { + if (seg.Offset.Opcode != WASM_OPCODE_I32_CONST) fatal(toString(this) + ": invalid table elements"); - uint32_t Offset = Seg.Offset.Value.Int32; - for (uint32_t Index = 0; Index < Seg.Functions.size(); Index++) { + uint32_t offset = seg.Offset.Value.Int32; + for (uint32_t index = 0; index < seg.Functions.size(); index++) { - uint32_t FunctionIndex = Seg.Functions[Index]; - TableEntries[FunctionIndex] = Offset + Index; + uint32_t functionIndex = seg.Functions[index]; + tableEntries[functionIndex] = offset + index; } } - uint32_t SectionIndex = 0; + uint32_t sectionIndex = 0; // Bool for each symbol, true if called directly. This allows us to implement // a weaker form of signature checking where undefined functions that are not @@ -267,185 +267,185 @@ void ObjFile::parse(bool IgnoreComdats) { // function's signature. We cannot do this for directly called functions // because those signatures are checked at validation times. // See https://bugs.llvm.org/show_bug.cgi?id=40412 - std::vector<bool> IsCalledDirectly(WasmObj->getNumberOfSymbols(), false); - for (const SectionRef &Sec : WasmObj->sections()) { - const WasmSection &Section = WasmObj->getWasmSection(Sec); + std::vector<bool> isCalledDirectly(wasmObj->getNumberOfSymbols(), false); + for (const SectionRef &sec : wasmObj->sections()) { + const WasmSection §ion = wasmObj->getWasmSection(sec); // Wasm objects can have at most one code and one data section. - if (Section.Type == WASM_SEC_CODE) { - assert(!CodeSection); - CodeSection = &Section; - } else if (Section.Type == WASM_SEC_DATA) { - assert(!DataSection); - DataSection = &Section; - } else if (Section.Type == WASM_SEC_CUSTOM) { - CustomSections.emplace_back(make<InputSection>(Section, this)); - CustomSections.back()->setRelocations(Section.Relocations); - CustomSectionsByIndex[SectionIndex] = CustomSections.back(); + if (section.Type == WASM_SEC_CODE) { + assert(!codeSection); + codeSection = §ion; + } else if (section.Type == WASM_SEC_DATA) { + assert(!dataSection); + dataSection = §ion; + } else if (section.Type == WASM_SEC_CUSTOM) { + customSections.emplace_back(make<InputSection>(section, this)); + customSections.back()->setRelocations(section.Relocations); + customSectionsByIndex[sectionIndex] = customSections.back(); } - SectionIndex++; + sectionIndex++; // Scans relocations to dermine determine if a function symbol is called // directly - for (const WasmRelocation &Reloc : Section.Relocations) - if (Reloc.Type == R_WASM_FUNCTION_INDEX_LEB) - IsCalledDirectly[Reloc.Index] = true; + for (const WasmRelocation &reloc : section.Relocations) + if (reloc.Type == R_WASM_FUNCTION_INDEX_LEB) + isCalledDirectly[reloc.Index] = true; } - TypeMap.resize(getWasmObj()->types().size()); - TypeIsUsed.resize(getWasmObj()->types().size(), false); + typeMap.resize(getWasmObj()->types().size()); + typeIsUsed.resize(getWasmObj()->types().size(), false); - ArrayRef<StringRef> Comdats = WasmObj->linkingData().Comdats; - for (StringRef Comdat : Comdats) { - bool IsNew = IgnoreComdats || Symtab->addComdat(Comdat); - KeptComdats.push_back(IsNew); + ArrayRef<StringRef> comdats = wasmObj->linkingData().Comdats; + for (StringRef comdat : comdats) { + bool isNew = ignoreComdats || symtab->addComdat(comdat); + keptComdats.push_back(isNew); } // Populate `Segments`. - for (const WasmSegment &S : WasmObj->dataSegments()) { - auto* Seg = make<InputSegment>(S, this); - Seg->Discarded = isExcludedByComdat(Seg); - Segments.emplace_back(Seg); + for (const WasmSegment &s : wasmObj->dataSegments()) { + auto* seg = make<InputSegment>(s, this); + seg->discarded = isExcludedByComdat(seg); + segments.emplace_back(seg); } - setRelocs(Segments, DataSection); + setRelocs(segments, dataSection); // Populate `Functions`. - ArrayRef<WasmFunction> Funcs = WasmObj->functions(); - ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes(); - ArrayRef<WasmSignature> Types = WasmObj->types(); - Functions.reserve(Funcs.size()); - - for (size_t I = 0, E = Funcs.size(); I != E; ++I) { - auto* Func = make<InputFunction>(Types[FuncTypes[I]], &Funcs[I], this); - Func->Discarded = isExcludedByComdat(Func); - Functions.emplace_back(Func); + ArrayRef<WasmFunction> funcs = wasmObj->functions(); + ArrayRef<uint32_t> funcTypes = wasmObj->functionTypes(); + ArrayRef<WasmSignature> types = wasmObj->types(); + functions.reserve(funcs.size()); + + for (size_t i = 0, e = funcs.size(); i != e; ++i) { + auto* func = make<InputFunction>(types[funcTypes[i]], &funcs[i], this); + func->discarded = isExcludedByComdat(func); + functions.emplace_back(func); } - setRelocs(Functions, CodeSection); + setRelocs(functions, codeSection); // Populate `Globals`. - for (const WasmGlobal &G : WasmObj->globals()) - Globals.emplace_back(make<InputGlobal>(G, this)); + for (const WasmGlobal &g : wasmObj->globals()) + globals.emplace_back(make<InputGlobal>(g, this)); // Populate `Events`. - for (const WasmEvent &E : WasmObj->events()) - Events.emplace_back(make<InputEvent>(Types[E.Type.SigIndex], E, this)); + for (const WasmEvent &e : wasmObj->events()) + events.emplace_back(make<InputEvent>(types[e.Type.SigIndex], e, this)); // Populate `Symbols` based on the WasmSymbols in the object. - Symbols.reserve(WasmObj->getNumberOfSymbols()); - for (const SymbolRef &Sym : WasmObj->symbols()) { - const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl()); - if (WasmSym.isDefined()) { + symbols.reserve(wasmObj->getNumberOfSymbols()); + for (const SymbolRef &sym : wasmObj->symbols()) { + const WasmSymbol &wasmSym = wasmObj->getWasmSymbol(sym.getRawDataRefImpl()); + if (wasmSym.isDefined()) { // createDefined may fail if the symbol is comdat excluded in which case // we fall back to creating an undefined symbol - if (Symbol *D = createDefined(WasmSym)) { - Symbols.push_back(D); + if (Symbol *d = createDefined(wasmSym)) { + symbols.push_back(d); continue; } } - size_t Idx = Symbols.size(); - Symbols.push_back(createUndefined(WasmSym, IsCalledDirectly[Idx])); + size_t idx = symbols.size(); + symbols.push_back(createUndefined(wasmSym, isCalledDirectly[idx])); } } -bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const { - uint32_t C = Chunk->getComdat(); - if (C == UINT32_MAX) +bool ObjFile::isExcludedByComdat(InputChunk *chunk) const { + uint32_t c = chunk->getComdat(); + if (c == UINT32_MAX) return false; - return !KeptComdats[C]; + return !keptComdats[c]; } -FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const { - return cast<FunctionSymbol>(Symbols[Index]); +FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t index) const { + return cast<FunctionSymbol>(symbols[index]); } -GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const { - return cast<GlobalSymbol>(Symbols[Index]); +GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t index) const { + return cast<GlobalSymbol>(symbols[index]); } -EventSymbol *ObjFile::getEventSymbol(uint32_t Index) const { - return cast<EventSymbol>(Symbols[Index]); +EventSymbol *ObjFile::getEventSymbol(uint32_t index) const { + return cast<EventSymbol>(symbols[index]); } -SectionSymbol *ObjFile::getSectionSymbol(uint32_t Index) const { - return cast<SectionSymbol>(Symbols[Index]); +SectionSymbol *ObjFile::getSectionSymbol(uint32_t index) const { + return cast<SectionSymbol>(symbols[index]); } -DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const { - return cast<DataSymbol>(Symbols[Index]); +DataSymbol *ObjFile::getDataSymbol(uint32_t index) const { + return cast<DataSymbol>(symbols[index]); } -Symbol *ObjFile::createDefined(const WasmSymbol &Sym) { - StringRef Name = Sym.Info.Name; - uint32_t Flags = Sym.Info.Flags; +Symbol *ObjFile::createDefined(const WasmSymbol &sym) { + StringRef name = sym.Info.Name; + uint32_t flags = sym.Info.Flags; - switch (Sym.Info.Kind) { + switch (sym.Info.Kind) { case WASM_SYMBOL_TYPE_FUNCTION: { - InputFunction *Func = - Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()]; - if (Func->Discarded) + InputFunction *func = + functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()]; + if (func->discarded) return nullptr; - if (Sym.isBindingLocal()) - return make<DefinedFunction>(Name, Flags, this, Func); - return Symtab->addDefinedFunction(Name, Flags, this, Func); + if (sym.isBindingLocal()) + return make<DefinedFunction>(name, flags, this, func); + return symtab->addDefinedFunction(name, flags, this, func); } case WASM_SYMBOL_TYPE_DATA: { - InputSegment *Seg = Segments[Sym.Info.DataRef.Segment]; - if (Seg->Discarded) + InputSegment *seg = segments[sym.Info.DataRef.Segment]; + if (seg->discarded) return nullptr; - uint32_t Offset = Sym.Info.DataRef.Offset; - uint32_t Size = Sym.Info.DataRef.Size; + uint32_t offset = sym.Info.DataRef.Offset; + uint32_t size = sym.Info.DataRef.Size; - if (Sym.isBindingLocal()) - return make<DefinedData>(Name, Flags, this, Seg, Offset, Size); - return Symtab->addDefinedData(Name, Flags, this, Seg, Offset, Size); + if (sym.isBindingLocal()) + return make<DefinedData>(name, flags, this, seg, offset, size); + return symtab->addDefinedData(name, flags, this, seg, offset, size); } case WASM_SYMBOL_TYPE_GLOBAL: { - InputGlobal *Global = - Globals[Sym.Info.ElementIndex - WasmObj->getNumImportedGlobals()]; - if (Sym.isBindingLocal()) - return make<DefinedGlobal>(Name, Flags, this, Global); - return Symtab->addDefinedGlobal(Name, Flags, this, Global); + InputGlobal *global = + globals[sym.Info.ElementIndex - wasmObj->getNumImportedGlobals()]; + if (sym.isBindingLocal()) + return make<DefinedGlobal>(name, flags, this, global); + return symtab->addDefinedGlobal(name, flags, this, global); } case WASM_SYMBOL_TYPE_SECTION: { - InputSection *Section = CustomSectionsByIndex[Sym.Info.ElementIndex]; - assert(Sym.isBindingLocal()); - return make<SectionSymbol>(Flags, Section, this); + InputSection *section = customSectionsByIndex[sym.Info.ElementIndex]; + assert(sym.isBindingLocal()); + return make<SectionSymbol>(flags, section, this); } case WASM_SYMBOL_TYPE_EVENT: { - InputEvent *Event = - Events[Sym.Info.ElementIndex - WasmObj->getNumImportedEvents()]; - if (Sym.isBindingLocal()) - return make<DefinedEvent>(Name, Flags, this, Event); - return Symtab->addDefinedEvent(Name, Flags, this, Event); + InputEvent *event = + events[sym.Info.ElementIndex - wasmObj->getNumImportedEvents()]; + if (sym.isBindingLocal()) + return make<DefinedEvent>(name, flags, this, event); + return symtab->addDefinedEvent(name, flags, this, event); } } llvm_unreachable("unknown symbol kind"); } -Symbol *ObjFile::createUndefined(const WasmSymbol &Sym, bool IsCalledDirectly) { - StringRef Name = Sym.Info.Name; - uint32_t Flags = Sym.Info.Flags; +Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) { + StringRef name = sym.Info.Name; + uint32_t flags = sym.Info.Flags; - switch (Sym.Info.Kind) { + switch (sym.Info.Kind) { case WASM_SYMBOL_TYPE_FUNCTION: - if (Sym.isBindingLocal()) - return make<UndefinedFunction>(Name, Sym.Info.ImportName, - Sym.Info.ImportModule, Flags, this, - Sym.Signature, IsCalledDirectly); - return Symtab->addUndefinedFunction(Name, Sym.Info.ImportName, - Sym.Info.ImportModule, Flags, this, - Sym.Signature, IsCalledDirectly); + if (sym.isBindingLocal()) + return make<UndefinedFunction>(name, sym.Info.ImportName, + sym.Info.ImportModule, flags, this, + sym.Signature, isCalledDirectly); + return symtab->addUndefinedFunction(name, sym.Info.ImportName, + sym.Info.ImportModule, flags, this, + sym.Signature, isCalledDirectly); case WASM_SYMBOL_TYPE_DATA: - if (Sym.isBindingLocal()) - return make<UndefinedData>(Name, Flags, this); - return Symtab->addUndefinedData(Name, Flags, this); + if (sym.isBindingLocal()) + return make<UndefinedData>(name, flags, this); + return symtab->addUndefinedData(name, flags, this); case WASM_SYMBOL_TYPE_GLOBAL: - if (Sym.isBindingLocal()) - return make<UndefinedGlobal>(Name, Sym.Info.ImportName, - Sym.Info.ImportModule, Flags, this, - Sym.GlobalType); - return Symtab->addUndefinedGlobal(Name, Sym.Info.ImportName, - Sym.Info.ImportModule, Flags, this, - Sym.GlobalType); + if (sym.isBindingLocal()) + return make<UndefinedGlobal>(name, sym.Info.ImportName, + sym.Info.ImportModule, flags, this, + sym.GlobalType); + return symtab->addUndefinedGlobal(name, sym.Info.ImportName, + sym.Info.ImportModule, flags, this, + sym.GlobalType); case WASM_SYMBOL_TYPE_SECTION: llvm_unreachable("section symbols cannot be undefined"); } @@ -455,41 +455,41 @@ Symbol *ObjFile::createUndefined(const WasmSymbol &Sym, bool IsCalledDirectly) { void ArchiveFile::parse() { // Parse a MemoryBufferRef as an archive file. LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n"); - File = CHECK(Archive::create(MB), toString(this)); + file = CHECK(Archive::create(mb), toString(this)); // Read the symbol table to construct Lazy symbols. - int Count = 0; - for (const Archive::Symbol &Sym : File->symbols()) { - Symtab->addLazy(this, &Sym); - ++Count; + int count = 0; + for (const Archive::Symbol &sym : file->symbols()) { + symtab->addLazy(this, &sym); + ++count; } - LLVM_DEBUG(dbgs() << "Read " << Count << " symbols\n"); + LLVM_DEBUG(dbgs() << "Read " << count << " symbols\n"); } -void ArchiveFile::addMember(const Archive::Symbol *Sym) { - const Archive::Child &C = - CHECK(Sym->getMember(), - "could not get the member for symbol " + Sym->getName()); +void ArchiveFile::addMember(const Archive::Symbol *sym) { + const Archive::Child &c = + CHECK(sym->getMember(), + "could not get the member for symbol " + sym->getName()); // Don't try to load the same member twice (this can happen when members // mutually reference each other). - if (!Seen.insert(C.getChildOffset()).second) + if (!seen.insert(c.getChildOffset()).second) return; - LLVM_DEBUG(dbgs() << "loading lazy: " << Sym->getName() << "\n"); + LLVM_DEBUG(dbgs() << "loading lazy: " << sym->getName() << "\n"); LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n"); - MemoryBufferRef MB = - CHECK(C.getMemoryBufferRef(), + MemoryBufferRef mb = + CHECK(c.getMemoryBufferRef(), "could not get the buffer for the member defining symbol " + - Sym->getName()); + sym->getName()); - InputFile *Obj = createObjectFile(MB, getName()); - Symtab->addFile(Obj); + InputFile *obj = createObjectFile(mb, getName()); + symtab->addFile(obj); } -static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) { - switch (GvVisibility) { +static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) { + switch (gvVisibility) { case GlobalValue::DefaultVisibility: return WASM_SYMBOL_VISIBILITY_DEFAULT; case GlobalValue::HiddenVisibility: @@ -499,52 +499,52 @@ static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) { llvm_unreachable("unknown visibility"); } -static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats, - const lto::InputFile::Symbol &ObjSym, - BitcodeFile &F) { - StringRef Name = Saver.save(ObjSym.getName()); +static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats, + const lto::InputFile::Symbol &objSym, + BitcodeFile &f) { + StringRef name = saver.save(objSym.getName()); - uint32_t Flags = ObjSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0; - Flags |= mapVisibility(ObjSym.getVisibility()); + uint32_t flags = objSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0; + flags |= mapVisibility(objSym.getVisibility()); - int C = ObjSym.getComdatIndex(); - bool ExcludedByComdat = C != -1 && !KeptComdats[C]; + int c = objSym.getComdatIndex(); + bool excludedByComdat = c != -1 && !keptComdats[c]; - if (ObjSym.isUndefined() || ExcludedByComdat) { - if (ObjSym.isExecutable()) - return Symtab->addUndefinedFunction(Name, Name, DefaultModule, Flags, &F, + if (objSym.isUndefined() || excludedByComdat) { + if (objSym.isExecutable()) + return symtab->addUndefinedFunction(name, name, defaultModule, flags, &f, nullptr, true); - return Symtab->addUndefinedData(Name, Flags, &F); + return symtab->addUndefinedData(name, flags, &f); } - if (ObjSym.isExecutable()) - return Symtab->addDefinedFunction(Name, Flags, &F, nullptr); - return Symtab->addDefinedData(Name, Flags, &F, nullptr, 0, 0); + if (objSym.isExecutable()) + return symtab->addDefinedFunction(name, flags, &f, nullptr); + return symtab->addDefinedData(name, flags, &f, nullptr, 0, 0); } void BitcodeFile::parse() { - Obj = check(lto::InputFile::create(MemoryBufferRef( - MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier())))); - Triple T(Obj->getTargetTriple()); - if (T.getArch() != Triple::wasm32) { - error(toString(MB.getBufferIdentifier()) + ": machine type must be wasm32"); + obj = check(lto::InputFile::create(MemoryBufferRef( + mb.getBuffer(), saver.save(archiveName + mb.getBufferIdentifier())))); + Triple t(obj->getTargetTriple()); + if (t.getArch() != Triple::wasm32) { + error(toString(mb.getBufferIdentifier()) + ": machine type must be wasm32"); return; } - std::vector<bool> KeptComdats; - for (StringRef S : Obj->getComdatTable()) - KeptComdats.push_back(Symtab->addComdat(S)); + std::vector<bool> keptComdats; + for (StringRef s : obj->getComdatTable()) + keptComdats.push_back(symtab->addComdat(s)); - for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) - Symbols.push_back(createBitcodeSymbol(KeptComdats, ObjSym, *this)); + for (const lto::InputFile::Symbol &objSym : obj->symbols()) + symbols.push_back(createBitcodeSymbol(keptComdats, objSym, *this)); } // Returns a string in the format of "foo.o" or "foo.a(bar.o)". -std::string lld::toString(const wasm::InputFile *File) { - if (!File) +std::string lld::toString(const wasm::InputFile *file) { + if (!file) return "<internal>"; - if (File->ArchiveName.empty()) - return File->getName(); + if (file->archiveName.empty()) + return file->getName(); - return (File->ArchiveName + "(" + File->getName() + ")").str(); + return (file->archiveName + "(" + file->getName() + ")").str(); } |