2017-11-18 02:14:09 +08:00
|
|
|
//===- OutputSections.h -----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
|
|
|
|
using llvm::raw_ostream;
|
|
|
|
using llvm::raw_string_ostream;
|
|
|
|
|
|
|
|
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:
|
|
|
|
raw_string_ostream BodyOutputStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
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-04-11 00:12:49 +08:00
|
|
|
// Represents a custom section in the output file. Wasm custom sections are
|
|
|
|
// 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
|