Coverage Report

Created: 2026-03-10 08:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/coff-mips.c
Line
Count
Source
1
/* BFD back-end for MIPS Extended-Coff files.
2
   Copyright (C) 1990-2026 Free Software Foundation, Inc.
3
   Original version by Per Bothner.
4
   Full support added by Ian Lance Taylor, ian@cygnus.com.
5
6
   This file is part of BFD, the Binary File Descriptor library.
7
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
22
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "bfdlink.h"
26
#include "libbfd.h"
27
#include "coff/internal.h"
28
#include "coff/sym.h"
29
#include "coff/symconst.h"
30
#include "coff/ecoff.h"
31
#include "coff/mips.h"
32
#include "libcoff.h"
33
#include "libecoff.h"
34
35
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
36
0
#define OCTETS_PER_BYTE(ABFD, SEC) 1
37

38
/* Prototypes for static functions.  */
39
static bfd_reloc_status_type
40
mips_generic_reloc
41
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42
static bfd_reloc_status_type
43
mips_refhi_reloc
44
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45
static bfd_reloc_status_type
46
mips_reflo_reloc
47
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48
static bfd_reloc_status_type
49
mips_gprel_reloc
50
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
51
52

53
/* ECOFF has COFF sections, but the debugging information is stored in
54
   a completely different format.  ECOFF targets use some of the
55
   swapping routines from coffswap.h, and some of the generic COFF
56
   routines in coffgen.c, but, unlike the real COFF targets, do not
57
   use coffcode.h itself.
58
59
   Get the generic COFF swapping routines, except for the reloc,
60
   symbol, and lineno ones.  Give them ECOFF names.  */
61
#define MIPSECOFF
62
#define NO_COFF_RELOCS
63
#define NO_COFF_SYMBOLS
64
#define NO_COFF_LINENOS
65
#define coff_swap_filehdr_in  mips_ecoff_swap_filehdr_in
66
#define coff_swap_filehdr_out mips_ecoff_swap_filehdr_out
67
#define coff_swap_aouthdr_in  mips_ecoff_swap_aouthdr_in
68
#define coff_swap_aouthdr_out mips_ecoff_swap_aouthdr_out
69
#define coff_swap_scnhdr_in   mips_ecoff_swap_scnhdr_in
70
#define coff_swap_scnhdr_out  mips_ecoff_swap_scnhdr_out
71
72
#include "coffswap.h"
73
74
/* Get the ECOFF swapping routines.  */
75
#define ECOFF_32
76
#include "ecoffswap.h"
77

78
/* How to process the various relocs types.  */
79
80
static reloc_howto_type mips_howto_table[] =
81
{
82
  /* Reloc type 0 is ignored.  The reloc reading code ensures that
83
     this is a reference to the .abs section, which will cause
84
     bfd_perform_relocation to do nothing.  */
85
  HOWTO (MIPS_R_IGNORE, /* type */
86
   0,     /* rightshift */
87
   1,     /* size */
88
   8,     /* bitsize */
89
   false,     /* pc_relative */
90
   0,     /* bitpos */
91
   complain_overflow_dont, /* complain_on_overflow */
92
   0,     /* special_function */
93
   "IGNORE",    /* name */
94
   false,     /* partial_inplace */
95
   0,     /* src_mask */
96
   0,     /* dst_mask */
97
   false),    /* pcrel_offset */
98
99
  /* A 16 bit reference to a symbol, normally from a data section.  */
100
  HOWTO (MIPS_R_REFHALF,  /* type */
101
   0,     /* rightshift */
102
   2,     /* size */
103
   16,      /* bitsize */
104
   false,     /* pc_relative */
105
   0,     /* bitpos */
106
   complain_overflow_bitfield, /* complain_on_overflow */
107
   mips_generic_reloc,  /* special_function */
108
   "REFHALF",   /* name */
109
   true,      /* partial_inplace */
110
   0xffff,    /* src_mask */
111
   0xffff,    /* dst_mask */
112
   false),    /* pcrel_offset */
113
114
  /* A 32 bit reference to a symbol, normally from a data section.  */
115
  HOWTO (MIPS_R_REFWORD,  /* type */
116
   0,     /* rightshift */
117
   4,     /* size */
118
   32,      /* bitsize */
119
   false,     /* pc_relative */
120
   0,     /* bitpos */
121
   complain_overflow_bitfield, /* complain_on_overflow */
122
   mips_generic_reloc,  /* special_function */
123
   "REFWORD",   /* name */
124
   true,      /* partial_inplace */
125
   0xffffffff,    /* src_mask */
126
   0xffffffff,    /* dst_mask */
127
   false),    /* pcrel_offset */
128
129
  /* A 26 bit absolute jump address.  */
130
  HOWTO (MIPS_R_JMPADDR,  /* type */
131
   2,     /* rightshift */
132
   4,     /* size */
133
   26,      /* bitsize */
134
   false,     /* pc_relative */
135
   0,     /* bitpos */
136
   complain_overflow_dont, /* complain_on_overflow */
137
        /* This needs complex overflow
138
           detection, because the upper four
139
           bits must match the PC.  */
140
   mips_generic_reloc,  /* special_function */
141
   "JMPADDR",   /* name */
142
   true,      /* partial_inplace */
143
   0x3ffffff,   /* src_mask */
144
   0x3ffffff,   /* dst_mask */
145
   false),    /* pcrel_offset */
146
147
  /* The high 16 bits of a symbol value.  Handled by the function
148
     mips_refhi_reloc.  */
149
  HOWTO (MIPS_R_REFHI,    /* type */
150
   16,      /* rightshift */
151
   4,     /* size */
152
   16,      /* bitsize */
153
   false,     /* pc_relative */
154
   0,     /* bitpos */
155
   complain_overflow_bitfield, /* complain_on_overflow */
156
   mips_refhi_reloc,  /* special_function */
157
   "REFHI",   /* name */
158
   true,      /* partial_inplace */
159
   0xffff,    /* src_mask */
160
   0xffff,    /* dst_mask */
161
   false),    /* pcrel_offset */
162
163
  /* The low 16 bits of a symbol value.  */
164
  HOWTO (MIPS_R_REFLO,    /* type */
165
   0,     /* rightshift */
166
   4,     /* size */
167
   16,      /* bitsize */
168
   false,     /* pc_relative */
169
   0,     /* bitpos */
170
   complain_overflow_dont, /* complain_on_overflow */
171
   mips_reflo_reloc,  /* special_function */
172
   "REFLO",   /* name */
173
   true,      /* partial_inplace */
174
   0xffff,    /* src_mask */
175
   0xffff,    /* dst_mask */
176
   false),    /* pcrel_offset */
177
178
  /* A reference to an offset from the gp register.  Handled by the
179
     function mips_gprel_reloc.  */
180
  HOWTO (MIPS_R_GPREL,    /* type */
181
   0,     /* rightshift */
182
   4,     /* size */
183
   16,      /* bitsize */
184
   false,     /* pc_relative */
185
   0,     /* bitpos */
186
   complain_overflow_signed, /* complain_on_overflow */
187
   mips_gprel_reloc,  /* special_function */
188
   "GPREL",   /* name */
189
   true,      /* partial_inplace */
190
   0xffff,    /* src_mask */
191
   0xffff,    /* dst_mask */
192
   false),    /* pcrel_offset */
193
194
  /* A reference to a literal using an offset from the gp register.
195
     Handled by the function mips_gprel_reloc.  */
196
  HOWTO (MIPS_R_LITERAL,  /* type */
197
   0,     /* rightshift */
198
   4,     /* size */
199
   16,      /* bitsize */
200
   false,     /* pc_relative */
201
   0,     /* bitpos */
202
   complain_overflow_signed, /* complain_on_overflow */
203
   mips_gprel_reloc,  /* special_function */
204
   "LITERAL",   /* name */
205
   true,      /* partial_inplace */
206
   0xffff,    /* src_mask */
207
   0xffff,    /* dst_mask */
208
   false),    /* pcrel_offset */
209
210
  EMPTY_HOWTO (8),
211
  EMPTY_HOWTO (9),
212
  EMPTY_HOWTO (10),
213
  EMPTY_HOWTO (11),
214
215
  /* FIXME: This relocation is used (internally only) to represent branches
216
     when assembling.  It should never appear in output files, and
217
     be removed.  (It used to be used for embedded-PIC support.)  */
218
  HOWTO (MIPS_R_PCREL16,  /* type */
219
   2,     /* rightshift */
220
   4,     /* size */
221
   16,      /* bitsize */
222
   true,      /* pc_relative */
223
   0,     /* bitpos */
224
   complain_overflow_signed, /* complain_on_overflow */
225
   mips_generic_reloc,  /* special_function */
226
   "PCREL16",   /* name */
227
   true,      /* partial_inplace */
228
   0xffff,    /* src_mask */
229
   0xffff,    /* dst_mask */
230
   true),     /* pcrel_offset */
231
};
232
233
#define MIPS_HOWTO_COUNT \
234
  (sizeof mips_howto_table / sizeof mips_howto_table[0])
