Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/InputChunks.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- InputChunks.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 "InputChunks.h"
10
#include "Config.h"
11
#include "OutputSegment.h"
12
#include "WriterUtils.h"
13
#include "lld/Common/ErrorHandler.h"
14
#include "lld/Common/LLVM.h"
15
#include "llvm/Support/LEB128.h"
16
17
#define DEBUG_TYPE "lld"
18
19
using namespace llvm;
20
using namespace llvm::wasm;
21
using namespace llvm::support::endian;
22
using namespace lld;
23
using namespace lld::wasm;
24
25
3
StringRef lld::relocTypeToString(uint8_t RelocType) {
26
3
  switch (RelocType) {
27
3
#define WASM_RELOC(NAME, REL)                                                  \
28
3
  case REL:                                                                    \
29
3
    return #NAME;
30
3
#include 
"llvm/BinaryFormat/WasmRelocs.def"0
31
3
#undef WASM_RELOC
32
3
  }
33
3
  
llvm_unreachable0
("unknown reloc type");
34
3
}
35
36
5
std::string lld::toString(const InputChunk *C) {
37
5
  return (toString(C->File) + ":(" + C->getName() + ")").str();
38
5
}
39
40
218
StringRef InputChunk::getComdatName() const {
41
218
  uint32_t Index = getComdat();
42
218
  if (Index == UINT32_MAX)
43
218
    
return StringRef()216
;
44
2
  return File->getWasmObj()->linkingData().Comdats[Index];
45
2
}
46
47
0
void InputChunk::verifyRelocTargets() const {
48
0
  for (const WasmRelocation &Rel : Relocations) {
49
0
    uint32_t ExistingValue;
50
0
    unsigned BytesRead = 0;
51
0
    uint32_t Offset = Rel.Offset - getInputSectionOffset();
52
0
    const uint8_t *Loc = data().data() + Offset;
53
0
    switch (Rel.Type) {
54
0
    case R_WASM_TYPE_INDEX_LEB:
55
0
    case R_WASM_FUNCTION_INDEX_LEB:
56
0
    case R_WASM_GLOBAL_INDEX_LEB:
57
0
    case R_WASM_EVENT_INDEX_LEB:
58
0
    case R_WASM_MEMORY_ADDR_LEB:
59
0
      ExistingValue = decodeULEB128(Loc, &BytesRead);
60
0
      break;
61
0
    case R_WASM_TABLE_INDEX_SLEB:
62
0
    case R_WASM_TABLE_INDEX_REL_SLEB:
63
0
    case R_WASM_MEMORY_ADDR_SLEB:
64
0
    case R_WASM_MEMORY_ADDR_REL_SLEB:
65
0
      ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead));
66
0
      break;
67
0
    case R_WASM_TABLE_INDEX_I32:
68
0
    case R_WASM_MEMORY_ADDR_I32:
69
0
    case R_WASM_FUNCTION_OFFSET_I32:
70
0
    case R_WASM_SECTION_OFFSET_I32:
71
0
      ExistingValue = static_cast<uint32_t>(read32le(Loc));
72
0
      break;
73
0
    default:
74
0
      llvm_unreachable("unknown relocation type");
75
0
    }
76
0
77
0
    if (BytesRead && BytesRead != 5)
78
0
      warn("expected LEB at relocation site be 5-byte padded");
79
0
80
0
    if (Rel.Type != R_WASM_GLOBAL_INDEX_LEB) {
81
0
      uint32_t ExpectedValue = File->calcExpectedValue(Rel);
82
0
      if (ExpectedValue != ExistingValue)
83
0
        warn("unexpected existing value for " + relocTypeToString(Rel.Type) +
84
0
             ": existing=" + Twine(ExistingValue) +
85
0
             " expected=" + Twine(ExpectedValue));
86
0
    }
87
0
  }
