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