2017-11-18 02:14:09 +08:00
|
|
|
//===- InputFiles.cpp -----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Config.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.h"
|
2018-12-08 14:17:43 +08:00
|
|
|
#include "InputEvent.h"
|
2018-02-23 13:08:53 +08:00
|
|
|
#include "InputGlobal.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2019-05-21 19:52:14 +08:00
|
|
|
#include "lld/Common/Reproduce.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
2019-05-21 19:52:14 +08:00
|
|
|
#include "llvm/Support/TarWriter.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::wasm;
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
|
2019-05-21 19:52:14 +08:00
|
|
|
std::unique_ptr<llvm::TarWriter> lld::wasm::Tar;
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
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());
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
|
|
|
|
MemoryBufferRef MBRef = MB->getMemBufferRef();
|
|
|
|
make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
|
|
|
|
|
2019-05-21 19:52:14 +08:00
|
|
|
if (Tar)
|
|
|
|
Tar->append(relativeToRoot(Path), MBRef.getBuffer());
|
2017-11-18 02:14:09 +08:00
|
|
|
return MBRef;
|
|
|
|
}
|
|
|
|
|
2019-04-09 13:41:52 +08:00
|
|
|
InputFile *lld::wasm::createObjectFile(MemoryBufferRef MB,
|
|
|
|
StringRef ArchiveName) {
|
2018-07-24 07:51:19 +08:00
|
|
|
file_magic Magic = identify_magic(MB.getBuffer());
|
2019-03-14 05:29:20 +08:00
|
|
|
if (Magic == file_magic::wasm_object) {
|
2019-06-26 08:52:46 +08:00
|
|
|
std::unique_ptr<Binary> Bin =
|
|
|
|
CHECK(createBinary(MB), MB.getBufferIdentifier());
|
2019-03-14 05:29:20 +08:00
|
|
|
auto *Obj = cast<WasmObjectFile>(Bin.get());
|
|
|
|
if (Obj->isSharedObject())
|
|
|
|
return make<SharedFile>(MB);
|
2019-04-09 13:41:52 +08:00
|
|
|
return make<ObjFile>(MB, ArchiveName);
|
2019-03-14 05:29:20 +08:00
|
|
|
}
|
2018-07-24 07:51:19 +08:00
|
|
|
|
|
|
|
if (Magic == file_magic::bitcode)
|
2019-04-09 13:41:52 +08:00
|
|
|
return make<BitcodeFile>(MB, ArchiveName);
|
2018-07-24 07:51:19 +08:00
|
|
|
|
|
|
|
fatal("unknown file type: " + MB.getBufferIdentifier());
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void ObjFile::dumpInfo() const {
|
2019-05-16 00:03:28 +08:00
|
|
|
log("info for: " + toString(this) +
|
2018-02-28 10:57:37 +08:00
|
|
|
"\n Symbols : " + Twine(Symbols.size()) +
|
|
|
|
"\n Function Imports : " + Twine(WasmObj->getNumImportedFunctions()) +
|
2018-12-08 14:17:43 +08:00
|
|
|
"\n Global Imports : " + Twine(WasmObj->getNumImportedGlobals()) +
|
|
|
|
"\n Event Imports : " + Twine(WasmObj->getNumImportedEvents()));
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-03-11 09:35:02 +08:00
|
|
|
// Relocations contain either symbol or type indices. This function takes a
|
|
|
|
// relocation and returns relocated index (i.e. translates from the input
|
2018-11-20 07:31:28 +08:00
|
|
|
// symbol/type space to the output symbol/type space).
|
2018-01-11 03:22:42 +08:00
|
|
|
uint32_t ObjFile::calcNewIndex(const WasmRelocation &Reloc) const {
|
2019-02-05 01:49:33 +08:00
|
|
|
if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) {
|
2018-02-28 08:26:26 +08:00
|
|
|
assert(TypeIsUsed[Reloc.Index]);
|
|
|
|
return TypeMap[Reloc.Index];
|
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
const Symbol *Sym = Symbols[Reloc.Index];
|
|
|
|
if (auto *SS = dyn_cast<SectionSymbol>(Sym))
|
|
|
|
Sym = SS->getOutputSectionSymbol();
|
|
|
|
return Sym->getOutputSymbolIndex();
|
2018-01-11 03:22:42 +08:00
|
|
|
}
|
|
|
|
|
2018-05-05 07:14:42 +08:00
|
|
|
// 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) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
2019-06-26 09:26:53 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
|
|
|
case R_WASM_FUNCTION_OFFSET_I32:
|
2018-05-05 07:14:42 +08:00
|
|
|
return Reloc.Addend;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_SECTION_OFFSET_I32:
|
2018-05-05 07:14:42 +08:00
|
|
|
return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 03:54:26 +08:00
|
|
|
// 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) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TABLE_INDEX_I32:
|
2019-04-05 01:43:50 +08:00
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB: {
|
2018-08-30 05:03:16 +08:00
|
|
|
const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index];
|
2018-03-13 03:54:26 +08:00
|
|
|
return TableEntries[Sym.Info.ElementIndex];
|
|
|
|
}
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
2019-04-05 01:43:50 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB: {
|
2018-08-30 05:03:16 +08:00
|
|
|
const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index];
|
2018-03-13 03:54:26 +08:00
|
|
|
if (Sym.isUndefined())
|
|
|
|
return 0;
|
2018-08-30 05:03:16 +08:00
|
|
|
const WasmSegment &Segment =
|
|
|
|
WasmObj->dataSegments()[Sym.Info.DataRef.Segment];
|
2018-03-14 08:53:34 +08:00
|
|
|
return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset +
|
|
|
|
Reloc.Addend;
|
2018-03-13 03:54:26 +08:00
|
|
|
}
|
2019-04-22 13:26:44 +08:00
|
|
|
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;
|
|
|
|
}
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_SECTION_OFFSET_I32:
|
2018-05-05 07:14:42 +08:00
|
|
|
return Reloc.Addend;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TYPE_INDEX_LEB:
|
2018-03-13 03:54:26 +08:00
|
|
|
return Reloc.Index;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_FUNCTION_INDEX_LEB:
|
|
|
|
case R_WASM_GLOBAL_INDEX_LEB:
|
|
|
|
case R_WASM_EVENT_INDEX_LEB: {
|
2018-08-30 05:03:16 +08:00
|
|
|
const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index];
|
2018-03-13 03:54:26 +08:00
|
|
|
return Sym.Info.ElementIndex;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
// Translate from the relocation's index into the final linked output value.
|
|
|
|
uint32_t ObjFile::calcNewValue(const WasmRelocation &Reloc) const {
|
2019-04-23 00:12:54 +08:00
|
|
|
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())
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
switch (Reloc.Type) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TABLE_INDEX_I32:
|
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
2019-04-05 01:43:50 +08:00
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB:
|
2019-05-10 09:52:08 +08:00
|
|
|
if (Config->Pic && !getFunctionSymbol(Reloc.Index)->hasTableIndex())
|
|
|
|
return 0;
|
2018-03-10 01:06:38 +08:00
|
|
|
return getFunctionSymbol(Reloc.Index)->getTableIndex();
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
2019-04-05 01:43:50 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB:
|
2019-07-10 04:45:20 +08:00
|
|
|
if (isa<UndefinedData>(Sym))
|
|
|
|
return 0;
|
2019-04-23 00:12:54 +08:00
|
|
|
return cast<DefinedData>(Sym)->getVirtualAddress() + Reloc.Addend;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TYPE_INDEX_LEB:
|
2018-02-28 08:26:26 +08:00
|
|
|
return TypeMap[Reloc.Index];
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_FUNCTION_INDEX_LEB:
|
2018-03-13 03:56:23 +08:00
|
|
|
return getFunctionSymbol(Reloc.Index)->getFunctionIndex();
|
2019-04-23 00:12:54 +08:00
|
|
|
case R_WASM_GLOBAL_INDEX_LEB:
|
2019-03-27 03:46:15 +08:00
|
|
|
if (auto GS = dyn_cast<GlobalSymbol>(Sym))
|
|
|
|
return GS->getGlobalIndex();
|
|
|
|
return Sym->getGOTIndex();
|
2019-04-23 00:12:54 +08:00
|
|
|
case R_WASM_EVENT_INDEX_LEB:
|
2018-12-08 14:17:43 +08:00
|
|
|
return getEventSymbol(Reloc.Index)->getEventIndex();
|
2019-04-23 00:12:54 +08:00
|
|
|
case R_WASM_FUNCTION_OFFSET_I32: {
|
|
|
|
auto *F = cast<DefinedFunction>(Sym);
|
|
|
|
return F->Function->OutputOffset + F->Function->getFunctionCodeOffset() +
|
|
|
|
Reloc.Addend;
|
|
|
|
}
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_SECTION_OFFSET_I32:
|
2018-05-05 07:14:42 +08:00
|
|
|
return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend;
|
2018-01-23 09:25:56 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 01:50:51 +08:00
|
|
|
template <class T>
|
|
|
|
static void setRelocs(const std::vector<T *> &Chunks,
|
|
|
|
const WasmSection *Section) {
|
|
|
|
if (!Section)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ArrayRef<WasmRelocation> Relocs = Section->Relocations;
|
2018-08-30 05:03:16 +08:00
|
|
|
assert(std::is_sorted(Relocs.begin(), Relocs.end(),
|
|
|
|
[](const WasmRelocation &R1, const WasmRelocation &R2) {
|
|
|
|
return R1.Offset < R2.Offset;
|
|
|
|
}));
|
2018-08-23 01:50:51 +08:00
|
|
|
assert(std::is_sorted(
|
2018-08-30 05:03:16 +08:00
|
|
|
Chunks.begin(), Chunks.end(), [](InputChunk *C1, InputChunk *C2) {
|
2018-08-23 01:50:51 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
for (InputChunk *C : Chunks) {
|
|
|
|
auto RelocsStart = std::lower_bound(RelocsNext, RelocsEnd,
|
|
|
|
C->getInputSectionOffset(), RelocLess);
|
2018-08-30 05:03:16 +08:00
|
|
|
RelocsNext = std::lower_bound(
|
|
|
|
RelocsStart, RelocsEnd, C->getInputSectionOffset() + C->getInputSize(),
|
|
|
|
RelocLess);
|
2018-08-23 01:50:51 +08:00
|
|
|
C->setRelocations(ArrayRef<WasmRelocation>(RelocsStart, RelocsNext));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 00:03:28 +08:00
|
|
|
void ObjFile::parse(bool IgnoreComdats) {
|
2017-11-18 02:14:09 +08:00
|
|
|
// Parse a memory buffer as a wasm file.
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n");
|
2017-12-07 06:08:17 +08:00
|
|
|
std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), toString(this));
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
auto *Obj = dyn_cast<WasmObjectFile>(Bin.get());
|
|
|
|
if (!Obj)
|
|
|
|
fatal(toString(this) + ": not a wasm file");
|
|
|
|
if (!Obj->isRelocatableObject())
|
|
|
|
fatal(toString(this) + ": not a relocatable wasm file");
|
|
|
|
|
|
|
|
Bin.release();
|
|
|
|
WasmObj.reset(Obj);
|
|
|
|
|
2018-03-13 03:54:26 +08:00
|
|
|
// 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)
|
|
|
|
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 FunctionIndex = Seg.Functions[Index];
|
|
|
|
TableEntries[FunctionIndex] = Offset + Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 07:14:42 +08:00
|
|
|
uint32_t SectionIndex = 0;
|
2019-05-29 23:41:08 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// called directly (i.e. only address taken) don't have to match the defined
|
|
|
|
// 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);
|
2017-11-18 02:14:09 +08:00
|
|
|
for (const SectionRef &Sec : WasmObj->sections()) {
|
|
|
|
const WasmSection &Section = WasmObj->getWasmSection(Sec);
|
2019-05-25 06:45:08 +08:00
|
|
|
// Wasm objects can have at most one code and one data section.
|
2018-05-05 07:14:42 +08:00
|
|
|
if (Section.Type == WASM_SEC_CODE) {
|
2019-05-25 06:45:08 +08:00
|
|
|
assert(!CodeSection);
|
2017-11-18 02:14:09 +08:00
|
|
|
CodeSection = &Section;
|
2018-05-05 07:14:42 +08:00
|
|
|
} else if (Section.Type == WASM_SEC_DATA) {
|
2019-05-25 06:45:08 +08:00
|
|
|
assert(!DataSection);
|
2017-11-18 02:14:09 +08:00
|
|
|
DataSection = &Section;
|
2018-05-05 07:14:42 +08:00
|
|
|
} else if (Section.Type == WASM_SEC_CUSTOM) {
|
2018-04-11 00:12:49 +08:00
|
|
|
CustomSections.emplace_back(make<InputSection>(Section, this));
|
2018-08-23 01:50:51 +08:00
|
|
|
CustomSections.back()->setRelocations(Section.Relocations);
|
2018-05-05 07:14:42 +08:00
|
|
|
CustomSectionsByIndex[SectionIndex] = CustomSections.back();
|
|
|
|
}
|
|
|
|
SectionIndex++;
|
2019-05-25 06:45:08 +08:00
|
|
|
// 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)
|
2019-05-29 23:41:08 +08:00
|
|
|
IsCalledDirectly[Reloc.Index] = true;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-01 07:48:14 +08:00
|
|
|
TypeMap.resize(getWasmObj()->types().size());
|
|
|
|
TypeIsUsed.resize(getWasmObj()->types().size(), false);
|
|
|
|
|
2018-03-14 23:45:11 +08:00
|
|
|
ArrayRef<StringRef> Comdats = WasmObj->linkingData().Comdats;
|
2019-06-07 14:00:46 +08:00
|
|
|
for (StringRef Comdat : Comdats) {
|
|
|
|
bool IsNew = IgnoreComdats || Symtab->addComdat(Comdat);
|
2019-06-06 01:39:37 +08:00
|
|
|
KeptComdats.push_back(IsNew);
|
|
|
|
}
|
2018-03-14 23:45:11 +08:00
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
// Populate `Segments`.
|
2019-06-07 14:00:46 +08:00
|
|
|
for (const WasmSegment &S : WasmObj->dataSegments()) {
|
|
|
|
auto* Seg = make<InputSegment>(S, this);
|
|
|
|
Seg->Discarded = isExcludedByComdat(Seg);
|
|
|
|
Segments.emplace_back(Seg);
|
|
|
|
}
|
2018-08-23 01:50:51 +08:00
|
|
|
setRelocs(Segments, DataSection);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
// Populate `Functions`.
|
|
|
|
ArrayRef<WasmFunction> Funcs = WasmObj->functions();
|
|
|
|
ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes();
|
|
|
|
ArrayRef<WasmSignature> Types = WasmObj->types();
|
|
|
|
Functions.reserve(Funcs.size());
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-06-07 14:00:46 +08:00
|
|
|
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);
|
|
|
|
}
|
2018-08-23 01:50:51 +08:00
|
|
|
setRelocs(Functions, CodeSection);
|
2018-01-10 07:56:44 +08:00
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
// Populate `Globals`.
|
|
|
|
for (const WasmGlobal &G : WasmObj->globals())
|
2018-04-21 01:28:12 +08:00
|
|
|
Globals.emplace_back(make<InputGlobal>(G, this));
|
2018-02-28 10:57:37 +08:00
|
|
|
|
2018-12-08 14:17:43 +08:00
|
|
|
// Populate `Events`.
|
|
|
|
for (const WasmEvent &E : WasmObj->events())
|
|
|
|
Events.emplace_back(make<InputEvent>(Types[E.Type.SigIndex], E, this));
|
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
// Populate `Symbols` based on the WasmSymbols in the object.
|
|
|
|
Symbols.reserve(WasmObj->getNumberOfSymbols());
|
2017-11-18 02:14:09 +08:00
|
|
|
for (const SymbolRef &Sym : WasmObj->symbols()) {
|
|
|
|
const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl());
|
2019-05-25 06:45:08 +08:00
|
|
|
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);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t Idx = Symbols.size();
|
2019-05-29 23:41:08 +08:00
|
|
|
Symbols.push_back(createUndefined(WasmSym, IsCalledDirectly[Idx]));
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const {
|
2018-03-14 23:45:11 +08:00
|
|
|
uint32_t C = Chunk->getComdat();
|
|
|
|
if (C == UINT32_MAX)
|
2018-03-02 07:29:05 +08:00
|
|
|
return false;
|
2019-05-16 00:03:28 +08:00
|
|
|
return !KeptComdats[C];
|
2018-02-28 10:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const {
|
|
|
|
return cast<FunctionSymbol>(Symbols[Index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const {
|
|
|
|
return cast<GlobalSymbol>(Symbols[Index]);
|
|
|
|
}
|
|
|
|
|
2018-12-08 14:17:43 +08:00
|
|
|
EventSymbol *ObjFile::getEventSymbol(uint32_t Index) const {
|
|
|
|
return cast<EventSymbol>(Symbols[Index]);
|
|
|
|
}
|
|
|
|
|
2018-05-05 07:14:42 +08:00
|
|
|
SectionSymbol *ObjFile::getSectionSymbol(uint32_t Index) const {
|
|
|
|
return cast<SectionSymbol>(Symbols[Index]);
|
|
|
|
}
|
|
|
|
|
2018-02-28 10:57:37 +08:00
|
|
|
DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const {
|
|
|
|
return cast<DataSymbol>(Symbols[Index]);
|
|
|
|
}
|
|
|
|
|
2018-02-28 08:50:54 +08:00
|
|
|
Symbol *ObjFile::createDefined(const WasmSymbol &Sym) {
|
2018-03-03 05:19:55 +08:00
|
|
|
StringRef Name = Sym.Info.Name;
|
|
|
|
uint32_t Flags = Sym.Info.Flags;
|
|
|
|
|
2018-02-28 08:50:54 +08:00
|
|
|
switch (Sym.Info.Kind) {
|
|
|
|
case WASM_SYMBOL_TYPE_FUNCTION: {
|
2018-02-28 10:57:37 +08:00
|
|
|
InputFunction *Func =
|
|
|
|
Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()];
|
2019-06-07 14:00:46 +08:00
|
|
|
if (Func->Discarded)
|
2018-02-28 08:50:54 +08:00
|
|
|
return nullptr;
|
|
|
|
if (Sym.isBindingLocal())
|
2018-03-03 05:19:55 +08:00
|
|
|
return make<DefinedFunction>(Name, Flags, this, Func);
|
|
|
|
return Symtab->addDefinedFunction(Name, Flags, this, Func);
|
2018-02-28 08:50:54 +08:00
|
|
|
}
|
|
|
|
case WASM_SYMBOL_TYPE_DATA: {
|
2018-02-28 10:57:37 +08:00
|
|
|
InputSegment *Seg = Segments[Sym.Info.DataRef.Segment];
|
2019-06-07 14:00:46 +08:00
|
|
|
if (Seg->Discarded)
|
2018-02-28 08:50:54 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
uint32_t Offset = Sym.Info.DataRef.Offset;
|
|
|
|
uint32_t Size = Sym.Info.DataRef.Size;
|
|
|
|
|
|
|
|
if (Sym.isBindingLocal())
|
2018-03-03 05:19:55 +08:00
|
|
|
return make<DefinedData>(Name, Flags, this, Seg, Offset, Size);
|
|
|
|
return Symtab->addDefinedData(Name, Flags, this, Seg, Offset, Size);
|
2018-02-28 08:50:54 +08:00
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
case WASM_SYMBOL_TYPE_GLOBAL: {
|
2018-02-28 10:57:37 +08:00
|
|
|
InputGlobal *Global =
|
|
|
|
Globals[Sym.Info.ElementIndex - WasmObj->getNumImportedGlobals()];
|
2018-02-28 08:50:54 +08:00
|
|
|
if (Sym.isBindingLocal())
|
2018-03-03 05:19:55 +08:00
|
|
|
return make<DefinedGlobal>(Name, Flags, this, Global);
|
|
|
|
return Symtab->addDefinedGlobal(Name, Flags, this, Global);
|
2018-02-28 08:50:54 +08:00
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
case WASM_SYMBOL_TYPE_SECTION: {
|
|
|
|
InputSection *Section = CustomSectionsByIndex[Sym.Info.ElementIndex];
|
|
|
|
assert(Sym.isBindingLocal());
|
2019-05-21 17:13:09 +08:00
|
|
|
return make<SectionSymbol>(Flags, Section, this);
|
2018-05-05 07:14:42 +08:00
|
|
|
}
|
2018-12-08 14:17:43 +08:00
|
|
|
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);
|
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("unknown symbol kind");
|
2018-02-28 08:50:54 +08:00
|
|
|
}
|
|
|
|
|
2019-05-25 06:45:08 +08:00
|
|
|
Symbol *ObjFile::createUndefined(const WasmSymbol &Sym, bool IsCalledDirectly) {
|
2018-02-28 08:09:22 +08:00
|
|
|
StringRef Name = Sym.Info.Name;
|
|
|
|
uint32_t Flags = Sym.Info.Flags;
|
|
|
|
|
|
|
|
switch (Sym.Info.Kind) {
|
|
|
|
case WASM_SYMBOL_TYPE_FUNCTION:
|
2019-06-07 14:00:46 +08:00
|
|
|
if (Sym.isBindingLocal())
|
|
|
|
return make<UndefinedFunction>(Name, Sym.Info.ImportName,
|
|
|
|
Sym.Info.ImportModule, Flags, this,
|
|
|
|
Sym.Signature, IsCalledDirectly);
|
2019-02-08 06:00:48 +08:00
|
|
|
return Symtab->addUndefinedFunction(Name, Sym.Info.ImportName,
|
|
|
|
Sym.Info.ImportModule, Flags, this,
|
2019-05-25 06:45:08 +08:00
|
|
|
Sym.Signature, IsCalledDirectly);
|
2018-02-28 08:09:22 +08:00
|
|
|
case WASM_SYMBOL_TYPE_DATA:
|
2019-06-07 14:00:46 +08:00
|
|
|
if (Sym.isBindingLocal())
|
|
|
|
return make<UndefinedData>(Name, Flags, this);
|
2018-02-28 08:09:22 +08:00
|
|
|
return Symtab->addUndefinedData(Name, Flags, this);
|
|
|
|
case WASM_SYMBOL_TYPE_GLOBAL:
|
2019-06-07 14:00:46 +08:00
|
|
|
if (Sym.isBindingLocal())
|
|
|
|
return make<UndefinedGlobal>(Name, Sym.Info.ImportName,
|
|
|
|
Sym.Info.ImportModule, Flags, this,
|
|
|
|
Sym.GlobalType);
|
2019-02-08 06:00:48 +08:00
|
|
|
return Symtab->addUndefinedGlobal(Name, Sym.Info.ImportName,
|
|
|
|
Sym.Info.ImportModule, Flags, this,
|
|
|
|
Sym.GlobalType);
|
2018-05-05 07:14:42 +08:00
|
|
|
case WASM_SYMBOL_TYPE_SECTION:
|
|
|
|
llvm_unreachable("section symbols cannot be undefined");
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
llvm_unreachable("unknown symbol kind");
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-06-06 01:50:45 +08:00
|
|
|
void ArchiveFile::parse() {
|
2017-11-18 02:14:09 +08:00
|
|
|
// Parse a MemoryBufferRef as an archive file.
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");
|
2017-12-07 06:08:17 +08:00
|
|
|
File = CHECK(Archive::create(MB), toString(this));
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// Read the symbol table to construct Lazy symbols.
|
|
|
|
int Count = 0;
|
|
|
|
for (const Archive::Symbol &Sym : File->symbols()) {
|
|
|
|
Symtab->addLazy(this, &Sym);
|
|
|
|
++Count;
|
|
|
|
}
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Read " << Count << " symbols\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArchiveFile::addMember(const Archive::Symbol *Sym) {
|
|
|
|
const Archive::Child &C =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(Sym->getMember(),
|
2017-11-18 02:14:09 +08:00
|
|
|
"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)
|
|
|
|
return;
|
|
|
|
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "loading lazy: " << Sym->getName() << "\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
MemoryBufferRef MB =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(C.getMemoryBufferRef(),
|
2017-11-18 02:14:09 +08:00
|
|
|
"could not get the buffer for the member defining symbol " +
|
|
|
|
Sym->getName());
|
|
|
|
|
2019-04-09 13:41:52 +08:00
|
|
|
InputFile *Obj = createObjectFile(MB, getName());
|
2017-11-18 02:14:09 +08:00
|
|
|
Symtab->addFile(Obj);
|
|
|
|
}
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
|
|
|
|
switch (GvVisibility) {
|
|
|
|
case GlobalValue::DefaultVisibility:
|
|
|
|
return WASM_SYMBOL_VISIBILITY_DEFAULT;
|
|
|
|
case GlobalValue::HiddenVisibility:
|
|
|
|
case GlobalValue::ProtectedVisibility:
|
|
|
|
return WASM_SYMBOL_VISIBILITY_HIDDEN;
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknown visibility");
|
|
|
|
}
|
|
|
|
|
2019-05-16 00:03:28 +08:00
|
|
|
static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
|
|
|
|
const lto::InputFile::Symbol &ObjSym,
|
2018-05-31 02:07:52 +08:00
|
|
|
BitcodeFile &F) {
|
|
|
|
StringRef Name = Saver.save(ObjSym.getName());
|
|
|
|
|
|
|
|
uint32_t Flags = ObjSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0;
|
|
|
|
Flags |= mapVisibility(ObjSym.getVisibility());
|
|
|
|
|
2019-05-16 00:03:28 +08:00
|
|
|
int C = ObjSym.getComdatIndex();
|
|
|
|
bool ExcludedByComdat = C != -1 && !KeptComdats[C];
|
|
|
|
|
|
|
|
if (ObjSym.isUndefined() || ExcludedByComdat) {
|
2018-05-31 02:07:52 +08:00
|
|
|
if (ObjSym.isExecutable())
|
2019-02-08 06:00:48 +08:00
|
|
|
return Symtab->addUndefinedFunction(Name, Name, DefaultModule, Flags, &F,
|
2019-05-25 06:45:08 +08:00
|
|
|
nullptr, true);
|
2018-05-31 02:07:52 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-06 01:50:45 +08:00
|
|
|
void BitcodeFile::parse() {
|
2018-05-31 02:07:52 +08:00
|
|
|
Obj = check(lto::InputFile::create(MemoryBufferRef(
|
2018-07-19 05:46:09 +08:00
|
|
|
MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier()))));
|
2018-05-31 02:07:52 +08:00
|
|
|
Triple T(Obj->getTargetTriple());
|
|
|
|
if (T.getArch() != Triple::wasm32) {
|
|
|
|
error(toString(MB.getBufferIdentifier()) + ": machine type must be wasm32");
|
|
|
|
return;
|
|
|
|
}
|
2019-05-16 00:03:28 +08:00
|
|
|
std::vector<bool> KeptComdats;
|
|
|
|
for (StringRef S : Obj->getComdatTable())
|
2019-06-06 01:39:37 +08:00
|
|
|
KeptComdats.push_back(Symtab->addComdat(S));
|
2018-05-31 02:07:52 +08:00
|
|
|
|
|
|
|
for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
|
2019-05-16 00:03:28 +08:00
|
|
|
Symbols.push_back(createBitcodeSymbol(KeptComdats, ObjSym, *this));
|
2018-05-31 02:07:52 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
// Returns a string in the format of "foo.o" or "foo.a(bar.o)".
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string lld::toString(const wasm::InputFile *File) {
|
2017-11-18 02:14:09 +08:00
|
|
|
if (!File)
|
|
|
|
return "<internal>";
|
|
|
|
|
2018-07-19 05:46:09 +08:00
|
|
|
if (File->ArchiveName.empty())
|
2017-11-18 02:14:09 +08:00
|
|
|
return File->getName();
|
|
|
|
|
2018-07-19 05:46:09 +08:00
|
|
|
return (File->ArchiveName + "(" + File->getName() + ")").str();
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|