Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selection of the linker executable #1067

Closed
pinicarus opened this issue Nov 11, 2023 · 6 comments
Closed

Allow selection of the linker executable #1067

pinicarus opened this issue Nov 11, 2023 · 6 comments
Assignees

Comments

@pinicarus
Copy link
Contributor

New feature

Some build environments may require the use of a particular linker.

As per https://github.com/c3lang/c3c/blob/master/wrapper/src/wrapper.cpp#L73, the choice currently only depends on the target format.

A new compiler flag (e.g. --linker=FILE) would allow to override the default choice in the switch.

Example usage

Cosmopolitan libc is an example of such build environment.

Simple C3 source:

module hello;

extern fn void printf(char*, ...);

fn int main() {
        printf("Hello, %s\n", "cosmo");
        return 0;
}

Build command: c3c compile --target elf-x64 --single-module=yes -o hello.com.dbg -z -static -z -nostdlib -z -no-pie -z -T -z ape.lds -z --gc-sections -z -z -z common-page-size=0x1000 -z -z -z max-page-size=0x1000 -z -z -z noexecstack -z crt.o -z ape-no-modify-self.o -z cosmopolitan.a hello.c3

ape.lds, crt.o, ape-no-modify-self.o and cosmopolitan.a files to be taken from the latest (3.0.2 at writing time) cosmopolitan amalgamation release.

Version used:

$ c3c --version
C3 Compiler Version (pre-alpha):   0.4.699
Installed directory:               /usr/bin/
LLVM version:                      16.0.6
LLVM default target:               x86_64-pc-linux-gnu

Error produced:

ld.lld: error: ape.lds:383: modulo by zero
ld.lld: error: ape.lds:383: modulo by zero
ld.lld: error: ape.lds:383: modulo by zero
Failed to create an executable: (null)

Example of different outcome based on the chosen linker:

ld.gold (from binutils 2.40)

$ ld.gold -o main.com.dbg hello.o std.atomic.o std.io.o std.io.os.o std.io.file.o std.core.string.conv.o std.core.string.o std.core.dstring.o std.core.mem.allocator.o std.core.types.o std.core.builtin.o std.core.mem.o libc.o libc.os.o std.math.o std.ascii.o -static -nostdlib -no-pie -T ape.lds --gc-sections -z common-page-size=0x1000 -z max-page-size=0x1000 -z noexecstack crt.o ape-no-modify-self.o cosmopolitan.a
ld.gold: error: ape.lds:3:17: syntax error, unexpected '|', expecting ')'
ld.gold: fatal error: unable to parse script file ape.lds

ld.bfd (from binutils 2.40)

$ ld.bfd -o main.com.dbg hello.o std.atomic.o std.io.o std.io.os.o std.io.file.o std.core.string.conv.o std.core.string.o std.core.dstring.o std.core.mem.allocator.o std.core.types.o std.core.builtin.o std.core.mem.o libc.o libc.os.o std.math.o std.ascii.o -static -nostdlib -no-pie -T ape.lds --gc-sections -z common-page-size=0x1000 -z max-page-size=0x1000 -z noexecstack crt.o ape-no-modify-self.o cosmopolitan.a
# Succeeds!

ld.lld (from lld 16.0.6)

$ ld.lld -o main.com.dbg hello.o std.atomic.o std.io.o std.io.os.o std.io.file.o std.core.string.conv.o std.core.string.o std.core.dstring.o std.core.mem.allocator.o std.core.types.o std.core.builtin.o std.core.mem.o libc.o libc.os.o std.math.o std.ascii.o -static -nostdlib -no-pie -T ape.lds --gc-sections -z common-page-size=0x1000 -z max-page-size=0x1000 -z noexecstack crt.o ape-no-modify-self.o cosmopolitan.a
ld.lld: error: ape.lds:383: modulo by zero
ld.lld: error: ape.lds:383: modulo by zero
ld.lld: error: ape.lds:383: modulo by zero

ld.mold (from mold 2.1.0)

$ ld.mold -o main.com.dbg hello.o std.atomic.o std.io.o std.io.os.o std.io.file.o std.core.string.conv.o std.core.string.o std.core.dstring.o std.core.mem.allocator.o std.core.types.o std.core.builtin.o std.core.mem.o libc.o libc.os.o std.math.o std.ascii.o -static -nostdlib -no-pie -T ape.lds --gc-sections -z common-page-size=0x1000 -z max-page-size=0x1000 -z noexecstack crt.o ape-no-modify-self.o cosmopolitan.a
mold: fatal: ape.lds:1: ENTRY(_start)
                 ^ unknown linker script token
@lerno
Copy link
Collaborator

lerno commented Nov 18, 2023

