Coverage Report

Created: 2019-01-18 03:29

/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 "lld/Common/LLVM.h"
17
#include "llvm/ADT/DenseMap.h"
18
19
namespace lld {
20
21
namespace wasm {
22
class OutputSection;
23
}
24
std::string toString(const wasm::OutputSection &Section);
25
26
namespace wasm {
27
28
class OutputSegment;
29
30
class OutputSection {
31
public:
32
  OutputSection(uint32_t Type, std::string Name = "")
33
909
      : Type(Type), Name(Name) {}
34
909
  virtual ~OutputSection() = default;
35
36
  StringRef getSectionName() const;
37
909
  void setOffset(size_t NewOffset) {
38
909
    log("setOffset: " + toString(*this) + ": " + Twine(NewOffset));
39
909
    Offset = NewOffset;
40
909
  }
41
  void createHeader(size_t BodySize);
42
  virtual size_t getSize() const = 0;
43
  virtual void writeTo(uint8_t *Buf) = 0;
44
146
  virtual void finalizeContents() {}
45
95
  virtual uint32_t numRelocations() const { return 0; }
46
0
  virtual void writeRelocations(raw_ostream &OS) const {}
47
48
  std::string Header;
49
  uint32_t Type;
50
  std::string Name;
51
52
protected:
53
  size_t Offset = 0;
54
};
55
56
class SyntheticSection : public OutputSection {
57
public:
58
  SyntheticSection(uint32_t Type, std::string Name = "")
59
763
      : OutputSection(Type, Name), BodyOutputStream(Body) {
60
763
    if (!Name.empty())
61
131
      writeStr(BodyOutputStream, Name, "section name");
62
763
  }
63
64
756
  void writeTo(uint8_t *Buf) override {
65
756
    assert(Offset);
66
756
    log("writing " + toString(*this));
67
756
    memcpy(Buf + Offset, Header.data(), Header.size());
68
756
    memcpy(Buf + Offset + Header.size(), Body.data(), Body.size());
69
756
  }
70
71
1.52k
  size_t getSize() const override { return Header.size() + Body.size(); }
72
73
763
  void finalizeContents() override {
74
763
    BodyOutputStream.flush();
75
763
    createHeader(Body.size());
76
763
  }
77
78
763
  raw_ostream &getStream() { return BodyOutputStream; }
79
80
  std::string Body;
81
82
protected:
83
  llvm::raw_string_ostream BodyOutputStream;
84
};
85
86
class CodeSection : public OutputSection {
87
public:
88
  explicit CodeSection(ArrayRef<InputFunction *> Functions);
89
314
  size_t getSize() const override { return Header.size() + BodySize; }
90
  void writeTo(uint8_t *Buf) override;
91
  uint32_t numRelocations() const override;
92
  void writeRelocations(raw_ostream &OS) const override;
93
94
protected:
95
  ArrayRef<InputFunction *> Functions;
96
  std::string CodeSectionHeader;
97
  size_t BodySize = 0;
98
};
99
100
class DataSection : public OutputSection {
101
public:
102
  explicit DataSection(ArrayRef<OutputSegment *> Segments);
103
69
  size_t getSize() const override { return Header.size() + BodySize; }
104
  void writeTo(uint8_t *Buf) override;
105
  uint32_t numRelocations() const override;
106
  void writeRelocations(raw_ostream &OS) const override;
107
108
protected:
109
  ArrayRef<OutputSegment *> Segments;
110
  std::string DataSectionHeader;
111
  size_t BodySize = 0;
112
};
113
114
// Represents a custom section in the output file.  Wasm custom sections are
115
// used for storing user-defined metadata.  Unlike the core sections types
116
// they are identified by their string name.
117
// The linker combines custom sections that have the same name by simply
118
// concatenating them.
119
// Note that some custom sections such as "name" and "linking" are handled
120
// separately and are instead synthesized by the linker.
121
class CustomSection : public OutputSection {
122
public:
123
  CustomSection(std::string Name, ArrayRef<InputSection *> InputSections);
124
54
  size_t getSize() const override {
125
54
    return Header.size() + NameData.size() + PayloadSize;
126
54
  }
127
  void writeTo(uint8_t *Buf) override;
128
  uint32_t numRelocations() const override;
129
  void writeRelocations(raw_ostream &OS) const override;
130
131
protected:
132
  size_t PayloadSize;
133
  ArrayRef<InputSection *> InputSections;
134
  std::string NameData;
135
};
136
137
} // namespace wasm
138
} // namespace lld
139
140
#endif // LLD_WASM_OUTPUT_SECTIONS_H