Do not include empty sections in loadable segments.
authorNick Clifton <nickc@redhat.com>
Thu, 6 Jun 2002 10:03:38 +0000 (10:03 +0000)
committerNick Clifton <nickc@redhat.com>
Thu, 6 Jun 2002 10:03:38 +0000 (10:03 +0000)
bfd/ChangeLog
bfd/elf.c
ld/testsuite/ChangeLog
ld/testsuite/ld-scripts/phdrs2.exp [new file with mode: 0644]
ld/testsuite/ld-scripts/phdrs2.s [new file with mode: 0644]
ld/testsuite/ld-scripts/phdrs2.t [new file with mode: 0644]

index 51f9a90ffb698a5fb4a6b3a214c342805f944e59..107403d07f66e14a870e5d2c9a98de7220e3d5a7 100644 (file)
@@ -1,3 +1,9 @@
+2002-06-06  David Heine <dlheine@tensilica.com>
+
+       * elf.c (assign_file_positions_for_segments): Remove unallocated
+       sections from the section to segment mapping for PT_LOAD segments.
+       Update comment about empty loadable segments.
+
 2002-06-06  Richard Sandiford  <rsandifo@redhat.com>
 
        * stabs.c (_bfd_link_section_stabs): Check that the symbol offset
@@ -44,7 +50,7 @@
        * elf64-sh64.c (sh_elf64_get_relocated_section_contents): Likewise.
        * elfxx-ia64.c (elfNN_ia64_relax_section): Likewise.
 
-Wed Jun  5 20:43:27 2002  J"orn Rennecke <joern.rennecke@superh.com>
+2002-06-05  J"orn Rennecke <joern.rennecke@superh.com>
 
        * config.bfd (sh64l*-*-elf*, shl*-*-elf*): New configurations.
 
index 36cbb7697b1a8b721c554e41268a7f8c76e2e933..fd9aa4b9263655505b43980ad07d933a71d80e1b 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -3455,6 +3455,37 @@ assign_file_positions_for_segments (abfd)
       if (! map_sections_to_segments (abfd))
        return false;
     }
