blob: 1a3777bbd721115df4332521bf4a1e5236697540 [file] [log] [blame]
Petr Hosek05a04cb2017-08-01 00:33:58 +00001//===- Object.h -------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
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 LLVM_OBJCOPY_OBJECT_H
11#define LLVM_OBJCOPY_OBJECT_H
12
13#include "llvm/MC/StringTableBuilder.h"
14#include "llvm/Object/ELFObjectFile.h"
15#include "llvm/Support/FileOutputBuffer.h"
16
17#include <memory>
18#include <set>
19
20class Segment;
21
22class SectionBase {
23public:
24 llvm::StringRef Name;
25 Segment *ParentSegment = nullptr;
26 uint64_t HeaderOffset;
27 uint64_t OriginalOffset;
28 uint32_t Index;
29
30 uint64_t Addr = 0;
31 uint64_t Align = 1;
32 uint32_t EntrySize = 0;
33 uint64_t Flags = 0;
34 uint64_t Info = 0;
35 uint64_t Link = llvm::ELF::SHN_UNDEF;
36 uint64_t NameIndex = 0;
37 uint64_t Offset = 0;
38 uint64_t Size = 0;
39 uint64_t Type = llvm::ELF::SHT_NULL;
40
41 virtual ~SectionBase() {}
42 virtual void finalize();
43 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
44 virtual void writeSection(llvm::FileOutputBuffer &Out) const = 0;
45};
46
47class Segment {
48private:
49 struct SectionCompare {
50 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
51 // Some sections might have the same address if one of them is empty. To
52 // fix this we can use the lexicographic ordering on ->Addr and the
53 // address of the actully stored section.
54 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
55 return Lhs < Rhs;
56 return Lhs->OriginalOffset < Rhs->OriginalOffset;
57 }
58 };
59
60 std::set<const SectionBase *, SectionCompare> Sections;
Petr Hosekc4df10e2017-08-04 21:09:26 +000061 llvm::ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +000062
63public:
64 uint64_t Align;
65 uint64_t FileSize;
66 uint32_t Flags;
67 uint32_t Index;
68 uint64_t MemSize;
69 uint64_t Offset;
70 uint64_t PAddr;
71 uint64_t Type;
72 uint64_t VAddr;
73
Petr Hosek3f383832017-08-26 01:32:20 +000074 uint64_t OriginalOffset;
75
Petr Hosekc4df10e2017-08-04 21:09:26 +000076 Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000077 void finalize();
78 const SectionBase *firstSection() const {
79 if (!Sections.empty())
80 return *Sections.begin();
81 return nullptr;
82 }
83 void addSection(const SectionBase *sec) { Sections.insert(sec); }
84 template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
Petr Hosekc4df10e2017-08-04 21:09:26 +000085 void writeSegment(llvm::FileOutputBuffer &Out) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +000086};
87
88class Section : public SectionBase {
89private:
90 llvm::ArrayRef<uint8_t> Contents;
91
92public:
93 Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
94 void writeSection(llvm::FileOutputBuffer &Out) const override;
95};
96
97// This is just a wraper around a StringTableBuilder that implements SectionBase
98class StringTableSection : public SectionBase {
99private:
100 llvm::StringTableBuilder StrTabBuilder;
101
102public:
103 StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) {
104 Type = llvm::ELF::SHT_STRTAB;
105 }
106
107 void addString(llvm::StringRef Name);
108 uint32_t findIndex(llvm::StringRef Name) const;
109 void finalize() override;
110 void writeSection(llvm::FileOutputBuffer &Out) const override;
111 static bool classof(const SectionBase *S) {
112 return S->Type == llvm::ELF::SHT_STRTAB;
113 }
114};
115
116template <class ELFT> class Object {
117private:
118 typedef std::unique_ptr<SectionBase> SecPtr;
119 typedef std::unique_ptr<Segment> SegPtr;
120
121 typedef typename ELFT::Shdr Elf_Shdr;
122 typedef typename ELFT::Ehdr Elf_Ehdr;
123 typedef typename ELFT::Phdr Elf_Phdr;
124
Petr Hosekb1bb3e52017-08-04 05:33:44 +0000125 SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
126 const Elf_Shdr &Shdr);
127 void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
128 void readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000129
130protected:
131 StringTableSection *SectionNames;
132 std::vector<SecPtr> Sections;
133 std::vector<SegPtr> Segments;
134
Petr Hosek05a04cb2017-08-01 00:33:58 +0000135 void writeHeader(llvm::FileOutputBuffer &Out) const;
136 void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
137 void writeSectionData(llvm::FileOutputBuffer &Out) const;
138 void writeSectionHeaders(llvm::FileOutputBuffer &Out) const;
139
140public:
141 uint8_t Ident[16];
142 uint64_t Entry;
143 uint64_t SHOffset;
144 uint32_t Type;
145 uint32_t Machine;
146 uint32_t Version;
147 uint32_t Flags;
148
149 Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000150 virtual size_t totalSize() const = 0;
151 virtual void finalize() = 0;
152 virtual void write(llvm::FileOutputBuffer &Out) const = 0;
153 virtual ~Object() = default;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000154};
155
Petr Hosekc4df10e2017-08-04 21:09:26 +0000156template <class ELFT> class ELFObject : public Object<ELFT> {
157private:
158 typedef std::unique_ptr<SectionBase> SecPtr;
159 typedef std::unique_ptr<Segment> SegPtr;
160
161 typedef typename ELFT::Shdr Elf_Shdr;
162 typedef typename ELFT::Ehdr Elf_Ehdr;
163 typedef typename ELFT::Phdr Elf_Phdr;
164
165 void sortSections();
166 void assignOffsets();
167
168public:
169 ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
170 void finalize() override;
171 size_t totalSize() const override;
172 void write(llvm::FileOutputBuffer &Out) const override;
173};
174
175template <class ELFT> class BinaryObject : public Object<ELFT> {
176private:
177 typedef std::unique_ptr<SectionBase> SecPtr;
178 typedef std::unique_ptr<Segment> SegPtr;
179
180 uint64_t TotalSize;
181
182public:
183 BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
184 : Object<ELFT>(Obj) {}
185 void finalize() override;
186 size_t totalSize() const override;
187 void write(llvm::FileOutputBuffer &Out) const override;
188};
Petr Hosek05a04cb2017-08-01 00:33:58 +0000189#endif