1 //===- OutputSections.cpp -------------------------------------------------===//
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 #include "OutputSections.h"
10 #include "InputChunks.h"
11 #include "InputFiles.h"
12 #include "OutputSegment.h"
13 #include "WriterUtils.h"
14 #include "lld/Common/ErrorHandler.h"
15 #include "lld/Common/Threads.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/LEB128.h"
18 
19 #define DEBUG_TYPE "lld"
20 
21 using namespace llvm;
22 using namespace llvm::wasm;
23 using namespace lld;
24 using namespace lld::wasm;
25 
26 static StringRef sectionTypeToString(uint32_t SectionType) {
27   switch (SectionType) {
28   case WASM_SEC_CUSTOM:
29     return "CUSTOM";
30   case WASM_SEC_TYPE:
31     return "TYPE";
32   case WASM_SEC_IMPORT:
33     return "IMPORT";
34   case WASM_SEC_FUNCTION:
35     return "FUNCTION";
36   case WASM_SEC_TABLE:
37     return "TABLE";
38   case WASM_SEC_MEMORY:
39     return "MEMORY";
40   case WASM_SEC_GLOBAL:
41     return "GLOBAL";
42   case WASM_SEC_EVENT:
43     return "EVENT";
44   case WASM_SEC_EXPORT:
45     return "EXPORT";
46   case WASM_SEC_START:
47     return "START";
48   case WASM_SEC_ELEM:
49     return "ELEM";
50   case WASM_SEC_CODE:
51     return "CODE";
52   case WASM_SEC_DATA:
53     return "DATA";
54   case WASM_SEC_DATACOUNT:
55     return "DATACOUNT";
56   default:
57     fatal("invalid section type");
58   }
59 }
60 
61 // Returns a string, e.g. "FUNCTION(.text)".
62 std::string lld::toString(const OutputSection &Sec) {
63   if (!Sec.Name.empty())
64     return (Sec.getSectionName() + "(" + Sec.Name + ")").str();
65   return Sec.getSectionName();
66 }
67 
68 StringRef OutputSection::getSectionName() const {
69   return sectionTypeToString(Type);
70 }
71 
72 void OutputSection::createHeader(size_t BodySize) {
73   raw_string_ostream OS(Header);
74   debugWrite(OS.tell(), "section type [" + getSectionName() + "]");
75   encodeULEB128(Type, OS);
76   writeUleb128(OS, BodySize, "section size");
77   OS.flush();
78   log("createHeader: " + toString(*this) + " body=" + Twine(BodySize) +
79       " total=" + Twine(getSize()));
80 }
81 
82 CodeSection::CodeSection(ArrayRef<InputFunction *> Functions)
83     : OutputSection(WASM_SEC_CODE), Functions(Functions) {
84   assert(Functions.size() > 0);
85 
86   raw_string_ostream OS(CodeSectionHeader);
87   writeUleb128(OS, Functions.size(), "function count");
88   OS.flush();
89   BodySize = CodeSectionHeader.size();
90 
91   for (InputFunction *Func : Functions) {
92     Func->OutputOffset = BodySize;
93     Func->calculateSize();
94     BodySize += Func->getSize();
95   }
96 
97   createHeader(BodySize);
98 }
99 
100 void CodeSection::writeTo(uint8_t *Buf) {
101   log("writing " + toString(*this));
102   log(" size=" + Twine(getSize()));
103   log(" headersize=" + Twine(Header.size()));
104   log(" codeheadersize=" + Twine(CodeSectionHeader.size()));
105   Buf += Offset;
106 
107   // Write section header
108   memcpy(Buf, Header.data(), Header.size());
109   Buf += Header.size();
110 
111   // Write code section headers
112   memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size());
113 
114   // Write code section bodies
115   for (const InputChunk *Chunk : Functions)
116     Chunk->writeTo(Buf);
117 }
118 
119 uint32_t CodeSection::numRelocations() const {
120   uint32_t Count = 0;
121   for (const InputChunk *Func : Functions)
122     Count += Func->NumRelocations();
123   return Count;
124 }
125 
126 void CodeSection::writeRelocations(raw_ostream &OS) const {
127   for (const InputChunk *C : Functions)
128     C->writeRelocations(OS);
129 }
130 
131 DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
132     : OutputSection(WASM_SEC_DATA), Segments(Segments) {
133   raw_string_ostream OS(DataSectionHeader);
134 
135   writeUleb128(OS, Segments.size(), "data segment count");
136   OS.flush();
137   BodySize = DataSectionHeader.size();
138 
139   for (OutputSegment *Segment : Segments) {
140     raw_string_ostream OS(Segment->Header);
141     writeUleb128(OS, 0, "memory index");
142     WasmInitExpr InitExpr;
143     if (Config->Pic) {
144       assert(Segments.size() <= 1 &&
145              "Currenly only a single data segment is supported in PIC mode");
146       InitExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
147       InitExpr.Value.Global = WasmSym::MemoryBase->getGlobalIndex();
148     } else {
149       InitExpr.Opcode = WASM_OPCODE_I32_CONST;
150       InitExpr.Value.Int32 = Segment->StartVA;
151     }
152     writeInitExpr(OS, InitExpr);
153     writeUleb128(OS, Segment->Size, "segment size");
154     OS.flush();
155 
156     Segment->SectionOffset = BodySize;
157     BodySize += Segment->Header.size() + Segment->Size;
158     log("Data segment: size=" + Twine(Segment->Size));
159 
160     for (InputSegment *InputSeg : Segment->InputSegments)
161       InputSeg->OutputOffset = Segment->SectionOffset + Segment->Header.size() +
162                                InputSeg->OutputSegmentOffset;
163   }
164 
165   createHeader(BodySize);
166 }
167 
168 void DataSection::writeTo(uint8_t *Buf) {
169   log("writing " + toString(*this) + " size=" + Twine(getSize()) +
170       " body=" + Twine(BodySize));
171   Buf += Offset;
172 
173   // Write section header
174   memcpy(Buf, Header.data(), Header.size());
175   Buf += Header.size();
176 
177   // Write data section headers
178   memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size());
179 
180   for (const OutputSegment *Segment : Segments) {
181     // Write data segment header
182     uint8_t *SegStart = Buf + Segment->SectionOffset;
183     memcpy(SegStart, Segment->Header.data(), Segment->Header.size());
184 
185     // Write segment data payload
186     for (const InputChunk *Chunk : Segment->InputSegments)
187       Chunk->writeTo(Buf);
188   }
189 }
190 
191 uint32_t DataSection::numRelocations() const {
192   uint32_t Count = 0;
193   for (const OutputSegment *Seg : Segments)
194     for (const InputChunk *InputSeg : Seg->InputSegments)
195       Count += InputSeg->NumRelocations();
196   return Count;
197 }
198 
199 void DataSection::writeRelocations(raw_ostream &OS) const {
200   for (const OutputSegment *Seg : Segments)
201     for (const InputChunk *C : Seg->InputSegments)
202       C->writeRelocations(OS);
203 }
204 
205 CustomSection::CustomSection(std::string Name,
206                              ArrayRef<InputSection *> InputSections)
207     : OutputSection(WASM_SEC_CUSTOM, Name), PayloadSize(0),
208       InputSections(InputSections) {
209   raw_string_ostream OS(NameData);
210   encodeULEB128(Name.size(), OS);
211   OS << Name;
212   OS.flush();
213 
214   for (InputSection *Section : InputSections) {
215     Section->OutputOffset = PayloadSize;
216     PayloadSize += Section->getSize();
217   }
218 
219   createHeader(PayloadSize + NameData.size());
220 }
221 
222 void CustomSection::writeTo(uint8_t *Buf) {
223   log("writing " + toString(*this) + " size=" + Twine(getSize()) +
224       " chunks=" + Twine(InputSections.size()));
225 
226   assert(Offset);
227   Buf += Offset;
228 
229   // Write section header
230   memcpy(Buf, Header.data(), Header.size());
231   Buf += Header.size();
232   memcpy(Buf, NameData.data(), NameData.size());
233   Buf += NameData.size();
234 
235   // Write custom sections payload
236   for (const InputSection *Section : InputSections)
237     Section->writeTo(Buf);
238 }
239 
240 uint32_t CustomSection::numRelocations() const {
241   uint32_t Count = 0;
242   for (const InputSection *InputSect : InputSections)
243     Count += InputSect->NumRelocations();
244   return Count;
245 }
246 
247 void CustomSection::writeRelocations(raw_ostream &OS) const {
248   for (const InputSection *S : InputSections)
249     S->writeRelocations(OS);
250 }
251