Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1 | //===- 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 Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 10 | #ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H |
| 11 | #define LLVM_TOOLS_OBJCOPY_OBJECT_H |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 12 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 13 | #include "llvm/ADT/ArrayRef.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
| 15 | #include "llvm/ADT/Twine.h" |
| 16 | #include "llvm/BinaryFormat/ELF.h" |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 17 | #include "llvm/MC/StringTableBuilder.h" |
| 18 | #include "llvm/Object/ELFObjectFile.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 19 | #include <cstddef> |
| 20 | #include <cstdint> |
| 21 | #include <functional> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <set> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 24 | #include <vector> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 25 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 26 | namespace llvm { |
| 27 | |
| 28 | class FileOutputBuffer; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 29 | class SectionBase; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 30 | class Segment; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 31 | |
| 32 | class SectionTableRef { |
| 33 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 34 | ArrayRef<std::unique_ptr<SectionBase>> Sections; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 35 | |
| 36 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 37 | SectionTableRef(ArrayRef<std::unique_ptr<SectionBase>> Secs) |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 38 | : Sections(Secs) {} |
| 39 | SectionTableRef(const SectionTableRef &) = default; |
| 40 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 41 | SectionBase *getSection(uint16_t Index, Twine ErrMsg); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 42 | |
| 43 | template <class T> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 44 | T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 45 | }; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 46 | |
| 47 | class SectionBase { |
| 48 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 49 | StringRef Name; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 50 | Segment *ParentSegment = nullptr; |
| 51 | uint64_t HeaderOffset; |
| 52 | uint64_t OriginalOffset; |
| 53 | uint32_t Index; |
| 54 | |
| 55 | uint64_t Addr = 0; |
| 56 | uint64_t Align = 1; |
| 57 | uint32_t EntrySize = 0; |
| 58 | uint64_t Flags = 0; |
| 59 | uint64_t Info = 0; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 60 | uint64_t Link = ELF::SHN_UNDEF; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 61 | uint64_t NameIndex = 0; |
| 62 | uint64_t Offset = 0; |
| 63 | uint64_t Size = 0; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 64 | uint64_t Type = ELF::SHT_NULL; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 65 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 66 | virtual ~SectionBase() = default; |
| 67 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 68 | virtual void initialize(SectionTableRef SecTable); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 69 | virtual void finalize(); |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 70 | virtual void removeSectionReferences(const SectionBase *Sec); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 71 | template <class ELFT> void writeHeader(FileOutputBuffer &Out) const; |
| 72 | virtual void writeSection(FileOutputBuffer &Out) const = 0; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | class Segment { |
| 76 | private: |
| 77 | struct SectionCompare { |
| 78 | bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const { |
| 79 | // Some sections might have the same address if one of them is empty. To |
| 80 | // fix this we can use the lexicographic ordering on ->Addr and the |
| 81 | // address of the actully stored section. |
| 82 | if (Lhs->OriginalOffset == Rhs->OriginalOffset) |
| 83 | return Lhs < Rhs; |
| 84 | return Lhs->OriginalOffset < Rhs->OriginalOffset; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | std::set<const SectionBase *, SectionCompare> Sections; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 89 | ArrayRef<uint8_t> Contents; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 90 | |
| 91 | public: |
| 92 | uint64_t Align; |
| 93 | uint64_t FileSize; |
| 94 | uint32_t Flags; |
| 95 | uint32_t Index; |
| 96 | uint64_t MemSize; |
| 97 | uint64_t Offset; |
| 98 | uint64_t PAddr; |
| 99 | uint64_t Type; |
| 100 | uint64_t VAddr; |
| 101 | |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 102 | uint64_t OriginalOffset; |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 103 | Segment *ParentSegment = nullptr; |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 104 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 105 | Segment(ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 106 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 107 | const SectionBase *firstSection() const { |
| 108 | if (!Sections.empty()) |
| 109 | return *Sections.begin(); |
| 110 | return nullptr; |
| 111 | } |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 112 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 113 | void removeSection(const SectionBase *Sec) { Sections.erase(Sec); } |
| 114 | void addSection(const SectionBase *Sec) { Sections.insert(Sec); } |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 115 | template <class ELFT> void writeHeader(FileOutputBuffer &Out) const; |
| 116 | void writeSegment(FileOutputBuffer &Out) const; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | class Section : public SectionBase { |
| 120 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 121 | ArrayRef<uint8_t> Contents; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 122 | |
| 123 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 124 | Section(ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 125 | |
| 126 | void writeSection(FileOutputBuffer &Out) const override; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 129 | // There are two types of string tables that can exist, dynamic and not dynamic. |
| 130 | // In the dynamic case the string table is allocated. Changing a dynamic string |
| 131 | // table would mean altering virtual addresses and thus the memory image. So |
| 132 | // dynamic string tables should not have an interface to modify them or |
| 133 | // reconstruct them. This type lets us reconstruct a string table. To avoid |
| 134 | // this class being used for dynamic string tables (which has happened) the |
| 135 | // classof method checks that the particular instance is not allocated. This |
| 136 | // then agrees with the makeSection method used to construct most sections. |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 137 | class StringTableSection : public SectionBase { |
| 138 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 139 | StringTableBuilder StrTabBuilder; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 140 | |
| 141 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 142 | StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) { |
| 143 | Type = ELF::SHT_STRTAB; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 146 | void addString(StringRef Name); |
| 147 | uint32_t findIndex(StringRef Name) const; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 148 | void finalize() override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 149 | void writeSection(FileOutputBuffer &Out) const override; |
| 150 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 151 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 152 | if (S->Flags & ELF::SHF_ALLOC) |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 153 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 154 | return S->Type == ELF::SHT_STRTAB; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 155 | } |
| 156 | }; |
| 157 | |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 158 | // Symbols have a st_shndx field that normally stores an index but occasionally |
| 159 | // stores a different special value. This enum keeps track of what the st_shndx |
| 160 | // field means. Most of the values are just copies of the special SHN_* values. |
| 161 | // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section. |
| 162 | enum SymbolShndxType { |
| 163 | SYMBOL_SIMPLE_INDEX = 0, |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 164 | SYMBOL_ABS = ELF::SHN_ABS, |
| 165 | SYMBOL_COMMON = ELF::SHN_COMMON, |
| 166 | SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON, |
| 167 | SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2, |
| 168 | SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4, |
| 169 | SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8, |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 172 | struct Symbol { |
| 173 | uint8_t Binding; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 174 | SectionBase *DefinedIn = nullptr; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 175 | SymbolShndxType ShndxType; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 176 | uint32_t Index; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 177 | StringRef Name; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 178 | uint32_t NameIndex; |
| 179 | uint64_t Size; |
| 180 | uint8_t Type; |
| 181 | uint64_t Value; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 182 | |
| 183 | uint16_t getShndx() const; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | class SymbolTableSection : public SectionBase { |
| 187 | protected: |
| 188 | std::vector<std::unique_ptr<Symbol>> Symbols; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 189 | StringTableSection *SymbolNames = nullptr; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 190 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 191 | using SymPtr = std::unique_ptr<Symbol>; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 192 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 193 | public: |
| 194 | void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 195 | void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type, |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 196 | SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx, |
| 197 | uint64_t Sz); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 198 | void addSymbolNames(); |
| 199 | const Symbol *getSymbolByIndex(uint32_t Index) const; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 200 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 201 | void initialize(SectionTableRef SecTable) override; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 202 | void finalize() override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 203 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 204 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 205 | return S->Type == ELF::SHT_SYMTAB; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 206 | } |
| 207 | }; |
| 208 | |
| 209 | // Only writeSection depends on the ELF type so we implement it in a subclass. |
| 210 | template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 211 | void writeSection(FileOutputBuffer &Out) const override; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 214 | struct Relocation { |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 215 | const Symbol *RelocSymbol = nullptr; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 216 | uint64_t Offset; |
| 217 | uint64_t Addend; |
| 218 | uint32_t Type; |
| 219 | }; |
| 220 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 221 | // All relocation sections denote relocations to apply to another section. |
| 222 | // However, some relocation sections use a dynamic symbol table and others use |
| 223 | // a regular symbol table. Because the types of the two symbol tables differ in |
| 224 | // our system (because they should behave differently) we can't uniformly |
| 225 | // represent all relocations with the same base class if we expose an interface |
| 226 | // that mentions the symbol table type. So we split the two base types into two |
| 227 | // different classes, one which handles the section the relocation is applied to |
| 228 | // and another which handles the symbol table type. The symbol table type is |
| 229 | // taken as a type parameter to the class (see RelocSectionWithSymtabBase). |
| 230 | class RelocationSectionBase : public SectionBase { |
| 231 | protected: |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 232 | SectionBase *SecToApplyRel = nullptr; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 233 | |
| 234 | public: |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 235 | const SectionBase *getSection() const { return SecToApplyRel; } |
Jake Ehrlich | c5ff727 | 2017-10-10 18:32:22 +0000 | [diff] [blame] | 236 | void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 237 | |
| 238 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 239 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 240 | } |
| 241 | }; |
| 242 | |
| 243 | // Takes the symbol table type to use as a parameter so that we can deduplicate |
| 244 | // that code between the two symbol table types. |
| 245 | template <class SymTabType> |
| 246 | class RelocSectionWithSymtabBase : public RelocationSectionBase { |
| 247 | private: |
| 248 | SymTabType *Symbols = nullptr; |
| 249 | |
| 250 | protected: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 251 | RelocSectionWithSymtabBase() = default; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 252 | |
| 253 | public: |
| 254 | void setSymTab(SymTabType *StrTab) { Symbols = StrTab; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 255 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 256 | void initialize(SectionTableRef SecTable) override; |
| 257 | void finalize() override; |
| 258 | }; |
| 259 | |
| 260 | template <class ELFT> |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 261 | class RelocationSection |
| 262 | : public RelocSectionWithSymtabBase<SymbolTableSection> { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 263 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 264 | using Elf_Rel = typename ELFT::Rel; |
| 265 | using Elf_Rela = typename ELFT::Rela; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 266 | |
| 267 | std::vector<Relocation> Relocations; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 268 | |
| 269 | template <class T> void writeRel(T *Buf) const; |
| 270 | |
| 271 | public: |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 272 | void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 273 | void writeSection(FileOutputBuffer &Out) const override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 274 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 275 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 276 | if (S->Flags & ELF::SHF_ALLOC) |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 277 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 278 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 279 | } |
| 280 | }; |
| 281 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 282 | class SectionWithStrTab : public Section { |
| 283 | private: |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 284 | const SectionBase *StrTab = nullptr; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 285 | |
| 286 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 287 | SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {} |
| 288 | |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 289 | void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 290 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 291 | void initialize(SectionTableRef SecTable) override; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 292 | void finalize() override; |
| 293 | static bool classof(const SectionBase *S); |
| 294 | }; |
| 295 | |
| 296 | class DynamicSymbolTableSection : public SectionWithStrTab { |
| 297 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 298 | DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} |
| 299 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 300 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 301 | return S->Type == ELF::SHT_DYNSYM; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 302 | } |
| 303 | }; |
| 304 | |
| 305 | class DynamicSection : public SectionWithStrTab { |
| 306 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 307 | DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} |
| 308 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 309 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 310 | return S->Type == ELF::SHT_DYNAMIC; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 311 | } |
| 312 | }; |
| 313 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 314 | class DynamicRelocationSection |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 315 | : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> { |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 316 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 317 | ArrayRef<uint8_t> Contents; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 318 | |
| 319 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 320 | DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 321 | |
| 322 | void writeSection(FileOutputBuffer &Out) const override; |
| 323 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 324 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 325 | if (!(S->Flags & ELF::SHF_ALLOC)) |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 326 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 327 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 328 | } |
| 329 | }; |
| 330 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 331 | template <class ELFT> class Object { |
| 332 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 333 | using SecPtr = std::unique_ptr<SectionBase>; |
| 334 | using SegPtr = std::unique_ptr<Segment>; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 335 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 336 | using Elf_Shdr = typename ELFT::Shdr; |
| 337 | using Elf_Ehdr = typename ELFT::Ehdr; |
| 338 | using Elf_Phdr = typename ELFT::Phdr; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 339 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 340 | void initSymbolTable(const object::ELFFile<ELFT> &ElfFile, |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 341 | SymbolTableSection *SymTab, SectionTableRef SecTable); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 342 | SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile, |
Petr Hosek | b1bb3e5 | 2017-08-04 05:33:44 +0000 | [diff] [blame] | 343 | const Elf_Shdr &Shdr); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 344 | void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile); |
| 345 | SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 346 | |
| 347 | protected: |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 348 | StringTableSection *SectionNames = nullptr; |
| 349 | SymbolTableSection *SymbolTable = nullptr; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 350 | std::vector<SecPtr> Sections; |
| 351 | std::vector<SegPtr> Segments; |
| 352 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 353 | void writeHeader(FileOutputBuffer &Out) const; |
| 354 | void writeProgramHeaders(FileOutputBuffer &Out) const; |
| 355 | void writeSectionData(FileOutputBuffer &Out) const; |
| 356 | void writeSectionHeaders(FileOutputBuffer &Out) const; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 357 | |
| 358 | public: |
| 359 | uint8_t Ident[16]; |
| 360 | uint64_t Entry; |
| 361 | uint64_t SHOffset; |
| 362 | uint32_t Type; |
| 363 | uint32_t Machine; |
| 364 | uint32_t Version; |
| 365 | uint32_t Flags; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 366 | bool WriteSectionHeaders = true; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 367 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 368 | Object(const object::ELFObjectFile<ELFT> &Obj); |
| 369 | virtual ~Object() = default; |
| 370 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 371 | void removeSections(std::function<bool(const SectionBase &)> ToRemove); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 372 | virtual size_t totalSize() const = 0; |
| 373 | virtual void finalize() = 0; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 374 | virtual void write(FileOutputBuffer &Out) const = 0; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 375 | }; |
| 376 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 377 | template <class ELFT> class ELFObject : public Object<ELFT> { |
| 378 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 379 | using SecPtr = std::unique_ptr<SectionBase>; |
| 380 | using SegPtr = std::unique_ptr<Segment>; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 381 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 382 | using Elf_Shdr = typename ELFT::Shdr; |
| 383 | using Elf_Ehdr = typename ELFT::Ehdr; |
| 384 | using Elf_Phdr = typename ELFT::Phdr; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 385 | |
| 386 | void sortSections(); |
| 387 | void assignOffsets(); |
| 388 | |
| 389 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 390 | ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} |
| 391 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 392 | void finalize() override; |
| 393 | size_t totalSize() const override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 394 | void write(FileOutputBuffer &Out) const override; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | template <class ELFT> class BinaryObject : public Object<ELFT> { |
| 398 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 399 | using SecPtr = std::unique_ptr<SectionBase>; |
| 400 | using SegPtr = std::unique_ptr<Segment>; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 401 | |
| 402 | uint64_t TotalSize; |
| 403 | |
| 404 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 405 | BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} |
| 406 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 407 | void finalize() override; |
| 408 | size_t totalSize() const override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 409 | void write(FileOutputBuffer &Out) const override; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 410 | }; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 411 | |
| 412 | } // end namespace llvm |
| 413 | |
| 414 | #endif // LLVM_TOOLS_OBJCOPY_OBJECT_H |