Index: llvm/lib/Object/ELF.cpp =================================================================== --- llvm/lib/Object/ELF.cpp +++ llvm/lib/Object/ELF.cpp @@ -538,17 +538,15 @@ return ArrayRef(); } - if (Dyn.empty()) - // TODO: this error is untested. - return createError("invalid empty dynamic section"); - - if (DynSecSize % sizeof(Elf_Dyn) != 0) - // TODO: this error is untested. - return createError("malformed dynamic section"); - - if (Dyn.back().d_tag != ELF::DT_NULL) - // TODO: this error is untested. - return createError("dynamic sections must be DT_NULL terminated"); + if (!Dyn.empty()) { + if (DynSecSize % sizeof(Elf_Dyn) != 0) + // TODO: this error is untested. + return createError("malformed dynamic section"); + + if (Dyn.back().d_tag != ELF::DT_NULL) + // TODO: this error is untested. + return createError("dynamic sections must be DT_NULL terminated"); + } return Dyn; } Index: llvm/test/tools/llvm-objdump/elf-keep-debug-only.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-objdump/elf-keep-debug-only.test @@ -0,0 +1,27 @@ +# 'objcopy --only-keep-debug' will create objects with empty dynamic section, +# llvm-objcopy should not crash for these files. +# RUN: yaml2obj %s > %t +# RUN: llvm-objdump -p %t | FileCheck %s + +# CHECK: Dynamic Section: + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .dynamic + Type: SHT_DYNAMIC + Address: 0x1010 +ProgramHeaders: + - Type: PT_LOAD + VAddr: 0x1000 + Sections: + - Section: .dynstr + - Section: .dynamic + - Type: PT_DYNAMIC + VAddr: 0x1010 + Sections: + - Section: .dynamic