Skip to content

Commit ccb4124

Browse files
committed
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. Differential Revision: https://reviews.llvm.org/D87321
1 parent 7dcd004 commit ccb4124

File tree

8 files changed

+65
-7
lines changed

8 files changed

+65
-7
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
351351

352352
std::string Linker = getToolChain().GetProgramPath(getShortName());
353353
ArgStringList CmdArgs;
354+
addLinkerCompressDebugSectionsOption(getToolChain(), Args, CmdArgs);
354355
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
355356
CmdArgs.push_back("-shared");
356357
CmdArgs.push_back("-o");

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
214214
}
215215
}
216216

217+
void tools::addLinkerCompressDebugSectionsOption(
218+
const ToolChain &TC, const llvm::opt::ArgList &Args,
219+
llvm::opt::ArgStringList &CmdArgs) {
220+
// GNU ld supports --compress-debug-sections=none|zlib|zlib-gnu|zlib-gabi
221+
// whereas zlib is an alias to zlib-gabi. Therefore -gz=none|zlib|zlib-gnu
222+
// are translated to --compress-debug-sections=none|zlib|zlib-gnu.
223+
// -gz is not translated since ld --compress-debug-sections option requires an
224+
// argument.
225+
if (const Arg *A = Args.getLastArg(options::OPT_gz_EQ)) {
226+
StringRef V = A->getValue();
227+
if (V == "none" || V == "zlib" || V == "zlib-gnu")
228+
CmdArgs.push_back(Args.MakeArgString("--compress-debug-sections=" + V));
229+
else
230+
TC.getDriver().Diag(diag::err_drv_unsupported_option_argument)
231+
<< A->getOption().getName() << V;
232+
}
233+
}
234+
217235
void tools::AddTargetFeature(const ArgList &Args,
218236
std::vector<StringRef> &Features,
219237
OptSpecifier OnOpt, OptSpecifier OffOpt,

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
2727
const llvm::opt::ArgList &Args,
2828
llvm::opt::ArgStringList &CmdArgs, const JobAction &JA);
2929

30+
void addLinkerCompressDebugSectionsOption(const ToolChain &TC,
31+
const llvm::opt::ArgList &Args,
32+
llvm::opt::ArgStringList &CmdArgs);
33+
3034
void claimNoWarnArgs(const llvm::opt::ArgList &Args);
3135

3236
bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args,

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
556556

557557
bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
558558
bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
559+
addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
559560
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
560561
// The profile runtime also needs access to system libraries.
561562
getToolChain().addProfileRTLibs(Args, CmdArgs);

clang/lib/Driver/ToolChains/HIP.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
8989
if (C.getDriver().isSaveTempsEnabled())
9090
LldArgs.push_back("-save-temps");
9191

92+
addLinkerCompressDebugSectionsOption(TC, Args, LldArgs);
93+
9294
LldArgs.append({"-o", Output.getFilename()});
9395
for (auto Input : Inputs)
9496
LldArgs.push_back(Input.getFilename());
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// REQUIRES: zlib, amdgpu-registered-target
2+
3+
// RUN: %clang -### -target amdgcn-amd-amdhsa -gz=none -x assembler %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_NONE %s
4+
// RUN: %clang -### -target amdgcn-amd-amdhsa -gz=none %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_NONE %s
5+
// CHECK-OPT_GZ_EQ_NONE: {{".*clang.*".* "--compress-debug-sections=none"}}
6+
// CHECK-OPT_GZ_EQ_NONE: "--compress-debug-sections=none"
7+
8+
// RUN: %clang -### -target amdgcn-amd-amdhsa -gz=zlib -x assembler %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB %s
9+
// RUN: %clang -### -target amdgcn-amd-amdhsa -gz=zlib %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB %s
10+
// CHECK-OPT_GZ_EQ_ZLIB: {{".*clang.*".* "--compress-debug-sections=zlib"}}
11+
// CHECK-OPT_GZ_EQ_ZLIB: "--compress-debug-sections=zlib"
12+
13+
// RUN: %clang -### -target amdgcn-amd-amdhsa -gz=zlib-gnu -x assembler %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB_GNU %s
14+
// RUN: %clang -### -target amdgcn-amd-amdhsa -gz=zlib-gnu %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB_GNU %s
15+
// CHECK-OPT_GZ_EQ_ZLIB_GNU: {{".*clang.*".* "--compress-debug-sections=zlib-gnu"}}
16+
// CHECK-OPT_GZ_EQ_ZLIB_GNU: "--compress-debug-sections=zlib-gnu"

