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"; |
| 43 | case WASM_SEC_EXPORT: |
| 44 | return "EXPORT"; |
| 45 | case WASM_SEC_START: |
| 46 | return "START"; |
| 47 | case WASM_SEC_ELEM: |
| 48 | return "ELEM"; |
| 49 | case WASM_SEC_CODE: |
| 50 | return "CODE"; |
| 51 | case WASM_SEC_DATA: |
| 52 | return "DATA"; |
| 53 | default: |
| 54 | fatal("invalid section type"); |
| 55 | } |
| 56 | } |
| 57 | |
Rui Ueyama | 22c8f33 | 2018-02-28 17:33:04 +0000 | [diff] [blame] | 58 | // Returns a string, e.g. "FUNCTION(.text)". |
| 59 | std::string lld::toString(const OutputSection &Sec) { |
| 60 | if (!Sec.Name.empty()) |
| 61 | return (Sec.getSectionName() + "(" + Sec.Name + ")").str(); |
| 62 | return Sec.getSectionName(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Rui Ueyama | 22c8f33 | 2018-02-28 17:33:04 +0000 | [diff] [blame] | 65 | StringRef OutputSection::getSectionName() const { |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 +0000 | [diff] [blame] | 66 | return sectionTypeToString(Type); |
| 67 | } |
| 68 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 69 | void OutputSection::createHeader(size_t BodySize) { |
| 70 | raw_string_ostream OS(Header); |
Rui Ueyama | 22c8f33 | 2018-02-28 17:33:04 +0000 | [diff] [blame] | 71 | debugWrite(OS.tell(), "section type [" + getSectionName() + "]"); |
Rui Ueyama | 1184253 | 2018-02-16 20:38:00 +0000 | [diff] [blame] | 72 | encodeULEB128(Type, OS); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 73 | writeUleb128(OS, BodySize, "section size"); |
| 74 | OS.flush(); |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 75 | log("createHeader: " + toString(*this) + " body=" + Twine(BodySize) + |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 76 | " total=" + Twine(getSize())); |
| 77 | } |
| 78 | |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 79 | CodeSection::CodeSection(ArrayRef<InputFunction *> Functions) |
| 80 | : OutputSection(WASM_SEC_CODE), Functions(Functions) { |
| 81 | assert(Functions.size() > 0); |
| 82 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 83 | raw_string_ostream OS(CodeSectionHeader); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 84 | writeUleb128(OS, Functions.size(), "function count"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 85 | OS.flush(); |
| 86 | BodySize = CodeSectionHeader.size(); |
| 87 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 88 | for (InputChunk *Func : Functions) { |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 89 | Func->OutputOffset = BodySize; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 90 | BodySize += Func->getSize(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | createHeader(BodySize); |
| 94 | } |
| 95 | |
| 96 | void CodeSection::writeTo(uint8_t *Buf) { |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 97 | log("writing " + toString(*this)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 98 | log(" size=" + Twine(getSize())); |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 99 | log(" headersize=" + Twine(Header.size())); |
| 100 | log(" codeheadersize=" + Twine(CodeSectionHeader.size())); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 101 | Buf += Offset; |
| 102 | |
| 103 | // Write section header |
| 104 | memcpy(Buf, Header.data(), Header.size()); |
| 105 | Buf += Header.size(); |
| 106 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 107 | // Write code section headers |
| 108 | memcpy(Buf, CodeSectionHeader.data(), CodeSectionHeader.size()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 109 | |
| 110 | // Write code section bodies |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 111 | parallelForEach(Functions, |
| 112 | [&](const InputChunk *Chunk) { Chunk->writeTo(Buf); }); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | uint32_t CodeSection::numRelocations() const { |
| 116 | uint32_t Count = 0; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 117 | for (const InputChunk *Func : Functions) |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 118 | Count += Func->NumRelocations(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 119 | return Count; |
| 120 | } |
| 121 | |
| 122 | void CodeSection::writeRelocations(raw_ostream &OS) const { |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 123 | for (const InputChunk *C : Functions) |
| 124 | C->writeRelocations(OS); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Sam Clegg | 0fb6faa | 2017-12-08 01:09:21 +0000 | [diff] [blame] | 127 | DataSection::DataSection(ArrayRef<OutputSegment *> Segments) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 128 | : OutputSection(WASM_SEC_DATA), Segments(Segments) { |
| 129 | raw_string_ostream OS(DataSectionHeader); |
| 130 | |
| 131 | writeUleb128(OS, Segments.size(), "data segment count"); |
| 132 | OS.flush(); |
| 133 | BodySize = DataSectionHeader.size(); |
| 134 | |
| 135 | for (OutputSegment *Segment : Segments) { |
| 136 | raw_string_ostream OS(Segment->Header); |
| 137 | writeUleb128(OS, 0, "memory index"); |
| 138 | writeUleb128(OS, WASM_OPCODE_I32_CONST, "opcode:i32const"); |
| 139 | writeSleb128(OS, Segment->StartVA, "memory offset"); |
| 140 | writeUleb128(OS, WASM_OPCODE_END, "opcode:end"); |
| 141 | writeUleb128(OS, Segment->Size, "segment size"); |
| 142 | OS.flush(); |
Rui Ueyama | ac95bb1 | 2018-04-05 19:37:31 +0000 | [diff] [blame^] | 143 | |
| 144 | Segment->SectionOffset = BodySize; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 145 | BodySize += Segment->Header.size() + Segment->Size; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 146 | log("Data segment: size=" + Twine(Segment->Size)); |
Rui Ueyama | ac95bb1 | 2018-04-05 19:37:31 +0000 | [diff] [blame^] | 147 | |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 148 | for (InputSegment *InputSeg : Segment->InputSegments) |
Rui Ueyama | ac95bb1 | 2018-04-05 19:37:31 +0000 | [diff] [blame^] | 149 | InputSeg->OutputOffset = Segment->SectionOffset + Segment->Header.size() + |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 150 | InputSeg->OutputSegmentOffset; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | createHeader(BodySize); |
| 154 | } |
| 155 | |
| 156 | void DataSection::writeTo(uint8_t *Buf) { |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 157 | log("writing " + toString(*this) + " size=" + Twine(getSize()) + |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 158 | " body=" + Twine(BodySize)); |
| 159 | Buf += Offset; |
| 160 | |
| 161 | // Write section header |
| 162 | memcpy(Buf, Header.data(), Header.size()); |
| 163 | Buf += Header.size(); |
| 164 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 165 | // Write data section headers |
| 166 | memcpy(Buf, DataSectionHeader.data(), DataSectionHeader.size()); |
| 167 | |
Rui Ueyama | 319eb8b | 2018-02-28 00:31:16 +0000 | [diff] [blame] | 168 | parallelForEach(Segments, [&](const OutputSegment *Segment) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 169 | // Write data segment header |
Rui Ueyama | ac95bb1 | 2018-04-05 19:37:31 +0000 | [diff] [blame^] | 170 | uint8_t *SegStart = Buf + Segment->SectionOffset; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 171 | memcpy(SegStart, Segment->Header.data(), Segment->Header.size()); |
| 172 | |
| 173 | // Write segment data payload |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 174 | for (const InputChunk *Chunk : Segment->InputSegments) |
Rui Ueyama | 319eb8b | 2018-02-28 00:31:16 +0000 | [diff] [blame] | 175 | Chunk->writeTo(Buf); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 176 | }); |
Sam Clegg | 5e8cba9 | 2017-12-19 20:45:15 +0000 | [diff] [blame] | 177 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 178 | |
Sam Clegg | 5e8cba9 | 2017-12-19 20:45:15 +0000 | [diff] [blame] | 179 | uint32_t DataSection::numRelocations() const { |
| 180 | uint32_t Count = 0; |
| 181 | for (const OutputSegment *Seg : Segments) |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 182 | for (const InputChunk *InputSeg : Seg->InputSegments) |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 183 | Count += InputSeg->NumRelocations(); |
Sam Clegg | 5e8cba9 | 2017-12-19 20:45:15 +0000 | [diff] [blame] | 184 | return Count; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void DataSection::writeRelocations(raw_ostream &OS) const { |
Sam Clegg | 5e8cba9 | 2017-12-19 20:45:15 +0000 | [diff] [blame] | 188 | for (const OutputSegment *Seg : Segments) |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 189 | for (const InputChunk *C : Seg->InputSegments) |
| 190 | C->writeRelocations(OS); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 191 | } |