summaryrefslogtreecommitdiffstats
path: root/usr.sbin/elf2exe/elf2exe.c
blob: d0959eb1400c0a7bee49dd241170b9b8439abf7a (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*-
 * Copyright (c) 1999 Stefan Esser
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Make an ARC firmware executable from an ELF file.
 */

#ifndef lint
static const char rcsid[] =
  "$FreeBSD$";
#endif /* not lint */

#include <sys/types.h>
#include <sys/elf64.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define ALPHA_FMAGIC	0x184

#define TOTHSZ		0x200

typedef struct filehdr {
    u_int16_t		f_magic;
    u_int16_t		f_nscns;
    u_int32_t		f_timdat;
    u_int32_t		f_symptr;
    u_int32_t		f_nsyms;
    u_int16_t		f_opthdr;
    u_int16_t		f_flags;
} FILHDR;
#define FILHSZ	20

#define ALPHA_AMAGIC	0407

typedef struct aouthdr {
    u_int16_t		a_magic;
    u_int16_t		a_vstamp;
    u_int32_t		a_tsize;
    u_int32_t		a_dsize;
    u_int32_t		a_bsize;
    u_int32_t		a_entry;
    u_int32_t		a_text_start;
    u_int32_t		a_data_start;
    u_int32_t		a_bss_start;
    u_int32_t		a_gprmask;
    u_int32_t		a_cprmask[4];
    u_int32_t		a_gp_value;
} AOUTHDR;
#define AOUTSZ	56

typedef struct scnhdr {
    char		s_name[8];
    u_int32_t		s_fill;
    u_int32_t		s_vaddr;
    u_int32_t		s_size;
    u_int32_t		s_scnptr;
    u_int32_t		s_relptr;
    u_int32_t		s_lnnoptr;
    u_int16_t		s_nreloc;
    u_int16_t		s_nlnno;
    u_int32_t		s_flags;
} SCNHDR;
#define SCNHSZ	40

#define ROUNDUP(a,b)	((((a) -1) | ((b) -1)) +1)

/*
 * initialization subroutines
 */

int 
open_elffile(char *filename)
{
	int fileno = open(filename, O_RDONLY);

	if (fileno < 0)
		err(1, "%s", filename);
	return (fileno);
}


Elf64_Ehdr *
load_ehdr(int fileno)
{
	Elf64_Ehdr *ehdr;
	size_t bytes = sizeof(*ehdr);

	ehdr = malloc(bytes);
	if (ehdr) {
		lseek(fileno, 0, SEEK_SET);
		if (read(fileno, ehdr, bytes) != bytes)
			errx(1, "file truncated (ehdr)");
	}
	return (ehdr);
}

Elf64_Phdr *
load_phdr(int fileno, Elf64_Ehdr *ehdr)
{
	size_t bytes = ehdr->e_phentsize * ehdr->e_phnum;
	Elf64_Phdr *phdr = malloc(bytes);

	if (phdr) {
		lseek(fileno, ehdr->e_phoff, SEEK_SET);
		if (read(fileno, phdr, bytes) != bytes)
			errx(1, "file truncated (phdr)");
	}
	return (phdr);
}

Elf64_Shdr *
load_shdr(int fileno, Elf64_Ehdr *ehdr)
{
	size_t bytes = ehdr->e_shentsize * ehdr->e_shnum;
	Elf64_Shdr *shdr = malloc(bytes);

	if (shdr) {
		lseek(fileno, ehdr->e_shoff, SEEK_SET);
		if (read(fileno, shdr, bytes) != bytes)
			errx(1, "file truncated (shdr)");
	}
	return (shdr);
}

char *
find_shstrtable(int fileno, int sections, Elf64_Shdr *shdr)
{
	size_t bytes;
	char *shstrtab = NULL;
	int i, shstrtabindex;

	for (i = 0; shstrtab == NULL && i < sections; i++) {
		if (shdr[i].sh_type == 3 && shdr[i].sh_flags == 0) {
			shstrtabindex = i;

			bytes = shdr[shstrtabindex].sh_size;
			shstrtab = malloc(bytes);
			if (shstrtab == NULL)
				errx(1, "malloc failed");
			lseek(fileno, shdr[shstrtabindex].sh_offset, SEEK_SET);
			read(fileno, shstrtab, bytes);

			if (strcmp (shstrtab + shdr[i].sh_name, ".shstrtab")) {
				free(shstrtab);
				shstrtab = NULL;
			}
		}
	}
	return (shstrtab);
}

int
open_exefile(char *filename)
{
	int fileno = open(filename, O_RDWR | O_TRUNC | O_CREAT, 0666);

	if (fileno < 0)
		err(1, "%s", filename);
	return (fileno);
}

/*
 * utility subroutines 
 */

static char *shstrtab;

char *
section_name(Elf64_Shdr *shdr, int i)
{
	return (shstrtab + shdr[i].sh_name);
}

int
section_index(Elf64_Shdr *shdr, int sections, char *name)
{
	int i;

	for (i = 0; i < sections; i++)
		if (strcmp (name, section_name(shdr, i)) == 0)
			return (i);
	return (-1);
}

/* first byte of section */
u_int64_t
section_start(Elf64_Shdr *shdr, int sections, char *name)
{
	int i = section_index(shdr, sections, name);

	if (i < 0)
		return (-1);
	return (shdr[i].sh_addr);
}

/* last byte of section */
u_int64_t
section_end(Elf64_Shdr *shdr, int sections, char *name)
{
	int i = section_index(shdr, sections, name);

	if (i < 0)
		return (-1);
	return (shdr[i].sh_addr + shdr[i].sh_size -1);
}

/* last byte of section */
u_int64_t
section_size(Elf64_Shdr *shdr, int sections, char *name)
{
	int i = section_index(shdr, sections, name);

	if (i < 0)
		return (-1);

	return (shdr[i].sh_size);
}

/* file position of section start */
u_int64_t
section_fpos(Elf64_Shdr *shdr, int sections, char *name)
{
	int i = section_index(shdr, sections, name);

	if (i < 0)
		return (-1);
	return (shdr[i].sh_offset);
}

void
usage(void)
{
	fprintf(stderr, "usage: elf2exe infile outfile\n");
	exit(1);
}

int
main(int argc, char** argv)
{
	int infd, outfd, i;
	Elf64_Ehdr *ehdr;
	Elf64_Phdr *phdr;
	Elf64_Shdr *shdr;
	FILHDR filehdr;
	AOUTHDR aouthdr;
	SCNHDR textscn, datascn;
	u_int64_t textstart, textsize, textsize2, textfsize, textfpos;
	u_int64_t datastart, datasize, datafsize, datafpos;
	u_int64_t bssstart, bsssize;
	u_int64_t progentry;
	char* p;
	int sections;

	if (argc != 3)
		usage();

	infd = open_elffile(argv[1]);
	ehdr = load_ehdr(infd);

	if (ehdr == NULL)
		errx(1, "cannot read Elf Header\n");

	sections = ehdr->e_shnum;
	progentry = ehdr->e_entry;

	phdr = load_phdr(infd, ehdr);
	shdr = load_shdr(infd, ehdr);
	outfd = open_exefile(argv[2]);

	shstrtab = find_shstrtable(infd, sections, shdr);

	for (i = 1; i < sections; i++) {
		printf("section %d (%s): "
		    "type=%x flags=0%llx "
		    "offs=%llx size=%llx addr=%llx\n",
		    i, shstrtab + shdr[i].sh_name,
		    shdr[i].sh_type, (long long)shdr[i].sh_flags,
		    (long long)shdr[i].sh_offset, (long long)shdr[i].sh_size,
		    (long long)shdr[i].sh_addr);
	}

	textstart = section_start(shdr, sections, ".text");
	textsize  = section_size(shdr, sections,  ".text");
	textsize2 = section_end(shdr, sections,  ".rodata") - textstart +1;
	if (textsize < textsize2)
		textsize = textsize2;
	textfsize = ROUNDUP(textsize, 512);
	textfpos  = section_fpos(shdr, sections, ".text");

	datastart = section_start(shdr, sections, ".data");
	datasize  = section_start(shdr, sections, ".bss") - datastart;
	datafsize = ROUNDUP(datasize, 512);
	datafpos  = section_fpos(shdr, sections, ".data");

	bssstart  = section_start(shdr, sections, ".bss");
	bsssize   = section_size(shdr, sections, ".bss");

	printf("text: %llx(%llx) @%llx  data: %llx(%llx) @%llx  "
	    "bss: %llx(%llx)\n",
	    (long long)textstart, (long long)textsize, (long long)textfpos,
	    (long long)datastart, (long long)datasize, (long long)datafpos,
	    (long long)bssstart, (long long)bsssize);

	memset(&filehdr, 0, sizeof filehdr);
	memset(&aouthdr, 0, sizeof aouthdr);
	memset(&textscn, 0, sizeof textscn);
	memset(&datascn, 0, sizeof datascn);

	filehdr.f_magic = ALPHA_FMAGIC;
	filehdr.f_nscns = 2;
	filehdr.f_timdat = time(0);
	filehdr.f_symptr = 0;
	filehdr.f_nsyms = 0;
	filehdr.f_opthdr = AOUTSZ;
	filehdr.f_flags = 0x010f;

	aouthdr.a_magic = ALPHA_AMAGIC;
	aouthdr.a_vstamp = 0x5004;
	aouthdr.a_tsize = textfsize;
	aouthdr.a_dsize = datafsize;
	aouthdr.a_bsize = bsssize;
	aouthdr.a_entry = progentry;
	aouthdr.a_text_start = textstart;
	aouthdr.a_data_start = datastart;
	aouthdr.a_bss_start = bssstart;

	strcpy(textscn.s_name, ".text");
	textscn.s_fill = textsize;
	textscn.s_vaddr = textstart;
	textscn.s_size = textfsize;
	textscn.s_scnptr = 0x200;
	textscn.s_relptr = 0;
	textscn.s_lnnoptr = 0;
	textscn.s_nreloc = 0;
	textscn.s_nlnno = 0;
	textscn.s_flags = 0x20;

	strcpy(datascn.s_name, ".data");
	datascn.s_fill = datasize;
	datascn.s_vaddr = datastart;
	datascn.s_size = datafsize;
	datascn.s_scnptr = 0x200 + textfsize;
	datascn.s_relptr = 0;
	datascn.s_lnnoptr = 0;
	datascn.s_nreloc = 0;
	datascn.s_nlnno = 0;
	datascn.s_flags = 0x40;

	write(outfd, &filehdr, FILHSZ);
	write(outfd, &aouthdr, AOUTSZ);
	write(outfd, &textscn, SCNHSZ);
	write(outfd, &datascn, SCNHSZ);

	lseek(outfd, textscn.s_scnptr, SEEK_SET);
	p = malloc(ROUNDUP(textsize, 512));
	if (p == NULL)
		errx(1, "malloc failed");
	memset(p, 0, ROUNDUP(textsize, 512));
	lseek(infd, textfpos, SEEK_SET);
	read(infd, p, textsize);
	write(outfd, p, ROUNDUP(textsize, 512));
	free(p);

	lseek(outfd, datascn.s_scnptr, SEEK_SET);
	p = malloc(ROUNDUP(datasize, 512));
	if (p == NULL)
		errx(1, "malloc failed");
	memset(p, 0, ROUNDUP(datasize, 512));
	lseek(infd, datafpos, SEEK_SET);
	read(infd, p, datasize);
	write(outfd, p, ROUNDUP(datasize, 512));
	free(p);

	return (0);
}
OpenPOWER on IntegriCloud