3 ms·
Since rust compiles to native machine code (bytes), how does it calculate the starting address of the code? for example, If there is a JMP instruction, JMP take
by _RPM 10y ago
Since rust compiles to native machine code (bytes), how does it calculate the starting address of the code? for example, If there is a JMP instruction, JMP takes an address as an operand most likely -- Doesn't the kernel determine the starting address, or is the starting address the address returned by mmap?
- fulafel 10y agoModern amd64 code is position-independent, so the JMP address is relative. On other architectures, it's the linker's job to convert symbolic addresses and it can choose all the addresses in one go as it's the final stage producting the executable.
- vidarh 10y agoThat solves internal offsets. But code can also be loaded at different addresses on many platforms/architecture combos. That's solved by relocation tables: For every address, the linker will assume a starting address of 0, and then add the location of the address to a table. The runtime linker will then go through the relocation tables and fix up any entries with the real address.
- _RPM 10y agoRelative to what, the start address? Does rust determine the start address from the return value of `mmap`?
- fulafel 10y agoRelative to the current instruction pointer aka program counter. You say things like "JMP -100 bytes".