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