88
0
}
89
90
// Copy this input chunk to an mmap'ed output file and apply relocations.
91
595
void InputChunk::writeTo(uint8_t *Buf) const {
92
595
  // Copy contents
93
595
  memcpy(Buf + OutputOffset, data().data(), data().size());
94
595
95
595
  // Apply relocations
96
595
  if (Relocations.empty())
97
267
    return;
98
328
99
#ifndef NDEBUG
100
  verifyRelocTargets();
101
#endif
102
103
328
  LLVM_DEBUG(dbgs() << "applying relocations: " << getName()
104
328
                    << " count=" << Relocations.size() << "\n");
105
328
  int32_t Off = OutputOffset - getInputSectionOffset();
106
328
107
478
  for (const WasmRelocation &Rel : Relocations) {
108
478
    uint8_t *Loc = Buf + Rel.Offset + Off;
109
478
    uint32_t Value = File->calcNewValue(Rel);
110
478
    LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(Rel.Type)
111
478
                      << " addend=" << Rel.Addend << " index=" << Rel.Index
112
478
                      << " value=" << Value << " offset=" << Rel.Offset
113
478
                      << "\n");
114
478
115
478
    switch (Rel.Type) {
116
478
    case R_WASM_TYPE_INDEX_LEB:
117
313
    case R_WASM_FUNCTION_INDEX_LEB:
118
313
    case R_WASM_GLOBAL_INDEX_LEB:
119
313
    case R_WASM_EVENT_INDEX_LEB:
120
313
    case R_WASM_MEMORY_ADDR_LEB:
121
313
      encodeULEB128(Value, Loc, 5);
122
313
      break;
123
313
    case R_WASM_TABLE_INDEX_SLEB:
124
75
    case R_WASM_TABLE_INDEX_REL_SLEB:
125
75
    case R_WASM_MEMORY_ADDR_SLEB:
126
75
    case R_WASM_MEMORY_ADDR_REL_SLEB:
127
75
      encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
128
75
      break;
129
93
    case R_WASM_TABLE_INDEX_I32:
130
93
    case R_WASM_MEMORY_ADDR_I32:
131
93
    case R_WASM_FUNCTION_OFFSET_I32:
132
93
    case R_WASM_SECTION_OFFSET_I32:
133
93
      write32le(Loc, Value);
134
93
      break;
135
93
    default:
136
0
      llvm_unreachable("unknown relocation type");
137
478
    }
138
478
  }
139
328
}
140
141
// Copy relocation entries to a given output stream.
142
// This function is used only when a user passes "-r". For a regular link,
143
// we consume relocations instead of copying them to an output file.
144
201
void InputChunk::writeRelocations(raw_ostream &OS) const {
145
201
  if (Relocations.empty())
146
22
    return;
147
179
148
179
  int32_t Off = OutputOffset - getInputSectionOffset();
149
179
  LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName()
150
179
                    << " offset=" << Twine(Off) << "\n");
151
179
152
206
  for (const WasmRelocation &Rel : Relocations) {
153
206
    writeUleb128(OS, Rel.Type, "reloc type");
154
206
    writeUleb128(OS, Rel.Offset + Off, "reloc offset");
155
206
    writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
156
206
157
206
    switch (Rel.Type) {
158
206
    case R_WASM_MEMORY_ADDR_LEB:
159
151
    case R_WASM_MEMORY_ADDR_SLEB:
160
151
    case R_WASM_MEMORY_ADDR_I32:
161
151
    case R_WASM_FUNCTION_OFFSET_I32:
162
151
    case R_WASM_SECTION_OFFSET_I32:
163
151
      writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
164
151
      break;
165
206
    }
166
206
  }
167
179
}
168
169
475
void InputFunction::setFunctionIndex(uint32_t Index) {
170
475
  LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName()
171
475
                    << " -> " << Index << "\n");
172
475
  assert(!hasFunctionIndex());
173
475
  FunctionIndex = Index;
174
475
}
175
176
54
void InputFunction::setTableIndex(uint32_t Index) {
177
54
  LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
178
54
                    << Index << "\n");
179
54
  assert(!hasTableIndex());
180
54
  TableIndex = Index;
