This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: [3/21] Keep .text, .data and .bss on AIX
Hi Nick,
Thanks for the reviews.
Nick Clifton <nickc@redhat.com> writes:
> Hi Richard,
>> + /* We must always have .text, .data and .bss output sections, so that
>> + the header can refer to them. The kernel refuses to load shared
>> + objects that have missing sections. */
>> + for (i = 0; i < ARRAY_SIZE (must_keep_sections); i++)
>> + {
>> + asection *sec;
>> +
>> + sec = bfd_get_section_by_name (link_info.output_bfd,
>> + must_keep_sections[i]);
>> + if (sec)
>> + sec->flags |= SEC_KEEP;
>> + }
>
> Should a warning be issued if one of these must-keep sections cannot be
> found ?
Good point.
> Presumably this would only matter if they are missing in the
> output bfd (and the bfd is an executabe or shared library) so maybe this
> is not the right place for such a test.
Yeah, the code should probably be restricted to non-relocatable links.
Keeping sections unconditionally should be conservatively correct,
but it's probably too conservative.
I checked that, unlike gas, the native assembler only emits
referenced sections. For example, the output for:
.extern f2
.globl foo
.csect foo[RW]
foo:
.long f2
only has a .data section. If you then do a relocatable link with the
native linker, the output also only has a .data section. If, on the
other hand, you do a shared library link, you get empty .text and .bss
sections too. So I agree a !link_info.relocatable guard is appropriate.
I think the check is in the right place though. The code is checking
whether the sections are in the output bfd, and is marking the output
section as SEC_KEEP.
Does this revised patch look OK? I checked that the error triggers for
non-relocatable links with a .text-less linker script and with .text-less
input bfds. (I created the latter using objcopy --remove-section,
to get around the gas behaviour mentioned above.)
Richard
ld/
* emultempl/aix.em (gld${EMULATION_NAME}_before_allocation): Mark
.text, .data and .bss output sections as SEC_KEEP.
ld/testsuite/
* ld-powerpc/aix-core-sec-1.s, ld-powerpc/aix-core-sec-1.ex,
ld-powerpc/aix-core-sec-1.hd, ld-powerpc/aix-core-sec-2.s,
ld-powerpc/aix-core-sec-2.ex, ld-powerpc/aix-core-sec-2.hd,
ld-powerpc/aix-core-sec-3.s, ld-powerpc/aix-core-sec-3.ex,
ld-powerpc/aix-core-sec-3.hd: New tests.
* ld-powerpc/aix52.exp: New harness.
Index: ld/emultempl/aix.em
===================================================================
--- ld/emultempl/aix.em 2009-03-12 17:06:11.000000000 +0000
+++ ld/emultempl/aix.em 2009-03-13 09:19:16.000000000 +0000
@@ -616,7 +616,12 @@ gld${EMULATION_NAME}_before_allocation (
struct export_symbol_list *el;
char *libpath;
asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
- int i;
+ static const char *const must_keep_sections[] = {
+ ".text",
+ ".data",
+ ".bss"
+ };
+ unsigned int i;
/* Handle the import and export files, if any. */
for (fl = import_files; fl != NULL; fl = fl->next)
@@ -824,6 +829,22 @@ gld${EMULATION_NAME}_before_allocation (
}
}
+ /* Executables and shared objects must always have .text, .data
+ and .bss output sections, so that the header can refer to them.
+ The kernel refuses to load objects that have missing sections. */
+ if (!link_info.relocatable)
+ for (i = 0; i < ARRAY_SIZE (must_keep_sections); i++)
+ {
+ asection *sec;
+
+ sec = bfd_get_section_by_name (link_info.output_bfd,
+ must_keep_sections[i]);
+ if (sec == NULL)
+ einfo ("%P: can't find output section %s\n", must_keep_sections[i]);
+ else
+ sec->flags |= SEC_KEEP;
+ }
+
before_allocation_default ();
}
Index: ld/testsuite/ld-powerpc/aix-core-sec-1.s
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix-core-sec-1.s 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,5 @@
+ .globl foo
+ .csect foo[RO]
+foo:
+ .long 0x12345678
+ .long 0xdeadbeef
Index: ld/testsuite/ld-powerpc/aix-core-sec-1.ex
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix-core-sec-1.ex 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,1 @@
+foo
Index: ld/testsuite/ld-powerpc/aix-core-sec-1.hd
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix-core-sec-1.hd 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,11 @@
+#...
+Sections:
+.*
+ * 0 * \.text * 0+8 .*
+ * CONTENTS, ALLOC, LOAD, CODE
+ * 1 * \.data * 0+0 .*
+ * ALLOC, LOAD, DATA
+ * 2 * \.bss * 0+0 .*
+ * ALLOC
+ * 3 * \.loader .*
+ * CONTENTS, ALLOC, LOAD
Index: ld/testsuite/ld-powerpc/aix-core-sec-2.s
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix-core-sec-2.s 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,5 @@
+ .globl foo
+ .csect foo[RW]
+foo:
+ .long 0x12345678
+ .long 0xdeadbeef
Index: ld/testsuite/ld-powerpc/aix-core-sec-2.ex
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix-core-sec-2.ex 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,1 @@
+foo
Index: ld/testsuite/ld-powerpc/aix-core-sec-2.hd
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix-core-sec-2.hd 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,11 @@
+#...
+Sections:
+.*
+ * 0 * \.text * 0+0 .*
+ * ALLOC, LOAD, CODE
+ * 1 * \.data * 0+8 .*
+ * CONTENTS, ALLOC, LOAD, DATA
+ * 2 * \.bss * 0+0 .*
+ * ALLOC
+ * 3 * \.loader .*
+ * CONTENTS, ALLOC, LOAD
Index: ld/testsuite/ld-powerpc/aix-core-sec-3.s
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix-core-sec-3.s 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,1 @@
+ .comm foo,8
Index: ld/testsuite/ld-powerpc/aix-core-sec-3.ex
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix-core-sec-3.ex 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,1 @@
+foo
Index: ld/testsuite/ld-powerpc/aix-core-sec-3.hd
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix-core-sec-3.hd 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,11 @@
+#...
+Sections:
+.*
+ * 0 * \.text * 0+0 .*
+ * ALLOC, LOAD, CODE
+ * 1 * \.data * 0+0 .*
+ * ALLOC, LOAD, DATA
+ * 2 * \.bss * 0+8 .*
+ * ALLOC
+ * 3 * \.loader .*
+ * CONTENTS, ALLOC, LOAD
Index: ld/testsuite/ld-powerpc/aix52.exp
===================================================================
--- /dev/null 2009-02-06 09:11:03.343159000 +0000
+++ ld/testsuite/ld-powerpc/aix52.exp 2009-03-12 17:06:18.000000000 +0000
@@ -0,0 +1,89 @@
+# Expect script for AIX 5.2+ tests
+# Copyright 2009 Free Software Foundation
+#
+# This file is part of the GNU Binutils.
+#
+# This program 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+#
+
+if { ![istarget "powerpc*-*-aix\[5-9\]*"]
+ || [istarget "powerpc*-*-aix5.\[01\]*"] } {
+ return
+}
+
+# Run a run_link_tests-style test for AIX. SIZE selects the target size
+# (32 or 64). The other arguments are elements of a run_link_tests test.
+#
+# Make the following changes before running the test:
+#
+# - Mention SIZE in the test name.
+# - Add "-aSIZE --defsym size=SIZE" to the assembler options.
+# - Add the source directory to any "-bI:" and "-bE:" linker options.
+# - Add "-bSIZE" to the linker options.
+# - Add "-XSIZE" to the archiver options.
+# - Replace "SIZE" with SIZE in TOOLS.
+# - When testing 64-bit targets:
+# - Turn tmpdir/aix-* into tmpdir/aix64-*.
+# - Turn tmpdir/libaix-* into tmpdir/libaix64-*.
+# - Turn -laix* into -laix64*, to compensate for the above.
+proc run_aix_test { size name ldopts asopts sources tools output } {
+ global srcdir subdir
+
+ if { $size == 64 } {
+ regsub -all {tmpdir/aix-} $ldopts {tmpdir/aix64-} ldopts
+ regsub {^aix} $output {aix64} output
+
+ regsub -all -- {-laix-} $ldopts {-laix64-} ldopts
+ regsub {^libaix} $output {libaix64} output
+ }
+ if { [regexp {.a$} $output] } {
+ append ldopts " -X$size"
+ } else {
+ regsub -all {(-b[IE]):} $ldopts "\\1:$srcdir/$subdir/" ldopts
+ append ldopts " -b$size"
+ }
+ regsub -all {SIZE} $tools $size tools
+ run_ld_link_tests [list [list "$name ($size-bit)" \
+ $ldopts \
+ "$asopts -a$size --defsym size=$size" \
+ $sources \
+ $tools \
+ $output]]
+}
+
+set aix52tests {
+ {"Core sections test 1" "-shared -bE:aix-core-sec-1.ex"
+ "" {aix-core-sec-1.s}
+ {{objdump -h aix-core-sec-1.hd}}
+ "aix-core-sec-1.so"}
+
+ {"Core sections test 2" "-shared -bE:aix-core-sec-2.ex"
+ "" {aix-core-sec-2.s}
+ {{objdump -h aix-core-sec-2.hd}}
+ "aix-core-sec-2.so"}
+
+ {"Core sections test 3" "-shared -bE:aix-core-sec-3.ex"
+ "" {aix-core-sec-3.s}
+ {{objdump -h aix-core-sec-3.hd}}
+ "aix-core-sec-3.so"}
+}
+
+foreach test $aix52tests {
+ foreach { name ldopts asopts sources tools output } $test {
+ run_aix_test 32 $name $ldopts $asopts $sources $tools $output
+ run_aix_test 64 $name $ldopts $asopts $sources $tools $output
+ }
+}