2004-04-21 Andrew Cagney <cagney@redhat.com>
[binutils.git] / bfd / elf64-mmix.c
blob4b78681faecae4fe11e4a710977b85cc49c8884a
1 /* MMIX-specific support for 64-bit ELF.
2 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Hans-Peter Nilsson <hp@bitrange.com>
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* No specific ABI or "processor-specific supplement" defined. */
23 /* TODO:
24 - "Traditional" linker relaxation (shrinking whole sections).
25 - Merge reloc stubs jumping to same location.
26 - GETA stub relaxation (call a stub for out of range new
27 R_MMIX_GETA_STUBBABLE). */
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/mmix.h"
34 #include "opcode/mmix.h"
36 #define MINUS_ONE (((bfd_vma) 0) - 1)
38 #define MAX_PUSHJ_STUB_SIZE (5 * 4)
40 /* Put these everywhere in new code. */
41 #define FATAL_DEBUG \
42 _bfd_abort (__FILE__, __LINE__, \
43 "Internal: Non-debugged code (test-case missing)")
45 #define BAD_CASE(x) \
46 _bfd_abort (__FILE__, __LINE__, \
47 "bad case for " #x)
49 struct _mmix_elf_section_data
51 struct bfd_elf_section_data elf;
52 union
54 struct bpo_reloc_section_info *reloc;
55 struct bpo_greg_section_info *greg;
56 } bpo;
58 struct pushj_stub_info
60 /* Maximum number of stubs needed for this section. */
61 bfd_size_type n_pushj_relocs;
63 /* Size of stubs after a mmix_elf_relax_section round. */
64 bfd_size_type stubs_size_sum;
66 /* Per-reloc stubs_size_sum information. The stubs_size_sum member is the sum
67 of these. Allocated in mmix_elf_check_common_relocs. */
68 bfd_size_type *stub_size;
70 /* Offset of next stub during relocation. Somewhat redundant with the
71 above: error coverage is easier and we don't have to reset the
72 stubs_size_sum for relocation. */
73 bfd_size_type stub_offset;
74 } pjs;
77 #define mmix_elf_section_data(sec) \
78 ((struct _mmix_elf_section_data *) elf_section_data (sec))
80 /* For each section containing a base-plus-offset (BPO) reloc, we attach
81 this struct as mmix_elf_section_data (section)->bpo, which is otherwise
82 NULL. */
83 struct bpo_reloc_section_info
85 /* The base is 1; this is the first number in this section. */
86 size_t first_base_plus_offset_reloc;
88 /* Number of BPO-relocs in this section. */
89 size_t n_bpo_relocs_this_section;
91 /* Running index, used at relocation time. */
92 size_t bpo_index;
94 /* We don't have access to the bfd_link_info struct in
95 mmix_final_link_relocate. What we really want to get at is the
96 global single struct greg_relocation, so we stash it here. */
97 asection *bpo_greg_section;
100 /* Helper struct (in global context) for the one below.
101 There's one of these created for every BPO reloc. */
102 struct bpo_reloc_request
104 bfd_vma value;
106 /* Valid after relaxation. The base is 0; the first register number
107 must be added. The offset is in range 0..255. */
108 size_t regindex;
109 size_t offset;
111 /* The order number for this BPO reloc, corresponding to the order in
112 which BPO relocs were found. Used to create an index after reloc
113 requests are sorted. */
114 size_t bpo_reloc_no;
116 /* Set when the value is computed. Better than coding "guard values"
117 into the other members. Is FALSE only for BPO relocs in a GC:ed
118 section. */
119 bfd_boolean valid;
122 /* We attach this as mmix_elf_section_data (sec)->bpo in the linker-allocated
123 greg contents section (MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME),
124 which is linked into the register contents section
125 (MMIX_REG_CONTENTS_SECTION_NAME). This section is created by the
126 linker; using the same hook as for usual with BPO relocs does not
127 collide. */
128 struct bpo_greg_section_info
130 /* After GC, this reflects the number of remaining, non-excluded
131 BPO-relocs. */
132 size_t n_bpo_relocs;
134 /* This is the number of allocated bpo_reloc_requests; the size of
135 sorted_indexes. Valid after the check.*relocs functions are called
136 for all incoming sections. It includes the number of BPO relocs in
137 sections that were GC:ed. */
138 size_t n_max_bpo_relocs;
140 /* A counter used to find out when to fold the BPO gregs, since we
141 don't have a single "after-relaxation" hook. */
142 size_t n_remaining_bpo_relocs_this_relaxation_round;
144 /* The number of linker-allocated GREGs resulting from BPO relocs.
145 This is an approximation after _bfd_mmix_before_linker_allocation
146 and supposedly accurate after mmix_elf_relax_section is called for
147 all incoming non-collected sections. */
148 size_t n_allocated_bpo_gregs;
150 /* Index into reloc_request[], sorted on increasing "value", secondary
151 by increasing index for strict sorting order. */
152 size_t *bpo_reloc_indexes;
154 /* An array of all relocations, with the "value" member filled in by
155 the relaxation function. */
156 struct bpo_reloc_request *reloc_request;
159 static bfd_boolean mmix_elf_link_output_symbol_hook
160 PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
161 asection *, struct elf_link_hash_entry *));
163 static bfd_reloc_status_type mmix_elf_reloc
164 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
166 static reloc_howto_type *bfd_elf64_bfd_reloc_type_lookup
167 PARAMS ((bfd *, bfd_reloc_code_real_type));
169 static void mmix_info_to_howto_rela
170 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
172 static int mmix_elf_sort_relocs PARAMS ((const PTR, const PTR));
174 static bfd_boolean mmix_elf_new_section_hook
175 PARAMS ((bfd *, asection *));
177 static bfd_boolean mmix_elf_check_relocs
178 PARAMS ((bfd *, struct bfd_link_info *, asection *,
179 const Elf_Internal_Rela *));
181 static bfd_boolean mmix_elf_check_common_relocs
182 PARAMS ((bfd *, struct bfd_link_info *, asection *,
183 const Elf_Internal_Rela *));
185 static bfd_boolean mmix_elf_relocate_section
186 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
187 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
189 static asection * mmix_elf_gc_mark_hook
190 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
191 struct elf_link_hash_entry *, Elf_Internal_Sym *));
193 static bfd_boolean mmix_elf_gc_sweep_hook
194 PARAMS ((bfd *, struct bfd_link_info *, asection *,
195 const Elf_Internal_Rela *));
197 static bfd_reloc_status_type mmix_final_link_relocate
198 PARAMS ((reloc_howto_type *, asection *, bfd_byte *,
199 bfd_vma, bfd_signed_vma, bfd_vma, const char *, asection *));
201 static bfd_reloc_status_type mmix_elf_perform_relocation
202 PARAMS ((asection *, reloc_howto_type *, PTR, bfd_vma, bfd_vma));
204 static bfd_boolean mmix_elf_section_from_bfd_section
205 PARAMS ((bfd *, asection *, int *));
207 static bfd_boolean mmix_elf_add_symbol_hook
208 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
209 const char **, flagword *, asection **, bfd_vma *));
211 static bfd_boolean mmix_elf_is_local_label_name
212 PARAMS ((bfd *, const char *));
214 static int bpo_reloc_request_sort_fn PARAMS ((const PTR, const PTR));
216 static bfd_boolean mmix_elf_relax_section
217 PARAMS ((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
218 bfd_boolean *again));
220 extern bfd_boolean mmix_elf_final_link PARAMS ((bfd *, struct bfd_link_info *));
222 extern void mmix_elf_symbol_processing PARAMS ((bfd *, asymbol *));
224 /* Only intended to be called from a debugger. */
225 extern void mmix_dump_bpo_gregs
226 PARAMS ((struct bfd_link_info *, bfd_error_handler_type));
228 static void
229 mmix_set_relaxable_size
230 PARAMS ((bfd *, asection *, void *));
232 static bfd_boolean
233 mmix_elf_get_section_contents
234 PARAMS ((bfd *, sec_ptr, void *, file_ptr, bfd_size_type));
237 /* Watch out: this currently needs to have elements with the same index as
238 their R_MMIX_ number. */
239 static reloc_howto_type elf_mmix_howto_table[] =
241 /* This reloc does nothing. */
242 HOWTO (R_MMIX_NONE, /* type */
243 0, /* rightshift */
244 2, /* size (0 = byte, 1 = short, 2 = long) */
245 32, /* bitsize */
246 FALSE, /* pc_relative */
247 0, /* bitpos */
248 complain_overflow_bitfield, /* complain_on_overflow */
249 bfd_elf_generic_reloc, /* special_function */
250 "R_MMIX_NONE", /* name */
251 FALSE, /* partial_inplace */
252 0, /* src_mask */
253 0, /* dst_mask */
254 FALSE), /* pcrel_offset */
256 /* An 8 bit absolute relocation. */
257 HOWTO (R_MMIX_8, /* type */
258 0, /* rightshift */
259 0, /* size (0 = byte, 1 = short, 2 = long) */
260 8, /* bitsize */
261 FALSE, /* pc_relative */
262 0, /* bitpos */
263 complain_overflow_bitfield, /* complain_on_overflow */
264 bfd_elf_generic_reloc, /* special_function */
265 "R_MMIX_8", /* name */
266 FALSE, /* partial_inplace */
267 0, /* src_mask */
268 0xff, /* dst_mask */
269 FALSE), /* pcrel_offset */
271 /* An 16 bit absolute relocation. */
272 HOWTO (R_MMIX_16, /* type */
273 0, /* rightshift */
274 1, /* size (0 = byte, 1 = short, 2 = long) */
275 16, /* bitsize */
276 FALSE, /* pc_relative */
277 0, /* bitpos */
278 complain_overflow_bitfield, /* complain_on_overflow */
279 bfd_elf_generic_reloc, /* special_function */
280 "R_MMIX_16", /* name */
281 FALSE, /* partial_inplace */
282 0, /* src_mask */
283 0xffff, /* dst_mask */
284 FALSE), /* pcrel_offset */
286 /* An 24 bit absolute relocation. */
287 HOWTO (R_MMIX_24, /* type */
288 0, /* rightshift */
289 2, /* size (0 = byte, 1 = short, 2 = long) */
290 24, /* bitsize */
291 FALSE, /* pc_relative */
292 0, /* bitpos */
293 complain_overflow_bitfield, /* complain_on_overflow */
294 bfd_elf_generic_reloc, /* special_function */
295 "R_MMIX_24", /* name */
296 FALSE, /* partial_inplace */
297 ~0xffffff, /* src_mask */
298 0xffffff, /* dst_mask */
299 FALSE), /* pcrel_offset */
301 /* A 32 bit absolute relocation. */
302 HOWTO (R_MMIX_32, /* type */
303 0, /* rightshift */
304 2, /* size (0 = byte, 1 = short, 2 = long) */
305 32, /* bitsize */
306 FALSE, /* pc_relative */
307 0, /* bitpos */
308 complain_overflow_bitfield, /* complain_on_overflow */
309 bfd_elf_generic_reloc, /* special_function */
310 "R_MMIX_32", /* name */
311 FALSE, /* partial_inplace */
312 0, /* src_mask */
313 0xffffffff, /* dst_mask */
314 FALSE), /* pcrel_offset */
316 /* 64 bit relocation. */
317 HOWTO (R_MMIX_64, /* type */
318 0, /* rightshift */
319 4, /* size (0 = byte, 1 = short, 2 = long) */
320 64, /* bitsize */
321 FALSE, /* pc_relative */
322 0, /* bitpos */
323 complain_overflow_bitfield, /* complain_on_overflow */
324 bfd_elf_generic_reloc, /* special_function */
325 "R_MMIX_64", /* name */
326 FALSE, /* partial_inplace */
327 0, /* src_mask */
328 MINUS_ONE, /* dst_mask */
329 FALSE), /* pcrel_offset */
331 /* An 8 bit PC-relative relocation. */
332 HOWTO (R_MMIX_PC_8, /* type */
333 0, /* rightshift */
334 0, /* size (0 = byte, 1 = short, 2 = long) */
335 8, /* bitsize */
336 TRUE, /* pc_relative */
337 0, /* bitpos */
338 complain_overflow_bitfield, /* complain_on_overflow */
339 bfd_elf_generic_reloc, /* special_function */
340 "R_MMIX_PC_8", /* name */
341 FALSE, /* partial_inplace */
342 0, /* src_mask */
343 0xff, /* dst_mask */
344 TRUE), /* pcrel_offset */
346 /* An 16 bit PC-relative relocation. */
347 HOWTO (R_MMIX_PC_16, /* type */
348 0, /* rightshift */
349 1, /* size (0 = byte, 1 = short, 2 = long) */
350 16, /* bitsize */
351 TRUE, /* pc_relative */
352 0, /* bitpos */
353 complain_overflow_bitfield, /* complain_on_overflow */
354 bfd_elf_generic_reloc, /* special_function */
355 "R_MMIX_PC_16", /* name */
356 FALSE, /* partial_inplace */
357 0, /* src_mask */
358 0xffff, /* dst_mask */
359 TRUE), /* pcrel_offset */
361 /* An 24 bit PC-relative relocation. */
362 HOWTO (R_MMIX_PC_24, /* type */
363 0, /* rightshift */
364 2, /* size (0 = byte, 1 = short, 2 = long) */
365 24, /* bitsize */
366 TRUE, /* pc_relative */
367 0, /* bitpos */
368 complain_overflow_bitfield, /* complain_on_overflow */
369 bfd_elf_generic_reloc, /* special_function */
370 "R_MMIX_PC_24", /* name */
371 FALSE, /* partial_inplace */
372 ~0xffffff, /* src_mask */
373 0xffffff, /* dst_mask */
374 TRUE), /* pcrel_offset */
376 /* A 32 bit absolute PC-relative relocation. */
377 HOWTO (R_MMIX_PC_32, /* type */
378 0, /* rightshift */
379 2, /* size (0 = byte, 1 = short, 2 = long) */
380 32, /* bitsize */
381 TRUE, /* pc_relative */
382 0, /* bitpos */
383 complain_overflow_bitfield, /* complain_on_overflow */
384 bfd_elf_generic_reloc, /* special_function */
385 "R_MMIX_PC_32", /* name */
386 FALSE, /* partial_inplace */
387 0, /* src_mask */
388 0xffffffff, /* dst_mask */
389 TRUE), /* pcrel_offset */
391 /* 64 bit PC-relative relocation. */
392 HOWTO (R_MMIX_PC_64, /* type */
393 0, /* rightshift */
394 4, /* size (0 = byte, 1 = short, 2 = long) */
395 64, /* bitsize */
396 TRUE, /* pc_relative */
397 0, /* bitpos */
398 complain_overflow_bitfield, /* complain_on_overflow */
399 bfd_elf_generic_reloc, /* special_function */
400 "R_MMIX_PC_64", /* name */
401 FALSE, /* partial_inplace */
402 0, /* src_mask */
403 MINUS_ONE, /* dst_mask */
404 TRUE), /* pcrel_offset */
406 /* GNU extension to record C++ vtable hierarchy. */
407 HOWTO (R_MMIX_GNU_VTINHERIT, /* type */
408 0, /* rightshift */
409 0, /* size (0 = byte, 1 = short, 2 = long) */
410 0, /* bitsize */
411 FALSE, /* pc_relative */
412 0, /* bitpos */
413 complain_overflow_dont, /* complain_on_overflow */
414 NULL, /* special_function */
415 "R_MMIX_GNU_VTINHERIT", /* name */
416 FALSE, /* partial_inplace */
417 0, /* src_mask */
418 0, /* dst_mask */
419 TRUE), /* pcrel_offset */
421 /* GNU extension to record C++ vtable member usage. */
422 HOWTO (R_MMIX_GNU_VTENTRY, /* type */
423 0, /* rightshift */
424 0, /* size (0 = byte, 1 = short, 2 = long) */
425 0, /* bitsize */
426 FALSE, /* pc_relative */
427 0, /* bitpos */
428 complain_overflow_dont, /* complain_on_overflow */
429 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
430 "R_MMIX_GNU_VTENTRY", /* name */
431 FALSE, /* partial_inplace */
432 0, /* src_mask */
433 0, /* dst_mask */
434 FALSE), /* pcrel_offset */
436 /* The GETA relocation is supposed to get any address that could
437 possibly be reached by the GETA instruction. It can silently expand
438 to get a 64-bit operand, but will complain if any of the two least
439 significant bits are set. The howto members reflect a simple GETA. */
440 HOWTO (R_MMIX_GETA, /* type */
441 2, /* rightshift */
442 2, /* size (0 = byte, 1 = short, 2 = long) */
443 19, /* bitsize */
444 TRUE, /* pc_relative */
445 0, /* bitpos */
446 complain_overflow_signed, /* complain_on_overflow */
447 mmix_elf_reloc, /* special_function */
448 "R_MMIX_GETA", /* name */
449 FALSE, /* partial_inplace */
450 ~0x0100ffff, /* src_mask */
451 0x0100ffff, /* dst_mask */
452 TRUE), /* pcrel_offset */
454 HOWTO (R_MMIX_GETA_1, /* type */
455 2, /* rightshift */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
457 19, /* bitsize */
458 TRUE, /* pc_relative */
459 0, /* bitpos */
460 complain_overflow_signed, /* complain_on_overflow */
461 mmix_elf_reloc, /* special_function */
462 "R_MMIX_GETA_1", /* name */
463 FALSE, /* partial_inplace */
464 ~0x0100ffff, /* src_mask */
465 0x0100ffff, /* dst_mask */
466 TRUE), /* pcrel_offset */
468 HOWTO (R_MMIX_GETA_2, /* type */
469 2, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 19, /* bitsize */
472 TRUE, /* pc_relative */
473 0, /* bitpos */
474 complain_overflow_signed, /* complain_on_overflow */
475 mmix_elf_reloc, /* special_function */
476 "R_MMIX_GETA_2", /* name */
477 FALSE, /* partial_inplace */
478 ~0x0100ffff, /* src_mask */
479 0x0100ffff, /* dst_mask */
480 TRUE), /* pcrel_offset */
482 HOWTO (R_MMIX_GETA_3, /* type */
483 2, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 19, /* bitsize */
486 TRUE, /* pc_relative */
487 0, /* bitpos */
488 complain_overflow_signed, /* complain_on_overflow */
489 mmix_elf_reloc, /* special_function */
490 "R_MMIX_GETA_3", /* name */
491 FALSE, /* partial_inplace */
492 ~0x0100ffff, /* src_mask */
493 0x0100ffff, /* dst_mask */
494 TRUE), /* pcrel_offset */
496 /* The conditional branches are supposed to reach any (code) address.
497 It can silently expand to a 64-bit operand, but will emit an error if
498 any of the two least significant bits are set. The howto members
499 reflect a simple branch. */
500 HOWTO (R_MMIX_CBRANCH, /* type */
501 2, /* rightshift */
502 2, /* size (0 = byte, 1 = short, 2 = long) */
503 19, /* bitsize */
504 TRUE, /* pc_relative */
505 0, /* bitpos */
506 complain_overflow_signed, /* complain_on_overflow */
507 mmix_elf_reloc, /* special_function */
508 "R_MMIX_CBRANCH", /* name */
509 FALSE, /* partial_inplace */
510 ~0x0100ffff, /* src_mask */
511 0x0100ffff, /* dst_mask */
512 TRUE), /* pcrel_offset */
514 HOWTO (R_MMIX_CBRANCH_J, /* type */
515 2, /* rightshift */
516 2, /* size (0 = byte, 1 = short, 2 = long) */
517 19, /* bitsize */
518 TRUE, /* pc_relative */
519 0, /* bitpos */
520 complain_overflow_signed, /* complain_on_overflow */
521 mmix_elf_reloc, /* special_function */
522 "R_MMIX_CBRANCH_J", /* name */
523 FALSE, /* partial_inplace */
524 ~0x0100ffff, /* src_mask */
525 0x0100ffff, /* dst_mask */
526 TRUE), /* pcrel_offset */
528 HOWTO (R_MMIX_CBRANCH_1, /* type */
529 2, /* rightshift */
530 2, /* size (0 = byte, 1 = short, 2 = long) */
531 19, /* bitsize */
532 TRUE, /* pc_relative */
533 0, /* bitpos */
534 complain_overflow_signed, /* complain_on_overflow */
535 mmix_elf_reloc, /* special_function */
536 "R_MMIX_CBRANCH_1", /* name */
537 FALSE, /* partial_inplace */
538 ~0x0100ffff, /* src_mask */
539 0x0100ffff, /* dst_mask */
540 TRUE), /* pcrel_offset */
542 HOWTO (R_MMIX_CBRANCH_2, /* type */
543 2, /* rightshift */
544 2, /* size (0 = byte, 1 = short, 2 = long) */
545 19, /* bitsize */
546 TRUE, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_signed, /* complain_on_overflow */
549 mmix_elf_reloc, /* special_function */
550 "R_MMIX_CBRANCH_2", /* name */
551 FALSE, /* partial_inplace */
552 ~0x0100ffff, /* src_mask */
553 0x0100ffff, /* dst_mask */
554 TRUE), /* pcrel_offset */
556 HOWTO (R_MMIX_CBRANCH_3, /* type */
557 2, /* rightshift */
558 2, /* size (0 = byte, 1 = short, 2 = long) */
559 19, /* bitsize */
560 TRUE, /* pc_relative */
561 0, /* bitpos */
562 complain_overflow_signed, /* complain_on_overflow */
563 mmix_elf_reloc, /* special_function */
564 "R_MMIX_CBRANCH_3", /* name */
565 FALSE, /* partial_inplace */
566 ~0x0100ffff, /* src_mask */
567 0x0100ffff, /* dst_mask */
568 TRUE), /* pcrel_offset */
570 /* The PUSHJ instruction can reach any (code) address, as long as it's
571 the beginning of a function (no usable restriction). It can silently
572 expand to a 64-bit operand, but will emit an error if any of the two
573 least significant bits are set. It can also expand into a call to a
574 stub; see R_MMIX_PUSHJ_STUBBABLE. The howto members reflect a simple
575 PUSHJ. */
576 HOWTO (R_MMIX_PUSHJ, /* type */
577 2, /* rightshift */
578 2, /* size (0 = byte, 1 = short, 2 = long) */
579 19, /* bitsize */
580 TRUE, /* pc_relative */
581 0, /* bitpos */
582 complain_overflow_signed, /* complain_on_overflow */
583 mmix_elf_reloc, /* special_function */
584 "R_MMIX_PUSHJ", /* name */
585 FALSE, /* partial_inplace */
586 ~0x0100ffff, /* src_mask */
587 0x0100ffff, /* dst_mask */
588 TRUE), /* pcrel_offset */
590 HOWTO (R_MMIX_PUSHJ_1, /* type */
591 2, /* rightshift */
592 2, /* size (0 = byte, 1 = short, 2 = long) */
593 19, /* bitsize */
594 TRUE, /* pc_relative */
595 0, /* bitpos */
596 complain_overflow_signed, /* complain_on_overflow */
597 mmix_elf_reloc, /* special_function */
598 "R_MMIX_PUSHJ_1", /* name */
599 FALSE, /* partial_inplace */
600 ~0x0100ffff, /* src_mask */
601 0x0100ffff, /* dst_mask */
602 TRUE), /* pcrel_offset */
604 HOWTO (R_MMIX_PUSHJ_2, /* type */
605 2, /* rightshift */
606 2, /* size (0 = byte, 1 = short, 2 = long) */
607 19, /* bitsize */
608 TRUE, /* pc_relative */
609 0, /* bitpos */
610 complain_overflow_signed, /* complain_on_overflow */
611 mmix_elf_reloc, /* special_function */
612 "R_MMIX_PUSHJ_2", /* name */
613 FALSE, /* partial_inplace */
614 ~0x0100ffff, /* src_mask */
615 0x0100ffff, /* dst_mask */
616 TRUE), /* pcrel_offset */
618 HOWTO (R_MMIX_PUSHJ_3, /* type */
619 2, /* rightshift */
620 2, /* size (0 = byte, 1 = short, 2 = long) */
621 19, /* bitsize */
622 TRUE, /* pc_relative */
623 0, /* bitpos */
624 complain_overflow_signed, /* complain_on_overflow */
625 mmix_elf_reloc, /* special_function */
626 "R_MMIX_PUSHJ_3", /* name */
627 FALSE, /* partial_inplace */
628 ~0x0100ffff, /* src_mask */
629 0x0100ffff, /* dst_mask */
630 TRUE), /* pcrel_offset */
632 /* A JMP is supposed to reach any (code) address. By itself, it can
633 reach +-64M; the expansion can reach all 64 bits. Note that the 64M
634 limit is soon reached if you link the program in wildly different
635 memory segments. The howto members reflect a trivial JMP. */
636 HOWTO (R_MMIX_JMP, /* type */
637 2, /* rightshift */
638 2, /* size (0 = byte, 1 = short, 2 = long) */
639 27, /* bitsize */
640 TRUE, /* pc_relative */
641 0, /* bitpos */
642 complain_overflow_signed, /* complain_on_overflow */
643 mmix_elf_reloc, /* special_function */
644 "R_MMIX_JMP", /* name */
645 FALSE, /* partial_inplace */
646 ~0x1ffffff, /* src_mask */
647 0x1ffffff, /* dst_mask */
648 TRUE), /* pcrel_offset */
650 HOWTO (R_MMIX_JMP_1, /* type */
651 2, /* rightshift */
652 2, /* size (0 = byte, 1 = short, 2 = long) */
653 27, /* bitsize */
654 TRUE, /* pc_relative */
655 0, /* bitpos */
656 complain_overflow_signed, /* complain_on_overflow */
657 mmix_elf_reloc, /* special_function */
658 "R_MMIX_JMP_1", /* name */
659 FALSE, /* partial_inplace */
660 ~0x1ffffff, /* src_mask */
661 0x1ffffff, /* dst_mask */
662 TRUE), /* pcrel_offset */
664 HOWTO (R_MMIX_JMP_2, /* type */
665 2, /* rightshift */
666 2, /* size (0 = byte, 1 = short, 2 = long) */
667 27, /* bitsize */
668 TRUE, /* pc_relative */
669 0, /* bitpos */
670 complain_overflow_signed, /* complain_on_overflow */
671 mmix_elf_reloc, /* special_function */
672 "R_MMIX_JMP_2", /* name */
673 FALSE, /* partial_inplace */
674 ~0x1ffffff, /* src_mask */
675 0x1ffffff, /* dst_mask */
676 TRUE), /* pcrel_offset */
678 HOWTO (R_MMIX_JMP_3, /* type */
679 2, /* rightshift */
680 2, /* size (0 = byte, 1 = short, 2 = long) */
681 27, /* bitsize */
682 TRUE, /* pc_relative */
683 0, /* bitpos */
684 complain_overflow_signed, /* complain_on_overflow */
685 mmix_elf_reloc, /* special_function */
686 "R_MMIX_JMP_3", /* name */
687 FALSE, /* partial_inplace */
688 ~0x1ffffff, /* src_mask */
689 0x1ffffff, /* dst_mask */
690 TRUE), /* pcrel_offset */
692 /* When we don't emit link-time-relaxable code from the assembler, or
693 when relaxation has done all it can do, these relocs are used. For
694 GETA/PUSHJ/branches. */
695 HOWTO (R_MMIX_ADDR19, /* type */
696 2, /* rightshift */
697 2, /* size (0 = byte, 1 = short, 2 = long) */
698 19, /* bitsize */
699 TRUE, /* pc_relative */
700 0, /* bitpos */
701 complain_overflow_signed, /* complain_on_overflow */
702 mmix_elf_reloc, /* special_function */
703 "R_MMIX_ADDR19", /* name */
704 FALSE, /* partial_inplace */
705 ~0x0100ffff, /* src_mask */
706 0x0100ffff, /* dst_mask */
707 TRUE), /* pcrel_offset */
709 /* For JMP. */
710 HOWTO (R_MMIX_ADDR27, /* type */
711 2, /* rightshift */
712 2, /* size (0 = byte, 1 = short, 2 = long) */
713 27, /* bitsize */
714 TRUE, /* pc_relative */
715 0, /* bitpos */
716 complain_overflow_signed, /* complain_on_overflow */
717 mmix_elf_reloc, /* special_function */
718 "R_MMIX_ADDR27", /* name */
719 FALSE, /* partial_inplace */
720 ~0x1ffffff, /* src_mask */
721 0x1ffffff, /* dst_mask */
722 TRUE), /* pcrel_offset */
724 /* A general register or the value 0..255. If a value, then the
725 instruction (offset -3) needs adjusting. */
726 HOWTO (R_MMIX_REG_OR_BYTE, /* type */
727 0, /* rightshift */
728 1, /* size (0 = byte, 1 = short, 2 = long) */
729 8, /* bitsize */
730 FALSE, /* pc_relative */
731 0, /* bitpos */
732 complain_overflow_bitfield, /* complain_on_overflow */
733 mmix_elf_reloc, /* special_function */
734 "R_MMIX_REG_OR_BYTE", /* name */
735 FALSE, /* partial_inplace */
736 0, /* src_mask */
737 0xff, /* dst_mask */
738 FALSE), /* pcrel_offset */
740 /* A general register. */
741 HOWTO (R_MMIX_REG, /* type */
742 0, /* rightshift */
743 1, /* size (0 = byte, 1 = short, 2 = long) */
744 8, /* bitsize */
745 FALSE, /* pc_relative */
746 0, /* bitpos */
747 complain_overflow_bitfield, /* complain_on_overflow */
748 mmix_elf_reloc, /* special_function */
749 "R_MMIX_REG", /* name */
750 FALSE, /* partial_inplace */
751 0, /* src_mask */
752 0xff, /* dst_mask */
753 FALSE), /* pcrel_offset */
755 /* A register plus an index, corresponding to the relocation expression.
756 The sizes must correspond to the valid range of the expression, while
757 the bitmasks correspond to what we store in the image. */
758 HOWTO (R_MMIX_BASE_PLUS_OFFSET, /* type */
759 0, /* rightshift */
760 4, /* size (0 = byte, 1 = short, 2 = long) */
761 64, /* bitsize */
762 FALSE, /* pc_relative */
763 0, /* bitpos */
764 complain_overflow_bitfield, /* complain_on_overflow */
765 mmix_elf_reloc, /* special_function */
766 "R_MMIX_BASE_PLUS_OFFSET", /* name */
767 FALSE, /* partial_inplace */
768 0, /* src_mask */
769 0xffff, /* dst_mask */
770 FALSE), /* pcrel_offset */
772 /* A "magic" relocation for a LOCAL expression, asserting that the
773 expression is less than the number of global registers. No actual
774 modification of the contents is done. Implementing this as a
775 relocation was less intrusive than e.g. putting such expressions in a
776 section to discard *after* relocation. */
777 HOWTO (R_MMIX_LOCAL, /* type */
778 0, /* rightshift */
779 0, /* size (0 = byte, 1 = short, 2 = long) */
780 0, /* bitsize */
781 FALSE, /* pc_relative */
782 0, /* bitpos */
783 complain_overflow_dont, /* complain_on_overflow */
784 mmix_elf_reloc, /* special_function */
785 "R_MMIX_LOCAL", /* name */
786 FALSE, /* partial_inplace */
787 0, /* src_mask */
788 0, /* dst_mask */
789 FALSE), /* pcrel_offset */
791 HOWTO (R_MMIX_PUSHJ_STUBBABLE, /* type */
792 2, /* rightshift */
793 2, /* size (0 = byte, 1 = short, 2 = long) */
794 19, /* bitsize */
795 TRUE, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_signed, /* complain_on_overflow */
798 mmix_elf_reloc, /* special_function */
799 "R_MMIX_PUSHJ_STUBBABLE", /* name */
800 FALSE, /* partial_inplace */
801 ~0x0100ffff, /* src_mask */
802 0x0100ffff, /* dst_mask */
803 TRUE) /* pcrel_offset */
807 /* Map BFD reloc types to MMIX ELF reloc types. */
809 struct mmix_reloc_map
811 bfd_reloc_code_real_type bfd_reloc_val;
812 enum elf_mmix_reloc_type elf_reloc_val;
816 static const struct mmix_reloc_map mmix_reloc_map[] =
818 {BFD_RELOC_NONE, R_MMIX_NONE},
819 {BFD_RELOC_8, R_MMIX_8},
820 {BFD_RELOC_16, R_MMIX_16},
821 {BFD_RELOC_24, R_MMIX_24},
822 {BFD_RELOC_32, R_MMIX_32},
823 {BFD_RELOC_64, R_MMIX_64},
824 {BFD_RELOC_8_PCREL, R_MMIX_PC_8},
825 {BFD_RELOC_16_PCREL, R_MMIX_PC_16},
826 {BFD_RELOC_24_PCREL, R_MMIX_PC_24},
827 {BFD_RELOC_32_PCREL, R_MMIX_PC_32},
828 {BFD_RELOC_64_PCREL, R_MMIX_PC_64},
829 {BFD_RELOC_VTABLE_INHERIT, R_MMIX_GNU_VTINHERIT},
830 {BFD_RELOC_VTABLE_ENTRY, R_MMIX_GNU_VTENTRY},
831 {BFD_RELOC_MMIX_GETA, R_MMIX_GETA},
832 {BFD_RELOC_MMIX_CBRANCH, R_MMIX_CBRANCH},
833 {BFD_RELOC_MMIX_PUSHJ, R_MMIX_PUSHJ},
834 {BFD_RELOC_MMIX_JMP, R_MMIX_JMP},
835 {BFD_RELOC_MMIX_ADDR19, R_MMIX_ADDR19},
836 {BFD_RELOC_MMIX_ADDR27, R_MMIX_ADDR27},
837 {BFD_RELOC_MMIX_REG_OR_BYTE, R_MMIX_REG_OR_BYTE},
838 {BFD_RELOC_MMIX_REG, R_MMIX_REG},
839 {BFD_RELOC_MMIX_BASE_PLUS_OFFSET, R_MMIX_BASE_PLUS_OFFSET},
840 {BFD_RELOC_MMIX_LOCAL, R_MMIX_LOCAL},
841 {BFD_RELOC_MMIX_PUSHJ_STUBBABLE, R_MMIX_PUSHJ_STUBBABLE}
844 static reloc_howto_type *
845 bfd_elf64_bfd_reloc_type_lookup (abfd, code)
846 bfd *abfd ATTRIBUTE_UNUSED;
847 bfd_reloc_code_real_type code;
849 unsigned int i;
851 for (i = 0;
852 i < sizeof (mmix_reloc_map) / sizeof (mmix_reloc_map[0]);
853 i++)
855 if (mmix_reloc_map[i].bfd_reloc_val == code)
856 return &elf_mmix_howto_table[mmix_reloc_map[i].elf_reloc_val];
859 return NULL;
862 static bfd_boolean
863 mmix_elf_new_section_hook (abfd, sec)
864 bfd *abfd;
865 asection *sec;
867 struct _mmix_elf_section_data *sdata;
868 bfd_size_type amt = sizeof (*sdata);
870 sdata = (struct _mmix_elf_section_data *) bfd_zalloc (abfd, amt);
871 if (sdata == NULL)
872 return FALSE;
873 sec->used_by_bfd = (PTR) sdata;
875 return _bfd_elf_new_section_hook (abfd, sec);
879 /* This function performs the actual bitfiddling and sanity check for a
880 final relocation. Each relocation gets its *worst*-case expansion
881 in size when it arrives here; any reduction in size should have been
882 caught in linker relaxation earlier. When we get here, the relocation
883 looks like the smallest instruction with SWYM:s (nop:s) appended to the
884 max size. We fill in those nop:s.
886 R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
887 GETA $N,foo
889 SETL $N,foo & 0xffff
890 INCML $N,(foo >> 16) & 0xffff
891 INCMH $N,(foo >> 32) & 0xffff
892 INCH $N,(foo >> 48) & 0xffff
894 R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
895 condbranches needing relaxation might be rare enough to not be
896 worthwhile.)
897 [P]Bcc $N,foo
899 [~P]B~cc $N,.+20
900 SETL $255,foo & ...
901 INCML ...
902 INCMH ...
903 INCH ...
904 GO $255,$255,0
906 R_MMIX_PUSHJ: (FIXME: Relaxation...)
907 PUSHJ $N,foo
909 SETL $255,foo & ...
910 INCML ...
911 INCMH ...
912 INCH ...
913 PUSHGO $N,$255,0
915 R_MMIX_JMP: (FIXME: Relaxation...)
916 JMP foo
918 SETL $255,foo & ...
919 INCML ...
920 INCMH ...
921 INCH ...
922 GO $255,$255,0
924 R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in. */
926 static bfd_reloc_status_type
927 mmix_elf_perform_relocation (isec, howto, datap, addr, value)
928 asection *isec;
929 reloc_howto_type *howto;
930 PTR datap;
931 bfd_vma addr;
932 bfd_vma value;
934 bfd *abfd = isec->owner;
935 bfd_reloc_status_type flag = bfd_reloc_ok;
936 bfd_reloc_status_type r;
937 int offs = 0;
938 int reg = 255;
940 /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
941 We handle the differences here and the common sequence later. */
942 switch (howto->type)
944 case R_MMIX_GETA:
945 offs = 0;
946 reg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
948 /* We change to an absolute value. */
949 value += addr;
950 break;
952 case R_MMIX_CBRANCH:
954 int in1 = bfd_get_16 (abfd, (bfd_byte *) datap) << 16;
956 /* Invert the condition and prediction bit, and set the offset
957 to five instructions ahead.
959 We *can* do better if we want to. If the branch is found to be
960 within limits, we could leave the branch as is; there'll just
961 be a bunch of NOP:s after it. But we shouldn't see this
962 sequence often enough that it's worth doing it. */
964 bfd_put_32 (abfd,
965 (((in1 ^ ((PRED_INV_BIT | COND_INV_BIT) << 24)) & ~0xffff)
966 | (24/4)),
967 (bfd_byte *) datap);
969 /* Put a "GO $255,$255,0" after the common sequence. */
970 bfd_put_32 (abfd,
971 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24) | 0xffff00,
972 (bfd_byte *) datap + 20);
974 /* Common sequence starts at offset 4. */
975 offs = 4;
977 /* We change to an absolute value. */
978 value += addr;
980 break;
982 case R_MMIX_PUSHJ_STUBBABLE:
983 /* If the address fits, we're fine. */
984 if ((value & 3) == 0
985 /* Note rightshift 0; see R_MMIX_JMP case below. */
986 && (r = bfd_check_overflow (complain_overflow_signed,
987 howto->bitsize,
989 bfd_arch_bits_per_address (abfd),
990 value)) == bfd_reloc_ok)
991 goto pcrel_mmix_reloc_fits;
992 else
994 bfd_size_type raw_size
995 = (isec->_raw_size
996 - mmix_elf_section_data (isec)->pjs.n_pushj_relocs
997 * MAX_PUSHJ_STUB_SIZE);
999 /* We have the bytes at the PUSHJ insn and need to get the
1000 position for the stub. There's supposed to be room allocated
1001 for the stub. */
1002 bfd_byte *stubcontents
1003 = ((char *) datap
1004 - (addr - (isec->output_section->vma + isec->output_offset))
1005 + raw_size
1006 + mmix_elf_section_data (isec)->pjs.stub_offset);
1007 bfd_vma stubaddr;
1009 /* The address doesn't fit, so redirect the PUSHJ to the
1010 location of the stub. */
1011 r = mmix_elf_perform_relocation (isec,
1012 &elf_mmix_howto_table
1013 [R_MMIX_ADDR19],
1014 datap,
1015 addr,
1016 isec->output_section->vma
1017 + isec->output_offset
1018 + raw_size
1019 + (mmix_elf_section_data (isec)
1020 ->pjs.stub_offset)
1021 - addr);
1022 if (r != bfd_reloc_ok)
1023 return r;
1025 stubaddr
1026 = (isec->output_section->vma
1027 + isec->output_offset
1028 + raw_size
1029 + mmix_elf_section_data (isec)->pjs.stub_offset);
1031 /* We generate a simple JMP if that suffices, else the whole 5
1032 insn stub. */
1033 if (bfd_check_overflow (complain_overflow_signed,
1034 elf_mmix_howto_table[R_MMIX_ADDR27].bitsize,
1036 bfd_arch_bits_per_address (abfd),
1037 addr + value - stubaddr) == bfd_reloc_ok)
1039 bfd_put_32 (abfd, JMP_INSN_BYTE << 24, stubcontents);
1040 r = mmix_elf_perform_relocation (isec,
1041 &elf_mmix_howto_table
1042 [R_MMIX_ADDR27],
1043 stubcontents,
1044 stubaddr,
1045 value + addr - stubaddr);
1046 mmix_elf_section_data (isec)->pjs.stub_offset += 4;
1048 if (raw_size
1049 + mmix_elf_section_data (isec)->pjs.stub_offset
1050 > isec->_cooked_size)
1051 abort ();
1053 return r;
1055 else
1057 /* Put a "GO $255,0" after the common sequence. */
1058 bfd_put_32 (abfd,
1059 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1060 | 0xff00, (bfd_byte *) stubcontents + 16);
1062 /* Prepare for the general code to set the first part of the
1063 linker stub, and */
1064 value += addr;
1065 datap = stubcontents;
1066 mmix_elf_section_data (isec)->pjs.stub_offset
1067 += MAX_PUSHJ_STUB_SIZE;
1070 break;
1072 case R_MMIX_PUSHJ:
1074 int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
1076 /* Put a "PUSHGO $N,$255,0" after the common sequence. */
1077 bfd_put_32 (abfd,
1078 ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1079 | (inreg << 16)
1080 | 0xff00,
1081 (bfd_byte *) datap + 16);
1083 /* We change to an absolute value. */
1084 value += addr;
1086 break;
1088 case R_MMIX_JMP:
1089 /* This one is a little special. If we get here on a non-relaxing
1090 link, and the destination is actually in range, we don't need to
1091 execute the nops.
1092 If so, we fall through to the bit-fiddling relocs.
1094 FIXME: bfd_check_overflow seems broken; the relocation is
1095 rightshifted before testing, so supply a zero rightshift. */
1097 if (! ((value & 3) == 0
1098 && (r = bfd_check_overflow (complain_overflow_signed,
1099 howto->bitsize,
1101 bfd_arch_bits_per_address (abfd),
1102 value)) == bfd_reloc_ok))
1104 /* If the relocation doesn't fit in a JMP, we let the NOP:s be
1105 modified below, and put a "GO $255,$255,0" after the
1106 address-loading sequence. */
1107 bfd_put_32 (abfd,
1108 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1109 | 0xffff00,
1110 (bfd_byte *) datap + 16);
1112 /* We change to an absolute value. */
1113 value += addr;
1114 break;
1116 /* FALLTHROUGH. */
1117 case R_MMIX_ADDR19:
1118 case R_MMIX_ADDR27:
1119 pcrel_mmix_reloc_fits:
1120 /* These must be in range, or else we emit an error. */
1121 if ((value & 3) == 0
1122 /* Note rightshift 0; see above. */
1123 && (r = bfd_check_overflow (complain_overflow_signed,
1124 howto->bitsize,
1126 bfd_arch_bits_per_address (abfd),
1127 value)) == bfd_reloc_ok)
1129 bfd_vma in1
1130 = bfd_get_32 (abfd, (bfd_byte *) datap);
1131 bfd_vma highbit;
1133 if ((bfd_signed_vma) value < 0)
1135 highbit = 1 << 24;
1136 value += (1 << (howto->bitsize - 1));
1138 else
1139 highbit = 0;
1141 value >>= 2;
1143 bfd_put_32 (abfd,
1144 (in1 & howto->src_mask)
1145 | highbit
1146 | (value & howto->dst_mask),
1147 (bfd_byte *) datap);
1149 return bfd_reloc_ok;
1151 else
1152 return bfd_reloc_overflow;
1154 case R_MMIX_BASE_PLUS_OFFSET:
1156 struct bpo_reloc_section_info *bpodata
1157 = mmix_elf_section_data (isec)->bpo.reloc;
1158 asection *bpo_greg_section
1159 = bpodata->bpo_greg_section;
1160 struct bpo_greg_section_info *gregdata
1161 = mmix_elf_section_data (bpo_greg_section)->bpo.greg;
1162 size_t bpo_index
1163 = gregdata->bpo_reloc_indexes[bpodata->bpo_index++];
1165 /* A consistency check: The value we now have in "relocation" must
1166 be the same as the value we stored for that relocation. It
1167 doesn't cost much, so can be left in at all times. */
1168 if (value != gregdata->reloc_request[bpo_index].value)
1170 (*_bfd_error_handler)
1171 (_("%s: Internal inconsistency error for value for\n\
1172 linker-allocated global register: linked: 0x%lx%08lx != relaxed: 0x%lx%08lx\n"),
1173 bfd_get_filename (isec->owner),
1174 (unsigned long) (value >> 32), (unsigned long) value,
1175 (unsigned long) (gregdata->reloc_request[bpo_index].value
1176 >> 32),
1177 (unsigned long) gregdata->reloc_request[bpo_index].value);
1178 bfd_set_error (bfd_error_bad_value);
1179 return bfd_reloc_overflow;
1182 /* Then store the register number and offset for that register
1183 into datap and datap + 1 respectively. */
1184 bfd_put_8 (abfd,
1185 gregdata->reloc_request[bpo_index].regindex
1186 + bpo_greg_section->output_section->vma / 8,
1187 datap);
1188 bfd_put_8 (abfd,
1189 gregdata->reloc_request[bpo_index].offset,
1190 ((unsigned char *) datap) + 1);
1191 return bfd_reloc_ok;
1194 case R_MMIX_REG_OR_BYTE:
1195 case R_MMIX_REG:
1196 if (value > 255)
1197 return bfd_reloc_overflow;
1198 bfd_put_8 (abfd, value, datap);
1199 return bfd_reloc_ok;
1201 default:
1202 BAD_CASE (howto->type);
1205 /* This code adds the common SETL/INCML/INCMH/INCH worst-case
1206 sequence. */
1208 /* Lowest two bits must be 0. We return bfd_reloc_overflow for
1209 everything that looks strange. */
1210 if (value & 3)
1211 flag = bfd_reloc_overflow;
1213 bfd_put_32 (abfd,
1214 (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
1215 (bfd_byte *) datap + offs);
1216 bfd_put_32 (abfd,
1217 (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
1218 (bfd_byte *) datap + offs + 4);
1219 bfd_put_32 (abfd,
1220 (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
1221 (bfd_byte *) datap + offs + 8);
1222 bfd_put_32 (abfd,
1223 (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
1224 (bfd_byte *) datap + offs + 12);
1226 return flag;
1229 /* Set the howto pointer for an MMIX ELF reloc (type RELA). */
1231 static void
1232 mmix_info_to_howto_rela (abfd, cache_ptr, dst)
1233 bfd *abfd ATTRIBUTE_UNUSED;
1234 arelent *cache_ptr;
1235 Elf_Internal_Rela *dst;
1237 unsigned int r_type;
1239 r_type = ELF64_R_TYPE (dst->r_info);
1240 BFD_ASSERT (r_type < (unsigned int) R_MMIX_max);
1241 cache_ptr->howto = &elf_mmix_howto_table[r_type];
1244 /* Any MMIX-specific relocation gets here at assembly time or when linking
1245 to other formats (such as mmo); this is the relocation function from
1246 the reloc_table. We don't get here for final pure ELF linking. */
1248 static bfd_reloc_status_type
1249 mmix_elf_reloc (abfd, reloc_entry, symbol, data, input_section,
1250 output_bfd, error_message)
1251 bfd *abfd;
1252 arelent *reloc_entry;
1253 asymbol *symbol;
1254 PTR data;
1255 asection *input_section;
1256 bfd *output_bfd;
1257 char **error_message ATTRIBUTE_UNUSED;
1259 bfd_vma relocation;
1260 bfd_reloc_status_type r;
1261 asection *reloc_target_output_section;
1262 bfd_reloc_status_type flag = bfd_reloc_ok;
1263 bfd_vma output_base = 0;
1264 bfd_vma addr;
1266 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1267 input_section, output_bfd, error_message);
1269 /* If that was all that was needed (i.e. this isn't a final link, only
1270 some segment adjustments), we're done. */
1271 if (r != bfd_reloc_continue)
1272 return r;
1274 if (bfd_is_und_section (symbol->section)
1275 && (symbol->flags & BSF_WEAK) == 0
1276 && output_bfd == (bfd *) NULL)
1277 return bfd_reloc_undefined;
1279 /* Is the address of the relocation really within the section? */
1280 if (reloc_entry->address > input_section->_cooked_size)
1281 return bfd_reloc_outofrange;
1283 /* Work out which section the relocation is targeted at and the
1284 initial relocation command value. */
1286 /* Get symbol value. (Common symbols are special.) */
1287 if (bfd_is_com_section (symbol->section))
1288 relocation = 0;
1289 else
1290 relocation = symbol->value;
1292 reloc_target_output_section = bfd_get_output_section (symbol);
1294 /* Here the variable relocation holds the final address of the symbol we
1295 are relocating against, plus any addend. */
1296 if (output_bfd)
1297 output_base = 0;
1298 else
1299 output_base = reloc_target_output_section->vma;
1301 relocation += output_base + symbol->section->output_offset;
1303 /* Get position of relocation. */
1304 addr = (reloc_entry->address + input_section->output_section->vma
1305 + input_section->output_offset);
1306 if (output_bfd != (bfd *) NULL)
1308 /* Add in supplied addend. */
1309 relocation += reloc_entry->addend;
1311 /* This is a partial relocation, and we want to apply the
1312 relocation to the reloc entry rather than the raw data.
1313 Modify the reloc inplace to reflect what we now know. */
1314 reloc_entry->addend = relocation;
1315 reloc_entry->address += input_section->output_offset;
1316 return flag;
1319 return mmix_final_link_relocate (reloc_entry->howto, input_section,
1320 data, reloc_entry->address,
1321 reloc_entry->addend, relocation,
1322 bfd_asymbol_name (symbol),
1323 reloc_target_output_section);
1326 /* Relocate an MMIX ELF section. Modified from elf32-fr30.c; look to it
1327 for guidance if you're thinking of copying this. */
1329 static bfd_boolean
1330 mmix_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1331 contents, relocs, local_syms, local_sections)
1332 bfd *output_bfd ATTRIBUTE_UNUSED;
1333 struct bfd_link_info *info;
1334 bfd *input_bfd;
1335 asection *input_section;
1336 bfd_byte *contents;
1337 Elf_Internal_Rela *relocs;
1338 Elf_Internal_Sym *local_syms;
1339 asection **local_sections;
1341 Elf_Internal_Shdr *symtab_hdr;
1342 struct elf_link_hash_entry **sym_hashes;
1343 Elf_Internal_Rela *rel;
1344 Elf_Internal_Rela *relend;
1345 bfd_size_type raw_size
1346 = (input_section->_raw_size
1347 - mmix_elf_section_data (input_section)->pjs.n_pushj_relocs
1348 * MAX_PUSHJ_STUB_SIZE);
1349 size_t pjsno = 0;
1351 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1352 sym_hashes = elf_sym_hashes (input_bfd);
1353 relend = relocs + input_section->reloc_count;
1355 for (rel = relocs; rel < relend; rel ++)
1357 reloc_howto_type *howto;
1358 unsigned long r_symndx;
1359 Elf_Internal_Sym *sym;
1360 asection *sec;
1361 struct elf_link_hash_entry *h;
1362 bfd_vma relocation;
1363 bfd_reloc_status_type r;
1364 const char *name = NULL;
1365 int r_type;
1366 bfd_boolean undefined_signalled = FALSE;
1368 r_type = ELF64_R_TYPE (rel->r_info);
1370 if (r_type == R_MMIX_GNU_VTINHERIT
1371 || r_type == R_MMIX_GNU_VTENTRY)
1372 continue;
1374 r_symndx = ELF64_R_SYM (rel->r_info);
1376 if (info->relocatable)
1378 /* This is a relocatable link. For most relocs we don't have to
1379 change anything, unless the reloc is against a section
1380 symbol, in which case we have to adjust according to where
1381 the section symbol winds up in the output section. */
1382 if (r_symndx < symtab_hdr->sh_info)
1384 sym = local_syms + r_symndx;
1386 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1388 sec = local_sections [r_symndx];
1389 rel->r_addend += sec->output_offset + sym->st_value;
1393 /* For PUSHJ stub relocs however, we may need to change the
1394 reloc and the section contents, if the reloc doesn't reach
1395 beyond the end of the output section and previous stubs.
1396 Then we change the section contents to be a PUSHJ to the end
1397 of the input section plus stubs (we can do that without using
1398 a reloc), and then we change the reloc to be a R_MMIX_PUSHJ
1399 at the stub location. */
1400 if (r_type == R_MMIX_PUSHJ_STUBBABLE)
1402 /* We've already checked whether we need a stub; use that
1403 knowledge. */
1404 if (mmix_elf_section_data (input_section)->pjs.stub_size[pjsno]
1405 != 0)
1407 Elf_Internal_Rela relcpy;
1409 if (mmix_elf_section_data (input_section)
1410 ->pjs.stub_size[pjsno] != MAX_PUSHJ_STUB_SIZE)
1411 abort ();
1413 /* There's already a PUSHJ insn there, so just fill in
1414 the offset bits to the stub. */
1415 if (mmix_final_link_relocate (elf_mmix_howto_table
1416 + R_MMIX_ADDR19,
1417 input_section,
1418 contents,
1419 rel->r_offset,
1421 input_section
1422 ->output_section->vma
1423 + input_section->output_offset
1424 + raw_size
1425 + mmix_elf_section_data (input_section)
1426 ->pjs.stub_offset,
1427 NULL, NULL) != bfd_reloc_ok)
1428 return FALSE;
1430 /* Put a JMP insn at the stub; it goes with the
1431 R_MMIX_JMP reloc. */
1432 bfd_put_32 (output_bfd, JMP_INSN_BYTE << 24,
1433 contents
1434 + raw_size
1435 + mmix_elf_section_data (input_section)
1436 ->pjs.stub_offset);
1438 /* Change the reloc to be at the stub, and to a full
1439 R_MMIX_JMP reloc. */
1440 rel->r_info = ELF64_R_INFO (r_symndx, R_MMIX_JMP);
1441 rel->r_offset
1442 = (raw_size
1443 + mmix_elf_section_data (input_section)
1444 ->pjs.stub_offset);
1446 mmix_elf_section_data (input_section)->pjs.stub_offset
1447 += MAX_PUSHJ_STUB_SIZE;
1449 /* Shift this reloc to the end of the relocs to maintain
1450 the r_offset sorted reloc order. */
1451 relcpy = *rel;
1452 memmove (rel, rel + 1, (char *) relend - (char *) rel);
1453 relend[-1] = relcpy;
1455 /* Back up one reloc, or else we'd skip the next reloc
1456 in turn. */
1457 rel--;
1460 pjsno++;
1462 continue;
1465 /* This is a final link. */
1466 howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
1467 h = NULL;
1468 sym = NULL;
1469 sec = NULL;
1471 if (r_symndx < symtab_hdr->sh_info)
1473 sym = local_syms + r_symndx;
1474 sec = local_sections [r_symndx];
1475 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1477 name = bfd_elf_string_from_elf_section (input_bfd,
1478 symtab_hdr->sh_link,
1479 sym->st_name);
1480 if (name == NULL)
1481 name = bfd_section_name (input_bfd, sec);
1483 else
1485 bfd_boolean unresolved_reloc;
1487 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1488 r_symndx, symtab_hdr, sym_hashes,
1489 h, sec, relocation,
1490 unresolved_reloc, undefined_signalled);
1491 name = h->root.root.string;
1494 r = mmix_final_link_relocate (howto, input_section,
1495 contents, rel->r_offset,
1496 rel->r_addend, relocation, name, sec);
1498 if (r != bfd_reloc_ok)
1500 bfd_boolean check_ok = TRUE;
1501 const char * msg = (const char *) NULL;
1503 switch (r)
1505 case bfd_reloc_overflow:
1506 check_ok = info->callbacks->reloc_overflow
1507 (info, name, howto->name, (bfd_vma) 0,
1508 input_bfd, input_section, rel->r_offset);
1509 break;
1511 case bfd_reloc_undefined:
1512 /* We may have sent this message above. */
1513 if (! undefined_signalled)
1514 check_ok = info->callbacks->undefined_symbol
1515 (info, name, input_bfd, input_section, rel->r_offset,
1516 TRUE);
1517 undefined_signalled = TRUE;
1518 break;
1520 case bfd_reloc_outofrange:
1521 msg = _("internal error: out of range error");
1522 break;
1524 case bfd_reloc_notsupported:
1525 msg = _("internal error: unsupported relocation error");
1526 break;
1528 case bfd_reloc_dangerous:
1529 msg = _("internal error: dangerous relocation");
1530 break;
1532 default:
1533 msg = _("internal error: unknown error");
1534 break;
1537 if (msg)
1538 check_ok = info->callbacks->warning
1539 (info, msg, name, input_bfd, input_section, rel->r_offset);
1541 if (! check_ok)
1542 return FALSE;
1546 return TRUE;
1549 /* Perform a single relocation. By default we use the standard BFD
1550 routines. A few relocs we have to do ourselves. */
1552 static bfd_reloc_status_type
1553 mmix_final_link_relocate (howto, input_section, contents,
1554 r_offset, r_addend, relocation, symname, symsec)
1555 reloc_howto_type *howto;
1556 asection *input_section;
1557 bfd_byte *contents;
1558 bfd_vma r_offset;
1559 bfd_signed_vma r_addend;
1560 bfd_vma relocation;
1561 const char *symname;
1562 asection *symsec;
1564 bfd_reloc_status_type r = bfd_reloc_ok;
1565 bfd_vma addr
1566 = (input_section->output_section->vma
1567 + input_section->output_offset
1568 + r_offset);
1569 bfd_signed_vma srel
1570 = (bfd_signed_vma) relocation + r_addend;
1572 switch (howto->type)
1574 /* All these are PC-relative. */
1575 case R_MMIX_PUSHJ_STUBBABLE:
1576 case R_MMIX_PUSHJ:
1577 case R_MMIX_CBRANCH:
1578 case R_MMIX_ADDR19:
1579 case R_MMIX_GETA:
1580 case R_MMIX_ADDR27:
1581 case R_MMIX_JMP:
1582 contents += r_offset;
1584 srel -= (input_section->output_section->vma
1585 + input_section->output_offset
1586 + r_offset);
1588 r = mmix_elf_perform_relocation (input_section, howto, contents,
1589 addr, srel);
1590 break;
1592 case R_MMIX_BASE_PLUS_OFFSET:
1593 if (symsec == NULL)
1594 return bfd_reloc_undefined;
1596 /* Check that we're not relocating against a register symbol. */
1597 if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1598 MMIX_REG_CONTENTS_SECTION_NAME) == 0
1599 || strcmp (bfd_get_section_name (symsec->owner, symsec),
1600 MMIX_REG_SECTION_NAME) == 0)
1602 /* Note: This is separated out into two messages in order
1603 to ease the translation into other languages. */
1604 if (symname == NULL || *symname == 0)
1605 (*_bfd_error_handler)
1606 (_("%s: base-plus-offset relocation against register symbol: (unknown) in %s"),
1607 bfd_get_filename (input_section->owner),
1608 bfd_get_section_name (symsec->owner, symsec));
1609 else
1610 (*_bfd_error_handler)
1611 (_("%s: base-plus-offset relocation against register symbol: %s in %s"),
1612 bfd_get_filename (input_section->owner), symname,
1613 bfd_get_section_name (symsec->owner, symsec));
1614 return bfd_reloc_overflow;
1616 goto do_mmix_reloc;
1618 case R_MMIX_REG_OR_BYTE:
1619 case R_MMIX_REG:
1620 /* For now, we handle these alike. They must refer to an register
1621 symbol, which is either relative to the register section and in
1622 the range 0..255, or is in the register contents section with vma
1623 regno * 8. */
1625 /* FIXME: A better way to check for reg contents section?
1626 FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1627 if (symsec == NULL)
1628 return bfd_reloc_undefined;
1630 if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1631 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1633 if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1635 /* The bfd_reloc_outofrange return value, though intuitively
1636 a better value, will not get us an error. */
1637 return bfd_reloc_overflow;
1639 srel /= 8;
1641 else if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1642 MMIX_REG_SECTION_NAME) == 0)
1644 if (srel < 0 || srel > 255)
1645 /* The bfd_reloc_outofrange return value, though intuitively a
1646 better value, will not get us an error. */
1647 return bfd_reloc_overflow;
1649 else
1651 /* Note: This is separated out into two messages in order
1652 to ease the translation into other languages. */
1653 if (symname == NULL || *symname == 0)
1654 (*_bfd_error_handler)
1655 (_("%s: register relocation against non-register symbol: (unknown) in %s"),
1656 bfd_get_filename (input_section->owner),
1657 bfd_get_section_name (symsec->owner, symsec));
1658 else
1659 (*_bfd_error_handler)
1660 (_("%s: register relocation against non-register symbol: %s in %s"),
1661 bfd_get_filename (input_section->owner), symname,
1662 bfd_get_section_name (symsec->owner, symsec));
1664 /* The bfd_reloc_outofrange return value, though intuitively a
1665 better value, will not get us an error. */
1666 return bfd_reloc_overflow;
1668 do_mmix_reloc:
1669 contents += r_offset;
1670 r = mmix_elf_perform_relocation (input_section, howto, contents,
1671 addr, srel);
1672 break;
1674 case R_MMIX_LOCAL:
1675 /* This isn't a real relocation, it's just an assertion that the
1676 final relocation value corresponds to a local register. We
1677 ignore the actual relocation; nothing is changed. */
1679 asection *regsec
1680 = bfd_get_section_by_name (input_section->output_section->owner,
1681 MMIX_REG_CONTENTS_SECTION_NAME);
1682 bfd_vma first_global;
1684 /* Check that this is an absolute value, or a reference to the
1685 register contents section or the register (symbol) section.
1686 Absolute numbers can get here as undefined section. Undefined
1687 symbols are signalled elsewhere, so there's no conflict in us
1688 accidentally handling it. */
1689 if (!bfd_is_abs_section (symsec)
1690 && !bfd_is_und_section (symsec)
1691 && strcmp (bfd_get_section_name (symsec->owner, symsec),
1692 MMIX_REG_CONTENTS_SECTION_NAME) != 0
1693 && strcmp (bfd_get_section_name (symsec->owner, symsec),
1694 MMIX_REG_SECTION_NAME) != 0)
1696 (*_bfd_error_handler)
1697 (_("%s: directive LOCAL valid only with a register or absolute value"),
1698 bfd_get_filename (input_section->owner));
1700 return bfd_reloc_overflow;
1703 /* If we don't have a register contents section, then $255 is the
1704 first global register. */
1705 if (regsec == NULL)
1706 first_global = 255;
1707 else
1709 first_global = bfd_get_section_vma (abfd, regsec) / 8;
1710 if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1711 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1713 if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1714 /* The bfd_reloc_outofrange return value, though
1715 intuitively a better value, will not get us an error. */
1716 return bfd_reloc_overflow;
1717 srel /= 8;
1721 if ((bfd_vma) srel >= first_global)
1723 /* FIXME: Better error message. */
1724 (*_bfd_error_handler)
1725 (_("%s: LOCAL directive: Register $%ld is not a local register. First global register is $%ld."),
1726 bfd_get_filename (input_section->owner), (long) srel, (long) first_global);
1728 return bfd_reloc_overflow;
1731 r = bfd_reloc_ok;
1732 break;
1734 default:
1735 r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
1736 contents, r_offset,
1737 relocation, r_addend);
1740 return r;
1743 /* Return the section that should be marked against GC for a given
1744 relocation. */
1746 static asection *
1747 mmix_elf_gc_mark_hook (sec, info, rel, h, sym)
1748 asection *sec;
1749 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1750 Elf_Internal_Rela *rel;
1751 struct elf_link_hash_entry *h;
1752 Elf_Internal_Sym *sym;
1754 if (h != NULL)
1756 switch (ELF64_R_TYPE (rel->r_info))
1758 case R_MMIX_GNU_VTINHERIT:
1759 case R_MMIX_GNU_VTENTRY:
1760 break;
1762 default:
1763 switch (h->root.type)
1765 case bfd_link_hash_defined:
1766 case bfd_link_hash_defweak:
1767 return h->root.u.def.section;
1769 case bfd_link_hash_common:
1770 return h->root.u.c.p->section;
1772 default:
1773 break;
1777 else
1778 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1780 return NULL;
1783 /* Update relocation info for a GC-excluded section. We could supposedly
1784 perform the allocation after GC, but there's no suitable hook between
1785 GC (or section merge) and the point when all input sections must be
1786 present. Better to waste some memory and (perhaps) a little time. */
1788 static bfd_boolean
1789 mmix_elf_gc_sweep_hook (abfd, info, sec, relocs)
1790 bfd *abfd ATTRIBUTE_UNUSED;
1791 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1792 asection *sec ATTRIBUTE_UNUSED;
1793 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1795 struct bpo_reloc_section_info *bpodata
1796 = mmix_elf_section_data (sec)->bpo.reloc;
1797 asection *allocated_gregs_section;
1799 /* If no bpodata here, we have nothing to do. */
1800 if (bpodata == NULL)
1801 return TRUE;
1803 allocated_gregs_section = bpodata->bpo_greg_section;
1805 mmix_elf_section_data (allocated_gregs_section)->bpo.greg->n_bpo_relocs
1806 -= bpodata->n_bpo_relocs_this_section;
1808 return TRUE;
1811 /* Sort register relocs to come before expanding relocs. */
1813 static int
1814 mmix_elf_sort_relocs (p1, p2)
1815 const PTR p1;
1816 const PTR p2;
1818 const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
1819 const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
1820 int r1_is_reg, r2_is_reg;
1822 /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1823 insns. */
1824 if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
1825 return 1;
1826 else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
1827 return -1;
1829 r1_is_reg
1830 = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
1831 || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
1832 r2_is_reg
1833 = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
1834 || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
1835 if (r1_is_reg != r2_is_reg)
1836 return r2_is_reg - r1_is_reg;
1838 /* Neither or both are register relocs. Then sort on full offset. */
1839 if (r1->r_offset > r2->r_offset)
1840 return 1;
1841 else if (r1->r_offset < r2->r_offset)
1842 return -1;
1843 return 0;
1846 /* Subset of mmix_elf_check_relocs, common to ELF and mmo linking. */
1848 static bfd_boolean
1849 mmix_elf_check_common_relocs (abfd, info, sec, relocs)
1850 bfd *abfd;
1851 struct bfd_link_info *info;
1852 asection *sec;
1853 const Elf_Internal_Rela *relocs;
1855 bfd *bpo_greg_owner = NULL;
1856 asection *allocated_gregs_section = NULL;
1857 struct bpo_greg_section_info *gregdata = NULL;
1858 struct bpo_reloc_section_info *bpodata = NULL;
1859 const Elf_Internal_Rela *rel;
1860 const Elf_Internal_Rela *rel_end;
1862 /* We currently have to abuse this COFF-specific member, since there's
1863 no target-machine-dedicated member. There's no alternative outside
1864 the bfd_link_info struct; we can't specialize a hash-table since
1865 they're different between ELF and mmo. */
1866 bpo_greg_owner = (bfd *) info->base_file;
1868 rel_end = relocs + sec->reloc_count;
1869 for (rel = relocs; rel < rel_end; rel++)
1871 switch (ELF64_R_TYPE (rel->r_info))
1873 /* This relocation causes a GREG allocation. We need to count
1874 them, and we need to create a section for them, so we need an
1875 object to fake as the owner of that section. We can't use
1876 the ELF dynobj for this, since the ELF bits assume lots of
1877 DSO-related stuff if that member is non-NULL. */
1878 case R_MMIX_BASE_PLUS_OFFSET:
1879 /* We don't do anything with this reloc for a relocatable link. */
1880 if (info->relocatable)
1881 break;
1883 if (bpo_greg_owner == NULL)
1885 bpo_greg_owner = abfd;
1886 info->base_file = (PTR) bpo_greg_owner;
1889 if (allocated_gregs_section == NULL)
1890 allocated_gregs_section
1891 = bfd_get_section_by_name (bpo_greg_owner,
1892 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
1894 if (allocated_gregs_section == NULL)
1896 allocated_gregs_section
1897 = bfd_make_section (bpo_greg_owner,
1898 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
1899 /* Setting both SEC_ALLOC and SEC_LOAD means the section is
1900 treated like any other section, and we'd get errors for
1901 address overlap with the text section. Let's set none of
1902 those flags, as that is what currently happens for usual
1903 GREG allocations, and that works. */
1904 if (allocated_gregs_section == NULL
1905 || !bfd_set_section_flags (bpo_greg_owner,
1906 allocated_gregs_section,
1907 (SEC_HAS_CONTENTS
1908 | SEC_IN_MEMORY
1909 | SEC_LINKER_CREATED))
1910 || !bfd_set_section_alignment (bpo_greg_owner,
1911 allocated_gregs_section,
1913 return FALSE;
1915 gregdata = (struct bpo_greg_section_info *)
1916 bfd_zalloc (bpo_greg_owner, sizeof (struct bpo_greg_section_info));
1917 if (gregdata == NULL)
1918 return FALSE;
1919 mmix_elf_section_data (allocated_gregs_section)->bpo.greg
1920 = gregdata;
1922 else if (gregdata == NULL)
1923 gregdata
1924 = mmix_elf_section_data (allocated_gregs_section)->bpo.greg;
1926 /* Get ourselves some auxiliary info for the BPO-relocs. */
1927 if (bpodata == NULL)
1929 /* No use doing a separate iteration pass to find the upper
1930 limit - just use the number of relocs. */
1931 bpodata = (struct bpo_reloc_section_info *)
1932 bfd_alloc (bpo_greg_owner,
1933 sizeof (struct bpo_reloc_section_info)
1934 * (sec->reloc_count + 1));
1935 if (bpodata == NULL)
1936 return FALSE;
1937 mmix_elf_section_data (sec)->bpo.reloc = bpodata;
1938 bpodata->first_base_plus_offset_reloc
1939 = bpodata->bpo_index
1940 = gregdata->n_max_bpo_relocs;
1941 bpodata->bpo_greg_section
1942 = allocated_gregs_section;
1943 bpodata->n_bpo_relocs_this_section = 0;
1946 bpodata->n_bpo_relocs_this_section++;
1947 gregdata->n_max_bpo_relocs++;
1949 /* We don't get another chance to set this before GC; we've not
1950 set up any hook that runs before GC. */
1951 gregdata->n_bpo_relocs
1952 = gregdata->n_max_bpo_relocs;
1953 break;
1955 case R_MMIX_PUSHJ_STUBBABLE:
1956 mmix_elf_section_data (sec)->pjs.n_pushj_relocs++;
1957 break;
1961 /* Allocate per-reloc stub storage and initialize it to the max stub
1962 size. */
1963 if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs != 0)
1965 size_t i;
1967 mmix_elf_section_data (sec)->pjs.stub_size
1968 = bfd_alloc (abfd, mmix_elf_section_data (sec)->pjs.n_pushj_relocs
1969 * sizeof (mmix_elf_section_data (sec)
1970 ->pjs.stub_size[0]));
1971 if (mmix_elf_section_data (sec)->pjs.stub_size == NULL)
1972 return FALSE;
1974 for (i = 0; i < mmix_elf_section_data (sec)->pjs.n_pushj_relocs; i++)
1975 mmix_elf_section_data (sec)->pjs.stub_size[i] = MAX_PUSHJ_STUB_SIZE;
1978 return TRUE;
1981 /* Look through the relocs for a section during the first phase. */
1983 static bfd_boolean
1984 mmix_elf_check_relocs (abfd, info, sec, relocs)
1985 bfd *abfd;
1986 struct bfd_link_info *info;
1987 asection *sec;
1988 const Elf_Internal_Rela *relocs;
1990 Elf_Internal_Shdr *symtab_hdr;
1991 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
1992 const Elf_Internal_Rela *rel;
1993 const Elf_Internal_Rela *rel_end;
1995 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1996 sym_hashes = elf_sym_hashes (abfd);
1997 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf64_External_Sym);
1998 if (!elf_bad_symtab (abfd))
1999 sym_hashes_end -= symtab_hdr->sh_info;
2001 /* First we sort the relocs so that any register relocs come before
2002 expansion-relocs to the same insn. FIXME: Not done for mmo. */
2003 qsort ((PTR) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
2004 mmix_elf_sort_relocs);
2006 /* Do the common part. */
2007 if (!mmix_elf_check_common_relocs (abfd, info, sec, relocs))
2008 return FALSE;
2010 if (info->relocatable)
2011 return TRUE;
2013 rel_end = relocs + sec->reloc_count;
2014 for (rel = relocs; rel < rel_end; rel++)
2016 struct elf_link_hash_entry *h;
2017 unsigned long r_symndx;
2019 r_symndx = ELF64_R_SYM (rel->r_info);
2020 if (r_symndx < symtab_hdr->sh_info)
2021 h = NULL;
2022 else
2023 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2025 switch (ELF64_R_TYPE (rel->r_info))
2027 /* This relocation describes the C++ object vtable hierarchy.
2028 Reconstruct it for later use during GC. */
2029 case R_MMIX_GNU_VTINHERIT:
2030 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2031 return FALSE;
2032 break;
2034 /* This relocation describes which C++ vtable entries are actually
2035 used. Record for later use during GC. */
2036 case R_MMIX_GNU_VTENTRY:
2037 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2038 return FALSE;
2039 break;
2043 return TRUE;
2046 /* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
2047 Copied from elf_link_add_object_symbols. */
2049 bfd_boolean
2050 _bfd_mmix_check_all_relocs (abfd, info)
2051 bfd *abfd;
2052 struct bfd_link_info *info;
2054 asection *o;
2056 for (o = abfd->sections; o != NULL; o = o->next)
2058 Elf_Internal_Rela *internal_relocs;
2059 bfd_boolean ok;
2061 if ((o->flags & SEC_RELOC) == 0
2062 || o->reloc_count == 0
2063 || ((info->strip == strip_all || info->strip == strip_debugger)
2064 && (o->flags & SEC_DEBUGGING) != 0)
2065 || bfd_is_abs_section (o->output_section))
2066 continue;
2068 internal_relocs
2069 = _bfd_elf_link_read_relocs (abfd, o, (PTR) NULL,
2070 (Elf_Internal_Rela *) NULL,
2071 info->keep_memory);
2072 if (internal_relocs == NULL)
2073 return FALSE;
2075 ok = mmix_elf_check_common_relocs (abfd, info, o, internal_relocs);
2077 if (! info->keep_memory)
2078 free (internal_relocs);
2080 if (! ok)
2081 return FALSE;
2084 return TRUE;
2087 /* Change symbols relative to the reg contents section to instead be to
2088 the register section, and scale them down to correspond to the register
2089 number. */
2091 static bfd_boolean
2092 mmix_elf_link_output_symbol_hook (info, name, sym, input_sec, h)
2093 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2094 const char *name ATTRIBUTE_UNUSED;
2095 Elf_Internal_Sym *sym;
2096 asection *input_sec;
2097 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED;
2099 if (input_sec != NULL
2100 && input_sec->name != NULL
2101 && ELF_ST_TYPE (sym->st_info) != STT_SECTION
2102 && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2104 sym->st_value /= 8;
2105 sym->st_shndx = SHN_REGISTER;
2108 return TRUE;
2111 /* We fake a register section that holds values that are register numbers.
2112 Having a SHN_REGISTER and register section translates better to other
2113 formats (e.g. mmo) than for example a STT_REGISTER attribute.
2114 This section faking is based on a construct in elf32-mips.c. */
2115 static asection mmix_elf_reg_section;
2116 static asymbol mmix_elf_reg_section_symbol;
2117 static asymbol *mmix_elf_reg_section_symbol_ptr;
2119 /* Handle the special section numbers that a symbol may use. */
2121 void
2122 mmix_elf_symbol_processing (abfd, asym)
2123 bfd *abfd ATTRIBUTE_UNUSED;
2124 asymbol *asym;
2126 elf_symbol_type *elfsym;
2128 elfsym = (elf_symbol_type *) asym;
2129 switch (elfsym->internal_elf_sym.st_shndx)
2131 case SHN_REGISTER:
2132 if (mmix_elf_reg_section.name == NULL)
2134 /* Initialize the register section. */
2135 mmix_elf_reg_section.name = MMIX_REG_SECTION_NAME;
2136 mmix_elf_reg_section.flags = SEC_NO_FLAGS;
2137 mmix_elf_reg_section.output_section = &mmix_elf_reg_section;
2138 mmix_elf_reg_section.symbol = &mmix_elf_reg_section_symbol;
2139 mmix_elf_reg_section.symbol_ptr_ptr = &mmix_elf_reg_section_symbol_ptr;
2140 mmix_elf_reg_section_symbol.name = MMIX_REG_SECTION_NAME;
2141 mmix_elf_reg_section_symbol.flags = BSF_SECTION_SYM;
2142 mmix_elf_reg_section_symbol.section = &mmix_elf_reg_section;
2143 mmix_elf_reg_section_symbol_ptr = &mmix_elf_reg_section_symbol;
2145 asym->section = &mmix_elf_reg_section;
2146 break;
2148 default:
2149 break;
2153 /* Given a BFD section, try to locate the corresponding ELF section
2154 index. */
2156 static bfd_boolean
2157 mmix_elf_section_from_bfd_section (abfd, sec, retval)
2158 bfd * abfd ATTRIBUTE_UNUSED;
2159 asection * sec;
2160 int * retval;
2162 if (strcmp (bfd_get_section_name (abfd, sec), MMIX_REG_SECTION_NAME) == 0)
2163 *retval = SHN_REGISTER;
2164 else
2165 return FALSE;
2167 return TRUE;
2170 /* Hook called by the linker routine which adds symbols from an object
2171 file. We must handle the special SHN_REGISTER section number here.
2173 We also check that we only have *one* each of the section-start
2174 symbols, since otherwise having two with the same value would cause
2175 them to be "merged", but with the contents serialized. */
2177 bfd_boolean
2178 mmix_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2179 bfd *abfd;
2180 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2181 Elf_Internal_Sym *sym;
2182 const char **namep ATTRIBUTE_UNUSED;
2183 flagword *flagsp ATTRIBUTE_UNUSED;
2184 asection **secp;
2185 bfd_vma *valp ATTRIBUTE_UNUSED;
2187 if (sym->st_shndx == SHN_REGISTER)
2188 *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2189 else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
2190 && strncmp (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
2191 strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)) == 0)
2193 /* See if we have another one. */
2194 struct bfd_link_hash_entry *h = bfd_link_hash_lookup (info->hash,
2195 *namep,
2196 FALSE,
2197 FALSE,
2198 FALSE);
2200 if (h != NULL && h->type != bfd_link_hash_undefined)
2202 /* How do we get the asymbol (or really: the filename) from h?
2203 h->u.def.section->owner is NULL. */
2204 ((*_bfd_error_handler)
2205 (_("%s: Error: multiple definition of `%s'; start of %s is set in a earlier linked file\n"),
2206 bfd_get_filename (abfd), *namep,
2207 *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)));
2208 bfd_set_error (bfd_error_bad_value);
2209 return FALSE;
2213 return TRUE;
2216 /* We consider symbols matching "L.*:[0-9]+" to be local symbols. */
2218 bfd_boolean
2219 mmix_elf_is_local_label_name (abfd, name)
2220 bfd *abfd;
2221 const char *name;
2223 const char *colpos;
2224 int digits;
2226 /* Also include the default local-label definition. */
2227 if (_bfd_elf_is_local_label_name (abfd, name))
2228 return TRUE;
2230 if (*name != 'L')
2231 return FALSE;
2233 /* If there's no ":", or more than one, it's not a local symbol. */
2234 colpos = strchr (name, ':');
2235 if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
2236 return FALSE;
2238 /* Check that there are remaining characters and that they are digits. */
2239 if (colpos[1] == 0)
2240 return FALSE;
2242 digits = strspn (colpos + 1, "0123456789");
2243 return digits != 0 && colpos[1 + digits] == 0;
2246 /* We get rid of the register section here. */
2248 bfd_boolean
2249 mmix_elf_final_link (abfd, info)
2250 bfd *abfd;
2251 struct bfd_link_info *info;
2253 /* We never output a register section, though we create one for
2254 temporary measures. Check that nobody entered contents into it. */
2255 asection *reg_section;
2256 asection **secpp;
2258 reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
2260 if (reg_section != NULL)
2262 /* FIXME: Pass error state gracefully. */
2263 if (bfd_get_section_flags (abfd, reg_section) & SEC_HAS_CONTENTS)
2264 _bfd_abort (__FILE__, __LINE__, _("Register section has contents\n"));
2266 /* Really remove the section. */
2267 for (secpp = &abfd->sections;
2268 *secpp != reg_section;
2269 secpp = &(*secpp)->next)
2271 bfd_section_list_remove (abfd, secpp);
2272 --abfd->section_count;
2275 if (! bfd_elf_final_link (abfd, info))
2276 return FALSE;
2278 /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
2279 the regular linker machinery. We do it here, like other targets with
2280 special sections. */
2281 if (info->base_file != NULL)
2283 asection *greg_section
2284 = bfd_get_section_by_name ((bfd *) info->base_file,
2285 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2286 if (!bfd_set_section_contents (abfd,
2287 greg_section->output_section,
2288 greg_section->contents,
2289 (file_ptr) greg_section->output_offset,
2290 greg_section->_cooked_size))
2291 return FALSE;
2293 return TRUE;
2296 /* We need to include the maximum size of PUSHJ-stubs in the initial
2297 section size. This is expected to shrink during linker relaxation.
2299 You might think that we should set *only* _cooked_size, but that won't
2300 work: section contents allocation will be using _raw_size in mixed
2301 format linking and not enough storage will be allocated. FIXME: That's
2302 a major bug, including the name bfd_get_section_size_before_reloc; it
2303 should be bfd_get_section_size_before_relax. The relaxation functions
2304 set _cooked size. Relaxation happens before relocation. All functions
2305 *after relaxation* should be using _cooked size. */
2307 static void
2308 mmix_set_relaxable_size (abfd, sec, ptr)
2309 bfd *abfd ATTRIBUTE_UNUSED;
2310 asection *sec;
2311 void *ptr;
2313 struct bfd_link_info *info = ptr;
2315 /* Make sure we only do this for section where we know we want this,
2316 otherwise we might end up resetting the size of COMMONs. */
2317 if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0)
2318 return;
2320 sec->_cooked_size
2321 = (sec->_raw_size
2322 + mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2323 * MAX_PUSHJ_STUB_SIZE);
2324 sec->_raw_size = sec->_cooked_size;
2326 /* For use in relocatable link, we start with a max stubs size. See
2327 mmix_elf_relax_section. */
2328 if (info->relocatable && sec->output_section)
2329 mmix_elf_section_data (sec->output_section)->pjs.stubs_size_sum
2330 += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2331 * MAX_PUSHJ_STUB_SIZE);
2334 /* Initialize stuff for the linker-generated GREGs to match
2335 R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker. */
2337 bfd_boolean
2338 _bfd_mmix_before_linker_allocation (abfd, info)
2339 bfd *abfd ATTRIBUTE_UNUSED;
2340 struct bfd_link_info *info;
2342 asection *bpo_gregs_section;
2343 bfd *bpo_greg_owner;
2344 struct bpo_greg_section_info *gregdata;
2345 size_t n_gregs;
2346 bfd_vma gregs_size;
2347 size_t i;
2348 size_t *bpo_reloc_indexes;
2349 bfd *ibfd;
2351 /* Set the initial size of sections. */
2352 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2353 bfd_map_over_sections (ibfd, mmix_set_relaxable_size, info);
2355 /* The bpo_greg_owner bfd is supposed to have been set by
2356 mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
2357 If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2358 bpo_greg_owner = (bfd *) info->base_file;
2359 if (bpo_greg_owner == NULL)
2360 return TRUE;
2362 bpo_gregs_section
2363 = bfd_get_section_by_name (bpo_greg_owner,
2364 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2366 if (bpo_gregs_section == NULL)
2367 return TRUE;
2369 /* We use the target-data handle in the ELF section data. */
2370 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2371 if (gregdata == NULL)
2372 return FALSE;
2374 n_gregs = gregdata->n_bpo_relocs;
2375 gregdata->n_allocated_bpo_gregs = n_gregs;
2377 /* When this reaches zero during relaxation, all entries have been
2378 filled in and the size of the linker gregs can be calculated. */
2379 gregdata->n_remaining_bpo_relocs_this_relaxation_round = n_gregs;
2381 /* Set the zeroth-order estimate for the GREGs size. */
2382 gregs_size = n_gregs * 8;
2384 if (!bfd_set_section_size (bpo_greg_owner, bpo_gregs_section, gregs_size))
2385 return FALSE;
2387 /* Allocate and set up the GREG arrays. They're filled in at relaxation
2388 time. Note that we must use the max number ever noted for the array,
2389 since the index numbers were created before GC. */
2390 gregdata->reloc_request
2391 = bfd_zalloc (bpo_greg_owner,
2392 sizeof (struct bpo_reloc_request)
2393 * gregdata->n_max_bpo_relocs);
2395 gregdata->bpo_reloc_indexes
2396 = bpo_reloc_indexes
2397 = bfd_alloc (bpo_greg_owner,
2398 gregdata->n_max_bpo_relocs
2399 * sizeof (size_t));
2400 if (bpo_reloc_indexes == NULL)
2401 return FALSE;
2403 /* The default order is an identity mapping. */
2404 for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2406 bpo_reloc_indexes[i] = i;
2407 gregdata->reloc_request[i].bpo_reloc_no = i;
2410 return TRUE;
2413 /* Fill in contents in the linker allocated gregs. Everything is
2414 calculated at this point; we just move the contents into place here. */
2416 bfd_boolean
2417 _bfd_mmix_after_linker_allocation (abfd, link_info)
2418 bfd *abfd ATTRIBUTE_UNUSED;
2419 struct bfd_link_info *link_info;
2421 asection *bpo_gregs_section;
2422 bfd *bpo_greg_owner;
2423 struct bpo_greg_section_info *gregdata;
2424 size_t n_gregs;
2425 size_t i, j;
2426 size_t lastreg;
2427 bfd_byte *contents;
2429 /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
2430 when the first R_MMIX_BASE_PLUS_OFFSET is seen. If there is no such
2431 object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2432 bpo_greg_owner = (bfd *) link_info->base_file;
2433 if (bpo_greg_owner == NULL)
2434 return TRUE;
2436 bpo_gregs_section
2437 = bfd_get_section_by_name (bpo_greg_owner,
2438 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2440 /* This can't happen without DSO handling. When DSOs are handled
2441 without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
2442 section. */
2443 if (bpo_gregs_section == NULL)
2444 return TRUE;
2446 /* We use the target-data handle in the ELF section data. */
2448 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2449 if (gregdata == NULL)
2450 return FALSE;
2452 n_gregs = gregdata->n_allocated_bpo_gregs;
2454 /* We need to have a _raw_size contents even though there's only
2455 _cooked_size worth of data, since the generic relocation machinery
2456 will allocate and copy that much temporarily. */
2457 bpo_gregs_section->contents
2458 = contents = bfd_alloc (bpo_greg_owner, bpo_gregs_section->_raw_size);
2459 if (contents == NULL)
2460 return FALSE;
2462 /* Sanity check: If these numbers mismatch, some relocation has not been
2463 accounted for and the rest of gregdata is probably inconsistent.
2464 It's a bug, but it's more helpful to identify it than segfaulting
2465 below. */
2466 if (gregdata->n_remaining_bpo_relocs_this_relaxation_round
2467 != gregdata->n_bpo_relocs)
2469 (*_bfd_error_handler)
2470 (_("Internal inconsistency: remaining %u != max %u.\n\
2471 Please report this bug."),
2472 gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2473 gregdata->n_bpo_relocs);
2474 return FALSE;
2477 for (lastreg = 255, i = 0, j = 0; j < n_gregs; i++)
2478 if (gregdata->reloc_request[i].regindex != lastreg)
2480 bfd_put_64 (bpo_greg_owner, gregdata->reloc_request[i].value,
2481 contents + j * 8);
2482 lastreg = gregdata->reloc_request[i].regindex;
2483 j++;
2486 return TRUE;
2489 /* Sort valid relocs to come before non-valid relocs, then on increasing
2490 value. */
2492 static int
2493 bpo_reloc_request_sort_fn (p1, p2)
2494 const PTR p1;
2495 const PTR p2;
2497 const struct bpo_reloc_request *r1 = (const struct bpo_reloc_request *) p1;
2498 const struct bpo_reloc_request *r2 = (const struct bpo_reloc_request *) p2;
2500 /* Primary function is validity; non-valid relocs sorted after valid
2501 ones. */
2502 if (r1->valid != r2->valid)
2503 return r2->valid - r1->valid;
2505 /* Then sort on value. Don't simplify and return just the difference of
2506 the values: the upper bits of the 64-bit value would be truncated on
2507 a host with 32-bit ints. */
2508 if (r1->value != r2->value)
2509 return r1->value > r2->value ? 1 : -1;
2511 /* As a last re-sort, use the relocation number, so we get a stable
2512 sort. The *addresses* aren't stable since items are swapped during
2513 sorting. It depends on the qsort implementation if this actually
2514 happens. */
2515 return r1->bpo_reloc_no > r2->bpo_reloc_no
2516 ? 1 : (r1->bpo_reloc_no < r2->bpo_reloc_no ? -1 : 0);
2519 /* For debug use only. Dumps the global register allocations resulting
2520 from base-plus-offset relocs. */
2522 void
2523 mmix_dump_bpo_gregs (link_info, pf)
2524 struct bfd_link_info *link_info;
2525 bfd_error_handler_type pf;
2527 bfd *bpo_greg_owner;
2528 asection *bpo_gregs_section;
2529 struct bpo_greg_section_info *gregdata;
2530 unsigned int i;
2532 if (link_info == NULL || link_info->base_file == NULL)
2533 return;
2535 bpo_greg_owner = (bfd *) link_info->base_file;
2537 bpo_gregs_section
2538 = bfd_get_section_by_name (bpo_greg_owner,
2539 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2541 if (bpo_gregs_section == NULL)
2542 return;
2544 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2545 if (gregdata == NULL)
2546 return;
2548 if (pf == NULL)
2549 pf = _bfd_error_handler;
2551 /* These format strings are not translated. They are for debug purposes
2552 only and never displayed to an end user. Should they escape, we
2553 surely want them in original. */
2554 (*pf) (" n_bpo_relocs: %u\n n_max_bpo_relocs: %u\n n_remain...round: %u\n\
2555 n_allocated_bpo_gregs: %u\n", gregdata->n_bpo_relocs,
2556 gregdata->n_max_bpo_relocs,
2557 gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2558 gregdata->n_allocated_bpo_gregs);
2560 if (gregdata->reloc_request)
2561 for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2562 (*pf) ("%4u (%4u)/%4u#%u: 0x%08lx%08lx r: %3u o: %3u\n",
2564 (gregdata->bpo_reloc_indexes != NULL
2565 ? gregdata->bpo_reloc_indexes[i] : (size_t) -1),
2566 gregdata->reloc_request[i].bpo_reloc_no,
2567 gregdata->reloc_request[i].valid,
2569 (unsigned long) (gregdata->reloc_request[i].value >> 32),
2570 (unsigned long) gregdata->reloc_request[i].value,
2571 gregdata->reloc_request[i].regindex,
2572 gregdata->reloc_request[i].offset);
2575 /* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
2576 when the last such reloc is done, an index-array is sorted according to
2577 the values and iterated over to produce register numbers (indexed by 0
2578 from the first allocated register number) and offsets for use in real
2579 relocation.
2581 PUSHJ stub accounting is also done here.
2583 Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
2585 static bfd_boolean
2586 mmix_elf_relax_section (abfd, sec, link_info, again)
2587 bfd *abfd;
2588 asection *sec;
2589 struct bfd_link_info *link_info;
2590 bfd_boolean *again;
2592 Elf_Internal_Shdr *symtab_hdr;
2593 Elf_Internal_Rela *internal_relocs;
2594 Elf_Internal_Rela *irel, *irelend;
2595 asection *bpo_gregs_section = NULL;
2596 struct bpo_greg_section_info *gregdata;
2597 struct bpo_reloc_section_info *bpodata
2598 = mmix_elf_section_data (sec)->bpo.reloc;
2599 /* The initialization is to quiet compiler warnings. The value is to
2600 spot a missing actual initialization. */
2601 size_t bpono = (size_t) -1;
2602 size_t pjsno = 0;
2603 bfd *bpo_greg_owner;
2604 Elf_Internal_Sym *isymbuf = NULL;
2605 bfd_size_type raw_size
2606 = (sec->_raw_size
2607 - mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2608 * MAX_PUSHJ_STUB_SIZE);
2610 mmix_elf_section_data (sec)->pjs.stubs_size_sum = 0;
2612 /* Assume nothing changes. */
2613 *again = FALSE;
2615 /* If this is the first time we have been called for this section,
2616 initialize the cooked size. */
2617 if (sec->_cooked_size == 0 && sec->_raw_size != 0)
2618 abort ();
2620 /* We don't have to do anything if this section does not have relocs, or
2621 if this is not a code section. */
2622 if ((sec->flags & SEC_RELOC) == 0
2623 || sec->reloc_count == 0
2624 || (sec->flags & SEC_CODE) == 0
2625 || (sec->flags & SEC_LINKER_CREATED) != 0
2626 /* If no R_MMIX_BASE_PLUS_OFFSET relocs and no PUSHJ-stub relocs,
2627 then nothing to do. */
2628 || (bpodata == NULL
2629 && mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0))
2630 return TRUE;
2632 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2634 bpo_greg_owner = (bfd *) link_info->base_file;
2636 if (bpodata != NULL)
2638 bpo_gregs_section = bpodata->bpo_greg_section;
2639 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2640 bpono = bpodata->first_base_plus_offset_reloc;
2642 else
2643 gregdata = NULL;
2645 /* Get a copy of the native relocations. */
2646 internal_relocs
2647 = _bfd_elf_link_read_relocs (abfd, sec, (PTR) NULL,
2648 (Elf_Internal_Rela *) NULL,
2649 link_info->keep_memory);
2650 if (internal_relocs == NULL)
2651 goto error_return;
2653 /* Walk through them looking for relaxing opportunities. */
2654 irelend = internal_relocs + sec->reloc_count;
2655 for (irel = internal_relocs; irel < irelend; irel++)
2657 bfd_vma symval;
2658 struct elf_link_hash_entry *h = NULL;
2660 /* We only process two relocs. */
2661 if (ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_BASE_PLUS_OFFSET
2662 && ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_PUSHJ_STUBBABLE)
2663 continue;
2665 /* We process relocs in a distinctly different way when this is a
2666 relocatable link (for one, we don't look at symbols), so we avoid
2667 mixing its code with that for the "normal" relaxation. */
2668 if (link_info->relocatable)
2670 /* The only transformation in a relocatable link is to generate
2671 a full stub at the location of the stub calculated for the
2672 input section, if the relocated stub location, the end of the
2673 output section plus earlier stubs, cannot be reached. Thus
2674 relocatable linking can only lead to worse code, but it still
2675 works. */
2676 if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
2678 /* If we can reach the end of the output-section and beyond
2679 any current stubs, then we don't need a stub for this
2680 reloc. The relaxed order of output stub allocation may
2681 not exactly match the straightforward order, so we always
2682 assume presence of output stubs, which will allow
2683 relaxation only on relocations indifferent to the
2684 presence of output stub allocations for other relocations
2685 and thus the order of output stub allocation. */
2686 if (bfd_check_overflow (complain_overflow_signed,
2689 bfd_arch_bits_per_address (abfd),
2690 /* Output-stub location. */
2691 sec->output_section->_cooked_size
2692 + (mmix_elf_section_data (sec
2693 ->output_section)
2694 ->pjs.stubs_size_sum)
2695 /* Location of this PUSHJ reloc. */
2696 - (sec->output_offset + irel->r_offset)
2697 /* Don't count *this* stub twice. */
2698 - (mmix_elf_section_data (sec)
2699 ->pjs.stub_size[pjsno]
2700 + MAX_PUSHJ_STUB_SIZE))
2701 == bfd_reloc_ok)
2702 mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2704 mmix_elf_section_data (sec)->pjs.stubs_size_sum
2705 += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2707 pjsno++;
2710 continue;
2713 /* Get the value of the symbol referred to by the reloc. */
2714 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2716 /* A local symbol. */
2717 Elf_Internal_Sym *isym;
2718 asection *sym_sec;
2720 /* Read this BFD's local symbols if we haven't already. */
2721 if (isymbuf == NULL)
2723 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2724 if (isymbuf == NULL)
2725 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2726 symtab_hdr->sh_info, 0,
2727 NULL, NULL, NULL);
2728 if (isymbuf == 0)
2729 goto error_return;
2732 isym = isymbuf + ELF64_R_SYM (irel->r_info);
2733 if (isym->st_shndx == SHN_UNDEF)
2734 sym_sec = bfd_und_section_ptr;
2735 else if (isym->st_shndx == SHN_ABS)
2736 sym_sec = bfd_abs_section_ptr;
2737 else if (isym->st_shndx == SHN_COMMON)
2738 sym_sec = bfd_com_section_ptr;
2739 else
2740 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2741 symval = (isym->st_value
2742 + sym_sec->output_section->vma
2743 + sym_sec->output_offset);
2745 else
2747 unsigned long indx;
2749 /* An external symbol. */
2750 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2751 h = elf_sym_hashes (abfd)[indx];
2752 BFD_ASSERT (h != NULL);
2753 if (h->root.type != bfd_link_hash_defined
2754 && h->root.type != bfd_link_hash_defweak)
2756 /* This appears to be a reference to an undefined symbol. Just
2757 ignore it--it will be caught by the regular reloc processing.
2758 We need to keep BPO reloc accounting consistent, though
2759 else we'll abort instead of emitting an error message. */
2760 if (ELF64_R_TYPE (irel->r_info) == R_MMIX_BASE_PLUS_OFFSET
2761 && gregdata != NULL)
2763 gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2764 bpono++;
2766 continue;
2769 symval = (h->root.u.def.value
2770 + h->root.u.def.section->output_section->vma
2771 + h->root.u.def.section->output_offset);
2774 if (ELF64_R_TYPE (irel->r_info) == (int) R_MMIX_PUSHJ_STUBBABLE)
2776 bfd_vma value = symval + irel->r_addend;
2777 bfd_vma dot
2778 = (sec->output_section->vma
2779 + sec->output_offset
2780 + irel->r_offset);
2781 bfd_vma stubaddr
2782 = (sec->output_section->vma
2783 + sec->output_offset
2784 + raw_size
2785 + mmix_elf_section_data (sec)->pjs.stubs_size_sum);
2787 if ((value & 3) == 0
2788 && bfd_check_overflow (complain_overflow_signed,
2791 bfd_arch_bits_per_address (abfd),
2792 value - dot
2793 - (value > dot
2794 ? mmix_elf_section_data (sec)
2795 ->pjs.stub_size[pjsno]
2796 : 0))
2797 == bfd_reloc_ok)
2798 /* If the reloc fits, no stub is needed. */
2799 mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2800 else
2801 /* Maybe we can get away with just a JMP insn? */
2802 if ((value & 3) == 0
2803 && bfd_check_overflow (complain_overflow_signed,
2806 bfd_arch_bits_per_address (abfd),
2807 value - stubaddr
2808 - (value > dot
2809 ? mmix_elf_section_data (sec)
2810 ->pjs.stub_size[pjsno] - 4
2811 : 0))
2812 == bfd_reloc_ok)
2813 /* Yep, account for a stub consisting of a single JMP insn. */
2814 mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 4;
2815 else
2816 /* Nope, go for the full insn stub. It doesn't seem useful to
2817 emit the intermediate sizes; those will only be useful for
2818 a >64M program assuming contiguous code. */
2819 mmix_elf_section_data (sec)->pjs.stub_size[pjsno]
2820 = MAX_PUSHJ_STUB_SIZE;
2822 mmix_elf_section_data (sec)->pjs.stubs_size_sum
2823 += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2824 pjsno++;
2825 continue;
2828 /* We're looking at a R_MMIX_BASE_PLUS_OFFSET reloc. */
2830 gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono]].value
2831 = symval + irel->r_addend;
2832 gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono++]].valid = TRUE;
2833 gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2836 /* Check if that was the last BPO-reloc. If so, sort the values and
2837 calculate how many registers we need to cover them. Set the size of
2838 the linker gregs, and if the number of registers changed, indicate
2839 that we need to relax some more because we have more work to do. */
2840 if (gregdata != NULL
2841 && gregdata->n_remaining_bpo_relocs_this_relaxation_round == 0)
2843 size_t i;
2844 bfd_vma prev_base;
2845 size_t regindex;
2847 /* First, reset the remaining relocs for the next round. */
2848 gregdata->n_remaining_bpo_relocs_this_relaxation_round
2849 = gregdata->n_bpo_relocs;
2851 qsort ((PTR) gregdata->reloc_request,
2852 gregdata->n_max_bpo_relocs,
2853 sizeof (struct bpo_reloc_request),
2854 bpo_reloc_request_sort_fn);
2856 /* Recalculate indexes. When we find a change (however unlikely
2857 after the initial iteration), we know we need to relax again,
2858 since items in the GREG-array are sorted by increasing value and
2859 stored in the relaxation phase. */
2860 for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2861 if (gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2862 != i)
2864 gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2865 = i;
2866 *again = TRUE;
2869 /* Allocate register numbers (indexing from 0). Stop at the first
2870 non-valid reloc. */
2871 for (i = 0, regindex = 0, prev_base = gregdata->reloc_request[0].value;
2872 i < gregdata->n_bpo_relocs;
2873 i++)
2875 if (gregdata->reloc_request[i].value > prev_base + 255)
2877 regindex++;
2878 prev_base = gregdata->reloc_request[i].value;
2880 gregdata->reloc_request[i].regindex = regindex;
2881 gregdata->reloc_request[i].offset
2882 = gregdata->reloc_request[i].value - prev_base;
2885 /* If it's not the same as the last time, we need to relax again,
2886 because the size of the section has changed. I'm not sure we
2887 actually need to do any adjustments since the shrinking happens
2888 at the start of this section, but better safe than sorry. */
2889 if (gregdata->n_allocated_bpo_gregs != regindex + 1)
2891 gregdata->n_allocated_bpo_gregs = regindex + 1;
2892 *again = TRUE;
2895 bpo_gregs_section->_cooked_size = (regindex + 1) * 8;
2898 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
2900 if (! link_info->keep_memory)
2901 free (isymbuf);
2902 else
2904 /* Cache the symbols for elf_link_input_bfd. */
2905 symtab_hdr->contents = (unsigned char *) isymbuf;
2909 if (internal_relocs != NULL
2910 && elf_section_data (sec)->relocs != internal_relocs)
2911 free (internal_relocs);
2913 if (sec->_cooked_size
2914 < raw_size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
2915 abort ();
2917 if (sec->_cooked_size
2918 > raw_size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
2920 sec->_cooked_size
2921 = raw_size + mmix_elf_section_data (sec)->pjs.stubs_size_sum;
2922 *again = TRUE;
2925 return TRUE;
2927 error_return:
2928 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
2929 free (isymbuf);
2930 if (internal_relocs != NULL
2931 && elf_section_data (sec)->relocs != internal_relocs)
2932 free (internal_relocs);
2933 return FALSE;
2936 /* Because we set _raw_size to include the max size of pushj stubs,
2937 i.e. larger than the actual section input size (see
2938 mmix_set_relaxable_raw_size), we have to take care of that when reading
2939 the section. */
2941 static bfd_boolean
2942 mmix_elf_get_section_contents (abfd, section, location, offset, count)
2943 bfd *abfd;
2944 sec_ptr section;
2945 void *location;
2946 file_ptr offset;
2947 bfd_size_type count;
2949 bfd_size_type raw_size
2950 = (section->_raw_size
2951 - mmix_elf_section_data (section)->pjs.n_pushj_relocs
2952 * MAX_PUSHJ_STUB_SIZE);
2954 if (offset + count > section->_raw_size)
2956 abort();
2957 bfd_set_error (bfd_error_invalid_operation);
2958 return FALSE;
2961 /* Check bounds against the faked raw_size. */
2962 if (offset + count > raw_size)
2964 /* Clear the part in the faked area. */
2965 memset (location + raw_size - offset, 0, count - (raw_size - offset));
2967 /* If there's no initial part within the "real" contents, we're
2968 done. */
2969 if ((bfd_size_type) offset >= raw_size)
2970 return TRUE;
2972 /* Else adjust the count and fall through to call the generic
2973 function. */
2974 count = raw_size - offset;
2977 return
2978 _bfd_generic_get_section_contents (abfd, section, location, offset,
2979 count);
2983 #define ELF_ARCH bfd_arch_mmix
2984 #define ELF_MACHINE_CODE EM_MMIX
2986 /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
2987 However, that's too much for something somewhere in the linker part of
2988 BFD; perhaps the start-address has to be a non-zero multiple of this
2989 number, or larger than this number. The symptom is that the linker
2990 complains: "warning: allocated section `.text' not in segment". We
2991 settle for 64k; the page-size used in examples is 8k.
2992 #define ELF_MAXPAGESIZE 0x10000
2994 Unfortunately, this causes excessive padding in the supposedly small
2995 for-education programs that are the expected usage (where people would
2996 inspect output). We stick to 256 bytes just to have *some* default
2997 alignment. */
2998 #define ELF_MAXPAGESIZE 0x100
3000 #define TARGET_BIG_SYM bfd_elf64_mmix_vec
3001 #define TARGET_BIG_NAME "elf64-mmix"
3003 #define elf_info_to_howto_rel NULL
3004 #define elf_info_to_howto mmix_info_to_howto_rela
3005 #define elf_backend_relocate_section mmix_elf_relocate_section
3006 #define elf_backend_gc_mark_hook mmix_elf_gc_mark_hook
3007 #define elf_backend_gc_sweep_hook mmix_elf_gc_sweep_hook
3009 #define elf_backend_link_output_symbol_hook \
3010 mmix_elf_link_output_symbol_hook
3011 #define elf_backend_add_symbol_hook mmix_elf_add_symbol_hook
3013 #define elf_backend_check_relocs mmix_elf_check_relocs
3014 #define elf_backend_symbol_processing mmix_elf_symbol_processing
3016 #define bfd_elf64_bfd_is_local_label_name \
3017 mmix_elf_is_local_label_name
3019 #define elf_backend_may_use_rel_p 0
3020 #define elf_backend_may_use_rela_p 1
3021 #define elf_backend_default_use_rela_p 1
3023 #define elf_backend_can_gc_sections 1
3024 #define elf_backend_section_from_bfd_section \
3025 mmix_elf_section_from_bfd_section
3027 #define bfd_elf64_new_section_hook mmix_elf_new_section_hook
3028 #define bfd_elf64_bfd_final_link mmix_elf_final_link
3029 #define bfd_elf64_bfd_relax_section mmix_elf_relax_section
3030 #define bfd_elf64_get_section_contents mmix_elf_get_section_contents
3032 #include "elf64-target.h"