2017-11-18 02:14:09 +08:00
|
|
|
//===- OutputSections.cpp -------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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";
|
|
|
|
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";
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2018-01-10 07:56:44 +08:00
|
|
|
CodeSection::CodeSection(ArrayRef<InputFunction *> Functions)
|
|
|
|
: OutputSection(WASM_SEC_CODE), Functions(Functions) {
|
|
|
|
assert(Functions.size() > 0);
|
|
|
|
|
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();
|
|
|
|
|
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
|
2018-03-07 21:28:16 +08:00
|
|
|
parallelForEach(Functions,
|
|
|
|
[&](const InputChunk *Chunk) { Chunk->writeTo(Buf); });
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CodeSection::numRelocations() const {
|
|
|
|
uint32_t Count = 0;
|
2018-01-10 09:13:34 +08:00
|
|
|
for (const InputChunk *Func : Functions)
|
2018-02-20 12:26:26 +08:00
|
|
|
Count += Func->NumRelocations();
|
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
|
|
|
}
|
|
|
|
|
2017-12-08 09:09:21 +08:00
|
|
|
DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
|
2017-11-18 02:14:09 +08:00
|
|
|
: OutputSection(WASM_SEC_DATA), Segments(Segments) {
|
|
|
|
raw_string_ostream OS(DataSectionHeader);
|
|
|
|
|
|
|
|
writeUleb128(OS, Segments.size(), "data segment count");
|
|
|
|
OS.flush();
|
|
|
|
BodySize = DataSectionHeader.size();
|
|
|
|
|
|
|
|
for (OutputSegment *Segment : Segments) {
|
|
|
|
raw_string_ostream OS(Segment->Header);
|
|
|
|
writeUleb128(OS, 0, "memory index");
|
|
|
|
writeUleb128(OS, WASM_OPCODE_I32_CONST, "opcode:i32const");
|
|
|
|
writeSleb128(OS, Segment->StartVA, "memory offset");
|
|
|
|
writeUleb128(OS, WASM_OPCODE_END, "opcode:end");
|
|
|
|
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;
|
2017-11-18 02:14:09 +08:00
|
|
|
log("Data segment: size=" + Twine(Segment->Size));
|
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());
|
|
|
|
|
2018-02-28 08:31:16 +08:00
|
|
|
parallelForEach(Segments, [&](const OutputSegment *Segment) {
|
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);
|
2017-11-18 02:14:09 +08:00
|
|
|
});
|
2017-12-20 04:45:15 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2017-12-20 04:45:15 +08:00
|
|
|
uint32_t DataSection::numRelocations() const {
|
|
|
|
uint32_t Count = 0;
|
|
|
|
for (const OutputSegment *Seg : Segments)
|
2018-01-10 09:13:34 +08:00
|
|
|
for (const InputChunk *InputSeg : Seg->InputSegments)
|
2018-02-20 12:26:26 +08:00
|
|
|
Count += InputSeg->NumRelocations();
|
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
|
|
|
|
|
|
|
CustomSection::CustomSection(std::string Name,
|
|
|
|
ArrayRef<InputSection *> InputSections)
|
|
|
|
: OutputSection(WASM_SEC_CUSTOM, Name), PayloadSize(0),
|
|
|
|
InputSections(InputSections) {
|
|
|
|
raw_string_ostream OS(NameData);
|
|
|
|
encodeULEB128(Name.size(), OS);
|
|
|
|
OS << Name;
|
|
|
|
OS.flush();
|
|
|
|
|
|
|
|
for (InputSection *Section : InputSections) {
|
|
|
|
Section->OutputOffset = PayloadSize;
|
|
|
|
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
|
|
|
|
parallelForEach(InputSections,
|
|
|
|
[&](const InputSection *Section) { Section->writeTo(Buf); });
|
|
|
|
}
|
2018-05-05 07:14:42 +08:00
|
|
|
|
|
|
|
uint32_t CustomSection::numRelocations() const {
|
|
|
|
uint32_t Count = 0;
|
|
|
|
for (const InputSection *InputSect : InputSections)
|
|
|
|
Count += InputSect->NumRelocations();
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CustomSection::writeRelocations(raw_ostream &OS) const {
|
|
|
|
for (const InputSection *S : InputSections)
|
|
|
|
S->writeRelocations(OS);
|
|
|
|
}
|