xref: /openbsd/gnu/llvm/lld/wasm/OutputSections.h (revision 73471bf0)
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       : type(type), name(name) {}
33   virtual ~OutputSection() = default;
34 
35   StringRef getSectionName() const;
36   void setOffset(size_t newOffset) {
37     log("setOffset: " + toString(*this) + ": " + Twine(newOffset));
38     offset = newOffset;
39   }
40   void createHeader(size_t bodySize);
41   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   virtual uint32_t getNumRelocations() const { return 0; }
46   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       : OutputSection(llvm::wasm::WASM_SEC_CODE), functions(functions) {}
62 
63   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   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       : OutputSection(llvm::wasm::WASM_SEC_DATA), segments(segments) {}
80 
81   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   bool isNeeded() const override;
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         inputSections(inputSections) {}
106   size_t getSize() const override {
107     return header.size() + nameData.size() + payloadSize;
108   }
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
124