1 /* MMIX-specific support for 64-bit ELF.
2 Copyright 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Hans-Peter Nilsson <hp@bitrange.com>
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* No specific ABI or "processor-specific supplement" defined. */
24 - Linker relaxation. */
31 #include "opcode/mmix.h"
33 #define MINUS_ONE (((bfd_vma) 0) - 1)
35 /* Put these everywhere in new code. */
37 _bfd_abort (__FILE__, __LINE__, \
38 "Internal: Non-debugged code (test-case missing)")
41 _bfd_abort (__FILE__, __LINE__, \
44 /* For each section containing a base-plus-offset (BPO) reloc, we attach
45 this struct as elf_section_data (section)->tdata, which is otherwise
47 struct bpo_reloc_section_info
49 /* The base is 1; this is the first number in this section. */
50 size_t first_base_plus_offset_reloc
;
52 /* Number of BPO-relocs in this section. */
53 size_t n_bpo_relocs_this_section
;
55 /* Running index, used at relocation time. */
58 /* We don't have access to the bfd_link_info struct in
59 mmix_final_link_relocate. What we really want to get at is the
60 global single struct greg_relocation, so we stash it here. */
61 asection
*bpo_greg_section
;
64 /* Helper struct (in global context) for the one below.
65 There's one of these created for every BPO reloc. */
66 struct bpo_reloc_request
70 /* Valid after relaxation. The base is 0; the first register number
71 must be added. The offset is in range 0..255. */
75 /* The order number for this BPO reloc, corresponding to the order in
76 which BPO relocs were found. Used to create an index after reloc
77 requests are sorted. */
80 /* Set when the value is computed. Better than coding "guard values"
81 into the other members. Is false only for BPO relocs in a GC:ed
86 /* We attach this as elf_section_data (sec)->tdata in the linker-allocated
87 greg contents section (MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME),
88 which is linked into the register contents section
89 (MMIX_REG_CONTENTS_SECTION_NAME). This section is created by the
90 linker; using the same hook as for usual with BPO relocs does not
92 struct bpo_greg_section_info
94 /* After GC, this reflects the number of remaining, non-excluded
98 /* This is the number of allocated bpo_reloc_requests; the size of
99 sorted_indexes. Valid after the check.*relocs functions are called
100 for all incoming sections. It includes the number of BPO relocs in
101 sections that were GC:ed. */
102 size_t n_max_bpo_relocs
;
104 /* A counter used to find out when to fold the BPO gregs, since we
105 don't have a single "after-relaxation" hook. */
106 size_t n_remaining_bpo_relocs_this_relaxation_round
;
108 /* The number of linker-allocated GREGs resulting from BPO relocs.
109 This is an approximation after _bfd_mmix_allocated_gregs_init and
110 supposedly accurate after mmix_elf_relax_section is called for all
111 incoming non-collected sections. */
112 size_t n_allocated_bpo_gregs
;
114 /* Index into reloc_request[], sorted on increasing "value", secondary
115 by increasing index for strict sorting order. */
116 size_t *bpo_reloc_indexes
;
118 /* An array of all relocations, with the "value" member filled in by
119 the relaxation function. */
120 struct bpo_reloc_request
*reloc_request
;
123 static boolean mmix_elf_link_output_symbol_hook
124 PARAMS ((bfd
*, struct bfd_link_info
*, const char *,
125 Elf_Internal_Sym
*, asection
*));
127 static bfd_reloc_status_type mmix_elf_reloc
128 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
130 static reloc_howto_type
*bfd_elf64_bfd_reloc_type_lookup
131 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
133 static void mmix_info_to_howto_rela
134 PARAMS ((bfd
*, arelent
*, Elf64_Internal_Rela
*));
136 static int mmix_elf_sort_relocs
PARAMS ((const PTR
, const PTR
));
138 static boolean mmix_elf_check_relocs
139 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
140 const Elf_Internal_Rela
*));
142 static boolean mmix_elf_check_common_relocs
143 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
144 const Elf_Internal_Rela
*));
146 static boolean mmix_elf_relocate_section
147 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
148 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
150 static asection
* mmix_elf_gc_mark_hook
151 PARAMS ((bfd
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
152 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
154 static boolean mmix_elf_gc_sweep_hook
155 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
156 const Elf_Internal_Rela
*));
158 static bfd_reloc_status_type mmix_final_link_relocate
159 PARAMS ((reloc_howto_type
*, asection
*, bfd_byte
*,
160 bfd_vma
, bfd_signed_vma
, bfd_vma
, const char *, asection
*));
162 static bfd_reloc_status_type mmix_elf_perform_relocation
163 PARAMS ((asection
*, reloc_howto_type
*, PTR
, bfd_vma
, bfd_vma
));
165 static boolean mmix_elf_section_from_bfd_section
166 PARAMS ((bfd
*, asection
*, int *));
168 static boolean mmix_elf_add_symbol_hook
169 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
170 const char **, flagword
*, asection
**, bfd_vma
*));
172 static boolean mmix_elf_is_local_label_name
173 PARAMS ((bfd
*, const char *));
175 static int bpo_reloc_request_sort_fn
PARAMS ((const PTR
, const PTR
));
177 static boolean mmix_elf_relax_section
178 PARAMS ((bfd
*abfd
, asection
*sec
, struct bfd_link_info
*link_info
,
181 extern boolean mmix_elf_final_link
PARAMS ((bfd
*, struct bfd_link_info
*));
183 extern void mmix_elf_symbol_processing
PARAMS ((bfd
*, asymbol
*));
185 /* Only intended to be called from a debugger. */
186 extern void mmix_dump_bpo_gregs
187 PARAMS ((struct bfd_link_info
*, bfd_error_handler_type
));
189 /* Watch out: this currently needs to have elements with the same index as
190 their R_MMIX_ number. */
191 static reloc_howto_type elf_mmix_howto_table
[] =
193 /* This reloc does nothing. */
194 HOWTO (R_MMIX_NONE
, /* type */
196 2, /* size (0 = byte, 1 = short, 2 = long) */
198 false, /* pc_relative */
200 complain_overflow_bitfield
, /* complain_on_overflow */
201 bfd_elf_generic_reloc
, /* special_function */
202 "R_MMIX_NONE", /* name */
203 false, /* partial_inplace */
206 false), /* pcrel_offset */
208 /* An 8 bit absolute relocation. */
209 HOWTO (R_MMIX_8
, /* type */
211 0, /* size (0 = byte, 1 = short, 2 = long) */
213 false, /* pc_relative */
215 complain_overflow_bitfield
, /* complain_on_overflow */
216 bfd_elf_generic_reloc
, /* special_function */
217 "R_MMIX_8", /* name */
218 false, /* partial_inplace */
221 false), /* pcrel_offset */
223 /* An 16 bit absolute relocation. */
224 HOWTO (R_MMIX_16
, /* type */
226 1, /* size (0 = byte, 1 = short, 2 = long) */
228 false, /* pc_relative */
230 complain_overflow_bitfield
, /* complain_on_overflow */
231 bfd_elf_generic_reloc
, /* special_function */
232 "R_MMIX_16", /* name */
233 false, /* partial_inplace */
235 0xffff, /* dst_mask */
236 false), /* pcrel_offset */
238 /* An 24 bit absolute relocation. */
239 HOWTO (R_MMIX_24
, /* type */
241 2, /* size (0 = byte, 1 = short, 2 = long) */
243 false, /* pc_relative */
245 complain_overflow_bitfield
, /* complain_on_overflow */
246 bfd_elf_generic_reloc
, /* special_function */
247 "R_MMIX_24", /* name */
248 false, /* partial_inplace */
249 ~0xffffff, /* src_mask */
250 0xffffff, /* dst_mask */
251 false), /* pcrel_offset */
253 /* A 32 bit absolute relocation. */
254 HOWTO (R_MMIX_32
, /* type */
256 2, /* size (0 = byte, 1 = short, 2 = long) */
258 false, /* pc_relative */
260 complain_overflow_bitfield
, /* complain_on_overflow */
261 bfd_elf_generic_reloc
, /* special_function */
262 "R_MMIX_32", /* name */
263 false, /* partial_inplace */
265 0xffffffff, /* dst_mask */
266 false), /* pcrel_offset */
268 /* 64 bit relocation. */
269 HOWTO (R_MMIX_64
, /* type */
271 4, /* size (0 = byte, 1 = short, 2 = long) */
273 false, /* pc_relative */
275 complain_overflow_bitfield
, /* complain_on_overflow */
276 bfd_elf_generic_reloc
, /* special_function */
277 "R_MMIX_64", /* name */
278 false, /* partial_inplace */
280 MINUS_ONE
, /* dst_mask */
281 false), /* pcrel_offset */
283 /* An 8 bit PC-relative relocation. */
284 HOWTO (R_MMIX_PC_8
, /* type */
286 0, /* size (0 = byte, 1 = short, 2 = long) */
288 true, /* pc_relative */
290 complain_overflow_bitfield
, /* complain_on_overflow */
291 bfd_elf_generic_reloc
, /* special_function */
292 "R_MMIX_PC_8", /* name */
293 false, /* partial_inplace */
296 true), /* pcrel_offset */
298 /* An 16 bit PC-relative relocation. */
299 HOWTO (R_MMIX_PC_16
, /* type */
301 1, /* size (0 = byte, 1 = short, 2 = long) */
303 true, /* pc_relative */
305 complain_overflow_bitfield
, /* complain_on_overflow */
306 bfd_elf_generic_reloc
, /* special_function */
307 "R_MMIX_PC_16", /* name */
308 false, /* partial_inplace */
310 0xffff, /* dst_mask */
311 true), /* pcrel_offset */
313 /* An 24 bit PC-relative relocation. */
314 HOWTO (R_MMIX_PC_24
, /* type */
316 2, /* size (0 = byte, 1 = short, 2 = long) */
318 true, /* pc_relative */
320 complain_overflow_bitfield
, /* complain_on_overflow */
321 bfd_elf_generic_reloc
, /* special_function */
322 "R_MMIX_PC_24", /* name */
323 false, /* partial_inplace */
324 ~0xffffff, /* src_mask */
325 0xffffff, /* dst_mask */
326 true), /* pcrel_offset */
328 /* A 32 bit absolute PC-relative relocation. */
329 HOWTO (R_MMIX_PC_32
, /* type */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
333 true, /* pc_relative */
335 complain_overflow_bitfield
, /* complain_on_overflow */
336 bfd_elf_generic_reloc
, /* special_function */
337 "R_MMIX_PC_32", /* name */
338 false, /* partial_inplace */
340 0xffffffff, /* dst_mask */
341 true), /* pcrel_offset */
343 /* 64 bit PC-relative relocation. */
344 HOWTO (R_MMIX_PC_64
, /* type */
346 4, /* size (0 = byte, 1 = short, 2 = long) */
348 true, /* pc_relative */
350 complain_overflow_bitfield
, /* complain_on_overflow */
351 bfd_elf_generic_reloc
, /* special_function */
352 "R_MMIX_PC_64", /* name */
353 false, /* partial_inplace */
355 MINUS_ONE
, /* dst_mask */
356 true), /* pcrel_offset */
358 /* GNU extension to record C++ vtable hierarchy. */
359 HOWTO (R_MMIX_GNU_VTINHERIT
, /* type */
361 0, /* size (0 = byte, 1 = short, 2 = long) */
363 false, /* pc_relative */
365 complain_overflow_dont
, /* complain_on_overflow */
366 NULL
, /* special_function */
367 "R_MMIX_GNU_VTINHERIT", /* name */
368 false, /* partial_inplace */
371 true), /* pcrel_offset */
373 /* GNU extension to record C++ vtable member usage. */
374 HOWTO (R_MMIX_GNU_VTENTRY
, /* type */
376 0, /* size (0 = byte, 1 = short, 2 = long) */
378 false, /* pc_relative */
380 complain_overflow_dont
, /* complain_on_overflow */
381 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
382 "R_MMIX_GNU_VTENTRY", /* name */
383 false, /* partial_inplace */
386 false), /* pcrel_offset */
388 /* The GETA relocation is supposed to get any address that could
389 possibly be reached by the GETA instruction. It can silently expand
390 to get a 64-bit operand, but will complain if any of the two least
391 significant bits are set. The howto members reflect a simple GETA. */
392 HOWTO (R_MMIX_GETA
, /* type */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
396 true, /* pc_relative */
398 complain_overflow_signed
, /* complain_on_overflow */
399 mmix_elf_reloc
, /* special_function */
400 "R_MMIX_GETA", /* name */
401 false, /* partial_inplace */
402 ~0x0100ffff, /* src_mask */
403 0x0100ffff, /* dst_mask */
404 true), /* pcrel_offset */
406 HOWTO (R_MMIX_GETA_1
, /* type */
408 2, /* size (0 = byte, 1 = short, 2 = long) */
410 true, /* pc_relative */
412 complain_overflow_signed
, /* complain_on_overflow */
413 mmix_elf_reloc
, /* special_function */
414 "R_MMIX_GETA_1", /* name */
415 false, /* partial_inplace */
416 ~0x0100ffff, /* src_mask */
417 0x0100ffff, /* dst_mask */
418 true), /* pcrel_offset */
420 HOWTO (R_MMIX_GETA_2
, /* type */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
424 true, /* pc_relative */
426 complain_overflow_signed
, /* complain_on_overflow */
427 mmix_elf_reloc
, /* special_function */
428 "R_MMIX_GETA_2", /* name */
429 false, /* partial_inplace */
430 ~0x0100ffff, /* src_mask */
431 0x0100ffff, /* dst_mask */
432 true), /* pcrel_offset */
434 HOWTO (R_MMIX_GETA_3
, /* type */
436 2, /* size (0 = byte, 1 = short, 2 = long) */
438 true, /* pc_relative */
440 complain_overflow_signed
, /* complain_on_overflow */
441 mmix_elf_reloc
, /* special_function */
442 "R_MMIX_GETA_3", /* name */
443 false, /* partial_inplace */
444 ~0x0100ffff, /* src_mask */
445 0x0100ffff, /* dst_mask */
446 true), /* pcrel_offset */
448 /* The conditional branches are supposed to reach any (code) address.
449 It can silently expand to a 64-bit operand, but will emit an error if
450 any of the two least significant bits are set. The howto members
451 reflect a simple branch. */
452 HOWTO (R_MMIX_CBRANCH
, /* type */
454 2, /* size (0 = byte, 1 = short, 2 = long) */
456 true, /* pc_relative */
458 complain_overflow_signed
, /* complain_on_overflow */
459 mmix_elf_reloc
, /* special_function */
460 "R_MMIX_CBRANCH", /* name */
461 false, /* partial_inplace */
462 ~0x0100ffff, /* src_mask */
463 0x0100ffff, /* dst_mask */
464 true), /* pcrel_offset */
466 HOWTO (R_MMIX_CBRANCH_J
, /* type */
468 2, /* size (0 = byte, 1 = short, 2 = long) */
470 true, /* pc_relative */
472 complain_overflow_signed
, /* complain_on_overflow */
473 mmix_elf_reloc
, /* special_function */
474 "R_MMIX_CBRANCH_J", /* name */
475 false, /* partial_inplace */
476 ~0x0100ffff, /* src_mask */
477 0x0100ffff, /* dst_mask */
478 true), /* pcrel_offset */
480 HOWTO (R_MMIX_CBRANCH_1
, /* type */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
484 true, /* pc_relative */
486 complain_overflow_signed
, /* complain_on_overflow */
487 mmix_elf_reloc
, /* special_function */
488 "R_MMIX_CBRANCH_1", /* name */
489 false, /* partial_inplace */
490 ~0x0100ffff, /* src_mask */
491 0x0100ffff, /* dst_mask */
492 true), /* pcrel_offset */
494 HOWTO (R_MMIX_CBRANCH_2
, /* type */
496 2, /* size (0 = byte, 1 = short, 2 = long) */
498 true, /* pc_relative */
500 complain_overflow_signed
, /* complain_on_overflow */
501 mmix_elf_reloc
, /* special_function */
502 "R_MMIX_CBRANCH_2", /* name */
503 false, /* partial_inplace */
504 ~0x0100ffff, /* src_mask */
505 0x0100ffff, /* dst_mask */
506 true), /* pcrel_offset */
508 HOWTO (R_MMIX_CBRANCH_3
, /* type */
510 2, /* size (0 = byte, 1 = short, 2 = long) */
512 true, /* pc_relative */
514 complain_overflow_signed
, /* complain_on_overflow */
515 mmix_elf_reloc
, /* special_function */
516 "R_MMIX_CBRANCH_3", /* name */
517 false, /* partial_inplace */
518 ~0x0100ffff, /* src_mask */
519 0x0100ffff, /* dst_mask */
520 true), /* pcrel_offset */
522 /* The PUSHJ instruction can reach any (code) address, as long as it's
523 the beginning of a function (no usable restriction). It can silently
524 expand to a 64-bit operand, but will emit an error if any of the two
525 least significant bits are set. The howto members reflect a simple
527 HOWTO (R_MMIX_PUSHJ
, /* type */
529 2, /* size (0 = byte, 1 = short, 2 = long) */
531 true, /* pc_relative */
533 complain_overflow_signed
, /* complain_on_overflow */
534 mmix_elf_reloc
, /* special_function */
535 "R_MMIX_PUSHJ", /* name */
536 false, /* partial_inplace */
537 ~0x0100ffff, /* src_mask */
538 0x0100ffff, /* dst_mask */
539 true), /* pcrel_offset */
541 HOWTO (R_MMIX_PUSHJ_1
, /* type */
543 2, /* size (0 = byte, 1 = short, 2 = long) */
545 true, /* pc_relative */
547 complain_overflow_signed
, /* complain_on_overflow */
548 mmix_elf_reloc
, /* special_function */
549 "R_MMIX_PUSHJ_1", /* name */
550 false, /* partial_inplace */
551 ~0x0100ffff, /* src_mask */
552 0x0100ffff, /* dst_mask */
553 true), /* pcrel_offset */
555 HOWTO (R_MMIX_PUSHJ_2
, /* type */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
559 true, /* pc_relative */
561 complain_overflow_signed
, /* complain_on_overflow */
562 mmix_elf_reloc
, /* special_function */
563 "R_MMIX_PUSHJ_2", /* name */
564 false, /* partial_inplace */
565 ~0x0100ffff, /* src_mask */
566 0x0100ffff, /* dst_mask */
567 true), /* pcrel_offset */
569 HOWTO (R_MMIX_PUSHJ_3
, /* type */
571 2, /* size (0 = byte, 1 = short, 2 = long) */
573 true, /* pc_relative */
575 complain_overflow_signed
, /* complain_on_overflow */
576 mmix_elf_reloc
, /* special_function */
577 "R_MMIX_PUSHJ_3", /* name */
578 false, /* partial_inplace */
579 ~0x0100ffff, /* src_mask */
580 0x0100ffff, /* dst_mask */
581 true), /* pcrel_offset */
583 /* A JMP is supposed to reach any (code) address. By itself, it can
584 reach +-64M; the expansion can reach all 64 bits. Note that the 64M
585 limit is soon reached if you link the program in wildly different
586 memory segments. The howto members reflect a trivial JMP. */
587 HOWTO (R_MMIX_JMP
, /* type */
589 2, /* size (0 = byte, 1 = short, 2 = long) */
591 true, /* pc_relative */
593 complain_overflow_signed
, /* complain_on_overflow */
594 mmix_elf_reloc
, /* special_function */
595 "R_MMIX_JMP", /* name */
596 false, /* partial_inplace */
597 ~0x1ffffff, /* src_mask */
598 0x1ffffff, /* dst_mask */
599 true), /* pcrel_offset */
601 HOWTO (R_MMIX_JMP_1
, /* type */
603 2, /* size (0 = byte, 1 = short, 2 = long) */
605 true, /* pc_relative */
607 complain_overflow_signed
, /* complain_on_overflow */
608 mmix_elf_reloc
, /* special_function */
609 "R_MMIX_JMP_1", /* name */
610 false, /* partial_inplace */
611 ~0x1ffffff, /* src_mask */
612 0x1ffffff, /* dst_mask */
613 true), /* pcrel_offset */
615 HOWTO (R_MMIX_JMP_2
, /* type */
617 2, /* size (0 = byte, 1 = short, 2 = long) */
619 true, /* pc_relative */
621 complain_overflow_signed
, /* complain_on_overflow */
622 mmix_elf_reloc
, /* special_function */
623 "R_MMIX_JMP_2", /* name */
624 false, /* partial_inplace */
625 ~0x1ffffff, /* src_mask */
626 0x1ffffff, /* dst_mask */
627 true), /* pcrel_offset */
629 HOWTO (R_MMIX_JMP_3
, /* type */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
633 true, /* pc_relative */
635 complain_overflow_signed
, /* complain_on_overflow */
636 mmix_elf_reloc
, /* special_function */
637 "R_MMIX_JMP_3", /* name */
638 false, /* partial_inplace */
639 ~0x1ffffff, /* src_mask */
640 0x1ffffff, /* dst_mask */
641 true), /* pcrel_offset */
643 /* When we don't emit link-time-relaxable code from the assembler, or
644 when relaxation has done all it can do, these relocs are used. For
645 GETA/PUSHJ/branches. */
646 HOWTO (R_MMIX_ADDR19
, /* type */
648 2, /* size (0 = byte, 1 = short, 2 = long) */
650 true, /* pc_relative */
652 complain_overflow_signed
, /* complain_on_overflow */
653 mmix_elf_reloc
, /* special_function */
654 "R_MMIX_ADDR19", /* name */
655 false, /* partial_inplace */
656 ~0x0100ffff, /* src_mask */
657 0x0100ffff, /* dst_mask */
658 true), /* pcrel_offset */
661 HOWTO (R_MMIX_ADDR27
, /* type */
663 2, /* size (0 = byte, 1 = short, 2 = long) */
665 true, /* pc_relative */
667 complain_overflow_signed
, /* complain_on_overflow */
668 mmix_elf_reloc
, /* special_function */
669 "R_MMIX_ADDR27", /* name */
670 false, /* partial_inplace */
671 ~0x1ffffff, /* src_mask */
672 0x1ffffff, /* dst_mask */
673 true), /* pcrel_offset */
675 /* A general register or the value 0..255. If a value, then the
676 instruction (offset -3) needs adjusting. */
677 HOWTO (R_MMIX_REG_OR_BYTE
, /* type */
679 1, /* size (0 = byte, 1 = short, 2 = long) */
681 false, /* pc_relative */
683 complain_overflow_bitfield
, /* complain_on_overflow */
684 mmix_elf_reloc
, /* special_function */
685 "R_MMIX_REG_OR_BYTE", /* name */
686 false, /* partial_inplace */
689 false), /* pcrel_offset */
691 /* A general register. */
692 HOWTO (R_MMIX_REG
, /* type */
694 1, /* size (0 = byte, 1 = short, 2 = long) */
696 false, /* pc_relative */
698 complain_overflow_bitfield
, /* complain_on_overflow */
699 mmix_elf_reloc
, /* special_function */
700 "R_MMIX_REG", /* name */
701 false, /* partial_inplace */
704 false), /* pcrel_offset */
706 /* A register plus an index, corresponding to the relocation expression.
707 The sizes must correspond to the valid range of the expression, while
708 the bitmasks correspond to what we store in the image. */
709 HOWTO (R_MMIX_BASE_PLUS_OFFSET
, /* type */
711 4, /* size (0 = byte, 1 = short, 2 = long) */
713 false, /* pc_relative */
715 complain_overflow_bitfield
, /* complain_on_overflow */
716 mmix_elf_reloc
, /* special_function */
717 "R_MMIX_BASE_PLUS_OFFSET", /* name */
718 false, /* partial_inplace */
720 0xffff, /* dst_mask */
721 false), /* pcrel_offset */
723 /* A "magic" relocation for a LOCAL expression, asserting that the
724 expression is less than the number of global registers. No actual
725 modification of the contents is done. Implementing this as a
726 relocation was less intrusive than e.g. putting such expressions in a
727 section to discard *after* relocation. */
728 HOWTO (R_MMIX_LOCAL
, /* type */
730 0, /* size (0 = byte, 1 = short, 2 = long) */
732 false, /* pc_relative */
734 complain_overflow_dont
, /* complain_on_overflow */
735 mmix_elf_reloc
, /* special_function */
736 "R_MMIX_LOCAL", /* name */
737 false, /* partial_inplace */
740 false), /* pcrel_offset */
744 /* Map BFD reloc types to MMIX ELF reloc types. */
746 struct mmix_reloc_map
748 bfd_reloc_code_real_type bfd_reloc_val
;
749 enum elf_mmix_reloc_type elf_reloc_val
;
753 static const struct mmix_reloc_map mmix_reloc_map
[] =
755 {BFD_RELOC_NONE
, R_MMIX_NONE
},
756 {BFD_RELOC_8
, R_MMIX_8
},
757 {BFD_RELOC_16
, R_MMIX_16
},
758 {BFD_RELOC_24
, R_MMIX_24
},
759 {BFD_RELOC_32
, R_MMIX_32
},
760 {BFD_RELOC_64
, R_MMIX_64
},
761 {BFD_RELOC_8_PCREL
, R_MMIX_PC_8
},
762 {BFD_RELOC_16_PCREL
, R_MMIX_PC_16
},
763 {BFD_RELOC_24_PCREL
, R_MMIX_PC_24
},
764 {BFD_RELOC_32_PCREL
, R_MMIX_PC_32
},
765 {BFD_RELOC_64_PCREL
, R_MMIX_PC_64
},
766 {BFD_RELOC_VTABLE_INHERIT
, R_MMIX_GNU_VTINHERIT
},
767 {BFD_RELOC_VTABLE_ENTRY
, R_MMIX_GNU_VTENTRY
},
768 {BFD_RELOC_MMIX_GETA
, R_MMIX_GETA
},
769 {BFD_RELOC_MMIX_CBRANCH
, R_MMIX_CBRANCH
},
770 {BFD_RELOC_MMIX_PUSHJ
, R_MMIX_PUSHJ
},
771 {BFD_RELOC_MMIX_JMP
, R_MMIX_JMP
},
772 {BFD_RELOC_MMIX_ADDR19
, R_MMIX_ADDR19
},
773 {BFD_RELOC_MMIX_ADDR27
, R_MMIX_ADDR27
},
774 {BFD_RELOC_MMIX_REG_OR_BYTE
, R_MMIX_REG_OR_BYTE
},
775 {BFD_RELOC_MMIX_REG
, R_MMIX_REG
},
776 {BFD_RELOC_MMIX_BASE_PLUS_OFFSET
, R_MMIX_BASE_PLUS_OFFSET
},
777 {BFD_RELOC_MMIX_LOCAL
, R_MMIX_LOCAL
}
780 static reloc_howto_type
*
781 bfd_elf64_bfd_reloc_type_lookup (abfd
, code
)
782 bfd
*abfd ATTRIBUTE_UNUSED
;
783 bfd_reloc_code_real_type code
;
788 i
< sizeof (mmix_reloc_map
) / sizeof (mmix_reloc_map
[0]);
791 if (mmix_reloc_map
[i
].bfd_reloc_val
== code
)
792 return &elf_mmix_howto_table
[mmix_reloc_map
[i
].elf_reloc_val
];
799 /* This function performs the actual bitfiddling and sanity check for a
800 final relocation. Each relocation gets its *worst*-case expansion
801 in size when it arrives here; any reduction in size should have been
802 caught in linker relaxation earlier. When we get here, the relocation
803 looks like the smallest instruction with SWYM:s (nop:s) appended to the
804 max size. We fill in those nop:s.
806 R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
810 INCML $N,(foo >> 16) & 0xffff
811 INCMH $N,(foo >> 32) & 0xffff
812 INCH $N,(foo >> 48) & 0xffff
814 R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
815 condbranches needing relaxation might be rare enough to not be
826 R_MMIX_PUSHJ: (FIXME: Relaxation...)
835 R_MMIX_JMP: (FIXME: Relaxation...)
844 R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in. */
846 static bfd_reloc_status_type
847 mmix_elf_perform_relocation (isec
, howto
, datap
, addr
, value
)
849 reloc_howto_type
*howto
;
851 bfd_vma addr ATTRIBUTE_UNUSED
;
854 bfd
*abfd
= isec
->owner
;
855 bfd_reloc_status_type flag
= bfd_reloc_ok
;
856 bfd_reloc_status_type r
;
860 /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
861 We handle the differences here and the common sequence later. */
866 reg
= bfd_get_8 (abfd
, (bfd_byte
*) datap
+ 1);
868 /* We change to an absolute value. */
874 int in1
= bfd_get_16 (abfd
, (bfd_byte
*) datap
) << 16;
876 /* Invert the condition and prediction bit, and set the offset
877 to five instructions ahead.
879 We *can* do better if we want to. If the branch is found to be
880 within limits, we could leave the branch as is; there'll just
881 be a bunch of NOP:s after it. But we shouldn't see this
882 sequence often enough that it's worth doing it. */
885 (((in1
^ ((PRED_INV_BIT
| COND_INV_BIT
) << 24)) & ~0xffff)
889 /* Put a "GO $255,$255,0" after the common sequence. */
891 ((GO_INSN_BYTE
| IMM_OFFSET_BIT
) << 24) | 0xffff00,
892 (bfd_byte
*) datap
+ 20);
894 /* Common sequence starts at offset 4. */
897 /* We change to an absolute value. */
904 int inreg
= bfd_get_8 (abfd
, (bfd_byte
*) datap
+ 1);
906 /* Put a "PUSHGO $N,$255,0" after the common sequence. */
908 ((PUSHGO_INSN_BYTE
| IMM_OFFSET_BIT
) << 24)
911 (bfd_byte
*) datap
+ 16);
913 /* We change to an absolute value. */
919 /* This one is a little special. If we get here on a non-relaxing
920 link, and the destination is actually in range, we don't need to
922 If so, we fall through to the bit-fiddling relocs.
924 FIXME: bfd_check_overflow seems broken; the relocation is
925 rightshifted before testing, so supply a zero rightshift. */
927 if (! ((value
& 3) == 0
928 && (r
= bfd_check_overflow (complain_overflow_signed
,
931 bfd_arch_bits_per_address (abfd
),
932 value
)) == bfd_reloc_ok
))
934 /* If the relocation doesn't fit in a JMP, we let the NOP:s be
935 modified below, and put a "GO $255,$255,0" after the
936 address-loading sequence. */
938 ((GO_INSN_BYTE
| IMM_OFFSET_BIT
) << 24)
940 (bfd_byte
*) datap
+ 16);
942 /* We change to an absolute value. */
949 /* These must be in range, or else we emit an error. */
951 /* Note rightshift 0; see above. */
952 && (r
= bfd_check_overflow (complain_overflow_signed
,
955 bfd_arch_bits_per_address (abfd
),
956 value
)) == bfd_reloc_ok
)
959 = bfd_get_32 (abfd
, (bfd_byte
*) datap
);
962 if ((bfd_signed_vma
) value
< 0)
965 value
+= (1 << (howto
->bitsize
- 1));
973 (in1
& howto
->src_mask
)
975 | (value
& howto
->dst_mask
),
981 return bfd_reloc_overflow
;
983 case R_MMIX_BASE_PLUS_OFFSET
:
985 struct bpo_reloc_section_info
*bpodata
986 = (struct bpo_reloc_section_info
*)
987 elf_section_data (isec
)->tdata
;
988 asection
*bpo_greg_section
989 = bpodata
->bpo_greg_section
;
990 struct bpo_greg_section_info
*gregdata
991 = (struct bpo_greg_section_info
*)
992 elf_section_data (bpo_greg_section
)->tdata
;
994 = gregdata
->bpo_reloc_indexes
[bpodata
->bpo_index
++];
996 /* A consistency check: The value we now have in "relocation" must
997 be the same as the value we stored for that relocation. It
998 doesn't cost much, so can be left in at all times. */
999 if (value
!= gregdata
->reloc_request
[bpo_index
].value
)
1001 (*_bfd_error_handler
)
1002 (_("%s: Internal inconsistency error for value for\n\
1003 linker-allocated global register: linked: 0x%lx%08lx != relaxed: 0x%lx%08lx\n"),
1004 bfd_get_filename (isec
->owner
),
1005 (unsigned long) (value
>> 32), (unsigned long) value
,
1006 (unsigned long) (gregdata
->reloc_request
[bpo_index
].value
1008 (unsigned long) gregdata
->reloc_request
[bpo_index
].value
);
1009 bfd_set_error (bfd_error_bad_value
);
1010 return bfd_reloc_overflow
;
1013 /* Then store the register number and offset for that register
1014 into datap and datap + 1 respectively. */
1016 gregdata
->reloc_request
[bpo_index
].regindex
1017 + bpo_greg_section
->output_section
->vma
/ 8,
1020 gregdata
->reloc_request
[bpo_index
].offset
,
1021 ((unsigned char *) datap
) + 1);
1022 return bfd_reloc_ok
;
1025 case R_MMIX_REG_OR_BYTE
:
1028 return bfd_reloc_overflow
;
1029 bfd_put_8 (abfd
, value
, datap
);
1030 return bfd_reloc_ok
;
1033 BAD_CASE (howto
->type
);
1036 /* This code adds the common SETL/INCML/INCMH/INCH worst-case
1039 /* Lowest two bits must be 0. We return bfd_reloc_overflow for
1040 everything that looks strange. */
1042 flag
= bfd_reloc_overflow
;
1045 (SETL_INSN_BYTE
<< 24) | (value
& 0xffff) | (reg
<< 16),
1046 (bfd_byte
*) datap
+ offs
);
1048 (INCML_INSN_BYTE
<< 24) | ((value
>> 16) & 0xffff) | (reg
<< 16),
1049 (bfd_byte
*) datap
+ offs
+ 4);
1051 (INCMH_INSN_BYTE
<< 24) | ((value
>> 32) & 0xffff) | (reg
<< 16),
1052 (bfd_byte
*) datap
+ offs
+ 8);
1054 (INCH_INSN_BYTE
<< 24) | ((value
>> 48) & 0xffff) | (reg
<< 16),
1055 (bfd_byte
*) datap
+ offs
+ 12);
1060 /* Set the howto pointer for an MMIX ELF reloc (type RELA). */
1063 mmix_info_to_howto_rela (abfd
, cache_ptr
, dst
)
1064 bfd
*abfd ATTRIBUTE_UNUSED
;
1066 Elf64_Internal_Rela
*dst
;
1068 unsigned int r_type
;
1070 r_type
= ELF64_R_TYPE (dst
->r_info
);
1071 BFD_ASSERT (r_type
< (unsigned int) R_MMIX_max
);
1072 cache_ptr
->howto
= &elf_mmix_howto_table
[r_type
];
1075 /* Any MMIX-specific relocation gets here at assembly time or when linking
1076 to other formats (such as mmo); this is the relocation function from
1077 the reloc_table. We don't get here for final pure ELF linking. */
1079 static bfd_reloc_status_type
1080 mmix_elf_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
,
1081 output_bfd
, error_message
)
1083 arelent
*reloc_entry
;
1086 asection
*input_section
;
1088 char **error_message ATTRIBUTE_UNUSED
;
1091 bfd_reloc_status_type r
;
1092 asection
*reloc_target_output_section
;
1093 bfd_reloc_status_type flag
= bfd_reloc_ok
;
1094 bfd_vma output_base
= 0;
1097 r
= bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
1098 input_section
, output_bfd
, error_message
);
1100 /* If that was all that was needed (i.e. this isn't a final link, only
1101 some segment adjustments), we're done. */
1102 if (r
!= bfd_reloc_continue
)
1105 if (bfd_is_und_section (symbol
->section
)
1106 && (symbol
->flags
& BSF_WEAK
) == 0
1107 && output_bfd
== (bfd
*) NULL
)
1108 return bfd_reloc_undefined
;
1110 /* Is the address of the relocation really within the section? */
1111 if (reloc_entry
->address
> input_section
->_cooked_size
)
1112 return bfd_reloc_outofrange
;
1114 /* Work out which section the relocation is targetted at and the
1115 initial relocation command value. */
1117 /* Get symbol value. (Common symbols are special.) */
1118 if (bfd_is_com_section (symbol
->section
))
1121 relocation
= symbol
->value
;
1123 reloc_target_output_section
= bfd_get_output_section (symbol
);
1125 /* Here the variable relocation holds the final address of the symbol we
1126 are relocating against, plus any addend. */
1130 output_base
= reloc_target_output_section
->vma
;
1132 relocation
+= output_base
+ symbol
->section
->output_offset
;
1134 /* Get position of relocation. */
1135 addr
= (reloc_entry
->address
+ input_section
->output_section
->vma
1136 + input_section
->output_offset
);
1137 if (output_bfd
!= (bfd
*) NULL
)
1139 /* Add in supplied addend. */
1140 relocation
+= reloc_entry
->addend
;
1142 /* This is a partial relocation, and we want to apply the
1143 relocation to the reloc entry rather than the raw data.
1144 Modify the reloc inplace to reflect what we now know. */
1145 reloc_entry
->addend
= relocation
;
1146 reloc_entry
->address
+= input_section
->output_offset
;
1150 return mmix_final_link_relocate (reloc_entry
->howto
, input_section
,
1151 data
, reloc_entry
->address
,
1152 reloc_entry
->addend
, relocation
,
1153 bfd_asymbol_name (symbol
),
1154 reloc_target_output_section
);
1157 /* Relocate an MMIX ELF section. Modified from elf32-fr30.c; look to it
1158 for guidance if you're thinking of copying this. */
1161 mmix_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1162 contents
, relocs
, local_syms
, local_sections
)
1163 bfd
*output_bfd ATTRIBUTE_UNUSED
;
1164 struct bfd_link_info
*info
;
1166 asection
*input_section
;
1168 Elf_Internal_Rela
*relocs
;
1169 Elf_Internal_Sym
*local_syms
;
1170 asection
**local_sections
;
1172 Elf_Internal_Shdr
*symtab_hdr
;
1173 struct elf_link_hash_entry
**sym_hashes
;
1174 Elf_Internal_Rela
*rel
;
1175 Elf_Internal_Rela
*relend
;
1177 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1178 sym_hashes
= elf_sym_hashes (input_bfd
);
1179 relend
= relocs
+ input_section
->reloc_count
;
1181 for (rel
= relocs
; rel
< relend
; rel
++)
1183 reloc_howto_type
*howto
;
1184 unsigned long r_symndx
;
1185 Elf_Internal_Sym
*sym
;
1187 struct elf_link_hash_entry
*h
;
1189 bfd_reloc_status_type r
;
1190 const char *name
= NULL
;
1192 boolean undefined_signalled
= false;
1194 r_type
= ELF64_R_TYPE (rel
->r_info
);
1196 if (r_type
== R_MMIX_GNU_VTINHERIT
1197 || r_type
== R_MMIX_GNU_VTENTRY
)
1200 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1202 if (info
->relocateable
)
1204 /* This is a relocateable link. We don't have to change
1205 anything, unless the reloc is against a section symbol,
1206 in which case we have to adjust according to where the
1207 section symbol winds up in the output section. */
1208 if (r_symndx
< symtab_hdr
->sh_info
)
1210 sym
= local_syms
+ r_symndx
;
1212 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1214 sec
= local_sections
[r_symndx
];
1215 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
1222 /* This is a final link. */
1223 howto
= elf_mmix_howto_table
+ ELF64_R_TYPE (rel
->r_info
);
1228 if (r_symndx
< symtab_hdr
->sh_info
)
1230 sym
= local_syms
+ r_symndx
;
1231 sec
= local_sections
[r_symndx
];
1232 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, sec
, rel
);
1234 name
= bfd_elf_string_from_elf_section
1235 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1236 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
1240 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1242 while (h
->root
.type
== bfd_link_hash_indirect
1243 || h
->root
.type
== bfd_link_hash_warning
)
1244 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1246 name
= h
->root
.root
.string
;
1248 if (h
->root
.type
== bfd_link_hash_defined
1249 || h
->root
.type
== bfd_link_hash_defweak
)
1251 sec
= h
->root
.u
.def
.section
;
1252 relocation
= (h
->root
.u
.def
.value
1253 + sec
->output_section
->vma
1254 + sec
->output_offset
);
1256 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1258 else if (info
->shared
1259 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
1263 /* The test on undefined_signalled is redundant at the
1264 moment, but kept for symmetry. */
1265 if (! undefined_signalled
1266 && ! ((*info
->callbacks
->undefined_symbol
)
1267 (info
, h
->root
.root
.string
, input_bfd
,
1268 input_section
, rel
->r_offset
, true)))
1270 undefined_signalled
= true;
1275 r
= mmix_final_link_relocate (howto
, input_section
,
1276 contents
, rel
->r_offset
,
1277 rel
->r_addend
, relocation
, name
, sec
);
1279 if (r
!= bfd_reloc_ok
)
1281 boolean check_ok
= true;
1282 const char * msg
= (const char *) NULL
;
1286 case bfd_reloc_overflow
:
1287 check_ok
= info
->callbacks
->reloc_overflow
1288 (info
, name
, howto
->name
, (bfd_vma
) 0,
1289 input_bfd
, input_section
, rel
->r_offset
);
1292 case bfd_reloc_undefined
:
1293 /* We may have sent this message above. */
1294 if (! undefined_signalled
)
1295 check_ok
= info
->callbacks
->undefined_symbol
1296 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
1298 undefined_signalled
= true;
1301 case bfd_reloc_outofrange
:
1302 msg
= _("internal error: out of range error");
1305 case bfd_reloc_notsupported
:
1306 msg
= _("internal error: unsupported relocation error");
1309 case bfd_reloc_dangerous
:
1310 msg
= _("internal error: dangerous relocation");
1314 msg
= _("internal error: unknown error");
1319 check_ok
= info
->callbacks
->warning
1320 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
1330 /* Perform a single relocation. By default we use the standard BFD
1331 routines. A few relocs we have to do ourselves. */
1333 static bfd_reloc_status_type
1334 mmix_final_link_relocate (howto
, input_section
, contents
,
1335 r_offset
, r_addend
, relocation
, symname
, symsec
)
1336 reloc_howto_type
*howto
;
1337 asection
*input_section
;
1340 bfd_signed_vma r_addend
;
1342 const char *symname
;
1345 bfd_reloc_status_type r
= bfd_reloc_ok
;
1347 = (input_section
->output_section
->vma
1348 + input_section
->output_offset
1351 = (bfd_signed_vma
) relocation
+ r_addend
;
1353 switch (howto
->type
)
1355 /* All these are PC-relative. */
1357 case R_MMIX_CBRANCH
:
1362 contents
+= r_offset
;
1364 srel
-= (input_section
->output_section
->vma
1365 + input_section
->output_offset
1368 r
= mmix_elf_perform_relocation (input_section
, howto
, contents
,
1372 case R_MMIX_BASE_PLUS_OFFSET
:
1374 return bfd_reloc_undefined
;
1376 /* Check that we're not relocating against a register symbol. */
1377 if (strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1378 MMIX_REG_CONTENTS_SECTION_NAME
) == 0
1379 || strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1380 MMIX_REG_SECTION_NAME
) == 0)
1382 /* Note: This is separated out into two messages in order
1383 to ease the translation into other languages. */
1384 if (symname
== NULL
|| *symname
== 0)
1385 (*_bfd_error_handler
)
1386 (_("%s: base-plus-offset relocation against register symbol: (unknown) in %s"),
1387 bfd_get_filename (input_section
->owner
),
1388 bfd_get_section_name (symsec
->owner
, symsec
));
1390 (*_bfd_error_handler
)
1391 (_("%s: base-plus-offset relocation against register symbol: %s in %s"),
1392 bfd_get_filename (input_section
->owner
), symname
,
1393 bfd_get_section_name (symsec
->owner
, symsec
));
1394 return bfd_reloc_overflow
;
1398 case R_MMIX_REG_OR_BYTE
:
1400 /* For now, we handle these alike. They must refer to an register
1401 symbol, which is either relative to the register section and in
1402 the range 0..255, or is in the register contents section with vma
1405 /* FIXME: A better way to check for reg contents section?
1406 FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1408 return bfd_reloc_undefined
;
1410 if (strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1411 MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
1413 if ((srel
& 7) != 0 || srel
< 32*8 || srel
> 255*8)
1415 /* The bfd_reloc_outofrange return value, though intuitively
1416 a better value, will not get us an error. */
1417 return bfd_reloc_overflow
;
1421 else if (strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1422 MMIX_REG_SECTION_NAME
) == 0)
1424 if (srel
< 0 || srel
> 255)
1425 /* The bfd_reloc_outofrange return value, though intuitively a
1426 better value, will not get us an error. */
1427 return bfd_reloc_overflow
;
1431 /* Note: This is separated out into two messages in order
1432 to ease the translation into other languages. */
1433 if (symname
== NULL
|| *symname
== 0)
1434 (*_bfd_error_handler
)
1435 (_("%s: register relocation against non-register symbol: (unknown) in %s"),
1436 bfd_get_filename (input_section
->owner
),
1437 bfd_get_section_name (symsec
->owner
, symsec
));
1439 (*_bfd_error_handler
)
1440 (_("%s: register relocation against non-register symbol: %s in %s"),
1441 bfd_get_filename (input_section
->owner
), symname
,
1442 bfd_get_section_name (symsec
->owner
, symsec
));
1444 /* The bfd_reloc_outofrange return value, though intuitively a
1445 better value, will not get us an error. */
1446 return bfd_reloc_overflow
;
1449 contents
+= r_offset
;
1450 r
= mmix_elf_perform_relocation (input_section
, howto
, contents
,
1455 /* This isn't a real relocation, it's just an assertion that the
1456 final relocation value corresponds to a local register. We
1457 ignore the actual relocation; nothing is changed. */
1460 = bfd_get_section_by_name (input_section
->output_section
->owner
,
1461 MMIX_REG_CONTENTS_SECTION_NAME
);
1462 bfd_vma first_global
;
1464 /* Check that this is an absolute value, or a reference to the
1465 register contents section or the register (symbol) section.
1466 Absolute numbers can get here as undefined section. Undefined
1467 symbols are signalled elsewhere, so there's no conflict in us
1468 accidentally handling it. */
1469 if (!bfd_is_abs_section (symsec
)
1470 && !bfd_is_und_section (symsec
)
1471 && strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1472 MMIX_REG_CONTENTS_SECTION_NAME
) != 0
1473 && strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1474 MMIX_REG_SECTION_NAME
) != 0)
1476 (*_bfd_error_handler
)
1477 (_("%s: directive LOCAL valid only with a register or absolute value"),
1478 bfd_get_filename (input_section
->owner
));
1480 return bfd_reloc_overflow
;
1483 /* If we don't have a register contents section, then $255 is the
1484 first global register. */
1489 first_global
= bfd_get_section_vma (abfd
, regsec
) / 8;
1490 if (strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1491 MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
1493 if ((srel
& 7) != 0 || srel
< 32*8 || srel
> 255*8)
1494 /* The bfd_reloc_outofrange return value, though
1495 intuitively a better value, will not get us an error. */
1496 return bfd_reloc_overflow
;
1501 if ((bfd_vma
) srel
>= first_global
)
1503 /* FIXME: Better error message. */
1504 (*_bfd_error_handler
)
1505 (_("%s: LOCAL directive: Register $%ld is not a local register. First global register is $%ld."),
1506 bfd_get_filename (input_section
->owner
), (long) srel
, (long) first_global
);
1508 return bfd_reloc_overflow
;
1515 r
= _bfd_final_link_relocate (howto
, input_section
->owner
, input_section
,
1517 relocation
, r_addend
);
1523 /* Return the section that should be marked against GC for a given
1527 mmix_elf_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
1529 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1530 Elf_Internal_Rela
*rel
;
1531 struct elf_link_hash_entry
*h
;
1532 Elf_Internal_Sym
*sym
;
1536 switch (ELF64_R_TYPE (rel
->r_info
))
1538 case R_MMIX_GNU_VTINHERIT
:
1539 case R_MMIX_GNU_VTENTRY
:
1543 switch (h
->root
.type
)
1545 case bfd_link_hash_defined
:
1546 case bfd_link_hash_defweak
:
1547 return h
->root
.u
.def
.section
;
1549 case bfd_link_hash_common
:
1550 return h
->root
.u
.c
.p
->section
;
1559 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
1565 /* Update relocation info for a GC-excluded section. We could supposedly
1566 perform the allocation after GC, but there's no suitable hook between
1567 GC (or section merge) and the point when all input sections must be
1568 present. Better to waste some memory and (perhaps) a little time. */
1571 mmix_elf_gc_sweep_hook (abfd
, info
, sec
, relocs
)
1572 bfd
*abfd ATTRIBUTE_UNUSED
;
1573 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1574 asection
*sec ATTRIBUTE_UNUSED
;
1575 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
;
1577 struct bpo_reloc_section_info
*bpodata
1578 = (struct bpo_reloc_section_info
*)
1579 elf_section_data (sec
)->tdata
;
1580 asection
*allocated_gregs_section
;
1582 /* If no bpodata here, we have nothing to do. */
1583 if (bpodata
== NULL
)
1586 allocated_gregs_section
= bpodata
->bpo_greg_section
;
1588 ((struct bpo_greg_section_info
*)
1589 elf_section_data (allocated_gregs_section
)->tdata
)
1591 -= bpodata
->n_bpo_relocs_this_section
;
1596 /* Sort register relocs to come before expanding relocs. */
1599 mmix_elf_sort_relocs (p1
, p2
)
1603 const Elf_Internal_Rela
*r1
= (const Elf_Internal_Rela
*) p1
;
1604 const Elf_Internal_Rela
*r2
= (const Elf_Internal_Rela
*) p2
;
1605 int r1_is_reg
, r2_is_reg
;
1607 /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1609 if ((r1
->r_offset
& ~(bfd_vma
) 3) > (r2
->r_offset
& ~(bfd_vma
) 3))
1611 else if ((r1
->r_offset
& ~(bfd_vma
) 3) < (r2
->r_offset
& ~(bfd_vma
) 3))
1615 = (ELF64_R_TYPE (r1
->r_info
) == R_MMIX_REG_OR_BYTE
1616 || ELF64_R_TYPE (r1
->r_info
) == R_MMIX_REG
);
1618 = (ELF64_R_TYPE (r2
->r_info
) == R_MMIX_REG_OR_BYTE
1619 || ELF64_R_TYPE (r2
->r_info
) == R_MMIX_REG
);
1620 if (r1_is_reg
!= r2_is_reg
)
1621 return r2_is_reg
- r1_is_reg
;
1623 /* Neither or both are register relocs. Then sort on full offset. */
1624 if (r1
->r_offset
> r2
->r_offset
)
1626 else if (r1
->r_offset
< r2
->r_offset
)
1631 /* Subset of mmix_elf_check_relocs, common to ELF and mmo linking. */
1634 mmix_elf_check_common_relocs (abfd
, info
, sec
, relocs
)
1636 struct bfd_link_info
*info
;
1638 const Elf_Internal_Rela
*relocs
;
1640 bfd
*bpo_greg_owner
= NULL
;
1641 asection
*allocated_gregs_section
= NULL
;
1642 struct bpo_greg_section_info
*gregdata
= NULL
;
1643 struct bpo_reloc_section_info
*bpodata
= NULL
;
1644 const Elf_Internal_Rela
*rel
;
1645 const Elf_Internal_Rela
*rel_end
;
1647 if (info
->relocateable
)
1650 /* We currently have to abuse this COFF-specific member, since there's
1651 no target-machine-dedicated member. There's no alternative outside
1652 the bfd_link_info struct; we can't specialize a hash-table since
1653 they're different between ELF and mmo. */
1654 bpo_greg_owner
= (bfd
*) info
->base_file
;
1656 rel_end
= relocs
+ sec
->reloc_count
;
1657 for (rel
= relocs
; rel
< rel_end
; rel
++)
1659 switch (ELF64_R_TYPE (rel
->r_info
))
1661 /* This relocation causes a GREG allocation. We need to count
1662 them, and we need to create a section for them, so we need an
1663 object to fake as the owner of that section. We can't use
1664 the ELF dynobj for this, since the ELF bits assume lots of
1665 DSO-related stuff if that member is non-NULL. */
1666 case R_MMIX_BASE_PLUS_OFFSET
:
1667 if (bpo_greg_owner
== NULL
)
1669 bpo_greg_owner
= abfd
;
1670 info
->base_file
= (PTR
) bpo_greg_owner
;
1673 if (allocated_gregs_section
== NULL
)
1674 allocated_gregs_section
1675 = bfd_get_section_by_name (bpo_greg_owner
,
1676 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
1678 if (allocated_gregs_section
== NULL
)
1680 allocated_gregs_section
1681 = bfd_make_section (bpo_greg_owner
,
1682 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
1683 /* Setting both SEC_ALLOC and SEC_LOAD means the section is
1684 treated like any other section, and we'd get errors for
1685 address overlap with the text section. Let's set none of
1686 those flags, as that is what currently happens for usual
1687 GREG allocations, and that works. */
1688 if (allocated_gregs_section
== NULL
1689 || !bfd_set_section_flags (bpo_greg_owner
,
1690 allocated_gregs_section
,
1693 | SEC_LINKER_CREATED
))
1694 || !bfd_set_section_alignment (bpo_greg_owner
,
1695 allocated_gregs_section
,
1699 gregdata
= (struct bpo_greg_section_info
*)
1700 bfd_zalloc (bpo_greg_owner
, sizeof (struct bpo_greg_section_info
));
1701 if (gregdata
== NULL
)
1703 elf_section_data (allocated_gregs_section
)->tdata
= gregdata
;
1705 else if (gregdata
== NULL
)
1706 gregdata
= elf_section_data (allocated_gregs_section
)->tdata
;
1708 /* Get ourselves some auxiliary info for the BPO-relocs. */
1709 if (bpodata
== NULL
)
1711 /* No use doing a separate iteration pass to find the upper
1712 limit - just use the number of relocs. */
1713 bpodata
= (struct bpo_reloc_section_info
*)
1714 bfd_alloc (bpo_greg_owner
,
1715 sizeof (struct bpo_reloc_section_info
)
1716 * (sec
->reloc_count
+ 1));
1717 if (bpodata
== NULL
)
1719 elf_section_data (sec
)->tdata
= bpodata
;
1720 bpodata
->first_base_plus_offset_reloc
1721 = bpodata
->bpo_index
1722 = gregdata
->n_max_bpo_relocs
;
1723 bpodata
->bpo_greg_section
1724 = allocated_gregs_section
;
1725 bpodata
->n_bpo_relocs_this_section
= 0;
1728 bpodata
->n_bpo_relocs_this_section
++;
1729 gregdata
->n_max_bpo_relocs
++;
1731 /* We don't get another chance to set this before GC; we've not
1732 set up set up any hook that runs before GC. */
1733 gregdata
->n_bpo_relocs
1734 = gregdata
->n_max_bpo_relocs
;
1742 /* Look through the relocs for a section during the first phase. */
1745 mmix_elf_check_relocs (abfd
, info
, sec
, relocs
)
1747 struct bfd_link_info
*info
;
1749 const Elf_Internal_Rela
*relocs
;
1751 Elf_Internal_Shdr
*symtab_hdr
;
1752 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
1753 const Elf_Internal_Rela
*rel
;
1754 const Elf_Internal_Rela
*rel_end
;
1756 if (info
->relocateable
)
1759 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1760 sym_hashes
= elf_sym_hashes (abfd
);
1761 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof(Elf64_External_Sym
);
1762 if (!elf_bad_symtab (abfd
))
1763 sym_hashes_end
-= symtab_hdr
->sh_info
;
1765 /* First we sort the relocs so that any register relocs come before
1766 expansion-relocs to the same insn. FIXME: Not done for mmo. */
1767 qsort ((PTR
) relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
1768 mmix_elf_sort_relocs
);
1770 /* Do the common part. */
1771 if (!mmix_elf_check_common_relocs (abfd
, info
, sec
, relocs
))
1774 rel_end
= relocs
+ sec
->reloc_count
;
1775 for (rel
= relocs
; rel
< rel_end
; rel
++)
1777 struct elf_link_hash_entry
*h
;
1778 unsigned long r_symndx
;
1780 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1781 if (r_symndx
< symtab_hdr
->sh_info
)
1784 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1786 switch (ELF64_R_TYPE (rel
->r_info
))
1788 /* This relocation describes the C++ object vtable hierarchy.
1789 Reconstruct it for later use during GC. */
1790 case R_MMIX_GNU_VTINHERIT
:
1791 if (!_bfd_elf64_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1795 /* This relocation describes which C++ vtable entries are actually
1796 used. Record for later use during GC. */
1797 case R_MMIX_GNU_VTENTRY
:
1798 if (!_bfd_elf64_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1807 /* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
1808 Copied from elf_link_add_object_symbols. */
1811 _bfd_mmix_check_all_relocs (abfd
, info
)
1813 struct bfd_link_info
*info
;
1817 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1819 Elf_Internal_Rela
*internal_relocs
;
1822 if ((o
->flags
& SEC_RELOC
) == 0
1823 || o
->reloc_count
== 0
1824 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1825 && (o
->flags
& SEC_DEBUGGING
) != 0)
1826 || bfd_is_abs_section (o
->output_section
))
1830 = _bfd_elf64_link_read_relocs (abfd
, o
, (PTR
) NULL
,
1831 (Elf_Internal_Rela
*) NULL
,
1833 if (internal_relocs
== NULL
)
1836 ok
= mmix_elf_check_common_relocs (abfd
, info
, o
, internal_relocs
);
1838 if (! info
->keep_memory
)
1839 free (internal_relocs
);
1848 /* Change symbols relative to the reg contents section to instead be to
1849 the register section, and scale them down to correspond to the register
1853 mmix_elf_link_output_symbol_hook (abfd
, info
, name
, sym
, input_sec
)
1854 bfd
*abfd ATTRIBUTE_UNUSED
;
1855 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1856 const char *name ATTRIBUTE_UNUSED
;
1857 Elf_Internal_Sym
*sym
;
1858 asection
*input_sec
;
1860 if (input_sec
!= NULL
1861 && input_sec
->name
!= NULL
1862 && ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
1863 && strcmp (input_sec
->name
, MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
1866 sym
->st_shndx
= SHN_REGISTER
;
1872 /* We fake a register section that holds values that are register numbers.
1873 Having a SHN_REGISTER and register section translates better to other
1874 formats (e.g. mmo) than for example a STT_REGISTER attribute.
1875 This section faking is based on a construct in elf32-mips.c. */
1876 static asection mmix_elf_reg_section
;
1877 static asymbol mmix_elf_reg_section_symbol
;
1878 static asymbol
*mmix_elf_reg_section_symbol_ptr
;
1880 /* Handle the special MIPS section numbers that a symbol may use.
1881 This is used for both the 32-bit and the 64-bit ABI. */
1884 mmix_elf_symbol_processing (abfd
, asym
)
1885 bfd
*abfd ATTRIBUTE_UNUSED
;
1888 elf_symbol_type
*elfsym
;
1890 elfsym
= (elf_symbol_type
*) asym
;
1891 switch (elfsym
->internal_elf_sym
.st_shndx
)
1894 if (mmix_elf_reg_section
.name
== NULL
)
1896 /* Initialize the register section. */
1897 mmix_elf_reg_section
.name
= MMIX_REG_SECTION_NAME
;
1898 mmix_elf_reg_section
.flags
= SEC_NO_FLAGS
;
1899 mmix_elf_reg_section
.output_section
= &mmix_elf_reg_section
;
1900 mmix_elf_reg_section
.symbol
= &mmix_elf_reg_section_symbol
;
1901 mmix_elf_reg_section
.symbol_ptr_ptr
= &mmix_elf_reg_section_symbol_ptr
;
1902 mmix_elf_reg_section_symbol
.name
= MMIX_REG_SECTION_NAME
;
1903 mmix_elf_reg_section_symbol
.flags
= BSF_SECTION_SYM
;
1904 mmix_elf_reg_section_symbol
.section
= &mmix_elf_reg_section
;
1905 mmix_elf_reg_section_symbol_ptr
= &mmix_elf_reg_section_symbol
;
1907 asym
->section
= &mmix_elf_reg_section
;
1915 /* Given a BFD section, try to locate the corresponding ELF section
1919 mmix_elf_section_from_bfd_section (abfd
, sec
, retval
)
1920 bfd
* abfd ATTRIBUTE_UNUSED
;
1924 if (strcmp (bfd_get_section_name (abfd
, sec
), MMIX_REG_SECTION_NAME
) == 0)
1925 *retval
= SHN_REGISTER
;
1932 /* Hook called by the linker routine which adds symbols from an object
1933 file. We must handle the special SHN_REGISTER section number here.
1935 We also check that we only have *one* each of the section-start
1936 symbols, since otherwise having two with the same value would cause
1937 them to be "merged", but with the contents serialized. */
1940 mmix_elf_add_symbol_hook (abfd
, info
, sym
, namep
, flagsp
, secp
, valp
)
1942 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1943 const Elf_Internal_Sym
*sym
;
1944 const char **namep ATTRIBUTE_UNUSED
;
1945 flagword
*flagsp ATTRIBUTE_UNUSED
;
1947 bfd_vma
*valp ATTRIBUTE_UNUSED
;
1949 if (sym
->st_shndx
== SHN_REGISTER
)
1950 *secp
= bfd_make_section_old_way (abfd
, MMIX_REG_SECTION_NAME
);
1951 else if ((*namep
)[0] == '_' && (*namep
)[1] == '_' && (*namep
)[2] == '.'
1952 && strncmp (*namep
, MMIX_LOC_SECTION_START_SYMBOL_PREFIX
,
1953 strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX
)) == 0)
1955 /* See if we have another one. */
1956 struct bfd_link_hash_entry
*h
= bfd_link_hash_lookup (info
->hash
,
1962 if (h
!= NULL
&& h
->type
!= bfd_link_hash_undefined
)
1964 /* How do we get the asymbol (or really: the filename) from h?
1965 h->u.def.section->owner is NULL. */
1966 ((*_bfd_error_handler
)
1967 (_("%s: Error: multiple definition of `%s'; start of %s is set in a earlier linked file\n"),
1968 bfd_get_filename (abfd
), *namep
,
1969 *namep
+ strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX
)));
1970 bfd_set_error (bfd_error_bad_value
);
1978 /* We consider symbols matching "L.*:[0-9]+" to be local symbols. */
1981 mmix_elf_is_local_label_name (abfd
, name
)
1988 /* Also include the default local-label definition. */
1989 if (_bfd_elf_is_local_label_name (abfd
, name
))
1995 /* If there's no ":", or more than one, it's not a local symbol. */
1996 colpos
= strchr (name
, ':');
1997 if (colpos
== NULL
|| strchr (colpos
+ 1, ':') != NULL
)
2000 /* Check that there are remaining characters and that they are digits. */
2004 digits
= strspn (colpos
+ 1, "0123456789");
2005 return digits
!= 0 && colpos
[1 + digits
] == 0;
2008 /* We get rid of the register section here. */
2011 mmix_elf_final_link (abfd
, info
)
2013 struct bfd_link_info
*info
;
2015 /* We never output a register section, though we create one for
2016 temporary measures. Check that nobody entered contents into it. */
2017 asection
*reg_section
;
2020 reg_section
= bfd_get_section_by_name (abfd
, MMIX_REG_SECTION_NAME
);
2022 if (reg_section
!= NULL
)
2024 /* FIXME: Pass error state gracefully. */
2025 if (bfd_get_section_flags (abfd
, reg_section
) & SEC_HAS_CONTENTS
)
2026 _bfd_abort (__FILE__
, __LINE__
, _("Register section has contents\n"));
2028 /* Really remove the section. */
2029 for (secpp
= &abfd
->sections
;
2030 *secpp
!= reg_section
;
2031 secpp
= &(*secpp
)->next
)
2033 bfd_section_list_remove (abfd
, secpp
);
2034 --abfd
->section_count
;
2037 if (! bfd_elf64_bfd_final_link (abfd
, info
))
2040 /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
2041 the regular linker machinery. We do it here, like other targets with
2042 special sections. */
2043 if (info
->base_file
!= NULL
)
2045 asection
*greg_section
2046 = bfd_get_section_by_name ((bfd
*) info
->base_file
,
2047 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
2048 if (!bfd_set_section_contents (abfd
,
2049 greg_section
->output_section
,
2050 greg_section
->contents
,
2051 (file_ptr
) greg_section
->output_offset
,
2052 greg_section
->_cooked_size
))
2058 /* Initialize stuff for the linker-generated GREGs to match
2059 R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker. */
2062 _bfd_mmix_prepare_linker_allocated_gregs (abfd
, info
)
2063 bfd
*abfd ATTRIBUTE_UNUSED
;
2064 struct bfd_link_info
*info
;
2066 asection
*bpo_gregs_section
;
2067 bfd
*bpo_greg_owner
;
2068 struct bpo_greg_section_info
*gregdata
;
2072 size_t *bpo_reloc_indexes
;
2074 /* The bpo_greg_owner bfd is supposed to have been set by
2075 mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
2076 If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2077 bpo_greg_owner
= (bfd
*) info
->base_file
;
2078 if (bpo_greg_owner
== NULL
)
2082 = bfd_get_section_by_name (bpo_greg_owner
,
2083 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
2085 if (bpo_gregs_section
== NULL
)
2088 /* We use the target-data handle in the ELF section data. */
2089 gregdata
= (struct bpo_greg_section_info
*)
2090 elf_section_data (bpo_gregs_section
)->tdata
;
2091 if (gregdata
== NULL
)
2094 n_gregs
= gregdata
->n_bpo_relocs
;
2095 gregdata
->n_allocated_bpo_gregs
= n_gregs
;
2097 /* When this reaches zero during relaxation, all entries have been
2098 filled in and the size of the linker gregs can be calculated. */
2099 gregdata
->n_remaining_bpo_relocs_this_relaxation_round
= n_gregs
;
2101 /* Set the zeroth-order estimate for the GREGs size. */
2102 gregs_size
= n_gregs
* 8;
2104 if (!bfd_set_section_size (bpo_greg_owner
, bpo_gregs_section
, gregs_size
))
2107 /* Allocate and set up the GREG arrays. They're filled in at relaxation
2108 time. Note that we must use the max number ever noted for the array,
2109 since the index numbers were created before GC. */
2110 gregdata
->reloc_request
2111 = bfd_zalloc (bpo_greg_owner
,
2112 sizeof (struct bpo_reloc_request
)
2113 * gregdata
->n_max_bpo_relocs
);
2115 gregdata
->bpo_reloc_indexes
2117 = bfd_alloc (bpo_greg_owner
,
2118 gregdata
->n_max_bpo_relocs
2120 if (bpo_reloc_indexes
== NULL
)
2123 /* The default order is an identity mapping. */
2124 for (i
= 0; i
< gregdata
->n_max_bpo_relocs
; i
++)
2126 bpo_reloc_indexes
[i
] = i
;
2127 gregdata
->reloc_request
[i
].bpo_reloc_no
= i
;
2133 /* Fill in contents in the linker allocated gregs. Everything is
2134 calculated at this point; we just move the contents into place here. */
2137 _bfd_mmix_finalize_linker_allocated_gregs (abfd
, link_info
)
2138 bfd
*abfd ATTRIBUTE_UNUSED
;
2139 struct bfd_link_info
*link_info
;
2141 asection
*bpo_gregs_section
;
2142 bfd
*bpo_greg_owner
;
2143 struct bpo_greg_section_info
*gregdata
;
2149 /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
2150 when the first R_MMIX_BASE_PLUS_OFFSET is seen. If there is no such
2151 object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2152 bpo_greg_owner
= (bfd
*) link_info
->base_file
;
2153 if (bpo_greg_owner
== NULL
)
2157 = bfd_get_section_by_name (bpo_greg_owner
,
2158 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
2160 /* This can't happen without DSO handling. When DSOs are handled
2161 without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
2163 if (bpo_gregs_section
== NULL
)
2166 /* We use the target-data handle in the ELF section data. */
2168 gregdata
= (struct bpo_greg_section_info
*)
2169 elf_section_data (bpo_gregs_section
)->tdata
;
2170 if (gregdata
== NULL
)
2173 n_gregs
= gregdata
->n_allocated_bpo_gregs
;
2175 bpo_gregs_section
->contents
2176 = contents
= bfd_alloc (bpo_greg_owner
, bpo_gregs_section
->_cooked_size
);
2177 if (contents
== NULL
)
2180 /* Sanity check: If these numbers mismatch, some relocation has not been
2181 accounted for and the rest of gregdata is probably inconsistent.
2182 It's a bug, but it's more helpful to identify it than segfaulting
2184 if (gregdata
->n_remaining_bpo_relocs_this_relaxation_round
2185 != gregdata
->n_bpo_relocs
)
2187 (*_bfd_error_handler
)
2188 (_("Internal inconsistency: remaining %u != max %u.\n\
2189 Please report this bug."),
2190 gregdata
->n_remaining_bpo_relocs_this_relaxation_round
,
2191 gregdata
->n_bpo_relocs
);
2195 for (lastreg
= 255, i
= 0, j
= 0; j
< n_gregs
; i
++)
2196 if (gregdata
->reloc_request
[i
].regindex
!= lastreg
)
2198 bfd_put_64 (bpo_greg_owner
, gregdata
->reloc_request
[i
].value
,
2200 lastreg
= gregdata
->reloc_request
[i
].regindex
;
2207 /* Sort valid relocs to come before non-valid relocs, then on increasing
2211 bpo_reloc_request_sort_fn (p1
, p2
)
2215 const struct bpo_reloc_request
*r1
= (const struct bpo_reloc_request
*) p1
;
2216 const struct bpo_reloc_request
*r2
= (const struct bpo_reloc_request
*) p2
;
2218 /* Primary function is validity; non-valid relocs sorted after valid
2220 if (r1
->valid
!= r2
->valid
)
2221 return r2
->valid
- r1
->valid
;
2223 /* Then sort on value. Don't simplify and return just the difference of
2224 the values: the upper bits of the 64-bit value would be truncated on
2225 a host with 32-bit ints. */
2226 if (r1
->value
!= r2
->value
)
2227 return r1
->value
> r2
->value
? 1 : -1;
2229 /* As a last re-sort, use the address so we get a stable sort. */
2230 return r1
> r2
? 1 : (r1
< r2
? -1 : 0);
2233 /* For debug use only. Dumps the global register allocations resulting
2234 from base-plus-offset relocs. */
2237 mmix_dump_bpo_gregs (link_info
, pf
)
2238 struct bfd_link_info
*link_info
;
2239 bfd_error_handler_type pf
;
2241 bfd
*bpo_greg_owner
;
2242 asection
*bpo_gregs_section
;
2243 struct bpo_greg_section_info
*gregdata
;
2246 if (link_info
== NULL
|| link_info
->base_file
== NULL
)
2249 bpo_greg_owner
= (bfd
*) link_info
->base_file
;
2252 = bfd_get_section_by_name (bpo_greg_owner
,
2253 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
2255 if (bpo_gregs_section
== NULL
)
2258 gregdata
= (struct bpo_greg_section_info
*)
2259 elf_section_data (bpo_gregs_section
)->tdata
;
2260 if (gregdata
== NULL
)
2264 pf
= _bfd_error_handler
;
2266 /* These format strings are not translated. They are for debug purposes
2267 only and never displayed to an end user. Should they escape, we
2268 surely want them in original. */
2269 (*pf
) (" n_bpo_relocs: %u\n n_max_bpo_relocs: %u\n n_remain...round: %u\n\
2270 n_allocated_bpo_gregs: %u\n", gregdata
->n_bpo_relocs
,
2271 gregdata
->n_max_bpo_relocs
,
2272 gregdata
->n_remaining_bpo_relocs_this_relaxation_round
,
2273 gregdata
->n_allocated_bpo_gregs
);
2275 if (gregdata
->reloc_request
)
2276 for (i
= 0; i
< gregdata
->n_max_bpo_relocs
; i
++)
2277 (*pf
) ("%4u (%4u)/%4u#%u: 0x%08lx%08lx r: %3u o: %3u\n",
2279 gregdata
->bpo_reloc_indexes
!= NULL
2280 ? gregdata
->bpo_reloc_indexes
[i
] : -1,
2281 gregdata
->reloc_request
[i
].bpo_reloc_no
,
2282 gregdata
->reloc_request
[i
].valid
,
2284 (unsigned long) (gregdata
->reloc_request
[i
].value
>> 32),
2285 (unsigned long) gregdata
->reloc_request
[i
].value
,
2286 gregdata
->reloc_request
[i
].regindex
,
2287 gregdata
->reloc_request
[i
].offset
);
2290 /* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
2291 when the last such reloc is done, an index-array is sorted according to
2292 the values and iterated over to produce register numbers (indexed by 0
2293 from the first allocated register number) and offsets for use in real
2296 Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
2299 mmix_elf_relax_section (abfd
, sec
, link_info
, again
)
2302 struct bfd_link_info
*link_info
;
2305 Elf_Internal_Shdr
*symtab_hdr
;
2306 Elf_Internal_Shdr
*shndx_hdr
;
2307 Elf_Internal_Rela
*internal_relocs
;
2308 Elf_Internal_Rela
*free_relocs
= NULL
;
2309 Elf_Internal_Rela
*irel
, *irelend
;
2310 asection
*bpo_gregs_section
= NULL
;
2311 struct bpo_greg_section_info
*gregdata
;
2312 struct bpo_reloc_section_info
*bpodata
2313 = (struct bpo_reloc_section_info
*)
2314 elf_section_data (sec
)->tdata
;
2316 bfd
*bpo_greg_owner
;
2317 Elf64_External_Sym
*extsyms
= NULL
;
2318 Elf64_External_Sym
*free_extsyms
= NULL
;
2319 Elf_External_Sym_Shndx
*shndx_buf
= NULL
;
2321 /* Assume nothing changes. */
2324 /* If this is the first time we have been called for this section,
2325 initialize the cooked size. */
2326 if (sec
->_cooked_size
== 0)
2327 sec
->_cooked_size
= sec
->_raw_size
;
2329 /* We don't have to do anything for a relocateable link, if
2330 this section does not have relocs, or if this is not a
2332 if (link_info
->relocateable
2333 || (sec
->flags
& SEC_RELOC
) == 0
2334 || sec
->reloc_count
== 0
2335 || (sec
->flags
& SEC_CODE
) == 0
2336 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2337 /* If no R_MMIX_BASE_PLUS_OFFSET relocs, then nothing to do. */
2341 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2342 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2344 bpo_greg_owner
= (bfd
*) link_info
->base_file
;
2345 bpo_gregs_section
= bpodata
->bpo_greg_section
;
2346 gregdata
= (struct bpo_greg_section_info
*)
2347 elf_section_data (bpo_gregs_section
)->tdata
;
2349 bpono
= bpodata
->first_base_plus_offset_reloc
;
2351 /* Get a copy of the native relocations. */
2353 = _bfd_elf64_link_read_relocs (abfd
, sec
, (PTR
) NULL
,
2354 (Elf_Internal_Rela
*) NULL
,
2355 link_info
->keep_memory
);
2356 if (internal_relocs
== NULL
)
2358 if (! link_info
->keep_memory
)
2359 free_relocs
= internal_relocs
;
2361 /* Walk through them looking for relaxing opportunities. */
2362 irelend
= internal_relocs
+ sec
->reloc_count
;
2363 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2367 if (ELF64_R_TYPE (irel
->r_info
) != (int) R_MMIX_BASE_PLUS_OFFSET
)
2370 /* Read this BFD's symbols if we haven't done so already. */
2371 if (extsyms
== NULL
)
2373 /* Get cached copy if it exists. */
2374 if (symtab_hdr
->contents
!= NULL
)
2375 extsyms
= (Elf64_External_Sym
*) symtab_hdr
->contents
;
2378 /* Go get them off disk. */
2381 amt
= symtab_hdr
->sh_info
;
2382 amt
*= sizeof (Elf64_External_Sym
);
2383 extsyms
= (Elf64_External_Sym
*) bfd_malloc (amt
);
2384 if (extsyms
== NULL
)
2386 free_extsyms
= extsyms
;
2387 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
2388 || bfd_bread ((PTR
) extsyms
, amt
, abfd
) != amt
)
2390 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2393 /* If >64k sections, this presumable happens. No test-case. */
2394 if (shndx_hdr
->sh_size
!= 0)
2398 amt
= symtab_hdr
->sh_info
;
2399 amt
*= sizeof (Elf_External_Sym_Shndx
);
2400 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
2401 if (shndx_buf
== NULL
)
2403 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
2404 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
2406 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
2410 /* Get the value of the symbol referred to by the reloc. */
2411 if (ELF64_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2413 /* A local symbol. */
2414 Elf64_External_Sym
*esym
;
2415 Elf_External_Sym_Shndx
*shndx
;
2416 Elf_Internal_Sym isym
;
2419 esym
= extsyms
+ ELF64_R_SYM (irel
->r_info
);
2420 shndx
= shndx_buf
+ (shndx_buf
2421 ? ELF64_R_SYM (irel
->r_info
) : 0);
2422 bfd_elf64_swap_symbol_in (abfd
, (const PTR
) esym
, (const PTR
) shndx
,
2425 if (isym
.st_shndx
== SHN_UNDEF
)
2426 sym_sec
= bfd_und_section_ptr
;
2427 else if (isym
.st_shndx
== SHN_ABS
)
2428 sym_sec
= bfd_abs_section_ptr
;
2429 else if (isym
.st_shndx
== SHN_COMMON
)
2430 sym_sec
= bfd_com_section_ptr
;
2432 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
2433 symval
= (isym
.st_value
2434 + sym_sec
->output_section
->vma
2435 + sym_sec
->output_offset
);
2440 struct elf_link_hash_entry
*h
;
2442 /* An external symbol. */
2443 indx
= ELF64_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
2444 h
= elf_sym_hashes (abfd
)[indx
];
2445 BFD_ASSERT (h
!= NULL
);
2446 if (h
->root
.type
!= bfd_link_hash_defined
2447 && h
->root
.type
!= bfd_link_hash_defweak
)
2449 /* This appears to be a reference to an undefined symbol.
2450 Just ignore it--it will be caught by the regular reloc
2451 processing. We need to keep BPO reloc accounting
2452 consistent, though. */
2453 gregdata
->n_remaining_bpo_relocs_this_relaxation_round
--;
2458 symval
= (h
->root
.u
.def
.value
2459 + h
->root
.u
.def
.section
->output_section
->vma
2460 + h
->root
.u
.def
.section
->output_offset
);
2463 gregdata
->reloc_request
[gregdata
->bpo_reloc_indexes
[bpono
]].value
2464 = symval
+ irel
->r_addend
;
2465 gregdata
->reloc_request
[gregdata
->bpo_reloc_indexes
[bpono
++]].valid
= true;
2466 gregdata
->n_remaining_bpo_relocs_this_relaxation_round
--;
2469 /* Check if that was the last BPO-reloc. If so, sort the values and
2470 calculate how many registers we need to cover them. Set the size of
2471 the linker gregs, and if the number of registers changed, indicate
2472 that we need to relax some more because we have more work to do. */
2473 if (gregdata
->n_remaining_bpo_relocs_this_relaxation_round
== 0)
2479 /* First, reset the remaining relocs for the next round. */
2480 gregdata
->n_remaining_bpo_relocs_this_relaxation_round
2481 = gregdata
->n_bpo_relocs
;
2483 qsort ((PTR
) gregdata
->reloc_request
,
2484 gregdata
->n_max_bpo_relocs
,
2485 sizeof (struct bpo_reloc_request
),
2486 bpo_reloc_request_sort_fn
);
2488 /* Recalculate indexes. When we find a change (however unlikely
2489 after the initial iteration), we know we need to relax again,
2490 since items in the GREG-array are sorted by increasing value and
2491 stored in the relaxation phase. */
2492 for (i
= 0; i
< gregdata
->n_max_bpo_relocs
; i
++)
2493 if (gregdata
->bpo_reloc_indexes
[gregdata
->reloc_request
[i
].bpo_reloc_no
]
2496 gregdata
->bpo_reloc_indexes
[gregdata
->reloc_request
[i
].bpo_reloc_no
]
2501 /* Allocate register numbers (indexing from 0). Stop at the first
2503 for (i
= 0, regindex
= 0, prev_base
= gregdata
->reloc_request
[0].value
;
2504 i
< gregdata
->n_bpo_relocs
;
2507 if (gregdata
->reloc_request
[i
].value
> prev_base
+ 255)
2510 prev_base
= gregdata
->reloc_request
[i
].value
;
2512 gregdata
->reloc_request
[i
].regindex
= regindex
;
2513 gregdata
->reloc_request
[i
].offset
2514 = gregdata
->reloc_request
[i
].value
- prev_base
;
2517 /* If it's not the same as the last time, we need to relax again,
2518 because the size of the section has changed. I'm not sure we
2519 actually need to do any adjustments since the shrinking happens
2520 at the start of this section, but better safe than sorry. */
2521 if (gregdata
->n_allocated_bpo_gregs
!= regindex
+ 1)
2523 gregdata
->n_allocated_bpo_gregs
= regindex
+ 1;
2527 bpo_gregs_section
->_cooked_size
= (regindex
+ 1) * 8;
2530 if (free_relocs
!= NULL
)
2533 if (shndx_buf
!= NULL
)
2535 shndx_hdr
->contents
= NULL
;
2539 if (free_extsyms
!= NULL
)
2541 if (! link_info
->keep_memory
)
2543 symtab_hdr
->contents
= NULL
;
2544 free (free_extsyms
);
2551 if (free_relocs
!= NULL
)
2553 if (shndx_buf
!= NULL
)
2555 shndx_hdr
->contents
= NULL
;
2558 if (free_extsyms
!= NULL
)
2560 symtab_hdr
->contents
= NULL
;
2561 free (free_extsyms
);
2567 #define ELF_ARCH bfd_arch_mmix
2568 #define ELF_MACHINE_CODE EM_MMIX
2570 /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
2571 However, that's too much for something somewhere in the linker part of
2572 BFD; perhaps the start-address has to be a non-zero multiple of this
2573 number, or larger than this number. The symptom is that the linker
2574 complains: "warning: allocated section `.text' not in segment". We
2575 settle for 64k; the page-size used in examples is 8k.
2576 #define ELF_MAXPAGESIZE 0x10000
2578 Unfortunately, this causes excessive padding in the supposedly small
2579 for-education programs that are the expected usage (where people would
2580 inspect output). We stick to 256 bytes just to have *some* default
2582 #define ELF_MAXPAGESIZE 0x100
2584 #define TARGET_BIG_SYM bfd_elf64_mmix_vec
2585 #define TARGET_BIG_NAME "elf64-mmix"
2587 #define elf_info_to_howto_rel NULL
2588 #define elf_info_to_howto mmix_info_to_howto_rela
2589 #define elf_backend_relocate_section mmix_elf_relocate_section
2590 #define elf_backend_gc_mark_hook mmix_elf_gc_mark_hook
2591 #define elf_backend_gc_sweep_hook mmix_elf_gc_sweep_hook
2593 #define elf_backend_link_output_symbol_hook \
2594 mmix_elf_link_output_symbol_hook
2595 #define elf_backend_add_symbol_hook mmix_elf_add_symbol_hook
2597 #define elf_backend_check_relocs mmix_elf_check_relocs
2598 #define elf_backend_symbol_processing mmix_elf_symbol_processing
2600 #define bfd_elf64_bfd_is_local_label_name \
2601 mmix_elf_is_local_label_name
2603 #define elf_backend_may_use_rel_p 0
2604 #define elf_backend_may_use_rela_p 1
2605 #define elf_backend_default_use_rela_p 1
2607 #define elf_backend_can_gc_sections 1
2608 #define elf_backend_section_from_bfd_section \
2609 mmix_elf_section_from_bfd_section
2611 #define bfd_elf64_bfd_final_link mmix_elf_final_link
2612 #define bfd_elf64_bfd_relax_section mmix_elf_relax_section
2614 #include "elf64-target.h"