[ELF] - Do not fail on R_*_NONE relocations when parsing the debug info.

This is https://bugs.llvm.org//show_bug.cgi?id=38919.

Currently, LLD may report "unsupported relocation target while parsing debug info"
when parsing the debug information.

At the same time LLD does that for zeroed R_X86_64_NONE relocations,
which obviously has "invalid" targets.

The nature of R_*_NONE relocation assumes them should be ignored.
This patch teaches LLD to stop reporting the debug information parsing errors for them.

Differential revision: https://reviews.llvm.org/D52408

git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@343078 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/ELF/Arch/AArch64.cpp b/ELF/Arch/AArch64.cpp
index 59c8d15..7a0d28e 100644
--- a/ELF/Arch/AArch64.cpp
+++ b/ELF/Arch/AArch64.cpp
@@ -58,6 +58,7 @@
   RelativeRel = R_AARCH64_RELATIVE;
   IRelativeRel = R_AARCH64_IRELATIVE;
   GotRel = R_AARCH64_GLOB_DAT;
+  NoneRel = R_AARCH64_NONE;
   PltRel = R_AARCH64_JUMP_SLOT;
   TlsDescRel = R_AARCH64_TLSDESC;
   TlsGotRel = R_AARCH64_TLS_TPREL64;
diff --git a/ELF/Arch/AMDGPU.cpp b/ELF/Arch/AMDGPU.cpp
index 48b27f2..a7c6c84 100644
--- a/ELF/Arch/AMDGPU.cpp
+++ b/ELF/Arch/AMDGPU.cpp
@@ -35,6 +35,7 @@
 AMDGPU::AMDGPU() {
   RelativeRel = R_AMDGPU_RELATIVE64;
   GotRel = R_AMDGPU_ABS64;
+  NoneRel = R_AMDGPU_NONE;
   GotEntrySize = 8;
 }
 
diff --git a/ELF/Arch/ARM.cpp b/ELF/Arch/ARM.cpp
index 1fa2acd..13a04fe 100644
--- a/ELF/Arch/ARM.cpp
+++ b/ELF/Arch/ARM.cpp
@@ -51,6 +51,7 @@
   RelativeRel = R_ARM_RELATIVE;
   IRelativeRel = R_ARM_IRELATIVE;
   GotRel = R_ARM_GLOB_DAT;
+  NoneRel = R_ARM_NONE;
   PltRel = R_ARM_JUMP_SLOT;
   TlsGotRel = R_ARM_TLS_TPOFF32;
   TlsModuleIndexRel = R_ARM_TLS_DTPMOD32;
diff --git a/ELF/Arch/AVR.cpp b/ELF/Arch/AVR.cpp
index 02ac770..637da37 100644
--- a/ELF/Arch/AVR.cpp
+++ b/ELF/Arch/AVR.cpp
@@ -43,12 +43,15 @@
 namespace {
 class AVR final : public TargetInfo {
 public:
+  AVR();
   RelExpr getRelExpr(RelType Type, const Symbol &S,
                      const uint8_t *Loc) const override;
   void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
 };
 } // namespace
 
+AVR::AVR() { NoneRel = R_AVR_NONE; }
+
 RelExpr AVR::getRelExpr(RelType Type, const Symbol &S,
                         const uint8_t *Loc) const {
   return R_ABS;
diff --git a/ELF/Arch/Hexagon.cpp b/ELF/Arch/Hexagon.cpp
index 4d4bf25..afbf76d 100644
--- a/ELF/Arch/Hexagon.cpp
+++ b/ELF/Arch/Hexagon.cpp
@@ -36,6 +36,7 @@
 Hexagon::Hexagon() {
   // Hexagon Linux uses 64K pages by default.
   DefaultMaxPageSize = 0x10000;
+  NoneRel = R_HEX_NONE;
 }
 
 // Support V60 only at the moment.
diff --git a/ELF/Arch/Mips.cpp b/ELF/Arch/Mips.cpp
index 6fb4531..f6402f8 100644
--- a/ELF/Arch/Mips.cpp
+++ b/ELF/Arch/Mips.cpp
@@ -53,6 +53,7 @@
   PltEntrySize = 16;
   PltHeaderSize = 32;
   CopyRel = R_MIPS_COPY;
+  NoneRel = R_MIPS_NONE;
   PltRel = R_MIPS_JUMP_SLOT;
   NeedsThunks = true;
   TrapInstr = 0xefefefef;
diff --git a/ELF/Arch/PPC.cpp b/ELF/Arch/PPC.cpp
index 20cae0e..a01068c 100644
--- a/ELF/Arch/PPC.cpp
+++ b/ELF/Arch/PPC.cpp
@@ -29,6 +29,7 @@
 } // namespace
 
 PPC::PPC() {
+  NoneRel = R_PPC_NONE;
   GotBaseSymOff = 0x8000;
   GotBaseSymInGotPlt = false;
 }
