Any way for offset to not be hard-coded for far jump?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Any way for offset to not be hard-coded for far jump?

Post by ScropTheOSAdventurer »

I am working on re-implementing my GDT code, and I was wondering: can the offset in a far jump come from a register (I mean like offset:address in this case)? I have looked on the web, but I can't find any answer for this on the web, so I am sorry if it is obvious.
"Procrastination is the art of keeping up with yesterday."
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Any way for offset to not be hard-coded for far jump?

Post by bluemoon »

use indirect jump, which you store the address in a memory location and dereference it.
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Any way for offset to not be hard-coded for far jump?

Post by ScropTheOSAdventurer »

Just to make sure, in GAS syntax (forgive me if it is bad; still learning to work with GAS instead of Intel) it is so:

Code: Select all

jmp (addressofoffsetmemory):thetarget
"Procrastination is the art of keeping up with yesterday."
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Any way for offset to not be hard-coded for far jump?

Post by sortie »

I recall there being a useful popf instruction.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Any way for offset to not be hard-coded for far jump?

Post by bluemoon »

sortie wrote:I recall there being a useful popf instruction.
you mean retf? While it works well to reload GDT in 32bit and earlier arch, sadly retf has some problem with 64bit arch (which you use iretq instead).
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Any way for offset to not be hard-coded for far jump?

Post by ScropTheOSAdventurer »

Well, what I said in the last post I gave doesn't assemble. Here are two messages:

GDTAssembly.s:16: Error: junk `:*' after expression
GDTAssembly.s:16: Warning: indirect jmp without `*'


In any case, I will look into retf.
"Procrastination is the art of keeping up with yesterday."
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Any way for offset to not be hard-coded for far jump?

Post by ScropTheOSAdventurer »

Here is the best explanation that I can find on the web for retf :
Far returns pop the IP followed by the CS, while near
returns pop only the IP register.


So does this mean that right before I call the function I push the offset, or while I am in the function I push it? In other words, is the new CS value on top of IP or BELOW it (assuming you think of the stack as going UPWARDS, although it doesn't)?
"Procrastination is the art of keeping up with yesterday."
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Any way for offset to not be hard-coded for far jump?

Post by alexfru »

You could always dust off your favorite Intel or AMD manual (or google.com) and see the pseudo code for RETF. But, in case you can't find your duster, you first push CS (zero extended to 4 bytes if you're in 32-bit mode or the target is 32-bit code), then (E)IP, then do RETF (prefixed with db 0x66, if you're switching from 16-bit to 32-bit code). IRET can be used similarly.

Another option is to have a JMPF selector:offset instruction and patch the offset in it before executing it.
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Any way for offset to not be hard-coded for far jump?

Post by ScropTheOSAdventurer »

I should have read the manual instead of googling. In any case, I pretty much implemented what you just told me (except that I wrongly assumed cs was in 16 bits instead of 32, which explains my triple fault). Thanks!
"Procrastination is the art of keeping up with yesterday."
User avatar
ScropTheOSAdventurer
Member
Member
Posts: 86
Joined: Sun Aug 25, 2013 5:47 pm
Location: Nebraska, USA

Re: Any way for offset to not be hard-coded for far jump?

Post by ScropTheOSAdventurer »

Ok, here is the code I finally settled on (it works). You would call it in C like this

Code: Select all

load_gdt(gdtpointerstruct, dataoffsetingdt, codeoffsetingdt); 
    



Now the implementation:

Code: Select all

Reload_CS: 
	retf 

.global load_gdt 

.type load_gdt, @function 


load_gdt: 
	xor %edx, %edx 
	movw 16(%esp), %dx 
	movw 12(%esp), %ax 
	lgdt 4(%esp) 
	movw  %ax, %ds 
	movw %ax, %es 
	movw %ax, %fs 
	movw %ax, %gs 
	movw %ax, %ss 
	push %edx 
	call Reload_CS 
	ret 
	
	  


Thanks guys! :D
"Procrastination is the art of keeping up with yesterday."
Post Reply