Index: mn10200/ChangeLog
[deliverable/binutils-gdb.git] / bfd / elf64-mmix.c
CommitLineData
3c3bdf30 1/* MMIX-specific support for 64-bit ELF.
b2a8e766 2 Copyright 2001, 2002, 2003, 2004 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
754021d0
AM
160 PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
161 asection *, struct elf_link_hash_entry *));
3c3bdf30
NC
162
163static bfd_reloc_status_type mmix_elf_reloc
164 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
165
166static reloc_howto_type *bfd_elf64_bfd_reloc_type_lookup
167 PARAMS ((bfd *, bfd_reloc_code_real_type));
168
169static void mmix_info_to_howto_rela
947216bf 170 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
3c3bdf30
NC
171
172static int mmix_elf_sort_relocs PARAMS ((const PTR, const PTR));
173
f0abc2a1
AM
174static bfd_boolean mmix_elf_new_section_hook
175 PARAMS ((bfd *, asection *));
176
b34976b6 177static bfd_boolean mmix_elf_check_relocs
3c3bdf30
NC
178 PARAMS ((bfd *, struct bfd_link_info *, asection *,
179 const Elf_Internal_Rela *));
180
b34976b6 181static bfd_boolean mmix_elf_check_common_relocs
930b4cb2
HPN
182 PARAMS ((bfd *, struct bfd_link_info *, asection *,
183 const Elf_Internal_Rela *));
184
b34976b6 185static bfd_boolean mmix_elf_relocate_section
3c3bdf30
NC
186 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
187 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
188
189static asection * mmix_elf_gc_mark_hook
1e2f5b6e 190 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
3c3bdf30
NC
191 struct elf_link_hash_entry *, Elf_Internal_Sym *));
192
b34976b6 193static bfd_boolean mmix_elf_gc_sweep_hook
930b4cb2
HPN
194 PARAMS ((bfd *, struct bfd_link_info *, asection *,
195 const Elf_Internal_Rela *));
196
3c3bdf30
NC
197static bfd_reloc_status_type mmix_final_link_relocate
198 PARAMS ((reloc_howto_type *, asection *, bfd_byte *,
199 bfd_vma, bfd_signed_vma, bfd_vma, const char *, asection *));
200
201static bfd_reloc_status_type mmix_elf_perform_relocation
202 PARAMS ((asection *, reloc_howto_type *, PTR, bfd_vma, bfd_vma));
203
b34976b6 204static bfd_boolean mmix_elf_section_from_bfd_section
af746e92 205 PARAMS ((bfd *, asection *, int *));
3c3bdf30 206
b34976b6 207static bfd_boolean mmix_elf_add_symbol_hook
555cd476 208 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3c3bdf30
NC
209 const char **, flagword *, asection **, bfd_vma *));
210
b34976b6 211static bfd_boolean mmix_elf_is_local_label_name
3c3bdf30
NC
212 PARAMS ((bfd *, const char *));
213
930b4cb2
HPN
214static int bpo_reloc_request_sort_fn PARAMS ((const PTR, const PTR));
215
b34976b6 216static bfd_boolean mmix_elf_relax_section
930b4cb2 217 PARAMS ((bfd *abfd, asection *sec, struct bfd_link_info *link_info,
b34976b6 218 bfd_boolean *again));
930b4cb2 219
b34976b6 220extern bfd_boolean mmix_elf_final_link PARAMS ((bfd *, struct bfd_link_info *));
3c3bdf30
NC
221
222extern void mmix_elf_symbol_processing PARAMS ((bfd *, asymbol *));
223
4fa5c2a8
HPN
224/* Only intended to be called from a debugger. */
225extern void mmix_dump_bpo_gregs
226 PARAMS ((struct bfd_link_info *, bfd_error_handler_type));
227
f60ebe14
HPN
228static void
229mmix_set_relaxable_size
230 PARAMS ((bfd *, asection *, void *));
231
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 {
eea6121a
AM
994 bfd_size_type size
995 = (isec->size
996 - (mmix_elf_section_data (isec)->pjs.n_pushj_relocs
997 * MAX_PUSHJ_STUB_SIZE));
f60ebe14
HPN
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))
eea6121a 1005 + size
f60ebe14
HPN
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
eea6121a 1018 + size
f60ebe14
HPN
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
eea6121a 1028 + size
f60ebe14
HPN
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
eea6121a
AM
1048 if (size + mmix_elf_section_data (isec)->pjs.stub_offset
1049 > isec->size)
f60ebe14
HPN
1050 abort ();
1051
1052 return r;
1053 }
1054 else
1055 {
1056 /* Put a "GO $255,0" after the common sequence. */
1057 bfd_put_32 (abfd,
1058 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1059 | 0xff00, (bfd_byte *) stubcontents + 16);
1060
1061 /* Prepare for the general code to set the first part of the
1062 linker stub, and */
1063 value += addr;
1064 datap = stubcontents;
1065 mmix_elf_section_data (isec)->pjs.stub_offset
1066 += MAX_PUSHJ_STUB_SIZE;
1067 }
1068 }
1069 break;
1070
3c3bdf30
NC
1071 case R_MMIX_PUSHJ:
1072 {
1073 int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
1074
1075 /* Put a "PUSHGO $N,$255,0" after the common sequence. */
1076 bfd_put_32 (abfd,
1077 ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1078 | (inreg << 16)
1079 | 0xff00,
1080 (bfd_byte *) datap + 16);
1081
1082 /* We change to an absolute value. */
1083 value += addr;
1084 }
1085 break;
1086
1087 case R_MMIX_JMP:
1088 /* This one is a little special. If we get here on a non-relaxing
1089 link, and the destination is actually in range, we don't need to
1090 execute the nops.
1091 If so, we fall through to the bit-fiddling relocs.
1092
1093 FIXME: bfd_check_overflow seems broken; the relocation is
1094 rightshifted before testing, so supply a zero rightshift. */
1095
1096 if (! ((value & 3) == 0
1097 && (r = bfd_check_overflow (complain_overflow_signed,
1098 howto->bitsize,
1099 0,
1100 bfd_arch_bits_per_address (abfd),
1101 value)) == bfd_reloc_ok))
1102 {
1103 /* If the relocation doesn't fit in a JMP, we let the NOP:s be
1104 modified below, and put a "GO $255,$255,0" after the
1105 address-loading sequence. */
1106 bfd_put_32 (abfd,
1107 ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
1108 | 0xffff00,
1109 (bfd_byte *) datap + 16);
1110
1111 /* We change to an absolute value. */
1112 value += addr;
1113 break;
1114 }
cedb70c5 1115 /* FALLTHROUGH. */
3c3bdf30
NC
1116 case R_MMIX_ADDR19:
1117 case R_MMIX_ADDR27:
f60ebe14 1118 pcrel_mmix_reloc_fits:
3c3bdf30
NC
1119 /* These must be in range, or else we emit an error. */
1120 if ((value & 3) == 0
1121 /* Note rightshift 0; see above. */
1122 && (r = bfd_check_overflow (complain_overflow_signed,
1123 howto->bitsize,
1124 0,
1125 bfd_arch_bits_per_address (abfd),
1126 value)) == bfd_reloc_ok)
1127 {
1128 bfd_vma in1
1129 = bfd_get_32 (abfd, (bfd_byte *) datap);
1130 bfd_vma highbit;
1131
1132 if ((bfd_signed_vma) value < 0)
1133 {
f60ebe14 1134 highbit = 1 << 24;
3c3bdf30
NC
1135 value += (1 << (howto->bitsize - 1));
1136 }
1137 else
1138 highbit = 0;
1139
1140 value >>= 2;
1141
1142 bfd_put_32 (abfd,
930b4cb2 1143 (in1 & howto->src_mask)
3c3bdf30
NC
1144 | highbit
1145 | (value & howto->dst_mask),
1146 (bfd_byte *) datap);
1147
1148 return bfd_reloc_ok;
1149 }
1150 else
1151 return bfd_reloc_overflow;
1152
930b4cb2
HPN
1153 case R_MMIX_BASE_PLUS_OFFSET:
1154 {
1155 struct bpo_reloc_section_info *bpodata
f0abc2a1 1156 = mmix_elf_section_data (isec)->bpo.reloc;
930b4cb2
HPN
1157 asection *bpo_greg_section
1158 = bpodata->bpo_greg_section;
1159 struct bpo_greg_section_info *gregdata
f0abc2a1 1160 = mmix_elf_section_data (bpo_greg_section)->bpo.greg;
930b4cb2
HPN
1161 size_t bpo_index
1162 = gregdata->bpo_reloc_indexes[bpodata->bpo_index++];
1163
1164 /* A consistency check: The value we now have in "relocation" must
1165 be the same as the value we stored for that relocation. It
1166 doesn't cost much, so can be left in at all times. */
1167 if (value != gregdata->reloc_request[bpo_index].value)
1168 {
1169 (*_bfd_error_handler)
1170 (_("%s: Internal inconsistency error for value for\n\
1171 linker-allocated global register: linked: 0x%lx%08lx != relaxed: 0x%lx%08lx\n"),
1172 bfd_get_filename (isec->owner),
1173 (unsigned long) (value >> 32), (unsigned long) value,
1174 (unsigned long) (gregdata->reloc_request[bpo_index].value
1175 >> 32),
1176 (unsigned long) gregdata->reloc_request[bpo_index].value);
1177 bfd_set_error (bfd_error_bad_value);
1178 return bfd_reloc_overflow;
1179 }
1180
1181 /* Then store the register number and offset for that register
1182 into datap and datap + 1 respectively. */
1183 bfd_put_8 (abfd,
1184 gregdata->reloc_request[bpo_index].regindex
1185 + bpo_greg_section->output_section->vma / 8,
1186 datap);
1187 bfd_put_8 (abfd,
1188 gregdata->reloc_request[bpo_index].offset,
1189 ((unsigned char *) datap) + 1);
1190 return bfd_reloc_ok;
1191 }
1192
3c3bdf30
NC
1193 case R_MMIX_REG_OR_BYTE:
1194 case R_MMIX_REG:
1195 if (value > 255)
1196 return bfd_reloc_overflow;
1197 bfd_put_8 (abfd, value, datap);
1198 return bfd_reloc_ok;
1199
1200 default:
1201 BAD_CASE (howto->type);
1202 }
1203
1204 /* This code adds the common SETL/INCML/INCMH/INCH worst-case
1205 sequence. */
1206
1207 /* Lowest two bits must be 0. We return bfd_reloc_overflow for
1208 everything that looks strange. */
1209 if (value & 3)
1210 flag = bfd_reloc_overflow;
1211
1212 bfd_put_32 (abfd,
1213 (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
1214 (bfd_byte *) datap + offs);
1215 bfd_put_32 (abfd,
1216 (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
1217 (bfd_byte *) datap + offs + 4);
1218 bfd_put_32 (abfd,
1219 (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
1220 (bfd_byte *) datap + offs + 8);
1221 bfd_put_32 (abfd,
1222 (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
1223 (bfd_byte *) datap + offs + 12);
1224
1225 return flag;
1226}
1227
1228/* Set the howto pointer for an MMIX ELF reloc (type RELA). */
1229
1230static void
1231mmix_info_to_howto_rela (abfd, cache_ptr, dst)
1232 bfd *abfd ATTRIBUTE_UNUSED;
1233 arelent *cache_ptr;
947216bf 1234 Elf_Internal_Rela *dst;
3c3bdf30
NC
1235{
1236 unsigned int r_type;
1237
1238 r_type = ELF64_R_TYPE (dst->r_info);
1239 BFD_ASSERT (r_type < (unsigned int) R_MMIX_max);
1240 cache_ptr->howto = &elf_mmix_howto_table[r_type];
1241}
1242
1243/* Any MMIX-specific relocation gets here at assembly time or when linking
1244 to other formats (such as mmo); this is the relocation function from
1245 the reloc_table. We don't get here for final pure ELF linking. */
1246
1247static bfd_reloc_status_type
1248mmix_elf_reloc (abfd, reloc_entry, symbol, data, input_section,
1249 output_bfd, error_message)
1250 bfd *abfd;
1251 arelent *reloc_entry;
1252 asymbol *symbol;
1253 PTR data;
1254 asection *input_section;
1255 bfd *output_bfd;
1256 char **error_message ATTRIBUTE_UNUSED;
1257{
1258 bfd_vma relocation;
1259 bfd_reloc_status_type r;
1260 asection *reloc_target_output_section;
1261 bfd_reloc_status_type flag = bfd_reloc_ok;
1262 bfd_vma output_base = 0;
1263 bfd_vma addr;
eea6121a 1264 bfd_size_type sz;
3c3bdf30
NC
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? */
eea6121a
AM
1280 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
1281 if (reloc_entry->address > sz)
3c3bdf30
NC
1282 return bfd_reloc_outofrange;
1283
4cc11e76 1284 /* Work out which section the relocation is targeted at and the
3c3bdf30
NC
1285 initial relocation command value. */
1286
1287 /* Get symbol value. (Common symbols are special.) */
1288 if (bfd_is_com_section (symbol->section))
1289 relocation = 0;
1290 else
1291 relocation = symbol->value;
1292
1293 reloc_target_output_section = bfd_get_output_section (symbol);
1294
1295 /* Here the variable relocation holds the final address of the symbol we
1296 are relocating against, plus any addend. */
1297 if (output_bfd)
1298 output_base = 0;
1299 else
1300 output_base = reloc_target_output_section->vma;
1301
1302 relocation += output_base + symbol->section->output_offset;
1303
1304 /* Get position of relocation. */
1305 addr = (reloc_entry->address + input_section->output_section->vma
1306 + input_section->output_offset);
1307 if (output_bfd != (bfd *) NULL)
1308 {
1309 /* Add in supplied addend. */
1310 relocation += reloc_entry->addend;
1311
1312 /* This is a partial relocation, and we want to apply the
1313 relocation to the reloc entry rather than the raw data.
1314 Modify the reloc inplace to reflect what we now know. */
1315 reloc_entry->addend = relocation;
1316 reloc_entry->address += input_section->output_offset;
1317 return flag;
1318 }
1319
1320 return mmix_final_link_relocate (reloc_entry->howto, input_section,
1321 data, reloc_entry->address,
1322 reloc_entry->addend, relocation,
1323 bfd_asymbol_name (symbol),
1324 reloc_target_output_section);
1325}
e06fcc86 1326\f
3c3bdf30
NC
1327/* Relocate an MMIX ELF section. Modified from elf32-fr30.c; look to it
1328 for guidance if you're thinking of copying this. */
1329
b34976b6 1330static bfd_boolean
3c3bdf30
NC
1331mmix_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1332 contents, relocs, local_syms, local_sections)
1333 bfd *output_bfd ATTRIBUTE_UNUSED;
1334 struct bfd_link_info *info;
1335 bfd *input_bfd;
1336 asection *input_section;
1337 bfd_byte *contents;
1338 Elf_Internal_Rela *relocs;
1339 Elf_Internal_Sym *local_syms;
1340 asection **local_sections;
1341{
1342 Elf_Internal_Shdr *symtab_hdr;
1343 struct elf_link_hash_entry **sym_hashes;
1344 Elf_Internal_Rela *rel;
1345 Elf_Internal_Rela *relend;
eea6121a
AM
1346 bfd_size_type size
1347 = (input_section->size
1348 - (mmix_elf_section_data (input_section)->pjs.n_pushj_relocs
1349 * MAX_PUSHJ_STUB_SIZE));
f60ebe14 1350 size_t pjsno = 0;
3c3bdf30
NC
1351
1352 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1353 sym_hashes = elf_sym_hashes (input_bfd);
1354 relend = relocs + input_section->reloc_count;
1355
1356 for (rel = relocs; rel < relend; rel ++)
1357 {
1358 reloc_howto_type *howto;
1359 unsigned long r_symndx;
1360 Elf_Internal_Sym *sym;
1361 asection *sec;
1362 struct elf_link_hash_entry *h;
1363 bfd_vma relocation;
1364 bfd_reloc_status_type r;
1365 const char *name = NULL;
1366 int r_type;
b34976b6 1367 bfd_boolean undefined_signalled = FALSE;
3c3bdf30
NC
1368
1369 r_type = ELF64_R_TYPE (rel->r_info);
1370
1371 if (r_type == R_MMIX_GNU_VTINHERIT
1372 || r_type == R_MMIX_GNU_VTENTRY)
1373 continue;
1374
1375 r_symndx = ELF64_R_SYM (rel->r_info);
1376
1049f94e 1377 if (info->relocatable)
3c3bdf30 1378 {
f60ebe14
HPN
1379 /* This is a relocatable link. For most relocs we don't have to
1380 change anything, unless the reloc is against a section
1381 symbol, in which case we have to adjust according to where
1382 the section symbol winds up in the output section. */
3c3bdf30
NC
1383 if (r_symndx < symtab_hdr->sh_info)
1384 {
1385 sym = local_syms + r_symndx;
1386
1387 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1388 {
1389 sec = local_sections [r_symndx];
1390 rel->r_addend += sec->output_offset + sym->st_value;
1391 }
1392 }
1393
f60ebe14
HPN
1394 /* For PUSHJ stub relocs however, we may need to change the
1395 reloc and the section contents, if the reloc doesn't reach
1396 beyond the end of the output section and previous stubs.
1397 Then we change the section contents to be a PUSHJ to the end
1398 of the input section plus stubs (we can do that without using
1399 a reloc), and then we change the reloc to be a R_MMIX_PUSHJ
1400 at the stub location. */
1401 if (r_type == R_MMIX_PUSHJ_STUBBABLE)
1402 {
1403 /* We've already checked whether we need a stub; use that
1404 knowledge. */
1405 if (mmix_elf_section_data (input_section)->pjs.stub_size[pjsno]
1406 != 0)
1407 {
1408 Elf_Internal_Rela relcpy;
1409
1410 if (mmix_elf_section_data (input_section)
1411 ->pjs.stub_size[pjsno] != MAX_PUSHJ_STUB_SIZE)
1412 abort ();
1413
1414 /* There's already a PUSHJ insn there, so just fill in
1415 the offset bits to the stub. */
1416 if (mmix_final_link_relocate (elf_mmix_howto_table
1417 + R_MMIX_ADDR19,
1418 input_section,
1419 contents,
1420 rel->r_offset,
1421 0,
1422 input_section
1423 ->output_section->vma
1424 + input_section->output_offset
eea6121a 1425 + size
f60ebe14
HPN
1426 + mmix_elf_section_data (input_section)
1427 ->pjs.stub_offset,
1428 NULL, NULL) != bfd_reloc_ok)
1429 return FALSE;
1430
1431 /* Put a JMP insn at the stub; it goes with the
1432 R_MMIX_JMP reloc. */
1433 bfd_put_32 (output_bfd, JMP_INSN_BYTE << 24,
1434 contents
eea6121a 1435 + size
f60ebe14
HPN
1436 + mmix_elf_section_data (input_section)
1437 ->pjs.stub_offset);
1438
1439 /* Change the reloc to be at the stub, and to a full
1440 R_MMIX_JMP reloc. */
1441 rel->r_info = ELF64_R_INFO (r_symndx, R_MMIX_JMP);
1442 rel->r_offset
eea6121a 1443 = (size
f60ebe14
HPN
1444 + mmix_elf_section_data (input_section)
1445 ->pjs.stub_offset);
1446
1447 mmix_elf_section_data (input_section)->pjs.stub_offset
1448 += MAX_PUSHJ_STUB_SIZE;
1449
1450 /* Shift this reloc to the end of the relocs to maintain
1451 the r_offset sorted reloc order. */
1452 relcpy = *rel;
1453 memmove (rel, rel + 1, (char *) relend - (char *) rel);
1454 relend[-1] = relcpy;
1455
1456 /* Back up one reloc, or else we'd skip the next reloc
1457 in turn. */
1458 rel--;
1459 }
1460
1461 pjsno++;
1462 }
3c3bdf30
NC
1463 continue;
1464 }
1465
1466 /* This is a final link. */
1467 howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
1468 h = NULL;
1469 sym = NULL;
1470 sec = NULL;
1471
1472 if (r_symndx < symtab_hdr->sh_info)
1473 {
1474 sym = local_syms + r_symndx;
1475 sec = local_sections [r_symndx];
8517fae7 1476 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3c3bdf30 1477
6a9cae7f
AM
1478 name = bfd_elf_string_from_elf_section (input_bfd,
1479 symtab_hdr->sh_link,
1480 sym->st_name);
1481 if (name == NULL)
1482 name = bfd_section_name (input_bfd, sec);
3c3bdf30
NC
1483 }
1484 else
1485 {
59c2e50f 1486 bfd_boolean unresolved_reloc;
3c3bdf30 1487
b2a8e766
AM
1488 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1489 r_symndx, symtab_hdr, sym_hashes,
1490 h, sec, relocation,
1491 unresolved_reloc, undefined_signalled);
6a9cae7f 1492 name = h->root.root.string;
3c3bdf30
NC
1493 }
1494
1495 r = mmix_final_link_relocate (howto, input_section,
1496 contents, rel->r_offset,
1497 rel->r_addend, relocation, name, sec);
1498
1499 if (r != bfd_reloc_ok)
1500 {
b34976b6 1501 bfd_boolean check_ok = TRUE;
3c3bdf30
NC
1502 const char * msg = (const char *) NULL;
1503
1504 switch (r)
1505 {
1506 case bfd_reloc_overflow:
1507 check_ok = info->callbacks->reloc_overflow
1508 (info, name, howto->name, (bfd_vma) 0,
1509 input_bfd, input_section, rel->r_offset);
1510 break;
1511
1512 case bfd_reloc_undefined:
1513 /* We may have sent this message above. */
1514 if (! undefined_signalled)
1515 check_ok = info->callbacks->undefined_symbol
1516 (info, name, input_bfd, input_section, rel->r_offset,
b34976b6
AM
1517 TRUE);
1518 undefined_signalled = TRUE;
3c3bdf30
NC
1519 break;
1520
1521 case bfd_reloc_outofrange:
1522 msg = _("internal error: out of range error");
1523 break;
1524
1525 case bfd_reloc_notsupported:
1526 msg = _("internal error: unsupported relocation error");
1527 break;
1528
1529 case bfd_reloc_dangerous:
1530 msg = _("internal error: dangerous relocation");
1531 break;
1532
1533 default:
1534 msg = _("internal error: unknown error");
1535 break;
1536 }
1537
1538 if (msg)
1539 check_ok = info->callbacks->warning
1540 (info, msg, name, input_bfd, input_section, rel->r_offset);
1541
1542 if (! check_ok)
b34976b6 1543 return FALSE;
3c3bdf30
NC
1544 }
1545 }
1546
b34976b6 1547 return TRUE;
3c3bdf30 1548}
e06fcc86 1549\f
3c3bdf30
NC
1550/* Perform a single relocation. By default we use the standard BFD
1551 routines. A few relocs we have to do ourselves. */
1552
1553static bfd_reloc_status_type
1554mmix_final_link_relocate (howto, input_section, contents,
1555 r_offset, r_addend, relocation, symname, symsec)
1556 reloc_howto_type *howto;
1557 asection *input_section;
1558 bfd_byte *contents;
1559 bfd_vma r_offset;
1560 bfd_signed_vma r_addend;
1561 bfd_vma relocation;
1562 const char *symname;
1563 asection *symsec;
1564{
1565 bfd_reloc_status_type r = bfd_reloc_ok;
1566 bfd_vma addr
1567 = (input_section->output_section->vma
1568 + input_section->output_offset
1569 + r_offset);
1570 bfd_signed_vma srel
1571 = (bfd_signed_vma) relocation + r_addend;
1572
1573 switch (howto->type)
1574 {
1575 /* All these are PC-relative. */
f60ebe14 1576 case R_MMIX_PUSHJ_STUBBABLE:
3c3bdf30
NC
1577 case R_MMIX_PUSHJ:
1578 case R_MMIX_CBRANCH:
1579 case R_MMIX_ADDR19:
1580 case R_MMIX_GETA:
1581 case R_MMIX_ADDR27:
1582 case R_MMIX_JMP:
1583 contents += r_offset;
1584
1585 srel -= (input_section->output_section->vma
1586 + input_section->output_offset
1587 + r_offset);
1588
1589 r = mmix_elf_perform_relocation (input_section, howto, contents,
1590 addr, srel);
1591 break;
1592
930b4cb2
HPN
1593 case R_MMIX_BASE_PLUS_OFFSET:
1594 if (symsec == NULL)
1595 return bfd_reloc_undefined;
1596
1597 /* Check that we're not relocating against a register symbol. */
1598 if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1599 MMIX_REG_CONTENTS_SECTION_NAME) == 0
1600 || strcmp (bfd_get_section_name (symsec->owner, symsec),
1601 MMIX_REG_SECTION_NAME) == 0)
1602 {
1603 /* Note: This is separated out into two messages in order
1604 to ease the translation into other languages. */
1605 if (symname == NULL || *symname == 0)
1606 (*_bfd_error_handler)
1607 (_("%s: base-plus-offset relocation against register symbol: (unknown) in %s"),
1608 bfd_get_filename (input_section->owner),
1609 bfd_get_section_name (symsec->owner, symsec));
1610 else
1611 (*_bfd_error_handler)
1612 (_("%s: base-plus-offset relocation against register symbol: %s in %s"),
1613 bfd_get_filename (input_section->owner), symname,
1614 bfd_get_section_name (symsec->owner, symsec));
1615 return bfd_reloc_overflow;
1616 }
1617 goto do_mmix_reloc;
1618
3c3bdf30
NC
1619 case R_MMIX_REG_OR_BYTE:
1620 case R_MMIX_REG:
1621 /* For now, we handle these alike. They must refer to an register
1622 symbol, which is either relative to the register section and in
1623 the range 0..255, or is in the register contents section with vma
1624 regno * 8. */
1625
1626 /* FIXME: A better way to check for reg contents section?
1627 FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
1628 if (symsec == NULL)
1629 return bfd_reloc_undefined;
1630
1631 if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1632 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1633 {
1634 if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1635 {
1636 /* The bfd_reloc_outofrange return value, though intuitively
1637 a better value, will not get us an error. */
1638 return bfd_reloc_overflow;
1639 }
1640 srel /= 8;
1641 }
1642 else if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1643 MMIX_REG_SECTION_NAME) == 0)
1644 {
1645 if (srel < 0 || srel > 255)
1646 /* The bfd_reloc_outofrange return value, though intuitively a
1647 better value, will not get us an error. */
1648 return bfd_reloc_overflow;
1649 }
1650 else
1651 {
930b4cb2 1652 /* Note: This is separated out into two messages in order
ca09e32b
NC
1653 to ease the translation into other languages. */
1654 if (symname == NULL || *symname == 0)
1655 (*_bfd_error_handler)
1656 (_("%s: register relocation against non-register symbol: (unknown) in %s"),
1657 bfd_get_filename (input_section->owner),
1658 bfd_get_section_name (symsec->owner, symsec));
1659 else
1660 (*_bfd_error_handler)
1661 (_("%s: register relocation against non-register symbol: %s in %s"),
1662 bfd_get_filename (input_section->owner), symname,
1663 bfd_get_section_name (symsec->owner, symsec));
3c3bdf30
NC
1664
1665 /* The bfd_reloc_outofrange return value, though intuitively a
1666 better value, will not get us an error. */
1667 return bfd_reloc_overflow;
1668 }
930b4cb2 1669 do_mmix_reloc:
3c3bdf30
NC
1670 contents += r_offset;
1671 r = mmix_elf_perform_relocation (input_section, howto, contents,
1672 addr, srel);
1673 break;
1674
1675 case R_MMIX_LOCAL:
1676 /* This isn't a real relocation, it's just an assertion that the
1677 final relocation value corresponds to a local register. We
1678 ignore the actual relocation; nothing is changed. */
1679 {
1680 asection *regsec
1681 = bfd_get_section_by_name (input_section->output_section->owner,
1682 MMIX_REG_CONTENTS_SECTION_NAME);
1683 bfd_vma first_global;
1684
1685 /* Check that this is an absolute value, or a reference to the
1686 register contents section or the register (symbol) section.
1687 Absolute numbers can get here as undefined section. Undefined
1688 symbols are signalled elsewhere, so there's no conflict in us
1689 accidentally handling it. */
1690 if (!bfd_is_abs_section (symsec)
1691 && !bfd_is_und_section (symsec)
1692 && strcmp (bfd_get_section_name (symsec->owner, symsec),
1693 MMIX_REG_CONTENTS_SECTION_NAME) != 0
1694 && strcmp (bfd_get_section_name (symsec->owner, symsec),
1695 MMIX_REG_SECTION_NAME) != 0)
1696 {
1697 (*_bfd_error_handler)
1698 (_("%s: directive LOCAL valid only with a register or absolute value"),
1699 bfd_get_filename (input_section->owner));
1700
1701 return bfd_reloc_overflow;
1702 }
1703
1704 /* If we don't have a register contents section, then $255 is the
1705 first global register. */
1706 if (regsec == NULL)
1707 first_global = 255;
1708 else
1709 {
1710 first_global = bfd_get_section_vma (abfd, regsec) / 8;
1711 if (strcmp (bfd_get_section_name (symsec->owner, symsec),
1712 MMIX_REG_CONTENTS_SECTION_NAME) == 0)
1713 {
1714 if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
1715 /* The bfd_reloc_outofrange return value, though
1716 intuitively a better value, will not get us an error. */
1717 return bfd_reloc_overflow;
1718 srel /= 8;
1719 }
1720 }
1721
1722 if ((bfd_vma) srel >= first_global)
1723 {
1724 /* FIXME: Better error message. */
1725 (*_bfd_error_handler)
1726 (_("%s: LOCAL directive: Register $%ld is not a local register. First global register is $%ld."),
1727 bfd_get_filename (input_section->owner), (long) srel, (long) first_global);
1728
1729 return bfd_reloc_overflow;
1730 }
1731 }
1732 r = bfd_reloc_ok;
1733 break;
1734
1735 default:
1736 r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
1737 contents, r_offset,
1738 relocation, r_addend);
1739 }
1740
1741 return r;
1742}
e06fcc86 1743\f
3c3bdf30
NC
1744/* Return the section that should be marked against GC for a given
1745 relocation. */
1746
1747static asection *
1e2f5b6e
AM
1748mmix_elf_gc_mark_hook (sec, info, rel, h, sym)
1749 asection *sec;
3c3bdf30
NC
1750 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1751 Elf_Internal_Rela *rel;
1752 struct elf_link_hash_entry *h;
1753 Elf_Internal_Sym *sym;
1754{
1755 if (h != NULL)
1756 {
1757 switch (ELF64_R_TYPE (rel->r_info))
1758 {
1759 case R_MMIX_GNU_VTINHERIT:
1760 case R_MMIX_GNU_VTENTRY:
1761 break;
1762
1763 default:
1764 switch (h->root.type)
1765 {
1766 case bfd_link_hash_defined:
1767 case bfd_link_hash_defweak:
1768 return h->root.u.def.section;
1769
1770 case bfd_link_hash_common:
1771 return h->root.u.c.p->section;
1772
1773 default:
1774 break;
1775 }
1776 }
1777 }
1778 else
1e2f5b6e 1779 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3c3bdf30
NC
1780
1781 return NULL;
1782}
930b4cb2
HPN
1783
1784/* Update relocation info for a GC-excluded section. We could supposedly
1785 perform the allocation after GC, but there's no suitable hook between
1786 GC (or section merge) and the point when all input sections must be
1787 present. Better to waste some memory and (perhaps) a little time. */
1788
b34976b6 1789static bfd_boolean
930b4cb2
HPN
1790mmix_elf_gc_sweep_hook (abfd, info, sec, relocs)
1791 bfd *abfd ATTRIBUTE_UNUSED;
1792 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1793 asection *sec ATTRIBUTE_UNUSED;
1794 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1795{
1796 struct bpo_reloc_section_info *bpodata
f0abc2a1 1797 = mmix_elf_section_data (sec)->bpo.reloc;
930b4cb2
HPN
1798 asection *allocated_gregs_section;
1799
1800 /* If no bpodata here, we have nothing to do. */
1801 if (bpodata == NULL)
b34976b6 1802 return TRUE;
930b4cb2
HPN
1803
1804 allocated_gregs_section = bpodata->bpo_greg_section;
1805
f0abc2a1 1806 mmix_elf_section_data (allocated_gregs_section)->bpo.greg->n_bpo_relocs
930b4cb2
HPN
1807 -= bpodata->n_bpo_relocs_this_section;
1808
b34976b6 1809 return TRUE;
930b4cb2 1810}
e06fcc86 1811\f
3c3bdf30
NC
1812/* Sort register relocs to come before expanding relocs. */
1813
1814static int
1815mmix_elf_sort_relocs (p1, p2)
1816 const PTR p1;
1817 const PTR p2;
1818{
1819 const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
1820 const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
1821 int r1_is_reg, r2_is_reg;
1822
1823 /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
1824 insns. */
1825 if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
1826 return 1;
1827 else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
1828 return -1;
1829
1830 r1_is_reg
1831 = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
1832 || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
1833 r2_is_reg
1834 = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
1835 || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
1836 if (r1_is_reg != r2_is_reg)
1837 return r2_is_reg - r1_is_reg;
1838
1839 /* Neither or both are register relocs. Then sort on full offset. */
1840 if (r1->r_offset > r2->r_offset)
1841 return 1;
1842 else if (r1->r_offset < r2->r_offset)
1843 return -1;
1844 return 0;
1845}
1846
930b4cb2
HPN
1847/* Subset of mmix_elf_check_relocs, common to ELF and mmo linking. */
1848
b34976b6 1849static bfd_boolean
930b4cb2
HPN
1850mmix_elf_check_common_relocs (abfd, info, sec, relocs)
1851 bfd *abfd;
1852 struct bfd_link_info *info;
1853 asection *sec;
1854 const Elf_Internal_Rela *relocs;
1855{
1856 bfd *bpo_greg_owner = NULL;
1857 asection *allocated_gregs_section = NULL;
1858 struct bpo_greg_section_info *gregdata = NULL;
1859 struct bpo_reloc_section_info *bpodata = NULL;
1860 const Elf_Internal_Rela *rel;
1861 const Elf_Internal_Rela *rel_end;
1862
930b4cb2
HPN
1863 /* We currently have to abuse this COFF-specific member, since there's
1864 no target-machine-dedicated member. There's no alternative outside
1865 the bfd_link_info struct; we can't specialize a hash-table since
1866 they're different between ELF and mmo. */
1867 bpo_greg_owner = (bfd *) info->base_file;
1868
1869 rel_end = relocs + sec->reloc_count;
1870 for (rel = relocs; rel < rel_end; rel++)
1871 {
1872 switch (ELF64_R_TYPE (rel->r_info))
1873 {
1874 /* This relocation causes a GREG allocation. We need to count
1875 them, and we need to create a section for them, so we need an
1876 object to fake as the owner of that section. We can't use
1877 the ELF dynobj for this, since the ELF bits assume lots of
1878 DSO-related stuff if that member is non-NULL. */
1879 case R_MMIX_BASE_PLUS_OFFSET:
f60ebe14
HPN
1880 /* We don't do anything with this reloc for a relocatable link. */
1881 if (info->relocatable)
1882 break;
1883
930b4cb2
HPN
1884 if (bpo_greg_owner == NULL)
1885 {
1886 bpo_greg_owner = abfd;
1887 info->base_file = (PTR) bpo_greg_owner;
1888 }
1889
4fa5c2a8
HPN
1890 if (allocated_gregs_section == NULL)
1891 allocated_gregs_section
1892 = bfd_get_section_by_name (bpo_greg_owner,
1893 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
1894
930b4cb2
HPN
1895 if (allocated_gregs_section == NULL)
1896 {
1897 allocated_gregs_section
1898 = bfd_make_section (bpo_greg_owner,
1899 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
1900 /* Setting both SEC_ALLOC and SEC_LOAD means the section is
1901 treated like any other section, and we'd get errors for
1902 address overlap with the text section. Let's set none of
1903 those flags, as that is what currently happens for usual
1904 GREG allocations, and that works. */
1905 if (allocated_gregs_section == NULL
1906 || !bfd_set_section_flags (bpo_greg_owner,
1907 allocated_gregs_section,
1908 (SEC_HAS_CONTENTS
1909 | SEC_IN_MEMORY
1910 | SEC_LINKER_CREATED))
1911 || !bfd_set_section_alignment (bpo_greg_owner,
1912 allocated_gregs_section,
1913 3))
b34976b6 1914 return FALSE;
930b4cb2
HPN
1915
1916 gregdata = (struct bpo_greg_section_info *)
1917 bfd_zalloc (bpo_greg_owner, sizeof (struct bpo_greg_section_info));
1918 if (gregdata == NULL)
b34976b6 1919 return FALSE;
f0abc2a1
AM
1920 mmix_elf_section_data (allocated_gregs_section)->bpo.greg
1921 = gregdata;
930b4cb2
HPN
1922 }
1923 else if (gregdata == NULL)
f0abc2a1
AM
1924 gregdata
1925 = mmix_elf_section_data (allocated_gregs_section)->bpo.greg;
930b4cb2
HPN
1926
1927 /* Get ourselves some auxiliary info for the BPO-relocs. */
1928 if (bpodata == NULL)
1929 {
1930 /* No use doing a separate iteration pass to find the upper
1931 limit - just use the number of relocs. */
1932 bpodata = (struct bpo_reloc_section_info *)
1933 bfd_alloc (bpo_greg_owner,
1934 sizeof (struct bpo_reloc_section_info)
1935 * (sec->reloc_count + 1));
1936 if (bpodata == NULL)
b34976b6 1937 return FALSE;
f0abc2a1 1938 mmix_elf_section_data (sec)->bpo.reloc = bpodata;
930b4cb2
HPN
1939 bpodata->first_base_plus_offset_reloc
1940 = bpodata->bpo_index
1941 = gregdata->n_max_bpo_relocs;
1942 bpodata->bpo_greg_section
1943 = allocated_gregs_section;
4fa5c2a8 1944 bpodata->n_bpo_relocs_this_section = 0;
930b4cb2
HPN
1945 }
1946
1947 bpodata->n_bpo_relocs_this_section++;
1948 gregdata->n_max_bpo_relocs++;
1949
1950 /* We don't get another chance to set this before GC; we've not
f60ebe14 1951 set up any hook that runs before GC. */
930b4cb2
HPN
1952 gregdata->n_bpo_relocs
1953 = gregdata->n_max_bpo_relocs;
1954 break;
f60ebe14
HPN
1955
1956 case R_MMIX_PUSHJ_STUBBABLE:
1957 mmix_elf_section_data (sec)->pjs.n_pushj_relocs++;
1958 break;
930b4cb2
HPN
1959 }
1960 }
1961
f60ebe14
HPN
1962 /* Allocate per-reloc stub storage and initialize it to the max stub
1963 size. */
1964 if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs != 0)
1965 {
1966 size_t i;
1967
1968 mmix_elf_section_data (sec)->pjs.stub_size
1969 = bfd_alloc (abfd, mmix_elf_section_data (sec)->pjs.n_pushj_relocs
1970 * sizeof (mmix_elf_section_data (sec)
1971 ->pjs.stub_size[0]));
1972 if (mmix_elf_section_data (sec)->pjs.stub_size == NULL)
1973 return FALSE;
1974
1975 for (i = 0; i < mmix_elf_section_data (sec)->pjs.n_pushj_relocs; i++)
1976 mmix_elf_section_data (sec)->pjs.stub_size[i] = MAX_PUSHJ_STUB_SIZE;
1977 }
1978
b34976b6 1979 return TRUE;
930b4cb2
HPN
1980}
1981
3c3bdf30
NC
1982/* Look through the relocs for a section during the first phase. */
1983
b34976b6 1984static bfd_boolean
3c3bdf30
NC
1985mmix_elf_check_relocs (abfd, info, sec, relocs)
1986 bfd *abfd;
1987 struct bfd_link_info *info;
1988 asection *sec;
1989 const Elf_Internal_Rela *relocs;
1990{
1991 Elf_Internal_Shdr *symtab_hdr;
1992 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
1993 const Elf_Internal_Rela *rel;
1994 const Elf_Internal_Rela *rel_end;
1995
3c3bdf30
NC
1996 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1997 sym_hashes = elf_sym_hashes (abfd);
1998 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf64_External_Sym);
1999 if (!elf_bad_symtab (abfd))
2000 sym_hashes_end -= symtab_hdr->sh_info;
2001
2002 /* First we sort the relocs so that any register relocs come before
2003 expansion-relocs to the same insn. FIXME: Not done for mmo. */
2004 qsort ((PTR) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
2005 mmix_elf_sort_relocs);
2006
930b4cb2
HPN
2007 /* Do the common part. */
2008 if (!mmix_elf_check_common_relocs (abfd, info, sec, relocs))
b34976b6 2009 return FALSE;
930b4cb2 2010
f60ebe14
HPN
2011 if (info->relocatable)
2012 return TRUE;
2013
3c3bdf30
NC
2014 rel_end = relocs + sec->reloc_count;
2015 for (rel = relocs; rel < rel_end; rel++)
2016 {
2017 struct elf_link_hash_entry *h;
2018 unsigned long r_symndx;
2019
2020 r_symndx = ELF64_R_SYM (rel->r_info);
2021 if (r_symndx < symtab_hdr->sh_info)
2022 h = NULL;
2023 else
2024 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2025
2026 switch (ELF64_R_TYPE (rel->r_info))
930b4cb2 2027 {
3c3bdf30
NC
2028 /* This relocation describes the C++ object vtable hierarchy.
2029 Reconstruct it for later use during GC. */
2030 case R_MMIX_GNU_VTINHERIT:
c152c796 2031 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 2032 return FALSE;
3c3bdf30
NC
2033 break;
2034
2035 /* This relocation describes which C++ vtable entries are actually
2036 used. Record for later use during GC. */
2037 case R_MMIX_GNU_VTENTRY:
c152c796 2038 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 2039 return FALSE;
3c3bdf30 2040 break;
930b4cb2
HPN
2041 }
2042 }
2043
b34976b6 2044 return TRUE;
930b4cb2
HPN
2045}
2046
2047/* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
2048 Copied from elf_link_add_object_symbols. */
2049
b34976b6 2050bfd_boolean
930b4cb2
HPN
2051_bfd_mmix_check_all_relocs (abfd, info)
2052 bfd *abfd;
2053 struct bfd_link_info *info;
2054{
2055 asection *o;
2056
2057 for (o = abfd->sections; o != NULL; o = o->next)
2058 {
2059 Elf_Internal_Rela *internal_relocs;
b34976b6 2060 bfd_boolean ok;
930b4cb2
HPN
2061
2062 if ((o->flags & SEC_RELOC) == 0
2063 || o->reloc_count == 0
2064 || ((info->strip == strip_all || info->strip == strip_debugger)
2065 && (o->flags & SEC_DEBUGGING) != 0)
2066 || bfd_is_abs_section (o->output_section))
2067 continue;
2068
2069 internal_relocs
45d6a902
AM
2070 = _bfd_elf_link_read_relocs (abfd, o, (PTR) NULL,
2071 (Elf_Internal_Rela *) NULL,
2072 info->keep_memory);
930b4cb2 2073 if (internal_relocs == NULL)
b34976b6 2074 return FALSE;
930b4cb2
HPN
2075
2076 ok = mmix_elf_check_common_relocs (abfd, info, o, internal_relocs);
2077
2078 if (! info->keep_memory)
2079 free (internal_relocs);
2080
2081 if (! ok)
b34976b6 2082 return FALSE;
3c3bdf30
NC
2083 }
2084
b34976b6 2085 return TRUE;
3c3bdf30 2086}
e06fcc86 2087\f
3c3bdf30
NC
2088/* Change symbols relative to the reg contents section to instead be to
2089 the register section, and scale them down to correspond to the register
2090 number. */
2091
b34976b6 2092static bfd_boolean
754021d0 2093mmix_elf_link_output_symbol_hook (info, name, sym, input_sec, h)
3c3bdf30
NC
2094 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2095 const char *name ATTRIBUTE_UNUSED;
2096 Elf_Internal_Sym *sym;
2097 asection *input_sec;
754021d0 2098 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED;
3c3bdf30
NC
2099{
2100 if (input_sec != NULL
2101 && input_sec->name != NULL
2102 && ELF_ST_TYPE (sym->st_info) != STT_SECTION
2103 && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
2104 {
2105 sym->st_value /= 8;
2106 sym->st_shndx = SHN_REGISTER;
2107 }
2108
b34976b6 2109 return TRUE;
3c3bdf30
NC
2110}
2111
2112/* We fake a register section that holds values that are register numbers.
2113 Having a SHN_REGISTER and register section translates better to other
2114 formats (e.g. mmo) than for example a STT_REGISTER attribute.
2115 This section faking is based on a construct in elf32-mips.c. */
2116static asection mmix_elf_reg_section;
2117static asymbol mmix_elf_reg_section_symbol;
2118static asymbol *mmix_elf_reg_section_symbol_ptr;
2119
f60ebe14 2120/* Handle the special section numbers that a symbol may use. */
3c3bdf30
NC
2121
2122void
2123mmix_elf_symbol_processing (abfd, asym)
2124 bfd *abfd ATTRIBUTE_UNUSED;
2125 asymbol *asym;
2126{
2127 elf_symbol_type *elfsym;
2128
2129 elfsym = (elf_symbol_type *) asym;
2130 switch (elfsym->internal_elf_sym.st_shndx)
2131 {
2132 case SHN_REGISTER:
2133 if (mmix_elf_reg_section.name == NULL)
2134 {
2135 /* Initialize the register section. */
2136 mmix_elf_reg_section.name = MMIX_REG_SECTION_NAME;
2137 mmix_elf_reg_section.flags = SEC_NO_FLAGS;
2138 mmix_elf_reg_section.output_section = &mmix_elf_reg_section;
2139 mmix_elf_reg_section.symbol = &mmix_elf_reg_section_symbol;
2140 mmix_elf_reg_section.symbol_ptr_ptr = &mmix_elf_reg_section_symbol_ptr;
2141 mmix_elf_reg_section_symbol.name = MMIX_REG_SECTION_NAME;
2142 mmix_elf_reg_section_symbol.flags = BSF_SECTION_SYM;
2143 mmix_elf_reg_section_symbol.section = &mmix_elf_reg_section;
2144 mmix_elf_reg_section_symbol_ptr = &mmix_elf_reg_section_symbol;
2145 }
2146 asym->section = &mmix_elf_reg_section;
2147 break;
2148
2149 default:
2150 break;
2151 }
2152}
2153
2154/* Given a BFD section, try to locate the corresponding ELF section
2155 index. */
2156
b34976b6 2157static bfd_boolean
af746e92 2158mmix_elf_section_from_bfd_section (abfd, sec, retval)
3c3bdf30 2159 bfd * abfd ATTRIBUTE_UNUSED;
3c3bdf30
NC
2160 asection * sec;
2161 int * retval;
2162{
2163 if (strcmp (bfd_get_section_name (abfd, sec), MMIX_REG_SECTION_NAME) == 0)
2164 *retval = SHN_REGISTER;
2165 else
b34976b6 2166 return FALSE;
3c3bdf30 2167
b34976b6 2168 return TRUE;
3c3bdf30
NC
2169}
2170
2171/* Hook called by the linker routine which adds symbols from an object
2172 file. We must handle the special SHN_REGISTER section number here.
2173
2174 We also check that we only have *one* each of the section-start
2175 symbols, since otherwise having two with the same value would cause
2176 them to be "merged", but with the contents serialized. */
2177
b34976b6 2178bfd_boolean
3c3bdf30
NC
2179mmix_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2180 bfd *abfd;
2181 struct bfd_link_info *info ATTRIBUTE_UNUSED;
555cd476 2182 Elf_Internal_Sym *sym;
3c3bdf30
NC
2183 const char **namep ATTRIBUTE_UNUSED;
2184 flagword *flagsp ATTRIBUTE_UNUSED;
2185 asection **secp;
2186 bfd_vma *valp ATTRIBUTE_UNUSED;
2187{
2188 if (sym->st_shndx == SHN_REGISTER)
2189 *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
2190 else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
2191 && strncmp (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX,
2192 strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)) == 0)
2193 {
2194 /* See if we have another one. */
4ab82700
AM
2195 struct bfd_link_hash_entry *h = bfd_link_hash_lookup (info->hash,
2196 *namep,
b34976b6
AM
2197 FALSE,
2198 FALSE,
2199 FALSE);
3c3bdf30 2200
4ab82700 2201 if (h != NULL && h->type != bfd_link_hash_undefined)
3c3bdf30
NC
2202 {
2203 /* How do we get the asymbol (or really: the filename) from h?
4ab82700 2204 h->u.def.section->owner is NULL. */
3c3bdf30
NC
2205 ((*_bfd_error_handler)
2206 (_("%s: Error: multiple definition of `%s'; start of %s is set in a earlier linked file\n"),
2207 bfd_get_filename (abfd), *namep,
2208 *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX)));
2209 bfd_set_error (bfd_error_bad_value);
b34976b6 2210 return FALSE;
3c3bdf30
NC
2211 }
2212 }
2213
b34976b6 2214 return TRUE;
3c3bdf30
NC
2215}
2216
2217/* We consider symbols matching "L.*:[0-9]+" to be local symbols. */
2218
b34976b6 2219bfd_boolean
3c3bdf30
NC
2220mmix_elf_is_local_label_name (abfd, name)
2221 bfd *abfd;
2222 const char *name;
2223{
2224 const char *colpos;
2225 int digits;
2226
2227 /* Also include the default local-label definition. */
2228 if (_bfd_elf_is_local_label_name (abfd, name))
b34976b6 2229 return TRUE;
3c3bdf30
NC
2230
2231 if (*name != 'L')
b34976b6 2232 return FALSE;
3c3bdf30
NC
2233
2234 /* If there's no ":", or more than one, it's not a local symbol. */
2235 colpos = strchr (name, ':');
2236 if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
b34976b6 2237 return FALSE;
3c3bdf30
NC
2238
2239 /* Check that there are remaining characters and that they are digits. */
2240 if (colpos[1] == 0)
b34976b6 2241 return FALSE;
3c3bdf30
NC
2242
2243 digits = strspn (colpos + 1, "0123456789");
2244 return digits != 0 && colpos[1 + digits] == 0;
2245}
2246
2247/* We get rid of the register section here. */
2248
b34976b6 2249bfd_boolean
3c3bdf30
NC
2250mmix_elf_final_link (abfd, info)
2251 bfd *abfd;
2252 struct bfd_link_info *info;
2253{
2254 /* We never output a register section, though we create one for
2255 temporary measures. Check that nobody entered contents into it. */
2256 asection *reg_section;
2257 asection **secpp;
2258
2259 reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
2260
2261 if (reg_section != NULL)
2262 {
2263 /* FIXME: Pass error state gracefully. */
2264 if (bfd_get_section_flags (abfd, reg_section) & SEC_HAS_CONTENTS)
2265 _bfd_abort (__FILE__, __LINE__, _("Register section has contents\n"));
2266
3c3bdf30
NC
2267 /* Really remove the section. */
2268 for (secpp = &abfd->sections;
2269 *secpp != reg_section;
2270 secpp = &(*secpp)->next)
2271 ;
9e7b37b3 2272 bfd_section_list_remove (abfd, secpp);
3c3bdf30
NC
2273 --abfd->section_count;
2274 }
2275
c152c796 2276 if (! bfd_elf_final_link (abfd, info))
b34976b6 2277 return FALSE;
3c3bdf30 2278
930b4cb2
HPN
2279 /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
2280 the regular linker machinery. We do it here, like other targets with
2281 special sections. */
2282 if (info->base_file != NULL)
2283 {
2284 asection *greg_section
2285 = bfd_get_section_by_name ((bfd *) info->base_file,
2286 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2287 if (!bfd_set_section_contents (abfd,
2288 greg_section->output_section,
2289 greg_section->contents,
2290 (file_ptr) greg_section->output_offset,
eea6121a 2291 greg_section->size))
b34976b6 2292 return FALSE;
930b4cb2 2293 }
b34976b6 2294 return TRUE;
930b4cb2
HPN
2295}
2296
f60ebe14 2297/* We need to include the maximum size of PUSHJ-stubs in the initial
eea6121a 2298 section size. This is expected to shrink during linker relaxation. */
f60ebe14
HPN
2299
2300static void
2301mmix_set_relaxable_size (abfd, sec, ptr)
2302 bfd *abfd ATTRIBUTE_UNUSED;
2303 asection *sec;
2304 void *ptr;
2305{
2306 struct bfd_link_info *info = ptr;
2307
2308 /* Make sure we only do this for section where we know we want this,
2309 otherwise we might end up resetting the size of COMMONs. */
2310 if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0)
2311 return;
2312
eea6121a
AM
2313 sec->size += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2314 * MAX_PUSHJ_STUB_SIZE);
f60ebe14
HPN
2315
2316 /* For use in relocatable link, we start with a max stubs size. See
2317 mmix_elf_relax_section. */
2318 if (info->relocatable && sec->output_section)
2319 mmix_elf_section_data (sec->output_section)->pjs.stubs_size_sum
2320 += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2321 * MAX_PUSHJ_STUB_SIZE);
2322}
2323
930b4cb2
HPN
2324/* Initialize stuff for the linker-generated GREGs to match
2325 R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker. */
2326
b34976b6 2327bfd_boolean
f60ebe14 2328_bfd_mmix_before_linker_allocation (abfd, info)
930b4cb2
HPN
2329 bfd *abfd ATTRIBUTE_UNUSED;
2330 struct bfd_link_info *info;
2331{
2332 asection *bpo_gregs_section;
2333 bfd *bpo_greg_owner;
2334 struct bpo_greg_section_info *gregdata;
2335 size_t n_gregs;
2336 bfd_vma gregs_size;
2337 size_t i;
2338 size_t *bpo_reloc_indexes;
f60ebe14
HPN
2339 bfd *ibfd;
2340
2341 /* Set the initial size of sections. */
2342 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2343 bfd_map_over_sections (ibfd, mmix_set_relaxable_size, info);
930b4cb2
HPN
2344
2345 /* The bpo_greg_owner bfd is supposed to have been set by
2346 mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
2347 If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2348 bpo_greg_owner = (bfd *) info->base_file;
2349 if (bpo_greg_owner == NULL)
b34976b6 2350 return TRUE;
930b4cb2
HPN
2351
2352 bpo_gregs_section
2353 = bfd_get_section_by_name (bpo_greg_owner,
2354 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2355
930b4cb2 2356 if (bpo_gregs_section == NULL)
b34976b6 2357 return TRUE;
930b4cb2
HPN
2358
2359 /* We use the target-data handle in the ELF section data. */
f0abc2a1 2360 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
930b4cb2 2361 if (gregdata == NULL)
b34976b6 2362 return FALSE;
930b4cb2
HPN
2363
2364 n_gregs = gregdata->n_bpo_relocs;
2365 gregdata->n_allocated_bpo_gregs = n_gregs;
2366
2367 /* When this reaches zero during relaxation, all entries have been
2368 filled in and the size of the linker gregs can be calculated. */
2369 gregdata->n_remaining_bpo_relocs_this_relaxation_round = n_gregs;
2370
2371 /* Set the zeroth-order estimate for the GREGs size. */
2372 gregs_size = n_gregs * 8;
2373
2374 if (!bfd_set_section_size (bpo_greg_owner, bpo_gregs_section, gregs_size))
b34976b6 2375 return FALSE;
930b4cb2
HPN
2376
2377 /* Allocate and set up the GREG arrays. They're filled in at relaxation
2378 time. Note that we must use the max number ever noted for the array,
2379 since the index numbers were created before GC. */
2380 gregdata->reloc_request
2381 = bfd_zalloc (bpo_greg_owner,
2382 sizeof (struct bpo_reloc_request)
2383 * gregdata->n_max_bpo_relocs);
2384
2385 gregdata->bpo_reloc_indexes
2386 = bpo_reloc_indexes
2387 = bfd_alloc (bpo_greg_owner,
2388 gregdata->n_max_bpo_relocs
2389 * sizeof (size_t));
2390 if (bpo_reloc_indexes == NULL)
b34976b6 2391 return FALSE;
930b4cb2
HPN
2392
2393 /* The default order is an identity mapping. */
2394 for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2395 {
2396 bpo_reloc_indexes[i] = i;
2397 gregdata->reloc_request[i].bpo_reloc_no = i;
2398 }
2399
b34976b6 2400 return TRUE;
3c3bdf30 2401}
e06fcc86 2402\f
930b4cb2
HPN
2403/* Fill in contents in the linker allocated gregs. Everything is
2404 calculated at this point; we just move the contents into place here. */
2405
b34976b6 2406bfd_boolean
f60ebe14 2407_bfd_mmix_after_linker_allocation (abfd, link_info)
930b4cb2
HPN
2408 bfd *abfd ATTRIBUTE_UNUSED;
2409 struct bfd_link_info *link_info;
2410{
2411 asection *bpo_gregs_section;
2412 bfd *bpo_greg_owner;
2413 struct bpo_greg_section_info *gregdata;
2414 size_t n_gregs;
2415 size_t i, j;
2416 size_t lastreg;
2417 bfd_byte *contents;
2418
2419 /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
2420 when the first R_MMIX_BASE_PLUS_OFFSET is seen. If there is no such
2421 object, there was no R_MMIX_BASE_PLUS_OFFSET. */
2422 bpo_greg_owner = (bfd *) link_info->base_file;
2423 if (bpo_greg_owner == NULL)
b34976b6 2424 return TRUE;
930b4cb2
HPN
2425
2426 bpo_gregs_section
2427 = bfd_get_section_by_name (bpo_greg_owner,
2428 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2429
2430 /* This can't happen without DSO handling. When DSOs are handled
2431 without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
2432 section. */
2433 if (bpo_gregs_section == NULL)
b34976b6 2434 return TRUE;
930b4cb2
HPN
2435
2436 /* We use the target-data handle in the ELF section data. */
2437
f0abc2a1 2438 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
930b4cb2 2439 if (gregdata == NULL)
b34976b6 2440 return FALSE;
930b4cb2
HPN
2441
2442 n_gregs = gregdata->n_allocated_bpo_gregs;
2443
2444 bpo_gregs_section->contents
eea6121a 2445 = contents = bfd_alloc (bpo_greg_owner, bpo_gregs_section->size);
930b4cb2 2446 if (contents == NULL)
b34976b6 2447 return FALSE;
930b4cb2 2448
7e799044
HPN
2449 /* Sanity check: If these numbers mismatch, some relocation has not been
2450 accounted for and the rest of gregdata is probably inconsistent.
2451 It's a bug, but it's more helpful to identify it than segfaulting
2452 below. */
2453 if (gregdata->n_remaining_bpo_relocs_this_relaxation_round
2454 != gregdata->n_bpo_relocs)
2455 {
2456 (*_bfd_error_handler)
2457 (_("Internal inconsistency: remaining %u != max %u.\n\
2458 Please report this bug."),
2459 gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2460 gregdata->n_bpo_relocs);
b34976b6 2461 return FALSE;
7e799044
HPN
2462 }
2463
930b4cb2
HPN
2464 for (lastreg = 255, i = 0, j = 0; j < n_gregs; i++)
2465 if (gregdata->reloc_request[i].regindex != lastreg)
2466 {
2467 bfd_put_64 (bpo_greg_owner, gregdata->reloc_request[i].value,
2468 contents + j * 8);
2469 lastreg = gregdata->reloc_request[i].regindex;
2470 j++;
2471 }
2472
b34976b6 2473 return TRUE;
930b4cb2
HPN
2474}
2475
2476/* Sort valid relocs to come before non-valid relocs, then on increasing
2477 value. */
2478
2479static int
2480bpo_reloc_request_sort_fn (p1, p2)
2481 const PTR p1;
2482 const PTR p2;
2483{
2484 const struct bpo_reloc_request *r1 = (const struct bpo_reloc_request *) p1;
2485 const struct bpo_reloc_request *r2 = (const struct bpo_reloc_request *) p2;
2486
2487 /* Primary function is validity; non-valid relocs sorted after valid
2488 ones. */
2489 if (r1->valid != r2->valid)
2490 return r2->valid - r1->valid;
2491
4fa5c2a8
HPN
2492 /* Then sort on value. Don't simplify and return just the difference of
2493 the values: the upper bits of the 64-bit value would be truncated on
2494 a host with 32-bit ints. */
930b4cb2 2495 if (r1->value != r2->value)
4fa5c2a8 2496 return r1->value > r2->value ? 1 : -1;
930b4cb2 2497
dfbbae4c
HPN
2498 /* As a last re-sort, use the relocation number, so we get a stable
2499 sort. The *addresses* aren't stable since items are swapped during
2500 sorting. It depends on the qsort implementation if this actually
2501 happens. */
2502 return r1->bpo_reloc_no > r2->bpo_reloc_no
2503 ? 1 : (r1->bpo_reloc_no < r2->bpo_reloc_no ? -1 : 0);
930b4cb2
HPN
2504}
2505
4fa5c2a8
HPN
2506/* For debug use only. Dumps the global register allocations resulting
2507 from base-plus-offset relocs. */
2508
2509void
2510mmix_dump_bpo_gregs (link_info, pf)
2511 struct bfd_link_info *link_info;
2512 bfd_error_handler_type pf;
2513{
2514 bfd *bpo_greg_owner;
2515 asection *bpo_gregs_section;
2516 struct bpo_greg_section_info *gregdata;
2517 unsigned int i;
2518
2519 if (link_info == NULL || link_info->base_file == NULL)
2520 return;
2521
2522 bpo_greg_owner = (bfd *) link_info->base_file;
2523
2524 bpo_gregs_section
2525 = bfd_get_section_by_name (bpo_greg_owner,
2526 MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
2527
2528 if (bpo_gregs_section == NULL)
2529 return;
2530
f0abc2a1 2531 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
4fa5c2a8
HPN
2532 if (gregdata == NULL)
2533 return;
2534
2535 if (pf == NULL)
2536 pf = _bfd_error_handler;
2537
2538 /* These format strings are not translated. They are for debug purposes
2539 only and never displayed to an end user. Should they escape, we
2540 surely want them in original. */
2541 (*pf) (" n_bpo_relocs: %u\n n_max_bpo_relocs: %u\n n_remain...round: %u\n\
2542 n_allocated_bpo_gregs: %u\n", gregdata->n_bpo_relocs,
2543 gregdata->n_max_bpo_relocs,
2544 gregdata->n_remaining_bpo_relocs_this_relaxation_round,
2545 gregdata->n_allocated_bpo_gregs);
2546
2547 if (gregdata->reloc_request)
2548 for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2549 (*pf) ("%4u (%4u)/%4u#%u: 0x%08lx%08lx r: %3u o: %3u\n",
2550 i,
cf3d882d
AM
2551 (gregdata->bpo_reloc_indexes != NULL
2552 ? gregdata->bpo_reloc_indexes[i] : (size_t) -1),
4fa5c2a8
HPN
2553 gregdata->reloc_request[i].bpo_reloc_no,
2554 gregdata->reloc_request[i].valid,
2555
2556 (unsigned long) (gregdata->reloc_request[i].value >> 32),
2557 (unsigned long) gregdata->reloc_request[i].value,
2558 gregdata->reloc_request[i].regindex,
2559 gregdata->reloc_request[i].offset);
2560}
2561
930b4cb2
HPN
2562/* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
2563 when the last such reloc is done, an index-array is sorted according to
2564 the values and iterated over to produce register numbers (indexed by 0
2565 from the first allocated register number) and offsets for use in real
2566 relocation.
2567
f60ebe14
HPN
2568 PUSHJ stub accounting is also done here.
2569
930b4cb2
HPN
2570 Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
2571
b34976b6 2572static bfd_boolean
930b4cb2
HPN
2573mmix_elf_relax_section (abfd, sec, link_info, again)
2574 bfd *abfd;
2575 asection *sec;
2576 struct bfd_link_info *link_info;
b34976b6 2577 bfd_boolean *again;
930b4cb2 2578{
930b4cb2 2579 Elf_Internal_Shdr *symtab_hdr;
930b4cb2 2580 Elf_Internal_Rela *internal_relocs;
930b4cb2
HPN
2581 Elf_Internal_Rela *irel, *irelend;
2582 asection *bpo_gregs_section = NULL;
2583 struct bpo_greg_section_info *gregdata;
2584 struct bpo_reloc_section_info *bpodata
f0abc2a1 2585 = mmix_elf_section_data (sec)->bpo.reloc;
f60ebe14
HPN
2586 /* The initialization is to quiet compiler warnings. The value is to
2587 spot a missing actual initialization. */
2588 size_t bpono = (size_t) -1;
2589 size_t pjsno = 0;
930b4cb2 2590 bfd *bpo_greg_owner;
6cdc0ccc 2591 Elf_Internal_Sym *isymbuf = NULL;
eea6121a
AM
2592 bfd_size_type size = (sec->size
2593 - (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
2594 * MAX_PUSHJ_STUB_SIZE));
f60ebe14
HPN
2595
2596 mmix_elf_section_data (sec)->pjs.stubs_size_sum = 0;
930b4cb2
HPN
2597
2598 /* Assume nothing changes. */
b34976b6 2599 *again = FALSE;
930b4cb2 2600
f60ebe14
HPN
2601 /* We don't have to do anything if this section does not have relocs, or
2602 if this is not a code section. */
2603 if ((sec->flags & SEC_RELOC) == 0
930b4cb2
HPN
2604 || sec->reloc_count == 0
2605 || (sec->flags & SEC_CODE) == 0
2606 || (sec->flags & SEC_LINKER_CREATED) != 0
f60ebe14
HPN
2607 /* If no R_MMIX_BASE_PLUS_OFFSET relocs and no PUSHJ-stub relocs,
2608 then nothing to do. */
2609 || (bpodata == NULL
2610 && mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0))
b34976b6 2611 return TRUE;
930b4cb2
HPN
2612
2613 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
930b4cb2
HPN
2614
2615 bpo_greg_owner = (bfd *) link_info->base_file;
930b4cb2 2616
f60ebe14
HPN
2617 if (bpodata != NULL)
2618 {
2619 bpo_gregs_section = bpodata->bpo_greg_section;
2620 gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
2621 bpono = bpodata->first_base_plus_offset_reloc;
2622 }
2623 else
2624 gregdata = NULL;
930b4cb2
HPN
2625
2626 /* Get a copy of the native relocations. */
2627 internal_relocs
45d6a902
AM
2628 = _bfd_elf_link_read_relocs (abfd, sec, (PTR) NULL,
2629 (Elf_Internal_Rela *) NULL,
2630 link_info->keep_memory);
930b4cb2
HPN
2631 if (internal_relocs == NULL)
2632 goto error_return;
930b4cb2
HPN
2633
2634 /* Walk through them looking for relaxing opportunities. */
2635 irelend = internal_relocs + sec->reloc_count;
2636 for (irel = internal_relocs; irel < irelend; irel++)
2637 {
2638 bfd_vma symval;
f60ebe14 2639 struct elf_link_hash_entry *h = NULL;
930b4cb2 2640
f60ebe14
HPN
2641 /* We only process two relocs. */
2642 if (ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_BASE_PLUS_OFFSET
2643 && ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_PUSHJ_STUBBABLE)
930b4cb2
HPN
2644 continue;
2645
f60ebe14
HPN
2646 /* We process relocs in a distinctly different way when this is a
2647 relocatable link (for one, we don't look at symbols), so we avoid
2648 mixing its code with that for the "normal" relaxation. */
2649 if (link_info->relocatable)
2650 {
2651 /* The only transformation in a relocatable link is to generate
2652 a full stub at the location of the stub calculated for the
2653 input section, if the relocated stub location, the end of the
2654 output section plus earlier stubs, cannot be reached. Thus
2655 relocatable linking can only lead to worse code, but it still
2656 works. */
2657 if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
2658 {
2659 /* If we can reach the end of the output-section and beyond
2660 any current stubs, then we don't need a stub for this
2661 reloc. The relaxed order of output stub allocation may
2662 not exactly match the straightforward order, so we always
2663 assume presence of output stubs, which will allow
2664 relaxation only on relocations indifferent to the
2665 presence of output stub allocations for other relocations
2666 and thus the order of output stub allocation. */
2667 if (bfd_check_overflow (complain_overflow_signed,
2668 19,
2669 0,
2670 bfd_arch_bits_per_address (abfd),
2671 /* Output-stub location. */
eea6121a 2672 sec->output_section->size
f60ebe14
HPN
2673 + (mmix_elf_section_data (sec
2674 ->output_section)
2675 ->pjs.stubs_size_sum)
2676 /* Location of this PUSHJ reloc. */
2677 - (sec->output_offset + irel->r_offset)
2678 /* Don't count *this* stub twice. */
2679 - (mmix_elf_section_data (sec)
2680 ->pjs.stub_size[pjsno]
2681 + MAX_PUSHJ_STUB_SIZE))
2682 == bfd_reloc_ok)
2683 mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2684
2685 mmix_elf_section_data (sec)->pjs.stubs_size_sum
2686 += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2687
2688 pjsno++;
2689 }
2690
2691 continue;
2692 }
2693
930b4cb2
HPN
2694 /* Get the value of the symbol referred to by the reloc. */
2695 if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2696 {
2697 /* A local symbol. */
6cdc0ccc 2698 Elf_Internal_Sym *isym;
930b4cb2
HPN
2699 asection *sym_sec;
2700
6cdc0ccc
AM
2701 /* Read this BFD's local symbols if we haven't already. */
2702 if (isymbuf == NULL)
2703 {
2704 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2705 if (isymbuf == NULL)
2706 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2707 symtab_hdr->sh_info, 0,
2708 NULL, NULL, NULL);
2709 if (isymbuf == 0)
2710 goto error_return;
2711 }
930b4cb2 2712
6cdc0ccc
AM
2713 isym = isymbuf + ELF64_R_SYM (irel->r_info);
2714 if (isym->st_shndx == SHN_UNDEF)
930b4cb2 2715 sym_sec = bfd_und_section_ptr;
6cdc0ccc 2716 else if (isym->st_shndx == SHN_ABS)
930b4cb2 2717 sym_sec = bfd_abs_section_ptr;
6cdc0ccc 2718 else if (isym->st_shndx == SHN_COMMON)
930b4cb2
HPN
2719 sym_sec = bfd_com_section_ptr;
2720 else
6cdc0ccc
AM
2721 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2722 symval = (isym->st_value
930b4cb2
HPN
2723 + sym_sec->output_section->vma
2724 + sym_sec->output_offset);
2725 }
2726 else
2727 {
2728 unsigned long indx;
930b4cb2
HPN
2729
2730 /* An external symbol. */
2731 indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2732 h = elf_sym_hashes (abfd)[indx];
2733 BFD_ASSERT (h != NULL);
2734 if (h->root.type != bfd_link_hash_defined
2735 && h->root.type != bfd_link_hash_defweak)
2736 {
f60ebe14
HPN
2737 /* This appears to be a reference to an undefined symbol. Just
2738 ignore it--it will be caught by the regular reloc processing.
2739 We need to keep BPO reloc accounting consistent, though
2740 else we'll abort instead of emitting an error message. */
2741 if (ELF64_R_TYPE (irel->r_info) == R_MMIX_BASE_PLUS_OFFSET
2742 && gregdata != NULL)
2743 {
2744 gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2745 bpono++;
2746 }
930b4cb2
HPN
2747 continue;
2748 }
2749
2750 symval = (h->root.u.def.value
2751 + h->root.u.def.section->output_section->vma
2752 + h->root.u.def.section->output_offset);
2753 }
2754
f60ebe14
HPN
2755 if (ELF64_R_TYPE (irel->r_info) == (int) R_MMIX_PUSHJ_STUBBABLE)
2756 {
2757 bfd_vma value = symval + irel->r_addend;
2758 bfd_vma dot
2759 = (sec->output_section->vma
2760 + sec->output_offset
2761 + irel->r_offset);
2762 bfd_vma stubaddr
2763 = (sec->output_section->vma
2764 + sec->output_offset
eea6121a 2765 + size
f60ebe14
HPN
2766 + mmix_elf_section_data (sec)->pjs.stubs_size_sum);
2767
2768 if ((value & 3) == 0
2769 && bfd_check_overflow (complain_overflow_signed,
2770 19,
2771 0,
2772 bfd_arch_bits_per_address (abfd),
2773 value - dot
2774 - (value > dot
2775 ? mmix_elf_section_data (sec)
2776 ->pjs.stub_size[pjsno]
2777 : 0))
2778 == bfd_reloc_ok)
2779 /* If the reloc fits, no stub is needed. */
2780 mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
2781 else
2782 /* Maybe we can get away with just a JMP insn? */
2783 if ((value & 3) == 0
2784 && bfd_check_overflow (complain_overflow_signed,
2785 27,
2786 0,
2787 bfd_arch_bits_per_address (abfd),
2788 value - stubaddr
2789 - (value > dot
2790 ? mmix_elf_section_data (sec)
2791 ->pjs.stub_size[pjsno] - 4
2792 : 0))
2793 == bfd_reloc_ok)
2794 /* Yep, account for a stub consisting of a single JMP insn. */
2795 mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 4;
2796 else
2797 /* Nope, go for the full insn stub. It doesn't seem useful to
2798 emit the intermediate sizes; those will only be useful for
2799 a >64M program assuming contiguous code. */
2800 mmix_elf_section_data (sec)->pjs.stub_size[pjsno]
2801 = MAX_PUSHJ_STUB_SIZE;
2802
2803 mmix_elf_section_data (sec)->pjs.stubs_size_sum
2804 += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
2805 pjsno++;
2806 continue;
2807 }
2808
2809 /* We're looking at a R_MMIX_BASE_PLUS_OFFSET reloc. */
2810
930b4cb2
HPN
2811 gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono]].value
2812 = symval + irel->r_addend;
b34976b6 2813 gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono++]].valid = TRUE;
930b4cb2
HPN
2814 gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
2815 }
2816
2817 /* Check if that was the last BPO-reloc. If so, sort the values and
2818 calculate how many registers we need to cover them. Set the size of
2819 the linker gregs, and if the number of registers changed, indicate
2820 that we need to relax some more because we have more work to do. */
f60ebe14
HPN
2821 if (gregdata != NULL
2822 && gregdata->n_remaining_bpo_relocs_this_relaxation_round == 0)
930b4cb2
HPN
2823 {
2824 size_t i;
2825 bfd_vma prev_base;
2826 size_t regindex;
2827
2828 /* First, reset the remaining relocs for the next round. */
2829 gregdata->n_remaining_bpo_relocs_this_relaxation_round
2830 = gregdata->n_bpo_relocs;
2831
2832 qsort ((PTR) gregdata->reloc_request,
2833 gregdata->n_max_bpo_relocs,
2834 sizeof (struct bpo_reloc_request),
2835 bpo_reloc_request_sort_fn);
2836
2837 /* Recalculate indexes. When we find a change (however unlikely
2838 after the initial iteration), we know we need to relax again,
2839 since items in the GREG-array are sorted by increasing value and
2840 stored in the relaxation phase. */
2841 for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
2842 if (gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2843 != i)
2844 {
2845 gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
2846 = i;
b34976b6 2847 *again = TRUE;
930b4cb2
HPN
2848 }
2849
2850 /* Allocate register numbers (indexing from 0). Stop at the first
2851 non-valid reloc. */
2852 for (i = 0, regindex = 0, prev_base = gregdata->reloc_request[0].value;
2853 i < gregdata->n_bpo_relocs;
2854 i++)
2855 {
2856 if (gregdata->reloc_request[i].value > prev_base + 255)
2857 {
2858 regindex++;
2859 prev_base = gregdata->reloc_request[i].value;
2860 }
2861 gregdata->reloc_request[i].regindex = regindex;
2862 gregdata->reloc_request[i].offset
2863 = gregdata->reloc_request[i].value - prev_base;
2864 }
2865
2866 /* If it's not the same as the last time, we need to relax again,
2867 because the size of the section has changed. I'm not sure we
2868 actually need to do any adjustments since the shrinking happens
2869 at the start of this section, but better safe than sorry. */
2870 if (gregdata->n_allocated_bpo_gregs != regindex + 1)
2871 {
2872 gregdata->n_allocated_bpo_gregs = regindex + 1;
b34976b6 2873 *again = TRUE;
930b4cb2
HPN
2874 }
2875
eea6121a 2876 bpo_gregs_section->size = (regindex + 1) * 8;
930b4cb2
HPN
2877 }
2878
6cdc0ccc 2879 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
930b4cb2
HPN
2880 {
2881 if (! link_info->keep_memory)
6cdc0ccc
AM
2882 free (isymbuf);
2883 else
930b4cb2 2884 {
6cdc0ccc
AM
2885 /* Cache the symbols for elf_link_input_bfd. */
2886 symtab_hdr->contents = (unsigned char *) isymbuf;
930b4cb2
HPN
2887 }
2888 }
2889
6cdc0ccc
AM
2890 if (internal_relocs != NULL
2891 && elf_section_data (sec)->relocs != internal_relocs)
2892 free (internal_relocs);
2893
eea6121a 2894 if (sec->size < size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
f60ebe14
HPN
2895 abort ();
2896
eea6121a 2897 if (sec->size > size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
f60ebe14 2898 {
eea6121a 2899 sec->size = size + mmix_elf_section_data (sec)->pjs.stubs_size_sum;
f60ebe14
HPN
2900 *again = TRUE;
2901 }
2902
b34976b6 2903 return TRUE;
930b4cb2
HPN
2904
2905 error_return:
6cdc0ccc
AM
2906 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
2907 free (isymbuf);
2908 if (internal_relocs != NULL
2909 && elf_section_data (sec)->relocs != internal_relocs)
2910 free (internal_relocs);
b34976b6 2911 return FALSE;
930b4cb2 2912}
f60ebe14 2913
eea6121a 2914/* Because we set size to include the max size of pushj stubs,
f60ebe14 2915 i.e. larger than the actual section input size (see
eea6121a 2916 mmix_set_relaxablesize), we have to take care of that when reading
f60ebe14
HPN
2917 the section. */
2918
2919static bfd_boolean
2920mmix_elf_get_section_contents (abfd, section, location, offset, count)
2921 bfd *abfd;
2922 sec_ptr section;
2923 void *location;
2924 file_ptr offset;
2925 bfd_size_type count;
2926{
eea6121a
AM
2927 bfd_size_type size = (section->size
2928 - (mmix_elf_section_data (section)->pjs.n_pushj_relocs
2929 * MAX_PUSHJ_STUB_SIZE));
f60ebe14 2930
eea6121a 2931 if (offset + count > section->size)
f60ebe14
HPN
2932 {
2933 abort();
2934 bfd_set_error (bfd_error_invalid_operation);
2935 return FALSE;
2936 }
2937
eea6121a
AM
2938 /* Check bounds against the faked size. */
2939 if (offset + count > size)
f60ebe14
HPN
2940 {
2941 /* Clear the part in the faked area. */
eea6121a 2942 memset (location + size - offset, 0, count - (size - offset));
f60ebe14
HPN
2943
2944 /* If there's no initial part within the "real" contents, we're
2945 done. */
eea6121a 2946 if ((bfd_size_type) offset >= size)
f60ebe14
HPN
2947 return TRUE;
2948
2949 /* Else adjust the count and fall through to call the generic
2950 function. */
eea6121a 2951 count = size - offset;
f60ebe14
HPN
2952 }
2953
2954 return
2955 _bfd_generic_get_section_contents (abfd, section, location, offset,
2956 count);
2957}
2958
930b4cb2 2959\f
3c3bdf30
NC
2960#define ELF_ARCH bfd_arch_mmix
2961#define ELF_MACHINE_CODE EM_MMIX
2962
2963/* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
2964 However, that's too much for something somewhere in the linker part of
2965 BFD; perhaps the start-address has to be a non-zero multiple of this
2966 number, or larger than this number. The symptom is that the linker
2967 complains: "warning: allocated section `.text' not in segment". We
2968 settle for 64k; the page-size used in examples is 8k.
2969 #define ELF_MAXPAGESIZE 0x10000
2970
2971 Unfortunately, this causes excessive padding in the supposedly small
2972 for-education programs that are the expected usage (where people would
2973 inspect output). We stick to 256 bytes just to have *some* default
2974 alignment. */
2975#define ELF_MAXPAGESIZE 0x100
2976
2977#define TARGET_BIG_SYM bfd_elf64_mmix_vec
2978#define TARGET_BIG_NAME "elf64-mmix"
2979
2980#define elf_info_to_howto_rel NULL
2981#define elf_info_to_howto mmix_info_to_howto_rela
2982#define elf_backend_relocate_section mmix_elf_relocate_section
2983#define elf_backend_gc_mark_hook mmix_elf_gc_mark_hook
930b4cb2
HPN
2984#define elf_backend_gc_sweep_hook mmix_elf_gc_sweep_hook
2985
3c3bdf30
NC
2986#define elf_backend_link_output_symbol_hook \
2987 mmix_elf_link_output_symbol_hook
2988#define elf_backend_add_symbol_hook mmix_elf_add_symbol_hook
2989
2990#define elf_backend_check_relocs mmix_elf_check_relocs
2991#define elf_backend_symbol_processing mmix_elf_symbol_processing
2992
2993#define bfd_elf64_bfd_is_local_label_name \
2994 mmix_elf_is_local_label_name
2995
2996#define elf_backend_may_use_rel_p 0
2997#define elf_backend_may_use_rela_p 1
2998#define elf_backend_default_use_rela_p 1
2999
3000#define elf_backend_can_gc_sections 1
3001#define elf_backend_section_from_bfd_section \
3002 mmix_elf_section_from_bfd_section
3003
f0abc2a1 3004#define bfd_elf64_new_section_hook mmix_elf_new_section_hook
3c3bdf30 3005#define bfd_elf64_bfd_final_link mmix_elf_final_link
930b4cb2 3006#define bfd_elf64_bfd_relax_section mmix_elf_relax_section
f60ebe14 3007#define bfd_elf64_get_section_contents mmix_elf_get_section_contents
3c3bdf30
NC
3008
3009#include "elf64-target.h"
This page took 0.335766 seconds and 4 git commands to generate.