Coverage Report

Created: 2019-01-18 03:29

/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
//                             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 "InputChunks.h"
11
#include "Config.h"
12
#include "OutputSegment.h"
13
#include "WriterUtils.h"
14
#include "lld/Common/ErrorHandler.h"
15
#include "lld/Common/LLVM.h"
16
#include "llvm/Support/LEB128.h"
17
18
#define DEBUG_TYPE "lld"
19
20
using namespace llvm;
21
using namespace llvm::wasm;
22
using namespace llvm::support::endian;
23
using namespace lld;
24
using namespace lld::wasm;
25
26
0
static StringRef ReloctTypeToString(uint8_t RelocType) {
27
0
  switch (RelocType) {
28
0
#define WASM_RELOC(NAME, REL)                                                  \
29
0
  case REL:                                                                    \
30
0
    return #NAME;
31
0
#include "llvm/BinaryFormat/WasmRelocs.def"
32
0
#undef WASM_RELOC
33
0
  }
34
0
  llvm_unreachable("unknown reloc type");
35
0
}
36
37
3
std::string lld::toString(const InputChunk *C) {
38
3
  return (toString(C->File) + ":(" + C->getName() + ")").str();
39
3
}
40
41
216
StringRef InputChunk::getComdatName() const {
42
216
  uint32_t Index = getComdat();
43
216
  if (Index == UINT32_MAX)
44
216
    
return StringRef()214
;
45
2
  return File->getWasmObj()->linkingData().Comdats[Index];
46
2
}
47
48
0
void InputChunk::verifyRelocTargets() const {
49
0
  for (const WasmRelocation &Rel : Relocations) {
50
0
    uint32_t ExistingValue;
51
0
    unsigned BytesRead = 0;
52
0
    uint32_t Offset = Rel.Offset - getInputSectionOffset();
53
0
    const uint8_t *Loc = data().data() + Offset;
54
0
    switch (Rel.Type) {
55
0
    case R_WEBASSEMBLY_TYPE_INDEX_LEB:
56
0
    case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
57
0
    case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
58
0
    case R_WEBASSEMBLY_EVENT_INDEX_LEB:
59
0
    case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
60
0
      ExistingValue = decodeULEB128(Loc, &BytesRead);
61
0
      break;
62
0
    case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
63
0
    case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
64
0
      ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead));
65
0
      break;
66
0
    case R_WEBASSEMBLY_TABLE_INDEX_I32:
67
0
    case R_WEBASSEMBLY_MEMORY_ADDR_I32:
68
0
    case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
69
0
    case R_WEBASSEMBLY_SECTION_OFFSET_I32:
70
0
      ExistingValue = static_cast<uint32_t>(read32le(Loc));
71
0
      break;
72
0
    default:
73
0
      llvm_unreachable("unknown relocation type");
74
0
    }
75
0
76
0
    if (BytesRead && BytesRead != 5)
77
0
      warn("expected LEB at relocation site be 5-byte padded");
78
0
    uint32_t ExpectedValue = File->calcExpectedValue(Rel);
79
0
    if (ExpectedValue != ExistingValue)
80
0
      warn("unexpected existing value for " + ReloctTypeToString(Rel.Type) +
81
0
           ": existing=" + Twine(ExistingValue) +
82
0
           " expected=" + Twine(ExpectedValue));
83
0
  }
84
0
}
85
86
// Copy this input chunk to an mmap'ed output file and apply relocations.
87
592
void InputChunk::writeTo(uint8_t *Buf) const {
88
592
  // Copy contents
89
592
  memcpy(Buf + OutputOffset, data().data(), data().size());
90
592
91
592
  // Apply relocations
92
592
  if (Relocations.empty())
93
292
    return;
94
300
95
#ifndef NDEBUG
96
  verifyRelocTargets();
97
#endif
98
99
300
  LLVM_DEBUG(dbgs() << "applying relocations: " << getName()
100
300
                    << " count=" << Relocations.size() << "\n");
101
300
  int32_t Off = OutputOffset - getInputSectionOffset();
102
300
103
437
  for (const WasmRelocation &Rel : Relocations) {
104
437
    uint8_t *Loc = Buf + Rel.Offset + Off;
105
437
    uint32_t Value = File->calcNewValue(Rel);
106
437
    LLVM_DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type)
107
437
                      << " addend=" << Rel.Addend << " index=" << Rel.Index
108
437
                      << " value=" << Value << " offset=" << Rel.Offset
109
437
                      << "\n");
