Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ELF loader behaves differently from a raw binary object #3392

Open
SBird1337 opened this issue Jan 5, 2025 · 1 comment
Open

ELF loader behaves differently from a raw binary object #3392

SBird1337 opened this issue Jan 5, 2025 · 1 comment

Comments

@SBird1337
Copy link

For lack of better words in the tittle :(

As also discussed in the discord conversation there seems to be some discrepancy between how objdump writes binary files from an ELF and how mgba interpretes ELF. Specifically the LMA of some section is sometimes not put into the respective ROM section as specied by the ELF.

An example can be found in the pokeemerald-expansion project's test system. Steps to reproduce:

diff --git a/ld_script_test.ld b/ld_script_test.ld
index d279d6b4f6..50198deeca 100644
--- a/ld_script_test.ld
+++ b/ld_script_test.ld
@@ -118,7 +118,7 @@ SECTIONS {
     } > ROM =0

     .data.iwram :
-    ALIGN(8)
+    ALIGN(4)
     {
         __iwram_lma = .;
         . = . + (__iwram_end - __iwram_start);
  • run make -j$(nproc) pokeemerald-test.elf.

The issue occured to me using gcc14, but others reported it with other gcc versions as well. Funkily enough it does not happen for me with gcc10.

The issue also disappears once you build a rom file e.g. with arm-none-eabi-objcopy -O binary pokeemerald-test.elf pokeemerald-test.gba

@endrift
Copy link
Member

endrift commented Jan 10, 2025

Looking into this, I think this might be an issue with the ELF after all. The resulting ELF has the following two load commands (a subset of the objdump -p output):

    LOAD off    0x018de000 vaddr 0x03000000 paddr 0x098dcbb4 align 2**12
         filesz 0x00000864 memsz 0x00000864 flags rw-
    LOAD off    0x018debb4 vaddr 0x098dcbb4 paddr 0x098dcbb4 align 2**12
         filesz 0x001d6164 memsz 0x001d6164 flags rwx

These tell the loader to load two different portions of the ELF into overlapping regions of physical memory. In fact, the desired memory is loaded, but then immediately overwritten by this second load command. It's probably a bug with the linker script generating the load commands in the wrong order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants