[lld] r360305 - [ELF] Initialize Target before it may be dereferenced by findAux when reporting "duplicate symbol" error

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed May 8 18:45:53 PDT 2019


Author: maskray
Date: Wed May  8 18:45:53 2019
New Revision: 360305

URL: http://llvm.org/viewvc/llvm-project?rev=360305&view=rev
Log:
[ELF] Initialize Target before it may be dereferenced by findAux when reporting "duplicate symbol" error

    for (InputFile *F : Files)
      Symtab->addFile<ELFT>(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<Defined>(&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

Modified:
    lld/trunk/ELF/Driver.cpp
    lld/trunk/test/ELF/undef-broken-debug.test

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=360305&r1=360304&r2=360305&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Wed May  8 18:45:53 2019
@@ -441,6 +441,11 @@ void LinkerDriver::main(ArrayRef<const c
   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<ELF32LE>(Args);
@@ -1617,11 +1622,6 @@ template <class ELFT> void LinkerDriver:
   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);

Modified: lld/trunk/test/ELF/undef-broken-debug.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/undef-broken-debug.test?rev=360305&r1=360304&r2=360305&view=diff
==============================================================================
--- lld/trunk/test/ELF/undef-broken-debug.test (original)
+++ lld/trunk/test/ELF/undef-broken-debug.test Wed May  8 18:45:53 2019
@@ -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




More information about the llvm-commits mailing list