public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3] elf: Set p_align to the minimum page size if possible
@ 2021-12-29 19:39 H.J. Lu
  2022-01-14  8:12 ` Jan Beulich
  2022-01-14  8:27 ` Jan Beulich
  0 siblings, 2 replies; 16+ messages in thread
From: H.J. Lu @ 2021-12-29 19:39 UTC (permalink / raw)
  To: binutils

Currently, on 32-bit and 64-bit ARM, it seems that ld generates p_align
values of 0x10000 even if no section alignment is greater than 0x1000.
The issue is more general and probably affects other targets with multiple
page sizes.

While file layout absolutely must take 64K page size into account, that
does not have to be reflected in the p_align value.  If running on a 64K
kernel, the file will be loaded at a 64K page boundary by necessity. On
a 4K kernel, 64K alignment is not needed.

The glibc loader has been fixed to honor p_align:

https://sourceware.org/bugzilla/show_bug.cgi?id=28676

similar to kernel:

commit ce81bb256a224259ab686742a6284930cbe4f1fa
Author: Chris Kennelly <ckennelly@google.com>
Date:   Thu Oct 15 20:12:32 2020 -0700

    fs/binfmt_elf: use PT_LOAD p_align values for suitable start address

This means that on 4K kernels, we will start to do extra work for 64K
p_align, but this pointless for pretty much all binaries (whose section
alignment rarely exceeds 16).

The minimum page size is used, instead of the maximum section alignment
due to this glibc bug:

https://sourceware.org/bugzilla/show_bug.cgi?id=28688

It has been fixed in glibc 2.35.  But linker output must work on existing
glibc binaries.

1. Set p_align to the minimum page size while laying out segments aligning
to the maximum page size or section alignment.  The run-time loader can
align segments to the minimum page size or above, depending on system page
size.
2. If -z max-page-size=NNN is used, p_align will be set to the maximum
page size or the largest section alignment.
3. If a section requires alignment higher than the minimum page size,
don't set p_align to the minimum page size.
4. If a section requires alignment higher than the maximum page size,
set p_align to the section alignment.
5. For objcopy, when the minimum page size != the maximum page size,
p_align may be set to the minimum page size while segments are aligned
to the maximum page size.  In this case, the input p_align will be
ignored and the maximum page size will be used to align the ouput
segments.
6. Update linker to disallow the common page size > the maximum page size.
7. Update linker to avoid the common page size > the maximum page size.
8. Adjust pru_irq_map-1.d to expect p_align == sh_addralign:

Section Headers:
  [Nr] Name   Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]        NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text  PROGBITS        20000000 00007c 000004 00  AX  0   0  4
...
Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000074 0x00000000 0x00000000 0x00008 0x00008 RW  0x1
  LOAD           0x00007c 0x20000000 0x20000000 0x00004 0x00004 R E 0x4

vs.

Section Headers:
  [Nr] Name   Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]        NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text  PROGBITS        20000000 00007c 000004 00  AX  0   0  4
...
Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000074 0x00000000 0x00000000 0x00008 0x00008 RW  0x1
  LOAD           0x00007c 0x20000000 0x20000000 0x00004 0x00004 R E 0x1

To enable this linker optimization, the backend should define ELF_P_ALIGN
to ELF_MINPAGESIZE.

bfd/

	PR ld/28689
	PR ld/28695
	* elf-bfd.h (elf_backend_data): Add p_align.
	* elf.c (assign_file_positions_for_load_sections): Set p_align
	to the default p_align value while laying out segments aligning
	to maximum page size or section alignment.
	(elf_is_p_align_valid): New function.
	(copy_elf_program_header): Call elf_is_p_align_valid to determine
	if p_align is valid.
	* elfxx-target.h (ELF_P_ALIGN): New.  Default to 0.
	(elfNN_bed): Add ELF_P_ALIGN.
	* elfxx-x86.h (ELF_P_ALIGN): New.  Set to ELF_MINPAGESIZE.

include/

	PR ld/28689
	PR ld/28695
	* bfdlink.h (bfd_link_info): Add maxpagesize_is_set.

ld/

	PR ld/28689
	PR ld/28695
	* emultempl/elf.em (gld${EMULATION_NAME}_handle_option): Set
	link_info.maxpagesize_is_set for -z max-page-size=NNN.
	* ldelf.c (ldelf_after_parse): Disallow link_info.commonpagesize
	> link_info.maxpagesize.
	* testsuite/ld-elf/elf.exp: Pass -z max-page-size=0x4000 to
	linker to build mbind2a and mbind2b.
	* testsuite/ld-elf/header.d: Add -z common-page-size=0x100.
	* testsuite/ld-elf/linux-x86.exp: Add PR ld/28689 tests.
	* testsuite/ld-elf/p_align-1.c: New file.
	* testsuite/ld-elf/page-size-1.d: New test.
	* testsuite/ld-elf/pr26936.d: Add -z common-page-size=0x1000.
	* testsuite/ld-elf/seg.d: Likewise.
	* testsuite/ld-scripts/rgn-at5.d: Likewise.
	* testsuite/ld-pru/pru_irq_map-1.d: Append 1 to name.  Adjust
	expected PT_LOAD segment alignment.
	* testsuite/ld-pru/pru_irq_map-2.d: Append 2 to name.
	* testsuite/ld-scripts/pr23571.d: Add -z max-page-size=0x1000.
---
 bfd/elf-bfd.h                       |  4 ++
 bfd/elf.c                           | 71 ++++++++++++++++++++++++++++-
 bfd/elfxx-target.h                  |  5 ++
 bfd/elfxx-x86.h                     |  2 +
 include/bfdlink.h                   |  3 ++
 ld/emultempl/elf.em                 |  1 +
 ld/ldelf.c                          |  3 ++
 ld/testsuite/ld-elf/elf.exp         |  4 +-
 ld/testsuite/ld-elf/header.d        |  2 +-
 ld/testsuite/ld-elf/linux-x86.exp   | 36 +++++++++++++++
 ld/testsuite/ld-elf/p_align-1.c     | 25 ++++++++++
 ld/testsuite/ld-elf/page-size-1.d   |  4 ++
 ld/testsuite/ld-elf/pr26936.d       |  2 +-
 ld/testsuite/ld-elf/seg.d           |  2 +-
 ld/testsuite/ld-pru/pru_irq_map-1.d |  4 +-
 ld/testsuite/ld-pru/pru_irq_map-2.d |  2 +-
 ld/testsuite/ld-scripts/pr23571.d   |  2 +-
 ld/testsuite/ld-scripts/rgn-at5.d   |  2 +-
 18 files changed, 162 insertions(+), 12 deletions(-)
 create mode 100644 ld/testsuite/ld-elf/p_align-1.c
 create mode 100644 ld/testsuite/ld-elf/page-size-1.d

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index be2eb38ea6a..d446ee41f44 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -945,6 +945,10 @@ struct elf_backend_data
   /* The value of commonpagesize to use when -z relro for this backend.  */
   bfd_vma relropagesize;
 
+  /* The p_align value for this backend.  If it is set, p_align of
+      PT_LOAD alignment will be to p_align by default.  */
+  bfd_vma p_align;
+
   /* The BFD flags applied to sections created for dynamic linking.  */
   flagword dynamic_sec_flags;
 
diff --git a/bfd/elf.c b/bfd/elf.c
index 92c06f2e44f..6c440a6dafc 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -5407,6 +5407,8 @@ assign_file_positions_for_load_sections (bfd *abfd,
   Elf_Internal_Phdr *p;
   file_ptr off;  /* Octets.  */
   bfd_size_type maxpagesize;
+  bfd_size_type p_align;
+  bool p_align_p = false;
   unsigned int alloc, actual;
   unsigned int i, j;
   struct elf_segment_map **sorted_seg_map;
@@ -5491,6 +5493,7 @@ assign_file_positions_for_load_sections (bfd *abfd,
     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
 	   elf_sort_segments);
 
+  p_align = bed->p_align;
   maxpagesize = 1;
   if ((abfd->flags & D_PAGED) != 0)
     {
@@ -5561,6 +5564,15 @@ assign_file_positions_for_load_sections (bfd *abfd,
 	     segment.  */
 	  if (m->p_align_valid)
 	    maxpagesize = m->p_align;
+	  else if (p_align != 0
+		   && (link_info == NULL
+		       || !link_info->maxpagesize_is_set))
+	    /* Set p_align to the default p_align value while laying
+	       out segments aligning to the maximum page size or the
+	       largest section alignment.  The run-time loader can
+	       align segments to the default p_align value or the
+	       maximum page size, depending on system page size.  */
+	    p_align_p = true;
 
 	  p->p_align = maxpagesize;
 	}
@@ -5598,7 +5610,22 @@ assign_file_positions_for_load_sections (bfd *abfd,
 		}
 	      align = (bfd_size_type) 1 << align_power;
 	      if (align < maxpagesize)
-		align = maxpagesize;
+		{
+		  /* If a section requires alignment higher than the
+		     default p_align value, don't set p_align to the
+		     default p_align value.  */
+		  if (align > p_align)
+		    p_align_p = false;
+		  align = maxpagesize;
+		}
+	      else
+		{
+		  /* If a section requires alignment higher than the
+		     maximum page size, set p_align to the section
+		     alignment.  */
+		  p_align_p = true;
+		  p_align = align;
+		}
 	    }
 
 	  for (i = 0; i < m->count; i++)
@@ -5977,6 +6004,9 @@ assign_file_positions_for_load_sections (bfd *abfd,
 		  print_segment_map (m);
 		}
 	    }
+
+	  if (p_align_p)
+	    p->p_align = p_align;
 	}
     }
 
@@ -7485,6 +7515,40 @@ rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   return true;
 }
 
+/* Return true if p_align in the ELF program header in ABFD is valid.  */
+
+static bool
+elf_is_p_align_valid (bfd *abfd)
+{
+  unsigned int i;
+  Elf_Internal_Phdr *segment;
+  unsigned int num_segments;
+  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
+  bfd_size_type maxpagesize = bed->maxpagesize;
+  bfd_size_type p_align = bed->p_align;
+
+  /* Return true if the default p_align value isn't set or the maximum
+     page size is the same as the minimum page size.  */
+  if (p_align == 0 || maxpagesize == bed->minpagesize)
+    return true;
+
+  /* When the default p_align value is set, p_align may be set to the
+     default p_align value while segments are aligned to the maximum
+     page size.  In this case, the input p_align will be ignored and
+     the maximum page size will be used to align the output segments.  */
+  segment = elf_tdata (abfd)->phdr;
+  num_segments = elf_elfheader (abfd)->e_phnum;
+  for (i = 0; i < num_segments; i++, segment++)
+    if (segment->p_type == PT_LOAD
+	&& (segment->p_align != p_align
+	    || vma_page_aligned_bias (segment->p_vaddr,
+				      segment->p_offset,
+				      maxpagesize) != 0))
+      return true;
+
+  return false;
+}
+
 /* Copy ELF program header information.  */
 
 static bool
@@ -7499,6 +7563,7 @@ copy_elf_program_header (bfd *ibfd, bfd *obfd)
   unsigned int num_segments;
   bool phdr_included = false;
   bool p_paddr_valid;
+  bool p_palign_valid;
   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
 
   iehdr = elf_elfheader (ibfd);
@@ -7519,6 +7584,8 @@ copy_elf_program_header (bfd *ibfd, bfd *obfd)
 	break;
       }
 
+  p_palign_valid = elf_is_p_align_valid (ibfd);
+
   for (i = 0, segment = elf_tdata (ibfd)->phdr;
        i < num_segments;
        i++, segment++)
@@ -7561,7 +7628,7 @@ copy_elf_program_header (bfd *ibfd, bfd *obfd)
       map->p_paddr = segment->p_paddr;
       map->p_paddr_valid = p_paddr_valid;
       map->p_align = segment->p_align;
