Coverage Report

Created: 2018-10-20 12:32

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