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