clang/test/Driver/compress.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@
1818
// RUN: %clang -### -fintegrated-as -gz -c %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ %s
1919
// CHECK-OPT_GZ: "--compress-debug-sections"
2020

21-
// RUN: %clang -### -fintegrated-as -gz=none -x assembler -c %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_NONE %s
22-
// RUN: %clang -### -fintegrated-as -gz=none -c %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_NONE %s
21+
// RUN: %clang -### -target x86_64-unknown-linux-gnu -gz=none -x assembler %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_NONE %s
22+
// RUN: %clang -### -target x86_64-unknown-linux-gnu -gz=none %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_NONE %s
23+
// CHECK-OPT_GZ_EQ_NONE: {{".*clang.*".* "--compress-debug-sections=none"}}
2324
// CHECK-OPT_GZ_EQ_NONE: "--compress-debug-sections=none"
2425

25-
// RUN: %clang -### -fintegrated-as -gz=zlib -x assembler -c %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB %s
26-
// RUN: %clang -### -fintegrated-as -gz=zlib -c %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB %s
26+
// RUN: %clang -### -target x86_64-unknown-linux-gnu -gz=zlib -x assembler %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB %s
27+
// RUN: %clang -### -target x86_64-unknown-linux-gnu -gz=zlib %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB %s
28+
// CHECK-OPT_GZ_EQ_ZLIB: {{".*clang.*".* "--compress-debug-sections=zlib"}}
2729
// CHECK-OPT_GZ_EQ_ZLIB: "--compress-debug-sections=zlib"
2830

29-
// RUN: %clang -### -fintegrated-as -gz=zlib-gnu -x assembler -c %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB_GNU %s
30-
// RUN: %clang -### -fintegrated-as -gz=zlib-gnu -c %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB_GNU %s
31+
// RUN: %clang -### -target x86_64-unknown-linux-gnu -gz=zlib-gnu -x assembler %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB_GNU %s
32+
// RUN: %clang -### -target x86_64-unknown-linux-gnu -gz=zlib-gnu %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_ZLIB_GNU %s
33+
// CHECK-OPT_GZ_EQ_ZLIB_GNU: {{".*clang.*".* "--compress-debug-sections=zlib-gnu"}}
3134
// CHECK-OPT_GZ_EQ_ZLIB_GNU: "--compress-debug-sections=zlib-gnu"
3235

3336
// RUN: %clang -### -fintegrated-as -gz=invalid -x assembler -c %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_INVALID %s
3437
// RUN: %clang -### -fintegrated-as -gz=invalid -c %s 2>&1 | FileCheck -check-prefix CHECK-OPT_GZ_EQ_INVALID %s
3538
// CHECK-OPT_GZ_EQ_INVALID: error: unsupported argument 'invalid' to option 'gz='
36-

clang/test/Driver/hip-gz-options.hip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// REQUIRES: zlib, clang-driver, amdgpu-registered-target
2+
3+
// RUN: %clang -### -target x86_64-unknown-linux-gnu \
4+
// RUN: --offload-arch=gfx906 %s -nogpulib -nogpuinc \
5+
// RUN: -ggdb -gz=zlib 2>&1 | FileCheck %s
6+
7+
// RUN: %clang -### -target x86_64-unknown-linux-gnu \
8+
// RUN: -fgpu-rdc --offload-arch=gfx906 %s -nogpulib -nogpuinc \
9+
// RUN: -ggdb -gz=zlib 2>&1 | FileCheck %s
10+
11+
// CHECK: {{".*clang.*" .* "--compress-debug-sections=zlib"}}
12+
// CHECK-DAG: {{".*lld.*" .* "--compress-debug-sections=zlib"}}
13+
// CHECK-DAG: {{".*clang.*" .* "--compress-debug-sections=zlib"}}
14+
// CHECK: "--compress-debug-sections=zlib"

0 commit comments

Comments
 (0)