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;
107 static constexpr offset_t R_68K_8 = 3;
108 static constexpr offset_t R_68K_PC8 = 6;
109 static constexpr offset_t R_68K_16 = 2;
110 static constexpr offset_t R_68K_PC16 = 5;
111 static constexpr offset_t R_68K_32 = 1;
112 static constexpr offset_t R_68K_PC32 = 4;
114 static constexpr offset_t R_ARM_ABS8 = 8;
115 static constexpr offset_t R_ARM_ABS16 = 16;
116 static constexpr offset_t R_ARM_ABS32 = 2;
117 static constexpr offset_t R_ARM_REL32 = 3;
118 static constexpr offset_t R_ARM_CALL = 28;
119 static constexpr offset_t R_ARM_JUMP24 = 29;
120 static constexpr offset_t R_ARM_PC24 = 1;
121 static constexpr offset_t R_ARM_V4BX = 40;
123 static constexpr offset_t DT_NULL = 0;
124 static constexpr offset_t DT_NEEDED = 1;
125 static constexpr offset_t DT_PLTRELSZ = 2;
126 static constexpr offset_t DT_PLTGOT = 3;
127 static constexpr offset_t DT_HASH = 4;
128 static constexpr offset_t DT_STRTAB = 5;
129 static constexpr offset_t DT_SYMTAB = 6;
130 static constexpr offset_t DT_RELA = 7;
131 static constexpr offset_t DT_RELASZ = 8;
132 static constexpr offset_t DT_RELAENT = 9;
133 static constexpr offset_t DT_STRSZ = 10;
134 static constexpr offset_t DT_SYMENT = 11;
135 static constexpr offset_t DT_INIT = 12;
136 static constexpr offset_t DT_FINI = 13;
137 static constexpr offset_t DT_SONAME = 14;
138 static constexpr offset_t DT_RPATH = 15;
139 static constexpr offset_t DT_SYMBOLIC = 16;
140 static constexpr offset_t DT_REL = 17;
141 static constexpr offset_t DT_RELSZ = 18;
142 static constexpr offset_t DT_RELENT = 19;
143 static constexpr offset_t DT_PLTREL = 20;
144 static constexpr offset_t DT_DEBUG = 21;
145 static constexpr offset_t DT_TEXTREL = 22;
146 static constexpr offset_t DT_JMPREL = 23;
147 static constexpr offset_t DT_BIND_NOW = 24;
148 static constexpr offset_t DT_INIT_ARRAY = 25;
149 static constexpr offset_t DT_FINI_ARRAY = 26;
150 static constexpr offset_t DT_INIT_ARRAYSZ = 27;
151 static constexpr offset_t DT_FINI_ARRAYSZ = 28;
152 static constexpr offset_t DT_RUNPATH = 29;
153 static constexpr offset_t DT_FLAGS = 30;
154 static constexpr offset_t DT_ENCODING = 31;
155 static constexpr offset_t DT_PREINIT_ARRAY = 32;
156 static constexpr offset_t DT_PREINIT_ARRAYSZ = 33;
157 static constexpr offset_t DT_SYMTAB_SHNDX = 34;
158 static constexpr offset_t DT_RELRSZ = 35;
159 static constexpr offset_t DT_RELR = 36;
160 static constexpr offset_t DT_RELRENT = 37;
162 static constexpr offset_t DT_EXPORT = 0x60000001;
163 static constexpr offset_t DT_EXPORTSZ = 0x60000002;
164 static constexpr offset_t DT_EXPENT = 0x60000003;
165 static constexpr offset_t DT_IMPORT = 0x60000004;
166 static constexpr offset_t DT_IMPORTSZ = 0x60000005;
167 static constexpr offset_t DT_IMPENT = 0x60000006;
168 static constexpr offset_t DT_IT = 0x60000007;
169 static constexpr offset_t DT_ITPRTY = 0x60000008;
170 static constexpr offset_t DT_INITTERM = 0x60000009;
171 static constexpr offset_t DT_STACKSZ = 0x6000000A;
173 static constexpr offset_t DT_GNU_FLAGS1 = 0x6FFFFDF4;
174 static constexpr offset_t DT_GNU_PRELINKED = 0x6FFFFDF5;
175 static constexpr offset_t DT_GNU_CONFLICTSZ = 0x6FFFFDF6;
176 static constexpr offset_t DT_GNU_LIBLISTSZ = 0x6FFFFDF7;
177 static constexpr offset_t DT_CHECKSUM = 0x6FFFFDF8;
178 static constexpr offset_t DT_PLTPADSZ = 0x6FFFFDF9;
179 static constexpr offset_t DT_MOVEENT = 0x6FFFFDFA;
180 static constexpr offset_t DT_MOVESZ = 0x6FFFFDFB;
181 static constexpr offset_t DT_FEATURE = 0x6FFFFDFC;
182 static constexpr offset_t DT_POSTFLAG_1 = 0x6FFFFDFD;
183 static constexpr offset_t DT_SYMINSZ = 0x6FFFFDFE;
184 static constexpr offset_t DT_SYMINENT = 0x6FFFFDFF;
185 static constexpr offset_t DT_GNU_HASH = 0x6FFFFEF5;
186 static constexpr offset_t DT_TLSDESC_PLT = 0x6FFFFEF6;
187 static constexpr offset_t DT_TLSDESC_GOT = 0x6FFFFEF7;
188 static constexpr offset_t DT_GNU_CONFLICT = 0x6FFFFFEF8;
189 static constexpr offset_t DT_GNU_LIBLIST = 0x6FFFFEF9;
190 static constexpr offset_t DT_CONFIG = 0x6FFFFEFA;
191 static constexpr offset_t DT_DEPAUDIT = 0x6FFFFEFB;
192 static constexpr offset_t DT_AUDIT = 0x6FFFFEFC;
193 static constexpr offset_t DT_PLTPAD = 0x6FFFFEFD;
194 static constexpr offset_t DT_MOVETAB = 0x6FFFFEFE;
195 static constexpr offset_t DT_SYMINFO = 0x6FFFFEFF;
196 static constexpr offset_t DT_VERSYM = 0x6FFFFFF0;
197 static constexpr offset_t DT_RELACOUNT = 0x6FFFFFF9;
198 static constexpr offset_t DT_RELCOUNT = 0x6FFFFFFA;
199 static constexpr offset_t DT_FLAGS_1 = 0x6FFFFFFB;
200 static constexpr offset_t DT_VERDEF = 0x6FFFFFFC;
201 static constexpr offset_t DT_VERDEFNUM = 0x6FFFFFFD;
202 static constexpr offset_t DT_VERNEED = 0x6FFFFFFE;
203 static constexpr offset_t DT_VERNEEDNUM = 0x6FFFFFFF;
204 static constexpr offset_t DT_AUXILIARY = 0x7FFFFFFD;
205 static constexpr offset_t DT_USED = 0x7FFFFFFE;
206 static constexpr offset_t DT_FILTER = 0x7FFFFFFFF;
208 offset_t file_offset = 0;
210 uint8_t file_class = 0;
211 EndianType endiantype = ::LittleEndian;
213 uint8_t data_encoding = 0;
214 size_t wordbytes = 4;
300 EM_ARC_A5 = EM_ARC_COMPACT,
322 EM_ALTERA_NIOS32 = 113,
339 EM_LATTICEMICO32 = 138,
348 EM_CYPRESS_M8C = 161,
356 EM_ECOG1X = EM_ECOG1,
363 EM_MCST_ELBRUS = 175,
380 EM_CLOUDSHIELD = 192,
383 EM_ARC_COMPACT2 = 195,
407 EM_CSR_KALIMBA = 219,
420 EM_GRAPHCORE_IPU = 248,
425 EM_ARC_COMPACT3_64 = 253,
427 EM_ARC_COMPACT3 = 255,
442 cpu_type cpu = EM_NONE;
444 uint8_t header_version = 0;
446 uint8_t abi_version = 0;
456 file_type object_file_type = ET_NONE;
457 uint16_t file_version = 0;
460 offset_t program_header_offset = 0;
461 offset_t section_header_offset = 0;
463 uint16_t elf_header_size = 0;
464 uint16_t program_header_entry_size = 0;
465 uint16_t section_header_entry_size = 0;
466 uint16_t section_name_string_table = 0;
471 virtual void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const;
478 uint32_t name_offset = 0;
480 offset_t value = 0, size = 0;
481 uint8_t bind = 0, type = 0, other = 0;
483 uint32_t sh_link = 0;
484 bool defined =
false;
485 bool unallocated =
false;
496 std::vector<Symbol> symbols;
499 : wordbytes(wordbytes), entsize(entsize)
514 std::vector<std::string> strings;
532 std::vector<offset_t> array;
534 Array(offset_t entsize)
559 void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const override;
580 uint32_t sh_link = 0, sh_info = 0;
581 bool addend_from_section_data =
false;
583 size_t GetSize(cpu_type cpu)
const;
584 std::string GetName(cpu_type cpu)
const;
593 std::vector<Relocation> relocations;
596 : wordbytes(wordbytes), entsize(entsize)
609 std::vector<uint32_t> buckets;
610 std::vector<uint32_t> chains;
612 static uint32_t Hash(
const std::string& name);
618 void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const override;
634 : tag(tag), value(value)
645 std::vector<DynamicObject> dynamic;
648 : wordbytes(wordbytes), entsize(entsize)
662 std::string descriptor;
672 std::vector<Note> notes;
694 uint32_t name_offset = 0;
696 uint32_t offset_next_entry = 0;
699 uint16_t version = 0;
700 uint32_t file_name_offset = 0;
701 std::string file_name;
702 uint32_t offset_auxiliary_array = 0;
703 std::vector<Auxiliary> auxiliary_array;
704 uint32_t offset_next_entry = 0;
710 std::vector<VersionRequirement> requirements;
722 enum system_type : uint32_t
730 system_type os_type = EOS_NONE;
731 uint32_t os_size = 0;
732 bool IsOS2Specific()
const;
736 enum os2_session : uint8_t
743 os2_session sessiontype = OS2_SES_NONE;
744 uint8_t sessionflags;
747 std::vector<uint8_t> os_specific;
752 void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const override;
759 uint32_t ordinal = 0, name_offset = 0;
767 import_type type = IMP_IGNORED;
769 std::string dll_name;
777 std::vector<IBMImportEntry> imports;
794 uint32_t ordinal = 0, symbol_index = 0, name_offset = 0;
796 uint32_t sh_link = 0, sh_info = 0;
804 std::vector<IBMExportEntry> exports;
822 uint32_t ordinal = 0;
823 uint32_t name_offset = 0;
825 uint32_t data_offset = 0;
826 uint32_t data_size = 0;
827 std::shared_ptr<Linker::Image> data;
841 uint16_t version = 0;
843 uint32_t name_offset = 0;
845 uint32_t item_array_offset = 0;
846 uint32_t item_array_entry_size = 0;
847 uint32_t header_size = 0;
848 uint32_t string_table_offset = 0;
849 uint32_t locale_offset = 0;
851 char16_t country[2] = { };
852 char16_t language[2] = { };
854 std::vector<IBMResource> resources;
860 void AddDumperFields(std::unique_ptr<Dumper::Region>& region,
Dumper::Dumper& dump,
const ELFFormat& fmt,
unsigned index)
const override;
865 class IBMResourceFile
868 uint8_t file_class = 0;
869 EndianType endiantype = ::LittleEndian;
871 uint8_t data_encoding = 0;
872 size_t wordbytes = 4;
876 uint32_t header_size = 0;
877 uint32_t resource_collection_offset = 0;
878 std::vector<IBMResourceCollection> resource_collections;
885 uint32_t name_offset = 0;
903 SHT_PREINIT_ARRAY = 16,
905 SHT_SYMTAB_SHNDX = 18,
908 SHT_IMPORTS = 0x60000002,
909 SHT_EXPORTS = 0x60000003,
910 SHT_RES = 0x60000004,
913 SHT_OLD_IMPORTS = 13,
914 SHT_OLD_EXPORTS = 14,
918 SHT_GNU_INCREMENTAL_INPUTS = 0x6FFF4700,
919 SHT_GNU_ATTRIBUTES = 0x6FFFFFF5,
920 SHT_GNU_HASH = 0x6FFFFFF6,
921 SHT_GNU_LIBLIST = 0x6FFFFFF7,
922 SHT_SUNW_verdef = 0x6FFFFFFD,
923 SHT_SUNW_verneed = 0x6FFFFFFE,
924 SHT_SUNW_versym = 0x6FFFFFFF,
926 section_type type = SHT_NULL;
927 uint32_t link = 0, info = 0;
929 offset_t address = 0, file_offset = 0, size = 0, align = 0, entsize = 0;
931 std::shared_ptr<Linker::Image> contents;
933 std::shared_ptr<Linker::Section> GetSection();
934 const std::shared_ptr<Linker::Section> GetSection()
const;
936 std::shared_ptr<SymbolTable> GetSymbolTable();
937 const std::shared_ptr<SymbolTable> GetSymbolTable()
const;
940 std::shared_ptr<StringTable> GetStringTable();
942 std::shared_ptr<Array> GetArray();
944 std::shared_ptr<Relocations> GetRelocations();
945 const std::shared_ptr<Relocations> GetRelocations()
const;
947 std::shared_ptr<DynamicSection> GetDynamicSection();
950 std::shared_ptr<NotesSection> GetNotesSection();
952 std::shared_ptr<IBMSystemInfo> GetIBMSystemInfo();
955 std::shared_ptr<IBMImportTable> GetIBMImportTable();
956 std::shared_ptr<IBMExportTable> GetIBMExportTable();
958 bool GetFileSize()
const;
962 static std::shared_ptr<Linker::Section> ReadProgBits(
Linker::Reader& rd, offset_t file_offset,
const std::string& name, offset_t size);
963 static std::shared_ptr<Linker::Section> ReadNoBits(
const std::string& name, offset_t size);
964 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);
965 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);
966 static std::shared_ptr<StringTable> ReadStringTable(
Linker::Reader& rd, offset_t file_offset, offset_t section_size);
967 static std::shared_ptr<Array> ReadArray(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
968 static std::shared_ptr<SectionGroup> ReadSectionGroup(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
969 static std::shared_ptr<IndexArray> ReadIndexArray(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
970 static std::shared_ptr<HashTable> ReadHashTable(
Linker::Reader& rd, offset_t file_offset);
971 static std::shared_ptr<DynamicSection> ReadDynamic(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize,
size_t wordbytes);
972 static std::shared_ptr<NotesSection> ReadNote(
Linker::Reader& rd, offset_t file_offset, offset_t section_size);
973 static std::shared_ptr<VersionRequirements> ReadVersionRequirements(
Linker::Reader& rd, offset_t file_offset, offset_t section_link, offset_t section_info);
974 static std::shared_ptr<IBMSystemInfo> ReadIBMSystemInfo(
Linker::Reader& rd, offset_t file_offset);
975 static std::shared_ptr<IBMImportTable> ReadIBMImportTable(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
976 static std::shared_ptr<IBMExportTable> ReadIBMExportTable(
Linker::Reader& rd, offset_t file_offset, offset_t section_size, offset_t entsize);
977 static std::shared_ptr<IBMResourceCollection> ReadIBMResourceCollection(
Linker::Reader& rd, offset_t file_offset);
979 std::vector<Section> sections;
1001 PT_SUNW_EH_FRAME = 0x6474E550,
1002 PT_GNU_STACK = 0x6474E551,
1003 PT_GNU_RELRO = 0x6474E552,
1004 PT_GNU_PROPERTY = 0x6474E553,
1005 PT_GNU_SFRAME = 0x6474E554,
1006 PT_GNU_MBIND_LO = 0x6474E555,
1007 PT_GNU_MBIND_HI = 0x6474F554,
1008 PT_OPENBSD_MUTABLE = 0x65A3DBE5,
1009 PT_OPENBSD_RANDOMIZE = 0x65A3DBE6,
1010 PT_OPENBSD_WXNEEDED = 0x65A3DBE7,
1011 PT_OPENBSD_NOBTCFI = 0x65A3DBE8,
1012 PT_OPENBSD_BOOTDATA = 0x65A41BE6,
1016 offset_t offset = 0, vaddr = 0, paddr = 0, filesz = 0, memsz = 0, align = 0;
1031 offset_t offset, size;
1032 Part(
part_type type, uint16_t index, offset_t offset, offset_t size)
1033 : type(type), index(index), offset(offset), size(size)
1037 offset_t GetOffset(
const ELFFormat& fmt)
const;
1038 offset_t GetActualSize(
const ELFFormat& fmt)
const;
1041 std::vector<Part> parts;
1043 std::vector<Segment> segments;
1048 offset_t offset = 0;
1050 std::shared_ptr<Linker::Image> image;
1051 Block(offset_t offset = 0, offset_t size = 0)
1052 : offset(offset), size(size)
1068 offset_t unknown1, offset, size, unknown2;
1069 std::shared_ptr<Linker::Buffer> image;
1071 offset_t hobbit_beos_resource_offset = 0;
1072 std::vector<HobbitBeOSResource> hobbit_beos_resources;
1092 bool option_16bit =
true;
1093 bool option_linear =
false;
1095 void SetupOptions(std::shared_ptr<Linker::OutputFormat> format)
override;
1112 ELFFormat::cpu_type cpu = ELFFormat::EM_NONE;
1114 uint8_t abi_version = 0;
1115 uint8_t file_class = 0;
1116 uint8_t data_encoding = 0;
1117 uint64_t offset = 0;
1119 std::shared_ptr<Linker::Image> image;
1124 uint16_t version = ELFFormat::EV_CURRENT;
1125 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