OSDN Git Service

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