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 "llvm/ADT/Twine.h"
|
2018-02-17 04:38:00 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
[Support] Move LLD's parallel algorithm wrappers to support
Essentially takes the lld/Common/Threads.h wrappers and moves them to
the llvm/Support/Paralle.h algorithm header.
The changes are:
- Remove policy parameter, since all clients use `par`.
- Rename the methods to `parallelSort` etc to match LLVM style, since
they are no longer C++17 pstl compatible.
- Move algorithms from llvm::parallel:: to llvm::, since they have
"parallel" in the name and are no longer overloads of the regular
algorithms.
- Add range overloads
- Use the sequential algorithm directly when 1 thread is requested
(skips task grouping)
- Fix the index type of parallelForEachN to size_t. Nobody in LLVM was
using any other parameter, and it made overload resolution hard for
for_each_n(par, 0, foo.size(), ...) because 0 is int, not size_t.
Remove Threads.h and update LLD for that.
This is a prerequisite for parallel public symbol processing in the PDB
library, which is in LLVM.
Reviewed By: MaskRay, aganea
Differential Revision: https://reviews.llvm.org/D79390
2020-05-05 11:03:19 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
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();
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(sec.getSectionName());
|
2019-10-10 13:25:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace wasm {
|
2017-11-18 02:14:09 +08:00
|
|
|
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
|
|
|
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) {
|
2020-03-28 07:52:27 +08:00
|
|
|
func->outputSec = this;
|
2018-02-20 12:26:26 +08:00
|
|
|
func->outputOffset = bodySize;
|
2018-05-19 07:28:05 +08:00
|
|
|
func->calculateSize();
|
2020-12-03 07:55:25 +08:00
|
|
|
// All functions should have a non-empty body at this point
|
|
|
|
assert(func->getSize());
|
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);
|
2019-10-16 03:05:11 +08:00
|
|
|
unsigned segmentCount =
|
|
|
|
std::count_if(segments.begin(), segments.end(),
|
|
|
|
[](OutputSegment *segment) { return !segment->isBss; });
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2020-11-11 09:46:52 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
unsigned activeCount = std::count_if(
|
|
|
|
segments.begin(), segments.end(), [](OutputSegment *segment) {
|
2020-11-30 21:55:29 +08:00
|
|
|
return (segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0;
|
2020-11-11 09:46:52 +08:00
|
|
|
});
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert((!config->isPic || activeCount <= 1) &&
|
|
|
|
"Currenly only a single data segment is supported in PIC mode");
|
|
|
|
|
2019-10-16 03:05:11 +08:00
|
|
|
writeUleb128(os, segmentCount, "data segment count");
|
2017-11-18 02:14:09 +08:00
|
|
|
os.flush();
|
|
|
|
bodySize = dataSectionHeader.size();
|
|
|
|
|
|
|
|
for (OutputSegment *segment : segments) {
|
2019-10-16 03:05:11 +08:00
|
|
|
if (segment->isBss)
|
|
|
|
continue;
|
2017-11-18 02:14:09 +08:00
|
|
|
raw_string_ostream os(segment->header);
|
2019-07-04 06:04:54 +08:00
|
|
|
writeUleb128(os, segment->initFlags, "init flags");
|
2020-11-30 21:55:29 +08:00
|
|
|
if (segment->initFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX)
|
2019-07-04 06:04:54 +08:00
|
|
|
writeUleb128(os, 0, "memory index");
|
2020-11-30 21:55:29 +08:00
|
|
|
if ((segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
|
2019-07-04 06:04:54 +08:00
|
|
|
WasmInitExpr initExpr;
|
|
|
|
if (config->isPic) {
|
|
|
|
initExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
|
|
|
|
initExpr.Value.Global = WasmSym::memoryBase->getGlobalIndex();
|
2021-01-29 08:38:24 +08:00
|
|
|
} else if (config->is64.getValueOr(false)) {
|
|
|
|
initExpr.Opcode = WASM_OPCODE_I64_CONST;
|
|
|
|
initExpr.Value.Int64 = static_cast<int64_t>(segment->startVA);
|
2019-07-04 06:04:54 +08:00
|
|
|
} else {
|
|
|
|
initExpr.Opcode = WASM_OPCODE_I32_CONST;
|
2021-01-29 08:38:24 +08:00
|
|
|
initExpr.Value.Int32 = static_cast<int32_t>(segment->startVA);
|
2019-07-04 06:04:54 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2020-03-28 07:52:27 +08:00
|
|
|
for (InputSegment *inputSeg : segment->inputSegments) {
|
|
|
|
inputSeg->outputSec = this;
|
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;
|
2020-03-28 07:52:27 +08:00
|
|
|
}
|
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) {
|
2019-10-16 03:05:11 +08:00
|
|
|
if (segment->isBss)
|
|
|
|
continue;
|
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-10-16 03:05:11 +08:00
|
|
|
bool DataSection::isNeeded() const {
|
|
|
|
for (const OutputSegment *seg : segments)
|
|
|
|
if (!seg->isBss)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-10 02:57:27 +08:00
|
|
|
assert(!section->discarded);
|
2019-05-21 17:13:09 +08:00
|
|
|
section->outputSec = this;
|
2020-03-28 07:52:27 +08:00
|
|
|
section->outputOffset = payloadSize;
|
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);
|
|
|
|
}
|
2019-10-10 13:25:39 +08:00
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|