blob: 53e0f9f9678252670b4e9ea2b027f39f4a7dad8a [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;
Michael J. Spencer84487f12015-07-24 21:03:07 +000021
Rafael Espindola71675852015-09-22 00:16:19 +000022// This corresponds to a section of an input file.
Rafael Espindola53d5cea2015-09-21 17:47:00 +000023template <class ELFT> class InputSection {
Rafael Espindola7d4038dc2015-09-15 12:43:09 +000024 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 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;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
30public:
Rafael Espindola53d5cea2015-09-21 17:47:00 +000031 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
Rafael Espindola83b0dc62015-08-13 22:21:37 +000032
Rafael Espindola71675852015-09-22 00:16:19 +000033 // Returns the size of this section (even if this is a common or BSS.)
Rafael Espindola83b0dc62015-08-13 22:21:37 +000034 size_t getSize() const { return Header->sh_size; }
35
Rafael Espindola71675852015-09-22 00:16:19 +000036 // Write this section to a mmap'ed file, assuming Buf is pointing to
Rafael Espindola83b0dc62015-08-13 22:21:37 +000037 // beginning of the output section.
Rui Ueyama15ef5e12015-10-07 19:18:16 +000038 void writeTo(uint8_t *Buf);
Rafael Espindola83b0dc62015-08-13 22:21:37 +000039
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000040 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000041 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000042 ObjectFile<ELFT> *getFile() const { return File; }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000043
Rafael Espindola83b0dc62015-08-13 22:21:37 +000044 // The writer sets and uses the addresses.
Rafael Espindolaf3837072015-08-25 15:53:17 +000045 uintX_t getOutputSectionOff() const { return OutputSectionOff; }
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 void setOutputSectionOff(uint64_t V) { OutputSectionOff = V; }
52
Rui Ueyamab4908762015-10-07 17:04:18 +000053 void setOutputSection(OutputSection<ELFT> *O) { OutSec = O; }
54 OutputSection<ELFT> *getOutputSection() const { return OutSec; }
Rafael Espindola832b93f2015-08-24 20:06:32 +000055
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000056 // Relocation sections that refer to this one.
57 SmallVector<const Elf_Shdr *, 1> RelocSections;
58
Michael J. Spencer84487f12015-07-24 21:03:07 +000059private:
Rafael Espindola4ea00212015-09-21 22:01:00 +000060 template <bool isRela>
61 void relocate(uint8_t *Buf,
62 llvm::iterator_range<
63 const llvm::object::Elf_Rel_Impl<ELFT, isRela> *> Rels,
Rui Ueyama15ef5e12015-10-07 19:18:16 +000064 const ObjectFile<ELFT> &File, uintX_t BaseAddr);
Rafael Espindola4ea00212015-09-21 22:01:00 +000065
Rafael Espindola71675852015-09-22 00:16:19 +000066 // The offset from beginning of the output sections this section was assigned
Rafael Espindola83b0dc62015-08-13 22:21:37 +000067 // to. The writer sets a value.
68 uint64_t OutputSectionOff = 0;
69
Rafael Espindola71675852015-09-22 00:16:19 +000070 // The file this section is from.
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000071 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000072
Rui Ueyamab4908762015-10-07 17:04:18 +000073 OutputSection<ELFT> *OutSec = nullptr;
Rafael Espindola832b93f2015-08-24 20:06:32 +000074
Michael J. Spencer84487f12015-07-24 21:03:07 +000075 const Elf_Shdr *Header;
Michael J. Spencer84487f12015-07-24 21:03:07 +000076};
77
78} // namespace elf2
79} // namespace lld
80
81#endif