Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 1 | //===- Chunks.h -----------------------------------------------------------===// |
| 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_ELF_CHUNKS_H |
| 11 | #define LLD_ELF_CHUNKS_H |
| 12 | |
| 13 | #include "lld/Core/LLVM.h" |
| 14 | #include "llvm/Object/ELF.h" |
| 15 | |
| 16 | namespace lld { |
| 17 | namespace elf2 { |
| 18 | |
| 19 | class Defined; |
| 20 | template <class ELFT> class ObjectFile; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 21 | |
| 22 | // A Chunk represents a chunk of data that will occupy space in the |
| 23 | // output (if the resolver chose that). It may or may not be backed by |
| 24 | // a section of an input file. It could be linker-created data, or |
| 25 | // doesn't even have actual data (if common or bss). |
| 26 | class Chunk { |
| 27 | public: |
| 28 | virtual ~Chunk() = default; |
| 29 | |
| 30 | // Returns the size of this chunk (even if this is a common or BSS.) |
| 31 | virtual size_t getSize() const = 0; |
| 32 | |
| 33 | // Write this chunk to a mmap'ed file, assuming Buf is pointing to |
| 34 | // beginning of the file. Because this function may use VA values |
| 35 | // of other chunks for relocations, you need to set them properly |
| 36 | // before calling this function. |
| 37 | virtual void writeTo(uint8_t *Buf) = 0; |
| 38 | |
| 39 | // The writer sets and uses the addresses. |
Rafael Espindola | 674b5d5 | 2015-08-13 15:54:36 +0000 | [diff] [blame^] | 40 | uint64_t getOutputSectionOff() { return OutputSectionOff; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 41 | uint32_t getAlign() { return Align; } |
Rafael Espindola | 674b5d5 | 2015-08-13 15:54:36 +0000 | [diff] [blame^] | 42 | void setOutputSectionOff(uint64_t V) { OutputSectionOff = V; } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 43 | |
| 44 | // Returns the section name if this is a section chunk. |
| 45 | // It is illegal to call this function on non-section chunks. |
| 46 | virtual StringRef getSectionName() const = 0; |
| 47 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 48 | protected: |
Rafael Espindola | 674b5d5 | 2015-08-13 15:54:36 +0000 | [diff] [blame^] | 49 | // The offset from beginning of the output sections this chunk was assigned |
| 50 | // to. The writer sets a value. |
| 51 | uint64_t OutputSectionOff = 0; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 52 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 53 | // The alignment of this chunk. The writer uses the value. |
| 54 | uint32_t Align = 1; |
| 55 | }; |
| 56 | |
| 57 | // A chunk corresponding a section of an input file. |
| 58 | template <class ELFT> class SectionChunk : public Chunk { |
| 59 | typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr; |
| 60 | typedef llvm::object::Elf_Rel_Impl<ELFT, true> Elf_Rela; |
| 61 | typedef llvm::object::Elf_Rel_Impl<ELFT, false> Elf_Rel; |
| 62 | |
| 63 | public: |
| 64 | SectionChunk(llvm::object::ELFFile<ELFT> *Obj, const Elf_Shdr *Header); |
| 65 | size_t getSize() const override { return Header->sh_size; } |
| 66 | void writeTo(uint8_t *Buf) override; |
| 67 | StringRef getSectionName() const override { return SectionName; } |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 68 | const Elf_Shdr *getSectionHdr() const { return Header; } |
| 69 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 70 | private: |
| 71 | // A file this chunk was created from. |
| 72 | llvm::object::ELFFile<ELFT> *Obj; |
| 73 | |
| 74 | const Elf_Shdr *Header; |
| 75 | StringRef SectionName; |
| 76 | }; |
| 77 | |
| 78 | } // namespace elf2 |
| 79 | } // namespace lld |
| 80 | |
| 81 | #endif |