Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/Symbols.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Symbols.cpp --------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "Symbols.h"
10
#include "Config.h"
11
#include "InputChunks.h"
12
#include "InputEvent.h"
13
#include "InputFiles.h"
14
#include "InputGlobal.h"
15
#include "OutputSections.h"
16
#include "OutputSegment.h"
17
#include "lld/Common/ErrorHandler.h"
18
#include "lld/Common/Strings.h"
19
20
#define DEBUG_TYPE "lld"
21
22
using namespace llvm;
23
using namespace llvm::wasm;
24
using namespace lld;
25
using namespace lld::wasm;
26
27
DefinedFunction *WasmSym::callCtors;
28
DefinedFunction *WasmSym::initMemory;
29
DefinedFunction *WasmSym::applyRelocs;
30
DefinedFunction *WasmSym::initTLS;
31
DefinedData *WasmSym::dsoHandle;
32
DefinedData *WasmSym::dataEnd;
33
DefinedData *WasmSym::globalBase;
34
DefinedData *WasmSym::heapBase;
35
GlobalSymbol *WasmSym::stackPointer;
36
GlobalSymbol *WasmSym::tlsBase;
37
GlobalSymbol *WasmSym::tlsSize;
38
GlobalSymbol *WasmSym::tlsAlign;
39
UndefinedGlobal *WasmSym::tableBase;
40
UndefinedGlobal *WasmSym::memoryBase;
41
42
254
WasmSymbolType Symbol::getWasmType() const {
43
254
  if (isa<FunctionSymbol>(this))
44
216
    return WASM_SYMBOL_TYPE_FUNCTION;
45
38
  if (isa<DataSymbol>(this))
46
27
    return WASM_SYMBOL_TYPE_DATA;
47
11
  if (isa<GlobalSymbol>(this))
48
7
    return WASM_SYMBOL_TYPE_GLOBAL;
49
4
  if (isa<EventSymbol>(this))
50
0
    return WASM_SYMBOL_TYPE_EVENT;
51
4
  if (isa<SectionSymbol>(this) || isa<OutputSectionSymbol>(this))
52
4
    return WASM_SYMBOL_TYPE_SECTION;
53
0
  llvm_unreachable("invalid symbol kind");
54
0
}
55
56
556
const WasmSignature *Symbol::getSignature() const {
57
556
  if (auto* f = dyn_cast<FunctionSymbol>(this))
58
537
    return f->signature;
59
19
  if (auto *l = dyn_cast<LazySymbol>(this))
60
18
    return l->signature;
61
1
  return nullptr;
62
1
}
63
64
2.56k
InputChunk *Symbol::getChunk() const {
65
2.56k
  if (auto *f = dyn_cast<DefinedFunction>(this))
66
1.91k
    return f->function;
67
650
  if (auto *d = dyn_cast<DefinedData>(this))
68
417
    return d->segment;
69
233
  return nullptr;
70
233
}
71
72
47
bool Symbol::isDiscarded() const {
73
47
  if (InputChunk *c = getChunk())
74
44
    return c->discarded;
75
3
  return false;
76
3
}
77
78
1.87k
bool Symbol::isLive() const {
79
1.87k
  if (auto *g = dyn_cast<DefinedGlobal>(this))
80
17
    return g->global->live;
81
1.85k
  if (auto *e = dyn_cast<DefinedEvent>(this))
82
2
    return e->event->live;
83
1.85k
  if (InputChunk *c = getChunk())
84
1.64k
    return c->live;
85
206
  return referenced;
86
206
}
87
88
340
void Symbol::markLive() {
89
340
  assert(!isDiscarded());
90
340
  if (auto *g = dyn_cast<DefinedGlobal>(this))
91
1
    g->global->live = true;
92
340
  if (auto *e = dyn_cast<DefinedEvent>(this))
93
1
    e->event->live = true;
94
340
  if (InputChunk *c = getChunk())
95
297
    c->live = true;
96
340
  referenced = true;
97
340
}
98
99
223
uint32_t Symbol::getOutputSymbolIndex() const {
100
223
  assert(outputSymbolIndex != INVALID_INDEX);
101
223
  return outputSymbolIndex;
102
223
}
103
104
253
void Symbol::setOutputSymbolIndex(uint32_t index) {
105
253
  LLVM_DEBUG(dbgs() << "setOutputSymbolIndex " << name << " -> " << index
106
253
                    << "\n");
107
253
  assert(outputSymbolIndex == INVALID_INDEX);
108
253
  outputSymbolIndex = index;
109
253
}
110
111
8
void Symbol::setGOTIndex(uint32_t index) {
112
8
  LLVM_DEBUG(dbgs() << "setGOTIndex " << name << " -> " << index << "\n");
113
8
  assert(gotIndex == INVALID_INDEX);
114
8
  // Any symbol that is assigned a GOT entry must be exported othewise the
115
8
  // dynamic linker won't be able create the entry that contains it.
116
8
  forceExport = true;
117
8
  gotIndex = index;
118
8
}
119
120
1.47k
bool Symbol::isWeak() const {
121
1.47k
  return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK;
122
1.47k
}
123
124
2.20k
bool Symbol::isLocal() const {
125
2.20k
  return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_LOCAL;
126
2.20k
}
127
128
506
bool Symbol::isHidden() const {
129
506
  return (flags & WASM_SYMBOL_VISIBILITY_MASK) == WASM_SYMBOL_VISIBILITY_HIDDEN;
130
506
}
131
132
128
void Symbol::setHidden(bool isHidden) {
133
128
  LLVM_DEBUG(dbgs() << "setHidden: " << name << " -> " << isHidden << "\n");
134
128
  flags &= ~WASM_SYMBOL_VISIBILITY_MASK;
135
128
  if (isHidden)
136
9
    flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
137
119
  else
138
119
    flags |= WASM_SYMBOL_VISIBILITY_DEFAULT;
139
128
}
140
141
1.79k
bool Symbol::isExported() const {
142
1.79k
  if (!isDefined() || 
isLocal()1.68k
)
143
107
    return false;
144
1.68k
145
1.68k
  if (forceExport || 
config->exportAll1.42k
)
146
291
    return true;
147
1.39k
148
1.39k
  if (config->exportDynamic && 
!isHidden()241
)
149
129
    return true;
150
1.26k
151
1.26k
  return flags & WASM_SYMBOL_EXPORTED;
152
1.26k
}
153
154
636
uint32_t FunctionSymbol::getFunctionIndex() const {
155
636
  if (auto *f = dyn_cast<DefinedFunction>(this))
156
575
    return f->function->getFunctionIndex();
157
61
  assert(functionIndex != INVALID_INDEX);
158
61
  return functionIndex;
159
61
}
160
161
30
void FunctionSymbol::setFunctionIndex(uint32_t index) {
162
30
  LLVM_DEBUG(dbgs() << "setFunctionIndex " << name << " -> " << index << "\n");
163
30
  assert(functionIndex == INVALID_INDEX);
164
30
  functionIndex = index;
165
30
}
166
167
0
bool FunctionSymbol::hasFunctionIndex() const {
168
0
  if (auto *f = dyn_cast<DefinedFunction>(this))
169
0
    return f->function->hasFunctionIndex();
170
0
  return functionIndex != INVALID_INDEX;
171
0
}
172
173
64
uint32_t FunctionSymbol::getTableIndex() const {
174
64
  if (auto *f = dyn_cast<DefinedFunction>(this))
175
58
    return f->function->getTableIndex();
176
6
  assert(tableIndex != INVALID_INDEX);
177
6
  return tableIndex;
178
6
}
179
180
112
bool FunctionSymbol::hasTableIndex() const {
181
112
  if (auto *f = dyn_cast<DefinedFunction>(this))
182
98
    return f->function->hasTableIndex();
183
14
  return tableIndex != INVALID_INDEX;
184
14
}
185
186
54
void FunctionSymbol::setTableIndex(uint32_t index) {
187
54
  // For imports, we set the table index here on the Symbol; for defined
188
54
  // functions we set the index on the InputFunction so that we don't export
189
54
  // the same thing twice (keeps the table size down).
190
54
  if (auto *f = dyn_cast<DefinedFunction>(this)) {
191
47
    f->function->setTableIndex(index);
192
47
    return;
193
47
  }
194
7
  LLVM_DEBUG(dbgs() << "setTableIndex " << name << " -> " << index << "\n");
195
7
  assert(tableIndex == INVALID_INDEX);
196
7
  tableIndex = index;
197
7
}
198
199
DefinedFunction::DefinedFunction(StringRef name, uint32_t flags, InputFile *f,
200
                                 InputFunction *function)