+  else
+    {
+      /* The placement algorithm assumes that non allocated sections are
+        not in PT_LOAD segments.  We ensure this here by removing such
+        sections from the segment map.  */
+      for (m = elf_tdata (abfd)->segment_map;
+          m != NULL;
+          m = m->next)
+       {
+         unsigned int new_count;
+         unsigned int i;
+
+         if (m->p_type != PT_LOAD)
+           continue;
+
+         new_count = 0;
+         for (i = 0; i < m->count; i ++)
+           {
+             if ((m->sections[i]->flags & SEC_ALLOC) != 0)
+               {
+                 if (i != new_count) 
+                   m->sections[new_count] = m->sections[i];
+
+                 new_count ++;
+               }
+           }
+
+         if (new_count != m->count)
+           m->count = new_count;
+       }
+    }
 
   if (bed->elf_backend_modify_segment_map)
     {
@@ -4610,10 +4641,11 @@ copy_private_bfd_data (ibfd, obfd)
        {
          /* Special segments, such as the PT_PHDR segment, may contain
             no sections, but ordinary, loadable segments should contain
-            something.  */
+            something.  They are allowed by the ELF spec however, so only
+            a warning is produced.  */
          if (segment->p_type == PT_LOAD)
            (*_bfd_error_handler)
-             (_("%s: warning: Empty loadable segment detected\n"),
+             (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
               bfd_archive_filename (ibfd));
 
          map->count = 0;
index 1328cdefb1b42f3a8c0453b6604772af5bf37716..551aaf96e607c3763aeae4b10f2001e094afd057 100644 (file)
@@ -1,3 +1,10 @@
+2002-06-06  David Heine <dlheine@tensilica.com>
+
+       * ld-scripts/phdrs2.exp: New file: Run second phdrs test.
+       * ld-scripts/phdrs2.s: New file: Dummy assembler source.
+       * ld-scripts/phdrs2.t: New file: Linker script with an empty
+       section at the start of a loadable segment.
+
 2005-06-02  H.J. Lu <hjl@gnu.org>
 
        * ld-srec/sr3.cc (__dso_handle): Added for gcc 3.1 with
diff --git a/ld/testsuite/ld-scripts/phdrs2.exp b/ld/testsuite/ld-scripts/phdrs2.exp
new file mode 100644 (file)
index 0000000..bbe36d0
--- /dev/null
@@ -0,0 +1,64 @@
+# Test PHDRS with empty sections in a linker script.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+# PHDRS2 is only meaningful for ELF.
+if {    ![istarget *-*-sysv4*] \
+     && ![istarget *-*-unixware*] \
+     && ![istarget *-*-elf*] \
+     && ![istarget *-*-eabi*] \
+     && ![istarget *-*-linux*] \
+     && ![istarget *-*-irix5*] \
+     && ![istarget *-*-irix6*] \
+     && ![istarget *-*-solaris2*] } {
+    return
+}
+
+if { [istarget *-*-linux*aout*] \
+     || [istarget *-*-linux*oldld*] } {
+    return
+}
+
+# This is a very simplistic test.
+
+set testname "PHDRS2"
+
+if ![ld_assemble $as $srcdir/$subdir/phdrs2.s tmpdir/phdrs2.o] {
+    unresolved $testname
+    return
+}
+
+set phdrs_regexp \
+    ".*Program Header:.*LOAD *off *0x00\[0-9a-f\]* *vaddr *0x00*800000 *paddr *0x00*800000.*filesz *0x0\[0-9a-f\]* *memsz *0x0\[0-9a-f\]*.*LOAD *off *0x00\[0-9a-f\]* *vaddr *0x00*800004 *paddr *0x00*800004.*filesz *0x00*\[0-9a-f\]* *memsz *0x0\[0-9a-f\]* *flags rw.*"
+
+if ![ld_simple_link $ld tmpdir/phdrs2 "-T $srcdir/$subdir/phdrs2.t tmpdir/phdrs2.o"] {
+    fail $testname
+} else {
+    if {[which $objdump] == 0} {
+       unresolved $testname
+       return
+    }
+
+    verbose -log "$objdump --private tmpdir/phdrs2"
+    catch "exec $objdump --private tmpdir/phdrs2" exec_output
+    set exec_output [prune_warnings $exec_output]
+    verbose -log $exec_output
+
+    if [regexp $phdrs_regexp $exec_output] {
+       pass $testname
+    } else {
+       fail $testname
+    }
+}
diff --git a/ld/testsuite/ld-scripts/phdrs2.s b/ld/testsuite/ld-scripts/phdrs2.s
new file mode 100644 (file)
index 0000000..8092c18
--- /dev/null
@@ -0,0 +1,7 @@
+       .text
+       .align 4
+       .long   1
+
+       .data
+       .align 4
+       .long   2
diff --git a/ld/testsuite/ld-scripts/phdrs2.t b/ld/testsuite/ld-scripts/phdrs2.t
new file mode 100644 (file)
index 0000000..0d32b39
--- /dev/null
@@ -0,0 +1,23 @@
+PHDRS
+{
+  text PT_LOAD ;
+  data PT_LOAD ;
+}
+
+SECTIONS
+{
+  . = 0x800000 - 1;
+  /* The PHDRS generated should start at the aligned .text section
+     address, not the unaligned .empty section address */
+  .empty : { 
+       EMPTY_START = ABSOLUTE(.) ;
+       *(.empty) 
+       EMPTY_END = ABSOLUTE(.) ;
+       } : text
+  .text : { *(.text) } :text
+  .data : { *(.data)
+       LONG(EMPTY_START) ;
+        } :data
+       
+  /DISCARD/ : { *(.*) }
+}