Index: lld/MachO/InputFiles.cpp =================================================================== --- lld/MachO/InputFiles.cpp +++ 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 { Index: lld/test/MachO/dwarf-no-compile-unit.s =================================================================== --- /dev/null +++ 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 Index: llvm/include/llvm/ADT/STLExtras.h =================================================================== --- llvm/include/llvm/ADT/STLExtras.h +++ 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.