blob: 3b8126e689c1d70539900c13bac091884f958309 [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:54 +00001//===- Chunks.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
10#ifndef LLD_ELF_CHUNKS_H
11#define LLD_ELF_CHUNKS_H
12
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
Michael J. Spencer84487f12015-07-24 21:03:07 +000022// A chunk corresponding a section of an input file.
Rafael Espindola83b0dc62015-08-13 22:21:37 +000023template <class ELFT> class SectionChunk {
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 Espindolac5c82912015-08-24 19:28:31 +000027 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028
29public:
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000030 SectionChunk(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
Rafael Espindola83b0dc62015-08-13 22:21:37 +000031
32 // Returns the size of this chunk (even if this is a common or BSS.)
33 size_t getSize() const { return Header->sh_size; }
34
35 // Write this chunk to a mmap'ed file, assuming Buf is pointing to
36 // beginning of the output section.
37 void writeTo(uint8_t *Buf);
38
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000039 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000040 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindola19e38892015-09-16 15:54:15 +000041 const ObjectFile<ELFT> *getFile() const { return File; }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000042
Rafael Espindola83b0dc62015-08-13 22:21:37 +000043 // The writer sets and uses the addresses.
Rafael Espindolaf3837072015-08-25 15:53:17 +000044 uintX_t getOutputSectionOff() const { return OutputSectionOff; }
Michael J. Spencerbaae5382015-09-05 00:25:33 +000045 uintX_t getAlign() {
46 // The ELF spec states that a value of 0 means the section has no alignment
47 // constraits.
48 return std::max<uintX_t>(Header->sh_addralign, 1);
49 }
Rafael Espindola83b0dc62015-08-13 22:21:37 +000050 void setOutputSectionOff(uint64_t V) { OutputSectionOff = V; }
51
Rafael Espindola832b93f2015-08-24 20:06:32 +000052 void setOutputSection(OutputSection<ELFT> *O) { Out = O; }
53 OutputSection<ELFT> *getOutputSection() const { return Out; }
54
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000055 // Relocation sections that refer to this one.
56 SmallVector<const Elf_Shdr *, 1> RelocSections;
57
Michael J. Spencer84487f12015-07-24 21:03:07 +000058private:
Rafael Espindola83b0dc62015-08-13 22:21:37 +000059 // The offset from beginning of the output sections this chunk was assigned
60 // to. The writer sets a value.
61 uint64_t OutputSectionOff = 0;
62
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000063 // The file this chunk was created from.
64 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000065
Rafael Espindola832b93f2015-08-24 20:06:32 +000066 OutputSection<ELFT> *Out = nullptr;
67
Michael J. Spencer84487f12015-07-24 21:03:07 +000068 const Elf_Shdr *Header;
Michael J. Spencer84487f12015-07-24 21:03:07 +000069};
70
71} // namespace elf2
72} // namespace lld
73
74#endif