Coverage Report

Created: 2018-08-19 14:04

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/OutputSections.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- OutputSections.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 "OutputSections.h"
11
#include "InputChunks.h"
12
#include "InputFiles.h"
13
#include "OutputSegment.h"
14
#include "WriterUtils.h"
15
#include "lld/Common/ErrorHandler.h"
16
#include "lld/Common/Threads.h"
17
#include "llvm/ADT/Twine.h"
18
#include "llvm/Support/LEB128.h"
19
20
#define DEBUG_TYPE "lld"
21
22
using namespace llvm;
23
using namespace llvm::wasm;
24
using namespace lld;
25
using namespace lld::wasm;
26
27
static StringRef sectionTypeToString(uint32_t SectionType) {
28
  switch (SectionType) {
29
  case WASM_SEC_CUSTOM:
30
    return "CUSTOM";
31
  case WASM_SEC_TYPE:
32
    return "TYPE";
33
  case WASM_SEC_IMPORT:
34
    return "IMPORT";
35
  case WASM_SEC_FUNCTION:
36
    return "FUNCTION";
37
  case WASM_SEC_TABLE:
38
    return "TABLE";
39
  case WASM_SEC_MEMORY:
40
    return "MEMORY";
41
  case WASM_SEC_GLOBAL:
42
    return "GLOBAL";
43
  case WASM_SEC_EXPORT:
44
    return "EXPORT";
45
  case WASM_SEC_START:
46
    return "START";
47
  case WASM_SEC_ELEM:
48
    return "ELEM";
49
  case WASM_SEC_CODE:
50
    return "CODE";
51
  case WASM_SEC_DATA:
52
    return "DATA";
53
  default:
54
    fatal("invalid section type");
55
  }
56
}
57
58
// Returns a string, e.g. "FUNCTION(.text)".
59
0
std::string lld::toString(const OutputSection &Sec) {
60
0
  if (!Sec.Name.empty())
61
0
    return (Sec.getSectionName() + "(" + Sec.Name + ")").str();
62
0
  return Sec.getSectionName();
63
0
}
64
65
0
StringRef OutputSection::getSectionName() const {
66
0
  return sectionTypeToString(Type);
67
0
}
68
69
0
void OutputSection::createHeader(size_t BodySize) {
70
0
  raw_string_ostream OS(Header);
71
0
  debugWrite(OS.tell(), "section type [" + getSectionName() + "]");
72
0
  encodeULEB128(Type, OS);
73
0
  writeUleb128(OS, BodySize, "section size");
74
0
  OS.flush();
75
0
  log("createHeader: " + toString(*this) + " body=" + Twine(BodySize) +
76
0
      " total=" + Twine(getSize()));
77
0
}
78
79
CodeSection::CodeSection(ArrayRef<InputFunction *> Functions)
80
0
    : OutputSection(WASM_SEC_CODE), Functions(Functions) {
81
0
  assert(Functions.size() > 0);
82
0
83
0
  raw_string_ostream OS(CodeSectionHeader);
84
0
  writeUleb128(OS, Functions.size(), "function count");
85
0
  OS.flush();
86
0
  BodySize = CodeSectionHeader.size();
87
0
88
0
  for (InputFunction *Func : Functions) {
89
0
    Func->OutputOffset = BodySize;
90
0
    Func->calculateSize();
91
0
    BodySize += Func->getSize();
92
0
  }
93
0
94
0
  createHeader(BodySize);
95
0
}
96
97
0
void CodeSection::writeTo(uint8_t *Buf) {
98
0
  log("writing " + toString(*this));
99
0
  log(" size=" + Twine(getSize()));
100
0
  log(" headersize=" + Twine(Header.size()));
101
0
  log(" codeheadersize=" + Twine(CodeSectionHeader.size()));
102
0
  Buf += Offset;
103
0
104
0
  // Write section header
105
0
  memcpy(Buf, Header.data(), Header.size());
106
0
  Buf += Header.size();
107
0
108
0
  // Write code section headers
109
0
  memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size());
110
0
111
0
  // Write code section bodies
112
0
  parallelForEach(Functions,
113
0
                  [&](const InputChunk *Chunk) { Chunk->writeTo(Buf); });
