Coverage Report

Created: 2018-06-18 20:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/InputFiles.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- InputFiles.cpp -----------------------------------------------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "InputFiles.h"
11
#include "Config.h"
12
#include "InputChunks.h"
13
#include "InputGlobal.h"
14
#include "SymbolTable.h"
15
#include "lld/Common/ErrorHandler.h"
16
#include "lld/Common/Memory.h"
17
#include "llvm/Object/Binary.h"
18
#include "llvm/Object/Wasm.h"
19
#include "llvm/Support/raw_ostream.h"
20
21
#define DEBUG_TYPE "lld"
22
23
using namespace lld;
24
using namespace lld::wasm;
25
26
using namespace llvm;
27
using namespace llvm::object;
28
using namespace llvm::wasm;
29
30
0
Optional<MemoryBufferRef> lld::wasm::readFile(StringRef Path) {
31
0
  log("Loading: " + Path);
32
0
33
0
  auto MBOrErr = MemoryBuffer::getFile(Path);
34
0
  if (auto EC = MBOrErr.getError()) {
35
0
    error("cannot open " + Path + ": " + EC.message());
36
0
    return None;
37
0
  }
38
0
  std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
39
0
  MemoryBufferRef MBRef = MB->getMemBufferRef();
40
0
  make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
41
0
42
0
  return MBRef;
43
0
}
44
45
0
void ObjFile::dumpInfo() const {
46
0
  log("info for: " + getName() +
47
0
      "\n              Symbols : " + Twine(Symbols.size()) +
48
0
      "\n     Function Imports : " + Twine(WasmObj->getNumImportedFunctions()) +
49
0
      "\n       Global Imports : " + Twine(WasmObj->getNumImportedGlobals()));
50
0
}
51
52
// Relocations contain either symbol or type indices.  This function takes a
53
// relocation and returns relocated index (i.e. translates from the input
54
// sybmol/type space to the output symbol/type space).
55
0
uint32_t ObjFile::calcNewIndex(const WasmRelocation &Reloc) const {
56
0
  if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB) {
57
0
    assert(TypeIsUsed[Reloc.Index]);
58
0
    return TypeMap[Reloc.Index];
59
0
  }
60
0
  return Symbols[Reloc.Index]->getOutputSymbolIndex();
