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

add -fstrip=debug_info command line option #22591

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

leroycep
Copy link
Contributor

This pull request adds a third option for -fstrip, -fstrip=debug_info. It also gives aliases to -fno-strip and -fstrip, -fstrip=none and -fstrip=all respectively.

-fstrip=debug_info is similar to objcopy --strip-debug, removing DWARF debug sections, but retaining the ELF symbol table.

Note that this pull request does not currently update the zig build system to expose this functionality.

See also #18520 and #22077

Trying out all 3 strip options on a test case.

Test case:

const std = @import("std");

noinline fn foo(x: u32) u32 {
    return x * x;
}

noinline fn bar() u32 {
    return foo(std.math.maxInt(u32));
}

pub fn main() !void {
    std.debug.print("{}", .{bar()});
}

Not stripping anything, equivalent to -fno-strip:

~/code/zig/build/example-elfsymtab-backtrace> ../stage4/bin/zig build-exe main.zig -fstrip=none -femit-bin=main-strip-none
~/code/zig/build/example-elfsymtab-backtrace> try { ./main-strip-none }
thread 396791 panic: integer overflow
/home/geemili/code/zig/build/example-elfsymtab-backtrace/main.zig:4:14: 0x103a6d2 in foo (main)
    return x * x;
             ^
/home/geemili/code/zig/build/example-elfsymtab-backtrace/main.zig:8:15: 0x1038acd in bar (main)
    return foo(std.math.maxInt(u32));
              ^
/home/geemili/code/zig/build/example-elfsymtab-backtrace/main.zig:12:32: 0x1038aa0 in main (main)
    std.debug.print("{}", .{bar()});
                               ^
/home/geemili/code/zig/lib/std/start.zig:656:37: 0x1038a62 in posixCallMainAndExit (main)
            const result = root.main() catch |err| {
                                    ^
/home/geemili/code/zig/lib/std/start.zig:271:5: 0x103863d in _start (main)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)

Stripping debug info but leaving symbols in:

~/code/zig/build/example-elfsymtab-backtrace> ../stage4/bin/zig build-exe main.zig -fstrip=debug_info -femit-bin=main-strip-debuginfo
~/code/zig/build/example-elfsymtab-backtrace> try { ./main-strip-debuginfo }
thread 396316 panic: integer overflow
Unwind information for `exe:0x1051fd4` was not available, trace may be incomplete

???:?:?: 0x103876f in ??? (exe)
???:?:?: 0x103708d in ??? (exe)
???:?:?: 0x1037060 in ??? (exe)
???:?:?: 0x1037029 in ??? (exe)
???:?:?: 0x1036d1d in ??? (exe)

Stripping debug info and symbol table, equivalent to -fstrip:

~/code/zig/build/example-elfsymtab-backtrace> ../stage4/bin/zig build-exe main.zig -fstrip=all -femit-bin=main-strip-all
~/code/zig/build/example-elfsymtab-backtrace> try { ./main-strip-all }
thread 397178 panic: integer overflow
Unable to dump stack trace: debug info stripped
Combining with #22077 (teaching std.debug to read the Elf symbol table)
~/code/zig/build/example-elfsymtab-backtrace> ../stage4/bin/zig build-exe main.zig -fstrip=debug_info -femit-bin=main-strip-debuginfo
~/code/zig/build/example-elfsymtab-backtrace> try { ./main-strip-debuginfo }
thread 397866 panic: integer overflow
???:?:?: 0x1039a1f in main.foo (???)
???:?:?: 0x103833d in main.bar (???)
???:?:?: 0x1038310 in main.main (???)
???:?:?: 0x10382d9 in start.posixCallMainAndExit (???)
???:?:?: 0x1037fcd in _start (???)
???:?:?: 0x0 in ??? (???)
This work is licensed on the same terms as the Zig project.

Copyright © 2025 TigerBeetle, Inc.

This code was written under contract for TigerBeetle. As a work made for hire, authorship and copyright goes to TigerBeetle.

Author certificate

LeRoyce Pearson <[email protected]> [TigerBeetle, Inc.]

This work is licensed on the same terms as this project (Zig).

@leroycep leroycep mentioned this pull request Jan 24, 2025
3 tasks
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

Successfully merging this pull request may close these issues.

1 participant