114
0
}
115
116
0
uint32_t CodeSection::numRelocations() const {
117
0
  uint32_t Count = 0;
118
0
  for (const InputChunk *Func : Functions)
119
0
    Count += Func->NumRelocations();
120
0
  return Count;
121
0
}
122
123
0
void CodeSection::writeRelocations(raw_ostream &OS) const {
124
0
  for (const InputChunk *C : Functions)
125
0
    C->writeRelocations(OS);
126
0
}
127
128
DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
129
0
    : OutputSection(WASM_SEC_DATA), Segments(Segments) {
130
0
  raw_string_ostream OS(DataSectionHeader);
131
0
132
0
  writeUleb128(OS, Segments.size(), "data segment count");
133
0
  OS.flush();
134
0
  BodySize = DataSectionHeader.size();
135
0
136
0
  for (OutputSegment *Segment : Segments) {
137
0
    raw_string_ostream OS(Segment->Header);
138
0
    writeUleb128(OS, 0, "memory index");
139
0
    writeUleb128(OS, WASM_OPCODE_I32_CONST, "opcode:i32const");
140
0
    writeSleb128(OS, Segment->StartVA, "memory offset");
141
0
    writeUleb128(OS, WASM_OPCODE_END, "opcode:end");
142
0
    writeUleb128(OS, Segment->Size, "segment size");
143
0
    OS.flush();
144
0
145
0
    Segment->SectionOffset = BodySize;
146
0
    BodySize += Segment->Header.size() + Segment->Size;
147
0
    log("Data segment: size=" + Twine(Segment->Size));
148
0
149
0
    for (InputSegment *InputSeg : Segment->InputSegments)
150
0
      InputSeg->OutputOffset = Segment->SectionOffset + Segment->Header.size() +
151
0
                               InputSeg->OutputSegmentOffset;
152
0
  }
153
0
154
0
  createHeader(BodySize);
155
0
}
156
157
0
void DataSection::writeTo(uint8_t *Buf) {
158
0
  log("writing " + toString(*this) + " size=" + Twine(getSize()) +
159
0
      " body=" + Twine(BodySize));
160
0
  Buf += Offset;
161
0
162
0
  // Write section header
163
0
  memcpy(Buf, Header.data(), Header.size());
164
0
  Buf += Header.size();
165
0
166
0
  // Write data section headers
167
0
  memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size());
168
0
169
0
  parallelForEach(Segments, [&](const OutputSegment *Segment) {
170
0
    // Write data segment header
171
0
    uint8_t *SegStart = Buf + Segment->SectionOffset;
172
0
    memcpy(SegStart, Segment->Header.data(), Segment->Header.size());
173
0
174
0
    // Write segment data payload
175
0
    for (const InputChunk *Chunk : Segment->InputSegments)
176
0
      Chunk->writeTo(Buf);
177
0
  });
178
0
}
179
180
0
uint32_t DataSection::numRelocations() const {
181
0
  uint32_t Count = 0;
182
0
  for (const OutputSegment *Seg : Segments)
183
0
    for (const InputChunk *InputSeg : Seg->InputSegments)
184
0
      Count += InputSeg->NumRelocations();
185
0
  return Count;
186
0
}
187
188
0
void DataSection::writeRelocations(raw_ostream &OS) const {
189
0
  for (const OutputSegment *Seg : Segments)
190
0
    for (const InputChunk *C : Seg->InputSegments)
191
0
      C->writeRelocations(OS);
192
0
}
193
194
CustomSection::CustomSection(std::string Name,
195
                             ArrayRef<InputSection *> InputSections)
196
    : OutputSection(WASM_SEC_CUSTOM, Name), PayloadSize(0),
197
0
      InputSections(InputSections) {
198
0
  raw_string_ostream OS(NameData);
199
0
  encodeULEB128(Name.size(), OS);
200
0
  OS << Name;
201
0
  OS.flush();
202
0
203
0
  for (InputSection *Section : InputSections) {
204
0
    Section->OutputOffset = PayloadSize;
205
0
    PayloadSize += Section->getSize();
206
0
  }
207
0
208
0
  createHeader(PayloadSize + NameData.size());
209
0
}
210
211
0
void CustomSection::writeTo(uint8_t *Buf) {
212
0
  log("writing " + toString(*this) + " size=" + Twine(getSize()) +
213
0
      " chunks=" + Twine(InputSections.size()));
214
0
215
0
  assert(Offset);
216
0
  Buf += Offset;
217
0
218
0
  // Write section header
219
0
  memcpy(Buf, Header.data(), Header.size());
220
0
  Buf += Header.size();
221
0
  memcpy(Buf, NameData.data(), NameData.size());
222
0
  Buf += NameData.size();
223
0
224
0
  // Write custom sections payload
225
0
  parallelForEach(InputSections,
226
0
                  [&](const InputSection *Section) { Section->writeTo(Buf); });
227
0
}
228
229
0
uint32_t CustomSection::numRelocations() const {
230
0
  uint32_t Count = 0;
231
0
  for (const InputSection *InputSect : InputSections)
232
0
    Count += InputSect->NumRelocations();
233
0
  return Count;
234
0
}
235
236
0
void CustomSection::writeRelocations(raw_ostream &OS) const {
237
0
  for (const InputSection *S : InputSections)
238
0
    S->writeRelocations(OS);
239
0
}