-      map->p_align_valid = 1;
+      map->p_align_valid = p_palign_valid;
       map->p_vaddr_offset = 0;
 
       if (map->p_type == PT_GNU_RELRO
diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h
index 4c6b1f20340..d0e2779a061 100644
--- a/bfd/elfxx-target.h
+++ b/bfd/elfxx-target.h
@@ -400,6 +400,10 @@
 # error ELF_MINPAGESIZE > ELF_RELROPAGESIZE
 #endif
 
+#ifndef ELF_P_ALIGN
+#define ELF_P_ALIGN 0
+#endif
+
 #ifndef ELF_DYNAMIC_SEC_FLAGS
 /* Note that we set the SEC_IN_MEMORY flag for these sections.  */
 #define ELF_DYNAMIC_SEC_FLAGS			\
@@ -813,6 +817,7 @@ static const struct elf_backend_data elfNN_bed =
   ELF_MINPAGESIZE,		/* minpagesize */
   ELF_COMMONPAGESIZE,		/* commonpagesize */
   ELF_RELROPAGESIZE,		/* commonpagesize to use with -z relro */
+  ELF_P_ALIGN,			/* p_align */
   ELF_DYNAMIC_SEC_FLAGS,	/* dynamic_sec_flags */
   elf_backend_arch_data,
   elf_info_to_howto,
diff --git a/bfd/elfxx-x86.h b/bfd/elfxx-x86.h
index aab641ab751..0c95f3533af 100644
--- a/bfd/elfxx-x86.h
+++ b/bfd/elfxx-x86.h
@@ -732,6 +732,8 @@ extern void _bfd_x86_elf_link_report_relative_reloc
 #define elf_backend_fixup_gnu_properties \
   _bfd_x86_elf_link_fixup_gnu_properties
 
+#define ELF_P_ALIGN ELF_MINPAGESIZE
+
 /* Return true if H is a __start_SECNAME/__stop_SECNAME symbol for the
    SECNAME section which has been garbage collected by --gc-sections
    -z start-stop-gc.  */
diff --git a/include/bfdlink.h b/include/bfdlink.h
index 566529ee644..8cf34b05c1a 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -525,6 +525,9 @@ struct bfd_link_info
   /* TRUE if all symbol names should be unique.  */
   unsigned int unique_symbol : 1;
 
+  /* TRUE if maxpagesize is set on command-line.  */
+  unsigned int maxpagesize_is_set : 1;
+
   /* Char that may appear as the first char of a symbol, but should be
      skipped (like symbol_leading_char) when looking up symbols in
      wrap_hash.  Used by PowerPC Linux for 'dot' symbols.  */
diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
index bfaf8130a3e..5eadab9f4b9 100644
--- a/ld/emultempl/elf.em
+++ b/ld/emultempl/elf.em
@@ -721,6 +721,7 @@ fragment <<EOF
 	      || (link_info.maxpagesize & (link_info.maxpagesize - 1)) != 0)
 	    einfo (_("%F%P: invalid maximum page size \`%s'\n"),
 		   optarg + 14);
+	  link_info.maxpagesize_is_set = true;
 	}
       else if (startswith (optarg, "common-page-size="))
 	{
diff --git a/ld/ldelf.c b/ld/ldelf.c
index 529992b02ae..ee09df5f35c 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -72,6 +72,9 @@ ldelf_after_parse (void)
       link_info.dynamic_undefined_weak = 0;
     }
   after_parse_default ();
+  if (link_info.commonpagesize > link_info.maxpagesize)
+    einfo (_("%F%P: common page size (0x%v) > maximum page size (0x%v)\n"),
+	   link_info.commonpagesize, link_info.maxpagesize);
 }
 
 /* Handle the generation of DT_NEEDED tags.  */
diff --git a/ld/testsuite/ld-elf/elf.exp b/ld/testsuite/ld-elf/elf.exp
index 01d22faad9a..ae8f76db1c7 100644
--- a/ld/testsuite/ld-elf/elf.exp
+++ b/ld/testsuite/ld-elf/elf.exp
@@ -365,7 +365,7 @@ if { [istarget *-*-linux*]
     run_ld_link_exec_tests [list \
 	[list \
 	    "Run mbind2a" \
-	    "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000" \
+	    "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000,-z,max-page-size=0x4000" \
 	    "" \
 	    { mbind2a.s mbind2b.c } \
 	    "mbind2a" \
@@ -374,7 +374,7 @@ if { [istarget *-*-linux*]
 	] \
 	[list \
 	    "Run mbind2b" \
-	    "-static -Wl,-z,common-page-size=0x4000" \
+	    "-static -Wl,-z,common-page-size=0x4000,-z,max-page-size=0x4000" \
 	    "" \
 	    { mbind2a.s mbind2b.c } \
 	    "mbind2b" \
diff --git a/ld/testsuite/ld-elf/header.d b/ld/testsuite/ld-elf/header.d
index c4d174a98da..67f0c981920 100644
--- a/ld/testsuite/ld-elf/header.d
+++ b/ld/testsuite/ld-elf/header.d
@@ -1,5 +1,5 @@
 # target: *-*-linux* *-*-gnu* *-*-vxworks arm*-*-uclinuxfdpiceabi
-# ld: -T header.t -z max-page-size=0x100
+# ld: -T header.t -z max-page-size=0x100 -z common-page-size=0x100
 # objdump: -hpw
 
 #...
diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp
index ee03b565faf..25a8d0411d9 100644
--- a/ld/testsuite/ld-elf/linux-x86.exp
+++ b/ld/testsuite/ld-elf/linux-x86.exp
@@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
 	"" \
 	"tmpdir/indirect-extern-access-2.so" \
     ] \
+    [list \
+	"Run p_align-1a without PIE" \
+	"$NOPIE_LDFLAGS" \
+	"" \
+	{ p_align-1.c } \
+	"p_align-1a" \
+	"pass.out" \
+	"$NOPIE_CFLAGS" \
+    ] \
+    [list \
+	"Run p_align-1b with PIE" \
+	"-pie" \
+	"" \
+	{ p_align-1.c } \
+	"p_align-1b" \
+	"pass.out" \
+	"-fpie" \
+    ] \
+    [list \
+	"Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
+	"$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
+	"" \
+	{ p_align-1.c } \
+	"p_align-1c" \
+	"pass.out" \
+	"$NOPIE_CFLAGS" \
+    ] \
+    [list \
+	"Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
+	"-pie -Wl,-z,max-page-size=0x1000" \
+	"" \
+	{ p_align-1.c } \
+	"p_align-1d" \
+	"pass.out" \
+	"-fpie" \
+    ] \
 ]
 
 proc elfedit_test { options test output } {
diff --git a/ld/testsuite/ld-elf/p_align-1.c b/ld/testsuite/ld-elf/p_align-1.c
new file mode 100644
index 00000000000..6579cd74e52
--- /dev/null
+++ b/ld/testsuite/ld-elf/p_align-1.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#ifndef ALIGN
+# define ALIGN 0x800000
+#endif
+
+int
+__attribute__ ((weak))
+is_aligned (void *p, int align)
+{
+  return (((uintptr_t) p) & (align - 1)) == 0;
+}
+
+int foo __attribute__ ((aligned (ALIGN))) = 1;
+
+int
+main (void)
+{
+  if (!is_aligned (&foo, ALIGN))
+    abort ();
+  printf ("PASS\n");
+  return 0;
+}
diff --git a/ld/testsuite/ld-elf/page-size-1.d b/ld/testsuite/ld-elf/page-size-1.d
new file mode 100644
index 00000000000..04d2153b2f9
--- /dev/null
+++ b/ld/testsuite/ld-elf/page-size-1.d
@@ -0,0 +1,4 @@
+#source: dummy.s
+#ld: -z common-page-size=0x4000 -z max-page-size=0x1000
+#error: common page size \(0x4000\) > maximum page size \(0x1000\)
+#target: *-*-linux-gnu *-*-gnu* arm*-*-uclinuxfdpiceabi
diff --git a/ld/testsuite/ld-elf/pr26936.d b/ld/testsuite/ld-elf/pr26936.d
index 0a2831ddba8..c479f475829 100644
--- a/ld/testsuite/ld-elf/pr26936.d
+++ b/ld/testsuite/ld-elf/pr26936.d
@@ -2,7 +2,7 @@
 #source: pr26936b.s
 #source: pr26936c.s
 #as: --gen-debug
-#ld: -z noseparate-code -Ttext-segment 0x10000 -z max-page-size=0x1000
+#ld: -z noseparate-code -Ttext-segment 0x10000 -z max-page-size=0x1000 -z common-page-size=0x1000
 #readelf: -wL -W
 #target: [check_shared_lib_support]
 # Assembly source file for the HPPA assembler is renamed and modifed by
diff --git a/ld/testsuite/ld-elf/seg.d b/ld/testsuite/ld-elf/seg.d
index 3ff7aba3166..9dce11e59e3 100644
--- a/ld/testsuite/ld-elf/seg.d
+++ b/ld/testsuite/ld-elf/seg.d
@@ -1,6 +1,6 @@
 #target: *-*-linux* *-*-gnu* *-*-vxworks arm*-*-uclinuxfdpiceabi
 #source: seg.s
-#ld: -T seg.t -z max-page-size=0x1000
+#ld: -T seg.t -z max-page-size=0x1000 -z common-page-size=0x1000
 #readelf: -l --wide
 
 #...
diff --git a/ld/testsuite/ld-pru/pru_irq_map-1.d b/ld/testsuite/ld-pru/pru_irq_map-1.d
index bbdf5b6ce4e..2538f4c4edb 100644
--- a/ld/testsuite/ld-pru/pru_irq_map-1.d
+++ b/ld/testsuite/ld-pru/pru_irq_map-1.d
@@ -1,4 +1,4 @@
-#name: pru_irq_map special section for host
+#name: pru_irq_map special section for host 1
 #source: pru_irq_map.s
 #ld: --defsym=__HEAP_SIZE=0 --defsym=__STACK_SIZE=0
 #readelf: -l --wide
@@ -9,7 +9,7 @@
 Program Headers:
  +Type +Offset +VirtAddr +PhysAddr +FileSiz +MemSiz +Flg +Align
  +LOAD +0x[0-9a-f]+ 0x0+ 0x0+ 0x0+8 0x0+8 RW  0x1
- +LOAD +0x[0-9a-f]+ 0x20+ 0x20+ 0x0+4 0x0+4 R E 0x1
+ +LOAD +0x[0-9a-f]+ 0x20+ 0x20+ 0x0+4 0x0+4 R E 0x4
 
  Section to Segment mapping:
  +Segment Sections...
diff --git a/ld/testsuite/ld-pru/pru_irq_map-2.d b/ld/testsuite/ld-pru/pru_irq_map-2.d
index 31665955de7..d6c583f1d9c 100644
--- a/ld/testsuite/ld-pru/pru_irq_map-2.d
+++ b/ld/testsuite/ld-pru/pru_irq_map-2.d
@@ -1,4 +1,4 @@
-#name: pru_irq_map special section for host
+#name: pru_irq_map special section for host 2
 #source: pru_irq_map.s
 #ld: --defsym=__HEAP_SIZE=0 --defsym=__STACK_SIZE=0
 #readelf: -S --wide
diff --git a/ld/testsuite/ld-scripts/pr23571.d b/ld/testsuite/ld-scripts/pr23571.d
index adf479660bb..45b40592d3b 100644
--- a/ld/testsuite/ld-scripts/pr23571.d
+++ b/ld/testsuite/ld-scripts/pr23571.d
@@ -1,5 +1,5 @@
 #source: align2a.s
-#ld: -T pr23571.t -z common-page-size=0x1000
+#ld: -T pr23571.t -z common-page-size=0x1000 -z max-page-size=0x1000
 #objdump: -h -w
 
 .*: +file format .*
diff --git a/ld/testsuite/ld-scripts/rgn-at5.d b/ld/testsuite/ld-scripts/rgn-at5.d
index 767285ca6ab..e9ab08123df 100644
--- a/ld/testsuite/ld-scripts/rgn-at5.d
+++ b/ld/testsuite/ld-scripts/rgn-at5.d
@@ -1,6 +1,6 @@
 # name: rgn-at5
 # source: rgn-at5.s
-# ld: -T rgn-at5.t -z max-page-size=0x1000
+# ld: -T rgn-at5.t -z max-page-size=0x1000 -z common-page-size=0x1000
 # objdump: -w -h
 # target: *-*-linux* *-*-gnu* arm*-*-uclinuxfdpiceabi
 # xfail: rx-*-*
-- 
2.33.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2021-12-29 19:39 [PATCH v3] elf: Set p_align to the minimum page size if possible H.J. Lu
@ 2022-01-14  8:12 ` Jan Beulich
  2022-02-10 12:44   ` Jan Beulich
  2022-01-14  8:27 ` Jan Beulich
  1 sibling, 1 reply; 16+ messages in thread
From: Jan Beulich @ 2022-01-14  8:12 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
> --- /dev/null
> +++ b/ld/testsuite/ld-elf/p_align-1.c
> @@ -0,0 +1,25 @@
> +#include <stdio.h>
> +#include <stdint.h>
> +#include <stdlib.h>
> +
> +#ifndef ALIGN
> +# define ALIGN 0x800000
> +#endif
> +
> +int
> +__attribute__ ((weak))
> +is_aligned (void *p, int align)
> +{
> +  return (((uintptr_t) p) & (align - 1)) == 0;
> +}
> +
> +int foo __attribute__ ((aligned (ALIGN))) = 1;

Alongside newer distros I also continue to build and test binutils on an
oldish one. gcc 4.3 looks to silently ignore[1] alignment values larger
than 1Mb on at least 32-bit x86. Hence all 4 derived tests fail there. I
think you want to verify that foo's alignment is actually 8Mb in the
object file, or use an assembler source instead of a C one (albeit I can
see that this would undermine your PIE / non-PIE test variants).

Jan

[1] It's actually worse than "ignore": The variable gets no alignment at
all then, not even the expected 4-byte one.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2021-12-29 19:39 [PATCH v3] elf: Set p_align to the minimum page size if possible H.J. Lu
  2022-01-14  8:12 ` Jan Beulich
@ 2022-01-14  8:27 ` Jan Beulich
  2022-01-14 13:03   ` H.J. Lu
  1 sibling, 1 reply; 16+ messages in thread
From: Jan Beulich @ 2022-01-14  8:27 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
> --- a/ld/testsuite/ld-elf/linux-x86.exp
> +++ b/ld/testsuite/ld-elf/linux-x86.exp
> @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
>  	"" \
>  	"tmpdir/indirect-extern-access-2.so" \
>      ] \
> +    [list \
> +	"Run p_align-1a without PIE" \
> +	"$NOPIE_LDFLAGS" \
> +	"" \
> +	{ p_align-1.c } \
> +	"p_align-1a" \
> +	"pass.out" \
> +	"$NOPIE_CFLAGS" \
> +    ] \
> +    [list \
> +	"Run p_align-1b with PIE" \
> +	"-pie" \
> +	"" \
> +	{ p_align-1.c } \
> +	"p_align-1b" \
> +	"pass.out" \
> +	"-fpie" \
> +    ] \
> +    [list \
> +	"Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
> +	"$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
> +	"" \
> +	{ p_align-1.c } \
> +	"p_align-1c" \
> +	"pass.out" \
> +	"$NOPIE_CFLAGS" \
> +    ] \
> +    [list \
> +	"Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
> +	"-pie -Wl,-z,max-page-size=0x1000" \
> +	"" \
> +	{ p_align-1.c } \
> +	"p_align-1d" \
> +	"pass.out" \
> +	"-fpie" \
> +    ] \
>  ]

The two PIE variants of this also fail for me on glibc 2.26. Looks
like LOAD segments' alignment isn't being honored there, at least
not if it's as big as it is here.

Jan


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2022-01-14  8:27 ` Jan Beulich
@ 2022-01-14 13:03   ` H.J. Lu
  2022-01-14 13:40     ` Jan Beulich
  0 siblings, 1 reply; 16+ messages in thread
From: H.J. Lu @ 2022-01-14 13:03 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Binutils

On Fri, Jan 14, 2022 at 12:27 AM Jan Beulich <jbeulich@suse.com> wrote:
>
> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
> > --- a/ld/testsuite/ld-elf/linux-x86.exp
> > +++ b/ld/testsuite/ld-elf/linux-x86.exp
> > @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
> >       "" \
> >       "tmpdir/indirect-extern-access-2.so" \
> >      ] \
> > +    [list \
> > +     "Run p_align-1a without PIE" \
> > +     "$NOPIE_LDFLAGS" \
> > +     "" \
> > +     { p_align-1.c } \
> > +     "p_align-1a" \
> > +     "pass.out" \
> > +     "$NOPIE_CFLAGS" \
> > +    ] \
> > +    [list \
> > +     "Run p_align-1b with PIE" \
> > +     "-pie" \
> > +     "" \
> > +     { p_align-1.c } \
> > +     "p_align-1b" \
> > +     "pass.out" \
> > +     "-fpie" \
> > +    ] \
> > +    [list \
> > +     "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
> > +     "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
> > +     "" \
> > +     { p_align-1.c } \
> > +     "p_align-1c" \
> > +     "pass.out" \
> > +     "$NOPIE_CFLAGS" \
> > +    ] \
> > +    [list \
> > +     "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
> > +     "-pie -Wl,-z,max-page-size=0x1000" \
> > +     "" \
> > +     { p_align-1.c } \
> > +     "p_align-1d" \
> > +     "pass.out" \
> > +     "-fpie" \
> > +    ] \
> >  ]
>
> The two PIE variants of this also fail for me on glibc 2.26. Looks
> like LOAD segments' alignment isn't being honored there, at least
> not if it's as big as it is here.
>

The PIE alignment needs the kernel fix:

commit ce81bb256a224259ab686742a6284930cbe4f1fa
Author: Chris Kennelly <ckennelly@google.com>
Date:   Thu Oct 15 20:12:32 2020 -0700

    fs/binfmt_elf: use PT_LOAD p_align values for suitable start address


-- 
H.J.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2022-01-14 13:03   ` H.J. Lu
@ 2022-01-14 13:40     ` Jan Beulich
  2022-01-14 14:02       ` H.J. Lu
  0 siblings, 1 reply; 16+ messages in thread
From: Jan Beulich @ 2022-01-14 13:40 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Binutils

On 14.01.2022 14:03, H.J. Lu wrote:
> On Fri, Jan 14, 2022 at 12:27 AM Jan Beulich <jbeulich@suse.com> wrote:
>>
>> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
>>> --- a/ld/testsuite/ld-elf/linux-x86.exp
>>> +++ b/ld/testsuite/ld-elf/linux-x86.exp
>>> @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
>>>       "" \
>>>       "tmpdir/indirect-extern-access-2.so" \
>>>      ] \
>>> +    [list \
>>> +     "Run p_align-1a without PIE" \
>>> +     "$NOPIE_LDFLAGS" \
>>> +     "" \
>>> +     { p_align-1.c } \
>>> +     "p_align-1a" \
>>> +     "pass.out" \
>>> +     "$NOPIE_CFLAGS" \
>>> +    ] \
>>> +    [list \
>>> +     "Run p_align-1b with PIE" \
>>> +     "-pie" \
>>> +     "" \
>>> +     { p_align-1.c } \
>>> +     "p_align-1b" \
>>> +     "pass.out" \
>>> +     "-fpie" \
>>> +    ] \
>>> +    [list \
>>> +     "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
>>> +     "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
>>> +     "" \
>>> +     { p_align-1.c } \
>>> +     "p_align-1c" \
>>> +     "pass.out" \
>>> +     "$NOPIE_CFLAGS" \
>>> +    ] \
>>> +    [list \
>>> +     "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
>>> +     "-pie -Wl,-z,max-page-size=0x1000" \
>>> +     "" \
>>> +     { p_align-1.c } \
>>> +     "p_align-1d" \
>>> +     "pass.out" \
>>> +     "-fpie" \
>>> +    ] \
>>>  ]
>>
>> The two PIE variants of this also fail for me on glibc 2.26. Looks
>> like LOAD segments' alignment isn't being honored there, at least
>> not if it's as big as it is here.
>>
> 
> The PIE alignment needs the kernel fix:
> 
> commit ce81bb256a224259ab686742a6284930cbe4f1fa
> Author: Chris Kennelly <ckennelly@google.com>
> Date:   Thu Oct 15 20:12:32 2020 -0700
> 
>     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address

