Coverage Report

Created: 2018-10-20 12:32

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