* elf32-mips.c, elfn32-mips.c, elf64-mips.c: Convert prototypes.
[external/binutils.git] / bfd / elf32-mips.c
1 /* MIPS-specific support for 32-bit ELF
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003 Free Software Foundation, Inc.
4
5    Most of the information added by Ian Lance Taylor, Cygnus Support,
6    <ian@cygnus.com>.
7    N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
8    <mark@codesourcery.com>
9    Traditional MIPS targets support added by Koundinya.K, Dansk Data
10    Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11
12 This file is part of BFD, the Binary File Descriptor library.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
27
28 /* This file handles MIPS ELF targets.  SGI Irix 5 uses a slightly
29    different MIPS ELF from other targets.  This matters when linking.
30    This file supports both, switching at runtime.  */
31
32 #include "bfd.h"
33 #include "sysdep.h"
34 #include "libbfd.h"
35 #include "bfdlink.h"
36 #include "genlink.h"
37 #include "elf-bfd.h"
38 #include "elfxx-mips.h"
39 #include "elf/mips.h"
40
41 /* Get the ECOFF swapping routines.  */
42 #include "coff/sym.h"
43 #include "coff/symconst.h"
44 #include "coff/internal.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
47 #define ECOFF_SIGNED_32
48 #include "ecoffswap.h"
49
50 static bfd_reloc_status_type mips_elf_generic_reloc
51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type mips_elf_hi16_reloc
53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type mips_elf_lo16_reloc
55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type mips_elf_got16_reloc
57   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type gprel32_with_gp
59   (bfd *, asymbol *, arelent *, asection *, bfd_boolean, void *, bfd_vma);
60 static bfd_reloc_status_type mips_elf_gprel32_reloc
61   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type mips32_64bit_reloc
63   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
65   (bfd *, bfd_reloc_code_real_type);
66 static reloc_howto_type *mips_elf32_rtype_to_howto
67   (unsigned int, bfd_boolean);
68 static void mips_info_to_howto_rel
69   (bfd *, arelent *, Elf_Internal_Rela *);
70 static void mips_info_to_howto_rela
71   (bfd *, arelent *, Elf_Internal_Rela *);
72 static bfd_boolean mips_elf_sym_is_global
73   (bfd *, asymbol *);
74 static bfd_boolean mips_elf32_object_p
75   (bfd *);
76 static bfd_boolean mips_elf_is_local_label_name
77   (bfd *, const char *);
78 static bfd_reloc_status_type mips16_jump_reloc
79   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
80 static bfd_reloc_status_type mips16_gprel_reloc
81   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
82 static bfd_reloc_status_type mips_elf_final_gp
83   (bfd *, asymbol *, bfd_boolean, char **, bfd_vma *);
84 static bfd_boolean mips_elf_assign_gp
85   (bfd *, bfd_vma *);
86 static bfd_boolean elf32_mips_grok_prstatus
87   (bfd *, Elf_Internal_Note *);
88 static bfd_boolean elf32_mips_grok_psinfo
89   (bfd *, Elf_Internal_Note *);
90 static irix_compat_t elf32_mips_irix_compat
91   (bfd *);
92
93 extern const bfd_target bfd_elf32_bigmips_vec;
94 extern const bfd_target bfd_elf32_littlemips_vec;
95
96 /* Nonzero if ABFD is using the N32 ABI.  */
97 #define ABI_N32_P(abfd) \
98   ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
99
100 /* Whether we are trying to be compatible with IRIX at all.  */
101 #define SGI_COMPAT(abfd) \
102   (elf32_mips_irix_compat (abfd) != ict_none)
103
104 /* The number of local .got entries we reserve.  */
105 #define MIPS_RESERVED_GOTNO (2)
106
107 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
108    from smaller values.  Start with zero, widen, *then* decrement.  */
109 #define MINUS_ONE       (((bfd_vma)0) - 1)
110
111 /* The relocation table used for SHT_REL sections.  */
112
113 static reloc_howto_type elf_mips_howto_table_rel[] =
114 {
115   /* No relocation.  */
116   HOWTO (R_MIPS_NONE,           /* type */
117          0,                     /* rightshift */
118          0,                     /* size (0 = byte, 1 = short, 2 = long) */
119          0,                     /* bitsize */
120          FALSE,                 /* pc_relative */
121          0,                     /* bitpos */
122          complain_overflow_dont, /* complain_on_overflow */
123          mips_elf_generic_reloc, /* special_function */
124          "R_MIPS_NONE",         /* name */
125          FALSE,                 /* partial_inplace */
126          0,                     /* src_mask */
127          0,                     /* dst_mask */
128          FALSE),                /* pcrel_offset */
129
130   /* 16 bit relocation.  */
131   HOWTO (R_MIPS_16,             /* type */
132          0,                     /* rightshift */
133          2,                     /* size (0 = byte, 1 = short, 2 = long) */
134          16,                    /* bitsize */
135          FALSE,                 /* pc_relative */
136          0,                     /* bitpos */
137          complain_overflow_signed, /* complain_on_overflow */
138          mips_elf_generic_reloc, /* special_function */
139          "R_MIPS_16",           /* name */
140          TRUE,                  /* partial_inplace */
141          0x0000ffff,            /* src_mask */
142          0x0000ffff,            /* dst_mask */
143          FALSE),                /* pcrel_offset */
144
145   /* 32 bit relocation.  */
146   HOWTO (R_MIPS_32,             /* type */
147          0,                     /* rightshift */
148          2,                     /* size (0 = byte, 1 = short, 2 = long) */
149          32,                    /* bitsize */
150          FALSE,                 /* pc_relative */
151          0,                     /* bitpos */
152          complain_overflow_dont, /* complain_on_overflow */
153          mips_elf_generic_reloc, /* special_function */
154          "R_MIPS_32",           /* name */
155          TRUE,                  /* partial_inplace */
156          0xffffffff,            /* src_mask */
157          0xffffffff,            /* dst_mask */
158          FALSE),                /* pcrel_offset */
159
160   /* 32 bit symbol relative relocation.  */
161   HOWTO (R_MIPS_REL32,          /* type */
162          0,                     /* rightshift */
163          2,                     /* size (0 = byte, 1 = short, 2 = long) */
164          32,                    /* bitsize */
165          FALSE,                 /* pc_relative */
166          0,                     /* bitpos */
167          complain_overflow_dont, /* complain_on_overflow */
168          mips_elf_generic_reloc, /* special_function */
169          "R_MIPS_REL32",        /* name */
170          TRUE,                  /* partial_inplace */
171          0xffffffff,            /* src_mask */
172          0xffffffff,            /* dst_mask */
173          FALSE),                /* pcrel_offset */
174
175   /* 26 bit jump address.  */
176   HOWTO (R_MIPS_26,             /* type */
177          2,                     /* rightshift */
178          2,                     /* size (0 = byte, 1 = short, 2 = long) */
179          26,                    /* bitsize */
180          FALSE,                 /* pc_relative */
181          0,                     /* bitpos */
182          complain_overflow_dont, /* complain_on_overflow */
183                                 /* This needs complex overflow
184                                    detection, because the upper four
185                                    bits must match the PC + 4.  */
186          mips_elf_generic_reloc, /* special_function */
187          "R_MIPS_26",           /* name */
188          TRUE,                  /* partial_inplace */
189          0x03ffffff,            /* src_mask */
190          0x03ffffff,            /* dst_mask */
191          FALSE),                /* pcrel_offset */
192
193   /* High 16 bits of symbol value.  */
194   HOWTO (R_MIPS_HI16,           /* type */
195          0,                     /* rightshift */
196          2,                     /* size (0 = byte, 1 = short, 2 = long) */
197          16,                    /* bitsize */
198          FALSE,                 /* pc_relative */
199          0,                     /* bitpos */
200          complain_overflow_dont, /* complain_on_overflow */
201          mips_elf_hi16_reloc,   /* special_function */
202          "R_MIPS_HI16",         /* name */
203          TRUE,                  /* partial_inplace */
204          0x0000ffff,            /* src_mask */
205          0x0000ffff,            /* dst_mask */
206          FALSE),                /* pcrel_offset */
207
208   /* Low 16 bits of symbol value.  */
209   HOWTO (R_MIPS_LO16,           /* type */
210          0,                     /* rightshift */
211          2,                     /* size (0 = byte, 1 = short, 2 = long) */
212          16,                    /* bitsize */
213          FALSE,                 /* pc_relative */
214          0,                     /* bitpos */
215          complain_overflow_dont, /* complain_on_overflow */
216          mips_elf_lo16_reloc,   /* special_function */
217          "R_MIPS_LO16",         /* name */
218          TRUE,                  /* partial_inplace */
219          0x0000ffff,            /* src_mask */
220          0x0000ffff,            /* dst_mask */
221          FALSE),                /* pcrel_offset */
222
223   /* GP relative reference.  */
224   HOWTO (R_MIPS_GPREL16,        /* type */
225          0,                     /* rightshift */
226          2,                     /* size (0 = byte, 1 = short, 2 = long) */
227          16,                    /* bitsize */
228          FALSE,                 /* pc_relative */
229          0,                     /* bitpos */
230          complain_overflow_signed, /* complain_on_overflow */
231          _bfd_mips_elf32_gprel16_reloc, /* special_function */
232          "R_MIPS_GPREL16",      /* name */
233          TRUE,                  /* partial_inplace */
234          0x0000ffff,            /* src_mask */
235          0x0000ffff,            /* dst_mask */
236          FALSE),                /* pcrel_offset */
237
238   /* Reference to literal section.  */
239   HOWTO (R_MIPS_LITERAL,        /* type */
240          0,                     /* rightshift */
241          2,                     /* size (0 = byte, 1 = short, 2 = long) */
242          16,                    /* bitsize */
243          FALSE,                 /* pc_relative */
244          0,                     /* bitpos */
245          complain_overflow_signed, /* complain_on_overflow */
246          _bfd_mips_elf32_gprel16_reloc, /* special_function */
247          "R_MIPS_LITERAL",      /* name */
248          TRUE,                  /* partial_inplace */
249          0x0000ffff,            /* src_mask */
250          0x0000ffff,            /* dst_mask */
251          FALSE),                /* pcrel_offset */
252
253   /* Reference to global offset table.  */
254   HOWTO (R_MIPS_GOT16,          /* type */
255          0,                     /* rightshift */
256          2,                     /* size (0 = byte, 1 = short, 2 = long) */
257          16,                    /* bitsize */
258          FALSE,                 /* pc_relative */
259          0,                     /* bitpos */
260          complain_overflow_signed, /* complain_on_overflow */
261          mips_elf_got16_reloc,  /* special_function */
262          "R_MIPS_GOT16",        /* name */
263          TRUE,                  /* partial_inplace */
264          0x0000ffff,            /* src_mask */
265          0x0000ffff,            /* dst_mask */
266          FALSE),                /* pcrel_offset */
267
268   /* 16 bit PC relative reference.  */
269   HOWTO (R_MIPS_PC16,           /* type */
270          0,                     /* rightshift */
271          2,                     /* size (0 = byte, 1 = short, 2 = long) */
272          16,                    /* bitsize */
273          TRUE,                  /* pc_relative */
274          0,                     /* bitpos */
275          complain_overflow_signed, /* complain_on_overflow */
276          mips_elf_generic_reloc, /* special_function */
277          "R_MIPS_PC16",         /* name */
278          TRUE,                  /* partial_inplace */
279          0x0000ffff,            /* src_mask */
280          0x0000ffff,            /* dst_mask */
281          TRUE),                 /* pcrel_offset */
282
283   /* 16 bit call through global offset table.  */
284   HOWTO (R_MIPS_CALL16,         /* type */
285          0,                     /* rightshift */
286          2,                     /* size (0 = byte, 1 = short, 2 = long) */
287          16,                    /* bitsize */
288          FALSE,                 /* pc_relative */
289          0,                     /* bitpos */
290          complain_overflow_signed, /* complain_on_overflow */
291          mips_elf_generic_reloc, /* special_function */
292          "R_MIPS_CALL16",       /* name */
293          TRUE,                  /* partial_inplace */
294          0x0000ffff,            /* src_mask */
295          0x0000ffff,            /* dst_mask */
296          FALSE),                /* pcrel_offset */
297
298   /* 32 bit GP relative reference.  */
299   HOWTO (R_MIPS_GPREL32,        /* type */
300          0,                     /* rightshift */
301          2,                     /* size (0 = byte, 1 = short, 2 = long) */
302          32,                    /* bitsize */
303          FALSE,                 /* pc_relative */
304          0,                     /* bitpos */
305          complain_overflow_dont, /* complain_on_overflow */
306          mips_elf_gprel32_reloc, /* special_function */
307          "R_MIPS_GPREL32",      /* name */
308          TRUE,                  /* partial_inplace */
309          0xffffffff,            /* src_mask */
310          0xffffffff,            /* dst_mask */
311          FALSE),                /* pcrel_offset */
312
313   /* The remaining relocs are defined on Irix 5, although they are
314      not defined by the ABI.  */
315   EMPTY_HOWTO (13),
316   EMPTY_HOWTO (14),
317   EMPTY_HOWTO (15),
318
319   /* A 5 bit shift field.  */
320   HOWTO (R_MIPS_SHIFT5,         /* type */
321          0,                     /* rightshift */
322          2,                     /* size (0 = byte, 1 = short, 2 = long) */
323          5,                     /* bitsize */
324          FALSE,                 /* pc_relative */
325          6,                     /* bitpos */
326          complain_overflow_bitfield, /* complain_on_overflow */
327          mips_elf_generic_reloc, /* special_function */
328          "R_MIPS_SHIFT5",       /* name */
329          TRUE,                  /* partial_inplace */
330          0x000007c0,            /* src_mask */
331          0x000007c0,            /* dst_mask */
332          FALSE),                /* pcrel_offset */
333
334   /* A 6 bit shift field.  */
335   /* FIXME: This is not handled correctly; a special function is
336      needed to put the most significant bit in the right place.  */
337   HOWTO (R_MIPS_SHIFT6,         /* type */
338          0,                     /* rightshift */
339          2,                     /* size (0 = byte, 1 = short, 2 = long) */
340          6,                     /* bitsize */
341          FALSE,                 /* pc_relative */
342          6,                     /* bitpos */
343          complain_overflow_bitfield, /* complain_on_overflow */
344          mips_elf_generic_reloc, /* special_function */
345          "R_MIPS_SHIFT6",       /* name */
346          TRUE,                  /* partial_inplace */
347          0x000007c4,            /* src_mask */
348          0x000007c4,            /* dst_mask */
349          FALSE),                /* pcrel_offset */
350
351   /* A 64 bit relocation.  */
352   HOWTO (R_MIPS_64,             /* type */
353          0,                     /* rightshift */
354          4,                     /* size (0 = byte, 1 = short, 2 = long) */
355          64,                    /* bitsize */
356          FALSE,                 /* pc_relative */
357          0,                     /* bitpos */
358          complain_overflow_dont, /* complain_on_overflow */
359          mips32_64bit_reloc,    /* special_function */
360          "R_MIPS_64",           /* name */
361          TRUE,                  /* partial_inplace */
362          MINUS_ONE,             /* src_mask */
363          MINUS_ONE,             /* dst_mask */
364          FALSE),                /* pcrel_offset */
365
366   /* Displacement in the global offset table.  */
367   HOWTO (R_MIPS_GOT_DISP,       /* type */
368          0,                     /* rightshift */
369          2,                     /* size (0 = byte, 1 = short, 2 = long) */
370          16,                    /* bitsize */
371          FALSE,                 /* pc_relative */
372          0,                     /* bitpos */
373          complain_overflow_signed, /* complain_on_overflow */
374          mips_elf_generic_reloc, /* special_function */
375          "R_MIPS_GOT_DISP",     /* name */
376          TRUE,                  /* partial_inplace */
377          0x0000ffff,            /* src_mask */
378          0x0000ffff,            /* dst_mask */
379          FALSE),                /* pcrel_offset */
380
381   /* Displacement to page pointer in the global offset table.  */
382   HOWTO (R_MIPS_GOT_PAGE,       /* type */
383          0,                     /* rightshift */
384          2,                     /* size (0 = byte, 1 = short, 2 = long) */
385          16,                    /* bitsize */
386          FALSE,                 /* pc_relative */
387          0,                     /* bitpos */
388          complain_overflow_signed, /* complain_on_overflow */
389          mips_elf_generic_reloc, /* special_function */
390          "R_MIPS_GOT_PAGE",     /* name */
391          TRUE,                  /* partial_inplace */
392          0x0000ffff,            /* src_mask */
393          0x0000ffff,            /* dst_mask */
394          FALSE),                /* pcrel_offset */
395
396   /* Offset from page pointer in the global offset table.  */
397   HOWTO (R_MIPS_GOT_OFST,       /* type */
398          0,                     /* rightshift */
399          2,                     /* size (0 = byte, 1 = short, 2 = long) */
400          16,                    /* bitsize */
401          FALSE,                 /* pc_relative */
402          0,                     /* bitpos */
403          complain_overflow_signed, /* complain_on_overflow */
404          mips_elf_generic_reloc, /* special_function */
405          "R_MIPS_GOT_OFST",     /* name */
406          TRUE,                  /* partial_inplace */
407          0x0000ffff,            /* src_mask */
408          0x0000ffff,            /* dst_mask */
409          FALSE),                /* pcrel_offset */
410
411   /* High 16 bits of displacement in global offset table.  */
412   HOWTO (R_MIPS_GOT_HI16,       /* type */
413          0,                     /* rightshift */
414          2,                     /* size (0 = byte, 1 = short, 2 = long) */
415          16,                    /* bitsize */
416          FALSE,                 /* pc_relative */
417          0,                     /* bitpos */
418          complain_overflow_dont, /* complain_on_overflow */
419          mips_elf_generic_reloc, /* special_function */
420          "R_MIPS_GOT_HI16",     /* name */
421          TRUE,                  /* partial_inplace */
422          0x0000ffff,            /* src_mask */
423          0x0000ffff,            /* dst_mask */
424          FALSE),                /* pcrel_offset */
425
426   /* Low 16 bits of displacement in global offset table.  */
427   HOWTO (R_MIPS_GOT_LO16,       /* type */
428          0,                     /* rightshift */
429          2,                     /* size (0 = byte, 1 = short, 2 = long) */
430          16,                    /* bitsize */
431          FALSE,                 /* pc_relative */
432          0,                     /* bitpos */
433          complain_overflow_dont, /* complain_on_overflow */
434          mips_elf_generic_reloc, /* special_function */
435          "R_MIPS_GOT_LO16",     /* name */
436          TRUE,                  /* partial_inplace */
437          0x0000ffff,            /* src_mask */
438          0x0000ffff,            /* dst_mask */
439          FALSE),                /* pcrel_offset */
440
441   /* 64 bit subtraction.  Used in the N32 ABI.  */
442   HOWTO (R_MIPS_SUB,            /* type */
443          0,                     /* rightshift */
444          4,                     /* size (0 = byte, 1 = short, 2 = long) */
445          64,                    /* bitsize */
446          FALSE,                 /* pc_relative */
447          0,                     /* bitpos */
448          complain_overflow_dont, /* complain_on_overflow */
449          mips_elf_generic_reloc, /* special_function */
450          "R_MIPS_SUB",          /* name */
451          TRUE,                  /* partial_inplace */
452          MINUS_ONE,             /* src_mask */
453          MINUS_ONE,             /* dst_mask */
454          FALSE),                /* pcrel_offset */
455
456   /* Used to cause the linker to insert and delete instructions?  */
457   EMPTY_HOWTO (R_MIPS_INSERT_A),
458   EMPTY_HOWTO (R_MIPS_INSERT_B),
459   EMPTY_HOWTO (R_MIPS_DELETE),
460
461   /* Get the higher value of a 64 bit addend.  */
462   HOWTO (R_MIPS_HIGHER,         /* type */
463          0,                     /* rightshift */
464          2,                     /* size (0 = byte, 1 = short, 2 = long) */
465          16,                    /* bitsize */
466          FALSE,                 /* pc_relative */
467          0,                     /* bitpos */
468          complain_overflow_dont, /* complain_on_overflow */
469          mips_elf_generic_reloc, /* special_function */
470          "R_MIPS_HIGHER",       /* name */
471          TRUE,                  /* partial_inplace */
472          0x0000ffff,            /* src_mask */
473          0x0000ffff,            /* dst_mask */
474          FALSE),                /* pcrel_offset */
475
476   /* Get the highest value of a 64 bit addend.  */
477   HOWTO (R_MIPS_HIGHEST,        /* type */
478          0,                     /* rightshift */
479          2,                     /* size (0 = byte, 1 = short, 2 = long) */
480          16,                    /* bitsize */
481          FALSE,                 /* pc_relative */
482          0,                     /* bitpos */
483          complain_overflow_dont, /* complain_on_overflow */
484          mips_elf_generic_reloc, /* special_function */
485          "R_MIPS_HIGHEST",      /* name */
486          TRUE,                  /* partial_inplace */
487          0x0000ffff,            /* src_mask */
488          0x0000ffff,            /* dst_mask */
489          FALSE),                /* pcrel_offset */
490
491   /* High 16 bits of displacement in global offset table.  */
492   HOWTO (R_MIPS_CALL_HI16,      /* type */
493          0,                     /* rightshift */
494          2,                     /* size (0 = byte, 1 = short, 2 = long) */
495          16,                    /* bitsize */
496          FALSE,                 /* pc_relative */
497          0,                     /* bitpos */
498          complain_overflow_dont, /* complain_on_overflow */
499          mips_elf_generic_reloc, /* special_function */
500          "R_MIPS_CALL_HI16",    /* name */
501          TRUE,                  /* partial_inplace */
502          0x0000ffff,            /* src_mask */
503          0x0000ffff,            /* dst_mask */
504          FALSE),                /* pcrel_offset */
505
506   /* Low 16 bits of displacement in global offset table.  */
507   HOWTO (R_MIPS_CALL_LO16,      /* type */
508          0,                     /* rightshift */
509          2,                     /* size (0 = byte, 1 = short, 2 = long) */
510          16,                    /* bitsize */
511          FALSE,                 /* pc_relative */
512          0,                     /* bitpos */
513          complain_overflow_dont, /* complain_on_overflow */
514          mips_elf_generic_reloc, /* special_function */
515          "R_MIPS_CALL_LO16",    /* name */
516          TRUE,                  /* partial_inplace */
517          0x0000ffff,            /* src_mask */
518          0x0000ffff,            /* dst_mask */
519          FALSE),                /* pcrel_offset */
520
521   /* Section displacement.  */
522   HOWTO (R_MIPS_SCN_DISP,       /* type */
523          0,                     /* rightshift */
524          2,                     /* size (0 = byte, 1 = short, 2 = long) */
525          32,                    /* bitsize */
526          FALSE,                 /* pc_relative */
527          0,                     /* bitpos */
528          complain_overflow_dont, /* complain_on_overflow */
529          mips_elf_generic_reloc, /* special_function */
530          "R_MIPS_SCN_DISP",     /* name */
531          TRUE,                  /* partial_inplace */
532          0xffffffff,            /* src_mask */
533          0xffffffff,            /* dst_mask */
534          FALSE),                /* pcrel_offset */
535
536   EMPTY_HOWTO (R_MIPS_REL16),
537   EMPTY_HOWTO (R_MIPS_ADD_IMMEDIATE),
538   EMPTY_HOWTO (R_MIPS_PJUMP),
539   EMPTY_HOWTO (R_MIPS_RELGOT),
540
541   /* Protected jump conversion.  This is an optimization hint.  No
542      relocation is required for correctness.  */
543   HOWTO (R_MIPS_JALR,           /* type */
544          0,                     /* rightshift */
545          2,                     /* size (0 = byte, 1 = short, 2 = long) */
546          32,                    /* bitsize */
547          FALSE,                 /* pc_relative */
548          0,                     /* bitpos */
549          complain_overflow_dont, /* complain_on_overflow */
550          mips_elf_generic_reloc, /* special_function */
551          "R_MIPS_JALR",         /* name */
552          FALSE,                 /* partial_inplace */
553          0x00000000,            /* src_mask */
554          0x00000000,            /* dst_mask */
555          FALSE),                /* pcrel_offset */
556 };
557
558 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link.  This
559    is a hack to make the linker think that we need 64 bit values.  */
560 static reloc_howto_type elf_mips_ctor64_howto =
561   HOWTO (R_MIPS_64,             /* type */
562          0,                     /* rightshift */
563          4,                     /* size (0 = byte, 1 = short, 2 = long) */
564          32,                    /* bitsize */
565          FALSE,                 /* pc_relative */
566          0,                     /* bitpos */
567          complain_overflow_signed, /* complain_on_overflow */
568          mips32_64bit_reloc,    /* special_function */
569          "R_MIPS_64",           /* name */
570          TRUE,                  /* partial_inplace */
571          0xffffffff,            /* src_mask */
572          0xffffffff,            /* dst_mask */
573          FALSE);                /* pcrel_offset */
574
575 /* The reloc used for the mips16 jump instruction.  */
576 static reloc_howto_type elf_mips16_jump_howto =
577   HOWTO (R_MIPS16_26,           /* type */
578          2,                     /* rightshift */
579          2,                     /* size (0 = byte, 1 = short, 2 = long) */
580          26,                    /* bitsize */
581          FALSE,                 /* pc_relative */
582          0,                     /* bitpos */
583          complain_overflow_dont, /* complain_on_overflow */
584                                 /* This needs complex overflow
585                                    detection, because the upper four
586                                    bits must match the PC.  */
587          mips16_jump_reloc,     /* special_function */
588          "R_MIPS16_26",         /* name */
589          TRUE,                  /* partial_inplace */
590          0x3ffffff,             /* src_mask */
591          0x3ffffff,             /* dst_mask */
592          FALSE);                /* pcrel_offset */
593
594 /* The reloc used for the mips16 gprel instruction.  */
595 static reloc_howto_type elf_mips16_gprel_howto =
596   HOWTO (R_MIPS16_GPREL,        /* type */
597          0,                     /* rightshift */
598          2,                     /* size (0 = byte, 1 = short, 2 = long) */
599          16,                    /* bitsize */
600          FALSE,                 /* pc_relative */
601          0,                     /* bitpos */
602          complain_overflow_signed, /* complain_on_overflow */
603          mips16_gprel_reloc,    /* special_function */
604          "R_MIPS16_GPREL",      /* name */
605          TRUE,                  /* partial_inplace */
606          0x07ff001f,            /* src_mask */
607          0x07ff001f,            /* dst_mask */
608          FALSE);                /* pcrel_offset */
609
610 /* GNU extensions for embedded-pic.  */
611 /* High 16 bits of symbol value, pc-relative.  */
612 static reloc_howto_type elf_mips_gnu_rel_hi16 =
613   HOWTO (R_MIPS_GNU_REL_HI16,   /* type */
614          0,                     /* rightshift */
615          2,                     /* size (0 = byte, 1 = short, 2 = long) */
616          16,                    /* bitsize */
617          TRUE,                  /* pc_relative */
618          0,                     /* bitpos */
619          complain_overflow_dont, /* complain_on_overflow */
620          mips_elf_hi16_reloc,   /* special_function */
621          "R_MIPS_GNU_REL_HI16", /* name */
622          TRUE,                  /* partial_inplace */
623          0xffff,                /* src_mask */
624          0xffff,                /* dst_mask */
625          TRUE);                 /* pcrel_offset */
626
627 /* Low 16 bits of symbol value, pc-relative.  */
628 static reloc_howto_type elf_mips_gnu_rel_lo16 =
629   HOWTO (R_MIPS_GNU_REL_LO16,   /* type */
630          0,                     /* rightshift */
631          2,                     /* size (0 = byte, 1 = short, 2 = long) */
632          16,                    /* bitsize */
633          TRUE,                  /* pc_relative */
634          0,                     /* bitpos */
635          complain_overflow_dont, /* complain_on_overflow */
636          mips_elf_lo16_reloc,   /* special_function */
637          "R_MIPS_GNU_REL_LO16", /* name */
638          TRUE,                  /* partial_inplace */
639          0xffff,                /* src_mask */
640          0xffff,                /* dst_mask */
641          TRUE);                 /* pcrel_offset */
642
643 /* 16 bit offset for pc-relative branches.  */
644 static reloc_howto_type elf_mips_gnu_rel16_s2 =
645   HOWTO (R_MIPS_GNU_REL16_S2,   /* type */
646          2,                     /* rightshift */
647          2,                     /* size (0 = byte, 1 = short, 2 = long) */
648          16,                    /* bitsize */
649          TRUE,                  /* pc_relative */
650          0,                     /* bitpos */
651          complain_overflow_signed, /* complain_on_overflow */
652          mips_elf_generic_reloc, /* special_function */
653          "R_MIPS_GNU_REL16_S2", /* name */
654          TRUE,                  /* partial_inplace */
655          0xffff,                /* src_mask */
656          0xffff,                /* dst_mask */
657          TRUE);                 /* pcrel_offset */
658
659 /* 64 bit pc-relative.  */
660 static reloc_howto_type elf_mips_gnu_pcrel64 =
661   HOWTO (R_MIPS_PC64,           /* type */
662          0,                     /* rightshift */
663          4,                     /* size (0 = byte, 1 = short, 2 = long) */
664          64,                    /* bitsize */
665          TRUE,                  /* pc_relative */
666          0,                     /* bitpos */
667          complain_overflow_signed, /* complain_on_overflow */
668          mips_elf_generic_reloc, /* special_function */
669          "R_MIPS_PC64",         /* name */
670          TRUE,                  /* partial_inplace */
671          MINUS_ONE,             /* src_mask */
672          MINUS_ONE,             /* dst_mask */
673          TRUE);                 /* pcrel_offset */
674
675 /* 32 bit pc-relative.  */
676 static reloc_howto_type elf_mips_gnu_pcrel32 =
677   HOWTO (R_MIPS_PC32,           /* type */
678          0,                     /* rightshift */
679          2,                     /* size (0 = byte, 1 = short, 2 = long) */
680          32,                    /* bitsize */
681          TRUE,                  /* pc_relative */
682          0,                     /* bitpos */
683          complain_overflow_signed, /* complain_on_overflow */
684          mips_elf_generic_reloc, /* special_function */
685          "R_MIPS_PC32",         /* name */
686          TRUE,                  /* partial_inplace */
687          0xffffffff,            /* src_mask */
688          0xffffffff,            /* dst_mask */
689          TRUE);                 /* pcrel_offset */
690
691 /* GNU extension to record C++ vtable hierarchy */
692 static reloc_howto_type elf_mips_gnu_vtinherit_howto =
693   HOWTO (R_MIPS_GNU_VTINHERIT,  /* type */
694          0,                     /* rightshift */
695          2,                     /* size (0 = byte, 1 = short, 2 = long) */
696          0,                     /* bitsize */
697          FALSE,                 /* pc_relative */
698          0,                     /* bitpos */
699          complain_overflow_dont, /* complain_on_overflow */
700          NULL,                  /* special_function */
701          "R_MIPS_GNU_VTINHERIT", /* name */
702          FALSE,                 /* partial_inplace */
703          0,                     /* src_mask */
704          0,                     /* dst_mask */
705          FALSE);                /* pcrel_offset */
706
707 /* GNU extension to record C++ vtable member usage */
708 static reloc_howto_type elf_mips_gnu_vtentry_howto =
709   HOWTO (R_MIPS_GNU_VTENTRY,    /* type */
710          0,                     /* rightshift */
711          2,                     /* size (0 = byte, 1 = short, 2 = long) */
712          0,                     /* bitsize */
713          FALSE,                 /* pc_relative */
714          0,                     /* bitpos */
715          complain_overflow_dont, /* complain_on_overflow */
716          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
717          "R_MIPS_GNU_VTENTRY",  /* name */
718          FALSE,                 /* partial_inplace */
719          0,                     /* src_mask */
720          0,                     /* dst_mask */
721          FALSE);                /* pcrel_offset */
722
723 /* We use this instead of bfd_elf_generic_reloc because the latter
724    gets the handling of zero addends wrong. */
725 static bfd_reloc_status_type
726 mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
727                         asymbol *symbol, void *data ATTRIBUTE_UNUSED,
728                         asection *input_section, bfd *output_bfd,
729                         char **error_message ATTRIBUTE_UNUSED)
730 {
731   /* If we're relocating, and this is an external symbol, we don't want
732      to change anything.  */
733   if (output_bfd != NULL
734       && (symbol->flags & BSF_SECTION_SYM) == 0
735       && (symbol->flags & BSF_LOCAL) != 0)
736     {
737       reloc_entry->address += input_section->output_offset;
738       return bfd_reloc_ok;
739     }
740
741   /* Just go on, nothing to see here.  */
742   return bfd_reloc_continue;
743 }
744
745 /* Do a R_MIPS_HI16 relocation.  This has to be done in combination
746    with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
747    the HI16.  Here we just save the information we need; we do the
748    actual relocation when we see the LO16.
749
750    MIPS ELF requires that the LO16 immediately follow the HI16.  As a
751    GNU extension, for non-pc-relative relocations, we permit an
752    arbitrary number of HI16 relocs to be associated with a single LO16
753    reloc.  This extension permits gcc to output the HI and LO relocs
754    itself.
755
756    This cannot be done for PC-relative relocations because both the HI16
757    and LO16 parts of the relocations must be done relative to the LO16
758    part, and there can be carry to or borrow from the HI16 part.  */
759
760 struct mips_hi16
761 {
762   struct mips_hi16 *next;
763   bfd_byte *addr;
764   bfd_vma addend;
765 };
766
767 /* FIXME: This should not be a static variable.  */
768
769 static struct mips_hi16 *mips_hi16_list;
770
771 static bfd_reloc_status_type
772 mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
773                      asymbol *symbol, void *data, asection *input_section,
774                      bfd *output_bfd, char **error_message)
775 {
776   bfd_reloc_status_type ret;
777   bfd_vma relocation;
778   struct mips_hi16 *n;
779
780   /* If we're relocating, and this is an external symbol, we don't want
781      to change anything.  */
782   if (output_bfd != NULL
783       && (symbol->flags & BSF_SECTION_SYM) == 0
784       && (symbol->flags & BSF_LOCAL) != 0)
785     {
786       reloc_entry->address += input_section->output_offset;
787       return bfd_reloc_ok;
788     }
789
790   ret = bfd_reloc_ok;
791
792   if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
793     {
794       bfd_boolean relocatable;
795       bfd_vma gp;
796
797       if (ret == bfd_reloc_undefined)
798         abort ();
799
800       if (output_bfd != NULL)
801         relocatable = TRUE;
802       else
803         {
804           relocatable = FALSE;
805           output_bfd = symbol->section->output_section->owner;
806         }
807
808       ret = mips_elf_final_gp (output_bfd, symbol, relocatable,
809                                error_message, &gp);
810       if (ret != bfd_reloc_ok)
811         return ret;
812
813       relocation = gp - reloc_entry->address;
814     }
815   else
816     {
817       if (bfd_is_und_section (symbol->section) && output_bfd == NULL)
818         ret = bfd_reloc_undefined;
819
820       if (bfd_is_com_section (symbol->section))
821         relocation = 0;
822       else
823         relocation = symbol->value;
824     }
825
826   relocation += symbol->section->output_section->vma;
827   relocation += symbol->section->output_offset;
828   relocation += reloc_entry->addend;
829
830   if (reloc_entry->address > input_section->_cooked_size)
831     return bfd_reloc_outofrange;
832
833   /* Save the information, and let LO16 do the actual relocation.  */
834   n = bfd_malloc (sizeof *n);
835   if (n == NULL)
836     return bfd_reloc_outofrange;
837   n->addr = (bfd_byte *) data + reloc_entry->address;
838   n->addend = relocation;
839   n->next = mips_hi16_list;
840   mips_hi16_list = n;
841
842   if (output_bfd != NULL)
843     reloc_entry->address += input_section->output_offset;
844
845   return ret;
846 }
847
848 /* Do a R_MIPS_LO16 relocation.  This is a straightforward 16 bit
849    inplace relocation; this function exists in order to do the
850    R_MIPS_HI16 relocation described above.  */
851
852 static bfd_reloc_status_type
853 mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
854                      void *data, asection *input_section, bfd *output_bfd,
855                      char **error_message)
856 {
857   arelent gp_disp_relent;
858
859   if (mips_hi16_list != NULL)
860     {
861       struct mips_hi16 *l;
862
863       l = mips_hi16_list;
864       while (l != NULL)
865         {
866           unsigned long insn;
867           unsigned long val;
868           unsigned long vallo;
869           struct mips_hi16 *next;
870
871           if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
872             {
873               gp_disp_relent = *reloc_entry;
874               reloc_entry = &gp_disp_relent;
875               reloc_entry->addend = l->addend;
876             }
877           else
878             {
879               /* Do the HI16 relocation.  Note that we actually don't need
880                  to know anything about the LO16 itself, except where to
881                  find the low 16 bits of the addend needed by the LO16.  */
882               insn = bfd_get_32 (abfd, l->addr);
883               vallo = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
884               /* The low order 16 bits are always treated as a signed
885                  value.  */
886               vallo = ((vallo & 0xffff) ^ 0x8000) - 0x8000;
887               val = ((insn & 0xffff) << 16) + vallo;
888               val += l->addend;
889
890               /* If PC-relative, we need to subtract out the address of the LO
891                  half of the HI/LO.  (The actual relocation is relative
892                  to that instruction.)  */
893               if (reloc_entry->howto->pc_relative)
894                 val -= reloc_entry->address;
895
896               /* At this point, "val" has the value of the combined HI/LO
897                  pair.  If the low order 16 bits (which will be used for
898                  the LO16 insn) are negative, then we will need an
899                  adjustment for the high order 16 bits.  */
900               val += 0x8000;
901               val = (val >> 16) & 0xffff;
902
903               insn &= ~ (bfd_vma) 0xffff;
904               insn |= val;
905               bfd_put_32 (abfd, insn, l->addr);
906             }
907
908           next = l->next;
909           free (l);
910           l = next;
911         }
912
913       mips_hi16_list = NULL;
914     }
915   else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
916     {
917       bfd_reloc_status_type ret;
918       bfd_vma gp, relocation;
919
920       /* FIXME: Does this case ever occur?  */
921
922       ret = mips_elf_final_gp (output_bfd, symbol, TRUE, error_message, &gp);
923       if (ret != bfd_reloc_ok)
924         return ret;
925
926       relocation = gp - reloc_entry->address;
927       relocation += symbol->section->output_section->vma;
928       relocation += symbol->section->output_offset;
929       relocation += reloc_entry->addend;
930
931       if (reloc_entry->address > input_section->_cooked_size)
932         return bfd_reloc_outofrange;
933
934       gp_disp_relent = *reloc_entry;
935       reloc_entry = &gp_disp_relent;
936       reloc_entry->addend = relocation - 4;
937     }
938
939   /* Now do the LO16 reloc in the usual way.  */
940   return mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
941                                  input_section, output_bfd, error_message);
942 }
943
944 /* Do a R_MIPS_GOT16 reloc.  This is a reloc against the global offset
945    table used for PIC code.  If the symbol is an external symbol, the
946    instruction is modified to contain the offset of the appropriate
947    entry in the global offset table.  If the symbol is a section
948    symbol, the next reloc is a R_MIPS_LO16 reloc.  The two 16 bit
949    addends are combined to form the real addend against the section
950    symbol; the GOT16 is modified to contain the offset of an entry in
951    the global offset table, and the LO16 is modified to offset it
952    appropriately.  Thus an offset larger than 16 bits requires a
953    modified value in the global offset table.
954
955    This implementation suffices for the assembler, but the linker does
956    not yet know how to create global offset tables.  */
957
958 static bfd_reloc_status_type
959 mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
960                       void *data, asection *input_section, bfd *output_bfd,
961                       char **error_message)
962 {
963   /* If we're relocating, and this is an external symbol, we don't want
964      to change anything.  */
965   if (output_bfd != NULL
966       && (symbol->flags & BSF_SECTION_SYM) == 0
967       && (symbol->flags & BSF_LOCAL) != 0)
968     {
969       reloc_entry->address += input_section->output_offset;
970       return bfd_reloc_ok;
971     }
972
973   return mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
974                               input_section, output_bfd, error_message);
975 }
976
977 /* Set the GP value for OUTPUT_BFD.  Returns FALSE if this is a
978    dangerous relocation.  */
979
980 static bfd_boolean
981 mips_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp)
982 {
983   unsigned int count;
984   asymbol **sym;
985   unsigned int i;
986
987   /* If we've already figured out what GP will be, just return it.  */
988   *pgp = _bfd_get_gp_value (output_bfd);
989   if (*pgp)
990     return TRUE;
991
992   count = bfd_get_symcount (output_bfd);
993   sym = bfd_get_outsymbols (output_bfd);
994
995   /* The linker script will have created a symbol named `_gp' with the
996      appropriate value.  */
997   if (sym == NULL)
998     i = count;
999   else
1000     {
1001       for (i = 0; i < count; i++, sym++)
1002         {
1003           register const char *name;
1004
1005           name = bfd_asymbol_name (*sym);
1006           if (*name == '_' && strcmp (name, "_gp") == 0)
1007             {
1008               *pgp = bfd_asymbol_value (*sym);
1009               _bfd_set_gp_value (output_bfd, *pgp);
1010               break;
1011             }
1012         }
1013     }
1014
1015   if (i >= count)
1016     {
1017       /* Only get the error once.  */
1018       *pgp = 4;
1019       _bfd_set_gp_value (output_bfd, *pgp);
1020       return FALSE;
1021     }
1022
1023   return TRUE;
1024 }
1025
1026 /* We have to figure out the gp value, so that we can adjust the
1027    symbol value correctly.  We look up the symbol _gp in the output
1028    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
1029    target data.  We don't need to adjust the symbol value for an
1030    external symbol if we are producing relocatable output.  */
1031
1032 static bfd_reloc_status_type
1033 mips_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable,
1034                    char **error_message, bfd_vma *pgp)
1035 {
1036   if (bfd_is_und_section (symbol->section)
1037       && ! relocatable)
1038     {
1039       *pgp = 0;
1040       return bfd_reloc_undefined;
1041     }
1042
1043   *pgp = _bfd_get_gp_value (output_bfd);
1044   if (*pgp == 0
1045       && (! relocatable
1046           || (symbol->flags & BSF_SECTION_SYM) != 0))
1047     {
1048       if (relocatable)
1049         {
1050           /* Make up a value.  */
1051           *pgp = symbol->section->output_section->vma + 0x4000;
1052           _bfd_set_gp_value (output_bfd, *pgp);
1053         }
1054       else if (!mips_elf_assign_gp (output_bfd, pgp))
1055         {
1056           *error_message =
1057             (char *) _("GP relative relocation when _gp not defined");
1058           return bfd_reloc_dangerous;
1059         }
1060     }
1061
1062   return bfd_reloc_ok;
1063 }
1064
1065 /* Do a R_MIPS_GPREL16 relocation.  This is a 16 bit value which must
1066    become the offset from the gp register.  This function also handles
1067    R_MIPS_LITERAL relocations, although those can be handled more
1068    cleverly because the entries in the .lit8 and .lit4 sections can be
1069    merged.  */
1070
1071 bfd_reloc_status_type
1072 _bfd_mips_elf32_gprel16_reloc (bfd *abfd, arelent *reloc_entry,
1073                                asymbol *symbol, void *data,
1074                                asection *input_section, bfd *output_bfd,
1075                                char **error_message)
1076 {
1077   bfd_boolean relocatable;
1078   bfd_reloc_status_type ret;
1079   bfd_vma gp;
1080
1081   /* If we're relocating, and this is an external symbol, we don't want
1082      to change anything.  */
1083   if (output_bfd != NULL
1084       && (symbol->flags & BSF_SECTION_SYM) == 0
1085       && (symbol->flags & BSF_LOCAL) != 0)
1086     {
1087       reloc_entry->address += input_section->output_offset;
1088       return bfd_reloc_ok;
1089     }
1090
1091   if (output_bfd != NULL)
1092     relocatable = TRUE;
1093   else
1094     {
1095       relocatable = FALSE;
1096       output_bfd = symbol->section->output_section->owner;
1097     }
1098
1099   ret = mips_elf_final_gp (output_bfd, symbol, relocatable, error_message,
1100                            &gp);
1101   if (ret != bfd_reloc_ok)
1102     return ret;
1103
1104   return _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
1105                                         input_section, relocatable,
1106                                         data, gp);
1107 }
1108
1109 /* Do a R_MIPS_GPREL32 relocation.  This is a 32 bit value which must
1110    become the offset from the gp register.  */
1111
1112 static bfd_reloc_status_type
1113 mips_elf_gprel32_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1114                         void *data, asection *input_section, bfd *output_bfd,
1115                         char **error_message)
1116 {
1117   bfd_boolean relocatable;
1118   bfd_reloc_status_type ret;
1119   bfd_vma gp;
1120
1121   /* If we're relocating, and this is an external symbol, we don't want
1122      to change anything.  */
1123   if (output_bfd != NULL
1124       && (symbol->flags & BSF_SECTION_SYM) == 0
1125       && (symbol->flags & BSF_LOCAL) != 0)
1126     {
1127       *error_message = (char *)
1128         _("32bits gp relative relocation occurs for an external symbol");
1129       return bfd_reloc_outofrange;
1130     }
1131
1132   if (output_bfd != NULL)
1133     relocatable = TRUE;
1134   else
1135     {
1136       relocatable = FALSE;
1137       output_bfd = symbol->section->output_section->owner;
1138     }
1139
1140   ret = mips_elf_final_gp (output_bfd, symbol, relocatable,
1141                            error_message, &gp);
1142   if (ret != bfd_reloc_ok)
1143     return ret;
1144
1145   return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1146                           relocatable, data, gp);
1147 }
1148
1149 static bfd_reloc_status_type
1150 gprel32_with_gp (bfd *abfd, asymbol *symbol, arelent *reloc_entry,
1151                  asection *input_section, bfd_boolean relocatable,
1152                  void *data, bfd_vma gp)
1153 {
1154   bfd_vma relocation;
1155   bfd_vma val;
1156
1157   if (bfd_is_com_section (symbol->section))
1158     relocation = 0;
1159   else
1160     relocation = symbol->value;
1161
1162   relocation += symbol->section->output_section->vma;
1163   relocation += symbol->section->output_offset;
1164
1165   if (reloc_entry->address > input_section->_cooked_size)
1166     return bfd_reloc_outofrange;
1167
1168   /* Set val to the offset into the section or symbol.  */
1169   val = reloc_entry->addend;
1170
1171   if (reloc_entry->howto->partial_inplace)
1172     val += bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1173
1174   /* Adjust val for the final section location and GP value.  If we
1175      are producing relocatable output, we don't want to do this for
1176      an external symbol.  */
1177   if (! relocatable
1178       || (symbol->flags & BSF_SECTION_SYM) != 0)
1179     val += relocation - gp;
1180
1181   if (reloc_entry->howto->partial_inplace)
1182     bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1183   else
1184     reloc_entry->addend = val;
1185
1186   if (relocatable)
1187     reloc_entry->address += input_section->output_offset;
1188
1189   return bfd_reloc_ok;
1190 }
1191
1192 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file.  These are
1193    generated when addresses are 64 bits.  The upper 32 bits are a simple
1194    sign extension.  */
1195
1196 static bfd_reloc_status_type
1197 mips32_64bit_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1198                     void *data, asection *input_section, bfd *output_bfd,
1199                     char **error_message)
1200 {
1201   bfd_reloc_status_type r;
1202   arelent reloc32;
1203   unsigned long val;
1204   bfd_size_type addr;
1205
1206   r = mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1207                               input_section, output_bfd, error_message);
1208   if (r != bfd_reloc_continue)
1209     return r;
1210
1211   /* Do a normal 32 bit relocation on the lower 32 bits.  */
1212   reloc32 = *reloc_entry;
1213   if (bfd_big_endian (abfd))
1214     reloc32.address += 4;
1215   reloc32.howto = &elf_mips_howto_table_rel[R_MIPS_32];
1216   r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1217                               output_bfd, error_message);
1218
1219   /* Sign extend into the upper 32 bits.  */
1220   val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1221   if ((val & 0x80000000) != 0)
1222     val = 0xffffffff;
1223   else
1224     val = 0;
1225   addr = reloc_entry->address;
1226   if (bfd_little_endian (abfd))
1227     addr += 4;
1228   bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1229
1230   return r;
1231 }
1232
1233 /* Handle a mips16 jump.  */
1234
1235 static bfd_reloc_status_type
1236 mips16_jump_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
1237                    asymbol *symbol, void *data ATTRIBUTE_UNUSED,
1238                    asection *input_section, bfd *output_bfd,
1239                    char **error_message ATTRIBUTE_UNUSED)
1240 {
1241   if (output_bfd != NULL
1242       && (symbol->flags & BSF_SECTION_SYM) == 0
1243       && reloc_entry->addend == 0)
1244     {
1245       reloc_entry->address += input_section->output_offset;
1246       return bfd_reloc_ok;
1247     }
1248
1249   /* FIXME.  */
1250   {
1251     static bfd_boolean warned;
1252
1253     if (! warned)
1254       (*_bfd_error_handler)
1255         (_("Linking mips16 objects into %s format is not supported"),
1256          bfd_get_target (input_section->output_section->owner));
1257     warned = TRUE;
1258   }
1259
1260   return bfd_reloc_undefined;
1261 }
1262
1263 /* Handle a mips16 GP relative reloc.  */
1264
1265 static bfd_reloc_status_type
1266 mips16_gprel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1267                     void *data, asection *input_section, bfd *output_bfd,
1268                     char **error_message)
1269 {
1270   bfd_boolean relocatable;
1271   bfd_reloc_status_type ret;
1272   bfd_vma gp;
1273   unsigned short extend = 0;
1274   unsigned short insn = 0;
1275   bfd_signed_vma val;
1276   bfd_vma relocation;
1277
1278   /* If we're relocating, and this is an external symbol, we don't want
1279      to change anything.  */
1280   if (output_bfd != NULL
1281       && (symbol->flags & BSF_SECTION_SYM) == 0
1282       && (symbol->flags & BSF_LOCAL) != 0)
1283     {
1284       reloc_entry->address += input_section->output_offset;
1285       return bfd_reloc_ok;
1286     }
1287
1288   if (output_bfd != NULL)
1289     relocatable = TRUE;
1290   else
1291     {
1292       relocatable = FALSE;
1293       output_bfd = symbol->section->output_section->owner;
1294     }
1295
1296   ret = mips_elf_final_gp (output_bfd, symbol, relocatable, error_message,
1297                            &gp);
1298   if (ret != bfd_reloc_ok)
1299     return ret;
1300
1301   if (reloc_entry->address > input_section->_cooked_size)
1302     return bfd_reloc_outofrange;
1303
1304   if (bfd_is_com_section (symbol->section))
1305     relocation = 0;
1306   else
1307     relocation = symbol->value;
1308
1309   relocation += symbol->section->output_section->vma;
1310   relocation += symbol->section->output_offset;
1311
1312   /* Set val to the offset into the section or symbol.  */
1313   val = reloc_entry->addend;
1314
1315   if (reloc_entry->howto->partial_inplace)
1316     {
1317       /* Pick up the mips16 extend instruction and the real instruction.  */
1318       extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1319       insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1320       val += ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1321     }
1322
1323   _bfd_mips_elf_sign_extend(val, 16);
1324
1325   /* Adjust val for the final section location and GP value.  If we
1326      are producing relocatable output, we don't want to do this for
1327      an external symbol.  */
1328   if (! relocatable
1329       || (symbol->flags & BSF_SECTION_SYM) != 0)
1330     val += relocation - gp;
1331
1332   if (reloc_entry->howto->partial_inplace)
1333     {
1334       bfd_put_16 (abfd,
1335                   (extend & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0),
1336                   (bfd_byte *) data + reloc_entry->address);
1337       bfd_put_16 (abfd,
1338                   (insn & 0xffe0) | (val & 0x1f),
1339                   (bfd_byte *) data + reloc_entry->address + 2);
1340     }
1341   else
1342     reloc_entry->addend = val;
1343
1344   if (relocatable)
1345     reloc_entry->address += input_section->output_offset;
1346   else if (((val & ~0xffff) != ~0xffff) && ((val & ~0xffff) != 0))
1347     return bfd_reloc_overflow;
1348
1349   return bfd_reloc_ok;
1350 }
1351
1352 /* A mapping from BFD reloc types to MIPS ELF reloc types.  */
1353
1354 struct elf_reloc_map {
1355   bfd_reloc_code_real_type bfd_val;
1356   enum elf_mips_reloc_type elf_val;
1357 };
1358
1359 static const struct elf_reloc_map mips_reloc_map[] =
1360 {
1361   { BFD_RELOC_NONE, R_MIPS_NONE },
1362   { BFD_RELOC_16, R_MIPS_16 },
1363   { BFD_RELOC_32, R_MIPS_32 },
1364   /* There is no BFD reloc for R_MIPS_REL32.  */
1365   { BFD_RELOC_64, R_MIPS_64 },
1366   { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1367   { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1368   { BFD_RELOC_LO16, R_MIPS_LO16 },
1369   { BFD_RELOC_GPREL16, R_MIPS_GPREL16 },
1370   { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1371   { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1372   { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1373   { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1374   { BFD_RELOC_GPREL32, R_MIPS_GPREL32 },
1375   { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1376   { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1377   { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1378   { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 },
1379   { BFD_RELOC_MIPS_SUB, R_MIPS_SUB },
1380   { BFD_RELOC_MIPS_GOT_PAGE, R_MIPS_GOT_PAGE },
1381   { BFD_RELOC_MIPS_GOT_OFST, R_MIPS_GOT_OFST },
1382   { BFD_RELOC_MIPS_GOT_DISP, R_MIPS_GOT_DISP }
1383 };
1384
1385 /* Given a BFD reloc type, return a howto structure.  */
1386
1387 static reloc_howto_type *
1388 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1389 {
1390   unsigned int i;
1391   reloc_howto_type *howto_table = elf_mips_howto_table_rel;
1392
1393   for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map);
1394        i++)
1395     {
1396       if (mips_reloc_map[i].bfd_val == code)
1397         return &howto_table[(int) mips_reloc_map[i].elf_val];
1398     }
1399
1400   switch (code)
1401     {
1402     default:
1403       bfd_set_error (bfd_error_bad_value);
1404       return NULL;
1405
1406     case BFD_RELOC_CTOR:
1407       /* We need to handle BFD_RELOC_CTOR specially.
1408          Select the right relocation (R_MIPS_32 or R_MIPS_64) based on the
1409          size of addresses of the ABI.  */
1410       if ((elf_elfheader (abfd)->e_flags & (E_MIPS_ABI_O64
1411                                             | E_MIPS_ABI_EABI64)) != 0)
1412         return &elf_mips_ctor64_howto;
1413       else
1414         return &howto_table[(int) R_MIPS_32];
1415
1416     case BFD_RELOC_MIPS16_JMP:
1417       return &elf_mips16_jump_howto;
1418     case BFD_RELOC_MIPS16_GPREL:
1419       return &elf_mips16_gprel_howto;
1420     case BFD_RELOC_VTABLE_INHERIT:
1421       return &elf_mips_gnu_vtinherit_howto;
1422     case BFD_RELOC_VTABLE_ENTRY:
1423       return &elf_mips_gnu_vtentry_howto;
1424     case BFD_RELOC_PCREL_HI16_S:
1425       return &elf_mips_gnu_rel_hi16;
1426     case BFD_RELOC_PCREL_LO16:
1427       return &elf_mips_gnu_rel_lo16;
1428     case BFD_RELOC_16_PCREL_S2:
1429       return &elf_mips_gnu_rel16_s2;
1430     case BFD_RELOC_64_PCREL:
1431       return &elf_mips_gnu_pcrel64;
1432     case BFD_RELOC_32_PCREL:
1433       return &elf_mips_gnu_pcrel32;
1434     }
1435 }
1436
1437 /* Given a MIPS Elf_Internal_Rel, fill in an arelent structure.  */
1438
1439 static reloc_howto_type *
1440 mips_elf32_rtype_to_howto (unsigned int r_type,
1441                            bfd_boolean rela_p ATTRIBUTE_UNUSED)
1442 {
1443   switch (r_type)
1444     {
1445     case R_MIPS16_26:
1446       return &elf_mips16_jump_howto;
1447     case R_MIPS16_GPREL:
1448       return &elf_mips16_gprel_howto;
1449     case R_MIPS_GNU_VTINHERIT:
1450       return &elf_mips_gnu_vtinherit_howto;
1451     case R_MIPS_GNU_VTENTRY:
1452       return &elf_mips_gnu_vtentry_howto;
1453     case R_MIPS_GNU_REL_HI16:
1454       return &elf_mips_gnu_rel_hi16;
1455     case R_MIPS_GNU_REL_LO16:
1456       return &elf_mips_gnu_rel_lo16;
1457     case R_MIPS_GNU_REL16_S2:
1458       return &elf_mips_gnu_rel16_s2;
1459     case R_MIPS_PC64:
1460       return &elf_mips_gnu_pcrel64;
1461     case R_MIPS_PC32:
1462       return &elf_mips_gnu_pcrel32;
1463     default:
1464       BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1465       return &elf_mips_howto_table_rel[r_type];
1466     }
1467 }
1468
1469 /* Given a MIPS Elf_Internal_Rel, fill in an arelent structure.  */
1470
1471 static void
1472 mips_info_to_howto_rel (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
1473 {
1474   unsigned int r_type;
1475
1476   r_type = ELF32_R_TYPE (dst->r_info);
1477   cache_ptr->howto = mips_elf32_rtype_to_howto (r_type, FALSE);
1478
1479   /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1480      value for the object file.  We get the addend now, rather than
1481      when we do the relocation, because the symbol manipulations done
1482      by the linker may cause us to lose track of the input BFD.  */
1483   if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1484       && (r_type == (unsigned int) R_MIPS_GPREL16
1485           || r_type == (unsigned int) R_MIPS_LITERAL))
1486     cache_ptr->addend = elf_gp (abfd);
1487 }
1488
1489 /* Given a MIPS Elf_Internal_Rela, fill in an arelent structure.  */
1490
1491 static void
1492 mips_info_to_howto_rela (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
1493 {
1494   mips_info_to_howto_rel (abfd, cache_ptr, dst);
1495
1496   /* If we ever need to do any extra processing with dst->r_addend
1497      (the field omitted in an Elf_Internal_Rel) we can do it here.  */
1498 }
1499 \f
1500 /* Determine whether a symbol is global for the purposes of splitting
1501    the symbol table into global symbols and local symbols.  At least
1502    on Irix 5, this split must be between section symbols and all other
1503    symbols.  On most ELF targets the split is between static symbols
1504    and externally visible symbols.  */
1505
1506 static bfd_boolean
1507 mips_elf_sym_is_global (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
1508 {
1509   if (SGI_COMPAT (abfd))
1510     return (sym->flags & BSF_SECTION_SYM) == 0;
1511   else
1512     return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1513             || bfd_is_und_section (bfd_get_section (sym))
1514             || bfd_is_com_section (bfd_get_section (sym)));
1515 }
1516 \f
1517 /* Set the right machine number for a MIPS ELF file.  */
1518
1519 static bfd_boolean
1520 mips_elf32_object_p (bfd *abfd)
1521 {
1522   unsigned long mach;
1523
1524   /* Irix 5 and 6 are broken.  Object file symbol tables are not always
1525      sorted correctly such that local symbols precede global symbols,
1526      and the sh_info field in the symbol table is not always right.  */
1527   if (SGI_COMPAT (abfd))
1528     elf_bad_symtab (abfd) = TRUE;
1529
1530   if (ABI_N32_P (abfd))
1531     return FALSE;
1532
1533   mach = _bfd_elf_mips_mach (elf_elfheader (abfd)->e_flags);
1534   bfd_default_set_arch_mach (abfd, bfd_arch_mips, mach);
1535
1536   return TRUE;
1537 }
1538 \f
1539 /* MIPS ELF local labels start with '$', not 'L'.  */
1540
1541 static bfd_boolean
1542 mips_elf_is_local_label_name (bfd *abfd, const char *name)
1543 {
1544   if (name[0] == '$')
1545     return TRUE;
1546
1547   /* On Irix 6, the labels go back to starting with '.', so we accept
1548      the generic ELF local label syntax as well.  */
1549   return _bfd_elf_is_local_label_name (abfd, name);
1550 }
1551 \f
1552 /* Support for core dump NOTE sections.  */
1553 static bfd_boolean
1554 elf32_mips_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1555 {
1556   int offset;
1557   unsigned int raw_size;
1558
1559   switch (note->descsz)
1560     {
1561       default:
1562         return FALSE;
1563
1564       case 256:         /* Linux/MIPS */
1565         /* pr_cursig */
1566         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1567
1568         /* pr_pid */
1569         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1570
1571         /* pr_reg */
1572         offset = 72;
1573         raw_size = 180;
1574
1575         break;
1576     }
1577
1578   /* Make a ".reg/999" section.  */
1579   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1580                                           raw_size, note->descpos + offset);
1581 }
1582
1583 static bfd_boolean
1584 elf32_mips_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1585 {
1586   switch (note->descsz)
1587     {
1588       default:
1589         return FALSE;
1590
1591       case 128:         /* Linux/MIPS elf_prpsinfo */
1592         elf_tdata (abfd)->core_program
1593          = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1594         elf_tdata (abfd)->core_command
1595          = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1596     }
1597
1598   /* Note that for some reason, a spurious space is tacked
1599      onto the end of the args in some (at least one anyway)
1600      implementations, so strip it off if it exists.  */
1601
1602   {
1603     char *command = elf_tdata (abfd)->core_command;
1604     int n = strlen (command);
1605
1606     if (0 < n && command[n - 1] == ' ')
1607       command[n - 1] = '\0';
1608   }
1609
1610   return TRUE;
1611 }
1612 \f
1613 /* Depending on the target vector we generate some version of Irix
1614    executables or "normal" MIPS ELF ABI executables.  */
1615 static irix_compat_t
1616 elf32_mips_irix_compat (bfd *abfd)
1617 {
1618   if ((abfd->xvec == &bfd_elf32_bigmips_vec)
1619       || (abfd->xvec == &bfd_elf32_littlemips_vec))
1620     return ict_irix5;
1621   else
1622     return ict_none;
1623 }
1624 \f
1625 /* Given a data section and an in-memory embedded reloc section, store
1626    relocation information into the embedded reloc section which can be
1627    used at runtime to relocate the data section.  This is called by the
1628    linker when the --embedded-relocs switch is used.  This is called
1629    after the add_symbols entry point has been called for all the
1630    objects, and before the final_link entry point is called.  */
1631
1632 bfd_boolean
1633 bfd_mips_elf32_create_embedded_relocs (bfd *abfd, struct bfd_link_info *info,
1634                                        asection *datasec, asection *relsec,
1635                                        char **errmsg)
1636 {
1637   Elf_Internal_Shdr *symtab_hdr;
1638   Elf_Internal_Sym *isymbuf = NULL;
1639   Elf_Internal_Rela *internal_relocs = NULL;
1640   Elf_Internal_Rela *irel, *irelend;
1641   bfd_byte *p;
1642
1643   BFD_ASSERT (! info->relocatable);
1644
1645   *errmsg = NULL;
1646
1647   if (datasec->reloc_count == 0)
1648     return TRUE;
1649
1650   /* Read this BFD's symbols if we haven't done so already, or get the cached
1651      copy if it exists.  */
1652   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1653   if (symtab_hdr->sh_info != 0)
1654     {
1655       isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1656       if (isymbuf == NULL)
1657         isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1658                                         symtab_hdr->sh_info, 0,
1659                                         NULL, NULL, NULL);
1660       if (isymbuf == NULL)
1661         goto error_return;
1662     }
1663
1664   /* Get a copy of the native relocations.  */
1665   internal_relocs = _bfd_elf_link_read_relocs (abfd, datasec, NULL, NULL,
1666                                                info->keep_memory);
1667   if (internal_relocs == NULL)
1668     goto error_return;
1669
1670   relsec->contents = bfd_alloc (abfd, datasec->reloc_count * 12);
1671   if (relsec->contents == NULL)
1672     goto error_return;
1673
1674   p = relsec->contents;
1675
1676   irelend = internal_relocs + datasec->reloc_count;
1677
1678   for (irel = internal_relocs; irel < irelend; irel++, p += 12)
1679     {
1680       asection *targetsec;
1681
1682       /* We are going to write a four byte longword into the runtime
1683          reloc section.  The longword will be the address in the data
1684          section which must be relocated.  It is followed by the name
1685          of the target section NUL-padded or truncated to 8
1686          characters.  */
1687
1688       /* We can only relocate absolute longword relocs at run time.  */
1689       if ((ELF32_R_TYPE (irel->r_info) != (int) R_MIPS_32) &&
1690           (ELF32_R_TYPE (irel->r_info) != (int) R_MIPS_64))
1691         {
1692           *errmsg = _("unsupported reloc type");
1693           bfd_set_error (bfd_error_bad_value);
1694           goto error_return;
1695         }
1696       /* Get the target section referred to by the reloc.  */
1697       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1698         {
1699           Elf_Internal_Sym *isym;
1700
1701           /* A local symbol.  */
1702           isym = isymbuf + ELF32_R_SYM (irel->r_info);
1703           targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1704         }
1705       else
1706         {
1707           unsigned long indx;
1708           struct elf_link_hash_entry *h;
1709
1710           /* An external symbol.  */
1711           indx = ELF32_R_SYM (irel->r_info);
1712           h = elf_sym_hashes (abfd)[indx];
1713           targetsec = NULL;
1714           /*
1715              For some reason, in certain programs, the symbol will
1716              not be in the hash table.  It seems to happen when you
1717              declare a static table of pointers to const external structures.
1718              In this case, the relocs are relative to data, not
1719              text, so just treating it like an undefined link
1720              should be sufficient.  */
1721           BFD_ASSERT(h != NULL);
1722           if (h->root.type == bfd_link_hash_defined
1723               || h->root.type == bfd_link_hash_defweak)
1724             targetsec = h->root.u.def.section;
1725         }
1726
1727
1728       /*
1729          Set the low bit of the relocation offset if it's a MIPS64 reloc.
1730          Relocations will always be on (at least) 32-bit boundaries.  */
1731
1732       bfd_put_32 (abfd, ((irel->r_offset + datasec->output_offset) +
1733                   ((ELF32_R_TYPE (irel->r_info) == (int) R_MIPS_64) ? 1 : 0)),
1734                   p);
1735       memset (p + 4, 0, 8);
1736       if (targetsec != NULL)
1737         strncpy (p + 4, targetsec->output_section->name, 8);
1738     }
1739
1740   if (internal_relocs != NULL
1741       && elf_section_data (datasec)->relocs != internal_relocs)
1742     free (internal_relocs);
1743   if (isymbuf != NULL
1744       && symtab_hdr->contents != (unsigned char *) isymbuf)
1745     free (isymbuf);
1746   return TRUE;
1747
1748  error_return:
1749   if (internal_relocs != NULL
1750       && elf_section_data (datasec)->relocs != internal_relocs)
1751     free (internal_relocs);
1752   if (isymbuf != NULL
1753       && symtab_hdr->contents != (unsigned char *) isymbuf)
1754     free (isymbuf);
1755   return FALSE;
1756 }
1757 \f
1758 /* ECOFF swapping routines.  These are used when dealing with the
1759    .mdebug section, which is in the ECOFF debugging format.  */
1760 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
1761   /* Symbol table magic number.  */
1762   magicSym,
1763   /* Alignment of debugging information.  E.g., 4.  */
1764   4,
1765   /* Sizes of external symbolic information.  */
1766   sizeof (struct hdr_ext),
1767   sizeof (struct dnr_ext),
1768   sizeof (struct pdr_ext),
1769   sizeof (struct sym_ext),
1770   sizeof (struct opt_ext),
1771   sizeof (struct fdr_ext),
1772   sizeof (struct rfd_ext),
1773   sizeof (struct ext_ext),
1774   /* Functions to swap in external symbolic data.  */
1775   ecoff_swap_hdr_in,
1776   ecoff_swap_dnr_in,
1777   ecoff_swap_pdr_in,
1778   ecoff_swap_sym_in,
1779   ecoff_swap_opt_in,
1780   ecoff_swap_fdr_in,
1781   ecoff_swap_rfd_in,
1782   ecoff_swap_ext_in,
1783   _bfd_ecoff_swap_tir_in,
1784   _bfd_ecoff_swap_rndx_in,
1785   /* Functions to swap out external symbolic data.  */
1786   ecoff_swap_hdr_out,
1787   ecoff_swap_dnr_out,
1788   ecoff_swap_pdr_out,
1789   ecoff_swap_sym_out,
1790   ecoff_swap_opt_out,
1791   ecoff_swap_fdr_out,
1792   ecoff_swap_rfd_out,
1793   ecoff_swap_ext_out,
1794   _bfd_ecoff_swap_tir_out,
1795   _bfd_ecoff_swap_rndx_out,
1796   /* Function to read in symbolic data.  */
1797   _bfd_mips_elf_read_ecoff_info
1798 };
1799 \f
1800 #define ELF_ARCH                        bfd_arch_mips
1801 #define ELF_MACHINE_CODE                EM_MIPS
1802
1803 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
1804    a value of 0x1000, and we are compatible.  */
1805 #define ELF_MAXPAGESIZE                 0x1000
1806
1807 #define elf_backend_collect             TRUE
1808 #define elf_backend_type_change_ok      TRUE
1809 #define elf_backend_can_gc_sections     TRUE
1810 #define elf_info_to_howto               mips_info_to_howto_rela
1811 #define elf_info_to_howto_rel           mips_info_to_howto_rel
1812 #define elf_backend_sym_is_global       mips_elf_sym_is_global
1813 #define elf_backend_object_p            mips_elf32_object_p
1814 #define elf_backend_symbol_processing   _bfd_mips_elf_symbol_processing
1815 #define elf_backend_section_processing  _bfd_mips_elf_section_processing
1816 #define elf_backend_section_from_shdr   _bfd_mips_elf_section_from_shdr
1817 #define elf_backend_fake_sections       _bfd_mips_elf_fake_sections
1818 #define elf_backend_section_from_bfd_section \
1819                                         _bfd_mips_elf_section_from_bfd_section
1820 #define elf_backend_add_symbol_hook     _bfd_mips_elf_add_symbol_hook
1821 #define elf_backend_link_output_symbol_hook \
1822                                         _bfd_mips_elf_link_output_symbol_hook
1823 #define elf_backend_create_dynamic_sections \
1824                                         _bfd_mips_elf_create_dynamic_sections
1825 #define elf_backend_check_relocs        _bfd_mips_elf_check_relocs
1826 #define elf_backend_adjust_dynamic_symbol \
1827                                         _bfd_mips_elf_adjust_dynamic_symbol
1828 #define elf_backend_always_size_sections \
1829                                         _bfd_mips_elf_always_size_sections
1830 #define elf_backend_size_dynamic_sections \
1831                                         _bfd_mips_elf_size_dynamic_sections
1832 #define elf_backend_relocate_section    _bfd_mips_elf_relocate_section
1833 #define elf_backend_finish_dynamic_symbol \
1834                                         _bfd_mips_elf_finish_dynamic_symbol
1835 #define elf_backend_finish_dynamic_sections \
1836                                         _bfd_mips_elf_finish_dynamic_sections
1837 #define elf_backend_final_write_processing \
1838                                         _bfd_mips_elf_final_write_processing
1839 #define elf_backend_additional_program_headers \
1840                                         _bfd_mips_elf_additional_program_headers
1841 #define elf_backend_modify_segment_map  _bfd_mips_elf_modify_segment_map
1842 #define elf_backend_gc_mark_hook        _bfd_mips_elf_gc_mark_hook
1843 #define elf_backend_gc_sweep_hook       _bfd_mips_elf_gc_sweep_hook
1844 #define elf_backend_copy_indirect_symbol \
1845                                         _bfd_mips_elf_copy_indirect_symbol
1846 #define elf_backend_hide_symbol         _bfd_mips_elf_hide_symbol
1847 #define elf_backend_grok_prstatus       elf32_mips_grok_prstatus
1848 #define elf_backend_grok_psinfo         elf32_mips_grok_psinfo
1849 #define elf_backend_ecoff_debug_swap    &mips_elf32_ecoff_debug_swap
1850
1851 #define elf_backend_got_header_size     (4 * MIPS_RESERVED_GOTNO)
1852 #define elf_backend_may_use_rel_p       1
1853 #define elf_backend_may_use_rela_p      0
1854 #define elf_backend_default_use_rela_p  0
1855 #define elf_backend_sign_extend_vma     TRUE
1856
1857 #define elf_backend_discard_info        _bfd_mips_elf_discard_info
1858 #define elf_backend_ignore_discarded_relocs \
1859                                         _bfd_mips_elf_ignore_discarded_relocs
1860 #define elf_backend_mips_irix_compat    elf32_mips_irix_compat
1861 #define elf_backend_mips_rtype_to_howto mips_elf32_rtype_to_howto
1862 #define bfd_elf32_bfd_is_local_label_name \
1863                                         mips_elf_is_local_label_name
1864 #define bfd_elf32_find_nearest_line     _bfd_mips_elf_find_nearest_line
1865 #define bfd_elf32_new_section_hook      _bfd_mips_elf_new_section_hook
1866 #define bfd_elf32_set_section_contents  _bfd_mips_elf_set_section_contents
1867 #define bfd_elf32_bfd_get_relocated_section_contents \
1868                                 _bfd_elf_mips_get_relocated_section_contents
1869 #define bfd_elf32_bfd_link_hash_table_create \
1870                                         _bfd_mips_elf_link_hash_table_create
1871 #define bfd_elf32_bfd_final_link        _bfd_mips_elf_final_link
1872 #define bfd_elf32_bfd_merge_private_bfd_data \
1873                                         _bfd_mips_elf_merge_private_bfd_data
1874 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
1875 #define bfd_elf32_bfd_print_private_bfd_data \
1876                                         _bfd_mips_elf_print_private_bfd_data
1877
1878 /* Support for SGI-ish mips targets.  */
1879 #define TARGET_LITTLE_SYM               bfd_elf32_littlemips_vec
1880 #define TARGET_LITTLE_NAME              "elf32-littlemips"
1881 #define TARGET_BIG_SYM                  bfd_elf32_bigmips_vec
1882 #define TARGET_BIG_NAME                 "elf32-bigmips"
1883
1884 #include "elf32-target.h"
1885
1886 /* Support for traditional mips targets.  */
1887 #define INCLUDED_TARGET_FILE            /* More a type of flag.  */
1888
1889 #undef TARGET_LITTLE_SYM
1890 #undef TARGET_LITTLE_NAME
1891 #undef TARGET_BIG_SYM
1892 #undef TARGET_BIG_NAME
1893
1894 #define TARGET_LITTLE_SYM               bfd_elf32_tradlittlemips_vec
1895 #define TARGET_LITTLE_NAME              "elf32-tradlittlemips"
1896 #define TARGET_BIG_SYM                  bfd_elf32_tradbigmips_vec
1897 #define TARGET_BIG_NAME                 "elf32-tradbigmips"
1898
1899 /* Include the target file again for this target.  */
1900 #include "elf32-target.h"