jart / cosmopolitan

build-once run-anywhere c library
ISC License
17.84k stars 610 forks source link

Unable to compile example hello.c from the website due to linking errors #22

Closed Phate6660 closed 3 years ago

Phate6660 commented 3 years ago
$ gcc -g -Os -static -fno-pie -mno-red-zone -nostdlib -nostdinc -o hello.com hello.c \
-Wl,--oformat=binary -Wl,--gc-sections -Wl,-z,max-page-size=0x1000 \
-Wl,-T,ape.lds -include cosmopolitan.h crt.o ape.o cosmopolitan.a
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: error: ape.lds:57:21: syntax error, unexpected '('
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: fatal error: unable to parse script file ape.lds
collect2: error: ld returned 1 exit status

This is with using the files from the provided zip.

It also fails when using files compiled from this repo.

jart commented 3 years ago

Does it work if you pass the flag -fuse-ld=bfd? If so, then the issue might be that your distro is configured to use the Gold linker. Could you provide details on how your linker came to be configured that way by default?

Phate6660 commented 3 years ago

Yep, that was the issue. I personally set it to Gold linker because I prefer it and rarely have issues with it. Don't know why I didn't think to try with bfd first before opening the issue.

jart commented 3 years ago

The root cause seems to be that Gold doesn't support SORT_NONE which is sad. Google did a great job contributing the Gold linker long ago, but they probably stopped maintaining it when they switched to LLVM. No one's happy with BFD's performance but it is certainly the most dependable and featureful linker.