blob: 25330a8a0b2853e49d2de4978ab53b9f95626e9e [file] [log] [blame]
Rafael Espindola9d06ab62015-09-22 00:01:39 +00001//===- InputSection.h -------------------------------------------*- C++ -*-===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
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 Espindola9d06ab62015-09-22 00:01:39 +000010#ifndef LLD_ELF_INPUT_SECTION_H
11#define LLD_ELF_INPUT_SECTION_H
Michael J. Spencer84487f12015-07-24 21:03:07 +000012
13#include "lld/Core/LLVM.h"
14#include "llvm/Object/ELF.h"
15
16namespace lld {
17namespace elf2 {
18
Michael J. Spencer84487f12015-07-24 21:03:07 +000019template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000020template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000021template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
Rafael Espindola71675852015-09-22 00:16:19 +000023// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000024template <class ELFT> class InputSectionBase {
25protected:
Rafael Espindola7d4038dc2015-09-15 12:43:09 +000026 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola4ea00212015-09-21 22:01:00 +000027 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolac5c82912015-08-24 19:28:31 +000028 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000029 const Elf_Shdr *Header;
30
31 // The file this section is from.
32 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000033
34public:
Rafael Espindolac159c962015-10-19 21:00:02 +000035 enum Kind { Regular, Merge };
36 Kind SectionKind;
37
38 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
39 Kind SectionKind);
40 OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000041
Rafael Espindola71675852015-09-22 00:16:19 +000042 // Returns the size of this section (even if this is a common or BSS.)
Rafael Espindola83b0dc62015-08-13 22:21:37 +000043 size_t getSize() const { return Header->sh_size; }
44
Rafael Espindolac159c962015-10-19 21:00:02 +000045 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000046
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000047 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000048 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000049 ObjectFile<ELFT> *getFile() const { return File; }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000050
Rafael Espindola83b0dc62015-08-13 22:21:37 +000051 // The writer sets and uses the addresses.
Michael J. Spencerbaae5382015-09-05 00:25:33 +000052 uintX_t getAlign() {
53 // The ELF spec states that a value of 0 means the section has no alignment
54 // constraits.
55 return std::max<uintX_t>(Header->sh_addralign, 1);
56 }
Rafael Espindola83b0dc62015-08-13 22:21:37 +000057
Rafael Espindolac159c962015-10-19 21:00:02 +000058 uintX_t getOffset(const Elf_Sym &Sym) const;
59 ArrayRef<uint8_t> getSectionData() const;
60};
61
62template <class ELFT>
63InputSectionBase<ELFT>
64 InputSectionBase<ELFT>::Discarded(nullptr, nullptr,
65 InputSectionBase<ELFT>::Regular);
66
67// This corresponds to a SHF_MERGE section of an input file.
68template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
69 typedef InputSectionBase<ELFT> Base;
70 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
71 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
72 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
73
74public:
75 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
76 static bool classof(const InputSectionBase<ELFT> *S);
77 uintX_t getOffset(uintX_t Offset) const;
78};
79
80// This corresponds to a non SHF_MERGE section of an input file.
81template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
82 typedef InputSectionBase<ELFT> Base;
83 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
84 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
85 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
86 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
87 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
88
89public:
90 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
91
92 // Write this section to a mmap'ed file, assuming Buf is pointing to
93 // beginning of the output section.
94 void writeTo(uint8_t *Buf);
95
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000096 // Relocation sections that refer to this one.
97 SmallVector<const Elf_Shdr *, 1> RelocSections;
98
Rui Ueyamaedffd912015-10-14 21:00:23 +000099 // The offset from beginning of the output sections this section was assigned
100 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000101 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000102
Rafael Espindolac159c962015-10-19 21:00:02 +0000103 static bool classof(const InputSectionBase<ELFT> *S);
Rafael Espindola444576d2015-10-09 19:25:07 +0000104
Michael J. Spencer84487f12015-07-24 21:03:07 +0000105private:
Rafael Espindola4ea00212015-09-21 22:01:00 +0000106 template <bool isRela>
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000107 void relocate(uint8_t *Buf, uint8_t *BufEnd,
Rafael Espindola4ea00212015-09-21 22:01:00 +0000108 llvm::iterator_range<
109 const llvm::object::Elf_Rel_Impl<ELFT, isRela> *> Rels,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000110 const ObjectFile<ELFT> &File, uintX_t BaseAddr);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000111};
112
Michael J. Spencer84487f12015-07-24 21:03:07 +0000113} // namespace elf2
114} // namespace lld
115
116#endif