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