Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/Writer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Writer.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 "Writer.h"
10
#include "Config.h"
11
#include "InputChunks.h"
12
#include "InputEvent.h"
13
#include "InputGlobal.h"
14
#include "OutputSections.h"
15
#include "OutputSegment.h"
16
#include "Relocations.h"
17
#include "SymbolTable.h"
18
#include "SyntheticSections.h"
19
#include "WriterUtils.h"
20
#include "lld/Common/ErrorHandler.h"
21
#include "lld/Common/Memory.h"
22
#include "lld/Common/Strings.h"
23
#include "lld/Common/Threads.h"
24
#include "llvm/ADT/DenseSet.h"
25
#include "llvm/ADT/SmallSet.h"
26
#include "llvm/ADT/SmallVector.h"
27
#include "llvm/ADT/StringMap.h"
28
#include "llvm/BinaryFormat/Wasm.h"
29
#include "llvm/Object/WasmTraits.h"
30
#include "llvm/Support/FileOutputBuffer.h"
31
#include "llvm/Support/Format.h"
32
#include "llvm/Support/FormatVariadic.h"
33
#include "llvm/Support/LEB128.h"
34
35
#include <cstdarg>
36
#include <map>
37
38
#define DEBUG_TYPE "lld"
39
40
using namespace llvm;
41
using namespace llvm::wasm;
42
using namespace lld;
43
using namespace lld::wasm;
44
45
static constexpr int stackAlignment = 16;
46
47
namespace {
48
49
// The writer writes a SymbolTable result to a file.
50
class Writer {
51
public:
52
  void run();
53
54
private:
55
  void openFile();
56
57
  void createInitMemoryFunction();
58
  void createApplyRelocationsFunction();
59
  void createCallCtorsFunction();
60
  void createInitTLSFunction();
61
62
  void assignIndexes();
63
  void populateSymtab();
64
  void populateProducers();
65
  void populateTargetFeatures();
66
  void calculateInitFunctions();
67
  void calculateImports();
68
  void calculateExports();
69
  void calculateCustomSections();
70
  void calculateTypes();
71
  void createOutputSegments();
72
  void layoutMemory();
73
  void createHeader();
74
75
  void addSection(OutputSection *sec);
76
77
  void addSections();
78
79
  void createCustomSections();
80
  void createSyntheticSections();
81
  void finalizeSections();
82
83
  // Custom sections
84
  void createRelocSections();
85
86
  void writeHeader();
87
  void writeSections();
88
89
  uint64_t fileSize = 0;
90
  uint32_t tableBase = 0;
91
92
  std::vector<WasmInitEntry> initFunctions;
93
  llvm::StringMap<std::vector<InputSection *>> customSectionMapping;
94
95
  // Elements that are used to construct the final output
96
  std::string header;
97
  std::vector<OutputSection *> outputSections;
98
99
  std::unique_ptr<FileOutputBuffer> buffer;
100
101
  std::vector<OutputSegment *> segments;
102
  llvm::SmallDenseMap<StringRef, OutputSegment *> segmentMap;
103
};
104
105
} // anonymous namespace
106
107
172
void Writer::calculateCustomSections() {
108
172
  log("calculateCustomSections");
109
172
  bool stripDebug = config->stripDebug || 
config->stripAll168
;
110
263
  for (ObjFile *file : symtab->objectFiles) {
111
483
    for (InputSection *section : file->customSections) {
112
483
      StringRef name = section->getName();
113
483
      // These custom sections are known the linker and synthesized rather than
114
483
      // blindly copied
115
483
      if (name == "linking" || 
name == "name"220
||
name == "producers"220
||
116
483
          
name == "target_features"217
||
name.startswith("reloc.")171
)
117
449
        continue;
118
34
      // .. or it is a debug section
119
34
      if (stripDebug && 
name.startswith(".debug_")0
)
120
0
        continue;
121
34
      customSectionMapping[name].push_back(section);
122
34
    }
123
263
  }
124
172
}
125
126
172
void Writer::createCustomSections() {
127
172
  log("createCustomSections");
128
172
  for (auto &pair : customSectionMapping) {
129
22
    StringRef name = pair.first();
130
22
    LLVM_DEBUG(dbgs() << "createCustomSection: " << name << "\n");
131
22
132
22
    OutputSection *sec = make<CustomSection>(name, pair.second);
133
22
    if (config->relocatable || 
config->emitRelocs18
) {
134
4
      auto *sym = make<OutputSectionSymbol>(sec);
135
4
      out.linkingSec->addToSymtab(sym);
136
4
      sec->sectionSym = sym;
137
4
    }
138
22
    addSection(sec);
139
22
  }
140
172
}
141
142
// Create relocations sections in the final output.
143
// These are only created when relocatable output is requested.
144
23
void Writer::createRelocSections() {
145
23
  log("createRelocSections");
146
23
  // Don't use iterator here since we are adding to OutputSection
147
23
  size_t origSize = outputSections.size();
148
184
  for (size_t i = 0; i < origSize; 
i++161
) {
149
161
    LLVM_DEBUG(dbgs() << "check section " << i << "\n");
150
161
    OutputSection *sec = outputSections[i];
151
161
152
161
    // Count the number of needed sections.
153
161
    uint32_t count = sec->getNumRelocations();
154
161
    if (!count)
155
144
      continue;
156
17
157
17
    StringRef name;
158
17
    if (sec->type == WASM_SEC_DATA)
159
3
      name = "reloc.DATA";
160
14
    else if (sec->type == WASM_SEC_CODE)
161
13
      name = "reloc.CODE";
162
1
    else if (sec->type == WASM_SEC_CUSTOM)
163
1
      name = saver.save("reloc." + sec->name);
164
1
    else
165
1
      
llvm_unreachable0
(
166
17
          "relocations only supported for code, data, or custom sections");
167
17
168
17
    addSection(make<RelocSection>(name, sec));
169
17
  }
170
23
}
171
172
193
void Writer::populateProducers() {
173
289
  for (ObjFile *file : symtab->objectFiles) {
174
289
    const WasmProducerInfo &info = file->getWasmObj()->getProducerInfo();
175
289
    out.producersSec->addInfo(info);
176
289
  }
177
193
}
178
179
172
void Writer::writeHeader() {
180
172
  memcpy(buffer->getBufferStart(), header.data(), header.size());
181
172
}
182
183
void Writer::writeSections() {
184
  uint8_t *buf = buffer->getBufferStart();
185
1.42k
  parallelForEach(outputSections, [buf](OutputSection *s) {
186
1.42k
    assert(s->isNeeded());
187
1.42k
    s->writeTo(buf);
188
1.42k
  });
189
}
190
191
// Fix the memory layout of the output binary.  This assigns memory offsets
192
// to each of the input data sections as well as the explicit stack region.
193
// The default memory layout is as follows, from low to high.
194
//
195
//  - initialized data (starting at Config->globalBase)
196
//  - BSS data (not currently implemented in llvm)
197
//  - explicit stack (Config->ZStackSize)
198
//  - heap start / unallocated
199
//
200
// The --stack-first option means that stack is placed before any static data.
201
// This can be useful since it means that stack overflow traps immediately
202
// rather than overwriting global data, but also increases code size since all
203
// static data loads and stores requires larger offsets.
204
193
void Writer::layoutMemory() {
205
193
  uint32_t memoryPtr = 0;
206
193
207
193
  auto placeStack = [&]() {
208
189
    if (config->relocatable || 
config->isPic169
)
209
22
      return;
210
167
    memoryPtr = alignTo(memoryPtr, stackAlignment);
211
167
    if (config->zStackSize != alignTo(config->zStackSize, stackAlignment))
212
1
      error("stack size must be " + Twine(stackAlignment) + "-byte aligned");
213
167
    log("mem: stack size  = " + Twine(config->zStackSize));
214
167
    log("mem: stack base  = " + Twine(memoryPtr));
215
167
    memoryPtr += config->zStackSize;
216
167
    auto *sp = cast<DefinedGlobal>(WasmSym::stackPointer);
217
167
    sp->global->global.InitExpr.Value.Int32 = memoryPtr;
218
167
    log("mem: stack top   = " + Twine(memoryPtr));
219
167
  };
220
193
221
193
  if (config->stackFirst) {
222
1
    placeStack();
223
192
  } else {
224
192
    memoryPtr = config->globalBase;
225
192
    log("mem: global base = " + Twine(config->globalBase));
226
192
  }
227
193
228
193
  if (WasmSym::globalBase)
229
4
    WasmSym::globalBase->setVirtualAddress(config->globalBase);
230
193
231
193
  uint32_t dataStart = memoryPtr;
232
193
233
193
  // Arbitrarily set __dso_handle handle to point to the start of the data
234
193
  // segments.
235
193
  if (WasmSym::dsoHandle)
236
173
    WasmSym::dsoHandle->setVirtualAddress(dataStart);
237
193
238
193
  out.dylinkSec->memAlign = 0;
239
193
  for (OutputSegment *seg : segments) {
240
102
    out.dylinkSec->memAlign = std::max(out.dylinkSec->memAlign, seg->alignment);
241
102
    memoryPtr = alignTo(memoryPtr, 1ULL << seg->alignment);
242
102
    seg->startVA = memoryPtr;
243
102
    log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}", seg->name,
244
102
                memoryPtr, seg->size, seg->alignment));