Well, then the test needs to be skipped if that fix is not in place.
After all you're testing binutils behavior here, not kernel or libc one.
I'm running a variety of (largely up-to-date) kernels on all of my
systems. But it looks like our kernel folks decided against backporting
this particular change. And I don't think you expect people to remember
to run the testsuite only on top of "certain" kernels?

Jan


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2022-01-14 13:40     ` Jan Beulich
@ 2022-01-14 14:02       ` H.J. Lu
  2022-01-14 14:07         ` Jan Beulich
  0 siblings, 1 reply; 16+ messages in thread
From: H.J. Lu @ 2022-01-14 14:02 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Binutils

On Fri, Jan 14, 2022 at 5:40 AM Jan Beulich <jbeulich@suse.com> wrote:
>
> On 14.01.2022 14:03, H.J. Lu wrote:
> > On Fri, Jan 14, 2022 at 12:27 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>
> >> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
> >>> --- a/ld/testsuite/ld-elf/linux-x86.exp
> >>> +++ b/ld/testsuite/ld-elf/linux-x86.exp
> >>> @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
> >>>       "" \
> >>>       "tmpdir/indirect-extern-access-2.so" \
> >>>      ] \
> >>> +    [list \
> >>> +     "Run p_align-1a without PIE" \
> >>> +     "$NOPIE_LDFLAGS" \
> >>> +     "" \
> >>> +     { p_align-1.c } \
> >>> +     "p_align-1a" \
> >>> +     "pass.out" \
> >>> +     "$NOPIE_CFLAGS" \
> >>> +    ] \
> >>> +    [list \
> >>> +     "Run p_align-1b with PIE" \
> >>> +     "-pie" \
> >>> +     "" \
> >>> +     { p_align-1.c } \
> >>> +     "p_align-1b" \
> >>> +     "pass.out" \
> >>> +     "-fpie" \
> >>> +    ] \
> >>> +    [list \
> >>> +     "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
> >>> +     "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
> >>> +     "" \
> >>> +     { p_align-1.c } \
> >>> +     "p_align-1c" \
> >>> +     "pass.out" \
> >>> +     "$NOPIE_CFLAGS" \
> >>> +    ] \
> >>> +    [list \
> >>> +     "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
> >>> +     "-pie -Wl,-z,max-page-size=0x1000" \
> >>> +     "" \
> >>> +     { p_align-1.c } \
> >>> +     "p_align-1d" \
> >>> +     "pass.out" \
> >>> +     "-fpie" \
> >>> +    ] \
> >>>  ]
> >>
> >> The two PIE variants of this also fail for me on glibc 2.26. Looks
> >> like LOAD segments' alignment isn't being honored there, at least
> >> not if it's as big as it is here.
> >>
> >
> > The PIE alignment needs the kernel fix:
> >
> > commit ce81bb256a224259ab686742a6284930cbe4f1fa
> > Author: Chris Kennelly <ckennelly@google.com>
> > Date:   Thu Oct 15 20:12:32 2020 -0700
> >
> >     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address
>
> Well, then the test needs to be skipped if that fix is not in place.
> After all you're testing binutils behavior here, not kernel or libc one.
> I'm running a variety of (largely up-to-date) kernels on all of my
> systems. But it looks like our kernel folks decided against backporting
> this particular change. And I don't think you expect people to remember
> to run the testsuite only on top of "certain" kernels?

Care to submit a patch?

-- 
H.J.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2022-01-14 14:02       ` H.J. Lu
@ 2022-01-14 14:07         ` Jan Beulich
  2022-01-14 14:14           ` H.J. Lu
  0 siblings, 1 reply; 16+ messages in thread
From: Jan Beulich @ 2022-01-14 14:07 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Binutils

