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