From fa1477dc34e6ce19b90ff0171074c295133730a3 Mon Sep 17 00:00:00 2001 From: Stephen Casner Date: Tue, 14 Apr 2020 14:41:27 +0100 Subject: [PATCH] Fixes for the magic number used in PDP11 AOUT binaries. PR ld/25677 include * aout/aout64.h (N_DATADDR): Add IMAGIC case. bfd * pdp11.c: Add implementation of --imagic option. (adjust_o_magic): Fix objcopy --extract-symbol test. * libaout.h (enum aout_magic): Add i_magic. ld * emulparams/pdp11.sh (SCRIPT_NAME): Change to pdp11. (EXTRA_EM_FILE): New, add emulation file pdp11. * scripttempl/pdp11.sc: New, derived from aout.sc without irrelevant input sections. * emultempl/pdp11.em (_add_options, _handle_option) (_list_options): New. Add options -z, --imagic for pdp11-aout. (_before_parse): Make --omagic be default instead of --nmagic. (_get_script): Modify special-case linker script for --imagic. * lexsup.c (parse_args): Explictly set config.text_read_only for -n. * ld.texi (Options): Add documentation of PDP11-specific options. (Options): Fix unrelated typo to --no-compact-branches. * gen-doc.texi: @set PDP11. * testsuite/ld-pdp11/pdp11.exp: New, start pdp11 testing. * testsuite/ld-pdp11/sections.s: New, source for options tests. * testsuite/ld-pdp11/imagic.d: New, test --imagic format. * testsuite/ld-pdp11/imagicz.d: New, test -z (imagic) format. * testsuite/ld-pdp11/nmagic.d: New, test --nmagic format. * testsuite/ld-pdp11/omagic.d: New, test --omagic format. --- bfd/ChangeLog | 7 ++ bfd/libaout.h | 3 +- bfd/pdp11.c | 62 ++++++++++++++- include/ChangeLog | 5 ++ include/aout/aout64.h | 5 +- ld/ChangeLog | 22 ++++++ ld/NEWS | 4 + ld/emulparams/pdp11.sh | 3 +- ld/emultempl/pdp11.em | 132 +++++++++++++++++++++++++++++++ ld/gen-doc.texi | 1 + ld/ld.texi | 70 +++++++++++++++- ld/lexsup.c | 1 + ld/scripttempl/pdp11.sc | 56 +++++++++++++ ld/testsuite/ld-pdp11/imagic.d | 12 +++ ld/testsuite/ld-pdp11/imagicz.d | 12 +++ ld/testsuite/ld-pdp11/nmagic.d | 11 +++ ld/testsuite/ld-pdp11/omagic.d | 12 +++ ld/testsuite/ld-pdp11/pdp11.exp | 33 ++++++++ ld/testsuite/ld-pdp11/sections.s | 13 +++ 19 files changed, 457 insertions(+), 7 deletions(-) create mode 100644 ld/emultempl/pdp11.em create mode 100644 ld/scripttempl/pdp11.sc create mode 100644 ld/testsuite/ld-pdp11/imagic.d create mode 100644 ld/testsuite/ld-pdp11/imagicz.d create mode 100644 ld/testsuite/ld-pdp11/nmagic.d create mode 100644 ld/testsuite/ld-pdp11/omagic.d create mode 100644 ld/testsuite/ld-pdp11/pdp11.exp create mode 100644 ld/testsuite/ld-pdp11/sections.s diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 120fb38646..0ef3b2faef 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,10 @@ +2020-04-14 Stephen Casner + + PR ld/25677 + * pdp11.c: Add implementation of --imagic option. + (adjust_o_magic): Fix objcopy --extract-symbol test. + * libaout.h (enum aout_magic): Add i_magic. + 2020-04-07 Rainer Orth Nick Clifton diff --git a/bfd/libaout.h b/bfd/libaout.h index bdf917e556..61746db243 100644 --- a/bfd/libaout.h +++ b/bfd/libaout.h @@ -359,7 +359,8 @@ enum aout_magic { undecided_magic = 0, z_magic, o_magic, - n_magic + n_magic, + i_magic }; struct aoutdata diff --git a/bfd/pdp11.c b/bfd/pdp11.c index c13e742c0d..1f8c4061e9 100644 --- a/bfd/pdp11.c +++ b/bfd/pdp11.c @@ -63,6 +63,7 @@ #define N_SET_FLAGS(execp, flags) do { } while (0) #define N_BADMAG(x) (N_MAGIC(x) != OMAGIC \ && N_MAGIC(x) != NMAGIC \ + && N_MAGIC(x) != IMAGIC \ && N_MAGIC(x) != ZMAGIC) #include "sysdep.h" @@ -90,7 +91,8 @@ struct pdp11_external_exec #define A_MAGIC2 NMAGIC #define NMAGIC 0410 /* Pure executable. */ #define ZMAGIC 0413 /* Demand-paged executable. */ -#define A_MAGIC3 0411 /* Separated I&D. */ +#define IMAGIC 0411 /* Separated I&D. */ +#define A_MAGIC3 IMAGIC #define A_MAGIC4 0405 /* Overlay. */ #define A_MAGIC5 0430 /* Auto-overlay (nonseparate). */ #define A_MAGIC6 0431 /* Auto-overlay (separate). */ @@ -242,6 +244,10 @@ struct aout_final_link_info struct external_nlist *output_syms; }; +/* Copy of the link_info.separate_code boolean to select the output format with + separate instruction and data spaces selected by --imagic */ +static bfd_boolean separate_i_d = FALSE; + reloc_howto_type howto_table_pdp11[] = { /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */ @@ -498,6 +504,8 @@ NAME (aout, some_aout_object_p) (bfd *abfd, } else if (N_MAGIC (execp) == OMAGIC) adata (abfd).magic = o_magic; + else if (N_MAGIC (execp) == IMAGIC) + adata (abfd).magic = i_magic; else { /* Should have been checked with N_BADMAG before this routine @@ -825,7 +833,7 @@ adjust_o_magic (bfd *abfd, struct internal_exec *execp) vma += pad; bss->vma = vma; } - else + else if (data->size > 0 || bss->size > 0) /* PR25677: for objcopy --extract-symbol */ { /* The VMA of the .bss section is set by the VMA of the .data section plus the size of the .data section. We may @@ -988,6 +996,47 @@ adjust_n_magic (bfd *abfd, struct internal_exec *execp) N_SET_MAGIC (execp, NMAGIC); } +static void +adjust_i_magic (bfd *abfd, struct internal_exec *execp) +{ + file_ptr pos = adata (abfd).exec_bytes_size; + bfd_vma vma = 0; + int pad; + asection *text = obj_textsec (abfd); + asection *data = obj_datasec (abfd); + asection *bss = obj_bsssec (abfd); + + /* Text. */ + text->filepos = pos; + if (!text->user_set_vma) + text->vma = vma; + else + vma = text->vma; + pos += execp->a_text; + + /* Data. */ + data->filepos = pos; + if (!data->user_set_vma) + data->vma = 0; + vma = data->vma; + + /* Since BSS follows data immediately, see if it needs alignment. */ + vma += data->size; + pad = align_power (vma, bss->alignment_power) - vma; + execp->a_data = data->size + pad; + pos += execp->a_data; + + /* BSS. */ + if (!bss->user_set_vma) + bss->vma = vma; + else + vma = bss->vma; + + /* Fix up exec header. */ + execp->a_bss = bss->size; + N_SET_MAGIC (execp, IMAGIC); +} + bfd_boolean NAME (aout, adjust_sizes_and_vmas) (bfd *abfd) { @@ -1018,7 +1067,9 @@ NAME (aout, adjust_sizes_and_vmas) (bfd *abfd) I understand it better now, but I haven't time to do the cleanup this minute. */ - if (abfd->flags & WP_TEXT) + if (separate_i_d) + adata (abfd).magic = i_magic; + else if (abfd->flags & WP_TEXT) adata (abfd).magic = n_magic; else adata (abfd).magic = o_magic; @@ -1031,6 +1082,7 @@ NAME (aout, adjust_sizes_and_vmas) (bfd *abfd) { case n_magic: str = "NMAGIC"; break; case o_magic: str = "OMAGIC"; break; + case i_magic: str = "IMAGIC"; break; case z_magic: str = "ZMAGIC"; break; default: abort (); } @@ -1056,6 +1108,9 @@ NAME (aout, adjust_sizes_and_vmas) (bfd *abfd) case n_magic: adjust_n_magic (abfd, execp); break; + case i_magic: + adjust_i_magic (abfd, execp); + break; default: abort (); } @@ -3624,6 +3679,7 @@ NAME (aout, final_link) (bfd *abfd, if (bfd_link_pic (info)) abfd->flags |= DYNAMIC; + separate_i_d = info->separate_code; aout_info.info = info; aout_info.output_bfd = abfd; aout_info.contents = NULL; diff --git a/include/ChangeLog b/include/ChangeLog index 7964db299b..97409d9bf1 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,3 +1,8 @@ +2020-04-14 Stephen Casner + + PR ld/25677 + * aout/aout64.h (N_DATADDR): Add IMAGIC case. + 2020-04-02 Jan W. Jagersma * coff/go32exe.h: Remove file. diff --git a/include/aout/aout64.h b/include/aout/aout64.h index 87f145aa2e..f52ae4152e 100644 --- a/include/aout/aout64.h +++ b/include/aout/aout64.h @@ -56,6 +56,7 @@ struct external_exec #else #define OMAGIC 0407 /* Object file or impure executable. */ #define NMAGIC 0410 /* Code indicating pure executable. */ +#define IMAGIC 0411 /* Separate instruction & data spaces for PDP-11. */ #define ZMAGIC 0413 /* Code indicating demand-paged executable. */ #define BMAGIC 0415 /* Used by a b.out object. */ @@ -211,7 +212,9 @@ struct external_exec up to a N_SEGSIZE boundary for pure or pageable files. */ #ifndef N_DATADDR #define N_DATADDR(x) \ - (N_MAGIC (x) == OMAGIC \ + (N_MAGIC (x) == IMAGIC \ + ? (bfd_vma) 0 \ + : N_MAGIC (x) == OMAGIC \ ? (N_TXTADDR (x) + N_TXTSIZE (x)) \ : (N_SEGSIZE (x) + ((N_TXTADDR (x) + N_TXTSIZE (x) - 1) \ & ~ (bfd_vma) (N_SEGSIZE (x) - 1)))) diff --git a/ld/ChangeLog b/ld/ChangeLog index 62c0e0d927..f53b226084 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,25 @@ +2020-04-14 Stephen Casner + + PR ld/25677 + * emulparams/pdp11.sh (SCRIPT_NAME): Change to pdp11. + (EXTRA_EM_FILE): New, add emulation file pdp11. + * scripttempl/pdp11.sc: New, derived from aout.sc without + irrelevant input sections. + * emultempl/pdp11.em (_add_options, _handle_option) + (_list_options): New. Add options -z, --imagic for pdp11-aout. + (_before_parse): Make --omagic be default instead of --nmagic. + (_get_script): Modify special-case linker script for --imagic. + * lexsup.c (parse_args): Explictly set config.text_read_only for -n. + * ld.texi (Options): Add documentation of PDP11-specific options. + (Options): Fix unrelated typo to --no-compact-branches. + * gen-doc.texi: @set PDP11. + * testsuite/ld-pdp11/pdp11.exp: New, start pdp11 testing. + * testsuite/ld-pdp11/sections.s: New, source for options tests. + * testsuite/ld-pdp11/imagic.d: New, test --imagic format. + * testsuite/ld-pdp11/imagicz.d: New, test -z (imagic) format. + * testsuite/ld-pdp11/nmagic.d: New, test --nmagic format. + * testsuite/ld-pdp11/omagic.d: New, test --omagic format. + 2020-04-14 H.J. Lu PR binutils/25707 diff --git a/ld/NEWS b/ld/NEWS index 563af67a34..9795b58a5f 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -3,6 +3,10 @@ * Add command-line options --enable-non-contiguous-regions and --enable-non-contiguous-regions-warnings. +* Add command-line option --imagic for the pdp11-aout target to output format + IMAGIC (0411) for separate instruction and data spaces, and change the + default format option for pdp11-aout to be --omagic. + Changes in 2.34: * The ld check for "PHDR segment not covered by LOAD segment" is more diff --git a/ld/emulparams/pdp11.sh b/ld/emulparams/pdp11.sh index 9b6bbbbd25..3f3326d121 100644 --- a/ld/emulparams/pdp11.sh +++ b/ld/emulparams/pdp11.sh @@ -1,5 +1,6 @@ -SCRIPT_NAME=aout +SCRIPT_NAME=pdp11 OUTPUT_FORMAT="a.out-pdp11" TEXT_START_ADDR=0 TARGET_PAGE_SIZE=8192 +EXTRA_EM_FILE=pdp11 ARCH=pdp11 diff --git a/ld/emultempl/pdp11.em b/ld/emultempl/pdp11.em new file mode 100644 index 0000000000..d18a4207c4 --- /dev/null +++ b/ld/emultempl/pdp11.em @@ -0,0 +1,132 @@ +# This shell script emits a C file. -*- C -*- +# Copyright (C) 2006-2020 Free Software Foundation, Inc. +# +# 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. + +fragment <> e${EMULATION_NAME}.c +echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c +sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c +echo ' ; else if (link_info.separate_code) return' >> e${EMULATION_NAME}.c +sed $sc ldscripts/${EMULATION_NAME}.xn | \ + sed -e "s/ALIGN($TARGET_PAGE_SIZE)/0/" >> e${EMULATION_NAME}.c +echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c +sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c +echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c +sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c +echo ' ; else return' >> e${EMULATION_NAME}.c +sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c +echo '; }' >> e${EMULATION_NAME}.c + +fragment <