245
102
    memoryPtr += seg->size;
246
102
247
102
    if (WasmSym::tlsSize && 
seg->name == ".tdata"27
) {
248
3
      auto *tlsSize = cast<DefinedGlobal>(WasmSym::tlsSize);
249
3
      tlsSize->global->global.InitExpr.Value.Int32 = seg->size;
250
3
251
3
      auto *tlsAlign = cast<DefinedGlobal>(WasmSym::tlsAlign);
252
3
      tlsAlign->global->global.InitExpr.Value.Int32 = 1U << seg->alignment;
253
3
    }
254
102
  }
255
193
256
193
  // TODO: Add .bss space here.
257
193
  if (WasmSym::dataEnd)
258
7
    WasmSym::dataEnd->setVirtualAddress(memoryPtr);
259
193
260
193
  log("mem: static data = " + Twine(memoryPtr - dataStart));
261
193
262
193
  if (config->shared) {
263
4
    out.dylinkSec->memSize = memoryPtr;
264
4
    return;
265
4
  }
266
189
267
189
  if (!config->stackFirst)
268
188
    placeStack();
269
189
270
189
  // Set `__heap_base` to directly follow the end of the stack or global data.
271
189
  // The fact that this comes last means that a malloc/brk implementation
272
189
  // can grow the heap at runtime.
