blob: f38217d89e4b2b857f768481370a80ad0fb6d53a [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
Michael J. Spencer84487f12015-07-24 21:03:07 +000044protected:
Rafael Espindola674b5d52015-08-13 15:54:36 +000045 // The offset from beginning of the output sections this chunk was assigned
46 // to. The writer sets a value.
47 uint64_t OutputSectionOff = 0;
Michael J. Spencer84487f12015-07-24 21:03:07 +000048
Michael J. Spencer84487f12015-07-24 21:03:07 +000049 // The alignment of this chunk. The writer uses the value.
50 uint32_t Align = 1;
51};
52
53// A chunk corresponding a section of an input file.
54template <class ELFT> class SectionChunk : public Chunk {
55 typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr;
56 typedef llvm::object::Elf_Rel_Impl<ELFT, true> Elf_Rela;
57 typedef llvm::object::Elf_Rel_Impl<ELFT, false> Elf_Rel;
58
59public:
60 SectionChunk(llvm::object::ELFFile<ELFT> *Obj, const Elf_Shdr *Header);
61 size_t getSize() const override { return Header->sh_size; }
62 void writeTo(uint8_t *Buf) override;
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000063 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000064 const Elf_Shdr *getSectionHdr() const { return Header; }
65
Michael J. Spencer84487f12015-07-24 21:03:07 +000066private:
67 // A file this chunk was created from.
68 llvm::object::ELFFile<ELFT> *Obj;
69
70 const Elf_Shdr *Header;
Michael J. Spencer84487f12015-07-24 21:03:07 +000071};
72
73} // namespace elf2
74} // namespace lld
75
76#endif