A while back I ported Newlib to BareMetal OS (Instructions so far are here: http://code.google.com/p/baremetal/wiki/BuildingNewlib). Now I am looking to add to its functionality.
I hoping that someone else on here that has ported Newlib can answer my questions.
In crt0.s (or .c) I want to push argc and argv onto the stack. In what order is this done? More importantly what format should argv be in? Pointers to NULL-terminated strings?
Also where can I find details on what open(), close(), read(), and wirte() expect (mainly in regards to file operations).
Thanks,
-Ian
Newlib questions
Newlib questions
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Re: Newlib questions
AFAIR, reverse order... but there are certainly ASM gurus around that can answer that one with confidence.ReturnInfinity wrote:In crt0.s (or .c) I want to push argc and argv onto the stack. In what order is this done?
Yep, an array of C strings it is. (After all, that's what main() expects to find on the stack, now is it?)More importantly what format should argv be in? Pointers to NULL-terminated strings?
Don't understand this as a RTFM reply, but I actually recommend the corresponding man pages. They are quite detailed, and sometimes point out holes in the generic POSIX docs (which would be the "standard" documentation otherwise.)Also where can I find details on what open(), close(), read(), and write() expect (mainly in regards to file operations).
Every good solution is obvious once you've found it.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Newlib questions
At least on 32 bit x86 (assuming cdecl), that's definitely the case. For cdecl, the leftmost argument to the C function should be on the top of the stack before the "call". Note that if you're compiling your C code with GCC 4.5 or above, the stack has to be 16-byte aligned for the C function.Solar wrote:AFAIR, reverse order... but there are certainly ASM gurus around that can answer that one with confidence.ReturnInfinity wrote:In crt0.s (or .c) I want to push argc and argv onto the stack. In what order is this done?
Edit: Hmm, maybe x86_64 doesn't use cdecl at all... based on this, it could be that GCC uses a register calling convention. I would check your compiler's ABI documentation to be sure.
Edit 2: fixed link.
Last edited by NickJohnson on Fri Sep 30, 2011 1:14 pm, edited 1 time in total.
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Newlib questions
GCC uses the platform's calling convention. For most platforms, this is defined by the SystemV ABI. For Win32+ (AKA Win64, because Microsoft have a thing against putting the letters "64" in anything 64-bit) a different (simpler, but less efficient) register calling convention is used
Re: Newlib questions
Thanks for the replies everyone. I tried passing the values (argc, and the argv[*] pointers) via the stack but it didn't work. I also took a look at the System V ABI and even it shows pushing the values to the stack. I traced the program execution under Bochs and it looks like it is pulling out the values using a different method.
On program start I need to put argc in the RDI register. argv[*] requires a pointer table with the table address in RSI.
Not what I was expecting but it works correctly now. I don't know if this is a newlib thing or it could be due to me using flat binaries for the compiled programs.
As for open(), read(), etc I will take a look at the POSIX docs.
Thanks,
-Ian
On program start I need to put argc in the RDI register. argv[*] requires a pointer table with the table address in RSI.
Not what I was expecting but it works correctly now. I don't know if this is a newlib thing or it could be due to me using flat binaries for the compiled programs.
As for open(), read(), etc I will take a look at the POSIX docs.
Thanks,
-Ian
BareMetal OS - http://www.returninfinity.com/
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Mono-tasking 64-bit OS for x86-64 based computers, written entirely in Assembly
Re: Newlib questions
According to my version of the System V ABI (AMD64 processor supplement) the behaviour you are seeing is entirely expected. See section 3.2.3 'Parameter passing'. Arguments of type _Bool, char, short, int, long, long long (all signed + unsigned) and pointers are in the 'INTEGER' class. Arguments in this class are passed in the order rdi, rsi, rdx, rcx, r8 and r9 and then finally on the stack once these registers are used up.
Regards,
John.
Regards,
John.