g++ main function problem
Posted: Sun Dec 01, 2013 7:51 am
Hi,
I have been converting the few things I implemented for my OS from assembly to C++ and everything works except one thing I can't figure out how to work around. As you all know, if I don't use "-fwhole-program" I will get a bunch of unused functions because g++ assumes that the program may be a library and needs to export functions, so the small functions that got inlined will have an unused base function just laying around.
On one hand I can start my code with a random named function like "loader" or something but I can't use "-fwhole-program" because it will assume all my code as unused and erases it all so I get an empty binary.
On the the other hand I can call my start function main and "-fwhole-program" works wonderfully as it is supposed to but at the beginning of the binary (2nd instruction right after sub rsp) either I get a "call qword 0x9" and it breaks the stack (which can be fixed by inline asm "add $8, %rsp") or I get a "lea rax,[rel 0xb]" "call rax" if I use "-mcmodel=large" like I want to and an infinite loop will begin, calling that call itself until rsp is negative and canonical address exception happens. Is there a way to tell g++ to not put a useless code breaking call at the start of the "main" function?
Cheers,
Luís
I have been converting the few things I implemented for my OS from assembly to C++ and everything works except one thing I can't figure out how to work around. As you all know, if I don't use "-fwhole-program" I will get a bunch of unused functions because g++ assumes that the program may be a library and needs to export functions, so the small functions that got inlined will have an unused base function just laying around.
On one hand I can start my code with a random named function like "loader" or something but I can't use "-fwhole-program" because it will assume all my code as unused and erases it all so I get an empty binary.
On the the other hand I can call my start function main and "-fwhole-program" works wonderfully as it is supposed to but at the beginning of the binary (2nd instruction right after sub rsp) either I get a "call qword 0x9" and it breaks the stack (which can be fixed by inline asm "add $8, %rsp") or I get a "lea rax,[rel 0xb]" "call rax" if I use "-mcmodel=large" like I want to and an infinite loop will begin, calling that call itself until rsp is negative and canonical address exception happens. Is there a way to tell g++ to not put a useless code breaking call at the start of the "main" function?
Cheers,
Luís