235

236
/* See whether the magic number matches.  */
237
238
static bool
239
mips_ecoff_bad_format_hook (bfd * abfd, void * filehdr)
240
407k
{
241
407k
  struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
242
243
407k
  switch (internal_f->f_magic)
244
407k
    {
245
1.23k
    case MIPS_MAGIC_1:
246
      /* I don't know what endianness this implies.  */
247
1.23k
      return true;
248
249
89
    case MIPS_MAGIC_BIG:
250
184
    case MIPS_MAGIC_BIG2:
251
279
    case MIPS_MAGIC_BIG3:
252
279
      return bfd_big_endian (abfd);
253
254
56
    case MIPS_MAGIC_LITTLE:
255
2.53k
    case MIPS_MAGIC_LITTLE2:
256
2.79k
    case MIPS_MAGIC_LITTLE3:
257
2.79k
      return bfd_little_endian (abfd);
258
259
403k
    default:
260
403k
      return false;
261
407k
    }
262
407k
}
263

264
/* Reloc handling.  MIPS ECOFF relocs are packed into 8 bytes in
265
   external form.  They use a bit which indicates whether the symbol
266
   is external.  */
267
268
/* Swap a reloc in.  */
269
270
static void
271
mips_ecoff_swap_reloc_in (bfd *  abfd,
272
        void * ext_ptr,
273
        struct internal_reloc *intern)
