Skip to content

Commit

Permalink
Make -fraw obey endian swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
kulp committed Jul 19, 2015
1 parent a2e7d7e commit c4c2fc1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ static int raw_in(FILE *stream, struct element *i, void *ud)
{
int *offset = ud;
int rc = (fread(&i->insn.u.word, 4, 1, stream) == 1) ? 1 : -1;
i->insn.u.word = fixup_endian(i->insn.u.word);
i->insn.reladdr = (*offset)++;
return rc;
}
Expand All @@ -436,7 +437,8 @@ static int raw_out(FILE *stream, struct element *i, void *ud)
}

if (i->insn.size > 0) {
if (fwrite(&i->insn.u.word, sizeof i->insn.u.word, 1, stream) != 1)
UWord temp = fixup_endian(i->insn.u.word);
if (fwrite(&temp, sizeof i->insn.u.word, 1, stream) != 1)
return -1;
++*offset;
}
Expand Down
17 changes: 17 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ static inline char *strcopy(char *dest, const char *src, size_t sz)
return result;
}

static inline uint32_t swapword(const uint32_t in)
{
return (((in >> 24) & 0xff) << 0) |
(((in >> 16) & 0xff) << 8) |
(((in >> 8) & 0xff) << 16) |
(((in >> 0) & 0xff) << 24);
}

static inline uint32_t fixup_endian(const uint32_t in)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return in;
#else
return swapword(in);
#endif
}

long long numberise(char *str, int base);

#define ALIASING_CAST(Type,Expr) \
Expand Down
8 changes: 0 additions & 8 deletions src/obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ static inline void put_sized_le(const void *what, size_t size, size_t count, FIL
#define get_sized get_sized_le
#define put_sized put_sized_le
#else
static inline UWord swapword(const UWord in)
{
return (((in >> 24) & 0xff) << 0) |
(((in >> 16) & 0xff) << 8) |
(((in >> 8) & 0xff) << 16) |
(((in >> 0) & 0xff) << 24);
}

static inline void get_sized_be(void *what, size_t size, size_t count, FILE *where)
{
get_sized_le(what, size, count, where);
Expand Down

0 comments on commit c4c2fc1

Please sign in to comment.