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