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