On 14.01.2022 15:02, H.J. Lu wrote:
> On Fri, Jan 14, 2022 at 5:40 AM Jan Beulich <jbeulich@suse.com> wrote:
>>
>> On 14.01.2022 14:03, H.J. Lu wrote:
>>> On Fri, Jan 14, 2022 at 12:27 AM Jan Beulich <jbeulich@suse.com> wrote:
>>>>
>>>> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
>>>>> --- a/ld/testsuite/ld-elf/linux-x86.exp
>>>>> +++ b/ld/testsuite/ld-elf/linux-x86.exp
>>>>> @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
>>>>>       "" \
>>>>>       "tmpdir/indirect-extern-access-2.so" \
>>>>>      ] \
>>>>> +    [list \
>>>>> +     "Run p_align-1a without PIE" \
>>>>> +     "$NOPIE_LDFLAGS" \
>>>>> +     "" \
>>>>> +     { p_align-1.c } \
>>>>> +     "p_align-1a" \
>>>>> +     "pass.out" \
>>>>> +     "$NOPIE_CFLAGS" \
>>>>> +    ] \
>>>>> +    [list \
>>>>> +     "Run p_align-1b with PIE" \
>>>>> +     "-pie" \
>>>>> +     "" \
>>>>> +     { p_align-1.c } \
>>>>> +     "p_align-1b" \
>>>>> +     "pass.out" \
>>>>> +     "-fpie" \
>>>>> +    ] \
>>>>> +    [list \
>>>>> +     "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
>>>>> +     "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
>>>>> +     "" \
>>>>> +     { p_align-1.c } \
>>>>> +     "p_align-1c" \
>>>>> +     "pass.out" \
>>>>> +     "$NOPIE_CFLAGS" \
>>>>> +    ] \
>>>>> +    [list \
>>>>> +     "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
>>>>> +     "-pie -Wl,-z,max-page-size=0x1000" \
>>>>> +     "" \
>>>>> +     { p_align-1.c } \
>>>>> +     "p_align-1d" \
>>>>> +     "pass.out" \
>>>>> +     "-fpie" \
>>>>> +    ] \
>>>>>  ]
>>>>
>>>> The two PIE variants of this also fail for me on glibc 2.26. Looks
>>>> like LOAD segments' alignment isn't being honored there, at least
>>>> not if it's as big as it is here.
>>>>
>>>
>>> The PIE alignment needs the kernel fix:
>>>
>>> commit ce81bb256a224259ab686742a6284930cbe4f1fa
>>> Author: Chris Kennelly <ckennelly@google.com>
>>> Date:   Thu Oct 15 20:12:32 2020 -0700
>>>
>>>     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address
>>
>> Well, then the test needs to be skipped if that fix is not in place.
>> After all you're testing binutils behavior here, not kernel or libc one.
>> I'm running a variety of (largely up-to-date) kernels on all of my
>> systems. But it looks like our kernel folks decided against backporting
>> this particular change. And I don't think you expect people to remember
>> to run the testsuite only on top of "certain" kernels?
> 
> Care to submit a patch?

I have no idea what to check for. I would really expect you to fix such
an issue (or really two of them, considering the other problem) recently
introduced by you.

Jan


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2022-01-14 14:07         ` Jan Beulich
@ 2022-01-14 14:14           ` H.J. Lu
  2022-01-17 10:38             ` Jan Beulich
  0 siblings, 1 reply; 16+ messages in thread
From: H.J. Lu @ 2022-01-14 14:14 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Binutils

On Fri, Jan 14, 2022 at 6:08 AM Jan Beulich <jbeulich@suse.com> wrote:
>
> On 14.01.2022 15:02, H.J. Lu wrote:
> > On Fri, Jan 14, 2022 at 5:40 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>
> >> On 14.01.2022 14:03, H.J. Lu wrote:
> >>> On Fri, Jan 14, 2022 at 12:27 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>>>
> >>>> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
> >>>>> --- a/ld/testsuite/ld-elf/linux-x86.exp
> >>>>> +++ b/ld/testsuite/ld-elf/linux-x86.exp
> >>>>> @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
> >>>>>       "" \
> >>>>>       "tmpdir/indirect-extern-access-2.so" \
> >>>>>      ] \
> >>>>> +    [list \
> >>>>> +     "Run p_align-1a without PIE" \
> >>>>> +     "$NOPIE_LDFLAGS" \
> >>>>> +     "" \
> >>>>> +     { p_align-1.c } \
> >>>>> +     "p_align-1a" \
> >>>>> +     "pass.out" \
> >>>>> +     "$NOPIE_CFLAGS" \
> >>>>> +    ] \
> >>>>> +    [list \
> >>>>> +     "Run p_align-1b with PIE" \
> >>>>> +     "-pie" \
> >>>>> +     "" \
> >>>>> +     { p_align-1.c } \
> >>>>> +     "p_align-1b" \
> >>>>> +     "pass.out" \
> >>>>> +     "-fpie" \
> >>>>> +    ] \
> >>>>> +    [list \
> >>>>> +     "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
> >>>>> +     "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
> >>>>> +     "" \
> >>>>> +     { p_align-1.c } \
> >>>>> +     "p_align-1c" \
> >>>>> +     "pass.out" \
> >>>>> +     "$NOPIE_CFLAGS" \
> >>>>> +    ] \
> >>>>> +    [list \
> >>>>> +     "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
> >>>>> +     "-pie -Wl,-z,max-page-size=0x1000" \
> >>>>> +     "" \
> >>>>> +     { p_align-1.c } \
> >>>>> +     "p_align-1d" \
> >>>>> +     "pass.out" \
> >>>>> +     "-fpie" \
> >>>>> +    ] \
> >>>>>  ]
> >>>>
> >>>> The two PIE variants of this also fail for me on glibc 2.26. Looks
> >>>> like LOAD segments' alignment isn't being honored there, at least
> >>>> not if it's as big as it is here.
> >>>>
> >>>
> >>> The PIE alignment needs the kernel fix:
> >>>
> >>> commit ce81bb256a224259ab686742a6284930cbe4f1fa
> >>> Author: Chris Kennelly <ckennelly@google.com>
> >>> Date:   Thu Oct 15 20:12:32 2020 -0700
> >>>
> >>>     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address
> >>
> >> Well, then the test needs to be skipped if that fix is not in place.
> >> After all you're testing binutils behavior here, not kernel or libc one.
> >> I'm running a variety of (largely up-to-date) kernels on all of my
> >> systems. But it looks like our kernel folks decided against backporting
> >> this particular change. And I don't think you expect people to remember
> >> to run the testsuite only on top of "certain" kernels?
> >
> > Care to submit a patch?
>
> I have no idea what to check for. I would really expect you to fix such
> an issue (or really two of them, considering the other problem) recently
> introduced by you.

What compiler are you using on the broken kernel?

-- 
H.J.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2022-01-14 14:14           ` H.J. Lu
@ 2022-01-17 10:38             ` Jan Beulich
  2022-01-17 13:44               ` [PATCH] ld: Require GCC 8.0 for p_align-1.c tests H.J. Lu
  0 siblings, 1 reply; 16+ messages in thread
From: Jan Beulich @ 2022-01-17 10:38 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Binutils

On 14.01.2022 15:14, H.J. Lu wrote:
> On Fri, Jan 14, 2022 at 6:08 AM Jan Beulich <jbeulich@suse.com> wrote:
>>
>> On 14.01.2022 15:02, H.J. Lu wrote:
>>> On Fri, Jan 14, 2022 at 5:40 AM Jan Beulich <jbeulich@suse.com> wrote:
>>>>
>>>> On 14.01.2022 14:03, H.J. Lu wrote:
>>>>> On Fri, Jan 14, 2022 at 12:27 AM Jan Beulich <jbeulich@suse.com> wrote:
>>>>>>
>>>>>> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
>>>>>>> --- a/ld/testsuite/ld-elf/linux-x86.exp
>>>>>>> +++ b/ld/testsuite/ld-elf/linux-x86.exp
>>>>>>> @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
>>>>>>>       "" \
>>>>>>>       "tmpdir/indirect-extern-access-2.so" \
>>>>>>>      ] \
>>>>>>> +    [list \
>>>>>>> +     "Run p_align-1a without PIE" \
>>>>>>> +     "$NOPIE_LDFLAGS" \
>>>>>>> +     "" \
>>>>>>> +     { p_align-1.c } \
>>>>>>> +     "p_align-1a" \
>>>>>>> +     "pass.out" \
>>>>>>> +     "$NOPIE_CFLAGS" \
>>>>>>> +    ] \
>>>>>>> +    [list \
>>>>>>> +     "Run p_align-1b with PIE" \
>>>>>>> +     "-pie" \
>>>>>>> +     "" \
>>>>>>> +     { p_align-1.c } \
>>>>>>> +     "p_align-1b" \
>>>>>>> +     "pass.out" \
>>>>>>> +     "-fpie" \
>>>>>>> +    ] \
>>>>>>> +    [list \
>>>>>>> +     "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
>>>>>>> +     "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
>>>>>>> +     "" \
>>>>>>> +     { p_align-1.c } \
>>>>>>> +     "p_align-1c" \
>>>>>>> +     "pass.out" \
>>>>>>> +     "$NOPIE_CFLAGS" \
>>>>>>> +    ] \
>>>>>>> +    [list \
>>>>>>> +     "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
>>>>>>> +     "-pie -Wl,-z,max-page-size=0x1000" \
>>>>>>> +     "" \
>>>>>>> +     { p_align-1.c } \
>>>>>>> +     "p_align-1d" \
>>>>>>> +     "pass.out" \
>>>>>>> +     "-fpie" \
>>>>>>> +    ] \
>>>>>>>  ]
>>>>>>
>>>>>> The two PIE variants of this also fail for me on glibc 2.26. Looks
>>>>>> like LOAD segments' alignment isn't being honored there, at least
>>>>>> not if it's as big as it is here.
>>>>>>
>>>>>
>>>>> The PIE alignment needs the kernel fix:
>>>>>
>>>>> commit ce81bb256a224259ab686742a6284930cbe4f1fa
>>>>> Author: Chris Kennelly <ckennelly@google.com>
>>>>> Date:   Thu Oct 15 20:12:32 2020 -0700
>>>>>
>>>>>     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address
>>>>
>>>> Well, then the test needs to be skipped if that fix is not in place.
>>>> After all you're testing binutils behavior here, not kernel or libc one.
>>>> I'm running a variety of (largely up-to-date) kernels on all of my
>>>> systems. But it looks like our kernel folks decided against backporting
>>>> this particular change. And I don't think you expect people to remember
>>>> to run the testsuite only on top of "certain" kernels?
>>>
>>> Care to submit a patch?
>>
>> I have no idea what to check for. I would really expect you to fix such
>> an issue (or really two of them, considering the other problem) recently
>> introduced by you.
> 
> What compiler are you using on the broken kernel?

gcc 7.4.1

No idea how that matters, though.

