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