Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1 | //===- OutputSections.h -----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLD_WASM_OUTPUT_SECTIONS_H |
| 11 | #define LLD_WASM_OUTPUT_SECTIONS_H |
| 12 | |
| 13 | #include "InputSegment.h" |
| 14 | #include "WriterUtils.h" |
| 15 | #include "lld/Common/ErrorHandler.h" |
| 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | |
| 18 | using llvm::raw_ostream; |
| 19 | using llvm::raw_string_ostream; |
| 20 | |
| 21 | namespace lld { |
| 22 | |
| 23 | namespace wasm { |
| 24 | class OutputSection; |
| 25 | } |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame^] | 26 | std::string toString(const wasm::OutputSection &Section); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 27 | |
| 28 | namespace wasm { |
| 29 | |
| 30 | class OutputSegment; |
| 31 | class ObjFile; |
| 32 | |
| 33 | class OutputSection { |
| 34 | public: |
| 35 | OutputSection(uint32_t Type, std::string Name = "") |
| 36 | : Type(Type), Name(Name) {} |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 37 | virtual ~OutputSection() = default; |
| 38 | |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame^] | 39 | std::string getSectionName() const; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 40 | void setOffset(size_t NewOffset) { |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame^] | 41 | log("setOffset: " + toString(*this) + ": " + Twine(NewOffset)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 42 | Offset = NewOffset; |
| 43 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 44 | void createHeader(size_t BodySize); |
| 45 | virtual size_t getSize() const = 0; |
| 46 | virtual void writeTo(uint8_t *Buf) = 0; |
| 47 | virtual void finalizeContents() {} |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame^] | 48 | virtual uint32_t numRelocations() const { return 0; } |
| 49 | virtual void writeRelocations(raw_ostream &OS) const {} |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 50 | |
| 51 | std::string Header; |
| 52 | uint32_t Type; |
| 53 | std::string Name; |
| 54 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 55 | protected: |
| 56 | size_t Offset = 0; |
| 57 | }; |
| 58 | |
| 59 | class SyntheticSection : public OutputSection { |
| 60 | public: |
| 61 | SyntheticSection(uint32_t Type, std::string Name = "") |
| 62 | : OutputSection(Type, Name), BodyOutputStream(Body) { |
| 63 | if (!Name.empty()) |
| 64 | writeStr(BodyOutputStream, Name); |
| 65 | } |
| 66 | |
| 67 | void writeTo(uint8_t *Buf) override { |
| 68 | assert(Offset); |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame^] | 69 | log("writing " + toString(*this)); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 70 | memcpy(Buf + Offset, Header.data(), Header.size()); |
| 71 | memcpy(Buf + Offset + Header.size(), Body.data(), Body.size()); |
| 72 | } |
| 73 | |
| 74 | size_t getSize() const override { return Header.size() + Body.size(); } |
| 75 | |
| 76 | void finalizeContents() override { |
| 77 | BodyOutputStream.flush(); |
| 78 | createHeader(Body.size()); |
| 79 | } |
| 80 | |
| 81 | raw_ostream &getStream() { return BodyOutputStream; } |
| 82 | |
| 83 | std::string Body; |
| 84 | |
| 85 | protected: |
| 86 | raw_string_ostream BodyOutputStream; |
| 87 | }; |
| 88 | |
| 89 | // Some synthetic sections (e.g. "name" and "linking") have subsections. |
| 90 | // Just like the synthetic sections themselves these need to be created before |
| 91 | // they can be written out (since they are preceded by their length). This |
| 92 | // class is used to create subsections and then write them into the stream |
| 93 | // of the parent section. |
| 94 | class SubSection : public SyntheticSection { |
| 95 | public: |
| 96 | explicit SubSection(uint32_t Type) : SyntheticSection(Type) {} |
| 97 | |
Sam Clegg | ab2ac29 | 2017-12-20 05:14:48 +0000 | [diff] [blame^] | 98 | std::string getSectionName() const; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 99 | void writeToStream(raw_ostream &OS) { |
| 100 | writeBytes(OS, Header.data(), Header.size()); |
| 101 | writeBytes(OS, Body.data(), Body.size()); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | class CodeSection : public OutputSection { |
| 106 | public: |
Sam Clegg | 0fb6faa | 2017-12-08 01:09:21 +0000 | [diff] [blame] | 107 | explicit CodeSection(uint32_t NumFunctions, ArrayRef<ObjFile *> Objs); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 108 | size_t getSize() const override { return Header.size() + BodySize; } |
| 109 | void writeTo(uint8_t *Buf) override; |
| 110 | uint32_t numRelocations() const override; |
| 111 | void writeRelocations(raw_ostream &OS) const override; |
| 112 | |
| 113 | protected: |
Sam Clegg | 0fb6faa | 2017-12-08 01:09:21 +0000 | [diff] [blame] | 114 | ArrayRef<ObjFile *> InputObjects; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 115 | std::string CodeSectionHeader; |
| 116 | size_t BodySize = 0; |
| 117 | }; |
| 118 | |
| 119 | class DataSection : public OutputSection { |
| 120 | public: |
Sam Clegg | 0fb6faa | 2017-12-08 01:09:21 +0000 | [diff] [blame] | 121 | explicit DataSection(ArrayRef<OutputSegment *> Segments); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 122 | size_t getSize() const override { return Header.size() + BodySize; } |
| 123 | void writeTo(uint8_t *Buf) override; |
Sam Clegg | 5e8cba9 | 2017-12-19 20:45:15 +0000 | [diff] [blame] | 124 | uint32_t numRelocations() const override; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 125 | void writeRelocations(raw_ostream &OS) const override; |
| 126 | |
| 127 | protected: |
Sam Clegg | 0fb6faa | 2017-12-08 01:09:21 +0000 | [diff] [blame] | 128 | ArrayRef<OutputSegment *> Segments; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 129 | std::string DataSectionHeader; |
| 130 | size_t BodySize = 0; |
| 131 | }; |
| 132 | |
| 133 | } // namespace wasm |
| 134 | } // namespace lld |
| 135 | |
| 136 | #endif // LLD_WASM_OUTPUT_SECTIONS_H |