181
54
}
182
183
// Write a relocation value without padding and return the number of bytes
184
// witten.
185
static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel,
186
8
                                     uint32_t Value) {
187
8
  switch (Rel.Type) {
188
8
  case R_WASM_TYPE_INDEX_LEB:
189
8
  case R_WASM_FUNCTION_INDEX_LEB:
190
8
  case R_WASM_GLOBAL_INDEX_LEB:
191
8
  case R_WASM_EVENT_INDEX_LEB:
192
8
  case R_WASM_MEMORY_ADDR_LEB:
193
8
    return encodeULEB128(Value, Buf);
194
8
  case R_WASM_TABLE_INDEX_SLEB:
195
0
  case R_WASM_MEMORY_ADDR_SLEB:
196
0
    return encodeSLEB128(static_cast<int32_t>(Value), Buf);
197
0
  default:
198
0
    llvm_unreachable("unexpected relocation type");
199
8
  }
200
8
}
201
202
8
static unsigned getRelocWidthPadded(const WasmRelocation &Rel) {
203
8
  switch (Rel.Type) {
204
8
  case R_WASM_TYPE_INDEX_LEB:
205
8
  case R_WASM_FUNCTION_INDEX_LEB:
206
8
  case R_WASM_GLOBAL_INDEX_LEB:
207
8
  case R_WASM_EVENT_INDEX_LEB:
208
8
  case R_WASM_MEMORY_ADDR_LEB:
209
8
  case R_WASM_TABLE_INDEX_SLEB:
210
8
  case R_WASM_MEMORY_ADDR_SLEB:
211
8
    return 5;
212
8
  default:
213
0
    llvm_unreachable("unexpected relocation type");
214
8
  }
215
8
}
216
217
4
static unsigned getRelocWidth(const WasmRelocation &Rel, uint32_t Value) {
218
4
  uint8_t Buf[5];
219
4
  return writeCompressedReloc(Buf, Rel, Value);
220
4
}
221
222
// Relocations of type LEB and SLEB in the code section are padded to 5 bytes
223
// so that a fast linker can blindly overwrite them without needing to worry
224
// about the number of bytes needed to encode the values.
225
// However, for optimal output the code section can be compressed to remove
226
// the padding then outputting non-relocatable files.
227
// In this case we need to perform a size calculation based on the value at each
228
// relocation.  At best we end up saving 4 bytes for each relocation entry.
229
//
230
// This function only computes the final output size.  It must be called
231
// before getSize() is used to calculate of layout of the code section.
232
475
void InputFunction::calculateSize() {
233
475
  if (!File || 
!Config->CompressRelocations433
)
234
471
    return;
235
4
236
4
  LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n");
237
4
238
4
  const uint8_t *SecStart = File->CodeSection->Content.data();
239
4
  const uint8_t *FuncStart = SecStart + getInputSectionOffset();
240
4
  uint32_t FunctionSizeLength;
241
4
  decodeULEB128(FuncStart, &FunctionSizeLength);
242
4
243
4
  uint32_t Start = getInputSectionOffset();
244
4
  uint32_t End = Start + Function->Size;
245
4
246
4
  uint32_t LastRelocEnd = Start + FunctionSizeLength;
247
4
  for (const WasmRelocation &Rel : Relocations) {
248
4
    LLVM_DEBUG(dbgs() << "  region: " << (Rel.Offset - LastRelocEnd) << "\n");
249
4
    CompressedFuncSize += Rel.Offset - LastRelocEnd;
250
4
    CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel));
251
4
    LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel);
252
4
  }
253
4
  LLVM_DEBUG(dbgs() << "  final region: " << (End - LastRelocEnd) << "\n");
254
4
  CompressedFuncSize += End - LastRelocEnd;
255
4
256
4
  // Now we know how long the resulting function is we can add the encoding
257
4
  // of its length
258
4
  uint8_t Buf[5];
259
4
  CompressedSize = CompressedFuncSize + encodeULEB128(CompressedFuncSize, Buf);
260
4
261
4
  LLVM_DEBUG(dbgs() << "  calculateSize orig: " << Function->Size << "\n");
262
4
  LLVM_DEBUG(dbgs() << "  calculateSize  new: " << CompressedSize << "\n");