274
575k
{
275
575k
  const RELOC *ext = (RELOC *) ext_ptr;
276
277
575k
  intern->r_vaddr = H_GET_32 (abfd, ext->r_vaddr);
278
575k
  if (bfd_header_big_endian (abfd))
279
42.7k
    {
280
42.7k
      intern->r_symndx = (((int) ext->r_bits[0]
281
42.7k
         << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
282
42.7k
        | ((int) ext->r_bits[1]
283
42.7k
           << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
284
42.7k
        | ((int) ext->r_bits[2]
285
42.7k
           << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
286
42.7k
      intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
287
42.7k
      >> RELOC_BITS3_TYPE_SH_BIG);
288
42.7k
      intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
289
42.7k
    }
290
533k
  else
291
533k
    {
292
533k
      intern->r_symndx = (((int) ext->r_bits[0]
293
533k
         << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
294
533k
        | ((int) ext->r_bits[1]
295
533k
           << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
296
533k
        | ((int) ext->r_bits[2]
297
533k
           << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
298
533k
      intern->r_type = (((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
299
533k
       >> RELOC_BITS3_TYPE_SH_LITTLE)
300
533k
      | ((ext->r_bits[3] & RELOC_BITS3_TYPEHI_LITTLE)
301
533k
         << RELOC_BITS3_TYPEHI_SH_LITTLE));
302
533k
      intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
303
533k
    }
304
575k
}
305
306
/* Swap a reloc out.  */
307
308
static void
309
mips_ecoff_swap_reloc_out (bfd * abfd,
310
         const struct internal_reloc * intern,
311
         void * dst)
312
634
{
313
634
  RELOC *ext = (RELOC *) dst;
314
634
  long r_symndx;
315
316
634
  BFD_ASSERT (intern->r_extern
317
634
        || (intern->r_symndx >= 0 && intern->r_symndx <= 12));
318
319
634
  r_symndx = intern->r_symndx;
320
321
634
  H_PUT_32 (abfd, intern->r_vaddr, ext->r_vaddr);
322
634
  if (bfd_header_big_endian (abfd))
323
634
    {
324
634
      ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
325
634
      ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
326
634
      ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
327
634
      ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
328
634
       & RELOC_BITS3_TYPE_BIG)
329
634
      | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
330
634
    }
331
0
  else
332
0
    {
333
0
      ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
334
0
      ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
335
0
      ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
336
0
      ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
337
0
       & RELOC_BITS3_TYPE_LITTLE)
338
0
      | ((intern->r_type >> RELOC_BITS3_TYPEHI_SH_LITTLE
339
0
          & RELOC_BITS3_TYPEHI_LITTLE))
340
0
      | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
341
0
    }
342
634
}
343
344
/* Finish canonicalizing a reloc.  Part of this is generic to all
345
   ECOFF targets, and that part is in ecoff.c.  The rest is done in
346
   this backend routine.  It must fill in the howto field.  */
347
348
static void
349
mips_adjust_reloc_in (bfd *abfd,
350
          const struct internal_reloc *intern,
351
          arelent *rptr)
352
575k
{
353
575k
  if (intern->r_type > MIPS_R_PCREL16)
354
136k
    {
355
      /* xgettext:c-format */
356
136k
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
357
136k
        abfd, intern->r_type);
358
136k
      bfd_set_error (bfd_error_bad_value);
359
136k
      rptr->howto  = NULL;
360
136k
      return;
361
136k
    }
362
363
439k
  if (! intern->r_extern
364
418k
      && (intern->r_type == MIPS_R_GPREL
365
412k
    || intern->r_type == MIPS_R_LITERAL))
366
7.87k
    rptr->addend += ecoff_data (abfd)->gp;
367
368
  /* If the type is MIPS_R_IGNORE, make sure this is a reference to
369
     the absolute section so that the reloc is ignored.  */
370
439k
  if (intern->r_type == MIPS_R_IGNORE)
371
340k
    rptr->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
372
373
439k
  rptr->howto = &mips_howto_table[intern->r_type];
374
439k
}
375
376
/* Make any adjustments needed to a reloc before writing it out.  None
377
   are needed for MIPS.  */
378
379
static void
380
mips_adjust_reloc_out (bfd *abfd ATTRIBUTE_UNUSED,
381
           const arelent *rel ATTRIBUTE_UNUSED,
382
           struct internal_reloc *intern ATTRIBUTE_UNUSED)
383
634
{
384
634
}
385
386
/* ECOFF relocs are either against external symbols, or against
387
   sections.  If we are producing relocatable output, and the reloc
388
   is against an external symbol, and nothing has given us any
389
   additional addend, the resulting reloc will also be against the
390
   same symbol.  In such a case, we don't want to change anything
391
   about the way the reloc is handled, since it will all be done at
392
   final link time.  Rather than put special case code into
393
   bfd_perform_relocation, all the reloc types use this howto
394
   function.  It just short circuits the reloc if producing
395
   relocatable output against an external symbol.  */
396
397
static bfd_reloc_status_type
398
mips_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
399
        arelent *reloc_entry,
400
        asymbol *symbol,
401
        void * data ATTRIBUTE_UNUSED,
402
        asection *input_section,
403
        bfd *output_bfd,
404
        char **error_message ATTRIBUTE_UNUSED)
405
0
{
406
0
  if (output_bfd != (bfd *) NULL
407
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
408
0
      && reloc_entry->addend == 0)
409
0
    {
410
0
      reloc_entry->address += input_section->output_offset;
411
0
      return bfd_reloc_ok;
412
0
    }
413
414
0
  return bfd_reloc_continue;
415
0
}
416
417
/* Do a REFHI relocation.  This has to be done in combination with a
418
   REFLO reloc, because there is a carry from the REFLO to the REFHI.
419
   Here we just save the information we need; we do the actual
420
   relocation when we see the REFLO.  MIPS ECOFF requires that the
421
   REFLO immediately follow the REFHI.  As a GNU extension, we permit
422
   an arbitrary number of HI relocs to be associated with a single LO
423
   reloc.  This extension permits gcc to output the HI and LO relocs
424
   itself.  */
425
426
static bfd_reloc_status_type
427
mips_refhi_reloc (bfd *abfd,
428
      arelent *reloc_entry,
429
      asymbol *symbol,
430
      void * data,
431
      asection *input_section,
432
      bfd *output_bfd,
433
      char **error_message ATTRIBUTE_UNUSED)
434
13
{
435
13
  bfd_reloc_status_type ret;
436
13
  bfd_vma relocation;
437
13
  struct mips_hi *n;
438
439
  /* If we're relocating, and this an external symbol, we don't want
440
     to change anything.  */
441
13
  if (output_bfd != (bfd *) NULL
442
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
443
0
      && reloc_entry->addend == 0)
444
0
    {
445
0
      reloc_entry->address += input_section->output_offset;
446
0
      return bfd_reloc_ok;
447
0
    }
448
449
13
  ret = bfd_reloc_ok;
450
13
  if (bfd_is_und_section (symbol->section)
451
0
      && output_bfd == (bfd *) NULL)
452
0
    ret = bfd_reloc_undefined;
453
454
13
  if (bfd_is_com_section (symbol->section))
455
0
    relocation = 0;
456
13
  else
457
13
    relocation = symbol->value;
458
459
13
  relocation += symbol->section->output_section->vma;
460
13
  relocation += symbol->section->output_offset;
461
13
  relocation += reloc_entry->addend;
462
463
13
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
464
13
    return bfd_reloc_outofrange;
465
466
  /* Save the information, and let REFLO do the actual relocation.  */
467
0
  n = (struct mips_hi *) bfd_malloc ((bfd_size_type) sizeof *n);
468
0
  if (n == NULL)
469
0
    return bfd_reloc_outofrange;
470
0
  n->addr = (bfd_byte *) data + reloc_entry->address;
471
0
  n->addend = relocation;
472
0
  n->next = ecoff_data (abfd)->mips_refhi_list;
473
0
  ecoff_data (abfd)->mips_refhi_list = n;
474
475
0
  if (output_bfd != (bfd *) NULL)
476
0
    reloc_entry->address += input_section->output_offset;
477
478
0
  return ret;
479
0
}
480
481
/* Do a REFLO relocation.  This is a straightforward 16 bit inplace
482
   relocation; this function exists in order to do the REFHI
483
   relocation described above.  */
484
485
static bfd_reloc_status_type
486
mips_reflo_reloc (bfd *abfd,
487
      arelent *reloc_entry,
488
      asymbol *symbol,
489
      void * data,
490
      asection *input_section,
491
      bfd *output_bfd,
492
      char **error_message)
493
0
{
494
0
  if (ecoff_data (abfd)->mips_refhi_list != NULL)
495
0
    {
496
0
      struct mips_hi *l;
497
498
0
      l = ecoff_data (abfd)->mips_refhi_list;
499
0
      while (l != NULL)
500
0
  {
501
0
    unsigned long insn;
502
0
    unsigned long val;
503
0
    unsigned long vallo;
504
0
    struct mips_hi *next;
505
0
    bfd_size_type octets = (reloc_entry->address
506
0
          * OCTETS_PER_BYTE (abfd, input_section));
507
0
    bfd_byte *loc = (bfd_byte *) data + octets;
508
509
0
    if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
510
0
            input_section, octets))
511
0
      return bfd_reloc_outofrange;
512
513
    /* Do the REFHI relocation.  Note that we actually don't
514
       need to know anything about the REFLO itself, except
515
       where to find the low 16 bits of the addend needed by the
516
       REFHI.  */
517
0
    insn = bfd_get_32 (abfd, l->addr);
518
0
    vallo = bfd_get_32 (abfd, loc) & 0xffff;
519
0
    val = ((insn & 0xffff) << 16) + vallo;
520
0
    val += l->addend;
521
522
    /* The low order 16 bits are always treated as a signed
523
       value.  Therefore, a negative value in the low order bits
524
       requires an adjustment in the high order bits.  We need
525
       to make this adjustment in two ways: once for the bits we
526
       took from the data, and once for the bits we are putting
527
       back in to the data.  */
528
0
    if ((vallo & 0x8000) != 0)
529
0
      val -= 0x10000;
530
0
    if ((val & 0x8000) != 0)
531
0
      val += 0x10000;
532
533
0
    insn = (insn &~ (unsigned) 0xffff) | ((val >> 16) & 0xffff);
534
0
    bfd_put_32 (abfd, (bfd_vma) insn, l->addr);
535
536
0
    next = l->next;
537
0
    free (l);
538
0
    l = next;
539
0
  }
540
541
0
      ecoff_data (abfd)->mips_refhi_list = NULL;
542
0
    }
543
544
  /* Now do the REFLO reloc in the usual way.  */
545
0
  return mips_generic_reloc (abfd, reloc_entry, symbol, data,
546
0
           input_section, output_bfd, error_message);
547
0
}
548
549
/* Do a GPREL relocation.  This is a 16 bit value which must become
550
   the offset from the gp register.  */
551
552
static bfd_reloc_status_type
553
mips_gprel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
554
      arelent *reloc_entry,
555
      asymbol *symbol,
556
      void * data,
557
      asection *input_section,
558
      bfd *output_bfd,
559
      char **error_message ATTRIBUTE_UNUSED)
560
0
{
561
0
  bool relocatable;
562
0
  bfd_vma gp;
563
0
  bfd_vma relocation;
564
0
  unsigned long val;
565
0
  unsigned long insn;
566
567
  /* If we're relocating, and this is an external symbol with no
568
     addend, we don't want to change anything.  We will only have an
569
     addend if this is a newly created reloc, not read from an ECOFF
570
     file.  */
571
0
  if (output_bfd != (bfd *) NULL
572
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
573
0
      && reloc_entry->addend == 0)
574
0
    {
575
0
      reloc_entry->address += input_section->output_offset;
576
0
      return bfd_reloc_ok;
577
0
    }
578
579
0
  if (output_bfd != (bfd *) NULL)
580
0
    relocatable = true;
581
0
  else
582
0
    {
583
0
      relocatable = false;
584
0
      output_bfd = symbol->section->output_section->owner;
585
0
      if (output_bfd == NULL)
586
0
  return bfd_reloc_undefined;
587
0
    }
588
589
  /* We have to figure out the gp value, so that we can adjust the
590
     symbol value correctly.  We look up the symbol _gp in the output
591
     BFD.  If we can't find it, we're stuck.  We cache it in the ECOFF
592
     target data.  We don't need to adjust the symbol value for an
593
     external symbol if we are producing relocatable output.  */
594
0
  gp = _bfd_get_gp_value (output_bfd);
595
0
  if (gp == 0
596
0
      && (! relocatable
597
0
    || (symbol->flags & BSF_SECTION_SYM) != 0))
598
0
    {
599
0
      if (relocatable)
600
0
  {
601
    /* Make up a value.  */
602
0
    gp = symbol->section->output_section->vma + 0x4000;
603
0
    _bfd_set_gp_value (output_bfd, gp);
604
0
  }
605
0
      else
606
0
  {
607
0
    unsigned int count;
608
0
    asymbol **sym;
609
0
    unsigned int i;
610
611
0
    count = bfd_get_symcount (output_bfd);
612
0
    sym = bfd_get_outsymbols (output_bfd);
613
614
0
    if (sym == (asymbol **) NULL)
615
0
      i = count;
616
0
    else
617
0
      {
618
0
        for (i = 0; i < count; i++, sym++)
619
0
    {
620
0
      register const char *name;
621
622
0
      name = bfd_asymbol_name (*sym);
623
0
      if (*name == '_' && strcmp (name, "_gp") == 0)
624
0
        {
625
0
          gp = bfd_asymbol_value (*sym);
626
0
          _bfd_set_gp_value (output_bfd, gp);
627
0
          break;
628
0
        }
629
0
    }
630
0
      }
631
632
0
    if (i >= count)
633
0
      {
634
        /* Only get the error once.  */
635
0
        gp = 4;
636
0
        _bfd_set_gp_value (output_bfd, gp);
637
0
        *error_message =
638
0
    (char *) _("GP relative relocation when _gp not defined");
639
0
        return bfd_reloc_dangerous;
640
0
      }
641
0
  }
642
0
    }
643
644
0
  if (bfd_is_com_section (symbol->section))
645
0
    relocation = 0;
646
0
  else
647
0
    relocation = symbol->value;
648
649
0
  relocation += symbol->section->output_section->vma;
650
0
  relocation += symbol->section->output_offset;
651
652
0
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
653
0
    return bfd_reloc_outofrange;
654
655
0
  insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
656
657
  /* Set val to the offset into the section or symbol.  */
658
0
  val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
659
0
  if (val & 0x8000)
660
0
    val -= 0x10000;
661
662
  /* Adjust val for the final section location and GP value.  If we
663
     are producing relocatable output, we don't want to do this for
664
     an external symbol.  */
665
0
  if (! relocatable
666
0
      || (symbol->flags & BSF_SECTION_SYM) != 0)
667
0
    val += relocation - gp;
668
669
0
  insn = (insn &~ (unsigned) 0xffff) | (val & 0xffff);
670
0
  bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + reloc_entry->address);
671
672
0
  if (relocatable)
673
0
    reloc_entry->address += input_section->output_offset;
674
675
  /* Make sure it fit in 16 bits.  */
676
0
  if ((long) val >= 0x8000 || (long) val < -0x8000)
677
0
    return bfd_reloc_overflow;
678
679
0
  return bfd_reloc_ok;
680
0
}
681
682
/* Get the howto structure for a generic reloc type.  */
683
684
static reloc_howto_type *
685
mips_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
686
          bfd_reloc_code_real_type code)
