2011-06-09 Tristan Gingold <gingold@adacore.com>
[binutils.git] / bfd / elfxx-mips.c
blobd5f677fad5fb0edee592bf986a8fdc8b84eaf020
1 /* MIPS-specific support for ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Most of the information added by Ian Lance Taylor, Cygnus Support,
7 <ian@cygnus.com>.
8 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
9 <mark@codesourcery.com>
10 Traditional MIPS targets support added by Koundinya.K, Dansk Data
11 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
13 This file is part of BFD, the Binary File Descriptor library.
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
28 MA 02110-1301, USA. */
31 /* This file handles functionality common to the different MIPS ABI's. */
33 #include "sysdep.h"
34 #include "bfd.h"
35 #include "libbfd.h"
36 #include "libiberty.h"
37 #include "elf-bfd.h"
38 #include "elfxx-mips.h"
39 #include "elf/mips.h"
40 #include "elf-vxworks.h"
42 /* Get the ECOFF swapping routines. */
43 #include "coff/sym.h"
44 #include "coff/symconst.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
48 #include "hashtab.h"
50 /* This structure is used to hold information about one GOT entry.
51 There are three types of entry:
53 (1) absolute addresses
54 (abfd == NULL)
55 (2) SYMBOL + OFFSET addresses, where SYMBOL is local to an input bfd
56 (abfd != NULL, symndx >= 0)
57 (3) SYMBOL addresses, where SYMBOL is not local to an input bfd
58 (abfd != NULL, symndx == -1)
60 Type (3) entries are treated differently for different types of GOT.
61 In the "master" GOT -- i.e. the one that describes every GOT
62 reference needed in the link -- the mips_got_entry is keyed on both
63 the symbol and the input bfd that references it. If it turns out
64 that we need multiple GOTs, we can then use this information to
65 create separate GOTs for each input bfd.
67 However, we want each of these separate GOTs to have at most one
68 entry for a given symbol, so their type (3) entries are keyed only
69 on the symbol. The input bfd given by the "abfd" field is somewhat
70 arbitrary in this case.
72 This means that when there are multiple GOTs, each GOT has a unique
73 mips_got_entry for every symbol within it. We can therefore use the
74 mips_got_entry fields (tls_type and gotidx) to track the symbol's
75 GOT index.
77 However, if it turns out that we need only a single GOT, we continue
78 to use the master GOT to describe it. There may therefore be several
79 mips_got_entries for the same symbol, each with a different input bfd.
80 We want to make sure that each symbol gets a unique GOT entry, so when
81 there's a single GOT, we use the symbol's hash entry, not the
82 mips_got_entry fields, to track a symbol's GOT index. */
83 struct mips_got_entry
85 /* The input bfd in which the symbol is defined. */
86 bfd *abfd;
87 /* The index of the symbol, as stored in the relocation r_info, if
88 we have a local symbol; -1 otherwise. */
89 long symndx;
90 union
92 /* If abfd == NULL, an address that must be stored in the got. */
93 bfd_vma address;
94 /* If abfd != NULL && symndx != -1, the addend of the relocation
95 that should be added to the symbol value. */
96 bfd_vma addend;
97 /* If abfd != NULL && symndx == -1, the hash table entry
98 corresponding to symbol in the GOT. The symbol's entry
99 is in the local area if h->global_got_area is GGA_NONE,
100 otherwise it is in the global area. */
101 struct mips_elf_link_hash_entry *h;
102 } d;
104 /* The TLS types included in this GOT entry (specifically, GD and
105 IE). The GD and IE flags can be added as we encounter new
106 relocations. LDM can also be set; it will always be alone, not
107 combined with any GD or IE flags. An LDM GOT entry will be
108 a local symbol entry with r_symndx == 0. */
109 unsigned char tls_type;
111 /* The offset from the beginning of the .got section to the entry
112 corresponding to this symbol+addend. If it's a global symbol
113 whose offset is yet to be decided, it's going to be -1. */
114 long gotidx;
117 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
118 The structures form a non-overlapping list that is sorted by increasing
119 MIN_ADDEND. */
120 struct mips_got_page_range
122 struct mips_got_page_range *next;
123 bfd_signed_vma min_addend;
124 bfd_signed_vma max_addend;
127 /* This structure describes the range of addends that are applied to page
128 relocations against a given symbol. */
129 struct mips_got_page_entry
131 /* The input bfd in which the symbol is defined. */
132 bfd *abfd;
133 /* The index of the symbol, as stored in the relocation r_info. */
134 long symndx;
135 /* The ranges for this page entry. */
136 struct mips_got_page_range *ranges;
137 /* The maximum number of page entries needed for RANGES. */
138 bfd_vma num_pages;
141 /* This structure is used to hold .got information when linking. */
143 struct mips_got_info
145 /* The global symbol in the GOT with the lowest index in the dynamic
146 symbol table. */
147 struct elf_link_hash_entry *global_gotsym;
148 /* The number of global .got entries. */
149 unsigned int global_gotno;
150 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
151 unsigned int reloc_only_gotno;
152 /* The number of .got slots used for TLS. */
153 unsigned int tls_gotno;
154 /* The first unused TLS .got entry. Used only during
155 mips_elf_initialize_tls_index. */
156 unsigned int tls_assigned_gotno;
157 /* The number of local .got entries, eventually including page entries. */
158 unsigned int local_gotno;
159 /* The maximum number of page entries needed. */
160 unsigned int page_gotno;
161 /* The number of local .got entries we have used. */
162 unsigned int assigned_gotno;
163 /* A hash table holding members of the got. */
164 struct htab *got_entries;
165 /* A hash table of mips_got_page_entry structures. */
166 struct htab *got_page_entries;
167 /* A hash table mapping input bfds to other mips_got_info. NULL
168 unless multi-got was necessary. */
169 struct htab *bfd2got;
170 /* In multi-got links, a pointer to the next got (err, rather, most
171 of the time, it points to the previous got). */
172 struct mips_got_info *next;
173 /* This is the GOT index of the TLS LDM entry for the GOT, MINUS_ONE
174 for none, or MINUS_TWO for not yet assigned. This is needed
175 because a single-GOT link may have multiple hash table entries
176 for the LDM. It does not get initialized in multi-GOT mode. */
177 bfd_vma tls_ldm_offset;
180 /* Map an input bfd to a got in a multi-got link. */
182 struct mips_elf_bfd2got_hash
184 bfd *bfd;
185 struct mips_got_info *g;
188 /* Structure passed when traversing the bfd2got hash table, used to
189 create and merge bfd's gots. */
191 struct mips_elf_got_per_bfd_arg
193 /* A hashtable that maps bfds to gots. */
194 htab_t bfd2got;
195 /* The output bfd. */
196 bfd *obfd;
197 /* The link information. */
198 struct bfd_link_info *info;
199 /* A pointer to the primary got, i.e., the one that's going to get
200 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
201 DT_MIPS_GOTSYM. */
202 struct mips_got_info *primary;
203 /* A non-primary got we're trying to merge with other input bfd's
204 gots. */
205 struct mips_got_info *current;
206 /* The maximum number of got entries that can be addressed with a
207 16-bit offset. */
208 unsigned int max_count;
209 /* The maximum number of page entries needed by each got. */
210 unsigned int max_pages;
211 /* The total number of global entries which will live in the
212 primary got and be automatically relocated. This includes
213 those not referenced by the primary GOT but included in
214 the "master" GOT. */
215 unsigned int global_count;
218 /* Another structure used to pass arguments for got entries traversal. */
220 struct mips_elf_set_global_got_offset_arg
222 struct mips_got_info *g;
223 int value;
224 unsigned int needed_relocs;
225 struct bfd_link_info *info;
228 /* A structure used to count TLS relocations or GOT entries, for GOT
229 entry or ELF symbol table traversal. */
231 struct mips_elf_count_tls_arg
233 struct bfd_link_info *info;
234 unsigned int needed;
237 struct _mips_elf_section_data
239 struct bfd_elf_section_data elf;
240 union
242 bfd_byte *tdata;
243 } u;
246 #define mips_elf_section_data(sec) \
247 ((struct _mips_elf_section_data *) elf_section_data (sec))
249 #define is_mips_elf(bfd) \
250 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
251 && elf_tdata (bfd) != NULL \
252 && elf_object_id (bfd) == MIPS_ELF_DATA)
254 /* The ABI says that every symbol used by dynamic relocations must have
255 a global GOT entry. Among other things, this provides the dynamic
256 linker with a free, directly-indexed cache. The GOT can therefore
257 contain symbols that are not referenced by GOT relocations themselves
258 (in other words, it may have symbols that are not referenced by things
259 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
261 GOT relocations are less likely to overflow if we put the associated
262 GOT entries towards the beginning. We therefore divide the global
263 GOT entries into two areas: "normal" and "reloc-only". Entries in
264 the first area can be used for both dynamic relocations and GP-relative
265 accesses, while those in the "reloc-only" area are for dynamic
266 relocations only.
268 These GGA_* ("Global GOT Area") values are organised so that lower
269 values are more general than higher values. Also, non-GGA_NONE
270 values are ordered by the position of the area in the GOT. */
271 #define GGA_NORMAL 0
272 #define GGA_RELOC_ONLY 1
273 #define GGA_NONE 2
275 /* Information about a non-PIC interface to a PIC function. There are
276 two ways of creating these interfaces. The first is to add:
278 lui $25,%hi(func)
279 addiu $25,$25,%lo(func)
281 immediately before a PIC function "func". The second is to add:
283 lui $25,%hi(func)
284 j func
285 addiu $25,$25,%lo(func)
287 to a separate trampoline section.
289 Stubs of the first kind go in a new section immediately before the
290 target function. Stubs of the second kind go in a single section
291 pointed to by the hash table's "strampoline" field. */
292 struct mips_elf_la25_stub {
293 /* The generated section that contains this stub. */
294 asection *stub_section;
296 /* The offset of the stub from the start of STUB_SECTION. */
297 bfd_vma offset;
299 /* One symbol for the original function. Its location is available
300 in H->root.root.u.def. */
301 struct mips_elf_link_hash_entry *h;
304 /* Macros for populating a mips_elf_la25_stub. */
306 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
307 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
308 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
310 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
311 the dynamic symbols. */
313 struct mips_elf_hash_sort_data
315 /* The symbol in the global GOT with the lowest dynamic symbol table
316 index. */
317 struct elf_link_hash_entry *low;
318 /* The least dynamic symbol table index corresponding to a non-TLS
319 symbol with a GOT entry. */
320 long min_got_dynindx;
321 /* The greatest dynamic symbol table index corresponding to a symbol
322 with a GOT entry that is not referenced (e.g., a dynamic symbol
323 with dynamic relocations pointing to it from non-primary GOTs). */
324 long max_unref_got_dynindx;
325 /* The greatest dynamic symbol table index not corresponding to a
326 symbol without a GOT entry. */
327 long max_non_got_dynindx;
330 /* The MIPS ELF linker needs additional information for each symbol in
331 the global hash table. */
333 struct mips_elf_link_hash_entry
335 struct elf_link_hash_entry root;
337 /* External symbol information. */
338 EXTR esym;
340 /* The la25 stub we have created for ths symbol, if any. */
341 struct mips_elf_la25_stub *la25_stub;
343 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
344 this symbol. */
345 unsigned int possibly_dynamic_relocs;
347 /* If there is a stub that 32 bit functions should use to call this
348 16 bit function, this points to the section containing the stub. */
349 asection *fn_stub;
351 /* If there is a stub that 16 bit functions should use to call this
352 32 bit function, this points to the section containing the stub. */
353 asection *call_stub;
355 /* This is like the call_stub field, but it is used if the function
356 being called returns a floating point value. */
357 asection *call_fp_stub;
359 #define GOT_NORMAL 0
360 #define GOT_TLS_GD 1
361 #define GOT_TLS_LDM 2
362 #define GOT_TLS_IE 4
363 #define GOT_TLS_OFFSET_DONE 0x40
364 #define GOT_TLS_DONE 0x80
365 unsigned char tls_type;
367 /* This is only used in single-GOT mode; in multi-GOT mode there
368 is one mips_got_entry per GOT entry, so the offset is stored
369 there. In single-GOT mode there may be many mips_got_entry
370 structures all referring to the same GOT slot. It might be
371 possible to use root.got.offset instead, but that field is
372 overloaded already. */
373 bfd_vma tls_got_offset;
375 /* The highest GGA_* value that satisfies all references to this symbol. */
376 unsigned int global_got_area : 2;
378 /* True if all GOT relocations against this symbol are for calls. This is
379 a looser condition than no_fn_stub below, because there may be other
380 non-call non-GOT relocations against the symbol. */
381 unsigned int got_only_for_calls : 1;
383 /* True if one of the relocations described by possibly_dynamic_relocs
384 is against a readonly section. */
385 unsigned int readonly_reloc : 1;
387 /* True if there is a relocation against this symbol that must be
388 resolved by the static linker (in other words, if the relocation
389 cannot possibly be made dynamic). */
390 unsigned int has_static_relocs : 1;
392 /* True if we must not create a .MIPS.stubs entry for this symbol.
393 This is set, for example, if there are relocations related to
394 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
395 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
396 unsigned int no_fn_stub : 1;
398 /* Whether we need the fn_stub; this is true if this symbol appears
399 in any relocs other than a 16 bit call. */
400 unsigned int need_fn_stub : 1;
402 /* True if this symbol is referenced by branch relocations from
403 any non-PIC input file. This is used to determine whether an
404 la25 stub is required. */
405 unsigned int has_nonpic_branches : 1;
407 /* Does this symbol need a traditional MIPS lazy-binding stub
408 (as opposed to a PLT entry)? */
409 unsigned int needs_lazy_stub : 1;
412 /* MIPS ELF linker hash table. */
414 struct mips_elf_link_hash_table
416 struct elf_link_hash_table root;
417 #if 0
418 /* We no longer use this. */
419 /* String section indices for the dynamic section symbols. */
420 bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
421 #endif
423 /* The number of .rtproc entries. */
424 bfd_size_type procedure_count;
426 /* The size of the .compact_rel section (if SGI_COMPAT). */
427 bfd_size_type compact_rel_size;
429 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
430 entry is set to the address of __rld_obj_head as in IRIX5. */
431 bfd_boolean use_rld_obj_head;
433 /* This is the value of the __rld_map or __rld_obj_head symbol. */
434 bfd_vma rld_value;
436 /* This is set if we see any mips16 stub sections. */
437 bfd_boolean mips16_stubs_seen;
439 /* True if we can generate copy relocs and PLTs. */
440 bfd_boolean use_plts_and_copy_relocs;
442 /* True if we're generating code for VxWorks. */
443 bfd_boolean is_vxworks;
445 /* True if we already reported the small-data section overflow. */
446 bfd_boolean small_data_overflow_reported;
448 /* Shortcuts to some dynamic sections, or NULL if they are not
449 being used. */
450 asection *srelbss;
451 asection *sdynbss;
452 asection *srelplt;
453 asection *srelplt2;
454 asection *sgotplt;
455 asection *splt;
456 asection *sstubs;
457 asection *sgot;
459 /* The master GOT information. */
460 struct mips_got_info *got_info;
462 /* The size of the PLT header in bytes. */
463 bfd_vma plt_header_size;
465 /* The size of a PLT entry in bytes. */
466 bfd_vma plt_entry_size;
468 /* The number of functions that need a lazy-binding stub. */
469 bfd_vma lazy_stub_count;
471 /* The size of a function stub entry in bytes. */
472 bfd_vma function_stub_size;
474 /* The number of reserved entries at the beginning of the GOT. */
475 unsigned int reserved_gotno;
477 /* The section used for mips_elf_la25_stub trampolines.
478 See the comment above that structure for details. */
479 asection *strampoline;
481 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
482 pairs. */
483 htab_t la25_stubs;
485 /* A function FN (NAME, IS, OS) that creates a new input section
486 called NAME and links it to output section OS. If IS is nonnull,
487 the new section should go immediately before it, otherwise it
488 should go at the (current) beginning of OS.
490 The function returns the new section on success, otherwise it
491 returns null. */
492 asection *(*add_stub_section) (const char *, asection *, asection *);
495 /* Get the MIPS ELF linker hash table from a link_info structure. */
497 #define mips_elf_hash_table(p) \
498 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
499 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
501 /* A structure used to communicate with htab_traverse callbacks. */
502 struct mips_htab_traverse_info
504 /* The usual link-wide information. */
505 struct bfd_link_info *info;
506 bfd *output_bfd;
508 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
509 bfd_boolean error;
512 #define TLS_RELOC_P(r_type) \
513 (r_type == R_MIPS_TLS_DTPMOD32 \
514 || r_type == R_MIPS_TLS_DTPMOD64 \
515 || r_type == R_MIPS_TLS_DTPREL32 \
516 || r_type == R_MIPS_TLS_DTPREL64 \
517 || r_type == R_MIPS_TLS_GD \
518 || r_type == R_MIPS_TLS_LDM \
519 || r_type == R_MIPS_TLS_DTPREL_HI16 \
520 || r_type == R_MIPS_TLS_DTPREL_LO16 \
521 || r_type == R_MIPS_TLS_GOTTPREL \
522 || r_type == R_MIPS_TLS_TPREL32 \
523 || r_type == R_MIPS_TLS_TPREL64 \
524 || r_type == R_MIPS_TLS_TPREL_HI16 \
525 || r_type == R_MIPS_TLS_TPREL_LO16)
527 /* Structure used to pass information to mips_elf_output_extsym. */
529 struct extsym_info
531 bfd *abfd;
532 struct bfd_link_info *info;
533 struct ecoff_debug_info *debug;
534 const struct ecoff_debug_swap *swap;
535 bfd_boolean failed;
538 /* The names of the runtime procedure table symbols used on IRIX5. */
540 static const char * const mips_elf_dynsym_rtproc_names[] =
542 "_procedure_table",
543 "_procedure_string_table",
544 "_procedure_table_size",
545 NULL
548 /* These structures are used to generate the .compact_rel section on
549 IRIX5. */
551 typedef struct
553 unsigned long id1; /* Always one? */
554 unsigned long num; /* Number of compact relocation entries. */
555 unsigned long id2; /* Always two? */
556 unsigned long offset; /* The file offset of the first relocation. */
557 unsigned long reserved0; /* Zero? */
558 unsigned long reserved1; /* Zero? */
559 } Elf32_compact_rel;
561 typedef struct
563 bfd_byte id1[4];
564 bfd_byte num[4];
565 bfd_byte id2[4];
566 bfd_byte offset[4];
567 bfd_byte reserved0[4];
568 bfd_byte reserved1[4];
569 } Elf32_External_compact_rel;
571 typedef struct
573 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
574 unsigned int rtype : 4; /* Relocation types. See below. */
575 unsigned int dist2to : 8;
576 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
577 unsigned long konst; /* KONST field. See below. */
578 unsigned long vaddr; /* VADDR to be relocated. */
579 } Elf32_crinfo;
581 typedef struct
583 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
584 unsigned int rtype : 4; /* Relocation types. See below. */
585 unsigned int dist2to : 8;
586 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
587 unsigned long konst; /* KONST field. See below. */
588 } Elf32_crinfo2;
590 typedef struct
592 bfd_byte info[4];
593 bfd_byte konst[4];
594 bfd_byte vaddr[4];
595 } Elf32_External_crinfo;
597 typedef struct
599 bfd_byte info[4];
600 bfd_byte konst[4];
601 } Elf32_External_crinfo2;
603 /* These are the constants used to swap the bitfields in a crinfo. */
605 #define CRINFO_CTYPE (0x1)
606 #define CRINFO_CTYPE_SH (31)
607 #define CRINFO_RTYPE (0xf)
608 #define CRINFO_RTYPE_SH (27)
609 #define CRINFO_DIST2TO (0xff)
610 #define CRINFO_DIST2TO_SH (19)
611 #define CRINFO_RELVADDR (0x7ffff)
612 #define CRINFO_RELVADDR_SH (0)
614 /* A compact relocation info has long (3 words) or short (2 words)
615 formats. A short format doesn't have VADDR field and relvaddr
616 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
617 #define CRF_MIPS_LONG 1
618 #define CRF_MIPS_SHORT 0
620 /* There are 4 types of compact relocation at least. The value KONST
621 has different meaning for each type:
623 (type) (konst)
624 CT_MIPS_REL32 Address in data
625 CT_MIPS_WORD Address in word (XXX)
626 CT_MIPS_GPHI_LO GP - vaddr
627 CT_MIPS_JMPAD Address to jump
630 #define CRT_MIPS_REL32 0xa
631 #define CRT_MIPS_WORD 0xb
632 #define CRT_MIPS_GPHI_LO 0xc
633 #define CRT_MIPS_JMPAD 0xd
635 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
636 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
637 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
638 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
640 /* The structure of the runtime procedure descriptor created by the
641 loader for use by the static exception system. */
643 typedef struct runtime_pdr {
644 bfd_vma adr; /* Memory address of start of procedure. */
645 long regmask; /* Save register mask. */
646 long regoffset; /* Save register offset. */
647 long fregmask; /* Save floating point register mask. */
648 long fregoffset; /* Save floating point register offset. */
649 long frameoffset; /* Frame size. */
650 short framereg; /* Frame pointer register. */
651 short pcreg; /* Offset or reg of return pc. */
652 long irpss; /* Index into the runtime string table. */
653 long reserved;
654 struct exception_info *exception_info;/* Pointer to exception array. */
655 } RPDR, *pRPDR;
656 #define cbRPDR sizeof (RPDR)
657 #define rpdNil ((pRPDR) 0)
659 static struct mips_got_entry *mips_elf_create_local_got_entry
660 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
661 struct mips_elf_link_hash_entry *, int);
662 static bfd_boolean mips_elf_sort_hash_table_f
663 (struct mips_elf_link_hash_entry *, void *);
664 static bfd_vma mips_elf_high
665 (bfd_vma);
666 static bfd_boolean mips_elf_create_dynamic_relocation
667 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
668 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
669 bfd_vma *, asection *);
670 static hashval_t mips_elf_got_entry_hash
671 (const void *);
672 static bfd_vma mips_elf_adjust_gp
673 (bfd *, struct mips_got_info *, bfd *);
674 static struct mips_got_info *mips_elf_got_for_ibfd
675 (struct mips_got_info *, bfd *);
677 /* This will be used when we sort the dynamic relocation records. */
678 static bfd *reldyn_sorting_bfd;
680 /* True if ABFD is for CPUs with load interlocking that include
681 non-MIPS1 CPUs and R3900. */
682 #define LOAD_INTERLOCKS_P(abfd) \
683 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
684 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
686 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
687 This should be safe for all architectures. We enable this predicate
688 for RM9000 for now. */
689 #define JAL_TO_BAL_P(abfd) \
690 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
692 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
693 This should be safe for all architectures. We enable this predicate for
694 all CPUs. */
695 #define JALR_TO_BAL_P(abfd) 1
697 /* True if ABFD is for CPUs that are faster if JR is converted to B.
698 This should be safe for all architectures. We enable this predicate for
699 all CPUs. */
700 #define JR_TO_B_P(abfd) 1
702 /* True if ABFD is a PIC object. */
703 #define PIC_OBJECT_P(abfd) \
704 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
706 /* Nonzero if ABFD is using the N32 ABI. */
707 #define ABI_N32_P(abfd) \
708 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
710 /* Nonzero if ABFD is using the N64 ABI. */
711 #define ABI_64_P(abfd) \
712 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
714 /* Nonzero if ABFD is using NewABI conventions. */
715 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
717 /* The IRIX compatibility level we are striving for. */
718 #define IRIX_COMPAT(abfd) \
719 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
721 /* Whether we are trying to be compatible with IRIX at all. */
722 #define SGI_COMPAT(abfd) \
723 (IRIX_COMPAT (abfd) != ict_none)
725 /* The name of the options section. */
726 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
727 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
729 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
730 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
731 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
732 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
734 /* Whether the section is readonly. */
735 #define MIPS_ELF_READONLY_SECTION(sec) \
736 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
737 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
739 /* The name of the stub section. */
740 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
742 /* The size of an external REL relocation. */
743 #define MIPS_ELF_REL_SIZE(abfd) \
744 (get_elf_backend_data (abfd)->s->sizeof_rel)
746 /* The size of an external RELA relocation. */
747 #define MIPS_ELF_RELA_SIZE(abfd) \
748 (get_elf_backend_data (abfd)->s->sizeof_rela)
750 /* The size of an external dynamic table entry. */
751 #define MIPS_ELF_DYN_SIZE(abfd) \
752 (get_elf_backend_data (abfd)->s->sizeof_dyn)
754 /* The size of a GOT entry. */
755 #define MIPS_ELF_GOT_SIZE(abfd) \
756 (get_elf_backend_data (abfd)->s->arch_size / 8)
758 /* The size of a symbol-table entry. */
759 #define MIPS_ELF_SYM_SIZE(abfd) \
760 (get_elf_backend_data (abfd)->s->sizeof_sym)
762 /* The default alignment for sections, as a power of two. */
763 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
764 (get_elf_backend_data (abfd)->s->log_file_align)
766 /* Get word-sized data. */
767 #define MIPS_ELF_GET_WORD(abfd, ptr) \
768 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
770 /* Put out word-sized data. */
771 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
772 (ABI_64_P (abfd) \
773 ? bfd_put_64 (abfd, val, ptr) \
774 : bfd_put_32 (abfd, val, ptr))
776 /* The opcode for word-sized loads (LW or LD). */
777 #define MIPS_ELF_LOAD_WORD(abfd) \
778 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
780 /* Add a dynamic symbol table-entry. */
781 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
782 _bfd_elf_add_dynamic_entry (info, tag, val)
784 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
785 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
787 /* The name of the dynamic relocation section. */
788 #define MIPS_ELF_REL_DYN_NAME(INFO) \
789 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
791 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
792 from smaller values. Start with zero, widen, *then* decrement. */
793 #define MINUS_ONE (((bfd_vma)0) - 1)
794 #define MINUS_TWO (((bfd_vma)0) - 2)
796 /* The value to write into got[1] for SVR4 targets, to identify it is
797 a GNU object. The dynamic linker can then use got[1] to store the
798 module pointer. */
799 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
800 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
802 /* The offset of $gp from the beginning of the .got section. */
803 #define ELF_MIPS_GP_OFFSET(INFO) \
804 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
806 /* The maximum size of the GOT for it to be addressable using 16-bit
807 offsets from $gp. */
808 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
810 /* Instructions which appear in a stub. */
811 #define STUB_LW(abfd) \
812 ((ABI_64_P (abfd) \
813 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
814 : 0x8f998010)) /* lw t9,0x8010(gp) */
815 #define STUB_MOVE(abfd) \
816 ((ABI_64_P (abfd) \
817 ? 0x03e0782d /* daddu t7,ra */ \
818 : 0x03e07821)) /* addu t7,ra */
819 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
820 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
821 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
822 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
823 #define STUB_LI16S(abfd, VAL) \
824 ((ABI_64_P (abfd) \
825 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
826 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
828 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
829 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
831 /* The name of the dynamic interpreter. This is put in the .interp
832 section. */
834 #define ELF_DYNAMIC_INTERPRETER(abfd) \
835 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
836 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
837 : "/usr/lib/libc.so.1")
839 #ifdef BFD64
840 #define MNAME(bfd,pre,pos) \
841 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
842 #define ELF_R_SYM(bfd, i) \
843 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
844 #define ELF_R_TYPE(bfd, i) \
845 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
846 #define ELF_R_INFO(bfd, s, t) \
847 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
848 #else
849 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
850 #define ELF_R_SYM(bfd, i) \
851 (ELF32_R_SYM (i))
852 #define ELF_R_TYPE(bfd, i) \
853 (ELF32_R_TYPE (i))
854 #define ELF_R_INFO(bfd, s, t) \
855 (ELF32_R_INFO (s, t))
856 #endif
858 /* The mips16 compiler uses a couple of special sections to handle
859 floating point arguments.
861 Section names that look like .mips16.fn.FNNAME contain stubs that
862 copy floating point arguments from the fp regs to the gp regs and
863 then jump to FNNAME. If any 32 bit function calls FNNAME, the
864 call should be redirected to the stub instead. If no 32 bit
865 function calls FNNAME, the stub should be discarded. We need to
866 consider any reference to the function, not just a call, because
867 if the address of the function is taken we will need the stub,
868 since the address might be passed to a 32 bit function.
870 Section names that look like .mips16.call.FNNAME contain stubs
871 that copy floating point arguments from the gp regs to the fp
872 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
873 then any 16 bit function that calls FNNAME should be redirected
874 to the stub instead. If FNNAME is not a 32 bit function, the
875 stub should be discarded.
877 .mips16.call.fp.FNNAME sections are similar, but contain stubs
878 which call FNNAME and then copy the return value from the fp regs
879 to the gp regs. These stubs store the return value in $18 while
880 calling FNNAME; any function which might call one of these stubs
881 must arrange to save $18 around the call. (This case is not
882 needed for 32 bit functions that call 16 bit functions, because
883 16 bit functions always return floating point values in both
884 $f0/$f1 and $2/$3.)
886 Note that in all cases FNNAME might be defined statically.
887 Therefore, FNNAME is not used literally. Instead, the relocation
888 information will indicate which symbol the section is for.
890 We record any stubs that we find in the symbol table. */
892 #define FN_STUB ".mips16.fn."
893 #define CALL_STUB ".mips16.call."
894 #define CALL_FP_STUB ".mips16.call.fp."
896 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
897 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
898 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
900 /* The format of the first PLT entry in an O32 executable. */
901 static const bfd_vma mips_o32_exec_plt0_entry[] =
903 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
904 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
905 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
906 0x031cc023, /* subu $24, $24, $28 */
907 0x03e07821, /* move $15, $31 */
908 0x0018c082, /* srl $24, $24, 2 */
909 0x0320f809, /* jalr $25 */
910 0x2718fffe /* subu $24, $24, 2 */
913 /* The format of the first PLT entry in an N32 executable. Different
914 because gp ($28) is not available; we use t2 ($14) instead. */
915 static const bfd_vma mips_n32_exec_plt0_entry[] =
917 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
918 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
919 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
920 0x030ec023, /* subu $24, $24, $14 */
921 0x03e07821, /* move $15, $31 */
922 0x0018c082, /* srl $24, $24, 2 */
923 0x0320f809, /* jalr $25 */
924 0x2718fffe /* subu $24, $24, 2 */
927 /* The format of the first PLT entry in an N64 executable. Different
928 from N32 because of the increased size of GOT entries. */
929 static const bfd_vma mips_n64_exec_plt0_entry[] =
931 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
932 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
933 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
934 0x030ec023, /* subu $24, $24, $14 */
935 0x03e07821, /* move $15, $31 */
936 0x0018c0c2, /* srl $24, $24, 3 */
937 0x0320f809, /* jalr $25 */
938 0x2718fffe /* subu $24, $24, 2 */
941 /* The format of subsequent PLT entries. */
942 static const bfd_vma mips_exec_plt_entry[] =
944 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
945 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
946 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
947 0x03200008 /* jr $25 */
950 /* The format of the first PLT entry in a VxWorks executable. */
951 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
953 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
954 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
955 0x8f390008, /* lw t9, 8(t9) */
956 0x00000000, /* nop */
957 0x03200008, /* jr t9 */
958 0x00000000 /* nop */
961 /* The format of subsequent PLT entries. */
962 static const bfd_vma mips_vxworks_exec_plt_entry[] =
964 0x10000000, /* b .PLT_resolver */
965 0x24180000, /* li t8, <pltindex> */
966 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
967 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
968 0x8f390000, /* lw t9, 0(t9) */
969 0x00000000, /* nop */
970 0x03200008, /* jr t9 */
971 0x00000000 /* nop */
974 /* The format of the first PLT entry in a VxWorks shared object. */
975 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
977 0x8f990008, /* lw t9, 8(gp) */
978 0x00000000, /* nop */
979 0x03200008, /* jr t9 */
980 0x00000000, /* nop */
981 0x00000000, /* nop */
982 0x00000000 /* nop */
985 /* The format of subsequent PLT entries. */
986 static const bfd_vma mips_vxworks_shared_plt_entry[] =
988 0x10000000, /* b .PLT_resolver */
989 0x24180000 /* li t8, <pltindex> */
992 /* Look up an entry in a MIPS ELF linker hash table. */
994 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
995 ((struct mips_elf_link_hash_entry *) \
996 elf_link_hash_lookup (&(table)->root, (string), (create), \
997 (copy), (follow)))
999 /* Traverse a MIPS ELF linker hash table. */
1001 #define mips_elf_link_hash_traverse(table, func, info) \
1002 (elf_link_hash_traverse \
1003 (&(table)->root, \
1004 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1005 (info)))
1007 /* Find the base offsets for thread-local storage in this object,
1008 for GD/LD and IE/LE respectively. */
1010 #define TP_OFFSET 0x7000
1011 #define DTP_OFFSET 0x8000
1013 static bfd_vma
1014 dtprel_base (struct bfd_link_info *info)
1016 /* If tls_sec is NULL, we should have signalled an error already. */
1017 if (elf_hash_table (info)->tls_sec == NULL)
1018 return 0;
1019 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1022 static bfd_vma
1023 tprel_base (struct bfd_link_info *info)
1025 /* If tls_sec is NULL, we should have signalled an error already. */
1026 if (elf_hash_table (info)->tls_sec == NULL)
1027 return 0;
1028 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1031 /* Create an entry in a MIPS ELF linker hash table. */
1033 static struct bfd_hash_entry *
1034 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1035 struct bfd_hash_table *table, const char *string)
1037 struct mips_elf_link_hash_entry *ret =
1038 (struct mips_elf_link_hash_entry *) entry;
1040 /* Allocate the structure if it has not already been allocated by a
1041 subclass. */
1042 if (ret == NULL)
1043 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1044 if (ret == NULL)
1045 return (struct bfd_hash_entry *) ret;
1047 /* Call the allocation method of the superclass. */
1048 ret = ((struct mips_elf_link_hash_entry *)
1049 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1050 table, string));
1051 if (ret != NULL)
1053 /* Set local fields. */
1054 memset (&ret->esym, 0, sizeof (EXTR));
1055 /* We use -2 as a marker to indicate that the information has
1056 not been set. -1 means there is no associated ifd. */
1057 ret->esym.ifd = -2;
1058 ret->la25_stub = 0;
1059 ret->possibly_dynamic_relocs = 0;
1060 ret->fn_stub = NULL;
1061 ret->call_stub = NULL;
1062 ret->call_fp_stub = NULL;
1063 ret->tls_type = GOT_NORMAL;
1064 ret->global_got_area = GGA_NONE;
1065 ret->got_only_for_calls = TRUE;
1066 ret->readonly_reloc = FALSE;
1067 ret->has_static_relocs = FALSE;
1068 ret->no_fn_stub = FALSE;
1069 ret->need_fn_stub = FALSE;
1070 ret->has_nonpic_branches = FALSE;
1071 ret->needs_lazy_stub = FALSE;
1074 return (struct bfd_hash_entry *) ret;
1077 bfd_boolean
1078 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1080 if (!sec->used_by_bfd)
1082 struct _mips_elf_section_data *sdata;
1083 bfd_size_type amt = sizeof (*sdata);
1085 sdata = bfd_zalloc (abfd, amt);
1086 if (sdata == NULL)
1087 return FALSE;
1088 sec->used_by_bfd = sdata;
1091 return _bfd_elf_new_section_hook (abfd, sec);
1094 /* Read ECOFF debugging information from a .mdebug section into a
1095 ecoff_debug_info structure. */
1097 bfd_boolean
1098 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1099 struct ecoff_debug_info *debug)
1101 HDRR *symhdr;
1102 const struct ecoff_debug_swap *swap;
1103 char *ext_hdr;
1105 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1106 memset (debug, 0, sizeof (*debug));
1108 ext_hdr = bfd_malloc (swap->external_hdr_size);
1109 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1110 goto error_return;
1112 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1113 swap->external_hdr_size))
1114 goto error_return;
1116 symhdr = &debug->symbolic_header;
1117 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1119 /* The symbolic header contains absolute file offsets and sizes to
1120 read. */
1121 #define READ(ptr, offset, count, size, type) \
1122 if (symhdr->count == 0) \
1123 debug->ptr = NULL; \
1124 else \
1126 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1127 debug->ptr = bfd_malloc (amt); \
1128 if (debug->ptr == NULL) \
1129 goto error_return; \
1130 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1131 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1132 goto error_return; \
1135 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1136 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1137 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1138 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1139 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1140 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1141 union aux_ext *);
1142 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1143 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1144 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1145 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1146 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1147 #undef READ
1149 debug->fdr = NULL;
1151 return TRUE;
1153 error_return:
1154 if (ext_hdr != NULL)
1155 free (ext_hdr);
1156 if (debug->line != NULL)
1157 free (debug->line);
1158 if (debug->external_dnr != NULL)
1159 free (debug->external_dnr);
1160 if (debug->external_pdr != NULL)
1161 free (debug->external_pdr);
1162 if (debug->external_sym != NULL)
1163 free (debug->external_sym);
1164 if (debug->external_opt != NULL)
1165 free (debug->external_opt);
1166 if (debug->external_aux != NULL)
1167 free (debug->external_aux);
1168 if (debug->ss != NULL)
1169 free (debug->ss);
1170 if (debug->ssext != NULL)
1171 free (debug->ssext);
1172 if (debug->external_fdr != NULL)
1173 free (debug->external_fdr);
1174 if (debug->external_rfd != NULL)
1175 free (debug->external_rfd);
1176 if (debug->external_ext != NULL)
1177 free (debug->external_ext);
1178 return FALSE;
1181 /* Swap RPDR (runtime procedure table entry) for output. */
1183 static void
1184 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1186 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1187 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1188 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1189 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1190 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1191 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1193 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1194 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1196 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1199 /* Create a runtime procedure table from the .mdebug section. */
1201 static bfd_boolean
1202 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1203 struct bfd_link_info *info, asection *s,
1204 struct ecoff_debug_info *debug)
1206 const struct ecoff_debug_swap *swap;
1207 HDRR *hdr = &debug->symbolic_header;
1208 RPDR *rpdr, *rp;
1209 struct rpdr_ext *erp;
1210 void *rtproc;
1211 struct pdr_ext *epdr;
1212 struct sym_ext *esym;
1213 char *ss, **sv;
1214 char *str;
1215 bfd_size_type size;
1216 bfd_size_type count;
1217 unsigned long sindex;
1218 unsigned long i;
1219 PDR pdr;
1220 SYMR sym;
1221 const char *no_name_func = _("static procedure (no name)");
1223 epdr = NULL;
1224 rpdr = NULL;
1225 esym = NULL;
1226 ss = NULL;
1227 sv = NULL;
1229 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1231 sindex = strlen (no_name_func) + 1;
1232 count = hdr->ipdMax;
1233 if (count > 0)
1235 size = swap->external_pdr_size;
1237 epdr = bfd_malloc (size * count);
1238 if (epdr == NULL)
1239 goto error_return;
1241 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1242 goto error_return;
1244 size = sizeof (RPDR);
1245 rp = rpdr = bfd_malloc (size * count);
1246 if (rpdr == NULL)
1247 goto error_return;
1249 size = sizeof (char *);
1250 sv = bfd_malloc (size * count);
1251 if (sv == NULL)
1252 goto error_return;
1254 count = hdr->isymMax;
1255 size = swap->external_sym_size;
1256 esym = bfd_malloc (size * count);
1257 if (esym == NULL)
1258 goto error_return;
1260 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1261 goto error_return;
1263 count = hdr->issMax;
1264 ss = bfd_malloc (count);
1265 if (ss == NULL)
1266 goto error_return;
1267 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1268 goto error_return;
1270 count = hdr->ipdMax;
1271 for (i = 0; i < (unsigned long) count; i++, rp++)
1273 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1274 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1275 rp->adr = sym.value;
1276 rp->regmask = pdr.regmask;
1277 rp->regoffset = pdr.regoffset;
1278 rp->fregmask = pdr.fregmask;
1279 rp->fregoffset = pdr.fregoffset;
1280 rp->frameoffset = pdr.frameoffset;
1281 rp->framereg = pdr.framereg;
1282 rp->pcreg = pdr.pcreg;
1283 rp->irpss = sindex;
1284 sv[i] = ss + sym.iss;
1285 sindex += strlen (sv[i]) + 1;
1289 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1290 size = BFD_ALIGN (size, 16);
1291 rtproc = bfd_alloc (abfd, size);
1292 if (rtproc == NULL)
1294 mips_elf_hash_table (info)->procedure_count = 0;
1295 goto error_return;
1298 mips_elf_hash_table (info)->procedure_count = count + 2;
1300 erp = rtproc;
1301 memset (erp, 0, sizeof (struct rpdr_ext));
1302 erp++;
1303 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1304 strcpy (str, no_name_func);
1305 str += strlen (no_name_func) + 1;
1306 for (i = 0; i < count; i++)
1308 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1309 strcpy (str, sv[i]);
1310 str += strlen (sv[i]) + 1;
1312 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1314 /* Set the size and contents of .rtproc section. */
1315 s->size = size;
1316 s->contents = rtproc;
1318 /* Skip this section later on (I don't think this currently
1319 matters, but someday it might). */
1320 s->map_head.link_order = NULL;
1322 if (epdr != NULL)
1323 free (epdr);
1324 if (rpdr != NULL)
1325 free (rpdr);
1326 if (esym != NULL)
1327 free (esym);
1328 if (ss != NULL)
1329 free (ss);
1330 if (sv != NULL)
1331 free (sv);
1333 return TRUE;
1335 error_return:
1336 if (epdr != NULL)
1337 free (epdr);
1338 if (rpdr != NULL)
1339 free (rpdr);
1340 if (esym != NULL)
1341 free (esym);
1342 if (ss != NULL)
1343 free (ss);
1344 if (sv != NULL)
1345 free (sv);
1346 return FALSE;
1349 /* We're going to create a stub for H. Create a symbol for the stub's
1350 value and size, to help make the disassembly easier to read. */
1352 static bfd_boolean
1353 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1354 struct mips_elf_link_hash_entry *h,
1355 const char *prefix, asection *s, bfd_vma value,
1356 bfd_vma size)
1358 struct bfd_link_hash_entry *bh;
1359 struct elf_link_hash_entry *elfh;
1360 const char *name;
1362 /* Create a new symbol. */
1363 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1364 bh = NULL;
1365 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1366 BSF_LOCAL, s, value, NULL,
1367 TRUE, FALSE, &bh))
1368 return FALSE;
1370 /* Make it a local function. */
1371 elfh = (struct elf_link_hash_entry *) bh;
1372 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1373 elfh->size = size;
1374 elfh->forced_local = 1;
1375 return TRUE;
1378 /* We're about to redefine H. Create a symbol to represent H's
1379 current value and size, to help make the disassembly easier
1380 to read. */
1382 static bfd_boolean
1383 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1384 struct mips_elf_link_hash_entry *h,
1385 const char *prefix)
1387 struct bfd_link_hash_entry *bh;
1388 struct elf_link_hash_entry *elfh;
1389 const char *name;
1390 asection *s;
1391 bfd_vma value;
1393 /* Read the symbol's value. */
1394 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1395 || h->root.root.type == bfd_link_hash_defweak);
1396 s = h->root.root.u.def.section;
1397 value = h->root.root.u.def.value;
1399 /* Create a new symbol. */
1400 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1401 bh = NULL;
1402 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1403 BSF_LOCAL, s, value, NULL,
1404 TRUE, FALSE, &bh))
1405 return FALSE;
1407 /* Make it local and copy the other attributes from H. */
1408 elfh = (struct elf_link_hash_entry *) bh;
1409 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1410 elfh->other = h->root.other;
1411 elfh->size = h->root.size;
1412 elfh->forced_local = 1;
1413 return TRUE;
1416 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1417 function rather than to a hard-float stub. */
1419 static bfd_boolean
1420 section_allows_mips16_refs_p (asection *section)
1422 const char *name;
1424 name = bfd_get_section_name (section->owner, section);
1425 return (FN_STUB_P (name)
1426 || CALL_STUB_P (name)
1427 || CALL_FP_STUB_P (name)
1428 || strcmp (name, ".pdr") == 0);
1431 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1432 stub section of some kind. Return the R_SYMNDX of the target
1433 function, or 0 if we can't decide which function that is. */
1435 static unsigned long
1436 mips16_stub_symndx (asection *sec ATTRIBUTE_UNUSED,
1437 const Elf_Internal_Rela *relocs,
1438 const Elf_Internal_Rela *relend)
1440 const Elf_Internal_Rela *rel;
1442 /* Trust the first R_MIPS_NONE relocation, if any. */
1443 for (rel = relocs; rel < relend; rel++)
1444 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1445 return ELF_R_SYM (sec->owner, rel->r_info);
1447 /* Otherwise trust the first relocation, whatever its kind. This is
1448 the traditional behavior. */
1449 if (relocs < relend)
1450 return ELF_R_SYM (sec->owner, relocs->r_info);
1452 return 0;
1455 /* Check the mips16 stubs for a particular symbol, and see if we can
1456 discard them. */
1458 static void
1459 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1460 struct mips_elf_link_hash_entry *h)
1462 /* Dynamic symbols must use the standard call interface, in case other
1463 objects try to call them. */
1464 if (h->fn_stub != NULL
1465 && h->root.dynindx != -1)
1467 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1468 h->need_fn_stub = TRUE;
1471 if (h->fn_stub != NULL
1472 && ! h->need_fn_stub)
1474 /* We don't need the fn_stub; the only references to this symbol
1475 are 16 bit calls. Clobber the size to 0 to prevent it from
1476 being included in the link. */
1477 h->fn_stub->size = 0;
1478 h->fn_stub->flags &= ~SEC_RELOC;
1479 h->fn_stub->reloc_count = 0;
1480 h->fn_stub->flags |= SEC_EXCLUDE;
1483 if (h->call_stub != NULL
1484 && ELF_ST_IS_MIPS16 (h->root.other))
1486 /* We don't need the call_stub; this is a 16 bit function, so
1487 calls from other 16 bit functions are OK. Clobber the size
1488 to 0 to prevent it from being included in the link. */
1489 h->call_stub->size = 0;
1490 h->call_stub->flags &= ~SEC_RELOC;
1491 h->call_stub->reloc_count = 0;
1492 h->call_stub->flags |= SEC_EXCLUDE;
1495 if (h->call_fp_stub != NULL
1496 && ELF_ST_IS_MIPS16 (h->root.other))
1498 /* We don't need the call_stub; this is a 16 bit function, so
1499 calls from other 16 bit functions are OK. Clobber the size
1500 to 0 to prevent it from being included in the link. */
1501 h->call_fp_stub->size = 0;
1502 h->call_fp_stub->flags &= ~SEC_RELOC;
1503 h->call_fp_stub->reloc_count = 0;
1504 h->call_fp_stub->flags |= SEC_EXCLUDE;
1508 /* Hashtable callbacks for mips_elf_la25_stubs. */
1510 static hashval_t
1511 mips_elf_la25_stub_hash (const void *entry_)
1513 const struct mips_elf_la25_stub *entry;
1515 entry = (struct mips_elf_la25_stub *) entry_;
1516 return entry->h->root.root.u.def.section->id
1517 + entry->h->root.root.u.def.value;
1520 static int
1521 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1523 const struct mips_elf_la25_stub *entry1, *entry2;
1525 entry1 = (struct mips_elf_la25_stub *) entry1_;
1526 entry2 = (struct mips_elf_la25_stub *) entry2_;
1527 return ((entry1->h->root.root.u.def.section
1528 == entry2->h->root.root.u.def.section)
1529 && (entry1->h->root.root.u.def.value
1530 == entry2->h->root.root.u.def.value));
1533 /* Called by the linker to set up the la25 stub-creation code. FN is
1534 the linker's implementation of add_stub_function. Return true on
1535 success. */
1537 bfd_boolean
1538 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1539 asection *(*fn) (const char *, asection *,
1540 asection *))
1542 struct mips_elf_link_hash_table *htab;
1544 htab = mips_elf_hash_table (info);
1545 if (htab == NULL)
1546 return FALSE;
1548 htab->add_stub_section = fn;
1549 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1550 mips_elf_la25_stub_eq, NULL);
1551 if (htab->la25_stubs == NULL)
1552 return FALSE;
1554 return TRUE;
1557 /* Return true if H is a locally-defined PIC function, in the sense
1558 that it might need $25 to be valid on entry. Note that MIPS16
1559 functions never need $25 to be valid on entry; they set up $gp
1560 using PC-relative instructions instead. */
1562 static bfd_boolean
1563 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1565 return ((h->root.root.type == bfd_link_hash_defined
1566 || h->root.root.type == bfd_link_hash_defweak)
1567 && h->root.def_regular
1568 && !bfd_is_abs_section (h->root.root.u.def.section)
1569 && !ELF_ST_IS_MIPS16 (h->root.other)
1570 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1571 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1574 /* STUB describes an la25 stub that we have decided to implement
1575 by inserting an LUI/ADDIU pair before the target function.
1576 Create the section and redirect the function symbol to it. */
1578 static bfd_boolean
1579 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1580 struct bfd_link_info *info)
1582 struct mips_elf_link_hash_table *htab;
1583 char *name;
1584 asection *s, *input_section;
1585 unsigned int align;
1587 htab = mips_elf_hash_table (info);
1588 if (htab == NULL)
1589 return FALSE;
1591 /* Create a unique name for the new section. */
1592 name = bfd_malloc (11 + sizeof (".text.stub."));
1593 if (name == NULL)
1594 return FALSE;
1595 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1597 /* Create the section. */
1598 input_section = stub->h->root.root.u.def.section;
1599 s = htab->add_stub_section (name, input_section,
1600 input_section->output_section);
1601 if (s == NULL)
1602 return FALSE;
1604 /* Make sure that any padding goes before the stub. */
1605 align = input_section->alignment_power;
1606 if (!bfd_set_section_alignment (s->owner, s, align))
1607 return FALSE;
1608 if (align > 3)
1609 s->size = (1 << align) - 8;
1611 /* Create a symbol for the stub. */
1612 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1613 stub->stub_section = s;
1614 stub->offset = s->size;
1616 /* Allocate room for it. */
1617 s->size += 8;
1618 return TRUE;
1621 /* STUB describes an la25 stub that we have decided to implement
1622 with a separate trampoline. Allocate room for it and redirect
1623 the function symbol to it. */
1625 static bfd_boolean
1626 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1627 struct bfd_link_info *info)
1629 struct mips_elf_link_hash_table *htab;
1630 asection *s;
1632 htab = mips_elf_hash_table (info);
1633 if (htab == NULL)
1634 return FALSE;
1636 /* Create a trampoline section, if we haven't already. */
1637 s = htab->strampoline;
1638 if (s == NULL)
1640 asection *input_section = stub->h->root.root.u.def.section;
1641 s = htab->add_stub_section (".text", NULL,
1642 input_section->output_section);
1643 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1644 return FALSE;
1645 htab->strampoline = s;
1648 /* Create a symbol for the stub. */
1649 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1650 stub->stub_section = s;
1651 stub->offset = s->size;
1653 /* Allocate room for it. */
1654 s->size += 16;
1655 return TRUE;
1658 /* H describes a symbol that needs an la25 stub. Make sure that an
1659 appropriate stub exists and point H at it. */
1661 static bfd_boolean
1662 mips_elf_add_la25_stub (struct bfd_link_info *info,
1663 struct mips_elf_link_hash_entry *h)
1665 struct mips_elf_link_hash_table *htab;
1666 struct mips_elf_la25_stub search, *stub;
1667 bfd_boolean use_trampoline_p;
1668 asection *s;
1669 bfd_vma value;
1670 void **slot;
1672 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1673 of the section and if we would need no more than 2 nops. */
1674 s = h->root.root.u.def.section;
1675 value = h->root.root.u.def.value;
1676 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1678 /* Describe the stub we want. */
1679 search.stub_section = NULL;
1680 search.offset = 0;
1681 search.h = h;
1683 /* See if we've already created an equivalent stub. */
1684 htab = mips_elf_hash_table (info);
1685 if (htab == NULL)
1686 return FALSE;
1688 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1689 if (slot == NULL)
1690 return FALSE;
1692 stub = (struct mips_elf_la25_stub *) *slot;
1693 if (stub != NULL)
1695 /* We can reuse the existing stub. */
1696 h->la25_stub = stub;
1697 return TRUE;
1700 /* Create a permanent copy of ENTRY and add it to the hash table. */
1701 stub = bfd_malloc (sizeof (search));
1702 if (stub == NULL)
1703 return FALSE;
1704 *stub = search;
1705 *slot = stub;
1707 h->la25_stub = stub;
1708 return (use_trampoline_p
1709 ? mips_elf_add_la25_trampoline (stub, info)
1710 : mips_elf_add_la25_intro (stub, info));
1713 /* A mips_elf_link_hash_traverse callback that is called before sizing
1714 sections. DATA points to a mips_htab_traverse_info structure. */
1716 static bfd_boolean
1717 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1719 struct mips_htab_traverse_info *hti;
1721 hti = (struct mips_htab_traverse_info *) data;
1722 if (h->root.root.type == bfd_link_hash_warning)
1723 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
1725 if (!hti->info->relocatable)
1726 mips_elf_check_mips16_stubs (hti->info, h);
1728 if (mips_elf_local_pic_function_p (h))
1730 /* PR 12845: If H is in a section that has been garbage
1731 collected it will have its output section set to *ABS*. */
1732 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1733 return TRUE;
1735 /* H is a function that might need $25 to be valid on entry.
1736 If we're creating a non-PIC relocatable object, mark H as
1737 being PIC. If we're creating a non-relocatable object with
1738 non-PIC branches and jumps to H, make sure that H has an la25
1739 stub. */
1740 if (hti->info->relocatable)
1742 if (!PIC_OBJECT_P (hti->output_bfd))
1743 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1745 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1747 hti->error = TRUE;
1748 return FALSE;
1751 return TRUE;
1754 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1755 Most mips16 instructions are 16 bits, but these instructions
1756 are 32 bits.
1758 The format of these instructions is:
1760 +--------------+--------------------------------+
1761 | JALX | X| Imm 20:16 | Imm 25:21 |
1762 +--------------+--------------------------------+
1763 | Immediate 15:0 |
1764 +-----------------------------------------------+
1766 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1767 Note that the immediate value in the first word is swapped.
1769 When producing a relocatable object file, R_MIPS16_26 is
1770 handled mostly like R_MIPS_26. In particular, the addend is
1771 stored as a straight 26-bit value in a 32-bit instruction.
1772 (gas makes life simpler for itself by never adjusting a
1773 R_MIPS16_26 reloc to be against a section, so the addend is
1774 always zero). However, the 32 bit instruction is stored as 2
1775 16-bit values, rather than a single 32-bit value. In a
1776 big-endian file, the result is the same; in a little-endian
1777 file, the two 16-bit halves of the 32 bit value are swapped.
1778 This is so that a disassembler can recognize the jal
1779 instruction.
1781 When doing a final link, R_MIPS16_26 is treated as a 32 bit
1782 instruction stored as two 16-bit values. The addend A is the
1783 contents of the targ26 field. The calculation is the same as
1784 R_MIPS_26. When storing the calculated value, reorder the
1785 immediate value as shown above, and don't forget to store the
1786 value as two 16-bit values.
1788 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
1789 defined as
1791 big-endian:
1792 +--------+----------------------+
1793 | | |
1794 | | targ26-16 |
1795 |31 26|25 0|
1796 +--------+----------------------+
1798 little-endian:
1799 +----------+------+-------------+
1800 | | | |
1801 | sub1 | | sub2 |
1802 |0 9|10 15|16 31|
1803 +----------+--------------------+
1804 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
1805 ((sub1 << 16) | sub2)).
1807 When producing a relocatable object file, the calculation is
1808 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1809 When producing a fully linked file, the calculation is
1810 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
1811 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
1813 The table below lists the other MIPS16 instruction relocations.
1814 Each one is calculated in the same way as the non-MIPS16 relocation
1815 given on the right, but using the extended MIPS16 layout of 16-bit
1816 immediate fields:
1818 R_MIPS16_GPREL R_MIPS_GPREL16
1819 R_MIPS16_GOT16 R_MIPS_GOT16
1820 R_MIPS16_CALL16 R_MIPS_CALL16
1821 R_MIPS16_HI16 R_MIPS_HI16
1822 R_MIPS16_LO16 R_MIPS_LO16
1824 A typical instruction will have a format like this:
1826 +--------------+--------------------------------+
1827 | EXTEND | Imm 10:5 | Imm 15:11 |
1828 +--------------+--------------------------------+
1829 | Major | rx | ry | Imm 4:0 |
1830 +--------------+--------------------------------+
1832 EXTEND is the five bit value 11110. Major is the instruction
1833 opcode.
1835 All we need to do here is shuffle the bits appropriately.
1836 As above, the two 16-bit halves must be swapped on a
1837 little-endian system. */
1839 static inline bfd_boolean
1840 mips16_reloc_p (int r_type)
1842 switch (r_type)
1844 case R_MIPS16_26:
1845 case R_MIPS16_GPREL:
1846 case R_MIPS16_GOT16:
1847 case R_MIPS16_CALL16:
1848 case R_MIPS16_HI16:
1849 case R_MIPS16_LO16:
1850 return TRUE;
1852 default:
1853 return FALSE;
1857 static inline bfd_boolean
1858 got16_reloc_p (int r_type)
1860 return r_type == R_MIPS_GOT16 || r_type == R_MIPS16_GOT16;
1863 static inline bfd_boolean
1864 call16_reloc_p (int r_type)
1866 return r_type == R_MIPS_CALL16 || r_type == R_MIPS16_CALL16;
1869 static inline bfd_boolean
1870 hi16_reloc_p (int r_type)
1872 return r_type == R_MIPS_HI16 || r_type == R_MIPS16_HI16;
1875 static inline bfd_boolean
1876 lo16_reloc_p (int r_type)
1878 return r_type == R_MIPS_LO16 || r_type == R_MIPS16_LO16;
1881 static inline bfd_boolean
1882 mips16_call_reloc_p (int r_type)
1884 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
1887 static inline bfd_boolean
1888 jal_reloc_p (int r_type)
1890 return r_type == R_MIPS_26 || r_type == R_MIPS16_26;
1893 void
1894 _bfd_mips16_elf_reloc_unshuffle (bfd *abfd, int r_type,
1895 bfd_boolean jal_shuffle, bfd_byte *data)
1897 bfd_vma extend, insn, val;
1899 if (!mips16_reloc_p (r_type))
1900 return;
1902 /* Pick up the mips16 extend instruction and the real instruction. */
1903 extend = bfd_get_16 (abfd, data);
1904 insn = bfd_get_16 (abfd, data + 2);
1905 if (r_type == R_MIPS16_26)
1907 if (jal_shuffle)
1908 val = ((extend & 0xfc00) << 16) | ((extend & 0x3e0) << 11)
1909 | ((extend & 0x1f) << 21) | insn;
1910 else
1911 val = extend << 16 | insn;
1913 else
1914 val = ((extend & 0xf800) << 16) | ((insn & 0xffe0) << 11)
1915 | ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
1916 bfd_put_32 (abfd, val, data);
1919 void
1920 _bfd_mips16_elf_reloc_shuffle (bfd *abfd, int r_type,
1921 bfd_boolean jal_shuffle, bfd_byte *data)
1923 bfd_vma extend, insn, val;
1925 if (!mips16_reloc_p (r_type))
1926 return;
1928 val = bfd_get_32 (abfd, data);
1929 if (r_type == R_MIPS16_26)
1931 if (jal_shuffle)
1933 insn = val & 0xffff;
1934 extend = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
1935 | ((val >> 21) & 0x1f);
1937 else
1939 insn = val & 0xffff;
1940 extend = val >> 16;
1943 else
1945 insn = ((val >> 11) & 0xffe0) | (val & 0x1f);
1946 extend = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
1948 bfd_put_16 (abfd, insn, data + 2);
1949 bfd_put_16 (abfd, extend, data);
1952 bfd_reloc_status_type
1953 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
1954 arelent *reloc_entry, asection *input_section,
1955 bfd_boolean relocatable, void *data, bfd_vma gp)
1957 bfd_vma relocation;
1958 bfd_signed_vma val;
1959 bfd_reloc_status_type status;
1961 if (bfd_is_com_section (symbol->section))
1962 relocation = 0;
1963 else
1964 relocation = symbol->value;
1966 relocation += symbol->section->output_section->vma;
1967 relocation += symbol->section->output_offset;
1969 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1970 return bfd_reloc_outofrange;
1972 /* Set val to the offset into the section or symbol. */
1973 val = reloc_entry->addend;
1975 _bfd_mips_elf_sign_extend (val, 16);
1977 /* Adjust val for the final section location and GP value. If we
1978 are producing relocatable output, we don't want to do this for
1979 an external symbol. */
1980 if (! relocatable
1981 || (symbol->flags & BSF_SECTION_SYM) != 0)
1982 val += relocation - gp;
1984 if (reloc_entry->howto->partial_inplace)
1986 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
1987 (bfd_byte *) data
1988 + reloc_entry->address);
1989 if (status != bfd_reloc_ok)
1990 return status;
1992 else
1993 reloc_entry->addend = val;
1995 if (relocatable)
1996 reloc_entry->address += input_section->output_offset;
1998 return bfd_reloc_ok;
2001 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2002 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2003 that contains the relocation field and DATA points to the start of
2004 INPUT_SECTION. */
2006 struct mips_hi16
2008 struct mips_hi16 *next;
2009 bfd_byte *data;
2010 asection *input_section;
2011 arelent rel;
2014 /* FIXME: This should not be a static variable. */
2016 static struct mips_hi16 *mips_hi16_list;
2018 /* A howto special_function for REL *HI16 relocations. We can only
2019 calculate the correct value once we've seen the partnering
2020 *LO16 relocation, so just save the information for later.
2022 The ABI requires that the *LO16 immediately follow the *HI16.
2023 However, as a GNU extension, we permit an arbitrary number of
2024 *HI16s to be associated with a single *LO16. This significantly
2025 simplies the relocation handling in gcc. */
2027 bfd_reloc_status_type
2028 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2029 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2030 asection *input_section, bfd *output_bfd,
2031 char **error_message ATTRIBUTE_UNUSED)
2033 struct mips_hi16 *n;
2035 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2036 return bfd_reloc_outofrange;
2038 n = bfd_malloc (sizeof *n);
2039 if (n == NULL)
2040 return bfd_reloc_outofrange;
2042 n->next = mips_hi16_list;
2043 n->data = data;
2044 n->input_section = input_section;
2045 n->rel = *reloc_entry;
2046 mips_hi16_list = n;
2048 if (output_bfd != NULL)
2049 reloc_entry->address += input_section->output_offset;
2051 return bfd_reloc_ok;
2054 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2055 like any other 16-bit relocation when applied to global symbols, but is
2056 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2058 bfd_reloc_status_type
2059 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2060 void *data, asection *input_section,
2061 bfd *output_bfd, char **error_message)
2063 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2064 || bfd_is_und_section (bfd_get_section (symbol))
2065 || bfd_is_com_section (bfd_get_section (symbol)))
2066 /* The relocation is against a global symbol. */
2067 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2068 input_section, output_bfd,
2069 error_message);
2071 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2072 input_section, output_bfd, error_message);
2075 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2076 is a straightforward 16 bit inplace relocation, but we must deal with
2077 any partnering high-part relocations as well. */
2079 bfd_reloc_status_type
2080 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2081 void *data, asection *input_section,
2082 bfd *output_bfd, char **error_message)
2084 bfd_vma vallo;
2085 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2087 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2088 return bfd_reloc_outofrange;
2090 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2091 location);
2092 vallo = bfd_get_32 (abfd, location);
2093 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2094 location);
2096 while (mips_hi16_list != NULL)
2098 bfd_reloc_status_type ret;
2099 struct mips_hi16 *hi;
2101 hi = mips_hi16_list;
2103 /* R_MIPS*_GOT16 relocations are something of a special case. We
2104 want to install the addend in the same way as for a R_MIPS*_HI16
2105 relocation (with a rightshift of 16). However, since GOT16
2106 relocations can also be used with global symbols, their howto
2107 has a rightshift of 0. */
2108 if (hi->rel.howto->type == R_MIPS_GOT16)
2109 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2110 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2111 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2113 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2114 carry or borrow will induce a change of +1 or -1 in the high part. */
2115 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2117 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2118 hi->input_section, output_bfd,
2119 error_message);
2120 if (ret != bfd_reloc_ok)
2121 return ret;
2123 mips_hi16_list = hi->next;
2124 free (hi);
2127 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2128 input_section, output_bfd,
2129 error_message);
2132 /* A generic howto special_function. This calculates and installs the
2133 relocation itself, thus avoiding the oft-discussed problems in
2134 bfd_perform_relocation and bfd_install_relocation. */
2136 bfd_reloc_status_type
2137 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2138 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2139 asection *input_section, bfd *output_bfd,
2140 char **error_message ATTRIBUTE_UNUSED)
2142 bfd_signed_vma val;
2143 bfd_reloc_status_type status;
2144 bfd_boolean relocatable;
2146 relocatable = (output_bfd != NULL);
2148 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2149 return bfd_reloc_outofrange;
2151 /* Build up the field adjustment in VAL. */
2152 val = 0;
2153 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2155 /* Either we're calculating the final field value or we have a
2156 relocation against a section symbol. Add in the section's
2157 offset or address. */
2158 val += symbol->section->output_section->vma;
2159 val += symbol->section->output_offset;
2162 if (!relocatable)
2164 /* We're calculating the final field value. Add in the symbol's value
2165 and, if pc-relative, subtract the address of the field itself. */
2166 val += symbol->value;
2167 if (reloc_entry->howto->pc_relative)
2169 val -= input_section->output_section->vma;
2170 val -= input_section->output_offset;
2171 val -= reloc_entry->address;
2175 /* VAL is now the final adjustment. If we're keeping this relocation
2176 in the output file, and if the relocation uses a separate addend,
2177 we just need to add VAL to that addend. Otherwise we need to add
2178 VAL to the relocation field itself. */
2179 if (relocatable && !reloc_entry->howto->partial_inplace)
2180 reloc_entry->addend += val;
2181 else
2183 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2185 /* Add in the separate addend, if any. */
2186 val += reloc_entry->addend;
2188 /* Add VAL to the relocation field. */
2189 _bfd_mips16_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2190 location);
2191 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2192 location);
2193 _bfd_mips16_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2194 location);
2196 if (status != bfd_reloc_ok)
2197 return status;
2200 if (relocatable)
2201 reloc_entry->address += input_section->output_offset;
2203 return bfd_reloc_ok;
2206 /* Swap an entry in a .gptab section. Note that these routines rely
2207 on the equivalence of the two elements of the union. */
2209 static void
2210 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2211 Elf32_gptab *in)
2213 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2214 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2217 static void
2218 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2219 Elf32_External_gptab *ex)
2221 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2222 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2225 static void
2226 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2227 Elf32_External_compact_rel *ex)
2229 H_PUT_32 (abfd, in->id1, ex->id1);
2230 H_PUT_32 (abfd, in->num, ex->num);
2231 H_PUT_32 (abfd, in->id2, ex->id2);
2232 H_PUT_32 (abfd, in->offset, ex->offset);
2233 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2234 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2237 static void
2238 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2239 Elf32_External_crinfo *ex)
2241 unsigned long l;
2243 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2244 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2245 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2246 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2247 H_PUT_32 (abfd, l, ex->info);
2248 H_PUT_32 (abfd, in->konst, ex->konst);
2249 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2252 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2253 routines swap this structure in and out. They are used outside of
2254 BFD, so they are globally visible. */
2256 void
2257 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2258 Elf32_RegInfo *in)
2260 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2261 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2262 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2263 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2264 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2265 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2268 void
2269 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2270 Elf32_External_RegInfo *ex)
2272 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2273 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2274 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2275 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2276 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2277 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2280 /* In the 64 bit ABI, the .MIPS.options section holds register
2281 information in an Elf64_Reginfo structure. These routines swap
2282 them in and out. They are globally visible because they are used
2283 outside of BFD. These routines are here so that gas can call them
2284 without worrying about whether the 64 bit ABI has been included. */
2286 void
2287 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2288 Elf64_Internal_RegInfo *in)
2290 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2291 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2292 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2293 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2294 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2295 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2296 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2299 void
2300 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2301 Elf64_External_RegInfo *ex)
2303 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2304 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2305 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2306 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2307 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2308 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2309 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2312 /* Swap in an options header. */
2314 void
2315 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2316 Elf_Internal_Options *in)
2318 in->kind = H_GET_8 (abfd, ex->kind);
2319 in->size = H_GET_8 (abfd, ex->size);
2320 in->section = H_GET_16 (abfd, ex->section);
2321 in->info = H_GET_32 (abfd, ex->info);
2324 /* Swap out an options header. */
2326 void
2327 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2328 Elf_External_Options *ex)
2330 H_PUT_8 (abfd, in->kind, ex->kind);
2331 H_PUT_8 (abfd, in->size, ex->size);
2332 H_PUT_16 (abfd, in->section, ex->section);
2333 H_PUT_32 (abfd, in->info, ex->info);
2336 /* This function is called via qsort() to sort the dynamic relocation
2337 entries by increasing r_symndx value. */
2339 static int
2340 sort_dynamic_relocs (const void *arg1, const void *arg2)
2342 Elf_Internal_Rela int_reloc1;
2343 Elf_Internal_Rela int_reloc2;
2344 int diff;
2346 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2347 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2349 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2350 if (diff != 0)
2351 return diff;
2353 if (int_reloc1.r_offset < int_reloc2.r_offset)
2354 return -1;
2355 if (int_reloc1.r_offset > int_reloc2.r_offset)
2356 return 1;
2357 return 0;
2360 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2362 static int
2363 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2364 const void *arg2 ATTRIBUTE_UNUSED)
2366 #ifdef BFD64
2367 Elf_Internal_Rela int_reloc1[3];
2368 Elf_Internal_Rela int_reloc2[3];
2370 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2371 (reldyn_sorting_bfd, arg1, int_reloc1);
2372 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2373 (reldyn_sorting_bfd, arg2, int_reloc2);
2375 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2376 return -1;
2377 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2378 return 1;
2380 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2381 return -1;
2382 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2383 return 1;
2384 return 0;
2385 #else
2386 abort ();
2387 #endif
2391 /* This routine is used to write out ECOFF debugging external symbol
2392 information. It is called via mips_elf_link_hash_traverse. The
2393 ECOFF external symbol information must match the ELF external
2394 symbol information. Unfortunately, at this point we don't know
2395 whether a symbol is required by reloc information, so the two
2396 tables may wind up being different. We must sort out the external
2397 symbol information before we can set the final size of the .mdebug
2398 section, and we must set the size of the .mdebug section before we
2399 can relocate any sections, and we can't know which symbols are
2400 required by relocation until we relocate the sections.
2401 Fortunately, it is relatively unlikely that any symbol will be
2402 stripped but required by a reloc. In particular, it can not happen
2403 when generating a final executable. */
2405 static bfd_boolean
2406 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2408 struct extsym_info *einfo = data;
2409 bfd_boolean strip;
2410 asection *sec, *output_section;
2412 if (h->root.root.type == bfd_link_hash_warning)
2413 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
2415 if (h->root.indx == -2)
2416 strip = FALSE;
2417 else if ((h->root.def_dynamic
2418 || h->root.ref_dynamic
2419 || h->root.type == bfd_link_hash_new)
2420 && !h->root.def_regular
2421 && !h->root.ref_regular)
2422 strip = TRUE;
2423 else if (einfo->info->strip == strip_all
2424 || (einfo->info->strip == strip_some
2425 && bfd_hash_lookup (einfo->info->keep_hash,
2426 h->root.root.root.string,
2427 FALSE, FALSE) == NULL))
2428 strip = TRUE;
2429 else
2430 strip = FALSE;
2432 if (strip)
2433 return TRUE;
2435 if (h->esym.ifd == -2)
2437 h->esym.jmptbl = 0;
2438 h->esym.cobol_main = 0;
2439 h->esym.weakext = 0;
2440 h->esym.reserved = 0;
2441 h->esym.ifd = ifdNil;
2442 h->esym.asym.value = 0;
2443 h->esym.asym.st = stGlobal;
2445 if (h->root.root.type == bfd_link_hash_undefined
2446 || h->root.root.type == bfd_link_hash_undefweak)
2448 const char *name;
2450 /* Use undefined class. Also, set class and type for some
2451 special symbols. */
2452 name = h->root.root.root.string;
2453 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2454 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2456 h->esym.asym.sc = scData;
2457 h->esym.asym.st = stLabel;
2458 h->esym.asym.value = 0;
2460 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2462 h->esym.asym.sc = scAbs;
2463 h->esym.asym.st = stLabel;
2464 h->esym.asym.value =
2465 mips_elf_hash_table (einfo->info)->procedure_count;
2467 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2469 h->esym.asym.sc = scAbs;
2470 h->esym.asym.st = stLabel;
2471 h->esym.asym.value = elf_gp (einfo->abfd);
2473 else
2474 h->esym.asym.sc = scUndefined;
2476 else if (h->root.root.type != bfd_link_hash_defined
2477 && h->root.root.type != bfd_link_hash_defweak)
2478 h->esym.asym.sc = scAbs;
2479 else
2481 const char *name;
2483 sec = h->root.root.u.def.section;
2484 output_section = sec->output_section;
2486 /* When making a shared library and symbol h is the one from
2487 the another shared library, OUTPUT_SECTION may be null. */
2488 if (output_section == NULL)
2489 h->esym.asym.sc = scUndefined;
2490 else
2492 name = bfd_section_name (output_section->owner, output_section);
2494 if (strcmp (name, ".text") == 0)
2495 h->esym.asym.sc = scText;
2496 else if (strcmp (name, ".data") == 0)
2497 h->esym.asym.sc = scData;
2498 else if (strcmp (name, ".sdata") == 0)
2499 h->esym.asym.sc = scSData;
2500 else if (strcmp (name, ".rodata") == 0
2501 || strcmp (name, ".rdata") == 0)
2502 h->esym.asym.sc = scRData;
2503 else if (strcmp (name, ".bss") == 0)
2504 h->esym.asym.sc = scBss;
2505 else if (strcmp (name, ".sbss") == 0)
2506 h->esym.asym.sc = scSBss;
2507 else if (strcmp (name, ".init") == 0)
2508 h->esym.asym.sc = scInit;
2509 else if (strcmp (name, ".fini") == 0)
2510 h->esym.asym.sc = scFini;
2511 else
2512 h->esym.asym.sc = scAbs;
2516 h->esym.asym.reserved = 0;
2517 h->esym.asym.index = indexNil;
2520 if (h->root.root.type == bfd_link_hash_common)
2521 h->esym.asym.value = h->root.root.u.c.size;
2522 else if (h->root.root.type == bfd_link_hash_defined
2523 || h->root.root.type == bfd_link_hash_defweak)
2525 if (h->esym.asym.sc == scCommon)
2526 h->esym.asym.sc = scBss;
2527 else if (h->esym.asym.sc == scSCommon)
2528 h->esym.asym.sc = scSBss;
2530 sec = h->root.root.u.def.section;
2531 output_section = sec->output_section;
2532 if (output_section != NULL)
2533 h->esym.asym.value = (h->root.root.u.def.value
2534 + sec->output_offset
2535 + output_section->vma);
2536 else
2537 h->esym.asym.value = 0;
2539 else
2541 struct mips_elf_link_hash_entry *hd = h;
2543 while (hd->root.root.type == bfd_link_hash_indirect)
2544 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2546 if (hd->needs_lazy_stub)
2548 /* Set type and value for a symbol with a function stub. */
2549 h->esym.asym.st = stProc;
2550 sec = hd->root.root.u.def.section;
2551 if (sec == NULL)
2552 h->esym.asym.value = 0;
2553 else
2555 output_section = sec->output_section;
2556 if (output_section != NULL)
2557 h->esym.asym.value = (hd->root.plt.offset
2558 + sec->output_offset
2559 + output_section->vma);
2560 else
2561 h->esym.asym.value = 0;
2566 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2567 h->root.root.root.string,
2568 &h->esym))
2570 einfo->failed = TRUE;
2571 return FALSE;
2574 return TRUE;
2577 /* A comparison routine used to sort .gptab entries. */
2579 static int
2580 gptab_compare (const void *p1, const void *p2)
2582 const Elf32_gptab *a1 = p1;
2583 const Elf32_gptab *a2 = p2;
2585 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2588 /* Functions to manage the got entry hash table. */
2590 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2591 hash number. */
2593 static INLINE hashval_t
2594 mips_elf_hash_bfd_vma (bfd_vma addr)
2596 #ifdef BFD64
2597 return addr + (addr >> 32);
2598 #else
2599 return addr;
2600 #endif
2603 /* got_entries only match if they're identical, except for gotidx, so
2604 use all fields to compute the hash, and compare the appropriate
2605 union members. */
2607 static hashval_t
2608 mips_elf_got_entry_hash (const void *entry_)
2610 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2612 return entry->symndx
2613 + ((entry->tls_type & GOT_TLS_LDM) << 17)
2614 + (! entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2615 : entry->abfd->id
2616 + (entry->symndx >= 0 ? mips_elf_hash_bfd_vma (entry->d.addend)
2617 : entry->d.h->root.root.root.hash));
2620 static int
2621 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2623 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2624 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2626 /* An LDM entry can only match another LDM entry. */
2627 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2628 return 0;
2630 return e1->abfd == e2->abfd && e1->symndx == e2->symndx
2631 && (! e1->abfd ? e1->d.address == e2->d.address
2632 : e1->symndx >= 0 ? e1->d.addend == e2->d.addend
2633 : e1->d.h == e2->d.h);
2636 /* multi_got_entries are still a match in the case of global objects,
2637 even if the input bfd in which they're referenced differs, so the
2638 hash computation and compare functions are adjusted
2639 accordingly. */
2641 static hashval_t
2642 mips_elf_multi_got_entry_hash (const void *entry_)
2644 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2646 return entry->symndx
2647 + (! entry->abfd
2648 ? mips_elf_hash_bfd_vma (entry->d.address)
2649 : entry->symndx >= 0
2650 ? ((entry->tls_type & GOT_TLS_LDM)
2651 ? (GOT_TLS_LDM << 17)
2652 : (entry->abfd->id
2653 + mips_elf_hash_bfd_vma (entry->d.addend)))
2654 : entry->d.h->root.root.root.hash);
2657 static int
2658 mips_elf_multi_got_entry_eq (const void *entry1, const void *entry2)
2660 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2661 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2663 /* Any two LDM entries match. */
2664 if (e1->tls_type & e2->tls_type & GOT_TLS_LDM)
2665 return 1;
2667 /* Nothing else matches an LDM entry. */
2668 if ((e1->tls_type ^ e2->tls_type) & GOT_TLS_LDM)
2669 return 0;
2671 return e1->symndx == e2->symndx
2672 && (e1->symndx >= 0 ? e1->abfd == e2->abfd && e1->d.addend == e2->d.addend
2673 : e1->abfd == NULL || e2->abfd == NULL
2674 ? e1->abfd == e2->abfd && e1->d.address == e2->d.address
2675 : e1->d.h == e2->d.h);
2678 static hashval_t
2679 mips_got_page_entry_hash (const void *entry_)
2681 const struct mips_got_page_entry *entry;
2683 entry = (const struct mips_got_page_entry *) entry_;
2684 return entry->abfd->id + entry->symndx;
2687 static int
2688 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2690 const struct mips_got_page_entry *entry1, *entry2;
2692 entry1 = (const struct mips_got_page_entry *) entry1_;
2693 entry2 = (const struct mips_got_page_entry *) entry2_;
2694 return entry1->abfd == entry2->abfd && entry1->symndx == entry2->symndx;
2697 /* Return the dynamic relocation section. If it doesn't exist, try to
2698 create a new it if CREATE_P, otherwise return NULL. Also return NULL
2699 if creation fails. */
2701 static asection *
2702 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
2704 const char *dname;
2705 asection *sreloc;
2706 bfd *dynobj;
2708 dname = MIPS_ELF_REL_DYN_NAME (info);
2709 dynobj = elf_hash_table (info)->dynobj;
2710 sreloc = bfd_get_section_by_name (dynobj, dname);
2711 if (sreloc == NULL && create_p)
2713 sreloc = bfd_make_section_with_flags (dynobj, dname,
2714 (SEC_ALLOC
2715 | SEC_LOAD
2716 | SEC_HAS_CONTENTS
2717 | SEC_IN_MEMORY
2718 | SEC_LINKER_CREATED
2719 | SEC_READONLY));
2720 if (sreloc == NULL
2721 || ! bfd_set_section_alignment (dynobj, sreloc,
2722 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
2723 return NULL;
2725 return sreloc;
2728 /* Count the number of relocations needed for a TLS GOT entry, with
2729 access types from TLS_TYPE, and symbol H (or a local symbol if H
2730 is NULL). */
2732 static int
2733 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
2734 struct elf_link_hash_entry *h)
2736 int indx = 0;
2737 int ret = 0;
2738 bfd_boolean need_relocs = FALSE;
2739 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2741 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2742 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
2743 indx = h->dynindx;
2745 if ((info->shared || indx != 0)
2746 && (h == NULL
2747 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2748 || h->root.type != bfd_link_hash_undefweak))
2749 need_relocs = TRUE;
2751 if (!need_relocs)
2752 return FALSE;
2754 if (tls_type & GOT_TLS_GD)
2756 ret++;
2757 if (indx != 0)
2758 ret++;
2761 if (tls_type & GOT_TLS_IE)
2762 ret++;
2764 if ((tls_type & GOT_TLS_LDM) && info->shared)
2765 ret++;
2767 return ret;
2770 /* Count the number of TLS relocations required for the GOT entry in
2771 ARG1, if it describes a local symbol. */
2773 static int
2774 mips_elf_count_local_tls_relocs (void **arg1, void *arg2)
2776 struct mips_got_entry *entry = * (struct mips_got_entry **) arg1;
2777 struct mips_elf_count_tls_arg *arg = arg2;
2779 if (entry->abfd != NULL && entry->symndx != -1)
2780 arg->needed += mips_tls_got_relocs (arg->info, entry->tls_type, NULL);
2782 return 1;
2785 /* Count the number of TLS GOT entries required for the global (or
2786 forced-local) symbol in ARG1. */
2788 static int
2789 mips_elf_count_global_tls_entries (void *arg1, void *arg2)
2791 struct mips_elf_link_hash_entry *hm
2792 = (struct mips_elf_link_hash_entry *) arg1;
2793 struct mips_elf_count_tls_arg *arg = arg2;
2795 if (hm->tls_type & GOT_TLS_GD)
2796 arg->needed += 2;
2797 if (hm->tls_type & GOT_TLS_IE)
2798 arg->needed += 1;
2800 return 1;
2803 /* Count the number of TLS relocations required for the global (or
2804 forced-local) symbol in ARG1. */
2806 static int
2807 mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
2809 struct mips_elf_link_hash_entry *hm
2810 = (struct mips_elf_link_hash_entry *) arg1;
2811 struct mips_elf_count_tls_arg *arg = arg2;
2813 arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
2815 return 1;
2818 /* Output a simple dynamic relocation into SRELOC. */
2820 static void
2821 mips_elf_output_dynamic_relocation (bfd *output_bfd,
2822 asection *sreloc,
2823 unsigned long reloc_index,
2824 unsigned long indx,
2825 int r_type,
2826 bfd_vma offset)
2828 Elf_Internal_Rela rel[3];
2830 memset (rel, 0, sizeof (rel));
2832 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
2833 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
2835 if (ABI_64_P (output_bfd))
2837 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
2838 (output_bfd, &rel[0],
2839 (sreloc->contents
2840 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
2842 else
2843 bfd_elf32_swap_reloc_out
2844 (output_bfd, &rel[0],
2845 (sreloc->contents
2846 + reloc_index * sizeof (Elf32_External_Rel)));
2849 /* Initialize a set of TLS GOT entries for one symbol. */
2851 static void
2852 mips_elf_initialize_tls_slots (bfd *abfd, bfd_vma got_offset,
2853 unsigned char *tls_type_p,
2854 struct bfd_link_info *info,
2855 struct mips_elf_link_hash_entry *h,
2856 bfd_vma value)
2858 struct mips_elf_link_hash_table *htab;
2859 int indx;
2860 asection *sreloc, *sgot;
2861 bfd_vma offset, offset2;
2862 bfd_boolean need_relocs = FALSE;
2864 htab = mips_elf_hash_table (info);
2865 if (htab == NULL)
2866 return;
2868 sgot = htab->sgot;
2870 indx = 0;
2871 if (h != NULL)
2873 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
2875 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
2876 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
2877 indx = h->root.dynindx;
2880 if (*tls_type_p & GOT_TLS_DONE)
2881 return;
2883 if ((info->shared || indx != 0)
2884 && (h == NULL
2885 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
2886 || h->root.type != bfd_link_hash_undefweak))
2887 need_relocs = TRUE;
2889 /* MINUS_ONE means the symbol is not defined in this object. It may not
2890 be defined at all; assume that the value doesn't matter in that
2891 case. Otherwise complain if we would use the value. */
2892 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
2893 || h->root.root.type == bfd_link_hash_undefweak);
2895 /* Emit necessary relocations. */
2896 sreloc = mips_elf_rel_dyn_section (info, FALSE);
2898 /* General Dynamic. */
2899 if (*tls_type_p & GOT_TLS_GD)
2901 offset = got_offset;
2902 offset2 = offset + MIPS_ELF_GOT_SIZE (abfd);
2904 if (need_relocs)
2906 mips_elf_output_dynamic_relocation
2907 (abfd, sreloc, sreloc->reloc_count++, indx,
2908 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2909 sgot->output_offset + sgot->output_section->vma + offset);
2911 if (indx)
2912 mips_elf_output_dynamic_relocation
2913 (abfd, sreloc, sreloc->reloc_count++, indx,
2914 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
2915 sgot->output_offset + sgot->output_section->vma + offset2);
2916 else
2917 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2918 sgot->contents + offset2);
2920 else
2922 MIPS_ELF_PUT_WORD (abfd, 1,
2923 sgot->contents + offset);
2924 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
2925 sgot->contents + offset2);
2928 got_offset += 2 * MIPS_ELF_GOT_SIZE (abfd);
2931 /* Initial Exec model. */
2932 if (*tls_type_p & GOT_TLS_IE)
2934 offset = got_offset;
2936 if (need_relocs)
2938 if (indx == 0)
2939 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
2940 sgot->contents + offset);
2941 else
2942 MIPS_ELF_PUT_WORD (abfd, 0,
2943 sgot->contents + offset);
2945 mips_elf_output_dynamic_relocation
2946 (abfd, sreloc, sreloc->reloc_count++, indx,
2947 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
2948 sgot->output_offset + sgot->output_section->vma + offset);
2950 else
2951 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
2952 sgot->contents + offset);
2955 if (*tls_type_p & GOT_TLS_LDM)
2957 /* The initial offset is zero, and the LD offsets will include the
2958 bias by DTP_OFFSET. */
2959 MIPS_ELF_PUT_WORD (abfd, 0,
2960 sgot->contents + got_offset
2961 + MIPS_ELF_GOT_SIZE (abfd));
2963 if (!info->shared)
2964 MIPS_ELF_PUT_WORD (abfd, 1,
2965 sgot->contents + got_offset);
2966 else
2967 mips_elf_output_dynamic_relocation
2968 (abfd, sreloc, sreloc->reloc_count++, indx,
2969 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
2970 sgot->output_offset + sgot->output_section->vma + got_offset);
2973 *tls_type_p |= GOT_TLS_DONE;
2976 /* Return the GOT index to use for a relocation of type R_TYPE against
2977 a symbol accessed using TLS_TYPE models. The GOT entries for this
2978 symbol in this GOT start at GOT_INDEX. This function initializes the
2979 GOT entries and corresponding relocations. */
2981 static bfd_vma
2982 mips_tls_got_index (bfd *abfd, bfd_vma got_index, unsigned char *tls_type,
2983 int r_type, struct bfd_link_info *info,
2984 struct mips_elf_link_hash_entry *h, bfd_vma symbol)
2986 BFD_ASSERT (r_type == R_MIPS_TLS_GOTTPREL || r_type == R_MIPS_TLS_GD
2987 || r_type == R_MIPS_TLS_LDM);
2989 mips_elf_initialize_tls_slots (abfd, got_index, tls_type, info, h, symbol);
2991 if (r_type == R_MIPS_TLS_GOTTPREL)
2993 BFD_ASSERT (*tls_type & GOT_TLS_IE);
2994 if (*tls_type & GOT_TLS_GD)
2995 return got_index + 2 * MIPS_ELF_GOT_SIZE (abfd);
2996 else
2997 return got_index;
3000 if (r_type == R_MIPS_TLS_GD)
3002 BFD_ASSERT (*tls_type & GOT_TLS_GD);
3003 return got_index;
3006 if (r_type == R_MIPS_TLS_LDM)
3008 BFD_ASSERT (*tls_type & GOT_TLS_LDM);
3009 return got_index;
3012 return got_index;
3015 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3016 for global symbol H. .got.plt comes before the GOT, so the offset
3017 will be negative. */
3019 static bfd_vma
3020 mips_elf_gotplt_index (struct bfd_link_info *info,
3021 struct elf_link_hash_entry *h)
3023 bfd_vma plt_index, got_address, got_value;
3024 struct mips_elf_link_hash_table *htab;
3026 htab = mips_elf_hash_table (info);
3027 BFD_ASSERT (htab != NULL);
3029 BFD_ASSERT (h->plt.offset != (bfd_vma) -1);
3031 /* This function only works for VxWorks, because a non-VxWorks .got.plt
3032 section starts with reserved entries. */
3033 BFD_ASSERT (htab->is_vxworks);
3035 /* Calculate the index of the symbol's PLT entry. */
3036 plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
3038 /* Calculate the address of the associated .got.plt entry. */
3039 got_address = (htab->sgotplt->output_section->vma
3040 + htab->sgotplt->output_offset
3041 + plt_index * 4);
3043 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3044 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3045 + htab->root.hgot->root.u.def.section->output_offset
3046 + htab->root.hgot->root.u.def.value);
3048 return got_address - got_value;
3051 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3052 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3053 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3054 offset can be found. */
3056 static bfd_vma
3057 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3058 bfd_vma value, unsigned long r_symndx,
3059 struct mips_elf_link_hash_entry *h, int r_type)
3061 struct mips_elf_link_hash_table *htab;
3062 struct mips_got_entry *entry;
3064 htab = mips_elf_hash_table (info);
3065 BFD_ASSERT (htab != NULL);
3067 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3068 r_symndx, h, r_type);
3069 if (!entry)
3070 return MINUS_ONE;
3072 if (TLS_RELOC_P (r_type))
3074 if (entry->symndx == -1 && htab->got_info->next == NULL)
3075 /* A type (3) entry in the single-GOT case. We use the symbol's
3076 hash table entry to track the index. */
3077 return mips_tls_got_index (abfd, h->tls_got_offset, &h->tls_type,
3078 r_type, info, h, value);
3079 else
3080 return mips_tls_got_index (abfd, entry->gotidx, &entry->tls_type,
3081 r_type, info, h, value);
3083 else
3084 return entry->gotidx;
3087 /* Returns the GOT index for the global symbol indicated by H. */
3089 static bfd_vma
3090 mips_elf_global_got_index (bfd *abfd, bfd *ibfd, struct elf_link_hash_entry *h,
3091 int r_type, struct bfd_link_info *info)
3093 struct mips_elf_link_hash_table *htab;
3094 bfd_vma got_index;
3095 struct mips_got_info *g, *gg;
3096 long global_got_dynindx = 0;
3098 htab = mips_elf_hash_table (info);
3099 BFD_ASSERT (htab != NULL);
3101 gg = g = htab->got_info;
3102 if (g->bfd2got && ibfd)
3104 struct mips_got_entry e, *p;
3106 BFD_ASSERT (h->dynindx >= 0);
3108 g = mips_elf_got_for_ibfd (g, ibfd);
3109 if (g->next != gg || TLS_RELOC_P (r_type))
3111 e.abfd = ibfd;
3112 e.symndx = -1;
3113 e.d.h = (struct mips_elf_link_hash_entry *)h;
3114 e.tls_type = 0;
3116 p = htab_find (g->got_entries, &e);
3118 BFD_ASSERT (p->gotidx > 0);
3120 if (TLS_RELOC_P (r_type))
3122 bfd_vma value = MINUS_ONE;
3123 if ((h->root.type == bfd_link_hash_defined
3124 || h->root.type == bfd_link_hash_defweak)
3125 && h->root.u.def.section->output_section)
3126 value = (h->root.u.def.value
3127 + h->root.u.def.section->output_offset
3128 + h->root.u.def.section->output_section->vma);
3130 return mips_tls_got_index (abfd, p->gotidx, &p->tls_type, r_type,
3131 info, e.d.h, value);
3133 else
3134 return p->gotidx;
3138 if (gg->global_gotsym != NULL)
3139 global_got_dynindx = gg->global_gotsym->dynindx;
3141 if (TLS_RELOC_P (r_type))
3143 struct mips_elf_link_hash_entry *hm
3144 = (struct mips_elf_link_hash_entry *) h;
3145 bfd_vma value = MINUS_ONE;
3147 if ((h->root.type == bfd_link_hash_defined
3148 || h->root.type == bfd_link_hash_defweak)
3149 && h->root.u.def.section->output_section)
3150 value = (h->root.u.def.value
3151 + h->root.u.def.section->output_offset
3152 + h->root.u.def.section->output_section->vma);
3154 got_index = mips_tls_got_index (abfd, hm->tls_got_offset, &hm->tls_type,
3155 r_type, info, hm, value);
3157 else
3159 /* Once we determine the global GOT entry with the lowest dynamic
3160 symbol table index, we must put all dynamic symbols with greater
3161 indices into the GOT. That makes it easy to calculate the GOT
3162 offset. */
3163 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3164 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3165 * MIPS_ELF_GOT_SIZE (abfd));
3167 BFD_ASSERT (got_index < htab->sgot->size);
3169 return got_index;
3172 /* Find a GOT page entry that points to within 32KB of VALUE. These
3173 entries are supposed to be placed at small offsets in the GOT, i.e.,
3174 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3175 entry could be created. If OFFSETP is nonnull, use it to return the
3176 offset of the GOT entry from VALUE. */
3178 static bfd_vma
3179 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3180 bfd_vma value, bfd_vma *offsetp)
3182 bfd_vma page, got_index;
3183 struct mips_got_entry *entry;
3185 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3186 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3187 NULL, R_MIPS_GOT_PAGE);
3189 if (!entry)
3190 return MINUS_ONE;
3192 got_index = entry->gotidx;
3194 if (offsetp)
3195 *offsetp = value - entry->d.address;
3197 return got_index;
3200 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3201 EXTERNAL is true if the relocation was originally against a global
3202 symbol that binds locally. */
3204 static bfd_vma
3205 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3206 bfd_vma value, bfd_boolean external)
3208 struct mips_got_entry *entry;
3210 /* GOT16 relocations against local symbols are followed by a LO16
3211 relocation; those against global symbols are not. Thus if the
3212 symbol was originally local, the GOT16 relocation should load the
3213 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3214 if (! external)
3215 value = mips_elf_high (value) << 16;
3217 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3218 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3219 same in all cases. */
3220 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3221 NULL, R_MIPS_GOT16);
3222 if (entry)
3223 return entry->gotidx;
3224 else
3225 return MINUS_ONE;
3228 /* Returns the offset for the entry at the INDEXth position
3229 in the GOT. */
3231 static bfd_vma
3232 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3233 bfd *input_bfd, bfd_vma got_index)
3235 struct mips_elf_link_hash_table *htab;
3236 asection *sgot;
3237 bfd_vma gp;
3239 htab = mips_elf_hash_table (info);
3240 BFD_ASSERT (htab != NULL);
3242 sgot = htab->sgot;
3243 gp = _bfd_get_gp_value (output_bfd)
3244 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3246 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3249 /* Create and return a local GOT entry for VALUE, which was calculated
3250 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3251 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3252 instead. */
3254 static struct mips_got_entry *
3255 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3256 bfd *ibfd, bfd_vma value,
3257 unsigned long r_symndx,
3258 struct mips_elf_link_hash_entry *h,
3259 int r_type)
3261 struct mips_got_entry entry, **loc;
3262 struct mips_got_info *g;
3263 struct mips_elf_link_hash_table *htab;
3265 htab = mips_elf_hash_table (info);
3266 BFD_ASSERT (htab != NULL);
3268 entry.abfd = NULL;
3269 entry.symndx = -1;
3270 entry.d.address = value;
3271 entry.tls_type = 0;
3273 g = mips_elf_got_for_ibfd (htab->got_info, ibfd);
3274 if (g == NULL)
3276 g = mips_elf_got_for_ibfd (htab->got_info, abfd);
3277 BFD_ASSERT (g != NULL);
3280 /* This function shouldn't be called for symbols that live in the global
3281 area of the GOT. */
3282 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3283 if (TLS_RELOC_P (r_type))
3285 struct mips_got_entry *p;
3287 entry.abfd = ibfd;
3288 if (r_type == R_MIPS_TLS_LDM)
3290 entry.tls_type = GOT_TLS_LDM;
3291 entry.symndx = 0;
3292 entry.d.addend = 0;
3294 else if (h == NULL)
3296 entry.symndx = r_symndx;
3297 entry.d.addend = 0;
3299 else
3300 entry.d.h = h;
3302 p = (struct mips_got_entry *)
3303 htab_find (g->got_entries, &entry);
3305 BFD_ASSERT (p);
3306 return p;
3309 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3310 INSERT);
3311 if (*loc)
3312 return *loc;
3314 entry.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3315 entry.tls_type = 0;
3317 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3319 if (! *loc)
3320 return NULL;
3322 memcpy (*loc, &entry, sizeof entry);
3324 if (g->assigned_gotno > g->local_gotno)
3326 (*loc)->gotidx = -1;
3327 /* We didn't allocate enough space in the GOT. */
3328 (*_bfd_error_handler)
3329 (_("not enough GOT space for local GOT entries"));
3330 bfd_set_error (bfd_error_bad_value);
3331 return NULL;
3334 MIPS_ELF_PUT_WORD (abfd, value,
3335 (htab->sgot->contents + entry.gotidx));
3337 /* These GOT entries need a dynamic relocation on VxWorks. */
3338 if (htab->is_vxworks)
3340 Elf_Internal_Rela outrel;
3341 asection *s;
3342 bfd_byte *rloc;
3343 bfd_vma got_address;
3345 s = mips_elf_rel_dyn_section (info, FALSE);
3346 got_address = (htab->sgot->output_section->vma
3347 + htab->sgot->output_offset
3348 + entry.gotidx);
3350 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3351 outrel.r_offset = got_address;
3352 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3353 outrel.r_addend = value;
3354 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3357 return *loc;
3360 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3361 The number might be exact or a worst-case estimate, depending on how
3362 much information is available to elf_backend_omit_section_dynsym at
3363 the current linking stage. */
3365 static bfd_size_type
3366 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3368 bfd_size_type count;
3370 count = 0;
3371 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3373 asection *p;
3374 const struct elf_backend_data *bed;
3376 bed = get_elf_backend_data (output_bfd);
3377 for (p = output_bfd->sections; p ; p = p->next)
3378 if ((p->flags & SEC_EXCLUDE) == 0
3379 && (p->flags & SEC_ALLOC) != 0
3380 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3381 ++count;
3383 return count;
3386 /* Sort the dynamic symbol table so that symbols that need GOT entries
3387 appear towards the end. */
3389 static bfd_boolean
3390 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3392 struct mips_elf_link_hash_table *htab;
3393 struct mips_elf_hash_sort_data hsd;
3394 struct mips_got_info *g;
3396 if (elf_hash_table (info)->dynsymcount == 0)
3397 return TRUE;
3399 htab = mips_elf_hash_table (info);
3400 BFD_ASSERT (htab != NULL);
3402 g = htab->got_info;
3403 if (g == NULL)
3404 return TRUE;
3406 hsd.low = NULL;
3407 hsd.max_unref_got_dynindx
3408 = hsd.min_got_dynindx
3409 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3410 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3411 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3412 elf_hash_table (info)),
3413 mips_elf_sort_hash_table_f,
3414 &hsd);
3416 /* There should have been enough room in the symbol table to
3417 accommodate both the GOT and non-GOT symbols. */
3418 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3419 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3420 == elf_hash_table (info)->dynsymcount);
3421 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3422 == g->global_gotno);
3424 /* Now we know which dynamic symbol has the lowest dynamic symbol
3425 table index in the GOT. */
3426 g->global_gotsym = hsd.low;
3428 return TRUE;
3431 /* If H needs a GOT entry, assign it the highest available dynamic
3432 index. Otherwise, assign it the lowest available dynamic
3433 index. */
3435 static bfd_boolean
3436 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3438 struct mips_elf_hash_sort_data *hsd = data;
3440 if (h->root.root.type == bfd_link_hash_warning)
3441 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3443 /* Symbols without dynamic symbol table entries aren't interesting
3444 at all. */
3445 if (h->root.dynindx == -1)
3446 return TRUE;
3448 switch (h->global_got_area)
3450 case GGA_NONE:
3451 h->root.dynindx = hsd->max_non_got_dynindx++;
3452 break;
3454 case GGA_NORMAL:
3455 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3457 h->root.dynindx = --hsd->min_got_dynindx;
3458 hsd->low = (struct elf_link_hash_entry *) h;
3459 break;
3461 case GGA_RELOC_ONLY:
3462 BFD_ASSERT (h->tls_type == GOT_NORMAL);
3464 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3465 hsd->low = (struct elf_link_hash_entry *) h;
3466 h->root.dynindx = hsd->max_unref_got_dynindx++;
3467 break;
3470 return TRUE;
3473 /* If H is a symbol that needs a global GOT entry, but has a dynamic
3474 symbol table index lower than any we've seen to date, record it for
3475 posterity. FOR_CALL is true if the caller is only interested in
3476 using the GOT entry for calls. */
3478 static bfd_boolean
3479 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3480 bfd *abfd, struct bfd_link_info *info,
3481 bfd_boolean for_call,
3482 unsigned char tls_flag)
3484 struct mips_elf_link_hash_table *htab;
3485 struct mips_elf_link_hash_entry *hmips;
3486 struct mips_got_entry entry, **loc;
3487 struct mips_got_info *g;
3489 htab = mips_elf_hash_table (info);
3490 BFD_ASSERT (htab != NULL);
3492 hmips = (struct mips_elf_link_hash_entry *) h;
3493 if (!for_call)
3494 hmips->got_only_for_calls = FALSE;
3496 /* A global symbol in the GOT must also be in the dynamic symbol
3497 table. */
3498 if (h->dynindx == -1)
3500 switch (ELF_ST_VISIBILITY (h->other))
3502 case STV_INTERNAL:
3503 case STV_HIDDEN:
3504 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3505 break;
3507 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3508 return FALSE;
3511 /* Make sure we have a GOT to put this entry into. */
3512 g = htab->got_info;
3513 BFD_ASSERT (g != NULL);
3515 entry.abfd = abfd;
3516 entry.symndx = -1;
3517 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3518 entry.tls_type = 0;
3520 loc = (struct mips_got_entry **) htab_find_slot (g->got_entries, &entry,
3521 INSERT);
3523 /* If we've already marked this entry as needing GOT space, we don't
3524 need to do it again. */
3525 if (*loc)
3527 (*loc)->tls_type |= tls_flag;
3528 return TRUE;
3531 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3533 if (! *loc)
3534 return FALSE;
3536 entry.gotidx = -1;
3537 entry.tls_type = tls_flag;
3539 memcpy (*loc, &entry, sizeof entry);
3541 if (tls_flag == 0)
3542 hmips->global_got_area = GGA_NORMAL;
3544 return TRUE;
3547 /* Reserve space in G for a GOT entry containing the value of symbol
3548 SYMNDX in input bfd ABDF, plus ADDEND. */
3550 static bfd_boolean
3551 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3552 struct bfd_link_info *info,
3553 unsigned char tls_flag)
3555 struct mips_elf_link_hash_table *htab;
3556 struct mips_got_info *g;
3557 struct mips_got_entry entry, **loc;
3559 htab = mips_elf_hash_table (info);
3560 BFD_ASSERT (htab != NULL);
3562 g = htab->got_info;
3563 BFD_ASSERT (g != NULL);
3565 entry.abfd = abfd;
3566 entry.symndx = symndx;
3567 entry.d.addend = addend;
3568 entry.tls_type = tls_flag;
3569 loc = (struct mips_got_entry **)
3570 htab_find_slot (g->got_entries, &entry, INSERT);
3572 if (*loc)
3574 if (tls_flag == GOT_TLS_GD && !((*loc)->tls_type & GOT_TLS_GD))
3576 g->tls_gotno += 2;
3577 (*loc)->tls_type |= tls_flag;
3579 else if (tls_flag == GOT_TLS_IE && !((*loc)->tls_type & GOT_TLS_IE))
3581 g->tls_gotno += 1;
3582 (*loc)->tls_type |= tls_flag;
3584 return TRUE;
3587 if (tls_flag != 0)
3589 entry.gotidx = -1;
3590 entry.tls_type = tls_flag;
3591 if (tls_flag == GOT_TLS_IE)
3592 g->tls_gotno += 1;
3593 else if (tls_flag == GOT_TLS_GD)
3594 g->tls_gotno += 2;
3595 else if (g->tls_ldm_offset == MINUS_ONE)
3597 g->tls_ldm_offset = MINUS_TWO;
3598 g->tls_gotno += 2;
3601 else
3603 entry.gotidx = g->local_gotno++;
3604 entry.tls_type = 0;
3607 *loc = (struct mips_got_entry *)bfd_alloc (abfd, sizeof entry);
3609 if (! *loc)
3610 return FALSE;
3612 memcpy (*loc, &entry, sizeof entry);
3614 return TRUE;
3617 /* Return the maximum number of GOT page entries required for RANGE. */
3619 static bfd_vma
3620 mips_elf_pages_for_range (const struct mips_got_page_range *range)
3622 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
3625 /* Record that ABFD has a page relocation against symbol SYMNDX and
3626 that ADDEND is the addend for that relocation.
3628 This function creates an upper bound on the number of GOT slots
3629 required; no attempt is made to combine references to non-overridable
3630 global symbols across multiple input files. */
3632 static bfd_boolean
3633 mips_elf_record_got_page_entry (struct bfd_link_info *info, bfd *abfd,
3634 long symndx, bfd_signed_vma addend)
3636 struct mips_elf_link_hash_table *htab;
3637 struct mips_got_info *g;
3638 struct mips_got_page_entry lookup, *entry;
3639 struct mips_got_page_range **range_ptr, *range;
3640 bfd_vma old_pages, new_pages;
3641 void **loc;
3643 htab = mips_elf_hash_table (info);
3644 BFD_ASSERT (htab != NULL);
3646 g = htab->got_info;
3647 BFD_ASSERT (g != NULL);
3649 /* Find the mips_got_page_entry hash table entry for this symbol. */
3650 lookup.abfd = abfd;
3651 lookup.symndx = symndx;
3652 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
3653 if (loc == NULL)
3654 return FALSE;
3656 /* Create a mips_got_page_entry if this is the first time we've
3657 seen the symbol. */
3658 entry = (struct mips_got_page_entry *) *loc;
3659 if (!entry)
3661 entry = bfd_alloc (abfd, sizeof (*entry));
3662 if (!entry)
3663 return FALSE;
3665 entry->abfd = abfd;
3666 entry->symndx = symndx;
3667 entry->ranges = NULL;
3668 entry->num_pages = 0;
3669 *loc = entry;
3672 /* Skip over ranges whose maximum extent cannot share a page entry
3673 with ADDEND. */
3674 range_ptr = &entry->ranges;
3675 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
3676 range_ptr = &(*range_ptr)->next;
3678 /* If we scanned to the end of the list, or found a range whose
3679 minimum extent cannot share a page entry with ADDEND, create
3680 a new singleton range. */
3681 range = *range_ptr;
3682 if (!range || addend < range->min_addend - 0xffff)
3684 range = bfd_alloc (abfd, sizeof (*range));
3685 if (!range)
3686 return FALSE;
3688 range->next = *range_ptr;
3689 range->min_addend = addend;
3690 range->max_addend = addend;
3692 *range_ptr = range;
3693 entry->num_pages++;
3694 g->page_gotno++;
3695 return TRUE;
3698 /* Remember how many pages the old range contributed. */
3699 old_pages = mips_elf_pages_for_range (range);
3701 /* Update the ranges. */
3702 if (addend < range->min_addend)
3703 range->min_addend = addend;
3704 else if (addend > range->max_addend)
3706 if (range->next && addend >= range->next->min_addend - 0xffff)
3708 old_pages += mips_elf_pages_for_range (range->next);
3709 range->max_addend = range->next->max_addend;
3710 range->next = range->next->next;
3712 else
3713 range->max_addend = addend;
3716 /* Record any change in the total estimate. */
3717 new_pages = mips_elf_pages_for_range (range);
3718 if (old_pages != new_pages)
3720 entry->num_pages += new_pages - old_pages;
3721 g->page_gotno += new_pages - old_pages;
3724 return TRUE;
3727 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3729 static void
3730 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3731 unsigned int n)
3733 asection *s;
3734 struct mips_elf_link_hash_table *htab;
3736 htab = mips_elf_hash_table (info);
3737 BFD_ASSERT (htab != NULL);
3739 s = mips_elf_rel_dyn_section (info, FALSE);
3740 BFD_ASSERT (s != NULL);
3742 if (htab->is_vxworks)
3743 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3744 else
3746 if (s->size == 0)
3748 /* Make room for a null element. */
3749 s->size += MIPS_ELF_REL_SIZE (abfd);
3750 ++s->reloc_count;
3752 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3756 /* A htab_traverse callback for GOT entries. Set boolean *DATA to true
3757 if the GOT entry is for an indirect or warning symbol. */
3759 static int
3760 mips_elf_check_recreate_got (void **entryp, void *data)
3762 struct mips_got_entry *entry;
3763 bfd_boolean *must_recreate;
3765 entry = (struct mips_got_entry *) *entryp;
3766 must_recreate = (bfd_boolean *) data;
3767 if (entry->abfd != NULL && entry->symndx == -1)
3769 struct mips_elf_link_hash_entry *h;
3771 h = entry->d.h;
3772 if (h->root.root.type == bfd_link_hash_indirect
3773 || h->root.root.type == bfd_link_hash_warning)
3775 *must_recreate = TRUE;
3776 return 0;
3779 return 1;
3782 /* A htab_traverse callback for GOT entries. Add all entries to
3783 hash table *DATA, converting entries for indirect and warning
3784 symbols into entries for the target symbol. Set *DATA to null
3785 on error. */
3787 static int
3788 mips_elf_recreate_got (void **entryp, void *data)
3790 htab_t *new_got;
3791 struct mips_got_entry *entry;
3792 void **slot;
3794 new_got = (htab_t *) data;
3795 entry = (struct mips_got_entry *) *entryp;
3796 if (entry->abfd != NULL && entry->symndx == -1)
3798 struct mips_elf_link_hash_entry *h;
3800 h = entry->d.h;
3801 while (h->root.root.type == bfd_link_hash_indirect
3802 || h->root.root.type == bfd_link_hash_warning)
3804 BFD_ASSERT (h->global_got_area == GGA_NONE);
3805 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
3807 entry->d.h = h;
3809 slot = htab_find_slot (*new_got, entry, INSERT);
3810 if (slot == NULL)
3812 *new_got = NULL;
3813 return 0;
3815 if (*slot == NULL)
3816 *slot = entry;
3817 else
3818 free (entry);
3819 return 1;
3822 /* If any entries in G->got_entries are for indirect or warning symbols,
3823 replace them with entries for the target symbol. */
3825 static bfd_boolean
3826 mips_elf_resolve_final_got_entries (struct mips_got_info *g)
3828 bfd_boolean must_recreate;
3829 htab_t new_got;
3831 must_recreate = FALSE;
3832 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &must_recreate);
3833 if (must_recreate)
3835 new_got = htab_create (htab_size (g->got_entries),
3836 mips_elf_got_entry_hash,
3837 mips_elf_got_entry_eq, NULL);
3838 htab_traverse (g->got_entries, mips_elf_recreate_got, &new_got);
3839 if (new_got == NULL)
3840 return FALSE;
3842 /* Each entry in g->got_entries has either been copied to new_got
3843 or freed. Now delete the hash table itself. */
3844 htab_delete (g->got_entries);
3845 g->got_entries = new_got;
3847 return TRUE;
3850 /* A mips_elf_link_hash_traverse callback for which DATA points
3851 to the link_info structure. Count the number of type (3) entries
3852 in the master GOT. */
3854 static int
3855 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
3857 struct bfd_link_info *info;
3858 struct mips_elf_link_hash_table *htab;
3859 struct mips_got_info *g;
3861 info = (struct bfd_link_info *) data;
3862 htab = mips_elf_hash_table (info);
3863 g = htab->got_info;
3864 if (h->global_got_area != GGA_NONE)
3866 /* Make a final decision about whether the symbol belongs in the
3867 local or global GOT. Symbols that bind locally can (and in the
3868 case of forced-local symbols, must) live in the local GOT.
3869 Those that are aren't in the dynamic symbol table must also
3870 live in the local GOT.
3872 Note that the former condition does not always imply the
3873 latter: symbols do not bind locally if they are completely
3874 undefined. We'll report undefined symbols later if appropriate. */
3875 if (h->root.dynindx == -1
3876 || (h->got_only_for_calls
3877 ? SYMBOL_CALLS_LOCAL (info, &h->root)
3878 : SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3880 /* The symbol belongs in the local GOT. We no longer need this
3881 entry if it was only used for relocations; those relocations
3882 will be against the null or section symbol instead of H. */
3883 if (h->global_got_area != GGA_RELOC_ONLY)
3884 g->local_gotno++;
3885 h->global_got_area = GGA_NONE;
3887 else if (htab->is_vxworks
3888 && h->got_only_for_calls
3889 && h->root.plt.offset != MINUS_ONE)
3890 /* On VxWorks, calls can refer directly to the .got.plt entry;
3891 they don't need entries in the regular GOT. .got.plt entries
3892 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
3893 h->global_got_area = GGA_NONE;
3894 else
3896 g->global_gotno++;
3897 if (h->global_got_area == GGA_RELOC_ONLY)
3898 g->reloc_only_gotno++;
3901 return 1;
3904 /* Compute the hash value of the bfd in a bfd2got hash entry. */
3906 static hashval_t
3907 mips_elf_bfd2got_entry_hash (const void *entry_)
3909 const struct mips_elf_bfd2got_hash *entry
3910 = (struct mips_elf_bfd2got_hash *)entry_;
3912 return entry->bfd->id;
3915 /* Check whether two hash entries have the same bfd. */
3917 static int
3918 mips_elf_bfd2got_entry_eq (const void *entry1, const void *entry2)
3920 const struct mips_elf_bfd2got_hash *e1
3921 = (const struct mips_elf_bfd2got_hash *)entry1;
3922 const struct mips_elf_bfd2got_hash *e2
3923 = (const struct mips_elf_bfd2got_hash *)entry2;
3925 return e1->bfd == e2->bfd;
3928 /* In a multi-got link, determine the GOT to be used for IBFD. G must
3929 be the master GOT data. */
3931 static struct mips_got_info *
3932 mips_elf_got_for_ibfd (struct mips_got_info *g, bfd *ibfd)
3934 struct mips_elf_bfd2got_hash e, *p;
3936 if (! g->bfd2got)
3937 return g;
3939 e.bfd = ibfd;
3940 p = htab_find (g->bfd2got, &e);
3941 return p ? p->g : NULL;
3944 /* Use BFD2GOT to find ABFD's got entry, creating one if none exists.
3945 Return NULL if an error occured. */
3947 static struct mips_got_info *
3948 mips_elf_get_got_for_bfd (struct htab *bfd2got, bfd *output_bfd,
3949 bfd *input_bfd)
3951 struct mips_elf_bfd2got_hash bfdgot_entry, *bfdgot;
3952 struct mips_got_info *g;
3953 void **bfdgotp;
3955 bfdgot_entry.bfd = input_bfd;
3956 bfdgotp = htab_find_slot (bfd2got, &bfdgot_entry, INSERT);
3957 bfdgot = (struct mips_elf_bfd2got_hash *) *bfdgotp;
3959 if (bfdgot == NULL)
3961 bfdgot = ((struct mips_elf_bfd2got_hash *)
3962 bfd_alloc (output_bfd, sizeof (struct mips_elf_bfd2got_hash)));
3963 if (bfdgot == NULL)
3964 return NULL;
3966 *bfdgotp = bfdgot;
3968 g = ((struct mips_got_info *)
3969 bfd_alloc (output_bfd, sizeof (struct mips_got_info)));
3970 if (g == NULL)
3971 return NULL;
3973 bfdgot->bfd = input_bfd;
3974 bfdgot->g = g;
3976 g->global_gotsym = NULL;
3977 g->global_gotno = 0;
3978 g->reloc_only_gotno = 0;
3979 g->local_gotno = 0;
3980 g->page_gotno = 0;
3981 g->assigned_gotno = -1;
3982 g->tls_gotno = 0;
3983 g->tls_assigned_gotno = 0;
3984 g->tls_ldm_offset = MINUS_ONE;
3985 g->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
3986 mips_elf_multi_got_entry_eq, NULL);
3987 if (g->got_entries == NULL)
3988 return NULL;
3990 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
3991 mips_got_page_entry_eq, NULL);
3992 if (g->got_page_entries == NULL)
3993 return NULL;
3995 g->bfd2got = NULL;
3996 g->next = NULL;
3999 return bfdgot->g;
4002 /* A htab_traverse callback for the entries in the master got.
4003 Create one separate got for each bfd that has entries in the global
4004 got, such that we can tell how many local and global entries each
4005 bfd requires. */
4007 static int
4008 mips_elf_make_got_per_bfd (void **entryp, void *p)
4010 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4011 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4012 struct mips_got_info *g;
4014 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4015 if (g == NULL)
4017 arg->obfd = NULL;
4018 return 0;
4021 /* Insert the GOT entry in the bfd's got entry hash table. */
4022 entryp = htab_find_slot (g->got_entries, entry, INSERT);
4023 if (*entryp != NULL)
4024 return 1;
4026 *entryp = entry;
4028 if (entry->tls_type)
4030 if (entry->tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4031 g->tls_gotno += 2;
4032 if (entry->tls_type & GOT_TLS_IE)
4033 g->tls_gotno += 1;
4035 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
4036 ++g->local_gotno;
4037 else
4038 ++g->global_gotno;
4040 return 1;
4043 /* A htab_traverse callback for the page entries in the master got.
4044 Associate each page entry with the bfd's got. */
4046 static int
4047 mips_elf_make_got_pages_per_bfd (void **entryp, void *p)
4049 struct mips_got_page_entry *entry = (struct mips_got_page_entry *) *entryp;
4050 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *) p;
4051 struct mips_got_info *g;
4053 g = mips_elf_get_got_for_bfd (arg->bfd2got, arg->obfd, entry->abfd);
4054 if (g == NULL)
4056 arg->obfd = NULL;
4057 return 0;
4060 /* Insert the GOT entry in the bfd's got entry hash table. */
4061 entryp = htab_find_slot (g->got_page_entries, entry, INSERT);
4062 if (*entryp != NULL)
4063 return 1;
4065 *entryp = entry;
4066 g->page_gotno += entry->num_pages;
4067 return 1;
4070 /* Consider merging the got described by BFD2GOT with TO, using the
4071 information given by ARG. Return -1 if this would lead to overflow,
4072 1 if they were merged successfully, and 0 if a merge failed due to
4073 lack of memory. (These values are chosen so that nonnegative return
4074 values can be returned by a htab_traverse callback.) */
4076 static int
4077 mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
4078 struct mips_got_info *to,
4079 struct mips_elf_got_per_bfd_arg *arg)
4081 struct mips_got_info *from = bfd2got->g;
4082 unsigned int estimate;
4084 /* Work out how many page entries we would need for the combined GOT. */
4085 estimate = arg->max_pages;
4086 if (estimate >= from->page_gotno + to->page_gotno)
4087 estimate = from->page_gotno + to->page_gotno;
4089 /* And conservatively estimate how many local and TLS entries
4090 would be needed. */
4091 estimate += from->local_gotno + to->local_gotno;
4092 estimate += from->tls_gotno + to->tls_gotno;
4094 /* If we're merging with the primary got, we will always have
4095 the full set of global entries. Otherwise estimate those
4096 conservatively as well. */
4097 if (to == arg->primary)
4098 estimate += arg->global_count;
4099 else
4100 estimate += from->global_gotno + to->global_gotno;
4102 /* Bail out if the combined GOT might be too big. */
4103 if (estimate > arg->max_count)
4104 return -1;
4106 /* Commit to the merge. Record that TO is now the bfd for this got. */
4107 bfd2got->g = to;
4109 /* Transfer the bfd's got information from FROM to TO. */
4110 htab_traverse (from->got_entries, mips_elf_make_got_per_bfd, arg);
4111 if (arg->obfd == NULL)
4112 return 0;
4114 htab_traverse (from->got_page_entries, mips_elf_make_got_pages_per_bfd, arg);
4115 if (arg->obfd == NULL)
4116 return 0;
4118 /* We don't have to worry about releasing memory of the actual
4119 got entries, since they're all in the master got_entries hash
4120 table anyway. */
4121 htab_delete (from->got_entries);
4122 htab_delete (from->got_page_entries);
4123 return 1;
4126 /* Attempt to merge gots of different input bfds. Try to use as much
4127 as possible of the primary got, since it doesn't require explicit
4128 dynamic relocations, but don't use bfds that would reference global
4129 symbols out of the addressable range. Failing the primary got,
4130 attempt to merge with the current got, or finish the current got
4131 and then make make the new got current. */
4133 static int
4134 mips_elf_merge_gots (void **bfd2got_, void *p)
4136 struct mips_elf_bfd2got_hash *bfd2got
4137 = (struct mips_elf_bfd2got_hash *)*bfd2got_;
4138 struct mips_elf_got_per_bfd_arg *arg = (struct mips_elf_got_per_bfd_arg *)p;
4139 struct mips_got_info *g;
4140 unsigned int estimate;
4141 int result;
4143 g = bfd2got->g;
4145 /* Work out the number of page, local and TLS entries. */
4146 estimate = arg->max_pages;
4147 if (estimate > g->page_gotno)
4148 estimate = g->page_gotno;
4149 estimate += g->local_gotno + g->tls_gotno;
4151 /* We place TLS GOT entries after both locals and globals. The globals
4152 for the primary GOT may overflow the normal GOT size limit, so be
4153 sure not to merge a GOT which requires TLS with the primary GOT in that
4154 case. This doesn't affect non-primary GOTs. */
4155 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4157 if (estimate <= arg->max_count)
4159 /* If we don't have a primary GOT, use it as
4160 a starting point for the primary GOT. */
4161 if (!arg->primary)
4163 arg->primary = bfd2got->g;
4164 return 1;
4167 /* Try merging with the primary GOT. */
4168 result = mips_elf_merge_got_with (bfd2got, arg->primary, arg);
4169 if (result >= 0)
4170 return result;
4173 /* If we can merge with the last-created got, do it. */
4174 if (arg->current)
4176 result = mips_elf_merge_got_with (bfd2got, arg->current, arg);
4177 if (result >= 0)
4178 return result;
4181 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4182 fits; if it turns out that it doesn't, we'll get relocation
4183 overflows anyway. */
4184 g->next = arg->current;
4185 arg->current = g;
4187 return 1;
4190 /* Set the TLS GOT index for the GOT entry in ENTRYP. ENTRYP's NEXT field
4191 is null iff there is just a single GOT. */
4193 static int
4194 mips_elf_initialize_tls_index (void **entryp, void *p)
4196 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4197 struct mips_got_info *g = p;
4198 bfd_vma next_index;
4199 unsigned char tls_type;
4201 /* We're only interested in TLS symbols. */
4202 if (entry->tls_type == 0)
4203 return 1;
4205 next_index = MIPS_ELF_GOT_SIZE (entry->abfd) * (long) g->tls_assigned_gotno;
4207 if (entry->symndx == -1 && g->next == NULL)
4209 /* A type (3) got entry in the single-GOT case. We use the symbol's
4210 hash table entry to track its index. */
4211 if (entry->d.h->tls_type & GOT_TLS_OFFSET_DONE)
4212 return 1;
4213 entry->d.h->tls_type |= GOT_TLS_OFFSET_DONE;
4214 entry->d.h->tls_got_offset = next_index;
4215 tls_type = entry->d.h->tls_type;
4217 else
4219 if (entry->tls_type & GOT_TLS_LDM)
4221 /* There are separate mips_got_entry objects for each input bfd
4222 that requires an LDM entry. Make sure that all LDM entries in
4223 a GOT resolve to the same index. */
4224 if (g->tls_ldm_offset != MINUS_TWO && g->tls_ldm_offset != MINUS_ONE)
4226 entry->gotidx = g->tls_ldm_offset;
4227 return 1;
4229 g->tls_ldm_offset = next_index;
4231 entry->gotidx = next_index;
4232 tls_type = entry->tls_type;
4235 /* Account for the entries we've just allocated. */
4236 if (tls_type & (GOT_TLS_GD | GOT_TLS_LDM))
4237 g->tls_assigned_gotno += 2;
4238 if (tls_type & GOT_TLS_IE)
4239 g->tls_assigned_gotno += 1;
4241 return 1;
4244 /* If passed a NULL mips_got_info in the argument, set the marker used
4245 to tell whether a global symbol needs a got entry (in the primary
4246 got) to the given VALUE.
4248 If passed a pointer G to a mips_got_info in the argument (it must
4249 not be the primary GOT), compute the offset from the beginning of
4250 the (primary) GOT section to the entry in G corresponding to the
4251 global symbol. G's assigned_gotno must contain the index of the
4252 first available global GOT entry in G. VALUE must contain the size
4253 of a GOT entry in bytes. For each global GOT entry that requires a
4254 dynamic relocation, NEEDED_RELOCS is incremented, and the symbol is
4255 marked as not eligible for lazy resolution through a function
4256 stub. */
4257 static int
4258 mips_elf_set_global_got_offset (void **entryp, void *p)
4260 struct mips_got_entry *entry = (struct mips_got_entry *)*entryp;
4261 struct mips_elf_set_global_got_offset_arg *arg
4262 = (struct mips_elf_set_global_got_offset_arg *)p;
4263 struct mips_got_info *g = arg->g;
4265 if (g && entry->tls_type != GOT_NORMAL)
4266 arg->needed_relocs +=
4267 mips_tls_got_relocs (arg->info, entry->tls_type,
4268 entry->symndx == -1 ? &entry->d.h->root : NULL);
4270 if (entry->abfd != NULL
4271 && entry->symndx == -1
4272 && entry->d.h->global_got_area != GGA_NONE)
4274 if (g)
4276 BFD_ASSERT (g->global_gotsym == NULL);
4278 entry->gotidx = arg->value * (long) g->assigned_gotno++;
4279 if (arg->info->shared
4280 || (elf_hash_table (arg->info)->dynamic_sections_created
4281 && entry->d.h->root.def_dynamic
4282 && !entry->d.h->root.def_regular))
4283 ++arg->needed_relocs;
4285 else
4286 entry->d.h->global_got_area = arg->value;
4289 return 1;
4292 /* A htab_traverse callback for GOT entries for which DATA is the
4293 bfd_link_info. Forbid any global symbols from having traditional
4294 lazy-binding stubs. */
4296 static int
4297 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4299 struct bfd_link_info *info;
4300 struct mips_elf_link_hash_table *htab;
4301 struct mips_got_entry *entry;
4303 entry = (struct mips_got_entry *) *entryp;
4304 info = (struct bfd_link_info *) data;
4305 htab = mips_elf_hash_table (info);
4306 BFD_ASSERT (htab != NULL);
4308 if (entry->abfd != NULL
4309 && entry->symndx == -1
4310 && entry->d.h->needs_lazy_stub)
4312 entry->d.h->needs_lazy_stub = FALSE;
4313 htab->lazy_stub_count--;
4316 return 1;
4319 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4320 the primary GOT. */
4321 static bfd_vma
4322 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4324 if (g->bfd2got == NULL)
4325 return 0;
4327 g = mips_elf_got_for_ibfd (g, ibfd);
4328 if (! g)
4329 return 0;
4331 BFD_ASSERT (g->next);
4333 g = g->next;
4335 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4336 * MIPS_ELF_GOT_SIZE (abfd);
4339 /* Turn a single GOT that is too big for 16-bit addressing into
4340 a sequence of GOTs, each one 16-bit addressable. */
4342 static bfd_boolean
4343 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4344 asection *got, bfd_size_type pages)
4346 struct mips_elf_link_hash_table *htab;
4347 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4348 struct mips_elf_set_global_got_offset_arg set_got_offset_arg;
4349 struct mips_got_info *g, *gg;
4350 unsigned int assign, needed_relocs;
4351 bfd *dynobj;
4353 dynobj = elf_hash_table (info)->dynobj;
4354 htab = mips_elf_hash_table (info);
4355 BFD_ASSERT (htab != NULL);
4357 g = htab->got_info;
4358 g->bfd2got = htab_try_create (1, mips_elf_bfd2got_entry_hash,
4359 mips_elf_bfd2got_entry_eq, NULL);
4360 if (g->bfd2got == NULL)
4361 return FALSE;
4363 got_per_bfd_arg.bfd2got = g->bfd2got;
4364 got_per_bfd_arg.obfd = abfd;
4365 got_per_bfd_arg.info = info;
4367 /* Count how many GOT entries each input bfd requires, creating a
4368 map from bfd to got info while at that. */
4369 htab_traverse (g->got_entries, mips_elf_make_got_per_bfd, &got_per_bfd_arg);
4370 if (got_per_bfd_arg.obfd == NULL)
4371 return FALSE;
4373 /* Also count how many page entries each input bfd requires. */
4374 htab_traverse (g->got_page_entries, mips_elf_make_got_pages_per_bfd,
4375 &got_per_bfd_arg);
4376 if (got_per_bfd_arg.obfd == NULL)
4377 return FALSE;
4379 got_per_bfd_arg.current = NULL;
4380 got_per_bfd_arg.primary = NULL;
4381 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4382 / MIPS_ELF_GOT_SIZE (abfd))
4383 - htab->reserved_gotno);
4384 got_per_bfd_arg.max_pages = pages;
4385 /* The number of globals that will be included in the primary GOT.
4386 See the calls to mips_elf_set_global_got_offset below for more
4387 information. */
4388 got_per_bfd_arg.global_count = g->global_gotno;
4390 /* Try to merge the GOTs of input bfds together, as long as they
4391 don't seem to exceed the maximum GOT size, choosing one of them
4392 to be the primary GOT. */
4393 htab_traverse (g->bfd2got, mips_elf_merge_gots, &got_per_bfd_arg);
4394 if (got_per_bfd_arg.obfd == NULL)
4395 return FALSE;
4397 /* If we do not find any suitable primary GOT, create an empty one. */
4398 if (got_per_bfd_arg.primary == NULL)
4400 g->next = (struct mips_got_info *)
4401 bfd_alloc (abfd, sizeof (struct mips_got_info));
4402 if (g->next == NULL)
4403 return FALSE;
4405 g->next->global_gotsym = NULL;
4406 g->next->global_gotno = 0;
4407 g->next->reloc_only_gotno = 0;
4408 g->next->local_gotno = 0;
4409 g->next->page_gotno = 0;
4410 g->next->tls_gotno = 0;
4411 g->next->assigned_gotno = 0;
4412 g->next->tls_assigned_gotno = 0;
4413 g->next->tls_ldm_offset = MINUS_ONE;
4414 g->next->got_entries = htab_try_create (1, mips_elf_multi_got_entry_hash,
4415 mips_elf_multi_got_entry_eq,
4416 NULL);
4417 if (g->next->got_entries == NULL)
4418 return FALSE;
4419 g->next->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4420 mips_got_page_entry_eq,
4421 NULL);
4422 if (g->next->got_page_entries == NULL)
4423 return FALSE;
4424 g->next->bfd2got = NULL;
4426 else
4427 g->next = got_per_bfd_arg.primary;
4428 g->next->next = got_per_bfd_arg.current;
4430 /* GG is now the master GOT, and G is the primary GOT. */
4431 gg = g;
4432 g = g->next;
4434 /* Map the output bfd to the primary got. That's what we're going
4435 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4436 didn't mark in check_relocs, and we want a quick way to find it.
4437 We can't just use gg->next because we're going to reverse the
4438 list. */
4440 struct mips_elf_bfd2got_hash *bfdgot;
4441 void **bfdgotp;
4443 bfdgot = (struct mips_elf_bfd2got_hash *)bfd_alloc
4444 (abfd, sizeof (struct mips_elf_bfd2got_hash));
4446 if (bfdgot == NULL)
4447 return FALSE;
4449 bfdgot->bfd = abfd;
4450 bfdgot->g = g;
4451 bfdgotp = htab_find_slot (gg->bfd2got, bfdgot, INSERT);
4453 BFD_ASSERT (*bfdgotp == NULL);
4454 *bfdgotp = bfdgot;
4457 /* Every symbol that is referenced in a dynamic relocation must be
4458 present in the primary GOT, so arrange for them to appear after
4459 those that are actually referenced. */
4460 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4461 g->global_gotno = gg->global_gotno;
4463 set_got_offset_arg.g = NULL;
4464 set_got_offset_arg.value = GGA_RELOC_ONLY;
4465 htab_traverse (gg->got_entries, mips_elf_set_global_got_offset,
4466 &set_got_offset_arg);
4467 set_got_offset_arg.value = GGA_NORMAL;
4468 htab_traverse (g->got_entries, mips_elf_set_global_got_offset,
4469 &set_got_offset_arg);
4471 /* Now go through the GOTs assigning them offset ranges.
4472 [assigned_gotno, local_gotno[ will be set to the range of local
4473 entries in each GOT. We can then compute the end of a GOT by
4474 adding local_gotno to global_gotno. We reverse the list and make
4475 it circular since then we'll be able to quickly compute the
4476 beginning of a GOT, by computing the end of its predecessor. To
4477 avoid special cases for the primary GOT, while still preserving
4478 assertions that are valid for both single- and multi-got links,
4479 we arrange for the main got struct to have the right number of
4480 global entries, but set its local_gotno such that the initial
4481 offset of the primary GOT is zero. Remember that the primary GOT
4482 will become the last item in the circular linked list, so it
4483 points back to the master GOT. */
4484 gg->local_gotno = -g->global_gotno;
4485 gg->global_gotno = g->global_gotno;
4486 gg->tls_gotno = 0;
4487 assign = 0;
4488 gg->next = gg;
4492 struct mips_got_info *gn;
4494 assign += htab->reserved_gotno;
4495 g->assigned_gotno = assign;
4496 g->local_gotno += assign;
4497 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4498 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4500 /* Take g out of the direct list, and push it onto the reversed
4501 list that gg points to. g->next is guaranteed to be nonnull after
4502 this operation, as required by mips_elf_initialize_tls_index. */
4503 gn = g->next;
4504 g->next = gg->next;
4505 gg->next = g;
4507 /* Set up any TLS entries. We always place the TLS entries after
4508 all non-TLS entries. */
4509 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4510 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
4512 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4513 g = gn;
4515 /* Forbid global symbols in every non-primary GOT from having
4516 lazy-binding stubs. */
4517 if (g)
4518 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4520 while (g);
4522 got->size = (gg->next->local_gotno
4523 + gg->next->global_gotno
4524 + gg->next->tls_gotno) * MIPS_ELF_GOT_SIZE (abfd);
4526 needed_relocs = 0;
4527 set_got_offset_arg.value = MIPS_ELF_GOT_SIZE (abfd);
4528 set_got_offset_arg.info = info;
4529 for (g = gg->next; g && g->next != gg; g = g->next)
4531 unsigned int save_assign;
4533 /* Assign offsets to global GOT entries. */
4534 save_assign = g->assigned_gotno;
4535 g->assigned_gotno = g->local_gotno;
4536 set_got_offset_arg.g = g;
4537 set_got_offset_arg.needed_relocs = 0;
4538 htab_traverse (g->got_entries,
4539 mips_elf_set_global_got_offset,
4540 &set_got_offset_arg);
4541 needed_relocs += set_got_offset_arg.needed_relocs;
4542 BFD_ASSERT (g->assigned_gotno - g->local_gotno <= g->global_gotno);
4544 g->assigned_gotno = save_assign;
4545 if (info->shared)
4547 needed_relocs += g->local_gotno - g->assigned_gotno;
4548 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4549 + g->next->global_gotno
4550 + g->next->tls_gotno
4551 + htab->reserved_gotno);
4555 if (needed_relocs)
4556 mips_elf_allocate_dynamic_relocations (dynobj, info,
4557 needed_relocs);
4559 return TRUE;
4563 /* Returns the first relocation of type r_type found, beginning with
4564 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4566 static const Elf_Internal_Rela *
4567 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4568 const Elf_Internal_Rela *relocation,
4569 const Elf_Internal_Rela *relend)
4571 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4573 while (relocation < relend)
4575 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4576 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4577 return relocation;
4579 ++relocation;
4582 /* We didn't find it. */
4583 return NULL;
4586 /* Return whether an input relocation is against a local symbol. */
4588 static bfd_boolean
4589 mips_elf_local_relocation_p (bfd *input_bfd,
4590 const Elf_Internal_Rela *relocation,
4591 asection **local_sections)
4593 unsigned long r_symndx;
4594 Elf_Internal_Shdr *symtab_hdr;
4595 size_t extsymoff;
4597 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4598 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4599 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4601 if (r_symndx < extsymoff)
4602 return TRUE;
4603 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4604 return TRUE;
4606 return FALSE;
4609 /* Sign-extend VALUE, which has the indicated number of BITS. */
4611 bfd_vma
4612 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4614 if (value & ((bfd_vma) 1 << (bits - 1)))
4615 /* VALUE is negative. */
4616 value |= ((bfd_vma) - 1) << bits;
4618 return value;
4621 /* Return non-zero if the indicated VALUE has overflowed the maximum
4622 range expressible by a signed number with the indicated number of
4623 BITS. */
4625 static bfd_boolean
4626 mips_elf_overflow_p (bfd_vma value, int bits)
4628 bfd_signed_vma svalue = (bfd_signed_vma) value;
4630 if (svalue > (1 << (bits - 1)) - 1)
4631 /* The value is too big. */
4632 return TRUE;
4633 else if (svalue < -(1 << (bits - 1)))
4634 /* The value is too small. */
4635 return TRUE;
4637 /* All is well. */
4638 return FALSE;
4641 /* Calculate the %high function. */
4643 static bfd_vma
4644 mips_elf_high (bfd_vma value)
4646 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4649 /* Calculate the %higher function. */
4651 static bfd_vma
4652 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4654 #ifdef BFD64
4655 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4656 #else
4657 abort ();
4658 return MINUS_ONE;
4659 #endif
4662 /* Calculate the %highest function. */
4664 static bfd_vma
4665 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4667 #ifdef BFD64
4668 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4669 #else
4670 abort ();
4671 return MINUS_ONE;
4672 #endif
4675 /* Create the .compact_rel section. */
4677 static bfd_boolean
4678 mips_elf_create_compact_rel_section
4679 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4681 flagword flags;
4682 register asection *s;
4684 if (bfd_get_section_by_name (abfd, ".compact_rel") == NULL)
4686 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4687 | SEC_READONLY);
4689 s = bfd_make_section_with_flags (abfd, ".compact_rel", flags);
4690 if (s == NULL
4691 || ! bfd_set_section_alignment (abfd, s,
4692 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4693 return FALSE;
4695 s->size = sizeof (Elf32_External_compact_rel);
4698 return TRUE;
4701 /* Create the .got section to hold the global offset table. */
4703 static bfd_boolean
4704 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
4706 flagword flags;
4707 register asection *s;
4708 struct elf_link_hash_entry *h;
4709 struct bfd_link_hash_entry *bh;
4710 struct mips_got_info *g;
4711 bfd_size_type amt;
4712 struct mips_elf_link_hash_table *htab;
4714 htab = mips_elf_hash_table (info);
4715 BFD_ASSERT (htab != NULL);
4717 /* This function may be called more than once. */
4718 if (htab->sgot)
4719 return TRUE;
4721 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4722 | SEC_LINKER_CREATED);
4724 /* We have to use an alignment of 2**4 here because this is hardcoded
4725 in the function stub generation and in the linker script. */
4726 s = bfd_make_section_with_flags (abfd, ".got", flags);
4727 if (s == NULL
4728 || ! bfd_set_section_alignment (abfd, s, 4))
4729 return FALSE;
4730 htab->sgot = s;
4732 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
4733 linker script because we don't want to define the symbol if we
4734 are not creating a global offset table. */
4735 bh = NULL;
4736 if (! (_bfd_generic_link_add_one_symbol
4737 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
4738 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
4739 return FALSE;
4741 h = (struct elf_link_hash_entry *) bh;
4742 h->non_elf = 0;
4743 h->def_regular = 1;
4744 h->type = STT_OBJECT;
4745 elf_hash_table (info)->hgot = h;
4747 if (info->shared
4748 && ! bfd_elf_link_record_dynamic_symbol (info, h))
4749 return FALSE;
4751 amt = sizeof (struct mips_got_info);
4752 g = bfd_alloc (abfd, amt);
4753 if (g == NULL)
4754 return FALSE;
4755 g->global_gotsym = NULL;
4756 g->global_gotno = 0;
4757 g->reloc_only_gotno = 0;
4758 g->tls_gotno = 0;
4759 g->local_gotno = 0;
4760 g->page_gotno = 0;
4761 g->assigned_gotno = 0;
4762 g->bfd2got = NULL;
4763 g->next = NULL;
4764 g->tls_ldm_offset = MINUS_ONE;
4765 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
4766 mips_elf_got_entry_eq, NULL);
4767 if (g->got_entries == NULL)
4768 return FALSE;
4769 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4770 mips_got_page_entry_eq, NULL);
4771 if (g->got_page_entries == NULL)
4772 return FALSE;
4773 htab->got_info = g;
4774 mips_elf_section_data (s)->elf.this_hdr.sh_flags
4775 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
4777 /* We also need a .got.plt section when generating PLTs. */
4778 s = bfd_make_section_with_flags (abfd, ".got.plt",
4779 SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4780 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4781 if (s == NULL)
4782 return FALSE;
4783 htab->sgotplt = s;
4785 return TRUE;
4788 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
4789 __GOTT_INDEX__ symbols. These symbols are only special for
4790 shared objects; they are not used in executables. */
4792 static bfd_boolean
4793 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
4795 return (mips_elf_hash_table (info)->is_vxworks
4796 && info->shared
4797 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
4798 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
4801 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
4802 require an la25 stub. See also mips_elf_local_pic_function_p,
4803 which determines whether the destination function ever requires a
4804 stub. */
4806 static bfd_boolean
4807 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type)
4809 /* We specifically ignore branches and jumps from EF_PIC objects,
4810 where the onus is on the compiler or programmer to perform any
4811 necessary initialization of $25. Sometimes such initialization
4812 is unnecessary; for example, -mno-shared functions do not use
4813 the incoming value of $25, and may therefore be called directly. */
4814 if (PIC_OBJECT_P (input_bfd))
4815 return FALSE;
4817 switch (r_type)
4819 case R_MIPS_26:
4820 case R_MIPS_PC16:
4821 case R_MIPS16_26:
4822 return TRUE;
4824 default:
4825 return FALSE;
4829 /* Calculate the value produced by the RELOCATION (which comes from
4830 the INPUT_BFD). The ADDEND is the addend to use for this
4831 RELOCATION; RELOCATION->R_ADDEND is ignored.
4833 The result of the relocation calculation is stored in VALUEP.
4834 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
4835 is a MIPS16 jump to non-MIPS16 code, or vice versa.
4837 This function returns bfd_reloc_continue if the caller need take no
4838 further action regarding this relocation, bfd_reloc_notsupported if
4839 something goes dramatically wrong, bfd_reloc_overflow if an
4840 overflow occurs, and bfd_reloc_ok to indicate success. */
4842 static bfd_reloc_status_type
4843 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
4844 asection *input_section,
4845 struct bfd_link_info *info,
4846 const Elf_Internal_Rela *relocation,
4847 bfd_vma addend, reloc_howto_type *howto,
4848 Elf_Internal_Sym *local_syms,
4849 asection **local_sections, bfd_vma *valuep,
4850 const char **namep,
4851 bfd_boolean *cross_mode_jump_p,
4852 bfd_boolean save_addend)
4854 /* The eventual value we will return. */
4855 bfd_vma value;
4856 /* The address of the symbol against which the relocation is
4857 occurring. */
4858 bfd_vma symbol = 0;
4859 /* The final GP value to be used for the relocatable, executable, or
4860 shared object file being produced. */
4861 bfd_vma gp;
4862 /* The place (section offset or address) of the storage unit being
4863 relocated. */
4864 bfd_vma p;
4865 /* The value of GP used to create the relocatable object. */
4866 bfd_vma gp0;
4867 /* The offset into the global offset table at which the address of
4868 the relocation entry symbol, adjusted by the addend, resides
4869 during execution. */
4870 bfd_vma g = MINUS_ONE;
4871 /* The section in which the symbol referenced by the relocation is
4872 located. */
4873 asection *sec = NULL;
4874 struct mips_elf_link_hash_entry *h = NULL;
4875 /* TRUE if the symbol referred to by this relocation is a local
4876 symbol. */
4877 bfd_boolean local_p, was_local_p;
4878 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
4879 bfd_boolean gp_disp_p = FALSE;
4880 /* TRUE if the symbol referred to by this relocation is
4881 "__gnu_local_gp". */
4882 bfd_boolean gnu_local_gp_p = FALSE;
4883 Elf_Internal_Shdr *symtab_hdr;
4884 size_t extsymoff;
4885 unsigned long r_symndx;
4886 int r_type;
4887 /* TRUE if overflow occurred during the calculation of the
4888 relocation value. */
4889 bfd_boolean overflowed_p;
4890 /* TRUE if this relocation refers to a MIPS16 function. */
4891 bfd_boolean target_is_16_bit_code_p = FALSE;
4892 struct mips_elf_link_hash_table *htab;
4893 bfd *dynobj;
4895 dynobj = elf_hash_table (info)->dynobj;
4896 htab = mips_elf_hash_table (info);
4897 BFD_ASSERT (htab != NULL);
4899 /* Parse the relocation. */
4900 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4901 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
4902 p = (input_section->output_section->vma
4903 + input_section->output_offset
4904 + relocation->r_offset);
4906 /* Assume that there will be no overflow. */
4907 overflowed_p = FALSE;
4909 /* Figure out whether or not the symbol is local, and get the offset
4910 used in the array of hash table entries. */
4911 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4912 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
4913 local_sections);
4914 was_local_p = local_p;
4915 if (! elf_bad_symtab (input_bfd))
4916 extsymoff = symtab_hdr->sh_info;
4917 else
4919 /* The symbol table does not follow the rule that local symbols
4920 must come before globals. */
4921 extsymoff = 0;
4924 /* Figure out the value of the symbol. */
4925 if (local_p)
4927 Elf_Internal_Sym *sym;
4929 sym = local_syms + r_symndx;
4930 sec = local_sections[r_symndx];
4932 symbol = sec->output_section->vma + sec->output_offset;
4933 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
4934 || (sec->flags & SEC_MERGE))
4935 symbol += sym->st_value;
4936 if ((sec->flags & SEC_MERGE)
4937 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4939 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
4940 addend -= symbol;
4941 addend += sec->output_section->vma + sec->output_offset;
4944 /* MIPS16 text labels should be treated as odd. */
4945 if (ELF_ST_IS_MIPS16 (sym->st_other))
4946 ++symbol;
4948 /* Record the name of this symbol, for our caller. */
4949 *namep = bfd_elf_string_from_elf_section (input_bfd,
4950 symtab_hdr->sh_link,
4951 sym->st_name);
4952 if (*namep == '\0')
4953 *namep = bfd_section_name (input_bfd, sec);
4955 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
4957 else
4959 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
4961 /* For global symbols we look up the symbol in the hash-table. */
4962 h = ((struct mips_elf_link_hash_entry *)
4963 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
4964 /* Find the real hash-table entry for this symbol. */
4965 while (h->root.root.type == bfd_link_hash_indirect
4966 || h->root.root.type == bfd_link_hash_warning)
4967 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4969 /* Record the name of this symbol, for our caller. */
4970 *namep = h->root.root.root.string;
4972 /* See if this is the special _gp_disp symbol. Note that such a
4973 symbol must always be a global symbol. */
4974 if (strcmp (*namep, "_gp_disp") == 0
4975 && ! NEWABI_P (input_bfd))
4977 /* Relocations against _gp_disp are permitted only with
4978 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
4979 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
4980 return bfd_reloc_notsupported;
4982 gp_disp_p = TRUE;
4984 /* See if this is the special _gp symbol. Note that such a
4985 symbol must always be a global symbol. */
4986 else if (strcmp (*namep, "__gnu_local_gp") == 0)
4987 gnu_local_gp_p = TRUE;
4990 /* If this symbol is defined, calculate its address. Note that
4991 _gp_disp is a magic symbol, always implicitly defined by the
4992 linker, so it's inappropriate to check to see whether or not
4993 its defined. */
4994 else if ((h->root.root.type == bfd_link_hash_defined
4995 || h->root.root.type == bfd_link_hash_defweak)
4996 && h->root.root.u.def.section)
4998 sec = h->root.root.u.def.section;
4999 if (sec->output_section)
5000 symbol = (h->root.root.u.def.value
5001 + sec->output_section->vma
5002 + sec->output_offset);
5003 else
5004 symbol = h->root.root.u.def.value;
5006 else if (h->root.root.type == bfd_link_hash_undefweak)
5007 /* We allow relocations against undefined weak symbols, giving
5008 it the value zero, so that you can undefined weak functions
5009 and check to see if they exist by looking at their
5010 addresses. */
5011 symbol = 0;
5012 else if (info->unresolved_syms_in_objects == RM_IGNORE
5013 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5014 symbol = 0;
5015 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5016 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5018 /* If this is a dynamic link, we should have created a
5019 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5020 in in _bfd_mips_elf_create_dynamic_sections.
5021 Otherwise, we should define the symbol with a value of 0.
5022 FIXME: It should probably get into the symbol table
5023 somehow as well. */
5024 BFD_ASSERT (! info->shared);
5025 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5026 symbol = 0;
5028 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5030 /* This is an optional symbol - an Irix specific extension to the
5031 ELF spec. Ignore it for now.
5032 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5033 than simply ignoring them, but we do not handle this for now.
5034 For information see the "64-bit ELF Object File Specification"
5035 which is available from here:
5036 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5037 symbol = 0;
5039 else if ((*info->callbacks->undefined_symbol)
5040 (info, h->root.root.root.string, input_bfd,
5041 input_section, relocation->r_offset,
5042 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5043 || ELF_ST_VISIBILITY (h->root.other)))
5045 return bfd_reloc_undefined;
5047 else
5049 return bfd_reloc_notsupported;
5052 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5055 /* If this is a reference to a 16-bit function with a stub, we need
5056 to redirect the relocation to the stub unless:
5058 (a) the relocation is for a MIPS16 JAL;
5060 (b) the relocation is for a MIPS16 PIC call, and there are no
5061 non-MIPS16 uses of the GOT slot; or
5063 (c) the section allows direct references to MIPS16 functions. */
5064 if (r_type != R_MIPS16_26
5065 && !info->relocatable
5066 && ((h != NULL
5067 && h->fn_stub != NULL
5068 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5069 || (local_p
5070 && elf_tdata (input_bfd)->local_stubs != NULL
5071 && elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5072 && !section_allows_mips16_refs_p (input_section))
5074 /* This is a 32- or 64-bit call to a 16-bit function. We should
5075 have already noticed that we were going to need the
5076 stub. */
5077 if (local_p)
5078 sec = elf_tdata (input_bfd)->local_stubs[r_symndx];
5079 else
5081 BFD_ASSERT (h->need_fn_stub);
5082 sec = h->fn_stub;
5085 symbol = sec->output_section->vma + sec->output_offset;
5086 /* The target is 16-bit, but the stub isn't. */
5087 target_is_16_bit_code_p = FALSE;
5089 /* If this is a 16-bit call to a 32- or 64-bit function with a stub, we
5090 need to redirect the call to the stub. Note that we specifically
5091 exclude R_MIPS16_CALL16 from this behavior; indirect calls should
5092 use an indirect stub instead. */
5093 else if (r_type == R_MIPS16_26 && !info->relocatable
5094 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5095 || (local_p
5096 && elf_tdata (input_bfd)->local_call_stubs != NULL
5097 && elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5098 && !target_is_16_bit_code_p)
5100 if (local_p)
5101 sec = elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5102 else
5104 /* If both call_stub and call_fp_stub are defined, we can figure
5105 out which one to use by checking which one appears in the input
5106 file. */
5107 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5109 asection *o;
5111 sec = NULL;
5112 for (o = input_bfd->sections; o != NULL; o = o->next)
5114 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5116 sec = h->call_fp_stub;
5117 break;
5120 if (sec == NULL)
5121 sec = h->call_stub;
5123 else if (h->call_stub != NULL)
5124 sec = h->call_stub;
5125 else
5126 sec = h->call_fp_stub;
5129 BFD_ASSERT (sec->size > 0);
5130 symbol = sec->output_section->vma + sec->output_offset;
5132 /* If this is a direct call to a PIC function, redirect to the
5133 non-PIC stub. */
5134 else if (h != NULL && h->la25_stub
5135 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type))
5136 symbol = (h->la25_stub->stub_section->output_section->vma
5137 + h->la25_stub->stub_section->output_offset
5138 + h->la25_stub->offset);
5140 /* Calls from 16-bit code to 32-bit code and vice versa require the
5141 mode change. */
5142 *cross_mode_jump_p = !info->relocatable
5143 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5144 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5145 && target_is_16_bit_code_p));
5147 local_p = h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->root);
5149 gp0 = _bfd_get_gp_value (input_bfd);
5150 gp = _bfd_get_gp_value (abfd);
5151 if (htab->got_info)
5152 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5154 if (gnu_local_gp_p)
5155 symbol = gp;
5157 /* Global R_MIPS_GOT_PAGE relocations are equivalent to R_MIPS_GOT_DISP.
5158 The addend is applied by the corresponding R_MIPS_GOT_OFST. */
5159 if (r_type == R_MIPS_GOT_PAGE && !local_p)
5161 r_type = R_MIPS_GOT_DISP;
5162 addend = 0;
5165 /* If we haven't already determined the GOT offset, and we're going
5166 to need it, get it now. */
5167 switch (r_type)
5169 case R_MIPS16_CALL16:
5170 case R_MIPS16_GOT16:
5171 case R_MIPS_CALL16:
5172 case R_MIPS_GOT16:
5173 case R_MIPS_GOT_DISP:
5174 case R_MIPS_GOT_HI16:
5175 case R_MIPS_CALL_HI16:
5176 case R_MIPS_GOT_LO16:
5177 case R_MIPS_CALL_LO16:
5178 case R_MIPS_TLS_GD:
5179 case R_MIPS_TLS_GOTTPREL:
5180 case R_MIPS_TLS_LDM:
5181 /* Find the index into the GOT where this value is located. */
5182 if (r_type == R_MIPS_TLS_LDM)
5184 g = mips_elf_local_got_index (abfd, input_bfd, info,
5185 0, 0, NULL, r_type);
5186 if (g == MINUS_ONE)
5187 return bfd_reloc_outofrange;
5189 else if (!local_p)
5191 /* On VxWorks, CALL relocations should refer to the .got.plt
5192 entry, which is initialized to point at the PLT stub. */
5193 if (htab->is_vxworks
5194 && (r_type == R_MIPS_CALL_HI16
5195 || r_type == R_MIPS_CALL_LO16
5196 || call16_reloc_p (r_type)))
5198 BFD_ASSERT (addend == 0);
5199 BFD_ASSERT (h->root.needs_plt);
5200 g = mips_elf_gotplt_index (info, &h->root);
5202 else
5204 BFD_ASSERT (addend == 0);
5205 g = mips_elf_global_got_index (dynobj, input_bfd,
5206 &h->root, r_type, info);
5207 if (h->tls_type == GOT_NORMAL
5208 && !elf_hash_table (info)->dynamic_sections_created)
5209 /* This is a static link. We must initialize the GOT entry. */
5210 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5213 else if (!htab->is_vxworks
5214 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5215 /* The calculation below does not involve "g". */
5216 break;
5217 else
5219 g = mips_elf_local_got_index (abfd, input_bfd, info,
5220 symbol + addend, r_symndx, h, r_type);
5221 if (g == MINUS_ONE)
5222 return bfd_reloc_outofrange;
5225 /* Convert GOT indices to actual offsets. */
5226 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5227 break;
5230 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5231 symbols are resolved by the loader. Add them to .rela.dyn. */
5232 if (h != NULL && is_gott_symbol (info, &h->root))
5234 Elf_Internal_Rela outrel;
5235 bfd_byte *loc;
5236 asection *s;
5238 s = mips_elf_rel_dyn_section (info, FALSE);
5239 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5241 outrel.r_offset = (input_section->output_section->vma
5242 + input_section->output_offset
5243 + relocation->r_offset);
5244 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5245 outrel.r_addend = addend;
5246 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5248 /* If we've written this relocation for a readonly section,
5249 we need to set DF_TEXTREL again, so that we do not delete the
5250 DT_TEXTREL tag. */
5251 if (MIPS_ELF_READONLY_SECTION (input_section))
5252 info->flags |= DF_TEXTREL;
5254 *valuep = 0;
5255 return bfd_reloc_ok;
5258 /* Figure out what kind of relocation is being performed. */
5259 switch (r_type)
5261 case R_MIPS_NONE:
5262 return bfd_reloc_continue;
5264 case R_MIPS_16:
5265 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5266 overflowed_p = mips_elf_overflow_p (value, 16);
5267 break;
5269 case R_MIPS_32:
5270 case R_MIPS_REL32:
5271 case R_MIPS_64:
5272 if ((info->shared
5273 || (htab->root.dynamic_sections_created
5274 && h != NULL
5275 && h->root.def_dynamic
5276 && !h->root.def_regular
5277 && !h->has_static_relocs))
5278 && r_symndx != STN_UNDEF
5279 && (h == NULL
5280 || h->root.root.type != bfd_link_hash_undefweak
5281 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5282 && (input_section->flags & SEC_ALLOC) != 0)
5284 /* If we're creating a shared library, then we can't know
5285 where the symbol will end up. So, we create a relocation
5286 record in the output, and leave the job up to the dynamic
5287 linker. We must do the same for executable references to
5288 shared library symbols, unless we've decided to use copy
5289 relocs or PLTs instead. */
5290 value = addend;
5291 if (!mips_elf_create_dynamic_relocation (abfd,
5292 info,
5293 relocation,
5295 sec,
5296 symbol,
5297 &value,
5298 input_section))
5299 return bfd_reloc_undefined;
5301 else
5303 if (r_type != R_MIPS_REL32)
5304 value = symbol + addend;
5305 else
5306 value = addend;
5308 value &= howto->dst_mask;
5309 break;
5311 case R_MIPS_PC32:
5312 value = symbol + addend - p;
5313 value &= howto->dst_mask;
5314 break;
5316 case R_MIPS16_26:
5317 /* The calculation for R_MIPS16_26 is just the same as for an
5318 R_MIPS_26. It's only the storage of the relocated field into
5319 the output file that's different. That's handled in
5320 mips_elf_perform_relocation. So, we just fall through to the
5321 R_MIPS_26 case here. */
5322 case R_MIPS_26:
5323 if (was_local_p)
5324 value = ((addend | ((p + 4) & 0xf0000000)) + symbol) >> 2;
5325 else
5327 value = (_bfd_mips_elf_sign_extend (addend, 28) + symbol) >> 2;
5328 if (h->root.root.type != bfd_link_hash_undefweak)
5329 overflowed_p = (value >> 26) != ((p + 4) >> 28);
5331 value &= howto->dst_mask;
5332 break;
5334 case R_MIPS_TLS_DTPREL_HI16:
5335 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5336 & howto->dst_mask);
5337 break;
5339 case R_MIPS_TLS_DTPREL_LO16:
5340 case R_MIPS_TLS_DTPREL32:
5341 case R_MIPS_TLS_DTPREL64:
5342 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5343 break;
5345 case R_MIPS_TLS_TPREL_HI16:
5346 value = (mips_elf_high (addend + symbol - tprel_base (info))
5347 & howto->dst_mask);
5348 break;
5350 case R_MIPS_TLS_TPREL_LO16:
5351 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5352 break;
5354 case R_MIPS_HI16:
5355 case R_MIPS16_HI16:
5356 if (!gp_disp_p)
5358 value = mips_elf_high (addend + symbol);
5359 value &= howto->dst_mask;
5361 else
5363 /* For MIPS16 ABI code we generate this sequence
5364 0: li $v0,%hi(_gp_disp)
5365 4: addiupc $v1,%lo(_gp_disp)
5366 8: sll $v0,16
5367 12: addu $v0,$v1
5368 14: move $gp,$v0
5369 So the offsets of hi and lo relocs are the same, but the
5370 $pc is four higher than $t9 would be, so reduce
5371 both reloc addends by 4. */
5372 if (r_type == R_MIPS16_HI16)
5373 value = mips_elf_high (addend + gp - p - 4);
5374 else
5375 value = mips_elf_high (addend + gp - p);
5376 overflowed_p = mips_elf_overflow_p (value, 16);
5378 break;
5380 case R_MIPS_LO16:
5381 case R_MIPS16_LO16:
5382 if (!gp_disp_p)
5383 value = (symbol + addend) & howto->dst_mask;
5384 else
5386 /* See the comment for R_MIPS16_HI16 above for the reason
5387 for this conditional. */
5388 if (r_type == R_MIPS16_LO16)
5389 value = addend + gp - p;
5390 else
5391 value = addend + gp - p + 4;
5392 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5393 for overflow. But, on, say, IRIX5, relocations against
5394 _gp_disp are normally generated from the .cpload
5395 pseudo-op. It generates code that normally looks like
5396 this:
5398 lui $gp,%hi(_gp_disp)
5399 addiu $gp,$gp,%lo(_gp_disp)
5400 addu $gp,$gp,$t9
5402 Here $t9 holds the address of the function being called,
5403 as required by the MIPS ELF ABI. The R_MIPS_LO16
5404 relocation can easily overflow in this situation, but the
5405 R_MIPS_HI16 relocation will handle the overflow.
5406 Therefore, we consider this a bug in the MIPS ABI, and do
5407 not check for overflow here. */
5409 break;
5411 case R_MIPS_LITERAL:
5412 /* Because we don't merge literal sections, we can handle this
5413 just like R_MIPS_GPREL16. In the long run, we should merge
5414 shared literals, and then we will need to additional work
5415 here. */
5417 /* Fall through. */
5419 case R_MIPS16_GPREL:
5420 /* The R_MIPS16_GPREL performs the same calculation as
5421 R_MIPS_GPREL16, but stores the relocated bits in a different
5422 order. We don't need to do anything special here; the
5423 differences are handled in mips_elf_perform_relocation. */
5424 case R_MIPS_GPREL16:
5425 /* Only sign-extend the addend if it was extracted from the
5426 instruction. If the addend was separate, leave it alone,
5427 otherwise we may lose significant bits. */
5428 if (howto->partial_inplace)
5429 addend = _bfd_mips_elf_sign_extend (addend, 16);
5430 value = symbol + addend - gp;
5431 /* If the symbol was local, any earlier relocatable links will
5432 have adjusted its addend with the gp offset, so compensate
5433 for that now. Don't do it for symbols forced local in this
5434 link, though, since they won't have had the gp offset applied
5435 to them before. */
5436 if (was_local_p)
5437 value += gp0;
5438 overflowed_p = mips_elf_overflow_p (value, 16);
5439 break;
5441 case R_MIPS16_GOT16:
5442 case R_MIPS16_CALL16:
5443 case R_MIPS_GOT16:
5444 case R_MIPS_CALL16:
5445 /* VxWorks does not have separate local and global semantics for
5446 R_MIPS*_GOT16; every relocation evaluates to "G". */
5447 if (!htab->is_vxworks && local_p)
5449 value = mips_elf_got16_entry (abfd, input_bfd, info,
5450 symbol + addend, !was_local_p);
5451 if (value == MINUS_ONE)
5452 return bfd_reloc_outofrange;
5453 value
5454 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5455 overflowed_p = mips_elf_overflow_p (value, 16);
5456 break;
5459 /* Fall through. */
5461 case R_MIPS_TLS_GD:
5462 case R_MIPS_TLS_GOTTPREL:
5463 case R_MIPS_TLS_LDM:
5464 case R_MIPS_GOT_DISP:
5465 value = g;
5466 overflowed_p = mips_elf_overflow_p (value, 16);
5467 break;
5469 case R_MIPS_GPREL32:
5470 value = (addend + symbol + gp0 - gp);
5471 if (!save_addend)
5472 value &= howto->dst_mask;
5473 break;
5475 case R_MIPS_PC16:
5476 case R_MIPS_GNU_REL16_S2:
5477 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5478 overflowed_p = mips_elf_overflow_p (value, 18);
5479 value >>= howto->rightshift;
5480 value &= howto->dst_mask;
5481 break;
5483 case R_MIPS_GOT_HI16:
5484 case R_MIPS_CALL_HI16:
5485 /* We're allowed to handle these two relocations identically.
5486 The dynamic linker is allowed to handle the CALL relocations
5487 differently by creating a lazy evaluation stub. */
5488 value = g;
5489 value = mips_elf_high (value);
5490 value &= howto->dst_mask;
5491 break;
5493 case R_MIPS_GOT_LO16:
5494 case R_MIPS_CALL_LO16:
5495 value = g & howto->dst_mask;
5496 break;
5498 case R_MIPS_GOT_PAGE:
5499 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5500 if (value == MINUS_ONE)
5501 return bfd_reloc_outofrange;
5502 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5503 overflowed_p = mips_elf_overflow_p (value, 16);
5504 break;
5506 case R_MIPS_GOT_OFST:
5507 if (local_p)
5508 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5509 else
5510 value = addend;
5511 overflowed_p = mips_elf_overflow_p (value, 16);
5512 break;
5514 case R_MIPS_SUB:
5515 value = symbol - addend;
5516 value &= howto->dst_mask;
5517 break;
5519 case R_MIPS_HIGHER:
5520 value = mips_elf_higher (addend + symbol);
5521 value &= howto->dst_mask;
5522 break;
5524 case R_MIPS_HIGHEST:
5525 value = mips_elf_highest (addend + symbol);
5526 value &= howto->dst_mask;
5527 break;
5529 case R_MIPS_SCN_DISP:
5530 value = symbol + addend - sec->output_offset;
5531 value &= howto->dst_mask;
5532 break;
5534 case R_MIPS_JALR:
5535 /* This relocation is only a hint. In some cases, we optimize
5536 it into a bal instruction. But we don't try to optimize
5537 when the symbol does not resolve locally. */
5538 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
5539 return bfd_reloc_continue;
5540 value = symbol + addend;
5541 break;
5543 case R_MIPS_PJUMP:
5544 case R_MIPS_GNU_VTINHERIT:
5545 case R_MIPS_GNU_VTENTRY:
5546 /* We don't do anything with these at present. */
5547 return bfd_reloc_continue;
5549 default:
5550 /* An unrecognized relocation type. */
5551 return bfd_reloc_notsupported;
5554 /* Store the VALUE for our caller. */
5555 *valuep = value;
5556 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
5559 /* Obtain the field relocated by RELOCATION. */
5561 static bfd_vma
5562 mips_elf_obtain_contents (reloc_howto_type *howto,
5563 const Elf_Internal_Rela *relocation,
5564 bfd *input_bfd, bfd_byte *contents)
5566 bfd_vma x;
5567 bfd_byte *location = contents + relocation->r_offset;
5569 /* Obtain the bytes. */
5570 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
5572 return x;
5575 /* It has been determined that the result of the RELOCATION is the
5576 VALUE. Use HOWTO to place VALUE into the output file at the
5577 appropriate position. The SECTION is the section to which the
5578 relocation applies.
5579 CROSS_MODE_JUMP_P is true if the relocation field
5580 is a MIPS16 jump to non-MIPS16 code, or vice versa.
5582 Returns FALSE if anything goes wrong. */
5584 static bfd_boolean
5585 mips_elf_perform_relocation (struct bfd_link_info *info,
5586 reloc_howto_type *howto,
5587 const Elf_Internal_Rela *relocation,
5588 bfd_vma value, bfd *input_bfd,
5589 asection *input_section, bfd_byte *contents,
5590 bfd_boolean cross_mode_jump_p)
5592 bfd_vma x;
5593 bfd_byte *location;
5594 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5596 /* Figure out where the relocation is occurring. */
5597 location = contents + relocation->r_offset;
5599 _bfd_mips16_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5601 /* Obtain the current value. */
5602 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5604 /* Clear the field we are setting. */
5605 x &= ~howto->dst_mask;
5607 /* Set the field. */
5608 x |= (value & howto->dst_mask);
5610 /* If required, turn JAL into JALX. */
5611 if (cross_mode_jump_p && jal_reloc_p (r_type))
5613 bfd_boolean ok;
5614 bfd_vma opcode = x >> 26;
5615 bfd_vma jalx_opcode;
5617 /* Check to see if the opcode is already JAL or JALX. */
5618 if (r_type == R_MIPS16_26)
5620 ok = ((opcode == 0x6) || (opcode == 0x7));
5621 jalx_opcode = 0x7;
5623 else
5625 ok = ((opcode == 0x3) || (opcode == 0x1d));
5626 jalx_opcode = 0x1d;
5629 /* If the opcode is not JAL or JALX, there's a problem. */
5630 if (!ok)
5632 (*_bfd_error_handler)
5633 (_("%B: %A+0x%lx: Direct jumps between ISA modes are not allowed; consider recompiling with interlinking enabled."),
5634 input_bfd,
5635 input_section,
5636 (unsigned long) relocation->r_offset);
5637 bfd_set_error (bfd_error_bad_value);
5638 return FALSE;
5641 /* Make this the JALX opcode. */
5642 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
5645 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
5646 range. */
5647 if (!info->relocatable
5648 && !cross_mode_jump_p
5649 && ((JAL_TO_BAL_P (input_bfd)
5650 && r_type == R_MIPS_26
5651 && (x >> 26) == 0x3) /* jal addr */
5652 || (JALR_TO_BAL_P (input_bfd)
5653 && r_type == R_MIPS_JALR
5654 && x == 0x0320f809) /* jalr t9 */
5655 || (JR_TO_B_P (input_bfd)
5656 && r_type == R_MIPS_JALR
5657 && x == 0x03200008))) /* jr t9 */
5659 bfd_vma addr;
5660 bfd_vma dest;
5661 bfd_signed_vma off;
5663 addr = (input_section->output_section->vma
5664 + input_section->output_offset
5665 + relocation->r_offset
5666 + 4);
5667 if (r_type == R_MIPS_26)
5668 dest = (value << 2) | ((addr >> 28) << 28);
5669 else
5670 dest = value;
5671 off = dest - addr;
5672 if (off <= 0x1ffff && off >= -0x20000)
5674 if (x == 0x03200008) /* jr t9 */
5675 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
5676 else
5677 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
5681 /* Put the value into the output. */
5682 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
5684 _bfd_mips16_elf_reloc_shuffle(input_bfd, r_type, !info->relocatable,
5685 location);
5687 return TRUE;
5690 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
5691 is the original relocation, which is now being transformed into a
5692 dynamic relocation. The ADDENDP is adjusted if necessary; the
5693 caller should store the result in place of the original addend. */
5695 static bfd_boolean
5696 mips_elf_create_dynamic_relocation (bfd *output_bfd,
5697 struct bfd_link_info *info,
5698 const Elf_Internal_Rela *rel,
5699 struct mips_elf_link_hash_entry *h,
5700 asection *sec, bfd_vma symbol,
5701 bfd_vma *addendp, asection *input_section)
5703 Elf_Internal_Rela outrel[3];
5704 asection *sreloc;
5705 bfd *dynobj;
5706 int r_type;
5707 long indx;
5708 bfd_boolean defined_p;
5709 struct mips_elf_link_hash_table *htab;
5711 htab = mips_elf_hash_table (info);
5712 BFD_ASSERT (htab != NULL);
5714 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
5715 dynobj = elf_hash_table (info)->dynobj;
5716 sreloc = mips_elf_rel_dyn_section (info, FALSE);
5717 BFD_ASSERT (sreloc != NULL);
5718 BFD_ASSERT (sreloc->contents != NULL);
5719 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
5720 < sreloc->size);
5722 outrel[0].r_offset =
5723 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
5724 if (ABI_64_P (output_bfd))
5726 outrel[1].r_offset =
5727 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
5728 outrel[2].r_offset =
5729 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
5732 if (outrel[0].r_offset == MINUS_ONE)
5733 /* The relocation field has been deleted. */
5734 return TRUE;
5736 if (outrel[0].r_offset == MINUS_TWO)
5738 /* The relocation field has been converted into a relative value of
5739 some sort. Functions like _bfd_elf_write_section_eh_frame expect
5740 the field to be fully relocated, so add in the symbol's value. */
5741 *addendp += symbol;
5742 return TRUE;
5745 /* We must now calculate the dynamic symbol table index to use
5746 in the relocation. */
5747 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5749 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5750 indx = h->root.dynindx;
5751 if (SGI_COMPAT (output_bfd))
5752 defined_p = h->root.def_regular;
5753 else
5754 /* ??? glibc's ld.so just adds the final GOT entry to the
5755 relocation field. It therefore treats relocs against
5756 defined symbols in the same way as relocs against
5757 undefined symbols. */
5758 defined_p = FALSE;
5760 else
5762 if (sec != NULL && bfd_is_abs_section (sec))
5763 indx = 0;
5764 else if (sec == NULL || sec->owner == NULL)
5766 bfd_set_error (bfd_error_bad_value);
5767 return FALSE;
5769 else
5771 indx = elf_section_data (sec->output_section)->dynindx;
5772 if (indx == 0)
5774 asection *osec = htab->root.text_index_section;
5775 indx = elf_section_data (osec)->dynindx;
5777 if (indx == 0)
5778 abort ();
5781 /* Instead of generating a relocation using the section
5782 symbol, we may as well make it a fully relative
5783 relocation. We want to avoid generating relocations to
5784 local symbols because we used to generate them
5785 incorrectly, without adding the original symbol value,
5786 which is mandated by the ABI for section symbols. In
5787 order to give dynamic loaders and applications time to
5788 phase out the incorrect use, we refrain from emitting
5789 section-relative relocations. It's not like they're
5790 useful, after all. This should be a bit more efficient
5791 as well. */
5792 /* ??? Although this behavior is compatible with glibc's ld.so,
5793 the ABI says that relocations against STN_UNDEF should have
5794 a symbol value of 0. Irix rld honors this, so relocations
5795 against STN_UNDEF have no effect. */
5796 if (!SGI_COMPAT (output_bfd))
5797 indx = 0;
5798 defined_p = TRUE;
5801 /* If the relocation was previously an absolute relocation and
5802 this symbol will not be referred to by the relocation, we must
5803 adjust it by the value we give it in the dynamic symbol table.
5804 Otherwise leave the job up to the dynamic linker. */
5805 if (defined_p && r_type != R_MIPS_REL32)
5806 *addendp += symbol;
5808 if (htab->is_vxworks)
5809 /* VxWorks uses non-relative relocations for this. */
5810 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
5811 else
5812 /* The relocation is always an REL32 relocation because we don't
5813 know where the shared library will wind up at load-time. */
5814 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
5815 R_MIPS_REL32);
5817 /* For strict adherence to the ABI specification, we should
5818 generate a R_MIPS_64 relocation record by itself before the
5819 _REL32/_64 record as well, such that the addend is read in as
5820 a 64-bit value (REL32 is a 32-bit relocation, after all).
5821 However, since none of the existing ELF64 MIPS dynamic
5822 loaders seems to care, we don't waste space with these
5823 artificial relocations. If this turns out to not be true,
5824 mips_elf_allocate_dynamic_relocation() should be tweaked so
5825 as to make room for a pair of dynamic relocations per
5826 invocation if ABI_64_P, and here we should generate an
5827 additional relocation record with R_MIPS_64 by itself for a
5828 NULL symbol before this relocation record. */
5829 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
5830 ABI_64_P (output_bfd)
5831 ? R_MIPS_64
5832 : R_MIPS_NONE);
5833 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
5835 /* Adjust the output offset of the relocation to reference the
5836 correct location in the output file. */
5837 outrel[0].r_offset += (input_section->output_section->vma
5838 + input_section->output_offset);
5839 outrel[1].r_offset += (input_section->output_section->vma
5840 + input_section->output_offset);
5841 outrel[2].r_offset += (input_section->output_section->vma
5842 + input_section->output_offset);
5844 /* Put the relocation back out. We have to use the special
5845 relocation outputter in the 64-bit case since the 64-bit
5846 relocation format is non-standard. */
5847 if (ABI_64_P (output_bfd))
5849 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
5850 (output_bfd, &outrel[0],
5851 (sreloc->contents
5852 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
5854 else if (htab->is_vxworks)
5856 /* VxWorks uses RELA rather than REL dynamic relocations. */
5857 outrel[0].r_addend = *addendp;
5858 bfd_elf32_swap_reloca_out
5859 (output_bfd, &outrel[0],
5860 (sreloc->contents
5861 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
5863 else
5864 bfd_elf32_swap_reloc_out
5865 (output_bfd, &outrel[0],
5866 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
5868 /* We've now added another relocation. */
5869 ++sreloc->reloc_count;
5871 /* Make sure the output section is writable. The dynamic linker
5872 will be writing to it. */
5873 elf_section_data (input_section->output_section)->this_hdr.sh_flags
5874 |= SHF_WRITE;
5876 /* On IRIX5, make an entry of compact relocation info. */
5877 if (IRIX_COMPAT (output_bfd) == ict_irix5)
5879 asection *scpt = bfd_get_section_by_name (dynobj, ".compact_rel");
5880 bfd_byte *cr;
5882 if (scpt)
5884 Elf32_crinfo cptrel;
5886 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
5887 cptrel.vaddr = (rel->r_offset
5888 + input_section->output_section->vma
5889 + input_section->output_offset);
5890 if (r_type == R_MIPS_REL32)
5891 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
5892 else
5893 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
5894 mips_elf_set_cr_dist2to (cptrel, 0);
5895 cptrel.konst = *addendp;
5897 cr = (scpt->contents
5898 + sizeof (Elf32_External_compact_rel));
5899 mips_elf_set_cr_relvaddr (cptrel, 0);
5900 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
5901 ((Elf32_External_crinfo *) cr
5902 + scpt->reloc_count));
5903 ++scpt->reloc_count;
5907 /* If we've written this relocation for a readonly section,
5908 we need to set DF_TEXTREL again, so that we do not delete the
5909 DT_TEXTREL tag. */
5910 if (MIPS_ELF_READONLY_SECTION (input_section))
5911 info->flags |= DF_TEXTREL;
5913 return TRUE;
5916 /* Return the MACH for a MIPS e_flags value. */
5918 unsigned long
5919 _bfd_elf_mips_mach (flagword flags)
5921 switch (flags & EF_MIPS_MACH)
5923 case E_MIPS_MACH_3900:
5924 return bfd_mach_mips3900;
5926 case E_MIPS_MACH_4010:
5927 return bfd_mach_mips4010;
5929 case E_MIPS_MACH_4100:
5930 return bfd_mach_mips4100;
5932 case E_MIPS_MACH_4111:
5933 return bfd_mach_mips4111;
5935 case E_MIPS_MACH_4120:
5936 return bfd_mach_mips4120;
5938 case E_MIPS_MACH_4650:
5939 return bfd_mach_mips4650;
5941 case E_MIPS_MACH_5400:
5942 return bfd_mach_mips5400;
5944 case E_MIPS_MACH_5500:
5945 return bfd_mach_mips5500;
5947 case E_MIPS_MACH_9000:
5948 return bfd_mach_mips9000;
5950 case E_MIPS_MACH_SB1:
5951 return bfd_mach_mips_sb1;
5953 case E_MIPS_MACH_LS2E:
5954 return bfd_mach_mips_loongson_2e;
5956 case E_MIPS_MACH_LS2F:
5957 return bfd_mach_mips_loongson_2f;
5959 case E_MIPS_MACH_LS3A:
5960 return bfd_mach_mips_loongson_3a;
5962 case E_MIPS_MACH_OCTEON:
5963 return bfd_mach_mips_octeon;
5965 case E_MIPS_MACH_XLR:
5966 return bfd_mach_mips_xlr;
5968 default:
5969 switch (flags & EF_MIPS_ARCH)
5971 default:
5972 case E_MIPS_ARCH_1:
5973 return bfd_mach_mips3000;
5975 case E_MIPS_ARCH_2:
5976 return bfd_mach_mips6000;
5978 case E_MIPS_ARCH_3:
5979 return bfd_mach_mips4000;
5981 case E_MIPS_ARCH_4:
5982 return bfd_mach_mips8000;
5984 case E_MIPS_ARCH_5:
5985 return bfd_mach_mips5;
5987 case E_MIPS_ARCH_32:
5988 return bfd_mach_mipsisa32;
5990 case E_MIPS_ARCH_64:
5991 return bfd_mach_mipsisa64;
5993 case E_MIPS_ARCH_32R2:
5994 return bfd_mach_mipsisa32r2;
5996 case E_MIPS_ARCH_64R2:
5997 return bfd_mach_mipsisa64r2;
6001 return 0;
6004 /* Return printable name for ABI. */
6006 static INLINE char *
6007 elf_mips_abi_name (bfd *abfd)
6009 flagword flags;
6011 flags = elf_elfheader (abfd)->e_flags;
6012 switch (flags & EF_MIPS_ABI)
6014 case 0:
6015 if (ABI_N32_P (abfd))
6016 return "N32";
6017 else if (ABI_64_P (abfd))
6018 return "64";
6019 else
6020 return "none";
6021 case E_MIPS_ABI_O32:
6022 return "O32";
6023 case E_MIPS_ABI_O64:
6024 return "O64";
6025 case E_MIPS_ABI_EABI32:
6026 return "EABI32";
6027 case E_MIPS_ABI_EABI64:
6028 return "EABI64";
6029 default:
6030 return "unknown abi";
6034 /* MIPS ELF uses two common sections. One is the usual one, and the
6035 other is for small objects. All the small objects are kept
6036 together, and then referenced via the gp pointer, which yields
6037 faster assembler code. This is what we use for the small common
6038 section. This approach is copied from ecoff.c. */
6039 static asection mips_elf_scom_section;
6040 static asymbol mips_elf_scom_symbol;
6041 static asymbol *mips_elf_scom_symbol_ptr;
6043 /* MIPS ELF also uses an acommon section, which represents an
6044 allocated common symbol which may be overridden by a
6045 definition in a shared library. */
6046 static asection mips_elf_acom_section;
6047 static asymbol mips_elf_acom_symbol;
6048 static asymbol *mips_elf_acom_symbol_ptr;
6050 /* This is used for both the 32-bit and the 64-bit ABI. */
6052 void
6053 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6055 elf_symbol_type *elfsym;
6057 /* Handle the special MIPS section numbers that a symbol may use. */
6058 elfsym = (elf_symbol_type *) asym;
6059 switch (elfsym->internal_elf_sym.st_shndx)
6061 case SHN_MIPS_ACOMMON:
6062 /* This section is used in a dynamically linked executable file.
6063 It is an allocated common section. The dynamic linker can
6064 either resolve these symbols to something in a shared
6065 library, or it can just leave them here. For our purposes,
6066 we can consider these symbols to be in a new section. */
6067 if (mips_elf_acom_section.name == NULL)
6069 /* Initialize the acommon section. */
6070 mips_elf_acom_section.name = ".acommon";
6071 mips_elf_acom_section.flags = SEC_ALLOC;
6072 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6073 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6074 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6075 mips_elf_acom_symbol.name = ".acommon";
6076 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6077 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6078 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6080 asym->section = &mips_elf_acom_section;
6081 break;
6083 case SHN_COMMON:
6084 /* Common symbols less than the GP size are automatically
6085 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6086 if (asym->value > elf_gp_size (abfd)
6087 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6088 || IRIX_COMPAT (abfd) == ict_irix6)
6089 break;
6090 /* Fall through. */
6091 case SHN_MIPS_SCOMMON:
6092 if (mips_elf_scom_section.name == NULL)
6094 /* Initialize the small common section. */
6095 mips_elf_scom_section.name = ".scommon";
6096 mips_elf_scom_section.flags = SEC_IS_COMMON;
6097 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6098 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6099 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6100 mips_elf_scom_symbol.name = ".scommon";
6101 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6102 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6103 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6105 asym->section = &mips_elf_scom_section;
6106 asym->value = elfsym->internal_elf_sym.st_size;
6107 break;
6109 case SHN_MIPS_SUNDEFINED:
6110 asym->section = bfd_und_section_ptr;
6111 break;
6113 case SHN_MIPS_TEXT:
6115 asection *section = bfd_get_section_by_name (abfd, ".text");
6117 BFD_ASSERT (SGI_COMPAT (abfd));
6118 if (section != NULL)
6120 asym->section = section;
6121 /* MIPS_TEXT is a bit special, the address is not an offset
6122 to the base of the .text section. So substract the section
6123 base address to make it an offset. */
6124 asym->value -= section->vma;
6127 break;
6129 case SHN_MIPS_DATA:
6131 asection *section = bfd_get_section_by_name (abfd, ".data");
6133 BFD_ASSERT (SGI_COMPAT (abfd));
6134 if (section != NULL)
6136 asym->section = section;
6137 /* MIPS_DATA is a bit special, the address is not an offset
6138 to the base of the .data section. So substract the section
6139 base address to make it an offset. */
6140 asym->value -= section->vma;
6143 break;
6146 /* If this is an odd-valued function symbol, assume it's a MIPS16 one. */
6147 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6148 && (asym->value & 1) != 0)
6150 asym->value--;
6151 elfsym->internal_elf_sym.st_other
6152 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6156 /* Implement elf_backend_eh_frame_address_size. This differs from
6157 the default in the way it handles EABI64.
6159 EABI64 was originally specified as an LP64 ABI, and that is what
6160 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6161 historically accepted the combination of -mabi=eabi and -mlong32,
6162 and this ILP32 variation has become semi-official over time.
6163 Both forms use elf32 and have pointer-sized FDE addresses.
6165 If an EABI object was generated by GCC 4.0 or above, it will have
6166 an empty .gcc_compiled_longXX section, where XX is the size of longs
6167 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6168 have no special marking to distinguish them from LP64 objects.
6170 We don't want users of the official LP64 ABI to be punished for the
6171 existence of the ILP32 variant, but at the same time, we don't want
6172 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6173 We therefore take the following approach:
6175 - If ABFD contains a .gcc_compiled_longXX section, use it to
6176 determine the pointer size.
6178 - Otherwise check the type of the first relocation. Assume that
6179 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6181 - Otherwise punt.
6183 The second check is enough to detect LP64 objects generated by pre-4.0
6184 compilers because, in the kind of output generated by those compilers,
6185 the first relocation will be associated with either a CIE personality
6186 routine or an FDE start address. Furthermore, the compilers never
6187 used a special (non-pointer) encoding for this ABI.
6189 Checking the relocation type should also be safe because there is no
6190 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6191 did so. */
6193 unsigned int
6194 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6196 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6197 return 8;
6198 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6200 bfd_boolean long32_p, long64_p;
6202 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6203 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6204 if (long32_p && long64_p)
6205 return 0;
6206 if (long32_p)
6207 return 4;
6208 if (long64_p)
6209 return 8;
6211 if (sec->reloc_count > 0
6212 && elf_section_data (sec)->relocs != NULL
6213 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6214 == R_MIPS_64))
6215 return 8;
6217 return 0;
6219 return 4;
6222 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6223 relocations against two unnamed section symbols to resolve to the
6224 same address. For example, if we have code like:
6226 lw $4,%got_disp(.data)($gp)
6227 lw $25,%got_disp(.text)($gp)
6228 jalr $25
6230 then the linker will resolve both relocations to .data and the program
6231 will jump there rather than to .text.
6233 We can work around this problem by giving names to local section symbols.
6234 This is also what the MIPSpro tools do. */
6236 bfd_boolean
6237 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6239 return SGI_COMPAT (abfd);
6242 /* Work over a section just before writing it out. This routine is
6243 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6244 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6245 a better way. */
6247 bfd_boolean
6248 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6250 if (hdr->sh_type == SHT_MIPS_REGINFO
6251 && hdr->sh_size > 0)
6253 bfd_byte buf[4];
6255 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6256 BFD_ASSERT (hdr->contents == NULL);
6258 if (bfd_seek (abfd,
6259 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6260 SEEK_SET) != 0)
6261 return FALSE;
6262 H_PUT_32 (abfd, elf_gp (abfd), buf);
6263 if (bfd_bwrite (buf, 4, abfd) != 4)
6264 return FALSE;
6267 if (hdr->sh_type == SHT_MIPS_OPTIONS
6268 && hdr->bfd_section != NULL
6269 && mips_elf_section_data (hdr->bfd_section) != NULL
6270 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6272 bfd_byte *contents, *l, *lend;
6274 /* We stored the section contents in the tdata field in the
6275 set_section_contents routine. We save the section contents
6276 so that we don't have to read them again.
6277 At this point we know that elf_gp is set, so we can look
6278 through the section contents to see if there is an
6279 ODK_REGINFO structure. */
6281 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6282 l = contents;
6283 lend = contents + hdr->sh_size;
6284 while (l + sizeof (Elf_External_Options) <= lend)
6286 Elf_Internal_Options intopt;
6288 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6289 &intopt);
6290 if (intopt.size < sizeof (Elf_External_Options))
6292 (*_bfd_error_handler)
6293 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6294 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6295 break;
6297 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6299 bfd_byte buf[8];
6301 if (bfd_seek (abfd,
6302 (hdr->sh_offset
6303 + (l - contents)
6304 + sizeof (Elf_External_Options)
6305 + (sizeof (Elf64_External_RegInfo) - 8)),
6306 SEEK_SET) != 0)
6307 return FALSE;
6308 H_PUT_64 (abfd, elf_gp (abfd), buf);
6309 if (bfd_bwrite (buf, 8, abfd) != 8)
6310 return FALSE;
6312 else if (intopt.kind == ODK_REGINFO)
6314 bfd_byte buf[4];
6316 if (bfd_seek (abfd,
6317 (hdr->sh_offset
6318 + (l - contents)
6319 + sizeof (Elf_External_Options)
6320 + (sizeof (Elf32_External_RegInfo) - 4)),
6321 SEEK_SET) != 0)
6322 return FALSE;
6323 H_PUT_32 (abfd, elf_gp (abfd), buf);
6324 if (bfd_bwrite (buf, 4, abfd) != 4)
6325 return FALSE;
6327 l += intopt.size;
6331 if (hdr->bfd_section != NULL)
6333 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6335 /* .sbss is not handled specially here because the GNU/Linux
6336 prelinker can convert .sbss from NOBITS to PROGBITS and
6337 changing it back to NOBITS breaks the binary. The entry in
6338 _bfd_mips_elf_special_sections will ensure the correct flags
6339 are set on .sbss if BFD creates it without reading it from an
6340 input file, and without special handling here the flags set
6341 on it in an input file will be followed. */
6342 if (strcmp (name, ".sdata") == 0
6343 || strcmp (name, ".lit8") == 0
6344 || strcmp (name, ".lit4") == 0)
6346 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6347 hdr->sh_type = SHT_PROGBITS;
6349 else if (strcmp (name, ".srdata") == 0)
6351 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6352 hdr->sh_type = SHT_PROGBITS;
6354 else if (strcmp (name, ".compact_rel") == 0)
6356 hdr->sh_flags = 0;
6357 hdr->sh_type = SHT_PROGBITS;
6359 else if (strcmp (name, ".rtproc") == 0)
6361 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6363 unsigned int adjust;
6365 adjust = hdr->sh_size % hdr->sh_addralign;
6366 if (adjust != 0)
6367 hdr->sh_size += hdr->sh_addralign - adjust;
6372 return TRUE;
6375 /* Handle a MIPS specific section when reading an object file. This
6376 is called when elfcode.h finds a section with an unknown type.
6377 This routine supports both the 32-bit and 64-bit ELF ABI.
6379 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6380 how to. */
6382 bfd_boolean
6383 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6384 Elf_Internal_Shdr *hdr,
6385 const char *name,
6386 int shindex)
6388 flagword flags = 0;
6390 /* There ought to be a place to keep ELF backend specific flags, but
6391 at the moment there isn't one. We just keep track of the
6392 sections by their name, instead. Fortunately, the ABI gives
6393 suggested names for all the MIPS specific sections, so we will
6394 probably get away with this. */
6395 switch (hdr->sh_type)
6397 case SHT_MIPS_LIBLIST:
6398 if (strcmp (name, ".liblist") != 0)
6399 return FALSE;
6400 break;
6401 case SHT_MIPS_MSYM:
6402 if (strcmp (name, ".msym") != 0)
6403 return FALSE;
6404 break;
6405 case SHT_MIPS_CONFLICT:
6406 if (strcmp (name, ".conflict") != 0)
6407 return FALSE;
6408 break;
6409 case SHT_MIPS_GPTAB:
6410 if (! CONST_STRNEQ (name, ".gptab."))
6411 return FALSE;
6412 break;
6413 case SHT_MIPS_UCODE:
6414 if (strcmp (name, ".ucode") != 0)
6415 return FALSE;
6416 break;
6417 case SHT_MIPS_DEBUG:
6418 if (strcmp (name, ".mdebug") != 0)
6419 return FALSE;
6420 flags = SEC_DEBUGGING;
6421 break;
6422 case SHT_MIPS_REGINFO:
6423 if (strcmp (name, ".reginfo") != 0
6424 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6425 return FALSE;
6426 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6427 break;
6428 case SHT_MIPS_IFACE:
6429 if (strcmp (name, ".MIPS.interfaces") != 0)
6430 return FALSE;
6431 break;
6432 case SHT_MIPS_CONTENT:
6433 if (! CONST_STRNEQ (name, ".MIPS.content"))
6434 return FALSE;
6435 break;
6436 case SHT_MIPS_OPTIONS:
6437 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6438 return FALSE;
6439 break;
6440 case SHT_MIPS_DWARF:
6441 if (! CONST_STRNEQ (name, ".debug_")
6442 && ! CONST_STRNEQ (name, ".zdebug_"))
6443 return FALSE;
6444 break;
6445 case SHT_MIPS_SYMBOL_LIB:
6446 if (strcmp (name, ".MIPS.symlib") != 0)
6447 return FALSE;
6448 break;
6449 case SHT_MIPS_EVENTS:
6450 if (! CONST_STRNEQ (name, ".MIPS.events")
6451 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6452 return FALSE;
6453 break;
6454 default:
6455 break;
6458 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6459 return FALSE;
6461 if (flags)
6463 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6464 (bfd_get_section_flags (abfd,
6465 hdr->bfd_section)
6466 | flags)))
6467 return FALSE;
6470 /* FIXME: We should record sh_info for a .gptab section. */
6472 /* For a .reginfo section, set the gp value in the tdata information
6473 from the contents of this section. We need the gp value while
6474 processing relocs, so we just get it now. The .reginfo section
6475 is not used in the 64-bit MIPS ELF ABI. */
6476 if (hdr->sh_type == SHT_MIPS_REGINFO)
6478 Elf32_External_RegInfo ext;
6479 Elf32_RegInfo s;
6481 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6482 &ext, 0, sizeof ext))
6483 return FALSE;
6484 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6485 elf_gp (abfd) = s.ri_gp_value;
6488 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6489 set the gp value based on what we find. We may see both
6490 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6491 they should agree. */
6492 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6494 bfd_byte *contents, *l, *lend;
6496 contents = bfd_malloc (hdr->sh_size);
6497 if (contents == NULL)
6498 return FALSE;
6499 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6500 0, hdr->sh_size))
6502 free (contents);
6503 return FALSE;
6505 l = contents;
6506 lend = contents + hdr->sh_size;
6507 while (l + sizeof (Elf_External_Options) <= lend)
6509 Elf_Internal_Options intopt;
6511 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6512 &intopt);
6513 if (intopt.size < sizeof (Elf_External_Options))
6515 (*_bfd_error_handler)
6516 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6517 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6518 break;
6520 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6522 Elf64_Internal_RegInfo intreg;
6524 bfd_mips_elf64_swap_reginfo_in
6525 (abfd,
6526 ((Elf64_External_RegInfo *)
6527 (l + sizeof (Elf_External_Options))),
6528 &intreg);
6529 elf_gp (abfd) = intreg.ri_gp_value;
6531 else if (intopt.kind == ODK_REGINFO)
6533 Elf32_RegInfo intreg;
6535 bfd_mips_elf32_swap_reginfo_in
6536 (abfd,
6537 ((Elf32_External_RegInfo *)
6538 (l + sizeof (Elf_External_Options))),
6539 &intreg);
6540 elf_gp (abfd) = intreg.ri_gp_value;
6542 l += intopt.size;
6544 free (contents);
6547 return TRUE;
6550 /* Set the correct type for a MIPS ELF section. We do this by the
6551 section name, which is a hack, but ought to work. This routine is
6552 used by both the 32-bit and the 64-bit ABI. */
6554 bfd_boolean
6555 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
6557 const char *name = bfd_get_section_name (abfd, sec);
6559 if (strcmp (name, ".liblist") == 0)
6561 hdr->sh_type = SHT_MIPS_LIBLIST;
6562 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
6563 /* The sh_link field is set in final_write_processing. */
6565 else if (strcmp (name, ".conflict") == 0)
6566 hdr->sh_type = SHT_MIPS_CONFLICT;
6567 else if (CONST_STRNEQ (name, ".gptab."))
6569 hdr->sh_type = SHT_MIPS_GPTAB;
6570 hdr->sh_entsize = sizeof (Elf32_External_gptab);
6571 /* The sh_info field is set in final_write_processing. */
6573 else if (strcmp (name, ".ucode") == 0)
6574 hdr->sh_type = SHT_MIPS_UCODE;
6575 else if (strcmp (name, ".mdebug") == 0)
6577 hdr->sh_type = SHT_MIPS_DEBUG;
6578 /* In a shared object on IRIX 5.3, the .mdebug section has an
6579 entsize of 0. FIXME: Does this matter? */
6580 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
6581 hdr->sh_entsize = 0;
6582 else
6583 hdr->sh_entsize = 1;
6585 else if (strcmp (name, ".reginfo") == 0)
6587 hdr->sh_type = SHT_MIPS_REGINFO;
6588 /* In a shared object on IRIX 5.3, the .reginfo section has an
6589 entsize of 0x18. FIXME: Does this matter? */
6590 if (SGI_COMPAT (abfd))
6592 if ((abfd->flags & DYNAMIC) != 0)
6593 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6594 else
6595 hdr->sh_entsize = 1;
6597 else
6598 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
6600 else if (SGI_COMPAT (abfd)
6601 && (strcmp (name, ".hash") == 0
6602 || strcmp (name, ".dynamic") == 0
6603 || strcmp (name, ".dynstr") == 0))
6605 if (SGI_COMPAT (abfd))
6606 hdr->sh_entsize = 0;
6607 #if 0
6608 /* This isn't how the IRIX6 linker behaves. */
6609 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
6610 #endif
6612 else if (strcmp (name, ".got") == 0
6613 || strcmp (name, ".srdata") == 0
6614 || strcmp (name, ".sdata") == 0
6615 || strcmp (name, ".sbss") == 0
6616 || strcmp (name, ".lit4") == 0
6617 || strcmp (name, ".lit8") == 0)
6618 hdr->sh_flags |= SHF_MIPS_GPREL;
6619 else if (strcmp (name, ".MIPS.interfaces") == 0)
6621 hdr->sh_type = SHT_MIPS_IFACE;
6622 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6624 else if (CONST_STRNEQ (name, ".MIPS.content"))
6626 hdr->sh_type = SHT_MIPS_CONTENT;
6627 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6628 /* The sh_info field is set in final_write_processing. */
6630 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6632 hdr->sh_type = SHT_MIPS_OPTIONS;
6633 hdr->sh_entsize = 1;
6634 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6636 else if (CONST_STRNEQ (name, ".debug_")
6637 || CONST_STRNEQ (name, ".zdebug_"))
6639 hdr->sh_type = SHT_MIPS_DWARF;
6641 /* Irix facilities such as libexc expect a single .debug_frame
6642 per executable, the system ones have NOSTRIP set and the linker
6643 doesn't merge sections with different flags so ... */
6644 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
6645 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6647 else if (strcmp (name, ".MIPS.symlib") == 0)
6649 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
6650 /* The sh_link and sh_info fields are set in
6651 final_write_processing. */
6653 else if (CONST_STRNEQ (name, ".MIPS.events")
6654 || CONST_STRNEQ (name, ".MIPS.post_rel"))
6656 hdr->sh_type = SHT_MIPS_EVENTS;
6657 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
6658 /* The sh_link field is set in final_write_processing. */
6660 else if (strcmp (name, ".msym") == 0)
6662 hdr->sh_type = SHT_MIPS_MSYM;
6663 hdr->sh_flags |= SHF_ALLOC;
6664 hdr->sh_entsize = 8;
6667 /* The generic elf_fake_sections will set up REL_HDR using the default
6668 kind of relocations. We used to set up a second header for the
6669 non-default kind of relocations here, but only NewABI would use
6670 these, and the IRIX ld doesn't like resulting empty RELA sections.
6671 Thus we create those header only on demand now. */
6673 return TRUE;
6676 /* Given a BFD section, try to locate the corresponding ELF section
6677 index. This is used by both the 32-bit and the 64-bit ABI.
6678 Actually, it's not clear to me that the 64-bit ABI supports these,
6679 but for non-PIC objects we will certainly want support for at least
6680 the .scommon section. */
6682 bfd_boolean
6683 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6684 asection *sec, int *retval)
6686 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
6688 *retval = SHN_MIPS_SCOMMON;
6689 return TRUE;
6691 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
6693 *retval = SHN_MIPS_ACOMMON;
6694 return TRUE;
6696 return FALSE;
6699 /* Hook called by the linker routine which adds symbols from an object
6700 file. We must handle the special MIPS section numbers here. */
6702 bfd_boolean
6703 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
6704 Elf_Internal_Sym *sym, const char **namep,
6705 flagword *flagsp ATTRIBUTE_UNUSED,
6706 asection **secp, bfd_vma *valp)
6708 if (SGI_COMPAT (abfd)
6709 && (abfd->flags & DYNAMIC) != 0
6710 && strcmp (*namep, "_rld_new_interface") == 0)
6712 /* Skip IRIX5 rld entry name. */
6713 *namep = NULL;
6714 return TRUE;
6717 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
6718 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
6719 by setting a DT_NEEDED for the shared object. Since _gp_disp is
6720 a magic symbol resolved by the linker, we ignore this bogus definition
6721 of _gp_disp. New ABI objects do not suffer from this problem so this
6722 is not done for them. */
6723 if (!NEWABI_P(abfd)
6724 && (sym->st_shndx == SHN_ABS)
6725 && (strcmp (*namep, "_gp_disp") == 0))
6727 *namep = NULL;
6728 return TRUE;
6731 switch (sym->st_shndx)
6733 case SHN_COMMON:
6734 /* Common symbols less than the GP size are automatically
6735 treated as SHN_MIPS_SCOMMON symbols. */
6736 if (sym->st_size > elf_gp_size (abfd)
6737 || ELF_ST_TYPE (sym->st_info) == STT_TLS
6738 || IRIX_COMPAT (abfd) == ict_irix6)
6739 break;
6740 /* Fall through. */
6741 case SHN_MIPS_SCOMMON:
6742 *secp = bfd_make_section_old_way (abfd, ".scommon");
6743 (*secp)->flags |= SEC_IS_COMMON;
6744 *valp = sym->st_size;
6745 break;
6747 case SHN_MIPS_TEXT:
6748 /* This section is used in a shared object. */
6749 if (elf_tdata (abfd)->elf_text_section == NULL)
6751 asymbol *elf_text_symbol;
6752 asection *elf_text_section;
6753 bfd_size_type amt = sizeof (asection);
6755 elf_text_section = bfd_zalloc (abfd, amt);
6756 if (elf_text_section == NULL)
6757 return FALSE;
6759 amt = sizeof (asymbol);
6760 elf_text_symbol = bfd_zalloc (abfd, amt);
6761 if (elf_text_symbol == NULL)
6762 return FALSE;
6764 /* Initialize the section. */
6766 elf_tdata (abfd)->elf_text_section = elf_text_section;
6767 elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
6769 elf_text_section->symbol = elf_text_symbol;
6770 elf_text_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_text_symbol;
6772 elf_text_section->name = ".text";
6773 elf_text_section->flags = SEC_NO_FLAGS;
6774 elf_text_section->output_section = NULL;
6775 elf_text_section->owner = abfd;
6776 elf_text_symbol->name = ".text";
6777 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6778 elf_text_symbol->section = elf_text_section;
6780 /* This code used to do *secp = bfd_und_section_ptr if
6781 info->shared. I don't know why, and that doesn't make sense,
6782 so I took it out. */
6783 *secp = elf_tdata (abfd)->elf_text_section;
6784 break;
6786 case SHN_MIPS_ACOMMON:
6787 /* Fall through. XXX Can we treat this as allocated data? */
6788 case SHN_MIPS_DATA:
6789 /* This section is used in a shared object. */
6790 if (elf_tdata (abfd)->elf_data_section == NULL)
6792 asymbol *elf_data_symbol;
6793 asection *elf_data_section;
6794 bfd_size_type amt = sizeof (asection);
6796 elf_data_section = bfd_zalloc (abfd, amt);
6797 if (elf_data_section == NULL)
6798 return FALSE;
6800 amt = sizeof (asymbol);
6801 elf_data_symbol = bfd_zalloc (abfd, amt);
6802 if (elf_data_symbol == NULL)
6803 return FALSE;
6805 /* Initialize the section. */
6807 elf_tdata (abfd)->elf_data_section = elf_data_section;
6808 elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
6810 elf_data_section->symbol = elf_data_symbol;
6811 elf_data_section->symbol_ptr_ptr = &elf_tdata (abfd)->elf_data_symbol;
6813 elf_data_section->name = ".data";
6814 elf_data_section->flags = SEC_NO_FLAGS;
6815 elf_data_section->output_section = NULL;
6816 elf_data_section->owner = abfd;
6817 elf_data_symbol->name = ".data";
6818 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
6819 elf_data_symbol->section = elf_data_section;
6821 /* This code used to do *secp = bfd_und_section_ptr if
6822 info->shared. I don't know why, and that doesn't make sense,
6823 so I took it out. */
6824 *secp = elf_tdata (abfd)->elf_data_section;
6825 break;
6827 case SHN_MIPS_SUNDEFINED:
6828 *secp = bfd_und_section_ptr;
6829 break;
6832 if (SGI_COMPAT (abfd)
6833 && ! info->shared
6834 && info->output_bfd->xvec == abfd->xvec
6835 && strcmp (*namep, "__rld_obj_head") == 0)
6837 struct elf_link_hash_entry *h;
6838 struct bfd_link_hash_entry *bh;
6840 /* Mark __rld_obj_head as dynamic. */
6841 bh = NULL;
6842 if (! (_bfd_generic_link_add_one_symbol
6843 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
6844 get_elf_backend_data (abfd)->collect, &bh)))
6845 return FALSE;
6847 h = (struct elf_link_hash_entry *) bh;
6848 h->non_elf = 0;
6849 h->def_regular = 1;
6850 h->type = STT_OBJECT;
6852 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6853 return FALSE;
6855 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
6858 /* If this is a mips16 text symbol, add 1 to the value to make it
6859 odd. This will cause something like .word SYM to come up with
6860 the right value when it is loaded into the PC. */
6861 if (ELF_ST_IS_MIPS16 (sym->st_other))
6862 ++*valp;
6864 return TRUE;
6867 /* This hook function is called before the linker writes out a global
6868 symbol. We mark symbols as small common if appropriate. This is
6869 also where we undo the increment of the value for a mips16 symbol. */
6872 _bfd_mips_elf_link_output_symbol_hook
6873 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6874 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
6875 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6877 /* If we see a common symbol, which implies a relocatable link, then
6878 if a symbol was small common in an input file, mark it as small
6879 common in the output file. */
6880 if (sym->st_shndx == SHN_COMMON
6881 && strcmp (input_sec->name, ".scommon") == 0)
6882 sym->st_shndx = SHN_MIPS_SCOMMON;
6884 if (ELF_ST_IS_MIPS16 (sym->st_other))
6885 sym->st_value &= ~1;
6887 return 1;
6890 /* Functions for the dynamic linker. */
6892 /* Create dynamic sections when linking against a dynamic object. */
6894 bfd_boolean
6895 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
6897 struct elf_link_hash_entry *h;
6898 struct bfd_link_hash_entry *bh;
6899 flagword flags;
6900 register asection *s;
6901 const char * const *namep;
6902 struct mips_elf_link_hash_table *htab;
6904 htab = mips_elf_hash_table (info);
6905 BFD_ASSERT (htab != NULL);
6907 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
6908 | SEC_LINKER_CREATED | SEC_READONLY);
6910 /* The psABI requires a read-only .dynamic section, but the VxWorks
6911 EABI doesn't. */
6912 if (!htab->is_vxworks)
6914 s = bfd_get_section_by_name (abfd, ".dynamic");
6915 if (s != NULL)
6917 if (! bfd_set_section_flags (abfd, s, flags))
6918 return FALSE;
6922 /* We need to create .got section. */
6923 if (!mips_elf_create_got_section (abfd, info))
6924 return FALSE;
6926 if (! mips_elf_rel_dyn_section (info, TRUE))
6927 return FALSE;
6929 /* Create .stub section. */
6930 s = bfd_make_section_with_flags (abfd,
6931 MIPS_ELF_STUB_SECTION_NAME (abfd),
6932 flags | SEC_CODE);
6933 if (s == NULL
6934 || ! bfd_set_section_alignment (abfd, s,
6935 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6936 return FALSE;
6937 htab->sstubs = s;
6939 if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
6940 && !info->shared
6941 && bfd_get_section_by_name (abfd, ".rld_map") == NULL)
6943 s = bfd_make_section_with_flags (abfd, ".rld_map",
6944 flags &~ (flagword) SEC_READONLY);
6945 if (s == NULL
6946 || ! bfd_set_section_alignment (abfd, s,
6947 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
6948 return FALSE;
6951 /* On IRIX5, we adjust add some additional symbols and change the
6952 alignments of several sections. There is no ABI documentation
6953 indicating that this is necessary on IRIX6, nor any evidence that
6954 the linker takes such action. */
6955 if (IRIX_COMPAT (abfd) == ict_irix5)
6957 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
6959 bh = NULL;
6960 if (! (_bfd_generic_link_add_one_symbol
6961 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
6962 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
6963 return FALSE;
6965 h = (struct elf_link_hash_entry *) bh;
6966 h->non_elf = 0;
6967 h->def_regular = 1;
6968 h->type = STT_SECTION;
6970 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6971 return FALSE;
6974 /* We need to create a .compact_rel section. */
6975 if (SGI_COMPAT (abfd))
6977 if (!mips_elf_create_compact_rel_section (abfd, info))
6978 return FALSE;
6981 /* Change alignments of some sections. */
6982 s = bfd_get_section_by_name (abfd, ".hash");
6983 if (s != NULL)
6984 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6985 s = bfd_get_section_by_name (abfd, ".dynsym");
6986 if (s != NULL)
6987 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6988 s = bfd_get_section_by_name (abfd, ".dynstr");
6989 if (s != NULL)
6990 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6991 s = bfd_get_section_by_name (abfd, ".reginfo");
6992 if (s != NULL)
6993 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6994 s = bfd_get_section_by_name (abfd, ".dynamic");
6995 if (s != NULL)
6996 bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
6999 if (!info->shared)
7001 const char *name;
7003 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7004 bh = NULL;
7005 if (!(_bfd_generic_link_add_one_symbol
7006 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7007 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7008 return FALSE;
7010 h = (struct elf_link_hash_entry *) bh;
7011 h->non_elf = 0;
7012 h->def_regular = 1;
7013 h->type = STT_SECTION;
7015 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7016 return FALSE;
7018 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7020 /* __rld_map is a four byte word located in the .data section
7021 and is filled in by the rtld to contain a pointer to
7022 the _r_debug structure. Its symbol value will be set in
7023 _bfd_mips_elf_finish_dynamic_symbol. */
7024 s = bfd_get_section_by_name (abfd, ".rld_map");
7025 BFD_ASSERT (s != NULL);
7027 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7028 bh = NULL;
7029 if (!(_bfd_generic_link_add_one_symbol
7030 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7031 get_elf_backend_data (abfd)->collect, &bh)))
7032 return FALSE;
7034 h = (struct elf_link_hash_entry *) bh;
7035 h->non_elf = 0;
7036 h->def_regular = 1;
7037 h->type = STT_OBJECT;
7039 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7040 return FALSE;
7044 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7045 Also create the _PROCEDURE_LINKAGE_TABLE symbol. */
7046 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7047 return FALSE;
7049 /* Cache the sections created above. */
7050 htab->splt = bfd_get_section_by_name (abfd, ".plt");
7051 htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
7052 if (htab->is_vxworks)
7054 htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
7055 htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
7057 else
7058 htab->srelplt = bfd_get_section_by_name (abfd, ".rel.plt");
7059 if (!htab->sdynbss
7060 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7061 || !htab->srelplt
7062 || !htab->splt)
7063 abort ();
7065 if (htab->is_vxworks)
7067 /* Do the usual VxWorks handling. */
7068 if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7069 return FALSE;
7071 /* Work out the PLT sizes. */
7072 if (info->shared)
7074 htab->plt_header_size
7075 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
7076 htab->plt_entry_size
7077 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
7079 else
7081 htab->plt_header_size
7082 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
7083 htab->plt_entry_size
7084 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
7087 else if (!info->shared)
7089 /* All variants of the plt0 entry are the same size. */
7090 htab->plt_header_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
7091 htab->plt_entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
7094 return TRUE;
7097 /* Return true if relocation REL against section SEC is a REL rather than
7098 RELA relocation. RELOCS is the first relocation in the section and
7099 ABFD is the bfd that contains SEC. */
7101 static bfd_boolean
7102 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7103 const Elf_Internal_Rela *relocs,
7104 const Elf_Internal_Rela *rel)
7106 Elf_Internal_Shdr *rel_hdr;
7107 const struct elf_backend_data *bed;
7109 /* To determine which flavor of relocation this is, we depend on the
7110 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7111 rel_hdr = elf_section_data (sec)->rel.hdr;
7112 if (rel_hdr == NULL)
7113 return FALSE;
7114 bed = get_elf_backend_data (abfd);
7115 return ((size_t) (rel - relocs)
7116 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7119 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7120 HOWTO is the relocation's howto and CONTENTS points to the contents
7121 of the section that REL is against. */
7123 static bfd_vma
7124 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7125 reloc_howto_type *howto, bfd_byte *contents)
7127 bfd_byte *location;
7128 unsigned int r_type;
7129 bfd_vma addend;
7131 r_type = ELF_R_TYPE (abfd, rel->r_info);
7132 location = contents + rel->r_offset;
7134 /* Get the addend, which is stored in the input file. */
7135 _bfd_mips16_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7136 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7137 _bfd_mips16_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7139 return addend & howto->src_mask;
7142 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7143 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7144 and update *ADDEND with the final addend. Return true on success
7145 or false if the LO16 could not be found. RELEND is the exclusive
7146 upper bound on the relocations for REL's section. */
7148 static bfd_boolean
7149 mips_elf_add_lo16_rel_addend (bfd *abfd,
7150 const Elf_Internal_Rela *rel,
7151 const Elf_Internal_Rela *relend,
7152 bfd_byte *contents, bfd_vma *addend)
7154 unsigned int r_type, lo16_type;
7155 const Elf_Internal_Rela *lo16_relocation;
7156 reloc_howto_type *lo16_howto;
7157 bfd_vma l;
7159 r_type = ELF_R_TYPE (abfd, rel->r_info);
7160 if (mips16_reloc_p (r_type))
7161 lo16_type = R_MIPS16_LO16;
7162 else
7163 lo16_type = R_MIPS_LO16;
7165 /* The combined value is the sum of the HI16 addend, left-shifted by
7166 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7167 code does a `lui' of the HI16 value, and then an `addiu' of the
7168 LO16 value.)
7170 Scan ahead to find a matching LO16 relocation.
7172 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7173 be immediately following. However, for the IRIX6 ABI, the next
7174 relocation may be a composed relocation consisting of several
7175 relocations for the same address. In that case, the R_MIPS_LO16
7176 relocation may occur as one of these. We permit a similar
7177 extension in general, as that is useful for GCC.
7179 In some cases GCC dead code elimination removes the LO16 but keeps
7180 the corresponding HI16. This is strictly speaking a violation of
7181 the ABI but not immediately harmful. */
7182 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7183 if (lo16_relocation == NULL)
7184 return FALSE;
7186 /* Obtain the addend kept there. */
7187 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7188 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7190 l <<= lo16_howto->rightshift;
7191 l = _bfd_mips_elf_sign_extend (l, 16);
7193 *addend <<= 16;
7194 *addend += l;
7195 return TRUE;
7198 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7199 store the contents in *CONTENTS on success. Assume that *CONTENTS
7200 already holds the contents if it is nonull on entry. */
7202 static bfd_boolean
7203 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7205 if (*contents)
7206 return TRUE;
7208 /* Get cached copy if it exists. */
7209 if (elf_section_data (sec)->this_hdr.contents != NULL)
7211 *contents = elf_section_data (sec)->this_hdr.contents;
7212 return TRUE;
7215 return bfd_malloc_and_get_section (abfd, sec, contents);
7218 /* Look through the relocs for a section during the first phase, and
7219 allocate space in the global offset table. */
7221 bfd_boolean
7222 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7223 asection *sec, const Elf_Internal_Rela *relocs)
7225 const char *name;
7226 bfd *dynobj;
7227 Elf_Internal_Shdr *symtab_hdr;
7228 struct elf_link_hash_entry **sym_hashes;
7229 size_t extsymoff;
7230 const Elf_Internal_Rela *rel;
7231 const Elf_Internal_Rela *rel_end;
7232 asection *sreloc;
7233 const struct elf_backend_data *bed;
7234 struct mips_elf_link_hash_table *htab;
7235 bfd_byte *contents;
7236 bfd_vma addend;
7237 reloc_howto_type *howto;
7239 if (info->relocatable)
7240 return TRUE;
7242 htab = mips_elf_hash_table (info);
7243 BFD_ASSERT (htab != NULL);
7245 dynobj = elf_hash_table (info)->dynobj;
7246 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7247 sym_hashes = elf_sym_hashes (abfd);
7248 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7250 bed = get_elf_backend_data (abfd);
7251 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7253 /* Check for the mips16 stub sections. */
7255 name = bfd_get_section_name (abfd, sec);
7256 if (FN_STUB_P (name))
7258 unsigned long r_symndx;
7260 /* Look at the relocation information to figure out which symbol
7261 this is for. */
7263 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7264 if (r_symndx == 0)
7266 (*_bfd_error_handler)
7267 (_("%B: Warning: cannot determine the target function for"
7268 " stub section `%s'"),
7269 abfd, name);
7270 bfd_set_error (bfd_error_bad_value);
7271 return FALSE;
7274 if (r_symndx < extsymoff
7275 || sym_hashes[r_symndx - extsymoff] == NULL)
7277 asection *o;
7279 /* This stub is for a local symbol. This stub will only be
7280 needed if there is some relocation in this BFD, other
7281 than a 16 bit function call, which refers to this symbol. */
7282 for (o = abfd->sections; o != NULL; o = o->next)
7284 Elf_Internal_Rela *sec_relocs;
7285 const Elf_Internal_Rela *r, *rend;
7287 /* We can ignore stub sections when looking for relocs. */
7288 if ((o->flags & SEC_RELOC) == 0
7289 || o->reloc_count == 0
7290 || section_allows_mips16_refs_p (o))
7291 continue;
7293 sec_relocs
7294 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7295 info->keep_memory);
7296 if (sec_relocs == NULL)
7297 return FALSE;
7299 rend = sec_relocs + o->reloc_count;
7300 for (r = sec_relocs; r < rend; r++)
7301 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7302 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7303 break;
7305 if (elf_section_data (o)->relocs != sec_relocs)
7306 free (sec_relocs);
7308 if (r < rend)
7309 break;
7312 if (o == NULL)
7314 /* There is no non-call reloc for this stub, so we do
7315 not need it. Since this function is called before
7316 the linker maps input sections to output sections, we
7317 can easily discard it by setting the SEC_EXCLUDE
7318 flag. */
7319 sec->flags |= SEC_EXCLUDE;
7320 return TRUE;
7323 /* Record this stub in an array of local symbol stubs for
7324 this BFD. */
7325 if (elf_tdata (abfd)->local_stubs == NULL)
7327 unsigned long symcount;
7328 asection **n;
7329 bfd_size_type amt;
7331 if (elf_bad_symtab (abfd))
7332 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7333 else
7334 symcount = symtab_hdr->sh_info;
7335 amt = symcount * sizeof (asection *);
7336 n = bfd_zalloc (abfd, amt);
7337 if (n == NULL)
7338 return FALSE;
7339 elf_tdata (abfd)->local_stubs = n;
7342 sec->flags |= SEC_KEEP;
7343 elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7345 /* We don't need to set mips16_stubs_seen in this case.
7346 That flag is used to see whether we need to look through
7347 the global symbol table for stubs. We don't need to set
7348 it here, because we just have a local stub. */
7350 else
7352 struct mips_elf_link_hash_entry *h;
7354 h = ((struct mips_elf_link_hash_entry *)
7355 sym_hashes[r_symndx - extsymoff]);
7357 while (h->root.root.type == bfd_link_hash_indirect
7358 || h->root.root.type == bfd_link_hash_warning)
7359 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7361 /* H is the symbol this stub is for. */
7363 /* If we already have an appropriate stub for this function, we
7364 don't need another one, so we can discard this one. Since
7365 this function is called before the linker maps input sections
7366 to output sections, we can easily discard it by setting the
7367 SEC_EXCLUDE flag. */
7368 if (h->fn_stub != NULL)
7370 sec->flags |= SEC_EXCLUDE;
7371 return TRUE;
7374 sec->flags |= SEC_KEEP;
7375 h->fn_stub = sec;
7376 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7379 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7381 unsigned long r_symndx;
7382 struct mips_elf_link_hash_entry *h;
7383 asection **loc;
7385 /* Look at the relocation information to figure out which symbol
7386 this is for. */
7388 r_symndx = mips16_stub_symndx (sec, relocs, rel_end);
7389 if (r_symndx == 0)
7391 (*_bfd_error_handler)
7392 (_("%B: Warning: cannot determine the target function for"
7393 " stub section `%s'"),
7394 abfd, name);
7395 bfd_set_error (bfd_error_bad_value);
7396 return FALSE;
7399 if (r_symndx < extsymoff
7400 || sym_hashes[r_symndx - extsymoff] == NULL)
7402 asection *o;
7404 /* This stub is for a local symbol. This stub will only be
7405 needed if there is some relocation (R_MIPS16_26) in this BFD
7406 that refers to this symbol. */
7407 for (o = abfd->sections; o != NULL; o = o->next)
7409 Elf_Internal_Rela *sec_relocs;
7410 const Elf_Internal_Rela *r, *rend;
7412 /* We can ignore stub sections when looking for relocs. */
7413 if ((o->flags & SEC_RELOC) == 0
7414 || o->reloc_count == 0
7415 || section_allows_mips16_refs_p (o))
7416 continue;
7418 sec_relocs
7419 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7420 info->keep_memory);
7421 if (sec_relocs == NULL)
7422 return FALSE;
7424 rend = sec_relocs + o->reloc_count;
7425 for (r = sec_relocs; r < rend; r++)
7426 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7427 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7428 break;
7430 if (elf_section_data (o)->relocs != sec_relocs)
7431 free (sec_relocs);
7433 if (r < rend)
7434 break;
7437 if (o == NULL)
7439 /* There is no non-call reloc for this stub, so we do
7440 not need it. Since this function is called before
7441 the linker maps input sections to output sections, we
7442 can easily discard it by setting the SEC_EXCLUDE
7443 flag. */
7444 sec->flags |= SEC_EXCLUDE;
7445 return TRUE;
7448 /* Record this stub in an array of local symbol call_stubs for
7449 this BFD. */
7450 if (elf_tdata (abfd)->local_call_stubs == NULL)
7452 unsigned long symcount;
7453 asection **n;
7454 bfd_size_type amt;
7456 if (elf_bad_symtab (abfd))
7457 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7458 else
7459 symcount = symtab_hdr->sh_info;
7460 amt = symcount * sizeof (asection *);
7461 n = bfd_zalloc (abfd, amt);
7462 if (n == NULL)
7463 return FALSE;
7464 elf_tdata (abfd)->local_call_stubs = n;
7467 sec->flags |= SEC_KEEP;
7468 elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7470 /* We don't need to set mips16_stubs_seen in this case.
7471 That flag is used to see whether we need to look through
7472 the global symbol table for stubs. We don't need to set
7473 it here, because we just have a local stub. */
7475 else
7477 h = ((struct mips_elf_link_hash_entry *)
7478 sym_hashes[r_symndx - extsymoff]);
7480 /* H is the symbol this stub is for. */
7482 if (CALL_FP_STUB_P (name))
7483 loc = &h->call_fp_stub;
7484 else
7485 loc = &h->call_stub;
7487 /* If we already have an appropriate stub for this function, we
7488 don't need another one, so we can discard this one. Since
7489 this function is called before the linker maps input sections
7490 to output sections, we can easily discard it by setting the
7491 SEC_EXCLUDE flag. */
7492 if (*loc != NULL)
7494 sec->flags |= SEC_EXCLUDE;
7495 return TRUE;
7498 sec->flags |= SEC_KEEP;
7499 *loc = sec;
7500 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7504 sreloc = NULL;
7505 contents = NULL;
7506 for (rel = relocs; rel < rel_end; ++rel)
7508 unsigned long r_symndx;
7509 unsigned int r_type;
7510 struct elf_link_hash_entry *h;
7511 bfd_boolean can_make_dynamic_p;
7513 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7514 r_type = ELF_R_TYPE (abfd, rel->r_info);
7516 if (r_symndx < extsymoff)
7517 h = NULL;
7518 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7520 (*_bfd_error_handler)
7521 (_("%B: Malformed reloc detected for section %s"),
7522 abfd, name);
7523 bfd_set_error (bfd_error_bad_value);
7524 return FALSE;
7526 else
7528 h = sym_hashes[r_symndx - extsymoff];
7529 while (h != NULL
7530 && (h->root.type == bfd_link_hash_indirect
7531 || h->root.type == bfd_link_hash_warning))
7532 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7535 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
7536 relocation into a dynamic one. */
7537 can_make_dynamic_p = FALSE;
7538 switch (r_type)
7540 case R_MIPS16_GOT16:
7541 case R_MIPS16_CALL16:
7542 case R_MIPS_GOT16:
7543 case R_MIPS_CALL16:
7544 case R_MIPS_CALL_HI16:
7545 case R_MIPS_CALL_LO16:
7546 case R_MIPS_GOT_HI16:
7547 case R_MIPS_GOT_LO16:
7548 case R_MIPS_GOT_PAGE:
7549 case R_MIPS_GOT_OFST:
7550 case R_MIPS_GOT_DISP:
7551 case R_MIPS_TLS_GOTTPREL:
7552 case R_MIPS_TLS_GD:
7553 case R_MIPS_TLS_LDM:
7554 if (dynobj == NULL)
7555 elf_hash_table (info)->dynobj = dynobj = abfd;
7556 if (!mips_elf_create_got_section (dynobj, info))
7557 return FALSE;
7558 if (htab->is_vxworks && !info->shared)
7560 (*_bfd_error_handler)
7561 (_("%B: GOT reloc at 0x%lx not expected in executables"),
7562 abfd, (unsigned long) rel->r_offset);
7563 bfd_set_error (bfd_error_bad_value);
7564 return FALSE;
7566 break;
7568 /* This is just a hint; it can safely be ignored. Don't set
7569 has_static_relocs for the corresponding symbol. */
7570 case R_MIPS_JALR:
7571 break;
7573 case R_MIPS_32:
7574 case R_MIPS_REL32:
7575 case R_MIPS_64:
7576 /* In VxWorks executables, references to external symbols
7577 must be handled using copy relocs or PLT entries; it is not
7578 possible to convert this relocation into a dynamic one.
7580 For executables that use PLTs and copy-relocs, we have a
7581 choice between converting the relocation into a dynamic
7582 one or using copy relocations or PLT entries. It is
7583 usually better to do the former, unless the relocation is
7584 against a read-only section. */
7585 if ((info->shared
7586 || (h != NULL
7587 && !htab->is_vxworks
7588 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
7589 && !(!info->nocopyreloc
7590 && !PIC_OBJECT_P (abfd)
7591 && MIPS_ELF_READONLY_SECTION (sec))))
7592 && (sec->flags & SEC_ALLOC) != 0)
7594 can_make_dynamic_p = TRUE;
7595 if (dynobj == NULL)
7596 elf_hash_table (info)->dynobj = dynobj = abfd;
7597 break;
7599 /* For sections that are not SEC_ALLOC a copy reloc would be
7600 output if possible (implying questionable semantics for
7601 read-only data objects) or otherwise the final link would
7602 fail as ld.so will not process them and could not therefore
7603 handle any outstanding dynamic relocations.
7605 For such sections that are also SEC_DEBUGGING, we can avoid
7606 these problems by simply ignoring any relocs as these
7607 sections have a predefined use and we know it is safe to do
7610 This is needed in cases such as a global symbol definition
7611 in a shared library causing a common symbol from an object
7612 file to be converted to an undefined reference. If that
7613 happens, then all the relocations against this symbol from
7614 SEC_DEBUGGING sections in the object file will resolve to
7615 nil. */
7616 if ((sec->flags & SEC_DEBUGGING) != 0)
7617 break;
7618 /* Fall through. */
7620 default:
7621 /* Most static relocations require pointer equality, except
7622 for branches. */
7623 if (h)
7624 h->pointer_equality_needed = TRUE;
7625 /* Fall through. */
7627 case R_MIPS_26:
7628 case R_MIPS_PC16:
7629 case R_MIPS16_26:
7630 if (h)
7631 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = TRUE;
7632 break;
7635 if (h)
7637 /* Relocations against the special VxWorks __GOTT_BASE__ and
7638 __GOTT_INDEX__ symbols must be left to the loader. Allocate
7639 room for them in .rela.dyn. */
7640 if (is_gott_symbol (info, h))
7642 if (sreloc == NULL)
7644 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7645 if (sreloc == NULL)
7646 return FALSE;
7648 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7649 if (MIPS_ELF_READONLY_SECTION (sec))
7650 /* We tell the dynamic linker that there are
7651 relocations against the text segment. */
7652 info->flags |= DF_TEXTREL;
7655 else if (r_type == R_MIPS_CALL_LO16
7656 || r_type == R_MIPS_GOT_LO16
7657 || r_type == R_MIPS_GOT_DISP
7658 || (got16_reloc_p (r_type) && htab->is_vxworks))
7660 /* We may need a local GOT entry for this relocation. We
7661 don't count R_MIPS_GOT_PAGE because we can estimate the
7662 maximum number of pages needed by looking at the size of
7663 the segment. Similar comments apply to R_MIPS*_GOT16 and
7664 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
7665 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
7666 R_MIPS_CALL_HI16 because these are always followed by an
7667 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
7668 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7669 rel->r_addend, info, 0))
7670 return FALSE;
7673 if (h != NULL && mips_elf_relocation_needs_la25_stub (abfd, r_type))
7674 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
7676 switch (r_type)
7678 case R_MIPS_CALL16:
7679 case R_MIPS16_CALL16:
7680 if (h == NULL)
7682 (*_bfd_error_handler)
7683 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
7684 abfd, (unsigned long) rel->r_offset);
7685 bfd_set_error (bfd_error_bad_value);
7686 return FALSE;
7688 /* Fall through. */
7690 case R_MIPS_CALL_HI16:
7691 case R_MIPS_CALL_LO16:
7692 if (h != NULL)
7694 /* Make sure there is room in the regular GOT to hold the
7695 function's address. We may eliminate it in favour of
7696 a .got.plt entry later; see mips_elf_count_got_symbols. */
7697 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE, 0))
7698 return FALSE;
7700 /* We need a stub, not a plt entry for the undefined
7701 function. But we record it as if it needs plt. See
7702 _bfd_elf_adjust_dynamic_symbol. */
7703 h->needs_plt = 1;
7704 h->type = STT_FUNC;
7706 break;
7708 case R_MIPS_GOT_PAGE:
7709 /* If this is a global, overridable symbol, GOT_PAGE will
7710 decay to GOT_DISP, so we'll need a GOT entry for it. */
7711 if (h)
7713 struct mips_elf_link_hash_entry *hmips =
7714 (struct mips_elf_link_hash_entry *) h;
7716 /* This symbol is definitely not overridable. */
7717 if (hmips->root.def_regular
7718 && ! (info->shared && ! info->symbolic
7719 && ! hmips->root.forced_local))
7720 h = NULL;
7722 /* Fall through. */
7724 case R_MIPS16_GOT16:
7725 case R_MIPS_GOT16:
7726 case R_MIPS_GOT_HI16:
7727 case R_MIPS_GOT_LO16:
7728 if (!h || r_type == R_MIPS_GOT_PAGE)
7730 /* This relocation needs (or may need, if h != NULL) a
7731 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
7732 know for sure until we know whether the symbol is
7733 preemptible. */
7734 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
7736 if (!mips_elf_get_section_contents (abfd, sec, &contents))
7737 return FALSE;
7738 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7739 addend = mips_elf_read_rel_addend (abfd, rel,
7740 howto, contents);
7741 if (got16_reloc_p (r_type))
7742 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
7743 contents, &addend);
7744 else
7745 addend <<= howto->rightshift;
7747 else
7748 addend = rel->r_addend;
7749 if (!mips_elf_record_got_page_entry (info, abfd, r_symndx,
7750 addend))
7751 return FALSE;
7753 /* Fall through. */
7755 case R_MIPS_GOT_DISP:
7756 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
7757 FALSE, 0))
7758 return FALSE;
7759 break;
7761 case R_MIPS_TLS_GOTTPREL:
7762 if (info->shared)
7763 info->flags |= DF_STATIC_TLS;
7764 /* Fall through */
7766 case R_MIPS_TLS_LDM:
7767 if (r_type == R_MIPS_TLS_LDM)
7769 r_symndx = STN_UNDEF;
7770 h = NULL;
7772 /* Fall through */
7774 case R_MIPS_TLS_GD:
7775 /* This symbol requires a global offset table entry, or two
7776 for TLS GD relocations. */
7778 unsigned char flag = (r_type == R_MIPS_TLS_GD
7779 ? GOT_TLS_GD
7780 : r_type == R_MIPS_TLS_LDM
7781 ? GOT_TLS_LDM
7782 : GOT_TLS_IE);
7783 if (h != NULL)
7785 struct mips_elf_link_hash_entry *hmips =
7786 (struct mips_elf_link_hash_entry *) h;
7787 hmips->tls_type |= flag;
7789 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
7790 FALSE, flag))
7791 return FALSE;
7793 else
7795 BFD_ASSERT (flag == GOT_TLS_LDM || r_symndx != STN_UNDEF);
7797 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
7798 rel->r_addend,
7799 info, flag))
7800 return FALSE;
7803 break;
7805 case R_MIPS_32:
7806 case R_MIPS_REL32:
7807 case R_MIPS_64:
7808 /* In VxWorks executables, references to external symbols
7809 are handled using copy relocs or PLT stubs, so there's
7810 no need to add a .rela.dyn entry for this relocation. */
7811 if (can_make_dynamic_p)
7813 if (sreloc == NULL)
7815 sreloc = mips_elf_rel_dyn_section (info, TRUE);
7816 if (sreloc == NULL)
7817 return FALSE;
7819 if (info->shared && h == NULL)
7821 /* When creating a shared object, we must copy these
7822 reloc types into the output file as R_MIPS_REL32
7823 relocs. Make room for this reloc in .rel(a).dyn. */
7824 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
7825 if (MIPS_ELF_READONLY_SECTION (sec))
7826 /* We tell the dynamic linker that there are
7827 relocations against the text segment. */
7828 info->flags |= DF_TEXTREL;
7830 else
7832 struct mips_elf_link_hash_entry *hmips;
7834 /* For a shared object, we must copy this relocation
7835 unless the symbol turns out to be undefined and
7836 weak with non-default visibility, in which case
7837 it will be left as zero.
7839 We could elide R_MIPS_REL32 for locally binding symbols
7840 in shared libraries, but do not yet do so.
7842 For an executable, we only need to copy this
7843 reloc if the symbol is defined in a dynamic
7844 object. */
7845 hmips = (struct mips_elf_link_hash_entry *) h;
7846 ++hmips->possibly_dynamic_relocs;
7847 if (MIPS_ELF_READONLY_SECTION (sec))
7848 /* We need it to tell the dynamic linker if there
7849 are relocations against the text segment. */
7850 hmips->readonly_reloc = TRUE;
7854 if (SGI_COMPAT (abfd))
7855 mips_elf_hash_table (info)->compact_rel_size +=
7856 sizeof (Elf32_External_crinfo);
7857 break;
7859 case R_MIPS_26:
7860 case R_MIPS_GPREL16:
7861 case R_MIPS_LITERAL:
7862 case R_MIPS_GPREL32:
7863 if (SGI_COMPAT (abfd))
7864 mips_elf_hash_table (info)->compact_rel_size +=
7865 sizeof (Elf32_External_crinfo);
7866 break;
7868 /* This relocation describes the C++ object vtable hierarchy.
7869 Reconstruct it for later use during GC. */
7870 case R_MIPS_GNU_VTINHERIT:
7871 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7872 return FALSE;
7873 break;
7875 /* This relocation describes which C++ vtable entries are actually
7876 used. Record for later use during GC. */
7877 case R_MIPS_GNU_VTENTRY:
7878 BFD_ASSERT (h != NULL);
7879 if (h != NULL
7880 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7881 return FALSE;
7882 break;
7884 default:
7885 break;
7888 /* We must not create a stub for a symbol that has relocations
7889 related to taking the function's address. This doesn't apply to
7890 VxWorks, where CALL relocs refer to a .got.plt entry instead of
7891 a normal .got entry. */
7892 if (!htab->is_vxworks && h != NULL)
7893 switch (r_type)
7895 default:
7896 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
7897 break;
7898 case R_MIPS16_CALL16:
7899 case R_MIPS_CALL16:
7900 case R_MIPS_CALL_HI16:
7901 case R_MIPS_CALL_LO16:
7902 case R_MIPS_JALR:
7903 break;
7906 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
7907 if there is one. We only need to handle global symbols here;
7908 we decide whether to keep or delete stubs for local symbols
7909 when processing the stub's relocations. */
7910 if (h != NULL
7911 && !mips16_call_reloc_p (r_type)
7912 && !section_allows_mips16_refs_p (sec))
7914 struct mips_elf_link_hash_entry *mh;
7916 mh = (struct mips_elf_link_hash_entry *) h;
7917 mh->need_fn_stub = TRUE;
7920 /* Refuse some position-dependent relocations when creating a
7921 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
7922 not PIC, but we can create dynamic relocations and the result
7923 will be fine. Also do not refuse R_MIPS_LO16, which can be
7924 combined with R_MIPS_GOT16. */
7925 if (info->shared)
7927 switch (r_type)
7929 case R_MIPS16_HI16:
7930 case R_MIPS_HI16:
7931 case R_MIPS_HIGHER:
7932 case R_MIPS_HIGHEST:
7933 /* Don't refuse a high part relocation if it's against
7934 no symbol (e.g. part of a compound relocation). */
7935 if (r_symndx == STN_UNDEF)
7936 break;
7938 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
7939 and has a special meaning. */
7940 if (!NEWABI_P (abfd) && h != NULL
7941 && strcmp (h->root.root.string, "_gp_disp") == 0)
7942 break;
7944 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
7945 if (is_gott_symbol (info, h))
7946 break;
7948 /* FALLTHROUGH */
7950 case R_MIPS16_26:
7951 case R_MIPS_26:
7952 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
7953 (*_bfd_error_handler)
7954 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
7955 abfd, howto->name,
7956 (h) ? h->root.root.string : "a local symbol");
7957 bfd_set_error (bfd_error_bad_value);
7958 return FALSE;
7959 default:
7960 break;
7965 return TRUE;
7968 bfd_boolean
7969 _bfd_mips_relax_section (bfd *abfd, asection *sec,
7970 struct bfd_link_info *link_info,
7971 bfd_boolean *again)
7973 Elf_Internal_Rela *internal_relocs;
7974 Elf_Internal_Rela *irel, *irelend;
7975 Elf_Internal_Shdr *symtab_hdr;
7976 bfd_byte *contents = NULL;
7977 size_t extsymoff;
7978 bfd_boolean changed_contents = FALSE;
7979 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
7980 Elf_Internal_Sym *isymbuf = NULL;
7982 /* We are not currently changing any sizes, so only one pass. */
7983 *again = FALSE;
7985 if (link_info->relocatable)
7986 return TRUE;
7988 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7989 link_info->keep_memory);
7990 if (internal_relocs == NULL)
7991 return TRUE;
7993 irelend = internal_relocs + sec->reloc_count
7994 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
7995 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7996 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7998 for (irel = internal_relocs; irel < irelend; irel++)
8000 bfd_vma symval;
8001 bfd_signed_vma sym_offset;
8002 unsigned int r_type;
8003 unsigned long r_symndx;
8004 asection *sym_sec;
8005 unsigned long instruction;
8007 /* Turn jalr into bgezal, and jr into beq, if they're marked
8008 with a JALR relocation, that indicate where they jump to.
8009 This saves some pipeline bubbles. */
8010 r_type = ELF_R_TYPE (abfd, irel->r_info);
8011 if (r_type != R_MIPS_JALR)
8012 continue;
8014 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8015 /* Compute the address of the jump target. */
8016 if (r_symndx >= extsymoff)
8018 struct mips_elf_link_hash_entry *h
8019 = ((struct mips_elf_link_hash_entry *)
8020 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8022 while (h->root.root.type == bfd_link_hash_indirect
8023 || h->root.root.type == bfd_link_hash_warning)
8024 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8026 /* If a symbol is undefined, or if it may be overridden,
8027 skip it. */
8028 if (! ((h->root.root.type == bfd_link_hash_defined
8029 || h->root.root.type == bfd_link_hash_defweak)
8030 && h->root.root.u.def.section)
8031 || (link_info->shared && ! link_info->symbolic
8032 && !h->root.forced_local))
8033 continue;
8035 sym_sec = h->root.root.u.def.section;
8036 if (sym_sec->output_section)
8037 symval = (h->root.root.u.def.value
8038 + sym_sec->output_section->vma
8039 + sym_sec->output_offset);
8040 else
8041 symval = h->root.root.u.def.value;
8043 else
8045 Elf_Internal_Sym *isym;
8047 /* Read this BFD's symbols if we haven't done so already. */
8048 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8050 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8051 if (isymbuf == NULL)
8052 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8053 symtab_hdr->sh_info, 0,
8054 NULL, NULL, NULL);
8055 if (isymbuf == NULL)
8056 goto relax_return;
8059 isym = isymbuf + r_symndx;
8060 if (isym->st_shndx == SHN_UNDEF)
8061 continue;
8062 else if (isym->st_shndx == SHN_ABS)
8063 sym_sec = bfd_abs_section_ptr;
8064 else if (isym->st_shndx == SHN_COMMON)
8065 sym_sec = bfd_com_section_ptr;
8066 else
8067 sym_sec
8068 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8069 symval = isym->st_value
8070 + sym_sec->output_section->vma
8071 + sym_sec->output_offset;
8074 /* Compute branch offset, from delay slot of the jump to the
8075 branch target. */
8076 sym_offset = (symval + irel->r_addend)
8077 - (sec_start + irel->r_offset + 4);
8079 /* Branch offset must be properly aligned. */
8080 if ((sym_offset & 3) != 0)
8081 continue;
8083 sym_offset >>= 2;
8085 /* Check that it's in range. */
8086 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8087 continue;
8089 /* Get the section contents if we haven't done so already. */
8090 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8091 goto relax_return;
8093 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8095 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8096 if ((instruction & 0xfc1fffff) == 0x0000f809)
8097 instruction = 0x04110000;
8098 /* If it was jr <reg>, turn it into b <target>. */
8099 else if ((instruction & 0xfc1fffff) == 0x00000008)
8100 instruction = 0x10000000;
8101 else
8102 continue;
8104 instruction |= (sym_offset & 0xffff);
8105 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8106 changed_contents = TRUE;
8109 if (contents != NULL
8110 && elf_section_data (sec)->this_hdr.contents != contents)
8112 if (!changed_contents && !link_info->keep_memory)
8113 free (contents);
8114 else
8116 /* Cache the section contents for elf_link_input_bfd. */
8117 elf_section_data (sec)->this_hdr.contents = contents;
8120 return TRUE;
8122 relax_return:
8123 if (contents != NULL
8124 && elf_section_data (sec)->this_hdr.contents != contents)
8125 free (contents);
8126 return FALSE;
8129 /* Allocate space for global sym dynamic relocs. */
8131 static bfd_boolean
8132 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8134 struct bfd_link_info *info = inf;
8135 bfd *dynobj;
8136 struct mips_elf_link_hash_entry *hmips;
8137 struct mips_elf_link_hash_table *htab;
8139 htab = mips_elf_hash_table (info);
8140 BFD_ASSERT (htab != NULL);
8142 dynobj = elf_hash_table (info)->dynobj;
8143 hmips = (struct mips_elf_link_hash_entry *) h;
8145 /* VxWorks executables are handled elsewhere; we only need to
8146 allocate relocations in shared objects. */
8147 if (htab->is_vxworks && !info->shared)
8148 return TRUE;
8150 /* Ignore indirect and warning symbols. All relocations against
8151 such symbols will be redirected to the target symbol. */
8152 if (h->root.type == bfd_link_hash_indirect
8153 || h->root.type == bfd_link_hash_warning)
8154 return TRUE;
8156 /* If this symbol is defined in a dynamic object, or we are creating
8157 a shared library, we will need to copy any R_MIPS_32 or
8158 R_MIPS_REL32 relocs against it into the output file. */
8159 if (! info->relocatable
8160 && hmips->possibly_dynamic_relocs != 0
8161 && (h->root.type == bfd_link_hash_defweak
8162 || !h->def_regular
8163 || info->shared))
8165 bfd_boolean do_copy = TRUE;
8167 if (h->root.type == bfd_link_hash_undefweak)
8169 /* Do not copy relocations for undefined weak symbols with
8170 non-default visibility. */
8171 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8172 do_copy = FALSE;
8174 /* Make sure undefined weak symbols are output as a dynamic
8175 symbol in PIEs. */
8176 else if (h->dynindx == -1 && !h->forced_local)
8178 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8179 return FALSE;
8183 if (do_copy)
8185 /* Even though we don't directly need a GOT entry for this symbol,
8186 the SVR4 psABI requires it to have a dynamic symbol table
8187 index greater that DT_MIPS_GOTSYM if there are dynamic
8188 relocations against it.
8190 VxWorks does not enforce the same mapping between the GOT
8191 and the symbol table, so the same requirement does not
8192 apply there. */
8193 if (!htab->is_vxworks)
8195 if (hmips->global_got_area > GGA_RELOC_ONLY)
8196 hmips->global_got_area = GGA_RELOC_ONLY;
8197 hmips->got_only_for_calls = FALSE;
8200 mips_elf_allocate_dynamic_relocations
8201 (dynobj, info, hmips->possibly_dynamic_relocs);
8202 if (hmips->readonly_reloc)
8203 /* We tell the dynamic linker that there are relocations
8204 against the text segment. */
8205 info->flags |= DF_TEXTREL;
8209 return TRUE;
8212 /* Adjust a symbol defined by a dynamic object and referenced by a
8213 regular object. The current definition is in some section of the
8214 dynamic object, but we're not including those sections. We have to
8215 change the definition to something the rest of the link can
8216 understand. */
8218 bfd_boolean
8219 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8220 struct elf_link_hash_entry *h)
8222 bfd *dynobj;
8223 struct mips_elf_link_hash_entry *hmips;
8224 struct mips_elf_link_hash_table *htab;
8226 htab = mips_elf_hash_table (info);
8227 BFD_ASSERT (htab != NULL);
8229 dynobj = elf_hash_table (info)->dynobj;
8230 hmips = (struct mips_elf_link_hash_entry *) h;
8232 /* Make sure we know what is going on here. */
8233 BFD_ASSERT (dynobj != NULL
8234 && (h->needs_plt
8235 || h->u.weakdef != NULL
8236 || (h->def_dynamic
8237 && h->ref_regular
8238 && !h->def_regular)));
8240 hmips = (struct mips_elf_link_hash_entry *) h;
8242 /* If there are call relocations against an externally-defined symbol,
8243 see whether we can create a MIPS lazy-binding stub for it. We can
8244 only do this if all references to the function are through call
8245 relocations, and in that case, the traditional lazy-binding stubs
8246 are much more efficient than PLT entries.
8248 Traditional stubs are only available on SVR4 psABI-based systems;
8249 VxWorks always uses PLTs instead. */
8250 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8252 if (! elf_hash_table (info)->dynamic_sections_created)
8253 return TRUE;
8255 /* If this symbol is not defined in a regular file, then set
8256 the symbol to the stub location. This is required to make
8257 function pointers compare as equal between the normal
8258 executable and the shared library. */
8259 if (!h->def_regular)
8261 hmips->needs_lazy_stub = TRUE;
8262 htab->lazy_stub_count++;
8263 return TRUE;
8266 /* As above, VxWorks requires PLT entries for externally-defined
8267 functions that are only accessed through call relocations.
8269 Both VxWorks and non-VxWorks targets also need PLT entries if there
8270 are static-only relocations against an externally-defined function.
8271 This can technically occur for shared libraries if there are
8272 branches to the symbol, although it is unlikely that this will be
8273 used in practice due to the short ranges involved. It can occur
8274 for any relative or absolute relocation in executables; in that
8275 case, the PLT entry becomes the function's canonical address. */
8276 else if (((h->needs_plt && !hmips->no_fn_stub)
8277 || (h->type == STT_FUNC && hmips->has_static_relocs))
8278 && htab->use_plts_and_copy_relocs
8279 && !SYMBOL_CALLS_LOCAL (info, h)
8280 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8281 && h->root.type == bfd_link_hash_undefweak))
8283 /* If this is the first symbol to need a PLT entry, allocate room
8284 for the header. */
8285 if (htab->splt->size == 0)
8287 BFD_ASSERT (htab->sgotplt->size == 0);
8289 /* If we're using the PLT additions to the psABI, each PLT
8290 entry is 16 bytes and the PLT0 entry is 32 bytes.
8291 Encourage better cache usage by aligning. We do this
8292 lazily to avoid pessimizing traditional objects. */
8293 if (!htab->is_vxworks
8294 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8295 return FALSE;
8297 /* Make sure that .got.plt is word-aligned. We do this lazily
8298 for the same reason as above. */
8299 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8300 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8301 return FALSE;
8303 htab->splt->size += htab->plt_header_size;
8305 /* On non-VxWorks targets, the first two entries in .got.plt
8306 are reserved. */
8307 if (!htab->is_vxworks)
8308 htab->sgotplt->size += 2 * MIPS_ELF_GOT_SIZE (dynobj);
8310 /* On VxWorks, also allocate room for the header's
8311 .rela.plt.unloaded entries. */
8312 if (htab->is_vxworks && !info->shared)
8313 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8316 /* Assign the next .plt entry to this symbol. */
8317 h->plt.offset = htab->splt->size;
8318 htab->splt->size += htab->plt_entry_size;
8320 /* If the output file has no definition of the symbol, set the
8321 symbol's value to the address of the stub. */
8322 if (!info->shared && !h->def_regular)
8324 h->root.u.def.section = htab->splt;
8325 h->root.u.def.value = h->plt.offset;
8326 /* For VxWorks, point at the PLT load stub rather than the
8327 lazy resolution stub; this stub will become the canonical
8328 function address. */
8329 if (htab->is_vxworks)
8330 h->root.u.def.value += 8;
8333 /* Make room for the .got.plt entry and the R_MIPS_JUMP_SLOT
8334 relocation. */
8335 htab->sgotplt->size += MIPS_ELF_GOT_SIZE (dynobj);
8336 htab->srelplt->size += (htab->is_vxworks
8337 ? MIPS_ELF_RELA_SIZE (dynobj)
8338 : MIPS_ELF_REL_SIZE (dynobj));
8340 /* Make room for the .rela.plt.unloaded relocations. */
8341 if (htab->is_vxworks && !info->shared)
8342 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8344 /* All relocations against this symbol that could have been made
8345 dynamic will now refer to the PLT entry instead. */
8346 hmips->possibly_dynamic_relocs = 0;
8348 return TRUE;
8351 /* If this is a weak symbol, and there is a real definition, the
8352 processor independent code will have arranged for us to see the
8353 real definition first, and we can just use the same value. */
8354 if (h->u.weakdef != NULL)
8356 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8357 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8358 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8359 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8360 return TRUE;
8363 /* Otherwise, there is nothing further to do for symbols defined
8364 in regular objects. */
8365 if (h->def_regular)
8366 return TRUE;
8368 /* There's also nothing more to do if we'll convert all relocations
8369 against this symbol into dynamic relocations. */
8370 if (!hmips->has_static_relocs)
8371 return TRUE;
8373 /* We're now relying on copy relocations. Complain if we have
8374 some that we can't convert. */
8375 if (!htab->use_plts_and_copy_relocs || info->shared)
8377 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8378 "dynamic symbol %s"),
8379 h->root.root.string);
8380 bfd_set_error (bfd_error_bad_value);
8381 return FALSE;
8384 /* We must allocate the symbol in our .dynbss section, which will
8385 become part of the .bss section of the executable. There will be
8386 an entry for this symbol in the .dynsym section. The dynamic
8387 object will contain position independent code, so all references
8388 from the dynamic object to this symbol will go through the global
8389 offset table. The dynamic linker will use the .dynsym entry to
8390 determine the address it must put in the global offset table, so
8391 both the dynamic object and the regular object will refer to the
8392 same memory location for the variable. */
8394 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8396 if (htab->is_vxworks)
8397 htab->srelbss->size += sizeof (Elf32_External_Rela);
8398 else
8399 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8400 h->needs_copy = 1;
8403 /* All relocations against this symbol that could have been made
8404 dynamic will now refer to the local copy instead. */
8405 hmips->possibly_dynamic_relocs = 0;
8407 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
8410 /* This function is called after all the input files have been read,
8411 and the input sections have been assigned to output sections. We
8412 check for any mips16 stub sections that we can discard. */
8414 bfd_boolean
8415 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
8416 struct bfd_link_info *info)
8418 asection *ri;
8419 struct mips_elf_link_hash_table *htab;
8420 struct mips_htab_traverse_info hti;
8422 htab = mips_elf_hash_table (info);
8423 BFD_ASSERT (htab != NULL);
8425 /* The .reginfo section has a fixed size. */
8426 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
8427 if (ri != NULL)
8428 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
8430 hti.info = info;
8431 hti.output_bfd = output_bfd;
8432 hti.error = FALSE;
8433 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
8434 mips_elf_check_symbols, &hti);
8435 if (hti.error)
8436 return FALSE;
8438 return TRUE;
8441 /* If the link uses a GOT, lay it out and work out its size. */
8443 static bfd_boolean
8444 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
8446 bfd *dynobj;
8447 asection *s;
8448 struct mips_got_info *g;
8449 bfd_size_type loadable_size = 0;
8450 bfd_size_type page_gotno;
8451 bfd *sub;
8452 struct mips_elf_count_tls_arg count_tls_arg;
8453 struct mips_elf_link_hash_table *htab;
8455 htab = mips_elf_hash_table (info);
8456 BFD_ASSERT (htab != NULL);
8458 s = htab->sgot;
8459 if (s == NULL)
8460 return TRUE;
8462 dynobj = elf_hash_table (info)->dynobj;
8463 g = htab->got_info;
8465 /* Allocate room for the reserved entries. VxWorks always reserves
8466 3 entries; other objects only reserve 2 entries. */
8467 BFD_ASSERT (g->assigned_gotno == 0);
8468 if (htab->is_vxworks)
8469 htab->reserved_gotno = 3;
8470 else
8471 htab->reserved_gotno = 2;
8472 g->local_gotno += htab->reserved_gotno;
8473 g->assigned_gotno = htab->reserved_gotno;
8475 /* Replace entries for indirect and warning symbols with entries for
8476 the target symbol. */
8477 if (!mips_elf_resolve_final_got_entries (g))
8478 return FALSE;
8480 /* Count the number of GOT symbols. */
8481 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
8483 /* Calculate the total loadable size of the output. That
8484 will give us the maximum number of GOT_PAGE entries
8485 required. */
8486 for (sub = info->input_bfds; sub; sub = sub->link_next)
8488 asection *subsection;
8490 for (subsection = sub->sections;
8491 subsection;
8492 subsection = subsection->next)
8494 if ((subsection->flags & SEC_ALLOC) == 0)
8495 continue;
8496 loadable_size += ((subsection->size + 0xf)
8497 &~ (bfd_size_type) 0xf);
8501 if (htab->is_vxworks)
8502 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
8503 relocations against local symbols evaluate to "G", and the EABI does
8504 not include R_MIPS_GOT_PAGE. */
8505 page_gotno = 0;
8506 else
8507 /* Assume there are two loadable segments consisting of contiguous
8508 sections. Is 5 enough? */
8509 page_gotno = (loadable_size >> 16) + 5;
8511 /* Choose the smaller of the two estimates; both are intended to be
8512 conservative. */
8513 if (page_gotno > g->page_gotno)
8514 page_gotno = g->page_gotno;
8516 g->local_gotno += page_gotno;
8517 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8518 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8520 /* We need to calculate tls_gotno for global symbols at this point
8521 instead of building it up earlier, to avoid doublecounting
8522 entries for one global symbol from multiple input files. */
8523 count_tls_arg.info = info;
8524 count_tls_arg.needed = 0;
8525 elf_link_hash_traverse (elf_hash_table (info),
8526 mips_elf_count_global_tls_entries,
8527 &count_tls_arg);
8528 g->tls_gotno += count_tls_arg.needed;
8529 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
8531 /* VxWorks does not support multiple GOTs. It initializes $gp to
8532 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
8533 dynamic loader. */
8534 if (htab->is_vxworks)
8536 /* VxWorks executables do not need a GOT. */
8537 if (info->shared)
8539 /* Each VxWorks GOT entry needs an explicit relocation. */
8540 unsigned int count;
8542 count = g->global_gotno + g->local_gotno - htab->reserved_gotno;
8543 if (count)
8544 mips_elf_allocate_dynamic_relocations (dynobj, info, count);
8547 else if (s->size > MIPS_ELF_GOT_MAX_SIZE (info))
8549 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
8550 return FALSE;
8552 else
8554 struct mips_elf_count_tls_arg arg;
8556 /* Set up TLS entries. */
8557 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
8558 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
8560 /* Allocate room for the TLS relocations. */
8561 arg.info = info;
8562 arg.needed = 0;
8563 htab_traverse (g->got_entries, mips_elf_count_local_tls_relocs, &arg);
8564 elf_link_hash_traverse (elf_hash_table (info),
8565 mips_elf_count_global_tls_relocs,
8566 &arg);
8567 if (arg.needed)
8568 mips_elf_allocate_dynamic_relocations (dynobj, info, arg.needed);
8571 return TRUE;
8574 /* Estimate the size of the .MIPS.stubs section. */
8576 static void
8577 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
8579 struct mips_elf_link_hash_table *htab;
8580 bfd_size_type dynsymcount;
8582 htab = mips_elf_hash_table (info);
8583 BFD_ASSERT (htab != NULL);
8585 if (htab->lazy_stub_count == 0)
8586 return;
8588 /* IRIX rld assumes that a function stub isn't at the end of the .text
8589 section, so add a dummy entry to the end. */
8590 htab->lazy_stub_count++;
8592 /* Get a worst-case estimate of the number of dynamic symbols needed.
8593 At this point, dynsymcount does not account for section symbols
8594 and count_section_dynsyms may overestimate the number that will
8595 be needed. */
8596 dynsymcount = (elf_hash_table (info)->dynsymcount
8597 + count_section_dynsyms (output_bfd, info));
8599 /* Determine the size of one stub entry. */
8600 htab->function_stub_size = (dynsymcount > 0x10000
8601 ? MIPS_FUNCTION_STUB_BIG_SIZE
8602 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
8604 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
8607 /* A mips_elf_link_hash_traverse callback for which DATA points to the
8608 MIPS hash table. If H needs a traditional MIPS lazy-binding stub,
8609 allocate an entry in the stubs section. */
8611 static bfd_boolean
8612 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void **data)
8614 struct mips_elf_link_hash_table *htab;
8616 htab = (struct mips_elf_link_hash_table *) data;
8617 if (h->needs_lazy_stub)
8619 h->root.root.u.def.section = htab->sstubs;
8620 h->root.root.u.def.value = htab->sstubs->size;
8621 h->root.plt.offset = htab->sstubs->size;
8622 htab->sstubs->size += htab->function_stub_size;
8624 return TRUE;
8627 /* Allocate offsets in the stubs section to each symbol that needs one.
8628 Set the final size of the .MIPS.stub section. */
8630 static void
8631 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
8633 struct mips_elf_link_hash_table *htab;
8635 htab = mips_elf_hash_table (info);
8636 BFD_ASSERT (htab != NULL);
8638 if (htab->lazy_stub_count == 0)
8639 return;
8641 htab->sstubs->size = 0;
8642 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, htab);
8643 htab->sstubs->size += htab->function_stub_size;
8644 BFD_ASSERT (htab->sstubs->size
8645 == htab->lazy_stub_count * htab->function_stub_size);
8648 /* Set the sizes of the dynamic sections. */
8650 bfd_boolean
8651 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
8652 struct bfd_link_info *info)
8654 bfd *dynobj;
8655 asection *s, *sreldyn;
8656 bfd_boolean reltext;
8657 struct mips_elf_link_hash_table *htab;
8659 htab = mips_elf_hash_table (info);
8660 BFD_ASSERT (htab != NULL);
8661 dynobj = elf_hash_table (info)->dynobj;
8662 BFD_ASSERT (dynobj != NULL);
8664 if (elf_hash_table (info)->dynamic_sections_created)
8666 /* Set the contents of the .interp section to the interpreter. */
8667 if (info->executable)
8669 s = bfd_get_section_by_name (dynobj, ".interp");
8670 BFD_ASSERT (s != NULL);
8671 s->size
8672 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
8673 s->contents
8674 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
8677 /* Create a symbol for the PLT, if we know that we are using it. */
8678 if (htab->splt && htab->splt->size > 0 && htab->root.hplt == NULL)
8680 struct elf_link_hash_entry *h;
8682 BFD_ASSERT (htab->use_plts_and_copy_relocs);
8684 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
8685 "_PROCEDURE_LINKAGE_TABLE_");
8686 htab->root.hplt = h;
8687 if (h == NULL)
8688 return FALSE;
8689 h->type = STT_FUNC;
8693 /* Allocate space for global sym dynamic relocs. */
8694 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (PTR) info);
8696 mips_elf_estimate_stub_size (output_bfd, info);
8698 if (!mips_elf_lay_out_got (output_bfd, info))
8699 return FALSE;
8701 mips_elf_lay_out_lazy_stubs (info);
8703 /* The check_relocs and adjust_dynamic_symbol entry points have
8704 determined the sizes of the various dynamic sections. Allocate
8705 memory for them. */
8706 reltext = FALSE;
8707 for (s = dynobj->sections; s != NULL; s = s->next)
8709 const char *name;
8711 /* It's OK to base decisions on the section name, because none
8712 of the dynobj section names depend upon the input files. */
8713 name = bfd_get_section_name (dynobj, s);
8715 if ((s->flags & SEC_LINKER_CREATED) == 0)
8716 continue;
8718 if (CONST_STRNEQ (name, ".rel"))
8720 if (s->size != 0)
8722 const char *outname;
8723 asection *target;
8725 /* If this relocation section applies to a read only
8726 section, then we probably need a DT_TEXTREL entry.
8727 If the relocation section is .rel(a).dyn, we always
8728 assert a DT_TEXTREL entry rather than testing whether
8729 there exists a relocation to a read only section or
8730 not. */
8731 outname = bfd_get_section_name (output_bfd,
8732 s->output_section);
8733 target = bfd_get_section_by_name (output_bfd, outname + 4);
8734 if ((target != NULL
8735 && (target->flags & SEC_READONLY) != 0
8736 && (target->flags & SEC_ALLOC) != 0)
8737 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
8738 reltext = TRUE;
8740 /* We use the reloc_count field as a counter if we need
8741 to copy relocs into the output file. */
8742 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
8743 s->reloc_count = 0;
8745 /* If combreloc is enabled, elf_link_sort_relocs() will
8746 sort relocations, but in a different way than we do,
8747 and before we're done creating relocations. Also, it
8748 will move them around between input sections'
8749 relocation's contents, so our sorting would be
8750 broken, so don't let it run. */
8751 info->combreloc = 0;
8754 else if (! info->shared
8755 && ! mips_elf_hash_table (info)->use_rld_obj_head
8756 && CONST_STRNEQ (name, ".rld_map"))
8758 /* We add a room for __rld_map. It will be filled in by the
8759 rtld to contain a pointer to the _r_debug structure. */
8760 s->size += 4;
8762 else if (SGI_COMPAT (output_bfd)
8763 && CONST_STRNEQ (name, ".compact_rel"))
8764 s->size += mips_elf_hash_table (info)->compact_rel_size;
8765 else if (s == htab->splt)
8767 /* If the last PLT entry has a branch delay slot, allocate
8768 room for an extra nop to fill the delay slot. This is
8769 for CPUs without load interlocking. */
8770 if (! LOAD_INTERLOCKS_P (output_bfd)
8771 && ! htab->is_vxworks && s->size > 0)
8772 s->size += 4;
8774 else if (! CONST_STRNEQ (name, ".init")
8775 && s != htab->sgot
8776 && s != htab->sgotplt
8777 && s != htab->sstubs
8778 && s != htab->sdynbss)
8780 /* It's not one of our sections, so don't allocate space. */
8781 continue;
8784 if (s->size == 0)
8786 s->flags |= SEC_EXCLUDE;
8787 continue;
8790 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8791 continue;
8793 /* Allocate memory for the section contents. */
8794 s->contents = bfd_zalloc (dynobj, s->size);
8795 if (s->contents == NULL)
8797 bfd_set_error (bfd_error_no_memory);
8798 return FALSE;
8802 if (elf_hash_table (info)->dynamic_sections_created)
8804 /* Add some entries to the .dynamic section. We fill in the
8805 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
8806 must add the entries now so that we get the correct size for
8807 the .dynamic section. */
8809 /* SGI object has the equivalence of DT_DEBUG in the
8810 DT_MIPS_RLD_MAP entry. This must come first because glibc
8811 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and GDB only
8812 looks at the first one it sees. */
8813 if (!info->shared
8814 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
8815 return FALSE;
8817 /* The DT_DEBUG entry may be filled in by the dynamic linker and
8818 used by the debugger. */
8819 if (info->executable
8820 && !SGI_COMPAT (output_bfd)
8821 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
8822 return FALSE;
8824 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
8825 info->flags |= DF_TEXTREL;
8827 if ((info->flags & DF_TEXTREL) != 0)
8829 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
8830 return FALSE;
8832 /* Clear the DF_TEXTREL flag. It will be set again if we
8833 write out an actual text relocation; we may not, because
8834 at this point we do not know whether e.g. any .eh_frame
8835 absolute relocations have been converted to PC-relative. */
8836 info->flags &= ~DF_TEXTREL;
8839 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
8840 return FALSE;
8842 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
8843 if (htab->is_vxworks)
8845 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
8846 use any of the DT_MIPS_* tags. */
8847 if (sreldyn && sreldyn->size > 0)
8849 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
8850 return FALSE;
8852 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
8853 return FALSE;
8855 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
8856 return FALSE;
8859 else
8861 if (sreldyn && sreldyn->size > 0)
8863 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
8864 return FALSE;
8866 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
8867 return FALSE;
8869 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
8870 return FALSE;
8873 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
8874 return FALSE;
8876 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
8877 return FALSE;
8879 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
8880 return FALSE;
8882 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
8883 return FALSE;
8885 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
8886 return FALSE;
8888 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
8889 return FALSE;
8891 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
8892 return FALSE;
8894 if (IRIX_COMPAT (dynobj) == ict_irix5
8895 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
8896 return FALSE;
8898 if (IRIX_COMPAT (dynobj) == ict_irix6
8899 && (bfd_get_section_by_name
8900 (dynobj, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
8901 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
8902 return FALSE;
8904 if (htab->splt->size > 0)
8906 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
8907 return FALSE;
8909 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
8910 return FALSE;
8912 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
8913 return FALSE;
8915 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
8916 return FALSE;
8918 if (htab->is_vxworks
8919 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
8920 return FALSE;
8923 return TRUE;
8926 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
8927 Adjust its R_ADDEND field so that it is correct for the output file.
8928 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
8929 and sections respectively; both use symbol indexes. */
8931 static void
8932 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
8933 bfd *input_bfd, Elf_Internal_Sym *local_syms,
8934 asection **local_sections, Elf_Internal_Rela *rel)
8936 unsigned int r_type, r_symndx;
8937 Elf_Internal_Sym *sym;
8938 asection *sec;
8940 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
8942 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8943 if (r_type == R_MIPS16_GPREL
8944 || r_type == R_MIPS_GPREL16
8945 || r_type == R_MIPS_GPREL32
8946 || r_type == R_MIPS_LITERAL)
8948 rel->r_addend += _bfd_get_gp_value (input_bfd);
8949 rel->r_addend -= _bfd_get_gp_value (output_bfd);
8952 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
8953 sym = local_syms + r_symndx;
8955 /* Adjust REL's addend to account for section merging. */
8956 if (!info->relocatable)
8958 sec = local_sections[r_symndx];
8959 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
8962 /* This would normally be done by the rela_normal code in elflink.c. */
8963 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8964 rel->r_addend += local_sections[r_symndx]->output_offset;
8968 /* Relocate a MIPS ELF section. */
8970 bfd_boolean
8971 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
8972 bfd *input_bfd, asection *input_section,
8973 bfd_byte *contents, Elf_Internal_Rela *relocs,
8974 Elf_Internal_Sym *local_syms,
8975 asection **local_sections)
8977 Elf_Internal_Rela *rel;
8978 const Elf_Internal_Rela *relend;
8979 bfd_vma addend = 0;
8980 bfd_boolean use_saved_addend_p = FALSE;
8981 const struct elf_backend_data *bed;
8983 bed = get_elf_backend_data (output_bfd);
8984 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
8985 for (rel = relocs; rel < relend; ++rel)
8987 const char *name;
8988 bfd_vma value = 0;
8989 reloc_howto_type *howto;
8990 bfd_boolean cross_mode_jump_p;
8991 /* TRUE if the relocation is a RELA relocation, rather than a
8992 REL relocation. */
8993 bfd_boolean rela_relocation_p = TRUE;
8994 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
8995 const char *msg;
8996 unsigned long r_symndx;
8997 asection *sec;
8998 Elf_Internal_Shdr *symtab_hdr;
8999 struct elf_link_hash_entry *h;
9000 bfd_boolean rel_reloc;
9002 rel_reloc = (NEWABI_P (input_bfd)
9003 && mips_elf_rel_relocation_p (input_bfd, input_section,
9004 relocs, rel));
9005 /* Find the relocation howto for this relocation. */
9006 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9008 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
9009 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9010 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9012 sec = local_sections[r_symndx];
9013 h = NULL;
9015 else
9017 unsigned long extsymoff;
9019 extsymoff = 0;
9020 if (!elf_bad_symtab (input_bfd))
9021 extsymoff = symtab_hdr->sh_info;
9022 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9023 while (h->root.type == bfd_link_hash_indirect
9024 || h->root.type == bfd_link_hash_warning)
9025 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9027 sec = NULL;
9028 if (h->root.type == bfd_link_hash_defined
9029 || h->root.type == bfd_link_hash_defweak)
9030 sec = h->root.u.def.section;
9033 if (sec != NULL && elf_discarded_section (sec))
9034 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9035 rel, relend, howto, contents);
9037 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9039 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9040 64-bit code, but make sure all their addresses are in the
9041 lowermost or uppermost 32-bit section of the 64-bit address
9042 space. Thus, when they use an R_MIPS_64 they mean what is
9043 usually meant by R_MIPS_32, with the exception that the
9044 stored value is sign-extended to 64 bits. */
9045 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9047 /* On big-endian systems, we need to lie about the position
9048 of the reloc. */
9049 if (bfd_big_endian (input_bfd))
9050 rel->r_offset += 4;
9053 if (!use_saved_addend_p)
9055 /* If these relocations were originally of the REL variety,
9056 we must pull the addend out of the field that will be
9057 relocated. Otherwise, we simply use the contents of the
9058 RELA relocation. */
9059 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9060 relocs, rel))
9062 rela_relocation_p = FALSE;
9063 addend = mips_elf_read_rel_addend (input_bfd, rel,
9064 howto, contents);
9065 if (hi16_reloc_p (r_type)
9066 || (got16_reloc_p (r_type)
9067 && mips_elf_local_relocation_p (input_bfd, rel,
9068 local_sections)))
9070 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9071 contents, &addend))
9073 if (h)
9074 name = h->root.root.string;
9075 else
9076 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9077 local_syms + r_symndx,
9078 sec);
9079 (*_bfd_error_handler)
9080 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9081 input_bfd, input_section, name, howto->name,
9082 rel->r_offset);
9085 else
9086 addend <<= howto->rightshift;
9088 else
9089 addend = rel->r_addend;
9090 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9091 local_syms, local_sections, rel);
9094 if (info->relocatable)
9096 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9097 && bfd_big_endian (input_bfd))
9098 rel->r_offset -= 4;
9100 if (!rela_relocation_p && rel->r_addend)
9102 addend += rel->r_addend;
9103 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9104 addend = mips_elf_high (addend);
9105 else if (r_type == R_MIPS_HIGHER)
9106 addend = mips_elf_higher (addend);
9107 else if (r_type == R_MIPS_HIGHEST)
9108 addend = mips_elf_highest (addend);
9109 else
9110 addend >>= howto->rightshift;
9112 /* We use the source mask, rather than the destination
9113 mask because the place to which we are writing will be
9114 source of the addend in the final link. */
9115 addend &= howto->src_mask;
9117 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9118 /* See the comment above about using R_MIPS_64 in the 32-bit
9119 ABI. Here, we need to update the addend. It would be
9120 possible to get away with just using the R_MIPS_32 reloc
9121 but for endianness. */
9123 bfd_vma sign_bits;
9124 bfd_vma low_bits;
9125 bfd_vma high_bits;
9127 if (addend & ((bfd_vma) 1 << 31))
9128 #ifdef BFD64
9129 sign_bits = ((bfd_vma) 1 << 32) - 1;
9130 #else
9131 sign_bits = -1;
9132 #endif
9133 else
9134 sign_bits = 0;
9136 /* If we don't know that we have a 64-bit type,
9137 do two separate stores. */
9138 if (bfd_big_endian (input_bfd))
9140 /* Store the sign-bits (which are most significant)
9141 first. */
9142 low_bits = sign_bits;
9143 high_bits = addend;
9145 else
9147 low_bits = addend;
9148 high_bits = sign_bits;
9150 bfd_put_32 (input_bfd, low_bits,
9151 contents + rel->r_offset);
9152 bfd_put_32 (input_bfd, high_bits,
9153 contents + rel->r_offset + 4);
9154 continue;
9157 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9158 input_bfd, input_section,
9159 contents, FALSE))
9160 return FALSE;
9163 /* Go on to the next relocation. */
9164 continue;
9167 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9168 relocations for the same offset. In that case we are
9169 supposed to treat the output of each relocation as the addend
9170 for the next. */
9171 if (rel + 1 < relend
9172 && rel->r_offset == rel[1].r_offset
9173 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9174 use_saved_addend_p = TRUE;
9175 else
9176 use_saved_addend_p = FALSE;
9178 /* Figure out what value we are supposed to relocate. */
9179 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9180 input_section, info, rel,
9181 addend, howto, local_syms,
9182 local_sections, &value,
9183 &name, &cross_mode_jump_p,
9184 use_saved_addend_p))
9186 case bfd_reloc_continue:
9187 /* There's nothing to do. */
9188 continue;
9190 case bfd_reloc_undefined:
9191 /* mips_elf_calculate_relocation already called the
9192 undefined_symbol callback. There's no real point in
9193 trying to perform the relocation at this point, so we
9194 just skip ahead to the next relocation. */
9195 continue;
9197 case bfd_reloc_notsupported:
9198 msg = _("internal error: unsupported relocation error");
9199 info->callbacks->warning
9200 (info, msg, name, input_bfd, input_section, rel->r_offset);
9201 return FALSE;
9203 case bfd_reloc_overflow:
9204 if (use_saved_addend_p)
9205 /* Ignore overflow until we reach the last relocation for
9206 a given location. */
9208 else
9210 struct mips_elf_link_hash_table *htab;
9212 htab = mips_elf_hash_table (info);
9213 BFD_ASSERT (htab != NULL);
9214 BFD_ASSERT (name != NULL);
9215 if (!htab->small_data_overflow_reported
9216 && (gprel16_reloc_p (howto->type)
9217 || howto->type == R_MIPS_LITERAL))
9219 msg = _("small-data section exceeds 64KB;"
9220 " lower small-data size limit (see option -G)");
9222 htab->small_data_overflow_reported = TRUE;
9223 (*info->callbacks->einfo) ("%P: %s\n", msg);
9225 if (! ((*info->callbacks->reloc_overflow)
9226 (info, NULL, name, howto->name, (bfd_vma) 0,
9227 input_bfd, input_section, rel->r_offset)))
9228 return FALSE;
9230 break;
9232 case bfd_reloc_ok:
9233 break;
9235 default:
9236 abort ();
9237 break;
9240 /* If we've got another relocation for the address, keep going
9241 until we reach the last one. */
9242 if (use_saved_addend_p)
9244 addend = value;
9245 continue;
9248 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9249 /* See the comment above about using R_MIPS_64 in the 32-bit
9250 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
9251 that calculated the right value. Now, however, we
9252 sign-extend the 32-bit result to 64-bits, and store it as a
9253 64-bit value. We are especially generous here in that we
9254 go to extreme lengths to support this usage on systems with
9255 only a 32-bit VMA. */
9257 bfd_vma sign_bits;
9258 bfd_vma low_bits;
9259 bfd_vma high_bits;
9261 if (value & ((bfd_vma) 1 << 31))
9262 #ifdef BFD64
9263 sign_bits = ((bfd_vma) 1 << 32) - 1;
9264 #else
9265 sign_bits = -1;
9266 #endif
9267 else
9268 sign_bits = 0;
9270 /* If we don't know that we have a 64-bit type,
9271 do two separate stores. */
9272 if (bfd_big_endian (input_bfd))
9274 /* Undo what we did above. */
9275 rel->r_offset -= 4;
9276 /* Store the sign-bits (which are most significant)
9277 first. */
9278 low_bits = sign_bits;
9279 high_bits = value;
9281 else
9283 low_bits = value;
9284 high_bits = sign_bits;
9286 bfd_put_32 (input_bfd, low_bits,
9287 contents + rel->r_offset);
9288 bfd_put_32 (input_bfd, high_bits,
9289 contents + rel->r_offset + 4);
9290 continue;
9293 /* Actually perform the relocation. */
9294 if (! mips_elf_perform_relocation (info, howto, rel, value,
9295 input_bfd, input_section,
9296 contents, cross_mode_jump_p))
9297 return FALSE;
9300 return TRUE;
9303 /* A function that iterates over each entry in la25_stubs and fills
9304 in the code for each one. DATA points to a mips_htab_traverse_info. */
9306 static int
9307 mips_elf_create_la25_stub (void **slot, void *data)
9309 struct mips_htab_traverse_info *hti;
9310 struct mips_elf_link_hash_table *htab;
9311 struct mips_elf_la25_stub *stub;
9312 asection *s;
9313 bfd_byte *loc;
9314 bfd_vma offset, target, target_high, target_low;
9316 stub = (struct mips_elf_la25_stub *) *slot;
9317 hti = (struct mips_htab_traverse_info *) data;
9318 htab = mips_elf_hash_table (hti->info);
9319 BFD_ASSERT (htab != NULL);
9321 /* Create the section contents, if we haven't already. */
9322 s = stub->stub_section;
9323 loc = s->contents;
9324 if (loc == NULL)
9326 loc = bfd_malloc (s->size);
9327 if (loc == NULL)
9329 hti->error = TRUE;
9330 return FALSE;
9332 s->contents = loc;
9335 /* Work out where in the section this stub should go. */
9336 offset = stub->offset;
9338 /* Work out the target address. */
9339 target = (stub->h->root.root.u.def.section->output_section->vma
9340 + stub->h->root.root.u.def.section->output_offset
9341 + stub->h->root.root.u.def.value);
9342 target_high = ((target + 0x8000) >> 16) & 0xffff;
9343 target_low = (target & 0xffff);
9345 if (stub->stub_section != htab->strampoline)
9347 /* This is a simple LUI/ADIDU stub. Zero out the beginning
9348 of the section and write the two instructions at the end. */
9349 memset (loc, 0, offset);
9350 loc += offset;
9351 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9352 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
9354 else
9356 /* This is trampoline. */
9357 loc += offset;
9358 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
9359 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
9360 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
9361 bfd_put_32 (hti->output_bfd, 0, loc + 12);
9363 return TRUE;
9366 /* If NAME is one of the special IRIX6 symbols defined by the linker,
9367 adjust it appropriately now. */
9369 static void
9370 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
9371 const char *name, Elf_Internal_Sym *sym)
9373 /* The linker script takes care of providing names and values for
9374 these, but we must place them into the right sections. */
9375 static const char* const text_section_symbols[] = {
9376 "_ftext",
9377 "_etext",
9378 "__dso_displacement",
9379 "__elf_header",
9380 "__program_header_table",
9381 NULL
9384 static const char* const data_section_symbols[] = {
9385 "_fdata",
9386 "_edata",
9387 "_end",
9388 "_fbss",
9389 NULL
9392 const char* const *p;
9393 int i;
9395 for (i = 0; i < 2; ++i)
9396 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
9398 ++p)
9399 if (strcmp (*p, name) == 0)
9401 /* All of these symbols are given type STT_SECTION by the
9402 IRIX6 linker. */
9403 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9404 sym->st_other = STO_PROTECTED;
9406 /* The IRIX linker puts these symbols in special sections. */
9407 if (i == 0)
9408 sym->st_shndx = SHN_MIPS_TEXT;
9409 else
9410 sym->st_shndx = SHN_MIPS_DATA;
9412 break;
9416 /* Finish up dynamic symbol handling. We set the contents of various
9417 dynamic sections here. */
9419 bfd_boolean
9420 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
9421 struct bfd_link_info *info,
9422 struct elf_link_hash_entry *h,
9423 Elf_Internal_Sym *sym)
9425 bfd *dynobj;
9426 asection *sgot;
9427 struct mips_got_info *g, *gg;
9428 const char *name;
9429 int idx;
9430 struct mips_elf_link_hash_table *htab;
9431 struct mips_elf_link_hash_entry *hmips;
9433 htab = mips_elf_hash_table (info);
9434 BFD_ASSERT (htab != NULL);
9435 dynobj = elf_hash_table (info)->dynobj;
9436 hmips = (struct mips_elf_link_hash_entry *) h;
9438 BFD_ASSERT (!htab->is_vxworks);
9440 if (h->plt.offset != MINUS_ONE && hmips->no_fn_stub)
9442 /* We've decided to create a PLT entry for this symbol. */
9443 bfd_byte *loc;
9444 bfd_vma header_address, plt_index, got_address;
9445 bfd_vma got_address_high, got_address_low, load;
9446 const bfd_vma *plt_entry;
9448 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9449 BFD_ASSERT (h->dynindx != -1);
9450 BFD_ASSERT (htab->splt != NULL);
9451 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9452 BFD_ASSERT (!h->def_regular);
9454 /* Calculate the address of the PLT header. */
9455 header_address = (htab->splt->output_section->vma
9456 + htab->splt->output_offset);
9458 /* Calculate the index of the entry. */
9459 plt_index = ((h->plt.offset - htab->plt_header_size)
9460 / htab->plt_entry_size);
9462 /* Calculate the address of the .got.plt entry. */
9463 got_address = (htab->sgotplt->output_section->vma
9464 + htab->sgotplt->output_offset
9465 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9466 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9467 got_address_low = got_address & 0xffff;
9469 /* Initially point the .got.plt entry at the PLT header. */
9470 loc = (htab->sgotplt->contents
9471 + (2 + plt_index) * MIPS_ELF_GOT_SIZE (dynobj));
9472 if (ABI_64_P (output_bfd))
9473 bfd_put_64 (output_bfd, header_address, loc);
9474 else
9475 bfd_put_32 (output_bfd, header_address, loc);
9477 /* Find out where the .plt entry should go. */
9478 loc = htab->splt->contents + h->plt.offset;
9480 /* Pick the load opcode. */
9481 load = MIPS_ELF_LOAD_WORD (output_bfd);
9483 /* Fill in the PLT entry itself. */
9484 plt_entry = mips_exec_plt_entry;
9485 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
9486 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load, loc + 4);
9488 if (! LOAD_INTERLOCKS_P (output_bfd))
9490 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
9491 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9493 else
9495 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
9496 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 12);
9499 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9500 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
9501 plt_index, h->dynindx,
9502 R_MIPS_JUMP_SLOT, got_address);
9504 /* We distinguish between PLT entries and lazy-binding stubs by
9505 giving the former an st_other value of STO_MIPS_PLT. Set the
9506 flag and leave the value if there are any relocations in the
9507 binary where pointer equality matters. */
9508 sym->st_shndx = SHN_UNDEF;
9509 if (h->pointer_equality_needed)
9510 sym->st_other = STO_MIPS_PLT;
9511 else
9512 sym->st_value = 0;
9514 else if (h->plt.offset != MINUS_ONE)
9516 /* We've decided to create a lazy-binding stub. */
9517 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
9519 /* This symbol has a stub. Set it up. */
9521 BFD_ASSERT (h->dynindx != -1);
9523 BFD_ASSERT ((htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9524 || (h->dynindx <= 0xffff));
9526 /* Values up to 2^31 - 1 are allowed. Larger values would cause
9527 sign extension at runtime in the stub, resulting in a negative
9528 index value. */
9529 if (h->dynindx & ~0x7fffffff)
9530 return FALSE;
9532 /* Fill the stub. */
9533 idx = 0;
9534 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
9535 idx += 4;
9536 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
9537 idx += 4;
9538 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9540 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
9541 stub + idx);
9542 idx += 4;
9544 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
9545 idx += 4;
9547 /* If a large stub is not required and sign extension is not a
9548 problem, then use legacy code in the stub. */
9549 if (htab->function_stub_size == MIPS_FUNCTION_STUB_BIG_SIZE)
9550 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff), stub + idx);
9551 else if (h->dynindx & ~0x7fff)
9552 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff), stub + idx);
9553 else
9554 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
9555 stub + idx);
9557 BFD_ASSERT (h->plt.offset <= htab->sstubs->size);
9558 memcpy (htab->sstubs->contents + h->plt.offset,
9559 stub, htab->function_stub_size);
9561 /* Mark the symbol as undefined. plt.offset != -1 occurs
9562 only for the referenced symbol. */
9563 sym->st_shndx = SHN_UNDEF;
9565 /* The run-time linker uses the st_value field of the symbol
9566 to reset the global offset table entry for this external
9567 to its stub address when unlinking a shared object. */
9568 sym->st_value = (htab->sstubs->output_section->vma
9569 + htab->sstubs->output_offset
9570 + h->plt.offset);
9573 /* If we have a MIPS16 function with a stub, the dynamic symbol must
9574 refer to the stub, since only the stub uses the standard calling
9575 conventions. */
9576 if (h->dynindx != -1 && hmips->fn_stub != NULL)
9578 BFD_ASSERT (hmips->need_fn_stub);
9579 sym->st_value = (hmips->fn_stub->output_section->vma
9580 + hmips->fn_stub->output_offset);
9581 sym->st_size = hmips->fn_stub->size;
9582 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
9585 BFD_ASSERT (h->dynindx != -1
9586 || h->forced_local);
9588 sgot = htab->sgot;
9589 g = htab->got_info;
9590 BFD_ASSERT (g != NULL);
9592 /* Run through the global symbol table, creating GOT entries for all
9593 the symbols that need them. */
9594 if (hmips->global_got_area != GGA_NONE)
9596 bfd_vma offset;
9597 bfd_vma value;
9599 value = sym->st_value;
9600 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9601 R_MIPS_GOT16, info);
9602 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
9605 if (hmips->global_got_area != GGA_NONE && g->next && h->type != STT_TLS)
9607 struct mips_got_entry e, *p;
9608 bfd_vma entry;
9609 bfd_vma offset;
9611 gg = g;
9613 e.abfd = output_bfd;
9614 e.symndx = -1;
9615 e.d.h = hmips;
9616 e.tls_type = 0;
9618 for (g = g->next; g->next != gg; g = g->next)
9620 if (g->got_entries
9621 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
9622 &e)))
9624 offset = p->gotidx;
9625 if (info->shared
9626 || (elf_hash_table (info)->dynamic_sections_created
9627 && p->d.h != NULL
9628 && p->d.h->root.def_dynamic
9629 && !p->d.h->root.def_regular))
9631 /* Create an R_MIPS_REL32 relocation for this entry. Due to
9632 the various compatibility problems, it's easier to mock
9633 up an R_MIPS_32 or R_MIPS_64 relocation and leave
9634 mips_elf_create_dynamic_relocation to calculate the
9635 appropriate addend. */
9636 Elf_Internal_Rela rel[3];
9638 memset (rel, 0, sizeof (rel));
9639 if (ABI_64_P (output_bfd))
9640 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
9641 else
9642 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
9643 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
9645 entry = 0;
9646 if (! (mips_elf_create_dynamic_relocation
9647 (output_bfd, info, rel,
9648 e.d.h, NULL, sym->st_value, &entry, sgot)))
9649 return FALSE;
9651 else
9652 entry = sym->st_value;
9653 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
9658 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
9659 name = h->root.root.string;
9660 if (strcmp (name, "_DYNAMIC") == 0
9661 || h == elf_hash_table (info)->hgot)
9662 sym->st_shndx = SHN_ABS;
9663 else if (strcmp (name, "_DYNAMIC_LINK") == 0
9664 || strcmp (name, "_DYNAMIC_LINKING") == 0)
9666 sym->st_shndx = SHN_ABS;
9667 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9668 sym->st_value = 1;
9670 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
9672 sym->st_shndx = SHN_ABS;
9673 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9674 sym->st_value = elf_gp (output_bfd);
9676 else if (SGI_COMPAT (output_bfd))
9678 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
9679 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
9681 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9682 sym->st_other = STO_PROTECTED;
9683 sym->st_value = 0;
9684 sym->st_shndx = SHN_MIPS_DATA;
9686 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
9688 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
9689 sym->st_other = STO_PROTECTED;
9690 sym->st_value = mips_elf_hash_table (info)->procedure_count;
9691 sym->st_shndx = SHN_ABS;
9693 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
9695 if (h->type == STT_FUNC)
9696 sym->st_shndx = SHN_MIPS_TEXT;
9697 else if (h->type == STT_OBJECT)
9698 sym->st_shndx = SHN_MIPS_DATA;
9702 /* Emit a copy reloc, if needed. */
9703 if (h->needs_copy)
9705 asection *s;
9706 bfd_vma symval;
9708 BFD_ASSERT (h->dynindx != -1);
9709 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9711 s = mips_elf_rel_dyn_section (info, FALSE);
9712 symval = (h->root.u.def.section->output_section->vma
9713 + h->root.u.def.section->output_offset
9714 + h->root.u.def.value);
9715 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
9716 h->dynindx, R_MIPS_COPY, symval);
9719 /* Handle the IRIX6-specific symbols. */
9720 if (IRIX_COMPAT (output_bfd) == ict_irix6)
9721 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
9723 if (! info->shared)
9725 if (! mips_elf_hash_table (info)->use_rld_obj_head
9726 && (strcmp (name, "__rld_map") == 0
9727 || strcmp (name, "__RLD_MAP") == 0))
9729 asection *s = bfd_get_section_by_name (dynobj, ".rld_map");
9730 BFD_ASSERT (s != NULL);
9731 sym->st_value = s->output_section->vma + s->output_offset;
9732 bfd_put_32 (output_bfd, 0, s->contents);
9733 if (mips_elf_hash_table (info)->rld_value == 0)
9734 mips_elf_hash_table (info)->rld_value = sym->st_value;
9736 else if (mips_elf_hash_table (info)->use_rld_obj_head
9737 && strcmp (name, "__rld_obj_head") == 0)
9739 /* IRIX6 does not use a .rld_map section. */
9740 if (IRIX_COMPAT (output_bfd) == ict_irix5
9741 || IRIX_COMPAT (output_bfd) == ict_none)
9742 BFD_ASSERT (bfd_get_section_by_name (dynobj, ".rld_map")
9743 != NULL);
9744 mips_elf_hash_table (info)->rld_value = sym->st_value;
9748 /* Keep dynamic MIPS16 symbols odd. This allows the dynamic linker to
9749 treat MIPS16 symbols like any other. */
9750 if (ELF_ST_IS_MIPS16 (sym->st_other))
9752 BFD_ASSERT (sym->st_value & 1);
9753 sym->st_other -= STO_MIPS16;
9756 return TRUE;
9759 /* Likewise, for VxWorks. */
9761 bfd_boolean
9762 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
9763 struct bfd_link_info *info,
9764 struct elf_link_hash_entry *h,
9765 Elf_Internal_Sym *sym)
9767 bfd *dynobj;
9768 asection *sgot;
9769 struct mips_got_info *g;
9770 struct mips_elf_link_hash_table *htab;
9771 struct mips_elf_link_hash_entry *hmips;
9773 htab = mips_elf_hash_table (info);
9774 BFD_ASSERT (htab != NULL);
9775 dynobj = elf_hash_table (info)->dynobj;
9776 hmips = (struct mips_elf_link_hash_entry *) h;
9778 if (h->plt.offset != (bfd_vma) -1)
9780 bfd_byte *loc;
9781 bfd_vma plt_address, plt_index, got_address, got_offset, branch_offset;
9782 Elf_Internal_Rela rel;
9783 static const bfd_vma *plt_entry;
9785 BFD_ASSERT (h->dynindx != -1);
9786 BFD_ASSERT (htab->splt != NULL);
9787 BFD_ASSERT (h->plt.offset <= htab->splt->size);
9789 /* Calculate the address of the .plt entry. */
9790 plt_address = (htab->splt->output_section->vma
9791 + htab->splt->output_offset
9792 + h->plt.offset);
9794 /* Calculate the index of the entry. */
9795 plt_index = ((h->plt.offset - htab->plt_header_size)
9796 / htab->plt_entry_size);
9798 /* Calculate the address of the .got.plt entry. */
9799 got_address = (htab->sgotplt->output_section->vma
9800 + htab->sgotplt->output_offset
9801 + plt_index * 4);
9803 /* Calculate the offset of the .got.plt entry from
9804 _GLOBAL_OFFSET_TABLE_. */
9805 got_offset = mips_elf_gotplt_index (info, h);
9807 /* Calculate the offset for the branch at the start of the PLT
9808 entry. The branch jumps to the beginning of .plt. */
9809 branch_offset = -(h->plt.offset / 4 + 1) & 0xffff;
9811 /* Fill in the initial value of the .got.plt entry. */
9812 bfd_put_32 (output_bfd, plt_address,
9813 htab->sgotplt->contents + plt_index * 4);
9815 /* Find out where the .plt entry should go. */
9816 loc = htab->splt->contents + h->plt.offset;
9818 if (info->shared)
9820 plt_entry = mips_vxworks_shared_plt_entry;
9821 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9822 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9824 else
9826 bfd_vma got_address_high, got_address_low;
9828 plt_entry = mips_vxworks_exec_plt_entry;
9829 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
9830 got_address_low = got_address & 0xffff;
9832 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
9833 bfd_put_32 (output_bfd, plt_entry[1] | plt_index, loc + 4);
9834 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
9835 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
9836 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9837 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9838 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9839 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9841 loc = (htab->srelplt2->contents
9842 + (plt_index * 3 + 2) * sizeof (Elf32_External_Rela));
9844 /* Emit a relocation for the .got.plt entry. */
9845 rel.r_offset = got_address;
9846 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
9847 rel.r_addend = h->plt.offset;
9848 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9850 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
9851 loc += sizeof (Elf32_External_Rela);
9852 rel.r_offset = plt_address + 8;
9853 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
9854 rel.r_addend = got_offset;
9855 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9857 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
9858 loc += sizeof (Elf32_External_Rela);
9859 rel.r_offset += 4;
9860 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
9861 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9864 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
9865 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
9866 rel.r_offset = got_address;
9867 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
9868 rel.r_addend = 0;
9869 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
9871 if (!h->def_regular)
9872 sym->st_shndx = SHN_UNDEF;
9875 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
9877 sgot = htab->sgot;
9878 g = htab->got_info;
9879 BFD_ASSERT (g != NULL);
9881 /* See if this symbol has an entry in the GOT. */
9882 if (hmips->global_got_area != GGA_NONE)
9884 bfd_vma offset;
9885 Elf_Internal_Rela outrel;
9886 bfd_byte *loc;
9887 asection *s;
9889 /* Install the symbol value in the GOT. */
9890 offset = mips_elf_global_got_index (dynobj, output_bfd, h,
9891 R_MIPS_GOT16, info);
9892 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
9894 /* Add a dynamic relocation for it. */
9895 s = mips_elf_rel_dyn_section (info, FALSE);
9896 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
9897 outrel.r_offset = (sgot->output_section->vma
9898 + sgot->output_offset
9899 + offset);
9900 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
9901 outrel.r_addend = 0;
9902 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
9905 /* Emit a copy reloc, if needed. */
9906 if (h->needs_copy)
9908 Elf_Internal_Rela rel;
9910 BFD_ASSERT (h->dynindx != -1);
9912 rel.r_offset = (h->root.u.def.section->output_section->vma
9913 + h->root.u.def.section->output_offset
9914 + h->root.u.def.value);
9915 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
9916 rel.r_addend = 0;
9917 bfd_elf32_swap_reloca_out (output_bfd, &rel,
9918 htab->srelbss->contents
9919 + (htab->srelbss->reloc_count
9920 * sizeof (Elf32_External_Rela)));
9921 ++htab->srelbss->reloc_count;
9924 /* If this is a mips16 symbol, force the value to be even. */
9925 if (ELF_ST_IS_MIPS16 (sym->st_other))
9926 sym->st_value &= ~1;
9928 return TRUE;
9931 /* Write out a plt0 entry to the beginning of .plt. */
9933 static void
9934 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9936 bfd_byte *loc;
9937 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
9938 static const bfd_vma *plt_entry;
9939 struct mips_elf_link_hash_table *htab;
9941 htab = mips_elf_hash_table (info);
9942 BFD_ASSERT (htab != NULL);
9944 if (ABI_64_P (output_bfd))
9945 plt_entry = mips_n64_exec_plt0_entry;
9946 else if (ABI_N32_P (output_bfd))
9947 plt_entry = mips_n32_exec_plt0_entry;
9948 else
9949 plt_entry = mips_o32_exec_plt0_entry;
9951 /* Calculate the value of .got.plt. */
9952 gotplt_value = (htab->sgotplt->output_section->vma
9953 + htab->sgotplt->output_offset);
9954 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
9955 gotplt_value_low = gotplt_value & 0xffff;
9957 /* The PLT sequence is not safe for N64 if .got.plt's address can
9958 not be loaded in two instructions. */
9959 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
9960 || ~(gotplt_value | 0x7fffffff) == 0);
9962 /* Install the PLT header. */
9963 loc = htab->splt->contents;
9964 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
9965 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
9966 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
9967 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
9968 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
9969 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
9970 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
9971 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
9974 /* Install the PLT header for a VxWorks executable and finalize the
9975 contents of .rela.plt.unloaded. */
9977 static void
9978 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
9980 Elf_Internal_Rela rela;
9981 bfd_byte *loc;
9982 bfd_vma got_value, got_value_high, got_value_low, plt_address;
9983 static const bfd_vma *plt_entry;
9984 struct mips_elf_link_hash_table *htab;
9986 htab = mips_elf_hash_table (info);
9987 BFD_ASSERT (htab != NULL);
9989 plt_entry = mips_vxworks_exec_plt0_entry;
9991 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
9992 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
9993 + htab->root.hgot->root.u.def.section->output_offset
9994 + htab->root.hgot->root.u.def.value);
9996 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
9997 got_value_low = got_value & 0xffff;
9999 /* Calculate the address of the PLT header. */
10000 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
10002 /* Install the PLT header. */
10003 loc = htab->splt->contents;
10004 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
10005 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
10006 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
10007 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10008 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10009 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10011 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
10012 loc = htab->srelplt2->contents;
10013 rela.r_offset = plt_address;
10014 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10015 rela.r_addend = 0;
10016 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10017 loc += sizeof (Elf32_External_Rela);
10019 /* Output the relocation for the following addiu of
10020 %lo(_GLOBAL_OFFSET_TABLE_). */
10021 rela.r_offset += 4;
10022 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10023 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
10024 loc += sizeof (Elf32_External_Rela);
10026 /* Fix up the remaining relocations. They may have the wrong
10027 symbol index for _G_O_T_ or _P_L_T_ depending on the order
10028 in which symbols were output. */
10029 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
10031 Elf_Internal_Rela rel;
10033 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10034 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10035 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10036 loc += sizeof (Elf32_External_Rela);
10038 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10039 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10040 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10041 loc += sizeof (Elf32_External_Rela);
10043 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
10044 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10045 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10046 loc += sizeof (Elf32_External_Rela);
10050 /* Install the PLT header for a VxWorks shared library. */
10052 static void
10053 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
10055 unsigned int i;
10056 struct mips_elf_link_hash_table *htab;
10058 htab = mips_elf_hash_table (info);
10059 BFD_ASSERT (htab != NULL);
10061 /* We just need to copy the entry byte-by-byte. */
10062 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
10063 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
10064 htab->splt->contents + i * 4);
10067 /* Finish up the dynamic sections. */
10069 bfd_boolean
10070 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
10071 struct bfd_link_info *info)
10073 bfd *dynobj;
10074 asection *sdyn;
10075 asection *sgot;
10076 struct mips_got_info *gg, *g;
10077 struct mips_elf_link_hash_table *htab;
10079 htab = mips_elf_hash_table (info);
10080 BFD_ASSERT (htab != NULL);
10082 dynobj = elf_hash_table (info)->dynobj;
10084 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
10086 sgot = htab->sgot;
10087 gg = htab->got_info;
10089 if (elf_hash_table (info)->dynamic_sections_created)
10091 bfd_byte *b;
10092 int dyn_to_skip = 0, dyn_skipped = 0;
10094 BFD_ASSERT (sdyn != NULL);
10095 BFD_ASSERT (gg != NULL);
10097 g = mips_elf_got_for_ibfd (gg, output_bfd);
10098 BFD_ASSERT (g != NULL);
10100 for (b = sdyn->contents;
10101 b < sdyn->contents + sdyn->size;
10102 b += MIPS_ELF_DYN_SIZE (dynobj))
10104 Elf_Internal_Dyn dyn;
10105 const char *name;
10106 size_t elemsize;
10107 asection *s;
10108 bfd_boolean swap_out_p;
10110 /* Read in the current dynamic entry. */
10111 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10113 /* Assume that we're going to modify it and write it out. */
10114 swap_out_p = TRUE;
10116 switch (dyn.d_tag)
10118 case DT_RELENT:
10119 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
10120 break;
10122 case DT_RELAENT:
10123 BFD_ASSERT (htab->is_vxworks);
10124 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
10125 break;
10127 case DT_STRSZ:
10128 /* Rewrite DT_STRSZ. */
10129 dyn.d_un.d_val =
10130 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
10131 break;
10133 case DT_PLTGOT:
10134 s = htab->sgot;
10135 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10136 break;
10138 case DT_MIPS_PLTGOT:
10139 s = htab->sgotplt;
10140 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
10141 break;
10143 case DT_MIPS_RLD_VERSION:
10144 dyn.d_un.d_val = 1; /* XXX */
10145 break;
10147 case DT_MIPS_FLAGS:
10148 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
10149 break;
10151 case DT_MIPS_TIME_STAMP:
10153 time_t t;
10154 time (&t);
10155 dyn.d_un.d_val = t;
10157 break;
10159 case DT_MIPS_ICHECKSUM:
10160 /* XXX FIXME: */
10161 swap_out_p = FALSE;
10162 break;
10164 case DT_MIPS_IVERSION:
10165 /* XXX FIXME: */
10166 swap_out_p = FALSE;
10167 break;
10169 case DT_MIPS_BASE_ADDRESS:
10170 s = output_bfd->sections;
10171 BFD_ASSERT (s != NULL);
10172 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
10173 break;
10175 case DT_MIPS_LOCAL_GOTNO:
10176 dyn.d_un.d_val = g->local_gotno;
10177 break;
10179 case DT_MIPS_UNREFEXTNO:
10180 /* The index into the dynamic symbol table which is the
10181 entry of the first external symbol that is not
10182 referenced within the same object. */
10183 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
10184 break;
10186 case DT_MIPS_GOTSYM:
10187 if (gg->global_gotsym)
10189 dyn.d_un.d_val = gg->global_gotsym->dynindx;
10190 break;
10192 /* In case if we don't have global got symbols we default
10193 to setting DT_MIPS_GOTSYM to the same value as
10194 DT_MIPS_SYMTABNO, so we just fall through. */
10196 case DT_MIPS_SYMTABNO:
10197 name = ".dynsym";
10198 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
10199 s = bfd_get_section_by_name (output_bfd, name);
10200 BFD_ASSERT (s != NULL);
10202 dyn.d_un.d_val = s->size / elemsize;
10203 break;
10205 case DT_MIPS_HIPAGENO:
10206 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
10207 break;
10209 case DT_MIPS_RLD_MAP:
10210 dyn.d_un.d_ptr = mips_elf_hash_table (info)->rld_value;
10211 break;
10213 case DT_MIPS_OPTIONS:
10214 s = (bfd_get_section_by_name
10215 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
10216 dyn.d_un.d_ptr = s->vma;
10217 break;
10219 case DT_RELASZ:
10220 BFD_ASSERT (htab->is_vxworks);
10221 /* The count does not include the JUMP_SLOT relocations. */
10222 if (htab->srelplt)
10223 dyn.d_un.d_val -= htab->srelplt->size;
10224 break;
10226 case DT_PLTREL:
10227 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10228 if (htab->is_vxworks)
10229 dyn.d_un.d_val = DT_RELA;
10230 else
10231 dyn.d_un.d_val = DT_REL;
10232 break;
10234 case DT_PLTRELSZ:
10235 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10236 dyn.d_un.d_val = htab->srelplt->size;
10237 break;
10239 case DT_JMPREL:
10240 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10241 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
10242 + htab->srelplt->output_offset);
10243 break;
10245 case DT_TEXTREL:
10246 /* If we didn't need any text relocations after all, delete
10247 the dynamic tag. */
10248 if (!(info->flags & DF_TEXTREL))
10250 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
10251 swap_out_p = FALSE;
10253 break;
10255 case DT_FLAGS:
10256 /* If we didn't need any text relocations after all, clear
10257 DF_TEXTREL from DT_FLAGS. */
10258 if (!(info->flags & DF_TEXTREL))
10259 dyn.d_un.d_val &= ~DF_TEXTREL;
10260 else
10261 swap_out_p = FALSE;
10262 break;
10264 default:
10265 swap_out_p = FALSE;
10266 if (htab->is_vxworks
10267 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
10268 swap_out_p = TRUE;
10269 break;
10272 if (swap_out_p || dyn_skipped)
10273 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10274 (dynobj, &dyn, b - dyn_skipped);
10276 if (dyn_to_skip)
10278 dyn_skipped += dyn_to_skip;
10279 dyn_to_skip = 0;
10283 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
10284 if (dyn_skipped > 0)
10285 memset (b - dyn_skipped, 0, dyn_skipped);
10288 if (sgot != NULL && sgot->size > 0
10289 && !bfd_is_abs_section (sgot->output_section))
10291 if (htab->is_vxworks)
10293 /* The first entry of the global offset table points to the
10294 ".dynamic" section. The second is initialized by the
10295 loader and contains the shared library identifier.
10296 The third is also initialized by the loader and points
10297 to the lazy resolution stub. */
10298 MIPS_ELF_PUT_WORD (output_bfd,
10299 sdyn->output_offset + sdyn->output_section->vma,
10300 sgot->contents);
10301 MIPS_ELF_PUT_WORD (output_bfd, 0,
10302 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10303 MIPS_ELF_PUT_WORD (output_bfd, 0,
10304 sgot->contents
10305 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
10307 else
10309 /* The first entry of the global offset table will be filled at
10310 runtime. The second entry will be used by some runtime loaders.
10311 This isn't the case of IRIX rld. */
10312 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
10313 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10314 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
10317 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
10318 = MIPS_ELF_GOT_SIZE (output_bfd);
10321 /* Generate dynamic relocations for the non-primary gots. */
10322 if (gg != NULL && gg->next)
10324 Elf_Internal_Rela rel[3];
10325 bfd_vma addend = 0;
10327 memset (rel, 0, sizeof (rel));
10328 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
10330 for (g = gg->next; g->next != gg; g = g->next)
10332 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
10333 + g->next->tls_gotno;
10335 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
10336 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10337 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
10338 sgot->contents
10339 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
10341 if (! info->shared)
10342 continue;
10344 while (got_index < g->assigned_gotno)
10346 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
10347 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
10348 if (!(mips_elf_create_dynamic_relocation
10349 (output_bfd, info, rel, NULL,
10350 bfd_abs_section_ptr,
10351 0, &addend, sgot)))
10352 return FALSE;
10353 BFD_ASSERT (addend == 0);
10358 /* The generation of dynamic relocations for the non-primary gots
10359 adds more dynamic relocations. We cannot count them until
10360 here. */
10362 if (elf_hash_table (info)->dynamic_sections_created)
10364 bfd_byte *b;
10365 bfd_boolean swap_out_p;
10367 BFD_ASSERT (sdyn != NULL);
10369 for (b = sdyn->contents;
10370 b < sdyn->contents + sdyn->size;
10371 b += MIPS_ELF_DYN_SIZE (dynobj))
10373 Elf_Internal_Dyn dyn;
10374 asection *s;
10376 /* Read in the current dynamic entry. */
10377 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
10379 /* Assume that we're going to modify it and write it out. */
10380 swap_out_p = TRUE;
10382 switch (dyn.d_tag)
10384 case DT_RELSZ:
10385 /* Reduce DT_RELSZ to account for any relocations we
10386 decided not to make. This is for the n64 irix rld,
10387 which doesn't seem to apply any relocations if there
10388 are trailing null entries. */
10389 s = mips_elf_rel_dyn_section (info, FALSE);
10390 dyn.d_un.d_val = (s->reloc_count
10391 * (ABI_64_P (output_bfd)
10392 ? sizeof (Elf64_Mips_External_Rel)
10393 : sizeof (Elf32_External_Rel)));
10394 /* Adjust the section size too. Tools like the prelinker
10395 can reasonably expect the values to the same. */
10396 elf_section_data (s->output_section)->this_hdr.sh_size
10397 = dyn.d_un.d_val;
10398 break;
10400 default:
10401 swap_out_p = FALSE;
10402 break;
10405 if (swap_out_p)
10406 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
10407 (dynobj, &dyn, b);
10412 asection *s;
10413 Elf32_compact_rel cpt;
10415 if (SGI_COMPAT (output_bfd))
10417 /* Write .compact_rel section out. */
10418 s = bfd_get_section_by_name (dynobj, ".compact_rel");
10419 if (s != NULL)
10421 cpt.id1 = 1;
10422 cpt.num = s->reloc_count;
10423 cpt.id2 = 2;
10424 cpt.offset = (s->output_section->filepos
10425 + sizeof (Elf32_External_compact_rel));
10426 cpt.reserved0 = 0;
10427 cpt.reserved1 = 0;
10428 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
10429 ((Elf32_External_compact_rel *)
10430 s->contents));
10432 /* Clean up a dummy stub function entry in .text. */
10433 if (htab->sstubs != NULL)
10435 file_ptr dummy_offset;
10437 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
10438 dummy_offset = htab->sstubs->size - htab->function_stub_size;
10439 memset (htab->sstubs->contents + dummy_offset, 0,
10440 htab->function_stub_size);
10445 /* The psABI says that the dynamic relocations must be sorted in
10446 increasing order of r_symndx. The VxWorks EABI doesn't require
10447 this, and because the code below handles REL rather than RELA
10448 relocations, using it for VxWorks would be outright harmful. */
10449 if (!htab->is_vxworks)
10451 s = mips_elf_rel_dyn_section (info, FALSE);
10452 if (s != NULL
10453 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
10455 reldyn_sorting_bfd = output_bfd;
10457 if (ABI_64_P (output_bfd))
10458 qsort ((Elf64_External_Rel *) s->contents + 1,
10459 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
10460 sort_dynamic_relocs_64);
10461 else
10462 qsort ((Elf32_External_Rel *) s->contents + 1,
10463 s->reloc_count - 1, sizeof (Elf32_External_Rel),
10464 sort_dynamic_relocs);
10469 if (htab->splt && htab->splt->size > 0)
10471 if (htab->is_vxworks)
10473 if (info->shared)
10474 mips_vxworks_finish_shared_plt (output_bfd, info);
10475 else
10476 mips_vxworks_finish_exec_plt (output_bfd, info);
10478 else
10480 BFD_ASSERT (!info->shared);
10481 mips_finish_exec_plt (output_bfd, info);
10484 return TRUE;
10488 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
10490 static void
10491 mips_set_isa_flags (bfd *abfd)
10493 flagword val;
10495 switch (bfd_get_mach (abfd))
10497 default:
10498 case bfd_mach_mips3000:
10499 val = E_MIPS_ARCH_1;
10500 break;
10502 case bfd_mach_mips3900:
10503 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
10504 break;
10506 case bfd_mach_mips6000:
10507 val = E_MIPS_ARCH_2;
10508 break;
10510 case bfd_mach_mips4000:
10511 case bfd_mach_mips4300:
10512 case bfd_mach_mips4400:
10513 case bfd_mach_mips4600:
10514 val = E_MIPS_ARCH_3;
10515 break;
10517 case bfd_mach_mips4010:
10518 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
10519 break;
10521 case bfd_mach_mips4100:
10522 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
10523 break;
10525 case bfd_mach_mips4111:
10526 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
10527 break;
10529 case bfd_mach_mips4120:
10530 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
10531 break;
10533 case bfd_mach_mips4650:
10534 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
10535 break;
10537 case bfd_mach_mips5400:
10538 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
10539 break;
10541 case bfd_mach_mips5500:
10542 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
10543 break;
10545 case bfd_mach_mips9000:
10546 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
10547 break;
10549 case bfd_mach_mips5000:
10550 case bfd_mach_mips7000:
10551 case bfd_mach_mips8000:
10552 case bfd_mach_mips10000:
10553 case bfd_mach_mips12000:
10554 case bfd_mach_mips14000:
10555 case bfd_mach_mips16000:
10556 val = E_MIPS_ARCH_4;
10557 break;
10559 case bfd_mach_mips5:
10560 val = E_MIPS_ARCH_5;
10561 break;
10563 case bfd_mach_mips_loongson_2e:
10564 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
10565 break;
10567 case bfd_mach_mips_loongson_2f:
10568 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
10569 break;
10571 case bfd_mach_mips_sb1:
10572 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
10573 break;
10575 case bfd_mach_mips_loongson_3a:
10576 val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
10577 break;
10579 case bfd_mach_mips_octeon:
10580 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
10581 break;
10583 case bfd_mach_mips_xlr:
10584 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
10585 break;
10587 case bfd_mach_mipsisa32:
10588 val = E_MIPS_ARCH_32;
10589 break;
10591 case bfd_mach_mipsisa64:
10592 val = E_MIPS_ARCH_64;
10593 break;
10595 case bfd_mach_mipsisa32r2:
10596 val = E_MIPS_ARCH_32R2;
10597 break;
10599 case bfd_mach_mipsisa64r2:
10600 val = E_MIPS_ARCH_64R2;
10601 break;
10603 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
10604 elf_elfheader (abfd)->e_flags |= val;
10609 /* The final processing done just before writing out a MIPS ELF object
10610 file. This gets the MIPS architecture right based on the machine
10611 number. This is used by both the 32-bit and the 64-bit ABI. */
10613 void
10614 _bfd_mips_elf_final_write_processing (bfd *abfd,
10615 bfd_boolean linker ATTRIBUTE_UNUSED)
10617 unsigned int i;
10618 Elf_Internal_Shdr **hdrpp;
10619 const char *name;
10620 asection *sec;
10622 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
10623 is nonzero. This is for compatibility with old objects, which used
10624 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
10625 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
10626 mips_set_isa_flags (abfd);
10628 /* Set the sh_info field for .gptab sections and other appropriate
10629 info for each special section. */
10630 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
10631 i < elf_numsections (abfd);
10632 i++, hdrpp++)
10634 switch ((*hdrpp)->sh_type)
10636 case SHT_MIPS_MSYM:
10637 case SHT_MIPS_LIBLIST:
10638 sec = bfd_get_section_by_name (abfd, ".dynstr");
10639 if (sec != NULL)
10640 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10641 break;
10643 case SHT_MIPS_GPTAB:
10644 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10645 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10646 BFD_ASSERT (name != NULL
10647 && CONST_STRNEQ (name, ".gptab."));
10648 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
10649 BFD_ASSERT (sec != NULL);
10650 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10651 break;
10653 case SHT_MIPS_CONTENT:
10654 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10655 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10656 BFD_ASSERT (name != NULL
10657 && CONST_STRNEQ (name, ".MIPS.content"));
10658 sec = bfd_get_section_by_name (abfd,
10659 name + sizeof ".MIPS.content" - 1);
10660 BFD_ASSERT (sec != NULL);
10661 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10662 break;
10664 case SHT_MIPS_SYMBOL_LIB:
10665 sec = bfd_get_section_by_name (abfd, ".dynsym");
10666 if (sec != NULL)
10667 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10668 sec = bfd_get_section_by_name (abfd, ".liblist");
10669 if (sec != NULL)
10670 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
10671 break;
10673 case SHT_MIPS_EVENTS:
10674 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
10675 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
10676 BFD_ASSERT (name != NULL);
10677 if (CONST_STRNEQ (name, ".MIPS.events"))
10678 sec = bfd_get_section_by_name (abfd,
10679 name + sizeof ".MIPS.events" - 1);
10680 else
10682 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
10683 sec = bfd_get_section_by_name (abfd,
10684 (name
10685 + sizeof ".MIPS.post_rel" - 1));
10687 BFD_ASSERT (sec != NULL);
10688 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
10689 break;
10695 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
10696 segments. */
10699 _bfd_mips_elf_additional_program_headers (bfd *abfd,
10700 struct bfd_link_info *info ATTRIBUTE_UNUSED)
10702 asection *s;
10703 int ret = 0;
10705 /* See if we need a PT_MIPS_REGINFO segment. */
10706 s = bfd_get_section_by_name (abfd, ".reginfo");
10707 if (s && (s->flags & SEC_LOAD))
10708 ++ret;
10710 /* See if we need a PT_MIPS_OPTIONS segment. */
10711 if (IRIX_COMPAT (abfd) == ict_irix6
10712 && bfd_get_section_by_name (abfd,
10713 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
10714 ++ret;
10716 /* See if we need a PT_MIPS_RTPROC segment. */
10717 if (IRIX_COMPAT (abfd) == ict_irix5
10718 && bfd_get_section_by_name (abfd, ".dynamic")
10719 && bfd_get_section_by_name (abfd, ".mdebug"))
10720 ++ret;
10722 /* Allocate a PT_NULL header in dynamic objects. See
10723 _bfd_mips_elf_modify_segment_map for details. */
10724 if (!SGI_COMPAT (abfd)
10725 && bfd_get_section_by_name (abfd, ".dynamic"))
10726 ++ret;
10728 return ret;
10731 /* Modify the segment map for an IRIX5 executable. */
10733 bfd_boolean
10734 _bfd_mips_elf_modify_segment_map (bfd *abfd,
10735 struct bfd_link_info *info)
10737 asection *s;
10738 struct elf_segment_map *m, **pm;
10739 bfd_size_type amt;
10741 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
10742 segment. */
10743 s = bfd_get_section_by_name (abfd, ".reginfo");
10744 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10746 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10747 if (m->p_type == PT_MIPS_REGINFO)
10748 break;
10749 if (m == NULL)
10751 amt = sizeof *m;
10752 m = bfd_zalloc (abfd, amt);
10753 if (m == NULL)
10754 return FALSE;
10756 m->p_type = PT_MIPS_REGINFO;
10757 m->count = 1;
10758 m->sections[0] = s;
10760 /* We want to put it after the PHDR and INTERP segments. */
10761 pm = &elf_tdata (abfd)->segment_map;
10762 while (*pm != NULL
10763 && ((*pm)->p_type == PT_PHDR
10764 || (*pm)->p_type == PT_INTERP))
10765 pm = &(*pm)->next;
10767 m->next = *pm;
10768 *pm = m;
10772 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
10773 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
10774 PT_MIPS_OPTIONS segment immediately following the program header
10775 table. */
10776 if (NEWABI_P (abfd)
10777 /* On non-IRIX6 new abi, we'll have already created a segment
10778 for this section, so don't create another. I'm not sure this
10779 is not also the case for IRIX 6, but I can't test it right
10780 now. */
10781 && IRIX_COMPAT (abfd) == ict_irix6)
10783 for (s = abfd->sections; s; s = s->next)
10784 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
10785 break;
10787 if (s)
10789 struct elf_segment_map *options_segment;
10791 pm = &elf_tdata (abfd)->segment_map;
10792 while (*pm != NULL
10793 && ((*pm)->p_type == PT_PHDR
10794 || (*pm)->p_type == PT_INTERP))
10795 pm = &(*pm)->next;
10797 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
10799 amt = sizeof (struct elf_segment_map);
10800 options_segment = bfd_zalloc (abfd, amt);
10801 options_segment->next = *pm;
10802 options_segment->p_type = PT_MIPS_OPTIONS;
10803 options_segment->p_flags = PF_R;
10804 options_segment->p_flags_valid = TRUE;
10805 options_segment->count = 1;
10806 options_segment->sections[0] = s;
10807 *pm = options_segment;
10811 else
10813 if (IRIX_COMPAT (abfd) == ict_irix5)
10815 /* If there are .dynamic and .mdebug sections, we make a room
10816 for the RTPROC header. FIXME: Rewrite without section names. */
10817 if (bfd_get_section_by_name (abfd, ".interp") == NULL
10818 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
10819 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
10821 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
10822 if (m->p_type == PT_MIPS_RTPROC)
10823 break;
10824 if (m == NULL)
10826 amt = sizeof *m;
10827 m = bfd_zalloc (abfd, amt);
10828 if (m == NULL)
10829 return FALSE;
10831 m->p_type = PT_MIPS_RTPROC;
10833 s = bfd_get_section_by_name (abfd, ".rtproc");
10834 if (s == NULL)
10836 m->count = 0;
10837 m->p_flags = 0;
10838 m->p_flags_valid = 1;
10840 else
10842 m->count = 1;
10843 m->sections[0] = s;
10846 /* We want to put it after the DYNAMIC segment. */
10847 pm = &elf_tdata (abfd)->segment_map;
10848 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
10849 pm = &(*pm)->next;
10850 if (*pm != NULL)
10851 pm = &(*pm)->next;
10853 m->next = *pm;
10854 *pm = m;
10858 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
10859 .dynstr, .dynsym, and .hash sections, and everything in
10860 between. */
10861 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL;
10862 pm = &(*pm)->next)
10863 if ((*pm)->p_type == PT_DYNAMIC)
10864 break;
10865 m = *pm;
10866 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
10868 /* For a normal mips executable the permissions for the PT_DYNAMIC
10869 segment are read, write and execute. We do that here since
10870 the code in elf.c sets only the read permission. This matters
10871 sometimes for the dynamic linker. */
10872 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
10874 m->p_flags = PF_R | PF_W | PF_X;
10875 m->p_flags_valid = 1;
10878 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
10879 glibc's dynamic linker has traditionally derived the number of
10880 tags from the p_filesz field, and sometimes allocates stack
10881 arrays of that size. An overly-big PT_DYNAMIC segment can
10882 be actively harmful in such cases. Making PT_DYNAMIC contain
10883 other sections can also make life hard for the prelinker,
10884 which might move one of the other sections to a different
10885 PT_LOAD segment. */
10886 if (SGI_COMPAT (abfd)
10887 && m != NULL
10888 && m->count == 1
10889 && strcmp (m->sections[0]->name, ".dynamic") == 0)
10891 static const char *sec_names[] =
10893 ".dynamic", ".dynstr", ".dynsym", ".hash"
10895 bfd_vma low, high;
10896 unsigned int i, c;
10897 struct elf_segment_map *n;
10899 low = ~(bfd_vma) 0;
10900 high = 0;
10901 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
10903 s = bfd_get_section_by_name (abfd, sec_names[i]);
10904 if (s != NULL && (s->flags & SEC_LOAD) != 0)
10906 bfd_size_type sz;
10908 if (low > s->vma)
10909 low = s->vma;
10910 sz = s->size;
10911 if (high < s->vma + sz)
10912 high = s->vma + sz;
10916 c = 0;
10917 for (s = abfd->sections; s != NULL; s = s->next)
10918 if ((s->flags & SEC_LOAD) != 0
10919 && s->vma >= low
10920 && s->vma + s->size <= high)
10921 ++c;
10923 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
10924 n = bfd_zalloc (abfd, amt);
10925 if (n == NULL)
10926 return FALSE;
10927 *n = *m;
10928 n->count = c;
10930 i = 0;
10931 for (s = abfd->sections; s != NULL; s = s->next)
10933 if ((s->flags & SEC_LOAD) != 0
10934 && s->vma >= low
10935 && s->vma + s->size <= high)
10937 n->sections[i] = s;
10938 ++i;
10942 *pm = n;
10946 /* Allocate a spare program header in dynamic objects so that tools
10947 like the prelinker can add an extra PT_LOAD entry.
10949 If the prelinker needs to make room for a new PT_LOAD entry, its
10950 standard procedure is to move the first (read-only) sections into
10951 the new (writable) segment. However, the MIPS ABI requires
10952 .dynamic to be in a read-only segment, and the section will often
10953 start within sizeof (ElfNN_Phdr) bytes of the last program header.
10955 Although the prelinker could in principle move .dynamic to a
10956 writable segment, it seems better to allocate a spare program
10957 header instead, and avoid the need to move any sections.
10958 There is a long tradition of allocating spare dynamic tags,
10959 so allocating a spare program header seems like a natural
10960 extension.
10962 If INFO is NULL, we may be copying an already prelinked binary
10963 with objcopy or strip, so do not add this header. */
10964 if (info != NULL
10965 && !SGI_COMPAT (abfd)
10966 && bfd_get_section_by_name (abfd, ".dynamic"))
10968 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next)
10969 if ((*pm)->p_type == PT_NULL)
10970 break;
10971 if (*pm == NULL)
10973 m = bfd_zalloc (abfd, sizeof (*m));
10974 if (m == NULL)
10975 return FALSE;
10977 m->p_type = PT_NULL;
10978 *pm = m;
10982 return TRUE;
10985 /* Return the section that should be marked against GC for a given
10986 relocation. */
10988 asection *
10989 _bfd_mips_elf_gc_mark_hook (asection *sec,
10990 struct bfd_link_info *info,
10991 Elf_Internal_Rela *rel,
10992 struct elf_link_hash_entry *h,
10993 Elf_Internal_Sym *sym)
10995 /* ??? Do mips16 stub sections need to be handled special? */
10997 if (h != NULL)
10998 switch (ELF_R_TYPE (sec->owner, rel->r_info))
11000 case R_MIPS_GNU_VTINHERIT:
11001 case R_MIPS_GNU_VTENTRY:
11002 return NULL;
11005 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
11008 /* Update the got entry reference counts for the section being removed. */
11010 bfd_boolean
11011 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
11012 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11013 asection *sec ATTRIBUTE_UNUSED,
11014 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
11016 #if 0
11017 Elf_Internal_Shdr *symtab_hdr;
11018 struct elf_link_hash_entry **sym_hashes;
11019 bfd_signed_vma *local_got_refcounts;
11020 const Elf_Internal_Rela *rel, *relend;
11021 unsigned long r_symndx;
11022 struct elf_link_hash_entry *h;
11024 if (info->relocatable)
11025 return TRUE;
11027 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11028 sym_hashes = elf_sym_hashes (abfd);
11029 local_got_refcounts = elf_local_got_refcounts (abfd);
11031 relend = relocs + sec->reloc_count;
11032 for (rel = relocs; rel < relend; rel++)
11033 switch (ELF_R_TYPE (abfd, rel->r_info))
11035 case R_MIPS16_GOT16:
11036 case R_MIPS16_CALL16:
11037 case R_MIPS_GOT16:
11038 case R_MIPS_CALL16:
11039 case R_MIPS_CALL_HI16:
11040 case R_MIPS_CALL_LO16:
11041 case R_MIPS_GOT_HI16:
11042 case R_MIPS_GOT_LO16:
11043 case R_MIPS_GOT_DISP:
11044 case R_MIPS_GOT_PAGE:
11045 case R_MIPS_GOT_OFST:
11046 /* ??? It would seem that the existing MIPS code does no sort
11047 of reference counting or whatnot on its GOT and PLT entries,
11048 so it is not possible to garbage collect them at this time. */
11049 break;
11051 default:
11052 break;
11054 #endif
11056 return TRUE;
11059 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
11060 hiding the old indirect symbol. Process additional relocation
11061 information. Also called for weakdefs, in which case we just let
11062 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
11064 void
11065 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
11066 struct elf_link_hash_entry *dir,
11067 struct elf_link_hash_entry *ind)
11069 struct mips_elf_link_hash_entry *dirmips, *indmips;
11071 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
11073 dirmips = (struct mips_elf_link_hash_entry *) dir;
11074 indmips = (struct mips_elf_link_hash_entry *) ind;
11075 /* Any absolute non-dynamic relocations against an indirect or weak
11076 definition will be against the target symbol. */
11077 if (indmips->has_static_relocs)
11078 dirmips->has_static_relocs = TRUE;
11080 if (ind->root.type != bfd_link_hash_indirect)
11081 return;
11083 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
11084 if (indmips->readonly_reloc)
11085 dirmips->readonly_reloc = TRUE;
11086 if (indmips->no_fn_stub)
11087 dirmips->no_fn_stub = TRUE;
11088 if (indmips->fn_stub)
11090 dirmips->fn_stub = indmips->fn_stub;
11091 indmips->fn_stub = NULL;
11093 if (indmips->need_fn_stub)
11095 dirmips->need_fn_stub = TRUE;
11096 indmips->need_fn_stub = FALSE;
11098 if (indmips->call_stub)
11100 dirmips->call_stub = indmips->call_stub;
11101 indmips->call_stub = NULL;
11103 if (indmips->call_fp_stub)
11105 dirmips->call_fp_stub = indmips->call_fp_stub;
11106 indmips->call_fp_stub = NULL;
11108 if (indmips->global_got_area < dirmips->global_got_area)
11109 dirmips->global_got_area = indmips->global_got_area;
11110 if (indmips->global_got_area < GGA_NONE)
11111 indmips->global_got_area = GGA_NONE;
11112 if (indmips->has_nonpic_branches)
11113 dirmips->has_nonpic_branches = TRUE;
11115 if (dirmips->tls_type == 0)
11116 dirmips->tls_type = indmips->tls_type;
11119 #define PDR_SIZE 32
11121 bfd_boolean
11122 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
11123 struct bfd_link_info *info)
11125 asection *o;
11126 bfd_boolean ret = FALSE;
11127 unsigned char *tdata;
11128 size_t i, skip;
11130 o = bfd_get_section_by_name (abfd, ".pdr");
11131 if (! o)
11132 return FALSE;
11133 if (o->size == 0)
11134 return FALSE;
11135 if (o->size % PDR_SIZE != 0)
11136 return FALSE;
11137 if (o->output_section != NULL
11138 && bfd_is_abs_section (o->output_section))
11139 return FALSE;
11141 tdata = bfd_zmalloc (o->size / PDR_SIZE);
11142 if (! tdata)
11143 return FALSE;
11145 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
11146 info->keep_memory);
11147 if (!cookie->rels)
11149 free (tdata);
11150 return FALSE;
11153 cookie->rel = cookie->rels;
11154 cookie->relend = cookie->rels + o->reloc_count;
11156 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
11158 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
11160 tdata[i] = 1;
11161 skip ++;
11165 if (skip != 0)
11167 mips_elf_section_data (o)->u.tdata = tdata;
11168 o->size -= skip * PDR_SIZE;
11169 ret = TRUE;
11171 else
11172 free (tdata);
11174 if (! info->keep_memory)
11175 free (cookie->rels);
11177 return ret;
11180 bfd_boolean
11181 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
11183 if (strcmp (sec->name, ".pdr") == 0)
11184 return TRUE;
11185 return FALSE;
11188 bfd_boolean
11189 _bfd_mips_elf_write_section (bfd *output_bfd,
11190 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
11191 asection *sec, bfd_byte *contents)
11193 bfd_byte *to, *from, *end;
11194 int i;
11196 if (strcmp (sec->name, ".pdr") != 0)
11197 return FALSE;
11199 if (mips_elf_section_data (sec)->u.tdata == NULL)
11200 return FALSE;
11202 to = contents;
11203 end = contents + sec->size;
11204 for (from = contents, i = 0;
11205 from < end;
11206 from += PDR_SIZE, i++)
11208 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
11209 continue;
11210 if (to != from)
11211 memcpy (to, from, PDR_SIZE);
11212 to += PDR_SIZE;
11214 bfd_set_section_contents (output_bfd, sec->output_section, contents,
11215 sec->output_offset, sec->size);
11216 return TRUE;
11219 /* MIPS ELF uses a special find_nearest_line routine in order the
11220 handle the ECOFF debugging information. */
11222 struct mips_elf_find_line
11224 struct ecoff_debug_info d;
11225 struct ecoff_find_line i;
11228 bfd_boolean
11229 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
11230 asymbol **symbols, bfd_vma offset,
11231 const char **filename_ptr,
11232 const char **functionname_ptr,
11233 unsigned int *line_ptr)
11235 asection *msec;
11237 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
11238 filename_ptr, functionname_ptr,
11239 line_ptr))
11240 return TRUE;
11242 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
11243 filename_ptr, functionname_ptr,
11244 line_ptr, ABI_64_P (abfd) ? 8 : 0,
11245 &elf_tdata (abfd)->dwarf2_find_line_info))
11246 return TRUE;
11248 msec = bfd_get_section_by_name (abfd, ".mdebug");
11249 if (msec != NULL)
11251 flagword origflags;
11252 struct mips_elf_find_line *fi;
11253 const struct ecoff_debug_swap * const swap =
11254 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
11256 /* If we are called during a link, mips_elf_final_link may have
11257 cleared the SEC_HAS_CONTENTS field. We force it back on here
11258 if appropriate (which it normally will be). */
11259 origflags = msec->flags;
11260 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
11261 msec->flags |= SEC_HAS_CONTENTS;
11263 fi = elf_tdata (abfd)->find_line_info;
11264 if (fi == NULL)
11266 bfd_size_type external_fdr_size;
11267 char *fraw_src;
11268 char *fraw_end;
11269 struct fdr *fdr_ptr;
11270 bfd_size_type amt = sizeof (struct mips_elf_find_line);
11272 fi = bfd_zalloc (abfd, amt);
11273 if (fi == NULL)
11275 msec->flags = origflags;
11276 return FALSE;
11279 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
11281 msec->flags = origflags;
11282 return FALSE;
11285 /* Swap in the FDR information. */
11286 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
11287 fi->d.fdr = bfd_alloc (abfd, amt);
11288 if (fi->d.fdr == NULL)
11290 msec->flags = origflags;
11291 return FALSE;
11293 external_fdr_size = swap->external_fdr_size;
11294 fdr_ptr = fi->d.fdr;
11295 fraw_src = (char *) fi->d.external_fdr;
11296 fraw_end = (fraw_src
11297 + fi->d.symbolic_header.ifdMax * external_fdr_size);
11298 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
11299 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
11301 elf_tdata (abfd)->find_line_info = fi;
11303 /* Note that we don't bother to ever free this information.
11304 find_nearest_line is either called all the time, as in
11305 objdump -l, so the information should be saved, or it is
11306 rarely called, as in ld error messages, so the memory
11307 wasted is unimportant. Still, it would probably be a
11308 good idea for free_cached_info to throw it away. */
11311 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
11312 &fi->i, filename_ptr, functionname_ptr,
11313 line_ptr))
11315 msec->flags = origflags;
11316 return TRUE;
11319 msec->flags = origflags;
11322 /* Fall back on the generic ELF find_nearest_line routine. */
11324 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
11325 filename_ptr, functionname_ptr,
11326 line_ptr);
11329 bfd_boolean
11330 _bfd_mips_elf_find_inliner_info (bfd *abfd,
11331 const char **filename_ptr,
11332 const char **functionname_ptr,
11333 unsigned int *line_ptr)
11335 bfd_boolean found;
11336 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
11337 functionname_ptr, line_ptr,
11338 & elf_tdata (abfd)->dwarf2_find_line_info);
11339 return found;
11343 /* When are writing out the .options or .MIPS.options section,
11344 remember the bytes we are writing out, so that we can install the
11345 GP value in the section_processing routine. */
11347 bfd_boolean
11348 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
11349 const void *location,
11350 file_ptr offset, bfd_size_type count)
11352 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
11354 bfd_byte *c;
11356 if (elf_section_data (section) == NULL)
11358 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
11359 section->used_by_bfd = bfd_zalloc (abfd, amt);
11360 if (elf_section_data (section) == NULL)
11361 return FALSE;
11363 c = mips_elf_section_data (section)->u.tdata;
11364 if (c == NULL)
11366 c = bfd_zalloc (abfd, section->size);
11367 if (c == NULL)
11368 return FALSE;
11369 mips_elf_section_data (section)->u.tdata = c;
11372 memcpy (c + offset, location, count);
11375 return _bfd_elf_set_section_contents (abfd, section, location, offset,
11376 count);
11379 /* This is almost identical to bfd_generic_get_... except that some
11380 MIPS relocations need to be handled specially. Sigh. */
11382 bfd_byte *
11383 _bfd_elf_mips_get_relocated_section_contents
11384 (bfd *abfd,
11385 struct bfd_link_info *link_info,
11386 struct bfd_link_order *link_order,
11387 bfd_byte *data,
11388 bfd_boolean relocatable,
11389 asymbol **symbols)
11391 /* Get enough memory to hold the stuff */
11392 bfd *input_bfd = link_order->u.indirect.section->owner;
11393 asection *input_section = link_order->u.indirect.section;
11394 bfd_size_type sz;
11396 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
11397 arelent **reloc_vector = NULL;
11398 long reloc_count;
11400 if (reloc_size < 0)
11401 goto error_return;
11403 reloc_vector = bfd_malloc (reloc_size);
11404 if (reloc_vector == NULL && reloc_size != 0)
11405 goto error_return;
11407 /* read in the section */
11408 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
11409 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
11410 goto error_return;
11412 reloc_count = bfd_canonicalize_reloc (input_bfd,
11413 input_section,
11414 reloc_vector,
11415 symbols);
11416 if (reloc_count < 0)
11417 goto error_return;
11419 if (reloc_count > 0)
11421 arelent **parent;
11422 /* for mips */
11423 int gp_found;
11424 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
11427 struct bfd_hash_entry *h;
11428 struct bfd_link_hash_entry *lh;
11429 /* Skip all this stuff if we aren't mixing formats. */
11430 if (abfd && input_bfd
11431 && abfd->xvec == input_bfd->xvec)
11432 lh = 0;
11433 else
11435 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
11436 lh = (struct bfd_link_hash_entry *) h;
11438 lookup:
11439 if (lh)
11441 switch (lh->type)
11443 case bfd_link_hash_undefined:
11444 case bfd_link_hash_undefweak:
11445 case bfd_link_hash_common:
11446 gp_found = 0;
11447 break;
11448 case bfd_link_hash_defined:
11449 case bfd_link_hash_defweak:
11450 gp_found = 1;
11451 gp = lh->u.def.value;
11452 break;
11453 case bfd_link_hash_indirect:
11454 case bfd_link_hash_warning:
11455 lh = lh->u.i.link;
11456 /* @@FIXME ignoring warning for now */
11457 goto lookup;
11458 case bfd_link_hash_new:
11459 default:
11460 abort ();
11463 else
11464 gp_found = 0;
11466 /* end mips */
11467 for (parent = reloc_vector; *parent != NULL; parent++)
11469 char *error_message = NULL;
11470 bfd_reloc_status_type r;
11472 /* Specific to MIPS: Deal with relocation types that require
11473 knowing the gp of the output bfd. */
11474 asymbol *sym = *(*parent)->sym_ptr_ptr;
11476 /* If we've managed to find the gp and have a special
11477 function for the relocation then go ahead, else default
11478 to the generic handling. */
11479 if (gp_found
11480 && (*parent)->howto->special_function
11481 == _bfd_mips_elf32_gprel16_reloc)
11482 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
11483 input_section, relocatable,
11484 data, gp);
11485 else
11486 r = bfd_perform_relocation (input_bfd, *parent, data,
11487 input_section,
11488 relocatable ? abfd : NULL,
11489 &error_message);
11491 if (relocatable)
11493 asection *os = input_section->output_section;
11495 /* A partial link, so keep the relocs */
11496 os->orelocation[os->reloc_count] = *parent;
11497 os->reloc_count++;
11500 if (r != bfd_reloc_ok)
11502 switch (r)
11504 case bfd_reloc_undefined:
11505 if (!((*link_info->callbacks->undefined_symbol)
11506 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11507 input_bfd, input_section, (*parent)->address, TRUE)))
11508 goto error_return;
11509 break;
11510 case bfd_reloc_dangerous:
11511 BFD_ASSERT (error_message != NULL);
11512 if (!((*link_info->callbacks->reloc_dangerous)
11513 (link_info, error_message, input_bfd, input_section,
11514 (*parent)->address)))
11515 goto error_return;
11516 break;
11517 case bfd_reloc_overflow:
11518 if (!((*link_info->callbacks->reloc_overflow)
11519 (link_info, NULL,
11520 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
11521 (*parent)->howto->name, (*parent)->addend,
11522 input_bfd, input_section, (*parent)->address)))
11523 goto error_return;
11524 break;
11525 case bfd_reloc_outofrange:
11526 default:
11527 abort ();
11528 break;
11534 if (reloc_vector != NULL)
11535 free (reloc_vector);
11536 return data;
11538 error_return:
11539 if (reloc_vector != NULL)
11540 free (reloc_vector);
11541 return NULL;
11544 /* Create a MIPS ELF linker hash table. */
11546 struct bfd_link_hash_table *
11547 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
11549 struct mips_elf_link_hash_table *ret;
11550 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
11552 ret = bfd_malloc (amt);
11553 if (ret == NULL)
11554 return NULL;
11556 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
11557 mips_elf_link_hash_newfunc,
11558 sizeof (struct mips_elf_link_hash_entry),
11559 MIPS_ELF_DATA))
11561 free (ret);
11562 return NULL;
11565 #if 0
11566 /* We no longer use this. */
11567 for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
11568 ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
11569 #endif
11570 ret->procedure_count = 0;
11571 ret->compact_rel_size = 0;
11572 ret->use_rld_obj_head = FALSE;
11573 ret->rld_value = 0;
11574 ret->mips16_stubs_seen = FALSE;
11575 ret->use_plts_and_copy_relocs = FALSE;
11576 ret->is_vxworks = FALSE;
11577 ret->small_data_overflow_reported = FALSE;
11578 ret->srelbss = NULL;
11579 ret->sdynbss = NULL;
11580 ret->srelplt = NULL;
11581 ret->srelplt2 = NULL;
11582 ret->sgotplt = NULL;
11583 ret->splt = NULL;
11584 ret->sstubs = NULL;
11585 ret->sgot = NULL;
11586 ret->got_info = NULL;
11587 ret->plt_header_size = 0;
11588 ret->plt_entry_size = 0;
11589 ret->lazy_stub_count = 0;
11590 ret->function_stub_size = 0;
11591 ret->strampoline = NULL;
11592 ret->la25_stubs = NULL;
11593 ret->add_stub_section = NULL;
11595 return &ret->root.root;
11598 /* Likewise, but indicate that the target is VxWorks. */
11600 struct bfd_link_hash_table *
11601 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
11603 struct bfd_link_hash_table *ret;
11605 ret = _bfd_mips_elf_link_hash_table_create (abfd);
11606 if (ret)
11608 struct mips_elf_link_hash_table *htab;
11610 htab = (struct mips_elf_link_hash_table *) ret;
11611 htab->use_plts_and_copy_relocs = TRUE;
11612 htab->is_vxworks = TRUE;
11614 return ret;
11617 /* A function that the linker calls if we are allowed to use PLTs
11618 and copy relocs. */
11620 void
11621 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
11623 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
11626 /* We need to use a special link routine to handle the .reginfo and
11627 the .mdebug sections. We need to merge all instances of these
11628 sections together, not write them all out sequentially. */
11630 bfd_boolean
11631 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11633 asection *o;
11634 struct bfd_link_order *p;
11635 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
11636 asection *rtproc_sec;
11637 Elf32_RegInfo reginfo;
11638 struct ecoff_debug_info debug;
11639 struct mips_htab_traverse_info hti;
11640 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11641 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
11642 HDRR *symhdr = &debug.symbolic_header;
11643 void *mdebug_handle = NULL;
11644 asection *s;
11645 EXTR esym;
11646 unsigned int i;
11647 bfd_size_type amt;
11648 struct mips_elf_link_hash_table *htab;
11650 static const char * const secname[] =
11652 ".text", ".init", ".fini", ".data",
11653 ".rodata", ".sdata", ".sbss", ".bss"
11655 static const int sc[] =
11657 scText, scInit, scFini, scData,
11658 scRData, scSData, scSBss, scBss
11661 /* Sort the dynamic symbols so that those with GOT entries come after
11662 those without. */
11663 htab = mips_elf_hash_table (info);
11664 BFD_ASSERT (htab != NULL);
11666 if (!mips_elf_sort_hash_table (abfd, info))
11667 return FALSE;
11669 /* Create any scheduled LA25 stubs. */
11670 hti.info = info;
11671 hti.output_bfd = abfd;
11672 hti.error = FALSE;
11673 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
11674 if (hti.error)
11675 return FALSE;
11677 /* Get a value for the GP register. */
11678 if (elf_gp (abfd) == 0)
11680 struct bfd_link_hash_entry *h;
11682 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
11683 if (h != NULL && h->type == bfd_link_hash_defined)
11684 elf_gp (abfd) = (h->u.def.value
11685 + h->u.def.section->output_section->vma
11686 + h->u.def.section->output_offset);
11687 else if (htab->is_vxworks
11688 && (h = bfd_link_hash_lookup (info->hash,
11689 "_GLOBAL_OFFSET_TABLE_",
11690 FALSE, FALSE, TRUE))
11691 && h->type == bfd_link_hash_defined)
11692 elf_gp (abfd) = (h->u.def.section->output_section->vma
11693 + h->u.def.section->output_offset
11694 + h->u.def.value);
11695 else if (info->relocatable)
11697 bfd_vma lo = MINUS_ONE;
11699 /* Find the GP-relative section with the lowest offset. */
11700 for (o = abfd->sections; o != NULL; o = o->next)
11701 if (o->vma < lo
11702 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
11703 lo = o->vma;
11705 /* And calculate GP relative to that. */
11706 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
11708 else
11710 /* If the relocate_section function needs to do a reloc
11711 involving the GP value, it should make a reloc_dangerous
11712 callback to warn that GP is not defined. */
11716 /* Go through the sections and collect the .reginfo and .mdebug
11717 information. */
11718 reginfo_sec = NULL;
11719 mdebug_sec = NULL;
11720 gptab_data_sec = NULL;
11721 gptab_bss_sec = NULL;
11722 for (o = abfd->sections; o != NULL; o = o->next)
11724 if (strcmp (o->name, ".reginfo") == 0)
11726 memset (&reginfo, 0, sizeof reginfo);
11728 /* We have found the .reginfo section in the output file.
11729 Look through all the link_orders comprising it and merge
11730 the information together. */
11731 for (p = o->map_head.link_order; p != NULL; p = p->next)
11733 asection *input_section;
11734 bfd *input_bfd;
11735 Elf32_External_RegInfo ext;
11736 Elf32_RegInfo sub;
11738 if (p->type != bfd_indirect_link_order)
11740 if (p->type == bfd_data_link_order)
11741 continue;
11742 abort ();
11745 input_section = p->u.indirect.section;
11746 input_bfd = input_section->owner;
11748 if (! bfd_get_section_contents (input_bfd, input_section,
11749 &ext, 0, sizeof ext))
11750 return FALSE;
11752 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
11754 reginfo.ri_gprmask |= sub.ri_gprmask;
11755 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
11756 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
11757 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
11758 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
11760 /* ri_gp_value is set by the function
11761 mips_elf32_section_processing when the section is
11762 finally written out. */
11764 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11765 elf_link_input_bfd ignores this section. */
11766 input_section->flags &= ~SEC_HAS_CONTENTS;
11769 /* Size has been set in _bfd_mips_elf_always_size_sections. */
11770 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
11772 /* Skip this section later on (I don't think this currently
11773 matters, but someday it might). */
11774 o->map_head.link_order = NULL;
11776 reginfo_sec = o;
11779 if (strcmp (o->name, ".mdebug") == 0)
11781 struct extsym_info einfo;
11782 bfd_vma last;
11784 /* We have found the .mdebug section in the output file.
11785 Look through all the link_orders comprising it and merge
11786 the information together. */
11787 symhdr->magic = swap->sym_magic;
11788 /* FIXME: What should the version stamp be? */
11789 symhdr->vstamp = 0;
11790 symhdr->ilineMax = 0;
11791 symhdr->cbLine = 0;
11792 symhdr->idnMax = 0;
11793 symhdr->ipdMax = 0;
11794 symhdr->isymMax = 0;
11795 symhdr->ioptMax = 0;
11796 symhdr->iauxMax = 0;
11797 symhdr->issMax = 0;
11798 symhdr->issExtMax = 0;
11799 symhdr->ifdMax = 0;
11800 symhdr->crfd = 0;
11801 symhdr->iextMax = 0;
11803 /* We accumulate the debugging information itself in the
11804 debug_info structure. */
11805 debug.line = NULL;
11806 debug.external_dnr = NULL;
11807 debug.external_pdr = NULL;
11808 debug.external_sym = NULL;
11809 debug.external_opt = NULL;
11810 debug.external_aux = NULL;
11811 debug.ss = NULL;
11812 debug.ssext = debug.ssext_end = NULL;
11813 debug.external_fdr = NULL;
11814 debug.external_rfd = NULL;
11815 debug.external_ext = debug.external_ext_end = NULL;
11817 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
11818 if (mdebug_handle == NULL)
11819 return FALSE;
11821 esym.jmptbl = 0;
11822 esym.cobol_main = 0;
11823 esym.weakext = 0;
11824 esym.reserved = 0;
11825 esym.ifd = ifdNil;
11826 esym.asym.iss = issNil;
11827 esym.asym.st = stLocal;
11828 esym.asym.reserved = 0;
11829 esym.asym.index = indexNil;
11830 last = 0;
11831 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
11833 esym.asym.sc = sc[i];
11834 s = bfd_get_section_by_name (abfd, secname[i]);
11835 if (s != NULL)
11837 esym.asym.value = s->vma;
11838 last = s->vma + s->size;
11840 else
11841 esym.asym.value = last;
11842 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
11843 secname[i], &esym))
11844 return FALSE;
11847 for (p = o->map_head.link_order; p != NULL; p = p->next)
11849 asection *input_section;
11850 bfd *input_bfd;
11851 const struct ecoff_debug_swap *input_swap;
11852 struct ecoff_debug_info input_debug;
11853 char *eraw_src;
11854 char *eraw_end;
11856 if (p->type != bfd_indirect_link_order)
11858 if (p->type == bfd_data_link_order)
11859 continue;
11860 abort ();
11863 input_section = p->u.indirect.section;
11864 input_bfd = input_section->owner;
11866 if (!is_mips_elf (input_bfd))
11868 /* I don't know what a non MIPS ELF bfd would be
11869 doing with a .mdebug section, but I don't really
11870 want to deal with it. */
11871 continue;
11874 input_swap = (get_elf_backend_data (input_bfd)
11875 ->elf_backend_ecoff_debug_swap);
11877 BFD_ASSERT (p->size == input_section->size);
11879 /* The ECOFF linking code expects that we have already
11880 read in the debugging information and set up an
11881 ecoff_debug_info structure, so we do that now. */
11882 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
11883 &input_debug))
11884 return FALSE;
11886 if (! (bfd_ecoff_debug_accumulate
11887 (mdebug_handle, abfd, &debug, swap, input_bfd,
11888 &input_debug, input_swap, info)))
11889 return FALSE;
11891 /* Loop through the external symbols. For each one with
11892 interesting information, try to find the symbol in
11893 the linker global hash table and save the information
11894 for the output external symbols. */
11895 eraw_src = input_debug.external_ext;
11896 eraw_end = (eraw_src
11897 + (input_debug.symbolic_header.iextMax
11898 * input_swap->external_ext_size));
11899 for (;
11900 eraw_src < eraw_end;
11901 eraw_src += input_swap->external_ext_size)
11903 EXTR ext;
11904 const char *name;
11905 struct mips_elf_link_hash_entry *h;
11907 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
11908 if (ext.asym.sc == scNil
11909 || ext.asym.sc == scUndefined
11910 || ext.asym.sc == scSUndefined)
11911 continue;
11913 name = input_debug.ssext + ext.asym.iss;
11914 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
11915 name, FALSE, FALSE, TRUE);
11916 if (h == NULL || h->esym.ifd != -2)
11917 continue;
11919 if (ext.ifd != -1)
11921 BFD_ASSERT (ext.ifd
11922 < input_debug.symbolic_header.ifdMax);
11923 ext.ifd = input_debug.ifdmap[ext.ifd];
11926 h->esym = ext;
11929 /* Free up the information we just read. */
11930 free (input_debug.line);
11931 free (input_debug.external_dnr);
11932 free (input_debug.external_pdr);
11933 free (input_debug.external_sym);
11934 free (input_debug.external_opt);
11935 free (input_debug.external_aux);
11936 free (input_debug.ss);
11937 free (input_debug.ssext);
11938 free (input_debug.external_fdr);
11939 free (input_debug.external_rfd);
11940 free (input_debug.external_ext);
11942 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11943 elf_link_input_bfd ignores this section. */
11944 input_section->flags &= ~SEC_HAS_CONTENTS;
11947 if (SGI_COMPAT (abfd) && info->shared)
11949 /* Create .rtproc section. */
11950 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
11951 if (rtproc_sec == NULL)
11953 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
11954 | SEC_LINKER_CREATED | SEC_READONLY);
11956 rtproc_sec = bfd_make_section_with_flags (abfd,
11957 ".rtproc",
11958 flags);
11959 if (rtproc_sec == NULL
11960 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
11961 return FALSE;
11964 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
11965 info, rtproc_sec,
11966 &debug))
11967 return FALSE;
11970 /* Build the external symbol information. */
11971 einfo.abfd = abfd;
11972 einfo.info = info;
11973 einfo.debug = &debug;
11974 einfo.swap = swap;
11975 einfo.failed = FALSE;
11976 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
11977 mips_elf_output_extsym, &einfo);
11978 if (einfo.failed)
11979 return FALSE;
11981 /* Set the size of the .mdebug section. */
11982 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
11984 /* Skip this section later on (I don't think this currently
11985 matters, but someday it might). */
11986 o->map_head.link_order = NULL;
11988 mdebug_sec = o;
11991 if (CONST_STRNEQ (o->name, ".gptab."))
11993 const char *subname;
11994 unsigned int c;
11995 Elf32_gptab *tab;
11996 Elf32_External_gptab *ext_tab;
11997 unsigned int j;
11999 /* The .gptab.sdata and .gptab.sbss sections hold
12000 information describing how the small data area would
12001 change depending upon the -G switch. These sections
12002 not used in executables files. */
12003 if (! info->relocatable)
12005 for (p = o->map_head.link_order; p != NULL; p = p->next)
12007 asection *input_section;
12009 if (p->type != bfd_indirect_link_order)
12011 if (p->type == bfd_data_link_order)
12012 continue;
12013 abort ();
12016 input_section = p->u.indirect.section;
12018 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12019 elf_link_input_bfd ignores this section. */
12020 input_section->flags &= ~SEC_HAS_CONTENTS;
12023 /* Skip this section later on (I don't think this
12024 currently matters, but someday it might). */
12025 o->map_head.link_order = NULL;
12027 /* Really remove the section. */
12028 bfd_section_list_remove (abfd, o);
12029 --abfd->section_count;
12031 continue;
12034 /* There is one gptab for initialized data, and one for
12035 uninitialized data. */
12036 if (strcmp (o->name, ".gptab.sdata") == 0)
12037 gptab_data_sec = o;
12038 else if (strcmp (o->name, ".gptab.sbss") == 0)
12039 gptab_bss_sec = o;
12040 else
12042 (*_bfd_error_handler)
12043 (_("%s: illegal section name `%s'"),
12044 bfd_get_filename (abfd), o->name);
12045 bfd_set_error (bfd_error_nonrepresentable_section);
12046 return FALSE;
12049 /* The linker script always combines .gptab.data and
12050 .gptab.sdata into .gptab.sdata, and likewise for
12051 .gptab.bss and .gptab.sbss. It is possible that there is
12052 no .sdata or .sbss section in the output file, in which
12053 case we must change the name of the output section. */
12054 subname = o->name + sizeof ".gptab" - 1;
12055 if (bfd_get_section_by_name (abfd, subname) == NULL)
12057 if (o == gptab_data_sec)
12058 o->name = ".gptab.data";
12059 else
12060 o->name = ".gptab.bss";
12061 subname = o->name + sizeof ".gptab" - 1;
12062 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
12065 /* Set up the first entry. */
12066 c = 1;
12067 amt = c * sizeof (Elf32_gptab);
12068 tab = bfd_malloc (amt);
12069 if (tab == NULL)
12070 return FALSE;
12071 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
12072 tab[0].gt_header.gt_unused = 0;
12074 /* Combine the input sections. */
12075 for (p = o->map_head.link_order; p != NULL; p = p->next)
12077 asection *input_section;
12078 bfd *input_bfd;
12079 bfd_size_type size;
12080 unsigned long last;
12081 bfd_size_type gpentry;
12083 if (p->type != bfd_indirect_link_order)
12085 if (p->type == bfd_data_link_order)
12086 continue;
12087 abort ();
12090 input_section = p->u.indirect.section;
12091 input_bfd = input_section->owner;
12093 /* Combine the gptab entries for this input section one
12094 by one. We know that the input gptab entries are
12095 sorted by ascending -G value. */
12096 size = input_section->size;
12097 last = 0;
12098 for (gpentry = sizeof (Elf32_External_gptab);
12099 gpentry < size;
12100 gpentry += sizeof (Elf32_External_gptab))
12102 Elf32_External_gptab ext_gptab;
12103 Elf32_gptab int_gptab;
12104 unsigned long val;
12105 unsigned long add;
12106 bfd_boolean exact;
12107 unsigned int look;
12109 if (! (bfd_get_section_contents
12110 (input_bfd, input_section, &ext_gptab, gpentry,
12111 sizeof (Elf32_External_gptab))))
12113 free (tab);
12114 return FALSE;
12117 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
12118 &int_gptab);
12119 val = int_gptab.gt_entry.gt_g_value;
12120 add = int_gptab.gt_entry.gt_bytes - last;
12122 exact = FALSE;
12123 for (look = 1; look < c; look++)
12125 if (tab[look].gt_entry.gt_g_value >= val)
12126 tab[look].gt_entry.gt_bytes += add;
12128 if (tab[look].gt_entry.gt_g_value == val)
12129 exact = TRUE;
12132 if (! exact)
12134 Elf32_gptab *new_tab;
12135 unsigned int max;
12137 /* We need a new table entry. */
12138 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
12139 new_tab = bfd_realloc (tab, amt);
12140 if (new_tab == NULL)
12142 free (tab);
12143 return FALSE;
12145 tab = new_tab;
12146 tab[c].gt_entry.gt_g_value = val;
12147 tab[c].gt_entry.gt_bytes = add;
12149 /* Merge in the size for the next smallest -G
12150 value, since that will be implied by this new
12151 value. */
12152 max = 0;
12153 for (look = 1; look < c; look++)
12155 if (tab[look].gt_entry.gt_g_value < val
12156 && (max == 0
12157 || (tab[look].gt_entry.gt_g_value
12158 > tab[max].gt_entry.gt_g_value)))
12159 max = look;
12161 if (max != 0)
12162 tab[c].gt_entry.gt_bytes +=
12163 tab[max].gt_entry.gt_bytes;
12165 ++c;
12168 last = int_gptab.gt_entry.gt_bytes;
12171 /* Hack: reset the SEC_HAS_CONTENTS flag so that
12172 elf_link_input_bfd ignores this section. */
12173 input_section->flags &= ~SEC_HAS_CONTENTS;
12176 /* The table must be sorted by -G value. */
12177 if (c > 2)
12178 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
12180 /* Swap out the table. */
12181 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
12182 ext_tab = bfd_alloc (abfd, amt);
12183 if (ext_tab == NULL)
12185 free (tab);
12186 return FALSE;
12189 for (j = 0; j < c; j++)
12190 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
12191 free (tab);
12193 o->size = c * sizeof (Elf32_External_gptab);
12194 o->contents = (bfd_byte *) ext_tab;
12196 /* Skip this section later on (I don't think this currently
12197 matters, but someday it might). */
12198 o->map_head.link_order = NULL;
12202 /* Invoke the regular ELF backend linker to do all the work. */
12203 if (!bfd_elf_final_link (abfd, info))
12204 return FALSE;
12206 /* Now write out the computed sections. */
12208 if (reginfo_sec != NULL)
12210 Elf32_External_RegInfo ext;
12212 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
12213 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
12214 return FALSE;
12217 if (mdebug_sec != NULL)
12219 BFD_ASSERT (abfd->output_has_begun);
12220 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
12221 swap, info,
12222 mdebug_sec->filepos))
12223 return FALSE;
12225 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
12228 if (gptab_data_sec != NULL)
12230 if (! bfd_set_section_contents (abfd, gptab_data_sec,
12231 gptab_data_sec->contents,
12232 0, gptab_data_sec->size))
12233 return FALSE;
12236 if (gptab_bss_sec != NULL)
12238 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
12239 gptab_bss_sec->contents,
12240 0, gptab_bss_sec->size))
12241 return FALSE;
12244 if (SGI_COMPAT (abfd))
12246 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
12247 if (rtproc_sec != NULL)
12249 if (! bfd_set_section_contents (abfd, rtproc_sec,
12250 rtproc_sec->contents,
12251 0, rtproc_sec->size))
12252 return FALSE;
12256 return TRUE;
12259 /* Structure for saying that BFD machine EXTENSION extends BASE. */
12261 struct mips_mach_extension {
12262 unsigned long extension, base;
12266 /* An array describing how BFD machines relate to one another. The entries
12267 are ordered topologically with MIPS I extensions listed last. */
12269 static const struct mips_mach_extension mips_mach_extensions[] = {
12270 /* MIPS64r2 extensions. */
12271 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
12273 /* MIPS64 extensions. */
12274 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
12275 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
12276 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
12277 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
12279 /* MIPS V extensions. */
12280 { bfd_mach_mipsisa64, bfd_mach_mips5 },
12282 /* R10000 extensions. */
12283 { bfd_mach_mips12000, bfd_mach_mips10000 },
12284 { bfd_mach_mips14000, bfd_mach_mips10000 },
12285 { bfd_mach_mips16000, bfd_mach_mips10000 },
12287 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
12288 vr5400 ISA, but doesn't include the multimedia stuff. It seems
12289 better to allow vr5400 and vr5500 code to be merged anyway, since
12290 many libraries will just use the core ISA. Perhaps we could add
12291 some sort of ASE flag if this ever proves a problem. */
12292 { bfd_mach_mips5500, bfd_mach_mips5400 },
12293 { bfd_mach_mips5400, bfd_mach_mips5000 },
12295 /* MIPS IV extensions. */
12296 { bfd_mach_mips5, bfd_mach_mips8000 },
12297 { bfd_mach_mips10000, bfd_mach_mips8000 },
12298 { bfd_mach_mips5000, bfd_mach_mips8000 },
12299 { bfd_mach_mips7000, bfd_mach_mips8000 },
12300 { bfd_mach_mips9000, bfd_mach_mips8000 },
12302 /* VR4100 extensions. */
12303 { bfd_mach_mips4120, bfd_mach_mips4100 },
12304 { bfd_mach_mips4111, bfd_mach_mips4100 },
12306 /* MIPS III extensions. */
12307 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
12308 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
12309 { bfd_mach_mips8000, bfd_mach_mips4000 },
12310 { bfd_mach_mips4650, bfd_mach_mips4000 },
12311 { bfd_mach_mips4600, bfd_mach_mips4000 },
12312 { bfd_mach_mips4400, bfd_mach_mips4000 },
12313 { bfd_mach_mips4300, bfd_mach_mips4000 },
12314 { bfd_mach_mips4100, bfd_mach_mips4000 },
12315 { bfd_mach_mips4010, bfd_mach_mips4000 },
12317 /* MIPS32 extensions. */
12318 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
12320 /* MIPS II extensions. */
12321 { bfd_mach_mips4000, bfd_mach_mips6000 },
12322 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
12324 /* MIPS I extensions. */
12325 { bfd_mach_mips6000, bfd_mach_mips3000 },
12326 { bfd_mach_mips3900, bfd_mach_mips3000 }
12330 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
12332 static bfd_boolean
12333 mips_mach_extends_p (unsigned long base, unsigned long extension)
12335 size_t i;
12337 if (extension == base)
12338 return TRUE;
12340 if (base == bfd_mach_mipsisa32
12341 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
12342 return TRUE;
12344 if (base == bfd_mach_mipsisa32r2
12345 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
12346 return TRUE;
12348 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
12349 if (extension == mips_mach_extensions[i].extension)
12351 extension = mips_mach_extensions[i].base;
12352 if (extension == base)
12353 return TRUE;
12356 return FALSE;
12360 /* Return true if the given ELF header flags describe a 32-bit binary. */
12362 static bfd_boolean
12363 mips_32bit_flags_p (flagword flags)
12365 return ((flags & EF_MIPS_32BITMODE) != 0
12366 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
12367 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
12368 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
12369 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
12370 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
12371 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
12375 /* Merge object attributes from IBFD into OBFD. Raise an error if
12376 there are conflicting attributes. */
12377 static bfd_boolean
12378 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
12380 obj_attribute *in_attr;
12381 obj_attribute *out_attr;
12383 if (!elf_known_obj_attributes_proc (obfd)[0].i)
12385 /* This is the first object. Copy the attributes. */
12386 _bfd_elf_copy_obj_attributes (ibfd, obfd);
12388 /* Use the Tag_null value to indicate the attributes have been
12389 initialized. */
12390 elf_known_obj_attributes_proc (obfd)[0].i = 1;
12392 return TRUE;
12395 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
12396 non-conflicting ones. */
12397 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
12398 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
12399 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
12401 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
12402 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12403 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
12404 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
12406 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12407 _bfd_error_handler
12408 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
12409 in_attr[Tag_GNU_MIPS_ABI_FP].i);
12410 else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
12411 _bfd_error_handler
12412 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
12413 out_attr[Tag_GNU_MIPS_ABI_FP].i);
12414 else
12415 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
12417 case 1:
12418 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12420 case 2:
12421 _bfd_error_handler
12422 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12423 obfd, ibfd);
12424 break;
12426 case 3:
12427 _bfd_error_handler
12428 (_("Warning: %B uses hard float, %B uses soft float"),
12429 obfd, ibfd);
12430 break;
12432 case 4:
12433 _bfd_error_handler
12434 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12435 obfd, ibfd);
12436 break;
12438 default:
12439 abort ();
12441 break;
12443 case 2:
12444 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12446 case 1:
12447 _bfd_error_handler
12448 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
12449 ibfd, obfd);
12450 break;
12452 case 3:
12453 _bfd_error_handler
12454 (_("Warning: %B uses hard float, %B uses soft float"),
12455 obfd, ibfd);
12456 break;
12458 case 4:
12459 _bfd_error_handler
12460 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12461 obfd, ibfd);
12462 break;
12464 default:
12465 abort ();
12467 break;
12469 case 3:
12470 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12472 case 1:
12473 case 2:
12474 case 4:
12475 _bfd_error_handler
12476 (_("Warning: %B uses hard float, %B uses soft float"),
12477 ibfd, obfd);
12478 break;
12480 default:
12481 abort ();
12483 break;
12485 case 4:
12486 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
12488 case 1:
12489 _bfd_error_handler
12490 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
12491 ibfd, obfd);
12492 break;
12494 case 2:
12495 _bfd_error_handler
12496 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
12497 ibfd, obfd);
12498 break;
12500 case 3:
12501 _bfd_error_handler
12502 (_("Warning: %B uses hard float, %B uses soft float"),
12503 obfd, ibfd);
12504 break;
12506 default:
12507 abort ();
12509 break;
12511 default:
12512 abort ();
12516 /* Merge Tag_compatibility attributes and any common GNU ones. */
12517 _bfd_elf_merge_object_attributes (ibfd, obfd);
12519 return TRUE;
12522 /* Merge backend specific data from an object file to the output
12523 object file when linking. */
12525 bfd_boolean
12526 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
12528 flagword old_flags;
12529 flagword new_flags;
12530 bfd_boolean ok;
12531 bfd_boolean null_input_bfd = TRUE;
12532 asection *sec;
12534 /* Check if we have the same endianness. */
12535 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
12537 (*_bfd_error_handler)
12538 (_("%B: endianness incompatible with that of the selected emulation"),
12539 ibfd);
12540 return FALSE;
12543 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
12544 return TRUE;
12546 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
12548 (*_bfd_error_handler)
12549 (_("%B: ABI is incompatible with that of the selected emulation"),
12550 ibfd);
12551 return FALSE;
12554 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
12555 return FALSE;
12557 new_flags = elf_elfheader (ibfd)->e_flags;
12558 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
12559 old_flags = elf_elfheader (obfd)->e_flags;
12561 if (! elf_flags_init (obfd))
12563 elf_flags_init (obfd) = TRUE;
12564 elf_elfheader (obfd)->e_flags = new_flags;
12565 elf_elfheader (obfd)->e_ident[EI_CLASS]
12566 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
12568 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
12569 && (bfd_get_arch_info (obfd)->the_default
12570 || mips_mach_extends_p (bfd_get_mach (obfd),
12571 bfd_get_mach (ibfd))))
12573 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
12574 bfd_get_mach (ibfd)))
12575 return FALSE;
12578 return TRUE;
12581 /* Check flag compatibility. */
12583 new_flags &= ~EF_MIPS_NOREORDER;
12584 old_flags &= ~EF_MIPS_NOREORDER;
12586 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
12587 doesn't seem to matter. */
12588 new_flags &= ~EF_MIPS_XGOT;
12589 old_flags &= ~EF_MIPS_XGOT;
12591 /* MIPSpro generates ucode info in n64 objects. Again, we should
12592 just be able to ignore this. */
12593 new_flags &= ~EF_MIPS_UCODE;
12594 old_flags &= ~EF_MIPS_UCODE;
12596 /* DSOs should only be linked with CPIC code. */
12597 if ((ibfd->flags & DYNAMIC) != 0)
12598 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
12600 if (new_flags == old_flags)
12601 return TRUE;
12603 /* Check to see if the input BFD actually contains any sections.
12604 If not, its flags may not have been initialised either, but it cannot
12605 actually cause any incompatibility. */
12606 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
12608 /* Ignore synthetic sections and empty .text, .data and .bss sections
12609 which are automatically generated by gas. Also ignore fake
12610 (s)common sections, since merely defining a common symbol does
12611 not affect compatibility. */
12612 if ((sec->flags & SEC_IS_COMMON) == 0
12613 && strcmp (sec->name, ".reginfo")
12614 && strcmp (sec->name, ".mdebug")
12615 && (sec->size != 0
12616 || (strcmp (sec->name, ".text")
12617 && strcmp (sec->name, ".data")
12618 && strcmp (sec->name, ".bss"))))
12620 null_input_bfd = FALSE;
12621 break;
12624 if (null_input_bfd)
12625 return TRUE;
12627 ok = TRUE;
12629 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
12630 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
12632 (*_bfd_error_handler)
12633 (_("%B: warning: linking abicalls files with non-abicalls files"),
12634 ibfd);
12635 ok = TRUE;
12638 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
12639 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
12640 if (! (new_flags & EF_MIPS_PIC))
12641 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
12643 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12644 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
12646 /* Compare the ISAs. */
12647 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
12649 (*_bfd_error_handler)
12650 (_("%B: linking 32-bit code with 64-bit code"),
12651 ibfd);
12652 ok = FALSE;
12654 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
12656 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
12657 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
12659 /* Copy the architecture info from IBFD to OBFD. Also copy
12660 the 32-bit flag (if set) so that we continue to recognise
12661 OBFD as a 32-bit binary. */
12662 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
12663 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12664 elf_elfheader (obfd)->e_flags
12665 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12667 /* Copy across the ABI flags if OBFD doesn't use them
12668 and if that was what caused us to treat IBFD as 32-bit. */
12669 if ((old_flags & EF_MIPS_ABI) == 0
12670 && mips_32bit_flags_p (new_flags)
12671 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
12672 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
12674 else
12676 /* The ISAs aren't compatible. */
12677 (*_bfd_error_handler)
12678 (_("%B: linking %s module with previous %s modules"),
12679 ibfd,
12680 bfd_printable_name (ibfd),
12681 bfd_printable_name (obfd));
12682 ok = FALSE;
12686 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12687 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
12689 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
12690 does set EI_CLASS differently from any 32-bit ABI. */
12691 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
12692 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12693 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12695 /* Only error if both are set (to different values). */
12696 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
12697 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
12698 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
12700 (*_bfd_error_handler)
12701 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
12702 ibfd,
12703 elf_mips_abi_name (ibfd),
12704 elf_mips_abi_name (obfd));
12705 ok = FALSE;
12707 new_flags &= ~EF_MIPS_ABI;
12708 old_flags &= ~EF_MIPS_ABI;
12711 /* For now, allow arbitrary mixing of ASEs (retain the union). */
12712 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
12714 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
12716 new_flags &= ~ EF_MIPS_ARCH_ASE;
12717 old_flags &= ~ EF_MIPS_ARCH_ASE;
12720 /* Warn about any other mismatches */
12721 if (new_flags != old_flags)
12723 (*_bfd_error_handler)
12724 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
12725 ibfd, (unsigned long) new_flags,
12726 (unsigned long) old_flags);
12727 ok = FALSE;
12730 if (! ok)
12732 bfd_set_error (bfd_error_bad_value);
12733 return FALSE;
12736 return TRUE;
12739 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
12741 bfd_boolean
12742 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
12744 BFD_ASSERT (!elf_flags_init (abfd)
12745 || elf_elfheader (abfd)->e_flags == flags);
12747 elf_elfheader (abfd)->e_flags = flags;
12748 elf_flags_init (abfd) = TRUE;
12749 return TRUE;
12752 char *
12753 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
12755 switch (dtag)
12757 default: return "";
12758 case DT_MIPS_RLD_VERSION:
12759 return "MIPS_RLD_VERSION";
12760 case DT_MIPS_TIME_STAMP:
12761 return "MIPS_TIME_STAMP";
12762 case DT_MIPS_ICHECKSUM:
12763 return "MIPS_ICHECKSUM";
12764 case DT_MIPS_IVERSION:
12765 return "MIPS_IVERSION";
12766 case DT_MIPS_FLAGS:
12767 return "MIPS_FLAGS";
12768 case DT_MIPS_BASE_ADDRESS:
12769 return "MIPS_BASE_ADDRESS";
12770 case DT_MIPS_MSYM:
12771 return "MIPS_MSYM";
12772 case DT_MIPS_CONFLICT:
12773 return "MIPS_CONFLICT";
12774 case DT_MIPS_LIBLIST:
12775 return "MIPS_LIBLIST";
12776 case DT_MIPS_LOCAL_GOTNO:
12777 return "MIPS_LOCAL_GOTNO";
12778 case DT_MIPS_CONFLICTNO:
12779 return "MIPS_CONFLICTNO";
12780 case DT_MIPS_LIBLISTNO:
12781 return "MIPS_LIBLISTNO";
12782 case DT_MIPS_SYMTABNO:
12783 return "MIPS_SYMTABNO";
12784 case DT_MIPS_UNREFEXTNO:
12785 return "MIPS_UNREFEXTNO";
12786 case DT_MIPS_GOTSYM:
12787 return "MIPS_GOTSYM";
12788 case DT_MIPS_HIPAGENO:
12789 return "MIPS_HIPAGENO";
12790 case DT_MIPS_RLD_MAP:
12791 return "MIPS_RLD_MAP";
12792 case DT_MIPS_DELTA_CLASS:
12793 return "MIPS_DELTA_CLASS";
12794 case DT_MIPS_DELTA_CLASS_NO:
12795 return "MIPS_DELTA_CLASS_NO";
12796 case DT_MIPS_DELTA_INSTANCE:
12797 return "MIPS_DELTA_INSTANCE";
12798 case DT_MIPS_DELTA_INSTANCE_NO:
12799 return "MIPS_DELTA_INSTANCE_NO";
12800 case DT_MIPS_DELTA_RELOC:
12801 return "MIPS_DELTA_RELOC";
12802 case DT_MIPS_DELTA_RELOC_NO:
12803 return "MIPS_DELTA_RELOC_NO";
12804 case DT_MIPS_DELTA_SYM:
12805 return "MIPS_DELTA_SYM";
12806 case DT_MIPS_DELTA_SYM_NO:
12807 return "MIPS_DELTA_SYM_NO";
12808 case DT_MIPS_DELTA_CLASSSYM:
12809 return "MIPS_DELTA_CLASSSYM";
12810 case DT_MIPS_DELTA_CLASSSYM_NO:
12811 return "MIPS_DELTA_CLASSSYM_NO";
12812 case DT_MIPS_CXX_FLAGS:
12813 return "MIPS_CXX_FLAGS";
12814 case DT_MIPS_PIXIE_INIT:
12815 return "MIPS_PIXIE_INIT";
12816 case DT_MIPS_SYMBOL_LIB:
12817 return "MIPS_SYMBOL_LIB";
12818 case DT_MIPS_LOCALPAGE_GOTIDX:
12819 return "MIPS_LOCALPAGE_GOTIDX";
12820 case DT_MIPS_LOCAL_GOTIDX:
12821 return "MIPS_LOCAL_GOTIDX";
12822 case DT_MIPS_HIDDEN_GOTIDX:
12823 return "MIPS_HIDDEN_GOTIDX";
12824 case DT_MIPS_PROTECTED_GOTIDX:
12825 return "MIPS_PROTECTED_GOT_IDX";
12826 case DT_MIPS_OPTIONS:
12827 return "MIPS_OPTIONS";
12828 case DT_MIPS_INTERFACE:
12829 return "MIPS_INTERFACE";
12830 case DT_MIPS_DYNSTR_ALIGN:
12831 return "DT_MIPS_DYNSTR_ALIGN";
12832 case DT_MIPS_INTERFACE_SIZE:
12833 return "DT_MIPS_INTERFACE_SIZE";
12834 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
12835 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
12836 case DT_MIPS_PERF_SUFFIX:
12837 return "DT_MIPS_PERF_SUFFIX";
12838 case DT_MIPS_COMPACT_SIZE:
12839 return "DT_MIPS_COMPACT_SIZE";
12840 case DT_MIPS_GP_VALUE:
12841 return "DT_MIPS_GP_VALUE";
12842 case DT_MIPS_AUX_DYNAMIC:
12843 return "DT_MIPS_AUX_DYNAMIC";
12844 case DT_MIPS_PLTGOT:
12845 return "DT_MIPS_PLTGOT";
12846 case DT_MIPS_RWPLT:
12847 return "DT_MIPS_RWPLT";
12851 bfd_boolean
12852 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
12854 FILE *file = ptr;
12856 BFD_ASSERT (abfd != NULL && ptr != NULL);
12858 /* Print normal ELF private data. */
12859 _bfd_elf_print_private_bfd_data (abfd, ptr);
12861 /* xgettext:c-format */
12862 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
12864 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
12865 fprintf (file, _(" [abi=O32]"));
12866 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
12867 fprintf (file, _(" [abi=O64]"));
12868 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
12869 fprintf (file, _(" [abi=EABI32]"));
12870 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
12871 fprintf (file, _(" [abi=EABI64]"));
12872 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
12873 fprintf (file, _(" [abi unknown]"));
12874 else if (ABI_N32_P (abfd))
12875 fprintf (file, _(" [abi=N32]"));
12876 else if (ABI_64_P (abfd))
12877 fprintf (file, _(" [abi=64]"));
12878 else
12879 fprintf (file, _(" [no abi set]"));
12881 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
12882 fprintf (file, " [mips1]");
12883 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
12884 fprintf (file, " [mips2]");
12885 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
12886 fprintf (file, " [mips3]");
12887 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
12888 fprintf (file, " [mips4]");
12889 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
12890 fprintf (file, " [mips5]");
12891 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
12892 fprintf (file, " [mips32]");
12893 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
12894 fprintf (file, " [mips64]");
12895 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
12896 fprintf (file, " [mips32r2]");
12897 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
12898 fprintf (file, " [mips64r2]");
12899 else
12900 fprintf (file, _(" [unknown ISA]"));
12902 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
12903 fprintf (file, " [mdmx]");
12905 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
12906 fprintf (file, " [mips16]");
12908 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
12909 fprintf (file, " [32bitmode]");
12910 else
12911 fprintf (file, _(" [not 32bitmode]"));
12913 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
12914 fprintf (file, " [noreorder]");
12916 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
12917 fprintf (file, " [PIC]");
12919 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
12920 fprintf (file, " [CPIC]");
12922 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
12923 fprintf (file, " [XGOT]");
12925 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
12926 fprintf (file, " [UCODE]");
12928 fputc ('\n', file);
12930 return TRUE;
12933 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
12935 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12936 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12937 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
12938 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12939 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
12940 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
12941 { NULL, 0, 0, 0, 0 }
12944 /* Merge non visibility st_other attributes. Ensure that the
12945 STO_OPTIONAL flag is copied into h->other, even if this is not a
12946 definiton of the symbol. */
12947 void
12948 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
12949 const Elf_Internal_Sym *isym,
12950 bfd_boolean definition,
12951 bfd_boolean dynamic ATTRIBUTE_UNUSED)
12953 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
12955 unsigned char other;
12957 other = (definition ? isym->st_other : h->other);
12958 other &= ~ELF_ST_VISIBILITY (-1);
12959 h->other = other | ELF_ST_VISIBILITY (h->other);
12962 if (!definition
12963 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
12964 h->other |= STO_OPTIONAL;
12967 /* Decide whether an undefined symbol is special and can be ignored.
12968 This is the case for OPTIONAL symbols on IRIX. */
12969 bfd_boolean
12970 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
12972 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
12975 bfd_boolean
12976 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
12978 return (sym->st_shndx == SHN_COMMON
12979 || sym->st_shndx == SHN_MIPS_ACOMMON
12980 || sym->st_shndx == SHN_MIPS_SCOMMON);
12983 /* Return address for Ith PLT stub in section PLT, for relocation REL
12984 or (bfd_vma) -1 if it should not be included. */
12986 bfd_vma
12987 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
12988 const arelent *rel ATTRIBUTE_UNUSED)
12990 return (plt->vma
12991 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
12992 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
12995 void
12996 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
12998 struct mips_elf_link_hash_table *htab;
12999 Elf_Internal_Ehdr *i_ehdrp;
13001 i_ehdrp = elf_elfheader (abfd);
13002 if (link_info)
13004 htab = mips_elf_hash_table (link_info);
13005 BFD_ASSERT (htab != NULL);
13007 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
13008 i_ehdrp->e_ident[EI_ABIVERSION] = 1;