How should the compiler handle -l and -L args as they may be different in these linkers?

@pinicarus
Copy link
Contributor Author

pinicarus commented Nov 19, 2023

How should the compiler handle -l and -L args as they may be different in these linkers?

I would pass them along to the linker.
All four linkers I mentionned handle them the same way, and I guess any newcomer would play along in order to be compatible.
If not, the user could still do c3c compile -z -l ... -z -L ... instead of using these flags directly

lerno added a commit that referenced this issue Feb 14, 2024
…rides the need for `@` in macro names. Fixes to macro context evaluation. Updated allocator api. Removed install_win_reqs.bat. Deterministic @init for MacOS. Fixed temp memory issue with formatter. Support LLVM 19. Add support to compare bitstructs using == and !=. Support Windows `.def` files. Removed invalid grammar from grammar.y. Support compile time folding of &|^~ for bitstructs. `output` project setting now respected. Fix issue where constants were not properly constant folded. Add temp_push/pop. Aliased declarations caused errors when used in initializers. Fix export output. Fix of const ternary #1118. Fix of $$MODULE in nested macros #1117. Fix debug info on globals. `out` now correctly detects subscript[] use #1116. Lateral implicit imports removed. Default to '.' if no libdir is specified. Improved error messages for `--lib`. Fix raylib snake example. Overzealous local escape check corrected #1127. Improved yacc grammar #1128. `--linker` argument #1067. Fixes to the matrix operations #1130.
lerno added a commit that referenced this issue Feb 15, 2024
…rides the need for `@` in macro names. Fixes to macro context evaluation. Updated allocator api. Removed install_win_reqs.bat. Deterministic @init for MacOS. Fixed temp memory issue with formatter. Support LLVM 19. Add support to compare bitstructs using == and !=. Support Windows `.def` files. Removed invalid grammar from grammar.y. Support compile time folding of &|^~ for bitstructs. `output` project setting now respected. Fix issue where constants were not properly constant folded. Add temp_push/pop. Aliased declarations caused errors when used in initializers. Fix export output. Fix of const ternary #1118. Fix of $$MODULE in nested macros #1117. Fix debug info on globals. `out` now correctly detects subscript[] use #1116. Lateral implicit imports removed. Default to '.' if no libdir is specified. Improved error messages for `--lib`. Fix raylib snake example. Overzealous local escape check corrected #1127. Improved yacc grammar #1128. `--linker` argument #1067. Fixes to the matrix operations #1130. Added GenericList.
lerno added a commit that referenced this issue Feb 15, 2024
…rides the need for `@` in macro names. Fixes to macro context evaluation. Updated allocator api. Removed install_win_reqs.bat. Deterministic @init for MacOS. Fixed temp memory issue with formatter. Support LLVM 19. Add support to compare bitstructs using == and !=. Support Windows `.def` files. Removed invalid grammar from grammar.y. Support compile time folding of &|^~ for bitstructs. `output` project setting now respected. Fix issue where constants were not properly constant folded. Add temp_push/pop. Aliased declarations caused errors when used in initializers. Fix export output. Fix of const ternary #1118. Fix of $$MODULE in nested macros #1117. Fix debug info on globals. `out` now correctly detects subscript[] use #1116. Lateral implicit imports removed. Default to '.' if no libdir is specified. Improved error messages for `--lib`. Fix raylib snake example. Overzealous local escape check corrected #1127. Improved yacc grammar #1128. `--linker` argument #1067. Fixes to the matrix operations #1130. Added GenericList.
lerno added a commit that referenced this issue Feb 15, 2024
…rides the need for `@` in macro names. Fixes to macro context evaluation. Updated allocator api. Removed install_win_reqs.bat. Deterministic @init for MacOS. Fixed temp memory issue with formatter. Support LLVM 19. Add support to compare bitstructs using == and !=. Support Windows `.def` files. Removed invalid grammar from grammar.y. Support compile time folding of &|^~ for bitstructs. `output` project setting now respected. Fix issue where constants were not properly constant folded. Add temp_push/pop. Aliased declarations caused errors when used in initializers. Fix export output. Fix of const ternary #1118. Fix of $$MODULE in nested macros #1117. Fix debug info on globals. `out` now correctly detects subscript[] use #1116. Lateral implicit imports removed. Default to '.' if no libdir is specified. Improved error messages for `--lib`. Fix raylib snake example. Overzealous local escape check corrected #1127. Improved yacc grammar #1128. `--linker` argument #1067. Fixes to the matrix operations #1130. Added GenericList.
lerno added a commit that referenced this issue Feb 15, 2024
…rides the need for `@` in macro names. Fixes to macro context evaluation. Updated allocator api. Removed install_win_reqs.bat. Deterministic @init for MacOS. Fixed temp memory issue with formatter. Support LLVM 19. Add support to compare bitstructs using == and !=. Support Windows `.def` files. Removed invalid grammar from grammar.y. Support compile time folding of &|^~ for bitstructs. `output` project setting now respected. Fix issue where constants were not properly constant folded. Add temp_push/pop. Aliased declarations caused errors when used in initializers. Fix export output. Fix of const ternary #1118. Fix of $$MODULE in nested macros #1117. Fix debug info on globals. `out` now correctly detects subscript[] use #1116. Lateral implicit imports removed. Default to '.' if no libdir is specified. Improved error messages for `--lib`. Fix raylib snake example. Overzealous local escape check corrected #1127. Improved yacc grammar #1128. `--linker` argument #1067. Fixes to the matrix operations #1130. Added GenericList.
lerno added a commit that referenced this issue Feb 15, 2024
…rides the need for `@` in macro names. Fixes to macro context evaluation. Updated allocator api. Removed install_win_reqs.bat. Deterministic @init for MacOS. Fixed temp memory issue with formatter. Support LLVM 19. Add support to compare bitstructs using == and !=. Support Windows `.def` files. Removed invalid grammar from grammar.y. Support compile time folding of &|^~ for bitstructs. `output` project setting now respected. Fix issue where constants were not properly constant folded. Add temp_push/pop. Aliased declarations caused errors when used in initializers. Fix export output. Fix of const ternary #1118. Fix of $$MODULE in nested macros #1117. Fix debug info on globals. `out` now correctly detects subscript[] use #1116. Lateral implicit imports removed. Default to '.' if no libdir is specified. Improved error messages for `--lib`. Fix raylib snake example. Overzealous local escape check corrected #1127. Improved yacc grammar #1128. `--linker` argument #1067. Fixes to the matrix operations #1130. Added GenericList.
lerno added a commit that referenced this issue Feb 15, 2024
…rides the need for `@` in macro names. Fixes to macro context evaluation. Updated allocator api. Removed install_win_reqs.bat. Deterministic @init for MacOS. Fixed temp memory issue with formatter. Support LLVM 19. Add support to compare bitstructs using == and !=. Support Windows `.def` files. Removed invalid grammar from grammar.y. Support compile time folding of &|^~ for bitstructs. `output` project setting now respected. Fix issue where constants were not properly constant folded. Add temp_push/pop. Aliased declarations caused errors when used in initializers. Fix export output. Fix of const ternary #1118. Fix of $$MODULE in nested macros #1117. Fix debug info on globals. `out` now correctly detects subscript[] use #1116. Lateral implicit imports removed. Default to '.' if no libdir is specified. Improved error messages for `--lib`. Fix raylib snake example. Overzealous local escape check corrected #1127. Improved yacc grammar #1128. `--linker` argument #1067. Fixes to the matrix operations #1130. Added GenericList.
lerno added a commit that referenced this issue Feb 15, 2024
…rides the need for `@` in macro names. Fixes to macro context evaluation. Updated allocator api. Removed install_win_reqs.bat. Deterministic @init for MacOS. Fixed temp memory issue with formatter. Support LLVM 19. Add support to compare bitstructs using == and !=. Support Windows `.def` files. Removed invalid grammar from grammar.y. Support compile time folding of &|^~ for bitstructs. `output` project setting now respected. Fix issue where constants were not properly constant folded. Add temp_push/pop. Aliased declarations caused errors when used in initializers. Fix export output. Fix of const ternary #1118. Fix of $$MODULE in nested macros #1117. Fix debug info on globals. `out` now correctly detects subscript[] use #1116. Lateral implicit imports removed. Default to '.' if no libdir is specified. Improved error messages for `--lib`. Fix raylib snake example. Overzealous local escape check corrected #1127. Improved yacc grammar #1128. `--linker` argument #1067. Fixes to the matrix operations #1130. Added GenericList.
@lerno
Copy link
Collaborator

lerno commented Feb 15, 2024

This should now work in 0.5.4, try it out.

@lerno lerno self-assigned this Feb 15, 2024
@lerno lerno added the Enhancement Request New feature or request label Feb 15, 2024
@pinicarus
Copy link
Contributor Author

Selection of the linker workers in 0.5.4, but the example in this issue fails to compile because of the added -lm at the end of the linking command.

You can consider this issue solved, though, as linker selection does indeed work.

@lerno
Copy link
Collaborator

lerno commented Mar 15, 2024

@pinicarus is that still added now?

@pinicarus
Copy link
Contributor Author

Linker selection works

@lerno lerno removed the Enhancement Request New feature or request label Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants