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