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