blob: f590f2449011d3c0084904754d1fd53ac205f983 [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
Michael J. Spencer84487f12015-07-24 21:03:07 +000024// A chunk corresponding 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
35 // Returns the size of this chunk (even if this is a common or BSS.)
36 size_t getSize() const { return Header->sh_size; }
37
38 // Write this chunk to a mmap'ed file, assuming Buf is pointing to
39 // beginning of the output section.
Rafael Espindola4ea00212015-09-21 22:01:00 +000040 void writeTo(uint8_t *Buf, const PltSection<ELFT> &PltSec,
41 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 void relocateOne(uint8_t *Buf, const Elf_Rela &Rel, uint32_t Type,
64 uintX_t BaseAddr, uintX_t SymVA);
65 void relocateOne(uint8_t *Buf, const Elf_Rel &Rel, uint32_t Type,
66 uintX_t BaseAddr, uintX_t SymVA);
67
68 template <bool isRela>
69 void relocate(uint8_t *Buf,
70 llvm::iterator_range<
71 const llvm::object::Elf_Rel_Impl<ELFT, isRela> *> Rels,
72 const ObjectFile<ELFT> &File, uintX_t BaseAddr,
73 const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec);
74
Rafael Espindola83b0dc62015-08-13 22:21:37 +000075 // The offset from beginning of the output sections this chunk was assigned
76 // to. The writer sets a value.
77 uint64_t OutputSectionOff = 0;
78
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000079 // The file this chunk was created from.
80 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000081
Rafael Espindola832b93f2015-08-24 20:06:32 +000082 OutputSection<ELFT> *Out = nullptr;
83
Michael J. Spencer84487f12015-07-24 21:03:07 +000084 const Elf_Shdr *Header;
Michael J. Spencer84487f12015-07-24 21:03:07 +000085};
86
87} // namespace elf2
88} // namespace lld
89
90#endif