Exclude C runtime library from linking?

Hi,

I am writing my own little toy programming language and when I try to link the binary object file (.o file) it requires a _main symbol. I wonder if there is a way to exclude this, presumably, a C runtime requirement?

Is it safe to provide a stub _main symbol, or provide a the _main as the entry point to my own little runtime?

What is the correct way to invoke the linker with the appropriate flags?

What exactly are you trying to suppress here?

Consider this example:

% cat empty.c 
int main() {
    return 0;
}
% clang -c empty.c 
% ld empty.o -o empty -map map.txt
% otool -t -v empty
empty:
(__TEXT,__text) section
_main:
0000000100003f94    sub sp, sp, #0x10
0000000100003f98    mov w0, #0x0
0000000100003f9c    str wzr, [sp, #0xc]
0000000100003fa0    add sp, sp, #0x10
0000000100003fa4    ret
% cat map.txt 
# Path: empty
# Arch: arm64
# Object files:
[  0] linker synthesized
[  1] /Users/quinn/Test/empty.o
# Sections:
# Address   Size        Segment Section
0x100003F94 0x00000014  __TEXT  __text
0x100003FA8 0x00000058  __TEXT  __unwind_info
# Symbols:
# Address   Size        File  Name
0x100003F94 0x00000014  [  1] _main

Note This is Xcode 15.4 on macOS 14.5.

The linker hasn’t insert any extra code here. The only thing present is the main function generated by the Clang compiler. And you have to have that because, when building an executable, LC_MAIN has to point somewhere.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hey Quinn,

I am writing a small toy compiler based off of LLVM and trying to stay away from a C runtime library as a direct dependency, hence I am invoking the ld linker manually from within the compiler. When I generate the object file (for now just an empty one), I assume it's looking for _main entry point as it is required by the C runtime. Correct me if I am wrong, but the C runtime entry point starts with _start symbol, no?

I have also noticed that ld does not support static linking, which I find quite strange.

Exclude C runtime library from linking?
 
 
Q