Skip to content

Commit

Permalink
[ELF] Use compression::getReasonIfUnsupported for zlib/zstd unavailab…
Browse files Browse the repository at this point in the history
…le error

The error message now matches llvm-objcopy --compress-debug-sections=[zlib|zstd].
  • Loading branch information
MaskRay committed Jul 6, 2023
1 parent 4b36b2c commit b7d36a1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
30 changes: 15 additions & 15 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,21 +1013,19 @@ template <class ELFT> static void readCallGraphsFromObjectFiles() {
}
}

static DebugCompressionType getCompressDebugSections(opt::InputArgList &args) {
StringRef s = args.getLastArgValue(OPT_compress_debug_sections, "none");
if (s == "zlib") {
if (!compression::zlib::isAvailable())
error("--compress-debug-sections: zlib is not available");
return DebugCompressionType::Zlib;
}
if (s == "zstd") {
if (!compression::zstd::isAvailable())
error("--compress-debug-sections: zstd is not available");
return DebugCompressionType::Zstd;
static DebugCompressionType getCompressionType(StringRef s, StringRef option) {
DebugCompressionType type = StringSwitch<DebugCompressionType>(s)
.Case("zlib", DebugCompressionType::Zlib)
.Case("zstd", DebugCompressionType::Zstd)
.Default(DebugCompressionType::None);
if (type == DebugCompressionType::None) {
if (s != "none")
error("unknown " + option + " value: " + s);
} else if (const char *reason = compression::getReasonIfUnsupported(
compression::formatFor(type))) {
error(option + ": " + reason);
}
if (s != "none")
error("unknown --compress-debug-sections value: " + s);
return DebugCompressionType::None;
return type;
}

static StringRef getAliasSpelling(opt::Arg *arg) {
Expand Down Expand Up @@ -1148,7 +1146,9 @@ static void readConfigs(opt::InputArgList &args) {
config->checkSections =
args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
config->chroot = args.getLastArgValue(OPT_chroot);
config->compressDebugSections = getCompressDebugSections(args);
config->compressDebugSections = getCompressionType(
args.getLastArgValue(OPT_compress_debug_sections, "none"),
"--compress-debug-sections");
config->cref = args.hasArg(OPT_cref);
config->optimizeBBJumps =
args.hasFlag(OPT_optimize_bb_jumps, OPT_no_optimize_bb_jumps, false);
Expand Down
12 changes: 12 additions & 0 deletions lld/test/ELF/compress-sections-err.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# REQUIRES: x86
# UNSUPPORTED: zlib

# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
# RUN: ld.lld %t.o --compress-debug-sections=zlib --compress-debug-sections=none -o /dev/null 2>&1 | count 0
# RUN: not ld.lld %t.o --compress-debug-sections=zlib -o /dev/null 2>&1 | \
# RUN: FileCheck %s --implicit-check-not=error:

# CHECK: error: --compress-debug-sections: LLVM was not built with LLVM_ENABLE_ZLIB or did not find zlib at build time

.globl _start
_start:

0 comments on commit b7d36a1

Please sign in to comment.