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