Coverage Report

Created: 2018-08-19 14:04

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