201
    : FunctionSymbol(name, DefinedFunctionKind, flags, f,
202
                     function ? &function->signature : nullptr),
203
830
      function(function) {}
204
205
239
uint32_t DefinedData::getVirtualAddress() const {
206
239
  LLVM_DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
207
239
  if (segment) {
208
212
    // For thread local data, the symbol location is relative to the start of
209
212
    // the .tdata section, since they are used as offsets from __tls_base.
210
212
    // Hence, we do not add in segment->outputSeg->startVA.
211
212
    if (segment->outputSeg->name == ".tdata")
212
6
      return segment->outputSegmentOffset + offset;
213
206
    return segment->outputSeg->startVA + segment->outputSegmentOffset + offset;
214
206
  }
215
27
  return offset;
216
27
}
217
218
205
void DefinedData::setVirtualAddress(uint32_t value) {
219
205
  LLVM_DEBUG(dbgs() << "setVirtualAddress " << name << " -> " << value << "\n");
220
205
  assert(!segment);
221
205
  offset = value;
222
205
}
223
224
23
uint32_t DefinedData::getOutputSegmentOffset() const {
225
23
  LLVM_DEBUG(dbgs() << "getOutputSegmentOffset: " << getName() << "\n");
226
23
  return segment->outputSegmentOffset + offset;
227
23
}
228
229
23
uint32_t DefinedData::getOutputSegmentIndex() const {
230
23
  LLVM_DEBUG(dbgs() << "getOutputSegmentIndex: " << getName() << "\n");
231
23
  return segment->outputSeg->index;
232
23
}
233
234
69
uint32_t GlobalSymbol::getGlobalIndex() const {
235
69
  if (auto *f = dyn_cast<DefinedGlobal>(this))
236
26
    return f->global->getGlobalIndex();
237
43
  assert(globalIndex != INVALID_INDEX);
238
43
  return globalIndex;
239
43
}
240
241
22
void GlobalSymbol::setGlobalIndex(uint32_t index) {
242
22
  LLVM_DEBUG(dbgs() << "setGlobalIndex " << name << " -> " << index << "\n");
243
22
  assert(globalIndex == INVALID_INDEX);
244
22
  globalIndex = index;
245
22
}
246
247
0
bool GlobalSymbol::hasGlobalIndex() const {
248
0
  if (auto *f = dyn_cast<DefinedGlobal>(this))
249
0
    return f->global->hasGlobalIndex();
250
0
  return globalIndex != INVALID_INDEX;
251
0
}
252
253
DefinedGlobal::DefinedGlobal(StringRef name, uint32_t flags, InputFile *file,
254
                             InputGlobal *global)
