7#include "../dumper/dumper.h"
8#include "../linker/buffer.h"
9#include "../linker/format.h"
10#include "../linker/module.h"
11#include "../linker/reader.h"
12#include "../linker/segment_manager.h"
26 static constexpr uint8_t EI_OSABI = 7;
27 static constexpr uint8_t EI_ABIVERSION = 8;
28 static constexpr uint8_t EI_CLASS = 4;
29 static constexpr uint8_t EI_DATA = 5;
31 static constexpr uint8_t ELFCLASSNONE = 0;
32 static constexpr uint8_t ELFCLASS32 = 1;
33 static constexpr uint8_t ELFCLASS64 = 2;
35 static constexpr uint8_t ELFDATANONE = 0;
36 static constexpr uint8_t ELFDATA2LSB = 1;
37 static constexpr uint8_t ELFDATA2MSB = 2;
39 static constexpr uint8_t EV_NONE = 0;
40 static constexpr uint8_t EV_CURRENT = 1;
42 static constexpr uint8_t ELFOSABI_NONE = 0;
44 static constexpr offset_t SHF_WRITE = 0x0001;
45 static constexpr offset_t SHF_ALLOC = 0x0002;
46 static constexpr offset_t SHF_EXECINSTR = 0x0004;
47 static constexpr offset_t SHF_MERGE = 0x0010;
48 static constexpr offset_t SHF_STRINGS = 0x0020;
49 static constexpr offset_t SHF_INFO_LINK = 0x0040;
50 static constexpr offset_t SHF_LINK_ORDER = 0x0080;
51 static constexpr offset_t SHF_OS_NONCONFORMING = 0x0100;
52 static constexpr offset_t SHF_GROUP = 0x0200;
53 static constexpr offset_t SHF_TLS = 0x0400;
54 static constexpr offset_t SHF_COMPRESSED = 0x0800;
56 static constexpr offset_t SHF_GNU_RETAIN = 0x00200000;
57 static constexpr offset_t SHF_GNU_MBIND = 0x01000000;
58 static constexpr offset_t SHF_EXCLUDE = 0x80000000;
60 static constexpr offset_t SHF_BEGIN = 0x01000000;
61 static constexpr offset_t SHF_END = 0x02000000;
63 static constexpr uint16_t SHN_UNDEF = 0;
64 static constexpr uint16_t SHN_LORESERVE = 0xFF00;
65 static constexpr uint16_t SHN_ABS = 0xFFF1;
66 static constexpr uint16_t SHN_COMMON = 0xFFF2;
67 static constexpr uint16_t SHN_XINDEX = 0xFFFF;
69 static constexpr uint8_t STB_LOCAL = 0;
70 static constexpr uint8_t STB_GLOBAL = 1;
71 static constexpr uint8_t STB_WEAK = 2;
72 static constexpr uint8_t STB_ENTRY = 12;
74 static constexpr uint8_t STT_NOTYPE = 0;
75 static constexpr uint8_t STT_OBJECT = 1;
76 static constexpr uint8_t STT_FUNC = 2;
77 static constexpr uint8_t STT_SECTION = 3;
78 static constexpr uint8_t STT_FILE = 4;
79 static constexpr uint8_t STT_COMMON = 5;
80 static constexpr uint8_t STT_TLS = 6;
81 static constexpr uint8_t STT_IMPORT = 11;
83 static constexpr uint8_t STV_DEFAULT = 0;
84 static constexpr uint8_t STV_INTERNAL = 1;
85 static constexpr uint8_t STV_HIDDEN = 2;
86 static constexpr uint8_t STV_PROTECTED = 3;
88 static constexpr offset_t R_386_8 = 22;
89 static constexpr offset_t R_386_PC8 = 23;
90 static constexpr offset_t R_386_16 = 20;
91 static constexpr offset_t R_386_PC16 = 21;
92 static constexpr offset_t R_386_32 = 1;
93 static constexpr offset_t R_386_PC32 = 2;
94 static constexpr offset_t R_386_GOT32 = 3;
95 static constexpr offset_t R_386_GOTPC = 10;
96 static constexpr offset_t R_386_GOTOFF = 9;
97 static constexpr offset_t R_386_PLT32 = 4;
111 static constexpr offset_t R_68K_8 = 3;
112 static constexpr offset_t R_68K_PC8 = 6;
113 static constexpr offset_t R_68K_16 = 2;
114 static constexpr offset_t R_68K_PC16 = 5;
115 static constexpr offset_t R_68K_32 = 1;
116 static constexpr offset_t R_68K_PC32 = 4;
117 static constexpr offset_t R_68K_GOT8 = 9;
118 static constexpr offset_t R_68K_GOT8O = 12;
119 static constexpr offset_t R_68K_GOT16 = 8;
120 static constexpr offset_t R_68K_GOT16O = 11;
121 static constexpr offset_t R_68K_GOT32 = 7;
122 static constexpr offset_t R_68K_GOT32O = 10;
123 static constexpr offset_t R_68K_PLT8 = 15;
124 static constexpr offset_t R_68K_PLT8O = 18;
125 static constexpr offset_t R_68K_PLT16 = 14;
126 static constexpr offset_t R_68K_PLT16O = 17;
127 static constexpr offset_t R_68K_PLT32 = 13;
128 static constexpr offset_t R_68K_PLT32O = 16;
130 static constexpr offset_t R_ARM_ABS8 = 8;
131 static constexpr offset_t R_ARM_ABS16 = 16;
132 static constexpr offset_t R_ARM_ABS32 = 2;
133 static constexpr offset_t R_ARM_REL32 = 3;
134 static constexpr offset_t R_ARM_CALL = 28;
135 static constexpr offset_t R_ARM_JUMP24 = 29;
136 static constexpr offset_t R_ARM_PC24 = 1;
137 static constexpr offset_t R_ARM_V4BX = 40;
139 static constexpr offset_t DT_NULL = 0;
140 static constexpr offset_t DT_NEEDED = 1;
141 static constexpr offset_t DT_PLTRELSZ = 2;
142 static constexpr offset_t DT_PLTGOT = 3;
143 static constexpr offset_t DT_HASH = 4;
144 static constexpr offset_t DT_STRTAB = 5;
145 static constexpr offset_t DT_SYMTAB = 6;
146 static constexpr offset_t DT_RELA = 7;
147 static constexpr offset_t DT_RELASZ = 8;
148 static constexpr offset_t DT_RELAENT = 9;
149 static constexpr offset_t DT_STRSZ = 10;
150 static constexpr offset_t DT_SYMENT = 11;
151 static constexpr offset_t DT_INIT = 12;
152 static constexpr offset_t DT_FINI = 13;
153 static constexpr offset_t DT_SONAME = 14;
154 static constexpr offset_t DT_RPATH = 15;
155 static constexpr offset_t DT_SYMBOLIC = 16;
156 static constexpr offset_t DT_REL = 17;
157 static constexpr offset_t DT_RELSZ = 18;
158 static constexpr offset_t DT_RELENT = 19;
159 static constexpr offset_t DT_PLTREL = 20;
160 static constexpr offset_t DT_DEBUG = 21;
161 static constexpr offset_t DT_TEXTREL = 22;
162 static constexpr offset_t DT_JMPREL = 23;
163 static constexpr offset_t DT_BIND_NOW = 24;
164 static constexpr offset_t DT_INIT_ARRAY = 25;
165 static constexpr offset_t DT_FINI_ARRAY = 26;
166 static constexpr offset_t DT_INIT_ARRAYSZ = 27;
167 static constexpr offset_t DT_FINI_ARRAYSZ = 28;
168 static constexpr offset_t DT_RUNPATH = 29;
169 static constexpr offset_t DT_FLAGS = 30;
170 static constexpr offset_t DT_ENCODING = 31;
171 static constexpr offset_t DT_PREINIT_ARRAY = 32;
172 static constexpr offset_t DT_PREINIT_ARRAYSZ = 33;
173 static constexpr offset_t DT_SYMTAB_SHNDX = 34;
174 static constexpr offset_t DT_RELRSZ = 35;
175 static constexpr offset_t DT_RELR = 36;
176 static constexpr offset_t DT_RELRENT = 37;
178 static constexpr offset_t DT_EXPORT = 0x60000001;
179 static constexpr offset_t DT_EXPORTSZ = 0x60000002;
180 static constexpr offset_t DT_EXPENT = 0x60000003;
181 static constexpr offset_t DT_IMPORT = 0x60000004;
182 static constexpr offset_t DT_IMPORTSZ = 0x60000005;
183 static constexpr offset_t DT_IMPENT = 0x60000006;
184 static constexpr offset_t DT_IT = 0x60000007;
185 static constexpr offset_t DT_ITPRTY = 0x60000008;
186 static constexpr offset_t DT_INITTERM = 0x60000009;
187 static constexpr offset_t DT_STACKSZ = 0x6000000A;
189 static constexpr offset_t DT_GNU_FLAGS1 = 0x6FFFFDF4;
190 static constexpr offset_t DT_GNU_PRELINKED = 0x6FFFFDF5;
191 static constexpr offset_t DT_GNU_CONFLICTSZ = 0x6FFFFDF6;
192 static constexpr offset_t DT_GNU_LIBLISTSZ = 0x6FFFFDF7;
193 static constexpr offset_t DT_CHECKSUM = 0x6FFFFDF8;
194 static constexpr offset_t DT_PLTPADSZ = 0x6FFFFDF9;
195 static constexpr offset_t DT_MOVEENT = 0x6FFFFDFA;
196 static constexpr offset_t DT_MOVESZ = 0x6FFFFDFB;
197 static constexpr offset_t DT_FEATURE = 0x6FFFFDFC;
198 static constexpr offset_t DT_POSTFLAG_1 = 0x6FFFFDFD;
199 static constexpr offset_t DT_SYMINSZ = 0x6FFFFDFE;
200 static constexpr offset_t DT_SYMINENT = 0x6FFFFDFF;
201 static constexpr offset_t DT_GNU_HASH = 0x6FFFFEF5;
202 static constexpr offset_t DT_TLSDESC_PLT = 0x6FFFFEF6;
203 static constexpr offset_t DT_TLSDESC_GOT = 0x6FFFFEF7;
204 static constexpr offset_t DT_GNU_CONFLICT = 0x6FFFFFEF8;
205 static constexpr offset_t DT_GNU_LIBLIST = 0x6FFFFEF9;
206 static constexpr offset_t DT_CONFIG = 0x6FFFFEFA;
207 static constexpr offset_t DT_DEPAUDIT = 0x6FFFFEFB;
208 static constexpr offset_t DT_AUDIT = 0x6FFFFEFC;
209 static constexpr offset_t DT_PLTPAD = 0x6FFFFEFD;
210 static constexpr offset_t DT_MOVETAB = 0x6FFFFEFE;
211 static constexpr offset_t DT_SYMINFO = 0x6FFFFEFF;
212 static constexpr offset_t DT_VERSYM = 0x6FFFFFF0;
213 static constexpr offset_t DT_RELACOUNT = 0x6FFFFFF9;
214 static constexpr offset_t DT_RELCOUNT = 0x6FFFFFFA;
215 static constexpr offset_t DT_FLAGS_1 = 0x6FFFFFFB;
216 static constexpr offset_t DT_VERDEF = 0x6FFFFFFC;
217 static constexpr offset_t DT_VERDEFNUM = 0x6FFFFFFD;
218 static constexpr offset_t DT_VERNEED = 0x6FFFFFFE;
219 static constexpr offset_t DT_VERNEEDNUM = 0x6FFFFFFF;
220 static constexpr offset_t DT_AUXILIARY = 0x7FFFFFFD;
221 static constexpr offset_t DT_USED = 0x7FFFFFFE;
222 static constexpr offset_t DT_FILTER = 0x7FFFFFFFF;
224 offset_t file_offset = 0;
226 uint8_t file_class = 0;
227 EndianType endiantype = ::LittleEndian;
229 uint8_t data_encoding = 0;
230 size_t wordbytes = 4;
316 EM_ARC_A5 = EM_ARC_COMPACT,
338 EM_ALTERA_NIOS32 = 113,
355 EM_LATTICEMICO32 = 138,
364 EM_CYPRESS_M8C = 161,
372 EM_ECOG1X = EM_ECOG1,
379 EM_MCST_ELBRUS = 175,
396 EM_CLOUDSHIELD = 192,
399 EM_ARC_COMPACT2 = 195,
423 EM_CSR_KALIMBA = 219,
436 EM_GRAPHCORE_IPU = 248,
441 EM_ARC_COMPACT3_64 = 253,
443 EM_ARC_COMPACT3 = 255,
458 cpu_type cpu = EM_NONE;
460 uint8_t header_version = 0;
462 uint8_t abi_version = 0;
472 file_type object_file_type = ET_NONE;
473 uint16_t file_version = 0;
476 offset_t program_header_offset = 0;
477 offset_t section_header_offset = 0;
479 uint16_t elf_header_size = 0;
480 uint16_t program_header_entry_size = 0;
481 uint16_t section_header_entry_size = 0;
482 uint16_t section_name_string_table = 0;
487 virtual void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const;
494 uint32_t name_offset = 0;
496 offset_t value = 0, size = 0;
497 uint8_t bind = 0, type = 0, other = 0;
499 uint32_t sh_link = 0;
500 bool defined =
false;
501 bool unallocated =
false;
512 std::vector<Symbol> symbols;
515 : wordbytes(wordbytes), entsize(entsize)
530 std::vector<std::string> strings;
548 std::vector<offset_t> array;
550 Array(offset_t entsize)
575 void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const override;
596 uint32_t sh_link = 0, sh_info = 0;
597 bool addend_from_section_data =
false;
599 size_t GetSize(cpu_type cpu)
const;
600 std::string GetName(cpu_type cpu)
const;
609 std::vector<Relocation> relocations;
612 : wordbytes(wordbytes), entsize(entsize)
625 std::vector<uint32_t> buckets;
626 std::vector<uint32_t> chains;
628 static uint32_t Hash(
const std::string& name);
634 void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const override;
650 : tag(tag), value(value)
661 std::vector<DynamicObject> dynamic;
664 : wordbytes(wordbytes), entsize(entsize)
678 std::string descriptor;
688 std::vector<Note> notes;
710 uint32_t name_offset = 0;
712 uint32_t offset_next_entry = 0;
715 uint16_t version = 0;
716 uint32_t file_name_offset = 0;
717 std::string file_name;
718 uint32_t offset_auxiliary_array = 0;
719 std::vector<Auxiliary> auxiliary_array;
720 uint32_t offset_next_entry = 0;
726 std::vector<VersionRequirement> requirements;
738 enum system_type : uint32_t
746 system_type os_type = EOS_NONE;
747 uint32_t os_size = 0;
748 bool IsOS2Specific()
const;
752 enum os2_session : uint8_t
759 os2_session sessiontype = OS2_SES_NONE;
760 uint8_t sessionflags;
763 std::vector<uint8_t> os_specific;
768 void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const override;
775 uint32_t ordinal = 0, name_offset = 0;
783 import_type type = IMP_IGNORED;
785 std::string dll_name;
793 std::vector<IBMImportEntry> imports;
810 uint32_t ordinal = 0, symbol_index = 0, name_offset = 0;
812 uint32_t sh_link = 0, sh_info = 0;
820 std::vector<IBMExportEntry> exports;
838 uint32_t ordinal = 0;
839 uint32_t name_offset = 0;
841 uint32_t data_offset = 0;
842 uint32_t data_size = 0;
843 std::shared_ptr<Linker::Image> data;
857 uint16_t version = 0;
859 uint32_t name_offset = 0;
861 uint32_t item_array_offset = 0;
862 uint32_t item_array_entry_size = 0;
863 uint32_t header_size = 0;
864 uint32_t string_table_offset = 0;
865 uint32_t locale_offset = 0;
867 char16_t country[2] = { };
868 char16_t language[2] = { };
870 std::vector<IBMResource> resources;
876 void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const override;
881 class IBMResourceFile
884 uint8_t file_class = 0;
885 EndianType endiantype = ::LittleEndian;
887 uint8_t data_encoding = 0;
888 size_t wordbytes = 4;
892 uint32_t header_size = 0;
893 uint32_t resource_collection_offset = 0;
894 std::vector<IBMResourceCollection> resource_collections;
901 uint32_t name_offset = 0;
919 SHT_PREINIT_ARRAY = 16,
921 SHT_SYMTAB_SHNDX = 18,
924 SHT_IMPORTS = 0x60000002,
925 SHT_EXPORTS = 0x60000003,
926 SHT_RES = 0x60000004,
929 SHT_OLD_IMPORTS = 13,
930 SHT_OLD_EXPORTS = 14,
934 SHT_GNU_INCREMENTAL_INPUTS = 0x6FFF4700,
935 SHT_GNU_ATTRIBUTES = 0x6FFFFFF5,
936 SHT_GNU_HASH = 0x6FFFFFF6,
937 SHT_GNU_LIBLIST = 0x6FFFFFF7,
938 SHT_SUNW_verdef = 0x6FFFFFFD,
939 SHT_SUNW_verneed = 0x6FFFFFFE,
940 SHT_SUNW_versym = 0x6FFFFFFF,
942 section_type type = SHT_NULL;
943 uint32_t link = 0, info = 0;
945 offset_t address = 0, file_offset = 0, size = 0, align = 0, entsize = 0;
947 std::shared_ptr<Linker::Image> contents;
949 std::shared_ptr<Linker::Section> GetSection();
950 const std::shared_ptr<Linker::Section> GetSection()
const;
952 std::shared_ptr<SymbolTable> GetSymbolTable();
953 const std::shared_ptr<SymbolTable> GetSymbolTable()
const;
956 std::shared_ptr<StringTable> GetStringTable();
958 std::shared_ptr<Array> GetArray();
960 std::shared_ptr<Relocations> GetRelocations();
961 const std::shared_ptr<Relocations> GetRelocations()
const;
963 std::shared_ptr<DynamicSection> GetDynamicSection();
966 std::shared_ptr<NotesSection> GetNotesSection();
968 std::shared_ptr<IBMSystemInfo> GetIBMSystemInfo();
971 std::shared_ptr<IBMImportTable> GetIBMImportTable();
972 std::shared_ptr<IBMExportTable> GetIBMExportTable();
974 bool GetFileSize()
const;
978 static std::shared_ptr<Linker::Section> ReadProgBits(
Linker::Reader& rd, offset_t file_offset,
const std::string& name, offset_t size);
979 static std::shared_ptr<Linker::Section> ReadNoBits(
const std::string& name, offset_t size);
980 static std::shared_ptr<SymbolTable> ReadSymbolTable(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize, uint32_t section_link,
size_t wordbytes);
981 static std::shared_ptr<Relocations> ReadRelocations(
Linker::Reader& rd, Section::section_type type, offset_t file_offset, offset_t section_size, offset_t entsize, uint32_t section_link, uint32_t section_info,
size_t wordbytes);
982 static std::shared_ptr<StringTable> ReadStringTable(
Linker::Reader& rd, offset_t file_offset, offset_t section_size);
983 static std::shared_ptr<Array> ReadArray(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
984 static std::shared_ptr<SectionGroup> ReadSectionGroup(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
985 static std::shared_ptr<IndexArray> ReadIndexArray(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
986 static std::shared_ptr<HashTable> ReadHashTable(
Linker::Reader& rd, offset_t file_offset);
987 static std::shared_ptr<DynamicSection> ReadDynamic(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize,
size_t wordbytes);
988 static std::shared_ptr<NotesSection> ReadNote(
Linker::Reader& rd, offset_t file_offset, offset_t section_size);
989 static std::shared_ptr<VersionRequirements> ReadVersionRequirements(
Linker::Reader& rd, offset_t file_offset, offset_t section_link, offset_t section_info);
990 static std::shared_ptr<IBMSystemInfo> ReadIBMSystemInfo(
Linker::Reader& rd, offset_t file_offset);
991 static std::shared_ptr<IBMImportTable> ReadIBMImportTable(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
992 static std::shared_ptr<IBMExportTable> ReadIBMExportTable(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
993 static std::shared_ptr<IBMResourceCollection> ReadIBMResourceCollection(
Linker::Reader& rd, offset_t file_offset);
995 std::vector<Section> sections;
1012 PT_RES = 0x60000002,
1017 PT_SUNW_EH_FRAME = 0x6474E550,
1018 PT_GNU_STACK = 0x6474E551,
1019 PT_GNU_RELRO = 0x6474E552,
1020 PT_GNU_PROPERTY = 0x6474E553,
1021 PT_GNU_SFRAME = 0x6474E554,
1022 PT_GNU_MBIND_LO = 0x6474E555,
1023 PT_GNU_MBIND_HI = 0x6474F554,
1024 PT_OPENBSD_MUTABLE = 0x65A3DBE5,
1025 PT_OPENBSD_RANDOMIZE = 0x65A3DBE6,
1026 PT_OPENBSD_WXNEEDED = 0x65A3DBE7,
1027 PT_OPENBSD_NOBTCFI = 0x65A3DBE8,
1028 PT_OPENBSD_BOOTDATA = 0x65A41BE6,
1032 offset_t offset = 0, vaddr = 0, paddr = 0, filesz = 0, memsz = 0, align = 0;
1047 offset_t offset, size;
1048 Part(
part_type type, uint16_t index, offset_t offset, offset_t size)
1049 : type(type), index(index), offset(offset), size(size)
1053 offset_t GetOffset(
const ELFFormat& fmt)
const;
1054 offset_t GetActualSize(
const ELFFormat& fmt)
const;
1057 std::vector<Part> parts;
1059 std::vector<Segment> segments;
1064 offset_t offset = 0;
1066 std::shared_ptr<Linker::Image> image;
1067 Block(offset_t offset = 0, offset_t size = 0)
1068 : offset(offset), size(size)
1084 offset_t unknown1, offset, size, unknown2;
1085 std::shared_ptr<Linker::Buffer> image;
1087 offset_t hobbit_beos_resource_offset = 0;
1088 std::vector<HobbitBeOSResource> hobbit_beos_resources;
1108 bool option_16bit =
true;
1109 bool option_linear =
false;
1115 void SetupOptions(std::shared_ptr<Linker::OutputFormat> format)
override;
1132 ELFFormat::cpu_type cpu = ELFFormat::EM_NONE;
1134 uint8_t abi_version = 0;
1135 uint8_t file_class = 0;
1136 uint8_t data_encoding = 0;
1137 uint64_t offset = 0;
1139 std::shared_ptr<Linker::Image> image;
1144 uint16_t version = ELFFormat::EV_CURRENT;
1145 std::vector<Record> records;
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:586
Represents an abstract data image whose data can be written to a file.
Definition image.h:17
virtual offset_t WriteFile(Writer &wr, offset_t count, offset_t offset=0) const =0
Writes data of non-zero filled sections.
Represents a single offset within a section, or an absolute location in memory if the section is null...
Definition location.h:17
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
A helper class to collect sections into segments.
Definition segment_manager.h:32
Represents a symbol definition.
Definition symbol_definition.h:16
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15