61
0
}
62
63
// Relocations can contain addend for combined sections. This function takes a
64
// relocation and returns updated addend by offset in the output section.
65
0
uint32_t ObjFile::calcNewAddend(const WasmRelocation &Reloc) const {
66
0
  switch (Reloc.Type) {
67
0
  case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
68
0
  case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
69
0
  case R_WEBASSEMBLY_MEMORY_ADDR_I32:
70
0
  case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
71
0
    return Reloc.Addend;
72
0
  case R_WEBASSEMBLY_SECTION_OFFSET_I32:
73
0
    return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend;
74
0
  default:
75
0
    llvm_unreachable("unexpected relocation type");
76
0
  }
77
0
}
78
79
// Calculate the value we expect to find at the relocation location.
80
// This is used as a sanity check before applying a relocation to a given
81
// location.  It is useful for catching bugs in the compiler and linker.
82
0
uint32_t ObjFile::calcExpectedValue(const WasmRelocation &Reloc) const {
83
0
  switch (Reloc.Type) {
84
0
  case R_WEBASSEMBLY_TABLE_INDEX_I32:
85
0
  case R_WEBASSEMBLY_TABLE_INDEX_SLEB: {
86
0
    const WasmSymbol& Sym = WasmObj->syms()[Reloc.Index];
87
0
    return TableEntries[Sym.Info.ElementIndex];
88
0
  }
89
0
  case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
90
0
  case R_WEBASSEMBLY_MEMORY_ADDR_I32:
91
0
  case R_WEBASSEMBLY_MEMORY_ADDR_LEB: {
92
0
    const WasmSymbol& Sym = WasmObj->syms()[Reloc.Index];
93
0
    if (Sym.isUndefined())
94
0
      return 0;
95
0
    const WasmSegment& Segment = WasmObj->dataSegments()[Sym.Info.DataRef.Segment];
96
0
    return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset +
97
0
           Reloc.Addend;
98
0
  }
99
0
  case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
100
0
    if (auto *Sym = dyn_cast<DefinedFunction>(getFunctionSymbol(Reloc.Index))) {
101
0
      return Sym->Function->getFunctionInputOffset() +
102
0
             Sym->Function->getFunctionCodeOffset() + Reloc.Addend;
103
0
    }
104
0
    return 0;
105
0
  case R_WEBASSEMBLY_SECTION_OFFSET_I32:
106
0
    return Reloc.Addend;
107
0
  case R_WEBASSEMBLY_TYPE_INDEX_LEB:
108
0
    return Reloc.Index;
109
0
  case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
110
0
  case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: {
111
0
    const WasmSymbol& Sym = WasmObj->syms()[Reloc.Index];
112
0
    return Sym.Info.ElementIndex;
113
0
  }
114
0
  default:
115
0
    llvm_unreachable("unknown relocation type");
116
0
  }
117
0
}
118
119
// Translate from the relocation's index into the final linked output value.
120
0
uint32_t ObjFile::calcNewValue(const WasmRelocation &Reloc) const {
121
0
  switch (Reloc.Type) {
122
0
  case R_WEBASSEMBLY_TABLE_INDEX_I32:
123
0
  case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
124
0
    return getFunctionSymbol(Reloc.Index)->getTableIndex();
125
0
  case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
126
0
  case R_WEBASSEMBLY_MEMORY_ADDR_I32:
127
0
  case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
128
0
    if (auto *Sym = dyn_cast<DefinedData>(getDataSymbol(Reloc.Index)))
129
0
      if (Sym->isLive())
130
0
        return Sym->getVirtualAddress() + Reloc.Addend;
131
0
    return 0;
132
0
  case R_WEBASSEMBLY_TYPE_INDEX_LEB:
133
0
    return TypeMap[Reloc.Index];
134
0
  case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
135
0
    return getFunctionSymbol(Reloc.Index)->getFunctionIndex();
136
0
  case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
137
0
    return getGlobalSymbol(Reloc.Index)->getGlobalIndex();
138
0
  case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
139
0
    if (auto *Sym = dyn_cast<DefinedFunction>(getFunctionSymbol(Reloc.Index))) {
140
0
      return Sym->Function->OutputOffset +
141
0
             Sym->Function->getFunctionCodeOffset() + Reloc.Addend;
142
0
    }
143
0
    return 0;
144
0
  case R_WEBASSEMBLY_SECTION_OFFSET_I32:
145
0
    return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend;
146
0
  default:
147
0
    llvm_unreachable("unknown relocation type");
148
0
  }
149
0
}
150
151
0
void ObjFile::parse() {
152
0
  // Parse a memory buffer as a wasm file.
153
0
  LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n");
154
0
  std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), toString(this));
155
0
156
0
  auto *Obj = dyn_cast<WasmObjectFile>(Bin.get());
157
0
  if (!Obj)
158
0
    fatal(toString(this) + ": not a wasm file");
159
0
  if (!Obj->isRelocatableObject())
160
0
    fatal(toString(this) + ": not a relocatable wasm file");
161
0
162
0
  Bin.release();
163
0
  WasmObj.reset(Obj);
164
0
165
0
  // Build up a map of function indices to table indices for use when
166
0
  // verifying the existing table index relocations
167
0
  uint32_t TotalFunctions =
168
0
      WasmObj->getNumImportedFunctions() + WasmObj->functions().size();
169
0
  TableEntries.resize(TotalFunctions);
