28.5.1.1. nostartfiles programs
Assembly examples under nostartfiles
directories can use the standard library, but they don’t use the pre-main
boilerplate and start directly at our explicitly given _start
:
I’m not sure how much stdlib functionality is supposed to work without the pre-main stuff, but I guess we’ll just have to find out!
Was going to ask the following markdown question, but I noticed half way that:
-
without
-static
, I see a bunch of dynamic loader instructions, so not much is gained -
with
-static
, the program segfaults, including on the host with stack:#0 0x0000000000429625 in _IO_cleanup () #1 0x0000000000400c72 in __run_exit_handlers () #2 0x0000000000400caa in exit () #3 0x0000000000400a01 in _start () at exit.S:4
so I didn’t really have a good question.
The Markdown question that was almost asked:
When working in emulators, I often want to keep my workloads as small as possible to more easily study instruction traces and reproduce bugs. One of the ways I often want to do that, especially when doing [user mode simulations](https://wiki.debian.org/QemuUserEmulation), is by not running [the code that normally runs before main](https://stackoverflow.com/questions/53570678/what-happens-before-main-in-c) so that I can start directly in the instructions of interest that I control myself, which can be achieved with the `gcc -nostartfiles` option and by starting the program directly at `_start`. Here is a tiny example that calls just `exit` from the C standard library: main.S
global _start
_start: mov $0, %rdi call exit
Compile and run with:
gcc -ggdb3 -nostartfiles -static -o exit.out exit.S qemu-x86_64 -d in_asm exit.out
However, for programming convenience, and to potentially keep my examples more OS portable, I would like to avoid making raw system calls, which would of course work, by using C standard library functions instead. But I'm afraid that some of those C standard library functions will fail in subtle ways because I have skipped required initialization steps that would normally happen before `main`. Is it any easy to determine which functions I can use or not, in case there are any that I can't use?