110
437
111
437
    switch (Rel.Type) {
112
437
    case R_WEBASSEMBLY_TYPE_INDEX_LEB:
113
286
    case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
114
286
    case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
115
286
    case R_WEBASSEMBLY_EVENT_INDEX_LEB:
116
286
    case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
117
286
      encodeULEB128(Value, Loc, 5);
118
286
      break;
119
286
    case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
120
70
    case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
121
70
      encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
122
70
      break;
123
81
    case R_WEBASSEMBLY_TABLE_INDEX_I32:
124
81
    case R_WEBASSEMBLY_MEMORY_ADDR_I32:
125
81
    case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
126
81
    case R_WEBASSEMBLY_SECTION_OFFSET_I32:
127
81
      write32le(Loc, Value);
128
81
      break;
129
81
    default:
130
0
      llvm_unreachable("unknown relocation type");
131
437
    }
132
437
  }
133
300
}
134
135
// Copy relocation entries to a given output stream.
136
// This function is used only when a user passes "-r". For a regular link,
137
// we consume relocations instead of copying them to an output file.
138
198
void InputChunk::writeRelocations(raw_ostream &OS) const {
139
198
  if (Relocations.empty())
140
22
    return;
141
176
142
176
  int32_t Off = OutputOffset - getInputSectionOffset();
143
176
  LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName()
144
176
                    << " offset=" << Twine(Off) << "\n");
145
176
146
201
  for (const WasmRelocation &Rel : Relocations) {
147
201
    writeUleb128(OS, Rel.Type, "reloc type");
148
201
    writeUleb128(OS, Rel.Offset + Off, "reloc offset");
149
201
    writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
150
201
151
201
    switch (Rel.Type) {
152
201
    case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
153
148
    case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
154
148
    case R_WEBASSEMBLY_MEMORY_ADDR_I32:
155
148
    case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
156
148
    case R_WEBASSEMBLY_SECTION_OFFSET_I32:
157
148
      writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
158
148
      break;
159
201
    }
160
201
  }
161
176
}
162
163
494
void InputFunction::setFunctionIndex(uint32_t Index) {
164
494
  LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName()
165
494
                    << " -> " << Index << "\n");
166
494
  assert(!hasFunctionIndex());
167
494
  FunctionIndex = Index;
168
494
}
169
170
47
void InputFunction::setTableIndex(uint32_t Index) {
171
47
  LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
172
47
                    << Index << "\n");
173
47
  assert(!hasTableIndex());
174
47
  TableIndex = Index;
