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