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