/
fatelf-utils.c
947 lines (797 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
/**
* FatELF; support multiple ELF binaries in one file.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
/* code shared between all FatELF utilities... */
11
12
13
#define FATELF_UTILS 1
#include "fatelf-utils.h"
14
15
16
17
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
18
const char *unlink_on_xfail = NULL;
19
20
static uint8_t zerobuf[4096];
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
#ifndef APPID
#define APPID fatelf
#endif
#ifndef APPREV
#define APPREV "???"
#endif
#if (defined __GNUC__)
# define VERSTR2(x) #x
# define VERSTR(x) VERSTR2(x)
# define COMPILERVER " " VERSTR(__GNUC__) "." VERSTR(__GNUC_MINOR__) "." VERSTR(__GNUC_PATCHLEVEL__)
#elif (defined __SUNPRO_C)
# define VERSTR2(x) #x
# define VERSTR(x) VERSTR2(x)
# define COMPILERVER " " VERSTR(__SUNPRO_C)
#elif (defined __VERSION__)
# define COMPILERVER " " __VERSION__
#else
# define COMPILERVER ""
#endif
#ifndef __DATE__
#define __DATE__ "(Unknown build date)"
#endif
#ifndef __TIME__
#define __TIME__ "(Unknown build time)"
#endif
#ifndef COMPILER
#if (defined __GNUC__)
#define COMPILER "GCC"
#elif (defined _MSC_VER)
#define COMPILER "Visual Studio"
#elif (defined __SUNPRO_C)
#define COMPILER "Sun Studio"
#else
#error Please define your platform.
#endif
#endif
// macro mess so we can turn APPID and APPREV into a string literal...
#define MAKEBUILDVERSTRINGLITERAL2(id, rev) \
#id ", revision " rev ", built " __DATE__ " " __TIME__ \
", by " COMPILER COMPILERVER
#define MAKEBUILDVERSTRINGLITERAL(id, rev) MAKEBUILDVERSTRINGLITERAL2(id, rev)
const char *fatelf_build_version = MAKEBUILDVERSTRINGLITERAL(APPID, APPREV);
75
// Report an error to stderr and terminate immediately with exit(1).
76
void xfail(const char *fmt, ...)
77
78
79
80
81
82
83
84
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
fflush(stderr);
85
86
87
if (unlink_on_xfail != NULL)
unlink(unlink_on_xfail); // don't care if this fails.
unlink_on_xfail = NULL;
88
89
exit(1);
90
} // xfail
91
92
93
// Wrap malloc() with an xfail(), so this returns memory or calls exit().
94
// Memory is guaranteed to be initialized to zero.
95
void *xmalloc(const size_t len)
96
97
98
{
void *retval = calloc(1, len);
if (retval == NULL)
99
xfail("Out of memory!");
100
101
102
103
return retval;
} // xmalloc
104
105
106
107
108
109
110
111
112
// Allocate a copy of (str), xfail() on allocation failure.
char *xstrdup(const char *str)
{
char *retval = (char *) xmalloc(strlen(str) + 1);
strcpy(retval, str);
return retval;
} // xstrdup
113
114
// xfail() on error.
int xopen(const char *fname, const int flags, const int perms)
115
116
117
{
const int retval = open(fname, flags, perms);
if (retval == -1)
118
xfail("Failed to open '%s': %s", fname, strerror(errno));
119
120
121
122
return retval;
} // xopen
123
124
125
// xfail() on error, handle EINTR.
ssize_t xread(const char *fname, const int fd, void *buf,
const size_t len, const int must_read)
126
127
128
129
{
ssize_t rc;
while (((rc = read(fd,buf,len)) == -1) && (errno == EINTR)) { /* spin */ }
if ( (rc == -1) || ((must_read) && (rc != len)) )
130
xfail("Failed to read '%s': %s", fname, strerror(errno));
131
132
133
134
return rc;
} // xread
135
136
137
// xfail() on error, handle EINTR.
ssize_t xwrite(const char *fname, const int fd,
const void *buf, const size_t len)
138
139
140
141
{
ssize_t rc;
while (((rc = write(fd,buf,len)) == -1) && (errno == EINTR)) { /* spin */ }
if (rc == -1)
142
xfail("Failed to write '%s': %s", fname, strerror(errno));
143
144
145
return rc;
} // xwrite
146
147
// xfail() on error, handle EINTR.
void xwrite_zeros(const char *fname, const int fd, size_t len)
148
149
150
151
152
153
154
155
156
{
while (len > 0)
{
const size_t count = (len < sizeof (zerobuf)) ? len : sizeof (zerobuf);
xwrite(fname, fd, zerobuf, count);
len -= count;
} // while
} // xwrite_zeros
157
158
// xfail() on error, handle EINTR.
void xclose(const char *fname, const int fd)
159
160
161
162
{
int rc;
while ( ((rc = close(fd)) == -1) && (errno == EINTR) ) { /* spin. */ }
if (rc == -1)
163
xfail("Failed to close '%s': %s", fname, strerror(errno));
164
165
166
} // xopen
167
168
169
// xfail() on error.
void xlseek(const char *fname, const int fd,
const off_t offset, const int whence)
170
171
{
if (lseek(fd, offset, whence) == -1)
172
xfail("Failed to seek in '%s': %s", fname, strerror(errno));
173
174
175
} // xlseek
176
177
178
179
180
181
182
183
184
uint64_t xget_file_size(const char *fname, const int fd)
{
struct stat statbuf;
if (fstat(fd, &statbuf) == -1)
xfail("Failed to fstat '%s': %s", fname, strerror(errno));
return (uint64_t) statbuf.st_size;
} // xget_file_size
185
186
static uint8_t copybuf[256 * 1024];
187
188
189
// xfail() on error.
uint64_t xcopyfile(const char *in, const int infd,
const char *out, const int outfd)
190
191
192
193
{
uint64_t retval = 0;
ssize_t rc = 0;
xlseek(in, infd, 0, SEEK_SET);
194
while ( (rc = xread(in, infd, copybuf, sizeof (copybuf), 0)) > 0 )
195
{
196
xwrite(out, outfd, copybuf, rc);
197
198
199
200
201
202
203
retval += (uint64_t) rc;
} // while
return retval;
} // xcopyfile
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
static inline uint64_t minui64(const uint64_t a, const uint64_t b)
{
return (a < b) ? a : b;
} // minui64
void xcopyfile_range(const char *in, const int infd,
const char *out, const int outfd,
const uint64_t offset, const uint64_t size)
{
uint64_t remaining = size;
xlseek(in, infd, (off_t) offset, SEEK_SET);
while (remaining)
{
const size_t cpysize = minui64(remaining, sizeof (copybuf));
xread(in, infd, copybuf, cpysize, 1);
xwrite(out, outfd, copybuf, cpysize);
remaining -= (uint64_t) cpysize;
} // while
} // xcopyfile_range
226
227
void xread_elf_header(const char *fname, const int fd, const uint64_t offset,
FATELF_record *record)
228
229
230
{
const uint8_t magic[4] = { 0x7F, 0x45, 0x4C, 0x46 };
uint8_t buf[20]; // we only care about the first 20 bytes.
231
xlseek(fname, fd, offset, SEEK_SET);
232
233
xread(fname, fd, buf, sizeof (buf), 1);
if (memcmp(magic, buf, sizeof (magic)) != 0)
234
xfail("'%s' is not an ELF binary", fname);
235
236
237
record->osabi = buf[7];
record->osabi_version = buf[8];
238
239
record->word_size = buf[4];
record->byte_order = buf[5];
240
241
record->reserved0 = 0;
record->reserved1 = 0;
242
243
record->offset = 0;
record->size = 0;
244
245
246
247
248
249
250
251
if ((record->word_size != FATELF_32BITS) &&
(record->word_size != FATELF_64BITS))
{
xfail("Unexpected word size (%d) in '%s'", record->word_size, fname);
} // if
if (record->byte_order == FATELF_BIGENDIAN)
252
record->machine = (((uint16_t)buf[18]) << 8) | (((uint16_t)buf[19]));
253
else if (record->byte_order == FATELF_LITTLEENDIAN)
254
record->machine = (((uint16_t)buf[19]) << 8) | (((uint16_t)buf[18]));
255
else
256
257
258
259
{
xfail("Unexpected byte order (%d) in '%s'",
(int) record->byte_order, fname);
} // else
260
261
262
} // xread_elf_header
263
size_t fatelf_header_size(const int bincount)
264
{
265
return (sizeof (FATELF_header) + (sizeof (FATELF_record) * bincount));
266
267
268
} // fatelf_header_size
269
270
271
272
// Write a uint8_t to a buffer.
static inline uint8_t *putui8(uint8_t *ptr, const uint8_t val)
{
*(ptr++) = val;
273
return ptr;
274
275
276
} // putui8
277
// Write a native uint16_t to a buffer in littleendian format.
278
static inline uint8_t *putui16(uint8_t *ptr, const uint16_t val)
279
280
281
282
283
284
285
286
{
*(ptr++) = ((uint8_t) ((val >> 0) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 8) & 0xFF));
return ptr;
} // putui16
// Write a native uint32_t to a buffer in littleendian format.
287
static inline uint8_t *putui32(uint8_t *ptr, const uint32_t val)
288
289
290
291
292
293
294
295
296
297
{
*(ptr++) = ((uint8_t) ((val >> 0) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 8) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 16) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 24) & 0xFF));
return ptr;
} // putui32
// Write a native uint64_t to a buffer in littleendian format.
298
static inline uint8_t *putui64(uint8_t *ptr, const uint64_t val)
299
300
301
302
303
304
305
306
307
308
309
310
311
{
*(ptr++) = ((uint8_t) ((val >> 0) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 8) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 16) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 24) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 32) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 40) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 48) & 0xFF));
*(ptr++) = ((uint8_t) ((val >> 56) & 0xFF));
return ptr;
} // putui64
312
313
314
315
316
317
318
319
// Read a uint8_t from a buffer.
static inline uint8_t *getui8(uint8_t *ptr, uint8_t *val)
{
*val = *ptr;
return ptr + sizeof (*val);
} // getui8
320
// Read a littleendian uint16_t from a buffer in native format.
321
static inline uint8_t *getui16(uint8_t *ptr, uint16_t *val)
322
323
324
325
326
327
328
{
*val = ( (((uint16_t) ptr[0]) << 0) | (((uint16_t) ptr[1]) << 8) );
return ptr + sizeof (*val);
} // getui16
// Read a littleendian uint32_t from a buffer in native format.
329
static inline uint8_t *getui32(uint8_t *ptr, uint32_t *val)
330
331
332
333
334
335
336
337
338
339
{
*val = ( (((uint32_t) ptr[0]) << 0) |
(((uint32_t) ptr[1]) << 8) |
(((uint32_t) ptr[2]) << 16) |
(((uint32_t) ptr[3]) << 24) );
return ptr + sizeof (*val);
} // getui32
// Read a littleendian uint64_t from a buffer in native format.
340
static inline uint8_t *getui64(uint8_t *ptr, uint64_t *val)
341
342
343
344
345
346
347
348
349
350
351
352
353
{
*val = ( (((uint64_t) ptr[0]) << 0) |
(((uint64_t) ptr[1]) << 8) |
(((uint64_t) ptr[2]) << 16) |
(((uint64_t) ptr[3]) << 24) |
(((uint64_t) ptr[4]) << 32) |
(((uint64_t) ptr[5]) << 40) |
(((uint64_t) ptr[6]) << 48) |
(((uint64_t) ptr[7]) << 56) );
return ptr + sizeof (*val);
} // getui64
354
355
void xwrite_fatelf_header(const char *fname, const int fd,
const FATELF_header *header)
356
{
357
const size_t buflen = FATELF_DISK_FORMAT_SIZE(header->num_records);
358
359
360
361
362
363
uint8_t *buf = (uint8_t *) xmalloc(buflen);
uint8_t *ptr = buf;
int i;
ptr = putui32(ptr, header->magic);
ptr = putui16(ptr, header->version);
364
ptr = putui8(ptr, header->num_records);
365
ptr = putui8(ptr, header->reserved0);
366
367
for (i = 0; i < header->num_records; i++)
368
{
369
ptr = putui16(ptr, header->records[i].machine);
370
371
ptr = putui8(ptr, header->records[i].osabi);
ptr = putui8(ptr, header->records[i].osabi_version);
372
373
ptr = putui8(ptr, header->records[i].word_size);
ptr = putui8(ptr, header->records[i].byte_order);
374
375
ptr = putui8(ptr, header->records[i].reserved0);
ptr = putui8(ptr, header->records[i].reserved1);
376
377
ptr = putui64(ptr, header->records[i].offset);
ptr = putui64(ptr, header->records[i].size);
378
379
380
381
382
383
384
385
386
387
} // for
assert(ptr == (buf + buflen));
xlseek(fname, fd, 0, SEEK_SET); // jump to start of file again.
xwrite(fname, fd, buf, buflen);
free(buf);
} // xwrite_fatelf_header
388
// don't forget to free() the returned pointer!
389
FATELF_header *xread_fatelf_header(const char *fname, const int fd)
390
{
391
392
393
394
395
396
FATELF_header *header = NULL;
uint8_t buf[8];
uint8_t *fullbuf = NULL;
uint8_t *ptr = buf;
uint32_t magic = 0;
uint16_t version = 0;
397
398
uint8_t bincount = 0;
uint8_t reserved0 = 0;
399
400
401
402
403
404
405
size_t buflen = 0;
int i = 0;
xlseek(fname, fd, 0, SEEK_SET); // just in case.
xread(fname, fd, buf, sizeof (buf), 1);
ptr = getui32(ptr, &magic);
ptr = getui16(ptr, &version);
406
407
ptr = getui8(ptr, &bincount);
ptr = getui8(ptr, &reserved0);
408
409
if (magic != FATELF_MAGIC)
410
xfail("'%s' is not a FatELF binary.", fname);
411
else if (version != 1)
412
xfail("'%s' uses an unknown FatELF version.", fname);
413
414
415
416
417
418
419
420
buflen = FATELF_DISK_FORMAT_SIZE(bincount) - sizeof (buf);
ptr = fullbuf = (uint8_t *) xmalloc(buflen);
xread(fname, fd, fullbuf, buflen, 1);
header = (FATELF_header *) xmalloc(fatelf_header_size(bincount));
header->magic = magic;
header->version = version;
421
header->num_records = bincount;
422
header->reserved0 = reserved0;
423
424
425
for (i = 0; i < bincount; i++)
{
426
ptr = getui16(ptr, &header->records[i].machine);
427
428
ptr = getui8(ptr, &header->records[i].osabi);
ptr = getui8(ptr, &header->records[i].osabi_version);
429
430
ptr = getui8(ptr, &header->records[i].word_size);
ptr = getui8(ptr, &header->records[i].byte_order);
431
432
ptr = getui8(ptr, &header->records[i].reserved0);
ptr = getui8(ptr, &header->records[i].reserved1);
433
434
ptr = getui64(ptr, &header->records[i].offset);
ptr = getui64(ptr, &header->records[i].size);
435
436
437
438
439
440
441
442
} // for
assert(ptr == (fullbuf + buflen));
free(fullbuf);
return header;
} // xread_fatelf_header
443
444
uint64_t align_to_page(const uint64_t offset)
445
446
447
448
449
450
{
const size_t pagesize = 4096; // !!! FIXME: hardcoded pagesize.
const size_t overflow = (offset % pagesize);
return overflow ? (offset + (pagesize - overflow)) : offset;
} // align_to_page
451
452
453
454
// !!! FIXME: these names/descs aren't set in stone.
// List from: http://www.sco.com/developers/gabi/latest/ch4.eheader.html
static const fatelf_machine_info machines[] =
455
{
456
457
458
459
// MUST BE SORTED BY ID!
{ 0, "none", "No machine" },
{ 1, "m32", "AT&T WE 32100" },
{ 2, "sparc", "SPARC" },
460
{ 3, "i386", "Intel 80386" },
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
{ 4, "68k", "Motorola 68000" },
{ 5, "88k", "Motorola 88000" },
{ 7, "860", "Intel 80860" },
{ 8, "mips", "MIPS I" },
{ 9, "s370", "IBM System/370" },
{ 10, "mips-rs3", "MIPS RS3000" },
{ 15, "pa-risc", "Hewlett-Packard PA-RISC" },
{ 17, "vpp500", "Fujitsu VPP500" },
{ 18, "sparc32plus", "Enhanced instruction set SPARC" },
{ 19, "960", "Intel 80960" },
{ 20, "ppc", "PowerPC" },
{ 21, "ppc64", "64-bit PowerPC" },
{ 22, "s390", "IBM System/390" },
{ 36, "v800", "NEC V800" },
{ 37, "fr20", "Fujitsu FR20" },
{ 38, "rh32", "TRW RH-32" },
{ 39, "rce", "Motorola RCE" },
{ 40, "arm", "Advanced RISC Machines ARM" },
{ 41, "alpha", "Digital Alpha" },
{ 42, "sh", "Hitachi SH" },
{ 43, "sparcv9", "SPARC Version 9" },
{ 44, "tricore", "Siemens Tricore embedded" },
{ 45, "arc", "Argonaut RISC Core" },
{ 46, "h8-300", "Hitachi H8/300" },
{ 47, "h8-300h", "Hitachi H8/300H" },
{ 48, "h8s", "Hitachi H8S" },
{ 49, "h8-500", "Hitachi H8/500" },
{ 50, "ia64", "Intel IA-64" },
{ 51, "mipsx", "Stanford MIPS-X" },
{ 52, "coldfire", "Motorola Coldfire" },
{ 53, "m68hc12", "Motorola M68HC12" },
{ 54, "mma", "Fujitsu MMA Multimedia Accelerator" },
{ 55, "pcp", "Siemens PCP" },
{ 56, "ncpu", "Sony nCPU embedded RISC" },
{ 57, "ndr1", "Denso NDR1" },
{ 58, "starcore", "Motorola Star*Core" },
{ 59, "me16", "Toyota ME16" },
{ 60, "st100", "STMicroelectronics ST100" },
{ 61, "tinyj", "Advanced Logic Corp. TinyJ" },
{ 62, "x86_64", "AMD x86-64" },
{ 63, "pdsp", "Sony DSP" },
{ 64, "pdp10", "Digital Equipment Corp. PDP-10" },
{ 65, "pdp11", "Digital Equipment Corp. PDP-11" },
{ 66, "fx66", "Siemens FX66" },
{ 67, "st9plus", "STMicroelectronics ST9+" },
{ 68, "st7", "STMicroelectronics ST7" },
{ 69, "68hc16", "Motorola MC68HC16" },
{ 70, "68hc11", "Motorola MC68HC11" },
{ 70, "68hc11", "Motorola MC68HC11" },
{ 71, "68hc08", "Motorola MC68HC08" },
{ 72, "68hc05", "Motorola MC68HC05" },
{ 73, "svx", "Silicon Graphics SVx" },
{ 74, "st19", "STMicroelectronics ST19" },
{ 75, "vax", "Digital VAX" },
{ 76, "cris", "Axis Communications 32-bit embedded processor" },
{ 77, "javelin", "Infineon Technologies 32-bit embedded processor" },
{ 78, "firepath", "Element 14 64-bit DSP Processor" },
{ 79, "zsp", "LSI Logic 16-bit DSP Processor" },
{ 80, "mmix", "Donald Knuth's educational 64-bit processor" },
{ 81, "huany", "Harvard University machine-independent object files" },
{ 82, "prism", "SiTera Prism" },
{ 83, "avr", "Atmel AVR" },
{ 84, "fr30", "Fujitsu FR30" },
{ 85, "d10v", "Mitsubishi D10V" },
{ 86, "d30v", "Mitsubishi D30V" },
{ 87, "v850", "NEC v850" },
{ 88, "m32r", "Mitsubishi M32R" },
{ 89, "mn10300", "Matsushita MN10300" },
{ 90, "mn10200", "Matsushita MN10200" },
{ 91, "pj", "picoJava" },
{ 92, "openrisc", "OpenRISC" },
{ 93, "arc_a5", "ARC Cores Tangent-A5" },
{ 94, "xtensa", "Tensilica Xtensa" },
{ 95, "videocore", "Alphamosaic VideoCore" },
{ 96, "tmm_gpp", "Thompson Multimedia General Purpose Processor" },
{ 97, "ns32k", "National Semiconductor 32000 series" },
{ 98, "tpc", "Tenor Network TPC" },
{ 99, "snp1k", "Trebia SNP 1000" },
{ 100, "st200", "STMicroelectronics ST200" },
{ 101, "ip2k", "Ubicom IP2xxx" },
{ 102, "max", "MAX Processor" },
{ 103, "cr", "National Semiconductor CompactRISC" },
{ 104, "f2mc16", "Fujitsu F2MC16" },
{ 105, "msp430", "Texas Instruments msp430" },
{ 106, "blackfin", "Analog Devices Blackfin" },
{ 107, "se_c33", "S1C33 Family of Seiko Epson processors" },
{ 108, "sep", "Sharp embedded microprocessor" },
{ 109, "arca", "Arca RISC Microprocessor" },
{ 110, "unicore", "Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University" },
{ 0x9026, "alpha", "Digital Alpha" }, // linux headers use this.
{ 0x9080, "v850", "NEC v850" }, // old tools use this, apparently.
{ 0x9041, "m32r", "Mitsubishi M32R" }, // old tools use this, apparently.
{ 0xA390, "s390", "IBM System/390" }, // legacy value.
{ 0xBEEF, "mn10300", "Matsushita MN10300" }, // old tools.
};
// !!! FIXME: these names/descs aren't set in stone.
// List from: http://www.sco.com/developers/gabi/latest/ch4.eheader.html
560
static const fatelf_osabi_info osabis[] =
561
562
{
// MUST BE SORTED BY ID!
563
{ 0, "sysv", "UNIX System V" },
564
565
{ 1, "hpux", "Hewlett-Packard HP-UX" },
{ 2, "netbsd", "NetBSD" },
566
567
568
{ 3, "linux", "Linux" },
{ 4, "hurd", "Hurd" },
{ 5, "86open", "86Open common IA32" },
569
570
571
572
573
574
{ 6, "solaris", "Sun Solaris" },
{ 7, "aix", "AIX" },
{ 8, "irix", "IRIX" },
{ 9, "freebsd", "FreeBSD" },
{ 10, "tru64", "Compaq TRU64 UNIX" },
{ 11, "modesto", "Novell Modesto" },
575
576
{ 12, "openbsd", "OpenBSD" },
{ 13, "openvms", "OpenVMS" },
577
578
{ 14, "nsk", "Hewlett-Packard Non-Stop Kernel" },
{ 15, "aros", "Amiga Research OS" },
579
{ 97, "armabi", "ARM" },
580
{ 255, "standalone", "Standalone application" },
581
582
583
};
584
const fatelf_machine_info *get_machine_by_id(const uint16_t id)
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
{
int i;
for (i = 0; i < (sizeof (machines) / sizeof (machines[0])); i++)
{
if (machines[i].id == id)
return &machines[i];
else if (machines[i].id > id)
break; // not found (sorted by id).
} // for
return NULL;
} // get_machine_by_id
const fatelf_machine_info *get_machine_by_name(const char *name)
{
int i;
for (i = 0; i < (sizeof (machines) / sizeof (machines[0])); i++)
{
if (strcmp(machines[i].name, name) == 0)
return &machines[i];
} // for
607
608
609
return NULL;
} // get_machine_by_name
610
611
612
const fatelf_osabi_info *get_osabi_by_id(const uint8_t id)
613
614
{
int i;
615
for (i = 0; i < (sizeof (osabis) / sizeof (osabis[0])); i++)
616
{
617
618
619
if (osabis[i].id == id)
return &osabis[i];
else if (osabis[i].id > id)
620
621
622
623
break; // not found (sorted by id).
} // for
return NULL;
624
} // get_osabi_by_id
625
626
627
const fatelf_osabi_info *get_osabi_by_name(const char *name)
628
{
629
int i;
630
for (i = 0; i < (sizeof (osabis) / sizeof (osabis[0])); i++)
631
{
632
633
if (strcmp(osabis[i].name, name) == 0)
return &osabis[i];
634
635
636
} // for
return NULL;
637
} // get_osabi_by_name
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
static int parse_abi_version_string(const char *str)
{
long num = 0;
char *endptr = NULL;
const char *prefix = "osabiver";
const size_t prefix_len = 8;
assert(strlen(prefix) == prefix_len);
if (strncmp(str, prefix, prefix_len) != 0)
return -1;
str += prefix_len;
num = strtol(str, &endptr, 0);
return ( ((endptr == str) || (*endptr != '\0')) ? -1 : ((int) num) );
} // parse_abi_version_string
static int xfind_fatelf_record_by_fields(const FATELF_header *header,
const char *target)
{
char *buf = xstrdup(target);
const fatelf_osabi_info *osabi = NULL;
const fatelf_machine_info *machine = NULL;
FATELF_record rec;
663
int wants = 0;
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
int abiver = 0;
char *str = buf;
char *ptr = buf;
int retval = -1;
int i = 0;
while (1)
{
const char ch = *ptr;
if ((ch == ':') || (ch == '\0'))
{
*ptr = '\0';
if (ptr == str)
{
// no-op for empty string.
} // if
else if ((strcmp(str,"be")==0) || (strcmp(str,"bigendian")==0))
{
683
wants |= FATELF_WANT_BYTEORDER;
684
685
686
687
rec.byte_order = FATELF_BIGENDIAN;
} // if
else if ((strcmp(str,"le")==0) || (strcmp(str,"littleendian")==0))
{
688
wants |= FATELF_WANT_BYTEORDER;
689
690
691
692
rec.byte_order = FATELF_LITTLEENDIAN;
} // else if
else if (strcmp(str,"32bit") == 0)
{
693
wants |= FATELF_WANT_WORDSIZE;
694
695
696
697
rec.word_size = FATELF_32BITS;
} // else if
else if (strcmp(str,"64bit") == 0)
{
698
wants |= FATELF_WANT_WORDSIZE;
699
700
701
702
rec.word_size = FATELF_64BITS;
} // else if
else if ((machine = get_machine_by_name(str)) != NULL)
{
703
wants |= FATELF_WANT_MACHINE;
704
705
706
707
rec.machine = machine->id;
} // else if
else if ((osabi = get_osabi_by_name(str)) != NULL)
{
708
wants |= FATELF_WANT_OSABI;
709
710
711
712
rec.osabi = osabi->id;
} // else if
else if ((abiver = parse_abi_version_string(str)) != -1)
{
713
wants |= FATELF_WANT_OSABIVER;
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
rec.osabi_version = (uint8_t) abiver;
} // else if
else
{
xfail("Unknown target '%s'", str);
} // else
if (ch == '\0')
break; // we're done.
str = ptr + 1;
} // if
ptr++;
} // while
free(buf);
for (i = 0; i < ((int) header->num_records); i++)
{
const FATELF_record *prec = &header->records[i];
735
if ((wants & FATELF_WANT_MACHINE) && (rec.machine != prec->machine))
736
continue;
737
else if ((wants & FATELF_WANT_OSABI) && (rec.osabi != prec->osabi))
738
continue;
739
else if ((wants & FATELF_WANT_OSABIVER) && (rec.osabi_version != prec->osabi_version))
740
continue;
741
else if ((wants & FATELF_WANT_WORDSIZE) && (rec.word_size != prec->word_size))
742
continue;
743
else if ((wants & FATELF_WANT_BYTEORDER) && (rec.byte_order != prec->byte_order))
744
745
746
747
748
749
750
751
752
753
754
755
756
continue;
if (retval != -1)
xfail("Ambiguous target '%s'", target);
retval = i;
} // for
return retval;
} // xfind_fatelf_record_by_fields
int xfind_fatelf_record(const FATELF_header *header, const char *target)
{
757
if (strncmp(target, "record", 6) == 0)
758
{
759
760
761
762
763
764
765
766
767
768
769
770
char *endptr = NULL;
const long num = strtol(target+6, &endptr, 0);
if ((endptr != target+6) && (*endptr == '\0')) // a numeric index?
{
const long recs = (long) header->num_records;
if ((num < 0) || (num > recs))
{
xfail("No record #%ld in FatELF header (max %d)",
num, (int) recs);
} // if
return (int) num;
} // if
771
772
773
774
775
776
} // if
return xfind_fatelf_record_by_fields(header, target);
} // xfind_fatelf_record
777
778
779
780
781
782
783
784
785
786
int fatelf_record_matches(const FATELF_record *a, const FATELF_record *b)
{
return ( (a->machine == b->machine) &&
(a->osabi == b->osabi) &&
(a->osabi_version == b->osabi_version) &&
(a->word_size == b->word_size) &&
(a->byte_order == b->byte_order) );
} // fatelf_record_matches
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
int find_furthest_record(const FATELF_header *header)
{
// there's nothing that says the records have to be in order, although
// we probably _should_. Just in case, check them all.
const int total = (int) header->num_records;
uint64_t furthest = 0;
int retval = -1;
int i;
for (i = 0; i < total; i++)
{
const FATELF_record *rec = &header->records[i];
const uint64_t edge = rec->offset + rec->size;
if (edge > furthest)
{
retval = i;
furthest = edge;
} // if
} // for
return retval;
} // find_furthest_record
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
const char *fatelf_get_wordsize_string(const uint8_t wordsize)
{
if (wordsize == FATELF_32BITS)
return "32";
else if (wordsize == FATELF_64BITS)
return "64";
return "???";
} // fatelf_get_wordsize_string
const char *fatelf_get_byteorder_name(const uint8_t byteorder)
{
if (byteorder == FATELF_LITTLEENDIAN)
return "Littleendian";
else if (byteorder == FATELF_BIGENDIAN)
return "Bigendian";
return "???";
} // get_byteorder_name
831
const char *fatelf_get_byteorder_target_name(const uint8_t byteorder)
832
833
834
835
836
837
{
if (byteorder == FATELF_LITTLEENDIAN)
return "le";
else if (byteorder == FATELF_BIGENDIAN)
return "be";
return NULL;
838
} // fatelf_get_byteorder_target_name
839
840
841
const char *fatelf_get_wordsize_target_name(const uint8_t wordsize)
842
843
844
845
846
847
{
if (wordsize == FATELF_32BITS)
return "32bits";
else if (wordsize == FATELF_64BITS)
return "64bits";
return NULL;
848
} // fatelf_get_wordsize_target_name
849
850
851
852
const char *fatelf_get_target_name(const FATELF_record *rec, const int wants)
853
854
855
856
857
{
// !!! FIXME: this code is sort of stinky.
static char buffer[128];
const fatelf_osabi_info *osabi = get_osabi_by_id(rec->osabi);
const fatelf_machine_info *machine = get_machine_by_id(rec->machine);
858
859
const char *order = fatelf_get_byteorder_target_name(rec->byte_order);
const char *wordsize = fatelf_get_wordsize_target_name(rec->word_size);
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
buffer[0] = '\0';
if ((wants & FATELF_WANT_MACHINE) && (machine))
{
if (buffer[0])
strcat(buffer, ":");
strcat(buffer, machine->name);
} // if
if ((wants & FATELF_WANT_WORDSIZE) && (wordsize))
{
if (buffer[0])
strcat(buffer, ":");
strcat(buffer, wordsize);
} // if
if ((wants & FATELF_WANT_BYTEORDER) && (order))
{
if (buffer[0])
strcat(buffer, ":");
strcat(buffer, order);
} // if
if ((wants & FATELF_WANT_OSABI) && (osabi))
{
if (buffer[0])
strcat(buffer, ":");
strcat(buffer, osabi->name);
} // if
if (wants & FATELF_WANT_OSABIVER)
{
char tmp[32];
if (buffer[0])
strcat(buffer, ":");
snprintf(tmp, sizeof (tmp), "osabiver%d", (int) rec->osabi_version);
strcat(buffer, tmp);
} // if
return buffer;
901
} // fatelf_get_target_name
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
int xfind_junk(const char *fname, const int fd, const FATELF_header *header,
uint64_t *offset, uint64_t *size)
{
const int furthest = find_furthest_record(header);
if (furthest >= 0) // presumably, we failed elsewhere, but oh well.
{
const uint64_t fsize = xget_file_size(fname, fd);
const FATELF_record *rec = &header->records[furthest];
const uint64_t edge = rec->offset + rec->size;
if (fsize > edge)
{
*offset = edge;
*size = fsize - edge;
return 1;
} // if
} // if
return 0;
} // xfind_junk
void xappend_junk(const char *fname, const int fd,
const char *out, const int outfd,
const FATELF_header *header)
{
uint64_t offset, size;
if (xfind_junk(fname, fd, header, &offset, &size))
xcopyfile_range(fname, fd, out, outfd, offset, size);
} // xappend_junk
936
937
938
void xfatelf_init(int argc, const char **argv)
{
memset(zerobuf, '\0', sizeof (zerobuf)); // just in case.
939
940
941
942
943
if ((argc >= 2) && (strcmp(argv[1], "--version") == 0))
{
printf("%s\n", fatelf_build_version);
exit(0);
} // if
944
945
} // xfatelf_init
946
// end of fatelf-utils.c ...