Jan


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH] ld: Require GCC 8.0 for p_align-1.c tests
  2022-01-17 10:38             ` Jan Beulich
@ 2022-01-17 13:44               ` H.J. Lu
  2022-01-17 13:51                 ` Jan Beulich
  0 siblings, 1 reply; 16+ messages in thread
From: H.J. Lu @ 2022-01-17 13:44 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Binutils

[-- Attachment #1: Type: text/plain, Size: 3278 bytes --]

On Mon, Jan 17, 2022 at 2:38 AM Jan Beulich <jbeulich@suse.com> wrote:
>
> On 14.01.2022 15:14, H.J. Lu wrote:
> > On Fri, Jan 14, 2022 at 6:08 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>
> >> On 14.01.2022 15:02, H.J. Lu wrote:
> >>> On Fri, Jan 14, 2022 at 5:40 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>>>
> >>>> On 14.01.2022 14:03, H.J. Lu wrote:
> >>>>> On Fri, Jan 14, 2022 at 12:27 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>>>>>
> >>>>>> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
> >>>>>>> --- a/ld/testsuite/ld-elf/linux-x86.exp
> >>>>>>> +++ b/ld/testsuite/ld-elf/linux-x86.exp
> >>>>>>> @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
> >>>>>>>       "" \
> >>>>>>>       "tmpdir/indirect-extern-access-2.so" \
> >>>>>>>      ] \
> >>>>>>> +    [list \
> >>>>>>> +     "Run p_align-1a without PIE" \
> >>>>>>> +     "$NOPIE_LDFLAGS" \
> >>>>>>> +     "" \
> >>>>>>> +     { p_align-1.c } \
> >>>>>>> +     "p_align-1a" \
> >>>>>>> +     "pass.out" \
> >>>>>>> +     "$NOPIE_CFLAGS" \
> >>>>>>> +    ] \
> >>>>>>> +    [list \
> >>>>>>> +     "Run p_align-1b with PIE" \
> >>>>>>> +     "-pie" \
> >>>>>>> +     "" \
> >>>>>>> +     { p_align-1.c } \
> >>>>>>> +     "p_align-1b" \
> >>>>>>> +     "pass.out" \
> >>>>>>> +     "-fpie" \
> >>>>>>> +    ] \
> >>>>>>> +    [list \
> >>>>>>> +     "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
> >>>>>>> +     "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
> >>>>>>> +     "" \
> >>>>>>> +     { p_align-1.c } \
> >>>>>>> +     "p_align-1c" \
> >>>>>>> +     "pass.out" \
> >>>>>>> +     "$NOPIE_CFLAGS" \
> >>>>>>> +    ] \
> >>>>>>> +    [list \
> >>>>>>> +     "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
> >>>>>>> +     "-pie -Wl,-z,max-page-size=0x1000" \
> >>>>>>> +     "" \
> >>>>>>> +     { p_align-1.c } \
> >>>>>>> +     "p_align-1d" \
> >>>>>>> +     "pass.out" \
> >>>>>>> +     "-fpie" \
> >>>>>>> +    ] \
> >>>>>>>  ]
> >>>>>>
> >>>>>> The two PIE variants of this also fail for me on glibc 2.26. Looks
> >>>>>> like LOAD segments' alignment isn't being honored there, at least
> >>>>>> not if it's as big as it is here.
> >>>>>>
> >>>>>
> >>>>> The PIE alignment needs the kernel fix:
> >>>>>
> >>>>> commit ce81bb256a224259ab686742a6284930cbe4f1fa
> >>>>> Author: Chris Kennelly <ckennelly@google.com>
> >>>>> Date:   Thu Oct 15 20:12:32 2020 -0700
> >>>>>
> >>>>>     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address
> >>>>
> >>>> Well, then the test needs to be skipped if that fix is not in place.
> >>>> After all you're testing binutils behavior here, not kernel or libc one.
> >>>> I'm running a variety of (largely up-to-date) kernels on all of my
> >>>> systems. But it looks like our kernel folks decided against backporting
> >>>> this particular change. And I don't think you expect people to remember
> >>>> to run the testsuite only on top of "certain" kernels?
> >>>
> >>> Care to submit a patch?
> >>
> >> I have no idea what to check for. I would really expect you to fix such
> >> an issue (or really two of them, considering the other problem) recently
> >> introduced by you.
> >
> > What compiler are you using on the broken kernel?
>
> gcc 7.4.1
>
> No idea how that matters, though.

Try this.

-- 
H.J.

[-- Attachment #2: 0001-ld-Require-GCC-8.0-for-p_align-1.c-tests.patch --]
[-- Type: text/x-patch, Size: 2616 bytes --]

From 0120bae52ccdb050203bd90b310cdca9995e6e42 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Mon, 17 Jan 2022 05:33:10 -0800
Subject: [PATCH] ld: Require GCC 8.0 for p_align-1.c tests

Require GCC 8.0 for p_align-1.c tests to avoid GCC and Linux kernel
which ignore aligned in p_align-1.c.

	PR ld/28689
	PR ld/28695
	* ld-elf/linux-x86.exp: Require GCC 8.0 for p_align-1.c tests.
---
 ld/testsuite/ld-elf/linux-x86.exp | 79 +++++++++++++++++--------------
 1 file changed, 43 insertions(+), 36 deletions(-)

diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp
index 2e0cbd37f17..6bc280f3cdd 100644
--- a/ld/testsuite/ld-elf/linux-x86.exp
+++ b/ld/testsuite/ld-elf/linux-x86.exp
@@ -185,44 +185,51 @@ run_ld_link_exec_tests [list \
 	"" \
 	"tmpdir/indirect-extern-access-2.so" \
     ] \
-    [list \
-	"Run p_align-1a without PIE" \
-	"$NOPIE_LDFLAGS" \
-	"" \
-	{ p_align-1.c } \
-	"p_align-1a" \
-	"pass.out" \
-	"$NOPIE_CFLAGS" \
-    ] \
-    [list \
-	"Run p_align-1b with PIE" \
-	"-pie" \
-	"" \
-	{ p_align-1.c } \
-	"p_align-1b" \
-	"pass.out" \
-	"-fpie" \
-    ] \
-    [list \
-	"Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
-	"$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
-	"" \
-	{ p_align-1.c } \
-	"p_align-1c" \
-	"pass.out" \
-	"$NOPIE_CFLAGS" \
-    ] \
-    [list \
-	"Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
-	"-pie -Wl,-z,max-page-size=0x1000" \
-	"" \
-	{ p_align-1.c } \
-	"p_align-1d" \
-	"pass.out" \
-	"-fpie" \
-    ] \
 ]
 
+# Require GCC 8.0 to avoid GCC and Linux kernel which ignore aligned
+# attribute in p_align-1.c.
+if { [at_least_gcc_version 8 0] } {
+    run_ld_link_exec_tests [list \
+	[list \
+	    "Run p_align-1a without PIE" \
+	    "$NOPIE_LDFLAGS" \
+	    "" \
+	    { p_align-1.c } \
+	    "p_align-1a" \
+	    "pass.out" \
+	    "$NOPIE_CFLAGS" \
+	] \
+	[list \
+	    "Run p_align-1b with PIE" \
+	    "-pie" \
+	    "" \
+	    { p_align-1.c } \
+	    "p_align-1b" \
+	    "pass.out" \
+	    "-fpie" \
+	] \
+	[list \
+	    "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
+	    "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
+	    "" \
+	    { p_align-1.c } \
+	    "p_align-1c" \
+	    "pass.out" \
+	    "$NOPIE_CFLAGS" \
+	] \
+	[list \
+	    "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
+	    "-pie -Wl,-z,max-page-size=0x1000" \
+	    "" \
+	    { p_align-1.c } \
+	    "p_align-1d" \
+	    "pass.out" \
+	    "-fpie" \
+	] \
+    ]
+}
+
 proc elfedit_test { options test output } {
     global ELFEDIT
     global READELF
-- 
2.34.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] ld: Require GCC 8.0 for p_align-1.c tests
  2022-01-17 13:44               ` [PATCH] ld: Require GCC 8.0 for p_align-1.c tests H.J. Lu
@ 2022-01-17 13:51                 ` Jan Beulich
  2022-01-17 13:54                   ` H.J. Lu
  0 siblings, 1 reply; 16+ messages in thread
From: Jan Beulich @ 2022-01-17 13:51 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Binutils

On 17.01.2022 14:44, H.J. Lu wrote:
> On Mon, Jan 17, 2022 at 2:38 AM Jan Beulich <jbeulich@suse.com> wrote:
>>
>> On 14.01.2022 15:14, H.J. Lu wrote:
>>> On Fri, Jan 14, 2022 at 6:08 AM Jan Beulich <jbeulich@suse.com> wrote:
>>>>
>>>> On 14.01.2022 15:02, H.J. Lu wrote:
>>>>> On Fri, Jan 14, 2022 at 5:40 AM Jan Beulich <jbeulich@suse.com> wrote:
>>>>>>
>>>>>> On 14.01.2022 14:03, H.J. Lu wrote:
>>>>>>> On Fri, Jan 14, 2022 at 12:27 AM Jan Beulich <jbeulich@suse.com> wrote:
>>>>>>>>
>>>>>>>> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
>>>>>>>>> --- a/ld/testsuite/ld-elf/linux-x86.exp
>>>>>>>>> +++ b/ld/testsuite/ld-elf/linux-x86.exp
>>>>>>>>> @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
>>>>>>>>>       "" \
>>>>>>>>>       "tmpdir/indirect-extern-access-2.so" \
>>>>>>>>>      ] \
>>>>>>>>> +    [list \
>>>>>>>>> +     "Run p_align-1a without PIE" \
>>>>>>>>> +     "$NOPIE_LDFLAGS" \
>>>>>>>>> +     "" \
>>>>>>>>> +     { p_align-1.c } \
>>>>>>>>> +     "p_align-1a" \
>>>>>>>>> +     "pass.out" \
>>>>>>>>> +     "$NOPIE_CFLAGS" \
>>>>>>>>> +    ] \
>>>>>>>>> +    [list \
>>>>>>>>> +     "Run p_align-1b with PIE" \
>>>>>>>>> +     "-pie" \
>>>>>>>>> +     "" \
>>>>>>>>> +     { p_align-1.c } \
>>>>>>>>> +     "p_align-1b" \
>>>>>>>>> +     "pass.out" \
>>>>>>>>> +     "-fpie" \
>>>>>>>>> +    ] \
>>>>>>>>> +    [list \
>>>>>>>>> +     "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
>>>>>>>>> +     "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
>>>>>>>>> +     "" \
>>>>>>>>> +     { p_align-1.c } \
>>>>>>>>> +     "p_align-1c" \
>>>>>>>>> +     "pass.out" \
>>>>>>>>> +     "$NOPIE_CFLAGS" \
>>>>>>>>> +    ] \
>>>>>>>>> +    [list \
>>>>>>>>> +     "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
>>>>>>>>> +     "-pie -Wl,-z,max-page-size=0x1000" \
>>>>>>>>> +     "" \
>>>>>>>>> +     { p_align-1.c } \
>>>>>>>>> +     "p_align-1d" \
>>>>>>>>> +     "pass.out" \
>>>>>>>>> +     "-fpie" \
>>>>>>>>> +    ] \
>>>>>>>>>  ]
>>>>>>>>
>>>>>>>> The two PIE variants of this also fail for me on glibc 2.26. Looks
>>>>>>>> like LOAD segments' alignment isn't being honored there, at least
>>>>>>>> not if it's as big as it is here.
>>>>>>>>
>>>>>>>
>>>>>>> The PIE alignment needs the kernel fix:
>>>>>>>
>>>>>>> commit ce81bb256a224259ab686742a6284930cbe4f1fa
>>>>>>> Author: Chris Kennelly <ckennelly@google.com>
>>>>>>> Date:   Thu Oct 15 20:12:32 2020 -0700
>>>>>>>
>>>>>>>     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address
>>>>>>
>>>>>> Well, then the test needs to be skipped if that fix is not in place.
>>>>>> After all you're testing binutils behavior here, not kernel or libc one.
>>>>>> I'm running a variety of (largely up-to-date) kernels on all of my
>>>>>> systems. But it looks like our kernel folks decided against backporting
>>>>>> this particular change. And I don't think you expect people to remember
>>>>>> to run the testsuite only on top of "certain" kernels?
>>>>>
>>>>> Care to submit a patch?
>>>>
>>>> I have no idea what to check for. I would really expect you to fix such
>>>> an issue (or really two of them, considering the other problem) recently
>>>> introduced by you.
>>>
>>> What compiler are you using on the broken kernel?
>>
>> gcc 7.4.1
>>
>> No idea how that matters, though.
> 
> Try this.

I can see that this might help with the other problem I did report, but I
don't see how gcc version and kernel in use would correlate. I guess I
could build binutils on that box with gcc 11, and then the kernel still
wouldn't do what you expect it to do. Much like I could continue using
gcc 7 and use a newer kernel, and then the test would be skipped for no
reason.

What I don't understand is why this needs to be a "run" test in the first
place. You're only after checking that the produced binary is correct,
aren't you?

Jan


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH] ld: Require GCC 8.0 for p_align-1.c tests
  2022-01-17 13:51                 ` Jan Beulich
@ 2022-01-17 13:54                   ` H.J. Lu
  0 siblings, 0 replies; 16+ messages in thread
From: H.J. Lu @ 2022-01-17 13:54 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Binutils

