Coverage Report

Created: 2018-01-17 21:32

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/wasm/OutputSections.h
Line
Count
Source (jump to first uncovered line)
1
//===- OutputSections.h -----------------------------------------*- C++ -*-===//
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
#ifndef LLD_WASM_OUTPUT_SECTIONS_H
11
#define LLD_WASM_OUTPUT_SECTIONS_H
12
13
#include "InputChunks.h"
14
#include "WriterUtils.h"
15
#include "lld/Common/ErrorHandler.h"
16
#include "llvm/ADT/DenseMap.h"
17
18
using llvm::raw_ostream;
19
using llvm::raw_string_ostream;
20
21
namespace lld {
22
23
namespace wasm {
24
class OutputSection;
25
}
26
std::string toString(const wasm::OutputSection &Section);
27
28
namespace wasm {
29
30
class OutputSegment;
31
32
class OutputSection {
33
public:
34
  OutputSection(uint32_t Type, std::string Name = "")
35
0
      : Type(Type), Name(Name) {}
36
0
  virtual ~OutputSection() = default;
37
38
  std::string getSectionName() const;
39
0
  void setOffset(size_t NewOffset) {
40
0
    log("setOffset: " + toString(*this) + ": " + Twine(NewOffset));
41
0
    Offset = NewOffset;
42
0
  }
43
  void createHeader(size_t BodySize);
44
  virtual size_t getSize() const = 0;
45
  virtual void writeTo(uint8_t *Buf) = 0;
46
0
  virtual void finalizeContents() {}
47
0
  virtual uint32_t numRelocations() const { return 0; }
48
0
  virtual void writeRelocations(raw_ostream &OS) const {}
49
50
  std::string Header;
51
  uint32_t Type;
52
  std::string Name;
53
54
protected:
55
  size_t Offset = 0;
56
};
57
58
class SyntheticSection : public OutputSection {
59
public:
60
  SyntheticSection(uint32_t Type, std::string Name = "")
61
0
      : OutputSection(Type, Name), BodyOutputStream(Body) {
62
0
    if (!Name.empty())
63
0
      writeStr(BodyOutputStream, Name);
64
0
  }
65
66
0
  void writeTo(uint8_t *Buf) override {
67
0
    assert(Offset);
68
0
    log("writing " + toString(*this));
69
0
    memcpy(Buf + Offset, Header.data(), Header.size());
70
0
    memcpy(Buf + Offset + Header.size(), Body.data(), Body.size());
71
0
  }
72
73
0
  size_t getSize() const override { return Header.size() + Body.size(); }
74
75
0
  void finalizeContents() override {
76
0
    BodyOutputStream.flush();
77
0
    createHeader(Body.size());
78
0
  }
79
80
0
  raw_ostream &getStream() { return BodyOutputStream; }
81
82
  std::string Body;
83
84
protected:
85
  raw_string_ostream BodyOutputStream;
86
};
87
88
// Some synthetic sections (e.g. "name" and "linking") have subsections.
89
// Just like the synthetic sections themselves these need to be created before
90
// they can be written out (since they are preceded by their length). This
91
// class is used to create subsections and then write them into the stream
92
// of the parent section.
93
class SubSection : public SyntheticSection {
94
public:
95
0
  explicit SubSection(uint32_t Type) : SyntheticSection(Type) {}
96
97
  std::string getSectionName() const;
98
0
  void writeToStream(raw_ostream &OS) {
99
0
    writeBytes(OS, Header.data(), Header.size());
100
0
    writeBytes(OS, Body.data(), Body.size());
101
0
  }
102
};
103
104
class CodeSection : public OutputSection {
105
public:
106
  explicit CodeSection(ArrayRef<InputFunction *> Functions);
107
0
  size_t getSize() const override { return Header.size() + BodySize; }
108
  void writeTo(uint8_t *Buf) override;
109
  uint32_t numRelocations() const override;
110
  void writeRelocations(raw_ostream &OS) const override;
111
112
protected:
113
  ArrayRef<InputFunction *> Functions;
114
  std::string CodeSectionHeader;
115
  size_t BodySize = 0;
116
};
117
118
class DataSection : public OutputSection {
119
public:
120
  explicit DataSection(ArrayRef<OutputSegment *> Segments);
121
0
  size_t getSize() const override { return Header.size() + BodySize; }
122
  void writeTo(uint8_t *Buf) override;
123
  uint32_t numRelocations() const override;
124
  void writeRelocations(raw_ostream &OS) const override;
125
126
protected:
127
  ArrayRef<OutputSegment *> Segments;
128
  std::string DataSectionHeader;
129
  size_t BodySize = 0;
130
};
131
132
} // namespace wasm
133
} // namespace lld
134
135
#endif // LLD_WASM_OUTPUT_SECTIONS_H