Coverage Report

Created: 2018-10-23 15:26

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