Skip to content
This repository was archived by the owner on Jun 23, 2026. It is now read-only.

Commit f6b9c46

Browse files
fix: host-target endianness conversion for Elf32_Rela
1 parent 2ae993f commit f6b9c46

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

src/ezld/linker.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,13 @@ static Elf32_Phdr endian_phdr(Elf32_Phdr phdr) {
630630
return phdr;
631631
}
632632

633+
static Elf32_Rela endian_rela(Elf32_Rela rela) {
634+
rela.r_offset = endian32(rela.r_offset);
635+
rela.r_info = endian32(rela.r_info);
636+
rela.r_addend = endian32(rela.r_addend);
637+
return rela;
638+
}
639+
633640
/**
634641
* @brief Reads a section header from an object file
635642
*
@@ -874,15 +881,6 @@ static void read_object(ezld_obj_t *obj) {
874881
EZLD_ECODE_BADFILE, "'%s' is not a 32-bit ELF", obj->obj_filepath);
875882
}
876883

877-
uint16_t obj_arch = endian16(ehdr.e_machine);
878-
if (!EZLD_IS_SUPPORTED_ARCH(obj_arch)) {
879-
ezld_runtime_exit(
880-
EZLD_ECODE_BADFILE,
881-
"file '%s' is for unsupported machine architecture '%s'",
882-
obj->obj_filepath,
883-
arch_name(obj_arch));
884-
}
885-
886884
if (!g_self->i_out.out_set) {
887885
g_self->i_out.out_set = true;
888886
g_self->i_out.out_endian = ehdr.e_ident[EI_DATA];
@@ -902,6 +900,14 @@ static void read_object(ezld_obj_t *obj) {
902900
ehdr = endian_ehdr(ehdr);
903901
obj->obj_ehdr = ehdr;
904902

903+
if (!EZLD_IS_SUPPORTED_ARCH(ehdr.e_machine)) {
904+
ezld_runtime_exit(
905+
EZLD_ECODE_BADFILE,
906+
"file '%s' is for unsupported machine architecture '%s'",
907+
obj->obj_filepath,
908+
arch_name(ehdr.e_machine));
909+
}
910+
905911
if (ehdr.e_type != ET_REL) {
906912
ezld_runtime_exit(EZLD_ECODE_BADFILE,
907913
"'%s' is not a relocatable object file",
@@ -1332,7 +1338,7 @@ static void free_instance(void) {
13321338
fclose(g_self->i_out.out_file);
13331339
}
13341340

1335-
// TODO: fix HUGE endianness UB here
1341+
// TODO: fix HUGE endianness issues here
13361342
/**
13371343
* @brief Applies relocations to the final executable (after it has been written
13381344
* to disk)
@@ -1505,9 +1511,8 @@ static void rela_section(ezld_obj_sec_t *objsec) {
15051511
size_t num_entries = objsec->os_elems;
15061512
Elf32_Rela *relas = (Elf32_Rela *)objsec->os_data;
15071513

1508-
// TODO: fix endianness here too
15091514
for (size_t i = 0; i < num_entries; i++) {
1510-
Elf32_Rela entry = relas[i];
1515+
Elf32_Rela entry = endian_rela(relas[i]);
15111516

15121517
if (entry.r_offset >= target->os_shdr.sh_size) {
15131518
ezld_runtime_exit(EZLD_ECODE_BADSEC,

0 commit comments

Comments
 (0)