S/390: Prevent GOT access rewrite for certain symbols

Message ID 1536855587-12025-1-git-send-email-krebbel@linux.ibm.com
State New
Headers show
Series
  • S/390: Prevent GOT access rewrite for certain symbols
Related show

Commit Message

Andreas Krebbel Sept. 13, 2018, 4:19 p.m.
From: Andreas Krebbel <krebbel@linux.vnet.ibm.com>


When dereferencing a GOT slot with lgrl or lg we rewrite this using
larl to get rid of the extra memory access.  However, we cannot do
this for:

- symbols marked for absolute addressing
- symbols at odd addresses (larl can handle only even addresses)

Fixed with the attached patch.

bfd/ChangeLog:

2018-09-13  Andreas Krebbel  <krebbel@linux.ibm.com>

	* elf64-s390.c (elf_s390_relocate_section): Prevent rewriting of
	GOT accesses with larl for ABS or misaligned symbols.

ld/ChangeLog:

2018-09-13  Andreas Krebbel  <krebbel@linux.ibm.com>

	* testsuite/ld-s390/gotreloc-1.s: Add tests for ABS and misaligned
	symbol. Move variables into data section. Make bar 8 bytes wide.
	* testsuite/ld-s390/gotreloc-1.ver: Make misaligned_sym resolve locally.
	* testsuite/ld-s390/gotreloc_31-1.dd: Adjust patterns.
	* testsuite/ld-s390/gotreloc_64-norelro-1.dd: Likewise.
	* testsuite/ld-s390/gotreloc_64-relro-1.dd: Likewise.
---
 bfd/elf64-s390.c                              | 20 ++++++++++++++++++--
 ld/testsuite/ld-s390/gotreloc-1.s             | 12 ++++++++++--
 ld/testsuite/ld-s390/gotreloc-1.ver           |  2 +-
 ld/testsuite/ld-s390/gotreloc_31-1.dd         | 14 +++++++-------
 ld/testsuite/ld-s390/gotreloc_64-norelro-1.dd | 14 +++++++-------
 ld/testsuite/ld-s390/gotreloc_64-relro-1.dd   | 14 +++++++-------
 6 files changed, 50 insertions(+), 26 deletions(-)

-- 
2.7.4

Comments

Andreas Krebbel Sept. 17, 2018, 9:09 a.m. | #1
On 13.09.2018 18:19, Andreas Krebbel wrote:
> From: Andreas Krebbel <krebbel@linux.vnet.ibm.com>

> 

> When dereferencing a GOT slot with lgrl or lg we rewrite this using

> larl to get rid of the extra memory access.  However, we cannot do

> this for:

> 

> - symbols marked for absolute addressing

> - symbols at odd addresses (larl can handle only even addresses)

> 

> Fixed with the attached patch.

> 

> bfd/ChangeLog:

> 

> 2018-09-13  Andreas Krebbel  <krebbel@linux.ibm.com>

> 

> 	* elf64-s390.c (elf_s390_relocate_section): Prevent rewriting of

> 	GOT accesses with larl for ABS or misaligned symbols.

> 

> ld/ChangeLog:

> 

> 2018-09-13  Andreas Krebbel  <krebbel@linux.ibm.com>

> 

> 	* testsuite/ld-s390/gotreloc-1.s: Add tests for ABS and misaligned

> 	symbol. Move variables into data section. Make bar 8 bytes wide.

> 	* testsuite/ld-s390/gotreloc-1.ver: Make misaligned_sym resolve locally.

> 	* testsuite/ld-s390/gotreloc_31-1.dd: Adjust patterns.

> 	* testsuite/ld-s390/gotreloc_64-norelro-1.dd: Likewise.

> 	* testsuite/ld-s390/gotreloc_64-relro-1.dd: Likewise.


Pushed to mainline. No testsuite regressions.

Andreas

Patch

diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c
index 91b76bc..c2a2955 100644
--- a/bfd/elf64-s390.c
+++ b/bfd/elf64-s390.c
@@ -2341,6 +2341,9 @@  elf_s390_relocate_section (bfd *output_bfd,
 			   && SYMBOL_REFERENCES_LOCAL (info, h))
 		       || resolved_to_zero)
 		{
+		  Elf_Internal_Sym *isym;
+		  asection *sym_sec;
+
 		  /* This is actually a static link, or it is a
 		     -Bsymbolic link and the symbol is defined
 		     locally, or the symbol was forced to be local
@@ -2362,6 +2365,10 @@  elf_s390_relocate_section (bfd *output_bfd,
 		      h->got.offset |= 1;
 		    }
 
+		  /* When turning a GOT slot dereference into a direct
+		     reference using larl we have to make sure that
+		     the symbol is 1. properly aligned and 2. it is no
+		     ABS symbol or will become one.  */
 		  if ((h->def_regular
 		       && bfd_link_pic (info)
 		       && SYMBOL_REFERENCES_LOCAL (info, h))
@@ -2376,8 +2383,17 @@  elf_s390_relocate_section (bfd *output_bfd,
 					      contents + rel->r_offset - 2)
 				  & 0xff00f000) == 0xe300c000
 			      && bfd_get_8 (input_bfd,
-					    contents + rel->r_offset + 3) == 0x04)))
-
+					    contents + rel->r_offset + 3) == 0x04))
+		      && (isym = bfd_sym_from_r_symndx (&htab->sym_cache,
+							input_bfd, r_symndx))
+		      && isym->st_shndx != SHN_ABS
+		      && h != htab->elf.hdynamic
+		      && h != htab->elf.hgot
+		      && h != htab->elf.hplt
+		      && !(isym->st_value & 1)
+		      && (sym_sec = bfd_section_from_elf_index (input_bfd,
+								isym->st_shndx))
+		      && sym_sec->alignment_power)
 		    {
 		      unsigned short new_insn =
 			(0xc000 | (bfd_get_8 (input_bfd,
diff --git a/ld/testsuite/ld-s390/gotreloc-1.s b/ld/testsuite/ld-s390/gotreloc-1.s
index b60d6c1..0e1f6f7 100644
--- a/ld/testsuite/ld-s390/gotreloc-1.s
+++ b/ld/testsuite/ld-s390/gotreloc-1.s
@@ -1,4 +1,4 @@ 
-	.text
+.text
 	.globl foo
 foo:
 	lgrl	%r1,bar@GOTENT
@@ -6,6 +6,14 @@  foo:
 	lrl	%r1,bar@GOTENT
 	l	%r1,bar@GOT(%r12)
 	ly	%r1,bar@GOT(%r12)
+	lgrl	%r1,_GLOBAL_OFFSET_TABLE_@GOTENT
+	lgrl	%r1,misaligned_sym@GOTENT
 
+.data
 .globl	bar
-bar:	.long	0x123
+bar:	.quad	0x123
+
+.globl  misaligned_sym
+	.byte	1
+misaligned_sym:
+	.quad	42
diff --git a/ld/testsuite/ld-s390/gotreloc-1.ver b/ld/testsuite/ld-s390/gotreloc-1.ver
index 1cb06d6..0eb6096 100644
--- a/ld/testsuite/ld-s390/gotreloc-1.ver
+++ b/ld/testsuite/ld-s390/gotreloc-1.ver
@@ -1 +1 @@ 
-{ local: bar; };
+{ local: bar; misaligned_sym; };
diff --git a/ld/testsuite/ld-s390/gotreloc_31-1.dd b/ld/testsuite/ld-s390/gotreloc_31-1.dd
index 5e3f824..b1cf37c 100644
--- a/ld/testsuite/ld-s390/gotreloc_31-1.dd
+++ b/ld/testsuite/ld-s390/gotreloc_31-1.dd
@@ -4,10 +4,10 @@  tmpdir/gotreloc_31-1:     file format elf32-s390
 Disassembly of section .text:
 
 .* <foo>:
-.*:	c4 18 00 00 08 4e [	 ]*lgrl	%r1,118c <_GLOBAL_OFFSET_TABLE_\+0xc>
-.*:	e3 10 c0 0c 00 04 [	 ]*lg	%r1,12\(%r12\)
-.*:	c0 10 00 00 00 08 [	 ]*larl	%r1,10c <bar>
-.*:	58 10 c0 0c [	 ]*l	%r1,12\(%r12\)
-.*:	c0 10 00 00 00 03 [	 ]*larl	%r1,10c <bar>
-.* <bar>:
-.*:	00 00 01 23       	.long	0x00000123
+.*:	c4 18 00 00 08 56 [	 ]*lgrl	%r1,11b4 <_GLOBAL_OFFSET_TABLE_\+0x14>
+.*:	e3 10 c0 14 00 04 [	 ]*lg	%r1,20\(%r12\)
+.*:	c0 10 00 00 08 52 [	 ]*larl	%r1,11b8 <bar>
+.*:	58 10 c0 14 [	 ]*l	%r1,20\(%r12\)
+.*:	c0 10 00 00 08 4d [	 ]*larl	%r1,11b8 <bar>
+.*:	c4 18 00 00 08 46 [	 ]*lgrl	%r1,11b0 <_GLOBAL_OFFSET_TABLE_\+0x10>
+.*:	c4 18 00 00 08 41 [	 ]*lgrl	%r1,11ac <_GLOBAL_OFFSET_TABLE_\+0xc>
diff --git a/ld/testsuite/ld-s390/gotreloc_64-norelro-1.dd b/ld/testsuite/ld-s390/gotreloc_64-norelro-1.dd
index 8c8c619..8debd97 100644
--- a/ld/testsuite/ld-s390/gotreloc_64-norelro-1.dd
+++ b/ld/testsuite/ld-s390/gotreloc_64-norelro-1.dd
@@ -3,10 +3,10 @@  tmpdir/gotreloc_64-1:     file format elf64-s390
 Disassembly of section .text:
 
 .* <foo>:
-.*:	c0 10 00 00 00 0e [	 ]*larl	%r1,.* <bar>
-.*:	c0 10 00 00 00 0b [	 ]*larl	%r1,.* <bar>
-.*:	c4 1d 00 00 08 86 [	 ]*lrl	%r1,.* <_GLOBAL_OFFSET_TABLE_\+0x18>
-.*:	58 10 c0 18 [	 ]*l	%r1,24\(%r12\)
-.*:	e3 10 c0 18 00 58 [	 ]*ly	%r1,24\(%r12\)
-.* <bar>:
-.*:	00 00 01 23       	.long	0x00000123
+.*:	c0 10 00 00 08 9c [	 ]*larl	%r1,12e8 <bar>
+.*:	c0 10 00 00 08 99 [	 ]*larl	%r1,12e8 <bar>
+.*:	c4 1d 00 00 08 92 [	 ]*lrl	%r1,12e0 <_GLOBAL_OFFSET_TABLE_\+0x28>
+.*:	58 10 c0 28 [	 ]*l	%r1,40\(%r12\)
+.*:	e3 10 c0 28 00 58 [	 ]*ly	%r1,40\(%r12\)
+.*:	c4 18 00 00 08 86 [	 ]*lgrl	%r1,12d8 <_GLOBAL_OFFSET_TABLE_\+0x20>
+.*:	c4 18 00 00 08 7f [	 ]*lgrl	%r1,12d0 <_GLOBAL_OFFSET_TABLE_\+0x18>
diff --git a/ld/testsuite/ld-s390/gotreloc_64-relro-1.dd b/ld/testsuite/ld-s390/gotreloc_64-relro-1.dd
index 36f5001..64151d1 100644
--- a/ld/testsuite/ld-s390/gotreloc_64-relro-1.dd
+++ b/ld/testsuite/ld-s390/gotreloc_64-relro-1.dd
@@ -3,10 +3,10 @@  tmpdir/gotreloc_64-1:     file format elf64-s390
 Disassembly of section .text:
 
 .* <foo>:
-.*:	c0 10 00 00 00 0e [	 ]*larl	%r1,.* <bar>
-.*:	c0 10 00 00 00 0b [	 ]*larl	%r1,.* <bar>
-.*:	c4 1d 00 00 0f 1a [	 ]*lrl	%r1,.* <_GLOBAL_OFFSET_TABLE_\+0x18>
-.*:	58 10 c0 18 [	 ]*l	%r1,24\(%r12\)
-.*:	e3 10 c0 18 00 58 [	 ]*ly	%r1,24\(%r12\)
-.* <bar>:
-.*:	00 00 01 23       	.long	0x00000123
+.*:	c0 10 00 00 0f 0c [	 ]*larl	%r1,2000 <bar>
+.*:	c0 10 00 00 0f 09 [	 ]*larl	%r1,2000 <bar>
+.*:	c4 1d 00 00 0f 02 [	 ]*lrl	%r1,1ff8 <_GLOBAL_OFFSET_TABLE_\+0x28>
+.*:	58 10 c0 28 [	 ]*l	%r1,40\(%r12\)
+.*:	e3 10 c0 28 00 58 [	 ]*ly	%r1,40\(%r12\)
+.*:	c4 18 00 00 0e f6 [	 ]*lgrl	%r1,1ff0 <_GLOBAL_OFFSET_TABLE_\+0x20>
+.*:	c4 18 00 00 0e ef [	 ]*lgrl	%r1,1fe8 <_GLOBAL_OFFSET_TABLE_\+0x18>