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