blob: 12811a82b3006c5288a812cc3e352fa8af9030c8 [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;
Rafael Espindolaae81a7b2015-10-15 15:29:53 +000021template <bool Is64Bits> 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 Espindola53d5cea2015-09-21 17:47:00 +000024template <class ELFT> class InputSection {
Rafael Espindola7d4038dc2015-09-15 12:43:09 +000025 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
26 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
27 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Rafael Espindola4ea00212015-09-21 22:01:00 +000028 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolac5c82912015-08-24 19:28:31 +000029 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
31public:
Rafael Espindola53d5cea2015-09-21 17:47:00 +000032 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
Rafael Espindola83b0dc62015-08-13 22:21:37 +000033
Rafael Espindola71675852015-09-22 00:16:19 +000034 // Returns the size of this section (even if this is a common or BSS.)
Rafael Espindola83b0dc62015-08-13 22:21:37 +000035 size_t getSize() const { return Header->sh_size; }
36
Rafael Espindola71675852015-09-22 00:16:19 +000037 // Write this section to a mmap'ed file, assuming Buf is pointing to
Rafael Espindola83b0dc62015-08-13 22:21:37 +000038 // beginning of the output section.
Rui Ueyama15ef5e12015-10-07 19:18:16 +000039 void writeTo(uint8_t *Buf);
Rafael Espindola83b0dc62015-08-13 22:21:37 +000040
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000041 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000042 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000043 ObjectFile<ELFT> *getFile() const { return File; }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000044
Rafael Espindola83b0dc62015-08-13 22:21:37 +000045 // The writer sets and uses the addresses.
Michael J. Spencerbaae5382015-09-05 00:25:33 +000046 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 Espindola83b0dc62015-08-13 22:21:37 +000051
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000052 // Relocation sections that refer to this one.
53 SmallVector<const Elf_Shdr *, 1> RelocSections;
54
Rui Ueyamaedffd912015-10-14 21:00:23 +000055 // The offset from beginning of the output sections this section was assigned
56 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +000057 uint64_t OutSecOff = 0;
Rafael Espindolaae81a7b2015-10-15 15:29:53 +000058 OutputSectionBase<ELFT::Is64Bits> *OutSec = nullptr;
Rui Ueyamaedffd912015-10-14 21:00:23 +000059
Rafael Espindola444576d2015-10-09 19:25:07 +000060 static InputSection<ELFT> Discarded;
61
Michael J. Spencer84487f12015-07-24 21:03:07 +000062private:
Rafael Espindola4ea00212015-09-21 22:01:00 +000063 template <bool isRela>
Hal Finkel87bbd5f2015-10-12 21:19:18 +000064 void relocate(uint8_t *Buf, uint8_t *BufEnd,
Rafael Espindola4ea00212015-09-21 22:01:00 +000065 llvm::iterator_range<
66 const llvm::object::Elf_Rel_Impl<ELFT, isRela> *> Rels,
Rui Ueyama15ef5e12015-10-07 19:18:16 +000067 const ObjectFile<ELFT> &File, uintX_t BaseAddr);
Rafael Espindola4ea00212015-09-21 22:01:00 +000068
Rafael Espindola71675852015-09-22 00:16:19 +000069 // The file this section is from.
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000070 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000071
72 const Elf_Shdr *Header;
Michael J. Spencer84487f12015-07-24 21:03:07 +000073};
74
Rafael Espindola444576d2015-10-09 19:25:07 +000075template <class ELFT>
76InputSection<ELFT> InputSection<ELFT>::Discarded(nullptr, nullptr);
77
Michael J. Spencer84487f12015-07-24 21:03:07 +000078} // namespace elf2
79} // namespace lld
80
81#endif