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