Rafael Espindola | beee25e | 2015-08-14 14:12:54 +0000 | [diff] [blame] | 1 | //===- Chunks.h -------------------------------------------------*- C++ -*-===// |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 19 | template <class ELFT> class ObjectFile; |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 20 | template <class ELFT> class OutputSection; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 21 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 22 | // A chunk corresponding a section of an input file. |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 23 | template <class ELFT> class SectionChunk { |
Rafael Espindola | 7d4038dc | 2015-09-15 12:43:09 +0000 | [diff] [blame] | 24 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 25 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
| 26 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
Rafael Espindola | c5c8291 | 2015-08-24 19:28:31 +0000 | [diff] [blame] | 27 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 28 | |
| 29 | public: |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 30 | SectionChunk(ObjectFile<ELFT> *F, const Elf_Shdr *Header); |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 31 | |
| 32 | // Returns the size of this chunk (even if this is a common or BSS.) |
| 33 | size_t getSize() const { return Header->sh_size; } |
| 34 | |
| 35 | // Write this chunk to a mmap'ed file, assuming Buf is pointing to |
| 36 | // beginning of the output section. |
| 37 | void writeTo(uint8_t *Buf); |
| 38 | |
Rafael Espindola | 5d83ccd | 2015-08-13 19:18:30 +0000 | [diff] [blame] | 39 | StringRef getSectionName() const; |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 40 | const Elf_Shdr *getSectionHdr() const { return Header; } |
Rafael Espindola | 19e3889 | 2015-09-16 15:54:15 +0000 | [diff] [blame^] | 41 | const ObjectFile<ELFT> *getFile() const { return File; } |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 42 | |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 43 | // The writer sets and uses the addresses. |
Rafael Espindola | f383707 | 2015-08-25 15:53:17 +0000 | [diff] [blame] | 44 | uintX_t getOutputSectionOff() const { return OutputSectionOff; } |
Michael J. Spencer | baae538 | 2015-09-05 00:25:33 +0000 | [diff] [blame] | 45 | uintX_t getAlign() { |
| 46 | // The ELF spec states that a value of 0 means the section has no alignment |
| 47 | // constraits. |
| 48 | return std::max<uintX_t>(Header->sh_addralign, 1); |
| 49 | } |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 50 | void setOutputSectionOff(uint64_t V) { OutputSectionOff = V; } |
| 51 | |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 52 | void setOutputSection(OutputSection<ELFT> *O) { Out = O; } |
| 53 | OutputSection<ELFT> *getOutputSection() const { return Out; } |
| 54 | |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 55 | // Relocation sections that refer to this one. |
| 56 | SmallVector<const Elf_Shdr *, 1> RelocSections; |
| 57 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 58 | private: |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 59 | // The offset from beginning of the output sections this chunk was assigned |
| 60 | // to. The writer sets a value. |
| 61 | uint64_t OutputSectionOff = 0; |
| 62 | |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 63 | // The file this chunk was created from. |
| 64 | ObjectFile<ELFT> *File; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 65 | |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 66 | OutputSection<ELFT> *Out = nullptr; |
| 67 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 68 | const Elf_Shdr *Header; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace elf2 |
| 72 | } // namespace lld |
| 73 | |
| 74 | #endif |