RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
elf.h
1#ifndef ELF_H
2#define ELF_H
3
4#include <sstream>
5#include <vector>
6#include "../common.h"
7#include "../linker/format.h"
8#include "../linker/linker.h"
9#include "../linker/module.h"
10#include "../linker/reader.h"
11
12namespace ELF
13{
19 class ELFFormat : public virtual Linker::InputFormat, public virtual Linker::OutputFormat, public Linker::LinkerManager
20 {
21 public:
22 void WriteFile(Linker::Writer& wr) override;
23
24 void SetupOptions(std::shared_ptr<Linker::OutputFormat> format) override;
25
26 private:
27 void GenerateModule(Linker::Module& module) const;
28
29 public:
30 void ProduceModule(Linker::Module& module, Linker::Reader& rd) override;
31
32 EndianType endiantype = ::LittleEndian;
33
34 ELFFormat()
35 {
36 }
37
38 bool option_16bit = true;
39 bool option_linear = false;
40
41 size_t wordbytes;
42
43 enum cpu_type
44 {
45 I386,
46 M68K,
47 ARM,
48 };
49 cpu_type cpu;
50
51 class Symbol
52 {
53 public:
54 uint32_t name_offset = 0;
55 std::string name;
56 offset_t value = 0, size = 0;
57 uint8_t bind = 0, type = 0, other = 0;
58 uint16_t shndx = 0;
59 uint16_t sh_link = 0;
60 bool defined = false;
61 bool unallocated = false;
62 Linker::Location location;
63 Linker::CommonSymbol specification;
64 };
65
66 class Section
67 {
68 public:
69 uint32_t name_offset = 0;
70 std::string name;
71 uint32_t type = 0, link = 0, info = 0;
72 offset_t flags = 0;
73 offset_t address = 0, file_offset = 0, size = 0, align = 0, entsize = 0;
74 std::shared_ptr<Linker::Section> section;
75 std::vector<Symbol> symbols;
76 };
77 std::vector<Section> sections;
78
80 {
81 public:
82 offset_t offset = 0;
83 uint32_t type = 0;
84 uint32_t symbol = 0;
85 int64_t addend = 0;
86 uint16_t sh_link = 0, sh_info = 0;
87 bool addend_from_section_data = false;
88 };
89 std::vector<Relocation> relocations;
90
91 static const uint8_t ELFCLASS32 = 1;
92 static const uint8_t ELFCLASS64 = 2;
93 static const uint8_t ELFDATA2LSB = 1;
94 static const uint8_t ELFDATA2MSB = 2;
95
96 static const uint16_t EM_386 = 3;
97 static const uint16_t EM_68K = 4;
98 static const uint16_t EM_ARM = 40;
99
100 static const uint32_t SHT_PROGBITS = 1;
101 static const uint32_t SHT_SYMTAB = 2;
102 static const uint32_t SHT_STRTAB = 3;
103 static const uint32_t SHT_RELA = 4;
104 static const uint32_t SHT_NOBITS = 8;
105 static const uint32_t SHT_REL = 9;
106 static const uint32_t SHT_GROUP = 10;
107
108 static const offset_t SHF_WRITE = 0x0001;
109 static const offset_t SHF_ALLOC = 0x0002;
110 static const offset_t SHF_EXECINSTR = 0x0004;
111 static const offset_t SHF_MERGE = 0x0010;
112 static const offset_t SHF_GROUP = 0x0200;
113
114 static const uint16_t SHN_UNDEF = 0;
115 static const uint16_t SHN_ABS = 0xFFF1;
116 static const uint16_t SHN_COMMON = 0xFFF2;
117 static const uint16_t SHN_XINDEX = 0xFFFF;
118
119 static const uint8_t STB_LOCAL = 0;
120 static const uint8_t STB_GLOBAL = 1;
121
122 static const offset_t R_386_8 = 22;
123 static const offset_t R_386_PC8 = 23;
124 static const offset_t R_386_16 = 20;
125 static const offset_t R_386_PC16 = 21;
126 static const offset_t R_386_32 = 1;
127 static const offset_t R_386_PC32 = 2;
128 /* extensions, see https://github.com/tkchia/build-ia16/blob/master/elf16-writeup.md (TODO: unsupported for now) */
129 static const offset_t R_386_SEG16 = 45;
130 static const offset_t R_386_SUB16 = 46;
131 static const offset_t R_386_SUB32 = 47;
132 static const offset_t R_386_SEGRELATIVE = 48;
133 static const offset_t R_386_OZSEG16 = 80;
134 static const offset_t R_386_OZRELSEG16 = 81;
135
136 static const offset_t R_68K_8 = 3;
137 static const offset_t R_68K_PC8 = 6;
138 static const offset_t R_68K_16 = 2;
139 static const offset_t R_68K_PC16 = 5;
140 static const offset_t R_68K_32 = 1;
141 static const offset_t R_68K_PC32 = 4;
142
143 static const offset_t R_ARM_ABS8 = 8;
144 static const offset_t R_ARM_ABS16 = 16;
145 static const offset_t R_ARM_ABS32 = 2;
146 static const offset_t R_ARM_REL32 = 3;
147 static const offset_t R_ARM_CALL = 28;
148 static const offset_t R_ARM_JUMP24 = 29;
149 static const offset_t R_ARM_PC24 = 1;
150 static const offset_t R_ARM_V4BX = 40;
151
152 void ReadFile(Linker::Reader& in) override;
153 };
154
155}
156
157#endif /* ELF_H */
Definition elf.h:80
Definition elf.h:67
Definition elf.h:52
ELF object and executable format.
Definition elf.h:20
void SetupOptions(std::shared_ptr< Linker::OutputFormat > format) override
Initializes the reader for linking purposes.
Definition elf.cc:11
void WriteFile(Linker::Writer &wr) override
Stores data in memory to file.
Definition elf.cc:6
void ReadFile(Linker::Reader &in) override
Loads file into memory.
Definition elf.cc:191
void ProduceModule(Linker::Module &module, Linker::Reader &rd) override
Reads a file and loads the information into a module object.
Definition elf.cc:185
Represents a currently unallocated variable that should be allocated in the final stages of the linki...
Definition symbol.h:224
A class that provides a general interface to loading a module.
Definition format.h:161
A helper class to collect sections into segments.
Definition linker.h:19
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:20
A class that provides a general interface to setting up generation for a format.
Definition format.h:56
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15