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