From bf355ffd89d51cae09bf5dbd1abe736afe4aee58 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Wed, 25 Sep 2019 10:14:50 +0000 Subject: [PATCH] [llvm-readobj] - Don't crash when dumping .stack_sizes and unable to find a relocation resolver. The crash might happen when we have either a broken or unsupported object and trying to resolve relocations when dumping the .stack_sizes section. For the test case I used a 32-bits ELF header and a 64-bit relocation. In this case a null pointer is returned by the code instead of the relocation resolver function and then we crash. Differential revision: https://reviews.llvm.org/D67962 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@372838 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/tools/llvm-readobj/stack-sizes.test | 26 ++++++++++++++++++++++++ tools/llvm-readobj/ELFDumper.cpp | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/test/tools/llvm-readobj/stack-sizes.test b/test/tools/llvm-readobj/stack-sizes.test index a3d5f929412..46eac7b2489 100644 --- a/test/tools/llvm-readobj/stack-sizes.test +++ b/test/tools/llvm-readobj/stack-sizes.test @@ -597,3 +597,29 @@ Symbols: Value: 0x10 Type: STT_FUNC Binding: STB_GLOBAL + +## Check that we report an error when we are unable to resolve a relocation for a given ELF architecture. +## Here we have a 64-bit relocation used in a 32-bit object. + +# RUN: yaml2obj --docnum=12 %s > %t17 +# RUN: not llvm-readelf --stack-sizes %t17 2>&1 | FileCheck %s -DFILE=%t17 --check-prefix=UNSUPPRELOC2 +# RUN: not llvm-readobj --stack-sizes %t17 2>&1 | FileCheck %s -DFILE=%t17 --check-prefix=UNSUPPRELOC2 + +# UNSUPPRELOC2: error: '[[FILE]]': unsupported relocation type in section .rela.stack_sizes: R_X86_64_64 + +--- !ELF +FileHeader: + Class: ELFCLASS32 + Data: ELFDATA2MSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .stack_sizes + Type: SHT_PROGBITS + Content: "00" + - Name: .rela.stack_sizes + Type: SHT_RELA + Info: .stack_sizes + Relocations: + - Offset: 0 + Type: R_X86_64_64 diff --git a/tools/llvm-readobj/ELFDumper.cpp b/tools/llvm-readobj/ELFDumper.cpp index 17d85c27ae0..e8fdf22c7b6 100644 --- a/tools/llvm-readobj/ELFDumper.cpp +++ b/tools/llvm-readobj/ELFDumper.cpp @@ -4935,7 +4935,7 @@ void DumpStyle::printRelocatableStackSizes( auto Contents = unwrapOrError(this->FileName, StackSizesSec.getContents()); DataExtractor Data(Contents, Obj->isLittleEndian(), sizeof(Elf_Addr)); for (const RelocationRef &Reloc : RelocSec.relocations()) { - if (!IsSupportedFn(Reloc.getType())) + if (!IsSupportedFn || !IsSupportedFn(Reloc.getType())) reportError(createStringError( object_error::parse_failed, "unsupported relocation type in section %s: %s", -- 2.40.0