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