On Mon, Jan 17, 2022 at 5:51 AM Jan Beulich <jbeulich@suse.com> wrote:
>
> On 17.01.2022 14:44, H.J. Lu wrote:
> > On Mon, Jan 17, 2022 at 2:38 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>
> >> On 14.01.2022 15:14, H.J. Lu wrote:
> >>> On Fri, Jan 14, 2022 at 6:08 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>>>
> >>>> On 14.01.2022 15:02, H.J. Lu wrote:
> >>>>> On Fri, Jan 14, 2022 at 5:40 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>>>>>
> >>>>>> On 14.01.2022 14:03, H.J. Lu wrote:
> >>>>>>> On Fri, Jan 14, 2022 at 12:27 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>>>>>>>
> >>>>>>>> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
> >>>>>>>>> --- a/ld/testsuite/ld-elf/linux-x86.exp
> >>>>>>>>> +++ b/ld/testsuite/ld-elf/linux-x86.exp
> >>>>>>>>> @@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
> >>>>>>>>>       "" \
> >>>>>>>>>       "tmpdir/indirect-extern-access-2.so" \
> >>>>>>>>>      ] \
> >>>>>>>>> +    [list \
> >>>>>>>>> +     "Run p_align-1a without PIE" \
> >>>>>>>>> +     "$NOPIE_LDFLAGS" \
> >>>>>>>>> +     "" \
> >>>>>>>>> +     { p_align-1.c } \
> >>>>>>>>> +     "p_align-1a" \
> >>>>>>>>> +     "pass.out" \
> >>>>>>>>> +     "$NOPIE_CFLAGS" \
> >>>>>>>>> +    ] \
> >>>>>>>>> +    [list \
> >>>>>>>>> +     "Run p_align-1b with PIE" \
> >>>>>>>>> +     "-pie" \
> >>>>>>>>> +     "" \
> >>>>>>>>> +     { p_align-1.c } \
> >>>>>>>>> +     "p_align-1b" \
> >>>>>>>>> +     "pass.out" \
> >>>>>>>>> +     "-fpie" \
> >>>>>>>>> +    ] \
> >>>>>>>>> +    [list \
> >>>>>>>>> +     "Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
> >>>>>>>>> +     "$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
> >>>>>>>>> +     "" \
> >>>>>>>>> +     { p_align-1.c } \
> >>>>>>>>> +     "p_align-1c" \
> >>>>>>>>> +     "pass.out" \
> >>>>>>>>> +     "$NOPIE_CFLAGS" \
> >>>>>>>>> +    ] \
> >>>>>>>>> +    [list \
> >>>>>>>>> +     "Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
> >>>>>>>>> +     "-pie -Wl,-z,max-page-size=0x1000" \
> >>>>>>>>> +     "" \
> >>>>>>>>> +     { p_align-1.c } \
> >>>>>>>>> +     "p_align-1d" \
> >>>>>>>>> +     "pass.out" \
> >>>>>>>>> +     "-fpie" \
> >>>>>>>>> +    ] \
> >>>>>>>>>  ]
> >>>>>>>>
> >>>>>>>> The two PIE variants of this also fail for me on glibc 2.26. Looks
> >>>>>>>> like LOAD segments' alignment isn't being honored there, at least
> >>>>>>>> not if it's as big as it is here.
> >>>>>>>>
> >>>>>>>
> >>>>>>> The PIE alignment needs the kernel fix:
> >>>>>>>
> >>>>>>> commit ce81bb256a224259ab686742a6284930cbe4f1fa
> >>>>>>> Author: Chris Kennelly <ckennelly@google.com>
> >>>>>>> Date:   Thu Oct 15 20:12:32 2020 -0700
> >>>>>>>
> >>>>>>>     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address
> >>>>>>
> >>>>>> Well, then the test needs to be skipped if that fix is not in place.
> >>>>>> After all you're testing binutils behavior here, not kernel or libc one.
> >>>>>> I'm running a variety of (largely up-to-date) kernels on all of my
> >>>>>> systems. But it looks like our kernel folks decided against backporting
> >>>>>> this particular change. And I don't think you expect people to remember
> >>>>>> to run the testsuite only on top of "certain" kernels?
> >>>>>
> >>>>> Care to submit a patch?
> >>>>
> >>>> I have no idea what to check for. I would really expect you to fix such
> >>>> an issue (or really two of them, considering the other problem) recently
> >>>> introduced by you.
> >>>
> >>> What compiler are you using on the broken kernel?
> >>
> >> gcc 7.4.1
> >>
> >> No idea how that matters, though.
> >
> > Try this.
>
> I can see that this might help with the other problem I did report, but I
> don't see how gcc version and kernel in use would correlate. I guess I
> could build binutils on that box with gcc 11, and then the kernel still
> wouldn't do what you expect it to do. Much like I could continue using
> gcc 7 and use a newer kernel, and then the test would be skipped for no
> reason.
>

Then don't do it.

> What I don't understand is why this needs to be a "run" test in the first
> place. You're only after checking that the produced binary is correct,
> aren't you?

The run test is the most reliable way to test it.  It can catch regressions
in GCC and kernel.

-- 
H.J.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2022-01-14  8:12 ` Jan Beulich
@ 2022-02-10 12:44   ` Jan Beulich
  0 siblings, 0 replies; 16+ messages in thread
From: Jan Beulich @ 2022-02-10 12:44 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils, Nick Clifton

On 14.01.2022 09:12, Jan Beulich wrote:
> On 29.12.2021 20:39, H.J. Lu via Binutils wrote:
>> --- /dev/null
>> +++ b/ld/testsuite/ld-elf/p_align-1.c
>> @@ -0,0 +1,25 @@
>> +#include <stdio.h>
>> +#include <stdint.h>
>> +#include <stdlib.h>
>> +
>> +#ifndef ALIGN
>> +# define ALIGN 0x800000
>> +#endif
>> +
>> +int
>> +__attribute__ ((weak))
>> +is_aligned (void *p, int align)
>> +{
>> +  return (((uintptr_t) p) & (align - 1)) == 0;
>> +}
>> +
>> +int foo __attribute__ ((aligned (ALIGN))) = 1;
> 
> Alongside newer distros I also continue to build and test binutils on an
> oldish one. gcc 4.3 looks to silently ignore[1] alignment values larger
> than 1Mb on at least 32-bit x86. Hence all 4 derived tests fail there. I
> think you want to verify that foo's alignment is actually 8Mb in the
> object file, or use an assembler source instead of a C one (albeit I can
> see that this would undermine your PIE / non-PIE test variants).

I have to admit that I find it disappointing that this and the later
reported failure on glibc 2.26 (or, as you were suggesting, an unpatched
kernel) have not been taken care of in time for 2.38. I understand that
I did not come back with results of testing your proposed workaround, but
as said in an earlier reply I don't view adding a gcc version dependency
as a viable workaround, let alone a fix. Hence I didn't see any basis for
putting time into testing that change.

Jan


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2021-12-29  0:42 ` Alan Modra
@ 2021-12-29 19:41   ` H.J. Lu
  0 siblings, 0 replies; 16+ messages in thread
From: H.J. Lu @ 2021-12-29 19:41 UTC (permalink / raw)
  To: Alan Modra; +Cc: Binutils

On Tue, Dec 28, 2021 at 4:43 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Fri, Dec 24, 2021 at 07:54:18PM -0800, H.J. Lu via Binutils wrote:
> > Changes in v3:
> >
> > 1. To enable this linker optimization, the backend must define ELF_P_ALIGN
> > to ELF_MINPAGESIZE.
>
> All this means is that you're only breaking x86 binaries for those
> with older glibc who might be running with page size larger than 4k.

Correct.

> If this isn't so, what am I missing?
>
> Also, please check for fallout in future.
>
> alpha-linux  +FAIL: ld-elf/pr26936
> alpha-linux  +FAIL: ld-elf/seg
> alpha-linux  +FAIL: rgn-at5
> alpha-netbsd  +FAIL: ld-elf/pr26936
> alpha-unknown-freebsd4.7  +FAIL: ld-elf/pr26936
> arc-elf  +FAIL: ld-elf/pr26936
> arc-linux-uclibc  +FAIL: ld-elf/pr26936
> arc-linux-uclibc  +FAIL: ld-elf/seg
> arc-linux-uclibc  +FAIL: rgn-at5
> arm-nacl  +FAIL: ld-elf/pr26936
> avr-elf  +FAIL: ld-scripts/pr23571
> cr16-elf  +FAIL: ld-scripts/pr23571
> cris-linux  +FAIL: ld-elf/pr26936
> cris-linux  +FAIL: ld-elf/seg
> cris-linux  +FAIL: rgn-at5
> crisv32-linux  +FAIL: ld-elf/pr26936
> crisv32-linux  +FAIL: ld-elf/seg
> crisv32-linux  +FAIL: rgn-at5
> crx-elf  +FAIL: ld-scripts/pr23571
> frv-linux  +FAIL: ld-elf/pr26936
> frv-linux  +FAIL: ld-elf/seg
> frv-linux  +FAIL: rgn-at5
> h8300-elf  +FAIL: ld-scripts/pr23571
> h8300-linux  +FAIL: ld-scripts/pr23571
> ia64-elf  +FAIL: ld-elf/pr26936
> ia64-freebsd5  +FAIL: ld-elf/pr26936
> ia64-linux  +FAIL: ld-elf/pr26936
> ia64-linux  +FAIL: ld-elf/seg
> ia64-linux  +FAIL: rgn-at5
> ia64-netbsd  +FAIL: ld-elf/pr26936
> ip2k-elf  +FAIL: ld-scripts/pr23571
> lm32-linux  +FAIL: ld-elf/pr26936
> lm32-linux  +FAIL: ld-elf/seg
> lm32-linux  +FAIL: rgn-at5
> loongarch64-linux  +FAIL: ld-elf/seg
> loongarch64-linux  +FAIL: rgn-at5
> m32c-elf  +FAIL: ld-scripts/pr23571
> m32r-elf  +FAIL: ld-scripts/pr23571
> m68k-elf  +FAIL: ld-elf/pr26936
> m68k-linux  +FAIL: ld-elf/pr26936
> m68k-linux  +FAIL: ld-elf/seg
> m68k-linux  +FAIL: rgn-at5
> moxie-elf  +FAIL: ld-scripts/pr23571
> nds32le-linux  +FAIL: ld-elf/pr26936
> nds32le-linux  +FAIL: ld-elf/seg
> nds32le-linux  +FAIL: rgn-at5
> or1k-linux  +FAIL: ld-elf/pr26936
> or1k-linux  +FAIL: ld-elf/seg
> or1k-linux  +FAIL: rgn-at5
> pru-elf  +FAIL: pru_irq_map special section for host
> pru-elf  +FAIL: ld-scripts/pr23571
> score-elf  +FAIL: ld-elf/pr26936
> shle-unknown-netbsdelf  +FAIL: ld-elf/pr26936
> sh-nto  +FAIL: ld-scripts/pr23571
> sh-rtems  +FAIL: ld-scripts/pr23571
> sparc64-linux  +FAIL: ld-elf/pr26936
> sparc64-linux  +FAIL: ld-elf/seg
> sparc64-linux  +FAIL: rgn-at5
> sparc-elf  +FAIL: ld-elf/pr26936
> sparc-linux  +FAIL: ld-elf/pr26936
> sparc-linux  +FAIL: ld-elf/seg
> sparc-linux  +FAIL: rgn-at5
> sparc-sun-solaris2  +FAIL: ld-elf/pr26936
> spu-elf  +FAIL: ld-scripts/pr23571
> tilegx-linux  +FAIL: ld-elf/pr26936
> tilegx-linux  +FAIL: ld-elf/seg
> tilegx-linux  +FAIL: rgn-at5
> tilepro-linux  +FAIL: ld-elf/pr26936
> tilepro-linux  +FAIL: ld-elf/seg
> tilepro-linux  +FAIL: rgn-at5
> visium-elf  +FAIL: ld-scripts/pr23571
> xstormy16-elf  +FAIL: ld-scripts/pr23571

I adjusted these tests in the v3 patch:

https://sourceware.org/pipermail/binutils/2021-December/119047.html

-- 
H.J.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3] elf: Set p_align to the minimum page size if possible
  2021-12-25  3:54 [PATCH v3] elf: Set p_align to the minimum page size if possible H.J. Lu
@ 2021-12-29  0:42 ` Alan Modra
  2021-12-29 19:41   ` H.J. Lu
  0 siblings, 1 reply; 16+ messages in thread