175
47
}
176
177
// Write a relocation value without padding and return the number of bytes
178
// witten.
179
static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel,
180
8
                                     uint32_t Value) {
181
8
  switch (Rel.Type) {
182
8
  case R_WEBASSEMBLY_TYPE_INDEX_LEB:
183
8
  case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
184
8
  case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
185
8
  case R_WEBASSEMBLY_EVENT_INDEX_LEB:
186
8
  case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
187
8
    return encodeULEB128(Value, Buf);
188
8
  case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
189
0
  case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
190
0
    return encodeSLEB128(static_cast<int32_t>(Value), Buf);
191
0
  default:
192
0
    llvm_unreachable("unexpected relocation type");
193
8
  }
194
8
}
195
196
8
static unsigned getRelocWidthPadded(const WasmRelocation &Rel) {
197
8
  switch (Rel.Type) {
198
8
  case R_WEBASSEMBLY_TYPE_INDEX_LEB:
199
8
  case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
200
8
  case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
201
8
  case R_WEBASSEMBLY_EVENT_INDEX_LEB:
202
8
  case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
203
8
  case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
204
8
  case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
205
8
    return 5;
206
8
  default:
207
0
    llvm_unreachable("unexpected relocation type");
208
8
  }
209
8
}
210
211
4
static unsigned getRelocWidth(const WasmRelocation &Rel, uint32_t Value) {
212
4
  uint8_t Buf[5];
213
4
  return writeCompressedReloc(Buf, Rel, Value);
214
4
}
215
216
// Relocations of type LEB and SLEB in the code section are padded to 5 bytes
217
// so that a fast linker can blindly overwrite them without needing to worry
218
// about the number of bytes needed to encode the values.
219
// However, for optimal output the code section can be compressed to remove
220
// the padding then outputting non-relocatable files.
221
// In this case we need to perform a size calculation based on the value at each
222
// relocation.  At best we end up saving 4 bytes for each relocation entry.
223
//
224
// This function only computes the final output size.  It must be called
225
// before getSize() is used to calculate of layout of the code section.
226
494
void InputFunction::calculateSize() {
227
494
  if (!File || 
!Config->CompressRelocations400
)
228
490
    return;
229
4
230
4
  LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n");
231
4
232
4
  const uint8_t *SecStart = File->CodeSection->Content.data();
233
4
  const uint8_t *FuncStart = SecStart + getInputSectionOffset();
234
4
  uint32_t FunctionSizeLength;
235
4
  decodeULEB128(FuncStart, &FunctionSizeLength);
236
4
237
4
  uint32_t Start = getInputSectionOffset();
238
4
  uint32_t End = Start + Function->Size;
239
4
240
4
  uint32_t LastRelocEnd = Start + FunctionSizeLength;
241
4
  for (const WasmRelocation &Rel : Relocations) {
242
4
    LLVM_DEBUG(dbgs() << "  region: " << (Rel.Offset - LastRelocEnd) << "\n");
243
4
    CompressedFuncSize += Rel.Offset - LastRelocEnd;
244
4
    CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel));
245
4
    LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel);
246
4
  }
247
4
  LLVM_DEBUG(dbgs() << "  final region: " << (End - LastRelocEnd) << "\n");
248
4
  CompressedFuncSize += End - LastRelocEnd;
249
4
250
4
  // Now we know how long the resulting function is we can add the encoding
251
4
  // of its length
252
4
  uint8_t Buf[5];
253
4
  CompressedSize = CompressedFuncSize + encodeULEB128(CompressedFuncSize, Buf);
254
4
255
4
  LLVM_DEBUG(dbgs() << "  calculateSize orig: " << Function->Size << "\n");
256
4
  LLVM_DEBUG(dbgs() << "  calculateSize  new: " << CompressedSize << "\n");
257
4
}
258
259
// Override the default writeTo method so that we can (optionally) write the
260
// compressed version of the function.
261
492
void InputFunction::writeTo(uint8_t *Buf) const {
262
492
  if (!File || 
!Config->CompressRelocations399
)
263
488
    return InputChunk::writeTo(Buf);
264
4
265
4
  Buf += OutputOffset;
266
4
  uint8_t *Orig = Buf;
267
4
  (void)Orig;
268
4
269
4
  const uint8_t *SecStart = File->CodeSection->Content.data();
270
4
  const uint8_t *FuncStart = SecStart + getInputSectionOffset();
271
4
  const uint8_t *End = FuncStart + Function->Size;
272
4
  uint32_t Count;
273
4
  decodeULEB128(FuncStart, &Count);
274
4
  FuncStart += Count;
275
4
276
4
  LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n");
277
4
  Buf += encodeULEB128(CompressedFuncSize, Buf);
278
4
  const uint8_t *LastRelocEnd = FuncStart;
279
4
  for (const WasmRelocation &Rel : Relocations) {
280
4
    unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd;
281
4
    LLVM_DEBUG(dbgs() << "  write chunk: " << ChunkSize << "\n");
282
4
    memcpy(Buf, LastRelocEnd, ChunkSize);
283
4
    Buf += ChunkSize;
284
4
    Buf += writeCompressedReloc(Buf, Rel, File->calcNewValue(Rel));
285
4
    LastRelocEnd = SecStart + Rel.Offset + getRelocWidthPadded(Rel);
286
4
  }
287
4
288
4
  unsigned ChunkSize = End - LastRelocEnd;
289
4
  LLVM_DEBUG(dbgs() << "  write final chunk: " << ChunkSize << "\n");
290
4
  memcpy(Buf, LastRelocEnd, ChunkSize);
291
4
  LLVM_DEBUG(dbgs() << "  total: " << (Buf + ChunkSize - Orig) << "\n");
292
4
}