255
    : GlobalSymbol(name, DefinedGlobalKind, flags, file,
256
                   global ? &global->getType() : nullptr),
257
246
      global(global) {}
258
259
2
uint32_t EventSymbol::getEventIndex() const {
260
2
  if (auto *f = dyn_cast<DefinedEvent>(this))
261
2
    return f->event->getEventIndex();
262
0
  assert(eventIndex != INVALID_INDEX);
263
0
  return eventIndex;
264
0
}
265
266
0
void EventSymbol::setEventIndex(uint32_t index) {
267
0
  LLVM_DEBUG(dbgs() << "setEventIndex " << name << " -> " << index << "\n");
268
0
  assert(eventIndex == INVALID_INDEX);
269
0
  eventIndex = index;
270
0
}
271
272
0
bool EventSymbol::hasEventIndex() const {
273
0
  if (auto *f = dyn_cast<DefinedEvent>(this))
274
0
    return f->event->hasEventIndex();
275
0
  return eventIndex != INVALID_INDEX;
276
0
}
277
278
DefinedEvent::DefinedEvent(StringRef name, uint32_t flags, InputFile *file,
279
                           InputEvent *event)
280
    : EventSymbol(name, DefinedEventKind, flags, file,
281
                  event ? &event->getType() : nullptr,
282
                  event ? &event->signature : nullptr),
