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