merge from gcc
[gdb/gnu.git] / bfd / elfxx-mips.c
blob1c64ad35d83366daf895b45613e594ed7dd18652
1 /* MIPS-specific support for ELF
2 Copyright 1993-2013 Free Software Foundation, Inc.
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7 <mark@codesourcery.com>
8 Traditional MIPS targets support added by Koundinya.K, Dansk Data
9 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
11 This file is part of BFD, the Binary File Descriptor library.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26 MA 02110-1301, USA. */
29 /* This file handles functionality common to the different MIPS ABI's. */
31 #include "sysdep.h"
32 #include "bfd.h"
33 #include "libbfd.h"
34 #include "libiberty.h"
35 #include "elf-bfd.h"
36 #include "elfxx-mips.h"
37 #include "elf/mips.h"
38 #include "elf-vxworks.h"
40 /* Get the ECOFF swapping routines. */
41 #include "coff/sym.h"
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
44 #include "coff/mips.h"
46 #include "hashtab.h"
48 /* Types of TLS GOT entry. */
49 enum mips_got_tls_type {
50 GOT_TLS_NONE,
51 GOT_TLS_GD,
52 GOT_TLS_LDM,
53 GOT_TLS_IE
56 /* This structure is used to hold information about one GOT entry.
57 There are four types of entry:
59 (1) an absolute address
60 requires: abfd == NULL
61 fields: d.address
63 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
64 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
65 fields: abfd, symndx, d.addend, tls_type
67 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
68 requires: abfd != NULL, symndx == -1
69 fields: d.h, tls_type
71 (4) a TLS LDM slot
72 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
73 fields: none; there's only one of these per GOT. */
74 struct mips_got_entry
76 /* One input bfd that needs the GOT entry. */
77 bfd *abfd;
78 /* The index of the symbol, as stored in the relocation r_info, if
79 we have a local symbol; -1 otherwise. */
80 long symndx;
81 union
83 /* If abfd == NULL, an address that must be stored in the got. */
84 bfd_vma address;
85 /* If abfd != NULL && symndx != -1, the addend of the relocation
86 that should be added to the symbol value. */
87 bfd_vma addend;
88 /* If abfd != NULL && symndx == -1, the hash table entry
89 corresponding to a symbol in the GOT. The symbol's entry
90 is in the local area if h->global_got_area is GGA_NONE,
91 otherwise it is in the global area. */
92 struct mips_elf_link_hash_entry *h;
93 } d;
95 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
96 symbol entry with r_symndx == 0. */
97 unsigned char tls_type;
99 /* True if we have filled in the GOT contents for a TLS entry,
100 and created the associated relocations. */
101 unsigned char tls_initialized;
103 /* The offset from the beginning of the .got section to the entry
104 corresponding to this symbol+addend. If it's a global symbol
105 whose offset is yet to be decided, it's going to be -1. */
106 long gotidx;
109 /* This structure represents a GOT page reference from an input bfd.
110 Each instance represents a symbol + ADDEND, where the representation
111 of the symbol depends on whether it is local to the input bfd.
112 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
113 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
115 Page references with SYMNDX >= 0 always become page references
116 in the output. Page references with SYMNDX < 0 only become page
117 references if the symbol binds locally; in other cases, the page
118 reference decays to a global GOT reference. */
119 struct mips_got_page_ref
121 long symndx;
122 union
124 struct mips_elf_link_hash_entry *h;
125 bfd *abfd;
126 } u;
127 bfd_vma addend;
130 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
131 The structures form a non-overlapping list that is sorted by increasing
132 MIN_ADDEND. */
133 struct mips_got_page_range
135 struct mips_got_page_range *next;
136 bfd_signed_vma min_addend;
137 bfd_signed_vma max_addend;
140 /* This structure describes the range of addends that are applied to page
141 relocations against a given section. */
142 struct mips_got_page_entry
144 /* The section that these entries are based on. */
145 asection *sec;
146 /* The ranges for this page entry. */
147 struct mips_got_page_range *ranges;
148 /* The maximum number of page entries needed for RANGES. */
149 bfd_vma num_pages;
152 /* This structure is used to hold .got information when linking. */
154 struct mips_got_info
156 /* The number of global .got entries. */
157 unsigned int global_gotno;
158 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
159 unsigned int reloc_only_gotno;
160 /* The number of .got slots used for TLS. */
161 unsigned int tls_gotno;
162 /* The first unused TLS .got entry. Used only during
163 mips_elf_initialize_tls_index. */
164 unsigned int tls_assigned_gotno;
165 /* The number of local .got entries, eventually including page entries. */
166 unsigned int local_gotno;
167 /* The maximum number of page entries needed. */
168 unsigned int page_gotno;
169 /* The number of relocations needed for the GOT entries. */
170 unsigned int relocs;
171 /* The number of local .got entries we have used. */
172 unsigned int assigned_gotno;
173 /* A hash table holding members of the got. */
174 struct htab *got_entries;
175 /* A hash table holding mips_got_page_ref structures. */
176 struct htab *got_page_refs;
177 /* A hash table of mips_got_page_entry structures. */
178 struct htab *got_page_entries;
179 /* In multi-got links, a pointer to the next got (err, rather, most
180 of the time, it points to the previous got). */
181 struct mips_got_info *next;
184 /* Structure passed when merging bfds' gots. */
186 struct mips_elf_got_per_bfd_arg
188 /* The output bfd. */
189 bfd *obfd;
190 /* The link information. */
191 struct bfd_link_info *info;
192 /* A pointer to the primary got, i.e., the one that's going to get
193 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
194 DT_MIPS_GOTSYM. */
195 struct mips_got_info *primary;
196 /* A non-primary got we're trying to merge with other input bfd's
197 gots. */
198 struct mips_got_info *current;
199 /* The maximum number of got entries that can be addressed with a
200 16-bit offset. */
201 unsigned int max_count;
202 /* The maximum number of page entries needed by each got. */
203 unsigned int max_pages;
204 /* The total number of global entries which will live in the
205 primary got and be automatically relocated. This includes
206 those not referenced by the primary GOT but included in
207 the "master" GOT. */
208 unsigned int global_count;
211 /* A structure used to pass information to htab_traverse callbacks
212 when laying out the GOT. */
214 struct mips_elf_traverse_got_arg
216 struct bfd_link_info *info;
217 struct mips_got_info *g;
218 int value;
221 struct _mips_elf_section_data
223 struct bfd_elf_section_data elf;
224 union
226 bfd_byte *tdata;
227 } u;
230 #define mips_elf_section_data(sec) \
231 ((struct _mips_elf_section_data *) elf_section_data (sec))
233 #define is_mips_elf(bfd) \
234 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
235 && elf_tdata (bfd) != NULL \
236 && elf_object_id (bfd) == MIPS_ELF_DATA)
238 /* The ABI says that every symbol used by dynamic relocations must have
239 a global GOT entry. Among other things, this provides the dynamic
240 linker with a free, directly-indexed cache. The GOT can therefore
241 contain symbols that are not referenced by GOT relocations themselves
242 (in other words, it may have symbols that are not referenced by things
243 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
245 GOT relocations are less likely to overflow if we put the associated
246 GOT entries towards the beginning. We therefore divide the global
247 GOT entries into two areas: "normal" and "reloc-only". Entries in
248 the first area can be used for both dynamic relocations and GP-relative
249 accesses, while those in the "reloc-only" area are for dynamic
250 relocations only.
252 These GGA_* ("Global GOT Area") values are organised so that lower
253 values are more general than higher values. Also, non-GGA_NONE
254 values are ordered by the position of the area in the GOT. */
255 #define GGA_NORMAL 0
256 #define GGA_RELOC_ONLY 1
257 #define GGA_NONE 2
259 /* Information about a non-PIC interface to a PIC function. There are
260 two ways of creating these interfaces. The first is to add:
262 lui $25,%hi(func)
263 addiu $25,$25,%lo(func)
265 immediately before a PIC function "func". The second is to add:
267 lui $25,%hi(func)
268 j func
269 addiu $25,$25,%lo(func)
271 to a separate trampoline section.
273 Stubs of the first kind go in a new section immediately before the
274 target function. Stubs of the second kind go in a single section
275 pointed to by the hash table's "strampoline" field. */
276 struct mips_elf_la25_stub {
277 /* The generated section that contains this stub. */
278 asection *stub_section;
280 /* The offset of the stub from the start of STUB_SECTION. */
281 bfd_vma offset;
283 /* One symbol for the original function. Its location is available
284 in H->root.root.u.def. */
285 struct mips_elf_link_hash_entry *h;
288 /* Macros for populating a mips_elf_la25_stub. */
290 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
291 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
292 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
293 #define LA25_LUI_MICROMIPS(VAL) \
294 (0x41b90000 | (VAL)) /* lui t9,VAL */
295 #define LA25_J_MICROMIPS(VAL) \
296 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
297 #define LA25_ADDIU_MICROMIPS(VAL) \
298 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
300 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
301 the dynamic symbols. */
303 struct mips_elf_hash_sort_data
305 /* The symbol in the global GOT with the lowest dynamic symbol table
306 index. */
307 struct elf_link_hash_entry *low;
308 /* The least dynamic symbol table index corresponding to a non-TLS
309 symbol with a GOT entry. */
310 long min_got_dynindx;
311 /* The greatest dynamic symbol table index corresponding to a symbol
312 with a GOT entry that is not referenced (e.g., a dynamic symbol
313 with dynamic relocations pointing to it from non-primary GOTs). */
314 long max_unref_got_dynindx;
315 /* The greatest dynamic symbol table index not corresponding to a
316 symbol without a GOT entry. */
317 long max_non_got_dynindx;
320 /* We make up to two PLT entries if needed, one for standard MIPS code
321 and one for compressed code, either a MIPS16 or microMIPS one. We
322 keep a separate record of traditional lazy-binding stubs, for easier
323 processing. */
325 struct plt_entry
327 /* Traditional SVR4 stub offset, or -1 if none. */
328 bfd_vma stub_offset;
330 /* Standard PLT entry offset, or -1 if none. */
331 bfd_vma mips_offset;
333 /* Compressed PLT entry offset, or -1 if none. */
334 bfd_vma comp_offset;
336 /* The corresponding .got.plt index, or -1 if none. */
337 bfd_vma gotplt_index;
339 /* Whether we need a standard PLT entry. */
340 unsigned int need_mips : 1;
342 /* Whether we need a compressed PLT entry. */
343 unsigned int need_comp : 1;
346 /* The MIPS ELF linker needs additional information for each symbol in
347 the global hash table. */
349 struct mips_elf_link_hash_entry
351 struct elf_link_hash_entry root;
353 /* External symbol information. */
354 EXTR esym;
356 /* The la25 stub we have created for ths symbol, if any. */
357 struct mips_elf_la25_stub *la25_stub;
359 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
360 this symbol. */
361 unsigned int possibly_dynamic_relocs;
363 /* If there is a stub that 32 bit functions should use to call this
364 16 bit function, this points to the section containing the stub. */
365 asection *fn_stub;
367 /* If there is a stub that 16 bit functions should use to call this
368 32 bit function, this points to the section containing the stub. */
369 asection *call_stub;
371 /* This is like the call_stub field, but it is used if the function
372 being called returns a floating point value. */
373 asection *call_fp_stub;
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;
411 /* Does this symbol resolve to a PLT entry? */
412 unsigned int use_plt_entry : 1;
415 /* MIPS ELF linker hash table. */
417 struct mips_elf_link_hash_table
419 struct elf_link_hash_table root;
421 /* The number of .rtproc entries. */
422 bfd_size_type procedure_count;
424 /* The size of the .compact_rel section (if SGI_COMPAT). */
425 bfd_size_type compact_rel_size;
427 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
428 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
429 bfd_boolean use_rld_obj_head;
431 /* The __rld_map or __rld_obj_head symbol. */
432 struct elf_link_hash_entry *rld_symbol;
434 /* This is set if we see any mips16 stub sections. */
435 bfd_boolean mips16_stubs_seen;
437 /* True if we can generate copy relocs and PLTs. */
438 bfd_boolean use_plts_and_copy_relocs;
440 /* True if we can only use 32-bit microMIPS instructions. */
441 bfd_boolean insn32;
443 /* True if we're generating code for VxWorks. */
444 bfd_boolean is_vxworks;
446 /* True if we already reported the small-data section overflow. */
447 bfd_boolean small_data_overflow_reported;
449 /* Shortcuts to some dynamic sections, or NULL if they are not
450 being used. */
451 asection *srelbss;
452 asection *sdynbss;
453 asection *srelplt;
454 asection *srelplt2;
455 asection *sgotplt;
456 asection *splt;
457 asection *sstubs;
458 asection *sgot;
460 /* The master GOT information. */
461 struct mips_got_info *got_info;
463 /* The global symbol in the GOT with the lowest index in the dynamic
464 symbol table. */
465 struct elf_link_hash_entry *global_gotsym;
467 /* The size of the PLT header in bytes. */
468 bfd_vma plt_header_size;
470 /* The size of a standard PLT entry in bytes. */
471 bfd_vma plt_mips_entry_size;
473 /* The size of a compressed PLT entry in bytes. */
474 bfd_vma plt_comp_entry_size;
476 /* The offset of the next standard PLT entry to create. */
477 bfd_vma plt_mips_offset;
479 /* The offset of the next compressed PLT entry to create. */
480 bfd_vma plt_comp_offset;
482 /* The index of the next .got.plt entry to create. */
483 bfd_vma plt_got_index;
485 /* The number of functions that need a lazy-binding stub. */
486 bfd_vma lazy_stub_count;
488 /* The size of a function stub entry in bytes. */
489 bfd_vma function_stub_size;
491 /* The number of reserved entries at the beginning of the GOT. */
492 unsigned int reserved_gotno;
494 /* The section used for mips_elf_la25_stub trampolines.
495 See the comment above that structure for details. */
496 asection *strampoline;
498 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
499 pairs. */
500 htab_t la25_stubs;
502 /* A function FN (NAME, IS, OS) that creates a new input section
503 called NAME and links it to output section OS. If IS is nonnull,
504 the new section should go immediately before it, otherwise it
505 should go at the (current) beginning of OS.
507 The function returns the new section on success, otherwise it
508 returns null. */
509 asection *(*add_stub_section) (const char *, asection *, asection *);
511 /* Small local sym cache. */
512 struct sym_cache sym_cache;
514 /* Is the PLT header compressed? */
515 unsigned int plt_header_is_comp : 1;
518 /* Get the MIPS ELF linker hash table from a link_info structure. */
520 #define mips_elf_hash_table(p) \
521 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
522 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
524 /* A structure used to communicate with htab_traverse callbacks. */
525 struct mips_htab_traverse_info
527 /* The usual link-wide information. */
528 struct bfd_link_info *info;
529 bfd *output_bfd;
531 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
532 bfd_boolean error;
535 /* MIPS ELF private object data. */
537 struct mips_elf_obj_tdata
539 /* Generic ELF private object data. */
540 struct elf_obj_tdata root;
542 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
543 bfd *abi_fp_bfd;
545 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
546 bfd *abi_msa_bfd;
548 /* The GOT requirements of input bfds. */
549 struct mips_got_info *got;
551 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
552 included directly in this one, but there's no point to wasting
553 the memory just for the infrequently called find_nearest_line. */
554 struct mips_elf_find_line *find_line_info;
556 /* An array of stub sections indexed by symbol number. */
557 asection **local_stubs;
558 asection **local_call_stubs;
560 /* The Irix 5 support uses two virtual sections, which represent
561 text/data symbols defined in dynamic objects. */
562 asymbol *elf_data_symbol;
563 asymbol *elf_text_symbol;
564 asection *elf_data_section;
565 asection *elf_text_section;
568 /* Get MIPS ELF private object data from BFD's tdata. */
570 #define mips_elf_tdata(bfd) \
571 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
573 #define TLS_RELOC_P(r_type) \
574 (r_type == R_MIPS_TLS_DTPMOD32 \
575 || r_type == R_MIPS_TLS_DTPMOD64 \
576 || r_type == R_MIPS_TLS_DTPREL32 \
577 || r_type == R_MIPS_TLS_DTPREL64 \
578 || r_type == R_MIPS_TLS_GD \
579 || r_type == R_MIPS_TLS_LDM \
580 || r_type == R_MIPS_TLS_DTPREL_HI16 \
581 || r_type == R_MIPS_TLS_DTPREL_LO16 \
582 || r_type == R_MIPS_TLS_GOTTPREL \
583 || r_type == R_MIPS_TLS_TPREL32 \
584 || r_type == R_MIPS_TLS_TPREL64 \
585 || r_type == R_MIPS_TLS_TPREL_HI16 \
586 || r_type == R_MIPS_TLS_TPREL_LO16 \
587 || r_type == R_MIPS16_TLS_GD \
588 || r_type == R_MIPS16_TLS_LDM \
589 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
590 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
591 || r_type == R_MIPS16_TLS_GOTTPREL \
592 || r_type == R_MIPS16_TLS_TPREL_HI16 \
593 || r_type == R_MIPS16_TLS_TPREL_LO16 \
594 || r_type == R_MICROMIPS_TLS_GD \
595 || r_type == R_MICROMIPS_TLS_LDM \
596 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
597 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
598 || r_type == R_MICROMIPS_TLS_GOTTPREL \
599 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
600 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
602 /* Structure used to pass information to mips_elf_output_extsym. */
604 struct extsym_info
606 bfd *abfd;
607 struct bfd_link_info *info;
608 struct ecoff_debug_info *debug;
609 const struct ecoff_debug_swap *swap;
610 bfd_boolean failed;
613 /* The names of the runtime procedure table symbols used on IRIX5. */
615 static const char * const mips_elf_dynsym_rtproc_names[] =
617 "_procedure_table",
618 "_procedure_string_table",
619 "_procedure_table_size",
620 NULL
623 /* These structures are used to generate the .compact_rel section on
624 IRIX5. */
626 typedef struct
628 unsigned long id1; /* Always one? */
629 unsigned long num; /* Number of compact relocation entries. */
630 unsigned long id2; /* Always two? */
631 unsigned long offset; /* The file offset of the first relocation. */
632 unsigned long reserved0; /* Zero? */
633 unsigned long reserved1; /* Zero? */
634 } Elf32_compact_rel;
636 typedef struct
638 bfd_byte id1[4];
639 bfd_byte num[4];
640 bfd_byte id2[4];
641 bfd_byte offset[4];
642 bfd_byte reserved0[4];
643 bfd_byte reserved1[4];
644 } Elf32_External_compact_rel;
646 typedef struct
648 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
649 unsigned int rtype : 4; /* Relocation types. See below. */
650 unsigned int dist2to : 8;
651 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
652 unsigned long konst; /* KONST field. See below. */
653 unsigned long vaddr; /* VADDR to be relocated. */
654 } Elf32_crinfo;
656 typedef struct
658 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
659 unsigned int rtype : 4; /* Relocation types. See below. */
660 unsigned int dist2to : 8;
661 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
662 unsigned long konst; /* KONST field. See below. */
663 } Elf32_crinfo2;
665 typedef struct
667 bfd_byte info[4];
668 bfd_byte konst[4];
669 bfd_byte vaddr[4];
670 } Elf32_External_crinfo;
672 typedef struct
674 bfd_byte info[4];
675 bfd_byte konst[4];
676 } Elf32_External_crinfo2;
678 /* These are the constants used to swap the bitfields in a crinfo. */
680 #define CRINFO_CTYPE (0x1)
681 #define CRINFO_CTYPE_SH (31)
682 #define CRINFO_RTYPE (0xf)
683 #define CRINFO_RTYPE_SH (27)
684 #define CRINFO_DIST2TO (0xff)
685 #define CRINFO_DIST2TO_SH (19)
686 #define CRINFO_RELVADDR (0x7ffff)
687 #define CRINFO_RELVADDR_SH (0)
689 /* A compact relocation info has long (3 words) or short (2 words)
690 formats. A short format doesn't have VADDR field and relvaddr
691 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
692 #define CRF_MIPS_LONG 1
693 #define CRF_MIPS_SHORT 0
695 /* There are 4 types of compact relocation at least. The value KONST
696 has different meaning for each type:
698 (type) (konst)
699 CT_MIPS_REL32 Address in data
700 CT_MIPS_WORD Address in word (XXX)
701 CT_MIPS_GPHI_LO GP - vaddr
702 CT_MIPS_JMPAD Address to jump
705 #define CRT_MIPS_REL32 0xa
706 #define CRT_MIPS_WORD 0xb
707 #define CRT_MIPS_GPHI_LO 0xc
708 #define CRT_MIPS_JMPAD 0xd
710 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
711 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
712 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
713 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
715 /* The structure of the runtime procedure descriptor created by the
716 loader for use by the static exception system. */
718 typedef struct runtime_pdr {
719 bfd_vma adr; /* Memory address of start of procedure. */
720 long regmask; /* Save register mask. */
721 long regoffset; /* Save register offset. */
722 long fregmask; /* Save floating point register mask. */
723 long fregoffset; /* Save floating point register offset. */
724 long frameoffset; /* Frame size. */
725 short framereg; /* Frame pointer register. */
726 short pcreg; /* Offset or reg of return pc. */
727 long irpss; /* Index into the runtime string table. */
728 long reserved;
729 struct exception_info *exception_info;/* Pointer to exception array. */
730 } RPDR, *pRPDR;
731 #define cbRPDR sizeof (RPDR)
732 #define rpdNil ((pRPDR) 0)
734 static struct mips_got_entry *mips_elf_create_local_got_entry
735 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
736 struct mips_elf_link_hash_entry *, int);
737 static bfd_boolean mips_elf_sort_hash_table_f
738 (struct mips_elf_link_hash_entry *, void *);
739 static bfd_vma mips_elf_high
740 (bfd_vma);
741 static bfd_boolean mips_elf_create_dynamic_relocation
742 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
743 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
744 bfd_vma *, asection *);
745 static bfd_vma mips_elf_adjust_gp
746 (bfd *, struct mips_got_info *, bfd *);
748 /* This will be used when we sort the dynamic relocation records. */
749 static bfd *reldyn_sorting_bfd;
751 /* True if ABFD is for CPUs with load interlocking that include
752 non-MIPS1 CPUs and R3900. */
753 #define LOAD_INTERLOCKS_P(abfd) \
754 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
755 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
757 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
758 This should be safe for all architectures. We enable this predicate
759 for RM9000 for now. */
760 #define JAL_TO_BAL_P(abfd) \
761 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
763 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
764 This should be safe for all architectures. We enable this predicate for
765 all CPUs. */
766 #define JALR_TO_BAL_P(abfd) 1
768 /* True if ABFD is for CPUs that are faster if JR is converted to B.
769 This should be safe for all architectures. We enable this predicate for
770 all CPUs. */
771 #define JR_TO_B_P(abfd) 1
773 /* True if ABFD is a PIC object. */
774 #define PIC_OBJECT_P(abfd) \
775 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
777 /* Nonzero if ABFD is using the N32 ABI. */
778 #define ABI_N32_P(abfd) \
779 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
781 /* Nonzero if ABFD is using the N64 ABI. */
782 #define ABI_64_P(abfd) \
783 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
785 /* Nonzero if ABFD is using NewABI conventions. */
786 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
788 /* Nonzero if ABFD has microMIPS code. */
789 #define MICROMIPS_P(abfd) \
790 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
792 /* The IRIX compatibility level we are striving for. */
793 #define IRIX_COMPAT(abfd) \
794 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
796 /* Whether we are trying to be compatible with IRIX at all. */
797 #define SGI_COMPAT(abfd) \
798 (IRIX_COMPAT (abfd) != ict_none)
800 /* The name of the options section. */
801 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
802 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
804 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
805 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
806 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
807 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
809 /* Whether the section is readonly. */
810 #define MIPS_ELF_READONLY_SECTION(sec) \
811 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
812 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
814 /* The name of the stub section. */
815 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
817 /* The size of an external REL relocation. */
818 #define MIPS_ELF_REL_SIZE(abfd) \
819 (get_elf_backend_data (abfd)->s->sizeof_rel)
821 /* The size of an external RELA relocation. */
822 #define MIPS_ELF_RELA_SIZE(abfd) \
823 (get_elf_backend_data (abfd)->s->sizeof_rela)
825 /* The size of an external dynamic table entry. */
826 #define MIPS_ELF_DYN_SIZE(abfd) \
827 (get_elf_backend_data (abfd)->s->sizeof_dyn)
829 /* The size of a GOT entry. */
830 #define MIPS_ELF_GOT_SIZE(abfd) \
831 (get_elf_backend_data (abfd)->s->arch_size / 8)
833 /* The size of the .rld_map section. */
834 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
835 (get_elf_backend_data (abfd)->s->arch_size / 8)
837 /* The size of a symbol-table entry. */
838 #define MIPS_ELF_SYM_SIZE(abfd) \
839 (get_elf_backend_data (abfd)->s->sizeof_sym)
841 /* The default alignment for sections, as a power of two. */
842 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
843 (get_elf_backend_data (abfd)->s->log_file_align)
845 /* Get word-sized data. */
846 #define MIPS_ELF_GET_WORD(abfd, ptr) \
847 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
849 /* Put out word-sized data. */
850 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
851 (ABI_64_P (abfd) \
852 ? bfd_put_64 (abfd, val, ptr) \
853 : bfd_put_32 (abfd, val, ptr))
855 /* The opcode for word-sized loads (LW or LD). */
856 #define MIPS_ELF_LOAD_WORD(abfd) \
857 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
859 /* Add a dynamic symbol table-entry. */
860 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
861 _bfd_elf_add_dynamic_entry (info, tag, val)
863 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
864 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
866 /* The name of the dynamic relocation section. */
867 #define MIPS_ELF_REL_DYN_NAME(INFO) \
868 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
870 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
871 from smaller values. Start with zero, widen, *then* decrement. */
872 #define MINUS_ONE (((bfd_vma)0) - 1)
873 #define MINUS_TWO (((bfd_vma)0) - 2)
875 /* The value to write into got[1] for SVR4 targets, to identify it is
876 a GNU object. The dynamic linker can then use got[1] to store the
877 module pointer. */
878 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
879 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
881 /* The offset of $gp from the beginning of the .got section. */
882 #define ELF_MIPS_GP_OFFSET(INFO) \
883 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
885 /* The maximum size of the GOT for it to be addressable using 16-bit
886 offsets from $gp. */
887 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
889 /* Instructions which appear in a stub. */
890 #define STUB_LW(abfd) \
891 ((ABI_64_P (abfd) \
892 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
893 : 0x8f998010)) /* lw t9,0x8010(gp) */
894 #define STUB_MOVE(abfd) \
895 ((ABI_64_P (abfd) \
896 ? 0x03e0782d /* daddu t7,ra */ \
897 : 0x03e07821)) /* addu t7,ra */
898 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
899 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
900 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
901 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
902 #define STUB_LI16S(abfd, VAL) \
903 ((ABI_64_P (abfd) \
904 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
905 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
907 /* Likewise for the microMIPS ASE. */
908 #define STUB_LW_MICROMIPS(abfd) \
909 (ABI_64_P (abfd) \
910 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
911 : 0xff3c8010) /* lw t9,0x8010(gp) */
912 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
913 #define STUB_MOVE32_MICROMIPS(abfd) \
914 (ABI_64_P (abfd) \
915 ? 0x581f7950 /* daddu t7,ra,zero */ \
916 : 0x001f7950) /* addu t7,ra,zero */
917 #define STUB_LUI_MICROMIPS(VAL) \
918 (0x41b80000 + (VAL)) /* lui t8,VAL */
919 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
920 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
921 #define STUB_ORI_MICROMIPS(VAL) \
922 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
923 #define STUB_LI16U_MICROMIPS(VAL) \
924 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
925 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
926 (ABI_64_P (abfd) \
927 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
928 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
930 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
931 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
932 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
933 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
934 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
935 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
937 /* The name of the dynamic interpreter. This is put in the .interp
938 section. */
940 #define ELF_DYNAMIC_INTERPRETER(abfd) \
941 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
942 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
943 : "/usr/lib/libc.so.1")
945 #ifdef BFD64
946 #define MNAME(bfd,pre,pos) \
947 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
948 #define ELF_R_SYM(bfd, i) \
949 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
950 #define ELF_R_TYPE(bfd, i) \
951 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
952 #define ELF_R_INFO(bfd, s, t) \
953 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
954 #else
955 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
956 #define ELF_R_SYM(bfd, i) \
957 (ELF32_R_SYM (i))
958 #define ELF_R_TYPE(bfd, i) \
959 (ELF32_R_TYPE (i))
960 #define ELF_R_INFO(bfd, s, t) \
961 (ELF32_R_INFO (s, t))
962 #endif
964 /* The mips16 compiler uses a couple of special sections to handle
965 floating point arguments.
967 Section names that look like .mips16.fn.FNNAME contain stubs that
968 copy floating point arguments from the fp regs to the gp regs and
969 then jump to FNNAME. If any 32 bit function calls FNNAME, the
970 call should be redirected to the stub instead. If no 32 bit
971 function calls FNNAME, the stub should be discarded. We need to
972 consider any reference to the function, not just a call, because
973 if the address of the function is taken we will need the stub,
974 since the address might be passed to a 32 bit function.
976 Section names that look like .mips16.call.FNNAME contain stubs
977 that copy floating point arguments from the gp regs to the fp
978 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
979 then any 16 bit function that calls FNNAME should be redirected
980 to the stub instead. If FNNAME is not a 32 bit function, the
981 stub should be discarded.
983 .mips16.call.fp.FNNAME sections are similar, but contain stubs
984 which call FNNAME and then copy the return value from the fp regs
985 to the gp regs. These stubs store the return value in $18 while
986 calling FNNAME; any function which might call one of these stubs
987 must arrange to save $18 around the call. (This case is not
988 needed for 32 bit functions that call 16 bit functions, because
989 16 bit functions always return floating point values in both
990 $f0/$f1 and $2/$3.)
992 Note that in all cases FNNAME might be defined statically.
993 Therefore, FNNAME is not used literally. Instead, the relocation
994 information will indicate which symbol the section is for.
996 We record any stubs that we find in the symbol table. */
998 #define FN_STUB ".mips16.fn."
999 #define CALL_STUB ".mips16.call."
1000 #define CALL_FP_STUB ".mips16.call.fp."
1002 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1003 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1004 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
1006 /* The format of the first PLT entry in an O32 executable. */
1007 static const bfd_vma mips_o32_exec_plt0_entry[] =
1009 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1010 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1011 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1012 0x031cc023, /* subu $24, $24, $28 */
1013 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1014 0x0018c082, /* srl $24, $24, 2 */
1015 0x0320f809, /* jalr $25 */
1016 0x2718fffe /* subu $24, $24, 2 */
1019 /* The format of the first PLT entry in an N32 executable. Different
1020 because gp ($28) is not available; we use t2 ($14) instead. */
1021 static const bfd_vma mips_n32_exec_plt0_entry[] =
1023 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1024 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1025 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1026 0x030ec023, /* subu $24, $24, $14 */
1027 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1028 0x0018c082, /* srl $24, $24, 2 */
1029 0x0320f809, /* jalr $25 */
1030 0x2718fffe /* subu $24, $24, 2 */
1033 /* The format of the first PLT entry in an N64 executable. Different
1034 from N32 because of the increased size of GOT entries. */
1035 static const bfd_vma mips_n64_exec_plt0_entry[] =
1037 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1038 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1039 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1040 0x030ec023, /* subu $24, $24, $14 */
1041 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1042 0x0018c0c2, /* srl $24, $24, 3 */
1043 0x0320f809, /* jalr $25 */
1044 0x2718fffe /* subu $24, $24, 2 */
1047 /* The format of the microMIPS first PLT entry in an O32 executable.
1048 We rely on v0 ($2) rather than t8 ($24) to contain the address
1049 of the GOTPLT entry handled, so this stub may only be used when
1050 all the subsequent PLT entries are microMIPS code too.
1052 The trailing NOP is for alignment and correct disassembly only. */
1053 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1055 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1056 0xff23, 0x0000, /* lw $25, 0($3) */
1057 0x0535, /* subu $2, $2, $3 */
1058 0x2525, /* srl $2, $2, 2 */
1059 0x3302, 0xfffe, /* subu $24, $2, 2 */
1060 0x0dff, /* move $15, $31 */
1061 0x45f9, /* jalrs $25 */
1062 0x0f83, /* move $28, $3 */
1063 0x0c00 /* nop */
1066 /* The format of the microMIPS first PLT entry in an O32 executable
1067 in the insn32 mode. */
1068 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1070 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1071 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1072 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1073 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1074 0x001f, 0x7950, /* move $15, $31 */
1075 0x0318, 0x1040, /* srl $24, $24, 2 */
1076 0x03f9, 0x0f3c, /* jalr $25 */
1077 0x3318, 0xfffe /* subu $24, $24, 2 */
1080 /* The format of subsequent standard PLT entries. */
1081 static const bfd_vma mips_exec_plt_entry[] =
1083 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1084 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1085 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1086 0x03200008 /* jr $25 */
1089 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1090 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1091 directly addressable. */
1092 static const bfd_vma mips16_o32_exec_plt_entry[] =
1094 0xb203, /* lw $2, 12($pc) */
1095 0x9a60, /* lw $3, 0($2) */
1096 0x651a, /* move $24, $2 */
1097 0xeb00, /* jr $3 */
1098 0x653b, /* move $25, $3 */
1099 0x6500, /* nop */
1100 0x0000, 0x0000 /* .word (.got.plt entry) */
1103 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1104 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1105 static const bfd_vma micromips_o32_exec_plt_entry[] =
1107 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1108 0xff22, 0x0000, /* lw $25, 0($2) */
1109 0x4599, /* jr $25 */
1110 0x0f02 /* move $24, $2 */
1113 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1114 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1116 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1117 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1118 0x0019, 0x0f3c, /* jr $25 */
1119 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1122 /* The format of the first PLT entry in a VxWorks executable. */
1123 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1125 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1126 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1127 0x8f390008, /* lw t9, 8(t9) */
1128 0x00000000, /* nop */
1129 0x03200008, /* jr t9 */
1130 0x00000000 /* nop */
1133 /* The format of subsequent PLT entries. */
1134 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1136 0x10000000, /* b .PLT_resolver */
1137 0x24180000, /* li t8, <pltindex> */
1138 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1139 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1140 0x8f390000, /* lw t9, 0(t9) */
1141 0x00000000, /* nop */
1142 0x03200008, /* jr t9 */
1143 0x00000000 /* nop */
1146 /* The format of the first PLT entry in a VxWorks shared object. */
1147 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1149 0x8f990008, /* lw t9, 8(gp) */
1150 0x00000000, /* nop */
1151 0x03200008, /* jr t9 */
1152 0x00000000, /* nop */
1153 0x00000000, /* nop */
1154 0x00000000 /* nop */
1157 /* The format of subsequent PLT entries. */
1158 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1160 0x10000000, /* b .PLT_resolver */
1161 0x24180000 /* li t8, <pltindex> */
1164 /* microMIPS 32-bit opcode helper installer. */
1166 static void
1167 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1169 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1170 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1173 /* microMIPS 32-bit opcode helper retriever. */
1175 static bfd_vma
1176 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1178 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1181 /* Look up an entry in a MIPS ELF linker hash table. */
1183 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1184 ((struct mips_elf_link_hash_entry *) \
1185 elf_link_hash_lookup (&(table)->root, (string), (create), \
1186 (copy), (follow)))
1188 /* Traverse a MIPS ELF linker hash table. */
1190 #define mips_elf_link_hash_traverse(table, func, info) \
1191 (elf_link_hash_traverse \
1192 (&(table)->root, \
1193 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1194 (info)))
1196 /* Find the base offsets for thread-local storage in this object,
1197 for GD/LD and IE/LE respectively. */
1199 #define TP_OFFSET 0x7000
1200 #define DTP_OFFSET 0x8000
1202 static bfd_vma
1203 dtprel_base (struct bfd_link_info *info)
1205 /* If tls_sec is NULL, we should have signalled an error already. */
1206 if (elf_hash_table (info)->tls_sec == NULL)
1207 return 0;
1208 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1211 static bfd_vma
1212 tprel_base (struct bfd_link_info *info)
1214 /* If tls_sec is NULL, we should have signalled an error already. */
1215 if (elf_hash_table (info)->tls_sec == NULL)
1216 return 0;
1217 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1220 /* Create an entry in a MIPS ELF linker hash table. */
1222 static struct bfd_hash_entry *
1223 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1224 struct bfd_hash_table *table, const char *string)
1226 struct mips_elf_link_hash_entry *ret =
1227 (struct mips_elf_link_hash_entry *) entry;
1229 /* Allocate the structure if it has not already been allocated by a
1230 subclass. */
1231 if (ret == NULL)
1232 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1233 if (ret == NULL)
1234 return (struct bfd_hash_entry *) ret;
1236 /* Call the allocation method of the superclass. */
1237 ret = ((struct mips_elf_link_hash_entry *)
1238 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1239 table, string));
1240 if (ret != NULL)
1242 /* Set local fields. */
1243 memset (&ret->esym, 0, sizeof (EXTR));
1244 /* We use -2 as a marker to indicate that the information has
1245 not been set. -1 means there is no associated ifd. */
1246 ret->esym.ifd = -2;
1247 ret->la25_stub = 0;
1248 ret->possibly_dynamic_relocs = 0;
1249 ret->fn_stub = NULL;
1250 ret->call_stub = NULL;
1251 ret->call_fp_stub = NULL;
1252 ret->global_got_area = GGA_NONE;
1253 ret->got_only_for_calls = TRUE;
1254 ret->readonly_reloc = FALSE;
1255 ret->has_static_relocs = FALSE;
1256 ret->no_fn_stub = FALSE;
1257 ret->need_fn_stub = FALSE;
1258 ret->has_nonpic_branches = FALSE;
1259 ret->needs_lazy_stub = FALSE;
1260 ret->use_plt_entry = FALSE;
1263 return (struct bfd_hash_entry *) ret;
1266 /* Allocate MIPS ELF private object data. */
1268 bfd_boolean
1269 _bfd_mips_elf_mkobject (bfd *abfd)
1271 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1272 MIPS_ELF_DATA);
1275 bfd_boolean
1276 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1278 if (!sec->used_by_bfd)
1280 struct _mips_elf_section_data *sdata;
1281 bfd_size_type amt = sizeof (*sdata);
1283 sdata = bfd_zalloc (abfd, amt);
1284 if (sdata == NULL)
1285 return FALSE;
1286 sec->used_by_bfd = sdata;
1289 return _bfd_elf_new_section_hook (abfd, sec);
1292 /* Read ECOFF debugging information from a .mdebug section into a
1293 ecoff_debug_info structure. */
1295 bfd_boolean
1296 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1297 struct ecoff_debug_info *debug)
1299 HDRR *symhdr;
1300 const struct ecoff_debug_swap *swap;
1301 char *ext_hdr;
1303 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1304 memset (debug, 0, sizeof (*debug));
1306 ext_hdr = bfd_malloc (swap->external_hdr_size);
1307 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1308 goto error_return;
1310 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1311 swap->external_hdr_size))
1312 goto error_return;
1314 symhdr = &debug->symbolic_header;
1315 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1317 /* The symbolic header contains absolute file offsets and sizes to
1318 read. */
1319 #define READ(ptr, offset, count, size, type) \
1320 if (symhdr->count == 0) \
1321 debug->ptr = NULL; \
1322 else \
1324 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1325 debug->ptr = bfd_malloc (amt); \
1326 if (debug->ptr == NULL) \
1327 goto error_return; \
1328 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1329 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1330 goto error_return; \
1333 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1334 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1335 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1336 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1337 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1338 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1339 union aux_ext *);
1340 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1341 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1342 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1343 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1344 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1345 #undef READ
1347 debug->fdr = NULL;
1349 return TRUE;
1351 error_return:
1352 if (ext_hdr != NULL)
1353 free (ext_hdr);
1354 if (debug->line != NULL)
1355 free (debug->line);
1356 if (debug->external_dnr != NULL)
1357 free (debug->external_dnr);
1358 if (debug->external_pdr != NULL)
1359 free (debug->external_pdr);
1360 if (debug->external_sym != NULL)
1361 free (debug->external_sym);
1362 if (debug->external_opt != NULL)
1363 free (debug->external_opt);
1364 if (debug->external_aux != NULL)
1365 free (debug->external_aux);
1366 if (debug->ss != NULL)
1367 free (debug->ss);
1368 if (debug->ssext != NULL)
1369 free (debug->ssext);
1370 if (debug->external_fdr != NULL)
1371 free (debug->external_fdr);
1372 if (debug->external_rfd != NULL)
1373 free (debug->external_rfd);
1374 if (debug->external_ext != NULL)
1375 free (debug->external_ext);
1376 return FALSE;
1379 /* Swap RPDR (runtime procedure table entry) for output. */
1381 static void
1382 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1384 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1385 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1386 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1387 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1388 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1389 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1391 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1392 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1394 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1397 /* Create a runtime procedure table from the .mdebug section. */
1399 static bfd_boolean
1400 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1401 struct bfd_link_info *info, asection *s,
1402 struct ecoff_debug_info *debug)
1404 const struct ecoff_debug_swap *swap;
1405 HDRR *hdr = &debug->symbolic_header;
1406 RPDR *rpdr, *rp;
1407 struct rpdr_ext *erp;
1408 void *rtproc;
1409 struct pdr_ext *epdr;
1410 struct sym_ext *esym;
1411 char *ss, **sv;
1412 char *str;
1413 bfd_size_type size;
1414 bfd_size_type count;
1415 unsigned long sindex;
1416 unsigned long i;
1417 PDR pdr;
1418 SYMR sym;
1419 const char *no_name_func = _("static procedure (no name)");
1421 epdr = NULL;
1422 rpdr = NULL;
1423 esym = NULL;
1424 ss = NULL;
1425 sv = NULL;
1427 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1429 sindex = strlen (no_name_func) + 1;
1430 count = hdr->ipdMax;
1431 if (count > 0)
1433 size = swap->external_pdr_size;
1435 epdr = bfd_malloc (size * count);
1436 if (epdr == NULL)
1437 goto error_return;
1439 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1440 goto error_return;
1442 size = sizeof (RPDR);
1443 rp = rpdr = bfd_malloc (size * count);
1444 if (rpdr == NULL)
1445 goto error_return;
1447 size = sizeof (char *);
1448 sv = bfd_malloc (size * count);
1449 if (sv == NULL)
1450 goto error_return;
1452 count = hdr->isymMax;
1453 size = swap->external_sym_size;
1454 esym = bfd_malloc (size * count);
1455 if (esym == NULL)
1456 goto error_return;
1458 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1459 goto error_return;
1461 count = hdr->issMax;
1462 ss = bfd_malloc (count);
1463 if (ss == NULL)
1464 goto error_return;
1465 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1466 goto error_return;
1468 count = hdr->ipdMax;
1469 for (i = 0; i < (unsigned long) count; i++, rp++)
1471 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1472 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1473 rp->adr = sym.value;
1474 rp->regmask = pdr.regmask;
1475 rp->regoffset = pdr.regoffset;
1476 rp->fregmask = pdr.fregmask;
1477 rp->fregoffset = pdr.fregoffset;
1478 rp->frameoffset = pdr.frameoffset;
1479 rp->framereg = pdr.framereg;
1480 rp->pcreg = pdr.pcreg;
1481 rp->irpss = sindex;
1482 sv[i] = ss + sym.iss;
1483 sindex += strlen (sv[i]) + 1;
1487 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1488 size = BFD_ALIGN (size, 16);
1489 rtproc = bfd_alloc (abfd, size);
1490 if (rtproc == NULL)
1492 mips_elf_hash_table (info)->procedure_count = 0;
1493 goto error_return;
1496 mips_elf_hash_table (info)->procedure_count = count + 2;
1498 erp = rtproc;
1499 memset (erp, 0, sizeof (struct rpdr_ext));
1500 erp++;
1501 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1502 strcpy (str, no_name_func);
1503 str += strlen (no_name_func) + 1;
1504 for (i = 0; i < count; i++)
1506 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1507 strcpy (str, sv[i]);
1508 str += strlen (sv[i]) + 1;
1510 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1512 /* Set the size and contents of .rtproc section. */
1513 s->size = size;
1514 s->contents = rtproc;
1516 /* Skip this section later on (I don't think this currently
1517 matters, but someday it might). */
1518 s->map_head.link_order = NULL;
1520 if (epdr != NULL)
1521 free (epdr);
1522 if (rpdr != NULL)
1523 free (rpdr);
1524 if (esym != NULL)
1525 free (esym);
1526 if (ss != NULL)
1527 free (ss);
1528 if (sv != NULL)
1529 free (sv);
1531 return TRUE;
1533 error_return:
1534 if (epdr != NULL)
1535 free (epdr);
1536 if (rpdr != NULL)
1537 free (rpdr);
1538 if (esym != NULL)
1539 free (esym);
1540 if (ss != NULL)
1541 free (ss);
1542 if (sv != NULL)
1543 free (sv);
1544 return FALSE;
1547 /* We're going to create a stub for H. Create a symbol for the stub's
1548 value and size, to help make the disassembly easier to read. */
1550 static bfd_boolean
1551 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1552 struct mips_elf_link_hash_entry *h,
1553 const char *prefix, asection *s, bfd_vma value,
1554 bfd_vma size)
1556 struct bfd_link_hash_entry *bh;
1557 struct elf_link_hash_entry *elfh;
1558 const char *name;
1560 if (ELF_ST_IS_MICROMIPS (h->root.other))
1561 value |= 1;
1563 /* Create a new symbol. */
1564 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1565 bh = NULL;
1566 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1567 BSF_LOCAL, s, value, NULL,
1568 TRUE, FALSE, &bh))
1569 return FALSE;
1571 /* Make it a local function. */
1572 elfh = (struct elf_link_hash_entry *) bh;
1573 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1574 elfh->size = size;
1575 elfh->forced_local = 1;
1576 return TRUE;
1579 /* We're about to redefine H. Create a symbol to represent H's
1580 current value and size, to help make the disassembly easier
1581 to read. */
1583 static bfd_boolean
1584 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1585 struct mips_elf_link_hash_entry *h,
1586 const char *prefix)
1588 struct bfd_link_hash_entry *bh;
1589 struct elf_link_hash_entry *elfh;
1590 const char *name;
1591 asection *s;
1592 bfd_vma value;
1594 /* Read the symbol's value. */
1595 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1596 || h->root.root.type == bfd_link_hash_defweak);
1597 s = h->root.root.u.def.section;
1598 value = h->root.root.u.def.value;
1600 /* Create a new symbol. */
1601 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1602 bh = NULL;
1603 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1604 BSF_LOCAL, s, value, NULL,
1605 TRUE, FALSE, &bh))
1606 return FALSE;
1608 /* Make it local and copy the other attributes from H. */
1609 elfh = (struct elf_link_hash_entry *) bh;
1610 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1611 elfh->other = h->root.other;
1612 elfh->size = h->root.size;
1613 elfh->forced_local = 1;
1614 return TRUE;
1617 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1618 function rather than to a hard-float stub. */
1620 static bfd_boolean
1621 section_allows_mips16_refs_p (asection *section)
1623 const char *name;
1625 name = bfd_get_section_name (section->owner, section);
1626 return (FN_STUB_P (name)
1627 || CALL_STUB_P (name)
1628 || CALL_FP_STUB_P (name)
1629 || strcmp (name, ".pdr") == 0);
1632 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1633 stub section of some kind. Return the R_SYMNDX of the target
1634 function, or 0 if we can't decide which function that is. */
1636 static unsigned long
1637 mips16_stub_symndx (const struct elf_backend_data *bed,
1638 asection *sec ATTRIBUTE_UNUSED,
1639 const Elf_Internal_Rela *relocs,
1640 const Elf_Internal_Rela *relend)
1642 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1643 const Elf_Internal_Rela *rel;
1645 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1646 one in a compound relocation. */
1647 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1648 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1649 return ELF_R_SYM (sec->owner, rel->r_info);
1651 /* Otherwise trust the first relocation, whatever its kind. This is
1652 the traditional behavior. */
1653 if (relocs < relend)
1654 return ELF_R_SYM (sec->owner, relocs->r_info);
1656 return 0;
1659 /* Check the mips16 stubs for a particular symbol, and see if we can
1660 discard them. */
1662 static void
1663 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1664 struct mips_elf_link_hash_entry *h)
1666 /* Dynamic symbols must use the standard call interface, in case other
1667 objects try to call them. */
1668 if (h->fn_stub != NULL
1669 && h->root.dynindx != -1)
1671 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1672 h->need_fn_stub = TRUE;
1675 if (h->fn_stub != NULL
1676 && ! h->need_fn_stub)
1678 /* We don't need the fn_stub; the only references to this symbol
1679 are 16 bit calls. Clobber the size to 0 to prevent it from
1680 being included in the link. */
1681 h->fn_stub->size = 0;
1682 h->fn_stub->flags &= ~SEC_RELOC;
1683 h->fn_stub->reloc_count = 0;
1684 h->fn_stub->flags |= SEC_EXCLUDE;
1687 if (h->call_stub != NULL
1688 && ELF_ST_IS_MIPS16 (h->root.other))
1690 /* We don't need the call_stub; this is a 16 bit function, so
1691 calls from other 16 bit functions are OK. Clobber the size
1692 to 0 to prevent it from being included in the link. */
1693 h->call_stub->size = 0;
1694 h->call_stub->flags &= ~SEC_RELOC;
1695 h->call_stub->reloc_count = 0;
1696 h->call_stub->flags |= SEC_EXCLUDE;
1699 if (h->call_fp_stub != NULL
1700 && ELF_ST_IS_MIPS16 (h->root.other))
1702 /* We don't need the call_stub; this is a 16 bit function, so
1703 calls from other 16 bit functions are OK. Clobber the size
1704 to 0 to prevent it from being included in the link. */
1705 h->call_fp_stub->size = 0;
1706 h->call_fp_stub->flags &= ~SEC_RELOC;
1707 h->call_fp_stub->reloc_count = 0;
1708 h->call_fp_stub->flags |= SEC_EXCLUDE;
1712 /* Hashtable callbacks for mips_elf_la25_stubs. */
1714 static hashval_t
1715 mips_elf_la25_stub_hash (const void *entry_)
1717 const struct mips_elf_la25_stub *entry;
1719 entry = (struct mips_elf_la25_stub *) entry_;
1720 return entry->h->root.root.u.def.section->id
1721 + entry->h->root.root.u.def.value;
1724 static int
1725 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1727 const struct mips_elf_la25_stub *entry1, *entry2;
1729 entry1 = (struct mips_elf_la25_stub *) entry1_;
1730 entry2 = (struct mips_elf_la25_stub *) entry2_;
1731 return ((entry1->h->root.root.u.def.section
1732 == entry2->h->root.root.u.def.section)
1733 && (entry1->h->root.root.u.def.value
1734 == entry2->h->root.root.u.def.value));
1737 /* Called by the linker to set up the la25 stub-creation code. FN is
1738 the linker's implementation of add_stub_function. Return true on
1739 success. */
1741 bfd_boolean
1742 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1743 asection *(*fn) (const char *, asection *,
1744 asection *))
1746 struct mips_elf_link_hash_table *htab;
1748 htab = mips_elf_hash_table (info);
1749 if (htab == NULL)
1750 return FALSE;
1752 htab->add_stub_section = fn;
1753 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1754 mips_elf_la25_stub_eq, NULL);
1755 if (htab->la25_stubs == NULL)
1756 return FALSE;
1758 return TRUE;
1761 /* Return true if H is a locally-defined PIC function, in the sense
1762 that it or its fn_stub might need $25 to be valid on entry.
1763 Note that MIPS16 functions set up $gp using PC-relative instructions,
1764 so they themselves never need $25 to be valid. Only non-MIPS16
1765 entry points are of interest here. */
1767 static bfd_boolean
1768 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1770 return ((h->root.root.type == bfd_link_hash_defined
1771 || h->root.root.type == bfd_link_hash_defweak)
1772 && h->root.def_regular
1773 && !bfd_is_abs_section (h->root.root.u.def.section)
1774 && (!ELF_ST_IS_MIPS16 (h->root.other)
1775 || (h->fn_stub && h->need_fn_stub))
1776 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1777 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1780 /* Set *SEC to the input section that contains the target of STUB.
1781 Return the offset of the target from the start of that section. */
1783 static bfd_vma
1784 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1785 asection **sec)
1787 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1789 BFD_ASSERT (stub->h->need_fn_stub);
1790 *sec = stub->h->fn_stub;
1791 return 0;
1793 else
1795 *sec = stub->h->root.root.u.def.section;
1796 return stub->h->root.root.u.def.value;
1800 /* STUB describes an la25 stub that we have decided to implement
1801 by inserting an LUI/ADDIU pair before the target function.
1802 Create the section and redirect the function symbol to it. */
1804 static bfd_boolean
1805 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1806 struct bfd_link_info *info)
1808 struct mips_elf_link_hash_table *htab;
1809 char *name;
1810 asection *s, *input_section;
1811 unsigned int align;
1813 htab = mips_elf_hash_table (info);
1814 if (htab == NULL)
1815 return FALSE;
1817 /* Create a unique name for the new section. */
1818 name = bfd_malloc (11 + sizeof (".text.stub."));
1819 if (name == NULL)
1820 return FALSE;
1821 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1823 /* Create the section. */
1824 mips_elf_get_la25_target (stub, &input_section);
1825 s = htab->add_stub_section (name, input_section,
1826 input_section->output_section);
1827 if (s == NULL)
1828 return FALSE;
1830 /* Make sure that any padding goes before the stub. */
1831 align = input_section->alignment_power;
1832 if (!bfd_set_section_alignment (s->owner, s, align))
1833 return FALSE;
1834 if (align > 3)
1835 s->size = (1 << align) - 8;
1837 /* Create a symbol for the stub. */
1838 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1839 stub->stub_section = s;
1840 stub->offset = s->size;
1842 /* Allocate room for it. */
1843 s->size += 8;
1844 return TRUE;
1847 /* STUB describes an la25 stub that we have decided to implement
1848 with a separate trampoline. Allocate room for it and redirect
1849 the function symbol to it. */
1851 static bfd_boolean
1852 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1853 struct bfd_link_info *info)
1855 struct mips_elf_link_hash_table *htab;
1856 asection *s;
1858 htab = mips_elf_hash_table (info);
1859 if (htab == NULL)
1860 return FALSE;
1862 /* Create a trampoline section, if we haven't already. */
1863 s = htab->strampoline;
1864 if (s == NULL)
1866 asection *input_section = stub->h->root.root.u.def.section;
1867 s = htab->add_stub_section (".text", NULL,
1868 input_section->output_section);
1869 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1870 return FALSE;
1871 htab->strampoline = s;
1874 /* Create a symbol for the stub. */
1875 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1876 stub->stub_section = s;
1877 stub->offset = s->size;
1879 /* Allocate room for it. */
1880 s->size += 16;
1881 return TRUE;
1884 /* H describes a symbol that needs an la25 stub. Make sure that an
1885 appropriate stub exists and point H at it. */
1887 static bfd_boolean
1888 mips_elf_add_la25_stub (struct bfd_link_info *info,
1889 struct mips_elf_link_hash_entry *h)
1891 struct mips_elf_link_hash_table *htab;
1892 struct mips_elf_la25_stub search, *stub;
1893 bfd_boolean use_trampoline_p;
1894 asection *s;
1895 bfd_vma value;
1896 void **slot;
1898 /* Describe the stub we want. */
1899 search.stub_section = NULL;
1900 search.offset = 0;
1901 search.h = h;
1903 /* See if we've already created an equivalent stub. */
1904 htab = mips_elf_hash_table (info);
1905 if (htab == NULL)
1906 return FALSE;
1908 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1909 if (slot == NULL)
1910 return FALSE;
1912 stub = (struct mips_elf_la25_stub *) *slot;
1913 if (stub != NULL)
1915 /* We can reuse the existing stub. */
1916 h->la25_stub = stub;
1917 return TRUE;
1920 /* Create a permanent copy of ENTRY and add it to the hash table. */
1921 stub = bfd_malloc (sizeof (search));
1922 if (stub == NULL)
1923 return FALSE;
1924 *stub = search;
1925 *slot = stub;
1927 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1928 of the section and if we would need no more than 2 nops. */
1929 value = mips_elf_get_la25_target (stub, &s);
1930 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1932 h->la25_stub = stub;
1933 return (use_trampoline_p
1934 ? mips_elf_add_la25_trampoline (stub, info)
1935 : mips_elf_add_la25_intro (stub, info));
1938 /* A mips_elf_link_hash_traverse callback that is called before sizing
1939 sections. DATA points to a mips_htab_traverse_info structure. */
1941 static bfd_boolean
1942 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1944 struct mips_htab_traverse_info *hti;
1946 hti = (struct mips_htab_traverse_info *) data;
1947 if (!hti->info->relocatable)
1948 mips_elf_check_mips16_stubs (hti->info, h);
1950 if (mips_elf_local_pic_function_p (h))
1952 /* PR 12845: If H is in a section that has been garbage
1953 collected it will have its output section set to *ABS*. */
1954 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1955 return TRUE;
1957 /* H is a function that might need $25 to be valid on entry.
1958 If we're creating a non-PIC relocatable object, mark H as
1959 being PIC. If we're creating a non-relocatable object with
1960 non-PIC branches and jumps to H, make sure that H has an la25
1961 stub. */
1962 if (hti->info->relocatable)
1964 if (!PIC_OBJECT_P (hti->output_bfd))
1965 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1967 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1969 hti->error = TRUE;
1970 return FALSE;
1973 return TRUE;
1976 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1977 Most mips16 instructions are 16 bits, but these instructions
1978 are 32 bits.
1980 The format of these instructions is:
1982 +--------------+--------------------------------+
1983 | JALX | X| Imm 20:16 | Imm 25:21 |
1984 +--------------+--------------------------------+
1985 | Immediate 15:0 |
1986 +-----------------------------------------------+
1988 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1989 Note that the immediate value in the first word is swapped.
1991 When producing a relocatable object file, R_MIPS16_26 is
1992 handled mostly like R_MIPS_26. In particular, the addend is
1993 stored as a straight 26-bit value in a 32-bit instruction.
1994 (gas makes life simpler for itself by never adjusting a
1995 R_MIPS16_26 reloc to be against a section, so the addend is
1996 always zero). However, the 32 bit instruction is stored as 2
1997 16-bit values, rather than a single 32-bit value. In a
1998 big-endian file, the result is the same; in a little-endian
1999 file, the two 16-bit halves of the 32 bit value are swapped.
2000 This is so that a disassembler can recognize the jal
2001 instruction.
2003 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2004 instruction stored as two 16-bit values. The addend A is the
2005 contents of the targ26 field. The calculation is the same as
2006 R_MIPS_26. When storing the calculated value, reorder the
2007 immediate value as shown above, and don't forget to store the
2008 value as two 16-bit values.
2010 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2011 defined as
2013 big-endian:
2014 +--------+----------------------+
2015 | | |
2016 | | targ26-16 |
2017 |31 26|25 0|
2018 +--------+----------------------+
2020 little-endian:
2021 +----------+------+-------------+
2022 | | | |
2023 | sub1 | | sub2 |
2024 |0 9|10 15|16 31|
2025 +----------+--------------------+
2026 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2027 ((sub1 << 16) | sub2)).
2029 When producing a relocatable object file, the calculation is
2030 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2031 When producing a fully linked file, the calculation is
2032 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2033 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2035 The table below lists the other MIPS16 instruction relocations.
2036 Each one is calculated in the same way as the non-MIPS16 relocation
2037 given on the right, but using the extended MIPS16 layout of 16-bit
2038 immediate fields:
2040 R_MIPS16_GPREL R_MIPS_GPREL16
2041 R_MIPS16_GOT16 R_MIPS_GOT16
2042 R_MIPS16_CALL16 R_MIPS_CALL16
2043 R_MIPS16_HI16 R_MIPS_HI16
2044 R_MIPS16_LO16 R_MIPS_LO16
2046 A typical instruction will have a format like this:
2048 +--------------+--------------------------------+
2049 | EXTEND | Imm 10:5 | Imm 15:11 |
2050 +--------------+--------------------------------+
2051 | Major | rx | ry | Imm 4:0 |
2052 +--------------+--------------------------------+
2054 EXTEND is the five bit value 11110. Major is the instruction
2055 opcode.
2057 All we need to do here is shuffle the bits appropriately.
2058 As above, the two 16-bit halves must be swapped on a
2059 little-endian system. */
2061 static inline bfd_boolean
2062 mips16_reloc_p (int r_type)
2064 switch (r_type)
2066 case R_MIPS16_26:
2067 case R_MIPS16_GPREL:
2068 case R_MIPS16_GOT16:
2069 case R_MIPS16_CALL16:
2070 case R_MIPS16_HI16:
2071 case R_MIPS16_LO16:
2072 case R_MIPS16_TLS_GD:
2073 case R_MIPS16_TLS_LDM:
2074 case R_MIPS16_TLS_DTPREL_HI16:
2075 case R_MIPS16_TLS_DTPREL_LO16:
2076 case R_MIPS16_TLS_GOTTPREL:
2077 case R_MIPS16_TLS_TPREL_HI16:
2078 case R_MIPS16_TLS_TPREL_LO16:
2079 return TRUE;
2081 default:
2082 return FALSE;
2086 /* Check if a microMIPS reloc. */
2088 static inline bfd_boolean
2089 micromips_reloc_p (unsigned int r_type)
2091 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2094 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2095 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2096 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2098 static inline bfd_boolean
2099 micromips_reloc_shuffle_p (unsigned int r_type)
2101 return (micromips_reloc_p (r_type)
2102 && r_type != R_MICROMIPS_PC7_S1
2103 && r_type != R_MICROMIPS_PC10_S1);
2106 static inline bfd_boolean
2107 got16_reloc_p (int r_type)
2109 return (r_type == R_MIPS_GOT16
2110 || r_type == R_MIPS16_GOT16
2111 || r_type == R_MICROMIPS_GOT16);
2114 static inline bfd_boolean
2115 call16_reloc_p (int r_type)
2117 return (r_type == R_MIPS_CALL16
2118 || r_type == R_MIPS16_CALL16
2119 || r_type == R_MICROMIPS_CALL16);
2122 static inline bfd_boolean
2123 got_disp_reloc_p (unsigned int r_type)
2125 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2128 static inline bfd_boolean
2129 got_page_reloc_p (unsigned int r_type)
2131 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2134 static inline bfd_boolean
2135 got_ofst_reloc_p (unsigned int r_type)
2137 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
2140 static inline bfd_boolean
2141 got_hi16_reloc_p (unsigned int r_type)
2143 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
2146 static inline bfd_boolean
2147 got_lo16_reloc_p (unsigned int r_type)
2149 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2152 static inline bfd_boolean
2153 call_hi16_reloc_p (unsigned int r_type)
2155 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2158 static inline bfd_boolean
2159 call_lo16_reloc_p (unsigned int r_type)
2161 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2164 static inline bfd_boolean
2165 hi16_reloc_p (int r_type)
2167 return (r_type == R_MIPS_HI16
2168 || r_type == R_MIPS16_HI16
2169 || r_type == R_MICROMIPS_HI16);
2172 static inline bfd_boolean
2173 lo16_reloc_p (int r_type)
2175 return (r_type == R_MIPS_LO16
2176 || r_type == R_MIPS16_LO16
2177 || r_type == R_MICROMIPS_LO16);
2180 static inline bfd_boolean
2181 mips16_call_reloc_p (int r_type)
2183 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2186 static inline bfd_boolean
2187 jal_reloc_p (int r_type)
2189 return (r_type == R_MIPS_26
2190 || r_type == R_MIPS16_26
2191 || r_type == R_MICROMIPS_26_S1);
2194 static inline bfd_boolean
2195 micromips_branch_reloc_p (int r_type)
2197 return (r_type == R_MICROMIPS_26_S1
2198 || r_type == R_MICROMIPS_PC16_S1
2199 || r_type == R_MICROMIPS_PC10_S1
2200 || r_type == R_MICROMIPS_PC7_S1);
2203 static inline bfd_boolean
2204 tls_gd_reloc_p (unsigned int r_type)
2206 return (r_type == R_MIPS_TLS_GD
2207 || r_type == R_MIPS16_TLS_GD
2208 || r_type == R_MICROMIPS_TLS_GD);
2211 static inline bfd_boolean
2212 tls_ldm_reloc_p (unsigned int r_type)
2214 return (r_type == R_MIPS_TLS_LDM
2215 || r_type == R_MIPS16_TLS_LDM
2216 || r_type == R_MICROMIPS_TLS_LDM);
2219 static inline bfd_boolean
2220 tls_gottprel_reloc_p (unsigned int r_type)
2222 return (r_type == R_MIPS_TLS_GOTTPREL
2223 || r_type == R_MIPS16_TLS_GOTTPREL
2224 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2227 void
2228 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2229 bfd_boolean jal_shuffle, bfd_byte *data)
2231 bfd_vma first, second, val;
2233 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2234 return;
2236 /* Pick up the first and second halfwords of the instruction. */
2237 first = bfd_get_16 (abfd, data);
2238 second = bfd_get_16 (abfd, data + 2);
2239 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2240 val = first << 16 | second;
2241 else if (r_type != R_MIPS16_26)
2242 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2243 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2244 else
2245 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2246 | ((first & 0x1f) << 21) | second);
2247 bfd_put_32 (abfd, val, data);
2250 void
2251 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2252 bfd_boolean jal_shuffle, bfd_byte *data)
2254 bfd_vma first, second, val;
2256 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2257 return;
2259 val = bfd_get_32 (abfd, data);
2260 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2262 second = val & 0xffff;
2263 first = val >> 16;
2265 else if (r_type != R_MIPS16_26)
2267 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2268 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2270 else
2272 second = val & 0xffff;
2273 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2274 | ((val >> 21) & 0x1f);
2276 bfd_put_16 (abfd, second, data + 2);
2277 bfd_put_16 (abfd, first, data);
2280 bfd_reloc_status_type
2281 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2282 arelent *reloc_entry, asection *input_section,
2283 bfd_boolean relocatable, void *data, bfd_vma gp)
2285 bfd_vma relocation;
2286 bfd_signed_vma val;
2287 bfd_reloc_status_type status;
2289 if (bfd_is_com_section (symbol->section))
2290 relocation = 0;
2291 else
2292 relocation = symbol->value;
2294 relocation += symbol->section->output_section->vma;
2295 relocation += symbol->section->output_offset;
2297 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2298 return bfd_reloc_outofrange;
2300 /* Set val to the offset into the section or symbol. */
2301 val = reloc_entry->addend;
2303 _bfd_mips_elf_sign_extend (val, 16);
2305 /* Adjust val for the final section location and GP value. If we
2306 are producing relocatable output, we don't want to do this for
2307 an external symbol. */
2308 if (! relocatable
2309 || (symbol->flags & BSF_SECTION_SYM) != 0)
2310 val += relocation - gp;
2312 if (reloc_entry->howto->partial_inplace)
2314 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2315 (bfd_byte *) data
2316 + reloc_entry->address);
2317 if (status != bfd_reloc_ok)
2318 return status;
2320 else
2321 reloc_entry->addend = val;
2323 if (relocatable)
2324 reloc_entry->address += input_section->output_offset;
2326 return bfd_reloc_ok;
2329 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2330 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2331 that contains the relocation field and DATA points to the start of
2332 INPUT_SECTION. */
2334 struct mips_hi16
2336 struct mips_hi16 *next;
2337 bfd_byte *data;
2338 asection *input_section;
2339 arelent rel;
2342 /* FIXME: This should not be a static variable. */
2344 static struct mips_hi16 *mips_hi16_list;
2346 /* A howto special_function for REL *HI16 relocations. We can only
2347 calculate the correct value once we've seen the partnering
2348 *LO16 relocation, so just save the information for later.
2350 The ABI requires that the *LO16 immediately follow the *HI16.
2351 However, as a GNU extension, we permit an arbitrary number of
2352 *HI16s to be associated with a single *LO16. This significantly
2353 simplies the relocation handling in gcc. */
2355 bfd_reloc_status_type
2356 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2357 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2358 asection *input_section, bfd *output_bfd,
2359 char **error_message ATTRIBUTE_UNUSED)
2361 struct mips_hi16 *n;
2363 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2364 return bfd_reloc_outofrange;
2366 n = bfd_malloc (sizeof *n);
2367 if (n == NULL)
2368 return bfd_reloc_outofrange;
2370 n->next = mips_hi16_list;
2371 n->data = data;
2372 n->input_section = input_section;
2373 n->rel = *reloc_entry;
2374 mips_hi16_list = n;
2376 if (output_bfd != NULL)
2377 reloc_entry->address += input_section->output_offset;
2379 return bfd_reloc_ok;
2382 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2383 like any other 16-bit relocation when applied to global symbols, but is
2384 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2386 bfd_reloc_status_type
2387 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2388 void *data, asection *input_section,
2389 bfd *output_bfd, char **error_message)
2391 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2392 || bfd_is_und_section (bfd_get_section (symbol))
2393 || bfd_is_com_section (bfd_get_section (symbol)))
2394 /* The relocation is against a global symbol. */
2395 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2396 input_section, output_bfd,
2397 error_message);
2399 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2400 input_section, output_bfd, error_message);
2403 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2404 is a straightforward 16 bit inplace relocation, but we must deal with
2405 any partnering high-part relocations as well. */
2407 bfd_reloc_status_type
2408 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2409 void *data, asection *input_section,
2410 bfd *output_bfd, char **error_message)
2412 bfd_vma vallo;
2413 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2415 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2416 return bfd_reloc_outofrange;
2418 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2419 location);
2420 vallo = bfd_get_32 (abfd, location);
2421 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2422 location);
2424 while (mips_hi16_list != NULL)
2426 bfd_reloc_status_type ret;
2427 struct mips_hi16 *hi;
2429 hi = mips_hi16_list;
2431 /* R_MIPS*_GOT16 relocations are something of a special case. We
2432 want to install the addend in the same way as for a R_MIPS*_HI16
2433 relocation (with a rightshift of 16). However, since GOT16
2434 relocations can also be used with global symbols, their howto
2435 has a rightshift of 0. */
2436 if (hi->rel.howto->type == R_MIPS_GOT16)
2437 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2438 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2439 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2440 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2441 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2443 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2444 carry or borrow will induce a change of +1 or -1 in the high part. */
2445 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2447 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2448 hi->input_section, output_bfd,
2449 error_message);
2450 if (ret != bfd_reloc_ok)
2451 return ret;
2453 mips_hi16_list = hi->next;
2454 free (hi);
2457 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2458 input_section, output_bfd,
2459 error_message);
2462 /* A generic howto special_function. This calculates and installs the
2463 relocation itself, thus avoiding the oft-discussed problems in
2464 bfd_perform_relocation and bfd_install_relocation. */
2466 bfd_reloc_status_type
2467 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2468 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2469 asection *input_section, bfd *output_bfd,
2470 char **error_message ATTRIBUTE_UNUSED)
2472 bfd_signed_vma val;
2473 bfd_reloc_status_type status;
2474 bfd_boolean relocatable;
2476 relocatable = (output_bfd != NULL);
2478 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2479 return bfd_reloc_outofrange;
2481 /* Build up the field adjustment in VAL. */
2482 val = 0;
2483 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2485 /* Either we're calculating the final field value or we have a
2486 relocation against a section symbol. Add in the section's
2487 offset or address. */
2488 val += symbol->section->output_section->vma;
2489 val += symbol->section->output_offset;
2492 if (!relocatable)
2494 /* We're calculating the final field value. Add in the symbol's value
2495 and, if pc-relative, subtract the address of the field itself. */
2496 val += symbol->value;
2497 if (reloc_entry->howto->pc_relative)
2499 val -= input_section->output_section->vma;
2500 val -= input_section->output_offset;
2501 val -= reloc_entry->address;
2505 /* VAL is now the final adjustment. If we're keeping this relocation
2506 in the output file, and if the relocation uses a separate addend,
2507 we just need to add VAL to that addend. Otherwise we need to add
2508 VAL to the relocation field itself. */
2509 if (relocatable && !reloc_entry->howto->partial_inplace)
2510 reloc_entry->addend += val;
2511 else
2513 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2515 /* Add in the separate addend, if any. */
2516 val += reloc_entry->addend;
2518 /* Add VAL to the relocation field. */
2519 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2520 location);
2521 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2522 location);
2523 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2524 location);
2526 if (status != bfd_reloc_ok)
2527 return status;
2530 if (relocatable)
2531 reloc_entry->address += input_section->output_offset;
2533 return bfd_reloc_ok;
2536 /* Swap an entry in a .gptab section. Note that these routines rely
2537 on the equivalence of the two elements of the union. */
2539 static void
2540 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2541 Elf32_gptab *in)
2543 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2544 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2547 static void
2548 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2549 Elf32_External_gptab *ex)
2551 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2552 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2555 static void
2556 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2557 Elf32_External_compact_rel *ex)
2559 H_PUT_32 (abfd, in->id1, ex->id1);
2560 H_PUT_32 (abfd, in->num, ex->num);
2561 H_PUT_32 (abfd, in->id2, ex->id2);
2562 H_PUT_32 (abfd, in->offset, ex->offset);
2563 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2564 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2567 static void
2568 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2569 Elf32_External_crinfo *ex)
2571 unsigned long l;
2573 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2574 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2575 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2576 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2577 H_PUT_32 (abfd, l, ex->info);
2578 H_PUT_32 (abfd, in->konst, ex->konst);
2579 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2582 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2583 routines swap this structure in and out. They are used outside of
2584 BFD, so they are globally visible. */
2586 void
2587 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2588 Elf32_RegInfo *in)
2590 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2591 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2592 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2593 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2594 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2595 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2598 void
2599 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2600 Elf32_External_RegInfo *ex)
2602 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2603 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2604 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2605 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2606 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2607 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2610 /* In the 64 bit ABI, the .MIPS.options section holds register
2611 information in an Elf64_Reginfo structure. These routines swap
2612 them in and out. They are globally visible because they are used
2613 outside of BFD. These routines are here so that gas can call them
2614 without worrying about whether the 64 bit ABI has been included. */
2616 void
2617 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2618 Elf64_Internal_RegInfo *in)
2620 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2621 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2622 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2623 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2624 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2625 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2626 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2629 void
2630 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2631 Elf64_External_RegInfo *ex)
2633 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2634 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2635 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2636 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2637 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2638 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2639 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2642 /* Swap in an options header. */
2644 void
2645 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2646 Elf_Internal_Options *in)
2648 in->kind = H_GET_8 (abfd, ex->kind);
2649 in->size = H_GET_8 (abfd, ex->size);
2650 in->section = H_GET_16 (abfd, ex->section);
2651 in->info = H_GET_32 (abfd, ex->info);
2654 /* Swap out an options header. */
2656 void
2657 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2658 Elf_External_Options *ex)
2660 H_PUT_8 (abfd, in->kind, ex->kind);
2661 H_PUT_8 (abfd, in->size, ex->size);
2662 H_PUT_16 (abfd, in->section, ex->section);
2663 H_PUT_32 (abfd, in->info, ex->info);
2666 /* This function is called via qsort() to sort the dynamic relocation
2667 entries by increasing r_symndx value. */
2669 static int
2670 sort_dynamic_relocs (const void *arg1, const void *arg2)
2672 Elf_Internal_Rela int_reloc1;
2673 Elf_Internal_Rela int_reloc2;
2674 int diff;
2676 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2677 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2679 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2680 if (diff != 0)
2681 return diff;
2683 if (int_reloc1.r_offset < int_reloc2.r_offset)
2684 return -1;
2685 if (int_reloc1.r_offset > int_reloc2.r_offset)
2686 return 1;
2687 return 0;
2690 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2692 static int
2693 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2694 const void *arg2 ATTRIBUTE_UNUSED)
2696 #ifdef BFD64
2697 Elf_Internal_Rela int_reloc1[3];
2698 Elf_Internal_Rela int_reloc2[3];
2700 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2701 (reldyn_sorting_bfd, arg1, int_reloc1);
2702 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2703 (reldyn_sorting_bfd, arg2, int_reloc2);
2705 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2706 return -1;
2707 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2708 return 1;
2710 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2711 return -1;
2712 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2713 return 1;
2714 return 0;
2715 #else
2716 abort ();
2717 #endif
2721 /* This routine is used to write out ECOFF debugging external symbol
2722 information. It is called via mips_elf_link_hash_traverse. The
2723 ECOFF external symbol information must match the ELF external
2724 symbol information. Unfortunately, at this point we don't know
2725 whether a symbol is required by reloc information, so the two
2726 tables may wind up being different. We must sort out the external
2727 symbol information before we can set the final size of the .mdebug
2728 section, and we must set the size of the .mdebug section before we
2729 can relocate any sections, and we can't know which symbols are
2730 required by relocation until we relocate the sections.
2731 Fortunately, it is relatively unlikely that any symbol will be
2732 stripped but required by a reloc. In particular, it can not happen
2733 when generating a final executable. */
2735 static bfd_boolean
2736 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2738 struct extsym_info *einfo = data;
2739 bfd_boolean strip;
2740 asection *sec, *output_section;
2742 if (h->root.indx == -2)
2743 strip = FALSE;
2744 else if ((h->root.def_dynamic
2745 || h->root.ref_dynamic
2746 || h->root.type == bfd_link_hash_new)
2747 && !h->root.def_regular
2748 && !h->root.ref_regular)
2749 strip = TRUE;
2750 else if (einfo->info->strip == strip_all
2751 || (einfo->info->strip == strip_some
2752 && bfd_hash_lookup (einfo->info->keep_hash,
2753 h->root.root.root.string,
2754 FALSE, FALSE) == NULL))
2755 strip = TRUE;
2756 else
2757 strip = FALSE;
2759 if (strip)
2760 return TRUE;
2762 if (h->esym.ifd == -2)
2764 h->esym.jmptbl = 0;
2765 h->esym.cobol_main = 0;
2766 h->esym.weakext = 0;
2767 h->esym.reserved = 0;
2768 h->esym.ifd = ifdNil;
2769 h->esym.asym.value = 0;
2770 h->esym.asym.st = stGlobal;
2772 if (h->root.root.type == bfd_link_hash_undefined
2773 || h->root.root.type == bfd_link_hash_undefweak)
2775 const char *name;
2777 /* Use undefined class. Also, set class and type for some
2778 special symbols. */
2779 name = h->root.root.root.string;
2780 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2781 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2783 h->esym.asym.sc = scData;
2784 h->esym.asym.st = stLabel;
2785 h->esym.asym.value = 0;
2787 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2789 h->esym.asym.sc = scAbs;
2790 h->esym.asym.st = stLabel;
2791 h->esym.asym.value =
2792 mips_elf_hash_table (einfo->info)->procedure_count;
2794 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2796 h->esym.asym.sc = scAbs;
2797 h->esym.asym.st = stLabel;
2798 h->esym.asym.value = elf_gp (einfo->abfd);
2800 else
2801 h->esym.asym.sc = scUndefined;
2803 else if (h->root.root.type != bfd_link_hash_defined
2804 && h->root.root.type != bfd_link_hash_defweak)
2805 h->esym.asym.sc = scAbs;
2806 else
2808 const char *name;
2810 sec = h->root.root.u.def.section;
2811 output_section = sec->output_section;
2813 /* When making a shared library and symbol h is the one from
2814 the another shared library, OUTPUT_SECTION may be null. */
2815 if (output_section == NULL)
2816 h->esym.asym.sc = scUndefined;
2817 else
2819 name = bfd_section_name (output_section->owner, output_section);
2821 if (strcmp (name, ".text") == 0)
2822 h->esym.asym.sc = scText;
2823 else if (strcmp (name, ".data") == 0)
2824 h->esym.asym.sc = scData;
2825 else if (strcmp (name, ".sdata") == 0)
2826 h->esym.asym.sc = scSData;
2827 else if (strcmp (name, ".rodata") == 0
2828 || strcmp (name, ".rdata") == 0)
2829 h->esym.asym.sc = scRData;
2830 else if (strcmp (name, ".bss") == 0)
2831 h->esym.asym.sc = scBss;
2832 else if (strcmp (name, ".sbss") == 0)
2833 h->esym.asym.sc = scSBss;
2834 else if (strcmp (name, ".init") == 0)
2835 h->esym.asym.sc = scInit;
2836 else if (strcmp (name, ".fini") == 0)
2837 h->esym.asym.sc = scFini;
2838 else
2839 h->esym.asym.sc = scAbs;
2843 h->esym.asym.reserved = 0;
2844 h->esym.asym.index = indexNil;
2847 if (h->root.root.type == bfd_link_hash_common)
2848 h->esym.asym.value = h->root.root.u.c.size;
2849 else if (h->root.root.type == bfd_link_hash_defined
2850 || h->root.root.type == bfd_link_hash_defweak)
2852 if (h->esym.asym.sc == scCommon)
2853 h->esym.asym.sc = scBss;
2854 else if (h->esym.asym.sc == scSCommon)
2855 h->esym.asym.sc = scSBss;
2857 sec = h->root.root.u.def.section;
2858 output_section = sec->output_section;
2859 if (output_section != NULL)
2860 h->esym.asym.value = (h->root.root.u.def.value
2861 + sec->output_offset
2862 + output_section->vma);
2863 else
2864 h->esym.asym.value = 0;
2866 else
2868 struct mips_elf_link_hash_entry *hd = h;
2870 while (hd->root.root.type == bfd_link_hash_indirect)
2871 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2873 if (hd->needs_lazy_stub)
2875 BFD_ASSERT (hd->root.plt.plist != NULL);
2876 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
2877 /* Set type and value for a symbol with a function stub. */
2878 h->esym.asym.st = stProc;
2879 sec = hd->root.root.u.def.section;
2880 if (sec == NULL)
2881 h->esym.asym.value = 0;
2882 else
2884 output_section = sec->output_section;
2885 if (output_section != NULL)
2886 h->esym.asym.value = (hd->root.plt.plist->stub_offset
2887 + sec->output_offset
2888 + output_section->vma);
2889 else
2890 h->esym.asym.value = 0;
2895 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2896 h->root.root.root.string,
2897 &h->esym))
2899 einfo->failed = TRUE;
2900 return FALSE;
2903 return TRUE;
2906 /* A comparison routine used to sort .gptab entries. */
2908 static int
2909 gptab_compare (const void *p1, const void *p2)
2911 const Elf32_gptab *a1 = p1;
2912 const Elf32_gptab *a2 = p2;
2914 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2917 /* Functions to manage the got entry hash table. */
2919 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2920 hash number. */
2922 static INLINE hashval_t
2923 mips_elf_hash_bfd_vma (bfd_vma addr)
2925 #ifdef BFD64
2926 return addr + (addr >> 32);
2927 #else
2928 return addr;
2929 #endif
2932 static hashval_t
2933 mips_elf_got_entry_hash (const void *entry_)
2935 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2937 return (entry->symndx
2938 + ((entry->tls_type == GOT_TLS_LDM) << 18)
2939 + (entry->tls_type == GOT_TLS_LDM ? 0
2940 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2941 : entry->symndx >= 0 ? (entry->abfd->id
2942 + mips_elf_hash_bfd_vma (entry->d.addend))
2943 : entry->d.h->root.root.root.hash));
2946 static int
2947 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2949 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2950 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2952 return (e1->symndx == e2->symndx
2953 && e1->tls_type == e2->tls_type
2954 && (e1->tls_type == GOT_TLS_LDM ? TRUE
2955 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
2956 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
2957 && e1->d.addend == e2->d.addend)
2958 : e2->abfd && e1->d.h == e2->d.h));
2961 static hashval_t
2962 mips_got_page_ref_hash (const void *ref_)
2964 const struct mips_got_page_ref *ref;
2966 ref = (const struct mips_got_page_ref *) ref_;
2967 return ((ref->symndx >= 0
2968 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
2969 : ref->u.h->root.root.root.hash)
2970 + mips_elf_hash_bfd_vma (ref->addend));
2973 static int
2974 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
2976 const struct mips_got_page_ref *ref1, *ref2;
2978 ref1 = (const struct mips_got_page_ref *) ref1_;
2979 ref2 = (const struct mips_got_page_ref *) ref2_;
2980 return (ref1->symndx == ref2->symndx
2981 && (ref1->symndx < 0
2982 ? ref1->u.h == ref2->u.h
2983 : ref1->u.abfd == ref2->u.abfd)
2984 && ref1->addend == ref2->addend);
2987 static hashval_t
2988 mips_got_page_entry_hash (const void *entry_)
2990 const struct mips_got_page_entry *entry;
2992 entry = (const struct mips_got_page_entry *) entry_;
2993 return entry->sec->id;
2996 static int
2997 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
2999 const struct mips_got_page_entry *entry1, *entry2;
3001 entry1 = (const struct mips_got_page_entry *) entry1_;
3002 entry2 = (const struct mips_got_page_entry *) entry2_;
3003 return entry1->sec == entry2->sec;
3006 /* Create and return a new mips_got_info structure. */
3008 static struct mips_got_info *
3009 mips_elf_create_got_info (bfd *abfd)
3011 struct mips_got_info *g;
3013 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3014 if (g == NULL)
3015 return NULL;
3017 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3018 mips_elf_got_entry_eq, NULL);
3019 if (g->got_entries == NULL)
3020 return NULL;
3022 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3023 mips_got_page_ref_eq, NULL);
3024 if (g->got_page_refs == NULL)
3025 return NULL;
3027 return g;
3030 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3031 CREATE_P and if ABFD doesn't already have a GOT. */
3033 static struct mips_got_info *
3034 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3036 struct mips_elf_obj_tdata *tdata;
3038 if (!is_mips_elf (abfd))
3039 return NULL;
3041 tdata = mips_elf_tdata (abfd);
3042 if (!tdata->got && create_p)
3043 tdata->got = mips_elf_create_got_info (abfd);
3044 return tdata->got;
3047 /* Record that ABFD should use output GOT G. */
3049 static void
3050 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3052 struct mips_elf_obj_tdata *tdata;
3054 BFD_ASSERT (is_mips_elf (abfd));
3055 tdata = mips_elf_tdata (abfd);
3056 if (tdata->got)
3058 /* The GOT structure itself and the hash table entries are
3059 allocated to a bfd, but the hash tables aren't. */
3060 htab_delete (tdata->got->got_entries);
3061 htab_delete (tdata->got->got_page_refs);
3062 if (tdata->got->got_page_entries)
3063 htab_delete (tdata->got->got_page_entries);
3065 tdata->got = g;
3068 /* Return the dynamic relocation section. If it doesn't exist, try to
3069 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3070 if creation fails. */
3072 static asection *
3073 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
3075 const char *dname;
3076 asection *sreloc;
3077 bfd *dynobj;
3079 dname = MIPS_ELF_REL_DYN_NAME (info);
3080 dynobj = elf_hash_table (info)->dynobj;
3081 sreloc = bfd_get_linker_section (dynobj, dname);
3082 if (sreloc == NULL && create_p)
3084 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3085 (SEC_ALLOC
3086 | SEC_LOAD
3087 | SEC_HAS_CONTENTS
3088 | SEC_IN_MEMORY
3089 | SEC_LINKER_CREATED
3090 | SEC_READONLY));
3091 if (sreloc == NULL
3092 || ! bfd_set_section_alignment (dynobj, sreloc,
3093 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3094 return NULL;
3096 return sreloc;
3099 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3101 static int
3102 mips_elf_reloc_tls_type (unsigned int r_type)
3104 if (tls_gd_reloc_p (r_type))
3105 return GOT_TLS_GD;
3107 if (tls_ldm_reloc_p (r_type))
3108 return GOT_TLS_LDM;
3110 if (tls_gottprel_reloc_p (r_type))
3111 return GOT_TLS_IE;
3113 return GOT_TLS_NONE;
3116 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3118 static int
3119 mips_tls_got_entries (unsigned int type)
3121 switch (type)
3123 case GOT_TLS_GD:
3124 case GOT_TLS_LDM:
3125 return 2;
3127 case GOT_TLS_IE:
3128 return 1;
3130 case GOT_TLS_NONE:
3131 return 0;
3133 abort ();
3136 /* Count the number of relocations needed for a TLS GOT entry, with
3137 access types from TLS_TYPE, and symbol H (or a local symbol if H
3138 is NULL). */
3140 static int
3141 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3142 struct elf_link_hash_entry *h)
3144 int indx = 0;
3145 bfd_boolean need_relocs = FALSE;
3146 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3148 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3149 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
3150 indx = h->dynindx;
3152 if ((info->shared || indx != 0)
3153 && (h == NULL
3154 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3155 || h->root.type != bfd_link_hash_undefweak))
3156 need_relocs = TRUE;
3158 if (!need_relocs)
3159 return 0;
3161 switch (tls_type)
3163 case GOT_TLS_GD:
3164 return indx != 0 ? 2 : 1;
3166 case GOT_TLS_IE:
3167 return 1;
3169 case GOT_TLS_LDM:
3170 return info->shared ? 1 : 0;
3172 default:
3173 return 0;
3177 /* Add the number of GOT entries and TLS relocations required by ENTRY
3178 to G. */
3180 static void
3181 mips_elf_count_got_entry (struct bfd_link_info *info,
3182 struct mips_got_info *g,
3183 struct mips_got_entry *entry)
3185 if (entry->tls_type)
3187 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3188 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3189 entry->symndx < 0
3190 ? &entry->d.h->root : NULL);
3192 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3193 g->local_gotno += 1;
3194 else
3195 g->global_gotno += 1;
3198 /* Output a simple dynamic relocation into SRELOC. */
3200 static void
3201 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3202 asection *sreloc,
3203 unsigned long reloc_index,
3204 unsigned long indx,
3205 int r_type,
3206 bfd_vma offset)
3208 Elf_Internal_Rela rel[3];
3210 memset (rel, 0, sizeof (rel));
3212 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3213 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3215 if (ABI_64_P (output_bfd))
3217 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3218 (output_bfd, &rel[0],
3219 (sreloc->contents
3220 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3222 else
3223 bfd_elf32_swap_reloc_out
3224 (output_bfd, &rel[0],
3225 (sreloc->contents
3226 + reloc_index * sizeof (Elf32_External_Rel)));
3229 /* Initialize a set of TLS GOT entries for one symbol. */
3231 static void
3232 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3233 struct mips_got_entry *entry,
3234 struct mips_elf_link_hash_entry *h,
3235 bfd_vma value)
3237 struct mips_elf_link_hash_table *htab;
3238 int indx;
3239 asection *sreloc, *sgot;
3240 bfd_vma got_offset, got_offset2;
3241 bfd_boolean need_relocs = FALSE;
3243 htab = mips_elf_hash_table (info);
3244 if (htab == NULL)
3245 return;
3247 sgot = htab->sgot;
3249 indx = 0;
3250 if (h != NULL)
3252 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3254 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3255 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3256 indx = h->root.dynindx;
3259 if (entry->tls_initialized)
3260 return;
3262 if ((info->shared || indx != 0)
3263 && (h == NULL
3264 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3265 || h->root.type != bfd_link_hash_undefweak))
3266 need_relocs = TRUE;
3268 /* MINUS_ONE means the symbol is not defined in this object. It may not
3269 be defined at all; assume that the value doesn't matter in that
3270 case. Otherwise complain if we would use the value. */
3271 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3272 || h->root.root.type == bfd_link_hash_undefweak);
3274 /* Emit necessary relocations. */
3275 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3276 got_offset = entry->gotidx;
3278 switch (entry->tls_type)
3280 case GOT_TLS_GD:
3281 /* General Dynamic. */
3282 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3284 if (need_relocs)
3286 mips_elf_output_dynamic_relocation
3287 (abfd, sreloc, sreloc->reloc_count++, indx,
3288 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3289 sgot->output_offset + sgot->output_section->vma + got_offset);
3291 if (indx)
3292 mips_elf_output_dynamic_relocation
3293 (abfd, sreloc, sreloc->reloc_count++, indx,
3294 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3295 sgot->output_offset + sgot->output_section->vma + got_offset2);
3296 else
3297 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3298 sgot->contents + got_offset2);
3300 else
3302 MIPS_ELF_PUT_WORD (abfd, 1,
3303 sgot->contents + got_offset);
3304 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3305 sgot->contents + got_offset2);
3307 break;
3309 case GOT_TLS_IE:
3310 /* Initial Exec model. */
3311 if (need_relocs)
3313 if (indx == 0)
3314 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3315 sgot->contents + got_offset);
3316 else
3317 MIPS_ELF_PUT_WORD (abfd, 0,
3318 sgot->contents + got_offset);
3320 mips_elf_output_dynamic_relocation
3321 (abfd, sreloc, sreloc->reloc_count++, indx,
3322 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3323 sgot->output_offset + sgot->output_section->vma + got_offset);
3325 else
3326 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3327 sgot->contents + got_offset);
3328 break;
3330 case GOT_TLS_LDM:
3331 /* The initial offset is zero, and the LD offsets will include the
3332 bias by DTP_OFFSET. */
3333 MIPS_ELF_PUT_WORD (abfd, 0,
3334 sgot->contents + got_offset
3335 + MIPS_ELF_GOT_SIZE (abfd));
3337 if (!info->shared)
3338 MIPS_ELF_PUT_WORD (abfd, 1,
3339 sgot->contents + got_offset);
3340 else
3341 mips_elf_output_dynamic_relocation
3342 (abfd, sreloc, sreloc->reloc_count++, indx,
3343 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3344 sgot->output_offset + sgot->output_section->vma + got_offset);
3345 break;
3347 default:
3348 abort ();
3351 entry->tls_initialized = TRUE;
3354 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3355 for global symbol H. .got.plt comes before the GOT, so the offset
3356 will be negative. */
3358 static bfd_vma
3359 mips_elf_gotplt_index (struct bfd_link_info *info,
3360 struct elf_link_hash_entry *h)
3362 bfd_vma got_address, got_value;
3363 struct mips_elf_link_hash_table *htab;
3365 htab = mips_elf_hash_table (info);
3366 BFD_ASSERT (htab != NULL);
3368 BFD_ASSERT (h->plt.plist != NULL);
3369 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3371 /* Calculate the address of the associated .got.plt entry. */
3372 got_address = (htab->sgotplt->output_section->vma
3373 + htab->sgotplt->output_offset
3374 + (h->plt.plist->gotplt_index
3375 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3377 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3378 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3379 + htab->root.hgot->root.u.def.section->output_offset
3380 + htab->root.hgot->root.u.def.value);
3382 return got_address - got_value;
3385 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3386 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3387 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3388 offset can be found. */
3390 static bfd_vma
3391 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3392 bfd_vma value, unsigned long r_symndx,
3393 struct mips_elf_link_hash_entry *h, int r_type)
3395 struct mips_elf_link_hash_table *htab;
3396 struct mips_got_entry *entry;
3398 htab = mips_elf_hash_table (info);
3399 BFD_ASSERT (htab != NULL);
3401 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3402 r_symndx, h, r_type);
3403 if (!entry)
3404 return MINUS_ONE;
3406 if (entry->tls_type)
3407 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3408 return entry->gotidx;
3411 /* Return the GOT index of global symbol H in the primary GOT. */
3413 static bfd_vma
3414 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3415 struct elf_link_hash_entry *h)
3417 struct mips_elf_link_hash_table *htab;
3418 long global_got_dynindx;
3419 struct mips_got_info *g;
3420 bfd_vma got_index;
3422 htab = mips_elf_hash_table (info);
3423 BFD_ASSERT (htab != NULL);
3425 global_got_dynindx = 0;
3426 if (htab->global_gotsym != NULL)
3427 global_got_dynindx = htab->global_gotsym->dynindx;
3429 /* Once we determine the global GOT entry with the lowest dynamic
3430 symbol table index, we must put all dynamic symbols with greater
3431 indices into the primary GOT. That makes it easy to calculate the
3432 GOT offset. */
3433 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3434 g = mips_elf_bfd_got (obfd, FALSE);
3435 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3436 * MIPS_ELF_GOT_SIZE (obfd));
3437 BFD_ASSERT (got_index < htab->sgot->size);
3439 return got_index;
3442 /* Return the GOT index for the global symbol indicated by H, which is
3443 referenced by a relocation of type R_TYPE in IBFD. */
3445 static bfd_vma
3446 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3447 struct elf_link_hash_entry *h, int r_type)
3449 struct mips_elf_link_hash_table *htab;
3450 struct mips_got_info *g;
3451 struct mips_got_entry lookup, *entry;
3452 bfd_vma gotidx;
3454 htab = mips_elf_hash_table (info);
3455 BFD_ASSERT (htab != NULL);
3457 g = mips_elf_bfd_got (ibfd, FALSE);
3458 BFD_ASSERT (g);
3460 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3461 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3462 return mips_elf_primary_global_got_index (obfd, info, h);
3464 lookup.abfd = ibfd;
3465 lookup.symndx = -1;
3466 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3467 entry = htab_find (g->got_entries, &lookup);
3468 BFD_ASSERT (entry);
3470 gotidx = entry->gotidx;
3471 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3473 if (lookup.tls_type)
3475 bfd_vma value = MINUS_ONE;
3477 if ((h->root.type == bfd_link_hash_defined
3478 || h->root.type == bfd_link_hash_defweak)
3479 && h->root.u.def.section->output_section)
3480 value = (h->root.u.def.value
3481 + h->root.u.def.section->output_offset
3482 + h->root.u.def.section->output_section->vma);
3484 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3486 return gotidx;
3489 /* Find a GOT page entry that points to within 32KB of VALUE. These
3490 entries are supposed to be placed at small offsets in the GOT, i.e.,
3491 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3492 entry could be created. If OFFSETP is nonnull, use it to return the
3493 offset of the GOT entry from VALUE. */
3495 static bfd_vma
3496 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3497 bfd_vma value, bfd_vma *offsetp)
3499 bfd_vma page, got_index;
3500 struct mips_got_entry *entry;
3502 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3503 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3504 NULL, R_MIPS_GOT_PAGE);
3506 if (!entry)
3507 return MINUS_ONE;
3509 got_index = entry->gotidx;
3511 if (offsetp)
3512 *offsetp = value - entry->d.address;
3514 return got_index;
3517 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3518 EXTERNAL is true if the relocation was originally against a global
3519 symbol that binds locally. */
3521 static bfd_vma
3522 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3523 bfd_vma value, bfd_boolean external)
3525 struct mips_got_entry *entry;
3527 /* GOT16 relocations against local symbols are followed by a LO16
3528 relocation; those against global symbols are not. Thus if the
3529 symbol was originally local, the GOT16 relocation should load the
3530 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3531 if (! external)
3532 value = mips_elf_high (value) << 16;
3534 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3535 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3536 same in all cases. */
3537 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3538 NULL, R_MIPS_GOT16);
3539 if (entry)
3540 return entry->gotidx;
3541 else
3542 return MINUS_ONE;
3545 /* Returns the offset for the entry at the INDEXth position
3546 in the GOT. */
3548 static bfd_vma
3549 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3550 bfd *input_bfd, bfd_vma got_index)
3552 struct mips_elf_link_hash_table *htab;
3553 asection *sgot;
3554 bfd_vma gp;
3556 htab = mips_elf_hash_table (info);
3557 BFD_ASSERT (htab != NULL);
3559 sgot = htab->sgot;
3560 gp = _bfd_get_gp_value (output_bfd)
3561 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3563 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3566 /* Create and return a local GOT entry for VALUE, which was calculated
3567 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3568 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3569 instead. */
3571 static struct mips_got_entry *
3572 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3573 bfd *ibfd, bfd_vma value,
3574 unsigned long r_symndx,
3575 struct mips_elf_link_hash_entry *h,
3576 int r_type)
3578 struct mips_got_entry lookup, *entry;
3579 void **loc;
3580 struct mips_got_info *g;
3581 struct mips_elf_link_hash_table *htab;
3582 bfd_vma gotidx;
3584 htab = mips_elf_hash_table (info);
3585 BFD_ASSERT (htab != NULL);
3587 g = mips_elf_bfd_got (ibfd, FALSE);
3588 if (g == NULL)
3590 g = mips_elf_bfd_got (abfd, FALSE);
3591 BFD_ASSERT (g != NULL);
3594 /* This function shouldn't be called for symbols that live in the global
3595 area of the GOT. */
3596 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3598 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3599 if (lookup.tls_type)
3601 lookup.abfd = ibfd;
3602 if (tls_ldm_reloc_p (r_type))
3604 lookup.symndx = 0;
3605 lookup.d.addend = 0;
3607 else if (h == NULL)
3609 lookup.symndx = r_symndx;
3610 lookup.d.addend = 0;
3612 else
3614 lookup.symndx = -1;
3615 lookup.d.h = h;
3618 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3619 BFD_ASSERT (entry);
3621 gotidx = entry->gotidx;
3622 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3624 return entry;
3627 lookup.abfd = NULL;
3628 lookup.symndx = -1;
3629 lookup.d.address = value;
3630 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3631 if (!loc)
3632 return NULL;
3634 entry = (struct mips_got_entry *) *loc;
3635 if (entry)
3636 return entry;
3638 if (g->assigned_gotno >= g->local_gotno)
3640 /* We didn't allocate enough space in the GOT. */
3641 (*_bfd_error_handler)
3642 (_("not enough GOT space for local GOT entries"));
3643 bfd_set_error (bfd_error_bad_value);
3644 return NULL;
3647 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3648 if (!entry)
3649 return NULL;
3651 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_gotno++;
3652 *entry = lookup;
3653 *loc = entry;
3655 MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
3657 /* These GOT entries need a dynamic relocation on VxWorks. */
3658 if (htab->is_vxworks)
3660 Elf_Internal_Rela outrel;
3661 asection *s;
3662 bfd_byte *rloc;
3663 bfd_vma got_address;
3665 s = mips_elf_rel_dyn_section (info, FALSE);
3666 got_address = (htab->sgot->output_section->vma
3667 + htab->sgot->output_offset
3668 + entry->gotidx);
3670 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3671 outrel.r_offset = got_address;
3672 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3673 outrel.r_addend = value;
3674 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3677 return entry;
3680 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3681 The number might be exact or a worst-case estimate, depending on how
3682 much information is available to elf_backend_omit_section_dynsym at
3683 the current linking stage. */
3685 static bfd_size_type
3686 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3688 bfd_size_type count;
3690 count = 0;
3691 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3693 asection *p;
3694 const struct elf_backend_data *bed;
3696 bed = get_elf_backend_data (output_bfd);
3697 for (p = output_bfd->sections; p ; p = p->next)
3698 if ((p->flags & SEC_EXCLUDE) == 0
3699 && (p->flags & SEC_ALLOC) != 0
3700 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3701 ++count;
3703 return count;
3706 /* Sort the dynamic symbol table so that symbols that need GOT entries
3707 appear towards the end. */
3709 static bfd_boolean
3710 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3712 struct mips_elf_link_hash_table *htab;
3713 struct mips_elf_hash_sort_data hsd;
3714 struct mips_got_info *g;
3716 if (elf_hash_table (info)->dynsymcount == 0)
3717 return TRUE;
3719 htab = mips_elf_hash_table (info);
3720 BFD_ASSERT (htab != NULL);
3722 g = htab->got_info;
3723 if (g == NULL)
3724 return TRUE;
3726 hsd.low = NULL;
3727 hsd.max_unref_got_dynindx
3728 = hsd.min_got_dynindx
3729 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3730 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3731 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3732 elf_hash_table (info)),
3733 mips_elf_sort_hash_table_f,
3734 &hsd);
3736 /* There should have been enough room in the symbol table to
3737 accommodate both the GOT and non-GOT symbols. */
3738 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3739 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3740 == elf_hash_table (info)->dynsymcount);
3741 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3742 == g->global_gotno);
3744 /* Now we know which dynamic symbol has the lowest dynamic symbol
3745 table index in the GOT. */
3746 htab->global_gotsym = hsd.low;
3748 return TRUE;
3751 /* If H needs a GOT entry, assign it the highest available dynamic
3752 index. Otherwise, assign it the lowest available dynamic
3753 index. */
3755 static bfd_boolean
3756 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3758 struct mips_elf_hash_sort_data *hsd = data;
3760 /* Symbols without dynamic symbol table entries aren't interesting
3761 at all. */
3762 if (h->root.dynindx == -1)
3763 return TRUE;
3765 switch (h->global_got_area)
3767 case GGA_NONE:
3768 h->root.dynindx = hsd->max_non_got_dynindx++;
3769 break;
3771 case GGA_NORMAL:
3772 h->root.dynindx = --hsd->min_got_dynindx;
3773 hsd->low = (struct elf_link_hash_entry *) h;
3774 break;
3776 case GGA_RELOC_ONLY:
3777 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3778 hsd->low = (struct elf_link_hash_entry *) h;
3779 h->root.dynindx = hsd->max_unref_got_dynindx++;
3780 break;
3783 return TRUE;
3786 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3787 (which is owned by the caller and shouldn't be added to the
3788 hash table directly). */
3790 static bfd_boolean
3791 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3792 struct mips_got_entry *lookup)
3794 struct mips_elf_link_hash_table *htab;
3795 struct mips_got_entry *entry;
3796 struct mips_got_info *g;
3797 void **loc, **bfd_loc;
3799 /* Make sure there's a slot for this entry in the master GOT. */
3800 htab = mips_elf_hash_table (info);
3801 g = htab->got_info;
3802 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3803 if (!loc)
3804 return FALSE;
3806 /* Populate the entry if it isn't already. */
3807 entry = (struct mips_got_entry *) *loc;
3808 if (!entry)
3810 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3811 if (!entry)
3812 return FALSE;
3814 lookup->tls_initialized = FALSE;
3815 lookup->gotidx = -1;
3816 *entry = *lookup;
3817 *loc = entry;
3820 /* Reuse the same GOT entry for the BFD's GOT. */
3821 g = mips_elf_bfd_got (abfd, TRUE);
3822 if (!g)
3823 return FALSE;
3825 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3826 if (!bfd_loc)
3827 return FALSE;
3829 if (!*bfd_loc)
3830 *bfd_loc = entry;
3831 return TRUE;
3834 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3835 entry for it. FOR_CALL is true if the caller is only interested in
3836 using the GOT entry for calls. */
3838 static bfd_boolean
3839 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3840 bfd *abfd, struct bfd_link_info *info,
3841 bfd_boolean for_call, int r_type)
3843 struct mips_elf_link_hash_table *htab;
3844 struct mips_elf_link_hash_entry *hmips;
3845 struct mips_got_entry entry;
3846 unsigned char tls_type;
3848 htab = mips_elf_hash_table (info);
3849 BFD_ASSERT (htab != NULL);
3851 hmips = (struct mips_elf_link_hash_entry *) h;
3852 if (!for_call)
3853 hmips->got_only_for_calls = FALSE;
3855 /* A global symbol in the GOT must also be in the dynamic symbol
3856 table. */
3857 if (h->dynindx == -1)
3859 switch (ELF_ST_VISIBILITY (h->other))
3861 case STV_INTERNAL:
3862 case STV_HIDDEN:
3863 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3864 break;
3866 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3867 return FALSE;
3870 tls_type = mips_elf_reloc_tls_type (r_type);
3871 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
3872 hmips->global_got_area = GGA_NORMAL;
3874 entry.abfd = abfd;
3875 entry.symndx = -1;
3876 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3877 entry.tls_type = tls_type;
3878 return mips_elf_record_got_entry (info, abfd, &entry);
3881 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3882 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
3884 static bfd_boolean
3885 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3886 struct bfd_link_info *info, int r_type)
3888 struct mips_elf_link_hash_table *htab;
3889 struct mips_got_info *g;
3890 struct mips_got_entry entry;
3892 htab = mips_elf_hash_table (info);
3893 BFD_ASSERT (htab != NULL);
3895 g = htab->got_info;
3896 BFD_ASSERT (g != NULL);
3898 entry.abfd = abfd;
3899 entry.symndx = symndx;
3900 entry.d.addend = addend;
3901 entry.tls_type = mips_elf_reloc_tls_type (r_type);
3902 return mips_elf_record_got_entry (info, abfd, &entry);
3905 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
3906 H is the symbol's hash table entry, or null if SYMNDX is local
3907 to ABFD. */
3909 static bfd_boolean
3910 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
3911 long symndx, struct elf_link_hash_entry *h,
3912 bfd_signed_vma addend)
3914 struct mips_elf_link_hash_table *htab;
3915 struct mips_got_info *g1, *g2;
3916 struct mips_got_page_ref lookup, *entry;
3917 void **loc, **bfd_loc;
3919 htab = mips_elf_hash_table (info);
3920 BFD_ASSERT (htab != NULL);
3922 g1 = htab->got_info;
3923 BFD_ASSERT (g1 != NULL);
3925 if (h)
3927 lookup.symndx = -1;
3928 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
3930 else
3932 lookup.symndx = symndx;
3933 lookup.u.abfd = abfd;
3935 lookup.addend = addend;
3936 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
3937 if (loc == NULL)
3938 return FALSE;
3940 entry = (struct mips_got_page_ref *) *loc;
3941 if (!entry)
3943 entry = bfd_alloc (abfd, sizeof (*entry));
3944 if (!entry)
3945 return FALSE;
3947 *entry = lookup;
3948 *loc = entry;
3951 /* Add the same entry to the BFD's GOT. */
3952 g2 = mips_elf_bfd_got (abfd, TRUE);
3953 if (!g2)
3954 return FALSE;
3956 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
3957 if (!bfd_loc)
3958 return FALSE;
3960 if (!*bfd_loc)
3961 *bfd_loc = entry;
3963 return TRUE;
3966 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3968 static void
3969 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3970 unsigned int n)
3972 asection *s;
3973 struct mips_elf_link_hash_table *htab;
3975 htab = mips_elf_hash_table (info);
3976 BFD_ASSERT (htab != NULL);
3978 s = mips_elf_rel_dyn_section (info, FALSE);
3979 BFD_ASSERT (s != NULL);
3981 if (htab->is_vxworks)
3982 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3983 else
3985 if (s->size == 0)
3987 /* Make room for a null element. */
3988 s->size += MIPS_ELF_REL_SIZE (abfd);
3989 ++s->reloc_count;
3991 s->size += n * MIPS_ELF_REL_SIZE (abfd);
3995 /* A htab_traverse callback for GOT entries, with DATA pointing to a
3996 mips_elf_traverse_got_arg structure. Count the number of GOT
3997 entries and TLS relocs. Set DATA->value to true if we need
3998 to resolve indirect or warning symbols and then recreate the GOT. */
4000 static int
4001 mips_elf_check_recreate_got (void **entryp, void *data)
4003 struct mips_got_entry *entry;
4004 struct mips_elf_traverse_got_arg *arg;
4006 entry = (struct mips_got_entry *) *entryp;
4007 arg = (struct mips_elf_traverse_got_arg *) data;
4008 if (entry->abfd != NULL && entry->symndx == -1)
4010 struct mips_elf_link_hash_entry *h;
4012 h = entry->d.h;
4013 if (h->root.root.type == bfd_link_hash_indirect
4014 || h->root.root.type == bfd_link_hash_warning)
4016 arg->value = TRUE;
4017 return 0;
4020 mips_elf_count_got_entry (arg->info, arg->g, entry);
4021 return 1;
4024 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4025 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4026 converting entries for indirect and warning symbols into entries
4027 for the target symbol. Set DATA->g to null on error. */
4029 static int
4030 mips_elf_recreate_got (void **entryp, void *data)
4032 struct mips_got_entry new_entry, *entry;
4033 struct mips_elf_traverse_got_arg *arg;
4034 void **slot;
4036 entry = (struct mips_got_entry *) *entryp;
4037 arg = (struct mips_elf_traverse_got_arg *) data;
4038 if (entry->abfd != NULL
4039 && entry->symndx == -1
4040 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4041 || entry->d.h->root.root.type == bfd_link_hash_warning))
4043 struct mips_elf_link_hash_entry *h;
4045 new_entry = *entry;
4046 entry = &new_entry;
4047 h = entry->d.h;
4050 BFD_ASSERT (h->global_got_area == GGA_NONE);
4051 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4053 while (h->root.root.type == bfd_link_hash_indirect
4054 || h->root.root.type == bfd_link_hash_warning);
4055 entry->d.h = h;
4057 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4058 if (slot == NULL)
4060 arg->g = NULL;
4061 return 0;
4063 if (*slot == NULL)
4065 if (entry == &new_entry)
4067 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4068 if (!entry)
4070 arg->g = NULL;
4071 return 0;
4073 *entry = new_entry;
4075 *slot = entry;
4076 mips_elf_count_got_entry (arg->info, arg->g, entry);
4078 return 1;
4081 /* Return the maximum number of GOT page entries required for RANGE. */
4083 static bfd_vma
4084 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4086 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4089 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4091 static bfd_boolean
4092 mips_elf_record_got_page_entry (struct mips_got_info *g,
4093 asection *sec, bfd_signed_vma addend)
4095 struct mips_got_page_entry lookup, *entry;
4096 struct mips_got_page_range **range_ptr, *range;
4097 bfd_vma old_pages, new_pages;
4098 void **loc;
4100 /* Find the mips_got_page_entry hash table entry for this section. */
4101 lookup.sec = sec;
4102 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4103 if (loc == NULL)
4104 return FALSE;
4106 /* Create a mips_got_page_entry if this is the first time we've
4107 seen the section. */
4108 entry = (struct mips_got_page_entry *) *loc;
4109 if (!entry)
4111 entry = bfd_zalloc (sec->owner, sizeof (*entry));
4112 if (!entry)
4113 return FALSE;
4115 entry->sec = sec;
4116 *loc = entry;
4119 /* Skip over ranges whose maximum extent cannot share a page entry
4120 with ADDEND. */
4121 range_ptr = &entry->ranges;
4122 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4123 range_ptr = &(*range_ptr)->next;
4125 /* If we scanned to the end of the list, or found a range whose
4126 minimum extent cannot share a page entry with ADDEND, create
4127 a new singleton range. */
4128 range = *range_ptr;
4129 if (!range || addend < range->min_addend - 0xffff)
4131 range = bfd_zalloc (sec->owner, sizeof (*range));
4132 if (!range)
4133 return FALSE;
4135 range->next = *range_ptr;
4136 range->min_addend = addend;
4137 range->max_addend = addend;
4139 *range_ptr = range;
4140 entry->num_pages++;
4141 g->page_gotno++;
4142 return TRUE;
4145 /* Remember how many pages the old range contributed. */
4146 old_pages = mips_elf_pages_for_range (range);
4148 /* Update the ranges. */
4149 if (addend < range->min_addend)
4150 range->min_addend = addend;
4151 else if (addend > range->max_addend)
4153 if (range->next && addend >= range->next->min_addend - 0xffff)
4155 old_pages += mips_elf_pages_for_range (range->next);
4156 range->max_addend = range->next->max_addend;
4157 range->next = range->next->next;
4159 else
4160 range->max_addend = addend;
4163 /* Record any change in the total estimate. */
4164 new_pages = mips_elf_pages_for_range (range);
4165 if (old_pages != new_pages)
4167 entry->num_pages += new_pages - old_pages;
4168 g->page_gotno += new_pages - old_pages;
4171 return TRUE;
4174 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4175 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4176 whether the page reference described by *REFP needs a GOT page entry,
4177 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4179 static bfd_boolean
4180 mips_elf_resolve_got_page_ref (void **refp, void *data)
4182 struct mips_got_page_ref *ref;
4183 struct mips_elf_traverse_got_arg *arg;
4184 struct mips_elf_link_hash_table *htab;
4185 asection *sec;
4186 bfd_vma addend;
4188 ref = (struct mips_got_page_ref *) *refp;
4189 arg = (struct mips_elf_traverse_got_arg *) data;
4190 htab = mips_elf_hash_table (arg->info);
4192 if (ref->symndx < 0)
4194 struct mips_elf_link_hash_entry *h;
4196 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4197 h = ref->u.h;
4198 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4199 return 1;
4201 /* Ignore undefined symbols; we'll issue an error later if
4202 appropriate. */
4203 if (!((h->root.root.type == bfd_link_hash_defined
4204 || h->root.root.type == bfd_link_hash_defweak)
4205 && h->root.root.u.def.section))
4206 return 1;
4208 sec = h->root.root.u.def.section;
4209 addend = h->root.root.u.def.value + ref->addend;
4211 else
4213 Elf_Internal_Sym *isym;
4215 /* Read in the symbol. */
4216 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4217 ref->symndx);
4218 if (isym == NULL)
4220 arg->g = NULL;
4221 return 0;
4224 /* Get the associated input section. */
4225 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4226 if (sec == NULL)
4228 arg->g = NULL;
4229 return 0;
4232 /* If this is a mergable section, work out the section and offset
4233 of the merged data. For section symbols, the addend specifies
4234 of the offset _of_ the first byte in the data, otherwise it
4235 specifies the offset _from_ the first byte. */
4236 if (sec->flags & SEC_MERGE)
4238 void *secinfo;
4240 secinfo = elf_section_data (sec)->sec_info;
4241 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4242 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4243 isym->st_value + ref->addend);
4244 else
4245 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4246 isym->st_value) + ref->addend;
4248 else
4249 addend = isym->st_value + ref->addend;
4251 if (!mips_elf_record_got_page_entry (arg->g, sec, addend))
4253 arg->g = NULL;
4254 return 0;
4256 return 1;
4259 /* If any entries in G->got_entries are for indirect or warning symbols,
4260 replace them with entries for the target symbol. Convert g->got_page_refs
4261 into got_page_entry structures and estimate the number of page entries
4262 that they require. */
4264 static bfd_boolean
4265 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4266 struct mips_got_info *g)
4268 struct mips_elf_traverse_got_arg tga;
4269 struct mips_got_info oldg;
4271 oldg = *g;
4273 tga.info = info;
4274 tga.g = g;
4275 tga.value = FALSE;
4276 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4277 if (tga.value)
4279 *g = oldg;
4280 g->got_entries = htab_create (htab_size (oldg.got_entries),
4281 mips_elf_got_entry_hash,
4282 mips_elf_got_entry_eq, NULL);
4283 if (!g->got_entries)
4284 return FALSE;
4286 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4287 if (!tga.g)
4288 return FALSE;
4290 htab_delete (oldg.got_entries);
4293 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4294 mips_got_page_entry_eq, NULL);
4295 if (g->got_page_entries == NULL)
4296 return FALSE;
4298 tga.info = info;
4299 tga.g = g;
4300 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4302 return TRUE;
4305 /* Return true if a GOT entry for H should live in the local rather than
4306 global GOT area. */
4308 static bfd_boolean
4309 mips_use_local_got_p (struct bfd_link_info *info,
4310 struct mips_elf_link_hash_entry *h)
4312 /* Symbols that aren't in the dynamic symbol table must live in the
4313 local GOT. This includes symbols that are completely undefined
4314 and which therefore don't bind locally. We'll report undefined
4315 symbols later if appropriate. */
4316 if (h->root.dynindx == -1)
4317 return TRUE;
4319 /* Symbols that bind locally can (and in the case of forced-local
4320 symbols, must) live in the local GOT. */
4321 if (h->got_only_for_calls
4322 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4323 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4324 return TRUE;
4326 /* If this is an executable that must provide a definition of the symbol,
4327 either though PLTs or copy relocations, then that address should go in
4328 the local rather than global GOT. */
4329 if (info->executable && h->has_static_relocs)
4330 return TRUE;
4332 return FALSE;
4335 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4336 link_info structure. Decide whether the hash entry needs an entry in
4337 the global part of the primary GOT, setting global_got_area accordingly.
4338 Count the number of global symbols that are in the primary GOT only
4339 because they have relocations against them (reloc_only_gotno). */
4341 static int
4342 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4344 struct bfd_link_info *info;
4345 struct mips_elf_link_hash_table *htab;
4346 struct mips_got_info *g;
4348 info = (struct bfd_link_info *) data;
4349 htab = mips_elf_hash_table (info);
4350 g = htab->got_info;
4351 if (h->global_got_area != GGA_NONE)
4353 /* Make a final decision about whether the symbol belongs in the
4354 local or global GOT. */
4355 if (mips_use_local_got_p (info, h))
4356 /* The symbol belongs in the local GOT. We no longer need this
4357 entry if it was only used for relocations; those relocations
4358 will be against the null or section symbol instead of H. */
4359 h->global_got_area = GGA_NONE;
4360 else if (htab->is_vxworks
4361 && h->got_only_for_calls
4362 && h->root.plt.plist->mips_offset != MINUS_ONE)
4363 /* On VxWorks, calls can refer directly to the .got.plt entry;
4364 they don't need entries in the regular GOT. .got.plt entries
4365 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4366 h->global_got_area = GGA_NONE;
4367 else if (h->global_got_area == GGA_RELOC_ONLY)
4369 g->reloc_only_gotno++;
4370 g->global_gotno++;
4373 return 1;
4376 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4377 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4379 static int
4380 mips_elf_add_got_entry (void **entryp, void *data)
4382 struct mips_got_entry *entry;
4383 struct mips_elf_traverse_got_arg *arg;
4384 void **slot;
4386 entry = (struct mips_got_entry *) *entryp;
4387 arg = (struct mips_elf_traverse_got_arg *) data;
4388 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4389 if (!slot)
4391 arg->g = NULL;
4392 return 0;
4394 if (!*slot)
4396 *slot = entry;
4397 mips_elf_count_got_entry (arg->info, arg->g, entry);
4399 return 1;
4402 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4403 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4405 static int
4406 mips_elf_add_got_page_entry (void **entryp, void *data)
4408 struct mips_got_page_entry *entry;
4409 struct mips_elf_traverse_got_arg *arg;
4410 void **slot;
4412 entry = (struct mips_got_page_entry *) *entryp;
4413 arg = (struct mips_elf_traverse_got_arg *) data;
4414 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4415 if (!slot)
4417 arg->g = NULL;
4418 return 0;
4420 if (!*slot)
4422 *slot = entry;
4423 arg->g->page_gotno += entry->num_pages;
4425 return 1;
4428 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4429 this would lead to overflow, 1 if they were merged successfully,
4430 and 0 if a merge failed due to lack of memory. (These values are chosen
4431 so that nonnegative return values can be returned by a htab_traverse
4432 callback.) */
4434 static int
4435 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4436 struct mips_got_info *to,
4437 struct mips_elf_got_per_bfd_arg *arg)
4439 struct mips_elf_traverse_got_arg tga;
4440 unsigned int estimate;
4442 /* Work out how many page entries we would need for the combined GOT. */
4443 estimate = arg->max_pages;
4444 if (estimate >= from->page_gotno + to->page_gotno)
4445 estimate = from->page_gotno + to->page_gotno;
4447 /* And conservatively estimate how many local and TLS entries
4448 would be needed. */
4449 estimate += from->local_gotno + to->local_gotno;
4450 estimate += from->tls_gotno + to->tls_gotno;
4452 /* If we're merging with the primary got, any TLS relocations will
4453 come after the full set of global entries. Otherwise estimate those
4454 conservatively as well. */
4455 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4456 estimate += arg->global_count;
4457 else
4458 estimate += from->global_gotno + to->global_gotno;
4460 /* Bail out if the combined GOT might be too big. */
4461 if (estimate > arg->max_count)
4462 return -1;
4464 /* Transfer the bfd's got information from FROM to TO. */
4465 tga.info = arg->info;
4466 tga.g = to;
4467 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4468 if (!tga.g)
4469 return 0;
4471 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4472 if (!tga.g)
4473 return 0;
4475 mips_elf_replace_bfd_got (abfd, to);
4476 return 1;
4479 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4480 as possible of the primary got, since it doesn't require explicit
4481 dynamic relocations, but don't use bfds that would reference global
4482 symbols out of the addressable range. Failing the primary got,
4483 attempt to merge with the current got, or finish the current got
4484 and then make make the new got current. */
4486 static bfd_boolean
4487 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4488 struct mips_elf_got_per_bfd_arg *arg)
4490 unsigned int estimate;
4491 int result;
4493 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4494 return FALSE;
4496 /* Work out the number of page, local and TLS entries. */
4497 estimate = arg->max_pages;
4498 if (estimate > g->page_gotno)
4499 estimate = g->page_gotno;
4500 estimate += g->local_gotno + g->tls_gotno;
4502 /* We place TLS GOT entries after both locals and globals. The globals
4503 for the primary GOT may overflow the normal GOT size limit, so be
4504 sure not to merge a GOT which requires TLS with the primary GOT in that
4505 case. This doesn't affect non-primary GOTs. */
4506 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4508 if (estimate <= arg->max_count)
4510 /* If we don't have a primary GOT, use it as
4511 a starting point for the primary GOT. */
4512 if (!arg->primary)
4514 arg->primary = g;
4515 return TRUE;
4518 /* Try merging with the primary GOT. */
4519 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4520 if (result >= 0)
4521 return result;
4524 /* If we can merge with the last-created got, do it. */
4525 if (arg->current)
4527 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4528 if (result >= 0)
4529 return result;
4532 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4533 fits; if it turns out that it doesn't, we'll get relocation
4534 overflows anyway. */
4535 g->next = arg->current;
4536 arg->current = g;
4538 return TRUE;
4541 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4542 to GOTIDX, duplicating the entry if it has already been assigned
4543 an index in a different GOT. */
4545 static bfd_boolean
4546 mips_elf_set_gotidx (void **entryp, long gotidx)
4548 struct mips_got_entry *entry;
4550 entry = (struct mips_got_entry *) *entryp;
4551 if (entry->gotidx > 0)
4553 struct mips_got_entry *new_entry;
4555 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4556 if (!new_entry)
4557 return FALSE;
4559 *new_entry = *entry;
4560 *entryp = new_entry;
4561 entry = new_entry;
4563 entry->gotidx = gotidx;
4564 return TRUE;
4567 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4568 mips_elf_traverse_got_arg in which DATA->value is the size of one
4569 GOT entry. Set DATA->g to null on failure. */
4571 static int
4572 mips_elf_initialize_tls_index (void **entryp, void *data)
4574 struct mips_got_entry *entry;
4575 struct mips_elf_traverse_got_arg *arg;
4577 /* We're only interested in TLS symbols. */
4578 entry = (struct mips_got_entry *) *entryp;
4579 if (entry->tls_type == GOT_TLS_NONE)
4580 return 1;
4582 arg = (struct mips_elf_traverse_got_arg *) data;
4583 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4585 arg->g = NULL;
4586 return 0;
4589 /* Account for the entries we've just allocated. */
4590 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4591 return 1;
4594 /* A htab_traverse callback for GOT entries, where DATA points to a
4595 mips_elf_traverse_got_arg. Set the global_got_area of each global
4596 symbol to DATA->value. */
4598 static int
4599 mips_elf_set_global_got_area (void **entryp, void *data)
4601 struct mips_got_entry *entry;
4602 struct mips_elf_traverse_got_arg *arg;
4604 entry = (struct mips_got_entry *) *entryp;
4605 arg = (struct mips_elf_traverse_got_arg *) data;
4606 if (entry->abfd != NULL
4607 && entry->symndx == -1
4608 && entry->d.h->global_got_area != GGA_NONE)
4609 entry->d.h->global_got_area = arg->value;
4610 return 1;
4613 /* A htab_traverse callback for secondary GOT entries, where DATA points
4614 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4615 and record the number of relocations they require. DATA->value is
4616 the size of one GOT entry. Set DATA->g to null on failure. */
4618 static int
4619 mips_elf_set_global_gotidx (void **entryp, void *data)
4621 struct mips_got_entry *entry;
4622 struct mips_elf_traverse_got_arg *arg;
4624 entry = (struct mips_got_entry *) *entryp;
4625 arg = (struct mips_elf_traverse_got_arg *) data;
4626 if (entry->abfd != NULL
4627 && entry->symndx == -1
4628 && entry->d.h->global_got_area != GGA_NONE)
4630 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_gotno))
4632 arg->g = NULL;
4633 return 0;
4635 arg->g->assigned_gotno += 1;
4637 if (arg->info->shared
4638 || (elf_hash_table (arg->info)->dynamic_sections_created
4639 && entry->d.h->root.def_dynamic
4640 && !entry->d.h->root.def_regular))
4641 arg->g->relocs += 1;
4644 return 1;
4647 /* A htab_traverse callback for GOT entries for which DATA is the
4648 bfd_link_info. Forbid any global symbols from having traditional
4649 lazy-binding stubs. */
4651 static int
4652 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4654 struct bfd_link_info *info;
4655 struct mips_elf_link_hash_table *htab;
4656 struct mips_got_entry *entry;
4658 entry = (struct mips_got_entry *) *entryp;
4659 info = (struct bfd_link_info *) data;
4660 htab = mips_elf_hash_table (info);
4661 BFD_ASSERT (htab != NULL);
4663 if (entry->abfd != NULL
4664 && entry->symndx == -1
4665 && entry->d.h->needs_lazy_stub)
4667 entry->d.h->needs_lazy_stub = FALSE;
4668 htab->lazy_stub_count--;
4671 return 1;
4674 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4675 the primary GOT. */
4676 static bfd_vma
4677 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4679 if (!g->next)
4680 return 0;
4682 g = mips_elf_bfd_got (ibfd, FALSE);
4683 if (! g)
4684 return 0;
4686 BFD_ASSERT (g->next);
4688 g = g->next;
4690 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4691 * MIPS_ELF_GOT_SIZE (abfd);
4694 /* Turn a single GOT that is too big for 16-bit addressing into
4695 a sequence of GOTs, each one 16-bit addressable. */
4697 static bfd_boolean
4698 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4699 asection *got, bfd_size_type pages)
4701 struct mips_elf_link_hash_table *htab;
4702 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4703 struct mips_elf_traverse_got_arg tga;
4704 struct mips_got_info *g, *gg;
4705 unsigned int assign, needed_relocs;
4706 bfd *dynobj, *ibfd;
4708 dynobj = elf_hash_table (info)->dynobj;
4709 htab = mips_elf_hash_table (info);
4710 BFD_ASSERT (htab != NULL);
4712 g = htab->got_info;
4714 got_per_bfd_arg.obfd = abfd;
4715 got_per_bfd_arg.info = info;
4716 got_per_bfd_arg.current = NULL;
4717 got_per_bfd_arg.primary = NULL;
4718 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4719 / MIPS_ELF_GOT_SIZE (abfd))
4720 - htab->reserved_gotno);
4721 got_per_bfd_arg.max_pages = pages;
4722 /* The number of globals that will be included in the primary GOT.
4723 See the calls to mips_elf_set_global_got_area below for more
4724 information. */
4725 got_per_bfd_arg.global_count = g->global_gotno;
4727 /* Try to merge the GOTs of input bfds together, as long as they
4728 don't seem to exceed the maximum GOT size, choosing one of them
4729 to be the primary GOT. */
4730 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4732 gg = mips_elf_bfd_got (ibfd, FALSE);
4733 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4734 return FALSE;
4737 /* If we do not find any suitable primary GOT, create an empty one. */
4738 if (got_per_bfd_arg.primary == NULL)
4739 g->next = mips_elf_create_got_info (abfd);
4740 else
4741 g->next = got_per_bfd_arg.primary;
4742 g->next->next = got_per_bfd_arg.current;
4744 /* GG is now the master GOT, and G is the primary GOT. */
4745 gg = g;
4746 g = g->next;
4748 /* Map the output bfd to the primary got. That's what we're going
4749 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4750 didn't mark in check_relocs, and we want a quick way to find it.
4751 We can't just use gg->next because we're going to reverse the
4752 list. */
4753 mips_elf_replace_bfd_got (abfd, g);
4755 /* Every symbol that is referenced in a dynamic relocation must be
4756 present in the primary GOT, so arrange for them to appear after
4757 those that are actually referenced. */
4758 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4759 g->global_gotno = gg->global_gotno;
4761 tga.info = info;
4762 tga.value = GGA_RELOC_ONLY;
4763 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4764 tga.value = GGA_NORMAL;
4765 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
4767 /* Now go through the GOTs assigning them offset ranges.
4768 [assigned_gotno, local_gotno[ will be set to the range of local
4769 entries in each GOT. We can then compute the end of a GOT by
4770 adding local_gotno to global_gotno. We reverse the list and make
4771 it circular since then we'll be able to quickly compute the
4772 beginning of a GOT, by computing the end of its predecessor. To
4773 avoid special cases for the primary GOT, while still preserving
4774 assertions that are valid for both single- and multi-got links,
4775 we arrange for the main got struct to have the right number of
4776 global entries, but set its local_gotno such that the initial
4777 offset of the primary GOT is zero. Remember that the primary GOT
4778 will become the last item in the circular linked list, so it
4779 points back to the master GOT. */
4780 gg->local_gotno = -g->global_gotno;
4781 gg->global_gotno = g->global_gotno;
4782 gg->tls_gotno = 0;
4783 assign = 0;
4784 gg->next = gg;
4788 struct mips_got_info *gn;
4790 assign += htab->reserved_gotno;
4791 g->assigned_gotno = assign;
4792 g->local_gotno += assign;
4793 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4794 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4796 /* Take g out of the direct list, and push it onto the reversed
4797 list that gg points to. g->next is guaranteed to be nonnull after
4798 this operation, as required by mips_elf_initialize_tls_index. */
4799 gn = g->next;
4800 g->next = gg->next;
4801 gg->next = g;
4803 /* Set up any TLS entries. We always place the TLS entries after
4804 all non-TLS entries. */
4805 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4806 tga.g = g;
4807 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4808 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4809 if (!tga.g)
4810 return FALSE;
4811 BFD_ASSERT (g->tls_assigned_gotno == assign);
4813 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4814 g = gn;
4816 /* Forbid global symbols in every non-primary GOT from having
4817 lazy-binding stubs. */
4818 if (g)
4819 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4821 while (g);
4823 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
4825 needed_relocs = 0;
4826 for (g = gg->next; g && g->next != gg; g = g->next)
4828 unsigned int save_assign;
4830 /* Assign offsets to global GOT entries and count how many
4831 relocations they need. */
4832 save_assign = g->assigned_gotno;
4833 g->assigned_gotno = g->local_gotno;
4834 tga.info = info;
4835 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4836 tga.g = g;
4837 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
4838 if (!tga.g)
4839 return FALSE;
4840 BFD_ASSERT (g->assigned_gotno == g->local_gotno + g->global_gotno);
4841 g->assigned_gotno = save_assign;
4843 if (info->shared)
4845 g->relocs += g->local_gotno - g->assigned_gotno;
4846 BFD_ASSERT (g->assigned_gotno == g->next->local_gotno
4847 + g->next->global_gotno
4848 + g->next->tls_gotno
4849 + htab->reserved_gotno);
4851 needed_relocs += g->relocs;
4853 needed_relocs += g->relocs;
4855 if (needed_relocs)
4856 mips_elf_allocate_dynamic_relocations (dynobj, info,
4857 needed_relocs);
4859 return TRUE;
4863 /* Returns the first relocation of type r_type found, beginning with
4864 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4866 static const Elf_Internal_Rela *
4867 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4868 const Elf_Internal_Rela *relocation,
4869 const Elf_Internal_Rela *relend)
4871 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4873 while (relocation < relend)
4875 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4876 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4877 return relocation;
4879 ++relocation;
4882 /* We didn't find it. */
4883 return NULL;
4886 /* Return whether an input relocation is against a local symbol. */
4888 static bfd_boolean
4889 mips_elf_local_relocation_p (bfd *input_bfd,
4890 const Elf_Internal_Rela *relocation,
4891 asection **local_sections)
4893 unsigned long r_symndx;
4894 Elf_Internal_Shdr *symtab_hdr;
4895 size_t extsymoff;
4897 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4898 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4899 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4901 if (r_symndx < extsymoff)
4902 return TRUE;
4903 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4904 return TRUE;
4906 return FALSE;
4909 /* Sign-extend VALUE, which has the indicated number of BITS. */
4911 bfd_vma
4912 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4914 if (value & ((bfd_vma) 1 << (bits - 1)))
4915 /* VALUE is negative. */
4916 value |= ((bfd_vma) - 1) << bits;
4918 return value;
4921 /* Return non-zero if the indicated VALUE has overflowed the maximum
4922 range expressible by a signed number with the indicated number of
4923 BITS. */
4925 static bfd_boolean
4926 mips_elf_overflow_p (bfd_vma value, int bits)
4928 bfd_signed_vma svalue = (bfd_signed_vma) value;
4930 if (svalue > (1 << (bits - 1)) - 1)
4931 /* The value is too big. */
4932 return TRUE;
4933 else if (svalue < -(1 << (bits - 1)))
4934 /* The value is too small. */
4935 return TRUE;
4937 /* All is well. */
4938 return FALSE;
4941 /* Calculate the %high function. */
4943 static bfd_vma
4944 mips_elf_high (bfd_vma value)
4946 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4949 /* Calculate the %higher function. */
4951 static bfd_vma
4952 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4954 #ifdef BFD64
4955 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4956 #else
4957 abort ();
4958 return MINUS_ONE;
4959 #endif
4962 /* Calculate the %highest function. */
4964 static bfd_vma
4965 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4967 #ifdef BFD64
4968 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4969 #else
4970 abort ();
4971 return MINUS_ONE;
4972 #endif
4975 /* Create the .compact_rel section. */
4977 static bfd_boolean
4978 mips_elf_create_compact_rel_section
4979 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4981 flagword flags;
4982 register asection *s;
4984 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
4986 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4987 | SEC_READONLY);
4989 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
4990 if (s == NULL
4991 || ! bfd_set_section_alignment (abfd, s,
4992 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4993 return FALSE;
4995 s->size = sizeof (Elf32_External_compact_rel);
4998 return TRUE;
5001 /* Create the .got section to hold the global offset table. */
5003 static bfd_boolean
5004 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5006 flagword flags;
5007 register asection *s;
5008 struct elf_link_hash_entry *h;
5009 struct bfd_link_hash_entry *bh;
5010 struct mips_elf_link_hash_table *htab;
5012 htab = mips_elf_hash_table (info);
5013 BFD_ASSERT (htab != NULL);
5015 /* This function may be called more than once. */
5016 if (htab->sgot)
5017 return TRUE;
5019 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5020 | SEC_LINKER_CREATED);
5022 /* We have to use an alignment of 2**4 here because this is hardcoded
5023 in the function stub generation and in the linker script. */
5024 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5025 if (s == NULL
5026 || ! bfd_set_section_alignment (abfd, s, 4))
5027 return FALSE;
5028 htab->sgot = s;
5030 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5031 linker script because we don't want to define the symbol if we
5032 are not creating a global offset table. */
5033 bh = NULL;
5034 if (! (_bfd_generic_link_add_one_symbol
5035 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5036 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5037 return FALSE;
5039 h = (struct elf_link_hash_entry *) bh;
5040 h->non_elf = 0;
5041 h->def_regular = 1;
5042 h->type = STT_OBJECT;
5043 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5044 elf_hash_table (info)->hgot = h;
5046 if (info->shared
5047 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5048 return FALSE;
5050 htab->got_info = mips_elf_create_got_info (abfd);
5051 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5052 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5054 /* We also need a .got.plt section when generating PLTs. */
5055 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5056 SEC_ALLOC | SEC_LOAD
5057 | SEC_HAS_CONTENTS
5058 | SEC_IN_MEMORY
5059 | SEC_LINKER_CREATED);
5060 if (s == NULL)
5061 return FALSE;
5062 htab->sgotplt = s;
5064 return TRUE;
5067 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5068 __GOTT_INDEX__ symbols. These symbols are only special for
5069 shared objects; they are not used in executables. */
5071 static bfd_boolean
5072 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5074 return (mips_elf_hash_table (info)->is_vxworks
5075 && info->shared
5076 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5077 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5080 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5081 require an la25 stub. See also mips_elf_local_pic_function_p,
5082 which determines whether the destination function ever requires a
5083 stub. */
5085 static bfd_boolean
5086 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5087 bfd_boolean target_is_16_bit_code_p)
5089 /* We specifically ignore branches and jumps from EF_PIC objects,
5090 where the onus is on the compiler or programmer to perform any
5091 necessary initialization of $25. Sometimes such initialization
5092 is unnecessary; for example, -mno-shared functions do not use
5093 the incoming value of $25, and may therefore be called directly. */
5094 if (PIC_OBJECT_P (input_bfd))
5095 return FALSE;
5097 switch (r_type)
5099 case R_MIPS_26:
5100 case R_MIPS_PC16:
5101 case R_MICROMIPS_26_S1:
5102 case R_MICROMIPS_PC7_S1:
5103 case R_MICROMIPS_PC10_S1:
5104 case R_MICROMIPS_PC16_S1:
5105 case R_MICROMIPS_PC23_S2:
5106 return TRUE;
5108 case R_MIPS16_26:
5109 return !target_is_16_bit_code_p;
5111 default:
5112 return FALSE;
5116 /* Calculate the value produced by the RELOCATION (which comes from
5117 the INPUT_BFD). The ADDEND is the addend to use for this
5118 RELOCATION; RELOCATION->R_ADDEND is ignored.
5120 The result of the relocation calculation is stored in VALUEP.
5121 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5122 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5124 This function returns bfd_reloc_continue if the caller need take no
5125 further action regarding this relocation, bfd_reloc_notsupported if
5126 something goes dramatically wrong, bfd_reloc_overflow if an
5127 overflow occurs, and bfd_reloc_ok to indicate success. */
5129 static bfd_reloc_status_type
5130 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5131 asection *input_section,
5132 struct bfd_link_info *info,
5133 const Elf_Internal_Rela *relocation,
5134 bfd_vma addend, reloc_howto_type *howto,
5135 Elf_Internal_Sym *local_syms,
5136 asection **local_sections, bfd_vma *valuep,
5137 const char **namep,
5138 bfd_boolean *cross_mode_jump_p,
5139 bfd_boolean save_addend)
5141 /* The eventual value we will return. */
5142 bfd_vma value;
5143 /* The address of the symbol against which the relocation is
5144 occurring. */
5145 bfd_vma symbol = 0;
5146 /* The final GP value to be used for the relocatable, executable, or
5147 shared object file being produced. */
5148 bfd_vma gp;
5149 /* The place (section offset or address) of the storage unit being
5150 relocated. */
5151 bfd_vma p;
5152 /* The value of GP used to create the relocatable object. */
5153 bfd_vma gp0;
5154 /* The offset into the global offset table at which the address of
5155 the relocation entry symbol, adjusted by the addend, resides
5156 during execution. */
5157 bfd_vma g = MINUS_ONE;
5158 /* The section in which the symbol referenced by the relocation is
5159 located. */
5160 asection *sec = NULL;
5161 struct mips_elf_link_hash_entry *h = NULL;
5162 /* TRUE if the symbol referred to by this relocation is a local
5163 symbol. */
5164 bfd_boolean local_p, was_local_p;
5165 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5166 bfd_boolean gp_disp_p = FALSE;
5167 /* TRUE if the symbol referred to by this relocation is
5168 "__gnu_local_gp". */
5169 bfd_boolean gnu_local_gp_p = FALSE;
5170 Elf_Internal_Shdr *symtab_hdr;
5171 size_t extsymoff;
5172 unsigned long r_symndx;
5173 int r_type;
5174 /* TRUE if overflow occurred during the calculation of the
5175 relocation value. */
5176 bfd_boolean overflowed_p;
5177 /* TRUE if this relocation refers to a MIPS16 function. */
5178 bfd_boolean target_is_16_bit_code_p = FALSE;
5179 bfd_boolean target_is_micromips_code_p = FALSE;
5180 struct mips_elf_link_hash_table *htab;
5181 bfd *dynobj;
5183 dynobj = elf_hash_table (info)->dynobj;
5184 htab = mips_elf_hash_table (info);
5185 BFD_ASSERT (htab != NULL);
5187 /* Parse the relocation. */
5188 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5189 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5190 p = (input_section->output_section->vma
5191 + input_section->output_offset
5192 + relocation->r_offset);
5194 /* Assume that there will be no overflow. */
5195 overflowed_p = FALSE;
5197 /* Figure out whether or not the symbol is local, and get the offset
5198 used in the array of hash table entries. */
5199 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5200 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5201 local_sections);
5202 was_local_p = local_p;
5203 if (! elf_bad_symtab (input_bfd))
5204 extsymoff = symtab_hdr->sh_info;
5205 else
5207 /* The symbol table does not follow the rule that local symbols
5208 must come before globals. */
5209 extsymoff = 0;
5212 /* Figure out the value of the symbol. */
5213 if (local_p)
5215 Elf_Internal_Sym *sym;
5217 sym = local_syms + r_symndx;
5218 sec = local_sections[r_symndx];
5220 symbol = sec->output_section->vma + sec->output_offset;
5221 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5222 || (sec->flags & SEC_MERGE))
5223 symbol += sym->st_value;
5224 if ((sec->flags & SEC_MERGE)
5225 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5227 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5228 addend -= symbol;
5229 addend += sec->output_section->vma + sec->output_offset;
5232 /* MIPS16/microMIPS text labels should be treated as odd. */
5233 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5234 ++symbol;
5236 /* Record the name of this symbol, for our caller. */
5237 *namep = bfd_elf_string_from_elf_section (input_bfd,
5238 symtab_hdr->sh_link,
5239 sym->st_name);
5240 if (*namep == '\0')
5241 *namep = bfd_section_name (input_bfd, sec);
5243 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5244 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5246 else
5248 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5250 /* For global symbols we look up the symbol in the hash-table. */
5251 h = ((struct mips_elf_link_hash_entry *)
5252 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5253 /* Find the real hash-table entry for this symbol. */
5254 while (h->root.root.type == bfd_link_hash_indirect
5255 || h->root.root.type == bfd_link_hash_warning)
5256 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5258 /* Record the name of this symbol, for our caller. */
5259 *namep = h->root.root.root.string;
5261 /* See if this is the special _gp_disp symbol. Note that such a
5262 symbol must always be a global symbol. */
5263 if (strcmp (*namep, "_gp_disp") == 0
5264 && ! NEWABI_P (input_bfd))
5266 /* Relocations against _gp_disp are permitted only with
5267 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5268 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5269 return bfd_reloc_notsupported;
5271 gp_disp_p = TRUE;
5273 /* See if this is the special _gp symbol. Note that such a
5274 symbol must always be a global symbol. */
5275 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5276 gnu_local_gp_p = TRUE;
5279 /* If this symbol is defined, calculate its address. Note that
5280 _gp_disp is a magic symbol, always implicitly defined by the
5281 linker, so it's inappropriate to check to see whether or not
5282 its defined. */
5283 else if ((h->root.root.type == bfd_link_hash_defined
5284 || h->root.root.type == bfd_link_hash_defweak)
5285 && h->root.root.u.def.section)
5287 sec = h->root.root.u.def.section;
5288 if (sec->output_section)
5289 symbol = (h->root.root.u.def.value
5290 + sec->output_section->vma
5291 + sec->output_offset);
5292 else
5293 symbol = h->root.root.u.def.value;
5295 else if (h->root.root.type == bfd_link_hash_undefweak)
5296 /* We allow relocations against undefined weak symbols, giving
5297 it the value zero, so that you can undefined weak functions
5298 and check to see if they exist by looking at their
5299 addresses. */
5300 symbol = 0;
5301 else if (info->unresolved_syms_in_objects == RM_IGNORE
5302 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5303 symbol = 0;
5304 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5305 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5307 /* If this is a dynamic link, we should have created a
5308 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5309 in in _bfd_mips_elf_create_dynamic_sections.
5310 Otherwise, we should define the symbol with a value of 0.
5311 FIXME: It should probably get into the symbol table
5312 somehow as well. */
5313 BFD_ASSERT (! info->shared);
5314 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5315 symbol = 0;
5317 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5319 /* This is an optional symbol - an Irix specific extension to the
5320 ELF spec. Ignore it for now.
5321 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5322 than simply ignoring them, but we do not handle this for now.
5323 For information see the "64-bit ELF Object File Specification"
5324 which is available from here:
5325 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5326 symbol = 0;
5328 else if ((*info->callbacks->undefined_symbol)
5329 (info, h->root.root.root.string, input_bfd,
5330 input_section, relocation->r_offset,
5331 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5332 || ELF_ST_VISIBILITY (h->root.other)))
5334 return bfd_reloc_undefined;
5336 else
5338 return bfd_reloc_notsupported;
5341 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5342 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5345 /* If this is a reference to a 16-bit function with a stub, we need
5346 to redirect the relocation to the stub unless:
5348 (a) the relocation is for a MIPS16 JAL;
5350 (b) the relocation is for a MIPS16 PIC call, and there are no
5351 non-MIPS16 uses of the GOT slot; or
5353 (c) the section allows direct references to MIPS16 functions. */
5354 if (r_type != R_MIPS16_26
5355 && !info->relocatable
5356 && ((h != NULL
5357 && h->fn_stub != NULL
5358 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5359 || (local_p
5360 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5361 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5362 && !section_allows_mips16_refs_p (input_section))
5364 /* This is a 32- or 64-bit call to a 16-bit function. We should
5365 have already noticed that we were going to need the
5366 stub. */
5367 if (local_p)
5369 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5370 value = 0;
5372 else
5374 BFD_ASSERT (h->need_fn_stub);
5375 if (h->la25_stub)
5377 /* If a LA25 header for the stub itself exists, point to the
5378 prepended LUI/ADDIU sequence. */
5379 sec = h->la25_stub->stub_section;
5380 value = h->la25_stub->offset;
5382 else
5384 sec = h->fn_stub;
5385 value = 0;
5389 symbol = sec->output_section->vma + sec->output_offset + value;
5390 /* The target is 16-bit, but the stub isn't. */
5391 target_is_16_bit_code_p = FALSE;
5393 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5394 to a standard MIPS function, we need to redirect the call to the stub.
5395 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5396 indirect calls should use an indirect stub instead. */
5397 else if (r_type == R_MIPS16_26 && !info->relocatable
5398 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5399 || (local_p
5400 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5401 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5402 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5404 if (local_p)
5405 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5406 else
5408 /* If both call_stub and call_fp_stub are defined, we can figure
5409 out which one to use by checking which one appears in the input
5410 file. */
5411 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5413 asection *o;
5415 sec = NULL;
5416 for (o = input_bfd->sections; o != NULL; o = o->next)
5418 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5420 sec = h->call_fp_stub;
5421 break;
5424 if (sec == NULL)
5425 sec = h->call_stub;
5427 else if (h->call_stub != NULL)
5428 sec = h->call_stub;
5429 else
5430 sec = h->call_fp_stub;
5433 BFD_ASSERT (sec->size > 0);
5434 symbol = sec->output_section->vma + sec->output_offset;
5436 /* If this is a direct call to a PIC function, redirect to the
5437 non-PIC stub. */
5438 else if (h != NULL && h->la25_stub
5439 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5440 target_is_16_bit_code_p))
5441 symbol = (h->la25_stub->stub_section->output_section->vma
5442 + h->la25_stub->stub_section->output_offset
5443 + h->la25_stub->offset);
5444 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5445 entry is used if a standard PLT entry has also been made. In this
5446 case the symbol will have been set by mips_elf_set_plt_sym_value
5447 to point to the standard PLT entry, so redirect to the compressed
5448 one. */
5449 else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
5450 && !info->relocatable
5451 && h != NULL
5452 && h->use_plt_entry
5453 && h->root.plt.plist->comp_offset != MINUS_ONE
5454 && h->root.plt.plist->mips_offset != MINUS_ONE)
5456 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5458 sec = htab->splt;
5459 symbol = (sec->output_section->vma
5460 + sec->output_offset
5461 + htab->plt_header_size
5462 + htab->plt_mips_offset
5463 + h->root.plt.plist->comp_offset
5464 + 1);
5466 target_is_16_bit_code_p = !micromips_p;
5467 target_is_micromips_code_p = micromips_p;
5470 /* Make sure MIPS16 and microMIPS are not used together. */
5471 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5472 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5474 (*_bfd_error_handler)
5475 (_("MIPS16 and microMIPS functions cannot call each other"));
5476 return bfd_reloc_notsupported;
5479 /* Calls from 16-bit code to 32-bit code and vice versa require the
5480 mode change. However, we can ignore calls to undefined weak symbols,
5481 which should never be executed at runtime. This exception is important
5482 because the assembly writer may have "known" that any definition of the
5483 symbol would be 16-bit code, and that direct jumps were therefore
5484 acceptable. */
5485 *cross_mode_jump_p = (!info->relocatable
5486 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5487 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5488 || (r_type == R_MICROMIPS_26_S1
5489 && !target_is_micromips_code_p)
5490 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5491 && (target_is_16_bit_code_p
5492 || target_is_micromips_code_p))));
5494 local_p = (h == NULL || mips_use_local_got_p (info, h));
5496 gp0 = _bfd_get_gp_value (input_bfd);
5497 gp = _bfd_get_gp_value (abfd);
5498 if (htab->got_info)
5499 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5501 if (gnu_local_gp_p)
5502 symbol = gp;
5504 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5505 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5506 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5507 if (got_page_reloc_p (r_type) && !local_p)
5509 r_type = (micromips_reloc_p (r_type)
5510 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5511 addend = 0;
5514 /* If we haven't already determined the GOT offset, and we're going
5515 to need it, get it now. */
5516 switch (r_type)
5518 case R_MIPS16_CALL16:
5519 case R_MIPS16_GOT16:
5520 case R_MIPS_CALL16:
5521 case R_MIPS_GOT16:
5522 case R_MIPS_GOT_DISP:
5523 case R_MIPS_GOT_HI16:
5524 case R_MIPS_CALL_HI16:
5525 case R_MIPS_GOT_LO16:
5526 case R_MIPS_CALL_LO16:
5527 case R_MICROMIPS_CALL16:
5528 case R_MICROMIPS_GOT16:
5529 case R_MICROMIPS_GOT_DISP:
5530 case R_MICROMIPS_GOT_HI16:
5531 case R_MICROMIPS_CALL_HI16:
5532 case R_MICROMIPS_GOT_LO16:
5533 case R_MICROMIPS_CALL_LO16:
5534 case R_MIPS_TLS_GD:
5535 case R_MIPS_TLS_GOTTPREL:
5536 case R_MIPS_TLS_LDM:
5537 case R_MIPS16_TLS_GD:
5538 case R_MIPS16_TLS_GOTTPREL:
5539 case R_MIPS16_TLS_LDM:
5540 case R_MICROMIPS_TLS_GD:
5541 case R_MICROMIPS_TLS_GOTTPREL:
5542 case R_MICROMIPS_TLS_LDM:
5543 /* Find the index into the GOT where this value is located. */
5544 if (tls_ldm_reloc_p (r_type))
5546 g = mips_elf_local_got_index (abfd, input_bfd, info,
5547 0, 0, NULL, r_type);
5548 if (g == MINUS_ONE)
5549 return bfd_reloc_outofrange;
5551 else if (!local_p)
5553 /* On VxWorks, CALL relocations should refer to the .got.plt
5554 entry, which is initialized to point at the PLT stub. */
5555 if (htab->is_vxworks
5556 && (call_hi16_reloc_p (r_type)
5557 || call_lo16_reloc_p (r_type)
5558 || call16_reloc_p (r_type)))
5560 BFD_ASSERT (addend == 0);
5561 BFD_ASSERT (h->root.needs_plt);
5562 g = mips_elf_gotplt_index (info, &h->root);
5564 else
5566 BFD_ASSERT (addend == 0);
5567 g = mips_elf_global_got_index (abfd, info, input_bfd,
5568 &h->root, r_type);
5569 if (!TLS_RELOC_P (r_type)
5570 && !elf_hash_table (info)->dynamic_sections_created)
5571 /* This is a static link. We must initialize the GOT entry. */
5572 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5575 else if (!htab->is_vxworks
5576 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5577 /* The calculation below does not involve "g". */
5578 break;
5579 else
5581 g = mips_elf_local_got_index (abfd, input_bfd, info,
5582 symbol + addend, r_symndx, h, r_type);
5583 if (g == MINUS_ONE)
5584 return bfd_reloc_outofrange;
5587 /* Convert GOT indices to actual offsets. */
5588 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5589 break;
5592 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5593 symbols are resolved by the loader. Add them to .rela.dyn. */
5594 if (h != NULL && is_gott_symbol (info, &h->root))
5596 Elf_Internal_Rela outrel;
5597 bfd_byte *loc;
5598 asection *s;
5600 s = mips_elf_rel_dyn_section (info, FALSE);
5601 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5603 outrel.r_offset = (input_section->output_section->vma
5604 + input_section->output_offset
5605 + relocation->r_offset);
5606 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5607 outrel.r_addend = addend;
5608 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5610 /* If we've written this relocation for a readonly section,
5611 we need to set DF_TEXTREL again, so that we do not delete the
5612 DT_TEXTREL tag. */
5613 if (MIPS_ELF_READONLY_SECTION (input_section))
5614 info->flags |= DF_TEXTREL;
5616 *valuep = 0;
5617 return bfd_reloc_ok;
5620 /* Figure out what kind of relocation is being performed. */
5621 switch (r_type)
5623 case R_MIPS_NONE:
5624 return bfd_reloc_continue;
5626 case R_MIPS_16:
5627 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5628 overflowed_p = mips_elf_overflow_p (value, 16);
5629 break;
5631 case R_MIPS_32:
5632 case R_MIPS_REL32:
5633 case R_MIPS_64:
5634 if ((info->shared
5635 || (htab->root.dynamic_sections_created
5636 && h != NULL
5637 && h->root.def_dynamic
5638 && !h->root.def_regular
5639 && !h->has_static_relocs))
5640 && r_symndx != STN_UNDEF
5641 && (h == NULL
5642 || h->root.root.type != bfd_link_hash_undefweak
5643 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5644 && (input_section->flags & SEC_ALLOC) != 0)
5646 /* If we're creating a shared library, then we can't know
5647 where the symbol will end up. So, we create a relocation
5648 record in the output, and leave the job up to the dynamic
5649 linker. We must do the same for executable references to
5650 shared library symbols, unless we've decided to use copy
5651 relocs or PLTs instead. */
5652 value = addend;
5653 if (!mips_elf_create_dynamic_relocation (abfd,
5654 info,
5655 relocation,
5657 sec,
5658 symbol,
5659 &value,
5660 input_section))
5661 return bfd_reloc_undefined;
5663 else
5665 if (r_type != R_MIPS_REL32)
5666 value = symbol + addend;
5667 else
5668 value = addend;
5670 value &= howto->dst_mask;
5671 break;
5673 case R_MIPS_PC32:
5674 value = symbol + addend - p;
5675 value &= howto->dst_mask;
5676 break;
5678 case R_MIPS16_26:
5679 /* The calculation for R_MIPS16_26 is just the same as for an
5680 R_MIPS_26. It's only the storage of the relocated field into
5681 the output file that's different. That's handled in
5682 mips_elf_perform_relocation. So, we just fall through to the
5683 R_MIPS_26 case here. */
5684 case R_MIPS_26:
5685 case R_MICROMIPS_26_S1:
5687 unsigned int shift;
5689 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5690 the correct ISA mode selector and bit 1 must be 0. */
5691 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5692 return bfd_reloc_outofrange;
5694 /* Shift is 2, unusually, for microMIPS JALX. */
5695 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5697 if (was_local_p)
5698 value = addend | ((p + 4) & (0xfc000000 << shift));
5699 else
5700 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5701 value = (value + symbol) >> shift;
5702 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5703 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5704 value &= howto->dst_mask;
5706 break;
5708 case R_MIPS_TLS_DTPREL_HI16:
5709 case R_MIPS16_TLS_DTPREL_HI16:
5710 case R_MICROMIPS_TLS_DTPREL_HI16:
5711 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5712 & howto->dst_mask);
5713 break;
5715 case R_MIPS_TLS_DTPREL_LO16:
5716 case R_MIPS_TLS_DTPREL32:
5717 case R_MIPS_TLS_DTPREL64:
5718 case R_MIPS16_TLS_DTPREL_LO16:
5719 case R_MICROMIPS_TLS_DTPREL_LO16:
5720 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5721 break;
5723 case R_MIPS_TLS_TPREL_HI16:
5724 case R_MIPS16_TLS_TPREL_HI16:
5725 case R_MICROMIPS_TLS_TPREL_HI16:
5726 value = (mips_elf_high (addend + symbol - tprel_base (info))
5727 & howto->dst_mask);
5728 break;
5730 case R_MIPS_TLS_TPREL_LO16:
5731 case R_MIPS_TLS_TPREL32:
5732 case R_MIPS_TLS_TPREL64:
5733 case R_MIPS16_TLS_TPREL_LO16:
5734 case R_MICROMIPS_TLS_TPREL_LO16:
5735 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5736 break;
5738 case R_MIPS_HI16:
5739 case R_MIPS16_HI16:
5740 case R_MICROMIPS_HI16:
5741 if (!gp_disp_p)
5743 value = mips_elf_high (addend + symbol);
5744 value &= howto->dst_mask;
5746 else
5748 /* For MIPS16 ABI code we generate this sequence
5749 0: li $v0,%hi(_gp_disp)
5750 4: addiupc $v1,%lo(_gp_disp)
5751 8: sll $v0,16
5752 12: addu $v0,$v1
5753 14: move $gp,$v0
5754 So the offsets of hi and lo relocs are the same, but the
5755 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5756 ADDIUPC clears the low two bits of the instruction address,
5757 so the base is ($t9 + 4) & ~3. */
5758 if (r_type == R_MIPS16_HI16)
5759 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
5760 /* The microMIPS .cpload sequence uses the same assembly
5761 instructions as the traditional psABI version, but the
5762 incoming $t9 has the low bit set. */
5763 else if (r_type == R_MICROMIPS_HI16)
5764 value = mips_elf_high (addend + gp - p - 1);
5765 else
5766 value = mips_elf_high (addend + gp - p);
5767 overflowed_p = mips_elf_overflow_p (value, 16);
5769 break;
5771 case R_MIPS_LO16:
5772 case R_MIPS16_LO16:
5773 case R_MICROMIPS_LO16:
5774 case R_MICROMIPS_HI0_LO16:
5775 if (!gp_disp_p)
5776 value = (symbol + addend) & howto->dst_mask;
5777 else
5779 /* See the comment for R_MIPS16_HI16 above for the reason
5780 for this conditional. */
5781 if (r_type == R_MIPS16_LO16)
5782 value = addend + gp - (p & ~(bfd_vma) 0x3);
5783 else if (r_type == R_MICROMIPS_LO16
5784 || r_type == R_MICROMIPS_HI0_LO16)
5785 value = addend + gp - p + 3;
5786 else
5787 value = addend + gp - p + 4;
5788 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5789 for overflow. But, on, say, IRIX5, relocations against
5790 _gp_disp are normally generated from the .cpload
5791 pseudo-op. It generates code that normally looks like
5792 this:
5794 lui $gp,%hi(_gp_disp)
5795 addiu $gp,$gp,%lo(_gp_disp)
5796 addu $gp,$gp,$t9
5798 Here $t9 holds the address of the function being called,
5799 as required by the MIPS ELF ABI. The R_MIPS_LO16
5800 relocation can easily overflow in this situation, but the
5801 R_MIPS_HI16 relocation will handle the overflow.
5802 Therefore, we consider this a bug in the MIPS ABI, and do
5803 not check for overflow here. */
5805 break;
5807 case R_MIPS_LITERAL:
5808 case R_MICROMIPS_LITERAL:
5809 /* Because we don't merge literal sections, we can handle this
5810 just like R_MIPS_GPREL16. In the long run, we should merge
5811 shared literals, and then we will need to additional work
5812 here. */
5814 /* Fall through. */
5816 case R_MIPS16_GPREL:
5817 /* The R_MIPS16_GPREL performs the same calculation as
5818 R_MIPS_GPREL16, but stores the relocated bits in a different
5819 order. We don't need to do anything special here; the
5820 differences are handled in mips_elf_perform_relocation. */
5821 case R_MIPS_GPREL16:
5822 case R_MICROMIPS_GPREL7_S2:
5823 case R_MICROMIPS_GPREL16:
5824 /* Only sign-extend the addend if it was extracted from the
5825 instruction. If the addend was separate, leave it alone,
5826 otherwise we may lose significant bits. */
5827 if (howto->partial_inplace)
5828 addend = _bfd_mips_elf_sign_extend (addend, 16);
5829 value = symbol + addend - gp;
5830 /* If the symbol was local, any earlier relocatable links will
5831 have adjusted its addend with the gp offset, so compensate
5832 for that now. Don't do it for symbols forced local in this
5833 link, though, since they won't have had the gp offset applied
5834 to them before. */
5835 if (was_local_p)
5836 value += gp0;
5837 overflowed_p = mips_elf_overflow_p (value, 16);
5838 break;
5840 case R_MIPS16_GOT16:
5841 case R_MIPS16_CALL16:
5842 case R_MIPS_GOT16:
5843 case R_MIPS_CALL16:
5844 case R_MICROMIPS_GOT16:
5845 case R_MICROMIPS_CALL16:
5846 /* VxWorks does not have separate local and global semantics for
5847 R_MIPS*_GOT16; every relocation evaluates to "G". */
5848 if (!htab->is_vxworks && local_p)
5850 value = mips_elf_got16_entry (abfd, input_bfd, info,
5851 symbol + addend, !was_local_p);
5852 if (value == MINUS_ONE)
5853 return bfd_reloc_outofrange;
5854 value
5855 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5856 overflowed_p = mips_elf_overflow_p (value, 16);
5857 break;
5860 /* Fall through. */
5862 case R_MIPS_TLS_GD:
5863 case R_MIPS_TLS_GOTTPREL:
5864 case R_MIPS_TLS_LDM:
5865 case R_MIPS_GOT_DISP:
5866 case R_MIPS16_TLS_GD:
5867 case R_MIPS16_TLS_GOTTPREL:
5868 case R_MIPS16_TLS_LDM:
5869 case R_MICROMIPS_TLS_GD:
5870 case R_MICROMIPS_TLS_GOTTPREL:
5871 case R_MICROMIPS_TLS_LDM:
5872 case R_MICROMIPS_GOT_DISP:
5873 value = g;
5874 overflowed_p = mips_elf_overflow_p (value, 16);
5875 break;
5877 case R_MIPS_GPREL32:
5878 value = (addend + symbol + gp0 - gp);
5879 if (!save_addend)
5880 value &= howto->dst_mask;
5881 break;
5883 case R_MIPS_PC16:
5884 case R_MIPS_GNU_REL16_S2:
5885 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5886 overflowed_p = mips_elf_overflow_p (value, 18);
5887 value >>= howto->rightshift;
5888 value &= howto->dst_mask;
5889 break;
5891 case R_MICROMIPS_PC7_S1:
5892 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5893 overflowed_p = mips_elf_overflow_p (value, 8);
5894 value >>= howto->rightshift;
5895 value &= howto->dst_mask;
5896 break;
5898 case R_MICROMIPS_PC10_S1:
5899 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5900 overflowed_p = mips_elf_overflow_p (value, 11);
5901 value >>= howto->rightshift;
5902 value &= howto->dst_mask;
5903 break;
5905 case R_MICROMIPS_PC16_S1:
5906 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5907 overflowed_p = mips_elf_overflow_p (value, 17);
5908 value >>= howto->rightshift;
5909 value &= howto->dst_mask;
5910 break;
5912 case R_MICROMIPS_PC23_S2:
5913 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5914 overflowed_p = mips_elf_overflow_p (value, 25);
5915 value >>= howto->rightshift;
5916 value &= howto->dst_mask;
5917 break;
5919 case R_MIPS_GOT_HI16:
5920 case R_MIPS_CALL_HI16:
5921 case R_MICROMIPS_GOT_HI16:
5922 case R_MICROMIPS_CALL_HI16:
5923 /* We're allowed to handle these two relocations identically.
5924 The dynamic linker is allowed to handle the CALL relocations
5925 differently by creating a lazy evaluation stub. */
5926 value = g;
5927 value = mips_elf_high (value);
5928 value &= howto->dst_mask;
5929 break;
5931 case R_MIPS_GOT_LO16:
5932 case R_MIPS_CALL_LO16:
5933 case R_MICROMIPS_GOT_LO16:
5934 case R_MICROMIPS_CALL_LO16:
5935 value = g & howto->dst_mask;
5936 break;
5938 case R_MIPS_GOT_PAGE:
5939 case R_MICROMIPS_GOT_PAGE:
5940 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5941 if (value == MINUS_ONE)
5942 return bfd_reloc_outofrange;
5943 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5944 overflowed_p = mips_elf_overflow_p (value, 16);
5945 break;
5947 case R_MIPS_GOT_OFST:
5948 case R_MICROMIPS_GOT_OFST:
5949 if (local_p)
5950 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5951 else
5952 value = addend;
5953 overflowed_p = mips_elf_overflow_p (value, 16);
5954 break;
5956 case R_MIPS_SUB:
5957 case R_MICROMIPS_SUB:
5958 value = symbol - addend;
5959 value &= howto->dst_mask;
5960 break;
5962 case R_MIPS_HIGHER:
5963 case R_MICROMIPS_HIGHER:
5964 value = mips_elf_higher (addend + symbol);
5965 value &= howto->dst_mask;
5966 break;
5968 case R_MIPS_HIGHEST:
5969 case R_MICROMIPS_HIGHEST:
5970 value = mips_elf_highest (addend + symbol);
5971 value &= howto->dst_mask;
5972 break;
5974 case R_MIPS_SCN_DISP:
5975 case R_MICROMIPS_SCN_DISP:
5976 value = symbol + addend - sec->output_offset;
5977 value &= howto->dst_mask;
5978 break;
5980 case R_MIPS_JALR:
5981 case R_MICROMIPS_JALR:
5982 /* This relocation is only a hint. In some cases, we optimize
5983 it into a bal instruction. But we don't try to optimize
5984 when the symbol does not resolve locally. */
5985 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
5986 return bfd_reloc_continue;
5987 value = symbol + addend;
5988 break;
5990 case R_MIPS_PJUMP:
5991 case R_MIPS_GNU_VTINHERIT:
5992 case R_MIPS_GNU_VTENTRY:
5993 /* We don't do anything with these at present. */
5994 return bfd_reloc_continue;
5996 default:
5997 /* An unrecognized relocation type. */
5998 return bfd_reloc_notsupported;
6001 /* Store the VALUE for our caller. */
6002 *valuep = value;
6003 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6006 /* Obtain the field relocated by RELOCATION. */
6008 static bfd_vma
6009 mips_elf_obtain_contents (reloc_howto_type *howto,
6010 const Elf_Internal_Rela *relocation,
6011 bfd *input_bfd, bfd_byte *contents)
6013 bfd_vma x;
6014 bfd_byte *location = contents + relocation->r_offset;
6016 /* Obtain the bytes. */
6017 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
6019 return x;
6022 /* It has been determined that the result of the RELOCATION is the
6023 VALUE. Use HOWTO to place VALUE into the output file at the
6024 appropriate position. The SECTION is the section to which the
6025 relocation applies.
6026 CROSS_MODE_JUMP_P is true if the relocation field
6027 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6029 Returns FALSE if anything goes wrong. */
6031 static bfd_boolean
6032 mips_elf_perform_relocation (struct bfd_link_info *info,
6033 reloc_howto_type *howto,
6034 const Elf_Internal_Rela *relocation,
6035 bfd_vma value, bfd *input_bfd,
6036 asection *input_section, bfd_byte *contents,
6037 bfd_boolean cross_mode_jump_p)
6039 bfd_vma x;
6040 bfd_byte *location;
6041 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6043 /* Figure out where the relocation is occurring. */
6044 location = contents + relocation->r_offset;
6046 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
6048 /* Obtain the current value. */
6049 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6051 /* Clear the field we are setting. */
6052 x &= ~howto->dst_mask;
6054 /* Set the field. */
6055 x |= (value & howto->dst_mask);
6057 /* If required, turn JAL into JALX. */
6058 if (cross_mode_jump_p && jal_reloc_p (r_type))
6060 bfd_boolean ok;
6061 bfd_vma opcode = x >> 26;
6062 bfd_vma jalx_opcode;
6064 /* Check to see if the opcode is already JAL or JALX. */
6065 if (r_type == R_MIPS16_26)
6067 ok = ((opcode == 0x6) || (opcode == 0x7));
6068 jalx_opcode = 0x7;
6070 else if (r_type == R_MICROMIPS_26_S1)
6072 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6073 jalx_opcode = 0x3c;
6075 else
6077 ok = ((opcode == 0x3) || (opcode == 0x1d));
6078 jalx_opcode = 0x1d;
6081 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6082 convert J or JALS to JALX. */
6083 if (!ok)
6085 (*_bfd_error_handler)
6086 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
6087 input_bfd,
6088 input_section,
6089 (unsigned long) relocation->r_offset);
6090 bfd_set_error (bfd_error_bad_value);
6091 return FALSE;
6094 /* Make this the JALX opcode. */
6095 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6098 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6099 range. */
6100 if (!info->relocatable
6101 && !cross_mode_jump_p
6102 && ((JAL_TO_BAL_P (input_bfd)
6103 && r_type == R_MIPS_26
6104 && (x >> 26) == 0x3) /* jal addr */
6105 || (JALR_TO_BAL_P (input_bfd)
6106 && r_type == R_MIPS_JALR
6107 && x == 0x0320f809) /* jalr t9 */
6108 || (JR_TO_B_P (input_bfd)
6109 && r_type == R_MIPS_JALR
6110 && x == 0x03200008))) /* jr t9 */
6112 bfd_vma addr;
6113 bfd_vma dest;
6114 bfd_signed_vma off;
6116 addr = (input_section->output_section->vma
6117 + input_section->output_offset
6118 + relocation->r_offset
6119 + 4);
6120 if (r_type == R_MIPS_26)
6121 dest = (value << 2) | ((addr >> 28) << 28);
6122 else
6123 dest = value;
6124 off = dest - addr;
6125 if (off <= 0x1ffff && off >= -0x20000)
6127 if (x == 0x03200008) /* jr t9 */
6128 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6129 else
6130 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6134 /* Put the value into the output. */
6135 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6137 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
6138 location);
6140 return TRUE;
6143 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6144 is the original relocation, which is now being transformed into a
6145 dynamic relocation. The ADDENDP is adjusted if necessary; the
6146 caller should store the result in place of the original addend. */
6148 static bfd_boolean
6149 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6150 struct bfd_link_info *info,
6151 const Elf_Internal_Rela *rel,
6152 struct mips_elf_link_hash_entry *h,
6153 asection *sec, bfd_vma symbol,
6154 bfd_vma *addendp, asection *input_section)
6156 Elf_Internal_Rela outrel[3];
6157 asection *sreloc;
6158 bfd *dynobj;
6159 int r_type;
6160 long indx;
6161 bfd_boolean defined_p;
6162 struct mips_elf_link_hash_table *htab;
6164 htab = mips_elf_hash_table (info);
6165 BFD_ASSERT (htab != NULL);
6167 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6168 dynobj = elf_hash_table (info)->dynobj;
6169 sreloc = mips_elf_rel_dyn_section (info, FALSE);
6170 BFD_ASSERT (sreloc != NULL);
6171 BFD_ASSERT (sreloc->contents != NULL);
6172 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6173 < sreloc->size);
6175 outrel[0].r_offset =
6176 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6177 if (ABI_64_P (output_bfd))
6179 outrel[1].r_offset =
6180 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6181 outrel[2].r_offset =
6182 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6185 if (outrel[0].r_offset == MINUS_ONE)
6186 /* The relocation field has been deleted. */
6187 return TRUE;
6189 if (outrel[0].r_offset == MINUS_TWO)
6191 /* The relocation field has been converted into a relative value of
6192 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6193 the field to be fully relocated, so add in the symbol's value. */
6194 *addendp += symbol;
6195 return TRUE;
6198 /* We must now calculate the dynamic symbol table index to use
6199 in the relocation. */
6200 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6202 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
6203 indx = h->root.dynindx;
6204 if (SGI_COMPAT (output_bfd))
6205 defined_p = h->root.def_regular;
6206 else
6207 /* ??? glibc's ld.so just adds the final GOT entry to the
6208 relocation field. It therefore treats relocs against
6209 defined symbols in the same way as relocs against
6210 undefined symbols. */
6211 defined_p = FALSE;
6213 else
6215 if (sec != NULL && bfd_is_abs_section (sec))
6216 indx = 0;
6217 else if (sec == NULL || sec->owner == NULL)
6219 bfd_set_error (bfd_error_bad_value);
6220 return FALSE;
6222 else
6224 indx = elf_section_data (sec->output_section)->dynindx;
6225 if (indx == 0)
6227 asection *osec = htab->root.text_index_section;
6228 indx = elf_section_data (osec)->dynindx;
6230 if (indx == 0)
6231 abort ();
6234 /* Instead of generating a relocation using the section
6235 symbol, we may as well make it a fully relative
6236 relocation. We want to avoid generating relocations to
6237 local symbols because we used to generate them
6238 incorrectly, without adding the original symbol value,
6239 which is mandated by the ABI for section symbols. In
6240 order to give dynamic loaders and applications time to
6241 phase out the incorrect use, we refrain from emitting
6242 section-relative relocations. It's not like they're
6243 useful, after all. This should be a bit more efficient
6244 as well. */
6245 /* ??? Although this behavior is compatible with glibc's ld.so,
6246 the ABI says that relocations against STN_UNDEF should have
6247 a symbol value of 0. Irix rld honors this, so relocations
6248 against STN_UNDEF have no effect. */
6249 if (!SGI_COMPAT (output_bfd))
6250 indx = 0;
6251 defined_p = TRUE;
6254 /* If the relocation was previously an absolute relocation and
6255 this symbol will not be referred to by the relocation, we must
6256 adjust it by the value we give it in the dynamic symbol table.
6257 Otherwise leave the job up to the dynamic linker. */
6258 if (defined_p && r_type != R_MIPS_REL32)
6259 *addendp += symbol;
6261 if (htab->is_vxworks)
6262 /* VxWorks uses non-relative relocations for this. */
6263 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6264 else
6265 /* The relocation is always an REL32 relocation because we don't
6266 know where the shared library will wind up at load-time. */
6267 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6268 R_MIPS_REL32);
6270 /* For strict adherence to the ABI specification, we should
6271 generate a R_MIPS_64 relocation record by itself before the
6272 _REL32/_64 record as well, such that the addend is read in as
6273 a 64-bit value (REL32 is a 32-bit relocation, after all).
6274 However, since none of the existing ELF64 MIPS dynamic
6275 loaders seems to care, we don't waste space with these
6276 artificial relocations. If this turns out to not be true,
6277 mips_elf_allocate_dynamic_relocation() should be tweaked so
6278 as to make room for a pair of dynamic relocations per
6279 invocation if ABI_64_P, and here we should generate an
6280 additional relocation record with R_MIPS_64 by itself for a
6281 NULL symbol before this relocation record. */
6282 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6283 ABI_64_P (output_bfd)
6284 ? R_MIPS_64
6285 : R_MIPS_NONE);
6286 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6288 /* Adjust the output offset of the relocation to reference the
6289 correct location in the output file. */
6290 outrel[0].r_offset += (input_section->output_section->vma
6291 + input_section->output_offset);
6292 outrel[1].r_offset += (input_section->output_section->vma
6293 + input_section->output_offset);
6294 outrel[2].r_offset += (input_section->output_section->vma
6295 + input_section->output_offset);
6297 /* Put the relocation back out. We have to use the special
6298 relocation outputter in the 64-bit case since the 64-bit
6299 relocation format is non-standard. */
6300 if (ABI_64_P (output_bfd))
6302 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6303 (output_bfd, &outrel[0],
6304 (sreloc->contents
6305 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6307 else if (htab->is_vxworks)
6309 /* VxWorks uses RELA rather than REL dynamic relocations. */
6310 outrel[0].r_addend = *addendp;
6311 bfd_elf32_swap_reloca_out
6312 (output_bfd, &outrel[0],
6313 (sreloc->contents
6314 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6316 else
6317 bfd_elf32_swap_reloc_out
6318 (output_bfd, &outrel[0],
6319 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6321 /* We've now added another relocation. */
6322 ++sreloc->reloc_count;
6324 /* Make sure the output section is writable. The dynamic linker
6325 will be writing to it. */
6326 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6327 |= SHF_WRITE;
6329 /* On IRIX5, make an entry of compact relocation info. */
6330 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6332 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6333 bfd_byte *cr;
6335 if (scpt)
6337 Elf32_crinfo cptrel;
6339 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6340 cptrel.vaddr = (rel->r_offset
6341 + input_section->output_section->vma
6342 + input_section->output_offset);
6343 if (r_type == R_MIPS_REL32)
6344 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6345 else
6346 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6347 mips_elf_set_cr_dist2to (cptrel, 0);
6348 cptrel.konst = *addendp;
6350 cr = (scpt->contents
6351 + sizeof (Elf32_External_compact_rel));
6352 mips_elf_set_cr_relvaddr (cptrel, 0);
6353 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6354 ((Elf32_External_crinfo *) cr
6355 + scpt->reloc_count));
6356 ++scpt->reloc_count;
6360 /* If we've written this relocation for a readonly section,
6361 we need to set DF_TEXTREL again, so that we do not delete the
6362 DT_TEXTREL tag. */
6363 if (MIPS_ELF_READONLY_SECTION (input_section))
6364 info->flags |= DF_TEXTREL;
6366 return TRUE;
6369 /* Return the MACH for a MIPS e_flags value. */
6371 unsigned long
6372 _bfd_elf_mips_mach (flagword flags)
6374 switch (flags & EF_MIPS_MACH)
6376 case E_MIPS_MACH_3900:
6377 return bfd_mach_mips3900;
6379 case E_MIPS_MACH_4010:
6380 return bfd_mach_mips4010;
6382 case E_MIPS_MACH_4100:
6383 return bfd_mach_mips4100;
6385 case E_MIPS_MACH_4111:
6386 return bfd_mach_mips4111;
6388 case E_MIPS_MACH_4120:
6389 return bfd_mach_mips4120;
6391 case E_MIPS_MACH_4650:
6392 return bfd_mach_mips4650;
6394 case E_MIPS_MACH_5400:
6395 return bfd_mach_mips5400;
6397 case E_MIPS_MACH_5500:
6398 return bfd_mach_mips5500;
6400 case E_MIPS_MACH_5900:
6401 return bfd_mach_mips5900;
6403 case E_MIPS_MACH_9000:
6404 return bfd_mach_mips9000;
6406 case E_MIPS_MACH_SB1:
6407 return bfd_mach_mips_sb1;
6409 case E_MIPS_MACH_LS2E:
6410 return bfd_mach_mips_loongson_2e;
6412 case E_MIPS_MACH_LS2F:
6413 return bfd_mach_mips_loongson_2f;
6415 case E_MIPS_MACH_LS3A:
6416 return bfd_mach_mips_loongson_3a;
6418 case E_MIPS_MACH_OCTEON2:
6419 return bfd_mach_mips_octeon2;
6421 case E_MIPS_MACH_OCTEON:
6422 return bfd_mach_mips_octeon;
6424 case E_MIPS_MACH_XLR:
6425 return bfd_mach_mips_xlr;
6427 default:
6428 switch (flags & EF_MIPS_ARCH)
6430 default:
6431 case E_MIPS_ARCH_1:
6432 return bfd_mach_mips3000;
6434 case E_MIPS_ARCH_2:
6435 return bfd_mach_mips6000;
6437 case E_MIPS_ARCH_3:
6438 return bfd_mach_mips4000;
6440 case E_MIPS_ARCH_4:
6441 return bfd_mach_mips8000;
6443 case E_MIPS_ARCH_5:
6444 return bfd_mach_mips5;
6446 case E_MIPS_ARCH_32:
6447 return bfd_mach_mipsisa32;
6449 case E_MIPS_ARCH_64:
6450 return bfd_mach_mipsisa64;
6452 case E_MIPS_ARCH_32R2:
6453 return bfd_mach_mipsisa32r2;
6455 case E_MIPS_ARCH_64R2:
6456 return bfd_mach_mipsisa64r2;
6460 return 0;
6463 /* Return printable name for ABI. */
6465 static INLINE char *
6466 elf_mips_abi_name (bfd *abfd)
6468 flagword flags;
6470 flags = elf_elfheader (abfd)->e_flags;
6471 switch (flags & EF_MIPS_ABI)
6473 case 0:
6474 if (ABI_N32_P (abfd))
6475 return "N32";
6476 else if (ABI_64_P (abfd))
6477 return "64";
6478 else
6479 return "none";
6480 case E_MIPS_ABI_O32:
6481 return "O32";
6482 case E_MIPS_ABI_O64:
6483 return "O64";
6484 case E_MIPS_ABI_EABI32:
6485 return "EABI32";
6486 case E_MIPS_ABI_EABI64:
6487 return "EABI64";
6488 default:
6489 return "unknown abi";
6493 /* MIPS ELF uses two common sections. One is the usual one, and the
6494 other is for small objects. All the small objects are kept
6495 together, and then referenced via the gp pointer, which yields
6496 faster assembler code. This is what we use for the small common
6497 section. This approach is copied from ecoff.c. */
6498 static asection mips_elf_scom_section;
6499 static asymbol mips_elf_scom_symbol;
6500 static asymbol *mips_elf_scom_symbol_ptr;
6502 /* MIPS ELF also uses an acommon section, which represents an
6503 allocated common symbol which may be overridden by a
6504 definition in a shared library. */
6505 static asection mips_elf_acom_section;
6506 static asymbol mips_elf_acom_symbol;
6507 static asymbol *mips_elf_acom_symbol_ptr;
6509 /* This is used for both the 32-bit and the 64-bit ABI. */
6511 void
6512 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6514 elf_symbol_type *elfsym;
6516 /* Handle the special MIPS section numbers that a symbol may use. */
6517 elfsym = (elf_symbol_type *) asym;
6518 switch (elfsym->internal_elf_sym.st_shndx)
6520 case SHN_MIPS_ACOMMON:
6521 /* This section is used in a dynamically linked executable file.
6522 It is an allocated common section. The dynamic linker can
6523 either resolve these symbols to something in a shared
6524 library, or it can just leave them here. For our purposes,
6525 we can consider these symbols to be in a new section. */
6526 if (mips_elf_acom_section.name == NULL)
6528 /* Initialize the acommon section. */
6529 mips_elf_acom_section.name = ".acommon";
6530 mips_elf_acom_section.flags = SEC_ALLOC;
6531 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6532 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6533 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6534 mips_elf_acom_symbol.name = ".acommon";
6535 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6536 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6537 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6539 asym->section = &mips_elf_acom_section;
6540 break;
6542 case SHN_COMMON:
6543 /* Common symbols less than the GP size are automatically
6544 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6545 if (asym->value > elf_gp_size (abfd)
6546 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6547 || IRIX_COMPAT (abfd) == ict_irix6)
6548 break;
6549 /* Fall through. */
6550 case SHN_MIPS_SCOMMON:
6551 if (mips_elf_scom_section.name == NULL)
6553 /* Initialize the small common section. */
6554 mips_elf_scom_section.name = ".scommon";
6555 mips_elf_scom_section.flags = SEC_IS_COMMON;
6556 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6557 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6558 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6559 mips_elf_scom_symbol.name = ".scommon";
6560 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6561 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6562 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6564 asym->section = &mips_elf_scom_section;
6565 asym->value = elfsym->internal_elf_sym.st_size;
6566 break;
6568 case SHN_MIPS_SUNDEFINED:
6569 asym->section = bfd_und_section_ptr;
6570 break;
6572 case SHN_MIPS_TEXT:
6574 asection *section = bfd_get_section_by_name (abfd, ".text");
6576 if (section != NULL)
6578 asym->section = section;
6579 /* MIPS_TEXT is a bit special, the address is not an offset
6580 to the base of the .text section. So substract the section
6581 base address to make it an offset. */
6582 asym->value -= section->vma;
6585 break;
6587 case SHN_MIPS_DATA:
6589 asection *section = bfd_get_section_by_name (abfd, ".data");
6591 if (section != NULL)
6593 asym->section = section;
6594 /* MIPS_DATA is a bit special, the address is not an offset
6595 to the base of the .data section. So substract the section
6596 base address to make it an offset. */
6597 asym->value -= section->vma;
6600 break;
6603 /* If this is an odd-valued function symbol, assume it's a MIPS16
6604 or microMIPS one. */
6605 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6606 && (asym->value & 1) != 0)
6608 asym->value--;
6609 if (MICROMIPS_P (abfd))
6610 elfsym->internal_elf_sym.st_other
6611 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6612 else
6613 elfsym->internal_elf_sym.st_other
6614 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6618 /* Implement elf_backend_eh_frame_address_size. This differs from
6619 the default in the way it handles EABI64.
6621 EABI64 was originally specified as an LP64 ABI, and that is what
6622 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6623 historically accepted the combination of -mabi=eabi and -mlong32,
6624 and this ILP32 variation has become semi-official over time.
6625 Both forms use elf32 and have pointer-sized FDE addresses.
6627 If an EABI object was generated by GCC 4.0 or above, it will have
6628 an empty .gcc_compiled_longXX section, where XX is the size of longs
6629 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6630 have no special marking to distinguish them from LP64 objects.
6632 We don't want users of the official LP64 ABI to be punished for the
6633 existence of the ILP32 variant, but at the same time, we don't want
6634 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6635 We therefore take the following approach:
6637 - If ABFD contains a .gcc_compiled_longXX section, use it to
6638 determine the pointer size.
6640 - Otherwise check the type of the first relocation. Assume that
6641 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6643 - Otherwise punt.
6645 The second check is enough to detect LP64 objects generated by pre-4.0
6646 compilers because, in the kind of output generated by those compilers,
6647 the first relocation will be associated with either a CIE personality
6648 routine or an FDE start address. Furthermore, the compilers never
6649 used a special (non-pointer) encoding for this ABI.
6651 Checking the relocation type should also be safe because there is no
6652 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6653 did so. */
6655 unsigned int
6656 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6658 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6659 return 8;
6660 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6662 bfd_boolean long32_p, long64_p;
6664 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6665 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6666 if (long32_p && long64_p)
6667 return 0;
6668 if (long32_p)
6669 return 4;
6670 if (long64_p)
6671 return 8;
6673 if (sec->reloc_count > 0
6674 && elf_section_data (sec)->relocs != NULL
6675 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6676 == R_MIPS_64))
6677 return 8;
6679 return 0;
6681 return 4;
6684 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6685 relocations against two unnamed section symbols to resolve to the
6686 same address. For example, if we have code like:
6688 lw $4,%got_disp(.data)($gp)
6689 lw $25,%got_disp(.text)($gp)
6690 jalr $25
6692 then the linker will resolve both relocations to .data and the program
6693 will jump there rather than to .text.
6695 We can work around this problem by giving names to local section symbols.
6696 This is also what the MIPSpro tools do. */
6698 bfd_boolean
6699 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6701 return SGI_COMPAT (abfd);
6704 /* Work over a section just before writing it out. This routine is
6705 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6706 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6707 a better way. */
6709 bfd_boolean
6710 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6712 if (hdr->sh_type == SHT_MIPS_REGINFO
6713 && hdr->sh_size > 0)
6715 bfd_byte buf[4];
6717 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6718 BFD_ASSERT (hdr->contents == NULL);
6720 if (bfd_seek (abfd,
6721 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6722 SEEK_SET) != 0)
6723 return FALSE;
6724 H_PUT_32 (abfd, elf_gp (abfd), buf);
6725 if (bfd_bwrite (buf, 4, abfd) != 4)
6726 return FALSE;
6729 if (hdr->sh_type == SHT_MIPS_OPTIONS
6730 && hdr->bfd_section != NULL
6731 && mips_elf_section_data (hdr->bfd_section) != NULL
6732 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6734 bfd_byte *contents, *l, *lend;
6736 /* We stored the section contents in the tdata field in the
6737 set_section_contents routine. We save the section contents
6738 so that we don't have to read them again.
6739 At this point we know that elf_gp is set, so we can look
6740 through the section contents to see if there is an
6741 ODK_REGINFO structure. */
6743 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6744 l = contents;
6745 lend = contents + hdr->sh_size;
6746 while (l + sizeof (Elf_External_Options) <= lend)
6748 Elf_Internal_Options intopt;
6750 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6751 &intopt);
6752 if (intopt.size < sizeof (Elf_External_Options))
6754 (*_bfd_error_handler)
6755 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6756 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6757 break;
6759 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6761 bfd_byte buf[8];
6763 if (bfd_seek (abfd,
6764 (hdr->sh_offset
6765 + (l - contents)
6766 + sizeof (Elf_External_Options)
6767 + (sizeof (Elf64_External_RegInfo) - 8)),
6768 SEEK_SET) != 0)
6769 return FALSE;
6770 H_PUT_64 (abfd, elf_gp (abfd), buf);
6771 if (bfd_bwrite (buf, 8, abfd) != 8)
6772 return FALSE;
6774 else if (intopt.kind == ODK_REGINFO)
6776 bfd_byte buf[4];
6778 if (bfd_seek (abfd,
6779 (hdr->sh_offset
6780 + (l - contents)
6781 + sizeof (Elf_External_Options)
6782 + (sizeof (Elf32_External_RegInfo) - 4)),
6783 SEEK_SET) != 0)
6784 return FALSE;
6785 H_PUT_32 (abfd, elf_gp (abfd), buf);
6786 if (bfd_bwrite (buf, 4, abfd) != 4)
6787 return FALSE;
6789 l += intopt.size;
6793 if (hdr->bfd_section != NULL)
6795 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6797 /* .sbss is not handled specially here because the GNU/Linux
6798 prelinker can convert .sbss from NOBITS to PROGBITS and
6799 changing it back to NOBITS breaks the binary. The entry in
6800 _bfd_mips_elf_special_sections will ensure the correct flags
6801 are set on .sbss if BFD creates it without reading it from an
6802 input file, and without special handling here the flags set
6803 on it in an input file will be followed. */
6804 if (strcmp (name, ".sdata") == 0
6805 || strcmp (name, ".lit8") == 0
6806 || strcmp (name, ".lit4") == 0)
6808 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6809 hdr->sh_type = SHT_PROGBITS;
6811 else if (strcmp (name, ".srdata") == 0)
6813 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6814 hdr->sh_type = SHT_PROGBITS;
6816 else if (strcmp (name, ".compact_rel") == 0)
6818 hdr->sh_flags = 0;
6819 hdr->sh_type = SHT_PROGBITS;
6821 else if (strcmp (name, ".rtproc") == 0)
6823 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6825 unsigned int adjust;
6827 adjust = hdr->sh_size % hdr->sh_addralign;
6828 if (adjust != 0)
6829 hdr->sh_size += hdr->sh_addralign - adjust;
6834 return TRUE;
6837 /* Handle a MIPS specific section when reading an object file. This
6838 is called when elfcode.h finds a section with an unknown type.
6839 This routine supports both the 32-bit and 64-bit ELF ABI.
6841 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6842 how to. */
6844 bfd_boolean
6845 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6846 Elf_Internal_Shdr *hdr,
6847 const char *name,
6848 int shindex)
6850 flagword flags = 0;
6852 /* There ought to be a place to keep ELF backend specific flags, but
6853 at the moment there isn't one. We just keep track of the
6854 sections by their name, instead. Fortunately, the ABI gives
6855 suggested names for all the MIPS specific sections, so we will
6856 probably get away with this. */
6857 switch (hdr->sh_type)
6859 case SHT_MIPS_LIBLIST:
6860 if (strcmp (name, ".liblist") != 0)
6861 return FALSE;
6862 break;
6863 case SHT_MIPS_MSYM:
6864 if (strcmp (name, ".msym") != 0)
6865 return FALSE;
6866 break;
6867 case SHT_MIPS_CONFLICT:
6868 if (strcmp (name, ".conflict") != 0)
6869 return FALSE;
6870 break;
6871 case SHT_MIPS_GPTAB:
6872 if (! CONST_STRNEQ (name, ".gptab."))
6873 return FALSE;
6874 break;
6875 case SHT_MIPS_UCODE:
6876 if (strcmp (name, ".ucode") != 0)
6877 return FALSE;
6878 break;
6879 case SHT_MIPS_DEBUG:
6880 if (strcmp (name, ".mdebug") != 0)
6881 return FALSE;
6882 flags = SEC_DEBUGGING;
6883 break;
6884 case SHT_MIPS_REGINFO:
6885 if (strcmp (name, ".reginfo") != 0
6886 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6887 return FALSE;
6888 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6889 break;
6890 case SHT_MIPS_IFACE:
6891 if (strcmp (name, ".MIPS.interfaces") != 0)
6892 return FALSE;
6893 break;
6894 case SHT_MIPS_CONTENT:
6895 if (! CONST_STRNEQ (name, ".MIPS.content"))
6896 return FALSE;
6897 break;
6898 case SHT_MIPS_OPTIONS:
6899 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6900 return FALSE;
6901 break;
6902 case SHT_MIPS_DWARF:
6903 if (! CONST_STRNEQ (name, ".debug_")
6904 && ! CONST_STRNEQ (name, ".zdebug_"))
6905 return FALSE;
6906 break;
6907 case SHT_MIPS_SYMBOL_LIB:
6908 if (strcmp (name, ".MIPS.symlib") != 0)
6909 return FALSE;
6910 break;
6911 case SHT_MIPS_EVENTS:
6912 if (! CONST_STRNEQ (name, ".MIPS.events")
6913 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6914 return FALSE;
6915 break;
6916 default:
6917 break;
6920 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6921 return FALSE;
6923 if (flags)
6925 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6926 (bfd_get_section_flags (abfd,
6927 hdr->bfd_section)
6928 | flags)))
6929 return FALSE;
6932 /* FIXME: We should record sh_info for a .gptab section. */
6934 /* For a .reginfo section, set the gp value in the tdata information
6935 from the contents of this section. We need the gp value while
6936 processing relocs, so we just get it now. The .reginfo section
6937 is not used in the 64-bit MIPS ELF ABI. */
6938 if (hdr->sh_type == SHT_MIPS_REGINFO)
6940 Elf32_External_RegInfo ext;
6941 Elf32_RegInfo s;
6943 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6944 &ext, 0, sizeof ext))
6945 return FALSE;
6946 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6947 elf_gp (abfd) = s.ri_gp_value;
6950 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6951 set the gp value based on what we find. We may see both
6952 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6953 they should agree. */
6954 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6956 bfd_byte *contents, *l, *lend;
6958 contents = bfd_malloc (hdr->sh_size);
6959 if (contents == NULL)
6960 return FALSE;
6961 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6962 0, hdr->sh_size))
6964 free (contents);
6965 return FALSE;
6967 l = contents;
6968 lend = contents + hdr->sh_size;
6969 while (l + sizeof (Elf_External_Options) <= lend)
6971 Elf_Internal_Options intopt;
6973 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6974 &intopt);
6975 if (intopt.size < sizeof (Elf_External_Options))
6977 (*_bfd_error_handler)
6978 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6979 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6980 break;
6982 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6984 Elf64_Internal_RegInfo intreg;
6986 bfd_mips_elf64_swap_reginfo_in
6987 (abfd,
6988 ((Elf64_External_RegInfo *)
6989 (l + sizeof (Elf_External_Options))),
6990 &intreg);
6991 elf_gp (abfd) = intreg.ri_gp_value;
6993 else if (intopt.kind == ODK_REGINFO)
6995 Elf32_RegInfo intreg;
6997 bfd_mips_elf32_swap_reginfo_in
6998 (abfd,
6999 ((Elf32_External_RegInfo *)
7000 (l + sizeof (Elf_External_Options))),
7001 &intreg);
7002 elf_gp (abfd) = intreg.ri_gp_value;
7004 l += intopt.size;
7006 free (contents);
7009 return TRUE;
7012 /* Set the correct type for a MIPS ELF section. We do this by the
7013 section name, which is a hack, but ought to work. This routine is
7014 used by both the 32-bit and the 64-bit ABI. */
7016 bfd_boolean
7017 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7019 const char *name = bfd_get_section_name (abfd, sec);
7021 if (strcmp (name, ".liblist") == 0)
7023 hdr->sh_type = SHT_MIPS_LIBLIST;
7024 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7025 /* The sh_link field is set in final_write_processing. */
7027 else if (strcmp (name, ".conflict") == 0)
7028 hdr->sh_type = SHT_MIPS_CONFLICT;
7029 else if (CONST_STRNEQ (name, ".gptab."))
7031 hdr->sh_type = SHT_MIPS_GPTAB;
7032 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7033 /* The sh_info field is set in final_write_processing. */
7035 else if (strcmp (name, ".ucode") == 0)
7036 hdr->sh_type = SHT_MIPS_UCODE;
7037 else if (strcmp (name, ".mdebug") == 0)
7039 hdr->sh_type = SHT_MIPS_DEBUG;
7040 /* In a shared object on IRIX 5.3, the .mdebug section has an
7041 entsize of 0. FIXME: Does this matter? */
7042 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7043 hdr->sh_entsize = 0;
7044 else
7045 hdr->sh_entsize = 1;
7047 else if (strcmp (name, ".reginfo") == 0)
7049 hdr->sh_type = SHT_MIPS_REGINFO;
7050 /* In a shared object on IRIX 5.3, the .reginfo section has an
7051 entsize of 0x18. FIXME: Does this matter? */
7052 if (SGI_COMPAT (abfd))
7054 if ((abfd->flags & DYNAMIC) != 0)
7055 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7056 else
7057 hdr->sh_entsize = 1;
7059 else
7060 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7062 else if (SGI_COMPAT (abfd)
7063 && (strcmp (name, ".hash") == 0
7064 || strcmp (name, ".dynamic") == 0
7065 || strcmp (name, ".dynstr") == 0))
7067 if (SGI_COMPAT (abfd))
7068 hdr->sh_entsize = 0;
7069 #if 0
7070 /* This isn't how the IRIX6 linker behaves. */
7071 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7072 #endif
7074 else if (strcmp (name, ".got") == 0
7075 || strcmp (name, ".srdata") == 0
7076 || strcmp (name, ".sdata") == 0
7077 || strcmp (name, ".sbss") == 0
7078 || strcmp (name, ".lit4") == 0
7079 || strcmp (name, ".lit8") == 0)
7080 hdr->sh_flags |= SHF_MIPS_GPREL;
7081 else if (strcmp (name, ".MIPS.interfaces") == 0)
7083 hdr->sh_type = SHT_MIPS_IFACE;
7084 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7086 else if (CONST_STRNEQ (name, ".MIPS.content"))
7088 hdr->sh_type = SHT_MIPS_CONTENT;
7089 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7090 /* The sh_info field is set in final_write_processing. */
7092 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7094 hdr->sh_type = SHT_MIPS_OPTIONS;
7095 hdr->sh_entsize = 1;
7096 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7098 else if (CONST_STRNEQ (name, ".debug_")
7099 || CONST_STRNEQ (name, ".zdebug_"))
7101 hdr->sh_type = SHT_MIPS_DWARF;
7103 /* Irix facilities such as libexc expect a single .debug_frame
7104 per executable, the system ones have NOSTRIP set and the linker
7105 doesn't merge sections with different flags so ... */
7106 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7107 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7109 else if (strcmp (name, ".MIPS.symlib") == 0)
7111 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7112 /* The sh_link and sh_info fields are set in
7113 final_write_processing. */
7115 else if (CONST_STRNEQ (name, ".MIPS.events")
7116 || CONST_STRNEQ (name, ".MIPS.post_rel"))
7118 hdr->sh_type = SHT_MIPS_EVENTS;
7119 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7120 /* The sh_link field is set in final_write_processing. */
7122 else if (strcmp (name, ".msym") == 0)
7124 hdr->sh_type = SHT_MIPS_MSYM;
7125 hdr->sh_flags |= SHF_ALLOC;
7126 hdr->sh_entsize = 8;
7129 /* The generic elf_fake_sections will set up REL_HDR using the default
7130 kind of relocations. We used to set up a second header for the
7131 non-default kind of relocations here, but only NewABI would use
7132 these, and the IRIX ld doesn't like resulting empty RELA sections.
7133 Thus we create those header only on demand now. */
7135 return TRUE;
7138 /* Given a BFD section, try to locate the corresponding ELF section
7139 index. This is used by both the 32-bit and the 64-bit ABI.
7140 Actually, it's not clear to me that the 64-bit ABI supports these,
7141 but for non-PIC objects we will certainly want support for at least
7142 the .scommon section. */
7144 bfd_boolean
7145 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7146 asection *sec, int *retval)
7148 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7150 *retval = SHN_MIPS_SCOMMON;
7151 return TRUE;
7153 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7155 *retval = SHN_MIPS_ACOMMON;
7156 return TRUE;
7158 return FALSE;
7161 /* Hook called by the linker routine which adds symbols from an object
7162 file. We must handle the special MIPS section numbers here. */
7164 bfd_boolean
7165 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7166 Elf_Internal_Sym *sym, const char **namep,
7167 flagword *flagsp ATTRIBUTE_UNUSED,
7168 asection **secp, bfd_vma *valp)
7170 if (SGI_COMPAT (abfd)
7171 && (abfd->flags & DYNAMIC) != 0
7172 && strcmp (*namep, "_rld_new_interface") == 0)
7174 /* Skip IRIX5 rld entry name. */
7175 *namep = NULL;
7176 return TRUE;
7179 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7180 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7181 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7182 a magic symbol resolved by the linker, we ignore this bogus definition
7183 of _gp_disp. New ABI objects do not suffer from this problem so this
7184 is not done for them. */
7185 if (!NEWABI_P(abfd)
7186 && (sym->st_shndx == SHN_ABS)
7187 && (strcmp (*namep, "_gp_disp") == 0))
7189 *namep = NULL;
7190 return TRUE;
7193 switch (sym->st_shndx)
7195 case SHN_COMMON:
7196 /* Common symbols less than the GP size are automatically
7197 treated as SHN_MIPS_SCOMMON symbols. */
7198 if (sym->st_size > elf_gp_size (abfd)
7199 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7200 || IRIX_COMPAT (abfd) == ict_irix6)
7201 break;
7202 /* Fall through. */
7203 case SHN_MIPS_SCOMMON:
7204 *secp = bfd_make_section_old_way (abfd, ".scommon");
7205 (*secp)->flags |= SEC_IS_COMMON;
7206 *valp = sym->st_size;
7207 break;
7209 case SHN_MIPS_TEXT:
7210 /* This section is used in a shared object. */
7211 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7213 asymbol *elf_text_symbol;
7214 asection *elf_text_section;
7215 bfd_size_type amt = sizeof (asection);
7217 elf_text_section = bfd_zalloc (abfd, amt);
7218 if (elf_text_section == NULL)
7219 return FALSE;
7221 amt = sizeof (asymbol);
7222 elf_text_symbol = bfd_zalloc (abfd, amt);
7223 if (elf_text_symbol == NULL)
7224 return FALSE;
7226 /* Initialize the section. */
7228 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7229 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7231 elf_text_section->symbol = elf_text_symbol;
7232 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7234 elf_text_section->name = ".text";
7235 elf_text_section->flags = SEC_NO_FLAGS;
7236 elf_text_section->output_section = NULL;
7237 elf_text_section->owner = abfd;
7238 elf_text_symbol->name = ".text";
7239 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7240 elf_text_symbol->section = elf_text_section;
7242 /* This code used to do *secp = bfd_und_section_ptr if
7243 info->shared. I don't know why, and that doesn't make sense,
7244 so I took it out. */
7245 *secp = mips_elf_tdata (abfd)->elf_text_section;
7246 break;
7248 case SHN_MIPS_ACOMMON:
7249 /* Fall through. XXX Can we treat this as allocated data? */
7250 case SHN_MIPS_DATA:
7251 /* This section is used in a shared object. */
7252 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7254 asymbol *elf_data_symbol;
7255 asection *elf_data_section;
7256 bfd_size_type amt = sizeof (asection);
7258 elf_data_section = bfd_zalloc (abfd, amt);
7259 if (elf_data_section == NULL)
7260 return FALSE;
7262 amt = sizeof (asymbol);
7263 elf_data_symbol = bfd_zalloc (abfd, amt);
7264 if (elf_data_symbol == NULL)
7265 return FALSE;
7267 /* Initialize the section. */
7269 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7270 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7272 elf_data_section->symbol = elf_data_symbol;
7273 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7275 elf_data_section->name = ".data";
7276 elf_data_section->flags = SEC_NO_FLAGS;
7277 elf_data_section->output_section = NULL;
7278 elf_data_section->owner = abfd;
7279 elf_data_symbol->name = ".data";
7280 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7281 elf_data_symbol->section = elf_data_section;
7283 /* This code used to do *secp = bfd_und_section_ptr if
7284 info->shared. I don't know why, and that doesn't make sense,
7285 so I took it out. */
7286 *secp = mips_elf_tdata (abfd)->elf_data_section;
7287 break;
7289 case SHN_MIPS_SUNDEFINED:
7290 *secp = bfd_und_section_ptr;
7291 break;
7294 if (SGI_COMPAT (abfd)
7295 && ! info->shared
7296 && info->output_bfd->xvec == abfd->xvec
7297 && strcmp (*namep, "__rld_obj_head") == 0)
7299 struct elf_link_hash_entry *h;
7300 struct bfd_link_hash_entry *bh;
7302 /* Mark __rld_obj_head as dynamic. */
7303 bh = NULL;
7304 if (! (_bfd_generic_link_add_one_symbol
7305 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7306 get_elf_backend_data (abfd)->collect, &bh)))
7307 return FALSE;
7309 h = (struct elf_link_hash_entry *) bh;
7310 h->non_elf = 0;
7311 h->def_regular = 1;
7312 h->type = STT_OBJECT;
7314 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7315 return FALSE;
7317 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7318 mips_elf_hash_table (info)->rld_symbol = h;
7321 /* If this is a mips16 text symbol, add 1 to the value to make it
7322 odd. This will cause something like .word SYM to come up with
7323 the right value when it is loaded into the PC. */
7324 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7325 ++*valp;
7327 return TRUE;
7330 /* This hook function is called before the linker writes out a global
7331 symbol. We mark symbols as small common if appropriate. This is
7332 also where we undo the increment of the value for a mips16 symbol. */
7335 _bfd_mips_elf_link_output_symbol_hook
7336 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7337 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7338 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7340 /* If we see a common symbol, which implies a relocatable link, then
7341 if a symbol was small common in an input file, mark it as small
7342 common in the output file. */
7343 if (sym->st_shndx == SHN_COMMON
7344 && strcmp (input_sec->name, ".scommon") == 0)
7345 sym->st_shndx = SHN_MIPS_SCOMMON;
7347 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7348 sym->st_value &= ~1;
7350 return 1;
7353 /* Functions for the dynamic linker. */
7355 /* Create dynamic sections when linking against a dynamic object. */
7357 bfd_boolean
7358 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7360 struct elf_link_hash_entry *h;
7361 struct bfd_link_hash_entry *bh;
7362 flagword flags;
7363 register asection *s;
7364 const char * const *namep;
7365 struct mips_elf_link_hash_table *htab;
7367 htab = mips_elf_hash_table (info);
7368 BFD_ASSERT (htab != NULL);
7370 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7371 | SEC_LINKER_CREATED | SEC_READONLY);
7373 /* The psABI requires a read-only .dynamic section, but the VxWorks
7374 EABI doesn't. */
7375 if (!htab->is_vxworks)
7377 s = bfd_get_linker_section (abfd, ".dynamic");
7378 if (s != NULL)
7380 if (! bfd_set_section_flags (abfd, s, flags))
7381 return FALSE;
7385 /* We need to create .got section. */
7386 if (!mips_elf_create_got_section (abfd, info))
7387 return FALSE;
7389 if (! mips_elf_rel_dyn_section (info, TRUE))
7390 return FALSE;
7392 /* Create .stub section. */
7393 s = bfd_make_section_anyway_with_flags (abfd,
7394 MIPS_ELF_STUB_SECTION_NAME (abfd),
7395 flags | SEC_CODE);
7396 if (s == NULL
7397 || ! bfd_set_section_alignment (abfd, s,
7398 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7399 return FALSE;
7400 htab->sstubs = s;
7402 if (!mips_elf_hash_table (info)->use_rld_obj_head
7403 && !info->shared
7404 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
7406 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7407 flags &~ (flagword) SEC_READONLY);
7408 if (s == NULL
7409 || ! bfd_set_section_alignment (abfd, s,
7410 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7411 return FALSE;
7414 /* On IRIX5, we adjust add some additional symbols and change the
7415 alignments of several sections. There is no ABI documentation
7416 indicating that this is necessary on IRIX6, nor any evidence that
7417 the linker takes such action. */
7418 if (IRIX_COMPAT (abfd) == ict_irix5)
7420 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7422 bh = NULL;
7423 if (! (_bfd_generic_link_add_one_symbol
7424 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7425 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7426 return FALSE;
7428 h = (struct elf_link_hash_entry *) bh;
7429 h->non_elf = 0;
7430 h->def_regular = 1;
7431 h->type = STT_SECTION;
7433 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7434 return FALSE;
7437 /* We need to create a .compact_rel section. */
7438 if (SGI_COMPAT (abfd))
7440 if (!mips_elf_create_compact_rel_section (abfd, info))
7441 return FALSE;
7444 /* Change alignments of some sections. */
7445 s = bfd_get_linker_section (abfd, ".hash");
7446 if (s != NULL)
7447 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7449 s = bfd_get_linker_section (abfd, ".dynsym");
7450 if (s != NULL)
7451 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7453 s = bfd_get_linker_section (abfd, ".dynstr");
7454 if (s != NULL)
7455 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7457 /* ??? */
7458 s = bfd_get_section_by_name (abfd, ".reginfo");
7459 if (s != NULL)
7460 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7462 s = bfd_get_linker_section (abfd, ".dynamic");
7463 if (s != NULL)
7464 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7467 if (!info->shared)
7469 const char *name;
7471 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7472 bh = NULL;
7473 if (!(_bfd_generic_link_add_one_symbol
7474 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7475 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7476 return FALSE;
7478 h = (struct elf_link_hash_entry *) bh;
7479 h->non_elf = 0;
7480 h->def_regular = 1;
7481 h->type = STT_SECTION;
7483 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7484 return FALSE;
7486 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7488 /* __rld_map is a four byte word located in the .data section
7489 and is filled in by the rtld to contain a pointer to
7490 the _r_debug structure. Its symbol value will be set in
7491 _bfd_mips_elf_finish_dynamic_symbol. */
7492 s = bfd_get_linker_section (abfd, ".rld_map");
7493 BFD_ASSERT (s != NULL);
7495 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7496 bh = NULL;
7497 if (!(_bfd_generic_link_add_one_symbol
7498 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7499 get_elf_backend_data (abfd)->collect, &bh)))
7500 return FALSE;
7502 h = (struct elf_link_hash_entry *) bh;
7503 h->non_elf = 0;
7504 h->def_regular = 1;
7505 h->type = STT_OBJECT;
7507 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7508 return FALSE;
7509 mips_elf_hash_table (info)->rld_symbol = h;
7513 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7514 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
7515 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7516 return FALSE;
7518 /* Cache the sections created above. */
7519 htab->splt = bfd_get_linker_section (abfd, ".plt");
7520 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
7521 if (htab->is_vxworks)
7523 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7524 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
7526 else
7527 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
7528 if (!htab->sdynbss
7529 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7530 || !htab->srelplt
7531 || !htab->splt)
7532 abort ();
7534 /* Do the usual VxWorks handling. */
7535 if (htab->is_vxworks
7536 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7537 return FALSE;
7539 return TRUE;
7542 /* Return true if relocation REL against section SEC is a REL rather than
7543 RELA relocation. RELOCS is the first relocation in the section and
7544 ABFD is the bfd that contains SEC. */
7546 static bfd_boolean
7547 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7548 const Elf_Internal_Rela *relocs,
7549 const Elf_Internal_Rela *rel)
7551 Elf_Internal_Shdr *rel_hdr;
7552 const struct elf_backend_data *bed;
7554 /* To determine which flavor of relocation this is, we depend on the
7555 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7556 rel_hdr = elf_section_data (sec)->rel.hdr;
7557 if (rel_hdr == NULL)
7558 return FALSE;
7559 bed = get_elf_backend_data (abfd);
7560 return ((size_t) (rel - relocs)
7561 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7564 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7565 HOWTO is the relocation's howto and CONTENTS points to the contents
7566 of the section that REL is against. */
7568 static bfd_vma
7569 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7570 reloc_howto_type *howto, bfd_byte *contents)
7572 bfd_byte *location;
7573 unsigned int r_type;
7574 bfd_vma addend;
7576 r_type = ELF_R_TYPE (abfd, rel->r_info);
7577 location = contents + rel->r_offset;
7579 /* Get the addend, which is stored in the input file. */
7580 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7581 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7582 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7584 return addend & howto->src_mask;
7587 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7588 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7589 and update *ADDEND with the final addend. Return true on success
7590 or false if the LO16 could not be found. RELEND is the exclusive
7591 upper bound on the relocations for REL's section. */
7593 static bfd_boolean
7594 mips_elf_add_lo16_rel_addend (bfd *abfd,
7595 const Elf_Internal_Rela *rel,
7596 const Elf_Internal_Rela *relend,
7597 bfd_byte *contents, bfd_vma *addend)
7599 unsigned int r_type, lo16_type;
7600 const Elf_Internal_Rela *lo16_relocation;
7601 reloc_howto_type *lo16_howto;
7602 bfd_vma l;
7604 r_type = ELF_R_TYPE (abfd, rel->r_info);
7605 if (mips16_reloc_p (r_type))
7606 lo16_type = R_MIPS16_LO16;
7607 else if (micromips_reloc_p (r_type))
7608 lo16_type = R_MICROMIPS_LO16;
7609 else
7610 lo16_type = R_MIPS_LO16;
7612 /* The combined value is the sum of the HI16 addend, left-shifted by
7613 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7614 code does a `lui' of the HI16 value, and then an `addiu' of the
7615 LO16 value.)
7617 Scan ahead to find a matching LO16 relocation.
7619 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7620 be immediately following. However, for the IRIX6 ABI, the next
7621 relocation may be a composed relocation consisting of several
7622 relocations for the same address. In that case, the R_MIPS_LO16
7623 relocation may occur as one of these. We permit a similar
7624 extension in general, as that is useful for GCC.
7626 In some cases GCC dead code elimination removes the LO16 but keeps
7627 the corresponding HI16. This is strictly speaking a violation of
7628 the ABI but not immediately harmful. */
7629 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7630 if (lo16_relocation == NULL)
7631 return FALSE;
7633 /* Obtain the addend kept there. */
7634 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7635 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7637 l <<= lo16_howto->rightshift;
7638 l = _bfd_mips_elf_sign_extend (l, 16);
7640 *addend <<= 16;
7641 *addend += l;
7642 return TRUE;
7645 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7646 store the contents in *CONTENTS on success. Assume that *CONTENTS
7647 already holds the contents if it is nonull on entry. */
7649 static bfd_boolean
7650 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7652 if (*contents)
7653 return TRUE;
7655 /* Get cached copy if it exists. */
7656 if (elf_section_data (sec)->this_hdr.contents != NULL)
7658 *contents = elf_section_data (sec)->this_hdr.contents;
7659 return TRUE;
7662 return bfd_malloc_and_get_section (abfd, sec, contents);
7665 /* Make a new PLT record to keep internal data. */
7667 static struct plt_entry *
7668 mips_elf_make_plt_record (bfd *abfd)
7670 struct plt_entry *entry;
7672 entry = bfd_zalloc (abfd, sizeof (*entry));
7673 if (entry == NULL)
7674 return NULL;
7676 entry->stub_offset = MINUS_ONE;
7677 entry->mips_offset = MINUS_ONE;
7678 entry->comp_offset = MINUS_ONE;
7679 entry->gotplt_index = MINUS_ONE;
7680 return entry;
7683 /* Look through the relocs for a section during the first phase, and
7684 allocate space in the global offset table and record the need for
7685 standard MIPS and compressed procedure linkage table entries. */
7687 bfd_boolean
7688 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7689 asection *sec, const Elf_Internal_Rela *relocs)
7691 const char *name;
7692 bfd *dynobj;
7693 Elf_Internal_Shdr *symtab_hdr;
7694 struct elf_link_hash_entry **sym_hashes;
7695 size_t extsymoff;
7696 const Elf_Internal_Rela *rel;
7697 const Elf_Internal_Rela *rel_end;
7698 asection *sreloc;
7699 const struct elf_backend_data *bed;
7700 struct mips_elf_link_hash_table *htab;
7701 bfd_byte *contents;
7702 bfd_vma addend;
7703 reloc_howto_type *howto;
7705 if (info->relocatable)
7706 return TRUE;
7708 htab = mips_elf_hash_table (info);
7709 BFD_ASSERT (htab != NULL);
7711 dynobj = elf_hash_table (info)->dynobj;
7712 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7713 sym_hashes = elf_sym_hashes (abfd);
7714 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7716 bed = get_elf_backend_data (abfd);
7717 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7719 /* Check for the mips16 stub sections. */
7721 name = bfd_get_section_name (abfd, sec);
7722 if (FN_STUB_P (name))
7724 unsigned long r_symndx;
7726 /* Look at the relocation information to figure out which symbol
7727 this is for. */
7729 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7730 if (r_symndx == 0)
7732 (*_bfd_error_handler)
7733 (_("%B: Warning: cannot determine the target function for"
7734 " stub section `%s'"),
7735 abfd, name);
7736 bfd_set_error (bfd_error_bad_value);
7737 return FALSE;
7740 if (r_symndx < extsymoff
7741 || sym_hashes[r_symndx - extsymoff] == NULL)
7743 asection *o;
7745 /* This stub is for a local symbol. This stub will only be
7746 needed if there is some relocation in this BFD, other
7747 than a 16 bit function call, which refers to this symbol. */
7748 for (o = abfd->sections; o != NULL; o = o->next)
7750 Elf_Internal_Rela *sec_relocs;
7751 const Elf_Internal_Rela *r, *rend;
7753 /* We can ignore stub sections when looking for relocs. */
7754 if ((o->flags & SEC_RELOC) == 0
7755 || o->reloc_count == 0
7756 || section_allows_mips16_refs_p (o))
7757 continue;
7759 sec_relocs
7760 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7761 info->keep_memory);
7762 if (sec_relocs == NULL)
7763 return FALSE;
7765 rend = sec_relocs + o->reloc_count;
7766 for (r = sec_relocs; r < rend; r++)
7767 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7768 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7769 break;
7771 if (elf_section_data (o)->relocs != sec_relocs)
7772 free (sec_relocs);
7774 if (r < rend)
7775 break;
7778 if (o == NULL)
7780 /* There is no non-call reloc for this stub, so we do
7781 not need it. Since this function is called before
7782 the linker maps input sections to output sections, we
7783 can easily discard it by setting the SEC_EXCLUDE
7784 flag. */
7785 sec->flags |= SEC_EXCLUDE;
7786 return TRUE;
7789 /* Record this stub in an array of local symbol stubs for
7790 this BFD. */
7791 if (mips_elf_tdata (abfd)->local_stubs == NULL)
7793 unsigned long symcount;
7794 asection **n;
7795 bfd_size_type amt;
7797 if (elf_bad_symtab (abfd))
7798 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7799 else
7800 symcount = symtab_hdr->sh_info;
7801 amt = symcount * sizeof (asection *);
7802 n = bfd_zalloc (abfd, amt);
7803 if (n == NULL)
7804 return FALSE;
7805 mips_elf_tdata (abfd)->local_stubs = n;
7808 sec->flags |= SEC_KEEP;
7809 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7811 /* We don't need to set mips16_stubs_seen in this case.
7812 That flag is used to see whether we need to look through
7813 the global symbol table for stubs. We don't need to set
7814 it here, because we just have a local stub. */
7816 else
7818 struct mips_elf_link_hash_entry *h;
7820 h = ((struct mips_elf_link_hash_entry *)
7821 sym_hashes[r_symndx - extsymoff]);
7823 while (h->root.root.type == bfd_link_hash_indirect
7824 || h->root.root.type == bfd_link_hash_warning)
7825 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7827 /* H is the symbol this stub is for. */
7829 /* If we already have an appropriate stub for this function, we
7830 don't need another one, so we can discard this one. Since
7831 this function is called before the linker maps input sections
7832 to output sections, we can easily discard it by setting the
7833 SEC_EXCLUDE flag. */
7834 if (h->fn_stub != NULL)
7836 sec->flags |= SEC_EXCLUDE;
7837 return TRUE;
7840 sec->flags |= SEC_KEEP;
7841 h->fn_stub = sec;
7842 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7845 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7847 unsigned long r_symndx;
7848 struct mips_elf_link_hash_entry *h;
7849 asection **loc;
7851 /* Look at the relocation information to figure out which symbol
7852 this is for. */
7854 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7855 if (r_symndx == 0)
7857 (*_bfd_error_handler)
7858 (_("%B: Warning: cannot determine the target function for"
7859 " stub section `%s'"),
7860 abfd, name);
7861 bfd_set_error (bfd_error_bad_value);
7862 return FALSE;
7865 if (r_symndx < extsymoff
7866 || sym_hashes[r_symndx - extsymoff] == NULL)
7868 asection *o;
7870 /* This stub is for a local symbol. This stub will only be
7871 needed if there is some relocation (R_MIPS16_26) in this BFD
7872 that refers to this symbol. */
7873 for (o = abfd->sections; o != NULL; o = o->next)
7875 Elf_Internal_Rela *sec_relocs;
7876 const Elf_Internal_Rela *r, *rend;
7878 /* We can ignore stub sections when looking for relocs. */
7879 if ((o->flags & SEC_RELOC) == 0
7880 || o->reloc_count == 0
7881 || section_allows_mips16_refs_p (o))
7882 continue;
7884 sec_relocs
7885 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7886 info->keep_memory);
7887 if (sec_relocs == NULL)
7888 return FALSE;
7890 rend = sec_relocs + o->reloc_count;
7891 for (r = sec_relocs; r < rend; r++)
7892 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7893 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7894 break;
7896 if (elf_section_data (o)->relocs != sec_relocs)
7897 free (sec_relocs);
7899 if (r < rend)
7900 break;
7903 if (o == NULL)
7905 /* There is no non-call reloc for this stub, so we do
7906 not need it. Since this function is called before
7907 the linker maps input sections to output sections, we
7908 can easily discard it by setting the SEC_EXCLUDE
7909 flag. */
7910 sec->flags |= SEC_EXCLUDE;
7911 return TRUE;
7914 /* Record this stub in an array of local symbol call_stubs for
7915 this BFD. */
7916 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
7918 unsigned long symcount;
7919 asection **n;
7920 bfd_size_type amt;
7922 if (elf_bad_symtab (abfd))
7923 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7924 else
7925 symcount = symtab_hdr->sh_info;
7926 amt = symcount * sizeof (asection *);
7927 n = bfd_zalloc (abfd, amt);
7928 if (n == NULL)
7929 return FALSE;
7930 mips_elf_tdata (abfd)->local_call_stubs = n;
7933 sec->flags |= SEC_KEEP;
7934 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7936 /* We don't need to set mips16_stubs_seen in this case.
7937 That flag is used to see whether we need to look through
7938 the global symbol table for stubs. We don't need to set
7939 it here, because we just have a local stub. */
7941 else
7943 h = ((struct mips_elf_link_hash_entry *)
7944 sym_hashes[r_symndx - extsymoff]);
7946 /* H is the symbol this stub is for. */
7948 if (CALL_FP_STUB_P (name))
7949 loc = &h->call_fp_stub;
7950 else
7951 loc = &h->call_stub;
7953 /* If we already have an appropriate stub for this function, we
7954 don't need another one, so we can discard this one. Since
7955 this function is called before the linker maps input sections
7956 to output sections, we can easily discard it by setting the
7957 SEC_EXCLUDE flag. */
7958 if (*loc != NULL)
7960 sec->flags |= SEC_EXCLUDE;
7961 return TRUE;
7964 sec->flags |= SEC_KEEP;
7965 *loc = sec;
7966 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7970 sreloc = NULL;
7971 contents = NULL;
7972 for (rel = relocs; rel < rel_end; ++rel)
7974 unsigned long r_symndx;
7975 unsigned int r_type;
7976 struct elf_link_hash_entry *h;
7977 bfd_boolean can_make_dynamic_p;
7978 bfd_boolean call_reloc_p;
7979 bfd_boolean constrain_symbol_p;
7981 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7982 r_type = ELF_R_TYPE (abfd, rel->r_info);
7984 if (r_symndx < extsymoff)
7985 h = NULL;
7986 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7988 (*_bfd_error_handler)
7989 (_("%B: Malformed reloc detected for section %s"),
7990 abfd, name);
7991 bfd_set_error (bfd_error_bad_value);
7992 return FALSE;
7994 else
7996 h = sym_hashes[r_symndx - extsymoff];
7997 if (h != NULL)
7999 while (h->root.type == bfd_link_hash_indirect
8000 || h->root.type == bfd_link_hash_warning)
8001 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8003 /* PR15323, ref flags aren't set for references in the
8004 same object. */
8005 h->root.non_ir_ref = 1;
8009 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8010 relocation into a dynamic one. */
8011 can_make_dynamic_p = FALSE;
8013 /* Set CALL_RELOC_P to true if the relocation is for a call,
8014 and if pointer equality therefore doesn't matter. */
8015 call_reloc_p = FALSE;
8017 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8018 into account when deciding how to define the symbol.
8019 Relocations in nonallocatable sections such as .pdr and
8020 .debug* should have no effect. */
8021 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8023 switch (r_type)
8025 case R_MIPS_CALL16:
8026 case R_MIPS_CALL_HI16:
8027 case R_MIPS_CALL_LO16:
8028 case R_MIPS16_CALL16:
8029 case R_MICROMIPS_CALL16:
8030 case R_MICROMIPS_CALL_HI16:
8031 case R_MICROMIPS_CALL_LO16:
8032 call_reloc_p = TRUE;
8033 /* Fall through. */
8035 case R_MIPS_GOT16:
8036 case R_MIPS_GOT_HI16:
8037 case R_MIPS_GOT_LO16:
8038 case R_MIPS_GOT_PAGE:
8039 case R_MIPS_GOT_OFST:
8040 case R_MIPS_GOT_DISP:
8041 case R_MIPS_TLS_GOTTPREL:
8042 case R_MIPS_TLS_GD:
8043 case R_MIPS_TLS_LDM:
8044 case R_MIPS16_GOT16:
8045 case R_MIPS16_TLS_GOTTPREL:
8046 case R_MIPS16_TLS_GD:
8047 case R_MIPS16_TLS_LDM:
8048 case R_MICROMIPS_GOT16:
8049 case R_MICROMIPS_GOT_HI16:
8050 case R_MICROMIPS_GOT_LO16:
8051 case R_MICROMIPS_GOT_PAGE:
8052 case R_MICROMIPS_GOT_OFST:
8053 case R_MICROMIPS_GOT_DISP:
8054 case R_MICROMIPS_TLS_GOTTPREL:
8055 case R_MICROMIPS_TLS_GD:
8056 case R_MICROMIPS_TLS_LDM:
8057 if (dynobj == NULL)
8058 elf_hash_table (info)->dynobj = dynobj = abfd;
8059 if (!mips_elf_create_got_section (dynobj, info))
8060 return FALSE;
8061 if (htab->is_vxworks && !info->shared)
8063 (*_bfd_error_handler)
8064 (_("%B: GOT reloc at 0x%lx not expected in executables"),
8065 abfd, (unsigned long) rel->r_offset);
8066 bfd_set_error (bfd_error_bad_value);
8067 return FALSE;
8069 can_make_dynamic_p = TRUE;
8070 break;
8072 case R_MIPS_NONE:
8073 case R_MIPS_JALR:
8074 case R_MICROMIPS_JALR:
8075 /* These relocations have empty fields and are purely there to
8076 provide link information. The symbol value doesn't matter. */
8077 constrain_symbol_p = FALSE;
8078 break;
8080 case R_MIPS_GPREL16:
8081 case R_MIPS_GPREL32:
8082 case R_MIPS16_GPREL:
8083 case R_MICROMIPS_GPREL16:
8084 /* GP-relative relocations always resolve to a definition in a
8085 regular input file, ignoring the one-definition rule. This is
8086 important for the GP setup sequence in NewABI code, which
8087 always resolves to a local function even if other relocations
8088 against the symbol wouldn't. */
8089 constrain_symbol_p = FALSE;
8090 break;
8092 case R_MIPS_32:
8093 case R_MIPS_REL32:
8094 case R_MIPS_64:
8095 /* In VxWorks executables, references to external symbols
8096 must be handled using copy relocs or PLT entries; it is not
8097 possible to convert this relocation into a dynamic one.
8099 For executables that use PLTs and copy-relocs, we have a
8100 choice between converting the relocation into a dynamic
8101 one or using copy relocations or PLT entries. It is
8102 usually better to do the former, unless the relocation is
8103 against a read-only section. */
8104 if ((info->shared
8105 || (h != NULL
8106 && !htab->is_vxworks
8107 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8108 && !(!info->nocopyreloc
8109 && !PIC_OBJECT_P (abfd)
8110 && MIPS_ELF_READONLY_SECTION (sec))))
8111 && (sec->flags & SEC_ALLOC) != 0)
8113 can_make_dynamic_p = TRUE;
8114 if (dynobj == NULL)
8115 elf_hash_table (info)->dynobj = dynobj = abfd;
8117 break;
8119 case R_MIPS_26:
8120 case R_MIPS_PC16:
8121 case R_MIPS16_26:
8122 case R_MICROMIPS_26_S1:
8123 case R_MICROMIPS_PC7_S1:
8124 case R_MICROMIPS_PC10_S1:
8125 case R_MICROMIPS_PC16_S1:
8126 case R_MICROMIPS_PC23_S2:
8127 call_reloc_p = TRUE;
8128 break;
8131 if (h)
8133 if (constrain_symbol_p)
8135 if (!can_make_dynamic_p)
8136 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8138 if (!call_reloc_p)
8139 h->pointer_equality_needed = 1;
8141 /* We must not create a stub for a symbol that has
8142 relocations related to taking the function's address.
8143 This doesn't apply to VxWorks, where CALL relocs refer
8144 to a .got.plt entry instead of a normal .got entry. */
8145 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8146 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8149 /* Relocations against the special VxWorks __GOTT_BASE__ and
8150 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8151 room for them in .rela.dyn. */
8152 if (is_gott_symbol (info, h))
8154 if (sreloc == NULL)
8156 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8157 if (sreloc == NULL)
8158 return FALSE;
8160 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8161 if (MIPS_ELF_READONLY_SECTION (sec))
8162 /* We tell the dynamic linker that there are
8163 relocations against the text segment. */
8164 info->flags |= DF_TEXTREL;
8167 else if (call_lo16_reloc_p (r_type)
8168 || got_lo16_reloc_p (r_type)
8169 || got_disp_reloc_p (r_type)
8170 || (got16_reloc_p (r_type) && htab->is_vxworks))
8172 /* We may need a local GOT entry for this relocation. We
8173 don't count R_MIPS_GOT_PAGE because we can estimate the
8174 maximum number of pages needed by looking at the size of
8175 the segment. Similar comments apply to R_MIPS*_GOT16 and
8176 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8177 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8178 R_MIPS_CALL_HI16 because these are always followed by an
8179 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8180 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8181 rel->r_addend, info, r_type))
8182 return FALSE;
8185 if (h != NULL
8186 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8187 ELF_ST_IS_MIPS16 (h->other)))
8188 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8190 switch (r_type)
8192 case R_MIPS_CALL16:
8193 case R_MIPS16_CALL16:
8194 case R_MICROMIPS_CALL16:
8195 if (h == NULL)
8197 (*_bfd_error_handler)
8198 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
8199 abfd, (unsigned long) rel->r_offset);
8200 bfd_set_error (bfd_error_bad_value);
8201 return FALSE;
8203 /* Fall through. */
8205 case R_MIPS_CALL_HI16:
8206 case R_MIPS_CALL_LO16:
8207 case R_MICROMIPS_CALL_HI16:
8208 case R_MICROMIPS_CALL_LO16:
8209 if (h != NULL)
8211 /* Make sure there is room in the regular GOT to hold the
8212 function's address. We may eliminate it in favour of
8213 a .got.plt entry later; see mips_elf_count_got_symbols. */
8214 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8215 r_type))
8216 return FALSE;
8218 /* We need a stub, not a plt entry for the undefined
8219 function. But we record it as if it needs plt. See
8220 _bfd_elf_adjust_dynamic_symbol. */
8221 h->needs_plt = 1;
8222 h->type = STT_FUNC;
8224 break;
8226 case R_MIPS_GOT_PAGE:
8227 case R_MICROMIPS_GOT_PAGE:
8228 case R_MIPS16_GOT16:
8229 case R_MIPS_GOT16:
8230 case R_MIPS_GOT_HI16:
8231 case R_MIPS_GOT_LO16:
8232 case R_MICROMIPS_GOT16:
8233 case R_MICROMIPS_GOT_HI16:
8234 case R_MICROMIPS_GOT_LO16:
8235 if (!h || got_page_reloc_p (r_type))
8237 /* This relocation needs (or may need, if h != NULL) a
8238 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8239 know for sure until we know whether the symbol is
8240 preemptible. */
8241 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8243 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8244 return FALSE;
8245 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8246 addend = mips_elf_read_rel_addend (abfd, rel,
8247 howto, contents);
8248 if (got16_reloc_p (r_type))
8249 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8250 contents, &addend);
8251 else
8252 addend <<= howto->rightshift;
8254 else
8255 addend = rel->r_addend;
8256 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8257 h, addend))
8258 return FALSE;
8260 if (h)
8262 struct mips_elf_link_hash_entry *hmips =
8263 (struct mips_elf_link_hash_entry *) h;
8265 /* This symbol is definitely not overridable. */
8266 if (hmips->root.def_regular
8267 && ! (info->shared && ! info->symbolic
8268 && ! hmips->root.forced_local))
8269 h = NULL;
8272 /* If this is a global, overridable symbol, GOT_PAGE will
8273 decay to GOT_DISP, so we'll need a GOT entry for it. */
8274 /* Fall through. */
8276 case R_MIPS_GOT_DISP:
8277 case R_MICROMIPS_GOT_DISP:
8278 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8279 FALSE, r_type))
8280 return FALSE;
8281 break;
8283 case R_MIPS_TLS_GOTTPREL:
8284 case R_MIPS16_TLS_GOTTPREL:
8285 case R_MICROMIPS_TLS_GOTTPREL:
8286 if (info->shared)
8287 info->flags |= DF_STATIC_TLS;
8288 /* Fall through */
8290 case R_MIPS_TLS_LDM:
8291 case R_MIPS16_TLS_LDM:
8292 case R_MICROMIPS_TLS_LDM:
8293 if (tls_ldm_reloc_p (r_type))
8295 r_symndx = STN_UNDEF;
8296 h = NULL;
8298 /* Fall through */
8300 case R_MIPS_TLS_GD:
8301 case R_MIPS16_TLS_GD:
8302 case R_MICROMIPS_TLS_GD:
8303 /* This symbol requires a global offset table entry, or two
8304 for TLS GD relocations. */
8305 if (h != NULL)
8307 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8308 FALSE, r_type))
8309 return FALSE;
8311 else
8313 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8314 rel->r_addend,
8315 info, r_type))
8316 return FALSE;
8318 break;
8320 case R_MIPS_32:
8321 case R_MIPS_REL32:
8322 case R_MIPS_64:
8323 /* In VxWorks executables, references to external symbols
8324 are handled using copy relocs or PLT stubs, so there's
8325 no need to add a .rela.dyn entry for this relocation. */
8326 if (can_make_dynamic_p)
8328 if (sreloc == NULL)
8330 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8331 if (sreloc == NULL)
8332 return FALSE;
8334 if (info->shared && h == NULL)
8336 /* When creating a shared object, we must copy these
8337 reloc types into the output file as R_MIPS_REL32
8338 relocs. Make room for this reloc in .rel(a).dyn. */
8339 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8340 if (MIPS_ELF_READONLY_SECTION (sec))
8341 /* We tell the dynamic linker that there are
8342 relocations against the text segment. */
8343 info->flags |= DF_TEXTREL;
8345 else
8347 struct mips_elf_link_hash_entry *hmips;
8349 /* For a shared object, we must copy this relocation
8350 unless the symbol turns out to be undefined and
8351 weak with non-default visibility, in which case
8352 it will be left as zero.
8354 We could elide R_MIPS_REL32 for locally binding symbols
8355 in shared libraries, but do not yet do so.
8357 For an executable, we only need to copy this
8358 reloc if the symbol is defined in a dynamic
8359 object. */
8360 hmips = (struct mips_elf_link_hash_entry *) h;
8361 ++hmips->possibly_dynamic_relocs;
8362 if (MIPS_ELF_READONLY_SECTION (sec))
8363 /* We need it to tell the dynamic linker if there
8364 are relocations against the text segment. */
8365 hmips->readonly_reloc = TRUE;
8369 if (SGI_COMPAT (abfd))
8370 mips_elf_hash_table (info)->compact_rel_size +=
8371 sizeof (Elf32_External_crinfo);
8372 break;
8374 case R_MIPS_26:
8375 case R_MIPS_GPREL16:
8376 case R_MIPS_LITERAL:
8377 case R_MIPS_GPREL32:
8378 case R_MICROMIPS_26_S1:
8379 case R_MICROMIPS_GPREL16:
8380 case R_MICROMIPS_LITERAL:
8381 case R_MICROMIPS_GPREL7_S2:
8382 if (SGI_COMPAT (abfd))
8383 mips_elf_hash_table (info)->compact_rel_size +=
8384 sizeof (Elf32_External_crinfo);
8385 break;
8387 /* This relocation describes the C++ object vtable hierarchy.
8388 Reconstruct it for later use during GC. */
8389 case R_MIPS_GNU_VTINHERIT:
8390 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
8391 return FALSE;
8392 break;
8394 /* This relocation describes which C++ vtable entries are actually
8395 used. Record for later use during GC. */
8396 case R_MIPS_GNU_VTENTRY:
8397 BFD_ASSERT (h != NULL);
8398 if (h != NULL
8399 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
8400 return FALSE;
8401 break;
8403 default:
8404 break;
8407 /* Record the need for a PLT entry. At this point we don't know
8408 yet if we are going to create a PLT in the first place, but
8409 we only record whether the relocation requires a standard MIPS
8410 or a compressed code entry anyway. If we don't make a PLT after
8411 all, then we'll just ignore these arrangements. Likewise if
8412 a PLT entry is not created because the symbol is satisfied
8413 locally. */
8414 if (h != NULL
8415 && jal_reloc_p (r_type)
8416 && !SYMBOL_CALLS_LOCAL (info, h))
8418 if (h->plt.plist == NULL)
8419 h->plt.plist = mips_elf_make_plt_record (abfd);
8420 if (h->plt.plist == NULL)
8421 return FALSE;
8423 if (r_type == R_MIPS_26)
8424 h->plt.plist->need_mips = TRUE;
8425 else
8426 h->plt.plist->need_comp = TRUE;
8429 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8430 if there is one. We only need to handle global symbols here;
8431 we decide whether to keep or delete stubs for local symbols
8432 when processing the stub's relocations. */
8433 if (h != NULL
8434 && !mips16_call_reloc_p (r_type)
8435 && !section_allows_mips16_refs_p (sec))
8437 struct mips_elf_link_hash_entry *mh;
8439 mh = (struct mips_elf_link_hash_entry *) h;
8440 mh->need_fn_stub = TRUE;
8443 /* Refuse some position-dependent relocations when creating a
8444 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8445 not PIC, but we can create dynamic relocations and the result
8446 will be fine. Also do not refuse R_MIPS_LO16, which can be
8447 combined with R_MIPS_GOT16. */
8448 if (info->shared)
8450 switch (r_type)
8452 case R_MIPS16_HI16:
8453 case R_MIPS_HI16:
8454 case R_MIPS_HIGHER:
8455 case R_MIPS_HIGHEST:
8456 case R_MICROMIPS_HI16:
8457 case R_MICROMIPS_HIGHER:
8458 case R_MICROMIPS_HIGHEST:
8459 /* Don't refuse a high part relocation if it's against
8460 no symbol (e.g. part of a compound relocation). */
8461 if (r_symndx == STN_UNDEF)
8462 break;
8464 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8465 and has a special meaning. */
8466 if (!NEWABI_P (abfd) && h != NULL
8467 && strcmp (h->root.root.string, "_gp_disp") == 0)
8468 break;
8470 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8471 if (is_gott_symbol (info, h))
8472 break;
8474 /* FALLTHROUGH */
8476 case R_MIPS16_26:
8477 case R_MIPS_26:
8478 case R_MICROMIPS_26_S1:
8479 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8480 (*_bfd_error_handler)
8481 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8482 abfd, howto->name,
8483 (h) ? h->root.root.string : "a local symbol");
8484 bfd_set_error (bfd_error_bad_value);
8485 return FALSE;
8486 default:
8487 break;
8492 return TRUE;
8495 bfd_boolean
8496 _bfd_mips_relax_section (bfd *abfd, asection *sec,
8497 struct bfd_link_info *link_info,
8498 bfd_boolean *again)
8500 Elf_Internal_Rela *internal_relocs;
8501 Elf_Internal_Rela *irel, *irelend;
8502 Elf_Internal_Shdr *symtab_hdr;
8503 bfd_byte *contents = NULL;
8504 size_t extsymoff;
8505 bfd_boolean changed_contents = FALSE;
8506 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8507 Elf_Internal_Sym *isymbuf = NULL;
8509 /* We are not currently changing any sizes, so only one pass. */
8510 *again = FALSE;
8512 if (link_info->relocatable)
8513 return TRUE;
8515 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8516 link_info->keep_memory);
8517 if (internal_relocs == NULL)
8518 return TRUE;
8520 irelend = internal_relocs + sec->reloc_count
8521 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8522 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8523 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8525 for (irel = internal_relocs; irel < irelend; irel++)
8527 bfd_vma symval;
8528 bfd_signed_vma sym_offset;
8529 unsigned int r_type;
8530 unsigned long r_symndx;
8531 asection *sym_sec;
8532 unsigned long instruction;
8534 /* Turn jalr into bgezal, and jr into beq, if they're marked
8535 with a JALR relocation, that indicate where they jump to.
8536 This saves some pipeline bubbles. */
8537 r_type = ELF_R_TYPE (abfd, irel->r_info);
8538 if (r_type != R_MIPS_JALR)
8539 continue;
8541 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8542 /* Compute the address of the jump target. */
8543 if (r_symndx >= extsymoff)
8545 struct mips_elf_link_hash_entry *h
8546 = ((struct mips_elf_link_hash_entry *)
8547 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8549 while (h->root.root.type == bfd_link_hash_indirect
8550 || h->root.root.type == bfd_link_hash_warning)
8551 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8553 /* If a symbol is undefined, or if it may be overridden,
8554 skip it. */
8555 if (! ((h->root.root.type == bfd_link_hash_defined
8556 || h->root.root.type == bfd_link_hash_defweak)
8557 && h->root.root.u.def.section)
8558 || (link_info->shared && ! link_info->symbolic
8559 && !h->root.forced_local))
8560 continue;
8562 sym_sec = h->root.root.u.def.section;
8563 if (sym_sec->output_section)
8564 symval = (h->root.root.u.def.value
8565 + sym_sec->output_section->vma
8566 + sym_sec->output_offset);
8567 else
8568 symval = h->root.root.u.def.value;
8570 else
8572 Elf_Internal_Sym *isym;
8574 /* Read this BFD's symbols if we haven't done so already. */
8575 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8577 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8578 if (isymbuf == NULL)
8579 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8580 symtab_hdr->sh_info, 0,
8581 NULL, NULL, NULL);
8582 if (isymbuf == NULL)
8583 goto relax_return;
8586 isym = isymbuf + r_symndx;
8587 if (isym->st_shndx == SHN_UNDEF)
8588 continue;
8589 else if (isym->st_shndx == SHN_ABS)
8590 sym_sec = bfd_abs_section_ptr;
8591 else if (isym->st_shndx == SHN_COMMON)
8592 sym_sec = bfd_com_section_ptr;
8593 else
8594 sym_sec
8595 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8596 symval = isym->st_value
8597 + sym_sec->output_section->vma
8598 + sym_sec->output_offset;
8601 /* Compute branch offset, from delay slot of the jump to the
8602 branch target. */
8603 sym_offset = (symval + irel->r_addend)
8604 - (sec_start + irel->r_offset + 4);
8606 /* Branch offset must be properly aligned. */
8607 if ((sym_offset & 3) != 0)
8608 continue;
8610 sym_offset >>= 2;
8612 /* Check that it's in range. */
8613 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8614 continue;
8616 /* Get the section contents if we haven't done so already. */
8617 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8618 goto relax_return;
8620 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8622 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8623 if ((instruction & 0xfc1fffff) == 0x0000f809)
8624 instruction = 0x04110000;
8625 /* If it was jr <reg>, turn it into b <target>. */
8626 else if ((instruction & 0xfc1fffff) == 0x00000008)
8627 instruction = 0x10000000;
8628 else
8629 continue;
8631 instruction |= (sym_offset & 0xffff);
8632 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8633 changed_contents = TRUE;
8636 if (contents != NULL
8637 && elf_section_data (sec)->this_hdr.contents != contents)
8639 if (!changed_contents && !link_info->keep_memory)
8640 free (contents);
8641 else
8643 /* Cache the section contents for elf_link_input_bfd. */
8644 elf_section_data (sec)->this_hdr.contents = contents;
8647 return TRUE;
8649 relax_return:
8650 if (contents != NULL
8651 && elf_section_data (sec)->this_hdr.contents != contents)
8652 free (contents);
8653 return FALSE;
8656 /* Allocate space for global sym dynamic relocs. */
8658 static bfd_boolean
8659 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8661 struct bfd_link_info *info = inf;
8662 bfd *dynobj;
8663 struct mips_elf_link_hash_entry *hmips;
8664 struct mips_elf_link_hash_table *htab;
8666 htab = mips_elf_hash_table (info);
8667 BFD_ASSERT (htab != NULL);
8669 dynobj = elf_hash_table (info)->dynobj;
8670 hmips = (struct mips_elf_link_hash_entry *) h;
8672 /* VxWorks executables are handled elsewhere; we only need to
8673 allocate relocations in shared objects. */
8674 if (htab->is_vxworks && !info->shared)
8675 return TRUE;
8677 /* Ignore indirect symbols. All relocations against such symbols
8678 will be redirected to the target symbol. */
8679 if (h->root.type == bfd_link_hash_indirect)
8680 return TRUE;
8682 /* If this symbol is defined in a dynamic object, or we are creating
8683 a shared library, we will need to copy any R_MIPS_32 or
8684 R_MIPS_REL32 relocs against it into the output file. */
8685 if (! info->relocatable
8686 && hmips->possibly_dynamic_relocs != 0
8687 && (h->root.type == bfd_link_hash_defweak
8688 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
8689 || info->shared))
8691 bfd_boolean do_copy = TRUE;
8693 if (h->root.type == bfd_link_hash_undefweak)
8695 /* Do not copy relocations for undefined weak symbols with
8696 non-default visibility. */
8697 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8698 do_copy = FALSE;
8700 /* Make sure undefined weak symbols are output as a dynamic
8701 symbol in PIEs. */
8702 else if (h->dynindx == -1 && !h->forced_local)
8704 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8705 return FALSE;
8709 if (do_copy)
8711 /* Even though we don't directly need a GOT entry for this symbol,
8712 the SVR4 psABI requires it to have a dynamic symbol table
8713 index greater that DT_MIPS_GOTSYM if there are dynamic
8714 relocations against it.
8716 VxWorks does not enforce the same mapping between the GOT
8717 and the symbol table, so the same requirement does not
8718 apply there. */
8719 if (!htab->is_vxworks)
8721 if (hmips->global_got_area > GGA_RELOC_ONLY)
8722 hmips->global_got_area = GGA_RELOC_ONLY;
8723 hmips->got_only_for_calls = FALSE;
8726 mips_elf_allocate_dynamic_relocations
8727 (dynobj, info, hmips->possibly_dynamic_relocs);
8728 if (hmips->readonly_reloc)
8729 /* We tell the dynamic linker that there are relocations
8730 against the text segment. */
8731 info->flags |= DF_TEXTREL;
8735 return TRUE;
8738 /* Adjust a symbol defined by a dynamic object and referenced by a
8739 regular object. The current definition is in some section of the
8740 dynamic object, but we're not including those sections. We have to
8741 change the definition to something the rest of the link can
8742 understand. */
8744 bfd_boolean
8745 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8746 struct elf_link_hash_entry *h)
8748 bfd *dynobj;
8749 struct mips_elf_link_hash_entry *hmips;
8750 struct mips_elf_link_hash_table *htab;
8752 htab = mips_elf_hash_table (info);
8753 BFD_ASSERT (htab != NULL);
8755 dynobj = elf_hash_table (info)->dynobj;
8756 hmips = (struct mips_elf_link_hash_entry *) h;
8758 /* Make sure we know what is going on here. */
8759 BFD_ASSERT (dynobj != NULL
8760 && (h->needs_plt
8761 || h->u.weakdef != NULL
8762 || (h->def_dynamic
8763 && h->ref_regular
8764 && !h->def_regular)));
8766 hmips = (struct mips_elf_link_hash_entry *) h;
8768 /* If there are call relocations against an externally-defined symbol,
8769 see whether we can create a MIPS lazy-binding stub for it. We can
8770 only do this if all references to the function are through call
8771 relocations, and in that case, the traditional lazy-binding stubs
8772 are much more efficient than PLT entries.
8774 Traditional stubs are only available on SVR4 psABI-based systems;
8775 VxWorks always uses PLTs instead. */
8776 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8778 if (! elf_hash_table (info)->dynamic_sections_created)
8779 return TRUE;
8781 /* If this symbol is not defined in a regular file, then set
8782 the symbol to the stub location. This is required to make
8783 function pointers compare as equal between the normal
8784 executable and the shared library. */
8785 if (!h->def_regular)
8787 hmips->needs_lazy_stub = TRUE;
8788 htab->lazy_stub_count++;
8789 return TRUE;
8792 /* As above, VxWorks requires PLT entries for externally-defined
8793 functions that are only accessed through call relocations.
8795 Both VxWorks and non-VxWorks targets also need PLT entries if there
8796 are static-only relocations against an externally-defined function.
8797 This can technically occur for shared libraries if there are
8798 branches to the symbol, although it is unlikely that this will be
8799 used in practice due to the short ranges involved. It can occur
8800 for any relative or absolute relocation in executables; in that
8801 case, the PLT entry becomes the function's canonical address. */
8802 else if (((h->needs_plt && !hmips->no_fn_stub)
8803 || (h->type == STT_FUNC && hmips->has_static_relocs))
8804 && htab->use_plts_and_copy_relocs
8805 && !SYMBOL_CALLS_LOCAL (info, h)
8806 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8807 && h->root.type == bfd_link_hash_undefweak))
8809 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
8810 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
8812 /* If this is the first symbol to need a PLT entry, then make some
8813 basic setup. Also work out PLT entry sizes. We'll need them
8814 for PLT offset calculations. */
8815 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
8817 BFD_ASSERT (htab->sgotplt->size == 0);
8818 BFD_ASSERT (htab->plt_got_index == 0);
8820 /* If we're using the PLT additions to the psABI, each PLT
8821 entry is 16 bytes and the PLT0 entry is 32 bytes.
8822 Encourage better cache usage by aligning. We do this
8823 lazily to avoid pessimizing traditional objects. */
8824 if (!htab->is_vxworks
8825 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8826 return FALSE;
8828 /* Make sure that .got.plt is word-aligned. We do this lazily
8829 for the same reason as above. */
8830 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8831 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8832 return FALSE;
8834 /* On non-VxWorks targets, the first two entries in .got.plt
8835 are reserved. */
8836 if (!htab->is_vxworks)
8837 htab->plt_got_index
8838 += (get_elf_backend_data (dynobj)->got_header_size
8839 / MIPS_ELF_GOT_SIZE (dynobj));
8841 /* On VxWorks, also allocate room for the header's
8842 .rela.plt.unloaded entries. */
8843 if (htab->is_vxworks && !info->shared)
8844 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8846 /* Now work out the sizes of individual PLT entries. */
8847 if (htab->is_vxworks && info->shared)
8848 htab->plt_mips_entry_size
8849 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
8850 else if (htab->is_vxworks)
8851 htab->plt_mips_entry_size
8852 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
8853 else if (newabi_p)
8854 htab->plt_mips_entry_size
8855 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8856 else if (!micromips_p)
8858 htab->plt_mips_entry_size
8859 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8860 htab->plt_comp_entry_size
8861 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
8863 else if (htab->insn32)
8865 htab->plt_mips_entry_size
8866 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8867 htab->plt_comp_entry_size
8868 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
8870 else
8872 htab->plt_mips_entry_size
8873 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8874 htab->plt_comp_entry_size
8875 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
8879 if (h->plt.plist == NULL)
8880 h->plt.plist = mips_elf_make_plt_record (dynobj);
8881 if (h->plt.plist == NULL)
8882 return FALSE;
8884 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
8885 n32 or n64, so always use a standard entry there.
8887 If the symbol has a MIPS16 call stub and gets a PLT entry, then
8888 all MIPS16 calls will go via that stub, and there is no benefit
8889 to having a MIPS16 entry. And in the case of call_stub a
8890 standard entry actually has to be used as the stub ends with a J
8891 instruction. */
8892 if (newabi_p
8893 || htab->is_vxworks
8894 || hmips->call_stub
8895 || hmips->call_fp_stub)
8897 h->plt.plist->need_mips = TRUE;
8898 h->plt.plist->need_comp = FALSE;
8901 /* Otherwise, if there are no direct calls to the function, we
8902 have a free choice of whether to use standard or compressed
8903 entries. Prefer microMIPS entries if the object is known to
8904 contain microMIPS code, so that it becomes possible to create
8905 pure microMIPS binaries. Prefer standard entries otherwise,
8906 because MIPS16 ones are no smaller and are usually slower. */
8907 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
8909 if (micromips_p)
8910 h->plt.plist->need_comp = TRUE;
8911 else
8912 h->plt.plist->need_mips = TRUE;
8915 if (h->plt.plist->need_mips)
8917 h->plt.plist->mips_offset = htab->plt_mips_offset;
8918 htab->plt_mips_offset += htab->plt_mips_entry_size;
8920 if (h->plt.plist->need_comp)
8922 h->plt.plist->comp_offset = htab->plt_comp_offset;
8923 htab->plt_comp_offset += htab->plt_comp_entry_size;
8926 /* Reserve the corresponding .got.plt entry now too. */
8927 h->plt.plist->gotplt_index = htab->plt_got_index++;
8929 /* If the output file has no definition of the symbol, set the
8930 symbol's value to the address of the stub. */
8931 if (!info->shared && !h->def_regular)
8932 hmips->use_plt_entry = TRUE;
8934 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
8935 htab->srelplt->size += (htab->is_vxworks
8936 ? MIPS_ELF_RELA_SIZE (dynobj)
8937 : MIPS_ELF_REL_SIZE (dynobj));
8939 /* Make room for the .rela.plt.unloaded relocations. */
8940 if (htab->is_vxworks && !info->shared)
8941 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8943 /* All relocations against this symbol that could have been made
8944 dynamic will now refer to the PLT entry instead. */
8945 hmips->possibly_dynamic_relocs = 0;
8947 return TRUE;
8950 /* If this is a weak symbol, and there is a real definition, the
8951 processor independent code will have arranged for us to see the
8952 real definition first, and we can just use the same value. */
8953 if (h->u.weakdef != NULL)
8955 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8956 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8957 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8958 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8959 return TRUE;
8962 /* Otherwise, there is nothing further to do for symbols defined
8963 in regular objects. */
8964 if (h->def_regular)
8965 return TRUE;
8967 /* There's also nothing more to do if we'll convert all relocations
8968 against this symbol into dynamic relocations. */
8969 if (!hmips->has_static_relocs)
8970 return TRUE;
8972 /* We're now relying on copy relocations. Complain if we have
8973 some that we can't convert. */
8974 if (!htab->use_plts_and_copy_relocs || info->shared)
8976 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8977 "dynamic symbol %s"),
8978 h->root.root.string);
8979 bfd_set_error (bfd_error_bad_value);
8980 return FALSE;
8983 /* We must allocate the symbol in our .dynbss section, which will
8984 become part of the .bss section of the executable. There will be
8985 an entry for this symbol in the .dynsym section. The dynamic
8986 object will contain position independent code, so all references
8987 from the dynamic object to this symbol will go through the global
8988 offset table. The dynamic linker will use the .dynsym entry to
8989 determine the address it must put in the global offset table, so
8990 both the dynamic object and the regular object will refer to the
8991 same memory location for the variable. */
8993 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
8995 if (htab->is_vxworks)
8996 htab->srelbss->size += sizeof (Elf32_External_Rela);
8997 else
8998 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8999 h->needs_copy = 1;
9002 /* All relocations against this symbol that could have been made
9003 dynamic will now refer to the local copy instead. */
9004 hmips->possibly_dynamic_relocs = 0;
9006 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
9009 /* This function is called after all the input files have been read,
9010 and the input sections have been assigned to output sections. We
9011 check for any mips16 stub sections that we can discard. */
9013 bfd_boolean
9014 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9015 struct bfd_link_info *info)
9017 asection *ri;
9018 struct mips_elf_link_hash_table *htab;
9019 struct mips_htab_traverse_info hti;
9021 htab = mips_elf_hash_table (info);
9022 BFD_ASSERT (htab != NULL);
9024 /* The .reginfo section has a fixed size. */
9025 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
9026 if (ri != NULL)
9027 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
9029 hti.info = info;
9030 hti.output_bfd = output_bfd;
9031 hti.error = FALSE;
9032 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9033 mips_elf_check_symbols, &hti);
9034 if (hti.error)
9035 return FALSE;
9037 return TRUE;
9040 /* If the link uses a GOT, lay it out and work out its size. */
9042 static bfd_boolean
9043 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9045 bfd *dynobj;
9046 asection *s;
9047 struct mips_got_info *g;
9048 bfd_size_type loadable_size = 0;
9049 bfd_size_type page_gotno;
9050 bfd *ibfd;
9051 struct mips_elf_traverse_got_arg tga;
9052 struct mips_elf_link_hash_table *htab;
9054 htab = mips_elf_hash_table (info);
9055 BFD_ASSERT (htab != NULL);
9057 s = htab->sgot;
9058 if (s == NULL)
9059 return TRUE;
9061 dynobj = elf_hash_table (info)->dynobj;
9062 g = htab->got_info;
9064 /* Allocate room for the reserved entries. VxWorks always reserves
9065 3 entries; other objects only reserve 2 entries. */
9066 BFD_ASSERT (g->assigned_gotno == 0);
9067 if (htab->is_vxworks)
9068 htab->reserved_gotno = 3;
9069 else
9070 htab->reserved_gotno = 2;
9071 g->local_gotno += htab->reserved_gotno;
9072 g->assigned_gotno = htab->reserved_gotno;
9074 /* Decide which symbols need to go in the global part of the GOT and
9075 count the number of reloc-only GOT symbols. */
9076 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9078 if (!mips_elf_resolve_final_got_entries (info, g))
9079 return FALSE;
9081 /* Calculate the total loadable size of the output. That
9082 will give us the maximum number of GOT_PAGE entries
9083 required. */
9084 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
9086 asection *subsection;
9088 for (subsection = ibfd->sections;
9089 subsection;
9090 subsection = subsection->next)
9092 if ((subsection->flags & SEC_ALLOC) == 0)
9093 continue;
9094 loadable_size += ((subsection->size + 0xf)
9095 &~ (bfd_size_type) 0xf);
9099 if (htab->is_vxworks)
9100 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9101 relocations against local symbols evaluate to "G", and the EABI does
9102 not include R_MIPS_GOT_PAGE. */
9103 page_gotno = 0;
9104 else
9105 /* Assume there are two loadable segments consisting of contiguous
9106 sections. Is 5 enough? */
9107 page_gotno = (loadable_size >> 16) + 5;
9109 /* Choose the smaller of the two page estimates; both are intended to be
9110 conservative. */
9111 if (page_gotno > g->page_gotno)
9112 page_gotno = g->page_gotno;
9114 g->local_gotno += page_gotno;
9116 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9117 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9118 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9120 /* VxWorks does not support multiple GOTs. It initializes $gp to
9121 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9122 dynamic loader. */
9123 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9125 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9126 return FALSE;
9128 else
9130 /* Record that all bfds use G. This also has the effect of freeing
9131 the per-bfd GOTs, which we no longer need. */
9132 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
9133 if (mips_elf_bfd_got (ibfd, FALSE))
9134 mips_elf_replace_bfd_got (ibfd, g);
9135 mips_elf_replace_bfd_got (output_bfd, g);
9137 /* Set up TLS entries. */
9138 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9139 tga.info = info;
9140 tga.g = g;
9141 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9142 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9143 if (!tga.g)
9144 return FALSE;
9145 BFD_ASSERT (g->tls_assigned_gotno
9146 == g->global_gotno + g->local_gotno + g->tls_gotno);
9148 /* Each VxWorks GOT entry needs an explicit relocation. */
9149 if (htab->is_vxworks && info->shared)
9150 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9152 /* Allocate room for the TLS relocations. */
9153 if (g->relocs)
9154 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9157 return TRUE;
9160 /* Estimate the size of the .MIPS.stubs section. */
9162 static void
9163 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9165 struct mips_elf_link_hash_table *htab;
9166 bfd_size_type dynsymcount;
9168 htab = mips_elf_hash_table (info);
9169 BFD_ASSERT (htab != NULL);
9171 if (htab->lazy_stub_count == 0)
9172 return;
9174 /* IRIX rld assumes that a function stub isn't at the end of the .text
9175 section, so add a dummy entry to the end. */
9176 htab->lazy_stub_count++;
9178 /* Get a worst-case estimate of the number of dynamic symbols needed.
9179 At this point, dynsymcount does not account for section symbols
9180 and count_section_dynsyms may overestimate the number that will
9181 be needed. */
9182 dynsymcount = (elf_hash_table (info)->dynsymcount
9183 + count_section_dynsyms (output_bfd, info));
9185 /* Determine the size of one stub entry. There's no disadvantage
9186 from using microMIPS code here, so for the sake of pure-microMIPS
9187 binaries we prefer it whenever there's any microMIPS code in
9188 output produced at all. This has a benefit of stubs being
9189 shorter by 4 bytes each too, unless in the insn32 mode. */
9190 if (!MICROMIPS_P (output_bfd))
9191 htab->function_stub_size = (dynsymcount > 0x10000
9192 ? MIPS_FUNCTION_STUB_BIG_SIZE
9193 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9194 else if (htab->insn32)
9195 htab->function_stub_size = (dynsymcount > 0x10000
9196 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9197 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9198 else
9199 htab->function_stub_size = (dynsymcount > 0x10000
9200 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9201 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9203 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9206 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9207 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9208 stub, allocate an entry in the stubs section. */
9210 static bfd_boolean
9211 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9213 struct mips_htab_traverse_info *hti = data;
9214 struct mips_elf_link_hash_table *htab;
9215 struct bfd_link_info *info;
9216 bfd *output_bfd;
9218 info = hti->info;
9219 output_bfd = hti->output_bfd;
9220 htab = mips_elf_hash_table (info);
9221 BFD_ASSERT (htab != NULL);
9223 if (h->needs_lazy_stub)
9225 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9226 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9227 bfd_vma isa_bit = micromips_p;
9229 BFD_ASSERT (htab->root.dynobj != NULL);
9230 if (h->root.plt.plist == NULL)
9231 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9232 if (h->root.plt.plist == NULL)
9234 hti->error = TRUE;
9235 return FALSE;
9237 h->root.root.u.def.section = htab->sstubs;
9238 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9239 h->root.plt.plist->stub_offset = htab->sstubs->size;
9240 h->root.other = other;
9241 htab->sstubs->size += htab->function_stub_size;
9243 return TRUE;
9246 /* Allocate offsets in the stubs section to each symbol that needs one.
9247 Set the final size of the .MIPS.stub section. */
9249 static bfd_boolean
9250 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9252 bfd *output_bfd = info->output_bfd;
9253 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9254 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9255 bfd_vma isa_bit = micromips_p;
9256 struct mips_elf_link_hash_table *htab;
9257 struct mips_htab_traverse_info hti;
9258 struct elf_link_hash_entry *h;
9259 bfd *dynobj;
9261 htab = mips_elf_hash_table (info);
9262 BFD_ASSERT (htab != NULL);
9264 if (htab->lazy_stub_count == 0)
9265 return TRUE;
9267 htab->sstubs->size = 0;
9268 hti.info = info;
9269 hti.output_bfd = output_bfd;
9270 hti.error = FALSE;
9271 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9272 if (hti.error)
9273 return FALSE;
9274 htab->sstubs->size += htab->function_stub_size;
9275 BFD_ASSERT (htab->sstubs->size
9276 == htab->lazy_stub_count * htab->function_stub_size);
9278 dynobj = elf_hash_table (info)->dynobj;
9279 BFD_ASSERT (dynobj != NULL);
9280 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9281 if (h == NULL)
9282 return FALSE;
9283 h->root.u.def.value = isa_bit;
9284 h->other = other;
9285 h->type = STT_FUNC;
9287 return TRUE;
9290 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9291 bfd_link_info. If H uses the address of a PLT entry as the value
9292 of the symbol, then set the entry in the symbol table now. Prefer
9293 a standard MIPS PLT entry. */
9295 static bfd_boolean
9296 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9298 struct bfd_link_info *info = data;
9299 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9300 struct mips_elf_link_hash_table *htab;
9301 unsigned int other;
9302 bfd_vma isa_bit;
9303 bfd_vma val;
9305 htab = mips_elf_hash_table (info);
9306 BFD_ASSERT (htab != NULL);
9308 if (h->use_plt_entry)
9310 BFD_ASSERT (h->root.plt.plist != NULL);
9311 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9312 || h->root.plt.plist->comp_offset != MINUS_ONE);
9314 val = htab->plt_header_size;
9315 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9317 isa_bit = 0;
9318 val += h->root.plt.plist->mips_offset;
9319 other = 0;
9321 else
9323 isa_bit = 1;
9324 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9325 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9327 val += isa_bit;
9328 /* For VxWorks, point at the PLT load stub rather than the lazy
9329 resolution stub; this stub will become the canonical function
9330 address. */
9331 if (htab->is_vxworks)
9332 val += 8;
9334 h->root.root.u.def.section = htab->splt;
9335 h->root.root.u.def.value = val;
9336 h->root.other = other;
9339 return TRUE;
9342 /* Set the sizes of the dynamic sections. */
9344 bfd_boolean
9345 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9346 struct bfd_link_info *info)
9348 bfd *dynobj;
9349 asection *s, *sreldyn;
9350 bfd_boolean reltext;
9351 struct mips_elf_link_hash_table *htab;
9353 htab = mips_elf_hash_table (info);
9354 BFD_ASSERT (htab != NULL);
9355 dynobj = elf_hash_table (info)->dynobj;
9356 BFD_ASSERT (dynobj != NULL);
9358 if (elf_hash_table (info)->dynamic_sections_created)
9360 /* Set the contents of the .interp section to the interpreter. */
9361 if (info->executable)
9363 s = bfd_get_linker_section (dynobj, ".interp");
9364 BFD_ASSERT (s != NULL);
9365 s->size
9366 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9367 s->contents
9368 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9371 /* Figure out the size of the PLT header if we know that we
9372 are using it. For the sake of cache alignment always use
9373 a standard header whenever any standard entries are present
9374 even if microMIPS entries are present as well. This also
9375 lets the microMIPS header rely on the value of $v0 only set
9376 by microMIPS entries, for a small size reduction.
9378 Set symbol table entry values for symbols that use the
9379 address of their PLT entry now that we can calculate it.
9381 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9382 haven't already in _bfd_elf_create_dynamic_sections. */
9383 if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
9385 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9386 && !htab->plt_mips_offset);
9387 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9388 bfd_vma isa_bit = micromips_p;
9389 struct elf_link_hash_entry *h;
9390 bfd_vma size;
9392 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9393 BFD_ASSERT (htab->sgotplt->size == 0);
9394 BFD_ASSERT (htab->splt->size == 0);
9396 if (htab->is_vxworks && info->shared)
9397 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9398 else if (htab->is_vxworks)
9399 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9400 else if (ABI_64_P (output_bfd))
9401 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9402 else if (ABI_N32_P (output_bfd))
9403 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9404 else if (!micromips_p)
9405 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
9406 else if (htab->insn32)
9407 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
9408 else
9409 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
9411 htab->plt_header_is_comp = micromips_p;
9412 htab->plt_header_size = size;
9413 htab->splt->size = (size
9414 + htab->plt_mips_offset
9415 + htab->plt_comp_offset);
9416 htab->sgotplt->size = (htab->plt_got_index
9417 * MIPS_ELF_GOT_SIZE (dynobj));
9419 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9421 if (htab->root.hplt == NULL)
9423 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
9424 "_PROCEDURE_LINKAGE_TABLE_");
9425 htab->root.hplt = h;
9426 if (h == NULL)
9427 return FALSE;
9430 h = htab->root.hplt;
9431 h->root.u.def.value = isa_bit;
9432 h->other = other;
9433 h->type = STT_FUNC;
9437 /* Allocate space for global sym dynamic relocs. */
9438 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9440 mips_elf_estimate_stub_size (output_bfd, info);
9442 if (!mips_elf_lay_out_got (output_bfd, info))
9443 return FALSE;
9445 mips_elf_lay_out_lazy_stubs (info);
9447 /* The check_relocs and adjust_dynamic_symbol entry points have
9448 determined the sizes of the various dynamic sections. Allocate
9449 memory for them. */
9450 reltext = FALSE;
9451 for (s = dynobj->sections; s != NULL; s = s->next)
9453 const char *name;
9455 /* It's OK to base decisions on the section name, because none
9456 of the dynobj section names depend upon the input files. */
9457 name = bfd_get_section_name (dynobj, s);
9459 if ((s->flags & SEC_LINKER_CREATED) == 0)
9460 continue;
9462 if (CONST_STRNEQ (name, ".rel"))
9464 if (s->size != 0)
9466 const char *outname;
9467 asection *target;
9469 /* If this relocation section applies to a read only
9470 section, then we probably need a DT_TEXTREL entry.
9471 If the relocation section is .rel(a).dyn, we always
9472 assert a DT_TEXTREL entry rather than testing whether
9473 there exists a relocation to a read only section or
9474 not. */
9475 outname = bfd_get_section_name (output_bfd,
9476 s->output_section);
9477 target = bfd_get_section_by_name (output_bfd, outname + 4);
9478 if ((target != NULL
9479 && (target->flags & SEC_READONLY) != 0
9480 && (target->flags & SEC_ALLOC) != 0)
9481 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
9482 reltext = TRUE;
9484 /* We use the reloc_count field as a counter if we need
9485 to copy relocs into the output file. */
9486 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
9487 s->reloc_count = 0;
9489 /* If combreloc is enabled, elf_link_sort_relocs() will
9490 sort relocations, but in a different way than we do,
9491 and before we're done creating relocations. Also, it
9492 will move them around between input sections'
9493 relocation's contents, so our sorting would be
9494 broken, so don't let it run. */
9495 info->combreloc = 0;
9498 else if (! info->shared
9499 && ! mips_elf_hash_table (info)->use_rld_obj_head
9500 && CONST_STRNEQ (name, ".rld_map"))
9502 /* We add a room for __rld_map. It will be filled in by the
9503 rtld to contain a pointer to the _r_debug structure. */
9504 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
9506 else if (SGI_COMPAT (output_bfd)
9507 && CONST_STRNEQ (name, ".compact_rel"))
9508 s->size += mips_elf_hash_table (info)->compact_rel_size;
9509 else if (s == htab->splt)
9511 /* If the last PLT entry has a branch delay slot, allocate
9512 room for an extra nop to fill the delay slot. This is
9513 for CPUs without load interlocking. */
9514 if (! LOAD_INTERLOCKS_P (output_bfd)
9515 && ! htab->is_vxworks && s->size > 0)
9516 s->size += 4;
9518 else if (! CONST_STRNEQ (name, ".init")
9519 && s != htab->sgot
9520 && s != htab->sgotplt
9521 && s != htab->sstubs
9522 && s != htab->sdynbss)
9524 /* It's not one of our sections, so don't allocate space. */
9525 continue;
9528 if (s->size == 0)
9530 s->flags |= SEC_EXCLUDE;
9531 continue;
9534 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9535 continue;
9537 /* Allocate memory for the section contents. */
9538 s->contents = bfd_zalloc (dynobj, s->size);
9539 if (s->contents == NULL)
9541 bfd_set_error (bfd_error_no_memory);
9542 return FALSE;
9546 if (elf_hash_table (info)->dynamic_sections_created)
9548 /* Add some entries to the .dynamic section. We fill in the
9549 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9550 must add the entries now so that we get the correct size for
9551 the .dynamic section. */
9553 /* SGI object has the equivalence of DT_DEBUG in the
9554 DT_MIPS_RLD_MAP entry. This must come first because glibc
9555 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9556 may only look at the first one they see. */
9557 if (!info->shared
9558 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9559 return FALSE;
9561 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9562 used by the debugger. */
9563 if (info->executable
9564 && !SGI_COMPAT (output_bfd)
9565 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9566 return FALSE;
9568 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
9569 info->flags |= DF_TEXTREL;
9571 if ((info->flags & DF_TEXTREL) != 0)
9573 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
9574 return FALSE;
9576 /* Clear the DF_TEXTREL flag. It will be set again if we
9577 write out an actual text relocation; we may not, because
9578 at this point we do not know whether e.g. any .eh_frame
9579 absolute relocations have been converted to PC-relative. */
9580 info->flags &= ~DF_TEXTREL;
9583 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
9584 return FALSE;
9586 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
9587 if (htab->is_vxworks)
9589 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9590 use any of the DT_MIPS_* tags. */
9591 if (sreldyn && sreldyn->size > 0)
9593 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9594 return FALSE;
9596 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9597 return FALSE;
9599 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9600 return FALSE;
9603 else
9605 if (sreldyn && sreldyn->size > 0)
9607 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9608 return FALSE;
9610 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9611 return FALSE;
9613 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9614 return FALSE;
9617 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9618 return FALSE;
9620 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9621 return FALSE;
9623 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9624 return FALSE;
9626 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9627 return FALSE;
9629 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9630 return FALSE;
9632 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9633 return FALSE;
9635 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9636 return FALSE;
9638 if (IRIX_COMPAT (dynobj) == ict_irix5
9639 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9640 return FALSE;
9642 if (IRIX_COMPAT (dynobj) == ict_irix6
9643 && (bfd_get_section_by_name
9644 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
9645 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9646 return FALSE;
9648 if (htab->splt->size > 0)
9650 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9651 return FALSE;
9653 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9654 return FALSE;
9656 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9657 return FALSE;
9659 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9660 return FALSE;
9662 if (htab->is_vxworks
9663 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9664 return FALSE;
9667 return TRUE;
9670 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9671 Adjust its R_ADDEND field so that it is correct for the output file.
9672 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9673 and sections respectively; both use symbol indexes. */
9675 static void
9676 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9677 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9678 asection **local_sections, Elf_Internal_Rela *rel)
9680 unsigned int r_type, r_symndx;
9681 Elf_Internal_Sym *sym;
9682 asection *sec;
9684 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9686 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9687 if (gprel16_reloc_p (r_type)
9688 || r_type == R_MIPS_GPREL32
9689 || literal_reloc_p (r_type))
9691 rel->r_addend += _bfd_get_gp_value (input_bfd);
9692 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9695 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9696 sym = local_syms + r_symndx;
9698 /* Adjust REL's addend to account for section merging. */
9699 if (!info->relocatable)
9701 sec = local_sections[r_symndx];
9702 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9705 /* This would normally be done by the rela_normal code in elflink.c. */
9706 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9707 rel->r_addend += local_sections[r_symndx]->output_offset;
9711 /* Handle relocations against symbols from removed linkonce sections,
9712 or sections discarded by a linker script. We use this wrapper around
9713 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9714 on 64-bit ELF targets. In this case for any relocation handled, which
9715 always be the first in a triplet, the remaining two have to be processed
9716 together with the first, even if they are R_MIPS_NONE. It is the symbol
9717 index referred by the first reloc that applies to all the three and the
9718 remaining two never refer to an object symbol. And it is the final
9719 relocation (the last non-null one) that determines the output field of
9720 the whole relocation so retrieve the corresponding howto structure for
9721 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9723 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9724 and therefore requires to be pasted in a loop. It also defines a block
9725 and does not protect any of its arguments, hence the extra brackets. */
9727 static void
9728 mips_reloc_against_discarded_section (bfd *output_bfd,
9729 struct bfd_link_info *info,
9730 bfd *input_bfd, asection *input_section,
9731 Elf_Internal_Rela **rel,
9732 const Elf_Internal_Rela **relend,
9733 bfd_boolean rel_reloc,
9734 reloc_howto_type *howto,
9735 bfd_byte *contents)
9737 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9738 int count = bed->s->int_rels_per_ext_rel;
9739 unsigned int r_type;
9740 int i;
9742 for (i = count - 1; i > 0; i--)
9744 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9745 if (r_type != R_MIPS_NONE)
9747 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9748 break;
9753 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9754 (*rel), count, (*relend),
9755 howto, i, contents);
9757 while (0);
9760 /* Relocate a MIPS ELF section. */
9762 bfd_boolean
9763 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9764 bfd *input_bfd, asection *input_section,
9765 bfd_byte *contents, Elf_Internal_Rela *relocs,
9766 Elf_Internal_Sym *local_syms,
9767 asection **local_sections)
9769 Elf_Internal_Rela *rel;
9770 const Elf_Internal_Rela *relend;
9771 bfd_vma addend = 0;
9772 bfd_boolean use_saved_addend_p = FALSE;
9773 const struct elf_backend_data *bed;
9775 bed = get_elf_backend_data (output_bfd);
9776 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9777 for (rel = relocs; rel < relend; ++rel)
9779 const char *name;
9780 bfd_vma value = 0;
9781 reloc_howto_type *howto;
9782 bfd_boolean cross_mode_jump_p = FALSE;
9783 /* TRUE if the relocation is a RELA relocation, rather than a
9784 REL relocation. */
9785 bfd_boolean rela_relocation_p = TRUE;
9786 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9787 const char *msg;
9788 unsigned long r_symndx;
9789 asection *sec;
9790 Elf_Internal_Shdr *symtab_hdr;
9791 struct elf_link_hash_entry *h;
9792 bfd_boolean rel_reloc;
9794 rel_reloc = (NEWABI_P (input_bfd)
9795 && mips_elf_rel_relocation_p (input_bfd, input_section,
9796 relocs, rel));
9797 /* Find the relocation howto for this relocation. */
9798 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9800 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
9801 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9802 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9804 sec = local_sections[r_symndx];
9805 h = NULL;
9807 else
9809 unsigned long extsymoff;
9811 extsymoff = 0;
9812 if (!elf_bad_symtab (input_bfd))
9813 extsymoff = symtab_hdr->sh_info;
9814 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9815 while (h->root.type == bfd_link_hash_indirect
9816 || h->root.type == bfd_link_hash_warning)
9817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9819 sec = NULL;
9820 if (h->root.type == bfd_link_hash_defined
9821 || h->root.type == bfd_link_hash_defweak)
9822 sec = h->root.u.def.section;
9825 if (sec != NULL && discarded_section (sec))
9827 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
9828 input_section, &rel, &relend,
9829 rel_reloc, howto, contents);
9830 continue;
9833 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9835 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9836 64-bit code, but make sure all their addresses are in the
9837 lowermost or uppermost 32-bit section of the 64-bit address
9838 space. Thus, when they use an R_MIPS_64 they mean what is
9839 usually meant by R_MIPS_32, with the exception that the
9840 stored value is sign-extended to 64 bits. */
9841 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9843 /* On big-endian systems, we need to lie about the position
9844 of the reloc. */
9845 if (bfd_big_endian (input_bfd))
9846 rel->r_offset += 4;
9849 if (!use_saved_addend_p)
9851 /* If these relocations were originally of the REL variety,
9852 we must pull the addend out of the field that will be
9853 relocated. Otherwise, we simply use the contents of the
9854 RELA relocation. */
9855 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9856 relocs, rel))
9858 rela_relocation_p = FALSE;
9859 addend = mips_elf_read_rel_addend (input_bfd, rel,
9860 howto, contents);
9861 if (hi16_reloc_p (r_type)
9862 || (got16_reloc_p (r_type)
9863 && mips_elf_local_relocation_p (input_bfd, rel,
9864 local_sections)))
9866 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9867 contents, &addend))
9869 if (h)
9870 name = h->root.root.string;
9871 else
9872 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9873 local_syms + r_symndx,
9874 sec);
9875 (*_bfd_error_handler)
9876 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9877 input_bfd, input_section, name, howto->name,
9878 rel->r_offset);
9881 else
9882 addend <<= howto->rightshift;
9884 else
9885 addend = rel->r_addend;
9886 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9887 local_syms, local_sections, rel);
9890 if (info->relocatable)
9892 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9893 && bfd_big_endian (input_bfd))
9894 rel->r_offset -= 4;
9896 if (!rela_relocation_p && rel->r_addend)
9898 addend += rel->r_addend;
9899 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9900 addend = mips_elf_high (addend);
9901 else if (r_type == R_MIPS_HIGHER)
9902 addend = mips_elf_higher (addend);
9903 else if (r_type == R_MIPS_HIGHEST)
9904 addend = mips_elf_highest (addend);
9905 else
9906 addend >>= howto->rightshift;
9908 /* We use the source mask, rather than the destination
9909 mask because the place to which we are writing will be
9910 source of the addend in the final link. */
9911 addend &= howto->src_mask;
9913 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9914 /* See the comment above about using R_MIPS_64 in the 32-bit
9915 ABI. Here, we need to update the addend. It would be
9916 possible to get away with just using the R_MIPS_32 reloc
9917 but for endianness. */
9919 bfd_vma sign_bits;
9920 bfd_vma low_bits;
9921 bfd_vma high_bits;
9923 if (addend & ((bfd_vma) 1 << 31))
9924 #ifdef BFD64
9925 sign_bits = ((bfd_vma) 1 << 32) - 1;
9926 #else
9927 sign_bits = -1;
9928 #endif
9929 else
9930 sign_bits = 0;
9932 /* If we don't know that we have a 64-bit type,
9933 do two separate stores. */
9934 if (bfd_big_endian (input_bfd))
9936 /* Store the sign-bits (which are most significant)
9937 first. */
9938 low_bits = sign_bits;
9939 high_bits = addend;
9941 else
9943 low_bits = addend;
9944 high_bits = sign_bits;
9946 bfd_put_32 (input_bfd, low_bits,
9947 contents + rel->r_offset);
9948 bfd_put_32 (input_bfd, high_bits,
9949 contents + rel->r_offset + 4);
9950 continue;
9953 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9954 input_bfd, input_section,
9955 contents, FALSE))
9956 return FALSE;
9959 /* Go on to the next relocation. */
9960 continue;
9963 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9964 relocations for the same offset. In that case we are
9965 supposed to treat the output of each relocation as the addend
9966 for the next. */
9967 if (rel + 1 < relend
9968 && rel->r_offset == rel[1].r_offset
9969 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9970 use_saved_addend_p = TRUE;
9971 else
9972 use_saved_addend_p = FALSE;
9974 /* Figure out what value we are supposed to relocate. */
9975 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9976 input_section, info, rel,
9977 addend, howto, local_syms,
9978 local_sections, &value,
9979 &name, &cross_mode_jump_p,
9980 use_saved_addend_p))
9982 case bfd_reloc_continue:
9983 /* There's nothing to do. */
9984 continue;
9986 case bfd_reloc_undefined:
9987 /* mips_elf_calculate_relocation already called the
9988 undefined_symbol callback. There's no real point in
9989 trying to perform the relocation at this point, so we
9990 just skip ahead to the next relocation. */
9991 continue;
9993 case bfd_reloc_notsupported:
9994 msg = _("internal error: unsupported relocation error");
9995 info->callbacks->warning
9996 (info, msg, name, input_bfd, input_section, rel->r_offset);
9997 return FALSE;
9999 case bfd_reloc_overflow:
10000 if (use_saved_addend_p)
10001 /* Ignore overflow until we reach the last relocation for
10002 a given location. */
10004 else
10006 struct mips_elf_link_hash_table *htab;
10008 htab = mips_elf_hash_table (info);
10009 BFD_ASSERT (htab != NULL);
10010 BFD_ASSERT (name != NULL);
10011 if (!htab->small_data_overflow_reported
10012 && (gprel16_reloc_p (howto->type)
10013 || literal_reloc_p (howto->type)))
10015 msg = _("small-data section exceeds 64KB;"
10016 " lower small-data size limit (see option -G)");
10018 htab->small_data_overflow_reported = TRUE;
10019 (*info->callbacks->einfo) ("%P: %s\n", msg);
10021 if (! ((*info->callbacks->reloc_overflow)
10022 (info, NULL, name, howto->name, (bfd_vma) 0,
10023 input_bfd, input_section, rel->r_offset)))
10024 return FALSE;
10026 break;
10028 case bfd_reloc_ok:
10029 break;
10031 case bfd_reloc_outofrange:
10032 if (jal_reloc_p (howto->type))
10034 msg = _("JALX to a non-word-aligned address");
10035 info->callbacks->warning
10036 (info, msg, name, input_bfd, input_section, rel->r_offset);
10037 return FALSE;
10039 /* Fall through. */
10041 default:
10042 abort ();
10043 break;
10046 /* If we've got another relocation for the address, keep going
10047 until we reach the last one. */
10048 if (use_saved_addend_p)
10050 addend = value;
10051 continue;
10054 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10055 /* See the comment above about using R_MIPS_64 in the 32-bit
10056 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10057 that calculated the right value. Now, however, we
10058 sign-extend the 32-bit result to 64-bits, and store it as a
10059 64-bit value. We are especially generous here in that we
10060 go to extreme lengths to support this usage on systems with
10061 only a 32-bit VMA. */
10063 bfd_vma sign_bits;
10064 bfd_vma low_bits;
10065 bfd_vma high_bits;
10067 if (value & ((bfd_vma) 1 << 31))
10068 #ifdef BFD64
10069 sign_bits = ((bfd_vma) 1 << 32) - 1;
10070 #else
10071 sign_bits = -1;
10072 #endif
10073 else
10074 sign_bits = 0;
10076 /* If we don't know that we have a 64-bit type,
10077 do two separate stores. */
10078 if (bfd_big_endian (input_bfd))
10080 /* Undo what we did above. */
10081 rel->r_offset -= 4;
10082 /* Store the sign-bits (which are most significant)
10083 first. */
10084 low_bits = sign_bits;
10085 high_bits = value;
10087 else
10089 low_bits = value;
10090 high_bits = sign_bits;
10092 bfd_put_32 (input_bfd, low_bits,
10093 contents + rel->r_offset);
10094 bfd_put_32 (input_bfd, high_bits,
10095 contents + rel->r_offset + 4);
10096 continue;
10099 /* Actually perform the relocation. */
10100 if (! mips_elf_perform_relocation (info, howto, rel, value,
10101 input_bfd, input_section,
10102 contents, cross_mode_jump_p))
10103 return FALSE;
10106 return TRUE;
10109 /* A function that iterates over each entry in la25_stubs and fills
10110 in the code for each one. DATA points to a mips_htab_traverse_info. */
10112 static int
10113 mips_elf_create_la25_stub (void **slot, void *data)
10115 struct mips_htab_traverse_info *hti;
10116 struct mips_elf_link_hash_table *htab;
10117 struct mips_elf_la25_stub *stub;
10118 asection *s;
10119 bfd_byte *loc;
10120 bfd_vma offset, target, target_high, target_low;
10122 stub = (struct mips_elf_la25_stub *) *slot;
10123 hti = (struct mips_htab_traverse_info *) data;
10124 htab = mips_elf_hash_table (hti->info);
10125 BFD_ASSERT (htab != NULL);
10127 /* Create the section contents, if we haven't already. */
10128 s = stub->stub_section;
10129 loc = s->contents;
10130 if (loc == NULL)
10132 loc = bfd_malloc (s->size);
10133 if (loc == NULL)
10135 hti->error = TRUE;
10136 return FALSE;
10138 s->contents = loc;
10141 /* Work out where in the section this stub should go. */
10142 offset = stub->offset;
10144 /* Work out the target address. */
10145 target = mips_elf_get_la25_target (stub, &s);
10146 target += s->output_section->vma + s->output_offset;
10148 target_high = ((target + 0x8000) >> 16) & 0xffff;
10149 target_low = (target & 0xffff);
10151 if (stub->stub_section != htab->strampoline)
10153 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10154 of the section and write the two instructions at the end. */
10155 memset (loc, 0, offset);
10156 loc += offset;
10157 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10159 bfd_put_micromips_32 (hti->output_bfd,
10160 LA25_LUI_MICROMIPS (target_high),
10161 loc);
10162 bfd_put_micromips_32 (hti->output_bfd,
10163 LA25_ADDIU_MICROMIPS (target_low),
10164 loc + 4);
10166 else
10168 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10169 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10172 else
10174 /* This is trampoline. */
10175 loc += offset;
10176 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10178 bfd_put_micromips_32 (hti->output_bfd,
10179 LA25_LUI_MICROMIPS (target_high), loc);
10180 bfd_put_micromips_32 (hti->output_bfd,
10181 LA25_J_MICROMIPS (target), loc + 4);
10182 bfd_put_micromips_32 (hti->output_bfd,
10183 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10184 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10186 else
10188 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10189 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10190 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10191 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10194 return TRUE;
10197 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10198 adjust it appropriately now. */
10200 static void
10201 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10202 const char *name, Elf_Internal_Sym *sym)
10204 /* The linker script takes care of providing names and values for
10205 these, but we must place them into the right sections. */
10206 static const char* const text_section_symbols[] = {
10207 "_ftext",
10208 "_etext",
10209 "__dso_displacement",
10210 "__elf_header",
10211 "__program_header_table",
10212 NULL
10215 static const char* const data_section_symbols[] = {
10216 "_fdata",
10217 "_edata",
10218 "_end",
10219 "_fbss",
10220 NULL
10223 const char* const *p;
10224 int i;
10226 for (i = 0; i < 2; ++i)
10227 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10229 ++p)
10230 if (strcmp (*p, name) == 0)
10232 /* All of these symbols are given type STT_SECTION by the
10233 IRIX6 linker. */
10234 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10235 sym->st_other = STO_PROTECTED;
10237 /* The IRIX linker puts these symbols in special sections. */
10238 if (i == 0)
10239 sym->st_shndx = SHN_MIPS_TEXT;
10240 else
10241 sym->st_shndx = SHN_MIPS_DATA;
10243 break;
10247 /* Finish up dynamic symbol handling. We set the contents of various
10248 dynamic sections here. */
10250 bfd_boolean
10251 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10252 struct bfd_link_info *info,
10253 struct elf_link_hash_entry *h,
10254 Elf_Internal_Sym *sym)
10256 bfd *dynobj;
10257 asection *sgot;
10258 struct mips_got_info *g, *gg;
10259 const char *name;
10260 int idx;
10261 struct mips_elf_link_hash_table *htab;
10262 struct mips_elf_link_hash_entry *hmips;
10264 htab = mips_elf_hash_table (info);
10265 BFD_ASSERT (htab != NULL);
10266 dynobj = elf_hash_table (info)->dynobj;
10267 hmips = (struct mips_elf_link_hash_entry *) h;
10269 BFD_ASSERT (!htab->is_vxworks);
10271 if (h->plt.plist != NULL
10272 && (h->plt.plist->mips_offset != MINUS_ONE
10273 || h->plt.plist->comp_offset != MINUS_ONE))
10275 /* We've decided to create a PLT entry for this symbol. */
10276 bfd_byte *loc;
10277 bfd_vma header_address, got_address;
10278 bfd_vma got_address_high, got_address_low, load;
10279 bfd_vma got_index;
10280 bfd_vma isa_bit;
10282 got_index = h->plt.plist->gotplt_index;
10284 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10285 BFD_ASSERT (h->dynindx != -1);
10286 BFD_ASSERT (htab->splt != NULL);
10287 BFD_ASSERT (got_index != MINUS_ONE);
10288 BFD_ASSERT (!h->def_regular);
10290 /* Calculate the address of the PLT header. */
10291 isa_bit = htab->plt_header_is_comp;
10292 header_address = (htab->splt->output_section->vma
10293 + htab->splt->output_offset + isa_bit);
10295 /* Calculate the address of the .got.plt entry. */
10296 got_address = (htab->sgotplt->output_section->vma
10297 + htab->sgotplt->output_offset
10298 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10300 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10301 got_address_low = got_address & 0xffff;
10303 /* Initially point the .got.plt entry at the PLT header. */
10304 loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10305 if (ABI_64_P (output_bfd))
10306 bfd_put_64 (output_bfd, header_address, loc);
10307 else
10308 bfd_put_32 (output_bfd, header_address, loc);
10310 /* Now handle the PLT itself. First the standard entry (the order
10311 does not matter, we just have to pick one). */
10312 if (h->plt.plist->mips_offset != MINUS_ONE)
10314 const bfd_vma *plt_entry;
10315 bfd_vma plt_offset;
10317 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10319 BFD_ASSERT (plt_offset <= htab->splt->size);
10321 /* Find out where the .plt entry should go. */
10322 loc = htab->splt->contents + plt_offset;
10324 /* Pick the load opcode. */
10325 load = MIPS_ELF_LOAD_WORD (output_bfd);
10327 /* Fill in the PLT entry itself. */
10328 plt_entry = mips_exec_plt_entry;
10329 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10330 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10331 loc + 4);
10333 if (! LOAD_INTERLOCKS_P (output_bfd))
10335 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10336 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10338 else
10340 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10341 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10342 loc + 12);
10346 /* Now the compressed entry. They come after any standard ones. */
10347 if (h->plt.plist->comp_offset != MINUS_ONE)
10349 bfd_vma plt_offset;
10351 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10352 + h->plt.plist->comp_offset);
10354 BFD_ASSERT (plt_offset <= htab->splt->size);
10356 /* Find out where the .plt entry should go. */
10357 loc = htab->splt->contents + plt_offset;
10359 /* Fill in the PLT entry itself. */
10360 if (!MICROMIPS_P (output_bfd))
10362 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10364 bfd_put_16 (output_bfd, plt_entry[0], loc);
10365 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10366 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10367 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10368 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10369 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10370 bfd_put_32 (output_bfd, got_address, loc + 12);
10372 else if (htab->insn32)
10374 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10376 bfd_put_16 (output_bfd, plt_entry[0], loc);
10377 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10378 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10379 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10380 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10381 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10382 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10383 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10385 else
10387 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10388 bfd_signed_vma gotpc_offset;
10389 bfd_vma loc_address;
10391 BFD_ASSERT (got_address % 4 == 0);
10393 loc_address = (htab->splt->output_section->vma
10394 + htab->splt->output_offset + plt_offset);
10395 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10397 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10398 if (gotpc_offset + 0x1000000 >= 0x2000000)
10400 (*_bfd_error_handler)
10401 (_("%B: `%A' offset of %ld from `%A' "
10402 "beyond the range of ADDIUPC"),
10403 output_bfd,
10404 htab->sgotplt->output_section,
10405 htab->splt->output_section,
10406 (long) gotpc_offset);
10407 bfd_set_error (bfd_error_no_error);
10408 return FALSE;
10410 bfd_put_16 (output_bfd,
10411 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10412 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10413 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10414 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10415 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10416 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10420 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10421 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
10422 got_index - 2, h->dynindx,
10423 R_MIPS_JUMP_SLOT, got_address);
10425 /* We distinguish between PLT entries and lazy-binding stubs by
10426 giving the former an st_other value of STO_MIPS_PLT. Set the
10427 flag and leave the value if there are any relocations in the
10428 binary where pointer equality matters. */
10429 sym->st_shndx = SHN_UNDEF;
10430 if (h->pointer_equality_needed)
10431 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
10432 else
10434 sym->st_value = 0;
10435 sym->st_other = 0;
10439 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
10441 /* We've decided to create a lazy-binding stub. */
10442 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10443 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10444 bfd_vma stub_size = htab->function_stub_size;
10445 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
10446 bfd_vma isa_bit = micromips_p;
10447 bfd_vma stub_big_size;
10449 if (!micromips_p)
10450 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
10451 else if (htab->insn32)
10452 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10453 else
10454 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
10456 /* This symbol has a stub. Set it up. */
10458 BFD_ASSERT (h->dynindx != -1);
10460 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
10462 /* Values up to 2^31 - 1 are allowed. Larger values would cause
10463 sign extension at runtime in the stub, resulting in a negative
10464 index value. */
10465 if (h->dynindx & ~0x7fffffff)
10466 return FALSE;
10468 /* Fill the stub. */
10469 if (micromips_p)
10471 idx = 0;
10472 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
10473 stub + idx);
10474 idx += 4;
10475 if (htab->insn32)
10477 bfd_put_micromips_32 (output_bfd,
10478 STUB_MOVE32_MICROMIPS (output_bfd),
10479 stub + idx);
10480 idx += 4;
10482 else
10484 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
10485 idx += 2;
10487 if (stub_size == stub_big_size)
10489 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
10491 bfd_put_micromips_32 (output_bfd,
10492 STUB_LUI_MICROMIPS (dynindx_hi),
10493 stub + idx);
10494 idx += 4;
10496 if (htab->insn32)
10498 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
10499 stub + idx);
10500 idx += 4;
10502 else
10504 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
10505 idx += 2;
10508 /* If a large stub is not required and sign extension is not a
10509 problem, then use legacy code in the stub. */
10510 if (stub_size == stub_big_size)
10511 bfd_put_micromips_32 (output_bfd,
10512 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
10513 stub + idx);
10514 else if (h->dynindx & ~0x7fff)
10515 bfd_put_micromips_32 (output_bfd,
10516 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
10517 stub + idx);
10518 else
10519 bfd_put_micromips_32 (output_bfd,
10520 STUB_LI16S_MICROMIPS (output_bfd,
10521 h->dynindx),
10522 stub + idx);
10524 else
10526 idx = 0;
10527 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
10528 idx += 4;
10529 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
10530 idx += 4;
10531 if (stub_size == stub_big_size)
10533 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
10534 stub + idx);
10535 idx += 4;
10537 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
10538 idx += 4;
10540 /* If a large stub is not required and sign extension is not a
10541 problem, then use legacy code in the stub. */
10542 if (stub_size == stub_big_size)
10543 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
10544 stub + idx);
10545 else if (h->dynindx & ~0x7fff)
10546 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
10547 stub + idx);
10548 else
10549 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
10550 stub + idx);
10553 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
10554 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
10555 stub, stub_size);
10557 /* Mark the symbol as undefined. stub_offset != -1 occurs
10558 only for the referenced symbol. */
10559 sym->st_shndx = SHN_UNDEF;
10561 /* The run-time linker uses the st_value field of the symbol
10562 to reset the global offset table entry for this external
10563 to its stub address when unlinking a shared object. */
10564 sym->st_value = (htab->sstubs->output_section->vma
10565 + htab->sstubs->output_offset
10566 + h->plt.plist->stub_offset
10567 + isa_bit);
10568 sym->st_other = other;
10571 /* If we have a MIPS16 function with a stub, the dynamic symbol must
10572 refer to the stub, since only the stub uses the standard calling
10573 conventions. */
10574 if (h->dynindx != -1 && hmips->fn_stub != NULL)
10576 BFD_ASSERT (hmips->need_fn_stub);
10577 sym->st_value = (hmips->fn_stub->output_section->vma
10578 + hmips->fn_stub->output_offset);
10579 sym->st_size = hmips->fn_stub->size;
10580 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
10583 BFD_ASSERT (h->dynindx != -1
10584 || h->forced_local);
10586 sgot = htab->sgot;
10587 g = htab->got_info;
10588 BFD_ASSERT (g != NULL);
10590 /* Run through the global symbol table, creating GOT entries for all
10591 the symbols that need them. */
10592 if (hmips->global_got_area != GGA_NONE)
10594 bfd_vma offset;
10595 bfd_vma value;
10597 value = sym->st_value;
10598 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10599 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
10602 if (hmips->global_got_area != GGA_NONE && g->next)
10604 struct mips_got_entry e, *p;
10605 bfd_vma entry;
10606 bfd_vma offset;
10608 gg = g;
10610 e.abfd = output_bfd;
10611 e.symndx = -1;
10612 e.d.h = hmips;
10613 e.tls_type = GOT_TLS_NONE;
10615 for (g = g->next; g->next != gg; g = g->next)
10617 if (g->got_entries
10618 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10619 &e)))
10621 offset = p->gotidx;
10622 BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
10623 if (info->shared
10624 || (elf_hash_table (info)->dynamic_sections_created
10625 && p->d.h != NULL
10626 && p->d.h->root.def_dynamic
10627 && !p->d.h->root.def_regular))
10629 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10630 the various compatibility problems, it's easier to mock
10631 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10632 mips_elf_create_dynamic_relocation to calculate the
10633 appropriate addend. */
10634 Elf_Internal_Rela rel[3];
10636 memset (rel, 0, sizeof (rel));
10637 if (ABI_64_P (output_bfd))
10638 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10639 else
10640 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10641 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10643 entry = 0;
10644 if (! (mips_elf_create_dynamic_relocation
10645 (output_bfd, info, rel,
10646 e.d.h, NULL, sym->st_value, &entry, sgot)))
10647 return FALSE;
10649 else
10650 entry = sym->st_value;
10651 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
10656 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10657 name = h->root.root.string;
10658 if (h == elf_hash_table (info)->hdynamic
10659 || h == elf_hash_table (info)->hgot)
10660 sym->st_shndx = SHN_ABS;
10661 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10662 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10664 sym->st_shndx = SHN_ABS;
10665 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10666 sym->st_value = 1;
10668 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
10670 sym->st_shndx = SHN_ABS;
10671 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10672 sym->st_value = elf_gp (output_bfd);
10674 else if (SGI_COMPAT (output_bfd))
10676 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10677 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10679 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10680 sym->st_other = STO_PROTECTED;
10681 sym->st_value = 0;
10682 sym->st_shndx = SHN_MIPS_DATA;
10684 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10686 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10687 sym->st_other = STO_PROTECTED;
10688 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10689 sym->st_shndx = SHN_ABS;
10691 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10693 if (h->type == STT_FUNC)
10694 sym->st_shndx = SHN_MIPS_TEXT;
10695 else if (h->type == STT_OBJECT)
10696 sym->st_shndx = SHN_MIPS_DATA;
10700 /* Emit a copy reloc, if needed. */
10701 if (h->needs_copy)
10703 asection *s;
10704 bfd_vma symval;
10706 BFD_ASSERT (h->dynindx != -1);
10707 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10709 s = mips_elf_rel_dyn_section (info, FALSE);
10710 symval = (h->root.u.def.section->output_section->vma
10711 + h->root.u.def.section->output_offset
10712 + h->root.u.def.value);
10713 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10714 h->dynindx, R_MIPS_COPY, symval);
10717 /* Handle the IRIX6-specific symbols. */
10718 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10719 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10721 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
10722 to treat compressed symbols like any other. */
10723 if (ELF_ST_IS_MIPS16 (sym->st_other))
10725 BFD_ASSERT (sym->st_value & 1);
10726 sym->st_other -= STO_MIPS16;
10728 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
10730 BFD_ASSERT (sym->st_value & 1);
10731 sym->st_other -= STO_MICROMIPS;
10734 return TRUE;
10737 /* Likewise, for VxWorks. */
10739 bfd_boolean
10740 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10741 struct bfd_link_info *info,
10742 struct elf_link_hash_entry *h,
10743 Elf_Internal_Sym *sym)
10745 bfd *dynobj;
10746 asection *sgot;
10747 struct mips_got_info *g;
10748 struct mips_elf_link_hash_table *htab;
10749 struct mips_elf_link_hash_entry *hmips;
10751 htab = mips_elf_hash_table (info);
10752 BFD_ASSERT (htab != NULL);
10753 dynobj = elf_hash_table (info)->dynobj;
10754 hmips = (struct mips_elf_link_hash_entry *) h;
10756 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
10758 bfd_byte *loc;
10759 bfd_vma plt_address, got_address, got_offset, branch_offset;
10760 Elf_Internal_Rela rel;
10761 static const bfd_vma *plt_entry;
10762 bfd_vma gotplt_index;
10763 bfd_vma plt_offset;
10765 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10766 gotplt_index = h->plt.plist->gotplt_index;
10768 BFD_ASSERT (h->dynindx != -1);
10769 BFD_ASSERT (htab->splt != NULL);
10770 BFD_ASSERT (gotplt_index != MINUS_ONE);
10771 BFD_ASSERT (plt_offset <= htab->splt->size);
10773 /* Calculate the address of the .plt entry. */
10774 plt_address = (htab->splt->output_section->vma
10775 + htab->splt->output_offset
10776 + plt_offset);
10778 /* Calculate the address of the .got.plt entry. */
10779 got_address = (htab->sgotplt->output_section->vma
10780 + htab->sgotplt->output_offset
10781 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
10783 /* Calculate the offset of the .got.plt entry from
10784 _GLOBAL_OFFSET_TABLE_. */
10785 got_offset = mips_elf_gotplt_index (info, h);
10787 /* Calculate the offset for the branch at the start of the PLT
10788 entry. The branch jumps to the beginning of .plt. */
10789 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
10791 /* Fill in the initial value of the .got.plt entry. */
10792 bfd_put_32 (output_bfd, plt_address,
10793 (htab->sgotplt->contents
10794 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
10796 /* Find out where the .plt entry should go. */
10797 loc = htab->splt->contents + plt_offset;
10799 if (info->shared)
10801 plt_entry = mips_vxworks_shared_plt_entry;
10802 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10803 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
10805 else
10807 bfd_vma got_address_high, got_address_low;
10809 plt_entry = mips_vxworks_exec_plt_entry;
10810 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10811 got_address_low = got_address & 0xffff;
10813 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10814 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
10815 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10816 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10817 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10818 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10819 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10820 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10822 loc = (htab->srelplt2->contents
10823 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
10825 /* Emit a relocation for the .got.plt entry. */
10826 rel.r_offset = got_address;
10827 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10828 rel.r_addend = plt_offset;
10829 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10831 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10832 loc += sizeof (Elf32_External_Rela);
10833 rel.r_offset = plt_address + 8;
10834 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10835 rel.r_addend = got_offset;
10836 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10838 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10839 loc += sizeof (Elf32_External_Rela);
10840 rel.r_offset += 4;
10841 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10842 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10845 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10846 loc = (htab->srelplt->contents
10847 + gotplt_index * sizeof (Elf32_External_Rela));
10848 rel.r_offset = got_address;
10849 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10850 rel.r_addend = 0;
10851 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10853 if (!h->def_regular)
10854 sym->st_shndx = SHN_UNDEF;
10857 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10859 sgot = htab->sgot;
10860 g = htab->got_info;
10861 BFD_ASSERT (g != NULL);
10863 /* See if this symbol has an entry in the GOT. */
10864 if (hmips->global_got_area != GGA_NONE)
10866 bfd_vma offset;
10867 Elf_Internal_Rela outrel;
10868 bfd_byte *loc;
10869 asection *s;
10871 /* Install the symbol value in the GOT. */
10872 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10873 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10875 /* Add a dynamic relocation for it. */
10876 s = mips_elf_rel_dyn_section (info, FALSE);
10877 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10878 outrel.r_offset = (sgot->output_section->vma
10879 + sgot->output_offset
10880 + offset);
10881 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10882 outrel.r_addend = 0;
10883 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10886 /* Emit a copy reloc, if needed. */
10887 if (h->needs_copy)
10889 Elf_Internal_Rela rel;
10891 BFD_ASSERT (h->dynindx != -1);
10893 rel.r_offset = (h->root.u.def.section->output_section->vma
10894 + h->root.u.def.section->output_offset
10895 + h->root.u.def.value);
10896 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10897 rel.r_addend = 0;
10898 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10899 htab->srelbss->contents
10900 + (htab->srelbss->reloc_count
10901 * sizeof (Elf32_External_Rela)));
10902 ++htab->srelbss->reloc_count;
10905 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10906 if (ELF_ST_IS_COMPRESSED (sym->st_other))
10907 sym->st_value &= ~1;
10909 return TRUE;
10912 /* Write out a plt0 entry to the beginning of .plt. */
10914 static bfd_boolean
10915 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10917 bfd_byte *loc;
10918 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10919 static const bfd_vma *plt_entry;
10920 struct mips_elf_link_hash_table *htab;
10922 htab = mips_elf_hash_table (info);
10923 BFD_ASSERT (htab != NULL);
10925 if (ABI_64_P (output_bfd))
10926 plt_entry = mips_n64_exec_plt0_entry;
10927 else if (ABI_N32_P (output_bfd))
10928 plt_entry = mips_n32_exec_plt0_entry;
10929 else if (!htab->plt_header_is_comp)
10930 plt_entry = mips_o32_exec_plt0_entry;
10931 else if (htab->insn32)
10932 plt_entry = micromips_insn32_o32_exec_plt0_entry;
10933 else
10934 plt_entry = micromips_o32_exec_plt0_entry;
10936 /* Calculate the value of .got.plt. */
10937 gotplt_value = (htab->sgotplt->output_section->vma
10938 + htab->sgotplt->output_offset);
10939 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10940 gotplt_value_low = gotplt_value & 0xffff;
10942 /* The PLT sequence is not safe for N64 if .got.plt's address can
10943 not be loaded in two instructions. */
10944 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10945 || ~(gotplt_value | 0x7fffffff) == 0);
10947 /* Install the PLT header. */
10948 loc = htab->splt->contents;
10949 if (plt_entry == micromips_o32_exec_plt0_entry)
10951 bfd_vma gotpc_offset;
10952 bfd_vma loc_address;
10953 size_t i;
10955 BFD_ASSERT (gotplt_value % 4 == 0);
10957 loc_address = (htab->splt->output_section->vma
10958 + htab->splt->output_offset);
10959 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
10961 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10962 if (gotpc_offset + 0x1000000 >= 0x2000000)
10964 (*_bfd_error_handler)
10965 (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
10966 output_bfd,
10967 htab->sgotplt->output_section,
10968 htab->splt->output_section,
10969 (long) gotpc_offset);
10970 bfd_set_error (bfd_error_no_error);
10971 return FALSE;
10973 bfd_put_16 (output_bfd,
10974 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10975 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10976 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
10977 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
10979 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
10981 size_t i;
10983 bfd_put_16 (output_bfd, plt_entry[0], loc);
10984 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
10985 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10986 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
10987 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10988 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
10989 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
10990 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
10992 else
10994 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
10995 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
10996 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
10997 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10998 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10999 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11000 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11001 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11004 return TRUE;
11007 /* Install the PLT header for a VxWorks executable and finalize the
11008 contents of .rela.plt.unloaded. */
11010 static void
11011 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11013 Elf_Internal_Rela rela;
11014 bfd_byte *loc;
11015 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11016 static const bfd_vma *plt_entry;
11017 struct mips_elf_link_hash_table *htab;
11019 htab = mips_elf_hash_table (info);
11020 BFD_ASSERT (htab != NULL);
11022 plt_entry = mips_vxworks_exec_plt0_entry;
11024 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11025 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11026 + htab->root.hgot->root.u.def.section->output_offset
11027 + htab->root.hgot->root.u.def.value);
11029 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11030 got_value_low = got_value & 0xffff;
11032 /* Calculate the address of the PLT header. */
11033 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
11035 /* Install the PLT header. */
11036 loc = htab->splt->contents;
11037 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11038 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11039 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11040 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11041 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11042 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11044 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11045 loc = htab->srelplt2->contents;
11046 rela.r_offset = plt_address;
11047 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11048 rela.r_addend = 0;
11049 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11050 loc += sizeof (Elf32_External_Rela);
11052 /* Output the relocation for the following addiu of
11053 %lo(_GLOBAL_OFFSET_TABLE_). */
11054 rela.r_offset += 4;
11055 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11056 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11057 loc += sizeof (Elf32_External_Rela);
11059 /* Fix up the remaining relocations. They may have the wrong
11060 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11061 in which symbols were output. */
11062 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11064 Elf_Internal_Rela rel;
11066 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11067 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11068 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11069 loc += sizeof (Elf32_External_Rela);
11071 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11072 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11073 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11074 loc += sizeof (Elf32_External_Rela);
11076 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11077 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11078 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11079 loc += sizeof (Elf32_External_Rela);
11083 /* Install the PLT header for a VxWorks shared library. */
11085 static void
11086 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11088 unsigned int i;
11089 struct mips_elf_link_hash_table *htab;
11091 htab = mips_elf_hash_table (info);
11092 BFD_ASSERT (htab != NULL);
11094 /* We just need to copy the entry byte-by-byte. */
11095 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11096 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11097 htab->splt->contents + i * 4);
11100 /* Finish up the dynamic sections. */
11102 bfd_boolean
11103 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11104 struct bfd_link_info *info)
11106 bfd *dynobj;
11107 asection *sdyn;
11108 asection *sgot;
11109 struct mips_got_info *gg, *g;
11110 struct mips_elf_link_hash_table *htab;
11112 htab = mips_elf_hash_table (info);
11113 BFD_ASSERT (htab != NULL);
11115 dynobj = elf_hash_table (info)->dynobj;
11117 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11119 sgot = htab->sgot;
11120 gg = htab->got_info;
11122 if (elf_hash_table (info)->dynamic_sections_created)
11124 bfd_byte *b;
11125 int dyn_to_skip = 0, dyn_skipped = 0;
11127 BFD_ASSERT (sdyn != NULL);
11128 BFD_ASSERT (gg != NULL);
11130 g = mips_elf_bfd_got (output_bfd, FALSE);
11131 BFD_ASSERT (g != NULL);
11133 for (b = sdyn->contents;
11134 b < sdyn->contents + sdyn->size;
11135 b += MIPS_ELF_DYN_SIZE (dynobj))
11137 Elf_Internal_Dyn dyn;
11138 const char *name;
11139 size_t elemsize;
11140 asection *s;
11141 bfd_boolean swap_out_p;
11143 /* Read in the current dynamic entry. */
11144 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11146 /* Assume that we're going to modify it and write it out. */
11147 swap_out_p = TRUE;
11149 switch (dyn.d_tag)
11151 case DT_RELENT:
11152 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11153 break;
11155 case DT_RELAENT:
11156 BFD_ASSERT (htab->is_vxworks);
11157 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11158 break;
11160 case DT_STRSZ:
11161 /* Rewrite DT_STRSZ. */
11162 dyn.d_un.d_val =
11163 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11164 break;
11166 case DT_PLTGOT:
11167 s = htab->sgot;
11168 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11169 break;
11171 case DT_MIPS_PLTGOT:
11172 s = htab->sgotplt;
11173 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11174 break;
11176 case DT_MIPS_RLD_VERSION:
11177 dyn.d_un.d_val = 1; /* XXX */
11178 break;
11180 case DT_MIPS_FLAGS:
11181 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11182 break;
11184 case DT_MIPS_TIME_STAMP:
11186 time_t t;
11187 time (&t);
11188 dyn.d_un.d_val = t;
11190 break;
11192 case DT_MIPS_ICHECKSUM:
11193 /* XXX FIXME: */
11194 swap_out_p = FALSE;
11195 break;
11197 case DT_MIPS_IVERSION:
11198 /* XXX FIXME: */
11199 swap_out_p = FALSE;
11200 break;
11202 case DT_MIPS_BASE_ADDRESS:
11203 s = output_bfd->sections;
11204 BFD_ASSERT (s != NULL);
11205 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11206 break;
11208 case DT_MIPS_LOCAL_GOTNO:
11209 dyn.d_un.d_val = g->local_gotno;
11210 break;
11212 case DT_MIPS_UNREFEXTNO:
11213 /* The index into the dynamic symbol table which is the
11214 entry of the first external symbol that is not
11215 referenced within the same object. */
11216 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11217 break;
11219 case DT_MIPS_GOTSYM:
11220 if (htab->global_gotsym)
11222 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11223 break;
11225 /* In case if we don't have global got symbols we default
11226 to setting DT_MIPS_GOTSYM to the same value as
11227 DT_MIPS_SYMTABNO, so we just fall through. */
11229 case DT_MIPS_SYMTABNO:
11230 name = ".dynsym";
11231 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11232 s = bfd_get_section_by_name (output_bfd, name);
11233 BFD_ASSERT (s != NULL);
11235 dyn.d_un.d_val = s->size / elemsize;
11236 break;
11238 case DT_MIPS_HIPAGENO:
11239 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11240 break;
11242 case DT_MIPS_RLD_MAP:
11244 struct elf_link_hash_entry *h;
11245 h = mips_elf_hash_table (info)->rld_symbol;
11246 if (!h)
11248 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11249 swap_out_p = FALSE;
11250 break;
11252 s = h->root.u.def.section;
11253 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11254 + h->root.u.def.value);
11256 break;
11258 case DT_MIPS_OPTIONS:
11259 s = (bfd_get_section_by_name
11260 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11261 dyn.d_un.d_ptr = s->vma;
11262 break;
11264 case DT_RELASZ:
11265 BFD_ASSERT (htab->is_vxworks);
11266 /* The count does not include the JUMP_SLOT relocations. */
11267 if (htab->srelplt)
11268 dyn.d_un.d_val -= htab->srelplt->size;
11269 break;
11271 case DT_PLTREL:
11272 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11273 if (htab->is_vxworks)
11274 dyn.d_un.d_val = DT_RELA;
11275 else
11276 dyn.d_un.d_val = DT_REL;
11277 break;
11279 case DT_PLTRELSZ:
11280 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11281 dyn.d_un.d_val = htab->srelplt->size;
11282 break;
11284 case DT_JMPREL:
11285 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11286 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
11287 + htab->srelplt->output_offset);
11288 break;
11290 case DT_TEXTREL:
11291 /* If we didn't need any text relocations after all, delete
11292 the dynamic tag. */
11293 if (!(info->flags & DF_TEXTREL))
11295 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11296 swap_out_p = FALSE;
11298 break;
11300 case DT_FLAGS:
11301 /* If we didn't need any text relocations after all, clear
11302 DF_TEXTREL from DT_FLAGS. */
11303 if (!(info->flags & DF_TEXTREL))
11304 dyn.d_un.d_val &= ~DF_TEXTREL;
11305 else
11306 swap_out_p = FALSE;
11307 break;
11309 default:
11310 swap_out_p = FALSE;
11311 if (htab->is_vxworks
11312 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11313 swap_out_p = TRUE;
11314 break;
11317 if (swap_out_p || dyn_skipped)
11318 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11319 (dynobj, &dyn, b - dyn_skipped);
11321 if (dyn_to_skip)
11323 dyn_skipped += dyn_to_skip;
11324 dyn_to_skip = 0;
11328 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11329 if (dyn_skipped > 0)
11330 memset (b - dyn_skipped, 0, dyn_skipped);
11333 if (sgot != NULL && sgot->size > 0
11334 && !bfd_is_abs_section (sgot->output_section))
11336 if (htab->is_vxworks)
11338 /* The first entry of the global offset table points to the
11339 ".dynamic" section. The second is initialized by the
11340 loader and contains the shared library identifier.
11341 The third is also initialized by the loader and points
11342 to the lazy resolution stub. */
11343 MIPS_ELF_PUT_WORD (output_bfd,
11344 sdyn->output_offset + sdyn->output_section->vma,
11345 sgot->contents);
11346 MIPS_ELF_PUT_WORD (output_bfd, 0,
11347 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11348 MIPS_ELF_PUT_WORD (output_bfd, 0,
11349 sgot->contents
11350 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11352 else
11354 /* The first entry of the global offset table will be filled at
11355 runtime. The second entry will be used by some runtime loaders.
11356 This isn't the case of IRIX rld. */
11357 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
11358 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11359 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11362 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11363 = MIPS_ELF_GOT_SIZE (output_bfd);
11366 /* Generate dynamic relocations for the non-primary gots. */
11367 if (gg != NULL && gg->next)
11369 Elf_Internal_Rela rel[3];
11370 bfd_vma addend = 0;
11372 memset (rel, 0, sizeof (rel));
11373 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11375 for (g = gg->next; g->next != gg; g = g->next)
11377 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
11378 + g->next->tls_gotno;
11380 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
11381 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11382 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11383 sgot->contents
11384 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11386 if (! info->shared)
11387 continue;
11389 while (got_index < g->assigned_gotno)
11391 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
11392 = got_index++ * MIPS_ELF_GOT_SIZE (output_bfd);
11393 if (!(mips_elf_create_dynamic_relocation
11394 (output_bfd, info, rel, NULL,
11395 bfd_abs_section_ptr,
11396 0, &addend, sgot)))
11397 return FALSE;
11398 BFD_ASSERT (addend == 0);
11403 /* The generation of dynamic relocations for the non-primary gots
11404 adds more dynamic relocations. We cannot count them until
11405 here. */
11407 if (elf_hash_table (info)->dynamic_sections_created)
11409 bfd_byte *b;
11410 bfd_boolean swap_out_p;
11412 BFD_ASSERT (sdyn != NULL);
11414 for (b = sdyn->contents;
11415 b < sdyn->contents + sdyn->size;
11416 b += MIPS_ELF_DYN_SIZE (dynobj))
11418 Elf_Internal_Dyn dyn;
11419 asection *s;
11421 /* Read in the current dynamic entry. */
11422 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11424 /* Assume that we're going to modify it and write it out. */
11425 swap_out_p = TRUE;
11427 switch (dyn.d_tag)
11429 case DT_RELSZ:
11430 /* Reduce DT_RELSZ to account for any relocations we
11431 decided not to make. This is for the n64 irix rld,
11432 which doesn't seem to apply any relocations if there
11433 are trailing null entries. */
11434 s = mips_elf_rel_dyn_section (info, FALSE);
11435 dyn.d_un.d_val = (s->reloc_count
11436 * (ABI_64_P (output_bfd)
11437 ? sizeof (Elf64_Mips_External_Rel)
11438 : sizeof (Elf32_External_Rel)));
11439 /* Adjust the section size too. Tools like the prelinker
11440 can reasonably expect the values to the same. */
11441 elf_section_data (s->output_section)->this_hdr.sh_size
11442 = dyn.d_un.d_val;
11443 break;
11445 default:
11446 swap_out_p = FALSE;
11447 break;
11450 if (swap_out_p)
11451 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11452 (dynobj, &dyn, b);
11457 asection *s;
11458 Elf32_compact_rel cpt;
11460 if (SGI_COMPAT (output_bfd))
11462 /* Write .compact_rel section out. */
11463 s = bfd_get_linker_section (dynobj, ".compact_rel");
11464 if (s != NULL)
11466 cpt.id1 = 1;
11467 cpt.num = s->reloc_count;
11468 cpt.id2 = 2;
11469 cpt.offset = (s->output_section->filepos
11470 + sizeof (Elf32_External_compact_rel));
11471 cpt.reserved0 = 0;
11472 cpt.reserved1 = 0;
11473 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
11474 ((Elf32_External_compact_rel *)
11475 s->contents));
11477 /* Clean up a dummy stub function entry in .text. */
11478 if (htab->sstubs != NULL)
11480 file_ptr dummy_offset;
11482 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
11483 dummy_offset = htab->sstubs->size - htab->function_stub_size;
11484 memset (htab->sstubs->contents + dummy_offset, 0,
11485 htab->function_stub_size);
11490 /* The psABI says that the dynamic relocations must be sorted in
11491 increasing order of r_symndx. The VxWorks EABI doesn't require
11492 this, and because the code below handles REL rather than RELA
11493 relocations, using it for VxWorks would be outright harmful. */
11494 if (!htab->is_vxworks)
11496 s = mips_elf_rel_dyn_section (info, FALSE);
11497 if (s != NULL
11498 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
11500 reldyn_sorting_bfd = output_bfd;
11502 if (ABI_64_P (output_bfd))
11503 qsort ((Elf64_External_Rel *) s->contents + 1,
11504 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
11505 sort_dynamic_relocs_64);
11506 else
11507 qsort ((Elf32_External_Rel *) s->contents + 1,
11508 s->reloc_count - 1, sizeof (Elf32_External_Rel),
11509 sort_dynamic_relocs);
11514 if (htab->splt && htab->splt->size > 0)
11516 if (htab->is_vxworks)
11518 if (info->shared)
11519 mips_vxworks_finish_shared_plt (output_bfd, info);
11520 else
11521 mips_vxworks_finish_exec_plt (output_bfd, info);
11523 else
11525 BFD_ASSERT (!info->shared);
11526 if (!mips_finish_exec_plt (output_bfd, info))
11527 return FALSE;
11530 return TRUE;
11534 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
11536 static void
11537 mips_set_isa_flags (bfd *abfd)
11539 flagword val;
11541 switch (bfd_get_mach (abfd))
11543 default:
11544 case bfd_mach_mips3000:
11545 val = E_MIPS_ARCH_1;
11546 break;
11548 case bfd_mach_mips3900:
11549 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
11550 break;
11552 case bfd_mach_mips6000:
11553 val = E_MIPS_ARCH_2;
11554 break;
11556 case bfd_mach_mips4000:
11557 case bfd_mach_mips4300:
11558 case bfd_mach_mips4400:
11559 case bfd_mach_mips4600:
11560 val = E_MIPS_ARCH_3;
11561 break;
11563 case bfd_mach_mips4010:
11564 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
11565 break;
11567 case bfd_mach_mips4100:
11568 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
11569 break;
11571 case bfd_mach_mips4111:
11572 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
11573 break;
11575 case bfd_mach_mips4120:
11576 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
11577 break;
11579 case bfd_mach_mips4650:
11580 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
11581 break;
11583 case bfd_mach_mips5400:
11584 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
11585 break;
11587 case bfd_mach_mips5500:
11588 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
11589 break;
11591 case bfd_mach_mips5900:
11592 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
11593 break;
11595 case bfd_mach_mips9000:
11596 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
11597 break;
11599 case bfd_mach_mips5000:
11600 case bfd_mach_mips7000:
11601 case bfd_mach_mips8000:
11602 case bfd_mach_mips10000:
11603 case bfd_mach_mips12000:
11604 case bfd_mach_mips14000:
11605 case bfd_mach_mips16000:
11606 val = E_MIPS_ARCH_4;
11607 break;
11609 case bfd_mach_mips5:
11610 val = E_MIPS_ARCH_5;
11611 break;
11613 case bfd_mach_mips_loongson_2e:
11614 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
11615 break;
11617 case bfd_mach_mips_loongson_2f:
11618 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
11619 break;
11621 case bfd_mach_mips_sb1:
11622 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
11623 break;
11625 case bfd_mach_mips_loongson_3a:
11626 val = E_MIPS_ARCH_64 | E_MIPS_MACH_LS3A;
11627 break;
11629 case bfd_mach_mips_octeon:
11630 case bfd_mach_mips_octeonp:
11631 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
11632 break;
11634 case bfd_mach_mips_xlr:
11635 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
11636 break;
11638 case bfd_mach_mips_octeon2:
11639 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
11640 break;
11642 case bfd_mach_mipsisa32:
11643 val = E_MIPS_ARCH_32;
11644 break;
11646 case bfd_mach_mipsisa64:
11647 val = E_MIPS_ARCH_64;
11648 break;
11650 case bfd_mach_mipsisa32r2:
11651 val = E_MIPS_ARCH_32R2;
11652 break;
11654 case bfd_mach_mipsisa64r2:
11655 val = E_MIPS_ARCH_64R2;
11656 break;
11658 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11659 elf_elfheader (abfd)->e_flags |= val;
11664 /* The final processing done just before writing out a MIPS ELF object
11665 file. This gets the MIPS architecture right based on the machine
11666 number. This is used by both the 32-bit and the 64-bit ABI. */
11668 void
11669 _bfd_mips_elf_final_write_processing (bfd *abfd,
11670 bfd_boolean linker ATTRIBUTE_UNUSED)
11672 unsigned int i;
11673 Elf_Internal_Shdr **hdrpp;
11674 const char *name;
11675 asection *sec;
11677 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
11678 is nonzero. This is for compatibility with old objects, which used
11679 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
11680 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
11681 mips_set_isa_flags (abfd);
11683 /* Set the sh_info field for .gptab sections and other appropriate
11684 info for each special section. */
11685 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11686 i < elf_numsections (abfd);
11687 i++, hdrpp++)
11689 switch ((*hdrpp)->sh_type)
11691 case SHT_MIPS_MSYM:
11692 case SHT_MIPS_LIBLIST:
11693 sec = bfd_get_section_by_name (abfd, ".dynstr");
11694 if (sec != NULL)
11695 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11696 break;
11698 case SHT_MIPS_GPTAB:
11699 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11700 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11701 BFD_ASSERT (name != NULL
11702 && CONST_STRNEQ (name, ".gptab."));
11703 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11704 BFD_ASSERT (sec != NULL);
11705 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11706 break;
11708 case SHT_MIPS_CONTENT:
11709 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11710 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11711 BFD_ASSERT (name != NULL
11712 && CONST_STRNEQ (name, ".MIPS.content"));
11713 sec = bfd_get_section_by_name (abfd,
11714 name + sizeof ".MIPS.content" - 1);
11715 BFD_ASSERT (sec != NULL);
11716 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11717 break;
11719 case SHT_MIPS_SYMBOL_LIB:
11720 sec = bfd_get_section_by_name (abfd, ".dynsym");
11721 if (sec != NULL)
11722 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11723 sec = bfd_get_section_by_name (abfd, ".liblist");
11724 if (sec != NULL)
11725 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11726 break;
11728 case SHT_MIPS_EVENTS:
11729 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11730 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11731 BFD_ASSERT (name != NULL);
11732 if (CONST_STRNEQ (name, ".MIPS.events"))
11733 sec = bfd_get_section_by_name (abfd,
11734 name + sizeof ".MIPS.events" - 1);
11735 else
11737 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
11738 sec = bfd_get_section_by_name (abfd,
11739 (name
11740 + sizeof ".MIPS.post_rel" - 1));
11742 BFD_ASSERT (sec != NULL);
11743 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11744 break;
11750 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
11751 segments. */
11754 _bfd_mips_elf_additional_program_headers (bfd *abfd,
11755 struct bfd_link_info *info ATTRIBUTE_UNUSED)
11757 asection *s;
11758 int ret = 0;
11760 /* See if we need a PT_MIPS_REGINFO segment. */
11761 s = bfd_get_section_by_name (abfd, ".reginfo");
11762 if (s && (s->flags & SEC_LOAD))
11763 ++ret;
11765 /* See if we need a PT_MIPS_OPTIONS segment. */
11766 if (IRIX_COMPAT (abfd) == ict_irix6
11767 && bfd_get_section_by_name (abfd,
11768 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11769 ++ret;
11771 /* See if we need a PT_MIPS_RTPROC segment. */
11772 if (IRIX_COMPAT (abfd) == ict_irix5
11773 && bfd_get_section_by_name (abfd, ".dynamic")
11774 && bfd_get_section_by_name (abfd, ".mdebug"))
11775 ++ret;
11777 /* Allocate a PT_NULL header in dynamic objects. See
11778 _bfd_mips_elf_modify_segment_map for details. */
11779 if (!SGI_COMPAT (abfd)
11780 && bfd_get_section_by_name (abfd, ".dynamic"))
11781 ++ret;
11783 return ret;
11786 /* Modify the segment map for an IRIX5 executable. */
11788 bfd_boolean
11789 _bfd_mips_elf_modify_segment_map (bfd *abfd,
11790 struct bfd_link_info *info)
11792 asection *s;
11793 struct elf_segment_map *m, **pm;
11794 bfd_size_type amt;
11796 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11797 segment. */
11798 s = bfd_get_section_by_name (abfd, ".reginfo");
11799 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11801 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
11802 if (m->p_type == PT_MIPS_REGINFO)
11803 break;
11804 if (m == NULL)
11806 amt = sizeof *m;
11807 m = bfd_zalloc (abfd, amt);
11808 if (m == NULL)
11809 return FALSE;
11811 m->p_type = PT_MIPS_REGINFO;
11812 m->count = 1;
11813 m->sections[0] = s;
11815 /* We want to put it after the PHDR and INTERP segments. */
11816 pm = &elf_seg_map (abfd);
11817 while (*pm != NULL
11818 && ((*pm)->p_type == PT_PHDR
11819 || (*pm)->p_type == PT_INTERP))
11820 pm = &(*pm)->next;
11822 m->next = *pm;
11823 *pm = m;
11827 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11828 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
11829 PT_MIPS_OPTIONS segment immediately following the program header
11830 table. */
11831 if (NEWABI_P (abfd)
11832 /* On non-IRIX6 new abi, we'll have already created a segment
11833 for this section, so don't create another. I'm not sure this
11834 is not also the case for IRIX 6, but I can't test it right
11835 now. */
11836 && IRIX_COMPAT (abfd) == ict_irix6)
11838 for (s = abfd->sections; s; s = s->next)
11839 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11840 break;
11842 if (s)
11844 struct elf_segment_map *options_segment;
11846 pm = &elf_seg_map (abfd);
11847 while (*pm != NULL
11848 && ((*pm)->p_type == PT_PHDR
11849 || (*pm)->p_type == PT_INTERP))
11850 pm = &(*pm)->next;
11852 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11854 amt = sizeof (struct elf_segment_map);
11855 options_segment = bfd_zalloc (abfd, amt);
11856 options_segment->next = *pm;
11857 options_segment->p_type = PT_MIPS_OPTIONS;
11858 options_segment->p_flags = PF_R;
11859 options_segment->p_flags_valid = TRUE;
11860 options_segment->count = 1;
11861 options_segment->sections[0] = s;
11862 *pm = options_segment;
11866 else
11868 if (IRIX_COMPAT (abfd) == ict_irix5)
11870 /* If there are .dynamic and .mdebug sections, we make a room
11871 for the RTPROC header. FIXME: Rewrite without section names. */
11872 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11873 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11874 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11876 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
11877 if (m->p_type == PT_MIPS_RTPROC)
11878 break;
11879 if (m == NULL)
11881 amt = sizeof *m;
11882 m = bfd_zalloc (abfd, amt);
11883 if (m == NULL)
11884 return FALSE;
11886 m->p_type = PT_MIPS_RTPROC;
11888 s = bfd_get_section_by_name (abfd, ".rtproc");
11889 if (s == NULL)
11891 m->count = 0;
11892 m->p_flags = 0;
11893 m->p_flags_valid = 1;
11895 else
11897 m->count = 1;
11898 m->sections[0] = s;
11901 /* We want to put it after the DYNAMIC segment. */
11902 pm = &elf_seg_map (abfd);
11903 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11904 pm = &(*pm)->next;
11905 if (*pm != NULL)
11906 pm = &(*pm)->next;
11908 m->next = *pm;
11909 *pm = m;
11913 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
11914 .dynstr, .dynsym, and .hash sections, and everything in
11915 between. */
11916 for (pm = &elf_seg_map (abfd); *pm != NULL;
11917 pm = &(*pm)->next)
11918 if ((*pm)->p_type == PT_DYNAMIC)
11919 break;
11920 m = *pm;
11921 if (m != NULL && IRIX_COMPAT (abfd) == ict_none)
11923 /* For a normal mips executable the permissions for the PT_DYNAMIC
11924 segment are read, write and execute. We do that here since
11925 the code in elf.c sets only the read permission. This matters
11926 sometimes for the dynamic linker. */
11927 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
11929 m->p_flags = PF_R | PF_W | PF_X;
11930 m->p_flags_valid = 1;
11933 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11934 glibc's dynamic linker has traditionally derived the number of
11935 tags from the p_filesz field, and sometimes allocates stack
11936 arrays of that size. An overly-big PT_DYNAMIC segment can
11937 be actively harmful in such cases. Making PT_DYNAMIC contain
11938 other sections can also make life hard for the prelinker,
11939 which might move one of the other sections to a different
11940 PT_LOAD segment. */
11941 if (SGI_COMPAT (abfd)
11942 && m != NULL
11943 && m->count == 1
11944 && strcmp (m->sections[0]->name, ".dynamic") == 0)
11946 static const char *sec_names[] =
11948 ".dynamic", ".dynstr", ".dynsym", ".hash"
11950 bfd_vma low, high;
11951 unsigned int i, c;
11952 struct elf_segment_map *n;
11954 low = ~(bfd_vma) 0;
11955 high = 0;
11956 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11958 s = bfd_get_section_by_name (abfd, sec_names[i]);
11959 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11961 bfd_size_type sz;
11963 if (low > s->vma)
11964 low = s->vma;
11965 sz = s->size;
11966 if (high < s->vma + sz)
11967 high = s->vma + sz;
11971 c = 0;
11972 for (s = abfd->sections; s != NULL; s = s->next)
11973 if ((s->flags & SEC_LOAD) != 0
11974 && s->vma >= low
11975 && s->vma + s->size <= high)
11976 ++c;
11978 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
11979 n = bfd_zalloc (abfd, amt);
11980 if (n == NULL)
11981 return FALSE;
11982 *n = *m;
11983 n->count = c;
11985 i = 0;
11986 for (s = abfd->sections; s != NULL; s = s->next)
11988 if ((s->flags & SEC_LOAD) != 0
11989 && s->vma >= low
11990 && s->vma + s->size <= high)
11992 n->sections[i] = s;
11993 ++i;
11997 *pm = n;
12001 /* Allocate a spare program header in dynamic objects so that tools
12002 like the prelinker can add an extra PT_LOAD entry.
12004 If the prelinker needs to make room for a new PT_LOAD entry, its
12005 standard procedure is to move the first (read-only) sections into
12006 the new (writable) segment. However, the MIPS ABI requires
12007 .dynamic to be in a read-only segment, and the section will often
12008 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12010 Although the prelinker could in principle move .dynamic to a
12011 writable segment, it seems better to allocate a spare program
12012 header instead, and avoid the need to move any sections.
12013 There is a long tradition of allocating spare dynamic tags,
12014 so allocating a spare program header seems like a natural
12015 extension.
12017 If INFO is NULL, we may be copying an already prelinked binary
12018 with objcopy or strip, so do not add this header. */
12019 if (info != NULL
12020 && !SGI_COMPAT (abfd)
12021 && bfd_get_section_by_name (abfd, ".dynamic"))
12023 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12024 if ((*pm)->p_type == PT_NULL)
12025 break;
12026 if (*pm == NULL)
12028 m = bfd_zalloc (abfd, sizeof (*m));
12029 if (m == NULL)
12030 return FALSE;
12032 m->p_type = PT_NULL;
12033 *pm = m;
12037 return TRUE;
12040 /* Return the section that should be marked against GC for a given
12041 relocation. */
12043 asection *
12044 _bfd_mips_elf_gc_mark_hook (asection *sec,
12045 struct bfd_link_info *info,
12046 Elf_Internal_Rela *rel,
12047 struct elf_link_hash_entry *h,
12048 Elf_Internal_Sym *sym)
12050 /* ??? Do mips16 stub sections need to be handled special? */
12052 if (h != NULL)
12053 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12055 case R_MIPS_GNU_VTINHERIT:
12056 case R_MIPS_GNU_VTENTRY:
12057 return NULL;
12060 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12063 /* Update the got entry reference counts for the section being removed. */
12065 bfd_boolean
12066 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
12067 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12068 asection *sec ATTRIBUTE_UNUSED,
12069 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
12071 #if 0
12072 Elf_Internal_Shdr *symtab_hdr;
12073 struct elf_link_hash_entry **sym_hashes;
12074 bfd_signed_vma *local_got_refcounts;
12075 const Elf_Internal_Rela *rel, *relend;
12076 unsigned long r_symndx;
12077 struct elf_link_hash_entry *h;
12079 if (info->relocatable)
12080 return TRUE;
12082 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12083 sym_hashes = elf_sym_hashes (abfd);
12084 local_got_refcounts = elf_local_got_refcounts (abfd);
12086 relend = relocs + sec->reloc_count;
12087 for (rel = relocs; rel < relend; rel++)
12088 switch (ELF_R_TYPE (abfd, rel->r_info))
12090 case R_MIPS16_GOT16:
12091 case R_MIPS16_CALL16:
12092 case R_MIPS_GOT16:
12093 case R_MIPS_CALL16:
12094 case R_MIPS_CALL_HI16:
12095 case R_MIPS_CALL_LO16:
12096 case R_MIPS_GOT_HI16:
12097 case R_MIPS_GOT_LO16:
12098 case R_MIPS_GOT_DISP:
12099 case R_MIPS_GOT_PAGE:
12100 case R_MIPS_GOT_OFST:
12101 case R_MICROMIPS_GOT16:
12102 case R_MICROMIPS_CALL16:
12103 case R_MICROMIPS_CALL_HI16:
12104 case R_MICROMIPS_CALL_LO16:
12105 case R_MICROMIPS_GOT_HI16:
12106 case R_MICROMIPS_GOT_LO16:
12107 case R_MICROMIPS_GOT_DISP:
12108 case R_MICROMIPS_GOT_PAGE:
12109 case R_MICROMIPS_GOT_OFST:
12110 /* ??? It would seem that the existing MIPS code does no sort
12111 of reference counting or whatnot on its GOT and PLT entries,
12112 so it is not possible to garbage collect them at this time. */
12113 break;
12115 default:
12116 break;
12118 #endif
12120 return TRUE;
12123 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12124 hiding the old indirect symbol. Process additional relocation
12125 information. Also called for weakdefs, in which case we just let
12126 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12128 void
12129 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12130 struct elf_link_hash_entry *dir,
12131 struct elf_link_hash_entry *ind)
12133 struct mips_elf_link_hash_entry *dirmips, *indmips;
12135 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12137 dirmips = (struct mips_elf_link_hash_entry *) dir;
12138 indmips = (struct mips_elf_link_hash_entry *) ind;
12139 /* Any absolute non-dynamic relocations against an indirect or weak
12140 definition will be against the target symbol. */
12141 if (indmips->has_static_relocs)
12142 dirmips->has_static_relocs = TRUE;
12144 if (ind->root.type != bfd_link_hash_indirect)
12145 return;
12147 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12148 if (indmips->readonly_reloc)
12149 dirmips->readonly_reloc = TRUE;
12150 if (indmips->no_fn_stub)
12151 dirmips->no_fn_stub = TRUE;
12152 if (indmips->fn_stub)
12154 dirmips->fn_stub = indmips->fn_stub;
12155 indmips->fn_stub = NULL;
12157 if (indmips->need_fn_stub)
12159 dirmips->need_fn_stub = TRUE;
12160 indmips->need_fn_stub = FALSE;
12162 if (indmips->call_stub)
12164 dirmips->call_stub = indmips->call_stub;
12165 indmips->call_stub = NULL;
12167 if (indmips->call_fp_stub)
12169 dirmips->call_fp_stub = indmips->call_fp_stub;
12170 indmips->call_fp_stub = NULL;
12172 if (indmips->global_got_area < dirmips->global_got_area)
12173 dirmips->global_got_area = indmips->global_got_area;
12174 if (indmips->global_got_area < GGA_NONE)
12175 indmips->global_got_area = GGA_NONE;
12176 if (indmips->has_nonpic_branches)
12177 dirmips->has_nonpic_branches = TRUE;
12180 #define PDR_SIZE 32
12182 bfd_boolean
12183 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12184 struct bfd_link_info *info)
12186 asection *o;
12187 bfd_boolean ret = FALSE;
12188 unsigned char *tdata;
12189 size_t i, skip;
12191 o = bfd_get_section_by_name (abfd, ".pdr");
12192 if (! o)
12193 return FALSE;
12194 if (o->size == 0)
12195 return FALSE;
12196 if (o->size % PDR_SIZE != 0)
12197 return FALSE;
12198 if (o->output_section != NULL
12199 && bfd_is_abs_section (o->output_section))
12200 return FALSE;
12202 tdata = bfd_zmalloc (o->size / PDR_SIZE);
12203 if (! tdata)
12204 return FALSE;
12206 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
12207 info->keep_memory);
12208 if (!cookie->rels)
12210 free (tdata);
12211 return FALSE;
12214 cookie->rel = cookie->rels;
12215 cookie->relend = cookie->rels + o->reloc_count;
12217 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
12219 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
12221 tdata[i] = 1;
12222 skip ++;
12226 if (skip != 0)
12228 mips_elf_section_data (o)->u.tdata = tdata;
12229 o->size -= skip * PDR_SIZE;
12230 ret = TRUE;
12232 else
12233 free (tdata);
12235 if (! info->keep_memory)
12236 free (cookie->rels);
12238 return ret;
12241 bfd_boolean
12242 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
12244 if (strcmp (sec->name, ".pdr") == 0)
12245 return TRUE;
12246 return FALSE;
12249 bfd_boolean
12250 _bfd_mips_elf_write_section (bfd *output_bfd,
12251 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
12252 asection *sec, bfd_byte *contents)
12254 bfd_byte *to, *from, *end;
12255 int i;
12257 if (strcmp (sec->name, ".pdr") != 0)
12258 return FALSE;
12260 if (mips_elf_section_data (sec)->u.tdata == NULL)
12261 return FALSE;
12263 to = contents;
12264 end = contents + sec->size;
12265 for (from = contents, i = 0;
12266 from < end;
12267 from += PDR_SIZE, i++)
12269 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
12270 continue;
12271 if (to != from)
12272 memcpy (to, from, PDR_SIZE);
12273 to += PDR_SIZE;
12275 bfd_set_section_contents (output_bfd, sec->output_section, contents,
12276 sec->output_offset, sec->size);
12277 return TRUE;
12280 /* microMIPS code retains local labels for linker relaxation. Omit them
12281 from output by default for clarity. */
12283 bfd_boolean
12284 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12286 return _bfd_elf_is_local_label_name (abfd, sym->name);
12289 /* MIPS ELF uses a special find_nearest_line routine in order the
12290 handle the ECOFF debugging information. */
12292 struct mips_elf_find_line
12294 struct ecoff_debug_info d;
12295 struct ecoff_find_line i;
12298 bfd_boolean
12299 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
12300 asymbol **symbols, bfd_vma offset,
12301 const char **filename_ptr,
12302 const char **functionname_ptr,
12303 unsigned int *line_ptr)
12305 asection *msec;
12307 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
12308 filename_ptr, functionname_ptr,
12309 line_ptr))
12310 return TRUE;
12312 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
12313 section, symbols, offset,
12314 filename_ptr, functionname_ptr,
12315 line_ptr, NULL, ABI_64_P (abfd) ? 8 : 0,
12316 &elf_tdata (abfd)->dwarf2_find_line_info))
12317 return TRUE;
12319 msec = bfd_get_section_by_name (abfd, ".mdebug");
12320 if (msec != NULL)
12322 flagword origflags;
12323 struct mips_elf_find_line *fi;
12324 const struct ecoff_debug_swap * const swap =
12325 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12327 /* If we are called during a link, mips_elf_final_link may have
12328 cleared the SEC_HAS_CONTENTS field. We force it back on here
12329 if appropriate (which it normally will be). */
12330 origflags = msec->flags;
12331 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12332 msec->flags |= SEC_HAS_CONTENTS;
12334 fi = mips_elf_tdata (abfd)->find_line_info;
12335 if (fi == NULL)
12337 bfd_size_type external_fdr_size;
12338 char *fraw_src;
12339 char *fraw_end;
12340 struct fdr *fdr_ptr;
12341 bfd_size_type amt = sizeof (struct mips_elf_find_line);
12343 fi = bfd_zalloc (abfd, amt);
12344 if (fi == NULL)
12346 msec->flags = origflags;
12347 return FALSE;
12350 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
12352 msec->flags = origflags;
12353 return FALSE;
12356 /* Swap in the FDR information. */
12357 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
12358 fi->d.fdr = bfd_alloc (abfd, amt);
12359 if (fi->d.fdr == NULL)
12361 msec->flags = origflags;
12362 return FALSE;
12364 external_fdr_size = swap->external_fdr_size;
12365 fdr_ptr = fi->d.fdr;
12366 fraw_src = (char *) fi->d.external_fdr;
12367 fraw_end = (fraw_src
12368 + fi->d.symbolic_header.ifdMax * external_fdr_size);
12369 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
12370 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
12372 mips_elf_tdata (abfd)->find_line_info = fi;
12374 /* Note that we don't bother to ever free this information.
12375 find_nearest_line is either called all the time, as in
12376 objdump -l, so the information should be saved, or it is
12377 rarely called, as in ld error messages, so the memory
12378 wasted is unimportant. Still, it would probably be a
12379 good idea for free_cached_info to throw it away. */
12382 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
12383 &fi->i, filename_ptr, functionname_ptr,
12384 line_ptr))
12386 msec->flags = origflags;
12387 return TRUE;
12390 msec->flags = origflags;
12393 /* Fall back on the generic ELF find_nearest_line routine. */
12395 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
12396 filename_ptr, functionname_ptr,
12397 line_ptr);
12400 bfd_boolean
12401 _bfd_mips_elf_find_inliner_info (bfd *abfd,
12402 const char **filename_ptr,
12403 const char **functionname_ptr,
12404 unsigned int *line_ptr)
12406 bfd_boolean found;
12407 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
12408 functionname_ptr, line_ptr,
12409 & elf_tdata (abfd)->dwarf2_find_line_info);
12410 return found;
12414 /* When are writing out the .options or .MIPS.options section,
12415 remember the bytes we are writing out, so that we can install the
12416 GP value in the section_processing routine. */
12418 bfd_boolean
12419 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
12420 const void *location,
12421 file_ptr offset, bfd_size_type count)
12423 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
12425 bfd_byte *c;
12427 if (elf_section_data (section) == NULL)
12429 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
12430 section->used_by_bfd = bfd_zalloc (abfd, amt);
12431 if (elf_section_data (section) == NULL)
12432 return FALSE;
12434 c = mips_elf_section_data (section)->u.tdata;
12435 if (c == NULL)
12437 c = bfd_zalloc (abfd, section->size);
12438 if (c == NULL)
12439 return FALSE;
12440 mips_elf_section_data (section)->u.tdata = c;
12443 memcpy (c + offset, location, count);
12446 return _bfd_elf_set_section_contents (abfd, section, location, offset,
12447 count);
12450 /* This is almost identical to bfd_generic_get_... except that some
12451 MIPS relocations need to be handled specially. Sigh. */
12453 bfd_byte *
12454 _bfd_elf_mips_get_relocated_section_contents
12455 (bfd *abfd,
12456 struct bfd_link_info *link_info,
12457 struct bfd_link_order *link_order,
12458 bfd_byte *data,
12459 bfd_boolean relocatable,
12460 asymbol **symbols)
12462 /* Get enough memory to hold the stuff */
12463 bfd *input_bfd = link_order->u.indirect.section->owner;
12464 asection *input_section = link_order->u.indirect.section;
12465 bfd_size_type sz;
12467 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12468 arelent **reloc_vector = NULL;
12469 long reloc_count;
12471 if (reloc_size < 0)
12472 goto error_return;
12474 reloc_vector = bfd_malloc (reloc_size);
12475 if (reloc_vector == NULL && reloc_size != 0)
12476 goto error_return;
12478 /* read in the section */
12479 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
12480 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
12481 goto error_return;
12483 reloc_count = bfd_canonicalize_reloc (input_bfd,
12484 input_section,
12485 reloc_vector,
12486 symbols);
12487 if (reloc_count < 0)
12488 goto error_return;
12490 if (reloc_count > 0)
12492 arelent **parent;
12493 /* for mips */
12494 int gp_found;
12495 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
12498 struct bfd_hash_entry *h;
12499 struct bfd_link_hash_entry *lh;
12500 /* Skip all this stuff if we aren't mixing formats. */
12501 if (abfd && input_bfd
12502 && abfd->xvec == input_bfd->xvec)
12503 lh = 0;
12504 else
12506 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
12507 lh = (struct bfd_link_hash_entry *) h;
12509 lookup:
12510 if (lh)
12512 switch (lh->type)
12514 case bfd_link_hash_undefined:
12515 case bfd_link_hash_undefweak:
12516 case bfd_link_hash_common:
12517 gp_found = 0;
12518 break;
12519 case bfd_link_hash_defined:
12520 case bfd_link_hash_defweak:
12521 gp_found = 1;
12522 gp = lh->u.def.value;
12523 break;
12524 case bfd_link_hash_indirect:
12525 case bfd_link_hash_warning:
12526 lh = lh->u.i.link;
12527 /* @@FIXME ignoring warning for now */
12528 goto lookup;
12529 case bfd_link_hash_new:
12530 default:
12531 abort ();
12534 else
12535 gp_found = 0;
12537 /* end mips */
12538 for (parent = reloc_vector; *parent != NULL; parent++)
12540 char *error_message = NULL;
12541 bfd_reloc_status_type r;
12543 /* Specific to MIPS: Deal with relocation types that require
12544 knowing the gp of the output bfd. */
12545 asymbol *sym = *(*parent)->sym_ptr_ptr;
12547 /* If we've managed to find the gp and have a special
12548 function for the relocation then go ahead, else default
12549 to the generic handling. */
12550 if (gp_found
12551 && (*parent)->howto->special_function
12552 == _bfd_mips_elf32_gprel16_reloc)
12553 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
12554 input_section, relocatable,
12555 data, gp);
12556 else
12557 r = bfd_perform_relocation (input_bfd, *parent, data,
12558 input_section,
12559 relocatable ? abfd : NULL,
12560 &error_message);
12562 if (relocatable)
12564 asection *os = input_section->output_section;
12566 /* A partial link, so keep the relocs */
12567 os->orelocation[os->reloc_count] = *parent;
12568 os->reloc_count++;
12571 if (r != bfd_reloc_ok)
12573 switch (r)
12575 case bfd_reloc_undefined:
12576 if (!((*link_info->callbacks->undefined_symbol)
12577 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12578 input_bfd, input_section, (*parent)->address, TRUE)))
12579 goto error_return;
12580 break;
12581 case bfd_reloc_dangerous:
12582 BFD_ASSERT (error_message != NULL);
12583 if (!((*link_info->callbacks->reloc_dangerous)
12584 (link_info, error_message, input_bfd, input_section,
12585 (*parent)->address)))
12586 goto error_return;
12587 break;
12588 case bfd_reloc_overflow:
12589 if (!((*link_info->callbacks->reloc_overflow)
12590 (link_info, NULL,
12591 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12592 (*parent)->howto->name, (*parent)->addend,
12593 input_bfd, input_section, (*parent)->address)))
12594 goto error_return;
12595 break;
12596 case bfd_reloc_outofrange:
12597 default:
12598 abort ();
12599 break;
12605 if (reloc_vector != NULL)
12606 free (reloc_vector);
12607 return data;
12609 error_return:
12610 if (reloc_vector != NULL)
12611 free (reloc_vector);
12612 return NULL;
12615 static bfd_boolean
12616 mips_elf_relax_delete_bytes (bfd *abfd,
12617 asection *sec, bfd_vma addr, int count)
12619 Elf_Internal_Shdr *symtab_hdr;
12620 unsigned int sec_shndx;
12621 bfd_byte *contents;
12622 Elf_Internal_Rela *irel, *irelend;
12623 Elf_Internal_Sym *isym;
12624 Elf_Internal_Sym *isymend;
12625 struct elf_link_hash_entry **sym_hashes;
12626 struct elf_link_hash_entry **end_hashes;
12627 struct elf_link_hash_entry **start_hashes;
12628 unsigned int symcount;
12630 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
12631 contents = elf_section_data (sec)->this_hdr.contents;
12633 irel = elf_section_data (sec)->relocs;
12634 irelend = irel + sec->reloc_count;
12636 /* Actually delete the bytes. */
12637 memmove (contents + addr, contents + addr + count,
12638 (size_t) (sec->size - addr - count));
12639 sec->size -= count;
12641 /* Adjust all the relocs. */
12642 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
12644 /* Get the new reloc address. */
12645 if (irel->r_offset > addr)
12646 irel->r_offset -= count;
12649 BFD_ASSERT (addr % 2 == 0);
12650 BFD_ASSERT (count % 2 == 0);
12652 /* Adjust the local symbols defined in this section. */
12653 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12654 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
12655 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
12656 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
12657 isym->st_value -= count;
12659 /* Now adjust the global symbols defined in this section. */
12660 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
12661 - symtab_hdr->sh_info);
12662 sym_hashes = start_hashes = elf_sym_hashes (abfd);
12663 end_hashes = sym_hashes + symcount;
12665 for (; sym_hashes < end_hashes; sym_hashes++)
12667 struct elf_link_hash_entry *sym_hash = *sym_hashes;
12669 if ((sym_hash->root.type == bfd_link_hash_defined
12670 || sym_hash->root.type == bfd_link_hash_defweak)
12671 && sym_hash->root.u.def.section == sec)
12673 bfd_vma value = sym_hash->root.u.def.value;
12675 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
12676 value &= MINUS_TWO;
12677 if (value > addr)
12678 sym_hash->root.u.def.value -= count;
12682 return TRUE;
12686 /* Opcodes needed for microMIPS relaxation as found in
12687 opcodes/micromips-opc.c. */
12689 struct opcode_descriptor {
12690 unsigned long match;
12691 unsigned long mask;
12694 /* The $ra register aka $31. */
12696 #define RA 31
12698 /* 32-bit instruction format register fields. */
12700 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
12701 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
12703 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
12705 #define OP16_VALID_REG(r) \
12706 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
12709 /* 32-bit and 16-bit branches. */
12711 static const struct opcode_descriptor b_insns_32[] = {
12712 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
12713 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
12714 { 0, 0 } /* End marker for find_match(). */
12717 static const struct opcode_descriptor bc_insn_32 =
12718 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
12720 static const struct opcode_descriptor bz_insn_32 =
12721 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
12723 static const struct opcode_descriptor bzal_insn_32 =
12724 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
12726 static const struct opcode_descriptor beq_insn_32 =
12727 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
12729 static const struct opcode_descriptor b_insn_16 =
12730 { /* "b", "mD", */ 0xcc00, 0xfc00 };
12732 static const struct opcode_descriptor bz_insn_16 =
12733 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
12736 /* 32-bit and 16-bit branch EQ and NE zero. */
12738 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12739 eq and second the ne. This convention is used when replacing a
12740 32-bit BEQ/BNE with the 16-bit version. */
12742 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12744 static const struct opcode_descriptor bz_rs_insns_32[] = {
12745 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12746 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12747 { 0, 0 } /* End marker for find_match(). */
12750 static const struct opcode_descriptor bz_rt_insns_32[] = {
12751 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12752 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12753 { 0, 0 } /* End marker for find_match(). */
12756 static const struct opcode_descriptor bzc_insns_32[] = {
12757 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12758 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12759 { 0, 0 } /* End marker for find_match(). */
12762 static const struct opcode_descriptor bz_insns_16[] = {
12763 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12764 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12765 { 0, 0 } /* End marker for find_match(). */
12768 /* Switch between a 5-bit register index and its 3-bit shorthand. */
12770 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12771 #define BZ16_REG_FIELD(r) \
12772 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12775 /* 32-bit instructions with a delay slot. */
12777 static const struct opcode_descriptor jal_insn_32_bd16 =
12778 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12780 static const struct opcode_descriptor jal_insn_32_bd32 =
12781 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12783 static const struct opcode_descriptor jal_x_insn_32_bd32 =
12784 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12786 static const struct opcode_descriptor j_insn_32 =
12787 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12789 static const struct opcode_descriptor jalr_insn_32 =
12790 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12792 /* This table can be compacted, because no opcode replacement is made. */
12794 static const struct opcode_descriptor ds_insns_32_bd16[] = {
12795 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12797 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12798 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12800 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12801 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12802 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12803 { 0, 0 } /* End marker for find_match(). */
12806 /* This table can be compacted, because no opcode replacement is made. */
12808 static const struct opcode_descriptor ds_insns_32_bd32[] = {
12809 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12811 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12812 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12813 { 0, 0 } /* End marker for find_match(). */
12817 /* 16-bit instructions with a delay slot. */
12819 static const struct opcode_descriptor jalr_insn_16_bd16 =
12820 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12822 static const struct opcode_descriptor jalr_insn_16_bd32 =
12823 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12825 static const struct opcode_descriptor jr_insn_16 =
12826 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12828 #define JR16_REG(opcode) ((opcode) & 0x1f)
12830 /* This table can be compacted, because no opcode replacement is made. */
12832 static const struct opcode_descriptor ds_insns_16_bd16[] = {
12833 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12835 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12836 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12837 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12838 { 0, 0 } /* End marker for find_match(). */
12842 /* LUI instruction. */
12844 static const struct opcode_descriptor lui_insn =
12845 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12848 /* ADDIU instruction. */
12850 static const struct opcode_descriptor addiu_insn =
12851 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12853 static const struct opcode_descriptor addiupc_insn =
12854 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12856 #define ADDIUPC_REG_FIELD(r) \
12857 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12860 /* Relaxable instructions in a JAL delay slot: MOVE. */
12862 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12863 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12864 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12865 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12867 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12868 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12870 static const struct opcode_descriptor move_insns_32[] = {
12871 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12872 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12873 { 0, 0 } /* End marker for find_match(). */
12876 static const struct opcode_descriptor move_insn_16 =
12877 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12880 /* NOP instructions. */
12882 static const struct opcode_descriptor nop_insn_32 =
12883 { /* "nop", "", */ 0x00000000, 0xffffffff };
12885 static const struct opcode_descriptor nop_insn_16 =
12886 { /* "nop", "", */ 0x0c00, 0xffff };
12889 /* Instruction match support. */
12891 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12893 static int
12894 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12896 unsigned long indx;
12898 for (indx = 0; insn[indx].mask != 0; indx++)
12899 if (MATCH (opcode, insn[indx]))
12900 return indx;
12902 return -1;
12906 /* Branch and delay slot decoding support. */
12908 /* If PTR points to what *might* be a 16-bit branch or jump, then
12909 return the minimum length of its delay slot, otherwise return 0.
12910 Non-zero results are not definitive as we might be checking against
12911 the second half of another instruction. */
12913 static int
12914 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12916 unsigned long opcode;
12917 int bdsize;
12919 opcode = bfd_get_16 (abfd, ptr);
12920 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12921 /* 16-bit branch/jump with a 32-bit delay slot. */
12922 bdsize = 4;
12923 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12924 || find_match (opcode, ds_insns_16_bd16) >= 0)
12925 /* 16-bit branch/jump with a 16-bit delay slot. */
12926 bdsize = 2;
12927 else
12928 /* No delay slot. */
12929 bdsize = 0;
12931 return bdsize;
12934 /* If PTR points to what *might* be a 32-bit branch or jump, then
12935 return the minimum length of its delay slot, otherwise return 0.
12936 Non-zero results are not definitive as we might be checking against
12937 the second half of another instruction. */
12939 static int
12940 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12942 unsigned long opcode;
12943 int bdsize;
12945 opcode = bfd_get_micromips_32 (abfd, ptr);
12946 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12947 /* 32-bit branch/jump with a 32-bit delay slot. */
12948 bdsize = 4;
12949 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12950 /* 32-bit branch/jump with a 16-bit delay slot. */
12951 bdsize = 2;
12952 else
12953 /* No delay slot. */
12954 bdsize = 0;
12956 return bdsize;
12959 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12960 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12962 static bfd_boolean
12963 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12965 unsigned long opcode;
12967 opcode = bfd_get_16 (abfd, ptr);
12968 if (MATCH (opcode, b_insn_16)
12969 /* B16 */
12970 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12971 /* JR16 */
12972 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12973 /* BEQZ16, BNEZ16 */
12974 || (MATCH (opcode, jalr_insn_16_bd32)
12975 /* JALR16 */
12976 && reg != JR16_REG (opcode) && reg != RA))
12977 return TRUE;
12979 return FALSE;
12982 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12983 then return TRUE, otherwise FALSE. */
12985 static bfd_boolean
12986 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12988 unsigned long opcode;
12990 opcode = bfd_get_micromips_32 (abfd, ptr);
12991 if (MATCH (opcode, j_insn_32)
12992 /* J */
12993 || MATCH (opcode, bc_insn_32)
12994 /* BC1F, BC1T, BC2F, BC2T */
12995 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
12996 /* JAL, JALX */
12997 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
12998 /* BGEZ, BGTZ, BLEZ, BLTZ */
12999 || (MATCH (opcode, bzal_insn_32)
13000 /* BGEZAL, BLTZAL */
13001 && reg != OP32_SREG (opcode) && reg != RA)
13002 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13003 /* JALR, JALR.HB, BEQ, BNE */
13004 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13005 return TRUE;
13007 return FALSE;
13010 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13011 IRELEND) at OFFSET indicate that there must be a compact branch there,
13012 then return TRUE, otherwise FALSE. */
13014 static bfd_boolean
13015 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13016 const Elf_Internal_Rela *internal_relocs,
13017 const Elf_Internal_Rela *irelend)
13019 const Elf_Internal_Rela *irel;
13020 unsigned long opcode;
13022 opcode = bfd_get_micromips_32 (abfd, ptr);
13023 if (find_match (opcode, bzc_insns_32) < 0)
13024 return FALSE;
13026 for (irel = internal_relocs; irel < irelend; irel++)
13027 if (irel->r_offset == offset
13028 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13029 return TRUE;
13031 return FALSE;
13034 /* Bitsize checking. */
13035 #define IS_BITSIZE(val, N) \
13036 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13037 - (1ULL << ((N) - 1))) == (val))
13040 bfd_boolean
13041 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13042 struct bfd_link_info *link_info,
13043 bfd_boolean *again)
13045 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
13046 Elf_Internal_Shdr *symtab_hdr;
13047 Elf_Internal_Rela *internal_relocs;
13048 Elf_Internal_Rela *irel, *irelend;
13049 bfd_byte *contents = NULL;
13050 Elf_Internal_Sym *isymbuf = NULL;
13052 /* Assume nothing changes. */
13053 *again = FALSE;
13055 /* We don't have to do anything for a relocatable link, if
13056 this section does not have relocs, or if this is not a
13057 code section. */
13059 if (link_info->relocatable
13060 || (sec->flags & SEC_RELOC) == 0
13061 || sec->reloc_count == 0
13062 || (sec->flags & SEC_CODE) == 0)
13063 return TRUE;
13065 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13067 /* Get a copy of the native relocations. */
13068 internal_relocs = (_bfd_elf_link_read_relocs
13069 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
13070 link_info->keep_memory));
13071 if (internal_relocs == NULL)
13072 goto error_return;
13074 /* Walk through them looking for relaxing opportunities. */
13075 irelend = internal_relocs + sec->reloc_count;
13076 for (irel = internal_relocs; irel < irelend; irel++)
13078 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13079 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13080 bfd_boolean target_is_micromips_code_p;
13081 unsigned long opcode;
13082 bfd_vma symval;
13083 bfd_vma pcrval;
13084 bfd_byte *ptr;
13085 int fndopc;
13087 /* The number of bytes to delete for relaxation and from where
13088 to delete these bytes starting at irel->r_offset. */
13089 int delcnt = 0;
13090 int deloff = 0;
13092 /* If this isn't something that can be relaxed, then ignore
13093 this reloc. */
13094 if (r_type != R_MICROMIPS_HI16
13095 && r_type != R_MICROMIPS_PC16_S1
13096 && r_type != R_MICROMIPS_26_S1)
13097 continue;
13099 /* Get the section contents if we haven't done so already. */
13100 if (contents == NULL)
13102 /* Get cached copy if it exists. */
13103 if (elf_section_data (sec)->this_hdr.contents != NULL)
13104 contents = elf_section_data (sec)->this_hdr.contents;
13105 /* Go get them off disk. */
13106 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13107 goto error_return;
13109 ptr = contents + irel->r_offset;
13111 /* Read this BFD's local symbols if we haven't done so already. */
13112 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13114 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13115 if (isymbuf == NULL)
13116 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13117 symtab_hdr->sh_info, 0,
13118 NULL, NULL, NULL);
13119 if (isymbuf == NULL)
13120 goto error_return;
13123 /* Get the value of the symbol referred to by the reloc. */
13124 if (r_symndx < symtab_hdr->sh_info)
13126 /* A local symbol. */
13127 Elf_Internal_Sym *isym;
13128 asection *sym_sec;
13130 isym = isymbuf + r_symndx;
13131 if (isym->st_shndx == SHN_UNDEF)
13132 sym_sec = bfd_und_section_ptr;
13133 else if (isym->st_shndx == SHN_ABS)
13134 sym_sec = bfd_abs_section_ptr;
13135 else if (isym->st_shndx == SHN_COMMON)
13136 sym_sec = bfd_com_section_ptr;
13137 else
13138 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13139 symval = (isym->st_value
13140 + sym_sec->output_section->vma
13141 + sym_sec->output_offset);
13142 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13144 else
13146 unsigned long indx;
13147 struct elf_link_hash_entry *h;
13149 /* An external symbol. */
13150 indx = r_symndx - symtab_hdr->sh_info;
13151 h = elf_sym_hashes (abfd)[indx];
13152 BFD_ASSERT (h != NULL);
13154 if (h->root.type != bfd_link_hash_defined
13155 && h->root.type != bfd_link_hash_defweak)
13156 /* This appears to be a reference to an undefined
13157 symbol. Just ignore it -- it will be caught by the
13158 regular reloc processing. */
13159 continue;
13161 symval = (h->root.u.def.value
13162 + h->root.u.def.section->output_section->vma
13163 + h->root.u.def.section->output_offset);
13164 target_is_micromips_code_p = (!h->needs_plt
13165 && ELF_ST_IS_MICROMIPS (h->other));
13169 /* For simplicity of coding, we are going to modify the
13170 section contents, the section relocs, and the BFD symbol
13171 table. We must tell the rest of the code not to free up this
13172 information. It would be possible to instead create a table
13173 of changes which have to be made, as is done in coff-mips.c;
13174 that would be more work, but would require less memory when
13175 the linker is run. */
13177 /* Only 32-bit instructions relaxed. */
13178 if (irel->r_offset + 4 > sec->size)
13179 continue;
13181 opcode = bfd_get_micromips_32 (abfd, ptr);
13183 /* This is the pc-relative distance from the instruction the
13184 relocation is applied to, to the symbol referred. */
13185 pcrval = (symval
13186 - (sec->output_section->vma + sec->output_offset)
13187 - irel->r_offset);
13189 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13190 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13191 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13193 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13195 where pcrval has first to be adjusted to apply against the LO16
13196 location (we make the adjustment later on, when we have figured
13197 out the offset). */
13198 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13200 bfd_boolean bzc = FALSE;
13201 unsigned long nextopc;
13202 unsigned long reg;
13203 bfd_vma offset;
13205 /* Give up if the previous reloc was a HI16 against this symbol
13206 too. */
13207 if (irel > internal_relocs
13208 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13209 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13210 continue;
13212 /* Or if the next reloc is not a LO16 against this symbol. */
13213 if (irel + 1 >= irelend
13214 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13215 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13216 continue;
13218 /* Or if the second next reloc is a LO16 against this symbol too. */
13219 if (irel + 2 >= irelend
13220 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13221 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13222 continue;
13224 /* See if the LUI instruction *might* be in a branch delay slot.
13225 We check whether what looks like a 16-bit branch or jump is
13226 actually an immediate argument to a compact branch, and let
13227 it through if so. */
13228 if (irel->r_offset >= 2
13229 && check_br16_dslot (abfd, ptr - 2)
13230 && !(irel->r_offset >= 4
13231 && (bzc = check_relocated_bzc (abfd,
13232 ptr - 4, irel->r_offset - 4,
13233 internal_relocs, irelend))))
13234 continue;
13235 if (irel->r_offset >= 4
13236 && !bzc
13237 && check_br32_dslot (abfd, ptr - 4))
13238 continue;
13240 reg = OP32_SREG (opcode);
13242 /* We only relax adjacent instructions or ones separated with
13243 a branch or jump that has a delay slot. The branch or jump
13244 must not fiddle with the register used to hold the address.
13245 Subtract 4 for the LUI itself. */
13246 offset = irel[1].r_offset - irel[0].r_offset;
13247 switch (offset - 4)
13249 case 0:
13250 break;
13251 case 2:
13252 if (check_br16 (abfd, ptr + 4, reg))
13253 break;
13254 continue;
13255 case 4:
13256 if (check_br32 (abfd, ptr + 4, reg))
13257 break;
13258 continue;
13259 default:
13260 continue;
13263 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
13265 /* Give up unless the same register is used with both
13266 relocations. */
13267 if (OP32_SREG (nextopc) != reg)
13268 continue;
13270 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13271 and rounding up to take masking of the two LSBs into account. */
13272 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13274 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13275 if (IS_BITSIZE (symval, 16))
13277 /* Fix the relocation's type. */
13278 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13280 /* Instructions using R_MICROMIPS_LO16 have the base or
13281 source register in bits 20:16. This register becomes $0
13282 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
13283 nextopc &= ~0x001f0000;
13284 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13285 contents + irel[1].r_offset);
13288 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13289 We add 4 to take LUI deletion into account while checking
13290 the PC-relative distance. */
13291 else if (symval % 4 == 0
13292 && IS_BITSIZE (pcrval + 4, 25)
13293 && MATCH (nextopc, addiu_insn)
13294 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13295 && OP16_VALID_REG (OP32_TREG (nextopc)))
13297 /* Fix the relocation's type. */
13298 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13300 /* Replace ADDIU with the ADDIUPC version. */
13301 nextopc = (addiupc_insn.match
13302 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13304 bfd_put_micromips_32 (abfd, nextopc,
13305 contents + irel[1].r_offset);
13308 /* Can't do anything, give up, sigh... */
13309 else
13310 continue;
13312 /* Fix the relocation's type. */
13313 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13315 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13316 delcnt = 4;
13317 deloff = 0;
13320 /* Compact branch relaxation -- due to the multitude of macros
13321 employed by the compiler/assembler, compact branches are not
13322 always generated. Obviously, this can/will be fixed elsewhere,
13323 but there is no drawback in double checking it here. */
13324 else if (r_type == R_MICROMIPS_PC16_S1
13325 && irel->r_offset + 5 < sec->size
13326 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13327 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
13328 && ((!insn32
13329 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13330 nop_insn_16) ? 2 : 0))
13331 || (irel->r_offset + 7 < sec->size
13332 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13333 ptr + 4),
13334 nop_insn_32) ? 4 : 0))))
13336 unsigned long reg;
13338 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13340 /* Replace BEQZ/BNEZ with the compact version. */
13341 opcode = (bzc_insns_32[fndopc].match
13342 | BZC32_REG_FIELD (reg)
13343 | (opcode & 0xffff)); /* Addend value. */
13345 bfd_put_micromips_32 (abfd, opcode, ptr);
13347 /* Delete the delay slot NOP: two or four bytes from
13348 irel->offset + 4; delcnt has already been set above. */
13349 deloff = 4;
13352 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
13353 to check the distance from the next instruction, so subtract 2. */
13354 else if (!insn32
13355 && r_type == R_MICROMIPS_PC16_S1
13356 && IS_BITSIZE (pcrval - 2, 11)
13357 && find_match (opcode, b_insns_32) >= 0)
13359 /* Fix the relocation's type. */
13360 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
13362 /* Replace the 32-bit opcode with a 16-bit opcode. */
13363 bfd_put_16 (abfd,
13364 (b_insn_16.match
13365 | (opcode & 0x3ff)), /* Addend value. */
13366 ptr);
13368 /* Delete 2 bytes from irel->r_offset + 2. */
13369 delcnt = 2;
13370 deloff = 2;
13373 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
13374 to check the distance from the next instruction, so subtract 2. */
13375 else if (!insn32
13376 && r_type == R_MICROMIPS_PC16_S1
13377 && IS_BITSIZE (pcrval - 2, 8)
13378 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13379 && OP16_VALID_REG (OP32_SREG (opcode)))
13380 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
13381 && OP16_VALID_REG (OP32_TREG (opcode)))))
13383 unsigned long reg;
13385 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13387 /* Fix the relocation's type. */
13388 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
13390 /* Replace the 32-bit opcode with a 16-bit opcode. */
13391 bfd_put_16 (abfd,
13392 (bz_insns_16[fndopc].match
13393 | BZ16_REG_FIELD (reg)
13394 | (opcode & 0x7f)), /* Addend value. */
13395 ptr);
13397 /* Delete 2 bytes from irel->r_offset + 2. */
13398 delcnt = 2;
13399 deloff = 2;
13402 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
13403 else if (!insn32
13404 && r_type == R_MICROMIPS_26_S1
13405 && target_is_micromips_code_p
13406 && irel->r_offset + 7 < sec->size
13407 && MATCH (opcode, jal_insn_32_bd32))
13409 unsigned long n32opc;
13410 bfd_boolean relaxed = FALSE;
13412 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
13414 if (MATCH (n32opc, nop_insn_32))
13416 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
13417 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
13419 relaxed = TRUE;
13421 else if (find_match (n32opc, move_insns_32) >= 0)
13423 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
13424 bfd_put_16 (abfd,
13425 (move_insn_16.match
13426 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
13427 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
13428 ptr + 4);
13430 relaxed = TRUE;
13432 /* Other 32-bit instructions relaxable to 16-bit
13433 instructions will be handled here later. */
13435 if (relaxed)
13437 /* JAL with 32-bit delay slot that is changed to a JALS
13438 with 16-bit delay slot. */
13439 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
13441 /* Delete 2 bytes from irel->r_offset + 6. */
13442 delcnt = 2;
13443 deloff = 6;
13447 if (delcnt != 0)
13449 /* Note that we've changed the relocs, section contents, etc. */
13450 elf_section_data (sec)->relocs = internal_relocs;
13451 elf_section_data (sec)->this_hdr.contents = contents;
13452 symtab_hdr->contents = (unsigned char *) isymbuf;
13454 /* Delete bytes depending on the delcnt and deloff. */
13455 if (!mips_elf_relax_delete_bytes (abfd, sec,
13456 irel->r_offset + deloff, delcnt))
13457 goto error_return;
13459 /* That will change things, so we should relax again.
13460 Note that this is not required, and it may be slow. */
13461 *again = TRUE;
13465 if (isymbuf != NULL
13466 && symtab_hdr->contents != (unsigned char *) isymbuf)
13468 if (! link_info->keep_memory)
13469 free (isymbuf);
13470 else
13472 /* Cache the symbols for elf_link_input_bfd. */
13473 symtab_hdr->contents = (unsigned char *) isymbuf;
13477 if (contents != NULL
13478 && elf_section_data (sec)->this_hdr.contents != contents)
13480 if (! link_info->keep_memory)
13481 free (contents);
13482 else
13484 /* Cache the section contents for elf_link_input_bfd. */
13485 elf_section_data (sec)->this_hdr.contents = contents;
13489 if (internal_relocs != NULL
13490 && elf_section_data (sec)->relocs != internal_relocs)
13491 free (internal_relocs);
13493 return TRUE;
13495 error_return:
13496 if (isymbuf != NULL
13497 && symtab_hdr->contents != (unsigned char *) isymbuf)
13498 free (isymbuf);
13499 if (contents != NULL
13500 && elf_section_data (sec)->this_hdr.contents != contents)
13501 free (contents);
13502 if (internal_relocs != NULL
13503 && elf_section_data (sec)->relocs != internal_relocs)
13504 free (internal_relocs);
13506 return FALSE;
13509 /* Create a MIPS ELF linker hash table. */
13511 struct bfd_link_hash_table *
13512 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
13514 struct mips_elf_link_hash_table *ret;
13515 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
13517 ret = bfd_zmalloc (amt);
13518 if (ret == NULL)
13519 return NULL;
13521 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
13522 mips_elf_link_hash_newfunc,
13523 sizeof (struct mips_elf_link_hash_entry),
13524 MIPS_ELF_DATA))
13526 free (ret);
13527 return NULL;
13529 ret->root.init_plt_refcount.plist = NULL;
13530 ret->root.init_plt_offset.plist = NULL;
13532 return &ret->root.root;
13535 /* Likewise, but indicate that the target is VxWorks. */
13537 struct bfd_link_hash_table *
13538 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
13540 struct bfd_link_hash_table *ret;
13542 ret = _bfd_mips_elf_link_hash_table_create (abfd);
13543 if (ret)
13545 struct mips_elf_link_hash_table *htab;
13547 htab = (struct mips_elf_link_hash_table *) ret;
13548 htab->use_plts_and_copy_relocs = TRUE;
13549 htab->is_vxworks = TRUE;
13551 return ret;
13554 /* A function that the linker calls if we are allowed to use PLTs
13555 and copy relocs. */
13557 void
13558 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
13560 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
13563 /* A function that the linker calls to select between all or only
13564 32-bit microMIPS instructions. */
13566 void
13567 _bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
13569 mips_elf_hash_table (info)->insn32 = on;
13572 /* We need to use a special link routine to handle the .reginfo and
13573 the .mdebug sections. We need to merge all instances of these
13574 sections together, not write them all out sequentially. */
13576 bfd_boolean
13577 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
13579 asection *o;
13580 struct bfd_link_order *p;
13581 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
13582 asection *rtproc_sec;
13583 Elf32_RegInfo reginfo;
13584 struct ecoff_debug_info debug;
13585 struct mips_htab_traverse_info hti;
13586 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13587 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
13588 HDRR *symhdr = &debug.symbolic_header;
13589 void *mdebug_handle = NULL;
13590 asection *s;
13591 EXTR esym;
13592 unsigned int i;
13593 bfd_size_type amt;
13594 struct mips_elf_link_hash_table *htab;
13596 static const char * const secname[] =
13598 ".text", ".init", ".fini", ".data",
13599 ".rodata", ".sdata", ".sbss", ".bss"
13601 static const int sc[] =
13603 scText, scInit, scFini, scData,
13604 scRData, scSData, scSBss, scBss
13607 /* Sort the dynamic symbols so that those with GOT entries come after
13608 those without. */
13609 htab = mips_elf_hash_table (info);
13610 BFD_ASSERT (htab != NULL);
13612 if (!mips_elf_sort_hash_table (abfd, info))
13613 return FALSE;
13615 /* Create any scheduled LA25 stubs. */
13616 hti.info = info;
13617 hti.output_bfd = abfd;
13618 hti.error = FALSE;
13619 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
13620 if (hti.error)
13621 return FALSE;
13623 /* Get a value for the GP register. */
13624 if (elf_gp (abfd) == 0)
13626 struct bfd_link_hash_entry *h;
13628 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
13629 if (h != NULL && h->type == bfd_link_hash_defined)
13630 elf_gp (abfd) = (h->u.def.value
13631 + h->u.def.section->output_section->vma
13632 + h->u.def.section->output_offset);
13633 else if (htab->is_vxworks
13634 && (h = bfd_link_hash_lookup (info->hash,
13635 "_GLOBAL_OFFSET_TABLE_",
13636 FALSE, FALSE, TRUE))
13637 && h->type == bfd_link_hash_defined)
13638 elf_gp (abfd) = (h->u.def.section->output_section->vma
13639 + h->u.def.section->output_offset
13640 + h->u.def.value);
13641 else if (info->relocatable)
13643 bfd_vma lo = MINUS_ONE;
13645 /* Find the GP-relative section with the lowest offset. */
13646 for (o = abfd->sections; o != NULL; o = o->next)
13647 if (o->vma < lo
13648 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
13649 lo = o->vma;
13651 /* And calculate GP relative to that. */
13652 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
13654 else
13656 /* If the relocate_section function needs to do a reloc
13657 involving the GP value, it should make a reloc_dangerous
13658 callback to warn that GP is not defined. */
13662 /* Go through the sections and collect the .reginfo and .mdebug
13663 information. */
13664 reginfo_sec = NULL;
13665 mdebug_sec = NULL;
13666 gptab_data_sec = NULL;
13667 gptab_bss_sec = NULL;
13668 for (o = abfd->sections; o != NULL; o = o->next)
13670 if (strcmp (o->name, ".reginfo") == 0)
13672 memset (&reginfo, 0, sizeof reginfo);
13674 /* We have found the .reginfo section in the output file.
13675 Look through all the link_orders comprising it and merge
13676 the information together. */
13677 for (p = o->map_head.link_order; p != NULL; p = p->next)
13679 asection *input_section;
13680 bfd *input_bfd;
13681 Elf32_External_RegInfo ext;
13682 Elf32_RegInfo sub;
13684 if (p->type != bfd_indirect_link_order)
13686 if (p->type == bfd_data_link_order)
13687 continue;
13688 abort ();
13691 input_section = p->u.indirect.section;
13692 input_bfd = input_section->owner;
13694 if (! bfd_get_section_contents (input_bfd, input_section,
13695 &ext, 0, sizeof ext))
13696 return FALSE;
13698 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
13700 reginfo.ri_gprmask |= sub.ri_gprmask;
13701 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
13702 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
13703 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
13704 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
13706 /* ri_gp_value is set by the function
13707 mips_elf32_section_processing when the section is
13708 finally written out. */
13710 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13711 elf_link_input_bfd ignores this section. */
13712 input_section->flags &= ~SEC_HAS_CONTENTS;
13715 /* Size has been set in _bfd_mips_elf_always_size_sections. */
13716 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
13718 /* Skip this section later on (I don't think this currently
13719 matters, but someday it might). */
13720 o->map_head.link_order = NULL;
13722 reginfo_sec = o;
13725 if (strcmp (o->name, ".mdebug") == 0)
13727 struct extsym_info einfo;
13728 bfd_vma last;
13730 /* We have found the .mdebug section in the output file.
13731 Look through all the link_orders comprising it and merge
13732 the information together. */
13733 symhdr->magic = swap->sym_magic;
13734 /* FIXME: What should the version stamp be? */
13735 symhdr->vstamp = 0;
13736 symhdr->ilineMax = 0;
13737 symhdr->cbLine = 0;
13738 symhdr->idnMax = 0;
13739 symhdr->ipdMax = 0;
13740 symhdr->isymMax = 0;
13741 symhdr->ioptMax = 0;
13742 symhdr->iauxMax = 0;
13743 symhdr->issMax = 0;
13744 symhdr->issExtMax = 0;
13745 symhdr->ifdMax = 0;
13746 symhdr->crfd = 0;
13747 symhdr->iextMax = 0;
13749 /* We accumulate the debugging information itself in the
13750 debug_info structure. */
13751 debug.line = NULL;
13752 debug.external_dnr = NULL;
13753 debug.external_pdr = NULL;
13754 debug.external_sym = NULL;
13755 debug.external_opt = NULL;
13756 debug.external_aux = NULL;
13757 debug.ss = NULL;
13758 debug.ssext = debug.ssext_end = NULL;
13759 debug.external_fdr = NULL;
13760 debug.external_rfd = NULL;
13761 debug.external_ext = debug.external_ext_end = NULL;
13763 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
13764 if (mdebug_handle == NULL)
13765 return FALSE;
13767 esym.jmptbl = 0;
13768 esym.cobol_main = 0;
13769 esym.weakext = 0;
13770 esym.reserved = 0;
13771 esym.ifd = ifdNil;
13772 esym.asym.iss = issNil;
13773 esym.asym.st = stLocal;
13774 esym.asym.reserved = 0;
13775 esym.asym.index = indexNil;
13776 last = 0;
13777 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13779 esym.asym.sc = sc[i];
13780 s = bfd_get_section_by_name (abfd, secname[i]);
13781 if (s != NULL)
13783 esym.asym.value = s->vma;
13784 last = s->vma + s->size;
13786 else
13787 esym.asym.value = last;
13788 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13789 secname[i], &esym))
13790 return FALSE;
13793 for (p = o->map_head.link_order; p != NULL; p = p->next)
13795 asection *input_section;
13796 bfd *input_bfd;
13797 const struct ecoff_debug_swap *input_swap;
13798 struct ecoff_debug_info input_debug;
13799 char *eraw_src;
13800 char *eraw_end;
13802 if (p->type != bfd_indirect_link_order)
13804 if (p->type == bfd_data_link_order)
13805 continue;
13806 abort ();
13809 input_section = p->u.indirect.section;
13810 input_bfd = input_section->owner;
13812 if (!is_mips_elf (input_bfd))
13814 /* I don't know what a non MIPS ELF bfd would be
13815 doing with a .mdebug section, but I don't really
13816 want to deal with it. */
13817 continue;
13820 input_swap = (get_elf_backend_data (input_bfd)
13821 ->elf_backend_ecoff_debug_swap);
13823 BFD_ASSERT (p->size == input_section->size);
13825 /* The ECOFF linking code expects that we have already
13826 read in the debugging information and set up an
13827 ecoff_debug_info structure, so we do that now. */
13828 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13829 &input_debug))
13830 return FALSE;
13832 if (! (bfd_ecoff_debug_accumulate
13833 (mdebug_handle, abfd, &debug, swap, input_bfd,
13834 &input_debug, input_swap, info)))
13835 return FALSE;
13837 /* Loop through the external symbols. For each one with
13838 interesting information, try to find the symbol in
13839 the linker global hash table and save the information
13840 for the output external symbols. */
13841 eraw_src = input_debug.external_ext;
13842 eraw_end = (eraw_src
13843 + (input_debug.symbolic_header.iextMax
13844 * input_swap->external_ext_size));
13845 for (;
13846 eraw_src < eraw_end;
13847 eraw_src += input_swap->external_ext_size)
13849 EXTR ext;
13850 const char *name;
13851 struct mips_elf_link_hash_entry *h;
13853 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
13854 if (ext.asym.sc == scNil
13855 || ext.asym.sc == scUndefined
13856 || ext.asym.sc == scSUndefined)
13857 continue;
13859 name = input_debug.ssext + ext.asym.iss;
13860 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
13861 name, FALSE, FALSE, TRUE);
13862 if (h == NULL || h->esym.ifd != -2)
13863 continue;
13865 if (ext.ifd != -1)
13867 BFD_ASSERT (ext.ifd
13868 < input_debug.symbolic_header.ifdMax);
13869 ext.ifd = input_debug.ifdmap[ext.ifd];
13872 h->esym = ext;
13875 /* Free up the information we just read. */
13876 free (input_debug.line);
13877 free (input_debug.external_dnr);
13878 free (input_debug.external_pdr);
13879 free (input_debug.external_sym);
13880 free (input_debug.external_opt);
13881 free (input_debug.external_aux);
13882 free (input_debug.ss);
13883 free (input_debug.ssext);
13884 free (input_debug.external_fdr);
13885 free (input_debug.external_rfd);
13886 free (input_debug.external_ext);
13888 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13889 elf_link_input_bfd ignores this section. */
13890 input_section->flags &= ~SEC_HAS_CONTENTS;
13893 if (SGI_COMPAT (abfd) && info->shared)
13895 /* Create .rtproc section. */
13896 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
13897 if (rtproc_sec == NULL)
13899 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13900 | SEC_LINKER_CREATED | SEC_READONLY);
13902 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
13903 ".rtproc",
13904 flags);
13905 if (rtproc_sec == NULL
13906 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
13907 return FALSE;
13910 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13911 info, rtproc_sec,
13912 &debug))
13913 return FALSE;
13916 /* Build the external symbol information. */
13917 einfo.abfd = abfd;
13918 einfo.info = info;
13919 einfo.debug = &debug;
13920 einfo.swap = swap;
13921 einfo.failed = FALSE;
13922 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
13923 mips_elf_output_extsym, &einfo);
13924 if (einfo.failed)
13925 return FALSE;
13927 /* Set the size of the .mdebug section. */
13928 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
13930 /* Skip this section later on (I don't think this currently
13931 matters, but someday it might). */
13932 o->map_head.link_order = NULL;
13934 mdebug_sec = o;
13937 if (CONST_STRNEQ (o->name, ".gptab."))
13939 const char *subname;
13940 unsigned int c;
13941 Elf32_gptab *tab;
13942 Elf32_External_gptab *ext_tab;
13943 unsigned int j;
13945 /* The .gptab.sdata and .gptab.sbss sections hold
13946 information describing how the small data area would
13947 change depending upon the -G switch. These sections
13948 not used in executables files. */
13949 if (! info->relocatable)
13951 for (p = o->map_head.link_order; p != NULL; p = p->next)
13953 asection *input_section;
13955 if (p->type != bfd_indirect_link_order)
13957 if (p->type == bfd_data_link_order)
13958 continue;
13959 abort ();
13962 input_section = p->u.indirect.section;
13964 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13965 elf_link_input_bfd ignores this section. */
13966 input_section->flags &= ~SEC_HAS_CONTENTS;
13969 /* Skip this section later on (I don't think this
13970 currently matters, but someday it might). */
13971 o->map_head.link_order = NULL;
13973 /* Really remove the section. */
13974 bfd_section_list_remove (abfd, o);
13975 --abfd->section_count;
13977 continue;
13980 /* There is one gptab for initialized data, and one for
13981 uninitialized data. */
13982 if (strcmp (o->name, ".gptab.sdata") == 0)
13983 gptab_data_sec = o;
13984 else if (strcmp (o->name, ".gptab.sbss") == 0)
13985 gptab_bss_sec = o;
13986 else
13988 (*_bfd_error_handler)
13989 (_("%s: illegal section name `%s'"),
13990 bfd_get_filename (abfd), o->name);
13991 bfd_set_error (bfd_error_nonrepresentable_section);
13992 return FALSE;
13995 /* The linker script always combines .gptab.data and
13996 .gptab.sdata into .gptab.sdata, and likewise for
13997 .gptab.bss and .gptab.sbss. It is possible that there is
13998 no .sdata or .sbss section in the output file, in which
13999 case we must change the name of the output section. */
14000 subname = o->name + sizeof ".gptab" - 1;
14001 if (bfd_get_section_by_name (abfd, subname) == NULL)
14003 if (o == gptab_data_sec)
14004 o->name = ".gptab.data";
14005 else
14006 o->name = ".gptab.bss";
14007 subname = o->name + sizeof ".gptab" - 1;
14008 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
14011 /* Set up the first entry. */
14012 c = 1;
14013 amt = c * sizeof (Elf32_gptab);
14014 tab = bfd_malloc (amt);
14015 if (tab == NULL)
14016 return FALSE;
14017 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
14018 tab[0].gt_header.gt_unused = 0;
14020 /* Combine the input sections. */
14021 for (p = o->map_head.link_order; p != NULL; p = p->next)
14023 asection *input_section;
14024 bfd *input_bfd;
14025 bfd_size_type size;
14026 unsigned long last;
14027 bfd_size_type gpentry;
14029 if (p->type != bfd_indirect_link_order)
14031 if (p->type == bfd_data_link_order)
14032 continue;
14033 abort ();
14036 input_section = p->u.indirect.section;
14037 input_bfd = input_section->owner;
14039 /* Combine the gptab entries for this input section one
14040 by one. We know that the input gptab entries are
14041 sorted by ascending -G value. */
14042 size = input_section->size;
14043 last = 0;
14044 for (gpentry = sizeof (Elf32_External_gptab);
14045 gpentry < size;
14046 gpentry += sizeof (Elf32_External_gptab))
14048 Elf32_External_gptab ext_gptab;
14049 Elf32_gptab int_gptab;
14050 unsigned long val;
14051 unsigned long add;
14052 bfd_boolean exact;
14053 unsigned int look;
14055 if (! (bfd_get_section_contents
14056 (input_bfd, input_section, &ext_gptab, gpentry,
14057 sizeof (Elf32_External_gptab))))
14059 free (tab);
14060 return FALSE;
14063 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
14064 &int_gptab);
14065 val = int_gptab.gt_entry.gt_g_value;
14066 add = int_gptab.gt_entry.gt_bytes - last;
14068 exact = FALSE;
14069 for (look = 1; look < c; look++)
14071 if (tab[look].gt_entry.gt_g_value >= val)
14072 tab[look].gt_entry.gt_bytes += add;
14074 if (tab[look].gt_entry.gt_g_value == val)
14075 exact = TRUE;
14078 if (! exact)
14080 Elf32_gptab *new_tab;
14081 unsigned int max;
14083 /* We need a new table entry. */
14084 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
14085 new_tab = bfd_realloc (tab, amt);
14086 if (new_tab == NULL)
14088 free (tab);
14089 return FALSE;
14091 tab = new_tab;
14092 tab[c].gt_entry.gt_g_value = val;
14093 tab[c].gt_entry.gt_bytes = add;
14095 /* Merge in the size for the next smallest -G
14096 value, since that will be implied by this new
14097 value. */
14098 max = 0;
14099 for (look = 1; look < c; look++)
14101 if (tab[look].gt_entry.gt_g_value < val
14102 && (max == 0
14103 || (tab[look].gt_entry.gt_g_value
14104 > tab[max].gt_entry.gt_g_value)))
14105 max = look;
14107 if (max != 0)
14108 tab[c].gt_entry.gt_bytes +=
14109 tab[max].gt_entry.gt_bytes;
14111 ++c;
14114 last = int_gptab.gt_entry.gt_bytes;
14117 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14118 elf_link_input_bfd ignores this section. */
14119 input_section->flags &= ~SEC_HAS_CONTENTS;
14122 /* The table must be sorted by -G value. */
14123 if (c > 2)
14124 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
14126 /* Swap out the table. */
14127 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
14128 ext_tab = bfd_alloc (abfd, amt);
14129 if (ext_tab == NULL)
14131 free (tab);
14132 return FALSE;
14135 for (j = 0; j < c; j++)
14136 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
14137 free (tab);
14139 o->size = c * sizeof (Elf32_External_gptab);
14140 o->contents = (bfd_byte *) ext_tab;
14142 /* Skip this section later on (I don't think this currently
14143 matters, but someday it might). */
14144 o->map_head.link_order = NULL;
14148 /* Invoke the regular ELF backend linker to do all the work. */
14149 if (!bfd_elf_final_link (abfd, info))
14150 return FALSE;
14152 /* Now write out the computed sections. */
14154 if (reginfo_sec != NULL)
14156 Elf32_External_RegInfo ext;
14158 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
14159 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
14160 return FALSE;
14163 if (mdebug_sec != NULL)
14165 BFD_ASSERT (abfd->output_has_begun);
14166 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
14167 swap, info,
14168 mdebug_sec->filepos))
14169 return FALSE;
14171 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
14174 if (gptab_data_sec != NULL)
14176 if (! bfd_set_section_contents (abfd, gptab_data_sec,
14177 gptab_data_sec->contents,
14178 0, gptab_data_sec->size))
14179 return FALSE;
14182 if (gptab_bss_sec != NULL)
14184 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
14185 gptab_bss_sec->contents,
14186 0, gptab_bss_sec->size))
14187 return FALSE;
14190 if (SGI_COMPAT (abfd))
14192 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
14193 if (rtproc_sec != NULL)
14195 if (! bfd_set_section_contents (abfd, rtproc_sec,
14196 rtproc_sec->contents,
14197 0, rtproc_sec->size))
14198 return FALSE;
14202 return TRUE;
14205 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14207 struct mips_mach_extension
14209 unsigned long extension, base;
14213 /* An array describing how BFD machines relate to one another. The entries
14214 are ordered topologically with MIPS I extensions listed last. */
14216 static const struct mips_mach_extension mips_mach_extensions[] =
14218 /* MIPS64r2 extensions. */
14219 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14220 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14221 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14223 /* MIPS64 extensions. */
14224 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14225 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14226 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14227 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64 },
14229 /* MIPS V extensions. */
14230 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14232 /* R10000 extensions. */
14233 { bfd_mach_mips12000, bfd_mach_mips10000 },
14234 { bfd_mach_mips14000, bfd_mach_mips10000 },
14235 { bfd_mach_mips16000, bfd_mach_mips10000 },
14237 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14238 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14239 better to allow vr5400 and vr5500 code to be merged anyway, since
14240 many libraries will just use the core ISA. Perhaps we could add
14241 some sort of ASE flag if this ever proves a problem. */
14242 { bfd_mach_mips5500, bfd_mach_mips5400 },
14243 { bfd_mach_mips5400, bfd_mach_mips5000 },
14245 /* MIPS IV extensions. */
14246 { bfd_mach_mips5, bfd_mach_mips8000 },
14247 { bfd_mach_mips10000, bfd_mach_mips8000 },
14248 { bfd_mach_mips5000, bfd_mach_mips8000 },
14249 { bfd_mach_mips7000, bfd_mach_mips8000 },
14250 { bfd_mach_mips9000, bfd_mach_mips8000 },
14252 /* VR4100 extensions. */
14253 { bfd_mach_mips4120, bfd_mach_mips4100 },
14254 { bfd_mach_mips4111, bfd_mach_mips4100 },
14256 /* MIPS III extensions. */
14257 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14258 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14259 { bfd_mach_mips8000, bfd_mach_mips4000 },
14260 { bfd_mach_mips4650, bfd_mach_mips4000 },
14261 { bfd_mach_mips4600, bfd_mach_mips4000 },
14262 { bfd_mach_mips4400, bfd_mach_mips4000 },
14263 { bfd_mach_mips4300, bfd_mach_mips4000 },
14264 { bfd_mach_mips4100, bfd_mach_mips4000 },
14265 { bfd_mach_mips4010, bfd_mach_mips4000 },
14266 { bfd_mach_mips5900, bfd_mach_mips4000 },
14268 /* MIPS32 extensions. */
14269 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14271 /* MIPS II extensions. */
14272 { bfd_mach_mips4000, bfd_mach_mips6000 },
14273 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14275 /* MIPS I extensions. */
14276 { bfd_mach_mips6000, bfd_mach_mips3000 },
14277 { bfd_mach_mips3900, bfd_mach_mips3000 }
14281 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14283 static bfd_boolean
14284 mips_mach_extends_p (unsigned long base, unsigned long extension)
14286 size_t i;
14288 if (extension == base)
14289 return TRUE;
14291 if (base == bfd_mach_mipsisa32
14292 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14293 return TRUE;
14295 if (base == bfd_mach_mipsisa32r2
14296 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14297 return TRUE;
14299 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14300 if (extension == mips_mach_extensions[i].extension)
14302 extension = mips_mach_extensions[i].base;
14303 if (extension == base)
14304 return TRUE;
14307 return FALSE;
14311 /* Return true if the given ELF header flags describe a 32-bit binary. */
14313 static bfd_boolean
14314 mips_32bit_flags_p (flagword flags)
14316 return ((flags & EF_MIPS_32BITMODE) != 0
14317 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14318 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14319 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14320 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14321 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14322 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
14326 /* Merge object attributes from IBFD into OBFD. Raise an error if
14327 there are conflicting attributes. */
14328 static bfd_boolean
14329 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
14331 obj_attribute *in_attr;
14332 obj_attribute *out_attr;
14333 bfd *abi_fp_bfd;
14334 bfd *abi_msa_bfd;
14336 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
14337 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
14338 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
14339 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
14341 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
14342 if (!abi_msa_bfd
14343 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14344 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
14346 if (!elf_known_obj_attributes_proc (obfd)[0].i)
14348 /* This is the first object. Copy the attributes. */
14349 _bfd_elf_copy_obj_attributes (ibfd, obfd);
14351 /* Use the Tag_null value to indicate the attributes have been
14352 initialized. */
14353 elf_known_obj_attributes_proc (obfd)[0].i = 1;
14355 return TRUE;
14358 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
14359 non-conflicting ones. */
14360 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
14361 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
14363 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
14364 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
14365 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14366 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
14367 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
14369 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14370 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14372 case Val_GNU_MIPS_ABI_FP_SINGLE:
14373 _bfd_error_handler
14374 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14375 obfd, abi_fp_bfd, ibfd, "-mdouble-float", "-msingle-float");
14376 break;
14378 case Val_GNU_MIPS_ABI_FP_SOFT:
14379 _bfd_error_handler
14380 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14381 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
14382 break;
14384 case Val_GNU_MIPS_ABI_FP_64:
14385 _bfd_error_handler
14386 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14387 obfd, abi_fp_bfd, ibfd,
14388 "-mdouble-float", "-mips32r2 -mfp64");
14389 break;
14391 default:
14392 _bfd_error_handler
14393 (_("Warning: %B uses %s (set by %B), "
14394 "%B uses unknown floating point ABI %d"),
14395 obfd, abi_fp_bfd, ibfd,
14396 "-mdouble-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14397 break;
14399 break;
14401 case Val_GNU_MIPS_ABI_FP_SINGLE:
14402 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14404 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14405 _bfd_error_handler
14406 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14407 obfd, abi_fp_bfd, ibfd, "-msingle-float", "-mdouble-float");
14408 break;
14410 case Val_GNU_MIPS_ABI_FP_SOFT:
14411 _bfd_error_handler
14412 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14413 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
14414 break;
14416 case Val_GNU_MIPS_ABI_FP_64:
14417 _bfd_error_handler
14418 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14419 obfd, abi_fp_bfd, ibfd,
14420 "-msingle-float", "-mips32r2 -mfp64");
14421 break;
14423 default:
14424 _bfd_error_handler
14425 (_("Warning: %B uses %s (set by %B), "
14426 "%B uses unknown floating point ABI %d"),
14427 obfd, abi_fp_bfd, ibfd,
14428 "-msingle-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14429 break;
14431 break;
14433 case Val_GNU_MIPS_ABI_FP_SOFT:
14434 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14436 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14437 case Val_GNU_MIPS_ABI_FP_SINGLE:
14438 case Val_GNU_MIPS_ABI_FP_64:
14439 _bfd_error_handler
14440 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14441 obfd, abi_fp_bfd, ibfd, "-msoft-float", "-mhard-float");
14442 break;
14444 default:
14445 _bfd_error_handler
14446 (_("Warning: %B uses %s (set by %B), "
14447 "%B uses unknown floating point ABI %d"),
14448 obfd, abi_fp_bfd, ibfd,
14449 "-msoft-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14450 break;
14452 break;
14454 case Val_GNU_MIPS_ABI_FP_64:
14455 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14457 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14458 _bfd_error_handler
14459 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14460 obfd, abi_fp_bfd, ibfd,
14461 "-mips32r2 -mfp64", "-mdouble-float");
14462 break;
14464 case Val_GNU_MIPS_ABI_FP_SINGLE:
14465 _bfd_error_handler
14466 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14467 obfd, abi_fp_bfd, ibfd,
14468 "-mips32r2 -mfp64", "-msingle-float");
14469 break;
14471 case Val_GNU_MIPS_ABI_FP_SOFT:
14472 _bfd_error_handler
14473 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14474 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
14475 break;
14477 default:
14478 _bfd_error_handler
14479 (_("Warning: %B uses %s (set by %B), "
14480 "%B uses unknown floating point ABI %d"),
14481 obfd, abi_fp_bfd, ibfd,
14482 "-mips32r2 -mfp64", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14483 break;
14485 break;
14487 default:
14488 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14490 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14491 _bfd_error_handler
14492 (_("Warning: %B uses unknown floating point ABI %d "
14493 "(set by %B), %B uses %s"),
14494 obfd, abi_fp_bfd, ibfd,
14495 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mdouble-float");
14496 break;
14498 case Val_GNU_MIPS_ABI_FP_SINGLE:
14499 _bfd_error_handler
14500 (_("Warning: %B uses unknown floating point ABI %d "
14501 "(set by %B), %B uses %s"),
14502 obfd, abi_fp_bfd, ibfd,
14503 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msingle-float");
14504 break;
14506 case Val_GNU_MIPS_ABI_FP_SOFT:
14507 _bfd_error_handler
14508 (_("Warning: %B uses unknown floating point ABI %d "
14509 "(set by %B), %B uses %s"),
14510 obfd, abi_fp_bfd, ibfd,
14511 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msoft-float");
14512 break;
14514 case Val_GNU_MIPS_ABI_FP_64:
14515 _bfd_error_handler
14516 (_("Warning: %B uses unknown floating point ABI %d "
14517 "(set by %B), %B uses %s"),
14518 obfd, abi_fp_bfd, ibfd,
14519 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mips32r2 -mfp64");
14520 break;
14522 default:
14523 _bfd_error_handler
14524 (_("Warning: %B uses unknown floating point ABI %d "
14525 "(set by %B), %B uses unknown floating point ABI %d"),
14526 obfd, abi_fp_bfd, ibfd,
14527 out_attr[Tag_GNU_MIPS_ABI_FP].i,
14528 in_attr[Tag_GNU_MIPS_ABI_FP].i);
14529 break;
14531 break;
14535 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
14536 non-conflicting ones. */
14537 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14539 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
14540 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
14541 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
14542 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14543 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14545 case Val_GNU_MIPS_ABI_MSA_128:
14546 _bfd_error_handler
14547 (_("Warning: %B uses %s (set by %B), "
14548 "%B uses unknown MSA ABI %d"),
14549 obfd, abi_msa_bfd, ibfd,
14550 "-mmsa", in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14551 break;
14553 default:
14554 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
14556 case Val_GNU_MIPS_ABI_MSA_128:
14557 _bfd_error_handler
14558 (_("Warning: %B uses unknown MSA ABI %d "
14559 "(set by %B), %B uses %s"),
14560 obfd, abi_msa_bfd, ibfd,
14561 out_attr[Tag_GNU_MIPS_ABI_MSA].i, "-mmsa");
14562 break;
14564 default:
14565 _bfd_error_handler
14566 (_("Warning: %B uses unknown MSA ABI %d "
14567 "(set by %B), %B uses unknown MSA ABI %d"),
14568 obfd, abi_msa_bfd, ibfd,
14569 out_attr[Tag_GNU_MIPS_ABI_MSA].i,
14570 in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14571 break;
14576 /* Merge Tag_compatibility attributes and any common GNU ones. */
14577 _bfd_elf_merge_object_attributes (ibfd, obfd);
14579 return TRUE;
14582 /* Merge backend specific data from an object file to the output
14583 object file when linking. */
14585 bfd_boolean
14586 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
14588 flagword old_flags;
14589 flagword new_flags;
14590 bfd_boolean ok;
14591 bfd_boolean null_input_bfd = TRUE;
14592 asection *sec;
14594 /* Check if we have the same endianness. */
14595 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
14597 (*_bfd_error_handler)
14598 (_("%B: endianness incompatible with that of the selected emulation"),
14599 ibfd);
14600 return FALSE;
14603 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
14604 return TRUE;
14606 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
14608 (*_bfd_error_handler)
14609 (_("%B: ABI is incompatible with that of the selected emulation"),
14610 ibfd);
14611 return FALSE;
14614 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
14615 return FALSE;
14617 new_flags = elf_elfheader (ibfd)->e_flags;
14618 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
14619 old_flags = elf_elfheader (obfd)->e_flags;
14621 if (! elf_flags_init (obfd))
14623 elf_flags_init (obfd) = TRUE;
14624 elf_elfheader (obfd)->e_flags = new_flags;
14625 elf_elfheader (obfd)->e_ident[EI_CLASS]
14626 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
14628 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
14629 && (bfd_get_arch_info (obfd)->the_default
14630 || mips_mach_extends_p (bfd_get_mach (obfd),
14631 bfd_get_mach (ibfd))))
14633 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
14634 bfd_get_mach (ibfd)))
14635 return FALSE;
14638 return TRUE;
14641 /* Check flag compatibility. */
14643 new_flags &= ~EF_MIPS_NOREORDER;
14644 old_flags &= ~EF_MIPS_NOREORDER;
14646 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
14647 doesn't seem to matter. */
14648 new_flags &= ~EF_MIPS_XGOT;
14649 old_flags &= ~EF_MIPS_XGOT;
14651 /* MIPSpro generates ucode info in n64 objects. Again, we should
14652 just be able to ignore this. */
14653 new_flags &= ~EF_MIPS_UCODE;
14654 old_flags &= ~EF_MIPS_UCODE;
14656 /* DSOs should only be linked with CPIC code. */
14657 if ((ibfd->flags & DYNAMIC) != 0)
14658 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
14660 if (new_flags == old_flags)
14661 return TRUE;
14663 /* Check to see if the input BFD actually contains any sections.
14664 If not, its flags may not have been initialised either, but it cannot
14665 actually cause any incompatibility. */
14666 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
14668 /* Ignore synthetic sections and empty .text, .data and .bss sections
14669 which are automatically generated by gas. Also ignore fake
14670 (s)common sections, since merely defining a common symbol does
14671 not affect compatibility. */
14672 if ((sec->flags & SEC_IS_COMMON) == 0
14673 && strcmp (sec->name, ".reginfo")
14674 && strcmp (sec->name, ".mdebug")
14675 && (sec->size != 0
14676 || (strcmp (sec->name, ".text")
14677 && strcmp (sec->name, ".data")
14678 && strcmp (sec->name, ".bss"))))
14680 null_input_bfd = FALSE;
14681 break;
14684 if (null_input_bfd)
14685 return TRUE;
14687 ok = TRUE;
14689 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
14690 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
14692 (*_bfd_error_handler)
14693 (_("%B: warning: linking abicalls files with non-abicalls files"),
14694 ibfd);
14695 ok = TRUE;
14698 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
14699 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
14700 if (! (new_flags & EF_MIPS_PIC))
14701 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
14703 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
14704 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
14706 /* Compare the ISAs. */
14707 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
14709 (*_bfd_error_handler)
14710 (_("%B: linking 32-bit code with 64-bit code"),
14711 ibfd);
14712 ok = FALSE;
14714 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
14716 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
14717 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
14719 /* Copy the architecture info from IBFD to OBFD. Also copy
14720 the 32-bit flag (if set) so that we continue to recognise
14721 OBFD as a 32-bit binary. */
14722 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
14723 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
14724 elf_elfheader (obfd)->e_flags
14725 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14727 /* Copy across the ABI flags if OBFD doesn't use them
14728 and if that was what caused us to treat IBFD as 32-bit. */
14729 if ((old_flags & EF_MIPS_ABI) == 0
14730 && mips_32bit_flags_p (new_flags)
14731 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
14732 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
14734 else
14736 /* The ISAs aren't compatible. */
14737 (*_bfd_error_handler)
14738 (_("%B: linking %s module with previous %s modules"),
14739 ibfd,
14740 bfd_printable_name (ibfd),
14741 bfd_printable_name (obfd));
14742 ok = FALSE;
14746 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14747 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14749 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
14750 does set EI_CLASS differently from any 32-bit ABI. */
14751 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
14752 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14753 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14755 /* Only error if both are set (to different values). */
14756 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
14757 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14758 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14760 (*_bfd_error_handler)
14761 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
14762 ibfd,
14763 elf_mips_abi_name (ibfd),
14764 elf_mips_abi_name (obfd));
14765 ok = FALSE;
14767 new_flags &= ~EF_MIPS_ABI;
14768 old_flags &= ~EF_MIPS_ABI;
14771 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
14772 and allow arbitrary mixing of the remaining ASEs (retain the union). */
14773 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
14775 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14776 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14777 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
14778 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
14779 int micro_mis = old_m16 && new_micro;
14780 int m16_mis = old_micro && new_m16;
14782 if (m16_mis || micro_mis)
14784 (*_bfd_error_handler)
14785 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
14786 ibfd,
14787 m16_mis ? "MIPS16" : "microMIPS",
14788 m16_mis ? "microMIPS" : "MIPS16");
14789 ok = FALSE;
14792 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
14794 new_flags &= ~ EF_MIPS_ARCH_ASE;
14795 old_flags &= ~ EF_MIPS_ARCH_ASE;
14798 /* Compare NaN encodings. */
14799 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
14801 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
14802 ibfd,
14803 (new_flags & EF_MIPS_NAN2008
14804 ? "-mnan=2008" : "-mnan=legacy"),
14805 (old_flags & EF_MIPS_NAN2008
14806 ? "-mnan=2008" : "-mnan=legacy"));
14807 ok = FALSE;
14808 new_flags &= ~EF_MIPS_NAN2008;
14809 old_flags &= ~EF_MIPS_NAN2008;
14812 /* Warn about any other mismatches */
14813 if (new_flags != old_flags)
14815 (*_bfd_error_handler)
14816 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
14817 ibfd, (unsigned long) new_flags,
14818 (unsigned long) old_flags);
14819 ok = FALSE;
14822 if (! ok)
14824 bfd_set_error (bfd_error_bad_value);
14825 return FALSE;
14828 return TRUE;
14831 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
14833 bfd_boolean
14834 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
14836 BFD_ASSERT (!elf_flags_init (abfd)
14837 || elf_elfheader (abfd)->e_flags == flags);
14839 elf_elfheader (abfd)->e_flags = flags;
14840 elf_flags_init (abfd) = TRUE;
14841 return TRUE;
14844 char *
14845 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
14847 switch (dtag)
14849 default: return "";
14850 case DT_MIPS_RLD_VERSION:
14851 return "MIPS_RLD_VERSION";
14852 case DT_MIPS_TIME_STAMP:
14853 return "MIPS_TIME_STAMP";
14854 case DT_MIPS_ICHECKSUM:
14855 return "MIPS_ICHECKSUM";
14856 case DT_MIPS_IVERSION:
14857 return "MIPS_IVERSION";
14858 case DT_MIPS_FLAGS:
14859 return "MIPS_FLAGS";
14860 case DT_MIPS_BASE_ADDRESS:
14861 return "MIPS_BASE_ADDRESS";
14862 case DT_MIPS_MSYM:
14863 return "MIPS_MSYM";
14864 case DT_MIPS_CONFLICT:
14865 return "MIPS_CONFLICT";
14866 case DT_MIPS_LIBLIST:
14867 return "MIPS_LIBLIST";
14868 case DT_MIPS_LOCAL_GOTNO:
14869 return "MIPS_LOCAL_GOTNO";
14870 case DT_MIPS_CONFLICTNO:
14871 return "MIPS_CONFLICTNO";
14872 case DT_MIPS_LIBLISTNO:
14873 return "MIPS_LIBLISTNO";
14874 case DT_MIPS_SYMTABNO:
14875 return "MIPS_SYMTABNO";
14876 case DT_MIPS_UNREFEXTNO:
14877 return "MIPS_UNREFEXTNO";
14878 case DT_MIPS_GOTSYM:
14879 return "MIPS_GOTSYM";
14880 case DT_MIPS_HIPAGENO:
14881 return "MIPS_HIPAGENO";
14882 case DT_MIPS_RLD_MAP:
14883 return "MIPS_RLD_MAP";
14884 case DT_MIPS_DELTA_CLASS:
14885 return "MIPS_DELTA_CLASS";
14886 case DT_MIPS_DELTA_CLASS_NO:
14887 return "MIPS_DELTA_CLASS_NO";
14888 case DT_MIPS_DELTA_INSTANCE:
14889 return "MIPS_DELTA_INSTANCE";
14890 case DT_MIPS_DELTA_INSTANCE_NO:
14891 return "MIPS_DELTA_INSTANCE_NO";
14892 case DT_MIPS_DELTA_RELOC:
14893 return "MIPS_DELTA_RELOC";
14894 case DT_MIPS_DELTA_RELOC_NO:
14895 return "MIPS_DELTA_RELOC_NO";
14896 case DT_MIPS_DELTA_SYM:
14897 return "MIPS_DELTA_SYM";
14898 case DT_MIPS_DELTA_SYM_NO:
14899 return "MIPS_DELTA_SYM_NO";
14900 case DT_MIPS_DELTA_CLASSSYM:
14901 return "MIPS_DELTA_CLASSSYM";
14902 case DT_MIPS_DELTA_CLASSSYM_NO:
14903 return "MIPS_DELTA_CLASSSYM_NO";
14904 case DT_MIPS_CXX_FLAGS:
14905 return "MIPS_CXX_FLAGS";
14906 case DT_MIPS_PIXIE_INIT:
14907 return "MIPS_PIXIE_INIT";
14908 case DT_MIPS_SYMBOL_LIB:
14909 return "MIPS_SYMBOL_LIB";
14910 case DT_MIPS_LOCALPAGE_GOTIDX:
14911 return "MIPS_LOCALPAGE_GOTIDX";
14912 case DT_MIPS_LOCAL_GOTIDX:
14913 return "MIPS_LOCAL_GOTIDX";
14914 case DT_MIPS_HIDDEN_GOTIDX:
14915 return "MIPS_HIDDEN_GOTIDX";
14916 case DT_MIPS_PROTECTED_GOTIDX:
14917 return "MIPS_PROTECTED_GOT_IDX";
14918 case DT_MIPS_OPTIONS:
14919 return "MIPS_OPTIONS";
14920 case DT_MIPS_INTERFACE:
14921 return "MIPS_INTERFACE";
14922 case DT_MIPS_DYNSTR_ALIGN:
14923 return "DT_MIPS_DYNSTR_ALIGN";
14924 case DT_MIPS_INTERFACE_SIZE:
14925 return "DT_MIPS_INTERFACE_SIZE";
14926 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14927 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14928 case DT_MIPS_PERF_SUFFIX:
14929 return "DT_MIPS_PERF_SUFFIX";
14930 case DT_MIPS_COMPACT_SIZE:
14931 return "DT_MIPS_COMPACT_SIZE";
14932 case DT_MIPS_GP_VALUE:
14933 return "DT_MIPS_GP_VALUE";
14934 case DT_MIPS_AUX_DYNAMIC:
14935 return "DT_MIPS_AUX_DYNAMIC";
14936 case DT_MIPS_PLTGOT:
14937 return "DT_MIPS_PLTGOT";
14938 case DT_MIPS_RWPLT:
14939 return "DT_MIPS_RWPLT";
14943 bfd_boolean
14944 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
14946 FILE *file = ptr;
14948 BFD_ASSERT (abfd != NULL && ptr != NULL);
14950 /* Print normal ELF private data. */
14951 _bfd_elf_print_private_bfd_data (abfd, ptr);
14953 /* xgettext:c-format */
14954 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14956 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14957 fprintf (file, _(" [abi=O32]"));
14958 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14959 fprintf (file, _(" [abi=O64]"));
14960 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14961 fprintf (file, _(" [abi=EABI32]"));
14962 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14963 fprintf (file, _(" [abi=EABI64]"));
14964 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14965 fprintf (file, _(" [abi unknown]"));
14966 else if (ABI_N32_P (abfd))
14967 fprintf (file, _(" [abi=N32]"));
14968 else if (ABI_64_P (abfd))
14969 fprintf (file, _(" [abi=64]"));
14970 else
14971 fprintf (file, _(" [no abi set]"));
14973 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
14974 fprintf (file, " [mips1]");
14975 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
14976 fprintf (file, " [mips2]");
14977 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
14978 fprintf (file, " [mips3]");
14979 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
14980 fprintf (file, " [mips4]");
14981 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
14982 fprintf (file, " [mips5]");
14983 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
14984 fprintf (file, " [mips32]");
14985 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
14986 fprintf (file, " [mips64]");
14987 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
14988 fprintf (file, " [mips32r2]");
14989 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
14990 fprintf (file, " [mips64r2]");
14991 else
14992 fprintf (file, _(" [unknown ISA]"));
14994 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14995 fprintf (file, " [mdmx]");
14997 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14998 fprintf (file, " [mips16]");
15000 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
15001 fprintf (file, " [micromips]");
15003 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
15004 fprintf (file, " [nan2008]");
15006 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
15007 fprintf (file, " [fp64]");
15009 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
15010 fprintf (file, " [32bitmode]");
15011 else
15012 fprintf (file, _(" [not 32bitmode]"));
15014 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
15015 fprintf (file, " [noreorder]");
15017 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
15018 fprintf (file, " [PIC]");
15020 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
15021 fprintf (file, " [CPIC]");
15023 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
15024 fprintf (file, " [XGOT]");
15026 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
15027 fprintf (file, " [UCODE]");
15029 fputc ('\n', file);
15031 return TRUE;
15034 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
15036 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15037 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15038 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
15039 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15040 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15041 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
15042 { NULL, 0, 0, 0, 0 }
15045 /* Merge non visibility st_other attributes. Ensure that the
15046 STO_OPTIONAL flag is copied into h->other, even if this is not a
15047 definiton of the symbol. */
15048 void
15049 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
15050 const Elf_Internal_Sym *isym,
15051 bfd_boolean definition,
15052 bfd_boolean dynamic ATTRIBUTE_UNUSED)
15054 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
15056 unsigned char other;
15058 other = (definition ? isym->st_other : h->other);
15059 other &= ~ELF_ST_VISIBILITY (-1);
15060 h->other = other | ELF_ST_VISIBILITY (h->other);
15063 if (!definition
15064 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
15065 h->other |= STO_OPTIONAL;
15068 /* Decide whether an undefined symbol is special and can be ignored.
15069 This is the case for OPTIONAL symbols on IRIX. */
15070 bfd_boolean
15071 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
15073 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
15076 bfd_boolean
15077 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
15079 return (sym->st_shndx == SHN_COMMON
15080 || sym->st_shndx == SHN_MIPS_ACOMMON
15081 || sym->st_shndx == SHN_MIPS_SCOMMON);
15084 /* Return address for Ith PLT stub in section PLT, for relocation REL
15085 or (bfd_vma) -1 if it should not be included. */
15087 bfd_vma
15088 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
15089 const arelent *rel ATTRIBUTE_UNUSED)
15091 return (plt->vma
15092 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
15093 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
15096 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
15097 and microMIPS PLT slots we may have a many-to-one mapping between .plt
15098 and .got.plt and also the slots may be of a different size each we walk
15099 the PLT manually fetching instructions and matching them against known
15100 patterns. To make things easier standard MIPS slots, if any, always come
15101 first. As we don't create proper ELF symbols we use the UDATA.I member
15102 of ASYMBOL to carry ISA annotation. The encoding used is the same as
15103 with the ST_OTHER member of the ELF symbol. */
15105 long
15106 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
15107 long symcount ATTRIBUTE_UNUSED,
15108 asymbol **syms ATTRIBUTE_UNUSED,
15109 long dynsymcount, asymbol **dynsyms,
15110 asymbol **ret)
15112 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
15113 static const char microsuffix[] = "@micromipsplt";
15114 static const char m16suffix[] = "@mips16plt";
15115 static const char mipssuffix[] = "@plt";
15117 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
15118 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15119 bfd_boolean micromips_p = MICROMIPS_P (abfd);
15120 Elf_Internal_Shdr *hdr;
15121 bfd_byte *plt_data;
15122 bfd_vma plt_offset;
15123 unsigned int other;
15124 bfd_vma entry_size;
15125 bfd_vma plt0_size;
15126 asection *relplt;
15127 bfd_vma opcode;
15128 asection *plt;
15129 asymbol *send;
15130 size_t size;
15131 char *names;
15132 long counti;
15133 arelent *p;
15134 asymbol *s;
15135 char *nend;
15136 long count;
15137 long pi;
15138 long i;
15139 long n;
15141 *ret = NULL;
15143 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
15144 return 0;
15146 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
15147 if (relplt == NULL)
15148 return 0;
15150 hdr = &elf_section_data (relplt)->this_hdr;
15151 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
15152 return 0;
15154 plt = bfd_get_section_by_name (abfd, ".plt");
15155 if (plt == NULL)
15156 return 0;
15158 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
15159 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
15160 return -1;
15161 p = relplt->relocation;
15163 /* Calculating the exact amount of space required for symbols would
15164 require two passes over the PLT, so just pessimise assuming two
15165 PLT slots per relocation. */
15166 count = relplt->size / hdr->sh_entsize;
15167 counti = count * bed->s->int_rels_per_ext_rel;
15168 size = 2 * count * sizeof (asymbol);
15169 size += count * (sizeof (mipssuffix) +
15170 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
15171 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
15172 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
15174 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
15175 size += sizeof (asymbol) + sizeof (pltname);
15177 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
15178 return -1;
15180 if (plt->size < 16)
15181 return -1;
15183 s = *ret = bfd_malloc (size);
15184 if (s == NULL)
15185 return -1;
15186 send = s + 2 * count + 1;
15188 names = (char *) send;
15189 nend = (char *) s + size;
15190 n = 0;
15192 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
15193 if (opcode == 0x3302fffe)
15195 if (!micromips_p)
15196 return -1;
15197 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
15198 other = STO_MICROMIPS;
15200 else if (opcode == 0x0398c1d0)
15202 if (!micromips_p)
15203 return -1;
15204 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
15205 other = STO_MICROMIPS;
15207 else
15209 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
15210 other = 0;
15213 s->the_bfd = abfd;
15214 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
15215 s->section = plt;
15216 s->value = 0;
15217 s->name = names;
15218 s->udata.i = other;
15219 memcpy (names, pltname, sizeof (pltname));
15220 names += sizeof (pltname);
15221 ++s, ++n;
15223 pi = 0;
15224 for (plt_offset = plt0_size;
15225 plt_offset + 8 <= plt->size && s < send;
15226 plt_offset += entry_size)
15228 bfd_vma gotplt_addr;
15229 const char *suffix;
15230 bfd_vma gotplt_hi;
15231 bfd_vma gotplt_lo;
15232 size_t suffixlen;
15234 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
15236 /* Check if the second word matches the expected MIPS16 instruction. */
15237 if (opcode == 0x651aeb00)
15239 if (micromips_p)
15240 return -1;
15241 /* Truncated table??? */
15242 if (plt_offset + 16 > plt->size)
15243 break;
15244 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
15245 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
15246 suffixlen = sizeof (m16suffix);
15247 suffix = m16suffix;
15248 other = STO_MIPS16;
15250 /* Likewise the expected microMIPS instruction (no insn32 mode). */
15251 else if (opcode == 0xff220000)
15253 if (!micromips_p)
15254 return -1;
15255 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
15256 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15257 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
15258 gotplt_lo <<= 2;
15259 gotplt_addr = gotplt_hi + gotplt_lo;
15260 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
15261 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
15262 suffixlen = sizeof (microsuffix);
15263 suffix = microsuffix;
15264 other = STO_MICROMIPS;
15266 /* Likewise the expected microMIPS instruction (insn32 mode). */
15267 else if ((opcode & 0xffff0000) == 0xff2f0000)
15269 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15270 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
15271 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15272 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15273 gotplt_addr = gotplt_hi + gotplt_lo;
15274 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
15275 suffixlen = sizeof (microsuffix);
15276 suffix = microsuffix;
15277 other = STO_MICROMIPS;
15279 /* Otherwise assume standard MIPS code. */
15280 else
15282 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
15283 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
15284 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15285 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15286 gotplt_addr = gotplt_hi + gotplt_lo;
15287 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
15288 suffixlen = sizeof (mipssuffix);
15289 suffix = mipssuffix;
15290 other = 0;
15292 /* Truncated table??? */
15293 if (plt_offset + entry_size > plt->size)
15294 break;
15296 for (i = 0;
15297 i < count && p[pi].address != gotplt_addr;
15298 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
15300 if (i < count)
15302 size_t namelen;
15303 size_t len;
15305 *s = **p[pi].sym_ptr_ptr;
15306 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
15307 we are defining a symbol, ensure one of them is set. */
15308 if ((s->flags & BSF_LOCAL) == 0)
15309 s->flags |= BSF_GLOBAL;
15310 s->flags |= BSF_SYNTHETIC;
15311 s->section = plt;
15312 s->value = plt_offset;
15313 s->name = names;
15314 s->udata.i = other;
15316 len = strlen ((*p[pi].sym_ptr_ptr)->name);
15317 namelen = len + suffixlen;
15318 if (names + namelen > nend)
15319 break;
15321 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
15322 names += len;
15323 memcpy (names, suffix, suffixlen);
15324 names += suffixlen;
15326 ++s, ++n;
15327 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
15331 free (plt_data);
15333 return n;
15336 void
15337 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
15339 struct mips_elf_link_hash_table *htab;
15340 Elf_Internal_Ehdr *i_ehdrp;
15342 i_ehdrp = elf_elfheader (abfd);
15343 if (link_info)
15345 htab = mips_elf_hash_table (link_info);
15346 BFD_ASSERT (htab != NULL);
15348 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
15349 i_ehdrp->e_ident[EI_ABIVERSION] = 1;