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