elfxx-mips.c revision 1.1.1.7.4.1 1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2019 Free Software Foundation, Inc.
3
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian (at) cygnus.com>.
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7 <mark (at) codesourcery.com>
8 Traditional MIPS targets support added by Koundinya.K, Dansk Data
9 Elektronik & Operations Research Group. <kk (at) ddeorg.soft.net>
10
11 This file is part of BFD, the Binary File Descriptor library.
12
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.
17
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.
22
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. */
27
28
29 /* This file handles functionality common to the different MIPS ABI's. */
30
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"
39 #include "dwarf2.h"
40
41 /* Get the ECOFF swapping routines. */
42 #include "coff/sym.h"
43 #include "coff/symconst.h"
44 #include "coff/ecoff.h"
45 #include "coff/mips.h"
46
47 #include "hashtab.h"
48
49 /* Types of TLS GOT entry. */
50 enum mips_got_tls_type {
51 GOT_TLS_NONE,
52 GOT_TLS_GD,
53 GOT_TLS_LDM,
54 GOT_TLS_IE
55 };
56
57 /* This structure is used to hold information about one GOT entry.
58 There are four types of entry:
59
60 (1) an absolute address
61 requires: abfd == NULL
62 fields: d.address
63
64 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
65 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
66 fields: abfd, symndx, d.addend, tls_type
67
68 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
69 requires: abfd != NULL, symndx == -1
70 fields: d.h, tls_type
71
72 (4) a TLS LDM slot
73 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
74 fields: none; there's only one of these per GOT. */
75 struct mips_got_entry
76 {
77 /* One input bfd that needs the GOT entry. */
78 bfd *abfd;
79 /* The index of the symbol, as stored in the relocation r_info, if
80 we have a local symbol; -1 otherwise. */
81 long symndx;
82 union
83 {
84 /* If abfd == NULL, an address that must be stored in the got. */
85 bfd_vma address;
86 /* If abfd != NULL && symndx != -1, the addend of the relocation
87 that should be added to the symbol value. */
88 bfd_vma addend;
89 /* If abfd != NULL && symndx == -1, the hash table entry
90 corresponding to a symbol in the GOT. The symbol's entry
91 is in the local area if h->global_got_area is GGA_NONE,
92 otherwise it is in the global area. */
93 struct mips_elf_link_hash_entry *h;
94 } d;
95
96 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
97 symbol entry with r_symndx == 0. */
98 unsigned char tls_type;
99
100 /* True if we have filled in the GOT contents for a TLS entry,
101 and created the associated relocations. */
102 unsigned char tls_initialized;
103
104 /* The offset from the beginning of the .got section to the entry
105 corresponding to this symbol+addend. If it's a global symbol
106 whose offset is yet to be decided, it's going to be -1. */
107 long gotidx;
108 };
109
110 /* This structure represents a GOT page reference from an input bfd.
111 Each instance represents a symbol + ADDEND, where the representation
112 of the symbol depends on whether it is local to the input bfd.
113 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
114 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
115
116 Page references with SYMNDX >= 0 always become page references
117 in the output. Page references with SYMNDX < 0 only become page
118 references if the symbol binds locally; in other cases, the page
119 reference decays to a global GOT reference. */
120 struct mips_got_page_ref
121 {
122 long symndx;
123 union
124 {
125 struct mips_elf_link_hash_entry *h;
126 bfd *abfd;
127 } u;
128 bfd_vma addend;
129 };
130
131 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
132 The structures form a non-overlapping list that is sorted by increasing
133 MIN_ADDEND. */
134 struct mips_got_page_range
135 {
136 struct mips_got_page_range *next;
137 bfd_signed_vma min_addend;
138 bfd_signed_vma max_addend;
139 };
140
141 /* This structure describes the range of addends that are applied to page
142 relocations against a given section. */
143 struct mips_got_page_entry
144 {
145 /* The section that these entries are based on. */
146 asection *sec;
147 /* The ranges for this page entry. */
148 struct mips_got_page_range *ranges;
149 /* The maximum number of page entries needed for RANGES. */
150 bfd_vma num_pages;
151 };
152
153 /* This structure is used to hold .got information when linking. */
154
155 struct mips_got_info
156 {
157 /* The number of global .got entries. */
158 unsigned int global_gotno;
159 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
160 unsigned int reloc_only_gotno;
161 /* The number of .got slots used for TLS. */
162 unsigned int tls_gotno;
163 /* The first unused TLS .got entry. Used only during
164 mips_elf_initialize_tls_index. */
165 unsigned int tls_assigned_gotno;
166 /* The number of local .got entries, eventually including page entries. */
167 unsigned int local_gotno;
168 /* The maximum number of page entries needed. */
169 unsigned int page_gotno;
170 /* The number of relocations needed for the GOT entries. */
171 unsigned int relocs;
172 /* The first unused local .got entry. */
173 unsigned int assigned_low_gotno;
174 /* The last unused local .got entry. */
175 unsigned int assigned_high_gotno;
176 /* A hash table holding members of the got. */
177 struct htab *got_entries;
178 /* A hash table holding mips_got_page_ref structures. */
179 struct htab *got_page_refs;
180 /* A hash table of mips_got_page_entry structures. */
181 struct htab *got_page_entries;
182 /* In multi-got links, a pointer to the next got (err, rather, most
183 of the time, it points to the previous got). */
184 struct mips_got_info *next;
185 };
186
187 /* Structure passed when merging bfds' gots. */
188
189 struct mips_elf_got_per_bfd_arg
190 {
191 /* The output bfd. */
192 bfd *obfd;
193 /* The link information. */
194 struct bfd_link_info *info;
195 /* A pointer to the primary got, i.e., the one that's going to get
196 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
197 DT_MIPS_GOTSYM. */
198 struct mips_got_info *primary;
199 /* A non-primary got we're trying to merge with other input bfd's
200 gots. */
201 struct mips_got_info *current;
202 /* The maximum number of got entries that can be addressed with a
203 16-bit offset. */
204 unsigned int max_count;
205 /* The maximum number of page entries needed by each got. */
206 unsigned int max_pages;
207 /* The total number of global entries which will live in the
208 primary got and be automatically relocated. This includes
209 those not referenced by the primary GOT but included in
210 the "master" GOT. */
211 unsigned int global_count;
212 };
213
214 /* A structure used to pass information to htab_traverse callbacks
215 when laying out the GOT. */
216
217 struct mips_elf_traverse_got_arg
218 {
219 struct bfd_link_info *info;
220 struct mips_got_info *g;
221 int value;
222 };
223
224 struct _mips_elf_section_data
225 {
226 struct bfd_elf_section_data elf;
227 union
228 {
229 bfd_byte *tdata;
230 } u;
231 };
232
233 #define mips_elf_section_data(sec) \
234 ((struct _mips_elf_section_data *) elf_section_data (sec))
235
236 #define is_mips_elf(bfd) \
237 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
238 && elf_tdata (bfd) != NULL \
239 && elf_object_id (bfd) == MIPS_ELF_DATA)
240
241 /* The ABI says that every symbol used by dynamic relocations must have
242 a global GOT entry. Among other things, this provides the dynamic
243 linker with a free, directly-indexed cache. The GOT can therefore
244 contain symbols that are not referenced by GOT relocations themselves
245 (in other words, it may have symbols that are not referenced by things
246 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
247
248 GOT relocations are less likely to overflow if we put the associated
249 GOT entries towards the beginning. We therefore divide the global
250 GOT entries into two areas: "normal" and "reloc-only". Entries in
251 the first area can be used for both dynamic relocations and GP-relative
252 accesses, while those in the "reloc-only" area are for dynamic
253 relocations only.
254
255 These GGA_* ("Global GOT Area") values are organised so that lower
256 values are more general than higher values. Also, non-GGA_NONE
257 values are ordered by the position of the area in the GOT. */
258 #define GGA_NORMAL 0
259 #define GGA_RELOC_ONLY 1
260 #define GGA_NONE 2
261
262 /* Information about a non-PIC interface to a PIC function. There are
263 two ways of creating these interfaces. The first is to add:
264
265 lui $25,%hi(func)
266 addiu $25,$25,%lo(func)
267
268 immediately before a PIC function "func". The second is to add:
269
270 lui $25,%hi(func)
271 j func
272 addiu $25,$25,%lo(func)
273
274 to a separate trampoline section.
275
276 Stubs of the first kind go in a new section immediately before the
277 target function. Stubs of the second kind go in a single section
278 pointed to by the hash table's "strampoline" field. */
279 struct mips_elf_la25_stub {
280 /* The generated section that contains this stub. */
281 asection *stub_section;
282
283 /* The offset of the stub from the start of STUB_SECTION. */
284 bfd_vma offset;
285
286 /* One symbol for the original function. Its location is available
287 in H->root.root.u.def. */
288 struct mips_elf_link_hash_entry *h;
289 };
290
291 /* Macros for populating a mips_elf_la25_stub. */
292
293 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
294 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
295 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
296 #define LA25_LUI_MICROMIPS(VAL) \
297 (0x41b90000 | (VAL)) /* lui t9,VAL */
298 #define LA25_J_MICROMIPS(VAL) \
299 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
300 #define LA25_ADDIU_MICROMIPS(VAL) \
301 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
302
303 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
304 the dynamic symbols. */
305
306 struct mips_elf_hash_sort_data
307 {
308 /* The symbol in the global GOT with the lowest dynamic symbol table
309 index. */
310 struct elf_link_hash_entry *low;
311 /* The least dynamic symbol table index corresponding to a non-TLS
312 symbol with a GOT entry. */
313 bfd_size_type min_got_dynindx;
314 /* The greatest dynamic symbol table index corresponding to a symbol
315 with a GOT entry that is not referenced (e.g., a dynamic symbol
316 with dynamic relocations pointing to it from non-primary GOTs). */
317 bfd_size_type max_unref_got_dynindx;
318 /* The greatest dynamic symbol table index corresponding to a local
319 symbol. */
320 bfd_size_type max_local_dynindx;
321 /* The greatest dynamic symbol table index corresponding to an external
322 symbol without a GOT entry. */
323 bfd_size_type max_non_got_dynindx;
324 };
325
326 /* We make up to two PLT entries if needed, one for standard MIPS code
327 and one for compressed code, either a MIPS16 or microMIPS one. We
328 keep a separate record of traditional lazy-binding stubs, for easier
329 processing. */
330
331 struct plt_entry
332 {
333 /* Traditional SVR4 stub offset, or -1 if none. */
334 bfd_vma stub_offset;
335
336 /* Standard PLT entry offset, or -1 if none. */
337 bfd_vma mips_offset;
338
339 /* Compressed PLT entry offset, or -1 if none. */
340 bfd_vma comp_offset;
341
342 /* The corresponding .got.plt index, or -1 if none. */
343 bfd_vma gotplt_index;
344
345 /* Whether we need a standard PLT entry. */
346 unsigned int need_mips : 1;
347
348 /* Whether we need a compressed PLT entry. */
349 unsigned int need_comp : 1;
350 };
351
352 /* The MIPS ELF linker needs additional information for each symbol in
353 the global hash table. */
354
355 struct mips_elf_link_hash_entry
356 {
357 struct elf_link_hash_entry root;
358
359 /* External symbol information. */
360 EXTR esym;
361
362 /* The la25 stub we have created for ths symbol, if any. */
363 struct mips_elf_la25_stub *la25_stub;
364
365 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
366 this symbol. */
367 unsigned int possibly_dynamic_relocs;
368
369 /* If there is a stub that 32 bit functions should use to call this
370 16 bit function, this points to the section containing the stub. */
371 asection *fn_stub;
372
373 /* If there is a stub that 16 bit functions should use to call this
374 32 bit function, this points to the section containing the stub. */
375 asection *call_stub;
376
377 /* This is like the call_stub field, but it is used if the function
378 being called returns a floating point value. */
379 asection *call_fp_stub;
380
381 /* The highest GGA_* value that satisfies all references to this symbol. */
382 unsigned int global_got_area : 2;
383
384 /* True if all GOT relocations against this symbol are for calls. This is
385 a looser condition than no_fn_stub below, because there may be other
386 non-call non-GOT relocations against the symbol. */
387 unsigned int got_only_for_calls : 1;
388
389 /* True if one of the relocations described by possibly_dynamic_relocs
390 is against a readonly section. */
391 unsigned int readonly_reloc : 1;
392
393 /* True if there is a relocation against this symbol that must be
394 resolved by the static linker (in other words, if the relocation
395 cannot possibly be made dynamic). */
396 unsigned int has_static_relocs : 1;
397
398 /* True if we must not create a .MIPS.stubs entry for this symbol.
399 This is set, for example, if there are relocations related to
400 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
401 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
402 unsigned int no_fn_stub : 1;
403
404 /* Whether we need the fn_stub; this is true if this symbol appears
405 in any relocs other than a 16 bit call. */
406 unsigned int need_fn_stub : 1;
407
408 /* True if this symbol is referenced by branch relocations from
409 any non-PIC input file. This is used to determine whether an
410 la25 stub is required. */
411 unsigned int has_nonpic_branches : 1;
412
413 /* Does this symbol need a traditional MIPS lazy-binding stub
414 (as opposed to a PLT entry)? */
415 unsigned int needs_lazy_stub : 1;
416
417 /* Does this symbol resolve to a PLT entry? */
418 unsigned int use_plt_entry : 1;
419 };
420
421 /* MIPS ELF linker hash table. */
422
423 struct mips_elf_link_hash_table
424 {
425 struct elf_link_hash_table root;
426
427 /* The number of .rtproc entries. */
428 bfd_size_type procedure_count;
429
430 /* The size of the .compact_rel section (if SGI_COMPAT). */
431 bfd_size_type compact_rel_size;
432
433 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
434 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
435 bfd_boolean use_rld_obj_head;
436
437 /* The __rld_map or __rld_obj_head symbol. */
438 struct elf_link_hash_entry *rld_symbol;
439
440 /* This is set if we see any mips16 stub sections. */
441 bfd_boolean mips16_stubs_seen;
442
443 /* True if we can generate copy relocs and PLTs. */
444 bfd_boolean use_plts_and_copy_relocs;
445
446 /* True if we can only use 32-bit microMIPS instructions. */
447 bfd_boolean insn32;
448
449 /* True if we suppress checks for invalid branches between ISA modes. */
450 bfd_boolean ignore_branch_isa;
451
452 /* True if we're generating code for VxWorks. */
453 bfd_boolean is_vxworks;
454
455 /* True if we already reported the small-data section overflow. */
456 bfd_boolean small_data_overflow_reported;
457
458 /* True if we use the special `__gnu_absolute_zero' symbol. */
459 bfd_boolean use_absolute_zero;
460
461 /* True if we have been configured for a GNU target. */
462 bfd_boolean gnu_target;
463
464 /* Shortcuts to some dynamic sections, or NULL if they are not
465 being used. */
466 asection *srelplt2;
467 asection *sstubs;
468
469 /* The master GOT information. */
470 struct mips_got_info *got_info;
471
472 /* The global symbol in the GOT with the lowest index in the dynamic
473 symbol table. */
474 struct elf_link_hash_entry *global_gotsym;
475
476 /* The size of the PLT header in bytes. */
477 bfd_vma plt_header_size;
478
479 /* The size of a standard PLT entry in bytes. */
480 bfd_vma plt_mips_entry_size;
481
482 /* The size of a compressed PLT entry in bytes. */
483 bfd_vma plt_comp_entry_size;
484
485 /* The offset of the next standard PLT entry to create. */
486 bfd_vma plt_mips_offset;
487
488 /* The offset of the next compressed PLT entry to create. */
489 bfd_vma plt_comp_offset;
490
491 /* The index of the next .got.plt entry to create. */
492 bfd_vma plt_got_index;
493
494 /* The number of functions that need a lazy-binding stub. */
495 bfd_vma lazy_stub_count;
496
497 /* The size of a function stub entry in bytes. */
498 bfd_vma function_stub_size;
499
500 /* The number of reserved entries at the beginning of the GOT. */
501 unsigned int reserved_gotno;
502
503 /* The section used for mips_elf_la25_stub trampolines.
504 See the comment above that structure for details. */
505 asection *strampoline;
506
507 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
508 pairs. */
509 htab_t la25_stubs;
510
511 /* A function FN (NAME, IS, OS) that creates a new input section
512 called NAME and links it to output section OS. If IS is nonnull,
513 the new section should go immediately before it, otherwise it
514 should go at the (current) beginning of OS.
515
516 The function returns the new section on success, otherwise it
517 returns null. */
518 asection *(*add_stub_section) (const char *, asection *, asection *);
519
520 /* Small local sym cache. */
521 struct sym_cache sym_cache;
522
523 /* Is the PLT header compressed? */
524 unsigned int plt_header_is_comp : 1;
525 };
526
527 /* Get the MIPS ELF linker hash table from a link_info structure. */
528
529 #define mips_elf_hash_table(p) \
530 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
531 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
532
533 /* A structure used to communicate with htab_traverse callbacks. */
534 struct mips_htab_traverse_info
535 {
536 /* The usual link-wide information. */
537 struct bfd_link_info *info;
538 bfd *output_bfd;
539
540 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
541 bfd_boolean error;
542 };
543
544 /* MIPS ELF private object data. */
545
546 struct mips_elf_obj_tdata
547 {
548 /* Generic ELF private object data. */
549 struct elf_obj_tdata root;
550
551 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
552 bfd *abi_fp_bfd;
553
554 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
555 bfd *abi_msa_bfd;
556
557 /* The abiflags for this object. */
558 Elf_Internal_ABIFlags_v0 abiflags;
559 bfd_boolean abiflags_valid;
560
561 /* The GOT requirements of input bfds. */
562 struct mips_got_info *got;
563
564 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
565 included directly in this one, but there's no point to wasting
566 the memory just for the infrequently called find_nearest_line. */
567 struct mips_elf_find_line *find_line_info;
568
569 /* An array of stub sections indexed by symbol number. */
570 asection **local_stubs;
571 asection **local_call_stubs;
572
573 /* The Irix 5 support uses two virtual sections, which represent
574 text/data symbols defined in dynamic objects. */
575 asymbol *elf_data_symbol;
576 asymbol *elf_text_symbol;
577 asection *elf_data_section;
578 asection *elf_text_section;
579 };
580
581 /* Get MIPS ELF private object data from BFD's tdata. */
582
583 #define mips_elf_tdata(bfd) \
584 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
585
586 #define TLS_RELOC_P(r_type) \
587 (r_type == R_MIPS_TLS_DTPMOD32 \
588 || r_type == R_MIPS_TLS_DTPMOD64 \
589 || r_type == R_MIPS_TLS_DTPREL32 \
590 || r_type == R_MIPS_TLS_DTPREL64 \
591 || r_type == R_MIPS_TLS_GD \
592 || r_type == R_MIPS_TLS_LDM \
593 || r_type == R_MIPS_TLS_DTPREL_HI16 \
594 || r_type == R_MIPS_TLS_DTPREL_LO16 \
595 || r_type == R_MIPS_TLS_GOTTPREL \
596 || r_type == R_MIPS_TLS_TPREL32 \
597 || r_type == R_MIPS_TLS_TPREL64 \
598 || r_type == R_MIPS_TLS_TPREL_HI16 \
599 || r_type == R_MIPS_TLS_TPREL_LO16 \
600 || r_type == R_MIPS16_TLS_GD \
601 || r_type == R_MIPS16_TLS_LDM \
602 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
603 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
604 || r_type == R_MIPS16_TLS_GOTTPREL \
605 || r_type == R_MIPS16_TLS_TPREL_HI16 \
606 || r_type == R_MIPS16_TLS_TPREL_LO16 \
607 || r_type == R_MICROMIPS_TLS_GD \
608 || r_type == R_MICROMIPS_TLS_LDM \
609 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
610 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
611 || r_type == R_MICROMIPS_TLS_GOTTPREL \
612 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
613 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
614
615 /* Structure used to pass information to mips_elf_output_extsym. */
616
617 struct extsym_info
618 {
619 bfd *abfd;
620 struct bfd_link_info *info;
621 struct ecoff_debug_info *debug;
622 const struct ecoff_debug_swap *swap;
623 bfd_boolean failed;
624 };
625
626 /* The names of the runtime procedure table symbols used on IRIX5. */
627
628 static const char * const mips_elf_dynsym_rtproc_names[] =
629 {
630 "_procedure_table",
631 "_procedure_string_table",
632 "_procedure_table_size",
633 NULL
634 };
635
636 /* These structures are used to generate the .compact_rel section on
637 IRIX5. */
638
639 typedef struct
640 {
641 unsigned long id1; /* Always one? */
642 unsigned long num; /* Number of compact relocation entries. */
643 unsigned long id2; /* Always two? */
644 unsigned long offset; /* The file offset of the first relocation. */
645 unsigned long reserved0; /* Zero? */
646 unsigned long reserved1; /* Zero? */
647 } Elf32_compact_rel;
648
649 typedef struct
650 {
651 bfd_byte id1[4];
652 bfd_byte num[4];
653 bfd_byte id2[4];
654 bfd_byte offset[4];
655 bfd_byte reserved0[4];
656 bfd_byte reserved1[4];
657 } Elf32_External_compact_rel;
658
659 typedef struct
660 {
661 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
662 unsigned int rtype : 4; /* Relocation types. See below. */
663 unsigned int dist2to : 8;
664 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
665 unsigned long konst; /* KONST field. See below. */
666 unsigned long vaddr; /* VADDR to be relocated. */
667 } Elf32_crinfo;
668
669 typedef struct
670 {
671 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
672 unsigned int rtype : 4; /* Relocation types. See below. */
673 unsigned int dist2to : 8;
674 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
675 unsigned long konst; /* KONST field. See below. */
676 } Elf32_crinfo2;
677
678 typedef struct
679 {
680 bfd_byte info[4];
681 bfd_byte konst[4];
682 bfd_byte vaddr[4];
683 } Elf32_External_crinfo;
684
685 typedef struct
686 {
687 bfd_byte info[4];
688 bfd_byte konst[4];
689 } Elf32_External_crinfo2;
690
691 /* These are the constants used to swap the bitfields in a crinfo. */
692
693 #define CRINFO_CTYPE (0x1)
694 #define CRINFO_CTYPE_SH (31)
695 #define CRINFO_RTYPE (0xf)
696 #define CRINFO_RTYPE_SH (27)
697 #define CRINFO_DIST2TO (0xff)
698 #define CRINFO_DIST2TO_SH (19)
699 #define CRINFO_RELVADDR (0x7ffff)
700 #define CRINFO_RELVADDR_SH (0)
701
702 /* A compact relocation info has long (3 words) or short (2 words)
703 formats. A short format doesn't have VADDR field and relvaddr
704 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
705 #define CRF_MIPS_LONG 1
706 #define CRF_MIPS_SHORT 0
707
708 /* There are 4 types of compact relocation at least. The value KONST
709 has different meaning for each type:
710
711 (type) (konst)
712 CT_MIPS_REL32 Address in data
713 CT_MIPS_WORD Address in word (XXX)
714 CT_MIPS_GPHI_LO GP - vaddr
715 CT_MIPS_JMPAD Address to jump
716 */
717
718 #define CRT_MIPS_REL32 0xa
719 #define CRT_MIPS_WORD 0xb
720 #define CRT_MIPS_GPHI_LO 0xc
721 #define CRT_MIPS_JMPAD 0xd
722
723 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
724 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
725 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
726 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
727
728 /* The structure of the runtime procedure descriptor created by the
730 loader for use by the static exception system. */
731
732 typedef struct runtime_pdr {
733 bfd_vma adr; /* Memory address of start of procedure. */
734 long regmask; /* Save register mask. */
735 long regoffset; /* Save register offset. */
736 long fregmask; /* Save floating point register mask. */
737 long fregoffset; /* Save floating point register offset. */
738 long frameoffset; /* Frame size. */
739 short framereg; /* Frame pointer register. */
740 short pcreg; /* Offset or reg of return pc. */
741 long irpss; /* Index into the runtime string table. */
742 long reserved;
743 struct exception_info *exception_info;/* Pointer to exception array. */
744 } RPDR, *pRPDR;
745 #define cbRPDR sizeof (RPDR)
746 #define rpdNil ((pRPDR) 0)
747
748 static struct mips_got_entry *mips_elf_create_local_got_entry
750 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
751 struct mips_elf_link_hash_entry *, int);
752 static bfd_boolean mips_elf_sort_hash_table_f
753 (struct mips_elf_link_hash_entry *, void *);
754 static bfd_vma mips_elf_high
755 (bfd_vma);
756 static bfd_boolean mips_elf_create_dynamic_relocation
757 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
758 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
759 bfd_vma *, asection *);
760 static bfd_vma mips_elf_adjust_gp
761 (bfd *, struct mips_got_info *, bfd *);
762
763 /* This will be used when we sort the dynamic relocation records. */
764 static bfd *reldyn_sorting_bfd;
765
766 /* True if ABFD is for CPUs with load interlocking that include
767 non-MIPS1 CPUs and R3900. */
768 #define LOAD_INTERLOCKS_P(abfd) \
769 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
770 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
771
772 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
773 This should be safe for all architectures. We enable this predicate
774 for RM9000 for now. */
775 #define JAL_TO_BAL_P(abfd) \
776 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
777
778 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
779 This should be safe for all architectures. We enable this predicate for
780 all CPUs. */
781 #define JALR_TO_BAL_P(abfd) 1
782
783 /* True if ABFD is for CPUs that are faster if JR is converted to B.
784 This should be safe for all architectures. We enable this predicate for
785 all CPUs. */
786 #define JR_TO_B_P(abfd) 1
787
788 /* True if ABFD is a PIC object. */
789 #define PIC_OBJECT_P(abfd) \
790 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
791
792 /* Nonzero if ABFD is using the O32 ABI. */
793 #define ABI_O32_P(abfd) \
794 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
795
796 /* Nonzero if ABFD is using the N32 ABI. */
797 #define ABI_N32_P(abfd) \
798 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
799
800 /* Nonzero if ABFD is using the N64 ABI. */
801 #define ABI_64_P(abfd) \
802 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
803
804 /* Nonzero if ABFD is using NewABI conventions. */
805 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
806
807 /* Nonzero if ABFD has microMIPS code. */
808 #define MICROMIPS_P(abfd) \
809 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
810
811 /* Nonzero if ABFD is MIPS R6. */
812 #define MIPSR6_P(abfd) \
813 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
814 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
815
816 /* The IRIX compatibility level we are striving for. */
817 #define IRIX_COMPAT(abfd) \
818 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
819
820 /* Whether we are trying to be compatible with IRIX at all. */
821 #define SGI_COMPAT(abfd) \
822 (IRIX_COMPAT (abfd) != ict_none)
823
824 /* The name of the options section. */
825 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
826 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
827
828 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
829 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
830 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
831 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
832
833 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
834 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
835 (strcmp (NAME, ".MIPS.abiflags") == 0)
836
837 /* Whether the section is readonly. */
838 #define MIPS_ELF_READONLY_SECTION(sec) \
839 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
840 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
841
842 /* The name of the stub section. */
843 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
844
845 /* The size of an external REL relocation. */
846 #define MIPS_ELF_REL_SIZE(abfd) \
847 (get_elf_backend_data (abfd)->s->sizeof_rel)
848
849 /* The size of an external RELA relocation. */
850 #define MIPS_ELF_RELA_SIZE(abfd) \
851 (get_elf_backend_data (abfd)->s->sizeof_rela)
852
853 /* The size of an external dynamic table entry. */
854 #define MIPS_ELF_DYN_SIZE(abfd) \
855 (get_elf_backend_data (abfd)->s->sizeof_dyn)
856
857 /* The size of a GOT entry. */
858 #define MIPS_ELF_GOT_SIZE(abfd) \
859 (get_elf_backend_data (abfd)->s->arch_size / 8)
860
861 /* The size of the .rld_map section. */
862 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
863 (get_elf_backend_data (abfd)->s->arch_size / 8)
864
865 /* The size of a symbol-table entry. */
866 #define MIPS_ELF_SYM_SIZE(abfd) \
867 (get_elf_backend_data (abfd)->s->sizeof_sym)
868
869 /* The default alignment for sections, as a power of two. */
870 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
871 (get_elf_backend_data (abfd)->s->log_file_align)
872
873 /* Get word-sized data. */
874 #define MIPS_ELF_GET_WORD(abfd, ptr) \
875 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
876
877 /* Put out word-sized data. */
878 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
879 (ABI_64_P (abfd) \
880 ? bfd_put_64 (abfd, val, ptr) \
881 : bfd_put_32 (abfd, val, ptr))
882
883 /* The opcode for word-sized loads (LW or LD). */
884 #define MIPS_ELF_LOAD_WORD(abfd) \
885 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
886
887 /* Add a dynamic symbol table-entry. */
888 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
889 _bfd_elf_add_dynamic_entry (info, tag, val)
890
891 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
892 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
893
894 /* The name of the dynamic relocation section. */
895 #define MIPS_ELF_REL_DYN_NAME(INFO) \
896 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
897
898 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
899 from smaller values. Start with zero, widen, *then* decrement. */
900 #define MINUS_ONE (((bfd_vma)0) - 1)
901 #define MINUS_TWO (((bfd_vma)0) - 2)
902
903 /* The value to write into got[1] for SVR4 targets, to identify it is
904 a GNU object. The dynamic linker can then use got[1] to store the
905 module pointer. */
906 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
907 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
908
909 /* The offset of $gp from the beginning of the .got section. */
910 #define ELF_MIPS_GP_OFFSET(INFO) \
911 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
912
913 /* The maximum size of the GOT for it to be addressable using 16-bit
914 offsets from $gp. */
915 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
916
917 /* Instructions which appear in a stub. */
918 #define STUB_LW(abfd) \
919 ((ABI_64_P (abfd) \
920 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
921 : 0x8f998010)) /* lw t9,0x8010(gp) */
922 #define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
923 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
924 #define STUB_JALR 0x0320f809 /* jalr ra,t9 */
925 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
926 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
927 #define STUB_LI16S(abfd, VAL) \
928 ((ABI_64_P (abfd) \
929 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
930 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
931
932 /* Likewise for the microMIPS ASE. */
933 #define STUB_LW_MICROMIPS(abfd) \
934 (ABI_64_P (abfd) \
935 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
936 : 0xff3c8010) /* lw t9,0x8010(gp) */
937 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
938 #define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
939 #define STUB_LUI_MICROMIPS(VAL) \
940 (0x41b80000 + (VAL)) /* lui t8,VAL */
941 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
942 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
943 #define STUB_ORI_MICROMIPS(VAL) \
944 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
945 #define STUB_LI16U_MICROMIPS(VAL) \
946 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
947 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
948 (ABI_64_P (abfd) \
949 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
950 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
951
952 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
953 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
954 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
955 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
956 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
957 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
958
959 /* The name of the dynamic interpreter. This is put in the .interp
960 section. */
961
962 #define ELF_DYNAMIC_INTERPRETER(abfd) \
963 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
964 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
965 : "/usr/lib/libc.so.1")
966
967 #ifdef BFD64
968 #define MNAME(bfd,pre,pos) \
969 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
970 #define ELF_R_SYM(bfd, i) \
971 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
972 #define ELF_R_TYPE(bfd, i) \
973 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
974 #define ELF_R_INFO(bfd, s, t) \
975 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
976 #else
977 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
978 #define ELF_R_SYM(bfd, i) \
979 (ELF32_R_SYM (i))
980 #define ELF_R_TYPE(bfd, i) \
981 (ELF32_R_TYPE (i))
982 #define ELF_R_INFO(bfd, s, t) \
983 (ELF32_R_INFO (s, t))
984 #endif
985
986 /* The mips16 compiler uses a couple of special sections to handle
988 floating point arguments.
989
990 Section names that look like .mips16.fn.FNNAME contain stubs that
991 copy floating point arguments from the fp regs to the gp regs and
992 then jump to FNNAME. If any 32 bit function calls FNNAME, the
993 call should be redirected to the stub instead. If no 32 bit
994 function calls FNNAME, the stub should be discarded. We need to
995 consider any reference to the function, not just a call, because
996 if the address of the function is taken we will need the stub,
997 since the address might be passed to a 32 bit function.
998
999 Section names that look like .mips16.call.FNNAME contain stubs
1000 that copy floating point arguments from the gp regs to the fp
1001 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1002 then any 16 bit function that calls FNNAME should be redirected
1003 to the stub instead. If FNNAME is not a 32 bit function, the
1004 stub should be discarded.
1005
1006 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1007 which call FNNAME and then copy the return value from the fp regs
1008 to the gp regs. These stubs store the return value in $18 while
1009 calling FNNAME; any function which might call one of these stubs
1010 must arrange to save $18 around the call. (This case is not
1011 needed for 32 bit functions that call 16 bit functions, because
1012 16 bit functions always return floating point values in both
1013 $f0/$f1 and $2/$3.)
1014
1015 Note that in all cases FNNAME might be defined statically.
1016 Therefore, FNNAME is not used literally. Instead, the relocation
1017 information will indicate which symbol the section is for.
1018
1019 We record any stubs that we find in the symbol table. */
1020
1021 #define FN_STUB ".mips16.fn."
1022 #define CALL_STUB ".mips16.call."
1023 #define CALL_FP_STUB ".mips16.call.fp."
1024
1025 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1026 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1027 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
1028
1029 /* The format of the first PLT entry in an O32 executable. */
1031 static const bfd_vma mips_o32_exec_plt0_entry[] =
1032 {
1033 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1034 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1035 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1036 0x031cc023, /* subu $24, $24, $28 */
1037 0x03e07825, /* or t7, ra, zero */
1038 0x0018c082, /* srl $24, $24, 2 */
1039 0x0320f809, /* jalr $25 */
1040 0x2718fffe /* subu $24, $24, 2 */
1041 };
1042
1043 /* The format of the first PLT entry in an N32 executable. Different
1044 because gp ($28) is not available; we use t2 ($14) instead. */
1045 static const bfd_vma mips_n32_exec_plt0_entry[] =
1046 {
1047 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1048 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1049 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1050 0x030ec023, /* subu $24, $24, $14 */
1051 0x03e07825, /* or t7, ra, zero */
1052 0x0018c082, /* srl $24, $24, 2 */
1053 0x0320f809, /* jalr $25 */
1054 0x2718fffe /* subu $24, $24, 2 */
1055 };
1056
1057 /* The format of the first PLT entry in an N64 executable. Different
1058 from N32 because of the increased size of GOT entries. */
1059 static const bfd_vma mips_n64_exec_plt0_entry[] =
1060 {
1061 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1062 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1063 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1064 0x030ec023, /* subu $24, $24, $14 */
1065 0x03e07825, /* or t7, ra, zero */
1066 0x0018c0c2, /* srl $24, $24, 3 */
1067 0x0320f809, /* jalr $25 */
1068 0x2718fffe /* subu $24, $24, 2 */
1069 };
1070
1071 /* The format of the microMIPS first PLT entry in an O32 executable.
1072 We rely on v0 ($2) rather than t8 ($24) to contain the address
1073 of the GOTPLT entry handled, so this stub may only be used when
1074 all the subsequent PLT entries are microMIPS code too.
1075
1076 The trailing NOP is for alignment and correct disassembly only. */
1077 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1078 {
1079 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1080 0xff23, 0x0000, /* lw $25, 0($3) */
1081 0x0535, /* subu $2, $2, $3 */
1082 0x2525, /* srl $2, $2, 2 */
1083 0x3302, 0xfffe, /* subu $24, $2, 2 */
1084 0x0dff, /* move $15, $31 */
1085 0x45f9, /* jalrs $25 */
1086 0x0f83, /* move $28, $3 */
1087 0x0c00 /* nop */
1088 };
1089
1090 /* The format of the microMIPS first PLT entry in an O32 executable
1091 in the insn32 mode. */
1092 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1093 {
1094 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1095 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1096 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1097 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1098 0x001f, 0x7a90, /* or $15, $31, zero */
1099 0x0318, 0x1040, /* srl $24, $24, 2 */
1100 0x03f9, 0x0f3c, /* jalr $25 */
1101 0x3318, 0xfffe /* subu $24, $24, 2 */
1102 };
1103
1104 /* The format of subsequent standard PLT entries. */
1105 static const bfd_vma mips_exec_plt_entry[] =
1106 {
1107 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1108 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1109 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1110 0x03200008 /* jr $25 */
1111 };
1112
1113 /* In the following PLT entry the JR and ADDIU instructions will
1114 be swapped in _bfd_mips_elf_finish_dynamic_symbol because
1115 LOAD_INTERLOCKS_P will be true for MIPS R6. */
1116 static const bfd_vma mipsr6_exec_plt_entry[] =
1117 {
1118 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1119 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1120 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1121 0x03200009 /* jr $25 */
1122 };
1123
1124 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1125 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1126 directly addressable. */
1127 static const bfd_vma mips16_o32_exec_plt_entry[] =
1128 {
1129 0xb203, /* lw $2, 12($pc) */
1130 0x9a60, /* lw $3, 0($2) */
1131 0x651a, /* move $24, $2 */
1132 0xeb00, /* jr $3 */
1133 0x653b, /* move $25, $3 */
1134 0x6500, /* nop */
1135 0x0000, 0x0000 /* .word (.got.plt entry) */
1136 };
1137
1138 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1139 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1140 static const bfd_vma micromips_o32_exec_plt_entry[] =
1141 {
1142 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1143 0xff22, 0x0000, /* lw $25, 0($2) */
1144 0x4599, /* jr $25 */
1145 0x0f02 /* move $24, $2 */
1146 };
1147
1148 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1149 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1150 {
1151 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1152 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1153 0x0019, 0x0f3c, /* jr $25 */
1154 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1155 };
1156
1157 /* The format of the first PLT entry in a VxWorks executable. */
1158 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1159 {
1160 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1161 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1162 0x8f390008, /* lw t9, 8(t9) */
1163 0x00000000, /* nop */
1164 0x03200008, /* jr t9 */
1165 0x00000000 /* nop */
1166 };
1167
1168 /* The format of subsequent PLT entries. */
1169 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1170 {
1171 0x10000000, /* b .PLT_resolver */
1172 0x24180000, /* li t8, <pltindex> */
1173 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1174 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1175 0x8f390000, /* lw t9, 0(t9) */
1176 0x00000000, /* nop */
1177 0x03200008, /* jr t9 */
1178 0x00000000 /* nop */
1179 };
1180
1181 /* The format of the first PLT entry in a VxWorks shared object. */
1182 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1183 {
1184 0x8f990008, /* lw t9, 8(gp) */
1185 0x00000000, /* nop */
1186 0x03200008, /* jr t9 */
1187 0x00000000, /* nop */
1188 0x00000000, /* nop */
1189 0x00000000 /* nop */
1190 };
1191
1192 /* The format of subsequent PLT entries. */
1193 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1194 {
1195 0x10000000, /* b .PLT_resolver */
1196 0x24180000 /* li t8, <pltindex> */
1197 };
1198
1199 /* microMIPS 32-bit opcode helper installer. */
1201
1202 static void
1203 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1204 {
1205 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1206 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1207 }
1208
1209 /* microMIPS 32-bit opcode helper retriever. */
1210
1211 static bfd_vma
1212 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1213 {
1214 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1215 }
1216
1217 /* Look up an entry in a MIPS ELF linker hash table. */
1219
1220 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1221 ((struct mips_elf_link_hash_entry *) \
1222 elf_link_hash_lookup (&(table)->root, (string), (create), \
1223 (copy), (follow)))
1224
1225 /* Traverse a MIPS ELF linker hash table. */
1226
1227 #define mips_elf_link_hash_traverse(table, func, info) \
1228 (elf_link_hash_traverse \
1229 (&(table)->root, \
1230 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1231 (info)))
1232
1233 /* Find the base offsets for thread-local storage in this object,
1234 for GD/LD and IE/LE respectively. */
1235
1236 #define TP_OFFSET 0x7000
1237 #define DTP_OFFSET 0x8000
1238
1239 static bfd_vma
1240 dtprel_base (struct bfd_link_info *info)
1241 {
1242 /* If tls_sec is NULL, we should have signalled an error already. */
1243 if (elf_hash_table (info)->tls_sec == NULL)
1244 return 0;
1245 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1246 }
1247
1248 static bfd_vma
1249 tprel_base (struct bfd_link_info *info)
1250 {
1251 /* If tls_sec is NULL, we should have signalled an error already. */
1252 if (elf_hash_table (info)->tls_sec == NULL)
1253 return 0;
1254 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1255 }
1256
1257 /* Create an entry in a MIPS ELF linker hash table. */
1258
1259 static struct bfd_hash_entry *
1260 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1261 struct bfd_hash_table *table, const char *string)
1262 {
1263 struct mips_elf_link_hash_entry *ret =
1264 (struct mips_elf_link_hash_entry *) entry;
1265
1266 /* Allocate the structure if it has not already been allocated by a
1267 subclass. */
1268 if (ret == NULL)
1269 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1270 if (ret == NULL)
1271 return (struct bfd_hash_entry *) ret;
1272
1273 /* Call the allocation method of the superclass. */
1274 ret = ((struct mips_elf_link_hash_entry *)
1275 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1276 table, string));
1277 if (ret != NULL)
1278 {
1279 /* Set local fields. */
1280 memset (&ret->esym, 0, sizeof (EXTR));
1281 /* We use -2 as a marker to indicate that the information has
1282 not been set. -1 means there is no associated ifd. */
1283 ret->esym.ifd = -2;
1284 ret->la25_stub = 0;
1285 ret->possibly_dynamic_relocs = 0;
1286 ret->fn_stub = NULL;
1287 ret->call_stub = NULL;
1288 ret->call_fp_stub = NULL;
1289 ret->global_got_area = GGA_NONE;
1290 ret->got_only_for_calls = TRUE;
1291 ret->readonly_reloc = FALSE;
1292 ret->has_static_relocs = FALSE;
1293 ret->no_fn_stub = FALSE;
1294 ret->need_fn_stub = FALSE;
1295 ret->has_nonpic_branches = FALSE;
1296 ret->needs_lazy_stub = FALSE;
1297 ret->use_plt_entry = FALSE;
1298 }
1299
1300 return (struct bfd_hash_entry *) ret;
1301 }
1302
1303 /* Allocate MIPS ELF private object data. */
1304
1305 bfd_boolean
1306 _bfd_mips_elf_mkobject (bfd *abfd)
1307 {
1308 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1309 MIPS_ELF_DATA);
1310 }
1311
1312 bfd_boolean
1313 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1314 {
1315 if (!sec->used_by_bfd)
1316 {
1317 struct _mips_elf_section_data *sdata;
1318 bfd_size_type amt = sizeof (*sdata);
1319
1320 sdata = bfd_zalloc (abfd, amt);
1321 if (sdata == NULL)
1322 return FALSE;
1323 sec->used_by_bfd = sdata;
1324 }
1325
1326 return _bfd_elf_new_section_hook (abfd, sec);
1327 }
1328
1329 /* Read ECOFF debugging information from a .mdebug section into a
1331 ecoff_debug_info structure. */
1332
1333 bfd_boolean
1334 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1335 struct ecoff_debug_info *debug)
1336 {
1337 HDRR *symhdr;
1338 const struct ecoff_debug_swap *swap;
1339 char *ext_hdr;
1340
1341 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1342 memset (debug, 0, sizeof (*debug));
1343
1344 ext_hdr = bfd_malloc (swap->external_hdr_size);
1345 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1346 goto error_return;
1347
1348 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1349 swap->external_hdr_size))
1350 goto error_return;
1351
1352 symhdr = &debug->symbolic_header;
1353 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1354
1355 /* The symbolic header contains absolute file offsets and sizes to
1356 read. */
1357 #define READ(ptr, offset, count, size, type) \
1358 if (symhdr->count == 0) \
1359 debug->ptr = NULL; \
1360 else \
1361 { \
1362 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1363 debug->ptr = bfd_malloc (amt); \
1364 if (debug->ptr == NULL) \
1365 goto error_return; \
1366 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1367 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1368 goto error_return; \
1369 }
1370
1371 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1372 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1373 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1374 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1375 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1376 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1377 union aux_ext *);
1378 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1379 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1380 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1381 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1382 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1383 #undef READ
1384
1385 debug->fdr = NULL;
1386
1387 return TRUE;
1388
1389 error_return:
1390 if (ext_hdr != NULL)
1391 free (ext_hdr);
1392 if (debug->line != NULL)
1393 free (debug->line);
1394 if (debug->external_dnr != NULL)
1395 free (debug->external_dnr);
1396 if (debug->external_pdr != NULL)
1397 free (debug->external_pdr);
1398 if (debug->external_sym != NULL)
1399 free (debug->external_sym);
1400 if (debug->external_opt != NULL)
1401 free (debug->external_opt);
1402 if (debug->external_aux != NULL)
1403 free (debug->external_aux);
1404 if (debug->ss != NULL)
1405 free (debug->ss);
1406 if (debug->ssext != NULL)
1407 free (debug->ssext);
1408 if (debug->external_fdr != NULL)
1409 free (debug->external_fdr);
1410 if (debug->external_rfd != NULL)
1411 free (debug->external_rfd);
1412 if (debug->external_ext != NULL)
1413 free (debug->external_ext);
1414 return FALSE;
1415 }
1416
1417 /* Swap RPDR (runtime procedure table entry) for output. */
1419
1420 static void
1421 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1422 {
1423 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1424 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1425 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1426 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1427 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1428 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1429
1430 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1431 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1432
1433 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1434 }
1435
1436 /* Create a runtime procedure table from the .mdebug section. */
1437
1438 static bfd_boolean
1439 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1440 struct bfd_link_info *info, asection *s,
1441 struct ecoff_debug_info *debug)
1442 {
1443 const struct ecoff_debug_swap *swap;
1444 HDRR *hdr = &debug->symbolic_header;
1445 RPDR *rpdr, *rp;
1446 struct rpdr_ext *erp;
1447 void *rtproc;
1448 struct pdr_ext *epdr;
1449 struct sym_ext *esym;
1450 char *ss, **sv;
1451 char *str;
1452 bfd_size_type size;
1453 bfd_size_type count;
1454 unsigned long sindex;
1455 unsigned long i;
1456 PDR pdr;
1457 SYMR sym;
1458 const char *no_name_func = _("static procedure (no name)");
1459
1460 epdr = NULL;
1461 rpdr = NULL;
1462 esym = NULL;
1463 ss = NULL;
1464 sv = NULL;
1465
1466 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1467
1468 sindex = strlen (no_name_func) + 1;
1469 count = hdr->ipdMax;
1470 if (count > 0)
1471 {
1472 size = swap->external_pdr_size;
1473
1474 epdr = bfd_malloc (size * count);
1475 if (epdr == NULL)
1476 goto error_return;
1477
1478 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1479 goto error_return;
1480
1481 size = sizeof (RPDR);
1482 rp = rpdr = bfd_malloc (size * count);
1483 if (rpdr == NULL)
1484 goto error_return;
1485
1486 size = sizeof (char *);
1487 sv = bfd_malloc (size * count);
1488 if (sv == NULL)
1489 goto error_return;
1490
1491 count = hdr->isymMax;
1492 size = swap->external_sym_size;
1493 esym = bfd_malloc (size * count);
1494 if (esym == NULL)
1495 goto error_return;
1496
1497 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1498 goto error_return;
1499
1500 count = hdr->issMax;
1501 ss = bfd_malloc (count);
1502 if (ss == NULL)
1503 goto error_return;
1504 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1505 goto error_return;
1506
1507 count = hdr->ipdMax;
1508 for (i = 0; i < (unsigned long) count; i++, rp++)
1509 {
1510 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1511 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1512 rp->adr = sym.value;
1513 rp->regmask = pdr.regmask;
1514 rp->regoffset = pdr.regoffset;
1515 rp->fregmask = pdr.fregmask;
1516 rp->fregoffset = pdr.fregoffset;
1517 rp->frameoffset = pdr.frameoffset;
1518 rp->framereg = pdr.framereg;
1519 rp->pcreg = pdr.pcreg;
1520 rp->irpss = sindex;
1521 sv[i] = ss + sym.iss;
1522 sindex += strlen (sv[i]) + 1;
1523 }
1524 }
1525
1526 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1527 size = BFD_ALIGN (size, 16);
1528 rtproc = bfd_alloc (abfd, size);
1529 if (rtproc == NULL)
1530 {
1531 mips_elf_hash_table (info)->procedure_count = 0;
1532 goto error_return;
1533 }
1534
1535 mips_elf_hash_table (info)->procedure_count = count + 2;
1536
1537 erp = rtproc;
1538 memset (erp, 0, sizeof (struct rpdr_ext));
1539 erp++;
1540 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1541 strcpy (str, no_name_func);
1542 str += strlen (no_name_func) + 1;
1543 for (i = 0; i < count; i++)
1544 {
1545 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1546 strcpy (str, sv[i]);
1547 str += strlen (sv[i]) + 1;
1548 }
1549 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1550
1551 /* Set the size and contents of .rtproc section. */
1552 s->size = size;
1553 s->contents = rtproc;
1554
1555 /* Skip this section later on (I don't think this currently
1556 matters, but someday it might). */
1557 s->map_head.link_order = NULL;
1558
1559 if (epdr != NULL)
1560 free (epdr);
1561 if (rpdr != NULL)
1562 free (rpdr);
1563 if (esym != NULL)
1564 free (esym);
1565 if (ss != NULL)
1566 free (ss);
1567 if (sv != NULL)
1568 free (sv);
1569
1570 return TRUE;
1571
1572 error_return:
1573 if (epdr != NULL)
1574 free (epdr);
1575 if (rpdr != NULL)
1576 free (rpdr);
1577 if (esym != NULL)
1578 free (esym);
1579 if (ss != NULL)
1580 free (ss);
1581 if (sv != NULL)
1582 free (sv);
1583 return FALSE;
1584 }
1585
1586 /* We're going to create a stub for H. Create a symbol for the stub's
1588 value and size, to help make the disassembly easier to read. */
1589
1590 static bfd_boolean
1591 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1592 struct mips_elf_link_hash_entry *h,
1593 const char *prefix, asection *s, bfd_vma value,
1594 bfd_vma size)
1595 {
1596 bfd_boolean micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1597 struct bfd_link_hash_entry *bh;
1598 struct elf_link_hash_entry *elfh;
1599 char *name;
1600 bfd_boolean res;
1601
1602 if (micromips_p)
1603 value |= 1;
1604
1605 /* Create a new symbol. */
1606 name = concat (prefix, h->root.root.root.string, NULL);
1607 bh = NULL;
1608 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1609 BSF_LOCAL, s, value, NULL,
1610 TRUE, FALSE, &bh);
1611 free (name);
1612 if (! res)
1613 return FALSE;
1614
1615 /* Make it a local function. */
1616 elfh = (struct elf_link_hash_entry *) bh;
1617 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1618 elfh->size = size;
1619 elfh->forced_local = 1;
1620 if (micromips_p)
1621 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1622 return TRUE;
1623 }
1624
1625 /* We're about to redefine H. Create a symbol to represent H's
1626 current value and size, to help make the disassembly easier
1627 to read. */
1628
1629 static bfd_boolean
1630 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1631 struct mips_elf_link_hash_entry *h,
1632 const char *prefix)
1633 {
1634 struct bfd_link_hash_entry *bh;
1635 struct elf_link_hash_entry *elfh;
1636 char *name;
1637 asection *s;
1638 bfd_vma value;
1639 bfd_boolean res;
1640
1641 /* Read the symbol's value. */
1642 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1643 || h->root.root.type == bfd_link_hash_defweak);
1644 s = h->root.root.u.def.section;
1645 value = h->root.root.u.def.value;
1646
1647 /* Create a new symbol. */
1648 name = concat (prefix, h->root.root.root.string, NULL);
1649 bh = NULL;
1650 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1651 BSF_LOCAL, s, value, NULL,
1652 TRUE, FALSE, &bh);
1653 free (name);
1654 if (! res)
1655 return FALSE;
1656
1657 /* Make it local and copy the other attributes from H. */
1658 elfh = (struct elf_link_hash_entry *) bh;
1659 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1660 elfh->other = h->root.other;
1661 elfh->size = h->root.size;
1662 elfh->forced_local = 1;
1663 return TRUE;
1664 }
1665
1666 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1667 function rather than to a hard-float stub. */
1668
1669 static bfd_boolean
1670 section_allows_mips16_refs_p (asection *section)
1671 {
1672 const char *name;
1673
1674 name = bfd_get_section_name (section->owner, section);
1675 return (FN_STUB_P (name)
1676 || CALL_STUB_P (name)
1677 || CALL_FP_STUB_P (name)
1678 || strcmp (name, ".pdr") == 0);
1679 }
1680
1681 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1682 stub section of some kind. Return the R_SYMNDX of the target
1683 function, or 0 if we can't decide which function that is. */
1684
1685 static unsigned long
1686 mips16_stub_symndx (const struct elf_backend_data *bed,
1687 asection *sec ATTRIBUTE_UNUSED,
1688 const Elf_Internal_Rela *relocs,
1689 const Elf_Internal_Rela *relend)
1690 {
1691 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1692 const Elf_Internal_Rela *rel;
1693
1694 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1695 one in a compound relocation. */
1696 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1697 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1698 return ELF_R_SYM (sec->owner, rel->r_info);
1699
1700 /* Otherwise trust the first relocation, whatever its kind. This is
1701 the traditional behavior. */
1702 if (relocs < relend)
1703 return ELF_R_SYM (sec->owner, relocs->r_info);
1704
1705 return 0;
1706 }
1707
1708 /* Check the mips16 stubs for a particular symbol, and see if we can
1709 discard them. */
1710
1711 static void
1712 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1713 struct mips_elf_link_hash_entry *h)
1714 {
1715 /* Dynamic symbols must use the standard call interface, in case other
1716 objects try to call them. */
1717 if (h->fn_stub != NULL
1718 && h->root.dynindx != -1)
1719 {
1720 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1721 h->need_fn_stub = TRUE;
1722 }
1723
1724 if (h->fn_stub != NULL
1725 && ! h->need_fn_stub)
1726 {
1727 /* We don't need the fn_stub; the only references to this symbol
1728 are 16 bit calls. Clobber the size to 0 to prevent it from
1729 being included in the link. */
1730 h->fn_stub->size = 0;
1731 h->fn_stub->flags &= ~SEC_RELOC;
1732 h->fn_stub->reloc_count = 0;
1733 h->fn_stub->flags |= SEC_EXCLUDE;
1734 h->fn_stub->output_section = bfd_abs_section_ptr;
1735 }
1736
1737 if (h->call_stub != NULL
1738 && ELF_ST_IS_MIPS16 (h->root.other))
1739 {
1740 /* We don't need the call_stub; this is a 16 bit function, so
1741 calls from other 16 bit functions are OK. Clobber the size
1742 to 0 to prevent it from being included in the link. */
1743 h->call_stub->size = 0;
1744 h->call_stub->flags &= ~SEC_RELOC;
1745 h->call_stub->reloc_count = 0;
1746 h->call_stub->flags |= SEC_EXCLUDE;
1747 h->call_stub->output_section = bfd_abs_section_ptr;
1748 }
1749
1750 if (h->call_fp_stub != NULL
1751 && ELF_ST_IS_MIPS16 (h->root.other))
1752 {
1753 /* We don't need the call_stub; this is a 16 bit function, so
1754 calls from other 16 bit functions are OK. Clobber the size
1755 to 0 to prevent it from being included in the link. */
1756 h->call_fp_stub->size = 0;
1757 h->call_fp_stub->flags &= ~SEC_RELOC;
1758 h->call_fp_stub->reloc_count = 0;
1759 h->call_fp_stub->flags |= SEC_EXCLUDE;
1760 h->call_fp_stub->output_section = bfd_abs_section_ptr;
1761 }
1762 }
1763
1764 /* Hashtable callbacks for mips_elf_la25_stubs. */
1765
1766 static hashval_t
1767 mips_elf_la25_stub_hash (const void *entry_)
1768 {
1769 const struct mips_elf_la25_stub *entry;
1770
1771 entry = (struct mips_elf_la25_stub *) entry_;
1772 return entry->h->root.root.u.def.section->id
1773 + entry->h->root.root.u.def.value;
1774 }
1775
1776 static int
1777 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1778 {
1779 const struct mips_elf_la25_stub *entry1, *entry2;
1780
1781 entry1 = (struct mips_elf_la25_stub *) entry1_;
1782 entry2 = (struct mips_elf_la25_stub *) entry2_;
1783 return ((entry1->h->root.root.u.def.section
1784 == entry2->h->root.root.u.def.section)
1785 && (entry1->h->root.root.u.def.value
1786 == entry2->h->root.root.u.def.value));
1787 }
1788
1789 /* Called by the linker to set up the la25 stub-creation code. FN is
1790 the linker's implementation of add_stub_function. Return true on
1791 success. */
1792
1793 bfd_boolean
1794 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1795 asection *(*fn) (const char *, asection *,
1796 asection *))
1797 {
1798 struct mips_elf_link_hash_table *htab;
1799
1800 htab = mips_elf_hash_table (info);
1801 if (htab == NULL)
1802 return FALSE;
1803
1804 htab->add_stub_section = fn;
1805 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1806 mips_elf_la25_stub_eq, NULL);
1807 if (htab->la25_stubs == NULL)
1808 return FALSE;
1809
1810 return TRUE;
1811 }
1812
1813 /* Return true if H is a locally-defined PIC function, in the sense
1814 that it or its fn_stub might need $25 to be valid on entry.
1815 Note that MIPS16 functions set up $gp using PC-relative instructions,
1816 so they themselves never need $25 to be valid. Only non-MIPS16
1817 entry points are of interest here. */
1818
1819 static bfd_boolean
1820 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1821 {
1822 return ((h->root.root.type == bfd_link_hash_defined
1823 || h->root.root.type == bfd_link_hash_defweak)
1824 && h->root.def_regular
1825 && !bfd_is_abs_section (h->root.root.u.def.section)
1826 && !bfd_is_und_section (h->root.root.u.def.section)
1827 && (!ELF_ST_IS_MIPS16 (h->root.other)
1828 || (h->fn_stub && h->need_fn_stub))
1829 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1830 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1831 }
1832
1833 /* Set *SEC to the input section that contains the target of STUB.
1834 Return the offset of the target from the start of that section. */
1835
1836 static bfd_vma
1837 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1838 asection **sec)
1839 {
1840 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1841 {
1842 BFD_ASSERT (stub->h->need_fn_stub);
1843 *sec = stub->h->fn_stub;
1844 return 0;
1845 }
1846 else
1847 {
1848 *sec = stub->h->root.root.u.def.section;
1849 return stub->h->root.root.u.def.value;
1850 }
1851 }
1852
1853 /* STUB describes an la25 stub that we have decided to implement
1854 by inserting an LUI/ADDIU pair before the target function.
1855 Create the section and redirect the function symbol to it. */
1856
1857 static bfd_boolean
1858 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1859 struct bfd_link_info *info)
1860 {
1861 struct mips_elf_link_hash_table *htab;
1862 char *name;
1863 asection *s, *input_section;
1864 unsigned int align;
1865
1866 htab = mips_elf_hash_table (info);
1867 if (htab == NULL)
1868 return FALSE;
1869
1870 /* Create a unique name for the new section. */
1871 name = bfd_malloc (11 + sizeof (".text.stub."));
1872 if (name == NULL)
1873 return FALSE;
1874 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1875
1876 /* Create the section. */
1877 mips_elf_get_la25_target (stub, &input_section);
1878 s = htab->add_stub_section (name, input_section,
1879 input_section->output_section);
1880 if (s == NULL)
1881 return FALSE;
1882
1883 /* Make sure that any padding goes before the stub. */
1884 align = input_section->alignment_power;
1885 if (!bfd_set_section_alignment (s->owner, s, align))
1886 return FALSE;
1887 if (align > 3)
1888 s->size = (1 << align) - 8;
1889
1890 /* Create a symbol for the stub. */
1891 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1892 stub->stub_section = s;
1893 stub->offset = s->size;
1894
1895 /* Allocate room for it. */
1896 s->size += 8;
1897 return TRUE;
1898 }
1899
1900 /* STUB describes an la25 stub that we have decided to implement
1901 with a separate trampoline. Allocate room for it and redirect
1902 the function symbol to it. */
1903
1904 static bfd_boolean
1905 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1906 struct bfd_link_info *info)
1907 {
1908 struct mips_elf_link_hash_table *htab;
1909 asection *s;
1910
1911 htab = mips_elf_hash_table (info);
1912 if (htab == NULL)
1913 return FALSE;
1914
1915 /* Create a trampoline section, if we haven't already. */
1916 s = htab->strampoline;
1917 if (s == NULL)
1918 {
1919 asection *input_section = stub->h->root.root.u.def.section;
1920 s = htab->add_stub_section (".text", NULL,
1921 input_section->output_section);
1922 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1923 return FALSE;
1924 htab->strampoline = s;
1925 }
1926
1927 /* Create a symbol for the stub. */
1928 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1929 stub->stub_section = s;
1930 stub->offset = s->size;
1931
1932 /* Allocate room for it. */
1933 s->size += 16;
1934 return TRUE;
1935 }
1936
1937 /* H describes a symbol that needs an la25 stub. Make sure that an
1938 appropriate stub exists and point H at it. */
1939
1940 static bfd_boolean
1941 mips_elf_add_la25_stub (struct bfd_link_info *info,
1942 struct mips_elf_link_hash_entry *h)
1943 {
1944 struct mips_elf_link_hash_table *htab;
1945 struct mips_elf_la25_stub search, *stub;
1946 bfd_boolean use_trampoline_p;
1947 asection *s;
1948 bfd_vma value;
1949 void **slot;
1950
1951 /* Describe the stub we want. */
1952 search.stub_section = NULL;
1953 search.offset = 0;
1954 search.h = h;
1955
1956 /* See if we've already created an equivalent stub. */
1957 htab = mips_elf_hash_table (info);
1958 if (htab == NULL)
1959 return FALSE;
1960
1961 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1962 if (slot == NULL)
1963 return FALSE;
1964
1965 stub = (struct mips_elf_la25_stub *) *slot;
1966 if (stub != NULL)
1967 {
1968 /* We can reuse the existing stub. */
1969 h->la25_stub = stub;
1970 return TRUE;
1971 }
1972
1973 /* Create a permanent copy of ENTRY and add it to the hash table. */
1974 stub = bfd_malloc (sizeof (search));
1975 if (stub == NULL)
1976 return FALSE;
1977 *stub = search;
1978 *slot = stub;
1979
1980 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1981 of the section and if we would need no more than 2 nops. */
1982 value = mips_elf_get_la25_target (stub, &s);
1983 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
1984 value &= ~1;
1985 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1986
1987 h->la25_stub = stub;
1988 return (use_trampoline_p
1989 ? mips_elf_add_la25_trampoline (stub, info)
1990 : mips_elf_add_la25_intro (stub, info));
1991 }
1992
1993 /* A mips_elf_link_hash_traverse callback that is called before sizing
1994 sections. DATA points to a mips_htab_traverse_info structure. */
1995
1996 static bfd_boolean
1997 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1998 {
1999 struct mips_htab_traverse_info *hti;
2000
2001 hti = (struct mips_htab_traverse_info *) data;
2002 if (!bfd_link_relocatable (hti->info))
2003 mips_elf_check_mips16_stubs (hti->info, h);
2004
2005 if (mips_elf_local_pic_function_p (h))
2006 {
2007 /* PR 12845: If H is in a section that has been garbage
2008 collected it will have its output section set to *ABS*. */
2009 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2010 return TRUE;
2011
2012 /* H is a function that might need $25 to be valid on entry.
2013 If we're creating a non-PIC relocatable object, mark H as
2014 being PIC. If we're creating a non-relocatable object with
2015 non-PIC branches and jumps to H, make sure that H has an la25
2016 stub. */
2017 if (bfd_link_relocatable (hti->info))
2018 {
2019 if (!PIC_OBJECT_P (hti->output_bfd))
2020 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2021 }
2022 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2023 {
2024 hti->error = TRUE;
2025 return FALSE;
2026 }
2027 }
2028 return TRUE;
2029 }
2030
2031 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2033 Most mips16 instructions are 16 bits, but these instructions
2034 are 32 bits.
2035
2036 The format of these instructions is:
2037
2038 +--------------+--------------------------------+
2039 | JALX | X| Imm 20:16 | Imm 25:21 |
2040 +--------------+--------------------------------+
2041 | Immediate 15:0 |
2042 +-----------------------------------------------+
2043
2044 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2045 Note that the immediate value in the first word is swapped.
2046
2047 When producing a relocatable object file, R_MIPS16_26 is
2048 handled mostly like R_MIPS_26. In particular, the addend is
2049 stored as a straight 26-bit value in a 32-bit instruction.
2050 (gas makes life simpler for itself by never adjusting a
2051 R_MIPS16_26 reloc to be against a section, so the addend is
2052 always zero). However, the 32 bit instruction is stored as 2
2053 16-bit values, rather than a single 32-bit value. In a
2054 big-endian file, the result is the same; in a little-endian
2055 file, the two 16-bit halves of the 32 bit value are swapped.
2056 This is so that a disassembler can recognize the jal
2057 instruction.
2058
2059 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2060 instruction stored as two 16-bit values. The addend A is the
2061 contents of the targ26 field. The calculation is the same as
2062 R_MIPS_26. When storing the calculated value, reorder the
2063 immediate value as shown above, and don't forget to store the
2064 value as two 16-bit values.
2065
2066 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2067 defined as
2068
2069 big-endian:
2070 +--------+----------------------+
2071 | | |
2072 | | targ26-16 |
2073 |31 26|25 0|
2074 +--------+----------------------+
2075
2076 little-endian:
2077 +----------+------+-------------+
2078 | | | |
2079 | sub1 | | sub2 |
2080 |0 9|10 15|16 31|
2081 +----------+--------------------+
2082 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2083 ((sub1 << 16) | sub2)).
2084
2085 When producing a relocatable object file, the calculation is
2086 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2087 When producing a fully linked file, the calculation is
2088 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2089 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2090
2091 The table below lists the other MIPS16 instruction relocations.
2092 Each one is calculated in the same way as the non-MIPS16 relocation
2093 given on the right, but using the extended MIPS16 layout of 16-bit
2094 immediate fields:
2095
2096 R_MIPS16_GPREL R_MIPS_GPREL16
2097 R_MIPS16_GOT16 R_MIPS_GOT16
2098 R_MIPS16_CALL16 R_MIPS_CALL16
2099 R_MIPS16_HI16 R_MIPS_HI16
2100 R_MIPS16_LO16 R_MIPS_LO16
2101
2102 A typical instruction will have a format like this:
2103
2104 +--------------+--------------------------------+
2105 | EXTEND | Imm 10:5 | Imm 15:11 |
2106 +--------------+--------------------------------+
2107 | Major | rx | ry | Imm 4:0 |
2108 +--------------+--------------------------------+
2109
2110 EXTEND is the five bit value 11110. Major is the instruction
2111 opcode.
2112
2113 All we need to do here is shuffle the bits appropriately.
2114 As above, the two 16-bit halves must be swapped on a
2115 little-endian system.
2116
2117 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2118 relocatable field is shifted by 1 rather than 2 and the same bit
2119 shuffling is done as with the relocations above. */
2120
2121 static inline bfd_boolean
2122 mips16_reloc_p (int r_type)
2123 {
2124 switch (r_type)
2125 {
2126 case R_MIPS16_26:
2127 case R_MIPS16_GPREL:
2128 case R_MIPS16_GOT16:
2129 case R_MIPS16_CALL16:
2130 case R_MIPS16_HI16:
2131 case R_MIPS16_LO16:
2132 case R_MIPS16_TLS_GD:
2133 case R_MIPS16_TLS_LDM:
2134 case R_MIPS16_TLS_DTPREL_HI16:
2135 case R_MIPS16_TLS_DTPREL_LO16:
2136 case R_MIPS16_TLS_GOTTPREL:
2137 case R_MIPS16_TLS_TPREL_HI16:
2138 case R_MIPS16_TLS_TPREL_LO16:
2139 case R_MIPS16_PC16_S1:
2140 return TRUE;
2141
2142 default:
2143 return FALSE;
2144 }
2145 }
2146
2147 /* Check if a microMIPS reloc. */
2148
2149 static inline bfd_boolean
2150 micromips_reloc_p (unsigned int r_type)
2151 {
2152 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2153 }
2154
2155 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2156 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2157 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2158
2159 static inline bfd_boolean
2160 micromips_reloc_shuffle_p (unsigned int r_type)
2161 {
2162 return (micromips_reloc_p (r_type)
2163 && r_type != R_MICROMIPS_PC7_S1
2164 && r_type != R_MICROMIPS_PC10_S1);
2165 }
2166
2167 static inline bfd_boolean
2168 got16_reloc_p (int r_type)
2169 {
2170 return (r_type == R_MIPS_GOT16
2171 || r_type == R_MIPS16_GOT16
2172 || r_type == R_MICROMIPS_GOT16);
2173 }
2174
2175 static inline bfd_boolean
2176 call16_reloc_p (int r_type)
2177 {
2178 return (r_type == R_MIPS_CALL16
2179 || r_type == R_MIPS16_CALL16
2180 || r_type == R_MICROMIPS_CALL16);
2181 }
2182
2183 static inline bfd_boolean
2184 got_disp_reloc_p (unsigned int r_type)
2185 {
2186 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2187 }
2188
2189 static inline bfd_boolean
2190 got_page_reloc_p (unsigned int r_type)
2191 {
2192 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2193 }
2194
2195 static inline bfd_boolean
2196 got_lo16_reloc_p (unsigned int r_type)
2197 {
2198 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2199 }
2200
2201 static inline bfd_boolean
2202 call_hi16_reloc_p (unsigned int r_type)
2203 {
2204 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2205 }
2206
2207 static inline bfd_boolean
2208 call_lo16_reloc_p (unsigned int r_type)
2209 {
2210 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2211 }
2212
2213 static inline bfd_boolean
2214 hi16_reloc_p (int r_type)
2215 {
2216 return (r_type == R_MIPS_HI16
2217 || r_type == R_MIPS16_HI16
2218 || r_type == R_MICROMIPS_HI16
2219 || r_type == R_MIPS_PCHI16);
2220 }
2221
2222 static inline bfd_boolean
2223 lo16_reloc_p (int r_type)
2224 {
2225 return (r_type == R_MIPS_LO16
2226 || r_type == R_MIPS16_LO16
2227 || r_type == R_MICROMIPS_LO16
2228 || r_type == R_MIPS_PCLO16);
2229 }
2230
2231 static inline bfd_boolean
2232 mips16_call_reloc_p (int r_type)
2233 {
2234 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2235 }
2236
2237 static inline bfd_boolean
2238 jal_reloc_p (int r_type)
2239 {
2240 return (r_type == R_MIPS_26
2241 || r_type == R_MIPS16_26
2242 || r_type == R_MICROMIPS_26_S1);
2243 }
2244
2245 static inline bfd_boolean
2246 b_reloc_p (int r_type)
2247 {
2248 return (r_type == R_MIPS_PC26_S2
2249 || r_type == R_MIPS_PC21_S2
2250 || r_type == R_MIPS_PC16
2251 || r_type == R_MIPS_GNU_REL16_S2
2252 || r_type == R_MIPS16_PC16_S1
2253 || r_type == R_MICROMIPS_PC16_S1
2254 || r_type == R_MICROMIPS_PC10_S1
2255 || r_type == R_MICROMIPS_PC7_S1);
2256 }
2257
2258 static inline bfd_boolean
2259 aligned_pcrel_reloc_p (int r_type)
2260 {
2261 return (r_type == R_MIPS_PC18_S3
2262 || r_type == R_MIPS_PC19_S2);
2263 }
2264
2265 static inline bfd_boolean
2266 branch_reloc_p (int r_type)
2267 {
2268 return (r_type == R_MIPS_26
2269 || r_type == R_MIPS_PC26_S2
2270 || r_type == R_MIPS_PC21_S2
2271 || r_type == R_MIPS_PC16
2272 || r_type == R_MIPS_GNU_REL16_S2);
2273 }
2274
2275 static inline bfd_boolean
2276 mips16_branch_reloc_p (int r_type)
2277 {
2278 return (r_type == R_MIPS16_26
2279 || r_type == R_MIPS16_PC16_S1);
2280 }
2281
2282 static inline bfd_boolean
2283 micromips_branch_reloc_p (int r_type)
2284 {
2285 return (r_type == R_MICROMIPS_26_S1
2286 || r_type == R_MICROMIPS_PC16_S1
2287 || r_type == R_MICROMIPS_PC10_S1
2288 || r_type == R_MICROMIPS_PC7_S1);
2289 }
2290
2291 static inline bfd_boolean
2292 tls_gd_reloc_p (unsigned int r_type)
2293 {
2294 return (r_type == R_MIPS_TLS_GD
2295 || r_type == R_MIPS16_TLS_GD
2296 || r_type == R_MICROMIPS_TLS_GD);
2297 }
2298
2299 static inline bfd_boolean
2300 tls_ldm_reloc_p (unsigned int r_type)
2301 {
2302 return (r_type == R_MIPS_TLS_LDM
2303 || r_type == R_MIPS16_TLS_LDM
2304 || r_type == R_MICROMIPS_TLS_LDM);
2305 }
2306
2307 static inline bfd_boolean
2308 tls_gottprel_reloc_p (unsigned int r_type)
2309 {
2310 return (r_type == R_MIPS_TLS_GOTTPREL
2311 || r_type == R_MIPS16_TLS_GOTTPREL
2312 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2313 }
2314
2315 void
2316 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2317 bfd_boolean jal_shuffle, bfd_byte *data)
2318 {
2319 bfd_vma first, second, val;
2320
2321 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2322 return;
2323
2324 /* Pick up the first and second halfwords of the instruction. */
2325 first = bfd_get_16 (abfd, data);
2326 second = bfd_get_16 (abfd, data + 2);
2327 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2328 val = first << 16 | second;
2329 else if (r_type != R_MIPS16_26)
2330 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2331 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2332 else
2333 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2334 | ((first & 0x1f) << 21) | second);
2335 bfd_put_32 (abfd, val, data);
2336 }
2337
2338 void
2339 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2340 bfd_boolean jal_shuffle, bfd_byte *data)
2341 {
2342 bfd_vma first, second, val;
2343
2344 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2345 return;
2346
2347 val = bfd_get_32 (abfd, data);
2348 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2349 {
2350 second = val & 0xffff;
2351 first = val >> 16;
2352 }
2353 else if (r_type != R_MIPS16_26)
2354 {
2355 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2356 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2357 }
2358 else
2359 {
2360 second = val & 0xffff;
2361 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2362 | ((val >> 21) & 0x1f);
2363 }
2364 bfd_put_16 (abfd, second, data + 2);
2365 bfd_put_16 (abfd, first, data);
2366 }
2367
2368 bfd_reloc_status_type
2369 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2370 arelent *reloc_entry, asection *input_section,
2371 bfd_boolean relocatable, void *data, bfd_vma gp)
2372 {
2373 bfd_vma relocation;
2374 bfd_signed_vma val;
2375 bfd_reloc_status_type status;
2376
2377 if (bfd_is_com_section (symbol->section))
2378 relocation = 0;
2379 else
2380 relocation = symbol->value;
2381
2382 relocation += symbol->section->output_section->vma;
2383 relocation += symbol->section->output_offset;
2384
2385 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2386 return bfd_reloc_outofrange;
2387
2388 /* Set val to the offset into the section or symbol. */
2389 val = reloc_entry->addend;
2390
2391 _bfd_mips_elf_sign_extend (val, 16);
2392
2393 /* Adjust val for the final section location and GP value. If we
2394 are producing relocatable output, we don't want to do this for
2395 an external symbol. */
2396 if (! relocatable
2397 || (symbol->flags & BSF_SECTION_SYM) != 0)
2398 val += relocation - gp;
2399
2400 if (reloc_entry->howto->partial_inplace)
2401 {
2402 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2403 (bfd_byte *) data
2404 + reloc_entry->address);
2405 if (status != bfd_reloc_ok)
2406 return status;
2407 }
2408 else
2409 reloc_entry->addend = val;
2410
2411 if (relocatable)
2412 reloc_entry->address += input_section->output_offset;
2413
2414 return bfd_reloc_ok;
2415 }
2416
2417 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2418 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2419 that contains the relocation field and DATA points to the start of
2420 INPUT_SECTION. */
2421
2422 struct mips_hi16
2423 {
2424 struct mips_hi16 *next;
2425 bfd_byte *data;
2426 asection *input_section;
2427 arelent rel;
2428 };
2429
2430 /* FIXME: This should not be a static variable. */
2431
2432 static struct mips_hi16 *mips_hi16_list;
2433
2434 /* A howto special_function for REL *HI16 relocations. We can only
2435 calculate the correct value once we've seen the partnering
2436 *LO16 relocation, so just save the information for later.
2437
2438 The ABI requires that the *LO16 immediately follow the *HI16.
2439 However, as a GNU extension, we permit an arbitrary number of
2440 *HI16s to be associated with a single *LO16. This significantly
2441 simplies the relocation handling in gcc. */
2442
2443 bfd_reloc_status_type
2444 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2445 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2446 asection *input_section, bfd *output_bfd,
2447 char **error_message ATTRIBUTE_UNUSED)
2448 {
2449 struct mips_hi16 *n;
2450
2451 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2452 return bfd_reloc_outofrange;
2453
2454 n = bfd_malloc (sizeof *n);
2455 if (n == NULL)
2456 return bfd_reloc_outofrange;
2457
2458 n->next = mips_hi16_list;
2459 n->data = data;
2460 n->input_section = input_section;
2461 n->rel = *reloc_entry;
2462 mips_hi16_list = n;
2463
2464 if (output_bfd != NULL)
2465 reloc_entry->address += input_section->output_offset;
2466
2467 return bfd_reloc_ok;
2468 }
2469
2470 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2471 like any other 16-bit relocation when applied to global symbols, but is
2472 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2473
2474 bfd_reloc_status_type
2475 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2476 void *data, asection *input_section,
2477 bfd *output_bfd, char **error_message)
2478 {
2479 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2480 || bfd_is_und_section (bfd_get_section (symbol))
2481 || bfd_is_com_section (bfd_get_section (symbol)))
2482 /* The relocation is against a global symbol. */
2483 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2484 input_section, output_bfd,
2485 error_message);
2486
2487 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2488 input_section, output_bfd, error_message);
2489 }
2490
2491 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2492 is a straightforward 16 bit inplace relocation, but we must deal with
2493 any partnering high-part relocations as well. */
2494
2495 bfd_reloc_status_type
2496 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2497 void *data, asection *input_section,
2498 bfd *output_bfd, char **error_message)
2499 {
2500 bfd_vma vallo;
2501 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2502
2503 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2504 return bfd_reloc_outofrange;
2505
2506 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2507 location);
2508 vallo = bfd_get_32 (abfd, location);
2509 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2510 location);
2511
2512 while (mips_hi16_list != NULL)
2513 {
2514 bfd_reloc_status_type ret;
2515 struct mips_hi16 *hi;
2516
2517 hi = mips_hi16_list;
2518
2519 /* R_MIPS*_GOT16 relocations are something of a special case. We
2520 want to install the addend in the same way as for a R_MIPS*_HI16
2521 relocation (with a rightshift of 16). However, since GOT16
2522 relocations can also be used with global symbols, their howto
2523 has a rightshift of 0. */
2524 if (hi->rel.howto->type == R_MIPS_GOT16)
2525 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2526 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2527 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2528 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2529 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2530
2531 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2532 carry or borrow will induce a change of +1 or -1 in the high part. */
2533 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2534
2535 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2536 hi->input_section, output_bfd,
2537 error_message);
2538 if (ret != bfd_reloc_ok)
2539 return ret;
2540
2541 mips_hi16_list = hi->next;
2542 free (hi);
2543 }
2544
2545 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2546 input_section, output_bfd,
2547 error_message);
2548 }
2549
2550 /* A generic howto special_function. This calculates and installs the
2551 relocation itself, thus avoiding the oft-discussed problems in
2552 bfd_perform_relocation and bfd_install_relocation. */
2553
2554 bfd_reloc_status_type
2555 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2556 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2557 asection *input_section, bfd *output_bfd,
2558 char **error_message ATTRIBUTE_UNUSED)
2559 {
2560 bfd_signed_vma val;
2561 bfd_reloc_status_type status;
2562 bfd_boolean relocatable;
2563
2564 relocatable = (output_bfd != NULL);
2565
2566 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2567 return bfd_reloc_outofrange;
2568
2569 /* Build up the field adjustment in VAL. */
2570 val = 0;
2571 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2572 {
2573 /* Either we're calculating the final field value or we have a
2574 relocation against a section symbol. Add in the section's
2575 offset or address. */
2576 val += symbol->section->output_section->vma;
2577 val += symbol->section->output_offset;
2578 }
2579
2580 if (!relocatable)
2581 {
2582 /* We're calculating the final field value. Add in the symbol's value
2583 and, if pc-relative, subtract the address of the field itself. */
2584 val += symbol->value;
2585 if (reloc_entry->howto->pc_relative)
2586 {
2587 val -= input_section->output_section->vma;
2588 val -= input_section->output_offset;
2589 val -= reloc_entry->address;
2590 }
2591 }
2592
2593 /* VAL is now the final adjustment. If we're keeping this relocation
2594 in the output file, and if the relocation uses a separate addend,
2595 we just need to add VAL to that addend. Otherwise we need to add
2596 VAL to the relocation field itself. */
2597 if (relocatable && !reloc_entry->howto->partial_inplace)
2598 reloc_entry->addend += val;
2599 else
2600 {
2601 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2602
2603 /* Add in the separate addend, if any. */
2604 val += reloc_entry->addend;
2605
2606 /* Add VAL to the relocation field. */
2607 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2608 location);
2609 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2610 location);
2611 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2612 location);
2613
2614 if (status != bfd_reloc_ok)
2615 return status;
2616 }
2617
2618 if (relocatable)
2619 reloc_entry->address += input_section->output_offset;
2620
2621 return bfd_reloc_ok;
2622 }
2623
2624 /* Swap an entry in a .gptab section. Note that these routines rely
2626 on the equivalence of the two elements of the union. */
2627
2628 static void
2629 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2630 Elf32_gptab *in)
2631 {
2632 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2633 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2634 }
2635
2636 static void
2637 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2638 Elf32_External_gptab *ex)
2639 {
2640 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2641 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2642 }
2643
2644 static void
2645 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2646 Elf32_External_compact_rel *ex)
2647 {
2648 H_PUT_32 (abfd, in->id1, ex->id1);
2649 H_PUT_32 (abfd, in->num, ex->num);
2650 H_PUT_32 (abfd, in->id2, ex->id2);
2651 H_PUT_32 (abfd, in->offset, ex->offset);
2652 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2653 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2654 }
2655
2656 static void
2657 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2658 Elf32_External_crinfo *ex)
2659 {
2660 unsigned long l;
2661
2662 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2663 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2664 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2665 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2666 H_PUT_32 (abfd, l, ex->info);
2667 H_PUT_32 (abfd, in->konst, ex->konst);
2668 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2669 }
2670
2671 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2673 routines swap this structure in and out. They are used outside of
2674 BFD, so they are globally visible. */
2675
2676 void
2677 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2678 Elf32_RegInfo *in)
2679 {
2680 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2681 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2682 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2683 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2684 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2685 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2686 }
2687
2688 void
2689 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2690 Elf32_External_RegInfo *ex)
2691 {
2692 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2693 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2694 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2695 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2696 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2697 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2698 }
2699
2700 /* In the 64 bit ABI, the .MIPS.options section holds register
2701 information in an Elf64_Reginfo structure. These routines swap
2702 them in and out. They are globally visible because they are used
2703 outside of BFD. These routines are here so that gas can call them
2704 without worrying about whether the 64 bit ABI has been included. */
2705
2706 void
2707 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2708 Elf64_Internal_RegInfo *in)
2709 {
2710 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2711 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2712 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2713 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2714 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2715 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2716 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2717 }
2718
2719 void
2720 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2721 Elf64_External_RegInfo *ex)
2722 {
2723 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2724 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2725 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2726 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2727 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2728 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2729 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2730 }
2731
2732 /* Swap in an options header. */
2733
2734 void
2735 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2736 Elf_Internal_Options *in)
2737 {
2738 in->kind = H_GET_8 (abfd, ex->kind);
2739 in->size = H_GET_8 (abfd, ex->size);
2740 in->section = H_GET_16 (abfd, ex->section);
2741 in->info = H_GET_32 (abfd, ex->info);
2742 }
2743
2744 /* Swap out an options header. */
2745
2746 void
2747 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2748 Elf_External_Options *ex)
2749 {
2750 H_PUT_8 (abfd, in->kind, ex->kind);
2751 H_PUT_8 (abfd, in->size, ex->size);
2752 H_PUT_16 (abfd, in->section, ex->section);
2753 H_PUT_32 (abfd, in->info, ex->info);
2754 }
2755
2756 /* Swap in an abiflags structure. */
2757
2758 void
2759 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2760 const Elf_External_ABIFlags_v0 *ex,
2761 Elf_Internal_ABIFlags_v0 *in)
2762 {
2763 in->version = H_GET_16 (abfd, ex->version);
2764 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2765 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2766 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2767 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2768 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2769 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2770 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2771 in->ases = H_GET_32 (abfd, ex->ases);
2772 in->flags1 = H_GET_32 (abfd, ex->flags1);
2773 in->flags2 = H_GET_32 (abfd, ex->flags2);
2774 }
2775
2776 /* Swap out an abiflags structure. */
2777
2778 void
2779 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2780 const Elf_Internal_ABIFlags_v0 *in,
2781 Elf_External_ABIFlags_v0 *ex)
2782 {
2783 H_PUT_16 (abfd, in->version, ex->version);
2784 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2785 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2786 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2787 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2788 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2789 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2790 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2791 H_PUT_32 (abfd, in->ases, ex->ases);
2792 H_PUT_32 (abfd, in->flags1, ex->flags1);
2793 H_PUT_32 (abfd, in->flags2, ex->flags2);
2794 }
2795
2796 /* This function is called via qsort() to sort the dynamic relocation
2798 entries by increasing r_symndx value. */
2799
2800 static int
2801 sort_dynamic_relocs (const void *arg1, const void *arg2)
2802 {
2803 Elf_Internal_Rela int_reloc1;
2804 Elf_Internal_Rela int_reloc2;
2805 int diff;
2806
2807 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2808 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2809
2810 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2811 if (diff != 0)
2812 return diff;
2813
2814 if (int_reloc1.r_offset < int_reloc2.r_offset)
2815 return -1;
2816 if (int_reloc1.r_offset > int_reloc2.r_offset)
2817 return 1;
2818 return 0;
2819 }
2820
2821 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2822
2823 static int
2824 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2825 const void *arg2 ATTRIBUTE_UNUSED)
2826 {
2827 #ifdef BFD64
2828 Elf_Internal_Rela int_reloc1[3];
2829 Elf_Internal_Rela int_reloc2[3];
2830
2831 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2832 (reldyn_sorting_bfd, arg1, int_reloc1);
2833 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2834 (reldyn_sorting_bfd, arg2, int_reloc2);
2835
2836 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2837 return -1;
2838 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2839 return 1;
2840
2841 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2842 return -1;
2843 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2844 return 1;
2845 return 0;
2846 #else
2847 abort ();
2848 #endif
2849 }
2850
2851
2852 /* This routine is used to write out ECOFF debugging external symbol
2853 information. It is called via mips_elf_link_hash_traverse. The
2854 ECOFF external symbol information must match the ELF external
2855 symbol information. Unfortunately, at this point we don't know
2856 whether a symbol is required by reloc information, so the two
2857 tables may wind up being different. We must sort out the external
2858 symbol information before we can set the final size of the .mdebug
2859 section, and we must set the size of the .mdebug section before we
2860 can relocate any sections, and we can't know which symbols are
2861 required by relocation until we relocate the sections.
2862 Fortunately, it is relatively unlikely that any symbol will be
2863 stripped but required by a reloc. In particular, it can not happen
2864 when generating a final executable. */
2865
2866 static bfd_boolean
2867 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2868 {
2869 struct extsym_info *einfo = data;
2870 bfd_boolean strip;
2871 asection *sec, *output_section;
2872
2873 if (h->root.indx == -2)
2874 strip = FALSE;
2875 else if ((h->root.def_dynamic
2876 || h->root.ref_dynamic
2877 || h->root.type == bfd_link_hash_new)
2878 && !h->root.def_regular
2879 && !h->root.ref_regular)
2880 strip = TRUE;
2881 else if (einfo->info->strip == strip_all
2882 || (einfo->info->strip == strip_some
2883 && bfd_hash_lookup (einfo->info->keep_hash,
2884 h->root.root.root.string,
2885 FALSE, FALSE) == NULL))
2886 strip = TRUE;
2887 else
2888 strip = FALSE;
2889
2890 if (strip)
2891 return TRUE;
2892
2893 if (h->esym.ifd == -2)
2894 {
2895 h->esym.jmptbl = 0;
2896 h->esym.cobol_main = 0;
2897 h->esym.weakext = 0;
2898 h->esym.reserved = 0;
2899 h->esym.ifd = ifdNil;
2900 h->esym.asym.value = 0;
2901 h->esym.asym.st = stGlobal;
2902
2903 if (h->root.root.type == bfd_link_hash_undefined
2904 || h->root.root.type == bfd_link_hash_undefweak)
2905 {
2906 const char *name;
2907
2908 /* Use undefined class. Also, set class and type for some
2909 special symbols. */
2910 name = h->root.root.root.string;
2911 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2912 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2913 {
2914 h->esym.asym.sc = scData;
2915 h->esym.asym.st = stLabel;
2916 h->esym.asym.value = 0;
2917 }
2918 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2919 {
2920 h->esym.asym.sc = scAbs;
2921 h->esym.asym.st = stLabel;
2922 h->esym.asym.value =
2923 mips_elf_hash_table (einfo->info)->procedure_count;
2924 }
2925 else
2926 h->esym.asym.sc = scUndefined;
2927 }
2928 else if (h->root.root.type != bfd_link_hash_defined
2929 && h->root.root.type != bfd_link_hash_defweak)
2930 h->esym.asym.sc = scAbs;
2931 else
2932 {
2933 const char *name;
2934
2935 sec = h->root.root.u.def.section;
2936 output_section = sec->output_section;
2937
2938 /* When making a shared library and symbol h is the one from
2939 the another shared library, OUTPUT_SECTION may be null. */
2940 if (output_section == NULL)
2941 h->esym.asym.sc = scUndefined;
2942 else
2943 {
2944 name = bfd_section_name (output_section->owner, output_section);
2945
2946 if (strcmp (name, ".text") == 0)
2947 h->esym.asym.sc = scText;
2948 else if (strcmp (name, ".data") == 0)
2949 h->esym.asym.sc = scData;
2950 else if (strcmp (name, ".sdata") == 0)
2951 h->esym.asym.sc = scSData;
2952 else if (strcmp (name, ".rodata") == 0
2953 || strcmp (name, ".rdata") == 0)
2954 h->esym.asym.sc = scRData;
2955 else if (strcmp (name, ".bss") == 0)
2956 h->esym.asym.sc = scBss;
2957 else if (strcmp (name, ".sbss") == 0)
2958 h->esym.asym.sc = scSBss;
2959 else if (strcmp (name, ".init") == 0)
2960 h->esym.asym.sc = scInit;
2961 else if (strcmp (name, ".fini") == 0)
2962 h->esym.asym.sc = scFini;
2963 else
2964 h->esym.asym.sc = scAbs;
2965 }
2966 }
2967
2968 h->esym.asym.reserved = 0;
2969 h->esym.asym.index = indexNil;
2970 }
2971
2972 if (h->root.root.type == bfd_link_hash_common)
2973 h->esym.asym.value = h->root.root.u.c.size;
2974 else if (h->root.root.type == bfd_link_hash_defined
2975 || h->root.root.type == bfd_link_hash_defweak)
2976 {
2977 if (h->esym.asym.sc == scCommon)
2978 h->esym.asym.sc = scBss;
2979 else if (h->esym.asym.sc == scSCommon)
2980 h->esym.asym.sc = scSBss;
2981
2982 sec = h->root.root.u.def.section;
2983 output_section = sec->output_section;
2984 if (output_section != NULL)
2985 h->esym.asym.value = (h->root.root.u.def.value
2986 + sec->output_offset
2987 + output_section->vma);
2988 else
2989 h->esym.asym.value = 0;
2990 }
2991 else
2992 {
2993 struct mips_elf_link_hash_entry *hd = h;
2994
2995 while (hd->root.root.type == bfd_link_hash_indirect)
2996 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2997
2998 if (hd->needs_lazy_stub)
2999 {
3000 BFD_ASSERT (hd->root.plt.plist != NULL);
3001 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3002 /* Set type and value for a symbol with a function stub. */
3003 h->esym.asym.st = stProc;
3004 sec = hd->root.root.u.def.section;
3005 if (sec == NULL)
3006 h->esym.asym.value = 0;
3007 else
3008 {
3009 output_section = sec->output_section;
3010 if (output_section != NULL)
3011 h->esym.asym.value = (hd->root.plt.plist->stub_offset
3012 + sec->output_offset
3013 + output_section->vma);
3014 else
3015 h->esym.asym.value = 0;
3016 }
3017 }
3018 }
3019
3020 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3021 h->root.root.root.string,
3022 &h->esym))
3023 {
3024 einfo->failed = TRUE;
3025 return FALSE;
3026 }
3027
3028 return TRUE;
3029 }
3030
3031 /* A comparison routine used to sort .gptab entries. */
3032
3033 static int
3034 gptab_compare (const void *p1, const void *p2)
3035 {
3036 const Elf32_gptab *a1 = p1;
3037 const Elf32_gptab *a2 = p2;
3038
3039 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3040 }
3041
3042 /* Functions to manage the got entry hash table. */
3044
3045 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3046 hash number. */
3047
3048 static INLINE hashval_t
3049 mips_elf_hash_bfd_vma (bfd_vma addr)
3050 {
3051 #ifdef BFD64
3052 return addr + (addr >> 32);
3053 #else
3054 return addr;
3055 #endif
3056 }
3057
3058 static hashval_t
3059 mips_elf_got_entry_hash (const void *entry_)
3060 {
3061 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3062
3063 return (entry->symndx
3064 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3065 + (entry->tls_type == GOT_TLS_LDM ? 0
3066 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3067 : entry->symndx >= 0 ? (entry->abfd->id
3068 + mips_elf_hash_bfd_vma (entry->d.addend))
3069 : entry->d.h->root.root.root.hash));
3070 }
3071
3072 static int
3073 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3074 {
3075 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3076 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3077
3078 return (e1->symndx == e2->symndx
3079 && e1->tls_type == e2->tls_type
3080 && (e1->tls_type == GOT_TLS_LDM ? TRUE
3081 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3082 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3083 && e1->d.addend == e2->d.addend)
3084 : e2->abfd && e1->d.h == e2->d.h));
3085 }
3086
3087 static hashval_t
3088 mips_got_page_ref_hash (const void *ref_)
3089 {
3090 const struct mips_got_page_ref *ref;
3091
3092 ref = (const struct mips_got_page_ref *) ref_;
3093 return ((ref->symndx >= 0
3094 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3095 : ref->u.h->root.root.root.hash)
3096 + mips_elf_hash_bfd_vma (ref->addend));
3097 }
3098
3099 static int
3100 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3101 {
3102 const struct mips_got_page_ref *ref1, *ref2;
3103
3104 ref1 = (const struct mips_got_page_ref *) ref1_;
3105 ref2 = (const struct mips_got_page_ref *) ref2_;
3106 return (ref1->symndx == ref2->symndx
3107 && (ref1->symndx < 0
3108 ? ref1->u.h == ref2->u.h
3109 : ref1->u.abfd == ref2->u.abfd)
3110 && ref1->addend == ref2->addend);
3111 }
3112
3113 static hashval_t
3114 mips_got_page_entry_hash (const void *entry_)
3115 {
3116 const struct mips_got_page_entry *entry;
3117
3118 entry = (const struct mips_got_page_entry *) entry_;
3119 return entry->sec->id;
3120 }
3121
3122 static int
3123 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3124 {
3125 const struct mips_got_page_entry *entry1, *entry2;
3126
3127 entry1 = (const struct mips_got_page_entry *) entry1_;
3128 entry2 = (const struct mips_got_page_entry *) entry2_;
3129 return entry1->sec == entry2->sec;
3130 }
3131
3132 /* Create and return a new mips_got_info structure. */
3134
3135 static struct mips_got_info *
3136 mips_elf_create_got_info (bfd *abfd)
3137 {
3138 struct mips_got_info *g;
3139
3140 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3141 if (g == NULL)
3142 return NULL;
3143
3144 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3145 mips_elf_got_entry_eq, NULL);
3146 if (g->got_entries == NULL)
3147 return NULL;
3148
3149 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3150 mips_got_page_ref_eq, NULL);
3151 if (g->got_page_refs == NULL)
3152 return NULL;
3153
3154 return g;
3155 }
3156
3157 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3158 CREATE_P and if ABFD doesn't already have a GOT. */
3159
3160 static struct mips_got_info *
3161 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3162 {
3163 struct mips_elf_obj_tdata *tdata;
3164
3165 if (!is_mips_elf (abfd))
3166 return NULL;
3167
3168 tdata = mips_elf_tdata (abfd);
3169 if (!tdata->got && create_p)
3170 tdata->got = mips_elf_create_got_info (abfd);
3171 return tdata->got;
3172 }
3173
3174 /* Record that ABFD should use output GOT G. */
3175
3176 static void
3177 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3178 {
3179 struct mips_elf_obj_tdata *tdata;
3180
3181 BFD_ASSERT (is_mips_elf (abfd));
3182 tdata = mips_elf_tdata (abfd);
3183 if (tdata->got)
3184 {
3185 /* The GOT structure itself and the hash table entries are
3186 allocated to a bfd, but the hash tables aren't. */
3187 htab_delete (tdata->got->got_entries);
3188 htab_delete (tdata->got->got_page_refs);
3189 if (tdata->got->got_page_entries)
3190 htab_delete (tdata->got->got_page_entries);
3191 }
3192 tdata->got = g;
3193 }
3194
3195 /* Return the dynamic relocation section. If it doesn't exist, try to
3196 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3197 if creation fails. */
3198
3199 static asection *
3200 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
3201 {
3202 const char *dname;
3203 asection *sreloc;
3204 bfd *dynobj;
3205
3206 dname = MIPS_ELF_REL_DYN_NAME (info);
3207 dynobj = elf_hash_table (info)->dynobj;
3208 sreloc = bfd_get_linker_section (dynobj, dname);
3209 if (sreloc == NULL && create_p)
3210 {
3211 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3212 (SEC_ALLOC
3213 | SEC_LOAD
3214 | SEC_HAS_CONTENTS
3215 | SEC_IN_MEMORY
3216 | SEC_LINKER_CREATED
3217 | SEC_READONLY));
3218 if (sreloc == NULL
3219 || ! bfd_set_section_alignment (dynobj, sreloc,
3220 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3221 return NULL;
3222 }
3223 return sreloc;
3224 }
3225
3226 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3227
3228 static int
3229 mips_elf_reloc_tls_type (unsigned int r_type)
3230 {
3231 if (tls_gd_reloc_p (r_type))
3232 return GOT_TLS_GD;
3233
3234 if (tls_ldm_reloc_p (r_type))
3235 return GOT_TLS_LDM;
3236
3237 if (tls_gottprel_reloc_p (r_type))
3238 return GOT_TLS_IE;
3239
3240 return GOT_TLS_NONE;
3241 }
3242
3243 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3244
3245 static int
3246 mips_tls_got_entries (unsigned int type)
3247 {
3248 switch (type)
3249 {
3250 case GOT_TLS_GD:
3251 case GOT_TLS_LDM:
3252 return 2;
3253
3254 case GOT_TLS_IE:
3255 return 1;
3256
3257 case GOT_TLS_NONE:
3258 return 0;
3259 }
3260 abort ();
3261 }
3262
3263 /* Count the number of relocations needed for a TLS GOT entry, with
3264 access types from TLS_TYPE, and symbol H (or a local symbol if H
3265 is NULL). */
3266
3267 static int
3268 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3269 struct elf_link_hash_entry *h)
3270 {
3271 int indx = 0;
3272 bfd_boolean need_relocs = FALSE;
3273 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3274
3275 if (h != NULL
3276 && h->dynindx != -1
3277 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3278 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3279 indx = h->dynindx;
3280
3281 if ((bfd_link_dll (info) || indx != 0)
3282 && (h == NULL
3283 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3284 || h->root.type != bfd_link_hash_undefweak))
3285 need_relocs = TRUE;
3286
3287 if (!need_relocs)
3288 return 0;
3289
3290 switch (tls_type)
3291 {
3292 case GOT_TLS_GD:
3293 return indx != 0 ? 2 : 1;
3294
3295 case GOT_TLS_IE:
3296 return 1;
3297
3298 case GOT_TLS_LDM:
3299 return bfd_link_dll (info) ? 1 : 0;
3300
3301 default:
3302 return 0;
3303 }
3304 }
3305
3306 /* Add the number of GOT entries and TLS relocations required by ENTRY
3307 to G. */
3308
3309 static void
3310 mips_elf_count_got_entry (struct bfd_link_info *info,
3311 struct mips_got_info *g,
3312 struct mips_got_entry *entry)
3313 {
3314 if (entry->tls_type)
3315 {
3316 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3317 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3318 entry->symndx < 0
3319 ? &entry->d.h->root : NULL);
3320 }
3321 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3322 g->local_gotno += 1;
3323 else
3324 g->global_gotno += 1;
3325 }
3326
3327 /* Output a simple dynamic relocation into SRELOC. */
3328
3329 static void
3330 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3331 asection *sreloc,
3332 unsigned long reloc_index,
3333 unsigned long indx,
3334 int r_type,
3335 bfd_vma offset)
3336 {
3337 Elf_Internal_Rela rel[3];
3338
3339 memset (rel, 0, sizeof (rel));
3340
3341 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3342 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3343
3344 if (ABI_64_P (output_bfd))
3345 {
3346 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3347 (output_bfd, &rel[0],
3348 (sreloc->contents
3349 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3350 }
3351 else
3352 bfd_elf32_swap_reloc_out
3353 (output_bfd, &rel[0],
3354 (sreloc->contents
3355 + reloc_index * sizeof (Elf32_External_Rel)));
3356 }
3357
3358 /* Initialize a set of TLS GOT entries for one symbol. */
3359
3360 static void
3361 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3362 struct mips_got_entry *entry,
3363 struct mips_elf_link_hash_entry *h,
3364 bfd_vma value)
3365 {
3366 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3367 struct mips_elf_link_hash_table *htab;
3368 int indx;
3369 asection *sreloc, *sgot;
3370 bfd_vma got_offset, got_offset2;
3371 bfd_boolean need_relocs = FALSE;
3372
3373 htab = mips_elf_hash_table (info);
3374 if (htab == NULL)
3375 return;
3376
3377 sgot = htab->root.sgot;
3378
3379 indx = 0;
3380 if (h != NULL
3381 && h->root.dynindx != -1
3382 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3383 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3384 indx = h->root.dynindx;
3385
3386 if (entry->tls_initialized)
3387 return;
3388
3389 if ((bfd_link_dll (info) || indx != 0)
3390 && (h == NULL
3391 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3392 || h->root.type != bfd_link_hash_undefweak))
3393 need_relocs = TRUE;
3394
3395 /* MINUS_ONE means the symbol is not defined in this object. It may not
3396 be defined at all; assume that the value doesn't matter in that
3397 case. Otherwise complain if we would use the value. */
3398 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3399 || h->root.root.type == bfd_link_hash_undefweak);
3400
3401 /* Emit necessary relocations. */
3402 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3403 got_offset = entry->gotidx;
3404
3405 switch (entry->tls_type)
3406 {
3407 case GOT_TLS_GD:
3408 /* General Dynamic. */
3409 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3410
3411 if (need_relocs)
3412 {
3413 mips_elf_output_dynamic_relocation
3414 (abfd, sreloc, sreloc->reloc_count++, indx,
3415 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3416 sgot->output_offset + sgot->output_section->vma + got_offset);
3417
3418 if (indx)
3419 mips_elf_output_dynamic_relocation
3420 (abfd, sreloc, sreloc->reloc_count++, indx,
3421 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3422 sgot->output_offset + sgot->output_section->vma + got_offset2);
3423 else
3424 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3425 sgot->contents + got_offset2);
3426 }
3427 else
3428 {
3429 MIPS_ELF_PUT_WORD (abfd, 1,
3430 sgot->contents + got_offset);
3431 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3432 sgot->contents + got_offset2);
3433 }
3434 break;
3435
3436 case GOT_TLS_IE:
3437 /* Initial Exec model. */
3438 if (need_relocs)
3439 {
3440 if (indx == 0)
3441 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3442 sgot->contents + got_offset);
3443 else
3444 MIPS_ELF_PUT_WORD (abfd, 0,
3445 sgot->contents + got_offset);
3446
3447 mips_elf_output_dynamic_relocation
3448 (abfd, sreloc, sreloc->reloc_count++, indx,
3449 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3450 sgot->output_offset + sgot->output_section->vma + got_offset);
3451 }
3452 else
3453 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3454 sgot->contents + got_offset);
3455 break;
3456
3457 case GOT_TLS_LDM:
3458 /* The initial offset is zero, and the LD offsets will include the
3459 bias by DTP_OFFSET. */
3460 MIPS_ELF_PUT_WORD (abfd, 0,
3461 sgot->contents + got_offset
3462 + MIPS_ELF_GOT_SIZE (abfd));
3463
3464 if (!bfd_link_dll (info))
3465 MIPS_ELF_PUT_WORD (abfd, 1,
3466 sgot->contents + got_offset);
3467 else
3468 mips_elf_output_dynamic_relocation
3469 (abfd, sreloc, sreloc->reloc_count++, indx,
3470 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3471 sgot->output_offset + sgot->output_section->vma + got_offset);
3472 break;
3473
3474 default:
3475 abort ();
3476 }
3477
3478 entry->tls_initialized = TRUE;
3479 }
3480
3481 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3482 for global symbol H. .got.plt comes before the GOT, so the offset
3483 will be negative. */
3484
3485 static bfd_vma
3486 mips_elf_gotplt_index (struct bfd_link_info *info,
3487 struct elf_link_hash_entry *h)
3488 {
3489 bfd_vma got_address, got_value;
3490 struct mips_elf_link_hash_table *htab;
3491
3492 htab = mips_elf_hash_table (info);
3493 BFD_ASSERT (htab != NULL);
3494
3495 BFD_ASSERT (h->plt.plist != NULL);
3496 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3497
3498 /* Calculate the address of the associated .got.plt entry. */
3499 got_address = (htab->root.sgotplt->output_section->vma
3500 + htab->root.sgotplt->output_offset
3501 + (h->plt.plist->gotplt_index
3502 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3503
3504 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3505 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3506 + htab->root.hgot->root.u.def.section->output_offset
3507 + htab->root.hgot->root.u.def.value);
3508
3509 return got_address - got_value;
3510 }
3511
3512 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3513 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3514 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3515 offset can be found. */
3516
3517 static bfd_vma
3518 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3519 bfd_vma value, unsigned long r_symndx,
3520 struct mips_elf_link_hash_entry *h, int r_type)
3521 {
3522 struct mips_elf_link_hash_table *htab;
3523 struct mips_got_entry *entry;
3524
3525 htab = mips_elf_hash_table (info);
3526 BFD_ASSERT (htab != NULL);
3527
3528 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3529 r_symndx, h, r_type);
3530 if (!entry)
3531 return MINUS_ONE;
3532
3533 if (entry->tls_type)
3534 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3535 return entry->gotidx;
3536 }
3537
3538 /* Return the GOT index of global symbol H in the primary GOT. */
3539
3540 static bfd_vma
3541 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3542 struct elf_link_hash_entry *h)
3543 {
3544 struct mips_elf_link_hash_table *htab;
3545 long global_got_dynindx;
3546 struct mips_got_info *g;
3547 bfd_vma got_index;
3548
3549 htab = mips_elf_hash_table (info);
3550 BFD_ASSERT (htab != NULL);
3551
3552 global_got_dynindx = 0;
3553 if (htab->global_gotsym != NULL)
3554 global_got_dynindx = htab->global_gotsym->dynindx;
3555
3556 /* Once we determine the global GOT entry with the lowest dynamic
3557 symbol table index, we must put all dynamic symbols with greater
3558 indices into the primary GOT. That makes it easy to calculate the
3559 GOT offset. */
3560 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3561 g = mips_elf_bfd_got (obfd, FALSE);
3562 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3563 * MIPS_ELF_GOT_SIZE (obfd));
3564 BFD_ASSERT (got_index < htab->root.sgot->size);
3565
3566 return got_index;
3567 }
3568
3569 /* Return the GOT index for the global symbol indicated by H, which is
3570 referenced by a relocation of type R_TYPE in IBFD. */
3571
3572 static bfd_vma
3573 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3574 struct elf_link_hash_entry *h, int r_type)
3575 {
3576 struct mips_elf_link_hash_table *htab;
3577 struct mips_got_info *g;
3578 struct mips_got_entry lookup, *entry;
3579 bfd_vma gotidx;
3580
3581 htab = mips_elf_hash_table (info);
3582 BFD_ASSERT (htab != NULL);
3583
3584 g = mips_elf_bfd_got (ibfd, FALSE);
3585 BFD_ASSERT (g);
3586
3587 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3588 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3589 return mips_elf_primary_global_got_index (obfd, info, h);
3590
3591 lookup.abfd = ibfd;
3592 lookup.symndx = -1;
3593 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3594 entry = htab_find (g->got_entries, &lookup);
3595 BFD_ASSERT (entry);
3596
3597 gotidx = entry->gotidx;
3598 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3599
3600 if (lookup.tls_type)
3601 {
3602 bfd_vma value = MINUS_ONE;
3603
3604 if ((h->root.type == bfd_link_hash_defined
3605 || h->root.type == bfd_link_hash_defweak)
3606 && h->root.u.def.section->output_section)
3607 value = (h->root.u.def.value
3608 + h->root.u.def.section->output_offset
3609 + h->root.u.def.section->output_section->vma);
3610
3611 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3612 }
3613 return gotidx;
3614 }
3615
3616 /* Find a GOT page entry that points to within 32KB of VALUE. These
3617 entries are supposed to be placed at small offsets in the GOT, i.e.,
3618 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3619 entry could be created. If OFFSETP is nonnull, use it to return the
3620 offset of the GOT entry from VALUE. */
3621
3622 static bfd_vma
3623 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3624 bfd_vma value, bfd_vma *offsetp)
3625 {
3626 bfd_vma page, got_index;
3627 struct mips_got_entry *entry;
3628
3629 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3630 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3631 NULL, R_MIPS_GOT_PAGE);
3632
3633 if (!entry)
3634 return MINUS_ONE;
3635
3636 got_index = entry->gotidx;
3637
3638 if (offsetp)
3639 *offsetp = value - entry->d.address;
3640
3641 return got_index;
3642 }
3643
3644 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3645 EXTERNAL is true if the relocation was originally against a global
3646 symbol that binds locally. */
3647
3648 static bfd_vma
3649 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3650 bfd_vma value, bfd_boolean external)
3651 {
3652 struct mips_got_entry *entry;
3653
3654 /* GOT16 relocations against local symbols are followed by a LO16
3655 relocation; those against global symbols are not. Thus if the
3656 symbol was originally local, the GOT16 relocation should load the
3657 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3658 if (! external)
3659 value = mips_elf_high (value) << 16;
3660
3661 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3662 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3663 same in all cases. */
3664 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3665 NULL, R_MIPS_GOT16);
3666 if (entry)
3667 return entry->gotidx;
3668 else
3669 return MINUS_ONE;
3670 }
3671
3672 /* Returns the offset for the entry at the INDEXth position
3673 in the GOT. */
3674
3675 static bfd_vma
3676 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3677 bfd *input_bfd, bfd_vma got_index)
3678 {
3679 struct mips_elf_link_hash_table *htab;
3680 asection *sgot;
3681 bfd_vma gp;
3682
3683 htab = mips_elf_hash_table (info);
3684 BFD_ASSERT (htab != NULL);
3685
3686 sgot = htab->root.sgot;
3687 gp = _bfd_get_gp_value (output_bfd)
3688 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3689
3690 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3691 }
3692
3693 /* Create and return a local GOT entry for VALUE, which was calculated
3694 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3695 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3696 instead. */
3697
3698 static struct mips_got_entry *
3699 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3700 bfd *ibfd, bfd_vma value,
3701 unsigned long r_symndx,
3702 struct mips_elf_link_hash_entry *h,
3703 int r_type)
3704 {
3705 struct mips_got_entry lookup, *entry;
3706 void **loc;
3707 struct mips_got_info *g;
3708 struct mips_elf_link_hash_table *htab;
3709 bfd_vma gotidx;
3710
3711 htab = mips_elf_hash_table (info);
3712 BFD_ASSERT (htab != NULL);
3713
3714 g = mips_elf_bfd_got (ibfd, FALSE);
3715 if (g == NULL)
3716 {
3717 g = mips_elf_bfd_got (abfd, FALSE);
3718 BFD_ASSERT (g != NULL);
3719 }
3720
3721 /* This function shouldn't be called for symbols that live in the global
3722 area of the GOT. */
3723 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3724
3725 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3726 if (lookup.tls_type)
3727 {
3728 lookup.abfd = ibfd;
3729 if (tls_ldm_reloc_p (r_type))
3730 {
3731 lookup.symndx = 0;
3732 lookup.d.addend = 0;
3733 }
3734 else if (h == NULL)
3735 {
3736 lookup.symndx = r_symndx;
3737 lookup.d.addend = 0;
3738 }
3739 else
3740 {
3741 lookup.symndx = -1;
3742 lookup.d.h = h;
3743 }
3744
3745 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3746 BFD_ASSERT (entry);
3747
3748 gotidx = entry->gotidx;
3749 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3750
3751 return entry;
3752 }
3753
3754 lookup.abfd = NULL;
3755 lookup.symndx = -1;
3756 lookup.d.address = value;
3757 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3758 if (!loc)
3759 return NULL;
3760
3761 entry = (struct mips_got_entry *) *loc;
3762 if (entry)
3763 return entry;
3764
3765 if (g->assigned_low_gotno > g->assigned_high_gotno)
3766 {
3767 /* We didn't allocate enough space in the GOT. */
3768 _bfd_error_handler
3769 (_("not enough GOT space for local GOT entries"));
3770 bfd_set_error (bfd_error_bad_value);
3771 return NULL;
3772 }
3773
3774 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3775 if (!entry)
3776 return NULL;
3777
3778 if (got16_reloc_p (r_type)
3779 || call16_reloc_p (r_type)
3780 || got_page_reloc_p (r_type)
3781 || got_disp_reloc_p (r_type))
3782 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3783 else
3784 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3785
3786 *entry = lookup;
3787 *loc = entry;
3788
3789 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3790
3791 /* These GOT entries need a dynamic relocation on VxWorks. */
3792 if (htab->is_vxworks)
3793 {
3794 Elf_Internal_Rela outrel;
3795 asection *s;
3796 bfd_byte *rloc;
3797 bfd_vma got_address;
3798
3799 s = mips_elf_rel_dyn_section (info, FALSE);
3800 got_address = (htab->root.sgot->output_section->vma
3801 + htab->root.sgot->output_offset
3802 + entry->gotidx);
3803
3804 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3805 outrel.r_offset = got_address;
3806 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3807 outrel.r_addend = value;
3808 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3809 }
3810
3811 return entry;
3812 }
3813
3814 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3815 The number might be exact or a worst-case estimate, depending on how
3816 much information is available to elf_backend_omit_section_dynsym at
3817 the current linking stage. */
3818
3819 static bfd_size_type
3820 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3821 {
3822 bfd_size_type count;
3823
3824 count = 0;
3825 if (bfd_link_pic (info)
3826 || elf_hash_table (info)->is_relocatable_executable)
3827 {
3828 asection *p;
3829 const struct elf_backend_data *bed;
3830
3831 bed = get_elf_backend_data (output_bfd);
3832 for (p = output_bfd->sections; p ; p = p->next)
3833 if ((p->flags & SEC_EXCLUDE) == 0
3834 && (p->flags & SEC_ALLOC) != 0
3835 && elf_hash_table (info)->dynamic_relocs
3836 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3837 ++count;
3838 }
3839 return count;
3840 }
3841
3842 /* Sort the dynamic symbol table so that symbols that need GOT entries
3843 appear towards the end. */
3844
3845 static bfd_boolean
3846 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3847 {
3848 struct mips_elf_link_hash_table *htab;
3849 struct mips_elf_hash_sort_data hsd;
3850 struct mips_got_info *g;
3851
3852 htab = mips_elf_hash_table (info);
3853 BFD_ASSERT (htab != NULL);
3854
3855 if (htab->root.dynsymcount == 0)
3856 return TRUE;
3857
3858 g = htab->got_info;
3859 if (g == NULL)
3860 return TRUE;
3861
3862 hsd.low = NULL;
3863 hsd.max_unref_got_dynindx
3864 = hsd.min_got_dynindx
3865 = (htab->root.dynsymcount - g->reloc_only_gotno);
3866 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3867 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3868 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3869 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3870 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3871
3872 /* There should have been enough room in the symbol table to
3873 accommodate both the GOT and non-GOT symbols. */
3874 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3875 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3876 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3877 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3878
3879 /* Now we know which dynamic symbol has the lowest dynamic symbol
3880 table index in the GOT. */
3881 htab->global_gotsym = hsd.low;
3882
3883 return TRUE;
3884 }
3885
3886 /* If H needs a GOT entry, assign it the highest available dynamic
3887 index. Otherwise, assign it the lowest available dynamic
3888 index. */
3889
3890 static bfd_boolean
3891 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3892 {
3893 struct mips_elf_hash_sort_data *hsd = data;
3894
3895 /* Symbols without dynamic symbol table entries aren't interesting
3896 at all. */
3897 if (h->root.dynindx == -1)
3898 return TRUE;
3899
3900 switch (h->global_got_area)
3901 {
3902 case GGA_NONE:
3903 if (h->root.forced_local)
3904 h->root.dynindx = hsd->max_local_dynindx++;
3905 else
3906 h->root.dynindx = hsd->max_non_got_dynindx++;
3907 break;
3908
3909 case GGA_NORMAL:
3910 h->root.dynindx = --hsd->min_got_dynindx;
3911 hsd->low = (struct elf_link_hash_entry *) h;
3912 break;
3913
3914 case GGA_RELOC_ONLY:
3915 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3916 hsd->low = (struct elf_link_hash_entry *) h;
3917 h->root.dynindx = hsd->max_unref_got_dynindx++;
3918 break;
3919 }
3920
3921 return TRUE;
3922 }
3923
3924 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3925 (which is owned by the caller and shouldn't be added to the
3926 hash table directly). */
3927
3928 static bfd_boolean
3929 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3930 struct mips_got_entry *lookup)
3931 {
3932 struct mips_elf_link_hash_table *htab;
3933 struct mips_got_entry *entry;
3934 struct mips_got_info *g;
3935 void **loc, **bfd_loc;
3936
3937 /* Make sure there's a slot for this entry in the master GOT. */
3938 htab = mips_elf_hash_table (info);
3939 g = htab->got_info;
3940 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3941 if (!loc)
3942 return FALSE;
3943
3944 /* Populate the entry if it isn't already. */
3945 entry = (struct mips_got_entry *) *loc;
3946 if (!entry)
3947 {
3948 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3949 if (!entry)
3950 return FALSE;
3951
3952 lookup->tls_initialized = FALSE;
3953 lookup->gotidx = -1;
3954 *entry = *lookup;
3955 *loc = entry;
3956 }
3957
3958 /* Reuse the same GOT entry for the BFD's GOT. */
3959 g = mips_elf_bfd_got (abfd, TRUE);
3960 if (!g)
3961 return FALSE;
3962
3963 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3964 if (!bfd_loc)
3965 return FALSE;
3966
3967 if (!*bfd_loc)
3968 *bfd_loc = entry;
3969 return TRUE;
3970 }
3971
3972 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3973 entry for it. FOR_CALL is true if the caller is only interested in
3974 using the GOT entry for calls. */
3975
3976 static bfd_boolean
3977 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3978 bfd *abfd, struct bfd_link_info *info,
3979 bfd_boolean for_call, int r_type)
3980 {
3981 struct mips_elf_link_hash_table *htab;
3982 struct mips_elf_link_hash_entry *hmips;
3983 struct mips_got_entry entry;
3984 unsigned char tls_type;
3985
3986 htab = mips_elf_hash_table (info);
3987 BFD_ASSERT (htab != NULL);
3988
3989 hmips = (struct mips_elf_link_hash_entry *) h;
3990 if (!for_call)
3991 hmips->got_only_for_calls = FALSE;
3992
3993 /* A global symbol in the GOT must also be in the dynamic symbol
3994 table. */
3995 if (h->dynindx == -1)
3996 {
3997 switch (ELF_ST_VISIBILITY (h->other))
3998 {
3999 case STV_INTERNAL:
4000 case STV_HIDDEN:
4001 _bfd_mips_elf_hide_symbol (info, h, TRUE);
4002 break;
4003 }
4004 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4005 return FALSE;
4006 }
4007
4008 tls_type = mips_elf_reloc_tls_type (r_type);
4009 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4010 hmips->global_got_area = GGA_NORMAL;
4011
4012 entry.abfd = abfd;
4013 entry.symndx = -1;
4014 entry.d.h = (struct mips_elf_link_hash_entry *) h;
4015 entry.tls_type = tls_type;
4016 return mips_elf_record_got_entry (info, abfd, &entry);
4017 }
4018
4019 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4020 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
4021
4022 static bfd_boolean
4023 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4024 struct bfd_link_info *info, int r_type)
4025 {
4026 struct mips_elf_link_hash_table *htab;
4027 struct mips_got_info *g;
4028 struct mips_got_entry entry;
4029
4030 htab = mips_elf_hash_table (info);
4031 BFD_ASSERT (htab != NULL);
4032
4033 g = htab->got_info;
4034 BFD_ASSERT (g != NULL);
4035
4036 entry.abfd = abfd;
4037 entry.symndx = symndx;
4038 entry.d.addend = addend;
4039 entry.tls_type = mips_elf_reloc_tls_type (r_type);
4040 return mips_elf_record_got_entry (info, abfd, &entry);
4041 }
4042
4043 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4044 H is the symbol's hash table entry, or null if SYMNDX is local
4045 to ABFD. */
4046
4047 static bfd_boolean
4048 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4049 long symndx, struct elf_link_hash_entry *h,
4050 bfd_signed_vma addend)
4051 {
4052 struct mips_elf_link_hash_table *htab;
4053 struct mips_got_info *g1, *g2;
4054 struct mips_got_page_ref lookup, *entry;
4055 void **loc, **bfd_loc;
4056
4057 htab = mips_elf_hash_table (info);
4058 BFD_ASSERT (htab != NULL);
4059
4060 g1 = htab->got_info;
4061 BFD_ASSERT (g1 != NULL);
4062
4063 if (h)
4064 {
4065 lookup.symndx = -1;
4066 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4067 }
4068 else
4069 {
4070 lookup.symndx = symndx;
4071 lookup.u.abfd = abfd;
4072 }
4073 lookup.addend = addend;
4074 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4075 if (loc == NULL)
4076 return FALSE;
4077
4078 entry = (struct mips_got_page_ref *) *loc;
4079 if (!entry)
4080 {
4081 entry = bfd_alloc (abfd, sizeof (*entry));
4082 if (!entry)
4083 return FALSE;
4084
4085 *entry = lookup;
4086 *loc = entry;
4087 }
4088
4089 /* Add the same entry to the BFD's GOT. */
4090 g2 = mips_elf_bfd_got (abfd, TRUE);
4091 if (!g2)
4092 return FALSE;
4093
4094 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4095 if (!bfd_loc)
4096 return FALSE;
4097
4098 if (!*bfd_loc)
4099 *bfd_loc = entry;
4100
4101 return TRUE;
4102 }
4103
4104 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4105
4106 static void
4107 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4108 unsigned int n)
4109 {
4110 asection *s;
4111 struct mips_elf_link_hash_table *htab;
4112
4113 htab = mips_elf_hash_table (info);
4114 BFD_ASSERT (htab != NULL);
4115
4116 s = mips_elf_rel_dyn_section (info, FALSE);
4117 BFD_ASSERT (s != NULL);
4118
4119 if (htab->is_vxworks)
4120 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4121 else
4122 {
4123 if (s->size == 0)
4124 {
4125 /* Make room for a null element. */
4126 s->size += MIPS_ELF_REL_SIZE (abfd);
4127 ++s->reloc_count;
4128 }
4129 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4130 }
4131 }
4132
4133 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4135 mips_elf_traverse_got_arg structure. Count the number of GOT
4136 entries and TLS relocs. Set DATA->value to true if we need
4137 to resolve indirect or warning symbols and then recreate the GOT. */
4138
4139 static int
4140 mips_elf_check_recreate_got (void **entryp, void *data)
4141 {
4142 struct mips_got_entry *entry;
4143 struct mips_elf_traverse_got_arg *arg;
4144
4145 entry = (struct mips_got_entry *) *entryp;
4146 arg = (struct mips_elf_traverse_got_arg *) data;
4147 if (entry->abfd != NULL && entry->symndx == -1)
4148 {
4149 struct mips_elf_link_hash_entry *h;
4150
4151 h = entry->d.h;
4152 if (h->root.root.type == bfd_link_hash_indirect
4153 || h->root.root.type == bfd_link_hash_warning)
4154 {
4155 arg->value = TRUE;
4156 return 0;
4157 }
4158 }
4159 mips_elf_count_got_entry (arg->info, arg->g, entry);
4160 return 1;
4161 }
4162
4163 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4164 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4165 converting entries for indirect and warning symbols into entries
4166 for the target symbol. Set DATA->g to null on error. */
4167
4168 static int
4169 mips_elf_recreate_got (void **entryp, void *data)
4170 {
4171 struct mips_got_entry new_entry, *entry;
4172 struct mips_elf_traverse_got_arg *arg;
4173 void **slot;
4174
4175 entry = (struct mips_got_entry *) *entryp;
4176 arg = (struct mips_elf_traverse_got_arg *) data;
4177 if (entry->abfd != NULL
4178 && entry->symndx == -1
4179 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4180 || entry->d.h->root.root.type == bfd_link_hash_warning))
4181 {
4182 struct mips_elf_link_hash_entry *h;
4183
4184 new_entry = *entry;
4185 entry = &new_entry;
4186 h = entry->d.h;
4187 do
4188 {
4189 BFD_ASSERT (h->global_got_area == GGA_NONE);
4190 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4191 }
4192 while (h->root.root.type == bfd_link_hash_indirect
4193 || h->root.root.type == bfd_link_hash_warning);
4194 entry->d.h = h;
4195 }
4196 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4197 if (slot == NULL)
4198 {
4199 arg->g = NULL;
4200 return 0;
4201 }
4202 if (*slot == NULL)
4203 {
4204 if (entry == &new_entry)
4205 {
4206 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4207 if (!entry)
4208 {
4209 arg->g = NULL;
4210 return 0;
4211 }
4212 *entry = new_entry;
4213 }
4214 *slot = entry;
4215 mips_elf_count_got_entry (arg->info, arg->g, entry);
4216 }
4217 return 1;
4218 }
4219
4220 /* Return the maximum number of GOT page entries required for RANGE. */
4221
4222 static bfd_vma
4223 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4224 {
4225 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4226 }
4227
4228 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4229
4230 static bfd_boolean
4231 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4232 asection *sec, bfd_signed_vma addend)
4233 {
4234 struct mips_got_info *g = arg->g;
4235 struct mips_got_page_entry lookup, *entry;
4236 struct mips_got_page_range **range_ptr, *range;
4237 bfd_vma old_pages, new_pages;
4238 void **loc;
4239
4240 /* Find the mips_got_page_entry hash table entry for this section. */
4241 lookup.sec = sec;
4242 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4243 if (loc == NULL)
4244 return FALSE;
4245
4246 /* Create a mips_got_page_entry if this is the first time we've
4247 seen the section. */
4248 entry = (struct mips_got_page_entry *) *loc;
4249 if (!entry)
4250 {
4251 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4252 if (!entry)
4253 return FALSE;
4254
4255 entry->sec = sec;
4256 *loc = entry;
4257 }
4258
4259 /* Skip over ranges whose maximum extent cannot share a page entry
4260 with ADDEND. */
4261 range_ptr = &entry->ranges;
4262 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4263 range_ptr = &(*range_ptr)->next;
4264
4265 /* If we scanned to the end of the list, or found a range whose
4266 minimum extent cannot share a page entry with ADDEND, create
4267 a new singleton range. */
4268 range = *range_ptr;
4269 if (!range || addend < range->min_addend - 0xffff)
4270 {
4271 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4272 if (!range)
4273 return FALSE;
4274
4275 range->next = *range_ptr;
4276 range->min_addend = addend;
4277 range->max_addend = addend;
4278
4279 *range_ptr = range;
4280 entry->num_pages++;
4281 g->page_gotno++;
4282 return TRUE;
4283 }
4284
4285 /* Remember how many pages the old range contributed. */
4286 old_pages = mips_elf_pages_for_range (range);
4287
4288 /* Update the ranges. */
4289 if (addend < range->min_addend)
4290 range->min_addend = addend;
4291 else if (addend > range->max_addend)
4292 {
4293 if (range->next && addend >= range->next->min_addend - 0xffff)
4294 {
4295 old_pages += mips_elf_pages_for_range (range->next);
4296 range->max_addend = range->next->max_addend;
4297 range->next = range->next->next;
4298 }
4299 else
4300 range->max_addend = addend;
4301 }
4302
4303 /* Record any change in the total estimate. */
4304 new_pages = mips_elf_pages_for_range (range);
4305 if (old_pages != new_pages)
4306 {
4307 entry->num_pages += new_pages - old_pages;
4308 g->page_gotno += new_pages - old_pages;
4309 }
4310
4311 return TRUE;
4312 }
4313
4314 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4315 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4316 whether the page reference described by *REFP needs a GOT page entry,
4317 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4318
4319 static bfd_boolean
4320 mips_elf_resolve_got_page_ref (void **refp, void *data)
4321 {
4322 struct mips_got_page_ref *ref;
4323 struct mips_elf_traverse_got_arg *arg;
4324 struct mips_elf_link_hash_table *htab;
4325 asection *sec;
4326 bfd_vma addend;
4327
4328 ref = (struct mips_got_page_ref *) *refp;
4329 arg = (struct mips_elf_traverse_got_arg *) data;
4330 htab = mips_elf_hash_table (arg->info);
4331
4332 if (ref->symndx < 0)
4333 {
4334 struct mips_elf_link_hash_entry *h;
4335
4336 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4337 h = ref->u.h;
4338 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4339 return 1;
4340
4341 /* Ignore undefined symbols; we'll issue an error later if
4342 appropriate. */
4343 if (!((h->root.root.type == bfd_link_hash_defined
4344 || h->root.root.type == bfd_link_hash_defweak)
4345 && h->root.root.u.def.section))
4346 return 1;
4347
4348 sec = h->root.root.u.def.section;
4349 addend = h->root.root.u.def.value + ref->addend;
4350 }
4351 else
4352 {
4353 Elf_Internal_Sym *isym;
4354
4355 /* Read in the symbol. */
4356 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4357 ref->symndx);
4358 if (isym == NULL)
4359 {
4360 arg->g = NULL;
4361 return 0;
4362 }
4363
4364 /* Get the associated input section. */
4365 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4366 if (sec == NULL)
4367 {
4368 arg->g = NULL;
4369 return 0;
4370 }
4371
4372 /* If this is a mergable section, work out the section and offset
4373 of the merged data. For section symbols, the addend specifies
4374 of the offset _of_ the first byte in the data, otherwise it
4375 specifies the offset _from_ the first byte. */
4376 if (sec->flags & SEC_MERGE)
4377 {
4378 void *secinfo;
4379
4380 secinfo = elf_section_data (sec)->sec_info;
4381 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4382 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4383 isym->st_value + ref->addend);
4384 else
4385 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4386 isym->st_value) + ref->addend;
4387 }
4388 else
4389 addend = isym->st_value + ref->addend;
4390 }
4391 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4392 {
4393 arg->g = NULL;
4394 return 0;
4395 }
4396 return 1;
4397 }
4398
4399 /* If any entries in G->got_entries are for indirect or warning symbols,
4400 replace them with entries for the target symbol. Convert g->got_page_refs
4401 into got_page_entry structures and estimate the number of page entries
4402 that they require. */
4403
4404 static bfd_boolean
4405 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4406 struct mips_got_info *g)
4407 {
4408 struct mips_elf_traverse_got_arg tga;
4409 struct mips_got_info oldg;
4410
4411 oldg = *g;
4412
4413 tga.info = info;
4414 tga.g = g;
4415 tga.value = FALSE;
4416 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4417 if (tga.value)
4418 {
4419 *g = oldg;
4420 g->got_entries = htab_create (htab_size (oldg.got_entries),
4421 mips_elf_got_entry_hash,
4422 mips_elf_got_entry_eq, NULL);
4423 if (!g->got_entries)
4424 return FALSE;
4425
4426 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4427 if (!tga.g)
4428 return FALSE;
4429
4430 htab_delete (oldg.got_entries);
4431 }
4432
4433 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4434 mips_got_page_entry_eq, NULL);
4435 if (g->got_page_entries == NULL)
4436 return FALSE;
4437
4438 tga.info = info;
4439 tga.g = g;
4440 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4441
4442 return TRUE;
4443 }
4444
4445 /* Return true if a GOT entry for H should live in the local rather than
4446 global GOT area. */
4447
4448 static bfd_boolean
4449 mips_use_local_got_p (struct bfd_link_info *info,
4450 struct mips_elf_link_hash_entry *h)
4451 {
4452 /* Symbols that aren't in the dynamic symbol table must live in the
4453 local GOT. This includes symbols that are completely undefined
4454 and which therefore don't bind locally. We'll report undefined
4455 symbols later if appropriate. */
4456 if (h->root.dynindx == -1)
4457 return TRUE;
4458
4459 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4460 to the local GOT, as they would be implicitly relocated by the
4461 base address by the dynamic loader. */
4462 if (bfd_is_abs_symbol (&h->root.root))
4463 return FALSE;
4464
4465 /* Symbols that bind locally can (and in the case of forced-local
4466 symbols, must) live in the local GOT. */
4467 if (h->got_only_for_calls
4468 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4469 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4470 return TRUE;
4471
4472 /* If this is an executable that must provide a definition of the symbol,
4473 either though PLTs or copy relocations, then that address should go in
4474 the local rather than global GOT. */
4475 if (bfd_link_executable (info) && h->has_static_relocs)
4476 return TRUE;
4477
4478 return FALSE;
4479 }
4480
4481 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4482 link_info structure. Decide whether the hash entry needs an entry in
4483 the global part of the primary GOT, setting global_got_area accordingly.
4484 Count the number of global symbols that are in the primary GOT only
4485 because they have relocations against them (reloc_only_gotno). */
4486
4487 static int
4488 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4489 {
4490 struct bfd_link_info *info;
4491 struct mips_elf_link_hash_table *htab;
4492 struct mips_got_info *g;
4493
4494 info = (struct bfd_link_info *) data;
4495 htab = mips_elf_hash_table (info);
4496 g = htab->got_info;
4497 if (h->global_got_area != GGA_NONE)
4498 {
4499 /* Make a final decision about whether the symbol belongs in the
4500 local or global GOT. */
4501 if (mips_use_local_got_p (info, h))
4502 /* The symbol belongs in the local GOT. We no longer need this
4503 entry if it was only used for relocations; those relocations
4504 will be against the null or section symbol instead of H. */
4505 h->global_got_area = GGA_NONE;
4506 else if (htab->is_vxworks
4507 && h->got_only_for_calls
4508 && h->root.plt.plist->mips_offset != MINUS_ONE)
4509 /* On VxWorks, calls can refer directly to the .got.plt entry;
4510 they don't need entries in the regular GOT. .got.plt entries
4511 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4512 h->global_got_area = GGA_NONE;
4513 else if (h->global_got_area == GGA_RELOC_ONLY)
4514 {
4515 g->reloc_only_gotno++;
4516 g->global_gotno++;
4517 }
4518 }
4519 return 1;
4520 }
4521
4522 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4524 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4525
4526 static int
4527 mips_elf_add_got_entry (void **entryp, void *data)
4528 {
4529 struct mips_got_entry *entry;
4530 struct mips_elf_traverse_got_arg *arg;
4531 void **slot;
4532
4533 entry = (struct mips_got_entry *) *entryp;
4534 arg = (struct mips_elf_traverse_got_arg *) data;
4535 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4536 if (!slot)
4537 {
4538 arg->g = NULL;
4539 return 0;
4540 }
4541 if (!*slot)
4542 {
4543 *slot = entry;
4544 mips_elf_count_got_entry (arg->info, arg->g, entry);
4545 }
4546 return 1;
4547 }
4548
4549 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4550 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4551
4552 static int
4553 mips_elf_add_got_page_entry (void **entryp, void *data)
4554 {
4555 struct mips_got_page_entry *entry;
4556 struct mips_elf_traverse_got_arg *arg;
4557 void **slot;
4558
4559 entry = (struct mips_got_page_entry *) *entryp;
4560 arg = (struct mips_elf_traverse_got_arg *) data;
4561 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4562 if (!slot)
4563 {
4564 arg->g = NULL;
4565 return 0;
4566 }
4567 if (!*slot)
4568 {
4569 *slot = entry;
4570 arg->g->page_gotno += entry->num_pages;
4571 }
4572 return 1;
4573 }
4574
4575 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4576 this would lead to overflow, 1 if they were merged successfully,
4577 and 0 if a merge failed due to lack of memory. (These values are chosen
4578 so that nonnegative return values can be returned by a htab_traverse
4579 callback.) */
4580
4581 static int
4582 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4583 struct mips_got_info *to,
4584 struct mips_elf_got_per_bfd_arg *arg)
4585 {
4586 struct mips_elf_traverse_got_arg tga;
4587 unsigned int estimate;
4588
4589 /* Work out how many page entries we would need for the combined GOT. */
4590 estimate = arg->max_pages;
4591 if (estimate >= from->page_gotno + to->page_gotno)
4592 estimate = from->page_gotno + to->page_gotno;
4593
4594 /* And conservatively estimate how many local and TLS entries
4595 would be needed. */
4596 estimate += from->local_gotno + to->local_gotno;
4597 estimate += from->tls_gotno + to->tls_gotno;
4598
4599 /* If we're merging with the primary got, any TLS relocations will
4600 come after the full set of global entries. Otherwise estimate those
4601 conservatively as well. */
4602 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4603 estimate += arg->global_count;
4604 else
4605 estimate += from->global_gotno + to->global_gotno;
4606
4607 /* Bail out if the combined GOT might be too big. */
4608 if (estimate > arg->max_count)
4609 return -1;
4610
4611 /* Transfer the bfd's got information from FROM to TO. */
4612 tga.info = arg->info;
4613 tga.g = to;
4614 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4615 if (!tga.g)
4616 return 0;
4617
4618 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4619 if (!tga.g)
4620 return 0;
4621
4622 mips_elf_replace_bfd_got (abfd, to);
4623 return 1;
4624 }
4625
4626 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4627 as possible of the primary got, since it doesn't require explicit
4628 dynamic relocations, but don't use bfds that would reference global
4629 symbols out of the addressable range. Failing the primary got,
4630 attempt to merge with the current got, or finish the current got
4631 and then make make the new got current. */
4632
4633 static bfd_boolean
4634 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4635 struct mips_elf_got_per_bfd_arg *arg)
4636 {
4637 unsigned int estimate;
4638 int result;
4639
4640 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4641 return FALSE;
4642
4643 /* Work out the number of page, local and TLS entries. */
4644 estimate = arg->max_pages;
4645 if (estimate > g->page_gotno)
4646 estimate = g->page_gotno;
4647 estimate += g->local_gotno + g->tls_gotno;
4648
4649 /* We place TLS GOT entries after both locals and globals. The globals
4650 for the primary GOT may overflow the normal GOT size limit, so be
4651 sure not to merge a GOT which requires TLS with the primary GOT in that
4652 case. This doesn't affect non-primary GOTs. */
4653 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4654
4655 if (estimate <= arg->max_count)
4656 {
4657 /* If we don't have a primary GOT, use it as
4658 a starting point for the primary GOT. */
4659 if (!arg->primary)
4660 {
4661 arg->primary = g;
4662 return TRUE;
4663 }
4664
4665 /* Try merging with the primary GOT. */
4666 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4667 if (result >= 0)
4668 return result;
4669 }
4670
4671 /* If we can merge with the last-created got, do it. */
4672 if (arg->current)
4673 {
4674 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4675 if (result >= 0)
4676 return result;
4677 }
4678
4679 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4680 fits; if it turns out that it doesn't, we'll get relocation
4681 overflows anyway. */
4682 g->next = arg->current;
4683 arg->current = g;
4684
4685 return TRUE;
4686 }
4687
4688 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4689 to GOTIDX, duplicating the entry if it has already been assigned
4690 an index in a different GOT. */
4691
4692 static bfd_boolean
4693 mips_elf_set_gotidx (void **entryp, long gotidx)
4694 {
4695 struct mips_got_entry *entry;
4696
4697 entry = (struct mips_got_entry *) *entryp;
4698 if (entry->gotidx > 0)
4699 {
4700 struct mips_got_entry *new_entry;
4701
4702 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4703 if (!new_entry)
4704 return FALSE;
4705
4706 *new_entry = *entry;
4707 *entryp = new_entry;
4708 entry = new_entry;
4709 }
4710 entry->gotidx = gotidx;
4711 return TRUE;
4712 }
4713
4714 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4715 mips_elf_traverse_got_arg in which DATA->value is the size of one
4716 GOT entry. Set DATA->g to null on failure. */
4717
4718 static int
4719 mips_elf_initialize_tls_index (void **entryp, void *data)
4720 {
4721 struct mips_got_entry *entry;
4722 struct mips_elf_traverse_got_arg *arg;
4723
4724 /* We're only interested in TLS symbols. */
4725 entry = (struct mips_got_entry *) *entryp;
4726 if (entry->tls_type == GOT_TLS_NONE)
4727 return 1;
4728
4729 arg = (struct mips_elf_traverse_got_arg *) data;
4730 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4731 {
4732 arg->g = NULL;
4733 return 0;
4734 }
4735
4736 /* Account for the entries we've just allocated. */
4737 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4738 return 1;
4739 }
4740
4741 /* A htab_traverse callback for GOT entries, where DATA points to a
4742 mips_elf_traverse_got_arg. Set the global_got_area of each global
4743 symbol to DATA->value. */
4744
4745 static int
4746 mips_elf_set_global_got_area (void **entryp, void *data)
4747 {
4748 struct mips_got_entry *entry;
4749 struct mips_elf_traverse_got_arg *arg;
4750
4751 entry = (struct mips_got_entry *) *entryp;
4752 arg = (struct mips_elf_traverse_got_arg *) data;
4753 if (entry->abfd != NULL
4754 && entry->symndx == -1
4755 && entry->d.h->global_got_area != GGA_NONE)
4756 entry->d.h->global_got_area = arg->value;
4757 return 1;
4758 }
4759
4760 /* A htab_traverse callback for secondary GOT entries, where DATA points
4761 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4762 and record the number of relocations they require. DATA->value is
4763 the size of one GOT entry. Set DATA->g to null on failure. */
4764
4765 static int
4766 mips_elf_set_global_gotidx (void **entryp, void *data)
4767 {
4768 struct mips_got_entry *entry;
4769 struct mips_elf_traverse_got_arg *arg;
4770
4771 entry = (struct mips_got_entry *) *entryp;
4772 arg = (struct mips_elf_traverse_got_arg *) data;
4773 if (entry->abfd != NULL
4774 && entry->symndx == -1
4775 && entry->d.h->global_got_area != GGA_NONE)
4776 {
4777 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4778 {
4779 arg->g = NULL;
4780 return 0;
4781 }
4782 arg->g->assigned_low_gotno += 1;
4783
4784 if (bfd_link_pic (arg->info)
4785 || (elf_hash_table (arg->info)->dynamic_sections_created
4786 && entry->d.h->root.def_dynamic
4787 && !entry->d.h->root.def_regular))
4788 arg->g->relocs += 1;
4789 }
4790
4791 return 1;
4792 }
4793
4794 /* A htab_traverse callback for GOT entries for which DATA is the
4795 bfd_link_info. Forbid any global symbols from having traditional
4796 lazy-binding stubs. */
4797
4798 static int
4799 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4800 {
4801 struct bfd_link_info *info;
4802 struct mips_elf_link_hash_table *htab;
4803 struct mips_got_entry *entry;
4804
4805 entry = (struct mips_got_entry *) *entryp;
4806 info = (struct bfd_link_info *) data;
4807 htab = mips_elf_hash_table (info);
4808 BFD_ASSERT (htab != NULL);
4809
4810 if (entry->abfd != NULL
4811 && entry->symndx == -1
4812 && entry->d.h->needs_lazy_stub)
4813 {
4814 entry->d.h->needs_lazy_stub = FALSE;
4815 htab->lazy_stub_count--;
4816 }
4817
4818 return 1;
4819 }
4820
4821 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4822 the primary GOT. */
4823 static bfd_vma
4824 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4825 {
4826 if (!g->next)
4827 return 0;
4828
4829 g = mips_elf_bfd_got (ibfd, FALSE);
4830 if (! g)
4831 return 0;
4832
4833 BFD_ASSERT (g->next);
4834
4835 g = g->next;
4836
4837 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4838 * MIPS_ELF_GOT_SIZE (abfd);
4839 }
4840
4841 /* Turn a single GOT that is too big for 16-bit addressing into
4842 a sequence of GOTs, each one 16-bit addressable. */
4843
4844 static bfd_boolean
4845 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4846 asection *got, bfd_size_type pages)
4847 {
4848 struct mips_elf_link_hash_table *htab;
4849 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4850 struct mips_elf_traverse_got_arg tga;
4851 struct mips_got_info *g, *gg;
4852 unsigned int assign, needed_relocs;
4853 bfd *dynobj, *ibfd;
4854
4855 dynobj = elf_hash_table (info)->dynobj;
4856 htab = mips_elf_hash_table (info);
4857 BFD_ASSERT (htab != NULL);
4858
4859 g = htab->got_info;
4860
4861 got_per_bfd_arg.obfd = abfd;
4862 got_per_bfd_arg.info = info;
4863 got_per_bfd_arg.current = NULL;
4864 got_per_bfd_arg.primary = NULL;
4865 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4866 / MIPS_ELF_GOT_SIZE (abfd))
4867 - htab->reserved_gotno);
4868 got_per_bfd_arg.max_pages = pages;
4869 /* The number of globals that will be included in the primary GOT.
4870 See the calls to mips_elf_set_global_got_area below for more
4871 information. */
4872 got_per_bfd_arg.global_count = g->global_gotno;
4873
4874 /* Try to merge the GOTs of input bfds together, as long as they
4875 don't seem to exceed the maximum GOT size, choosing one of them
4876 to be the primary GOT. */
4877 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4878 {
4879 gg = mips_elf_bfd_got (ibfd, FALSE);
4880 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4881 return FALSE;
4882 }
4883
4884 /* If we do not find any suitable primary GOT, create an empty one. */
4885 if (got_per_bfd_arg.primary == NULL)
4886 g->next = mips_elf_create_got_info (abfd);
4887 else
4888 g->next = got_per_bfd_arg.primary;
4889 g->next->next = got_per_bfd_arg.current;
4890
4891 /* GG is now the master GOT, and G is the primary GOT. */
4892 gg = g;
4893 g = g->next;
4894
4895 /* Map the output bfd to the primary got. That's what we're going
4896 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4897 didn't mark in check_relocs, and we want a quick way to find it.
4898 We can't just use gg->next because we're going to reverse the
4899 list. */
4900 mips_elf_replace_bfd_got (abfd, g);
4901
4902 /* Every symbol that is referenced in a dynamic relocation must be
4903 present in the primary GOT, so arrange for them to appear after
4904 those that are actually referenced. */
4905 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4906 g->global_gotno = gg->global_gotno;
4907
4908 tga.info = info;
4909 tga.value = GGA_RELOC_ONLY;
4910 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4911 tga.value = GGA_NORMAL;
4912 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
4913
4914 /* Now go through the GOTs assigning them offset ranges.
4915 [assigned_low_gotno, local_gotno[ will be set to the range of local
4916 entries in each GOT. We can then compute the end of a GOT by
4917 adding local_gotno to global_gotno. We reverse the list and make
4918 it circular since then we'll be able to quickly compute the
4919 beginning of a GOT, by computing the end of its predecessor. To
4920 avoid special cases for the primary GOT, while still preserving
4921 assertions that are valid for both single- and multi-got links,
4922 we arrange for the main got struct to have the right number of
4923 global entries, but set its local_gotno such that the initial
4924 offset of the primary GOT is zero. Remember that the primary GOT
4925 will become the last item in the circular linked list, so it
4926 points back to the master GOT. */
4927 gg->local_gotno = -g->global_gotno;
4928 gg->global_gotno = g->global_gotno;
4929 gg->tls_gotno = 0;
4930 assign = 0;
4931 gg->next = gg;
4932
4933 do
4934 {
4935 struct mips_got_info *gn;
4936
4937 assign += htab->reserved_gotno;
4938 g->assigned_low_gotno = assign;
4939 g->local_gotno += assign;
4940 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4941 g->assigned_high_gotno = g->local_gotno - 1;
4942 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4943
4944 /* Take g out of the direct list, and push it onto the reversed
4945 list that gg points to. g->next is guaranteed to be nonnull after
4946 this operation, as required by mips_elf_initialize_tls_index. */
4947 gn = g->next;
4948 g->next = gg->next;
4949 gg->next = g;
4950
4951 /* Set up any TLS entries. We always place the TLS entries after
4952 all non-TLS entries. */
4953 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4954 tga.g = g;
4955 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4956 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4957 if (!tga.g)
4958 return FALSE;
4959 BFD_ASSERT (g->tls_assigned_gotno == assign);
4960
4961 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4962 g = gn;
4963
4964 /* Forbid global symbols in every non-primary GOT from having
4965 lazy-binding stubs. */
4966 if (g)
4967 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4968 }
4969 while (g);
4970
4971 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
4972
4973 needed_relocs = 0;
4974 for (g = gg->next; g && g->next != gg; g = g->next)
4975 {
4976 unsigned int save_assign;
4977
4978 /* Assign offsets to global GOT entries and count how many
4979 relocations they need. */
4980 save_assign = g->assigned_low_gotno;
4981 g->assigned_low_gotno = g->local_gotno;
4982 tga.info = info;
4983 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4984 tga.g = g;
4985 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
4986 if (!tga.g)
4987 return FALSE;
4988 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
4989 g->assigned_low_gotno = save_assign;
4990
4991 if (bfd_link_pic (info))
4992 {
4993 g->relocs += g->local_gotno - g->assigned_low_gotno;
4994 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
4995 + g->next->global_gotno
4996 + g->next->tls_gotno
4997 + htab->reserved_gotno);
4998 }
4999 needed_relocs += g->relocs;
5000 }
5001 needed_relocs += g->relocs;
5002
5003 if (needed_relocs)
5004 mips_elf_allocate_dynamic_relocations (dynobj, info,
5005 needed_relocs);
5006
5007 return TRUE;
5008 }
5009
5010
5011 /* Returns the first relocation of type r_type found, beginning with
5013 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5014
5015 static const Elf_Internal_Rela *
5016 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5017 const Elf_Internal_Rela *relocation,
5018 const Elf_Internal_Rela *relend)
5019 {
5020 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5021
5022 while (relocation < relend)
5023 {
5024 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5025 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5026 return relocation;
5027
5028 ++relocation;
5029 }
5030
5031 /* We didn't find it. */
5032 return NULL;
5033 }
5034
5035 /* Return whether an input relocation is against a local symbol. */
5036
5037 static bfd_boolean
5038 mips_elf_local_relocation_p (bfd *input_bfd,
5039 const Elf_Internal_Rela *relocation,
5040 asection **local_sections)
5041 {
5042 unsigned long r_symndx;
5043 Elf_Internal_Shdr *symtab_hdr;
5044 size_t extsymoff;
5045
5046 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5047 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5048 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5049
5050 if (r_symndx < extsymoff)
5051 return TRUE;
5052 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5053 return TRUE;
5054
5055 return FALSE;
5056 }
5057
5058 /* Sign-extend VALUE, which has the indicated number of BITS. */
5060
5061 bfd_vma
5062 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5063 {
5064 if (value & ((bfd_vma) 1 << (bits - 1)))
5065 /* VALUE is negative. */
5066 value |= ((bfd_vma) - 1) << bits;
5067
5068 return value;
5069 }
5070
5071 /* Return non-zero if the indicated VALUE has overflowed the maximum
5072 range expressible by a signed number with the indicated number of
5073 BITS. */
5074
5075 static bfd_boolean
5076 mips_elf_overflow_p (bfd_vma value, int bits)
5077 {
5078 bfd_signed_vma svalue = (bfd_signed_vma) value;
5079
5080 if (svalue > (1 << (bits - 1)) - 1)
5081 /* The value is too big. */
5082 return TRUE;
5083 else if (svalue < -(1 << (bits - 1)))
5084 /* The value is too small. */
5085 return TRUE;
5086
5087 /* All is well. */
5088 return FALSE;
5089 }
5090
5091 /* Calculate the %high function. */
5092
5093 static bfd_vma
5094 mips_elf_high (bfd_vma value)
5095 {
5096 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5097 }
5098
5099 /* Calculate the %higher function. */
5100
5101 static bfd_vma
5102 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5103 {
5104 #ifdef BFD64
5105 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5106 #else
5107 abort ();
5108 return MINUS_ONE;
5109 #endif
5110 }
5111
5112 /* Calculate the %highest function. */
5113
5114 static bfd_vma
5115 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5116 {
5117 #ifdef BFD64
5118 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5119 #else
5120 abort ();
5121 return MINUS_ONE;
5122 #endif
5123 }
5124
5125 /* Create the .compact_rel section. */
5127
5128 static bfd_boolean
5129 mips_elf_create_compact_rel_section
5130 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5131 {
5132 flagword flags;
5133 register asection *s;
5134
5135 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5136 {
5137 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5138 | SEC_READONLY);
5139
5140 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5141 if (s == NULL
5142 || ! bfd_set_section_alignment (abfd, s,
5143 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5144 return FALSE;
5145
5146 s->size = sizeof (Elf32_External_compact_rel);
5147 }
5148
5149 return TRUE;
5150 }
5151
5152 /* Create the .got section to hold the global offset table. */
5153
5154 static bfd_boolean
5155 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5156 {
5157 flagword flags;
5158 register asection *s;
5159 struct elf_link_hash_entry *h;
5160 struct bfd_link_hash_entry *bh;
5161 struct mips_elf_link_hash_table *htab;
5162
5163 htab = mips_elf_hash_table (info);
5164 BFD_ASSERT (htab != NULL);
5165
5166 /* This function may be called more than once. */
5167 if (htab->root.sgot)
5168 return TRUE;
5169
5170 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5171 | SEC_LINKER_CREATED);
5172
5173 /* We have to use an alignment of 2**4 here because this is hardcoded
5174 in the function stub generation and in the linker script. */
5175 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5176 if (s == NULL
5177 || ! bfd_set_section_alignment (abfd, s, 4))
5178 return FALSE;
5179 htab->root.sgot = s;
5180
5181 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5182 linker script because we don't want to define the symbol if we
5183 are not creating a global offset table. */
5184 bh = NULL;
5185 if (! (_bfd_generic_link_add_one_symbol
5186 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5187 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5188 return FALSE;
5189
5190 h = (struct elf_link_hash_entry *) bh;
5191 h->non_elf = 0;
5192 h->def_regular = 1;
5193 h->type = STT_OBJECT;
5194 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5195 elf_hash_table (info)->hgot = h;
5196
5197 if (bfd_link_pic (info)
5198 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5199 return FALSE;
5200
5201 htab->got_info = mips_elf_create_got_info (abfd);
5202 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5203 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5204
5205 /* We also need a .got.plt section when generating PLTs. */
5206 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5207 SEC_ALLOC | SEC_LOAD
5208 | SEC_HAS_CONTENTS
5209 | SEC_IN_MEMORY
5210 | SEC_LINKER_CREATED);
5211 if (s == NULL)
5212 return FALSE;
5213 htab->root.sgotplt = s;
5214
5215 return TRUE;
5216 }
5217
5218 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5220 __GOTT_INDEX__ symbols. These symbols are only special for
5221 shared objects; they are not used in executables. */
5222
5223 static bfd_boolean
5224 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5225 {
5226 return (mips_elf_hash_table (info)->is_vxworks
5227 && bfd_link_pic (info)
5228 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5229 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5230 }
5231
5232 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5233 require an la25 stub. See also mips_elf_local_pic_function_p,
5234 which determines whether the destination function ever requires a
5235 stub. */
5236
5237 static bfd_boolean
5238 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5239 bfd_boolean target_is_16_bit_code_p)
5240 {
5241 /* We specifically ignore branches and jumps from EF_PIC objects,
5242 where the onus is on the compiler or programmer to perform any
5243 necessary initialization of $25. Sometimes such initialization
5244 is unnecessary; for example, -mno-shared functions do not use
5245 the incoming value of $25, and may therefore be called directly. */
5246 if (PIC_OBJECT_P (input_bfd))
5247 return FALSE;
5248
5249 switch (r_type)
5250 {
5251 case R_MIPS_26:
5252 case R_MIPS_PC16:
5253 case R_MIPS_PC21_S2:
5254 case R_MIPS_PC26_S2:
5255 case R_MICROMIPS_26_S1:
5256 case R_MICROMIPS_PC7_S1:
5257 case R_MICROMIPS_PC10_S1:
5258 case R_MICROMIPS_PC16_S1:
5259 case R_MICROMIPS_PC23_S2:
5260 return TRUE;
5261
5262 case R_MIPS16_26:
5263 return !target_is_16_bit_code_p;
5264
5265 default:
5266 return FALSE;
5267 }
5268 }
5269
5270 /* Obtain the field relocated by RELOCATION. */
5272
5273 static bfd_vma
5274 mips_elf_obtain_contents (reloc_howto_type *howto,
5275 const Elf_Internal_Rela *relocation,
5276 bfd *input_bfd, bfd_byte *contents)
5277 {
5278 bfd_vma x = 0;
5279 bfd_byte *location = contents + relocation->r_offset;
5280 unsigned int size = bfd_get_reloc_size (howto);
5281
5282 /* Obtain the bytes. */
5283 if (size != 0)
5284 x = bfd_get (8 * size, input_bfd, location);
5285
5286 return x;
5287 }
5288
5289 /* Store the field relocated by RELOCATION. */
5290
5291 static void
5292 mips_elf_store_contents (reloc_howto_type *howto,
5293 const Elf_Internal_Rela *relocation,
5294 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5295 {
5296 bfd_byte *location = contents + relocation->r_offset;
5297 unsigned int size = bfd_get_reloc_size (howto);
5298
5299 /* Put the value into the output. */
5300 if (size != 0)
5301 bfd_put (8 * size, input_bfd, x, location);
5302 }
5303
5304 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
5305 RELOCATION described by HOWTO, with a move of 0 to the load target
5306 register, returning TRUE if that is successful and FALSE otherwise.
5307 If DOIT is FALSE, then only determine it patching is possible and
5308 return status without actually changing CONTENTS.
5309 */
5310
5311 static bfd_boolean
5312 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5313 const Elf_Internal_Rela *relocation,
5314 reloc_howto_type *howto, bfd_boolean doit)
5315 {
5316 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5317 bfd_byte *location = contents + relocation->r_offset;
5318 bfd_boolean nullified = TRUE;
5319 bfd_vma x;
5320
5321 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5322
5323 /* Obtain the current value. */
5324 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5325
5326 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5327 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5328 if (mips16_reloc_p (r_type)
5329 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5330 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
5331 x = (0x3cd << 22) | (x & (7 << 16)) << 3; /* LI */
5332 else if (micromips_reloc_p (r_type)
5333 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5334 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5335 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5336 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5337 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5338 else
5339 nullified = FALSE;
5340
5341 /* Put the value into the output. */
5342 if (doit && nullified)
5343 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5344
5345 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, FALSE, location);
5346
5347 return nullified;
5348 }
5349
5350 /* Calculate the value produced by the RELOCATION (which comes from
5351 the INPUT_BFD). The ADDEND is the addend to use for this
5352 RELOCATION; RELOCATION->R_ADDEND is ignored.
5353
5354 The result of the relocation calculation is stored in VALUEP.
5355 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5356 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5357
5358 This function returns bfd_reloc_continue if the caller need take no
5359 further action regarding this relocation, bfd_reloc_notsupported if
5360 something goes dramatically wrong, bfd_reloc_overflow if an
5361 overflow occurs, and bfd_reloc_ok to indicate success. */
5362
5363 static bfd_reloc_status_type
5364 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5365 asection *input_section, bfd_byte *contents,
5366 struct bfd_link_info *info,
5367 const Elf_Internal_Rela *relocation,
5368 bfd_vma addend, reloc_howto_type *howto,
5369 Elf_Internal_Sym *local_syms,
5370 asection **local_sections, bfd_vma *valuep,
5371 const char **namep,
5372 bfd_boolean *cross_mode_jump_p,
5373 bfd_boolean save_addend)
5374 {
5375 /* The eventual value we will return. */
5376 bfd_vma value;
5377 /* The address of the symbol against which the relocation is
5378 occurring. */
5379 bfd_vma symbol = 0;
5380 /* The final GP value to be used for the relocatable, executable, or
5381 shared object file being produced. */
5382 bfd_vma gp;
5383 /* The place (section offset or address) of the storage unit being
5384 relocated. */
5385 bfd_vma p;
5386 /* The value of GP used to create the relocatable object. */
5387 bfd_vma gp0;
5388 /* The offset into the global offset table at which the address of
5389 the relocation entry symbol, adjusted by the addend, resides
5390 during execution. */
5391 bfd_vma g = MINUS_ONE;
5392 /* The section in which the symbol referenced by the relocation is
5393 located. */
5394 asection *sec = NULL;
5395 struct mips_elf_link_hash_entry *h = NULL;
5396 /* TRUE if the symbol referred to by this relocation is a local
5397 symbol. */
5398 bfd_boolean local_p, was_local_p;
5399 /* TRUE if the symbol referred to by this relocation is a section
5400 symbol. */
5401 bfd_boolean section_p = FALSE;
5402 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5403 bfd_boolean gp_disp_p = FALSE;
5404 /* TRUE if the symbol referred to by this relocation is
5405 "__gnu_local_gp". */
5406 bfd_boolean gnu_local_gp_p = FALSE;
5407 Elf_Internal_Shdr *symtab_hdr;
5408 size_t extsymoff;
5409 unsigned long r_symndx;
5410 int r_type;
5411 /* TRUE if overflow occurred during the calculation of the
5412 relocation value. */
5413 bfd_boolean overflowed_p;
5414 /* TRUE if this relocation refers to a MIPS16 function. */
5415 bfd_boolean target_is_16_bit_code_p = FALSE;
5416 bfd_boolean target_is_micromips_code_p = FALSE;
5417 struct mips_elf_link_hash_table *htab;
5418 bfd *dynobj;
5419 bfd_boolean resolved_to_zero;
5420
5421 dynobj = elf_hash_table (info)->dynobj;
5422 htab = mips_elf_hash_table (info);
5423 BFD_ASSERT (htab != NULL);
5424
5425 /* Parse the relocation. */
5426 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5427 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5428 p = (input_section->output_section->vma
5429 + input_section->output_offset
5430 + relocation->r_offset);
5431
5432 /* Assume that there will be no overflow. */
5433 overflowed_p = FALSE;
5434
5435 /* Figure out whether or not the symbol is local, and get the offset
5436 used in the array of hash table entries. */
5437 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5438 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5439 local_sections);
5440 was_local_p = local_p;
5441 if (! elf_bad_symtab (input_bfd))
5442 extsymoff = symtab_hdr->sh_info;
5443 else
5444 {
5445 /* The symbol table does not follow the rule that local symbols
5446 must come before globals. */
5447 extsymoff = 0;
5448 }
5449
5450 /* Figure out the value of the symbol. */
5451 if (local_p)
5452 {
5453 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5454 Elf_Internal_Sym *sym;
5455
5456 sym = local_syms + r_symndx;
5457 sec = local_sections[r_symndx];
5458
5459 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5460
5461 symbol = sec->output_section->vma + sec->output_offset;
5462 if (!section_p || (sec->flags & SEC_MERGE))
5463 symbol += sym->st_value;
5464 if ((sec->flags & SEC_MERGE) && section_p)
5465 {
5466 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5467 addend -= symbol;
5468 addend += sec->output_section->vma + sec->output_offset;
5469 }
5470
5471 /* MIPS16/microMIPS text labels should be treated as odd. */
5472 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5473 ++symbol;
5474
5475 /* Record the name of this symbol, for our caller. */
5476 *namep = bfd_elf_string_from_elf_section (input_bfd,
5477 symtab_hdr->sh_link,
5478 sym->st_name);
5479 if (*namep == NULL || **namep == '\0')
5480 *namep = bfd_section_name (input_bfd, sec);
5481
5482 /* For relocations against a section symbol and ones against no
5483 symbol (absolute relocations) infer the ISA mode from the addend. */
5484 if (section_p || r_symndx == STN_UNDEF)
5485 {
5486 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5487 target_is_micromips_code_p = (addend & 1) && micromips_p;
5488 }
5489 /* For relocations against an absolute symbol infer the ISA mode
5490 from the value of the symbol plus addend. */
5491 else if (bfd_is_abs_section (sec))
5492 {
5493 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5494 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5495 }
5496 /* Otherwise just use the regular symbol annotation available. */
5497 else
5498 {
5499 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5500 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5501 }
5502 }
5503 else
5504 {
5505 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5506
5507 /* For global symbols we look up the symbol in the hash-table. */
5508 h = ((struct mips_elf_link_hash_entry *)
5509 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5510 /* Find the real hash-table entry for this symbol. */
5511 while (h->root.root.type == bfd_link_hash_indirect
5512 || h->root.root.type == bfd_link_hash_warning)
5513 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5514
5515 /* Record the name of this symbol, for our caller. */
5516 *namep = h->root.root.root.string;
5517
5518 /* See if this is the special _gp_disp symbol. Note that such a
5519 symbol must always be a global symbol. */
5520 if (strcmp (*namep, "_gp_disp") == 0
5521 && ! NEWABI_P (input_bfd))
5522 {
5523 /* Relocations against _gp_disp are permitted only with
5524 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5525 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5526 return bfd_reloc_notsupported;
5527
5528 gp_disp_p = TRUE;
5529 }
5530 /* See if this is the special _gp symbol. Note that such a
5531 symbol must always be a global symbol. */
5532 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5533 gnu_local_gp_p = TRUE;
5534
5535
5536 /* If this symbol is defined, calculate its address. Note that
5537 _gp_disp is a magic symbol, always implicitly defined by the
5538 linker, so it's inappropriate to check to see whether or not
5539 its defined. */
5540 else if ((h->root.root.type == bfd_link_hash_defined
5541 || h->root.root.type == bfd_link_hash_defweak)
5542 && h->root.root.u.def.section)
5543 {
5544 sec = h->root.root.u.def.section;
5545 if (sec->output_section)
5546 symbol = (h->root.root.u.def.value
5547 + sec->output_section->vma
5548 + sec->output_offset);
5549 else
5550 symbol = h->root.root.u.def.value;
5551 }
5552 else if (h->root.root.type == bfd_link_hash_undefweak)
5553 /* We allow relocations against undefined weak symbols, giving
5554 it the value zero, so that you can undefined weak functions
5555 and check to see if they exist by looking at their
5556 addresses. */
5557 symbol = 0;
5558 else if (info->unresolved_syms_in_objects == RM_IGNORE
5559 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5560 symbol = 0;
5561 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5562 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5563 {
5564 /* If this is a dynamic link, we should have created a
5565 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5566 in _bfd_mips_elf_create_dynamic_sections.
5567 Otherwise, we should define the symbol with a value of 0.
5568 FIXME: It should probably get into the symbol table
5569 somehow as well. */
5570 BFD_ASSERT (! bfd_link_pic (info));
5571 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5572 symbol = 0;
5573 }
5574 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5575 {
5576 /* This is an optional symbol - an Irix specific extension to the
5577 ELF spec. Ignore it for now.
5578 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5579 than simply ignoring them, but we do not handle this for now.
5580 For information see the "64-bit ELF Object File Specification"
5581 which is available from here:
5582 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5583 symbol = 0;
5584 }
5585 else
5586 {
5587 bfd_boolean reject_undefined
5588 = (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
5589 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5590
5591 (*info->callbacks->undefined_symbol)
5592 (info, h->root.root.root.string, input_bfd,
5593 input_section, relocation->r_offset, reject_undefined);
5594
5595 if (reject_undefined)
5596 return bfd_reloc_undefined;
5597
5598 symbol = 0;
5599 }
5600
5601 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5602 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5603 }
5604
5605 /* If this is a reference to a 16-bit function with a stub, we need
5606 to redirect the relocation to the stub unless:
5607
5608 (a) the relocation is for a MIPS16 JAL;
5609
5610 (b) the relocation is for a MIPS16 PIC call, and there are no
5611 non-MIPS16 uses of the GOT slot; or
5612
5613 (c) the section allows direct references to MIPS16 functions. */
5614 if (r_type != R_MIPS16_26
5615 && !bfd_link_relocatable (info)
5616 && ((h != NULL
5617 && h->fn_stub != NULL
5618 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5619 || (local_p
5620 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5621 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5622 && !section_allows_mips16_refs_p (input_section))
5623 {
5624 /* This is a 32- or 64-bit call to a 16-bit function. We should
5625 have already noticed that we were going to need the
5626 stub. */
5627 if (local_p)
5628 {
5629 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5630 value = 0;
5631 }
5632 else
5633 {
5634 BFD_ASSERT (h->need_fn_stub);
5635 if (h->la25_stub)
5636 {
5637 /* If a LA25 header for the stub itself exists, point to the
5638 prepended LUI/ADDIU sequence. */
5639 sec = h->la25_stub->stub_section;
5640 value = h->la25_stub->offset;
5641 }
5642 else
5643 {
5644 sec = h->fn_stub;
5645 value = 0;
5646 }
5647 }
5648
5649 symbol = sec->output_section->vma + sec->output_offset + value;
5650 /* The target is 16-bit, but the stub isn't. */
5651 target_is_16_bit_code_p = FALSE;
5652 }
5653 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5654 to a standard MIPS function, we need to redirect the call to the stub.
5655 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5656 indirect calls should use an indirect stub instead. */
5657 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5658 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5659 || (local_p
5660 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5661 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5662 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5663 {
5664 if (local_p)
5665 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5666 else
5667 {
5668 /* If both call_stub and call_fp_stub are defined, we can figure
5669 out which one to use by checking which one appears in the input
5670 file. */
5671 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5672 {
5673 asection *o;
5674
5675 sec = NULL;
5676 for (o = input_bfd->sections; o != NULL; o = o->next)
5677 {
5678 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5679 {
5680 sec = h->call_fp_stub;
5681 break;
5682 }
5683 }
5684 if (sec == NULL)
5685 sec = h->call_stub;
5686 }
5687 else if (h->call_stub != NULL)
5688 sec = h->call_stub;
5689 else
5690 sec = h->call_fp_stub;
5691 }
5692
5693 BFD_ASSERT (sec->size > 0);
5694 symbol = sec->output_section->vma + sec->output_offset;
5695 }
5696 /* If this is a direct call to a PIC function, redirect to the
5697 non-PIC stub. */
5698 else if (h != NULL && h->la25_stub
5699 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5700 target_is_16_bit_code_p))
5701 {
5702 symbol = (h->la25_stub->stub_section->output_section->vma
5703 + h->la25_stub->stub_section->output_offset
5704 + h->la25_stub->offset);
5705 if (ELF_ST_IS_MICROMIPS (h->root.other))
5706 symbol |= 1;
5707 }
5708 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5709 entry is used if a standard PLT entry has also been made. In this
5710 case the symbol will have been set by mips_elf_set_plt_sym_value
5711 to point to the standard PLT entry, so redirect to the compressed
5712 one. */
5713 else if ((mips16_branch_reloc_p (r_type)
5714 || micromips_branch_reloc_p (r_type))
5715 && !bfd_link_relocatable (info)
5716 && h != NULL
5717 && h->use_plt_entry
5718 && h->root.plt.plist->comp_offset != MINUS_ONE
5719 && h->root.plt.plist->mips_offset != MINUS_ONE)
5720 {
5721 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5722
5723 sec = htab->root.splt;
5724 symbol = (sec->output_section->vma
5725 + sec->output_offset
5726 + htab->plt_header_size
5727 + htab->plt_mips_offset
5728 + h->root.plt.plist->comp_offset
5729 + 1);
5730
5731 target_is_16_bit_code_p = !micromips_p;
5732 target_is_micromips_code_p = micromips_p;
5733 }
5734
5735 /* Make sure MIPS16 and microMIPS are not used together. */
5736 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5737 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5738 {
5739 _bfd_error_handler
5740 (_("MIPS16 and microMIPS functions cannot call each other"));
5741 return bfd_reloc_notsupported;
5742 }
5743
5744 /* Calls from 16-bit code to 32-bit code and vice versa require the
5745 mode change. However, we can ignore calls to undefined weak symbols,
5746 which should never be executed at runtime. This exception is important
5747 because the assembly writer may have "known" that any definition of the
5748 symbol would be 16-bit code, and that direct jumps were therefore
5749 acceptable. */
5750 *cross_mode_jump_p = (!bfd_link_relocatable (info)
5751 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5752 && ((mips16_branch_reloc_p (r_type)
5753 && !target_is_16_bit_code_p)
5754 || (micromips_branch_reloc_p (r_type)
5755 && !target_is_micromips_code_p)
5756 || ((branch_reloc_p (r_type)
5757 || r_type == R_MIPS_JALR)
5758 && (target_is_16_bit_code_p
5759 || target_is_micromips_code_p))));
5760
5761 resolved_to_zero = (h != NULL
5762 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5763
5764 switch (r_type)
5765 {
5766 case R_MIPS16_CALL16:
5767 case R_MIPS16_GOT16:
5768 case R_MIPS_CALL16:
5769 case R_MIPS_GOT16:
5770 case R_MIPS_GOT_PAGE:
5771 case R_MIPS_GOT_DISP:
5772 case R_MIPS_GOT_LO16:
5773 case R_MIPS_CALL_LO16:
5774 case R_MICROMIPS_CALL16:
5775 case R_MICROMIPS_GOT16:
5776 case R_MICROMIPS_GOT_PAGE:
5777 case R_MICROMIPS_GOT_DISP:
5778 case R_MICROMIPS_GOT_LO16:
5779 case R_MICROMIPS_CALL_LO16:
5780 if (resolved_to_zero
5781 && !bfd_link_relocatable (info)
5782 && mips_elf_nullify_got_load (input_bfd, contents,
5783 relocation, howto, TRUE))
5784 return bfd_reloc_continue;
5785
5786 /* Fall through. */
5787 case R_MIPS_GOT_HI16:
5788 case R_MIPS_CALL_HI16:
5789 case R_MICROMIPS_GOT_HI16:
5790 case R_MICROMIPS_CALL_HI16:
5791 if (resolved_to_zero
5792 && htab->use_absolute_zero
5793 && bfd_link_pic (info))
5794 {
5795 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5796 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5797 FALSE, FALSE, FALSE);
5798 BFD_ASSERT (h != NULL);
5799 }
5800 break;
5801 }
5802
5803 local_p = (h == NULL || mips_use_local_got_p (info, h));
5804
5805 gp0 = _bfd_get_gp_value (input_bfd);
5806 gp = _bfd_get_gp_value (abfd);
5807 if (htab->got_info)
5808 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5809
5810 if (gnu_local_gp_p)
5811 symbol = gp;
5812
5813 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5814 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5815 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5816 if (got_page_reloc_p (r_type) && !local_p)
5817 {
5818 r_type = (micromips_reloc_p (r_type)
5819 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5820 addend = 0;
5821 }
5822
5823 /* If we haven't already determined the GOT offset, and we're going
5824 to need it, get it now. */
5825 switch (r_type)
5826 {
5827 case R_MIPS16_CALL16:
5828 case R_MIPS16_GOT16:
5829 case R_MIPS_CALL16:
5830 case R_MIPS_GOT16:
5831 case R_MIPS_GOT_DISP:
5832 case R_MIPS_GOT_HI16:
5833 case R_MIPS_CALL_HI16:
5834 case R_MIPS_GOT_LO16:
5835 case R_MIPS_CALL_LO16:
5836 case R_MICROMIPS_CALL16:
5837 case R_MICROMIPS_GOT16:
5838 case R_MICROMIPS_GOT_DISP:
5839 case R_MICROMIPS_GOT_HI16:
5840 case R_MICROMIPS_CALL_HI16:
5841 case R_MICROMIPS_GOT_LO16:
5842 case R_MICROMIPS_CALL_LO16:
5843 case R_MIPS_TLS_GD:
5844 case R_MIPS_TLS_GOTTPREL:
5845 case R_MIPS_TLS_LDM:
5846 case R_MIPS16_TLS_GD:
5847 case R_MIPS16_TLS_GOTTPREL:
5848 case R_MIPS16_TLS_LDM:
5849 case R_MICROMIPS_TLS_GD:
5850 case R_MICROMIPS_TLS_GOTTPREL:
5851 case R_MICROMIPS_TLS_LDM:
5852 /* Find the index into the GOT where this value is located. */
5853 if (tls_ldm_reloc_p (r_type))
5854 {
5855 g = mips_elf_local_got_index (abfd, input_bfd, info,
5856 0, 0, NULL, r_type);
5857 if (g == MINUS_ONE)
5858 return bfd_reloc_outofrange;
5859 }
5860 else if (!local_p)
5861 {
5862 /* On VxWorks, CALL relocations should refer to the .got.plt
5863 entry, which is initialized to point at the PLT stub. */
5864 if (htab->is_vxworks
5865 && (call_hi16_reloc_p (r_type)
5866 || call_lo16_reloc_p (r_type)
5867 || call16_reloc_p (r_type)))
5868 {
5869 BFD_ASSERT (addend == 0);
5870 BFD_ASSERT (h->root.needs_plt);
5871 g = mips_elf_gotplt_index (info, &h->root);
5872 }
5873 else
5874 {
5875 BFD_ASSERT (addend == 0);
5876 g = mips_elf_global_got_index (abfd, info, input_bfd,
5877 &h->root, r_type);
5878 if (!TLS_RELOC_P (r_type)
5879 && !elf_hash_table (info)->dynamic_sections_created)
5880 /* This is a static link. We must initialize the GOT entry. */
5881 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
5882 }
5883 }
5884 else if (!htab->is_vxworks
5885 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5886 /* The calculation below does not involve "g". */
5887 break;
5888 else
5889 {
5890 g = mips_elf_local_got_index (abfd, input_bfd, info,
5891 symbol + addend, r_symndx, h, r_type);
5892 if (g == MINUS_ONE)
5893 return bfd_reloc_outofrange;
5894 }
5895
5896 /* Convert GOT indices to actual offsets. */
5897 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5898 break;
5899 }
5900
5901 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5902 symbols are resolved by the loader. Add them to .rela.dyn. */
5903 if (h != NULL && is_gott_symbol (info, &h->root))
5904 {
5905 Elf_Internal_Rela outrel;
5906 bfd_byte *loc;
5907 asection *s;
5908
5909 s = mips_elf_rel_dyn_section (info, FALSE);
5910 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5911
5912 outrel.r_offset = (input_section->output_section->vma
5913 + input_section->output_offset
5914 + relocation->r_offset);
5915 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5916 outrel.r_addend = addend;
5917 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5918
5919 /* If we've written this relocation for a readonly section,
5920 we need to set DF_TEXTREL again, so that we do not delete the
5921 DT_TEXTREL tag. */
5922 if (MIPS_ELF_READONLY_SECTION (input_section))
5923 info->flags |= DF_TEXTREL;
5924
5925 *valuep = 0;
5926 return bfd_reloc_ok;
5927 }
5928
5929 /* Figure out what kind of relocation is being performed. */
5930 switch (r_type)
5931 {
5932 case R_MIPS_NONE:
5933 return bfd_reloc_continue;
5934
5935 case R_MIPS_16:
5936 if (howto->partial_inplace)
5937 addend = _bfd_mips_elf_sign_extend (addend, 16);
5938 value = symbol + addend;
5939 overflowed_p = mips_elf_overflow_p (value, 16);
5940 break;
5941
5942 case R_MIPS_32:
5943 case R_MIPS_REL32:
5944 case R_MIPS_64:
5945 if ((bfd_link_pic (info)
5946 || (htab->root.dynamic_sections_created
5947 && h != NULL
5948 && h->root.def_dynamic
5949 && !h->root.def_regular
5950 && !h->has_static_relocs))
5951 && r_symndx != STN_UNDEF
5952 && (h == NULL
5953 || h->root.root.type != bfd_link_hash_undefweak
5954 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
5955 && !resolved_to_zero))
5956 && (input_section->flags & SEC_ALLOC) != 0)
5957 {
5958 /* If we're creating a shared library, then we can't know
5959 where the symbol will end up. So, we create a relocation
5960 record in the output, and leave the job up to the dynamic
5961 linker. We must do the same for executable references to
5962 shared library symbols, unless we've decided to use copy
5963 relocs or PLTs instead. */
5964 value = addend;
5965 if (!mips_elf_create_dynamic_relocation (abfd,
5966 info,
5967 relocation,
5968 h,
5969 sec,
5970 symbol,
5971 &value,
5972 input_section))
5973 return bfd_reloc_undefined;
5974 }
5975 else
5976 {
5977 if (r_type != R_MIPS_REL32)
5978 value = symbol + addend;
5979 else
5980 value = addend;
5981 }
5982 value &= howto->dst_mask;
5983 break;
5984
5985 case R_MIPS_PC32:
5986 value = symbol + addend - p;
5987 value &= howto->dst_mask;
5988 break;
5989
5990 case R_MIPS16_26:
5991 /* The calculation for R_MIPS16_26 is just the same as for an
5992 R_MIPS_26. It's only the storage of the relocated field into
5993 the output file that's different. That's handled in
5994 mips_elf_perform_relocation. So, we just fall through to the
5995 R_MIPS_26 case here. */
5996 case R_MIPS_26:
5997 case R_MICROMIPS_26_S1:
5998 {
5999 unsigned int shift;
6000
6001 /* Shift is 2, unusually, for microMIPS JALX. */
6002 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6003
6004 if (howto->partial_inplace && !section_p)
6005 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6006 else
6007 value = addend;
6008 value += symbol;
6009
6010 /* Make sure the target of a jump is suitably aligned. Bit 0 must
6011 be the correct ISA mode selector except for weak undefined
6012 symbols. */
6013 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6014 && (*cross_mode_jump_p
6015 ? (value & 3) != (r_type == R_MIPS_26)
6016 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6017 return bfd_reloc_outofrange;
6018
6019 value >>= shift;
6020 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6021 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6022 value &= howto->dst_mask;
6023 }
6024 break;
6025
6026 case R_MIPS_TLS_DTPREL_HI16:
6027 case R_MIPS16_TLS_DTPREL_HI16:
6028 case R_MICROMIPS_TLS_DTPREL_HI16:
6029 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6030 & howto->dst_mask);
6031 break;
6032
6033 case R_MIPS_TLS_DTPREL_LO16:
6034 case R_MIPS_TLS_DTPREL32:
6035 case R_MIPS_TLS_DTPREL64:
6036 case R_MIPS16_TLS_DTPREL_LO16:
6037 case R_MICROMIPS_TLS_DTPREL_LO16:
6038 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6039 break;
6040
6041 case R_MIPS_TLS_TPREL_HI16:
6042 case R_MIPS16_TLS_TPREL_HI16:
6043 case R_MICROMIPS_TLS_TPREL_HI16:
6044 value = (mips_elf_high (addend + symbol - tprel_base (info))
6045 & howto->dst_mask);
6046 break;
6047
6048 case R_MIPS_TLS_TPREL_LO16:
6049 case R_MIPS_TLS_TPREL32:
6050 case R_MIPS_TLS_TPREL64:
6051 case R_MIPS16_TLS_TPREL_LO16:
6052 case R_MICROMIPS_TLS_TPREL_LO16:
6053 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6054 break;
6055
6056 case R_MIPS_HI16:
6057 case R_MIPS16_HI16:
6058 case R_MICROMIPS_HI16:
6059 if (!gp_disp_p)
6060 {
6061 value = mips_elf_high (addend + symbol);
6062 value &= howto->dst_mask;
6063 }
6064 else
6065 {
6066 /* For MIPS16 ABI code we generate this sequence
6067 0: li $v0,%hi(_gp_disp)
6068 4: addiupc $v1,%lo(_gp_disp)
6069 8: sll $v0,16
6070 12: addu $v0,$v1
6071 14: move $gp,$v0
6072 So the offsets of hi and lo relocs are the same, but the
6073 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6074 ADDIUPC clears the low two bits of the instruction address,
6075 so the base is ($t9 + 4) & ~3. */
6076 if (r_type == R_MIPS16_HI16)
6077 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6078 /* The microMIPS .cpload sequence uses the same assembly
6079 instructions as the traditional psABI version, but the
6080 incoming $t9 has the low bit set. */
6081 else if (r_type == R_MICROMIPS_HI16)
6082 value = mips_elf_high (addend + gp - p - 1);
6083 else
6084 value = mips_elf_high (addend + gp - p);
6085 }
6086 break;
6087
6088 case R_MIPS_LO16:
6089 case R_MIPS16_LO16:
6090 case R_MICROMIPS_LO16:
6091 case R_MICROMIPS_HI0_LO16:
6092 if (!gp_disp_p)
6093 value = (symbol + addend) & howto->dst_mask;
6094 else
6095 {
6096 /* See the comment for R_MIPS16_HI16 above for the reason
6097 for this conditional. */
6098 if (r_type == R_MIPS16_LO16)
6099 value = addend + gp - (p & ~(bfd_vma) 0x3);
6100 else if (r_type == R_MICROMIPS_LO16
6101 || r_type == R_MICROMIPS_HI0_LO16)
6102 value = addend + gp - p + 3;
6103 else
6104 value = addend + gp - p + 4;
6105 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6106 for overflow. But, on, say, IRIX5, relocations against
6107 _gp_disp are normally generated from the .cpload
6108 pseudo-op. It generates code that normally looks like
6109 this:
6110
6111 lui $gp,%hi(_gp_disp)
6112 addiu $gp,$gp,%lo(_gp_disp)
6113 addu $gp,$gp,$t9
6114
6115 Here $t9 holds the address of the function being called,
6116 as required by the MIPS ELF ABI. The R_MIPS_LO16
6117 relocation can easily overflow in this situation, but the
6118 R_MIPS_HI16 relocation will handle the overflow.
6119 Therefore, we consider this a bug in the MIPS ABI, and do
6120 not check for overflow here. */
6121 }
6122 break;
6123
6124 case R_MIPS_LITERAL:
6125 case R_MICROMIPS_LITERAL:
6126 /* Because we don't merge literal sections, we can handle this
6127 just like R_MIPS_GPREL16. In the long run, we should merge
6128 shared literals, and then we will need to additional work
6129 here. */
6130
6131 /* Fall through. */
6132
6133 case R_MIPS16_GPREL:
6134 /* The R_MIPS16_GPREL performs the same calculation as
6135 R_MIPS_GPREL16, but stores the relocated bits in a different
6136 order. We don't need to do anything special here; the
6137 differences are handled in mips_elf_perform_relocation. */
6138 case R_MIPS_GPREL16:
6139 case R_MICROMIPS_GPREL7_S2:
6140 case R_MICROMIPS_GPREL16:
6141 /* Only sign-extend the addend if it was extracted from the
6142 instruction. If the addend was separate, leave it alone,
6143 otherwise we may lose significant bits. */
6144 if (howto->partial_inplace)
6145 addend = _bfd_mips_elf_sign_extend (addend, 16);
6146 value = symbol + addend - gp;
6147 /* If the symbol was local, any earlier relocatable links will
6148 have adjusted its addend with the gp offset, so compensate
6149 for that now. Don't do it for symbols forced local in this
6150 link, though, since they won't have had the gp offset applied
6151 to them before. */
6152 if (was_local_p)
6153 value += gp0;
6154 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6155 overflowed_p = mips_elf_overflow_p (value, 16);
6156 break;
6157
6158 case R_MIPS16_GOT16:
6159 case R_MIPS16_CALL16:
6160 case R_MIPS_GOT16:
6161 case R_MIPS_CALL16:
6162 case R_MICROMIPS_GOT16:
6163 case R_MICROMIPS_CALL16:
6164 /* VxWorks does not have separate local and global semantics for
6165 R_MIPS*_GOT16; every relocation evaluates to "G". */
6166 if (!htab->is_vxworks && local_p)
6167 {
6168 value = mips_elf_got16_entry (abfd, input_bfd, info,
6169 symbol + addend, !was_local_p);
6170 if (value == MINUS_ONE)
6171 return bfd_reloc_outofrange;
6172 value
6173 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6174 overflowed_p = mips_elf_overflow_p (value, 16);
6175 break;
6176 }
6177
6178 /* Fall through. */
6179
6180 case R_MIPS_TLS_GD:
6181 case R_MIPS_TLS_GOTTPREL:
6182 case R_MIPS_TLS_LDM:
6183 case R_MIPS_GOT_DISP:
6184 case R_MIPS16_TLS_GD:
6185 case R_MIPS16_TLS_GOTTPREL:
6186 case R_MIPS16_TLS_LDM:
6187 case R_MICROMIPS_TLS_GD:
6188 case R_MICROMIPS_TLS_GOTTPREL:
6189 case R_MICROMIPS_TLS_LDM:
6190 case R_MICROMIPS_GOT_DISP:
6191 value = g;
6192 overflowed_p = mips_elf_overflow_p (value, 16);
6193 break;
6194
6195 case R_MIPS_GPREL32:
6196 value = (addend + symbol + gp0 - gp);
6197 if (!save_addend)
6198 value &= howto->dst_mask;
6199 break;
6200
6201 case R_MIPS_PC16:
6202 case R_MIPS_GNU_REL16_S2:
6203 if (howto->partial_inplace)
6204 addend = _bfd_mips_elf_sign_extend (addend, 18);
6205
6206 /* No need to exclude weak undefined symbols here as they resolve
6207 to 0 and never set `*cross_mode_jump_p', so this alignment check
6208 will never trigger for them. */
6209 if (*cross_mode_jump_p
6210 ? ((symbol + addend) & 3) != 1
6211 : ((symbol + addend) & 3) != 0)
6212 return bfd_reloc_outofrange;
6213
6214 value = symbol + addend - p;
6215 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6216 overflowed_p = mips_elf_overflow_p (value, 18);
6217 value >>= howto->rightshift;
6218 value &= howto->dst_mask;
6219 break;
6220
6221 case R_MIPS16_PC16_S1:
6222 if (howto->partial_inplace)
6223 addend = _bfd_mips_elf_sign_extend (addend, 17);
6224
6225 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6226 && (*cross_mode_jump_p
6227 ? ((symbol + addend) & 3) != 0
6228 : ((symbol + addend) & 1) == 0))
6229 return bfd_reloc_outofrange;
6230
6231 value = symbol + addend - p;
6232 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6233 overflowed_p = mips_elf_overflow_p (value, 17);
6234 value >>= howto->rightshift;
6235 value &= howto->dst_mask;
6236 break;
6237
6238 case R_MIPS_PC21_S2:
6239 if (howto->partial_inplace)
6240 addend = _bfd_mips_elf_sign_extend (addend, 23);
6241
6242 if ((symbol + addend) & 3)
6243 return bfd_reloc_outofrange;
6244
6245 value = symbol + addend - p;
6246 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6247 overflowed_p = mips_elf_overflow_p (value, 23);
6248 value >>= howto->rightshift;
6249 value &= howto->dst_mask;
6250 break;
6251
6252 case R_MIPS_PC26_S2:
6253 if (howto->partial_inplace)
6254 addend = _bfd_mips_elf_sign_extend (addend, 28);
6255
6256 if ((symbol + addend) & 3)
6257 return bfd_reloc_outofrange;
6258
6259 value = symbol + addend - p;
6260 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6261 overflowed_p = mips_elf_overflow_p (value, 28);
6262 value >>= howto->rightshift;
6263 value &= howto->dst_mask;
6264 break;
6265
6266 case R_MIPS_PC18_S3:
6267 if (howto->partial_inplace)
6268 addend = _bfd_mips_elf_sign_extend (addend, 21);
6269
6270 if ((symbol + addend) & 7)
6271 return bfd_reloc_outofrange;
6272
6273 value = symbol + addend - ((p | 7) ^ 7);
6274 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6275 overflowed_p = mips_elf_overflow_p (value, 21);
6276 value >>= howto->rightshift;
6277 value &= howto->dst_mask;
6278 break;
6279
6280 case R_MIPS_PC19_S2:
6281 if (howto->partial_inplace)
6282 addend = _bfd_mips_elf_sign_extend (addend, 21);
6283
6284 if ((symbol + addend) & 3)
6285 return bfd_reloc_outofrange;
6286
6287 value = symbol + addend - p;
6288 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6289 overflowed_p = mips_elf_overflow_p (value, 21);
6290 value >>= howto->rightshift;
6291 value &= howto->dst_mask;
6292 break;
6293
6294 case R_MIPS_PCHI16:
6295 value = mips_elf_high (symbol + addend - p);
6296 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6297 overflowed_p = mips_elf_overflow_p (value, 16);
6298 value &= howto->dst_mask;
6299 break;
6300
6301 case R_MIPS_PCLO16:
6302 if (howto->partial_inplace)
6303 addend = _bfd_mips_elf_sign_extend (addend, 16);
6304 value = symbol + addend - p;
6305 value &= howto->dst_mask;
6306 break;
6307
6308 case R_MICROMIPS_PC7_S1:
6309 if (howto->partial_inplace)
6310 addend = _bfd_mips_elf_sign_extend (addend, 8);
6311
6312 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6313 && (*cross_mode_jump_p
6314 ? ((symbol + addend + 2) & 3) != 0
6315 : ((symbol + addend + 2) & 1) == 0))
6316 return bfd_reloc_outofrange;
6317
6318 value = symbol + addend - p;
6319 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6320 overflowed_p = mips_elf_overflow_p (value, 8);
6321 value >>= howto->rightshift;
6322 value &= howto->dst_mask;
6323 break;
6324
6325 case R_MICROMIPS_PC10_S1:
6326 if (howto->partial_inplace)
6327 addend = _bfd_mips_elf_sign_extend (addend, 11);
6328
6329 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6330 && (*cross_mode_jump_p
6331 ? ((symbol + addend + 2) & 3) != 0
6332 : ((symbol + addend + 2) & 1) == 0))
6333 return bfd_reloc_outofrange;
6334
6335 value = symbol + addend - p;
6336 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6337 overflowed_p = mips_elf_overflow_p (value, 11);
6338 value >>= howto->rightshift;
6339 value &= howto->dst_mask;
6340 break;
6341
6342 case R_MICROMIPS_PC16_S1:
6343 if (howto->partial_inplace)
6344 addend = _bfd_mips_elf_sign_extend (addend, 17);
6345
6346 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6347 && (*cross_mode_jump_p
6348 ? ((symbol + addend) & 3) != 0
6349 : ((symbol + addend) & 1) == 0))
6350 return bfd_reloc_outofrange;
6351
6352 value = symbol + addend - p;
6353 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6354 overflowed_p = mips_elf_overflow_p (value, 17);
6355 value >>= howto->rightshift;
6356 value &= howto->dst_mask;
6357 break;
6358
6359 case R_MICROMIPS_PC23_S2:
6360 if (howto->partial_inplace)
6361 addend = _bfd_mips_elf_sign_extend (addend, 25);
6362 value = symbol + addend - ((p | 3) ^ 3);
6363 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6364 overflowed_p = mips_elf_overflow_p (value, 25);
6365 value >>= howto->rightshift;
6366 value &= howto->dst_mask;
6367 break;
6368
6369 case R_MIPS_GOT_HI16:
6370 case R_MIPS_CALL_HI16:
6371 case R_MICROMIPS_GOT_HI16:
6372 case R_MICROMIPS_CALL_HI16:
6373 /* We're allowed to handle these two relocations identically.
6374 The dynamic linker is allowed to handle the CALL relocations
6375 differently by creating a lazy evaluation stub. */
6376 value = g;
6377 value = mips_elf_high (value);
6378 value &= howto->dst_mask;
6379 break;
6380
6381 case R_MIPS_GOT_LO16:
6382 case R_MIPS_CALL_LO16:
6383 case R_MICROMIPS_GOT_LO16:
6384 case R_MICROMIPS_CALL_LO16:
6385 value = g & howto->dst_mask;
6386 break;
6387
6388 case R_MIPS_GOT_PAGE:
6389 case R_MICROMIPS_GOT_PAGE:
6390 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6391 if (value == MINUS_ONE)
6392 return bfd_reloc_outofrange;
6393 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6394 overflowed_p = mips_elf_overflow_p (value, 16);
6395 break;
6396
6397 case R_MIPS_GOT_OFST:
6398 case R_MICROMIPS_GOT_OFST:
6399 if (local_p)
6400 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6401 else
6402 value = addend;
6403 overflowed_p = mips_elf_overflow_p (value, 16);
6404 break;
6405
6406 case R_MIPS_SUB:
6407 case R_MICROMIPS_SUB:
6408 value = symbol - addend;
6409 value &= howto->dst_mask;
6410 break;
6411
6412 case R_MIPS_HIGHER:
6413 case R_MICROMIPS_HIGHER:
6414 value = mips_elf_higher (addend + symbol);
6415 value &= howto->dst_mask;
6416 break;
6417
6418 case R_MIPS_HIGHEST:
6419 case R_MICROMIPS_HIGHEST:
6420 value = mips_elf_highest (addend + symbol);
6421 value &= howto->dst_mask;
6422 break;
6423
6424 case R_MIPS_SCN_DISP:
6425 case R_MICROMIPS_SCN_DISP:
6426 value = symbol + addend - sec->output_offset;
6427 value &= howto->dst_mask;
6428 break;
6429
6430 case R_MIPS_JALR:
6431 case R_MICROMIPS_JALR:
6432 /* This relocation is only a hint. In some cases, we optimize
6433 it into a bal instruction. But we don't try to optimize
6434 when the symbol does not resolve locally. */
6435 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6436 return bfd_reloc_continue;
6437 /* We can't optimize cross-mode jumps either. */
6438 if (*cross_mode_jump_p)
6439 return bfd_reloc_continue;
6440 value = symbol + addend;
6441 /* Neither we can non-instruction-aligned targets. */
6442 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6443 return bfd_reloc_continue;
6444 break;
6445
6446 case R_MIPS_PJUMP:
6447 case R_MIPS_GNU_VTINHERIT:
6448 case R_MIPS_GNU_VTENTRY:
6449 /* We don't do anything with these at present. */
6450 return bfd_reloc_continue;
6451
6452 default:
6453 /* An unrecognized relocation type. */
6454 return bfd_reloc_notsupported;
6455 }
6456
6457 /* Store the VALUE for our caller. */
6458 *valuep = value;
6459 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6460 }
6461
6462 /* It has been determined that the result of the RELOCATION is the
6463 VALUE. Use HOWTO to place VALUE into the output file at the
6464 appropriate position. The SECTION is the section to which the
6465 relocation applies.
6466 CROSS_MODE_JUMP_P is true if the relocation field
6467 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6468
6469 Returns FALSE if anything goes wrong. */
6470
6471 static bfd_boolean
6472 mips_elf_perform_relocation (struct bfd_link_info *info,
6473 reloc_howto_type *howto,
6474 const Elf_Internal_Rela *relocation,
6475 bfd_vma value, bfd *input_bfd,
6476 asection *input_section, bfd_byte *contents,
6477 bfd_boolean cross_mode_jump_p)
6478 {
6479 bfd_vma x;
6480 bfd_byte *location;
6481 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6482
6483 /* Figure out where the relocation is occurring. */
6484 location = contents + relocation->r_offset;
6485
6486 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
6487
6488 /* Obtain the current value. */
6489 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6490
6491 /* Clear the field we are setting. */
6492 x &= ~howto->dst_mask;
6493
6494 /* Set the field. */
6495 x |= (value & howto->dst_mask);
6496
6497 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
6498 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6499 {
6500 bfd_vma opcode = x >> 26;
6501
6502 if (r_type == R_MIPS16_26 ? opcode == 0x7
6503 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6504 : opcode == 0x1d)
6505 {
6506 info->callbacks->einfo
6507 (_("%X%H: unsupported JALX to the same ISA mode\n"),
6508 input_bfd, input_section, relocation->r_offset);
6509 return TRUE;
6510 }
6511 }
6512 if (cross_mode_jump_p && jal_reloc_p (r_type))
6513 {
6514 bfd_boolean ok;
6515 bfd_vma opcode = x >> 26;
6516 bfd_vma jalx_opcode;
6517
6518 /* Check to see if the opcode is already JAL or JALX. */
6519 if (r_type == R_MIPS16_26)
6520 {
6521 ok = ((opcode == 0x6) || (opcode == 0x7));
6522 jalx_opcode = 0x7;
6523 }
6524 else if (r_type == R_MICROMIPS_26_S1)
6525 {
6526 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6527 jalx_opcode = 0x3c;
6528 }
6529 else
6530 {
6531 ok = ((opcode == 0x3) || (opcode == 0x1d));
6532 jalx_opcode = 0x1d;
6533 }
6534
6535 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6536 convert J or JALS to JALX. */
6537 if (!ok)
6538 {
6539 info->callbacks->einfo
6540 (_("%X%H: unsupported jump between ISA modes; "
6541 "consider recompiling with interlinking enabled\n"),
6542 input_bfd, input_section, relocation->r_offset);
6543 return TRUE;
6544 }
6545
6546 /* Make this the JALX opcode. */
6547 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6548 }
6549 else if (cross_mode_jump_p && b_reloc_p (r_type))
6550 {
6551 bfd_boolean ok = FALSE;
6552 bfd_vma opcode = x >> 16;
6553 bfd_vma jalx_opcode = 0;
6554 bfd_vma sign_bit = 0;
6555 bfd_vma addr;
6556 bfd_vma dest;
6557
6558 if (r_type == R_MICROMIPS_PC16_S1)
6559 {
6560 ok = opcode == 0x4060;
6561 jalx_opcode = 0x3c;
6562 sign_bit = 0x10000;
6563 value <<= 1;
6564 }
6565 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6566 {
6567 ok = opcode == 0x411;
6568 jalx_opcode = 0x1d;
6569 sign_bit = 0x20000;
6570 value <<= 2;
6571 }
6572
6573 if (ok && !bfd_link_pic (info))
6574 {
6575 addr = (input_section->output_section->vma
6576 + input_section->output_offset
6577 + relocation->r_offset
6578 + 4);
6579 dest = (addr
6580 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6581
6582 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6583 {
6584 info->callbacks->einfo
6585 (_("%X%H: cannot convert branch between ISA modes "
6586 "to JALX: relocation out of range\n"),
6587 input_bfd, input_section, relocation->r_offset);
6588 return TRUE;
6589 }
6590
6591 /* Make this the JALX opcode. */
6592 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6593 }
6594 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6595 {
6596 info->callbacks->einfo
6597 (_("%X%H: unsupported branch between ISA modes\n"),
6598 input_bfd, input_section, relocation->r_offset);
6599 return TRUE;
6600 }
6601 }
6602
6603 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6604 range. */
6605 if (!bfd_link_relocatable (info)
6606 && !cross_mode_jump_p
6607 && ((JAL_TO_BAL_P (input_bfd)
6608 && r_type == R_MIPS_26
6609 && (x >> 26) == 0x3) /* jal addr */
6610 || (JALR_TO_BAL_P (input_bfd)
6611 && r_type == R_MIPS_JALR
6612 && x == 0x0320f809) /* jalr t9 */
6613 || (JR_TO_B_P (input_bfd)
6614 && r_type == R_MIPS_JALR
6615 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6616 {
6617 bfd_vma addr;
6618 bfd_vma dest;
6619 bfd_signed_vma off;
6620
6621 addr = (input_section->output_section->vma
6622 + input_section->output_offset
6623 + relocation->r_offset
6624 + 4);
6625 if (r_type == R_MIPS_26)
6626 dest = (value << 2) | ((addr >> 28) << 28);
6627 else
6628 dest = value;
6629 off = dest - addr;
6630 if (off <= 0x1ffff && off >= -0x20000)
6631 {
6632 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
6633 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6634 else
6635 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6636 }
6637 }
6638
6639 /* Put the value into the output. */
6640 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6641
6642 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6643 location);
6644
6645 return TRUE;
6646 }
6647
6648 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6650 is the original relocation, which is now being transformed into a
6651 dynamic relocation. The ADDENDP is adjusted if necessary; the
6652 caller should store the result in place of the original addend. */
6653
6654 static bfd_boolean
6655 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6656 struct bfd_link_info *info,
6657 const Elf_Internal_Rela *rel,
6658 struct mips_elf_link_hash_entry *h,
6659 asection *sec, bfd_vma symbol,
6660 bfd_vma *addendp, asection *input_section)
6661 {
6662 Elf_Internal_Rela outrel[3];
6663 asection *sreloc;
6664 bfd *dynobj;
6665 int r_type;
6666 long indx;
6667 bfd_boolean defined_p;
6668 struct mips_elf_link_hash_table *htab;
6669
6670 htab = mips_elf_hash_table (info);
6671 BFD_ASSERT (htab != NULL);
6672
6673 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6674 dynobj = elf_hash_table (info)->dynobj;
6675 sreloc = mips_elf_rel_dyn_section (info, FALSE);
6676 BFD_ASSERT (sreloc != NULL);
6677 BFD_ASSERT (sreloc->contents != NULL);
6678 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6679 < sreloc->size);
6680
6681 outrel[0].r_offset =
6682 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6683 if (ABI_64_P (output_bfd))
6684 {
6685 outrel[1].r_offset =
6686 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6687 outrel[2].r_offset =
6688 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6689 }
6690
6691 if (outrel[0].r_offset == MINUS_ONE)
6692 /* The relocation field has been deleted. */
6693 return TRUE;
6694
6695 if (outrel[0].r_offset == MINUS_TWO)
6696 {
6697 /* The relocation field has been converted into a relative value of
6698 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6699 the field to be fully relocated, so add in the symbol's value. */
6700 *addendp += symbol;
6701 return TRUE;
6702 }
6703
6704 /* We must now calculate the dynamic symbol table index to use
6705 in the relocation. */
6706 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6707 {
6708 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
6709 indx = h->root.dynindx;
6710 if (SGI_COMPAT (output_bfd))
6711 defined_p = h->root.def_regular;
6712 else
6713 /* ??? glibc's ld.so just adds the final GOT entry to the
6714 relocation field. It therefore treats relocs against
6715 defined symbols in the same way as relocs against
6716 undefined symbols. */
6717 defined_p = FALSE;
6718 }
6719 else
6720 {
6721 if (sec != NULL && bfd_is_abs_section (sec))
6722 indx = 0;
6723 else if (sec == NULL || sec->owner == NULL)
6724 {
6725 bfd_set_error (bfd_error_bad_value);
6726 return FALSE;
6727 }
6728 else
6729 {
6730 indx = elf_section_data (sec->output_section)->dynindx;
6731 if (indx == 0)
6732 {
6733 asection *osec = htab->root.text_index_section;
6734 indx = elf_section_data (osec)->dynindx;
6735 }
6736 if (indx == 0)
6737 abort ();
6738 }
6739
6740 /* Instead of generating a relocation using the section
6741 symbol, we may as well make it a fully relative
6742 relocation. We want to avoid generating relocations to
6743 local symbols because we used to generate them
6744 incorrectly, without adding the original symbol value,
6745 which is mandated by the ABI for section symbols. In
6746 order to give dynamic loaders and applications time to
6747 phase out the incorrect use, we refrain from emitting
6748 section-relative relocations. It's not like they're
6749 useful, after all. This should be a bit more efficient
6750 as well. */
6751 /* ??? Although this behavior is compatible with glibc's ld.so,
6752 the ABI says that relocations against STN_UNDEF should have
6753 a symbol value of 0. Irix rld honors this, so relocations
6754 against STN_UNDEF have no effect. */
6755 if (!SGI_COMPAT (output_bfd))
6756 indx = 0;
6757 defined_p = TRUE;
6758 }
6759
6760 /* If the relocation was previously an absolute relocation and
6761 this symbol will not be referred to by the relocation, we must
6762 adjust it by the value we give it in the dynamic symbol table.
6763 Otherwise leave the job up to the dynamic linker. */
6764 if (defined_p && r_type != R_MIPS_REL32)
6765 *addendp += symbol;
6766
6767 if (htab->is_vxworks)
6768 /* VxWorks uses non-relative relocations for this. */
6769 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6770 else
6771 /* The relocation is always an REL32 relocation because we don't
6772 know where the shared library will wind up at load-time. */
6773 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6774 R_MIPS_REL32);
6775
6776 /* For strict adherence to the ABI specification, we should
6777 generate a R_MIPS_64 relocation record by itself before the
6778 _REL32/_64 record as well, such that the addend is read in as
6779 a 64-bit value (REL32 is a 32-bit relocation, after all).
6780 However, since none of the existing ELF64 MIPS dynamic
6781 loaders seems to care, we don't waste space with these
6782 artificial relocations. If this turns out to not be true,
6783 mips_elf_allocate_dynamic_relocation() should be tweaked so
6784 as to make room for a pair of dynamic relocations per
6785 invocation if ABI_64_P, and here we should generate an
6786 additional relocation record with R_MIPS_64 by itself for a
6787 NULL symbol before this relocation record. */
6788 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6789 ABI_64_P (output_bfd)
6790 ? R_MIPS_64
6791 : R_MIPS_NONE);
6792 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6793
6794 /* Adjust the output offset of the relocation to reference the
6795 correct location in the output file. */
6796 outrel[0].r_offset += (input_section->output_section->vma
6797 + input_section->output_offset);
6798 outrel[1].r_offset += (input_section->output_section->vma
6799 + input_section->output_offset);
6800 outrel[2].r_offset += (input_section->output_section->vma
6801 + input_section->output_offset);
6802
6803 /* Put the relocation back out. We have to use the special
6804 relocation outputter in the 64-bit case since the 64-bit
6805 relocation format is non-standard. */
6806 if (ABI_64_P (output_bfd))
6807 {
6808 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6809 (output_bfd, &outrel[0],
6810 (sreloc->contents
6811 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6812 }
6813 else if (htab->is_vxworks)
6814 {
6815 /* VxWorks uses RELA rather than REL dynamic relocations. */
6816 outrel[0].r_addend = *addendp;
6817 bfd_elf32_swap_reloca_out
6818 (output_bfd, &outrel[0],
6819 (sreloc->contents
6820 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6821 }
6822 else
6823 bfd_elf32_swap_reloc_out
6824 (output_bfd, &outrel[0],
6825 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6826
6827 /* We've now added another relocation. */
6828 ++sreloc->reloc_count;
6829
6830 /* Make sure the output section is writable. The dynamic linker
6831 will be writing to it. */
6832 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6833 |= SHF_WRITE;
6834
6835 /* On IRIX5, make an entry of compact relocation info. */
6836 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6837 {
6838 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6839 bfd_byte *cr;
6840
6841 if (scpt)
6842 {
6843 Elf32_crinfo cptrel;
6844
6845 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6846 cptrel.vaddr = (rel->r_offset
6847 + input_section->output_section->vma
6848 + input_section->output_offset);
6849 if (r_type == R_MIPS_REL32)
6850 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6851 else
6852 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6853 mips_elf_set_cr_dist2to (cptrel, 0);
6854 cptrel.konst = *addendp;
6855
6856 cr = (scpt->contents
6857 + sizeof (Elf32_External_compact_rel));
6858 mips_elf_set_cr_relvaddr (cptrel, 0);
6859 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6860 ((Elf32_External_crinfo *) cr
6861 + scpt->reloc_count));
6862 ++scpt->reloc_count;
6863 }
6864 }
6865
6866 /* If we've written this relocation for a readonly section,
6867 we need to set DF_TEXTREL again, so that we do not delete the
6868 DT_TEXTREL tag. */
6869 if (MIPS_ELF_READONLY_SECTION (input_section))
6870 info->flags |= DF_TEXTREL;
6871
6872 return TRUE;
6873 }
6874
6875 /* Return the MACH for a MIPS e_flags value. */
6877
6878 unsigned long
6879 _bfd_elf_mips_mach (flagword flags)
6880 {
6881 switch (flags & EF_MIPS_MACH)
6882 {
6883 case E_MIPS_MACH_3900:
6884 return bfd_mach_mips3900;
6885
6886 case E_MIPS_MACH_4010:
6887 return bfd_mach_mips4010;
6888
6889 case E_MIPS_MACH_4100:
6890 return bfd_mach_mips4100;
6891
6892 case E_MIPS_MACH_4111:
6893 return bfd_mach_mips4111;
6894
6895 case E_MIPS_MACH_4120:
6896 return bfd_mach_mips4120;
6897
6898 case E_MIPS_MACH_4650:
6899 return bfd_mach_mips4650;
6900
6901 case E_MIPS_MACH_5400:
6902 return bfd_mach_mips5400;
6903
6904 case E_MIPS_MACH_5500:
6905 return bfd_mach_mips5500;
6906
6907 case E_MIPS_MACH_5900:
6908 return bfd_mach_mips5900;
6909
6910 case E_MIPS_MACH_9000:
6911 return bfd_mach_mips9000;
6912
6913 case E_MIPS_MACH_SB1:
6914 return bfd_mach_mips_sb1;
6915
6916 case E_MIPS_MACH_LS2E:
6917 return bfd_mach_mips_loongson_2e;
6918
6919 case E_MIPS_MACH_LS2F:
6920 return bfd_mach_mips_loongson_2f;
6921
6922 case E_MIPS_MACH_GS464:
6923 return bfd_mach_mips_gs464;
6924
6925 case E_MIPS_MACH_GS464E:
6926 return bfd_mach_mips_gs464e;
6927
6928 case E_MIPS_MACH_GS264E:
6929 return bfd_mach_mips_gs264e;
6930
6931 case E_MIPS_MACH_OCTEON3:
6932 return bfd_mach_mips_octeon3;
6933
6934 case E_MIPS_MACH_OCTEON2:
6935 return bfd_mach_mips_octeon2;
6936
6937 case E_MIPS_MACH_OCTEON:
6938 return bfd_mach_mips_octeon;
6939
6940 case E_MIPS_MACH_XLR:
6941 return bfd_mach_mips_xlr;
6942
6943 case E_MIPS_MACH_IAMR2:
6944 return bfd_mach_mips_interaptiv_mr2;
6945
6946 default:
6947 switch (flags & EF_MIPS_ARCH)
6948 {
6949 default:
6950 case E_MIPS_ARCH_1:
6951 return bfd_mach_mips3000;
6952
6953 case E_MIPS_ARCH_2:
6954 return bfd_mach_mips6000;
6955
6956 case E_MIPS_ARCH_3:
6957 return bfd_mach_mips4000;
6958
6959 case E_MIPS_ARCH_4:
6960 return bfd_mach_mips8000;
6961
6962 case E_MIPS_ARCH_5:
6963 return bfd_mach_mips5;
6964
6965 case E_MIPS_ARCH_32:
6966 return bfd_mach_mipsisa32;
6967
6968 case E_MIPS_ARCH_64:
6969 return bfd_mach_mipsisa64;
6970
6971 case E_MIPS_ARCH_32R2:
6972 return bfd_mach_mipsisa32r2;
6973
6974 case E_MIPS_ARCH_64R2:
6975 return bfd_mach_mipsisa64r2;
6976
6977 case E_MIPS_ARCH_32R6:
6978 return bfd_mach_mipsisa32r6;
6979
6980 case E_MIPS_ARCH_64R6:
6981 return bfd_mach_mipsisa64r6;
6982 }
6983 }
6984
6985 return 0;
6986 }
6987
6988 /* Return printable name for ABI. */
6989
6990 static INLINE char *
6991 elf_mips_abi_name (bfd *abfd)
6992 {
6993 flagword flags;
6994
6995 flags = elf_elfheader (abfd)->e_flags;
6996 switch (flags & EF_MIPS_ABI)
6997 {
6998 case 0:
6999 if (ABI_N32_P (abfd))
7000 return "N32";
7001 else if (ABI_64_P (abfd))
7002 return "64";
7003 else
7004 return "none";
7005 case E_MIPS_ABI_O32:
7006 return "O32";
7007 case E_MIPS_ABI_O64:
7008 return "O64";
7009 case E_MIPS_ABI_EABI32:
7010 return "EABI32";
7011 case E_MIPS_ABI_EABI64:
7012 return "EABI64";
7013 default:
7014 return "unknown abi";
7015 }
7016 }
7017
7018 /* MIPS ELF uses two common sections. One is the usual one, and the
7020 other is for small objects. All the small objects are kept
7021 together, and then referenced via the gp pointer, which yields
7022 faster assembler code. This is what we use for the small common
7023 section. This approach is copied from ecoff.c. */
7024 static asection mips_elf_scom_section;
7025 static asymbol mips_elf_scom_symbol;
7026 static asymbol *mips_elf_scom_symbol_ptr;
7027
7028 /* MIPS ELF also uses an acommon section, which represents an
7029 allocated common symbol which may be overridden by a
7030 definition in a shared library. */
7031 static asection mips_elf_acom_section;
7032 static asymbol mips_elf_acom_symbol;
7033 static asymbol *mips_elf_acom_symbol_ptr;
7034
7035 /* This is used for both the 32-bit and the 64-bit ABI. */
7036
7037 void
7038 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7039 {
7040 elf_symbol_type *elfsym;
7041
7042 /* Handle the special MIPS section numbers that a symbol may use. */
7043 elfsym = (elf_symbol_type *) asym;
7044 switch (elfsym->internal_elf_sym.st_shndx)
7045 {
7046 case SHN_MIPS_ACOMMON:
7047 /* This section is used in a dynamically linked executable file.
7048 It is an allocated common section. The dynamic linker can
7049 either resolve these symbols to something in a shared
7050 library, or it can just leave them here. For our purposes,
7051 we can consider these symbols to be in a new section. */
7052 if (mips_elf_acom_section.name == NULL)
7053 {
7054 /* Initialize the acommon section. */
7055 mips_elf_acom_section.name = ".acommon";
7056 mips_elf_acom_section.flags = SEC_ALLOC;
7057 mips_elf_acom_section.output_section = &mips_elf_acom_section;
7058 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
7059 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
7060 mips_elf_acom_symbol.name = ".acommon";
7061 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
7062 mips_elf_acom_symbol.section = &mips_elf_acom_section;
7063 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
7064 }
7065 asym->section = &mips_elf_acom_section;
7066 break;
7067
7068 case SHN_COMMON:
7069 /* Common symbols less than the GP size are automatically
7070 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
7071 if (asym->value > elf_gp_size (abfd)
7072 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7073 || IRIX_COMPAT (abfd) == ict_irix6)
7074 break;
7075 /* Fall through. */
7076 case SHN_MIPS_SCOMMON:
7077 if (mips_elf_scom_section.name == NULL)
7078 {
7079 /* Initialize the small common section. */
7080 mips_elf_scom_section.name = ".scommon";
7081 mips_elf_scom_section.flags = SEC_IS_COMMON;
7082 mips_elf_scom_section.output_section = &mips_elf_scom_section;
7083 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
7084 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
7085 mips_elf_scom_symbol.name = ".scommon";
7086 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
7087 mips_elf_scom_symbol.section = &mips_elf_scom_section;
7088 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
7089 }
7090 asym->section = &mips_elf_scom_section;
7091 asym->value = elfsym->internal_elf_sym.st_size;
7092 break;
7093
7094 case SHN_MIPS_SUNDEFINED:
7095 asym->section = bfd_und_section_ptr;
7096 break;
7097
7098 case SHN_MIPS_TEXT:
7099 {
7100 asection *section = bfd_get_section_by_name (abfd, ".text");
7101
7102 if (section != NULL)
7103 {
7104 asym->section = section;
7105 /* MIPS_TEXT is a bit special, the address is not an offset
7106 to the base of the .text section. So subtract the section
7107 base address to make it an offset. */
7108 asym->value -= section->vma;
7109 }
7110 }
7111 break;
7112
7113 case SHN_MIPS_DATA:
7114 {
7115 asection *section = bfd_get_section_by_name (abfd, ".data");
7116
7117 if (section != NULL)
7118 {
7119 asym->section = section;
7120 /* MIPS_DATA is a bit special, the address is not an offset
7121 to the base of the .data section. So subtract the section
7122 base address to make it an offset. */
7123 asym->value -= section->vma;
7124 }
7125 }
7126 break;
7127 }
7128
7129 /* If this is an odd-valued function symbol, assume it's a MIPS16
7130 or microMIPS one. */
7131 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7132 && (asym->value & 1) != 0)
7133 {
7134 asym->value--;
7135 if (MICROMIPS_P (abfd))
7136 elfsym->internal_elf_sym.st_other
7137 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7138 else
7139 elfsym->internal_elf_sym.st_other
7140 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7141 }
7142 }
7143
7144 /* Implement elf_backend_eh_frame_address_size. This differs from
7146 the default in the way it handles EABI64.
7147
7148 EABI64 was originally specified as an LP64 ABI, and that is what
7149 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7150 historically accepted the combination of -mabi=eabi and -mlong32,
7151 and this ILP32 variation has become semi-official over time.
7152 Both forms use elf32 and have pointer-sized FDE addresses.
7153
7154 If an EABI object was generated by GCC 4.0 or above, it will have
7155 an empty .gcc_compiled_longXX section, where XX is the size of longs
7156 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7157 have no special marking to distinguish them from LP64 objects.
7158
7159 We don't want users of the official LP64 ABI to be punished for the
7160 existence of the ILP32 variant, but at the same time, we don't want
7161 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7162 We therefore take the following approach:
7163
7164 - If ABFD contains a .gcc_compiled_longXX section, use it to
7165 determine the pointer size.
7166
7167 - Otherwise check the type of the first relocation. Assume that
7168 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7169
7170 - Otherwise punt.
7171
7172 The second check is enough to detect LP64 objects generated by pre-4.0
7173 compilers because, in the kind of output generated by those compilers,
7174 the first relocation will be associated with either a CIE personality
7175 routine or an FDE start address. Furthermore, the compilers never
7176 used a special (non-pointer) encoding for this ABI.
7177
7178 Checking the relocation type should also be safe because there is no
7179 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7180 did so. */
7181
7182 unsigned int
7183 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7184 {
7185 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7186 return 8;
7187 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7188 {
7189 bfd_boolean long32_p, long64_p;
7190
7191 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7192 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7193 if (long32_p && long64_p)
7194 return 0;
7195 if (long32_p)
7196 return 4;
7197 if (long64_p)
7198 return 8;
7199
7200 if (sec->reloc_count > 0
7201 && elf_section_data (sec)->relocs != NULL
7202 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7203 == R_MIPS_64))
7204 return 8;
7205
7206 return 0;
7207 }
7208 return 4;
7209 }
7210
7211 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7213 relocations against two unnamed section symbols to resolve to the
7214 same address. For example, if we have code like:
7215
7216 lw $4,%got_disp(.data)($gp)
7217 lw $25,%got_disp(.text)($gp)
7218 jalr $25
7219
7220 then the linker will resolve both relocations to .data and the program
7221 will jump there rather than to .text.
7222
7223 We can work around this problem by giving names to local section symbols.
7224 This is also what the MIPSpro tools do. */
7225
7226 bfd_boolean
7227 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7228 {
7229 return SGI_COMPAT (abfd);
7230 }
7231
7232 /* Work over a section just before writing it out. This routine is
7234 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7235 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7236 a better way. */
7237
7238 bfd_boolean
7239 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7240 {
7241 if (hdr->sh_type == SHT_MIPS_REGINFO
7242 && hdr->sh_size > 0)
7243 {
7244 bfd_byte buf[4];
7245
7246 BFD_ASSERT (hdr->contents == NULL);
7247
7248 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7249 {
7250 _bfd_error_handler
7251 (_("%pB: incorrect `.reginfo' section size; "
7252 "expected %" PRIu64 ", got %" PRIu64),
7253 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7254 (uint64_t) hdr->sh_size);
7255 bfd_set_error (bfd_error_bad_value);
7256 return FALSE;
7257 }
7258
7259 if (bfd_seek (abfd,
7260 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7261 SEEK_SET) != 0)
7262 return FALSE;
7263 H_PUT_32 (abfd, elf_gp (abfd), buf);
7264 if (bfd_bwrite (buf, 4, abfd) != 4)
7265 return FALSE;
7266 }
7267
7268 if (hdr->sh_type == SHT_MIPS_OPTIONS
7269 && hdr->bfd_section != NULL
7270 && mips_elf_section_data (hdr->bfd_section) != NULL
7271 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7272 {
7273 bfd_byte *contents, *l, *lend;
7274
7275 /* We stored the section contents in the tdata field in the
7276 set_section_contents routine. We save the section contents
7277 so that we don't have to read them again.
7278 At this point we know that elf_gp is set, so we can look
7279 through the section contents to see if there is an
7280 ODK_REGINFO structure. */
7281
7282 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7283 l = contents;
7284 lend = contents + hdr->sh_size;
7285 while (l + sizeof (Elf_External_Options) <= lend)
7286 {
7287 Elf_Internal_Options intopt;
7288
7289 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7290 &intopt);
7291 if (intopt.size < sizeof (Elf_External_Options))
7292 {
7293 _bfd_error_handler
7294 /* xgettext:c-format */
7295 (_("%pB: warning: bad `%s' option size %u smaller than"
7296 " its header"),
7297 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7298 break;
7299 }
7300 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7301 {
7302 bfd_byte buf[8];
7303
7304 if (bfd_seek (abfd,
7305 (hdr->sh_offset
7306 + (l - contents)
7307 + sizeof (Elf_External_Options)
7308 + (sizeof (Elf64_External_RegInfo) - 8)),
7309 SEEK_SET) != 0)
7310 return FALSE;
7311 H_PUT_64 (abfd, elf_gp (abfd), buf);
7312 if (bfd_bwrite (buf, 8, abfd) != 8)
7313 return FALSE;
7314 }
7315 else if (intopt.kind == ODK_REGINFO)
7316 {
7317 bfd_byte buf[4];
7318
7319 if (bfd_seek (abfd,
7320 (hdr->sh_offset
7321 + (l - contents)
7322 + sizeof (Elf_External_Options)
7323 + (sizeof (Elf32_External_RegInfo) - 4)),
7324 SEEK_SET) != 0)
7325 return FALSE;
7326 H_PUT_32 (abfd, elf_gp (abfd), buf);
7327 if (bfd_bwrite (buf, 4, abfd) != 4)
7328 return FALSE;
7329 }
7330 l += intopt.size;
7331 }
7332 }
7333
7334 if (hdr->bfd_section != NULL)
7335 {
7336 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
7337
7338 /* .sbss is not handled specially here because the GNU/Linux
7339 prelinker can convert .sbss from NOBITS to PROGBITS and
7340 changing it back to NOBITS breaks the binary. The entry in
7341 _bfd_mips_elf_special_sections will ensure the correct flags
7342 are set on .sbss if BFD creates it without reading it from an
7343 input file, and without special handling here the flags set
7344 on it in an input file will be followed. */
7345 if (strcmp (name, ".sdata") == 0
7346 || strcmp (name, ".lit8") == 0
7347 || strcmp (name, ".lit4") == 0)
7348 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7349 else if (strcmp (name, ".srdata") == 0)
7350 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7351 else if (strcmp (name, ".compact_rel") == 0)
7352 hdr->sh_flags = 0;
7353 else if (strcmp (name, ".rtproc") == 0)
7354 {
7355 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7356 {
7357 unsigned int adjust;
7358
7359 adjust = hdr->sh_size % hdr->sh_addralign;
7360 if (adjust != 0)
7361 hdr->sh_size += hdr->sh_addralign - adjust;
7362 }
7363 }
7364 }
7365
7366 return TRUE;
7367 }
7368
7369 /* Handle a MIPS specific section when reading an object file. This
7370 is called when elfcode.h finds a section with an unknown type.
7371 This routine supports both the 32-bit and 64-bit ELF ABI.
7372
7373 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
7374 how to. */
7375
7376 bfd_boolean
7377 _bfd_mips_elf_section_from_shdr (bfd *abfd,
7378 Elf_Internal_Shdr *hdr,
7379 const char *name,
7380 int shindex)
7381 {
7382 flagword flags = 0;
7383
7384 /* There ought to be a place to keep ELF backend specific flags, but
7385 at the moment there isn't one. We just keep track of the
7386 sections by their name, instead. Fortunately, the ABI gives
7387 suggested names for all the MIPS specific sections, so we will
7388 probably get away with this. */
7389 switch (hdr->sh_type)
7390 {
7391 case SHT_MIPS_LIBLIST:
7392 if (strcmp (name, ".liblist") != 0)
7393 return FALSE;
7394 break;
7395 case SHT_MIPS_MSYM:
7396 if (strcmp (name, ".msym") != 0)
7397 return FALSE;
7398 break;
7399 case SHT_MIPS_CONFLICT:
7400 if (strcmp (name, ".conflict") != 0)
7401 return FALSE;
7402 break;
7403 case SHT_MIPS_GPTAB:
7404 if (! CONST_STRNEQ (name, ".gptab."))
7405 return FALSE;
7406 break;
7407 case SHT_MIPS_UCODE:
7408 if (strcmp (name, ".ucode") != 0)
7409 return FALSE;
7410 break;
7411 case SHT_MIPS_DEBUG:
7412 if (strcmp (name, ".mdebug") != 0)
7413 return FALSE;
7414 flags = SEC_DEBUGGING;
7415 break;
7416 case SHT_MIPS_REGINFO:
7417 if (strcmp (name, ".reginfo") != 0
7418 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7419 return FALSE;
7420 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7421 break;
7422 case SHT_MIPS_IFACE:
7423 if (strcmp (name, ".MIPS.interfaces") != 0)
7424 return FALSE;
7425 break;
7426 case SHT_MIPS_CONTENT:
7427 if (! CONST_STRNEQ (name, ".MIPS.content"))
7428 return FALSE;
7429 break;
7430 case SHT_MIPS_OPTIONS:
7431 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7432 return FALSE;
7433 break;
7434 case SHT_MIPS_ABIFLAGS:
7435 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7436 return FALSE;
7437 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7438 break;
7439 case SHT_MIPS_DWARF:
7440 if (! CONST_STRNEQ (name, ".debug_")
7441 && ! CONST_STRNEQ (name, ".zdebug_"))
7442 return FALSE;
7443 break;
7444 case SHT_MIPS_SYMBOL_LIB:
7445 if (strcmp (name, ".MIPS.symlib") != 0)
7446 return FALSE;
7447 break;
7448 case SHT_MIPS_EVENTS:
7449 if (! CONST_STRNEQ (name, ".MIPS.events")
7450 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
7451 return FALSE;
7452 break;
7453 default:
7454 break;
7455 }
7456
7457 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7458 return FALSE;
7459
7460 if (flags)
7461 {
7462 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
7463 (bfd_get_section_flags (abfd,
7464 hdr->bfd_section)
7465 | flags)))
7466 return FALSE;
7467 }
7468
7469 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7470 {
7471 Elf_External_ABIFlags_v0 ext;
7472
7473 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7474 &ext, 0, sizeof ext))
7475 return FALSE;
7476 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7477 &mips_elf_tdata (abfd)->abiflags);
7478 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7479 return FALSE;
7480 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
7481 }
7482
7483 /* FIXME: We should record sh_info for a .gptab section. */
7484
7485 /* For a .reginfo section, set the gp value in the tdata information
7486 from the contents of this section. We need the gp value while
7487 processing relocs, so we just get it now. The .reginfo section
7488 is not used in the 64-bit MIPS ELF ABI. */
7489 if (hdr->sh_type == SHT_MIPS_REGINFO)
7490 {
7491 Elf32_External_RegInfo ext;
7492 Elf32_RegInfo s;
7493
7494 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7495 &ext, 0, sizeof ext))
7496 return FALSE;
7497 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7498 elf_gp (abfd) = s.ri_gp_value;
7499 }
7500
7501 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7502 set the gp value based on what we find. We may see both
7503 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7504 they should agree. */
7505 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7506 {
7507 bfd_byte *contents, *l, *lend;
7508
7509 contents = bfd_malloc (hdr->sh_size);
7510 if (contents == NULL)
7511 return FALSE;
7512 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
7513 0, hdr->sh_size))
7514 {
7515 free (contents);
7516 return FALSE;
7517 }
7518 l = contents;
7519 lend = contents + hdr->sh_size;
7520 while (l + sizeof (Elf_External_Options) <= lend)
7521 {
7522 Elf_Internal_Options intopt;
7523
7524 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7525 &intopt);
7526 if (intopt.size < sizeof (Elf_External_Options))
7527 {
7528 _bfd_error_handler
7529 /* xgettext:c-format */
7530 (_("%pB: warning: bad `%s' option size %u smaller than"
7531 " its header"),
7532 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7533 break;
7534 }
7535 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7536 {
7537 Elf64_Internal_RegInfo intreg;
7538
7539 bfd_mips_elf64_swap_reginfo_in
7540 (abfd,
7541 ((Elf64_External_RegInfo *)
7542 (l + sizeof (Elf_External_Options))),
7543 &intreg);
7544 elf_gp (abfd) = intreg.ri_gp_value;
7545 }
7546 else if (intopt.kind == ODK_REGINFO)
7547 {
7548 Elf32_RegInfo intreg;
7549
7550 bfd_mips_elf32_swap_reginfo_in
7551 (abfd,
7552 ((Elf32_External_RegInfo *)
7553 (l + sizeof (Elf_External_Options))),
7554 &intreg);
7555 elf_gp (abfd) = intreg.ri_gp_value;
7556 }
7557 l += intopt.size;
7558 }
7559 free (contents);
7560 }
7561
7562 return TRUE;
7563 }
7564
7565 /* Set the correct type for a MIPS ELF section. We do this by the
7566 section name, which is a hack, but ought to work. This routine is
7567 used by both the 32-bit and the 64-bit ABI. */
7568
7569 bfd_boolean
7570 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7571 {
7572 const char *name = bfd_get_section_name (abfd, sec);
7573
7574 if (strcmp (name, ".liblist") == 0)
7575 {
7576 hdr->sh_type = SHT_MIPS_LIBLIST;
7577 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7578 /* The sh_link field is set in final_write_processing. */
7579 }
7580 else if (strcmp (name, ".conflict") == 0)
7581 hdr->sh_type = SHT_MIPS_CONFLICT;
7582 else if (CONST_STRNEQ (name, ".gptab."))
7583 {
7584 hdr->sh_type = SHT_MIPS_GPTAB;
7585 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7586 /* The sh_info field is set in final_write_processing. */
7587 }
7588 else if (strcmp (name, ".ucode") == 0)
7589 hdr->sh_type = SHT_MIPS_UCODE;
7590 else if (strcmp (name, ".mdebug") == 0)
7591 {
7592 hdr->sh_type = SHT_MIPS_DEBUG;
7593 /* In a shared object on IRIX 5.3, the .mdebug section has an
7594 entsize of 0. FIXME: Does this matter? */
7595 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7596 hdr->sh_entsize = 0;
7597 else
7598 hdr->sh_entsize = 1;
7599 }
7600 else if (strcmp (name, ".reginfo") == 0)
7601 {
7602 hdr->sh_type = SHT_MIPS_REGINFO;
7603 /* In a shared object on IRIX 5.3, the .reginfo section has an
7604 entsize of 0x18. FIXME: Does this matter? */
7605 if (SGI_COMPAT (abfd))
7606 {
7607 if ((abfd->flags & DYNAMIC) != 0)
7608 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7609 else
7610 hdr->sh_entsize = 1;
7611 }
7612 else
7613 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7614 }
7615 else if (SGI_COMPAT (abfd)
7616 && (strcmp (name, ".hash") == 0
7617 || strcmp (name, ".dynamic") == 0
7618 || strcmp (name, ".dynstr") == 0))
7619 {
7620 if (SGI_COMPAT (abfd))
7621 hdr->sh_entsize = 0;
7622 #if 0
7623 /* This isn't how the IRIX6 linker behaves. */
7624 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7625 #endif
7626 }
7627 else if (strcmp (name, ".got") == 0
7628 || strcmp (name, ".srdata") == 0
7629 || strcmp (name, ".sdata") == 0
7630 || strcmp (name, ".sbss") == 0
7631 || strcmp (name, ".lit4") == 0
7632 || strcmp (name, ".lit8") == 0)
7633 hdr->sh_flags |= SHF_MIPS_GPREL;
7634 else if (strcmp (name, ".MIPS.interfaces") == 0)
7635 {
7636 hdr->sh_type = SHT_MIPS_IFACE;
7637 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7638 }
7639 else if (CONST_STRNEQ (name, ".MIPS.content"))
7640 {
7641 hdr->sh_type = SHT_MIPS_CONTENT;
7642 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7643 /* The sh_info field is set in final_write_processing. */
7644 }
7645 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7646 {
7647 hdr->sh_type = SHT_MIPS_OPTIONS;
7648 hdr->sh_entsize = 1;
7649 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7650 }
7651 else if (CONST_STRNEQ (name, ".MIPS.abiflags"))
7652 {
7653 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7654 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7655 }
7656 else if (CONST_STRNEQ (name, ".debug_")
7657 || CONST_STRNEQ (name, ".zdebug_"))
7658 {
7659 hdr->sh_type = SHT_MIPS_DWARF;
7660
7661 /* Irix facilities such as libexc expect a single .debug_frame
7662 per executable, the system ones have NOSTRIP set and the linker
7663 doesn't merge sections with different flags so ... */
7664 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7665 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7666 }
7667 else if (strcmp (name, ".MIPS.symlib") == 0)
7668 {
7669 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7670 /* The sh_link and sh_info fields are set in
7671 final_write_processing. */
7672 }
7673 else if (CONST_STRNEQ (name, ".MIPS.events")
7674 || CONST_STRNEQ (name, ".MIPS.post_rel"))
7675 {
7676 hdr->sh_type = SHT_MIPS_EVENTS;
7677 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7678 /* The sh_link field is set in final_write_processing. */
7679 }
7680 else if (strcmp (name, ".msym") == 0)
7681 {
7682 hdr->sh_type = SHT_MIPS_MSYM;
7683 hdr->sh_flags |= SHF_ALLOC;
7684 hdr->sh_entsize = 8;
7685 }
7686
7687 /* The generic elf_fake_sections will set up REL_HDR using the default
7688 kind of relocations. We used to set up a second header for the
7689 non-default kind of relocations here, but only NewABI would use
7690 these, and the IRIX ld doesn't like resulting empty RELA sections.
7691 Thus we create those header only on demand now. */
7692
7693 return TRUE;
7694 }
7695
7696 /* Given a BFD section, try to locate the corresponding ELF section
7697 index. This is used by both the 32-bit and the 64-bit ABI.
7698 Actually, it's not clear to me that the 64-bit ABI supports these,
7699 but for non-PIC objects we will certainly want support for at least
7700 the .scommon section. */
7701
7702 bfd_boolean
7703 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7704 asection *sec, int *retval)
7705 {
7706 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7707 {
7708 *retval = SHN_MIPS_SCOMMON;
7709 return TRUE;
7710 }
7711 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7712 {
7713 *retval = SHN_MIPS_ACOMMON;
7714 return TRUE;
7715 }
7716 return FALSE;
7717 }
7718
7719 /* Hook called by the linker routine which adds symbols from an object
7721 file. We must handle the special MIPS section numbers here. */
7722
7723 bfd_boolean
7724 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7725 Elf_Internal_Sym *sym, const char **namep,
7726 flagword *flagsp ATTRIBUTE_UNUSED,
7727 asection **secp, bfd_vma *valp)
7728 {
7729 if (SGI_COMPAT (abfd)
7730 && (abfd->flags & DYNAMIC) != 0
7731 && strcmp (*namep, "_rld_new_interface") == 0)
7732 {
7733 /* Skip IRIX5 rld entry name. */
7734 *namep = NULL;
7735 return TRUE;
7736 }
7737
7738 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7739 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7740 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7741 a magic symbol resolved by the linker, we ignore this bogus definition
7742 of _gp_disp. New ABI objects do not suffer from this problem so this
7743 is not done for them. */
7744 if (!NEWABI_P(abfd)
7745 && (sym->st_shndx == SHN_ABS)
7746 && (strcmp (*namep, "_gp_disp") == 0))
7747 {
7748 *namep = NULL;
7749 return TRUE;
7750 }
7751
7752 switch (sym->st_shndx)
7753 {
7754 case SHN_COMMON:
7755 /* Common symbols less than the GP size are automatically
7756 treated as SHN_MIPS_SCOMMON symbols. */
7757 if (sym->st_size > elf_gp_size (abfd)
7758 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7759 || IRIX_COMPAT (abfd) == ict_irix6)
7760 break;
7761 /* Fall through. */
7762 case SHN_MIPS_SCOMMON:
7763 *secp = bfd_make_section_old_way (abfd, ".scommon");
7764 (*secp)->flags |= SEC_IS_COMMON;
7765 *valp = sym->st_size;
7766 break;
7767
7768 case SHN_MIPS_TEXT:
7769 /* This section is used in a shared object. */
7770 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7771 {
7772 asymbol *elf_text_symbol;
7773 asection *elf_text_section;
7774 bfd_size_type amt = sizeof (asection);
7775
7776 elf_text_section = bfd_zalloc (abfd, amt);
7777 if (elf_text_section == NULL)
7778 return FALSE;
7779
7780 amt = sizeof (asymbol);
7781 elf_text_symbol = bfd_zalloc (abfd, amt);
7782 if (elf_text_symbol == NULL)
7783 return FALSE;
7784
7785 /* Initialize the section. */
7786
7787 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7788 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7789
7790 elf_text_section->symbol = elf_text_symbol;
7791 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7792
7793 elf_text_section->name = ".text";
7794 elf_text_section->flags = SEC_NO_FLAGS;
7795 elf_text_section->output_section = NULL;
7796 elf_text_section->owner = abfd;
7797 elf_text_symbol->name = ".text";
7798 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7799 elf_text_symbol->section = elf_text_section;
7800 }
7801 /* This code used to do *secp = bfd_und_section_ptr if
7802 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7803 so I took it out. */
7804 *secp = mips_elf_tdata (abfd)->elf_text_section;
7805 break;
7806
7807 case SHN_MIPS_ACOMMON:
7808 /* Fall through. XXX Can we treat this as allocated data? */
7809 case SHN_MIPS_DATA:
7810 /* This section is used in a shared object. */
7811 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7812 {
7813 asymbol *elf_data_symbol;
7814 asection *elf_data_section;
7815 bfd_size_type amt = sizeof (asection);
7816
7817 elf_data_section = bfd_zalloc (abfd, amt);
7818 if (elf_data_section == NULL)
7819 return FALSE;
7820
7821 amt = sizeof (asymbol);
7822 elf_data_symbol = bfd_zalloc (abfd, amt);
7823 if (elf_data_symbol == NULL)
7824 return FALSE;
7825
7826 /* Initialize the section. */
7827
7828 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7829 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7830
7831 elf_data_section->symbol = elf_data_symbol;
7832 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7833
7834 elf_data_section->name = ".data";
7835 elf_data_section->flags = SEC_NO_FLAGS;
7836 elf_data_section->output_section = NULL;
7837 elf_data_section->owner = abfd;
7838 elf_data_symbol->name = ".data";
7839 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7840 elf_data_symbol->section = elf_data_section;
7841 }
7842 /* This code used to do *secp = bfd_und_section_ptr if
7843 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7844 so I took it out. */
7845 *secp = mips_elf_tdata (abfd)->elf_data_section;
7846 break;
7847
7848 case SHN_MIPS_SUNDEFINED:
7849 *secp = bfd_und_section_ptr;
7850 break;
7851 }
7852
7853 if (SGI_COMPAT (abfd)
7854 && ! bfd_link_pic (info)
7855 && info->output_bfd->xvec == abfd->xvec
7856 && strcmp (*namep, "__rld_obj_head") == 0)
7857 {
7858 struct elf_link_hash_entry *h;
7859 struct bfd_link_hash_entry *bh;
7860
7861 /* Mark __rld_obj_head as dynamic. */
7862 bh = NULL;
7863 if (! (_bfd_generic_link_add_one_symbol
7864 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7865 get_elf_backend_data (abfd)->collect, &bh)))
7866 return FALSE;
7867
7868 h = (struct elf_link_hash_entry *) bh;
7869 h->non_elf = 0;
7870 h->def_regular = 1;
7871 h->type = STT_OBJECT;
7872
7873 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7874 return FALSE;
7875
7876 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7877 mips_elf_hash_table (info)->rld_symbol = h;
7878 }
7879
7880 /* If this is a mips16 text symbol, add 1 to the value to make it
7881 odd. This will cause something like .word SYM to come up with
7882 the right value when it is loaded into the PC. */
7883 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7884 ++*valp;
7885
7886 return TRUE;
7887 }
7888
7889 /* This hook function is called before the linker writes out a global
7890 symbol. We mark symbols as small common if appropriate. This is
7891 also where we undo the increment of the value for a mips16 symbol. */
7892
7893 int
7894 _bfd_mips_elf_link_output_symbol_hook
7895 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7896 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7897 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7898 {
7899 /* If we see a common symbol, which implies a relocatable link, then
7900 if a symbol was small common in an input file, mark it as small
7901 common in the output file. */
7902 if (sym->st_shndx == SHN_COMMON
7903 && strcmp (input_sec->name, ".scommon") == 0)
7904 sym->st_shndx = SHN_MIPS_SCOMMON;
7905
7906 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7907 sym->st_value &= ~1;
7908
7909 return 1;
7910 }
7911
7912 /* Functions for the dynamic linker. */
7914
7915 /* Create dynamic sections when linking against a dynamic object. */
7916
7917 bfd_boolean
7918 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7919 {
7920 struct elf_link_hash_entry *h;
7921 struct bfd_link_hash_entry *bh;
7922 flagword flags;
7923 register asection *s;
7924 const char * const *namep;
7925 struct mips_elf_link_hash_table *htab;
7926
7927 htab = mips_elf_hash_table (info);
7928 BFD_ASSERT (htab != NULL);
7929
7930 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7931 | SEC_LINKER_CREATED | SEC_READONLY);
7932
7933 /* The psABI requires a read-only .dynamic section, but the VxWorks
7934 EABI doesn't. */
7935 if (!htab->is_vxworks)
7936 {
7937 s = bfd_get_linker_section (abfd, ".dynamic");
7938 if (s != NULL)
7939 {
7940 if (! bfd_set_section_flags (abfd, s, flags))
7941 return FALSE;
7942 }
7943 }
7944
7945 /* We need to create .got section. */
7946 if (!mips_elf_create_got_section (abfd, info))
7947 return FALSE;
7948
7949 if (! mips_elf_rel_dyn_section (info, TRUE))
7950 return FALSE;
7951
7952 /* Create .stub section. */
7953 s = bfd_make_section_anyway_with_flags (abfd,
7954 MIPS_ELF_STUB_SECTION_NAME (abfd),
7955 flags | SEC_CODE);
7956 if (s == NULL
7957 || ! bfd_set_section_alignment (abfd, s,
7958 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7959 return FALSE;
7960 htab->sstubs = s;
7961
7962 if (!mips_elf_hash_table (info)->use_rld_obj_head
7963 && bfd_link_executable (info)
7964 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
7965 {
7966 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7967 flags &~ (flagword) SEC_READONLY);
7968 if (s == NULL
7969 || ! bfd_set_section_alignment (abfd, s,
7970 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7971 return FALSE;
7972 }
7973
7974 /* On IRIX5, we adjust add some additional symbols and change the
7975 alignments of several sections. There is no ABI documentation
7976 indicating that this is necessary on IRIX6, nor any evidence that
7977 the linker takes such action. */
7978 if (IRIX_COMPAT (abfd) == ict_irix5)
7979 {
7980 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7981 {
7982 bh = NULL;
7983 if (! (_bfd_generic_link_add_one_symbol
7984 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7985 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7986 return FALSE;
7987
7988 h = (struct elf_link_hash_entry *) bh;
7989 h->mark = 1;
7990 h->non_elf = 0;
7991 h->def_regular = 1;
7992 h->type = STT_SECTION;
7993
7994 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7995 return FALSE;
7996 }
7997
7998 /* We need to create a .compact_rel section. */
7999 if (SGI_COMPAT (abfd))
8000 {
8001 if (!mips_elf_create_compact_rel_section (abfd, info))
8002 return FALSE;
8003 }
8004
8005 /* Change alignments of some sections. */
8006 s = bfd_get_linker_section (abfd, ".hash");
8007 if (s != NULL)
8008 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8009
8010 s = bfd_get_linker_section (abfd, ".dynsym");
8011 if (s != NULL)
8012 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8013
8014 s = bfd_get_linker_section (abfd, ".dynstr");
8015 if (s != NULL)
8016 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8017
8018 /* ??? */
8019 s = bfd_get_section_by_name (abfd, ".reginfo");
8020 if (s != NULL)
8021 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8022
8023 s = bfd_get_linker_section (abfd, ".dynamic");
8024 if (s != NULL)
8025 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8026 }
8027
8028 if (bfd_link_executable (info))
8029 {
8030 const char *name;
8031
8032 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8033 bh = NULL;
8034 if (!(_bfd_generic_link_add_one_symbol
8035 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8036 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
8037 return FALSE;
8038
8039 h = (struct elf_link_hash_entry *) bh;
8040 h->non_elf = 0;
8041 h->def_regular = 1;
8042 h->type = STT_SECTION;
8043
8044 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8045 return FALSE;
8046
8047 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8048 {
8049 /* __rld_map is a four byte word located in the .data section
8050 and is filled in by the rtld to contain a pointer to
8051 the _r_debug structure. Its symbol value will be set in
8052 _bfd_mips_elf_finish_dynamic_symbol. */
8053 s = bfd_get_linker_section (abfd, ".rld_map");
8054 BFD_ASSERT (s != NULL);
8055
8056 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8057 bh = NULL;
8058 if (!(_bfd_generic_link_add_one_symbol
8059 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
8060 get_elf_backend_data (abfd)->collect, &bh)))
8061 return FALSE;
8062
8063 h = (struct elf_link_hash_entry *) bh;
8064 h->non_elf = 0;
8065 h->def_regular = 1;
8066 h->type = STT_OBJECT;
8067
8068 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8069 return FALSE;
8070 mips_elf_hash_table (info)->rld_symbol = h;
8071 }
8072 }
8073
8074 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8075 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
8076 if (!_bfd_elf_create_dynamic_sections (abfd, info))
8077 return FALSE;
8078
8079 /* Do the usual VxWorks handling. */
8080 if (htab->is_vxworks
8081 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8082 return FALSE;
8083
8084 return TRUE;
8085 }
8086
8087 /* Return true if relocation REL against section SEC is a REL rather than
8089 RELA relocation. RELOCS is the first relocation in the section and
8090 ABFD is the bfd that contains SEC. */
8091
8092 static bfd_boolean
8093 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8094 const Elf_Internal_Rela *relocs,
8095 const Elf_Internal_Rela *rel)
8096 {
8097 Elf_Internal_Shdr *rel_hdr;
8098 const struct elf_backend_data *bed;
8099
8100 /* To determine which flavor of relocation this is, we depend on the
8101 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8102 rel_hdr = elf_section_data (sec)->rel.hdr;
8103 if (rel_hdr == NULL)
8104 return FALSE;
8105 bed = get_elf_backend_data (abfd);
8106 return ((size_t) (rel - relocs)
8107 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8108 }
8109
8110 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8111 HOWTO is the relocation's howto and CONTENTS points to the contents
8112 of the section that REL is against. */
8113
8114 static bfd_vma
8115 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
8116 reloc_howto_type *howto, bfd_byte *contents)
8117 {
8118 bfd_byte *location;
8119 unsigned int r_type;
8120 bfd_vma addend;
8121 bfd_vma bytes;
8122
8123 r_type = ELF_R_TYPE (abfd, rel->r_info);
8124 location = contents + rel->r_offset;
8125
8126 /* Get the addend, which is stored in the input file. */
8127 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
8128 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8129 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
8130
8131 addend = bytes & howto->src_mask;
8132
8133 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8134 accordingly. */
8135 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8136 addend <<= 1;
8137
8138 return addend;
8139 }
8140
8141 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
8142 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8143 and update *ADDEND with the final addend. Return true on success
8144 or false if the LO16 could not be found. RELEND is the exclusive
8145 upper bound on the relocations for REL's section. */
8146
8147 static bfd_boolean
8148 mips_elf_add_lo16_rel_addend (bfd *abfd,
8149 const Elf_Internal_Rela *rel,
8150 const Elf_Internal_Rela *relend,
8151 bfd_byte *contents, bfd_vma *addend)
8152 {
8153 unsigned int r_type, lo16_type;
8154 const Elf_Internal_Rela *lo16_relocation;
8155 reloc_howto_type *lo16_howto;
8156 bfd_vma l;
8157
8158 r_type = ELF_R_TYPE (abfd, rel->r_info);
8159 if (mips16_reloc_p (r_type))
8160 lo16_type = R_MIPS16_LO16;
8161 else if (micromips_reloc_p (r_type))
8162 lo16_type = R_MICROMIPS_LO16;
8163 else if (r_type == R_MIPS_PCHI16)
8164 lo16_type = R_MIPS_PCLO16;
8165 else
8166 lo16_type = R_MIPS_LO16;
8167
8168 /* The combined value is the sum of the HI16 addend, left-shifted by
8169 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8170 code does a `lui' of the HI16 value, and then an `addiu' of the
8171 LO16 value.)
8172
8173 Scan ahead to find a matching LO16 relocation.
8174
8175 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8176 be immediately following. However, for the IRIX6 ABI, the next
8177 relocation may be a composed relocation consisting of several
8178 relocations for the same address. In that case, the R_MIPS_LO16
8179 relocation may occur as one of these. We permit a similar
8180 extension in general, as that is useful for GCC.
8181
8182 In some cases GCC dead code elimination removes the LO16 but keeps
8183 the corresponding HI16. This is strictly speaking a violation of
8184 the ABI but not immediately harmful. */
8185 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8186 if (lo16_relocation == NULL)
8187 return FALSE;
8188
8189 /* Obtain the addend kept there. */
8190 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
8191 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
8192
8193 l <<= lo16_howto->rightshift;
8194 l = _bfd_mips_elf_sign_extend (l, 16);
8195
8196 *addend <<= 16;
8197 *addend += l;
8198 return TRUE;
8199 }
8200
8201 /* Try to read the contents of section SEC in bfd ABFD. Return true and
8202 store the contents in *CONTENTS on success. Assume that *CONTENTS
8203 already holds the contents if it is nonull on entry. */
8204
8205 static bfd_boolean
8206 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8207 {
8208 if (*contents)
8209 return TRUE;
8210
8211 /* Get cached copy if it exists. */
8212 if (elf_section_data (sec)->this_hdr.contents != NULL)
8213 {
8214 *contents = elf_section_data (sec)->this_hdr.contents;
8215 return TRUE;
8216 }
8217
8218 return bfd_malloc_and_get_section (abfd, sec, contents);
8219 }
8220
8221 /* Make a new PLT record to keep internal data. */
8222
8223 static struct plt_entry *
8224 mips_elf_make_plt_record (bfd *abfd)
8225 {
8226 struct plt_entry *entry;
8227
8228 entry = bfd_zalloc (abfd, sizeof (*entry));
8229 if (entry == NULL)
8230 return NULL;
8231
8232 entry->stub_offset = MINUS_ONE;
8233 entry->mips_offset = MINUS_ONE;
8234 entry->comp_offset = MINUS_ONE;
8235 entry->gotplt_index = MINUS_ONE;
8236 return entry;
8237 }
8238
8239 /* Define the special `__gnu_absolute_zero' symbol. We only need this
8240 for PIC code, as otherwise there is no load-time relocation involved
8241 and local GOT entries whose value is zero at static link time will
8242 retain their value at load time. */
8243
8244 static bfd_boolean
8245 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8246 struct mips_elf_link_hash_table *htab,
8247 unsigned int r_type)
8248 {
8249 union
8250 {
8251 struct elf_link_hash_entry *eh;
8252 struct bfd_link_hash_entry *bh;
8253 }
8254 hzero;
8255
8256 BFD_ASSERT (!htab->use_absolute_zero);
8257 BFD_ASSERT (bfd_link_pic (info));
8258
8259 hzero.bh = NULL;
8260 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8261 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8262 NULL, FALSE, FALSE, &hzero.bh))
8263 return FALSE;
8264
8265 BFD_ASSERT (hzero.bh != NULL);
8266 hzero.eh->size = 0;
8267 hzero.eh->type = STT_NOTYPE;
8268 hzero.eh->other = STV_PROTECTED;
8269 hzero.eh->def_regular = 1;
8270 hzero.eh->non_elf = 0;
8271
8272 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, TRUE, r_type))
8273 return FALSE;
8274
8275 htab->use_absolute_zero = TRUE;
8276
8277 return TRUE;
8278 }
8279
8280 /* Look through the relocs for a section during the first phase, and
8281 allocate space in the global offset table and record the need for
8282 standard MIPS and compressed procedure linkage table entries. */
8283
8284 bfd_boolean
8285 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8286 asection *sec, const Elf_Internal_Rela *relocs)
8287 {
8288 const char *name;
8289 bfd *dynobj;
8290 Elf_Internal_Shdr *symtab_hdr;
8291 struct elf_link_hash_entry **sym_hashes;
8292 size_t extsymoff;
8293 const Elf_Internal_Rela *rel;
8294 const Elf_Internal_Rela *rel_end;
8295 asection *sreloc;
8296 const struct elf_backend_data *bed;
8297 struct mips_elf_link_hash_table *htab;
8298 bfd_byte *contents;
8299 bfd_vma addend;
8300 reloc_howto_type *howto;
8301
8302 if (bfd_link_relocatable (info))
8303 return TRUE;
8304
8305 htab = mips_elf_hash_table (info);
8306 BFD_ASSERT (htab != NULL);
8307
8308 dynobj = elf_hash_table (info)->dynobj;
8309 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8310 sym_hashes = elf_sym_hashes (abfd);
8311 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8312
8313 bed = get_elf_backend_data (abfd);
8314 rel_end = relocs + sec->reloc_count;
8315
8316 /* Check for the mips16 stub sections. */
8317
8318 name = bfd_get_section_name (abfd, sec);
8319 if (FN_STUB_P (name))
8320 {
8321 unsigned long r_symndx;
8322
8323 /* Look at the relocation information to figure out which symbol
8324 this is for. */
8325
8326 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8327 if (r_symndx == 0)
8328 {
8329 _bfd_error_handler
8330 /* xgettext:c-format */
8331 (_("%pB: warning: cannot determine the target function for"
8332 " stub section `%s'"),
8333 abfd, name);
8334 bfd_set_error (bfd_error_bad_value);
8335 return FALSE;
8336 }
8337
8338 if (r_symndx < extsymoff
8339 || sym_hashes[r_symndx - extsymoff] == NULL)
8340 {
8341 asection *o;
8342
8343 /* This stub is for a local symbol. This stub will only be
8344 needed if there is some relocation in this BFD, other
8345 than a 16 bit function call, which refers to this symbol. */
8346 for (o = abfd->sections; o != NULL; o = o->next)
8347 {
8348 Elf_Internal_Rela *sec_relocs;
8349 const Elf_Internal_Rela *r, *rend;
8350
8351 /* We can ignore stub sections when looking for relocs. */
8352 if ((o->flags & SEC_RELOC) == 0
8353 || o->reloc_count == 0
8354 || section_allows_mips16_refs_p (o))
8355 continue;
8356
8357 sec_relocs
8358 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8359 info->keep_memory);
8360 if (sec_relocs == NULL)
8361 return FALSE;
8362
8363 rend = sec_relocs + o->reloc_count;
8364 for (r = sec_relocs; r < rend; r++)
8365 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8366 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8367 break;
8368
8369 if (elf_section_data (o)->relocs != sec_relocs)
8370 free (sec_relocs);
8371
8372 if (r < rend)
8373 break;
8374 }
8375
8376 if (o == NULL)
8377 {
8378 /* There is no non-call reloc for this stub, so we do
8379 not need it. Since this function is called before
8380 the linker maps input sections to output sections, we
8381 can easily discard it by setting the SEC_EXCLUDE
8382 flag. */
8383 sec->flags |= SEC_EXCLUDE;
8384 return TRUE;
8385 }
8386
8387 /* Record this stub in an array of local symbol stubs for
8388 this BFD. */
8389 if (mips_elf_tdata (abfd)->local_stubs == NULL)
8390 {
8391 unsigned long symcount;
8392 asection **n;
8393 bfd_size_type amt;
8394
8395 if (elf_bad_symtab (abfd))
8396 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8397 else
8398 symcount = symtab_hdr->sh_info;
8399 amt = symcount * sizeof (asection *);
8400 n = bfd_zalloc (abfd, amt);
8401 if (n == NULL)
8402 return FALSE;
8403 mips_elf_tdata (abfd)->local_stubs = n;
8404 }
8405
8406 sec->flags |= SEC_KEEP;
8407 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8408
8409 /* We don't need to set mips16_stubs_seen in this case.
8410 That flag is used to see whether we need to look through
8411 the global symbol table for stubs. We don't need to set
8412 it here, because we just have a local stub. */
8413 }
8414 else
8415 {
8416 struct mips_elf_link_hash_entry *h;
8417
8418 h = ((struct mips_elf_link_hash_entry *)
8419 sym_hashes[r_symndx - extsymoff]);
8420
8421 while (h->root.root.type == bfd_link_hash_indirect
8422 || h->root.root.type == bfd_link_hash_warning)
8423 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8424
8425 /* H is the symbol this stub is for. */
8426
8427 /* If we already have an appropriate stub for this function, we
8428 don't need another one, so we can discard this one. Since
8429 this function is called before the linker maps input sections
8430 to output sections, we can easily discard it by setting the
8431 SEC_EXCLUDE flag. */
8432 if (h->fn_stub != NULL)
8433 {
8434 sec->flags |= SEC_EXCLUDE;
8435 return TRUE;
8436 }
8437
8438 sec->flags |= SEC_KEEP;
8439 h->fn_stub = sec;
8440 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
8441 }
8442 }
8443 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8444 {
8445 unsigned long r_symndx;
8446 struct mips_elf_link_hash_entry *h;
8447 asection **loc;
8448
8449 /* Look at the relocation information to figure out which symbol
8450 this is for. */
8451
8452 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8453 if (r_symndx == 0)
8454 {
8455 _bfd_error_handler
8456 /* xgettext:c-format */
8457 (_("%pB: warning: cannot determine the target function for"
8458 " stub section `%s'"),
8459 abfd, name);
8460 bfd_set_error (bfd_error_bad_value);
8461 return FALSE;
8462 }
8463
8464 if (r_symndx < extsymoff
8465 || sym_hashes[r_symndx - extsymoff] == NULL)
8466 {
8467 asection *o;
8468
8469 /* This stub is for a local symbol. This stub will only be
8470 needed if there is some relocation (R_MIPS16_26) in this BFD
8471 that refers to this symbol. */
8472 for (o = abfd->sections; o != NULL; o = o->next)
8473 {
8474 Elf_Internal_Rela *sec_relocs;
8475 const Elf_Internal_Rela *r, *rend;
8476
8477 /* We can ignore stub sections when looking for relocs. */
8478 if ((o->flags & SEC_RELOC) == 0
8479 || o->reloc_count == 0
8480 || section_allows_mips16_refs_p (o))
8481 continue;
8482
8483 sec_relocs
8484 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8485 info->keep_memory);
8486 if (sec_relocs == NULL)
8487 return FALSE;
8488
8489 rend = sec_relocs + o->reloc_count;
8490 for (r = sec_relocs; r < rend; r++)
8491 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8492 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8493 break;
8494
8495 if (elf_section_data (o)->relocs != sec_relocs)
8496 free (sec_relocs);
8497
8498 if (r < rend)
8499 break;
8500 }
8501
8502 if (o == NULL)
8503 {
8504 /* There is no non-call reloc for this stub, so we do
8505 not need it. Since this function is called before
8506 the linker maps input sections to output sections, we
8507 can easily discard it by setting the SEC_EXCLUDE
8508 flag. */
8509 sec->flags |= SEC_EXCLUDE;
8510 return TRUE;
8511 }
8512
8513 /* Record this stub in an array of local symbol call_stubs for
8514 this BFD. */
8515 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8516 {
8517 unsigned long symcount;
8518 asection **n;
8519 bfd_size_type amt;
8520
8521 if (elf_bad_symtab (abfd))
8522 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8523 else
8524 symcount = symtab_hdr->sh_info;
8525 amt = symcount * sizeof (asection *);
8526 n = bfd_zalloc (abfd, amt);
8527 if (n == NULL)
8528 return FALSE;
8529 mips_elf_tdata (abfd)->local_call_stubs = n;
8530 }
8531
8532 sec->flags |= SEC_KEEP;
8533 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8534
8535 /* We don't need to set mips16_stubs_seen in this case.
8536 That flag is used to see whether we need to look through
8537 the global symbol table for stubs. We don't need to set
8538 it here, because we just have a local stub. */
8539 }
8540 else
8541 {
8542 h = ((struct mips_elf_link_hash_entry *)
8543 sym_hashes[r_symndx - extsymoff]);
8544
8545 /* H is the symbol this stub is for. */
8546
8547 if (CALL_FP_STUB_P (name))
8548 loc = &h->call_fp_stub;
8549 else
8550 loc = &h->call_stub;
8551
8552 /* If we already have an appropriate stub for this function, we
8553 don't need another one, so we can discard this one. Since
8554 this function is called before the linker maps input sections
8555 to output sections, we can easily discard it by setting the
8556 SEC_EXCLUDE flag. */
8557 if (*loc != NULL)
8558 {
8559 sec->flags |= SEC_EXCLUDE;
8560 return TRUE;
8561 }
8562
8563 sec->flags |= SEC_KEEP;
8564 *loc = sec;
8565 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
8566 }
8567 }
8568
8569 sreloc = NULL;
8570 contents = NULL;
8571 for (rel = relocs; rel < rel_end; ++rel)
8572 {
8573 unsigned long r_symndx;
8574 unsigned int r_type;
8575 struct elf_link_hash_entry *h;
8576 bfd_boolean can_make_dynamic_p;
8577 bfd_boolean call_reloc_p;
8578 bfd_boolean constrain_symbol_p;
8579
8580 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8581 r_type = ELF_R_TYPE (abfd, rel->r_info);
8582
8583 if (r_symndx < extsymoff)
8584 h = NULL;
8585 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8586 {
8587 _bfd_error_handler
8588 /* xgettext:c-format */
8589 (_("%pB: malformed reloc detected for section %s"),
8590 abfd, name);
8591 bfd_set_error (bfd_error_bad_value);
8592 return FALSE;
8593 }
8594 else
8595 {
8596 h = sym_hashes[r_symndx - extsymoff];
8597 if (h != NULL)
8598 {
8599 while (h->root.type == bfd_link_hash_indirect
8600 || h->root.type == bfd_link_hash_warning)
8601 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8602 }
8603 }
8604
8605 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8606 relocation into a dynamic one. */
8607 can_make_dynamic_p = FALSE;
8608
8609 /* Set CALL_RELOC_P to true if the relocation is for a call,
8610 and if pointer equality therefore doesn't matter. */
8611 call_reloc_p = FALSE;
8612
8613 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8614 into account when deciding how to define the symbol.
8615 Relocations in nonallocatable sections such as .pdr and
8616 .debug* should have no effect. */
8617 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8618
8619 switch (r_type)
8620 {
8621 case R_MIPS_CALL16:
8622 case R_MIPS_CALL_HI16:
8623 case R_MIPS_CALL_LO16:
8624 case R_MIPS16_CALL16:
8625 case R_MICROMIPS_CALL16:
8626 case R_MICROMIPS_CALL_HI16:
8627 case R_MICROMIPS_CALL_LO16:
8628 call_reloc_p = TRUE;
8629 /* Fall through. */
8630
8631 case R_MIPS_GOT16:
8632 case R_MIPS_GOT_LO16:
8633 case R_MIPS_GOT_PAGE:
8634 case R_MIPS_GOT_DISP:
8635 case R_MIPS16_GOT16:
8636 case R_MICROMIPS_GOT16:
8637 case R_MICROMIPS_GOT_LO16:
8638 case R_MICROMIPS_GOT_PAGE:
8639 case R_MICROMIPS_GOT_DISP:
8640 /* If we have a symbol that will resolve to zero at static link
8641 time and it is used by a GOT relocation applied to code we
8642 cannot relax to an immediate zero load, then we will be using
8643 the special `__gnu_absolute_zero' symbol whose value is zero
8644 at dynamic load time. We ignore HI16-type GOT relocations at
8645 this stage, because their handling will depend entirely on
8646 the corresponding LO16-type GOT relocation. */
8647 if (!call_hi16_reloc_p (r_type)
8648 && h != NULL
8649 && bfd_link_pic (info)
8650 && !htab->use_absolute_zero
8651 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8652 {
8653 bfd_boolean rel_reloc;
8654
8655 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8656 return FALSE;
8657
8658 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8659 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8660
8661 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8662 FALSE))
8663 if (!mips_elf_define_absolute_zero (abfd, info, htab, r_type))
8664 return FALSE;
8665 }
8666
8667 /* Fall through. */
8668 case R_MIPS_GOT_HI16:
8669 case R_MIPS_GOT_OFST:
8670 case R_MIPS_TLS_GOTTPREL:
8671 case R_MIPS_TLS_GD:
8672 case R_MIPS_TLS_LDM:
8673 case R_MIPS16_TLS_GOTTPREL:
8674 case R_MIPS16_TLS_GD:
8675 case R_MIPS16_TLS_LDM:
8676 case R_MICROMIPS_GOT_HI16:
8677 case R_MICROMIPS_GOT_OFST:
8678 case R_MICROMIPS_TLS_GOTTPREL:
8679 case R_MICROMIPS_TLS_GD:
8680 case R_MICROMIPS_TLS_LDM:
8681 if (dynobj == NULL)
8682 elf_hash_table (info)->dynobj = dynobj = abfd;
8683 if (!mips_elf_create_got_section (dynobj, info))
8684 return FALSE;
8685 if (htab->is_vxworks && !bfd_link_pic (info))
8686 {
8687 _bfd_error_handler
8688 /* xgettext:c-format */
8689 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8690 abfd, (uint64_t) rel->r_offset);
8691 bfd_set_error (bfd_error_bad_value);
8692 return FALSE;
8693 }
8694 can_make_dynamic_p = TRUE;
8695 break;
8696
8697 case R_MIPS_NONE:
8698 case R_MIPS_JALR:
8699 case R_MICROMIPS_JALR:
8700 /* These relocations have empty fields and are purely there to
8701 provide link information. The symbol value doesn't matter. */
8702 constrain_symbol_p = FALSE;
8703 break;
8704
8705 case R_MIPS_GPREL16:
8706 case R_MIPS_GPREL32:
8707 case R_MIPS16_GPREL:
8708 case R_MICROMIPS_GPREL16:
8709 /* GP-relative relocations always resolve to a definition in a
8710 regular input file, ignoring the one-definition rule. This is
8711 important for the GP setup sequence in NewABI code, which
8712 always resolves to a local function even if other relocations
8713 against the symbol wouldn't. */
8714 constrain_symbol_p = FALSE;
8715 break;
8716
8717 case R_MIPS_32:
8718 case R_MIPS_REL32:
8719 case R_MIPS_64:
8720 /* In VxWorks executables, references to external symbols
8721 must be handled using copy relocs or PLT entries; it is not
8722 possible to convert this relocation into a dynamic one.
8723
8724 For executables that use PLTs and copy-relocs, we have a
8725 choice between converting the relocation into a dynamic
8726 one or using copy relocations or PLT entries. It is
8727 usually better to do the former, unless the relocation is
8728 against a read-only section. */
8729 if ((bfd_link_pic (info)
8730 || (h != NULL
8731 && !htab->is_vxworks
8732 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8733 && !(!info->nocopyreloc
8734 && !PIC_OBJECT_P (abfd)
8735 && MIPS_ELF_READONLY_SECTION (sec))))
8736 && (sec->flags & SEC_ALLOC) != 0)
8737 {
8738 can_make_dynamic_p = TRUE;
8739 if (dynobj == NULL)
8740 elf_hash_table (info)->dynobj = dynobj = abfd;
8741 }
8742 break;
8743
8744 case R_MIPS_26:
8745 case R_MIPS_PC16:
8746 case R_MIPS_PC21_S2:
8747 case R_MIPS_PC26_S2:
8748 case R_MIPS16_26:
8749 case R_MIPS16_PC16_S1:
8750 case R_MICROMIPS_26_S1:
8751 case R_MICROMIPS_PC7_S1:
8752 case R_MICROMIPS_PC10_S1:
8753 case R_MICROMIPS_PC16_S1:
8754 case R_MICROMIPS_PC23_S2:
8755 call_reloc_p = TRUE;
8756 break;
8757 }
8758
8759 if (h)
8760 {
8761 if (constrain_symbol_p)
8762 {
8763 if (!can_make_dynamic_p)
8764 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8765
8766 if (!call_reloc_p)
8767 h->pointer_equality_needed = 1;
8768
8769 /* We must not create a stub for a symbol that has
8770 relocations related to taking the function's address.
8771 This doesn't apply to VxWorks, where CALL relocs refer
8772 to a .got.plt entry instead of a normal .got entry. */
8773 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8774 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8775 }
8776
8777 /* Relocations against the special VxWorks __GOTT_BASE__ and
8778 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8779 room for them in .rela.dyn. */
8780 if (is_gott_symbol (info, h))
8781 {
8782 if (sreloc == NULL)
8783 {
8784 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8785 if (sreloc == NULL)
8786 return FALSE;
8787 }
8788 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8789 if (MIPS_ELF_READONLY_SECTION (sec))
8790 /* We tell the dynamic linker that there are
8791 relocations against the text segment. */
8792 info->flags |= DF_TEXTREL;
8793 }
8794 }
8795 else if (call_lo16_reloc_p (r_type)
8796 || got_lo16_reloc_p (r_type)
8797 || got_disp_reloc_p (r_type)
8798 || (got16_reloc_p (r_type) && htab->is_vxworks))
8799 {
8800 /* We may need a local GOT entry for this relocation. We
8801 don't count R_MIPS_GOT_PAGE because we can estimate the
8802 maximum number of pages needed by looking at the size of
8803 the segment. Similar comments apply to R_MIPS*_GOT16 and
8804 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8805 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8806 R_MIPS_CALL_HI16 because these are always followed by an
8807 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8808 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8809 rel->r_addend, info, r_type))
8810 return FALSE;
8811 }
8812
8813 if (h != NULL
8814 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8815 ELF_ST_IS_MIPS16 (h->other)))
8816 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8817
8818 switch (r_type)
8819 {
8820 case R_MIPS_CALL16:
8821 case R_MIPS16_CALL16:
8822 case R_MICROMIPS_CALL16:
8823 if (h == NULL)
8824 {
8825 _bfd_error_handler
8826 /* xgettext:c-format */
8827 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8828 abfd, (uint64_t) rel->r_offset);
8829 bfd_set_error (bfd_error_bad_value);
8830 return FALSE;
8831 }
8832 /* Fall through. */
8833
8834 case R_MIPS_CALL_HI16:
8835 case R_MIPS_CALL_LO16:
8836 case R_MICROMIPS_CALL_HI16:
8837 case R_MICROMIPS_CALL_LO16:
8838 if (h != NULL)
8839 {
8840 /* Make sure there is room in the regular GOT to hold the
8841 function's address. We may eliminate it in favour of
8842 a .got.plt entry later; see mips_elf_count_got_symbols. */
8843 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8844 r_type))
8845 return FALSE;
8846
8847 /* We need a stub, not a plt entry for the undefined
8848 function. But we record it as if it needs plt. See
8849 _bfd_elf_adjust_dynamic_symbol. */
8850 h->needs_plt = 1;
8851 h->type = STT_FUNC;
8852 }
8853 break;
8854
8855 case R_MIPS_GOT_PAGE:
8856 case R_MICROMIPS_GOT_PAGE:
8857 case R_MIPS16_GOT16:
8858 case R_MIPS_GOT16:
8859 case R_MIPS_GOT_HI16:
8860 case R_MIPS_GOT_LO16:
8861 case R_MICROMIPS_GOT16:
8862 case R_MICROMIPS_GOT_HI16:
8863 case R_MICROMIPS_GOT_LO16:
8864 if (!h || got_page_reloc_p (r_type))
8865 {
8866 /* This relocation needs (or may need, if h != NULL) a
8867 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8868 know for sure until we know whether the symbol is
8869 preemptible. */
8870 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8871 {
8872 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8873 return FALSE;
8874 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8875 addend = mips_elf_read_rel_addend (abfd, rel,
8876 howto, contents);
8877 if (got16_reloc_p (r_type))
8878 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8879 contents, &addend);
8880 else
8881 addend <<= howto->rightshift;
8882 }
8883 else
8884 addend = rel->r_addend;
8885 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8886 h, addend))
8887 return FALSE;
8888
8889 if (h)
8890 {
8891 struct mips_elf_link_hash_entry *hmips =
8892 (struct mips_elf_link_hash_entry *) h;
8893
8894 /* This symbol is definitely not overridable. */
8895 if (hmips->root.def_regular
8896 && ! (bfd_link_pic (info) && ! info->symbolic
8897 && ! hmips->root.forced_local))
8898 h = NULL;
8899 }
8900 }
8901 /* If this is a global, overridable symbol, GOT_PAGE will
8902 decay to GOT_DISP, so we'll need a GOT entry for it. */
8903 /* Fall through. */
8904
8905 case R_MIPS_GOT_DISP:
8906 case R_MICROMIPS_GOT_DISP:
8907 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8908 FALSE, r_type))
8909 return FALSE;
8910 break;
8911
8912 case R_MIPS_TLS_GOTTPREL:
8913 case R_MIPS16_TLS_GOTTPREL:
8914 case R_MICROMIPS_TLS_GOTTPREL:
8915 if (bfd_link_pic (info))
8916 info->flags |= DF_STATIC_TLS;
8917 /* Fall through */
8918
8919 case R_MIPS_TLS_LDM:
8920 case R_MIPS16_TLS_LDM:
8921 case R_MICROMIPS_TLS_LDM:
8922 if (tls_ldm_reloc_p (r_type))
8923 {
8924 r_symndx = STN_UNDEF;
8925 h = NULL;
8926 }
8927 /* Fall through */
8928
8929 case R_MIPS_TLS_GD:
8930 case R_MIPS16_TLS_GD:
8931 case R_MICROMIPS_TLS_GD:
8932 /* This symbol requires a global offset table entry, or two
8933 for TLS GD relocations. */
8934 if (h != NULL)
8935 {
8936 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8937 FALSE, r_type))
8938 return FALSE;
8939 }
8940 else
8941 {
8942 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8943 rel->r_addend,
8944 info, r_type))
8945 return FALSE;
8946 }
8947 break;
8948
8949 case R_MIPS_32:
8950 case R_MIPS_REL32:
8951 case R_MIPS_64:
8952 /* In VxWorks executables, references to external symbols
8953 are handled using copy relocs or PLT stubs, so there's
8954 no need to add a .rela.dyn entry for this relocation. */
8955 if (can_make_dynamic_p)
8956 {
8957 if (sreloc == NULL)
8958 {
8959 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8960 if (sreloc == NULL)
8961 return FALSE;
8962 }
8963 if (bfd_link_pic (info) && h == NULL)
8964 {
8965 /* When creating a shared object, we must copy these
8966 reloc types into the output file as R_MIPS_REL32
8967 relocs. Make room for this reloc in .rel(a).dyn. */
8968 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8969 if (MIPS_ELF_READONLY_SECTION (sec))
8970 /* We tell the dynamic linker that there are
8971 relocations against the text segment. */
8972 info->flags |= DF_TEXTREL;
8973 }
8974 else
8975 {
8976 struct mips_elf_link_hash_entry *hmips;
8977
8978 /* For a shared object, we must copy this relocation
8979 unless the symbol turns out to be undefined and
8980 weak with non-default visibility, in which case
8981 it will be left as zero.
8982
8983 We could elide R_MIPS_REL32 for locally binding symbols
8984 in shared libraries, but do not yet do so.
8985
8986 For an executable, we only need to copy this
8987 reloc if the symbol is defined in a dynamic
8988 object. */
8989 hmips = (struct mips_elf_link_hash_entry *) h;
8990 ++hmips->possibly_dynamic_relocs;
8991 if (MIPS_ELF_READONLY_SECTION (sec))
8992 /* We need it to tell the dynamic linker if there
8993 are relocations against the text segment. */
8994 hmips->readonly_reloc = TRUE;
8995 }
8996 }
8997
8998 if (SGI_COMPAT (abfd))
8999 mips_elf_hash_table (info)->compact_rel_size +=
9000 sizeof (Elf32_External_crinfo);
9001 break;
9002
9003 case R_MIPS_26:
9004 case R_MIPS_GPREL16:
9005 case R_MIPS_LITERAL:
9006 case R_MIPS_GPREL32:
9007 case R_MICROMIPS_26_S1:
9008 case R_MICROMIPS_GPREL16:
9009 case R_MICROMIPS_LITERAL:
9010 case R_MICROMIPS_GPREL7_S2:
9011 if (SGI_COMPAT (abfd))
9012 mips_elf_hash_table (info)->compact_rel_size +=
9013 sizeof (Elf32_External_crinfo);
9014 break;
9015
9016 /* This relocation describes the C++ object vtable hierarchy.
9017 Reconstruct it for later use during GC. */
9018 case R_MIPS_GNU_VTINHERIT:
9019 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9020 return FALSE;
9021 break;
9022
9023 /* This relocation describes which C++ vtable entries are actually
9024 used. Record for later use during GC. */
9025 case R_MIPS_GNU_VTENTRY:
9026 BFD_ASSERT (h != NULL);
9027 if (h != NULL
9028 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9029 return FALSE;
9030 break;
9031
9032 default:
9033 break;
9034 }
9035
9036 /* Record the need for a PLT entry. At this point we don't know
9037 yet if we are going to create a PLT in the first place, but
9038 we only record whether the relocation requires a standard MIPS
9039 or a compressed code entry anyway. If we don't make a PLT after
9040 all, then we'll just ignore these arrangements. Likewise if
9041 a PLT entry is not created because the symbol is satisfied
9042 locally. */
9043 if (h != NULL
9044 && (branch_reloc_p (r_type)
9045 || mips16_branch_reloc_p (r_type)
9046 || micromips_branch_reloc_p (r_type))
9047 && !SYMBOL_CALLS_LOCAL (info, h))
9048 {
9049 if (h->plt.plist == NULL)
9050 h->plt.plist = mips_elf_make_plt_record (abfd);
9051 if (h->plt.plist == NULL)
9052 return FALSE;
9053
9054 if (branch_reloc_p (r_type))
9055 h->plt.plist->need_mips = TRUE;
9056 else
9057 h->plt.plist->need_comp = TRUE;
9058 }
9059
9060 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9061 if there is one. We only need to handle global symbols here;
9062 we decide whether to keep or delete stubs for local symbols
9063 when processing the stub's relocations. */
9064 if (h != NULL
9065 && !mips16_call_reloc_p (r_type)
9066 && !section_allows_mips16_refs_p (sec))
9067 {
9068 struct mips_elf_link_hash_entry *mh;
9069
9070 mh = (struct mips_elf_link_hash_entry *) h;
9071 mh->need_fn_stub = TRUE;
9072 }
9073
9074 /* Refuse some position-dependent relocations when creating a
9075 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9076 not PIC, but we can create dynamic relocations and the result
9077 will be fine. Also do not refuse R_MIPS_LO16, which can be
9078 combined with R_MIPS_GOT16. */
9079 if (bfd_link_pic (info))
9080 {
9081 switch (r_type)
9082 {
9083 case R_MIPS16_HI16:
9084 case R_MIPS_HI16:
9085 case R_MIPS_HIGHER:
9086 case R_MIPS_HIGHEST:
9087 case R_MICROMIPS_HI16:
9088 case R_MICROMIPS_HIGHER:
9089 case R_MICROMIPS_HIGHEST:
9090 /* Don't refuse a high part relocation if it's against
9091 no symbol (e.g. part of a compound relocation). */
9092 if (r_symndx == STN_UNDEF)
9093 break;
9094
9095 /* Likewise an absolute symbol. */
9096 if (bfd_is_abs_symbol (&h->root))
9097 break;
9098
9099 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9100 and has a special meaning. */
9101 if (!NEWABI_P (abfd) && h != NULL
9102 && strcmp (h->root.root.string, "_gp_disp") == 0)
9103 break;
9104
9105 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9106 if (is_gott_symbol (info, h))
9107 break;
9108
9109 /* FALLTHROUGH */
9110
9111 case R_MIPS16_26:
9112 case R_MIPS_26:
9113 case R_MICROMIPS_26_S1:
9114 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
9115 info->callbacks->einfo
9116 /* xgettext:c-format */
9117 (_("%X%H: relocation %s against `%s' cannot be used"
9118 " when making a shared object; recompile with -fPIC\n"),
9119 abfd, sec, rel->r_offset, howto->name,
9120 (h) ? h->root.root.string : "a local symbol");
9121 break;
9122 default:
9123 break;
9124 }
9125 }
9126 }
9127
9128 return TRUE;
9129 }
9130
9131 /* Allocate space for global sym dynamic relocs. */
9133
9134 static bfd_boolean
9135 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9136 {
9137 struct bfd_link_info *info = inf;
9138 bfd *dynobj;
9139 struct mips_elf_link_hash_entry *hmips;
9140 struct mips_elf_link_hash_table *htab;
9141
9142 htab = mips_elf_hash_table (info);
9143 BFD_ASSERT (htab != NULL);
9144
9145 dynobj = elf_hash_table (info)->dynobj;
9146 hmips = (struct mips_elf_link_hash_entry *) h;
9147
9148 /* VxWorks executables are handled elsewhere; we only need to
9149 allocate relocations in shared objects. */
9150 if (htab->is_vxworks && !bfd_link_pic (info))
9151 return TRUE;
9152
9153 /* Ignore indirect symbols. All relocations against such symbols
9154 will be redirected to the target symbol. */
9155 if (h->root.type == bfd_link_hash_indirect)
9156 return TRUE;
9157
9158 /* If this symbol is defined in a dynamic object, or we are creating
9159 a shared library, we will need to copy any R_MIPS_32 or
9160 R_MIPS_REL32 relocs against it into the output file. */
9161 if (! bfd_link_relocatable (info)
9162 && hmips->possibly_dynamic_relocs != 0
9163 && (h->root.type == bfd_link_hash_defweak
9164 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9165 || bfd_link_pic (info)))
9166 {
9167 bfd_boolean do_copy = TRUE;
9168
9169 if (h->root.type == bfd_link_hash_undefweak)
9170 {
9171 /* Do not copy relocations for undefined weak symbols that
9172 we are not going to export. */
9173 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9174 do_copy = FALSE;
9175
9176 /* Make sure undefined weak symbols are output as a dynamic
9177 symbol in PIEs. */
9178 else if (h->dynindx == -1 && !h->forced_local)
9179 {
9180 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9181 return FALSE;
9182 }
9183 }
9184
9185 if (do_copy)
9186 {
9187 /* Even though we don't directly need a GOT entry for this symbol,
9188 the SVR4 psABI requires it to have a dynamic symbol table
9189 index greater that DT_MIPS_GOTSYM if there are dynamic
9190 relocations against it.
9191
9192 VxWorks does not enforce the same mapping between the GOT
9193 and the symbol table, so the same requirement does not
9194 apply there. */
9195 if (!htab->is_vxworks)
9196 {
9197 if (hmips->global_got_area > GGA_RELOC_ONLY)
9198 hmips->global_got_area = GGA_RELOC_ONLY;
9199 hmips->got_only_for_calls = FALSE;
9200 }
9201
9202 mips_elf_allocate_dynamic_relocations
9203 (dynobj, info, hmips->possibly_dynamic_relocs);
9204 if (hmips->readonly_reloc)
9205 /* We tell the dynamic linker that there are relocations
9206 against the text segment. */
9207 info->flags |= DF_TEXTREL;
9208 }
9209 }
9210
9211 return TRUE;
9212 }
9213
9214 /* Adjust a symbol defined by a dynamic object and referenced by a
9215 regular object. The current definition is in some section of the
9216 dynamic object, but we're not including those sections. We have to
9217 change the definition to something the rest of the link can
9218 understand. */
9219
9220 bfd_boolean
9221 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9222 struct elf_link_hash_entry *h)
9223 {
9224 bfd *dynobj;
9225 struct mips_elf_link_hash_entry *hmips;
9226 struct mips_elf_link_hash_table *htab;
9227 asection *s, *srel;
9228
9229 htab = mips_elf_hash_table (info);
9230 BFD_ASSERT (htab != NULL);
9231
9232 dynobj = elf_hash_table (info)->dynobj;
9233 hmips = (struct mips_elf_link_hash_entry *) h;
9234
9235 /* Make sure we know what is going on here. */
9236 BFD_ASSERT (dynobj != NULL
9237 && (h->needs_plt
9238 || h->is_weakalias
9239 || (h->def_dynamic
9240 && h->ref_regular
9241 && !h->def_regular)));
9242
9243 hmips = (struct mips_elf_link_hash_entry *) h;
9244
9245 /* If there are call relocations against an externally-defined symbol,
9246 see whether we can create a MIPS lazy-binding stub for it. We can
9247 only do this if all references to the function are through call
9248 relocations, and in that case, the traditional lazy-binding stubs
9249 are much more efficient than PLT entries.
9250
9251 Traditional stubs are only available on SVR4 psABI-based systems;
9252 VxWorks always uses PLTs instead. */
9253 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
9254 {
9255 if (! elf_hash_table (info)->dynamic_sections_created)
9256 return TRUE;
9257
9258 /* If this symbol is not defined in a regular file, then set
9259 the symbol to the stub location. This is required to make
9260 function pointers compare as equal between the normal
9261 executable and the shared library. */
9262 if (!h->def_regular
9263 && !bfd_is_abs_section (htab->sstubs->output_section))
9264 {
9265 hmips->needs_lazy_stub = TRUE;
9266 htab->lazy_stub_count++;
9267 return TRUE;
9268 }
9269 }
9270 /* As above, VxWorks requires PLT entries for externally-defined
9271 functions that are only accessed through call relocations.
9272
9273 Both VxWorks and non-VxWorks targets also need PLT entries if there
9274 are static-only relocations against an externally-defined function.
9275 This can technically occur for shared libraries if there are
9276 branches to the symbol, although it is unlikely that this will be
9277 used in practice due to the short ranges involved. It can occur
9278 for any relative or absolute relocation in executables; in that
9279 case, the PLT entry becomes the function's canonical address. */
9280 else if (((h->needs_plt && !hmips->no_fn_stub)
9281 || (h->type == STT_FUNC && hmips->has_static_relocs))
9282 && htab->use_plts_and_copy_relocs
9283 && !SYMBOL_CALLS_LOCAL (info, h)
9284 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9285 && h->root.type == bfd_link_hash_undefweak))
9286 {
9287 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9288 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
9289
9290 /* If this is the first symbol to need a PLT entry, then make some
9291 basic setup. Also work out PLT entry sizes. We'll need them
9292 for PLT offset calculations. */
9293 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9294 {
9295 BFD_ASSERT (htab->root.sgotplt->size == 0);
9296 BFD_ASSERT (htab->plt_got_index == 0);
9297
9298 /* If we're using the PLT additions to the psABI, each PLT
9299 entry is 16 bytes and the PLT0 entry is 32 bytes.
9300 Encourage better cache usage by aligning. We do this
9301 lazily to avoid pessimizing traditional objects. */
9302 if (!htab->is_vxworks
9303 && !bfd_set_section_alignment (dynobj, htab->root.splt, 5))
9304 return FALSE;
9305
9306 /* Make sure that .got.plt is word-aligned. We do this lazily
9307 for the same reason as above. */
9308 if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt,
9309 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9310 return FALSE;
9311
9312 /* On non-VxWorks targets, the first two entries in .got.plt
9313 are reserved. */
9314 if (!htab->is_vxworks)
9315 htab->plt_got_index
9316 += (get_elf_backend_data (dynobj)->got_header_size
9317 / MIPS_ELF_GOT_SIZE (dynobj));
9318
9319 /* On VxWorks, also allocate room for the header's
9320 .rela.plt.unloaded entries. */
9321 if (htab->is_vxworks && !bfd_link_pic (info))
9322 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9323
9324 /* Now work out the sizes of individual PLT entries. */
9325 if (htab->is_vxworks && bfd_link_pic (info))
9326 htab->plt_mips_entry_size
9327 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9328 else if (htab->is_vxworks)
9329 htab->plt_mips_entry_size
9330 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9331 else if (newabi_p)
9332 htab->plt_mips_entry_size
9333 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9334 else if (!micromips_p)
9335 {
9336 htab->plt_mips_entry_size
9337 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9338 htab->plt_comp_entry_size
9339 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9340 }
9341 else if (htab->insn32)
9342 {
9343 htab->plt_mips_entry_size
9344 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9345 htab->plt_comp_entry_size
9346 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9347 }
9348 else
9349 {
9350 htab->plt_mips_entry_size
9351 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9352 htab->plt_comp_entry_size
9353 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9354 }
9355 }
9356
9357 if (h->plt.plist == NULL)
9358 h->plt.plist = mips_elf_make_plt_record (dynobj);
9359 if (h->plt.plist == NULL)
9360 return FALSE;
9361
9362 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9363 n32 or n64, so always use a standard entry there.
9364
9365 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9366 all MIPS16 calls will go via that stub, and there is no benefit
9367 to having a MIPS16 entry. And in the case of call_stub a
9368 standard entry actually has to be used as the stub ends with a J
9369 instruction. */
9370 if (newabi_p
9371 || htab->is_vxworks
9372 || hmips->call_stub
9373 || hmips->call_fp_stub)
9374 {
9375 h->plt.plist->need_mips = TRUE;
9376 h->plt.plist->need_comp = FALSE;
9377 }
9378
9379 /* Otherwise, if there are no direct calls to the function, we
9380 have a free choice of whether to use standard or compressed
9381 entries. Prefer microMIPS entries if the object is known to
9382 contain microMIPS code, so that it becomes possible to create
9383 pure microMIPS binaries. Prefer standard entries otherwise,
9384 because MIPS16 ones are no smaller and are usually slower. */
9385 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9386 {
9387 if (micromips_p)
9388 h->plt.plist->need_comp = TRUE;
9389 else
9390 h->plt.plist->need_mips = TRUE;
9391 }
9392
9393 if (h->plt.plist->need_mips)
9394 {
9395 h->plt.plist->mips_offset = htab->plt_mips_offset;
9396 htab->plt_mips_offset += htab->plt_mips_entry_size;
9397 }
9398 if (h->plt.plist->need_comp)
9399 {
9400 h->plt.plist->comp_offset = htab->plt_comp_offset;
9401 htab->plt_comp_offset += htab->plt_comp_entry_size;
9402 }
9403
9404 /* Reserve the corresponding .got.plt entry now too. */
9405 h->plt.plist->gotplt_index = htab->plt_got_index++;
9406
9407 /* If the output file has no definition of the symbol, set the
9408 symbol's value to the address of the stub. */
9409 if (!bfd_link_pic (info) && !h->def_regular)
9410 hmips->use_plt_entry = TRUE;
9411
9412 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
9413 htab->root.srelplt->size += (htab->is_vxworks
9414 ? MIPS_ELF_RELA_SIZE (dynobj)
9415 : MIPS_ELF_REL_SIZE (dynobj));
9416
9417 /* Make room for the .rela.plt.unloaded relocations. */
9418 if (htab->is_vxworks && !bfd_link_pic (info))
9419 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9420
9421 /* All relocations against this symbol that could have been made
9422 dynamic will now refer to the PLT entry instead. */
9423 hmips->possibly_dynamic_relocs = 0;
9424
9425 return TRUE;
9426 }
9427
9428 /* If this is a weak symbol, and there is a real definition, the
9429 processor independent code will have arranged for us to see the
9430 real definition first, and we can just use the same value. */
9431 if (h->is_weakalias)
9432 {
9433 struct elf_link_hash_entry *def = weakdef (h);
9434 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9435 h->root.u.def.section = def->root.u.def.section;
9436 h->root.u.def.value = def->root.u.def.value;
9437 return TRUE;
9438 }
9439
9440 /* Otherwise, there is nothing further to do for symbols defined
9441 in regular objects. */
9442 if (h->def_regular)
9443 return TRUE;
9444
9445 /* There's also nothing more to do if we'll convert all relocations
9446 against this symbol into dynamic relocations. */
9447 if (!hmips->has_static_relocs)
9448 return TRUE;
9449
9450 /* We're now relying on copy relocations. Complain if we have
9451 some that we can't convert. */
9452 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9453 {
9454 _bfd_error_handler (_("non-dynamic relocations refer to "
9455 "dynamic symbol %s"),
9456 h->root.root.string);
9457 bfd_set_error (bfd_error_bad_value);
9458 return FALSE;
9459 }
9460
9461 /* We must allocate the symbol in our .dynbss section, which will
9462 become part of the .bss section of the executable. There will be
9463 an entry for this symbol in the .dynsym section. The dynamic
9464 object will contain position independent code, so all references
9465 from the dynamic object to this symbol will go through the global
9466 offset table. The dynamic linker will use the .dynsym entry to
9467 determine the address it must put in the global offset table, so
9468 both the dynamic object and the regular object will refer to the
9469 same memory location for the variable. */
9470
9471 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9472 {
9473 s = htab->root.sdynrelro;
9474 srel = htab->root.sreldynrelro;
9475 }
9476 else
9477 {
9478 s = htab->root.sdynbss;
9479 srel = htab->root.srelbss;
9480 }
9481 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9482 {
9483 if (htab->is_vxworks)
9484 srel->size += sizeof (Elf32_External_Rela);
9485 else
9486 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9487 h->needs_copy = 1;
9488 }
9489
9490 /* All relocations against this symbol that could have been made
9491 dynamic will now refer to the local copy instead. */
9492 hmips->possibly_dynamic_relocs = 0;
9493
9494 return _bfd_elf_adjust_dynamic_copy (info, h, s);
9495 }
9496
9497 /* This function is called after all the input files have been read,
9499 and the input sections have been assigned to output sections. We
9500 check for any mips16 stub sections that we can discard. */
9501
9502 bfd_boolean
9503 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9504 struct bfd_link_info *info)
9505 {
9506 asection *sect;
9507 struct mips_elf_link_hash_table *htab;
9508 struct mips_htab_traverse_info hti;
9509
9510 htab = mips_elf_hash_table (info);
9511 BFD_ASSERT (htab != NULL);
9512
9513 /* The .reginfo section has a fixed size. */
9514 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9515 if (sect != NULL)
9516 {
9517 bfd_set_section_size (output_bfd, sect, sizeof (Elf32_External_RegInfo));
9518 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9519 }
9520
9521 /* The .MIPS.abiflags section has a fixed size. */
9522 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9523 if (sect != NULL)
9524 {
9525 bfd_set_section_size (output_bfd, sect,
9526 sizeof (Elf_External_ABIFlags_v0));
9527 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9528 }
9529
9530 hti.info = info;
9531 hti.output_bfd = output_bfd;
9532 hti.error = FALSE;
9533 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9534 mips_elf_check_symbols, &hti);
9535 if (hti.error)
9536 return FALSE;
9537
9538 return TRUE;
9539 }
9540
9541 /* If the link uses a GOT, lay it out and work out its size. */
9542
9543 static bfd_boolean
9544 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9545 {
9546 bfd *dynobj;
9547 asection *s;
9548 struct mips_got_info *g;
9549 bfd_size_type loadable_size = 0;
9550 bfd_size_type page_gotno;
9551 bfd *ibfd;
9552 struct mips_elf_traverse_got_arg tga;
9553 struct mips_elf_link_hash_table *htab;
9554
9555 htab = mips_elf_hash_table (info);
9556 BFD_ASSERT (htab != NULL);
9557
9558 s = htab->root.sgot;
9559 if (s == NULL)
9560 return TRUE;
9561
9562 dynobj = elf_hash_table (info)->dynobj;
9563 g = htab->got_info;
9564
9565 /* Allocate room for the reserved entries. VxWorks always reserves
9566 3 entries; other objects only reserve 2 entries. */
9567 BFD_ASSERT (g->assigned_low_gotno == 0);
9568 if (htab->is_vxworks)
9569 htab->reserved_gotno = 3;
9570 else
9571 htab->reserved_gotno = 2;
9572 g->local_gotno += htab->reserved_gotno;
9573 g->assigned_low_gotno = htab->reserved_gotno;
9574
9575 /* Decide which symbols need to go in the global part of the GOT and
9576 count the number of reloc-only GOT symbols. */
9577 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9578
9579 if (!mips_elf_resolve_final_got_entries (info, g))
9580 return FALSE;
9581
9582 /* Calculate the total loadable size of the output. That
9583 will give us the maximum number of GOT_PAGE entries
9584 required. */
9585 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9586 {
9587 asection *subsection;
9588
9589 for (subsection = ibfd->sections;
9590 subsection;
9591 subsection = subsection->next)
9592 {
9593 if ((subsection->flags & SEC_ALLOC) == 0)
9594 continue;
9595 loadable_size += ((subsection->size + 0xf)
9596 &~ (bfd_size_type) 0xf);
9597 }
9598 }
9599
9600 if (htab->is_vxworks)
9601 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9602 relocations against local symbols evaluate to "G", and the EABI does
9603 not include R_MIPS_GOT_PAGE. */
9604 page_gotno = 0;
9605 else
9606 /* Assume there are two loadable segments consisting of contiguous
9607 sections. Is 5 enough? */
9608 page_gotno = (loadable_size >> 16) + 5;
9609
9610 /* Choose the smaller of the two page estimates; both are intended to be
9611 conservative. */
9612 if (page_gotno > g->page_gotno)
9613 page_gotno = g->page_gotno;
9614
9615 g->local_gotno += page_gotno;
9616 g->assigned_high_gotno = g->local_gotno - 1;
9617
9618 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9619 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9620 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9621
9622 /* VxWorks does not support multiple GOTs. It initializes $gp to
9623 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9624 dynamic loader. */
9625 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9626 {
9627 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9628 return FALSE;
9629 }
9630 else
9631 {
9632 /* Record that all bfds use G. This also has the effect of freeing
9633 the per-bfd GOTs, which we no longer need. */
9634 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9635 if (mips_elf_bfd_got (ibfd, FALSE))
9636 mips_elf_replace_bfd_got (ibfd, g);
9637 mips_elf_replace_bfd_got (output_bfd, g);
9638
9639 /* Set up TLS entries. */
9640 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9641 tga.info = info;
9642 tga.g = g;
9643 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9644 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9645 if (!tga.g)
9646 return FALSE;
9647 BFD_ASSERT (g->tls_assigned_gotno
9648 == g->global_gotno + g->local_gotno + g->tls_gotno);
9649
9650 /* Each VxWorks GOT entry needs an explicit relocation. */
9651 if (htab->is_vxworks && bfd_link_pic (info))
9652 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9653
9654 /* Allocate room for the TLS relocations. */
9655 if (g->relocs)
9656 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9657 }
9658
9659 return TRUE;
9660 }
9661
9662 /* Estimate the size of the .MIPS.stubs section. */
9663
9664 static void
9665 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9666 {
9667 struct mips_elf_link_hash_table *htab;
9668 bfd_size_type dynsymcount;
9669
9670 htab = mips_elf_hash_table (info);
9671 BFD_ASSERT (htab != NULL);
9672
9673 if (htab->lazy_stub_count == 0)
9674 return;
9675
9676 /* IRIX rld assumes that a function stub isn't at the end of the .text
9677 section, so add a dummy entry to the end. */
9678 htab->lazy_stub_count++;
9679
9680 /* Get a worst-case estimate of the number of dynamic symbols needed.
9681 At this point, dynsymcount does not account for section symbols
9682 and count_section_dynsyms may overestimate the number that will
9683 be needed. */
9684 dynsymcount = (elf_hash_table (info)->dynsymcount
9685 + count_section_dynsyms (output_bfd, info));
9686
9687 /* Determine the size of one stub entry. There's no disadvantage
9688 from using microMIPS code here, so for the sake of pure-microMIPS
9689 binaries we prefer it whenever there's any microMIPS code in
9690 output produced at all. This has a benefit of stubs being
9691 shorter by 4 bytes each too, unless in the insn32 mode. */
9692 if (!MICROMIPS_P (output_bfd))
9693 htab->function_stub_size = (dynsymcount > 0x10000
9694 ? MIPS_FUNCTION_STUB_BIG_SIZE
9695 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9696 else if (htab->insn32)
9697 htab->function_stub_size = (dynsymcount > 0x10000
9698 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9699 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9700 else
9701 htab->function_stub_size = (dynsymcount > 0x10000
9702 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9703 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9704
9705 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9706 }
9707
9708 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9709 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9710 stub, allocate an entry in the stubs section. */
9711
9712 static bfd_boolean
9713 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9714 {
9715 struct mips_htab_traverse_info *hti = data;
9716 struct mips_elf_link_hash_table *htab;
9717 struct bfd_link_info *info;
9718 bfd *output_bfd;
9719
9720 info = hti->info;
9721 output_bfd = hti->output_bfd;
9722 htab = mips_elf_hash_table (info);
9723 BFD_ASSERT (htab != NULL);
9724
9725 if (h->needs_lazy_stub)
9726 {
9727 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9728 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9729 bfd_vma isa_bit = micromips_p;
9730
9731 BFD_ASSERT (htab->root.dynobj != NULL);
9732 if (h->root.plt.plist == NULL)
9733 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9734 if (h->root.plt.plist == NULL)
9735 {
9736 hti->error = TRUE;
9737 return FALSE;
9738 }
9739 h->root.root.u.def.section = htab->sstubs;
9740 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9741 h->root.plt.plist->stub_offset = htab->sstubs->size;
9742 h->root.other = other;
9743 htab->sstubs->size += htab->function_stub_size;
9744 }
9745 return TRUE;
9746 }
9747
9748 /* Allocate offsets in the stubs section to each symbol that needs one.
9749 Set the final size of the .MIPS.stub section. */
9750
9751 static bfd_boolean
9752 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9753 {
9754 bfd *output_bfd = info->output_bfd;
9755 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9756 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9757 bfd_vma isa_bit = micromips_p;
9758 struct mips_elf_link_hash_table *htab;
9759 struct mips_htab_traverse_info hti;
9760 struct elf_link_hash_entry *h;
9761 bfd *dynobj;
9762
9763 htab = mips_elf_hash_table (info);
9764 BFD_ASSERT (htab != NULL);
9765
9766 if (htab->lazy_stub_count == 0)
9767 return TRUE;
9768
9769 htab->sstubs->size = 0;
9770 hti.info = info;
9771 hti.output_bfd = output_bfd;
9772 hti.error = FALSE;
9773 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9774 if (hti.error)
9775 return FALSE;
9776 htab->sstubs->size += htab->function_stub_size;
9777 BFD_ASSERT (htab->sstubs->size
9778 == htab->lazy_stub_count * htab->function_stub_size);
9779
9780 dynobj = elf_hash_table (info)->dynobj;
9781 BFD_ASSERT (dynobj != NULL);
9782 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9783 if (h == NULL)
9784 return FALSE;
9785 h->root.u.def.value = isa_bit;
9786 h->other = other;
9787 h->type = STT_FUNC;
9788
9789 return TRUE;
9790 }
9791
9792 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9793 bfd_link_info. If H uses the address of a PLT entry as the value
9794 of the symbol, then set the entry in the symbol table now. Prefer
9795 a standard MIPS PLT entry. */
9796
9797 static bfd_boolean
9798 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9799 {
9800 struct bfd_link_info *info = data;
9801 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9802 struct mips_elf_link_hash_table *htab;
9803 unsigned int other;
9804 bfd_vma isa_bit;
9805 bfd_vma val;
9806
9807 htab = mips_elf_hash_table (info);
9808 BFD_ASSERT (htab != NULL);
9809
9810 if (h->use_plt_entry)
9811 {
9812 BFD_ASSERT (h->root.plt.plist != NULL);
9813 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9814 || h->root.plt.plist->comp_offset != MINUS_ONE);
9815
9816 val = htab->plt_header_size;
9817 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9818 {
9819 isa_bit = 0;
9820 val += h->root.plt.plist->mips_offset;
9821 other = 0;
9822 }
9823 else
9824 {
9825 isa_bit = 1;
9826 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9827 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9828 }
9829 val += isa_bit;
9830 /* For VxWorks, point at the PLT load stub rather than the lazy
9831 resolution stub; this stub will become the canonical function
9832 address. */
9833 if (htab->is_vxworks)
9834 val += 8;
9835
9836 h->root.root.u.def.section = htab->root.splt;
9837 h->root.root.u.def.value = val;
9838 h->root.other = other;
9839 }
9840
9841 return TRUE;
9842 }
9843
9844 /* Set the sizes of the dynamic sections. */
9845
9846 bfd_boolean
9847 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9848 struct bfd_link_info *info)
9849 {
9850 bfd *dynobj;
9851 asection *s, *sreldyn;
9852 bfd_boolean reltext;
9853 struct mips_elf_link_hash_table *htab;
9854
9855 htab = mips_elf_hash_table (info);
9856 BFD_ASSERT (htab != NULL);
9857 dynobj = elf_hash_table (info)->dynobj;
9858 BFD_ASSERT (dynobj != NULL);
9859
9860 if (elf_hash_table (info)->dynamic_sections_created)
9861 {
9862 /* Set the contents of the .interp section to the interpreter. */
9863 if (bfd_link_executable (info) && !info->nointerp)
9864 {
9865 s = bfd_get_linker_section (dynobj, ".interp");
9866 BFD_ASSERT (s != NULL);
9867 s->size
9868 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9869 s->contents
9870 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9871 }
9872
9873 /* Figure out the size of the PLT header if we know that we
9874 are using it. For the sake of cache alignment always use
9875 a standard header whenever any standard entries are present
9876 even if microMIPS entries are present as well. This also
9877 lets the microMIPS header rely on the value of $v0 only set
9878 by microMIPS entries, for a small size reduction.
9879
9880 Set symbol table entry values for symbols that use the
9881 address of their PLT entry now that we can calculate it.
9882
9883 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9884 haven't already in _bfd_elf_create_dynamic_sections. */
9885 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
9886 {
9887 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9888 && !htab->plt_mips_offset);
9889 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9890 bfd_vma isa_bit = micromips_p;
9891 struct elf_link_hash_entry *h;
9892 bfd_vma size;
9893
9894 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9895 BFD_ASSERT (htab->root.sgotplt->size == 0);
9896 BFD_ASSERT (htab->root.splt->size == 0);
9897
9898 if (htab->is_vxworks && bfd_link_pic (info))
9899 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9900 else if (htab->is_vxworks)
9901 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9902 else if (ABI_64_P (output_bfd))
9903 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9904 else if (ABI_N32_P (output_bfd))
9905 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9906 else if (!micromips_p)
9907 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
9908 else if (htab->insn32)
9909 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
9910 else
9911 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
9912
9913 htab->plt_header_is_comp = micromips_p;
9914 htab->plt_header_size = size;
9915 htab->root.splt->size = (size
9916 + htab->plt_mips_offset
9917 + htab->plt_comp_offset);
9918 htab->root.sgotplt->size = (htab->plt_got_index
9919 * MIPS_ELF_GOT_SIZE (dynobj));
9920
9921 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9922
9923 if (htab->root.hplt == NULL)
9924 {
9925 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
9926 "_PROCEDURE_LINKAGE_TABLE_");
9927 htab->root.hplt = h;
9928 if (h == NULL)
9929 return FALSE;
9930 }
9931
9932 h = htab->root.hplt;
9933 h->root.u.def.value = isa_bit;
9934 h->other = other;
9935 h->type = STT_FUNC;
9936 }
9937 }
9938
9939 /* Allocate space for global sym dynamic relocs. */
9940 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9941
9942 mips_elf_estimate_stub_size (output_bfd, info);
9943
9944 if (!mips_elf_lay_out_got (output_bfd, info))
9945 return FALSE;
9946
9947 mips_elf_lay_out_lazy_stubs (info);
9948
9949 /* The check_relocs and adjust_dynamic_symbol entry points have
9950 determined the sizes of the various dynamic sections. Allocate
9951 memory for them. */
9952 reltext = FALSE;
9953 for (s = dynobj->sections; s != NULL; s = s->next)
9954 {
9955 const char *name;
9956
9957 /* It's OK to base decisions on the section name, because none
9958 of the dynobj section names depend upon the input files. */
9959 name = bfd_get_section_name (dynobj, s);
9960
9961 if ((s->flags & SEC_LINKER_CREATED) == 0)
9962 continue;
9963
9964 if (CONST_STRNEQ (name, ".rel"))
9965 {
9966 if (s->size != 0)
9967 {
9968 const char *outname;
9969 asection *target;
9970
9971 /* If this relocation section applies to a read only
9972 section, then we probably need a DT_TEXTREL entry.
9973 If the relocation section is .rel(a).dyn, we always
9974 assert a DT_TEXTREL entry rather than testing whether
9975 there exists a relocation to a read only section or
9976 not. */
9977 outname = bfd_get_section_name (output_bfd,
9978 s->output_section);
9979 target = bfd_get_section_by_name (output_bfd, outname + 4);
9980 if ((target != NULL
9981 && (target->flags & SEC_READONLY) != 0
9982 && (target->flags & SEC_ALLOC) != 0)
9983 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
9984 reltext = TRUE;
9985
9986 /* We use the reloc_count field as a counter if we need
9987 to copy relocs into the output file. */
9988 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
9989 s->reloc_count = 0;
9990
9991 /* If combreloc is enabled, elf_link_sort_relocs() will
9992 sort relocations, but in a different way than we do,
9993 and before we're done creating relocations. Also, it
9994 will move them around between input sections'
9995 relocation's contents, so our sorting would be
9996 broken, so don't let it run. */
9997 info->combreloc = 0;
9998 }
9999 }
10000 else if (bfd_link_executable (info)
10001 && ! mips_elf_hash_table (info)->use_rld_obj_head
10002 && CONST_STRNEQ (name, ".rld_map"))
10003 {
10004 /* We add a room for __rld_map. It will be filled in by the
10005 rtld to contain a pointer to the _r_debug structure. */
10006 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10007 }
10008 else if (SGI_COMPAT (output_bfd)
10009 && CONST_STRNEQ (name, ".compact_rel"))
10010 s->size += mips_elf_hash_table (info)->compact_rel_size;
10011 else if (s == htab->root.splt)
10012 {
10013 /* If the last PLT entry has a branch delay slot, allocate
10014 room for an extra nop to fill the delay slot. This is
10015 for CPUs without load interlocking. */
10016 if (! LOAD_INTERLOCKS_P (output_bfd)
10017 && ! htab->is_vxworks && s->size > 0)
10018 s->size += 4;
10019 }
10020 else if (! CONST_STRNEQ (name, ".init")
10021 && s != htab->root.sgot
10022 && s != htab->root.sgotplt
10023 && s != htab->sstubs
10024 && s != htab->root.sdynbss
10025 && s != htab->root.sdynrelro)
10026 {
10027 /* It's not one of our sections, so don't allocate space. */
10028 continue;
10029 }
10030
10031 if (s->size == 0)
10032 {
10033 s->flags |= SEC_EXCLUDE;
10034 continue;
10035 }
10036
10037 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10038 continue;
10039
10040 /* Allocate memory for the section contents. */
10041 s->contents = bfd_zalloc (dynobj, s->size);
10042 if (s->contents == NULL)
10043 {
10044 bfd_set_error (bfd_error_no_memory);
10045 return FALSE;
10046 }
10047 }
10048
10049 if (elf_hash_table (info)->dynamic_sections_created)
10050 {
10051 /* Add some entries to the .dynamic section. We fill in the
10052 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10053 must add the entries now so that we get the correct size for
10054 the .dynamic section. */
10055
10056 /* SGI object has the equivalence of DT_DEBUG in the
10057 DT_MIPS_RLD_MAP entry. This must come first because glibc
10058 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10059 may only look at the first one they see. */
10060 if (!bfd_link_pic (info)
10061 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10062 return FALSE;
10063
10064 if (bfd_link_executable (info)
10065 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10066 return FALSE;
10067
10068 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10069 used by the debugger. */
10070 if (bfd_link_executable (info)
10071 && !SGI_COMPAT (output_bfd)
10072 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10073 return FALSE;
10074
10075 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
10076 info->flags |= DF_TEXTREL;
10077
10078 if ((info->flags & DF_TEXTREL) != 0)
10079 {
10080 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10081 return FALSE;
10082
10083 /* Clear the DF_TEXTREL flag. It will be set again if we
10084 write out an actual text relocation; we may not, because
10085 at this point we do not know whether e.g. any .eh_frame
10086 absolute relocations have been converted to PC-relative. */
10087 info->flags &= ~DF_TEXTREL;
10088 }
10089
10090 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10091 return FALSE;
10092
10093 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
10094 if (htab->is_vxworks)
10095 {
10096 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10097 use any of the DT_MIPS_* tags. */
10098 if (sreldyn && sreldyn->size > 0)
10099 {
10100 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10101 return FALSE;
10102
10103 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10104 return FALSE;
10105
10106 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10107 return FALSE;
10108 }
10109 }
10110 else
10111 {
10112 if (sreldyn && sreldyn->size > 0
10113 && !bfd_is_abs_section (sreldyn->output_section))
10114 {
10115 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10116 return FALSE;
10117
10118 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10119 return FALSE;
10120
10121 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10122 return FALSE;
10123 }
10124
10125 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10126 return FALSE;
10127
10128 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10129 return FALSE;
10130
10131 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10132 return FALSE;
10133
10134 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10135 return FALSE;
10136
10137 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10138 return FALSE;
10139
10140 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10141 return FALSE;
10142
10143 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10144 return FALSE;
10145
10146 if (IRIX_COMPAT (dynobj) == ict_irix5
10147 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10148 return FALSE;
10149
10150 if (IRIX_COMPAT (dynobj) == ict_irix6
10151 && (bfd_get_section_by_name
10152 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10153 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10154 return FALSE;
10155 }
10156 if (htab->root.splt->size > 0)
10157 {
10158 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10159 return FALSE;
10160
10161 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10162 return FALSE;
10163
10164 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10165 return FALSE;
10166
10167 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10168 return FALSE;
10169 }
10170 if (htab->is_vxworks
10171 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10172 return FALSE;
10173 }
10174
10175 return TRUE;
10176 }
10177
10178 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10180 Adjust its R_ADDEND field so that it is correct for the output file.
10181 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10182 and sections respectively; both use symbol indexes. */
10183
10184 static void
10185 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10186 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10187 asection **local_sections, Elf_Internal_Rela *rel)
10188 {
10189 unsigned int r_type, r_symndx;
10190 Elf_Internal_Sym *sym;
10191 asection *sec;
10192
10193 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10194 {
10195 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10196 if (gprel16_reloc_p (r_type)
10197 || r_type == R_MIPS_GPREL32
10198 || literal_reloc_p (r_type))
10199 {
10200 rel->r_addend += _bfd_get_gp_value (input_bfd);
10201 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10202 }
10203
10204 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10205 sym = local_syms + r_symndx;
10206
10207 /* Adjust REL's addend to account for section merging. */
10208 if (!bfd_link_relocatable (info))
10209 {
10210 sec = local_sections[r_symndx];
10211 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10212 }
10213
10214 /* This would normally be done by the rela_normal code in elflink.c. */
10215 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10216 rel->r_addend += local_sections[r_symndx]->output_offset;
10217 }
10218 }
10219
10220 /* Handle relocations against symbols from removed linkonce sections,
10221 or sections discarded by a linker script. We use this wrapper around
10222 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10223 on 64-bit ELF targets. In this case for any relocation handled, which
10224 always be the first in a triplet, the remaining two have to be processed
10225 together with the first, even if they are R_MIPS_NONE. It is the symbol
10226 index referred by the first reloc that applies to all the three and the
10227 remaining two never refer to an object symbol. And it is the final
10228 relocation (the last non-null one) that determines the output field of
10229 the whole relocation so retrieve the corresponding howto structure for
10230 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10231
10232 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10233 and therefore requires to be pasted in a loop. It also defines a block
10234 and does not protect any of its arguments, hence the extra brackets. */
10235
10236 static void
10237 mips_reloc_against_discarded_section (bfd *output_bfd,
10238 struct bfd_link_info *info,
10239 bfd *input_bfd, asection *input_section,
10240 Elf_Internal_Rela **rel,
10241 const Elf_Internal_Rela **relend,
10242 bfd_boolean rel_reloc,
10243 reloc_howto_type *howto,
10244 bfd_byte *contents)
10245 {
10246 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10247 int count = bed->s->int_rels_per_ext_rel;
10248 unsigned int r_type;
10249 int i;
10250
10251 for (i = count - 1; i > 0; i--)
10252 {
10253 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10254 if (r_type != R_MIPS_NONE)
10255 {
10256 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10257 break;
10258 }
10259 }
10260 do
10261 {
10262 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10263 (*rel), count, (*relend),
10264 howto, i, contents);
10265 }
10266 while (0);
10267 }
10268
10269 /* Relocate a MIPS ELF section. */
10270
10271 bfd_boolean
10272 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10273 bfd *input_bfd, asection *input_section,
10274 bfd_byte *contents, Elf_Internal_Rela *relocs,
10275 Elf_Internal_Sym *local_syms,
10276 asection **local_sections)
10277 {
10278 Elf_Internal_Rela *rel;
10279 const Elf_Internal_Rela *relend;
10280 bfd_vma addend = 0;
10281 bfd_boolean use_saved_addend_p = FALSE;
10282
10283 relend = relocs + input_section->reloc_count;
10284 for (rel = relocs; rel < relend; ++rel)
10285 {
10286 const char *name;
10287 bfd_vma value = 0;
10288 reloc_howto_type *howto;
10289 bfd_boolean cross_mode_jump_p = FALSE;
10290 /* TRUE if the relocation is a RELA relocation, rather than a
10291 REL relocation. */
10292 bfd_boolean rela_relocation_p = TRUE;
10293 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10294 const char *msg;
10295 unsigned long r_symndx;
10296 asection *sec;
10297 Elf_Internal_Shdr *symtab_hdr;
10298 struct elf_link_hash_entry *h;
10299 bfd_boolean rel_reloc;
10300
10301 rel_reloc = (NEWABI_P (input_bfd)
10302 && mips_elf_rel_relocation_p (input_bfd, input_section,
10303 relocs, rel));
10304 /* Find the relocation howto for this relocation. */
10305 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10306
10307 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10308 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10309 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10310 {
10311 sec = local_sections[r_symndx];
10312 h = NULL;
10313 }
10314 else
10315 {
10316 unsigned long extsymoff;
10317
10318 extsymoff = 0;
10319 if (!elf_bad_symtab (input_bfd))
10320 extsymoff = symtab_hdr->sh_info;
10321 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10322 while (h->root.type == bfd_link_hash_indirect
10323 || h->root.type == bfd_link_hash_warning)
10324 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10325
10326 sec = NULL;
10327 if (h->root.type == bfd_link_hash_defined
10328 || h->root.type == bfd_link_hash_defweak)
10329 sec = h->root.u.def.section;
10330 }
10331
10332 if (sec != NULL && discarded_section (sec))
10333 {
10334 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10335 input_section, &rel, &relend,
10336 rel_reloc, howto, contents);
10337 continue;
10338 }
10339
10340 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10341 {
10342 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10343 64-bit code, but make sure all their addresses are in the
10344 lowermost or uppermost 32-bit section of the 64-bit address
10345 space. Thus, when they use an R_MIPS_64 they mean what is
10346 usually meant by R_MIPS_32, with the exception that the
10347 stored value is sign-extended to 64 bits. */
10348 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
10349
10350 /* On big-endian systems, we need to lie about the position
10351 of the reloc. */
10352 if (bfd_big_endian (input_bfd))
10353 rel->r_offset += 4;
10354 }
10355
10356 if (!use_saved_addend_p)
10357 {
10358 /* If these relocations were originally of the REL variety,
10359 we must pull the addend out of the field that will be
10360 relocated. Otherwise, we simply use the contents of the
10361 RELA relocation. */
10362 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10363 relocs, rel))
10364 {
10365 rela_relocation_p = FALSE;
10366 addend = mips_elf_read_rel_addend (input_bfd, rel,
10367 howto, contents);
10368 if (hi16_reloc_p (r_type)
10369 || (got16_reloc_p (r_type)
10370 && mips_elf_local_relocation_p (input_bfd, rel,
10371 local_sections)))
10372 {
10373 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
10374 contents, &addend))
10375 {
10376 if (h)
10377 name = h->root.root.string;
10378 else
10379 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10380 local_syms + r_symndx,
10381 sec);
10382 _bfd_error_handler
10383 /* xgettext:c-format */
10384 (_("%pB: can't find matching LO16 reloc against `%s'"
10385 " for %s at %#" PRIx64 " in section `%pA'"),
10386 input_bfd, name,
10387 howto->name, (uint64_t) rel->r_offset, input_section);
10388 }
10389 }
10390 else
10391 addend <<= howto->rightshift;
10392 }
10393 else
10394 addend = rel->r_addend;
10395 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10396 local_syms, local_sections, rel);
10397 }
10398
10399 if (bfd_link_relocatable (info))
10400 {
10401 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10402 && bfd_big_endian (input_bfd))
10403 rel->r_offset -= 4;
10404
10405 if (!rela_relocation_p && rel->r_addend)
10406 {
10407 addend += rel->r_addend;
10408 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10409 addend = mips_elf_high (addend);
10410 else if (r_type == R_MIPS_HIGHER)
10411 addend = mips_elf_higher (addend);
10412 else if (r_type == R_MIPS_HIGHEST)
10413 addend = mips_elf_highest (addend);
10414 else
10415 addend >>= howto->rightshift;
10416
10417 /* We use the source mask, rather than the destination
10418 mask because the place to which we are writing will be
10419 source of the addend in the final link. */
10420 addend &= howto->src_mask;
10421
10422 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10423 /* See the comment above about using R_MIPS_64 in the 32-bit
10424 ABI. Here, we need to update the addend. It would be
10425 possible to get away with just using the R_MIPS_32 reloc
10426 but for endianness. */
10427 {
10428 bfd_vma sign_bits;
10429 bfd_vma low_bits;
10430 bfd_vma high_bits;
10431
10432 if (addend & ((bfd_vma) 1 << 31))
10433 #ifdef BFD64
10434 sign_bits = ((bfd_vma) 1 << 32) - 1;
10435 #else
10436 sign_bits = -1;
10437 #endif
10438 else
10439 sign_bits = 0;
10440
10441 /* If we don't know that we have a 64-bit type,
10442 do two separate stores. */
10443 if (bfd_big_endian (input_bfd))
10444 {
10445 /* Store the sign-bits (which are most significant)
10446 first. */
10447 low_bits = sign_bits;
10448 high_bits = addend;
10449 }
10450 else
10451 {
10452 low_bits = addend;
10453 high_bits = sign_bits;
10454 }
10455 bfd_put_32 (input_bfd, low_bits,
10456 contents + rel->r_offset);
10457 bfd_put_32 (input_bfd, high_bits,
10458 contents + rel->r_offset + 4);
10459 continue;
10460 }
10461
10462 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10463 input_bfd, input_section,
10464 contents, FALSE))
10465 return FALSE;
10466 }
10467
10468 /* Go on to the next relocation. */
10469 continue;
10470 }
10471
10472 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10473 relocations for the same offset. In that case we are
10474 supposed to treat the output of each relocation as the addend
10475 for the next. */
10476 if (rel + 1 < relend
10477 && rel->r_offset == rel[1].r_offset
10478 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10479 use_saved_addend_p = TRUE;
10480 else
10481 use_saved_addend_p = FALSE;
10482
10483 /* Figure out what value we are supposed to relocate. */
10484 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10485 input_section, contents,
10486 info, rel, addend, howto,
10487 local_syms, local_sections,
10488 &value, &name, &cross_mode_jump_p,
10489 use_saved_addend_p))
10490 {
10491 case bfd_reloc_continue:
10492 /* There's nothing to do. */
10493 continue;
10494
10495 case bfd_reloc_undefined:
10496 /* mips_elf_calculate_relocation already called the
10497 undefined_symbol callback. There's no real point in
10498 trying to perform the relocation at this point, so we
10499 just skip ahead to the next relocation. */
10500 continue;
10501
10502 case bfd_reloc_notsupported:
10503 msg = _("internal error: unsupported relocation error");
10504 info->callbacks->warning
10505 (info, msg, name, input_bfd, input_section, rel->r_offset);
10506 return FALSE;
10507
10508 case bfd_reloc_overflow:
10509 if (use_saved_addend_p)
10510 /* Ignore overflow until we reach the last relocation for
10511 a given location. */
10512 ;
10513 else
10514 {
10515 struct mips_elf_link_hash_table *htab;
10516
10517 htab = mips_elf_hash_table (info);
10518 BFD_ASSERT (htab != NULL);
10519 BFD_ASSERT (name != NULL);
10520 if (!htab->small_data_overflow_reported
10521 && (gprel16_reloc_p (howto->type)
10522 || literal_reloc_p (howto->type)))
10523 {
10524 msg = _("small-data section exceeds 64KB;"
10525 " lower small-data size limit (see option -G)");
10526
10527 htab->small_data_overflow_reported = TRUE;
10528 (*info->callbacks->einfo) ("%P: %s\n", msg);
10529 }
10530 (*info->callbacks->reloc_overflow)
10531 (info, NULL, name, howto->name, (bfd_vma) 0,
10532 input_bfd, input_section, rel->r_offset);
10533 }
10534 break;
10535
10536 case bfd_reloc_ok:
10537 break;
10538
10539 case bfd_reloc_outofrange:
10540 msg = NULL;
10541 if (jal_reloc_p (howto->type))
10542 msg = (cross_mode_jump_p
10543 ? _("cannot convert a jump to JALX "
10544 "for a non-word-aligned address")
10545 : (howto->type == R_MIPS16_26
10546 ? _("jump to a non-word-aligned address")
10547 : _("jump to a non-instruction-aligned address")));
10548 else if (b_reloc_p (howto->type))
10549 msg = (cross_mode_jump_p
10550 ? _("cannot convert a branch to JALX "
10551 "for a non-word-aligned address")
10552 : _("branch to a non-instruction-aligned address"));
10553 else if (aligned_pcrel_reloc_p (howto->type))
10554 msg = _("PC-relative load from unaligned address");
10555 if (msg)
10556 {
10557 info->callbacks->einfo
10558 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10559 break;
10560 }
10561 /* Fall through. */
10562
10563 default:
10564 abort ();
10565 break;
10566 }
10567
10568 /* If we've got another relocation for the address, keep going
10569 until we reach the last one. */
10570 if (use_saved_addend_p)
10571 {
10572 addend = value;
10573 continue;
10574 }
10575
10576 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10577 /* See the comment above about using R_MIPS_64 in the 32-bit
10578 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10579 that calculated the right value. Now, however, we
10580 sign-extend the 32-bit result to 64-bits, and store it as a
10581 64-bit value. We are especially generous here in that we
10582 go to extreme lengths to support this usage on systems with
10583 only a 32-bit VMA. */
10584 {
10585 bfd_vma sign_bits;
10586 bfd_vma low_bits;
10587 bfd_vma high_bits;
10588
10589 if (value & ((bfd_vma) 1 << 31))
10590 #ifdef BFD64
10591 sign_bits = ((bfd_vma) 1 << 32) - 1;
10592 #else
10593 sign_bits = -1;
10594 #endif
10595 else
10596 sign_bits = 0;
10597
10598 /* If we don't know that we have a 64-bit type,
10599 do two separate stores. */
10600 if (bfd_big_endian (input_bfd))
10601 {
10602 /* Undo what we did above. */
10603 rel->r_offset -= 4;
10604 /* Store the sign-bits (which are most significant)
10605 first. */
10606 low_bits = sign_bits;
10607 high_bits = value;
10608 }
10609 else
10610 {
10611 low_bits = value;
10612 high_bits = sign_bits;
10613 }
10614 bfd_put_32 (input_bfd, low_bits,
10615 contents + rel->r_offset);
10616 bfd_put_32 (input_bfd, high_bits,
10617 contents + rel->r_offset + 4);
10618 continue;
10619 }
10620
10621 /* Actually perform the relocation. */
10622 if (! mips_elf_perform_relocation (info, howto, rel, value,
10623 input_bfd, input_section,
10624 contents, cross_mode_jump_p))
10625 return FALSE;
10626 }
10627
10628 return TRUE;
10629 }
10630
10631 /* A function that iterates over each entry in la25_stubs and fills
10633 in the code for each one. DATA points to a mips_htab_traverse_info. */
10634
10635 static int
10636 mips_elf_create_la25_stub (void **slot, void *data)
10637 {
10638 struct mips_htab_traverse_info *hti;
10639 struct mips_elf_link_hash_table *htab;
10640 struct mips_elf_la25_stub *stub;
10641 asection *s;
10642 bfd_byte *loc;
10643 bfd_vma offset, target, target_high, target_low;
10644
10645 stub = (struct mips_elf_la25_stub *) *slot;
10646 hti = (struct mips_htab_traverse_info *) data;
10647 htab = mips_elf_hash_table (hti->info);
10648 BFD_ASSERT (htab != NULL);
10649
10650 /* Create the section contents, if we haven't already. */
10651 s = stub->stub_section;
10652 loc = s->contents;
10653 if (loc == NULL)
10654 {
10655 loc = bfd_malloc (s->size);
10656 if (loc == NULL)
10657 {
10658 hti->error = TRUE;
10659 return FALSE;
10660 }
10661 s->contents = loc;
10662 }
10663
10664 /* Work out where in the section this stub should go. */
10665 offset = stub->offset;
10666
10667 /* Work out the target address. */
10668 target = mips_elf_get_la25_target (stub, &s);
10669 target += s->output_section->vma + s->output_offset;
10670
10671 target_high = ((target + 0x8000) >> 16) & 0xffff;
10672 target_low = (target & 0xffff);
10673
10674 if (stub->stub_section != htab->strampoline)
10675 {
10676 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10677 of the section and write the two instructions at the end. */
10678 memset (loc, 0, offset);
10679 loc += offset;
10680 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10681 {
10682 bfd_put_micromips_32 (hti->output_bfd,
10683 LA25_LUI_MICROMIPS (target_high),
10684 loc);
10685 bfd_put_micromips_32 (hti->output_bfd,
10686 LA25_ADDIU_MICROMIPS (target_low),
10687 loc + 4);
10688 }
10689 else
10690 {
10691 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10692 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10693 }
10694 }
10695 else
10696 {
10697 /* This is trampoline. */
10698 loc += offset;
10699 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10700 {
10701 bfd_put_micromips_32 (hti->output_bfd,
10702 LA25_LUI_MICROMIPS (target_high), loc);
10703 bfd_put_micromips_32 (hti->output_bfd,
10704 LA25_J_MICROMIPS (target), loc + 4);
10705 bfd_put_micromips_32 (hti->output_bfd,
10706 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10707 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10708 }
10709 else
10710 {
10711 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10712 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10713 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10714 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10715 }
10716 }
10717 return TRUE;
10718 }
10719
10720 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10721 adjust it appropriately now. */
10722
10723 static void
10724 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10725 const char *name, Elf_Internal_Sym *sym)
10726 {
10727 /* The linker script takes care of providing names and values for
10728 these, but we must place them into the right sections. */
10729 static const char* const text_section_symbols[] = {
10730 "_ftext",
10731 "_etext",
10732 "__dso_displacement",
10733 "__elf_header",
10734 "__program_header_table",
10735 NULL
10736 };
10737
10738 static const char* const data_section_symbols[] = {
10739 "_fdata",
10740 "_edata",
10741 "_end",
10742 "_fbss",
10743 NULL
10744 };
10745
10746 const char* const *p;
10747 int i;
10748
10749 for (i = 0; i < 2; ++i)
10750 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10751 *p;
10752 ++p)
10753 if (strcmp (*p, name) == 0)
10754 {
10755 /* All of these symbols are given type STT_SECTION by the
10756 IRIX6 linker. */
10757 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10758 sym->st_other = STO_PROTECTED;
10759
10760 /* The IRIX linker puts these symbols in special sections. */
10761 if (i == 0)
10762 sym->st_shndx = SHN_MIPS_TEXT;
10763 else
10764 sym->st_shndx = SHN_MIPS_DATA;
10765
10766 break;
10767 }
10768 }
10769
10770 /* Finish up dynamic symbol handling. We set the contents of various
10771 dynamic sections here. */
10772
10773 bfd_boolean
10774 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10775 struct bfd_link_info *info,
10776 struct elf_link_hash_entry *h,
10777 Elf_Internal_Sym *sym)
10778 {
10779 bfd *dynobj;
10780 asection *sgot;
10781 struct mips_got_info *g, *gg;
10782 const char *name;
10783 int idx;
10784 struct mips_elf_link_hash_table *htab;
10785 struct mips_elf_link_hash_entry *hmips;
10786
10787 htab = mips_elf_hash_table (info);
10788 BFD_ASSERT (htab != NULL);
10789 dynobj = elf_hash_table (info)->dynobj;
10790 hmips = (struct mips_elf_link_hash_entry *) h;
10791
10792 BFD_ASSERT (!htab->is_vxworks);
10793
10794 if (h->plt.plist != NULL
10795 && (h->plt.plist->mips_offset != MINUS_ONE
10796 || h->plt.plist->comp_offset != MINUS_ONE))
10797 {
10798 /* We've decided to create a PLT entry for this symbol. */
10799 bfd_byte *loc;
10800 bfd_vma header_address, got_address;
10801 bfd_vma got_address_high, got_address_low, load;
10802 bfd_vma got_index;
10803 bfd_vma isa_bit;
10804
10805 got_index = h->plt.plist->gotplt_index;
10806
10807 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10808 BFD_ASSERT (h->dynindx != -1);
10809 BFD_ASSERT (htab->root.splt != NULL);
10810 BFD_ASSERT (got_index != MINUS_ONE);
10811 BFD_ASSERT (!h->def_regular);
10812
10813 /* Calculate the address of the PLT header. */
10814 isa_bit = htab->plt_header_is_comp;
10815 header_address = (htab->root.splt->output_section->vma
10816 + htab->root.splt->output_offset + isa_bit);
10817
10818 /* Calculate the address of the .got.plt entry. */
10819 got_address = (htab->root.sgotplt->output_section->vma
10820 + htab->root.sgotplt->output_offset
10821 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10822
10823 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10824 got_address_low = got_address & 0xffff;
10825
10826 /* The PLT sequence is not safe for N64 if .got.plt entry's address
10827 cannot be loaded in two instructions. */
10828 if (ABI_64_P (output_bfd)
10829 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
10830 {
10831 _bfd_error_handler
10832 /* xgettext:c-format */
10833 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
10834 "supported; consider using `-Ttext-segment=...'"),
10835 output_bfd,
10836 htab->root.sgotplt->output_section,
10837 (int64_t) got_address);
10838 bfd_set_error (bfd_error_no_error);
10839 return FALSE;
10840 }
10841
10842 /* Initially point the .got.plt entry at the PLT header. */
10843 loc = (htab->root.sgotplt->contents
10844 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10845 if (ABI_64_P (output_bfd))
10846 bfd_put_64 (output_bfd, header_address, loc);
10847 else
10848 bfd_put_32 (output_bfd, header_address, loc);
10849
10850 /* Now handle the PLT itself. First the standard entry (the order
10851 does not matter, we just have to pick one). */
10852 if (h->plt.plist->mips_offset != MINUS_ONE)
10853 {
10854 const bfd_vma *plt_entry;
10855 bfd_vma plt_offset;
10856
10857 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10858
10859 BFD_ASSERT (plt_offset <= htab->root.splt->size);
10860
10861 /* Find out where the .plt entry should go. */
10862 loc = htab->root.splt->contents + plt_offset;
10863
10864 /* Pick the load opcode. */
10865 load = MIPS_ELF_LOAD_WORD (output_bfd);
10866
10867 /* Fill in the PLT entry itself. */
10868
10869 if (MIPSR6_P (output_bfd))
10870 plt_entry = mipsr6_exec_plt_entry;
10871 else
10872 plt_entry = mips_exec_plt_entry;
10873 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10874 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10875 loc + 4);
10876
10877 if (! LOAD_INTERLOCKS_P (output_bfd))
10878 {
10879 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10880 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10881 }
10882 else
10883 {
10884 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10885 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10886 loc + 12);
10887 }
10888 }
10889
10890 /* Now the compressed entry. They come after any standard ones. */
10891 if (h->plt.plist->comp_offset != MINUS_ONE)
10892 {
10893 bfd_vma plt_offset;
10894
10895 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10896 + h->plt.plist->comp_offset);
10897
10898 BFD_ASSERT (plt_offset <= htab->root.splt->size);
10899
10900 /* Find out where the .plt entry should go. */
10901 loc = htab->root.splt->contents + plt_offset;
10902
10903 /* Fill in the PLT entry itself. */
10904 if (!MICROMIPS_P (output_bfd))
10905 {
10906 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10907
10908 bfd_put_16 (output_bfd, plt_entry[0], loc);
10909 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10910 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10911 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10912 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10913 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10914 bfd_put_32 (output_bfd, got_address, loc + 12);
10915 }
10916 else if (htab->insn32)
10917 {
10918 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10919
10920 bfd_put_16 (output_bfd, plt_entry[0], loc);
10921 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10922 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10923 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10924 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10925 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10926 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10927 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10928 }
10929 else
10930 {
10931 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10932 bfd_signed_vma gotpc_offset;
10933 bfd_vma loc_address;
10934
10935 BFD_ASSERT (got_address % 4 == 0);
10936
10937 loc_address = (htab->root.splt->output_section->vma
10938 + htab->root.splt->output_offset + plt_offset);
10939 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10940
10941 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10942 if (gotpc_offset + 0x1000000 >= 0x2000000)
10943 {
10944 _bfd_error_handler
10945 /* xgettext:c-format */
10946 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
10947 "beyond the range of ADDIUPC"),
10948 output_bfd,
10949 htab->root.sgotplt->output_section,
10950 (int64_t) gotpc_offset,
10951 htab->root.splt->output_section);
10952 bfd_set_error (bfd_error_no_error);
10953 return FALSE;
10954 }
10955 bfd_put_16 (output_bfd,
10956 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10957 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10958 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10959 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10960 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10961 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10962 }
10963 }
10964
10965 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10966 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
10967 got_index - 2, h->dynindx,
10968 R_MIPS_JUMP_SLOT, got_address);
10969
10970 /* We distinguish between PLT entries and lazy-binding stubs by
10971 giving the former an st_other value of STO_MIPS_PLT. Set the
10972 flag and leave the value if there are any relocations in the
10973 binary where pointer equality matters. */
10974 sym->st_shndx = SHN_UNDEF;
10975 if (h->pointer_equality_needed)
10976 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
10977 else
10978 {
10979 sym->st_value = 0;
10980 sym->st_other = 0;
10981 }
10982 }
10983
10984 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
10985 {
10986 /* We've decided to create a lazy-binding stub. */
10987 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10988 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10989 bfd_vma stub_size = htab->function_stub_size;
10990 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
10991 bfd_vma isa_bit = micromips_p;
10992 bfd_vma stub_big_size;
10993
10994 if (!micromips_p)
10995 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
10996 else if (htab->insn32)
10997 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10998 else
10999 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11000
11001 /* This symbol has a stub. Set it up. */
11002
11003 BFD_ASSERT (h->dynindx != -1);
11004
11005 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11006
11007 /* Values up to 2^31 - 1 are allowed. Larger values would cause
11008 sign extension at runtime in the stub, resulting in a negative
11009 index value. */
11010 if (h->dynindx & ~0x7fffffff)
11011 return FALSE;
11012
11013 /* Fill the stub. */
11014 if (micromips_p)
11015 {
11016 idx = 0;
11017 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11018 stub + idx);
11019 idx += 4;
11020 if (htab->insn32)
11021 {
11022 bfd_put_micromips_32 (output_bfd,
11023 STUB_MOVE32_MICROMIPS, stub + idx);
11024 idx += 4;
11025 }
11026 else
11027 {
11028 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11029 idx += 2;
11030 }
11031 if (stub_size == stub_big_size)
11032 {
11033 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11034
11035 bfd_put_micromips_32 (output_bfd,
11036 STUB_LUI_MICROMIPS (dynindx_hi),
11037 stub + idx);
11038 idx += 4;
11039 }
11040 if (htab->insn32)
11041 {
11042 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11043 stub + idx);
11044 idx += 4;
11045 }
11046 else
11047 {
11048 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11049 idx += 2;
11050 }
11051
11052 /* If a large stub is not required and sign extension is not a
11053 problem, then use legacy code in the stub. */
11054 if (stub_size == stub_big_size)
11055 bfd_put_micromips_32 (output_bfd,
11056 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11057 stub + idx);
11058 else if (h->dynindx & ~0x7fff)
11059 bfd_put_micromips_32 (output_bfd,
11060 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11061 stub + idx);
11062 else
11063 bfd_put_micromips_32 (output_bfd,
11064 STUB_LI16S_MICROMIPS (output_bfd,
11065 h->dynindx),
11066 stub + idx);
11067 }
11068 else
11069 {
11070 idx = 0;
11071 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11072 idx += 4;
11073 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11074 idx += 4;
11075 if (stub_size == stub_big_size)
11076 {
11077 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11078 stub + idx);
11079 idx += 4;
11080 }
11081 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11082 idx += 4;
11083
11084 /* If a large stub is not required and sign extension is not a
11085 problem, then use legacy code in the stub. */
11086 if (stub_size == stub_big_size)
11087 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11088 stub + idx);
11089 else if (h->dynindx & ~0x7fff)
11090 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11091 stub + idx);
11092 else
11093 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11094 stub + idx);
11095 }
11096
11097 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11098 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11099 stub, stub_size);
11100
11101 /* Mark the symbol as undefined. stub_offset != -1 occurs
11102 only for the referenced symbol. */
11103 sym->st_shndx = SHN_UNDEF;
11104
11105 /* The run-time linker uses the st_value field of the symbol
11106 to reset the global offset table entry for this external
11107 to its stub address when unlinking a shared object. */
11108 sym->st_value = (htab->sstubs->output_section->vma
11109 + htab->sstubs->output_offset
11110 + h->plt.plist->stub_offset
11111 + isa_bit);
11112 sym->st_other = other;
11113 }
11114
11115 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11116 refer to the stub, since only the stub uses the standard calling
11117 conventions. */
11118 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11119 {
11120 BFD_ASSERT (hmips->need_fn_stub);
11121 sym->st_value = (hmips->fn_stub->output_section->vma
11122 + hmips->fn_stub->output_offset);
11123 sym->st_size = hmips->fn_stub->size;
11124 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11125 }
11126
11127 BFD_ASSERT (h->dynindx != -1
11128 || h->forced_local);
11129
11130 sgot = htab->root.sgot;
11131 g = htab->got_info;
11132 BFD_ASSERT (g != NULL);
11133
11134 /* Run through the global symbol table, creating GOT entries for all
11135 the symbols that need them. */
11136 if (hmips->global_got_area != GGA_NONE)
11137 {
11138 bfd_vma offset;
11139 bfd_vma value;
11140
11141 value = sym->st_value;
11142 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11143 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11144 }
11145
11146 if (hmips->global_got_area != GGA_NONE && g->next)
11147 {
11148 struct mips_got_entry e, *p;
11149 bfd_vma entry;
11150 bfd_vma offset;
11151
11152 gg = g;
11153
11154 e.abfd = output_bfd;
11155 e.symndx = -1;
11156 e.d.h = hmips;
11157 e.tls_type = GOT_TLS_NONE;
11158
11159 for (g = g->next; g->next != gg; g = g->next)
11160 {
11161 if (g->got_entries
11162 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11163 &e)))
11164 {
11165 offset = p->gotidx;
11166 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11167 if (bfd_link_pic (info)
11168 || (elf_hash_table (info)->dynamic_sections_created
11169 && p->d.h != NULL
11170 && p->d.h->root.def_dynamic
11171 && !p->d.h->root.def_regular))
11172 {
11173 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11174 the various compatibility problems, it's easier to mock
11175 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11176 mips_elf_create_dynamic_relocation to calculate the
11177 appropriate addend. */
11178 Elf_Internal_Rela rel[3];
11179
11180 memset (rel, 0, sizeof (rel));
11181 if (ABI_64_P (output_bfd))
11182 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11183 else
11184 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11185 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11186
11187 entry = 0;
11188 if (! (mips_elf_create_dynamic_relocation
11189 (output_bfd, info, rel,
11190 e.d.h, NULL, sym->st_value, &entry, sgot)))
11191 return FALSE;
11192 }
11193 else
11194 entry = sym->st_value;
11195 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11196 }
11197 }
11198 }
11199
11200 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11201 name = h->root.root.string;
11202 if (h == elf_hash_table (info)->hdynamic
11203 || h == elf_hash_table (info)->hgot)
11204 sym->st_shndx = SHN_ABS;
11205 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11206 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11207 {
11208 sym->st_shndx = SHN_ABS;
11209 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11210 sym->st_value = 1;
11211 }
11212 else if (SGI_COMPAT (output_bfd))
11213 {
11214 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11215 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11216 {
11217 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11218 sym->st_other = STO_PROTECTED;
11219 sym->st_value = 0;
11220 sym->st_shndx = SHN_MIPS_DATA;
11221 }
11222 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11223 {
11224 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11225 sym->st_other = STO_PROTECTED;
11226 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11227 sym->st_shndx = SHN_ABS;
11228 }
11229 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11230 {
11231 if (h->type == STT_FUNC)
11232 sym->st_shndx = SHN_MIPS_TEXT;
11233 else if (h->type == STT_OBJECT)
11234 sym->st_shndx = SHN_MIPS_DATA;
11235 }
11236 }
11237
11238 /* Emit a copy reloc, if needed. */
11239 if (h->needs_copy)
11240 {
11241 asection *s;
11242 bfd_vma symval;
11243
11244 BFD_ASSERT (h->dynindx != -1);
11245 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11246
11247 s = mips_elf_rel_dyn_section (info, FALSE);
11248 symval = (h->root.u.def.section->output_section->vma
11249 + h->root.u.def.section->output_offset
11250 + h->root.u.def.value);
11251 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11252 h->dynindx, R_MIPS_COPY, symval);
11253 }
11254
11255 /* Handle the IRIX6-specific symbols. */
11256 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11257 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11258
11259 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11260 to treat compressed symbols like any other. */
11261 if (ELF_ST_IS_MIPS16 (sym->st_other))
11262 {
11263 BFD_ASSERT (sym->st_value & 1);
11264 sym->st_other -= STO_MIPS16;
11265 }
11266 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11267 {
11268 BFD_ASSERT (sym->st_value & 1);
11269 sym->st_other -= STO_MICROMIPS;
11270 }
11271
11272 return TRUE;
11273 }
11274
11275 /* Likewise, for VxWorks. */
11276
11277 bfd_boolean
11278 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11279 struct bfd_link_info *info,
11280 struct elf_link_hash_entry *h,
11281 Elf_Internal_Sym *sym)
11282 {
11283 bfd *dynobj;
11284 asection *sgot;
11285 struct mips_got_info *g;
11286 struct mips_elf_link_hash_table *htab;
11287 struct mips_elf_link_hash_entry *hmips;
11288
11289 htab = mips_elf_hash_table (info);
11290 BFD_ASSERT (htab != NULL);
11291 dynobj = elf_hash_table (info)->dynobj;
11292 hmips = (struct mips_elf_link_hash_entry *) h;
11293
11294 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11295 {
11296 bfd_byte *loc;
11297 bfd_vma plt_address, got_address, got_offset, branch_offset;
11298 Elf_Internal_Rela rel;
11299 static const bfd_vma *plt_entry;
11300 bfd_vma gotplt_index;
11301 bfd_vma plt_offset;
11302
11303 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11304 gotplt_index = h->plt.plist->gotplt_index;
11305
11306 BFD_ASSERT (h->dynindx != -1);
11307 BFD_ASSERT (htab->root.splt != NULL);
11308 BFD_ASSERT (gotplt_index != MINUS_ONE);
11309 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11310
11311 /* Calculate the address of the .plt entry. */
11312 plt_address = (htab->root.splt->output_section->vma
11313 + htab->root.splt->output_offset
11314 + plt_offset);
11315
11316 /* Calculate the address of the .got.plt entry. */
11317 got_address = (htab->root.sgotplt->output_section->vma
11318 + htab->root.sgotplt->output_offset
11319 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11320
11321 /* Calculate the offset of the .got.plt entry from
11322 _GLOBAL_OFFSET_TABLE_. */
11323 got_offset = mips_elf_gotplt_index (info, h);
11324
11325 /* Calculate the offset for the branch at the start of the PLT
11326 entry. The branch jumps to the beginning of .plt. */
11327 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11328
11329 /* Fill in the initial value of the .got.plt entry. */
11330 bfd_put_32 (output_bfd, plt_address,
11331 (htab->root.sgotplt->contents
11332 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11333
11334 /* Find out where the .plt entry should go. */
11335 loc = htab->root.splt->contents + plt_offset;
11336
11337 if (bfd_link_pic (info))
11338 {
11339 plt_entry = mips_vxworks_shared_plt_entry;
11340 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11341 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11342 }
11343 else
11344 {
11345 bfd_vma got_address_high, got_address_low;
11346
11347 plt_entry = mips_vxworks_exec_plt_entry;
11348 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11349 got_address_low = got_address & 0xffff;
11350
11351 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11352 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11353 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11354 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11355 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11356 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11357 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11358 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11359
11360 loc = (htab->srelplt2->contents
11361 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11362
11363 /* Emit a relocation for the .got.plt entry. */
11364 rel.r_offset = got_address;
11365 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11366 rel.r_addend = plt_offset;
11367 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11368
11369 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11370 loc += sizeof (Elf32_External_Rela);
11371 rel.r_offset = plt_address + 8;
11372 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11373 rel.r_addend = got_offset;
11374 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11375
11376 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11377 loc += sizeof (Elf32_External_Rela);
11378 rel.r_offset += 4;
11379 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11380 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11381 }
11382
11383 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11384 loc = (htab->root.srelplt->contents
11385 + gotplt_index * sizeof (Elf32_External_Rela));
11386 rel.r_offset = got_address;
11387 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11388 rel.r_addend = 0;
11389 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11390
11391 if (!h->def_regular)
11392 sym->st_shndx = SHN_UNDEF;
11393 }
11394
11395 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11396
11397 sgot = htab->root.sgot;
11398 g = htab->got_info;
11399 BFD_ASSERT (g != NULL);
11400
11401 /* See if this symbol has an entry in the GOT. */
11402 if (hmips->global_got_area != GGA_NONE)
11403 {
11404 bfd_vma offset;
11405 Elf_Internal_Rela outrel;
11406 bfd_byte *loc;
11407 asection *s;
11408
11409 /* Install the symbol value in the GOT. */
11410 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11411 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11412
11413 /* Add a dynamic relocation for it. */
11414 s = mips_elf_rel_dyn_section (info, FALSE);
11415 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11416 outrel.r_offset = (sgot->output_section->vma
11417 + sgot->output_offset
11418 + offset);
11419 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11420 outrel.r_addend = 0;
11421 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11422 }
11423
11424 /* Emit a copy reloc, if needed. */
11425 if (h->needs_copy)
11426 {
11427 Elf_Internal_Rela rel;
11428 asection *srel;
11429 bfd_byte *loc;
11430
11431 BFD_ASSERT (h->dynindx != -1);
11432
11433 rel.r_offset = (h->root.u.def.section->output_section->vma
11434 + h->root.u.def.section->output_offset
11435 + h->root.u.def.value);
11436 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11437 rel.r_addend = 0;
11438 if (h->root.u.def.section == htab->root.sdynrelro)
11439 srel = htab->root.sreldynrelro;
11440 else
11441 srel = htab->root.srelbss;
11442 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11443 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11444 ++srel->reloc_count;
11445 }
11446
11447 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11448 if (ELF_ST_IS_COMPRESSED (sym->st_other))
11449 sym->st_value &= ~1;
11450
11451 return TRUE;
11452 }
11453
11454 /* Write out a plt0 entry to the beginning of .plt. */
11455
11456 static bfd_boolean
11457 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11458 {
11459 bfd_byte *loc;
11460 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11461 static const bfd_vma *plt_entry;
11462 struct mips_elf_link_hash_table *htab;
11463
11464 htab = mips_elf_hash_table (info);
11465 BFD_ASSERT (htab != NULL);
11466
11467 if (ABI_64_P (output_bfd))
11468 plt_entry = mips_n64_exec_plt0_entry;
11469 else if (ABI_N32_P (output_bfd))
11470 plt_entry = mips_n32_exec_plt0_entry;
11471 else if (!htab->plt_header_is_comp)
11472 plt_entry = mips_o32_exec_plt0_entry;
11473 else if (htab->insn32)
11474 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11475 else
11476 plt_entry = micromips_o32_exec_plt0_entry;
11477
11478 /* Calculate the value of .got.plt. */
11479 gotplt_value = (htab->root.sgotplt->output_section->vma
11480 + htab->root.sgotplt->output_offset);
11481 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11482 gotplt_value_low = gotplt_value & 0xffff;
11483
11484 /* The PLT sequence is not safe for N64 if .got.plt's address can
11485 not be loaded in two instructions. */
11486 if (ABI_64_P (output_bfd)
11487 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11488 {
11489 _bfd_error_handler
11490 /* xgettext:c-format */
11491 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11492 "supported; consider using `-Ttext-segment=...'"),
11493 output_bfd,
11494 htab->root.sgotplt->output_section,
11495 (int64_t) gotplt_value);
11496 bfd_set_error (bfd_error_no_error);
11497 return FALSE;
11498 }
11499
11500 /* Install the PLT header. */
11501 loc = htab->root.splt->contents;
11502 if (plt_entry == micromips_o32_exec_plt0_entry)
11503 {
11504 bfd_vma gotpc_offset;
11505 bfd_vma loc_address;
11506 size_t i;
11507
11508 BFD_ASSERT (gotplt_value % 4 == 0);
11509
11510 loc_address = (htab->root.splt->output_section->vma
11511 + htab->root.splt->output_offset);
11512 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11513
11514 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11515 if (gotpc_offset + 0x1000000 >= 0x2000000)
11516 {
11517 _bfd_error_handler
11518 /* xgettext:c-format */
11519 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11520 "beyond the range of ADDIUPC"),
11521 output_bfd,
11522 htab->root.sgotplt->output_section,
11523 (int64_t) gotpc_offset,
11524 htab->root.splt->output_section);
11525 bfd_set_error (bfd_error_no_error);
11526 return FALSE;
11527 }
11528 bfd_put_16 (output_bfd,
11529 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11530 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11531 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11532 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11533 }
11534 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11535 {
11536 size_t i;
11537
11538 bfd_put_16 (output_bfd, plt_entry[0], loc);
11539 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11540 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11541 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11542 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11543 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11544 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11545 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11546 }
11547 else
11548 {
11549 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11550 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11551 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11552 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11553 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11554 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11555 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11556 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11557 }
11558
11559 return TRUE;
11560 }
11561
11562 /* Install the PLT header for a VxWorks executable and finalize the
11563 contents of .rela.plt.unloaded. */
11564
11565 static void
11566 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11567 {
11568 Elf_Internal_Rela rela;
11569 bfd_byte *loc;
11570 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11571 static const bfd_vma *plt_entry;
11572 struct mips_elf_link_hash_table *htab;
11573
11574 htab = mips_elf_hash_table (info);
11575 BFD_ASSERT (htab != NULL);
11576
11577 plt_entry = mips_vxworks_exec_plt0_entry;
11578
11579 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11580 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11581 + htab->root.hgot->root.u.def.section->output_offset
11582 + htab->root.hgot->root.u.def.value);
11583
11584 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11585 got_value_low = got_value & 0xffff;
11586
11587 /* Calculate the address of the PLT header. */
11588 plt_address = (htab->root.splt->output_section->vma
11589 + htab->root.splt->output_offset);
11590
11591 /* Install the PLT header. */
11592 loc = htab->root.splt->contents;
11593 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11594 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11595 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11596 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11597 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11598 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11599
11600 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11601 loc = htab->srelplt2->contents;
11602 rela.r_offset = plt_address;
11603 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11604 rela.r_addend = 0;
11605 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11606 loc += sizeof (Elf32_External_Rela);
11607
11608 /* Output the relocation for the following addiu of
11609 %lo(_GLOBAL_OFFSET_TABLE_). */
11610 rela.r_offset += 4;
11611 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11612 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11613 loc += sizeof (Elf32_External_Rela);
11614
11615 /* Fix up the remaining relocations. They may have the wrong
11616 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11617 in which symbols were output. */
11618 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11619 {
11620 Elf_Internal_Rela rel;
11621
11622 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11623 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11624 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11625 loc += sizeof (Elf32_External_Rela);
11626
11627 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11628 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11629 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11630 loc += sizeof (Elf32_External_Rela);
11631
11632 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11633 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11634 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11635 loc += sizeof (Elf32_External_Rela);
11636 }
11637 }
11638
11639 /* Install the PLT header for a VxWorks shared library. */
11640
11641 static void
11642 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11643 {
11644 unsigned int i;
11645 struct mips_elf_link_hash_table *htab;
11646
11647 htab = mips_elf_hash_table (info);
11648 BFD_ASSERT (htab != NULL);
11649
11650 /* We just need to copy the entry byte-by-byte. */
11651 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11652 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11653 htab->root.splt->contents + i * 4);
11654 }
11655
11656 /* Finish up the dynamic sections. */
11657
11658 bfd_boolean
11659 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11660 struct bfd_link_info *info)
11661 {
11662 bfd *dynobj;
11663 asection *sdyn;
11664 asection *sgot;
11665 struct mips_got_info *gg, *g;
11666 struct mips_elf_link_hash_table *htab;
11667
11668 htab = mips_elf_hash_table (info);
11669 BFD_ASSERT (htab != NULL);
11670
11671 dynobj = elf_hash_table (info)->dynobj;
11672
11673 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11674
11675 sgot = htab->root.sgot;
11676 gg = htab->got_info;
11677
11678 if (elf_hash_table (info)->dynamic_sections_created)
11679 {
11680 bfd_byte *b;
11681 int dyn_to_skip = 0, dyn_skipped = 0;
11682
11683 BFD_ASSERT (sdyn != NULL);
11684 BFD_ASSERT (gg != NULL);
11685
11686 g = mips_elf_bfd_got (output_bfd, FALSE);
11687 BFD_ASSERT (g != NULL);
11688
11689 for (b = sdyn->contents;
11690 b < sdyn->contents + sdyn->size;
11691 b += MIPS_ELF_DYN_SIZE (dynobj))
11692 {
11693 Elf_Internal_Dyn dyn;
11694 const char *name;
11695 size_t elemsize;
11696 asection *s;
11697 bfd_boolean swap_out_p;
11698
11699 /* Read in the current dynamic entry. */
11700 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11701
11702 /* Assume that we're going to modify it and write it out. */
11703 swap_out_p = TRUE;
11704
11705 switch (dyn.d_tag)
11706 {
11707 case DT_RELENT:
11708 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11709 break;
11710
11711 case DT_RELAENT:
11712 BFD_ASSERT (htab->is_vxworks);
11713 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11714 break;
11715
11716 case DT_STRSZ:
11717 /* Rewrite DT_STRSZ. */
11718 dyn.d_un.d_val =
11719 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11720 break;
11721
11722 case DT_PLTGOT:
11723 s = htab->root.sgot;
11724 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11725 break;
11726
11727 case DT_MIPS_PLTGOT:
11728 s = htab->root.sgotplt;
11729 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11730 break;
11731
11732 case DT_MIPS_RLD_VERSION:
11733 dyn.d_un.d_val = 1; /* XXX */
11734 break;
11735
11736 case DT_MIPS_FLAGS:
11737 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11738 break;
11739
11740 case DT_MIPS_TIME_STAMP:
11741 {
11742 time_t t;
11743 time (&t);
11744 dyn.d_un.d_val = t;
11745 }
11746 break;
11747
11748 case DT_MIPS_ICHECKSUM:
11749 /* XXX FIXME: */
11750 swap_out_p = FALSE;
11751 break;
11752
11753 case DT_MIPS_IVERSION:
11754 /* XXX FIXME: */
11755 swap_out_p = FALSE;
11756 break;
11757
11758 case DT_MIPS_BASE_ADDRESS:
11759 s = output_bfd->sections;
11760 BFD_ASSERT (s != NULL);
11761 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11762 break;
11763
11764 case DT_MIPS_LOCAL_GOTNO:
11765 dyn.d_un.d_val = g->local_gotno;
11766 break;
11767
11768 case DT_MIPS_UNREFEXTNO:
11769 /* The index into the dynamic symbol table which is the
11770 entry of the first external symbol that is not
11771 referenced within the same object. */
11772 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11773 break;
11774
11775 case DT_MIPS_GOTSYM:
11776 if (htab->global_gotsym)
11777 {
11778 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11779 break;
11780 }
11781 /* In case if we don't have global got symbols we default
11782 to setting DT_MIPS_GOTSYM to the same value as
11783 DT_MIPS_SYMTABNO. */
11784 /* Fall through. */
11785
11786 case DT_MIPS_SYMTABNO:
11787 name = ".dynsym";
11788 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11789 s = bfd_get_linker_section (dynobj, name);
11790
11791 if (s != NULL)
11792 dyn.d_un.d_val = s->size / elemsize;
11793 else
11794 dyn.d_un.d_val = 0;
11795 break;
11796
11797 case DT_MIPS_HIPAGENO:
11798 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11799 break;
11800
11801 case DT_MIPS_RLD_MAP:
11802 {
11803 struct elf_link_hash_entry *h;
11804 h = mips_elf_hash_table (info)->rld_symbol;
11805 if (!h)
11806 {
11807 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11808 swap_out_p = FALSE;
11809 break;
11810 }
11811 s = h->root.u.def.section;
11812
11813 /* The MIPS_RLD_MAP tag stores the absolute address of the
11814 debug pointer. */
11815 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11816 + h->root.u.def.value);
11817 }
11818 break;
11819
11820 case DT_MIPS_RLD_MAP_REL:
11821 {
11822 struct elf_link_hash_entry *h;
11823 bfd_vma dt_addr, rld_addr;
11824 h = mips_elf_hash_table (info)->rld_symbol;
11825 if (!h)
11826 {
11827 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11828 swap_out_p = FALSE;
11829 break;
11830 }
11831 s = h->root.u.def.section;
11832
11833 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
11834 pointer, relative to the address of the tag. */
11835 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
11836 + (b - sdyn->contents));
11837 rld_addr = (s->output_section->vma + s->output_offset
11838 + h->root.u.def.value);
11839 dyn.d_un.d_ptr = rld_addr - dt_addr;
11840 }
11841 break;
11842
11843 case DT_MIPS_OPTIONS:
11844 s = (bfd_get_section_by_name
11845 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11846 dyn.d_un.d_ptr = s->vma;
11847 break;
11848
11849 case DT_PLTREL:
11850 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11851 if (htab->is_vxworks)
11852 dyn.d_un.d_val = DT_RELA;
11853 else
11854 dyn.d_un.d_val = DT_REL;
11855 break;
11856
11857 case DT_PLTRELSZ:
11858 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11859 dyn.d_un.d_val = htab->root.srelplt->size;
11860 break;
11861
11862 case DT_JMPREL:
11863 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11864 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
11865 + htab->root.srelplt->output_offset);
11866 break;
11867
11868 case DT_TEXTREL:
11869 /* If we didn't need any text relocations after all, delete
11870 the dynamic tag. */
11871 if (!(info->flags & DF_TEXTREL))
11872 {
11873 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11874 swap_out_p = FALSE;
11875 }
11876 break;
11877
11878 case DT_FLAGS:
11879 /* If we didn't need any text relocations after all, clear
11880 DF_TEXTREL from DT_FLAGS. */
11881 if (!(info->flags & DF_TEXTREL))
11882 dyn.d_un.d_val &= ~DF_TEXTREL;
11883 else
11884 swap_out_p = FALSE;
11885 break;
11886
11887 default:
11888 swap_out_p = FALSE;
11889 if (htab->is_vxworks
11890 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11891 swap_out_p = TRUE;
11892 break;
11893 }
11894
11895 if (swap_out_p || dyn_skipped)
11896 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11897 (dynobj, &dyn, b - dyn_skipped);
11898
11899 if (dyn_to_skip)
11900 {
11901 dyn_skipped += dyn_to_skip;
11902 dyn_to_skip = 0;
11903 }
11904 }
11905
11906 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11907 if (dyn_skipped > 0)
11908 memset (b - dyn_skipped, 0, dyn_skipped);
11909 }
11910
11911 if (sgot != NULL && sgot->size > 0
11912 && !bfd_is_abs_section (sgot->output_section))
11913 {
11914 if (htab->is_vxworks)
11915 {
11916 /* The first entry of the global offset table points to the
11917 ".dynamic" section. The second is initialized by the
11918 loader and contains the shared library identifier.
11919 The third is also initialized by the loader and points
11920 to the lazy resolution stub. */
11921 MIPS_ELF_PUT_WORD (output_bfd,
11922 sdyn->output_offset + sdyn->output_section->vma,
11923 sgot->contents);
11924 MIPS_ELF_PUT_WORD (output_bfd, 0,
11925 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11926 MIPS_ELF_PUT_WORD (output_bfd, 0,
11927 sgot->contents
11928 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11929 }
11930 else
11931 {
11932 /* The first entry of the global offset table will be filled at
11933 runtime. The second entry will be used by some runtime loaders.
11934 This isn't the case of IRIX rld. */
11935 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
11936 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11937 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11938 }
11939
11940 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11941 = MIPS_ELF_GOT_SIZE (output_bfd);
11942 }
11943
11944 /* Generate dynamic relocations for the non-primary gots. */
11945 if (gg != NULL && gg->next)
11946 {
11947 Elf_Internal_Rela rel[3];
11948 bfd_vma addend = 0;
11949
11950 memset (rel, 0, sizeof (rel));
11951 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11952
11953 for (g = gg->next; g->next != gg; g = g->next)
11954 {
11955 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
11956 + g->next->tls_gotno;
11957
11958 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
11959 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11960 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11961 sgot->contents
11962 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11963
11964 if (! bfd_link_pic (info))
11965 continue;
11966
11967 for (; got_index < g->local_gotno; got_index++)
11968 {
11969 if (got_index >= g->assigned_low_gotno
11970 && got_index <= g->assigned_high_gotno)
11971 continue;
11972
11973 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
11974 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
11975 if (!(mips_elf_create_dynamic_relocation
11976 (output_bfd, info, rel, NULL,
11977 bfd_abs_section_ptr,
11978 0, &addend, sgot)))
11979 return FALSE;
11980 BFD_ASSERT (addend == 0);
11981 }
11982 }
11983 }
11984
11985 /* The generation of dynamic relocations for the non-primary gots
11986 adds more dynamic relocations. We cannot count them until
11987 here. */
11988
11989 if (elf_hash_table (info)->dynamic_sections_created)
11990 {
11991 bfd_byte *b;
11992 bfd_boolean swap_out_p;
11993
11994 BFD_ASSERT (sdyn != NULL);
11995
11996 for (b = sdyn->contents;
11997 b < sdyn->contents + sdyn->size;
11998 b += MIPS_ELF_DYN_SIZE (dynobj))
11999 {
12000 Elf_Internal_Dyn dyn;
12001 asection *s;
12002
12003 /* Read in the current dynamic entry. */
12004 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12005
12006 /* Assume that we're going to modify it and write it out. */
12007 swap_out_p = TRUE;
12008
12009 switch (dyn.d_tag)
12010 {
12011 case DT_RELSZ:
12012 /* Reduce DT_RELSZ to account for any relocations we
12013 decided not to make. This is for the n64 irix rld,
12014 which doesn't seem to apply any relocations if there
12015 are trailing null entries. */
12016 s = mips_elf_rel_dyn_section (info, FALSE);
12017 dyn.d_un.d_val = (s->reloc_count
12018 * (ABI_64_P (output_bfd)
12019 ? sizeof (Elf64_Mips_External_Rel)
12020 : sizeof (Elf32_External_Rel)));
12021 /* Adjust the section size too. Tools like the prelinker
12022 can reasonably expect the values to the same. */
12023 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12024 elf_section_data (s->output_section)->this_hdr.sh_size
12025 = dyn.d_un.d_val;
12026 break;
12027
12028 default:
12029 swap_out_p = FALSE;
12030 break;
12031 }
12032
12033 if (swap_out_p)
12034 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12035 (dynobj, &dyn, b);
12036 }
12037 }
12038
12039 {
12040 asection *s;
12041 Elf32_compact_rel cpt;
12042
12043 if (SGI_COMPAT (output_bfd))
12044 {
12045 /* Write .compact_rel section out. */
12046 s = bfd_get_linker_section (dynobj, ".compact_rel");
12047 if (s != NULL)
12048 {
12049 cpt.id1 = 1;
12050 cpt.num = s->reloc_count;
12051 cpt.id2 = 2;
12052 cpt.offset = (s->output_section->filepos
12053 + sizeof (Elf32_External_compact_rel));
12054 cpt.reserved0 = 0;
12055 cpt.reserved1 = 0;
12056 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12057 ((Elf32_External_compact_rel *)
12058 s->contents));
12059
12060 /* Clean up a dummy stub function entry in .text. */
12061 if (htab->sstubs != NULL)
12062 {
12063 file_ptr dummy_offset;
12064
12065 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12066 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12067 memset (htab->sstubs->contents + dummy_offset, 0,
12068 htab->function_stub_size);
12069 }
12070 }
12071 }
12072
12073 /* The psABI says that the dynamic relocations must be sorted in
12074 increasing order of r_symndx. The VxWorks EABI doesn't require
12075 this, and because the code below handles REL rather than RELA
12076 relocations, using it for VxWorks would be outright harmful. */
12077 if (!htab->is_vxworks)
12078 {
12079 s = mips_elf_rel_dyn_section (info, FALSE);
12080 if (s != NULL
12081 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12082 {
12083 reldyn_sorting_bfd = output_bfd;
12084
12085 if (ABI_64_P (output_bfd))
12086 qsort ((Elf64_External_Rel *) s->contents + 1,
12087 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12088 sort_dynamic_relocs_64);
12089 else
12090 qsort ((Elf32_External_Rel *) s->contents + 1,
12091 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12092 sort_dynamic_relocs);
12093 }
12094 }
12095 }
12096
12097 if (htab->root.splt && htab->root.splt->size > 0)
12098 {
12099 if (htab->is_vxworks)
12100 {
12101 if (bfd_link_pic (info))
12102 mips_vxworks_finish_shared_plt (output_bfd, info);
12103 else
12104 mips_vxworks_finish_exec_plt (output_bfd, info);
12105 }
12106 else
12107 {
12108 BFD_ASSERT (!bfd_link_pic (info));
12109 if (!mips_finish_exec_plt (output_bfd, info))
12110 return FALSE;
12111 }
12112 }
12113 return TRUE;
12114 }
12115
12116
12117 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12118
12119 static void
12120 mips_set_isa_flags (bfd *abfd)
12121 {
12122 flagword val;
12123
12124 switch (bfd_get_mach (abfd))
12125 {
12126 default:
12127 case bfd_mach_mips3000:
12128 val = E_MIPS_ARCH_1;
12129 break;
12130
12131 case bfd_mach_mips3900:
12132 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12133 break;
12134
12135 case bfd_mach_mips6000:
12136 val = E_MIPS_ARCH_2;
12137 break;
12138
12139 case bfd_mach_mips4010:
12140 val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12141 break;
12142
12143 case bfd_mach_mips4000:
12144 case bfd_mach_mips4300:
12145 case bfd_mach_mips4400:
12146 case bfd_mach_mips4600:
12147 val = E_MIPS_ARCH_3;
12148 break;
12149
12150 case bfd_mach_mips4100:
12151 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12152 break;
12153
12154 case bfd_mach_mips4111:
12155 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12156 break;
12157
12158 case bfd_mach_mips4120:
12159 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12160 break;
12161
12162 case bfd_mach_mips4650:
12163 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12164 break;
12165
12166 case bfd_mach_mips5400:
12167 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12168 break;
12169
12170 case bfd_mach_mips5500:
12171 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12172 break;
12173
12174 case bfd_mach_mips5900:
12175 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12176 break;
12177
12178 case bfd_mach_mips9000:
12179 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12180 break;
12181
12182 case bfd_mach_mips5000:
12183 case bfd_mach_mips7000:
12184 case bfd_mach_mips8000:
12185 case bfd_mach_mips10000:
12186 case bfd_mach_mips12000:
12187 case bfd_mach_mips14000:
12188 case bfd_mach_mips16000:
12189 val = E_MIPS_ARCH_4;
12190 break;
12191
12192 case bfd_mach_mips5:
12193 val = E_MIPS_ARCH_5;
12194 break;
12195
12196 case bfd_mach_mips_loongson_2e:
12197 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12198 break;
12199
12200 case bfd_mach_mips_loongson_2f:
12201 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12202 break;
12203
12204 case bfd_mach_mips_sb1:
12205 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12206 break;
12207
12208 case bfd_mach_mips_gs464:
12209 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
12210 break;
12211
12212 case bfd_mach_mips_gs464e:
12213 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12214 break;
12215
12216 case bfd_mach_mips_gs264e:
12217 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12218 break;
12219
12220 case bfd_mach_mips_octeon:
12221 case bfd_mach_mips_octeonp:
12222 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12223 break;
12224
12225 case bfd_mach_mips_octeon3:
12226 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12227 break;
12228
12229 case bfd_mach_mips_xlr:
12230 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12231 break;
12232
12233 case bfd_mach_mips_octeon2:
12234 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12235 break;
12236
12237 case bfd_mach_mipsisa32:
12238 val = E_MIPS_ARCH_32;
12239 break;
12240
12241 case bfd_mach_mipsisa64:
12242 val = E_MIPS_ARCH_64;
12243 break;
12244
12245 case bfd_mach_mipsisa32r2:
12246 case bfd_mach_mipsisa32r3:
12247 case bfd_mach_mipsisa32r5:
12248 val = E_MIPS_ARCH_32R2;
12249 break;
12250
12251 case bfd_mach_mips_interaptiv_mr2:
12252 val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12253 break;
12254
12255 case bfd_mach_mipsisa64r2:
12256 case bfd_mach_mipsisa64r3:
12257 case bfd_mach_mipsisa64r5:
12258 val = E_MIPS_ARCH_64R2;
12259 break;
12260
12261 case bfd_mach_mipsisa32r6:
12262 val = E_MIPS_ARCH_32R6;
12263 break;
12264
12265 case bfd_mach_mipsisa64r6:
12266 val = E_MIPS_ARCH_64R6;
12267 break;
12268 }
12269 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12270 elf_elfheader (abfd)->e_flags |= val;
12271
12272 }
12273
12274
12275 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12276 Don't do so for code sections. We want to keep ordering of HI16/LO16
12277 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12278 relocs to be sorted. */
12279
12280 bfd_boolean
12281 _bfd_mips_elf_sort_relocs_p (asection *sec)
12282 {
12283 return (sec->flags & SEC_CODE) == 0;
12284 }
12285
12286
12287 /* The final processing done just before writing out a MIPS ELF object
12288 file. This gets the MIPS architecture right based on the machine
12289 number. This is used by both the 32-bit and the 64-bit ABI. */
12290
12291 void
12292 _bfd_mips_elf_final_write_processing (bfd *abfd,
12293 bfd_boolean linker ATTRIBUTE_UNUSED)
12294 {
12295 unsigned int i;
12296 Elf_Internal_Shdr **hdrpp;
12297 const char *name;
12298 asection *sec;
12299
12300 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12301 is nonzero. This is for compatibility with old objects, which used
12302 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12303 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12304 mips_set_isa_flags (abfd);
12305
12306 /* Set the sh_info field for .gptab sections and other appropriate
12307 info for each special section. */
12308 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12309 i < elf_numsections (abfd);
12310 i++, hdrpp++)
12311 {
12312 switch ((*hdrpp)->sh_type)
12313 {
12314 case SHT_MIPS_MSYM:
12315 case SHT_MIPS_LIBLIST:
12316 sec = bfd_get_section_by_name (abfd, ".dynstr");
12317 if (sec != NULL)
12318 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12319 break;
12320
12321 case SHT_MIPS_GPTAB:
12322 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12323 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
12324 BFD_ASSERT (name != NULL
12325 && CONST_STRNEQ (name, ".gptab."));
12326 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12327 BFD_ASSERT (sec != NULL);
12328 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12329 break;
12330
12331 case SHT_MIPS_CONTENT:
12332 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12333 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
12334 BFD_ASSERT (name != NULL
12335 && CONST_STRNEQ (name, ".MIPS.content"));
12336 sec = bfd_get_section_by_name (abfd,
12337 name + sizeof ".MIPS.content" - 1);
12338 BFD_ASSERT (sec != NULL);
12339 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12340 break;
12341
12342 case SHT_MIPS_SYMBOL_LIB:
12343 sec = bfd_get_section_by_name (abfd, ".dynsym");
12344 if (sec != NULL)
12345 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12346 sec = bfd_get_section_by_name (abfd, ".liblist");
12347 if (sec != NULL)
12348 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12349 break;
12350
12351 case SHT_MIPS_EVENTS:
12352 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12353 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
12354 BFD_ASSERT (name != NULL);
12355 if (CONST_STRNEQ (name, ".MIPS.events"))
12356 sec = bfd_get_section_by_name (abfd,
12357 name + sizeof ".MIPS.events" - 1);
12358 else
12359 {
12360 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
12361 sec = bfd_get_section_by_name (abfd,
12362 (name
12363 + sizeof ".MIPS.post_rel" - 1));
12364 }
12365 BFD_ASSERT (sec != NULL);
12366 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12367 break;
12368
12369 }
12370 }
12371 }
12372
12373 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
12375 segments. */
12376
12377 int
12378 _bfd_mips_elf_additional_program_headers (bfd *abfd,
12379 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12380 {
12381 asection *s;
12382 int ret = 0;
12383
12384 /* See if we need a PT_MIPS_REGINFO segment. */
12385 s = bfd_get_section_by_name (abfd, ".reginfo");
12386 if (s && (s->flags & SEC_LOAD))
12387 ++ret;
12388
12389 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12390 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12391 ++ret;
12392
12393 /* See if we need a PT_MIPS_OPTIONS segment. */
12394 if (IRIX_COMPAT (abfd) == ict_irix6
12395 && bfd_get_section_by_name (abfd,
12396 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12397 ++ret;
12398
12399 /* See if we need a PT_MIPS_RTPROC segment. */
12400 if (IRIX_COMPAT (abfd) == ict_irix5
12401 && bfd_get_section_by_name (abfd, ".dynamic")
12402 && bfd_get_section_by_name (abfd, ".mdebug"))
12403 ++ret;
12404
12405 /* Allocate a PT_NULL header in dynamic objects. See
12406 _bfd_mips_elf_modify_segment_map for details. */
12407 if (!SGI_COMPAT (abfd)
12408 && bfd_get_section_by_name (abfd, ".dynamic"))
12409 ++ret;
12410
12411 return ret;
12412 }
12413
12414 /* Modify the segment map for an IRIX5 executable. */
12415
12416 bfd_boolean
12417 _bfd_mips_elf_modify_segment_map (bfd *abfd,
12418 struct bfd_link_info *info)
12419 {
12420 asection *s;
12421 struct elf_segment_map *m, **pm;
12422 bfd_size_type amt;
12423
12424 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12425 segment. */
12426 s = bfd_get_section_by_name (abfd, ".reginfo");
12427 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12428 {
12429 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12430 if (m->p_type == PT_MIPS_REGINFO)
12431 break;
12432 if (m == NULL)
12433 {
12434 amt = sizeof *m;
12435 m = bfd_zalloc (abfd, amt);
12436 if (m == NULL)
12437 return FALSE;
12438
12439 m->p_type = PT_MIPS_REGINFO;
12440 m->count = 1;
12441 m->sections[0] = s;
12442
12443 /* We want to put it after the PHDR and INTERP segments. */
12444 pm = &elf_seg_map (abfd);
12445 while (*pm != NULL
12446 && ((*pm)->p_type == PT_PHDR
12447 || (*pm)->p_type == PT_INTERP))
12448 pm = &(*pm)->next;
12449
12450 m->next = *pm;
12451 *pm = m;
12452 }
12453 }
12454
12455 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12456 segment. */
12457 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12458 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12459 {
12460 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12461 if (m->p_type == PT_MIPS_ABIFLAGS)
12462 break;
12463 if (m == NULL)
12464 {
12465 amt = sizeof *m;
12466 m = bfd_zalloc (abfd, amt);
12467 if (m == NULL)
12468 return FALSE;
12469
12470 m->p_type = PT_MIPS_ABIFLAGS;
12471 m->count = 1;
12472 m->sections[0] = s;
12473
12474 /* We want to put it after the PHDR and INTERP segments. */
12475 pm = &elf_seg_map (abfd);
12476 while (*pm != NULL
12477 && ((*pm)->p_type == PT_PHDR
12478 || (*pm)->p_type == PT_INTERP))
12479 pm = &(*pm)->next;
12480
12481 m->next = *pm;
12482 *pm = m;
12483 }
12484 }
12485
12486 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12487 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
12488 PT_MIPS_OPTIONS segment immediately following the program header
12489 table. */
12490 if (NEWABI_P (abfd)
12491 /* On non-IRIX6 new abi, we'll have already created a segment
12492 for this section, so don't create another. I'm not sure this
12493 is not also the case for IRIX 6, but I can't test it right
12494 now. */
12495 && IRIX_COMPAT (abfd) == ict_irix6)
12496 {
12497 for (s = abfd->sections; s; s = s->next)
12498 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12499 break;
12500
12501 if (s)
12502 {
12503 struct elf_segment_map *options_segment;
12504
12505 pm = &elf_seg_map (abfd);
12506 while (*pm != NULL
12507 && ((*pm)->p_type == PT_PHDR
12508 || (*pm)->p_type == PT_INTERP))
12509 pm = &(*pm)->next;
12510
12511 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12512 {
12513 amt = sizeof (struct elf_segment_map);
12514 options_segment = bfd_zalloc (abfd, amt);
12515 options_segment->next = *pm;
12516 options_segment->p_type = PT_MIPS_OPTIONS;
12517 options_segment->p_flags = PF_R;
12518 options_segment->p_flags_valid = TRUE;
12519 options_segment->count = 1;
12520 options_segment->sections[0] = s;
12521 *pm = options_segment;
12522 }
12523 }
12524 }
12525 else
12526 {
12527 if (IRIX_COMPAT (abfd) == ict_irix5)
12528 {
12529 /* If there are .dynamic and .mdebug sections, we make a room
12530 for the RTPROC header. FIXME: Rewrite without section names. */
12531 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12532 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12533 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12534 {
12535 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12536 if (m->p_type == PT_MIPS_RTPROC)
12537 break;
12538 if (m == NULL)
12539 {
12540 amt = sizeof *m;
12541 m = bfd_zalloc (abfd, amt);
12542 if (m == NULL)
12543 return FALSE;
12544
12545 m->p_type = PT_MIPS_RTPROC;
12546
12547 s = bfd_get_section_by_name (abfd, ".rtproc");
12548 if (s == NULL)
12549 {
12550 m->count = 0;
12551 m->p_flags = 0;
12552 m->p_flags_valid = 1;
12553 }
12554 else
12555 {
12556 m->count = 1;
12557 m->sections[0] = s;
12558 }
12559
12560 /* We want to put it after the DYNAMIC segment. */
12561 pm = &elf_seg_map (abfd);
12562 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12563 pm = &(*pm)->next;
12564 if (*pm != NULL)
12565 pm = &(*pm)->next;
12566
12567 m->next = *pm;
12568 *pm = m;
12569 }
12570 }
12571 }
12572 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12573 .dynstr, .dynsym, and .hash sections, and everything in
12574 between. */
12575 for (pm = &elf_seg_map (abfd); *pm != NULL;
12576 pm = &(*pm)->next)
12577 if ((*pm)->p_type == PT_DYNAMIC)
12578 break;
12579 m = *pm;
12580 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12581 glibc's dynamic linker has traditionally derived the number of
12582 tags from the p_filesz field, and sometimes allocates stack
12583 arrays of that size. An overly-big PT_DYNAMIC segment can
12584 be actively harmful in such cases. Making PT_DYNAMIC contain
12585 other sections can also make life hard for the prelinker,
12586 which might move one of the other sections to a different
12587 PT_LOAD segment. */
12588 if (SGI_COMPAT (abfd)
12589 && m != NULL
12590 && m->count == 1
12591 && strcmp (m->sections[0]->name, ".dynamic") == 0)
12592 {
12593 static const char *sec_names[] =
12594 {
12595 ".dynamic", ".dynstr", ".dynsym", ".hash"
12596 };
12597 bfd_vma low, high;
12598 unsigned int i, c;
12599 struct elf_segment_map *n;
12600
12601 low = ~(bfd_vma) 0;
12602 high = 0;
12603 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12604 {
12605 s = bfd_get_section_by_name (abfd, sec_names[i]);
12606 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12607 {
12608 bfd_size_type sz;
12609
12610 if (low > s->vma)
12611 low = s->vma;
12612 sz = s->size;
12613 if (high < s->vma + sz)
12614 high = s->vma + sz;
12615 }
12616 }
12617
12618 c = 0;
12619 for (s = abfd->sections; s != NULL; s = s->next)
12620 if ((s->flags & SEC_LOAD) != 0
12621 && s->vma >= low
12622 && s->vma + s->size <= high)
12623 ++c;
12624
12625 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
12626 n = bfd_zalloc (abfd, amt);
12627 if (n == NULL)
12628 return FALSE;
12629 *n = *m;
12630 n->count = c;
12631
12632 i = 0;
12633 for (s = abfd->sections; s != NULL; s = s->next)
12634 {
12635 if ((s->flags & SEC_LOAD) != 0
12636 && s->vma >= low
12637 && s->vma + s->size <= high)
12638 {
12639 n->sections[i] = s;
12640 ++i;
12641 }
12642 }
12643
12644 *pm = n;
12645 }
12646 }
12647
12648 /* Allocate a spare program header in dynamic objects so that tools
12649 like the prelinker can add an extra PT_LOAD entry.
12650
12651 If the prelinker needs to make room for a new PT_LOAD entry, its
12652 standard procedure is to move the first (read-only) sections into
12653 the new (writable) segment. However, the MIPS ABI requires
12654 .dynamic to be in a read-only segment, and the section will often
12655 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12656
12657 Although the prelinker could in principle move .dynamic to a
12658 writable segment, it seems better to allocate a spare program
12659 header instead, and avoid the need to move any sections.
12660 There is a long tradition of allocating spare dynamic tags,
12661 so allocating a spare program header seems like a natural
12662 extension.
12663
12664 If INFO is NULL, we may be copying an already prelinked binary
12665 with objcopy or strip, so do not add this header. */
12666 if (info != NULL
12667 && !SGI_COMPAT (abfd)
12668 && bfd_get_section_by_name (abfd, ".dynamic"))
12669 {
12670 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12671 if ((*pm)->p_type == PT_NULL)
12672 break;
12673 if (*pm == NULL)
12674 {
12675 m = bfd_zalloc (abfd, sizeof (*m));
12676 if (m == NULL)
12677 return FALSE;
12678
12679 m->p_type = PT_NULL;
12680 *pm = m;
12681 }
12682 }
12683
12684 return TRUE;
12685 }
12686
12687 /* Return the section that should be marked against GC for a given
12689 relocation. */
12690
12691 asection *
12692 _bfd_mips_elf_gc_mark_hook (asection *sec,
12693 struct bfd_link_info *info,
12694 Elf_Internal_Rela *rel,
12695 struct elf_link_hash_entry *h,
12696 Elf_Internal_Sym *sym)
12697 {
12698 /* ??? Do mips16 stub sections need to be handled special? */
12699
12700 if (h != NULL)
12701 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12702 {
12703 case R_MIPS_GNU_VTINHERIT:
12704 case R_MIPS_GNU_VTENTRY:
12705 return NULL;
12706 }
12707
12708 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12709 }
12710
12711 /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12712
12713 bfd_boolean
12714 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12715 elf_gc_mark_hook_fn gc_mark_hook)
12716 {
12717 bfd *sub;
12718
12719 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12720
12721 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12722 {
12723 asection *o;
12724
12725 if (! is_mips_elf (sub))
12726 continue;
12727
12728 for (o = sub->sections; o != NULL; o = o->next)
12729 if (!o->gc_mark
12730 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P
12731 (bfd_get_section_name (sub, o)))
12732 {
12733 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12734 return FALSE;
12735 }
12736 }
12737
12738 return TRUE;
12739 }
12740
12741 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12743 hiding the old indirect symbol. Process additional relocation
12744 information. Also called for weakdefs, in which case we just let
12745 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12746
12747 void
12748 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12749 struct elf_link_hash_entry *dir,
12750 struct elf_link_hash_entry *ind)
12751 {
12752 struct mips_elf_link_hash_entry *dirmips, *indmips;
12753
12754 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12755
12756 dirmips = (struct mips_elf_link_hash_entry *) dir;
12757 indmips = (struct mips_elf_link_hash_entry *) ind;
12758 /* Any absolute non-dynamic relocations against an indirect or weak
12759 definition will be against the target symbol. */
12760 if (indmips->has_static_relocs)
12761 dirmips->has_static_relocs = TRUE;
12762
12763 if (ind->root.type != bfd_link_hash_indirect)
12764 return;
12765
12766 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12767 if (indmips->readonly_reloc)
12768 dirmips->readonly_reloc = TRUE;
12769 if (indmips->no_fn_stub)
12770 dirmips->no_fn_stub = TRUE;
12771 if (indmips->fn_stub)
12772 {
12773 dirmips->fn_stub = indmips->fn_stub;
12774 indmips->fn_stub = NULL;
12775 }
12776 if (indmips->need_fn_stub)
12777 {
12778 dirmips->need_fn_stub = TRUE;
12779 indmips->need_fn_stub = FALSE;
12780 }
12781 if (indmips->call_stub)
12782 {
12783 dirmips->call_stub = indmips->call_stub;
12784 indmips->call_stub = NULL;
12785 }
12786 if (indmips->call_fp_stub)
12787 {
12788 dirmips->call_fp_stub = indmips->call_fp_stub;
12789 indmips->call_fp_stub = NULL;
12790 }
12791 if (indmips->global_got_area < dirmips->global_got_area)
12792 dirmips->global_got_area = indmips->global_got_area;
12793 if (indmips->global_got_area < GGA_NONE)
12794 indmips->global_got_area = GGA_NONE;
12795 if (indmips->has_nonpic_branches)
12796 dirmips->has_nonpic_branches = TRUE;
12797 }
12798
12799 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
12800 to hide it. It has to remain global (it will also be protected) so as to
12801 be assigned a global GOT entry, which will then remain unchanged at load
12802 time. */
12803
12804 void
12805 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
12806 struct elf_link_hash_entry *entry,
12807 bfd_boolean force_local)
12808 {
12809 struct mips_elf_link_hash_table *htab;
12810
12811 htab = mips_elf_hash_table (info);
12812 BFD_ASSERT (htab != NULL);
12813 if (htab->use_absolute_zero
12814 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
12815 return;
12816
12817 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
12818 }
12819
12820 #define PDR_SIZE 32
12822
12823 bfd_boolean
12824 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12825 struct bfd_link_info *info)
12826 {
12827 asection *o;
12828 bfd_boolean ret = FALSE;
12829 unsigned char *tdata;
12830 size_t i, skip;
12831
12832 o = bfd_get_section_by_name (abfd, ".pdr");
12833 if (! o)
12834 return FALSE;
12835 if (o->size == 0)
12836 return FALSE;
12837 if (o->size % PDR_SIZE != 0)
12838 return FALSE;
12839 if (o->output_section != NULL
12840 && bfd_is_abs_section (o->output_section))
12841 return FALSE;
12842
12843 tdata = bfd_zmalloc (o->size / PDR_SIZE);
12844 if (! tdata)
12845 return FALSE;
12846
12847 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
12848 info->keep_memory);
12849 if (!cookie->rels)
12850 {
12851 free (tdata);
12852 return FALSE;
12853 }
12854
12855 cookie->rel = cookie->rels;
12856 cookie->relend = cookie->rels + o->reloc_count;
12857
12858 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
12859 {
12860 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
12861 {
12862 tdata[i] = 1;
12863 skip ++;
12864 }
12865 }
12866
12867 if (skip != 0)
12868 {
12869 mips_elf_section_data (o)->u.tdata = tdata;
12870 if (o->rawsize == 0)
12871 o->rawsize = o->size;
12872 o->size -= skip * PDR_SIZE;
12873 ret = TRUE;
12874 }
12875 else
12876 free (tdata);
12877
12878 if (! info->keep_memory)
12879 free (cookie->rels);
12880
12881 return ret;
12882 }
12883
12884 bfd_boolean
12885 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
12886 {
12887 if (strcmp (sec->name, ".pdr") == 0)
12888 return TRUE;
12889 return FALSE;
12890 }
12891
12892 bfd_boolean
12893 _bfd_mips_elf_write_section (bfd *output_bfd,
12894 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
12895 asection *sec, bfd_byte *contents)
12896 {
12897 bfd_byte *to, *from, *end;
12898 int i;
12899
12900 if (strcmp (sec->name, ".pdr") != 0)
12901 return FALSE;
12902
12903 if (mips_elf_section_data (sec)->u.tdata == NULL)
12904 return FALSE;
12905
12906 to = contents;
12907 end = contents + sec->size;
12908 for (from = contents, i = 0;
12909 from < end;
12910 from += PDR_SIZE, i++)
12911 {
12912 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
12913 continue;
12914 if (to != from)
12915 memcpy (to, from, PDR_SIZE);
12916 to += PDR_SIZE;
12917 }
12918 bfd_set_section_contents (output_bfd, sec->output_section, contents,
12919 sec->output_offset, sec->size);
12920 return TRUE;
12921 }
12922
12923 /* microMIPS code retains local labels for linker relaxation. Omit them
12925 from output by default for clarity. */
12926
12927 bfd_boolean
12928 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12929 {
12930 return _bfd_elf_is_local_label_name (abfd, sym->name);
12931 }
12932
12933 /* MIPS ELF uses a special find_nearest_line routine in order the
12934 handle the ECOFF debugging information. */
12935
12936 struct mips_elf_find_line
12937 {
12938 struct ecoff_debug_info d;
12939 struct ecoff_find_line i;
12940 };
12941
12942 bfd_boolean
12943 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
12944 asection *section, bfd_vma offset,
12945 const char **filename_ptr,
12946 const char **functionname_ptr,
12947 unsigned int *line_ptr,
12948 unsigned int *discriminator_ptr)
12949 {
12950 asection *msec;
12951
12952 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
12953 filename_ptr, functionname_ptr,
12954 line_ptr, discriminator_ptr,
12955 dwarf_debug_sections,
12956 ABI_64_P (abfd) ? 8 : 0,
12957 &elf_tdata (abfd)->dwarf2_find_line_info)
12958 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
12959 filename_ptr, functionname_ptr,
12960 line_ptr))
12961 {
12962 /* PR 22789: If the function name or filename was not found through
12963 the debug information, then try an ordinary lookup instead. */
12964 if ((functionname_ptr != NULL && *functionname_ptr == NULL)
12965 || (filename_ptr != NULL && *filename_ptr == NULL))
12966 {
12967 /* Do not override already discovered names. */
12968 if (functionname_ptr != NULL && *functionname_ptr != NULL)
12969 functionname_ptr = NULL;
12970
12971 if (filename_ptr != NULL && *filename_ptr != NULL)
12972 filename_ptr = NULL;
12973
12974 _bfd_elf_find_function (abfd, symbols, section, offset,
12975 filename_ptr, functionname_ptr);
12976 }
12977
12978 return TRUE;
12979 }
12980
12981 msec = bfd_get_section_by_name (abfd, ".mdebug");
12982 if (msec != NULL)
12983 {
12984 flagword origflags;
12985 struct mips_elf_find_line *fi;
12986 const struct ecoff_debug_swap * const swap =
12987 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12988
12989 /* If we are called during a link, mips_elf_final_link may have
12990 cleared the SEC_HAS_CONTENTS field. We force it back on here
12991 if appropriate (which it normally will be). */
12992 origflags = msec->flags;
12993 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12994 msec->flags |= SEC_HAS_CONTENTS;
12995
12996 fi = mips_elf_tdata (abfd)->find_line_info;
12997 if (fi == NULL)
12998 {
12999 bfd_size_type external_fdr_size;
13000 char *fraw_src;
13001 char *fraw_end;
13002 struct fdr *fdr_ptr;
13003 bfd_size_type amt = sizeof (struct mips_elf_find_line);
13004
13005 fi = bfd_zalloc (abfd, amt);
13006 if (fi == NULL)
13007 {
13008 msec->flags = origflags;
13009 return FALSE;
13010 }
13011
13012 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13013 {
13014 msec->flags = origflags;
13015 return FALSE;
13016 }
13017
13018 /* Swap in the FDR information. */
13019 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13020 fi->d.fdr = bfd_alloc (abfd, amt);
13021 if (fi->d.fdr == NULL)
13022 {
13023 msec->flags = origflags;
13024 return FALSE;
13025 }
13026 external_fdr_size = swap->external_fdr_size;
13027 fdr_ptr = fi->d.fdr;
13028 fraw_src = (char *) fi->d.external_fdr;
13029 fraw_end = (fraw_src
13030 + fi->d.symbolic_header.ifdMax * external_fdr_size);
13031 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13032 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13033
13034 mips_elf_tdata (abfd)->find_line_info = fi;
13035
13036 /* Note that we don't bother to ever free this information.
13037 find_nearest_line is either called all the time, as in
13038 objdump -l, so the information should be saved, or it is
13039 rarely called, as in ld error messages, so the memory
13040 wasted is unimportant. Still, it would probably be a
13041 good idea for free_cached_info to throw it away. */
13042 }
13043
13044 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13045 &fi->i, filename_ptr, functionname_ptr,
13046 line_ptr))
13047 {
13048 msec->flags = origflags;
13049 return TRUE;
13050 }
13051
13052 msec->flags = origflags;
13053 }
13054
13055 /* Fall back on the generic ELF find_nearest_line routine. */
13056
13057 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13058 filename_ptr, functionname_ptr,
13059 line_ptr, discriminator_ptr);
13060 }
13061
13062 bfd_boolean
13063 _bfd_mips_elf_find_inliner_info (bfd *abfd,
13064 const char **filename_ptr,
13065 const char **functionname_ptr,
13066 unsigned int *line_ptr)
13067 {
13068 bfd_boolean found;
13069 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13070 functionname_ptr, line_ptr,
13071 & elf_tdata (abfd)->dwarf2_find_line_info);
13072 return found;
13073 }
13074
13075
13076 /* When are writing out the .options or .MIPS.options section,
13078 remember the bytes we are writing out, so that we can install the
13079 GP value in the section_processing routine. */
13080
13081 bfd_boolean
13082 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13083 const void *location,
13084 file_ptr offset, bfd_size_type count)
13085 {
13086 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13087 {
13088 bfd_byte *c;
13089
13090 if (elf_section_data (section) == NULL)
13091 {
13092 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
13093 section->used_by_bfd = bfd_zalloc (abfd, amt);
13094 if (elf_section_data (section) == NULL)
13095 return FALSE;
13096 }
13097 c = mips_elf_section_data (section)->u.tdata;
13098 if (c == NULL)
13099 {
13100 c = bfd_zalloc (abfd, section->size);
13101 if (c == NULL)
13102 return FALSE;
13103 mips_elf_section_data (section)->u.tdata = c;
13104 }
13105
13106 memcpy (c + offset, location, count);
13107 }
13108
13109 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13110 count);
13111 }
13112
13113 /* This is almost identical to bfd_generic_get_... except that some
13114 MIPS relocations need to be handled specially. Sigh. */
13115
13116 bfd_byte *
13117 _bfd_elf_mips_get_relocated_section_contents
13118 (bfd *abfd,
13119 struct bfd_link_info *link_info,
13120 struct bfd_link_order *link_order,
13121 bfd_byte *data,
13122 bfd_boolean relocatable,
13123 asymbol **symbols)
13124 {
13125 /* Get enough memory to hold the stuff */
13126 bfd *input_bfd = link_order->u.indirect.section->owner;
13127 asection *input_section = link_order->u.indirect.section;
13128 bfd_size_type sz;
13129
13130 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13131 arelent **reloc_vector = NULL;
13132 long reloc_count;
13133
13134 if (reloc_size < 0)
13135 goto error_return;
13136
13137 reloc_vector = bfd_malloc (reloc_size);
13138 if (reloc_vector == NULL && reloc_size != 0)
13139 goto error_return;
13140
13141 /* read in the section */
13142 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
13143 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
13144 goto error_return;
13145
13146 reloc_count = bfd_canonicalize_reloc (input_bfd,
13147 input_section,
13148 reloc_vector,
13149 symbols);
13150 if (reloc_count < 0)
13151 goto error_return;
13152
13153 if (reloc_count > 0)
13154 {
13155 arelent **parent;
13156 /* for mips */
13157 int gp_found;
13158 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13159
13160 {
13161 struct bfd_hash_entry *h;
13162 struct bfd_link_hash_entry *lh;
13163 /* Skip all this stuff if we aren't mixing formats. */
13164 if (abfd && input_bfd
13165 && abfd->xvec == input_bfd->xvec)
13166 lh = 0;
13167 else
13168 {
13169 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
13170 lh = (struct bfd_link_hash_entry *) h;
13171 }
13172 lookup:
13173 if (lh)
13174 {
13175 switch (lh->type)
13176 {
13177 case bfd_link_hash_undefined:
13178 case bfd_link_hash_undefweak:
13179 case bfd_link_hash_common:
13180 gp_found = 0;
13181 break;
13182 case bfd_link_hash_defined:
13183 case bfd_link_hash_defweak:
13184 gp_found = 1;
13185 gp = lh->u.def.value;
13186 break;
13187 case bfd_link_hash_indirect:
13188 case bfd_link_hash_warning:
13189 lh = lh->u.i.link;
13190 /* @@FIXME ignoring warning for now */
13191 goto lookup;
13192 case bfd_link_hash_new:
13193 default:
13194 abort ();
13195 }
13196 }
13197 else
13198 gp_found = 0;
13199 }
13200 /* end mips */
13201 for (parent = reloc_vector; *parent != NULL; parent++)
13202 {
13203 char *error_message = NULL;
13204 bfd_reloc_status_type r;
13205
13206 /* Specific to MIPS: Deal with relocation types that require
13207 knowing the gp of the output bfd. */
13208 asymbol *sym = *(*parent)->sym_ptr_ptr;
13209
13210 /* If we've managed to find the gp and have a special
13211 function for the relocation then go ahead, else default
13212 to the generic handling. */
13213 if (gp_found
13214 && (*parent)->howto->special_function
13215 == _bfd_mips_elf32_gprel16_reloc)
13216 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
13217 input_section, relocatable,
13218 data, gp);
13219 else
13220 r = bfd_perform_relocation (input_bfd, *parent, data,
13221 input_section,
13222 relocatable ? abfd : NULL,
13223 &error_message);
13224
13225 if (relocatable)
13226 {
13227 asection *os = input_section->output_section;
13228
13229 /* A partial link, so keep the relocs */
13230 os->orelocation[os->reloc_count] = *parent;
13231 os->reloc_count++;
13232 }
13233
13234 if (r != bfd_reloc_ok)
13235 {
13236 switch (r)
13237 {
13238 case bfd_reloc_undefined:
13239 (*link_info->callbacks->undefined_symbol)
13240 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13241 input_bfd, input_section, (*parent)->address, TRUE);
13242 break;
13243 case bfd_reloc_dangerous:
13244 BFD_ASSERT (error_message != NULL);
13245 (*link_info->callbacks->reloc_dangerous)
13246 (link_info, error_message,
13247 input_bfd, input_section, (*parent)->address);
13248 break;
13249 case bfd_reloc_overflow:
13250 (*link_info->callbacks->reloc_overflow)
13251 (link_info, NULL,
13252 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13253 (*parent)->howto->name, (*parent)->addend,
13254 input_bfd, input_section, (*parent)->address);
13255 break;
13256 case bfd_reloc_outofrange:
13257 default:
13258 abort ();
13259 break;
13260 }
13261
13262 }
13263 }
13264 }
13265 if (reloc_vector != NULL)
13266 free (reloc_vector);
13267 return data;
13268
13269 error_return:
13270 if (reloc_vector != NULL)
13271 free (reloc_vector);
13272 return NULL;
13273 }
13274
13275 static bfd_boolean
13277 mips_elf_relax_delete_bytes (bfd *abfd,
13278 asection *sec, bfd_vma addr, int count)
13279 {
13280 Elf_Internal_Shdr *symtab_hdr;
13281 unsigned int sec_shndx;
13282 bfd_byte *contents;
13283 Elf_Internal_Rela *irel, *irelend;
13284 Elf_Internal_Sym *isym;
13285 Elf_Internal_Sym *isymend;
13286 struct elf_link_hash_entry **sym_hashes;
13287 struct elf_link_hash_entry **end_hashes;
13288 struct elf_link_hash_entry **start_hashes;
13289 unsigned int symcount;
13290
13291 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13292 contents = elf_section_data (sec)->this_hdr.contents;
13293
13294 irel = elf_section_data (sec)->relocs;
13295 irelend = irel + sec->reloc_count;
13296
13297 /* Actually delete the bytes. */
13298 memmove (contents + addr, contents + addr + count,
13299 (size_t) (sec->size - addr - count));
13300 sec->size -= count;
13301
13302 /* Adjust all the relocs. */
13303 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13304 {
13305 /* Get the new reloc address. */
13306 if (irel->r_offset > addr)
13307 irel->r_offset -= count;
13308 }
13309
13310 BFD_ASSERT (addr % 2 == 0);
13311 BFD_ASSERT (count % 2 == 0);
13312
13313 /* Adjust the local symbols defined in this section. */
13314 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13315 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13316 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13317 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13318 isym->st_value -= count;
13319
13320 /* Now adjust the global symbols defined in this section. */
13321 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13322 - symtab_hdr->sh_info);
13323 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13324 end_hashes = sym_hashes + symcount;
13325
13326 for (; sym_hashes < end_hashes; sym_hashes++)
13327 {
13328 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13329
13330 if ((sym_hash->root.type == bfd_link_hash_defined
13331 || sym_hash->root.type == bfd_link_hash_defweak)
13332 && sym_hash->root.u.def.section == sec)
13333 {
13334 bfd_vma value = sym_hash->root.u.def.value;
13335
13336 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13337 value &= MINUS_TWO;
13338 if (value > addr)
13339 sym_hash->root.u.def.value -= count;
13340 }
13341 }
13342
13343 return TRUE;
13344 }
13345
13346
13347 /* Opcodes needed for microMIPS relaxation as found in
13348 opcodes/micromips-opc.c. */
13349
13350 struct opcode_descriptor {
13351 unsigned long match;
13352 unsigned long mask;
13353 };
13354
13355 /* The $ra register aka $31. */
13356
13357 #define RA 31
13358
13359 /* 32-bit instruction format register fields. */
13360
13361 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13362 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13363
13364 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
13365
13366 #define OP16_VALID_REG(r) \
13367 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13368
13369
13370 /* 32-bit and 16-bit branches. */
13371
13372 static const struct opcode_descriptor b_insns_32[] = {
13373 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13374 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13375 { 0, 0 } /* End marker for find_match(). */
13376 };
13377
13378 static const struct opcode_descriptor bc_insn_32 =
13379 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13380
13381 static const struct opcode_descriptor bz_insn_32 =
13382 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13383
13384 static const struct opcode_descriptor bzal_insn_32 =
13385 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13386
13387 static const struct opcode_descriptor beq_insn_32 =
13388 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13389
13390 static const struct opcode_descriptor b_insn_16 =
13391 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13392
13393 static const struct opcode_descriptor bz_insn_16 =
13394 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
13395
13396
13397 /* 32-bit and 16-bit branch EQ and NE zero. */
13398
13399 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13400 eq and second the ne. This convention is used when replacing a
13401 32-bit BEQ/BNE with the 16-bit version. */
13402
13403 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13404
13405 static const struct opcode_descriptor bz_rs_insns_32[] = {
13406 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13407 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13408 { 0, 0 } /* End marker for find_match(). */
13409 };
13410
13411 static const struct opcode_descriptor bz_rt_insns_32[] = {
13412 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13413 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13414 { 0, 0 } /* End marker for find_match(). */
13415 };
13416
13417 static const struct opcode_descriptor bzc_insns_32[] = {
13418 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13419 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13420 { 0, 0 } /* End marker for find_match(). */
13421 };
13422
13423 static const struct opcode_descriptor bz_insns_16[] = {
13424 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13425 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13426 { 0, 0 } /* End marker for find_match(). */
13427 };
13428
13429 /* Switch between a 5-bit register index and its 3-bit shorthand. */
13430
13431 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13432 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13433
13434
13435 /* 32-bit instructions with a delay slot. */
13436
13437 static const struct opcode_descriptor jal_insn_32_bd16 =
13438 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13439
13440 static const struct opcode_descriptor jal_insn_32_bd32 =
13441 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13442
13443 static const struct opcode_descriptor jal_x_insn_32_bd32 =
13444 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13445
13446 static const struct opcode_descriptor j_insn_32 =
13447 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13448
13449 static const struct opcode_descriptor jalr_insn_32 =
13450 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13451
13452 /* This table can be compacted, because no opcode replacement is made. */
13453
13454 static const struct opcode_descriptor ds_insns_32_bd16[] = {
13455 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13456
13457 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13458 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13459
13460 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13461 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13462 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13463 { 0, 0 } /* End marker for find_match(). */
13464 };
13465
13466 /* This table can be compacted, because no opcode replacement is made. */
13467
13468 static const struct opcode_descriptor ds_insns_32_bd32[] = {
13469 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13470
13471 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13472 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13473 { 0, 0 } /* End marker for find_match(). */
13474 };
13475
13476
13477 /* 16-bit instructions with a delay slot. */
13478
13479 static const struct opcode_descriptor jalr_insn_16_bd16 =
13480 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13481
13482 static const struct opcode_descriptor jalr_insn_16_bd32 =
13483 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13484
13485 static const struct opcode_descriptor jr_insn_16 =
13486 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13487
13488 #define JR16_REG(opcode) ((opcode) & 0x1f)
13489
13490 /* This table can be compacted, because no opcode replacement is made. */
13491
13492 static const struct opcode_descriptor ds_insns_16_bd16[] = {
13493 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13494
13495 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13496 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13497 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13498 { 0, 0 } /* End marker for find_match(). */
13499 };
13500
13501
13502 /* LUI instruction. */
13503
13504 static const struct opcode_descriptor lui_insn =
13505 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13506
13507
13508 /* ADDIU instruction. */
13509
13510 static const struct opcode_descriptor addiu_insn =
13511 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13512
13513 static const struct opcode_descriptor addiupc_insn =
13514 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13515
13516 #define ADDIUPC_REG_FIELD(r) \
13517 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13518
13519
13520 /* Relaxable instructions in a JAL delay slot: MOVE. */
13521
13522 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13523 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13524 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13525 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13526
13527 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13528 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13529
13530 static const struct opcode_descriptor move_insns_32[] = {
13531 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
13532 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13533 { 0, 0 } /* End marker for find_match(). */
13534 };
13535
13536 static const struct opcode_descriptor move_insn_16 =
13537 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13538
13539
13540 /* NOP instructions. */
13541
13542 static const struct opcode_descriptor nop_insn_32 =
13543 { /* "nop", "", */ 0x00000000, 0xffffffff };
13544
13545 static const struct opcode_descriptor nop_insn_16 =
13546 { /* "nop", "", */ 0x0c00, 0xffff };
13547
13548
13549 /* Instruction match support. */
13550
13551 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13552
13553 static int
13554 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13555 {
13556 unsigned long indx;
13557
13558 for (indx = 0; insn[indx].mask != 0; indx++)
13559 if (MATCH (opcode, insn[indx]))
13560 return indx;
13561
13562 return -1;
13563 }
13564
13565
13566 /* Branch and delay slot decoding support. */
13567
13568 /* If PTR points to what *might* be a 16-bit branch or jump, then
13569 return the minimum length of its delay slot, otherwise return 0.
13570 Non-zero results are not definitive as we might be checking against
13571 the second half of another instruction. */
13572
13573 static int
13574 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13575 {
13576 unsigned long opcode;
13577 int bdsize;
13578
13579 opcode = bfd_get_16 (abfd, ptr);
13580 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13581 /* 16-bit branch/jump with a 32-bit delay slot. */
13582 bdsize = 4;
13583 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13584 || find_match (opcode, ds_insns_16_bd16) >= 0)
13585 /* 16-bit branch/jump with a 16-bit delay slot. */
13586 bdsize = 2;
13587 else
13588 /* No delay slot. */
13589 bdsize = 0;
13590
13591 return bdsize;
13592 }
13593
13594 /* If PTR points to what *might* be a 32-bit branch or jump, then
13595 return the minimum length of its delay slot, otherwise return 0.
13596 Non-zero results are not definitive as we might be checking against
13597 the second half of another instruction. */
13598
13599 static int
13600 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13601 {
13602 unsigned long opcode;
13603 int bdsize;
13604
13605 opcode = bfd_get_micromips_32 (abfd, ptr);
13606 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13607 /* 32-bit branch/jump with a 32-bit delay slot. */
13608 bdsize = 4;
13609 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13610 /* 32-bit branch/jump with a 16-bit delay slot. */
13611 bdsize = 2;
13612 else
13613 /* No delay slot. */
13614 bdsize = 0;
13615
13616 return bdsize;
13617 }
13618
13619 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13620 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13621
13622 static bfd_boolean
13623 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13624 {
13625 unsigned long opcode;
13626
13627 opcode = bfd_get_16 (abfd, ptr);
13628 if (MATCH (opcode, b_insn_16)
13629 /* B16 */
13630 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13631 /* JR16 */
13632 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13633 /* BEQZ16, BNEZ16 */
13634 || (MATCH (opcode, jalr_insn_16_bd32)
13635 /* JALR16 */
13636 && reg != JR16_REG (opcode) && reg != RA))
13637 return TRUE;
13638
13639 return FALSE;
13640 }
13641
13642 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13643 then return TRUE, otherwise FALSE. */
13644
13645 static bfd_boolean
13646 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13647 {
13648 unsigned long opcode;
13649
13650 opcode = bfd_get_micromips_32 (abfd, ptr);
13651 if (MATCH (opcode, j_insn_32)
13652 /* J */
13653 || MATCH (opcode, bc_insn_32)
13654 /* BC1F, BC1T, BC2F, BC2T */
13655 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13656 /* JAL, JALX */
13657 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13658 /* BGEZ, BGTZ, BLEZ, BLTZ */
13659 || (MATCH (opcode, bzal_insn_32)
13660 /* BGEZAL, BLTZAL */
13661 && reg != OP32_SREG (opcode) && reg != RA)
13662 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13663 /* JALR, JALR.HB, BEQ, BNE */
13664 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13665 return TRUE;
13666
13667 return FALSE;
13668 }
13669
13670 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13671 IRELEND) at OFFSET indicate that there must be a compact branch there,
13672 then return TRUE, otherwise FALSE. */
13673
13674 static bfd_boolean
13675 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13676 const Elf_Internal_Rela *internal_relocs,
13677 const Elf_Internal_Rela *irelend)
13678 {
13679 const Elf_Internal_Rela *irel;
13680 unsigned long opcode;
13681
13682 opcode = bfd_get_micromips_32 (abfd, ptr);
13683 if (find_match (opcode, bzc_insns_32) < 0)
13684 return FALSE;
13685
13686 for (irel = internal_relocs; irel < irelend; irel++)
13687 if (irel->r_offset == offset
13688 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13689 return TRUE;
13690
13691 return FALSE;
13692 }
13693
13694 /* Bitsize checking. */
13695 #define IS_BITSIZE(val, N) \
13696 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13697 - (1ULL << ((N) - 1))) == (val))
13698
13699
13700 bfd_boolean
13702 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13703 struct bfd_link_info *link_info,
13704 bfd_boolean *again)
13705 {
13706 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
13707 Elf_Internal_Shdr *symtab_hdr;
13708 Elf_Internal_Rela *internal_relocs;
13709 Elf_Internal_Rela *irel, *irelend;
13710 bfd_byte *contents = NULL;
13711 Elf_Internal_Sym *isymbuf = NULL;
13712
13713 /* Assume nothing changes. */
13714 *again = FALSE;
13715
13716 /* We don't have to do anything for a relocatable link, if
13717 this section does not have relocs, or if this is not a
13718 code section. */
13719
13720 if (bfd_link_relocatable (link_info)
13721 || (sec->flags & SEC_RELOC) == 0
13722 || sec->reloc_count == 0
13723 || (sec->flags & SEC_CODE) == 0)
13724 return TRUE;
13725
13726 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13727
13728 /* Get a copy of the native relocations. */
13729 internal_relocs = (_bfd_elf_link_read_relocs
13730 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
13731 link_info->keep_memory));
13732 if (internal_relocs == NULL)
13733 goto error_return;
13734
13735 /* Walk through them looking for relaxing opportunities. */
13736 irelend = internal_relocs + sec->reloc_count;
13737 for (irel = internal_relocs; irel < irelend; irel++)
13738 {
13739 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13740 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13741 bfd_boolean target_is_micromips_code_p;
13742 unsigned long opcode;
13743 bfd_vma symval;
13744 bfd_vma pcrval;
13745 bfd_byte *ptr;
13746 int fndopc;
13747
13748 /* The number of bytes to delete for relaxation and from where
13749 to delete these bytes starting at irel->r_offset. */
13750 int delcnt = 0;
13751 int deloff = 0;
13752
13753 /* If this isn't something that can be relaxed, then ignore
13754 this reloc. */
13755 if (r_type != R_MICROMIPS_HI16
13756 && r_type != R_MICROMIPS_PC16_S1
13757 && r_type != R_MICROMIPS_26_S1)
13758 continue;
13759
13760 /* Get the section contents if we haven't done so already. */
13761 if (contents == NULL)
13762 {
13763 /* Get cached copy if it exists. */
13764 if (elf_section_data (sec)->this_hdr.contents != NULL)
13765 contents = elf_section_data (sec)->this_hdr.contents;
13766 /* Go get them off disk. */
13767 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13768 goto error_return;
13769 }
13770 ptr = contents + irel->r_offset;
13771
13772 /* Read this BFD's local symbols if we haven't done so already. */
13773 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13774 {
13775 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13776 if (isymbuf == NULL)
13777 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13778 symtab_hdr->sh_info, 0,
13779 NULL, NULL, NULL);
13780 if (isymbuf == NULL)
13781 goto error_return;
13782 }
13783
13784 /* Get the value of the symbol referred to by the reloc. */
13785 if (r_symndx < symtab_hdr->sh_info)
13786 {
13787 /* A local symbol. */
13788 Elf_Internal_Sym *isym;
13789 asection *sym_sec;
13790
13791 isym = isymbuf + r_symndx;
13792 if (isym->st_shndx == SHN_UNDEF)
13793 sym_sec = bfd_und_section_ptr;
13794 else if (isym->st_shndx == SHN_ABS)
13795 sym_sec = bfd_abs_section_ptr;
13796 else if (isym->st_shndx == SHN_COMMON)
13797 sym_sec = bfd_com_section_ptr;
13798 else
13799 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13800 symval = (isym->st_value
13801 + sym_sec->output_section->vma
13802 + sym_sec->output_offset);
13803 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13804 }
13805 else
13806 {
13807 unsigned long indx;
13808 struct elf_link_hash_entry *h;
13809
13810 /* An external symbol. */
13811 indx = r_symndx - symtab_hdr->sh_info;
13812 h = elf_sym_hashes (abfd)[indx];
13813 BFD_ASSERT (h != NULL);
13814
13815 if (h->root.type != bfd_link_hash_defined
13816 && h->root.type != bfd_link_hash_defweak)
13817 /* This appears to be a reference to an undefined
13818 symbol. Just ignore it -- it will be caught by the
13819 regular reloc processing. */
13820 continue;
13821
13822 symval = (h->root.u.def.value
13823 + h->root.u.def.section->output_section->vma
13824 + h->root.u.def.section->output_offset);
13825 target_is_micromips_code_p = (!h->needs_plt
13826 && ELF_ST_IS_MICROMIPS (h->other));
13827 }
13828
13829
13830 /* For simplicity of coding, we are going to modify the
13831 section contents, the section relocs, and the BFD symbol
13832 table. We must tell the rest of the code not to free up this
13833 information. It would be possible to instead create a table
13834 of changes which have to be made, as is done in coff-mips.c;
13835 that would be more work, but would require less memory when
13836 the linker is run. */
13837
13838 /* Only 32-bit instructions relaxed. */
13839 if (irel->r_offset + 4 > sec->size)
13840 continue;
13841
13842 opcode = bfd_get_micromips_32 (abfd, ptr);
13843
13844 /* This is the pc-relative distance from the instruction the
13845 relocation is applied to, to the symbol referred. */
13846 pcrval = (symval
13847 - (sec->output_section->vma + sec->output_offset)
13848 - irel->r_offset);
13849
13850 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13851 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13852 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13853
13854 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13855
13856 where pcrval has first to be adjusted to apply against the LO16
13857 location (we make the adjustment later on, when we have figured
13858 out the offset). */
13859 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13860 {
13861 bfd_boolean bzc = FALSE;
13862 unsigned long nextopc;
13863 unsigned long reg;
13864 bfd_vma offset;
13865
13866 /* Give up if the previous reloc was a HI16 against this symbol
13867 too. */
13868 if (irel > internal_relocs
13869 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13870 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13871 continue;
13872
13873 /* Or if the next reloc is not a LO16 against this symbol. */
13874 if (irel + 1 >= irelend
13875 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13876 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13877 continue;
13878
13879 /* Or if the second next reloc is a LO16 against this symbol too. */
13880 if (irel + 2 >= irelend
13881 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13882 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13883 continue;
13884
13885 /* See if the LUI instruction *might* be in a branch delay slot.
13886 We check whether what looks like a 16-bit branch or jump is
13887 actually an immediate argument to a compact branch, and let
13888 it through if so. */
13889 if (irel->r_offset >= 2
13890 && check_br16_dslot (abfd, ptr - 2)
13891 && !(irel->r_offset >= 4
13892 && (bzc = check_relocated_bzc (abfd,
13893 ptr - 4, irel->r_offset - 4,
13894 internal_relocs, irelend))))
13895 continue;
13896 if (irel->r_offset >= 4
13897 && !bzc
13898 && check_br32_dslot (abfd, ptr - 4))
13899 continue;
13900
13901 reg = OP32_SREG (opcode);
13902
13903 /* We only relax adjacent instructions or ones separated with
13904 a branch or jump that has a delay slot. The branch or jump
13905 must not fiddle with the register used to hold the address.
13906 Subtract 4 for the LUI itself. */
13907 offset = irel[1].r_offset - irel[0].r_offset;
13908 switch (offset - 4)
13909 {
13910 case 0:
13911 break;
13912 case 2:
13913 if (check_br16 (abfd, ptr + 4, reg))
13914 break;
13915 continue;
13916 case 4:
13917 if (check_br32 (abfd, ptr + 4, reg))
13918 break;
13919 continue;
13920 default:
13921 continue;
13922 }
13923
13924 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
13925
13926 /* Give up unless the same register is used with both
13927 relocations. */
13928 if (OP32_SREG (nextopc) != reg)
13929 continue;
13930
13931 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13932 and rounding up to take masking of the two LSBs into account. */
13933 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13934
13935 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13936 if (IS_BITSIZE (symval, 16))
13937 {
13938 /* Fix the relocation's type. */
13939 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13940
13941 /* Instructions using R_MICROMIPS_LO16 have the base or
13942 source register in bits 20:16. This register becomes $0
13943 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
13944 nextopc &= ~0x001f0000;
13945 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13946 contents + irel[1].r_offset);
13947 }
13948
13949 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13950 We add 4 to take LUI deletion into account while checking
13951 the PC-relative distance. */
13952 else if (symval % 4 == 0
13953 && IS_BITSIZE (pcrval + 4, 25)
13954 && MATCH (nextopc, addiu_insn)
13955 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13956 && OP16_VALID_REG (OP32_TREG (nextopc)))
13957 {
13958 /* Fix the relocation's type. */
13959 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13960
13961 /* Replace ADDIU with the ADDIUPC version. */
13962 nextopc = (addiupc_insn.match
13963 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13964
13965 bfd_put_micromips_32 (abfd, nextopc,
13966 contents + irel[1].r_offset);
13967 }
13968
13969 /* Can't do anything, give up, sigh... */
13970 else
13971 continue;
13972
13973 /* Fix the relocation's type. */
13974 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13975
13976 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13977 delcnt = 4;
13978 deloff = 0;
13979 }
13980
13981 /* Compact branch relaxation -- due to the multitude of macros
13982 employed by the compiler/assembler, compact branches are not
13983 always generated. Obviously, this can/will be fixed elsewhere,
13984 but there is no drawback in double checking it here. */
13985 else if (r_type == R_MICROMIPS_PC16_S1
13986 && irel->r_offset + 5 < sec->size
13987 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13988 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
13989 && ((!insn32
13990 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13991 nop_insn_16) ? 2 : 0))
13992 || (irel->r_offset + 7 < sec->size
13993 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13994 ptr + 4),
13995 nop_insn_32) ? 4 : 0))))
13996 {
13997 unsigned long reg;
13998
13999 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14000
14001 /* Replace BEQZ/BNEZ with the compact version. */
14002 opcode = (bzc_insns_32[fndopc].match
14003 | BZC32_REG_FIELD (reg)
14004 | (opcode & 0xffff)); /* Addend value. */
14005
14006 bfd_put_micromips_32 (abfd, opcode, ptr);
14007
14008 /* Delete the delay slot NOP: two or four bytes from
14009 irel->offset + 4; delcnt has already been set above. */
14010 deloff = 4;
14011 }
14012
14013 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
14014 to check the distance from the next instruction, so subtract 2. */
14015 else if (!insn32
14016 && r_type == R_MICROMIPS_PC16_S1
14017 && IS_BITSIZE (pcrval - 2, 11)
14018 && find_match (opcode, b_insns_32) >= 0)
14019 {
14020 /* Fix the relocation's type. */
14021 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14022
14023 /* Replace the 32-bit opcode with a 16-bit opcode. */
14024 bfd_put_16 (abfd,
14025 (b_insn_16.match
14026 | (opcode & 0x3ff)), /* Addend value. */
14027 ptr);
14028
14029 /* Delete 2 bytes from irel->r_offset + 2. */
14030 delcnt = 2;
14031 deloff = 2;
14032 }
14033
14034 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
14035 to check the distance from the next instruction, so subtract 2. */
14036 else if (!insn32
14037 && r_type == R_MICROMIPS_PC16_S1
14038 && IS_BITSIZE (pcrval - 2, 8)
14039 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14040 && OP16_VALID_REG (OP32_SREG (opcode)))
14041 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14042 && OP16_VALID_REG (OP32_TREG (opcode)))))
14043 {
14044 unsigned long reg;
14045
14046 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14047
14048 /* Fix the relocation's type. */
14049 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14050
14051 /* Replace the 32-bit opcode with a 16-bit opcode. */
14052 bfd_put_16 (abfd,
14053 (bz_insns_16[fndopc].match
14054 | BZ16_REG_FIELD (reg)
14055 | (opcode & 0x7f)), /* Addend value. */
14056 ptr);
14057
14058 /* Delete 2 bytes from irel->r_offset + 2. */
14059 delcnt = 2;
14060 deloff = 2;
14061 }
14062
14063 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
14064 else if (!insn32
14065 && r_type == R_MICROMIPS_26_S1
14066 && target_is_micromips_code_p
14067 && irel->r_offset + 7 < sec->size
14068 && MATCH (opcode, jal_insn_32_bd32))
14069 {
14070 unsigned long n32opc;
14071 bfd_boolean relaxed = FALSE;
14072
14073 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14074
14075 if (MATCH (n32opc, nop_insn_32))
14076 {
14077 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
14078 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14079
14080 relaxed = TRUE;
14081 }
14082 else if (find_match (n32opc, move_insns_32) >= 0)
14083 {
14084 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14085 bfd_put_16 (abfd,
14086 (move_insn_16.match
14087 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14088 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14089 ptr + 4);
14090
14091 relaxed = TRUE;
14092 }
14093 /* Other 32-bit instructions relaxable to 16-bit
14094 instructions will be handled here later. */
14095
14096 if (relaxed)
14097 {
14098 /* JAL with 32-bit delay slot that is changed to a JALS
14099 with 16-bit delay slot. */
14100 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14101
14102 /* Delete 2 bytes from irel->r_offset + 6. */
14103 delcnt = 2;
14104 deloff = 6;
14105 }
14106 }
14107
14108 if (delcnt != 0)
14109 {
14110 /* Note that we've changed the relocs, section contents, etc. */
14111 elf_section_data (sec)->relocs = internal_relocs;
14112 elf_section_data (sec)->this_hdr.contents = contents;
14113 symtab_hdr->contents = (unsigned char *) isymbuf;
14114
14115 /* Delete bytes depending on the delcnt and deloff. */
14116 if (!mips_elf_relax_delete_bytes (abfd, sec,
14117 irel->r_offset + deloff, delcnt))
14118 goto error_return;
14119
14120 /* That will change things, so we should relax again.
14121 Note that this is not required, and it may be slow. */
14122 *again = TRUE;
14123 }
14124 }
14125
14126 if (isymbuf != NULL
14127 && symtab_hdr->contents != (unsigned char *) isymbuf)
14128 {
14129 if (! link_info->keep_memory)
14130 free (isymbuf);
14131 else
14132 {
14133 /* Cache the symbols for elf_link_input_bfd. */
14134 symtab_hdr->contents = (unsigned char *) isymbuf;
14135 }
14136 }
14137
14138 if (contents != NULL
14139 && elf_section_data (sec)->this_hdr.contents != contents)
14140 {
14141 if (! link_info->keep_memory)
14142 free (contents);
14143 else
14144 {
14145 /* Cache the section contents for elf_link_input_bfd. */
14146 elf_section_data (sec)->this_hdr.contents = contents;
14147 }
14148 }
14149
14150 if (internal_relocs != NULL
14151 && elf_section_data (sec)->relocs != internal_relocs)
14152 free (internal_relocs);
14153
14154 return TRUE;
14155
14156 error_return:
14157 if (isymbuf != NULL
14158 && symtab_hdr->contents != (unsigned char *) isymbuf)
14159 free (isymbuf);
14160 if (contents != NULL
14161 && elf_section_data (sec)->this_hdr.contents != contents)
14162 free (contents);
14163 if (internal_relocs != NULL
14164 && elf_section_data (sec)->relocs != internal_relocs)
14165 free (internal_relocs);
14166
14167 return FALSE;
14168 }
14169
14170 /* Create a MIPS ELF linker hash table. */
14172
14173 struct bfd_link_hash_table *
14174 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
14175 {
14176 struct mips_elf_link_hash_table *ret;
14177 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
14178
14179 ret = bfd_zmalloc (amt);
14180 if (ret == NULL)
14181 return NULL;
14182
14183 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14184 mips_elf_link_hash_newfunc,
14185 sizeof (struct mips_elf_link_hash_entry),
14186 MIPS_ELF_DATA))
14187 {
14188 free (ret);
14189 return NULL;
14190 }
14191 ret->root.init_plt_refcount.plist = NULL;
14192 ret->root.init_plt_offset.plist = NULL;
14193
14194 return &ret->root.root;
14195 }
14196
14197 /* Likewise, but indicate that the target is VxWorks. */
14198
14199 struct bfd_link_hash_table *
14200 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14201 {
14202 struct bfd_link_hash_table *ret;
14203
14204 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14205 if (ret)
14206 {
14207 struct mips_elf_link_hash_table *htab;
14208
14209 htab = (struct mips_elf_link_hash_table *) ret;
14210 htab->use_plts_and_copy_relocs = TRUE;
14211 htab->is_vxworks = TRUE;
14212 }
14213 return ret;
14214 }
14215
14216 /* A function that the linker calls if we are allowed to use PLTs
14217 and copy relocs. */
14218
14219 void
14220 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14221 {
14222 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
14223 }
14224
14225 /* A function that the linker calls to select between all or only
14226 32-bit microMIPS instructions, and between making or ignoring
14227 branch relocation checks for invalid transitions between ISA modes.
14228 Also record whether we have been configured for a GNU target. */
14229
14230 void
14231 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bfd_boolean insn32,
14232 bfd_boolean ignore_branch_isa,
14233 bfd_boolean gnu_target)
14234 {
14235 mips_elf_hash_table (info)->insn32 = insn32;
14236 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14237 mips_elf_hash_table (info)->gnu_target = gnu_target;
14238 }
14239
14240 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14242
14243 struct mips_mach_extension
14244 {
14245 unsigned long extension, base;
14246 };
14247
14248
14249 /* An array describing how BFD machines relate to one another. The entries
14250 are ordered topologically with MIPS I extensions listed last. */
14251
14252 static const struct mips_mach_extension mips_mach_extensions[] =
14253 {
14254 /* MIPS64r2 extensions. */
14255 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14256 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14257 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14258 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14259 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14260 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14261 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14262
14263 /* MIPS64 extensions. */
14264 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14265 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14266 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14267
14268 /* MIPS V extensions. */
14269 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14270
14271 /* R10000 extensions. */
14272 { bfd_mach_mips12000, bfd_mach_mips10000 },
14273 { bfd_mach_mips14000, bfd_mach_mips10000 },
14274 { bfd_mach_mips16000, bfd_mach_mips10000 },
14275
14276 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14277 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14278 better to allow vr5400 and vr5500 code to be merged anyway, since
14279 many libraries will just use the core ISA. Perhaps we could add
14280 some sort of ASE flag if this ever proves a problem. */
14281 { bfd_mach_mips5500, bfd_mach_mips5400 },
14282 { bfd_mach_mips5400, bfd_mach_mips5000 },
14283
14284 /* MIPS IV extensions. */
14285 { bfd_mach_mips5, bfd_mach_mips8000 },
14286 { bfd_mach_mips10000, bfd_mach_mips8000 },
14287 { bfd_mach_mips5000, bfd_mach_mips8000 },
14288 { bfd_mach_mips7000, bfd_mach_mips8000 },
14289 { bfd_mach_mips9000, bfd_mach_mips8000 },
14290
14291 /* VR4100 extensions. */
14292 { bfd_mach_mips4120, bfd_mach_mips4100 },
14293 { bfd_mach_mips4111, bfd_mach_mips4100 },
14294
14295 /* MIPS III extensions. */
14296 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14297 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14298 { bfd_mach_mips8000, bfd_mach_mips4000 },
14299 { bfd_mach_mips4650, bfd_mach_mips4000 },
14300 { bfd_mach_mips4600, bfd_mach_mips4000 },
14301 { bfd_mach_mips4400, bfd_mach_mips4000 },
14302 { bfd_mach_mips4300, bfd_mach_mips4000 },
14303 { bfd_mach_mips4100, bfd_mach_mips4000 },
14304 { bfd_mach_mips5900, bfd_mach_mips4000 },
14305
14306 /* MIPS32r3 extensions. */
14307 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14308
14309 /* MIPS32r2 extensions. */
14310 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14311
14312 /* MIPS32 extensions. */
14313 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14314
14315 /* MIPS II extensions. */
14316 { bfd_mach_mips4000, bfd_mach_mips6000 },
14317 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14318 { bfd_mach_mips4010, bfd_mach_mips6000 },
14319
14320 /* MIPS I extensions. */
14321 { bfd_mach_mips6000, bfd_mach_mips3000 },
14322 { bfd_mach_mips3900, bfd_mach_mips3000 }
14323 };
14324
14325 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14326
14327 static bfd_boolean
14328 mips_mach_extends_p (unsigned long base, unsigned long extension)
14329 {
14330 size_t i;
14331
14332 if (extension == base)
14333 return TRUE;
14334
14335 if (base == bfd_mach_mipsisa32
14336 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14337 return TRUE;
14338
14339 if (base == bfd_mach_mipsisa32r2
14340 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14341 return TRUE;
14342
14343 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14344 if (extension == mips_mach_extensions[i].extension)
14345 {
14346 extension = mips_mach_extensions[i].base;
14347 if (extension == base)
14348 return TRUE;
14349 }
14350
14351 return FALSE;
14352 }
14353
14354 /* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14355
14356 static unsigned long
14357 bfd_mips_isa_ext_mach (unsigned int isa_ext)
14358 {
14359 switch (isa_ext)
14360 {
14361 case AFL_EXT_3900: return bfd_mach_mips3900;
14362 case AFL_EXT_4010: return bfd_mach_mips4010;
14363 case AFL_EXT_4100: return bfd_mach_mips4100;
14364 case AFL_EXT_4111: return bfd_mach_mips4111;
14365 case AFL_EXT_4120: return bfd_mach_mips4120;
14366 case AFL_EXT_4650: return bfd_mach_mips4650;
14367 case AFL_EXT_5400: return bfd_mach_mips5400;
14368 case AFL_EXT_5500: return bfd_mach_mips5500;
14369 case AFL_EXT_5900: return bfd_mach_mips5900;
14370 case AFL_EXT_10000: return bfd_mach_mips10000;
14371 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14372 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14373 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
14374 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14375 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14376 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
14377 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14378 default: return bfd_mach_mips3000;
14379 }
14380 }
14381
14382 /* Return the .MIPS.abiflags value representing each ISA Extension. */
14383
14384 unsigned int
14385 bfd_mips_isa_ext (bfd *abfd)
14386 {
14387 switch (bfd_get_mach (abfd))
14388 {
14389 case bfd_mach_mips3900: return AFL_EXT_3900;
14390 case bfd_mach_mips4010: return AFL_EXT_4010;
14391 case bfd_mach_mips4100: return AFL_EXT_4100;
14392 case bfd_mach_mips4111: return AFL_EXT_4111;
14393 case bfd_mach_mips4120: return AFL_EXT_4120;
14394 case bfd_mach_mips4650: return AFL_EXT_4650;
14395 case bfd_mach_mips5400: return AFL_EXT_5400;
14396 case bfd_mach_mips5500: return AFL_EXT_5500;
14397 case bfd_mach_mips5900: return AFL_EXT_5900;
14398 case bfd_mach_mips10000: return AFL_EXT_10000;
14399 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14400 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14401 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14402 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14403 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14404 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14405 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14406 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
14407 case bfd_mach_mips_interaptiv_mr2:
14408 return AFL_EXT_INTERAPTIV_MR2;
14409 default: return 0;
14410 }
14411 }
14412
14413 /* Encode ISA level and revision as a single value. */
14414 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14415
14416 /* Decode a single value into level and revision. */
14417 #define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14418 #define ISA_REV(LEVREV) ((LEVREV) & 0x7)
14419
14420 /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14421
14422 static void
14423 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14424 {
14425 int new_isa = 0;
14426 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14427 {
14428 case E_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14429 case E_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14430 case E_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14431 case E_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14432 case E_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14433 case E_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14434 case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14435 case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14436 case E_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14437 case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14438 case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14439 default:
14440 _bfd_error_handler
14441 /* xgettext:c-format */
14442 (_("%pB: unknown architecture %s"),
14443 abfd, bfd_printable_name (abfd));
14444 }
14445
14446 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14447 {
14448 abiflags->isa_level = ISA_LEVEL (new_isa);
14449 abiflags->isa_rev = ISA_REV (new_isa);
14450 }
14451
14452 /* Update the isa_ext if ABFD describes a further extension. */
14453 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14454 bfd_get_mach (abfd)))
14455 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14456 }
14457
14458 /* Return true if the given ELF header flags describe a 32-bit binary. */
14459
14460 static bfd_boolean
14461 mips_32bit_flags_p (flagword flags)
14462 {
14463 return ((flags & EF_MIPS_32BITMODE) != 0
14464 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14465 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14466 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14467 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14468 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14469 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14470 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
14471 }
14472
14473 /* Infer the content of the ABI flags based on the elf header. */
14474
14475 static void
14476 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14477 {
14478 obj_attribute *in_attr;
14479
14480 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14481 update_mips_abiflags_isa (abfd, abiflags);
14482
14483 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14484 abiflags->gpr_size = AFL_REG_32;
14485 else
14486 abiflags->gpr_size = AFL_REG_64;
14487
14488 abiflags->cpr1_size = AFL_REG_NONE;
14489
14490 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14491 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14492
14493 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14494 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14495 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14496 && abiflags->gpr_size == AFL_REG_32))
14497 abiflags->cpr1_size = AFL_REG_32;
14498 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14499 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14500 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14501 abiflags->cpr1_size = AFL_REG_64;
14502
14503 abiflags->cpr2_size = AFL_REG_NONE;
14504
14505 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14506 abiflags->ases |= AFL_ASE_MDMX;
14507 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14508 abiflags->ases |= AFL_ASE_MIPS16;
14509 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14510 abiflags->ases |= AFL_ASE_MICROMIPS;
14511
14512 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14513 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14514 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14515 && abiflags->isa_level >= 32
14516 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14517 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14518 }
14519
14520 /* We need to use a special link routine to handle the .reginfo and
14521 the .mdebug sections. We need to merge all instances of these
14522 sections together, not write them all out sequentially. */
14523
14524 bfd_boolean
14525 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14526 {
14527 asection *o;
14528 struct bfd_link_order *p;
14529 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14530 asection *rtproc_sec, *abiflags_sec;
14531 Elf32_RegInfo reginfo;
14532 struct ecoff_debug_info debug;
14533 struct mips_htab_traverse_info hti;
14534 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14535 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14536 HDRR *symhdr = &debug.symbolic_header;
14537 void *mdebug_handle = NULL;
14538 asection *s;
14539 EXTR esym;
14540 unsigned int i;
14541 bfd_size_type amt;
14542 struct mips_elf_link_hash_table *htab;
14543
14544 static const char * const secname[] =
14545 {
14546 ".text", ".init", ".fini", ".data",
14547 ".rodata", ".sdata", ".sbss", ".bss"
14548 };
14549 static const int sc[] =
14550 {
14551 scText, scInit, scFini, scData,
14552 scRData, scSData, scSBss, scBss
14553 };
14554
14555 htab = mips_elf_hash_table (info);
14556 BFD_ASSERT (htab != NULL);
14557
14558 /* Sort the dynamic symbols so that those with GOT entries come after
14559 those without. */
14560 if (!mips_elf_sort_hash_table (abfd, info))
14561 return FALSE;
14562
14563 /* Create any scheduled LA25 stubs. */
14564 hti.info = info;
14565 hti.output_bfd = abfd;
14566 hti.error = FALSE;
14567 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14568 if (hti.error)
14569 return FALSE;
14570
14571 /* Get a value for the GP register. */
14572 if (elf_gp (abfd) == 0)
14573 {
14574 struct bfd_link_hash_entry *h;
14575
14576 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
14577 if (h != NULL && h->type == bfd_link_hash_defined)
14578 elf_gp (abfd) = (h->u.def.value
14579 + h->u.def.section->output_section->vma
14580 + h->u.def.section->output_offset);
14581 else if (htab->is_vxworks
14582 && (h = bfd_link_hash_lookup (info->hash,
14583 "_GLOBAL_OFFSET_TABLE_",
14584 FALSE, FALSE, TRUE))
14585 && h->type == bfd_link_hash_defined)
14586 elf_gp (abfd) = (h->u.def.section->output_section->vma
14587 + h->u.def.section->output_offset
14588 + h->u.def.value);
14589 else if (bfd_link_relocatable (info))
14590 {
14591 bfd_vma lo = MINUS_ONE;
14592
14593 /* Find the GP-relative section with the lowest offset. */
14594 for (o = abfd->sections; o != NULL; o = o->next)
14595 if (o->vma < lo
14596 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14597 lo = o->vma;
14598
14599 /* And calculate GP relative to that. */
14600 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14601 }
14602 else
14603 {
14604 /* If the relocate_section function needs to do a reloc
14605 involving the GP value, it should make a reloc_dangerous
14606 callback to warn that GP is not defined. */
14607 }
14608 }
14609
14610 /* Go through the sections and collect the .reginfo and .mdebug
14611 information. */
14612 abiflags_sec = NULL;
14613 reginfo_sec = NULL;
14614 mdebug_sec = NULL;
14615 gptab_data_sec = NULL;
14616 gptab_bss_sec = NULL;
14617 for (o = abfd->sections; o != NULL; o = o->next)
14618 {
14619 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14620 {
14621 /* We have found the .MIPS.abiflags section in the output file.
14622 Look through all the link_orders comprising it and remove them.
14623 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14624 for (p = o->map_head.link_order; p != NULL; p = p->next)
14625 {
14626 asection *input_section;
14627
14628 if (p->type != bfd_indirect_link_order)
14629 {
14630 if (p->type == bfd_data_link_order)
14631 continue;
14632 abort ();
14633 }
14634
14635 input_section = p->u.indirect.section;
14636
14637 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14638 elf_link_input_bfd ignores this section. */
14639 input_section->flags &= ~SEC_HAS_CONTENTS;
14640 }
14641
14642 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14643 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14644
14645 /* Skip this section later on (I don't think this currently
14646 matters, but someday it might). */
14647 o->map_head.link_order = NULL;
14648
14649 abiflags_sec = o;
14650 }
14651
14652 if (strcmp (o->name, ".reginfo") == 0)
14653 {
14654 memset (®info, 0, sizeof reginfo);
14655
14656 /* We have found the .reginfo section in the output file.
14657 Look through all the link_orders comprising it and merge
14658 the information together. */
14659 for (p = o->map_head.link_order; p != NULL; p = p->next)
14660 {
14661 asection *input_section;
14662 bfd *input_bfd;
14663 Elf32_External_RegInfo ext;
14664 Elf32_RegInfo sub;
14665 bfd_size_type sz;
14666
14667 if (p->type != bfd_indirect_link_order)
14668 {
14669 if (p->type == bfd_data_link_order)
14670 continue;
14671 abort ();
14672 }
14673
14674 input_section = p->u.indirect.section;
14675 input_bfd = input_section->owner;
14676
14677 sz = (input_section->size < sizeof (ext)
14678 ? input_section->size : sizeof (ext));
14679 memset (&ext, 0, sizeof (ext));
14680 if (! bfd_get_section_contents (input_bfd, input_section,
14681 &ext, 0, sz))
14682 return FALSE;
14683
14684 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14685
14686 reginfo.ri_gprmask |= sub.ri_gprmask;
14687 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14688 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14689 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14690 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14691
14692 /* ri_gp_value is set by the function
14693 `_bfd_mips_elf_section_processing' when the section is
14694 finally written out. */
14695
14696 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14697 elf_link_input_bfd ignores this section. */
14698 input_section->flags &= ~SEC_HAS_CONTENTS;
14699 }
14700
14701 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14702 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14703
14704 /* Skip this section later on (I don't think this currently
14705 matters, but someday it might). */
14706 o->map_head.link_order = NULL;
14707
14708 reginfo_sec = o;
14709 }
14710
14711 if (strcmp (o->name, ".mdebug") == 0)
14712 {
14713 struct extsym_info einfo;
14714 bfd_vma last;
14715
14716 /* We have found the .mdebug section in the output file.
14717 Look through all the link_orders comprising it and merge
14718 the information together. */
14719 symhdr->magic = swap->sym_magic;
14720 /* FIXME: What should the version stamp be? */
14721 symhdr->vstamp = 0;
14722 symhdr->ilineMax = 0;
14723 symhdr->cbLine = 0;
14724 symhdr->idnMax = 0;
14725 symhdr->ipdMax = 0;
14726 symhdr->isymMax = 0;
14727 symhdr->ioptMax = 0;
14728 symhdr->iauxMax = 0;
14729 symhdr->issMax = 0;
14730 symhdr->issExtMax = 0;
14731 symhdr->ifdMax = 0;
14732 symhdr->crfd = 0;
14733 symhdr->iextMax = 0;
14734
14735 /* We accumulate the debugging information itself in the
14736 debug_info structure. */
14737 debug.line = NULL;
14738 debug.external_dnr = NULL;
14739 debug.external_pdr = NULL;
14740 debug.external_sym = NULL;
14741 debug.external_opt = NULL;
14742 debug.external_aux = NULL;
14743 debug.ss = NULL;
14744 debug.ssext = debug.ssext_end = NULL;
14745 debug.external_fdr = NULL;
14746 debug.external_rfd = NULL;
14747 debug.external_ext = debug.external_ext_end = NULL;
14748
14749 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
14750 if (mdebug_handle == NULL)
14751 return FALSE;
14752
14753 esym.jmptbl = 0;
14754 esym.cobol_main = 0;
14755 esym.weakext = 0;
14756 esym.reserved = 0;
14757 esym.ifd = ifdNil;
14758 esym.asym.iss = issNil;
14759 esym.asym.st = stLocal;
14760 esym.asym.reserved = 0;
14761 esym.asym.index = indexNil;
14762 last = 0;
14763 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
14764 {
14765 esym.asym.sc = sc[i];
14766 s = bfd_get_section_by_name (abfd, secname[i]);
14767 if (s != NULL)
14768 {
14769 esym.asym.value = s->vma;
14770 last = s->vma + s->size;
14771 }
14772 else
14773 esym.asym.value = last;
14774 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
14775 secname[i], &esym))
14776 return FALSE;
14777 }
14778
14779 for (p = o->map_head.link_order; p != NULL; p = p->next)
14780 {
14781 asection *input_section;
14782 bfd *input_bfd;
14783 const struct ecoff_debug_swap *input_swap;
14784 struct ecoff_debug_info input_debug;
14785 char *eraw_src;
14786 char *eraw_end;
14787
14788 if (p->type != bfd_indirect_link_order)
14789 {
14790 if (p->type == bfd_data_link_order)
14791 continue;
14792 abort ();
14793 }
14794
14795 input_section = p->u.indirect.section;
14796 input_bfd = input_section->owner;
14797
14798 if (!is_mips_elf (input_bfd))
14799 {
14800 /* I don't know what a non MIPS ELF bfd would be
14801 doing with a .mdebug section, but I don't really
14802 want to deal with it. */
14803 continue;
14804 }
14805
14806 input_swap = (get_elf_backend_data (input_bfd)
14807 ->elf_backend_ecoff_debug_swap);
14808
14809 BFD_ASSERT (p->size == input_section->size);
14810
14811 /* The ECOFF linking code expects that we have already
14812 read in the debugging information and set up an
14813 ecoff_debug_info structure, so we do that now. */
14814 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
14815 &input_debug))
14816 return FALSE;
14817
14818 if (! (bfd_ecoff_debug_accumulate
14819 (mdebug_handle, abfd, &debug, swap, input_bfd,
14820 &input_debug, input_swap, info)))
14821 return FALSE;
14822
14823 /* Loop through the external symbols. For each one with
14824 interesting information, try to find the symbol in
14825 the linker global hash table and save the information
14826 for the output external symbols. */
14827 eraw_src = input_debug.external_ext;
14828 eraw_end = (eraw_src
14829 + (input_debug.symbolic_header.iextMax
14830 * input_swap->external_ext_size));
14831 for (;
14832 eraw_src < eraw_end;
14833 eraw_src += input_swap->external_ext_size)
14834 {
14835 EXTR ext;
14836 const char *name;
14837 struct mips_elf_link_hash_entry *h;
14838
14839 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
14840 if (ext.asym.sc == scNil
14841 || ext.asym.sc == scUndefined
14842 || ext.asym.sc == scSUndefined)
14843 continue;
14844
14845 name = input_debug.ssext + ext.asym.iss;
14846 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
14847 name, FALSE, FALSE, TRUE);
14848 if (h == NULL || h->esym.ifd != -2)
14849 continue;
14850
14851 if (ext.ifd != -1)
14852 {
14853 BFD_ASSERT (ext.ifd
14854 < input_debug.symbolic_header.ifdMax);
14855 ext.ifd = input_debug.ifdmap[ext.ifd];
14856 }
14857
14858 h->esym = ext;
14859 }
14860
14861 /* Free up the information we just read. */
14862 free (input_debug.line);
14863 free (input_debug.external_dnr);
14864 free (input_debug.external_pdr);
14865 free (input_debug.external_sym);
14866 free (input_debug.external_opt);
14867 free (input_debug.external_aux);
14868 free (input_debug.ss);
14869 free (input_debug.ssext);
14870 free (input_debug.external_fdr);
14871 free (input_debug.external_rfd);
14872 free (input_debug.external_ext);
14873
14874 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14875 elf_link_input_bfd ignores this section. */
14876 input_section->flags &= ~SEC_HAS_CONTENTS;
14877 }
14878
14879 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
14880 {
14881 /* Create .rtproc section. */
14882 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
14883 if (rtproc_sec == NULL)
14884 {
14885 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
14886 | SEC_LINKER_CREATED | SEC_READONLY);
14887
14888 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
14889 ".rtproc",
14890 flags);
14891 if (rtproc_sec == NULL
14892 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
14893 return FALSE;
14894 }
14895
14896 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
14897 info, rtproc_sec,
14898 &debug))
14899 return FALSE;
14900 }
14901
14902 /* Build the external symbol information. */
14903 einfo.abfd = abfd;
14904 einfo.info = info;
14905 einfo.debug = &debug;
14906 einfo.swap = swap;
14907 einfo.failed = FALSE;
14908 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
14909 mips_elf_output_extsym, &einfo);
14910 if (einfo.failed)
14911 return FALSE;
14912
14913 /* Set the size of the .mdebug section. */
14914 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
14915
14916 /* Skip this section later on (I don't think this currently
14917 matters, but someday it might). */
14918 o->map_head.link_order = NULL;
14919
14920 mdebug_sec = o;
14921 }
14922
14923 if (CONST_STRNEQ (o->name, ".gptab."))
14924 {
14925 const char *subname;
14926 unsigned int c;
14927 Elf32_gptab *tab;
14928 Elf32_External_gptab *ext_tab;
14929 unsigned int j;
14930
14931 /* The .gptab.sdata and .gptab.sbss sections hold
14932 information describing how the small data area would
14933 change depending upon the -G switch. These sections
14934 not used in executables files. */
14935 if (! bfd_link_relocatable (info))
14936 {
14937 for (p = o->map_head.link_order; p != NULL; p = p->next)
14938 {
14939 asection *input_section;
14940
14941 if (p->type != bfd_indirect_link_order)
14942 {
14943 if (p->type == bfd_data_link_order)
14944 continue;
14945 abort ();
14946 }
14947
14948 input_section = p->u.indirect.section;
14949
14950 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14951 elf_link_input_bfd ignores this section. */
14952 input_section->flags &= ~SEC_HAS_CONTENTS;
14953 }
14954
14955 /* Skip this section later on (I don't think this
14956 currently matters, but someday it might). */
14957 o->map_head.link_order = NULL;
14958
14959 /* Really remove the section. */
14960 bfd_section_list_remove (abfd, o);
14961 --abfd->section_count;
14962
14963 continue;
14964 }
14965
14966 /* There is one gptab for initialized data, and one for
14967 uninitialized data. */
14968 if (strcmp (o->name, ".gptab.sdata") == 0)
14969 gptab_data_sec = o;
14970 else if (strcmp (o->name, ".gptab.sbss") == 0)
14971 gptab_bss_sec = o;
14972 else
14973 {
14974 _bfd_error_handler
14975 /* xgettext:c-format */
14976 (_("%pB: illegal section name `%pA'"), abfd, o);
14977 bfd_set_error (bfd_error_nonrepresentable_section);
14978 return FALSE;
14979 }
14980
14981 /* The linker script always combines .gptab.data and
14982 .gptab.sdata into .gptab.sdata, and likewise for
14983 .gptab.bss and .gptab.sbss. It is possible that there is
14984 no .sdata or .sbss section in the output file, in which
14985 case we must change the name of the output section. */
14986 subname = o->name + sizeof ".gptab" - 1;
14987 if (bfd_get_section_by_name (abfd, subname) == NULL)
14988 {
14989 if (o == gptab_data_sec)
14990 o->name = ".gptab.data";
14991 else
14992 o->name = ".gptab.bss";
14993 subname = o->name + sizeof ".gptab" - 1;
14994 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
14995 }
14996
14997 /* Set up the first entry. */
14998 c = 1;
14999 amt = c * sizeof (Elf32_gptab);
15000 tab = bfd_malloc (amt);
15001 if (tab == NULL)
15002 return FALSE;
15003 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15004 tab[0].gt_header.gt_unused = 0;
15005
15006 /* Combine the input sections. */
15007 for (p = o->map_head.link_order; p != NULL; p = p->next)
15008 {
15009 asection *input_section;
15010 bfd *input_bfd;
15011 bfd_size_type size;
15012 unsigned long last;
15013 bfd_size_type gpentry;
15014
15015 if (p->type != bfd_indirect_link_order)
15016 {
15017 if (p->type == bfd_data_link_order)
15018 continue;
15019 abort ();
15020 }
15021
15022 input_section = p->u.indirect.section;
15023 input_bfd = input_section->owner;
15024
15025 /* Combine the gptab entries for this input section one
15026 by one. We know that the input gptab entries are
15027 sorted by ascending -G value. */
15028 size = input_section->size;
15029 last = 0;
15030 for (gpentry = sizeof (Elf32_External_gptab);
15031 gpentry < size;
15032 gpentry += sizeof (Elf32_External_gptab))
15033 {
15034 Elf32_External_gptab ext_gptab;
15035 Elf32_gptab int_gptab;
15036 unsigned long val;
15037 unsigned long add;
15038 bfd_boolean exact;
15039 unsigned int look;
15040
15041 if (! (bfd_get_section_contents
15042 (input_bfd, input_section, &ext_gptab, gpentry,
15043 sizeof (Elf32_External_gptab))))
15044 {
15045 free (tab);
15046 return FALSE;
15047 }
15048
15049 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15050 &int_gptab);
15051 val = int_gptab.gt_entry.gt_g_value;
15052 add = int_gptab.gt_entry.gt_bytes - last;
15053
15054 exact = FALSE;
15055 for (look = 1; look < c; look++)
15056 {
15057 if (tab[look].gt_entry.gt_g_value >= val)
15058 tab[look].gt_entry.gt_bytes += add;
15059
15060 if (tab[look].gt_entry.gt_g_value == val)
15061 exact = TRUE;
15062 }
15063
15064 if (! exact)
15065 {
15066 Elf32_gptab *new_tab;
15067 unsigned int max;
15068
15069 /* We need a new table entry. */
15070 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15071 new_tab = bfd_realloc (tab, amt);
15072 if (new_tab == NULL)
15073 {
15074 free (tab);
15075 return FALSE;
15076 }
15077 tab = new_tab;
15078 tab[c].gt_entry.gt_g_value = val;
15079 tab[c].gt_entry.gt_bytes = add;
15080
15081 /* Merge in the size for the next smallest -G
15082 value, since that will be implied by this new
15083 value. */
15084 max = 0;
15085 for (look = 1; look < c; look++)
15086 {
15087 if (tab[look].gt_entry.gt_g_value < val
15088 && (max == 0
15089 || (tab[look].gt_entry.gt_g_value
15090 > tab[max].gt_entry.gt_g_value)))
15091 max = look;
15092 }
15093 if (max != 0)
15094 tab[c].gt_entry.gt_bytes +=
15095 tab[max].gt_entry.gt_bytes;
15096
15097 ++c;
15098 }
15099
15100 last = int_gptab.gt_entry.gt_bytes;
15101 }
15102
15103 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15104 elf_link_input_bfd ignores this section. */
15105 input_section->flags &= ~SEC_HAS_CONTENTS;
15106 }
15107
15108 /* The table must be sorted by -G value. */
15109 if (c > 2)
15110 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15111
15112 /* Swap out the table. */
15113 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15114 ext_tab = bfd_alloc (abfd, amt);
15115 if (ext_tab == NULL)
15116 {
15117 free (tab);
15118 return FALSE;
15119 }
15120
15121 for (j = 0; j < c; j++)
15122 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15123 free (tab);
15124
15125 o->size = c * sizeof (Elf32_External_gptab);
15126 o->contents = (bfd_byte *) ext_tab;
15127
15128 /* Skip this section later on (I don't think this currently
15129 matters, but someday it might). */
15130 o->map_head.link_order = NULL;
15131 }
15132 }
15133
15134 /* Invoke the regular ELF backend linker to do all the work. */
15135 if (!bfd_elf_final_link (abfd, info))
15136 return FALSE;
15137
15138 /* Now write out the computed sections. */
15139
15140 if (abiflags_sec != NULL)
15141 {
15142 Elf_External_ABIFlags_v0 ext;
15143 Elf_Internal_ABIFlags_v0 *abiflags;
15144
15145 abiflags = &mips_elf_tdata (abfd)->abiflags;
15146
15147 /* Set up the abiflags if no valid input sections were found. */
15148 if (!mips_elf_tdata (abfd)->abiflags_valid)
15149 {
15150 infer_mips_abiflags (abfd, abiflags);
15151 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
15152 }
15153 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15154 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15155 return FALSE;
15156 }
15157
15158 if (reginfo_sec != NULL)
15159 {
15160 Elf32_External_RegInfo ext;
15161
15162 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
15163 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15164 return FALSE;
15165 }
15166
15167 if (mdebug_sec != NULL)
15168 {
15169 BFD_ASSERT (abfd->output_has_begun);
15170 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15171 swap, info,
15172 mdebug_sec->filepos))
15173 return FALSE;
15174
15175 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15176 }
15177
15178 if (gptab_data_sec != NULL)
15179 {
15180 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15181 gptab_data_sec->contents,
15182 0, gptab_data_sec->size))
15183 return FALSE;
15184 }
15185
15186 if (gptab_bss_sec != NULL)
15187 {
15188 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15189 gptab_bss_sec->contents,
15190 0, gptab_bss_sec->size))
15191 return FALSE;
15192 }
15193
15194 if (SGI_COMPAT (abfd))
15195 {
15196 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15197 if (rtproc_sec != NULL)
15198 {
15199 if (! bfd_set_section_contents (abfd, rtproc_sec,
15200 rtproc_sec->contents,
15201 0, rtproc_sec->size))
15202 return FALSE;
15203 }
15204 }
15205
15206 return TRUE;
15207 }
15208
15209 /* Merge object file header flags from IBFD into OBFD. Raise an error
15211 if there are conflicting settings. */
15212
15213 static bfd_boolean
15214 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15215 {
15216 bfd *obfd = info->output_bfd;
15217 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15218 flagword old_flags;
15219 flagword new_flags;
15220 bfd_boolean ok;
15221
15222 new_flags = elf_elfheader (ibfd)->e_flags;
15223 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15224 old_flags = elf_elfheader (obfd)->e_flags;
15225
15226 /* Check flag compatibility. */
15227
15228 new_flags &= ~EF_MIPS_NOREORDER;
15229 old_flags &= ~EF_MIPS_NOREORDER;
15230
15231 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15232 doesn't seem to matter. */
15233 new_flags &= ~EF_MIPS_XGOT;
15234 old_flags &= ~EF_MIPS_XGOT;
15235
15236 /* MIPSpro generates ucode info in n64 objects. Again, we should
15237 just be able to ignore this. */
15238 new_flags &= ~EF_MIPS_UCODE;
15239 old_flags &= ~EF_MIPS_UCODE;
15240
15241 /* DSOs should only be linked with CPIC code. */
15242 if ((ibfd->flags & DYNAMIC) != 0)
15243 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15244
15245 if (new_flags == old_flags)
15246 return TRUE;
15247
15248 ok = TRUE;
15249
15250 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15251 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15252 {
15253 _bfd_error_handler
15254 (_("%pB: warning: linking abicalls files with non-abicalls files"),
15255 ibfd);
15256 ok = TRUE;
15257 }
15258
15259 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15260 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15261 if (! (new_flags & EF_MIPS_PIC))
15262 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15263
15264 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15265 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15266
15267 /* Compare the ISAs. */
15268 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15269 {
15270 _bfd_error_handler
15271 (_("%pB: linking 32-bit code with 64-bit code"),
15272 ibfd);
15273 ok = FALSE;
15274 }
15275 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15276 {
15277 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15278 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15279 {
15280 /* Copy the architecture info from IBFD to OBFD. Also copy
15281 the 32-bit flag (if set) so that we continue to recognise
15282 OBFD as a 32-bit binary. */
15283 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15284 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15285 elf_elfheader (obfd)->e_flags
15286 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15287
15288 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15289 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15290
15291 /* Copy across the ABI flags if OBFD doesn't use them
15292 and if that was what caused us to treat IBFD as 32-bit. */
15293 if ((old_flags & EF_MIPS_ABI) == 0
15294 && mips_32bit_flags_p (new_flags)
15295 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15296 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15297 }
15298 else
15299 {
15300 /* The ISAs aren't compatible. */
15301 _bfd_error_handler
15302 /* xgettext:c-format */
15303 (_("%pB: linking %s module with previous %s modules"),
15304 ibfd,
15305 bfd_printable_name (ibfd),
15306 bfd_printable_name (obfd));
15307 ok = FALSE;
15308 }
15309 }
15310
15311 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15312 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15313
15314 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15315 does set EI_CLASS differently from any 32-bit ABI. */
15316 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15317 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15318 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15319 {
15320 /* Only error if both are set (to different values). */
15321 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15322 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15323 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15324 {
15325 _bfd_error_handler
15326 /* xgettext:c-format */
15327 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15328 ibfd,
15329 elf_mips_abi_name (ibfd),
15330 elf_mips_abi_name (obfd));
15331 ok = FALSE;
15332 }
15333 new_flags &= ~EF_MIPS_ABI;
15334 old_flags &= ~EF_MIPS_ABI;
15335 }
15336
15337 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15338 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15339 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15340 {
15341 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15342 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15343 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15344 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15345 int micro_mis = old_m16 && new_micro;
15346 int m16_mis = old_micro && new_m16;
15347
15348 if (m16_mis || micro_mis)
15349 {
15350 _bfd_error_handler
15351 /* xgettext:c-format */
15352 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15353 ibfd,
15354 m16_mis ? "MIPS16" : "microMIPS",
15355 m16_mis ? "microMIPS" : "MIPS16");
15356 ok = FALSE;
15357 }
15358
15359 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15360
15361 new_flags &= ~ EF_MIPS_ARCH_ASE;
15362 old_flags &= ~ EF_MIPS_ARCH_ASE;
15363 }
15364
15365 /* Compare NaN encodings. */
15366 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15367 {
15368 /* xgettext:c-format */
15369 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15370 ibfd,
15371 (new_flags & EF_MIPS_NAN2008
15372 ? "-mnan=2008" : "-mnan=legacy"),
15373 (old_flags & EF_MIPS_NAN2008
15374 ? "-mnan=2008" : "-mnan=legacy"));
15375 ok = FALSE;
15376 new_flags &= ~EF_MIPS_NAN2008;
15377 old_flags &= ~EF_MIPS_NAN2008;
15378 }
15379
15380 /* Compare FP64 state. */
15381 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15382 {
15383 /* xgettext:c-format */
15384 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15385 ibfd,
15386 (new_flags & EF_MIPS_FP64
15387 ? "-mfp64" : "-mfp32"),
15388 (old_flags & EF_MIPS_FP64
15389 ? "-mfp64" : "-mfp32"));
15390 ok = FALSE;
15391 new_flags &= ~EF_MIPS_FP64;
15392 old_flags &= ~EF_MIPS_FP64;
15393 }
15394
15395 /* Warn about any other mismatches */
15396 if (new_flags != old_flags)
15397 {
15398 /* xgettext:c-format */
15399 _bfd_error_handler
15400 (_("%pB: uses different e_flags (%#x) fields than previous modules "
15401 "(%#x)"),
15402 ibfd, new_flags, old_flags);
15403 ok = FALSE;
15404 }
15405
15406 return ok;
15407 }
15408
15409 /* Merge object attributes from IBFD into OBFD. Raise an error if
15410 there are conflicting attributes. */
15411 static bfd_boolean
15412 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15413 {
15414 bfd *obfd = info->output_bfd;
15415 obj_attribute *in_attr;
15416 obj_attribute *out_attr;
15417 bfd *abi_fp_bfd;
15418 bfd *abi_msa_bfd;
15419
15420 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15421 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15422 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15423 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15424
15425 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15426 if (!abi_msa_bfd
15427 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15428 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15429
15430 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15431 {
15432 /* This is the first object. Copy the attributes. */
15433 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15434
15435 /* Use the Tag_null value to indicate the attributes have been
15436 initialized. */
15437 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15438
15439 return TRUE;
15440 }
15441
15442 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15443 non-conflicting ones. */
15444 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15445 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15446 {
15447 int out_fp, in_fp;
15448
15449 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15450 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15451 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15452 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15453 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15454 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15455 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15456 || in_fp == Val_GNU_MIPS_ABI_FP_64
15457 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15458 {
15459 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15460 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15461 }
15462 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15463 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15464 || out_fp == Val_GNU_MIPS_ABI_FP_64
15465 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15466 /* Keep the current setting. */;
15467 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15468 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15469 {
15470 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15471 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15472 }
15473 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15474 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15475 /* Keep the current setting. */;
15476 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15477 {
15478 const char *out_string, *in_string;
15479
15480 out_string = _bfd_mips_fp_abi_string (out_fp);
15481 in_string = _bfd_mips_fp_abi_string (in_fp);
15482 /* First warn about cases involving unrecognised ABIs. */
15483 if (!out_string && !in_string)
15484 /* xgettext:c-format */
15485 _bfd_error_handler
15486 (_("warning: %pB uses unknown floating point ABI %d "
15487 "(set by %pB), %pB uses unknown floating point ABI %d"),
15488 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15489 else if (!out_string)
15490 _bfd_error_handler
15491 /* xgettext:c-format */
15492 (_("warning: %pB uses unknown floating point ABI %d "
15493 "(set by %pB), %pB uses %s"),
15494 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15495 else if (!in_string)
15496 _bfd_error_handler
15497 /* xgettext:c-format */
15498 (_("warning: %pB uses %s (set by %pB), "
15499 "%pB uses unknown floating point ABI %d"),
15500 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15501 else
15502 {
15503 /* If one of the bfds is soft-float, the other must be
15504 hard-float. The exact choice of hard-float ABI isn't
15505 really relevant to the error message. */
15506 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15507 out_string = "-mhard-float";
15508 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15509 in_string = "-mhard-float";
15510 _bfd_error_handler
15511 /* xgettext:c-format */
15512 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15513 obfd, out_string, abi_fp_bfd, ibfd, in_string);
15514 }
15515 }
15516 }
15517
15518 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15519 non-conflicting ones. */
15520 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15521 {
15522 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15523 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15524 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15525 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15526 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15527 {
15528 case Val_GNU_MIPS_ABI_MSA_128:
15529 _bfd_error_handler
15530 /* xgettext:c-format */
15531 (_("warning: %pB uses %s (set by %pB), "
15532 "%pB uses unknown MSA ABI %d"),
15533 obfd, "-mmsa", abi_msa_bfd,
15534 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15535 break;
15536
15537 default:
15538 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15539 {
15540 case Val_GNU_MIPS_ABI_MSA_128:
15541 _bfd_error_handler
15542 /* xgettext:c-format */
15543 (_("warning: %pB uses unknown MSA ABI %d "
15544 "(set by %pB), %pB uses %s"),
15545 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15546 abi_msa_bfd, ibfd, "-mmsa");
15547 break;
15548
15549 default:
15550 _bfd_error_handler
15551 /* xgettext:c-format */
15552 (_("warning: %pB uses unknown MSA ABI %d "
15553 "(set by %pB), %pB uses unknown MSA ABI %d"),
15554 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15555 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15556 break;
15557 }
15558 }
15559 }
15560
15561 /* Merge Tag_compatibility attributes and any common GNU ones. */
15562 return _bfd_elf_merge_object_attributes (ibfd, info);
15563 }
15564
15565 /* Merge object ABI flags from IBFD into OBFD. Raise an error if
15566 there are conflicting settings. */
15567
15568 static bfd_boolean
15569 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15570 {
15571 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15572 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15573 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15574
15575 /* Update the output abiflags fp_abi using the computed fp_abi. */
15576 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15577
15578 #define max(a, b) ((a) > (b) ? (a) : (b))
15579 /* Merge abiflags. */
15580 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15581 in_tdata->abiflags.isa_level);
15582 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15583 in_tdata->abiflags.isa_rev);
15584 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15585 in_tdata->abiflags.gpr_size);
15586 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15587 in_tdata->abiflags.cpr1_size);
15588 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15589 in_tdata->abiflags.cpr2_size);
15590 #undef max
15591 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15592 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15593
15594 return TRUE;
15595 }
15596
15597 /* Merge backend specific data from an object file to the output
15598 object file when linking. */
15599
15600 bfd_boolean
15601 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15602 {
15603 bfd *obfd = info->output_bfd;
15604 struct mips_elf_obj_tdata *out_tdata;
15605 struct mips_elf_obj_tdata *in_tdata;
15606 bfd_boolean null_input_bfd = TRUE;
15607 asection *sec;
15608 bfd_boolean ok;
15609
15610 /* Check if we have the same endianness. */
15611 if (! _bfd_generic_verify_endian_match (ibfd, info))
15612 {
15613 _bfd_error_handler
15614 (_("%pB: endianness incompatible with that of the selected emulation"),
15615 ibfd);
15616 return FALSE;
15617 }
15618
15619 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15620 return TRUE;
15621
15622 in_tdata = mips_elf_tdata (ibfd);
15623 out_tdata = mips_elf_tdata (obfd);
15624
15625 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15626 {
15627 _bfd_error_handler
15628 (_("%pB: ABI is incompatible with that of the selected emulation"),
15629 ibfd);
15630 return FALSE;
15631 }
15632
15633 /* Check to see if the input BFD actually contains any sections. If not,
15634 then it has no attributes, and its flags may not have been initialized
15635 either, but it cannot actually cause any incompatibility. */
15636 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15637 {
15638 /* Ignore synthetic sections and empty .text, .data and .bss sections
15639 which are automatically generated by gas. Also ignore fake
15640 (s)common sections, since merely defining a common symbol does
15641 not affect compatibility. */
15642 if ((sec->flags & SEC_IS_COMMON) == 0
15643 && strcmp (sec->name, ".reginfo")
15644 && strcmp (sec->name, ".mdebug")
15645 && (sec->size != 0
15646 || (strcmp (sec->name, ".text")
15647 && strcmp (sec->name, ".data")
15648 && strcmp (sec->name, ".bss"))))
15649 {
15650 null_input_bfd = FALSE;
15651 break;
15652 }
15653 }
15654 if (null_input_bfd)
15655 return TRUE;
15656
15657 /* Populate abiflags using existing information. */
15658 if (in_tdata->abiflags_valid)
15659 {
15660 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15661 Elf_Internal_ABIFlags_v0 in_abiflags;
15662 Elf_Internal_ABIFlags_v0 abiflags;
15663
15664 /* Set up the FP ABI attribute from the abiflags if it is not already
15665 set. */
15666 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15667 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15668
15669 infer_mips_abiflags (ibfd, &abiflags);
15670 in_abiflags = in_tdata->abiflags;
15671
15672 /* It is not possible to infer the correct ISA revision
15673 for R3 or R5 so drop down to R2 for the checks. */
15674 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15675 in_abiflags.isa_rev = 2;
15676
15677 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15678 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15679 _bfd_error_handler
15680 (_("%pB: warning: inconsistent ISA between e_flags and "
15681 ".MIPS.abiflags"), ibfd);
15682 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15683 && in_abiflags.fp_abi != abiflags.fp_abi)
15684 _bfd_error_handler
15685 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15686 ".MIPS.abiflags"), ibfd);
15687 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15688 _bfd_error_handler
15689 (_("%pB: warning: inconsistent ASEs between e_flags and "
15690 ".MIPS.abiflags"), ibfd);
15691 /* The isa_ext is allowed to be an extension of what can be inferred
15692 from e_flags. */
15693 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15694 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15695 _bfd_error_handler
15696 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15697 ".MIPS.abiflags"), ibfd);
15698 if (in_abiflags.flags2 != 0)
15699 _bfd_error_handler
15700 (_("%pB: warning: unexpected flag in the flags2 field of "
15701 ".MIPS.abiflags (0x%lx)"), ibfd,
15702 in_abiflags.flags2);
15703 }
15704 else
15705 {
15706 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15707 in_tdata->abiflags_valid = TRUE;
15708 }
15709
15710 if (!out_tdata->abiflags_valid)
15711 {
15712 /* Copy input abiflags if output abiflags are not already valid. */
15713 out_tdata->abiflags = in_tdata->abiflags;
15714 out_tdata->abiflags_valid = TRUE;
15715 }
15716
15717 if (! elf_flags_init (obfd))
15718 {
15719 elf_flags_init (obfd) = TRUE;
15720 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
15721 elf_elfheader (obfd)->e_ident[EI_CLASS]
15722 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
15723
15724 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
15725 && (bfd_get_arch_info (obfd)->the_default
15726 || mips_mach_extends_p (bfd_get_mach (obfd),
15727 bfd_get_mach (ibfd))))
15728 {
15729 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
15730 bfd_get_mach (ibfd)))
15731 return FALSE;
15732
15733 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
15734 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15735 }
15736
15737 ok = TRUE;
15738 }
15739 else
15740 ok = mips_elf_merge_obj_e_flags (ibfd, info);
15741
15742 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
15743
15744 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
15745
15746 if (!ok)
15747 {
15748 bfd_set_error (bfd_error_bad_value);
15749 return FALSE;
15750 }
15751
15752 return TRUE;
15753 }
15754
15755 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
15756
15757 bfd_boolean
15758 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
15759 {
15760 BFD_ASSERT (!elf_flags_init (abfd)
15761 || elf_elfheader (abfd)->e_flags == flags);
15762
15763 elf_elfheader (abfd)->e_flags = flags;
15764 elf_flags_init (abfd) = TRUE;
15765 return TRUE;
15766 }
15767
15768 char *
15769 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
15770 {
15771 switch (dtag)
15772 {
15773 default: return "";
15774 case DT_MIPS_RLD_VERSION:
15775 return "MIPS_RLD_VERSION";
15776 case DT_MIPS_TIME_STAMP:
15777 return "MIPS_TIME_STAMP";
15778 case DT_MIPS_ICHECKSUM:
15779 return "MIPS_ICHECKSUM";
15780 case DT_MIPS_IVERSION:
15781 return "MIPS_IVERSION";
15782 case DT_MIPS_FLAGS:
15783 return "MIPS_FLAGS";
15784 case DT_MIPS_BASE_ADDRESS:
15785 return "MIPS_BASE_ADDRESS";
15786 case DT_MIPS_MSYM:
15787 return "MIPS_MSYM";
15788 case DT_MIPS_CONFLICT:
15789 return "MIPS_CONFLICT";
15790 case DT_MIPS_LIBLIST:
15791 return "MIPS_LIBLIST";
15792 case DT_MIPS_LOCAL_GOTNO:
15793 return "MIPS_LOCAL_GOTNO";
15794 case DT_MIPS_CONFLICTNO:
15795 return "MIPS_CONFLICTNO";
15796 case DT_MIPS_LIBLISTNO:
15797 return "MIPS_LIBLISTNO";
15798 case DT_MIPS_SYMTABNO:
15799 return "MIPS_SYMTABNO";
15800 case DT_MIPS_UNREFEXTNO:
15801 return "MIPS_UNREFEXTNO";
15802 case DT_MIPS_GOTSYM:
15803 return "MIPS_GOTSYM";
15804 case DT_MIPS_HIPAGENO:
15805 return "MIPS_HIPAGENO";
15806 case DT_MIPS_RLD_MAP:
15807 return "MIPS_RLD_MAP";
15808 case DT_MIPS_RLD_MAP_REL:
15809 return "MIPS_RLD_MAP_REL";
15810 case DT_MIPS_DELTA_CLASS:
15811 return "MIPS_DELTA_CLASS";
15812 case DT_MIPS_DELTA_CLASS_NO:
15813 return "MIPS_DELTA_CLASS_NO";
15814 case DT_MIPS_DELTA_INSTANCE:
15815 return "MIPS_DELTA_INSTANCE";
15816 case DT_MIPS_DELTA_INSTANCE_NO:
15817 return "MIPS_DELTA_INSTANCE_NO";
15818 case DT_MIPS_DELTA_RELOC:
15819 return "MIPS_DELTA_RELOC";
15820 case DT_MIPS_DELTA_RELOC_NO:
15821 return "MIPS_DELTA_RELOC_NO";
15822 case DT_MIPS_DELTA_SYM:
15823 return "MIPS_DELTA_SYM";
15824 case DT_MIPS_DELTA_SYM_NO:
15825 return "MIPS_DELTA_SYM_NO";
15826 case DT_MIPS_DELTA_CLASSSYM:
15827 return "MIPS_DELTA_CLASSSYM";
15828 case DT_MIPS_DELTA_CLASSSYM_NO:
15829 return "MIPS_DELTA_CLASSSYM_NO";
15830 case DT_MIPS_CXX_FLAGS:
15831 return "MIPS_CXX_FLAGS";
15832 case DT_MIPS_PIXIE_INIT:
15833 return "MIPS_PIXIE_INIT";
15834 case DT_MIPS_SYMBOL_LIB:
15835 return "MIPS_SYMBOL_LIB";
15836 case DT_MIPS_LOCALPAGE_GOTIDX:
15837 return "MIPS_LOCALPAGE_GOTIDX";
15838 case DT_MIPS_LOCAL_GOTIDX:
15839 return "MIPS_LOCAL_GOTIDX";
15840 case DT_MIPS_HIDDEN_GOTIDX:
15841 return "MIPS_HIDDEN_GOTIDX";
15842 case DT_MIPS_PROTECTED_GOTIDX:
15843 return "MIPS_PROTECTED_GOT_IDX";
15844 case DT_MIPS_OPTIONS:
15845 return "MIPS_OPTIONS";
15846 case DT_MIPS_INTERFACE:
15847 return "MIPS_INTERFACE";
15848 case DT_MIPS_DYNSTR_ALIGN:
15849 return "DT_MIPS_DYNSTR_ALIGN";
15850 case DT_MIPS_INTERFACE_SIZE:
15851 return "DT_MIPS_INTERFACE_SIZE";
15852 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
15853 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
15854 case DT_MIPS_PERF_SUFFIX:
15855 return "DT_MIPS_PERF_SUFFIX";
15856 case DT_MIPS_COMPACT_SIZE:
15857 return "DT_MIPS_COMPACT_SIZE";
15858 case DT_MIPS_GP_VALUE:
15859 return "DT_MIPS_GP_VALUE";
15860 case DT_MIPS_AUX_DYNAMIC:
15861 return "DT_MIPS_AUX_DYNAMIC";
15862 case DT_MIPS_PLTGOT:
15863 return "DT_MIPS_PLTGOT";
15864 case DT_MIPS_RWPLT:
15865 return "DT_MIPS_RWPLT";
15866 }
15867 }
15868
15869 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
15870 not known. */
15871
15872 const char *
15873 _bfd_mips_fp_abi_string (int fp)
15874 {
15875 switch (fp)
15876 {
15877 /* These strings aren't translated because they're simply
15878 option lists. */
15879 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15880 return "-mdouble-float";
15881
15882 case Val_GNU_MIPS_ABI_FP_SINGLE:
15883 return "-msingle-float";
15884
15885 case Val_GNU_MIPS_ABI_FP_SOFT:
15886 return "-msoft-float";
15887
15888 case Val_GNU_MIPS_ABI_FP_OLD_64:
15889 return _("-mips32r2 -mfp64 (12 callee-saved)");
15890
15891 case Val_GNU_MIPS_ABI_FP_XX:
15892 return "-mfpxx";
15893
15894 case Val_GNU_MIPS_ABI_FP_64:
15895 return "-mgp32 -mfp64";
15896
15897 case Val_GNU_MIPS_ABI_FP_64A:
15898 return "-mgp32 -mfp64 -mno-odd-spreg";
15899
15900 default:
15901 return 0;
15902 }
15903 }
15904
15905 static void
15906 print_mips_ases (FILE *file, unsigned int mask)
15907 {
15908 if (mask & AFL_ASE_DSP)
15909 fputs ("\n\tDSP ASE", file);
15910 if (mask & AFL_ASE_DSPR2)
15911 fputs ("\n\tDSP R2 ASE", file);
15912 if (mask & AFL_ASE_DSPR3)
15913 fputs ("\n\tDSP R3 ASE", file);
15914 if (mask & AFL_ASE_EVA)
15915 fputs ("\n\tEnhanced VA Scheme", file);
15916 if (mask & AFL_ASE_MCU)
15917 fputs ("\n\tMCU (MicroController) ASE", file);
15918 if (mask & AFL_ASE_MDMX)
15919 fputs ("\n\tMDMX ASE", file);
15920 if (mask & AFL_ASE_MIPS3D)
15921 fputs ("\n\tMIPS-3D ASE", file);
15922 if (mask & AFL_ASE_MT)
15923 fputs ("\n\tMT ASE", file);
15924 if (mask & AFL_ASE_SMARTMIPS)
15925 fputs ("\n\tSmartMIPS ASE", file);
15926 if (mask & AFL_ASE_VIRT)
15927 fputs ("\n\tVZ ASE", file);
15928 if (mask & AFL_ASE_MSA)
15929 fputs ("\n\tMSA ASE", file);
15930 if (mask & AFL_ASE_MIPS16)
15931 fputs ("\n\tMIPS16 ASE", file);
15932 if (mask & AFL_ASE_MICROMIPS)
15933 fputs ("\n\tMICROMIPS ASE", file);
15934 if (mask & AFL_ASE_XPA)
15935 fputs ("\n\tXPA ASE", file);
15936 if (mask & AFL_ASE_MIPS16E2)
15937 fputs ("\n\tMIPS16e2 ASE", file);
15938 if (mask & AFL_ASE_CRC)
15939 fputs ("\n\tCRC ASE", file);
15940 if (mask & AFL_ASE_GINV)
15941 fputs ("\n\tGINV ASE", file);
15942 if (mask & AFL_ASE_LOONGSON_MMI)
15943 fputs ("\n\tLoongson MMI ASE", file);
15944 if (mask & AFL_ASE_LOONGSON_CAM)
15945 fputs ("\n\tLoongson CAM ASE", file);
15946 if (mask & AFL_ASE_LOONGSON_EXT)
15947 fputs ("\n\tLoongson EXT ASE", file);
15948 if (mask & AFL_ASE_LOONGSON_EXT2)
15949 fputs ("\n\tLoongson EXT2 ASE", file);
15950 if (mask == 0)
15951 fprintf (file, "\n\t%s", _("None"));
15952 else if ((mask & ~AFL_ASE_MASK) != 0)
15953 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
15954 }
15955
15956 static void
15957 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
15958 {
15959 switch (isa_ext)
15960 {
15961 case 0:
15962 fputs (_("None"), file);
15963 break;
15964 case AFL_EXT_XLR:
15965 fputs ("RMI XLR", file);
15966 break;
15967 case AFL_EXT_OCTEON3:
15968 fputs ("Cavium Networks Octeon3", file);
15969 break;
15970 case AFL_EXT_OCTEON2:
15971 fputs ("Cavium Networks Octeon2", file);
15972 break;
15973 case AFL_EXT_OCTEONP:
15974 fputs ("Cavium Networks OcteonP", file);
15975 break;
15976 case AFL_EXT_OCTEON:
15977 fputs ("Cavium Networks Octeon", file);
15978 break;
15979 case AFL_EXT_5900:
15980 fputs ("Toshiba R5900", file);
15981 break;
15982 case AFL_EXT_4650:
15983 fputs ("MIPS R4650", file);
15984 break;
15985 case AFL_EXT_4010:
15986 fputs ("LSI R4010", file);
15987 break;
15988 case AFL_EXT_4100:
15989 fputs ("NEC VR4100", file);
15990 break;
15991 case AFL_EXT_3900:
15992 fputs ("Toshiba R3900", file);
15993 break;
15994 case AFL_EXT_10000:
15995 fputs ("MIPS R10000", file);
15996 break;
15997 case AFL_EXT_SB1:
15998 fputs ("Broadcom SB-1", file);
15999 break;
16000 case AFL_EXT_4111:
16001 fputs ("NEC VR4111/VR4181", file);
16002 break;
16003 case AFL_EXT_4120:
16004 fputs ("NEC VR4120", file);
16005 break;
16006 case AFL_EXT_5400:
16007 fputs ("NEC VR5400", file);
16008 break;
16009 case AFL_EXT_5500:
16010 fputs ("NEC VR5500", file);
16011 break;
16012 case AFL_EXT_LOONGSON_2E:
16013 fputs ("ST Microelectronics Loongson 2E", file);
16014 break;
16015 case AFL_EXT_LOONGSON_2F:
16016 fputs ("ST Microelectronics Loongson 2F", file);
16017 break;
16018 case AFL_EXT_INTERAPTIV_MR2:
16019 fputs ("Imagination interAptiv MR2", file);
16020 break;
16021 default:
16022 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16023 break;
16024 }
16025 }
16026
16027 static void
16028 print_mips_fp_abi_value (FILE *file, int val)
16029 {
16030 switch (val)
16031 {
16032 case Val_GNU_MIPS_ABI_FP_ANY:
16033 fprintf (file, _("Hard or soft float\n"));
16034 break;
16035 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16036 fprintf (file, _("Hard float (double precision)\n"));
16037 break;
16038 case Val_GNU_MIPS_ABI_FP_SINGLE:
16039 fprintf (file, _("Hard float (single precision)\n"));
16040 break;
16041 case Val_GNU_MIPS_ABI_FP_SOFT:
16042 fprintf (file, _("Soft float\n"));
16043 break;
16044 case Val_GNU_MIPS_ABI_FP_OLD_64:
16045 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16046 break;
16047 case Val_GNU_MIPS_ABI_FP_XX:
16048 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16049 break;
16050 case Val_GNU_MIPS_ABI_FP_64:
16051 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16052 break;
16053 case Val_GNU_MIPS_ABI_FP_64A:
16054 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16055 break;
16056 default:
16057 fprintf (file, "??? (%d)\n", val);
16058 break;
16059 }
16060 }
16061
16062 static int
16063 get_mips_reg_size (int reg_size)
16064 {
16065 return (reg_size == AFL_REG_NONE) ? 0
16066 : (reg_size == AFL_REG_32) ? 32
16067 : (reg_size == AFL_REG_64) ? 64
16068 : (reg_size == AFL_REG_128) ? 128
16069 : -1;
16070 }
16071
16072 bfd_boolean
16073 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16074 {
16075 FILE *file = ptr;
16076
16077 BFD_ASSERT (abfd != NULL && ptr != NULL);
16078
16079 /* Print normal ELF private data. */
16080 _bfd_elf_print_private_bfd_data (abfd, ptr);
16081
16082 /* xgettext:c-format */
16083 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16084
16085 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16086 fprintf (file, _(" [abi=O32]"));
16087 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16088 fprintf (file, _(" [abi=O64]"));
16089 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16090 fprintf (file, _(" [abi=EABI32]"));
16091 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16092 fprintf (file, _(" [abi=EABI64]"));
16093 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16094 fprintf (file, _(" [abi unknown]"));
16095 else if (ABI_N32_P (abfd))
16096 fprintf (file, _(" [abi=N32]"));
16097 else if (ABI_64_P (abfd))
16098 fprintf (file, _(" [abi=64]"));
16099 else
16100 fprintf (file, _(" [no abi set]"));
16101
16102 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
16103 fprintf (file, " [mips1]");
16104 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
16105 fprintf (file, " [mips2]");
16106 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
16107 fprintf (file, " [mips3]");
16108 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
16109 fprintf (file, " [mips4]");
16110 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
16111 fprintf (file, " [mips5]");
16112 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
16113 fprintf (file, " [mips32]");
16114 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
16115 fprintf (file, " [mips64]");
16116 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
16117 fprintf (file, " [mips32r2]");
16118 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
16119 fprintf (file, " [mips64r2]");
16120 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16121 fprintf (file, " [mips32r6]");
16122 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16123 fprintf (file, " [mips64r6]");
16124 else
16125 fprintf (file, _(" [unknown ISA]"));
16126
16127 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16128 fprintf (file, " [mdmx]");
16129
16130 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16131 fprintf (file, " [mips16]");
16132
16133 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16134 fprintf (file, " [micromips]");
16135
16136 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16137 fprintf (file, " [nan2008]");
16138
16139 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16140 fprintf (file, " [old fp64]");
16141
16142 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16143 fprintf (file, " [32bitmode]");
16144 else
16145 fprintf (file, _(" [not 32bitmode]"));
16146
16147 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16148 fprintf (file, " [noreorder]");
16149
16150 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16151 fprintf (file, " [PIC]");
16152
16153 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16154 fprintf (file, " [CPIC]");
16155
16156 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16157 fprintf (file, " [XGOT]");
16158
16159 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16160 fprintf (file, " [UCODE]");
16161
16162 fputc ('\n', file);
16163
16164 if (mips_elf_tdata (abfd)->abiflags_valid)
16165 {
16166 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16167 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16168 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16169 if (abiflags->isa_rev > 1)
16170 fprintf (file, "r%d", abiflags->isa_rev);
16171 fprintf (file, "\nGPR size: %d",
16172 get_mips_reg_size (abiflags->gpr_size));
16173 fprintf (file, "\nCPR1 size: %d",
16174 get_mips_reg_size (abiflags->cpr1_size));
16175 fprintf (file, "\nCPR2 size: %d",
16176 get_mips_reg_size (abiflags->cpr2_size));
16177 fputs ("\nFP ABI: ", file);
16178 print_mips_fp_abi_value (file, abiflags->fp_abi);
16179 fputs ("ISA Extension: ", file);
16180 print_mips_isa_ext (file, abiflags->isa_ext);
16181 fputs ("\nASEs:", file);
16182 print_mips_ases (file, abiflags->ases);
16183 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16184 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16185 fputc ('\n', file);
16186 }
16187
16188 return TRUE;
16189 }
16190
16191 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16192 {
16193 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16194 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16195 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16196 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16197 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16198 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
16199 { NULL, 0, 0, 0, 0 }
16200 };
16201
16202 /* Merge non visibility st_other attributes. Ensure that the
16203 STO_OPTIONAL flag is copied into h->other, even if this is not a
16204 definiton of the symbol. */
16205 void
16206 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16207 const Elf_Internal_Sym *isym,
16208 bfd_boolean definition,
16209 bfd_boolean dynamic ATTRIBUTE_UNUSED)
16210 {
16211 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16212 {
16213 unsigned char other;
16214
16215 other = (definition ? isym->st_other : h->other);
16216 other &= ~ELF_ST_VISIBILITY (-1);
16217 h->other = other | ELF_ST_VISIBILITY (h->other);
16218 }
16219
16220 if (!definition
16221 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
16222 h->other |= STO_OPTIONAL;
16223 }
16224
16225 /* Decide whether an undefined symbol is special and can be ignored.
16226 This is the case for OPTIONAL symbols on IRIX. */
16227 bfd_boolean
16228 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16229 {
16230 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
16231 }
16232
16233 bfd_boolean
16234 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16235 {
16236 return (sym->st_shndx == SHN_COMMON
16237 || sym->st_shndx == SHN_MIPS_ACOMMON
16238 || sym->st_shndx == SHN_MIPS_SCOMMON);
16239 }
16240
16241 /* Return address for Ith PLT stub in section PLT, for relocation REL
16242 or (bfd_vma) -1 if it should not be included. */
16243
16244 bfd_vma
16245 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16246 const arelent *rel ATTRIBUTE_UNUSED)
16247 {
16248 return (plt->vma
16249 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16250 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16251 }
16252
16253 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16254 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16255 and .got.plt and also the slots may be of a different size each we walk
16256 the PLT manually fetching instructions and matching them against known
16257 patterns. To make things easier standard MIPS slots, if any, always come
16258 first. As we don't create proper ELF symbols we use the UDATA.I member
16259 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16260 with the ST_OTHER member of the ELF symbol. */
16261
16262 long
16263 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16264 long symcount ATTRIBUTE_UNUSED,
16265 asymbol **syms ATTRIBUTE_UNUSED,
16266 long dynsymcount, asymbol **dynsyms,
16267 asymbol **ret)
16268 {
16269 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16270 static const char microsuffix[] = "@micromipsplt";
16271 static const char m16suffix[] = "@mips16plt";
16272 static const char mipssuffix[] = "@plt";
16273
16274 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
16275 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16276 bfd_boolean micromips_p = MICROMIPS_P (abfd);
16277 Elf_Internal_Shdr *hdr;
16278 bfd_byte *plt_data;
16279 bfd_vma plt_offset;
16280 unsigned int other;
16281 bfd_vma entry_size;
16282 bfd_vma plt0_size;
16283 asection *relplt;
16284 bfd_vma opcode;
16285 asection *plt;
16286 asymbol *send;
16287 size_t size;
16288 char *names;
16289 long counti;
16290 arelent *p;
16291 asymbol *s;
16292 char *nend;
16293 long count;
16294 long pi;
16295 long i;
16296 long n;
16297
16298 *ret = NULL;
16299
16300 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16301 return 0;
16302
16303 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16304 if (relplt == NULL)
16305 return 0;
16306
16307 hdr = &elf_section_data (relplt)->this_hdr;
16308 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16309 return 0;
16310
16311 plt = bfd_get_section_by_name (abfd, ".plt");
16312 if (plt == NULL)
16313 return 0;
16314
16315 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16316 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
16317 return -1;
16318 p = relplt->relocation;
16319
16320 /* Calculating the exact amount of space required for symbols would
16321 require two passes over the PLT, so just pessimise assuming two
16322 PLT slots per relocation. */
16323 count = relplt->size / hdr->sh_entsize;
16324 counti = count * bed->s->int_rels_per_ext_rel;
16325 size = 2 * count * sizeof (asymbol);
16326 size += count * (sizeof (mipssuffix) +
16327 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16328 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16329 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16330
16331 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16332 size += sizeof (asymbol) + sizeof (pltname);
16333
16334 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16335 return -1;
16336
16337 if (plt->size < 16)
16338 return -1;
16339
16340 s = *ret = bfd_malloc (size);
16341 if (s == NULL)
16342 return -1;
16343 send = s + 2 * count + 1;
16344
16345 names = (char *) send;
16346 nend = (char *) s + size;
16347 n = 0;
16348
16349 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16350 if (opcode == 0x3302fffe)
16351 {
16352 if (!micromips_p)
16353 return -1;
16354 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16355 other = STO_MICROMIPS;
16356 }
16357 else if (opcode == 0x0398c1d0)
16358 {
16359 if (!micromips_p)
16360 return -1;
16361 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16362 other = STO_MICROMIPS;
16363 }
16364 else
16365 {
16366 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16367 other = 0;
16368 }
16369
16370 s->the_bfd = abfd;
16371 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16372 s->section = plt;
16373 s->value = 0;
16374 s->name = names;
16375 s->udata.i = other;
16376 memcpy (names, pltname, sizeof (pltname));
16377 names += sizeof (pltname);
16378 ++s, ++n;
16379
16380 pi = 0;
16381 for (plt_offset = plt0_size;
16382 plt_offset + 8 <= plt->size && s < send;
16383 plt_offset += entry_size)
16384 {
16385 bfd_vma gotplt_addr;
16386 const char *suffix;
16387 bfd_vma gotplt_hi;
16388 bfd_vma gotplt_lo;
16389 size_t suffixlen;
16390
16391 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16392
16393 /* Check if the second word matches the expected MIPS16 instruction. */
16394 if (opcode == 0x651aeb00)
16395 {
16396 if (micromips_p)
16397 return -1;
16398 /* Truncated table??? */
16399 if (plt_offset + 16 > plt->size)
16400 break;
16401 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16402 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16403 suffixlen = sizeof (m16suffix);
16404 suffix = m16suffix;
16405 other = STO_MIPS16;
16406 }
16407 /* Likewise the expected microMIPS instruction (no insn32 mode). */
16408 else if (opcode == 0xff220000)
16409 {
16410 if (!micromips_p)
16411 return -1;
16412 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16413 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16414 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16415 gotplt_lo <<= 2;
16416 gotplt_addr = gotplt_hi + gotplt_lo;
16417 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16418 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16419 suffixlen = sizeof (microsuffix);
16420 suffix = microsuffix;
16421 other = STO_MICROMIPS;
16422 }
16423 /* Likewise the expected microMIPS instruction (insn32 mode). */
16424 else if ((opcode & 0xffff0000) == 0xff2f0000)
16425 {
16426 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16427 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16428 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16429 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16430 gotplt_addr = gotplt_hi + gotplt_lo;
16431 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16432 suffixlen = sizeof (microsuffix);
16433 suffix = microsuffix;
16434 other = STO_MICROMIPS;
16435 }
16436 /* Otherwise assume standard MIPS code. */
16437 else
16438 {
16439 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16440 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16441 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16442 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16443 gotplt_addr = gotplt_hi + gotplt_lo;
16444 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16445 suffixlen = sizeof (mipssuffix);
16446 suffix = mipssuffix;
16447 other = 0;
16448 }
16449 /* Truncated table??? */
16450 if (plt_offset + entry_size > plt->size)
16451 break;
16452
16453 for (i = 0;
16454 i < count && p[pi].address != gotplt_addr;
16455 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16456
16457 if (i < count)
16458 {
16459 size_t namelen;
16460 size_t len;
16461
16462 *s = **p[pi].sym_ptr_ptr;
16463 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16464 we are defining a symbol, ensure one of them is set. */
16465 if ((s->flags & BSF_LOCAL) == 0)
16466 s->flags |= BSF_GLOBAL;
16467 s->flags |= BSF_SYNTHETIC;
16468 s->section = plt;
16469 s->value = plt_offset;
16470 s->name = names;
16471 s->udata.i = other;
16472
16473 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16474 namelen = len + suffixlen;
16475 if (names + namelen > nend)
16476 break;
16477
16478 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16479 names += len;
16480 memcpy (names, suffix, suffixlen);
16481 names += suffixlen;
16482
16483 ++s, ++n;
16484 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16485 }
16486 }
16487
16488 free (plt_data);
16489
16490 return n;
16491 }
16492
16493 /* Return the ABI flags associated with ABFD if available. */
16494
16495 Elf_Internal_ABIFlags_v0 *
16496 bfd_mips_elf_get_abiflags (bfd *abfd)
16497 {
16498 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16499
16500 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16501 }
16502
16503 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16504 field. Taken from `libc-abis.h' generated at GNU libc build time.
16505 Using a MIPS_ prefix as other libc targets use different values. */
16506 enum
16507 {
16508 MIPS_LIBC_ABI_DEFAULT = 0,
16509 MIPS_LIBC_ABI_MIPS_PLT,
16510 MIPS_LIBC_ABI_UNIQUE,
16511 MIPS_LIBC_ABI_MIPS_O32_FP64,
16512 MIPS_LIBC_ABI_ABSOLUTE,
16513 MIPS_LIBC_ABI_MAX
16514 };
16515
16516 void
16517 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
16518 {
16519 struct mips_elf_link_hash_table *htab = NULL;
16520 Elf_Internal_Ehdr *i_ehdrp;
16521
16522 i_ehdrp = elf_elfheader (abfd);
16523 if (link_info)
16524 {
16525 htab = mips_elf_hash_table (link_info);
16526 BFD_ASSERT (htab != NULL);
16527 }
16528
16529 if (htab != NULL && htab->use_plts_and_copy_relocs && !htab->is_vxworks)
16530 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16531
16532 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16533 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16534 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16535
16536 /* Mark that we need support for absolute symbols in the dynamic loader. */
16537 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16538 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16539
16540 _bfd_elf_post_process_headers (abfd, link_info);
16541 }
16542
16543 int
16544 _bfd_mips_elf_compact_eh_encoding
16545 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16546 {
16547 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16548 }
16549
16550 /* Return the opcode for can't unwind. */
16551
16552 int
16553 _bfd_mips_elf_cant_unwind_opcode
16554 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16555 {
16556 return COMPACT_EH_CANT_UNWIND_OPCODE;
16557 }
16558