170
0
  for (const WasmElemSegment &Seg : WasmObj->elements()) {
171
0
    if (Seg.Offset.Opcode != WASM_OPCODE_I32_CONST)
172
0
      fatal(toString(this) + ": invalid table elements");
173
0
    uint32_t Offset = Seg.Offset.Value.Int32;
174
0
    for (uint32_t Index = 0; Index < Seg.Functions.size(); Index++) {
175
0
176
0
      uint32_t FunctionIndex = Seg.Functions[Index];
177
0
      TableEntries[FunctionIndex] = Offset + Index;
178
0
    }
179
0
  }
180
0
181
0
  // Find the code and data sections.  Wasm objects can have at most one code
182
0
  // and one data section.
183
0
  uint32_t SectionIndex = 0;
184
0
  for (const SectionRef &Sec : WasmObj->sections()) {
185
0
    const WasmSection &Section = WasmObj->getWasmSection(Sec);
186
0
    if (Section.Type == WASM_SEC_CODE) {
187
0
      CodeSection = &Section;
188
0
    } else if (Section.Type == WASM_SEC_DATA) {
189
0
      DataSection = &Section;
190
0
    } else if (Section.Type == WASM_SEC_CUSTOM) {
191
0
      CustomSections.emplace_back(make<InputSection>(Section, this));
192
0
      CustomSections.back()->copyRelocations(Section);
193
0
      CustomSectionsByIndex[SectionIndex] = CustomSections.back();
194
0
    }
195
0
    SectionIndex++;
196
0
  }
197
0
198
0
  TypeMap.resize(getWasmObj()->types().size());
199
0
  TypeIsUsed.resize(getWasmObj()->types().size(), false);
200
0
201
0
  ArrayRef<StringRef> Comdats = WasmObj->linkingData().Comdats;
202
0
  UsedComdats.resize(Comdats.size());
203
0
  for (unsigned I = 0; I < Comdats.size(); ++I)
204
0
    UsedComdats[I] = Symtab->addComdat(Comdats[I]);
205
0
206
0
  // Populate `Segments`.
207
0
  for (const WasmSegment &S : WasmObj->dataSegments()) {
208
0
    InputSegment *Seg = make<InputSegment>(S, this);
209
0
    Seg->copyRelocations(*DataSection);
210
0
    Segments.emplace_back(Seg);
211
0
  }
212
0
213
0
  // Populate `Functions`.
214
0
  ArrayRef<WasmFunction> Funcs = WasmObj->functions();
215
0
  ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes();
216
0
  ArrayRef<WasmSignature> Types = WasmObj->types();
217
0
  Functions.reserve(Funcs.size());
218
0
219
0
  for (size_t I = 0, E = Funcs.size(); I != E; ++I) {
220
0
    InputFunction *F =
221
0
        make<InputFunction>(Types[FuncTypes[I]], &Funcs[I], this);
222
0
    F->copyRelocations(*CodeSection);
223
0
    Functions.emplace_back(F);
224
0
  }
225
0
226
0
  // Populate `Globals`.
227
0
  for (const WasmGlobal &G : WasmObj->globals())
228
0
    Globals.emplace_back(make<InputGlobal>(G, this));
229
0
230
0
  // Populate `Symbols` based on the WasmSymbols in the object.
231
0
  Symbols.reserve(WasmObj->getNumberOfSymbols());
232
0
  for (const SymbolRef &Sym : WasmObj->symbols()) {
233
0
    const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl());
234
0
    if (Symbol *Sym = createDefined(WasmSym))
235
0
      Symbols.push_back(Sym);
236
0
    else
237
0
      Symbols.push_back(createUndefined(WasmSym));
238
0
  }
