Coverage Report

Created: 2026-07-25 10:20

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