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