687
0
{
688
0
  int mips_type;
689
690
0
  switch (code)
691
0
    {
692
0
    case BFD_RELOC_16:
693
0
      mips_type = MIPS_R_REFHALF;
694
0
      break;
695
0
    case BFD_RELOC_32:
696
0
    case BFD_RELOC_CTOR:
697
0
      mips_type = MIPS_R_REFWORD;
698
0
      break;
699
0
    case BFD_RELOC_MIPS_JMP:
700
0
      mips_type = MIPS_R_JMPADDR;
701
0
      break;
702
0
    case BFD_RELOC_HI16_S:
703
0
      mips_type = MIPS_R_REFHI;
704
0
      break;
705
0
    case BFD_RELOC_LO16:
706
0
      mips_type = MIPS_R_REFLO;
707
0
      break;
708
0
    case BFD_RELOC_GPREL16:
709
0
      mips_type = MIPS_R_GPREL;
710
0
      break;
711
0
    case BFD_RELOC_MIPS_LITERAL:
712
0
      mips_type = MIPS_R_LITERAL;
713
0
      break;
714
0
    case BFD_RELOC_16_PCREL_S2:
715
0
      mips_type = MIPS_R_PCREL16;
716
0
      break;
717
0
    default:
718
0
      return (reloc_howto_type *) NULL;
719
0
    }
720
721
0
  return &mips_howto_table[mips_type];
722
0
}
723
724
static reloc_howto_type *
725
mips_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
726
          const char *r_name)
727
0
{
728
0
  unsigned int i;
729
730
0
  for (i = 0;
731
0
       i < sizeof (mips_howto_table) / sizeof (mips_howto_table[0]);
732
0
       i++)
733
0
    if (mips_howto_table[i].name != NULL
734
0
  && strcasecmp (mips_howto_table[i].name, r_name) == 0)
735
0
      return &mips_howto_table[i];
736
737
0
  return NULL;
738
0
}
739

740
/* A helper routine for mips_relocate_section which handles the REFHI
741
   relocations.  The REFHI relocation must be followed by a REFLO
742
   relocation, and the addend used is formed from the addends of both
743
   instructions.  */
744
745
static void
746
mips_relocate_hi (struct internal_reloc *refhi,
747
      struct internal_reloc *reflo,
748
      bfd *input_bfd,
749
      asection *input_section,
750
      bfd_byte *contents,
751
      bfd_vma relocation)
752
0
{
753
0
  unsigned long insn;
754
0
  unsigned long val;
755
0
  unsigned long vallo;
756
757
0
  if (refhi == NULL)
758
0
    return;
759
760
0
  insn = bfd_get_32 (input_bfd,
761
0
         contents + refhi->r_vaddr - input_section->vma);
762
0
  if (reflo == NULL)
763
0
    vallo = 0;
764
0
  else
765
0
    vallo = (bfd_get_32 (input_bfd,
766
0
       contents + reflo->r_vaddr - input_section->vma)
767
0
       & 0xffff);
768
769
0
  val = ((insn & 0xffff) << 16) + vallo;
770
0
  val += relocation;
771
772
  /* The low order 16 bits are always treated as a signed value.
773
     Therefore, a negative value in the low order bits requires an
774
     adjustment in the high order bits.  We need to make this
775
     adjustment in two ways: once for the bits we took from the data,
776
     and once for the bits we are putting back in to the data.  */
777
0
  if ((vallo & 0x8000) != 0)
778
0
    val -= 0x10000;
779
780
0
  if ((val & 0x8000) != 0)
781
0
    val += 0x10000;
782
783
0
  insn = (insn &~ (unsigned) 0xffff) | ((val >> 16) & 0xffff);
784
0
  bfd_put_32 (input_bfd, (bfd_vma) insn,
785
0
        contents + refhi->r_vaddr - input_section->vma);
786
0
}
787
788
/* Relocate a section while linking a MIPS ECOFF file.  */
789
790
static bool
791
mips_relocate_section (bfd *output_bfd,
792
           struct bfd_link_info *info,
793
           bfd *input_bfd,
794
           asection *input_section,
795
           bfd_byte *contents,
796
           void * external_relocs)
