xref: /openbsd-src/gnu/usr.bin/binutils-2.17/bfd/elf64-mmix.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* MMIX-specific support for 64-bit ELF.
2*3d8817e4Smiod    Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3*3d8817e4Smiod    Contributed by Hans-Peter Nilsson <hp@bitrange.com>
4*3d8817e4Smiod 
5*3d8817e4Smiod This file is part of BFD, the Binary File Descriptor library.
6*3d8817e4Smiod 
7*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
8*3d8817e4Smiod it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
10*3d8817e4Smiod (at your option) any later version.
11*3d8817e4Smiod 
12*3d8817e4Smiod This program is distributed in the hope that it will be useful,
13*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*3d8817e4Smiod GNU General Public License for more details.
16*3d8817e4Smiod 
17*3d8817e4Smiod You should have received a copy of the GNU General Public License
18*3d8817e4Smiod along with this program; if not, write to the Free Software
19*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20*3d8817e4Smiod 
21*3d8817e4Smiod /* No specific ABI or "processor-specific supplement" defined.  */
22*3d8817e4Smiod 
23*3d8817e4Smiod /* TODO:
24*3d8817e4Smiod    - "Traditional" linker relaxation (shrinking whole sections).
25*3d8817e4Smiod    - Merge reloc stubs jumping to same location.
26*3d8817e4Smiod    - GETA stub relaxation (call a stub for out of range new
27*3d8817e4Smiod      R_MMIX_GETA_STUBBABLE).  */
28*3d8817e4Smiod 
29*3d8817e4Smiod #include "bfd.h"
30*3d8817e4Smiod #include "sysdep.h"
31*3d8817e4Smiod #include "libbfd.h"
32*3d8817e4Smiod #include "elf-bfd.h"
33*3d8817e4Smiod #include "elf/mmix.h"
34*3d8817e4Smiod #include "opcode/mmix.h"
35*3d8817e4Smiod 
36*3d8817e4Smiod #define MINUS_ONE	(((bfd_vma) 0) - 1)
37*3d8817e4Smiod 
38*3d8817e4Smiod #define MAX_PUSHJ_STUB_SIZE (5 * 4)
39*3d8817e4Smiod 
40*3d8817e4Smiod /* Put these everywhere in new code.  */
41*3d8817e4Smiod #define FATAL_DEBUG						\
42*3d8817e4Smiod  _bfd_abort (__FILE__, __LINE__,				\
43*3d8817e4Smiod 	     "Internal: Non-debugged code (test-case missing)")
44*3d8817e4Smiod 
45*3d8817e4Smiod #define BAD_CASE(x)				\
46*3d8817e4Smiod  _bfd_abort (__FILE__, __LINE__,		\
47*3d8817e4Smiod 	     "bad case for " #x)
48*3d8817e4Smiod 
49*3d8817e4Smiod struct _mmix_elf_section_data
50*3d8817e4Smiod {
51*3d8817e4Smiod   struct bfd_elf_section_data elf;
52*3d8817e4Smiod   union
53*3d8817e4Smiod   {
54*3d8817e4Smiod     struct bpo_reloc_section_info *reloc;
55*3d8817e4Smiod     struct bpo_greg_section_info *greg;
56*3d8817e4Smiod   } bpo;
57*3d8817e4Smiod 
58*3d8817e4Smiod   struct pushj_stub_info
59*3d8817e4Smiod   {
60*3d8817e4Smiod     /* Maximum number of stubs needed for this section.  */
61*3d8817e4Smiod     bfd_size_type n_pushj_relocs;
62*3d8817e4Smiod 
63*3d8817e4Smiod     /* Size of stubs after a mmix_elf_relax_section round.  */
64*3d8817e4Smiod     bfd_size_type stubs_size_sum;
65*3d8817e4Smiod 
66*3d8817e4Smiod     /* Per-reloc stubs_size_sum information.  The stubs_size_sum member is the sum
67*3d8817e4Smiod        of these.  Allocated in mmix_elf_check_common_relocs.  */
68*3d8817e4Smiod     bfd_size_type *stub_size;
69*3d8817e4Smiod 
70*3d8817e4Smiod     /* Offset of next stub during relocation.  Somewhat redundant with the
71*3d8817e4Smiod        above: error coverage is easier and we don't have to reset the
72*3d8817e4Smiod        stubs_size_sum for relocation.  */
73*3d8817e4Smiod     bfd_size_type stub_offset;
74*3d8817e4Smiod   } pjs;
75*3d8817e4Smiod };
76*3d8817e4Smiod 
77*3d8817e4Smiod #define mmix_elf_section_data(sec) \
78*3d8817e4Smiod   ((struct _mmix_elf_section_data *) elf_section_data (sec))
79*3d8817e4Smiod 
80*3d8817e4Smiod /* For each section containing a base-plus-offset (BPO) reloc, we attach
81*3d8817e4Smiod    this struct as mmix_elf_section_data (section)->bpo, which is otherwise
82*3d8817e4Smiod    NULL.  */
83*3d8817e4Smiod struct bpo_reloc_section_info
84*3d8817e4Smiod   {
85*3d8817e4Smiod     /* The base is 1; this is the first number in this section.  */
86*3d8817e4Smiod     size_t first_base_plus_offset_reloc;
87*3d8817e4Smiod 
88*3d8817e4Smiod     /* Number of BPO-relocs in this section.  */
89*3d8817e4Smiod     size_t n_bpo_relocs_this_section;
90*3d8817e4Smiod 
91*3d8817e4Smiod     /* Running index, used at relocation time.  */
92*3d8817e4Smiod     size_t bpo_index;
93*3d8817e4Smiod 
94*3d8817e4Smiod     /* We don't have access to the bfd_link_info struct in
95*3d8817e4Smiod        mmix_final_link_relocate.  What we really want to get at is the
96*3d8817e4Smiod        global single struct greg_relocation, so we stash it here.  */
97*3d8817e4Smiod     asection *bpo_greg_section;
98*3d8817e4Smiod   };
99*3d8817e4Smiod 
100*3d8817e4Smiod /* Helper struct (in global context) for the one below.
101*3d8817e4Smiod    There's one of these created for every BPO reloc.  */
102*3d8817e4Smiod struct bpo_reloc_request
103*3d8817e4Smiod   {
104*3d8817e4Smiod     bfd_vma value;
105*3d8817e4Smiod 
106*3d8817e4Smiod     /* Valid after relaxation.  The base is 0; the first register number
107*3d8817e4Smiod        must be added.  The offset is in range 0..255.  */
108*3d8817e4Smiod     size_t regindex;
109*3d8817e4Smiod     size_t offset;
110*3d8817e4Smiod 
111*3d8817e4Smiod     /* The order number for this BPO reloc, corresponding to the order in
112*3d8817e4Smiod        which BPO relocs were found.  Used to create an index after reloc
113*3d8817e4Smiod        requests are sorted.  */
114*3d8817e4Smiod     size_t bpo_reloc_no;
115*3d8817e4Smiod 
116*3d8817e4Smiod     /* Set when the value is computed.  Better than coding "guard values"
117*3d8817e4Smiod        into the other members.  Is FALSE only for BPO relocs in a GC:ed
118*3d8817e4Smiod        section.  */
119*3d8817e4Smiod     bfd_boolean valid;
120*3d8817e4Smiod   };
121*3d8817e4Smiod 
122*3d8817e4Smiod /* We attach this as mmix_elf_section_data (sec)->bpo in the linker-allocated
123*3d8817e4Smiod    greg contents section (MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME),
124*3d8817e4Smiod    which is linked into the register contents section
125*3d8817e4Smiod    (MMIX_REG_CONTENTS_SECTION_NAME).  This section is created by the
126*3d8817e4Smiod    linker; using the same hook as for usual with BPO relocs does not
127*3d8817e4Smiod    collide.  */
128*3d8817e4Smiod struct bpo_greg_section_info
129*3d8817e4Smiod   {
130*3d8817e4Smiod     /* After GC, this reflects the number of remaining, non-excluded
131*3d8817e4Smiod        BPO-relocs.  */
132*3d8817e4Smiod     size_t n_bpo_relocs;
133*3d8817e4Smiod 
134*3d8817e4Smiod     /* This is the number of allocated bpo_reloc_requests; the size of
135*3d8817e4Smiod        sorted_indexes.  Valid after the check.*relocs functions are called
136*3d8817e4Smiod        for all incoming sections.  It includes the number of BPO relocs in
137*3d8817e4Smiod        sections that were GC:ed.  */
138*3d8817e4Smiod     size_t n_max_bpo_relocs;
139*3d8817e4Smiod 
140*3d8817e4Smiod     /* A counter used to find out when to fold the BPO gregs, since we
141*3d8817e4Smiod        don't have a single "after-relaxation" hook.  */
142*3d8817e4Smiod     size_t n_remaining_bpo_relocs_this_relaxation_round;
143*3d8817e4Smiod 
144*3d8817e4Smiod     /* The number of linker-allocated GREGs resulting from BPO relocs.
145*3d8817e4Smiod        This is an approximation after _bfd_mmix_before_linker_allocation
146*3d8817e4Smiod        and supposedly accurate after mmix_elf_relax_section is called for
147*3d8817e4Smiod        all incoming non-collected sections.  */
148*3d8817e4Smiod     size_t n_allocated_bpo_gregs;
149*3d8817e4Smiod 
150*3d8817e4Smiod     /* Index into reloc_request[], sorted on increasing "value", secondary
151*3d8817e4Smiod        by increasing index for strict sorting order.  */
152*3d8817e4Smiod     size_t *bpo_reloc_indexes;
153*3d8817e4Smiod 
154*3d8817e4Smiod     /* An array of all relocations, with the "value" member filled in by
155*3d8817e4Smiod        the relaxation function.  */
156*3d8817e4Smiod     struct bpo_reloc_request *reloc_request;
157*3d8817e4Smiod   };
158*3d8817e4Smiod 
159*3d8817e4Smiod static bfd_boolean mmix_elf_link_output_symbol_hook
160*3d8817e4Smiod   PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
161*3d8817e4Smiod 	   asection *, struct elf_link_hash_entry *));
162*3d8817e4Smiod 
163*3d8817e4Smiod static bfd_reloc_status_type mmix_elf_reloc
164*3d8817e4Smiod   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
165*3d8817e4Smiod 
166*3d8817e4Smiod static reloc_howto_type *bfd_elf64_bfd_reloc_type_lookup
167*3d8817e4Smiod   PARAMS ((bfd *, bfd_reloc_code_real_type));
168*3d8817e4Smiod 
169*3d8817e4Smiod static void mmix_info_to_howto_rela
170*3d8817e4Smiod   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
171*3d8817e4Smiod 
172*3d8817e4Smiod static int mmix_elf_sort_relocs PARAMS ((const PTR, const PTR));
173*3d8817e4Smiod 
174*3d8817e4Smiod static bfd_boolean mmix_elf_new_section_hook
175*3d8817e4Smiod   PARAMS ((bfd *, asection *));
176*3d8817e4Smiod 
177*3d8817e4Smiod static bfd_boolean mmix_elf_check_relocs
178*3d8817e4Smiod   PARAMS ((bfd *, struct bfd_link_info *, asection *,
179*3d8817e4Smiod 	   const Elf_Internal_Rela *));
180*3d8817e4Smiod 
181*3d8817e4Smiod static bfd_boolean mmix_elf_check_common_relocs
182*3d8817e4Smiod   PARAMS ((bfd *, struct bfd_link_info *, asection *,
183*3d8817e4Smiod 	   const Elf_Internal_Rela *));
184*3d8817e4Smiod 
185*3d8817e4Smiod static bfd_boolean mmix_elf_relocate_section
186*3d8817e4Smiod   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
187*3d8817e4Smiod 	   Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
188*3d8817e4Smiod 
189*3d8817e4Smiod static asection * mmix_elf_gc_mark_hook
190*3d8817e4Smiod   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
191*3d8817e4Smiod 	   struct elf_link_hash_entry *, Elf_Internal_Sym *));
192*3d8817e4Smiod 
193*3d8817e4Smiod static bfd_boolean mmix_elf_gc_sweep_hook
194*3d8817e4Smiod   PARAMS ((bfd *, struct bfd_link_info *, asection *,
195*3d8817e4Smiod 	   const Elf_Internal_Rela *));
196*3d8817e4Smiod 
197*3d8817e4Smiod static bfd_reloc_status_type mmix_final_link_relocate
198*3d8817e4Smiod   PARAMS ((reloc_howto_type *, asection *, bfd_byte *,
199*3d8817e4Smiod 	   bfd_vma, bfd_signed_vma, bfd_vma, const char *, asection *));
200*3d8817e4Smiod 
201*3d8817e4Smiod static bfd_reloc_status_type mmix_elf_perform_relocation
202*3d8817e4Smiod   PARAMS ((asection *, reloc_howto_type *, PTR, bfd_vma, bfd_vma));
203*3d8817e4Smiod 
204*3d8817e4Smiod static bfd_boolean mmix_elf_section_from_bfd_section
205*3d8817e4Smiod   PARAMS ((bfd *, asection *, int *));
206*3d8817e4Smiod 
207*3d8817e4Smiod static bfd_boolean mmix_elf_add_symbol_hook
208*3d8817e4Smiod   PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
209*3d8817e4Smiod 	   const char **, flagword *, asection **, bfd_vma *));
210*3d8817e4Smiod 
211*3d8817e4Smiod static bfd_boolean mmix_elf_is_local_label_name
212*3d8817e4Smiod   PARAMS ((bfd *, const char *));
213*3d8817e4Smiod 
214*3d8817e4Smiod static int bpo_reloc_request_sort_fn PARAMS ((const PTR, const PTR));
215*3d8817e4Smiod 
216*3d8817e4Smiod static bfd_boolean mmix_elf_relax_section
217*3d8817e4Smiod   PARAMS ((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
218*3d8817e4Smiod 	   bfd_boolean *again));
219*3d8817e4Smiod 
220*3d8817e4Smiod extern bfd_boolean mmix_elf_final_link PARAMS ((bfd *, struct bfd_link_info *));
221*3d8817e4Smiod 
222*3d8817e4Smiod extern void mmix_elf_symbol_processing PARAMS ((bfd *, asymbol *));
223*3d8817e4Smiod 
224*3d8817e4Smiod /* Only intended to be called from a debugger.  */
225*3d8817e4Smiod extern void mmix_dump_bpo_gregs
226*3d8817e4Smiod   PARAMS ((struct bfd_link_info *, bfd_error_handler_type));
227*3d8817e4Smiod 
228*3d8817e4Smiod static void
229*3d8817e4Smiod mmix_set_relaxable_size
230*3d8817e4Smiod   PARAMS ((bfd *, asection *, void *));
231*3d8817e4Smiod 
232*3d8817e4Smiod 
233*3d8817e4Smiod /* Watch out: this currently needs to have elements with the same index as
234*3d8817e4Smiod    their R_MMIX_ number.  */
235*3d8817e4Smiod static reloc_howto_type elf_mmix_howto_table[] =
236*3d8817e4Smiod  {
237*3d8817e4Smiod   /* This reloc does nothing.  */
238*3d8817e4Smiod   HOWTO (R_MMIX_NONE,		/* type */
239*3d8817e4Smiod 	 0,			/* rightshift */
240*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
241*3d8817e4Smiod 	 32,			/* bitsize */
242*3d8817e4Smiod 	 FALSE,			/* pc_relative */
243*3d8817e4Smiod 	 0,			/* bitpos */
244*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
245*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
246*3d8817e4Smiod 	 "R_MMIX_NONE",		/* name */
247*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
248*3d8817e4Smiod 	 0,			/* src_mask */
249*3d8817e4Smiod 	 0,			/* dst_mask */
250*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
251*3d8817e4Smiod 
252*3d8817e4Smiod   /* An 8 bit absolute relocation.  */
253*3d8817e4Smiod   HOWTO (R_MMIX_8,		/* type */
254*3d8817e4Smiod 	 0,			/* rightshift */
255*3d8817e4Smiod 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
256*3d8817e4Smiod 	 8,			/* bitsize */
257*3d8817e4Smiod 	 FALSE,			/* pc_relative */
258*3d8817e4Smiod 	 0,			/* bitpos */
259*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
260*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
261*3d8817e4Smiod 	 "R_MMIX_8",		/* name */
262*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
263*3d8817e4Smiod 	 0,			/* src_mask */
264*3d8817e4Smiod 	 0xff,			/* dst_mask */
265*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
266*3d8817e4Smiod 
267*3d8817e4Smiod   /* An 16 bit absolute relocation.  */
268*3d8817e4Smiod   HOWTO (R_MMIX_16,		/* type */
269*3d8817e4Smiod 	 0,			/* rightshift */
270*3d8817e4Smiod 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
271*3d8817e4Smiod 	 16,			/* bitsize */
272*3d8817e4Smiod 	 FALSE,			/* pc_relative */
273*3d8817e4Smiod 	 0,			/* bitpos */
274*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
275*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
276*3d8817e4Smiod 	 "R_MMIX_16",		/* name */
277*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
278*3d8817e4Smiod 	 0,			/* src_mask */
279*3d8817e4Smiod 	 0xffff,		/* dst_mask */
280*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
281*3d8817e4Smiod 
282*3d8817e4Smiod   /* An 24 bit absolute relocation.  */
283*3d8817e4Smiod   HOWTO (R_MMIX_24,		/* type */
284*3d8817e4Smiod 	 0,			/* rightshift */
285*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
286*3d8817e4Smiod 	 24,			/* bitsize */
287*3d8817e4Smiod 	 FALSE,			/* pc_relative */
288*3d8817e4Smiod 	 0,			/* bitpos */
289*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
290*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
291*3d8817e4Smiod 	 "R_MMIX_24",		/* name */
292*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
293*3d8817e4Smiod 	 ~0xffffff,		/* src_mask */
294*3d8817e4Smiod 	 0xffffff,		/* dst_mask */
295*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
296*3d8817e4Smiod 
297*3d8817e4Smiod   /* A 32 bit absolute relocation.  */
298*3d8817e4Smiod   HOWTO (R_MMIX_32,		/* type */
299*3d8817e4Smiod 	 0,			/* rightshift */
300*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
301*3d8817e4Smiod 	 32,			/* bitsize */
302*3d8817e4Smiod 	 FALSE,			/* pc_relative */
303*3d8817e4Smiod 	 0,			/* bitpos */
304*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
305*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
306*3d8817e4Smiod 	 "R_MMIX_32",		/* name */
307*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
308*3d8817e4Smiod 	 0,			/* src_mask */
309*3d8817e4Smiod 	 0xffffffff,		/* dst_mask */
310*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
311*3d8817e4Smiod 
312*3d8817e4Smiod   /* 64 bit relocation.  */
313*3d8817e4Smiod   HOWTO (R_MMIX_64,		/* type */
314*3d8817e4Smiod 	 0,			/* rightshift */
315*3d8817e4Smiod 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
316*3d8817e4Smiod 	 64,			/* bitsize */
317*3d8817e4Smiod 	 FALSE,			/* pc_relative */
318*3d8817e4Smiod 	 0,			/* bitpos */
319*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
320*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
321*3d8817e4Smiod 	 "R_MMIX_64",		/* name */
322*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
323*3d8817e4Smiod 	 0,			/* src_mask */
324*3d8817e4Smiod 	 MINUS_ONE,		/* dst_mask */
325*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
326*3d8817e4Smiod 
327*3d8817e4Smiod   /* An 8 bit PC-relative relocation.  */
328*3d8817e4Smiod   HOWTO (R_MMIX_PC_8,		/* type */
329*3d8817e4Smiod 	 0,			/* rightshift */
330*3d8817e4Smiod 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
331*3d8817e4Smiod 	 8,			/* bitsize */
332*3d8817e4Smiod 	 TRUE,			/* pc_relative */
333*3d8817e4Smiod 	 0,			/* bitpos */
334*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
335*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
336*3d8817e4Smiod 	 "R_MMIX_PC_8",		/* name */
337*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
338*3d8817e4Smiod 	 0,			/* src_mask */
339*3d8817e4Smiod 	 0xff,			/* dst_mask */
340*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
341*3d8817e4Smiod 
342*3d8817e4Smiod   /* An 16 bit PC-relative relocation.  */
343*3d8817e4Smiod   HOWTO (R_MMIX_PC_16,		/* type */
344*3d8817e4Smiod 	 0,			/* rightshift */
345*3d8817e4Smiod 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
346*3d8817e4Smiod 	 16,			/* bitsize */
347*3d8817e4Smiod 	 TRUE,			/* pc_relative */
348*3d8817e4Smiod 	 0,			/* bitpos */
349*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
350*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
351*3d8817e4Smiod 	 "R_MMIX_PC_16",	/* name */
352*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
353*3d8817e4Smiod 	 0,			/* src_mask */
354*3d8817e4Smiod 	 0xffff,		/* dst_mask */
355*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
356*3d8817e4Smiod 
357*3d8817e4Smiod   /* An 24 bit PC-relative relocation.  */
358*3d8817e4Smiod   HOWTO (R_MMIX_PC_24,		/* type */
359*3d8817e4Smiod 	 0,			/* rightshift */
360*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
361*3d8817e4Smiod 	 24,			/* bitsize */
362*3d8817e4Smiod 	 TRUE,			/* pc_relative */
363*3d8817e4Smiod 	 0,			/* bitpos */
364*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
365*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
366*3d8817e4Smiod 	 "R_MMIX_PC_24",	/* name */
367*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
368*3d8817e4Smiod 	 ~0xffffff,		/* src_mask */
369*3d8817e4Smiod 	 0xffffff,		/* dst_mask */
370*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
371*3d8817e4Smiod 
372*3d8817e4Smiod   /* A 32 bit absolute PC-relative relocation.  */
373*3d8817e4Smiod   HOWTO (R_MMIX_PC_32,		/* type */
374*3d8817e4Smiod 	 0,			/* rightshift */
375*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
376*3d8817e4Smiod 	 32,			/* bitsize */
377*3d8817e4Smiod 	 TRUE,			/* pc_relative */
378*3d8817e4Smiod 	 0,			/* bitpos */
379*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
380*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
381*3d8817e4Smiod 	 "R_MMIX_PC_32",	/* name */
382*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
383*3d8817e4Smiod 	 0,			/* src_mask */
384*3d8817e4Smiod 	 0xffffffff,		/* dst_mask */
385*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
386*3d8817e4Smiod 
387*3d8817e4Smiod   /* 64 bit PC-relative relocation.  */
388*3d8817e4Smiod   HOWTO (R_MMIX_PC_64,		/* type */
389*3d8817e4Smiod 	 0,			/* rightshift */
390*3d8817e4Smiod 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
391*3d8817e4Smiod 	 64,			/* bitsize */
392*3d8817e4Smiod 	 TRUE,			/* pc_relative */
393*3d8817e4Smiod 	 0,			/* bitpos */
394*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
395*3d8817e4Smiod 	 bfd_elf_generic_reloc,	/* special_function */
396*3d8817e4Smiod 	 "R_MMIX_PC_64",	/* name */
397*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
398*3d8817e4Smiod 	 0,			/* src_mask */
399*3d8817e4Smiod 	 MINUS_ONE,		/* dst_mask */
400*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
401*3d8817e4Smiod 
402*3d8817e4Smiod   /* GNU extension to record C++ vtable hierarchy.  */
403*3d8817e4Smiod   HOWTO (R_MMIX_GNU_VTINHERIT, /* type */
404*3d8817e4Smiod 	 0,			/* rightshift */
405*3d8817e4Smiod 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
406*3d8817e4Smiod 	 0,			/* bitsize */
407*3d8817e4Smiod 	 FALSE,			/* pc_relative */
408*3d8817e4Smiod 	 0,			/* bitpos */
409*3d8817e4Smiod 	 complain_overflow_dont, /* complain_on_overflow */
410*3d8817e4Smiod 	 NULL,			/* special_function */
411*3d8817e4Smiod 	 "R_MMIX_GNU_VTINHERIT", /* name */
412*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
413*3d8817e4Smiod 	 0,			/* src_mask */
414*3d8817e4Smiod 	 0,			/* dst_mask */
415*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
416*3d8817e4Smiod 
417*3d8817e4Smiod   /* GNU extension to record C++ vtable member usage.  */
418*3d8817e4Smiod   HOWTO (R_MMIX_GNU_VTENTRY,	/* type */
419*3d8817e4Smiod 	 0,			/* rightshift */
420*3d8817e4Smiod 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
421*3d8817e4Smiod 	 0,			/* bitsize */
422*3d8817e4Smiod 	 FALSE,			/* pc_relative */
423*3d8817e4Smiod 	 0,			/* bitpos */
424*3d8817e4Smiod 	 complain_overflow_dont, /* complain_on_overflow */
425*3d8817e4Smiod 	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
426*3d8817e4Smiod 	 "R_MMIX_GNU_VTENTRY", /* name */
427*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
428*3d8817e4Smiod 	 0,			/* src_mask */
429*3d8817e4Smiod 	 0,			/* dst_mask */
430*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
431*3d8817e4Smiod 
432*3d8817e4Smiod   /* The GETA relocation is supposed to get any address that could
433*3d8817e4Smiod      possibly be reached by the GETA instruction.  It can silently expand
434*3d8817e4Smiod      to get a 64-bit operand, but will complain if any of the two least
435*3d8817e4Smiod      significant bits are set.  The howto members reflect a simple GETA.  */
436*3d8817e4Smiod   HOWTO (R_MMIX_GETA,		/* type */
437*3d8817e4Smiod 	 2,			/* rightshift */
438*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
439*3d8817e4Smiod 	 19,			/* bitsize */
440*3d8817e4Smiod 	 TRUE,			/* pc_relative */
441*3d8817e4Smiod 	 0,			/* bitpos */
442*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
443*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
444*3d8817e4Smiod 	 "R_MMIX_GETA",		/* name */
445*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
446*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
447*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
448*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
449*3d8817e4Smiod 
450*3d8817e4Smiod   HOWTO (R_MMIX_GETA_1,		/* type */
451*3d8817e4Smiod 	 2,			/* rightshift */
452*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
453*3d8817e4Smiod 	 19,			/* bitsize */
454*3d8817e4Smiod 	 TRUE,			/* pc_relative */
455*3d8817e4Smiod 	 0,			/* bitpos */
456*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
457*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
458*3d8817e4Smiod 	 "R_MMIX_GETA_1",		/* name */
459*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
460*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
461*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
462*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
463*3d8817e4Smiod 
464*3d8817e4Smiod   HOWTO (R_MMIX_GETA_2,		/* type */
465*3d8817e4Smiod 	 2,			/* rightshift */
466*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
467*3d8817e4Smiod 	 19,			/* bitsize */
468*3d8817e4Smiod 	 TRUE,			/* pc_relative */
469*3d8817e4Smiod 	 0,			/* bitpos */
470*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
471*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
472*3d8817e4Smiod 	 "R_MMIX_GETA_2",		/* name */
473*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
474*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
475*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
476*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
477*3d8817e4Smiod 
478*3d8817e4Smiod   HOWTO (R_MMIX_GETA_3,		/* type */
479*3d8817e4Smiod 	 2,			/* rightshift */
480*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
481*3d8817e4Smiod 	 19,			/* bitsize */
482*3d8817e4Smiod 	 TRUE,			/* pc_relative */
483*3d8817e4Smiod 	 0,			/* bitpos */
484*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
485*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
486*3d8817e4Smiod 	 "R_MMIX_GETA_3",		/* name */
487*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
488*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
489*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
490*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
491*3d8817e4Smiod 
492*3d8817e4Smiod   /* The conditional branches are supposed to reach any (code) address.
493*3d8817e4Smiod      It can silently expand to a 64-bit operand, but will emit an error if
494*3d8817e4Smiod      any of the two least significant bits are set.  The howto members
495*3d8817e4Smiod      reflect a simple branch.  */
496*3d8817e4Smiod   HOWTO (R_MMIX_CBRANCH,	/* type */
497*3d8817e4Smiod 	 2,			/* rightshift */
498*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
499*3d8817e4Smiod 	 19,			/* bitsize */
500*3d8817e4Smiod 	 TRUE,			/* pc_relative */
501*3d8817e4Smiod 	 0,			/* bitpos */
502*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
503*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
504*3d8817e4Smiod 	 "R_MMIX_CBRANCH",	/* name */
505*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
506*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
507*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
508*3d8817e4Smiod 	 TRUE),		       	/* pcrel_offset */
509*3d8817e4Smiod 
510*3d8817e4Smiod   HOWTO (R_MMIX_CBRANCH_J,	/* type */
511*3d8817e4Smiod 	 2,			/* rightshift */
512*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
513*3d8817e4Smiod 	 19,			/* bitsize */
514*3d8817e4Smiod 	 TRUE,			/* pc_relative */
515*3d8817e4Smiod 	 0,			/* bitpos */
516*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
517*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
518*3d8817e4Smiod 	 "R_MMIX_CBRANCH_J",	/* name */
519*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
520*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
521*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
522*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
523*3d8817e4Smiod 
524*3d8817e4Smiod   HOWTO (R_MMIX_CBRANCH_1,	/* type */
525*3d8817e4Smiod 	 2,			/* rightshift */
526*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
527*3d8817e4Smiod 	 19,			/* bitsize */
528*3d8817e4Smiod 	 TRUE,			/* pc_relative */
529*3d8817e4Smiod 	 0,			/* bitpos */
530*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
531*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
532*3d8817e4Smiod 	 "R_MMIX_CBRANCH_1",	/* name */
533*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
534*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
535*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
536*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
537*3d8817e4Smiod 
538*3d8817e4Smiod   HOWTO (R_MMIX_CBRANCH_2,	/* type */
539*3d8817e4Smiod 	 2,			/* rightshift */
540*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
541*3d8817e4Smiod 	 19,			/* bitsize */
542*3d8817e4Smiod 	 TRUE,			/* pc_relative */
543*3d8817e4Smiod 	 0,			/* bitpos */
544*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
545*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
546*3d8817e4Smiod 	 "R_MMIX_CBRANCH_2",	/* name */
547*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
548*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
549*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
550*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
551*3d8817e4Smiod 
552*3d8817e4Smiod   HOWTO (R_MMIX_CBRANCH_3,	/* type */
553*3d8817e4Smiod 	 2,			/* rightshift */
554*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
555*3d8817e4Smiod 	 19,			/* bitsize */
556*3d8817e4Smiod 	 TRUE,			/* pc_relative */
557*3d8817e4Smiod 	 0,			/* bitpos */
558*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
559*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
560*3d8817e4Smiod 	 "R_MMIX_CBRANCH_3",	/* name */
561*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
562*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
563*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
564*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
565*3d8817e4Smiod 
566*3d8817e4Smiod   /* The PUSHJ instruction can reach any (code) address, as long as it's
567*3d8817e4Smiod      the beginning of a function (no usable restriction).  It can silently
568*3d8817e4Smiod      expand to a 64-bit operand, but will emit an error if any of the two
569*3d8817e4Smiod      least significant bits are set.  It can also expand into a call to a
570*3d8817e4Smiod      stub; see R_MMIX_PUSHJ_STUBBABLE.  The howto members reflect a simple
571*3d8817e4Smiod      PUSHJ.  */
572*3d8817e4Smiod   HOWTO (R_MMIX_PUSHJ,		/* type */
573*3d8817e4Smiod 	 2,			/* rightshift */
574*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
575*3d8817e4Smiod 	 19,			/* bitsize */
576*3d8817e4Smiod 	 TRUE,			/* pc_relative */
577*3d8817e4Smiod 	 0,			/* bitpos */
578*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
579*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
580*3d8817e4Smiod 	 "R_MMIX_PUSHJ",	/* name */
581*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
582*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
583*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
584*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
585*3d8817e4Smiod 
586*3d8817e4Smiod   HOWTO (R_MMIX_PUSHJ_1,	/* type */
587*3d8817e4Smiod 	 2,			/* rightshift */
588*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
589*3d8817e4Smiod 	 19,			/* bitsize */
590*3d8817e4Smiod 	 TRUE,			/* pc_relative */
591*3d8817e4Smiod 	 0,			/* bitpos */
592*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
593*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
594*3d8817e4Smiod 	 "R_MMIX_PUSHJ_1",	/* name */
595*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
596*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
597*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
598*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
599*3d8817e4Smiod 
600*3d8817e4Smiod   HOWTO (R_MMIX_PUSHJ_2,	/* type */
601*3d8817e4Smiod 	 2,			/* rightshift */
602*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
603*3d8817e4Smiod 	 19,			/* bitsize */
604*3d8817e4Smiod 	 TRUE,			/* pc_relative */
605*3d8817e4Smiod 	 0,			/* bitpos */
606*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
607*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
608*3d8817e4Smiod 	 "R_MMIX_PUSHJ_2",	/* name */
609*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
610*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
611*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
612*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
613*3d8817e4Smiod 
614*3d8817e4Smiod   HOWTO (R_MMIX_PUSHJ_3,	/* type */
615*3d8817e4Smiod 	 2,			/* rightshift */
616*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
617*3d8817e4Smiod 	 19,			/* bitsize */
618*3d8817e4Smiod 	 TRUE,			/* pc_relative */
619*3d8817e4Smiod 	 0,			/* bitpos */
620*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
621*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
622*3d8817e4Smiod 	 "R_MMIX_PUSHJ_3",	/* name */
623*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
624*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
625*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
626*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
627*3d8817e4Smiod 
628*3d8817e4Smiod   /* A JMP is supposed to reach any (code) address.  By itself, it can
629*3d8817e4Smiod      reach +-64M; the expansion can reach all 64 bits.  Note that the 64M
630*3d8817e4Smiod      limit is soon reached if you link the program in wildly different
631*3d8817e4Smiod      memory segments.  The howto members reflect a trivial JMP.  */
632*3d8817e4Smiod   HOWTO (R_MMIX_JMP,		/* type */
633*3d8817e4Smiod 	 2,			/* rightshift */
634*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
635*3d8817e4Smiod 	 27,			/* bitsize */
636*3d8817e4Smiod 	 TRUE,			/* pc_relative */
637*3d8817e4Smiod 	 0,			/* bitpos */
638*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
639*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
640*3d8817e4Smiod 	 "R_MMIX_JMP",		/* name */
641*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
642*3d8817e4Smiod 	 ~0x1ffffff,		/* src_mask */
643*3d8817e4Smiod 	 0x1ffffff,		/* dst_mask */
644*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
645*3d8817e4Smiod 
646*3d8817e4Smiod   HOWTO (R_MMIX_JMP_1,		/* type */
647*3d8817e4Smiod 	 2,			/* rightshift */
648*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
649*3d8817e4Smiod 	 27,			/* bitsize */
650*3d8817e4Smiod 	 TRUE,			/* pc_relative */
651*3d8817e4Smiod 	 0,			/* bitpos */
652*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
653*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
654*3d8817e4Smiod 	 "R_MMIX_JMP_1",	/* name */
655*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
656*3d8817e4Smiod 	 ~0x1ffffff,		/* src_mask */
657*3d8817e4Smiod 	 0x1ffffff,		/* dst_mask */
658*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
659*3d8817e4Smiod 
660*3d8817e4Smiod   HOWTO (R_MMIX_JMP_2,		/* type */
661*3d8817e4Smiod 	 2,			/* rightshift */
662*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
663*3d8817e4Smiod 	 27,			/* bitsize */
664*3d8817e4Smiod 	 TRUE,			/* pc_relative */
665*3d8817e4Smiod 	 0,			/* bitpos */
666*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
667*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
668*3d8817e4Smiod 	 "R_MMIX_JMP_2",	/* name */
669*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
670*3d8817e4Smiod 	 ~0x1ffffff,		/* src_mask */
671*3d8817e4Smiod 	 0x1ffffff,		/* dst_mask */
672*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
673*3d8817e4Smiod 
674*3d8817e4Smiod   HOWTO (R_MMIX_JMP_3,		/* type */
675*3d8817e4Smiod 	 2,			/* rightshift */
676*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
677*3d8817e4Smiod 	 27,			/* bitsize */
678*3d8817e4Smiod 	 TRUE,			/* pc_relative */
679*3d8817e4Smiod 	 0,			/* bitpos */
680*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
681*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
682*3d8817e4Smiod 	 "R_MMIX_JMP_3",	/* name */
683*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
684*3d8817e4Smiod 	 ~0x1ffffff,		/* src_mask */
685*3d8817e4Smiod 	 0x1ffffff,		/* dst_mask */
686*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
687*3d8817e4Smiod 
688*3d8817e4Smiod   /* When we don't emit link-time-relaxable code from the assembler, or
689*3d8817e4Smiod      when relaxation has done all it can do, these relocs are used.  For
690*3d8817e4Smiod      GETA/PUSHJ/branches.  */
691*3d8817e4Smiod   HOWTO (R_MMIX_ADDR19,		/* type */
692*3d8817e4Smiod 	 2,			/* rightshift */
693*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
694*3d8817e4Smiod 	 19,			/* bitsize */
695*3d8817e4Smiod 	 TRUE,			/* pc_relative */
696*3d8817e4Smiod 	 0,			/* bitpos */
697*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
698*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
699*3d8817e4Smiod 	 "R_MMIX_ADDR19",	/* name */
700*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
701*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
702*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
703*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
704*3d8817e4Smiod 
705*3d8817e4Smiod   /* For JMP.  */
706*3d8817e4Smiod   HOWTO (R_MMIX_ADDR27,		/* type */
707*3d8817e4Smiod 	 2,			/* rightshift */
708*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
709*3d8817e4Smiod 	 27,			/* bitsize */
710*3d8817e4Smiod 	 TRUE,			/* pc_relative */
711*3d8817e4Smiod 	 0,			/* bitpos */
712*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
713*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
714*3d8817e4Smiod 	 "R_MMIX_ADDR27",	/* name */
715*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
716*3d8817e4Smiod 	 ~0x1ffffff,		/* src_mask */
717*3d8817e4Smiod 	 0x1ffffff,		/* dst_mask */
718*3d8817e4Smiod 	 TRUE),			/* pcrel_offset */
719*3d8817e4Smiod 
720*3d8817e4Smiod   /* A general register or the value 0..255.  If a value, then the
721*3d8817e4Smiod      instruction (offset -3) needs adjusting.  */
722*3d8817e4Smiod   HOWTO (R_MMIX_REG_OR_BYTE,	/* type */
723*3d8817e4Smiod 	 0,			/* rightshift */
724*3d8817e4Smiod 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
725*3d8817e4Smiod 	 8,			/* bitsize */
726*3d8817e4Smiod 	 FALSE,			/* pc_relative */
727*3d8817e4Smiod 	 0,			/* bitpos */
728*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
729*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
730*3d8817e4Smiod 	 "R_MMIX_REG_OR_BYTE",	/* name */
731*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
732*3d8817e4Smiod 	 0,			/* src_mask */
733*3d8817e4Smiod 	 0xff,			/* dst_mask */
734*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
735*3d8817e4Smiod 
736*3d8817e4Smiod   /* A general register.  */
737*3d8817e4Smiod   HOWTO (R_MMIX_REG,		/* type */
738*3d8817e4Smiod 	 0,			/* rightshift */
739*3d8817e4Smiod 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
740*3d8817e4Smiod 	 8,			/* bitsize */
741*3d8817e4Smiod 	 FALSE,			/* pc_relative */
742*3d8817e4Smiod 	 0,			/* bitpos */
743*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
744*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
745*3d8817e4Smiod 	 "R_MMIX_REG",		/* name */
746*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
747*3d8817e4Smiod 	 0,			/* src_mask */
748*3d8817e4Smiod 	 0xff,			/* dst_mask */
749*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
750*3d8817e4Smiod 
751*3d8817e4Smiod   /* A register plus an index, corresponding to the relocation expression.
752*3d8817e4Smiod      The sizes must correspond to the valid range of the expression, while
753*3d8817e4Smiod      the bitmasks correspond to what we store in the image.  */
754*3d8817e4Smiod   HOWTO (R_MMIX_BASE_PLUS_OFFSET,	/* type */
755*3d8817e4Smiod 	 0,			/* rightshift */
756*3d8817e4Smiod 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
757*3d8817e4Smiod 	 64,			/* bitsize */
758*3d8817e4Smiod 	 FALSE,			/* pc_relative */
759*3d8817e4Smiod 	 0,			/* bitpos */
760*3d8817e4Smiod 	 complain_overflow_bitfield, /* complain_on_overflow */
761*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
762*3d8817e4Smiod 	 "R_MMIX_BASE_PLUS_OFFSET", /* name */
763*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
764*3d8817e4Smiod 	 0,			/* src_mask */
765*3d8817e4Smiod 	 0xffff,		/* dst_mask */
766*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
767*3d8817e4Smiod 
768*3d8817e4Smiod   /* A "magic" relocation for a LOCAL expression, asserting that the
769*3d8817e4Smiod      expression is less than the number of global registers.  No actual
770*3d8817e4Smiod      modification of the contents is done.  Implementing this as a
771*3d8817e4Smiod      relocation was less intrusive than e.g. putting such expressions in a
772*3d8817e4Smiod      section to discard *after* relocation.  */
773*3d8817e4Smiod   HOWTO (R_MMIX_LOCAL,		/* type */
774*3d8817e4Smiod 	 0,			/* rightshift */
775*3d8817e4Smiod 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
776*3d8817e4Smiod 	 0,			/* bitsize */
777*3d8817e4Smiod 	 FALSE,			/* pc_relative */
778*3d8817e4Smiod 	 0,			/* bitpos */
779*3d8817e4Smiod 	 complain_overflow_dont, /* complain_on_overflow */
780*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
781*3d8817e4Smiod 	 "R_MMIX_LOCAL",	/* name */
782*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
783*3d8817e4Smiod 	 0,			/* src_mask */
784*3d8817e4Smiod 	 0,			/* dst_mask */
785*3d8817e4Smiod 	 FALSE),		/* pcrel_offset */
786*3d8817e4Smiod 
787*3d8817e4Smiod   HOWTO (R_MMIX_PUSHJ_STUBBABLE, /* type */
788*3d8817e4Smiod 	 2,			/* rightshift */
789*3d8817e4Smiod 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
790*3d8817e4Smiod 	 19,			/* bitsize */
791*3d8817e4Smiod 	 TRUE,			/* pc_relative */
792*3d8817e4Smiod 	 0,			/* bitpos */
793*3d8817e4Smiod 	 complain_overflow_signed, /* complain_on_overflow */
794*3d8817e4Smiod 	 mmix_elf_reloc,	/* special_function */
795*3d8817e4Smiod 	 "R_MMIX_PUSHJ_STUBBABLE", /* name */
796*3d8817e4Smiod 	 FALSE,			/* partial_inplace */
797*3d8817e4Smiod 	 ~0x0100ffff,		/* src_mask */
798*3d8817e4Smiod 	 0x0100ffff,		/* dst_mask */
799*3d8817e4Smiod 	 TRUE)			/* pcrel_offset */
800*3d8817e4Smiod  };
801*3d8817e4Smiod 
802*3d8817e4Smiod 
803*3d8817e4Smiod /* Map BFD reloc types to MMIX ELF reloc types.  */
804*3d8817e4Smiod 
805*3d8817e4Smiod struct mmix_reloc_map
806*3d8817e4Smiod   {
807*3d8817e4Smiod     bfd_reloc_code_real_type bfd_reloc_val;
808*3d8817e4Smiod     enum elf_mmix_reloc_type elf_reloc_val;
809*3d8817e4Smiod   };
810*3d8817e4Smiod 
811*3d8817e4Smiod 
812*3d8817e4Smiod static const struct mmix_reloc_map mmix_reloc_map[] =
813*3d8817e4Smiod   {
814*3d8817e4Smiod     {BFD_RELOC_NONE, R_MMIX_NONE},
815*3d8817e4Smiod     {BFD_RELOC_8, R_MMIX_8},
816*3d8817e4Smiod     {BFD_RELOC_16, R_MMIX_16},
817*3d8817e4Smiod     {BFD_RELOC_24, R_MMIX_24},
818*3d8817e4Smiod     {BFD_RELOC_32, R_MMIX_32},
819*3d8817e4Smiod     {BFD_RELOC_64, R_MMIX_64},
820*3d8817e4Smiod     {BFD_RELOC_8_PCREL, R_MMIX_PC_8},
821*3d8817e4Smiod     {BFD_RELOC_16_PCREL, R_MMIX_PC_16},
822*3d8817e4Smiod     {BFD_RELOC_24_PCREL, R_MMIX_PC_24},
823*3d8817e4Smiod     {BFD_RELOC_32_PCREL, R_MMIX_PC_32},
824*3d8817e4Smiod     {BFD_RELOC_64_PCREL, R_MMIX_PC_64},
825*3d8817e4Smiod     {BFD_RELOC_VTABLE_INHERIT, R_MMIX_GNU_VTINHERIT},
826*3d8817e4Smiod     {BFD_RELOC_VTABLE_ENTRY, R_MMIX_GNU_VTENTRY},
827*3d8817e4Smiod     {BFD_RELOC_MMIX_GETA, R_MMIX_GETA},
828*3d8817e4Smiod     {BFD_RELOC_MMIX_CBRANCH, R_MMIX_CBRANCH},
829*3d8817e4Smiod     {BFD_RELOC_MMIX_PUSHJ, R_MMIX_PUSHJ},
830*3d8817e4Smiod     {BFD_RELOC_MMIX_JMP, R_MMIX_JMP},
831*3d8817e4Smiod     {BFD_RELOC_MMIX_ADDR19, R_MMIX_ADDR19},
832*3d8817e4Smiod     {BFD_RELOC_MMIX_ADDR27, R_MMIX_ADDR27},
833*3d8817e4Smiod     {BFD_RELOC_MMIX_REG_OR_BYTE, R_MMIX_REG_OR_BYTE},
834*3d8817e4Smiod     {BFD_RELOC_MMIX_REG, R_MMIX_REG},
835*3d8817e4Smiod     {BFD_RELOC_MMIX_BASE_PLUS_OFFSET, R_MMIX_BASE_PLUS_OFFSET},
836*3d8817e4Smiod     {BFD_RELOC_MMIX_LOCAL, R_MMIX_LOCAL},
837*3d8817e4Smiod     {BFD_RELOC_MMIX_PUSHJ_STUBBABLE, R_MMIX_PUSHJ_STUBBABLE}
838*3d8817e4Smiod   };
839*3d8817e4Smiod 
840*3d8817e4Smiod static reloc_howto_type *
bfd_elf64_bfd_reloc_type_lookup(abfd,code)841*3d8817e4Smiod bfd_elf64_bfd_reloc_type_lookup (abfd, code)
842*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
843*3d8817e4Smiod      bfd_reloc_code_real_type code;
844*3d8817e4Smiod {
845*3d8817e4Smiod   unsigned int i;
846*3d8817e4Smiod 
847*3d8817e4Smiod   for (i = 0;
848*3d8817e4Smiod        i < sizeof (mmix_reloc_map) / sizeof (mmix_reloc_map[0]);
849*3d8817e4Smiod        i++)
850*3d8817e4Smiod     {
851*3d8817e4Smiod       if (mmix_reloc_map[i].bfd_reloc_val == code)
852*3d8817e4Smiod 	return &elf_mmix_howto_table[mmix_reloc_map[i].elf_reloc_val];
853*3d8817e4Smiod     }
854*3d8817e4Smiod 
855*3d8817e4Smiod   return NULL;
856*3d8817e4Smiod }
857*3d8817e4Smiod 
858*3d8817e4Smiod static bfd_boolean
mmix_elf_new_section_hook(abfd,sec)859*3d8817e4Smiod mmix_elf_new_section_hook (abfd, sec)
860*3d8817e4Smiod      bfd *abfd;
861*3d8817e4Smiod      asection *sec;
862*3d8817e4Smiod {
863*3d8817e4Smiod   struct _mmix_elf_section_data *sdata;
864*3d8817e4Smiod   bfd_size_type amt = sizeof (*sdata);
865*3d8817e4Smiod 
866*3d8817e4Smiod   sdata = (struct _mmix_elf_section_data *) bfd_zalloc (abfd, amt);
867*3d8817e4Smiod   if (sdata == NULL)
868*3d8817e4Smiod     return FALSE;
869*3d8817e4Smiod   sec->used_by_bfd = (PTR) sdata;
870*3d8817e4Smiod 
871*3d8817e4Smiod   return _bfd_elf_new_section_hook (abfd, sec);
872*3d8817e4Smiod }
873*3d8817e4Smiod 
874*3d8817e4Smiod 
875*3d8817e4Smiod /* This function performs the actual bitfiddling and sanity check for a
876*3d8817e4Smiod    final relocation.  Each relocation gets its *worst*-case expansion
877*3d8817e4Smiod    in size when it arrives here; any reduction in size should have been
878*3d8817e4Smiod    caught in linker relaxation earlier.  When we get here, the relocation
879*3d8817e4Smiod    looks like the smallest instruction with SWYM:s (nop:s) appended to the
880*3d8817e4Smiod    max size.  We fill in those nop:s.
881*3d8817e4Smiod 
882*3d8817e4Smiod    R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
883*3d8817e4Smiod     GETA $N,foo
884*3d8817e4Smiod    ->
885*3d8817e4Smiod     SETL $N,foo & 0xffff
886*3d8817e4Smiod     INCML $N,(foo >> 16) & 0xffff
887*3d8817e4Smiod     INCMH $N,(foo >> 32) & 0xffff
888*3d8817e4Smiod     INCH $N,(foo >> 48) & 0xffff
889*3d8817e4Smiod 
890*3d8817e4Smiod    R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
891*3d8817e4Smiod    condbranches needing relaxation might be rare enough to not be
892*3d8817e4Smiod    worthwhile.)
893*3d8817e4Smiod     [P]Bcc $N,foo
894*3d8817e4Smiod    ->
895*3d8817e4Smiod     [~P]B~cc $N,.+20
896*3d8817e4Smiod     SETL $255,foo & ...
897*3d8817e4Smiod     INCML ...
898*3d8817e4Smiod     INCMH ...
899*3d8817e4Smiod     INCH ...
900*3d8817e4Smiod     GO $255,$255,0
901*3d8817e4Smiod 
902*3d8817e4Smiod    R_MMIX_PUSHJ: (FIXME: Relaxation...)
903*3d8817e4Smiod     PUSHJ $N,foo
904*3d8817e4Smiod    ->
905*3d8817e4Smiod     SETL $255,foo & ...
906*3d8817e4Smiod     INCML ...
907*3d8817e4Smiod     INCMH ...
908*3d8817e4Smiod     INCH ...
909*3d8817e4Smiod     PUSHGO $N,$255,0
910*3d8817e4Smiod 
911*3d8817e4Smiod    R_MMIX_JMP: (FIXME: Relaxation...)
912*3d8817e4Smiod     JMP foo
913*3d8817e4Smiod    ->
914*3d8817e4Smiod     SETL $255,foo & ...
915*3d8817e4Smiod     INCML ...
916*3d8817e4Smiod     INCMH ...
917*3d8817e4Smiod     INCH ...
918*3d8817e4Smiod     GO $255,$255,0
919*3d8817e4Smiod 
920*3d8817e4Smiod    R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in.  */
921*3d8817e4Smiod 
922*3d8817e4Smiod static bfd_reloc_status_type
mmix_elf_perform_relocation(isec,howto,datap,addr,value)923*3d8817e4Smiod mmix_elf_perform_relocation (isec, howto, datap, addr, value)
924*3d8817e4Smiod      asection *isec;
925*3d8817e4Smiod      reloc_howto_type *howto;
926*3d8817e4Smiod      PTR datap;
927*3d8817e4Smiod      bfd_vma addr;
928*3d8817e4Smiod      bfd_vma value;
929*3d8817e4Smiod {
930*3d8817e4Smiod   bfd *abfd = isec->owner;
931*3d8817e4Smiod   bfd_reloc_status_type flag = bfd_reloc_ok;
932*3d8817e4Smiod   bfd_reloc_status_type r;
933*3d8817e4Smiod   int offs = 0;
934*3d8817e4Smiod   int reg = 255;
935*3d8817e4Smiod 
936*3d8817e4Smiod   /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
937*3d8817e4Smiod      We handle the differences here and the common sequence later.  */
938*3d8817e4Smiod   switch (howto->type)
939*3d8817e4Smiod     {
940*3d8817e4Smiod     case R_MMIX_GETA:
941*3d8817e4Smiod       offs = 0;
942*3d8817e4Smiod       reg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
943*3d8817e4Smiod 
944*3d8817e4Smiod       /* We change to an absolute value.  */
945*3d8817e4Smiod       value += addr;
946*3d8817e4Smiod       break;
947*3d8817e4Smiod 
948*3d8817e4Smiod     case R_MMIX_CBRANCH:
949*3d8817e4Smiod       {
950*3d8817e4Smiod 	int in1 = bfd_get_16 (abfd, (bfd_byte *) datap) << 16;
951*3d8817e4Smiod 
952*3d8817e4Smiod 	/* Invert the condition and prediction bit, and set the offset
953*3d8817e4Smiod 	   to five instructions ahead.
954*3d8817e4Smiod 
955*3d8817e4Smiod 	   We *can* do better if we want to.  If the branch is found to be
956*3d8817e4Smiod 	   within limits, we could leave the branch as is; there'll just
957*3d8817e4Smiod 	   be a bunch of NOP:s after it.  But we shouldn't see this
958*3d8817e4Smiod 	   sequence often enough that it's worth doing it.  */
959*3d8817e4Smiod 
960*3d8817e4Smiod 	bfd_put_32 (abfd,
961*3d8817e4Smiod 		    (((in1 ^ ((PRED_INV_BIT | COND_INV_BIT) << 24)) & ~0xffff)
962*3d8817e4Smiod 		     | (24/4)),
963*3d8817e4Smiod 		    (bfd_byte *) datap);
964*3d8817e4Smiod 
965*3d8817e4Smiod 	/* Put a "GO $255,$255,0" after the common sequence.  */
966*3d8817e4Smiod 	bfd_put_32 (abfd,
967*3d8817e4Smiod 		    ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24) | 0xffff00,
968*3d8817e4Smiod 		    (bfd_byte *) datap + 20);
969*3d8817e4Smiod 
970*3d8817e4Smiod 	/* Common sequence starts at offset 4.  */
971*3d8817e4Smiod 	offs = 4;
972*3d8817e4Smiod 
973*3d8817e4Smiod 	/* We change to an absolute value.  */
974*3d8817e4Smiod 	value += addr;
975*3d8817e4Smiod       }
976*3d8817e4Smiod       break;
977*3d8817e4Smiod 
978*3d8817e4Smiod     case R_MMIX_PUSHJ_STUBBABLE:
979*3d8817e4Smiod       /* If the address fits, we're fine.  */
980*3d8817e4Smiod       if ((value & 3) == 0
981*3d8817e4Smiod 	  /* Note rightshift 0; see R_MMIX_JMP case below.  */
982*3d8817e4Smiod 	  && (r = bfd_check_overflow (complain_overflow_signed,
983*3d8817e4Smiod 				      howto->bitsize,
984*3d8817e4Smiod 				      0,
985*3d8817e4Smiod 				      bfd_arch_bits_per_address (abfd),
986*3d8817e4Smiod 				      value)) == bfd_reloc_ok)
987*3d8817e4Smiod 	goto pcrel_mmix_reloc_fits;
988*3d8817e4Smiod       else
989*3d8817e4Smiod 	{
990*3d8817e4Smiod 	  bfd_size_type size = isec->rawsize ? isec->rawsize : isec->size;
991*3d8817e4Smiod 
992*3d8817e4Smiod 	  /* We have the bytes at the PUSHJ insn and need to get the
993*3d8817e4Smiod 	     position for the stub.  There's supposed to be room allocated
994*3d8817e4Smiod 	     for the stub.  */
995*3d8817e4Smiod 	  bfd_byte *stubcontents
996*3d8817e4Smiod 	    = ((bfd_byte *) datap
997*3d8817e4Smiod 	       - (addr - (isec->output_section->vma + isec->output_offset))
998*3d8817e4Smiod 	       + size
999*3d8817e4Smiod 	       + mmix_elf_section_data (isec)->pjs.stub_offset);
1000*3d8817e4Smiod 	  bfd_vma stubaddr;
1001*3d8817e4Smiod 
1002*3d8817e4Smiod 	  /* The address doesn't fit, so redirect the PUSHJ to the
1003*3d8817e4Smiod 	     location of the stub.  */
1004*3d8817e4Smiod 	  r = mmix_elf_perform_relocation (isec,
1005*3d8817e4Smiod 					   &elf_mmix_howto_table
1006*3d8817e4Smiod 					   [R_MMIX_ADDR19],
1007*3d8817e4Smiod 					   datap,
1008*3d8817e4Smiod 					   addr,
1009*3d8817e4Smiod 					   isec->output_section->vma
1010*3d8817e4Smiod 					   + isec->output_offset
1011*3d8817e4Smiod 					   + size
1012*3d8817e4Smiod 					   + (mmix_elf_section_data (isec)
1013*3d8817e4Smiod 					      ->pjs.stub_offset)
1014*3d8817e4Smiod 					   - addr);
1015*3d8817e4Smiod 	  if (r != bfd_reloc_ok)
1016*3d8817e4Smiod 	    return r;
1017*3d8817e4Smiod 
1018*3d8817e4Smiod 	  stubaddr
1019*3d8817e4Smiod 	    = (isec->output_section->vma
1020*3d8817e4Smiod 	       + isec->output_offset
1021*3d8817e4Smiod 	       + size
1022*3d8817e4Smiod 	       + mmix_elf_section_data (isec)->pjs.stub_offset);
1023*3d8817e4Smiod 
1024*3d8817e4Smiod 	  /* We generate a simple JMP if that suffices, else the whole 5
1025*3d8817e4Smiod 	     insn stub.  */
1026*3d8817e4Smiod 	  if (bfd_check_overflow (complain_overflow_signed,
1027*3d8817e4Smiod 				  elf_mmix_howto_table[R_MMIX_ADDR27].bitsize,
1028*3d8817e4Smiod 				  0,
1029*3d8817e4Smiod 				  bfd_arch_bits_per_address (abfd),
1030*3d8817e4Smiod 				  addr + value - stubaddr) == bfd_reloc_ok)
1031*3d8817e4Smiod 	    {
1032*3d8817e4Smiod 	      bfd_put_32 (abfd, JMP_INSN_BYTE << 24, stubcontents);
1033*3d8817e4Smiod 	      r = mmix_elf_perform_relocation (isec,
1034*3d8817e4Smiod 					       &elf_mmix_howto_table
1035*3d8817e4Smiod 					       [R_MMIX_ADDR27],
1036*3d8817e4Smiod 					       stubcontents,
1037*3d8817e4Smiod 					       stubaddr,
1038*3d8817e4Smiod 					       value + addr - stubaddr);
1039*3d8817e4Smiod 	      mmix_elf_section_data (isec)->pjs.stub_offset += 4;
1040*3d8817e4Smiod 
1041*3d8817e4Smiod 	      if (size + mmix_elf_section_data (isec)->pjs.stub_offset
1042*3d8817e4Smiod 		  > isec->size)
1043*3d8817e4Smiod 		abort ();
1044*3d8817e4Smiod 
1045*3d8817e4Smiod 	      return r;
1046*3d8817e4Smiod 	    }
1047*3d8817e4Smiod 	  else
1048*3d8817e4Smiod 	    {
1049*3d8817e4Smiod 	      /* Put a "GO $255,0" after the common sequence.  */
1050*3d8817e4Smiod 	      bfd_put_32 (abfd,
1051*3d8817e4Smiod 			  ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1052*3d8817e4Smiod 			  | 0xff00, (bfd_byte *) stubcontents + 16);
1053*3d8817e4Smiod 
1054*3d8817e4Smiod 	      /* Prepare for the general code to set the first part of the
1055*3d8817e4Smiod 		 linker stub, and */
1056*3d8817e4Smiod 	      value += addr;
1057*3d8817e4Smiod 	      datap = stubcontents;
1058*3d8817e4Smiod 	      mmix_elf_section_data (isec)->pjs.stub_offset
1059*3d8817e4Smiod 		+= MAX_PUSHJ_STUB_SIZE;
1060*3d8817e4Smiod 	    }
1061*3d8817e4Smiod 	}
1062*3d8817e4Smiod       break;
1063*3d8817e4Smiod 
1064*3d8817e4Smiod     case R_MMIX_PUSHJ:
1065*3d8817e4Smiod       {
1066*3d8817e4Smiod 	int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
1067*3d8817e4Smiod 
1068*3d8817e4Smiod 	/* Put a "PUSHGO $N,$255,0" after the common sequence.  */
1069*3d8817e4Smiod 	bfd_put_32 (abfd,
1070*3d8817e4Smiod 		    ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1071*3d8817e4Smiod 		    | (inreg << 16)
1072*3d8817e4Smiod 		    | 0xff00,
1073*3d8817e4Smiod 		    (bfd_byte *) datap + 16);
1074*3d8817e4Smiod 
1075*3d8817e4Smiod 	/* We change to an absolute value.  */
1076*3d8817e4Smiod 	value += addr;
1077*3d8817e4Smiod       }
1078*3d8817e4Smiod       break;
1079*3d8817e4Smiod 
1080*3d8817e4Smiod     case R_MMIX_JMP:
1081*3d8817e4Smiod       /* This one is a little special.  If we get here on a non-relaxing
1082*3d8817e4Smiod 	 link, and the destination is actually in range, we don't need to
1083*3d8817e4Smiod 	 execute the nops.
1084*3d8817e4Smiod 	 If so, we fall through to the bit-fiddling relocs.
1085*3d8817e4Smiod 
1086*3d8817e4Smiod 	 FIXME: bfd_check_overflow seems broken; the relocation is
1087*3d8817e4Smiod 	 rightshifted before testing, so supply a zero rightshift.  */
1088*3d8817e4Smiod 
1089*3d8817e4Smiod       if (! ((value & 3) == 0
1090*3d8817e4Smiod 	     && (r = bfd_check_overflow (complain_overflow_signed,
1091*3d8817e4Smiod 					 howto->bitsize,
1092*3d8817e4Smiod 					 0,
1093*3d8817e4Smiod 					 bfd_arch_bits_per_address (abfd),
1094*3d8817e4Smiod 					 value)) == bfd_reloc_ok))
1095*3d8817e4Smiod 	{
1096*3d8817e4Smiod 	  /* If the relocation doesn't fit in a JMP, we let the NOP:s be
1097*3d8817e4Smiod 	     modified below, and put a "GO $255,$255,0" after the
1098*3d8817e4Smiod 	     address-loading sequence.  */
1099*3d8817e4Smiod 	  bfd_put_32 (abfd,
1100*3d8817e4Smiod 		      ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1101*3d8817e4Smiod 		      | 0xffff00,
1102*3d8817e4Smiod 		      (bfd_byte *) datap + 16);
1103*3d8817e4Smiod 
1104*3d8817e4Smiod 	  /* We change to an absolute value.  */
1105*3d8817e4Smiod 	  value += addr;
1106*3d8817e4Smiod 	  break;
1107*3d8817e4Smiod 	}
1108*3d8817e4Smiod       /* FALLTHROUGH.  */
1109*3d8817e4Smiod     case R_MMIX_ADDR19:
1110*3d8817e4Smiod     case R_MMIX_ADDR27:
1111*3d8817e4Smiod     pcrel_mmix_reloc_fits:
1112*3d8817e4Smiod       /* These must be in range, or else we emit an error.  */
1113*3d8817e4Smiod       if ((value & 3) == 0
1114*3d8817e4Smiod 	  /* Note rightshift 0; see above.  */
1115*3d8817e4Smiod 	  && (r = bfd_check_overflow (complain_overflow_signed,
1116*3d8817e4Smiod 				      howto->bitsize,
1117*3d8817e4Smiod 				      0,
1118*3d8817e4Smiod 				      bfd_arch_bits_per_address (abfd),
1119*3d8817e4Smiod 				      value)) == bfd_reloc_ok)
1120*3d8817e4Smiod 	{
1121*3d8817e4Smiod 	  bfd_vma in1
1122*3d8817e4Smiod 	    = bfd_get_32 (abfd, (bfd_byte *) datap);
1123*3d8817e4Smiod 	  bfd_vma highbit;
1124*3d8817e4Smiod 
1125*3d8817e4Smiod 	  if ((bfd_signed_vma) value < 0)
1126*3d8817e4Smiod 	    {
1127*3d8817e4Smiod 	      highbit = 1 << 24;
1128*3d8817e4Smiod 	      value += (1 << (howto->bitsize - 1));
1129*3d8817e4Smiod 	    }
1130*3d8817e4Smiod 	  else
1131*3d8817e4Smiod 	    highbit = 0;
1132*3d8817e4Smiod 
1133*3d8817e4Smiod 	  value >>= 2;
1134*3d8817e4Smiod 
1135*3d8817e4Smiod 	  bfd_put_32 (abfd,
1136*3d8817e4Smiod 		      (in1 & howto->src_mask)
1137*3d8817e4Smiod 		      | highbit
1138*3d8817e4Smiod 		      | (value & howto->dst_mask),
1139*3d8817e4Smiod 		      (bfd_byte *) datap);
1140*3d8817e4Smiod 
1141*3d8817e4Smiod 	  return bfd_reloc_ok;
1142*3d8817e4Smiod 	}
1143*3d8817e4Smiod       else
1144*3d8817e4Smiod 	return bfd_reloc_overflow;
1145*3d8817e4Smiod 
1146*3d8817e4Smiod     case R_MMIX_BASE_PLUS_OFFSET:
1147*3d8817e4Smiod       {
1148*3d8817e4Smiod 	struct bpo_reloc_section_info *bpodata
1149*3d8817e4Smiod 	  = mmix_elf_section_data (isec)->bpo.reloc;
1150*3d8817e4Smiod 	asection *bpo_greg_section
1151*3d8817e4Smiod 	  = bpodata->bpo_greg_section;
1152*3d8817e4Smiod 	struct bpo_greg_section_info *gregdata
1153*3d8817e4Smiod 	  = mmix_elf_section_data (bpo_greg_section)->bpo.greg;
1154*3d8817e4Smiod 	size_t bpo_index
1155*3d8817e4Smiod 	  = gregdata->bpo_reloc_indexes[bpodata->bpo_index++];
1156*3d8817e4Smiod 
1157*3d8817e4Smiod 	/* A consistency check: The value we now have in "relocation" must
1158*3d8817e4Smiod 	   be the same as the value we stored for that relocation.  It
1159*3d8817e4Smiod 	   doesn't cost much, so can be left in at all times.  */
1160*3d8817e4Smiod 	if (value != gregdata->reloc_request[bpo_index].value)
1161*3d8817e4Smiod 	  {
1162*3d8817e4Smiod 	    (*_bfd_error_handler)
1163*3d8817e4Smiod 	      (_("%s: Internal inconsistency error for value for\n\
1164*3d8817e4Smiod  linker-allocated global register: linked: 0x%lx%08lx != relaxed: 0x%lx%08lx\n"),
1165*3d8817e4Smiod 	       bfd_get_filename (isec->owner),
1166*3d8817e4Smiod 	       (unsigned long) (value >> 32), (unsigned long) value,
1167*3d8817e4Smiod 	       (unsigned long) (gregdata->reloc_request[bpo_index].value
1168*3d8817e4Smiod 				>> 32),
1169*3d8817e4Smiod 	       (unsigned long) gregdata->reloc_request[bpo_index].value);
1170*3d8817e4Smiod 	    bfd_set_error (bfd_error_bad_value);
1171*3d8817e4Smiod 	    return bfd_reloc_overflow;
1172*3d8817e4Smiod 	  }
1173*3d8817e4Smiod 
1174*3d8817e4Smiod 	/* Then store the register number and offset for that register
1175*3d8817e4Smiod 	   into datap and datap + 1 respectively.  */
1176*3d8817e4Smiod 	bfd_put_8 (abfd,
1177*3d8817e4Smiod 		   gregdata->reloc_request[bpo_index].regindex
1178*3d8817e4Smiod 		   + bpo_greg_section->output_section->vma / 8,
1179*3d8817e4Smiod 		   datap);
1180*3d8817e4Smiod 	bfd_put_8 (abfd,
1181*3d8817e4Smiod 		   gregdata->reloc_request[bpo_index].offset,
1182*3d8817e4Smiod 		   ((unsigned char *) datap) + 1);
1183*3d8817e4Smiod 	return bfd_reloc_ok;
1184*3d8817e4Smiod       }
1185*3d8817e4Smiod 
1186*3d8817e4Smiod     case R_MMIX_REG_OR_BYTE:
1187*3d8817e4Smiod     case R_MMIX_REG:
1188*3d8817e4Smiod       if (value > 255)
1189*3d8817e4Smiod 	return bfd_reloc_overflow;
1190*3d8817e4Smiod       bfd_put_8 (abfd, value, datap);
1191*3d8817e4Smiod       return bfd_reloc_ok;
1192*3d8817e4Smiod 
1193*3d8817e4Smiod     default:
1194*3d8817e4Smiod       BAD_CASE (howto->type);
1195*3d8817e4Smiod     }
1196*3d8817e4Smiod 
1197*3d8817e4Smiod   /* This code adds the common SETL/INCML/INCMH/INCH worst-case
1198*3d8817e4Smiod      sequence.  */
1199*3d8817e4Smiod 
1200*3d8817e4Smiod   /* Lowest two bits must be 0.  We return bfd_reloc_overflow for
1201*3d8817e4Smiod      everything that looks strange.  */
1202*3d8817e4Smiod   if (value & 3)
1203*3d8817e4Smiod     flag = bfd_reloc_overflow;
1204*3d8817e4Smiod 
1205*3d8817e4Smiod   bfd_put_32 (abfd,
1206*3d8817e4Smiod 	      (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
1207*3d8817e4Smiod 	      (bfd_byte *) datap + offs);
1208*3d8817e4Smiod   bfd_put_32 (abfd,
1209*3d8817e4Smiod 	      (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
1210*3d8817e4Smiod 	      (bfd_byte *) datap + offs + 4);
1211*3d8817e4Smiod   bfd_put_32 (abfd,
1212*3d8817e4Smiod 	      (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
1213*3d8817e4Smiod 	      (bfd_byte *) datap + offs + 8);
1214*3d8817e4Smiod   bfd_put_32 (abfd,
1215*3d8817e4Smiod 	      (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
1216*3d8817e4Smiod 	      (bfd_byte *) datap + offs + 12);
1217*3d8817e4Smiod 
1218*3d8817e4Smiod   return flag;
1219*3d8817e4Smiod }
1220*3d8817e4Smiod 
1221*3d8817e4Smiod /* Set the howto pointer for an MMIX ELF reloc (type RELA).  */
1222*3d8817e4Smiod 
1223*3d8817e4Smiod static void
mmix_info_to_howto_rela(abfd,cache_ptr,dst)1224*3d8817e4Smiod mmix_info_to_howto_rela (abfd, cache_ptr, dst)
1225*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
1226*3d8817e4Smiod      arelent *cache_ptr;
1227*3d8817e4Smiod      Elf_Internal_Rela *dst;
1228*3d8817e4Smiod {
1229*3d8817e4Smiod   unsigned int r_type;
1230*3d8817e4Smiod 
1231*3d8817e4Smiod   r_type = ELF64_R_TYPE (dst->r_info);
1232*3d8817e4Smiod   BFD_ASSERT (r_type < (unsigned int) R_MMIX_max);
1233*3d8817e4Smiod   cache_ptr->howto = &elf_mmix_howto_table[r_type];
1234*3d8817e4Smiod }
1235*3d8817e4Smiod 
1236*3d8817e4Smiod /* Any MMIX-specific relocation gets here at assembly time or when linking
1237*3d8817e4Smiod    to other formats (such as mmo); this is the relocation function from
1238*3d8817e4Smiod    the reloc_table.  We don't get here for final pure ELF linking.  */
1239*3d8817e4Smiod 
1240*3d8817e4Smiod static bfd_reloc_status_type
mmix_elf_reloc(abfd,reloc_entry,symbol,data,input_section,output_bfd,error_message)1241*3d8817e4Smiod mmix_elf_reloc (abfd, reloc_entry, symbol, data, input_section,
1242*3d8817e4Smiod 		output_bfd, error_message)
1243*3d8817e4Smiod      bfd *abfd;
1244*3d8817e4Smiod      arelent *reloc_entry;
1245*3d8817e4Smiod      asymbol *symbol;
1246*3d8817e4Smiod      PTR data;
1247*3d8817e4Smiod      asection *input_section;
1248*3d8817e4Smiod      bfd *output_bfd;
1249*3d8817e4Smiod      char **error_message ATTRIBUTE_UNUSED;
1250*3d8817e4Smiod {
1251*3d8817e4Smiod   bfd_vma relocation;
1252*3d8817e4Smiod   bfd_reloc_status_type r;
1253*3d8817e4Smiod   asection *reloc_target_output_section;
1254*3d8817e4Smiod   bfd_reloc_status_type flag = bfd_reloc_ok;
1255*3d8817e4Smiod   bfd_vma output_base = 0;
1256*3d8817e4Smiod   bfd_vma addr;
1257*3d8817e4Smiod 
1258*3d8817e4Smiod   r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1259*3d8817e4Smiod 			     input_section, output_bfd, error_message);
1260*3d8817e4Smiod 
1261*3d8817e4Smiod   /* If that was all that was needed (i.e. this isn't a final link, only
1262*3d8817e4Smiod      some segment adjustments), we're done.  */
1263*3d8817e4Smiod   if (r != bfd_reloc_continue)
1264*3d8817e4Smiod     return r;
1265*3d8817e4Smiod 
1266*3d8817e4Smiod   if (bfd_is_und_section (symbol->section)
1267*3d8817e4Smiod       && (symbol->flags & BSF_WEAK) == 0
1268*3d8817e4Smiod       && output_bfd == (bfd *) NULL)
1269*3d8817e4Smiod     return bfd_reloc_undefined;
1270*3d8817e4Smiod 
1271*3d8817e4Smiod   /* Is the address of the relocation really within the section?  */
1272*3d8817e4Smiod   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1273*3d8817e4Smiod     return bfd_reloc_outofrange;
1274*3d8817e4Smiod 
1275*3d8817e4Smiod   /* Work out which section the relocation is targeted at and the
1276*3d8817e4Smiod      initial relocation command value.  */
1277*3d8817e4Smiod 
1278*3d8817e4Smiod   /* Get symbol value.  (Common symbols are special.)  */
1279*3d8817e4Smiod   if (bfd_is_com_section (symbol->section))
1280*3d8817e4Smiod     relocation = 0;
1281*3d8817e4Smiod   else
1282*3d8817e4Smiod     relocation = symbol->value;
1283*3d8817e4Smiod 
1284*3d8817e4Smiod   reloc_target_output_section = bfd_get_output_section (symbol);
1285*3d8817e4Smiod 
1286*3d8817e4Smiod   /* Here the variable relocation holds the final address of the symbol we
1287*3d8817e4Smiod      are relocating against, plus any addend.  */
1288*3d8817e4Smiod   if (output_bfd)
1289*3d8817e4Smiod     output_base = 0;
1290*3d8817e4Smiod   else
1291*3d8817e4Smiod     output_base = reloc_target_output_section->vma;
1292*3d8817e4Smiod 
1293*3d8817e4Smiod   relocation += output_base + symbol->section->output_offset;
1294*3d8817e4Smiod 
1295*3d8817e4Smiod   /* Get position of relocation.  */
1296*3d8817e4Smiod   addr = (reloc_entry->address + input_section->output_section->vma
1297*3d8817e4Smiod 	  + input_section->output_offset);
1298*3d8817e4Smiod   if (output_bfd != (bfd *) NULL)
1299*3d8817e4Smiod     {
1300*3d8817e4Smiod       /* Add in supplied addend.  */
1301*3d8817e4Smiod       relocation += reloc_entry->addend;
1302*3d8817e4Smiod 
1303*3d8817e4Smiod       /* This is a partial relocation, and we want to apply the
1304*3d8817e4Smiod 	 relocation to the reloc entry rather than the raw data.
1305*3d8817e4Smiod 	 Modify the reloc inplace to reflect what we now know.  */
1306*3d8817e4Smiod       reloc_entry->addend = relocation;
1307*3d8817e4Smiod       reloc_entry->address += input_section->output_offset;
1308*3d8817e4Smiod       return flag;
1309*3d8817e4Smiod     }
1310*3d8817e4Smiod 
1311*3d8817e4Smiod   return mmix_final_link_relocate (reloc_entry->howto, input_section,
1312*3d8817e4Smiod 				   data, reloc_entry->address,
1313*3d8817e4Smiod 				   reloc_entry->addend, relocation,
1314*3d8817e4Smiod 				   bfd_asymbol_name (symbol),
1315*3d8817e4Smiod 				   reloc_target_output_section);
1316*3d8817e4Smiod }
1317*3d8817e4Smiod 
1318*3d8817e4Smiod /* Relocate an MMIX ELF section.  Modified from elf32-fr30.c; look to it
1319*3d8817e4Smiod    for guidance if you're thinking of copying this.  */
1320*3d8817e4Smiod 
1321*3d8817e4Smiod static bfd_boolean
mmix_elf_relocate_section(output_bfd,info,input_bfd,input_section,contents,relocs,local_syms,local_sections)1322*3d8817e4Smiod mmix_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1323*3d8817e4Smiod 			   contents, relocs, local_syms, local_sections)
1324*3d8817e4Smiod      bfd *output_bfd ATTRIBUTE_UNUSED;
1325*3d8817e4Smiod      struct bfd_link_info *info;
1326*3d8817e4Smiod      bfd *input_bfd;
1327*3d8817e4Smiod      asection *input_section;
1328*3d8817e4Smiod      bfd_byte *contents;
1329*3d8817e4Smiod      Elf_Internal_Rela *relocs;
1330*3d8817e4Smiod      Elf_Internal_Sym *local_syms;
1331*3d8817e4Smiod      asection **local_sections;
1332*3d8817e4Smiod {
1333*3d8817e4Smiod   Elf_Internal_Shdr *symtab_hdr;
1334*3d8817e4Smiod   struct elf_link_hash_entry **sym_hashes;
1335*3d8817e4Smiod   Elf_Internal_Rela *rel;
1336*3d8817e4Smiod   Elf_Internal_Rela *relend;
1337*3d8817e4Smiod   bfd_size_type size;
1338*3d8817e4Smiod   size_t pjsno = 0;
1339*3d8817e4Smiod 
1340*3d8817e4Smiod   size = input_section->rawsize ? input_section->rawsize : input_section->size;
1341*3d8817e4Smiod   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1342*3d8817e4Smiod   sym_hashes = elf_sym_hashes (input_bfd);
1343*3d8817e4Smiod   relend = relocs + input_section->reloc_count;
1344*3d8817e4Smiod 
1345*3d8817e4Smiod   /* Zero the stub area before we start.  */
1346*3d8817e4Smiod   if (input_section->rawsize != 0
1347*3d8817e4Smiod       && input_section->size > input_section->rawsize)
1348*3d8817e4Smiod     memset (contents + input_section->rawsize, 0,
1349*3d8817e4Smiod 	    input_section->size - input_section->rawsize);
1350*3d8817e4Smiod 
1351*3d8817e4Smiod   for (rel = relocs; rel < relend; rel ++)
1352*3d8817e4Smiod     {
1353*3d8817e4Smiod       reloc_howto_type *howto;
1354*3d8817e4Smiod       unsigned long r_symndx;
1355*3d8817e4Smiod       Elf_Internal_Sym *sym;
1356*3d8817e4Smiod       asection *sec;
1357*3d8817e4Smiod       struct elf_link_hash_entry *h;
1358*3d8817e4Smiod       bfd_vma relocation;
1359*3d8817e4Smiod       bfd_reloc_status_type r;
1360*3d8817e4Smiod       const char *name = NULL;
1361*3d8817e4Smiod       int r_type;
1362*3d8817e4Smiod       bfd_boolean undefined_signalled = FALSE;
1363*3d8817e4Smiod 
1364*3d8817e4Smiod       r_type = ELF64_R_TYPE (rel->r_info);
1365*3d8817e4Smiod 
1366*3d8817e4Smiod       if (r_type == R_MMIX_GNU_VTINHERIT
1367*3d8817e4Smiod 	  || r_type == R_MMIX_GNU_VTENTRY)
1368*3d8817e4Smiod 	continue;
1369*3d8817e4Smiod 
1370*3d8817e4Smiod       r_symndx = ELF64_R_SYM (rel->r_info);
1371*3d8817e4Smiod 
1372*3d8817e4Smiod       if (info->relocatable)
1373*3d8817e4Smiod 	{
1374*3d8817e4Smiod 	  /* This is a relocatable link.  For most relocs we don't have to
1375*3d8817e4Smiod 	     change anything, unless the reloc is against a section
1376*3d8817e4Smiod 	     symbol, in which case we have to adjust according to where
1377*3d8817e4Smiod 	     the section symbol winds up in the output section.  */
1378*3d8817e4Smiod 	  if (r_symndx < symtab_hdr->sh_info)
1379*3d8817e4Smiod 	    {
1380*3d8817e4Smiod 	      sym = local_syms + r_symndx;
1381*3d8817e4Smiod 
1382*3d8817e4Smiod 	      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1383*3d8817e4Smiod 		{
1384*3d8817e4Smiod 		  sec = local_sections [r_symndx];
1385*3d8817e4Smiod 		  rel->r_addend += sec->output_offset + sym->st_value;
1386*3d8817e4Smiod 		}
1387*3d8817e4Smiod 	    }
1388*3d8817e4Smiod 
1389*3d8817e4Smiod 	  /* For PUSHJ stub relocs however, we may need to change the
1390*3d8817e4Smiod 	     reloc and the section contents, if the reloc doesn't reach
1391*3d8817e4Smiod 	     beyond the end of the output section and previous stubs.
1392*3d8817e4Smiod 	     Then we change the section contents to be a PUSHJ to the end
1393*3d8817e4Smiod 	     of the input section plus stubs (we can do that without using
1394*3d8817e4Smiod 	     a reloc), and then we change the reloc to be a R_MMIX_PUSHJ
1395*3d8817e4Smiod 	     at the stub location.  */
1396*3d8817e4Smiod 	  if (r_type == R_MMIX_PUSHJ_STUBBABLE)
1397*3d8817e4Smiod 	    {
1398*3d8817e4Smiod 	      /* We've already checked whether we need a stub; use that
1399*3d8817e4Smiod 		 knowledge.  */
1400*3d8817e4Smiod 	      if (mmix_elf_section_data (input_section)->pjs.stub_size[pjsno]
1401*3d8817e4Smiod 		  != 0)
1402*3d8817e4Smiod 		{
1403*3d8817e4Smiod 		  Elf_Internal_Rela relcpy;
1404*3d8817e4Smiod 
1405*3d8817e4Smiod 		  if (mmix_elf_section_data (input_section)
1406*3d8817e4Smiod 		      ->pjs.stub_size[pjsno] != MAX_PUSHJ_STUB_SIZE)
1407*3d8817e4Smiod 		    abort ();
1408*3d8817e4Smiod 
1409*3d8817e4Smiod 		  /* There's already a PUSHJ insn there, so just fill in
1410*3d8817e4Smiod 		     the offset bits to the stub.  */
1411*3d8817e4Smiod 		  if (mmix_final_link_relocate (elf_mmix_howto_table
1412*3d8817e4Smiod 						+ R_MMIX_ADDR19,
1413*3d8817e4Smiod 						input_section,
1414*3d8817e4Smiod 						contents,
1415*3d8817e4Smiod 						rel->r_offset,
1416*3d8817e4Smiod 						0,
1417*3d8817e4Smiod 						input_section
1418*3d8817e4Smiod 						->output_section->vma
1419*3d8817e4Smiod 						+ input_section->output_offset
1420*3d8817e4Smiod 						+ size
1421*3d8817e4Smiod 						+ mmix_elf_section_data (input_section)
1422*3d8817e4Smiod 						->pjs.stub_offset,
1423*3d8817e4Smiod 						NULL, NULL) != bfd_reloc_ok)
1424*3d8817e4Smiod 		    return FALSE;
1425*3d8817e4Smiod 
1426*3d8817e4Smiod 		  /* Put a JMP insn at the stub; it goes with the
1427*3d8817e4Smiod 		     R_MMIX_JMP reloc.  */
1428*3d8817e4Smiod 		  bfd_put_32 (output_bfd, JMP_INSN_BYTE << 24,
1429*3d8817e4Smiod 			      contents
1430*3d8817e4Smiod 			      + size
1431*3d8817e4Smiod 			      + mmix_elf_section_data (input_section)
1432*3d8817e4Smiod 			      ->pjs.stub_offset);
1433*3d8817e4Smiod 
1434*3d8817e4Smiod 		  /* Change the reloc to be at the stub, and to a full
1435*3d8817e4Smiod 		     R_MMIX_JMP reloc.  */
1436*3d8817e4Smiod 		  rel->r_info = ELF64_R_INFO (r_symndx, R_MMIX_JMP);
1437*3d8817e4Smiod 		  rel->r_offset
1438*3d8817e4Smiod 		    = (size
1439*3d8817e4Smiod 		       + mmix_elf_section_data (input_section)
1440*3d8817e4Smiod 		       ->pjs.stub_offset);
1441*3d8817e4Smiod 
1442*3d8817e4Smiod 		  mmix_elf_section_data (input_section)->pjs.stub_offset
1443*3d8817e4Smiod 		    += MAX_PUSHJ_STUB_SIZE;
1444*3d8817e4Smiod 
1445*3d8817e4Smiod 		  /* Shift this reloc to the end of the relocs to maintain
1446*3d8817e4Smiod 		     the r_offset sorted reloc order.  */
1447*3d8817e4Smiod 		  relcpy = *rel;
1448*3d8817e4Smiod 		  memmove (rel, rel + 1, (char *) relend - (char *) rel);
1449*3d8817e4Smiod 		  relend[-1] = relcpy;
1450*3d8817e4Smiod 
1451*3d8817e4Smiod 		  /* Back up one reloc, or else we'd skip the next reloc
1452*3d8817e4Smiod 		   in turn.  */
1453*3d8817e4Smiod 		  rel--;
1454*3d8817e4Smiod 		}
1455*3d8817e4Smiod 
1456*3d8817e4Smiod 	      pjsno++;
1457*3d8817e4Smiod 	    }
1458*3d8817e4Smiod 	  continue;
1459*3d8817e4Smiod 	}
1460*3d8817e4Smiod 
1461*3d8817e4Smiod       /* This is a final link.  */
1462*3d8817e4Smiod       howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
1463*3d8817e4Smiod       h = NULL;
1464*3d8817e4Smiod       sym = NULL;
1465*3d8817e4Smiod       sec = NULL;
1466*3d8817e4Smiod 
1467*3d8817e4Smiod       if (r_symndx < symtab_hdr->sh_info)
1468*3d8817e4Smiod 	{
1469*3d8817e4Smiod 	  sym = local_syms + r_symndx;
1470*3d8817e4Smiod 	  sec = local_sections [r_symndx];
1471*3d8817e4Smiod 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1472*3d8817e4Smiod 
1473*3d8817e4Smiod 	  name = bfd_elf_string_from_elf_section (input_bfd,
1474*3d8817e4Smiod 						  symtab_hdr->sh_link,
1475*3d8817e4Smiod 						  sym->st_name);
1476*3d8817e4Smiod 	  if (name == NULL)
1477*3d8817e4Smiod 	    name = bfd_section_name (input_bfd, sec);
1478*3d8817e4Smiod 	}
1479*3d8817e4Smiod       else
1480*3d8817e4Smiod 	{
1481*3d8817e4Smiod 	  bfd_boolean unresolved_reloc;
1482*3d8817e4Smiod 
1483*3d8817e4Smiod 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1484*3d8817e4Smiod 				   r_symndx, symtab_hdr, sym_hashes,
1485*3d8817e4Smiod 				   h, sec, relocation,
1486*3d8817e4Smiod 				   unresolved_reloc, undefined_signalled);
1487*3d8817e4Smiod 	  name = h->root.root.string;
1488*3d8817e4Smiod 	}
1489*3d8817e4Smiod 
1490*3d8817e4Smiod       r = mmix_final_link_relocate (howto, input_section,
1491*3d8817e4Smiod 				    contents, rel->r_offset,
1492*3d8817e4Smiod 				    rel->r_addend, relocation, name, sec);
1493*3d8817e4Smiod 
1494*3d8817e4Smiod       if (r != bfd_reloc_ok)
1495*3d8817e4Smiod 	{
1496*3d8817e4Smiod 	  bfd_boolean check_ok = TRUE;
1497*3d8817e4Smiod 	  const char * msg = (const char *) NULL;
1498*3d8817e4Smiod 
1499*3d8817e4Smiod 	  switch (r)
1500*3d8817e4Smiod 	    {
1501*3d8817e4Smiod 	    case bfd_reloc_overflow:
1502*3d8817e4Smiod 	      check_ok = info->callbacks->reloc_overflow
1503*3d8817e4Smiod 		(info, (h ? &h->root : NULL), name, howto->name,
1504*3d8817e4Smiod 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1505*3d8817e4Smiod 	      break;
1506*3d8817e4Smiod 
1507*3d8817e4Smiod 	    case bfd_reloc_undefined:
1508*3d8817e4Smiod 	      /* We may have sent this message above.  */
1509*3d8817e4Smiod 	      if (! undefined_signalled)
1510*3d8817e4Smiod 		check_ok = info->callbacks->undefined_symbol
1511*3d8817e4Smiod 		  (info, name, input_bfd, input_section, rel->r_offset,
1512*3d8817e4Smiod 		   TRUE);
1513*3d8817e4Smiod 	      undefined_signalled = TRUE;
1514*3d8817e4Smiod 	      break;
1515*3d8817e4Smiod 
1516*3d8817e4Smiod 	    case bfd_reloc_outofrange:
1517*3d8817e4Smiod 	      msg = _("internal error: out of range error");
1518*3d8817e4Smiod 	      break;
1519*3d8817e4Smiod 
1520*3d8817e4Smiod 	    case bfd_reloc_notsupported:
1521*3d8817e4Smiod 	      msg = _("internal error: unsupported relocation error");
1522*3d8817e4Smiod 	      break;
1523*3d8817e4Smiod 
1524*3d8817e4Smiod 	    case bfd_reloc_dangerous:
1525*3d8817e4Smiod 	      msg = _("internal error: dangerous relocation");
1526*3d8817e4Smiod 	      break;
1527*3d8817e4Smiod 
1528*3d8817e4Smiod 	    default:
1529*3d8817e4Smiod 	      msg = _("internal error: unknown error");
1530*3d8817e4Smiod 	      break;
1531*3d8817e4Smiod 	    }
1532*3d8817e4Smiod 
1533*3d8817e4Smiod 	  if (msg)
1534*3d8817e4Smiod 	    check_ok = info->callbacks->warning
1535*3d8817e4Smiod 	      (info, msg, name, input_bfd, input_section, rel->r_offset);
1536*3d8817e4Smiod 
1537*3d8817e4Smiod 	  if (! check_ok)
1538*3d8817e4Smiod 	    return FALSE;
1539*3d8817e4Smiod 	}
1540*3d8817e4Smiod     }
1541*3d8817e4Smiod 
1542*3d8817e4Smiod   return TRUE;
1543*3d8817e4Smiod }
1544*3d8817e4Smiod 
1545*3d8817e4Smiod /* Perform a single relocation.  By default we use the standard BFD
1546*3d8817e4Smiod    routines.  A few relocs we have to do ourselves.  */
1547*3d8817e4Smiod 
1548*3d8817e4Smiod static bfd_reloc_status_type
mmix_final_link_relocate(howto,input_section,contents,r_offset,r_addend,relocation,symname,symsec)1549*3d8817e4Smiod mmix_final_link_relocate (howto, input_section, contents,
1550*3d8817e4Smiod 			  r_offset, r_addend, relocation, symname, symsec)
1551*3d8817e4Smiod      reloc_howto_type *howto;
1552*3d8817e4Smiod      asection *input_section;
1553*3d8817e4Smiod      bfd_byte *contents;
1554*3d8817e4Smiod      bfd_vma r_offset;
1555*3d8817e4Smiod      bfd_signed_vma r_addend;
1556*3d8817e4Smiod      bfd_vma relocation;
1557*3d8817e4Smiod      const char *symname;
1558*3d8817e4Smiod      asection *symsec;
1559*3d8817e4Smiod {
1560*3d8817e4Smiod   bfd_reloc_status_type r = bfd_reloc_ok;
1561*3d8817e4Smiod   bfd_vma addr
1562*3d8817e4Smiod     = (input_section->output_section->vma
1563*3d8817e4Smiod        + input_section->output_offset
1564*3d8817e4Smiod        + r_offset);
1565*3d8817e4Smiod   bfd_signed_vma srel
1566*3d8817e4Smiod     = (bfd_signed_vma) relocation + r_addend;
1567*3d8817e4Smiod 
1568*3d8817e4Smiod   switch (howto->type)
1569*3d8817e4Smiod     {
1570*3d8817e4Smiod       /* All these are PC-relative.  */
1571*3d8817e4Smiod     case R_MMIX_PUSHJ_STUBBABLE:
1572*3d8817e4Smiod     case R_MMIX_PUSHJ:
1573*3d8817e4Smiod     case R_MMIX_CBRANCH:
1574*3d8817e4Smiod     case R_MMIX_ADDR19:
1575*3d8817e4Smiod     case R_MMIX_GETA:
1576*3d8817e4Smiod     case R_MMIX_ADDR27:
1577*3d8817e4Smiod     case R_MMIX_JMP:
1578*3d8817e4Smiod       contents += r_offset;
1579*3d8817e4Smiod 
1580*3d8817e4Smiod       srel -= (input_section->output_section->vma
1581*3d8817e4Smiod 	       + input_section->output_offset
1582*3d8817e4Smiod 	       + r_offset);
1583*3d8817e4Smiod 
1584*3d8817e4Smiod       r = mmix_elf_perform_relocation (input_section, howto, contents,
1585*3d8817e4Smiod 				       addr, srel);
1586*3d8817e4Smiod       break;
1587*3d8817e4Smiod 
1588*3d8817e4Smiod     case R_MMIX_BASE_PLUS_OFFSET:
1589*3d8817e4Smiod       if (symsec == NULL)
1590*3d8817e4Smiod 	return bfd_reloc_undefined;
1591*3d8817e4Smiod 
1592*3d8817e4Smiod       /* Check that we're not relocating against a register symbol.  */
1593*3d8817e4Smiod       if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1594*3d8817e4Smiod 		  MMIX_REG_CONTENTS_SECTION_NAME) == 0
1595*3d8817e4Smiod 	  || strcmp (bfd_get_section_name (symsec->owner, symsec),
1596*3d8817e4Smiod 		     MMIX_REG_SECTION_NAME) == 0)
1597*3d8817e4Smiod 	{
1598*3d8817e4Smiod 	  /* Note: This is separated out into two messages in order
1599*3d8817e4Smiod 	     to ease the translation into other languages.  */
1600*3d8817e4Smiod 	  if (symname == NULL || *symname == 0)
1601*3d8817e4Smiod 	    (*_bfd_error_handler)
1602*3d8817e4Smiod 	      (_("%s: base-plus-offset relocation against register symbol: (unknown) in %s"),
1603*3d8817e4Smiod 	       bfd_get_filename (input_section->owner),
1604*3d8817e4Smiod 	       bfd_get_section_name (symsec->owner, symsec));
1605*3d8817e4Smiod 	  else
1606*3d8817e4Smiod 	    (*_bfd_error_handler)
1607*3d8817e4Smiod 	      (_("%s: base-plus-offset relocation against register symbol: %s in %s"),
1608*3d8817e4Smiod 	       bfd_get_filename (input_section->owner), symname,
1609*3d8817e4Smiod 	       bfd_get_section_name (symsec->owner, symsec));
1610*3d8817e4Smiod 	  return bfd_reloc_overflow;
1611*3d8817e4Smiod 	}
1612*3d8817e4Smiod       goto do_mmix_reloc;
1613*3d8817e4Smiod 
1614*3d8817e4Smiod     case R_MMIX_REG_OR_BYTE:
1615*3d8817e4Smiod     case R_MMIX_REG:
1616*3d8817e4Smiod       /* For now, we handle these alike.  They must refer to an register
1617*3d8817e4Smiod 	 symbol, which is either relative to the register section and in
1618*3d8817e4Smiod 	 the range 0..255, or is in the register contents section with vma
1619*3d8817e4Smiod 	 regno * 8.  */
1620*3d8817e4Smiod 
1621*3d8817e4Smiod       /* FIXME: A better way to check for reg contents section?
1622*3d8817e4Smiod 	 FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1623*3d8817e4Smiod       if (symsec == NULL)
1624*3d8817e4Smiod 	return bfd_reloc_undefined;
1625*3d8817e4Smiod 
1626*3d8817e4Smiod       if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1627*3d8817e4Smiod 		  MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1628*3d8817e4Smiod 	{
1629*3d8817e4Smiod 	  if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1630*3d8817e4Smiod 	    {
1631*3d8817e4Smiod 	      /* The bfd_reloc_outofrange return value, though intuitively
1632*3d8817e4Smiod 		 a better value, will not get us an error.  */
1633*3d8817e4Smiod 	      return bfd_reloc_overflow;
1634*3d8817e4Smiod 	    }
1635*3d8817e4Smiod 	  srel /= 8;
1636*3d8817e4Smiod 	}
1637*3d8817e4Smiod       else if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1638*3d8817e4Smiod 		       MMIX_REG_SECTION_NAME) == 0)
1639*3d8817e4Smiod 	{
1640*3d8817e4Smiod 	  if (srel < 0 || srel > 255)
1641*3d8817e4Smiod 	    /* The bfd_reloc_outofrange return value, though intuitively a
1642*3d8817e4Smiod 	       better value, will not get us an error.  */
1643*3d8817e4Smiod 	    return bfd_reloc_overflow;
1644*3d8817e4Smiod 	}
1645*3d8817e4Smiod       else
1646*3d8817e4Smiod 	{
1647*3d8817e4Smiod 	  /* Note: This is separated out into two messages in order
1648*3d8817e4Smiod 	     to ease the translation into other languages.  */
1649*3d8817e4Smiod 	  if (symname == NULL || *symname == 0)
1650*3d8817e4Smiod 	    (*_bfd_error_handler)
1651*3d8817e4Smiod 	      (_("%s: register relocation against non-register symbol: (unknown) in %s"),
1652*3d8817e4Smiod 	       bfd_get_filename (input_section->owner),
1653*3d8817e4Smiod 	       bfd_get_section_name (symsec->owner, symsec));
1654*3d8817e4Smiod 	  else
1655*3d8817e4Smiod 	    (*_bfd_error_handler)
1656*3d8817e4Smiod 	      (_("%s: register relocation against non-register symbol: %s in %s"),
1657*3d8817e4Smiod 	       bfd_get_filename (input_section->owner), symname,
1658*3d8817e4Smiod 	       bfd_get_section_name (symsec->owner, symsec));
1659*3d8817e4Smiod 
1660*3d8817e4Smiod 	  /* The bfd_reloc_outofrange return value, though intuitively a
1661*3d8817e4Smiod 	     better value, will not get us an error.  */
1662*3d8817e4Smiod 	  return bfd_reloc_overflow;
1663*3d8817e4Smiod 	}
1664*3d8817e4Smiod     do_mmix_reloc:
1665*3d8817e4Smiod       contents += r_offset;
1666*3d8817e4Smiod       r = mmix_elf_perform_relocation (input_section, howto, contents,
1667*3d8817e4Smiod 				       addr, srel);
1668*3d8817e4Smiod       break;
1669*3d8817e4Smiod 
1670*3d8817e4Smiod     case R_MMIX_LOCAL:
1671*3d8817e4Smiod       /* This isn't a real relocation, it's just an assertion that the
1672*3d8817e4Smiod 	 final relocation value corresponds to a local register.  We
1673*3d8817e4Smiod 	 ignore the actual relocation; nothing is changed.  */
1674*3d8817e4Smiod       {
1675*3d8817e4Smiod 	asection *regsec
1676*3d8817e4Smiod 	  = bfd_get_section_by_name (input_section->output_section->owner,
1677*3d8817e4Smiod 				     MMIX_REG_CONTENTS_SECTION_NAME);
1678*3d8817e4Smiod 	bfd_vma first_global;
1679*3d8817e4Smiod 
1680*3d8817e4Smiod 	/* Check that this is an absolute value, or a reference to the
1681*3d8817e4Smiod 	   register contents section or the register (symbol) section.
1682*3d8817e4Smiod 	   Absolute numbers can get here as undefined section.  Undefined
1683*3d8817e4Smiod 	   symbols are signalled elsewhere, so there's no conflict in us
1684*3d8817e4Smiod 	   accidentally handling it.  */
1685*3d8817e4Smiod 	if (!bfd_is_abs_section (symsec)
1686*3d8817e4Smiod 	    && !bfd_is_und_section (symsec)
1687*3d8817e4Smiod 	    && strcmp (bfd_get_section_name (symsec->owner, symsec),
1688*3d8817e4Smiod 		       MMIX_REG_CONTENTS_SECTION_NAME) != 0
1689*3d8817e4Smiod 	    && strcmp (bfd_get_section_name (symsec->owner, symsec),
1690*3d8817e4Smiod 		       MMIX_REG_SECTION_NAME) != 0)
1691*3d8817e4Smiod 	{
1692*3d8817e4Smiod 	  (*_bfd_error_handler)
1693*3d8817e4Smiod 	    (_("%s: directive LOCAL valid only with a register or absolute value"),
1694*3d8817e4Smiod 	     bfd_get_filename (input_section->owner));
1695*3d8817e4Smiod 
1696*3d8817e4Smiod 	  return bfd_reloc_overflow;
1697*3d8817e4Smiod 	}
1698*3d8817e4Smiod 
1699*3d8817e4Smiod       /* If we don't have a register contents section, then $255 is the
1700*3d8817e4Smiod 	 first global register.  */
1701*3d8817e4Smiod       if (regsec == NULL)
1702*3d8817e4Smiod 	first_global = 255;
1703*3d8817e4Smiod       else
1704*3d8817e4Smiod 	{
1705*3d8817e4Smiod 	  first_global = bfd_get_section_vma (abfd, regsec) / 8;
1706*3d8817e4Smiod 	  if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1707*3d8817e4Smiod 		      MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1708*3d8817e4Smiod 	    {
1709*3d8817e4Smiod 	      if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1710*3d8817e4Smiod 		/* The bfd_reloc_outofrange return value, though
1711*3d8817e4Smiod 		   intuitively a better value, will not get us an error.  */
1712*3d8817e4Smiod 		return bfd_reloc_overflow;
1713*3d8817e4Smiod 	      srel /= 8;
1714*3d8817e4Smiod 	    }
1715*3d8817e4Smiod 	}
1716*3d8817e4Smiod 
1717*3d8817e4Smiod 	if ((bfd_vma) srel >= first_global)
1718*3d8817e4Smiod 	  {
1719*3d8817e4Smiod 	    /* FIXME: Better error message.  */
1720*3d8817e4Smiod 	    (*_bfd_error_handler)
1721*3d8817e4Smiod 	      (_("%s: LOCAL directive: Register $%ld is not a local register.  First global register is $%ld."),
1722*3d8817e4Smiod 	       bfd_get_filename (input_section->owner), (long) srel, (long) first_global);
1723*3d8817e4Smiod 
1724*3d8817e4Smiod 	    return bfd_reloc_overflow;
1725*3d8817e4Smiod 	  }
1726*3d8817e4Smiod       }
1727*3d8817e4Smiod       r = bfd_reloc_ok;
1728*3d8817e4Smiod       break;
1729*3d8817e4Smiod 
1730*3d8817e4Smiod     default:
1731*3d8817e4Smiod       r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
1732*3d8817e4Smiod 				    contents, r_offset,
1733*3d8817e4Smiod 				    relocation, r_addend);
1734*3d8817e4Smiod     }
1735*3d8817e4Smiod 
1736*3d8817e4Smiod   return r;
1737*3d8817e4Smiod }
1738*3d8817e4Smiod 
1739*3d8817e4Smiod /* Return the section that should be marked against GC for a given
1740*3d8817e4Smiod    relocation.  */
1741*3d8817e4Smiod 
1742*3d8817e4Smiod static asection *
mmix_elf_gc_mark_hook(sec,info,rel,h,sym)1743*3d8817e4Smiod mmix_elf_gc_mark_hook (sec, info, rel, h, sym)
1744*3d8817e4Smiod      asection *sec;
1745*3d8817e4Smiod      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1746*3d8817e4Smiod      Elf_Internal_Rela *rel;
1747*3d8817e4Smiod      struct elf_link_hash_entry *h;
1748*3d8817e4Smiod      Elf_Internal_Sym *sym;
1749*3d8817e4Smiod {
1750*3d8817e4Smiod   if (h != NULL)
1751*3d8817e4Smiod     {
1752*3d8817e4Smiod       switch (ELF64_R_TYPE (rel->r_info))
1753*3d8817e4Smiod 	{
1754*3d8817e4Smiod 	case R_MMIX_GNU_VTINHERIT:
1755*3d8817e4Smiod 	case R_MMIX_GNU_VTENTRY:
1756*3d8817e4Smiod 	  break;
1757*3d8817e4Smiod 
1758*3d8817e4Smiod 	default:
1759*3d8817e4Smiod 	  switch (h->root.type)
1760*3d8817e4Smiod 	    {
1761*3d8817e4Smiod 	    case bfd_link_hash_defined:
1762*3d8817e4Smiod 	    case bfd_link_hash_defweak:
1763*3d8817e4Smiod 	      return h->root.u.def.section;
1764*3d8817e4Smiod 
1765*3d8817e4Smiod 	    case bfd_link_hash_common:
1766*3d8817e4Smiod 	      return h->root.u.c.p->section;
1767*3d8817e4Smiod 
1768*3d8817e4Smiod 	    default:
1769*3d8817e4Smiod 	      break;
1770*3d8817e4Smiod 	    }
1771*3d8817e4Smiod 	}
1772*3d8817e4Smiod     }
1773*3d8817e4Smiod   else
1774*3d8817e4Smiod     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1775*3d8817e4Smiod 
1776*3d8817e4Smiod   return NULL;
1777*3d8817e4Smiod }
1778*3d8817e4Smiod 
1779*3d8817e4Smiod /* Update relocation info for a GC-excluded section.  We could supposedly
1780*3d8817e4Smiod    perform the allocation after GC, but there's no suitable hook between
1781*3d8817e4Smiod    GC (or section merge) and the point when all input sections must be
1782*3d8817e4Smiod    present.  Better to waste some memory and (perhaps) a little time.  */
1783*3d8817e4Smiod 
1784*3d8817e4Smiod static bfd_boolean
mmix_elf_gc_sweep_hook(abfd,info,sec,relocs)1785*3d8817e4Smiod mmix_elf_gc_sweep_hook (abfd, info, sec, relocs)
1786*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
1787*3d8817e4Smiod      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1788*3d8817e4Smiod      asection *sec ATTRIBUTE_UNUSED;
1789*3d8817e4Smiod      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1790*3d8817e4Smiod {
1791*3d8817e4Smiod   struct bpo_reloc_section_info *bpodata
1792*3d8817e4Smiod     = mmix_elf_section_data (sec)->bpo.reloc;
1793*3d8817e4Smiod   asection *allocated_gregs_section;
1794*3d8817e4Smiod 
1795*3d8817e4Smiod   /* If no bpodata here, we have nothing to do.  */
1796*3d8817e4Smiod   if (bpodata == NULL)
1797*3d8817e4Smiod     return TRUE;
1798*3d8817e4Smiod 
1799*3d8817e4Smiod   allocated_gregs_section = bpodata->bpo_greg_section;
1800*3d8817e4Smiod 
1801*3d8817e4Smiod   mmix_elf_section_data (allocated_gregs_section)->bpo.greg->n_bpo_relocs
1802*3d8817e4Smiod     -= bpodata->n_bpo_relocs_this_section;
1803*3d8817e4Smiod 
1804*3d8817e4Smiod   return TRUE;
1805*3d8817e4Smiod }
1806*3d8817e4Smiod 
1807*3d8817e4Smiod /* Sort register relocs to come before expanding relocs.  */
1808*3d8817e4Smiod 
1809*3d8817e4Smiod static int
mmix_elf_sort_relocs(p1,p2)1810*3d8817e4Smiod mmix_elf_sort_relocs (p1, p2)
1811*3d8817e4Smiod      const PTR p1;
1812*3d8817e4Smiod      const PTR p2;
1813*3d8817e4Smiod {
1814*3d8817e4Smiod   const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
1815*3d8817e4Smiod   const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
1816*3d8817e4Smiod   int r1_is_reg, r2_is_reg;
1817*3d8817e4Smiod 
1818*3d8817e4Smiod   /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1819*3d8817e4Smiod      insns.  */
1820*3d8817e4Smiod   if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
1821*3d8817e4Smiod     return 1;
1822*3d8817e4Smiod   else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
1823*3d8817e4Smiod     return -1;
1824*3d8817e4Smiod 
1825*3d8817e4Smiod   r1_is_reg
1826*3d8817e4Smiod     = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
1827*3d8817e4Smiod        || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
1828*3d8817e4Smiod   r2_is_reg
1829*3d8817e4Smiod     = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
1830*3d8817e4Smiod        || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
1831*3d8817e4Smiod   if (r1_is_reg != r2_is_reg)
1832*3d8817e4Smiod     return r2_is_reg - r1_is_reg;
1833*3d8817e4Smiod 
1834*3d8817e4Smiod   /* Neither or both are register relocs.  Then sort on full offset.  */
1835*3d8817e4Smiod   if (r1->r_offset > r2->r_offset)
1836*3d8817e4Smiod     return 1;
1837*3d8817e4Smiod   else if (r1->r_offset < r2->r_offset)
1838*3d8817e4Smiod     return -1;
1839*3d8817e4Smiod   return 0;
1840*3d8817e4Smiod }
1841*3d8817e4Smiod 
1842*3d8817e4Smiod /* Subset of mmix_elf_check_relocs, common to ELF and mmo linking.  */
1843*3d8817e4Smiod 
1844*3d8817e4Smiod static bfd_boolean
mmix_elf_check_common_relocs(abfd,info,sec,relocs)1845*3d8817e4Smiod mmix_elf_check_common_relocs  (abfd, info, sec, relocs)
1846*3d8817e4Smiod      bfd *abfd;
1847*3d8817e4Smiod      struct bfd_link_info *info;
1848*3d8817e4Smiod      asection *sec;
1849*3d8817e4Smiod      const Elf_Internal_Rela *relocs;
1850*3d8817e4Smiod {
1851*3d8817e4Smiod   bfd *bpo_greg_owner = NULL;
1852*3d8817e4Smiod   asection *allocated_gregs_section = NULL;
1853*3d8817e4Smiod   struct bpo_greg_section_info *gregdata = NULL;
1854*3d8817e4Smiod   struct bpo_reloc_section_info *bpodata = NULL;
1855*3d8817e4Smiod   const Elf_Internal_Rela *rel;
1856*3d8817e4Smiod   const Elf_Internal_Rela *rel_end;
1857*3d8817e4Smiod 
1858*3d8817e4Smiod   /* We currently have to abuse this COFF-specific member, since there's
1859*3d8817e4Smiod      no target-machine-dedicated member.  There's no alternative outside
1860*3d8817e4Smiod      the bfd_link_info struct; we can't specialize a hash-table since
1861*3d8817e4Smiod      they're different between ELF and mmo.  */
1862*3d8817e4Smiod   bpo_greg_owner = (bfd *) info->base_file;
1863*3d8817e4Smiod 
1864*3d8817e4Smiod   rel_end = relocs + sec->reloc_count;
1865*3d8817e4Smiod   for (rel = relocs; rel < rel_end; rel++)
1866*3d8817e4Smiod     {
1867*3d8817e4Smiod       switch (ELF64_R_TYPE (rel->r_info))
1868*3d8817e4Smiod         {
1869*3d8817e4Smiod 	  /* This relocation causes a GREG allocation.  We need to count
1870*3d8817e4Smiod 	     them, and we need to create a section for them, so we need an
1871*3d8817e4Smiod 	     object to fake as the owner of that section.  We can't use
1872*3d8817e4Smiod 	     the ELF dynobj for this, since the ELF bits assume lots of
1873*3d8817e4Smiod 	     DSO-related stuff if that member is non-NULL.  */
1874*3d8817e4Smiod 	case R_MMIX_BASE_PLUS_OFFSET:
1875*3d8817e4Smiod 	  /* We don't do anything with this reloc for a relocatable link.  */
1876*3d8817e4Smiod 	  if (info->relocatable)
1877*3d8817e4Smiod 	    break;
1878*3d8817e4Smiod 
1879*3d8817e4Smiod 	  if (bpo_greg_owner == NULL)
1880*3d8817e4Smiod 	    {
1881*3d8817e4Smiod 	      bpo_greg_owner = abfd;
1882*3d8817e4Smiod 	      info->base_file = (PTR) bpo_greg_owner;
1883*3d8817e4Smiod 	    }
1884*3d8817e4Smiod 
1885*3d8817e4Smiod 	  if (allocated_gregs_section == NULL)
1886*3d8817e4Smiod 	    allocated_gregs_section
1887*3d8817e4Smiod 	      = bfd_get_section_by_name (bpo_greg_owner,
1888*3d8817e4Smiod 					 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
1889*3d8817e4Smiod 
1890*3d8817e4Smiod 	  if (allocated_gregs_section == NULL)
1891*3d8817e4Smiod 	    {
1892*3d8817e4Smiod 	      allocated_gregs_section
1893*3d8817e4Smiod 		= bfd_make_section_with_flags (bpo_greg_owner,
1894*3d8817e4Smiod 					       MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME,
1895*3d8817e4Smiod 					       (SEC_HAS_CONTENTS
1896*3d8817e4Smiod 						| SEC_IN_MEMORY
1897*3d8817e4Smiod 						| SEC_LINKER_CREATED));
1898*3d8817e4Smiod 	      /* Setting both SEC_ALLOC and SEC_LOAD means the section is
1899*3d8817e4Smiod 		 treated like any other section, and we'd get errors for
1900*3d8817e4Smiod 		 address overlap with the text section.  Let's set none of
1901*3d8817e4Smiod 		 those flags, as that is what currently happens for usual
1902*3d8817e4Smiod 		 GREG allocations, and that works.  */
1903*3d8817e4Smiod 	      if (allocated_gregs_section == NULL
1904*3d8817e4Smiod 		  || !bfd_set_section_alignment (bpo_greg_owner,
1905*3d8817e4Smiod 						 allocated_gregs_section,
1906*3d8817e4Smiod 						 3))
1907*3d8817e4Smiod 		return FALSE;
1908*3d8817e4Smiod 
1909*3d8817e4Smiod 	      gregdata = (struct bpo_greg_section_info *)
1910*3d8817e4Smiod 		bfd_zalloc (bpo_greg_owner, sizeof (struct bpo_greg_section_info));
1911*3d8817e4Smiod 	      if (gregdata == NULL)
1912*3d8817e4Smiod 		return FALSE;
1913*3d8817e4Smiod 	      mmix_elf_section_data (allocated_gregs_section)->bpo.greg
1914*3d8817e4Smiod 		= gregdata;
1915*3d8817e4Smiod 	    }
1916*3d8817e4Smiod 	  else if (gregdata == NULL)
1917*3d8817e4Smiod 	    gregdata
1918*3d8817e4Smiod 	      = mmix_elf_section_data (allocated_gregs_section)->bpo.greg;
1919*3d8817e4Smiod 
1920*3d8817e4Smiod 	  /* Get ourselves some auxiliary info for the BPO-relocs.  */
1921*3d8817e4Smiod 	  if (bpodata == NULL)
1922*3d8817e4Smiod 	    {
1923*3d8817e4Smiod 	      /* No use doing a separate iteration pass to find the upper
1924*3d8817e4Smiod 		 limit - just use the number of relocs.  */
1925*3d8817e4Smiod 	      bpodata = (struct bpo_reloc_section_info *)
1926*3d8817e4Smiod 		bfd_alloc (bpo_greg_owner,
1927*3d8817e4Smiod 			   sizeof (struct bpo_reloc_section_info)
1928*3d8817e4Smiod 			   * (sec->reloc_count + 1));
1929*3d8817e4Smiod 	      if (bpodata == NULL)
1930*3d8817e4Smiod 		return FALSE;
1931*3d8817e4Smiod 	      mmix_elf_section_data (sec)->bpo.reloc = bpodata;
1932*3d8817e4Smiod 	      bpodata->first_base_plus_offset_reloc
1933*3d8817e4Smiod 		= bpodata->bpo_index
1934*3d8817e4Smiod 		= gregdata->n_max_bpo_relocs;
1935*3d8817e4Smiod 	      bpodata->bpo_greg_section
1936*3d8817e4Smiod 		= allocated_gregs_section;
1937*3d8817e4Smiod 	      bpodata->n_bpo_relocs_this_section = 0;
1938*3d8817e4Smiod 	    }
1939*3d8817e4Smiod 
1940*3d8817e4Smiod 	  bpodata->n_bpo_relocs_this_section++;
1941*3d8817e4Smiod 	  gregdata->n_max_bpo_relocs++;
1942*3d8817e4Smiod 
1943*3d8817e4Smiod 	  /* We don't get another chance to set this before GC; we've not
1944*3d8817e4Smiod 	     set up any hook that runs before GC.  */
1945*3d8817e4Smiod 	  gregdata->n_bpo_relocs
1946*3d8817e4Smiod 	    = gregdata->n_max_bpo_relocs;
1947*3d8817e4Smiod 	  break;
1948*3d8817e4Smiod 
1949*3d8817e4Smiod 	case R_MMIX_PUSHJ_STUBBABLE:
1950*3d8817e4Smiod 	  mmix_elf_section_data (sec)->pjs.n_pushj_relocs++;
1951*3d8817e4Smiod 	  break;
1952*3d8817e4Smiod 	}
1953*3d8817e4Smiod     }
1954*3d8817e4Smiod 
1955*3d8817e4Smiod   /* Allocate per-reloc stub storage and initialize it to the max stub
1956*3d8817e4Smiod      size.  */
1957*3d8817e4Smiod   if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs != 0)
1958*3d8817e4Smiod     {
1959*3d8817e4Smiod       size_t i;
1960*3d8817e4Smiod 
1961*3d8817e4Smiod       mmix_elf_section_data (sec)->pjs.stub_size
1962*3d8817e4Smiod 	= bfd_alloc (abfd, mmix_elf_section_data (sec)->pjs.n_pushj_relocs
1963*3d8817e4Smiod 		     * sizeof (mmix_elf_section_data (sec)
1964*3d8817e4Smiod 			       ->pjs.stub_size[0]));
1965*3d8817e4Smiod       if (mmix_elf_section_data (sec)->pjs.stub_size == NULL)
1966*3d8817e4Smiod 	return FALSE;
1967*3d8817e4Smiod 
1968*3d8817e4Smiod       for (i = 0; i < mmix_elf_section_data (sec)->pjs.n_pushj_relocs; i++)
1969*3d8817e4Smiod 	mmix_elf_section_data (sec)->pjs.stub_size[i] = MAX_PUSHJ_STUB_SIZE;
1970*3d8817e4Smiod     }
1971*3d8817e4Smiod 
1972*3d8817e4Smiod   return TRUE;
1973*3d8817e4Smiod }
1974*3d8817e4Smiod 
1975*3d8817e4Smiod /* Look through the relocs for a section during the first phase.  */
1976*3d8817e4Smiod 
1977*3d8817e4Smiod static bfd_boolean
mmix_elf_check_relocs(abfd,info,sec,relocs)1978*3d8817e4Smiod mmix_elf_check_relocs (abfd, info, sec, relocs)
1979*3d8817e4Smiod      bfd *abfd;
1980*3d8817e4Smiod      struct bfd_link_info *info;
1981*3d8817e4Smiod      asection *sec;
1982*3d8817e4Smiod      const Elf_Internal_Rela *relocs;
1983*3d8817e4Smiod {
1984*3d8817e4Smiod   Elf_Internal_Shdr *symtab_hdr;
1985*3d8817e4Smiod   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
1986*3d8817e4Smiod   const Elf_Internal_Rela *rel;
1987*3d8817e4Smiod   const Elf_Internal_Rela *rel_end;
1988*3d8817e4Smiod 
1989*3d8817e4Smiod   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1990*3d8817e4Smiod   sym_hashes = elf_sym_hashes (abfd);
1991*3d8817e4Smiod   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf64_External_Sym);
1992*3d8817e4Smiod   if (!elf_bad_symtab (abfd))
1993*3d8817e4Smiod     sym_hashes_end -= symtab_hdr->sh_info;
1994*3d8817e4Smiod 
1995*3d8817e4Smiod   /* First we sort the relocs so that any register relocs come before
1996*3d8817e4Smiod      expansion-relocs to the same insn.  FIXME: Not done for mmo.  */
1997*3d8817e4Smiod   qsort ((PTR) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1998*3d8817e4Smiod 	 mmix_elf_sort_relocs);
1999*3d8817e4Smiod 
2000*3d8817e4Smiod   /* Do the common part.  */
2001*3d8817e4Smiod   if (!mmix_elf_check_common_relocs (abfd, info, sec, relocs))
2002*3d8817e4Smiod     return FALSE;
2003*3d8817e4Smiod 
2004*3d8817e4Smiod   if (info->relocatable)
2005*3d8817e4Smiod     return TRUE;
2006*3d8817e4Smiod 
2007*3d8817e4Smiod   rel_end = relocs + sec->reloc_count;
2008*3d8817e4Smiod   for (rel = relocs; rel < rel_end; rel++)
2009*3d8817e4Smiod     {
2010*3d8817e4Smiod       struct elf_link_hash_entry *h;
2011*3d8817e4Smiod       unsigned long r_symndx;
2012*3d8817e4Smiod 
2013*3d8817e4Smiod       r_symndx = ELF64_R_SYM (rel->r_info);
2014*3d8817e4Smiod       if (r_symndx < symtab_hdr->sh_info)
2015*3d8817e4Smiod         h = NULL;
2016*3d8817e4Smiod       else
2017*3d8817e4Smiod 	{
2018*3d8817e4Smiod 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2019*3d8817e4Smiod 	  while (h->root.type == bfd_link_hash_indirect
2020*3d8817e4Smiod 		 || h->root.type == bfd_link_hash_warning)
2021*3d8817e4Smiod 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2022*3d8817e4Smiod 	}
2023*3d8817e4Smiod 
2024*3d8817e4Smiod       switch (ELF64_R_TYPE (rel->r_info))
2025*3d8817e4Smiod 	{
2026*3d8817e4Smiod         /* This relocation describes the C++ object vtable hierarchy.
2027*3d8817e4Smiod            Reconstruct it for later use during GC.  */
2028*3d8817e4Smiod         case R_MMIX_GNU_VTINHERIT:
2029*3d8817e4Smiod           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2030*3d8817e4Smiod             return FALSE;
2031*3d8817e4Smiod           break;
2032*3d8817e4Smiod 
2033*3d8817e4Smiod         /* This relocation describes which C++ vtable entries are actually
2034*3d8817e4Smiod            used.  Record for later use during GC.  */
2035*3d8817e4Smiod         case R_MMIX_GNU_VTENTRY:
2036*3d8817e4Smiod           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2037*3d8817e4Smiod             return FALSE;
2038*3d8817e4Smiod           break;
2039*3d8817e4Smiod 	}
2040*3d8817e4Smiod     }
2041*3d8817e4Smiod 
2042*3d8817e4Smiod   return TRUE;
2043*3d8817e4Smiod }
2044*3d8817e4Smiod 
2045*3d8817e4Smiod /* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
2046*3d8817e4Smiod    Copied from elf_link_add_object_symbols.  */
2047*3d8817e4Smiod 
2048*3d8817e4Smiod bfd_boolean
_bfd_mmix_check_all_relocs(abfd,info)2049*3d8817e4Smiod _bfd_mmix_check_all_relocs (abfd, info)
2050*3d8817e4Smiod      bfd *abfd;
2051*3d8817e4Smiod      struct bfd_link_info *info;
2052*3d8817e4Smiod {
2053*3d8817e4Smiod   asection *o;
2054*3d8817e4Smiod 
2055*3d8817e4Smiod   for (o = abfd->sections; o != NULL; o = o->next)
2056*3d8817e4Smiod     {
2057*3d8817e4Smiod       Elf_Internal_Rela *internal_relocs;
2058*3d8817e4Smiod       bfd_boolean ok;
2059*3d8817e4Smiod 
2060*3d8817e4Smiod       if ((o->flags & SEC_RELOC) == 0
2061*3d8817e4Smiod 	  || o->reloc_count == 0
2062*3d8817e4Smiod 	  || ((info->strip == strip_all || info->strip == strip_debugger)
2063*3d8817e4Smiod 	      && (o->flags & SEC_DEBUGGING) != 0)
2064*3d8817e4Smiod 	  || bfd_is_abs_section (o->output_section))
2065*3d8817e4Smiod 	continue;
2066*3d8817e4Smiod 
2067*3d8817e4Smiod       internal_relocs
2068*3d8817e4Smiod 	= _bfd_elf_link_read_relocs (abfd, o, (PTR) NULL,
2069*3d8817e4Smiod 				     (Elf_Internal_Rela *) NULL,
2070*3d8817e4Smiod 				     info->keep_memory);
2071*3d8817e4Smiod       if (internal_relocs == NULL)
2072*3d8817e4Smiod 	return FALSE;
2073*3d8817e4Smiod 
2074*3d8817e4Smiod       ok = mmix_elf_check_common_relocs (abfd, info, o, internal_relocs);
2075*3d8817e4Smiod 
2076*3d8817e4Smiod       if (! info->keep_memory)
2077*3d8817e4Smiod 	free (internal_relocs);
2078*3d8817e4Smiod 
2079*3d8817e4Smiod       if (! ok)
2080*3d8817e4Smiod 	return FALSE;
2081*3d8817e4Smiod     }
2082*3d8817e4Smiod 
2083*3d8817e4Smiod   return TRUE;
2084*3d8817e4Smiod }
2085*3d8817e4Smiod 
2086*3d8817e4Smiod /* Change symbols relative to the reg contents section to instead be to
2087*3d8817e4Smiod    the register section, and scale them down to correspond to the register
2088*3d8817e4Smiod    number.  */
2089*3d8817e4Smiod 
2090*3d8817e4Smiod static bfd_boolean
mmix_elf_link_output_symbol_hook(info,name,sym,input_sec,h)2091*3d8817e4Smiod mmix_elf_link_output_symbol_hook (info, name, sym, input_sec, h)
2092*3d8817e4Smiod      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2093*3d8817e4Smiod      const char *name ATTRIBUTE_UNUSED;
2094*3d8817e4Smiod      Elf_Internal_Sym *sym;
2095*3d8817e4Smiod      asection *input_sec;
2096*3d8817e4Smiod      struct elf_link_hash_entry *h ATTRIBUTE_UNUSED;
2097*3d8817e4Smiod {
2098*3d8817e4Smiod   if (input_sec != NULL
2099*3d8817e4Smiod       && input_sec->name != NULL
2100*3d8817e4Smiod       && ELF_ST_TYPE (sym->st_info) != STT_SECTION
2101*3d8817e4Smiod       && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2102*3d8817e4Smiod     {
2103*3d8817e4Smiod       sym->st_value /= 8;
2104*3d8817e4Smiod       sym->st_shndx = SHN_REGISTER;
2105*3d8817e4Smiod     }
2106*3d8817e4Smiod 
2107*3d8817e4Smiod   return TRUE;
2108*3d8817e4Smiod }
2109*3d8817e4Smiod 
2110*3d8817e4Smiod /* We fake a register section that holds values that are register numbers.
2111*3d8817e4Smiod    Having a SHN_REGISTER and register section translates better to other
2112*3d8817e4Smiod    formats (e.g. mmo) than for example a STT_REGISTER attribute.
2113*3d8817e4Smiod    This section faking is based on a construct in elf32-mips.c.  */
2114*3d8817e4Smiod static asection mmix_elf_reg_section;
2115*3d8817e4Smiod static asymbol mmix_elf_reg_section_symbol;
2116*3d8817e4Smiod static asymbol *mmix_elf_reg_section_symbol_ptr;
2117*3d8817e4Smiod 
2118*3d8817e4Smiod /* Handle the special section numbers that a symbol may use.  */
2119*3d8817e4Smiod 
2120*3d8817e4Smiod void
mmix_elf_symbol_processing(abfd,asym)2121*3d8817e4Smiod mmix_elf_symbol_processing (abfd, asym)
2122*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
2123*3d8817e4Smiod      asymbol *asym;
2124*3d8817e4Smiod {
2125*3d8817e4Smiod   elf_symbol_type *elfsym;
2126*3d8817e4Smiod 
2127*3d8817e4Smiod   elfsym = (elf_symbol_type *) asym;
2128*3d8817e4Smiod   switch (elfsym->internal_elf_sym.st_shndx)
2129*3d8817e4Smiod     {
2130*3d8817e4Smiod     case SHN_REGISTER:
2131*3d8817e4Smiod       if (mmix_elf_reg_section.name == NULL)
2132*3d8817e4Smiod 	{
2133*3d8817e4Smiod 	  /* Initialize the register section.  */
2134*3d8817e4Smiod 	  mmix_elf_reg_section.name = MMIX_REG_SECTION_NAME;
2135*3d8817e4Smiod 	  mmix_elf_reg_section.flags = SEC_NO_FLAGS;
2136*3d8817e4Smiod 	  mmix_elf_reg_section.output_section = &mmix_elf_reg_section;
2137*3d8817e4Smiod 	  mmix_elf_reg_section.symbol = &mmix_elf_reg_section_symbol;
2138*3d8817e4Smiod 	  mmix_elf_reg_section.symbol_ptr_ptr = &mmix_elf_reg_section_symbol_ptr;
2139*3d8817e4Smiod 	  mmix_elf_reg_section_symbol.name = MMIX_REG_SECTION_NAME;
2140*3d8817e4Smiod 	  mmix_elf_reg_section_symbol.flags = BSF_SECTION_SYM;
2141*3d8817e4Smiod 	  mmix_elf_reg_section_symbol.section = &mmix_elf_reg_section;
2142*3d8817e4Smiod 	  mmix_elf_reg_section_symbol_ptr = &mmix_elf_reg_section_symbol;
2143*3d8817e4Smiod 	}
2144*3d8817e4Smiod       asym->section = &mmix_elf_reg_section;
2145*3d8817e4Smiod       break;
2146*3d8817e4Smiod 
2147*3d8817e4Smiod     default:
2148*3d8817e4Smiod       break;
2149*3d8817e4Smiod     }
2150*3d8817e4Smiod }
2151*3d8817e4Smiod 
2152*3d8817e4Smiod /* Given a BFD section, try to locate the corresponding ELF section
2153*3d8817e4Smiod    index.  */
2154*3d8817e4Smiod 
2155*3d8817e4Smiod static bfd_boolean
mmix_elf_section_from_bfd_section(abfd,sec,retval)2156*3d8817e4Smiod mmix_elf_section_from_bfd_section (abfd, sec, retval)
2157*3d8817e4Smiod      bfd *                 abfd ATTRIBUTE_UNUSED;
2158*3d8817e4Smiod      asection *            sec;
2159*3d8817e4Smiod      int *                 retval;
2160*3d8817e4Smiod {
2161*3d8817e4Smiod   if (strcmp (bfd_get_section_name (abfd, sec), MMIX_REG_SECTION_NAME) == 0)
2162*3d8817e4Smiod     *retval = SHN_REGISTER;
2163*3d8817e4Smiod   else
2164*3d8817e4Smiod     return FALSE;
2165*3d8817e4Smiod 
2166*3d8817e4Smiod   return TRUE;
2167*3d8817e4Smiod }
2168*3d8817e4Smiod 
2169*3d8817e4Smiod /* Hook called by the linker routine which adds symbols from an object
2170*3d8817e4Smiod    file.  We must handle the special SHN_REGISTER section number here.
2171*3d8817e4Smiod 
2172*3d8817e4Smiod    We also check that we only have *one* each of the section-start
2173*3d8817e4Smiod    symbols, since otherwise having two with the same value would cause
2174*3d8817e4Smiod    them to be "merged", but with the contents serialized.  */
2175*3d8817e4Smiod 
2176*3d8817e4Smiod bfd_boolean
mmix_elf_add_symbol_hook(abfd,info,sym,namep,flagsp,secp,valp)2177*3d8817e4Smiod mmix_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2178*3d8817e4Smiod      bfd *abfd;
2179*3d8817e4Smiod      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2180*3d8817e4Smiod      Elf_Internal_Sym *sym;
2181*3d8817e4Smiod      const char **namep ATTRIBUTE_UNUSED;
2182*3d8817e4Smiod      flagword *flagsp ATTRIBUTE_UNUSED;
2183*3d8817e4Smiod      asection **secp;
2184*3d8817e4Smiod      bfd_vma *valp ATTRIBUTE_UNUSED;
2185*3d8817e4Smiod {
2186*3d8817e4Smiod   if (sym->st_shndx == SHN_REGISTER)
2187*3d8817e4Smiod     {
2188*3d8817e4Smiod       *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2189*3d8817e4Smiod       (*secp)->flags |= SEC_LINKER_CREATED;
2190*3d8817e4Smiod     }
2191*3d8817e4Smiod   else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
2192*3d8817e4Smiod 	   && strncmp (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
2193*3d8817e4Smiod 		       strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)) == 0)
2194*3d8817e4Smiod     {
2195*3d8817e4Smiod       /* See if we have another one.  */
2196*3d8817e4Smiod       struct bfd_link_hash_entry *h = bfd_link_hash_lookup (info->hash,
2197*3d8817e4Smiod 							    *namep,
2198*3d8817e4Smiod 							    FALSE,
2199*3d8817e4Smiod 							    FALSE,
2200*3d8817e4Smiod 							    FALSE);
2201*3d8817e4Smiod 
2202*3d8817e4Smiod       if (h != NULL && h->type != bfd_link_hash_undefined)
2203*3d8817e4Smiod 	{
2204*3d8817e4Smiod 	  /* How do we get the asymbol (or really: the filename) from h?
2205*3d8817e4Smiod 	     h->u.def.section->owner is NULL.  */
2206*3d8817e4Smiod 	  ((*_bfd_error_handler)
2207*3d8817e4Smiod 	   (_("%s: Error: multiple definition of `%s'; start of %s is set in a earlier linked file\n"),
2208*3d8817e4Smiod 	    bfd_get_filename (abfd), *namep,
2209*3d8817e4Smiod 	    *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)));
2210*3d8817e4Smiod 	   bfd_set_error (bfd_error_bad_value);
2211*3d8817e4Smiod 	   return FALSE;
2212*3d8817e4Smiod 	}
2213*3d8817e4Smiod     }
2214*3d8817e4Smiod 
2215*3d8817e4Smiod   return TRUE;
2216*3d8817e4Smiod }
2217*3d8817e4Smiod 
2218*3d8817e4Smiod /* We consider symbols matching "L.*:[0-9]+" to be local symbols.  */
2219*3d8817e4Smiod 
2220*3d8817e4Smiod bfd_boolean
mmix_elf_is_local_label_name(abfd,name)2221*3d8817e4Smiod mmix_elf_is_local_label_name (abfd, name)
2222*3d8817e4Smiod      bfd *abfd;
2223*3d8817e4Smiod      const char *name;
2224*3d8817e4Smiod {
2225*3d8817e4Smiod   const char *colpos;
2226*3d8817e4Smiod   int digits;
2227*3d8817e4Smiod 
2228*3d8817e4Smiod   /* Also include the default local-label definition.  */
2229*3d8817e4Smiod   if (_bfd_elf_is_local_label_name (abfd, name))
2230*3d8817e4Smiod     return TRUE;
2231*3d8817e4Smiod 
2232*3d8817e4Smiod   if (*name != 'L')
2233*3d8817e4Smiod     return FALSE;
2234*3d8817e4Smiod 
2235*3d8817e4Smiod   /* If there's no ":", or more than one, it's not a local symbol.  */
2236*3d8817e4Smiod   colpos = strchr (name, ':');
2237*3d8817e4Smiod   if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
2238*3d8817e4Smiod     return FALSE;
2239*3d8817e4Smiod 
2240*3d8817e4Smiod   /* Check that there are remaining characters and that they are digits.  */
2241*3d8817e4Smiod   if (colpos[1] == 0)
2242*3d8817e4Smiod     return FALSE;
2243*3d8817e4Smiod 
2244*3d8817e4Smiod   digits = strspn (colpos + 1, "0123456789");
2245*3d8817e4Smiod   return digits != 0 && colpos[1 + digits] == 0;
2246*3d8817e4Smiod }
2247*3d8817e4Smiod 
2248*3d8817e4Smiod /* We get rid of the register section here.  */
2249*3d8817e4Smiod 
2250*3d8817e4Smiod bfd_boolean
mmix_elf_final_link(abfd,info)2251*3d8817e4Smiod mmix_elf_final_link (abfd, info)
2252*3d8817e4Smiod      bfd *abfd;
2253*3d8817e4Smiod      struct bfd_link_info *info;
2254*3d8817e4Smiod {
2255*3d8817e4Smiod   /* We never output a register section, though we create one for
2256*3d8817e4Smiod      temporary measures.  Check that nobody entered contents into it.  */
2257*3d8817e4Smiod   asection *reg_section;
2258*3d8817e4Smiod 
2259*3d8817e4Smiod   reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
2260*3d8817e4Smiod 
2261*3d8817e4Smiod   if (reg_section != NULL)
2262*3d8817e4Smiod     {
2263*3d8817e4Smiod       /* FIXME: Pass error state gracefully.  */
2264*3d8817e4Smiod       if (bfd_get_section_flags (abfd, reg_section) & SEC_HAS_CONTENTS)
2265*3d8817e4Smiod 	_bfd_abort (__FILE__, __LINE__, _("Register section has contents\n"));
2266*3d8817e4Smiod 
2267*3d8817e4Smiod       /* Really remove the section, if it hasn't already been done.  */
2268*3d8817e4Smiod       if (!bfd_section_removed_from_list (abfd, reg_section))
2269*3d8817e4Smiod 	{
2270*3d8817e4Smiod 	  bfd_section_list_remove (abfd, reg_section);
2271*3d8817e4Smiod 	  --abfd->section_count;
2272*3d8817e4Smiod 	}
2273*3d8817e4Smiod     }
2274*3d8817e4Smiod 
2275*3d8817e4Smiod   if (! bfd_elf_final_link (abfd, info))
2276*3d8817e4Smiod     return FALSE;
2277*3d8817e4Smiod 
2278*3d8817e4Smiod   /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
2279*3d8817e4Smiod      the regular linker machinery.  We do it here, like other targets with
2280*3d8817e4Smiod      special sections.  */
2281*3d8817e4Smiod   if (info->base_file != NULL)
2282*3d8817e4Smiod     {
2283*3d8817e4Smiod       asection *greg_section
2284*3d8817e4Smiod 	= bfd_get_section_by_name ((bfd *) info->base_file,
2285*3d8817e4Smiod 				   MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2286*3d8817e4Smiod       if (!bfd_set_section_contents (abfd,
2287*3d8817e4Smiod 				     greg_section->output_section,
2288*3d8817e4Smiod 				     greg_section->contents,
2289*3d8817e4Smiod 				     (file_ptr) greg_section->output_offset,
2290*3d8817e4Smiod 				     greg_section->size))
2291*3d8817e4Smiod 	return FALSE;
2292*3d8817e4Smiod     }
2293*3d8817e4Smiod   return TRUE;
2294*3d8817e4Smiod }
2295*3d8817e4Smiod 
2296*3d8817e4Smiod /* We need to include the maximum size of PUSHJ-stubs in the initial
2297*3d8817e4Smiod    section size.  This is expected to shrink during linker relaxation.  */
2298*3d8817e4Smiod 
2299*3d8817e4Smiod static void
mmix_set_relaxable_size(abfd,sec,ptr)2300*3d8817e4Smiod mmix_set_relaxable_size (abfd, sec, ptr)
2301*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
2302*3d8817e4Smiod      asection *sec;
2303*3d8817e4Smiod      void *ptr;
2304*3d8817e4Smiod {
2305*3d8817e4Smiod   struct bfd_link_info *info = ptr;
2306*3d8817e4Smiod 
2307*3d8817e4Smiod   /* Make sure we only do this for section where we know we want this,
2308*3d8817e4Smiod      otherwise we might end up resetting the size of COMMONs.  */
2309*3d8817e4Smiod   if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0)
2310*3d8817e4Smiod     return;
2311*3d8817e4Smiod 
2312*3d8817e4Smiod   sec->rawsize = sec->size;
2313*3d8817e4Smiod   sec->size += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2314*3d8817e4Smiod 		* MAX_PUSHJ_STUB_SIZE);
2315*3d8817e4Smiod 
2316*3d8817e4Smiod   /* For use in relocatable link, we start with a max stubs size.  See
2317*3d8817e4Smiod      mmix_elf_relax_section.  */
2318*3d8817e4Smiod   if (info->relocatable && sec->output_section)
2319*3d8817e4Smiod     mmix_elf_section_data (sec->output_section)->pjs.stubs_size_sum
2320*3d8817e4Smiod       += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2321*3d8817e4Smiod 	  * MAX_PUSHJ_STUB_SIZE);
2322*3d8817e4Smiod }
2323*3d8817e4Smiod 
2324*3d8817e4Smiod /* Initialize stuff for the linker-generated GREGs to match
2325*3d8817e4Smiod    R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker.  */
2326*3d8817e4Smiod 
2327*3d8817e4Smiod bfd_boolean
_bfd_mmix_before_linker_allocation(abfd,info)2328*3d8817e4Smiod _bfd_mmix_before_linker_allocation (abfd, info)
2329*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
2330*3d8817e4Smiod      struct bfd_link_info *info;
2331*3d8817e4Smiod {
2332*3d8817e4Smiod   asection *bpo_gregs_section;
2333*3d8817e4Smiod   bfd *bpo_greg_owner;
2334*3d8817e4Smiod   struct bpo_greg_section_info *gregdata;
2335*3d8817e4Smiod   size_t n_gregs;
2336*3d8817e4Smiod   bfd_vma gregs_size;
2337*3d8817e4Smiod   size_t i;
2338*3d8817e4Smiod   size_t *bpo_reloc_indexes;
2339*3d8817e4Smiod   bfd *ibfd;
2340*3d8817e4Smiod 
2341*3d8817e4Smiod   /* Set the initial size of sections.  */
2342*3d8817e4Smiod   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2343*3d8817e4Smiod     bfd_map_over_sections (ibfd, mmix_set_relaxable_size, info);
2344*3d8817e4Smiod 
2345*3d8817e4Smiod   /* The bpo_greg_owner bfd is supposed to have been set by
2346*3d8817e4Smiod      mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
2347*3d8817e4Smiod      If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET.  */
2348*3d8817e4Smiod   bpo_greg_owner = (bfd *) info->base_file;
2349*3d8817e4Smiod   if (bpo_greg_owner == NULL)
2350*3d8817e4Smiod     return TRUE;
2351*3d8817e4Smiod 
2352*3d8817e4Smiod   bpo_gregs_section
2353*3d8817e4Smiod     = bfd_get_section_by_name (bpo_greg_owner,
2354*3d8817e4Smiod 			       MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2355*3d8817e4Smiod 
2356*3d8817e4Smiod   if (bpo_gregs_section == NULL)
2357*3d8817e4Smiod     return TRUE;
2358*3d8817e4Smiod 
2359*3d8817e4Smiod   /* We use the target-data handle in the ELF section data.  */
2360*3d8817e4Smiod   gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2361*3d8817e4Smiod   if (gregdata == NULL)
2362*3d8817e4Smiod     return FALSE;
2363*3d8817e4Smiod 
2364*3d8817e4Smiod   n_gregs = gregdata->n_bpo_relocs;
2365*3d8817e4Smiod   gregdata->n_allocated_bpo_gregs = n_gregs;
2366*3d8817e4Smiod 
2367*3d8817e4Smiod   /* When this reaches zero during relaxation, all entries have been
2368*3d8817e4Smiod      filled in and the size of the linker gregs can be calculated.  */
2369*3d8817e4Smiod   gregdata->n_remaining_bpo_relocs_this_relaxation_round = n_gregs;
2370*3d8817e4Smiod 
2371*3d8817e4Smiod   /* Set the zeroth-order estimate for the GREGs size.  */
2372*3d8817e4Smiod   gregs_size = n_gregs * 8;
2373*3d8817e4Smiod 
2374*3d8817e4Smiod   if (!bfd_set_section_size (bpo_greg_owner, bpo_gregs_section, gregs_size))
2375*3d8817e4Smiod     return FALSE;
2376*3d8817e4Smiod 
2377*3d8817e4Smiod   /* Allocate and set up the GREG arrays.  They're filled in at relaxation
2378*3d8817e4Smiod      time.  Note that we must use the max number ever noted for the array,
2379*3d8817e4Smiod      since the index numbers were created before GC.  */
2380*3d8817e4Smiod   gregdata->reloc_request
2381*3d8817e4Smiod     = bfd_zalloc (bpo_greg_owner,
2382*3d8817e4Smiod 		  sizeof (struct bpo_reloc_request)
2383*3d8817e4Smiod 		  * gregdata->n_max_bpo_relocs);
2384*3d8817e4Smiod 
2385*3d8817e4Smiod   gregdata->bpo_reloc_indexes
2386*3d8817e4Smiod     = bpo_reloc_indexes
2387*3d8817e4Smiod     = bfd_alloc (bpo_greg_owner,
2388*3d8817e4Smiod 		 gregdata->n_max_bpo_relocs
2389*3d8817e4Smiod 		 * sizeof (size_t));
2390*3d8817e4Smiod   if (bpo_reloc_indexes == NULL)
2391*3d8817e4Smiod     return FALSE;
2392*3d8817e4Smiod 
2393*3d8817e4Smiod   /* The default order is an identity mapping.  */
2394*3d8817e4Smiod   for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2395*3d8817e4Smiod     {
2396*3d8817e4Smiod       bpo_reloc_indexes[i] = i;
2397*3d8817e4Smiod       gregdata->reloc_request[i].bpo_reloc_no = i;
2398*3d8817e4Smiod     }
2399*3d8817e4Smiod 
2400*3d8817e4Smiod   return TRUE;
2401*3d8817e4Smiod }
2402*3d8817e4Smiod 
2403*3d8817e4Smiod /* Fill in contents in the linker allocated gregs.  Everything is
2404*3d8817e4Smiod    calculated at this point; we just move the contents into place here.  */
2405*3d8817e4Smiod 
2406*3d8817e4Smiod bfd_boolean
_bfd_mmix_after_linker_allocation(abfd,link_info)2407*3d8817e4Smiod _bfd_mmix_after_linker_allocation (abfd, link_info)
2408*3d8817e4Smiod      bfd *abfd ATTRIBUTE_UNUSED;
2409*3d8817e4Smiod      struct bfd_link_info *link_info;
2410*3d8817e4Smiod {
2411*3d8817e4Smiod   asection *bpo_gregs_section;
2412*3d8817e4Smiod   bfd *bpo_greg_owner;
2413*3d8817e4Smiod   struct bpo_greg_section_info *gregdata;
2414*3d8817e4Smiod   size_t n_gregs;
2415*3d8817e4Smiod   size_t i, j;
2416*3d8817e4Smiod   size_t lastreg;
2417*3d8817e4Smiod   bfd_byte *contents;
2418*3d8817e4Smiod 
2419*3d8817e4Smiod   /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
2420*3d8817e4Smiod      when the first R_MMIX_BASE_PLUS_OFFSET is seen.  If there is no such
2421*3d8817e4Smiod      object, there was no R_MMIX_BASE_PLUS_OFFSET.  */
2422*3d8817e4Smiod   bpo_greg_owner = (bfd *) link_info->base_file;
2423*3d8817e4Smiod   if (bpo_greg_owner == NULL)
2424*3d8817e4Smiod     return TRUE;
2425*3d8817e4Smiod 
2426*3d8817e4Smiod   bpo_gregs_section
2427*3d8817e4Smiod     = bfd_get_section_by_name (bpo_greg_owner,
2428*3d8817e4Smiod 			       MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2429*3d8817e4Smiod 
2430*3d8817e4Smiod   /* This can't happen without DSO handling.  When DSOs are handled
2431*3d8817e4Smiod      without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
2432*3d8817e4Smiod      section.  */
2433*3d8817e4Smiod   if (bpo_gregs_section == NULL)
2434*3d8817e4Smiod     return TRUE;
2435*3d8817e4Smiod 
2436*3d8817e4Smiod   /* We use the target-data handle in the ELF section data.  */
2437*3d8817e4Smiod 
2438*3d8817e4Smiod   gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2439*3d8817e4Smiod   if (gregdata == NULL)
2440*3d8817e4Smiod     return FALSE;
2441*3d8817e4Smiod 
2442*3d8817e4Smiod   n_gregs = gregdata->n_allocated_bpo_gregs;
2443*3d8817e4Smiod 
2444*3d8817e4Smiod   bpo_gregs_section->contents
2445*3d8817e4Smiod     = contents = bfd_alloc (bpo_greg_owner, bpo_gregs_section->size);
2446*3d8817e4Smiod   if (contents == NULL)
2447*3d8817e4Smiod     return FALSE;
2448*3d8817e4Smiod 
2449*3d8817e4Smiod   /* Sanity check: If these numbers mismatch, some relocation has not been
2450*3d8817e4Smiod      accounted for and the rest of gregdata is probably inconsistent.
2451*3d8817e4Smiod      It's a bug, but it's more helpful to identify it than segfaulting
2452*3d8817e4Smiod      below.  */
2453*3d8817e4Smiod   if (gregdata->n_remaining_bpo_relocs_this_relaxation_round
2454*3d8817e4Smiod       != gregdata->n_bpo_relocs)
2455*3d8817e4Smiod     {
2456*3d8817e4Smiod       (*_bfd_error_handler)
2457*3d8817e4Smiod 	(_("Internal inconsistency: remaining %u != max %u.\n\
2458*3d8817e4Smiod   Please report this bug."),
2459*3d8817e4Smiod 	 gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2460*3d8817e4Smiod 	 gregdata->n_bpo_relocs);
2461*3d8817e4Smiod       return FALSE;
2462*3d8817e4Smiod     }
2463*3d8817e4Smiod 
2464*3d8817e4Smiod   for (lastreg = 255, i = 0, j = 0; j < n_gregs; i++)
2465*3d8817e4Smiod     if (gregdata->reloc_request[i].regindex != lastreg)
2466*3d8817e4Smiod       {
2467*3d8817e4Smiod 	bfd_put_64 (bpo_greg_owner, gregdata->reloc_request[i].value,
2468*3d8817e4Smiod 		    contents + j * 8);
2469*3d8817e4Smiod 	lastreg = gregdata->reloc_request[i].regindex;
2470*3d8817e4Smiod 	j++;
2471*3d8817e4Smiod       }
2472*3d8817e4Smiod 
2473*3d8817e4Smiod   return TRUE;
2474*3d8817e4Smiod }
2475*3d8817e4Smiod 
2476*3d8817e4Smiod /* Sort valid relocs to come before non-valid relocs, then on increasing
2477*3d8817e4Smiod    value.  */
2478*3d8817e4Smiod 
2479*3d8817e4Smiod static int
bpo_reloc_request_sort_fn(p1,p2)2480*3d8817e4Smiod bpo_reloc_request_sort_fn (p1, p2)
2481*3d8817e4Smiod      const PTR p1;
2482*3d8817e4Smiod      const PTR p2;
2483*3d8817e4Smiod {
2484*3d8817e4Smiod   const struct bpo_reloc_request *r1 = (const struct bpo_reloc_request *) p1;
2485*3d8817e4Smiod   const struct bpo_reloc_request *r2 = (const struct bpo_reloc_request *) p2;
2486*3d8817e4Smiod 
2487*3d8817e4Smiod   /* Primary function is validity; non-valid relocs sorted after valid
2488*3d8817e4Smiod      ones.  */
2489*3d8817e4Smiod   if (r1->valid != r2->valid)
2490*3d8817e4Smiod     return r2->valid - r1->valid;
2491*3d8817e4Smiod 
2492*3d8817e4Smiod   /* Then sort on value.  Don't simplify and return just the difference of
2493*3d8817e4Smiod      the values: the upper bits of the 64-bit value would be truncated on
2494*3d8817e4Smiod      a host with 32-bit ints.  */
2495*3d8817e4Smiod   if (r1->value != r2->value)
2496*3d8817e4Smiod     return r1->value > r2->value ? 1 : -1;
2497*3d8817e4Smiod 
2498*3d8817e4Smiod   /* As a last re-sort, use the relocation number, so we get a stable
2499*3d8817e4Smiod      sort.  The *addresses* aren't stable since items are swapped during
2500*3d8817e4Smiod      sorting.  It depends on the qsort implementation if this actually
2501*3d8817e4Smiod      happens.  */
2502*3d8817e4Smiod   return r1->bpo_reloc_no > r2->bpo_reloc_no
2503*3d8817e4Smiod     ? 1 : (r1->bpo_reloc_no < r2->bpo_reloc_no ? -1 : 0);
2504*3d8817e4Smiod }
2505*3d8817e4Smiod 
2506*3d8817e4Smiod /* For debug use only.  Dumps the global register allocations resulting
2507*3d8817e4Smiod    from base-plus-offset relocs.  */
2508*3d8817e4Smiod 
2509*3d8817e4Smiod void
mmix_dump_bpo_gregs(link_info,pf)2510*3d8817e4Smiod mmix_dump_bpo_gregs (link_info, pf)
2511*3d8817e4Smiod      struct bfd_link_info *link_info;
2512*3d8817e4Smiod      bfd_error_handler_type pf;
2513*3d8817e4Smiod {
2514*3d8817e4Smiod   bfd *bpo_greg_owner;
2515*3d8817e4Smiod   asection *bpo_gregs_section;
2516*3d8817e4Smiod   struct bpo_greg_section_info *gregdata;
2517*3d8817e4Smiod   unsigned int i;
2518*3d8817e4Smiod 
2519*3d8817e4Smiod   if (link_info == NULL || link_info->base_file == NULL)
2520*3d8817e4Smiod     return;
2521*3d8817e4Smiod 
2522*3d8817e4Smiod   bpo_greg_owner = (bfd *) link_info->base_file;
2523*3d8817e4Smiod 
2524*3d8817e4Smiod   bpo_gregs_section
2525*3d8817e4Smiod     = bfd_get_section_by_name (bpo_greg_owner,
2526*3d8817e4Smiod 			       MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2527*3d8817e4Smiod 
2528*3d8817e4Smiod   if (bpo_gregs_section == NULL)
2529*3d8817e4Smiod     return;
2530*3d8817e4Smiod 
2531*3d8817e4Smiod   gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2532*3d8817e4Smiod   if (gregdata == NULL)
2533*3d8817e4Smiod     return;
2534*3d8817e4Smiod 
2535*3d8817e4Smiod   if (pf == NULL)
2536*3d8817e4Smiod     pf = _bfd_error_handler;
2537*3d8817e4Smiod 
2538*3d8817e4Smiod   /* These format strings are not translated.  They are for debug purposes
2539*3d8817e4Smiod      only and never displayed to an end user.  Should they escape, we
2540*3d8817e4Smiod      surely want them in original.  */
2541*3d8817e4Smiod   (*pf) (" n_bpo_relocs: %u\n n_max_bpo_relocs: %u\n n_remain...round: %u\n\
2542*3d8817e4Smiod  n_allocated_bpo_gregs: %u\n", gregdata->n_bpo_relocs,
2543*3d8817e4Smiod      gregdata->n_max_bpo_relocs,
2544*3d8817e4Smiod      gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2545*3d8817e4Smiod      gregdata->n_allocated_bpo_gregs);
2546*3d8817e4Smiod 
2547*3d8817e4Smiod   if (gregdata->reloc_request)
2548*3d8817e4Smiod     for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2549*3d8817e4Smiod       (*pf) ("%4u (%4u)/%4u#%u: 0x%08lx%08lx  r: %3u o: %3u\n",
2550*3d8817e4Smiod 	     i,
2551*3d8817e4Smiod 	     (gregdata->bpo_reloc_indexes != NULL
2552*3d8817e4Smiod 	      ? gregdata->bpo_reloc_indexes[i] : (size_t) -1),
2553*3d8817e4Smiod 	     gregdata->reloc_request[i].bpo_reloc_no,
2554*3d8817e4Smiod 	     gregdata->reloc_request[i].valid,
2555*3d8817e4Smiod 
2556*3d8817e4Smiod 	     (unsigned long) (gregdata->reloc_request[i].value >> 32),
2557*3d8817e4Smiod 	     (unsigned long) gregdata->reloc_request[i].value,
2558*3d8817e4Smiod 	     gregdata->reloc_request[i].regindex,
2559*3d8817e4Smiod 	     gregdata->reloc_request[i].offset);
2560*3d8817e4Smiod }
2561*3d8817e4Smiod 
2562*3d8817e4Smiod /* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
2563*3d8817e4Smiod    when the last such reloc is done, an index-array is sorted according to
2564*3d8817e4Smiod    the values and iterated over to produce register numbers (indexed by 0
2565*3d8817e4Smiod    from the first allocated register number) and offsets for use in real
2566*3d8817e4Smiod    relocation.
2567*3d8817e4Smiod 
2568*3d8817e4Smiod    PUSHJ stub accounting is also done here.
2569*3d8817e4Smiod 
2570*3d8817e4Smiod    Symbol- and reloc-reading infrastructure copied from elf-m10200.c.  */
2571*3d8817e4Smiod 
2572*3d8817e4Smiod static bfd_boolean
mmix_elf_relax_section(abfd,sec,link_info,again)2573*3d8817e4Smiod mmix_elf_relax_section (abfd, sec, link_info, again)
2574*3d8817e4Smiod      bfd *abfd;
2575*3d8817e4Smiod      asection *sec;
2576*3d8817e4Smiod      struct bfd_link_info *link_info;
2577*3d8817e4Smiod      bfd_boolean *again;
2578*3d8817e4Smiod {
2579*3d8817e4Smiod   Elf_Internal_Shdr *symtab_hdr;
2580*3d8817e4Smiod   Elf_Internal_Rela *internal_relocs;
2581*3d8817e4Smiod   Elf_Internal_Rela *irel, *irelend;
2582*3d8817e4Smiod   asection *bpo_gregs_section = NULL;
2583*3d8817e4Smiod   struct bpo_greg_section_info *gregdata;
2584*3d8817e4Smiod   struct bpo_reloc_section_info *bpodata
2585*3d8817e4Smiod     = mmix_elf_section_data (sec)->bpo.reloc;
2586*3d8817e4Smiod   /* The initialization is to quiet compiler warnings.  The value is to
2587*3d8817e4Smiod      spot a missing actual initialization.  */
2588*3d8817e4Smiod   size_t bpono = (size_t) -1;
2589*3d8817e4Smiod   size_t pjsno = 0;
2590*3d8817e4Smiod   bfd *bpo_greg_owner;
2591*3d8817e4Smiod   Elf_Internal_Sym *isymbuf = NULL;
2592*3d8817e4Smiod   bfd_size_type size = sec->rawsize ? sec->rawsize : sec->size;
2593*3d8817e4Smiod 
2594*3d8817e4Smiod   mmix_elf_section_data (sec)->pjs.stubs_size_sum = 0;
2595*3d8817e4Smiod 
2596*3d8817e4Smiod   /* Assume nothing changes.  */
2597*3d8817e4Smiod   *again = FALSE;
2598*3d8817e4Smiod 
2599*3d8817e4Smiod   /* We don't have to do anything if this section does not have relocs, or
2600*3d8817e4Smiod      if this is not a code section.  */
2601*3d8817e4Smiod   if ((sec->flags & SEC_RELOC) == 0
2602*3d8817e4Smiod       || sec->reloc_count == 0
2603*3d8817e4Smiod       || (sec->flags & SEC_CODE) == 0
2604*3d8817e4Smiod       || (sec->flags & SEC_LINKER_CREATED) != 0
2605*3d8817e4Smiod       /* If no R_MMIX_BASE_PLUS_OFFSET relocs and no PUSHJ-stub relocs,
2606*3d8817e4Smiod          then nothing to do.  */
2607*3d8817e4Smiod       || (bpodata == NULL
2608*3d8817e4Smiod 	  && mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0))
2609*3d8817e4Smiod     return TRUE;
2610*3d8817e4Smiod 
2611*3d8817e4Smiod   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2612*3d8817e4Smiod 
2613*3d8817e4Smiod   bpo_greg_owner = (bfd *) link_info->base_file;
2614*3d8817e4Smiod 
2615*3d8817e4Smiod   if (bpodata != NULL)
2616*3d8817e4Smiod     {
2617*3d8817e4Smiod       bpo_gregs_section = bpodata->bpo_greg_section;
2618*3d8817e4Smiod       gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2619*3d8817e4Smiod       bpono = bpodata->first_base_plus_offset_reloc;
2620*3d8817e4Smiod     }
2621*3d8817e4Smiod   else
2622*3d8817e4Smiod     gregdata = NULL;
2623*3d8817e4Smiod 
2624*3d8817e4Smiod   /* Get a copy of the native relocations.  */
2625*3d8817e4Smiod   internal_relocs
2626*3d8817e4Smiod     = _bfd_elf_link_read_relocs (abfd, sec, (PTR) NULL,
2627*3d8817e4Smiod 				 (Elf_Internal_Rela *) NULL,
2628*3d8817e4Smiod 				 link_info->keep_memory);
2629*3d8817e4Smiod   if (internal_relocs == NULL)
2630*3d8817e4Smiod     goto error_return;
2631*3d8817e4Smiod 
2632*3d8817e4Smiod   /* Walk through them looking for relaxing opportunities.  */
2633*3d8817e4Smiod   irelend = internal_relocs + sec->reloc_count;
2634*3d8817e4Smiod   for (irel = internal_relocs; irel < irelend; irel++)
2635*3d8817e4Smiod     {
2636*3d8817e4Smiod       bfd_vma symval;
2637*3d8817e4Smiod       struct elf_link_hash_entry *h = NULL;
2638*3d8817e4Smiod 
2639*3d8817e4Smiod       /* We only process two relocs.  */
2640*3d8817e4Smiod       if (ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_BASE_PLUS_OFFSET
2641*3d8817e4Smiod 	  && ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_PUSHJ_STUBBABLE)
2642*3d8817e4Smiod 	continue;
2643*3d8817e4Smiod 
2644*3d8817e4Smiod       /* We process relocs in a distinctly different way when this is a
2645*3d8817e4Smiod 	 relocatable link (for one, we don't look at symbols), so we avoid
2646*3d8817e4Smiod 	 mixing its code with that for the "normal" relaxation.  */
2647*3d8817e4Smiod       if (link_info->relocatable)
2648*3d8817e4Smiod 	{
2649*3d8817e4Smiod 	  /* The only transformation in a relocatable link is to generate
2650*3d8817e4Smiod 	     a full stub at the location of the stub calculated for the
2651*3d8817e4Smiod 	     input section, if the relocated stub location, the end of the
2652*3d8817e4Smiod 	     output section plus earlier stubs, cannot be reached.  Thus
2653*3d8817e4Smiod 	     relocatable linking can only lead to worse code, but it still
2654*3d8817e4Smiod 	     works.  */
2655*3d8817e4Smiod 	  if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
2656*3d8817e4Smiod 	    {
2657*3d8817e4Smiod 	      /* If we can reach the end of the output-section and beyond
2658*3d8817e4Smiod 		 any current stubs, then we don't need a stub for this
2659*3d8817e4Smiod 		 reloc.  The relaxed order of output stub allocation may
2660*3d8817e4Smiod 		 not exactly match the straightforward order, so we always
2661*3d8817e4Smiod 		 assume presence of output stubs, which will allow
2662*3d8817e4Smiod 		 relaxation only on relocations indifferent to the
2663*3d8817e4Smiod 		 presence of output stub allocations for other relocations
2664*3d8817e4Smiod 		 and thus the order of output stub allocation.  */
2665*3d8817e4Smiod 	      if (bfd_check_overflow (complain_overflow_signed,
2666*3d8817e4Smiod 				      19,
2667*3d8817e4Smiod 				      0,
2668*3d8817e4Smiod 				      bfd_arch_bits_per_address (abfd),
2669*3d8817e4Smiod 				      /* Output-stub location.  */
2670*3d8817e4Smiod 				      sec->output_section->rawsize
2671*3d8817e4Smiod 				      + (mmix_elf_section_data (sec
2672*3d8817e4Smiod 							       ->output_section)
2673*3d8817e4Smiod 					 ->pjs.stubs_size_sum)
2674*3d8817e4Smiod 				      /* Location of this PUSHJ reloc.  */
2675*3d8817e4Smiod 				      - (sec->output_offset + irel->r_offset)
2676*3d8817e4Smiod 				      /* Don't count *this* stub twice.  */
2677*3d8817e4Smiod 				      - (mmix_elf_section_data (sec)
2678*3d8817e4Smiod 					 ->pjs.stub_size[pjsno]
2679*3d8817e4Smiod 					 + MAX_PUSHJ_STUB_SIZE))
2680*3d8817e4Smiod 		  == bfd_reloc_ok)
2681*3d8817e4Smiod 		mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2682*3d8817e4Smiod 
2683*3d8817e4Smiod 	      mmix_elf_section_data (sec)->pjs.stubs_size_sum
2684*3d8817e4Smiod 		+= mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2685*3d8817e4Smiod 
2686*3d8817e4Smiod 	      pjsno++;
2687*3d8817e4Smiod 	    }
2688*3d8817e4Smiod 
2689*3d8817e4Smiod 	  continue;
2690*3d8817e4Smiod 	}
2691*3d8817e4Smiod 
2692*3d8817e4Smiod       /* Get the value of the symbol referred to by the reloc.  */
2693*3d8817e4Smiod       if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2694*3d8817e4Smiod 	{
2695*3d8817e4Smiod 	  /* A local symbol.  */
2696*3d8817e4Smiod 	  Elf_Internal_Sym *isym;
2697*3d8817e4Smiod 	  asection *sym_sec;
2698*3d8817e4Smiod 
2699*3d8817e4Smiod 	  /* Read this BFD's local symbols if we haven't already.  */
2700*3d8817e4Smiod 	  if (isymbuf == NULL)
2701*3d8817e4Smiod 	    {
2702*3d8817e4Smiod 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2703*3d8817e4Smiod 	      if (isymbuf == NULL)
2704*3d8817e4Smiod 		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2705*3d8817e4Smiod 						symtab_hdr->sh_info, 0,
2706*3d8817e4Smiod 						NULL, NULL, NULL);
2707*3d8817e4Smiod 	      if (isymbuf == 0)
2708*3d8817e4Smiod 		goto error_return;
2709*3d8817e4Smiod 	    }
2710*3d8817e4Smiod 
2711*3d8817e4Smiod 	  isym = isymbuf + ELF64_R_SYM (irel->r_info);
2712*3d8817e4Smiod 	  if (isym->st_shndx == SHN_UNDEF)
2713*3d8817e4Smiod 	    sym_sec = bfd_und_section_ptr;
2714*3d8817e4Smiod 	  else if (isym->st_shndx == SHN_ABS)
2715*3d8817e4Smiod 	    sym_sec = bfd_abs_section_ptr;
2716*3d8817e4Smiod 	  else if (isym->st_shndx == SHN_COMMON)
2717*3d8817e4Smiod 	    sym_sec = bfd_com_section_ptr;
2718*3d8817e4Smiod 	  else
2719*3d8817e4Smiod 	    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2720*3d8817e4Smiod 	  symval = (isym->st_value
2721*3d8817e4Smiod 		    + sym_sec->output_section->vma
2722*3d8817e4Smiod 		    + sym_sec->output_offset);
2723*3d8817e4Smiod 	}
2724*3d8817e4Smiod       else
2725*3d8817e4Smiod 	{
2726*3d8817e4Smiod 	  unsigned long indx;
2727*3d8817e4Smiod 
2728*3d8817e4Smiod 	  /* An external symbol.  */
2729*3d8817e4Smiod 	  indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2730*3d8817e4Smiod 	  h = elf_sym_hashes (abfd)[indx];
2731*3d8817e4Smiod 	  BFD_ASSERT (h != NULL);
2732*3d8817e4Smiod 	  if (h->root.type != bfd_link_hash_defined
2733*3d8817e4Smiod 	      && h->root.type != bfd_link_hash_defweak)
2734*3d8817e4Smiod 	    {
2735*3d8817e4Smiod 	      /* This appears to be a reference to an undefined symbol.  Just
2736*3d8817e4Smiod 		 ignore it--it will be caught by the regular reloc processing.
2737*3d8817e4Smiod 		 We need to keep BPO reloc accounting consistent, though
2738*3d8817e4Smiod 		 else we'll abort instead of emitting an error message.  */
2739*3d8817e4Smiod 	      if (ELF64_R_TYPE (irel->r_info) == R_MMIX_BASE_PLUS_OFFSET
2740*3d8817e4Smiod 		  && gregdata != NULL)
2741*3d8817e4Smiod 		{
2742*3d8817e4Smiod 		  gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2743*3d8817e4Smiod 		  bpono++;
2744*3d8817e4Smiod 		}
2745*3d8817e4Smiod 	      continue;
2746*3d8817e4Smiod 	    }
2747*3d8817e4Smiod 
2748*3d8817e4Smiod 	  symval = (h->root.u.def.value
2749*3d8817e4Smiod 		    + h->root.u.def.section->output_section->vma
2750*3d8817e4Smiod 		    + h->root.u.def.section->output_offset);
2751*3d8817e4Smiod 	}
2752*3d8817e4Smiod 
2753*3d8817e4Smiod       if (ELF64_R_TYPE (irel->r_info) == (int) R_MMIX_PUSHJ_STUBBABLE)
2754*3d8817e4Smiod 	{
2755*3d8817e4Smiod 	  bfd_vma value = symval + irel->r_addend;
2756*3d8817e4Smiod 	  bfd_vma dot
2757*3d8817e4Smiod 	    = (sec->output_section->vma
2758*3d8817e4Smiod 	       + sec->output_offset
2759*3d8817e4Smiod 	       + irel->r_offset);
2760*3d8817e4Smiod 	  bfd_vma stubaddr
2761*3d8817e4Smiod 	    = (sec->output_section->vma
2762*3d8817e4Smiod 	       + sec->output_offset
2763*3d8817e4Smiod 	       + size
2764*3d8817e4Smiod 	       + mmix_elf_section_data (sec)->pjs.stubs_size_sum);
2765*3d8817e4Smiod 
2766*3d8817e4Smiod 	  if ((value & 3) == 0
2767*3d8817e4Smiod 	      && bfd_check_overflow (complain_overflow_signed,
2768*3d8817e4Smiod 				     19,
2769*3d8817e4Smiod 				     0,
2770*3d8817e4Smiod 				     bfd_arch_bits_per_address (abfd),
2771*3d8817e4Smiod 				     value - dot
2772*3d8817e4Smiod 				     - (value > dot
2773*3d8817e4Smiod 					? mmix_elf_section_data (sec)
2774*3d8817e4Smiod 					->pjs.stub_size[pjsno]
2775*3d8817e4Smiod 					: 0))
2776*3d8817e4Smiod 	      == bfd_reloc_ok)
2777*3d8817e4Smiod 	    /* If the reloc fits, no stub is needed.  */
2778*3d8817e4Smiod 	    mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2779*3d8817e4Smiod 	  else
2780*3d8817e4Smiod 	    /* Maybe we can get away with just a JMP insn?  */
2781*3d8817e4Smiod 	    if ((value & 3) == 0
2782*3d8817e4Smiod 		&& bfd_check_overflow (complain_overflow_signed,
2783*3d8817e4Smiod 				       27,
2784*3d8817e4Smiod 				       0,
2785*3d8817e4Smiod 				       bfd_arch_bits_per_address (abfd),
2786*3d8817e4Smiod 				       value - stubaddr
2787*3d8817e4Smiod 				       - (value > dot
2788*3d8817e4Smiod 					  ? mmix_elf_section_data (sec)
2789*3d8817e4Smiod 					  ->pjs.stub_size[pjsno] - 4
2790*3d8817e4Smiod 					  : 0))
2791*3d8817e4Smiod 		== bfd_reloc_ok)
2792*3d8817e4Smiod 	      /* Yep, account for a stub consisting of a single JMP insn.  */
2793*3d8817e4Smiod 	      mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 4;
2794*3d8817e4Smiod 	  else
2795*3d8817e4Smiod 	    /* Nope, go for the full insn stub.  It doesn't seem useful to
2796*3d8817e4Smiod 	       emit the intermediate sizes; those will only be useful for
2797*3d8817e4Smiod 	       a >64M program assuming contiguous code.  */
2798*3d8817e4Smiod 	    mmix_elf_section_data (sec)->pjs.stub_size[pjsno]
2799*3d8817e4Smiod 	      = MAX_PUSHJ_STUB_SIZE;
2800*3d8817e4Smiod 
2801*3d8817e4Smiod 	  mmix_elf_section_data (sec)->pjs.stubs_size_sum
2802*3d8817e4Smiod 	    += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2803*3d8817e4Smiod 	  pjsno++;
2804*3d8817e4Smiod 	  continue;
2805*3d8817e4Smiod 	}
2806*3d8817e4Smiod 
2807*3d8817e4Smiod       /* We're looking at a R_MMIX_BASE_PLUS_OFFSET reloc.  */
2808*3d8817e4Smiod 
2809*3d8817e4Smiod       gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono]].value
2810*3d8817e4Smiod 	= symval + irel->r_addend;
2811*3d8817e4Smiod       gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono++]].valid = TRUE;
2812*3d8817e4Smiod       gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2813*3d8817e4Smiod     }
2814*3d8817e4Smiod 
2815*3d8817e4Smiod   /* Check if that was the last BPO-reloc.  If so, sort the values and
2816*3d8817e4Smiod      calculate how many registers we need to cover them.  Set the size of
2817*3d8817e4Smiod      the linker gregs, and if the number of registers changed, indicate
2818*3d8817e4Smiod      that we need to relax some more because we have more work to do.  */
2819*3d8817e4Smiod   if (gregdata != NULL
2820*3d8817e4Smiod       && gregdata->n_remaining_bpo_relocs_this_relaxation_round == 0)
2821*3d8817e4Smiod     {
2822*3d8817e4Smiod       size_t i;
2823*3d8817e4Smiod       bfd_vma prev_base;
2824*3d8817e4Smiod       size_t regindex;
2825*3d8817e4Smiod 
2826*3d8817e4Smiod       /* First, reset the remaining relocs for the next round.  */
2827*3d8817e4Smiod       gregdata->n_remaining_bpo_relocs_this_relaxation_round
2828*3d8817e4Smiod 	= gregdata->n_bpo_relocs;
2829*3d8817e4Smiod 
2830*3d8817e4Smiod       qsort ((PTR) gregdata->reloc_request,
2831*3d8817e4Smiod 	     gregdata->n_max_bpo_relocs,
2832*3d8817e4Smiod 	     sizeof (struct bpo_reloc_request),
2833*3d8817e4Smiod 	     bpo_reloc_request_sort_fn);
2834*3d8817e4Smiod 
2835*3d8817e4Smiod       /* Recalculate indexes.  When we find a change (however unlikely
2836*3d8817e4Smiod 	 after the initial iteration), we know we need to relax again,
2837*3d8817e4Smiod 	 since items in the GREG-array are sorted by increasing value and
2838*3d8817e4Smiod 	 stored in the relaxation phase.  */
2839*3d8817e4Smiod       for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2840*3d8817e4Smiod 	if (gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2841*3d8817e4Smiod 	    != i)
2842*3d8817e4Smiod 	  {
2843*3d8817e4Smiod 	    gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2844*3d8817e4Smiod 	      = i;
2845*3d8817e4Smiod 	    *again = TRUE;
2846*3d8817e4Smiod 	  }
2847*3d8817e4Smiod 
2848*3d8817e4Smiod       /* Allocate register numbers (indexing from 0).  Stop at the first
2849*3d8817e4Smiod 	 non-valid reloc.  */
2850*3d8817e4Smiod       for (i = 0, regindex = 0, prev_base = gregdata->reloc_request[0].value;
2851*3d8817e4Smiod 	   i < gregdata->n_bpo_relocs;
2852*3d8817e4Smiod 	   i++)
2853*3d8817e4Smiod 	{
2854*3d8817e4Smiod 	  if (gregdata->reloc_request[i].value > prev_base + 255)
2855*3d8817e4Smiod 	    {
2856*3d8817e4Smiod 	      regindex++;
2857*3d8817e4Smiod 	      prev_base = gregdata->reloc_request[i].value;
2858*3d8817e4Smiod 	    }
2859*3d8817e4Smiod 	  gregdata->reloc_request[i].regindex = regindex;
2860*3d8817e4Smiod 	  gregdata->reloc_request[i].offset
2861*3d8817e4Smiod 	    = gregdata->reloc_request[i].value - prev_base;
2862*3d8817e4Smiod 	}
2863*3d8817e4Smiod 
2864*3d8817e4Smiod       /* If it's not the same as the last time, we need to relax again,
2865*3d8817e4Smiod 	 because the size of the section has changed.  I'm not sure we
2866*3d8817e4Smiod 	 actually need to do any adjustments since the shrinking happens
2867*3d8817e4Smiod 	 at the start of this section, but better safe than sorry.  */
2868*3d8817e4Smiod       if (gregdata->n_allocated_bpo_gregs != regindex + 1)
2869*3d8817e4Smiod 	{
2870*3d8817e4Smiod 	  gregdata->n_allocated_bpo_gregs = regindex + 1;
2871*3d8817e4Smiod 	  *again = TRUE;
2872*3d8817e4Smiod 	}
2873*3d8817e4Smiod 
2874*3d8817e4Smiod       bpo_gregs_section->size = (regindex + 1) * 8;
2875*3d8817e4Smiod     }
2876*3d8817e4Smiod 
2877*3d8817e4Smiod   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
2878*3d8817e4Smiod     {
2879*3d8817e4Smiod       if (! link_info->keep_memory)
2880*3d8817e4Smiod 	free (isymbuf);
2881*3d8817e4Smiod       else
2882*3d8817e4Smiod 	{
2883*3d8817e4Smiod 	  /* Cache the symbols for elf_link_input_bfd.  */
2884*3d8817e4Smiod 	  symtab_hdr->contents = (unsigned char *) isymbuf;
2885*3d8817e4Smiod 	}
2886*3d8817e4Smiod     }
2887*3d8817e4Smiod 
2888*3d8817e4Smiod   if (internal_relocs != NULL
2889*3d8817e4Smiod       && elf_section_data (sec)->relocs != internal_relocs)
2890*3d8817e4Smiod     free (internal_relocs);
2891*3d8817e4Smiod 
2892*3d8817e4Smiod   if (sec->size < size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
2893*3d8817e4Smiod     abort ();
2894*3d8817e4Smiod 
2895*3d8817e4Smiod   if (sec->size > size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
2896*3d8817e4Smiod     {
2897*3d8817e4Smiod       sec->size = size + mmix_elf_section_data (sec)->pjs.stubs_size_sum;
2898*3d8817e4Smiod       *again = TRUE;
2899*3d8817e4Smiod     }
2900*3d8817e4Smiod 
2901*3d8817e4Smiod   return TRUE;
2902*3d8817e4Smiod 
2903*3d8817e4Smiod  error_return:
2904*3d8817e4Smiod   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
2905*3d8817e4Smiod     free (isymbuf);
2906*3d8817e4Smiod   if (internal_relocs != NULL
2907*3d8817e4Smiod       && elf_section_data (sec)->relocs != internal_relocs)
2908*3d8817e4Smiod     free (internal_relocs);
2909*3d8817e4Smiod   return FALSE;
2910*3d8817e4Smiod }
2911*3d8817e4Smiod 
2912*3d8817e4Smiod #define ELF_ARCH		bfd_arch_mmix
2913*3d8817e4Smiod #define ELF_MACHINE_CODE 	EM_MMIX
2914*3d8817e4Smiod 
2915*3d8817e4Smiod /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
2916*3d8817e4Smiod    However, that's too much for something somewhere in the linker part of
2917*3d8817e4Smiod    BFD; perhaps the start-address has to be a non-zero multiple of this
2918*3d8817e4Smiod    number, or larger than this number.  The symptom is that the linker
2919*3d8817e4Smiod    complains: "warning: allocated section `.text' not in segment".  We
2920*3d8817e4Smiod    settle for 64k; the page-size used in examples is 8k.
2921*3d8817e4Smiod    #define ELF_MAXPAGESIZE 0x10000
2922*3d8817e4Smiod 
2923*3d8817e4Smiod    Unfortunately, this causes excessive padding in the supposedly small
2924*3d8817e4Smiod    for-education programs that are the expected usage (where people would
2925*3d8817e4Smiod    inspect output).  We stick to 256 bytes just to have *some* default
2926*3d8817e4Smiod    alignment.  */
2927*3d8817e4Smiod #define ELF_MAXPAGESIZE 0x100
2928*3d8817e4Smiod 
2929*3d8817e4Smiod #define TARGET_BIG_SYM		bfd_elf64_mmix_vec
2930*3d8817e4Smiod #define TARGET_BIG_NAME		"elf64-mmix"
2931*3d8817e4Smiod 
2932*3d8817e4Smiod #define elf_info_to_howto_rel		NULL
2933*3d8817e4Smiod #define elf_info_to_howto		mmix_info_to_howto_rela
2934*3d8817e4Smiod #define elf_backend_relocate_section	mmix_elf_relocate_section
2935*3d8817e4Smiod #define elf_backend_gc_mark_hook	mmix_elf_gc_mark_hook
2936*3d8817e4Smiod #define elf_backend_gc_sweep_hook	mmix_elf_gc_sweep_hook
2937*3d8817e4Smiod 
2938*3d8817e4Smiod #define elf_backend_link_output_symbol_hook \
2939*3d8817e4Smiod 	mmix_elf_link_output_symbol_hook
2940*3d8817e4Smiod #define elf_backend_add_symbol_hook	mmix_elf_add_symbol_hook
2941*3d8817e4Smiod 
2942*3d8817e4Smiod #define elf_backend_check_relocs	mmix_elf_check_relocs
2943*3d8817e4Smiod #define elf_backend_symbol_processing	mmix_elf_symbol_processing
2944*3d8817e4Smiod 
2945*3d8817e4Smiod #define bfd_elf64_bfd_is_local_label_name \
2946*3d8817e4Smiod 	mmix_elf_is_local_label_name
2947*3d8817e4Smiod 
2948*3d8817e4Smiod #define elf_backend_may_use_rel_p	0
2949*3d8817e4Smiod #define elf_backend_may_use_rela_p	1
2950*3d8817e4Smiod #define elf_backend_default_use_rela_p	1
2951*3d8817e4Smiod 
2952*3d8817e4Smiod #define elf_backend_can_gc_sections	1
2953*3d8817e4Smiod #define elf_backend_section_from_bfd_section \
2954*3d8817e4Smiod 	mmix_elf_section_from_bfd_section
2955*3d8817e4Smiod 
2956*3d8817e4Smiod #define bfd_elf64_new_section_hook	mmix_elf_new_section_hook
2957*3d8817e4Smiod #define bfd_elf64_bfd_final_link	mmix_elf_final_link
2958*3d8817e4Smiod #define bfd_elf64_bfd_relax_section	mmix_elf_relax_section
2959*3d8817e4Smiod 
2960*3d8817e4Smiod #include "elf64-target.h"
2961