Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Document nxodance #23

Open
roblabla opened this issue Nov 13, 2017 · 1 comment
Open

Document nxodance #23

roblabla opened this issue Nov 13, 2017 · 1 comment

Comments

@roblabla
Copy link
Member

Here's what misson sent me :


An NRO file has four sections in it: .text, .rodata, .data, and .bss.
The NRO file itself is just a concatenation of these sections, with a small header. To make things easier, we just include this header in the .text section and start .text at the beginning of the file.

The NRO header is 0x80 bytes long, which is why you see us skip 0x80 bytes at the start of .text in crt0.nro.S. The NRO header is generated by elf2nxo.py there. Another important header is the MOD0 header. I don't think we really need to include it exactly, since I'm pretty sure it's only used by the Nintendo runtime linker (rtld), but we include it anyway. It lives near the start of .data and describes, among other things, where the linking information lives.

As such, the flat NRO file looks like this:
.text

  • NRO header (0x80 bytes)
    • describes where each section is in the file and how big they all are
  • .text (skipping first 0x80 bytes since we overwrite them with NRO header)

.rodata

  • .rodata contents
  • .rela.dyn (concatenated by elf2nxo.py because I couldn't get the linker to put this in .rodata)

.data

  • MOD0 header
    • describes location of .dynamic
  • .data contents
  • .dynamic (again concatenated by elf2nxo.py because linker troubles)

Special care needs to be taken to ensure that elf2nxo.py places .rela.dyn and .dynamic where the linker expects. If anyone is better at link scripts than I am and can figure out how to get the linker to put those sections in .rodata and .data respectively, please so.

The NRO file on the disk is pretty much exactly the same as the NRO image that gets loaded. There's no compression, no sections get moved around, or anything. The last piece of the puzzle is to relocate all the code. This really should be done in C, but right now for us, that's part of NRO loading. You can see in Nxo.cpp in Mephisto the stupid linker I wrote, and we have a similar one written in JavaScript.

@roblabla
Copy link
Member Author

Just a small update, the relocation code is now written inside libtransistor, https://github.com/reswitched/libtransistor/blob/master/lib/crt0_common.c#L43

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

No branches or pull requests

2 participants