blob: 1462724dafbc8e00cd833410ff5223f901f72bb0 [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;
Michael J. Spencer84487f12015-07-24 21:03:07 +000021
22// A Chunk represents a chunk of data that will occupy space in the
23// output (if the resolver chose that). It may or may not be backed by
24// a section of an input file. It could be linker-created data, or
25// doesn't even have actual data (if common or bss).
26class Chunk {
27public:
28 virtual ~Chunk() = default;
29
30 // Returns the size of this chunk (even if this is a common or BSS.)
31 virtual size_t getSize() const = 0;
32
33 // Write this chunk to a mmap'ed file, assuming Buf is pointing to
34 // beginning of the file. Because this function may use VA values
35 // of other chunks for relocations, you need to set them properly
36 // before calling this function.
37 virtual void writeTo(uint8_t *Buf) = 0;
38
39 // The writer sets and uses the addresses.
Michael J. Spencer84487f12015-07-24 21:03:07 +000040 uint64_t getFileOff() { return FileOff; }
41 uint32_t getAlign() { return Align; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000042 void setFileOff(uint64_t V) { FileOff = V; }
43
44 // Returns the section name if this is a section chunk.
45 // It is illegal to call this function on non-section chunks.
46 virtual StringRef getSectionName() const = 0;
47
Michael J. Spencer84487f12015-07-24 21:03:07 +000048protected:
Michael J. Spencer84487f12015-07-24 21:03:07 +000049 // The offset from beginning of the output file. The writer sets a value.
50 uint64_t FileOff = 0;
51
Michael J. Spencer84487f12015-07-24 21:03:07 +000052 // The alignment of this chunk. The writer uses the value.
53 uint32_t Align = 1;
54};
55
56// A chunk corresponding a section of an input file.
57template <class ELFT> class SectionChunk : public Chunk {
58 typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr;
59 typedef llvm::object::Elf_Rel_Impl<ELFT, true> Elf_Rela;
60 typedef llvm::object::Elf_Rel_Impl<ELFT, false> Elf_Rel;
61
62public:
63 SectionChunk(llvm::object::ELFFile<ELFT> *Obj, const Elf_Shdr *Header);
64 size_t getSize() const override { return Header->sh_size; }
65 void writeTo(uint8_t *Buf) override;
66 StringRef getSectionName() const override { return SectionName; }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000067 const Elf_Shdr *getSectionHdr() const { return Header; }
68
Michael J. Spencer84487f12015-07-24 21:03:07 +000069private:
70 // A file this chunk was created from.
71 llvm::object::ELFFile<ELFT> *Obj;
72
73 const Elf_Shdr *Header;
74 StringRef SectionName;
75};
76
77} // namespace elf2
78} // namespace lld
79
80#endif