blob: 836a980f2166fd7902d97fe00326a66ec8a82304 [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- Chunks.h -----------------------------------------------------------===//
2//
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
19class Defined;
20template <class ELFT> class ObjectFile;
21class OutputSection;
22
23// A Chunk represents a chunk of data that will occupy space in the
24// output (if the resolver chose that). It may or may not be backed by
25// a section of an input file. It could be linker-created data, or
26// doesn't even have actual data (if common or bss).
27class Chunk {
28public:
Michael J. Spencer8039dae22015-07-29 00:30:10 +000029 enum Kind { SectionKind, OtherKind };
30 Kind kind() const { return ChunkKind; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000031 virtual ~Chunk() = default;
32
33 // Returns the size of this chunk (even if this is a common or BSS.)
34 virtual size_t getSize() const = 0;
35
36 // Write this chunk to a mmap'ed file, assuming Buf is pointing to
37 // beginning of the file. Because this function may use VA values
38 // of other chunks for relocations, you need to set them properly
39 // before calling this function.
40 virtual void writeTo(uint8_t *Buf) = 0;
41
42 // The writer sets and uses the addresses.
43 uint64_t getVA() { return VA; }
44 uint64_t getFileOff() { return FileOff; }
45 uint32_t getAlign() { return Align; }
46 void setVA(uint64_t V) { VA = V; }
47 void setFileOff(uint64_t V) { FileOff = V; }
48
49 // Returns the section name if this is a section chunk.
50 // It is illegal to call this function on non-section chunks.
51 virtual StringRef getSectionName() const = 0;
52
53 // An output section has pointers to chunks in the section, and each
54 // chunk has a back pointer to an output section.
55 void setOutputSection(OutputSection *O) { Out = O; }
56 OutputSection *getOutputSection() { return Out; }
57
58protected:
Michael J. Spencer8039dae22015-07-29 00:30:10 +000059 Chunk(Kind K = OtherKind) : ChunkKind(K) {}
60 const Kind ChunkKind;
61
Michael J. Spencer84487f12015-07-24 21:03:07 +000062 // The VA of this chunk in the output. The writer sets a value.
63 uint64_t VA = 0;
64
65 // The offset from beginning of the output file. The writer sets a value.
66 uint64_t FileOff = 0;
67
68 // The output section for this chunk.
69 OutputSection *Out = nullptr;
70
71 // The alignment of this chunk. The writer uses the value.
72 uint32_t Align = 1;
73};
74
75// A chunk corresponding a section of an input file.
76template <class ELFT> class SectionChunk : public Chunk {
77 typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr;
78 typedef llvm::object::Elf_Rel_Impl<ELFT, true> Elf_Rela;
79 typedef llvm::object::Elf_Rel_Impl<ELFT, false> Elf_Rel;
80
81public:
82 SectionChunk(llvm::object::ELFFile<ELFT> *Obj, const Elf_Shdr *Header);
83 size_t getSize() const override { return Header->sh_size; }
84 void writeTo(uint8_t *Buf) override;
85 StringRef getSectionName() const override { return SectionName; }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000086 const Elf_Shdr *getSectionHdr() const { return Header; }
87
88 static bool classof(const Chunk *C) {
89 return C->kind() == SectionKind;
90 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000091
92private:
93 // A file this chunk was created from.
94 llvm::object::ELFFile<ELFT> *Obj;
95
96 const Elf_Shdr *Header;
97 StringRef SectionName;
98};
99
100} // namespace elf2
101} // namespace lld
102
103#endif