Wierd behavior of the linker
Posted: Mon Mar 15, 2021 9:44 am
My second stage bootloader is composed of just one assembly file which I assemble with `nasm -f bin` to generate a flat binary with the code. I decided that I want to expand the code and add more files so now I want to generate an object file for each asm file and then link them together. Starting to convert the old process to this new one, I replaced this command
with these
So after adding the linking things I removed the org directive from the .asm file and added it to the ld as a flag together with the entry point.
The file outputted directly as binary is totally identical to the one with the linking stage except for a single instruction that has a major difference:
a near jump instruction in the 64-bit part of the code is totally different from the value it should be and obviously breaks everything.
dumping the binary I discovered that is exactly (0x130 + the org) bytes wrong so exactly these are the right bytes outputted from with the -f bin:
instead, these are the ones I get with the other method
The rest of the code is assembled perfectly.
It could be worth noting that there are no other jumps in the 64-bit part of the code other than the broken one.
I don't think the code is worth to be posted, but if it is necessary let me know and I'll create a branch in the git repo for this issue.
Code: Select all
nasm -f bin src/loader.asm -o bin/loader.bin
Code: Select all
nasm -f elf64 -Wno-all src/loader.asm -o obj/loader.o
ld -nostdlib -o bin/loader.bin obj/loader.o --oformat=binary --entry=load -Ttext=0x7e00
The file outputted directly as binary is totally identical to the one with the linking stage except for a single instruction that has a major difference:
a near jump instruction in the 64-bit part of the code is totally different from the value it should be and obviously breaks everything.
dumping the binary I discovered that is exactly (0x130 + the org) bytes wrong so exactly these are the right bytes outputted from with the -f bin:
Code: Select all
e9 cc90 0f00
Code: Select all
e9 fc0f 1000
It could be worth noting that there are no other jumps in the 64-bit part of the code other than the broken one.
I don't think the code is worth to be posted, but if it is necessary let me know and I'll create a branch in the git repo for this issue.