Bug 253942

Summary: clang -gz=zlib does not pass --compress-debug-sections to linker
Product: Base System Reporter: Ed Maste <emaste>
Component: binAssignee: Ed Maste <emaste>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: ---    
Version: CURRENT   
Hardware: Any   
OS: Any   

Description Ed Maste freebsd_committer 2021-03-01 17:54:30 UTC
-gz=zlib compiler flag should enable compressed debug sections. It works as expected for object files, but does not pass the required flag to the linker to have them compressed in the final library/binary.

Version:

FreeBSD clang version 11.0.1 (git@github.com:llvm/llvm-project.git llvmorg-11.0.1-0-g43ff75f2c3fe)

Object file has the 'C' flag:

% cc -c -g -gz=zlib hello.c
% readelf -S hello.o | grep -A1 ' .debug_info'
  [ 6] .debug_info       PROGBITS         0000000000000000  000000c7
       0000000000000055  0000000000000000   C       0     0     8

Linked binary does not have the 'C' flag:

% cc -g -gz=zlib  hello.c -o hello
% readelf -S hello | grep -A1 ' .debug_info'
  [28] .debug_info       PROGBITS         0000000000000000  00001520
       0000000000000a91  0000000000000000           0     0     1

% Using gcc, the linked binary does have 'C' flag:

% gcc10 -g -gz=zlib  hello.c -o hello
% readelf -S hello | grep -A1 ' .debug_info'
  [24] .debug_info       PROGBITS         0000000000000000  0000150a
       00000000000004de  0000000000000000   C       0     0     1

Using -v to see the command line, gcc is automatically adding `-compress-debug-sections=zlib` to the linker invocation.
Comment 1 Ed Maste freebsd_committer 2021-03-01 18:15:48 UTC
It appears that clang 11 in the base system has this issue on Linux and FreeBSD.

Testing upstream I see that the flag is passed to the linker for --target=x86_64-unknown-linux-gnu but not for --target=x86_64-unknown-freebsd.

Compare upstream
clang/lib/Driver/ToolChains/Gnu.cpp
clang/lib/Driver/ToolChains/FreeBSD.cpp

freebsd::Linker::ConstructJob() needs to have added

  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
Comment 3 Ed Maste freebsd_committer 2021-03-02 03:24:30 UTC
https://reviews.llvm.org/D97752
Comment 4 Ed Maste freebsd_committer 2021-03-02 13:47:57 UTC
Now committed as https://reviews.llvm.org/rG462cf39a5c180621b56f7602270ce33eb7b68d23
Comment 5 commit-hook freebsd_committer 2021-03-09 14:30:05 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=c4dbe899aa08f60b66426ecf63b1346bffe25ed8

commit c4dbe899aa08f60b66426ecf63b1346bffe25ed8
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2021-03-03 01:35:57 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2021-03-09 14:28:54 +0000

    clang: Fix -gz=zlib options for linker

    Clang commit ccb4124a4172bf2cb2e1cd7c253f0f1654fce294:

    Fix -gz=zlib options for linker

    gcc translates -gz=zlib to --compress-debug-options=zlib for both
    assembler and linker but clang only does this for assembler.

    The linker needs --compress-debug-options=zlib option to compress the
    debug sections in the generated executable or shared library.

    Due to this bug, -gz=zlib has no effect on the generated executable or
    shared library.

    This patch fixes that.

    Clang commit 462cf39a5c180621b56f7602270ce33eb7b68d23:

    [Driver] Fix -gz=zlib options for linker also on FreeBSD

    ccb4124a4172 fixed translating -gz=zlib to --compress-debug-sections for
    linker invocation for several ToolChains, but omitted FreeBSD.

    PR:             253942
    Approved by:    dim
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D29028

    (cherry picked from commit 19587d742264c5caec33d218e9cea6eb78f6c6bb)

 .../clang/lib/Driver/ToolChains/AMDGPU.cpp             |  1 +
 .../clang/lib/Driver/ToolChains/CommonArgs.cpp         | 18 ++++++++++++++++++
 .../clang/lib/Driver/ToolChains/CommonArgs.h           |  4 ++++
 .../clang/lib/Driver/ToolChains/FreeBSD.cpp            |  1 +
 .../llvm-project/clang/lib/Driver/ToolChains/Gnu.cpp   |  1 +
 .../llvm-project/clang/lib/Driver/ToolChains/HIP.cpp   |  2 ++
 6 files changed, 27 insertions(+)