]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-mmix.c
Add MMIX support
[thirdparty/binutils-gdb.git] / bfd / elf64-mmix.c
CommitLineData
3c3bdf30
NC
1/* MMIX-specific support for 64-bit ELF.
2 Copyright (C) 2001 Free Software Foundation, Inc.
3 Contributed by Hans-Peter Nilsson <hp@bitrange.com>
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/* No specific ABI or "processor-specific supplement" defined. */
22
23/* TODO:
24 - Linker relaxation.
25 - On-demand register allocation (from R_MMIX_BASE_PLUS_OFFSET). */
26
27#include "bfd.h"
28#include "sysdep.h"
29#include "libbfd.h"
30#include "elf-bfd.h"
31#include "elf/mmix.h"
32#include "opcode/mmix.h"
33
34#define MINUS_ONE (((bfd_vma) 0) - 1)
35
36/* Put these everywhere in new code. */
37#define FATAL_DEBUG \
38 _bfd_abort (__FILE__, __LINE__, \
39 "Internal: Non-debugged code (test-case missing)")
40
41#define BAD_CASE(x) \
42 _bfd_abort (__FILE__, __LINE__, \
43 "bad case for " #x)
44
45static boolean mmix_elf_link_output_symbol_hook
46 PARAMS ((bfd *, struct bfd_link_info *, const char *,
47 Elf_Internal_Sym *, asection *));
48
49static bfd_reloc_status_type mmix_elf_reloc
50 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
51
52static reloc_howto_type *bfd_elf64_bfd_reloc_type_lookup
53 PARAMS ((bfd *, bfd_reloc_code_real_type));
54
55static void mmix_info_to_howto_rela
56 PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
57
58static int mmix_elf_sort_relocs PARAMS ((const PTR, const PTR));
59
60static boolean mmix_elf_check_relocs
61 PARAMS ((bfd *, struct bfd_link_info *, asection *,
62 const Elf_Internal_Rela *));
63
64static boolean mmix_elf_relocate_section
65 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
66 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
67
68static asection * mmix_elf_gc_mark_hook
69 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
70 struct elf_link_hash_entry *, Elf_Internal_Sym *));
71
72static bfd_reloc_status_type mmix_final_link_relocate
73 PARAMS ((reloc_howto_type *, asection *, bfd_byte *,
74 bfd_vma, bfd_signed_vma, bfd_vma, const char *, asection *));
75
76static bfd_reloc_status_type mmix_elf_perform_relocation
77 PARAMS ((asection *, reloc_howto_type *, PTR, bfd_vma, bfd_vma));
78
79static boolean mmix_elf_section_from_bfd_section
80 PARAMS ((bfd *, Elf64_Internal_Shdr *, asection *, int *));
81
82static boolean mmix_elf_add_symbol_hook
83 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
84 const char **, flagword *, asection **, bfd_vma *));
85
86static boolean mmix_elf_is_local_label_name
87 PARAMS ((bfd *, const char *));
88
89extern boolean mmix_elf_final_link PARAMS ((bfd *, struct bfd_link_info *));
90
91extern void mmix_elf_symbol_processing PARAMS ((bfd *, asymbol *));
92
93/* Watch out: this currently needs to have elements with the same index as
94 their R_MMIX_ number. */
95static reloc_howto_type elf_mmix_howto_table[] =
96 {
97 /* This reloc does nothing. */
98 HOWTO (R_MMIX_NONE, /* type */
99 0, /* rightshift */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
101 32, /* bitsize */
102 false, /* pc_relative */
103 0, /* bitpos */
104 complain_overflow_bitfield, /* complain_on_overflow */
105 bfd_elf_generic_reloc, /* special_function */
106 "R_MMIX_NONE", /* name */
107 false, /* partial_inplace */
108 0, /* src_mask */
109 0, /* dst_mask */
110 false), /* pcrel_offset */
111
112 /* An 8 bit absolute relocation. */
113 HOWTO (R_MMIX_8, /* type */
114 0, /* rightshift */
115 0, /* size (0 = byte, 1 = short, 2 = long) */
116 8, /* bitsize */
117 false, /* pc_relative */
118 0, /* bitpos */
119 complain_overflow_bitfield, /* complain_on_overflow */
120 bfd_elf_generic_reloc, /* special_function */
121 "R_MMIX_8", /* name */
122 false, /* partial_inplace */
123 0xff, /* src_mask */
124 0xff, /* dst_mask */
125 false), /* pcrel_offset */
126
127 /* An 16 bit absolute relocation. */
128 HOWTO (R_MMIX_16, /* type */
129 0, /* rightshift */
130 1, /* size (0 = byte, 1 = short, 2 = long) */
131 16, /* bitsize */
132 false, /* pc_relative */
133 0, /* bitpos */
134 complain_overflow_bitfield, /* complain_on_overflow */
135 bfd_elf_generic_reloc, /* special_function */
136 "R_MMIX_16", /* name */
137 false, /* partial_inplace */
138 0xffff, /* src_mask */
139 0xffff, /* dst_mask */
140 false), /* pcrel_offset */
141
142 /* An 24 bit absolute relocation. */
143 HOWTO (R_MMIX_24, /* type */
144 0, /* rightshift */
145 2, /* size (0 = byte, 1 = short, 2 = long) */
146 24, /* bitsize */
147 false, /* pc_relative */
148 0, /* bitpos */
149 complain_overflow_bitfield, /* complain_on_overflow */
150 bfd_elf_generic_reloc, /* special_function */
151 "R_MMIX_24", /* name */
152 false, /* partial_inplace */
153 0xffffff, /* src_mask */
154 0xffffff, /* dst_mask */
155 false), /* pcrel_offset */
156
157 /* A 32 bit absolute relocation. */
158 HOWTO (R_MMIX_32, /* type */
159 0, /* rightshift */
160 2, /* size (0 = byte, 1 = short, 2 = long) */
161 32, /* bitsize */
162 false, /* pc_relative */
163 0, /* bitpos */
164 complain_overflow_bitfield, /* complain_on_overflow */
165 bfd_elf_generic_reloc, /* special_function */
166 "R_MMIX_32", /* name */
167 false, /* partial_inplace */
168 0xffffffff, /* src_mask */
169 0xffffffff, /* dst_mask */
170 false), /* pcrel_offset */
171
172 /* 64 bit relocation. */
173 HOWTO (R_MMIX_64, /* type */
174 0, /* rightshift */
175 4, /* size (0 = byte, 1 = short, 2 = long) */
176 64, /* bitsize */
177 false, /* pc_relative */
178 0, /* bitpos */
179 complain_overflow_bitfield, /* complain_on_overflow */
180 bfd_elf_generic_reloc, /* special_function */
181 "R_MMIX_64", /* name */
182 false, /* partial_inplace */
183 MINUS_ONE, /* src_mask */
184 MINUS_ONE, /* dst_mask */
185 false), /* pcrel_offset */
186
187 /* An 8 bit PC-relative relocation. */
188 HOWTO (R_MMIX_PC_8, /* type */
189 0, /* rightshift */
190 0, /* size (0 = byte, 1 = short, 2 = long) */
191 8, /* bitsize */
192 true, /* pc_relative */
193 0, /* bitpos */
194 complain_overflow_bitfield, /* complain_on_overflow */
195 bfd_elf_generic_reloc, /* special_function */
196 "R_MMIX_PC_8", /* name */
197 false, /* partial_inplace */
198 0xff, /* src_mask */
199 0xff, /* dst_mask */
200 true), /* pcrel_offset */
201
202 /* An 16 bit PC-relative relocation. */
203 HOWTO (R_MMIX_PC_16, /* type */
204 0, /* rightshift */
205 1, /* size (0 = byte, 1 = short, 2 = long) */
206 16, /* bitsize */
207 true, /* pc_relative */
208 0, /* bitpos */
209 complain_overflow_bitfield, /* complain_on_overflow */
210 bfd_elf_generic_reloc, /* special_function */
211 "R_MMIX_PC_16", /* name */
212 false, /* partial_inplace */
213 0xffff, /* src_mask */
214 0xffff, /* dst_mask */
215 true), /* pcrel_offset */
216
217 /* An 24 bit PC-relative relocation. */
218 HOWTO (R_MMIX_PC_24, /* type */
219 0, /* rightshift */
220 2, /* size (0 = byte, 1 = short, 2 = long) */
221 24, /* bitsize */
222 true, /* pc_relative */
223 0, /* bitpos */
224 complain_overflow_bitfield, /* complain_on_overflow */
225 bfd_elf_generic_reloc, /* special_function */
226 "R_MMIX_PC_24", /* name */
227 false, /* partial_inplace */
228 0xffffff, /* src_mask */
229 0xffffff, /* dst_mask */
230 true), /* pcrel_offset */
231
232 /* A 32 bit absolute PC-relative relocation. */
233 HOWTO (R_MMIX_PC_32, /* type */
234 0, /* rightshift */
235 2, /* size (0 = byte, 1 = short, 2 = long) */
236 32, /* bitsize */
237 true, /* pc_relative */
238 0, /* bitpos */
239 complain_overflow_bitfield, /* complain_on_overflow */
240 bfd_elf_generic_reloc, /* special_function */
241 "R_MMIX_PC_32", /* name */
242 false, /* partial_inplace */
243 0xffffffff, /* src_mask */
244 0xffffffff, /* dst_mask */
245 true), /* pcrel_offset */
246
247 /* 64 bit PC-relative relocation. */
248 HOWTO (R_MMIX_PC_64, /* type */
249 0, /* rightshift */
250 4, /* size (0 = byte, 1 = short, 2 = long) */
251 64, /* bitsize */
252 true, /* pc_relative */
253 0, /* bitpos */
254 complain_overflow_bitfield, /* complain_on_overflow */
255 bfd_elf_generic_reloc, /* special_function */
256 "R_MMIX_PC_64", /* name */
257 false, /* partial_inplace */
258 MINUS_ONE, /* src_mask */
259 MINUS_ONE, /* dst_mask */
260 true), /* pcrel_offset */
261
262 /* GNU extension to record C++ vtable hierarchy. */
263 HOWTO (R_MMIX_GNU_VTINHERIT, /* type */
264 0, /* rightshift */
265 0, /* size (0 = byte, 1 = short, 2 = long) */
266 0, /* bitsize */
267 false, /* pc_relative */
268 0, /* bitpos */
269 complain_overflow_dont, /* complain_on_overflow */
270 NULL, /* special_function */
271 "R_MMIX_GNU_VTINHERIT", /* name */
272 false, /* partial_inplace */
273 0, /* src_mask */
274 0, /* dst_mask */
275 true), /* pcrel_offset */
276
277 /* GNU extension to record C++ vtable member usage. */
278 HOWTO (R_MMIX_GNU_VTENTRY, /* type */
279 0, /* rightshift */
280 0, /* size (0 = byte, 1 = short, 2 = long) */
281 0, /* bitsize */
282 false, /* pc_relative */
283 0, /* bitpos */
284 complain_overflow_dont, /* complain_on_overflow */
285 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
286 "R_MMIX_GNU_VTENTRY", /* name */
287 false, /* partial_inplace */
288 0, /* src_mask */
289 0, /* dst_mask */
290 false), /* pcrel_offset */
291
292 /* The GETA relocation is supposed to get any address that could
293 possibly be reached by the GETA instruction. It can silently expand
294 to get a 64-bit operand, but will complain if any of the two least
295 significant bits are set. The howto members reflect a simple GETA. */
296 HOWTO (R_MMIX_GETA, /* type */
297 2, /* rightshift */
298 2, /* size (0 = byte, 1 = short, 2 = long) */
299 19, /* bitsize */
300 true, /* pc_relative */
301 0, /* bitpos */
302 complain_overflow_signed, /* complain_on_overflow */
303 mmix_elf_reloc, /* special_function */
304 "R_MMIX_GETA", /* name */
305 false, /* partial_inplace */
306 0x0100ffff, /* src_mask */
307 0x0100ffff, /* dst_mask */
308 true), /* pcrel_offset */
309
310 HOWTO (R_MMIX_GETA_1, /* type */
311 2, /* rightshift */
312 2, /* size (0 = byte, 1 = short, 2 = long) */
313 19, /* bitsize */
314 true, /* pc_relative */
315 0, /* bitpos */
316 complain_overflow_signed, /* complain_on_overflow */
317 mmix_elf_reloc, /* special_function */
318 "R_MMIX_GETA_1", /* name */
319 false, /* partial_inplace */
320 0x0100ffff, /* src_mask */
321 0x0100ffff, /* dst_mask */
322 true), /* pcrel_offset */
323
324 HOWTO (R_MMIX_GETA_2, /* type */
325 2, /* rightshift */
326 2, /* size (0 = byte, 1 = short, 2 = long) */
327 19, /* bitsize */
328 true, /* pc_relative */
329 0, /* bitpos */
330 complain_overflow_signed, /* complain_on_overflow */
331 mmix_elf_reloc, /* special_function */
332 "R_MMIX_GETA_2", /* name */
333 false, /* partial_inplace */
334 0x0100ffff, /* src_mask */
335 0x0100ffff, /* dst_mask */
336 true), /* pcrel_offset */
337
338 HOWTO (R_MMIX_GETA_3, /* type */
339 2, /* rightshift */
340 2, /* size (0 = byte, 1 = short, 2 = long) */
341 19, /* bitsize */
342 true, /* pc_relative */
343 0, /* bitpos */
344 complain_overflow_signed, /* complain_on_overflow */
345 mmix_elf_reloc, /* special_function */
346 "R_MMIX_GETA_3", /* name */
347 false, /* partial_inplace */
348 0x0100ffff, /* src_mask */
349 0x0100ffff, /* dst_mask */
350 true), /* pcrel_offset */
351
352 /* The conditional branches are supposed to reach any (code) address.
353 It can silently expand to a 64-bit operand, but will emit an error if
354 any of the two least significant bits are set. The howto members
355 reflect a simple branch. */
356 HOWTO (R_MMIX_CBRANCH, /* type */
357 2, /* rightshift */
358 2, /* size (0 = byte, 1 = short, 2 = long) */
359 19, /* bitsize */
360 true, /* pc_relative */
361 0, /* bitpos */
362 complain_overflow_signed, /* complain_on_overflow */
363 mmix_elf_reloc, /* special_function */
364 "R_MMIX_CBRANCH", /* name */
365 false, /* partial_inplace */
366 0x0100ffff, /* src_mask */
367 0x0100ffff, /* dst_mask */
368 true), /* pcrel_offset */
369
370 HOWTO (R_MMIX_CBRANCH_J, /* type */
371 2, /* rightshift */
372 2, /* size (0 = byte, 1 = short, 2 = long) */
373 19, /* bitsize */
374 true, /* pc_relative */
375 0, /* bitpos */
376 complain_overflow_signed, /* complain_on_overflow */
377 mmix_elf_reloc, /* special_function */
378 "R_MMIX_CBRANCH_J", /* name */
379 false, /* partial_inplace */
380 0x0100ffff, /* src_mask */
381 0x0100ffff, /* dst_mask */
382 true), /* pcrel_offset */
383
384 HOWTO (R_MMIX_CBRANCH_1, /* type */
385 2, /* rightshift */
386 2, /* size (0 = byte, 1 = short, 2 = long) */
387 19, /* bitsize */
388 true, /* pc_relative */
389 0, /* bitpos */
390 complain_overflow_signed, /* complain_on_overflow */
391 mmix_elf_reloc, /* special_function */
392 "R_MMIX_CBRANCH_1", /* name */
393 false, /* partial_inplace */
394 0x0100ffff, /* src_mask */
395 0x0100ffff, /* dst_mask */
396 true), /* pcrel_offset */
397
398 HOWTO (R_MMIX_CBRANCH_2, /* type */
399 2, /* rightshift */
400 2, /* size (0 = byte, 1 = short, 2 = long) */
401 19, /* bitsize */
402 true, /* pc_relative */
403 0, /* bitpos */
404 complain_overflow_signed, /* complain_on_overflow */
405 mmix_elf_reloc, /* special_function */
406 "R_MMIX_CBRANCH_2", /* name */
407 false, /* partial_inplace */
408 0x0100ffff, /* src_mask */
409 0x0100ffff, /* dst_mask */
410 true), /* pcrel_offset */
411
412 HOWTO (R_MMIX_CBRANCH_3, /* type */
413 2, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 19, /* bitsize */
416 true, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_signed, /* complain_on_overflow */
419 mmix_elf_reloc, /* special_function */
420 "R_MMIX_CBRANCH_3", /* name */
421 false, /* partial_inplace */
422 0x0100ffff, /* src_mask */
423 0x0100ffff, /* dst_mask */
424 true), /* pcrel_offset */
425
426 /* The PUSHJ instruction can reach any (code) address, as long as it's
427 the beginning of a function (no usable restriction). It can silently
428 expand to a 64-bit operand, but will emit an error if any of the two
429 least significant bits are set. The howto members reflect a simple
430 PUSHJ. */
431 HOWTO (R_MMIX_PUSHJ, /* type */
432 2, /* rightshift */
433 2, /* size (0 = byte, 1 = short, 2 = long) */
434 19, /* bitsize */
435 true, /* pc_relative */
436 0, /* bitpos */
437 complain_overflow_signed, /* complain_on_overflow */
438 mmix_elf_reloc, /* special_function */
439 "R_MMIX_PUSHJ", /* name */
440 false, /* partial_inplace */
441 0x0100ffff, /* src_mask */
442 0x0100ffff, /* dst_mask */
443 true), /* pcrel_offset */
444
445 HOWTO (R_MMIX_PUSHJ_1, /* type */
446 2, /* rightshift */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
448 19, /* bitsize */
449 true, /* pc_relative */
450 0, /* bitpos */
451 complain_overflow_signed, /* complain_on_overflow */
452 mmix_elf_reloc, /* special_function */
453 "R_MMIX_PUSHJ_1", /* name */
454 false, /* partial_inplace */
455 0x0100ffff, /* src_mask */
456 0x0100ffff, /* dst_mask */
457 true), /* pcrel_offset */
458
459 HOWTO (R_MMIX_PUSHJ_2, /* type */
460 2, /* rightshift */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
462 19, /* bitsize */
463 true, /* pc_relative */
464 0, /* bitpos */
465 complain_overflow_signed, /* complain_on_overflow */
466 mmix_elf_reloc, /* special_function */
467 "R_MMIX_PUSHJ_2", /* name */
468 false, /* partial_inplace */
469 0x0100ffff, /* src_mask */
470 0x0100ffff, /* dst_mask */
471 true), /* pcrel_offset */
472
473 HOWTO (R_MMIX_PUSHJ_3, /* type */
474 2, /* rightshift */
475 2, /* size (0 = byte, 1 = short, 2 = long) */
476 19, /* bitsize */
477 true, /* pc_relative */
478 0, /* bitpos */
479 complain_overflow_signed, /* complain_on_overflow */
480 mmix_elf_reloc, /* special_function */
481 "R_MMIX_PUSHJ_3", /* name */
482 false, /* partial_inplace */
483 0x0100ffff, /* src_mask */
484 0x0100ffff, /* dst_mask */
485 true), /* pcrel_offset */
486
487 /* A JMP is supposed to reach any (code) address. By itself, it can
488 reach +-64M; the expansion can reach all 64 bits. Note that the 64M
489 limit is soon reached if you link the program in wildly different
490 memory segments. The howto members reflect a trivial JMP. */
491 HOWTO (R_MMIX_JMP, /* type */
492 2, /* rightshift */
493 2, /* size (0 = byte, 1 = short, 2 = long) */
494 27, /* bitsize */
495 true, /* pc_relative */
496 0, /* bitpos */
497 complain_overflow_signed, /* complain_on_overflow */
498 mmix_elf_reloc, /* special_function */
499 "R_MMIX_JMP", /* name */
500 false, /* partial_inplace */
501 0x1ffffff, /* src_mask */
502 0x1ffffff, /* dst_mask */
503 true), /* pcrel_offset */
504
505 HOWTO (R_MMIX_JMP_1, /* type */
506 2, /* rightshift */
507 2, /* size (0 = byte, 1 = short, 2 = long) */
508 27, /* bitsize */
509 true, /* pc_relative */
510 0, /* bitpos */
511 complain_overflow_signed, /* complain_on_overflow */
512 mmix_elf_reloc, /* special_function */
513 "R_MMIX_JMP_1", /* name */
514 false, /* partial_inplace */
515 0x1ffffff, /* src_mask */
516 0x1ffffff, /* dst_mask */
517 true), /* pcrel_offset */
518
519 HOWTO (R_MMIX_JMP_2, /* type */
520 2, /* rightshift */
521 2, /* size (0 = byte, 1 = short, 2 = long) */
522 27, /* bitsize */
523 true, /* pc_relative */
524 0, /* bitpos */
525 complain_overflow_signed, /* complain_on_overflow */
526 mmix_elf_reloc, /* special_function */
527 "R_MMIX_JMP_2", /* name */
528 false, /* partial_inplace */
529 0x1ffffff, /* src_mask */
530 0x1ffffff, /* dst_mask */
531 true), /* pcrel_offset */
532
533 HOWTO (R_MMIX_JMP_3, /* type */
534 2, /* rightshift */
535 2, /* size (0 = byte, 1 = short, 2 = long) */
536 27, /* bitsize */
537 true, /* pc_relative */
538 0, /* bitpos */
539 complain_overflow_signed, /* complain_on_overflow */
540 mmix_elf_reloc, /* special_function */
541 "R_MMIX_JMP_3", /* name */
542 false, /* partial_inplace */
543 0x1ffffff, /* src_mask */
544 0x1ffffff, /* dst_mask */
545 true), /* pcrel_offset */
546
547 /* When we don't emit link-time-relaxable code from the assembler, or
548 when relaxation has done all it can do, these relocs are used. For
549 GETA/PUSHJ/branches. */
550 HOWTO (R_MMIX_ADDR19, /* type */
551 2, /* rightshift */
552 2, /* size (0 = byte, 1 = short, 2 = long) */
553 19, /* bitsize */
554 true, /* pc_relative */
555 0, /* bitpos */
556 complain_overflow_signed, /* complain_on_overflow */
557 mmix_elf_reloc, /* special_function */
558 "R_MMIX_ADDR19", /* name */
559 false, /* partial_inplace */
560 0x0100ffff, /* src_mask */
561 0x0100ffff, /* dst_mask */
562 true), /* pcrel_offset */
563
564 /* For JMP. */
565 HOWTO (R_MMIX_ADDR27, /* type */
566 2, /* rightshift */
567 2, /* size (0 = byte, 1 = short, 2 = long) */
568 27, /* bitsize */
569 true, /* pc_relative */
570 0, /* bitpos */
571 complain_overflow_signed, /* complain_on_overflow */
572 mmix_elf_reloc, /* special_function */
573 "R_MMIX_ADDR27", /* name */
574 false, /* partial_inplace */
575 0x1ffffff, /* src_mask */
576 0x1ffffff, /* dst_mask */
577 true), /* pcrel_offset */
578
579 /* A general register or the value 0..255. If a value, then the
580 instruction (offset -3) needs adjusting. */
581 HOWTO (R_MMIX_REG_OR_BYTE, /* type */
582 0, /* rightshift */
583 1, /* size (0 = byte, 1 = short, 2 = long) */
584 8, /* bitsize */
585 false, /* pc_relative */
586 0, /* bitpos */
587 complain_overflow_bitfield, /* complain_on_overflow */
588 mmix_elf_reloc, /* special_function */
589 "R_MMIX_REG_OR_BYTE", /* name */
590 false, /* partial_inplace */
591 0xff, /* src_mask */
592 0xff, /* dst_mask */
593 false), /* pcrel_offset */
594
595 /* A general register. */
596 HOWTO (R_MMIX_REG, /* type */
597 0, /* rightshift */
598 1, /* size (0 = byte, 1 = short, 2 = long) */
599 8, /* bitsize */
600 false, /* pc_relative */
601 0, /* bitpos */
602 complain_overflow_bitfield, /* complain_on_overflow */
603 mmix_elf_reloc, /* special_function */
604 "R_MMIX_REG", /* name */
605 false, /* partial_inplace */
606 0xff, /* src_mask */
607 0xff, /* dst_mask */
608 false), /* pcrel_offset */
609
610 /* A register plus an index, corresponding to the relocation expression.
611 The sizes must correspond to the valid range of the expression, while
612 the bitmasks correspond to what we store in the image. */
613 HOWTO (R_MMIX_BASE_PLUS_OFFSET, /* type */
614 0, /* rightshift */
615 4, /* size (0 = byte, 1 = short, 2 = long) */
616 64, /* bitsize */
617 false, /* pc_relative */
618 0, /* bitpos */
619 complain_overflow_bitfield, /* complain_on_overflow */
620 mmix_elf_reloc, /* special_function */
621 "R_MMIX_BASE_PLUS_OFFSET", /* name */
622 false, /* partial_inplace */
623 0xffff, /* src_mask */
624 0xffff, /* dst_mask */
625 false), /* pcrel_offset */
626
627 /* A "magic" relocation for a LOCAL expression, asserting that the
628 expression is less than the number of global registers. No actual
629 modification of the contents is done. Implementing this as a
630 relocation was less intrusive than e.g. putting such expressions in a
631 section to discard *after* relocation. */
632 HOWTO (R_MMIX_LOCAL, /* type */
633 0, /* rightshift */
634 0, /* size (0 = byte, 1 = short, 2 = long) */
635 0, /* bitsize */
636 false, /* pc_relative */
637 0, /* bitpos */
638 complain_overflow_dont, /* complain_on_overflow */
639 mmix_elf_reloc, /* special_function */
640 "R_MMIX_LOCAL", /* name */
641 false, /* partial_inplace */
642 0, /* src_mask */
643 0, /* dst_mask */
644 false), /* pcrel_offset */
645 };
646
647
648/* Map BFD reloc types to MMIX ELF reloc types. */
649
650struct mmix_reloc_map
651 {
652 bfd_reloc_code_real_type bfd_reloc_val;
653 enum elf_mmix_reloc_type elf_reloc_val;
654 };
655
656
657static const struct mmix_reloc_map mmix_reloc_map[] =
658 {
659 {BFD_RELOC_NONE, R_MMIX_NONE},
660 {BFD_RELOC_8, R_MMIX_8},
661 {BFD_RELOC_16, R_MMIX_16},
662 {BFD_RELOC_24, R_MMIX_24},
663 {BFD_RELOC_32, R_MMIX_32},
664 {BFD_RELOC_64, R_MMIX_64},
665 {BFD_RELOC_8_PCREL, R_MMIX_PC_8},
666 {BFD_RELOC_16_PCREL, R_MMIX_PC_16},
667 {BFD_RELOC_24_PCREL, R_MMIX_PC_24},
668 {BFD_RELOC_32_PCREL, R_MMIX_PC_32},
669 {BFD_RELOC_64_PCREL, R_MMIX_PC_64},
670 {BFD_RELOC_VTABLE_INHERIT, R_MMIX_GNU_VTINHERIT},
671 {BFD_RELOC_VTABLE_ENTRY, R_MMIX_GNU_VTENTRY},
672 {BFD_RELOC_MMIX_GETA, R_MMIX_GETA},
673 {BFD_RELOC_MMIX_CBRANCH, R_MMIX_CBRANCH},
674 {BFD_RELOC_MMIX_PUSHJ, R_MMIX_PUSHJ},
675 {BFD_RELOC_MMIX_JMP, R_MMIX_JMP},
676 {BFD_RELOC_MMIX_ADDR19, R_MMIX_ADDR19},
677 {BFD_RELOC_MMIX_ADDR27, R_MMIX_ADDR27},
678 {BFD_RELOC_MMIX_REG_OR_BYTE, R_MMIX_REG_OR_BYTE},
679 {BFD_RELOC_MMIX_REG, R_MMIX_REG},
680 {BFD_RELOC_MMIX_BASE_PLUS_OFFSET, R_MMIX_BASE_PLUS_OFFSET},
681 {BFD_RELOC_MMIX_LOCAL, R_MMIX_LOCAL}
682 };
683
684static reloc_howto_type *
685bfd_elf64_bfd_reloc_type_lookup (abfd, code)
686 bfd *abfd ATTRIBUTE_UNUSED;
687 bfd_reloc_code_real_type code;
688{
689 unsigned int i;
690
691 for (i = 0;
692 i < sizeof (mmix_reloc_map) / sizeof (mmix_reloc_map[0]);
693 i++)
694 {
695 if (mmix_reloc_map[i].bfd_reloc_val == code)
696 return &elf_mmix_howto_table[mmix_reloc_map[i].elf_reloc_val];
697 }
698
699 return NULL;
700}
701
702
703/* This function performs the actual bitfiddling and sanity check for a
704 final relocation. Each relocation gets its *worst*-case expansion
705 in size when it arrives here; any reduction in size should have been
706 caught in linker relaxation earlier. When we get here, the relocation
707 looks like the smallest instruction with SWYM:s (nop:s) appended to the
708 max size. We fill in those nop:s.
709
710 R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
711 GETA $N,foo
712 ->
713 SETL $N,foo & 0xffff
714 INCML $N,(foo >> 16) & 0xffff
715 INCMH $N,(foo >> 32) & 0xffff
716 INCH $N,(foo >> 48) & 0xffff
717
718 R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
719 condbranches needing relaxation might be rare enough to not be
720 worthwhile.)
721 [P]Bcc $N,foo
722 ->
723 [~P]B~cc $N,.+20
724 SETL $255,foo & ...
725 INCML ...
726 INCMH ...
727 INCH ...
728 GO $255,$255,0
729
730 R_MMIX_PUSHJ: (FIXME: Relaxation...)
731 PUSHJ $N,foo
732 ->
733 SETL $255,foo & ...
734 INCML ...
735 INCMH ...
736 INCH ...
737 PUSHGO $N,$255,0
738
739 R_MMIX_JMP: (FIXME: Relaxation...)
740 JMP foo
741 ->
742 SETL $255,foo & ...
743 INCML ...
744 INCMH ...
745 INCH ...
746 GO $255,$255,0
747
748 R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in. */
749
750static bfd_reloc_status_type
751mmix_elf_perform_relocation (isec, howto, datap, addr, value)
752 asection *isec;
753 reloc_howto_type *howto;
754 PTR datap;
755 bfd_vma addr ATTRIBUTE_UNUSED;
756 bfd_vma value;
757{
758 bfd *abfd = isec->owner;
759 bfd_reloc_status_type flag = bfd_reloc_ok;
760 bfd_reloc_status_type r;
761 int offs = 0;
762 int reg = 255;
763
764 /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
765 We handle the differences here and the common sequence later. */
766 switch (howto->type)
767 {
768 case R_MMIX_GETA:
769 offs = 0;
770 reg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
771
772 /* We change to an absolute value. */
773 value += addr;
774 break;
775
776 case R_MMIX_CBRANCH:
777 {
778 int in1 = bfd_get_16 (abfd, (bfd_byte *) datap) << 16;
779
780 /* Invert the condition and prediction bit, and set the offset
781 to five instructions ahead.
782
783 We *can* do better if we want to. If the branch is found to be
784 within limits, we could leave the branch as is; there'll just
785 be a bunch of NOP:s after it. But we shouldn't see this
786 sequence often enough that it's worth doing it. */
787
788 bfd_put_32 (abfd,
789 (((in1 ^ ((PRED_INV_BIT | COND_INV_BIT) << 24)) & ~0xffff)
790 | (24/4)),
791 (bfd_byte *) datap);
792
793 /* Put a "GO $255,$255,0" after the common sequence. */
794 bfd_put_32 (abfd,
795 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24) | 0xffff00,
796 (bfd_byte *) datap + 20);
797
798 /* Common sequence starts at offset 4. */
799 offs = 4;
800
801 /* We change to an absolute value. */
802 value += addr;
803 }
804 break;
805
806 case R_MMIX_PUSHJ:
807 {
808 int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
809
810 /* Put a "PUSHGO $N,$255,0" after the common sequence. */
811 bfd_put_32 (abfd,
812 ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
813 | (inreg << 16)
814 | 0xff00,
815 (bfd_byte *) datap + 16);
816
817 /* We change to an absolute value. */
818 value += addr;
819 }
820 break;
821
822 case R_MMIX_JMP:
823 /* This one is a little special. If we get here on a non-relaxing
824 link, and the destination is actually in range, we don't need to
825 execute the nops.
826 If so, we fall through to the bit-fiddling relocs.
827
828 FIXME: bfd_check_overflow seems broken; the relocation is
829 rightshifted before testing, so supply a zero rightshift. */
830
831 if (! ((value & 3) == 0
832 && (r = bfd_check_overflow (complain_overflow_signed,
833 howto->bitsize,
834 0,
835 bfd_arch_bits_per_address (abfd),
836 value)) == bfd_reloc_ok))
837 {
838 /* If the relocation doesn't fit in a JMP, we let the NOP:s be
839 modified below, and put a "GO $255,$255,0" after the
840 address-loading sequence. */
841 bfd_put_32 (abfd,
842 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
843 | 0xffff00,
844 (bfd_byte *) datap + 16);
845
846 /* We change to an absolute value. */
847 value += addr;
848 break;
849 }
850 /* FALLTHROUGH. */
851 case R_MMIX_ADDR19:
852 case R_MMIX_ADDR27:
853 /* These must be in range, or else we emit an error. */
854 if ((value & 3) == 0
855 /* Note rightshift 0; see above. */
856 && (r = bfd_check_overflow (complain_overflow_signed,
857 howto->bitsize,
858 0,
859 bfd_arch_bits_per_address (abfd),
860 value)) == bfd_reloc_ok)
861 {
862 bfd_vma in1
863 = bfd_get_32 (abfd, (bfd_byte *) datap);
864 bfd_vma highbit;
865
866 if ((bfd_signed_vma) value < 0)
867 {
868 highbit = (1 << 24);
869 value += (1 << (howto->bitsize - 1));
870 }
871 else
872 highbit = 0;
873
874 value >>= 2;
875
876 bfd_put_32 (abfd,
877 (in1 & ~howto->src_mask)
878 | highbit
879 | (value & howto->dst_mask),
880 (bfd_byte *) datap);
881
882 return bfd_reloc_ok;
883 }
884 else
885 return bfd_reloc_overflow;
886
887 case R_MMIX_REG_OR_BYTE:
888 case R_MMIX_REG:
889 if (value > 255)
890 return bfd_reloc_overflow;
891 bfd_put_8 (abfd, value, datap);
892 return bfd_reloc_ok;
893
894 default:
895 BAD_CASE (howto->type);
896 }
897
898 /* This code adds the common SETL/INCML/INCMH/INCH worst-case
899 sequence. */
900
901 /* Lowest two bits must be 0. We return bfd_reloc_overflow for
902 everything that looks strange. */
903 if (value & 3)
904 flag = bfd_reloc_overflow;
905
906 bfd_put_32 (abfd,
907 (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
908 (bfd_byte *) datap + offs);
909 bfd_put_32 (abfd,
910 (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
911 (bfd_byte *) datap + offs + 4);
912 bfd_put_32 (abfd,
913 (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
914 (bfd_byte *) datap + offs + 8);
915 bfd_put_32 (abfd,
916 (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
917 (bfd_byte *) datap + offs + 12);
918
919 return flag;
920}
921
922/* Set the howto pointer for an MMIX ELF reloc (type RELA). */
923
924static void
925mmix_info_to_howto_rela (abfd, cache_ptr, dst)
926 bfd *abfd ATTRIBUTE_UNUSED;
927 arelent *cache_ptr;
928 Elf64_Internal_Rela *dst;
929{
930 unsigned int r_type;
931
932 r_type = ELF64_R_TYPE (dst->r_info);
933 BFD_ASSERT (r_type < (unsigned int) R_MMIX_max);
934 cache_ptr->howto = &elf_mmix_howto_table[r_type];
935}
936
937/* Any MMIX-specific relocation gets here at assembly time or when linking
938 to other formats (such as mmo); this is the relocation function from
939 the reloc_table. We don't get here for final pure ELF linking. */
940
941static bfd_reloc_status_type
942mmix_elf_reloc (abfd, reloc_entry, symbol, data, input_section,
943 output_bfd, error_message)
944 bfd *abfd;
945 arelent *reloc_entry;
946 asymbol *symbol;
947 PTR data;
948 asection *input_section;
949 bfd *output_bfd;
950 char **error_message ATTRIBUTE_UNUSED;
951{
952 bfd_vma relocation;
953 bfd_reloc_status_type r;
954 asection *reloc_target_output_section;
955 bfd_reloc_status_type flag = bfd_reloc_ok;
956 bfd_vma output_base = 0;
957 bfd_vma addr;
958
959 r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
960 input_section, output_bfd, error_message);
961
962 /* If that was all that was needed (i.e. this isn't a final link, only
963 some segment adjustments), we're done. */
964 if (r != bfd_reloc_continue)
965 return r;
966
967 if (bfd_is_und_section (symbol->section)
968 && (symbol->flags & BSF_WEAK) == 0
969 && output_bfd == (bfd *) NULL)
970 return bfd_reloc_undefined;
971
972 /* Is the address of the relocation really within the section? */
973 if (reloc_entry->address > input_section->_cooked_size)
974 return bfd_reloc_outofrange;
975
976 /* Work out which section the relocation is targetted at and the
977 initial relocation command value. */
978
979 /* Get symbol value. (Common symbols are special.) */
980 if (bfd_is_com_section (symbol->section))
981 relocation = 0;
982 else
983 relocation = symbol->value;
984
985 reloc_target_output_section = bfd_get_output_section (symbol);
986
987 /* Here the variable relocation holds the final address of the symbol we
988 are relocating against, plus any addend. */
989 if (output_bfd)
990 output_base = 0;
991 else
992 output_base = reloc_target_output_section->vma;
993
994 relocation += output_base + symbol->section->output_offset;
995
996 /* Get position of relocation. */
997 addr = (reloc_entry->address + input_section->output_section->vma
998 + input_section->output_offset);
999 if (output_bfd != (bfd *) NULL)
1000 {
1001 /* Add in supplied addend. */
1002 relocation += reloc_entry->addend;
1003
1004 /* This is a partial relocation, and we want to apply the
1005 relocation to the reloc entry rather than the raw data.
1006 Modify the reloc inplace to reflect what we now know. */
1007 reloc_entry->addend = relocation;
1008 reloc_entry->address += input_section->output_offset;
1009 return flag;
1010 }
1011
1012 return mmix_final_link_relocate (reloc_entry->howto, input_section,
1013 data, reloc_entry->address,
1014 reloc_entry->addend, relocation,
1015 bfd_asymbol_name (symbol),
1016 reloc_target_output_section);
1017}
1018
1019/* Relocate an MMIX ELF section. Modified from elf32-fr30.c; look to it
1020 for guidance if you're thinking of copying this. */
1021
1022static boolean
1023mmix_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1024 contents, relocs, local_syms, local_sections)
1025 bfd *output_bfd ATTRIBUTE_UNUSED;
1026 struct bfd_link_info *info;
1027 bfd *input_bfd;
1028 asection *input_section;
1029 bfd_byte *contents;
1030 Elf_Internal_Rela *relocs;
1031 Elf_Internal_Sym *local_syms;
1032 asection **local_sections;
1033{
1034 Elf_Internal_Shdr *symtab_hdr;
1035 struct elf_link_hash_entry **sym_hashes;
1036 Elf_Internal_Rela *rel;
1037 Elf_Internal_Rela *relend;
1038
1039 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1040 sym_hashes = elf_sym_hashes (input_bfd);
1041 relend = relocs + input_section->reloc_count;
1042
1043 for (rel = relocs; rel < relend; rel ++)
1044 {
1045 reloc_howto_type *howto;
1046 unsigned long r_symndx;
1047 Elf_Internal_Sym *sym;
1048 asection *sec;
1049 struct elf_link_hash_entry *h;
1050 bfd_vma relocation;
1051 bfd_reloc_status_type r;
1052 const char *name = NULL;
1053 int r_type;
1054 boolean undefined_signalled = false;
1055
1056 r_type = ELF64_R_TYPE (rel->r_info);
1057
1058 if (r_type == R_MMIX_GNU_VTINHERIT
1059 || r_type == R_MMIX_GNU_VTENTRY)
1060 continue;
1061
1062 r_symndx = ELF64_R_SYM (rel->r_info);
1063
1064 if (info->relocateable)
1065 {
1066 /* This is a relocateable link. We don't have to change
1067 anything, unless the reloc is against a section symbol,
1068 in which case we have to adjust according to where the
1069 section symbol winds up in the output section. */
1070 if (r_symndx < symtab_hdr->sh_info)
1071 {
1072 sym = local_syms + r_symndx;
1073
1074 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1075 {
1076 sec = local_sections [r_symndx];
1077 rel->r_addend += sec->output_offset + sym->st_value;
1078 }
1079 }
1080
1081 continue;
1082 }
1083
1084 /* This is a final link. */
1085 howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
1086 h = NULL;
1087 sym = NULL;
1088 sec = NULL;
1089
1090 if (r_symndx < symtab_hdr->sh_info)
1091 {
1092 sym = local_syms + r_symndx;
1093 sec = local_sections [r_symndx];
1094 relocation = (sec->output_section->vma
1095 + sec->output_offset
1096 + sym->st_value);
1097
1098 name = bfd_elf_string_from_elf_section
1099 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1100 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1101 }
1102 else
1103 {
1104 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1105
1106 while (h->root.type == bfd_link_hash_indirect
1107 || h->root.type == bfd_link_hash_warning)
1108 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1109
1110 name = h->root.root.string;
1111
1112 if (h->root.type == bfd_link_hash_defined
1113 || h->root.type == bfd_link_hash_defweak)
1114 {
1115 sec = h->root.u.def.section;
1116 relocation = (h->root.u.def.value
1117 + sec->output_section->vma
1118 + sec->output_offset);
1119 }
1120 else if (h->root.type == bfd_link_hash_undefweak)
1121 relocation = 0;
1122 else if (info->shared
1123 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1124 relocation = 0;
1125 else
1126 {
1127 /* The test on undefined_signalled is redundant at the
1128 moment, but kept for symmetry. */
1129 if (! undefined_signalled
1130 && ! ((*info->callbacks->undefined_symbol)
1131 (info, h->root.root.string, input_bfd,
1132 input_section, rel->r_offset, true)))
1133 return false;
1134 undefined_signalled = true;
1135 relocation = 0;
1136 }
1137 }
1138
1139 r = mmix_final_link_relocate (howto, input_section,
1140 contents, rel->r_offset,
1141 rel->r_addend, relocation, name, sec);
1142
1143 if (r != bfd_reloc_ok)
1144 {
1145 boolean check_ok = true;
1146 const char * msg = (const char *) NULL;
1147
1148 switch (r)
1149 {
1150 case bfd_reloc_overflow:
1151 check_ok = info->callbacks->reloc_overflow
1152 (info, name, howto->name, (bfd_vma) 0,
1153 input_bfd, input_section, rel->r_offset);
1154 break;
1155
1156 case bfd_reloc_undefined:
1157 /* We may have sent this message above. */
1158 if (! undefined_signalled)
1159 check_ok = info->callbacks->undefined_symbol
1160 (info, name, input_bfd, input_section, rel->r_offset,
1161 true);
1162 undefined_signalled = true;
1163 break;
1164
1165 case bfd_reloc_outofrange:
1166 msg = _("internal error: out of range error");
1167 break;
1168
1169 case bfd_reloc_notsupported:
1170 msg = _("internal error: unsupported relocation error");
1171 break;
1172
1173 case bfd_reloc_dangerous:
1174 msg = _("internal error: dangerous relocation");
1175 break;
1176
1177 default:
1178 msg = _("internal error: unknown error");
1179 break;
1180 }
1181
1182 if (msg)
1183 check_ok = info->callbacks->warning
1184 (info, msg, name, input_bfd, input_section, rel->r_offset);
1185
1186 if (! check_ok)
1187 return false;
1188 }
1189 }
1190
1191 return true;
1192}
1193
1194/* Perform a single relocation. By default we use the standard BFD
1195 routines. A few relocs we have to do ourselves. */
1196
1197static bfd_reloc_status_type
1198mmix_final_link_relocate (howto, input_section, contents,
1199 r_offset, r_addend, relocation, symname, symsec)
1200 reloc_howto_type *howto;
1201 asection *input_section;
1202 bfd_byte *contents;
1203 bfd_vma r_offset;
1204 bfd_signed_vma r_addend;
1205 bfd_vma relocation;
1206 const char *symname;
1207 asection *symsec;
1208{
1209 bfd_reloc_status_type r = bfd_reloc_ok;
1210 bfd_vma addr
1211 = (input_section->output_section->vma
1212 + input_section->output_offset
1213 + r_offset);
1214 bfd_signed_vma srel
1215 = (bfd_signed_vma) relocation + r_addend;
1216
1217 switch (howto->type)
1218 {
1219 /* All these are PC-relative. */
1220 case R_MMIX_PUSHJ:
1221 case R_MMIX_CBRANCH:
1222 case R_MMIX_ADDR19:
1223 case R_MMIX_GETA:
1224 case R_MMIX_ADDR27:
1225 case R_MMIX_JMP:
1226 contents += r_offset;
1227
1228 srel -= (input_section->output_section->vma
1229 + input_section->output_offset
1230 + r_offset);
1231
1232 r = mmix_elf_perform_relocation (input_section, howto, contents,
1233 addr, srel);
1234 break;
1235
1236 case R_MMIX_REG_OR_BYTE:
1237 case R_MMIX_REG:
1238 /* For now, we handle these alike. They must refer to an register
1239 symbol, which is either relative to the register section and in
1240 the range 0..255, or is in the register contents section with vma
1241 regno * 8. */
1242
1243 /* FIXME: A better way to check for reg contents section?
1244 FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1245 if (symsec == NULL)
1246 return bfd_reloc_undefined;
1247
1248 if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1249 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1250 {
1251 if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1252 {
1253 /* The bfd_reloc_outofrange return value, though intuitively
1254 a better value, will not get us an error. */
1255 return bfd_reloc_overflow;
1256 }
1257 srel /= 8;
1258 }
1259 else if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1260 MMIX_REG_SECTION_NAME) == 0)
1261 {
1262 if (srel < 0 || srel > 255)
1263 /* The bfd_reloc_outofrange return value, though intuitively a
1264 better value, will not get us an error. */
1265 return bfd_reloc_overflow;
1266 }
1267 else
1268 {
1269 (*_bfd_error_handler)
1270 (_("%s: register relocation against non-register symbol: %s in %s"),
1271 bfd_get_filename (input_section->owner),
1272 symname == NULL || *symname == 0 ? _("(unknown)") : symname,
1273 bfd_get_section_name (symsec->owner, symsec));
1274
1275 /* The bfd_reloc_outofrange return value, though intuitively a
1276 better value, will not get us an error. */
1277 return bfd_reloc_overflow;
1278 }
1279 contents += r_offset;
1280 r = mmix_elf_perform_relocation (input_section, howto, contents,
1281 addr, srel);
1282 break;
1283
1284 case R_MMIX_LOCAL:
1285 /* This isn't a real relocation, it's just an assertion that the
1286 final relocation value corresponds to a local register. We
1287 ignore the actual relocation; nothing is changed. */
1288 {
1289 asection *regsec
1290 = bfd_get_section_by_name (input_section->output_section->owner,
1291 MMIX_REG_CONTENTS_SECTION_NAME);
1292 bfd_vma first_global;
1293
1294 /* Check that this is an absolute value, or a reference to the
1295 register contents section or the register (symbol) section.
1296 Absolute numbers can get here as undefined section. Undefined
1297 symbols are signalled elsewhere, so there's no conflict in us
1298 accidentally handling it. */
1299 if (!bfd_is_abs_section (symsec)
1300 && !bfd_is_und_section (symsec)
1301 && strcmp (bfd_get_section_name (symsec->owner, symsec),
1302 MMIX_REG_CONTENTS_SECTION_NAME) != 0
1303 && strcmp (bfd_get_section_name (symsec->owner, symsec),
1304 MMIX_REG_SECTION_NAME) != 0)
1305 {
1306 (*_bfd_error_handler)
1307 (_("%s: directive LOCAL valid only with a register or absolute value"),
1308 bfd_get_filename (input_section->owner));
1309
1310 return bfd_reloc_overflow;
1311 }
1312
1313 /* If we don't have a register contents section, then $255 is the
1314 first global register. */
1315 if (regsec == NULL)
1316 first_global = 255;
1317 else
1318 {
1319 first_global = bfd_get_section_vma (abfd, regsec) / 8;
1320 if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1321 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1322 {
1323 if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1324 /* The bfd_reloc_outofrange return value, though
1325 intuitively a better value, will not get us an error. */
1326 return bfd_reloc_overflow;
1327 srel /= 8;
1328 }
1329 }
1330
1331 if ((bfd_vma) srel >= first_global)
1332 {
1333 /* FIXME: Better error message. */
1334 (*_bfd_error_handler)
1335 (_("%s: LOCAL directive: Register $%ld is not a local register. First global register is $%ld."),
1336 bfd_get_filename (input_section->owner), (long) srel, (long) first_global);
1337
1338 return bfd_reloc_overflow;
1339 }
1340 }
1341 r = bfd_reloc_ok;
1342 break;
1343
1344 default:
1345 r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
1346 contents, r_offset,
1347 relocation, r_addend);
1348 }
1349
1350 return r;
1351}
1352
1353/* Return the section that should be marked against GC for a given
1354 relocation. */
1355
1356static asection *
1357mmix_elf_gc_mark_hook (abfd, info, rel, h, sym)
1358 bfd *abfd;
1359 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1360 Elf_Internal_Rela *rel;
1361 struct elf_link_hash_entry *h;
1362 Elf_Internal_Sym *sym;
1363{
1364 if (h != NULL)
1365 {
1366 switch (ELF64_R_TYPE (rel->r_info))
1367 {
1368 case R_MMIX_GNU_VTINHERIT:
1369 case R_MMIX_GNU_VTENTRY:
1370 break;
1371
1372 default:
1373 switch (h->root.type)
1374 {
1375 case bfd_link_hash_defined:
1376 case bfd_link_hash_defweak:
1377 return h->root.u.def.section;
1378
1379 case bfd_link_hash_common:
1380 return h->root.u.c.p->section;
1381
1382 default:
1383 break;
1384 }
1385 }
1386 }
1387 else
1388 {
1389 if (!(elf_bad_symtab (abfd)
1390 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1391 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1392 && sym->st_shndx != SHN_COMMON))
1393 {
1394 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1395 }
1396 }
1397
1398 return NULL;
1399}
1400
1401/* Sort register relocs to come before expanding relocs. */
1402
1403static int
1404mmix_elf_sort_relocs (p1, p2)
1405 const PTR p1;
1406 const PTR p2;
1407{
1408 const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
1409 const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
1410 int r1_is_reg, r2_is_reg;
1411
1412 /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1413 insns. */
1414 if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
1415 return 1;
1416 else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
1417 return -1;
1418
1419 r1_is_reg
1420 = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
1421 || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
1422 r2_is_reg
1423 = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
1424 || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
1425 if (r1_is_reg != r2_is_reg)
1426 return r2_is_reg - r1_is_reg;
1427
1428 /* Neither or both are register relocs. Then sort on full offset. */
1429 if (r1->r_offset > r2->r_offset)
1430 return 1;
1431 else if (r1->r_offset < r2->r_offset)
1432 return -1;
1433 return 0;
1434}
1435
1436/* Look through the relocs for a section during the first phase. */
1437
1438static boolean
1439mmix_elf_check_relocs (abfd, info, sec, relocs)
1440 bfd *abfd;
1441 struct bfd_link_info *info;
1442 asection *sec;
1443 const Elf_Internal_Rela *relocs;
1444{
1445 Elf_Internal_Shdr *symtab_hdr;
1446 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
1447 const Elf_Internal_Rela *rel;
1448 const Elf_Internal_Rela *rel_end;
1449
1450 if (info->relocateable)
1451 return true;
1452
1453 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1454 sym_hashes = elf_sym_hashes (abfd);
1455 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf64_External_Sym);
1456 if (!elf_bad_symtab (abfd))
1457 sym_hashes_end -= symtab_hdr->sh_info;
1458
1459 /* First we sort the relocs so that any register relocs come before
1460 expansion-relocs to the same insn. FIXME: Not done for mmo. */
1461 qsort ((PTR) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1462 mmix_elf_sort_relocs);
1463
1464 rel_end = relocs + sec->reloc_count;
1465 for (rel = relocs; rel < rel_end; rel++)
1466 {
1467 struct elf_link_hash_entry *h;
1468 unsigned long r_symndx;
1469
1470 r_symndx = ELF64_R_SYM (rel->r_info);
1471 if (r_symndx < symtab_hdr->sh_info)
1472 h = NULL;
1473 else
1474 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1475
1476 switch (ELF64_R_TYPE (rel->r_info))
1477 {
1478 /* This relocation describes the C++ object vtable hierarchy.
1479 Reconstruct it for later use during GC. */
1480 case R_MMIX_GNU_VTINHERIT:
1481 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1482 return false;
1483 break;
1484
1485 /* This relocation describes which C++ vtable entries are actually
1486 used. Record for later use during GC. */
1487 case R_MMIX_GNU_VTENTRY:
1488 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1489 return false;
1490 break;
1491 }
1492 }
1493
1494 return true;
1495}
1496
1497/* Change symbols relative to the reg contents section to instead be to
1498 the register section, and scale them down to correspond to the register
1499 number. */
1500
1501static boolean
1502mmix_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
1503 bfd *abfd ATTRIBUTE_UNUSED;
1504 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1505 const char *name ATTRIBUTE_UNUSED;
1506 Elf_Internal_Sym *sym;
1507 asection *input_sec;
1508{
1509 if (input_sec != NULL
1510 && input_sec->name != NULL
1511 && ELF_ST_TYPE (sym->st_info) != STT_SECTION
1512 && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1513 {
1514 sym->st_value /= 8;
1515 sym->st_shndx = SHN_REGISTER;
1516 }
1517
1518 return true;
1519}
1520
1521/* We fake a register section that holds values that are register numbers.
1522 Having a SHN_REGISTER and register section translates better to other
1523 formats (e.g. mmo) than for example a STT_REGISTER attribute.
1524 This section faking is based on a construct in elf32-mips.c. */
1525static asection mmix_elf_reg_section;
1526static asymbol mmix_elf_reg_section_symbol;
1527static asymbol *mmix_elf_reg_section_symbol_ptr;
1528
1529/* Handle the special MIPS section numbers that a symbol may use.
1530 This is used for both the 32-bit and the 64-bit ABI. */
1531
1532void
1533mmix_elf_symbol_processing (abfd, asym)
1534 bfd *abfd ATTRIBUTE_UNUSED;
1535 asymbol *asym;
1536{
1537 elf_symbol_type *elfsym;
1538
1539 elfsym = (elf_symbol_type *) asym;
1540 switch (elfsym->internal_elf_sym.st_shndx)
1541 {
1542 case SHN_REGISTER:
1543 if (mmix_elf_reg_section.name == NULL)
1544 {
1545 /* Initialize the register section. */
1546 mmix_elf_reg_section.name = MMIX_REG_SECTION_NAME;
1547 mmix_elf_reg_section.flags = SEC_NO_FLAGS;
1548 mmix_elf_reg_section.output_section = &mmix_elf_reg_section;
1549 mmix_elf_reg_section.symbol = &mmix_elf_reg_section_symbol;
1550 mmix_elf_reg_section.symbol_ptr_ptr = &mmix_elf_reg_section_symbol_ptr;
1551 mmix_elf_reg_section_symbol.name = MMIX_REG_SECTION_NAME;
1552 mmix_elf_reg_section_symbol.flags = BSF_SECTION_SYM;
1553 mmix_elf_reg_section_symbol.section = &mmix_elf_reg_section;
1554 mmix_elf_reg_section_symbol_ptr = &mmix_elf_reg_section_symbol;
1555 }
1556 asym->section = &mmix_elf_reg_section;
1557 break;
1558
1559 default:
1560 break;
1561 }
1562}
1563
1564/* Given a BFD section, try to locate the corresponding ELF section
1565 index. */
1566
1567static boolean
1568mmix_elf_section_from_bfd_section (abfd, hdr, sec, retval)
1569 bfd * abfd ATTRIBUTE_UNUSED;
1570 Elf64_Internal_Shdr * hdr ATTRIBUTE_UNUSED;
1571 asection * sec;
1572 int * retval;
1573{
1574 if (strcmp (bfd_get_section_name (abfd, sec), MMIX_REG_SECTION_NAME) == 0)
1575 *retval = SHN_REGISTER;
1576 else
1577 return false;
1578
1579 return true;
1580}
1581
1582/* Hook called by the linker routine which adds symbols from an object
1583 file. We must handle the special SHN_REGISTER section number here.
1584
1585 We also check that we only have *one* each of the section-start
1586 symbols, since otherwise having two with the same value would cause
1587 them to be "merged", but with the contents serialized. */
1588
1589boolean
1590mmix_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
1591 bfd *abfd;
1592 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1593 const Elf_Internal_Sym *sym;
1594 const char **namep ATTRIBUTE_UNUSED;
1595 flagword *flagsp ATTRIBUTE_UNUSED;
1596 asection **secp;
1597 bfd_vma *valp ATTRIBUTE_UNUSED;
1598{
1599 if (sym->st_shndx == SHN_REGISTER)
1600 *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
1601 else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
1602 && strncmp (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
1603 strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)) == 0)
1604 {
1605 /* See if we have another one. */
1606 struct elf_link_hash_entry *h
1607 = (struct elf_link_hash_entry *) bfd_link_hash_lookup (info->hash,
1608 *namep,
1609 false,
1610 false, false);
1611
1612 if (h != NULL && h->root.type != bfd_link_hash_undefined)
1613 {
1614 /* How do we get the asymbol (or really: the filename) from h?
1615 h->root.u.def.section->owner is NULL. */
1616 ((*_bfd_error_handler)
1617 (_("%s: Error: multiple definition of `%s'; start of %s is set in a earlier linked file\n"),
1618 bfd_get_filename (abfd), *namep,
1619 *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)));
1620 bfd_set_error (bfd_error_bad_value);
1621 return false;
1622 }
1623 }
1624
1625 return true;
1626}
1627
1628/* We consider symbols matching "L.*:[0-9]+" to be local symbols. */
1629
1630boolean
1631mmix_elf_is_local_label_name (abfd, name)
1632 bfd *abfd;
1633 const char *name;
1634{
1635 const char *colpos;
1636 int digits;
1637
1638 /* Also include the default local-label definition. */
1639 if (_bfd_elf_is_local_label_name (abfd, name))
1640 return true;
1641
1642 if (*name != 'L')
1643 return false;
1644
1645 /* If there's no ":", or more than one, it's not a local symbol. */
1646 colpos = strchr (name, ':');
1647 if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
1648 return false;
1649
1650 /* Check that there are remaining characters and that they are digits. */
1651 if (colpos[1] == 0)
1652 return false;
1653
1654 digits = strspn (colpos + 1, "0123456789");
1655 return digits != 0 && colpos[1 + digits] == 0;
1656}
1657
1658/* We get rid of the register section here. */
1659
1660boolean
1661mmix_elf_final_link (abfd, info)
1662 bfd *abfd;
1663 struct bfd_link_info *info;
1664{
1665 /* We never output a register section, though we create one for
1666 temporary measures. Check that nobody entered contents into it. */
1667 asection *reg_section;
1668 asection **secpp;
1669
1670 reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
1671
1672 if (reg_section != NULL)
1673 {
1674 /* FIXME: Pass error state gracefully. */
1675 if (bfd_get_section_flags (abfd, reg_section) & SEC_HAS_CONTENTS)
1676 _bfd_abort (__FILE__, __LINE__, _("Register section has contents\n"));
1677
1678 /* FIXME: This does not seem like the proper way to kill a section,
1679 but it's the way it's done elsewhere, like elf64-alpha.c. */
1680 /* Really remove the section. */
1681 for (secpp = &abfd->sections;
1682 *secpp != reg_section;
1683 secpp = &(*secpp)->next)
1684 ;
1685 *secpp = (*secpp)->next;
1686 --abfd->section_count;
1687 }
1688
1689 if (! bfd_elf64_bfd_final_link (abfd, info))
1690 return false;
1691
1692 return true;
1693}
1694
1695#define ELF_ARCH bfd_arch_mmix
1696#define ELF_MACHINE_CODE EM_MMIX
1697
1698/* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
1699 However, that's too much for something somewhere in the linker part of
1700 BFD; perhaps the start-address has to be a non-zero multiple of this
1701 number, or larger than this number. The symptom is that the linker
1702 complains: "warning: allocated section `.text' not in segment". We
1703 settle for 64k; the page-size used in examples is 8k.
1704 #define ELF_MAXPAGESIZE 0x10000
1705
1706 Unfortunately, this causes excessive padding in the supposedly small
1707 for-education programs that are the expected usage (where people would
1708 inspect output). We stick to 256 bytes just to have *some* default
1709 alignment. */
1710#define ELF_MAXPAGESIZE 0x100
1711
1712#define TARGET_BIG_SYM bfd_elf64_mmix_vec
1713#define TARGET_BIG_NAME "elf64-mmix"
1714
1715#define elf_info_to_howto_rel NULL
1716#define elf_info_to_howto mmix_info_to_howto_rela
1717#define elf_backend_relocate_section mmix_elf_relocate_section
1718#define elf_backend_gc_mark_hook mmix_elf_gc_mark_hook
1719#define elf_backend_link_output_symbol_hook \
1720 mmix_elf_link_output_symbol_hook
1721#define elf_backend_add_symbol_hook mmix_elf_add_symbol_hook
1722
1723#define elf_backend_check_relocs mmix_elf_check_relocs
1724#define elf_backend_symbol_processing mmix_elf_symbol_processing
1725
1726#define bfd_elf64_bfd_is_local_label_name \
1727 mmix_elf_is_local_label_name
1728
1729#define elf_backend_may_use_rel_p 0
1730#define elf_backend_may_use_rela_p 1
1731#define elf_backend_default_use_rela_p 1
1732
1733#define elf_backend_can_gc_sections 1
1734#define elf_backend_section_from_bfd_section \
1735 mmix_elf_section_from_bfd_section
1736
1737#define bfd_elf64_bfd_final_link mmix_elf_final_link
1738
1739#include "elf64-target.h"