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 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 23 | std::string toString(const wasm::OutputSection §ion); |
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: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 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; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 36 | void setOffset(size_t newOffset) { |
| 37 | log("setOffset: " + toString(*this) + ": " + Twine(newOffset)); |
| 38 | offset = newOffset; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 39 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 40 | void createHeader(size_t bodySize); |
Sam Clegg | 8fcf012 | 2019-05-21 09:13:09 +0000 | [diff] [blame] | 41 | virtual bool isNeeded() const { return true; } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 42 | virtual size_t getSize() const = 0; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 43 | virtual void writeTo(uint8_t *buf) = 0; |
Sam Clegg | d029bf0 | 2019-05-16 21:36:06 +0000 | [diff] [blame] | 44 | virtual void finalizeContents() = 0; |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 45 | virtual uint32_t getNumRelocations() const { return 0; } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 46 | virtual void writeRelocations(raw_ostream &os) const {} |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 47 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 48 | std::string header; |
| 49 | uint32_t type; |
| 50 | uint32_t sectionIndex = UINT32_MAX; |
| 51 | std::string name; |
| 52 | OutputSectionSymbol *sectionSym = nullptr; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 53 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 54 | protected: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 55 | size_t offset = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 58 | class CodeSection : public OutputSection { |
| 59 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 60 | explicit CodeSection(ArrayRef<InputFunction *> functions) |
| 61 | : OutputSection(llvm::wasm::WASM_SEC_CODE), functions(functions) {} |
Sam Clegg | d029bf0 | 2019-05-16 21:36:06 +0000 | [diff] [blame] | 62 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 63 | size_t getSize() const override { return header.size() + bodySize; } |
| 64 | void writeTo(uint8_t *buf) override; |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 65 | uint32_t getNumRelocations() const override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 66 | void writeRelocations(raw_ostream &os) const override; |
| 67 | bool isNeeded() const override { return functions.size() > 0; } |
Sam Clegg | d029bf0 | 2019-05-16 21:36:06 +0000 | [diff] [blame] | 68 | void finalizeContents() override; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 69 | |
| 70 | protected: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 71 | ArrayRef<InputFunction *> functions; |
| 72 | std::string codeSectionHeader; |
| 73 | size_t bodySize = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | class DataSection : public OutputSection { |
| 77 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 78 | explicit DataSection(ArrayRef<OutputSegment *> segments) |
| 79 | : OutputSection(llvm::wasm::WASM_SEC_DATA), segments(segments) {} |
Sam Clegg | d029bf0 | 2019-05-16 21:36:06 +0000 | [diff] [blame] | 80 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 81 | size_t getSize() const override { return header.size() + bodySize; } |
| 82 | void writeTo(uint8_t *buf) override; |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 83 | uint32_t getNumRelocations() const override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 84 | void writeRelocations(raw_ostream &os) const override; |
Thomas Lively | 190dacc | 2019-10-15 19:05:11 +0000 | [diff] [blame^] | 85 | bool isNeeded() const override; |
Sam Clegg | d029bf0 | 2019-05-16 21:36:06 +0000 | [diff] [blame] | 86 | void finalizeContents() override; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 87 | |
| 88 | protected: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 89 | ArrayRef<OutputSegment *> segments; |
| 90 | std::string dataSectionHeader; |
| 91 | size_t bodySize = 0; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Heejin Ahn | 4821ebf | 2018-08-29 21:03:16 +0000 | [diff] [blame] | 94 | // Represents a custom section in the output file. Wasm custom sections are |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 95 | // used for storing user-defined metadata. Unlike the core sections types |
| 96 | // they are identified by their string name. |
| 97 | // The linker combines custom sections that have the same name by simply |
| 98 | // concatenating them. |
| 99 | // Note that some custom sections such as "name" and "linking" are handled |
| 100 | // separately and are instead synthesized by the linker. |
| 101 | class CustomSection : public OutputSection { |
| 102 | public: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 103 | CustomSection(std::string name, ArrayRef<InputSection *> inputSections) |
| 104 | : OutputSection(llvm::wasm::WASM_SEC_CUSTOM, name), |
| 105 | inputSections(inputSections) {} |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 106 | size_t getSize() const override { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 107 | return header.size() + nameData.size() + payloadSize; |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 108 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 109 | void writeTo(uint8_t *buf) override; |
Rui Ueyama | 7e296ad | 2019-07-10 09:10:01 +0000 | [diff] [blame] | 110 | uint32_t getNumRelocations() const override; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 111 | void writeRelocations(raw_ostream &os) const override; |
Sam Clegg | d029bf0 | 2019-05-16 21:36:06 +0000 | [diff] [blame] | 112 | void finalizeContents() override; |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 113 | |
| 114 | protected: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 115 | size_t payloadSize = 0; |
| 116 | ArrayRef<InputSection *> inputSections; |
| 117 | std::string nameData; |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 120 | } // namespace wasm |
| 121 | } // namespace lld |
| 122 | |
| 123 | #endif // LLD_WASM_OUTPUT_SECTIONS_H |