diff --git a/ELF/Arch/PPC64.cpp b/ELF/Arch/PPC64.cpp
index 25dc8a6..eb01955 100644
--- a/ELF/Arch/PPC64.cpp
+++ b/ELF/Arch/PPC64.cpp
@@ -192,6 +192,7 @@
 
 PPC64::PPC64() {
   GotRel = R_PPC64_GLOB_DAT;
+  NoneRel = R_PPC64_NONE;
   PltRel = R_PPC64_JMP_SLOT;
   RelativeRel = R_PPC64_RELATIVE;
   IRelativeRel = R_PPC64_IRELATIVE;
diff --git a/ELF/Arch/RISCV.cpp b/ELF/Arch/RISCV.cpp
index 18a9c51..ef72722 100644
--- a/ELF/Arch/RISCV.cpp
+++ b/ELF/Arch/RISCV.cpp
@@ -21,6 +21,7 @@
 
 class RISCV final : public TargetInfo {
 public:
+  RISCV();
   virtual uint32_t calcEFlags() const override;
   RelExpr getRelExpr(RelType Type, const Symbol &S,
                      const uint8_t *Loc) const override;
@@ -29,6 +30,8 @@
 
 } // end anonymous namespace
 
+RISCV::RISCV() { NoneRel = R_RISCV_NONE; }
+
 static uint32_t getEFlags(InputFile *F) {
   if (Config->Is64)
     return cast<ObjFile<ELF64LE>>(F)->getObj().getHeader()->e_flags;
diff --git a/ELF/Arch/SPARCV9.cpp b/ELF/Arch/SPARCV9.cpp
index 36f5c83..831aa20 100644
--- a/ELF/Arch/SPARCV9.cpp
+++ b/ELF/Arch/SPARCV9.cpp
@@ -35,6 +35,7 @@
 SPARCV9::SPARCV9() {
   CopyRel = R_SPARC_COPY;
   GotRel = R_SPARC_GLOB_DAT;
+  NoneRel = R_SPARC_NONE;
   PltRel = R_SPARC_JMP_SLOT;
   RelativeRel = R_SPARC_RELATIVE;
   GotEntrySize = 8;
diff --git a/ELF/Arch/X86.cpp b/ELF/Arch/X86.cpp
index a0921cd..82246e6 100644
--- a/ELF/Arch/X86.cpp
+++ b/ELF/Arch/X86.cpp
@@ -48,6 +48,7 @@
 X86::X86() {
   CopyRel = R_386_COPY;
   GotRel = R_386_GLOB_DAT;
+  NoneRel = R_386_NONE;
   PltRel = R_386_JUMP_SLOT;
   IRelativeRel = R_386_IRELATIVE;
   RelativeRel = R_386_RELATIVE;
diff --git a/ELF/Arch/X86_64.cpp b/ELF/Arch/X86_64.cpp
index 3e7f04d..960bf78 100644
--- a/ELF/Arch/X86_64.cpp
+++ b/ELF/Arch/X86_64.cpp
@@ -55,6 +55,7 @@
 template <class ELFT> X86_64<ELFT>::X86_64() {
   CopyRel = R_X86_64_COPY;
   GotRel = R_X86_64_GLOB_DAT;
+  NoneRel = R_X86_64_NONE;
   PltRel = R_X86_64_JUMP_SLOT;
   RelativeRel = R_X86_64_RELATIVE;
   IRelativeRel = R_X86_64_IRELATIVE;
diff --git a/ELF/DWARF.cpp b/ELF/DWARF.cpp
index 2b62236..11c6105 100644
--- a/ELF/DWARF.cpp
+++ b/ELF/DWARF.cpp
@@ -16,6 +16,7 @@
 
 #include "DWARF.h"
 #include "Symbols.h"
+#include "Target.h"
 #include "lld/Common/Memory.h"
 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
 #include "llvm/Object/ELFObjectFile.h"
@@ -73,7 +74,10 @@
   // Broken debug info can point to a non-Defined symbol.
   auto *DR = dyn_cast<Defined>(&File->getRelocTargetSym(Rel));
   if (!DR) {
-    error("unsupported relocation target while parsing debug info");
+    RelType Type = Rel.getType(Config->IsMips64EL);
+    if (Type != Target->NoneRel)
+      error(toString(File) + ": relocation " + lld::toString(Type) + " at 0x" +
+            llvm::utohexstr(Rel.r_offset) + " has unsupported target");
     return None;
   }
   uint64_t Val = DR->Value + getAddend<ELFT>(Rel);
diff --git a/ELF/Target.h b/ELF/Target.h
index d48a9cd..1764963 100644
--- a/ELF/Target.h
+++ b/ELF/Target.h
@@ -95,6 +95,7 @@
 
   RelType CopyRel;
   RelType GotRel;
+  RelType NoneRel;
   RelType PltRel;
   RelType RelativeRel;
   RelType IRelativeRel;
diff --git a/test/ELF/debug-relocation-none.test b/test/ELF/debug-relocation-none.test
new file mode 100644
index 0000000..d22941d
--- /dev/null
+++ b/test/ELF/debug-relocation-none.test
@@ -0,0 +1,58 @@
+# REQUIRES: x86
+# RUN: yaml2obj %s -o %t.o
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
+
+## Previously we would report an error saying the relocation in .debug_info
+## has an unsupported target.
+## Check we do not report debug information parsing errors when relocation
+## used is of type R_*_NONE, what actually means it should be ignored.
+
+# CHECK-NOT: error
+# CHECK: error: undefined symbol: bar
+# CHECK-NOT: error
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Content:         '0000000000000000'
+  - Name:            .rela.text
+    Type:            SHT_RELA
+    AddressAlign:    8
+    Link:            .symtab
+    Info:            .text
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          bar
+        Type:            R_X86_64_64
+  - Name:            .debug_line
+    Type:            SHT_PROGBITS
+    Content:         3300000002001C0000000101FB0E0D000101010100000001000001006162632E7300000000000009020000000000000000140208000101
+  - Name:            .rela.debug_line
+    AddressAlign:    8
+    Type:            SHT_RELA
+    Link:            .symtab
+    Info:            .debug_line
+    Relocations:
+      - Offset:          0x0000000000000029
+        Type:            R_X86_64_NONE
+  - Name:            .debug_info
+    Type:            SHT_PROGBITS
+    AddressAlign:    0x0000000000000001
+    Content:         0C000000040000000000080100000000
+  - Name:            .debug_abbrev
+    Type:            SHT_PROGBITS
+    AddressAlign:    0x0000000000000001
+    Content:         '0111001017000000'
+
+Symbols:
+  Global:
+    - Name:            _start
+      Section:         .text
+    - Name:            bar
diff --git a/test/ELF/undef-broken-debug.test b/test/ELF/undef-broken-debug.test
index b93d399..c3405ad 100644
--- a/test/ELF/undef-broken-debug.test
+++ b/test/ELF/undef-broken-debug.test
@@ -5,7 +5,7 @@
 # The debug info has a broken relocation. Check that we don't crash
 # and still report the undefined symbol.
 
-# CHECK: error: unsupported relocation target while parsing debug info
+# CHECK: error: {{.*}}.o: relocation R_X86_64_64 at 0x29 has unsupported target
 # CHECK: error: undefined symbol: bar
 
 --- !ELF