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