OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [oc/] [gdb-5.0/] [bfd/] [coff-mips.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 104 markom
/* BFD back-end for MIPS Extended-Coff files.
2
   Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3
   Free Software Foundation, Inc.
4
   Original version by Per Bothner.
5
   Full support added by Ian Lance Taylor, ian@cygnus.com.
6
 
7
This file is part of BFD, the Binary File Descriptor library.
8
 
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13
 
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
GNU General Public License for more details.
18
 
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
 
23
#include "bfd.h"
24
#include "sysdep.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
/* Prototypes for static functions.  */
36
 
37
static boolean mips_ecoff_bad_format_hook PARAMS ((bfd *abfd, PTR filehdr));
38
static void mips_ecoff_swap_reloc_in PARAMS ((bfd *, PTR,
39
                                              struct internal_reloc *));
40
static void mips_ecoff_swap_reloc_out PARAMS ((bfd *,
41
                                               const struct internal_reloc *,
42
                                               PTR));
43
static void mips_adjust_reloc_in PARAMS ((bfd *,
44
                                          const struct internal_reloc *,
45
                                          arelent *));
46
static void mips_adjust_reloc_out PARAMS ((bfd *, const arelent *,
47
                                           struct internal_reloc *));
48
static bfd_reloc_status_type mips_generic_reloc PARAMS ((bfd *abfd,
49
                                                         arelent *reloc,
50
                                                         asymbol *symbol,
51
                                                         PTR data,
52
                                                         asection *section,
53
                                                         bfd *output_bfd,
54
                                                         char **error));
55
static bfd_reloc_status_type mips_refhi_reloc PARAMS ((bfd *abfd,
56
                                                       arelent *reloc,
57
                                                       asymbol *symbol,
58
                                                       PTR data,
59
                                                       asection *section,
60
                                                       bfd *output_bfd,
61
                                                       char **error));
62
static bfd_reloc_status_type mips_reflo_reloc PARAMS ((bfd *abfd,
63
                                                       arelent *reloc,
64
                                                       asymbol *symbol,
65
                                                       PTR data,
66
                                                       asection *section,
67
                                                       bfd *output_bfd,
68
                                                       char **error));
69
static bfd_reloc_status_type mips_gprel_reloc PARAMS ((bfd *abfd,
70
                                                       arelent *reloc,
71
                                                       asymbol *symbol,
72
                                                       PTR data,
73
                                                       asection *section,
74
                                                       bfd *output_bfd,
75
                                                       char **error));
76
static bfd_reloc_status_type mips_relhi_reloc PARAMS ((bfd *abfd,
77
                                                       arelent *reloc,
78
                                                       asymbol *symbol,
79
                                                       PTR data,
80
                                                       asection *section,
81
                                                       bfd *output_bfd,
82
                                                       char **error));
83
static bfd_reloc_status_type mips_rello_reloc PARAMS ((bfd *abfd,
84
                                                       arelent *reloc,
85
                                                       asymbol *symbol,
86
                                                       PTR data,
87
                                                       asection *section,
88
                                                       bfd *output_bfd,
89
                                                       char **error));
90
static bfd_reloc_status_type mips_switch_reloc PARAMS ((bfd *abfd,
91
                                                        arelent *reloc,
92
                                                        asymbol *symbol,
93
                                                        PTR data,
94
                                                        asection *section,
95
                                                        bfd *output_bfd,
96
                                                        char **error));
97
static void mips_relocate_hi PARAMS ((struct internal_reloc *refhi,
98
                                      struct internal_reloc *reflo,
99
                                      bfd *input_bfd,
100
                                      asection *input_section,
101
                                      bfd_byte *contents,
102
                                      size_t adjust,
103
                                      bfd_vma relocation,
104
                                      boolean pcrel));
105
static boolean mips_relocate_section PARAMS ((bfd *, struct bfd_link_info *,
106
                                              bfd *, asection *,
107
                                              bfd_byte *, PTR));
108
static boolean mips_read_relocs PARAMS ((bfd *, asection *));
109
static boolean mips_relax_section PARAMS ((bfd *, asection *,
110
                                           struct bfd_link_info *,
111
                                           boolean *));
112
static boolean mips_relax_pcrel16 PARAMS ((struct bfd_link_info *, bfd *,
113
                                           asection *,
114
                                           struct ecoff_link_hash_entry *,
115
                                           bfd_byte *, bfd_vma));
116
static reloc_howto_type *mips_bfd_reloc_type_lookup
117
  PARAMS ((bfd *, bfd_reloc_code_real_type));
118
 
119
 
120
/* ECOFF has COFF sections, but the debugging information is stored in
121
   a completely different format.  ECOFF targets use some of the
122
   swapping routines from coffswap.h, and some of the generic COFF
123
   routines in coffgen.c, but, unlike the real COFF targets, do not
124
   use coffcode.h itself.
125
 
126
   Get the generic COFF swapping routines, except for the reloc,
127
   symbol, and lineno ones.  Give them ECOFF names.  */
128
#define MIPSECOFF
129
#define NO_COFF_RELOCS
130
#define NO_COFF_SYMBOLS
131
#define NO_COFF_LINENOS
132
#define coff_swap_filehdr_in mips_ecoff_swap_filehdr_in
133
#define coff_swap_filehdr_out mips_ecoff_swap_filehdr_out
134
#define coff_swap_aouthdr_in mips_ecoff_swap_aouthdr_in
135
#define coff_swap_aouthdr_out mips_ecoff_swap_aouthdr_out
136
#define coff_swap_scnhdr_in mips_ecoff_swap_scnhdr_in
137
#define coff_swap_scnhdr_out mips_ecoff_swap_scnhdr_out
138
#include "coffswap.h"
139
 
140
/* Get the ECOFF swapping routines.  */
141
#define ECOFF_32
142
#include "ecoffswap.h"
143
 
144
/* How to process the various relocs types.  */
145
 
146
static reloc_howto_type mips_howto_table[] =
147
{
148
  /* Reloc type 0 is ignored.  The reloc reading code ensures that
149
     this is a reference to the .abs section, which will cause
150
     bfd_perform_relocation to do nothing.  */
151
  HOWTO (MIPS_R_IGNORE, /* type */
152
         0,                      /* rightshift */
153
         0,                      /* size (0 = byte, 1 = short, 2 = long) */
154
         8,                     /* bitsize */
155
         false,                 /* pc_relative */
156
         0,                      /* bitpos */
157
         complain_overflow_dont, /* complain_on_overflow */
158
         0,                      /* special_function */
159
         "IGNORE",              /* name */
160
         false,                 /* partial_inplace */
161
         0,                      /* src_mask */
162
         0,                      /* dst_mask */
163
         false),                /* pcrel_offset */
164
 
165
  /* A 16 bit reference to a symbol, normally from a data section.  */
166
  HOWTO (MIPS_R_REFHALF,        /* type */
167
         0,                      /* rightshift */
168
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
169
         16,                    /* bitsize */
170
         false,                 /* pc_relative */
171
         0,                      /* bitpos */
172
         complain_overflow_bitfield, /* complain_on_overflow */
173
         mips_generic_reloc,    /* special_function */
174
         "REFHALF",             /* name */
175
         true,                  /* partial_inplace */
176
         0xffff,                /* src_mask */
177
         0xffff,                /* dst_mask */
178
         false),                /* pcrel_offset */
179
 
180
  /* A 32 bit reference to a symbol, normally from a data section.  */
181
  HOWTO (MIPS_R_REFWORD,        /* type */
182
         0,                      /* rightshift */
183
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
184
         32,                    /* bitsize */
185
         false,                 /* pc_relative */
186
         0,                      /* bitpos */
187
         complain_overflow_bitfield, /* complain_on_overflow */
188
         mips_generic_reloc,    /* special_function */
189
         "REFWORD",             /* name */
190
         true,                  /* partial_inplace */
191
         0xffffffff,            /* src_mask */
192
         0xffffffff,            /* dst_mask */
193
         false),                /* pcrel_offset */
194
 
195
  /* A 26 bit absolute jump address.  */
196
  HOWTO (MIPS_R_JMPADDR,        /* type */
197
         2,                     /* rightshift */
198
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
199
         26,                    /* bitsize */
200
         false,                 /* pc_relative */
201
         0,                      /* bitpos */
202
         complain_overflow_dont, /* complain_on_overflow */
203
                                /* This needs complex overflow
204
                                   detection, because the upper four
205
                                   bits must match the PC.  */
206
         mips_generic_reloc,    /* special_function */
207
         "JMPADDR",             /* name */
208
         true,                  /* partial_inplace */
209
         0x3ffffff,             /* src_mask */
210
         0x3ffffff,             /* dst_mask */
211
         false),                /* pcrel_offset */
212
 
213
  /* The high 16 bits of a symbol value.  Handled by the function
214
     mips_refhi_reloc.  */
215
  HOWTO (MIPS_R_REFHI,          /* type */
216
         16,                    /* rightshift */
217
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
218
         16,                    /* bitsize */
219
         false,                 /* pc_relative */
220
         0,                      /* bitpos */
221
         complain_overflow_bitfield, /* complain_on_overflow */
222
         mips_refhi_reloc,      /* special_function */
223
         "REFHI",               /* name */
224
         true,                  /* partial_inplace */
225
         0xffff,                /* src_mask */
226
         0xffff,                /* dst_mask */
227
         false),                /* pcrel_offset */
228
 
229
  /* The low 16 bits of a symbol value.  */
230
  HOWTO (MIPS_R_REFLO,          /* type */
231
         0,                      /* rightshift */
232
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
233
         16,                    /* bitsize */
234
         false,                 /* pc_relative */
235
         0,                      /* bitpos */
236
         complain_overflow_dont, /* complain_on_overflow */
237
         mips_reflo_reloc,      /* special_function */
238
         "REFLO",               /* name */
239
         true,                  /* partial_inplace */
240
         0xffff,                /* src_mask */
241
         0xffff,                /* dst_mask */
242
         false),                /* pcrel_offset */
243
 
244
  /* A reference to an offset from the gp register.  Handled by the
245
     function mips_gprel_reloc.  */
246
  HOWTO (MIPS_R_GPREL,          /* type */
247
         0,                      /* rightshift */
248
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
249
         16,                    /* bitsize */
250
         false,                 /* pc_relative */
251
         0,                      /* bitpos */
252
         complain_overflow_signed, /* complain_on_overflow */
253
         mips_gprel_reloc,      /* special_function */
254
         "GPREL",               /* name */
255
         true,                  /* partial_inplace */
256
         0xffff,                /* src_mask */
257
         0xffff,                /* dst_mask */
258
         false),                /* pcrel_offset */
259
 
260
  /* A reference to a literal using an offset from the gp register.
261
     Handled by the function mips_gprel_reloc.  */
262
  HOWTO (MIPS_R_LITERAL,        /* type */
263
         0,                      /* rightshift */
264
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
265
         16,                    /* bitsize */
266
         false,                 /* pc_relative */
267
         0,                      /* bitpos */
268
         complain_overflow_signed, /* complain_on_overflow */
269
         mips_gprel_reloc,      /* special_function */
270
         "LITERAL",             /* name */
271
         true,                  /* partial_inplace */
272
         0xffff,                /* src_mask */
273
         0xffff,                /* dst_mask */
274
         false),                /* pcrel_offset */
275
 
276
  EMPTY_HOWTO (8),
277
  EMPTY_HOWTO (9),
278
  EMPTY_HOWTO (10),
279
  EMPTY_HOWTO (11),
280
 
281
  /* This reloc is a Cygnus extension used when generating position
282
     independent code for embedded systems.  It represents a 16 bit PC
283
     relative reloc rightshifted twice as used in the MIPS branch
284
     instructions.  */
285
  HOWTO (MIPS_R_PCREL16,        /* type */
286
         2,                     /* rightshift */
287
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
288
         16,                    /* bitsize */
289
         true,                  /* pc_relative */
290
         0,                      /* bitpos */
291
         complain_overflow_signed, /* complain_on_overflow */
292
         mips_generic_reloc,    /* special_function */
293
         "PCREL16",             /* name */
294
         true,                  /* partial_inplace */
295
         0xffff,                /* src_mask */
296
         0xffff,                /* dst_mask */
297
         true),                 /* pcrel_offset */
298
 
299
  /* This reloc is a Cygnus extension used when generating position
300
     independent code for embedded systems.  It represents the high 16
301
     bits of a PC relative reloc.  The next reloc must be
302
     MIPS_R_RELLO, and the addend is formed from the addends of the
303
     two instructions, just as in MIPS_R_REFHI and MIPS_R_REFLO.  The
304
     final value is actually PC relative to the location of the
305
     MIPS_R_RELLO reloc, not the MIPS_R_RELHI reloc.  */
306
  HOWTO (MIPS_R_RELHI,          /* type */
307
         16,                    /* rightshift */
308
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
309
         16,                    /* bitsize */
310
         true,                  /* pc_relative */
311
         0,                      /* bitpos */
312
         complain_overflow_bitfield, /* complain_on_overflow */
313
         mips_relhi_reloc,      /* special_function */
314
         "RELHI",               /* name */
315
         true,                  /* partial_inplace */
316
         0xffff,                /* src_mask */
317
         0xffff,                /* dst_mask */
318
         true),                 /* pcrel_offset */
319
 
320
  /* This reloc is a Cygnus extension used when generating position
321
     independent code for embedded systems.  It represents the low 16
322
     bits of a PC relative reloc.  */
323
  HOWTO (MIPS_R_RELLO,          /* type */
324
         0,                      /* rightshift */
325
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
326
         16,                    /* bitsize */
327
         true,                  /* pc_relative */
328
         0,                      /* bitpos */
329
         complain_overflow_dont, /* complain_on_overflow */
330
         mips_rello_reloc,      /* special_function */
331
         "RELLO",               /* name */
332
         true,                  /* partial_inplace */
333
         0xffff,                /* src_mask */
334
         0xffff,                /* dst_mask */
335
         true),                 /* pcrel_offset */
336
 
337
  EMPTY_HOWTO (15),
338
  EMPTY_HOWTO (16),
339
  EMPTY_HOWTO (17),
340
  EMPTY_HOWTO (18),
341
  EMPTY_HOWTO (19),
342
  EMPTY_HOWTO (20),
343
  EMPTY_HOWTO (21),
344
 
345
  /* This reloc is a Cygnus extension used when generating position
346
     independent code for embedded systems.  It represents an entry in
347
     a switch table, which is the difference between two symbols in
348
     the .text section.  The symndx is actually the offset from the
349
     reloc address to the subtrahend.  See include/coff/mips.h for
350
     more details.  */
351
  HOWTO (MIPS_R_SWITCH,         /* type */
352
         0,                      /* rightshift */
353
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
354
         32,                    /* bitsize */
355
         true,                  /* pc_relative */
356
         0,                      /* bitpos */
357
         complain_overflow_dont, /* complain_on_overflow */
358
         mips_switch_reloc,     /* special_function */
359
         "SWITCH",              /* name */
360
         true,                  /* partial_inplace */
361
         0xffffffff,            /* src_mask */
362
         0xffffffff,            /* dst_mask */
363
         true)                  /* pcrel_offset */
364
};
365
 
366
#define MIPS_HOWTO_COUNT \
367
  (sizeof mips_howto_table / sizeof mips_howto_table[0])
368
 
369
/* When the linker is doing relaxing, it may change a external PCREL16
370
   reloc.  This typically represents an instruction like
371
       bal foo
372
   We change it to
373
       .set  noreorder
374
       bal   $L1
375
       lui   $at,%hi(foo - $L1)
376
     $L1:
377
       addiu $at,%lo(foo - $L1)
378
       addu  $at,$at,$31
379
       jalr  $at
380
   PCREL16_EXPANSION_ADJUSTMENT is the number of bytes this changes the
381
   instruction by.  */
382
 
383
#define PCREL16_EXPANSION_ADJUSTMENT (4 * 4)
384
 
385
/* See whether the magic number matches.  */
386
 
387
static boolean
388
mips_ecoff_bad_format_hook (abfd, filehdr)
389
     bfd *abfd;
390
     PTR filehdr;
391
{
392
  struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
393
 
394
  switch (internal_f->f_magic)
395
    {
396
    case MIPS_MAGIC_1:
397
      /* I don't know what endianness this implies.  */
398
      return true;
399
 
400
    case MIPS_MAGIC_BIG:
401
    case MIPS_MAGIC_BIG2:
402
    case MIPS_MAGIC_BIG3:
403
      return bfd_big_endian (abfd);
404
 
405
    case MIPS_MAGIC_LITTLE:
406
    case MIPS_MAGIC_LITTLE2:
407
    case MIPS_MAGIC_LITTLE3:
408
      return bfd_little_endian (abfd);
409
 
410
    default:
411
      return false;
412
    }
413
}
414
 
415
/* Reloc handling.  MIPS ECOFF relocs are packed into 8 bytes in
416
   external form.  They use a bit which indicates whether the symbol
417
   is external.  */
418
 
419
/* Swap a reloc in.  */
420
 
421
static void
422
mips_ecoff_swap_reloc_in (abfd, ext_ptr, intern)
423
     bfd *abfd;
424
     PTR ext_ptr;
425
     struct internal_reloc *intern;
426
{
427
  const RELOC *ext = (RELOC *) ext_ptr;
428
 
429
  intern->r_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_vaddr);
430
  if (bfd_header_big_endian (abfd))
431
    {
432
      intern->r_symndx = (((int) ext->r_bits[0]
433
                           << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
434
                          | ((int) ext->r_bits[1]
435
                             << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
436
                          | ((int) ext->r_bits[2]
437
                             << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
438
      intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
439
                        >> RELOC_BITS3_TYPE_SH_BIG);
440
      intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
441
    }
442
  else
443
    {
444
      intern->r_symndx = (((int) ext->r_bits[0]
445
                           << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
446
                          | ((int) ext->r_bits[1]
447
                             << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
448
                          | ((int) ext->r_bits[2]
449
                             << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
450
      intern->r_type = (((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
451
                         >> RELOC_BITS3_TYPE_SH_LITTLE)
452
                        | ((ext->r_bits[3] & RELOC_BITS3_TYPEHI_LITTLE)
453
                           << RELOC_BITS3_TYPEHI_SH_LITTLE));
454
      intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
455
    }
456
 
457
  /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or
458
     MIPS_R_RELLO reloc, r_symndx is actually the offset from the
459
     reloc address to the base of the difference (see
460
     include/coff/mips.h for more details).  We copy symndx into the
461
     r_offset field so as not to confuse ecoff_slurp_reloc_table in
462
     ecoff.c.  In adjust_reloc_in we then copy r_offset into the reloc
463
     addend.  */
464
  if (intern->r_type == MIPS_R_SWITCH
465
      || (! intern->r_extern
466
          && (intern->r_type == MIPS_R_RELLO
467
              || intern->r_type == MIPS_R_RELHI)))
468
    {
469
      BFD_ASSERT (! intern->r_extern);
470
      intern->r_offset = intern->r_symndx;
471
      if (intern->r_offset & 0x800000)
472
        intern->r_offset -= 0x1000000;
473
      intern->r_symndx = RELOC_SECTION_TEXT;
474
    }
475
}
476
 
477
/* Swap a reloc out.  */
478
 
479
static void
480
mips_ecoff_swap_reloc_out (abfd, intern, dst)
481
     bfd *abfd;
482
     const struct internal_reloc *intern;
483
     PTR dst;
484
{
485
  RELOC *ext = (RELOC *) dst;
486
  long r_symndx;
487
 
488
  BFD_ASSERT (intern->r_extern
489
              || (intern->r_symndx >= 0 && intern->r_symndx <= 12));
490
 
491
  /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELLO or
492
     MIPS_R_RELHI reloc, we actually want to write the contents of
493
     r_offset out as the symbol index.  This undoes the change made by
494
     mips_ecoff_swap_reloc_in.  */
495
  if (intern->r_type != MIPS_R_SWITCH
496
      && (intern->r_extern
497
          || (intern->r_type != MIPS_R_RELHI
498
              && intern->r_type != MIPS_R_RELLO)))
499
    r_symndx = intern->r_symndx;
500
  else
501
    {
502
      BFD_ASSERT (intern->r_symndx == RELOC_SECTION_TEXT);
503
      r_symndx = intern->r_offset & 0xffffff;
504
    }
505
 
506
  bfd_h_put_32 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
507
  if (bfd_header_big_endian (abfd))
508
    {
509
      ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
510
      ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
511
      ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
512
      ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
513
                         & RELOC_BITS3_TYPE_BIG)
514
                        | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
515
    }
516
  else
517
    {
518
      ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
519
      ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
520
      ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
521
      ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
522
                         & RELOC_BITS3_TYPE_LITTLE)
523
                        | ((intern->r_type >> RELOC_BITS3_TYPEHI_SH_LITTLE
524
                            & RELOC_BITS3_TYPEHI_LITTLE))
525
                        | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
526
    }
527
}
528
 
529
/* Finish canonicalizing a reloc.  Part of this is generic to all
530
   ECOFF targets, and that part is in ecoff.c.  The rest is done in
531
   this backend routine.  It must fill in the howto field.  */
532
 
533
static void
534
mips_adjust_reloc_in (abfd, intern, rptr)
535
     bfd *abfd;
536
     const struct internal_reloc *intern;
537
     arelent *rptr;
538
{
539
  if (intern->r_type > MIPS_R_SWITCH)
540
    abort ();
541
 
542
  if (! intern->r_extern
543
      && (intern->r_type == MIPS_R_GPREL
544
          || intern->r_type == MIPS_R_LITERAL))
545
    rptr->addend += ecoff_data (abfd)->gp;
546
 
547
  /* If the type is MIPS_R_IGNORE, make sure this is a reference to
548
     the absolute section so that the reloc is ignored.  */
549
  if (intern->r_type == MIPS_R_IGNORE)
550
    rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
551
 
552
  /* If this is a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or
553
     MIPS_R_RELLO reloc, we want the addend field of the BFD relocto
554
     hold the value which was originally in the symndx field of the
555
     internal MIPS ECOFF reloc.  This value was copied into
556
     intern->r_offset by mips_swap_reloc_in, and here we copy it into
557
     the addend field.  */
558
  if (intern->r_type == MIPS_R_SWITCH
559
      || (! intern->r_extern
560
          && (intern->r_type == MIPS_R_RELHI
561
              || intern->r_type == MIPS_R_RELLO)))
562
    rptr->addend = intern->r_offset;
563
 
564
  rptr->howto = &mips_howto_table[intern->r_type];
565
}
566
 
567
/* Make any adjustments needed to a reloc before writing it out.  None
568
   are needed for MIPS.  */
569
 
570
static void
571
mips_adjust_reloc_out (abfd, rel, intern)
572
     bfd *abfd ATTRIBUTE_UNUSED;
573
     const arelent *rel;
574
     struct internal_reloc *intern;
575
{
576
  /* For a MIPS_R_SWITCH reloc, or an internal MIPS_R_RELHI or
577
     MIPS_R_RELLO reloc, we must copy rel->addend into
578
     intern->r_offset.  This will then be written out as the symbol
579
     index by mips_ecoff_swap_reloc_out.  This operation parallels the
580
     action of mips_adjust_reloc_in.  */
581
  if (intern->r_type == MIPS_R_SWITCH
582
      || (! intern->r_extern
583
          && (intern->r_type == MIPS_R_RELHI
584
              || intern->r_type == MIPS_R_RELLO)))
585
    intern->r_offset = rel->addend;
586
}
587
 
588
/* ECOFF relocs are either against external symbols, or against
589
   sections.  If we are producing relocateable output, and the reloc
590
   is against an external symbol, and nothing has given us any
591
   additional addend, the resulting reloc will also be against the
592
   same symbol.  In such a case, we don't want to change anything
593
   about the way the reloc is handled, since it will all be done at
594
   final link time.  Rather than put special case code into
595
   bfd_perform_relocation, all the reloc types use this howto
596
   function.  It just short circuits the reloc if producing
597
   relocateable output against an external symbol.  */
598
 
599
static bfd_reloc_status_type
600
mips_generic_reloc (abfd,
601
                    reloc_entry,
602
                    symbol,
603
                    data,
604
                    input_section,
605
                    output_bfd,
606
                    error_message)
607
     bfd *abfd ATTRIBUTE_UNUSED;
608
     arelent *reloc_entry;
609
     asymbol *symbol;
610
     PTR data ATTRIBUTE_UNUSED;
611
     asection *input_section;
612
     bfd *output_bfd;
613
     char **error_message ATTRIBUTE_UNUSED;
614
{
615
  if (output_bfd != (bfd *) NULL
616
      && (symbol->flags & BSF_SECTION_SYM) == 0
617
      && reloc_entry->addend == 0)
618
    {
619
      reloc_entry->address += input_section->output_offset;
620
      return bfd_reloc_ok;
621
    }
622
 
623
  return bfd_reloc_continue;
624
}
625
 
626
/* Do a REFHI relocation.  This has to be done in combination with a
627
   REFLO reloc, because there is a carry from the REFLO to the REFHI.
628
   Here we just save the information we need; we do the actual
629
   relocation when we see the REFLO.  MIPS ECOFF requires that the
630
   REFLO immediately follow the REFHI.  As a GNU extension, we permit
631
   an arbitrary number of HI relocs to be associated with a single LO
632
   reloc.  This extension permits gcc to output the HI and LO relocs
633
   itself.  */
634
 
635
struct mips_hi
636
{
637
  struct mips_hi *next;
638
  bfd_byte *addr;
639
  bfd_vma addend;
640
};
641
 
642
/* FIXME: This should not be a static variable.  */
643
 
644
static struct mips_hi *mips_refhi_list;
645
 
646
static bfd_reloc_status_type
647
mips_refhi_reloc (abfd,
648
                  reloc_entry,
649
                  symbol,
650
                  data,
651
                  input_section,
652
                  output_bfd,
653
                  error_message)
654
     bfd *abfd ATTRIBUTE_UNUSED;
655
     arelent *reloc_entry;
656
     asymbol *symbol;
657
     PTR data;
658
     asection *input_section;
659
     bfd *output_bfd;
660
     char **error_message ATTRIBUTE_UNUSED;
661
{
662
  bfd_reloc_status_type ret;
663
  bfd_vma relocation;
664
  struct mips_hi *n;
665
 
666
  /* If we're relocating, and this an external symbol, we don't want
667
     to change anything.  */
668
  if (output_bfd != (bfd *) NULL
669
      && (symbol->flags & BSF_SECTION_SYM) == 0
670
      && reloc_entry->addend == 0)
671
    {
672
      reloc_entry->address += input_section->output_offset;
673
      return bfd_reloc_ok;
674
    }
675
 
676
  ret = bfd_reloc_ok;
677
  if (bfd_is_und_section (symbol->section)
678
      && output_bfd == (bfd *) NULL)
679
    ret = bfd_reloc_undefined;
680
 
681
  if (bfd_is_com_section (symbol->section))
682
    relocation = 0;
683
  else
684
    relocation = symbol->value;
685
 
686
  relocation += symbol->section->output_section->vma;
687
  relocation += symbol->section->output_offset;
688
  relocation += reloc_entry->addend;
689
 
690
  if (reloc_entry->address > input_section->_cooked_size)
691
    return bfd_reloc_outofrange;
692
 
693
  /* Save the information, and let REFLO do the actual relocation.  */
694
  n = (struct mips_hi *) bfd_malloc (sizeof *n);
695
  if (n == NULL)
696
    return bfd_reloc_outofrange;
697
  n->addr = (bfd_byte *) data + reloc_entry->address;
698
  n->addend = relocation;
699
  n->next = mips_refhi_list;
700
  mips_refhi_list = n;
701
 
702
  if (output_bfd != (bfd *) NULL)
703
    reloc_entry->address += input_section->output_offset;
704
 
705
  return ret;
706
}
707
 
708
/* Do a REFLO relocation.  This is a straightforward 16 bit inplace
709
   relocation; this function exists in order to do the REFHI
710
   relocation described above.  */
711
 
712
static bfd_reloc_status_type
713
mips_reflo_reloc (abfd,
714
                  reloc_entry,
715
                  symbol,
716
                  data,
717
                  input_section,
718
                  output_bfd,
719
                  error_message)
720
     bfd *abfd;
721
     arelent *reloc_entry;
722
     asymbol *symbol;
723
     PTR data;
724
     asection *input_section;
725
     bfd *output_bfd;
726
     char **error_message;
727
{
728
  if (mips_refhi_list != NULL)
729
    {
730
      struct mips_hi *l;
731
 
732
      l = mips_refhi_list;
733
      while (l != NULL)
734
        {
735
          unsigned long insn;
736
          unsigned long val;
737
          unsigned long vallo;
738
          struct mips_hi *next;
739
 
740
          /* Do the REFHI relocation.  Note that we actually don't
741
             need to know anything about the REFLO itself, except
742
             where to find the low 16 bits of the addend needed by the
743
             REFHI.  */
744
          insn = bfd_get_32 (abfd, l->addr);
745
          vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
746
                   & 0xffff);
747
          val = ((insn & 0xffff) << 16) + vallo;
748
          val += l->addend;
749
 
750
          /* The low order 16 bits are always treated as a signed
751
             value.  Therefore, a negative value in the low order bits
752
             requires an adjustment in the high order bits.  We need
753
             to make this adjustment in two ways: once for the bits we
754
             took from the data, and once for the bits we are putting
755
             back in to the data.  */
756
          if ((vallo & 0x8000) != 0)
757
            val -= 0x10000;
758
          if ((val & 0x8000) != 0)
759
            val += 0x10000;
760
 
761
          insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
762
          bfd_put_32 (abfd, insn, l->addr);
763
 
764
          next = l->next;
765
          free (l);
766
          l = next;
767
        }
768
 
769
      mips_refhi_list = NULL;
770
    }
771
 
772
  /* Now do the REFLO reloc in the usual way.  */
773
  return mips_generic_reloc (abfd, reloc_entry, symbol, data,
774
                              input_section, output_bfd, error_message);
775
}
776
 
777
/* Do a GPREL relocation.  This is a 16 bit value which must become
778
   the offset from the gp register.  */
779
 
780
static bfd_reloc_status_type
781
mips_gprel_reloc (abfd,
782
                  reloc_entry,
783
                  symbol,
784
                  data,
785
                  input_section,
786
                  output_bfd,
787
                  error_message)
788
     bfd *abfd;
789
     arelent *reloc_entry;
790
     asymbol *symbol;
791
     PTR data;
792
     asection *input_section;
793
     bfd *output_bfd;
794
     char **error_message;
795
{
796
  boolean relocateable;
797
  bfd_vma gp;
798
  bfd_vma relocation;
799
  unsigned long val;
800
  unsigned long insn;
801
 
802
  /* If we're relocating, and this is an external symbol with no
803
     addend, we don't want to change anything.  We will only have an
804
     addend if this is a newly created reloc, not read from an ECOFF
805
     file.  */
806
  if (output_bfd != (bfd *) NULL
807
      && (symbol->flags & BSF_SECTION_SYM) == 0
808
      && reloc_entry->addend == 0)
809
    {
810
      reloc_entry->address += input_section->output_offset;
811
      return bfd_reloc_ok;
812
    }
813
 
814
  if (output_bfd != (bfd *) NULL)
815
    relocateable = true;
816
  else
817
    {
818
      relocateable = false;
819
      output_bfd = symbol->section->output_section->owner;
820
    }
821
 
822
  if (bfd_is_und_section (symbol->section)
823
      && relocateable == false)
824
    return bfd_reloc_undefined;
825
 
826
  /* We have to figure out the gp value, so that we can adjust the
827
     symbol value correctly.  We look up the symbol _gp in the output
828
     BFD.  If we can't find it, we're stuck.  We cache it in the ECOFF
829
     target data.  We don't need to adjust the symbol value for an
830
     external symbol if we are producing relocateable output.  */
831
  gp = _bfd_get_gp_value (output_bfd);
832
  if (gp == 0
833
      && (relocateable == false
834
          || (symbol->flags & BSF_SECTION_SYM) != 0))
835
    {
836
      if (relocateable != false)
837
        {
838
          /* Make up a value.  */
839
          gp = symbol->section->output_section->vma + 0x4000;
840
          _bfd_set_gp_value (output_bfd, gp);
841
        }
842
      else
843
        {
844
          unsigned int count;
845
          asymbol **sym;
846
          unsigned int i;
847
 
848
          count = bfd_get_symcount (output_bfd);
849
          sym = bfd_get_outsymbols (output_bfd);
850
 
851
          if (sym == (asymbol **) NULL)
852
            i = count;
853
          else
854
            {
855
              for (i = 0; i < count; i++, sym++)
856
                {
857
                  register CONST char *name;
858
 
859
                  name = bfd_asymbol_name (*sym);
860
                  if (*name == '_' && strcmp (name, "_gp") == 0)
861
                    {
862
                      gp = bfd_asymbol_value (*sym);
863
                      _bfd_set_gp_value (output_bfd, gp);
864
                      break;
865
                    }
866
                }
867
            }
868
 
869
          if (i >= count)
870
            {
871
              /* Only get the error once.  */
872
              gp = 4;
873
              _bfd_set_gp_value (output_bfd, gp);
874
              *error_message =
875
                (char *) _("GP relative relocation when _gp not defined");
876
              return bfd_reloc_dangerous;
877
            }
878
        }
879
    }
880
 
881
  if (bfd_is_com_section (symbol->section))
882
    relocation = 0;
883
  else
884
    relocation = symbol->value;
885
 
886
  relocation += symbol->section->output_section->vma;
887
  relocation += symbol->section->output_offset;
888
 
889
  if (reloc_entry->address > input_section->_cooked_size)
890
    return bfd_reloc_outofrange;
891
 
892
  insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
893
 
894
  /* Set val to the offset into the section or symbol.  */
895
  val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
896
  if (val & 0x8000)
897
    val -= 0x10000;
898
 
899
  /* Adjust val for the final section location and GP value.  If we
900
     are producing relocateable output, we don't want to do this for
901
     an external symbol.  */
902
  if (relocateable == false
903
      || (symbol->flags & BSF_SECTION_SYM) != 0)
904
    val += relocation - gp;
905
 
906
  insn = (insn &~ 0xffff) | (val & 0xffff);
907
  bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
908
 
909
  if (relocateable != false)
910
    reloc_entry->address += input_section->output_offset;
911
 
912
  /* Make sure it fit in 16 bits.  */
913
  if (val >= 0x8000 && val < 0xffff8000)
914
    return bfd_reloc_overflow;
915
 
916
  return bfd_reloc_ok;
917
}
918
 
919
/* Do a RELHI relocation.  We do this in conjunction with a RELLO
920
   reloc, just as REFHI and REFLO are done together.  RELHI and RELLO
921
   are Cygnus extensions used when generating position independent
922
   code for embedded systems.  */
923
 
924
/* FIXME: This should not be a static variable.  */
925
 
926
static struct mips_hi *mips_relhi_list;
927
 
928
static bfd_reloc_status_type
929
mips_relhi_reloc (abfd,
930
                  reloc_entry,
931
                  symbol,
932
                  data,
933
                  input_section,
934
                  output_bfd,
935
                  error_message)
936
     bfd *abfd ATTRIBUTE_UNUSED;
937
     arelent *reloc_entry;
938
     asymbol *symbol;
939
     PTR data;
940
     asection *input_section;
941
     bfd *output_bfd;
942
     char **error_message ATTRIBUTE_UNUSED;
943
{
944
  bfd_reloc_status_type ret;
945
  bfd_vma relocation;
946
  struct mips_hi *n;
947
 
948
  /* If this is a reloc against a section symbol, then it is correct
949
     in the object file.  The only time we want to change this case is
950
     when we are relaxing, and that is handled entirely by
951
     mips_relocate_section and never calls this function.  */
952
  if ((symbol->flags & BSF_SECTION_SYM) != 0)
953
    {
954
      if (output_bfd != (bfd *) NULL)
955
        reloc_entry->address += input_section->output_offset;
956
      return bfd_reloc_ok;
957
    }
958
 
959
  /* This is an external symbol.  If we're relocating, we don't want
960
     to change anything.  */
961
  if (output_bfd != (bfd *) NULL)
962
    {
963
      reloc_entry->address += input_section->output_offset;
964
      return bfd_reloc_ok;
965
    }
966
 
967
  ret = bfd_reloc_ok;
968
  if (bfd_is_und_section (symbol->section)
969
      && output_bfd == (bfd *) NULL)
970
    ret = bfd_reloc_undefined;
971
 
972
  if (bfd_is_com_section (symbol->section))
973
    relocation = 0;
974
  else
975
    relocation = symbol->value;
976
 
977
  relocation += symbol->section->output_section->vma;
978
  relocation += symbol->section->output_offset;
979
  relocation += reloc_entry->addend;
980
 
981
  if (reloc_entry->address > input_section->_cooked_size)
982
    return bfd_reloc_outofrange;
983
 
984
  /* Save the information, and let RELLO do the actual relocation.  */
985
  n = (struct mips_hi *) bfd_malloc (sizeof *n);
986
  if (n == NULL)
987
    return bfd_reloc_outofrange;
988
  n->addr = (bfd_byte *) data + reloc_entry->address;
989
  n->addend = relocation;
990
  n->next = mips_relhi_list;
991
  mips_relhi_list = n;
992
 
993
  if (output_bfd != (bfd *) NULL)
994
    reloc_entry->address += input_section->output_offset;
995
 
996
  return ret;
997
}
998
 
999
/* Do a RELLO relocation.  This is a straightforward 16 bit PC
1000
   relative relocation; this function exists in order to do the RELHI
1001
   relocation described above.  */
1002
 
1003
static bfd_reloc_status_type
1004
mips_rello_reloc (abfd,
1005
                  reloc_entry,
1006
                  symbol,
1007
                  data,
1008
                  input_section,
1009
                  output_bfd,
1010
                  error_message)
1011
     bfd *abfd;
1012
     arelent *reloc_entry;
1013
     asymbol *symbol;
1014
     PTR data;
1015
     asection *input_section;
1016
     bfd *output_bfd;
1017
     char **error_message;
1018
{
1019
  if (mips_relhi_list != NULL)
1020
    {
1021
      struct mips_hi *l;
1022
 
1023
      l = mips_relhi_list;
1024
      while (l != NULL)
1025
        {
1026
          unsigned long insn;
1027
          unsigned long val;
1028
          unsigned long vallo;
1029
          struct mips_hi *next;
1030
 
1031
          /* Do the RELHI relocation.  Note that we actually don't
1032
             need to know anything about the RELLO itself, except
1033
             where to find the low 16 bits of the addend needed by the
1034
             RELHI.  */
1035
          insn = bfd_get_32 (abfd, l->addr);
1036
          vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1037
                   & 0xffff);
1038
          val = ((insn & 0xffff) << 16) + vallo;
1039
          val += l->addend;
1040
 
1041
          /* If the symbol is defined, make val PC relative.  If the
1042
             symbol is not defined we don't want to do this, because
1043
             we don't want the value in the object file to incorporate
1044
             the address of the reloc.  */
1045
          if (! bfd_is_und_section (bfd_get_section (symbol))
1046
              && ! bfd_is_com_section (bfd_get_section (symbol)))
1047
            val -= (input_section->output_section->vma
1048
                    + input_section->output_offset
1049
                    + reloc_entry->address);
1050
 
1051
          /* The low order 16 bits are always treated as a signed
1052
             value.  Therefore, a negative value in the low order bits
1053
             requires an adjustment in the high order bits.  We need
1054
             to make this adjustment in two ways: once for the bits we
1055
             took from the data, and once for the bits we are putting
1056
             back in to the data.  */
1057
          if ((vallo & 0x8000) != 0)
1058
            val -= 0x10000;
1059
          if ((val & 0x8000) != 0)
1060
            val += 0x10000;
1061
 
1062
          insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1063
          bfd_put_32 (abfd, insn, l->addr);
1064
 
1065
          next = l->next;
1066
          free (l);
1067
          l = next;
1068
        }
1069
 
1070
      mips_relhi_list = NULL;
1071
    }
1072
 
1073
  /* If this is a reloc against a section symbol, then it is correct
1074
     in the object file.  The only time we want to change this case is
1075
     when we are relaxing, and that is handled entirely by
1076
     mips_relocate_section and never calls this function.  */
1077
  if ((symbol->flags & BSF_SECTION_SYM) != 0)
1078
    {
1079
      if (output_bfd != (bfd *) NULL)
1080
        reloc_entry->address += input_section->output_offset;
1081
      return bfd_reloc_ok;
1082
    }
1083
 
1084
  /* bfd_perform_relocation does not handle pcrel_offset relocations
1085
     correctly when generating a relocateable file, so handle them
1086
     directly here.  */
1087
  if (output_bfd != (bfd *) NULL)
1088
    {
1089
      reloc_entry->address += input_section->output_offset;
1090
      return bfd_reloc_ok;
1091
    }
1092
 
1093
  /* Now do the RELLO reloc in the usual way.  */
1094
  return mips_generic_reloc (abfd, reloc_entry, symbol, data,
1095
                              input_section, output_bfd, error_message);
1096
}
1097
 
1098
/* This is the special function for the MIPS_R_SWITCH reloc.  This
1099
   special reloc is normally correct in the object file, and only
1100
   requires special handling when relaxing.  We don't want
1101
   bfd_perform_relocation to tamper with it at all.  */
1102
 
1103
/*ARGSUSED*/
1104
static bfd_reloc_status_type
1105
mips_switch_reloc (abfd,
1106
                   reloc_entry,
1107
                   symbol,
1108
                   data,
1109
                   input_section,
1110
                   output_bfd,
1111
                   error_message)
1112
     bfd *abfd ATTRIBUTE_UNUSED;
1113
     arelent *reloc_entry ATTRIBUTE_UNUSED;
1114
     asymbol *symbol ATTRIBUTE_UNUSED;
1115
     PTR data ATTRIBUTE_UNUSED;
1116
     asection *input_section ATTRIBUTE_UNUSED;
1117
     bfd *output_bfd ATTRIBUTE_UNUSED;
1118
     char **error_message ATTRIBUTE_UNUSED;
1119
{
1120
  return bfd_reloc_ok;
1121
}
1122
 
1123
/* Get the howto structure for a generic reloc type.  */
1124
 
1125
static reloc_howto_type *
1126
mips_bfd_reloc_type_lookup (abfd, code)
1127
     bfd *abfd ATTRIBUTE_UNUSED;
1128
     bfd_reloc_code_real_type code;
1129
{
1130
  int mips_type;
1131
 
1132
  switch (code)
1133
    {
1134
    case BFD_RELOC_16:
1135
      mips_type = MIPS_R_REFHALF;
1136
      break;
1137
    case BFD_RELOC_32:
1138
    case BFD_RELOC_CTOR:
1139
      mips_type = MIPS_R_REFWORD;
1140
      break;
1141
    case BFD_RELOC_MIPS_JMP:
1142
      mips_type = MIPS_R_JMPADDR;
1143
      break;
1144
    case BFD_RELOC_HI16_S:
1145
      mips_type = MIPS_R_REFHI;
1146
      break;
1147
    case BFD_RELOC_LO16:
1148
      mips_type = MIPS_R_REFLO;
1149
      break;
1150
    case BFD_RELOC_MIPS_GPREL:
1151
      mips_type = MIPS_R_GPREL;
1152
      break;
1153
    case BFD_RELOC_MIPS_LITERAL:
1154
      mips_type = MIPS_R_LITERAL;
1155
      break;
1156
    case BFD_RELOC_16_PCREL_S2:
1157
      mips_type = MIPS_R_PCREL16;
1158
      break;
1159
    case BFD_RELOC_PCREL_HI16_S:
1160
      mips_type = MIPS_R_RELHI;
1161
      break;
1162
    case BFD_RELOC_PCREL_LO16:
1163
      mips_type = MIPS_R_RELLO;
1164
      break;
1165
    case BFD_RELOC_GPREL32:
1166
      mips_type = MIPS_R_SWITCH;
1167
      break;
1168
    default:
1169
      return (reloc_howto_type *) NULL;
1170
    }
1171
 
1172
  return &mips_howto_table[mips_type];
1173
}
1174
 
1175
/* A helper routine for mips_relocate_section which handles the REFHI
1176
   and RELHI relocations.  The REFHI relocation must be followed by a
1177
   REFLO relocation (and RELHI by a RELLO), and the addend used is
1178
   formed from the addends of both instructions.  */
1179
 
1180
static void
1181
mips_relocate_hi (refhi, reflo, input_bfd, input_section, contents, adjust,
1182
                  relocation, pcrel)
1183
     struct internal_reloc *refhi;
1184
     struct internal_reloc *reflo;
1185
     bfd *input_bfd;
1186
     asection *input_section;
1187
     bfd_byte *contents;
1188
     size_t adjust;
1189
     bfd_vma relocation;
1190
     boolean pcrel;
1191
{
1192
  unsigned long insn;
1193
  unsigned long val;
1194
  unsigned long vallo;
1195
 
1196
  if (refhi == NULL)
1197
    return;
1198
 
1199
  insn = bfd_get_32 (input_bfd,
1200
                     contents + adjust + refhi->r_vaddr - input_section->vma);
1201
  if (reflo == NULL)
1202
    vallo = 0;
1203
  else
1204
    vallo = (bfd_get_32 (input_bfd,
1205
                         contents + adjust + reflo->r_vaddr - input_section->vma)
1206
             & 0xffff);
1207
 
1208
  val = ((insn & 0xffff) << 16) + vallo;
1209
  val += relocation;
1210
 
1211
  /* The low order 16 bits are always treated as a signed value.
1212
     Therefore, a negative value in the low order bits requires an
1213
     adjustment in the high order bits.  We need to make this
1214
     adjustment in two ways: once for the bits we took from the data,
1215
     and once for the bits we are putting back in to the data.  */
1216
  if ((vallo & 0x8000) != 0)
1217
    val -= 0x10000;
1218
 
1219
  if (pcrel)
1220
    val -= (input_section->output_section->vma
1221
            + input_section->output_offset
1222
            + (reflo->r_vaddr - input_section->vma + adjust));
1223
 
1224
  if ((val & 0x8000) != 0)
1225
    val += 0x10000;
1226
 
1227
  insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1228
  bfd_put_32 (input_bfd, (bfd_vma) insn,
1229
              contents + adjust + refhi->r_vaddr - input_section->vma);
1230
}
1231
 
1232
/* Relocate a section while linking a MIPS ECOFF file.  */
1233
 
1234
static boolean
1235
mips_relocate_section (output_bfd, info, input_bfd, input_section,
1236
                       contents, external_relocs)
1237
     bfd *output_bfd;
1238
     struct bfd_link_info *info;
1239
     bfd *input_bfd;
1240
     asection *input_section;
1241
     bfd_byte *contents;
1242
     PTR external_relocs;
1243
{
1244
  asection **symndx_to_section;
1245
  struct ecoff_link_hash_entry **sym_hashes;
1246
  bfd_vma gp;
1247
  boolean gp_undefined;
1248
  size_t adjust;
1249
  long *offsets;
1250
  struct external_reloc *ext_rel;
1251
  struct external_reloc *ext_rel_end;
1252
  unsigned int i;
1253
  boolean got_lo;
1254
  struct internal_reloc lo_int_rel;
1255
 
1256
  BFD_ASSERT (input_bfd->xvec->byteorder
1257
              == output_bfd->xvec->byteorder);
1258
 
1259
  /* We keep a table mapping the symndx found in an internal reloc to
1260
     the appropriate section.  This is faster than looking up the
1261
     section by name each time.  */
1262
  symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
1263
  if (symndx_to_section == (asection **) NULL)
1264
    {
1265
      symndx_to_section = ((asection **)
1266
                           bfd_alloc (input_bfd,
1267
                                      (NUM_RELOC_SECTIONS
1268
                                       * sizeof (asection *))));
1269
      if (!symndx_to_section)
1270
        return false;
1271
 
1272
      symndx_to_section[RELOC_SECTION_NONE] = NULL;
1273
      symndx_to_section[RELOC_SECTION_TEXT] =
1274
        bfd_get_section_by_name (input_bfd, ".text");
1275
      symndx_to_section[RELOC_SECTION_RDATA] =
1276
        bfd_get_section_by_name (input_bfd, ".rdata");
1277
      symndx_to_section[RELOC_SECTION_DATA] =
1278
        bfd_get_section_by_name (input_bfd, ".data");
1279
      symndx_to_section[RELOC_SECTION_SDATA] =
1280
        bfd_get_section_by_name (input_bfd, ".sdata");
1281
      symndx_to_section[RELOC_SECTION_SBSS] =
1282
        bfd_get_section_by_name (input_bfd, ".sbss");
1283
      symndx_to_section[RELOC_SECTION_BSS] =
1284
        bfd_get_section_by_name (input_bfd, ".bss");
1285
      symndx_to_section[RELOC_SECTION_INIT] =
1286
        bfd_get_section_by_name (input_bfd, ".init");
1287
      symndx_to_section[RELOC_SECTION_LIT8] =
1288
        bfd_get_section_by_name (input_bfd, ".lit8");
1289
      symndx_to_section[RELOC_SECTION_LIT4] =
1290
        bfd_get_section_by_name (input_bfd, ".lit4");
1291
      symndx_to_section[RELOC_SECTION_XDATA] = NULL;
1292
      symndx_to_section[RELOC_SECTION_PDATA] = NULL;
1293
      symndx_to_section[RELOC_SECTION_FINI] =
1294
        bfd_get_section_by_name (input_bfd, ".fini");
1295
      symndx_to_section[RELOC_SECTION_LITA] = NULL;
1296
      symndx_to_section[RELOC_SECTION_ABS] = NULL;
1297
 
1298
      ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
1299
    }
1300
 
1301
  sym_hashes = ecoff_data (input_bfd)->sym_hashes;
1302
 
1303
  gp = _bfd_get_gp_value (output_bfd);
1304
  if (gp == 0)
1305
    gp_undefined = true;
1306
  else
1307
    gp_undefined = false;
1308
 
1309
  got_lo = false;
1310
 
1311
  adjust = 0;
1312
 
1313
  if (ecoff_section_data (input_bfd, input_section) == NULL)
1314
    offsets = NULL;
1315
  else
1316
    offsets = ecoff_section_data (input_bfd, input_section)->offsets;
1317
 
1318
  ext_rel = (struct external_reloc *) external_relocs;
1319
  ext_rel_end = ext_rel + input_section->reloc_count;
1320
  for (i = 0; ext_rel < ext_rel_end; ext_rel++, i++)
1321
    {
1322
      struct internal_reloc int_rel;
1323
      boolean use_lo = false;
1324
      bfd_vma addend;
1325
      reloc_howto_type *howto;
1326
      struct ecoff_link_hash_entry *h = NULL;
1327
      asection *s = NULL;
1328
      bfd_vma relocation;
1329
      bfd_reloc_status_type r;
1330
 
1331
      if (! got_lo)
1332
        mips_ecoff_swap_reloc_in (input_bfd, (PTR) ext_rel, &int_rel);
1333
      else
1334
        {
1335
          int_rel = lo_int_rel;
1336
          got_lo = false;
1337
        }
1338
 
1339
      BFD_ASSERT (int_rel.r_type
1340
                  < sizeof mips_howto_table / sizeof mips_howto_table[0]);
1341
 
1342
      /* The REFHI and RELHI relocs requires special handling.  they
1343
         must be followed by a REFLO or RELLO reloc, respectively, and
1344
         the addend is formed from both relocs.  */
1345
      if (int_rel.r_type == MIPS_R_REFHI
1346
          || int_rel.r_type == MIPS_R_RELHI)
1347
        {
1348
          struct external_reloc *lo_ext_rel;
1349
 
1350
          /* As a GNU extension, permit an arbitrary number of REFHI
1351
             or RELHI relocs before the REFLO or RELLO reloc.  This
1352
             permits gcc to emit the HI and LO relocs itself.  */
1353
          for (lo_ext_rel = ext_rel + 1;
1354
               lo_ext_rel < ext_rel_end;
1355
               lo_ext_rel++)
1356
            {
1357
              mips_ecoff_swap_reloc_in (input_bfd, (PTR) lo_ext_rel,
1358
                                        &lo_int_rel);
1359
              if (lo_int_rel.r_type != int_rel.r_type)
1360
                break;
1361
            }
1362
 
1363
          if (lo_ext_rel < ext_rel_end
1364
              && (lo_int_rel.r_type
1365
                  == (int_rel.r_type == MIPS_R_REFHI
1366
                      ? MIPS_R_REFLO
1367
                      : MIPS_R_RELLO))
1368
              && int_rel.r_extern == lo_int_rel.r_extern
1369
              && int_rel.r_symndx == lo_int_rel.r_symndx)
1370
            {
1371
              use_lo = true;
1372
              if (lo_ext_rel == ext_rel + 1)
1373
                got_lo = true;
1374
            }
1375
        }
1376
 
1377
      howto = &mips_howto_table[int_rel.r_type];
1378
 
1379
      /* The SWITCH reloc must be handled specially.  This reloc is
1380
         marks the location of a difference between two portions of an
1381
         object file.  The symbol index does not reference a symbol,
1382
         but is actually the offset from the reloc to the subtrahend
1383
         of the difference.  This reloc is correct in the object file,
1384
         and needs no further adjustment, unless we are relaxing.  If
1385
         we are relaxing, we may have to add in an offset.  Since no
1386
         symbols are involved in this reloc, we handle it completely
1387
         here.  */
1388
      if (int_rel.r_type == MIPS_R_SWITCH)
1389
        {
1390
          if (offsets != NULL
1391
              && offsets[i] != 0)
1392
            {
1393
              r = _bfd_relocate_contents (howto, input_bfd,
1394
                                          (bfd_vma) offsets[i],
1395
                                          (contents
1396
                                           + adjust
1397
                                           + int_rel.r_vaddr
1398
                                           - input_section->vma));
1399
              BFD_ASSERT (r == bfd_reloc_ok);
1400
            }
1401
 
1402
          continue;
1403
        }
1404
 
1405
      if (int_rel.r_extern)
1406
        {
1407
          h = sym_hashes[int_rel.r_symndx];
1408
          /* If h is NULL, that means that there is a reloc against an
1409
             external symbol which we thought was just a debugging
1410
             symbol.  This should not happen.  */
1411
          if (h == (struct ecoff_link_hash_entry *) NULL)
1412
            abort ();
1413
        }
1414
      else
1415
        {
1416
          if (int_rel.r_symndx < 0 || int_rel.r_symndx >= NUM_RELOC_SECTIONS)
1417
            s = NULL;
1418
          else
1419
            s = symndx_to_section[int_rel.r_symndx];
1420
 
1421
          if (s == (asection *) NULL)
1422
            abort ();
1423
        }
1424
 
1425
      /* The GPREL reloc uses an addend: the difference in the GP
1426
         values.  */
1427
      if (int_rel.r_type != MIPS_R_GPREL
1428
          && int_rel.r_type != MIPS_R_LITERAL)
1429
        addend = 0;
1430
      else
1431
        {
1432
          if (gp_undefined)
1433
            {
1434
              if (! ((*info->callbacks->reloc_dangerous)
1435
                     (info, _("GP relative relocation when GP not defined"),
1436
                      input_bfd, input_section,
1437
                      int_rel.r_vaddr - input_section->vma)))
1438
                return false;
1439
              /* Only give the error once per link.  */
1440
              gp = 4;
1441
              _bfd_set_gp_value (output_bfd, gp);
1442
              gp_undefined = false;
1443
            }
1444
          if (! int_rel.r_extern)
1445
            {
1446
              /* This is a relocation against a section.  The current
1447
                 addend in the instruction is the difference between
1448
                 INPUT_SECTION->vma and the GP value of INPUT_BFD.  We
1449
                 must change this to be the difference between the
1450
                 final definition (which will end up in RELOCATION)
1451
                 and the GP value of OUTPUT_BFD (which is in GP).  */
1452
              addend = ecoff_data (input_bfd)->gp - gp;
1453
            }
1454
          else if (! info->relocateable
1455
                   || h->root.type == bfd_link_hash_defined
1456
                   || h->root.type == bfd_link_hash_defweak)
1457
            {
1458
              /* This is a relocation against a defined symbol.  The
1459
                 current addend in the instruction is simply the
1460
                 desired offset into the symbol (normally zero).  We
1461
                 are going to change this into a relocation against a
1462
                 defined symbol, so we want the instruction to hold
1463
                 the difference between the final definition of the
1464
                 symbol (which will end up in RELOCATION) and the GP
1465
                 value of OUTPUT_BFD (which is in GP).  */
1466
              addend = - gp;
1467
            }
1468
          else
1469
            {
1470
              /* This is a relocation against an undefined or common
1471
                 symbol.  The current addend in the instruction is
1472
                 simply the desired offset into the symbol (normally
1473
                 zero).  We are generating relocateable output, and we
1474
                 aren't going to define this symbol, so we just leave
1475
                 the instruction alone.  */
1476
              addend = 0;
1477
            }
1478
        }
1479
 
1480
      /* If we are relaxing, mips_relax_section may have set
1481
         offsets[i] to some value.  A value of 1 means we must expand
1482
         a PC relative branch into a multi-instruction of sequence,
1483
         and any other value is an addend.  */
1484
      if (offsets != NULL
1485
          && offsets[i] != 0)
1486
        {
1487
          BFD_ASSERT (! info->relocateable);
1488
          BFD_ASSERT (int_rel.r_type == MIPS_R_PCREL16
1489
                      || int_rel.r_type == MIPS_R_RELHI
1490
                      || int_rel.r_type == MIPS_R_RELLO);
1491
          if (offsets[i] != 1)
1492
            addend += offsets[i];
1493
          else
1494
            {
1495
              bfd_byte *here;
1496
 
1497
              BFD_ASSERT (int_rel.r_extern
1498
                          && int_rel.r_type == MIPS_R_PCREL16);
1499
 
1500
              /* Move the rest of the instructions up.  */
1501
              here = (contents
1502
                      + adjust
1503
                      + int_rel.r_vaddr
1504
                      - input_section->vma);
1505
              memmove (here + PCREL16_EXPANSION_ADJUSTMENT, here,
1506
                       (size_t) (input_section->_raw_size
1507
                                 - (int_rel.r_vaddr - input_section->vma)));
1508
 
1509
              /* Generate the new instructions.  */
1510
              if (! mips_relax_pcrel16 (info, input_bfd, input_section,
1511
                                        h, here,
1512
                                        (input_section->output_section->vma
1513
                                         + input_section->output_offset
1514
                                         + (int_rel.r_vaddr
1515
                                            - input_section->vma)
1516
                                         + adjust)))
1517
                return false;
1518
 
1519
              /* We must adjust everything else up a notch.  */
1520
              adjust += PCREL16_EXPANSION_ADJUSTMENT;
1521
 
1522
              /* mips_relax_pcrel16 handles all the details of this
1523
                 relocation.  */
1524
              continue;
1525
            }
1526
        }
1527
 
1528
      /* If we are relaxing, and this is a reloc against the .text
1529
         segment, we may need to adjust it if some branches have been
1530
         expanded.  The reloc types which are likely to occur in the
1531
         .text section are handled efficiently by mips_relax_section,
1532
         and thus do not need to be handled here.  */
1533
      if (ecoff_data (input_bfd)->debug_info.adjust != NULL
1534
          && ! int_rel.r_extern
1535
          && int_rel.r_symndx == RELOC_SECTION_TEXT
1536
          && (strcmp (bfd_get_section_name (input_bfd, input_section),
1537
                      ".text") != 0
1538
              || (int_rel.r_type != MIPS_R_PCREL16
1539
                  && int_rel.r_type != MIPS_R_SWITCH
1540
                  && int_rel.r_type != MIPS_R_RELHI
1541
                  && int_rel.r_type != MIPS_R_RELLO)))
1542
        {
1543
          bfd_vma adr;
1544
          struct ecoff_value_adjust *a;
1545
 
1546
          /* We need to get the addend so that we know whether we need
1547
             to adjust the address.  */
1548
          BFD_ASSERT (int_rel.r_type == MIPS_R_REFWORD);
1549
 
1550
          adr = bfd_get_32 (input_bfd,
1551
                            (contents
1552
                             + adjust
1553
                             + int_rel.r_vaddr
1554
                             - input_section->vma));
1555
 
1556
          for (a = ecoff_data (input_bfd)->debug_info.adjust;
1557
               a != (struct ecoff_value_adjust *) NULL;
1558
               a = a->next)
1559
            {
1560
              if (adr >= a->start && adr < a->end)
1561
                addend += a->adjust;
1562
            }
1563
        }
1564
 
1565
      if (info->relocateable)
1566
        {
1567
          /* We are generating relocateable output, and must convert
1568
             the existing reloc.  */
1569
          if (int_rel.r_extern)
1570
            {
1571
              if ((h->root.type == bfd_link_hash_defined
1572
                   || h->root.type == bfd_link_hash_defweak)
1573
                  && ! bfd_is_abs_section (h->root.u.def.section))
1574
                {
1575
                  const char *name;
1576
 
1577
                  /* This symbol is defined in the output.  Convert
1578
                     the reloc from being against the symbol to being
1579
                     against the section.  */
1580
 
1581
                  /* Clear the r_extern bit.  */
1582
                  int_rel.r_extern = 0;
1583
 
1584
                  /* Compute a new r_symndx value.  */
1585
                  s = h->root.u.def.section;
1586
                  name = bfd_get_section_name (output_bfd,
1587
                                               s->output_section);
1588
 
1589
                  int_rel.r_symndx = -1;
1590
                  switch (name[1])
1591
                    {
1592
                    case 'b':
1593
                      if (strcmp (name, ".bss") == 0)
1594
                        int_rel.r_symndx = RELOC_SECTION_BSS;
1595
                      break;
1596
                    case 'd':
1597
                      if (strcmp (name, ".data") == 0)
1598
                        int_rel.r_symndx = RELOC_SECTION_DATA;
1599
                      break;
1600
                    case 'f':
1601
                      if (strcmp (name, ".fini") == 0)
1602
                        int_rel.r_symndx = RELOC_SECTION_FINI;
1603
                      break;
1604
                    case 'i':
1605
                      if (strcmp (name, ".init") == 0)
1606
                        int_rel.r_symndx = RELOC_SECTION_INIT;
1607
                      break;
1608
                    case 'l':
1609
                      if (strcmp (name, ".lit8") == 0)
1610
                        int_rel.r_symndx = RELOC_SECTION_LIT8;
1611
                      else if (strcmp (name, ".lit4") == 0)
1612
                        int_rel.r_symndx = RELOC_SECTION_LIT4;
1613
                      break;
1614
                    case 'r':
1615
                      if (strcmp (name, ".rdata") == 0)
1616
                        int_rel.r_symndx = RELOC_SECTION_RDATA;
1617
                      break;
1618
                    case 's':
1619
                      if (strcmp (name, ".sdata") == 0)
1620
                        int_rel.r_symndx = RELOC_SECTION_SDATA;
1621
                      else if (strcmp (name, ".sbss") == 0)
1622
                        int_rel.r_symndx = RELOC_SECTION_SBSS;
1623
                      break;
1624
                    case 't':
1625
                      if (strcmp (name, ".text") == 0)
1626
                        int_rel.r_symndx = RELOC_SECTION_TEXT;
1627
                      break;
1628
                    }
1629
 
1630
                  if (int_rel.r_symndx == -1)
1631
                    abort ();
1632
 
1633
                  /* Add the section VMA and the symbol value.  */
1634
                  relocation = (h->root.u.def.value
1635
                                + s->output_section->vma
1636
                                + s->output_offset);
1637
 
1638
                  /* For a PC relative relocation, the object file
1639
                     currently holds just the addend.  We must adjust
1640
                     by the address to get the right value.  */
1641
                  if (howto->pc_relative)
1642
                    {
1643
                      relocation -= int_rel.r_vaddr - input_section->vma;
1644
 
1645
                      /* If we are converting a RELHI or RELLO reloc
1646
                         from being against an external symbol to
1647
                         being against a section, we must put a
1648
                         special value into the r_offset field.  This
1649
                         value is the old addend.  The r_offset for
1650
                         both the RELHI and RELLO relocs are the same,
1651
                         and we set both when we see RELHI.  */
1652
                      if (int_rel.r_type == MIPS_R_RELHI)
1653
                        {
1654
                          long addhi, addlo;
1655
 
1656
                          addhi = bfd_get_32 (input_bfd,
1657
                                              (contents
1658
                                               + adjust
1659
                                               + int_rel.r_vaddr
1660
                                               - input_section->vma));
1661
                          addhi &= 0xffff;
1662
                          if (addhi & 0x8000)
1663
                            addhi -= 0x10000;
1664
                          addhi <<= 16;
1665
 
1666
                          if (! use_lo)
1667
                            addlo = 0;
1668
                          else
1669
                            {
1670
                              addlo = bfd_get_32 (input_bfd,
1671
                                                  (contents
1672
                                                   + adjust
1673
                                                   + lo_int_rel.r_vaddr
1674
                                                   - input_section->vma));
1675
                              addlo &= 0xffff;
1676
                              if (addlo & 0x8000)
1677
                                addlo -= 0x10000;
1678
 
1679
                              lo_int_rel.r_offset = addhi + addlo;
1680
                            }
1681
 
1682
                          int_rel.r_offset = addhi + addlo;
1683
                        }
1684
                    }
1685
 
1686
                  h = NULL;
1687
                }
1688
              else
1689
                {
1690
                  /* Change the symndx value to the right one for the
1691
                     output BFD.  */
1692
                  int_rel.r_symndx = h->indx;
1693
                  if (int_rel.r_symndx == -1)
1694
                    {
1695
                      /* This symbol is not being written out.  */
1696
                      if (! ((*info->callbacks->unattached_reloc)
1697
                             (info, h->root.root.string, input_bfd,
1698
                              input_section,
1699
                              int_rel.r_vaddr - input_section->vma)))
1700
                        return false;
1701
                      int_rel.r_symndx = 0;
1702
                    }
1703
                  relocation = 0;
1704
                }
1705
            }
1706
          else
1707
            {
1708
              /* This is a relocation against a section.  Adjust the
1709
                 value by the amount the section moved.  */
1710
              relocation = (s->output_section->vma
1711
                            + s->output_offset
1712
                            - s->vma);
1713
            }
1714
 
1715
          relocation += addend;
1716
          addend = 0;
1717
 
1718
          /* Adjust a PC relative relocation by removing the reference
1719
             to the original address in the section and including the
1720
             reference to the new address.  However, external RELHI
1721
             and RELLO relocs are PC relative, but don't include any
1722
             reference to the address.  The addend is merely an
1723
             addend.  */
1724
          if (howto->pc_relative
1725
              && (! int_rel.r_extern
1726
                  || (int_rel.r_type != MIPS_R_RELHI
1727
                      && int_rel.r_type != MIPS_R_RELLO)))
1728
            relocation -= (input_section->output_section->vma
1729
                           + input_section->output_offset
1730
                           - input_section->vma);
1731
 
1732
          /* Adjust the contents.  */
1733
          if (relocation == 0)
1734
            r = bfd_reloc_ok;
1735
          else
1736
            {
1737
              if (int_rel.r_type != MIPS_R_REFHI
1738
                  && int_rel.r_type != MIPS_R_RELHI)
1739
                r = _bfd_relocate_contents (howto, input_bfd, relocation,
1740
                                            (contents
1741
                                             + adjust
1742
                                             + int_rel.r_vaddr
1743
                                             - input_section->vma));
1744
              else
1745
                {
1746
                  mips_relocate_hi (&int_rel,
1747
                                    use_lo ? &lo_int_rel : NULL,
1748
                                    input_bfd, input_section, contents,
1749
                                    adjust, relocation,
1750
                                    int_rel.r_type == MIPS_R_RELHI);
1751
                  r = bfd_reloc_ok;
1752
                }
1753
            }
1754
 
1755
          /* Adjust the reloc address.  */
1756
          int_rel.r_vaddr += (input_section->output_section->vma
1757
                              + input_section->output_offset
1758
                              - input_section->vma);
1759
 
1760
          /* Save the changed reloc information.  */
1761
          mips_ecoff_swap_reloc_out (input_bfd, &int_rel, (PTR) ext_rel);
1762
        }
1763
      else
1764
        {
1765
          /* We are producing a final executable.  */
1766
          if (int_rel.r_extern)
1767
            {
1768
              /* This is a reloc against a symbol.  */
1769
              if (h->root.type == bfd_link_hash_defined
1770
                  || h->root.type == bfd_link_hash_defweak)
1771
                {
1772
                  asection *hsec;
1773
 
1774
                  hsec = h->root.u.def.section;
1775
                  relocation = (h->root.u.def.value
1776
                                + hsec->output_section->vma
1777
                                + hsec->output_offset);
1778
                }
1779
              else
1780
                {
1781
                  if (! ((*info->callbacks->undefined_symbol)
1782
                         (info, h->root.root.string, input_bfd,
1783
                          input_section,
1784
                          int_rel.r_vaddr - input_section->vma, true)))
1785
                    return false;
1786
                  relocation = 0;
1787
                }
1788
            }
1789
          else
1790
            {
1791
              /* This is a reloc against a section.  */
1792
              relocation = (s->output_section->vma
1793
                            + s->output_offset
1794
                            - s->vma);
1795
 
1796
              /* A PC relative reloc is already correct in the object
1797
                 file.  Make it look like a pcrel_offset relocation by
1798
                 adding in the start address.  */
1799
              if (howto->pc_relative)
1800
                {
1801
                  if (int_rel.r_type != MIPS_R_RELHI || ! use_lo)
1802
                    relocation += int_rel.r_vaddr + adjust;
1803
                  else
1804
                    relocation += lo_int_rel.r_vaddr + adjust;
1805
                }
1806
            }
1807
 
1808
          if (int_rel.r_type != MIPS_R_REFHI
1809
              && int_rel.r_type != MIPS_R_RELHI)
1810
            r = _bfd_final_link_relocate (howto,
1811
                                          input_bfd,
1812
                                          input_section,
1813
                                          contents,
1814
                                          (int_rel.r_vaddr
1815
                                           - input_section->vma
1816
                                           + adjust),
1817
                                          relocation,
1818
                                          addend);
1819
          else
1820
            {
1821
              mips_relocate_hi (&int_rel,
1822
                                use_lo ? &lo_int_rel : NULL,
1823
                                input_bfd, input_section, contents, adjust,
1824
                                relocation,
1825
                                int_rel.r_type == MIPS_R_RELHI);
1826
              r = bfd_reloc_ok;
1827
            }
1828
        }
1829
 
1830
      /* MIPS_R_JMPADDR requires peculiar overflow detection.  The
1831
         instruction provides a 28 bit address (the two lower bits are
1832
         implicit zeroes) which is combined with the upper four bits
1833
         of the instruction address.  */
1834
      if (r == bfd_reloc_ok
1835
          && int_rel.r_type == MIPS_R_JMPADDR
1836
          && (((relocation
1837
                + addend
1838
                + (int_rel.r_extern ? 0 : s->vma))
1839
               & 0xf0000000)
1840
              != ((input_section->output_section->vma
1841
                   + input_section->output_offset
1842
                   + (int_rel.r_vaddr - input_section->vma)
1843
                   + adjust)
1844
                  & 0xf0000000)))
1845
        r = bfd_reloc_overflow;
1846
 
1847
      if (r != bfd_reloc_ok)
1848
        {
1849
          switch (r)
1850
            {
1851
            default:
1852
            case bfd_reloc_outofrange:
1853
              abort ();
1854
            case bfd_reloc_overflow:
1855
              {
1856
                const char *name;
1857
 
1858
                if (int_rel.r_extern)
1859
                  name = h->root.root.string;
1860
                else
1861
                  name = bfd_section_name (input_bfd, s);
1862
                if (! ((*info->callbacks->reloc_overflow)
1863
                       (info, name, howto->name, (bfd_vma) 0,
1864
                        input_bfd, input_section,
1865
                        int_rel.r_vaddr - input_section->vma)))
1866
                  return false;
1867
              }
1868
              break;
1869
            }
1870
        }
1871
    }
1872
 
1873
  return true;
1874
}
1875
 
1876
/* Read in the relocs for a section.  */
1877
 
1878
static boolean
1879
mips_read_relocs (abfd, sec)
1880
     bfd *abfd;
1881
     asection *sec;
1882
{
1883
  struct ecoff_section_tdata *section_tdata;
1884
 
1885
  section_tdata = ecoff_section_data (abfd, sec);
1886
  if (section_tdata == (struct ecoff_section_tdata *) NULL)
1887
    {
1888
      sec->used_by_bfd =
1889
        (PTR) bfd_alloc (abfd, sizeof (struct ecoff_section_tdata));
1890
      if (sec->used_by_bfd == NULL)
1891
        return false;
1892
 
1893
      section_tdata = ecoff_section_data (abfd, sec);
1894
      section_tdata->external_relocs = NULL;
1895
      section_tdata->contents = NULL;
1896
      section_tdata->offsets = NULL;
1897
    }
1898
 
1899
  if (section_tdata->external_relocs == NULL)
1900
    {
1901
      bfd_size_type external_relocs_size;
1902
 
1903
      external_relocs_size = (ecoff_backend (abfd)->external_reloc_size
1904
                              * sec->reloc_count);
1905
 
1906
      section_tdata->external_relocs =
1907
        (PTR) bfd_alloc (abfd, external_relocs_size);
1908
      if (section_tdata->external_relocs == NULL && external_relocs_size != 0)
1909
        return false;
1910
 
1911
      if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
1912
          || (bfd_read (section_tdata->external_relocs, 1,
1913
                        external_relocs_size, abfd)
1914
              != external_relocs_size))
1915
        return false;
1916
    }
1917
 
1918
  return true;
1919
}
1920
 
1921
/* Relax a section when linking a MIPS ECOFF file.  This is used for
1922
   embedded PIC code, which always uses PC relative branches which
1923
   only have an 18 bit range on MIPS.  If a branch is not in range, we
1924
   generate a long instruction sequence to compensate.  Each time we
1925
   find a branch to expand, we have to check all the others again to
1926
   make sure they are still in range.  This is slow, but it only has
1927
   to be done when -relax is passed to the linker.
1928
 
1929
   This routine figures out which branches need to expand; the actual
1930
   expansion is done in mips_relocate_section when the section
1931
   contents are relocated.  The information is stored in the offsets
1932
   field of the ecoff_section_tdata structure.  An offset of 1 means
1933
   that the branch must be expanded into a multi-instruction PC
1934
   relative branch (such an offset will only occur for a PC relative
1935
   branch to an external symbol).  Any other offset must be a multiple
1936
   of four, and is the amount to change the branch by (such an offset
1937
   will only occur for a PC relative branch within the same section).
1938
 
1939
   We do not modify the section relocs or contents themselves so that
1940
   if memory usage becomes an issue we can discard them and read them
1941
   again.  The only information we must save in memory between this
1942
   routine and the mips_relocate_section routine is the table of
1943
   offsets.  */
1944
 
1945
static boolean
1946
mips_relax_section (abfd, sec, info, again)
1947
     bfd *abfd;
1948
     asection *sec;
1949
     struct bfd_link_info *info;
1950
     boolean *again;
1951
{
1952
  struct ecoff_section_tdata *section_tdata;
1953
  bfd_byte *contents = NULL;
1954
  long *offsets;
1955
  struct external_reloc *ext_rel;
1956
  struct external_reloc *ext_rel_end;
1957
  unsigned int i;
1958
 
1959
  /* Assume we are not going to need another pass.  */
1960
  *again = false;
1961
 
1962
  /* If we are not generating an ECOFF file, this is much too
1963
     confusing to deal with.  */
1964
  if (info->hash->creator->flavour != bfd_get_flavour (abfd))
1965
    return true;
1966
 
1967
  /* If there are no relocs, there is nothing to do.  */
1968
  if (sec->reloc_count == 0)
1969
    return true;
1970
 
1971
  /* We are only interested in PC relative relocs, and why would there
1972
     ever be one from anything but the .text section?  */
1973
  if (strcmp (bfd_get_section_name (abfd, sec), ".text") != 0)
1974
    return true;
1975
 
1976
  /* Read in the relocs, if we haven't already got them.  */
1977
  section_tdata = ecoff_section_data (abfd, sec);
1978
  if (section_tdata == (struct ecoff_section_tdata *) NULL
1979
      || section_tdata->external_relocs == NULL)
1980
    {
1981
      if (! mips_read_relocs (abfd, sec))
1982
        goto error_return;
1983
      section_tdata = ecoff_section_data (abfd, sec);
1984
    }
1985
 
1986
  if (sec->_cooked_size == 0)
1987
    {
1988
      /* We must initialize _cooked_size only the first time we are
1989
         called.  */
1990
      sec->_cooked_size = sec->_raw_size;
1991
    }
1992
 
1993
  contents = section_tdata->contents;
1994
  offsets = section_tdata->offsets;
1995
 
1996
  /* Look for any external PC relative relocs.  Internal PC relative
1997
     relocs are already correct in the object file, so they certainly
1998
     can not overflow.  */
1999
  ext_rel = (struct external_reloc *) section_tdata->external_relocs;
2000
  ext_rel_end = ext_rel + sec->reloc_count;
2001
  for (i = 0; ext_rel < ext_rel_end; ext_rel++, i++)
2002
    {
2003
      struct internal_reloc int_rel;
2004
      struct ecoff_link_hash_entry *h;
2005
      asection *hsec;
2006
      bfd_signed_vma relocation;
2007
      struct external_reloc *adj_ext_rel;
2008
      unsigned int adj_i;
2009
      unsigned long ext_count;
2010
      struct ecoff_link_hash_entry **adj_h_ptr;
2011
      struct ecoff_link_hash_entry **adj_h_ptr_end;
2012
      struct ecoff_value_adjust *adjust;
2013
 
2014
      /* If we have already expanded this reloc, we certainly don't
2015
         need to do it again.  */
2016
      if (offsets != (long *) NULL && offsets[i] == 1)
2017
        continue;
2018
 
2019
      /* Quickly check that this reloc is external PCREL16.  */
2020
      if (bfd_header_big_endian (abfd))
2021
        {
2022
          if ((ext_rel->r_bits[3] & RELOC_BITS3_EXTERN_BIG) == 0
2023
              || (((ext_rel->r_bits[3] & RELOC_BITS3_TYPE_BIG)
2024
                   >> RELOC_BITS3_TYPE_SH_BIG)
2025
                  != MIPS_R_PCREL16))
2026
            continue;
2027
        }
2028
      else
2029
        {
2030
          if ((ext_rel->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) == 0
2031
              || (((ext_rel->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
2032
                   >> RELOC_BITS3_TYPE_SH_LITTLE)
2033
                  != MIPS_R_PCREL16))
2034
            continue;
2035
        }
2036
 
2037
      mips_ecoff_swap_reloc_in (abfd, (PTR) ext_rel, &int_rel);
2038
 
2039
      h = ecoff_data (abfd)->sym_hashes[int_rel.r_symndx];
2040
      if (h == (struct ecoff_link_hash_entry *) NULL)
2041
        abort ();
2042
 
2043
      if (h->root.type != bfd_link_hash_defined
2044
          && h->root.type != bfd_link_hash_defweak)
2045
        {
2046
          /* Just ignore undefined symbols.  These will presumably
2047
             generate an error later in the link.  */
2048
          continue;
2049
        }
2050
 
2051
      /* Get the value of the symbol.  */
2052
      hsec = h->root.u.def.section;
2053
      relocation = (h->root.u.def.value
2054
                    + hsec->output_section->vma
2055
                    + hsec->output_offset);
2056
 
2057
      /* Subtract out the current address.  */
2058
      relocation -= (sec->output_section->vma
2059
                     + sec->output_offset
2060
                     + (int_rel.r_vaddr - sec->vma));
2061
 
2062
      /* The addend is stored in the object file.  In the normal case
2063
         of ``bal symbol'', the addend will be -4.  It will only be
2064
         different in the case of ``bal symbol+constant''.  To avoid
2065
         always reading in the section contents, we don't check the
2066
         addend in the object file (we could easily check the contents
2067
         if we happen to have already read them in, but I fear that
2068
         this could be confusing).  This means we will screw up if
2069
         there is a branch to a symbol that is in range, but added to
2070
         a constant which puts it out of range; in such a case the
2071
         link will fail with a reloc overflow error.  Since the
2072
         compiler will never generate such code, it should be easy
2073
         enough to work around it by changing the assembly code in the
2074
         source file.  */
2075
      relocation -= 4;
2076
 
2077
      /* Now RELOCATION is the number we want to put in the object
2078
         file.  See whether it fits.  */
2079
      if (relocation >= -0x20000 && relocation < 0x20000)
2080
        continue;
2081
 
2082
      /* Now that we know this reloc needs work, which will rarely
2083
         happen, go ahead and grab the section contents.  */
2084
      if (contents == (bfd_byte *) NULL)
2085
        {
2086
          if (info->keep_memory)
2087
            contents = (bfd_byte *) bfd_alloc (abfd, sec->_raw_size);
2088
          else
2089
            contents = (bfd_byte *) bfd_malloc ((size_t) sec->_raw_size);
2090
          if (contents == (bfd_byte *) NULL)
2091
            goto error_return;
2092
          if (! bfd_get_section_contents (abfd, sec, (PTR) contents,
2093
                                          (file_ptr) 0, sec->_raw_size))
2094
            goto error_return;
2095
          if (info->keep_memory)
2096
            section_tdata->contents = contents;
2097
        }
2098
 
2099
      /* We only support changing the bal instruction.  It would be
2100
         possible to handle other PC relative branches, but some of
2101
         them (the conditional branches) would require a different
2102
         length instruction sequence which would complicate both this
2103
         routine and mips_relax_pcrel16.  It could be written if
2104
         somebody felt it were important.  Ignoring this reloc will
2105
         presumably cause a reloc overflow error later on.  */
2106
      if (bfd_get_32 (abfd, contents + int_rel.r_vaddr - sec->vma)
2107
          != 0x0411ffff) /* bgezal $0,. == bal . */
2108
        continue;
2109
 
2110
      /* Bother.  We need to expand this reloc, and we will need to
2111
         make another relaxation pass since this change may put other
2112
         relocs out of range.  We need to examine the local branches
2113
         and we need to allocate memory to hold the offsets we must
2114
         add to them.  We also need to adjust the values of all
2115
         symbols in the object file following this location.  */
2116
 
2117
      sec->_cooked_size += PCREL16_EXPANSION_ADJUSTMENT;
2118
      *again = true;
2119
 
2120
      if (offsets == (long *) NULL)
2121
        {
2122
          size_t size;
2123
 
2124
          size = sec->reloc_count * sizeof (long);
2125
          offsets = (long *) bfd_alloc (abfd, size);
2126
          if (offsets == (long *) NULL)
2127
            goto error_return;
2128
          memset (offsets, 0, size);
2129
          section_tdata->offsets = offsets;
2130
        }
2131
 
2132
      offsets[i] = 1;
2133
 
2134
      /* Now look for all PC relative references that cross this reloc
2135
         and adjust their offsets.  */
2136
      adj_ext_rel = (struct external_reloc *) section_tdata->external_relocs;
2137
      for (adj_i = 0; adj_ext_rel < ext_rel_end; adj_ext_rel++, adj_i++)
2138
        {
2139
          struct internal_reloc adj_int_rel;
2140
          bfd_vma start, stop;
2141
          int change;
2142
 
2143
          mips_ecoff_swap_reloc_in (abfd, (PTR) adj_ext_rel, &adj_int_rel);
2144
 
2145
          if (adj_int_rel.r_type == MIPS_R_PCREL16)
2146
            {
2147
              unsigned long insn;
2148
 
2149
              /* We only care about local references.  External ones
2150
                 will be relocated correctly anyhow.  */
2151
              if (adj_int_rel.r_extern)
2152
                continue;
2153
 
2154
              /* We are only interested in a PC relative reloc within
2155
                 this section.  FIXME: Cross section PC relative
2156
                 relocs may not be handled correctly; does anybody
2157
                 care?  */
2158
              if (adj_int_rel.r_symndx != RELOC_SECTION_TEXT)
2159
                continue;
2160
 
2161
              start = adj_int_rel.r_vaddr;
2162
 
2163
              insn = bfd_get_32 (abfd,
2164
                                 contents + adj_int_rel.r_vaddr - sec->vma);
2165
 
2166
              stop = (insn & 0xffff) << 2;
2167
              if ((stop & 0x20000) != 0)
2168
                stop -= 0x40000;
2169
              stop += adj_int_rel.r_vaddr + 4;
2170
            }
2171
          else if (adj_int_rel.r_type == MIPS_R_RELHI)
2172
            {
2173
              struct internal_reloc rello;
2174
              long addhi, addlo;
2175
 
2176
              /* The next reloc must be MIPS_R_RELLO, and we handle
2177
                 them together.  */
2178
              BFD_ASSERT (adj_ext_rel + 1 < ext_rel_end);
2179
 
2180
              mips_ecoff_swap_reloc_in (abfd, (PTR) (adj_ext_rel + 1), &rello);
2181
 
2182
              BFD_ASSERT (rello.r_type == MIPS_R_RELLO);
2183
 
2184
              addhi = bfd_get_32 (abfd,
2185
                                   contents + adj_int_rel.r_vaddr - sec->vma);
2186
              addhi &= 0xffff;
2187
              if (addhi & 0x8000)
2188
                addhi -= 0x10000;
2189
              addhi <<= 16;
2190
 
2191
              addlo = bfd_get_32 (abfd, contents + rello.r_vaddr - sec->vma);
2192
              addlo &= 0xffff;
2193
              if (addlo & 0x8000)
2194
                addlo -= 0x10000;
2195
 
2196
              if (adj_int_rel.r_extern)
2197
                {
2198
                  /* The value we want here is
2199
                       sym - RELLOaddr + addend
2200
                     which we can express as
2201
                       sym - (RELLOaddr - addend)
2202
                     Therefore if we are expanding the area between
2203
                     RELLOaddr and RELLOaddr - addend we must adjust
2204
                     the addend.  This is admittedly ambiguous, since
2205
                     we might mean (sym + addend) - RELLOaddr, but in
2206
                     practice we don't, and there is no way to handle
2207
                     that case correctly since at this point we have
2208
                     no idea whether any reloc is being expanded
2209
                     between sym and sym + addend.  */
2210
                  start = rello.r_vaddr - (addhi + addlo);
2211
                  stop = rello.r_vaddr;
2212
                }
2213
              else
2214
                {
2215
                  /* An internal RELHI/RELLO pair represents the
2216
                     difference between two addresses, $LC0 - foo.
2217
                     The symndx value is actually the difference
2218
                     between the reloc address and $LC0.  This lets us
2219
                     compute $LC0, and, by considering the addend,
2220
                     foo.  If the reloc we are expanding falls between
2221
                     those two relocs, we must adjust the addend.  At
2222
                     this point, the symndx value is actually in the
2223
                     r_offset field, where it was put by
2224
                     mips_ecoff_swap_reloc_in.  */
2225
                  start = rello.r_vaddr - adj_int_rel.r_offset;
2226
                  stop = start + addhi + addlo;
2227
                }
2228
            }
2229
          else if (adj_int_rel.r_type == MIPS_R_SWITCH)
2230
            {
2231
              /* A MIPS_R_SWITCH reloc represents a word of the form
2232
                   .word $L3-$LS12
2233
                 The value in the object file is correct, assuming the
2234
                 original value of $L3.  The symndx value is actually
2235
                 the difference between the reloc address and $LS12.
2236
                 This lets us compute the original value of $LS12 as
2237
                   vaddr - symndx
2238
                 and the original value of $L3 as
2239
                   vaddr - symndx + addend
2240
                 where addend is the value from the object file.  At
2241
                 this point, the symndx value is actually found in the
2242
                 r_offset field, since it was moved by
2243
                 mips_ecoff_swap_reloc_in.  */
2244
              start = adj_int_rel.r_vaddr - adj_int_rel.r_offset;
2245
              stop = start + bfd_get_32 (abfd,
2246
                                         (contents
2247
                                          + adj_int_rel.r_vaddr
2248
                                          - sec->vma));
2249
            }
2250
          else
2251
            continue;
2252
 
2253
          /* If the range expressed by this reloc, which is the
2254
             distance between START and STOP crosses the reloc we are
2255
             expanding, we must adjust the offset.  The sign of the
2256
             adjustment depends upon the direction in which the range
2257
             crosses the reloc being expanded.  */
2258
          if (start <= int_rel.r_vaddr && stop > int_rel.r_vaddr)
2259
            change = PCREL16_EXPANSION_ADJUSTMENT;
2260
          else if (start > int_rel.r_vaddr && stop <= int_rel.r_vaddr)
2261
            change = - PCREL16_EXPANSION_ADJUSTMENT;
2262
          else
2263
            change = 0;
2264
 
2265
          offsets[adj_i] += change;
2266
 
2267
          if (adj_int_rel.r_type == MIPS_R_RELHI)
2268
            {
2269
              adj_ext_rel++;
2270
              adj_i++;
2271
              offsets[adj_i] += change;
2272
            }
2273
        }
2274
 
2275
      /* Find all symbols in this section defined by this object file
2276
         and adjust their values.  Note that we decide whether to
2277
         adjust the value based on the value stored in the ECOFF EXTR
2278
         structure, because the value stored in the hash table may
2279
         have been changed by an earlier expanded reloc and thus may
2280
         no longer correctly indicate whether the symbol is before or
2281
         after the expanded reloc.  */
2282
      ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
2283
      adj_h_ptr = ecoff_data (abfd)->sym_hashes;
2284
      adj_h_ptr_end = adj_h_ptr + ext_count;
2285
      for (; adj_h_ptr < adj_h_ptr_end; adj_h_ptr++)
2286
        {
2287
          struct ecoff_link_hash_entry *adj_h;
2288
 
2289
          adj_h = *adj_h_ptr;
2290
          if (adj_h != (struct ecoff_link_hash_entry *) NULL
2291
              && (adj_h->root.type == bfd_link_hash_defined
2292
                  || adj_h->root.type == bfd_link_hash_defweak)
2293
              && adj_h->root.u.def.section == sec
2294
              && adj_h->esym.asym.value > int_rel.r_vaddr)
2295
            adj_h->root.u.def.value += PCREL16_EXPANSION_ADJUSTMENT;
2296
        }
2297
 
2298
      /* Add an entry to the symbol value adjust list.  This is used
2299
         by bfd_ecoff_debug_accumulate to adjust the values of
2300
         internal symbols and FDR's.  */
2301
      adjust = ((struct ecoff_value_adjust *)
2302
                bfd_alloc (abfd, sizeof (struct ecoff_value_adjust)));
2303
      if (adjust == (struct ecoff_value_adjust *) NULL)
2304
        goto error_return;
2305
 
2306
      adjust->start = int_rel.r_vaddr;
2307
      adjust->end = sec->vma + sec->_raw_size;
2308
      adjust->adjust = PCREL16_EXPANSION_ADJUSTMENT;
2309
 
2310
      adjust->next = ecoff_data (abfd)->debug_info.adjust;
2311
      ecoff_data (abfd)->debug_info.adjust = adjust;
2312
    }
2313
 
2314
  if (contents != (bfd_byte *) NULL && ! info->keep_memory)
2315
    free (contents);
2316
 
2317
  return true;
2318
 
2319
 error_return:
2320
  if (contents != (bfd_byte *) NULL && ! info->keep_memory)
2321
    free (contents);
2322
  return false;
2323
}
2324
 
2325
/* This routine is called from mips_relocate_section when a PC
2326
   relative reloc must be expanded into the five instruction sequence.
2327
   It handles all the details of the expansion, including resolving
2328
   the reloc.  */
2329
 
2330
static boolean
2331
mips_relax_pcrel16 (info, input_bfd, input_section, h, location, address)
2332
     struct bfd_link_info *info ATTRIBUTE_UNUSED;
2333
     bfd *input_bfd;
2334
     asection *input_section ATTRIBUTE_UNUSED;
2335
     struct ecoff_link_hash_entry *h;
2336
     bfd_byte *location;
2337
     bfd_vma address;
2338
{
2339
  bfd_vma relocation;
2340
 
2341
  /* 0x0411ffff is bgezal $0,. == bal .  */
2342
  BFD_ASSERT (bfd_get_32 (input_bfd, location) == 0x0411ffff);
2343
 
2344
  /* We need to compute the distance between the symbol and the
2345
     current address plus eight.  */
2346
  relocation = (h->root.u.def.value
2347
                + h->root.u.def.section->output_section->vma
2348
                + h->root.u.def.section->output_offset);
2349
  relocation -= address + 8;
2350
 
2351
  /* If the lower half is negative, increment the upper 16 half.  */
2352
  if ((relocation & 0x8000) != 0)
2353
    relocation += 0x10000;
2354
 
2355
  bfd_put_32 (input_bfd, 0x04110001, location); /* bal .+8 */
2356
  bfd_put_32 (input_bfd,
2357
              0x3c010000 | ((relocation >> 16) & 0xffff), /* lui $at,XX */
2358
              location + 4);
2359
  bfd_put_32 (input_bfd,
2360
              0x24210000 | (relocation & 0xffff), /* addiu $at,$at,XX */
2361
              location + 8);
2362
  bfd_put_32 (input_bfd, 0x003f0821, location + 12); /* addu $at,$at,$ra */
2363
  bfd_put_32 (input_bfd, 0x0020f809, location + 16); /* jalr $at */
2364
 
2365
  return true;
2366
}
2367
 
2368
/* Given a .sdata section and a .rel.sdata in-memory section, store
2369
   relocation information into the .rel.sdata section which can be
2370
   used at runtime to relocate the section.  This is called by the
2371
   linker when the --embedded-relocs switch is used.  This is called
2372
   after the add_symbols entry point has been called for all the
2373
   objects, and before the final_link entry point is called.  This
2374
   function presumes that the object was compiled using
2375
   -membedded-pic.  */
2376
 
2377
boolean
2378
bfd_mips_ecoff_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2379
     bfd *abfd;
2380
     struct bfd_link_info *info;
2381
     asection *datasec;
2382
     asection *relsec;
2383
     char **errmsg;
2384
{
2385
  struct ecoff_link_hash_entry **sym_hashes;
2386
  struct ecoff_section_tdata *section_tdata;
2387
  struct external_reloc *ext_rel;
2388
  struct external_reloc *ext_rel_end;
2389
  bfd_byte *p;
2390
 
2391
  BFD_ASSERT (! info->relocateable);
2392
 
2393
  *errmsg = NULL;
2394
 
2395
  if (datasec->reloc_count == 0)
2396
    return true;
2397
 
2398
  sym_hashes = ecoff_data (abfd)->sym_hashes;
2399
 
2400
  if (! mips_read_relocs (abfd, datasec))
2401
    return false;
2402
 
2403
  relsec->contents = (bfd_byte *) bfd_alloc (abfd, datasec->reloc_count * 4);
2404
  if (relsec->contents == NULL)
2405
    return false;
2406
 
2407
  p = relsec->contents;
2408
 
2409
  section_tdata = ecoff_section_data (abfd, datasec);
2410
  ext_rel = (struct external_reloc *) section_tdata->external_relocs;
2411
  ext_rel_end = ext_rel + datasec->reloc_count;
2412
  for (; ext_rel < ext_rel_end; ext_rel++, p += 4)
2413
    {
2414
      struct internal_reloc int_rel;
2415
      boolean text_relative;
2416
 
2417
      mips_ecoff_swap_reloc_in (abfd, (PTR) ext_rel, &int_rel);
2418
 
2419
      /* We are going to write a four byte word into the runtime reloc
2420
         section.  The word will be the address in the data section
2421
         which must be relocated.  This must be on a word boundary,
2422
         which means the lower two bits must be zero.  We use the
2423
         least significant bit to indicate how the value in the data
2424
         section must be relocated.  A 0 means that the value is
2425
         relative to the text section, while a 1 indicates that the
2426
         value is relative to the data section.  Given that we are
2427
         assuming the code was compiled using -membedded-pic, there
2428
         should not be any other possibilities.  */
2429
 
2430
      /* We can only relocate REFWORD relocs at run time.  */
2431
      if (int_rel.r_type != MIPS_R_REFWORD)
2432
        {
2433
          *errmsg = _("unsupported reloc type");
2434
          bfd_set_error (bfd_error_bad_value);
2435
          return false;
2436
        }
2437
 
2438
      if (int_rel.r_extern)
2439
        {
2440
          struct ecoff_link_hash_entry *h;
2441
 
2442
          h = sym_hashes[int_rel.r_symndx];
2443
          /* If h is NULL, that means that there is a reloc against an
2444
             external symbol which we thought was just a debugging
2445
             symbol.  This should not happen.  */
2446
          if (h == (struct ecoff_link_hash_entry *) NULL)
2447
            abort ();
2448
          if ((h->root.type == bfd_link_hash_defined
2449
               || h->root.type == bfd_link_hash_defweak)
2450
              && (h->root.u.def.section->flags & SEC_CODE) != 0)
2451
            text_relative = true;
2452
          else
2453
            text_relative = false;
2454
        }
2455
      else
2456
        {
2457
          switch (int_rel.r_symndx)
2458
            {
2459
            case RELOC_SECTION_TEXT:
2460
              text_relative = true;
2461
              break;
2462
            case RELOC_SECTION_SDATA:
2463
            case RELOC_SECTION_SBSS:
2464
            case RELOC_SECTION_LIT8:
2465
              text_relative = false;
2466
              break;
2467
            default:
2468
              /* No other sections should appear in -membedded-pic
2469
                 code.  */
2470
              *errmsg = _("reloc against unsupported section");
2471
              bfd_set_error (bfd_error_bad_value);
2472
              return false;
2473
            }
2474
        }
2475
 
2476
      if ((int_rel.r_offset & 3) != 0)
2477
        {
2478
          *errmsg = _("reloc not properly aligned");
2479
          bfd_set_error (bfd_error_bad_value);
2480
          return false;
2481
        }
2482
 
2483
      bfd_put_32 (abfd,
2484
                  (int_rel.r_vaddr - datasec->vma + datasec->output_offset
2485
                   + (text_relative ? 0 : 1)),
2486
                  p);
2487
    }
2488
 
2489
  return true;
2490
}
2491
 
2492
/* This is the ECOFF backend structure.  The backend field of the
2493
   target vector points to this.  */
2494
 
2495
static const struct ecoff_backend_data mips_ecoff_backend_data =
2496
{
2497
  /* COFF backend structure.  */
2498
  {
2499
    (void (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR))) bfd_void, /* aux_in */
2500
    (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
2501
    (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
2502
    (unsigned (*) PARAMS ((bfd *,PTR,int,int,int,int,PTR)))bfd_void,/*aux_out*/
2503
    (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
2504
    (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
2505
    (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* reloc_out */
2506
    mips_ecoff_swap_filehdr_out, mips_ecoff_swap_aouthdr_out,
2507
    mips_ecoff_swap_scnhdr_out,
2508
    FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, true, false, 4,
2509
    mips_ecoff_swap_filehdr_in, mips_ecoff_swap_aouthdr_in,
2510
    mips_ecoff_swap_scnhdr_in, NULL,
2511
    mips_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook,
2512
    _bfd_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags,
2513
    _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table,
2514
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2515
    NULL, NULL
2516
  },
2517
  /* Supported architecture.  */
2518
  bfd_arch_mips,
2519
  /* Initial portion of armap string.  */
2520
  "__________",
2521
  /* The page boundary used to align sections in a demand-paged
2522
     executable file.  E.g., 0x1000.  */
2523
  0x1000,
2524
  /* True if the .rdata section is part of the text segment, as on the
2525
     Alpha.  False if .rdata is part of the data segment, as on the
2526
     MIPS.  */
2527
  false,
2528
  /* Bitsize of constructor entries.  */
2529
  32,
2530
  /* Reloc to use for constructor entries.  */
2531
  &mips_howto_table[MIPS_R_REFWORD],
2532
  {
2533
    /* Symbol table magic number.  */
2534
    magicSym,
2535
    /* Alignment of debugging information.  E.g., 4.  */
2536
    4,
2537
    /* Sizes of external symbolic information.  */
2538
    sizeof (struct hdr_ext),
2539
    sizeof (struct dnr_ext),
2540
    sizeof (struct pdr_ext),
2541
    sizeof (struct sym_ext),
2542
    sizeof (struct opt_ext),
2543
    sizeof (struct fdr_ext),
2544
    sizeof (struct rfd_ext),
2545
    sizeof (struct ext_ext),
2546
    /* Functions to swap in external symbolic data.  */
2547
    ecoff_swap_hdr_in,
2548
    ecoff_swap_dnr_in,
2549
    ecoff_swap_pdr_in,
2550
    ecoff_swap_sym_in,
2551
    ecoff_swap_opt_in,
2552
    ecoff_swap_fdr_in,
2553
    ecoff_swap_rfd_in,
2554
    ecoff_swap_ext_in,
2555
    _bfd_ecoff_swap_tir_in,
2556
    _bfd_ecoff_swap_rndx_in,
2557
    /* Functions to swap out external symbolic data.  */
2558
    ecoff_swap_hdr_out,
2559
    ecoff_swap_dnr_out,
2560
    ecoff_swap_pdr_out,
2561
    ecoff_swap_sym_out,
2562
    ecoff_swap_opt_out,
2563
    ecoff_swap_fdr_out,
2564
    ecoff_swap_rfd_out,
2565
    ecoff_swap_ext_out,
2566
    _bfd_ecoff_swap_tir_out,
2567
    _bfd_ecoff_swap_rndx_out,
2568
    /* Function to read in symbolic data.  */
2569
    _bfd_ecoff_slurp_symbolic_info
2570
  },
2571
  /* External reloc size.  */
2572
  RELSZ,
2573
  /* Reloc swapping functions.  */
2574
  mips_ecoff_swap_reloc_in,
2575
  mips_ecoff_swap_reloc_out,
2576
  /* Backend reloc tweaking.  */
2577
  mips_adjust_reloc_in,
2578
  mips_adjust_reloc_out,
2579
  /* Relocate section contents while linking.  */
2580
  mips_relocate_section,
2581
  /* Do final adjustments to filehdr and aouthdr.  */
2582
  NULL,
2583
  /* Read an element from an archive at a given file position.  */
2584
  _bfd_get_elt_at_filepos
2585
};
2586
 
2587
/* Looking up a reloc type is MIPS specific.  */
2588
#define _bfd_ecoff_bfd_reloc_type_lookup mips_bfd_reloc_type_lookup
2589
 
2590
/* Getting relocated section contents is generic.  */
2591
#define _bfd_ecoff_bfd_get_relocated_section_contents \
2592
  bfd_generic_get_relocated_section_contents
2593
 
2594
/* Handling file windows is generic.  */
2595
#define _bfd_ecoff_get_section_contents_in_window \
2596
  _bfd_generic_get_section_contents_in_window
2597
 
2598
/* Relaxing sections is MIPS specific.  */
2599
#define _bfd_ecoff_bfd_relax_section mips_relax_section
2600
 
2601
/* GC of sections is not done.  */
2602
#define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections
2603
 
2604
extern const bfd_target ecoff_big_vec;
2605
 
2606
const bfd_target ecoff_little_vec =
2607
{
2608
  "ecoff-littlemips",           /* name */
2609
  bfd_target_ecoff_flavour,
2610
  BFD_ENDIAN_LITTLE,            /* data byte order is little */
2611
  BFD_ENDIAN_LITTLE,            /* header byte order is little */
2612
 
2613
  (HAS_RELOC | EXEC_P |         /* object flags */
2614
   HAS_LINENO | HAS_DEBUG |
2615
   HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2616
 
2617
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
2618
  0,                             /* leading underscore */
2619
  ' ',                          /* ar_pad_char */
2620
  15,                           /* ar_max_namelen */
2621
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2622
     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2623
     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2624
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2625
     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2626
     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2627
 
2628
  {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2629
     _bfd_ecoff_archive_p, _bfd_dummy_target},
2630
  {bfd_false, _bfd_ecoff_mkobject,  /* bfd_set_format */
2631
     _bfd_generic_mkarchive, bfd_false},
2632
  {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
2633
     _bfd_write_archive_contents, bfd_false},
2634
 
2635
     BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
2636
     BFD_JUMP_TABLE_COPY (_bfd_ecoff),
2637
     BFD_JUMP_TABLE_CORE (_bfd_nocore),
2638
     BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
2639
     BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
2640
     BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
2641
     BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
2642
     BFD_JUMP_TABLE_LINK (_bfd_ecoff),
2643
     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2644
 
2645
  & ecoff_big_vec,
2646
 
2647
  (PTR) &mips_ecoff_backend_data
2648
};
2649
 
2650
const bfd_target ecoff_big_vec =
2651
{
2652
  "ecoff-bigmips",              /* name */
2653
  bfd_target_ecoff_flavour,
2654
  BFD_ENDIAN_BIG,               /* data byte order is big */
2655
  BFD_ENDIAN_BIG,               /* header byte order is big */
2656
 
2657
  (HAS_RELOC | EXEC_P |         /* object flags */
2658
   HAS_LINENO | HAS_DEBUG |
2659
   HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2660
 
2661
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
2662
  0,                             /* leading underscore */
2663
  ' ',                          /* ar_pad_char */
2664
  15,                           /* ar_max_namelen */
2665
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2666
     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2667
     bfd_getb16, bfd_getb_signed_16, bfd_putb16,
2668
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2669
     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2670
     bfd_getb16, bfd_getb_signed_16, bfd_putb16,
2671
 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2672
    _bfd_ecoff_archive_p, _bfd_dummy_target},
2673
 {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
2674
    _bfd_generic_mkarchive, bfd_false},
2675
 {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
2676
    _bfd_write_archive_contents, bfd_false},
2677
 
2678
     BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
2679
     BFD_JUMP_TABLE_COPY (_bfd_ecoff),
2680
     BFD_JUMP_TABLE_CORE (_bfd_nocore),
2681
     BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
2682
     BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
2683
     BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
2684
     BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
2685
     BFD_JUMP_TABLE_LINK (_bfd_ecoff),
2686
     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2687
 
2688
  & ecoff_little_vec,
2689
 
2690
  (PTR) &mips_ecoff_backend_data
2691
};
2692
 
2693
const bfd_target ecoff_biglittle_vec =
2694
{
2695
  "ecoff-biglittlemips",                /* name */
2696
  bfd_target_ecoff_flavour,
2697
  BFD_ENDIAN_LITTLE,            /* data byte order is little */
2698
  BFD_ENDIAN_BIG,               /* header byte order is big */
2699
 
2700
  (HAS_RELOC | EXEC_P |         /* object flags */
2701
   HAS_LINENO | HAS_DEBUG |
2702
   HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2703
 
2704
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
2705
  0,                             /* leading underscore */
2706
  ' ',                          /* ar_pad_char */
2707
  15,                           /* ar_max_namelen */
2708
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2709
     bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2710
     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2711
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2712
     bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2713
     bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2714
 
2715
  {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2716
     _bfd_ecoff_archive_p, _bfd_dummy_target},
2717
  {bfd_false, _bfd_ecoff_mkobject,  /* bfd_set_format */
2718
     _bfd_generic_mkarchive, bfd_false},
2719
  {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
2720
     _bfd_write_archive_contents, bfd_false},
2721
 
2722
     BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
2723
     BFD_JUMP_TABLE_COPY (_bfd_ecoff),
2724
     BFD_JUMP_TABLE_CORE (_bfd_nocore),
2725
     BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
2726
     BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
2727
     BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
2728
     BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
2729
     BFD_JUMP_TABLE_LINK (_bfd_ecoff),
2730
     BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2731
 
2732
  NULL,
2733
 
2734
  (PTR) &mips_ecoff_backend_data
2735
};

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.