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