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.
This commit is contained in:
Stephen Casner 2020-04-14 14:41:27 +01:00 committed by Nick Clifton
parent 194d088fb1
commit fa1477dc34
19 changed files with 457 additions and 7 deletions

View File

@ -1,3 +1,10 @@
2020-04-14 Stephen Casner <casner@acm.org>
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 <ro@CeBiTec.Uni-Bielefeld.DE>
Nick Clifton <nickc@redhat.com>

View File

@ -359,7 +359,8 @@ enum aout_magic {
undecided_magic = 0,
z_magic,
o_magic,
n_magic
n_magic,
i_magic
};
struct aoutdata

View File

@ -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;

View File

@ -1,3 +1,8 @@
2020-04-14 Stephen Casner <casner@acm.org>
PR ld/25677
* aout/aout64.h (N_DATADDR): Add IMAGIC case.
2020-04-02 Jan W. Jagersma <jwjagersma@gmail.com>
* coff/go32exe.h: Remove file.

View File

@ -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))))

View File

@ -1,3 +1,25 @@
2020-04-14 Stephen Casner <casner@acm.org>
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 <hongjiu.lu@intel.com>
PR binutils/25707

View File

@ -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

View File

@ -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

132
ld/emultempl/pdp11.em Normal file
View File

@ -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 <<EOF
/* --- \begin{pdp11.em} */
#include "getopt.h"
static void
gld${EMULATION_NAME}_before_parse (void)
{
ldfile_set_output_arch ("`echo ${ARCH}`", bfd_arch_unknown);
/* for PDP11 Unix compatibility, default to --omagic */
config.magic_demand_paged = FALSE;
config.text_read_only = FALSE;
}
/* PDP11 specific options. */
#define OPTION_IMAGIC 301
static void
gld${EMULATION_NAME}_add_options
(int ns ATTRIBUTE_UNUSED,
char **shortopts,
int nl,
struct option **longopts,
int nrl ATTRIBUTE_UNUSED,
struct option **really_longopts ATTRIBUTE_UNUSED)
{
static const char xtra_short[] = "z";
static const struct option xtra_long[] =
{
{"imagic", no_argument, NULL, OPTION_IMAGIC},
{NULL, no_argument, NULL, 0}
};
*shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
*longopts
= xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
}
static void
gld${EMULATION_NAME}_list_options (FILE *file)
{
fprintf (file, _(" -N, --omagic Do not make text readonly, do not page align data (default)\n"));
fprintf (file, _(" -n, --nmagic Make text readonly, align data to next page\n"));
fprintf (file, _(" -z, --imagic Make text readonly, separate instruction and data spaces\n"));
fprintf (file, _(" --no-omagic Equivalent to --nmagic\n"));
}
static bfd_boolean
gld${EMULATION_NAME}_handle_option (int optc)
{
switch (optc)
{
default:
return FALSE;
case 'z':
case OPTION_IMAGIC:
link_info.separate_code = 1;
/* The --imagic format causes the .text and .data sections to occupy the
same memory addresses in separate spaces, so don't check overlap. */
command_line.check_section_addresses = 0;
break;
}
return TRUE;
}
/* We need a special case to prepare an additional linker script for option
* --imagic where the .data section starts at address 0 rather than directly
* following the .text section or being aligned to the next page after the
* .text section. */
static char *
gld${EMULATION_NAME}_get_script (int *isfile)
EOF
# Scripts compiled in.
# sed commands to quote an ld script as a C string.
sc="-f stringify.sed"
fragment <<EOF
{
*isfile = 0;
if (bfd_link_relocatable (&link_info) && config.build_constructors)
return
EOF
sed $sc ldscripts/${EMULATION_NAME}.xu >> 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 <<EOF
/* --- \end{pdp11.em} */
EOF
LDEMUL_BEFORE_PARSE=gld"$EMULATION_NAME"_before_parse
LDEMUL_ADD_OPTIONS=gld"$EMULATION_NAME"_add_options
LDEMUL_HANDLE_OPTION=gld"$EMULATION_NAME"_handle_option
LDEMUL_LIST_OPTIONS=gld"$EMULATION_NAME"_list_options
LDEMUL_GET_SCRIPT=gld"$EMULATION_NAME"_get_script

View File

@ -19,6 +19,7 @@
@set MSP430
@set NDS32
@set NIOSII
@set PDP11
@set POWERPC
@set POWERPC64
@set Renesas

View File

@ -31,6 +31,7 @@
@set MSP430
@set NDS32
@set NIOSII
@set PDP11
@set POWERPC
@set POWERPC64
@set Renesas
@ -3260,7 +3261,7 @@ an error.
@kindex --compact-branches
@item --compact-branches
@kindex --no-compact-branches
@item --compact-branches
@itemx --no-compact-branches
These options control the generation of compact instructions by the linker
in the PLT entries for MIPS R6.
@ -3269,6 +3270,73 @@ in the PLT entries for MIPS R6.
@c man end
@end ifset
@ifset PDP11
@subsection Options specific to PDP11 targets
@c man begin OPTIONS
For the pdp11-aout target, three variants of the output format can be
produced as selected by the following options. The default variant
for pdp11-aout is the @samp{--omagic} option, whereas for other
targets @samp{--nmagic} is the default. The @samp{--imagic} option is
defined only for the pdp11-aout target, while the others are described
here as they apply to the pdp11-aout target.
@table @gcctabopt
@kindex -N
@item -N
@kindex --omagic
@itemx --omagic
Mark the output as @code{OMAGIC} (0407) in the @file{a.out} header to
indicate that the text segment is not to be write-protected and
shared. Since the text and data sections are both readable and
writable, the data section is allocated immediately contiguous after
the text segment. This is the oldest format for PDP11 executable
programs and is the default for @command{ld} on PDP11 Unix systems
from the beginning through 2.11BSD.
@kindex -n
@item -n
@kindex --nmagic
@itemx --nmagic
Mark the output as @code{NMAGIC} (0410) in the @file{a.out} header to
indicate that when the output file is executed, the text portion will
be read-only and shareable among all processes executing the same
file. This involves moving the data areas up to the first possible 8K
byte page boundary following the end of the text. This option creates
a @emph{pure executable} format.
@kindex -z
@item -z
@kindex --imagic
@itemx --imagic
Mark the output as @code{IMAGIC} (0411) in the @file{a.out} header to
indicate that when the output file is executed, the program text and
data areas will be loaded into separate address spaces using the split
instruction and data space feature of the memory management unit in
larger models of the PDP11. This doubles the address space available
to the program. The text segment is again pure, write-protected, and
shareable. The only difference in the output format between this
option and the others, besides the magic number, is that both the text
and data sections start at location 0. The @samp{-z} option selected
this format in 2.11BSD. This option creates a @emph{separate
executable} format.
@kindex --no-omagic
@item --no-omagic
Equivalent to @samp{--nmagic} for pdp11-aout.
@end table
@c man end
@end ifset
@ifset UsesEnvVars
@node Environment
@section Environment Variables

View File

@ -938,6 +938,7 @@ parse_args (unsigned argc, char **argv)
Use --call-shared or -Bdynamic for this. */
break;
case 'n':
config.text_read_only = TRUE;
config.magic_demand_paged = FALSE;
input_flags.dynamic = FALSE;
break;

56
ld/scripttempl/pdp11.sc Normal file
View File

@ -0,0 +1,56 @@
# Copyright (C) 2014-2020 Free Software Foundation, Inc.
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.
#
test -z "${BIG_OUTPUT_FORMAT}" && BIG_OUTPUT_FORMAT=${OUTPUT_FORMAT}
test -z "${LITTLE_OUTPUT_FORMAT}" && LITTLE_OUTPUT_FORMAT=${OUTPUT_FORMAT}
test -z "${ALIGNMENT}" && ALIGNMENT="2"
cat <<EOF
/* Copyright (C) 2014-2020 Free Software Foundation, Inc.
Copying and distribution of this script, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. */
OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
"${LITTLE_OUTPUT_FORMAT}")
OUTPUT_ARCH(${ARCH})
${RELOCATING+${LIB_SEARCH_DIRS}}
${STACKZERO+${RELOCATING+${STACKZERO}}}
${SHLIB_PATH+${RELOCATING+${SHLIB_PATH}}}
${RELOCATING+${EXECUTABLE_SYMBOLS}}
${RELOCATING+PROVIDE (__stack = 0);}
SECTIONS
{
${RELOCATING+. = ${TEXT_START_ADDR};}
.text :
{
CREATE_OBJECT_SYMBOLS
*(.text)
${RELOCATING+_etext = .;}
${RELOCATING+__etext = .;}
${PAD_TEXT+${RELOCATING+. = ${DATA_ALIGNMENT};}}
}
${RELOCATING+. = ${DATA_ALIGNMENT};}
.data :
{
*(.data)
${CONSTRUCTING+CONSTRUCTORS}
${RELOCATING+_edata = .;}
${RELOCATING+__edata = .;}
}
.bss :
{
${RELOCATING+ __bss_start = .};
*(.bss)
*(COMMON)
${RELOCATING+. = ALIGN(${ALIGNMENT});}
${RELOCATING+_end = . };
${RELOCATING+__end = . };
}
}
EOF

