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