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

figure out why unused functions from builtin.a are not getting omitted from final binary #2062

Closed
andrewrk opened this issue Mar 14, 2019 · 7 comments
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals.
Milestone

Comments

@andrewrk
Copy link
Member

I'm pretty sure when I solved #54 it was working but now if I do this:

$ ./zig build-exe ../examples/hello_world/hello.zig --release-small
$ objdump -d hello
...
0000000000201a40 <sqrt>:
...
0000000000201bf0 <sqrtf>:
...

That's not supposed to be in there. sqrt (among others) isn't called from this application.

On the other hand functions from compiler_rt.a are correctly getting omitted.

Maybe we should do -ffunction-sections?

@andrewrk andrewrk added this to the 0.5.0 milestone Mar 14, 2019
@andrewrk andrewrk added the contributor friendly This issue is limited in scope and/or knowledge of Zig internals. label Mar 14, 2019
@justinbalexander
Copy link
Contributor

It seems to happen only in release-safe and release-fast modes for me when compiling for
armv7em-freestanding-eabihf.

In debug and release small modes, there are no calls to any compiler-rt functions that I can see.

@andrewrk
Copy link
Member Author

Note for webassembly what @shritesh found: #2248 (comment)

@justinbalexander
Copy link
Contributor

justinbalexander commented May 12, 2019

I just pulled the latest zig and my freestanding arm project won't build anymore:

lld: error: undefined symbol: __zig_probe_stack
>>> referenced by debug.zig:1822 (/home/alexander/Code/zig/build/lib/zig/std/debug.zig:1822)
>>>               /home/alexander/Code/msp432-tiva-zig/zig-cache/o/siMKlSLAjLobrJZ7TlKJUPyWP9vPyHsp5b0NmB2ajmfniXlyxalD_J1uFQo1EQ7b/build.o:(std.debug.getLineNumberInfoDwarf)

I think it is related to this issue.

Also, unlike my previous comment, this error happens in all build modes.

EDIT: nevermind, build issue... sorry for the noise

@FireFox317
Copy link
Contributor

FireFox317 commented Jun 6, 2019

I'm pretty sure that the problem is that all the different compiler_rt functions all end up in one object file (compiler_rt.o). In that case the linker has no option to link the complete object and thus end up with all the unnecessary code in the final executable.

Instead to solve this problem all the different zig files in the compiler_rt should end up in a separate object file inside the libcompiler_rt.a This way the linker can choose the correct object file.

I found this by doing ar -t on the libcompiler_rt.a in the .local/share/zig cache folder and by doing the same command on the libclang_rt.a file.

Edit: I'm gonna try to make a PR for this, if I get a confirmation that this is the actual problem. I'm quite sure tho.

@FireFox317
Copy link
Contributor

@andrewrk I would like to solve this, because it's pretty annoying when build stuff for small embedded microcontrollers. However, I'm not sure if I should implement -ffunction-sections or go with the method described above?

@andrewrk
Copy link
Member Author

I would try -ffunction-sections first since in theory it should just be an option provided to LLVM. Getting compiler_rt to be more than 1 object file would be a lot more work and probably make compilation slower.

@andrewrk
Copy link
Member Author

andrewrk commented Jul 3, 2019

Landed in 372b615. Thanks @FireFox317! Hello World on Linux is now 6 KB after being stripped of debug info:

[nix-shell:~/downloads/zig/build]$ ./zig build-exe ../example/hello_world/hello.zig --release-small --strip --single-threaded

[nix-shell:~/downloads/zig/build]$ ls -ahl hello
-rwxr-xr-x 1 andy users 9.8K Jul  3 15:49 hello

[nix-shell:~/downloads/zig/build]$ strip hello # zig's --strip isn't fully complete yet

[nix-shell:~/downloads/zig/build]$ ls -ahl hello
-rwxr-xr-x 1 andy users 6.0K Jul  3 15:49 hello

[nix-shell:~/downloads/zig/build]$ ./hello
Hello, world!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals.
Projects
None yet
Development

No branches or pull requests

3 participants