273
189
  log("mem: heap base   = " + Twine(memoryPtr));
274
189
  if (WasmSym::heapBase)
275
4
    WasmSym::heapBase->setVirtualAddress(memoryPtr);
276
189
277
189
  if (config->initialMemory != 0) {
278
5
    if (config->initialMemory != alignTo(config->initialMemory, WasmPageSize))
279
0
      error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned");
280
5
    if (memoryPtr > config->initialMemory)
281
0
      error("initial memory too small, " + Twine(memoryPtr) + " bytes needed");
282
5
    else
283
5
      memoryPtr = config->initialMemory;
284
5
  }
285
189
  out.dylinkSec->memSize = memoryPtr;
286
189
  out.memorySec->numMemoryPages =
287
189
      alignTo(memoryPtr, WasmPageSize) / WasmPageSize;
288
189
  log("mem: total pages = " + Twine(out.memorySec->numMemoryPages));
289
189
290
189
  // Check max if explicitly supplied or required by shared memory
291
189
  if (config->maxMemory != 0 || 
config->sharedMemory170
) {
292
21
    if (config->maxMemory != alignTo(config->maxMemory, WasmPageSize))
293
2
      error("maximum memory must be " + Twine(WasmPageSize) + "-byte aligned");
294
21
    if (memoryPtr > config->maxMemory)
295
2
      error("maximum memory too small, " + Twine(memoryPtr) + " bytes needed");
296
21
    out.memorySec->maxMemoryPages = config->maxMemory / WasmPageSize;
297
21
    log("mem: max pages   = " + Twine(out.memorySec->maxMemoryPages));
298
21
  }
299
189
}
300
301
2.96k
void Writer::addSection(OutputSection *sec) {
302
2.96k
  if (!sec->isNeeded())
303
1.53k
    return;
304
1.42k
  log("addSection: " + toString(*sec));
305
1.42k
  sec->sectionIndex = outputSections.size();
306
1.42k
  outputSections.push_back(sec);
307
1.42k
}
308
309
// If a section name is valid as a C identifier (which is rare because of
310
// the leading '.'), linkers are expected to define __start_<secname> and
311
// __stop_<secname> symbols. They are at beginning and end of the section,
312
// respectively. This is not requested by the ELF standard, but GNU ld and
313
// gold provide the feature, and used by many programs.
314
83
static void addStartStopSymbols(const OutputSegment *seg) {
315
83
  StringRef name = seg->name;
316
83
  if (!isValidCIdentifier(name))
317
82
    return;
318
1
  LLVM_DEBUG(dbgs() << "addStartStopSymbols: " << name << "\n");
319
1
  uint32_t start = seg->startVA;
320
1
  uint32_t stop = start + seg->size;
321
1
  symtab->addOptionalDataSymbol(saver.save("__start_" + name), start);
322
1
  symtab->addOptionalDataSymbol(saver.save("__stop_" + name), stop);
323
1
}
324
325
172
void Writer::addSections() {
326
172
  addSection(out.dylinkSec);
327
172
  addSection(out.typeSec);
328
172
  addSection(out.importSec);
329
172
  addSection(out.functionSec);
330
172
  addSection(out.tableSec);
331
172
  addSection(out.memorySec);
332
172
  addSection(out.globalSec);
333
172
  addSection(out.eventSec);
334
172
  addSection(out.exportSec);
335
172
  addSection(out.elemSec);
336
172
  addSection(out.dataCountSec);
337
172
338
172
  addSection(make<CodeSection>(out.functionSec->inputFunctions));
339
172
  addSection(make<DataSection>(segments));
340
172
341
172
  createCustomSections();
342
172
343
172
  addSection(out.linkingSec);
344
172
  if (config->emitRelocs || 
config->relocatable169
) {
345
23
    createRelocSections();
346
23
  }
347
172
348
172
  addSection(out.nameSec);
349
172
  addSection(out.producersSec);
350
172
  addSection(out.targetFeaturesSec);
351
172
}
352
353
172
void Writer::finalizeSections() {
354
1.42k
  for (OutputSection *s : outputSections) {
355
1.42k
    s->setOffset(fileSize);
356
1.42k
    s->finalizeContents();
357
1.42k
    fileSize += s->getSize();
358
1.42k
  }
359
172
}
360
361
193
void Writer::populateTargetFeatures() {
362
193
  StringMap<std::string> used;
363
193
  StringMap<std::string> required;
364
193
  StringMap<std::string> disallowed;
365
193
  bool tlsUsed = false;
366
193
367
193
  // Only infer used features if user did not specify features
368
193
  bool inferFeatures = !config->features.hasValue();
369
193
370
193
  if (!inferFeatures) {
371
14
    for (auto &feature : config->features.getValue())
372
31
      out.targetFeaturesSec->features.insert(feature);
373
14
    // No need to read or check features
374
14
    if (!config->checkFeatures)
375
2
      return;
376
191
  }
377
191
378
191
  // Find the sets of used, required, and disallowed features
379
287
  
for (ObjFile *file : symtab->objectFiles)191
{
380
287
    StringRef fileName(file->getName());
381
287
    for (auto &feature : file->getWasmObj()->getTargetFeatures()) {
382
55
      switch (feature.Prefix) {
383
55
      case WASM_FEATURE_PREFIX_USED:
384
34
        used.insert({feature.Name, fileName});
385
34
        break;
386
55
      case WASM_FEATURE_PREFIX_REQUIRED:
387
9
        used.insert({feature.Name, fileName});
388
9
        required.insert({feature.Name, fileName});
389
9
        break;
390
55
      case WASM_FEATURE_PREFIX_DISALLOWED:
391
12
        disallowed.insert({feature.Name, fileName});
392
12
        break;
393
55
      default:
394
0
        error("Unrecognized feature policy prefix " +
395
0
              std::to_string(feature.Prefix));
396
55
      }
397
55
    }
398
287
399
287
    for (InputSegment *segment : file->segments) {
400
189
      if (!segment->live)
401
15
        continue;
402
174
      StringRef name = segment->getName();
403
174
      if (name.startswith(".tdata") || 
name.startswith(".tbss")168
)
404
6
        tlsUsed = true;
405
174
    }
406
287
  }
407
191
408
191
  if (inferFeatures)
409
179
    out.targetFeaturesSec->features.insert(used.keys().begin(),
410
179
                                           used.keys().end());
411
191
412
191
  if (out.targetFeaturesSec->features.count("atomics") &&
413
191
      
!config->sharedMemory10
) {
414
1
    if (inferFeatures)
415
0
      error(Twine("'atomics' feature is used by ") + used["atomics"] +
416
0
            ", so --shared-memory must be used");
417
1
    else
418
1
      error("'atomics' feature is used, so --shared-memory must be used");
419
1
  }
420
191
421
191
  if (!config->checkFeatures)
422
3
    return;
423
188
424
188
  if (disallowed.count("atomics") && 
config->sharedMemory3
)
425
1
    error("'atomics' feature is disallowed by " + disallowed["atomics"] +
426
1
          ", so --shared-memory must not be used");
427
188
428
188
  if (!used.count("bulk-memory") && 
config->passiveSegments175
)
429
3
    error("'bulk-memory' feature must be used in order to emit passive "
430
3
          "segments");
431
188
432
188
  if (!used.count("bulk-memory") && 
tlsUsed175
)
433
0
    error("'bulk-memory' feature must be used in order to use thread-local "
434
0
          "storage");
435
188
436
188
  // Validate that used features are allowed in output
437
188
  if (!inferFeatures) {
438
12
    for (auto &feature : used.keys()) {
439
6
      if (!out.targetFeaturesSec->features.count(feature))
440
2
        error(Twine("Target feature '") + feature + "' used by " +
441
2
              used[feature] + " is not allowed.");
442
6
    }
443
12
  }
444
188
445
188
  // Validate the required and disallowed constraints for each file
446
281
  for (ObjFile *file : symtab->objectFiles) {
447
281
    StringRef fileName(file->getName());
448
281
    SmallSet<std::string, 8> objectFeatures;
449
281
    for (auto &feature : file->getWasmObj()->getTargetFeatures()) {
450
50
      if (feature.Prefix == WASM_FEATURE_PREFIX_DISALLOWED)
451
10
        continue;
452
40
      objectFeatures.insert(feature.Name);
453
40
      if (disallowed.count(feature.Name))
454
2
        error(Twine("Target feature '") + feature.Name + "' used in " +
455
2
              fileName + " is disallowed by " + disallowed[feature.Name] +
456
2
              ". Use --no-check-features to suppress.");
457
40
    }
458
281
    for (auto &feature : required.keys()) {
459
10
      if (!objectFeatures.count(feature))
460
2
        error(Twine("Missing target feature '") + feature + "' in " + fileName +
461
2
              ", required by " + required[feature] +
462
2
              ". Use --no-check-features to suppress.");
463
10
    }
464
281
  }
465
188
}
466
467
193
void Writer::calculateImports() {
468
1.32k
  for (Symbol *sym : symtab->getSymbols()) {
469
1.32k
    if (!sym->isUndefined())
470
1.25k
      continue;
471
74
    if (sym->isWeak() && 
!config->relocatable3
)
472
1
      continue;
473
73
    if (!sym->isLive())
474
9
      continue;
475
64
    if (!sym->isUsedInRegularObj)
476
1
      continue;
477
63
    // We don't generate imports for data symbols. They however can be imported
478
63
    // as GOT entries.
479
63
    if (isa<DataSymbol>(sym))
480
11
      continue;
481
52
482
52
    LLVM_DEBUG(dbgs() << "import: " << sym->getName() << "\n");
483
52
    out.importSec->addImport(sym);
484
52
  }
485
193
}
486
487
172
void Writer::calculateExports() {
488
172
  if (config->relocatable)
489
20
    return;
490
152
491
152
  if (!config->relocatable && !config->importMemory)
492
146
    out.exportSec->exports.push_back(
493
146
        WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0});
494
152
495
152
  if (!config->relocatable && config->exportTable)
496
1
    out.exportSec->exports.push_back(
497
1
        WasmExport{functionTableName, WASM_EXTERNAL_TABLE, 0});
498
152
499
152
  unsigned fakeGlobalIndex = out.importSec->getNumImportedGlobals() +
500
152
                             out.globalSec->inputGlobals.size();
501
152
502
972
  for (Symbol *sym : symtab->getSymbols()) {
503
972
    if (!sym->isExported())
504
747
      continue;
505
225
    if (!sym->isLive())
506
0
      continue;
507
225
508
225
    StringRef name = sym->getName();
509
225
    WasmExport export_;
510
225
    if (auto *f = dyn_cast<DefinedFunction>(sym)) {
511
184
      export_ = {name, WASM_EXTERNAL_FUNCTION, f->getFunctionIndex()};
512
184
    } else 
if (auto *41
g41
= dyn_cast<DefinedGlobal>(sym)) {
513
2
      // TODO(sbc): Remove this check once to mutable global proposal is
514
2
      // implement in all major browsers.
515
2
      // See: https://github.com/WebAssembly/mutable-global
516
2
      if (g->getGlobalType()->Mutable) {
517
2
        // Only __stack_pointer and __tls_base should ever be create as mutable.
518
2
        assert(g == WasmSym::stackPointer || g == WasmSym::tlsBase);
519
2
        continue;
520
2
      }
521
0
      export_ = {name, WASM_EXTERNAL_GLOBAL, g->getGlobalIndex()};
522
39
    } else if (auto *e = dyn_cast<DefinedEvent>(sym)) {
523
0
      export_ = {name, WASM_EXTERNAL_EVENT, e->getEventIndex()};
524
39
    } else {
525
39
      auto *d = cast<DefinedData>(sym);
526
39
      out.globalSec->definedFakeGlobals.emplace_back(d);
527
39
      export_ = {name, WASM_EXTERNAL_GLOBAL, fakeGlobalIndex++};
528
39
    }
529
225
530
225
    
LLVM_DEBUG223
(dbgs() << "Export: " << name << "\n");
531
223
    out.exportSec->exports.push_back(export_);
532
223
  }
