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