1 //===- OutputSections.h -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_WASM_OUTPUT_SECTIONS_H
10 #define LLD_WASM_OUTPUT_SECTIONS_H
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"
23 std::string
toString(const wasm::OutputSection
§ion
);
31 OutputSection(uint32_t type
, std::string name
= "")
32 : type(type
), name(name
) {}
33 virtual ~OutputSection() = default;
35 StringRef
getSectionName() const;
36 void setOffset(size_t newOffset
) { offset
= newOffset
; }
37 void createHeader(size_t bodySize
);
38 virtual bool isNeeded() const { return true; }
39 virtual size_t getSize() const = 0;
40 virtual size_t getOffset() { return offset
; }
41 virtual void writeTo(uint8_t *buf
) = 0;
42 virtual void finalizeContents() = 0;
43 virtual uint32_t getNumRelocations() const { return 0; }
44 virtual void writeRelocations(raw_ostream
&os
) const {}
48 uint32_t sectionIndex
= UINT32_MAX
;
50 OutputSectionSymbol
*sectionSym
= nullptr;
56 class CodeSection
: public OutputSection
{
58 explicit CodeSection(ArrayRef
<InputFunction
*> functions
)
59 : OutputSection(llvm::wasm::WASM_SEC_CODE
), functions(functions
) {}
61 static bool classof(const OutputSection
*sec
) {
62 return sec
->type
== llvm::wasm::WASM_SEC_CODE
;
65 size_t getSize() const override
{ return header
.size() + bodySize
; }
66 void writeTo(uint8_t *buf
) override
;
67 uint32_t getNumRelocations() const override
;
68 void writeRelocations(raw_ostream
&os
) const override
;
69 bool isNeeded() const override
{ return functions
.size() > 0; }
70 void finalizeContents() override
;
72 ArrayRef
<InputFunction
*> functions
;
75 std::string codeSectionHeader
;
79 class DataSection
: public OutputSection
{
81 explicit DataSection(ArrayRef
<OutputSegment
*> segments
)
82 : OutputSection(llvm::wasm::WASM_SEC_DATA
), segments(segments
) {}
84 static bool classof(const OutputSection
*sec
) {
85 return sec
->type
== llvm::wasm::WASM_SEC_DATA
;
88 size_t getSize() const override
{ return header
.size() + bodySize
; }
89 void writeTo(uint8_t *buf
) override
;
90 uint32_t getNumRelocations() const override
;
91 void writeRelocations(raw_ostream
&os
) const override
;
92 bool isNeeded() const override
;
93 void finalizeContents() override
;
95 ArrayRef
<OutputSegment
*> segments
;
98 std::string dataSectionHeader
;
102 // Represents a custom section in the output file. Wasm custom sections are
103 // used for storing user-defined metadata. Unlike the core sections types
104 // they are identified by their string name.
105 // The linker combines custom sections that have the same name by simply
106 // concatenating them.
107 // Note that some custom sections such as "name" and "linking" are handled
108 // separately and are instead synthesized by the linker.
109 class CustomSection
: public OutputSection
{
111 CustomSection(std::string name
, ArrayRef
<InputChunk
*> inputSections
)
112 : OutputSection(llvm::wasm::WASM_SEC_CUSTOM
, name
),
113 inputSections(inputSections
) {}
115 static bool classof(const OutputSection
*sec
) {
116 return sec
->type
== llvm::wasm::WASM_SEC_CUSTOM
;
119 size_t getSize() const override
{
120 return header
.size() + nameData
.size() + payloadSize
;
122 void writeTo(uint8_t *buf
) override
;
123 uint32_t getNumRelocations() const override
;
124 void writeRelocations(raw_ostream
&os
) const override
;
125 void finalizeContents() override
;
128 void finalizeInputSections();
129 size_t payloadSize
= 0;
130 std::vector
<InputChunk
*> inputSections
;
131 std::string nameData
;
137 #endif // LLD_WASM_OUTPUT_SECTIONS_H