Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 1 | //===- InputSection.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 | |
Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 10 | #ifndef LLD_ELF_INPUT_SECTION_H |
| 11 | #define LLD_ELF_INPUT_SECTION_H |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 22 | // This corresponds to a section of an input file. |
Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 23 | template <class ELFT> class InputSection { |
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 | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 27 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | c5c8291 | 2015-08-24 19:28:31 +0000 | [diff] [blame] | 28 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 29 | |
| 30 | public: |
Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 31 | InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header); |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 32 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 33 | // Returns the size of this section (even if this is a common or BSS.) |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 34 | size_t getSize() const { return Header->sh_size; } |
| 35 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 36 | // Write this section to a mmap'ed file, assuming Buf is pointing to |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 37 | // beginning of the output section. |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 38 | void writeTo(uint8_t *Buf); |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 39 | |
Rafael Espindola | 5d83ccd | 2015-08-13 19:18:30 +0000 | [diff] [blame] | 40 | StringRef getSectionName() const; |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 41 | const Elf_Shdr *getSectionHdr() const { return Header; } |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 42 | ObjectFile<ELFT> *getFile() const { return File; } |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 43 | |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 44 | // The writer sets and uses the addresses. |
Rafael Espindola | f383707 | 2015-08-25 15:53:17 +0000 | [diff] [blame] | 45 | uintX_t getOutputSectionOff() const { return OutputSectionOff; } |
Michael J. Spencer | baae538 | 2015-09-05 00:25:33 +0000 | [diff] [blame] | 46 | uintX_t getAlign() { |
| 47 | // The ELF spec states that a value of 0 means the section has no alignment |
| 48 | // constraits. |
| 49 | return std::max<uintX_t>(Header->sh_addralign, 1); |
| 50 | } |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 51 | void setOutputSectionOff(uint64_t V) { OutputSectionOff = V; } |
| 52 | |
Rui Ueyama | b490876 | 2015-10-07 17:04:18 +0000 | [diff] [blame] | 53 | void setOutputSection(OutputSection<ELFT> *O) { OutSec = O; } |
| 54 | OutputSection<ELFT> *getOutputSection() const { return OutSec; } |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 55 | |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 56 | // Relocation sections that refer to this one. |
| 57 | SmallVector<const Elf_Shdr *, 1> RelocSections; |
| 58 | |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 59 | static InputSection<ELFT> Discarded; |
| 60 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 61 | private: |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 62 | template <bool isRela> |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 63 | void relocate(uint8_t *Buf, uint8_t *BufEnd, |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 64 | llvm::iterator_range< |
| 65 | const llvm::object::Elf_Rel_Impl<ELFT, isRela> *> Rels, |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 66 | const ObjectFile<ELFT> &File, uintX_t BaseAddr); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 67 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 68 | // The offset from beginning of the output sections this section was assigned |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 69 | // to. The writer sets a value. |
| 70 | uint64_t OutputSectionOff = 0; |
| 71 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 72 | // The file this section is from. |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 73 | ObjectFile<ELFT> *File; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 74 | |
Rui Ueyama | b490876 | 2015-10-07 17:04:18 +0000 | [diff] [blame] | 75 | OutputSection<ELFT> *OutSec = nullptr; |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 76 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 77 | const Elf_Shdr *Header; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 80 | template <class ELFT> |
| 81 | InputSection<ELFT> InputSection<ELFT>::Discarded(nullptr, nullptr); |
| 82 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 83 | } // namespace elf2 |
| 84 | } // namespace lld |
| 85 | |
| 86 | #endif |