blob: d005f0d49022646663c475e88ab23e7e05b0ea8c [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;
Paul Semel4246a462018-05-09 21:36:54 +000041struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000042
43class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000044 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000045
46public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000047 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
48
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000049 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000050 : Sections(Secs) {}
51 SectionTableRef(const SectionTableRef &) = default;
52
Jake Ehrlich76e91102018-01-25 22:46:17 +000053 iterator begin() { return iterator(Sections.data()); }
54 iterator end() { return iterator(Sections.data() + Sections.size()); }
55
Jake Ehrlich8b831c12018-03-07 20:33:02 +000056 SectionBase *getSection(uint16_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000057
58 template <class T>
Jake Ehrlich8b831c12018-03-07 20:33:02 +000059 T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000060};
Petr Hosek05a04cb2017-08-01 00:33:58 +000061
Jake Ehrlich76e91102018-01-25 22:46:17 +000062enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
63
64class SectionVisitor {
65public:
66 virtual ~SectionVisitor();
67
68 virtual void visit(const Section &Sec) = 0;
69 virtual void visit(const OwnedDataSection &Sec) = 0;
70 virtual void visit(const StringTableSection &Sec) = 0;
71 virtual void visit(const SymbolTableSection &Sec) = 0;
72 virtual void visit(const RelocationSection &Sec) = 0;
73 virtual void visit(const DynamicRelocationSection &Sec) = 0;
74 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000075 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000076};
77
78class SectionWriter : public SectionVisitor {
79protected:
80 FileOutputBuffer &Out;
81
82public:
83 virtual ~SectionWriter(){};
84
85 void visit(const Section &Sec) override;
86 void visit(const OwnedDataSection &Sec) override;
87 void visit(const StringTableSection &Sec) override;
88 void visit(const DynamicRelocationSection &Sec) override;
89 virtual void visit(const SymbolTableSection &Sec) override = 0;
90 virtual void visit(const RelocationSection &Sec) override = 0;
91 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000092 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000093
94 SectionWriter(FileOutputBuffer &Buf) : Out(Buf) {}
95};
96
97template <class ELFT> class ELFSectionWriter : public SectionWriter {
98private:
99 using Elf_Word = typename ELFT::Word;
100 using Elf_Rel = typename ELFT::Rel;
101 using Elf_Rela = typename ELFT::Rela;
102
103public:
104 virtual ~ELFSectionWriter() {}
105 void visit(const SymbolTableSection &Sec) override;
106 void visit(const RelocationSection &Sec) override;
107 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000108 void visit(const GroupSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000109
110 ELFSectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
111};
112
113#define MAKE_SEC_WRITER_FRIEND \
114 friend class SectionWriter; \
115 template <class ELFT> friend class ELFSectionWriter;
116
117class BinarySectionWriter : public SectionWriter {
118public:
119 virtual ~BinarySectionWriter() {}
120
121 void visit(const SymbolTableSection &Sec) override;
122 void visit(const RelocationSection &Sec) override;
123 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000124 void visit(const GroupSection &Sec) override;
125
Jake Ehrlich76e91102018-01-25 22:46:17 +0000126 BinarySectionWriter(FileOutputBuffer &Buf) : SectionWriter(Buf) {}
127};
128
129class Writer {
130protected:
131 StringRef File;
132 Object &Obj;
133 std::unique_ptr<FileOutputBuffer> BufPtr;
134
135 void createBuffer(uint64_t Size);
136
137public:
138 virtual ~Writer();
139
140 virtual void finalize() = 0;
141 virtual void write() = 0;
142
143 Writer(StringRef File, Object &Obj) : File(File), Obj(Obj) {}
144};
145
146template <class ELFT> class ELFWriter : public Writer {
147private:
148 using Elf_Shdr = typename ELFT::Shdr;
149 using Elf_Phdr = typename ELFT::Phdr;
150 using Elf_Ehdr = typename ELFT::Ehdr;
151
152 void writeEhdr();
153 void writePhdr(const Segment &Seg);
154 void writeShdr(const SectionBase &Sec);
155
156 void writePhdrs();
157 void writeShdrs();
158 void writeSectionData();
159
160 void assignOffsets();
161
162 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
163
164 size_t totalSize() const;
165
166public:
167 virtual ~ELFWriter() {}
168 bool WriteSectionHeaders = true;
169
170 void finalize() override;
171 void write() override;
172 ELFWriter(StringRef File, Object &Obj, bool WSH)
173 : Writer(File, Obj), WriteSectionHeaders(WSH) {}
174};
175
176class BinaryWriter : public Writer {
177private:
178 std::unique_ptr<BinarySectionWriter> SecWriter;
179
180 uint64_t TotalSize;
181
182public:
183 ~BinaryWriter() {}
184 void finalize() override;
185 void write() override;
186 BinaryWriter(StringRef File, Object &Obj) : Writer(File, Obj) {}
187};
188
Petr Hosek05a04cb2017-08-01 00:33:58 +0000189class SectionBase {
190public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000191 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000192 Segment *ParentSegment = nullptr;
193 uint64_t HeaderOffset;
194 uint64_t OriginalOffset;
195 uint32_t Index;
196
197 uint64_t Addr = 0;
198 uint64_t Align = 1;
199 uint32_t EntrySize = 0;
200 uint64_t Flags = 0;
201 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000202 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000203 uint64_t NameIndex = 0;
204 uint64_t Offset = 0;
205 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000206 uint64_t Type = ELF::SHT_NULL;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000207
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000208 virtual ~SectionBase() = default;
209
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000210 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000211 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000212 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000213 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000214 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000215 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000216};
217
218class Segment {
219private:
220 struct SectionCompare {
221 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
222 // Some sections might have the same address if one of them is empty. To
223 // fix this we can use the lexicographic ordering on ->Addr and the
224 // address of the actully stored section.
225 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
226 return Lhs < Rhs;
227 return Lhs->OriginalOffset < Rhs->OriginalOffset;
228 }
229 };
230
231 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000232 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000233
234public:
235 uint64_t Align;
236 uint64_t FileSize;
237 uint32_t Flags;
238 uint32_t Index;
239 uint64_t MemSize;
240 uint64_t Offset;
241 uint64_t PAddr;
242 uint64_t Type;
243 uint64_t VAddr;
244
Petr Hosek3f383832017-08-26 01:32:20 +0000245 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000246 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000247
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000248 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000249 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000250
Petr Hosek05a04cb2017-08-01 00:33:58 +0000251 const SectionBase *firstSection() const {
252 if (!Sections.empty())
253 return *Sections.begin();
254 return nullptr;
255 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000256
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000257 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
258 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000259};
260
261class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000262 MAKE_SEC_WRITER_FRIEND
263
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000264 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000265 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000266
267public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000268 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000269
Jake Ehrlich76e91102018-01-25 22:46:17 +0000270 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000271 void removeSectionReferences(const SectionBase *Sec) override;
272 void initialize(SectionTableRef SecTable) override;
273 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000274};
275
Jake Ehrliche8437de2017-12-19 00:47:30 +0000276class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000277 MAKE_SEC_WRITER_FRIEND
278
Jake Ehrliche8437de2017-12-19 00:47:30 +0000279 std::vector<uint8_t> Data;
280
281public:
282 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
283 : Data(std::begin(Data), std::end(Data)) {
284 Name = SecName;
285 Type = ELF::SHT_PROGBITS;
286 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000287 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000288 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000289
290 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000291};
292
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000293// There are two types of string tables that can exist, dynamic and not dynamic.
294// In the dynamic case the string table is allocated. Changing a dynamic string
295// table would mean altering virtual addresses and thus the memory image. So
296// dynamic string tables should not have an interface to modify them or
297// reconstruct them. This type lets us reconstruct a string table. To avoid
298// this class being used for dynamic string tables (which has happened) the
299// classof method checks that the particular instance is not allocated. This
300// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000301class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000302 MAKE_SEC_WRITER_FRIEND
303
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000304 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000305
306public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000307 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
308 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000309 }
310
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000311 void addString(StringRef Name);
312 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000313 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000314 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000315
Petr Hosek05a04cb2017-08-01 00:33:58 +0000316 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000317 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000318 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000319 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000320 }
321};
322
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000323// Symbols have a st_shndx field that normally stores an index but occasionally
324// stores a different special value. This enum keeps track of what the st_shndx
325// field means. Most of the values are just copies of the special SHN_* values.
326// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
327enum SymbolShndxType {
328 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000329 SYMBOL_ABS = ELF::SHN_ABS,
330 SYMBOL_COMMON = ELF::SHN_COMMON,
331 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
332 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
333 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
334 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000335};
336
Petr Hosek79cee9e2017-08-29 02:12:03 +0000337struct Symbol {
338 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000339 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000340 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000341 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000342 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000343 uint32_t NameIndex;
344 uint64_t Size;
345 uint8_t Type;
346 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000347 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000348 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000349
350 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000351};
352
353class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000354 MAKE_SEC_WRITER_FRIEND
355
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000356 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000357 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000358
Petr Hosek79cee9e2017-08-29 02:12:03 +0000359protected:
360 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000361 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000362
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000363 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000364
Petr Hosek79cee9e2017-08-29 02:12:03 +0000365public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000366 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000367 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
368 uint16_t Shndx, uint64_t Sz);
Jake Ehrlich8b831c12018-03-07 20:33:02 +0000369 void addSymbolNames();
Alexander Shaposhnikov6e7814c2018-05-22 18:24:07 +0000370 bool empty() const { return Symbols.empty(); }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000371 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000372 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000373 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000374 void updateSymbols(function_ref<void(Symbol &)> Callable);
375
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000376 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000377 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000378 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000379 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000380 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000381
Petr Hosek79cee9e2017-08-29 02:12:03 +0000382 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000383 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000384 }
385};
386
Petr Hosekd7df9b22017-09-06 23:41:02 +0000387struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000388 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000389 uint64_t Offset;
390 uint64_t Addend;
391 uint32_t Type;
392};
393
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000394// All relocation sections denote relocations to apply to another section.
395// However, some relocation sections use a dynamic symbol table and others use
396// a regular symbol table. Because the types of the two symbol tables differ in
397// our system (because they should behave differently) we can't uniformly
398// represent all relocations with the same base class if we expose an interface
399// that mentions the symbol table type. So we split the two base types into two
400// different classes, one which handles the section the relocation is applied to
401// and another which handles the symbol table type. The symbol table type is
402// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
403class RelocationSectionBase : public SectionBase {
404protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000405 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000406
407public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000408 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000409 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000410
411 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000412 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000413 }
414};
415
416// Takes the symbol table type to use as a parameter so that we can deduplicate
417// that code between the two symbol table types.
418template <class SymTabType>
419class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000420 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000421 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000422
423protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000424 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000425
426public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000427 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000428 void initialize(SectionTableRef SecTable) override;
429 void finalize() override;
430};
431
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000432class RelocationSection
433 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000434 MAKE_SEC_WRITER_FRIEND
435
Petr Hosekd7df9b22017-09-06 23:41:02 +0000436 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000437
Petr Hosekd7df9b22017-09-06 23:41:02 +0000438public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000439 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000440 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000441 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000442 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000443
Petr Hosekd7df9b22017-09-06 23:41:02 +0000444 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000445 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000446 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000447 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000448 }
449};
450
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000451// TODO: The way stripping and groups interact is complicated
452// and still needs to be worked on.
453
454class GroupSection : public SectionBase {
455 MAKE_SEC_WRITER_FRIEND
456 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000457 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000458 ELF::Elf32_Word FlagWord;
459 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000460
461public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000462 // TODO: Contents is present in several classes of the hierarchy.
463 // This needs to be refactored to avoid duplication.
464 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000465
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000466 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
467
468 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000469 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000470 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
471 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
472
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000473 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000474 void accept(SectionVisitor &) const override;
475 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000476 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000477 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000478
479 static bool classof(const SectionBase *S) {
480 return S->Type == ELF::SHT_GROUP;
481 }
482};
483
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000484class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000485public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000486 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000487
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000488 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000489 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000490 }
491};
492
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000493class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000494public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000495 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000496
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000497 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000498 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000499 }
500};
501
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000502class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000503 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000504 MAKE_SEC_WRITER_FRIEND
505
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000506private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000507 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000508
509public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000510 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000511
Jake Ehrlich76e91102018-01-25 22:46:17 +0000512 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000513
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000514 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000515 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000516 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000517 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000518 }
519};
520
Jake Ehrlich76e91102018-01-25 22:46:17 +0000521class GnuDebugLinkSection : public SectionBase {
522 MAKE_SEC_WRITER_FRIEND
523
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000524private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000525 StringRef FileName;
526 uint32_t CRC32;
527
528 void init(StringRef File, StringRef Data);
529
530public:
531 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000532 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000533 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000534};
535
Jake Ehrlich76e91102018-01-25 22:46:17 +0000536class Reader {
537public:
538 virtual ~Reader();
539 virtual std::unique_ptr<Object> create() const = 0;
540};
541
Jake Ehrlich76e91102018-01-25 22:46:17 +0000542using object::Binary;
543using object::ELFFile;
544using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000545using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000546
547template <class ELFT> class ELFBuilder {
548private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000549 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000550 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000551 using Elf_Ehdr = typename ELFT::Ehdr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000552
553 const ELFFile<ELFT> &ElfFile;
554 Object &Obj;
555
Jake Ehrlich6452b112018-02-14 23:31:33 +0000556 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000557 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000558 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000559 void initSymbolTable(SymbolTableSection *SymTab);
560 void readSectionHeaders();
561 SectionBase &makeSection(const Elf_Shdr &Shdr);
562
563public:
564 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
565 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
566
567 void build();
568};
569
570class ELFReader : public Reader {
571private:
Jake Ehrlich9634e182018-01-26 02:01:37 +0000572 std::unique_ptr<Binary> Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000573 std::shared_ptr<MemoryBuffer> Data;
574
575public:
576 ElfType getElfType() const;
577 std::unique_ptr<Object> create() const override;
578 ELFReader(StringRef File);
579};
580
581class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000582private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000583 using SecPtr = std::unique_ptr<SectionBase>;
584 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000585
Jake Ehrlich76e91102018-01-25 22:46:17 +0000586 std::shared_ptr<MemoryBuffer> OwnedData;
Petr Hosekc4df10e2017-08-04 21:09:26 +0000587 std::vector<SecPtr> Sections;
588 std::vector<SegPtr> Segments;
589
Petr Hosek05a04cb2017-08-01 00:33:58 +0000590public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000591 template <class T>
592 using Range = iterator_range<
593 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
594
595 template <class T>
596 using ConstRange = iterator_range<pointee_iterator<
597 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
598
Jake Ehrlich6452b112018-02-14 23:31:33 +0000599 // It is often the case that the ELF header and the program header table are
600 // not present in any segment. This could be a problem during file layout,
601 // because other segments may get assigned an offset where either of the
602 // two should reside, which will effectively corrupt the resulting binary.
603 // Other than that we use these segments to track program header offsets
604 // when they may not follow the ELF header.
605 Segment ElfHdrSegment;
606 Segment ProgramHdrSegment;
607
Petr Hosek05a04cb2017-08-01 00:33:58 +0000608 uint8_t Ident[16];
609 uint64_t Entry;
610 uint64_t SHOffset;
611 uint32_t Type;
612 uint32_t Machine;
613 uint32_t Version;
614 uint32_t Flags;
615
Jake Ehrlich76e91102018-01-25 22:46:17 +0000616 StringTableSection *SectionNames = nullptr;
617 SymbolTableSection *SymbolTable = nullptr;
618
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000619 explicit Object(std::shared_ptr<MemoryBuffer> Data)
620 : OwnedData(std::move(Data)) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000621 virtual ~Object() = default;
622
Aaron Ballman09f46a72018-01-25 21:03:38 +0000623 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000624 SectionTableRef sections() { return SectionTableRef(Sections); }
625 ConstRange<SectionBase> sections() const {
626 return make_pointee_range(Sections);
627 }
628 Range<Segment> segments() { return make_pointee_range(Segments); }
629 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000630
Jake Ehrlich76e91102018-01-25 22:46:17 +0000631 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000632 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000633 template <class T, class... Ts> T &addSection(Ts &&... Args) {
634 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
635 auto Ptr = Sec.get();
636 Sections.emplace_back(std::move(Sec));
637 return *Ptr;
638 }
639 Segment &addSegment(ArrayRef<uint8_t> Data) {
640 Segments.emplace_back(llvm::make_unique<Segment>(Data));
641 return *Segments.back();
642 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000643};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000644} // end namespace llvm
645
646#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H