diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -1013,7 +1013,7 @@ // FIXME: There can be more than one compile unit per object file. See // PR48637. auto it = units.begin(); - compileUnit = it->get(); + compileUnit = it != units.end() ? it->get() : nullptr; } ArrayRef ObjFile::getDataInCode() const { diff --git a/lld/test/MachO/dwarf-no-compile-unit.s b/lld/test/MachO/dwarf-no-compile-unit.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/dwarf-no-compile-unit.s @@ -0,0 +1,15 @@ +# REQUIRES: aarch64 + +## Check that LLD does not crash if it encounters DWARF sections +## without __debug_info compile unit DIEs being present. + +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.o +# RUN: %lld -arch arm64 %t.o -o /dev/null + +.text +.globl _main +_main: + ret + +.section __DWARF,__debug_abbrev,regular,debug + .byte 0 diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -444,6 +444,16 @@ findNextValid(); return *this; } + + decltype(auto) operator*() const { + assert(BaseT::wrapped() != End && "Cannot dereference end iterator!"); + return BaseT::operator*(); + } + + decltype(auto) operator->() const { + assert(BaseT::wrapped() != End && "Cannot dereference end iterator!"); + return BaseT::operator->(); + } }; /// Specialization of filter_iterator_base for forward iteration only.