[ELF][test] Rewrite st_value=0 copy relocation tests

The original tests have unneeded symbols and copy-relocation-zero-abs-addr.s
does not actually test anything.

Rewrite them and add copy-relocation-zero-addr.s instead.

Add --soname=b so that the address 0x203400 will be stable.  (When linking an
executable with %t.so, the path %t.so will be recorded in the DT_NEEDED entry if
%t.so doesn't have DT_SONAME. .dynstr will have varying lengths on different
systems.)
This commit is contained in:
Fangrui Song 2020-12-12 16:50:25 -08:00
parent eaa0982334
commit 5d1c723b73
6 changed files with 44 additions and 90 deletions

View File

@ -1,7 +0,0 @@
.globl ver1
.globl ver2
ver1 = 0x0
ver2 = 0x0
.type foo,@object
.comm foo,16,16

View File

@ -1,7 +0,0 @@
.balign 1024
.type foo,@object
.globl foo
goo:
foo:
.long 0
.size foo,4

View File

@ -1,3 +0,0 @@
SECTIONS {
goo = 0;
};

View File

@ -1,44 +0,0 @@
// REQUIRES: x86
// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/copy-relocation-zero-abs-addr.s -o %t.o
// RUN: ld.lld -shared -o %t2.so %t.o
// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t3.o
// RUN: ld.lld %t2.so %t3.o -o %t4
// RUN: llvm-readobj --symbols %t2.so | FileCheck -check-prefix=ABSADDR %s
// RUN: llvm-readobj -S -r --expand-relocs %t4 | FileCheck %s
// This tests that symbols with absolute addresses are properly
// handled. Normal DSO symbols are handled as usual.
.text
.globl _start
_start:
movl $5, foo
// ABSADDR: Name: ver1
// ABSADDR-NEXT: Value: 0x0
// ABSADDR-NEXT: Size: 0
// ABSADDR-NEXT: Binding: Global
// ABSADDR-NEXT: Type: None
// ABSADDR-NEXT: Other: 0
// ABSADDR-NEXT: Section: Absolute (0xFFF1)
// ABSADDR-NEXT: }
// ABSADDR-NEXT: Symbol {
// ABSADDR-NEXT: Name: ver2
// ABSADDR-NEXT: Value: 0x0
// ABSADDR-NEXT: Size: 0
// ABSADDR-NEXT: Binding: Global
// ABSADDR-NEXT: Type: None
// ABSADDR-NEXT: Other: 0
// ABSADDR-NEXT: Section: Absolute (0xFFF1)
// ABSADDR-NEXT: }
// CHECK: Relocations [
// CHECK-NEXT: Section (5) .rela.dyn {
// CHECK-NEXT: Relocation {
// CHECK-NEXT: Offset:
// CHECK-NEXT: Type: R_X86_64_COPY
// CHECK-NEXT: Symbol: foo
// CHECK-NEXT: Addend:
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: ]

View File

@ -0,0 +1,44 @@
# REQUIRES: x86
# RUN: split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/a.s -o %t/a.o
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/b.s -o %t/b.o
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/c.s -o %t/c.o
# RUN: ld.lld -shared -soname=b -Ttext=0 %t/b.o -o %t/b.so
# RUN: ld.lld %t/a.o %t/b.so -o %t1
# RUN: llvm-readelf -r -s %t1 | FileCheck %s
## In %t/b.so, foo has st_value==0 and its section alignment is 0x400. The
## alignment of the copy relocated foo is thus 0x400.
# CHECK: R_X86_64_COPY {{.*}} foo + 0
# CHECK: 0000000000203400 4 OBJECT GLOBAL DEFAULT [[#]] foo
## Error if attempting to copy relocate a SHN_ABS symbol (even if st_size is non-zero).
# RUN: ld.lld -shared -soname=c %t/c.o -o %t/c.so
# RUN: llvm-readelf -s %t/c.so | FileCheck %s --check-prefix=ABSADDR
# RUN: not ld.lld %t/a.o %t/c.so -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR
# ABSADDR: 0000000000000000 4 OBJECT GLOBAL DEFAULT ABS foo
# ERR: error: cannot create a copy relocation for symbol foo
#--- a.s
.text
.globl _start
_start:
movl $5, foo
#--- b.s
.data
.balign 0x400
.type foo,@object
.globl foo
foo:
.long 0
.size foo, 4
#--- c.s
.data
.globl foo
.type foo,@object
foo = 0x0
.size foo, 4

View File

@ -1,29 +0,0 @@
// REQUIRES: x86
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/copy-relocation-zero-nonabs-addr.s -o %t1.o
// RUN: ld.lld -Ttext=0 -o %t2.so --script=%p/Inputs/copy-relocation-zero-nonabs-addr.script %t1.o -shared
// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t3.o
// RUN: ld.lld %t2.so %t3.o -o %t4
// RUN: llvm-readobj --symbols %t2.so | FileCheck --check-prefix=CHECKSO %s
// RUN: llvm-readobj --symbols %t4 | FileCheck %s
.text
.globl _start
_start:
movl $5, foo
// Make sure foo has st_value == 0.
// CHECKSO: Name: foo
// CHECKSO-NEXT: Value: 0x0
// CHECKSO-NEXT: Size: 4
// CHECKSO-NEXT: Binding: Global
// CHECKSO-NEXT: Type: Object
// CHECKSO-NEXT: Other: 0
// CHECKSO-NEXT: Section: .text
// When foo has st_value == 0, it carries the section alignment.
// In this case, section alignment is 2^10, 0x202400 meets the requirement.
// CHECK: Name: foo
// CHECK-NEXT: Value: 0x202400
// CHECK-NEXT: Size: 4
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: Object