[lld] r270525 - Reject zero-sized symbols when creating copy relocations.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon May 23 19:37:40 PDT 2016


Author: ruiu
Date: Mon May 23 21:37:40 2016
New Revision: 270525

URL: http://llvm.org/viewvc/llvm-project?rev=270525&view=rev
Log:
Reject zero-sized symbols when creating copy relocations.

Copy relocations are relocations to copy data from DSOs to
executable's .bss segment at runtime. It doesn't make sense to
create such relocations for zero-sized symbols.

GNU linkers don't agree with each other. ld rejects such
relocation/symbol pair. gold don't reject that but do not create
copy relocations as well.  I took the former approach because
I don't think the latter is what user wants.

Added:
    lld/trunk/test/ELF/Inputs/copy-rel-corrupted.s
    lld/trunk/test/ELF/copy-rel-corrupted.s
Modified:
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/Inputs/copy-in-shared.s
    lld/trunk/test/ELF/Inputs/mips-dynamic.s
    lld/trunk/test/ELF/mips-got-and-copy.s

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=270525&r1=270524&r2=270525&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon May 23 21:37:40 2016
@@ -1055,10 +1055,15 @@ template <class ELFT> static uint32_t ge
 // Reserve space in .bss for copy relocation.
 template <class ELFT>
 void Writer<ELFT>::addCopyRelSymbol(SharedSymbol<ELFT> *SS) {
+  // Copy relocation against zero-sized symbol doesn't make sense.
+  uintX_t SymSize = SS->template getSize<ELFT>();
+  if (SymSize == 0)
+    fatal("cannot create a copy relocation for " + SS->getName());
+
   ensureBss();
   uintX_t Align = getAlignment(SS);
   uintX_t Off = alignTo(Out<ELFT>::Bss->getSize(), Align);
-  Out<ELFT>::Bss->setSize(Off + SS->template getSize<ELFT>());
+  Out<ELFT>::Bss->setSize(Off + SymSize);
   Out<ELFT>::Bss->updateAlign(Align);
   uintX_t Shndx = SS->Sym.st_shndx;
   uintX_t Value = SS->Sym.st_value;

Modified: lld/trunk/test/ELF/Inputs/copy-in-shared.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/copy-in-shared.s?rev=270525&r1=270524&r2=270525&view=diff
==============================================================================
--- lld/trunk/test/ELF/Inputs/copy-in-shared.s (original)
+++ lld/trunk/test/ELF/Inputs/copy-in-shared.s Mon May 23 21:37:40 2016
@@ -1,3 +1,4 @@
 .type foo, @object
 .global foo
 foo:
+.size foo, 4

Added: lld/trunk/test/ELF/Inputs/copy-rel-corrupted.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/copy-rel-corrupted.s?rev=270525&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/copy-rel-corrupted.s (added)
+++ lld/trunk/test/ELF/Inputs/copy-rel-corrupted.s Mon May 23 21:37:40 2016
@@ -0,0 +1,4 @@
+.type x, at object
+.globl x
+x:
+.size x, 0

Modified: lld/trunk/test/ELF/Inputs/mips-dynamic.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/mips-dynamic.s?rev=270525&r1=270524&r2=270525&view=diff
==============================================================================
--- lld/trunk/test/ELF/Inputs/mips-dynamic.s (original)
+++ lld/trunk/test/ELF/Inputs/mips-dynamic.s Mon May 23 21:37:40 2016
@@ -17,10 +17,12 @@ foo1:
   .data
   .globl data0
   .type data0, @object
+  .size data0, 4
 data0:
   .word 0
 
   .globl data1
   .type data1, @object
+  .size data1, 4
 data1:
   .word 0

Added: lld/trunk/test/ELF/copy-rel-corrupted.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/copy-rel-corrupted.s?rev=270525&view=auto
==============================================================================
--- lld/trunk/test/ELF/copy-rel-corrupted.s (added)
+++ lld/trunk/test/ELF/copy-rel-corrupted.s Mon May 23 21:37:40 2016
@@ -0,0 +1,10 @@
+// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
+// RUN: llvm-mc %p/Inputs/copy-rel-corrupted.s -o %t2.o -filetype=obj -triple=x86_64-pc-linux
+// RUN: ld.lld %t2.o -o %t2.so -shared
+// RUN: not ld.lld %t.o %t2.so -o %t.exe 2>&1 | FileCheck %s
+
+// CHECK: cannot create a copy relocation for x
+
+.global _start
+_start:
+        call x

Modified: lld/trunk/test/ELF/mips-got-and-copy.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/mips-got-and-copy.s?rev=270525&r1=270524&r2=270525&view=diff
==============================================================================
--- lld/trunk/test/ELF/mips-got-and-copy.s (original)
+++ lld/trunk/test/ELF/mips-got-and-copy.s Mon May 23 21:37:40 2016
@@ -33,8 +33,8 @@
 # CHECK-NEXT:     Entry {
 # CHECK-NEXT:       Address: 0x3000C
 # CHECK-NEXT:       Access: -32740
-# CHECK-NEXT:       Initial: 0x40010
-# CHECK-NEXT:       Value: 0x40010
+# CHECK-NEXT:       Initial: 0x40014
+# CHECK-NEXT:       Value: 0x40014
 # CHECK-NEXT:       Type: Object (0x1)
 # CHECK-NEXT:       Section: .bss (0xC)
 # CHECK-NEXT:       Name: data1@ (7)




More information about the llvm-commits mailing list