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