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