Coverage Report

Created: 2018-08-19 21:11

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