533
152
}
534
535
172
void Writer::populateSymtab() {
536
172
  if (!config->relocatable && 
!config->emitRelocs152
)
537
149
    return;
538
23
539
23
  for (Symbol *sym : symtab->getSymbols())
540
243
    if (sym->isUsedInRegularObj && 
sym->isLive()228
)
541
227
      out.linkingSec->addToSymtab(sym);
542
23
543
43
  for (ObjFile *file : symtab->objectFiles) {
544
43
    LLVM_DEBUG(dbgs() << "Local symtab entries: " << file->getName() << "\n");
545
43
    for (Symbol *sym : file->getSymbols())
546
258
      if (sym->isLocal() && 
!isa<SectionSymbol>(sym)24
&&
sym->isLive()22
)
547
22
        out.linkingSec->addToSymtab(sym);
548
43
  }
549
23
}
550
551
172
void Writer::calculateTypes() {
552
172
  // The output type section is the union of the following sets:
553
172
  // 1. Any signature used in the TYPE relocation
554
172
  // 2. The signatures of all imported functions
555
172
  // 3. The signatures of all defined functions
556
172
  // 4. The signatures of all imported events
557
172
  // 5. The signatures of all defined events
558
172
559
263
  for (ObjFile *file : symtab->objectFiles) {
560
263
    ArrayRef<WasmSignature> types = file->getWasmObj()->types();
561
559
    for (uint32_t i = 0; i < types.size(); 
i++296
)
562
296
      if (file->typeIsUsed[i])
563
16
        file->typeMap[i] = out.typeSec->registerType(types[i]);
564
263
  }
565
172
566
172
  for (const Symbol *sym : out.importSec->importedSymbols) {
567
46
    if (auto *f = dyn_cast<FunctionSymbol>(sym))
568
26
      out.typeSec->registerType(*f->signature);
569
20
    else if (auto *e = dyn_cast<EventSymbol>(sym))
570
0
      out.typeSec->registerType(*e->signature);
571
46
  }
572
172
573
172
  for (const InputFunction *f : out.functionSec->inputFunctions)
574
533
    out.typeSec->registerType(f->signature);
575
172
576
172
  for (const InputEvent *e : out.eventSec->inputEvents)
577
1
    out.typeSec->registerType(e->signature);
578
172
}
579
580
193
static void scanRelocations() {
581
289
  for (ObjFile *file : symtab->objectFiles) {
582
289
    LLVM_DEBUG(dbgs() << "scanRelocations: " << file->getName() << "\n");
583
289
    for (InputChunk *chunk : file->functions)
584
509
      scanRelocations(chunk);
585
289
    for (InputChunk *chunk : file->segments)
586
189
      scanRelocations(chunk);
587
289
    for (auto &p : file->customSections)
588
529
      scanRelocations(p);
589
289
  }
590
193
}
591
592
193
void Writer::assignIndexes() {
593
193
  // Seal the import section, since other index spaces such as function and
594
193
  // global are effected by the number of imports.
595
193
  out.importSec->seal();
596
193
597
193
  for (InputFunction *func : symtab->syntheticFunctions)
598
221
    out.functionSec->addFunction(func);
599
193
600
289
  for (ObjFile *file : symtab->objectFiles) {
601
289
    LLVM_DEBUG(dbgs() << "Functions: " << file->getName() << "\n");
602
289
    for (InputFunction *func : file->functions)
603
509
      out.functionSec->addFunction(func);
604
289
  }
605
193
606
193
  for (InputGlobal *global : symtab->syntheticGlobals)
607
221
    out.globalSec->addGlobal(global);
608
193
609
289
  for (ObjFile *file : symtab->objectFiles) {
610
289
    LLVM_DEBUG(dbgs() << "Globals: " << file->getName() << "\n");
611
289
    for (InputGlobal *global : file->globals)
612
4
      out.globalSec->addGlobal(global);
613
289
  }
614
193
615
289
  for (ObjFile *file : symtab->objectFiles) {
616
289
    LLVM_DEBUG(dbgs() << "Events: " << file->getName() << "\n");
617
289
    for (InputEvent *event : file->events)
618
2
      out.eventSec->addEvent(event);
619
289
  }
620
193
}
621
622
174
static StringRef getOutputDataSegmentName(StringRef name) {
623
174
  // With PIC code we currently only support a single data segment since
624
174
  // we only have a single __memory_base to use as our base address.
625
174
  if (config->isPic)
626
12
    return ".data";
627
162
  // We only support one thread-local segment, so we must merge the segments
628
162
  // despite --no-merge-data-segments.
629
162
  // We also need to merge .tbss into .tdata so they share the same offsets.
630
162
  if (name.startswith(".tdata") || 
name.startswith(".tbss")156
)
631
6
    return ".tdata";
632
156
  if (!config->mergeDataSegments)
633
35
    return name;
634
121
  if (name.startswith(".text."))
635
0
    return ".text";
636
121
  if (name.startswith(".data."))
637
73
    return ".data";
638
48
  if (name.startswith(".bss."))
639
20
    return ".bss";
640
28
  if (name.startswith(".rodata."))
641
26
    return ".rodata";
642
2
  return name;
643
2
}
644
645
193
void Writer::createOutputSegments() {
646
289
  for (ObjFile *file : symtab->objectFiles) {
647
289
    for (InputSegment *segment : file->segments) {
648
189
      if (!segment->live)
649
15
        continue;
650
174
      StringRef name = getOutputDataSegmentName(segment->getName());
651
174
      OutputSegment *&s = segmentMap[name];
652
174
      if (s == nullptr) {
653
102
        LLVM_DEBUG(dbgs() << "new segment: " << name << "\n");
654
102
        s = make<OutputSegment>(name, segments.size());
655
102
        if (config->passiveSegments || 
name == ".tdata"73
)
656
29
          s->initFlags = WASM_SEGMENT_IS_PASSIVE;
657
102
        segments.push_back(s);
658
102
      }
659
174
      s->addInputSegment(segment);
660
174
      LLVM_DEBUG(dbgs() << "added data: " << name << ": " << s->size << "\n");
661
174
    }
662
289
  }
663
193
}
664
665
68
static void createFunction(DefinedFunction *func, StringRef bodyContent) {
666
68
  std::string functionBody;
667
68
  {
668
68
    raw_string_ostream os(functionBody);
669
68
    writeUleb128(os, bodyContent.size(), "function size");
670
68
    os << bodyContent;
671
68
  }
672
68
  ArrayRef<uint8_t> body = arrayRefFromStringRef(saver.save(functionBody));
673
68
  cast<SyntheticFunction>(func->function)->setBody(body);
674
68
}
675
676
11
void Writer::createInitMemoryFunction() {
677
11
  LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n");
678
11
  std::string bodyContent;
679
11
  {
680
11
    raw_string_ostream os(bodyContent);
681
11
    writeUleb128(os, 0, "num locals");
682
11
683
11
    // initialize passive data segments
684
29
    for (const OutputSegment *s : segments) {
685
29
      if (s->initFlags & WASM_SEGMENT_IS_PASSIVE && s->name != ".tdata") {
686
26
        // destination address
687
26
        writeU8(os, WASM_OPCODE_I32_CONST, "i32.const");
688
26
        writeSleb128(os, s->startVA, "destination address");
689
26
        // source segment offset
690
26
        writeU8(os, WASM_OPCODE_I32_CONST, "i32.const");
691
26
        writeSleb128(os, 0, "segment offset");
692
26
        // memory region size
693
26
        writeU8(os, WASM_OPCODE_I32_CONST, "i32.const");
694
26
        writeSleb128(os, s->size, "memory region size");
695
26
        // memory.init instruction
696
26
        writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
697
26
        writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "MEMORY.INIT");
698
26
        writeUleb128(os, s->index, "segment index immediate");
699
26
        writeU8(os, 0, "memory index immediate");
700
26
        // data.drop instruction
701
26
        writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
702
26
        writeUleb128(os, WASM_OPCODE_DATA_DROP, "DATA.DROP");
703
26
        writeUleb128(os, s->index, "segment index immediate");
704
26
      }
705
29
    }
706
11
    writeU8(os, WASM_OPCODE_END, "END");
707
11
  }
708
11
709
11
  createFunction(WasmSym::initMemory, bodyContent);
