2017-11-18 02:14:09 +08:00
|
|
|
//===- OutputSections.cpp -------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OutputSections.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "OutputSegment.h"
|
2018-02-28 08:52:42 +08:00
|
|
|
#include "WriterUtils.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
|
|
|
#include "lld/Common/Threads.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2018-02-17 04:38:00 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::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";
|
2018-12-08 14:17:43 +08:00
|
|
|
case WASM_SEC_EVENT:
|
|
|
|
return "EVENT";
|
2017-11-18 02:14:09 +08:00
|
|
|
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";
|
2019-04-20 07:40:36 +08:00
|
|
|
case WASM_SEC_DATACOUNT:
|
|
|
|
return "DATACOUNT";
|
2017-11-18 02:14:09 +08:00
|
|
|
default:
|
|
|
|
fatal("invalid section type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 01:33:04 +08:00
|
|
|
// Returns a string, e.g. "FUNCTION(.text)".
|
|
|
|
std::string lld::toString(const OutputSection &sec) {
|
|
|
|
if (!sec.name.empty())
|
|
|
|
return (sec.getSectionName() + "(" + sec.name + ")").str();
|
|
|
|
return sec.getSectionName();
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 01:33:04 +08:00
|
|
|
StringRef OutputSection::getSectionName() const {
|
2017-12-20 01:09:45 +08:00
|
|
|
return sectionTypeToString(type);
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void OutputSection::createHeader(size_t bodySize) {
|
|
|
|
raw_string_ostream os(header);
|
2018-03-01 01:33:04 +08:00
|
|
|
debugWrite(os.tell(), "section type [" + getSectionName() + "]");
|
2018-02-17 04:38:00 +08:00
|
|
|
encodeULEB128(type, os);
|
2017-11-18 02:14:09 +08:00
|
|
|
writeUleb128(os, bodySize, "section size");
|
|
|
|
os.flush();
|
2017-12-20 13:14:48 +08:00
|
|
|
log("createHeader: " + toString(*this) + " body=" + Twine(bodySize) +
|
2017-11-18 02:14:09 +08:00
|
|
|
" total=" + Twine(getSize()));
|
|
|
|
}
|
|
|
|
|
2019-05-17 05:36:06 +08:00
|
|
|
void CodeSection::finalizeContents() {
|
2017-11-18 02:14:09 +08:00
|
|
|
raw_string_ostream os(codeSectionHeader);
|
2018-01-10 07:56:44 +08:00
|
|
|
writeUleb128(os, functions.size(), "function count");
|
2017-11-18 02:14:09 +08:00
|
|
|
os.flush();
|
|
|
|
bodySize = codeSectionHeader.size();
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-05-19 07:28:05 +08:00
|
|
|
for (InputFunction *func : functions) {
|
2018-02-20 12:26:26 +08:00
|
|
|
func->outputOffset = bodySize;
|
2018-05-19 07:28:05 +08:00
|
|
|
func->calculateSize();
|
2018-01-10 09:13:34 +08:00
|
|
|
bodySize += func->getSize();
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
createHeader(bodySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeSection::writeTo(uint8_t *buf) {
|
2017-12-20 13:14:48 +08:00
|
|
|
log("writing " + toString(*this));
|
2017-11-18 02:14:09 +08:00
|
|
|
log(" size=" + Twine(getSize()));
|
2018-01-10 07:56:44 +08:00
|
|
|
log(" headersize=" + Twine(header.size()));
|
|
|
|
log(" codeheadersize=" + Twine(codeSectionHeader.size()));
|
2017-11-18 02:14:09 +08:00
|
|
|
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
|
2019-04-17 10:12:47 +08:00
|
|
|
for (const InputChunk *chunk : functions)
|
|
|
|
chunk->writeTo(buf);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
uint32_t CodeSection::getNumRelocations() const {
|
2017-11-18 02:14:09 +08:00
|
|
|
uint32_t count = 0;
|
2018-01-10 09:13:34 +08:00
|
|
|
for (const InputChunk *func : functions)
|
2019-07-10 17:10:01 +08:00
|
|
|
count += func->getNumRelocations();
|
2017-11-18 02:14:09 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeSection::writeRelocations(raw_ostream &os) const {
|
2018-02-20 12:26:26 +08:00
|
|
|
for (const InputChunk *c : functions)
|
|
|
|
c->writeRelocations(os);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-05-17 05:36:06 +08:00
|
|
|
void DataSection::finalizeContents() {
|
2017-11-18 02:14:09 +08:00
|
|
|
raw_string_ostream os(dataSectionHeader);
|
|
|
|
|
|
|
|
writeUleb128(os, segments.size(), "data segment count");
|
|
|
|
os.flush();
|
|
|
|
bodySize = dataSectionHeader.size();
|
|
|
|
|
2019-07-09 06:05:02 +08:00
|
|
|
assert((!config->isPic || segments.size() <= 1) &&
|
|
|
|
"Currenly only a single data segment is supported in PIC mode");
|
2019-07-04 06:04:54 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
for (OutputSegment *segment : segments) {
|
|
|
|
raw_string_ostream os(segment->header);
|
2019-07-04 06:04:54 +08:00
|
|
|
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);
|
2018-11-15 08:37:21 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
writeUleb128(os, segment->size, "segment size");
|
|
|
|
os.flush();
|
2018-04-06 03:37:31 +08:00
|
|
|
|
|
|
|
segment->sectionOffset = bodySize;
|
2018-01-11 03:22:42 +08:00
|
|
|
bodySize += segment->header.size() + segment->size;
|
2019-06-05 05:13:41 +08:00
|
|
|
log("Data segment: size=" + Twine(segment->size) + ", startVA=" +
|
|
|
|
Twine::utohexstr(segment->startVA) + ", name=" + segment->name);
|
2018-04-06 03:37:31 +08:00
|
|
|
|
2018-01-11 03:22:42 +08:00
|
|
|
for (InputSegment *inputSeg : segment->inputSegments)
|
2018-04-06 03:37:31 +08:00
|
|
|
inputSeg->outputOffset = segment->sectionOffset + segment->header.size() +
|
2018-02-20 12:26:26 +08:00
|
|
|
inputSeg->outputSegmentOffset;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
createHeader(bodySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSection::writeTo(uint8_t *buf) {
|
2017-12-20 13:14:48 +08:00
|
|
|
log("writing " + toString(*this) + " size=" + Twine(getSize()) +
|
2017-11-18 02:14:09 +08:00
|
|
|
" 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());
|
|
|
|
|
2019-04-17 10:12:47 +08:00
|
|
|
for (const OutputSegment *segment : segments) {
|
2017-11-18 02:14:09 +08:00
|
|
|
// Write data segment header
|
2018-04-06 03:37:31 +08:00
|
|
|
uint8_t *segStart = buf + segment->sectionOffset;
|
2017-11-18 02:14:09 +08:00
|
|
|
memcpy(segStart, segment->header.data(), segment->header.size());
|
|
|
|
|
|
|
|
// Write segment data payload
|
2018-01-11 03:22:42 +08:00
|
|
|
for (const InputChunk *chunk : segment->inputSegments)
|
2018-02-28 08:31:16 +08:00
|
|
|
chunk->writeTo(buf);
|
2019-04-17 10:12:47 +08:00
|
|
|
}
|
2017-12-20 04:45:15 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
uint32_t DataSection::getNumRelocations() const {
|
2017-12-20 04:45:15 +08:00
|
|
|
uint32_t count = 0;
|
|
|
|
for (const OutputSegment *seg : segments)
|
2018-01-10 09:13:34 +08:00
|
|
|
for (const InputChunk *inputSeg : seg->inputSegments)
|
2019-07-10 17:10:01 +08:00
|
|
|
count += inputSeg->getNumRelocations();
|
2017-12-20 04:45:15 +08:00
|
|
|
return count;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSection::writeRelocations(raw_ostream &os) const {
|
2017-12-20 04:45:15 +08:00
|
|
|
for (const OutputSegment *seg : segments)
|
2018-02-20 12:26:26 +08:00
|
|
|
for (const InputChunk *c : seg->inputSegments)
|
|
|
|
c->writeRelocations(os);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2018-04-11 00:12:49 +08:00
|
|
|
|
2019-05-17 05:36:06 +08:00
|
|
|
void CustomSection::finalizeContents() {
|
2018-04-11 00:12:49 +08:00
|
|
|
raw_string_ostream os(nameData);
|
|
|
|
encodeULEB128(name.size(), os);
|
|
|
|
os << name;
|
|
|
|
os.flush();
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-04-11 00:12:49 +08:00
|
|
|
for (InputSection *section : inputSections) {
|
|
|
|
section->outputOffset = payloadSize;
|
2019-05-21 17:13:09 +08:00
|
|
|
section->outputSec = this;
|
2018-04-11 00:12:49 +08:00
|
|
|
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
|
2019-04-17 10:12:47 +08:00
|
|
|
for (const InputSection *section : inputSections)
|
|
|
|
section->writeTo(buf);
|
2018-04-11 00:12:49 +08:00
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
uint32_t CustomSection::getNumRelocations() const {
|
2018-05-05 07:14:42 +08:00
|
|
|
uint32_t count = 0;
|
|
|
|
for (const InputSection *inputSect : inputSections)
|
2019-07-10 17:10:01 +08:00
|
|
|
count += inputSect->getNumRelocations();
|
2018-05-05 07:14:42 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CustomSection::writeRelocations(raw_ostream &os) const {
|
|
|
|
for (const InputSection *s : inputSections)
|
|
|
|
s->writeRelocations(os);
|
|
|
|
}
|