797
0
{
798
0
  asection **symndx_to_section;
799
0
  struct ecoff_link_hash_entry **sym_hashes;
800
0
  bfd_vma gp;
801
0
  bool gp_undefined;
802
0
  struct external_reloc *ext_rel;
803
0
  struct external_reloc *ext_rel_end;
804
0
  bool got_lo;
805
0
  struct internal_reloc lo_int_rel;
806
0
  bfd_size_type amt;
807
808
0
  BFD_ASSERT (input_bfd->xvec->byteorder
809
0
        == output_bfd->xvec->byteorder);
810
811
  /* We keep a table mapping the symndx found in an internal reloc to
812
     the appropriate section.  This is faster than looking up the
813
     section by name each time.  */
814
0
  symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
815
0
  if (symndx_to_section == (asection **) NULL)
816
0
    {
817
0
      amt = NUM_RELOC_SECTIONS * sizeof (asection *);
818
0
      symndx_to_section = (asection **) bfd_alloc (input_bfd, amt);
819
0
      if (!symndx_to_section)
820
0
  return false;
821
822
0
      symndx_to_section[RELOC_SECTION_NONE] = NULL;
823
0
      symndx_to_section[RELOC_SECTION_TEXT] =
824
0
  bfd_get_section_by_name (input_bfd, ".text");
825
0
      symndx_to_section[RELOC_SECTION_RDATA] =
826
0
  bfd_get_section_by_name (input_bfd, ".rdata");
827
0
      symndx_to_section[RELOC_SECTION_DATA] =
828
0
  bfd_get_section_by_name (input_bfd, ".data");
829
0
      symndx_to_section[RELOC_SECTION_SDATA] =
830
0
  bfd_get_section_by_name (input_bfd, ".sdata");
831
0
      symndx_to_section[RELOC_SECTION_SBSS] =
832
0
  bfd_get_section_by_name (input_bfd, ".sbss");
833
0
      symndx_to_section[RELOC_SECTION_BSS] =
834
0
  bfd_get_section_by_name (input_bfd, ".bss");
835
0
      symndx_to_section[RELOC_SECTION_INIT] =
836
0
  bfd_get_section_by_name (input_bfd, ".init");
837
0
      symndx_to_section[RELOC_SECTION_LIT8] =
838
0
  bfd_get_section_by_name (input_bfd, ".lit8");
839
0
      symndx_to_section[RELOC_SECTION_LIT4] =
840
0
  bfd_get_section_by_name (input_bfd, ".lit4");
841
0
      symndx_to_section[RELOC_SECTION_XDATA] = NULL;
842
0
      symndx_to_section[RELOC_SECTION_PDATA] = NULL;
843
0
      symndx_to_section[RELOC_SECTION_FINI] =
844
0
  bfd_get_section_by_name (input_bfd, ".fini");
845
0
      symndx_to_section[RELOC_SECTION_LITA] = NULL;
846
0
      symndx_to_section[RELOC_SECTION_ABS] = NULL;
847
848
0
      ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
849
0
    }
850
851
0
  sym_hashes = ecoff_data (input_bfd)->sym_hashes;
852
853
0
  gp = _bfd_get_gp_value (output_bfd);
854
0
  if (gp == 0)
855
0
    gp_undefined = true;
856
0
  else
857
0
    gp_undefined = false;
858
859
0
  got_lo = false;
860
861
0
  ext_rel = (struct external_reloc *) external_relocs;
862
0
  ext_rel_end = ext_rel + input_section->reloc_count;
863
0
  for (; ext_rel < ext_rel_end; ext_rel++)
864
0
    {
865
0
      struct internal_reloc int_rel;
866
0
      bool use_lo = false;
867
0
      bfd_vma addend;
868
0
      reloc_howto_type *howto;
869
0
      struct ecoff_link_hash_entry *h = NULL;
870
0
      asection *s = NULL;
871
0
      bfd_vma relocation;
872
0
      bfd_reloc_status_type r;
873
874
0
      if (! got_lo)
875
0
  mips_ecoff_swap_reloc_in (input_bfd, ext_rel, &int_rel);
876
0
      else
877
0
  {
878
0
    int_rel = lo_int_rel;
879
0
    got_lo = false;
880
0
  }
881
882
0
      BFD_ASSERT (int_rel.r_type
883
0
      < sizeof mips_howto_table / sizeof mips_howto_table[0]);
884
885
      /* The REFHI reloc requires special handling.  It must be followed
886
   by a REFLO reloc, and the addend is formed from both relocs.  */
887
0
      if (int_rel.r_type == MIPS_R_REFHI)
888
0
  {
889
0
    struct external_reloc *lo_ext_rel;
890
891
    /* As a GNU extension, permit an arbitrary number of REFHI
892
       relocs before the REFLO reloc.  This permits gcc to emit
893
       the HI and LO relocs itself.  */
894
0
    for (lo_ext_rel = ext_rel + 1;
895
0
         lo_ext_rel < ext_rel_end;
896
0
         lo_ext_rel++)
897
0
      {
898
0
        mips_ecoff_swap_reloc_in (input_bfd, lo_ext_rel,
899
0
          &lo_int_rel);
900
0
        if (lo_int_rel.r_type != int_rel.r_type)
901
0
    break;
902
0
      }
903
904
0
    if (lo_ext_rel < ext_rel_end
905
0
        && lo_int_rel.r_type == MIPS_R_REFLO
906
0
        && int_rel.r_extern == lo_int_rel.r_extern
907
0
        && int_rel.r_symndx == lo_int_rel.r_symndx)
908
0
      {
909
0
        use_lo = true;
910
0
        if (lo_ext_rel == ext_rel + 1)
911
0
    got_lo = true;
912
0
      }
913
0
  }
914
915
0
      howto = &mips_howto_table[int_rel.r_type];
916
917
0
      if (int_rel.r_extern)
918
0
  {
919
0
    h = sym_hashes[int_rel.r_symndx];
920
    /* If h is NULL, that means that there is a reloc against an
921
       external symbol which we thought was just a debugging
922
       symbol.  This should not happen.  */
923
0
    if (h == (struct ecoff_link_hash_entry *) NULL)
924
0
      abort ();
925
0
  }
926
0
      else
927
0
  {
928
0
    if (int_rel.r_symndx < 0 || int_rel.r_symndx >= NUM_RELOC_SECTIONS)
929
0
      s = NULL;
930
0
    else
931
0
      s = symndx_to_section[int_rel.r_symndx];
932
933
0
    if (s == (asection *) NULL)
934
0
      abort ();
935
0
  }
936
937
      /* The GPREL reloc uses an addend: the difference in the GP
938
   values.  */
939
0
      if (int_rel.r_type != MIPS_R_GPREL
940
0
    && int_rel.r_type != MIPS_R_LITERAL)
941
0
  addend = 0;
942
0
      else
943
0
  {
944
0
    if (gp_undefined)
945
0
      {
946
0
        (*info->callbacks->reloc_dangerous)
947
0
    (info, _("GP relative relocation used when GP not defined"),
948
0
     input_bfd, input_section,
949
0
     int_rel.r_vaddr - input_section->vma);
950
        /* Only give the error once per link.  */
951
0
        gp = 4;
952
0
        _bfd_set_gp_value (output_bfd, gp);
953
0
        gp_undefined = false;
954
0
      }
955
0
    if (! int_rel.r_extern)
956
0
      {
957
        /* This is a relocation against a section.  The current
958
     addend in the instruction is the difference between
959
     INPUT_SECTION->vma and the GP value of INPUT_BFD.  We
960
     must change this to be the difference between the
961
     final definition (which will end up in RELOCATION)
962
     and the GP value of OUTPUT_BFD (which is in GP).  */
963
0
        addend = ecoff_data (input_bfd)->gp - gp;
964
0
      }
965
0
    else if (! bfd_link_relocatable (info)
966
0
       || h->root.type == bfd_link_hash_defined
967
0
       || h->root.type == bfd_link_hash_defweak)
968
0
      {
969
        /* This is a relocation against a defined symbol.  The
970
     current addend in the instruction is simply the
971
     desired offset into the symbol (normally zero).  We
972
     are going to change this into a relocation against a
973
     defined symbol, so we want the instruction to hold
974
     the difference between the final definition of the
975
     symbol (which will end up in RELOCATION) and the GP
976
     value of OUTPUT_BFD (which is in GP).  */
977
0
        addend = - gp;
978
0
      }
979
0
    else
980
0
      {
981
        /* This is a relocation against an undefined or common
982
     symbol.  The current addend in the instruction is
983
     simply the desired offset into the symbol (normally
984
     zero).  We are generating relocatable output, and we
985
     aren't going to define this symbol, so we just leave
986
     the instruction alone.  */
987
0
        addend = 0;
988
0
      }
989
0
  }
990
991
0
      if (bfd_link_relocatable (info))
992
0
  {
993
    /* We are generating relocatable output, and must convert
994
       the existing reloc.  */
995
0
    if (int_rel.r_extern)
996
0
      {
997
0
        if ((h->root.type == bfd_link_hash_defined
998
0
       || h->root.type == bfd_link_hash_defweak)
999
0
      && ! bfd_is_abs_section (h->root.u.def.section))
1000
0
    {
1001
0
      const char *name;
1002
1003
      /* This symbol is defined in the output.  Convert
1004
         the reloc from being against the symbol to being
1005
         against the section.  */
1006
1007
      /* Clear the r_extern bit.  */
1008
0
      int_rel.r_extern = 0;
1009
1010
      /* Compute a new r_symndx value.  */
1011
0
      s = h->root.u.def.section;
1012
0
      name = bfd_section_name (s->output_section);
1013
1014
0
      int_rel.r_symndx = -1;
1015
0
      switch (name[1])
1016
0
        {
1017
0
        case 'b':
1018
0
          if (strcmp (name, ".bss") == 0)
1019
0
      int_rel.r_symndx = RELOC_SECTION_BSS;
1020
0
          break;
1021
0
        case 'd':
1022
0
          if (strcmp (name, ".data") == 0)
1023
0
      int_rel.r_symndx = RELOC_SECTION_DATA;
1024
0
          break;
1025
0
        case 'f':
1026
0
          if (strcmp (name, ".fini") == 0)
1027
0
      int_rel.r_symndx = RELOC_SECTION_FINI;
1028
0
          break;
1029
0
        case 'i':
1030
0
          if (strcmp (name, ".init") == 0)
1031
0
      int_rel.r_symndx = RELOC_SECTION_INIT;
1032
0
          break;
1033
0
        case 'l':
1034
0
          if (strcmp (name, ".lit8") == 0)
1035
0
      int_rel.r_symndx = RELOC_SECTION_LIT8;
1036
0
          else if (strcmp (name, ".lit4") == 0)
1037
0
      int_rel.r_symndx = RELOC_SECTION_LIT4;
1038
0
          break;
1039
0
        case 'r':
1040
0
          if (strcmp (name, ".rdata") == 0)
1041
0
      int_rel.r_symndx = RELOC_SECTION_RDATA;
1042
0
          break;
1043
0
        case 's':
1044
0
          if (strcmp (name, ".sdata") == 0)
1045
0
      int_rel.r_symndx = RELOC_SECTION_SDATA;
1046
0
          else if (strcmp (name, ".sbss") == 0)
1047
0
      int_rel.r_symndx = RELOC_SECTION_SBSS;
1048
0
          break;
1049
0
        case 't':
1050
0
          if (strcmp (name, ".text") == 0)
1051
0
      int_rel.r_symndx = RELOC_SECTION_TEXT;
1052
0
          break;
1053
0
        }
1054
1055
0
      if (int_rel.r_symndx == -1)
1056
0
        abort ();
1057
1058
      /* Add the section VMA and the symbol value.  */
1059
0
      relocation = (h->root.u.def.value
1060
0
        + s->output_section->vma
1061
0
        + s->output_offset);
1062
1063
      /* For a PC relative relocation, the object file
1064
         currently holds just the addend.  We must adjust
1065
         by the address to get the right value.  */
1066
0
      if (howto->pc_relative)
1067
0
        relocation -= int_rel.r_vaddr - input_section->vma;
1068
1069
0
      h = NULL;
1070
0
    }
1071
0
        else
1072
0
    {
1073
      /* Change the symndx value to the right one for the
1074
         output BFD.  */
1075
0
      int_rel.r_symndx = h->indx;
1076
0
      if (int_rel.r_symndx == -1)
1077
0
        {
1078
          /* This symbol is not being written out.  */
1079
0
          (*info->callbacks->unattached_reloc)
1080
0
      (info, h->root.root.string, input_bfd, input_section,
1081
0
       int_rel.r_vaddr - input_section->vma);
1082
0
          int_rel.r_symndx = 0;
1083
0
        }
1084
0
      relocation = 0;
1085
0
    }
1086
0
      }
1087
0
    else
1088
0
      {
1089
        /* This is a relocation against a section.  Adjust the
1090
     value by the amount the section moved.  */
1091
0
        relocation = (s->output_section->vma
1092
0
          + s->output_offset
1093
0
          - s->vma);
1094
0
      }
1095
1096
0
    relocation += addend;
1097
0
    addend = 0;
1098
1099
    /* Adjust a PC relative relocation by removing the reference
1100
       to the original address in the section and including the
1101
       reference to the new address.  */
1102
0
    if (howto->pc_relative)
1103
0
      relocation -= (input_section->output_section->vma
1104
0
         + input_section->output_offset
1105
0
         - input_section->vma);
1106
1107
    /* Adjust the contents.  */
1108
0
    if (relocation == 0)
1109
0
      r = bfd_reloc_ok;
1110
0
    else
1111
0
      {
1112
0
        if (int_rel.r_type != MIPS_R_REFHI)
1113
0
    r = _bfd_relocate_contents (howto, input_bfd, relocation,
1114
0
              (contents
1115
0
               + int_rel.r_vaddr
1116
0
               - input_section->vma));
1117
0
        else
1118
0
    {
1119
0
      mips_relocate_hi (&int_rel,
1120
0
            use_lo ? &lo_int_rel : NULL,
1121
0
            input_bfd, input_section, contents,
1122
0
            relocation);
1123
0
      r = bfd_reloc_ok;
1124
0
    }
1125
0
      }
1126
1127
    /* Adjust the reloc address.  */
1128
0
    int_rel.r_vaddr += (input_section->output_section->vma
1129
0
            + input_section->output_offset
1130
0
            - input_section->vma);
1131
1132
    /* Save the changed reloc information.  */
1133
0
    mips_ecoff_swap_reloc_out (input_bfd, &int_rel, ext_rel);
1134
0
  }
1135
0
      else
1136
0
  {
1137
    /* We are producing a final executable.  */
1138
0
    if (int_rel.r_extern)
1139
0
      {
1140
        /* This is a reloc against a symbol.  */
1141
0
        if (h->root.type == bfd_link_hash_defined
1142
0
      || h->root.type == bfd_link_hash_defweak)
1143
0
    {
1144
0
      asection *hsec;
1145
1146
0
      hsec = h->root.u.def.section;
1147
0
      relocation = (h->root.u.def.value
1148
0
        + hsec->output_section->vma
1149
0
        + hsec->output_offset);
1150
0
    }
1151
0
        else
1152
0
    {
1153
0
      (*info->callbacks->undefined_symbol)
1154
0
        (info, h->root.root.string, input_bfd, input_section,
1155
0
         int_rel.r_vaddr - input_section->vma, true);
1156
0
      relocation = 0;
1157
0
    }
1158
0
      }
1159
0
    else
1160
0
      {
1161
        /* This is a reloc against a section.  */
1162
0
        relocation = (s->output_section->vma
1163
0
          + s->output_offset
1164
0
          - s->vma);
1165
1166
        /* A PC relative reloc is already correct in the object
1167
     file.  Make it look like a pcrel_offset relocation by
1168
     adding in the start address.  */
1169
0
        if (howto->pc_relative)
1170
0
    relocation += int_rel.r_vaddr;
1171
0
      }
1172
1173
0
    if (int_rel.r_type != MIPS_R_REFHI)
1174
0
      r = _bfd_final_link_relocate (howto,
1175
0
            input_bfd,
1176
0
            input_section,
1177
0
            contents,
1178
0
            (int_rel.r_vaddr
1179
0
             - input_section->vma),
1180
0
            relocation,
1181
0
            addend);
1182
0
    else
1183
0
      {
1184
0
        mips_relocate_hi (&int_rel,
1185
0
        use_lo ? &lo_int_rel : NULL,
1186
0
        input_bfd, input_section, contents,
1187
0
        relocation);
1188
0
        r = bfd_reloc_ok;
1189
0
      }
1190
0
  }
1191
1192
      /* MIPS_R_JMPADDR requires peculiar overflow detection.  The
1193
   instruction provides a 28 bit address (the two lower bits are
1194
   implicit zeroes) which is combined with the upper four bits
1195
   of the instruction address.  */
1196
0
      if (r == bfd_reloc_ok
1197
0
    && int_rel.r_type == MIPS_R_JMPADDR
1198
0
    && (((relocation
1199
0
    + addend
1200
0
    + (int_rel.r_extern ? 0 : s->vma))
1201
0
         & 0xf0000000)
1202
0
        != ((input_section->output_section->vma
1203
0
       + input_section->output_offset
1204
0
       + (int_rel.r_vaddr - input_section->vma))
1205
0
      & 0xf0000000)))
1206
0
  r = bfd_reloc_overflow;
1207
1208
0
      if (r != bfd_reloc_ok)
1209
0
  {
1210
0
    switch (r)
1211
0
      {
1212
0
      default:
1213
0
      case bfd_reloc_outofrange:
1214
0
        abort ();
1215
0
      case bfd_reloc_overflow:
1216
0
        {
1217
0
    const char *name;
1218
1219
0
    if (int_rel.r_extern)
1220
0
      name = NULL;
1221
0
    else
1222
0
      name = bfd_section_name (s);
1223
0
    (*info->callbacks->reloc_overflow)
1224
0
      (info, (h ? &h->root : NULL), name, howto->name,
1225
0
       (bfd_vma) 0, input_bfd, input_section,
1226
0
       int_rel.r_vaddr - input_section->vma);
1227
0
        }
1228
0
        break;
1229
0
      }
1230
0
  }
1231
0
    }
1232
1233
0
  return true;
1234
0
}
1235
1236
static void
1237
mips_ecoff_swap_coff_aux_in (bfd *abfd ATTRIBUTE_UNUSED,
1238
           void *ext1 ATTRIBUTE_UNUSED,
1239
           int type ATTRIBUTE_UNUSED,
1240
           int in_class ATTRIBUTE_UNUSED,
1241
           int indx ATTRIBUTE_UNUSED,
1242
           int numaux ATTRIBUTE_UNUSED,
1243
           void *in1 ATTRIBUTE_UNUSED)
