Coverage Report

Created: 2019-01-18 03:29

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