Coverage Report

Created: 2019-07-24 05:18

/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
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLD_WASM_OUTPUT_SECTIONS_H
10
#define LLD_WASM_OUTPUT_SECTIONS_H
11
12
#include "InputChunks.h"
13
#include "WriterUtils.h"
14
#include "lld/Common/ErrorHandler.h"
15
#include "lld/Common/LLVM.h"
16
#include "llvm/ADT/DenseMap.h"
17
18
namespace lld {
19
20
namespace wasm {
21
class OutputSection;
22
}
23
std::string toString(const wasm::OutputSection &section);
24
25
namespace wasm {
26
27
class OutputSegment;
28
29
class OutputSection {
30
public:
31
  OutputSection(uint32_t type, std::string name = "")
32
3.27k
      : type(type), name(name) {}
33
3.27k
  virtual ~OutputSection() = default;
34
35
  StringRef getSectionName() const;
36
1.42k
  void setOffset(size_t newOffset) {
37
1.42k
    log("setOffset: " + toString(*this) + ": " + Twine(newOffset));
38
1.42k
    offset = newOffset;
39
1.42k
  }
40
  void createHeader(size_t bodySize);
41
22
  virtual bool isNeeded() const { return true; }
42
  virtual size_t getSize() const = 0;
43
  virtual void writeTo(uint8_t *buf) = 0;
44
  virtual void finalizeContents() = 0;
45
130
  virtual uint32_t getNumRelocations() const { return 0; }
46
0
  virtual void writeRelocations(raw_ostream &os) const {}
47
48
  std::string header;
49
  uint32_t type;
50
  uint32_t sectionIndex = UINT32_MAX;
51
  std::string name;
52
  OutputSectionSymbol *sectionSym = nullptr;
53
54
protected:
55
  size_t offset = 0;
56
};
57
58
class CodeSection : public OutputSection {
59
public:
60
  explicit CodeSection(ArrayRef<InputFunction *> functions)
61
172
      : OutputSection(llvm::wasm::WASM_SEC_CODE), functions(functions) {}
62
63
441
  size_t getSize() const override { return header.size() + bodySize; }
64
  void writeTo(uint8_t *buf) override;
65
  uint32_t getNumRelocations() const override;
66
  void writeRelocations(raw_ostream &os) const override;
67
172
  bool isNeeded() const override { return functions.size() > 0; }
68
  void finalizeContents() override;
69
70
protected:
71
  ArrayRef<InputFunction *> functions;
72
  std::string codeSectionHeader;
73
  size_t bodySize = 0;
74
};
75
76
class DataSection : public OutputSection {
77
public:
78
  explicit DataSection(ArrayRef<OutputSegment *> segments)
79
172
      : OutputSection(llvm::wasm::WASM_SEC_DATA), segments(segments) {}
80
81
138
  size_t getSize() const override { return header.size() + bodySize; }
82
  void writeTo(uint8_t *buf) override;
83
  uint32_t getNumRelocations() const override;
84
  void writeRelocations(raw_ostream &os) const override;
85
172
  bool isNeeded() const override { return segments.size() > 0; }
86
  void finalizeContents() override;
87
88
protected:
89
  ArrayRef<OutputSegment *> segments;
90
  std::string dataSectionHeader;
91
  size_t bodySize = 0;
92
};
93
94
// Represents a custom section in the output file.  Wasm custom sections are
95
// used for storing user-defined metadata.  Unlike the core sections types
96
// they are identified by their string name.
97
// The linker combines custom sections that have the same name by simply
98
// concatenating them.
99
// Note that some custom sections such as "name" and "linking" are handled
100
// separately and are instead synthesized by the linker.
101
class CustomSection : public OutputSection {
102
public:
103
  CustomSection(std::string name, ArrayRef<InputSection *> inputSections)
104
      : OutputSection(llvm::wasm::WASM_SEC_CUSTOM, name),
105
22
        inputSections(inputSections) {}
106
66
  size_t getSize() const override {
107
66
    return header.size() + nameData.size() + payloadSize;
108
66
  }
109
  void writeTo(uint8_t *buf) override;
110
  uint32_t getNumRelocations() const override;
111
  void writeRelocations(raw_ostream &os) const override;
112
  void finalizeContents() override;
113
114
protected:
115
  size_t payloadSize = 0;
116
  ArrayRef<InputSection *> inputSections;
117
  std::string nameData;
118
};
119
120
} // namespace wasm
121
} // namespace lld
122
123
#endif // LLD_WASM_OUTPUT_SECTIONS_H