Coverage Report

Created: 2018-08-19 21:11

/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
  StringRef 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, "section 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
class CodeSection : public OutputSection {
89
public:
90
  explicit CodeSection(ArrayRef<InputFunction *> Functions);
91
0
  size_t getSize() const override { return Header.size() + BodySize; }
92
  void writeTo(uint8_t *Buf) override;
93
  uint32_t numRelocations() const override;
94
  void writeRelocations(raw_ostream &OS) const override;
95
96
protected:
97
  ArrayRef<InputFunction *> Functions;
98
  std::string CodeSectionHeader;
99
  size_t BodySize = 0;
100
};
101
102
class DataSection : public OutputSection {
103
public:
104
  explicit DataSection(ArrayRef<OutputSegment *> Segments);
105
0
  size_t getSize() const override { return Header.size() + BodySize; }
106
  void writeTo(uint8_t *Buf) override;
107
  uint32_t numRelocations() const override;
108
  void writeRelocations(raw_ostream &OS) const override;
109
110
protected:
111
  ArrayRef<OutputSegment *> Segments;
112
  std::string DataSectionHeader;
113
  size_t BodySize = 0;
114
};
115
116
// Represents a custom section in the output file.  Wasm custom sections are 
117
// used for storing user-defined metadata.  Unlike the core sections types
118
// they are identified by their string name.
119
// The linker combines custom sections that have the same name by simply
120
// concatenating them.
121
// Note that some custom sections such as "name" and "linking" are handled
122
// separately and are instead synthesized by the linker.
123
class CustomSection : public OutputSection {
124
public:
125
  CustomSection(std::string Name, ArrayRef<InputSection *> InputSections);
126
0
  size_t getSize() const override {
127
0
    return Header.size() + NameData.size() + PayloadSize;
128
0
  }
129
  void writeTo(uint8_t *Buf) override;
130
  uint32_t numRelocations() const override;
131
  void writeRelocations(raw_ostream &OS) const override;
132
133
protected:
134
  size_t PayloadSize;
135
  ArrayRef<InputSection *> InputSections;
136
  std::string NameData;
137
};
138
139
} // namespace wasm
140
} // namespace lld
141
142
#endif // LLD_WASM_OUTPUT_SECTIONS_H