Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 1 | //===- OutputSections.cpp -------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [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 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "OutputSections.h" |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 | [diff] [blame] | 10 | #include "InputChunks.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 11 | #include "InputFiles.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 12 | #include "OutputSegment.h" |
Rui Ueyama | 4a1b2bb | 2018-02-28 00:52:42 | [diff] [blame] | 13 | #include "WriterUtils.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 14 | #include "lld/Common/ErrorHandler.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 15 | #include "llvm/ADT/Twine.h" |
Rui Ueyama | 1184253 | 2018-02-16 20:38:00 | [diff] [blame] | 16 | #include "llvm/Support/LEB128.h" |
Reid Kleckner | 932f027 | 2020-05-05 03:03:19 | [diff] [blame] | 17 | #include "llvm/Support/Parallel.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 18 | |
| 19 | #define DEBUG_TYPE "lld" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace llvm::wasm; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 23 | |
Fangrui Song | 33c59ab | 2019-10-10 05:25:39 | [diff] [blame] | 24 | namespace lld { |
| 25 | |
| 26 | // Returns a string, e.g. "FUNCTION(.text)". |
| 27 | std::string toString(const wasm::OutputSection &sec) { |
| 28 | if (!sec.name.empty()) |
| 29 | return (sec.getSectionName() + "(" + sec.name + ")").str(); |
Benjamin Kramer | adcd026 | 2020-01-28 19:23:46 | [diff] [blame] | 30 | return std::string(sec.getSectionName()); |
Fangrui Song | 33c59ab | 2019-10-10 05:25:39 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | namespace wasm { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 34 | static StringRef sectionTypeToString(uint32_t sectionType) { |
| 35 | switch (sectionType) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 36 | case WASM_SEC_CUSTOM: |
| 37 | return "CUSTOM"; |
| 38 | case WASM_SEC_TYPE: |
| 39 | return "TYPE"; |
| 40 | case WASM_SEC_IMPORT: |
| 41 | return "IMPORT"; |
| 42 | case WASM_SEC_FUNCTION: |
| 43 | return "FUNCTION"; |
| 44 | case WASM_SEC_TABLE: |
| 45 | return "TABLE"; |
| 46 | case WASM_SEC_MEMORY: |
| 47 | return "MEMORY"; |
| 48 | case WASM_SEC_GLOBAL: |
| 49 | return "GLOBAL"; |
Heejin Ahn | e915a71 | 2018-12-08 06:17:43 | [diff] [blame] | 50 | case WASM_SEC_EVENT: |
| 51 | return "EVENT"; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 52 | case WASM_SEC_EXPORT: |
| 53 | return "EXPORT"; |
| 54 | case WASM_SEC_START: |
| 55 | return "START"; |
| 56 | case WASM_SEC_ELEM: |
| 57 | return "ELEM"; |
| 58 | case WASM_SEC_CODE: |
| 59 | return "CODE"; |
| 60 | case WASM_SEC_DATA: |
| 61 | return "DATA"; |
Thomas Lively | 84771e2 | 2019-04-19 23:40:36 | [diff] [blame] | 62 | case WASM_SEC_DATACOUNT: |
| 63 | return "DATACOUNT"; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 64 | default: |
| 65 | fatal("invalid section type"); |
| 66 | } |
| 67 | } |
| 68 | |
Rui Ueyama | 22c8f33 | 2018-02-28 17:33:04 | [diff] [blame] | 69 | StringRef OutputSection::getSectionName() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 70 | return sectionTypeToString(type); |
Sam Clegg | 0d0dd39 | 2017-12-19 17:09:45 | [diff] [blame] | 71 | } |
| 72 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 73 | void OutputSection::createHeader(size_t bodySize) { |
| 74 | raw_string_ostream os(header); |
| 75 | debugWrite(os.tell(), "section type [" + getSectionName() + "]"); |
| 76 | encodeULEB128(type, os); |
| 77 | writeUleb128(os, bodySize, "section size"); |
| 78 | os.flush(); |
| 79 | log("createHeader: " + toString(*this) + " body=" + Twine(bodySize) + |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 80 | " total=" + Twine(getSize())); |
| 81 | } |
| 82 | |
Sam Clegg | d029bf0 | 2019-05-16 21:36:06 | [diff] [blame] | 83 | void CodeSection::finalizeContents() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 84 | raw_string_ostream os(codeSectionHeader); |
| 85 | writeUleb128(os, functions.size(), "function count"); |
| 86 | os.flush(); |
| 87 | bodySize = codeSectionHeader.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 88 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 89 | for (InputFunction *func : functions) { |
| 90 | func->outputOffset = bodySize; |
| 91 | func->calculateSize(); |
| 92 | bodySize += func->getSize(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 93 | } |
| 94 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 95 | createHeader(bodySize); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 96 | } |
| 97 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 98 | void CodeSection::writeTo(uint8_t *buf) { |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 | [diff] [blame] | 99 | log("writing " + toString(*this)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 100 | log(" size=" + Twine(getSize())); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 101 | log(" headersize=" + Twine(header.size())); |
| 102 | log(" codeheadersize=" + Twine(codeSectionHeader.size())); |
| 103 | buf += offset; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 104 | |
| 105 | // Write section header |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 106 | memcpy(buf, header.data(), header.size()); |
| 107 | buf += header.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 108 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 109 | // Write code section headers |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 110 | memcpy(buf, codeSectionHeader.data(), codeSectionHeader.size()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 111 | |
| 112 | // Write code section bodies |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 113 | for (const InputChunk *chunk : functions) |
| 114 | chunk->writeTo(buf); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 115 | } |
| 116 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 | [diff] [blame] | 117 | uint32_t CodeSection::getNumRelocations() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 118 | uint32_t count = 0; |
| 119 | for (const InputChunk *func : functions) |
| 120 | count += func->getNumRelocations(); |
| 121 | return count; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 122 | } |
| 123 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 124 | void CodeSection::writeRelocations(raw_ostream &os) const { |
| 125 | for (const InputChunk *c : functions) |
| 126 | c->writeRelocations(os); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 127 | } |
| 128 | |
Sam Clegg | d029bf0 | 2019-05-16 21:36:06 | [diff] [blame] | 129 | void DataSection::finalizeContents() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 130 | raw_string_ostream os(dataSectionHeader); |
Thomas Lively | 190dacc | 2019-10-15 19:05:11 | [diff] [blame] | 131 | unsigned segmentCount = |
| 132 | std::count_if(segments.begin(), segments.end(), |
| 133 | [](OutputSegment *segment) { return !segment->isBss; }); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 134 | |
Thomas Lively | 190dacc | 2019-10-15 19:05:11 | [diff] [blame] | 135 | writeUleb128(os, segmentCount, "data segment count"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 136 | os.flush(); |
| 137 | bodySize = dataSectionHeader.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 138 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 139 | assert((!config->isPic || segments.size() <= 1) && |
Bill Wendling | 858e351 | 2019-07-08 22:05:02 | [diff] [blame] | 140 | "Currenly only a single data segment is supported in PIC mode"); |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 | [diff] [blame] | 141 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 142 | for (OutputSegment *segment : segments) { |
Thomas Lively | 190dacc | 2019-10-15 19:05:11 | [diff] [blame] | 143 | if (segment->isBss) |
| 144 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 145 | raw_string_ostream os(segment->header); |
| 146 | writeUleb128(os, segment->initFlags, "init flags"); |
| 147 | if (segment->initFlags & WASM_SEGMENT_HAS_MEMINDEX) |
| 148 | writeUleb128(os, 0, "memory index"); |
| 149 | if ((segment->initFlags & WASM_SEGMENT_IS_PASSIVE) == 0) { |
| 150 | WasmInitExpr initExpr; |
| 151 | if (config->isPic) { |
| 152 | initExpr.Opcode = WASM_OPCODE_GLOBAL_GET; |
| 153 | initExpr.Value.Global = WasmSym::memoryBase->getGlobalIndex(); |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 | [diff] [blame] | 154 | } else { |
Wouter van Oortmerssen | 582fd47 | 2020-08-07 20:24:43 | [diff] [blame] | 155 | // FIXME(wvo): I64? |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 156 | initExpr.Opcode = WASM_OPCODE_I32_CONST; |
| 157 | initExpr.Value.Int32 = segment->startVA; |
Thomas Lively | 6004d9a | 2019-07-03 22:04:54 | [diff] [blame] | 158 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 159 | writeInitExpr(os, initExpr); |
Sam Clegg | bfb7534 | 2018-11-15 00:37:21 | [diff] [blame] | 160 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 161 | writeUleb128(os, segment->size, "segment size"); |
| 162 | os.flush(); |
Rui Ueyama | ac95bb1 | 2018-04-05 19:37:31 | [diff] [blame] | 163 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 164 | segment->sectionOffset = bodySize; |
| 165 | bodySize += segment->header.size() + segment->size; |
| 166 | log("Data segment: size=" + Twine(segment->size) + ", startVA=" + |
| 167 | Twine::utohexstr(segment->startVA) + ", name=" + segment->name); |
Rui Ueyama | ac95bb1 | 2018-04-05 19:37:31 | [diff] [blame] | 168 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 169 | for (InputSegment *inputSeg : segment->inputSegments) |
| 170 | inputSeg->outputOffset = segment->sectionOffset + segment->header.size() + |
| 171 | inputSeg->outputSegmentOffset; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 172 | } |
| 173 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 174 | createHeader(bodySize); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 175 | } |
| 176 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 177 | void DataSection::writeTo(uint8_t *buf) { |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 | [diff] [blame] | 178 | log("writing " + toString(*this) + " size=" + Twine(getSize()) + |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 179 | " body=" + Twine(bodySize)); |
| 180 | buf += offset; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 181 | |
| 182 | // Write section header |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 183 | memcpy(buf, header.data(), header.size()); |
| 184 | buf += header.size(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 185 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 186 | // Write data section headers |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 187 | memcpy(buf, dataSectionHeader.data(), dataSectionHeader.size()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 188 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 189 | for (const OutputSegment *segment : segments) { |
Thomas Lively | 190dacc | 2019-10-15 19:05:11 | [diff] [blame] | 190 | if (segment->isBss) |
| 191 | continue; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 192 | // Write data segment header |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 193 | uint8_t *segStart = buf + segment->sectionOffset; |
| 194 | memcpy(segStart, segment->header.data(), segment->header.size()); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 195 | |
| 196 | // Write segment data payload |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 197 | for (const InputChunk *chunk : segment->inputSegments) |
| 198 | chunk->writeTo(buf); |
Rui Ueyama | 5081e41 | 2019-04-17 02:12:47 | [diff] [blame] | 199 | } |
Sam Clegg | 5e8cba9 | 2017-12-19 20:45:15 | [diff] [blame] | 200 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 201 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 | [diff] [blame] | 202 | uint32_t DataSection::getNumRelocations() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 203 | uint32_t count = 0; |
| 204 | for (const OutputSegment *seg : segments) |
| 205 | for (const InputChunk *inputSeg : seg->inputSegments) |
| 206 | count += inputSeg->getNumRelocations(); |
| 207 | return count; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 208 | } |
| 209 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 210 | void DataSection::writeRelocations(raw_ostream &os) const { |
| 211 | for (const OutputSegment *seg : segments) |
| 212 | for (const InputChunk *c : seg->inputSegments) |
| 213 | c->writeRelocations(os); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 | [diff] [blame] | 214 | } |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 | [diff] [blame] | 215 | |
Thomas Lively | 190dacc | 2019-10-15 19:05:11 | [diff] [blame] | 216 | bool DataSection::isNeeded() const { |
| 217 | for (const OutputSegment *seg : segments) |
| 218 | if (!seg->isBss) |
| 219 | return true; |
| 220 | return false; |
| 221 | } |
| 222 | |
Sam Clegg | d029bf0 | 2019-05-16 21:36:06 | [diff] [blame] | 223 | void CustomSection::finalizeContents() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 224 | raw_string_ostream os(nameData); |
| 225 | encodeULEB128(name.size(), os); |
| 226 | os << name; |
| 227 | os.flush(); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 | [diff] [blame] | 228 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 229 | for (InputSection *section : inputSections) { |
| 230 | section->outputOffset = payloadSize; |
| 231 | section->outputSec = this; |
| 232 | payloadSize += section->getSize(); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 | [diff] [blame] | 233 | } |
| 234 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 235 | createHeader(payloadSize + nameData.size()); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 | [diff] [blame] | 236 | } |
| 237 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 238 | void CustomSection::writeTo(uint8_t *buf) { |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 | [diff] [blame] | 239 | log("writing " + toString(*this) + " size=" + Twine(getSize()) + |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 240 | " chunks=" + Twine(inputSections.size())); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 | [diff] [blame] | 241 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 242 | assert(offset); |
| 243 | buf += offset; |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 | [diff] [blame] | 244 | |
| 245 | // Write section header |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 246 | memcpy(buf, header.data(), header.size()); |
| 247 | buf += header.size(); |
| 248 | memcpy(buf, nameData.data(), nameData.size()); |
| 249 | buf += nameData.size(); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 | [diff] [blame] | 250 | |
| 251 | // Write custom sections payload |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 252 | for (const InputSection *section : inputSections) |
| 253 | section->writeTo(buf); |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 | [diff] [blame] | 254 | } |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 | [diff] [blame] | 255 | |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 | [diff] [blame] | 256 | uint32_t CustomSection::getNumRelocations() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 257 | uint32_t count = 0; |
| 258 | for (const InputSection *inputSect : inputSections) |
| 259 | count += inputSect->getNumRelocations(); |
| 260 | return count; |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 | [diff] [blame] | 261 | } |
| 262 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 | [diff] [blame] | 263 | void CustomSection::writeRelocations(raw_ostream &os) const { |
| 264 | for (const InputSection *s : inputSections) |
| 265 | s->writeRelocations(os); |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 | [diff] [blame] | 266 | } |
Fangrui Song | 33c59ab | 2019-10-10 05:25:39 | [diff] [blame] | 267 | |
| 268 | } // namespace wasm |
| 269 | } // namespace lld |