Compile and run program without main() in C

前端 未结 2 1309
梦谈多话
梦谈多话 2020-12-22 23:51

I\'m trying to compile and run following program without main() function in C. I have compiled my program using the following command.



        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-22 23:53

    In C, when functions/subroutines are called the stack is populated as (in the order):

    1. The arguments,
    2. Return address,
    3. Local variables, --> top of the stack

    main() being the start point, ELF structures the program in such a way that whatever instructions comes first would get pushed first, in this case printfs are.

    Now, program is sort of truncated without return-address OR __end__ and infact it assumes that whatever is there on the stack at that(__end__) location is the return-address, but unfortunately its not and hence it crashes.

提交回复
热议问题