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; |
| 21 | class OutputSection; |
| 22 | |
| 23 | // A Chunk represents a chunk of data that will occupy space in the |
| 24 | // output (if the resolver chose that). It may or may not be backed by |
| 25 | // a section of an input file. It could be linker-created data, or |
| 26 | // doesn't even have actual data (if common or bss). |
| 27 | class Chunk { |
| 28 | public: |
| 29 | virtual ~Chunk() = default; |
| 30 | |
| 31 | // Returns the size of this chunk (even if this is a common or BSS.) |
| 32 | virtual size_t getSize() const = 0; |
| 33 | |
| 34 | // Write this chunk to a mmap'ed file, assuming Buf is pointing to |
| 35 | // beginning of the file. Because this function may use VA values |
| 36 | // of other chunks for relocations, you need to set them properly |
| 37 | // before calling this function. |
| 38 | virtual void writeTo(uint8_t *Buf) = 0; |
| 39 | |
| 40 | // The writer sets and uses the addresses. |
| 41 | uint64_t getVA() { return VA; } |
| 42 | uint64_t getFileOff() { return FileOff; } |
| 43 | uint32_t getAlign() { return Align; } |
| 44 | void setVA(uint64_t V) { VA = V; } |
| 45 | void setFileOff(uint64_t V) { FileOff = V; } |
| 46 | |
| 47 | // Returns the section name if this is a section chunk. |
| 48 | // It is illegal to call this function on non-section chunks. |
| 49 | virtual StringRef getSectionName() const = 0; |
| 50 | |
| 51 | // An output section has pointers to chunks in the section, and each |
| 52 | // chunk has a back pointer to an output section. |
| 53 | void setOutputSection(OutputSection *O) { Out = O; } |
| 54 | OutputSection *getOutputSection() { return Out; } |
| 55 | |
| 56 | protected: |
| 57 | // The VA of this chunk in the output. The writer sets a value. |
| 58 | uint64_t VA = 0; |
| 59 | |
| 60 | // The offset from beginning of the output file. The writer sets a value. |
| 61 | uint64_t FileOff = 0; |
| 62 | |
| 63 | // The output section for this chunk. |
| 64 | OutputSection *Out = nullptr; |
| 65 | |
| 66 | // The alignment of this chunk. The writer uses the value. |
| 67 | uint32_t Align = 1; |
| 68 | }; |
| 69 | |
| 70 | // A chunk corresponding a section of an input file. |
| 71 | template <class ELFT> class SectionChunk : public Chunk { |
| 72 | typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr; |
| 73 | typedef llvm::object::Elf_Rel_Impl<ELFT, true> Elf_Rela; |
| 74 | typedef llvm::object::Elf_Rel_Impl<ELFT, false> Elf_Rel; |
| 75 | |
| 76 | public: |
| 77 | SectionChunk(llvm::object::ELFFile<ELFT> *Obj, const Elf_Shdr *Header); |
| 78 | size_t getSize() const override { return Header->sh_size; } |
| 79 | void writeTo(uint8_t *Buf) override; |
| 80 | StringRef getSectionName() const override { return SectionName; } |
| 81 | |
| 82 | private: |
| 83 | // A file this chunk was created from. |
| 84 | llvm::object::ELFFile<ELFT> *Obj; |
| 85 | |
| 86 | const Elf_Shdr *Header; |
| 87 | StringRef SectionName; |
| 88 | }; |
| 89 | |
| 90 | } // namespace elf2 |
| 91 | } // namespace lld |
| 92 | |
| 93 | #endif |