1

So essentially I want to compile a c program statically with gcc, and I want it to be able to link c stdlib functions, but I want it to start at main, and not include the _start function as well as the libc init stuff that happens before main. Normally when you want to compile a program without _start, you run gcc with the -nostdlib flag, but I want to also be able to include code from stdlib, just not the libc init. Is there any way to do this?

I know that this could cause a lot of problems, but for my use case I'm not actually running the c program itself so it makes sense to do this.

Thanks in advance

5
  • 1
    You want to link (and probably call/execute) stdlib code while sabotaging its preconditions and "assumptions"? Sure? We might be looking at a meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented May 10, 2022 at 5:59
  • 1
    Did you try to add appropriate linker options as -lc? As with any other library, only unresolved references will pull modules from it. Commented May 10, 2022 at 6:12
  • 1
    @Yunnosch there might be perfectly legit reasons to not use the startup files of libc and still want to use a few libc functions. For exemple if you want to use libm (maths library) it requires a few symbols of libc. it works perfectly fine to link with -nostdlib -lm -lc Commented May 10, 2022 at 8:08
  • So there is a subset of stdlib which can be used without executing the startups? OK. Commented May 10, 2022 at 8:49
  • For background, I am trying to write a program that can create shellcode from arbitrary c code. Shellcode is hijacking the memory space of another process, so it doesn't need to do libc initialization itself. Commented May 10, 2022 at 16:05

1 Answer 1

1

The option -nostdlib tells the linker to not use the startup files (ie. the code that is executed before the main).

-nostdlib

Do not use the standard system startup files or libraries when linking. 
No startup files and only the libraries you specify are
passed to the linker, and options specifying linkage of the system
libraries, such as -static-libgcc or -shared-libgcc, are ignored.

The compiler may generate calls to memcmp, memset, memcpy and memmove. 
These entries are usually resolved by entries in libc. These
entry points should be supplied through some other mechanism when this
option is specified.

It is frequent to use this option in low-level bare-metal programming in order to control exactly what is going on.

You can still use the functions of your libc by using -lc. However keep in mind that some of the libc function depend on the startup code. For example in some implementations printf requires dynamic memory allocation and the heap is initialized by the startup code.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh okay thanks thats what I was looking for, I think -nostdlib -lc will accomplish my task

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.