Add translations for various sub-directories
[binutils-gdb.git] / bfd / elf64-mmix.c
blob1b5a65d847976525bbb32381b471f64dedce746a
1 /* MMIX-specific support for 64-bit ELF.
2 Copyright (C) 2001-2025 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 /* No specific ABI or "processor-specific supplement" defined. */
25 /* TODO:
26 - "Traditional" linker relaxation (shrinking whole sections).
27 - Merge reloc stubs jumping to same location.
28 - GETA stub relaxation (call a stub for out of range new
29 R_MMIX_GETA_STUBBABLE). */
31 #include "sysdep.h"
32 #include "bfd.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/mmix.h"
36 #include "opcode/mmix.h"
38 #define MINUS_ONE (((bfd_vma) 0) - 1)
40 #define MAX_PUSHJ_STUB_SIZE (5 * 4)
42 /* Put these everywhere in new code. */
43 #define FATAL_DEBUG \
44 _bfd_abort (__FILE__, __LINE__, \
45 "Internal: Non-debugged code (test-case missing)")
47 #define BAD_CASE(x) \
48 _bfd_abort (__FILE__, __LINE__, \
49 "bad case for " #x)
51 struct _mmix_elf_section_data
53 struct bfd_elf_section_data elf;
54 union
56 struct bpo_reloc_section_info *reloc;
57 struct bpo_greg_section_info *greg;
58 } bpo;
60 struct pushj_stub_info
62 /* Maximum number of stubs needed for this section. */
63 bfd_size_type n_pushj_relocs;
65 /* Size of stubs after a mmix_elf_relax_section round. */
66 bfd_size_type stubs_size_sum;
68 /* Per-reloc stubs_size_sum information. The stubs_size_sum member is the sum
69 of these. Allocated in mmix_elf_check_common_relocs. */
70 bfd_size_type *stub_size;
72 /* Offset of next stub during relocation. Somewhat redundant with the
73 above: error coverage is easier and we don't have to reset the
74 stubs_size_sum for relocation. */
75 bfd_size_type stub_offset;
76 } pjs;
78 /* Whether there has been a warning that this section could not be
79 linked due to a specific cause. FIXME: a way to access the
80 linker info or output section, then stuff the limiter guard
81 there. */
82 bool has_warned_bpo;
83 bool has_warned_pushj;
86 #define mmix_elf_section_data(sec) \
87 ((struct _mmix_elf_section_data *) elf_section_data (sec))
89 /* For each section containing a base-plus-offset (BPO) reloc, we attach
90 this struct as mmix_elf_section_data (section)->bpo, which is otherwise
91 NULL. */
92 struct bpo_reloc_section_info
94 /* The base is 1; this is the first number in this section. */
95 size_t first_base_plus_offset_reloc;
97 /* Number of BPO-relocs in this section. */
98 size_t n_bpo_relocs_this_section;
100 /* Running index, used at relocation time. */
101 size_t bpo_index;
103 /* We don't have access to the bfd_link_info struct in
104 mmix_final_link_relocate. What we really want to get at is the
105 global single struct greg_relocation, so we stash it here. */
106 asection *bpo_greg_section;
109 /* Helper struct (in global context) for the one below.
110 There's one of these created for every BPO reloc. */
111 struct bpo_reloc_request
113 bfd_vma value;
115 /* Valid after relaxation. The base is 0; the first register number
116 must be added. The offset is in range 0..255. */
117 size_t regindex;
118 size_t offset;
120 /* The order number for this BPO reloc, corresponding to the order in
121 which BPO relocs were found. Used to create an index after reloc
122 requests are sorted. */
123 size_t bpo_reloc_no;
125 /* Set when the value is computed. Better than coding "guard values"
126 into the other members. Is FALSE only for BPO relocs in a GC:ed
127 section. */
128 bool valid;
131 /* We attach this as mmix_elf_section_data (sec)->bpo in the linker-allocated
132 greg contents section (MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME),
133 which is linked into the register contents section
134 (MMIX_REG_CONTENTS_SECTION_NAME). This section is created by the
135 linker; using the same hook as for usual with BPO relocs does not
136 collide. */
137 struct bpo_greg_section_info
139 /* After GC, this reflects the number of remaining, non-excluded
140 BPO-relocs. */
141 size_t n_bpo_relocs;
143 /* This is the number of allocated bpo_reloc_requests; the size of
144 sorted_indexes. Valid after the check.*relocs functions are called
145 for all incoming sections. It includes the number of BPO relocs in
146 sections that were GC:ed. */
147 size_t n_max_bpo_relocs;
149 /* A counter used to find out when to fold the BPO gregs, since we
150 don't have a single "after-relaxation" hook. */
151 size_t n_remaining_bpo_relocs_this_relaxation_round;
153 /* The number of linker-allocated GREGs resulting from BPO relocs.
154 This is an approximation after _bfd_mmix_before_linker_allocation
155 and supposedly accurate after mmix_elf_relax_section is called for
156 all incoming non-collected sections. */
157 size_t n_allocated_bpo_gregs;
159 /* Index into reloc_request[], sorted on increasing "value", secondary
160 by increasing index for strict sorting order. */
161 size_t *bpo_reloc_indexes;
163 /* An array of all relocations, with the "value" member filled in by
164 the relaxation function. */
165 struct bpo_reloc_request *reloc_request;
169 extern bool mmix_elf_final_link (bfd *, struct bfd_link_info *);
171 extern void mmix_elf_symbol_processing (bfd *, asymbol *);
173 /* Only intended to be called from a debugger. */
174 extern void mmix_dump_bpo_gregs
175 (struct bfd_link_info *, void (*) (const char *, ...));
177 static void
178 mmix_set_relaxable_size (bfd *, asection *, void *);
179 static bfd_reloc_status_type
180 mmix_elf_reloc (bfd *, arelent *, asymbol *, void *,
181 asection *, bfd *, char **);
182 static bfd_reloc_status_type
183 mmix_final_link_relocate (reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
184 bfd_signed_vma, bfd_vma, const char *, asection *,
185 char **);
188 /* Watch out: this currently needs to have elements with the same index as
189 their R_MMIX_ number. */
190 static reloc_howto_type elf_mmix_howto_table[] =
192 /* This reloc does nothing. */
193 HOWTO (R_MMIX_NONE, /* type */
194 0, /* rightshift */
195 0, /* size */
196 0, /* bitsize */
197 false, /* pc_relative */
198 0, /* bitpos */
199 complain_overflow_dont, /* complain_on_overflow */
200 bfd_elf_generic_reloc, /* special_function */
201 "R_MMIX_NONE", /* name */
202 false, /* partial_inplace */
203 0, /* src_mask */
204 0, /* dst_mask */
205 false), /* pcrel_offset */
207 /* An 8 bit absolute relocation. */
208 HOWTO (R_MMIX_8, /* type */
209 0, /* rightshift */
210 1, /* size */
211 8, /* bitsize */
212 false, /* pc_relative */
213 0, /* bitpos */
214 complain_overflow_bitfield, /* complain_on_overflow */
215 bfd_elf_generic_reloc, /* special_function */
216 "R_MMIX_8", /* name */
217 false, /* partial_inplace */
218 0, /* src_mask */
219 0xff, /* dst_mask */
220 false), /* pcrel_offset */
222 /* An 16 bit absolute relocation. */
223 HOWTO (R_MMIX_16, /* type */
224 0, /* rightshift */
225 2, /* size */
226 16, /* bitsize */
227 false, /* pc_relative */
228 0, /* bitpos */
229 complain_overflow_bitfield, /* complain_on_overflow */
230 bfd_elf_generic_reloc, /* special_function */
231 "R_MMIX_16", /* name */
232 false, /* partial_inplace */
233 0, /* src_mask */
234 0xffff, /* dst_mask */
235 false), /* pcrel_offset */
237 /* An 24 bit absolute relocation. */
238 HOWTO (R_MMIX_24, /* type */
239 0, /* rightshift */
240 4, /* size */
241 24, /* bitsize */
242 false, /* pc_relative */
243 0, /* bitpos */
244 complain_overflow_bitfield, /* complain_on_overflow */
245 bfd_elf_generic_reloc, /* special_function */
246 "R_MMIX_24", /* name */
247 false, /* partial_inplace */
248 ~0xffffff, /* src_mask */
249 0xffffff, /* dst_mask */
250 false), /* pcrel_offset */
252 /* A 32 bit absolute relocation. */
253 HOWTO (R_MMIX_32, /* type */
254 0, /* rightshift */
255 4, /* size */
256 32, /* bitsize */
257 false, /* pc_relative */
258 0, /* bitpos */
259 complain_overflow_bitfield, /* complain_on_overflow */
260 bfd_elf_generic_reloc, /* special_function */
261 "R_MMIX_32", /* name */
262 false, /* partial_inplace */
263 0, /* src_mask */
264 0xffffffff, /* dst_mask */
265 false), /* pcrel_offset */
267 /* 64 bit relocation. */
268 HOWTO (R_MMIX_64, /* type */
269 0, /* rightshift */
270 8, /* size */
271 64, /* bitsize */
272 false, /* pc_relative */
273 0, /* bitpos */
274 complain_overflow_bitfield, /* complain_on_overflow */
275 bfd_elf_generic_reloc, /* special_function */
276 "R_MMIX_64", /* name */
277 false, /* partial_inplace */
278 0, /* src_mask */
279 MINUS_ONE, /* dst_mask */
280 false), /* pcrel_offset */
282 /* An 8 bit PC-relative relocation. */
283 HOWTO (R_MMIX_PC_8, /* type */
284 0, /* rightshift */
285 1, /* size */
286 8, /* bitsize */
287 true, /* pc_relative */
288 0, /* bitpos */
289 complain_overflow_bitfield, /* complain_on_overflow */
290 bfd_elf_generic_reloc, /* special_function */
291 "R_MMIX_PC_8", /* name */
292 false, /* partial_inplace */
293 0, /* src_mask */
294 0xff, /* dst_mask */
295 true), /* pcrel_offset */
297 /* An 16 bit PC-relative relocation. */
298 HOWTO (R_MMIX_PC_16, /* type */
299 0, /* rightshift */
300 2, /* size */
301 16, /* bitsize */
302 true, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_bitfield, /* complain_on_overflow */
305 bfd_elf_generic_reloc, /* special_function */
306 "R_MMIX_PC_16", /* name */
307 false, /* partial_inplace */
308 0, /* src_mask */
309 0xffff, /* dst_mask */
310 true), /* pcrel_offset */
312 /* An 24 bit PC-relative relocation. */
313 HOWTO (R_MMIX_PC_24, /* type */
314 0, /* rightshift */
315 4, /* size */
316 24, /* bitsize */
317 true, /* pc_relative */
318 0, /* bitpos */
319 complain_overflow_bitfield, /* complain_on_overflow */
320 bfd_elf_generic_reloc, /* special_function */
321 "R_MMIX_PC_24", /* name */
322 false, /* partial_inplace */
323 ~0xffffff, /* src_mask */
324 0xffffff, /* dst_mask */
325 true), /* pcrel_offset */
327 /* A 32 bit absolute PC-relative relocation. */
328 HOWTO (R_MMIX_PC_32, /* type */
329 0, /* rightshift */
330 4, /* size */
331 32, /* bitsize */
332 true, /* pc_relative */
333 0, /* bitpos */
334 complain_overflow_bitfield, /* complain_on_overflow */
335 bfd_elf_generic_reloc, /* special_function */
336 "R_MMIX_PC_32", /* name */
337 false, /* partial_inplace */
338 0, /* src_mask */
339 0xffffffff, /* dst_mask */
340 true), /* pcrel_offset */
342 /* 64 bit PC-relative relocation. */
343 HOWTO (R_MMIX_PC_64, /* type */
344 0, /* rightshift */
345 8, /* size */
346 64, /* bitsize */
347 true, /* pc_relative */
348 0, /* bitpos */
349 complain_overflow_bitfield, /* complain_on_overflow */
350 bfd_elf_generic_reloc, /* special_function */
351 "R_MMIX_PC_64", /* name */
352 false, /* partial_inplace */
353 0, /* src_mask */
354 MINUS_ONE, /* dst_mask */
355 true), /* pcrel_offset */
357 /* GNU extension to record C++ vtable hierarchy. */
358 HOWTO (R_MMIX_GNU_VTINHERIT, /* type */
359 0, /* rightshift */
360 0, /* size */
361 0, /* bitsize */
362 false, /* pc_relative */
363 0, /* bitpos */
364 complain_overflow_dont, /* complain_on_overflow */
365 NULL, /* special_function */
366 "R_MMIX_GNU_VTINHERIT", /* name */
367 false, /* partial_inplace */
368 0, /* src_mask */
369 0, /* dst_mask */
370 true), /* pcrel_offset */
372 /* GNU extension to record C++ vtable member usage. */
373 HOWTO (R_MMIX_GNU_VTENTRY, /* type */
374 0, /* rightshift */
375 0, /* size */
376 0, /* bitsize */
377 false, /* pc_relative */
378 0, /* bitpos */
379 complain_overflow_dont, /* complain_on_overflow */
380 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
381 "R_MMIX_GNU_VTENTRY", /* name */
382 false, /* partial_inplace */
383 0, /* src_mask */
384 0, /* dst_mask */
385 false), /* pcrel_offset */
387 /* The GETA relocation is supposed to get any address that could
388 possibly be reached by the GETA instruction. It can silently expand
389 to get a 64-bit operand, but will complain if any of the two least
390 significant bits are set. The howto members reflect a simple GETA. */
391 HOWTO (R_MMIX_GETA, /* type */
392 2, /* rightshift */
393 4, /* size */
394 19, /* bitsize */
395 true, /* pc_relative */
396 0, /* bitpos */
397 complain_overflow_signed, /* complain_on_overflow */
398 mmix_elf_reloc, /* special_function */
399 "R_MMIX_GETA", /* name */
400 false, /* partial_inplace */
401 ~0x0100ffff, /* src_mask */
402 0x0100ffff, /* dst_mask */
403 true), /* pcrel_offset */
405 HOWTO (R_MMIX_GETA_1, /* type */
406 2, /* rightshift */
407 4, /* size */
408 19, /* bitsize */
409 true, /* pc_relative */
410 0, /* bitpos */
411 complain_overflow_signed, /* complain_on_overflow */
412 mmix_elf_reloc, /* special_function */
413 "R_MMIX_GETA_1", /* name */
414 false, /* partial_inplace */
415 ~0x0100ffff, /* src_mask */
416 0x0100ffff, /* dst_mask */
417 true), /* pcrel_offset */
419 HOWTO (R_MMIX_GETA_2, /* type */
420 2, /* rightshift */
421 4, /* size */
422 19, /* bitsize */
423 true, /* pc_relative */
424 0, /* bitpos */
425 complain_overflow_signed, /* complain_on_overflow */
426 mmix_elf_reloc, /* special_function */
427 "R_MMIX_GETA_2", /* name */
428 false, /* partial_inplace */
429 ~0x0100ffff, /* src_mask */
430 0x0100ffff, /* dst_mask */
431 true), /* pcrel_offset */
433 HOWTO (R_MMIX_GETA_3, /* type */
434 2, /* rightshift */
435 4, /* size */
436 19, /* bitsize */
437 true, /* pc_relative */
438 0, /* bitpos */
439 complain_overflow_signed, /* complain_on_overflow */
440 mmix_elf_reloc, /* special_function */
441 "R_MMIX_GETA_3", /* name */
442 false, /* partial_inplace */
443 ~0x0100ffff, /* src_mask */
444 0x0100ffff, /* dst_mask */
445 true), /* pcrel_offset */
447 /* The conditional branches are supposed to reach any (code) address.
448 It can silently expand to a 64-bit operand, but will emit an error if
449 any of the two least significant bits are set. The howto members
450 reflect a simple branch. */
451 HOWTO (R_MMIX_CBRANCH, /* type */
452 2, /* rightshift */
453 4, /* size */
454 19, /* bitsize */
455 true, /* pc_relative */
456 0, /* bitpos */
457 complain_overflow_signed, /* complain_on_overflow */
458 mmix_elf_reloc, /* special_function */
459 "R_MMIX_CBRANCH", /* name */
460 false, /* partial_inplace */
461 ~0x0100ffff, /* src_mask */
462 0x0100ffff, /* dst_mask */
463 true), /* pcrel_offset */
465 HOWTO (R_MMIX_CBRANCH_J, /* type */
466 2, /* rightshift */
467 4, /* size */
468 19, /* bitsize */
469 true, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_signed, /* complain_on_overflow */
472 mmix_elf_reloc, /* special_function */
473 "R_MMIX_CBRANCH_J", /* name */
474 false, /* partial_inplace */
475 ~0x0100ffff, /* src_mask */
476 0x0100ffff, /* dst_mask */
477 true), /* pcrel_offset */
479 HOWTO (R_MMIX_CBRANCH_1, /* type */
480 2, /* rightshift */
481 4, /* size */
482 19, /* bitsize */
483 true, /* pc_relative */
484 0, /* bitpos */
485 complain_overflow_signed, /* complain_on_overflow */
486 mmix_elf_reloc, /* special_function */
487 "R_MMIX_CBRANCH_1", /* name */
488 false, /* partial_inplace */
489 ~0x0100ffff, /* src_mask */
490 0x0100ffff, /* dst_mask */
491 true), /* pcrel_offset */
493 HOWTO (R_MMIX_CBRANCH_2, /* type */
494 2, /* rightshift */
495 4, /* size */
496 19, /* bitsize */
497 true, /* pc_relative */
498 0, /* bitpos */
499 complain_overflow_signed, /* complain_on_overflow */
500 mmix_elf_reloc, /* special_function */
501 "R_MMIX_CBRANCH_2", /* name */
502 false, /* partial_inplace */
503 ~0x0100ffff, /* src_mask */
504 0x0100ffff, /* dst_mask */
505 true), /* pcrel_offset */
507 HOWTO (R_MMIX_CBRANCH_3, /* type */
508 2, /* rightshift */
509 4, /* size */
510 19, /* bitsize */
511 true, /* pc_relative */
512 0, /* bitpos */
513 complain_overflow_signed, /* complain_on_overflow */
514 mmix_elf_reloc, /* special_function */
515 "R_MMIX_CBRANCH_3", /* name */
516 false, /* partial_inplace */
517 ~0x0100ffff, /* src_mask */
518 0x0100ffff, /* dst_mask */
519 true), /* pcrel_offset */
521 /* The PUSHJ instruction can reach any (code) address, as long as it's
522 the beginning of a function (no usable restriction). It can silently
523 expand to a 64-bit operand, but will emit an error if any of the two
524 least significant bits are set. It can also expand into a call to a
525 stub; see R_MMIX_PUSHJ_STUBBABLE. The howto members reflect a simple
526 PUSHJ. */
527 HOWTO (R_MMIX_PUSHJ, /* type */
528 2, /* rightshift */
529 4, /* size */
530 19, /* bitsize */
531 true, /* pc_relative */
532 0, /* bitpos */
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 */
542 2, /* rightshift */
543 4, /* size */
544 19, /* bitsize */
545 true, /* pc_relative */
546 0, /* bitpos */
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 */
556 2, /* rightshift */
557 4, /* size */
558 19, /* bitsize */
559 true, /* pc_relative */
560 0, /* bitpos */
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 */
570 2, /* rightshift */
571 4, /* size */
572 19, /* bitsize */
573 true, /* pc_relative */
574 0, /* bitpos */
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 */
588 2, /* rightshift */
589 4, /* size */
590 27, /* bitsize */
591 true, /* pc_relative */
592 0, /* bitpos */
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 */
602 2, /* rightshift */
603 4, /* size */
604 27, /* bitsize */
605 true, /* pc_relative */
606 0, /* bitpos */
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 */
616 2, /* rightshift */
617 4, /* size */
618 27, /* bitsize */
619 true, /* pc_relative */
620 0, /* bitpos */
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 */
630 2, /* rightshift */
631 4, /* size */
632 27, /* bitsize */
633 true, /* pc_relative */
634 0, /* bitpos */
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 */
647 2, /* rightshift */
648 4, /* size */
649 19, /* bitsize */
650 true, /* pc_relative */
651 0, /* bitpos */
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 */
660 /* For JMP. */
661 HOWTO (R_MMIX_ADDR27, /* type */
662 2, /* rightshift */
663 4, /* size */
664 27, /* bitsize */
665 true, /* pc_relative */
666 0, /* bitpos */
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 */
678 0, /* rightshift */
679 2, /* size */
680 8, /* bitsize */
681 false, /* pc_relative */
682 0, /* bitpos */
683 complain_overflow_bitfield, /* complain_on_overflow */
684 mmix_elf_reloc, /* special_function */
685 "R_MMIX_REG_OR_BYTE", /* name */
686 false, /* partial_inplace */
687 0, /* src_mask */
688 0xff, /* dst_mask */
689 false), /* pcrel_offset */
691 /* A general register. */
692 HOWTO (R_MMIX_REG, /* type */
693 0, /* rightshift */
694 2, /* size */
695 8, /* bitsize */
696 false, /* pc_relative */
697 0, /* bitpos */
698 complain_overflow_bitfield, /* complain_on_overflow */
699 mmix_elf_reloc, /* special_function */
700 "R_MMIX_REG", /* name */
701 false, /* partial_inplace */
702 0, /* src_mask */
703 0xff, /* dst_mask */
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 */
710 0, /* rightshift */
711 8, /* size */
712 64, /* bitsize */
713 false, /* pc_relative */
714 0, /* bitpos */
715 complain_overflow_bitfield, /* complain_on_overflow */
716 mmix_elf_reloc, /* special_function */
717 "R_MMIX_BASE_PLUS_OFFSET", /* name */
718 false, /* partial_inplace */
719 0, /* src_mask */
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 */
729 0, /* rightshift */
730 0, /* size */
731 0, /* bitsize */
732 false, /* pc_relative */
733 0, /* bitpos */
734 complain_overflow_dont, /* complain_on_overflow */
735 mmix_elf_reloc, /* special_function */
736 "R_MMIX_LOCAL", /* name */
737 false, /* partial_inplace */
738 0, /* src_mask */
739 0, /* dst_mask */
740 false), /* pcrel_offset */
742 HOWTO (R_MMIX_PUSHJ_STUBBABLE, /* type */
743 2, /* rightshift */
744 4, /* size */
745 19, /* bitsize */
746 true, /* pc_relative */
747 0, /* bitpos */
748 complain_overflow_signed, /* complain_on_overflow */
749 mmix_elf_reloc, /* special_function */
750 "R_MMIX_PUSHJ_STUBBABLE", /* name */
751 false, /* partial_inplace */
752 ~0x0100ffff, /* src_mask */
753 0x0100ffff, /* dst_mask */
754 true) /* pcrel_offset */
758 /* Map BFD reloc types to MMIX ELF reloc types. */
760 struct mmix_reloc_map
762 bfd_reloc_code_real_type bfd_reloc_val;
763 enum elf_mmix_reloc_type elf_reloc_val;
767 static const struct mmix_reloc_map mmix_reloc_map[] =
769 {BFD_RELOC_NONE, R_MMIX_NONE},
770 {BFD_RELOC_8, R_MMIX_8},
771 {BFD_RELOC_16, R_MMIX_16},
772 {BFD_RELOC_24, R_MMIX_24},
773 {BFD_RELOC_32, R_MMIX_32},
774 {BFD_RELOC_64, R_MMIX_64},
775 {BFD_RELOC_8_PCREL, R_MMIX_PC_8},
776 {BFD_RELOC_16_PCREL, R_MMIX_PC_16},
777 {BFD_RELOC_24_PCREL, R_MMIX_PC_24},
778 {BFD_RELOC_32_PCREL, R_MMIX_PC_32},
779 {BFD_RELOC_64_PCREL, R_MMIX_PC_64},
780 {BFD_RELOC_VTABLE_INHERIT, R_MMIX_GNU_VTINHERIT},
781 {BFD_RELOC_VTABLE_ENTRY, R_MMIX_GNU_VTENTRY},
782 {BFD_RELOC_MMIX_GETA, R_MMIX_GETA},
783 {BFD_RELOC_MMIX_CBRANCH, R_MMIX_CBRANCH},
784 {BFD_RELOC_MMIX_PUSHJ, R_MMIX_PUSHJ},
785 {BFD_RELOC_MMIX_JMP, R_MMIX_JMP},
786 {BFD_RELOC_MMIX_ADDR19, R_MMIX_ADDR19},
787 {BFD_RELOC_MMIX_ADDR27, R_MMIX_ADDR27},
788 {BFD_RELOC_MMIX_REG_OR_BYTE, R_MMIX_REG_OR_BYTE},
789 {BFD_RELOC_MMIX_REG, R_MMIX_REG},
790 {BFD_RELOC_MMIX_BASE_PLUS_OFFSET, R_MMIX_BASE_PLUS_OFFSET},
791 {BFD_RELOC_MMIX_LOCAL, R_MMIX_LOCAL},
792 {BFD_RELOC_MMIX_PUSHJ_STUBBABLE, R_MMIX_PUSHJ_STUBBABLE}
795 static reloc_howto_type *
796 bfd_elf64_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
797 bfd_reloc_code_real_type code)
799 unsigned int i;
801 for (i = 0;
802 i < sizeof (mmix_reloc_map) / sizeof (mmix_reloc_map[0]);
803 i++)
805 if (mmix_reloc_map[i].bfd_reloc_val == code)
806 return &elf_mmix_howto_table[mmix_reloc_map[i].elf_reloc_val];
809 return NULL;
812 static reloc_howto_type *
813 bfd_elf64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
814 const char *r_name)
816 unsigned int i;
818 for (i = 0;
819 i < sizeof (elf_mmix_howto_table) / sizeof (elf_mmix_howto_table[0]);
820 i++)
821 if (elf_mmix_howto_table[i].name != NULL
822 && strcasecmp (elf_mmix_howto_table[i].name, r_name) == 0)
823 return &elf_mmix_howto_table[i];
825 return NULL;
828 static bool
829 mmix_elf_new_section_hook (bfd *abfd, asection *sec)
831 struct _mmix_elf_section_data *sdata;
833 sdata = bfd_zalloc (abfd, sizeof (*sdata));
834 if (sdata == NULL)
835 return false;
836 sec->used_by_bfd = sdata;
838 return _bfd_elf_new_section_hook (abfd, sec);
842 /* This function performs the actual bitfiddling and sanity check for a
843 final relocation. Each relocation gets its *worst*-case expansion
844 in size when it arrives here; any reduction in size should have been
845 caught in linker relaxation earlier. When we get here, the relocation
846 looks like the smallest instruction with SWYM:s (nop:s) appended to the
847 max size. We fill in those nop:s.
849 R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
850 GETA $N,foo
852 SETL $N,foo & 0xffff
853 INCML $N,(foo >> 16) & 0xffff
854 INCMH $N,(foo >> 32) & 0xffff
855 INCH $N,(foo >> 48) & 0xffff
857 R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
858 condbranches needing relaxation might be rare enough to not be
859 worthwhile.)
860 [P]Bcc $N,foo
862 [~P]B~cc $N,.+20
863 SETL $255,foo & ...
864 INCML ...
865 INCMH ...
866 INCH ...
867 GO $255,$255,0
869 R_MMIX_PUSHJ: (FIXME: Relaxation...)
870 PUSHJ $N,foo
872 SETL $255,foo & ...
873 INCML ...
874 INCMH ...
875 INCH ...
876 PUSHGO $N,$255,0
878 R_MMIX_JMP: (FIXME: Relaxation...)
879 JMP foo
881 SETL $255,foo & ...
882 INCML ...
883 INCMH ...
884 INCH ...
885 GO $255,$255,0
887 R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in. */
889 static bfd_reloc_status_type
890 mmix_elf_perform_relocation (asection *isec, reloc_howto_type *howto,
891 void *datap, bfd_vma addr, bfd_vma value,
892 char **error_message)
894 bfd *abfd = isec->owner;
895 bfd_reloc_status_type flag = bfd_reloc_ok;
896 bfd_reloc_status_type r;
897 int offs = 0;
898 int reg = 255;
900 /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
901 We handle the differences here and the common sequence later. */
902 switch (howto->type)
904 case R_MMIX_GETA:
905 offs = 0;
906 reg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
908 /* We change to an absolute value. */
909 value += addr;
910 break;
912 case R_MMIX_CBRANCH:
914 int in1 = bfd_get_16 (abfd, (bfd_byte *) datap) << 16;
916 /* Invert the condition and prediction bit, and set the offset
917 to five instructions ahead.
919 We *can* do better if we want to. If the branch is found to be
920 within limits, we could leave the branch as is; there'll just
921 be a bunch of NOP:s after it. But we shouldn't see this
922 sequence often enough that it's worth doing it. */
924 bfd_put_32 (abfd,
925 (((in1 ^ ((PRED_INV_BIT | COND_INV_BIT) << 24)) & ~0xffff)
926 | (24/4)),
927 (bfd_byte *) datap);
929 /* Put a "GO $255,$255,0" after the common sequence. */
930 bfd_put_32 (abfd,
931 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24) | 0xffff00,
932 (bfd_byte *) datap + 20);
934 /* Common sequence starts at offset 4. */
935 offs = 4;
937 /* We change to an absolute value. */
938 value += addr;
940 break;
942 case R_MMIX_PUSHJ_STUBBABLE:
943 /* If the address fits, we're fine. */
944 if ((value & 3) == 0
945 /* Note rightshift 0; see R_MMIX_JMP case below. */
946 && (r = bfd_check_overflow (complain_overflow_signed,
947 howto->bitsize,
949 bfd_arch_bits_per_address (abfd),
950 value)) == bfd_reloc_ok)
951 goto pcrel_mmix_reloc_fits;
952 else
954 bfd_size_type size = isec->rawsize ? isec->rawsize : isec->size;
956 /* We have the bytes at the PUSHJ insn and need to get the
957 position for the stub. There's supposed to be room allocated
958 for the stub. */
959 bfd_byte *stubcontents
960 = ((bfd_byte *) datap
961 - (addr - (isec->output_section->vma + isec->output_offset))
962 + size
963 + mmix_elf_section_data (isec)->pjs.stub_offset);
964 bfd_vma stubaddr;
966 if (mmix_elf_section_data (isec)->pjs.n_pushj_relocs == 0)
968 /* This shouldn't happen when linking to ELF or mmo, so
969 this is an attempt to link to "binary", right? We
970 can't access the output bfd, so we can't verify that
971 assumption. We only know that the critical
972 mmix_elf_check_common_relocs has not been called,
973 which happens when the output format is different
974 from the input format (and is not mmo). */
975 if (! mmix_elf_section_data (isec)->has_warned_pushj)
977 /* For the first such error per input section, produce
978 a verbose message. */
979 *error_message
980 = _("invalid input relocation when producing"
981 " non-ELF, non-mmo format output;"
982 " please use the objcopy program to convert from"
983 " ELF or mmo,"
984 " or assemble using"
985 " \"-no-expand\" (for gcc, \"-Wa,-no-expand\"");
986 mmix_elf_section_data (isec)->has_warned_pushj = true;
987 return bfd_reloc_dangerous;
990 /* For subsequent errors, return this one, which is
991 rate-limited but looks a little bit different,
992 hopefully without affecting user-friendliness. */
993 return bfd_reloc_overflow;
996 /* The address doesn't fit, so redirect the PUSHJ to the
997 location of the stub. */
998 r = mmix_elf_perform_relocation (isec,
999 &elf_mmix_howto_table
1000 [R_MMIX_ADDR19],
1001 datap,
1002 addr,
1003 isec->output_section->vma
1004 + isec->output_offset
1005 + size
1006 + (mmix_elf_section_data (isec)
1007 ->pjs.stub_offset)
1008 - addr,
1009 error_message);
1010 if (r != bfd_reloc_ok)
1011 return r;
1013 stubaddr
1014 = (isec->output_section->vma
1015 + isec->output_offset
1016 + size
1017 + mmix_elf_section_data (isec)->pjs.stub_offset);
1019 /* We generate a simple JMP if that suffices, else the whole 5
1020 insn stub. */
1021 if (bfd_check_overflow (complain_overflow_signed,
1022 elf_mmix_howto_table[R_MMIX_ADDR27].bitsize,
1024 bfd_arch_bits_per_address (abfd),
1025 addr + value - stubaddr) == bfd_reloc_ok)
1027 bfd_put_32 (abfd, JMP_INSN_BYTE << 24, stubcontents);
1028 r = mmix_elf_perform_relocation (isec,
1029 &elf_mmix_howto_table
1030 [R_MMIX_ADDR27],
1031 stubcontents,
1032 stubaddr,
1033 value + addr - stubaddr,
1034 error_message);
1035 mmix_elf_section_data (isec)->pjs.stub_offset += 4;
1037 if (size + mmix_elf_section_data (isec)->pjs.stub_offset
1038 > isec->size)
1039 abort ();
1041 return r;
1043 else
1045 /* Put a "GO $255,0" after the common sequence. */
1046 bfd_put_32 (abfd,
1047 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1048 | 0xff00, (bfd_byte *) stubcontents + 16);
1050 /* Prepare for the general code to set the first part of the
1051 linker stub, and */
1052 value += addr;
1053 datap = stubcontents;
1054 mmix_elf_section_data (isec)->pjs.stub_offset
1055 += MAX_PUSHJ_STUB_SIZE;
1058 break;
1060 case R_MMIX_PUSHJ:
1062 int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
1064 /* Put a "PUSHGO $N,$255,0" after the common sequence. */
1065 bfd_put_32 (abfd,
1066 ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1067 | (inreg << 16)
1068 | 0xff00,
1069 (bfd_byte *) datap + 16);
1071 /* We change to an absolute value. */
1072 value += addr;
1074 break;
1076 case R_MMIX_JMP:
1077 /* This one is a little special. If we get here on a non-relaxing
1078 link, and the destination is actually in range, we don't need to
1079 execute the nops.
1080 If so, we fall through to the bit-fiddling relocs.
1082 FIXME: bfd_check_overflow seems broken; the relocation is
1083 rightshifted before testing, so supply a zero rightshift. */
1085 if (! ((value & 3) == 0
1086 && (r = bfd_check_overflow (complain_overflow_signed,
1087 howto->bitsize,
1089 bfd_arch_bits_per_address (abfd),
1090 value)) == bfd_reloc_ok))
1092 /* If the relocation doesn't fit in a JMP, we let the NOP:s be
1093 modified below, and put a "GO $255,$255,0" after the
1094 address-loading sequence. */
1095 bfd_put_32 (abfd,
1096 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1097 | 0xffff00,
1098 (bfd_byte *) datap + 16);
1100 /* We change to an absolute value. */
1101 value += addr;
1102 break;
1104 /* FALLTHROUGH. */
1105 case R_MMIX_ADDR19:
1106 case R_MMIX_ADDR27:
1107 pcrel_mmix_reloc_fits:
1108 /* These must be in range, or else we emit an error. */
1109 if ((value & 3) == 0
1110 /* Note rightshift 0; see above. */
1111 && (r = bfd_check_overflow (complain_overflow_signed,
1112 howto->bitsize,
1114 bfd_arch_bits_per_address (abfd),
1115 value)) == bfd_reloc_ok)
1117 bfd_vma in1
1118 = bfd_get_32 (abfd, (bfd_byte *) datap);
1119 bfd_vma highbit;
1121 if ((bfd_signed_vma) value < 0)
1123 highbit = 1 << 24;
1124 value += (1 << (howto->bitsize - 1));
1126 else
1127 highbit = 0;
1129 value >>= 2;
1131 bfd_put_32 (abfd,
1132 (in1 & howto->src_mask)
1133 | highbit
1134 | (value & howto->dst_mask),
1135 (bfd_byte *) datap);
1137 return bfd_reloc_ok;
1139 else
1140 return bfd_reloc_overflow;
1142 case R_MMIX_BASE_PLUS_OFFSET:
1144 struct bpo_reloc_section_info *bpodata
1145 = mmix_elf_section_data (isec)->bpo.reloc;
1146 asection *bpo_greg_section;
1147 struct bpo_greg_section_info *gregdata;
1148 size_t bpo_index;
1150 if (bpodata == NULL)
1152 /* This shouldn't happen when linking to ELF or mmo, so
1153 this is an attempt to link to "binary", right? We
1154 can't access the output bfd, so we can't verify that
1155 assumption. We only know that the critical
1156 mmix_elf_check_common_relocs has not been called, which
1157 happens when the output format is different from the
1158 input format (and is not mmo). */
1159 if (! mmix_elf_section_data (isec)->has_warned_bpo)
1161 /* For the first such error per input section, produce
1162 a verbose message. */
1163 *error_message
1164 = _("invalid input relocation when producing"
1165 " non-ELF, non-mmo format output;"
1166 " please use the objcopy program to convert from"
1167 " ELF or mmo,"
1168 " or compile using the gcc-option"
1169 " \"-mno-base-addresses\".");
1170 mmix_elf_section_data (isec)->has_warned_bpo = true;
1171 return bfd_reloc_dangerous;
1174 /* For subsequent errors, return this one, which is
1175 rate-limited but looks a little bit different,
1176 hopefully without affecting user-friendliness. */
1177 return bfd_reloc_overflow;
1180 bpo_greg_section = bpodata->bpo_greg_section;
1181 gregdata = mmix_elf_section_data (bpo_greg_section)->bpo.greg;
1182 bpo_index = gregdata->bpo_reloc_indexes[bpodata->bpo_index++];
1184 /* A consistency check: The value we now have in "relocation" must
1185 be the same as the value we stored for that relocation. It
1186 doesn't cost much, so can be left in at all times. */
1187 if (value != gregdata->reloc_request[bpo_index].value)
1189 _bfd_error_handler
1190 /* xgettext:c-format */
1191 (_("%pB: Internal inconsistency error for value for\n\
1192 linker-allocated global register: linked: %#" PRIx64 " != relaxed: %#" PRIx64 ""),
1193 isec->owner,
1194 (uint64_t) value,
1195 (uint64_t) gregdata->reloc_request[bpo_index].value);
1196 bfd_set_error (bfd_error_bad_value);
1197 return bfd_reloc_overflow;
1200 /* Then store the register number and offset for that register
1201 into datap and datap + 1 respectively. */
1202 bfd_put_8 (abfd,
1203 gregdata->reloc_request[bpo_index].regindex
1204 + bpo_greg_section->output_section->vma / 8,
1205 datap);
1206 bfd_put_8 (abfd,
1207 gregdata->reloc_request[bpo_index].offset,
1208 ((unsigned char *) datap) + 1);
1209 return bfd_reloc_ok;
1212 case R_MMIX_REG_OR_BYTE:
1213 case R_MMIX_REG:
1214 if (value > 255)
1215 return bfd_reloc_overflow;
1216 bfd_put_8 (abfd, value, datap);
1217 return bfd_reloc_ok;
1219 default:
1220 BAD_CASE (howto->type);
1223 /* This code adds the common SETL/INCML/INCMH/INCH worst-case
1224 sequence. */
1226 /* Lowest two bits must be 0. We return bfd_reloc_overflow for
1227 everything that looks strange. */
1228 if (value & 3)
1229 flag = bfd_reloc_overflow;
1231 bfd_put_32 (abfd,
1232 (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
1233 (bfd_byte *) datap + offs);
1234 bfd_put_32 (abfd,
1235 (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
1236 (bfd_byte *) datap + offs + 4);
1237 bfd_put_32 (abfd,
1238 (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
1239 (bfd_byte *) datap + offs + 8);
1240 bfd_put_32 (abfd,
1241 (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
1242 (bfd_byte *) datap + offs + 12);
1244 return flag;
1247 /* Set the howto pointer for an MMIX ELF reloc (type RELA). */
1249 static bool
1250 mmix_info_to_howto_rela (bfd *abfd,
1251 arelent *cache_ptr,
1252 Elf_Internal_Rela *dst)
1254 unsigned int r_type;
1256 r_type = ELF64_R_TYPE (dst->r_info);
1257 if (r_type >= (unsigned int) R_MMIX_max)
1259 /* xgettext:c-format */
1260 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1261 abfd, r_type);
1262 bfd_set_error (bfd_error_bad_value);
1263 return false;
1265 cache_ptr->howto = &elf_mmix_howto_table[r_type];
1266 return true;
1269 /* Any MMIX-specific relocation gets here at assembly time or when linking
1270 to other formats (such as mmo); this is the relocation function from
1271 the reloc_table. We don't get here for final pure ELF linking. */
1273 static bfd_reloc_status_type
1274 mmix_elf_reloc (bfd *abfd,
1275 arelent *reloc_entry,
1276 asymbol *symbol,
1277 void * data,
1278 asection *input_section,
1279 bfd *output_bfd,
1280 char **error_message)
1282 bfd_vma relocation;
1283 bfd_reloc_status_type r;
1284 asection *reloc_target_output_section;
1285 bfd_reloc_status_type flag = bfd_reloc_ok;
1286 bfd_vma output_base = 0;
1288 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1289 input_section, output_bfd, error_message);
1291 /* If that was all that was needed (i.e. this isn't a final link, only
1292 some segment adjustments), we're done. */
1293 if (r != bfd_reloc_continue)
1294 return r;
1296 if (bfd_is_und_section (symbol->section)
1297 && (symbol->flags & BSF_WEAK) == 0
1298 && output_bfd == (bfd *) NULL)
1299 return bfd_reloc_undefined;
1301 /* Is the address of the relocation really within the section? */
1302 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1303 return bfd_reloc_outofrange;
1305 /* Work out which section the relocation is targeted at and the
1306 initial relocation command value. */
1308 /* Get symbol value. (Common symbols are special.) */
1309 if (bfd_is_com_section (symbol->section))
1310 relocation = 0;
1311 else
1312 relocation = symbol->value;
1314 reloc_target_output_section = bfd_asymbol_section (symbol)->output_section;
1316 /* Here the variable relocation holds the final address of the symbol we
1317 are relocating against, plus any addend. */
1318 if (output_bfd)
1319 output_base = 0;
1320 else
1321 output_base = reloc_target_output_section->vma;
1323 relocation += output_base + symbol->section->output_offset;
1325 if (output_bfd != (bfd *) NULL)
1327 /* Add in supplied addend. */
1328 relocation += reloc_entry->addend;
1330 /* This is a partial relocation, and we want to apply the
1331 relocation to the reloc entry rather than the raw data.
1332 Modify the reloc inplace to reflect what we now know. */
1333 reloc_entry->addend = relocation;
1334 reloc_entry->address += input_section->output_offset;
1335 return flag;
1338 return mmix_final_link_relocate (reloc_entry->howto, input_section,
1339 data, reloc_entry->address,
1340 reloc_entry->addend, relocation,
1341 bfd_asymbol_name (symbol),
1342 reloc_target_output_section,
1343 error_message);
1346 /* Relocate an MMIX ELF section. Modified from elf32-fr30.c; look to it
1347 for guidance if you're thinking of copying this. */
1349 static int
1350 mmix_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1351 struct bfd_link_info *info,
1352 bfd *input_bfd,
1353 asection *input_section,
1354 bfd_byte *contents,
1355 Elf_Internal_Rela *relocs,
1356 Elf_Internal_Sym *local_syms,
1357 asection **local_sections)
1359 Elf_Internal_Shdr *symtab_hdr;
1360 struct elf_link_hash_entry **sym_hashes;
1361 Elf_Internal_Rela *rel;
1362 Elf_Internal_Rela *relend;
1363 bfd_size_type size;
1364 size_t pjsno = 0;
1366 size = input_section->rawsize ? input_section->rawsize : input_section->size;
1367 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1368 sym_hashes = elf_sym_hashes (input_bfd);
1369 relend = relocs + input_section->reloc_count;
1371 /* Zero the stub area before we start. */
1372 if (input_section->rawsize != 0
1373 && input_section->size > input_section->rawsize)
1374 memset (contents + input_section->rawsize, 0,
1375 input_section->size - input_section->rawsize);
1377 for (rel = relocs; rel < relend; rel ++)
1379 reloc_howto_type *howto;
1380 unsigned long r_symndx;
1381 Elf_Internal_Sym *sym;
1382 asection *sec;
1383 struct elf_link_hash_entry *h;
1384 bfd_vma relocation;
1385 bfd_reloc_status_type r;
1386 const char *name = NULL;
1387 int r_type;
1388 bool undefined_signalled = false;
1390 r_type = ELF64_R_TYPE (rel->r_info);
1392 if (r_type == R_MMIX_GNU_VTINHERIT
1393 || r_type == R_MMIX_GNU_VTENTRY)
1394 continue;
1396 r_symndx = ELF64_R_SYM (rel->r_info);
1398 howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
1399 h = NULL;
1400 sym = NULL;
1401 sec = NULL;
1403 if (r_symndx < symtab_hdr->sh_info)
1405 sym = local_syms + r_symndx;
1406 sec = local_sections [r_symndx];
1407 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1409 name = bfd_elf_string_from_elf_section (input_bfd,
1410 symtab_hdr->sh_link,
1411 sym->st_name);
1412 if (name == NULL)
1413 name = bfd_section_name (sec);
1415 else
1417 bool unresolved_reloc, ignored;
1419 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1420 r_symndx, symtab_hdr, sym_hashes,
1421 h, sec, relocation,
1422 unresolved_reloc, undefined_signalled,
1423 ignored);
1424 name = h->root.root.string;
1427 if (sec != NULL && discarded_section (sec))
1428 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1429 rel, 1, relend, howto, 0, contents);
1431 if (bfd_link_relocatable (info))
1433 /* This is a relocatable link. For most relocs we don't have to
1434 change anything, unless the reloc is against a section
1435 symbol, in which case we have to adjust according to where
1436 the section symbol winds up in the output section. */
1437 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1438 rel->r_addend += sec->output_offset;
1440 /* For PUSHJ stub relocs however, we may need to change the
1441 reloc and the section contents, if the reloc doesn't reach
1442 beyond the end of the output section and previous stubs.
1443 Then we change the section contents to be a PUSHJ to the end
1444 of the input section plus stubs (we can do that without using
1445 a reloc), and then we change the reloc to be a R_MMIX_PUSHJ
1446 at the stub location. */
1447 if (r_type == R_MMIX_PUSHJ_STUBBABLE)
1449 /* We've already checked whether we need a stub; use that
1450 knowledge. */
1451 if (mmix_elf_section_data (input_section)->pjs.stub_size[pjsno]
1452 != 0)
1454 Elf_Internal_Rela relcpy;
1456 if (mmix_elf_section_data (input_section)
1457 ->pjs.stub_size[pjsno] != MAX_PUSHJ_STUB_SIZE)
1458 abort ();
1460 /* There's already a PUSHJ insn there, so just fill in
1461 the offset bits to the stub. */
1462 if (mmix_final_link_relocate (elf_mmix_howto_table
1463 + R_MMIX_ADDR19,
1464 input_section,
1465 contents,
1466 rel->r_offset,
1468 input_section
1469 ->output_section->vma
1470 + input_section->output_offset
1471 + size
1472 + mmix_elf_section_data (input_section)
1473 ->pjs.stub_offset,
1474 NULL, NULL, NULL) != bfd_reloc_ok)
1475 return false;
1477 /* Put a JMP insn at the stub; it goes with the
1478 R_MMIX_JMP reloc. */
1479 bfd_put_32 (output_bfd, JMP_INSN_BYTE << 24,
1480 contents
1481 + size
1482 + mmix_elf_section_data (input_section)
1483 ->pjs.stub_offset);
1485 /* Change the reloc to be at the stub, and to a full
1486 R_MMIX_JMP reloc. */
1487 rel->r_info = ELF64_R_INFO (r_symndx, R_MMIX_JMP);
1488 rel->r_offset
1489 = (size
1490 + mmix_elf_section_data (input_section)
1491 ->pjs.stub_offset);
1493 mmix_elf_section_data (input_section)->pjs.stub_offset
1494 += MAX_PUSHJ_STUB_SIZE;
1496 /* Shift this reloc to the end of the relocs to maintain
1497 the r_offset sorted reloc order. */
1498 relcpy = *rel;
1499 memmove (rel, rel + 1, (char *) relend - (char *) (rel + 1));
1500 relend[-1] = relcpy;
1502 /* Back up one reloc, or else we'd skip the next reloc
1503 in turn. */
1504 rel--;
1507 pjsno++;
1509 continue;
1512 r = mmix_final_link_relocate (howto, input_section,
1513 contents, rel->r_offset,
1514 rel->r_addend, relocation, name, sec, NULL);
1516 if (r != bfd_reloc_ok)
1518 const char * msg = (const char *) NULL;
1520 switch (r)
1522 case bfd_reloc_overflow:
1523 info->callbacks->reloc_overflow
1524 (info, (h ? &h->root : NULL), name, howto->name,
1525 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1526 break;
1528 case bfd_reloc_undefined:
1529 /* We may have sent this message above. */
1530 if (! undefined_signalled)
1531 info->callbacks->undefined_symbol
1532 (info, name, input_bfd, input_section, rel->r_offset, true);
1533 undefined_signalled = true;
1534 break;
1536 case bfd_reloc_outofrange:
1537 msg = _("internal error: out of range error");
1538 break;
1540 case bfd_reloc_notsupported:
1541 msg = _("internal error: unsupported relocation error");
1542 break;
1544 case bfd_reloc_dangerous:
1545 msg = _("internal error: dangerous relocation");
1546 break;
1548 default:
1549 msg = _("internal error: unknown error");
1550 break;
1553 if (msg)
1554 (*info->callbacks->warning) (info, msg, name, input_bfd,
1555 input_section, rel->r_offset);
1559 return true;
1562 /* Perform a single relocation. By default we use the standard BFD
1563 routines. A few relocs we have to do ourselves. */
1565 static bfd_reloc_status_type
1566 mmix_final_link_relocate (reloc_howto_type *howto, asection *input_section,
1567 bfd_byte *contents, bfd_vma r_offset,
1568 bfd_signed_vma r_addend, bfd_vma relocation,
1569 const char *symname, asection *symsec,
1570 char **error_message)
1572 bfd_reloc_status_type r = bfd_reloc_ok;
1573 bfd_vma addr
1574 = (input_section->output_section->vma
1575 + input_section->output_offset
1576 + r_offset);
1577 bfd_signed_vma srel
1578 = (bfd_signed_vma) relocation + r_addend;
1580 switch (howto->type)
1582 /* All these are PC-relative. */
1583 case R_MMIX_PUSHJ_STUBBABLE:
1584 case R_MMIX_PUSHJ:
1585 case R_MMIX_CBRANCH:
1586 case R_MMIX_ADDR19:
1587 case R_MMIX_GETA:
1588 case R_MMIX_ADDR27:
1589 case R_MMIX_JMP:
1590 contents += r_offset;
1592 srel -= (input_section->output_section->vma
1593 + input_section->output_offset
1594 + r_offset);
1596 r = mmix_elf_perform_relocation (input_section, howto, contents,
1597 addr, srel, error_message);
1598 break;
1600 case R_MMIX_BASE_PLUS_OFFSET:
1601 if (symsec == NULL)
1602 return bfd_reloc_undefined;
1604 /* Check that we're not relocating against a register symbol. */
1605 if (strcmp (bfd_section_name (symsec),
1606 MMIX_REG_CONTENTS_SECTION_NAME) == 0
1607 || strcmp (bfd_section_name (symsec),
1608 MMIX_REG_SECTION_NAME) == 0)
1610 /* Note: This is separated out into two messages in order
1611 to ease the translation into other languages. */
1612 if (symname == NULL || *symname == 0)
1613 _bfd_error_handler
1614 /* xgettext:c-format */
1615 (_("%pB: base-plus-offset relocation against register symbol:"
1616 " (unknown) in %pA"),
1617 input_section->owner, symsec);
1618 else
1619 _bfd_error_handler
1620 /* xgettext:c-format */
1621 (_("%pB: base-plus-offset relocation against register symbol:"
1622 " %s in %pA"),
1623 input_section->owner, symname, symsec);
1624 return bfd_reloc_overflow;
1626 goto do_mmix_reloc;
1628 case R_MMIX_REG_OR_BYTE:
1629 case R_MMIX_REG:
1630 /* For now, we handle these alike. They must refer to an register
1631 symbol, which is either relative to the register section and in
1632 the range 0..255, or is in the register contents section with vma
1633 regno * 8. */
1635 /* FIXME: A better way to check for reg contents section?
1636 FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1637 if (symsec == NULL)
1638 return bfd_reloc_undefined;
1640 if (strcmp (bfd_section_name (symsec),
1641 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1643 if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1645 /* The bfd_reloc_outofrange return value, though intuitively
1646 a better value, will not get us an error. */
1647 return bfd_reloc_overflow;
1649 srel /= 8;
1651 else if (strcmp (bfd_section_name (symsec),
1652 MMIX_REG_SECTION_NAME) == 0)
1654 if (srel < 0 || srel > 255)
1655 /* The bfd_reloc_outofrange return value, though intuitively a
1656 better value, will not get us an error. */
1657 return bfd_reloc_overflow;
1659 else
1661 /* Note: This is separated out into two messages in order
1662 to ease the translation into other languages. */
1663 if (symname == NULL || *symname == 0)
1664 _bfd_error_handler
1665 /* xgettext:c-format */
1666 (_("%pB: register relocation against non-register symbol:"
1667 " (unknown) in %pA"),
1668 input_section->owner, symsec);
1669 else
1670 _bfd_error_handler
1671 /* xgettext:c-format */
1672 (_("%pB: register relocation against non-register symbol:"
1673 " %s in %pA"),
1674 input_section->owner, symname, symsec);
1676 /* The bfd_reloc_outofrange return value, though intuitively a
1677 better value, will not get us an error. */
1678 return bfd_reloc_overflow;
1680 do_mmix_reloc:
1681 contents += r_offset;
1682 r = mmix_elf_perform_relocation (input_section, howto, contents,
1683 addr, srel, error_message);
1684 break;
1686 case R_MMIX_LOCAL:
1687 /* This isn't a real relocation, it's just an assertion that the
1688 final relocation value corresponds to a local register. We
1689 ignore the actual relocation; nothing is changed. */
1691 asection *regsec
1692 = bfd_get_section_by_name (input_section->output_section->owner,
1693 MMIX_REG_CONTENTS_SECTION_NAME);
1694 bfd_vma first_global;
1696 /* Check that this is an absolute value, or a reference to the
1697 register contents section or the register (symbol) section.
1698 Absolute numbers can get here as undefined section. Undefined
1699 symbols are signalled elsewhere, so there's no conflict in us
1700 accidentally handling it. */
1701 if (!bfd_is_abs_section (symsec)
1702 && !bfd_is_und_section (symsec)
1703 && strcmp (bfd_section_name (symsec),
1704 MMIX_REG_CONTENTS_SECTION_NAME) != 0
1705 && strcmp (bfd_section_name (symsec),
1706 MMIX_REG_SECTION_NAME) != 0)
1708 _bfd_error_handler
1709 (_("%pB: directive LOCAL valid only with a register or absolute value"),
1710 input_section->owner);
1712 return bfd_reloc_overflow;
1715 /* If we don't have a register contents section, then $255 is the
1716 first global register. */
1717 if (regsec == NULL)
1718 first_global = 255;
1719 else
1721 first_global = bfd_section_vma (regsec) / 8;
1722 if (strcmp (bfd_section_name (symsec),
1723 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1725 if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1726 /* The bfd_reloc_outofrange return value, though
1727 intuitively a better value, will not get us an error. */
1728 return bfd_reloc_overflow;
1729 srel /= 8;
1733 if ((bfd_vma) srel >= first_global)
1735 /* FIXME: Better error message. */
1736 _bfd_error_handler
1737 /* xgettext:c-format */
1738 (_("%pB: LOCAL directive: "
1739 "register $%" PRId64 " is not a local register;"
1740 " first global register is $%" PRId64),
1741 input_section->owner, (int64_t) srel, (int64_t) first_global);
1743 return bfd_reloc_overflow;
1746 r = bfd_reloc_ok;
1747 break;
1749 default:
1750 r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
1751 contents, r_offset,
1752 relocation, r_addend);
1755 return r;
1758 /* Return the section that should be marked against GC for a given
1759 relocation. */
1761 static asection *
1762 mmix_elf_gc_mark_hook (asection *sec,
1763 struct bfd_link_info *info,
1764 Elf_Internal_Rela *rel,
1765 struct elf_link_hash_entry *h,
1766 Elf_Internal_Sym *sym)
1768 if (h != NULL)
1769 switch (ELF64_R_TYPE (rel->r_info))
1771 case R_MMIX_GNU_VTINHERIT:
1772 case R_MMIX_GNU_VTENTRY:
1773 return NULL;
1776 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1779 /* Sort register relocs to come before expanding relocs. */
1781 static int
1782 mmix_elf_sort_relocs (const void * p1, const void * p2)
1784 const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
1785 const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
1786 int r1_is_reg, r2_is_reg;
1788 /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1789 insns. */
1790 if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
1791 return 1;
1792 else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
1793 return -1;
1795 r1_is_reg
1796 = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
1797 || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
1798 r2_is_reg
1799 = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
1800 || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
1801 if (r1_is_reg != r2_is_reg)
1802 return r2_is_reg - r1_is_reg;
1804 /* Neither or both are register relocs. Then sort on full offset. */
1805 if (r1->r_offset > r2->r_offset)
1806 return 1;
1807 else if (r1->r_offset < r2->r_offset)
1808 return -1;
1809 return 0;
1812 /* Subset of mmix_elf_check_relocs, common to ELF and mmo linking. */
1814 static bool
1815 mmix_elf_check_common_relocs (bfd *abfd,
1816 struct bfd_link_info *info,
1817 asection *sec,
1818 const Elf_Internal_Rela *relocs)
1820 bfd *bpo_greg_owner = NULL;
1821 asection *allocated_gregs_section = NULL;
1822 struct bpo_greg_section_info *gregdata = NULL;
1823 struct bpo_reloc_section_info *bpodata = NULL;
1824 const Elf_Internal_Rela *rel;
1825 const Elf_Internal_Rela *rel_end;
1827 /* We currently have to abuse this COFF-specific member, since there's
1828 no target-machine-dedicated member. There's no alternative outside
1829 the bfd_link_info struct; we can't specialize a hash-table since
1830 they're different between ELF and mmo. */
1831 bpo_greg_owner = (bfd *) info->base_file;
1833 rel_end = relocs + sec->reloc_count;
1834 for (rel = relocs; rel < rel_end; rel++)
1836 switch (ELF64_R_TYPE (rel->r_info))
1838 /* This relocation causes a GREG allocation. We need to count
1839 them, and we need to create a section for them, so we need an
1840 object to fake as the owner of that section. We can't use
1841 the ELF dynobj for this, since the ELF bits assume lots of
1842 DSO-related stuff if that member is non-NULL. */
1843 case R_MMIX_BASE_PLUS_OFFSET:
1844 /* We don't do anything with this reloc for a relocatable link. */
1845 if (bfd_link_relocatable (info))
1846 break;
1848 if (bpo_greg_owner == NULL)
1850 bpo_greg_owner = abfd;
1851 info->base_file = bpo_greg_owner;
1854 if (allocated_gregs_section == NULL)
1855 allocated_gregs_section
1856 = bfd_get_section_by_name (bpo_greg_owner,
1857 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
1859 if (allocated_gregs_section == NULL)
1861 allocated_gregs_section
1862 = bfd_make_section_with_flags (bpo_greg_owner,
1863 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME,
1864 (SEC_HAS_CONTENTS
1865 | SEC_IN_MEMORY
1866 | SEC_LINKER_CREATED));
1867 /* Setting both SEC_ALLOC and SEC_LOAD means the section is
1868 treated like any other section, and we'd get errors for
1869 address overlap with the text section. Let's set none of
1870 those flags, as that is what currently happens for usual
1871 GREG allocations, and that works. */
1872 if (allocated_gregs_section == NULL
1873 || !bfd_set_section_alignment (allocated_gregs_section, 3))
1874 return false;
1876 gregdata = (struct bpo_greg_section_info *)
1877 bfd_zalloc (bpo_greg_owner, sizeof (struct bpo_greg_section_info));
1878 if (gregdata == NULL)
1879 return false;
1880 mmix_elf_section_data (allocated_gregs_section)->bpo.greg
1881 = gregdata;
1883 else if (gregdata == NULL)
1884 gregdata
1885 = mmix_elf_section_data (allocated_gregs_section)->bpo.greg;
1887 /* Get ourselves some auxiliary info for the BPO-relocs. */
1888 if (bpodata == NULL)
1890 /* No use doing a separate iteration pass to find the upper
1891 limit - just use the number of relocs. */
1892 bpodata = (struct bpo_reloc_section_info *)
1893 bfd_alloc (bpo_greg_owner,
1894 sizeof (struct bpo_reloc_section_info)
1895 * (sec->reloc_count + 1));
1896 if (bpodata == NULL)
1897 return false;
1898 mmix_elf_section_data (sec)->bpo.reloc = bpodata;
1899 bpodata->first_base_plus_offset_reloc
1900 = bpodata->bpo_index
1901 = gregdata->n_max_bpo_relocs;
1902 bpodata->bpo_greg_section
1903 = allocated_gregs_section;
1904 bpodata->n_bpo_relocs_this_section = 0;
1907 bpodata->n_bpo_relocs_this_section++;
1908 gregdata->n_max_bpo_relocs++;
1910 /* We don't get another chance to set this before GC; we've not
1911 set up any hook that runs before GC. */
1912 gregdata->n_bpo_relocs
1913 = gregdata->n_max_bpo_relocs;
1914 break;
1916 case R_MMIX_PUSHJ_STUBBABLE:
1917 mmix_elf_section_data (sec)->pjs.n_pushj_relocs++;
1918 break;
1922 /* Allocate per-reloc stub storage and initialize it to the max stub
1923 size. */
1924 if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs != 0)
1926 size_t i;
1928 mmix_elf_section_data (sec)->pjs.stub_size
1929 = bfd_alloc (abfd, mmix_elf_section_data (sec)->pjs.n_pushj_relocs
1930 * sizeof (mmix_elf_section_data (sec)
1931 ->pjs.stub_size[0]));
1932 if (mmix_elf_section_data (sec)->pjs.stub_size == NULL)
1933 return false;
1935 for (i = 0; i < mmix_elf_section_data (sec)->pjs.n_pushj_relocs; i++)
1936 mmix_elf_section_data (sec)->pjs.stub_size[i] = MAX_PUSHJ_STUB_SIZE;
1939 return true;
1942 /* Look through the relocs for a section during the first phase. */
1944 static bool
1945 mmix_elf_check_relocs (bfd *abfd,
1946 struct bfd_link_info *info,
1947 asection *sec,
1948 const Elf_Internal_Rela *relocs)
1950 Elf_Internal_Shdr *symtab_hdr;
1951 struct elf_link_hash_entry **sym_hashes;
1952 const Elf_Internal_Rela *rel;
1953 const Elf_Internal_Rela *rel_end;
1955 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1956 sym_hashes = elf_sym_hashes (abfd);
1958 /* First we sort the relocs so that any register relocs come before
1959 expansion-relocs to the same insn. FIXME: Not done for mmo. */
1960 qsort ((void *) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1961 mmix_elf_sort_relocs);
1963 /* Do the common part. */
1964 if (!mmix_elf_check_common_relocs (abfd, info, sec, relocs))
1965 return false;
1967 if (bfd_link_relocatable (info))
1968 return true;
1970 rel_end = relocs + sec->reloc_count;
1971 for (rel = relocs; rel < rel_end; rel++)
1973 struct elf_link_hash_entry *h;
1974 unsigned long r_symndx;
1976 r_symndx = ELF64_R_SYM (rel->r_info);
1977 if (r_symndx < symtab_hdr->sh_info)
1978 h = NULL;
1979 else
1981 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1982 while (h->root.type == bfd_link_hash_indirect
1983 || h->root.type == bfd_link_hash_warning)
1984 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1987 switch (ELF64_R_TYPE (rel->r_info))
1989 /* This relocation describes the C++ object vtable hierarchy.
1990 Reconstruct it for later use during GC. */
1991 case R_MMIX_GNU_VTINHERIT:
1992 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1993 return false;
1994 break;
1996 /* This relocation describes which C++ vtable entries are actually
1997 used. Record for later use during GC. */
1998 case R_MMIX_GNU_VTENTRY:
1999 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2000 return false;
2001 break;
2005 return true;
2008 /* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
2009 Copied from elf_link_add_object_symbols. */
2011 bool
2012 _bfd_mmix_check_all_relocs (bfd *abfd, struct bfd_link_info *info)
2014 asection *o;
2016 for (o = abfd->sections; o != NULL; o = o->next)
2018 Elf_Internal_Rela *internal_relocs;
2019 bool ok;
2021 if ((o->flags & SEC_RELOC) == 0
2022 || o->reloc_count == 0
2023 || ((info->strip == strip_all || info->strip == strip_debugger)
2024 && (o->flags & SEC_DEBUGGING) != 0)
2025 || bfd_is_abs_section (o->output_section))
2026 continue;
2028 internal_relocs
2029 = _bfd_elf_link_read_relocs (abfd, o, NULL,
2030 (Elf_Internal_Rela *) NULL,
2031 info->keep_memory);
2032 if (internal_relocs == NULL)
2033 return false;
2035 ok = mmix_elf_check_common_relocs (abfd, info, o, internal_relocs);
2037 if (! info->keep_memory)
2038 free (internal_relocs);
2040 if (! ok)
2041 return false;
2044 return true;
2047 /* Change symbols relative to the reg contents section to instead be to
2048 the register section, and scale them down to correspond to the register
2049 number. */
2051 static int
2052 mmix_elf_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
2053 const char *name ATTRIBUTE_UNUSED,
2054 Elf_Internal_Sym *sym,
2055 asection *input_sec,
2056 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
2058 if (input_sec != NULL
2059 && input_sec->name != NULL
2060 && ELF_ST_TYPE (sym->st_info) != STT_SECTION
2061 && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2063 sym->st_value /= 8;
2064 sym->st_shndx = SHN_REGISTER;
2067 return 1;
2070 /* We fake a register section that holds values that are register numbers.
2071 Having a SHN_REGISTER and register section translates better to other
2072 formats (e.g. mmo) than for example a STT_REGISTER attribute.
2073 This section faking is based on a construct in elf32-mips.c. */
2074 static asection mmix_elf_reg_section;
2075 static const asymbol mmix_elf_reg_section_symbol =
2076 GLOBAL_SYM_INIT (MMIX_REG_SECTION_NAME, &mmix_elf_reg_section);
2077 static asection mmix_elf_reg_section =
2078 BFD_FAKE_SECTION (mmix_elf_reg_section, &mmix_elf_reg_section_symbol,
2079 MMIX_REG_SECTION_NAME, 0, SEC_NO_FLAGS);
2081 /* Handle the special section numbers that a symbol may use. */
2083 void
2084 mmix_elf_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
2086 elf_symbol_type *elfsym;
2088 elfsym = (elf_symbol_type *) asym;
2089 switch (elfsym->internal_elf_sym.st_shndx)
2091 case SHN_REGISTER:
2092 asym->section = &mmix_elf_reg_section;
2093 break;
2095 default:
2096 break;
2100 /* Given a BFD section, try to locate the corresponding ELF section
2101 index. */
2103 static bool
2104 mmix_elf_section_from_bfd_section (bfd * abfd ATTRIBUTE_UNUSED,
2105 asection * sec,
2106 int * retval)
2108 if (strcmp (bfd_section_name (sec), MMIX_REG_SECTION_NAME) == 0)
2109 *retval = SHN_REGISTER;
2110 else
2111 return false;
2113 return true;
2116 /* Hook called by the linker routine which adds symbols from an object
2117 file. We must handle the special SHN_REGISTER section number here.
2119 We also check that we only have *one* each of the section-start
2120 symbols, since otherwise having two with the same value would cause
2121 them to be "merged", but with the contents serialized. */
2123 static bool
2124 mmix_elf_add_symbol_hook (bfd *abfd,
2125 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2126 Elf_Internal_Sym *sym,
2127 const char **namep ATTRIBUTE_UNUSED,
2128 flagword *flagsp ATTRIBUTE_UNUSED,
2129 asection **secp,
2130 bfd_vma *valp ATTRIBUTE_UNUSED)
2132 if (sym->st_shndx == SHN_REGISTER)
2134 *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2135 (*secp)->flags |= SEC_LINKER_CREATED;
2137 else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
2138 && startswith (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX))
2140 /* See if we have another one. */
2141 struct bfd_link_hash_entry *h = bfd_link_hash_lookup (info->hash,
2142 *namep,
2143 false,
2144 false,
2145 false);
2147 if (h != NULL && h->type != bfd_link_hash_undefined)
2149 /* How do we get the asymbol (or really: the filename) from h?
2150 h->u.def.section->owner is NULL. */
2151 _bfd_error_handler
2152 /* xgettext:c-format */
2153 (_("%pB: error: multiple definition of `%s'; start of %s "
2154 "is set in a earlier linked file"),
2155 abfd, *namep,
2156 *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX));
2157 bfd_set_error (bfd_error_bad_value);
2158 return false;
2162 return true;
2165 /* We consider symbols matching "L.*:[0-9]+" to be local symbols. */
2167 static bool
2168 mmix_elf_is_local_label_name (bfd *abfd, const char *name)
2170 const char *colpos;
2171 int digits;
2173 /* Also include the default local-label definition. */
2174 if (_bfd_elf_is_local_label_name (abfd, name))
2175 return true;
2177 if (*name != 'L')
2178 return false;
2180 /* If there's no ":", or more than one, it's not a local symbol. */
2181 colpos = strchr (name, ':');
2182 if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
2183 return false;
2185 /* Check that there are remaining characters and that they are digits. */
2186 if (colpos[1] == 0)
2187 return false;
2189 digits = strspn (colpos + 1, "0123456789");
2190 return digits != 0 && colpos[1 + digits] == 0;
2193 /* We get rid of the register section here. */
2195 bool
2196 mmix_elf_final_link (bfd *abfd, struct bfd_link_info *info)
2198 /* We never output a register section, though we create one for
2199 temporary measures. Check that nobody entered contents into it. */
2200 asection *reg_section;
2202 reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
2204 if (reg_section != NULL)
2206 /* FIXME: Pass error state gracefully. */
2207 if (bfd_section_flags (reg_section) & SEC_HAS_CONTENTS)
2208 _bfd_abort (__FILE__, __LINE__, _("register section has contents\n"));
2210 /* Really remove the section, if it hasn't already been done. */
2211 if (!bfd_section_removed_from_list (abfd, reg_section))
2213 bfd_section_list_remove (abfd, reg_section);
2214 --abfd->section_count;
2218 if (! bfd_elf_final_link (abfd, info))
2219 return false;
2221 /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
2222 the regular linker machinery. We do it here, like other targets with
2223 special sections. */
2224 if (info->base_file != NULL)
2226 asection *greg_section
2227 = bfd_get_section_by_name ((bfd *) info->base_file,
2228 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2229 if (!bfd_set_section_contents (abfd,
2230 greg_section->output_section,
2231 greg_section->contents,
2232 (file_ptr) greg_section->output_offset,
2233 greg_section->size))
2234 return false;
2236 return true;
2239 /* We need to include the maximum size of PUSHJ-stubs in the initial
2240 section size. This is expected to shrink during linker relaxation. */
2242 static void
2243 mmix_set_relaxable_size (bfd *abfd ATTRIBUTE_UNUSED,
2244 asection *sec,
2245 void *ptr)
2247 struct bfd_link_info *info = ptr;
2249 /* Make sure we only do this for section where we know we want this,
2250 otherwise we might end up resetting the size of COMMONs. */
2251 if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0)
2252 return;
2254 sec->rawsize = sec->size;
2255 sec->size += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2256 * MAX_PUSHJ_STUB_SIZE);
2258 /* For use in relocatable link, we start with a max stubs size. See
2259 mmix_elf_relax_section. */
2260 if (bfd_link_relocatable (info) && sec->output_section)
2261 mmix_elf_section_data (sec->output_section)->pjs.stubs_size_sum
2262 += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2263 * MAX_PUSHJ_STUB_SIZE);
2266 /* Initialize stuff for the linker-generated GREGs to match
2267 R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker. */
2269 bool
2270 _bfd_mmix_before_linker_allocation (bfd *abfd ATTRIBUTE_UNUSED,
2271 struct bfd_link_info *info)
2273 asection *bpo_gregs_section;
2274 bfd *bpo_greg_owner;
2275 struct bpo_greg_section_info *gregdata;
2276 size_t n_gregs;
2277 bfd_vma gregs_size;
2278 size_t i;
2279 size_t *bpo_reloc_indexes;
2280 bfd *ibfd;
2282 /* Set the initial size of sections. */
2283 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2284 bfd_map_over_sections (ibfd, mmix_set_relaxable_size, info);
2286 /* The bpo_greg_owner bfd is supposed to have been set by
2287 mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
2288 If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2289 bpo_greg_owner = (bfd *) info->base_file;
2290 if (bpo_greg_owner == NULL)
2291 return true;
2293 bpo_gregs_section
2294 = bfd_get_section_by_name (bpo_greg_owner,
2295 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2297 if (bpo_gregs_section == NULL)
2298 return true;
2300 /* We use the target-data handle in the ELF section data. */
2301 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2302 if (gregdata == NULL)
2303 return false;
2305 n_gregs = gregdata->n_bpo_relocs;
2306 gregdata->n_allocated_bpo_gregs = n_gregs;
2308 /* When this reaches zero during relaxation, all entries have been
2309 filled in and the size of the linker gregs can be calculated. */
2310 gregdata->n_remaining_bpo_relocs_this_relaxation_round = n_gregs;
2312 /* Set the zeroth-order estimate for the GREGs size. */
2313 gregs_size = n_gregs * 8;
2315 if (!bfd_set_section_size (bpo_gregs_section, gregs_size))
2316 return false;
2318 /* Allocate and set up the GREG arrays. They're filled in at relaxation
2319 time. Note that we must use the max number ever noted for the array,
2320 since the index numbers were created before GC. */
2321 gregdata->reloc_request
2322 = bfd_zalloc (bpo_greg_owner,
2323 sizeof (struct bpo_reloc_request)
2324 * gregdata->n_max_bpo_relocs);
2326 gregdata->bpo_reloc_indexes
2327 = bpo_reloc_indexes
2328 = bfd_alloc (bpo_greg_owner,
2329 gregdata->n_max_bpo_relocs
2330 * sizeof (size_t));
2331 if (bpo_reloc_indexes == NULL)
2332 return false;
2334 /* The default order is an identity mapping. */
2335 for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2337 bpo_reloc_indexes[i] = i;
2338 gregdata->reloc_request[i].bpo_reloc_no = i;
2341 return true;
2344 /* Fill in contents in the linker allocated gregs. Everything is
2345 calculated at this point; we just move the contents into place here. */
2347 bool
2348 _bfd_mmix_after_linker_allocation (bfd *abfd ATTRIBUTE_UNUSED,
2349 struct bfd_link_info *link_info)
2351 asection *bpo_gregs_section;
2352 bfd *bpo_greg_owner;
2353 struct bpo_greg_section_info *gregdata;
2354 size_t n_gregs;
2355 size_t i, j;
2356 size_t lastreg;
2357 bfd_byte *contents;
2359 /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
2360 when the first R_MMIX_BASE_PLUS_OFFSET is seen. If there is no such
2361 object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2362 bpo_greg_owner = (bfd *) link_info->base_file;
2363 if (bpo_greg_owner == NULL)
2364 return true;
2366 bpo_gregs_section
2367 = bfd_get_section_by_name (bpo_greg_owner,
2368 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2370 /* This can't happen without DSO handling. When DSOs are handled
2371 without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
2372 section. */
2373 if (bpo_gregs_section == NULL)
2374 return true;
2376 /* We use the target-data handle in the ELF section data. */
2378 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2379 if (gregdata == NULL)
2380 return false;
2382 n_gregs = gregdata->n_allocated_bpo_gregs;
2384 bpo_gregs_section->contents
2385 = contents = bfd_alloc (bpo_greg_owner, bpo_gregs_section->size);
2386 if (contents == NULL)
2387 return false;
2388 bpo_gregs_section->alloced = 1;
2390 /* Sanity check: If these numbers mismatch, some relocation has not been
2391 accounted for and the rest of gregdata is probably inconsistent.
2392 It's a bug, but it's more helpful to identify it than segfaulting
2393 below. */
2394 if (gregdata->n_remaining_bpo_relocs_this_relaxation_round
2395 != gregdata->n_bpo_relocs)
2397 _bfd_error_handler
2398 /* xgettext:c-format */
2399 (_("internal inconsistency: remaining %lu != max %lu;"
2400 " please report this bug"),
2401 (unsigned long) gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2402 (unsigned long) gregdata->n_bpo_relocs);
2403 return false;
2406 for (lastreg = 255, i = 0, j = 0; j < n_gregs; i++)
2407 if (gregdata->reloc_request[i].regindex != lastreg)
2409 bfd_put_64 (bpo_greg_owner, gregdata->reloc_request[i].value,
2410 contents + j * 8);
2411 lastreg = gregdata->reloc_request[i].regindex;
2412 j++;
2415 return true;
2418 /* Sort valid relocs to come before non-valid relocs, then on increasing
2419 value. */
2421 static int
2422 bpo_reloc_request_sort_fn (const void * p1, const void * p2)
2424 const struct bpo_reloc_request *r1 = (const struct bpo_reloc_request *) p1;
2425 const struct bpo_reloc_request *r2 = (const struct bpo_reloc_request *) p2;
2427 /* Primary function is validity; non-valid relocs sorted after valid
2428 ones. */
2429 if (r1->valid != r2->valid)
2430 return r2->valid - r1->valid;
2432 /* Then sort on value. Don't simplify and return just the difference of
2433 the values: the upper bits of the 64-bit value would be truncated on
2434 a host with 32-bit ints. */
2435 if (r1->value != r2->value)
2436 return r1->value > r2->value ? 1 : -1;
2438 /* As a last re-sort, use the relocation number, so we get a stable
2439 sort. The *addresses* aren't stable since items are swapped during
2440 sorting. It depends on the qsort implementation if this actually
2441 happens. */
2442 return r1->bpo_reloc_no > r2->bpo_reloc_no
2443 ? 1 : (r1->bpo_reloc_no < r2->bpo_reloc_no ? -1 : 0);
2446 /* For debug use only. Dumps the global register allocations resulting
2447 from base-plus-offset relocs. */
2449 void
2450 mmix_dump_bpo_gregs (struct bfd_link_info *link_info,
2451 void (*pf) (const char *fmt, ...))
2453 bfd *bpo_greg_owner;
2454 asection *bpo_gregs_section;
2455 struct bpo_greg_section_info *gregdata;
2456 unsigned int i;
2458 if (link_info == NULL || link_info->base_file == NULL)
2459 return;
2461 bpo_greg_owner = (bfd *) link_info->base_file;
2463 bpo_gregs_section
2464 = bfd_get_section_by_name (bpo_greg_owner,
2465 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2467 if (bpo_gregs_section == NULL)
2468 return;
2470 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2471 if (gregdata == NULL)
2472 return;
2474 if (pf == NULL)
2475 pf = _bfd_error_handler;
2477 /* These format strings are not translated. They are for debug purposes
2478 only and never displayed to an end user. Should they escape, we
2479 surely want them in original. */
2480 (*pf) (" n_bpo_relocs: %u\n n_max_bpo_relocs: %u\n n_remain...round: %u\n\
2481 n_allocated_bpo_gregs: %u\n", gregdata->n_bpo_relocs,
2482 gregdata->n_max_bpo_relocs,
2483 gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2484 gregdata->n_allocated_bpo_gregs);
2486 if (gregdata->reloc_request)
2487 for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2488 (*pf) ("%4u (%4u)/%4u#%u: 0x%08lx%08lx r: %3u o: %3u\n",
2490 (gregdata->bpo_reloc_indexes != NULL
2491 ? gregdata->bpo_reloc_indexes[i] : (size_t) -1),
2492 gregdata->reloc_request[i].bpo_reloc_no,
2493 gregdata->reloc_request[i].valid,
2495 (unsigned long) (gregdata->reloc_request[i].value >> 32),
2496 (unsigned long) gregdata->reloc_request[i].value,
2497 gregdata->reloc_request[i].regindex,
2498 gregdata->reloc_request[i].offset);
2501 /* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
2502 when the last such reloc is done, an index-array is sorted according to
2503 the values and iterated over to produce register numbers (indexed by 0
2504 from the first allocated register number) and offsets for use in real
2505 relocation. (N.B.: Relocatable runs are handled, not just punted.)
2507 PUSHJ stub accounting is also done here.
2509 Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
2511 static bool
2512 mmix_elf_relax_section (bfd *abfd,
2513 asection *sec,
2514 struct bfd_link_info *link_info,
2515 bool *again)
2517 Elf_Internal_Shdr *symtab_hdr;
2518 Elf_Internal_Rela *internal_relocs;
2519 Elf_Internal_Rela *irel, *irelend;
2520 asection *bpo_gregs_section = NULL;
2521 struct bpo_greg_section_info *gregdata;
2522 struct bpo_reloc_section_info *bpodata
2523 = mmix_elf_section_data (sec)->bpo.reloc;
2524 /* The initialization is to quiet compiler warnings. The value is to
2525 spot a missing actual initialization. */
2526 size_t bpono = (size_t) -1;
2527 size_t pjsno = 0;
2528 size_t pjsno_undefs = 0;
2529 Elf_Internal_Sym *isymbuf = NULL;
2530 bfd_size_type size = sec->rawsize ? sec->rawsize : sec->size;
2532 mmix_elf_section_data (sec)->pjs.stubs_size_sum = 0;
2534 /* Assume nothing changes. */
2535 *again = false;
2537 /* We don't have to do anything if this section does not have relocs, or
2538 if this is not a code section. */
2539 if ((sec->flags & SEC_RELOC) == 0
2540 || sec->reloc_count == 0
2541 || (sec->flags & SEC_CODE) == 0
2542 || (sec->flags & SEC_LINKER_CREATED) != 0
2543 /* If no R_MMIX_BASE_PLUS_OFFSET relocs and no PUSHJ-stub relocs,
2544 then nothing to do. */
2545 || (bpodata == NULL
2546 && mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0))
2547 return true;
2549 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2551 if (bpodata != NULL)
2553 bpo_gregs_section = bpodata->bpo_greg_section;
2554 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2555 bpono = bpodata->first_base_plus_offset_reloc;
2557 else
2558 gregdata = NULL;
2560 /* Get a copy of the native relocations. */
2561 internal_relocs
2562 = _bfd_elf_link_read_relocs (abfd, sec, NULL,
2563 (Elf_Internal_Rela *) NULL,
2564 link_info->keep_memory);
2565 if (internal_relocs == NULL)
2566 goto error_return;
2568 /* Walk through them looking for relaxing opportunities. */
2569 irelend = internal_relocs + sec->reloc_count;
2570 for (irel = internal_relocs; irel < irelend; irel++)
2572 bfd_vma symval;
2573 struct elf_link_hash_entry *h = NULL;
2575 /* We only process two relocs. */
2576 if (ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_BASE_PLUS_OFFSET
2577 && ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_PUSHJ_STUBBABLE)
2578 continue;
2580 /* We process relocs in a distinctly different way when this is a
2581 relocatable link (for one, we don't look at symbols), so we avoid
2582 mixing its code with that for the "normal" relaxation. */
2583 if (bfd_link_relocatable (link_info))
2585 /* The only transformation in a relocatable link is to generate
2586 a full stub at the location of the stub calculated for the
2587 input section, if the relocated stub location, the end of the
2588 output section plus earlier stubs, cannot be reached. Thus
2589 relocatable linking can only lead to worse code, but it still
2590 works. */
2591 if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
2593 /* If we can reach the end of the output-section and beyond
2594 any current stubs, then we don't need a stub for this
2595 reloc. The relaxed order of output stub allocation may
2596 not exactly match the straightforward order, so we always
2597 assume presence of output stubs, which will allow
2598 relaxation only on relocations indifferent to the
2599 presence of output stub allocations for other relocations
2600 and thus the order of output stub allocation. */
2601 if (bfd_check_overflow (complain_overflow_signed,
2604 bfd_arch_bits_per_address (abfd),
2605 /* Output-stub location. */
2606 sec->output_section->rawsize
2607 + (mmix_elf_section_data (sec
2608 ->output_section)
2609 ->pjs.stubs_size_sum)
2610 /* Location of this PUSHJ reloc. */
2611 - (sec->output_offset + irel->r_offset)
2612 /* Don't count *this* stub twice. */
2613 - (mmix_elf_section_data (sec)
2614 ->pjs.stub_size[pjsno]
2615 + MAX_PUSHJ_STUB_SIZE))
2616 == bfd_reloc_ok)
2617 mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2619 mmix_elf_section_data (sec)->pjs.stubs_size_sum
2620 += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2622 pjsno++;
2625 continue;
2628 /* Get the value of the symbol referred to by the reloc. */
2629 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2631 /* A local symbol. */
2632 Elf_Internal_Sym *isym;
2633 asection *sym_sec;
2635 /* Read this BFD's local symbols if we haven't already. */
2636 if (isymbuf == NULL)
2638 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2639 if (isymbuf == NULL)
2640 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2641 symtab_hdr->sh_info, 0,
2642 NULL, NULL, NULL);
2643 if (isymbuf == 0)
2644 goto error_return;
2647 isym = isymbuf + ELF64_R_SYM (irel->r_info);
2648 if (isym->st_shndx == SHN_UNDEF)
2649 sym_sec = bfd_und_section_ptr;
2650 else if (isym->st_shndx == SHN_ABS)
2651 sym_sec = bfd_abs_section_ptr;
2652 else if (isym->st_shndx == SHN_COMMON)
2653 sym_sec = bfd_com_section_ptr;
2654 else
2655 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2656 symval = (isym->st_value
2657 + sym_sec->output_section->vma
2658 + sym_sec->output_offset);
2660 else
2662 unsigned long indx;
2664 /* An external symbol. */
2665 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2666 h = elf_sym_hashes (abfd)[indx];
2667 BFD_ASSERT (h != NULL);
2668 if (h->root.type == bfd_link_hash_undefweak)
2669 /* FIXME: for R_MMIX_PUSHJ_STUBBABLE, there are alternatives to
2670 the canonical value 0 for an unresolved weak symbol to
2671 consider: as the debug-friendly approach, resolve to "abort"
2672 (or a port-specific function), or as the space-friendly
2673 approach resolve to the next instruction (like some other
2674 ports, notably ARM and AArch64). These alternatives require
2675 matching code in mmix_elf_perform_relocation or its caller. */
2676 symval = 0;
2677 else if (h->root.type == bfd_link_hash_defined
2678 || h->root.type == bfd_link_hash_defweak)
2679 symval = (h->root.u.def.value
2680 + h->root.u.def.section->output_section->vma
2681 + h->root.u.def.section->output_offset);
2682 else
2684 /* This appears to be a reference to an undefined symbol. Just
2685 ignore it--it will be caught by the regular reloc processing.
2686 We need to keep BPO reloc accounting consistent, though
2687 else we'll abort instead of emitting an error message. */
2688 if (ELF64_R_TYPE (irel->r_info) == R_MMIX_BASE_PLUS_OFFSET
2689 && gregdata != NULL)
2691 gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2692 bpono++;
2695 /* Similarly, keep accounting consistent for PUSHJ
2696 referring to an undefined symbol. */
2697 if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
2698 pjsno_undefs++;
2699 continue;
2703 if (ELF64_R_TYPE (irel->r_info) == (int) R_MMIX_PUSHJ_STUBBABLE)
2705 bfd_vma value = symval + irel->r_addend;
2706 bfd_vma dot
2707 = (sec->output_section->vma
2708 + sec->output_offset
2709 + irel->r_offset);
2710 bfd_vma stubaddr
2711 = (sec->output_section->vma
2712 + sec->output_offset
2713 + size
2714 + mmix_elf_section_data (sec)->pjs.stubs_size_sum);
2716 if ((value & 3) == 0
2717 && bfd_check_overflow (complain_overflow_signed,
2720 bfd_arch_bits_per_address (abfd),
2721 value - dot
2722 - (value > dot
2723 ? mmix_elf_section_data (sec)
2724 ->pjs.stub_size[pjsno]
2725 : 0))
2726 == bfd_reloc_ok)
2727 /* If the reloc fits, no stub is needed. */
2728 mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2729 else
2730 /* Maybe we can get away with just a JMP insn? */
2731 if ((value & 3) == 0
2732 && bfd_check_overflow (complain_overflow_signed,
2735 bfd_arch_bits_per_address (abfd),
2736 value - stubaddr
2737 - (value > dot
2738 ? mmix_elf_section_data (sec)
2739 ->pjs.stub_size[pjsno] - 4
2740 : 0))
2741 == bfd_reloc_ok)
2742 /* Yep, account for a stub consisting of a single JMP insn. */
2743 mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 4;
2744 else
2745 /* Nope, go for the full insn stub. It doesn't seem useful to
2746 emit the intermediate sizes; those will only be useful for
2747 a >64M program assuming contiguous code. */
2748 mmix_elf_section_data (sec)->pjs.stub_size[pjsno]
2749 = MAX_PUSHJ_STUB_SIZE;
2751 mmix_elf_section_data (sec)->pjs.stubs_size_sum
2752 += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2753 pjsno++;
2754 continue;
2757 /* We're looking at a R_MMIX_BASE_PLUS_OFFSET reloc. */
2759 gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono]].value
2760 = symval + irel->r_addend;
2761 gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono++]].valid = true;
2762 gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2765 /* Check if that was the last BPO-reloc. If so, sort the values and
2766 calculate how many registers we need to cover them. Set the size of
2767 the linker gregs, and if the number of registers changed, indicate
2768 that we need to relax some more because we have more work to do. */
2769 if (gregdata != NULL
2770 && gregdata->n_remaining_bpo_relocs_this_relaxation_round == 0)
2772 size_t i;
2773 bfd_vma prev_base;
2774 size_t regindex;
2776 /* First, reset the remaining relocs for the next round. */
2777 gregdata->n_remaining_bpo_relocs_this_relaxation_round
2778 = gregdata->n_bpo_relocs;
2780 qsort (gregdata->reloc_request,
2781 gregdata->n_max_bpo_relocs,
2782 sizeof (struct bpo_reloc_request),
2783 bpo_reloc_request_sort_fn);
2785 /* Recalculate indexes. When we find a change (however unlikely
2786 after the initial iteration), we know we need to relax again,
2787 since items in the GREG-array are sorted by increasing value and
2788 stored in the relaxation phase. */
2789 for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2790 if (gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2791 != i)
2793 gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2794 = i;
2795 *again = true;
2798 /* Allocate register numbers (indexing from 0). Stop at the first
2799 non-valid reloc. */
2800 for (i = 0, regindex = 0, prev_base = gregdata->reloc_request[0].value;
2801 i < gregdata->n_bpo_relocs;
2802 i++)
2804 if (gregdata->reloc_request[i].value > prev_base + 255)
2806 regindex++;
2807 prev_base = gregdata->reloc_request[i].value;
2809 gregdata->reloc_request[i].regindex = regindex;
2810 gregdata->reloc_request[i].offset
2811 = gregdata->reloc_request[i].value - prev_base;
2814 /* If it's not the same as the last time, we need to relax again,
2815 because the size of the section has changed. I'm not sure we
2816 actually need to do any adjustments since the shrinking happens
2817 at the start of this section, but better safe than sorry. */
2818 if (gregdata->n_allocated_bpo_gregs != regindex + 1)
2820 gregdata->n_allocated_bpo_gregs = regindex + 1;
2821 *again = true;
2824 bpo_gregs_section->size = (regindex + 1) * 8;
2827 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
2829 if (! link_info->keep_memory)
2830 free (isymbuf);
2831 else
2833 /* Cache the symbols for elf_link_input_bfd. */
2834 symtab_hdr->contents = (unsigned char *) isymbuf;
2838 BFD_ASSERT(pjsno + pjsno_undefs
2839 == mmix_elf_section_data (sec)->pjs.n_pushj_relocs);
2841 if (elf_section_data (sec)->relocs != internal_relocs)
2842 free (internal_relocs);
2844 if (sec->size < size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
2845 abort ();
2847 if (sec->size > size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
2849 sec->size = size + mmix_elf_section_data (sec)->pjs.stubs_size_sum;
2850 *again = true;
2853 return true;
2855 error_return:
2856 if ((unsigned char *) isymbuf != symtab_hdr->contents)
2857 free (isymbuf);
2858 if (elf_section_data (sec)->relocs != internal_relocs)
2859 free (internal_relocs);
2860 return false;
2863 #define ELF_ARCH bfd_arch_mmix
2864 #define ELF_MACHINE_CODE EM_MMIX
2865 #define ELF_TARGET_ID MMIX_ELF_DATA
2867 /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
2868 However, that's too much for something somewhere in the linker part of
2869 BFD; perhaps the start-address has to be a non-zero multiple of this
2870 number, or larger than this number. The symptom is that the linker
2871 complains: "warning: allocated section `.text' not in segment". We
2872 settle for 64k; the page-size used in examples is 8k.
2873 #define ELF_MAXPAGESIZE 0x10000
2875 Unfortunately, this causes excessive padding in the supposedly small
2876 for-education programs that are the expected usage (where people would
2877 inspect output). We stick to 256 bytes just to have *some* default
2878 alignment. */
2879 #define ELF_MAXPAGESIZE 0x100
2881 #define TARGET_BIG_SYM mmix_elf64_vec
2882 #define TARGET_BIG_NAME "elf64-mmix"
2884 #define elf_info_to_howto_rel NULL
2885 #define elf_info_to_howto mmix_info_to_howto_rela
2886 #define elf_backend_relocate_section mmix_elf_relocate_section
2887 #define elf_backend_gc_mark_hook mmix_elf_gc_mark_hook
2889 #define elf_backend_link_output_symbol_hook \
2890 mmix_elf_link_output_symbol_hook
2891 #define elf_backend_add_symbol_hook mmix_elf_add_symbol_hook
2893 #define elf_backend_check_relocs mmix_elf_check_relocs
2894 #define elf_backend_symbol_processing mmix_elf_symbol_processing
2895 #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
2897 #define bfd_elf64_bfd_copy_link_hash_symbol_type \
2898 _bfd_generic_copy_link_hash_symbol_type
2900 #define bfd_elf64_bfd_is_local_label_name \
2901 mmix_elf_is_local_label_name
2903 #define elf_backend_may_use_rel_p 0
2904 #define elf_backend_may_use_rela_p 1
2905 #define elf_backend_default_use_rela_p 1
2907 #define elf_backend_can_gc_sections 1
2908 #define elf_backend_section_from_bfd_section \
2909 mmix_elf_section_from_bfd_section
2911 #define bfd_elf64_new_section_hook mmix_elf_new_section_hook
2912 #define bfd_elf64_bfd_final_link mmix_elf_final_link
2913 #define bfd_elf64_bfd_relax_section mmix_elf_relax_section
2915 #include "elf64-target.h"