nostdlib

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
belhifet
Posts: 4
Joined: Fri Oct 20, 2006 10:02 pm

nostdlib

Post by belhifet »

1. My C program code is:
main(){}

Is there a way for it to work without linking with glibc?

2. Also, for the program code:
void main(){puts("hello");}

Where puts is not the standard function of stdio.h, but my own.
What parameters do I need to include with gcc, so that it does use my own and ignores the stdio one?

My OS is x86 Linux.
cg123
Member
Member
Posts: 41
Joined: Wed Sep 27, 2006 2:34 pm

Post by cg123 »

Try -nostdinc -fno-builtin.
User avatar
kataklinger
Member
Member
Posts: 381
Joined: Fri Nov 04, 2005 12:00 am
Location: Serbia

Post by kataklinger »

Read OsFaq!

oprion is: -nostdlib or something like that!
belhifet
Posts: 4
Joined: Fri Oct 20, 2006 10:02 pm

Post by belhifet »

cg123 that worked, but it had no effect on the file size... and diff shows no differences between the two executables. :? How is that possible?

kataklinger: I already tried -nostdlib but ld gave me the following error:
warning: cannot find entry symbol _start; defaulting to 0000000008048094
Why?
JJeronimo
Member
Member
Posts: 202
Joined: Wed Oct 18, 2006 3:29 pm

Post by JJeronimo »

belhifet wrote: kataklinger: I already tried -nostdlib but ld gave me the following error:
warning: cannot find entry symbol _start; defaulting to 0000000008048094
Why?
That's because, on normal programs, ld automatically links your program to a especial initialization routine, indicated by the _start symbol, that calls your main function...

It *calls*, not jumps!
The evidence of this is that UNIX puts the argv pointer on top of the stack (and the argv long int just below that) with no return address after it... So if you access argv and argc as main()'s arguments, it's logical that you have a "call" and not a jump...
Also, note that you terminate a C program by returning from main() and the return value is passed to the parent as the error code... That's because, just below the call instruction, there must be a sequence that calls the exit system call with the error code returned by main()...

I don't know what package it belongs to, but it exists... you can find the object file by compiling an (invalid) usermode C program that has no main()... the error will talk about an object with an unresolved reference..., but I don't know where the source code is...

JJ
User avatar
kataklinger
Member
Member
Posts: 381
Joined: Fri Nov 04, 2005 12:00 am
Location: Serbia

Post by kataklinger »

It is entry point into your executable. Define that symbol where you want to begin execution of kernel. I don't know how your bootloader (is it GRUB?) handles executable files.

Code: Select all


global _start
_start:
    ; begining of kernel

User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

@ Joao:

It's the C runtime you are talking about. Yes, C requires a runtime environment... not only for the argc / argv handling you mentioned. Every "hosted" application (i.e., everything running in user space) needs quite a couple of things happening before main() is even entered. You need a function pointer vector of signal handlers. You need your malloc() control structures initialized. You need the stdin, stdout and stderr streams set up, and the default locale loaded. You need another function pointer vector where you can register functions with atexit(), and you need code that actually calls those functions when exit() or the end of main() is reached.

The file, if I remember correctly, is named crt0.o, and it is part of GCC, not glibc. You can avoid all this with the option --freestanding, but you wouldn't end up with a "working" application in the classical sense. ;-)

@ belhifet:

AFAICT, glibc would only get linked in if you use "standard"-named functions (like puts()), and don't define them yourself.

-nostdinc suppresses the searching of the "standard" include paths when looking for <someheader.h>. Only what you specify yourself using -I gets searched.

-nostartfiles suppresses linking of the runtime I mentioned above. You will have to provide your own _start.

-nodefaultlibs suppresses linking of the "standard" library object files. Only what you specify yourself gets passed to the linker. Note that GCC generates calls to memory functions (memset, memcmp, memcpy, memmove) by itself, so you have to provide these. Also not linked in is libgcc.a, which provides quite some support functions (e.g. for C++). You might want to do -nodefaultlibs -lgcc, to get no glibc object files but still have libgcc.a.

-nostdlib is equivalent to -nostartfiles -nodefaultlibs.

Hope this helps.
Every good solution is obvious once you've found it.
Post Reply