[lld] r255307 - Fix alignment computation for copy relocs.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 10 14:53:25 PST 2015


Author: rafael
Date: Thu Dec 10 16:53:24 2015
New Revision: 255307

URL: http://llvm.org/viewvc/llvm-project?rev=255307&view=rev
Log:
Fix alignment computation for copy relocs.

Fixes PR25798.

Thanks to Ed Maste for the bug report and suggested fix.

Added:
    lld/trunk/test/ELF/Inputs/relocation-copy-align.s
    lld/trunk/test/ELF/relocation-copy-align.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=255307&r1=255306&r2=255307&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Dec 10 16:53:24 2015
@@ -501,9 +501,10 @@ void Writer<ELFT>::addSharedCopySymbols(
     const Elf_Sym &Sym = C->Sym;
     const Elf_Shdr *Sec = C->File->getSection(Sym);
     uintX_t SecAlign = Sec->sh_addralign;
-    uintX_t Align = Sym.st_value % SecAlign;
-    if (Align == 0)
-      Align = SecAlign;
+    unsigned TrailingZeros =
+        std::min(countTrailingZeros(SecAlign),
+                 countTrailingZeros((uintX_t)Sym.st_value));
+    uintX_t Align = 1 << TrailingZeros;
     Out<ELFT>::Bss->updateAlign(Align);
     Off = RoundUpToAlignment(Off, Align);
     C->OffsetInBSS = Off;

Added: lld/trunk/test/ELF/Inputs/relocation-copy-align.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/relocation-copy-align.s?rev=255307&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/relocation-copy-align.s (added)
+++ lld/trunk/test/ELF/Inputs/relocation-copy-align.s Thu Dec 10 16:53:24 2015
@@ -0,0 +1,9 @@
+.data
+        .balign 16
+        .zero 12
+
+        .type x, at object
+        .globl x
+x:
+        .long 0
+        .size x, 4

Added: lld/trunk/test/ELF/relocation-copy-align.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/relocation-copy-align.s?rev=255307&view=auto
==============================================================================
--- lld/trunk/test/ELF/relocation-copy-align.s (added)
+++ lld/trunk/test/ELF/relocation-copy-align.s Thu Dec 10 16:53:24 2015
@@ -0,0 +1,31 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/relocation-copy-align.s -o %t2.o
+// RUN: ld.lld -shared %t2.o -o %t.so
+// RUN: ld.lld %t.o %t.so -o %t3
+// RUN: llvm-readobj -s -r --expand-relocs %t3 | FileCheck %s
+
+.global _start
+_start:
+movl $5, x
+
+// CHECK:    Name: .bss
+// CHECK-NEXT:    Type: SHT_NOBITS
+// CHECK-NEXT:    Flags [
+// CHECK-NEXT:      SHF_ALLOC
+// CHECK-NEXT:      SHF_WRITE
+// CHECK-NEXT:    ]
+// CHECK-NEXT:    Address:
+// CHECK-NEXT:    Offset:
+// CHECK-NEXT:    Size: 4
+// CHECK-NEXT:    Link:
+// CHECK-NEXT:    Info:
+// CHECK-NEXT:    AddressAlignment: 4
+// CHECK-NEXT:    EntrySize:
+
+// CHECK:      Relocation {
+// CHECK-NEXT:   Offset:
+// CHECK-NEXT:   Type: R_X86_64_COPY
+// CHECK-NEXT:   Symbol: x
+// CHECK-NEXT:   Addend: 0x0
+// CHECK-NEXT: }




More information about the llvm-commits mailing list