Page 1 of 1
stuff that gets pushed on to the stack in grub boot loader
Posted: Sat Jul 25, 2020 4:32 am
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?
Re: stuff that gets pushed on to the stack in grub boot load
Posted: Sat Jul 25, 2020 4:52 am
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.
Re: stuff that gets pushed on to the stack in grub boot load
Posted: Sat Jul 25, 2020 2:18 pm
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.
Re: stuff that gets pushed on to the stack in grub boot load
Posted: Sat Jul 25, 2020 5:55 pm
by ITchimp
main is declared as
Code: Select all
void main(struct multiboot *mboot_ptr , unsigned long init_esp)
you see between
and
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....
Re: stuff that gets pushed on to the stack in grub boot load
Posted: Sun Jul 26, 2020 7:47 am
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.
Re: stuff that gets pushed on to the stack in grub boot load
Posted: Sun Jul 26, 2020 9:02 am
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.