stuff that gets pushed on to the stack in grub boot loader

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
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

stuff that gets pushed on to the stack in grub boot loader

Post by ITchimp »

I obtained the esp in the boot.s

Code: Select all

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?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: stuff that gets pushed on to the stack in grub boot load

Post by iansjack »

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.
Octocontrabass
Member
Member
Posts: 5572
Joined: Mon Mar 25, 2013 7:01 pm

Re: stuff that gets pushed on to the stack in grub boot load

Post by Octocontrabass »

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.
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

Re: stuff that gets pushed on to the stack in grub boot load

Post by ITchimp »

main is declared as

Code: Select all

void main(struct multiboot *mboot_ptr , unsigned long init_esp)
you see between

Code: Select all

push esp
and

Code: Select all

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....
Rew
Member
Member
Posts: 28
Joined: Mon Oct 29, 2012 2:26 pm

Re: stuff that gets pushed on to the stack in grub boot load

Post by Rew »

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.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: stuff that gets pushed on to the stack in grub boot load

Post by iansjack »

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.
Post Reply