blob: f0de99ec08486653a90627802ee290a2e40cd9df [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 Espindola4ea00212015-09-21 22:01:00 +000021template <class ELFT> class PltSection;
22template <class ELFT> class GotSection;
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
Rafael Espindola71675852015-09-22 00:16:19 +000024// This corresponds to a section of an input file.
Rafael Espindola53d5cea2015-09-21 17:47:00 +000025template <class ELFT> class InputSection {
Rafael Espindola7d4038dc2015-09-15 12:43:09 +000026 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
27 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
28 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Rafael Espindola4ea00212015-09-21 22:01:00 +000029 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolac5c82912015-08-24 19:28:31 +000030 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031
32public:
Rafael Espindola53d5cea2015-09-21 17:47:00 +000033 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
Rafael Espindola83b0dc62015-08-13 22:21:37 +000034
Rafael Espindola71675852015-09-22 00:16:19 +000035 // Returns the size of this section (even if this is a common or BSS.)
Rafael Espindola83b0dc62015-08-13 22:21:37 +000036 size_t getSize() const { return Header->sh_size; }
37
Rafael Espindola71675852015-09-22 00:16:19 +000038 // Write this section to a mmap'ed file, assuming Buf is pointing to
Rafael Espindola83b0dc62015-08-13 22:21:37 +000039 // beginning of the output section.
Michael J. Spencer2812aa82015-09-23 16:57:31 +000040 void writeTo(uint8_t *Buf, const OutputSection<ELFT> &BssSec,
41 const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec);
Rafael Espindola83b0dc62015-08-13 22:21:37 +000042
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000043 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000044 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindola19e38892015-09-16 15:54:15 +000045 const ObjectFile<ELFT> *getFile() const { return File; }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000046
Rafael Espindola83b0dc62015-08-13 22:21:37 +000047 // The writer sets and uses the addresses.
Rafael Espindolaf3837072015-08-25 15:53:17 +000048 uintX_t getOutputSectionOff() const { return OutputSectionOff; }
Michael J. Spencerbaae5382015-09-05 00:25:33 +000049 uintX_t getAlign() {
50 // The ELF spec states that a value of 0 means the section has no alignment
51 // constraits.
52 return std::max<uintX_t>(Header->sh_addralign, 1);
53 }
Rafael Espindola83b0dc62015-08-13 22:21:37 +000054 void setOutputSectionOff(uint64_t V) { OutputSectionOff = V; }
55
Rafael Espindola832b93f2015-08-24 20:06:32 +000056 void setOutputSection(OutputSection<ELFT> *O) { Out = O; }
57 OutputSection<ELFT> *getOutputSection() const { return Out; }
58
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000059 // Relocation sections that refer to this one.
60 SmallVector<const Elf_Shdr *, 1> RelocSections;
61
Michael J. Spencer84487f12015-07-24 21:03:07 +000062private:
Rafael Espindola4ea00212015-09-21 22:01:00 +000063 template <bool isRela>
64 void relocate(uint8_t *Buf,
65 llvm::iterator_range<
66 const llvm::object::Elf_Rel_Impl<ELFT, isRela> *> Rels,
67 const ObjectFile<ELFT> &File, uintX_t BaseAddr,
Michael J. Spencer2812aa82015-09-23 16:57:31 +000068 const OutputSection<ELFT> &BssSec,
Rafael Espindola4ea00212015-09-21 22:01:00 +000069 const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec);
70
Rafael Espindola71675852015-09-22 00:16:19 +000071 // The offset from beginning of the output sections this section was assigned
Rafael Espindola83b0dc62015-08-13 22:21:37 +000072 // to. The writer sets a value.
73 uint64_t OutputSectionOff = 0;
74
Rafael Espindola71675852015-09-22 00:16:19 +000075 // The file this section is from.
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000076 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000077
Rafael Espindola832b93f2015-08-24 20:06:32 +000078 OutputSection<ELFT> *Out = nullptr;
79
Michael J. Spencer84487f12015-07-24 21:03:07 +000080 const Elf_Shdr *Header;
Michael J. Spencer84487f12015-07-24 21:03:07 +000081};
82
83} // namespace elf2
84} // namespace lld
85
86#endif