View File

@ -0,0 +1,12 @@
#name: pdp11-aout imagic format
# nm sort alphabetically since both _start and _data are 0
#source: sections.s
#ld: --imagic
#DUMPPROG: nm
#...
0+2 B _bss
#...
0+0 D _data
#...
0+0 T _start
#pass

View File

@ -0,0 +1,12 @@
#name: pdp11-aout imagic format -z
# nm sort alphabetically since both _start and _data are 0
#source: sections.s
#ld: -z
#DUMPPROG: nm
#...
0+2 B _bss
#...
0+0 D _data
#...
0+0 T _start
#pass

View File

@ -0,0 +1,11 @@
#name: pdp11-aout nmagic format
#source: sections.s
#ld: --nmagic
#nm: -n
#...
0+0 T _start
#...
0*2000 D _data
#...
0*2002 B _bss
#pass

View File

@ -0,0 +1,12 @@
#name: pdp11-aout omagic format
# also testing that --omagic is the default
#source: sections.s
#ld:
#nm: -n
#...
0+0 T _start
#...
0+6 D _data
#...
0+8 B _bss
#pass

View File

@ -0,0 +1,33 @@
# Expect script for ld-pdp11 tests
# Copyright (C) 2016-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.
#
# Test pdp11 linking; at this point just the options for the three
# a.out format variants.
if ![istarget "pdp11-*-*"] then {
return
}
set pdp11_test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
foreach pdp11_test $pdp11_test_list {
verbose [file rootname $pdp11_test]
run_dump_test [file rootname $pdp11_test]
}

View File

@ -0,0 +1,13 @@
.globl _start
.text
_start:
mov _data,_bss
.globl _data
.data
_data:
.word 1
.globl _bss
.bss
_bss:
.=.+2
.end