1244
0
{
1245
0
}
1246
1247
static void
1248
mips_ecoff_swap_coff_sym_in (bfd *abfd ATTRIBUTE_UNUSED,
1249
           void *ext1 ATTRIBUTE_UNUSED,
1250
           void *in1 ATTRIBUTE_UNUSED)
1251
0
{
1252
0
}
1253
1254
static void
1255
mips_ecoff_swap_coff_lineno_in (bfd *abfd ATTRIBUTE_UNUSED,
1256
        void *ext1 ATTRIBUTE_UNUSED,
1257
        void *in1 ATTRIBUTE_UNUSED)
1258
0
{
1259
0
}
1260
1261
static unsigned int
1262
mips_ecoff_swap_coff_aux_out (bfd *abfd ATTRIBUTE_UNUSED,
1263
            void *inp ATTRIBUTE_UNUSED,
1264
            int type ATTRIBUTE_UNUSED,
1265
            int in_class ATTRIBUTE_UNUSED,
1266
            int indx ATTRIBUTE_UNUSED,
1267
            int numaux ATTRIBUTE_UNUSED,
1268
            void *extp ATTRIBUTE_UNUSED)
1269
0
{
1270
0
  return 0;
1271
0
}
1272
1273
static unsigned int
1274
mips_ecoff_swap_coff_sym_out (bfd *abfd ATTRIBUTE_UNUSED,
1275
            void *inp ATTRIBUTE_UNUSED,
1276
            void *extp ATTRIBUTE_UNUSED)
