blob: 2e20b5b299f71b3623074f88c3004a7c70ddca8a [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
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000030class Buffer;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000031class SectionBase;
Jake Ehrlich76e91102018-01-25 22:46:17 +000032class Section;
33class OwnedDataSection;
34class StringTableSection;
35class SymbolTableSection;
36class RelocationSection;
37class DynamicRelocationSection;
38class GnuDebugLinkSection;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000039class GroupSection;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000040class Segment;
Jake Ehrlich76e91102018-01-25 22:46:17 +000041class Object;
Paul Semel4246a462018-05-09 21:36:54 +000042struct Symbol;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000043
44class SectionTableRef {
Jake Ehrlich76e91102018-01-25 22:46:17 +000045 MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
Jake Ehrlichf5a43772017-09-25 20:37:28 +000046
47public:
Jake Ehrlich76e91102018-01-25 22:46:17 +000048 using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
49
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000050 explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
Jake Ehrlichf5a43772017-09-25 20:37:28 +000051 : Sections(Secs) {}
52 SectionTableRef(const SectionTableRef &) = default;
53
Jake Ehrlich76e91102018-01-25 22:46:17 +000054 iterator begin() { return iterator(Sections.data()); }
55 iterator end() { return iterator(Sections.data() + Sections.size()); }
56
Jake Ehrlich8b831c12018-03-07 20:33:02 +000057 SectionBase *getSection(uint16_t Index, Twine ErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000058
59 template <class T>
Jake Ehrlich8b831c12018-03-07 20:33:02 +000060 T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
Jake Ehrlichf5a43772017-09-25 20:37:28 +000061};
Petr Hosek05a04cb2017-08-01 00:33:58 +000062
Jake Ehrlich76e91102018-01-25 22:46:17 +000063enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
64
65class SectionVisitor {
66public:
67 virtual ~SectionVisitor();
68
69 virtual void visit(const Section &Sec) = 0;
70 virtual void visit(const OwnedDataSection &Sec) = 0;
71 virtual void visit(const StringTableSection &Sec) = 0;
72 virtual void visit(const SymbolTableSection &Sec) = 0;
73 virtual void visit(const RelocationSection &Sec) = 0;
74 virtual void visit(const DynamicRelocationSection &Sec) = 0;
75 virtual void visit(const GnuDebugLinkSection &Sec) = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000076 virtual void visit(const GroupSection &Sec) = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000077};
78
79class SectionWriter : public SectionVisitor {
80protected:
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000081 Buffer &Out;
Jake Ehrlich76e91102018-01-25 22:46:17 +000082
83public:
84 virtual ~SectionWriter(){};
85
86 void visit(const Section &Sec) override;
87 void visit(const OwnedDataSection &Sec) override;
88 void visit(const StringTableSection &Sec) override;
89 void visit(const DynamicRelocationSection &Sec) override;
90 virtual void visit(const SymbolTableSection &Sec) override = 0;
91 virtual void visit(const RelocationSection &Sec) override = 0;
92 virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +000093 virtual void visit(const GroupSection &Sec) override = 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +000094
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000095 explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +000096};
97
98template <class ELFT> class ELFSectionWriter : public SectionWriter {
99private:
100 using Elf_Word = typename ELFT::Word;
101 using Elf_Rel = typename ELFT::Rel;
102 using Elf_Rela = typename ELFT::Rela;
103
104public:
105 virtual ~ELFSectionWriter() {}
106 void visit(const SymbolTableSection &Sec) override;
107 void visit(const RelocationSection &Sec) override;
108 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000109 void visit(const GroupSection &Sec) override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000110
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000111 explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000112};
113
114#define MAKE_SEC_WRITER_FRIEND \
115 friend class SectionWriter; \
116 template <class ELFT> friend class ELFSectionWriter;
117
118class BinarySectionWriter : public SectionWriter {
119public:
120 virtual ~BinarySectionWriter() {}
121
122 void visit(const SymbolTableSection &Sec) override;
123 void visit(const RelocationSection &Sec) override;
124 void visit(const GnuDebugLinkSection &Sec) override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000125 void visit(const GroupSection &Sec) override;
126
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000127 explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
128};
129
130// The class Buffer abstracts out the common interface of FileOutputBuffer and
131// WritableMemoryBuffer so that the hierarchy of Writers depends on this
132// abstract interface and doesn't depend on a particular implementation.
133// TODO: refactor the buffer classes in LLVM to enable us to use them here
134// directly.
135class Buffer {
136 StringRef Name;
137
138public:
139 virtual ~Buffer();
140 virtual void allocate(size_t Size) = 0;
141 virtual uint8_t *getBufferStart() = 0;
142 virtual Error commit() = 0;
143
144 explicit Buffer(StringRef Name) : Name(Name) {}
145 StringRef getName() const { return Name; }
146};
147
148class FileBuffer : public Buffer {
149 std::unique_ptr<FileOutputBuffer> Buf;
150
151public:
152 void allocate(size_t Size) override;
153 uint8_t *getBufferStart() override;
154 Error commit() override;
155
156 explicit FileBuffer(StringRef FileName) : Buffer(FileName) {}
157};
158
159class MemBuffer : public Buffer {
160 std::unique_ptr<WritableMemoryBuffer> Buf;
161
162public:
163 void allocate(size_t Size) override;
164 uint8_t *getBufferStart() override;
165 Error commit() override;
166
167 explicit MemBuffer(StringRef Name) : Buffer(Name) {}
168
169 std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000170};
171
172class Writer {
173protected:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000174 Object &Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000175 Buffer &Buf;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000176
177public:
178 virtual ~Writer();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000179 virtual void finalize() = 0;
180 virtual void write() = 0;
181
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000182 Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000183};
184
185template <class ELFT> class ELFWriter : public Writer {
186private:
187 using Elf_Shdr = typename ELFT::Shdr;
188 using Elf_Phdr = typename ELFT::Phdr;
189 using Elf_Ehdr = typename ELFT::Ehdr;
190
191 void writeEhdr();
192 void writePhdr(const Segment &Seg);
193 void writeShdr(const SectionBase &Sec);
194
195 void writePhdrs();
196 void writeShdrs();
197 void writeSectionData();
198
199 void assignOffsets();
200
201 std::unique_ptr<ELFSectionWriter<ELFT>> SecWriter;
202
203 size_t totalSize() const;
204
205public:
206 virtual ~ELFWriter() {}
207 bool WriteSectionHeaders = true;
208
209 void finalize() override;
210 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000211 ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
212 : Writer(Obj, Buf), WriteSectionHeaders(WSH) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000213};
214
215class BinaryWriter : public Writer {
216private:
217 std::unique_ptr<BinarySectionWriter> SecWriter;
218
219 uint64_t TotalSize;
220
221public:
222 ~BinaryWriter() {}
223 void finalize() override;
224 void write() override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000225 BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000226};
227
Petr Hosek05a04cb2017-08-01 00:33:58 +0000228class SectionBase {
229public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000230 StringRef Name;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000231 Segment *ParentSegment = nullptr;
232 uint64_t HeaderOffset;
233 uint64_t OriginalOffset;
234 uint32_t Index;
235
236 uint64_t Addr = 0;
237 uint64_t Align = 1;
238 uint32_t EntrySize = 0;
239 uint64_t Flags = 0;
240 uint64_t Info = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000241 uint64_t Link = ELF::SHN_UNDEF;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000242 uint64_t NameIndex = 0;
243 uint64_t Offset = 0;
244 uint64_t Size = 0;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000245 uint64_t Type = ELF::SHT_NULL;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000246
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000247 virtual ~SectionBase() = default;
248
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000249 virtual void initialize(SectionTableRef SecTable);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000250 virtual void finalize();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000251 virtual void removeSectionReferences(const SectionBase *Sec);
Paul Semel4246a462018-05-09 21:36:54 +0000252 virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000253 virtual void accept(SectionVisitor &Visitor) const = 0;
Paul Semel99dda0b2018-05-25 11:01:25 +0000254 virtual void markSymbols();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000255};
256
257class Segment {
258private:
259 struct SectionCompare {
260 bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
261 // Some sections might have the same address if one of them is empty. To
262 // fix this we can use the lexicographic ordering on ->Addr and the
263 // address of the actully stored section.
264 if (Lhs->OriginalOffset == Rhs->OriginalOffset)
265 return Lhs < Rhs;
266 return Lhs->OriginalOffset < Rhs->OriginalOffset;
267 }
268 };
269
270 std::set<const SectionBase *, SectionCompare> Sections;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000271 ArrayRef<uint8_t> Contents;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000272
273public:
274 uint64_t Align;
275 uint64_t FileSize;
276 uint32_t Flags;
277 uint32_t Index;
278 uint64_t MemSize;
279 uint64_t Offset;
280 uint64_t PAddr;
281 uint64_t Type;
282 uint64_t VAddr;
283
Petr Hosek3f383832017-08-26 01:32:20 +0000284 uint64_t OriginalOffset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000285 Segment *ParentSegment = nullptr;
Petr Hosek3f383832017-08-26 01:32:20 +0000286
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000287 explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
Jake Ehrlich6452b112018-02-14 23:31:33 +0000288 Segment() {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000289
Petr Hosek05a04cb2017-08-01 00:33:58 +0000290 const SectionBase *firstSection() const {
291 if (!Sections.empty())
292 return *Sections.begin();
293 return nullptr;
294 }
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000295
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000296 void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
297 void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000298};
299
300class Section : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000301 MAKE_SEC_WRITER_FRIEND
302
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000303 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000304 SectionBase *LinkSection = nullptr;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000305
306public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000307 explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000308
Jake Ehrlich76e91102018-01-25 22:46:17 +0000309 void accept(SectionVisitor &Visitor) const override;
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000310 void removeSectionReferences(const SectionBase *Sec) override;
311 void initialize(SectionTableRef SecTable) override;
312 void finalize() override;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000313};
314
Jake Ehrliche8437de2017-12-19 00:47:30 +0000315class OwnedDataSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000316 MAKE_SEC_WRITER_FRIEND
317
Jake Ehrliche8437de2017-12-19 00:47:30 +0000318 std::vector<uint8_t> Data;
319
320public:
321 OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
322 : Data(std::begin(Data), std::end(Data)) {
323 Name = SecName;
324 Type = ELF::SHT_PROGBITS;
325 Size = Data.size();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000326 OriginalOffset = std::numeric_limits<uint64_t>::max();
Jake Ehrliche8437de2017-12-19 00:47:30 +0000327 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000328
329 void accept(SectionVisitor &Sec) const override;
Jake Ehrliche8437de2017-12-19 00:47:30 +0000330};
331
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000332// There are two types of string tables that can exist, dynamic and not dynamic.
333// In the dynamic case the string table is allocated. Changing a dynamic string
334// table would mean altering virtual addresses and thus the memory image. So
335// dynamic string tables should not have an interface to modify them or
336// reconstruct them. This type lets us reconstruct a string table. To avoid
337// this class being used for dynamic string tables (which has happened) the
338// classof method checks that the particular instance is not allocated. This
339// then agrees with the makeSection method used to construct most sections.
Petr Hosek05a04cb2017-08-01 00:33:58 +0000340class StringTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000341 MAKE_SEC_WRITER_FRIEND
342
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000343 StringTableBuilder StrTabBuilder;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000344
345public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000346 StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
347 Type = ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000348 }
349
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000350 void addString(StringRef Name);
351 uint32_t findIndex(StringRef Name) const;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000352 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000353 void accept(SectionVisitor &Visitor) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000354
Petr Hosek05a04cb2017-08-01 00:33:58 +0000355 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000356 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich70bd75f2017-10-10 21:28:22 +0000357 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000358 return S->Type == ELF::SHT_STRTAB;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000359 }
360};
361
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000362// Symbols have a st_shndx field that normally stores an index but occasionally
363// stores a different special value. This enum keeps track of what the st_shndx
364// field means. Most of the values are just copies of the special SHN_* values.
365// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
366enum SymbolShndxType {
367 SYMBOL_SIMPLE_INDEX = 0,
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000368 SYMBOL_ABS = ELF::SHN_ABS,
369 SYMBOL_COMMON = ELF::SHN_COMMON,
370 SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
371 SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
372 SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
373 SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000374};
375
Petr Hosek79cee9e2017-08-29 02:12:03 +0000376struct Symbol {
377 uint8_t Binding;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000378 SectionBase *DefinedIn = nullptr;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000379 SymbolShndxType ShndxType;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000380 uint32_t Index;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000381 StringRef Name;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000382 uint32_t NameIndex;
383 uint64_t Size;
384 uint8_t Type;
385 uint64_t Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000386 uint8_t Visibility;
Paul Semel99dda0b2018-05-25 11:01:25 +0000387 bool Referenced = false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000388
389 uint16_t getShndx() const;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000390};
391
392class SymbolTableSection : public SectionBase {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000393 MAKE_SEC_WRITER_FRIEND
394
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000395 void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000396 void assignIndices();
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000397
Petr Hosek79cee9e2017-08-29 02:12:03 +0000398protected:
399 std::vector<std::unique_ptr<Symbol>> Symbols;
Jake Ehrliched95fce2017-09-27 00:44:00 +0000400 StringTableSection *SymbolNames = nullptr;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000401
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000402 using SymPtr = std::unique_ptr<Symbol>;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000403
Petr Hosek79cee9e2017-08-29 02:12:03 +0000404public:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000405 void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000406 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
407 uint16_t Shndx, uint64_t Sz);
Jake Ehrlich8b831c12018-03-07 20:33:02 +0000408 void addSymbolNames();
Paul Semel46201fb2018-06-01 16:19:46 +0000409 // An 'empty' symbol table still contains a null symbol.
410 bool empty() const { return Symbols.size() == 1; }
Jake Ehrlichef3b80c2017-11-30 20:14:53 +0000411 const SectionBase *getStrTab() const { return SymbolNames; }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000412 const Symbol *getSymbolByIndex(uint32_t Index) const;
Paul Semel99dda0b2018-05-25 11:01:25 +0000413 Symbol *getSymbolByIndex(uint32_t Index);
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000414 void updateSymbols(function_ref<void(Symbol &)> Callable);
415
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000416 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000417 void initialize(SectionTableRef SecTable) override;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000418 void finalize() override;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000419 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000420 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000421
Petr Hosek79cee9e2017-08-29 02:12:03 +0000422 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000423 return S->Type == ELF::SHT_SYMTAB;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000424 }
425};
426
Petr Hosekd7df9b22017-09-06 23:41:02 +0000427struct Relocation {
Paul Semel99dda0b2018-05-25 11:01:25 +0000428 Symbol *RelocSymbol = nullptr;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000429 uint64_t Offset;
430 uint64_t Addend;
431 uint32_t Type;
432};
433
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000434// All relocation sections denote relocations to apply to another section.
435// However, some relocation sections use a dynamic symbol table and others use
436// a regular symbol table. Because the types of the two symbol tables differ in
437// our system (because they should behave differently) we can't uniformly
438// represent all relocations with the same base class if we expose an interface
439// that mentions the symbol table type. So we split the two base types into two
440// different classes, one which handles the section the relocation is applied to
441// and another which handles the symbol table type. The symbol table type is
442// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
443class RelocationSectionBase : public SectionBase {
444protected:
Jake Ehrliched95fce2017-09-27 00:44:00 +0000445 SectionBase *SecToApplyRel = nullptr;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000446
447public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000448 const SectionBase *getSection() const { return SecToApplyRel; }
Jake Ehrlichc5ff7272017-10-10 18:32:22 +0000449 void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000450
451 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000452 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000453 }
454};
455
456// Takes the symbol table type to use as a parameter so that we can deduplicate
457// that code between the two symbol table types.
458template <class SymTabType>
459class RelocSectionWithSymtabBase : public RelocationSectionBase {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000460 SymTabType *Symbols = nullptr;
Alexander Shaposhnikova8f15502018-02-24 00:41:01 +0000461 void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000462
463protected:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000464 RelocSectionWithSymtabBase() = default;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000465
466public:
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000467 void removeSectionReferences(const SectionBase *Sec) override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000468 void initialize(SectionTableRef SecTable) override;
469 void finalize() override;
470};
471
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000472class RelocationSection
473 : public RelocSectionWithSymtabBase<SymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000474 MAKE_SEC_WRITER_FRIEND
475
Petr Hosekd7df9b22017-09-06 23:41:02 +0000476 std::vector<Relocation> Relocations;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000477
Petr Hosekd7df9b22017-09-06 23:41:02 +0000478public:
Petr Hosekd7df9b22017-09-06 23:41:02 +0000479 void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000480 void accept(SectionVisitor &Visitor) const override;
Paul Semel4246a462018-05-09 21:36:54 +0000481 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000482 void markSymbols() override;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000483
Petr Hosekd7df9b22017-09-06 23:41:02 +0000484 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000485 if (S->Flags & ELF::SHF_ALLOC)
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000486 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000487 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000488 }
489};
490
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000491// TODO: The way stripping and groups interact is complicated
492// and still needs to be worked on.
493
494class GroupSection : public SectionBase {
495 MAKE_SEC_WRITER_FRIEND
496 const SymbolTableSection *SymTab = nullptr;
Paul Semel99dda0b2018-05-25 11:01:25 +0000497 Symbol *Sym = nullptr;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000498 ELF::Elf32_Word FlagWord;
499 SmallVector<SectionBase *, 3> GroupMembers;
Alexander Shaposhnikov43b8acd2018-03-20 18:20:42 +0000500
501public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000502 // TODO: Contents is present in several classes of the hierarchy.
503 // This needs to be refactored to avoid duplication.
504 ArrayRef<uint8_t> Contents;
Alexander Shaposhnikov3b24ed72018-03-20 19:46:00 +0000505
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000506 explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
507
508 void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
Paul Semel99dda0b2018-05-25 11:01:25 +0000509 void setSymbol(Symbol *S) { Sym = S; }
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000510 void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
511 void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
512
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000513 void initialize(SectionTableRef SecTable) override{};
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000514 void accept(SectionVisitor &) const override;
515 void finalize() override;
Paul Semel4246a462018-05-09 21:36:54 +0000516 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
Paul Semel99dda0b2018-05-25 11:01:25 +0000517 void markSymbols() override;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000518
519 static bool classof(const SectionBase *S) {
520 return S->Type == ELF::SHT_GROUP;
521 }
522};
523
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000524class DynamicSymbolTableSection : public Section {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000525public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000526 explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000527
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000528 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000529 return S->Type == ELF::SHT_DYNSYM;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000530 }
531};
532
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000533class DynamicSection : public Section {
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000534public:
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000535 explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000536
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000537 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000538 return S->Type == ELF::SHT_DYNAMIC;
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000539 }
540};
541
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000542class DynamicRelocationSection
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000543 : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000544 MAKE_SEC_WRITER_FRIEND
545
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000546private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000547 ArrayRef<uint8_t> Contents;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000548
549public:
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000550 explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000551
Jake Ehrlich76e91102018-01-25 22:46:17 +0000552 void accept(SectionVisitor &) const override;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000553
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000554 static bool classof(const SectionBase *S) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000555 if (!(S->Flags & ELF::SHF_ALLOC))
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000556 return false;
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000557 return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000558 }
559};
560
Jake Ehrlich76e91102018-01-25 22:46:17 +0000561class GnuDebugLinkSection : public SectionBase {
562 MAKE_SEC_WRITER_FRIEND
563
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000564private:
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000565 StringRef FileName;
566 uint32_t CRC32;
567
568 void init(StringRef File, StringRef Data);
569
570public:
571 // If we add this section from an external source we can use this ctor.
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000572 explicit GnuDebugLinkSection(StringRef File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000573 void accept(SectionVisitor &Visitor) const override;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000574};
575
Jake Ehrlich76e91102018-01-25 22:46:17 +0000576class Reader {
577public:
578 virtual ~Reader();
579 virtual std::unique_ptr<Object> create() const = 0;
580};
581
Jake Ehrlich76e91102018-01-25 22:46:17 +0000582using object::Binary;
583using object::ELFFile;
584using object::ELFObjectFile;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000585using object::OwningBinary;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000586
587template <class ELFT> class ELFBuilder {
588private:
Jake Ehrlich6452b112018-02-14 23:31:33 +0000589 using Elf_Addr = typename ELFT::Addr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000590 using Elf_Shdr = typename ELFT::Shdr;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000591 using Elf_Ehdr = typename ELFT::Ehdr;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000592
593 const ELFFile<ELFT> &ElfFile;
594 Object &Obj;
595
Jake Ehrlich6452b112018-02-14 23:31:33 +0000596 void setParentSegment(Segment &Child);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000597 void readProgramHeaders();
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000598 void initGroupSection(GroupSection *GroupSec);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000599 void initSymbolTable(SymbolTableSection *SymTab);
600 void readSectionHeaders();
601 SectionBase &makeSection(const Elf_Shdr &Shdr);
602
603public:
604 ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
605 : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
606
607 void build();
608};
609
610class ELFReader : public Reader {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000611 Binary *Bin;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000612
613public:
614 ElfType getElfType() const;
615 std::unique_ptr<Object> create() const override;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000616 explicit ELFReader(Binary *B) : Bin(B){};
Jake Ehrlich76e91102018-01-25 22:46:17 +0000617};
618
619class Object {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000620private:
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000621 using SecPtr = std::unique_ptr<SectionBase>;
622 using SegPtr = std::unique_ptr<Segment>;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000623
Petr Hosekc4df10e2017-08-04 21:09:26 +0000624 std::vector<SecPtr> Sections;
625 std::vector<SegPtr> Segments;
626
Petr Hosek05a04cb2017-08-01 00:33:58 +0000627public:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000628 template <class T>
629 using Range = iterator_range<
630 pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
631
632 template <class T>
633 using ConstRange = iterator_range<pointee_iterator<
634 typename std::vector<std::unique_ptr<T>>::const_iterator>>;
635
Jake Ehrlich6452b112018-02-14 23:31:33 +0000636 // It is often the case that the ELF header and the program header table are
637 // not present in any segment. This could be a problem during file layout,
638 // because other segments may get assigned an offset where either of the
639 // two should reside, which will effectively corrupt the resulting binary.
640 // Other than that we use these segments to track program header offsets
641 // when they may not follow the ELF header.
642 Segment ElfHdrSegment;
643 Segment ProgramHdrSegment;
644
Petr Hosek05a04cb2017-08-01 00:33:58 +0000645 uint8_t Ident[16];
646 uint64_t Entry;
647 uint64_t SHOffset;
648 uint32_t Type;
649 uint32_t Machine;
650 uint32_t Version;
651 uint32_t Flags;
652
Jake Ehrlich76e91102018-01-25 22:46:17 +0000653 StringTableSection *SectionNames = nullptr;
654 SymbolTableSection *SymbolTable = nullptr;
655
Aaron Ballman09f46a72018-01-25 21:03:38 +0000656 void sortSections();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000657 SectionTableRef sections() { return SectionTableRef(Sections); }
658 ConstRange<SectionBase> sections() const {
659 return make_pointee_range(Sections);
660 }
661 Range<Segment> segments() { return make_pointee_range(Segments); }
662 ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
Aaron Ballman09f46a72018-01-25 21:03:38 +0000663
Jake Ehrlich76e91102018-01-25 22:46:17 +0000664 void removeSections(std::function<bool(const SectionBase &)> ToRemove);
Paul Semel4246a462018-05-09 21:36:54 +0000665 void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000666 template <class T, class... Ts> T &addSection(Ts &&... Args) {
667 auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
668 auto Ptr = Sec.get();
669 Sections.emplace_back(std::move(Sec));
670 return *Ptr;
671 }
672 Segment &addSegment(ArrayRef<uint8_t> Data) {
673 Segments.emplace_back(llvm::make_unique<Segment>(Data));
674 return *Segments.back();
675 }
Petr Hosekc4df10e2017-08-04 21:09:26 +0000676};
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000677} // end namespace llvm
678
679#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H