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