1277
0
{
1278
0
  return 0;
1279
0
}
1280
1281
static unsigned int
1282
mips_ecoff_swap_coff_lineno_out (bfd *abfd ATTRIBUTE_UNUSED,
1283
         void *inp ATTRIBUTE_UNUSED,
1284
         void *extp ATTRIBUTE_UNUSED)
1285
0
{
1286
0
  return 0;
1287
0
}
1288
1289
static unsigned int
1290
mips_ecoff_swap_coff_reloc_out (bfd *abfd ATTRIBUTE_UNUSED,
1291
        void *inp ATTRIBUTE_UNUSED,
1292
        void *extp ATTRIBUTE_UNUSED)
1293
0
{
1294
0
  return 0;
1295
0
}
1296

1297
/* This is the ECOFF backend structure.  The backend field of the
1298
   target vector points to this.  */
1299
1300
static const struct ecoff_backend_data mips_ecoff_backend_data =
1301
{
1302
  /* COFF backend structure.  */
1303
  {
1304
    mips_ecoff_swap_coff_aux_in, mips_ecoff_swap_coff_sym_in,
1305
    mips_ecoff_swap_coff_lineno_in, mips_ecoff_swap_coff_aux_out,
1306
    mips_ecoff_swap_coff_sym_out, mips_ecoff_swap_coff_lineno_out,
1307
    mips_ecoff_swap_coff_reloc_out,
1308
    mips_ecoff_swap_filehdr_out, mips_ecoff_swap_aouthdr_out,
1309
    mips_ecoff_swap_scnhdr_out,
1310
    FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, true,
1311
    ECOFF_NO_LONG_SECTION_NAMES, 4, false, 2, 32768,
1312
    mips_ecoff_swap_filehdr_in, mips_ecoff_swap_aouthdr_in,
1313
    mips_ecoff_swap_scnhdr_in, NULL,
1314
    mips_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook,
1315
    _bfd_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags,
1316
    _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table,
1317
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1318
    NULL, NULL,
1319
  },
1320
  /* Supported architecture.  */
1321
  bfd_arch_mips,
1322
  /* Initial portion of armap string.  */
1323
  "__________",
1324
  /* The page boundary used to align sections in a demand-paged
1325
     executable file.  E.g., 0x1000.  */
1326
  0x1000,
1327
  /* TRUE if the .rdata section is part of the text segment, as on the
1328
     Alpha.  FALSE if .rdata is part of the data segment, as on the
1329
     MIPS.  */
1330
  false,
1331
  /* Bitsize of constructor entries.  */
1332
  32,
1333
  /* Reloc to use for constructor entries.  */
1334
  &mips_howto_table[MIPS_R_REFWORD],
1335
  {
1336
    /* Symbol table magic number.  */
1337
    magicSym,
1338
    /* Alignment of debugging information.  E.g., 4.  */
1339
    4,
1340
    /* Sizes of external symbolic information.  */
1341
    sizeof (struct hdr_ext),
1342
    sizeof (struct dnr_ext),
1343
    sizeof (struct pdr_ext),
1344
    sizeof (struct sym_ext),
1345
    sizeof (struct opt_ext),
1346
    sizeof (struct fdr_ext),
1347
    sizeof (struct rfd_ext),
1348
    sizeof (struct ext_ext),
1349
    /* Functions to swap in external symbolic data.  */
1350
    ecoff_swap_hdr_in,
1351
    ecoff_swap_dnr_in,
1352
    ecoff_swap_pdr_in,
1353
    ecoff_swap_sym_in,
1354
    ecoff_swap_opt_in,
1355
    ecoff_swap_fdr_in,
1356
    ecoff_swap_rfd_in,
1357
    ecoff_swap_ext_in,
1358
    _bfd_ecoff_swap_tir_in,
1359
    _bfd_ecoff_swap_rndx_in,
1360
    /* Functions to swap out external symbolic data.  */
1361
    ecoff_swap_hdr_out,
1362
    ecoff_swap_dnr_out,
1363
    ecoff_swap_pdr_out,
1364
    ecoff_swap_sym_out,
1365
    ecoff_swap_opt_out,
1366
    ecoff_swap_fdr_out,
1367
    ecoff_swap_rfd_out,
1368
    ecoff_swap_ext_out,
1369
    _bfd_ecoff_swap_tir_out,
1370
    _bfd_ecoff_swap_rndx_out,
1371
    /* Function to read in symbolic data.  */
1372
    _bfd_ecoff_slurp_symbolic_info
1373
  },
1374
  /* External reloc size.  */
1375
  RELSZ,
1376
  /* Reloc swapping functions.  */
1377
  mips_ecoff_swap_reloc_in,
1378
  mips_ecoff_swap_reloc_out,
1379
  /* Backend reloc tweaking.  */
1380
  mips_adjust_reloc_in,
1381
  mips_adjust_reloc_out,
1382
  /* Relocate section contents while linking.  */
1383
  mips_relocate_section,
1384
  /* Do final adjustments to filehdr and aouthdr.  */
1385
  NULL,
1386
  /* Read an element from an archive at a given file position.  */
1387
  _bfd_get_elt_at_filepos
1388
};
1389
1390
/* Looking up a reloc type is MIPS specific.  */
1391
#define _bfd_ecoff_bfd_reloc_type_lookup mips_bfd_reloc_type_lookup
1392
#define _bfd_ecoff_bfd_reloc_name_lookup mips_bfd_reloc_name_lookup
1393
1394
/* Getting relocated section contents is generic.  */
1395
#define _bfd_ecoff_bfd_get_relocated_section_contents \
1396
  bfd_generic_get_relocated_section_contents
