blob: 2598b249a7cfd5a354a63e620ec1fb6659ca0112 [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
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000010#ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
11#define LLVM_TOOLS_OBJCOPY_OBJECT_H
Petr Hosek05a04cb2017-08-01 00:33:58 +000012
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000013#include "llvm/ADT/ArrayRef.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/BinaryFormat/ELF.h"
Petr Hosek05a04cb2017-08-01 00:33:58 +000017#include "llvm/MC/StringTableBuilder.h"
18#include "llvm/Object/ELFObjectFile.h"
Jake Ehrlich76e91102018-01-25 22:46:17 +000019#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000020#include "llvm/Support/JamCRC.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000021#include <cstddef>
22#include <cstdint>
23#include <functional>
Petr Hosek05a04cb2017-08-01 00:33:58 +000024#include <memory>
25#include <set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000026#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000027
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000028namespace llvm {
29
Jake Ehrlichf5a43772017-09-25 20:37:28 +000030class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000031class Section;
32class OwnedDataSection;
33class StringTableSection;
34class SymbolTableSection;
35class RelocationSection;
36class DynamicRelocationSection;
37class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000038class GroupSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000039class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000040class Object;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000041
42class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000043 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000044
45public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000046 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
47
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000048 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000049 : Sections(Secs) {}
50 SectionTableRef(const SectionTableRef &) = default;
51
Jake Ehrlich76e91102018-01-25 22:46:17 +000052 iterator begin() { return iterator(Sections.data()); }
53 iterator end() { return iterator(Sections.data() + Sections.size()); }
54
Jake Ehrlich8b831c12018-03-07 20:33:02 +000055 SectionBase *getSection(uint16_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000056
57 template <class T>
Jake Ehrlich8b831c12018-03-07 20:33:02 +000058 T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000059};
Petr Hosek05a04cb2017-08-01 00:33:58 +000060
Jake Ehrlich76e91102018-01-25 22:46:17 +000061enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
62
63class SectionVisitor {
64public:
65 virtual ~SectionVisitor();
66
67 virtual void visit(const Section &Sec) = 0;
68 virtual void visit(const OwnedDataSection &Sec) = 0;
69 virtual void visit(const StringTableSection &Sec) = 0;
70 virtual void visit(const SymbolTableSection &Sec) = 0;
71 virtual void visit(const RelocationSection &Sec) = 0;
72 virtual void visit(const DynamicRelocationSection &Sec) = 0;
73 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000074 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000075};
76
77class SectionWriter : public SectionVisitor {
78protected:
79 FileOutputBuffer &Out;
80
81public:
82 virtual ~SectionWriter(){};
83
84 void visit(const Section &Sec) override;
85 void visit(const OwnedDataSection &Sec) override;
86 void visit(const StringTableSection &Sec) override;
87 void visit(const DynamicRelocationSection &Sec) override;
88 virtual void visit(const SymbolTableSection &Sec) override = 0;
89 virtual void visit(const RelocationSection &Sec) override = 0;
90 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000091 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000092
93 SectionWriter(FileOutputBuffer &Buf) : Out(Buf) {}
94};
95
96template <class ELFT> class ELFSectionWriter : public SectionWriter {
97private:
98 using Elf_Word = typename ELFT::Word;
99 using Elf_Rel = typename ELFT::Rel;
100 using Elf_Rela = typename ELFT::Rela;
101
102public:
103 virtual ~ELFSectionWriter() {}
104 void visit(const SymbolTableSection &Sec) override;
105 void visit(const RelocationSection &Sec) override;
106 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000107 void visit(const GroupSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000108
109 ELFSectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
110};
111
112#define MAKE_SEC_WRITER_FRIEND \
113 friend class SectionWriter; \
114 template <class ELFT> friend class ELFSectionWriter;
115
116class BinarySectionWriter : public SectionWriter {
117public:
118 virtual ~BinarySectionWriter() {}
119
120 void visit(const SymbolTableSection &Sec) override;
121 void visit(const RelocationSection &Sec) override;
122 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000123 void visit(const GroupSection &Sec) override;
124
Jake Ehrlich76e91102018-01-25 22:46:17 +0000125 BinarySectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
126};
127
128class Writer {
129protected:
130 StringRef File;
131 Object &Obj;
132 std::unique_ptr<FileOutputBuffer> BufPtr;
133
134 void createBuffer(uint64_t Size);
135
136public:
137 virtual ~Writer();
138
139 virtual void finalize() = 0;
140 virtual void write() = 0;
141
142 Writer(StringRef File, Object &Obj) : File(File), Obj(Obj) {}
143};
144
145template <class ELFT> class ELFWriter : public Writer {
146private:
147 using Elf_Shdr = typename ELFT::Shdr;
148 using Elf_Phdr = typename ELFT::Phdr;
149 using Elf_Ehdr = typename ELFT::Ehdr;
150
151 void writeEhdr();
152 void writePhdr(const Segment &Seg);
153 void writeShdr(const SectionBase &Sec);
154
155 void writePhdrs();
156 void writeShdrs();
157 void writeSectionData();
158
159 void assignOffsets();
160
161 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
162
163 size_t totalSize() const;
164
165public:
166 virtual ~ELFWriter() {}
167 bool WriteSectionHeaders = true;
168
169 void finalize() override;
170 void write() override;
171 ELFWriter(StringRef File, Object &Obj, bool WSH)
172 : Writer(File, Obj), WriteSectionHeaders(WSH) {}
173};
174
175class BinaryWriter : public Writer {
176private:
177 std::unique_ptr<BinarySectionWriter> SecWriter;
178
179 uint64_t TotalSize;
180
181public:
182 ~BinaryWriter() {}
183 void finalize() override;
184 void write() override;
185 BinaryWriter(StringRef File, Object &Obj) : Writer(File, Obj) {}
186};
187
Petr Hosek05a04cb2017-08-01 00:33:58 +0000188class SectionBase {
189public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000190 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000191 Segment *ParentSegment = nullptr;
192 uint64_t HeaderOffset;
193 uint64_t OriginalOffset;
194 uint32_t Index;
195
196 uint64_t Addr = 0;
197 uint64_t Align = 1;
198 uint32_t EntrySize = 0;
199 uint64_t Flags = 0;
200 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000201 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000202 uint64_t NameIndex = 0;
203 uint64_t Offset = 0;
204 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000205 uint64_t Type = ELF::SHT_NULL;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000206
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000207 virtual ~SectionBase() = default;
208
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000209 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000210 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000211 virtual void removeSectionReferences(const SectionBase *Sec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000212 virtual void accept(SectionVisitor &Visitor) const = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000213};
214
215class Segment {
216private:
217 struct SectionCompare {
218 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
219 // Some sections might have the same address if one of them is empty. To
220 // fix this we can use the lexicographic ordering on ->Addr and the
221 // address of the actully stored section.
222 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
223 return Lhs < Rhs;
224 return Lhs->OriginalOffset < Rhs->OriginalOffset;
225 }
226 };
227
228 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000229 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000230
231public:
232 uint64_t Align;
233 uint64_t FileSize;
234 uint32_t Flags;
235 uint32_t Index;
236 uint64_t MemSize;
237 uint64_t Offset;
238 uint64_t PAddr;
239 uint64_t Type;
240 uint64_t VAddr;
241
Petr Hosek3f383832017-08-26 01:32:20 +0000242 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000243 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000244
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000245 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000246 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000247
Petr Hosek05a04cb2017-08-01 00:33:58 +0000248 const SectionBase *firstSection() const {
249 if (!Sections.empty())
250 return *Sections.begin();
251 return nullptr;
252 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000253
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000254 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
255 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000256};
257
258class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000259 MAKE_SEC_WRITER_FRIEND
260
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000261 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000262 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000263
264public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000265 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000266
Jake Ehrlich76e91102018-01-25 22:46:17 +0000267 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000268 void removeSectionReferences(const SectionBase *Sec) override;
269 void initialize(SectionTableRef SecTable) override;
270 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000271};
272
Jake Ehrliche8437de2017-12-19 00:47:30 +0000273class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000274 MAKE_SEC_WRITER_FRIEND
275
Jake Ehrliche8437de2017-12-19 00:47:30 +0000276 std::vector<uint8_t> Data;
277
278public:
279 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
280 : Data(std::begin(Data), std::end(Data)) {
281 Name = SecName;
282 Type = ELF::SHT_PROGBITS;
283 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000284 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000285 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000286
287 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000288};
289
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000290// There are two types of string tables that can exist, dynamic and not dynamic.
291// In the dynamic case the string table is allocated. Changing a dynamic string
292// table would mean altering virtual addresses and thus the memory image. So
293// dynamic string tables should not have an interface to modify them or
294// reconstruct them. This type lets us reconstruct a string table. To avoid
295// this class being used for dynamic string tables (which has happened) the
296// classof method checks that the particular instance is not allocated. This
297// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000298class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000299 MAKE_SEC_WRITER_FRIEND
300
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000301 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000302
303public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000304 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
305 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000306 }
307
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000308 void addString(StringRef Name);
309 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000310 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000311 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000312
Petr Hosek05a04cb2017-08-01 00:33:58 +0000313 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000314 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000315 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000316 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000317 }
318};
319
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000320// Symbols have a st_shndx field that normally stores an index but occasionally
321// stores a different special value. This enum keeps track of what the st_shndx
322// field means. Most of the values are just copies of the special SHN_* values.
323// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
324enum SymbolShndxType {
325 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000326 SYMBOL_ABS = ELF::SHN_ABS,
327 SYMBOL_COMMON = ELF::SHN_COMMON,
328 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
329 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
330 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
331 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000332};
333
Petr Hosek79cee9e2017-08-29 02:12:03 +0000334struct Symbol {
335 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000336 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000337 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000338 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000339 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000340 uint32_t NameIndex;
341 uint64_t Size;
342 uint8_t Type;
343 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000344 uint8_t Visibility;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000345
346 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000347};
348
349class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000350 MAKE_SEC_WRITER_FRIEND
351
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000352 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000353 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000354
Petr Hosek79cee9e2017-08-29 02:12:03 +0000355protected:
356 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000357 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000358
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000359 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000360
Petr Hosek79cee9e2017-08-29 02:12:03 +0000361public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000362 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000363 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
364 uint16_t Shndx, uint64_t Sz);
Jake Ehrlich8b831c12018-03-07 20:33:02 +0000365 void addSymbolNames();
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000366 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000367 const Symbol *getSymbolByIndex(uint32_t Index) const;
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000368 void updateSymbols(function_ref<void(Symbol &)> Callable);
369
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000370 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000371 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000372 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000373 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000374
Petr Hosek79cee9e2017-08-29 02:12:03 +0000375 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000376 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000377 }
378};
379
Petr Hosekd7df9b22017-09-06 23:41:02 +0000380struct Relocation {
Jake Ehrliched95fce2017-09-27 00:44:00 +0000381 const Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000382 uint64_t Offset;
383 uint64_t Addend;
384 uint32_t Type;
385};
386
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000387// All relocation sections denote relocations to apply to another section.
388// However, some relocation sections use a dynamic symbol table and others use
389// a regular symbol table. Because the types of the two symbol tables differ in
390// our system (because they should behave differently) we can't uniformly
391// represent all relocations with the same base class if we expose an interface
392// that mentions the symbol table type. So we split the two base types into two
393// different classes, one which handles the section the relocation is applied to
394// and another which handles the symbol table type. The symbol table type is
395// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
396class RelocationSectionBase : public SectionBase {
397protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000398 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000399
400public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000401 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000402 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000403
404 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000405 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000406 }
407};
408
409// Takes the symbol table type to use as a parameter so that we can deduplicate
410// that code between the two symbol table types.
411template <class SymTabType>
412class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000413 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000414 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000415
416protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000417 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000418
419public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000420 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000421 void initialize(SectionTableRef SecTable) override;
422 void finalize() override;
423};
424
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000425class RelocationSection
426 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000427 MAKE_SEC_WRITER_FRIEND
428
Petr Hosekd7df9b22017-09-06 23:41:02 +0000429 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000430
Petr Hosekd7df9b22017-09-06 23:41:02 +0000431public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000432 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000433 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000434
Petr Hosekd7df9b22017-09-06 23:41:02 +0000435 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000436 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000437 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000438 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000439 }
440};
441
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000442// TODO: The way stripping and groups interact is complicated
443// and still needs to be worked on.
444
445class GroupSection : public SectionBase {
446 MAKE_SEC_WRITER_FRIEND
447 const SymbolTableSection *SymTab = nullptr;
448 const Symbol *Sym = nullptr;
449 ELF::Elf32_Word FlagWord;
450 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000451
452public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000453 // TODO: Contents is present in several classes of the hierarchy.
454 // This needs to be refactored to avoid duplication.
455 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000456
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000457 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
458
459 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
460 void setSymbol(const Symbol *S) { Sym = S; }
461 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
462 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
463
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000464 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000465 void accept(SectionVisitor &) const override;
466 void finalize() override;
467
468 static bool classof(const SectionBase *S) {
469 return S->Type == ELF::SHT_GROUP;
470 }
471};
472
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000473class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000474public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000475 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000476
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000477 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000478 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000479 }
480};
481
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000482class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000483public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000484 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000485
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000486 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000487 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000488 }
489};
490
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000491class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000492 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000493 MAKE_SEC_WRITER_FRIEND
494
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000495private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000496 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000497
498public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000499 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000500
Jake Ehrlich76e91102018-01-25 22:46:17 +0000501 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000502
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000503 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000504 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000505 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000506 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000507 }
508};
509
Jake Ehrlich76e91102018-01-25 22:46:17 +0000510class GnuDebugLinkSection : public SectionBase {
511 MAKE_SEC_WRITER_FRIEND
512
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000513private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000514 StringRef FileName;
515 uint32_t CRC32;
516
517 void init(StringRef File, StringRef Data);
518
519public:
520 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000521 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000522 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000523};
524
Jake Ehrlich76e91102018-01-25 22:46:17 +0000525class Reader {
526public:
527 virtual ~Reader();
528 virtual std::unique_ptr<Object> create() const = 0;
529};
530
Jake Ehrlich76e91102018-01-25 22:46:17 +0000531using object::Binary;
532using object::ELFFile;
533using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000534using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000535
536template <class ELFT> class ELFBuilder {
537private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000538 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000539 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000540 using Elf_Ehdr = typename ELFT::Ehdr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000541
542 const ELFFile<ELFT> &ElfFile;
543 Object &Obj;
544
Jake Ehrlich6452b112018-02-14 23:31:33 +0000545 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000546 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000547 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000548 void initSymbolTable(SymbolTableSection *SymTab);
549 void readSectionHeaders();
550 SectionBase &makeSection(const Elf_Shdr &Shdr);
551
552public:
553 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
554 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
555
556 void build();
557};
558
559class ELFReader : public Reader {
560private:
Jake Ehrlich9634e182018-01-26 02:01:37 +0000561 std::unique_ptr<Binary> Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000562 std::shared_ptr<MemoryBuffer> Data;
563
564public:
565 ElfType getElfType() const;
566 std::unique_ptr<Object> create() const override;
567 ELFReader(StringRef File);
568};
569
570class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000571private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000572 using SecPtr = std::unique_ptr<SectionBase>;
573 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000574
Jake Ehrlich76e91102018-01-25 22:46:17 +0000575 std::shared_ptr<MemoryBuffer> OwnedData;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000576 std::vector<SecPtr> Sections;
577 std::vector<SegPtr> Segments;
578
Petr Hosek05a04cb2017-08-01 00:33:58 +0000579public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000580 template <class T>
581 using Range = iterator_range<
582 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
583
584 template <class T>
585 using ConstRange = iterator_range<pointee_iterator<
586 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
587
Jake Ehrlich6452b112018-02-14 23:31:33 +0000588 // It is often the case that the ELF header and the program header table are
589 // not present in any segment. This could be a problem during file layout,
590 // because other segments may get assigned an offset where either of the
591 // two should reside, which will effectively corrupt the resulting binary.
592 // Other than that we use these segments to track program header offsets
593 // when they may not follow the ELF header.
594 Segment ElfHdrSegment;
595 Segment ProgramHdrSegment;
596
Petr Hosek05a04cb2017-08-01 00:33:58 +0000597 uint8_t Ident[16];
598 uint64_t Entry;
599 uint64_t SHOffset;
600 uint32_t Type;
601 uint32_t Machine;
602 uint32_t Version;
603 uint32_t Flags;
604
Jake Ehrlich76e91102018-01-25 22:46:17 +0000605 StringTableSection *SectionNames = nullptr;
606 SymbolTableSection *SymbolTable = nullptr;
607
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000608 explicit Object(std::shared_ptr<MemoryBuffer> Data)
609 : OwnedData(std::move(Data)) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000610 virtual ~Object() = default;
611
Aaron Ballman09f46a72018-01-25 21:03:38 +0000612 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000613 SectionTableRef sections() { return SectionTableRef(Sections); }
614 ConstRange<SectionBase> sections() const {
615 return make_pointee_range(Sections);
616 }
617 Range<Segment> segments() { return make_pointee_range(Segments); }
618 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000619
Jake Ehrlich76e91102018-01-25 22:46:17 +0000620 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
621 template <class T, class... Ts> T &addSection(Ts &&... Args) {
622 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
623 auto Ptr = Sec.get();
624 Sections.emplace_back(std::move(Sec));
625 return *Ptr;
626 }
627 Segment &addSegment(ArrayRef<uint8_t> Data) {
628 Segments.emplace_back(llvm::make_unique<Segment>(Data));
629 return *Segments.back();
630 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000631};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000632} // end namespace llvm
633
634#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H