[llvm-objdump] Fix the Assertion failure when providing invalid --debug-vars or --dwarf values

As seen in https://bugs.llvm.org/show_bug.cgi?id=52213 llvm-objdump
asserts if either the --debug-vars or the --dwarf options are provided
with invalid values. As suggested, this fix adds use of a default value
to these options and errors when given bad input.

Differential Revision: https://reviews.llvm.org/D112183
This commit is contained in:
gbreynoo 2021-11-04 11:01:32 +00:00
parent 3d39612b3d
commit ced9287c2d
4 changed files with 40 additions and 22 deletions

View File

@ -8,26 +8,23 @@
## Generated with this compile command, with the source code in Inputs/debug.c:
## clang --target=arm--none-eabi -march=armv7-a -c debug.c -O1 -gdwarf-4 -S -o -
# RUN: llvm-mc -triple armv8a--none-eabi < %s -filetype=obj | \
# RUN: llvm-objdump - -d --debug-vars | \
# RUN: llvm-mc -triple armv8a--none-eabi < %s -filetype=obj -o %t.o
# RUN: llvm-objdump %t.o -d --debug-vars | \
# RUN: FileCheck %s --check-prefix=RAW --strict-whitespace
## Check that passing the default value for --debug-vars-indent (52) makes no
## change to the output.
# RUN: llvm-mc -triple armv8a--none-eabi < %s -filetype=obj | \
# RUN: llvm-objdump - -d --debug-vars --debug-vars-indent=52 | \
# RUN: llvm-objdump %t.o -d --debug-vars --debug-vars-indent=52 | \
# RUN: FileCheck %s --check-prefix=RAW --strict-whitespace
# RUN: llvm-mc -triple armv8a--none-eabi < %s -filetype=obj | \
# RUN: llvm-objdump - -d --debug-vars --debug-vars-indent=30 | \
# RUN: llvm-objdump %t.o -d --debug-vars --debug-vars-indent=30 | \
# RUN: FileCheck %s --check-prefix=INDENT --strict-whitespace
# RUN: llvm-mc -triple armv8a--none-eabi < %s -filetype=obj | \
# RUN: llvm-objdump - -d --debug-vars --no-show-raw-insn | \
# RUN: llvm-objdump %t.o -d --debug-vars --no-show-raw-insn | \
# RUN: FileCheck %s --check-prefix=NO-RAW --strict-whitespace
# RUN: llvm-mc -triple armv8a--none-eabi < %s -filetype=obj | \
# RUN: llvm-objdump - -d --debug-vars --no-show-raw-insn --line-numbers | \
# RUN: llvm-objdump %t.o -d --debug-vars --no-show-raw-insn --line-numbers | \
# RUN: FileCheck %s --check-prefix=LINE-NUMS --strict-whitespace
# RUN: mkdir -p %t/a
@ -39,12 +36,12 @@
## An optional argument to the --debug-vars= option can be used to switch
## between unicode and ascii output (with unicode being the default).
# RUN: llvm-mc -triple armv8a--none-eabi < %s -filetype=obj | \
# RUN: llvm-objdump - -d --debug-vars=unicode | \
# RUN: llvm-objdump %t.o -d --debug-vars=unicode | \
# RUN: FileCheck %s --check-prefix=RAW --strict-whitespace
# RUN: llvm-mc -triple armv8a--none-eabi < %s -filetype=obj | \
# RUN: llvm-objdump - -d --debug-vars=ascii | \
# RUN: llvm-objdump %t.o -d --debug-vars=ascii | \
# RUN: FileCheck %s --check-prefix=ASCII --strict-whitespace
# RUN: not llvm-objdump %t.o -d --debug-vars=bad_value 2>&1 | \
# RUN: FileCheck %s --check-prefix=ERROR
## Note that llvm-objdump emits tab characters in the disassembly, assuming an
## 8-byte tab stop, so these might not look aligned in a text editor.
@ -146,6 +143,8 @@
# ASCII-NEXT: c: 01 00 80 e2 add r0, r0, #1 |
# ASCII-NEXT: 10: 1e ff 2f e1 bx lr v
# ERROR: error: 'bad_value' is not a valid value for '--debug-vars='
.text
.syntax unified
.eabi_attribute 67, "2.09"

View File

@ -0,0 +1,12 @@
## Test invalid use of the --dwarf option.
# RUN: yaml2obj %s -o %t
# RUN: not llvm-objdump --dwarf=bad_value %t 2>&1 | FileCheck %s --check-prefix=ERROR
# ERROR: error: 'bad_value' is not a valid value for '--dwarf='
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_REL

View File

@ -2500,6 +2500,11 @@ static void parseIntArg(const llvm::opt::InputArgList &InputArgs, int ID,
}
}
static void invalidArgValue(const opt::Arg *A) {
reportCmdLineError("'" + StringRef(A->getValue()) +
"' is not a valid value for '" + A->getSpelling() + "'");
}
static std::vector<std::string>
commaSeparatedValues(const llvm::opt::InputArgList &InputArgs, int ID) {
std::vector<std::string> Values;
@ -2573,8 +2578,11 @@ static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
commaSeparatedValues(InputArgs, OBJDUMP_disassemble_symbols_EQ);
DisassembleZeroes = InputArgs.hasArg(OBJDUMP_disassemble_zeroes);
if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_dwarf_EQ)) {
DwarfDumpType =
StringSwitch<DIDumpType>(A->getValue()).Case("frames", DIDT_DebugFrame);
DwarfDumpType = StringSwitch<DIDumpType>(A->getValue())
.Case("frames", DIDT_DebugFrame)
.Default(DIDT_Null);
if (DwarfDumpType == DIDT_Null)
invalidArgValue(A);
}
DynamicRelocations = InputArgs.hasArg(OBJDUMP_dynamic_reloc);
FaultMapSection = InputArgs.hasArg(OBJDUMP_fault_map_section);
@ -2611,7 +2619,10 @@ static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_debug_vars_EQ)) {
DbgVariables = StringSwitch<DebugVarsFormat>(A->getValue())
.Case("ascii", DVASCII)
.Case("unicode", DVUnicode);
.Case("unicode", DVUnicode)
.Default(DVInvalid);
if (DbgVariables == DVInvalid)
invalidArgValue(A);
}
parseIntArg(InputArgs, OBJDUMP_debug_vars_indent_EQ, DbgIndent);

View File

@ -31,11 +31,7 @@ struct VersionEntry;
namespace objdump {
enum DebugVarsFormat {
DVDisabled,
DVUnicode,
DVASCII,
};
enum DebugVarsFormat { DVDisabled, DVUnicode, DVASCII, DVInvalid };
extern bool ArchiveHeaders;
extern int DbgIndent;