From bd086817f646a498d81a8114d269e2fa823a1eb3 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 9 May 2019 01:45:53 +0000 Subject: [PATCH] [ELF] Initialize Target before it may be dereferenced by findAux when reporting "duplicate symbol" error for (InputFile *F : Files) Symtab->addFile(F); // if there is a duplicate symbol error ... Target = getTarget(); When parsing .debug_info in the object file (for better diagnostics), DWARF.cpp findAux may dereference the null pointer Target auto *DR = dyn_cast(&File->getRelocTargetSym(Rel)); if (!DR) { // Broken debug info may point to a non-defined symbol, // some asan object files may also contain R_X86_64_NONE RelType Type = Rel.getType(Config->IsMips64EL); if (Type != Target->NoneRel) /// Target is null Move the assignment of Target to an earlier place to fix this. Reviewed By: ruiu Differential Revision: https://reviews.llvm.org/D61712 llvm-svn: 360305 --- lld/ELF/Driver.cpp | 10 +++++----- lld/test/ELF/undef-broken-debug.test | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 6799ea9..27f1864 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -441,6 +441,11 @@ void LinkerDriver::main(ArrayRef ArgsArr) { 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); @@ -1617,11 +1622,6 @@ template void LinkerDriver::link(opt::InputArgList &Args) { 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); diff --git a/lld/test/ELF/undef-broken-debug.test b/lld/test/ELF/undef-broken-debug.test index 444f3bf..2ca8494 100644 --- a/lld/test/ELF/undef-broken-debug.test +++ b/lld/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 -- 2.7.4