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