[lld] r357885 - Fix -emit-reloc against local symbols.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 7 23:45:07 PDT 2019


Author: ruiu
Date: Sun Apr  7 23:45:07 2019
New Revision: 357885

URL: http://llvm.org/viewvc/llvm-project?rev=357885&view=rev
Log:
Fix -emit-reloc against local symbols.

Previously, we drop symbols starting with .L from the symbol table, so
if there is a relocation that refers a .L symbol, it ended up
referencing a null -- which happened to be interpreted as an absolute
symbol.

This patch copies all symbols including local ones if -emit-reloc is
given.

Fixes https://bugs.llvm.org/show_bug.cgi?id=41385

Differential Revision: https://reviews.llvm.org/D60306

Added:
    lld/trunk/test/ELF/emit-relocs-mergeable2.s
Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=357885&r1=357884&r2=357885&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Sun Apr  7 23:45:07 2019
@@ -567,6 +567,11 @@ static bool shouldKeepInSymtab(const Def
   if (Config->Discard == DiscardPolicy::None)
     return true;
 
+  // If -emit-reloc is given, all symbols including local ones need to be
+  // copied because they may be referenced by relocations.
+  if (Config->EmitRelocs)
+    return true;
+
   // In ELF assembly .L symbols are normally discarded by the assembler.
   // If the assembler fails to do so, the linker discards them if
   // * --discard-locals is used.

Added: lld/trunk/test/ELF/emit-relocs-mergeable2.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/emit-relocs-mergeable2.s?rev=357885&view=auto
==============================================================================
--- lld/trunk/test/ELF/emit-relocs-mergeable2.s (added)
+++ lld/trunk/test/ELF/emit-relocs-mergeable2.s Sun Apr  7 23:45:07 2019
@@ -0,0 +1,14 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: ld.lld --emit-relocs %t.o -o %t.exe
+# RUN: llvm-readelf --relocations %t.exe | FileCheck %s
+
+# CHECK: 0000000000201004  000000010000000b R_X86_64_32S 0000000000200120 .Lfoo + 8
+
+.globl  _start
+_start:
+  movq .Lfoo+8, %rax
+.section .rodata.cst16,"aM", at progbits,16
+.Lfoo:
+  .quad 0
+  .quad 0




More information about the llvm-commits mailing list