1*56bb7041Schristos# This shell script emits a C file. -*- C -*-
2*56bb7041Schristos#   Copyright (C) 2001-2020 Free Software Foundation, Inc.
3*56bb7041Schristos#
4*56bb7041Schristos# This file is part of the GNU Binutils.
5*56bb7041Schristos#
6*56bb7041Schristos# This program is free software; you can redistribute it and/or modify
7*56bb7041Schristos# it under the terms of the GNU General Public License as published by
8*56bb7041Schristos# the Free Software Foundation; either version 3 of the License, or
9*56bb7041Schristos# (at your option) any later version.
10*56bb7041Schristos#
11*56bb7041Schristos# This program is distributed in the hope that it will be useful,
12*56bb7041Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
13*56bb7041Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*56bb7041Schristos# GNU General Public License for more details.
15*56bb7041Schristos#
16*56bb7041Schristos# You should have received a copy of the GNU General Public License
17*56bb7041Schristos# along with this program; if not, write to the Free Software
18*56bb7041Schristos# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19*56bb7041Schristos# MA 02110-1301, USA.
20*56bb7041Schristos#
21*56bb7041Schristos
22*56bb7041Schristos# This file is sourced from elf.em and mmo.em, used to define
23*56bb7041Schristos# MMIX-specific things common to ELF and MMO.
24*56bb7041Schristos
25*56bb7041Schristosfragment <<EOF
26*56bb7041Schristos#include "elf/mmix.h"
27*56bb7041Schristos
28*56bb7041Schristosstatic void gld${EMULATION_NAME}_before_parse (void);
29*56bb7041Schristos
30*56bb7041Schristosstatic void
31*56bb7041Schristosmmix_before_parse (void)
32*56bb7041Schristos{
33*56bb7041Schristos  link_info.check_relocs_after_open_input = TRUE;
34*56bb7041Schristos  gld${EMULATION_NAME}_before_parse ();
35*56bb7041Schristos}
36*56bb7041Schristos
37*56bb7041Schristos/* Set up handling of linker-allocated global registers.  */
38*56bb7041Schristos
39*56bb7041Schristosstatic void
40*56bb7041Schristosmmix_before_allocation (void)
41*56bb7041Schristos{
42*56bb7041Schristos  /* Call the default first.  */
43*56bb7041Schristos  gld${EMULATION_NAME}_before_allocation ();
44*56bb7041Schristos
45*56bb7041Schristos  /* There's a needrelax.em which uses this ..._before_allocation-hook and
46*56bb7041Schristos     just has the statement below as payload.  It's more of a hassle to
47*56bb7041Schristos     use that than to just include these two lines and take the
48*56bb7041Schristos     maintenance burden to keep them in sync.  (Of course we lose the
49*56bb7041Schristos     maintenance burden of checking that it still does what we need.)  */
50*56bb7041Schristos
51*56bb7041Schristos  /* Force -relax on (regardless of whether we're doing a relocatable
52*56bb7041Schristos     link).  */
53*56bb7041Schristos  ENABLE_RELAXATION;
54*56bb7041Schristos
55*56bb7041Schristos  if (!_bfd_mmix_before_linker_allocation (link_info.output_bfd, &link_info))
56*56bb7041Schristos    einfo (_("%X%P: internal problems setting up section %s"),
57*56bb7041Schristos	   MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
58*56bb7041Schristos}
59*56bb7041Schristos
60*56bb7041Schristos/* We need to set the VMA of the .MMIX.reg_contents section when it has
61*56bb7041Schristos   been allocated, and produce the final settings for the linker-generated
62*56bb7041Schristos   GREGs.  */
63*56bb7041Schristos
64*56bb7041Schristosstatic void
65*56bb7041Schristosmmix_after_allocation (void)
66*56bb7041Schristos{
67*56bb7041Schristos  asection *sec;
68*56bb7041Schristos  bfd_signed_vma regvma;
69*56bb7041Schristos
70*56bb7041Schristos  gld${EMULATION_NAME}_after_allocation ();
71*56bb7041Schristos
72*56bb7041Schristos  /* If there's no register section, we don't need to do anything.  On the
73*56bb7041Schristos     other hand, if there's a non-standard linker-script without a mapping
74*56bb7041Schristos     from MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME when that section is
75*56bb7041Schristos     present (as in the ld test "NOCROSSREFS 2"), that section (1) will be
76*56bb7041Schristos     orphaned; not inserted in MMIX_REG_CONTENTS_SECTION_NAME and (2) we
77*56bb7041Schristos     will not do the necessary preparations for those relocations that
78*56bb7041Schristos     caused it to be created.  We'll SEGV from the latter error.  The
79*56bb7041Schristos     former error in separation will result in a non-working binary, but
80*56bb7041Schristos     that's expected when you play tricks with linker scripts.  The
81*56bb7041Schristos     "NOCROSSREFS 2" test does not run the output so it does not matter
82*56bb7041Schristos     there.  */
83*56bb7041Schristos  sec = bfd_get_section_by_name (link_info.output_bfd,
84*56bb7041Schristos				 MMIX_REG_CONTENTS_SECTION_NAME);
85*56bb7041Schristos  if (sec == NULL)
86*56bb7041Schristos    sec
87*56bb7041Schristos      = bfd_get_section_by_name (link_info.output_bfd,
88*56bb7041Schristos				 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
89*56bb7041Schristos  if (sec == NULL)
90*56bb7041Schristos    return;
91*56bb7041Schristos
92*56bb7041Schristos  regvma = 256 * 8 - sec->size - 8;
93*56bb7041Schristos
94*56bb7041Schristos  /* If we start on a local register, we have too many global registers.
95*56bb7041Schristos     We treat this error as nonfatal (meaning processing will continue in
96*56bb7041Schristos     search for other errors), because it's a link error in the same way
97*56bb7041Schristos     as an undefined symbol.  */
98*56bb7041Schristos  if (regvma < 32 * 8)
99*56bb7041Schristos    {
100*56bb7041Schristos      einfo (_("%X%P: too many global registers: %u, max 223\n"),
101*56bb7041Schristos	     (unsigned) sec->size / 8);
102*56bb7041Schristos      regvma = 32 * 8;
103*56bb7041Schristos    }
104*56bb7041Schristos
105*56bb7041Schristos  /* Set vma to correspond to first such register number * 8.  */
106*56bb7041Schristos  bfd_set_section_vma (sec, (bfd_vma) regvma);
107*56bb7041Schristos
108*56bb7041Schristos  /* Simplify symbol output for the register section (without contents;
109*56bb7041Schristos     created for register symbols) by setting the output offset to 0.
110*56bb7041Schristos     This section is only present when there are register symbols.  */
111*56bb7041Schristos  sec = bfd_get_section_by_name (link_info.output_bfd, MMIX_REG_SECTION_NAME);
112*56bb7041Schristos  if (sec != NULL)
113*56bb7041Schristos    bfd_set_section_vma (sec, 0);
114*56bb7041Schristos
115*56bb7041Schristos  if (!_bfd_mmix_after_linker_allocation (link_info.output_bfd, &link_info))
116*56bb7041Schristos    {
117*56bb7041Schristos      /* This is a fatal error; make einfo call not return.  */
118*56bb7041Schristos      einfo (_("%F%P: can't finalize linker-allocated global registers\n"));
119*56bb7041Schristos    }
120*56bb7041Schristos}
121*56bb7041SchristosEOF
122*56bb7041Schristos
123*56bb7041SchristosLDEMUL_BEFORE_PARSE=mmix_before_parse
124*56bb7041SchristosLDEMUL_AFTER_ALLOCATION=mmix_after_allocation
125*56bb7041SchristosLDEMUL_BEFORE_ALLOCATION=mmix_before_allocation
126