1397
1398
/* Relaxing sections is MIPS specific.  */
1399
#define _bfd_ecoff_bfd_relax_section bfd_generic_relax_section
1400
1401
/* GC of sections is not done.  */
1402
#define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections
1403
1404
/* Input section flags is not implemented.  */
1405
#define _bfd_ecoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
1406
1407
#define _bfd_ecoff_bfd_is_group_section bfd_generic_is_group_section
1408
#define _bfd_ecoff_bfd_group_name bfd_generic_group_name
1409
#define _bfd_ecoff_bfd_discard_group bfd_generic_discard_group
1410
#define _bfd_ecoff_section_already_linked \
1411
  _bfd_coff_section_already_linked
1412
#define _bfd_ecoff_bfd_define_common_symbol bfd_generic_define_common_symbol
1413
#define _bfd_ecoff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
1414
#define _bfd_ecoff_bfd_define_start_stop bfd_generic_define_start_stop
1415
#define _bfd_ecoff_finalize_section_relocs _bfd_generic_finalize_section_relocs
1416
1417
extern const bfd_target mips_ecoff_be_vec;
1418
1419
const bfd_target mips_ecoff_le_vec =
1420
{
1421
  "ecoff-littlemips",   /* name */
1422
  bfd_target_ecoff_flavour,
1423
  BFD_ENDIAN_LITTLE,    /* data byte order is little */
1424
  BFD_ENDIAN_LITTLE,    /* header byte order is little */
1425
1426
  (HAS_RELOC | EXEC_P   /* object flags */
1427
   | HAS_LINENO | HAS_DEBUG
1428
   | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1429
1430
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE
1431
   | SEC_DATA | SEC_SMALL_DATA),
1432
  0,        /* leading underscore */
1433
  ' ',        /* ar_pad_char */
1434
  15,       /* ar_max_namelen */
1435
  0,        /* match priority.  */
1436
  TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
1437
  TARGET_MERGE_SECTIONS,
1438
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1439
     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1440
     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1441
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1442
     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1443
     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
1444
1445
  {       /* bfd_check_format */
1446
    _bfd_dummy_target,
1447
    coff_object_p,
1448
    bfd_generic_archive_p,
1449
    _bfd_dummy_target
1450
  },
1451
  {       /* bfd_set_format */
1452
    _bfd_bool_bfd_false_error,
1453
    _bfd_ecoff_mkobject,
1454
    _bfd_generic_mkarchive,
1455
    _bfd_bool_bfd_false_error
1456
  },
1457
  {       /* bfd_write_contents */
1458
    _bfd_bool_bfd_false_error,
1459
    _bfd_ecoff_write_object_contents,
1460
    _bfd_write_archive_contents,
1461
    _bfd_bool_bfd_false_error
1462
  },
1463
1464
  BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
1465
  BFD_JUMP_TABLE_COPY (_bfd_ecoff),
1466
  BFD_JUMP_TABLE_CORE (_bfd_nocore),
1467
  BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
1468
  BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
1469
  BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
1470
  BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
1471
  BFD_JUMP_TABLE_LINK (_bfd_ecoff),
1472
  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1473
1474
  &mips_ecoff_be_vec,
1475
1476
  &mips_ecoff_backend_data
1477
};
1478
1479
const bfd_target mips_ecoff_be_vec =
1480
{
1481
  "ecoff-bigmips",    /* name */
1482
  bfd_target_ecoff_flavour,
1483
  BFD_ENDIAN_BIG,   /* data byte order is big */
1484
  BFD_ENDIAN_BIG,   /* header byte order is big */
1485
1486
  (HAS_RELOC | EXEC_P   /* object flags */
1487
   | HAS_LINENO | HAS_DEBUG
1488
   | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1489
1490
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE
1491
   | SEC_DATA | SEC_SMALL_DATA),
1492
  0,        /* leading underscore */
1493
  ' ',        /* ar_pad_char */
1494
  15,       /* ar_max_namelen */
1495
  0,        /* match priority.  */
1496
  TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
1497
  TARGET_MERGE_SECTIONS,
1498
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1499
     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1500
     bfd_getb16, bfd_getb_signed_16, bfd_putb16,
1501
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1502
     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1503
     bfd_getb16, bfd_getb_signed_16, bfd_putb16,
1504
1505
  {       /* bfd_check_format */
1506
    _bfd_dummy_target,
1507
    coff_object_p,
1508
    bfd_generic_archive_p,
1509
    _bfd_dummy_target
1510
  },
1511
  {       /* bfd_set_format */
1512
    _bfd_bool_bfd_false_error,
1513
    _bfd_ecoff_mkobject,
1514
    _bfd_generic_mkarchive,
1515
    _bfd_bool_bfd_false_error
1516
  },
1517
  {       /* bfd_write_contents */
1518
    _bfd_bool_bfd_false_error,
1519
    _bfd_ecoff_write_object_contents,
1520
    _bfd_write_archive_contents,
1521
    _bfd_bool_bfd_false_error
1522
  },
1523
1524
  BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
1525
  BFD_JUMP_TABLE_COPY (_bfd_ecoff),
1526
  BFD_JUMP_TABLE_CORE (_bfd_nocore),
1527
  BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
1528
  BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
1529
  BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
1530
  BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
1531
  BFD_JUMP_TABLE_LINK (_bfd_ecoff),
1532
  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1533
1534
  &mips_ecoff_le_vec,
1535
1536
  &mips_ecoff_backend_data
1537
};
1538
1539
const bfd_target mips_ecoff_bele_vec =
1540
{
1541
  "ecoff-biglittlemips",    /* name */
1542
  bfd_target_ecoff_flavour,
1543
  BFD_ENDIAN_LITTLE,    /* data byte order is little */
1544
  BFD_ENDIAN_BIG,   /* header byte order is big */
1545
1546
  (HAS_RELOC | EXEC_P   /* object flags */
1547
   | HAS_LINENO | HAS_DEBUG
1548
   | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1549
1550
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE
1551
   | SEC_DATA | SEC_SMALL_DATA),
1552
  0,        /* leading underscore */
1553
  ' ',        /* ar_pad_char */
1554
  15,       /* ar_max_namelen */
1555
  0,        /* match priority.  */
1556
  TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
1557
  TARGET_MERGE_SECTIONS,
1558
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1559
     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1560
     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1561
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1562
     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1563
     bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1564
1565
  {       /* bfd_check_format */
1566
    _bfd_dummy_target,
1567
    coff_object_p,
1568
    bfd_generic_archive_p,
1569
    _bfd_dummy_target
1570
  },
1571
  {       /* bfd_set_format */
1572
    _bfd_bool_bfd_false_error,
1573
    _bfd_ecoff_mkobject,
1574
    _bfd_generic_mkarchive,
1575
    _bfd_bool_bfd_false_error
1576
  },
1577
  {       /* bfd_write_contents */
1578
    _bfd_bool_bfd_false_error,
1579
    _bfd_ecoff_write_object_contents,
1580
    _bfd_write_archive_contents,
1581
    _bfd_bool_bfd_false_error
1582
  },
1583
1584
  BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
1585
  BFD_JUMP_TABLE_COPY (_bfd_ecoff),
1586
  BFD_JUMP_TABLE_CORE (_bfd_nocore),
1587
  BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
1588
  BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
1589
  BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
1590
  BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
1591
  BFD_JUMP_TABLE_LINK (_bfd_ecoff),
1592
  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1593
1594
  NULL,
1595
1596
  &mips_ecoff_backend_data
1597
};