2017-11-18 02:14:09 +08:00
|
|
|
//===- OutputSections.h -----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_WASM_OUTPUT_SECTIONS_H
|
|
|
|
#define LLD_WASM_OUTPUT_SECTIONS_H
|
|
|
|
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "WriterUtils.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2018-11-27 09:08:16 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
|
|
|
|
namespace wasm {
|
|
|
|
class OutputSection;
|
|
|
|
}
|
2017-12-20 13:14:48 +08:00
|
|
|
std::string toString(const wasm::OutputSection &Section);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class OutputSegment;
|
|
|
|
|
|
|
|
class OutputSection {
|
|
|
|
public:
|
|
|
|
OutputSection(uint32_t Type, std::string Name = "")
|
|
|
|
: Type(Type), Name(Name) {}
|
|
|
|
virtual ~OutputSection() = default;
|
|
|
|
|
2018-03-01 01:33:04 +08:00
|
|
|
StringRef getSectionName() const;
|
2017-11-18 02:14:09 +08:00
|
|
|
void setOffset(size_t NewOffset) {
|
2017-12-20 13:14:48 +08:00
|
|
|
log("setOffset: " + toString(*this) + ": " + Twine(NewOffset));
|
2017-11-18 02:14:09 +08:00
|
|
|
Offset = NewOffset;
|
|
|
|
}
|
|
|
|
void createHeader(size_t BodySize);
|
|
|
|
virtual size_t getSize() const = 0;
|
|
|
|
virtual void writeTo(uint8_t *Buf) = 0;
|
|
|
|
virtual void finalizeContents() {}
|
2017-12-20 13:14:48 +08:00
|
|
|
virtual uint32_t numRelocations() const { return 0; }
|
|
|
|
virtual void writeRelocations(raw_ostream &OS) const {}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
std::string Header;
|
|
|
|
uint32_t Type;
|
|
|
|
std::string Name;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
size_t Offset = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SyntheticSection : public OutputSection {
|
|
|
|
public:
|
|
|
|
SyntheticSection(uint32_t Type, std::string Name = "")
|
|
|
|
: OutputSection(Type, Name), BodyOutputStream(Body) {
|
|
|
|
if (!Name.empty())
|
2018-02-17 04:38:00 +08:00
|
|
|
writeStr(BodyOutputStream, Name, "section name");
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeTo(uint8_t *Buf) override {
|
|
|
|
assert(Offset);
|
2017-12-20 13:14:48 +08:00
|
|
|
log("writing " + toString(*this));
|
2017-11-18 02:14:09 +08:00
|
|
|
memcpy(Buf + Offset, Header.data(), Header.size());
|
|
|
|
memcpy(Buf + Offset + Header.size(), Body.data(), Body.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getSize() const override { return Header.size() + Body.size(); }
|
|
|
|
|
|
|
|
void finalizeContents() override {
|
|
|
|
BodyOutputStream.flush();
|
|
|
|
createHeader(Body.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream &getStream() { return BodyOutputStream; }
|
|
|
|
|
|
|
|
std::string Body;
|
|
|
|
|
|
|
|
protected:
|
2018-11-27 09:08:16 +08:00
|
|
|
llvm::raw_string_ostream BodyOutputStream;
|
2017-11-18 02:14:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class CodeSection : public OutputSection {
|
|
|
|
public:
|
2018-01-10 07:56:44 +08:00
|
|
|
explicit CodeSection(ArrayRef<InputFunction *> Functions);
|
2017-11-18 02:14:09 +08:00
|
|
|
size_t getSize() const override { return Header.size() + BodySize; }
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
|
|
|
uint32_t numRelocations() const override;
|
|
|
|
void writeRelocations(raw_ostream &OS) const override;
|
|
|
|
|
|
|
|
protected:
|
2018-01-10 07:56:44 +08:00
|
|
|
ArrayRef<InputFunction *> Functions;
|
2017-11-18 02:14:09 +08:00
|
|
|
std::string CodeSectionHeader;
|
|
|
|
size_t BodySize = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DataSection : public OutputSection {
|
|
|
|
public:
|
2017-12-08 09:09:21 +08:00
|
|
|
explicit DataSection(ArrayRef<OutputSegment *> Segments);
|
2017-11-18 02:14:09 +08:00
|
|
|
size_t getSize() const override { return Header.size() + BodySize; }
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
2017-12-20 04:45:15 +08:00
|
|
|
uint32_t numRelocations() const override;
|
2017-11-18 02:14:09 +08:00
|
|
|
void writeRelocations(raw_ostream &OS) const override;
|
|
|
|
|
|
|
|
protected:
|
2017-12-08 09:09:21 +08:00
|
|
|
ArrayRef<OutputSegment *> Segments;
|
2017-11-18 02:14:09 +08:00
|
|
|
std::string DataSectionHeader;
|
|
|
|
size_t BodySize = 0;
|
|
|
|
};
|
|
|
|
|
2018-08-30 05:03:16 +08:00
|
|
|
// Represents a custom section in the output file. Wasm custom sections are
|
2018-04-11 00:12:49 +08:00
|
|
|
// used for storing user-defined metadata. Unlike the core sections types
|
|
|
|
// they are identified by their string name.
|
|
|
|
// The linker combines custom sections that have the same name by simply
|
|
|
|
// concatenating them.
|
|
|
|
// Note that some custom sections such as "name" and "linking" are handled
|
|
|
|
// separately and are instead synthesized by the linker.
|
|
|
|
class CustomSection : public OutputSection {
|
|
|
|
public:
|
|
|
|
CustomSection(std::string Name, ArrayRef<InputSection *> InputSections);
|
|
|
|
size_t getSize() const override {
|
|
|
|
return Header.size() + NameData.size() + PayloadSize;
|
|
|
|
}
|
|
|
|
void writeTo(uint8_t *Buf) override;
|
2018-05-05 07:14:42 +08:00
|
|
|
uint32_t numRelocations() const override;
|
|
|
|
void writeRelocations(raw_ostream &OS) const override;
|
2018-04-11 00:12:49 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
size_t PayloadSize;
|
|
|
|
ArrayRef<InputSection *> InputSections;
|
|
|
|
std::string NameData;
|
|
|
|
};
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif // LLD_WASM_OUTPUT_SECTIONS_H
|