Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1 | //===- OutputSections.h -----------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLD_WASM_OUTPUT_SECTIONS_H |
| 10 | #define LLD_WASM_OUTPUT_SECTIONS_H |
| 11 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 12 | #include "InputChunks.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 13 | #include "WriterUtils.h" |
| 14 | #include "lld/Common/ErrorHandler.h" |
Sam Clegg | 45218f4 | 2018-11-27 01:08:16 +0000 | [diff] [blame] | 15 | #include "lld/Common/LLVM.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 18 | namespace lld { |
| 19 | |
| 20 | namespace wasm { |
| 21 | class OutputSection; |
| 22 | } |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 23 | std::string toString(const wasm::OutputSection &Section); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 24 | |
| 25 | namespace wasm { |
| 26 | |
| 27 | class OutputSegment; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 28 | |
| 29 | class OutputSection { |
| 30 | public: |
| 31 | OutputSection(uint32_t Type, std::string Name = "") |
| 32 | : Type(Type), Name(Name) {} |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 33 | virtual ~OutputSection() = default; |
| 34 | |
Rui Ueyama | 22c8f33 | 2018-02-28 17:33:04 +0000 | [diff] [blame] | 35 | StringRef getSectionName() const; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 36 | void setOffset(size_t NewOffset) { |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 37 | log("setOffset: " + toString(*this) + ": " + Twine(NewOffset)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 38 | Offset = NewOffset; |
| 39 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 40 | void createHeader(size_t BodySize); |
| 41 | virtual size_t getSize() const = 0; |
| 42 | virtual void writeTo(uint8_t *Buf) = 0; |
| 43 | virtual void finalizeContents() {} |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 44 | virtual uint32_t numRelocations() const { return 0; } |
| 45 | virtual void writeRelocations(raw_ostream &OS) const {} |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 46 | |
| 47 | std::string Header; |
| 48 | uint32_t Type; |
| 49 | std::string Name; |
| 50 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 51 | protected: |
| 52 | size_t Offset = 0; |
| 53 | }; |
| 54 | |
| 55 | class SyntheticSection : public OutputSection { |
| 56 | public: |
| 57 | SyntheticSection(uint32_t Type, std::string Name = "") |
| 58 | : OutputSection(Type, Name), BodyOutputStream(Body) { |
| 59 | if (!Name.empty()) |
Rui Ueyama | 1184253 | 2018-02-16 20:38:00 +0000 | [diff] [blame] | 60 | writeStr(BodyOutputStream, Name, "section name"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void writeTo(uint8_t *Buf) override { |
| 64 | assert(Offset); |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame] | 65 | log("writing " + toString(*this)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 66 | memcpy(Buf + Offset, Header.data(), Header.size()); |
| 67 | memcpy(Buf + Offset + Header.size(), Body.data(), Body.size()); |
| 68 | } |
| 69 | |
| 70 | size_t getSize() const override { return Header.size() + Body.size(); } |
| 71 | |
| 72 | void finalizeContents() override { |
| 73 | BodyOutputStream.flush(); |
| 74 | createHeader(Body.size()); |
| 75 | } |
| 76 | |
| 77 | raw_ostream &getStream() { return BodyOutputStream; } |
| 78 | |
| 79 | std::string Body; |
| 80 | |
| 81 | protected: |
Sam Clegg | 45218f4 | 2018-11-27 01:08:16 +0000 | [diff] [blame] | 82 | llvm::raw_string_ostream BodyOutputStream; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 85 | class CodeSection : public OutputSection { |
| 86 | public: |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 87 | explicit CodeSection(ArrayRef<InputFunction *> Functions); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 88 | size_t getSize() const override { return Header.size() + BodySize; } |
| 89 | void writeTo(uint8_t *Buf) override; |
| 90 | uint32_t numRelocations() const override; |
| 91 | void writeRelocations(raw_ostream &OS) const override; |
| 92 | |
| 93 | protected: |
Sam Clegg | 8d146bb | 2018-01-09 23:56:44 +0000 | [diff] [blame] | 94 | ArrayRef<InputFunction *> Functions; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 95 | std::string CodeSectionHeader; |
| 96 | size_t BodySize = 0; |
| 97 | }; |
| 98 | |
| 99 | class DataSection : public OutputSection { |
| 100 | public: |
Sam Clegg | 0fb6faa | 2017-12-08 01:09:21 +0000 | [diff] [blame] | 101 | explicit DataSection(ArrayRef<OutputSegment *> Segments); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 102 | size_t getSize() const override { return Header.size() + BodySize; } |
| 103 | void writeTo(uint8_t *Buf) override; |
Sam Clegg | 5e8cba9 | 2017-12-19 20:45:15 +0000 | [diff] [blame] | 104 | uint32_t numRelocations() const override; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 105 | void writeRelocations(raw_ostream &OS) const override; |
| 106 | |
| 107 | protected: |
Sam Clegg | 0fb6faa | 2017-12-08 01:09:21 +0000 | [diff] [blame] | 108 | ArrayRef<OutputSegment *> Segments; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 109 | std::string DataSectionHeader; |
| 110 | size_t BodySize = 0; |
| 111 | }; |
| 112 | |
Heejin Ahn | 4821ebf | 2018-08-29 21:03:16 +0000 | [diff] [blame] | 113 | // Represents a custom section in the output file. Wasm custom sections are |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 114 | // used for storing user-defined metadata. Unlike the core sections types |
| 115 | // they are identified by their string name. |
| 116 | // The linker combines custom sections that have the same name by simply |
| 117 | // concatenating them. |
| 118 | // Note that some custom sections such as "name" and "linking" are handled |
| 119 | // separately and are instead synthesized by the linker. |
| 120 | class CustomSection : public OutputSection { |
| 121 | public: |
| 122 | CustomSection(std::string Name, ArrayRef<InputSection *> InputSections); |
| 123 | size_t getSize() const override { |
| 124 | return Header.size() + NameData.size() + PayloadSize; |
| 125 | } |
| 126 | void writeTo(uint8_t *Buf) override; |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 127 | uint32_t numRelocations() const override; |
| 128 | void writeRelocations(raw_ostream &OS) const override; |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 129 | |
| 130 | protected: |
| 131 | size_t PayloadSize; |
| 132 | ArrayRef<InputSection *> InputSections; |
| 133 | std::string NameData; |
| 134 | }; |
| 135 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 136 | } // namespace wasm |
| 137 | } // namespace lld |
| 138 | |
| 139 | #endif // LLD_WASM_OUTPUT_SECTIONS_H |