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