OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 11:49 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: stuff that gets pushed on to the stack in grub boot loader
PostPosted: Sat Jul 25, 2020 4:32 am 
Offline
Member
Member

Joined: Sat Aug 18, 2018 8:44 pm
Posts: 127
I obtained the esp in the boot.s
Code:
start:
   push esp
   push ecx
   cli
   call main


in the first line of main, I check the esp value and ebp value they are very different

the initial esp, one gets pushed on the stack before calling main is 0x0067ecc

the esp value and ebp value are both 0x0067d70

what is in the gap between those...? what does grub push onto the stack?


Top
 Profile  
 
 Post subject: Re: stuff that gets pushed on to the stack in grub boot load
PostPosted: Sat Jul 25, 2020 4:52 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4597
Location: Chichester, UK
Anything that grub does will be before start:, so I can't see how grub can be affecting the stack.

I think the question is, what is your C program main is doing to the stack. Did you declare main as void main(void) or int main(int, char**)? Can we assume that you are using a cross-compiler and are linking nothing other than your start: code and main.o into your executable? You might want to look at the assembly code produced from main.c to ascertain what is happening.


Top
 Profile  
 
 Post subject: Re: stuff that gets pushed on to the stack in grub boot load
PostPosted: Sat Jul 25, 2020 2:18 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5145
GRUB does not set up the stack at all. You must set ESP yourself before you can call main (or call anything else, since the CALL instruction requires a stack).

Refer to the Multiboot2 or Multiboot specification for details.


Top
 Profile  
 
 Post subject: Re: stuff that gets pushed on to the stack in grub boot load
PostPosted: Sat Jul 25, 2020 5:55 pm 
Offline
Member
Member

Joined: Sat Aug 18, 2018 8:44 pm
Posts: 127
main is declared as
Code:
void main(struct multiboot *mboot_ptr , unsigned long init_esp)


you see between
Code:
push esp
and
Code:
call main
there is only eip being pushed onto the stack.

How is it possible that the gap between init_esp and esp in main ( 348 bytes long)?g

also here is a clarification, main does nothing when esp/ebp are printed out....


Top
 Profile  
 
 Post subject: Re: stuff that gets pushed on to the stack in grub boot load
PostPosted: Sun Jul 26, 2020 7:47 am 
Offline
Member
Member

Joined: Mon Oct 29, 2012 2:26 pm
Posts: 28
To expand on what iansjack was hinting at, you need to understand the stack better. You should know the calling convention that your main() func is compiled with. You need to understand what the CALL and PUSH instructions do in x86. You need to read about function prologue/epilogue. You need to understand what happens when you declare a local var in your main func. You need to understand that a compiler may create additional local vars as part of its optimization. Looking at the disassembly of main will probably help you see what is happening. There is 0 code that can execute between your CALL instruction and the beginning of main in your example. Understanding CALL and looking at the disassembly of main should have an exact explanation of what is happening to your stack.

That being said, after you understand what is happening here you will want to setup your own stack prior to pushing anything to the stack or calling any C functions. Do not use or make assumptions about what a multiboot loader gives you. Many people just declare a small local chunk of memory in assm and load it into esp prior to calling main. This is usually sufficient as a stack until you get further in the boot process and want to move it elsewhere.


Top
 Profile  
 
 Post subject: Re: stuff that gets pushed on to the stack in grub boot load
PostPosted: Sun Jul 26, 2020 9:02 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4597
Location: Chichester, UK
Most certainly you should add up all the space reserved for local variables in your main program. Without seeing it I can't judge, but each needs space reserved for it on the stack.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 213 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