Skip to content

Commit c4c2fc1

Browse files
committed
Make -fraw obey endian swapping
1 parent a2e7d7e commit c4c2fc1

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/asm.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ static int raw_in(FILE *stream, struct element *i, void *ud)
420420
{
421421
int *offset = ud;
422422
int rc = (fread(&i->insn.u.word, 4, 1, stream) == 1) ? 1 : -1;
423+
i->insn.u.word = fixup_endian(i->insn.u.word);
423424
i->insn.reladdr = (*offset)++;
424425
return rc;
425426
}
@@ -436,7 +437,8 @@ static int raw_out(FILE *stream, struct element *i, void *ud)
436437
}
437438

438439
if (i->insn.size > 0) {
439-
if (fwrite(&i->insn.u.word, sizeof i->insn.u.word, 1, stream) != 1)
440+
UWord temp = fixup_endian(i->insn.u.word);
441+
if (fwrite(&temp, sizeof i->insn.u.word, 1, stream) != 1)
440442
return -1;
441443
++*offset;
442444
}

src/common.h

+17
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ static inline char *strcopy(char *dest, const char *src, size_t sz)
7878
return result;
7979
}
8080

81+
static inline uint32_t swapword(const uint32_t in)
82+
{
83+
return (((in >> 24) & 0xff) << 0) |
84+
(((in >> 16) & 0xff) << 8) |
85+
(((in >> 8) & 0xff) << 16) |
86+
(((in >> 0) & 0xff) << 24);
87+
}
88+
89+
static inline uint32_t fixup_endian(const uint32_t in)
90+
{
91+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
92+
return in;
93+
#else
94+
return swapword(in);
95+
#endif
96+
}
97+
8198
long long numberise(char *str, int base);
8299

83100
#define ALIASING_CAST(Type,Expr) \

src/obj.c

-8
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ static inline void put_sized_le(const void *what, size_t size, size_t count, FIL
3636
#define get_sized get_sized_le
3737
#define put_sized put_sized_le
3838
#else
39-
static inline UWord swapword(const UWord in)
40-
{
41-
return (((in >> 24) & 0xff) << 0) |
42-
(((in >> 16) & 0xff) << 8) |
43-
(((in >> 8) & 0xff) << 16) |
44-
(((in >> 0) & 0xff) << 24);
45-
}
46-
4739
static inline void get_sized_be(void *what, size_t size, size_t count, FILE *where)
4840
{
4941
get_sized_le(what, size, count, where);

0 commit comments

Comments
 (0)