From: Alan Modra @ 2021-12-29  0:42 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

On Fri, Dec 24, 2021 at 07:54:18PM -0800, H.J. Lu via Binutils wrote:
> Changes in v3:
> 
> 1. To enable this linker optimization, the backend must define ELF_P_ALIGN
> to ELF_MINPAGESIZE.

All this means is that you're only breaking x86 binaries for those
with older glibc who might be running with page size larger than 4k.
If this isn't so, what am I missing?

Also, please check for fallout in future.

alpha-linux  +FAIL: ld-elf/pr26936
alpha-linux  +FAIL: ld-elf/seg
alpha-linux  +FAIL: rgn-at5
alpha-netbsd  +FAIL: ld-elf/pr26936
alpha-unknown-freebsd4.7  +FAIL: ld-elf/pr26936
arc-elf  +FAIL: ld-elf/pr26936
arc-linux-uclibc  +FAIL: ld-elf/pr26936
arc-linux-uclibc  +FAIL: ld-elf/seg
arc-linux-uclibc  +FAIL: rgn-at5
arm-nacl  +FAIL: ld-elf/pr26936
avr-elf  +FAIL: ld-scripts/pr23571
cr16-elf  +FAIL: ld-scripts/pr23571
cris-linux  +FAIL: ld-elf/pr26936
cris-linux  +FAIL: ld-elf/seg
cris-linux  +FAIL: rgn-at5
crisv32-linux  +FAIL: ld-elf/pr26936
crisv32-linux  +FAIL: ld-elf/seg
crisv32-linux  +FAIL: rgn-at5
crx-elf  +FAIL: ld-scripts/pr23571
frv-linux  +FAIL: ld-elf/pr26936
frv-linux  +FAIL: ld-elf/seg
frv-linux  +FAIL: rgn-at5
h8300-elf  +FAIL: ld-scripts/pr23571
h8300-linux  +FAIL: ld-scripts/pr23571
ia64-elf  +FAIL: ld-elf/pr26936
ia64-freebsd5  +FAIL: ld-elf/pr26936
ia64-linux  +FAIL: ld-elf/pr26936
ia64-linux  +FAIL: ld-elf/seg
ia64-linux  +FAIL: rgn-at5
ia64-netbsd  +FAIL: ld-elf/pr26936
ip2k-elf  +FAIL: ld-scripts/pr23571
lm32-linux  +FAIL: ld-elf/pr26936
lm32-linux  +FAIL: ld-elf/seg
lm32-linux  +FAIL: rgn-at5
loongarch64-linux  +FAIL: ld-elf/seg
loongarch64-linux  +FAIL: rgn-at5
m32c-elf  +FAIL: ld-scripts/pr23571
m32r-elf  +FAIL: ld-scripts/pr23571
m68k-elf  +FAIL: ld-elf/pr26936
m68k-linux  +FAIL: ld-elf/pr26936
m68k-linux  +FAIL: ld-elf/seg
m68k-linux  +FAIL: rgn-at5
moxie-elf  +FAIL: ld-scripts/pr23571
nds32le-linux  +FAIL: ld-elf/pr26936
nds32le-linux  +FAIL: ld-elf/seg
nds32le-linux  +FAIL: rgn-at5
or1k-linux  +FAIL: ld-elf/pr26936
or1k-linux  +FAIL: ld-elf/seg
or1k-linux  +FAIL: rgn-at5
pru-elf  +FAIL: pru_irq_map special section for host
pru-elf  +FAIL: ld-scripts/pr23571
score-elf  +FAIL: ld-elf/pr26936
shle-unknown-netbsdelf  +FAIL: ld-elf/pr26936
sh-nto  +FAIL: ld-scripts/pr23571
sh-rtems  +FAIL: ld-scripts/pr23571
sparc64-linux  +FAIL: ld-elf/pr26936
sparc64-linux  +FAIL: ld-elf/seg
sparc64-linux  +FAIL: rgn-at5
sparc-elf  +FAIL: ld-elf/pr26936
sparc-linux  +FAIL: ld-elf/pr26936
sparc-linux  +FAIL: ld-elf/seg
sparc-linux  +FAIL: rgn-at5
sparc-sun-solaris2  +FAIL: ld-elf/pr26936
spu-elf  +FAIL: ld-scripts/pr23571
tilegx-linux  +FAIL: ld-elf/pr26936
tilegx-linux  +FAIL: ld-elf/seg
tilegx-linux  +FAIL: rgn-at5
tilepro-linux  +FAIL: ld-elf/pr26936
tilepro-linux  +FAIL: ld-elf/seg
tilepro-linux  +FAIL: rgn-at5
visium-elf  +FAIL: ld-scripts/pr23571
xstormy16-elf  +FAIL: ld-scripts/pr23571

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v3] elf: Set p_align to the minimum page size if possible
@ 2021-12-25  3:54 H.J. Lu
  2021-12-29  0:42 ` Alan Modra
  0 siblings, 1 reply; 16+ messages in thread
From: H.J. Lu @ 2021-12-25  3:54 UTC (permalink / raw)
  To: binutils

Changes in v3:

1. To enable this linker optimization, the backend must define ELF_P_ALIGN
to ELF_MINPAGESIZE.

Currently, on 32-bit and 64-bit ARM, it seems that ld generates p_align
values of 0x10000 even if no section alignment is greater than 0x1000.
The issue is more general and probably affects other targets with multiple
page sizes.

While file layout absolutely must take 64K page size into account, that
does not have to be reflected in the p_align value.  If running on a 64K
kernel, the file will be loaded at a 64K page boundary by necessity. On
a 4K kernel, 64K alignment is not needed.

The glibc loader has been fixed to honor p_align:

https://sourceware.org/bugzilla/show_bug.cgi?id=28676

similar to kernel:

commit ce81bb256a224259ab686742a6284930cbe4f1fa
Author: Chris Kennelly <ckennelly@google.com>
Date:   Thu Oct 15 20:12:32 2020 -0700

    fs/binfmt_elf: use PT_LOAD p_align values for suitable start address

This means that on 4K kernels, we will start to do extra work for 64K
p_align, but this pointless for pretty much all binaries (whose section
alignment rarely exceeds 16).

The minimum page size is used, instead of the maximum section alignment
due to this glibc bug:

https://sourceware.org/bugzilla/show_bug.cgi?id=28688

It has been fixed in glibc 2.35.  But linker output must work on existing
glibc binaries.

1. Set p_align to the minimum page size while laying out segments aligning
to the maximum page size or section alignment.  The run-time loader can
align segments to the minimum page size or above, depending on system page
size.
2. If -z max-page-size=NNN is used, p_align will be set to the maximum
page size or the largest section alignment.
3. If a section requires alignment higher than the minimum page size,
don't set p_align to the minimum page size.
4. If a section requires alignment higher than the maximum page size,
set p_align to the section alignment.
5. For objcopy, when the minimum page size != the maximum page size,
p_align may be set to the minimum page size while segments are aligned
to the maximum page size.  In this case, the input p_align will be
ignored and the maximum page size will be used to align the ouput
segments.
6. Update linker to disallow the common page size > the maximum page size.

To enable this linker optimization, the backend should define ELF_P_ALIGN
to ELF_MINPAGESIZE.

bfd/

	PR ld/28689
	PR ld/28695
	* elf-bfd.h (elf_backend_data): Add p_align.
	* elf.c (assign_file_positions_for_load_sections): Set p_align
	to the default p_align value while laying out segments aligning
	to maximum page size or section alignment.
	(elf_is_p_align_valid): New function.
	(copy_elf_program_header): Call elf_is_p_align_valid to determine
	if p_align is valid.
	* elfxx-target.h (ELF_P_ALIGN): New.  Default to 0.
	(elfNN_bed): Add ELF_P_ALIGN.
	* elfxx-x86.h (ELF_P_ALIGN): New.  Set to ELF_MINPAGESIZE.

include/

	PR ld/28689
	PR ld/28695
	* bfdlink.h (bfd_link_info): Add maxpagesize_is_set.

ld/

	PR ld/28689
	PR ld/28695
	* emultempl/elf.em (gld${EMULATION_NAME}_handle_option): Set
	link_info.maxpagesize_is_set for -z max-page-size=NNN.
	* ldelf.c (ldelf_after_parse): Disallow link_info.commonpagesize
	> link_info.maxpagesize.
	* testsuite/ld-elf/elf.exp: Pass -z max-page-size=0x4000 to
	linker to build mbind2a and mbind2b.
	* testsuite/ld-elf/header.d: Add -z common-page-size=0x100.
	* testsuite/ld-elf/linux-x86.exp: Add PR ld/28689 tests.
	* testsuite/ld-elf/p_align-1.c: New file.
	* testsuite/ld-elf/page-size-1.d: New test.
---
 bfd/elf-bfd.h                     |  4 ++
 bfd/elf.c                         | 71 ++++++++++++++++++++++++++++++-
 bfd/elfxx-target.h                |  5 +++
 bfd/elfxx-x86.h                   |  2 +
 include/bfdlink.h                 |  3 ++
 ld/emultempl/elf.em               |  1 +
 ld/ldelf.c                        |  3 ++
 ld/testsuite/ld-elf/elf.exp       |  4 +-
 ld/testsuite/ld-elf/header.d      |  2 +-
 ld/testsuite/ld-elf/linux-x86.exp | 36 ++++++++++++++++
 ld/testsuite/ld-elf/p_align-1.c   | 25 +++++++++++
 ld/testsuite/ld-elf/page-size-1.d |  4 ++
 12 files changed, 155 insertions(+), 5 deletions(-)
 create mode 100644 ld/testsuite/ld-elf/p_align-1.c
 create mode 100644 ld/testsuite/ld-elf/page-size-1.d

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 68e830c6f05..70331408a0b 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -945,6 +945,10 @@ struct elf_backend_data
   /* The value of commonpagesize to use when -z relro for this backend.  */
   bfd_vma relropagesize;
 
+  /* The p_align value for this backend.  If it is set, p_align of
+      PT_LOAD alignment will be to p_align by default.  */
+  bfd_vma p_align;
+
   /* The BFD flags applied to sections created for dynamic linking.  */
   flagword dynamic_sec_flags;
 
diff --git a/bfd/elf.c b/bfd/elf.c
index 92c06f2e44f..6c440a6dafc 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -5407,6 +5407,8 @@ assign_file_positions_for_load_sections (bfd *abfd,
   Elf_Internal_Phdr *p;
   file_ptr off;  /* Octets.  */
   bfd_size_type maxpagesize;
+  bfd_size_type p_align;
+  bool p_align_p = false;
   unsigned int alloc, actual;
   unsigned int i, j;
   struct elf_segment_map **sorted_seg_map;
@@ -5491,6 +5493,7 @@ assign_file_positions_for_load_sections (bfd *abfd,
     qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
 	   elf_sort_segments);
 
+  p_align = bed->p_align;
   maxpagesize = 1;
   if ((abfd->flags & D_PAGED) != 0)
     {
@@ -5561,6 +5564,15 @@ assign_file_positions_for_load_sections (bfd *abfd,
 	     segment.  */
 	  if (m->p_align_valid)
 	    maxpagesize = m->p_align;
+	  else if (p_align != 0
+		   && (link_info == NULL
+		       || !link_info->maxpagesize_is_set))
+	    /* Set p_align to the default p_align value while laying
+	       out segments aligning to the maximum page size or the
+	       largest section alignment.  The run-time loader can
+	       align segments to the default p_align value or the
+	       maximum page size, depending on system page size.  */
+	    p_align_p = true;
 
 	  p->p_align = maxpagesize;
 	}
@@ -5598,7 +5610,22 @@ assign_file_positions_for_load_sections (bfd *abfd,
 		}
 	      align = (bfd_size_type) 1 << align_power;
 	      if (align < maxpagesize)
-		align = maxpagesize;
+		{
+		  /* If a section requires alignment higher than the
+		     default p_align value, don't set p_align to the
+		     default p_align value.  */
+		  if (align > p_align)
+		    p_align_p = false;
+		  align = maxpagesize;
+		}
+	      else
+		{
+		  /* If a section requires alignment higher than the
+		     maximum page size, set p_align to the section
+		     alignment.  */
+		  p_align_p = true;
+		  p_align = align;
+		}
 	    }
 
 	  for (i = 0; i < m->count; i++)
@@ -5977,6 +6004,9 @@ assign_file_positions_for_load_sections (bfd *abfd,
 		  print_segment_map (m);
 		}
 	    }
+
+	  if (p_align_p)
+	    p->p_align = p_align;
 	}
     }
 
@@ -7485,6 +7515,40 @@ rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
   return true;
 }
 
+/* Return true if p_align in the ELF program header in ABFD is valid.  */
+
+static bool
+elf_is_p_align_valid (bfd *abfd)
+{
+  unsigned int i;
+  Elf_Internal_Phdr *segment;
+  unsigned int num_segments;
+  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
+  bfd_size_type maxpagesize = bed->maxpagesize;
+  bfd_size_type p_align = bed->p_align;
+
+  /* Return true if the default p_align value isn't set or the maximum
+     page size is the same as the minimum page size.  */
+  if (p_align == 0 || maxpagesize == bed->minpagesize)
+    return true;
+
+  /* When the default p_align value is set, p_align may be set to the
+     default p_align value while segments are aligned to the maximum
+     page size.  In this case, the input p_align will be ignored and
+     the maximum page size will be used to align the output segments.  */
+  segment = elf_tdata (abfd)->phdr;
+  num_segments = elf_elfheader (abfd)->e_phnum;
+  for (i = 0; i < num_segments; i++, segment++)
+    if (segment->p_type == PT_LOAD
+	&& (segment->p_align != p_align
+	    || vma_page_aligned_bias (segment->p_vaddr,
+				      segment->p_offset,
+				      maxpagesize) != 0))
+      return true;
+
+  return false;
+}
+
 /* Copy ELF program header information.  */
 
 static bool
@@ -7499,6 +7563,7 @@ copy_elf_program_header (bfd *ibfd, bfd *obfd)
   unsigned int num_segments;
   bool phdr_included = false;
   bool p_paddr_valid;
+  bool p_palign_valid;
   unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
 
   iehdr = elf_elfheader (ibfd);
@@ -7519,6 +7584,8 @@ copy_elf_program_header (bfd *ibfd, bfd *obfd)
 	break;
       }
 
+  p_palign_valid = elf_is_p_align_valid (ibfd);
+
   for (i = 0, segment = elf_tdata (ibfd)->phdr;
        i < num_segments;
        i++, segment++)
@@ -7561,7 +7628,7 @@ copy_elf_program_header (bfd *ibfd, bfd *obfd)
       map->p_paddr = segment->p_paddr;
       map->p_paddr_valid = p_paddr_valid;
       map->p_align = segment->p_align;
-      map->p_align_valid = 1;
+      map->p_align_valid = p_palign_valid;
       map->p_vaddr_offset = 0;
 
       if (map->p_type == PT_GNU_RELRO
diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h
index 4c6b1f20340..d0e2779a061 100644
--- a/bfd/elfxx-target.h
+++ b/bfd/elfxx-target.h
@@ -400,6 +400,10 @@
 # error ELF_MINPAGESIZE > ELF_RELROPAGESIZE
 #endif
 
+#ifndef ELF_P_ALIGN
+#define ELF_P_ALIGN 0
+#endif
+
 #ifndef ELF_DYNAMIC_SEC_FLAGS
 /* Note that we set the SEC_IN_MEMORY flag for these sections.  */
 #define ELF_DYNAMIC_SEC_FLAGS			\
@@ -813,6 +817,7 @@ static const struct elf_backend_data elfNN_bed =
   ELF_MINPAGESIZE,		/* minpagesize */
   ELF_COMMONPAGESIZE,		/* commonpagesize */
   ELF_RELROPAGESIZE,		/* commonpagesize to use with -z relro */
+  ELF_P_ALIGN,			/* p_align */
   ELF_DYNAMIC_SEC_FLAGS,	/* dynamic_sec_flags */
   elf_backend_arch_data,
   elf_info_to_howto,
diff --git a/bfd/elfxx-x86.h b/bfd/elfxx-x86.h
index aab641ab751..0c95f3533af 100644
--- a/bfd/elfxx-x86.h
+++ b/bfd/elfxx-x86.h
@@ -732,6 +732,8 @@ extern void _bfd_x86_elf_link_report_relative_reloc
 #define elf_backend_fixup_gnu_properties \
   _bfd_x86_elf_link_fixup_gnu_properties
 
+#define ELF_P_ALIGN ELF_MINPAGESIZE
+
 /* Return true if H is a __start_SECNAME/__stop_SECNAME symbol for the
    SECNAME section which has been garbage collected by --gc-sections
    -z start-stop-gc.  */
diff --git a/include/bfdlink.h b/include/bfdlink.h
index 566529ee644..8cf34b05c1a 100644
--- a/include/bfdlink.h
+++ b/include/bfdlink.h
@@ -525,6 +525,9 @@ struct bfd_link_info
   /* TRUE if all symbol names should be unique.  */
   unsigned int unique_symbol : 1;
 
+  /* TRUE if maxpagesize is set on command-line.  */
+  unsigned int maxpagesize_is_set : 1;
+
   /* Char that may appear as the first char of a symbol, but should be
      skipped (like symbol_leading_char) when looking up symbols in
      wrap_hash.  Used by PowerPC Linux for 'dot' symbols.  */
diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
index bfaf8130a3e..5eadab9f4b9 100644
--- a/ld/emultempl/elf.em
+++ b/ld/emultempl/elf.em
@@ -721,6 +721,7 @@ fragment <<EOF
 	      || (link_info.maxpagesize & (link_info.maxpagesize - 1)) != 0)
 	    einfo (_("%F%P: invalid maximum page size \`%s'\n"),
 		   optarg + 14);
+	  link_info.maxpagesize_is_set = true;
 	}
       else if (startswith (optarg, "common-page-size="))
 	{
diff --git a/ld/ldelf.c b/ld/ldelf.c
index 529992b02ae..ee09df5f35c 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -72,6 +72,9 @@ ldelf_after_parse (void)
       link_info.dynamic_undefined_weak = 0;
     }
   after_parse_default ();
+  if (link_info.commonpagesize > link_info.maxpagesize)
+    einfo (_("%F%P: common page size (0x%v) > maximum page size (0x%v)\n"),
+	   link_info.commonpagesize, link_info.maxpagesize);
 }
 
 /* Handle the generation of DT_NEEDED tags.  */
diff --git a/ld/testsuite/ld-elf/elf.exp b/ld/testsuite/ld-elf/elf.exp
index 01d22faad9a..ae8f76db1c7 100644
--- a/ld/testsuite/ld-elf/elf.exp
+++ b/ld/testsuite/ld-elf/elf.exp
@@ -365,7 +365,7 @@ if { [istarget *-*-linux*]
     run_ld_link_exec_tests [list \
 	[list \
 	    "Run mbind2a" \
-	    "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000" \
+	    "$NOPIE_LDFLAGS -Wl,-z,common-page-size=0x4000,-z,max-page-size=0x4000" \
 	    "" \
 	    { mbind2a.s mbind2b.c } \
 	    "mbind2a" \
@@ -374,7 +374,7 @@ if { [istarget *-*-linux*]
 	] \
 	[list \
 	    "Run mbind2b" \
-	    "-static -Wl,-z,common-page-size=0x4000" \
+	    "-static -Wl,-z,common-page-size=0x4000,-z,max-page-size=0x4000" \
 	    "" \
 	    { mbind2a.s mbind2b.c } \
 	    "mbind2b" \
diff --git a/ld/testsuite/ld-elf/header.d b/ld/testsuite/ld-elf/header.d
index c4d174a98da..67f0c981920 100644
--- a/ld/testsuite/ld-elf/header.d
+++ b/ld/testsuite/ld-elf/header.d
@@ -1,5 +1,5 @@
 # target: *-*-linux* *-*-gnu* *-*-vxworks arm*-*-uclinuxfdpiceabi
-# ld: -T header.t -z max-page-size=0x100
+# ld: -T header.t -z max-page-size=0x100 -z common-page-size=0x100
 # objdump: -hpw
 
 #...
diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp
index ee03b565faf..25a8d0411d9 100644
--- a/ld/testsuite/ld-elf/linux-x86.exp
+++ b/ld/testsuite/ld-elf/linux-x86.exp
@@ -185,6 +185,42 @@ run_ld_link_exec_tests [list \
 	"" \
 	"tmpdir/indirect-extern-access-2.so" \
     ] \
+    [list \
+	"Run p_align-1a without PIE" \
+	"$NOPIE_LDFLAGS" \
+	"" \
+	{ p_align-1.c } \
+	"p_align-1a" \
+	"pass.out" \
+	"$NOPIE_CFLAGS" \
+    ] \
+    [list \
+	"Run p_align-1b with PIE" \
+	"-pie" \
+	"" \
+	{ p_align-1.c } \
+	"p_align-1b" \
+	"pass.out" \
+	"-fpie" \
+    ] \
+    [list \
+	"Run p_align-1c with -Wl,-z,max-page-size=0x1000 without PIE" \
+	"$NOPIE_LDFLAGS -Wl,-z,max-page-size=0x1000" \
+	"" \
+	{ p_align-1.c } \
+	"p_align-1c" \
+	"pass.out" \
+	"$NOPIE_CFLAGS" \
+    ] \
+    [list \
+	"Run p_align-1d with -Wl,-z,max-page-size=0x1000 with PIE" \
+	"-pie -Wl,-z,max-page-size=0x1000" \
+	"" \
+	{ p_align-1.c } \
+	"p_align-1d" \
+	"pass.out" \
+	"-fpie" \
+    ] \
 ]
 
 proc elfedit_test { options test output } {
diff --git a/ld/testsuite/ld-elf/p_align-1.c b/ld/testsuite/ld-elf/p_align-1.c
new file mode 100644
index 00000000000..6579cd74e52
--- /dev/null
+++ b/ld/testsuite/ld-elf/p_align-1.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#ifndef ALIGN
+# define ALIGN 0x800000
+#endif
+
+int
+__attribute__ ((weak))
+is_aligned (void *p, int align)
+{
+  return (((uintptr_t) p) & (align - 1)) == 0;
+}
+
+int foo __attribute__ ((aligned (ALIGN))) = 1;
+
+int
+main (void)
+{
+  if (!is_aligned (&foo, ALIGN))
+    abort ();
+  printf ("PASS\n");
+  return 0;
+}
diff --git a/ld/testsuite/ld-elf/page-size-1.d b/ld/testsuite/ld-elf/page-size-1.d
new file mode 100644
index 00000000000..04d2153b2f9
--- /dev/null
+++ b/ld/testsuite/ld-elf/page-size-1.d
@@ -0,0 +1,4 @@
+#source: dummy.s
+#ld: -z common-page-size=0x4000 -z max-page-size=0x1000
+#error: common page size \(0x4000\) > maximum page size \(0x1000\)
+#target: *-*-linux-gnu *-*-gnu* arm*-*-uclinuxfdpiceabi
-- 
2.33.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2022-02-10 12:45 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-29 19:39 [PATCH v3] elf: Set p_align to the minimum page size if possible H.J. Lu
2022-01-14  8:12 ` Jan Beulich
2022-02-10 12:44   ` Jan Beulich
2022-01-14  8:27 ` Jan Beulich
2022-01-14 13:03   ` H.J. Lu
2022-01-14 13:40     ` Jan Beulich
2022-01-14 14:02       ` H.J. Lu
2022-01-14 14:07         ` Jan Beulich
2022-01-14 14:14           ` H.J. Lu
2022-01-17 10:38             ` Jan Beulich
2022-01-17 13:44               ` [PATCH] ld: Require GCC 8.0 for p_align-1.c tests H.J. Lu
2022-01-17 13:51                 ` Jan Beulich
2022-01-17 13:54                   ` H.J. Lu
  -- strict thread matches above, loose matches on Subject: below --
2021-12-25  3:54 [PATCH v3] elf: Set p_align to the minimum page size if possible H.J. Lu
2021-12-29  0:42 ` Alan Modra
2021-12-29 19:41   ` H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).