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