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