Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/elftoolchain/elfcopy/binary.c
39534 views
1
/*-
2
* Copyright (c) 2010,2011 Kai Wang
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
* SUCH DAMAGE.
25
*/
26
27
#include <sys/param.h>
28
#include <sys/stat.h>
29
#include <ctype.h>
30
#include <err.h>
31
#include <gelf.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <unistd.h>
36
37
#include "elfcopy.h"
38
39
ELFTC_VCSID("$Id: binary.c 3757 2019-06-28 01:15:28Z emaste $");
40
41
/*
42
* Convert ELF object to `binary'. Sections with SHF_ALLOC flag set
43
* are copied to the result binary. The relative offsets for each section
44
* are retained, so the result binary file might contain "holes".
45
*/
46
void
47
create_binary(int ifd, int ofd)
48
{
49
Elf *e;
50
Elf_Scn *scn;
51
Elf_Data *d;
52
Elf64_Addr baseaddr;
53
GElf_Shdr sh;
54
off_t baseoff, off;
55
int elferr;
56
57
if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
58
errx(EXIT_FAILURE, "elf_begin() failed: %s",
59
elf_errmsg(-1));
60
61
baseaddr = 0;
62
baseoff = 0;
63
if (lseek(ofd, baseoff, SEEK_SET) < 0)
64
err(EXIT_FAILURE, "lseek failed");
65
66
/*
67
* Find base offset in the first iteration.
68
*/
69
baseoff = -1;
70
scn = NULL;
71
while ((scn = elf_nextscn(e, scn)) != NULL) {
72
if (gelf_getshdr(scn, &sh) == NULL) {
73
warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
74
(void) elf_errno();
75
continue;
76
}
77
if ((sh.sh_flags & SHF_ALLOC) == 0 ||
78
sh.sh_type == SHT_NOBITS ||
79
sh.sh_size == 0)
80
continue;
81
if (baseoff == -1 || (off_t) sh.sh_offset < baseoff) {
82
baseoff = sh.sh_offset;
83
baseaddr = sh.sh_addr;
84
}
85
}
86
elferr = elf_errno();
87
if (elferr != 0)
88
warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
89
90
if (baseoff == -1)
91
return;
92
93
/*
94
* Write out sections in the second iteration.
95
*/
96
scn = NULL;
97
while ((scn = elf_nextscn(e, scn)) != NULL) {
98
if (gelf_getshdr(scn, &sh) == NULL) {
99
warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
100
(void) elf_errno();
101
continue;
102
}
103
if ((sh.sh_flags & SHF_ALLOC) == 0 ||
104
sh.sh_type == SHT_NOBITS ||
105
sh.sh_size == 0)
106
continue;
107
(void) elf_errno();
108
if ((d = elf_rawdata(scn, NULL)) == NULL) {
109
elferr = elf_errno();
110
if (elferr != 0)
111
warnx("elf_rawdata failed: %s", elf_errmsg(-1));
112
continue;
113
}
114
if (d->d_buf == NULL || d->d_size == 0)
115
continue;
116
117
/* lseek to section offset relative to `baseaddr'. */
118
off = sh.sh_addr - baseaddr;
119
if (lseek(ofd, off, SEEK_SET) < 0)
120
err(EXIT_FAILURE, "lseek failed");
121
122
/* Write out section contents. */
123
if (write(ofd, d->d_buf, d->d_size) != (ssize_t) d->d_size)
124
err(EXIT_FAILURE, "write failed");
125
}
126
elferr = elf_errno();
127
if (elferr != 0)
128
warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
129
}
130
131
#define _SYMBOL_NAMSZ 1024
132
133
/*
134
* Convert `binary' to ELF object. The input `binary' is converted to
135
* a relocatable (.o) file, a few symbols will also be created to make
136
* it easier to access the binary data in other compilation units.
137
*/
138
void
139
create_elf_from_binary(struct elfcopy *ecp, int ifd, const char *ifn)
140
{
141
char name[_SYMBOL_NAMSZ];
142
struct section *sec, *sec_temp, *shtab;
143
struct stat sb;
144
GElf_Ehdr oeh;
145
GElf_Shdr sh;
146
void *content;
147
uint64_t off, data_start, data_end, data_size;
148
char *sym_basename, *p;
149
150
/* Reset internal section list. */
151
if (!TAILQ_EMPTY(&ecp->v_sec))
152
TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) {
153
TAILQ_REMOVE(&ecp->v_sec, sec, sec_list);
154
free(sec);
155
}
156
157
if (fstat(ifd, &sb) == -1)
158
err(EXIT_FAILURE, "fstat failed");
159
160
/* Read the input binary file to a internal buffer. */
161
if ((content = malloc(sb.st_size)) == NULL)
162
err(EXIT_FAILURE, "malloc failed");
163
if (read(ifd, content, sb.st_size) != sb.st_size)
164
err(EXIT_FAILURE, "read failed");
165
166
/*
167
* TODO: copy the input binary to output binary verbatim if -O is not
168
* specified.
169
*/
170
171
/* Create EHDR for output .o file. */
172
if (gelf_newehdr(ecp->eout, ecp->oec) == NULL)
173
errx(EXIT_FAILURE, "gelf_newehdr failed: %s",
174
elf_errmsg(-1));
175
if (gelf_getehdr(ecp->eout, &oeh) == NULL)
176
errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
177
elf_errmsg(-1));
178
179
/* Initialise e_ident fields. */
180
oeh.e_ident[EI_CLASS] = ecp->oec;
181
oeh.e_ident[EI_DATA] = ecp->oed;
182
/*
183
* TODO: Set OSABI according to the OS platform where elfcopy(1)
184
* was build. (probably)
185
*/
186
oeh.e_ident[EI_OSABI] = ELFOSABI_NONE;
187
oeh.e_machine = ecp->oem;
188
oeh.e_type = ET_REL;
189
oeh.e_entry = 0;
190
191
ecp->flags |= RELOCATABLE;
192
193
/* Create .shstrtab section */
194
init_shstrtab(ecp);
195
ecp->shstrtab->off = 0;
196
197
/*
198
* Create `.data' section which contains the binary data. The
199
* section is inserted immediately after EHDR.
200
*/
201
off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT);
202
if (off == 0)
203
errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1));
204
(void) create_external_section(ecp, ".data", NULL, content, sb.st_size,
205
off, SHT_PROGBITS, ELF_T_BYTE, SHF_ALLOC | SHF_WRITE, 1, 0, 1);
206
207
/* Insert .shstrtab after .data section. */
208
if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL)
209
errx(EXIT_FAILURE, "elf_newscn failed: %s",
210
elf_errmsg(-1));
211
insert_to_sec_list(ecp, ecp->shstrtab, 1);
212
213
/* Insert section header table here. */
214
shtab = insert_shtab(ecp, 1);
215
216
/* Count in .symtab and .strtab section headers. */
217
shtab->sz += gelf_fsize(ecp->eout, ELF_T_SHDR, 2, EV_CURRENT);
218
219
if ((sym_basename = strdup(ifn)) == NULL)
220
err(1, "strdup");
221
for (p = sym_basename; *p != '\0'; p++)
222
if (!isalnum(*p & 0xFF))
223
*p = '_';
224
#define _GEN_SYMNAME(S) do { \
225
snprintf(name, sizeof(name), "%s%s%s", "_binary_", sym_basename, S); \
226
} while (0)
227
228
/*
229
* Create symbol table.
230
*/
231
create_external_symtab(ecp);
232
data_start = 0;
233
data_end = data_start + sb.st_size;
234
data_size = sb.st_size;
235
_GEN_SYMNAME("_start");
236
add_to_symtab(ecp, name, data_start, 0, 1,
237
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1);
238
_GEN_SYMNAME("_end");
239
add_to_symtab(ecp, name, data_end, 0, 1,
240
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1);
241
_GEN_SYMNAME("_size");
242
add_to_symtab(ecp, name, data_size, 0, SHN_ABS,
243
ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1);
244
finalize_external_symtab(ecp);
245
create_symtab_data(ecp);
246
#undef _GEN_SYMNAME
247
free(sym_basename);
248
249
/*
250
* Write the underlying ehdr. Note that it should be called
251
* before elf_setshstrndx() since it will overwrite e->e_shstrndx.
252
*/
253
if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
254
errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
255
elf_errmsg(-1));
256
257
/* Update sh_name pointer for each section header entry. */
258
ecp->flags |= SYMTAB_EXIST;
259
update_shdr(ecp, 0);
260
261
/* Properly set sh_link field of .symtab section. */
262
if (gelf_getshdr(ecp->symtab->os, &sh) == NULL)
263
errx(EXIT_FAILURE, "692 gelf_getshdr() failed: %s",
264
elf_errmsg(-1));
265
sh.sh_link = elf_ndxscn(ecp->strtab->os);
266
if (!gelf_update_shdr(ecp->symtab->os, &sh))
267
errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s",
268
elf_errmsg(-1));
269
270
/* Renew oeh to get the updated e_shstrndx. */
271
if (gelf_getehdr(ecp->eout, &oeh) == NULL)
272
errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
273
elf_errmsg(-1));
274
275
/* Resync section offsets. */
276
resync_sections(ecp);
277
278
/* Store SHDR offset in EHDR. */
279
oeh.e_shoff = shtab->off;
280
281
/* Update ehdr since we modified e_shoff. */
282
if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
283
errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
284
elf_errmsg(-1));
285
286
/* Write out the output elf object. */
287
if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
288
errx(EXIT_FAILURE, "elf_update() failed: %s",
289
elf_errmsg(-1));
290
291
/* Release allocated resource. */
292
free(content);
293
free_elf(ecp);
294
}
295
296