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 | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 129 | class OwnedDataSection : public SectionBase { |
| 130 | private: |
| 131 | std::vector<uint8_t> Data; |
| 132 | |
| 133 | public: |
| 134 | OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data) |
| 135 | : Data(std::begin(Data), std::end(Data)) { |
| 136 | Name = SecName; |
| 137 | Type = ELF::SHT_PROGBITS; |
| 138 | Size = Data.size(); |
| 139 | } |
| 140 | void writeSection(FileOutputBuffer &Out) const override; |
| 141 | }; |
| 142 | |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 143 | // There are two types of string tables that can exist, dynamic and not dynamic. |
| 144 | // In the dynamic case the string table is allocated. Changing a dynamic string |
| 145 | // table would mean altering virtual addresses and thus the memory image. So |
| 146 | // dynamic string tables should not have an interface to modify them or |
| 147 | // reconstruct them. This type lets us reconstruct a string table. To avoid |
| 148 | // this class being used for dynamic string tables (which has happened) the |
| 149 | // classof method checks that the particular instance is not allocated. This |
| 150 | // then agrees with the makeSection method used to construct most sections. |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 151 | class StringTableSection : public SectionBase { |
| 152 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 153 | StringTableBuilder StrTabBuilder; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 154 | |
| 155 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 156 | StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) { |
| 157 | Type = ELF::SHT_STRTAB; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 160 | void addString(StringRef Name); |
| 161 | uint32_t findIndex(StringRef Name) const; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 162 | void finalize() override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 163 | void writeSection(FileOutputBuffer &Out) const override; |
| 164 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 165 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 166 | if (S->Flags & ELF::SHF_ALLOC) |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 167 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 168 | return S->Type == ELF::SHT_STRTAB; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 169 | } |
| 170 | }; |
| 171 | |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 172 | // Symbols have a st_shndx field that normally stores an index but occasionally |
| 173 | // stores a different special value. This enum keeps track of what the st_shndx |
| 174 | // field means. Most of the values are just copies of the special SHN_* values. |
| 175 | // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section. |
| 176 | enum SymbolShndxType { |
| 177 | SYMBOL_SIMPLE_INDEX = 0, |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 178 | SYMBOL_ABS = ELF::SHN_ABS, |
| 179 | SYMBOL_COMMON = ELF::SHN_COMMON, |
| 180 | SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON, |
| 181 | SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2, |
| 182 | SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4, |
| 183 | SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8, |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 186 | struct Symbol { |
| 187 | uint8_t Binding; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 188 | SectionBase *DefinedIn = nullptr; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 189 | SymbolShndxType ShndxType; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 190 | uint32_t Index; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 191 | StringRef Name; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 192 | uint32_t NameIndex; |
| 193 | uint64_t Size; |
| 194 | uint8_t Type; |
| 195 | uint64_t Value; |
Jake Ehrlich | 30d927a | 2018-01-02 23:01:24 +0000 | [diff] [blame] | 196 | uint8_t Visibility; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 197 | |
| 198 | uint16_t getShndx() const; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | class SymbolTableSection : public SectionBase { |
| 202 | protected: |
| 203 | std::vector<std::unique_ptr<Symbol>> Symbols; |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 204 | StringTableSection *SymbolNames = nullptr; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 205 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 206 | using SymPtr = std::unique_ptr<Symbol>; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 207 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 208 | public: |
| 209 | void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 210 | void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type, |
Jake Ehrlich | 30d927a | 2018-01-02 23:01:24 +0000 | [diff] [blame] | 211 | SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility, |
| 212 | uint16_t Shndx, uint64_t Sz); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 213 | void addSymbolNames(); |
Jake Ehrlich | ef3b80c | 2017-11-30 20:14:53 +0000 | [diff] [blame] | 214 | const SectionBase *getStrTab() const { return SymbolNames; } |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 215 | const Symbol *getSymbolByIndex(uint32_t Index) const; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 216 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame^] | 217 | void localize(std::function<bool(const Symbol &)> ToLocalize); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 218 | void initialize(SectionTableRef SecTable) override; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 219 | void finalize() override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 220 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 221 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 222 | return S->Type == ELF::SHT_SYMTAB; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 223 | } |
| 224 | }; |
| 225 | |
| 226 | // Only writeSection depends on the ELF type so we implement it in a subclass. |
| 227 | template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 228 | void writeSection(FileOutputBuffer &Out) const override; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 231 | struct Relocation { |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 232 | const Symbol *RelocSymbol = nullptr; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 233 | uint64_t Offset; |
| 234 | uint64_t Addend; |
| 235 | uint32_t Type; |
| 236 | }; |
| 237 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 238 | // All relocation sections denote relocations to apply to another section. |
| 239 | // However, some relocation sections use a dynamic symbol table and others use |
| 240 | // a regular symbol table. Because the types of the two symbol tables differ in |
| 241 | // our system (because they should behave differently) we can't uniformly |
| 242 | // represent all relocations with the same base class if we expose an interface |
| 243 | // that mentions the symbol table type. So we split the two base types into two |
| 244 | // different classes, one which handles the section the relocation is applied to |
| 245 | // and another which handles the symbol table type. The symbol table type is |
| 246 | // taken as a type parameter to the class (see RelocSectionWithSymtabBase). |
| 247 | class RelocationSectionBase : public SectionBase { |
| 248 | protected: |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 249 | SectionBase *SecToApplyRel = nullptr; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 250 | |
| 251 | public: |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 252 | const SectionBase *getSection() const { return SecToApplyRel; } |
Jake Ehrlich | c5ff727 | 2017-10-10 18:32:22 +0000 | [diff] [blame] | 253 | void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 254 | |
| 255 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 256 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 257 | } |
| 258 | }; |
| 259 | |
| 260 | // Takes the symbol table type to use as a parameter so that we can deduplicate |
| 261 | // that code between the two symbol table types. |
| 262 | template <class SymTabType> |
| 263 | class RelocSectionWithSymtabBase : public RelocationSectionBase { |
| 264 | private: |
| 265 | SymTabType *Symbols = nullptr; |
| 266 | |
| 267 | protected: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 268 | RelocSectionWithSymtabBase() = default; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 269 | |
| 270 | public: |
| 271 | void setSymTab(SymTabType *StrTab) { Symbols = StrTab; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 272 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 273 | void initialize(SectionTableRef SecTable) override; |
| 274 | void finalize() override; |
| 275 | }; |
| 276 | |
| 277 | template <class ELFT> |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 278 | class RelocationSection |
| 279 | : public RelocSectionWithSymtabBase<SymbolTableSection> { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 280 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 281 | using Elf_Rel = typename ELFT::Rel; |
| 282 | using Elf_Rela = typename ELFT::Rela; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 283 | |
| 284 | std::vector<Relocation> Relocations; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 285 | |
| 286 | template <class T> void writeRel(T *Buf) const; |
| 287 | |
| 288 | public: |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 289 | void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 290 | void writeSection(FileOutputBuffer &Out) const override; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 291 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 292 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 293 | if (S->Flags & ELF::SHF_ALLOC) |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 294 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 295 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 296 | } |
| 297 | }; |
| 298 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 299 | class SectionWithStrTab : public Section { |
| 300 | private: |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 301 | const SectionBase *StrTab = nullptr; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 302 | |
| 303 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 304 | SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {} |
| 305 | |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 306 | void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 307 | void removeSectionReferences(const SectionBase *Sec) override; |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 308 | void initialize(SectionTableRef SecTable) override; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 309 | void finalize() override; |
| 310 | static bool classof(const SectionBase *S); |
| 311 | }; |
| 312 | |
| 313 | class DynamicSymbolTableSection : public SectionWithStrTab { |
| 314 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 315 | DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} |
| 316 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 317 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 318 | return S->Type == ELF::SHT_DYNSYM; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 319 | } |
| 320 | }; |
| 321 | |
| 322 | class DynamicSection : public SectionWithStrTab { |
| 323 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 324 | DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {} |
| 325 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 326 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 327 | return S->Type == ELF::SHT_DYNAMIC; |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 328 | } |
| 329 | }; |
| 330 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 331 | class DynamicRelocationSection |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 332 | : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> { |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 333 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 334 | ArrayRef<uint8_t> Contents; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 335 | |
| 336 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 337 | DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {} |
| 338 | |
| 339 | void writeSection(FileOutputBuffer &Out) const override; |
| 340 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 341 | static bool classof(const SectionBase *S) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 342 | if (!(S->Flags & ELF::SHF_ALLOC)) |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 343 | return false; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 344 | return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 345 | } |
| 346 | }; |
| 347 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 348 | template <class ELFT> class Object { |
| 349 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 350 | using SecPtr = std::unique_ptr<SectionBase>; |
| 351 | using SegPtr = std::unique_ptr<Segment>; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 352 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 353 | using Elf_Shdr = typename ELFT::Shdr; |
| 354 | using Elf_Ehdr = typename ELFT::Ehdr; |
| 355 | using Elf_Phdr = typename ELFT::Phdr; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 356 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 357 | void initSymbolTable(const object::ELFFile<ELFT> &ElfFile, |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 358 | SymbolTableSection *SymTab, SectionTableRef SecTable); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 359 | SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile, |
Petr Hosek | b1bb3e5 | 2017-08-04 05:33:44 +0000 | [diff] [blame] | 360 | const Elf_Shdr &Shdr); |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 361 | void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile); |
| 362 | SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 363 | |
| 364 | protected: |
Jake Ehrlich | ed95fce | 2017-09-27 00:44:00 +0000 | [diff] [blame] | 365 | StringTableSection *SectionNames = nullptr; |
| 366 | SymbolTableSection *SymbolTable = nullptr; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 367 | std::vector<SecPtr> Sections; |
| 368 | std::vector<SegPtr> Segments; |
| 369 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 370 | void writeHeader(FileOutputBuffer &Out) const; |
| 371 | void writeProgramHeaders(FileOutputBuffer &Out) const; |
| 372 | void writeSectionData(FileOutputBuffer &Out) const; |
| 373 | void writeSectionHeaders(FileOutputBuffer &Out) const; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 374 | |
| 375 | public: |
| 376 | uint8_t Ident[16]; |
| 377 | uint64_t Entry; |
| 378 | uint64_t SHOffset; |
| 379 | uint32_t Type; |
| 380 | uint32_t Machine; |
| 381 | uint32_t Version; |
| 382 | uint32_t Flags; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 383 | bool WriteSectionHeaders = true; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 384 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 385 | Object(const object::ELFObjectFile<ELFT> &Obj); |
| 386 | virtual ~Object() = default; |
| 387 | |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame^] | 388 | SymbolTableSection *getSymTab() const { return SymbolTable; } |
Jake Ehrlich | 5de70d9 | 2017-11-03 18:58:41 +0000 | [diff] [blame] | 389 | const SectionBase *getSectionHeaderStrTab() const { return SectionNames; } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 390 | void removeSections(std::function<bool(const SectionBase &)> ToRemove); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 391 | void addSection(StringRef SecName, ArrayRef<uint8_t> Data); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 392 | virtual size_t totalSize() const = 0; |
| 393 | virtual void finalize() = 0; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 394 | virtual void write(FileOutputBuffer &Out) const = 0; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 397 | template <class ELFT> class ELFObject : 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 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 402 | using Elf_Shdr = typename ELFT::Shdr; |
| 403 | using Elf_Ehdr = typename ELFT::Ehdr; |
| 404 | using Elf_Phdr = typename ELFT::Phdr; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 405 | |
| 406 | void sortSections(); |
| 407 | void assignOffsets(); |
| 408 | |
| 409 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 410 | ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} |
| 411 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 412 | void finalize() override; |
| 413 | size_t totalSize() const override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 414 | void write(FileOutputBuffer &Out) const override; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | template <class ELFT> class BinaryObject : public Object<ELFT> { |
| 418 | private: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 419 | using SecPtr = std::unique_ptr<SectionBase>; |
| 420 | using SegPtr = std::unique_ptr<Segment>; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 421 | |
| 422 | uint64_t TotalSize; |
| 423 | |
| 424 | public: |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 425 | BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {} |
| 426 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 427 | void finalize() override; |
| 428 | size_t totalSize() const override; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 429 | void write(FileOutputBuffer &Out) const override; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 430 | }; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 431 | |
| 432 | } // end namespace llvm |
| 433 | |
| 434 | #endif // LLVM_TOOLS_OBJCOPY_OBJECT_H |