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 /* Watch out: this currently needs to have elements with the same index as
186 their R_MMIX_ number. */
187 static reloc_howto_type elf_mmix_howto_table
[] =
189 /* This reloc does nothing. */
190 HOWTO (R_MMIX_NONE
, /* type */
192 2, /* size (0 = byte, 1 = short, 2 = long) */
194 false, /* pc_relative */
196 complain_overflow_bitfield
, /* complain_on_overflow */
197 bfd_elf_generic_reloc
, /* special_function */
198 "R_MMIX_NONE", /* name */
199 false, /* partial_inplace */
202 false), /* pcrel_offset */
204 /* An 8 bit absolute relocation. */
205 HOWTO (R_MMIX_8
, /* type */
207 0, /* size (0 = byte, 1 = short, 2 = long) */
209 false, /* pc_relative */
211 complain_overflow_bitfield
, /* complain_on_overflow */
212 bfd_elf_generic_reloc
, /* special_function */
213 "R_MMIX_8", /* name */
214 false, /* partial_inplace */
217 false), /* pcrel_offset */
219 /* An 16 bit absolute relocation. */
220 HOWTO (R_MMIX_16
, /* type */
222 1, /* size (0 = byte, 1 = short, 2 = long) */
224 false, /* pc_relative */
226 complain_overflow_bitfield
, /* complain_on_overflow */
227 bfd_elf_generic_reloc
, /* special_function */
228 "R_MMIX_16", /* name */
229 false, /* partial_inplace */
231 0xffff, /* dst_mask */
232 false), /* pcrel_offset */
234 /* An 24 bit absolute relocation. */
235 HOWTO (R_MMIX_24
, /* type */
237 2, /* size (0 = byte, 1 = short, 2 = long) */
239 false, /* pc_relative */
241 complain_overflow_bitfield
, /* complain_on_overflow */
242 bfd_elf_generic_reloc
, /* special_function */
243 "R_MMIX_24", /* name */
244 false, /* partial_inplace */
245 ~0xffffff, /* src_mask */
246 0xffffff, /* dst_mask */
247 false), /* pcrel_offset */
249 /* A 32 bit absolute relocation. */
250 HOWTO (R_MMIX_32
, /* type */
252 2, /* size (0 = byte, 1 = short, 2 = long) */
254 false, /* pc_relative */
256 complain_overflow_bitfield
, /* complain_on_overflow */
257 bfd_elf_generic_reloc
, /* special_function */
258 "R_MMIX_32", /* name */
259 false, /* partial_inplace */
261 0xffffffff, /* dst_mask */
262 false), /* pcrel_offset */
264 /* 64 bit relocation. */
265 HOWTO (R_MMIX_64
, /* type */
267 4, /* size (0 = byte, 1 = short, 2 = long) */
269 false, /* pc_relative */
271 complain_overflow_bitfield
, /* complain_on_overflow */
272 bfd_elf_generic_reloc
, /* special_function */
273 "R_MMIX_64", /* name */
274 false, /* partial_inplace */
276 MINUS_ONE
, /* dst_mask */
277 false), /* pcrel_offset */
279 /* An 8 bit PC-relative relocation. */
280 HOWTO (R_MMIX_PC_8
, /* type */
282 0, /* size (0 = byte, 1 = short, 2 = long) */
284 true, /* pc_relative */
286 complain_overflow_bitfield
, /* complain_on_overflow */
287 bfd_elf_generic_reloc
, /* special_function */
288 "R_MMIX_PC_8", /* name */
289 false, /* partial_inplace */
292 true), /* pcrel_offset */
294 /* An 16 bit PC-relative relocation. */
295 HOWTO (R_MMIX_PC_16
, /* type */
297 1, /* size (0 = byte, 1 = short, 2 = long) */
299 true, /* pc_relative */
301 complain_overflow_bitfield
, /* complain_on_overflow */
302 bfd_elf_generic_reloc
, /* special_function */
303 "R_MMIX_PC_16", /* name */
304 false, /* partial_inplace */
306 0xffff, /* dst_mask */
307 true), /* pcrel_offset */
309 /* An 24 bit PC-relative relocation. */
310 HOWTO (R_MMIX_PC_24
, /* type */
312 2, /* size (0 = byte, 1 = short, 2 = long) */
314 true, /* pc_relative */
316 complain_overflow_bitfield
, /* complain_on_overflow */
317 bfd_elf_generic_reloc
, /* special_function */
318 "R_MMIX_PC_24", /* name */
319 false, /* partial_inplace */
320 ~0xffffff, /* src_mask */
321 0xffffff, /* dst_mask */
322 true), /* pcrel_offset */
324 /* A 32 bit absolute PC-relative relocation. */
325 HOWTO (R_MMIX_PC_32
, /* type */
327 2, /* size (0 = byte, 1 = short, 2 = long) */
329 true, /* pc_relative */
331 complain_overflow_bitfield
, /* complain_on_overflow */
332 bfd_elf_generic_reloc
, /* special_function */
333 "R_MMIX_PC_32", /* name */
334 false, /* partial_inplace */
336 0xffffffff, /* dst_mask */
337 true), /* pcrel_offset */
339 /* 64 bit PC-relative relocation. */
340 HOWTO (R_MMIX_PC_64
, /* type */
342 4, /* size (0 = byte, 1 = short, 2 = long) */
344 true, /* pc_relative */
346 complain_overflow_bitfield
, /* complain_on_overflow */
347 bfd_elf_generic_reloc
, /* special_function */
348 "R_MMIX_PC_64", /* name */
349 false, /* partial_inplace */
351 MINUS_ONE
, /* dst_mask */
352 true), /* pcrel_offset */
354 /* GNU extension to record C++ vtable hierarchy. */
355 HOWTO (R_MMIX_GNU_VTINHERIT
, /* type */
357 0, /* size (0 = byte, 1 = short, 2 = long) */
359 false, /* pc_relative */
361 complain_overflow_dont
, /* complain_on_overflow */
362 NULL
, /* special_function */
363 "R_MMIX_GNU_VTINHERIT", /* name */
364 false, /* partial_inplace */
367 true), /* pcrel_offset */
369 /* GNU extension to record C++ vtable member usage. */
370 HOWTO (R_MMIX_GNU_VTENTRY
, /* type */
372 0, /* size (0 = byte, 1 = short, 2 = long) */
374 false, /* pc_relative */
376 complain_overflow_dont
, /* complain_on_overflow */
377 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
378 "R_MMIX_GNU_VTENTRY", /* name */
379 false, /* partial_inplace */
382 false), /* pcrel_offset */
384 /* The GETA relocation is supposed to get any address that could
385 possibly be reached by the GETA instruction. It can silently expand
386 to get a 64-bit operand, but will complain if any of the two least
387 significant bits are set. The howto members reflect a simple GETA. */
388 HOWTO (R_MMIX_GETA
, /* type */
390 2, /* size (0 = byte, 1 = short, 2 = long) */
392 true, /* pc_relative */
394 complain_overflow_signed
, /* complain_on_overflow */
395 mmix_elf_reloc
, /* special_function */
396 "R_MMIX_GETA", /* name */
397 false, /* partial_inplace */
398 ~0x0100ffff, /* src_mask */
399 0x0100ffff, /* dst_mask */
400 true), /* pcrel_offset */
402 HOWTO (R_MMIX_GETA_1
, /* type */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
406 true, /* pc_relative */
408 complain_overflow_signed
, /* complain_on_overflow */
409 mmix_elf_reloc
, /* special_function */
410 "R_MMIX_GETA_1", /* name */
411 false, /* partial_inplace */
412 ~0x0100ffff, /* src_mask */
413 0x0100ffff, /* dst_mask */
414 true), /* pcrel_offset */
416 HOWTO (R_MMIX_GETA_2
, /* type */
418 2, /* size (0 = byte, 1 = short, 2 = long) */
420 true, /* pc_relative */
422 complain_overflow_signed
, /* complain_on_overflow */
423 mmix_elf_reloc
, /* special_function */
424 "R_MMIX_GETA_2", /* name */
425 false, /* partial_inplace */
426 ~0x0100ffff, /* src_mask */
427 0x0100ffff, /* dst_mask */
428 true), /* pcrel_offset */
430 HOWTO (R_MMIX_GETA_3
, /* type */
432 2, /* size (0 = byte, 1 = short, 2 = long) */
434 true, /* pc_relative */
436 complain_overflow_signed
, /* complain_on_overflow */
437 mmix_elf_reloc
, /* special_function */
438 "R_MMIX_GETA_3", /* name */
439 false, /* partial_inplace */
440 ~0x0100ffff, /* src_mask */
441 0x0100ffff, /* dst_mask */
442 true), /* pcrel_offset */
444 /* The conditional branches are supposed to reach any (code) address.
445 It can silently expand to a 64-bit operand, but will emit an error if
446 any of the two least significant bits are set. The howto members
447 reflect a simple branch. */
448 HOWTO (R_MMIX_CBRANCH
, /* type */
450 2, /* size (0 = byte, 1 = short, 2 = long) */
452 true, /* pc_relative */
454 complain_overflow_signed
, /* complain_on_overflow */
455 mmix_elf_reloc
, /* special_function */
456 "R_MMIX_CBRANCH", /* name */
457 false, /* partial_inplace */
458 ~0x0100ffff, /* src_mask */
459 0x0100ffff, /* dst_mask */
460 true), /* pcrel_offset */
462 HOWTO (R_MMIX_CBRANCH_J
, /* type */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
466 true, /* pc_relative */
468 complain_overflow_signed
, /* complain_on_overflow */
469 mmix_elf_reloc
, /* special_function */
470 "R_MMIX_CBRANCH_J", /* name */
471 false, /* partial_inplace */
472 ~0x0100ffff, /* src_mask */
473 0x0100ffff, /* dst_mask */
474 true), /* pcrel_offset */
476 HOWTO (R_MMIX_CBRANCH_1
, /* type */
478 2, /* size (0 = byte, 1 = short, 2 = long) */
480 true, /* pc_relative */
482 complain_overflow_signed
, /* complain_on_overflow */
483 mmix_elf_reloc
, /* special_function */
484 "R_MMIX_CBRANCH_1", /* name */
485 false, /* partial_inplace */
486 ~0x0100ffff, /* src_mask */
487 0x0100ffff, /* dst_mask */
488 true), /* pcrel_offset */
490 HOWTO (R_MMIX_CBRANCH_2
, /* type */
492 2, /* size (0 = byte, 1 = short, 2 = long) */
494 true, /* pc_relative */
496 complain_overflow_signed
, /* complain_on_overflow */
497 mmix_elf_reloc
, /* special_function */
498 "R_MMIX_CBRANCH_2", /* name */
499 false, /* partial_inplace */
500 ~0x0100ffff, /* src_mask */
501 0x0100ffff, /* dst_mask */
502 true), /* pcrel_offset */
504 HOWTO (R_MMIX_CBRANCH_3
, /* type */
506 2, /* size (0 = byte, 1 = short, 2 = long) */
508 true, /* pc_relative */
510 complain_overflow_signed
, /* complain_on_overflow */
511 mmix_elf_reloc
, /* special_function */
512 "R_MMIX_CBRANCH_3", /* name */
513 false, /* partial_inplace */
514 ~0x0100ffff, /* src_mask */
515 0x0100ffff, /* dst_mask */
516 true), /* pcrel_offset */
518 /* The PUSHJ instruction can reach any (code) address, as long as it's
519 the beginning of a function (no usable restriction). It can silently
520 expand to a 64-bit operand, but will emit an error if any of the two
521 least significant bits are set. The howto members reflect a simple
523 HOWTO (R_MMIX_PUSHJ
, /* type */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
527 true, /* pc_relative */
529 complain_overflow_signed
, /* complain_on_overflow */
530 mmix_elf_reloc
, /* special_function */
531 "R_MMIX_PUSHJ", /* name */
532 false, /* partial_inplace */
533 ~0x0100ffff, /* src_mask */
534 0x0100ffff, /* dst_mask */
535 true), /* pcrel_offset */
537 HOWTO (R_MMIX_PUSHJ_1
, /* type */
539 2, /* size (0 = byte, 1 = short, 2 = long) */
541 true, /* pc_relative */
543 complain_overflow_signed
, /* complain_on_overflow */
544 mmix_elf_reloc
, /* special_function */
545 "R_MMIX_PUSHJ_1", /* name */
546 false, /* partial_inplace */
547 ~0x0100ffff, /* src_mask */
548 0x0100ffff, /* dst_mask */
549 true), /* pcrel_offset */
551 HOWTO (R_MMIX_PUSHJ_2
, /* type */
553 2, /* size (0 = byte, 1 = short, 2 = long) */
555 true, /* pc_relative */
557 complain_overflow_signed
, /* complain_on_overflow */
558 mmix_elf_reloc
, /* special_function */
559 "R_MMIX_PUSHJ_2", /* name */
560 false, /* partial_inplace */
561 ~0x0100ffff, /* src_mask */
562 0x0100ffff, /* dst_mask */
563 true), /* pcrel_offset */
565 HOWTO (R_MMIX_PUSHJ_3
, /* type */
567 2, /* size (0 = byte, 1 = short, 2 = long) */
569 true, /* pc_relative */
571 complain_overflow_signed
, /* complain_on_overflow */
572 mmix_elf_reloc
, /* special_function */
573 "R_MMIX_PUSHJ_3", /* name */
574 false, /* partial_inplace */
575 ~0x0100ffff, /* src_mask */
576 0x0100ffff, /* dst_mask */
577 true), /* pcrel_offset */
579 /* A JMP is supposed to reach any (code) address. By itself, it can
580 reach +-64M; the expansion can reach all 64 bits. Note that the 64M
581 limit is soon reached if you link the program in wildly different
582 memory segments. The howto members reflect a trivial JMP. */
583 HOWTO (R_MMIX_JMP
, /* type */
585 2, /* size (0 = byte, 1 = short, 2 = long) */
587 true, /* pc_relative */
589 complain_overflow_signed
, /* complain_on_overflow */
590 mmix_elf_reloc
, /* special_function */
591 "R_MMIX_JMP", /* name */
592 false, /* partial_inplace */
593 ~0x1ffffff, /* src_mask */
594 0x1ffffff, /* dst_mask */
595 true), /* pcrel_offset */
597 HOWTO (R_MMIX_JMP_1
, /* type */
599 2, /* size (0 = byte, 1 = short, 2 = long) */
601 true, /* pc_relative */
603 complain_overflow_signed
, /* complain_on_overflow */
604 mmix_elf_reloc
, /* special_function */
605 "R_MMIX_JMP_1", /* name */
606 false, /* partial_inplace */
607 ~0x1ffffff, /* src_mask */
608 0x1ffffff, /* dst_mask */
609 true), /* pcrel_offset */
611 HOWTO (R_MMIX_JMP_2
, /* type */
613 2, /* size (0 = byte, 1 = short, 2 = long) */
615 true, /* pc_relative */
617 complain_overflow_signed
, /* complain_on_overflow */
618 mmix_elf_reloc
, /* special_function */
619 "R_MMIX_JMP_2", /* name */
620 false, /* partial_inplace */
621 ~0x1ffffff, /* src_mask */
622 0x1ffffff, /* dst_mask */
623 true), /* pcrel_offset */
625 HOWTO (R_MMIX_JMP_3
, /* type */
627 2, /* size (0 = byte, 1 = short, 2 = long) */
629 true, /* pc_relative */
631 complain_overflow_signed
, /* complain_on_overflow */
632 mmix_elf_reloc
, /* special_function */
633 "R_MMIX_JMP_3", /* name */
634 false, /* partial_inplace */
635 ~0x1ffffff, /* src_mask */
636 0x1ffffff, /* dst_mask */
637 true), /* pcrel_offset */
639 /* When we don't emit link-time-relaxable code from the assembler, or
640 when relaxation has done all it can do, these relocs are used. For
641 GETA/PUSHJ/branches. */
642 HOWTO (R_MMIX_ADDR19
, /* type */
644 2, /* size (0 = byte, 1 = short, 2 = long) */
646 true, /* pc_relative */
648 complain_overflow_signed
, /* complain_on_overflow */
649 mmix_elf_reloc
, /* special_function */
650 "R_MMIX_ADDR19", /* name */
651 false, /* partial_inplace */
652 ~0x0100ffff, /* src_mask */
653 0x0100ffff, /* dst_mask */
654 true), /* pcrel_offset */
657 HOWTO (R_MMIX_ADDR27
, /* type */
659 2, /* size (0 = byte, 1 = short, 2 = long) */
661 true, /* pc_relative */
663 complain_overflow_signed
, /* complain_on_overflow */
664 mmix_elf_reloc
, /* special_function */
665 "R_MMIX_ADDR27", /* name */
666 false, /* partial_inplace */
667 ~0x1ffffff, /* src_mask */
668 0x1ffffff, /* dst_mask */
669 true), /* pcrel_offset */
671 /* A general register or the value 0..255. If a value, then the
672 instruction (offset -3) needs adjusting. */
673 HOWTO (R_MMIX_REG_OR_BYTE
, /* type */
675 1, /* size (0 = byte, 1 = short, 2 = long) */
677 false, /* pc_relative */
679 complain_overflow_bitfield
, /* complain_on_overflow */
680 mmix_elf_reloc
, /* special_function */
681 "R_MMIX_REG_OR_BYTE", /* name */
682 false, /* partial_inplace */
685 false), /* pcrel_offset */
687 /* A general register. */
688 HOWTO (R_MMIX_REG
, /* type */
690 1, /* size (0 = byte, 1 = short, 2 = long) */
692 false, /* pc_relative */
694 complain_overflow_bitfield
, /* complain_on_overflow */
695 mmix_elf_reloc
, /* special_function */
696 "R_MMIX_REG", /* name */
697 false, /* partial_inplace */
700 false), /* pcrel_offset */
702 /* A register plus an index, corresponding to the relocation expression.
703 The sizes must correspond to the valid range of the expression, while
704 the bitmasks correspond to what we store in the image. */
705 HOWTO (R_MMIX_BASE_PLUS_OFFSET
, /* type */
707 4, /* size (0 = byte, 1 = short, 2 = long) */
709 false, /* pc_relative */
711 complain_overflow_bitfield
, /* complain_on_overflow */
712 mmix_elf_reloc
, /* special_function */
713 "R_MMIX_BASE_PLUS_OFFSET", /* name */
714 false, /* partial_inplace */
716 0xffff, /* dst_mask */
717 false), /* pcrel_offset */
719 /* A "magic" relocation for a LOCAL expression, asserting that the
720 expression is less than the number of global registers. No actual
721 modification of the contents is done. Implementing this as a
722 relocation was less intrusive than e.g. putting such expressions in a
723 section to discard *after* relocation. */
724 HOWTO (R_MMIX_LOCAL
, /* type */
726 0, /* size (0 = byte, 1 = short, 2 = long) */
728 false, /* pc_relative */
730 complain_overflow_dont
, /* complain_on_overflow */
731 mmix_elf_reloc
, /* special_function */
732 "R_MMIX_LOCAL", /* name */
733 false, /* partial_inplace */
736 false), /* pcrel_offset */
740 /* Map BFD reloc types to MMIX ELF reloc types. */
742 struct mmix_reloc_map
744 bfd_reloc_code_real_type bfd_reloc_val
;
745 enum elf_mmix_reloc_type elf_reloc_val
;
749 static const struct mmix_reloc_map mmix_reloc_map
[] =
751 {BFD_RELOC_NONE
, R_MMIX_NONE
},
752 {BFD_RELOC_8
, R_MMIX_8
},
753 {BFD_RELOC_16
, R_MMIX_16
},
754 {BFD_RELOC_24
, R_MMIX_24
},
755 {BFD_RELOC_32
, R_MMIX_32
},
756 {BFD_RELOC_64
, R_MMIX_64
},
757 {BFD_RELOC_8_PCREL
, R_MMIX_PC_8
},
758 {BFD_RELOC_16_PCREL
, R_MMIX_PC_16
},
759 {BFD_RELOC_24_PCREL
, R_MMIX_PC_24
},
760 {BFD_RELOC_32_PCREL
, R_MMIX_PC_32
},
761 {BFD_RELOC_64_PCREL
, R_MMIX_PC_64
},
762 {BFD_RELOC_VTABLE_INHERIT
, R_MMIX_GNU_VTINHERIT
},
763 {BFD_RELOC_VTABLE_ENTRY
, R_MMIX_GNU_VTENTRY
},
764 {BFD_RELOC_MMIX_GETA
, R_MMIX_GETA
},
765 {BFD_RELOC_MMIX_CBRANCH
, R_MMIX_CBRANCH
},
766 {BFD_RELOC_MMIX_PUSHJ
, R_MMIX_PUSHJ
},
767 {BFD_RELOC_MMIX_JMP
, R_MMIX_JMP
},
768 {BFD_RELOC_MMIX_ADDR19
, R_MMIX_ADDR19
},
769 {BFD_RELOC_MMIX_ADDR27
, R_MMIX_ADDR27
},
770 {BFD_RELOC_MMIX_REG_OR_BYTE
, R_MMIX_REG_OR_BYTE
},
771 {BFD_RELOC_MMIX_REG
, R_MMIX_REG
},
772 {BFD_RELOC_MMIX_BASE_PLUS_OFFSET
, R_MMIX_BASE_PLUS_OFFSET
},
773 {BFD_RELOC_MMIX_LOCAL
, R_MMIX_LOCAL
}
776 static reloc_howto_type
*
777 bfd_elf64_bfd_reloc_type_lookup (abfd
, code
)
778 bfd
*abfd ATTRIBUTE_UNUSED
;
779 bfd_reloc_code_real_type code
;
784 i
< sizeof (mmix_reloc_map
) / sizeof (mmix_reloc_map
[0]);
787 if (mmix_reloc_map
[i
].bfd_reloc_val
== code
)
788 return &elf_mmix_howto_table
[mmix_reloc_map
[i
].elf_reloc_val
];
795 /* This function performs the actual bitfiddling and sanity check for a
796 final relocation. Each relocation gets its *worst*-case expansion
797 in size when it arrives here; any reduction in size should have been
798 caught in linker relaxation earlier. When we get here, the relocation
799 looks like the smallest instruction with SWYM:s (nop:s) appended to the
800 max size. We fill in those nop:s.
802 R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
806 INCML $N,(foo >> 16) & 0xffff
807 INCMH $N,(foo >> 32) & 0xffff
808 INCH $N,(foo >> 48) & 0xffff
810 R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
811 condbranches needing relaxation might be rare enough to not be
822 R_MMIX_PUSHJ: (FIXME: Relaxation...)
831 R_MMIX_JMP: (FIXME: Relaxation...)
840 R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in. */
842 static bfd_reloc_status_type
843 mmix_elf_perform_relocation (isec
, howto
, datap
, addr
, value
)
845 reloc_howto_type
*howto
;
847 bfd_vma addr ATTRIBUTE_UNUSED
;
850 bfd
*abfd
= isec
->owner
;
851 bfd_reloc_status_type flag
= bfd_reloc_ok
;
852 bfd_reloc_status_type r
;
856 /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
857 We handle the differences here and the common sequence later. */
862 reg
= bfd_get_8 (abfd
, (bfd_byte
*) datap
+ 1);
864 /* We change to an absolute value. */
870 int in1
= bfd_get_16 (abfd
, (bfd_byte
*) datap
) << 16;
872 /* Invert the condition and prediction bit, and set the offset
873 to five instructions ahead.
875 We *can* do better if we want to. If the branch is found to be
876 within limits, we could leave the branch as is; there'll just
877 be a bunch of NOP:s after it. But we shouldn't see this
878 sequence often enough that it's worth doing it. */
881 (((in1
^ ((PRED_INV_BIT
| COND_INV_BIT
) << 24)) & ~0xffff)
885 /* Put a "GO $255,$255,0" after the common sequence. */
887 ((GO_INSN_BYTE
| IMM_OFFSET_BIT
) << 24) | 0xffff00,
888 (bfd_byte
*) datap
+ 20);
890 /* Common sequence starts at offset 4. */
893 /* We change to an absolute value. */
900 int inreg
= bfd_get_8 (abfd
, (bfd_byte
*) datap
+ 1);
902 /* Put a "PUSHGO $N,$255,0" after the common sequence. */
904 ((PUSHGO_INSN_BYTE
| IMM_OFFSET_BIT
) << 24)
907 (bfd_byte
*) datap
+ 16);
909 /* We change to an absolute value. */
915 /* This one is a little special. If we get here on a non-relaxing
916 link, and the destination is actually in range, we don't need to
918 If so, we fall through to the bit-fiddling relocs.
920 FIXME: bfd_check_overflow seems broken; the relocation is
921 rightshifted before testing, so supply a zero rightshift. */
923 if (! ((value
& 3) == 0
924 && (r
= bfd_check_overflow (complain_overflow_signed
,
927 bfd_arch_bits_per_address (abfd
),
928 value
)) == bfd_reloc_ok
))
930 /* If the relocation doesn't fit in a JMP, we let the NOP:s be
931 modified below, and put a "GO $255,$255,0" after the
932 address-loading sequence. */
934 ((GO_INSN_BYTE
| IMM_OFFSET_BIT
) << 24)
936 (bfd_byte
*) datap
+ 16);
938 /* We change to an absolute value. */
945 /* These must be in range, or else we emit an error. */
947 /* Note rightshift 0; see above. */
948 && (r
= bfd_check_overflow (complain_overflow_signed
,
951 bfd_arch_bits_per_address (abfd
),
952 value
)) == bfd_reloc_ok
)
955 = bfd_get_32 (abfd
, (bfd_byte
*) datap
);
958 if ((bfd_signed_vma
) value
< 0)
961 value
+= (1 << (howto
->bitsize
- 1));
969 (in1
& howto
->src_mask
)
971 | (value
& howto
->dst_mask
),
977 return bfd_reloc_overflow
;
979 case R_MMIX_BASE_PLUS_OFFSET
:
981 struct bpo_reloc_section_info
*bpodata
982 = (struct bpo_reloc_section_info
*)
983 elf_section_data (isec
)->tdata
;
984 asection
*bpo_greg_section
985 = bpodata
->bpo_greg_section
;
986 struct bpo_greg_section_info
*gregdata
987 = (struct bpo_greg_section_info
*)
988 elf_section_data (bpo_greg_section
)->tdata
;
990 = gregdata
->bpo_reloc_indexes
[bpodata
->bpo_index
++];
992 /* A consistency check: The value we now have in "relocation" must
993 be the same as the value we stored for that relocation. It
994 doesn't cost much, so can be left in at all times. */
995 if (value
!= gregdata
->reloc_request
[bpo_index
].value
)
997 (*_bfd_error_handler
)
998 (_("%s: Internal inconsistency error for value for\n\
999 linker-allocated global register: linked: 0x%lx%08lx != relaxed: 0x%lx%08lx\n"),
1000 bfd_get_filename (isec
->owner
),
1001 (unsigned long) (value
>> 32), (unsigned long) value
,
1002 (unsigned long) (gregdata
->reloc_request
[bpo_index
].value
1004 (unsigned long) gregdata
->reloc_request
[bpo_index
].value
);
1005 bfd_set_error (bfd_error_bad_value
);
1006 return bfd_reloc_overflow
;
1009 /* Then store the register number and offset for that register
1010 into datap and datap + 1 respectively. */
1012 gregdata
->reloc_request
[bpo_index
].regindex
1013 + bpo_greg_section
->output_section
->vma
/ 8,
1016 gregdata
->reloc_request
[bpo_index
].offset
,
1017 ((unsigned char *) datap
) + 1);
1018 return bfd_reloc_ok
;
1021 case R_MMIX_REG_OR_BYTE
:
1024 return bfd_reloc_overflow
;
1025 bfd_put_8 (abfd
, value
, datap
);
1026 return bfd_reloc_ok
;
1029 BAD_CASE (howto
->type
);
1032 /* This code adds the common SETL/INCML/INCMH/INCH worst-case
1035 /* Lowest two bits must be 0. We return bfd_reloc_overflow for
1036 everything that looks strange. */
1038 flag
= bfd_reloc_overflow
;
1041 (SETL_INSN_BYTE
<< 24) | (value
& 0xffff) | (reg
<< 16),
1042 (bfd_byte
*) datap
+ offs
);
1044 (INCML_INSN_BYTE
<< 24) | ((value
>> 16) & 0xffff) | (reg
<< 16),
1045 (bfd_byte
*) datap
+ offs
+ 4);
1047 (INCMH_INSN_BYTE
<< 24) | ((value
>> 32) & 0xffff) | (reg
<< 16),
1048 (bfd_byte
*) datap
+ offs
+ 8);
1050 (INCH_INSN_BYTE
<< 24) | ((value
>> 48) & 0xffff) | (reg
<< 16),
1051 (bfd_byte
*) datap
+ offs
+ 12);
1056 /* Set the howto pointer for an MMIX ELF reloc (type RELA). */
1059 mmix_info_to_howto_rela (abfd
, cache_ptr
, dst
)
1060 bfd
*abfd ATTRIBUTE_UNUSED
;
1062 Elf64_Internal_Rela
*dst
;
1064 unsigned int r_type
;
1066 r_type
= ELF64_R_TYPE (dst
->r_info
);
1067 BFD_ASSERT (r_type
< (unsigned int) R_MMIX_max
);
1068 cache_ptr
->howto
= &elf_mmix_howto_table
[r_type
];
1071 /* Any MMIX-specific relocation gets here at assembly time or when linking
1072 to other formats (such as mmo); this is the relocation function from
1073 the reloc_table. We don't get here for final pure ELF linking. */
1075 static bfd_reloc_status_type
1076 mmix_elf_reloc (abfd
, reloc_entry
, symbol
, data
, input_section
,
1077 output_bfd
, error_message
)
1079 arelent
*reloc_entry
;
1082 asection
*input_section
;
1084 char **error_message ATTRIBUTE_UNUSED
;
1087 bfd_reloc_status_type r
;
1088 asection
*reloc_target_output_section
;
1089 bfd_reloc_status_type flag
= bfd_reloc_ok
;
1090 bfd_vma output_base
= 0;
1093 r
= bfd_elf_generic_reloc (abfd
, reloc_entry
, symbol
, data
,
1094 input_section
, output_bfd
, error_message
);
1096 /* If that was all that was needed (i.e. this isn't a final link, only
1097 some segment adjustments), we're done. */
1098 if (r
!= bfd_reloc_continue
)
1101 if (bfd_is_und_section (symbol
->section
)
1102 && (symbol
->flags
& BSF_WEAK
) == 0
1103 && output_bfd
== (bfd
*) NULL
)
1104 return bfd_reloc_undefined
;
1106 /* Is the address of the relocation really within the section? */
1107 if (reloc_entry
->address
> input_section
->_cooked_size
)
1108 return bfd_reloc_outofrange
;
1110 /* Work out which section the relocation is targetted at and the
1111 initial relocation command value. */
1113 /* Get symbol value. (Common symbols are special.) */
1114 if (bfd_is_com_section (symbol
->section
))
1117 relocation
= symbol
->value
;
1119 reloc_target_output_section
= bfd_get_output_section (symbol
);
1121 /* Here the variable relocation holds the final address of the symbol we
1122 are relocating against, plus any addend. */
1126 output_base
= reloc_target_output_section
->vma
;
1128 relocation
+= output_base
+ symbol
->section
->output_offset
;
1130 /* Get position of relocation. */
1131 addr
= (reloc_entry
->address
+ input_section
->output_section
->vma
1132 + input_section
->output_offset
);
1133 if (output_bfd
!= (bfd
*) NULL
)
1135 /* Add in supplied addend. */
1136 relocation
+= reloc_entry
->addend
;
1138 /* This is a partial relocation, and we want to apply the
1139 relocation to the reloc entry rather than the raw data.
1140 Modify the reloc inplace to reflect what we now know. */
1141 reloc_entry
->addend
= relocation
;
1142 reloc_entry
->address
+= input_section
->output_offset
;
1146 return mmix_final_link_relocate (reloc_entry
->howto
, input_section
,
1147 data
, reloc_entry
->address
,
1148 reloc_entry
->addend
, relocation
,
1149 bfd_asymbol_name (symbol
),
1150 reloc_target_output_section
);
1153 /* Relocate an MMIX ELF section. Modified from elf32-fr30.c; look to it
1154 for guidance if you're thinking of copying this. */
1157 mmix_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1158 contents
, relocs
, local_syms
, local_sections
)
1159 bfd
*output_bfd ATTRIBUTE_UNUSED
;
1160 struct bfd_link_info
*info
;
1162 asection
*input_section
;
1164 Elf_Internal_Rela
*relocs
;
1165 Elf_Internal_Sym
*local_syms
;
1166 asection
**local_sections
;
1168 Elf_Internal_Shdr
*symtab_hdr
;
1169 struct elf_link_hash_entry
**sym_hashes
;
1170 Elf_Internal_Rela
*rel
;
1171 Elf_Internal_Rela
*relend
;
1173 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1174 sym_hashes
= elf_sym_hashes (input_bfd
);
1175 relend
= relocs
+ input_section
->reloc_count
;
1177 for (rel
= relocs
; rel
< relend
; rel
++)
1179 reloc_howto_type
*howto
;
1180 unsigned long r_symndx
;
1181 Elf_Internal_Sym
*sym
;
1183 struct elf_link_hash_entry
*h
;
1185 bfd_reloc_status_type r
;
1186 const char *name
= NULL
;
1188 boolean undefined_signalled
= false;
1190 r_type
= ELF64_R_TYPE (rel
->r_info
);
1192 if (r_type
== R_MMIX_GNU_VTINHERIT
1193 || r_type
== R_MMIX_GNU_VTENTRY
)
1196 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1198 if (info
->relocateable
)
1200 /* This is a relocateable link. We don't have to change
1201 anything, unless the reloc is against a section symbol,
1202 in which case we have to adjust according to where the
1203 section symbol winds up in the output section. */
1204 if (r_symndx
< symtab_hdr
->sh_info
)
1206 sym
= local_syms
+ r_symndx
;
1208 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1210 sec
= local_sections
[r_symndx
];
1211 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
1218 /* This is a final link. */
1219 howto
= elf_mmix_howto_table
+ ELF64_R_TYPE (rel
->r_info
);
1224 if (r_symndx
< symtab_hdr
->sh_info
)
1226 sym
= local_syms
+ r_symndx
;
1227 sec
= local_sections
[r_symndx
];
1228 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, sec
, rel
);
1230 name
= bfd_elf_string_from_elf_section
1231 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1232 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
1236 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1238 while (h
->root
.type
== bfd_link_hash_indirect
1239 || h
->root
.type
== bfd_link_hash_warning
)
1240 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1242 name
= h
->root
.root
.string
;
1244 if (h
->root
.type
== bfd_link_hash_defined
1245 || h
->root
.type
== bfd_link_hash_defweak
)
1247 sec
= h
->root
.u
.def
.section
;
1248 relocation
= (h
->root
.u
.def
.value
1249 + sec
->output_section
->vma
1250 + sec
->output_offset
);
1252 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1254 else if (info
->shared
1255 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
1259 /* The test on undefined_signalled is redundant at the
1260 moment, but kept for symmetry. */
1261 if (! undefined_signalled
1262 && ! ((*info
->callbacks
->undefined_symbol
)
1263 (info
, h
->root
.root
.string
, input_bfd
,
1264 input_section
, rel
->r_offset
, true)))
1266 undefined_signalled
= true;
1271 r
= mmix_final_link_relocate (howto
, input_section
,
1272 contents
, rel
->r_offset
,
1273 rel
->r_addend
, relocation
, name
, sec
);
1275 if (r
!= bfd_reloc_ok
)
1277 boolean check_ok
= true;
1278 const char * msg
= (const char *) NULL
;
1282 case bfd_reloc_overflow
:
1283 check_ok
= info
->callbacks
->reloc_overflow
1284 (info
, name
, howto
->name
, (bfd_vma
) 0,
1285 input_bfd
, input_section
, rel
->r_offset
);
1288 case bfd_reloc_undefined
:
1289 /* We may have sent this message above. */
1290 if (! undefined_signalled
)
1291 check_ok
= info
->callbacks
->undefined_symbol
1292 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
1294 undefined_signalled
= true;
1297 case bfd_reloc_outofrange
:
1298 msg
= _("internal error: out of range error");
1301 case bfd_reloc_notsupported
:
1302 msg
= _("internal error: unsupported relocation error");
1305 case bfd_reloc_dangerous
:
1306 msg
= _("internal error: dangerous relocation");
1310 msg
= _("internal error: unknown error");
1315 check_ok
= info
->callbacks
->warning
1316 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
1326 /* Perform a single relocation. By default we use the standard BFD
1327 routines. A few relocs we have to do ourselves. */
1329 static bfd_reloc_status_type
1330 mmix_final_link_relocate (howto
, input_section
, contents
,
1331 r_offset
, r_addend
, relocation
, symname
, symsec
)
1332 reloc_howto_type
*howto
;
1333 asection
*input_section
;
1336 bfd_signed_vma r_addend
;
1338 const char *symname
;
1341 bfd_reloc_status_type r
= bfd_reloc_ok
;
1343 = (input_section
->output_section
->vma
1344 + input_section
->output_offset
1347 = (bfd_signed_vma
) relocation
+ r_addend
;
1349 switch (howto
->type
)
1351 /* All these are PC-relative. */
1353 case R_MMIX_CBRANCH
:
1358 contents
+= r_offset
;
1360 srel
-= (input_section
->output_section
->vma
1361 + input_section
->output_offset
1364 r
= mmix_elf_perform_relocation (input_section
, howto
, contents
,
1368 case R_MMIX_BASE_PLUS_OFFSET
:
1370 return bfd_reloc_undefined
;
1372 /* Check that we're not relocating against a register symbol. */
1373 if (strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1374 MMIX_REG_CONTENTS_SECTION_NAME
) == 0
1375 || strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1376 MMIX_REG_SECTION_NAME
) == 0)
1378 /* Note: This is separated out into two messages in order
1379 to ease the translation into other languages. */
1380 if (symname
== NULL
|| *symname
== 0)
1381 (*_bfd_error_handler
)
1382 (_("%s: base-plus-offset relocation against register symbol: (unknown) in %s"),
1383 bfd_get_filename (input_section
->owner
),
1384 bfd_get_section_name (symsec
->owner
, symsec
));
1386 (*_bfd_error_handler
)
1387 (_("%s: base-plus-offset relocation against register symbol: %s in %s"),
1388 bfd_get_filename (input_section
->owner
), symname
,
1389 bfd_get_section_name (symsec
->owner
, symsec
));
1390 return bfd_reloc_overflow
;
1394 case R_MMIX_REG_OR_BYTE
:
1396 /* For now, we handle these alike. They must refer to an register
1397 symbol, which is either relative to the register section and in
1398 the range 0..255, or is in the register contents section with vma
1401 /* FIXME: A better way to check for reg contents section?
1402 FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1404 return bfd_reloc_undefined
;
1406 if (strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1407 MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
1409 if ((srel
& 7) != 0 || srel
< 32*8 || srel
> 255*8)
1411 /* The bfd_reloc_outofrange return value, though intuitively
1412 a better value, will not get us an error. */
1413 return bfd_reloc_overflow
;
1417 else if (strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1418 MMIX_REG_SECTION_NAME
) == 0)
1420 if (srel
< 0 || srel
> 255)
1421 /* The bfd_reloc_outofrange return value, though intuitively a
1422 better value, will not get us an error. */
1423 return bfd_reloc_overflow
;
1427 /* Note: This is separated out into two messages in order
1428 to ease the translation into other languages. */
1429 if (symname
== NULL
|| *symname
== 0)
1430 (*_bfd_error_handler
)
1431 (_("%s: register relocation against non-register symbol: (unknown) in %s"),
1432 bfd_get_filename (input_section
->owner
),
1433 bfd_get_section_name (symsec
->owner
, symsec
));
1435 (*_bfd_error_handler
)
1436 (_("%s: register relocation against non-register symbol: %s in %s"),
1437 bfd_get_filename (input_section
->owner
), symname
,
1438 bfd_get_section_name (symsec
->owner
, symsec
));
1440 /* The bfd_reloc_outofrange return value, though intuitively a
1441 better value, will not get us an error. */
1442 return bfd_reloc_overflow
;
1445 contents
+= r_offset
;
1446 r
= mmix_elf_perform_relocation (input_section
, howto
, contents
,
1451 /* This isn't a real relocation, it's just an assertion that the
1452 final relocation value corresponds to a local register. We
1453 ignore the actual relocation; nothing is changed. */
1456 = bfd_get_section_by_name (input_section
->output_section
->owner
,
1457 MMIX_REG_CONTENTS_SECTION_NAME
);
1458 bfd_vma first_global
;
1460 /* Check that this is an absolute value, or a reference to the
1461 register contents section or the register (symbol) section.
1462 Absolute numbers can get here as undefined section. Undefined
1463 symbols are signalled elsewhere, so there's no conflict in us
1464 accidentally handling it. */
1465 if (!bfd_is_abs_section (symsec
)
1466 && !bfd_is_und_section (symsec
)
1467 && strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1468 MMIX_REG_CONTENTS_SECTION_NAME
) != 0
1469 && strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1470 MMIX_REG_SECTION_NAME
) != 0)
1472 (*_bfd_error_handler
)
1473 (_("%s: directive LOCAL valid only with a register or absolute value"),
1474 bfd_get_filename (input_section
->owner
));
1476 return bfd_reloc_overflow
;
1479 /* If we don't have a register contents section, then $255 is the
1480 first global register. */
1485 first_global
= bfd_get_section_vma (abfd
, regsec
) / 8;
1486 if (strcmp (bfd_get_section_name (symsec
->owner
, symsec
),
1487 MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
1489 if ((srel
& 7) != 0 || srel
< 32*8 || srel
> 255*8)
1490 /* The bfd_reloc_outofrange return value, though
1491 intuitively a better value, will not get us an error. */
1492 return bfd_reloc_overflow
;
1497 if ((bfd_vma
) srel
>= first_global
)
1499 /* FIXME: Better error message. */
1500 (*_bfd_error_handler
)
1501 (_("%s: LOCAL directive: Register $%ld is not a local register. First global register is $%ld."),
1502 bfd_get_filename (input_section
->owner
), (long) srel
, (long) first_global
);
1504 return bfd_reloc_overflow
;
1511 r
= _bfd_final_link_relocate (howto
, input_section
->owner
, input_section
,
1513 relocation
, r_addend
);
1519 /* Return the section that should be marked against GC for a given
1523 mmix_elf_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
1525 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1526 Elf_Internal_Rela
*rel
;
1527 struct elf_link_hash_entry
*h
;
1528 Elf_Internal_Sym
*sym
;
1532 switch (ELF64_R_TYPE (rel
->r_info
))
1534 case R_MMIX_GNU_VTINHERIT
:
1535 case R_MMIX_GNU_VTENTRY
:
1539 switch (h
->root
.type
)
1541 case bfd_link_hash_defined
:
1542 case bfd_link_hash_defweak
:
1543 return h
->root
.u
.def
.section
;
1545 case bfd_link_hash_common
:
1546 return h
->root
.u
.c
.p
->section
;
1555 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
1561 /* Update relocation info for a GC-excluded section. We could supposedly
1562 perform the allocation after GC, but there's no suitable hook between
1563 GC (or section merge) and the point when all input sections must be
1564 present. Better to waste some memory and (perhaps) a little time. */
1567 mmix_elf_gc_sweep_hook (abfd
, info
, sec
, relocs
)
1568 bfd
*abfd ATTRIBUTE_UNUSED
;
1569 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1570 asection
*sec ATTRIBUTE_UNUSED
;
1571 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
;
1573 struct bpo_reloc_section_info
*bpodata
1574 = (struct bpo_reloc_section_info
*)
1575 elf_section_data (sec
)->tdata
;
1576 asection
*allocated_gregs_section
;
1578 /* If no bpodata here, we have nothing to do. */
1579 if (bpodata
== NULL
)
1582 allocated_gregs_section
= bpodata
->bpo_greg_section
;
1584 ((struct bpo_greg_section_info
*)
1585 elf_section_data (allocated_gregs_section
)->tdata
)
1587 -= bpodata
->n_bpo_relocs_this_section
;
1592 /* Sort register relocs to come before expanding relocs. */
1595 mmix_elf_sort_relocs (p1
, p2
)
1599 const Elf_Internal_Rela
*r1
= (const Elf_Internal_Rela
*) p1
;
1600 const Elf_Internal_Rela
*r2
= (const Elf_Internal_Rela
*) p2
;
1601 int r1_is_reg
, r2_is_reg
;
1603 /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1605 if ((r1
->r_offset
& ~(bfd_vma
) 3) > (r2
->r_offset
& ~(bfd_vma
) 3))
1607 else if ((r1
->r_offset
& ~(bfd_vma
) 3) < (r2
->r_offset
& ~(bfd_vma
) 3))
1611 = (ELF64_R_TYPE (r1
->r_info
) == R_MMIX_REG_OR_BYTE
1612 || ELF64_R_TYPE (r1
->r_info
) == R_MMIX_REG
);
1614 = (ELF64_R_TYPE (r2
->r_info
) == R_MMIX_REG_OR_BYTE
1615 || ELF64_R_TYPE (r2
->r_info
) == R_MMIX_REG
);
1616 if (r1_is_reg
!= r2_is_reg
)
1617 return r2_is_reg
- r1_is_reg
;
1619 /* Neither or both are register relocs. Then sort on full offset. */
1620 if (r1
->r_offset
> r2
->r_offset
)
1622 else if (r1
->r_offset
< r2
->r_offset
)
1627 /* Subset of mmix_elf_check_relocs, common to ELF and mmo linking. */
1630 mmix_elf_check_common_relocs (abfd
, info
, sec
, relocs
)
1632 struct bfd_link_info
*info
;
1634 const Elf_Internal_Rela
*relocs
;
1636 bfd
*bpo_greg_owner
= NULL
;
1637 asection
*allocated_gregs_section
= NULL
;
1638 struct bpo_greg_section_info
*gregdata
= NULL
;
1639 struct bpo_reloc_section_info
*bpodata
= NULL
;
1640 const Elf_Internal_Rela
*rel
;
1641 const Elf_Internal_Rela
*rel_end
;
1643 if (info
->relocateable
)
1646 /* We currently have to abuse this COFF-specific member, since there's
1647 no target-machine-dedicated member. There's no alternative outside
1648 the bfd_link_info struct; we can't specialize a hash-table since
1649 they're different between ELF and mmo. */
1650 bpo_greg_owner
= (bfd
*) info
->base_file
;
1652 rel_end
= relocs
+ sec
->reloc_count
;
1653 for (rel
= relocs
; rel
< rel_end
; rel
++)
1655 switch (ELF64_R_TYPE (rel
->r_info
))
1657 /* This relocation causes a GREG allocation. We need to count
1658 them, and we need to create a section for them, so we need an
1659 object to fake as the owner of that section. We can't use
1660 the ELF dynobj for this, since the ELF bits assume lots of
1661 DSO-related stuff if that member is non-NULL. */
1662 case R_MMIX_BASE_PLUS_OFFSET
:
1663 if (bpo_greg_owner
== NULL
)
1665 bpo_greg_owner
= abfd
;
1666 info
->base_file
= (PTR
) bpo_greg_owner
;
1669 allocated_gregs_section
1670 = bfd_get_section_by_name (bpo_greg_owner
,
1671 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
1672 if (allocated_gregs_section
== NULL
)
1674 allocated_gregs_section
1675 = bfd_make_section (bpo_greg_owner
,
1676 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
1677 /* Setting both SEC_ALLOC and SEC_LOAD means the section is
1678 treated like any other section, and we'd get errors for
1679 address overlap with the text section. Let's set none of
1680 those flags, as that is what currently happens for usual
1681 GREG allocations, and that works. */
1682 if (allocated_gregs_section
== NULL
1683 || !bfd_set_section_flags (bpo_greg_owner
,
1684 allocated_gregs_section
,
1687 | SEC_LINKER_CREATED
))
1688 || !bfd_set_section_alignment (bpo_greg_owner
,
1689 allocated_gregs_section
,
1693 gregdata
= (struct bpo_greg_section_info
*)
1694 bfd_zalloc (bpo_greg_owner
, sizeof (struct bpo_greg_section_info
));
1695 if (gregdata
== NULL
)
1697 elf_section_data (allocated_gregs_section
)->tdata
= gregdata
;
1699 else if (gregdata
== NULL
)
1700 gregdata
= elf_section_data (allocated_gregs_section
)->tdata
;
1702 /* Get ourselves some auxiliary info for the BPO-relocs. */
1703 if (bpodata
== NULL
)
1705 /* No use doing a separate iteration pass to find the upper
1706 limit - just use the number of relocs. */
1707 bpodata
= (struct bpo_reloc_section_info
*)
1708 bfd_alloc (bpo_greg_owner
,
1709 sizeof (struct bpo_reloc_section_info
)
1710 * (sec
->reloc_count
+ 1));
1711 if (bpodata
== NULL
)
1713 elf_section_data (sec
)->tdata
= bpodata
;
1714 bpodata
->first_base_plus_offset_reloc
1715 = bpodata
->bpo_index
1716 = gregdata
->n_max_bpo_relocs
;
1717 bpodata
->bpo_greg_section
1718 = allocated_gregs_section
;
1721 bpodata
->n_bpo_relocs_this_section
++;
1722 gregdata
->n_max_bpo_relocs
++;
1724 /* We don't get another chance to set this before GC; we've not
1725 set up set up any hook that runs before GC. */
1726 gregdata
->n_bpo_relocs
1727 = gregdata
->n_max_bpo_relocs
;
1735 /* Look through the relocs for a section during the first phase. */
1738 mmix_elf_check_relocs (abfd
, info
, sec
, relocs
)
1740 struct bfd_link_info
*info
;
1742 const Elf_Internal_Rela
*relocs
;
1744 Elf_Internal_Shdr
*symtab_hdr
;
1745 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
1746 const Elf_Internal_Rela
*rel
;
1747 const Elf_Internal_Rela
*rel_end
;
1749 if (info
->relocateable
)
1752 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1753 sym_hashes
= elf_sym_hashes (abfd
);
1754 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof(Elf64_External_Sym
);
1755 if (!elf_bad_symtab (abfd
))
1756 sym_hashes_end
-= symtab_hdr
->sh_info
;
1758 /* First we sort the relocs so that any register relocs come before
1759 expansion-relocs to the same insn. FIXME: Not done for mmo. */
1760 qsort ((PTR
) relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
1761 mmix_elf_sort_relocs
);
1763 /* Do the common part. */
1764 if (!mmix_elf_check_common_relocs (abfd
, info
, sec
, relocs
))
1767 rel_end
= relocs
+ sec
->reloc_count
;
1768 for (rel
= relocs
; rel
< rel_end
; rel
++)
1770 struct elf_link_hash_entry
*h
;
1771 unsigned long r_symndx
;
1773 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1774 if (r_symndx
< symtab_hdr
->sh_info
)
1777 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1779 switch (ELF64_R_TYPE (rel
->r_info
))
1781 /* This relocation describes the C++ object vtable hierarchy.
1782 Reconstruct it for later use during GC. */
1783 case R_MMIX_GNU_VTINHERIT
:
1784 if (!_bfd_elf64_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
1788 /* This relocation describes which C++ vtable entries are actually
1789 used. Record for later use during GC. */
1790 case R_MMIX_GNU_VTENTRY
:
1791 if (!_bfd_elf64_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
1800 /* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
1801 Copied from elf_link_add_object_symbols. */
1804 _bfd_mmix_check_all_relocs (abfd
, info
)
1806 struct bfd_link_info
*info
;
1810 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1812 Elf_Internal_Rela
*internal_relocs
;
1815 if ((o
->flags
& SEC_RELOC
) == 0
1816 || o
->reloc_count
== 0
1817 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
1818 && (o
->flags
& SEC_DEBUGGING
) != 0)
1819 || bfd_is_abs_section (o
->output_section
))
1823 = _bfd_elf64_link_read_relocs (abfd
, o
, (PTR
) NULL
,
1824 (Elf_Internal_Rela
*) NULL
,
1826 if (internal_relocs
== NULL
)
1829 ok
= mmix_elf_check_common_relocs (abfd
, info
, o
, internal_relocs
);
1831 if (! info
->keep_memory
)
1832 free (internal_relocs
);
1841 /* Change symbols relative to the reg contents section to instead be to
1842 the register section, and scale them down to correspond to the register
1846 mmix_elf_link_output_symbol_hook (abfd
, info
, name
, sym
, input_sec
)
1847 bfd
*abfd ATTRIBUTE_UNUSED
;
1848 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1849 const char *name ATTRIBUTE_UNUSED
;
1850 Elf_Internal_Sym
*sym
;
1851 asection
*input_sec
;
1853 if (input_sec
!= NULL
1854 && input_sec
->name
!= NULL
1855 && ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
1856 && strcmp (input_sec
->name
, MMIX_REG_CONTENTS_SECTION_NAME
) == 0)
1859 sym
->st_shndx
= SHN_REGISTER
;
1865 /* We fake a register section that holds values that are register numbers.
1866 Having a SHN_REGISTER and register section translates better to other
1867 formats (e.g. mmo) than for example a STT_REGISTER attribute.
1868 This section faking is based on a construct in elf32-mips.c. */
1869 static asection mmix_elf_reg_section
;
1870 static asymbol mmix_elf_reg_section_symbol
;
1871 static asymbol
*mmix_elf_reg_section_symbol_ptr
;
1873 /* Handle the special MIPS section numbers that a symbol may use.
1874 This is used for both the 32-bit and the 64-bit ABI. */
1877 mmix_elf_symbol_processing (abfd
, asym
)
1878 bfd
*abfd ATTRIBUTE_UNUSED
;
1881 elf_symbol_type
*elfsym
;
1883 elfsym
= (elf_symbol_type
*) asym
;
1884 switch (elfsym
->internal_elf_sym
.st_shndx
)
1887 if (mmix_elf_reg_section
.name
== NULL
)
1889 /* Initialize the register section. */
1890 mmix_elf_reg_section
.name
= MMIX_REG_SECTION_NAME
;
1891 mmix_elf_reg_section
.flags
= SEC_NO_FLAGS
;
1892 mmix_elf_reg_section
.output_section
= &mmix_elf_reg_section
;
1893 mmix_elf_reg_section
.symbol
= &mmix_elf_reg_section_symbol
;
1894 mmix_elf_reg_section
.symbol_ptr_ptr
= &mmix_elf_reg_section_symbol_ptr
;
1895 mmix_elf_reg_section_symbol
.name
= MMIX_REG_SECTION_NAME
;
1896 mmix_elf_reg_section_symbol
.flags
= BSF_SECTION_SYM
;
1897 mmix_elf_reg_section_symbol
.section
= &mmix_elf_reg_section
;
1898 mmix_elf_reg_section_symbol_ptr
= &mmix_elf_reg_section_symbol
;
1900 asym
->section
= &mmix_elf_reg_section
;
1908 /* Given a BFD section, try to locate the corresponding ELF section
1912 mmix_elf_section_from_bfd_section (abfd
, sec
, retval
)
1913 bfd
* abfd ATTRIBUTE_UNUSED
;
1917 if (strcmp (bfd_get_section_name (abfd
, sec
), MMIX_REG_SECTION_NAME
) == 0)
1918 *retval
= SHN_REGISTER
;
1925 /* Hook called by the linker routine which adds symbols from an object
1926 file. We must handle the special SHN_REGISTER section number here.
1928 We also check that we only have *one* each of the section-start
1929 symbols, since otherwise having two with the same value would cause
1930 them to be "merged", but with the contents serialized. */
1933 mmix_elf_add_symbol_hook (abfd
, info
, sym
, namep
, flagsp
, secp
, valp
)
1935 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1936 const Elf_Internal_Sym
*sym
;
1937 const char **namep ATTRIBUTE_UNUSED
;
1938 flagword
*flagsp ATTRIBUTE_UNUSED
;
1940 bfd_vma
*valp ATTRIBUTE_UNUSED
;
1942 if (sym
->st_shndx
== SHN_REGISTER
)
1943 *secp
= bfd_make_section_old_way (abfd
, MMIX_REG_SECTION_NAME
);
1944 else if ((*namep
)[0] == '_' && (*namep
)[1] == '_' && (*namep
)[2] == '.'
1945 && strncmp (*namep
, MMIX_LOC_SECTION_START_SYMBOL_PREFIX
,
1946 strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX
)) == 0)
1948 /* See if we have another one. */
1949 struct elf_link_hash_entry
*h
1950 = (struct elf_link_hash_entry
*) bfd_link_hash_lookup (info
->hash
,
1955 if (h
!= NULL
&& h
->root
.type
!= bfd_link_hash_undefined
)
1957 /* How do we get the asymbol (or really: the filename) from h?
1958 h->root.u.def.section->owner is NULL. */
1959 ((*_bfd_error_handler
)
1960 (_("%s: Error: multiple definition of `%s'; start of %s is set in a earlier linked file\n"),
1961 bfd_get_filename (abfd
), *namep
,
1962 *namep
+ strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX
)));
1963 bfd_set_error (bfd_error_bad_value
);
1971 /* We consider symbols matching "L.*:[0-9]+" to be local symbols. */
1974 mmix_elf_is_local_label_name (abfd
, name
)
1981 /* Also include the default local-label definition. */
1982 if (_bfd_elf_is_local_label_name (abfd
, name
))
1988 /* If there's no ":", or more than one, it's not a local symbol. */
1989 colpos
= strchr (name
, ':');
1990 if (colpos
== NULL
|| strchr (colpos
+ 1, ':') != NULL
)
1993 /* Check that there are remaining characters and that they are digits. */
1997 digits
= strspn (colpos
+ 1, "0123456789");
1998 return digits
!= 0 && colpos
[1 + digits
] == 0;
2001 /* We get rid of the register section here. */
2004 mmix_elf_final_link (abfd
, info
)
2006 struct bfd_link_info
*info
;
2008 /* We never output a register section, though we create one for
2009 temporary measures. Check that nobody entered contents into it. */
2010 asection
*reg_section
;
2013 reg_section
= bfd_get_section_by_name (abfd
, MMIX_REG_SECTION_NAME
);
2015 if (reg_section
!= NULL
)
2017 /* FIXME: Pass error state gracefully. */
2018 if (bfd_get_section_flags (abfd
, reg_section
) & SEC_HAS_CONTENTS
)
2019 _bfd_abort (__FILE__
, __LINE__
, _("Register section has contents\n"));
2021 /* Really remove the section. */
2022 for (secpp
= &abfd
->sections
;
2023 *secpp
!= reg_section
;
2024 secpp
= &(*secpp
)->next
)
2026 bfd_section_list_remove (abfd
, secpp
);
2027 --abfd
->section_count
;
2030 if (! bfd_elf64_bfd_final_link (abfd
, info
))
2033 /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
2034 the regular linker machinery. We do it here, like other targets with
2035 special sections. */
2036 if (info
->base_file
!= NULL
)
2038 asection
*greg_section
2039 = bfd_get_section_by_name ((bfd
*) info
->base_file
,
2040 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
2041 if (!bfd_set_section_contents (abfd
,
2042 greg_section
->output_section
,
2043 greg_section
->contents
,
2044 (file_ptr
) greg_section
->output_offset
,
2045 greg_section
->_cooked_size
))
2051 /* Initialize stuff for the linker-generated GREGs to match
2052 R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker. */
2055 _bfd_mmix_prepare_linker_allocated_gregs (abfd
, info
)
2056 bfd
*abfd ATTRIBUTE_UNUSED
;
2057 struct bfd_link_info
*info
;
2059 asection
*bpo_gregs_section
;
2060 bfd
*bpo_greg_owner
;
2061 struct bpo_greg_section_info
*gregdata
;
2065 size_t *bpo_reloc_indexes
;
2067 /* The bpo_greg_owner bfd is supposed to have been set by
2068 mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
2069 If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2070 bpo_greg_owner
= (bfd
*) info
->base_file
;
2071 if (bpo_greg_owner
== NULL
)
2075 = bfd_get_section_by_name (bpo_greg_owner
,
2076 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
2078 /* This can't happen without DSO handling. When DSOs are handled
2079 without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
2081 if (bpo_gregs_section
== NULL
)
2084 /* We use the target-data handle in the ELF section data. */
2085 gregdata
= (struct bpo_greg_section_info
*)
2086 elf_section_data (bpo_gregs_section
)->tdata
;
2087 if (gregdata
== NULL
)
2090 n_gregs
= gregdata
->n_bpo_relocs
;
2091 gregdata
->n_allocated_bpo_gregs
= n_gregs
;
2093 /* When this reaches zero during relaxation, all entries have been
2094 filled in and the size of the linker gregs can be calculated. */
2095 gregdata
->n_remaining_bpo_relocs_this_relaxation_round
= n_gregs
;
2097 /* Set the zeroth-order estimate for the GREGs size. */
2098 gregs_size
= n_gregs
* 8;
2100 if (!bfd_set_section_size (bpo_greg_owner
, bpo_gregs_section
, gregs_size
))
2103 /* Allocate and set up the GREG arrays. They're filled in at relaxation
2104 time. Note that we must use the max number ever noted for the array,
2105 since the index numbers were created before GC. */
2106 gregdata
->reloc_request
2107 = bfd_zalloc (bpo_greg_owner
,
2108 sizeof (struct bpo_reloc_request
)
2109 * gregdata
->n_max_bpo_relocs
);
2111 gregdata
->bpo_reloc_indexes
2113 = bfd_alloc (bpo_greg_owner
,
2114 gregdata
->n_max_bpo_relocs
2116 if (bpo_reloc_indexes
== NULL
)
2119 /* The default order is an identity mapping. */
2120 for (i
= 0; i
< gregdata
->n_max_bpo_relocs
; i
++)
2122 bpo_reloc_indexes
[i
] = i
;
2123 gregdata
->reloc_request
[i
].bpo_reloc_no
= i
;
2129 /* Fill in contents in the linker allocated gregs. Everything is
2130 calculated at this point; we just move the contents into place here. */
2133 _bfd_mmix_finalize_linker_allocated_gregs (abfd
, link_info
)
2134 bfd
*abfd ATTRIBUTE_UNUSED
;
2135 struct bfd_link_info
*link_info
;
2137 asection
*bpo_gregs_section
;
2138 bfd
*bpo_greg_owner
;
2139 struct bpo_greg_section_info
*gregdata
;
2145 /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
2146 when the first R_MMIX_BASE_PLUS_OFFSET is seen. If there is no such
2147 object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2148 bpo_greg_owner
= (bfd
*) link_info
->base_file
;
2149 if (bpo_greg_owner
== NULL
)
2153 = bfd_get_section_by_name (bpo_greg_owner
,
2154 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME
);
2156 /* This can't happen without DSO handling. When DSOs are handled
2157 without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
2159 if (bpo_gregs_section
== NULL
)
2162 /* We use the target-data handle in the ELF section data. */
2164 gregdata
= (struct bpo_greg_section_info
*)
2165 elf_section_data (bpo_gregs_section
)->tdata
;
2166 if (gregdata
== NULL
)
2169 n_gregs
= gregdata
->n_allocated_bpo_gregs
;
2171 bpo_gregs_section
->contents
2172 = contents
= bfd_alloc (bpo_greg_owner
, bpo_gregs_section
->_cooked_size
);
2173 if (contents
== NULL
)
2176 for (lastreg
= 255, i
= 0, j
= 0; j
< n_gregs
; i
++)
2177 if (gregdata
->reloc_request
[i
].regindex
!= lastreg
)
2179 bfd_put_64 (bpo_greg_owner
, gregdata
->reloc_request
[i
].value
,
2181 lastreg
= gregdata
->reloc_request
[i
].regindex
;
2188 /* Sort valid relocs to come before non-valid relocs, then on increasing
2192 bpo_reloc_request_sort_fn (p1
, p2
)
2196 const struct bpo_reloc_request
*r1
= (const struct bpo_reloc_request
*) p1
;
2197 const struct bpo_reloc_request
*r2
= (const struct bpo_reloc_request
*) p2
;
2199 /* Primary function is validity; non-valid relocs sorted after valid
2201 if (r1
->valid
!= r2
->valid
)
2202 return r2
->valid
- r1
->valid
;
2204 /* Then sort on value. */
2205 if (r1
->value
!= r2
->value
)
2206 return r1
->value
- r2
->value
;
2208 /* As a last re-sort, use the address so we get a stable sort. */
2209 return r1
> r2
? 1 : (r1
< r2
? -1 : 0);
2212 /* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
2213 when the last such reloc is done, an index-array is sorted according to
2214 the values and iterated over to produce register numbers (indexed by 0
2215 from the first allocated register number) and offsets for use in real
2218 Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
2221 mmix_elf_relax_section (abfd
, sec
, link_info
, again
)
2224 struct bfd_link_info
*link_info
;
2228 Elf_Internal_Shdr
*symtab_hdr
;
2229 Elf_Internal_Shdr
*shndx_hdr
;
2230 Elf_Internal_Rela
*internal_relocs
;
2231 Elf_Internal_Rela
*free_relocs
= NULL
;
2232 Elf_Internal_Rela
*irel
, *irelend
;
2233 asection
*bpo_gregs_section
= NULL
;
2234 struct bpo_greg_section_info
*gregdata
;
2235 struct bpo_reloc_section_info
*bpodata
2236 = (struct bpo_reloc_section_info
*)
2237 elf_section_data (sec
)->tdata
;
2239 bfd
*bpo_greg_owner
;
2240 Elf64_External_Sym
*extsyms
= NULL
;
2241 Elf64_External_Sym
*free_extsyms
= NULL
;
2242 Elf_External_Sym_Shndx
*shndx_buf
= NULL
;
2244 /* Assume nothing changes. */
2247 /* If this is the first time we have been called for this section,
2248 initialize the cooked size. */
2249 if (sec
->_cooked_size
== 0)
2250 sec
->_cooked_size
= sec
->_raw_size
;
2252 /* We don't have to do anything for a relocateable link, if
2253 this section does not have relocs, or if this is not a
2255 if (link_info
->relocateable
2256 || (sec
->flags
& SEC_RELOC
) == 0
2257 || sec
->reloc_count
== 0
2258 || (sec
->flags
& SEC_CODE
) == 0
2259 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2260 /* If no R_MMIX_BASE_PLUS_OFFSET relocs, then nothing to do. */
2264 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2265 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2267 bpo_greg_owner
= (bfd
*) link_info
->base_file
;
2268 bpo_gregs_section
= bpodata
->bpo_greg_section
;
2269 gregdata
= (struct bpo_greg_section_info
*)
2270 elf_section_data (bpo_gregs_section
)->tdata
;
2272 bpono
= bpodata
->first_base_plus_offset_reloc
;
2274 /* Get a copy of the native relocations. */
2276 = _bfd_elf64_link_read_relocs (abfd
, sec
, (PTR
) NULL
,
2277 (Elf_Internal_Rela
*) NULL
,
2278 link_info
->keep_memory
);
2279 if (internal_relocs
== NULL
)
2281 if (! link_info
->keep_memory
)
2282 free_relocs
= internal_relocs
;
2284 /* Walk through them looking for relaxing opportunities. */
2285 irelend
= internal_relocs
+ sec
->reloc_count
;
2286 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2290 if (ELF64_R_TYPE (irel
->r_info
) != (int) R_MMIX_BASE_PLUS_OFFSET
)
2293 /* Read this BFD's symbols if we haven't done so already. */
2294 if (extsyms
== NULL
)
2296 /* Get cached copy if it exists. */
2297 if (symtab_hdr
->contents
!= NULL
)
2298 extsyms
= (Elf64_External_Sym
*) symtab_hdr
->contents
;
2301 /* Go get them off disk. */
2304 amt
= symtab_hdr
->sh_info
;
2305 amt
*= sizeof (Elf64_External_Sym
);
2306 extsyms
= (Elf64_External_Sym
*) bfd_malloc (amt
);
2307 if (extsyms
== NULL
)
2309 free_extsyms
= extsyms
;
2310 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
2311 || bfd_bread ((PTR
) extsyms
, amt
, abfd
) != amt
)
2313 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2316 /* If >64k sections, this presumable happens. No test-case. */
2317 if (shndx_hdr
->sh_size
!= 0)
2321 amt
= symtab_hdr
->sh_info
;
2322 amt
*= sizeof (Elf_External_Sym_Shndx
);
2323 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
2324 if (shndx_buf
== NULL
)
2326 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
2327 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
2329 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
2333 /* Get the value of the symbol referred to by the reloc. */
2334 if (ELF64_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2336 /* A local symbol. */
2337 Elf64_External_Sym
*esym
;
2338 Elf_External_Sym_Shndx
*shndx
;
2339 Elf_Internal_Sym isym
;
2342 esym
= extsyms
+ ELF64_R_SYM (irel
->r_info
);
2343 shndx
= shndx_buf
+ (shndx_buf
2344 ? ELF64_R_SYM (irel
->r_info
) : 0);
2345 bfd_elf64_swap_symbol_in (abfd
, esym
, shndx
, &isym
);
2347 if (isym
.st_shndx
== SHN_UNDEF
)
2348 sym_sec
= bfd_und_section_ptr
;
2349 else if (isym
.st_shndx
== SHN_ABS
)
2350 sym_sec
= bfd_abs_section_ptr
;
2351 else if (isym
.st_shndx
== SHN_COMMON
)
2352 sym_sec
= bfd_com_section_ptr
;
2354 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
2355 symval
= (isym
.st_value
2356 + sym_sec
->output_section
->vma
2357 + sym_sec
->output_offset
);
2362 struct elf_link_hash_entry
*h
;
2364 /* An external symbol. */
2365 indx
= ELF64_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
2366 h
= elf_sym_hashes (abfd
)[indx
];
2367 BFD_ASSERT (h
!= NULL
);
2368 if (h
->root
.type
!= bfd_link_hash_defined
2369 && h
->root
.type
!= bfd_link_hash_defweak
)
2371 /* This appears to be a reference to an undefined
2372 symbol. Just ignore it--it will be caught by the
2373 regular reloc processing. */
2377 symval
= (h
->root
.u
.def
.value
2378 + h
->root
.u
.def
.section
->output_section
->vma
2379 + h
->root
.u
.def
.section
->output_offset
);
2382 gregdata
->reloc_request
[gregdata
->bpo_reloc_indexes
[bpono
]].value
2383 = symval
+ irel
->r_addend
;
2384 gregdata
->reloc_request
[gregdata
->bpo_reloc_indexes
[bpono
++]].valid
= true;
2385 gregdata
->n_remaining_bpo_relocs_this_relaxation_round
--;
2388 /* Check if that was the last BPO-reloc. If so, sort the values and
2389 calculate how many registers we need to cover them. Set the size of
2390 the linker gregs, and if the number of registers changed, indicate
2391 that we need to relax some more because we have more work to do. */
2392 if (gregdata
->n_remaining_bpo_relocs_this_relaxation_round
== 0)
2398 /* First, reset the remaining relocs for the next round. */
2399 gregdata
->n_remaining_bpo_relocs_this_relaxation_round
2400 = gregdata
->n_bpo_relocs
;
2402 qsort ((PTR
) gregdata
->reloc_request
,
2403 gregdata
->n_max_bpo_relocs
,
2404 sizeof (struct bpo_reloc_request
),
2405 bpo_reloc_request_sort_fn
);
2407 /* Recalculate indexes. When we find a change (however unlikely
2408 after the initial iteration), we know we need to relax again,
2409 since items in the GREG-array are sorted by increasing value and
2410 stored in the relaxation phase. */
2411 for (i
= 0; i
< gregdata
->n_max_bpo_relocs
; i
++)
2412 if (gregdata
->bpo_reloc_indexes
[gregdata
->reloc_request
[i
].bpo_reloc_no
]
2415 gregdata
->bpo_reloc_indexes
[gregdata
->reloc_request
[i
].bpo_reloc_no
]
2420 /* Allocate register numbers (indexing from 0). Stop at the first
2422 for (i
= 0, regindex
= 0, prev_base
= gregdata
->reloc_request
[0].value
;
2423 i
< gregdata
->n_bpo_relocs
;
2426 if (gregdata
->reloc_request
[i
].value
> prev_base
+ 255)
2429 prev_base
= gregdata
->reloc_request
[i
].value
;
2431 gregdata
->reloc_request
[i
].regindex
= regindex
;
2432 gregdata
->reloc_request
[i
].offset
2433 = gregdata
->reloc_request
[i
].value
- prev_base
;
2436 /* If it's not the same as the last time, we need to relax again,
2437 because the size of the section has changed. I'm not sure we
2438 actually need to do any adjustments since the shrinking happens
2439 at the start of this section, but better safe than sorry. */
2440 if (gregdata
->n_allocated_bpo_gregs
!= regindex
+ 1)
2442 gregdata
->n_allocated_bpo_gregs
= regindex
+ 1;
2446 bpo_gregs_section
->_cooked_size
= (regindex
+ 1) * 8;
2449 if (free_relocs
!= NULL
)
2452 if (shndx_buf
!= NULL
)
2454 shndx_hdr
->contents
= NULL
;
2458 if (free_extsyms
!= NULL
)
2460 if (! link_info
->keep_memory
)
2462 symtab_hdr
->contents
= NULL
;
2463 free (free_extsyms
);
2470 if (free_relocs
!= NULL
)
2472 if (shndx_buf
!= NULL
)
2474 shndx_hdr
->contents
= NULL
;
2477 if (free_extsyms
!= NULL
)
2479 symtab_hdr
->contents
= NULL
;
2480 free (free_extsyms
);
2486 #define ELF_ARCH bfd_arch_mmix
2487 #define ELF_MACHINE_CODE EM_MMIX
2489 /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
2490 However, that's too much for something somewhere in the linker part of
2491 BFD; perhaps the start-address has to be a non-zero multiple of this
2492 number, or larger than this number. The symptom is that the linker
2493 complains: "warning: allocated section `.text' not in segment". We
2494 settle for 64k; the page-size used in examples is 8k.
2495 #define ELF_MAXPAGESIZE 0x10000
2497 Unfortunately, this causes excessive padding in the supposedly small
2498 for-education programs that are the expected usage (where people would
2499 inspect output). We stick to 256 bytes just to have *some* default
2501 #define ELF_MAXPAGESIZE 0x100
2503 #define TARGET_BIG_SYM bfd_elf64_mmix_vec
2504 #define TARGET_BIG_NAME "elf64-mmix"
2506 #define elf_info_to_howto_rel NULL
2507 #define elf_info_to_howto mmix_info_to_howto_rela
2508 #define elf_backend_relocate_section mmix_elf_relocate_section
2509 #define elf_backend_gc_mark_hook mmix_elf_gc_mark_hook
2510 #define elf_backend_gc_sweep_hook mmix_elf_gc_sweep_hook
2512 #define elf_backend_link_output_symbol_hook \
2513 mmix_elf_link_output_symbol_hook
2514 #define elf_backend_add_symbol_hook mmix_elf_add_symbol_hook
2516 #define elf_backend_check_relocs mmix_elf_check_relocs
2517 #define elf_backend_symbol_processing mmix_elf_symbol_processing
2519 #define bfd_elf64_bfd_is_local_label_name \
2520 mmix_elf_is_local_label_name
2522 #define elf_backend_may_use_rel_p 0
2523 #define elf_backend_may_use_rela_p 1
2524 #define elf_backend_default_use_rela_p 1
2526 #define elf_backend_can_gc_sections 1
2527 #define elf_backend_section_from_bfd_section \
2528 mmix_elf_section_from_bfd_section
2530 #define bfd_elf64_bfd_final_link mmix_elf_final_link
2531 #define bfd_elf64_bfd_relax_section mmix_elf_relax_section
2533 #include "elf64-target.h"