239
0
}
240
241
0
bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const {
242
0
  uint32_t C = Chunk->getComdat();
243
0
  if (C == UINT32_MAX)
244
0
    return false;
245
0
  return !UsedComdats[C];
246
0
}
247
248
0
FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const {
249
0
  return cast<FunctionSymbol>(Symbols[Index]);
250
0
}
251
252
0
GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const {
253
0
  return cast<GlobalSymbol>(Symbols[Index]);
254
0
}
255
256
0
SectionSymbol *ObjFile::getSectionSymbol(uint32_t Index) const {
257
0
  return cast<SectionSymbol>(Symbols[Index]);
258
0
}
259
260
0
DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const {
261
0
  return cast<DataSymbol>(Symbols[Index]);
262
0
}
263
264
0
Symbol *ObjFile::createDefined(const WasmSymbol &Sym) {
265
0
  if (!Sym.isDefined())
266
0
    return nullptr;
267
0
268
0
  StringRef Name = Sym.Info.Name;
269
0
  uint32_t Flags = Sym.Info.Flags;
270
0
271
0
  switch (Sym.Info.Kind) {
272
0
  case WASM_SYMBOL_TYPE_FUNCTION: {
273
0
    InputFunction *Func =
274
0
        Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()];
275
0
    if (isExcludedByComdat(Func)) {
276
0
      Func->Live = false;
277
0
      return nullptr;
278
0
    }
279
0
280
0
    if (Sym.isBindingLocal())
281
0
      return make<DefinedFunction>(Name, Flags, this, Func);
282
0
    return Symtab->addDefinedFunction(Name, Flags, this, Func);
283
0
  }
284
0
  case WASM_SYMBOL_TYPE_DATA: {
285
0
    InputSegment *Seg = Segments[Sym.Info.DataRef.Segment];
286
0
    if (isExcludedByComdat(Seg)) {
287
0
      Seg->Live = false;
288
0
      return nullptr;
289
0
    }
290
0
291
0
    uint32_t Offset = Sym.Info.DataRef.Offset;
292
0
    uint32_t Size = Sym.Info.DataRef.Size;
293
0
294
0
    if (Sym.isBindingLocal())
295
0
      return make<DefinedData>(Name, Flags, this, Seg, Offset, Size);
296
0
    return Symtab->addDefinedData(Name, Flags, this, Seg, Offset, Size);
297
0
  }
298
0
  case WASM_SYMBOL_TYPE_GLOBAL: {
299
0
    InputGlobal *Global =
300
0
        Globals[Sym.Info.ElementIndex - WasmObj->getNumImportedGlobals()];
301
0
    if (Sym.isBindingLocal())
302
0
      return make<DefinedGlobal>(Name, Flags, this, Global);
303
0
    return Symtab->addDefinedGlobal(Name, Flags, this, Global);
304
0
  }
305
0
  case WASM_SYMBOL_TYPE_SECTION: {
306
0
    InputSection *Section = CustomSectionsByIndex[Sym.Info.ElementIndex];
307
0
    assert(Sym.isBindingLocal());
308
0
    return make<SectionSymbol>(Name, Flags, Section, this);
309
0
  }
310
0
  }
311
0
  llvm_unreachable("unknown symbol kind");
