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