OSDN Git Service

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