Skip to content

Commit

Permalink
Added word_size and byte_order to FATELF_record.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Sep 10, 2009
1 parent f6e04a1 commit f568c7f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
11 changes: 10 additions & 1 deletion include/fatelf.h
Expand Up @@ -18,13 +18,22 @@
/* This does not count padding for page alignment at the end. */
#define FATELF_DISK_FORMAT_SIZE(bins) (8 + (24 * (bins)))

/* Valid FATELF_record::word_size values... */
#define FATELF_32BITS (1)
#define FATELF_64BITS (2)

/* Valid FATELF_record::byte_order values... */
#define FATELF_BIGENDIAN (0)
#define FATELF_LITTLEENDIAN (1)

/* Values on disk are always littleendian, and align like Elf64. */
typedef struct FATELF_record
{
uint16_t osabi;
uint16_t osabi_version;
uint16_t machine;
uint16_t reserved0;
uint8_t word_size; /* maps to e_ident[EI_CLASS]. */
uint8_t byte_order; /* maps to e_ident[EI_DATA]. */
uint64_t offset;
uint64_t size;
} FATELF_record;
Expand Down
2 changes: 2 additions & 0 deletions utils/fatelf-glue.c
Expand Up @@ -47,6 +47,8 @@ static int fatelf_glue(const char *out, const char **bins, const int bincount)
{
const FATELF_record *other = &header->records[j];
const int same = (other->machine == record->machine) &&
(other->word_size == record->word_size) &&
(other->byte_order == record->byte_order) &&
(other->osabi == record->osabi) &&
(other->osabi_version == record->osabi_version);
if (same)
Expand Down
22 changes: 22 additions & 0 deletions utils/fatelf-info.c
Expand Up @@ -9,6 +9,26 @@
#define FATELF_UTILS 1
#include "fatelf-utils.h"

static const char *get_wordsize(const uint8_t wordsize)
{
if (wordsize == FATELF_32BITS)
return "32";
else if (wordsize == FATELF_64BITS)
return "64";
return "???";
} // get_wordsize


static const char *get_byteorder_name(const uint8_t byteorder)
{
if (byteorder == FATELF_LITTLEENDIAN)
return "Littleendian";
else if (byteorder == FATELF_BIGENDIAN)
return "Bigendian";
return "???";
} // get_byteorder_name


static int fatelf_info(const char *fname)
{
const int fd = xopen(fname, O_RDONLY, 0755);
Expand All @@ -29,6 +49,8 @@ static int fatelf_info(const char *fname)
(unsigned int) rec->osabi, osabi ? osabi->name : "???",
osabi ? ": " : "", osabi ? osabi->desc : "",
(unsigned int) rec->osabi_version);
printf(" %s bits\n", get_wordsize(rec->word_size));
printf(" %s byteorder\n", get_byteorder_name(rec->byte_order));
printf(" Machine %u (%s%s%s)\n",
(unsigned int) rec->machine, machine ? machine->name : "???",
machine ? ": " : "", machine ? machine->desc : "");
Expand Down
24 changes: 19 additions & 5 deletions utils/fatelf-utils.c
Expand Up @@ -135,12 +135,24 @@ void xread_elf_header(const char *fname, const int fd, FATELF_record *record)
xfail("'%s' is not an ELF binary");
record->osabi = (uint16_t) buf[7];
record->osabi_version = (uint16_t) buf[8];
if (buf[5] == 0) // bigendian
record->word_size = buf[4];
record->byte_order = buf[5];

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)
record->machine = (((uint16_t)buf[18]) << 8) | (((uint16_t)buf[19]));
else if (buf[5] == 1) // littleendian
else if (record->byte_order == FATELF_LITTLEENDIAN)
record->machine = (((uint16_t)buf[19]) << 8) | (((uint16_t)buf[18]));
else
xfail("Unexpected data encoding in '%s'", fname);
{
xfail("Unexpected byte order (%d) in '%s'",
(int) record->byte_order, fname);
} // else
} // xread_elf_header


Expand Down Expand Up @@ -253,7 +265,8 @@ void xwrite_fatelf_header(const char *fname, const int fd,
ptr = putui16(ptr, header->records[i].osabi);
ptr = putui16(ptr, header->records[i].osabi_version);
ptr = putui16(ptr, header->records[i].machine);
ptr = putui16(ptr, header->records[i].reserved0);
ptr = putui8(ptr, header->records[i].word_size);
ptr = putui8(ptr, header->records[i].byte_order);
ptr = putui64(ptr, header->records[i].offset);
ptr = putui64(ptr, header->records[i].size);
} // for
Expand Down Expand Up @@ -307,7 +320,8 @@ FATELF_header *xread_fatelf_header(const char *fname, const int fd)
ptr = getui16(ptr, &header->records[i].osabi);
ptr = getui16(ptr, &header->records[i].osabi_version);
ptr = getui16(ptr, &header->records[i].machine);
ptr = getui16(ptr, &header->records[i].reserved0);
ptr = getui8(ptr, &header->records[i].word_size);
ptr = getui8(ptr, &header->records[i].byte_order);
ptr = getui64(ptr, &header->records[i].offset);
ptr = getui64(ptr, &header->records[i].size);
} // for
Expand Down

0 comments on commit f568c7f

Please sign in to comment.