312
0
}
313
314
0
Symbol *ObjFile::createUndefined(const WasmSymbol &Sym) {
315
0
  StringRef Name = Sym.Info.Name;
316
0
  uint32_t Flags = Sym.Info.Flags;
317
0
318
0
  switch (Sym.Info.Kind) {
319
0
  case WASM_SYMBOL_TYPE_FUNCTION:
320
0
    return Symtab->addUndefinedFunction(Name, Flags, this, Sym.FunctionType);
321
0
  case WASM_SYMBOL_TYPE_DATA:
322
0
    return Symtab->addUndefinedData(Name, Flags, this);
323
0
  case WASM_SYMBOL_TYPE_GLOBAL:
324
0
    return Symtab->addUndefinedGlobal(Name, Flags, this, Sym.GlobalType);
325
0
  case WASM_SYMBOL_TYPE_SECTION:
326
0
    llvm_unreachable("section symbols cannot be undefined");
327
0
  }
328
0
  llvm_unreachable("unknown symbol kind");
329
0
}
330
331
0
void ArchiveFile::parse() {
332
0
  // Parse a MemoryBufferRef as an archive file.
333
0
  LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");
334
0
  File = CHECK(Archive::create(MB), toString(this));
335
0
336
0
  // Read the symbol table to construct Lazy symbols.
337
0
  int Count = 0;
338
0
  for (const Archive::Symbol &Sym : File->symbols()) {
339
0
    Symtab->addLazy(this, &Sym);
340
0
    ++Count;
341
0
  }
342
0
  LLVM_DEBUG(dbgs() << "Read " << Count << " symbols\n");
343
0
}
344
345
0
void ArchiveFile::addMember(const Archive::Symbol *Sym) {
346
0
  const Archive::Child &C =
347
0
      CHECK(Sym->getMember(),
348
0
            "could not get the member for symbol " + Sym->getName());
349
0
350
0
  // Don't try to load the same member twice (this can happen when members
351
0
  // mutually reference each other).
352
0
  if (!Seen.insert(C.getChildOffset()).second)
353
0
    return;
354
0
355
0
  LLVM_DEBUG(dbgs() << "loading lazy: " << Sym->getName() << "\n");
356
0
  LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n");
357
0
358
0
  MemoryBufferRef MB =
359
0
      CHECK(C.getMemoryBufferRef(),
360
0
            "could not get the buffer for the member defining symbol " +
361
0
                Sym->getName());
362
0
363
0
  if (identify_magic(MB.getBuffer()) != file_magic::wasm_object) {
364
0
    error("unknown file type: " + MB.getBufferIdentifier());
365
0
    return;
366
0
  }
367
0
368
0
  InputFile *Obj = make<ObjFile>(MB);
369
0
  Obj->ParentName = ParentName;
370
0
  Symtab->addFile(Obj);
371
0
}
372
373
static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
374
  switch (GvVisibility) {
375
  case GlobalValue::DefaultVisibility:
376
    return WASM_SYMBOL_VISIBILITY_DEFAULT;
377
  case GlobalValue::HiddenVisibility:
378
  case GlobalValue::ProtectedVisibility:
379
    return WASM_SYMBOL_VISIBILITY_HIDDEN;
380
  }
381
  llvm_unreachable("unknown visibility");
382
}
383
384
static Symbol *createBitcodeSymbol(const lto::InputFile::Symbol &ObjSym,
385
0
                                   BitcodeFile &F) {
386
0
  StringRef Name = Saver.save(ObjSym.getName());
387
0
388
0
  uint32_t Flags = ObjSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0;
389
0
  Flags |= mapVisibility(ObjSym.getVisibility());
390
0
391
0
  if (ObjSym.isUndefined()) {
392
0
    if (ObjSym.isExecutable())
393
0
      return Symtab->addUndefinedFunction(Name, Flags, &F, nullptr);
394
0
    return Symtab->addUndefinedData(Name, Flags, &F);
395
0
  }
396
0
397
0
  if (ObjSym.isExecutable())
398
0
    return Symtab->addDefinedFunction(Name, Flags, &F, nullptr);
399
0
  return Symtab->addDefinedData(Name, Flags, &F, nullptr, 0, 0);
400
0
}
401
402
0
void BitcodeFile::parse() {
403
0
  Obj = check(lto::InputFile::create(MemoryBufferRef(
404
0
      MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
405
0
  Triple T(Obj->getTargetTriple());
406
0
  if (T.getArch() != Triple::wasm32) {
407
0
    error(toString(MB.getBufferIdentifier()) + ": machine type must be wasm32");
408
0
    return;
409
0
  }
410
0
411
0
  for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
412
0
    Symbols.push_back(createBitcodeSymbol(ObjSym, *this));
413
0
}
414
415
// Returns a string in the format of "foo.o" or "foo.a(bar.o)".
416
0
std::string lld::toString(const wasm::InputFile *File) {
417
0
  if (!File)
418
0
    return "<internal>";
419
0
420
0
  if (File->ParentName.empty())
421
0
    return File->getName();
422
0
423
0
  return (File->ParentName + "(" + File->getName() + ")").str();
424
0
}