Coverage Report

Created: 2026-09-14 08:07

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