710
11
}
711
712
// For -shared (PIC) output, we create create a synthetic function which will
713
// apply any relocations to the data segments on startup.  This function is
714
// called __wasm_apply_relocs and is added at the beginning of __wasm_call_ctors
715
// before any of the constructors run.
716
6
void Writer::createApplyRelocationsFunction() {
717
6
  LLVM_DEBUG(dbgs() << "createApplyRelocationsFunction\n");
718
6
  // First write the body's contents to a string.
719
6
  std::string bodyContent;
720
6
  {
721
6
    raw_string_ostream os(bodyContent);
722
6
    writeUleb128(os, 0, "num locals");
723
6
    for (const OutputSegment *seg : segments)
724
4
      for (const InputSegment *inSeg : seg->inputSegments)
725
12
        inSeg->generateRelocationCode(os);
726
6
    writeU8(os, WASM_OPCODE_END, "END");
727
6
  }
728
6
729
6
  createFunction(WasmSym::applyRelocs, bodyContent);
730
6
}
731
732
// Create synthetic "__wasm_call_ctors" function based on ctor functions
733
// in input object.
734
173
void Writer::createCallCtorsFunction() {
735
173
  if (!WasmSym::callCtors->isLive())
736
132
    return;
737
41
738
41
  // First write the body's contents to a string.
739
41
  std::string bodyContent;
740
41
  {
741
41
    raw_string_ostream os(bodyContent);
742
41
    writeUleb128(os, 0, "num locals");
743
41
744
41
    if (config->passiveSegments) {
745
10
      writeU8(os, WASM_OPCODE_CALL, "CALL");
746
10
      writeUleb128(os, WasmSym::initMemory->getFunctionIndex(),
747
10
                   "function index");
748
10
    }
749
41
750
41
    if (config->isPic) {
751
6
      writeU8(os, WASM_OPCODE_CALL, "CALL");
752
6
      writeUleb128(os, WasmSym::applyRelocs->getFunctionIndex(),
753
6
                   "function index");
754
6
    }
755
41
756
41
    // Call constructors
757
41
    for (const WasmInitEntry &f : initFunctions) {
758
16
      writeU8(os, WASM_OPCODE_CALL, "CALL");
759
16
      writeUleb128(os, f.sym->getFunctionIndex(), "function index");
760
16
    }
761
41
    writeU8(os, WASM_OPCODE_END, "END");
762
41
  }
763
41
764
41
  createFunction(WasmSym::callCtors, bodyContent);
765
41
}
766
767
18
void Writer::createInitTLSFunction() {
768
18
  if (!WasmSym::initTLS->isLive())
769
8
    return;
770
10
771
10
  std::string bodyContent;
772
10
  {
773
10
    raw_string_ostream os(bodyContent);
774
10
775
10
    OutputSegment *tlsSeg = nullptr;
776
27
    for (auto *seg : segments) {
777
27
      if (seg->name == ".tdata") {
778
3
        tlsSeg = seg;
779
3
        break;
780
3
      }
781
27
    }
782
10
783
10
    writeUleb128(os, 0, "num locals");
784
10
    if (tlsSeg) {
785
3
      writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
786
3
      writeUleb128(os, 0, "local index");
787
3
788
3
      writeU8(os, WASM_OPCODE_GLOBAL_SET, "global.set");
789
3
      writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "global index");
790
3
791
3
      writeU8(os, WASM_OPCODE_LOCAL_GET, "local.get");
792
3
      writeUleb128(os, 0, "local index");
793
3
794
3
      writeU8(os, WASM_OPCODE_I32_CONST, "i32.const");
795
3
      writeSleb128(os, 0, "segment offset");
796
3
797
3
      writeU8(os, WASM_OPCODE_I32_CONST, "i32.const");
798
3
      writeSleb128(os, tlsSeg->size, "memory region size");
799
3
800
3
      writeU8(os, WASM_OPCODE_MISC_PREFIX, "bulk-memory prefix");
801
3
      writeUleb128(os, WASM_OPCODE_MEMORY_INIT, "MEMORY.INIT");
802
3
      writeUleb128(os, tlsSeg->index, "segment index immediate");
803
3
      writeU8(os, 0, "memory index immediate");
804
3
    }
805
10
    writeU8(os, WASM_OPCODE_END, "end function");
806
10
  }
807
10
808
10
  createFunction(WasmSym::initTLS, bodyContent);
