blob: f953cfe508fb2773650265adf7e4911606a71218 [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.
Rafael Espindola674b5d52015-08-13 15:54:36 +000040 uint64_t getOutputSectionOff() { return OutputSectionOff; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000041 uint32_t getAlign() { return Align; }
Rafael Espindola674b5d52015-08-13 15:54:36 +000042 void setOutputSectionOff(uint64_t V) { OutputSectionOff = V; }
Michael J. Spencer84487f12015-07-24 21:03:07 +000043
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:
Rafael Espindola674b5d52015-08-13 15:54:36 +000049 // The offset from beginning of the output sections this chunk was assigned
50 // to. The writer sets a value.
51 uint64_t OutputSectionOff = 0;
Michael J. Spencer84487f12015-07-24 21:03:07 +000052
Michael J. Spencer84487f12015-07-24 21:03:07 +000053 // The alignment of this chunk. The writer uses the value.
54 uint32_t Align = 1;
55};
56
57// A chunk corresponding a section of an input file.
58template <class ELFT> class SectionChunk : public Chunk {
59 typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr;
60 typedef llvm::object::Elf_Rel_Impl<ELFT, true> Elf_Rela;
61 typedef llvm::object::Elf_Rel_Impl<ELFT, false> Elf_Rel;
62
63public:
64 SectionChunk(llvm::object::ELFFile<ELFT> *Obj, const Elf_Shdr *Header);
65 size_t getSize() const override { return Header->sh_size; }
66 void writeTo(uint8_t *Buf) override;
67 StringRef getSectionName() const override { return SectionName; }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000068 const Elf_Shdr *getSectionHdr() const { return Header; }
69
Michael J. Spencer84487f12015-07-24 21:03:07 +000070private:
71 // A file this chunk was created from.
72 llvm::object::ELFFile<ELFT> *Obj;
73
74 const Elf_Shdr *Header;
75 StringRef SectionName;
76};
77
78} // namespace elf2
79} // namespace lld
80
81#endif