x86: Delay setting the iplt section alignment
authorH.J. Lu <hjl.tools@gmail.com>
Sat, 1 Dec 2018 13:42:33 +0000 (05:42 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Sat, 1 Dec 2018 14:35:03 +0000 (06:35 -0800)
Delay setting its alignment until we know it is non-empty.  Otherwise an
empty iplt section may change vma and lma of the following sections, which
triggers moving dot of the following section backwards, resulting in a
warning and section lma not being set properly.  It later leads to a
"File truncated" error.

bfd/

PR ld/23930
* elfxx-x86.c (_bfd_x86_elf_size_dynamic_sections): Update
the iplt section alignment if it is non-empty.
(_bfd_x86_elf_link_setup_gnu_properties): Set plt.iplt_alignment
and delay setting the iplt section alignment.
* elfxx-x86.h (elf_x86_plt_layout): Add iplt_alignment.

ld/

PR ld/23930
* testsuite/ld-i386/i386.exp: Run pr23930.
* testsuite/ld-i386/pr23930.d: New file.
* testsuite/ld-x86-64/pr23930-32.t: Likewise.
* testsuite/ld-x86-64/pr23930-x32.d: Likewise.
* testsuite/ld-x86-64/pr23930.d: Likewise.
* testsuite/ld-x86-64/pr23930.t: Likewise.
* testsuite/ld-x86-64/pr23930a.s: Likewise.
* testsuite/ld-x86-64/pr23930b.s: Likewise.
* testsuite/ld-x86-64/x86-64.exp: Run pr23930 and pr23930-x32.

13 files changed:
bfd/ChangeLog
bfd/elfxx-x86.c
bfd/elfxx-x86.h
ld/ChangeLog
ld/testsuite/ld-i386/i386.exp
ld/testsuite/ld-i386/pr23930.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr23930-32.t [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr23930-x32.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr23930.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr23930.t [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr23930a.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr23930b.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index e0eb1559882455717c619c6c22746a0372ca9f0c..3210b0c380aa28692babad962370318063e65415 100644 (file)
@@ -1,3 +1,12 @@
+2018-12-01  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/23930
+       * elfxx-x86.c (_bfd_x86_elf_size_dynamic_sections): Update
+       the iplt section alignment if it is non-empty.
+       (_bfd_x86_elf_link_setup_gnu_properties): Set plt.iplt_alignment
+       and delay setting the iplt section alignment.
+       * elfxx-x86.h (elf_x86_plt_layout): Add iplt_alignment.
+
 2018-11-30  Nick Clifton  <nickc@redhat.com>
 
        PR 23942
index 05f5c6a2f95d11da9bc106d2db750b541f7d700f..40aac664f4a060125ecb6b4f540cf64d00ae3cbc 100644 (file)
@@ -1275,6 +1275,14 @@ _bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
       if ((s->flags & SEC_HAS_CONTENTS) == 0)
        continue;
 
+      /* NB: Initially, the iplt section has minimal alignment to
+        avoid moving dot of the following section backwards when
+        it is empty.  Update its section alignment now since it
+        is non-empty.  */
+      if (s == htab->elf.iplt)
+       bfd_set_section_alignment (s->owner, s,
+                                  htab->plt.iplt_alignment);
+
       /* Allocate memory for the section contents.  We use bfd_zalloc
         here in case unused entries are not reclaimed before the
         section's contents are written out.  This should not happen,
@@ -2907,15 +2915,23 @@ error_alignment:
        }
     }
 
-  if (normal_target)
+  /* The .iplt section is used for IFUNC symbols in static
+     executables.  */
+  sec = htab->elf.iplt;
+  if (sec != NULL)
     {
-      /* The .iplt section is used for IFUNC symbols in static
-        executables.  */
-      sec = htab->elf.iplt;
-      if (sec != NULL
-         && !bfd_set_section_alignment (sec->owner, sec,
-                                        plt_alignment))
+      /* NB: Delay setting its alignment until we know it is non-empty.
+        Otherwise an empty iplt section may change vma and lma of the
+        following sections, which triggers moving dot of the following
+        section backwards, resulting in a warning and section lma not
+        being set properly.  It later leads to a "File truncated"
+        error.  */
+      if (!bfd_set_section_alignment (sec->owner, sec, 0))
        goto error_alignment;
+
+      htab->plt.iplt_alignment = (normal_target
+                                 ? plt_alignment
+                                 : bed->plt_alignment);
     }
 
   return pbfd;
index 964843822e32187720cfc630ef95f564a13c46b1..d153623d3e8c2b445ceee5f2691433877d0d7e37 100644 (file)
@@ -389,6 +389,9 @@ struct elf_x86_plt_layout
      This is only used for x86-64.  */
   unsigned int plt_got_insn_size;
 
+  /* Alignment of the .iplt section.  */
+  unsigned int iplt_alignment;
+
   /* .eh_frame covering the .plt section.  */
   const bfd_byte *eh_frame_plt;
   unsigned int eh_frame_plt_size;
index e203edcf2dff2db6906f5c2f5de1beb77a724e6f..80fd06c1cae8a3eff5d4375c3c0500712d4d60c4 100644 (file)
@@ -1,3 +1,16 @@
+2018-12-01  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/23930
+       * testsuite/ld-i386/i386.exp: Run pr23930.
+       * testsuite/ld-i386/pr23930.d: New file.
+       * testsuite/ld-x86-64/pr23930-32.t: Likewise.
+       * testsuite/ld-x86-64/pr23930-x32.d: Likewise.
+       * testsuite/ld-x86-64/pr23930.d: Likewise.
+       * testsuite/ld-x86-64/pr23930.t: Likewise.
+       * testsuite/ld-x86-64/pr23930a.s: Likewise.
+       * testsuite/ld-x86-64/pr23930b.s: Likewise.
+       * testsuite/ld-x86-64/x86-64.exp: Run pr23930 and pr23930-x32.
+---
 2018-11-30  Alan Modra  <amodra@gmail.com>
 
        * testsuite/ld-powerpc/pr23937.d,
index 76577c4fcb1f9c440610b7185918b58257584f93..f86a54d27ae6ffa2d42ffbb56f02964516ebe627 100644 (file)
@@ -482,6 +482,7 @@ run_dump_test "pr23486b"
 run_dump_test "pr23486c"
 run_dump_test "pr23486d"
 run_dump_test "pr23854"
+run_dump_test "pr23930"
 
 if { !([istarget "i?86-*-linux*"]
        || [istarget "i?86-*-gnu*"]
diff --git a/ld/testsuite/ld-i386/pr23930.d b/ld/testsuite/ld-i386/pr23930.d
new file mode 100644 (file)
index 0000000..e9da510
--- /dev/null
@@ -0,0 +1,11 @@
+#source: ../ld-x86-64/pr23930a.s
+#source: ../ld-x86-64/pr23930b.s
+#as: --32
+#ld: -m elf_i386 -z separate-code -z norelro -T ../ld-x86-64/pr23930-32.t
+#objdump: --disassemble=main
+
+#...
+[a-f0-9]+ <main>:
+[a-f0-9]+:     31 c0                   xor    %eax,%eax
+[a-f0-9]+:     c3                      ret    
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr23930-32.t b/ld/testsuite/ld-x86-64/pr23930-32.t
new file mode 100644 (file)
index 0000000..29b5309
--- /dev/null
@@ -0,0 +1,10 @@
+PHDRS {
+ text PT_LOAD;
+}
+
+SECTIONS
+{
+ . = (0x8000000f + ALIGN(0x1000000, 0x1000000));
+ .text : AT(ADDR(.text) - 0x8000000f) {
+ } :text
+}
diff --git a/ld/testsuite/ld-x86-64/pr23930-x32.d b/ld/testsuite/ld-x86-64/pr23930-x32.d
new file mode 100644 (file)
index 0000000..b01d2b9
--- /dev/null
@@ -0,0 +1,11 @@
+#source: pr23930a.s
+#source: pr23930b.s
+#as: --x32
+#ld: -m elf32_x86_64 -z separate-code -z norelro -T pr23930-32.t
+#objdump: --disassemble=main
+
+#...
+[a-f0-9]+ <main>:
+[a-f0-9]+:     31 c0                   xor    %eax,%eax
+[a-f0-9]+:     c3                      retq   
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr23930.d b/ld/testsuite/ld-x86-64/pr23930.d
new file mode 100644 (file)
index 0000000..c849b68
--- /dev/null
@@ -0,0 +1,11 @@
+#source: pr23930a.s
+#source: pr23930b.s
+#as: --64
+#ld: -m elf_x86_64 -z separate-code -z norelro -T pr23930.t
+#objdump: --disassemble=main
+
+#...
+[a-f0-9]+ <main>:
+[a-f0-9]+:     31 c0                   xor    %eax,%eax
+[a-f0-9]+:     c3                      retq   
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr23930.t b/ld/testsuite/ld-x86-64/pr23930.t
new file mode 100644 (file)
index 0000000..2255ed3
--- /dev/null
@@ -0,0 +1,10 @@
+PHDRS {
+ text PT_LOAD;
+}
+
+SECTIONS
+{
+ . = (0xffffffff8000000f + ALIGN(0x1000000, 0x1000000));
+ .text : AT(ADDR(.text) - 0xffffffff8000000f) {
+ } :text
+}
diff --git a/ld/testsuite/ld-x86-64/pr23930a.s b/ld/testsuite/ld-x86-64/pr23930a.s
new file mode 100644 (file)
index 0000000..e1679a9
--- /dev/null
@@ -0,0 +1,7 @@
+       .text
+       .globl  other
+       .type   other, @function
+other:
+       xorl    %eax, %eax
+       ret
+       .size   other, .-other
diff --git a/ld/testsuite/ld-x86-64/pr23930b.s b/ld/testsuite/ld-x86-64/pr23930b.s
new file mode 100644 (file)
index 0000000..e4859c7
--- /dev/null
@@ -0,0 +1,14 @@
+       .text
+       .globl  orig
+       .type   orig, @function
+orig:
+       xorl    %eax, %eax
+       ret
+       .size   orig, .-orig
+       .section        .text.startup,"ax",@progbits
+       .globl  main
+       .type   main, @function
+main:
+       xorl    %eax, %eax
+       ret
+       .size   main, .-main
index 8e5348dfeab088e723cd3e76618ed9a53f99b120..b51ad7cebcbc72d7efbf490b3389744516d8244f 100644 (file)
@@ -422,6 +422,8 @@ run_dump_test "pr23486c-x32"
 run_dump_test "pr23486d"
 run_dump_test "pr23486d-x32"
 run_dump_test "pr23854"
+run_dump_test "pr23930"
+run_dump_test "pr23930-x32"
 
 if { ![istarget "x86_64-*-linux*"] && ![istarget "x86_64-*-nacl*"]} {
     return