From a8c75b765e57aaebb99d4e32e0f228835cff2737 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Tue, 14 Feb 2017 10:45:51 +1030 Subject: [PATCH] hppa -z relro again I misunderstood the hppa alias problem. File offsets of segments need to be such that no page is mapped twice with different permissions. (Which still seems to me like something the kernel could fix, but anyhow, this is not so difficult to achieve in ld.) PR 21000 bfd/ * elf-bfd.h (struct elf_backend_data): Add no_page_alias. * elfxx-target.h (elf_backend_no_page_alias): Define. (elfNN_bed): Init new field. * elf.c (assign_file_positions_for_load_sections): If no_page_alias ensure PT_LOAD segment starts on a new page. * elf32-hppa.c (elf_backend_no_page_alias): Define. ld/ * testsuite/ld-elf/loadaddr1.d: Adjust for hppa file offsets. * testsuite/ld-elf/loadaddr2.d: Likewise. * testsuite/ld-elf/loadaddr3a.d: Likewise. * testsuite/ld-scripts/rgn-at5.d: Likewise. --- bfd/ChangeLog | 10 ++++++++++ bfd/elf-bfd.h | 4 ++++ bfd/elf.c | 11 +++++++++++ bfd/elf32-hppa.c | 1 + bfd/elfxx-target.h | 4 ++++ ld/ChangeLog | 8 ++++++++ ld/testsuite/ld-elf/loadaddr1.d | 2 +- ld/testsuite/ld-elf/loadaddr2.d | 4 ++-- ld/testsuite/ld-elf/loadaddr3a.d | 2 +- ld/testsuite/ld-scripts/rgn-at5.d | 8 ++++---- 10 files changed, 46 insertions(+), 8 deletions(-) diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 4be12b80227..0c45c44bfed 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,13 @@ +2017-02-16 Alan Modra + + PR 21000 + * elf-bfd.h (struct elf_backend_data): Add no_page_alias. + * elfxx-target.h (elf_backend_no_page_alias): Define. + (elfNN_bed): Init new field. + * elf.c (assign_file_positions_for_load_sections): If no_page_alias + ensure PT_LOAD segment starts on a new page. + * elf32-hppa.c (elf_backend_no_page_alias): Define. + 2017-02-16 Alan Modra PR 21132 diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 9ea763a9fc7..f5a8d75e5a0 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -1461,6 +1461,10 @@ struct elf_backend_data This field indicates whether this behavior is required. */ unsigned want_p_paddr_set_to_zero : 1; + /* Target has broken hardware and/or kernel that requires pages not + to be mapped twice with different permissions. */ + unsigned no_page_alias : 1; + /* True if an object file lacking a .note.GNU-stack section should be assumed to be requesting exec stack. At least one other file in the link needs to have a .note.GNU-stack section diff --git a/bfd/elf.c b/bfd/elf.c index 33fb4d2c343..94726bad2e4 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -5125,6 +5125,7 @@ assign_file_positions_for_load_sections (bfd *abfd, Elf_Internal_Phdr *p; file_ptr off; bfd_size_type maxpagesize; + unsigned int pt_load_count = 0; unsigned int alloc; unsigned int i, j; bfd_vma header_pad = 0; @@ -5252,6 +5253,7 @@ assign_file_positions_for_load_sections (bfd *abfd, maxpagesize = m->p_align; p->p_align = maxpagesize; + pt_load_count += 1; } else if (m->p_align_valid) p->p_align = m->p_align; @@ -5303,6 +5305,15 @@ assign_file_positions_for_load_sections (bfd *abfd, } off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); + + /* Broken hardware and/or kernel require that files do not + map the same page with different permissions on some hppa + processors. */ + if (pt_load_count > 1 + && bed->no_page_alias + && (off & (maxpagesize - 1)) != 0 + && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize)) + off_adjust += maxpagesize; off += off_adjust; if (no_contents) { diff --git a/bfd/elf32-hppa.c b/bfd/elf32-hppa.c index e64ea9aff98..d5b911cf6a1 100644 --- a/bfd/elf32-hppa.c +++ b/bfd/elf32-hppa.c @@ -4646,6 +4646,7 @@ elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type) #define elf_backend_want_dynrelro 1 #define elf_backend_rela_normal 1 #define elf_backend_dtrel_excludes_plt 1 +#define elf_backend_no_page_alias 1 #define TARGET_BIG_SYM hppa_elf32_vec #define TARGET_BIG_NAME "elf32-hppa" diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h index 00252cee84b..d063fb7f1ba 100644 --- a/bfd/elfxx-target.h +++ b/bfd/elfxx-target.h @@ -114,6 +114,9 @@ #ifndef elf_backend_want_p_paddr_set_to_zero #define elf_backend_want_p_paddr_set_to_zero 0 #endif +#ifndef elf_backend_no_page_alias +#define elf_backend_no_page_alias 0 +#endif #ifndef elf_backend_default_execstack #define elf_backend_default_execstack 1 #endif @@ -860,6 +863,7 @@ static struct elf_backend_data elfNN_bed = elf_backend_want_dynbss, elf_backend_want_dynrelro, elf_backend_want_p_paddr_set_to_zero, + elf_backend_no_page_alias, elf_backend_default_execstack, elf_backend_caches_rawsize, elf_backend_extern_protected_data diff --git a/ld/ChangeLog b/ld/ChangeLog index f0b0b0ed9e9..1a7d18ca931 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,11 @@ +2017-02-16 Alan Modra + + PR 21000 + * testsuite/ld-elf/loadaddr1.d: Adjust for hppa file offsets. + * testsuite/ld-elf/loadaddr2.d: Likewise. + * testsuite/ld-elf/loadaddr3a.d: Likewise. + * testsuite/ld-scripts/rgn-at5.d: Likewise. + 2017-02-16 Alan Modra * testsuite/ld-powerpc/vxworks1-lib.s: Correct addi to addic. diff --git a/ld/testsuite/ld-elf/loadaddr1.d b/ld/testsuite/ld-elf/loadaddr1.d index 0fd96a78ea3..adb562a9019 100644 --- a/ld/testsuite/ld-elf/loadaddr1.d +++ b/ld/testsuite/ld-elf/loadaddr1.d @@ -6,5 +6,5 @@ #... LOAD +0x000000 0xf*80000000 0xf*80000000 0x100050 0x100050 RWE 0x200000 LOAD +0x200000 0xf*ff600000 0xf*80101000 0x0*10 0x0*10 R E 0x200000 - LOAD +0x302000 0xf*80102000 0xf*80102000 0x0*10 0x0*10 RW 0x200000 + LOAD +0x[35]02000 0xf*80102000 0xf*80102000 0x0*10 0x0*10 RW 0x200000 #pass diff --git a/ld/testsuite/ld-elf/loadaddr2.d b/ld/testsuite/ld-elf/loadaddr2.d index 5825fe8494c..eaa672da17e 100644 --- a/ld/testsuite/ld-elf/loadaddr2.d +++ b/ld/testsuite/ld-elf/loadaddr2.d @@ -5,6 +5,6 @@ #... LOAD +0x000000 0xf*80000000 0xf*80000000 0x100050 0x100050 RWE 0x200000 - LOAD +0x110000 0xf*80110000 0xf*80101000 0x0*10 0x0*10 R E 0x200000 - LOAD +0x302000 0xf*80302000 0xf*80302000 0x0*10 0x0*10 RW 0x200000 + LOAD +0x[13]10000 0xf*80110000 0xf*80101000 0x0*10 0x0*10 R E 0x200000 + LOAD +0x[35]02000 0xf*80302000 0xf*80302000 0x0*10 0x0*10 RW 0x200000 #pass diff --git a/ld/testsuite/ld-elf/loadaddr3a.d b/ld/testsuite/ld-elf/loadaddr3a.d index 10cb9a588f4..0082e1f033e 100644 --- a/ld/testsuite/ld-elf/loadaddr3a.d +++ b/ld/testsuite/ld-elf/loadaddr3a.d @@ -5,5 +5,5 @@ #... LOAD +0x000000 0x0*00000000 0x0*00000000 0x0*0110 0x0*0110 R E 0x.* - LOAD +0x000200 0x0*00000200 0x0*00000110 0x0*0010 0x0*0010 RW 0x.* + LOAD +0x[02]00200 0x0*00000200 0x0*00000110 0x0*0010 0x0*0010 RW 0x.* #pass diff --git a/ld/testsuite/ld-scripts/rgn-at5.d b/ld/testsuite/ld-scripts/rgn-at5.d index 7ba9cafb2f1..012451ae8ab 100644 --- a/ld/testsuite/ld-scripts/rgn-at5.d +++ b/ld/testsuite/ld-scripts/rgn-at5.d @@ -13,8 +13,8 @@ Sections: Idx +Name +Size +VMA +LMA +File off +Algn +Flags 0 .sec0 +0+4 +0+2000 +0+2000 +0+1000 +.* 1 .sec1 +0+4 +0+1000 +0+2004 +0+2000 +.* - 2 .sec2 +0+4 +0+4000 +0+603c +0+4000 +.* - 3 .sec3 +0+4 +0+5000 +0+5000 +0+3000 +.* - 4 .sec4 +0+4 +0+2008 +0+2008 +0+2008 +.* - 5 .sec5 +0+4 +0+200c +0+200c +0+200c +.* + 2 .sec2 +0+4 +0+4000 +0+603c +0+[45]000 +.* + 3 .sec3 +0+4 +0+5000 +0+5000 +0+[34]000 +.* + 4 .sec4 +0+4 +0+2008 +0+2008 +0+[23]008 +.* + 5 .sec5 +0+4 +0+200c +0+200c +0+[23]00c +.* #pass -- 2.30.2