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