]> Git Repo - binutils.git/blob - bfd/elf32-mips.c
* elf32-mips.c (elf_mips_ctor64_howto): Set complain_on_overflow
[binutils.git] / bfd / elf32-mips.c
1 /* MIPS-specific support for 32-bit ELF
2    Copyright 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
3
4    Most of the information added by Ian Lance Taylor, Cygnus Support,
5    <[email protected]>.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 /* This file handles MIPS ELF targets.  SGI Irix 5 uses a slightly
24    different MIPS ELF from other targets.  This matters when linking.
25    This file supports both, switching at runtime.  */
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
30 #include "bfdlink.h"
31 #include "genlink.h"
32 #include "elf-bfd.h"
33 #include "elf/mips.h"
34
35 /* Get the ECOFF swapping routines.  */
36 #include "coff/sym.h"
37 #include "coff/symconst.h"
38 #include "coff/internal.h"
39 #include "coff/ecoff.h"
40 #include "coff/mips.h"
41 #define ECOFF_32
42 #include "ecoffswap.h"
43
44 static bfd_reloc_status_type mips32_64bit_reloc
45   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
47   PARAMS ((bfd *, bfd_reloc_code_real_type));
48 static void mips_info_to_howto_rel
49   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
50 static void bfd_mips_elf32_swap_gptab_in
51   PARAMS ((bfd *, const Elf32_External_gptab *, Elf32_gptab *));
52 static void bfd_mips_elf32_swap_gptab_out
53   PARAMS ((bfd *, const Elf32_gptab *, Elf32_External_gptab *));
54 static boolean mips_elf_sym_is_global PARAMS ((bfd *, asymbol *));
55 static boolean mips_elf32_object_p PARAMS ((bfd *));
56 static boolean mips_elf_create_procedure_table
57   PARAMS ((PTR, bfd *, struct bfd_link_info *, asection *,
58            struct ecoff_debug_info *));
59 static int mips_elf_additional_program_headers PARAMS ((bfd *));
60 static boolean mips_elf_modify_segment_map PARAMS ((bfd *));
61 static INLINE int elf_mips_isa PARAMS ((flagword));
62 static boolean mips_elf32_section_from_shdr
63   PARAMS ((bfd *, Elf32_Internal_Shdr *, char *));
64 static boolean mips_elf32_section_processing
65   PARAMS ((bfd *, Elf32_Internal_Shdr *));
66 static boolean mips_elf_is_local_label_name
67   PARAMS ((bfd *, const char *));
68 static struct bfd_hash_entry *mips_elf_link_hash_newfunc
69   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
70 static struct bfd_link_hash_table *mips_elf_link_hash_table_create
71   PARAMS ((bfd *));
72 static int gptab_compare PARAMS ((const void *, const void *));
73 static boolean mips_elf_final_link
74   PARAMS ((bfd *, struct bfd_link_info *));
75 static void mips_elf_relocate_hi16
76   PARAMS ((bfd *, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *,
77            bfd_vma));
78 static boolean mips_elf_relocate_got_local
79   PARAMS ((bfd *, bfd *, asection *, Elf_Internal_Rela *,
80            Elf_Internal_Rela *, bfd_byte *, bfd_vma));
81 static void mips_elf_relocate_global_got
82    PARAMS ((bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma));
83 static bfd_reloc_status_type mips16_jump_reloc
84   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
85 static bfd_reloc_status_type mips16_gprel_reloc
86   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
87 static boolean mips_elf_adjust_dynindx
88   PARAMS ((struct elf_link_hash_entry *, PTR));
89 static boolean mips_elf_relocate_section
90   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
91            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
92 static boolean mips_elf_link_output_symbol_hook
93   PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
94            asection *));
95 static boolean mips_elf_create_dynamic_sections
96   PARAMS ((bfd *, struct bfd_link_info *));
97 static boolean mips_elf_create_compact_rel_section
98   PARAMS ((bfd *, struct bfd_link_info *));
99 static boolean mips_elf_create_got_section
100   PARAMS ((bfd *, struct bfd_link_info *));
101 static boolean mips_elf_check_relocs
102   PARAMS ((bfd *, struct bfd_link_info *, asection *,
103            const Elf_Internal_Rela *));
104 static boolean mips_elf_adjust_dynamic_symbol
105   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
106 static boolean mips_elf_always_size_sections
107   PARAMS ((bfd *, struct bfd_link_info *));
108 static boolean mips_elf_size_dynamic_sections
109   PARAMS ((bfd *, struct bfd_link_info *));
110 static boolean mips_elf_finish_dynamic_symbol
111   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
112            Elf_Internal_Sym *));
113 static boolean mips_elf_finish_dynamic_sections
114   PARAMS ((bfd *, struct bfd_link_info *));
115 static boolean mips_elf_add_symbol_hook
116   PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
117            const char **, flagword *, asection **, bfd_vma *));
118 static bfd_reloc_status_type mips_elf_final_gp
119   PARAMS ((bfd *, asymbol *, boolean, char **, bfd_vma *));
120 static bfd_byte *elf32_mips_get_relocated_section_contents
121   PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
122            bfd_byte *, boolean, asymbol **));
123
124 /* This is true for Irix 5 executables, false for normal MIPS ELF ABI
125    executables.  FIXME: At the moment, we default to always generating
126    Irix 5 executables.  */
127
128 #define SGI_COMPAT(abfd) (1)
129
130 /* This structure is used to hold .got information when linking.  It
131    is stored in the tdata field of the bfd_elf_section_data structure.  */
132
133 struct mips_got_info
134 {
135   /* The symbol index of the first global .got symbol.  */
136   unsigned long global_gotsym;
137   /* The number of local .got entries.  */
138   unsigned int local_gotno;
139   /* The number of local .got entries we have used.  */
140   unsigned int assigned_gotno;
141 };
142
143 /* The number of local .got entries we reserve.  */
144 #define MIPS_RESERVED_GOTNO (2)
145
146 /* Instructions which appear in a stub.  For some reason the stub is
147    slightly different on an SGI system.  */
148 #define ELF_MIPS_GP_OFFSET(abfd) (SGI_COMPAT (abfd) ? 0x7ff0 : 0x8000)
149 #define STUB_LW(abfd)                                   \
150   (SGI_COMPAT (abfd)                                    \
151    ? 0x8f998010                 /* lw t9,0x8010(gp) */  \
152    : 0x8f998000)                /* lw t9,0x8000(gp) */
153 #define STUB_MOVE 0x03e07825    /* move t7,ra */
154 #define STUB_JALR 0x0320f809    /* jal t9 */
155 #define STUB_LI16 0x34180000    /* ori t8,zero,0 */
156 #define MIPS_FUNCTION_STUB_SIZE (16)
157
158 /* Names of sections which appear in the .dynsym section in an Irix 5
159    executable.  */
160
161 static const char * const mips_elf_dynsym_sec_names[] =
162 {
163   ".text",
164   ".init",
165   ".fini",
166   ".data",
167   ".rodata",
168   ".sdata",
169   ".sbss",
170   ".bss",
171   NULL
172 };
173
174 #define SIZEOF_MIPS_DYNSYM_SECNAMES \
175   (sizeof mips_elf_dynsym_sec_names / sizeof mips_elf_dynsym_sec_names[0])
176
177 /* The number of entries in mips_elf_dynsym_sec_names which go in the
178    text segment.  */
179
180 #define MIPS_TEXT_DYNSYM_SECNO (3)
181
182 /* The names of the runtime procedure table symbols used on Irix 5.  */
183
184 static const char * const mips_elf_dynsym_rtproc_names[] =
185 {
186   "_procedure_table",
187   "_procedure_string_table",
188   "_procedure_table_size",
189   NULL
190 };
191
192 /* These structures are used to generate the .compact_rel section on
193    Irix 5.  */
194
195 typedef struct
196 {
197   unsigned long id1;            /* Always one?  */
198   unsigned long num;            /* Number of compact relocation entries.  */
199   unsigned long id2;            /* Always two?  */
200   unsigned long offset;         /* The file offset of the first relocation.  */
201   unsigned long reserved0;      /* Zero?  */
202   unsigned long reserved1;      /* Zero?  */
203 } Elf32_compact_rel;
204
205 typedef struct
206 {
207   bfd_byte id1[4];
208   bfd_byte num[4];
209   bfd_byte id2[4];
210   bfd_byte offset[4];
211   bfd_byte reserved0[4];
212   bfd_byte reserved1[4];
213 } Elf32_External_compact_rel;
214
215 typedef struct
216 {
217   unsigned int ctype : 1;       /* 1: long 0: short format. See below.  */
218   unsigned int rtype : 4;       /* Relocation types. See below. */
219   unsigned int dist2to : 8;
220   unsigned int relvaddr : 19;   /* (VADDR - vaddr of the previous entry)/ 4 */
221   unsigned long konst;          /* KONST field. See below.  */
222   unsigned long vaddr;          /* VADDR to be relocated.  */
223 } Elf32_crinfo;
224
225 typedef struct
226 {
227   unsigned int ctype : 1;       /* 1: long 0: short format. See below.  */
228   unsigned int rtype : 4;       /* Relocation types. See below. */
229   unsigned int dist2to : 8;
230   unsigned int relvaddr : 19;   /* (VADDR - vaddr of the previous entry)/ 4 */
231   unsigned long konst;          /* KONST field. See below.  */
232 } Elf32_crinfo2;
233
234 typedef struct
235 {
236   bfd_byte info[4];
237   bfd_byte konst[4];
238   bfd_byte vaddr[4];
239 } Elf32_External_crinfo;
240
241 typedef struct
242 {
243   bfd_byte info[4];
244   bfd_byte konst[4];
245 } Elf32_External_crinfo2;
246
247 /* These are the constants used to swap the bitfields in a crinfo.  */
248
249 #define CRINFO_CTYPE (0x1)
250 #define CRINFO_CTYPE_SH (31)
251 #define CRINFO_RTYPE (0xf)
252 #define CRINFO_RTYPE_SH (27)
253 #define CRINFO_DIST2TO (0xff)
254 #define CRINFO_DIST2TO_SH (19)
255 #define CRINFO_RELVADDR (0x7ffff)
256 #define CRINFO_RELVADDR_SH (0)
257
258 /* A compact relocation info has long (3 words) or short (2 words)
259    formats.  A short format doesn't have VADDR field and relvaddr
260    fields contains ((VADDR - vaddr of the previous entry) >> 2).  */
261 #define CRF_MIPS_LONG                   1
262 #define CRF_MIPS_SHORT                  0
263
264 /* There are 4 types of compact relocation at least. The value KONST
265    has different meaning for each type:
266
267    (type)               (konst)
268    CT_MIPS_REL32        Address in data
269    CT_MIPS_WORD         Address in word (XXX)
270    CT_MIPS_GPHI_LO      GP - vaddr
271    CT_MIPS_JMPAD        Address to jump
272    */
273
274 #define CRT_MIPS_REL32                  0xa
275 #define CRT_MIPS_WORD                   0xb
276 #define CRT_MIPS_GPHI_LO                0xc
277 #define CRT_MIPS_JMPAD                  0xd
278
279 #define mips_elf_set_cr_format(x,format)        ((x).ctype = (format))
280 #define mips_elf_set_cr_type(x,type)            ((x).rtype = (type))
281 #define mips_elf_set_cr_dist2to(x,v)            ((x).dist2to = (v))
282 #define mips_elf_set_cr_relvaddr(x,d)           ((x).relvaddr = (d)<<2)
283
284 static void bfd_elf32_swap_compact_rel_out
285   PARAMS ((bfd *, const Elf32_compact_rel *, Elf32_External_compact_rel *));
286 static void bfd_elf32_swap_crinfo_out
287   PARAMS ((bfd *, const Elf32_crinfo *, Elf32_External_crinfo *));
288
289 #define USE_REL 1               /* MIPS uses REL relocations instead of RELA */
290
291 enum reloc_type
292 {
293   R_MIPS_NONE = 0,
294   R_MIPS_16,            R_MIPS_32,
295   R_MIPS_REL32,         R_MIPS_26,
296   R_MIPS_HI16,          R_MIPS_LO16,
297   R_MIPS_GPREL16,       R_MIPS_LITERAL,
298   R_MIPS_GOT16,         R_MIPS_PC16,
299   R_MIPS_CALL16,        R_MIPS_GPREL32,
300   /* The remaining relocs are defined on Irix, although they are not
301      in the MIPS ELF ABI.  */
302   R_MIPS_UNUSED1,       R_MIPS_UNUSED2,
303   R_MIPS_UNUSED3,
304   R_MIPS_SHIFT5,        R_MIPS_SHIFT6,
305   R_MIPS_64,            R_MIPS_GOT_DISP,
306   R_MIPS_GOT_PAGE,      R_MIPS_GOT_OFST,
307   R_MIPS_GOT_HI16,      R_MIPS_GOT_LO16,
308   R_MIPS_SUB,           R_MIPS_INSERT_A,
309   R_MIPS_INSERT_B,      R_MIPS_DELETE,
310   R_MIPS_HIGHER,        R_MIPS_HIGHEST,
311   R_MIPS_CALL_HI16,     R_MIPS_CALL_LO16,
312   R_MIPS_max,
313   /* These relocs are used for the mips16.  */
314   R_MIPS16_26 = 100,
315   R_MIPS16_GPREL = 101
316 };
317
318 static reloc_howto_type elf_mips_howto_table[] =
319 {
320   /* No relocation.  */
321   HOWTO (R_MIPS_NONE,           /* type */
322          0,                     /* rightshift */
323          0,                     /* size (0 = byte, 1 = short, 2 = long) */
324          0,                     /* bitsize */
325          false,                 /* pc_relative */
326          0,                     /* bitpos */
327          complain_overflow_dont, /* complain_on_overflow */
328          bfd_elf_generic_reloc, /* special_function */
329          "R_MIPS_NONE",         /* name */
330          false,                 /* partial_inplace */
331          0,                     /* src_mask */
332          0,                     /* dst_mask */
333          false),                /* pcrel_offset */
334
335   /* 16 bit relocation.  */
336   HOWTO (R_MIPS_16,             /* type */
337          0,                     /* rightshift */
338          1,                     /* size (0 = byte, 1 = short, 2 = long) */
339          16,                    /* bitsize */
340          false,                 /* pc_relative */
341          0,                     /* bitpos */
342          complain_overflow_bitfield, /* complain_on_overflow */
343          bfd_elf_generic_reloc, /* special_function */
344          "R_MIPS_16",           /* name */
345          true,                  /* partial_inplace */
346          0xffff,                /* src_mask */
347          0xffff,                /* dst_mask */
348          false),                /* pcrel_offset */
349
350   /* 32 bit relocation.  */
351   HOWTO (R_MIPS_32,             /* type */
352          0,                     /* rightshift */
353          2,                     /* size (0 = byte, 1 = short, 2 = long) */
354          32,                    /* bitsize */
355          false,                 /* pc_relative */
356          0,                     /* bitpos */
357          complain_overflow_bitfield, /* complain_on_overflow */
358          bfd_elf_generic_reloc, /* special_function */
359          "R_MIPS_32",           /* name */
360          true,                  /* partial_inplace */
361          0xffffffff,            /* src_mask */
362          0xffffffff,            /* dst_mask */
363          false),                /* pcrel_offset */
364
365   /* 32 bit symbol relative relocation.  */
366   HOWTO (R_MIPS_REL32,          /* type */
367          0,                     /* rightshift */
368          2,                     /* size (0 = byte, 1 = short, 2 = long) */
369          32,                    /* bitsize */
370          false,                 /* pc_relative */
371          0,                     /* bitpos */
372          complain_overflow_bitfield, /* complain_on_overflow */
373          bfd_elf_generic_reloc, /* special_function */
374          "R_MIPS_REL32",        /* name */
375          true,                  /* partial_inplace */
376          0xffffffff,            /* src_mask */
377          0xffffffff,            /* dst_mask */
378          false),                /* pcrel_offset */
379
380   /* 26 bit branch address.  */
381   HOWTO (R_MIPS_26,             /* type */
382          2,                     /* rightshift */
383          2,                     /* size (0 = byte, 1 = short, 2 = long) */
384          26,                    /* bitsize */
385          false,                 /* pc_relative */
386          0,                     /* bitpos */
387          complain_overflow_dont, /* complain_on_overflow */
388                                 /* This needs complex overflow
389                                    detection, because the upper four
390                                    bits must match the PC.  */
391          bfd_elf_generic_reloc, /* special_function */
392          "R_MIPS_26",           /* name */
393          true,                  /* partial_inplace */
394          0x3ffffff,             /* src_mask */
395          0x3ffffff,             /* dst_mask */
396          false),                /* pcrel_offset */
397
398   /* High 16 bits of symbol value.  */
399   HOWTO (R_MIPS_HI16,           /* type */
400          0,                     /* rightshift */
401          2,                     /* size (0 = byte, 1 = short, 2 = long) */
402          16,                    /* bitsize */
403          false,                 /* pc_relative */
404          0,                     /* bitpos */
405          complain_overflow_dont, /* complain_on_overflow */
406          _bfd_mips_elf_hi16_reloc,      /* special_function */
407          "R_MIPS_HI16",         /* name */
408          true,                  /* partial_inplace */
409          0xffff,                /* src_mask */
410          0xffff,                /* dst_mask */
411          false),                /* pcrel_offset */
412
413   /* Low 16 bits of symbol value.  */
414   HOWTO (R_MIPS_LO16,           /* type */
415          0,                     /* rightshift */
416          2,                     /* size (0 = byte, 1 = short, 2 = long) */
417          16,                    /* bitsize */
418          false,                 /* pc_relative */
419          0,                     /* bitpos */
420          complain_overflow_dont, /* complain_on_overflow */
421          _bfd_mips_elf_lo16_reloc,      /* special_function */
422          "R_MIPS_LO16",         /* name */
423          true,                  /* partial_inplace */
424          0xffff,                /* src_mask */
425          0xffff,                /* dst_mask */
426          false),                /* pcrel_offset */
427
428   /* GP relative reference.  */
429   HOWTO (R_MIPS_GPREL16,        /* type */
430          0,                     /* rightshift */
431          2,                     /* size (0 = byte, 1 = short, 2 = long) */
432          16,                    /* bitsize */
433          false,                 /* pc_relative */
434          0,                     /* bitpos */
435          complain_overflow_signed, /* complain_on_overflow */
436          _bfd_mips_elf_gprel16_reloc, /* special_function */
437          "R_MIPS_GPREL16",      /* name */
438          true,                  /* partial_inplace */
439          0xffff,                /* src_mask */
440          0xffff,                /* dst_mask */
441          false),                /* pcrel_offset */
442
443   /* Reference to literal section.  */
444   HOWTO (R_MIPS_LITERAL,        /* type */
445          0,                     /* rightshift */
446          2,                     /* size (0 = byte, 1 = short, 2 = long) */
447          16,                    /* bitsize */
448          false,                 /* pc_relative */
449          0,                     /* bitpos */
450          complain_overflow_signed, /* complain_on_overflow */
451          _bfd_mips_elf_gprel16_reloc, /* special_function */
452          "R_MIPS_LITERAL",      /* name */
453          true,                  /* partial_inplace */
454          0xffff,                /* src_mask */
455          0xffff,                /* dst_mask */
456          false),                /* pcrel_offset */
457
458   /* Reference to global offset table.  */
459   HOWTO (R_MIPS_GOT16,          /* 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_signed, /* complain_on_overflow */
466          _bfd_mips_elf_got16_reloc,     /* special_function */
467          "R_MIPS_GOT16",        /* name */
468          false,                 /* partial_inplace */
469          0,                     /* src_mask */
470          0xffff,                /* dst_mask */
471          false),                /* pcrel_offset */
472
473   /* 16 bit PC relative reference.  */
474   HOWTO (R_MIPS_PC16,           /* type */
475          0,                     /* rightshift */
476          2,                     /* size (0 = byte, 1 = short, 2 = long) */
477          16,                    /* bitsize */
478          true,                  /* pc_relative */
479          0,                     /* bitpos */
480          complain_overflow_signed, /* complain_on_overflow */
481          bfd_elf_generic_reloc, /* special_function */
482          "R_MIPS_PC16",         /* name */
483          true,                  /* partial_inplace */
484          0xffff,                /* src_mask */
485          0xffff,                /* dst_mask */
486          false),                /* pcrel_offset */
487
488   /* 16 bit call through global offset table.  */
489   /* FIXME: This is not handled correctly.  */
490   HOWTO (R_MIPS_CALL16,         /* type */
491          0,                     /* rightshift */
492          2,                     /* size (0 = byte, 1 = short, 2 = long) */
493          16,                    /* bitsize */
494          false,                 /* pc_relative */
495          0,                     /* bitpos */
496          complain_overflow_signed, /* complain_on_overflow */
497          bfd_elf_generic_reloc, /* special_function */
498          "R_MIPS_CALL16",       /* name */
499          false,                 /* partial_inplace */
500          0,                     /* src_mask */
501          0xffff,                /* dst_mask */
502          false),                /* pcrel_offset */
503
504   /* 32 bit GP relative reference.  */
505   HOWTO (R_MIPS_GPREL32,        /* type */
506          0,                     /* rightshift */
507          2,                     /* size (0 = byte, 1 = short, 2 = long) */
508          32,                    /* bitsize */
509          false,                 /* pc_relative */
510          0,                     /* bitpos */
511          complain_overflow_bitfield, /* complain_on_overflow */
512          _bfd_mips_elf_gprel32_reloc, /* special_function */
513          "R_MIPS_GPREL32",      /* name */
514          true,                  /* partial_inplace */
515          0xffffffff,            /* src_mask */
516          0xffffffff,            /* dst_mask */
517          false),                /* pcrel_offset */
518
519     /* The remaining relocs are defined on Irix 5, although they are
520        not defined by the ABI.  */
521     { 13 },
522     { 14 },
523     { 15 },
524
525   /* A 5 bit shift field.  */
526   HOWTO (R_MIPS_SHIFT5,         /* type */
527          0,                     /* rightshift */
528          2,                     /* size (0 = byte, 1 = short, 2 = long) */
529          5,                     /* bitsize */
530          false,                 /* pc_relative */
531          6,                     /* bitpos */
532          complain_overflow_bitfield, /* complain_on_overflow */
533          bfd_elf_generic_reloc, /* special_function */
534          "R_MIPS_SHIFT5",       /* name */
535          true,                  /* partial_inplace */
536          0x000007c0,            /* src_mask */
537          0x000007c0,            /* dst_mask */
538          false),                /* pcrel_offset */
539
540   /* A 6 bit shift field.  */
541   /* FIXME: This is not handled correctly; a special function is
542      needed to put the most significant bit in the right place.  */
543   HOWTO (R_MIPS_SHIFT6,         /* type */
544          0,                     /* rightshift */
545          2,                     /* size (0 = byte, 1 = short, 2 = long) */
546          6,                     /* bitsize */
547          false,                 /* pc_relative */
548          6,                     /* bitpos */
549          complain_overflow_bitfield, /* complain_on_overflow */
550          bfd_elf_generic_reloc, /* special_function */
551          "R_MIPS_SHIFT6",       /* name */
552          true,                  /* partial_inplace */
553          0x000007c4,            /* src_mask */
554          0x000007c4,            /* dst_mask */
555          false),                /* pcrel_offset */
556
557   /* A 64 bit relocation.  This is used in 32 bit ELF when addresses
558      are 64 bits long; the upper 32 bits are simply a sign extension.
559      The fields of the howto should be the same as for R_MIPS_32,
560      other than the type, name, and special_function.  */
561   HOWTO (R_MIPS_64,             /* type */
562          0,                     /* rightshift */
563          2,                     /* size (0 = byte, 1 = short, 2 = long) */
564          32,                    /* bitsize */
565          false,                 /* pc_relative */
566          0,                     /* bitpos */
567          complain_overflow_bitfield, /* complain_on_overflow */
568          mips32_64bit_reloc,    /* special_function */
569          "R_MIPS_64",           /* name */
570          true,                  /* partial_inplace */
571          0xffffffff,            /* src_mask */
572          0xffffffff,            /* dst_mask */
573          false),                /* pcrel_offset */
574
575   /* Displacement in the global offset table.  */
576   /* FIXME: Not handled correctly.  */
577   HOWTO (R_MIPS_GOT_DISP,       /* type */
578          0,                     /* rightshift */
579          2,                     /* size (0 = byte, 1 = short, 2 = long) */
580          16,                    /* bitsize */
581          false,                 /* pc_relative */
582          0,                     /* bitpos */
583          complain_overflow_bitfield, /* complain_on_overflow */
584          bfd_elf_generic_reloc, /* special_function */
585          "R_MIPS_GOT_DISP",     /* name */
586          true,                  /* partial_inplace */
587          0x0000ffff,            /* src_mask */
588          0x0000ffff,            /* dst_mask */
589          false),                /* pcrel_offset */
590
591   /* Displacement to page pointer in the global offset table.  */
592   /* FIXME: Not handled correctly.  */
593   HOWTO (R_MIPS_GOT_PAGE,       /* 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_bitfield, /* complain_on_overflow */
600          bfd_elf_generic_reloc, /* special_function */
601          "R_MIPS_GOT_PAGE",     /* name */
602          true,                  /* partial_inplace */
603          0x0000ffff,            /* src_mask */
604          0x0000ffff,            /* dst_mask */
605          false),                /* pcrel_offset */
606
607   /* Offset from page pointer in the global offset table.  */
608   /* FIXME: Not handled correctly.  */
609   HOWTO (R_MIPS_GOT_OFST,       /* type */
610          0,                     /* rightshift */
611          2,                     /* size (0 = byte, 1 = short, 2 = long) */
612          16,                    /* bitsize */
613          false,                 /* pc_relative */
614          0,                     /* bitpos */
615          complain_overflow_bitfield, /* complain_on_overflow */
616          bfd_elf_generic_reloc, /* special_function */
617          "R_MIPS_GOT_OFST",     /* name */
618          true,                  /* partial_inplace */
619          0x0000ffff,            /* src_mask */
620          0x0000ffff,            /* dst_mask */
621          false),                /* pcrel_offset */
622
623   /* High 16 bits of displacement in global offset table.  */
624   /* FIXME: Not handled correctly.  */
625   HOWTO (R_MIPS_GOT_HI16,       /* type */
626          0,                     /* rightshift */
627          2,                     /* size (0 = byte, 1 = short, 2 = long) */
628          16,                    /* bitsize */
629          false,                 /* pc_relative */
630          0,                     /* bitpos */
631          complain_overflow_dont, /* complain_on_overflow */
632          bfd_elf_generic_reloc, /* special_function */
633          "R_MIPS_GOT_HI16",     /* name */
634          true,                  /* partial_inplace */
635          0x0000ffff,            /* src_mask */
636          0x0000ffff,            /* dst_mask */
637          false),                /* pcrel_offset */
638
639   /* Low 16 bits of displacement in global offset table.  */
640   /* FIXME: Not handled correctly.  */
641   HOWTO (R_MIPS_GOT_LO16,       /* type */
642          0,                     /* rightshift */
643          2,                     /* size (0 = byte, 1 = short, 2 = long) */
644          16,                    /* bitsize */
645          false,                 /* pc_relative */
646          0,                     /* bitpos */
647          complain_overflow_dont, /* complain_on_overflow */
648          bfd_elf_generic_reloc, /* special_function */
649          "R_MIPS_GOT_LO16",     /* name */
650          true,                  /* partial_inplace */
651          0x0000ffff,            /* src_mask */
652          0x0000ffff,            /* dst_mask */
653          false),                /* pcrel_offset */
654
655   /* 64 bit subtraction.  Presumably not used in 32 bit ELF.  */
656   { R_MIPS_SUB },
657
658   /* Used to cause the linker to insert and delete instructions?  */
659   { R_MIPS_INSERT_A },
660   { R_MIPS_INSERT_B },
661   { R_MIPS_DELETE },
662
663   /* Get the higher values of a 64 bit addend.  Presumably not used in
664      32 bit ELF.  */
665   { R_MIPS_HIGHER },
666   { R_MIPS_HIGHEST },
667
668   /* High 16 bits of displacement in global offset table.  */
669   /* FIXME: Not handled correctly.  */
670   HOWTO (R_MIPS_CALL_HI16,      /* type */
671          0,                     /* rightshift */
672          2,                     /* size (0 = byte, 1 = short, 2 = long) */
673          16,                    /* bitsize */
674          false,                 /* pc_relative */
675          0,                     /* bitpos */
676          complain_overflow_dont, /* complain_on_overflow */
677          bfd_elf_generic_reloc, /* special_function */
678          "R_MIPS_CALL_HI16",    /* name */
679          true,                  /* partial_inplace */
680          0x0000ffff,            /* src_mask */
681          0x0000ffff,            /* dst_mask */
682          false),                /* pcrel_offset */
683
684   /* Low 16 bits of displacement in global offset table.  */
685   /* FIXME: Not handled correctly.  */
686   HOWTO (R_MIPS_CALL_LO16,      /* type */
687          0,                     /* rightshift */
688          2,                     /* size (0 = byte, 1 = short, 2 = long) */
689          16,                    /* bitsize */
690          false,                 /* pc_relative */
691          0,                     /* bitpos */
692          complain_overflow_dont, /* complain_on_overflow */
693          bfd_elf_generic_reloc, /* special_function */
694          "R_MIPS_CALL_LO16",    /* name */
695          true,                  /* partial_inplace */
696          0x0000ffff,            /* src_mask */
697          0x0000ffff,            /* dst_mask */
698          false)                 /* pcrel_offset */
699 };
700
701 /* The reloc used for BFD_RELOC_CTOR when doing a 64 bit link.  This
702    is a hack to make the linker think that we need 64 bit values.  */
703 static reloc_howto_type elf_mips_ctor64_howto =
704   HOWTO (R_MIPS_64,             /* type */
705          0,                     /* rightshift */
706          4,                     /* size (0 = byte, 1 = short, 2 = long) */
707          32,                    /* bitsize */
708          false,                 /* pc_relative */
709          0,                     /* bitpos */
710          complain_overflow_signed, /* complain_on_overflow */
711          mips32_64bit_reloc,    /* special_function */
712          "R_MIPS_64",           /* name */
713          true,                  /* partial_inplace */
714          0xffffffff,            /* src_mask */
715          0xffffffff,            /* dst_mask */
716          false);                /* pcrel_offset */
717
718 /* The reloc used for the mips16 jump instruction.  */
719 static reloc_howto_type elf_mips16_jump_howto =
720   HOWTO (R_MIPS16_26,           /* type */
721          2,                     /* rightshift */
722          2,                     /* size (0 = byte, 1 = short, 2 = long) */
723          26,                    /* bitsize */
724          false,                 /* pc_relative */
725          0,                     /* bitpos */
726          complain_overflow_dont, /* complain_on_overflow */
727                                 /* This needs complex overflow
728                                    detection, because the upper four
729                                    bits must match the PC.  */
730          mips16_jump_reloc,     /* special_function */
731          "R_MIPS16_26",         /* name */
732          true,                  /* partial_inplace */
733          0x3ffffff,             /* src_mask */
734          0x3ffffff,             /* dst_mask */
735          false);                /* pcrel_offset */
736
737 /* The reloc used for the mips16 gprel instruction.  The src_mask and
738    dsk_mask for this howto do not reflect the actual instruction, in
739    which the value is not contiguous; the masks are for the
740    convenience of the relocate_section routine.  */
741 static reloc_howto_type elf_mips16_gprel_howto =
742   HOWTO (R_MIPS16_GPREL,        /* type */
743          0,                     /* rightshift */
744          2,                     /* size (0 = byte, 1 = short, 2 = long) */
745          16,                    /* bitsize */
746          false,                 /* pc_relative */
747          0,                     /* bitpos */
748          complain_overflow_signed, /* complain_on_overflow */
749          mips16_gprel_reloc,    /* special_function */
750          "R_MIPS16_GPREL",      /* name */
751          true,                  /* partial_inplace */
752          0xffff,                /* src_mask */
753          0xffff,                /* dst_mask */
754          false);                /* pcrel_offset */
755
756 /* Do a R_MIPS_HI16 relocation.  This has to be done in combination
757    with a R_MIPS_LO16 reloc, because there is a carry from the LO16 to
758    the HI16.  Here we just save the information we need; we do the
759    actual relocation when we see the LO16.  MIPS ELF requires that the
760    LO16 immediately follow the HI16.  As a GNU extension, we permit an
761    arbitrary number of HI16 relocs to be associated with a single LO16
762    reloc.  This extension permits gcc to output the HI and LO relocs
763    itself.  */
764
765 struct mips_hi16
766 {
767   struct mips_hi16 *next;
768   bfd_byte *addr;
769   bfd_vma addend;
770 };
771
772 /* FIXME: This should not be a static variable.  */
773
774 static struct mips_hi16 *mips_hi16_list;
775
776 bfd_reloc_status_type
777 _bfd_mips_elf_hi16_reloc (abfd,
778                      reloc_entry,
779                      symbol,
780                      data,
781                      input_section,
782                      output_bfd,
783                      error_message)
784      bfd *abfd;
785      arelent *reloc_entry;
786      asymbol *symbol;
787      PTR data;
788      asection *input_section;
789      bfd *output_bfd;
790      char **error_message;
791 {
792   bfd_reloc_status_type ret;
793   bfd_vma relocation;
794   struct mips_hi16 *n;
795
796   /* If we're relocating, and this an external symbol, we don't want
797      to change anything.  */
798   if (output_bfd != (bfd *) NULL
799       && (symbol->flags & BSF_SECTION_SYM) == 0
800       && reloc_entry->addend == 0)
801     {
802       reloc_entry->address += input_section->output_offset;
803       return bfd_reloc_ok;
804     }
805
806   ret = bfd_reloc_ok;
807
808   if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
809     {
810       boolean relocateable;
811       bfd_vma gp;
812
813       if (ret == bfd_reloc_undefined)
814         abort ();
815
816       if (output_bfd != NULL)
817         relocateable = true;
818       else
819         {
820           relocateable = false;
821           output_bfd = symbol->section->output_section->owner;
822         }
823
824       ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
825                                error_message, &gp);
826       if (ret != bfd_reloc_ok)
827         return ret;
828
829       relocation = gp - reloc_entry->address;
830     }
831   else
832     {
833       if (bfd_is_und_section (symbol->section)
834           && output_bfd == (bfd *) NULL)
835         ret = bfd_reloc_undefined;
836
837       if (bfd_is_com_section (symbol->section))
838         relocation = 0;
839       else
840         relocation = symbol->value;
841     }
842
843   relocation += symbol->section->output_section->vma;
844   relocation += symbol->section->output_offset;
845   relocation += reloc_entry->addend;
846
847   if (reloc_entry->address > input_section->_cooked_size)
848     return bfd_reloc_outofrange;
849
850   /* Save the information, and let LO16 do the actual relocation.  */
851   n = (struct mips_hi16 *) bfd_malloc (sizeof *n);
852   if (n == NULL)
853     return bfd_reloc_outofrange;
854   n->addr = (bfd_byte *) data + reloc_entry->address;
855   n->addend = relocation;
856   n->next = mips_hi16_list;
857   mips_hi16_list = n;
858
859   if (output_bfd != (bfd *) NULL)
860     reloc_entry->address += input_section->output_offset;
861
862   return ret;
863 }
864
865 /* Do a R_MIPS_LO16 relocation.  This is a straightforward 16 bit
866    inplace relocation; this function exists in order to do the
867    R_MIPS_HI16 relocation described above.  */
868
869 bfd_reloc_status_type
870 _bfd_mips_elf_lo16_reloc (abfd,
871                      reloc_entry,
872                      symbol,
873                      data,
874                      input_section,
875                      output_bfd,
876                      error_message)
877      bfd *abfd;
878      arelent *reloc_entry;
879      asymbol *symbol;
880      PTR data;
881      asection *input_section;
882      bfd *output_bfd;
883      char **error_message;
884 {
885   arelent gp_disp_relent;
886
887   if (mips_hi16_list != NULL)
888     {
889       struct mips_hi16 *l;
890
891       l = mips_hi16_list;
892       while (l != NULL)
893         {
894           unsigned long insn;
895           unsigned long val;
896           unsigned long vallo;
897           struct mips_hi16 *next;
898
899           /* Do the HI16 relocation.  Note that we actually don't need
900              to know anything about the LO16 itself, except where to
901              find the low 16 bits of the addend needed by the LO16.  */
902           insn = bfd_get_32 (abfd, l->addr);
903           vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
904                    & 0xffff);
905           val = ((insn & 0xffff) << 16) + vallo;
906           val += l->addend;
907
908           /* The low order 16 bits are always treated as a signed
909              value.  Therefore, a negative value in the low order bits
910              requires an adjustment in the high order bits.  We need
911              to make this adjustment in two ways: once for the bits we
912              took from the data, and once for the bits we are putting
913              back in to the data.  */
914           if ((vallo & 0x8000) != 0)
915             val -= 0x10000;
916           if ((val & 0x8000) != 0)
917             val += 0x10000;
918
919           insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
920           bfd_put_32 (abfd, insn, l->addr);
921
922           if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
923             {
924               gp_disp_relent = *reloc_entry;
925               reloc_entry = &gp_disp_relent;
926               reloc_entry->addend = l->addend;
927             }
928
929           next = l->next;
930           free (l);
931           l = next;
932         }
933
934       mips_hi16_list = NULL;
935     }
936   else if (strcmp (bfd_asymbol_name (symbol), "_gp_disp") == 0)
937     {
938       bfd_reloc_status_type ret;
939       bfd_vma gp, relocation;
940
941       /* FIXME: Does this case ever occur?  */
942
943       ret = mips_elf_final_gp (output_bfd, symbol, true, error_message, &gp);
944       if (ret != bfd_reloc_ok)
945         return ret;
946
947       relocation = gp - reloc_entry->address;
948       relocation += symbol->section->output_section->vma;
949       relocation += symbol->section->output_offset;
950       relocation += reloc_entry->addend;
951
952       if (reloc_entry->address > input_section->_cooked_size)
953         return bfd_reloc_outofrange;
954
955       gp_disp_relent = *reloc_entry;
956       reloc_entry = &gp_disp_relent;
957       reloc_entry->addend = relocation - 4;
958     }
959
960   /* Now do the LO16 reloc in the usual way.  */
961   return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
962                                 input_section, output_bfd, error_message);
963 }
964
965 /* Do a R_MIPS_GOT16 reloc.  This is a reloc against the global offset
966    table used for PIC code.  If the symbol is an external symbol, the
967    instruction is modified to contain the offset of the appropriate
968    entry in the global offset table.  If the symbol is a section
969    symbol, the next reloc is a R_MIPS_LO16 reloc.  The two 16 bit
970    addends are combined to form the real addend against the section
971    symbol; the GOT16 is modified to contain the offset of an entry in
972    the global offset table, and the LO16 is modified to offset it
973    appropriately.  Thus an offset larger than 16 bits requires a
974    modified value in the global offset table.
975
976    This implementation suffices for the assembler, but the linker does
977    not yet know how to create global offset tables.  */
978
979 bfd_reloc_status_type
980 _bfd_mips_elf_got16_reloc (abfd,
981                       reloc_entry,
982                       symbol,
983                       data,
984                       input_section,
985                       output_bfd,
986                       error_message)
987      bfd *abfd;
988      arelent *reloc_entry;
989      asymbol *symbol;
990      PTR data;
991      asection *input_section;
992      bfd *output_bfd;
993      char **error_message;
994 {
995   /* If we're relocating, and this an external symbol, we don't want
996      to change anything.  */
997   if (output_bfd != (bfd *) NULL
998       && (symbol->flags & BSF_SECTION_SYM) == 0
999       && reloc_entry->addend == 0)
1000     {
1001       reloc_entry->address += input_section->output_offset;
1002       return bfd_reloc_ok;
1003     }
1004
1005   /* If we're relocating, and this is a local symbol, we can handle it
1006      just like HI16.  */
1007   if (output_bfd != (bfd *) NULL
1008       && (symbol->flags & BSF_SECTION_SYM) != 0)
1009     return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
1010                                      input_section, output_bfd, error_message);
1011
1012   abort ();
1013 }
1014
1015 /* We have to figure out the gp value, so that we can adjust the
1016    symbol value correctly.  We look up the symbol _gp in the output
1017    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
1018    target data.  We don't need to adjust the symbol value for an
1019    external symbol if we are producing relocateable output.  */
1020
1021 static bfd_reloc_status_type
1022 mips_elf_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
1023      bfd *output_bfd;
1024      asymbol *symbol;
1025      boolean relocateable;
1026      char **error_message;
1027      bfd_vma *pgp;
1028 {
1029   if (bfd_is_und_section (symbol->section)
1030       && ! relocateable)
1031     {
1032       *pgp = 0;
1033       return bfd_reloc_undefined;
1034     }
1035
1036   *pgp = _bfd_get_gp_value (output_bfd);
1037   if (*pgp == 0
1038       && (! relocateable
1039           || (symbol->flags & BSF_SECTION_SYM) != 0))
1040     {
1041       if (relocateable)
1042         {
1043           /* Make up a value.  */
1044           *pgp = symbol->section->output_section->vma + 0x4000;
1045           _bfd_set_gp_value (output_bfd, *pgp);
1046         }
1047       else
1048         {
1049           unsigned int count;
1050           asymbol **sym;
1051           unsigned int i;
1052
1053           count = bfd_get_symcount (output_bfd);
1054           sym = bfd_get_outsymbols (output_bfd);
1055
1056           if (sym == (asymbol **) NULL)
1057             i = count;
1058           else
1059             {
1060               for (i = 0; i < count; i++, sym++)
1061                 {
1062                   register CONST char *name;
1063
1064                   name = bfd_asymbol_name (*sym);
1065                   if (*name == '_' && strcmp (name, "_gp") == 0)
1066                     {
1067                       *pgp = bfd_asymbol_value (*sym);
1068                       _bfd_set_gp_value (output_bfd, *pgp);
1069                       break;
1070                     }
1071                 }
1072             }
1073
1074           if (i >= count)
1075             {
1076               /* Only get the error once.  */
1077               *pgp = 4;
1078               _bfd_set_gp_value (output_bfd, *pgp);
1079               *error_message =
1080                 (char *) "GP relative relocation when _gp not defined";
1081               return bfd_reloc_dangerous;
1082             }
1083         }
1084     }
1085
1086   return bfd_reloc_ok;
1087 }
1088
1089 /* Do a R_MIPS_GPREL16 relocation.  This is a 16 bit value which must
1090    become the offset from the gp register.  This function also handles
1091    R_MIPS_LITERAL relocations, although those can be handled more
1092    cleverly because the entries in the .lit8 and .lit4 sections can be
1093    merged.  */
1094
1095 static bfd_reloc_status_type gprel16_with_gp PARAMS ((bfd *, asymbol *,
1096                                                       arelent *, asection *,
1097                                                       boolean, PTR, bfd_vma));
1098
1099 bfd_reloc_status_type
1100 _bfd_mips_elf_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
1101                              output_bfd, error_message)
1102      bfd *abfd;
1103      arelent *reloc_entry;
1104      asymbol *symbol;
1105      PTR data;
1106      asection *input_section;
1107      bfd *output_bfd;
1108      char **error_message;
1109 {
1110   boolean relocateable;
1111   bfd_reloc_status_type ret;
1112   bfd_vma gp;
1113
1114   /* If we're relocating, and this is an external symbol with no
1115      addend, we don't want to change anything.  We will only have an
1116      addend if this is a newly created reloc, not read from an ELF
1117      file.  */
1118   if (output_bfd != (bfd *) NULL
1119       && (symbol->flags & BSF_SECTION_SYM) == 0
1120       && reloc_entry->addend == 0)
1121     {
1122       reloc_entry->address += input_section->output_offset;
1123       return bfd_reloc_ok;
1124     }
1125
1126   if (output_bfd != (bfd *) NULL)
1127     relocateable = true;
1128   else
1129     {
1130       relocateable = false;
1131       output_bfd = symbol->section->output_section->owner;
1132     }
1133
1134   ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1135                            &gp);
1136   if (ret != bfd_reloc_ok)
1137     return ret;
1138
1139   return gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1140                           relocateable, data, gp);
1141 }
1142
1143 static bfd_reloc_status_type
1144 gprel16_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1145                  gp)
1146      bfd *abfd;
1147      asymbol *symbol;
1148      arelent *reloc_entry;
1149      asection *input_section;
1150      boolean relocateable;
1151      PTR data;
1152      bfd_vma gp;
1153 {
1154   bfd_vma relocation;
1155   unsigned long insn;
1156   unsigned long val;
1157
1158   if (bfd_is_com_section (symbol->section))
1159     relocation = 0;
1160   else
1161     relocation = symbol->value;
1162
1163   relocation += symbol->section->output_section->vma;
1164   relocation += symbol->section->output_offset;
1165
1166   if (reloc_entry->address > input_section->_cooked_size)
1167     return bfd_reloc_outofrange;
1168
1169   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1170
1171   /* Set val to the offset into the section or symbol.  */
1172   if (reloc_entry->howto->src_mask == 0)
1173     {
1174       /* This case occurs with the 64-bit MIPS ELF ABI.  */
1175       val = reloc_entry->addend;
1176     }
1177   else
1178     {
1179       val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1180       if (val & 0x8000)
1181         val -= 0x10000;
1182     }
1183
1184   /* Adjust val for the final section location and GP value.  If we
1185      are producing relocateable output, we don't want to do this for
1186      an external symbol.  */
1187   if (! relocateable
1188       || (symbol->flags & BSF_SECTION_SYM) != 0)
1189     val += relocation - gp;
1190
1191   insn = (insn &~ 0xffff) | (val & 0xffff);
1192   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1193
1194   if (relocateable)
1195     reloc_entry->address += input_section->output_offset;
1196
1197   /* Make sure it fit in 16 bits.  */
1198   if (val >= 0x8000 && val < 0xffff8000)
1199     return bfd_reloc_overflow;
1200
1201   return bfd_reloc_ok;
1202 }
1203
1204 /* Do a R_MIPS_GPREL32 relocation.  Is this 32 bit value the offset
1205    from the gp register? XXX */
1206
1207 static bfd_reloc_status_type gprel32_with_gp PARAMS ((bfd *, asymbol *,
1208                                                       arelent *, asection *,
1209                                                       boolean, PTR, bfd_vma));
1210
1211 bfd_reloc_status_type
1212 _bfd_mips_elf_gprel32_reloc (abfd,
1213                         reloc_entry,
1214                         symbol,
1215                         data,
1216                         input_section,
1217                         output_bfd,
1218                         error_message)
1219      bfd *abfd;
1220      arelent *reloc_entry;
1221      asymbol *symbol;
1222      PTR data;
1223      asection *input_section;
1224      bfd *output_bfd;
1225      char **error_message;
1226 {
1227   boolean relocateable;
1228   bfd_reloc_status_type ret;
1229   bfd_vma gp;
1230
1231   /* If we're relocating, and this is an external symbol with no
1232      addend, we don't want to change anything.  We will only have an
1233      addend if this is a newly created reloc, not read from an ELF
1234      file.  */
1235   if (output_bfd != (bfd *) NULL
1236       && (symbol->flags & BSF_SECTION_SYM) == 0
1237       && reloc_entry->addend == 0)
1238     {
1239       *error_message = (char *)
1240         "32bits gp relative relocation occurs for an external symbol";
1241       return bfd_reloc_outofrange;
1242     }
1243
1244   if (output_bfd != (bfd *) NULL)
1245     {
1246       relocateable = true;
1247       gp = _bfd_get_gp_value (output_bfd);
1248     }
1249   else
1250     {
1251       relocateable = false;
1252       output_bfd = symbol->section->output_section->owner;
1253
1254       ret = mips_elf_final_gp (output_bfd, symbol, relocateable,
1255                                error_message, &gp);
1256       if (ret != bfd_reloc_ok)
1257         return ret;
1258     }
1259
1260   return gprel32_with_gp (abfd, symbol, reloc_entry, input_section,
1261                           relocateable, data, gp);
1262 }
1263
1264 static bfd_reloc_status_type
1265 gprel32_with_gp (abfd, symbol, reloc_entry, input_section, relocateable, data,
1266                  gp)
1267      bfd *abfd;
1268      asymbol *symbol;
1269      arelent *reloc_entry;
1270      asection *input_section;
1271      boolean relocateable;
1272      PTR data;
1273      bfd_vma gp;
1274 {
1275   bfd_vma relocation;
1276   unsigned long val;
1277
1278   if (bfd_is_com_section (symbol->section))
1279     relocation = 0;
1280   else
1281     relocation = symbol->value;
1282
1283   relocation += symbol->section->output_section->vma;
1284   relocation += symbol->section->output_offset;
1285
1286   if (reloc_entry->address > input_section->_cooked_size)
1287     return bfd_reloc_outofrange;
1288
1289   if (reloc_entry->howto->src_mask == 0)
1290     {
1291       /* This case arises with the 64-bit MIPS ELF ABI.  */
1292       val = 0;
1293     }
1294   else
1295     val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1296
1297   /* Set val to the offset into the section or symbol.  */
1298   val += reloc_entry->addend;
1299
1300   /* Adjust val for the final section location and GP value.  If we
1301      are producing relocateable output, we don't want to do this for
1302      an external symbol.  */
1303   if (! relocateable
1304       || (symbol->flags & BSF_SECTION_SYM) != 0)
1305     val += relocation - gp;
1306
1307   bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
1308
1309   if (relocateable)
1310     reloc_entry->address += input_section->output_offset;
1311
1312   return bfd_reloc_ok;
1313 }
1314
1315 /* Handle a 64 bit reloc in a 32 bit MIPS ELF file.  These are
1316    generated when addreses are 64 bits.  The upper 32 bits are a simle
1317    sign extension.  */
1318
1319 static bfd_reloc_status_type
1320 mips32_64bit_reloc (abfd, reloc_entry, symbol, data, input_section,
1321                     output_bfd, error_message)
1322      bfd *abfd;
1323      arelent *reloc_entry;
1324      asymbol *symbol;
1325      PTR data;
1326      asection *input_section;
1327      bfd *output_bfd;
1328      char **error_message;
1329 {
1330   bfd_reloc_status_type r;
1331   arelent reloc32;
1332   unsigned long val;
1333   bfd_size_type addr;
1334
1335   r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1336                              input_section, output_bfd, error_message);
1337   if (r != bfd_reloc_continue)
1338     return r;
1339
1340   /* Do a normal 32 bit relocation on the lower 32 bits.  */
1341   reloc32 = *reloc_entry;
1342   if (bfd_big_endian (abfd))
1343     reloc32.address += 4;
1344   reloc32.howto = &elf_mips_howto_table[R_MIPS_32];
1345   r = bfd_perform_relocation (abfd, &reloc32, data, input_section,
1346                               output_bfd, error_message);
1347
1348   /* Sign extend into the upper 32 bits.  */
1349   val = bfd_get_32 (abfd, (bfd_byte *) data + reloc32.address);
1350   if ((val & 0x80000000) != 0)
1351     val = 0xffffffff;
1352   else
1353     val = 0;
1354   addr = reloc_entry->address;
1355   if (bfd_little_endian (abfd))
1356     addr += 4;
1357   bfd_put_32 (abfd, val, (bfd_byte *) data + addr);
1358
1359   return r;
1360 }
1361
1362 /* Handle a mips16 jump.  */
1363
1364 static bfd_reloc_status_type
1365 mips16_jump_reloc (abfd, reloc_entry, symbol, data, input_section,
1366                    output_bfd, error_message)
1367      bfd *abfd;
1368      arelent *reloc_entry;
1369      asymbol *symbol;
1370      PTR data;
1371      asection *input_section;
1372      bfd *output_bfd;
1373      char **error_message;
1374 {
1375   if (output_bfd != (bfd *) NULL
1376       && (symbol->flags & BSF_SECTION_SYM) == 0
1377       && reloc_entry->addend == 0)
1378     {
1379       reloc_entry->address += input_section->output_offset;
1380       return bfd_reloc_ok;
1381     }
1382
1383   /* FIXME.  */
1384   {
1385     static boolean warned;
1386
1387     if (! warned)
1388       (*_bfd_error_handler)
1389         ("Linking mips16 objects into %s format is not supported",
1390          bfd_get_target (input_section->output_section->owner));
1391     warned = true;
1392   }
1393
1394   return bfd_reloc_undefined;
1395 }
1396
1397 /* Handle a mips16 GP relative reloc.  */
1398
1399 static bfd_reloc_status_type
1400 mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
1401                     output_bfd, error_message)
1402      bfd *abfd;
1403      arelent *reloc_entry;
1404      asymbol *symbol;
1405      PTR data;
1406      asection *input_section;
1407      bfd *output_bfd;
1408      char **error_message;
1409 {
1410   boolean relocateable;
1411   bfd_reloc_status_type ret;
1412   bfd_vma gp;
1413   unsigned short extend, insn;
1414   unsigned long final;
1415
1416   /* If we're relocating, and this is an external symbol with no
1417      addend, we don't want to change anything.  We will only have an
1418      addend if this is a newly created reloc, not read from an ELF
1419      file.  */
1420   if (output_bfd != NULL
1421       && (symbol->flags & BSF_SECTION_SYM) == 0
1422       && reloc_entry->addend == 0)
1423     {
1424       reloc_entry->address += input_section->output_offset;
1425       return bfd_reloc_ok;
1426     }
1427
1428   if (output_bfd != NULL)
1429     relocateable = true;
1430   else
1431     {
1432       relocateable = false;
1433       output_bfd = symbol->section->output_section->owner;
1434     }
1435
1436   ret = mips_elf_final_gp (output_bfd, symbol, relocateable, error_message,
1437                            &gp);
1438   if (ret != bfd_reloc_ok)
1439     return ret;
1440
1441   if (reloc_entry->address > input_section->_cooked_size)
1442     return bfd_reloc_outofrange;
1443
1444   /* Pick up the mips16 extend instruction and the real instruction.  */
1445   extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
1446   insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
1447
1448   /* Stuff the current addend back as a 32 bit value, do the usual
1449      relocation, and then clean up.  */
1450   bfd_put_32 (abfd,
1451               (((extend & 0x1f) << 11)
1452                | (extend & 0x7e0)
1453                | (insn & 0x1f)),
1454               (bfd_byte *) data + reloc_entry->address);
1455
1456   ret = gprel16_with_gp (abfd, symbol, reloc_entry, input_section,
1457                          relocateable, data, gp);
1458
1459   final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1460   bfd_put_16 (abfd,
1461               ((extend & 0xf800)
1462                | ((final >> 11) & 0x1f)
1463                | (final & 0x7e0)),
1464               (bfd_byte *) data + reloc_entry->address);
1465   bfd_put_16 (abfd,
1466               ((insn & 0xffe0)
1467                | (final & 0x1f)),
1468               (bfd_byte *) data + reloc_entry->address + 2);
1469
1470   return ret;
1471 }
1472
1473 /* Return the ISA for a MIPS e_flags value.  */
1474
1475 static INLINE int
1476 elf_mips_isa (flags)
1477      flagword flags;
1478 {
1479   switch (flags & EF_MIPS_ARCH)
1480     {
1481     case E_MIPS_ARCH_1:
1482       return 1;
1483     case E_MIPS_ARCH_2:
1484       return 2;
1485     case E_MIPS_ARCH_3:
1486       return 3;
1487     case E_MIPS_ARCH_4:
1488       return 4;
1489     }
1490   return 4;
1491 }
1492
1493 /* A mapping from BFD reloc types to MIPS ELF reloc types.  */
1494
1495 struct elf_reloc_map {
1496   bfd_reloc_code_real_type bfd_reloc_val;
1497   enum reloc_type elf_reloc_val;
1498 };
1499
1500 static CONST struct elf_reloc_map mips_reloc_map[] =
1501 {
1502   { BFD_RELOC_NONE, R_MIPS_NONE, },
1503   { BFD_RELOC_16, R_MIPS_16 },
1504   { BFD_RELOC_32, R_MIPS_32 },
1505   { BFD_RELOC_64, R_MIPS_64 },
1506   { BFD_RELOC_MIPS_JMP, R_MIPS_26 },
1507   { BFD_RELOC_HI16_S, R_MIPS_HI16 },
1508   { BFD_RELOC_LO16, R_MIPS_LO16 },
1509   { BFD_RELOC_MIPS_GPREL, R_MIPS_GPREL16 },
1510   { BFD_RELOC_MIPS_LITERAL, R_MIPS_LITERAL },
1511   { BFD_RELOC_MIPS_GOT16, R_MIPS_GOT16 },
1512   { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
1513   { BFD_RELOC_MIPS_CALL16, R_MIPS_CALL16 },
1514   { BFD_RELOC_MIPS_GPREL32, R_MIPS_GPREL32 },
1515   { BFD_RELOC_MIPS_GOT_HI16, R_MIPS_GOT_HI16 },
1516   { BFD_RELOC_MIPS_GOT_LO16, R_MIPS_GOT_LO16 },
1517   { BFD_RELOC_MIPS_CALL_HI16, R_MIPS_CALL_HI16 },
1518   { BFD_RELOC_MIPS_CALL_LO16, R_MIPS_CALL_LO16 }
1519 };
1520
1521 /* Given a BFD reloc type, return a howto structure.  */
1522
1523 static reloc_howto_type *
1524 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
1525      bfd *abfd;
1526      bfd_reloc_code_real_type code;
1527 {
1528   unsigned int i;
1529
1530   for (i = 0; i < sizeof (mips_reloc_map) / sizeof (struct elf_reloc_map); i++)
1531     {
1532       if (mips_reloc_map[i].bfd_reloc_val == code)
1533         return &elf_mips_howto_table[(int) mips_reloc_map[i].elf_reloc_val];
1534     }
1535
1536   /* We need to handle BFD_RELOC_CTOR specially.  If this is a mips3
1537      file, then we assume that we are using 64 bit addresses, and use
1538      R_MIPS_64.  Otherwise, we use R_MIPS_32.  */
1539   if (code == BFD_RELOC_CTOR)
1540     {
1541       if (elf_mips_isa (elf_elfheader (abfd)->e_flags) < 3)
1542         return &elf_mips_howto_table[(int) R_MIPS_32];
1543       else
1544         return &elf_mips_ctor64_howto;
1545     }
1546
1547   /* Special handling for the MIPS16 relocs, since they are made up
1548      reloc types with a large value.  */
1549   if (code == BFD_RELOC_MIPS16_JMP)
1550     return &elf_mips16_jump_howto;
1551   else if (code == BFD_RELOC_MIPS16_GPREL)
1552     return &elf_mips16_gprel_howto;
1553
1554   return NULL;
1555 }
1556
1557 /* Given a MIPS reloc type, fill in an arelent structure.  */
1558
1559 static void
1560 mips_info_to_howto_rel (abfd, cache_ptr, dst)
1561      bfd *abfd;
1562      arelent *cache_ptr;
1563      Elf32_Internal_Rel *dst;
1564 {
1565   unsigned int r_type;
1566
1567   r_type = ELF32_R_TYPE (dst->r_info);
1568   if (r_type == R_MIPS16_26)
1569     cache_ptr->howto = &elf_mips16_jump_howto;
1570   else if (r_type == R_MIPS16_GPREL)
1571     cache_ptr->howto = &elf_mips16_gprel_howto;
1572   else
1573     {
1574       BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
1575       cache_ptr->howto = &elf_mips_howto_table[r_type];
1576     }
1577
1578   /* The addend for a GPREL16 or LITERAL relocation comes from the GP
1579      value for the object file.  We get the addend now, rather than
1580      when we do the relocation, because the symbol manipulations done
1581      by the linker may cause us to lose track of the input BFD.  */
1582   if (((*cache_ptr->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0
1583       && (r_type == (unsigned int) R_MIPS_GPREL16
1584           || r_type == (unsigned int) R_MIPS_LITERAL))
1585     cache_ptr->addend = elf_gp (abfd);
1586 }
1587 \f
1588 /* A .reginfo section holds a single Elf32_RegInfo structure.  These
1589    routines swap this structure in and out.  They are used outside of
1590    BFD, so they are globally visible.  */
1591
1592 void
1593 bfd_mips_elf32_swap_reginfo_in (abfd, ex, in)
1594      bfd *abfd;
1595      const Elf32_External_RegInfo *ex;
1596      Elf32_RegInfo *in;
1597 {
1598   in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1599   in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1600   in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1601   in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1602   in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1603   in->ri_gp_value = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gp_value);
1604 }
1605
1606 void
1607 bfd_mips_elf32_swap_reginfo_out (abfd, in, ex)
1608      bfd *abfd;
1609      const Elf32_RegInfo *in;
1610      Elf32_External_RegInfo *ex;
1611 {
1612   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1613                 (bfd_byte *) ex->ri_gprmask);
1614   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1615                 (bfd_byte *) ex->ri_cprmask[0]);
1616   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1617                 (bfd_byte *) ex->ri_cprmask[1]);
1618   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1619                 (bfd_byte *) ex->ri_cprmask[2]);
1620   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1621                 (bfd_byte *) ex->ri_cprmask[3]);
1622   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gp_value,
1623                 (bfd_byte *) ex->ri_gp_value);
1624 }
1625
1626 /* In the 64 bit ABI, the .MIPS.options section holds register
1627    information in an Elf64_Reginfo structure.  These routines swap
1628    them in and out.  They are globally visible because they are used
1629    outside of BFD.  These routines are here so that gas can call them
1630    without worrying about whether the 64 bit ABI has been included.  */
1631
1632 void
1633 bfd_mips_elf64_swap_reginfo_in (abfd, ex, in)
1634      bfd *abfd;
1635      const Elf64_External_RegInfo *ex;
1636      Elf64_Internal_RegInfo *in;
1637 {
1638   in->ri_gprmask = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_gprmask);
1639   in->ri_pad = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_pad);
1640   in->ri_cprmask[0] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[0]);
1641   in->ri_cprmask[1] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[1]);
1642   in->ri_cprmask[2] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[2]);
1643   in->ri_cprmask[3] = bfd_h_get_32 (abfd, (bfd_byte *) ex->ri_cprmask[3]);
1644   in->ri_gp_value = bfd_h_get_64 (abfd, (bfd_byte *) ex->ri_gp_value);
1645 }
1646
1647 void
1648 bfd_mips_elf64_swap_reginfo_out (abfd, in, ex)
1649      bfd *abfd;
1650      const Elf64_Internal_RegInfo *in;
1651      Elf64_External_RegInfo *ex;
1652 {
1653   bfd_h_put_32 (abfd, (bfd_vma) in->ri_gprmask,
1654                 (bfd_byte *) ex->ri_gprmask);
1655   bfd_h_put_32 (abfd, (bfd_vma) in->ri_pad,
1656                 (bfd_byte *) ex->ri_pad);
1657   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[0],
1658                 (bfd_byte *) ex->ri_cprmask[0]);
1659   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[1],
1660                 (bfd_byte *) ex->ri_cprmask[1]);
1661   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[2],
1662                 (bfd_byte *) ex->ri_cprmask[2]);
1663   bfd_h_put_32 (abfd, (bfd_vma) in->ri_cprmask[3],
1664                 (bfd_byte *) ex->ri_cprmask[3]);
1665   bfd_h_put_64 (abfd, (bfd_vma) in->ri_gp_value,
1666                 (bfd_byte *) ex->ri_gp_value);
1667 }
1668
1669 /* Swap an entry in a .gptab section.  Note that these routines rely
1670    on the equivalence of the two elements of the union.  */
1671
1672 static void
1673 bfd_mips_elf32_swap_gptab_in (abfd, ex, in)
1674      bfd *abfd;
1675      const Elf32_External_gptab *ex;
1676      Elf32_gptab *in;
1677 {
1678   in->gt_entry.gt_g_value = bfd_h_get_32 (abfd, ex->gt_entry.gt_g_value);
1679   in->gt_entry.gt_bytes = bfd_h_get_32 (abfd, ex->gt_entry.gt_bytes);
1680 }
1681
1682 static void
1683 bfd_mips_elf32_swap_gptab_out (abfd, in, ex)
1684      bfd *abfd;
1685      const Elf32_gptab *in;
1686      Elf32_External_gptab *ex;
1687 {
1688   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_g_value,
1689                 ex->gt_entry.gt_g_value);
1690   bfd_h_put_32 (abfd, (bfd_vma) in->gt_entry.gt_bytes,
1691                 ex->gt_entry.gt_bytes);
1692 }
1693
1694 static void
1695 bfd_elf32_swap_compact_rel_out (abfd, in, ex)
1696      bfd *abfd;
1697      const Elf32_compact_rel *in;
1698      Elf32_External_compact_rel *ex;
1699 {
1700   bfd_h_put_32 (abfd, (bfd_vma) in->id1, ex->id1);
1701   bfd_h_put_32 (abfd, (bfd_vma) in->num, ex->num);
1702   bfd_h_put_32 (abfd, (bfd_vma) in->id2, ex->id2);
1703   bfd_h_put_32 (abfd, (bfd_vma) in->offset, ex->offset);
1704   bfd_h_put_32 (abfd, (bfd_vma) in->reserved0, ex->reserved0);
1705   bfd_h_put_32 (abfd, (bfd_vma) in->reserved1, ex->reserved1);
1706 }
1707
1708 static void
1709 bfd_elf32_swap_crinfo_out (abfd, in, ex)
1710      bfd *abfd;
1711      const Elf32_crinfo *in;
1712      Elf32_External_crinfo *ex;
1713 {
1714   unsigned long l;
1715
1716   l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
1717        | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
1718        | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
1719        | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
1720   bfd_h_put_32 (abfd, (bfd_vma) l, ex->info);
1721   bfd_h_put_32 (abfd, (bfd_vma) in->konst, ex->konst);
1722   bfd_h_put_32 (abfd, (bfd_vma) in->vaddr, ex->vaddr);
1723 }
1724
1725 /* Swap in an options header.  */
1726
1727 void
1728 bfd_mips_elf_swap_options_in (abfd, ex, in)
1729      bfd *abfd;
1730      const Elf_External_Options *ex;
1731      Elf_Internal_Options *in;
1732 {
1733   in->kind = bfd_h_get_8 (abfd, ex->kind);
1734   in->size = bfd_h_get_8 (abfd, ex->size);
1735   in->section = bfd_h_get_16 (abfd, ex->section);
1736   in->info = bfd_h_get_32 (abfd, ex->info);
1737 }
1738
1739 /* Swap out an options header.  */
1740
1741 void
1742 bfd_mips_elf_swap_options_out (abfd, in, ex)
1743      bfd *abfd;
1744      const Elf_Internal_Options *in;
1745      Elf_External_Options *ex;
1746 {
1747   bfd_h_put_8 (abfd, in->kind, ex->kind);
1748   bfd_h_put_8 (abfd, in->size, ex->size);
1749   bfd_h_put_16 (abfd, in->section, ex->section);
1750   bfd_h_put_32 (abfd, in->info, ex->info);
1751 }
1752 \f
1753 /* Determine whether a symbol is global for the purposes of splitting
1754    the symbol table into global symbols and local symbols.  At least
1755    on Irix 5, this split must be between section symbols and all other
1756    symbols.  On most ELF targets the split is between static symbols
1757    and externally visible symbols.  */
1758
1759 /*ARGSUSED*/
1760 static boolean
1761 mips_elf_sym_is_global (abfd, sym)
1762      bfd *abfd;
1763      asymbol *sym;
1764 {
1765   return (sym->flags & BSF_SECTION_SYM) == 0 ? true : false;
1766 }
1767 \f
1768 /* Set the right machine number for a MIPS ELF file.  This is used for
1769    both the 32-bit and the 64-bit ABI.  */
1770
1771 boolean
1772 _bfd_mips_elf_object_p (abfd)
1773      bfd *abfd;
1774 {
1775   switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
1776     {
1777     default:
1778     case E_MIPS_ARCH_1:
1779       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 3000);
1780       break;
1781
1782     case E_MIPS_ARCH_2:
1783       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 6000);
1784       break;
1785
1786     case E_MIPS_ARCH_3:
1787       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 4000);
1788       break;
1789
1790     case E_MIPS_ARCH_4:
1791       (void) bfd_default_set_arch_mach (abfd, bfd_arch_mips, 8000);
1792       break;
1793     }
1794
1795   return true;
1796 }
1797
1798 /* Set the right machine number for a 32-bit MIPS ELF file.  */
1799
1800 static boolean
1801 mips_elf32_object_p (abfd)
1802      bfd *abfd;
1803 {
1804   /* Irix 5 is broken.  Object file symbol tables are not always
1805      sorted correctly such that local symbols precede global symbols,
1806      and the sh_info field in the symbol table is not always right.  */
1807   elf_bad_symtab (abfd) = true;
1808
1809   return _bfd_mips_elf_object_p (abfd);
1810 }
1811
1812 /* The final processing done just before writing out a MIPS ELF object
1813    file.  This gets the MIPS architecture right based on the machine
1814    number.  This is used by both the 32-bit and the 64-bit ABI.  */
1815
1816 /*ARGSUSED*/
1817 void
1818 _bfd_mips_elf_final_write_processing (abfd, linker)
1819      bfd *abfd;
1820      boolean linker;
1821 {
1822   unsigned long val;
1823   unsigned int i;
1824   Elf_Internal_Shdr **hdrpp;
1825   const char *name;
1826   asection *sec;
1827
1828   switch (bfd_get_mach (abfd))
1829     {
1830     case 3000:
1831       val = E_MIPS_ARCH_1;
1832       break;
1833
1834     case 6000:
1835       val = E_MIPS_ARCH_2;
1836       break;
1837
1838     case 4000:
1839       val = E_MIPS_ARCH_3;
1840       break;
1841
1842     case 8000:
1843       val = E_MIPS_ARCH_4;
1844       break;
1845
1846     default:
1847       val = 0;
1848       break;
1849     }
1850
1851   elf_elfheader (abfd)->e_flags &=~ EF_MIPS_ARCH;
1852   elf_elfheader (abfd)->e_flags |= val;
1853
1854   /* Set the sh_info field for .gptab sections.  */
1855   for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
1856        i < elf_elfheader (abfd)->e_shnum;
1857        i++, hdrpp++)
1858     {
1859       switch ((*hdrpp)->sh_type)
1860         {
1861         case SHT_MIPS_LIBLIST:
1862           sec = bfd_get_section_by_name (abfd, ".dynstr");
1863           if (sec != NULL)
1864             (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1865           break;
1866
1867         case SHT_MIPS_GPTAB:
1868           BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1869           name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1870           BFD_ASSERT (name != NULL
1871                       && strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0);
1872           sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
1873           BFD_ASSERT (sec != NULL);
1874           (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1875           break;
1876
1877         case SHT_MIPS_CONTENT:
1878           BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1879           name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1880           BFD_ASSERT (name != NULL
1881                       && strncmp (name, ".MIPS.content",
1882                                   sizeof ".MIPS.content" - 1) == 0);
1883           sec = bfd_get_section_by_name (abfd,
1884                                          name + sizeof ".MIPS.content" - 1);
1885           BFD_ASSERT (sec != NULL);
1886           (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1887           break;
1888
1889         case SHT_MIPS_SYMBOL_LIB:
1890           sec = bfd_get_section_by_name (abfd, ".dynsym");
1891           if (sec != NULL)
1892             (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1893           sec = bfd_get_section_by_name (abfd, ".liblist");
1894           if (sec != NULL)
1895             (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
1896           break;
1897
1898         case SHT_MIPS_EVENTS:
1899           BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
1900           name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
1901           BFD_ASSERT (name != NULL);
1902           if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0)
1903             sec = bfd_get_section_by_name (abfd,
1904                                            name + sizeof ".MIPS.events" - 1);
1905           else
1906             {
1907               BFD_ASSERT (strncmp (name, ".MIPS.post_rel",
1908                                    sizeof ".MIPS.post_rel" - 1) == 0);
1909               sec = bfd_get_section_by_name (abfd,
1910                                              (name
1911                                               + sizeof ".MIPS.post_rel" - 1));
1912             }
1913           BFD_ASSERT (sec != NULL);
1914           (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
1915           break;
1916         }
1917     }
1918 }
1919 \f
1920 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
1921
1922 boolean
1923 _bfd_mips_elf_set_private_flags (abfd, flags)
1924      bfd *abfd;
1925      flagword flags;
1926 {
1927   BFD_ASSERT (!elf_flags_init (abfd)
1928               || elf_elfheader (abfd)->e_flags == flags);
1929
1930   elf_elfheader (abfd)->e_flags = flags;
1931   elf_flags_init (abfd) = true;
1932   return true;
1933 }
1934
1935 /* Copy backend specific data from one object module to another */
1936
1937 boolean
1938 _bfd_mips_elf_copy_private_bfd_data (ibfd, obfd)
1939      bfd *ibfd;
1940      bfd *obfd;
1941 {
1942   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1943       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1944     return true;
1945
1946   BFD_ASSERT (!elf_flags_init (obfd)
1947               || (elf_elfheader (obfd)->e_flags
1948                   == elf_elfheader (ibfd)->e_flags));
1949
1950   elf_gp (obfd) = elf_gp (ibfd);
1951   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1952   elf_flags_init (obfd) = true;
1953   return true;
1954 }
1955
1956 /* Merge backend specific data from an object file to the output
1957    object file when linking.  */
1958
1959 boolean
1960 _bfd_mips_elf_merge_private_bfd_data (ibfd, obfd)
1961      bfd *ibfd;
1962      bfd *obfd;
1963 {
1964   flagword old_flags;
1965   flagword new_flags;
1966   boolean ok;
1967
1968   /* Check if we have the same endianess */
1969   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1970       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1971     {
1972       (*_bfd_error_handler)
1973         ("%s: compiled for a %s endian system and target is %s endian",
1974          bfd_get_filename (ibfd),
1975          bfd_big_endian (ibfd) ? "big" : "little",
1976          bfd_big_endian (obfd) ? "big" : "little");
1977
1978       bfd_set_error (bfd_error_wrong_format);
1979       return false;
1980     }
1981
1982   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1983       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1984     return true;
1985
1986   new_flags = elf_elfheader (ibfd)->e_flags;
1987   elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
1988   old_flags = elf_elfheader (obfd)->e_flags;
1989
1990   if (! elf_flags_init (obfd))
1991     {
1992       elf_flags_init (obfd) = true;
1993       elf_elfheader (obfd)->e_flags = new_flags;
1994
1995       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1996           && bfd_get_arch_info (obfd)->the_default)
1997         {
1998           if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1999                                    bfd_get_mach (ibfd)))
2000             return false;
2001         }
2002
2003       return true;
2004     }
2005
2006   /* Check flag compatibility.  */
2007
2008   new_flags &= ~EF_MIPS_NOREORDER;
2009   old_flags &= ~EF_MIPS_NOREORDER;
2010
2011   if (new_flags == old_flags)
2012     return true;
2013
2014   ok = true;
2015
2016   if ((new_flags & EF_MIPS_PIC) != (old_flags & EF_MIPS_PIC))
2017     {
2018       new_flags &= ~EF_MIPS_PIC;
2019       old_flags &= ~EF_MIPS_PIC;
2020       (*_bfd_error_handler)
2021         ("%s: linking PIC files with non-PIC files",
2022          bfd_get_filename (ibfd));
2023       ok = false;
2024     }
2025
2026   if ((new_flags & EF_MIPS_CPIC) != (old_flags & EF_MIPS_CPIC))
2027     {
2028       new_flags &= ~EF_MIPS_CPIC;
2029       old_flags &= ~EF_MIPS_CPIC;
2030       (*_bfd_error_handler)
2031         ("%s: linking abicalls files with non-abicalls files",
2032          bfd_get_filename (ibfd));
2033       ok = false;
2034     }
2035
2036   /* Don't warn about mixing -mips1 and -mips2 code, or mixing -mips3
2037      and -mips4 code.  They will normally use the same data sizes and
2038      calling conventions.  */
2039   if ((new_flags & EF_MIPS_ARCH) != (old_flags & EF_MIPS_ARCH))
2040     {
2041       int new_isa, old_isa;
2042
2043       new_isa = elf_mips_isa (new_flags);
2044       old_isa = elf_mips_isa (old_flags);
2045       if ((new_isa == 1 || new_isa == 2)
2046           ? (old_isa != 1 && old_isa != 2)
2047           : (old_isa == 1 || old_isa == 2))
2048         {
2049           (*_bfd_error_handler)
2050             ("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)",
2051              bfd_get_filename (ibfd), new_isa, old_isa);
2052           ok = false;
2053         }
2054
2055       new_flags &= ~ EF_MIPS_ARCH;
2056       old_flags &= ~ EF_MIPS_ARCH;
2057     }
2058
2059   /* Warn about any other mismatches */
2060   if (new_flags != old_flags)
2061     {
2062       (*_bfd_error_handler)
2063         ("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)",
2064          bfd_get_filename (ibfd), (unsigned long) new_flags,
2065          (unsigned long) old_flags);
2066       ok = false;
2067     }
2068
2069   if (! ok)
2070     {
2071       bfd_set_error (bfd_error_bad_value);
2072       return false;
2073     }
2074
2075   return true;
2076 }
2077 \f
2078 /* Handle a MIPS specific section when reading an object file.  This
2079    is called when elfcode.h finds a section with an unknown type.
2080    This routine supports both the 32-bit and 64-bit ELF ABI.
2081
2082    FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
2083    how to.  */
2084
2085 boolean
2086 _bfd_mips_elf_section_from_shdr (abfd, hdr, name)
2087      bfd *abfd;
2088      Elf_Internal_Shdr *hdr;
2089      const char *name;
2090 {
2091   /* There ought to be a place to keep ELF backend specific flags, but
2092      at the moment there isn't one.  We just keep track of the
2093      sections by their name, instead.  Fortunately, the ABI gives
2094      suggested names for all the MIPS specific sections, so we will
2095      probably get away with this.  */
2096   switch (hdr->sh_type)
2097     {
2098     case SHT_MIPS_LIBLIST:
2099       if (strcmp (name, ".liblist") != 0)
2100         return false;
2101       break;
2102     case SHT_MIPS_MSYM:
2103       if (strcmp (name, ".msym") != 0)
2104         return false;
2105       break;
2106     case SHT_MIPS_CONFLICT:
2107       if (strcmp (name, ".conflict") != 0)
2108         return false;
2109       break;
2110     case SHT_MIPS_GPTAB:
2111       if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) != 0)
2112         return false;
2113       break;
2114     case SHT_MIPS_UCODE:
2115       if (strcmp (name, ".ucode") != 0)
2116         return false;
2117       break;
2118     case SHT_MIPS_DEBUG:
2119       if (strcmp (name, ".mdebug") != 0)
2120         return false;
2121       break;
2122     case SHT_MIPS_REGINFO:
2123       if (strcmp (name, ".reginfo") != 0
2124           || hdr->sh_size != sizeof (Elf32_External_RegInfo))
2125         return false;
2126       break;
2127     case SHT_MIPS_IFACE:
2128       if (strcmp (name, ".MIPS.interfaces") != 0)
2129         return false;
2130       break;
2131     case SHT_MIPS_CONTENT:
2132       if (strncmp (name, ".MIPS.content", sizeof ".MIPS.content" - 1) != 0)
2133         return false;
2134       break;
2135     case SHT_MIPS_OPTIONS:
2136       if (strcmp (name, ".options") != 0
2137           && strcmp (name, ".MIPS.options") != 0)
2138         return false;
2139       break;
2140     case SHT_MIPS_DWARF:
2141       if (strncmp (name, ".debug_", sizeof ".debug_" - 1) != 0)
2142         return false;
2143       break;
2144     case SHT_MIPS_SYMBOL_LIB:
2145       if (strcmp (name, ".MIPS.symlib") != 0)
2146         return false;
2147       break;
2148     case SHT_MIPS_EVENTS:
2149       if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) != 0
2150           && strncmp (name, ".MIPS.post_rel",
2151                       sizeof ".MIPS.post_rel" - 1) != 0)
2152         return false;
2153       break;
2154     default:
2155       return false;
2156     }
2157
2158   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2159     return false;
2160
2161   if (hdr->sh_type == SHT_MIPS_DEBUG)
2162     {
2163       if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2164                                    (bfd_get_section_flags (abfd,
2165                                                            hdr->bfd_section)
2166                                     | SEC_DEBUGGING)))
2167         return false;
2168     }
2169
2170   return true;
2171 }
2172
2173 /* Handle a 32-bit MIPS ELF specific section.  */
2174
2175 static boolean
2176 mips_elf32_section_from_shdr (abfd, hdr, name)
2177      bfd *abfd;
2178      Elf_Internal_Shdr *hdr;
2179      char *name;
2180 {
2181   if (! _bfd_mips_elf_section_from_shdr (abfd, hdr, name))
2182     return false;
2183
2184   /* FIXME: We should record sh_info for a .gptab section.  */
2185
2186   /* For a .reginfo section, set the gp value in the tdata information
2187      from the contents of this section.  We need the gp value while
2188      processing relocs, so we just get it now.  The .reginfo section
2189      is not used in the 64-bit MIPS ELF ABI.  */
2190   if (hdr->sh_type == SHT_MIPS_REGINFO)
2191     {
2192       Elf32_External_RegInfo ext;
2193       Elf32_RegInfo s;
2194
2195       if (! bfd_get_section_contents (abfd, hdr->bfd_section, (PTR) &ext,
2196                                       (file_ptr) 0, sizeof ext))
2197         return false;
2198       bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
2199       elf_gp (abfd) = s.ri_gp_value;
2200     }
2201
2202   /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
2203      set the gp value based on what we find.  We may see both
2204      SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
2205      they should agree.  */
2206   if (hdr->sh_type == SHT_MIPS_OPTIONS)
2207     {
2208       bfd_byte *contents, *l, *lend;
2209
2210       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
2211       if (contents == NULL)
2212         return false;
2213       if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
2214                                       (file_ptr) 0, hdr->sh_size))
2215         {
2216           free (contents);
2217           return false;
2218         }
2219       l = contents;
2220       lend = contents + hdr->sh_size;
2221       while (l + sizeof (Elf_External_Options) <= lend)
2222         {
2223           Elf_Internal_Options intopt;
2224
2225           bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2226                                         &intopt);
2227           if (intopt.kind == ODK_REGINFO)
2228             {
2229               Elf32_RegInfo intreg;
2230
2231               bfd_mips_elf32_swap_reginfo_in
2232                 (abfd,
2233                  ((Elf32_External_RegInfo *)
2234                   (l + sizeof (Elf_External_Options))),
2235                  &intreg);
2236               elf_gp (abfd) = intreg.ri_gp_value;
2237             }
2238           l += intopt.size;
2239         }
2240       free (contents);
2241     }
2242
2243   return true;
2244 }
2245
2246 /* Set the correct type for a MIPS ELF section.  We do this by the
2247    section name, which is a hack, but ought to work.  This routine is
2248    used by both the 32-bit and the 64-bit ABI.  */
2249
2250 boolean
2251 _bfd_mips_elf_fake_sections (abfd, hdr, sec)
2252      bfd *abfd;
2253      Elf32_Internal_Shdr *hdr;
2254      asection *sec;
2255 {
2256   register const char *name;
2257
2258   name = bfd_get_section_name (abfd, sec);
2259
2260   if (strcmp (name, ".liblist") == 0)
2261     {
2262       hdr->sh_type = SHT_MIPS_LIBLIST;
2263       hdr->sh_info = sec->_raw_size / sizeof (Elf32_Lib);
2264       /* The sh_link field is set in final_write_processing.  */
2265     }
2266   else if (strcmp (name, ".msym") == 0)
2267     {
2268       hdr->sh_type = SHT_MIPS_MSYM;
2269       hdr->sh_entsize = 8;
2270       /* FIXME: Set the sh_info field.  */
2271     }
2272   else if (strcmp (name, ".conflict") == 0)
2273     hdr->sh_type = SHT_MIPS_CONFLICT;
2274   else if (strncmp (name, ".gptab.", sizeof ".gptab." - 1) == 0)
2275     {
2276       hdr->sh_type = SHT_MIPS_GPTAB;
2277       hdr->sh_entsize = sizeof (Elf32_External_gptab);
2278       /* The sh_info field is set in final_write_processing.  */
2279     }
2280   else if (strcmp (name, ".ucode") == 0)
2281     hdr->sh_type = SHT_MIPS_UCODE;
2282   else if (strcmp (name, ".mdebug") == 0)
2283     {
2284       hdr->sh_type = SHT_MIPS_DEBUG;
2285       /* In a shared object on Irix 5.3, the .mdebug section has an
2286          entsize of 0.  FIXME: Does this matter?  */
2287       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2288         hdr->sh_entsize = 0;
2289       else
2290         hdr->sh_entsize = 1;
2291     }
2292   else if (strcmp (name, ".reginfo") == 0)
2293     {
2294       hdr->sh_type = SHT_MIPS_REGINFO;
2295       /* In a shared object on Irix 5.3, the .reginfo section has an
2296          entsize of 0x18.  FIXME: Does this matter?  */
2297       if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
2298         hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
2299       else
2300         hdr->sh_entsize = 1;
2301
2302       /* Force the section size to the correct value, even if the
2303          linker thinks it is larger.  The link routine below will only
2304          write out this much data for .reginfo.  */
2305       hdr->sh_size = sec->_raw_size = sizeof (Elf32_External_RegInfo);
2306     }
2307   else if (SGI_COMPAT (abfd)
2308            && (strcmp (name, ".hash") == 0
2309                || strcmp (name, ".dynamic") == 0
2310                || strcmp (name, ".dynstr") == 0))
2311     {
2312       hdr->sh_entsize = 0;
2313       hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
2314     }
2315   else if (strcmp (name, ".got") == 0
2316            || strcmp (name, ".sdata") == 0
2317            || strcmp (name, ".sbss") == 0
2318            || strcmp (name, ".lit4") == 0
2319            || strcmp (name, ".lit8") == 0)
2320     hdr->sh_flags |= SHF_MIPS_GPREL;
2321   else if (strcmp (name, ".MIPS.interfaces") == 0)
2322     {
2323       hdr->sh_type = SHT_MIPS_IFACE;
2324       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2325     }
2326   else if (strcmp (name, ".MIPS.content") == 0)
2327     {
2328       hdr->sh_type = SHT_MIPS_CONTENT;
2329       /* The sh_info field is set in final_write_processing.  */
2330     }
2331   else if (strcmp (name, ".options") == 0
2332            || strcmp (name, ".MIPS.options") == 0)
2333     {
2334       hdr->sh_type = SHT_MIPS_OPTIONS;
2335       hdr->sh_entsize = 1;
2336       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2337     }
2338   else if (strncmp (name, ".debug_", sizeof ".debug_" - 1) == 0)
2339     hdr->sh_type = SHT_MIPS_DWARF;
2340   else if (strcmp (name, ".MIPS.symlib") == 0)
2341     {
2342       hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
2343       /* The sh_link and sh_info fields are set in
2344          final_write_processing.  */
2345     }
2346   else if (strncmp (name, ".MIPS.events", sizeof ".MIPS.events" - 1) == 0
2347            || strncmp (name, ".MIPS.post_rel",
2348                        sizeof ".MIPS.post_rel" - 1) == 0)
2349     {
2350       hdr->sh_type = SHT_MIPS_EVENTS;
2351       hdr->sh_flags |= SHF_MIPS_NOSTRIP;
2352       /* The sh_link field is set in final_write_processing.  */
2353     }
2354
2355   return true;
2356 }
2357
2358 /* Given a BFD section, try to locate the corresponding ELF section
2359    index.  This is used by both the 32-bit and the 64-bit ABI.
2360    Actually, it's not clear to me that the 64-bit ABI supports these,
2361    but for non-PIC objects we will certainly want support for at least
2362    the .scommon section.  */
2363
2364 boolean
2365 _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
2366      bfd *abfd;
2367      Elf32_Internal_Shdr *hdr;
2368      asection *sec;
2369      int *retval;
2370 {
2371   if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
2372     {
2373       *retval = SHN_MIPS_SCOMMON;
2374       return true;
2375     }
2376   if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
2377     {
2378       *retval = SHN_MIPS_ACOMMON;
2379       return true;
2380     }
2381   return false;
2382 }
2383
2384 /* When are writing out the .options or .MIPS.options section,
2385    remember the bytes we are writing out, so that we can install the
2386    GP value in the section_processing routine.  */
2387
2388 boolean
2389 _bfd_mips_elf_set_section_contents (abfd, section, location, offset, count)
2390      bfd *abfd;
2391      sec_ptr section;
2392      PTR location;
2393      file_ptr offset;
2394      bfd_size_type count;
2395 {
2396   if (strcmp (section->name, ".options") == 0
2397       || strcmp (section->name, ".MIPS.options") == 0)
2398     {
2399       bfd_byte *c;
2400
2401       if (elf_section_data (section) == NULL)
2402         {
2403           section->used_by_bfd =
2404             (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
2405           if (elf_section_data (section) == NULL)
2406             return false;
2407         }
2408       c = (bfd_byte *) elf_section_data (section)->tdata;
2409       if (c == NULL)
2410         {
2411           bfd_size_type size;
2412
2413           if (section->_cooked_size != 0)
2414             size = section->_cooked_size;
2415           else
2416             size = section->_raw_size;
2417           c = (PTR) bfd_zalloc (abfd, size);
2418           if (c == NULL)
2419             return false;
2420           elf_section_data (section)->tdata = (PTR) c;
2421         }
2422
2423       memcpy (c + offset, location, count);
2424     }
2425
2426   return _bfd_elf_set_section_contents (abfd, section, location, offset,
2427                                         count);
2428 }
2429
2430 /* Work over a section just before writing it out.  This routine is
2431    used by both the 32-bit and the 64-bit ABI.  FIXME: We recognize
2432    sections that need the SHF_MIPS_GPREL flag by name; there has to be
2433    a better way.  */
2434
2435 boolean
2436 _bfd_mips_elf_section_processing (abfd, hdr)
2437      bfd *abfd;
2438      Elf_Internal_Shdr *hdr;
2439 {
2440   if (hdr->bfd_section != NULL)
2441     {
2442       const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
2443
2444       if (strcmp (name, ".sdata") == 0)
2445         {
2446           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2447           hdr->sh_type = SHT_PROGBITS;
2448         }
2449       else if (strcmp (name, ".sbss") == 0)
2450         {
2451           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2452           hdr->sh_type = SHT_NOBITS;
2453         }
2454       else if (strcmp (name, ".lit8") == 0
2455                || strcmp (name, ".lit4") == 0)
2456         {
2457           hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
2458           hdr->sh_type = SHT_PROGBITS;
2459         }
2460       else if (strcmp (name, ".compact_rel") == 0)
2461         {
2462           hdr->sh_flags = 0;
2463           hdr->sh_type = SHT_PROGBITS;
2464         }
2465       else if (strcmp (name, ".rtproc") == 0)
2466         {
2467           if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
2468             {
2469               unsigned int adjust;
2470
2471               adjust = hdr->sh_size % hdr->sh_addralign;
2472               if (adjust != 0)
2473                 hdr->sh_size += hdr->sh_addralign - adjust;
2474             }
2475         }
2476     }
2477
2478   return true;
2479 }
2480
2481 /* Work over a section just before writing it out.  We update the GP
2482    value in the SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS sections based
2483    on the value we are using.  */
2484
2485 static boolean
2486 mips_elf32_section_processing (abfd, hdr)
2487      bfd *abfd;
2488      Elf32_Internal_Shdr *hdr;
2489 {
2490   if (hdr->sh_type == SHT_MIPS_REGINFO)
2491     {
2492       bfd_byte buf[4];
2493
2494       BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
2495       BFD_ASSERT (hdr->contents == NULL);
2496
2497       if (bfd_seek (abfd,
2498                     hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
2499                     SEEK_SET) == -1)
2500         return false;
2501       bfd_h_put_32 (abfd, (bfd_vma) elf_gp (abfd), buf);
2502       if (bfd_write (buf, (bfd_size_type) 1, (bfd_size_type) 4, abfd) != 4)
2503         return false;
2504     }
2505
2506   if (hdr->sh_type == SHT_MIPS_OPTIONS
2507       && hdr->bfd_section != NULL
2508       && elf_section_data (hdr->bfd_section) != NULL
2509       && elf_section_data (hdr->bfd_section)->tdata != NULL)
2510     {
2511       bfd_byte *contents, *l, *lend;
2512
2513       /* We stored the section contents in the elf_section_data tdata
2514          field in the set_section_contents routine.  We save the
2515          section contents so that we don't have to read them again.
2516          At this point we know that elf_gp is set, so we can look
2517          through the section contents to see if there is an
2518          ODK_REGINFO structure.  */
2519
2520       contents = (bfd_byte *) elf_section_data (hdr->bfd_section)->tdata;
2521       l = contents;
2522       lend = contents + hdr->sh_size;
2523       while (l + sizeof (Elf_External_Options) <= lend)
2524         {
2525           Elf_Internal_Options intopt;
2526
2527           bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
2528                                         &intopt);
2529           if (intopt.kind == ODK_REGINFO)
2530             {
2531               bfd_byte buf[4];
2532
2533               if (bfd_seek (abfd,
2534                             (hdr->sh_offset
2535                              + (l - contents)
2536                              + sizeof (Elf_External_Options)
2537                              + (sizeof (Elf32_External_RegInfo) - 4)),
2538                              SEEK_SET) == -1)
2539                 return false;
2540               bfd_h_put_32 (abfd, elf_gp (abfd), buf);
2541               if (bfd_write (buf, 1, 4, abfd) != 4)
2542                 return false;
2543             }
2544           l += intopt.size;
2545         }
2546     }
2547
2548   return _bfd_mips_elf_section_processing (abfd, hdr);
2549 }
2550 \f
2551 /* MIPS ELF uses two common sections.  One is the usual one, and the
2552    other is for small objects.  All the small objects are kept
2553    together, and then referenced via the gp pointer, which yields
2554    faster assembler code.  This is what we use for the small common
2555    section.  This approach is copied from ecoff.c.  */
2556 static asection mips_elf_scom_section;
2557 static asymbol mips_elf_scom_symbol;
2558 static asymbol *mips_elf_scom_symbol_ptr;
2559
2560 /* MIPS ELF also uses an acommon section, which represents an
2561    allocated common symbol which may be overridden by a
2562    definition in a shared library.  */
2563 static asection mips_elf_acom_section;
2564 static asymbol mips_elf_acom_symbol;
2565 static asymbol *mips_elf_acom_symbol_ptr;
2566
2567 /* The Irix 5 support uses two virtual sections, which represent
2568    text/data symbols defined in dynamic objects.  */
2569 static asection mips_elf_text_section;
2570 static asection *mips_elf_text_section_ptr;
2571 static asymbol mips_elf_text_symbol;
2572 static asymbol *mips_elf_text_symbol_ptr;
2573
2574 static asection mips_elf_data_section;
2575 static asection *mips_elf_data_section_ptr;
2576 static asymbol mips_elf_data_symbol;
2577 static asymbol *mips_elf_data_symbol_ptr;
2578
2579 /* Handle the special MIPS section numbers that a symbol may use.
2580    This is used for both the 32-bit and the 64-bit ABI.  */
2581
2582 void
2583 _bfd_mips_elf_symbol_processing (abfd, asym)
2584      bfd *abfd;
2585      asymbol *asym;
2586 {
2587   elf_symbol_type *elfsym;
2588
2589   elfsym = (elf_symbol_type *) asym;
2590   switch (elfsym->internal_elf_sym.st_shndx)
2591     {
2592     case SHN_MIPS_ACOMMON:
2593       /* This section is used in a dynamically linked executable file.
2594          It is an allocated common section.  The dynamic linker can
2595          either resolve these symbols to something in a shared
2596          library, or it can just leave them here.  For our purposes,
2597          we can consider these symbols to be in a new section.  */
2598       if (mips_elf_acom_section.name == NULL)
2599         {
2600           /* Initialize the acommon section.  */
2601           mips_elf_acom_section.name = ".acommon";
2602           mips_elf_acom_section.flags = SEC_ALLOC;
2603           mips_elf_acom_section.output_section = &mips_elf_acom_section;
2604           mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
2605           mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
2606           mips_elf_acom_symbol.name = ".acommon";
2607           mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
2608           mips_elf_acom_symbol.section = &mips_elf_acom_section;
2609           mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
2610         }
2611       asym->section = &mips_elf_acom_section;
2612       break;
2613
2614     case SHN_COMMON:
2615       /* Common symbols less than the GP size are automatically
2616          treated as SHN_MIPS_SCOMMON symbols.  */
2617       if (asym->value > elf_gp_size (abfd))
2618         break;
2619       /* Fall through.  */
2620     case SHN_MIPS_SCOMMON:
2621       if (mips_elf_scom_section.name == NULL)
2622         {
2623           /* Initialize the small common section.  */
2624           mips_elf_scom_section.name = ".scommon";
2625           mips_elf_scom_section.flags = SEC_IS_COMMON;
2626           mips_elf_scom_section.output_section = &mips_elf_scom_section;
2627           mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
2628           mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
2629           mips_elf_scom_symbol.name = ".scommon";
2630           mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
2631           mips_elf_scom_symbol.section = &mips_elf_scom_section;
2632           mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
2633         }
2634       asym->section = &mips_elf_scom_section;
2635       asym->value = elfsym->internal_elf_sym.st_size;
2636       break;
2637
2638     case SHN_MIPS_SUNDEFINED:
2639       asym->section = bfd_und_section_ptr;
2640       break;
2641
2642 #if 0 /* for SGI_COMPAT */
2643     case SHN_MIPS_TEXT:
2644       asym->section = mips_elf_text_section_ptr;
2645       break;
2646
2647     case SHN_MIPS_DATA:
2648       asym->section = mips_elf_data_section_ptr;
2649       break;
2650 #endif
2651     }
2652 }
2653 \f
2654 /* When creating an Irix 5 executable, we need REGINFO and RTPROC
2655    segments.  */
2656
2657 static int
2658 mips_elf_additional_program_headers (abfd)
2659      bfd *abfd;
2660 {
2661   asection *s;
2662   int ret;
2663
2664   ret = 0;
2665
2666   if (! SGI_COMPAT (abfd))
2667     return ret;
2668
2669   s = bfd_get_section_by_name (abfd, ".reginfo");
2670   if (s != NULL && (s->flags & SEC_LOAD) != 0)
2671     {
2672       /* We need a PT_MIPS_REGINFO segment.  */
2673       ++ret;
2674     }
2675
2676   if (bfd_get_section_by_name (abfd, ".dynamic") != NULL
2677       && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2678     {
2679       /* We need a PT_MIPS_RTPROC segment.  */
2680       ++ret;
2681     }
2682
2683   return ret;
2684 }
2685
2686 /* Modify the segment map for an Irix 5 executable.  */
2687
2688 static boolean
2689 mips_elf_modify_segment_map (abfd)
2690      bfd *abfd;
2691 {
2692   asection *s;
2693   struct elf_segment_map *m, **pm;
2694
2695   if (! SGI_COMPAT (abfd))
2696     return true;
2697
2698   /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
2699      segment.  */
2700   s = bfd_get_section_by_name (abfd, ".reginfo");
2701   if (s != NULL && (s->flags & SEC_LOAD) != 0)
2702     {
2703       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2704         if (m->p_type == PT_MIPS_REGINFO)
2705           break;
2706       if (m == NULL)
2707         {
2708           m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2709           if (m == NULL)
2710             return false;
2711
2712           m->p_type = PT_MIPS_REGINFO;
2713           m->count = 1;
2714           m->sections[0] = s;
2715
2716           /* We want to put it after the PHDR and INTERP segments.  */
2717           pm = &elf_tdata (abfd)->segment_map;
2718           while (*pm != NULL
2719                  && ((*pm)->p_type == PT_PHDR
2720                      || (*pm)->p_type == PT_INTERP))
2721             pm = &(*pm)->next;
2722
2723           m->next = *pm;
2724           *pm = m;
2725         }
2726     }
2727
2728   /* If there are .dynamic and .mdebug sections, we make a room for
2729      the RTPROC header.  FIXME: Rewrite without section names.  */
2730   if (bfd_get_section_by_name (abfd, ".interp") == NULL
2731       && bfd_get_section_by_name (abfd, ".dynamic") != NULL
2732       && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
2733     {
2734       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2735         if (m->p_type == PT_MIPS_RTPROC)
2736           break;
2737       if (m == NULL)
2738         {
2739           m = (struct elf_segment_map *) bfd_zalloc (abfd, sizeof *m);
2740           if (m == NULL)
2741             return false;
2742
2743           m->p_type = PT_MIPS_RTPROC;
2744
2745           s = bfd_get_section_by_name (abfd, ".rtproc");
2746           if (s == NULL)
2747             {
2748               m->count = 0;
2749               m->p_flags = 0;
2750               m->p_flags_valid = 1;
2751             }
2752           else
2753             {
2754               m->count = 1;
2755               m->sections[0] = s;
2756             }
2757
2758           /* We want to put it after the DYNAMIC segment.  */
2759           pm = &elf_tdata (abfd)->segment_map;
2760           while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
2761             pm = &(*pm)->next;
2762           if (*pm != NULL)
2763             pm = &(*pm)->next;
2764
2765           m->next = *pm;
2766           *pm = m;
2767         }
2768     }
2769
2770   /* On Irix 5, the PT_DYNAMIC segment includes the .dynamic, .dynstr,
2771      .dynsym, and .hash sections, and everything in between.  */
2772   for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
2773     if ((*pm)->p_type == PT_DYNAMIC)
2774       break;
2775   m = *pm;
2776   if (m != NULL
2777       && m->count == 1
2778       && strcmp (m->sections[0]->name, ".dynamic") == 0)
2779     {
2780       static const char *sec_names[] =
2781         { ".dynamic", ".dynstr", ".dynsym", ".hash" };
2782       bfd_vma low, high;
2783       unsigned int i, c;
2784       struct elf_segment_map *n;
2785
2786       low = 0xffffffff;
2787       high = 0;
2788       for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
2789         {
2790           s = bfd_get_section_by_name (abfd, sec_names[i]);
2791           if (s != NULL && (s->flags & SEC_LOAD) != 0)
2792             {
2793               bfd_size_type sz;
2794
2795               if (low > s->vma)
2796                 low = s->vma;
2797               sz = s->_cooked_size;
2798               if (sz == 0)
2799                 sz = s->_raw_size;
2800               if (high < s->vma + sz)
2801                 high = s->vma + sz;
2802             }
2803         }
2804
2805       c = 0;
2806       for (s = abfd->sections; s != NULL; s = s->next)
2807         if ((s->flags & SEC_LOAD) != 0
2808             && s->vma >= low
2809             && ((s->vma
2810                  + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2811                 <= high))
2812           ++c;
2813
2814       n = ((struct elf_segment_map *)
2815            bfd_zalloc (abfd, sizeof *n + (c - 1) * sizeof (asection *)));
2816       if (n == NULL)
2817         return false;
2818       *n = *m;
2819       n->count = c;
2820
2821       i = 0;
2822       for (s = abfd->sections; s != NULL; s = s->next)
2823         {
2824           if ((s->flags & SEC_LOAD) != 0
2825               && s->vma >= low
2826               && ((s->vma
2827                    + (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size))
2828                   <= high))
2829             {
2830               n->sections[i] = s;
2831               ++i;
2832             }
2833         }
2834
2835       *pm = n;
2836     }
2837
2838   return true;
2839 }
2840 \f
2841 /* The structure of the runtime procedure descriptor created by the
2842    loader for use by the static exception system.  */
2843
2844 typedef struct runtime_pdr {
2845         bfd_vma adr;            /* memory address of start of procedure */
2846         long    regmask;        /* save register mask */
2847         long    regoffset;      /* save register offset */
2848         long    fregmask;       /* save floating point register mask */
2849         long    fregoffset;     /* save floating point register offset */
2850         long    frameoffset;    /* frame size */
2851         short   framereg;       /* frame pointer register */
2852         short   pcreg;          /* offset or reg of return pc */
2853         long    irpss;          /* index into the runtime string table */
2854         long    reserved;
2855         struct exception_info *exception_info;/* pointer to exception array */
2856 } RPDR, *pRPDR;
2857 #define cbRPDR sizeof(RPDR)
2858 #define rpdNil ((pRPDR) 0)
2859
2860 /* Swap RPDR (runtime procedure table entry) for output.  */
2861
2862 static void ecoff_swap_rpdr_out
2863   PARAMS ((bfd *, const RPDR *, struct rpdr_ext *));
2864
2865 static void
2866 ecoff_swap_rpdr_out (abfd, in, ex)
2867      bfd *abfd;
2868      const RPDR *in;
2869      struct rpdr_ext *ex;
2870 {
2871   /* ecoff_put_off was defined in ecoffswap.h.  */
2872   ecoff_put_off (abfd, in->adr, (bfd_byte *) ex->p_adr);
2873   bfd_h_put_32 (abfd, in->regmask, (bfd_byte *) ex->p_regmask);
2874   bfd_h_put_32 (abfd, in->regoffset, (bfd_byte *) ex->p_regoffset);
2875   bfd_h_put_32 (abfd, in->fregmask, (bfd_byte *) ex->p_fregmask);
2876   bfd_h_put_32 (abfd, in->fregoffset, (bfd_byte *) ex->p_fregoffset);
2877   bfd_h_put_32 (abfd, in->frameoffset, (bfd_byte *) ex->p_frameoffset);
2878
2879   bfd_h_put_16 (abfd, in->framereg, (bfd_byte *) ex->p_framereg);
2880   bfd_h_put_16 (abfd, in->pcreg, (bfd_byte *) ex->p_pcreg);
2881
2882   bfd_h_put_32 (abfd, in->irpss, (bfd_byte *) ex->p_irpss);
2883 #if 0 /* FIXME */
2884   ecoff_put_off (abfd, in->exception_info, (bfd_byte *) ex->p_exception_info);
2885 #endif
2886 }
2887 \f
2888 /* Read ECOFF debugging information from a .mdebug section into a
2889    ecoff_debug_info structure.  */
2890
2891 boolean
2892 _bfd_mips_elf_read_ecoff_info (abfd, section, debug)
2893      bfd *abfd;
2894      asection *section;
2895      struct ecoff_debug_info *debug;
2896 {
2897   HDRR *symhdr;
2898   const struct ecoff_debug_swap *swap;
2899   char *ext_hdr = NULL;
2900
2901   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
2902
2903   ext_hdr = (char *) bfd_malloc ((size_t) swap->external_hdr_size);
2904   if (ext_hdr == NULL && swap->external_hdr_size != 0)
2905     goto error_return;
2906
2907   if (bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
2908                                 swap->external_hdr_size)
2909       == false)
2910     goto error_return;
2911
2912   symhdr = &debug->symbolic_header;
2913   (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
2914
2915   /* The symbolic header contains absolute file offsets and sizes to
2916      read.  */
2917 #define READ(ptr, offset, count, size, type)                            \
2918   if (symhdr->count == 0)                                               \
2919     debug->ptr = NULL;                                                  \
2920   else                                                                  \
2921     {                                                                   \
2922       debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
2923       if (debug->ptr == NULL)                                           \
2924         goto error_return;                                              \
2925       if (bfd_seek (abfd, (file_ptr) symhdr->offset, SEEK_SET) != 0     \
2926           || (bfd_read (debug->ptr, size, symhdr->count,                \
2927                         abfd) != size * symhdr->count))                 \
2928         goto error_return;                                              \
2929     }
2930
2931   READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
2932   READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
2933   READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
2934   READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
2935   READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
2936   READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
2937         union aux_ext *);
2938   READ (ss, cbSsOffset, issMax, sizeof (char), char *);
2939   READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
2940   READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
2941   READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
2942   READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, PTR);
2943 #undef READ
2944
2945   debug->fdr = NULL;
2946   debug->adjust = NULL;
2947
2948   return true;
2949
2950  error_return:
2951   if (ext_hdr != NULL)
2952     free (ext_hdr);
2953   if (debug->line != NULL)
2954     free (debug->line);
2955   if (debug->external_dnr != NULL)
2956     free (debug->external_dnr);
2957   if (debug->external_pdr != NULL)
2958     free (debug->external_pdr);
2959   if (debug->external_sym != NULL)
2960     free (debug->external_sym);
2961   if (debug->external_opt != NULL)
2962     free (debug->external_opt);
2963   if (debug->external_aux != NULL)
2964     free (debug->external_aux);
2965   if (debug->ss != NULL)
2966     free (debug->ss);
2967   if (debug->ssext != NULL)
2968     free (debug->ssext);
2969   if (debug->external_fdr != NULL)
2970     free (debug->external_fdr);
2971   if (debug->external_rfd != NULL)
2972     free (debug->external_rfd);
2973   if (debug->external_ext != NULL)
2974     free (debug->external_ext);
2975   return false;
2976 }
2977 \f
2978 /* MIPS ELF local labels start with '$', not 'L'.  */
2979
2980 /*ARGSUSED*/
2981 static boolean
2982 mips_elf_is_local_label_name (abfd, name)
2983      bfd *abfd;
2984      const char *name;
2985 {
2986   if (name[0] == '$')
2987     return true;
2988
2989   /* On Irix 6, the labels go back to starting with '.', so we accept
2990      the generic ELF local label syntax as well.  */
2991   return _bfd_elf_is_local_label_name (abfd, name);
2992 }
2993
2994 /* MIPS ELF uses a special find_nearest_line routine in order the
2995    handle the ECOFF debugging information.  */
2996
2997 struct mips_elf_find_line
2998 {
2999   struct ecoff_debug_info d;
3000   struct ecoff_find_line i;
3001 };
3002
3003 boolean
3004 _bfd_mips_elf_find_nearest_line (abfd, section, symbols, offset, filename_ptr,
3005                                  functionname_ptr, line_ptr)
3006      bfd *abfd;
3007      asection *section;
3008      asymbol **symbols;
3009      bfd_vma offset;
3010      const char **filename_ptr;
3011      const char **functionname_ptr;
3012      unsigned int *line_ptr;
3013 {
3014   asection *msec;
3015
3016   msec = bfd_get_section_by_name (abfd, ".mdebug");
3017   if (msec != NULL)
3018     {
3019       flagword origflags;
3020       struct mips_elf_find_line *fi;
3021       const struct ecoff_debug_swap * const swap =
3022         get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3023
3024       /* If we are called during a link, mips_elf_final_link may have
3025          cleared the SEC_HAS_CONTENTS field.  We force it back on here
3026          if appropriate (which it normally will be).  */
3027       origflags = msec->flags;
3028       if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
3029         msec->flags |= SEC_HAS_CONTENTS;
3030
3031       fi = elf_tdata (abfd)->find_line_info;
3032       if (fi == NULL)
3033         {
3034           bfd_size_type external_fdr_size;
3035           char *fraw_src;
3036           char *fraw_end;
3037           struct fdr *fdr_ptr;
3038
3039           fi = ((struct mips_elf_find_line *)
3040                 bfd_zalloc (abfd, sizeof (struct mips_elf_find_line)));
3041           if (fi == NULL)
3042             {
3043               msec->flags = origflags;
3044               return false;
3045             }
3046
3047           if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
3048             {
3049               msec->flags = origflags;
3050               return false;
3051             }
3052
3053           /* Swap in the FDR information.  */
3054           fi->d.fdr = ((struct fdr *)
3055                        bfd_alloc (abfd,
3056                                   (fi->d.symbolic_header.ifdMax *
3057                                    sizeof (struct fdr))));
3058           if (fi->d.fdr == NULL)
3059             {
3060               msec->flags = origflags;
3061               return false;
3062             }
3063           external_fdr_size = swap->external_fdr_size;
3064           fdr_ptr = fi->d.fdr;
3065           fraw_src = (char *) fi->d.external_fdr;
3066           fraw_end = (fraw_src
3067                       + fi->d.symbolic_header.ifdMax * external_fdr_size);
3068           for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
3069             (*swap->swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
3070
3071           elf_tdata (abfd)->find_line_info = fi;
3072
3073           /* Note that we don't bother to ever free this information.
3074              find_nearest_line is either called all the time, as in
3075              objdump -l, so the information should be saved, or it is
3076              rarely called, as in ld error messages, so the memory
3077              wasted is unimportant.  Still, it would probably be a
3078              good idea for free_cached_info to throw it away.  */
3079         }
3080
3081       if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
3082                                   &fi->i, filename_ptr, functionname_ptr,
3083                                   line_ptr))
3084         {
3085           msec->flags = origflags;
3086           return true;
3087         }
3088
3089       msec->flags = origflags;
3090     }
3091
3092   /* Fall back on the generic ELF find_nearest_line routine.  */
3093
3094   return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
3095                                      filename_ptr, functionname_ptr,
3096                                      line_ptr);
3097 }
3098 \f
3099   /* The mips16 compiler uses a couple of special sections to handle
3100      floating point arguments.
3101
3102      Section names that look like .mips16.fn.FNNAME contain stubs that
3103      copy floating point arguments from the fp regs to the gp regs and
3104      then jump to FNNAME.  If any 32 bit function calls FNNAME, the
3105      call should be redirected to the stub instead.  If no 32 bit
3106      function calls FNNAME, the stub should be discarded.  We need to
3107      consider any reference to the function, not just a call, because
3108      if the address of the function is taken we will need the stub,
3109      since the address might be passed to a 32 bit function.
3110
3111      Section names that look like .mips16.call.FNNAME contain stubs
3112      that copy floating point arguments from the gp regs to the fp
3113      regs and then jump to FNNAME.  If FNNAME is a 32 bit function,
3114      then any 16 bit function that calls FNNAME should be redirected
3115      to the stub instead.  If FNNAME is not a 32 bit function, the
3116      stub should be discarded.
3117
3118      .mips16.call.fp.FNNAME sections are similar, but contain stubs
3119      which call FNNAME and then copy the return value from the fp regs
3120      to the gp regs.  These stubs store the return value in $18 while
3121      calling FNNAME; any function which might call one of these stubs
3122      must arrange to save $18 around the call.  (This case is not
3123      needed for 32 bit functions that call 16 bit functions, because
3124      16 bit functions always return floating point values in both
3125      $f0/$f1 and $2/$3.)
3126
3127      Note that in all cases FNNAME might be defined statically.
3128      Therefore, FNNAME is not used literally.  Instead, the relocation
3129      information will indicate which symbol the section is for.
3130
3131      We record any stubs that we find in the symbol table.  */
3132
3133 #define FN_STUB ".mips16.fn."
3134 #define CALL_STUB ".mips16.call."
3135 #define CALL_FP_STUB ".mips16.call.fp."
3136
3137 /* The MIPS ELF linker needs additional information for each symbol in
3138    the global hash table.  */
3139
3140 struct mips_elf_link_hash_entry
3141 {
3142   struct elf_link_hash_entry root;
3143
3144   /* External symbol information.  */
3145   EXTR esym;
3146
3147   /* Number of MIPS_32 or MIPS_REL32 relocs against this symbol.  */
3148   unsigned int mips_32_relocs;
3149
3150   /* If there is a stub that 32 bit functions should use to call this
3151      16 bit function, this points to the section containing the stub.  */
3152   asection *fn_stub;
3153
3154   /* Whether we need the fn_stub; this is set if this symbol appears
3155      in any relocs other than a 16 bit call.  */
3156   boolean need_fn_stub;
3157
3158   /* If there is a stub that 16 bit functions should use to call this
3159      32 bit function, this points to the section containing the stub.  */
3160   asection *call_stub;
3161
3162   /* This is like the call_stub field, but it is used if the function
3163      being called returns a floating point value.  */
3164   asection *call_fp_stub;
3165 };
3166
3167 /* MIPS ELF linker hash table.  */
3168
3169 struct mips_elf_link_hash_table
3170 {
3171   struct elf_link_hash_table root;
3172   /* String section indices for the dynamic section symbols.  */
3173   bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
3174   /* The number of .rtproc entries.  */
3175   bfd_size_type procedure_count;
3176   /* The size of the .compact_rel section (if SGI_COMPAT).  */
3177   bfd_size_type compact_rel_size;
3178   /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
3179      entry is set to the address of __rld_obj_head as in Irix 5. */
3180   boolean use_rld_obj_head;
3181   /* This is the value of the __rld_map or __rld_obj_head symbol.  */
3182   bfd_vma rld_value;
3183   /* This is set if we see any mips16 stub sections. */
3184   boolean mips16_stubs_seen;
3185 };
3186
3187 /* Look up an entry in a MIPS ELF linker hash table.  */
3188
3189 #define mips_elf_link_hash_lookup(table, string, create, copy, follow)  \
3190   ((struct mips_elf_link_hash_entry *)                                  \
3191    elf_link_hash_lookup (&(table)->root, (string), (create),            \
3192                          (copy), (follow)))
3193
3194 /* Traverse a MIPS ELF linker hash table.  */
3195
3196 #define mips_elf_link_hash_traverse(table, func, info)                  \
3197   (elf_link_hash_traverse                                               \
3198    (&(table)->root,                                                     \
3199     (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func),  \
3200     (info)))
3201
3202 /* Get the MIPS ELF linker hash table from a link_info structure.  */
3203
3204 #define mips_elf_hash_table(p) \
3205   ((struct mips_elf_link_hash_table *) ((p)->hash))
3206
3207 static boolean mips_elf_output_extsym
3208   PARAMS ((struct mips_elf_link_hash_entry *, PTR));
3209
3210 /* Create an entry in a MIPS ELF linker hash table.  */
3211
3212 static struct bfd_hash_entry *
3213 mips_elf_link_hash_newfunc (entry, table, string)
3214      struct bfd_hash_entry *entry;
3215      struct bfd_hash_table *table;
3216      const char *string;
3217 {
3218   struct mips_elf_link_hash_entry *ret =
3219     (struct mips_elf_link_hash_entry *) entry;
3220
3221   /* Allocate the structure if it has not already been allocated by a
3222      subclass.  */
3223   if (ret == (struct mips_elf_link_hash_entry *) NULL)
3224     ret = ((struct mips_elf_link_hash_entry *)
3225            bfd_hash_allocate (table,
3226                               sizeof (struct mips_elf_link_hash_entry)));
3227   if (ret == (struct mips_elf_link_hash_entry *) NULL)
3228     return (struct bfd_hash_entry *) ret;
3229
3230   /* Call the allocation method of the superclass.  */
3231   ret = ((struct mips_elf_link_hash_entry *)
3232          _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3233                                      table, string));
3234   if (ret != (struct mips_elf_link_hash_entry *) NULL)
3235     {
3236       /* Set local fields.  */
3237       memset (&ret->esym, 0, sizeof (EXTR));
3238       /* We use -2 as a marker to indicate that the information has
3239          not been set.  -1 means there is no associated ifd.  */
3240       ret->esym.ifd = -2;
3241       ret->mips_32_relocs = 0;
3242       ret->fn_stub = NULL;
3243       ret->need_fn_stub = false;
3244       ret->call_stub = NULL;
3245       ret->call_fp_stub = NULL;
3246     }
3247
3248   return (struct bfd_hash_entry *) ret;
3249 }
3250
3251 /* Create a MIPS ELF linker hash table.  */
3252
3253 static struct bfd_link_hash_table *
3254 mips_elf_link_hash_table_create (abfd)
3255      bfd *abfd;
3256 {
3257   struct mips_elf_link_hash_table *ret;
3258   unsigned int i;
3259
3260   ret = ((struct mips_elf_link_hash_table *)
3261          bfd_alloc (abfd, sizeof (struct mips_elf_link_hash_table)));
3262   if (ret == (struct mips_elf_link_hash_table *) NULL)
3263     return NULL;
3264
3265   if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3266                                        mips_elf_link_hash_newfunc))
3267     {
3268       bfd_release (abfd, ret);
3269       return NULL;
3270     }
3271
3272   for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
3273     ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
3274   ret->procedure_count = 0;
3275   ret->compact_rel_size = 0;
3276   ret->use_rld_obj_head = false;
3277   ret->rld_value = 0;
3278   ret->mips16_stubs_seen = false;
3279
3280   return &ret->root.root;
3281 }
3282
3283 /* Hook called by the linker routine which adds symbols from an object
3284    file.  We must handle the special MIPS section numbers here.  */
3285
3286 /*ARGSUSED*/
3287 static boolean
3288 mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
3289      bfd *abfd;
3290      struct bfd_link_info *info;
3291      const Elf_Internal_Sym *sym;
3292      const char **namep;
3293      flagword *flagsp;
3294      asection **secp;
3295      bfd_vma *valp;
3296 {
3297   if (SGI_COMPAT (abfd)
3298       && (abfd->flags & DYNAMIC) != 0
3299       && strcmp (*namep, "_rld_new_interface") == 0)
3300     {
3301       /* Skip Irix 5 rld entry name.  */
3302       *namep = NULL;
3303       return true;
3304     }
3305
3306   switch (sym->st_shndx)
3307     {
3308     case SHN_COMMON:
3309       /* Common symbols less than the GP size are automatically
3310          treated as SHN_MIPS_SCOMMON symbols.  */
3311       if (sym->st_size > elf_gp_size (abfd))
3312         break;
3313       /* Fall through.  */
3314     case SHN_MIPS_SCOMMON:
3315       *secp = bfd_make_section_old_way (abfd, ".scommon");
3316       (*secp)->flags |= SEC_IS_COMMON;
3317       *valp = sym->st_size;
3318       break;
3319
3320     case SHN_MIPS_TEXT:
3321       /* This section is used in a shared object.  */
3322       if (mips_elf_text_section_ptr == NULL)
3323         {
3324           /* Initialize the section.  */
3325           mips_elf_text_section.name = ".text";
3326           mips_elf_text_section.flags = SEC_NO_FLAGS;
3327           mips_elf_text_section.output_section = NULL;
3328           mips_elf_text_section.symbol = &mips_elf_text_symbol;
3329           mips_elf_text_section.symbol_ptr_ptr = &mips_elf_text_symbol_ptr;
3330           mips_elf_text_symbol.name = ".text";
3331           mips_elf_text_symbol.flags = BSF_SECTION_SYM;
3332           mips_elf_text_symbol.section = &mips_elf_text_section;
3333           mips_elf_text_symbol_ptr = &mips_elf_text_symbol;
3334           mips_elf_text_section_ptr = &mips_elf_text_section;
3335         }
3336       /* This code used to do *secp = bfd_und_section_ptr if
3337          info->shared.  I don't know why, and that doesn't make sense,
3338          so I took it out.  */
3339       *secp = mips_elf_text_section_ptr;
3340       break;
3341
3342     case SHN_MIPS_ACOMMON:
3343       /* Fall through. XXX Can we treat this as allocated data?  */
3344     case SHN_MIPS_DATA:
3345       /* This section is used in a shared object.  */
3346       if (mips_elf_data_section_ptr == NULL)
3347         {
3348           /* Initialize the section.  */
3349           mips_elf_data_section.name = ".data";
3350           mips_elf_data_section.flags = SEC_NO_FLAGS;
3351           mips_elf_data_section.output_section = NULL;
3352           mips_elf_data_section.symbol = &mips_elf_data_symbol;
3353           mips_elf_data_section.symbol_ptr_ptr = &mips_elf_data_symbol_ptr;
3354           mips_elf_data_symbol.name = ".data";
3355           mips_elf_data_symbol.flags = BSF_SECTION_SYM;
3356           mips_elf_data_symbol.section = &mips_elf_data_section;
3357           mips_elf_data_symbol_ptr = &mips_elf_data_symbol;
3358           mips_elf_data_section_ptr = &mips_elf_data_section;
3359         }
3360       /* This code used to do *secp = bfd_und_section_ptr if
3361          info->shared.  I don't know why, and that doesn't make sense,
3362          so I took it out.  */
3363       *secp = mips_elf_data_section_ptr;
3364       break;
3365
3366     case SHN_MIPS_SUNDEFINED:
3367       *secp = bfd_und_section_ptr;
3368       break;
3369     }
3370
3371   if (SGI_COMPAT (abfd)
3372       && ! info->shared
3373       && info->hash->creator == abfd->xvec
3374       && strcmp (*namep, "__rld_obj_head") == 0)
3375     {
3376       struct elf_link_hash_entry *h;
3377
3378       /* Mark __rld_obj_head as dynamic.  */
3379       h = NULL;
3380       if (! (_bfd_generic_link_add_one_symbol
3381              (info, abfd, *namep, BSF_GLOBAL, *secp,
3382               (bfd_vma) *valp, (const char *) NULL, false,
3383               get_elf_backend_data (abfd)->collect,
3384               (struct bfd_link_hash_entry **) &h)))
3385         return false;
3386       h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
3387       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3388       h->type = STT_OBJECT;
3389
3390       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3391         return false;
3392
3393       mips_elf_hash_table (info)->use_rld_obj_head = true;
3394     }
3395
3396   /* If this is a mips16 text symbol, add 1 to the value to make it
3397      odd.  This will cause something like .word SYM to come up with
3398      the right value when it is loaded into the PC.  */
3399   if (sym->st_other == STO_MIPS16)
3400     ++*valp;
3401
3402   return true;
3403 }
3404
3405 /* Structure used to pass information to mips_elf_output_extsym.  */
3406
3407 struct extsym_info
3408 {
3409   bfd *abfd;
3410   struct bfd_link_info *info;
3411   struct ecoff_debug_info *debug;
3412   const struct ecoff_debug_swap *swap;
3413   boolean failed;
3414 };
3415
3416 /* This routine is used to write out ECOFF debugging external symbol
3417    information.  It is called via mips_elf_link_hash_traverse.  The
3418    ECOFF external symbol information must match the ELF external
3419    symbol information.  Unfortunately, at this point we don't know
3420    whether a symbol is required by reloc information, so the two
3421    tables may wind up being different.  We must sort out the external
3422    symbol information before we can set the final size of the .mdebug
3423    section, and we must set the size of the .mdebug section before we
3424    can relocate any sections, and we can't know which symbols are
3425    required by relocation until we relocate the sections.
3426    Fortunately, it is relatively unlikely that any symbol will be
3427    stripped but required by a reloc.  In particular, it can not happen
3428    when generating a final executable.  */
3429
3430 static boolean
3431 mips_elf_output_extsym (h, data)
3432      struct mips_elf_link_hash_entry *h;
3433      PTR data;
3434 {
3435   struct extsym_info *einfo = (struct extsym_info *) data;
3436   boolean strip;
3437   asection *sec, *output_section;
3438
3439   if (h->root.indx == -2)
3440     strip = false;
3441   else if (((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3442             || (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0)
3443            && (h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
3444            && (h->root.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)
3445     strip = true;
3446   else if (einfo->info->strip == strip_all
3447            || (einfo->info->strip == strip_some
3448                && bfd_hash_lookup (einfo->info->keep_hash,
3449                                    h->root.root.root.string,
3450                                    false, false) == NULL))
3451     strip = true;
3452   else
3453     strip = false;
3454
3455   if (strip)
3456     return true;
3457
3458   if (h->esym.ifd == -2)
3459     {
3460       h->esym.jmptbl = 0;
3461       h->esym.cobol_main = 0;
3462       h->esym.weakext = 0;
3463       h->esym.reserved = 0;
3464       h->esym.ifd = ifdNil;
3465       h->esym.asym.value = 0;
3466       h->esym.asym.st = stGlobal;
3467
3468       if (SGI_COMPAT (einfo->abfd)
3469           && (h->root.root.type == bfd_link_hash_undefined
3470               || h->root.root.type == bfd_link_hash_undefweak))
3471         {
3472           const char *name;
3473
3474           /* Use undefined class.  Also, set class and type for some
3475              special symbols.  */
3476           name = h->root.root.root.string;
3477           if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3478               || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3479             {
3480               h->esym.asym.sc = scData;
3481               h->esym.asym.st = stLabel;
3482               h->esym.asym.value = 0;
3483             }
3484           else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3485             {
3486               h->esym.asym.sc = scAbs;
3487               h->esym.asym.st = stLabel;
3488               h->esym.asym.value =
3489                 mips_elf_hash_table (einfo->info)->procedure_count;
3490             }
3491           else if (strcmp (name, "_gp_disp") == 0)
3492             {
3493               h->esym.asym.sc = scAbs;
3494               h->esym.asym.st = stLabel;
3495               h->esym.asym.value = elf_gp (einfo->abfd);
3496             }
3497           else
3498             h->esym.asym.sc = scUndefined;
3499         }
3500       else if (h->root.root.type != bfd_link_hash_defined
3501           && h->root.root.type != bfd_link_hash_defweak)
3502         h->esym.asym.sc = scAbs;
3503       else
3504         {
3505           const char *name;
3506
3507           sec = h->root.root.u.def.section;
3508           output_section = sec->output_section;
3509
3510           /* When making a shared library and symbol h is the one from
3511              the another shared library, OUTPUT_SECTION may be null.  */
3512           if (output_section == NULL)
3513             h->esym.asym.sc = scUndefined;
3514           else
3515             {
3516               name = bfd_section_name (output_section->owner, output_section);
3517
3518               if (strcmp (name, ".text") == 0)
3519                 h->esym.asym.sc = scText;
3520               else if (strcmp (name, ".data") == 0)
3521                 h->esym.asym.sc = scData;
3522               else if (strcmp (name, ".sdata") == 0)
3523                 h->esym.asym.sc = scSData;
3524               else if (strcmp (name, ".rodata") == 0
3525                        || strcmp (name, ".rdata") == 0)
3526                 h->esym.asym.sc = scRData;
3527               else if (strcmp (name, ".bss") == 0)
3528                 h->esym.asym.sc = scBss;
3529               else if (strcmp (name, ".sbss") == 0)
3530                 h->esym.asym.sc = scSBss;
3531               else if (strcmp (name, ".init") == 0)
3532                 h->esym.asym.sc = scInit;
3533               else if (strcmp (name, ".fini") == 0)
3534                 h->esym.asym.sc = scFini;
3535               else
3536                 h->esym.asym.sc = scAbs;
3537             }
3538         }
3539
3540       h->esym.asym.reserved = 0;
3541       h->esym.asym.index = indexNil;
3542     }
3543
3544   if (h->root.root.type == bfd_link_hash_common)
3545     h->esym.asym.value = h->root.root.u.c.size;
3546   else if (h->root.root.type == bfd_link_hash_defined
3547            || h->root.root.type == bfd_link_hash_defweak)
3548     {
3549       if (h->esym.asym.sc == scCommon)
3550         h->esym.asym.sc = scBss;
3551       else if (h->esym.asym.sc == scSCommon)
3552         h->esym.asym.sc = scSBss;
3553
3554       sec = h->root.root.u.def.section;
3555       output_section = sec->output_section;
3556       if (output_section != NULL)
3557         h->esym.asym.value = (h->root.root.u.def.value
3558                               + sec->output_offset
3559                               + output_section->vma);
3560       else
3561         h->esym.asym.value = 0;
3562     }
3563   else if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3564     {
3565       /* Set type and value for a symbol with a function stub.  */
3566       h->esym.asym.st = stProc;
3567       sec = h->root.root.u.def.section;
3568       if (sec == NULL)
3569         h->esym.asym.value = 0;
3570       else
3571         {
3572           output_section = sec->output_section;
3573           if (output_section != NULL)
3574             h->esym.asym.value = (h->root.plt_offset
3575                                   + sec->output_offset
3576                                   + output_section->vma);
3577           else
3578             h->esym.asym.value = 0;
3579         }
3580 #if 0 /* FIXME?  */
3581       h->esym.ifd = 0;
3582 #endif
3583     }
3584
3585   if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3586                                       h->root.root.root.string,
3587                                       &h->esym))
3588     {
3589       einfo->failed = true;
3590       return false;
3591     }
3592
3593   return true;
3594 }
3595
3596 /* Create a runtime procedure table from the .mdebug section.  */
3597
3598 static boolean
3599 mips_elf_create_procedure_table (handle, abfd, info, s, debug)
3600      PTR handle;
3601      bfd *abfd;
3602      struct bfd_link_info *info;
3603      asection *s;
3604      struct ecoff_debug_info *debug;
3605 {
3606   const struct ecoff_debug_swap *swap;
3607   HDRR *hdr = &debug->symbolic_header;
3608   RPDR *rpdr, *rp;
3609   struct rpdr_ext *erp;
3610   PTR rtproc;
3611   struct pdr_ext *epdr;
3612   struct sym_ext *esym;
3613   char *ss, **sv;
3614   char *str;
3615   unsigned long size, count;
3616   unsigned long sindex;
3617   unsigned long i;
3618   PDR pdr;
3619   SYMR sym;
3620   const char *no_name_func = "static procedure (no name)";
3621
3622   epdr = NULL;
3623   rpdr = NULL;
3624   esym = NULL;
3625   ss = NULL;
3626   sv = NULL;
3627
3628   swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3629
3630   sindex = strlen (no_name_func) + 1;
3631   count = hdr->ipdMax;
3632   if (count > 0)
3633     {
3634       size = swap->external_pdr_size;
3635
3636       epdr = (struct pdr_ext *) bfd_malloc (size * count);
3637       if (epdr == NULL)
3638         goto error_return;
3639
3640       if (! _bfd_ecoff_get_accumulated_pdr (handle, (PTR) epdr))
3641         goto error_return;
3642
3643       size = sizeof (RPDR);
3644       rp = rpdr = (RPDR *) bfd_malloc (size * count);
3645       if (rpdr == NULL)
3646         goto error_return;
3647
3648       sv = (char **) bfd_malloc (sizeof (char *) * count);
3649       if (sv == NULL)
3650         goto error_return;
3651
3652       count = hdr->isymMax;
3653       size = swap->external_sym_size;
3654       esym = (struct sym_ext *) bfd_malloc (size * count);
3655       if (esym == NULL)
3656         goto error_return;
3657
3658       if (! _bfd_ecoff_get_accumulated_sym (handle, (PTR) esym))
3659         goto error_return;
3660
3661       count = hdr->issMax;
3662       ss = (char *) bfd_malloc (count);
3663       if (ss == NULL)
3664         goto error_return;
3665       if (! _bfd_ecoff_get_accumulated_ss (handle, (PTR) ss))
3666         goto error_return;
3667
3668       count = hdr->ipdMax;
3669       for (i = 0; i < count; i++, rp++)
3670         {
3671           (*swap->swap_pdr_in) (abfd, (PTR) (epdr + i), &pdr);
3672           (*swap->swap_sym_in) (abfd, (PTR) &esym[pdr.isym], &sym);
3673           rp->adr = sym.value;
3674           rp->regmask = pdr.regmask;
3675           rp->regoffset = pdr.regoffset;
3676           rp->fregmask = pdr.fregmask;
3677           rp->fregoffset = pdr.fregoffset;
3678           rp->frameoffset = pdr.frameoffset;
3679           rp->framereg = pdr.framereg;
3680           rp->pcreg = pdr.pcreg;
3681           rp->irpss = sindex;
3682           sv[i] = ss + sym.iss;
3683           sindex += strlen (sv[i]) + 1;
3684         }
3685     }
3686
3687   size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
3688   size = BFD_ALIGN (size, 16);
3689   rtproc = (PTR) bfd_alloc (abfd, size);
3690   if (rtproc == NULL)
3691     {
3692       mips_elf_hash_table (info)->procedure_count = 0;
3693       goto error_return;
3694     }
3695
3696   mips_elf_hash_table (info)->procedure_count = count + 2;
3697
3698   erp = (struct rpdr_ext *) rtproc;
3699   memset (erp, 0, sizeof (struct rpdr_ext));
3700   erp++;
3701   str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
3702   strcpy (str, no_name_func);
3703   str += strlen (no_name_func) + 1;
3704   for (i = 0; i < count; i++)
3705     {
3706       ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
3707       strcpy (str, sv[i]);
3708       str += strlen (sv[i]) + 1;
3709     }
3710   ecoff_put_off (abfd, (bfd_vma) -1, (bfd_byte *) (erp + count)->p_adr);
3711
3712   /* Set the size and contents of .rtproc section.  */
3713   s->_raw_size = size;
3714   s->contents = rtproc;
3715
3716   /* Skip this section later on (I don't think this currently
3717      matters, but someday it might).  */
3718   s->link_order_head = (struct bfd_link_order *) NULL;
3719
3720   if (epdr != NULL)
3721     free (epdr);
3722   if (rpdr != NULL)
3723     free (rpdr);
3724   if (esym != NULL)
3725     free (esym);
3726   if (ss != NULL)
3727     free (ss);
3728   if (sv != NULL)
3729     free (sv);
3730
3731   return true;
3732
3733  error_return:
3734   if (epdr != NULL)
3735     free (epdr);
3736   if (rpdr != NULL)
3737     free (rpdr);
3738   if (esym != NULL)
3739     free (esym);
3740   if (ss != NULL)
3741     free (ss);
3742   if (sv != NULL)
3743     free (sv);
3744   return false;
3745 }
3746
3747 /* A comparison routine used to sort .gptab entries.  */
3748
3749 static int
3750 gptab_compare (p1, p2)
3751      const PTR p1;
3752      const PTR p2;
3753 {
3754   const Elf32_gptab *a1 = (const Elf32_gptab *) p1;
3755   const Elf32_gptab *a2 = (const Elf32_gptab *) p2;
3756
3757   return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3758 }
3759
3760 /* We need to use a special link routine to handle the .reginfo and
3761    the .mdebug sections.  We need to merge all instances of these
3762    sections together, not write them all out sequentially.  */
3763
3764 static boolean
3765 mips_elf_final_link (abfd, info)
3766      bfd *abfd;
3767      struct bfd_link_info *info;
3768 {
3769   asection **secpp;
3770   asection *o;
3771   struct bfd_link_order *p;
3772   asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
3773   asection *rtproc_sec;
3774   Elf32_RegInfo reginfo;
3775   struct ecoff_debug_info debug;
3776   const struct ecoff_debug_swap *swap
3777     = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
3778   HDRR *symhdr = &debug.symbolic_header;
3779   PTR mdebug_handle = NULL;
3780
3781   /* Drop the .options section, since it has special semantics which I
3782      haven't bothered to figure out.  */
3783   for (secpp = &abfd->sections; *secpp != NULL; secpp = &(*secpp)->next)
3784     {
3785       if (strcmp ((*secpp)->name, ".options") == 0)
3786         {
3787           for (p = (*secpp)->link_order_head; p != NULL; p = p->next)
3788             if (p->type == bfd_indirect_link_order)
3789               p->u.indirect.section->flags &=~ SEC_HAS_CONTENTS;
3790           (*secpp)->link_order_head = NULL;
3791           *secpp = (*secpp)->next;
3792           --abfd->section_count;
3793           break;
3794         }
3795     }
3796
3797   /* Get a value for the GP register.  */
3798   if (elf_gp (abfd) == 0)
3799     {
3800       struct bfd_link_hash_entry *h;
3801
3802       h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
3803       if (h != (struct bfd_link_hash_entry *) NULL
3804           && h->type == bfd_link_hash_defined)
3805         elf_gp (abfd) = (h->u.def.value
3806                          + h->u.def.section->output_section->vma
3807                          + h->u.def.section->output_offset);
3808       else if (info->relocateable)
3809         {
3810           bfd_vma lo;
3811
3812           /* Make up a value.  */
3813           lo = (bfd_vma) -1;
3814           for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3815             {
3816               if (o->vma < lo
3817                   && (strcmp (o->name, ".sbss") == 0
3818                       || strcmp (o->name, ".sdata") == 0
3819                       || strcmp (o->name, ".lit4") == 0
3820                       || strcmp (o->name, ".lit8") == 0))
3821                 lo = o->vma;
3822             }
3823           elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (abfd);
3824         }
3825       else
3826         {
3827           /* If the relocate_section function needs to do a reloc
3828              involving the GP value, it should make a reloc_dangerous
3829              callback to warn that GP is not defined.  */
3830         }
3831     }
3832
3833   /* Go through the sections and collect the .reginfo and .mdebug
3834      information.  */
3835   reginfo_sec = NULL;
3836   mdebug_sec = NULL;
3837   gptab_data_sec = NULL;
3838   gptab_bss_sec = NULL;
3839   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3840     {
3841       if (strcmp (o->name, ".reginfo") == 0)
3842         {
3843           memset (&reginfo, 0, sizeof reginfo);
3844
3845           /* We have found the .reginfo section in the output file.
3846              Look through all the link_orders comprising it and merge
3847              the information together.  */
3848           for (p = o->link_order_head;
3849                p != (struct bfd_link_order *) NULL;
3850                p = p->next)
3851             {
3852               asection *input_section;
3853               bfd *input_bfd;
3854               Elf32_External_RegInfo ext;
3855               Elf32_RegInfo sub;
3856
3857               if (p->type != bfd_indirect_link_order)
3858                 {
3859                   if (p->type == bfd_fill_link_order)
3860                     continue;
3861                   abort ();
3862                 }
3863
3864               input_section = p->u.indirect.section;
3865               input_bfd = input_section->owner;
3866
3867               /* The linker emulation code has probably clobbered the
3868                  size to be zero bytes.  */
3869               if (input_section->_raw_size == 0)
3870                 input_section->_raw_size = sizeof (Elf32_External_RegInfo);
3871
3872               if (! bfd_get_section_contents (input_bfd, input_section,
3873                                               (PTR) &ext,
3874                                               (file_ptr) 0,
3875                                               sizeof ext))
3876                 return false;
3877
3878               bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
3879
3880               reginfo.ri_gprmask |= sub.ri_gprmask;
3881               reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
3882               reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
3883               reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
3884               reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
3885
3886               /* ri_gp_value is set by the function
3887                  mips_elf32_section_processing when the section is
3888                  finally written out.  */
3889
3890               /* Hack: reset the SEC_HAS_CONTENTS flag so that
3891                  elf_link_input_bfd ignores this section.  */
3892               input_section->flags &=~ SEC_HAS_CONTENTS;
3893             }
3894
3895           /* Force the section size to the value we want.  */
3896           o->_raw_size = sizeof (Elf32_External_RegInfo);
3897
3898           /* Skip this section later on (I don't think this currently
3899              matters, but someday it might).  */
3900           o->link_order_head = (struct bfd_link_order *) NULL;
3901
3902           reginfo_sec = o;
3903         }
3904
3905       if (strcmp (o->name, ".mdebug") == 0)
3906         {
3907           struct extsym_info einfo;
3908
3909           /* We have found the .mdebug section in the output file.
3910              Look through all the link_orders comprising it and merge
3911              the information together.  */
3912           symhdr->magic = swap->sym_magic;
3913           /* FIXME: What should the version stamp be?  */
3914           symhdr->vstamp = 0;
3915           symhdr->ilineMax = 0;
3916           symhdr->cbLine = 0;
3917           symhdr->idnMax = 0;
3918           symhdr->ipdMax = 0;
3919           symhdr->isymMax = 0;
3920           symhdr->ioptMax = 0;
3921           symhdr->iauxMax = 0;
3922           symhdr->issMax = 0;
3923           symhdr->issExtMax = 0;
3924           symhdr->ifdMax = 0;
3925           symhdr->crfd = 0;
3926           symhdr->iextMax = 0;
3927
3928           /* We accumulate the debugging information itself in the
3929              debug_info structure.  */
3930           debug.line = NULL;
3931           debug.external_dnr = NULL;
3932           debug.external_pdr = NULL;
3933           debug.external_sym = NULL;
3934           debug.external_opt = NULL;
3935           debug.external_aux = NULL;
3936           debug.ss = NULL;
3937           debug.ssext = debug.ssext_end = NULL;
3938           debug.external_fdr = NULL;
3939           debug.external_rfd = NULL;
3940           debug.external_ext = debug.external_ext_end = NULL;
3941
3942           mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
3943           if (mdebug_handle == (PTR) NULL)
3944             return false;
3945
3946           if (SGI_COMPAT (abfd))
3947             {
3948               asection *s;
3949               EXTR esym;
3950               bfd_vma last;
3951               unsigned int i;
3952               static const char * const name[] =
3953                 { ".text", ".init", ".fini", ".data",
3954                     ".rodata", ".sdata", ".sbss", ".bss" };
3955               static const int sc[] = { scText, scInit, scFini, scData,
3956                                           scRData, scSData, scSBss, scBss };
3957
3958               esym.jmptbl = 0;
3959               esym.cobol_main = 0;
3960               esym.weakext = 0;
3961               esym.reserved = 0;
3962               esym.ifd = ifdNil;
3963               esym.asym.iss = issNil;
3964               esym.asym.st = stLocal;
3965               esym.asym.reserved = 0;
3966               esym.asym.index = indexNil;
3967               for (i = 0; i < 8; i++)
3968                 {
3969                   esym.asym.sc = sc[i];
3970                   s = bfd_get_section_by_name (abfd, name[i]);
3971                   if (s != NULL)
3972                     {
3973                       esym.asym.value = s->vma;
3974                       last = s->vma + s->_raw_size;
3975                     }
3976                   else
3977                     esym.asym.value = last;
3978
3979                   if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
3980                                                       name[i], &esym))
3981                     return false;
3982                 }
3983             }
3984
3985           for (p = o->link_order_head;
3986                p != (struct bfd_link_order *) NULL;
3987                p = p->next)
3988             {
3989               asection *input_section;
3990               bfd *input_bfd;
3991               const struct ecoff_debug_swap *input_swap;
3992               struct ecoff_debug_info input_debug;
3993               char *eraw_src;
3994               char *eraw_end;
3995
3996               if (p->type != bfd_indirect_link_order)
3997                 {
3998                   if (p->type == bfd_fill_link_order)
3999                     continue;
4000                   abort ();
4001                 }
4002
4003               input_section = p->u.indirect.section;
4004               input_bfd = input_section->owner;
4005
4006               if (bfd_get_flavour (input_bfd) != bfd_target_elf_flavour
4007                   || (get_elf_backend_data (input_bfd)
4008                       ->elf_backend_ecoff_debug_swap) == NULL)
4009                 {
4010                   /* I don't know what a non MIPS ELF bfd would be
4011                      doing with a .mdebug section, but I don't really
4012                      want to deal with it.  */
4013                   continue;
4014                 }
4015
4016               input_swap = (get_elf_backend_data (input_bfd)
4017                             ->elf_backend_ecoff_debug_swap);
4018
4019               BFD_ASSERT (p->size == input_section->_raw_size);
4020
4021               /* The ECOFF linking code expects that we have already
4022                  read in the debugging information and set up an
4023                  ecoff_debug_info structure, so we do that now.  */
4024               if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
4025                                                    &input_debug))
4026                 return false;
4027
4028               if (! (bfd_ecoff_debug_accumulate
4029                      (mdebug_handle, abfd, &debug, swap, input_bfd,
4030                       &input_debug, input_swap, info)))
4031                 return false;
4032
4033               /* Loop through the external symbols.  For each one with
4034                  interesting information, try to find the symbol in
4035                  the linker global hash table and save the information
4036                  for the output external symbols.  */
4037               eraw_src = input_debug.external_ext;
4038               eraw_end = (eraw_src
4039                           + (input_debug.symbolic_header.iextMax
4040                              * input_swap->external_ext_size));
4041               for (;
4042                    eraw_src < eraw_end;
4043                    eraw_src += input_swap->external_ext_size)
4044                 {
4045                   EXTR ext;
4046                   const char *name;
4047                   struct mips_elf_link_hash_entry *h;
4048
4049                   (*input_swap->swap_ext_in) (input_bfd, (PTR) eraw_src, &ext);
4050                   if (ext.asym.sc == scNil
4051                       || ext.asym.sc == scUndefined
4052                       || ext.asym.sc == scSUndefined)
4053                     continue;
4054
4055                   name = input_debug.ssext + ext.asym.iss;
4056                   h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
4057                                                  name, false, false, true);
4058                   if (h == NULL || h->esym.ifd != -2)
4059                     continue;
4060
4061                   if (ext.ifd != -1)
4062                     {
4063                       BFD_ASSERT (ext.ifd
4064                                   < input_debug.symbolic_header.ifdMax);
4065                       ext.ifd = input_debug.ifdmap[ext.ifd];
4066                     }
4067
4068                   h->esym = ext;
4069                 }
4070
4071               /* Free up the information we just read.  */
4072               free (input_debug.line);
4073               free (input_debug.external_dnr);
4074               free (input_debug.external_pdr);
4075               free (input_debug.external_sym);
4076               free (input_debug.external_opt);
4077               free (input_debug.external_aux);
4078               free (input_debug.ss);
4079               free (input_debug.ssext);
4080               free (input_debug.external_fdr);
4081               free (input_debug.external_rfd);
4082               free (input_debug.external_ext);
4083
4084               /* Hack: reset the SEC_HAS_CONTENTS flag so that
4085                  elf_link_input_bfd ignores this section.  */
4086               input_section->flags &=~ SEC_HAS_CONTENTS;
4087             }
4088
4089           if (SGI_COMPAT (abfd) && info->shared)
4090             {
4091               /* Create .rtproc section.  */
4092               rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4093               if (rtproc_sec == NULL)
4094                 {
4095                   flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
4096                                     | SEC_LINKER_CREATED | SEC_READONLY);
4097
4098                   rtproc_sec = bfd_make_section (abfd, ".rtproc");
4099                   if (rtproc_sec == NULL
4100                       || ! bfd_set_section_flags (abfd, rtproc_sec, flags)
4101                       || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
4102                     return false;
4103                 }
4104
4105               if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
4106                                                      info, rtproc_sec, &debug))
4107                 return false;
4108             }
4109
4110           /* Build the external symbol information.  */
4111           einfo.abfd = abfd;
4112           einfo.info = info;
4113           einfo.debug = &debug;
4114           einfo.swap = swap;
4115           einfo.failed = false;
4116           mips_elf_link_hash_traverse (mips_elf_hash_table (info),
4117                                        mips_elf_output_extsym,
4118                                        (PTR) &einfo);
4119           if (einfo.failed)
4120             return false;
4121
4122           /* Set the size of the .mdebug section.  */
4123           o->_raw_size = bfd_ecoff_debug_size (abfd, &debug, swap);
4124
4125           /* Skip this section later on (I don't think this currently
4126              matters, but someday it might).  */
4127           o->link_order_head = (struct bfd_link_order *) NULL;
4128
4129           mdebug_sec = o;
4130         }
4131
4132       if (strncmp (o->name, ".gptab.", sizeof ".gptab." - 1) == 0)
4133         {
4134           const char *subname;
4135           unsigned int c;
4136           Elf32_gptab *tab;
4137           Elf32_External_gptab *ext_tab;
4138           unsigned int i;
4139
4140           /* The .gptab.sdata and .gptab.sbss sections hold
4141              information describing how the small data area would
4142              change depending upon the -G switch.  These sections
4143              not used in executables files.  */
4144           if (! info->relocateable)
4145             {
4146               asection **secpp;
4147
4148               for (p = o->link_order_head;
4149                    p != (struct bfd_link_order *) NULL;
4150                    p = p->next)
4151                 {
4152                   asection *input_section;
4153
4154                   if (p->type != bfd_indirect_link_order)
4155                     {
4156                       if (p->type == bfd_fill_link_order)
4157                         continue;
4158                       abort ();
4159                     }
4160
4161                   input_section = p->u.indirect.section;
4162
4163                   /* Hack: reset the SEC_HAS_CONTENTS flag so that
4164                      elf_link_input_bfd ignores this section.  */
4165                   input_section->flags &=~ SEC_HAS_CONTENTS;
4166                 }
4167
4168               /* Skip this section later on (I don't think this
4169                  currently matters, but someday it might).  */
4170               o->link_order_head = (struct bfd_link_order *) NULL;
4171
4172               /* Really remove the section.  */
4173               for (secpp = &abfd->sections;
4174                    *secpp != o;
4175                    secpp = &(*secpp)->next)
4176                 ;
4177               *secpp = (*secpp)->next;
4178               --abfd->section_count;
4179
4180               continue;
4181             }
4182
4183           /* There is one gptab for initialized data, and one for
4184              uninitialized data.  */
4185           if (strcmp (o->name, ".gptab.sdata") == 0)
4186             gptab_data_sec = o;
4187           else if (strcmp (o->name, ".gptab.sbss") == 0)
4188             gptab_bss_sec = o;
4189           else
4190             {
4191               (*_bfd_error_handler)
4192                 ("%s: illegal section name `%s'",
4193                  bfd_get_filename (abfd), o->name);
4194               bfd_set_error (bfd_error_nonrepresentable_section);
4195               return false;
4196             }
4197
4198           /* The linker script always combines .gptab.data and
4199              .gptab.sdata into .gptab.sdata, and likewise for
4200              .gptab.bss and .gptab.sbss.  It is possible that there is
4201              no .sdata or .sbss section in the output file, in which
4202              case we must change the name of the output section.  */
4203           subname = o->name + sizeof ".gptab" - 1;
4204           if (bfd_get_section_by_name (abfd, subname) == NULL)
4205             {
4206               if (o == gptab_data_sec)
4207                 o->name = ".gptab.data";
4208               else
4209                 o->name = ".gptab.bss";
4210               subname = o->name + sizeof ".gptab" - 1;
4211               BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
4212             }
4213
4214           /* Set up the first entry.  */
4215           c = 1;
4216           tab = (Elf32_gptab *) bfd_malloc (c * sizeof (Elf32_gptab));
4217           if (tab == NULL)
4218             return false;
4219           tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
4220           tab[0].gt_header.gt_unused = 0;
4221
4222           /* Combine the input sections.  */
4223           for (p = o->link_order_head;
4224                p != (struct bfd_link_order *) NULL;
4225                p = p->next)
4226             {
4227               asection *input_section;
4228               bfd *input_bfd;
4229               bfd_size_type size;
4230               unsigned long last;
4231               bfd_size_type gpentry;
4232
4233               if (p->type != bfd_indirect_link_order)
4234                 {
4235                   if (p->type == bfd_fill_link_order)
4236                     continue;
4237                   abort ();
4238                 }
4239
4240               input_section = p->u.indirect.section;
4241               input_bfd = input_section->owner;
4242
4243               /* Combine the gptab entries for this input section one
4244                  by one.  We know that the input gptab entries are
4245                  sorted by ascending -G value.  */
4246               size = bfd_section_size (input_bfd, input_section);
4247               last = 0;
4248               for (gpentry = sizeof (Elf32_External_gptab);
4249                    gpentry < size;
4250                    gpentry += sizeof (Elf32_External_gptab))
4251                 {
4252                   Elf32_External_gptab ext_gptab;
4253                   Elf32_gptab int_gptab;
4254                   unsigned long val;
4255                   unsigned long add;
4256                   boolean exact;
4257                   unsigned int look;
4258
4259                   if (! (bfd_get_section_contents
4260                          (input_bfd, input_section, (PTR) &ext_gptab,
4261                           gpentry, sizeof (Elf32_External_gptab))))
4262                     {
4263                       free (tab);
4264                       return false;
4265                     }
4266
4267                   bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
4268                                                 &int_gptab);
4269                   val = int_gptab.gt_entry.gt_g_value;
4270                   add = int_gptab.gt_entry.gt_bytes - last;
4271
4272                   exact = false;
4273                   for (look = 1; look < c; look++)
4274                     {
4275                       if (tab[look].gt_entry.gt_g_value >= val)
4276                         tab[look].gt_entry.gt_bytes += add;
4277
4278                       if (tab[look].gt_entry.gt_g_value == val)
4279                         exact = true;
4280                     }
4281
4282                   if (! exact)
4283                     {
4284                       Elf32_gptab *new_tab;
4285                       unsigned int max;
4286
4287                       /* We need a new table entry.  */
4288                       new_tab = ((Elf32_gptab *)
4289                                  bfd_realloc ((PTR) tab,
4290                                               (c + 1) * sizeof (Elf32_gptab)));
4291                       if (new_tab == NULL)
4292                         {
4293                           free (tab);
4294                           return false;
4295                         }
4296                       tab = new_tab;
4297                       tab[c].gt_entry.gt_g_value = val;
4298                       tab[c].gt_entry.gt_bytes = add;
4299
4300                       /* Merge in the size for the next smallest -G
4301                          value, since that will be implied by this new
4302                          value.  */
4303                       max = 0;
4304                       for (look = 1; look < c; look++)
4305                         {
4306                           if (tab[look].gt_entry.gt_g_value < val
4307                               && (max == 0
4308                                   || (tab[look].gt_entry.gt_g_value
4309                                       > tab[max].gt_entry.gt_g_value)))
4310                             max = look;
4311                         }
4312                       if (max != 0)
4313                         tab[c].gt_entry.gt_bytes +=
4314                           tab[max].gt_entry.gt_bytes;
4315
4316                       ++c;
4317                     }
4318
4319                   last = int_gptab.gt_entry.gt_bytes;
4320                 }
4321
4322               /* Hack: reset the SEC_HAS_CONTENTS flag so that
4323                  elf_link_input_bfd ignores this section.  */
4324               input_section->flags &=~ SEC_HAS_CONTENTS;
4325             }
4326
4327           /* The table must be sorted by -G value.  */
4328           if (c > 2)
4329             qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
4330
4331           /* Swap out the table.  */
4332           ext_tab = ((Elf32_External_gptab *)
4333                      bfd_alloc (abfd, c * sizeof (Elf32_External_gptab)));
4334           if (ext_tab == NULL)
4335             {
4336               free (tab);
4337               return false;
4338             }
4339
4340           for (i = 0; i < c; i++)
4341             bfd_mips_elf32_swap_gptab_out (abfd, tab + i, ext_tab + i);
4342           free (tab);
4343
4344           o->_raw_size = c * sizeof (Elf32_External_gptab);
4345           o->contents = (bfd_byte *) ext_tab;
4346
4347           /* Skip this section later on (I don't think this currently
4348              matters, but someday it might).  */
4349           o->link_order_head = (struct bfd_link_order *) NULL;
4350         }
4351     }
4352
4353   /* Invoke the regular ELF backend linker to do all the work.  */
4354   if (! bfd_elf32_bfd_final_link (abfd, info))
4355     return false;
4356
4357   /* Now write out the computed sections.  */
4358
4359   if (reginfo_sec != (asection *) NULL)
4360     {
4361       Elf32_External_RegInfo ext;
4362
4363       bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
4364       if (! bfd_set_section_contents (abfd, reginfo_sec, (PTR) &ext,
4365                                       (file_ptr) 0, sizeof ext))
4366         return false;
4367     }
4368
4369   if (mdebug_sec != (asection *) NULL)
4370     {
4371       BFD_ASSERT (abfd->output_has_begun);
4372       if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
4373                                                swap, info,
4374                                                mdebug_sec->filepos))
4375         return false;
4376
4377       bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
4378     }
4379
4380   if (gptab_data_sec != (asection *) NULL)
4381     {
4382       if (! bfd_set_section_contents (abfd, gptab_data_sec,
4383                                       gptab_data_sec->contents,
4384                                       (file_ptr) 0,
4385                                       gptab_data_sec->_raw_size))
4386         return false;
4387     }
4388
4389   if (gptab_bss_sec != (asection *) NULL)
4390     {
4391       if (! bfd_set_section_contents (abfd, gptab_bss_sec,
4392                                       gptab_bss_sec->contents,
4393                                       (file_ptr) 0,
4394                                       gptab_bss_sec->_raw_size))
4395         return false;
4396     }
4397
4398   if (SGI_COMPAT (abfd))
4399     {
4400       rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
4401       if (rtproc_sec != NULL)
4402         {
4403           if (! bfd_set_section_contents (abfd, rtproc_sec,
4404                                           rtproc_sec->contents,
4405                                           (file_ptr) 0,
4406                                           rtproc_sec->_raw_size))
4407             return false;
4408         }
4409     }
4410
4411   return true;
4412 }
4413
4414 /* Handle a MIPS ELF HI16 reloc.  */
4415
4416 static void
4417 mips_elf_relocate_hi16 (input_bfd, relhi, rello, contents, addend)
4418      bfd *input_bfd;
4419      Elf_Internal_Rela *relhi;
4420      Elf_Internal_Rela *rello;
4421      bfd_byte *contents;
4422      bfd_vma addend;
4423 {
4424   bfd_vma insn;
4425   bfd_vma addlo;
4426
4427   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4428
4429   addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4430   addlo &= 0xffff;
4431
4432   addend += ((insn & 0xffff) << 16) + addlo;
4433
4434   if ((addlo & 0x8000) != 0)
4435     addend -= 0x10000;
4436   if ((addend & 0x8000) != 0)
4437     addend += 0x10000;
4438
4439   bfd_put_32 (input_bfd,
4440               (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
4441               contents + relhi->r_offset);
4442 }
4443
4444 /* Handle a MIPS ELF local GOT16 reloc.  */
4445
4446 static boolean
4447 mips_elf_relocate_got_local (output_bfd, input_bfd, sgot, relhi, rello,
4448                              contents, addend)
4449      bfd *output_bfd;
4450      bfd *input_bfd;
4451      asection *sgot;
4452      Elf_Internal_Rela *relhi;
4453      Elf_Internal_Rela *rello;
4454      bfd_byte *contents;
4455      bfd_vma addend;
4456 {
4457   unsigned int assigned_gotno;
4458   unsigned int i;
4459   bfd_vma insn;
4460   bfd_vma addlo;
4461   bfd_vma address;
4462   bfd_vma hipage;
4463   bfd_byte *got_contents;
4464   struct mips_got_info *g;
4465
4466   insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
4467
4468   addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
4469   addlo &= 0xffff;
4470
4471   addend += ((insn & 0xffff) << 16) + addlo;
4472
4473   if ((addlo & 0x8000) != 0)
4474     addend -= 0x10000;
4475   if ((addend & 0x8000) != 0)
4476     addend += 0x10000;
4477
4478   /* Get a got entry representing requested hipage.  */
4479   BFD_ASSERT (elf_section_data (sgot) != NULL);
4480   g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4481   BFD_ASSERT (g != NULL);
4482
4483   assigned_gotno = g->assigned_gotno;
4484   got_contents = sgot->contents;
4485   hipage = addend & 0xffff0000;
4486
4487   for (i = MIPS_RESERVED_GOTNO; i < assigned_gotno; i++)
4488     {
4489       address = bfd_get_32 (input_bfd, got_contents + i * 4);
4490       if (hipage == (address & 0xffff0000))
4491         break;
4492     }
4493
4494   if (i == assigned_gotno)
4495     {
4496       if (assigned_gotno >= g->local_gotno)
4497         {
4498           (*_bfd_error_handler)
4499             ("more got entries are needed for hipage relocations");
4500           bfd_set_error (bfd_error_bad_value);
4501           return false;
4502         }
4503
4504       bfd_put_32 (input_bfd, hipage, got_contents + assigned_gotno * 4);
4505       ++g->assigned_gotno;
4506     }
4507
4508   i = - ELF_MIPS_GP_OFFSET (output_bfd) + i * 4;
4509   bfd_put_32 (input_bfd, (insn & 0xffff0000) | (i & 0xffff),
4510               contents + relhi->r_offset);
4511
4512   return true;
4513 }
4514
4515 /* Handle MIPS ELF CALL16 reloc and global GOT16 reloc.  */
4516
4517 static void
4518 mips_elf_relocate_global_got (input_bfd, rel, contents, offset)
4519      bfd *input_bfd;
4520      Elf_Internal_Rela *rel;
4521      bfd_byte *contents;
4522      bfd_vma offset;
4523 {
4524   bfd_vma insn;
4525
4526   insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
4527   bfd_put_32 (input_bfd,
4528               (insn & 0xffff0000) | (offset & 0xffff),
4529               contents + rel->r_offset);
4530 }
4531
4532 /* Relocate a MIPS ELF section.  */
4533
4534 static boolean
4535 mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4536                            contents, relocs, local_syms, local_sections)
4537      bfd *output_bfd;
4538      struct bfd_link_info *info;
4539      bfd *input_bfd;
4540      asection *input_section;
4541      bfd_byte *contents;
4542      Elf_Internal_Rela *relocs;
4543      Elf_Internal_Sym *local_syms;
4544      asection **local_sections;
4545 {
4546   Elf_Internal_Shdr *symtab_hdr;
4547   size_t locsymcount;
4548   size_t extsymoff;
4549   asection *sgot, *sreloc, *scpt;
4550   bfd *dynobj;
4551   bfd_vma gp;
4552   Elf_Internal_Rela *rel;
4553   Elf_Internal_Rela *relend;
4554   struct mips_got_info *g;
4555
4556   dynobj = elf_hash_table (info)->dynobj;
4557   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4558
4559   sgot = NULL;
4560   sreloc = NULL;
4561   if (dynobj == NULL || ! SGI_COMPAT (output_bfd))
4562     scpt = NULL;
4563   else
4564     scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
4565   g = NULL;
4566
4567   if (elf_bad_symtab (input_bfd))
4568     {
4569       locsymcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
4570       extsymoff = 0;
4571     }
4572   else
4573     {
4574       locsymcount = symtab_hdr->sh_info;
4575       extsymoff = symtab_hdr->sh_info;
4576     }
4577
4578   gp = _bfd_get_gp_value (output_bfd);
4579
4580   rel = relocs;
4581   relend = relocs + input_section->reloc_count;
4582   for (; rel < relend; rel++)
4583     {
4584       int r_type;
4585       reloc_howto_type *howto;
4586       unsigned long r_symndx;
4587       bfd_vma addend;
4588       struct elf_link_hash_entry *h;
4589       asection *sec;
4590       Elf_Internal_Sym *sym;
4591       struct mips_elf_link_hash_entry *mh;
4592       int other;
4593       bfd_reloc_status_type r;
4594
4595       r_type = ELF32_R_TYPE (rel->r_info);
4596       if ((r_type < 0 || r_type >= (int) R_MIPS_max)
4597           && r_type != R_MIPS16_26
4598           && r_type != R_MIPS16_GPREL)
4599         {
4600           bfd_set_error (bfd_error_bad_value);
4601           return false;
4602         }
4603       if (r_type == R_MIPS16_26)
4604         howto = &elf_mips16_jump_howto;
4605       else if (r_type == R_MIPS16_GPREL)
4606         howto = &elf_mips16_gprel_howto;
4607       else
4608         howto = elf_mips_howto_table + r_type;
4609
4610       if (dynobj != NULL
4611           && (r_type == R_MIPS_CALL16
4612               || r_type == R_MIPS_GOT16
4613               || r_type == R_MIPS_CALL_HI16
4614               || r_type == R_MIPS_CALL_LO16
4615               || r_type == R_MIPS_GOT_HI16
4616               || r_type == R_MIPS_GOT_LO16))
4617         {
4618           /* We need the .got section.  */
4619           if (sgot == NULL)
4620             {
4621               sgot = bfd_get_section_by_name (dynobj, ".got");
4622               BFD_ASSERT (sgot != NULL);
4623               BFD_ASSERT (elf_section_data (sgot) != NULL);
4624               g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
4625               BFD_ASSERT (g != NULL);
4626             }
4627         }
4628
4629       r_symndx = ELF32_R_SYM (rel->r_info);
4630
4631       /* Mix in the change in GP address for a GP relative reloc.  */
4632       if (r_type != R_MIPS_GPREL16
4633           && r_type != R_MIPS_LITERAL
4634           && r_type != R_MIPS_GPREL32
4635           && r_type != R_MIPS16_GPREL)
4636         addend = 0;
4637       else
4638         {
4639           if (gp == 0)
4640             {
4641               if (! ((*info->callbacks->reloc_dangerous)
4642                      (info,
4643                       "GP relative relocation when GP not defined",
4644                       input_bfd, input_section,
4645                       rel->r_offset)))
4646                 return false;
4647               /* Only give the error once per link.  */
4648               gp = 4;
4649               _bfd_set_gp_value (output_bfd, gp);
4650             }
4651
4652           if (r_symndx < extsymoff
4653               || (elf_bad_symtab (input_bfd)
4654                   && local_sections[r_symndx] != NULL))
4655             {
4656               /* This is a relocation against a section.  The current
4657                  addend in the instruction is the difference between
4658                  INPUT_SECTION->vma and the GP value of INPUT_BFD.  We
4659                  must change this to be the difference between the
4660                  final definition (which will end up in RELOCATION)
4661                  and the GP value of OUTPUT_BFD (which is in GP).  */
4662               addend = elf_gp (input_bfd) - gp;
4663             }
4664           else if (! info->relocateable)
4665             {
4666               /* We are doing a final link.  The current addend in the
4667                  instruction is simply the desired offset into the
4668                  symbol (normally zero).  We want the instruction to
4669                  hold the difference between the final definition of
4670                  the symbol (which will end up in RELOCATION) and the
4671                  GP value of OUTPUT_BFD (which is in GP).  */
4672               addend = - gp;
4673             }
4674           else
4675             {
4676               /* We are generating relocateable output, and we aren't
4677                  going to define this symbol, so we just leave the
4678                  instruction alone.  */
4679               addend = 0;
4680             }
4681         }
4682
4683       h = NULL;
4684       sym = NULL;
4685       sec = NULL;
4686       if (info->relocateable)
4687         {
4688           /* This is a relocateable link.  We don't have to change
4689              anything, unless the reloc is against a section symbol,
4690              in which case we have to adjust according to where the
4691              section symbol winds up in the output section.  */
4692           if (r_symndx >= locsymcount
4693               || (elf_bad_symtab (input_bfd)
4694                   && local_sections[r_symndx] == NULL))
4695             r = bfd_reloc_ok;
4696           else
4697             {
4698               sym = local_syms + r_symndx;
4699               if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4700                 r = bfd_reloc_ok;
4701               else
4702                 {
4703                   sec = local_sections[r_symndx];
4704
4705                   /* It would be logical to add sym->st_value here,
4706                      but Irix 5 sometimes generates a garbage symbol
4707                      value.  */
4708                   addend += sec->output_offset;
4709
4710                   /* If this is HI16 or GOT16 with an associated LO16,
4711                      adjust the addend accordingly.  Otherwise, just
4712                      relocate.  */
4713                   if (r_type == R_MIPS_64 && bfd_big_endian (input_bfd))
4714                     r = _bfd_relocate_contents (howto, input_bfd,
4715                                                 addend,
4716                                                 contents + rel->r_offset + 4);
4717                   else if (r_type != R_MIPS_HI16 && r_type != R_MIPS_GOT16)
4718                     r = _bfd_relocate_contents (howto, input_bfd,
4719                                                 addend,
4720                                                 contents + rel->r_offset);
4721                   else
4722                     {
4723                       Elf_Internal_Rela *lorel;
4724
4725                       /* As a GNU extension, permit an arbitrary
4726                          number of R_MIPS_HI16 relocs before the
4727                          R_MIPS_LO16 reloc.  This permits gcc to emit
4728                          the HI and LO relocs itself.  */
4729                       if (r_type == R_MIPS_GOT16)
4730                         lorel = rel + 1;
4731                       else
4732                         {
4733                           for (lorel = rel + 1;
4734                                (lorel < relend
4735                                 && (ELF32_R_TYPE (lorel->r_info)
4736                                     == R_MIPS_HI16));
4737                                lorel++)
4738                             ;
4739                         }
4740                       if (lorel < relend
4741                           && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4742                         {
4743                           mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4744                                                   contents, addend);
4745                           r = bfd_reloc_ok;
4746                         }
4747                       else
4748                         r = _bfd_relocate_contents (howto, input_bfd,
4749                                                     addend,
4750                                                     contents + rel->r_offset);
4751                     }
4752                 }
4753             }
4754         }
4755       else
4756         {
4757           bfd_vma relocation;
4758           boolean local;
4759
4760           /* This is a final link.  */
4761           sym = NULL;
4762           if (r_symndx < extsymoff
4763               || (elf_bad_symtab (input_bfd)
4764                   && local_sections[r_symndx] != NULL))
4765             {
4766               local = true;
4767               sym = local_syms + r_symndx;
4768               sec = local_sections[r_symndx];
4769               relocation = (sec->output_section->vma
4770                             + sec->output_offset);
4771
4772               /* It would be logical to always add sym->st_value here,
4773                  but Irix 5 sometimes generates a garbage symbol
4774                  value.  */
4775               if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4776                 relocation += sym->st_value;
4777
4778               /* mips16 text labels should be treated as odd.  */
4779               if (sym->st_other == STO_MIPS16)
4780                 ++relocation;
4781             }
4782           else
4783             {
4784               long indx;
4785
4786               local = false;
4787               indx = r_symndx - extsymoff;
4788               h = elf_sym_hashes (input_bfd)[indx];
4789               while (h->root.type == bfd_link_hash_indirect
4790                      || h->root.type == bfd_link_hash_warning)
4791                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4792               if (strcmp (h->root.root.string, "_gp_disp") == 0)
4793                 {
4794                   if (gp == 0)
4795                     {
4796                       if (! ((*info->callbacks->reloc_dangerous)
4797                              (info,
4798                               "_gp_disp used when GP not defined",
4799                               input_bfd, input_section,
4800                               rel->r_offset)))
4801                         return false;
4802                       /* Only give the error once per link.  */
4803                       gp = 4;
4804                       _bfd_set_gp_value (output_bfd, gp);
4805                       relocation = 0;
4806                     }
4807                   else
4808                     {
4809                       sec = input_section;
4810                       if (sec->output_section != NULL)
4811                         relocation = (gp
4812                                       - (rel->r_offset
4813                                          + sec->output_section->vma
4814                                          + sec->output_offset));
4815                       else
4816                         relocation = gp - rel->r_offset;
4817                       if (r_type == R_MIPS_LO16)
4818                         relocation += 4;
4819                     }
4820                 }
4821               else if (h->root.type == bfd_link_hash_defined
4822                   || h->root.type == bfd_link_hash_defweak)
4823                 {
4824                   sec = h->root.u.def.section;
4825                   if (sec->output_section == NULL)
4826                     relocation = 0;
4827                   else
4828                     relocation = (h->root.u.def.value
4829                                   + sec->output_section->vma
4830                                   + sec->output_offset);
4831                 }
4832               else if (h->root.type == bfd_link_hash_undefweak)
4833                 relocation = 0;
4834               else if (info->shared && ! info->symbolic)
4835                 relocation = 0;
4836               else if (strcmp (h->root.root.string, "_DYNAMIC_LINK") == 0)
4837                 {
4838                   /* If this is a dynamic link, we should have created
4839                      a _DYNAMIC_LINK symbol in
4840                      mips_elf_create_dynamic_sections.  Otherwise, we
4841                      should define the symbol with a value of 0.
4842                      FIXME: It should probably get into the symbol
4843                      table somehow as well.  */
4844                   BFD_ASSERT (! info->shared);
4845                   BFD_ASSERT (bfd_get_section_by_name (output_bfd,
4846                                                        ".dynamic") == NULL);
4847                   relocation = 0;
4848                 }
4849               else
4850                 {
4851                   if (! ((*info->callbacks->undefined_symbol)
4852                          (info, h->root.root.string, input_bfd,
4853                           input_section, rel->r_offset)))
4854                     return false;
4855                   relocation = 0;
4856                 }
4857             }
4858
4859           mh = (struct mips_elf_link_hash_entry *) h;
4860           if (h != NULL)
4861             other = h->other;
4862           else if (sym != NULL)
4863             other = sym->st_other;
4864           else
4865             other = 0;
4866
4867           /* If this function has an fn_stub, then it is a mips16
4868              function which needs a stub if it is called by a 32 bit
4869              function.  If this reloc is anything other than a 16 bit
4870              call, redirect the reloc to the stub.  We don't redirect
4871              relocs from other stub functions.  */
4872           if (r_type != R_MIPS16_26
4873               && ((mh != NULL
4874                    && mh->fn_stub != NULL)
4875                   || (mh == NULL
4876                       && elf_tdata (input_bfd)->local_stubs != NULL
4877                       && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
4878               && strncmp (bfd_get_section_name (input_bfd, input_section),
4879                           FN_STUB, sizeof FN_STUB - 1) != 0
4880               && strncmp (bfd_get_section_name (input_bfd, input_section),
4881                           CALL_STUB, sizeof CALL_STUB - 1) != 0
4882               && strncmp (bfd_get_section_name (input_bfd, input_section),
4883                           CALL_FP_STUB, sizeof CALL_FP_STUB - 1) != 0)
4884             {
4885               if (mh != NULL)
4886                 {
4887                   BFD_ASSERT (mh->need_fn_stub);
4888                   relocation = (mh->fn_stub->output_section->vma
4889                                 + mh->fn_stub->output_offset);
4890                 }
4891               else
4892                 {
4893                   asection *fn_stub;
4894
4895                   fn_stub = elf_tdata (input_bfd)->local_stubs[r_symndx];
4896                   relocation = (fn_stub->output_section->vma
4897                                 + fn_stub->output_offset);
4898                 }
4899
4900               /* RELOCATION now points to 32 bit code.  */
4901               other = 0;
4902             }
4903
4904           /* If this function has a call_stub, then it is called by a
4905              mips16 function; the call needs to go through a stub if
4906              this function is a 32 bit function.  If this reloc is a
4907              16 bit call, and the symbol is not a 16 bit function,
4908              then redirect the reloc to the stub.  Note that we don't
4909              need to worry about calling the function through a
4910              function pointer; such calls are handled by routing
4911              through a special mips16 routine.  We don't have to check
4912              whether this call is from a stub; it can't be, because a
4913              stub contains 32 bit code, and hence can not have a 16
4914              bit reloc.  */
4915           if (r_type == R_MIPS16_26
4916               && mh != NULL
4917               && (mh->call_stub != NULL || mh->call_fp_stub != NULL)
4918               && other != STO_MIPS16)
4919             {
4920               asection *stub;
4921
4922               /* If both call_stub and call_fp_stub are defined, we
4923                  can figure out which one to use by seeing which one
4924                  appears in the input file.  */
4925               if (mh->call_stub != NULL && mh->call_fp_stub != NULL)
4926                 {
4927                   asection *o;
4928
4929                   stub = NULL;
4930                   for (o = input_bfd->sections; o != NULL; o = o->next)
4931                     {
4932                       if (strncmp (bfd_get_section_name (input_bfd, o),
4933                                    CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
4934                         {
4935                           stub = mh->call_fp_stub;
4936                           break;
4937                         }
4938                     }
4939                   if (stub == NULL)
4940                     stub = mh->call_stub;
4941                 }
4942               else if (mh->call_stub != NULL)
4943                 stub = mh->call_stub;
4944               else
4945                 stub = mh->call_fp_stub;
4946
4947               BFD_ASSERT (stub->_raw_size > 0);
4948               relocation = stub->output_section->vma + stub->output_offset;
4949             }
4950
4951           if (r_type == R_MIPS_HI16)
4952             {
4953               Elf_Internal_Rela *lorel;
4954
4955               /* As a GNU extension, permit an arbitrary number of
4956                  R_MIPS_HI16 relocs before the R_MIPS_LO16 reloc.
4957                  This permits gcc to emit the HI and LO relocs itself.  */
4958               for (lorel = rel + 1;
4959                    (lorel < relend
4960                     && ELF32_R_TYPE (lorel->r_info) == R_MIPS_HI16);
4961                    lorel++)
4962                 ;
4963               if (lorel < relend
4964                   && ELF32_R_TYPE (lorel->r_info) == R_MIPS_LO16)
4965                 {
4966                   mips_elf_relocate_hi16 (input_bfd, rel, lorel,
4967                                           contents, relocation + addend);
4968                   r = bfd_reloc_ok;
4969                 }
4970               else
4971                 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4972                                               contents, rel->r_offset,
4973                                               relocation, addend);
4974             }
4975           else if (r_type == R_MIPS_GOT16 && local)
4976             {
4977               /* GOT16 must also have an associated LO16 in the local
4978                  case.  In this case, the addend is extracted and the
4979                  section in which the referenced object is determined.
4980                  Then the final address of the object is computed and
4981                  the GOT entry for the hipage (an aligned 64kb chunk)
4982                  is added to .got section if needed.  The offset field
4983                  of the GOT16-relocated instruction is replaced by the
4984                  index of this GOT entry for the hipage.  */
4985               if ((rel + 1) < relend
4986                   && ELF32_R_TYPE ((rel + 1)->r_info) == R_MIPS_LO16)
4987                 {
4988                   if (! mips_elf_relocate_got_local (output_bfd, input_bfd,
4989                                                      sgot, rel, rel + 1,
4990                                                      contents,
4991                                                      relocation + addend))
4992                     return false;
4993                   r = bfd_reloc_ok;
4994                 }
4995               else
4996                 r = bfd_reloc_outofrange;
4997             }
4998           else if (r_type == R_MIPS_CALL16
4999                    || r_type == R_MIPS_GOT16
5000                    || r_type == R_MIPS_CALL_LO16
5001                    || r_type == R_MIPS_GOT_LO16)
5002             {
5003               bfd_vma offset;
5004
5005               /* This symbol must be registered as a global symbol
5006                  having the corresponding got entry.  */
5007               BFD_ASSERT (h->got_offset != (bfd_vma) -1);
5008
5009               offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5010               BFD_ASSERT (g->local_gotno <= offset
5011                           && offset < sgot->_raw_size);
5012               bfd_put_32 (output_bfd, relocation + addend,
5013                           sgot->contents + offset);
5014               offset = (sgot->output_section->vma + sgot->output_offset
5015                         + offset - gp);
5016               mips_elf_relocate_global_got (input_bfd, rel, contents,
5017                                             offset);
5018               r = bfd_reloc_ok;
5019             }
5020           else if (r_type == R_MIPS_CALL_HI16
5021                    || r_type == R_MIPS_GOT_HI16)
5022             {
5023               bfd_vma offset;
5024
5025               /* This must be a global symbol with a got entry.  The
5026                  next reloc must be the corresponding LO16 reloc.  */
5027               BFD_ASSERT (h != NULL && h->got_offset != (bfd_vma) -1);
5028               BFD_ASSERT ((rel + 1) < relend);
5029               BFD_ASSERT (ELF32_R_TYPE ((rel + 1)->r_info)
5030                           == (r_type == R_MIPS_CALL_HI16
5031                               ? R_MIPS_CALL_LO16
5032                               : R_MIPS_GOT_LO16));
5033
5034               offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
5035               BFD_ASSERT (g->local_gotno <= offset
5036                           && offset < sgot->_raw_size);
5037               bfd_put_32 (output_bfd, relocation + addend,
5038                           sgot->contents + offset);
5039               offset = (sgot->output_section->vma + sgot->output_offset
5040                         + offset - gp);
5041               mips_elf_relocate_hi16 (input_bfd, rel, rel + 1, contents,
5042                                       offset);
5043               r = bfd_reloc_ok;
5044             }
5045           else if (r_type == R_MIPS_REL32
5046                    || r_type == R_MIPS_32)
5047             {
5048               Elf_Internal_Rel outrel;
5049               Elf32_crinfo cptrel;
5050               bfd_byte *cr;
5051
5052               if ((info->shared
5053                    || (elf_hash_table (info)->dynamic_sections_created
5054                        && h != NULL
5055                        && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
5056                            == 0)))
5057                   && (input_section->flags & SEC_ALLOC) != 0)
5058                 {
5059                   boolean skip;
5060
5061                   /* When generating a shared object, these
5062                      relocations are copied into the output file to be
5063                      resolved at run time.  */
5064                   if (sreloc == NULL)
5065                     {
5066                       sreloc = bfd_get_section_by_name (dynobj, ".rel.dyn");
5067                       BFD_ASSERT (sreloc != NULL);
5068                     }
5069
5070                   skip = false;
5071
5072                   if (elf_section_data (input_section)->stab_info == NULL)
5073                     outrel.r_offset = rel->r_offset;
5074                   else
5075                     {
5076                       bfd_vma off;
5077
5078                       off = (_bfd_stab_section_offset
5079                              (output_bfd, &elf_hash_table (info)->stab_info,
5080                               input_section,
5081                               &elf_section_data (input_section)->stab_info,
5082                               rel->r_offset));
5083                       if (off == (bfd_vma) -1)
5084                         skip = true;
5085                       outrel.r_offset = off;
5086                     }
5087
5088                   outrel.r_offset += (input_section->output_section->vma
5089                                       + input_section->output_offset);
5090
5091                   addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
5092
5093                   if (skip)
5094                     memset (&outrel, 0, sizeof outrel);
5095                   else if (h != NULL
5096                            && (! info->symbolic
5097                                || (h->elf_link_hash_flags
5098                                    & ELF_LINK_HASH_DEF_REGULAR) == 0))
5099                     {
5100                       BFD_ASSERT (h->dynindx != -1);
5101                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_REL32);
5102                       sec = input_section;
5103                     }
5104                   else
5105                     {
5106                       long indx;
5107
5108                       if (h == NULL)
5109                         sec = local_sections[r_symndx];
5110                       else
5111                         {
5112                           BFD_ASSERT (h->root.type == bfd_link_hash_defined
5113                                       || (h->root.type
5114                                           == bfd_link_hash_defweak));
5115                           sec = h->root.u.def.section;
5116                         }
5117                       if (sec != NULL && bfd_is_abs_section (sec))
5118                         indx = 0;
5119                       else if (sec == NULL || sec->owner == NULL)
5120                         {
5121                           bfd_set_error (bfd_error_bad_value);
5122                           return false;
5123                         }
5124                       else
5125                         {
5126                           asection *osec;
5127
5128                           osec = sec->output_section;
5129                           indx = elf_section_data (osec)->dynindx;
5130                           if (indx == 0)
5131                             abort ();
5132                         }
5133
5134                       outrel.r_info = ELF32_R_INFO (indx, R_MIPS_REL32);
5135                       addend += relocation;
5136                     }
5137
5138                   if (! skip)
5139                     bfd_put_32 (output_bfd, addend, contents + rel->r_offset);
5140
5141                   bfd_elf32_swap_reloc_out (output_bfd, &outrel,
5142                                              (((Elf32_External_Rel *)
5143                                                sreloc->contents)
5144                                               + sreloc->reloc_count));
5145                   ++sreloc->reloc_count;
5146
5147                   if (! skip && SGI_COMPAT (output_bfd))
5148                     {
5149                       if (scpt == NULL)
5150                         continue;
5151
5152                       /* Make an entry of compact relocation info.  */
5153                       mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5154                       cptrel.vaddr = (rel->r_offset
5155                                       + input_section->output_section->vma
5156                                       + input_section->output_offset);
5157                       if (r_type == R_MIPS_REL32)
5158                         mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5159                       else
5160                         mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5161                       mips_elf_set_cr_dist2to (cptrel, 0);
5162                       cptrel.konst = addend;
5163
5164                       cr = (scpt->contents
5165                             + sizeof (Elf32_External_compact_rel));
5166                       bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5167                                                  ((Elf32_External_crinfo *) cr
5168                                                   + scpt->reloc_count));
5169                       ++scpt->reloc_count;
5170                     }
5171
5172                   /* This reloc will be computed at runtime, so
5173                      there's no need to do anything now.  */
5174                   continue;
5175                 }
5176               else
5177                 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5178                                               contents, rel->r_offset,
5179                                               relocation, addend);
5180             }
5181           else if (r_type == R_MIPS_64)
5182             {
5183               bfd_size_type addr;
5184               unsigned long val;
5185
5186               /* Do a 32 bit relocation, and sign extend to 64 bits.  */
5187               addr = rel->r_offset;
5188               if (bfd_big_endian (input_bfd))
5189                 addr += 4;
5190               r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5191                                             contents, addr, relocation,
5192                                             addend);
5193               val = bfd_get_32 (input_bfd, contents + addr);
5194               if ((val & 0x80000000) != 0)
5195                 val = 0xffffffff;
5196               else
5197                 val = 0;
5198               addr = rel->r_offset;
5199               if (bfd_little_endian (input_bfd))
5200                 addr += 4;
5201               bfd_put_32 (input_bfd, val, contents + addr);
5202             }
5203           else if (r_type == R_MIPS_26 && other == STO_MIPS16)
5204             {
5205               unsigned long insn;
5206
5207               /* This is a jump to a mips16 routine from a mips32
5208                  routine.  We need to change jal into jalx.  */
5209               insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5210               if (((insn >> 26) & 0x3f) != 0x3
5211                   && ((insn >> 26) & 0x3f) != 0x1d)
5212                 {
5213                   (*_bfd_error_handler)
5214                     ("%s: %s+0x%lx: jump to mips16 routine which is not jal",
5215                      bfd_get_filename (input_bfd),
5216                      input_section->name,
5217                      (unsigned long) rel->r_offset);
5218                   bfd_set_error (bfd_error_bad_value);
5219                   return false;
5220                 }
5221               insn = (insn & 0x3ffffff) | (0x1d << 26);
5222               bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5223               r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5224                                             contents, rel->r_offset,
5225                                             relocation, addend);
5226             }
5227           else if (r_type == R_MIPS16_26)
5228             {
5229               /* It's easiest to do the normal relocation, and then
5230                  dig out the instruction and swap the first word the
5231                  way the mips16 expects it.  If this is little endian,
5232                  though, we need to swap the two words first, and then
5233                  swap them back again later, so that the address looks
5234                  right.  */
5235
5236               if (bfd_little_endian (input_bfd))
5237                 {
5238                   unsigned long insn;
5239
5240                   insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5241                   insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5242                   bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5243                 }
5244
5245               r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5246                                             contents, rel->r_offset,
5247                                             relocation, addend);
5248               if (r == bfd_reloc_ok)
5249                 {
5250                   unsigned long insn;
5251
5252                   if (bfd_little_endian (input_bfd))
5253                     {
5254                       insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
5255                       insn = ((insn >> 16) & 0xffff) | ((insn & 0xffff) << 16);
5256                       bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
5257                     }
5258
5259                   insn = bfd_get_16 (input_bfd, contents + rel->r_offset);
5260                   insn = ((insn & 0xfc00)
5261                           | ((insn & 0x1f) << 5)
5262                           | ((insn & 0x3e0) >> 5));
5263                   /* If this is a jump to a 32 bit routine, then make
5264                      it jalx.  */
5265                   if (other != STO_MIPS16)
5266                     insn |= 0x400;
5267                   bfd_put_16 (input_bfd, insn, contents + rel->r_offset);
5268                 }
5269             }
5270           else if (r_type == R_MIPS16_GPREL)
5271             {
5272               unsigned short extend, insn;
5273               bfd_byte buf[4];
5274               unsigned long final;
5275
5276               /* Extract the addend into buf, run the regular reloc,
5277                  and stuff the resulting value back into the
5278                  instructions.  */
5279               if (rel->r_offset > input_section->_raw_size)
5280                 r = bfd_reloc_outofrange;
5281               else
5282                 {
5283                   extend = bfd_get_16 (input_bfd, contents + rel->r_offset);
5284                   insn = bfd_get_16 (input_bfd, contents + rel->r_offset + 2);
5285                   bfd_put_32 (input_bfd,
5286                               (((extend & 0x1f) << 11)
5287                                | (extend & 0x7e0)
5288                                | (insn & 0x1f)),
5289                               buf);
5290                   r = _bfd_final_link_relocate (howto, input_bfd,
5291                                                 input_section, buf,
5292                                                 (bfd_vma) 0, relocation,
5293                                                 addend);
5294                   final = bfd_get_32 (input_bfd, buf);
5295                   bfd_put_16 (input_bfd,
5296                               ((extend & 0xf800)
5297                                | ((final >> 11) & 0x1f)
5298                                | (final & 0x7e0)),
5299                               contents + rel->r_offset);
5300                   bfd_put_16 (input_bfd,
5301                               ((insn & 0xffe0)
5302                                | (final & 0x1f)),
5303                               contents + rel->r_offset + 2);
5304                 }
5305             }
5306           else
5307             r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5308                                           contents, rel->r_offset,
5309                                           relocation, addend);
5310
5311           /* The jal instruction can only jump to an address which is
5312              divisible by 4, and it can only jump to an address with
5313              the same upper 4 bits as the PC.  */
5314           if (r == bfd_reloc_ok
5315               && (r_type == R_MIPS16_26 || r_type == R_MIPS_26))
5316             {
5317               bfd_vma addr;
5318
5319               addr = relocation;
5320               if (other == STO_MIPS16)
5321                 addr &= ~ (bfd_vma) 1;
5322               addr += addend;
5323               if ((addr & 3) != 0
5324                   || ((addr & 0xf0000000)
5325                       != ((input_section->output_section->vma
5326                            + input_section->output_offset
5327                            + rel->r_offset)
5328                           & 0xf0000000)))
5329                 r = bfd_reloc_overflow;
5330             }
5331
5332           if (SGI_COMPAT (abfd)
5333               && scpt != NULL
5334               && (input_section->flags & SEC_ALLOC) != 0)
5335             {
5336               Elf32_crinfo cptrel;
5337               bfd_byte *cr;
5338
5339               /* Make an entry of compact relocation info.  */
5340               mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5341               cptrel.vaddr = (rel->r_offset
5342                               + input_section->output_section->vma
5343                               + input_section->output_offset);
5344
5345               switch (r_type)
5346                 {
5347                 case R_MIPS_26:
5348                   mips_elf_set_cr_type (cptrel, CRT_MIPS_JMPAD);
5349                   /* XXX How should we set dist2to in this case. */
5350                   mips_elf_set_cr_dist2to (cptrel, 8);
5351                   cptrel.konst = addend + relocation;
5352                   cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5353                   bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5354                                              ((Elf32_External_crinfo *) cr
5355                                               + scpt->reloc_count));
5356                   ++scpt->reloc_count;
5357                   break;
5358
5359                 case R_MIPS_GPREL16:
5360                 case R_MIPS_LITERAL:
5361                 case R_MIPS_GPREL32:
5362                   mips_elf_set_cr_type (cptrel, CRT_MIPS_GPHI_LO);
5363                   cptrel.konst = gp - cptrel.vaddr;
5364                   mips_elf_set_cr_dist2to (cptrel, 4);
5365                   cr = scpt->contents + sizeof (Elf32_External_compact_rel);
5366                   bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5367                                              ((Elf32_External_crinfo *) cr
5368                                               + scpt->reloc_count));
5369                   ++scpt->reloc_count;
5370                   break;
5371
5372                 default:
5373                   break;
5374                 }
5375             }
5376         }
5377
5378       if (r != bfd_reloc_ok)
5379         {
5380           switch (r)
5381             {
5382             default:
5383             case bfd_reloc_outofrange:
5384               abort ();
5385             case bfd_reloc_overflow:
5386               {
5387                 const char *name;
5388
5389                 if (h != NULL)
5390                   name = h->root.root.string;
5391                 else
5392                   {
5393                     name = bfd_elf_string_from_elf_section (input_bfd,
5394                                                             symtab_hdr->sh_link,
5395                                                             sym->st_name);
5396                     if (name == NULL)
5397                       return false;
5398                     if (*name == '\0')
5399                       name = bfd_section_name (input_bfd, sec);
5400                   }
5401                 if (! ((*info->callbacks->reloc_overflow)
5402                        (info, name, howto->name, (bfd_vma) 0,
5403                         input_bfd, input_section, rel->r_offset)))
5404                   return false;
5405               }
5406               break;
5407             }
5408         }
5409     }
5410
5411   return true;
5412 }
5413
5414 /* This hook function is called before the linker writes out a global
5415    symbol.  This is where we undo the increment of the value for a
5416    mips16 symbol.  */
5417
5418 /*ARGSIGNORED*/
5419 static boolean
5420 mips_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5421      bfd *abfd;
5422      struct bfd_link_info *info;
5423      const char *name;
5424      Elf_Internal_Sym *sym;
5425      asection *input_sec;
5426 {
5427   if (sym->st_other == STO_MIPS16
5428       && (sym->st_value & 1) != 0)
5429     --sym->st_value;
5430   return true;
5431 }
5432 \f
5433 /* Functions for the dynamic linker.  */
5434
5435 /* The name of the dynamic interpreter.  This is put in the .interp
5436    section.  */
5437
5438 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
5439
5440 /* Create dynamic sections when linking against a dynamic object.  */
5441
5442 static boolean
5443 mips_elf_create_dynamic_sections (abfd, info)
5444      bfd *abfd;
5445      struct bfd_link_info *info;
5446 {
5447   struct elf_link_hash_entry *h;
5448   flagword flags;
5449   register asection *s;
5450   const char * const *namep;
5451
5452   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5453            | SEC_LINKER_CREATED | SEC_READONLY);
5454
5455   /* Mips ABI requests the .dynamic section to be read only.  */
5456   s = bfd_get_section_by_name (abfd, ".dynamic");
5457   if (s != NULL)
5458     {
5459       if (! bfd_set_section_flags (abfd, s, flags))
5460         return false;
5461     }
5462
5463   /* We need to create .got section.  */
5464   if (! mips_elf_create_got_section (abfd, info))
5465     return false;
5466
5467   /* Create .stub section.  */
5468   if (bfd_get_section_by_name (abfd, ".stub") == NULL)
5469     {
5470       s = bfd_make_section (abfd, ".stub");
5471       if (s == NULL
5472           || ! bfd_set_section_flags (abfd, s, flags)
5473           || ! bfd_set_section_alignment (abfd, s, 2))
5474         return false;
5475     }
5476
5477   if (SGI_COMPAT (abfd)
5478       && !info->shared
5479       && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
5480     {
5481       s = bfd_make_section (abfd, ".rld_map");
5482       if (s == NULL
5483           || ! bfd_set_section_flags (abfd, s, flags & ~SEC_READONLY)
5484           || ! bfd_set_section_alignment (abfd, s, 2))
5485         return false;
5486     }
5487
5488   if (SGI_COMPAT (abfd))
5489     {
5490       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
5491         {
5492           h = NULL;
5493           if (! (_bfd_generic_link_add_one_symbol
5494                  (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
5495                   (bfd_vma) 0, (const char *) NULL, false,
5496                   get_elf_backend_data (abfd)->collect,
5497                   (struct bfd_link_hash_entry **) &h)))
5498             return false;
5499           h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5500           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5501           h->type = STT_SECTION;
5502
5503           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5504             return false;
5505         }
5506
5507       /* We need to create a .compact_rel section.  */
5508       if (! mips_elf_create_compact_rel_section (abfd, info))
5509         return false;
5510
5511       /* Change aligments of some sections.  */
5512       s = bfd_get_section_by_name (abfd, ".hash");
5513       if (s != NULL)
5514         bfd_set_section_alignment (abfd, s, 4);
5515       s = bfd_get_section_by_name (abfd, ".dynsym");
5516       if (s != NULL)
5517         bfd_set_section_alignment (abfd, s, 4);
5518       s = bfd_get_section_by_name (abfd, ".dynstr");
5519       if (s != NULL)
5520         bfd_set_section_alignment (abfd, s, 4);
5521       s = bfd_get_section_by_name (abfd, ".reginfo");
5522       if (s != NULL)
5523         bfd_set_section_alignment (abfd, s, 4);
5524       s = bfd_get_section_by_name (abfd, ".dynamic");
5525       if (s != NULL)
5526         bfd_set_section_alignment (abfd, s, 4);
5527     }
5528
5529   if (!info->shared)
5530     {
5531       h = NULL;
5532       if (! (_bfd_generic_link_add_one_symbol
5533              (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
5534               (bfd_vma) 0, (const char *) NULL, false,
5535               get_elf_backend_data (abfd)->collect,
5536               (struct bfd_link_hash_entry **) &h)))
5537         return false;
5538       h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5539       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5540       h->type = STT_SECTION;
5541
5542       if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5543         return false;
5544
5545       if (! mips_elf_hash_table (info)->use_rld_obj_head)
5546         {
5547           /* __rld_map is a four byte word located in the .data section
5548              and is filled in by the rtld to contain a pointer to
5549              the _r_debug structure. Its symbol value will be set in
5550              mips_elf_finish_dynamic_symbol.  */
5551           s = bfd_get_section_by_name (abfd, ".rld_map");
5552           BFD_ASSERT (s != NULL);
5553
5554           h = NULL;
5555           if (! (_bfd_generic_link_add_one_symbol
5556                  (info, abfd, "__rld_map", BSF_GLOBAL, s,
5557                   (bfd_vma) 0, (const char *) NULL, false,
5558                   get_elf_backend_data (abfd)->collect,
5559                   (struct bfd_link_hash_entry **) &h)))
5560             return false;
5561           h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5562           h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5563           h->type = STT_OBJECT;
5564
5565           if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5566             return false;
5567         }
5568     }
5569
5570   return true;
5571 }
5572
5573 /* Create the .compact_rel section.  */
5574
5575 static boolean
5576 mips_elf_create_compact_rel_section (abfd, info)
5577      bfd *abfd;
5578      struct bfd_link_info *info;
5579 {
5580   flagword flags;
5581   register asection *s;
5582
5583   if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
5584     {
5585       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5586                | SEC_READONLY);
5587
5588       s = bfd_make_section (abfd, ".compact_rel");
5589       if (s == NULL
5590           || ! bfd_set_section_flags (abfd, s, flags)
5591           || ! bfd_set_section_alignment (abfd, s, 2))
5592         return false;
5593
5594       s->_raw_size = sizeof (Elf32_External_compact_rel);
5595     }
5596
5597   return true;
5598 }
5599
5600 /* Create the .got section to hold the global offset table. */
5601
5602 static boolean
5603 mips_elf_create_got_section (abfd, info)
5604      bfd *abfd;
5605      struct bfd_link_info *info;
5606 {
5607   flagword flags;
5608   register asection *s;
5609   struct elf_link_hash_entry *h;
5610   struct mips_got_info *g;
5611
5612   /* This function may be called more than once.  */
5613   if (bfd_get_section_by_name (abfd, ".got") != NULL)
5614     return true;
5615
5616   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5617            | SEC_LINKER_CREATED);
5618
5619   s = bfd_make_section (abfd, ".got");
5620   if (s == NULL
5621       || ! bfd_set_section_flags (abfd, s, flags)
5622       || ! bfd_set_section_alignment (abfd, s, 4))
5623     return false;
5624
5625   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
5626      linker script because we don't want to define the symbol if we
5627      are not creating a global offset table.  */
5628   h = NULL;
5629   if (! (_bfd_generic_link_add_one_symbol
5630          (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5631           (bfd_vma) 0, (const char *) NULL, false,
5632           get_elf_backend_data (abfd)->collect,
5633           (struct bfd_link_hash_entry **) &h)))
5634     return false;
5635   h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
5636   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
5637   h->type = STT_OBJECT;
5638
5639   if (info->shared
5640       && ! bfd_elf32_link_record_dynamic_symbol (info, h))
5641     return false;
5642
5643   /* The first several global offset table entries are reserved.  */
5644   s->_raw_size = MIPS_RESERVED_GOTNO * 4;
5645
5646   g = (struct mips_got_info *) bfd_alloc (abfd,
5647                                           sizeof (struct mips_got_info));
5648   if (g == NULL)
5649     return false;
5650   g->global_gotsym = 0;
5651   g->local_gotno = MIPS_RESERVED_GOTNO;
5652   g->assigned_gotno = MIPS_RESERVED_GOTNO;
5653   if (elf_section_data (s) == NULL)
5654     {
5655       s->used_by_bfd =
5656         (PTR) bfd_zalloc (abfd, sizeof (struct bfd_elf_section_data));
5657       if (elf_section_data (s) == NULL)
5658         return false;
5659     }
5660   elf_section_data (s)->tdata = (PTR) g;
5661
5662   return true;
5663 }
5664
5665 /* Look through the relocs for a section during the first phase, and
5666    allocate space in the global offset table.  */
5667
5668 static boolean
5669 mips_elf_check_relocs (abfd, info, sec, relocs)
5670      bfd *abfd;
5671      struct bfd_link_info *info;
5672      asection *sec;
5673      const Elf_Internal_Rela *relocs;
5674 {
5675   const char *name;
5676   bfd *dynobj;
5677   Elf_Internal_Shdr *symtab_hdr;
5678   struct elf_link_hash_entry **sym_hashes;
5679   struct mips_got_info *g;
5680   size_t extsymoff;
5681   const Elf_Internal_Rela *rel;
5682   const Elf_Internal_Rela *rel_end;
5683   asection *sgot;
5684   asection *sreloc;
5685
5686   if (info->relocateable)
5687     return true;
5688
5689   dynobj = elf_hash_table (info)->dynobj;
5690   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5691   sym_hashes = elf_sym_hashes (abfd);
5692   extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
5693
5694   /* Check for the mips16 stub sections.  */
5695
5696   name = bfd_get_section_name (abfd, sec);
5697   if (strncmp (name, FN_STUB, sizeof FN_STUB - 1) == 0)
5698     {
5699       unsigned long r_symndx;
5700
5701       /* Look at the relocation information to figure out which symbol
5702          this is for.  */
5703
5704       r_symndx = ELF32_R_SYM (relocs->r_info);
5705
5706       if (r_symndx < extsymoff
5707           || sym_hashes[r_symndx - extsymoff] == NULL)
5708         {
5709           asection *o;
5710
5711           /* This stub is for a local symbol.  This stub will only be
5712              needed if there is some relocation in this BFD, other
5713              than a 16 bit function call, which refers to this symbol.  */
5714           for (o = abfd->sections; o != NULL; o = o->next)
5715             {
5716               Elf_Internal_Rela *sec_relocs;
5717               const Elf_Internal_Rela *r, *rend;
5718
5719               /* We can ignore stub sections when looking for relocs.  */
5720               if ((o->flags & SEC_RELOC) == 0
5721                   || o->reloc_count == 0
5722                   || strncmp (bfd_get_section_name (abfd, o), FN_STUB,
5723                               sizeof FN_STUB - 1) == 0
5724                   || strncmp (bfd_get_section_name (abfd, o), CALL_STUB,
5725                               sizeof CALL_STUB - 1) == 0
5726                   || strncmp (bfd_get_section_name (abfd, o), CALL_FP_STUB,
5727                               sizeof CALL_FP_STUB - 1) == 0)
5728                 continue;
5729
5730               sec_relocs = (_bfd_elf32_link_read_relocs
5731                             (abfd, o, (PTR) NULL,
5732                              (Elf_Internal_Rela *) NULL,
5733                              info->keep_memory));
5734               if (sec_relocs == NULL)
5735                 return false;
5736
5737               rend = sec_relocs + o->reloc_count;
5738               for (r = sec_relocs; r < rend; r++)
5739                 if (ELF32_R_SYM (r->r_info) == r_symndx
5740                     && ELF32_R_TYPE (r->r_info) != R_MIPS16_26)
5741                   break;
5742
5743               if (! info->keep_memory)
5744                 free (sec_relocs);
5745
5746               if (r < rend)
5747                 break;
5748             }
5749
5750           if (o == NULL)
5751             {
5752               /* There is no non-call reloc for this stub, so we do
5753                  not need it.  Since this function is called before
5754                  the linker maps input sections to output sections, we
5755                  can easily discard it by setting the SEC_EXCLUDE
5756                  flag.  */
5757               sec->flags |= SEC_EXCLUDE;
5758               return true;
5759             }
5760
5761           /* Record this stub in an array of local symbol stubs for
5762              this BFD. */
5763           if (elf_tdata (abfd)->local_stubs == NULL)
5764             {
5765               unsigned long symcount;
5766               asection **n;
5767
5768               if (elf_bad_symtab (abfd))
5769                 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
5770               else
5771                 symcount = symtab_hdr->sh_info;
5772               n = (asection **) bfd_zalloc (abfd,
5773                                             symcount * sizeof (asection *));
5774               if (n == NULL)
5775                 return false;
5776               elf_tdata (abfd)->local_stubs = n;
5777             }
5778
5779           elf_tdata (abfd)->local_stubs[r_symndx] = sec;
5780
5781           /* We don't need to set mips16_stubs_seen in this case.
5782              That flag is used to see whether we need to look through
5783              the global symbol table for stubs.  We don't need to set
5784              it here, because we just have a local stub.  */
5785         }
5786       else
5787         {
5788           struct mips_elf_link_hash_entry *h;
5789
5790           h = ((struct mips_elf_link_hash_entry *)
5791                sym_hashes[r_symndx - extsymoff]);
5792
5793           /* H is the symbol this stub is for.  */
5794
5795           h->fn_stub = sec;
5796           mips_elf_hash_table (info)->mips16_stubs_seen = true;
5797         }
5798     }
5799   else if (strncmp (name, CALL_STUB, sizeof CALL_STUB - 1) == 0
5800            || strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5801     {
5802       unsigned long r_symndx;
5803       struct mips_elf_link_hash_entry *h;
5804       asection **loc;
5805
5806       /* Look at the relocation information to figure out which symbol
5807          this is for.  */
5808
5809       r_symndx = ELF32_R_SYM (relocs->r_info);
5810
5811       if (r_symndx < extsymoff
5812           || sym_hashes[r_symndx - extsymoff] == NULL)
5813         {
5814           /* This stub was actually built for a static symbol defined
5815              in the same file.  We assume that all static symbols in
5816              mips16 code are themselves mips16, so we can simply
5817              discard this stub.  Since this function is called before
5818              the linker maps input sections to output sections, we can
5819              easily discard it by setting the SEC_EXCLUDE flag.  */
5820           sec->flags |= SEC_EXCLUDE;
5821           return true;
5822         }
5823
5824       h = ((struct mips_elf_link_hash_entry *)
5825            sym_hashes[r_symndx - extsymoff]);
5826
5827       /* H is the symbol this stub is for.  */
5828
5829       if (strncmp (name, CALL_FP_STUB, sizeof CALL_FP_STUB - 1) == 0)
5830         loc = &h->call_fp_stub;
5831       else
5832         loc = &h->call_stub;
5833
5834       /* If we already have an appropriate stub for this function, we
5835          don't need another one, so we can discard this one.  Since
5836          this function is called before the linker maps input sections
5837          to output sections, we can easily discard it by setting the
5838          SEC_EXCLUDE flag.  We can also discard this section if we
5839          happen to already know that this is a mips16 function; it is
5840          not necessary to check this here, as it is checked later, but
5841          it is slightly faster to check now.  */
5842       if (*loc != NULL || h->root.other == STO_MIPS16)
5843         {
5844           sec->flags |= SEC_EXCLUDE;
5845           return true;
5846         }
5847
5848       *loc = sec;
5849       mips_elf_hash_table (info)->mips16_stubs_seen = true;
5850     }
5851
5852   if (dynobj == NULL)
5853     {
5854       sgot = NULL;
5855       g = NULL;
5856     }
5857   else
5858     {
5859       sgot = bfd_get_section_by_name (dynobj, ".got");
5860       if (sgot == NULL)
5861         g = NULL;
5862       else
5863         {
5864           BFD_ASSERT (elf_section_data (sgot) != NULL);
5865           g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5866           BFD_ASSERT (g != NULL);
5867         }
5868     }
5869
5870   sreloc = NULL;
5871
5872   rel_end = relocs + sec->reloc_count;
5873   for (rel = relocs; rel < rel_end; rel++)
5874     {
5875       unsigned long r_symndx;
5876       struct elf_link_hash_entry *h;
5877
5878       r_symndx = ELF32_R_SYM (rel->r_info);
5879
5880       if (r_symndx < extsymoff)
5881         h = NULL;
5882       else
5883         {
5884           h = sym_hashes[r_symndx - extsymoff];
5885
5886           /* This may be an indirect symbol created because of a version.  */
5887           if (h != NULL)
5888             {
5889               while (h->root.type == bfd_link_hash_indirect)
5890                 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5891             }
5892         }
5893
5894       /* Some relocs require a global offset table.  */
5895       if (dynobj == NULL || sgot == NULL)
5896         {
5897           switch (ELF32_R_TYPE (rel->r_info))
5898             {
5899             case R_MIPS_GOT16:
5900             case R_MIPS_CALL16:
5901             case R_MIPS_CALL_HI16:
5902             case R_MIPS_CALL_LO16:
5903             case R_MIPS_GOT_HI16:
5904             case R_MIPS_GOT_LO16:
5905               if (dynobj == NULL)
5906                 elf_hash_table (info)->dynobj = dynobj = abfd;
5907               if (! mips_elf_create_got_section (dynobj, info))
5908                 return false;
5909               sgot = bfd_get_section_by_name (dynobj, ".got");
5910               BFD_ASSERT (sgot != NULL);
5911               BFD_ASSERT (elf_section_data (sgot) != NULL);
5912               g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
5913               BFD_ASSERT (g != NULL);
5914               break;
5915
5916             case R_MIPS_32:
5917             case R_MIPS_REL32:
5918               if (dynobj == NULL
5919                   && (info->shared || h != NULL)
5920                   && (sec->flags & SEC_ALLOC) != 0)
5921                 elf_hash_table (info)->dynobj = dynobj = abfd;
5922               break;
5923
5924             default:
5925               break;
5926             }
5927         }
5928
5929       switch (ELF32_R_TYPE (rel->r_info))
5930         {
5931         case R_MIPS_CALL16:
5932         case R_MIPS_CALL_HI16:
5933         case R_MIPS_CALL_LO16:
5934           /* This symbol requires a global offset table entry.  */
5935
5936           if (h == NULL)
5937             {
5938               (*_bfd_error_handler)
5939                 ("%s: CALL16 reloc at 0x%lx not against global symbol",
5940                  bfd_get_filename (abfd), (unsigned long) rel->r_offset);
5941               bfd_set_error (bfd_error_bad_value);
5942               return false;
5943             }
5944
5945           /* Make sure this symbol is output as a dynamic symbol.  */
5946           if (h->dynindx == -1)
5947             {
5948               if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5949                 return false;
5950             }
5951
5952           if (h->got_offset != (bfd_vma) -1)
5953             {
5954               /* We have already allocated space in the .got.  */
5955               break;
5956             }
5957
5958           /* Note the index of the first global got symbol in .dynsym.  */
5959           if (g->global_gotsym == 0
5960               || g->global_gotsym > (unsigned long) h->dynindx)
5961             g->global_gotsym = h->dynindx;
5962
5963           /* Make this symbol to have the corresponding got entry.  */
5964           h->got_offset = 0;
5965
5966           /* We need a stub, not a plt entry for the undefined
5967              function.  But we record it as if it needs plt.  See
5968              elf_adjust_dynamic_symbol in elflink.h.  */
5969           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
5970           h->type = STT_FUNC;
5971
5972           break;
5973
5974         case R_MIPS_GOT16:
5975         case R_MIPS_GOT_HI16:
5976         case R_MIPS_GOT_LO16:
5977           /* This symbol requires a global offset table entry.  */
5978
5979           if (h != NULL)
5980             {
5981               /* Make sure this symbol is output as a dynamic symbol.  */
5982               if (h->dynindx == -1)
5983                 {
5984                   if (! bfd_elf32_link_record_dynamic_symbol (info, h))
5985                     return false;
5986                 }
5987
5988               if (h->got_offset != (bfd_vma) -1)
5989                 {
5990                   /* We have already allocated space in the .got.  */
5991                   break;
5992                 }
5993               /* Note the index of the first global got symbol in
5994                  .dynsym.  */
5995               if (g->global_gotsym == 0
5996                   || g->global_gotsym > (unsigned long) h->dynindx)
5997                 g->global_gotsym = h->dynindx;
5998
5999               /* Make this symbol to be the global got symbol.  */
6000               h->got_offset = 0;
6001             }
6002
6003           break;
6004
6005         case R_MIPS_32:
6006         case R_MIPS_REL32:
6007           if ((info->shared || h != NULL)
6008               && (sec->flags & SEC_ALLOC) != 0)
6009             {
6010               if (sreloc == NULL)
6011                 {
6012                   const char *name = ".rel.dyn";
6013
6014                   sreloc = bfd_get_section_by_name (dynobj, name);
6015                   if (sreloc == NULL)
6016                     {
6017                       sreloc = bfd_make_section (dynobj, name);
6018                       if (sreloc == NULL
6019                           || ! bfd_set_section_flags (dynobj, sreloc,
6020                                                       (SEC_ALLOC
6021                                                        | SEC_LOAD
6022                                                        | SEC_HAS_CONTENTS
6023                                                        | SEC_IN_MEMORY
6024                                                        | SEC_LINKER_CREATED
6025                                                        | SEC_READONLY))
6026                           || ! bfd_set_section_alignment (dynobj, sreloc,
6027                                                           4))
6028                         return false;
6029                     }
6030                 }
6031               if (info->shared)
6032                 {
6033                   /* When creating a shared object, we must copy these
6034                      reloc types into the output file as R_MIPS_REL32
6035                      relocs.  We make room for this reloc in the
6036                      .rel.dyn reloc section */
6037                   if (sreloc->_raw_size == 0)
6038                     {
6039                       /* Add a null element. */
6040                       sreloc->_raw_size += sizeof (Elf32_External_Rel);
6041                       ++sreloc->reloc_count;
6042                     }
6043                   sreloc->_raw_size += sizeof (Elf32_External_Rel);
6044                 }
6045               else
6046                 {
6047                   struct mips_elf_link_hash_entry *hmips;
6048
6049                   /* We only need to copy this reloc if the symbol is
6050                      defined in a dynamic object.  */
6051                   hmips = (struct mips_elf_link_hash_entry *) h;
6052                   ++hmips->mips_32_relocs;
6053                 }
6054             }
6055
6056           if (SGI_COMPAT (abfd))
6057             mips_elf_hash_table (info)->compact_rel_size +=
6058               sizeof (Elf32_External_crinfo);
6059
6060           break;
6061
6062         case R_MIPS_26:
6063         case R_MIPS_GPREL16:
6064         case R_MIPS_LITERAL:
6065         case R_MIPS_GPREL32:
6066           if (SGI_COMPAT (abfd))
6067             mips_elf_hash_table (info)->compact_rel_size +=
6068               sizeof (Elf32_External_crinfo);
6069           break;
6070
6071         default:
6072           break;
6073         }
6074
6075       /* If this reloc is not a 16 bit call, and it has a global
6076          symbol, then we will need the fn_stub if there is one.
6077          References from a stub section do not count. */
6078       if (h != NULL
6079           && ELF32_R_TYPE (rel->r_info) != R_MIPS16_26
6080           && strncmp (bfd_get_section_name (abfd, sec), FN_STUB,
6081                       sizeof FN_STUB - 1) != 0
6082           && strncmp (bfd_get_section_name (abfd, sec), CALL_STUB,
6083                       sizeof CALL_STUB - 1) != 0
6084           && strncmp (bfd_get_section_name (abfd, sec), CALL_FP_STUB,
6085                       sizeof CALL_FP_STUB - 1) != 0)
6086         {
6087           struct mips_elf_link_hash_entry *mh;
6088
6089           mh = (struct mips_elf_link_hash_entry *) h;
6090           mh->need_fn_stub = true;
6091         }
6092     }
6093
6094   return true;
6095 }
6096
6097 /* Adjust a symbol defined by a dynamic object and referenced by a
6098    regular object.  The current definition is in some section of the
6099    dynamic object, but we're not including those sections.  We have to
6100    change the definition to something the rest of the link can
6101    understand.  */
6102
6103 static boolean
6104 mips_elf_adjust_dynamic_symbol (info, h)
6105      struct bfd_link_info *info;
6106      struct elf_link_hash_entry *h;
6107 {
6108   bfd *dynobj;
6109   struct mips_elf_link_hash_entry *hmips;
6110   asection *s;
6111
6112   dynobj = elf_hash_table (info)->dynobj;
6113
6114   /* Make sure we know what is going on here.  */
6115   BFD_ASSERT (dynobj != NULL
6116               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
6117                   || h->weakdef != NULL
6118                   || ((h->elf_link_hash_flags
6119                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
6120                       && (h->elf_link_hash_flags
6121                           & ELF_LINK_HASH_REF_REGULAR) != 0
6122                       && (h->elf_link_hash_flags
6123                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
6124
6125   /* If this symbol is defined in a dynamic object, we need to copy
6126      any R_MIPS_32 or R_MIPS_REL32 relocs against it into the output
6127      file.  */
6128   hmips = (struct mips_elf_link_hash_entry *) h;
6129   if (! info->relocateable
6130       && hmips->mips_32_relocs != 0
6131       && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6132     {
6133       s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6134       BFD_ASSERT (s != NULL);
6135
6136       if (s->_raw_size == 0)
6137         {
6138           /* Make room for a null element. */
6139           s->_raw_size += sizeof (Elf32_External_Rel);
6140           ++s->reloc_count;
6141         }
6142       s->_raw_size += hmips->mips_32_relocs * sizeof (Elf32_External_Rel);
6143     }
6144
6145   /* For a function, create a stub, if needed. */
6146   if (h->type == STT_FUNC
6147       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
6148     {
6149       if (! elf_hash_table (info)->dynamic_sections_created)
6150         return true;
6151
6152       /* If this symbol is not defined in a regular file, then set
6153          the symbol to the stub location.  This is required to make
6154          function pointers compare as equal between the normal
6155          executable and the shared library.  */
6156       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
6157         {
6158           /* We need .stub section.  */
6159           s = bfd_get_section_by_name (dynobj, ".stub");
6160           BFD_ASSERT (s != NULL);
6161
6162           h->root.u.def.section = s;
6163           h->root.u.def.value = s->_raw_size;
6164
6165           /* XXX Write this stub address somewhere.  */
6166           h->plt_offset = s->_raw_size;
6167
6168           /* Make room for this stub code.  */
6169           s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6170
6171           /* The last half word of the stub will be filled with the index
6172              of this symbol in .dynsym section.  */
6173           return true;
6174         }
6175     }
6176
6177   /* If this is a weak symbol, and there is a real definition, the
6178      processor independent code will have arranged for us to see the
6179      real definition first, and we can just use the same value.  */
6180   if (h->weakdef != NULL)
6181     {
6182       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
6183                   || h->weakdef->root.type == bfd_link_hash_defweak);
6184       h->root.u.def.section = h->weakdef->root.u.def.section;
6185       h->root.u.def.value = h->weakdef->root.u.def.value;
6186       return true;
6187     }
6188
6189   /* This is a reference to a symbol defined by a dynamic object which
6190      is not a function.  */
6191
6192   return true;
6193 }
6194
6195 /* This function is called after all the input files have been read,
6196    and the input sections have been assigned to output sections.  We
6197    check for any mips16 stub sections that we can discard.  */
6198
6199 static boolean mips_elf_check_mips16_stubs
6200   PARAMS ((struct mips_elf_link_hash_entry *, PTR));
6201
6202 static boolean
6203 mips_elf_always_size_sections (output_bfd, info)
6204      bfd *output_bfd;
6205      struct bfd_link_info *info;
6206 {
6207   if (info->relocateable
6208       || ! mips_elf_hash_table (info)->mips16_stubs_seen)
6209     return true;
6210
6211   mips_elf_link_hash_traverse (mips_elf_hash_table (info),
6212                                mips_elf_check_mips16_stubs,
6213                                (PTR) NULL);
6214
6215   return true;
6216 }
6217
6218 /* Check the mips16 stubs for a particular symbol, and see if we can
6219    discard them.  */
6220
6221 /*ARGSUSED*/
6222 static boolean
6223 mips_elf_check_mips16_stubs (h, data)
6224      struct mips_elf_link_hash_entry *h;
6225      PTR data;
6226 {
6227   if (h->fn_stub != NULL
6228       && ! h->need_fn_stub)
6229     {
6230       /* We don't need the fn_stub; the only references to this symbol
6231          are 16 bit calls.  Clobber the size to 0 to prevent it from
6232          being included in the link.  */
6233       h->fn_stub->_raw_size = 0;
6234       h->fn_stub->_cooked_size = 0;
6235       h->fn_stub->flags &= ~ SEC_RELOC;
6236       h->fn_stub->reloc_count = 0;
6237       h->fn_stub->flags |= SEC_EXCLUDE;
6238     }
6239
6240   if (h->call_stub != NULL
6241       && h->root.other == STO_MIPS16)
6242     {
6243       /* We don't need the call_stub; this is a 16 bit function, so
6244          calls from other 16 bit functions are OK.  Clobber the size
6245          to 0 to prevent it from being included in the link.  */
6246       h->call_stub->_raw_size = 0;
6247       h->call_stub->_cooked_size = 0;
6248       h->call_stub->flags &= ~ SEC_RELOC;
6249       h->call_stub->reloc_count = 0;
6250       h->call_stub->flags |= SEC_EXCLUDE;
6251     }
6252
6253   if (h->call_fp_stub != NULL
6254       && h->root.other == STO_MIPS16)
6255     {
6256       /* We don't need the call_stub; this is a 16 bit function, so
6257          calls from other 16 bit functions are OK.  Clobber the size
6258          to 0 to prevent it from being included in the link.  */
6259       h->call_fp_stub->_raw_size = 0;
6260       h->call_fp_stub->_cooked_size = 0;
6261       h->call_fp_stub->flags &= ~ SEC_RELOC;
6262       h->call_fp_stub->reloc_count = 0;
6263       h->call_fp_stub->flags |= SEC_EXCLUDE;
6264     }
6265
6266   return true;
6267 }
6268
6269 /* Set the sizes of the dynamic sections.  */
6270
6271 static boolean
6272 mips_elf_size_dynamic_sections (output_bfd, info)
6273      bfd *output_bfd;
6274      struct bfd_link_info *info;
6275 {
6276   bfd *dynobj;
6277   asection *s;
6278   boolean reltext;
6279   asection *sgot;
6280   struct mips_got_info *g;
6281
6282   dynobj = elf_hash_table (info)->dynobj;
6283   BFD_ASSERT (dynobj != NULL);
6284
6285   if (elf_hash_table (info)->dynamic_sections_created)
6286     {
6287       /* Set the contents of the .interp section to the interpreter.  */
6288       if (! info->shared)
6289         {
6290           s = bfd_get_section_by_name (dynobj, ".interp");
6291           BFD_ASSERT (s != NULL);
6292           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
6293           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
6294         }
6295     }
6296
6297   /* Recompute the size of .got for local entires (reserved and
6298      hipages) if needed.  To estimate it, get the upper bound of total
6299      size of loadable sections.  */
6300   sgot = bfd_get_section_by_name (dynobj, ".got");
6301
6302   if (sgot != NULL)
6303     {
6304       bfd_size_type loadable_size = 0;
6305       bfd_size_type local_gotno;
6306       struct _bfd *sub;
6307
6308       BFD_ASSERT (elf_section_data (sgot) != NULL);
6309       g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6310       BFD_ASSERT (g != NULL);
6311
6312       for (sub = info->input_bfds; sub; sub = sub->link_next)
6313         for (s = sub->sections; s != NULL; s = s->next)
6314           {
6315             if ((s->flags & SEC_ALLOC) == 0)
6316               continue;
6317             loadable_size += (s->_raw_size + 0xf) & ~0xf;
6318           }
6319
6320       loadable_size += MIPS_FUNCTION_STUB_SIZE;
6321
6322       /* Assume there are two loadable segments consisting of
6323          contiguous sections.  Is 5 enough? */
6324       local_gotno = (loadable_size >> 16) + 5 + MIPS_RESERVED_GOTNO;
6325       g->local_gotno = local_gotno;
6326       sgot->_raw_size += local_gotno * 4;
6327     }
6328
6329   /* The check_relocs and adjust_dynamic_symbol entry points have
6330      determined the sizes of the various dynamic sections.  Allocate
6331      memory for them.  */
6332   reltext = false;
6333   for (s = dynobj->sections; s != NULL; s = s->next)
6334     {
6335       const char *name;
6336       boolean strip;
6337
6338       /* It's OK to base decisions on the section name, because none
6339          of the dynobj section names depend upon the input files.  */
6340       name = bfd_get_section_name (dynobj, s);
6341
6342       if ((s->flags & SEC_LINKER_CREATED) == 0)
6343         continue;
6344
6345       strip = false;
6346
6347       if (strncmp (name, ".rel", 4) == 0)
6348         {
6349           if (s->_raw_size == 0)
6350             strip = true;
6351           else
6352             {
6353               const char *outname;
6354               asection *target;
6355
6356               /* If this relocation section applies to a read only
6357                  section, then we probably need a DT_TEXTREL entry.
6358                  If the relocation section is .rel.dyn, we always
6359                  assert a DT_TEXTREL entry rather than testing whether
6360                  there exists a relocation to a read only section or
6361                  not.  */
6362               outname = bfd_get_section_name (output_bfd,
6363                                               s->output_section);
6364               target = bfd_get_section_by_name (output_bfd, outname + 4);
6365               if ((target != NULL && (target->flags & SEC_READONLY) != 0)
6366                   || strcmp (outname, ".rel.dyn") == 0)
6367                 reltext = true;
6368
6369               /* We use the reloc_count field as a counter if we need
6370                  to copy relocs into the output file.  */
6371               if (strcmp (name, ".rel.dyn") != 0)
6372                 s->reloc_count = 0;
6373             }
6374         }
6375       else if (strncmp (name, ".got", 4) == 0)
6376         {
6377           int i;
6378
6379           BFD_ASSERT (elf_section_data (s) != NULL);
6380           g = (struct mips_got_info *) elf_section_data (s)->tdata;
6381           BFD_ASSERT (g != NULL);
6382
6383           /* Fix the size of .got section for the correspondence of
6384              global symbols and got entries. This adds some useless
6385              got entries. Is this required by ABI really?  */
6386           i = elf_hash_table (info)->dynsymcount - g->global_gotsym;
6387           s->_raw_size += i * 4;
6388         }
6389       else if (strncmp (name, ".stub", 5) == 0)
6390         {
6391           /* Irix rld assumes that the function stub isn't at the end
6392              of .text section. So put a dummy. XXX  */
6393           s->_raw_size += MIPS_FUNCTION_STUB_SIZE;
6394         }
6395       else if (! info->shared
6396                && ! mips_elf_hash_table (info)->use_rld_obj_head
6397                && strncmp (name, ".rld_map", 8) == 0)
6398         {
6399           /* We add a room for __rld_map. It will be filled in by the
6400              rtld to contain a pointer to the _r_debug structure.  */
6401           s->_raw_size += 4;
6402         }
6403       else if (SGI_COMPAT (output_bfd)
6404                && strncmp (name, ".compact_rel", 12) == 0)
6405         s->_raw_size += mips_elf_hash_table (info)->compact_rel_size;
6406       else if (strncmp (name, ".init", 5) != 0)
6407         {
6408           /* It's not one of our sections, so don't allocate space.  */
6409           continue;
6410         }
6411
6412       if (strip)
6413         {
6414           asection **spp;
6415
6416           for (spp = &s->output_section->owner->sections;
6417                *spp != s->output_section;
6418                spp = &(*spp)->next)
6419             ;
6420           *spp = s->output_section->next;
6421           --s->output_section->owner->section_count;
6422
6423           continue;
6424         }
6425
6426       /* Allocate memory for the section contents.  */
6427       s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size);
6428       if (s->contents == NULL && s->_raw_size != 0)
6429         {
6430           bfd_set_error (bfd_error_no_memory);
6431           return false;
6432         }
6433       memset (s->contents, 0, s->_raw_size);
6434     }
6435
6436   if (elf_hash_table (info)->dynamic_sections_created)
6437     {
6438       /* Add some entries to the .dynamic section.  We fill in the
6439          values later, in elf_mips_finish_dynamic_sections, but we
6440          must add the entries now so that we get the correct size for
6441          the .dynamic section.  The DT_DEBUG entry is filled in by the
6442          dynamic linker and used by the debugger.  */
6443       if (! info->shared)
6444         {
6445           if (SGI_COMPAT (output_bfd))
6446             {
6447               /* SGI object has the equivalence of DT_DEBUG in the
6448                  DT_MIPS_RLD_MAP entry.  */
6449               if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_MAP, 0))
6450                 return false;
6451             }
6452           else
6453             if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
6454               return false;
6455         }
6456
6457       if (reltext)
6458         {
6459           if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
6460             return false;
6461         }
6462
6463       if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0))
6464         return false;
6465
6466       if (bfd_get_section_by_name (dynobj, ".rel.dyn"))
6467         {
6468           if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0))
6469             return false;
6470
6471           if (! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0))
6472             return false;
6473
6474           if (! bfd_elf32_add_dynamic_entry (info, DT_RELENT, 0))
6475             return false;
6476         }
6477
6478       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICTNO, 0))
6479         return false;
6480
6481       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLISTNO, 0))
6482         return false;
6483
6484       if (bfd_get_section_by_name (dynobj, ".conflict") != NULL)
6485         {
6486           if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_CONFLICT, 0))
6487             return false;
6488
6489           s = bfd_get_section_by_name (dynobj, ".liblist");
6490           BFD_ASSERT (s != NULL);
6491
6492           if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LIBLIST, 0))
6493             return false;
6494         }
6495
6496       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_RLD_VERSION, 0))
6497         return false;
6498
6499       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_FLAGS, 0))
6500         return false;
6501
6502 #if 0
6503       /* Time stamps in executable files are a bad idea.  */
6504       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_TIME_STAMP, 0))
6505         return false;
6506 #endif
6507
6508 #if 0 /* FIXME  */
6509       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_ICHECKSUM, 0))
6510         return false;
6511 #endif
6512
6513 #if 0 /* FIXME  */
6514       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_IVERSION, 0))
6515         return false;
6516 #endif
6517
6518       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_BASE_ADDRESS, 0))
6519         return false;
6520
6521       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_LOCAL_GOTNO, 0))
6522         return false;
6523
6524       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_SYMTABNO, 0))
6525         return false;
6526
6527       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_UNREFEXTNO, 0))
6528         return false;
6529
6530       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_GOTSYM, 0))
6531         return false;
6532
6533       if (! bfd_elf32_add_dynamic_entry (info, DT_MIPS_HIPAGENO, 0))
6534         return false;
6535
6536 #if 0 /* (SGI_COMPAT) */
6537       if (! bfd_get_section_by_name (dynobj, ".init"))
6538         if (! bfd_elf32_add_dynamic_entry (info, DT_INIT, 0))
6539           return false;
6540
6541       if (! bfd_get_section_by_name (dynobj, ".fini"))
6542         if (! bfd_elf32_add_dynamic_entry (info, DT_FINI, 0))
6543           return false;
6544 #endif
6545     }
6546
6547   /* If we use dynamic linking, we generate a section symbol for each
6548      output section.  These are local symbols, which means that they
6549      must come first in the dynamic symbol table.
6550      That means we must increment the dynamic symbol index of every
6551      other dynamic symbol.  */
6552   {
6553     const char * const *namep;
6554     unsigned int c, i;
6555     bfd_size_type strindex;
6556     struct bfd_strtab_hash *dynstr;
6557     struct mips_got_info *g;
6558
6559     c = 0;
6560     if (elf_hash_table (info)->dynamic_sections_created)
6561       {
6562         if (SGI_COMPAT (output_bfd))
6563           {
6564             c = SIZEOF_MIPS_DYNSYM_SECNAMES - 1;
6565             elf_link_hash_traverse (elf_hash_table (info),
6566                                     mips_elf_adjust_dynindx,
6567                                     (PTR) &c);
6568             elf_hash_table (info)->dynsymcount += c;
6569
6570             dynstr = elf_hash_table (info)->dynstr;
6571             BFD_ASSERT (dynstr != NULL);
6572
6573             for (i = 1, namep = mips_elf_dynsym_sec_names;
6574                  *namep != NULL;
6575                  i++, namep++)
6576               {
6577                 s = bfd_get_section_by_name (output_bfd, *namep);
6578                 if (s != NULL)
6579                   elf_section_data (s)->dynindx = i;
6580
6581                 strindex = _bfd_stringtab_add (dynstr, *namep, true, false);
6582                 if (strindex == (bfd_size_type) -1)
6583                   return false;
6584
6585                 mips_elf_hash_table (info)->dynsym_sec_strindex[i] = strindex;
6586               }
6587           }
6588         else
6589           {
6590             c = bfd_count_sections (output_bfd);
6591             elf_link_hash_traverse (elf_hash_table (info),
6592                                     mips_elf_adjust_dynindx,
6593                                     (PTR) &c);
6594             elf_hash_table (info)->dynsymcount += c;
6595
6596             for (i = 1, s = output_bfd->sections; s != NULL; s = s->next, i++)
6597               {
6598                 elf_section_data (s)->dynindx = i;
6599                 /* These symbols will have no names, so we don't need to
6600                    fiddle with dynstr_index.  */
6601               }
6602           }
6603       }
6604
6605     if (sgot != NULL)
6606       {
6607         BFD_ASSERT (elf_section_data (sgot) != NULL);
6608         g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6609         BFD_ASSERT (g != NULL);
6610
6611         /* If there are no global got symbols, fake the last symbol so
6612            for safety.  */
6613         if (g->global_gotsym)
6614           g->global_gotsym += c;
6615         else
6616           g->global_gotsym = elf_hash_table (info)->dynsymcount - 1;
6617       }
6618   }
6619
6620   return true;
6621 }
6622
6623 /* Increment the index of a dynamic symbol by a given amount.  Called
6624    via elf_link_hash_traverse.  */
6625
6626 static boolean
6627 mips_elf_adjust_dynindx (h, cparg)
6628      struct elf_link_hash_entry *h;
6629      PTR cparg;
6630 {
6631   unsigned int *cp = (unsigned int *) cparg;
6632
6633   if (h->dynindx != -1)
6634     h->dynindx += *cp;
6635   return true;
6636 }
6637
6638 /* Finish up dynamic symbol handling.  We set the contents of various
6639    dynamic sections here.  */
6640
6641 static boolean
6642 mips_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
6643      bfd *output_bfd;
6644      struct bfd_link_info *info;
6645      struct elf_link_hash_entry *h;
6646      Elf_Internal_Sym *sym;
6647 {
6648   bfd *dynobj;
6649   bfd_vma gval;
6650   asection *sgot;
6651   struct mips_got_info *g;
6652   const char *name;
6653
6654   dynobj = elf_hash_table (info)->dynobj;
6655   gval = sym->st_value;
6656
6657   if (h->plt_offset != (bfd_vma) -1)
6658     {
6659       asection *s;
6660       bfd_byte *p;
6661       bfd_byte stub[MIPS_FUNCTION_STUB_SIZE];
6662
6663       /* This symbol has a stub.  Set it up.  */
6664
6665       BFD_ASSERT (h->dynindx != -1);
6666
6667       s = bfd_get_section_by_name (dynobj, ".stub");
6668       BFD_ASSERT (s != NULL);
6669
6670       /* Fill the stub.  */
6671       p = stub;
6672       bfd_put_32 (output_bfd, STUB_LW(output_bfd), p);
6673       p += 4;
6674       bfd_put_32 (output_bfd, STUB_MOVE, p);
6675       p += 4;
6676
6677       /* FIXME: Can h->dynindex be more than 64K?  */
6678       if (h->dynindx & 0xffff0000)
6679         return false;
6680
6681       bfd_put_32 (output_bfd, STUB_JALR, p);
6682       p += 4;
6683       bfd_put_32 (output_bfd, STUB_LI16 + h->dynindx, p);
6684
6685       BFD_ASSERT (h->plt_offset <= s->_raw_size);
6686       memcpy (s->contents + h->plt_offset, stub, MIPS_FUNCTION_STUB_SIZE);
6687
6688       /* Mark the symbol as undefined.  plt_offset != -1 occurs
6689          only for the referenced symbol.  */
6690       sym->st_shndx = SHN_UNDEF;
6691
6692       /* The run-time linker uses the st_value field of the symbol
6693          to reset the global offset table entry for this external
6694          to its stub address when unlinking a shared object.  */
6695       gval = s->output_section->vma + s->output_offset + h->plt_offset;
6696       sym->st_value = gval;
6697     }
6698
6699   BFD_ASSERT (h->dynindx != -1);
6700
6701   sgot = bfd_get_section_by_name (dynobj, ".got");
6702   BFD_ASSERT (sgot != NULL);
6703   BFD_ASSERT (elf_section_data (sgot) != NULL);
6704   g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6705   BFD_ASSERT (g != NULL);
6706
6707   if ((unsigned long) h->dynindx >= g->global_gotsym)
6708     {
6709       bfd_size_type offset;
6710
6711       /* This symbol has an entry in the global offset table.  Set its
6712          value to the corresponding got entry, if needed.  */
6713       if (h->got_offset == (bfd_vma) -1)
6714         {
6715           offset = (h->dynindx - g->global_gotsym + g->local_gotno) * 4;
6716           BFD_ASSERT (g->local_gotno * 4 <= offset
6717                       && offset < sgot->_raw_size);
6718           bfd_put_32 (output_bfd, gval, sgot->contents + offset);
6719         }
6720     }
6721
6722   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
6723   name = h->root.root.string;
6724   if (strcmp (name, "_DYNAMIC") == 0
6725       || strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
6726     sym->st_shndx = SHN_ABS;
6727   else if (strcmp (name, "_DYNAMIC_LINK") == 0)
6728     {
6729       sym->st_shndx = SHN_ABS;
6730       sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6731       sym->st_value = 1;
6732     }
6733   else if (SGI_COMPAT (output_bfd))
6734     {
6735       if (strcmp (name, "_gp_disp") == 0)
6736         {
6737           sym->st_shndx = SHN_ABS;
6738           sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6739           sym->st_value = elf_gp (output_bfd);
6740         }
6741       else if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
6742                || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
6743         {
6744           sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6745           sym->st_other = STO_PROTECTED;
6746           sym->st_value = 0;
6747           sym->st_shndx = SHN_MIPS_DATA;
6748         }
6749       else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
6750         {
6751           sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6752           sym->st_other = STO_PROTECTED;
6753           sym->st_value = mips_elf_hash_table (info)->procedure_count;
6754           sym->st_shndx = SHN_ABS;
6755         }
6756       else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
6757         {
6758           if (h->type == STT_FUNC)
6759             sym->st_shndx = SHN_MIPS_TEXT;
6760           else if (h->type == STT_OBJECT)
6761             sym->st_shndx = SHN_MIPS_DATA;
6762         }
6763     }
6764
6765   if (SGI_COMPAT (output_bfd)
6766       && ! info->shared)
6767     {
6768       if (! mips_elf_hash_table (info)->use_rld_obj_head
6769           && strcmp (name, "__rld_map") == 0)
6770         {
6771           asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
6772           BFD_ASSERT (s != NULL);
6773           sym->st_value = s->output_section->vma + s->output_offset;
6774           bfd_put_32 (output_bfd, (bfd_vma) 0, s->contents);
6775           if (mips_elf_hash_table (info)->rld_value == 0)
6776             mips_elf_hash_table (info)->rld_value = sym->st_value;
6777         }
6778       else if (mips_elf_hash_table (info)->use_rld_obj_head
6779                && strcmp (name, "__rld_obj_head") == 0)
6780         {
6781           asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
6782           BFD_ASSERT (s != NULL);
6783           mips_elf_hash_table (info)->rld_value = sym->st_value;
6784         }
6785     }
6786
6787   /* If this is a mips16 symbol, force the value to be even.  */
6788   if (sym->st_other == STO_MIPS16
6789       && (sym->st_value & 1) != 0)
6790     --sym->st_value;
6791
6792   return true;
6793 }
6794
6795 /* Finish up the dynamic sections.  */
6796
6797 static boolean
6798 mips_elf_finish_dynamic_sections (output_bfd, info)
6799      bfd *output_bfd;
6800      struct bfd_link_info *info;
6801 {
6802   bfd *dynobj;
6803   asection *sdyn;
6804   asection *sgot;
6805   struct mips_got_info *g;
6806
6807   dynobj = elf_hash_table (info)->dynobj;
6808
6809   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6810
6811   sgot = bfd_get_section_by_name (dynobj, ".got");
6812   if (sgot == NULL)
6813     g = NULL;
6814   else
6815     {
6816       BFD_ASSERT (elf_section_data (sgot) != NULL);
6817       g = (struct mips_got_info *) elf_section_data (sgot)->tdata;
6818       BFD_ASSERT (g != NULL);
6819     }
6820
6821   if (elf_hash_table (info)->dynamic_sections_created)
6822     {
6823       Elf32_External_Dyn *dyncon, *dynconend;
6824
6825       BFD_ASSERT (sdyn != NULL);
6826       BFD_ASSERT (g != NULL);
6827
6828       dyncon = (Elf32_External_Dyn *) sdyn->contents;
6829       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
6830       for (; dyncon < dynconend; dyncon++)
6831         {
6832           Elf_Internal_Dyn dyn;
6833           const char *name;
6834           size_t elemsize;
6835           asection *s;
6836
6837           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6838
6839           switch (dyn.d_tag)
6840             {
6841             default:
6842               break;
6843
6844             case DT_RELENT:
6845               s = bfd_get_section_by_name (dynobj, ".rel.dyn");
6846               BFD_ASSERT (s != NULL);
6847               dyn.d_un.d_val = sizeof (Elf32_External_Rel);
6848               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6849               break;
6850
6851             case DT_STRSZ:
6852               /* Rewrite DT_STRSZ.  */
6853               dyn.d_un.d_val =
6854                 _bfd_stringtab_size (elf_hash_table (info)->dynstr);
6855               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6856               break;
6857
6858             case DT_PLTGOT:
6859               name = ".got";
6860               goto get_vma;
6861             case DT_MIPS_CONFLICT:
6862               name = ".conflict";
6863               goto get_vma;
6864             case DT_MIPS_LIBLIST:
6865               name = ".liblist";
6866             get_vma:
6867               s = bfd_get_section_by_name (output_bfd, name);
6868               BFD_ASSERT (s != NULL);
6869               dyn.d_un.d_ptr = s->vma;
6870               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6871               break;
6872
6873             case DT_MIPS_RLD_VERSION:
6874               dyn.d_un.d_val = 1; /* XXX */
6875               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6876               break;
6877
6878             case DT_MIPS_FLAGS:
6879               dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
6880               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6881               break;
6882
6883             case DT_MIPS_CONFLICTNO:
6884               name = ".conflict";
6885               elemsize = sizeof (Elf32_Conflict);
6886               goto set_elemno;
6887
6888             case DT_MIPS_LIBLISTNO:
6889               name = ".liblist";
6890               elemsize = sizeof (Elf32_Lib);
6891             set_elemno:
6892               s = bfd_get_section_by_name (output_bfd, name);
6893               if (s != NULL)
6894                 {
6895                   if (s->_cooked_size != 0)
6896                     dyn.d_un.d_val = s->_cooked_size / elemsize;
6897                   else
6898                     dyn.d_un.d_val = s->_raw_size / elemsize;
6899                 }
6900               else
6901                     dyn.d_un.d_val = 0;
6902
6903               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6904               break;
6905
6906             case DT_MIPS_TIME_STAMP:
6907               time ((time_t *) &dyn.d_un.d_val);
6908               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6909               break;
6910
6911             case DT_MIPS_ICHECKSUM:
6912               /* XXX FIXME: */
6913               break;
6914
6915             case DT_MIPS_IVERSION:
6916               /* XXX FIXME: */
6917               break;
6918
6919             case DT_MIPS_BASE_ADDRESS:
6920               s = output_bfd->sections;
6921               BFD_ASSERT (s != NULL);
6922               dyn.d_un.d_ptr = s->vma & ~(0xffff);
6923               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6924               break;
6925
6926             case DT_MIPS_LOCAL_GOTNO:
6927               dyn.d_un.d_val = g->local_gotno;
6928               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6929               break;
6930
6931             case DT_MIPS_SYMTABNO:
6932               name = ".dynsym";
6933               elemsize = sizeof (Elf32_External_Sym);
6934               s = bfd_get_section_by_name (output_bfd, name);
6935               BFD_ASSERT (s != NULL);
6936
6937               if (s->_cooked_size != 0)
6938                 dyn.d_un.d_val = s->_cooked_size / elemsize;
6939               else
6940                 dyn.d_un.d_val = s->_raw_size / elemsize;
6941               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6942               break;
6943
6944             case DT_MIPS_UNREFEXTNO:
6945               /* XXX FIXME: */
6946               dyn.d_un.d_val = SIZEOF_MIPS_DYNSYM_SECNAMES;
6947               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6948               break;
6949
6950             case DT_MIPS_GOTSYM:
6951               dyn.d_un.d_val = g->global_gotsym;
6952               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6953               break;
6954
6955             case DT_MIPS_HIPAGENO:
6956               dyn.d_un.d_val = g->local_gotno - MIPS_RESERVED_GOTNO;
6957               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6958               break;
6959
6960             case DT_MIPS_RLD_MAP:
6961               dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
6962               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6963               break;
6964
6965             }
6966         }
6967     }
6968
6969   /* The first entry of the global offset table will be filled at
6970      runtime. The second entry will be used by some runtime loaders.
6971      This isn't the case of Irix rld. */
6972   if (sgot != NULL && sgot->_raw_size > 0)
6973     {
6974       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
6975       bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, sgot->contents + 4);
6976     }
6977
6978   if (sgot != NULL)
6979     elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
6980
6981   {
6982     asection *sdynsym;
6983     asection *s;
6984     unsigned int i;
6985     bfd_vma last;
6986     Elf_Internal_Sym sym;
6987     long dindx;
6988     const char *name;
6989     const char * const * namep = mips_elf_dynsym_sec_names;
6990     Elf32_compact_rel cpt;
6991
6992     /* Set up the section symbols for the output sections. SGI sets
6993        the STT_NOTYPE attribute for these symbols.  Should we do so?  */
6994
6995     sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
6996     if (sdynsym != NULL)
6997       {
6998         if (SGI_COMPAT (output_bfd))
6999           {
7000             sym.st_size = 0;
7001             sym.st_name = 0;
7002             sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_NOTYPE);
7003             sym.st_other = 0;
7004
7005             i = 0;
7006             while ((name = *namep++) != NULL)
7007               {
7008                 s = bfd_get_section_by_name (output_bfd, name);
7009                 if (s != NULL)
7010                   {
7011                     sym.st_value = s->vma;
7012                     dindx = elf_section_data (s)->dynindx;
7013                     last = s->vma + s->_raw_size;
7014                   }
7015                 else
7016                   {
7017                     sym.st_value = last;
7018                     dindx++;
7019                   }
7020
7021                 sym.st_shndx = (i < MIPS_TEXT_DYNSYM_SECNO
7022                                 ? SHN_MIPS_TEXT
7023                                 : SHN_MIPS_DATA);
7024                 ++i;
7025                 sym.st_name =
7026                   mips_elf_hash_table (info)->dynsym_sec_strindex[dindx];
7027
7028                 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7029                                            (((Elf32_External_Sym *)
7030                                              sdynsym->contents)
7031                                             + dindx));
7032               }
7033
7034             /* Set the sh_info field of the output .dynsym section to
7035                the index of the first global symbol.  */
7036             elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7037               SIZEOF_MIPS_DYNSYM_SECNAMES;
7038           }
7039         else
7040           {
7041             sym.st_size = 0;
7042             sym.st_name = 0;
7043             sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7044             sym.st_other = 0;
7045
7046             for (s = output_bfd->sections; s != NULL; s = s->next)
7047               {
7048                 int indx;
7049
7050                 sym.st_value = s->vma;
7051
7052                 indx = elf_section_data (s)->this_idx;
7053                 BFD_ASSERT (indx > 0);
7054                 sym.st_shndx = indx;
7055
7056                 bfd_elf32_swap_symbol_out (output_bfd, &sym,
7057                                            (((Elf32_External_Sym *)
7058                                              sdynsym->contents)
7059                                             + elf_section_data (s)->dynindx));
7060               }
7061
7062             /* Set the sh_info field of the output .dynsym section to
7063                the index of the first global symbol.  */
7064             elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
7065               bfd_count_sections (output_bfd) + 1;
7066           }
7067       }
7068
7069     if (SGI_COMPAT (output_bfd))
7070       {
7071         /* Write .compact_rel section out.  */
7072         s = bfd_get_section_by_name (dynobj, ".compact_rel");
7073         if (s != NULL)
7074           {
7075             cpt.id1 = 1;
7076             cpt.num = s->reloc_count;
7077             cpt.id2 = 2;
7078             cpt.offset = (s->output_section->filepos
7079                           + sizeof (Elf32_External_compact_rel));
7080             cpt.reserved0 = 0;
7081             cpt.reserved1 = 0;
7082             bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
7083                                             ((Elf32_External_compact_rel *)
7084                                              s->contents));
7085
7086             /* Clean up a dummy stub function entry in .text.  */
7087             s = bfd_get_section_by_name (dynobj, ".stub");
7088             if (s != NULL)
7089               {
7090                 file_ptr dummy_offset;
7091
7092                 BFD_ASSERT (s->_raw_size >= MIPS_FUNCTION_STUB_SIZE);
7093                 dummy_offset = s->_raw_size - MIPS_FUNCTION_STUB_SIZE;
7094                 memset (s->contents + dummy_offset, 0,
7095                         MIPS_FUNCTION_STUB_SIZE);
7096               }
7097           }
7098       }
7099
7100     /* Clean up a first relocation in .rel.dyn.  */
7101     s = bfd_get_section_by_name (dynobj, ".rel.dyn");
7102     if (s != NULL && s->_raw_size > 0)
7103       memset (s->contents, 0, sizeof (Elf32_External_Rel));
7104   }
7105
7106   return true;
7107 }
7108 \f
7109 /* This is almost identical to bfd_generic_get_... except that some
7110    MIPS relocations need to be handled specially.  Sigh.  */
7111
7112 static bfd_byte *
7113 elf32_mips_get_relocated_section_contents (abfd, link_info, link_order, data,
7114                                            relocateable, symbols)
7115      bfd *abfd;
7116      struct bfd_link_info *link_info;
7117      struct bfd_link_order *link_order;
7118      bfd_byte *data;
7119      boolean relocateable;
7120      asymbol **symbols;
7121 {
7122   /* Get enough memory to hold the stuff */
7123   bfd *input_bfd = link_order->u.indirect.section->owner;
7124   asection *input_section = link_order->u.indirect.section;
7125
7126   long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
7127   arelent **reloc_vector = NULL;
7128   long reloc_count;
7129
7130   if (reloc_size < 0)
7131     goto error_return;
7132
7133   reloc_vector = (arelent **) bfd_malloc (reloc_size);
7134   if (reloc_vector == NULL && reloc_size != 0)
7135     goto error_return;
7136
7137   /* read in the section */
7138   if (!bfd_get_section_contents (input_bfd,
7139                                  input_section,
7140                                  (PTR) data,
7141                                  0,
7142                                  input_section->_raw_size))
7143     goto error_return;
7144
7145   /* We're not relaxing the section, so just copy the size info */
7146   input_section->_cooked_size = input_section->_raw_size;
7147   input_section->reloc_done = true;
7148
7149   reloc_count = bfd_canonicalize_reloc (input_bfd,
7150                                         input_section,
7151                                         reloc_vector,
7152                                         symbols);
7153   if (reloc_count < 0)
7154     goto error_return;
7155
7156   if (reloc_count > 0)
7157     {
7158       arelent **parent;
7159       /* for mips */
7160       int gp_found;
7161       bfd_vma gp = 0x12345678;  /* initialize just to shut gcc up */
7162
7163       {
7164         struct bfd_hash_entry *h;
7165         struct bfd_link_hash_entry *lh;
7166         /* Skip all this stuff if we aren't mixing formats.  */
7167         if (abfd && input_bfd
7168             && abfd->xvec == input_bfd->xvec)
7169           lh = 0;
7170         else
7171           {
7172             h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
7173             lh = (struct bfd_link_hash_entry *) h;
7174           }
7175       lookup:
7176         if (lh)
7177           {
7178             switch (lh->type)
7179               {
7180               case bfd_link_hash_undefined:
7181               case bfd_link_hash_undefweak:
7182               case bfd_link_hash_common:
7183                 gp_found = 0;
7184                 break;
7185               case bfd_link_hash_defined:
7186               case bfd_link_hash_defweak:
7187                 gp_found = 1;
7188                 gp = lh->u.def.value;
7189                 break;
7190               case bfd_link_hash_indirect:
7191               case bfd_link_hash_warning:
7192                 lh = lh->u.i.link;
7193                 /* @@FIXME  ignoring warning for now */
7194                 goto lookup;
7195               case bfd_link_hash_new:
7196               default:
7197                 abort ();
7198               }
7199           }
7200         else
7201           gp_found = 0;
7202       }
7203       /* end mips */
7204       for (parent = reloc_vector; *parent != (arelent *) NULL;
7205            parent++)
7206         {
7207           char *error_message = (char *) NULL;
7208           bfd_reloc_status_type r;
7209
7210           /* Specific to MIPS: Deal with relocation types that require
7211              knowing the gp of the output bfd.  */
7212           asymbol *sym = *(*parent)->sym_ptr_ptr;
7213           if (bfd_is_abs_section (sym->section) && abfd)
7214             {
7215               /* The special_function wouldn't get called anyways.  */
7216             }
7217           else if (!gp_found)
7218             {
7219               /* The gp isn't there; let the special function code
7220                  fall over on its own.  */
7221             }
7222           else if ((*parent)->howto->special_function
7223                    == _bfd_mips_elf_gprel16_reloc)
7224             {
7225               /* bypass special_function call */
7226               r = gprel16_with_gp (input_bfd, sym, *parent, input_section,
7227                                    relocateable, (PTR) data, gp);
7228               goto skip_bfd_perform_relocation;
7229             }
7230           /* end mips specific stuff */
7231
7232           r = bfd_perform_relocation (input_bfd,
7233                                       *parent,
7234                                       (PTR) data,
7235                                       input_section,
7236                                       relocateable ? abfd : (bfd *) NULL,
7237                                       &error_message);
7238         skip_bfd_perform_relocation:
7239
7240           if (relocateable)
7241             {
7242               asection *os = input_section->output_section;
7243
7244               /* A partial link, so keep the relocs */
7245               os->orelocation[os->reloc_count] = *parent;
7246               os->reloc_count++;
7247             }
7248
7249           if (r != bfd_reloc_ok)
7250             {
7251               switch (r)
7252                 {
7253                 case bfd_reloc_undefined:
7254                   if (!((*link_info->callbacks->undefined_symbol)
7255                         (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7256                          input_bfd, input_section, (*parent)->address)))
7257                     goto error_return;
7258                   break;
7259                 case bfd_reloc_dangerous:
7260                   BFD_ASSERT (error_message != (char *) NULL);
7261                   if (!((*link_info->callbacks->reloc_dangerous)
7262                         (link_info, error_message, input_bfd, input_section,
7263                          (*parent)->address)))
7264                     goto error_return;
7265                   break;
7266                 case bfd_reloc_overflow:
7267                   if (!((*link_info->callbacks->reloc_overflow)
7268                         (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
7269                          (*parent)->howto->name, (*parent)->addend,
7270                          input_bfd, input_section, (*parent)->address)))
7271                     goto error_return;
7272                   break;
7273                 case bfd_reloc_outofrange:
7274                 default:
7275                   abort ();
7276                   break;
7277                 }
7278
7279             }
7280         }
7281     }
7282   if (reloc_vector != NULL)
7283     free (reloc_vector);
7284   return data;
7285
7286 error_return:
7287   if (reloc_vector != NULL)
7288     free (reloc_vector);
7289   return NULL;
7290 }
7291 #define bfd_elf32_bfd_get_relocated_section_contents \
7292   elf32_mips_get_relocated_section_contents
7293 \f
7294 /* ECOFF swapping routines.  These are used when dealing with the
7295    .mdebug section, which is in the ECOFF debugging format.  */
7296 static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap =
7297 {
7298   /* Symbol table magic number.  */
7299   magicSym,
7300   /* Alignment of debugging information.  E.g., 4.  */
7301   4,
7302   /* Sizes of external symbolic information.  */
7303   sizeof (struct hdr_ext),
7304   sizeof (struct dnr_ext),
7305   sizeof (struct pdr_ext),
7306   sizeof (struct sym_ext),
7307   sizeof (struct opt_ext),
7308   sizeof (struct fdr_ext),
7309   sizeof (struct rfd_ext),
7310   sizeof (struct ext_ext),
7311   /* Functions to swap in external symbolic data.  */
7312   ecoff_swap_hdr_in,
7313   ecoff_swap_dnr_in,
7314   ecoff_swap_pdr_in,
7315   ecoff_swap_sym_in,
7316   ecoff_swap_opt_in,
7317   ecoff_swap_fdr_in,
7318   ecoff_swap_rfd_in,
7319   ecoff_swap_ext_in,
7320   _bfd_ecoff_swap_tir_in,
7321   _bfd_ecoff_swap_rndx_in,
7322   /* Functions to swap out external symbolic data.  */
7323   ecoff_swap_hdr_out,
7324   ecoff_swap_dnr_out,
7325   ecoff_swap_pdr_out,
7326   ecoff_swap_sym_out,
7327   ecoff_swap_opt_out,
7328   ecoff_swap_fdr_out,
7329   ecoff_swap_rfd_out,
7330   ecoff_swap_ext_out,
7331   _bfd_ecoff_swap_tir_out,
7332   _bfd_ecoff_swap_rndx_out,
7333   /* Function to read in symbolic data.  */
7334   _bfd_mips_elf_read_ecoff_info
7335 };
7336 \f
7337 #define TARGET_LITTLE_SYM               bfd_elf32_littlemips_vec
7338 #define TARGET_LITTLE_NAME              "elf32-littlemips"
7339 #define TARGET_BIG_SYM                  bfd_elf32_bigmips_vec
7340 #define TARGET_BIG_NAME                 "elf32-bigmips"
7341 #define ELF_ARCH                        bfd_arch_mips
7342 #define ELF_MACHINE_CODE                EM_MIPS
7343
7344 /* The SVR4 MIPS ABI says that this should be 0x10000, but Irix 5 uses
7345    a value of 0x1000, and we are compatible.  */
7346 #define ELF_MAXPAGESIZE                 0x1000
7347
7348 #define elf_backend_collect             true
7349 #define elf_backend_type_change_ok      true
7350 #define elf_info_to_howto               0
7351 #define elf_info_to_howto_rel           mips_info_to_howto_rel
7352 #define elf_backend_sym_is_global       mips_elf_sym_is_global
7353 #define elf_backend_object_p            mips_elf32_object_p
7354 #define elf_backend_section_from_shdr   mips_elf32_section_from_shdr
7355 #define elf_backend_fake_sections       _bfd_mips_elf_fake_sections
7356 #define elf_backend_section_from_bfd_section \
7357                                         _bfd_mips_elf_section_from_bfd_section
7358 #define elf_backend_section_processing  mips_elf32_section_processing
7359 #define elf_backend_symbol_processing   _bfd_mips_elf_symbol_processing
7360 #define elf_backend_additional_program_headers \
7361                                         mips_elf_additional_program_headers
7362 #define elf_backend_modify_segment_map  mips_elf_modify_segment_map
7363 #define elf_backend_final_write_processing \
7364                                         _bfd_mips_elf_final_write_processing
7365 #define elf_backend_ecoff_debug_swap    &mips_elf32_ecoff_debug_swap
7366
7367 #define bfd_elf32_bfd_is_local_label_name \
7368                                         mips_elf_is_local_label_name
7369 #define bfd_elf32_find_nearest_line     _bfd_mips_elf_find_nearest_line
7370 #define bfd_elf32_set_section_contents  _bfd_mips_elf_set_section_contents
7371 #define bfd_elf32_bfd_link_hash_table_create \
7372                                         mips_elf_link_hash_table_create
7373 #define bfd_elf32_bfd_final_link        mips_elf_final_link
7374 #define bfd_elf32_bfd_copy_private_bfd_data \
7375                                         _bfd_mips_elf_copy_private_bfd_data
7376 #define bfd_elf32_bfd_merge_private_bfd_data \
7377                                         _bfd_mips_elf_merge_private_bfd_data
7378 #define bfd_elf32_bfd_set_private_flags _bfd_mips_elf_set_private_flags
7379 #define elf_backend_add_symbol_hook     mips_elf_add_symbol_hook
7380 #define elf_backend_create_dynamic_sections \
7381                                         mips_elf_create_dynamic_sections
7382 #define elf_backend_check_relocs        mips_elf_check_relocs
7383 #define elf_backend_adjust_dynamic_symbol \
7384                                         mips_elf_adjust_dynamic_symbol
7385 #define elf_backend_always_size_sections \
7386                                         mips_elf_always_size_sections
7387 #define elf_backend_size_dynamic_sections \
7388                                         mips_elf_size_dynamic_sections
7389 #define elf_backend_relocate_section    mips_elf_relocate_section
7390 #define elf_backend_link_output_symbol_hook \
7391                                         mips_elf_link_output_symbol_hook
7392 #define elf_backend_finish_dynamic_symbol \
7393                                         mips_elf_finish_dynamic_symbol
7394 #define elf_backend_finish_dynamic_sections \
7395                                         mips_elf_finish_dynamic_sections
7396
7397 #include "elf32-target.h"
This page took 0.439703 seconds and 4 git commands to generate.