OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 7:49 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Wierd behavior of the linker
PostPosted: Mon Mar 15, 2021 9:44 am 
Offline
Member
Member
User avatar

Joined: Wed Feb 19, 2020 1:08 pm
Posts: 270
Location: Italy
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
Code:
nasm -f bin src/loader.asm -o bin/loader.bin

with these
Code:
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

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:
Code:
e9 cc90 0f00

instead, these are the ones I get with the other method
Code:
e9 fc0f 1000

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.

_________________
Regards, Bonfra.


Top
 Profile  
 
 Post subject: Re: Wierd behavior of the linker
PostPosted: Mon Mar 15, 2021 2:27 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5143
Bonfra wrote:
I don't think the code is worth to be posted,

I couldn't reproduce the issue. You'll have to post some code. (You don't have to post the whole thing if you can come up with a small example to demonstrate the problem.)


Top
 Profile  
 
 Post subject: Re: Wierd behavior of the linker
PostPosted: Mon Mar 15, 2021 2:38 pm 
Offline
Member
Member
User avatar

Joined: Wed Feb 19, 2020 1:08 pm
Posts: 270
Location: Italy
Octocontrabass wrote:
You'll have to post some code.

Code:
;org 0x7e00
section .text
    global load

bits 64
load:
    jmp 0x00101000


I think that this is enough, it should be an absolute jump to that address

_________________
Regards, Bonfra.


Top
 Profile  
 
 Post subject: Re: Wierd behavior of the linker
PostPosted: Mon Mar 15, 2021 3:01 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5143
Huh! NASM appears to emit the correct code for later relocation, but then doesn't include a relocation entry.

Try something like this:
Code:
jmp 0x00101000 + load - 0x7e00


Top
 Profile  
 
 Post subject: Re: Wierd behavior of the linker
PostPosted: Mon Mar 15, 2021 3:22 pm 
Offline
Member
Member
User avatar

Joined: Wed Feb 19, 2020 1:08 pm
Posts: 270
Location: Italy
Octocontrabass wrote:
Huh! NASM appears to emit the correct code for later relocation, but then doesn't include a relocation entry.

So it's some sort of nasm bug?

Octocontrabass wrote:
Try something like this:
Code:
jmp 0x00101000 + load - 0x7e00

where load is? That 0x130 value? also from where does it come from?

_________________
Regards, Bonfra.


Top
 Profile  
 
 Post subject: Re: Wierd behavior of the linker
PostPosted: Mon Mar 15, 2021 6:47 pm 
Online
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5143
Bonfra wrote:
So it's some sort of nasm bug?

Maybe. I'm not sure if ELF allows an anonymous relocation like that, it might only work when you have a symbolic name.

Bonfra wrote:
where load is? That 0x130 value? also from where does it come from?

"load" is the label you put at 0x7e00 at the beginning of the .text section. The 0x130 value comes from the address of the JMP instruction. If you look at the encoded operand (0x100ffc) it's just 4 less than the destination address; that's how JMP operands are usually encoded before relocations are applied.


Top
 Profile  
 
 Post subject: Re: Wierd behavior of the linker
PostPosted: Wed Mar 17, 2021 6:13 am 
Offline
Member
Member
User avatar

Joined: Wed Feb 19, 2020 1:08 pm
Posts: 270
Location: Italy
Octocontrabass wrote:
"load" is the label you put at 0x7e00 at the beginning of the .text section. The 0x130 value comes from the address of the JMP instruction. If you look at the encoded operand (0x100ffc) it's just 4 less than the destination address; that's how JMP operands are usually encoded before relocations are applied.

Oh, yea I didn't connect the two things XD. Awesome it works thanks

_________________
Regards, Bonfra.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: SemrushBot [Bot] and 122 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group