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