809
10
}
810
811
// Populate InitFunctions vector with init functions from all input objects.
812
// This is then used either when creating the output linking section or to
813
// synthesize the "__wasm_call_ctors" function.
814
193
void Writer::calculateInitFunctions() {
815
193
  if (!config->relocatable && 
!WasmSym::callCtors->isLive()173
)
816
132
    return;
817
61
818
94
  
for (ObjFile *file : symtab->objectFiles)61
{
819
94
    const WasmLinkingData &l = file->getWasmObj()->linkingData();
820
94
    for (const WasmInitFunc &f : l.InitFunctions) {
821
30
      FunctionSymbol *sym = file->getFunctionSymbol(f.Symbol);
822
30
      // comdat exclusions can cause init functions be discarded.
823
30
      if (sym->isDiscarded())
824
1
        continue;
825
29
      assert(sym->isLive());
826
29
      if (*sym->signature != WasmSignature{{}, {}})
827
0
        error("invalid signature for init func: " + toString(*sym));
828
29
      LLVM_DEBUG(dbgs() << "initFunctions: " << toString(*sym) << "\n");
829
29
      initFunctions.emplace_back(WasmInitEntry{sym, f.Priority});
830
29
    }
831
94
  }
832
61
833
61
  // Sort in order of priority (lowest first) so that they are called
834
61
  // in the correct order.
835
61
  llvm::stable_sort(initFunctions,
836
65
                    [](const WasmInitEntry &l, const WasmInitEntry &r) {
837
65
                      return l.priority < r.priority;
838
65
                    });
839
61
}
840
841
193
void Writer::createSyntheticSections() {
842
193
  out.dylinkSec = make<DylinkSection>();
843
193
  out.typeSec = make<TypeSection>();
844
193
  out.importSec = make<ImportSection>();
845
193
  out.functionSec = make<FunctionSection>();
846
193
  out.tableSec = make<TableSection>();
847
193
  out.memorySec = make<MemorySection>();
848
193
  out.globalSec = make<GlobalSection>();
849
193
  out.eventSec = make<EventSection>();
850
193
  out.exportSec = make<ExportSection>();
851
193
  out.elemSec = make<ElemSection>(tableBase);
852
193
  out.dataCountSec = make<DataCountSection>(segments.size());
853
193
  out.linkingSec = make<LinkingSection>(initFunctions, segments);
854
193
  out.nameSec = make<NameSection>();
855
193
  out.producersSec = make<ProducersSection>();
856
193
  out.targetFeaturesSec = make<TargetFeaturesSection>();
857
193
}
858
859
void Writer::run() {
860
  if (config->relocatable || config->isPic)
861
    config->globalBase = 0;
862
863
  // For PIC code the table base is assigned dynamically by the loader.
864
  // For non-PIC, we start at 1 so that accessing table index 0 always traps.
865
  if (!config->isPic)
866
    tableBase = 1;
867
868
  log("-- createOutputSegments");
869
  createOutputSegments();
870
  log("-- createSyntheticSections");
871
  createSyntheticSections();
872
  log("-- populateProducers");
873
  populateProducers();
874
  log("-- populateTargetFeatures");
875
  populateTargetFeatures();
876
  log("-- calculateImports");
877
  calculateImports();
878
  log("-- layoutMemory");
879
  layoutMemory();
880
881
  if (!config->relocatable) {
882
    // Create linker synthesized __start_SECNAME/__stop_SECNAME symbols
883
    // This has to be done after memory layout is performed.
884
    for (const OutputSegment *seg : segments)
885
      addStartStopSymbols(seg);
886
  }
887
888
  log("-- scanRelocations");
889
  scanRelocations();
890
  log("-- assignIndexes");
891
  assignIndexes();
892
  log("-- calculateInitFunctions");
893
  calculateInitFunctions();
894
895
  if (!config->relocatable) {
896
    // Create linker synthesized functions
897
    if (config->passiveSegments)
898
      createInitMemoryFunction();
899
    if (config->isPic)
900
      createApplyRelocationsFunction();
901
    createCallCtorsFunction();
902
  }
903
904
  if (config->sharedMemory && !config->shared)
905
    createInitTLSFunction();
906
907
  if (errorCount())
908
    return;
909
910
  log("-- calculateTypes");
911
  calculateTypes();
912
  log("-- calculateExports");
913
  calculateExports();
914
  log("-- calculateCustomSections");
915
  calculateCustomSections();
916
  log("-- populateSymtab");
917
  populateSymtab();
918
  log("-- addSections");
919
  addSections();
920
921
  if (errorHandler().verbose) {
922
    log("Defined Functions: " + Twine(out.functionSec->inputFunctions.size()));
923
    log("Defined Globals  : " + Twine(out.globalSec->inputGlobals.size()));
924
    log("Defined Events   : " + Twine(out.eventSec->inputEvents.size()));
925
    log("Function Imports : " +
926
        Twine(out.importSec->getNumImportedFunctions()));
927
    log("Global Imports   : " + Twine(out.importSec->getNumImportedGlobals()));
928
    log("Event Imports    : " + Twine(out.importSec->getNumImportedEvents()));
929
    for (ObjFile *file : symtab->objectFiles)
930
      file->dumpInfo();
931
  }
932
933
  createHeader();
934
  log("-- finalizeSections");
935
  finalizeSections();
936
937
  log("-- openFile");
938
  openFile();
939
  if (errorCount())
940
    return;
941
942
  writeHeader();
943
944
  log("-- writeSections");
945
  writeSections();
946
  if (errorCount())
947
    return;
948
949
  if (Error e = buffer->commit())
950
    fatal("failed to write the output file: " + toString(std::move(e)));
951
}
952
953
// Open a result file.
954
172
void Writer::openFile() {
955
172
  log("writing: " + config->outputFile);
956
172
957
172
  Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
958
172
      FileOutputBuffer::create(config->outputFile, fileSize,
959
172
                               FileOutputBuffer::F_executable);
960
172
961
172
  if (!bufferOrErr)
962
0
    error("failed to open " + config->outputFile + ": " +
963
0
          toString(bufferOrErr.takeError()));
964
172
  else
965
172
    buffer = std::move(*bufferOrErr);
966
172
}
967
968
172
void Writer::createHeader() {
969
172
  raw_string_ostream os(header);
970
172
  writeBytes(os, WasmMagic, sizeof(WasmMagic), "wasm magic");
971
172
  writeU32(os, WasmVersion, "wasm version");
972
172
  os.flush();
973
172
  fileSize += header.size();
974
172
}
975
976
193
void lld::wasm::writeResult() { Writer().run(); }