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