-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoff.cpp
More file actions
174 lines (154 loc) · 4.63 KB
/
coff.cpp
File metadata and controls
174 lines (154 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "coff.hpp"
void generate_coff(std::ofstream &f, uint64_t bss_size) {
uint64_t strtab_size = 4;
for (auto &s : extern_labels) {
if (s.size() > 8)
strtab_size += s.size() + 1;
}
for (auto &s : labels) {
if (s.first.size() > 8)
strtab_size += s.first.size() + 1;
}
const size_t data_size = data_buffer.size();
const size_t rodata_size = rodata_buffer.size();
// COFF header
coff_header chdr;
chdr.sections = 1 + !!data_size + !!rodata_size + !!bss_size;
chdr.timestamp = (uint32_t)time(NULL);
chdr.symtab_off = sizeof(chdr) + chdr.sections * sizeof(coff_section_header); // coff hdr + section headers
chdr.num_symbols = extern_labels.size() + labels.size();
f.write((const char *)&chdr, sizeof(coff_header));
// section headers
// text section
coff_section_header shdr;
memcpy(shdr.name, ".text\0\0", 8);
shdr.vsize = text_buffer.size();
shdr.size = text_buffer.size();
shdr.offset = chdr.symtab_off + chdr.num_symbols * sizeof(coff_symbol) + strtab_size;
shdr.reloc_off = relocations.size() ? shdr.offset + shdr.size : 0;
shdr.num_relocs = relocations.size();
shdr.flags = 0x60500020; // code, execute, read, align 16
f.write((const char *)&shdr, sizeof(shdr));
size_t next_offset = (shdr.offset + shdr.size + shdr.num_relocs * sizeof(coff_relocation));
// data section
if (data_size) {
memcpy(shdr.name, ".data\0\0", 8);
shdr.vsize = data_size;
shdr.size = data_size;
shdr.offset = next_offset;
shdr.reloc_off = 0;
shdr.num_relocs = 0;
shdr.flags = 0xc0300040; // initialized data, read, write, align 4
f.write((const char *)&shdr, sizeof(shdr));
next_offset = shdr.offset + shdr.size;
}
// rodata section
if (rodata_size) {
memcpy(shdr.name, ".rodata", 8);
shdr.vsize = rodata_size;
shdr.size = rodata_size;
shdr.offset = next_offset;
shdr.reloc_off = 0;
shdr.num_relocs = 0;
shdr.flags = 0x40300040; // initialized data, read, align 4
f.write((const char *)&shdr, sizeof(shdr));
next_offset = shdr.offset + shdr.size;
}
// bss section
if (bss_size) {
memcpy(shdr.name, ".bss\0\0\0", 8);
shdr.vsize = bss_size;
shdr.size = 0;
shdr.offset = 0;
shdr.reloc_off = 0;
shdr.num_relocs = 0;
shdr.flags = 0xc0300080; // uninitialized data, read, write, align 4
f.write((const char *)&shdr, sizeof(shdr));
}
// symbol table
coff_symbol sym;
uint32_t i = 4;
std::vector<std::string> ordered_syms;
for (auto &l : labels) {
if (l.first.size() <= 8) {
memset(sym.name, 0, 8);
memcpy(sym.name, l.first.data(), l.first.size());
} else {
memset(sym.name, 0, 4);
memcpy(sym.name + 4, (const char *)&i, 4);
i += l.first.size() + 1;
}
sym.val = l.second.second;
if (l.second.first == TEXT)
sym.storage_class = 2; // external
else
sym.storage_class = 3; // static
if (l.second.first == TEXT)
sym.section = 1;
else if (l.second.first == DATA)
sym.section = 2;
else if (l.second.first == RODATA)
sym.section = 2 + !!data_size;
else
sym.section = 2 + !!data_size + !!rodata_size;
ordered_syms.push_back(l.first);
f.write((const char *)&sym, sizeof(sym));
}
sym.storage_class = 5; // external
for (auto &l : extern_labels) {
if (l.size() <= 8) {
memset(sym.name, 0, 8);
memcpy(sym.name, l.data(), l.size());
} else {
memset(sym.name, 0, 4);
memcpy(sym.name + 4, (const char *)&i, 4);
i += l.size() + 1;
}
sym.val = 0;
ordered_syms.push_back(l);
f.write((const char *)&sym, sizeof(sym));
}
// string table
f.write((const char *)&strtab_size, 4);
for (auto &l : ordered_syms) {
if (l.size() <= 8)
continue;
f.write(l.data(), l.size() + 1);
}
// relocation addends
for (auto &r : relocations) {
if (r.type == REL)
r.addend += 4;
if (r.size == 8) {
text_buffer[r.offset] = r.addend;
} else if (r.size == 16) {
*(int16_t *)(text_buffer.data() + r.offset) = r.addend;
} else if (r.size == 32) {
*(int32_t *)(text_buffer.data() + r.offset) = r.addend;
} else {
*(int64_t *)(text_buffer.data() + r.offset) = r.addend;
}
}
// text
f.write((const char *)text_buffer.data(), text_buffer.size());
// relocation table
coff_relocation rel;
for (auto &r : relocations) {
rel.vaddr = r.offset;
rel.sym = find(ordered_syms.begin(), ordered_syms.end(), r.symbol) - ordered_syms.begin();
if (r.type == ABS) {
rel.type = 2;
} else if (r.type == REL) {
rel.type = 4;
} else {
std::cerr << "avertissement : impossible de créer un réadressage vers PLT dans un fichier COFF" << std::endl;
rel.type = 0;
}
f.write((const char *)&rel, sizeof(rel));
}
// data
f.write((const char *)data_buffer.data(), data_size);
// rodata
f.write((const char *)rodata_buffer.data(), rodata_size);
f.close();
}