263
4
}
264
265
// Override the default writeTo method so that we can (optionally) write the
266
// compressed version of the function.
267
470
void InputFunction::writeTo(uint8_t *Buf) const {
268
470
  if (!File || 
!Config->CompressRelocations430
)
269
466
    return InputChunk::writeTo(Buf);
270
4
271
4
  Buf += OutputOffset;
272
4
  uint8_t *Orig = Buf;
273
4
  (void)Orig;
274
4
275
4
  const uint8_t *SecStart = File->CodeSection->Content.data();
276
4
  const uint8_t *FuncStart = SecStart + getInputSectionOffset();
277
4
  const uint8_t *End = FuncStart + Function->Size;
278
4
  uint32_t Count;
279
4
  decodeULEB128(FuncStart, &Count);
280
4
  FuncStart += Count;
281
4
282
4
  LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n");
283
4
  Buf += encodeULEB128(CompressedFuncSize, Buf);
284
4
  const uint8_t *LastRelocEnd = FuncStart;
285
4
  for (const WasmRelocation &Rel : Relocations) {
286
4
    unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd;
287
4
    LLVM_DEBUG(dbgs() << "  write chunk: " << ChunkSize << "\n");
288
4
    memcpy(Buf, LastRelocEnd, ChunkSize);
289
4
    Buf += ChunkSize;
290
4
    Buf += writeCompressedReloc(Buf, Rel, File->calcNewValue(Rel));
291
4
    LastRelocEnd = SecStart + Rel.Offset + getRelocWidthPadded(Rel);
292
4
  }
293
4
294
4
  unsigned ChunkSize = End - LastRelocEnd;
295
4
  LLVM_DEBUG(dbgs() << "  write final chunk: " << ChunkSize << "\n");
296
4
  memcpy(Buf, LastRelocEnd, ChunkSize);
297
4
  LLVM_DEBUG(dbgs() << "  total: " << (Buf + ChunkSize - Orig) << "\n");
298
4
}
299
300
// Generate code to apply relocations to the data section at runtime.
301
// This is only called when generating shared libaries (PIC) where address are
302
// not known at static link time.
303
11
void InputSegment::generateRelocationCode(raw_ostream &OS) const {
304
11
  LLVM_DEBUG(dbgs() << "generating runtime relocations: " << getName()
305
11
                    << " count=" << Relocations.size() << "\n");
306
11
307
11
  // TODO(sbc): Encode the relocations in the data section and write a loop
308
11
  // here to apply them.
309
11
  uint32_t SegmentVA = OutputSeg->StartVA + OutputSegmentOffset;
310
11
  for (const WasmRelocation &Rel : Relocations) {
311
8
    uint32_t Offset = Rel.Offset - getInputSectionOffset();
312
8
    uint32_t OutputOffset = SegmentVA + Offset;
313
8
314
8
    LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(Rel.Type)
315
8
                      << " addend=" << Rel.Addend << " index=" << Rel.Index
316
8
                      << " output offset=" << OutputOffset << "\n");
317
8
318
8
    // Get __memory_base
319
8
    writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
320
8
    writeUleb128(OS, WasmSym::MemoryBase->getGlobalIndex(), "memory_base");
321
8
322
8
    // Add the offset of the relocation
323
8
    writeU8(OS, WASM_OPCODE_I32_CONST, "I32_CONST");
324
8
    writeSleb128(OS, OutputOffset, "offset");
325
8
    writeU8(OS, WASM_OPCODE_I32_ADD, "ADD");
326
8
327
8
    Symbol *Sym = File->getSymbol(Rel);
328
8
    // Now figure out what we want to store
329
8
    if (Sym->hasGOTIndex()) {
330
5
      writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
331
5
      writeUleb128(OS, Sym->getGOTIndex(), "global index");
332
5
      if (Rel.Addend) {
333
1
        writeU8(OS, WASM_OPCODE_I32_CONST, "CONST");
334
1
        writeSleb128(OS, Rel.Addend, "addend");
335
1
        writeU8(OS, WASM_OPCODE_I32_ADD, "ADD");
336
1
      }
337
5
    } else {
338
3
      const GlobalSymbol* BaseSymbol = WasmSym::MemoryBase;
339
3
      if (Rel.Type == R_WASM_TABLE_INDEX_I32)
340
2
        BaseSymbol = WasmSym::TableBase;
341
3
      writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
342
3
      writeUleb128(OS, BaseSymbol->getGlobalIndex(), "base");
343
3
      writeU8(OS, WASM_OPCODE_I32_CONST, "CONST");
344
3
      writeSleb128(OS, File->calcNewValue(Rel), "offset");
345
3
      writeU8(OS, WASM_OPCODE_I32_ADD, "ADD");
346
3
    }
347
8
348
8
    // Store that value at the virtual address
349
8
    writeU8(OS, WASM_OPCODE_I32_STORE, "I32_STORE");
350
8
    writeUleb128(OS, 2, "align");
351
8
    writeUleb128(OS, 0, "offset");
352
8
  }
353
11
}