283
1
      event(event) {}
284
285
2
const OutputSectionSymbol *SectionSymbol::getOutputSectionSymbol() const {
286
2
  assert(section->outputSec && section->outputSec->sectionSym);
287
2
  return section->outputSec->sectionSym;
288
2
}
289
290
12
void LazySymbol::fetch() { cast<ArchiveFile>(file)->addMember(&archiveSymbol); }
291
292
53
std::string lld::toString(const wasm::Symbol &sym) {
293
53
  return lld::maybeDemangleSymbol(sym.getName());
294
53
}
295
296
564
std::string lld::maybeDemangleSymbol(StringRef name) {
297
564
  if (config->demangle)
298
560
    if (Optional<std::string> s = demangleItanium(name))
299
3
      return *s;
300
561
  return name;
301
561
}
302
303
0
std::string lld::toString(wasm::Symbol::Kind kind) {
304
0
  switch (kind) {
305
0
  case wasm::Symbol::DefinedFunctionKind:
306
0
    return "DefinedFunction";
307
0
  case wasm::Symbol::DefinedDataKind:
308
0
    return "DefinedData";
309
0
  case wasm::Symbol::DefinedGlobalKind:
310
0
    return "DefinedGlobal";
311
0
  case wasm::Symbol::DefinedEventKind:
312
0
    return "DefinedEvent";
313
0
  case wasm::Symbol::UndefinedFunctionKind:
314
0
    return "UndefinedFunction";
315
0
  case wasm::Symbol::UndefinedDataKind:
316
0
    return "UndefinedData";
317
0
  case wasm::Symbol::UndefinedGlobalKind:
318
0
    return "UndefinedGlobal";
319
0
  case wasm::Symbol::LazyKind:
320
0
    return "LazyKind";
321
0
  case wasm::Symbol::SectionKind:
322
0
    return "SectionKind";
323
0
  case wasm::Symbol::OutputSectionKind:
324
0
    return "OutputSectionKind";
325
0
  }
326
0
  llvm_unreachable("invalid symbol kind");
327
0
}
328
329
330
2
void lld::wasm::printTraceSymbolUndefined(StringRef name, const InputFile* file) {
331
2
  message(toString(file) + ": reference to " + name);
332
2
}
333
334
// Print out a log message for --trace-symbol.
335
6
void lld::wasm::printTraceSymbol(Symbol *sym) {
336
6
  // Undefined symbols are traced via printTraceSymbolUndefined
337
6
  if (sym->isUndefined())
338
1
    return;
339
5
340
5
  std::string s;
341
5
  if (sym->isLazy())
342
0
    s = ": lazy definition of ";
343
5
  else
344
5
    s = ": definition of ";
345
5
346
5
  message(toString(sym->getFile()) + s + sym->getName());
347
5
}
348
349
const char *lld::wasm::defaultModule = "env";
350
const char *lld::wasm::functionTableName = "__indirect_function_table";