Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -440,6 +440,11 @@ if (errorCount()) return; + // The Target instance handles target-specific stuff, such as applying + // relocations or writing a PLT section. It also contains target-dependent + // values such as a default image base address. + Target = getTarget(); + switch (Config->EKind) { case ELF32LEKind: link(Args); @@ -1616,11 +1621,6 @@ if (Config->Strip != StripPolicy::None) llvm::erase_if(InputSections, [](InputSectionBase *S) { return S->Debug; }); - // The Target instance handles target-specific stuff, such as applying - // relocations or writing a PLT section. It also contains target-dependent - // values such as a default image base address. - Target = getTarget(); - Config->EFlags = Target->calcEFlags(); Config->MaxPageSize = getMaxPageSize(Args); Config->ImageBase = getImageBase(Args); Index: test/ELF/undef-broken-debug.test =================================================================== --- test/ELF/undef-broken-debug.test +++ test/ELF/undef-broken-debug.test @@ -8,6 +8,10 @@ # CHECK: error: {{.*}}.o: relocation R_X86_64_64 at 0x29 has unsupported target # CHECK: error: undefined symbol: bar +# We used to dereference null Target in DWARF.cpp:findAux while reporting a duplicate symbol error, +# because Target wasn't initialized yet. +# RUN: not ld.lld %t.o %t.o -o /dev/null + --- !ELF FileHeader: Class: ELFCLASS64