]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elfxx-mips.c
Add note about adding ChangeLog.git to src-release.sh
[thirdparty/binutils-gdb.git] / bfd / elfxx-mips.c
1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2023 Free Software Foundation, Inc.
3
4 Most of the information added by Ian Lance Taylor, Cygnus Support,
5 <ian@cygnus.com>.
6 N32/64 ABI support added by Mark Mitchell, CodeSourcery, LLC.
7 <mark@codesourcery.com>
8 Traditional MIPS targets support added by Koundinya.K, Dansk Data
9 Elektronik & Operations Research Group. <kk@ddeorg.soft.net>
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 "ecoff-bfd.h"
37 #include "elfxx-mips.h"
38 #include "elf/mips.h"
39 #include "elf-vxworks.h"
40 #include "dwarf2.h"
41
42 /* Get the ECOFF swapping routines. */
43 #include "coff/sym.h"
44 #include "coff/symconst.h"
45 #include "coff/ecoff.h"
46 #include "coff/mips.h"
47
48 #include "hashtab.h"
49
50 /* Types of TLS GOT entry. */
51 enum mips_got_tls_type {
52 GOT_TLS_NONE,
53 GOT_TLS_GD,
54 GOT_TLS_LDM,
55 GOT_TLS_IE
56 };
57
58 /* This structure is used to hold information about one GOT entry.
59 There are four types of entry:
60
61 (1) an absolute address
62 requires: abfd == NULL
63 fields: d.address
64
65 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
66 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
67 fields: abfd, symndx, d.addend, tls_type
68
69 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
70 requires: abfd != NULL, symndx == -1
71 fields: d.h, tls_type
72
73 (4) a TLS LDM slot
74 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
75 fields: none; there's only one of these per GOT. */
76 struct mips_got_entry
77 {
78 /* One input bfd that needs the GOT entry. */
79 bfd *abfd;
80 /* The index of the symbol, as stored in the relocation r_info, if
81 we have a local symbol; -1 otherwise. */
82 long symndx;
83 union
84 {
85 /* If abfd == NULL, an address that must be stored in the got. */
86 bfd_vma address;
87 /* If abfd != NULL && symndx != -1, the addend of the relocation
88 that should be added to the symbol value. */
89 bfd_vma addend;
90 /* If abfd != NULL && symndx == -1, the hash table entry
91 corresponding to a symbol in the GOT. The symbol's entry
92 is in the local area if h->global_got_area is GGA_NONE,
93 otherwise it is in the global area. */
94 struct mips_elf_link_hash_entry *h;
95 } d;
96
97 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
98 symbol entry with r_symndx == 0. */
99 unsigned char tls_type;
100
101 /* True if we have filled in the GOT contents for a TLS entry,
102 and created the associated relocations. */
103 unsigned char tls_initialized;
104
105 /* The offset from the beginning of the .got section to the entry
106 corresponding to this symbol+addend. If it's a global symbol
107 whose offset is yet to be decided, it's going to be -1. */
108 long gotidx;
109 };
110
111 /* This structure represents a GOT page reference from an input bfd.
112 Each instance represents a symbol + ADDEND, where the representation
113 of the symbol depends on whether it is local to the input bfd.
114 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
115 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
116
117 Page references with SYMNDX >= 0 always become page references
118 in the output. Page references with SYMNDX < 0 only become page
119 references if the symbol binds locally; in other cases, the page
120 reference decays to a global GOT reference. */
121 struct mips_got_page_ref
122 {
123 long symndx;
124 union
125 {
126 struct mips_elf_link_hash_entry *h;
127 bfd *abfd;
128 } u;
129 bfd_vma addend;
130 };
131
132 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
133 The structures form a non-overlapping list that is sorted by increasing
134 MIN_ADDEND. */
135 struct mips_got_page_range
136 {
137 struct mips_got_page_range *next;
138 bfd_signed_vma min_addend;
139 bfd_signed_vma max_addend;
140 };
141
142 /* This structure describes the range of addends that are applied to page
143 relocations against a given section. */
144 struct mips_got_page_entry
145 {
146 /* The section that these entries are based on. */
147 asection *sec;
148 /* The ranges for this page entry. */
149 struct mips_got_page_range *ranges;
150 /* The maximum number of page entries needed for RANGES. */
151 bfd_vma num_pages;
152 };
153
154 /* This structure is used to hold .got information when linking. */
155
156 struct mips_got_info
157 {
158 /* The number of global .got entries. */
159 unsigned int global_gotno;
160 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
161 unsigned int reloc_only_gotno;
162 /* The number of .got slots used for TLS. */
163 unsigned int tls_gotno;
164 /* The first unused TLS .got entry. Used only during
165 mips_elf_initialize_tls_index. */
166 unsigned int tls_assigned_gotno;
167 /* The number of local .got entries, eventually including page entries. */
168 unsigned int local_gotno;
169 /* The maximum number of page entries needed. */
170 unsigned int page_gotno;
171 /* The number of relocations needed for the GOT entries. */
172 unsigned int relocs;
173 /* The first unused local .got entry. */
174 unsigned int assigned_low_gotno;
175 /* The last unused local .got entry. */
176 unsigned int assigned_high_gotno;
177 /* A hash table holding members of the got. */
178 struct htab *got_entries;
179 /* A hash table holding mips_got_page_ref structures. */
180 struct htab *got_page_refs;
181 /* A hash table of mips_got_page_entry structures. */
182 struct htab *got_page_entries;
183 /* In multi-got links, a pointer to the next got (err, rather, most
184 of the time, it points to the previous got). */
185 struct mips_got_info *next;
186 };
187
188 /* Structure passed when merging bfds' gots. */
189
190 struct mips_elf_got_per_bfd_arg
191 {
192 /* The output bfd. */
193 bfd *obfd;
194 /* The link information. */
195 struct bfd_link_info *info;
196 /* A pointer to the primary got, i.e., the one that's going to get
197 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198 DT_MIPS_GOTSYM. */
199 struct mips_got_info *primary;
200 /* A non-primary got we're trying to merge with other input bfd's
201 gots. */
202 struct mips_got_info *current;
203 /* The maximum number of got entries that can be addressed with a
204 16-bit offset. */
205 unsigned int max_count;
206 /* The maximum number of page entries needed by each got. */
207 unsigned int max_pages;
208 /* The total number of global entries which will live in the
209 primary got and be automatically relocated. This includes
210 those not referenced by the primary GOT but included in
211 the "master" GOT. */
212 unsigned int global_count;
213 };
214
215 /* A structure used to pass information to htab_traverse callbacks
216 when laying out the GOT. */
217
218 struct mips_elf_traverse_got_arg
219 {
220 struct bfd_link_info *info;
221 struct mips_got_info *g;
222 int value;
223 };
224
225 struct _mips_elf_section_data
226 {
227 struct bfd_elf_section_data elf;
228 union
229 {
230 bfd_byte *tdata;
231 } u;
232 };
233
234 #define mips_elf_section_data(sec) \
235 ((struct _mips_elf_section_data *) elf_section_data (sec))
236
237 #define is_mips_elf(bfd) \
238 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
239 && elf_tdata (bfd) != NULL \
240 && elf_object_id (bfd) == MIPS_ELF_DATA)
241
242 /* The ABI says that every symbol used by dynamic relocations must have
243 a global GOT entry. Among other things, this provides the dynamic
244 linker with a free, directly-indexed cache. The GOT can therefore
245 contain symbols that are not referenced by GOT relocations themselves
246 (in other words, it may have symbols that are not referenced by things
247 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
248
249 GOT relocations are less likely to overflow if we put the associated
250 GOT entries towards the beginning. We therefore divide the global
251 GOT entries into two areas: "normal" and "reloc-only". Entries in
252 the first area can be used for both dynamic relocations and GP-relative
253 accesses, while those in the "reloc-only" area are for dynamic
254 relocations only.
255
256 These GGA_* ("Global GOT Area") values are organised so that lower
257 values are more general than higher values. Also, non-GGA_NONE
258 values are ordered by the position of the area in the GOT. */
259 #define GGA_NORMAL 0
260 #define GGA_RELOC_ONLY 1
261 #define GGA_NONE 2
262
263 /* Information about a non-PIC interface to a PIC function. There are
264 two ways of creating these interfaces. The first is to add:
265
266 lui $25,%hi(func)
267 addiu $25,$25,%lo(func)
268
269 immediately before a PIC function "func". The second is to add:
270
271 lui $25,%hi(func)
272 j func
273 addiu $25,$25,%lo(func)
274
275 to a separate trampoline section.
276
277 Stubs of the first kind go in a new section immediately before the
278 target function. Stubs of the second kind go in a single section
279 pointed to by the hash table's "strampoline" field. */
280 struct mips_elf_la25_stub {
281 /* The generated section that contains this stub. */
282 asection *stub_section;
283
284 /* The offset of the stub from the start of STUB_SECTION. */
285 bfd_vma offset;
286
287 /* One symbol for the original function. Its location is available
288 in H->root.root.u.def. */
289 struct mips_elf_link_hash_entry *h;
290 };
291
292 /* Macros for populating a mips_elf_la25_stub. */
293
294 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
295 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
296 #define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
297 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
298 #define LA25_LUI_MICROMIPS(VAL) \
299 (0x41b90000 | (VAL)) /* lui t9,VAL */
300 #define LA25_J_MICROMIPS(VAL) \
301 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
302 #define LA25_ADDIU_MICROMIPS(VAL) \
303 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
304
305 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
306 the dynamic symbols. */
307
308 struct mips_elf_hash_sort_data
309 {
310 /* The symbol in the global GOT with the lowest dynamic symbol table
311 index. */
312 struct elf_link_hash_entry *low;
313 /* The least dynamic symbol table index corresponding to a non-TLS
314 symbol with a GOT entry. */
315 bfd_size_type min_got_dynindx;
316 /* The greatest dynamic symbol table index corresponding to a symbol
317 with a GOT entry that is not referenced (e.g., a dynamic symbol
318 with dynamic relocations pointing to it from non-primary GOTs). */
319 bfd_size_type max_unref_got_dynindx;
320 /* The greatest dynamic symbol table index corresponding to a local
321 symbol. */
322 bfd_size_type max_local_dynindx;
323 /* The greatest dynamic symbol table index corresponding to an external
324 symbol without a GOT entry. */
325 bfd_size_type max_non_got_dynindx;
326 /* If non-NULL, output BFD for .MIPS.xhash finalization. */
327 bfd *output_bfd;
328 /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
329 real final dynindx. */
330 bfd_byte *mipsxhash;
331 };
332
333 /* We make up to two PLT entries if needed, one for standard MIPS code
334 and one for compressed code, either a MIPS16 or microMIPS one. We
335 keep a separate record of traditional lazy-binding stubs, for easier
336 processing. */
337
338 struct plt_entry
339 {
340 /* Traditional SVR4 stub offset, or -1 if none. */
341 bfd_vma stub_offset;
342
343 /* Standard PLT entry offset, or -1 if none. */
344 bfd_vma mips_offset;
345
346 /* Compressed PLT entry offset, or -1 if none. */
347 bfd_vma comp_offset;
348
349 /* The corresponding .got.plt index, or -1 if none. */
350 bfd_vma gotplt_index;
351
352 /* Whether we need a standard PLT entry. */
353 unsigned int need_mips : 1;
354
355 /* Whether we need a compressed PLT entry. */
356 unsigned int need_comp : 1;
357 };
358
359 /* The MIPS ELF linker needs additional information for each symbol in
360 the global hash table. */
361
362 struct mips_elf_link_hash_entry
363 {
364 struct elf_link_hash_entry root;
365
366 /* External symbol information. */
367 EXTR esym;
368
369 /* The la25 stub we have created for ths symbol, if any. */
370 struct mips_elf_la25_stub *la25_stub;
371
372 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
373 this symbol. */
374 unsigned int possibly_dynamic_relocs;
375
376 /* If there is a stub that 32 bit functions should use to call this
377 16 bit function, this points to the section containing the stub. */
378 asection *fn_stub;
379
380 /* If there is a stub that 16 bit functions should use to call this
381 32 bit function, this points to the section containing the stub. */
382 asection *call_stub;
383
384 /* This is like the call_stub field, but it is used if the function
385 being called returns a floating point value. */
386 asection *call_fp_stub;
387
388 /* If non-zero, location in .MIPS.xhash to write real final dynindx. */
389 bfd_vma mipsxhash_loc;
390
391 /* The highest GGA_* value that satisfies all references to this symbol. */
392 unsigned int global_got_area : 2;
393
394 /* True if all GOT relocations against this symbol are for calls. This is
395 a looser condition than no_fn_stub below, because there may be other
396 non-call non-GOT relocations against the symbol. */
397 unsigned int got_only_for_calls : 1;
398
399 /* True if one of the relocations described by possibly_dynamic_relocs
400 is against a readonly section. */
401 unsigned int readonly_reloc : 1;
402
403 /* True if there is a relocation against this symbol that must be
404 resolved by the static linker (in other words, if the relocation
405 cannot possibly be made dynamic). */
406 unsigned int has_static_relocs : 1;
407
408 /* True if we must not create a .MIPS.stubs entry for this symbol.
409 This is set, for example, if there are relocations related to
410 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
411 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
412 unsigned int no_fn_stub : 1;
413
414 /* Whether we need the fn_stub; this is true if this symbol appears
415 in any relocs other than a 16 bit call. */
416 unsigned int need_fn_stub : 1;
417
418 /* True if this symbol is referenced by branch relocations from
419 any non-PIC input file. This is used to determine whether an
420 la25 stub is required. */
421 unsigned int has_nonpic_branches : 1;
422
423 /* Does this symbol need a traditional MIPS lazy-binding stub
424 (as opposed to a PLT entry)? */
425 unsigned int needs_lazy_stub : 1;
426
427 /* Does this symbol resolve to a PLT entry? */
428 unsigned int use_plt_entry : 1;
429 };
430
431 /* MIPS ELF linker hash table. */
432
433 struct mips_elf_link_hash_table
434 {
435 struct elf_link_hash_table root;
436
437 /* The number of .rtproc entries. */
438 bfd_size_type procedure_count;
439
440 /* The size of the .compact_rel section (if SGI_COMPAT). */
441 bfd_size_type compact_rel_size;
442
443 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
444 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
445 bool use_rld_obj_head;
446
447 /* The __rld_map or __rld_obj_head symbol. */
448 struct elf_link_hash_entry *rld_symbol;
449
450 /* This is set if we see any mips16 stub sections. */
451 bool mips16_stubs_seen;
452
453 /* True if we can generate copy relocs and PLTs. */
454 bool use_plts_and_copy_relocs;
455
456 /* True if we can only use 32-bit microMIPS instructions. */
457 bool insn32;
458
459 /* True if we suppress checks for invalid branches between ISA modes. */
460 bool ignore_branch_isa;
461
462 /* True if we are targetting R6 compact branches. */
463 bool compact_branches;
464
465 /* True if we already reported the small-data section overflow. */
466 bool small_data_overflow_reported;
467
468 /* True if we use the special `__gnu_absolute_zero' symbol. */
469 bool use_absolute_zero;
470
471 /* True if we have been configured for a GNU target. */
472 bool gnu_target;
473
474 /* Shortcuts to some dynamic sections, or NULL if they are not
475 being used. */
476 asection *srelplt2;
477 asection *sstubs;
478
479 /* The master GOT information. */
480 struct mips_got_info *got_info;
481
482 /* The global symbol in the GOT with the lowest index in the dynamic
483 symbol table. */
484 struct elf_link_hash_entry *global_gotsym;
485
486 /* The size of the PLT header in bytes. */
487 bfd_vma plt_header_size;
488
489 /* The size of a standard PLT entry in bytes. */
490 bfd_vma plt_mips_entry_size;
491
492 /* The size of a compressed PLT entry in bytes. */
493 bfd_vma plt_comp_entry_size;
494
495 /* The offset of the next standard PLT entry to create. */
496 bfd_vma plt_mips_offset;
497
498 /* The offset of the next compressed PLT entry to create. */
499 bfd_vma plt_comp_offset;
500
501 /* The index of the next .got.plt entry to create. */
502 bfd_vma plt_got_index;
503
504 /* The number of functions that need a lazy-binding stub. */
505 bfd_vma lazy_stub_count;
506
507 /* The size of a function stub entry in bytes. */
508 bfd_vma function_stub_size;
509
510 /* The number of reserved entries at the beginning of the GOT. */
511 unsigned int reserved_gotno;
512
513 /* The section used for mips_elf_la25_stub trampolines.
514 See the comment above that structure for details. */
515 asection *strampoline;
516
517 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
518 pairs. */
519 htab_t la25_stubs;
520
521 /* A function FN (NAME, IS, OS) that creates a new input section
522 called NAME and links it to output section OS. If IS is nonnull,
523 the new section should go immediately before it, otherwise it
524 should go at the (current) beginning of OS.
525
526 The function returns the new section on success, otherwise it
527 returns null. */
528 asection *(*add_stub_section) (const char *, asection *, asection *);
529
530 /* Is the PLT header compressed? */
531 unsigned int plt_header_is_comp : 1;
532 };
533
534 /* Get the MIPS ELF linker hash table from a link_info structure. */
535
536 #define mips_elf_hash_table(p) \
537 ((is_elf_hash_table ((p)->hash) \
538 && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA) \
539 ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
540
541 /* A structure used to communicate with htab_traverse callbacks. */
542 struct mips_htab_traverse_info
543 {
544 /* The usual link-wide information. */
545 struct bfd_link_info *info;
546 bfd *output_bfd;
547
548 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
549 bool error;
550 };
551
552 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
553 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
554 that contains the relocation field and DATA points to the start of
555 INPUT_SECTION. */
556
557 struct mips_hi16
558 {
559 struct mips_hi16 *next;
560 bfd_byte *data;
561 asection *input_section;
562 arelent rel;
563 };
564
565 /* MIPS ELF private object data. */
566
567 struct mips_elf_obj_tdata
568 {
569 /* Generic ELF private object data. */
570 struct elf_obj_tdata root;
571
572 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
573 bfd *abi_fp_bfd;
574
575 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
576 bfd *abi_msa_bfd;
577
578 /* The abiflags for this object. */
579 Elf_Internal_ABIFlags_v0 abiflags;
580 bool abiflags_valid;
581
582 /* The GOT requirements of input bfds. */
583 struct mips_got_info *got;
584
585 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
586 included directly in this one, but there's no point to wasting
587 the memory just for the infrequently called find_nearest_line. */
588 struct mips_elf_find_line *find_line_info;
589
590 /* An array of stub sections indexed by symbol number. */
591 asection **local_stubs;
592 asection **local_call_stubs;
593
594 /* The Irix 5 support uses two virtual sections, which represent
595 text/data symbols defined in dynamic objects. */
596 asymbol *elf_data_symbol;
597 asymbol *elf_text_symbol;
598 asection *elf_data_section;
599 asection *elf_text_section;
600
601 struct mips_hi16 *mips_hi16_list;
602 };
603
604 /* Get MIPS ELF private object data from BFD's tdata. */
605
606 #define mips_elf_tdata(bfd) \
607 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
608
609 #define TLS_RELOC_P(r_type) \
610 (r_type == R_MIPS_TLS_DTPMOD32 \
611 || r_type == R_MIPS_TLS_DTPMOD64 \
612 || r_type == R_MIPS_TLS_DTPREL32 \
613 || r_type == R_MIPS_TLS_DTPREL64 \
614 || r_type == R_MIPS_TLS_GD \
615 || r_type == R_MIPS_TLS_LDM \
616 || r_type == R_MIPS_TLS_DTPREL_HI16 \
617 || r_type == R_MIPS_TLS_DTPREL_LO16 \
618 || r_type == R_MIPS_TLS_GOTTPREL \
619 || r_type == R_MIPS_TLS_TPREL32 \
620 || r_type == R_MIPS_TLS_TPREL64 \
621 || r_type == R_MIPS_TLS_TPREL_HI16 \
622 || r_type == R_MIPS_TLS_TPREL_LO16 \
623 || r_type == R_MIPS16_TLS_GD \
624 || r_type == R_MIPS16_TLS_LDM \
625 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
626 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
627 || r_type == R_MIPS16_TLS_GOTTPREL \
628 || r_type == R_MIPS16_TLS_TPREL_HI16 \
629 || r_type == R_MIPS16_TLS_TPREL_LO16 \
630 || r_type == R_MICROMIPS_TLS_GD \
631 || r_type == R_MICROMIPS_TLS_LDM \
632 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
633 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
634 || r_type == R_MICROMIPS_TLS_GOTTPREL \
635 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
636 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
637
638 /* Structure used to pass information to mips_elf_output_extsym. */
639
640 struct extsym_info
641 {
642 bfd *abfd;
643 struct bfd_link_info *info;
644 struct ecoff_debug_info *debug;
645 const struct ecoff_debug_swap *swap;
646 bool failed;
647 };
648
649 /* The names of the runtime procedure table symbols used on IRIX5. */
650
651 static const char * const mips_elf_dynsym_rtproc_names[] =
652 {
653 "_procedure_table",
654 "_procedure_string_table",
655 "_procedure_table_size",
656 NULL
657 };
658
659 /* These structures are used to generate the .compact_rel section on
660 IRIX5. */
661
662 typedef struct
663 {
664 unsigned long id1; /* Always one? */
665 unsigned long num; /* Number of compact relocation entries. */
666 unsigned long id2; /* Always two? */
667 unsigned long offset; /* The file offset of the first relocation. */
668 unsigned long reserved0; /* Zero? */
669 unsigned long reserved1; /* Zero? */
670 } Elf32_compact_rel;
671
672 typedef struct
673 {
674 bfd_byte id1[4];
675 bfd_byte num[4];
676 bfd_byte id2[4];
677 bfd_byte offset[4];
678 bfd_byte reserved0[4];
679 bfd_byte reserved1[4];
680 } Elf32_External_compact_rel;
681
682 typedef struct
683 {
684 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
685 unsigned int rtype : 4; /* Relocation types. See below. */
686 unsigned int dist2to : 8;
687 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
688 unsigned long konst; /* KONST field. See below. */
689 unsigned long vaddr; /* VADDR to be relocated. */
690 } Elf32_crinfo;
691
692 typedef struct
693 {
694 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
695 unsigned int rtype : 4; /* Relocation types. See below. */
696 unsigned int dist2to : 8;
697 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
698 unsigned long konst; /* KONST field. See below. */
699 } Elf32_crinfo2;
700
701 typedef struct
702 {
703 bfd_byte info[4];
704 bfd_byte konst[4];
705 bfd_byte vaddr[4];
706 } Elf32_External_crinfo;
707
708 typedef struct
709 {
710 bfd_byte info[4];
711 bfd_byte konst[4];
712 } Elf32_External_crinfo2;
713
714 /* These are the constants used to swap the bitfields in a crinfo. */
715
716 #define CRINFO_CTYPE (0x1U)
717 #define CRINFO_CTYPE_SH (31)
718 #define CRINFO_RTYPE (0xfU)
719 #define CRINFO_RTYPE_SH (27)
720 #define CRINFO_DIST2TO (0xffU)
721 #define CRINFO_DIST2TO_SH (19)
722 #define CRINFO_RELVADDR (0x7ffffU)
723 #define CRINFO_RELVADDR_SH (0)
724
725 /* A compact relocation info has long (3 words) or short (2 words)
726 formats. A short format doesn't have VADDR field and relvaddr
727 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
728 #define CRF_MIPS_LONG 1
729 #define CRF_MIPS_SHORT 0
730
731 /* There are 4 types of compact relocation at least. The value KONST
732 has different meaning for each type:
733
734 (type) (konst)
735 CT_MIPS_REL32 Address in data
736 CT_MIPS_WORD Address in word (XXX)
737 CT_MIPS_GPHI_LO GP - vaddr
738 CT_MIPS_JMPAD Address to jump
739 */
740
741 #define CRT_MIPS_REL32 0xa
742 #define CRT_MIPS_WORD 0xb
743 #define CRT_MIPS_GPHI_LO 0xc
744 #define CRT_MIPS_JMPAD 0xd
745
746 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
747 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
748 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
749 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
750 \f
751 /* The structure of the runtime procedure descriptor created by the
752 loader for use by the static exception system. */
753
754 typedef struct runtime_pdr {
755 bfd_vma adr; /* Memory address of start of procedure. */
756 long regmask; /* Save register mask. */
757 long regoffset; /* Save register offset. */
758 long fregmask; /* Save floating point register mask. */
759 long fregoffset; /* Save floating point register offset. */
760 long frameoffset; /* Frame size. */
761 short framereg; /* Frame pointer register. */
762 short pcreg; /* Offset or reg of return pc. */
763 long irpss; /* Index into the runtime string table. */
764 long reserved;
765 struct exception_info *exception_info;/* Pointer to exception array. */
766 } RPDR, *pRPDR;
767 #define cbRPDR sizeof (RPDR)
768 #define rpdNil ((pRPDR) 0)
769 \f
770 static struct mips_got_entry *mips_elf_create_local_got_entry
771 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
772 struct mips_elf_link_hash_entry *, int);
773 static bool mips_elf_sort_hash_table_f
774 (struct mips_elf_link_hash_entry *, void *);
775 static bfd_vma mips_elf_high
776 (bfd_vma);
777 static bool mips_elf_create_dynamic_relocation
778 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
779 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
780 bfd_vma *, asection *);
781 static bfd_vma mips_elf_adjust_gp
782 (bfd *, struct mips_got_info *, bfd *);
783
784 /* This will be used when we sort the dynamic relocation records. */
785 static bfd *reldyn_sorting_bfd;
786
787 /* True if ABFD is for CPUs with load interlocking that include
788 non-MIPS1 CPUs and R3900. */
789 #define LOAD_INTERLOCKS_P(abfd) \
790 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
791 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
792
793 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
794 This should be safe for all architectures. We enable this predicate
795 for RM9000 for now. */
796 #define JAL_TO_BAL_P(abfd) \
797 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
798
799 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
800 This should be safe for all architectures. We enable this predicate for
801 all CPUs. */
802 #define JALR_TO_BAL_P(abfd) 1
803
804 /* True if ABFD is for CPUs that are faster if JR is converted to B.
805 This should be safe for all architectures. We enable this predicate for
806 all CPUs. */
807 #define JR_TO_B_P(abfd) 1
808
809 /* True if ABFD is a PIC object. */
810 #define PIC_OBJECT_P(abfd) \
811 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
812
813 /* Nonzero if ABFD is using the O32 ABI. */
814 #define ABI_O32_P(abfd) \
815 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
816
817 /* Nonzero if ABFD is using the N32 ABI. */
818 #define ABI_N32_P(abfd) \
819 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
820
821 /* Nonzero if ABFD is using the N64 ABI. */
822 #define ABI_64_P(abfd) \
823 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
824
825 /* Nonzero if ABFD is using NewABI conventions. */
826 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
827
828 /* Nonzero if ABFD has microMIPS code. */
829 #define MICROMIPS_P(abfd) \
830 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
831
832 /* Nonzero if ABFD is MIPS R6. */
833 #define MIPSR6_P(abfd) \
834 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
835 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
836
837 /* The IRIX compatibility level we are striving for. */
838 #define IRIX_COMPAT(abfd) \
839 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
840
841 /* Whether we are trying to be compatible with IRIX at all. */
842 #define SGI_COMPAT(abfd) \
843 (IRIX_COMPAT (abfd) != ict_none)
844
845 /* The name of the options section. */
846 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
847 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
848
849 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
850 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
851 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
852 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
853
854 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
855 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
856 (strcmp (NAME, ".MIPS.abiflags") == 0)
857
858 /* Whether the section is readonly. */
859 #define MIPS_ELF_READONLY_SECTION(sec) \
860 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
861 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
862
863 /* The name of the stub section. */
864 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
865
866 /* The size of an external REL relocation. */
867 #define MIPS_ELF_REL_SIZE(abfd) \
868 (get_elf_backend_data (abfd)->s->sizeof_rel)
869
870 /* The size of an external RELA relocation. */
871 #define MIPS_ELF_RELA_SIZE(abfd) \
872 (get_elf_backend_data (abfd)->s->sizeof_rela)
873
874 /* The size of an external dynamic table entry. */
875 #define MIPS_ELF_DYN_SIZE(abfd) \
876 (get_elf_backend_data (abfd)->s->sizeof_dyn)
877
878 /* The size of a GOT entry. */
879 #define MIPS_ELF_GOT_SIZE(abfd) \
880 (get_elf_backend_data (abfd)->s->arch_size / 8)
881
882 /* The size of the .rld_map section. */
883 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
884 (get_elf_backend_data (abfd)->s->arch_size / 8)
885
886 /* The size of a symbol-table entry. */
887 #define MIPS_ELF_SYM_SIZE(abfd) \
888 (get_elf_backend_data (abfd)->s->sizeof_sym)
889
890 /* The default alignment for sections, as a power of two. */
891 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
892 (get_elf_backend_data (abfd)->s->log_file_align)
893
894 /* Get word-sized data. */
895 #define MIPS_ELF_GET_WORD(abfd, ptr) \
896 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
897
898 /* Put out word-sized data. */
899 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
900 (ABI_64_P (abfd) \
901 ? bfd_put_64 (abfd, val, ptr) \
902 : bfd_put_32 (abfd, val, ptr))
903
904 /* The opcode for word-sized loads (LW or LD). */
905 #define MIPS_ELF_LOAD_WORD(abfd) \
906 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
907
908 /* Add a dynamic symbol table-entry. */
909 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
910 _bfd_elf_add_dynamic_entry (info, tag, val)
911
912 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
913 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
914
915 /* The name of the dynamic relocation section. */
916 #define MIPS_ELF_REL_DYN_NAME(INFO) \
917 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918 ? ".rela.dyn" : ".rel.dyn")
919
920 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
921 from smaller values. Start with zero, widen, *then* decrement. */
922 #define MINUS_ONE (((bfd_vma)0) - 1)
923 #define MINUS_TWO (((bfd_vma)0) - 2)
924
925 /* The value to write into got[1] for SVR4 targets, to identify it is
926 a GNU object. The dynamic linker can then use got[1] to store the
927 module pointer. */
928 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
929 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
930
931 /* The offset of $gp from the beginning of the .got section. */
932 #define ELF_MIPS_GP_OFFSET(INFO) \
933 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
934 ? 0x0 : 0x7ff0)
935
936 /* The maximum size of the GOT for it to be addressable using 16-bit
937 offsets from $gp. */
938 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
939
940 /* Instructions which appear in a stub. */
941 #define STUB_LW(abfd) \
942 ((ABI_64_P (abfd) \
943 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
944 : 0x8f998010)) /* lw t9,0x8010(gp) */
945 #define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
946 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
947 #define STUB_JALR 0x0320f809 /* jalr ra,t9 */
948 #define STUB_JALRC 0xf8190000 /* jalrc ra,t9 */
949 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
950 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
951 #define STUB_LI16S(abfd, VAL) \
952 ((ABI_64_P (abfd) \
953 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
954 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
955
956 /* Likewise for the microMIPS ASE. */
957 #define STUB_LW_MICROMIPS(abfd) \
958 (ABI_64_P (abfd) \
959 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
960 : 0xff3c8010) /* lw t9,0x8010(gp) */
961 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
962 #define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
963 #define STUB_LUI_MICROMIPS(VAL) \
964 (0x41b80000 + (VAL)) /* lui t8,VAL */
965 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
966 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
967 #define STUB_ORI_MICROMIPS(VAL) \
968 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
969 #define STUB_LI16U_MICROMIPS(VAL) \
970 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
971 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
972 (ABI_64_P (abfd) \
973 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
974 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
975
976 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
977 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
978 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
979 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
980 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
981 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
982
983 /* The name of the dynamic interpreter. This is put in the .interp
984 section. */
985
986 #define ELF_DYNAMIC_INTERPRETER(abfd) \
987 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
988 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
989 : "/usr/lib/libc.so.1")
990
991 #ifdef BFD64
992 #define MNAME(bfd,pre,pos) \
993 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
994 #define ELF_R_SYM(bfd, i) \
995 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
996 #define ELF_R_TYPE(bfd, i) \
997 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
998 #define ELF_R_INFO(bfd, s, t) \
999 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
1000 #else
1001 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
1002 #define ELF_R_SYM(bfd, i) \
1003 (ELF32_R_SYM (i))
1004 #define ELF_R_TYPE(bfd, i) \
1005 (ELF32_R_TYPE (i))
1006 #define ELF_R_INFO(bfd, s, t) \
1007 (ELF32_R_INFO (s, t))
1008 #endif
1009 \f
1010 /* The mips16 compiler uses a couple of special sections to handle
1011 floating point arguments.
1012
1013 Section names that look like .mips16.fn.FNNAME contain stubs that
1014 copy floating point arguments from the fp regs to the gp regs and
1015 then jump to FNNAME. If any 32 bit function calls FNNAME, the
1016 call should be redirected to the stub instead. If no 32 bit
1017 function calls FNNAME, the stub should be discarded. We need to
1018 consider any reference to the function, not just a call, because
1019 if the address of the function is taken we will need the stub,
1020 since the address might be passed to a 32 bit function.
1021
1022 Section names that look like .mips16.call.FNNAME contain stubs
1023 that copy floating point arguments from the gp regs to the fp
1024 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1025 then any 16 bit function that calls FNNAME should be redirected
1026 to the stub instead. If FNNAME is not a 32 bit function, the
1027 stub should be discarded.
1028
1029 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1030 which call FNNAME and then copy the return value from the fp regs
1031 to the gp regs. These stubs store the return value in $18 while
1032 calling FNNAME; any function which might call one of these stubs
1033 must arrange to save $18 around the call. (This case is not
1034 needed for 32 bit functions that call 16 bit functions, because
1035 16 bit functions always return floating point values in both
1036 $f0/$f1 and $2/$3.)
1037
1038 Note that in all cases FNNAME might be defined statically.
1039 Therefore, FNNAME is not used literally. Instead, the relocation
1040 information will indicate which symbol the section is for.
1041
1042 We record any stubs that we find in the symbol table. */
1043
1044 #define FN_STUB ".mips16.fn."
1045 #define CALL_STUB ".mips16.call."
1046 #define CALL_FP_STUB ".mips16.call.fp."
1047
1048 #define FN_STUB_P(name) startswith (name, FN_STUB)
1049 #define CALL_STUB_P(name) startswith (name, CALL_STUB)
1050 #define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
1051 \f
1052 /* The format of the first PLT entry in an O32 executable. */
1053 static const bfd_vma mips_o32_exec_plt0_entry[] =
1054 {
1055 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1056 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1057 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1058 0x031cc023, /* subu $24, $24, $28 */
1059 0x03e07825, /* or t7, ra, zero */
1060 0x0018c082, /* srl $24, $24, 2 */
1061 0x0320f809, /* jalr $25 */
1062 0x2718fffe /* subu $24, $24, 2 */
1063 };
1064
1065 /* The format of the first PLT entry in an O32 executable using compact
1066 jumps. */
1067 static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1068 {
1069 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1070 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1071 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1072 0x031cc023, /* subu $24, $24, $28 */
1073 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1074 0x0018c082, /* srl $24, $24, 2 */
1075 0x2718fffe, /* subu $24, $24, 2 */
1076 0xf8190000 /* jalrc $25 */
1077 };
1078
1079 /* The format of the first PLT entry in an N32 executable. Different
1080 because gp ($28) is not available; we use t2 ($14) instead. */
1081 static const bfd_vma mips_n32_exec_plt0_entry[] =
1082 {
1083 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1084 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1085 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1086 0x030ec023, /* subu $24, $24, $14 */
1087 0x03e07825, /* or t7, ra, zero */
1088 0x0018c082, /* srl $24, $24, 2 */
1089 0x0320f809, /* jalr $25 */
1090 0x2718fffe /* subu $24, $24, 2 */
1091 };
1092
1093 /* The format of the first PLT entry in an N32 executable using compact
1094 jumps. Different because gp ($28) is not available; we use t2 ($14)
1095 instead. */
1096 static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1097 {
1098 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1099 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1100 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1101 0x030ec023, /* subu $24, $24, $14 */
1102 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1103 0x0018c082, /* srl $24, $24, 2 */
1104 0x2718fffe, /* subu $24, $24, 2 */
1105 0xf8190000 /* jalrc $25 */
1106 };
1107
1108 /* The format of the first PLT entry in an N64 executable. Different
1109 from N32 because of the increased size of GOT entries. */
1110 static const bfd_vma mips_n64_exec_plt0_entry[] =
1111 {
1112 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1113 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1114 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1115 0x030ec023, /* subu $24, $24, $14 */
1116 0x03e07825, /* or t7, ra, zero */
1117 0x0018c0c2, /* srl $24, $24, 3 */
1118 0x0320f809, /* jalr $25 */
1119 0x2718fffe /* subu $24, $24, 2 */
1120 };
1121
1122 /* The format of the first PLT entry in an N64 executable using compact
1123 jumps. Different from N32 because of the increased size of GOT
1124 entries. */
1125 static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1126 {
1127 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1128 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1129 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1130 0x030ec023, /* subu $24, $24, $14 */
1131 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1132 0x0018c0c2, /* srl $24, $24, 3 */
1133 0x2718fffe, /* subu $24, $24, 2 */
1134 0xf8190000 /* jalrc $25 */
1135 };
1136
1137
1138 /* The format of the microMIPS first PLT entry in an O32 executable.
1139 We rely on v0 ($2) rather than t8 ($24) to contain the address
1140 of the GOTPLT entry handled, so this stub may only be used when
1141 all the subsequent PLT entries are microMIPS code too.
1142
1143 The trailing NOP is for alignment and correct disassembly only. */
1144 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1145 {
1146 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1147 0xff23, 0x0000, /* lw $25, 0($3) */
1148 0x0535, /* subu $2, $2, $3 */
1149 0x2525, /* srl $2, $2, 2 */
1150 0x3302, 0xfffe, /* subu $24, $2, 2 */
1151 0x0dff, /* move $15, $31 */
1152 0x45f9, /* jalrs $25 */
1153 0x0f83, /* move $28, $3 */
1154 0x0c00 /* nop */
1155 };
1156
1157 /* The format of the microMIPS first PLT entry in an O32 executable
1158 in the insn32 mode. */
1159 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1160 {
1161 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1162 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1163 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1164 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1165 0x001f, 0x7a90, /* or $15, $31, zero */
1166 0x0318, 0x1040, /* srl $24, $24, 2 */
1167 0x03f9, 0x0f3c, /* jalr $25 */
1168 0x3318, 0xfffe /* subu $24, $24, 2 */
1169 };
1170
1171 /* The format of subsequent standard PLT entries. */
1172 static const bfd_vma mips_exec_plt_entry[] =
1173 {
1174 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1175 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1176 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1177 0x03200008 /* jr $25 */
1178 };
1179
1180 static const bfd_vma mipsr6_exec_plt_entry[] =
1181 {
1182 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1183 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1184 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1185 0x03200009 /* jr $25 */
1186 };
1187
1188 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1189 {
1190 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1191 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1192 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1193 0xd8190000 /* jic $25, 0 */
1194 };
1195
1196 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1197 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1198 directly addressable. */
1199 static const bfd_vma mips16_o32_exec_plt_entry[] =
1200 {
1201 0xb203, /* lw $2, 12($pc) */
1202 0x9a60, /* lw $3, 0($2) */
1203 0x651a, /* move $24, $2 */
1204 0xeb00, /* jr $3 */
1205 0x653b, /* move $25, $3 */
1206 0x6500, /* nop */
1207 0x0000, 0x0000 /* .word (.got.plt entry) */
1208 };
1209
1210 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1211 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1212 static const bfd_vma micromips_o32_exec_plt_entry[] =
1213 {
1214 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1215 0xff22, 0x0000, /* lw $25, 0($2) */
1216 0x4599, /* jr $25 */
1217 0x0f02 /* move $24, $2 */
1218 };
1219
1220 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1221 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1222 {
1223 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1224 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1225 0x0019, 0x0f3c, /* jr $25 */
1226 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1227 };
1228
1229 /* The format of the first PLT entry in a VxWorks executable. */
1230 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1231 {
1232 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1233 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1234 0x8f390008, /* lw t9, 8(t9) */
1235 0x00000000, /* nop */
1236 0x03200008, /* jr t9 */
1237 0x00000000 /* nop */
1238 };
1239
1240 /* The format of subsequent PLT entries. */
1241 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1242 {
1243 0x10000000, /* b .PLT_resolver */
1244 0x24180000, /* li t8, <pltindex> */
1245 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1246 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1247 0x8f390000, /* lw t9, 0(t9) */
1248 0x00000000, /* nop */
1249 0x03200008, /* jr t9 */
1250 0x00000000 /* nop */
1251 };
1252
1253 /* The format of the first PLT entry in a VxWorks shared object. */
1254 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1255 {
1256 0x8f990008, /* lw t9, 8(gp) */
1257 0x00000000, /* nop */
1258 0x03200008, /* jr t9 */
1259 0x00000000, /* nop */
1260 0x00000000, /* nop */
1261 0x00000000 /* nop */
1262 };
1263
1264 /* The format of subsequent PLT entries. */
1265 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1266 {
1267 0x10000000, /* b .PLT_resolver */
1268 0x24180000 /* li t8, <pltindex> */
1269 };
1270 \f
1271 /* microMIPS 32-bit opcode helper installer. */
1272
1273 static void
1274 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1275 {
1276 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1277 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1278 }
1279
1280 /* microMIPS 32-bit opcode helper retriever. */
1281
1282 static bfd_vma
1283 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1284 {
1285 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1286 }
1287 \f
1288 /* Look up an entry in a MIPS ELF linker hash table. */
1289
1290 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1291 ((struct mips_elf_link_hash_entry *) \
1292 elf_link_hash_lookup (&(table)->root, (string), (create), \
1293 (copy), (follow)))
1294
1295 /* Traverse a MIPS ELF linker hash table. */
1296
1297 #define mips_elf_link_hash_traverse(table, func, info) \
1298 (elf_link_hash_traverse \
1299 (&(table)->root, \
1300 (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
1301 (info)))
1302
1303 /* Find the base offsets for thread-local storage in this object,
1304 for GD/LD and IE/LE respectively. */
1305
1306 #define TP_OFFSET 0x7000
1307 #define DTP_OFFSET 0x8000
1308
1309 static bfd_vma
1310 dtprel_base (struct bfd_link_info *info)
1311 {
1312 /* If tls_sec is NULL, we should have signalled an error already. */
1313 if (elf_hash_table (info)->tls_sec == NULL)
1314 return 0;
1315 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1316 }
1317
1318 static bfd_vma
1319 tprel_base (struct bfd_link_info *info)
1320 {
1321 /* If tls_sec is NULL, we should have signalled an error already. */
1322 if (elf_hash_table (info)->tls_sec == NULL)
1323 return 0;
1324 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1325 }
1326
1327 /* Create an entry in a MIPS ELF linker hash table. */
1328
1329 static struct bfd_hash_entry *
1330 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1331 struct bfd_hash_table *table, const char *string)
1332 {
1333 struct mips_elf_link_hash_entry *ret =
1334 (struct mips_elf_link_hash_entry *) entry;
1335
1336 /* Allocate the structure if it has not already been allocated by a
1337 subclass. */
1338 if (ret == NULL)
1339 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1340 if (ret == NULL)
1341 return (struct bfd_hash_entry *) ret;
1342
1343 /* Call the allocation method of the superclass. */
1344 ret = ((struct mips_elf_link_hash_entry *)
1345 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1346 table, string));
1347 if (ret != NULL)
1348 {
1349 /* Set local fields. */
1350 memset (&ret->esym, 0, sizeof (EXTR));
1351 /* We use -2 as a marker to indicate that the information has
1352 not been set. -1 means there is no associated ifd. */
1353 ret->esym.ifd = -2;
1354 ret->la25_stub = 0;
1355 ret->possibly_dynamic_relocs = 0;
1356 ret->fn_stub = NULL;
1357 ret->call_stub = NULL;
1358 ret->call_fp_stub = NULL;
1359 ret->mipsxhash_loc = 0;
1360 ret->global_got_area = GGA_NONE;
1361 ret->got_only_for_calls = true;
1362 ret->readonly_reloc = false;
1363 ret->has_static_relocs = false;
1364 ret->no_fn_stub = false;
1365 ret->need_fn_stub = false;
1366 ret->has_nonpic_branches = false;
1367 ret->needs_lazy_stub = false;
1368 ret->use_plt_entry = false;
1369 }
1370
1371 return (struct bfd_hash_entry *) ret;
1372 }
1373
1374 /* Allocate MIPS ELF private object data. */
1375
1376 bool
1377 _bfd_mips_elf_mkobject (bfd *abfd)
1378 {
1379 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1380 MIPS_ELF_DATA);
1381 }
1382
1383 /* MIPS ELF uses a special find_nearest_line routine in order the
1384 handle the ECOFF debugging information. */
1385
1386 struct mips_elf_find_line
1387 {
1388 struct ecoff_debug_info d;
1389 struct ecoff_find_line i;
1390 };
1391
1392 bool
1393 _bfd_mips_elf_free_cached_info (bfd *abfd)
1394 {
1395 struct mips_elf_obj_tdata *tdata;
1396
1397 if ((bfd_get_format (abfd) == bfd_object
1398 || bfd_get_format (abfd) == bfd_core)
1399 && (tdata = mips_elf_tdata (abfd)) != NULL)
1400 {
1401 BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1402 while (tdata->mips_hi16_list != NULL)
1403 {
1404 struct mips_hi16 *hi = tdata->mips_hi16_list;
1405 tdata->mips_hi16_list = hi->next;
1406 free (hi);
1407 }
1408 if (tdata->find_line_info != NULL)
1409 _bfd_ecoff_free_ecoff_debug_info (&tdata->find_line_info->d);
1410 }
1411 return _bfd_elf_free_cached_info (abfd);
1412 }
1413
1414 bool
1415 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1416 {
1417 if (!sec->used_by_bfd)
1418 {
1419 struct _mips_elf_section_data *sdata;
1420 size_t amt = sizeof (*sdata);
1421
1422 sdata = bfd_zalloc (abfd, amt);
1423 if (sdata == NULL)
1424 return false;
1425 sec->used_by_bfd = sdata;
1426 }
1427
1428 return _bfd_elf_new_section_hook (abfd, sec);
1429 }
1430 \f
1431 /* Read ECOFF debugging information from a .mdebug section into a
1432 ecoff_debug_info structure. */
1433
1434 bool
1435 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1436 struct ecoff_debug_info *debug)
1437 {
1438 HDRR *symhdr;
1439 const struct ecoff_debug_swap *swap;
1440 char *ext_hdr;
1441
1442 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1443 memset (debug, 0, sizeof (*debug));
1444
1445 ext_hdr = bfd_malloc (swap->external_hdr_size);
1446 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1447 goto error_return;
1448
1449 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1450 swap->external_hdr_size))
1451 goto error_return;
1452
1453 symhdr = &debug->symbolic_header;
1454 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1455 free (ext_hdr);
1456 ext_hdr = NULL;
1457
1458 /* The symbolic header contains absolute file offsets and sizes to
1459 read. */
1460 #define READ(ptr, offset, count, size) \
1461 do \
1462 { \
1463 size_t amt; \
1464 debug->ptr = NULL; \
1465 if (symhdr->count == 0) \
1466 break; \
1467 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
1468 { \
1469 bfd_set_error (bfd_error_file_too_big); \
1470 goto error_return; \
1471 } \
1472 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
1473 goto error_return; \
1474 debug->ptr = _bfd_malloc_and_read (abfd, amt + 1, amt); \
1475 if (debug->ptr == NULL) \
1476 goto error_return; \
1477 ((char *) debug->ptr)[amt] = 0; \
1478 } while (0)
1479
1480 READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
1481 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
1482 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
1483 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
1484 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
1485 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
1486 READ (ss, cbSsOffset, issMax, sizeof (char));
1487 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char));
1488 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
1489 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
1490 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size);
1491 #undef READ
1492
1493 return true;
1494
1495 error_return:
1496 free (ext_hdr);
1497 _bfd_ecoff_free_ecoff_debug_info (debug);
1498 return false;
1499 }
1500 \f
1501 /* Swap RPDR (runtime procedure table entry) for output. */
1502
1503 static void
1504 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1505 {
1506 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1507 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1508 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1509 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1510 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1511 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1512
1513 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1514 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1515
1516 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1517 }
1518
1519 /* Create a runtime procedure table from the .mdebug section. */
1520
1521 static bool
1522 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1523 struct bfd_link_info *info, asection *s,
1524 struct ecoff_debug_info *debug)
1525 {
1526 const struct ecoff_debug_swap *swap;
1527 HDRR *hdr = &debug->symbolic_header;
1528 RPDR *rpdr, *rp;
1529 struct rpdr_ext *erp;
1530 void *rtproc;
1531 struct pdr_ext *epdr;
1532 struct sym_ext *esym;
1533 char *ss, **sv;
1534 char *str;
1535 bfd_size_type size;
1536 bfd_size_type count;
1537 unsigned long sindex;
1538 unsigned long i;
1539 PDR pdr;
1540 SYMR sym;
1541 const char *no_name_func = _("static procedure (no name)");
1542
1543 epdr = NULL;
1544 rpdr = NULL;
1545 esym = NULL;
1546 ss = NULL;
1547 sv = NULL;
1548
1549 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1550
1551 sindex = strlen (no_name_func) + 1;
1552 count = hdr->ipdMax;
1553 if (count > 0)
1554 {
1555 size = swap->external_pdr_size;
1556
1557 epdr = bfd_malloc (size * count);
1558 if (epdr == NULL)
1559 goto error_return;
1560
1561 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1562 goto error_return;
1563
1564 size = sizeof (RPDR);
1565 rp = rpdr = bfd_malloc (size * count);
1566 if (rpdr == NULL)
1567 goto error_return;
1568
1569 size = sizeof (char *);
1570 sv = bfd_malloc (size * count);
1571 if (sv == NULL)
1572 goto error_return;
1573
1574 count = hdr->isymMax;
1575 size = swap->external_sym_size;
1576 esym = bfd_malloc (size * count);
1577 if (esym == NULL)
1578 goto error_return;
1579
1580 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1581 goto error_return;
1582
1583 count = hdr->issMax;
1584 ss = bfd_malloc (count);
1585 if (ss == NULL)
1586 goto error_return;
1587 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1588 goto error_return;
1589
1590 count = hdr->ipdMax;
1591 for (i = 0; i < (unsigned long) count; i++, rp++)
1592 {
1593 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1594 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1595 rp->adr = sym.value;
1596 rp->regmask = pdr.regmask;
1597 rp->regoffset = pdr.regoffset;
1598 rp->fregmask = pdr.fregmask;
1599 rp->fregoffset = pdr.fregoffset;
1600 rp->frameoffset = pdr.frameoffset;
1601 rp->framereg = pdr.framereg;
1602 rp->pcreg = pdr.pcreg;
1603 rp->irpss = sindex;
1604 sv[i] = ss + sym.iss;
1605 sindex += strlen (sv[i]) + 1;
1606 }
1607 }
1608
1609 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1610 size = BFD_ALIGN (size, 16);
1611 rtproc = bfd_alloc (abfd, size);
1612 if (rtproc == NULL)
1613 {
1614 mips_elf_hash_table (info)->procedure_count = 0;
1615 goto error_return;
1616 }
1617
1618 mips_elf_hash_table (info)->procedure_count = count + 2;
1619
1620 erp = rtproc;
1621 memset (erp, 0, sizeof (struct rpdr_ext));
1622 erp++;
1623 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1624 strcpy (str, no_name_func);
1625 str += strlen (no_name_func) + 1;
1626 for (i = 0; i < count; i++)
1627 {
1628 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1629 strcpy (str, sv[i]);
1630 str += strlen (sv[i]) + 1;
1631 }
1632 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1633
1634 /* Set the size and contents of .rtproc section. */
1635 s->size = size;
1636 s->contents = rtproc;
1637
1638 /* Skip this section later on (I don't think this currently
1639 matters, but someday it might). */
1640 s->map_head.link_order = NULL;
1641
1642 free (epdr);
1643 free (rpdr);
1644 free (esym);
1645 free (ss);
1646 free (sv);
1647 return true;
1648
1649 error_return:
1650 free (epdr);
1651 free (rpdr);
1652 free (esym);
1653 free (ss);
1654 free (sv);
1655 return false;
1656 }
1657 \f
1658 /* We're going to create a stub for H. Create a symbol for the stub's
1659 value and size, to help make the disassembly easier to read. */
1660
1661 static bool
1662 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1663 struct mips_elf_link_hash_entry *h,
1664 const char *prefix, asection *s, bfd_vma value,
1665 bfd_vma size)
1666 {
1667 bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1668 struct bfd_link_hash_entry *bh;
1669 struct elf_link_hash_entry *elfh;
1670 char *name;
1671 bool res;
1672
1673 if (micromips_p)
1674 value |= 1;
1675
1676 /* Create a new symbol. */
1677 name = concat (prefix, h->root.root.root.string, NULL);
1678 bh = NULL;
1679 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1680 BSF_LOCAL, s, value, NULL,
1681 true, false, &bh);
1682 free (name);
1683 if (! res)
1684 return false;
1685
1686 /* Make it a local function. */
1687 elfh = (struct elf_link_hash_entry *) bh;
1688 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1689 elfh->size = size;
1690 elfh->forced_local = 1;
1691 if (micromips_p)
1692 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1693 return true;
1694 }
1695
1696 /* We're about to redefine H. Create a symbol to represent H's
1697 current value and size, to help make the disassembly easier
1698 to read. */
1699
1700 static bool
1701 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1702 struct mips_elf_link_hash_entry *h,
1703 const char *prefix)
1704 {
1705 struct bfd_link_hash_entry *bh;
1706 struct elf_link_hash_entry *elfh;
1707 char *name;
1708 asection *s;
1709 bfd_vma value;
1710 bool res;
1711
1712 /* Read the symbol's value. */
1713 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1714 || h->root.root.type == bfd_link_hash_defweak);
1715 s = h->root.root.u.def.section;
1716 value = h->root.root.u.def.value;
1717
1718 /* Create a new symbol. */
1719 name = concat (prefix, h->root.root.root.string, NULL);
1720 bh = NULL;
1721 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1722 BSF_LOCAL, s, value, NULL,
1723 true, false, &bh);
1724 free (name);
1725 if (! res)
1726 return false;
1727
1728 /* Make it local and copy the other attributes from H. */
1729 elfh = (struct elf_link_hash_entry *) bh;
1730 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1731 elfh->other = h->root.other;
1732 elfh->size = h->root.size;
1733 elfh->forced_local = 1;
1734 return true;
1735 }
1736
1737 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1738 function rather than to a hard-float stub. */
1739
1740 static bool
1741 section_allows_mips16_refs_p (asection *section)
1742 {
1743 const char *name;
1744
1745 name = bfd_section_name (section);
1746 return (FN_STUB_P (name)
1747 || CALL_STUB_P (name)
1748 || CALL_FP_STUB_P (name)
1749 || strcmp (name, ".pdr") == 0);
1750 }
1751
1752 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1753 stub section of some kind. Return the R_SYMNDX of the target
1754 function, or 0 if we can't decide which function that is. */
1755
1756 static unsigned long
1757 mips16_stub_symndx (const struct elf_backend_data *bed,
1758 asection *sec ATTRIBUTE_UNUSED,
1759 const Elf_Internal_Rela *relocs,
1760 const Elf_Internal_Rela *relend)
1761 {
1762 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1763 const Elf_Internal_Rela *rel;
1764
1765 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1766 one in a compound relocation. */
1767 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1768 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1769 return ELF_R_SYM (sec->owner, rel->r_info);
1770
1771 /* Otherwise trust the first relocation, whatever its kind. This is
1772 the traditional behavior. */
1773 if (relocs < relend)
1774 return ELF_R_SYM (sec->owner, relocs->r_info);
1775
1776 return 0;
1777 }
1778
1779 /* Check the mips16 stubs for a particular symbol, and see if we can
1780 discard them. */
1781
1782 static void
1783 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1784 struct mips_elf_link_hash_entry *h)
1785 {
1786 /* Dynamic symbols must use the standard call interface, in case other
1787 objects try to call them. */
1788 if (h->fn_stub != NULL
1789 && h->root.dynindx != -1)
1790 {
1791 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1792 h->need_fn_stub = true;
1793 }
1794
1795 if (h->fn_stub != NULL
1796 && ! h->need_fn_stub)
1797 {
1798 /* We don't need the fn_stub; the only references to this symbol
1799 are 16 bit calls. Clobber the size to 0 to prevent it from
1800 being included in the link. */
1801 h->fn_stub->size = 0;
1802 h->fn_stub->flags &= ~SEC_RELOC;
1803 h->fn_stub->reloc_count = 0;
1804 h->fn_stub->flags |= SEC_EXCLUDE;
1805 h->fn_stub->output_section = bfd_abs_section_ptr;
1806 }
1807
1808 if (h->call_stub != NULL
1809 && ELF_ST_IS_MIPS16 (h->root.other))
1810 {
1811 /* We don't need the call_stub; this is a 16 bit function, so
1812 calls from other 16 bit functions are OK. Clobber the size
1813 to 0 to prevent it from being included in the link. */
1814 h->call_stub->size = 0;
1815 h->call_stub->flags &= ~SEC_RELOC;
1816 h->call_stub->reloc_count = 0;
1817 h->call_stub->flags |= SEC_EXCLUDE;
1818 h->call_stub->output_section = bfd_abs_section_ptr;
1819 }
1820
1821 if (h->call_fp_stub != NULL
1822 && ELF_ST_IS_MIPS16 (h->root.other))
1823 {
1824 /* We don't need the call_stub; this is a 16 bit function, so
1825 calls from other 16 bit functions are OK. Clobber the size
1826 to 0 to prevent it from being included in the link. */
1827 h->call_fp_stub->size = 0;
1828 h->call_fp_stub->flags &= ~SEC_RELOC;
1829 h->call_fp_stub->reloc_count = 0;
1830 h->call_fp_stub->flags |= SEC_EXCLUDE;
1831 h->call_fp_stub->output_section = bfd_abs_section_ptr;
1832 }
1833 }
1834
1835 /* Hashtable callbacks for mips_elf_la25_stubs. */
1836
1837 static hashval_t
1838 mips_elf_la25_stub_hash (const void *entry_)
1839 {
1840 const struct mips_elf_la25_stub *entry;
1841
1842 entry = (struct mips_elf_la25_stub *) entry_;
1843 return entry->h->root.root.u.def.section->id
1844 + entry->h->root.root.u.def.value;
1845 }
1846
1847 static int
1848 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1849 {
1850 const struct mips_elf_la25_stub *entry1, *entry2;
1851
1852 entry1 = (struct mips_elf_la25_stub *) entry1_;
1853 entry2 = (struct mips_elf_la25_stub *) entry2_;
1854 return ((entry1->h->root.root.u.def.section
1855 == entry2->h->root.root.u.def.section)
1856 && (entry1->h->root.root.u.def.value
1857 == entry2->h->root.root.u.def.value));
1858 }
1859
1860 /* Called by the linker to set up the la25 stub-creation code. FN is
1861 the linker's implementation of add_stub_function. Return true on
1862 success. */
1863
1864 bool
1865 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1866 asection *(*fn) (const char *, asection *,
1867 asection *))
1868 {
1869 struct mips_elf_link_hash_table *htab;
1870
1871 htab = mips_elf_hash_table (info);
1872 if (htab == NULL)
1873 return false;
1874
1875 htab->add_stub_section = fn;
1876 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1877 mips_elf_la25_stub_eq, NULL);
1878 if (htab->la25_stubs == NULL)
1879 return false;
1880
1881 return true;
1882 }
1883
1884 /* Return true if H is a locally-defined PIC function, in the sense
1885 that it or its fn_stub might need $25 to be valid on entry.
1886 Note that MIPS16 functions set up $gp using PC-relative instructions,
1887 so they themselves never need $25 to be valid. Only non-MIPS16
1888 entry points are of interest here. */
1889
1890 static bool
1891 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1892 {
1893 return ((h->root.root.type == bfd_link_hash_defined
1894 || h->root.root.type == bfd_link_hash_defweak)
1895 && h->root.def_regular
1896 && !bfd_is_abs_section (h->root.root.u.def.section)
1897 && !bfd_is_und_section (h->root.root.u.def.section)
1898 && (!ELF_ST_IS_MIPS16 (h->root.other)
1899 || (h->fn_stub && h->need_fn_stub))
1900 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1901 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1902 }
1903
1904 /* Set *SEC to the input section that contains the target of STUB.
1905 Return the offset of the target from the start of that section. */
1906
1907 static bfd_vma
1908 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1909 asection **sec)
1910 {
1911 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1912 {
1913 BFD_ASSERT (stub->h->need_fn_stub);
1914 *sec = stub->h->fn_stub;
1915 return 0;
1916 }
1917 else
1918 {
1919 *sec = stub->h->root.root.u.def.section;
1920 return stub->h->root.root.u.def.value;
1921 }
1922 }
1923
1924 /* STUB describes an la25 stub that we have decided to implement
1925 by inserting an LUI/ADDIU pair before the target function.
1926 Create the section and redirect the function symbol to it. */
1927
1928 static bool
1929 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1930 struct bfd_link_info *info)
1931 {
1932 struct mips_elf_link_hash_table *htab;
1933 char *name;
1934 asection *s, *input_section;
1935 unsigned int align;
1936
1937 htab = mips_elf_hash_table (info);
1938 if (htab == NULL)
1939 return false;
1940
1941 /* Create a unique name for the new section. */
1942 name = bfd_malloc (11 + sizeof (".text.stub."));
1943 if (name == NULL)
1944 return false;
1945 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1946
1947 /* Create the section. */
1948 mips_elf_get_la25_target (stub, &input_section);
1949 s = htab->add_stub_section (name, input_section,
1950 input_section->output_section);
1951 if (s == NULL)
1952 return false;
1953
1954 /* Make sure that any padding goes before the stub. */
1955 align = input_section->alignment_power;
1956 if (!bfd_set_section_alignment (s, align))
1957 return false;
1958 if (align > 3)
1959 s->size = (1 << align) - 8;
1960
1961 /* Create a symbol for the stub. */
1962 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1963 stub->stub_section = s;
1964 stub->offset = s->size;
1965
1966 /* Allocate room for it. */
1967 s->size += 8;
1968 return true;
1969 }
1970
1971 /* STUB describes an la25 stub that we have decided to implement
1972 with a separate trampoline. Allocate room for it and redirect
1973 the function symbol to it. */
1974
1975 static bool
1976 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1977 struct bfd_link_info *info)
1978 {
1979 struct mips_elf_link_hash_table *htab;
1980 asection *s;
1981
1982 htab = mips_elf_hash_table (info);
1983 if (htab == NULL)
1984 return false;
1985
1986 /* Create a trampoline section, if we haven't already. */
1987 s = htab->strampoline;
1988 if (s == NULL)
1989 {
1990 asection *input_section = stub->h->root.root.u.def.section;
1991 s = htab->add_stub_section (".text", NULL,
1992 input_section->output_section);
1993 if (s == NULL || !bfd_set_section_alignment (s, 4))
1994 return false;
1995 htab->strampoline = s;
1996 }
1997
1998 /* Create a symbol for the stub. */
1999 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
2000 stub->stub_section = s;
2001 stub->offset = s->size;
2002
2003 /* Allocate room for it. */
2004 s->size += 16;
2005 return true;
2006 }
2007
2008 /* H describes a symbol that needs an la25 stub. Make sure that an
2009 appropriate stub exists and point H at it. */
2010
2011 static bool
2012 mips_elf_add_la25_stub (struct bfd_link_info *info,
2013 struct mips_elf_link_hash_entry *h)
2014 {
2015 struct mips_elf_link_hash_table *htab;
2016 struct mips_elf_la25_stub search, *stub;
2017 bool use_trampoline_p;
2018 asection *s;
2019 bfd_vma value;
2020 void **slot;
2021
2022 /* Describe the stub we want. */
2023 search.stub_section = NULL;
2024 search.offset = 0;
2025 search.h = h;
2026
2027 /* See if we've already created an equivalent stub. */
2028 htab = mips_elf_hash_table (info);
2029 if (htab == NULL)
2030 return false;
2031
2032 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2033 if (slot == NULL)
2034 return false;
2035
2036 stub = (struct mips_elf_la25_stub *) *slot;
2037 if (stub != NULL)
2038 {
2039 /* We can reuse the existing stub. */
2040 h->la25_stub = stub;
2041 return true;
2042 }
2043
2044 /* Create a permanent copy of ENTRY and add it to the hash table. */
2045 stub = bfd_malloc (sizeof (search));
2046 if (stub == NULL)
2047 return false;
2048 *stub = search;
2049 *slot = stub;
2050
2051 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2052 of the section and if we would need no more than 2 nops. */
2053 value = mips_elf_get_la25_target (stub, &s);
2054 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2055 value &= ~1;
2056 use_trampoline_p = (value != 0 || s->alignment_power > 4);
2057
2058 h->la25_stub = stub;
2059 return (use_trampoline_p
2060 ? mips_elf_add_la25_trampoline (stub, info)
2061 : mips_elf_add_la25_intro (stub, info));
2062 }
2063
2064 /* A mips_elf_link_hash_traverse callback that is called before sizing
2065 sections. DATA points to a mips_htab_traverse_info structure. */
2066
2067 static bool
2068 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2069 {
2070 struct mips_htab_traverse_info *hti;
2071
2072 hti = (struct mips_htab_traverse_info *) data;
2073 if (!bfd_link_relocatable (hti->info))
2074 mips_elf_check_mips16_stubs (hti->info, h);
2075
2076 if (mips_elf_local_pic_function_p (h))
2077 {
2078 /* PR 12845: If H is in a section that has been garbage
2079 collected it will have its output section set to *ABS*. */
2080 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2081 return true;
2082
2083 /* H is a function that might need $25 to be valid on entry.
2084 If we're creating a non-PIC relocatable object, mark H as
2085 being PIC. If we're creating a non-relocatable object with
2086 non-PIC branches and jumps to H, make sure that H has an la25
2087 stub. */
2088 if (bfd_link_relocatable (hti->info))
2089 {
2090 if (!PIC_OBJECT_P (hti->output_bfd))
2091 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2092 }
2093 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2094 {
2095 hti->error = true;
2096 return false;
2097 }
2098 }
2099 return true;
2100 }
2101 \f
2102 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2103 Most mips16 instructions are 16 bits, but these instructions
2104 are 32 bits.
2105
2106 The format of these instructions is:
2107
2108 +--------------+--------------------------------+
2109 | JALX | X| Imm 20:16 | Imm 25:21 |
2110 +--------------+--------------------------------+
2111 | Immediate 15:0 |
2112 +-----------------------------------------------+
2113
2114 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2115 Note that the immediate value in the first word is swapped.
2116
2117 When producing a relocatable object file, R_MIPS16_26 is
2118 handled mostly like R_MIPS_26. In particular, the addend is
2119 stored as a straight 26-bit value in a 32-bit instruction.
2120 (gas makes life simpler for itself by never adjusting a
2121 R_MIPS16_26 reloc to be against a section, so the addend is
2122 always zero). However, the 32 bit instruction is stored as 2
2123 16-bit values, rather than a single 32-bit value. In a
2124 big-endian file, the result is the same; in a little-endian
2125 file, the two 16-bit halves of the 32 bit value are swapped.
2126 This is so that a disassembler can recognize the jal
2127 instruction.
2128
2129 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2130 instruction stored as two 16-bit values. The addend A is the
2131 contents of the targ26 field. The calculation is the same as
2132 R_MIPS_26. When storing the calculated value, reorder the
2133 immediate value as shown above, and don't forget to store the
2134 value as two 16-bit values.
2135
2136 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2137 defined as
2138
2139 big-endian:
2140 +--------+----------------------+
2141 | | |
2142 | | targ26-16 |
2143 |31 26|25 0|
2144 +--------+----------------------+
2145
2146 little-endian:
2147 +----------+------+-------------+
2148 | | | |
2149 | sub1 | | sub2 |
2150 |0 9|10 15|16 31|
2151 +----------+--------------------+
2152 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2153 ((sub1 << 16) | sub2)).
2154
2155 When producing a relocatable object file, the calculation is
2156 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2157 When producing a fully linked file, the calculation is
2158 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2159 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2160
2161 The table below lists the other MIPS16 instruction relocations.
2162 Each one is calculated in the same way as the non-MIPS16 relocation
2163 given on the right, but using the extended MIPS16 layout of 16-bit
2164 immediate fields:
2165
2166 R_MIPS16_GPREL R_MIPS_GPREL16
2167 R_MIPS16_GOT16 R_MIPS_GOT16
2168 R_MIPS16_CALL16 R_MIPS_CALL16
2169 R_MIPS16_HI16 R_MIPS_HI16
2170 R_MIPS16_LO16 R_MIPS_LO16
2171
2172 A typical instruction will have a format like this:
2173
2174 +--------------+--------------------------------+
2175 | EXTEND | Imm 10:5 | Imm 15:11 |
2176 +--------------+--------------------------------+
2177 | Major | rx | ry | Imm 4:0 |
2178 +--------------+--------------------------------+
2179
2180 EXTEND is the five bit value 11110. Major is the instruction
2181 opcode.
2182
2183 All we need to do here is shuffle the bits appropriately.
2184 As above, the two 16-bit halves must be swapped on a
2185 little-endian system.
2186
2187 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2188 relocatable field is shifted by 1 rather than 2 and the same bit
2189 shuffling is done as with the relocations above. */
2190
2191 static inline bool
2192 mips16_reloc_p (int r_type)
2193 {
2194 switch (r_type)
2195 {
2196 case R_MIPS16_26:
2197 case R_MIPS16_GPREL:
2198 case R_MIPS16_GOT16:
2199 case R_MIPS16_CALL16:
2200 case R_MIPS16_HI16:
2201 case R_MIPS16_LO16:
2202 case R_MIPS16_TLS_GD:
2203 case R_MIPS16_TLS_LDM:
2204 case R_MIPS16_TLS_DTPREL_HI16:
2205 case R_MIPS16_TLS_DTPREL_LO16:
2206 case R_MIPS16_TLS_GOTTPREL:
2207 case R_MIPS16_TLS_TPREL_HI16:
2208 case R_MIPS16_TLS_TPREL_LO16:
2209 case R_MIPS16_PC16_S1:
2210 return true;
2211
2212 default:
2213 return false;
2214 }
2215 }
2216
2217 /* Check if a microMIPS reloc. */
2218
2219 static inline bool
2220 micromips_reloc_p (unsigned int r_type)
2221 {
2222 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2223 }
2224
2225 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2226 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2227 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2228
2229 static inline bool
2230 micromips_reloc_shuffle_p (unsigned int r_type)
2231 {
2232 return (micromips_reloc_p (r_type)
2233 && r_type != R_MICROMIPS_PC7_S1
2234 && r_type != R_MICROMIPS_PC10_S1);
2235 }
2236
2237 static inline bool
2238 got16_reloc_p (int r_type)
2239 {
2240 return (r_type == R_MIPS_GOT16
2241 || r_type == R_MIPS16_GOT16
2242 || r_type == R_MICROMIPS_GOT16);
2243 }
2244
2245 static inline bool
2246 call16_reloc_p (int r_type)
2247 {
2248 return (r_type == R_MIPS_CALL16
2249 || r_type == R_MIPS16_CALL16
2250 || r_type == R_MICROMIPS_CALL16);
2251 }
2252
2253 static inline bool
2254 got_disp_reloc_p (unsigned int r_type)
2255 {
2256 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2257 }
2258
2259 static inline bool
2260 got_page_reloc_p (unsigned int r_type)
2261 {
2262 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2263 }
2264
2265 static inline bool
2266 got_lo16_reloc_p (unsigned int r_type)
2267 {
2268 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2269 }
2270
2271 static inline bool
2272 call_hi16_reloc_p (unsigned int r_type)
2273 {
2274 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2275 }
2276
2277 static inline bool
2278 call_lo16_reloc_p (unsigned int r_type)
2279 {
2280 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2281 }
2282
2283 static inline bool
2284 hi16_reloc_p (int r_type)
2285 {
2286 return (r_type == R_MIPS_HI16
2287 || r_type == R_MIPS16_HI16
2288 || r_type == R_MICROMIPS_HI16
2289 || r_type == R_MIPS_PCHI16);
2290 }
2291
2292 static inline bool
2293 lo16_reloc_p (int r_type)
2294 {
2295 return (r_type == R_MIPS_LO16
2296 || r_type == R_MIPS16_LO16
2297 || r_type == R_MICROMIPS_LO16
2298 || r_type == R_MIPS_PCLO16);
2299 }
2300
2301 static inline bool
2302 mips16_call_reloc_p (int r_type)
2303 {
2304 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2305 }
2306
2307 static inline bool
2308 jal_reloc_p (int r_type)
2309 {
2310 return (r_type == R_MIPS_26
2311 || r_type == R_MIPS16_26
2312 || r_type == R_MICROMIPS_26_S1);
2313 }
2314
2315 static inline bool
2316 b_reloc_p (int r_type)
2317 {
2318 return (r_type == R_MIPS_PC26_S2
2319 || r_type == R_MIPS_PC21_S2
2320 || r_type == R_MIPS_PC16
2321 || r_type == R_MIPS_GNU_REL16_S2
2322 || r_type == R_MIPS16_PC16_S1
2323 || r_type == R_MICROMIPS_PC16_S1
2324 || r_type == R_MICROMIPS_PC10_S1
2325 || r_type == R_MICROMIPS_PC7_S1);
2326 }
2327
2328 static inline bool
2329 aligned_pcrel_reloc_p (int r_type)
2330 {
2331 return (r_type == R_MIPS_PC18_S3
2332 || r_type == R_MIPS_PC19_S2);
2333 }
2334
2335 static inline bool
2336 branch_reloc_p (int r_type)
2337 {
2338 return (r_type == R_MIPS_26
2339 || r_type == R_MIPS_PC26_S2
2340 || r_type == R_MIPS_PC21_S2
2341 || r_type == R_MIPS_PC16
2342 || r_type == R_MIPS_GNU_REL16_S2);
2343 }
2344
2345 static inline bool
2346 mips16_branch_reloc_p (int r_type)
2347 {
2348 return (r_type == R_MIPS16_26
2349 || r_type == R_MIPS16_PC16_S1);
2350 }
2351
2352 static inline bool
2353 micromips_branch_reloc_p (int r_type)
2354 {
2355 return (r_type == R_MICROMIPS_26_S1
2356 || r_type == R_MICROMIPS_PC16_S1
2357 || r_type == R_MICROMIPS_PC10_S1
2358 || r_type == R_MICROMIPS_PC7_S1);
2359 }
2360
2361 static inline bool
2362 tls_gd_reloc_p (unsigned int r_type)
2363 {
2364 return (r_type == R_MIPS_TLS_GD
2365 || r_type == R_MIPS16_TLS_GD
2366 || r_type == R_MICROMIPS_TLS_GD);
2367 }
2368
2369 static inline bool
2370 tls_ldm_reloc_p (unsigned int r_type)
2371 {
2372 return (r_type == R_MIPS_TLS_LDM
2373 || r_type == R_MIPS16_TLS_LDM
2374 || r_type == R_MICROMIPS_TLS_LDM);
2375 }
2376
2377 static inline bool
2378 tls_gottprel_reloc_p (unsigned int r_type)
2379 {
2380 return (r_type == R_MIPS_TLS_GOTTPREL
2381 || r_type == R_MIPS16_TLS_GOTTPREL
2382 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2383 }
2384
2385 static inline bool
2386 needs_shuffle (int r_type)
2387 {
2388 return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2389 }
2390
2391 void
2392 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2393 bool jal_shuffle, bfd_byte *data)
2394 {
2395 bfd_vma first, second, val;
2396
2397 if (!needs_shuffle (r_type))
2398 return;
2399
2400 /* Pick up the first and second halfwords of the instruction. */
2401 first = bfd_get_16 (abfd, data);
2402 second = bfd_get_16 (abfd, data + 2);
2403 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2404 val = first << 16 | second;
2405 else if (r_type != R_MIPS16_26)
2406 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2407 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2408 else
2409 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2410 | ((first & 0x1f) << 21) | second);
2411 bfd_put_32 (abfd, val, data);
2412 }
2413
2414 void
2415 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2416 bool jal_shuffle, bfd_byte *data)
2417 {
2418 bfd_vma first, second, val;
2419
2420 if (!needs_shuffle (r_type))
2421 return;
2422
2423 val = bfd_get_32 (abfd, data);
2424 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2425 {
2426 second = val & 0xffff;
2427 first = val >> 16;
2428 }
2429 else if (r_type != R_MIPS16_26)
2430 {
2431 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2432 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2433 }
2434 else
2435 {
2436 second = val & 0xffff;
2437 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2438 | ((val >> 21) & 0x1f);
2439 }
2440 bfd_put_16 (abfd, second, data + 2);
2441 bfd_put_16 (abfd, first, data);
2442 }
2443
2444 /* Perform reloc offset checking.
2445 We can only use bfd_reloc_offset_in_range, which takes into account
2446 the size of the field being relocated, when section contents will
2447 be accessed because mips object files may use relocations that seem
2448 to access beyond section limits.
2449 gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2450 R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2451 section. The R_MIPS_SUB applies to the addend for the next reloc
2452 rather than the section contents.
2453
2454 CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2455 CHECK_INPLACE to only check partial_inplace relocs, and
2456 CHECK_SHUFFLE to only check relocs that shuffle/unshuffle. */
2457
2458 bool
2459 _bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2460 arelent *reloc_entry, enum reloc_check check)
2461 {
2462 if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2463 return true;
2464 if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2465 return true;
2466 return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2467 input_section, reloc_entry->address);
2468 }
2469
2470 bfd_reloc_status_type
2471 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2472 arelent *reloc_entry, asection *input_section,
2473 bool relocatable, void *data, bfd_vma gp)
2474 {
2475 bfd_vma relocation;
2476 bfd_signed_vma val;
2477 bfd_reloc_status_type status;
2478
2479 if (bfd_is_com_section (symbol->section))
2480 relocation = 0;
2481 else
2482 relocation = symbol->value;
2483
2484 if (symbol->section->output_section != NULL)
2485 {
2486 relocation += symbol->section->output_section->vma;
2487 relocation += symbol->section->output_offset;
2488 }
2489
2490 /* Set val to the offset into the section or symbol. */
2491 val = reloc_entry->addend;
2492
2493 _bfd_mips_elf_sign_extend (val, 16);
2494
2495 /* Adjust val for the final section location and GP value. If we
2496 are producing relocatable output, we don't want to do this for
2497 an external symbol. */
2498 if (! relocatable
2499 || (symbol->flags & BSF_SECTION_SYM) != 0)
2500 val += relocation - gp;
2501
2502 if (reloc_entry->howto->partial_inplace)
2503 {
2504 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2505 reloc_entry->address))
2506 return bfd_reloc_outofrange;
2507
2508 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2509 (bfd_byte *) data
2510 + reloc_entry->address);
2511 if (status != bfd_reloc_ok)
2512 return status;
2513 }
2514 else
2515 reloc_entry->addend = val;
2516
2517 if (relocatable)
2518 reloc_entry->address += input_section->output_offset;
2519
2520 return bfd_reloc_ok;
2521 }
2522
2523 /* A howto special_function for REL *HI16 relocations. We can only
2524 calculate the correct value once we've seen the partnering
2525 *LO16 relocation, so just save the information for later.
2526
2527 The ABI requires that the *LO16 immediately follow the *HI16.
2528 However, as a GNU extension, we permit an arbitrary number of
2529 *HI16s to be associated with a single *LO16. This significantly
2530 simplies the relocation handling in gcc. */
2531
2532 bfd_reloc_status_type
2533 _bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2534 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2535 asection *input_section, bfd *output_bfd,
2536 char **error_message ATTRIBUTE_UNUSED)
2537 {
2538 struct mips_hi16 *n;
2539 struct mips_elf_obj_tdata *tdata;
2540
2541 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2542 return bfd_reloc_outofrange;
2543
2544 n = bfd_malloc (sizeof *n);
2545 if (n == NULL)
2546 return bfd_reloc_outofrange;
2547
2548 tdata = mips_elf_tdata (abfd);
2549 n->next = tdata->mips_hi16_list;
2550 n->data = data;
2551 n->input_section = input_section;
2552 n->rel = *reloc_entry;
2553 tdata->mips_hi16_list = n;
2554
2555 if (output_bfd != NULL)
2556 reloc_entry->address += input_section->output_offset;
2557
2558 return bfd_reloc_ok;
2559 }
2560
2561 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2562 like any other 16-bit relocation when applied to global symbols, but is
2563 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2564
2565 bfd_reloc_status_type
2566 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2567 void *data, asection *input_section,
2568 bfd *output_bfd, char **error_message)
2569 {
2570 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2571 || bfd_is_und_section (bfd_asymbol_section (symbol))
2572 || bfd_is_com_section (bfd_asymbol_section (symbol)))
2573 /* The relocation is against a global symbol. */
2574 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2575 input_section, output_bfd,
2576 error_message);
2577
2578 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2579 input_section, output_bfd, error_message);
2580 }
2581
2582 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2583 is a straightforward 16 bit inplace relocation, but we must deal with
2584 any partnering high-part relocations as well. */
2585
2586 bfd_reloc_status_type
2587 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2588 void *data, asection *input_section,
2589 bfd *output_bfd, char **error_message)
2590 {
2591 bfd_vma vallo;
2592 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2593 struct mips_elf_obj_tdata *tdata;
2594
2595 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2596 reloc_entry->address))
2597 return bfd_reloc_outofrange;
2598
2599 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2600 location);
2601 /* The high 16 bits of the addend are stored in the high insn, the
2602 low 16 bits in the low insn, but there is a catch: You can't
2603 just concatenate the high and low parts. The high part of the
2604 addend is adjusted for the fact that the low part is sign
2605 extended. For example, an addend of 0x38000 would have 0x0004 in
2606 the high part and 0x8000 (=0xff..f8000) in the low part.
2607 To extract the actual addend, calculate (a)
2608 ((hi & 0xffff) << 16) + ((lo & 0xffff) ^ 0x8000) - 0x8000.
2609 We will be applying (symbol + addend) & 0xffff to the low insn,
2610 and we want to apply (b) (symbol + addend + 0x8000) >> 16 to the
2611 high insn (the +0x8000 adjusting for when the applied low part is
2612 negative). Substituting (a) into (b) and recognising that
2613 (hi & 0xffff) is already in the high insn gives a high part
2614 addend adjustment of (lo & 0xffff) ^ 0x8000. */
2615 vallo = (bfd_get_32 (abfd, location) & 0xffff) ^ 0x8000;
2616 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2617 location);
2618
2619 tdata = mips_elf_tdata (abfd);
2620 while (tdata->mips_hi16_list != NULL)
2621 {
2622 bfd_reloc_status_type ret;
2623 struct mips_hi16 *hi;
2624
2625 hi = tdata->mips_hi16_list;
2626
2627 /* R_MIPS*_GOT16 relocations are something of a special case. We
2628 want to install the addend in the same way as for a R_MIPS*_HI16
2629 relocation (with a rightshift of 16). However, since GOT16
2630 relocations can also be used with global symbols, their howto
2631 has a rightshift of 0. */
2632 if (hi->rel.howto->type == R_MIPS_GOT16)
2633 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2634 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2635 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2636 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2637 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2638
2639 hi->rel.addend += vallo;
2640
2641 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2642 hi->input_section, output_bfd,
2643 error_message);
2644 if (ret != bfd_reloc_ok)
2645 return ret;
2646
2647 tdata->mips_hi16_list = hi->next;
2648 free (hi);
2649 }
2650
2651 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2652 input_section, output_bfd,
2653 error_message);
2654 }
2655
2656 /* A generic howto special_function. This calculates and installs the
2657 relocation itself, thus avoiding the oft-discussed problems in
2658 bfd_perform_relocation and bfd_install_relocation. */
2659
2660 bfd_reloc_status_type
2661 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2662 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2663 asection *input_section, bfd *output_bfd,
2664 char **error_message ATTRIBUTE_UNUSED)
2665 {
2666 bfd_signed_vma val;
2667 bfd_reloc_status_type status;
2668 bool relocatable;
2669
2670 relocatable = (output_bfd != NULL);
2671
2672 if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2673 (relocatable
2674 ? check_inplace : check_std)))
2675 return bfd_reloc_outofrange;
2676
2677 /* Build up the field adjustment in VAL. */
2678 val = 0;
2679 if ((!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2680 && symbol->section->output_section != NULL)
2681 {
2682 /* Either we're calculating the final field value or we have a
2683 relocation against a section symbol. Add in the section's
2684 offset or address. */
2685 val += symbol->section->output_section->vma;
2686 val += symbol->section->output_offset;
2687 }
2688
2689 if (!relocatable)
2690 {
2691 /* We're calculating the final field value. Add in the symbol's value
2692 and, if pc-relative, subtract the address of the field itself. */
2693 val += symbol->value;
2694 if (reloc_entry->howto->pc_relative)
2695 {
2696 val -= input_section->output_section->vma;
2697 val -= input_section->output_offset;
2698 val -= reloc_entry->address;
2699 }
2700 }
2701
2702 /* VAL is now the final adjustment. If we're keeping this relocation
2703 in the output file, and if the relocation uses a separate addend,
2704 we just need to add VAL to that addend. Otherwise we need to add
2705 VAL to the relocation field itself. */
2706 if (relocatable && !reloc_entry->howto->partial_inplace)
2707 reloc_entry->addend += val;
2708 else
2709 {
2710 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2711
2712 /* Add in the separate addend, if any. */
2713 val += reloc_entry->addend;
2714
2715 /* Add VAL to the relocation field. */
2716 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2717 location);
2718 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2719 location);
2720 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2721 location);
2722
2723 if (status != bfd_reloc_ok)
2724 return status;
2725 }
2726
2727 if (relocatable)
2728 reloc_entry->address += input_section->output_offset;
2729
2730 return bfd_reloc_ok;
2731 }
2732 \f
2733 /* Swap an entry in a .gptab section. Note that these routines rely
2734 on the equivalence of the two elements of the union. */
2735
2736 static void
2737 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2738 Elf32_gptab *in)
2739 {
2740 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2741 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2742 }
2743
2744 static void
2745 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2746 Elf32_External_gptab *ex)
2747 {
2748 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2749 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2750 }
2751
2752 static void
2753 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2754 Elf32_External_compact_rel *ex)
2755 {
2756 H_PUT_32 (abfd, in->id1, ex->id1);
2757 H_PUT_32 (abfd, in->num, ex->num);
2758 H_PUT_32 (abfd, in->id2, ex->id2);
2759 H_PUT_32 (abfd, in->offset, ex->offset);
2760 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2761 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2762 }
2763
2764 static void
2765 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2766 Elf32_External_crinfo *ex)
2767 {
2768 unsigned long l;
2769
2770 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2771 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2772 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2773 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2774 H_PUT_32 (abfd, l, ex->info);
2775 H_PUT_32 (abfd, in->konst, ex->konst);
2776 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2777 }
2778 \f
2779 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2780 routines swap this structure in and out. They are used outside of
2781 BFD, so they are globally visible. */
2782
2783 void
2784 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2785 Elf32_RegInfo *in)
2786 {
2787 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2788 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2789 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2790 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2791 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2792 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2793 }
2794
2795 void
2796 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2797 Elf32_External_RegInfo *ex)
2798 {
2799 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2800 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2801 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2802 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2803 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2804 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2805 }
2806
2807 /* In the 64 bit ABI, the .MIPS.options section holds register
2808 information in an Elf64_Reginfo structure. These routines swap
2809 them in and out. They are globally visible because they are used
2810 outside of BFD. These routines are here so that gas can call them
2811 without worrying about whether the 64 bit ABI has been included. */
2812
2813 void
2814 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2815 Elf64_Internal_RegInfo *in)
2816 {
2817 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2818 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2819 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2820 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2821 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2822 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2823 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2824 }
2825
2826 void
2827 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2828 Elf64_External_RegInfo *ex)
2829 {
2830 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2831 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2832 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2833 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2834 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2835 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2836 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2837 }
2838
2839 /* Swap in an options header. */
2840
2841 void
2842 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2843 Elf_Internal_Options *in)
2844 {
2845 in->kind = H_GET_8 (abfd, ex->kind);
2846 in->size = H_GET_8 (abfd, ex->size);
2847 in->section = H_GET_16 (abfd, ex->section);
2848 in->info = H_GET_32 (abfd, ex->info);
2849 }
2850
2851 /* Swap out an options header. */
2852
2853 void
2854 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2855 Elf_External_Options *ex)
2856 {
2857 H_PUT_8 (abfd, in->kind, ex->kind);
2858 H_PUT_8 (abfd, in->size, ex->size);
2859 H_PUT_16 (abfd, in->section, ex->section);
2860 H_PUT_32 (abfd, in->info, ex->info);
2861 }
2862
2863 /* Swap in an abiflags structure. */
2864
2865 void
2866 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2867 const Elf_External_ABIFlags_v0 *ex,
2868 Elf_Internal_ABIFlags_v0 *in)
2869 {
2870 in->version = H_GET_16 (abfd, ex->version);
2871 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2872 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2873 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2874 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2875 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2876 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2877 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2878 in->ases = H_GET_32 (abfd, ex->ases);
2879 in->flags1 = H_GET_32 (abfd, ex->flags1);
2880 in->flags2 = H_GET_32 (abfd, ex->flags2);
2881 }
2882
2883 /* Swap out an abiflags structure. */
2884
2885 void
2886 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2887 const Elf_Internal_ABIFlags_v0 *in,
2888 Elf_External_ABIFlags_v0 *ex)
2889 {
2890 H_PUT_16 (abfd, in->version, ex->version);
2891 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2892 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2893 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2894 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2895 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2896 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2897 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2898 H_PUT_32 (abfd, in->ases, ex->ases);
2899 H_PUT_32 (abfd, in->flags1, ex->flags1);
2900 H_PUT_32 (abfd, in->flags2, ex->flags2);
2901 }
2902 \f
2903 /* This function is called via qsort() to sort the dynamic relocation
2904 entries by increasing r_symndx value. */
2905
2906 static int
2907 sort_dynamic_relocs (const void *arg1, const void *arg2)
2908 {
2909 Elf_Internal_Rela int_reloc1;
2910 Elf_Internal_Rela int_reloc2;
2911 int diff;
2912
2913 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2914 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2915
2916 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2917 if (diff != 0)
2918 return diff;
2919
2920 if (int_reloc1.r_offset < int_reloc2.r_offset)
2921 return -1;
2922 if (int_reloc1.r_offset > int_reloc2.r_offset)
2923 return 1;
2924 return 0;
2925 }
2926
2927 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2928
2929 static int
2930 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2931 const void *arg2 ATTRIBUTE_UNUSED)
2932 {
2933 #ifdef BFD64
2934 Elf_Internal_Rela int_reloc1[3];
2935 Elf_Internal_Rela int_reloc2[3];
2936
2937 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2938 (reldyn_sorting_bfd, arg1, int_reloc1);
2939 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2940 (reldyn_sorting_bfd, arg2, int_reloc2);
2941
2942 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2943 return -1;
2944 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2945 return 1;
2946
2947 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2948 return -1;
2949 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2950 return 1;
2951 return 0;
2952 #else
2953 abort ();
2954 #endif
2955 }
2956
2957
2958 /* This routine is used to write out ECOFF debugging external symbol
2959 information. It is called via mips_elf_link_hash_traverse. The
2960 ECOFF external symbol information must match the ELF external
2961 symbol information. Unfortunately, at this point we don't know
2962 whether a symbol is required by reloc information, so the two
2963 tables may wind up being different. We must sort out the external
2964 symbol information before we can set the final size of the .mdebug
2965 section, and we must set the size of the .mdebug section before we
2966 can relocate any sections, and we can't know which symbols are
2967 required by relocation until we relocate the sections.
2968 Fortunately, it is relatively unlikely that any symbol will be
2969 stripped but required by a reloc. In particular, it can not happen
2970 when generating a final executable. */
2971
2972 static bool
2973 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2974 {
2975 struct extsym_info *einfo = data;
2976 bool strip;
2977 asection *sec, *output_section;
2978
2979 if (h->root.indx == -2)
2980 strip = false;
2981 else if ((h->root.def_dynamic
2982 || h->root.ref_dynamic
2983 || h->root.type == bfd_link_hash_new)
2984 && !h->root.def_regular
2985 && !h->root.ref_regular)
2986 strip = true;
2987 else if (einfo->info->strip == strip_all
2988 || (einfo->info->strip == strip_some
2989 && bfd_hash_lookup (einfo->info->keep_hash,
2990 h->root.root.root.string,
2991 false, false) == NULL))
2992 strip = true;
2993 else
2994 strip = false;
2995
2996 if (strip)
2997 return true;
2998
2999 if (h->esym.ifd == -2)
3000 {
3001 h->esym.jmptbl = 0;
3002 h->esym.cobol_main = 0;
3003 h->esym.weakext = 0;
3004 h->esym.reserved = 0;
3005 h->esym.ifd = ifdNil;
3006 h->esym.asym.value = 0;
3007 h->esym.asym.st = stGlobal;
3008
3009 if (h->root.root.type == bfd_link_hash_undefined
3010 || h->root.root.type == bfd_link_hash_undefweak)
3011 {
3012 const char *name;
3013
3014 /* Use undefined class. Also, set class and type for some
3015 special symbols. */
3016 name = h->root.root.root.string;
3017 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
3018 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
3019 {
3020 h->esym.asym.sc = scData;
3021 h->esym.asym.st = stLabel;
3022 h->esym.asym.value = 0;
3023 }
3024 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3025 {
3026 h->esym.asym.sc = scAbs;
3027 h->esym.asym.st = stLabel;
3028 h->esym.asym.value =
3029 mips_elf_hash_table (einfo->info)->procedure_count;
3030 }
3031 else
3032 h->esym.asym.sc = scUndefined;
3033 }
3034 else if (h->root.root.type != bfd_link_hash_defined
3035 && h->root.root.type != bfd_link_hash_defweak)
3036 h->esym.asym.sc = scAbs;
3037 else
3038 {
3039 const char *name;
3040
3041 sec = h->root.root.u.def.section;
3042 output_section = sec->output_section;
3043
3044 /* When making a shared library and symbol h is the one from
3045 the another shared library, OUTPUT_SECTION may be null. */
3046 if (output_section == NULL)
3047 h->esym.asym.sc = scUndefined;
3048 else
3049 {
3050 name = bfd_section_name (output_section);
3051
3052 if (strcmp (name, ".text") == 0)
3053 h->esym.asym.sc = scText;
3054 else if (strcmp (name, ".data") == 0)
3055 h->esym.asym.sc = scData;
3056 else if (strcmp (name, ".sdata") == 0)
3057 h->esym.asym.sc = scSData;
3058 else if (strcmp (name, ".rodata") == 0
3059 || strcmp (name, ".rdata") == 0)
3060 h->esym.asym.sc = scRData;
3061 else if (strcmp (name, ".bss") == 0)
3062 h->esym.asym.sc = scBss;
3063 else if (strcmp (name, ".sbss") == 0)
3064 h->esym.asym.sc = scSBss;
3065 else if (strcmp (name, ".init") == 0)
3066 h->esym.asym.sc = scInit;
3067 else if (strcmp (name, ".fini") == 0)
3068 h->esym.asym.sc = scFini;
3069 else
3070 h->esym.asym.sc = scAbs;
3071 }
3072 }
3073
3074 h->esym.asym.reserved = 0;
3075 h->esym.asym.index = indexNil;
3076 }
3077
3078 if (h->root.root.type == bfd_link_hash_common)
3079 h->esym.asym.value = h->root.root.u.c.size;
3080 else if (h->root.root.type == bfd_link_hash_defined
3081 || h->root.root.type == bfd_link_hash_defweak)
3082 {
3083 if (h->esym.asym.sc == scCommon)
3084 h->esym.asym.sc = scBss;
3085 else if (h->esym.asym.sc == scSCommon)
3086 h->esym.asym.sc = scSBss;
3087
3088 sec = h->root.root.u.def.section;
3089 output_section = sec->output_section;
3090 if (output_section != NULL)
3091 h->esym.asym.value = (h->root.root.u.def.value
3092 + sec->output_offset
3093 + output_section->vma);
3094 else
3095 h->esym.asym.value = 0;
3096 }
3097 else
3098 {
3099 struct mips_elf_link_hash_entry *hd = h;
3100
3101 while (hd->root.root.type == bfd_link_hash_indirect)
3102 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3103
3104 if (hd->needs_lazy_stub)
3105 {
3106 BFD_ASSERT (hd->root.plt.plist != NULL);
3107 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3108 /* Set type and value for a symbol with a function stub. */
3109 h->esym.asym.st = stProc;
3110 sec = hd->root.root.u.def.section;
3111 if (sec == NULL)
3112 h->esym.asym.value = 0;
3113 else
3114 {
3115 output_section = sec->output_section;
3116 if (output_section != NULL)
3117 h->esym.asym.value = (hd->root.plt.plist->stub_offset
3118 + sec->output_offset
3119 + output_section->vma);
3120 else
3121 h->esym.asym.value = 0;
3122 }
3123 }
3124 }
3125
3126 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3127 h->root.root.root.string,
3128 &h->esym))
3129 {
3130 einfo->failed = true;
3131 return false;
3132 }
3133
3134 return true;
3135 }
3136
3137 /* A comparison routine used to sort .gptab entries. */
3138
3139 static int
3140 gptab_compare (const void *p1, const void *p2)
3141 {
3142 const Elf32_gptab *a1 = p1;
3143 const Elf32_gptab *a2 = p2;
3144
3145 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3146 }
3147 \f
3148 /* Functions to manage the got entry hash table. */
3149
3150 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3151 hash number. */
3152
3153 static inline hashval_t
3154 mips_elf_hash_bfd_vma (bfd_vma addr)
3155 {
3156 #ifdef BFD64
3157 return addr + (addr >> 32);
3158 #else
3159 return addr;
3160 #endif
3161 }
3162
3163 static hashval_t
3164 mips_elf_got_entry_hash (const void *entry_)
3165 {
3166 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3167
3168 return (entry->symndx
3169 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3170 + (entry->tls_type == GOT_TLS_LDM ? 0
3171 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3172 : entry->symndx >= 0 ? (entry->abfd->id
3173 + mips_elf_hash_bfd_vma (entry->d.addend))
3174 : entry->d.h->root.root.root.hash));
3175 }
3176
3177 static int
3178 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3179 {
3180 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3181 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3182
3183 return (e1->symndx == e2->symndx
3184 && e1->tls_type == e2->tls_type
3185 && (e1->tls_type == GOT_TLS_LDM ? true
3186 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3187 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3188 && e1->d.addend == e2->d.addend)
3189 : e2->abfd && e1->d.h == e2->d.h));
3190 }
3191
3192 static hashval_t
3193 mips_got_page_ref_hash (const void *ref_)
3194 {
3195 const struct mips_got_page_ref *ref;
3196
3197 ref = (const struct mips_got_page_ref *) ref_;
3198 return ((ref->symndx >= 0
3199 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3200 : ref->u.h->root.root.root.hash)
3201 + mips_elf_hash_bfd_vma (ref->addend));
3202 }
3203
3204 static int
3205 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3206 {
3207 const struct mips_got_page_ref *ref1, *ref2;
3208
3209 ref1 = (const struct mips_got_page_ref *) ref1_;
3210 ref2 = (const struct mips_got_page_ref *) ref2_;
3211 return (ref1->symndx == ref2->symndx
3212 && (ref1->symndx < 0
3213 ? ref1->u.h == ref2->u.h
3214 : ref1->u.abfd == ref2->u.abfd)
3215 && ref1->addend == ref2->addend);
3216 }
3217
3218 static hashval_t
3219 mips_got_page_entry_hash (const void *entry_)
3220 {
3221 const struct mips_got_page_entry *entry;
3222
3223 entry = (const struct mips_got_page_entry *) entry_;
3224 return entry->sec->id;
3225 }
3226
3227 static int
3228 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3229 {
3230 const struct mips_got_page_entry *entry1, *entry2;
3231
3232 entry1 = (const struct mips_got_page_entry *) entry1_;
3233 entry2 = (const struct mips_got_page_entry *) entry2_;
3234 return entry1->sec == entry2->sec;
3235 }
3236 \f
3237 /* Create and return a new mips_got_info structure. */
3238
3239 static struct mips_got_info *
3240 mips_elf_create_got_info (bfd *abfd)
3241 {
3242 struct mips_got_info *g;
3243
3244 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3245 if (g == NULL)
3246 return NULL;
3247
3248 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3249 mips_elf_got_entry_eq, NULL);
3250 if (g->got_entries == NULL)
3251 return NULL;
3252
3253 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3254 mips_got_page_ref_eq, NULL);
3255 if (g->got_page_refs == NULL)
3256 return NULL;
3257
3258 return g;
3259 }
3260
3261 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3262 CREATE_P and if ABFD doesn't already have a GOT. */
3263
3264 static struct mips_got_info *
3265 mips_elf_bfd_got (bfd *abfd, bool create_p)
3266 {
3267 struct mips_elf_obj_tdata *tdata;
3268
3269 if (!is_mips_elf (abfd))
3270 return NULL;
3271
3272 tdata = mips_elf_tdata (abfd);
3273 if (!tdata->got && create_p)
3274 tdata->got = mips_elf_create_got_info (abfd);
3275 return tdata->got;
3276 }
3277
3278 /* Record that ABFD should use output GOT G. */
3279
3280 static void
3281 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3282 {
3283 struct mips_elf_obj_tdata *tdata;
3284
3285 BFD_ASSERT (is_mips_elf (abfd));
3286 tdata = mips_elf_tdata (abfd);
3287 if (tdata->got)
3288 {
3289 /* The GOT structure itself and the hash table entries are
3290 allocated to a bfd, but the hash tables aren't. */
3291 htab_delete (tdata->got->got_entries);
3292 htab_delete (tdata->got->got_page_refs);
3293 if (tdata->got->got_page_entries)
3294 htab_delete (tdata->got->got_page_entries);
3295 }
3296 tdata->got = g;
3297 }
3298
3299 /* Return the dynamic relocation section. If it doesn't exist, try to
3300 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3301 if creation fails. */
3302
3303 static asection *
3304 mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3305 {
3306 const char *dname;
3307 asection *sreloc;
3308 bfd *dynobj;
3309
3310 dname = MIPS_ELF_REL_DYN_NAME (info);
3311 dynobj = elf_hash_table (info)->dynobj;
3312 sreloc = bfd_get_linker_section (dynobj, dname);
3313 if (sreloc == NULL && create_p)
3314 {
3315 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3316 (SEC_ALLOC
3317 | SEC_LOAD
3318 | SEC_HAS_CONTENTS
3319 | SEC_IN_MEMORY
3320 | SEC_LINKER_CREATED
3321 | SEC_READONLY));
3322 if (sreloc == NULL
3323 || !bfd_set_section_alignment (sreloc,
3324 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3325 return NULL;
3326 }
3327 return sreloc;
3328 }
3329
3330 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3331
3332 static int
3333 mips_elf_reloc_tls_type (unsigned int r_type)
3334 {
3335 if (tls_gd_reloc_p (r_type))
3336 return GOT_TLS_GD;
3337
3338 if (tls_ldm_reloc_p (r_type))
3339 return GOT_TLS_LDM;
3340
3341 if (tls_gottprel_reloc_p (r_type))
3342 return GOT_TLS_IE;
3343
3344 return GOT_TLS_NONE;
3345 }
3346
3347 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3348
3349 static int
3350 mips_tls_got_entries (unsigned int type)
3351 {
3352 switch (type)
3353 {
3354 case GOT_TLS_GD:
3355 case GOT_TLS_LDM:
3356 return 2;
3357
3358 case GOT_TLS_IE:
3359 return 1;
3360
3361 case GOT_TLS_NONE:
3362 return 0;
3363 }
3364 abort ();
3365 }
3366
3367 /* Count the number of relocations needed for a TLS GOT entry, with
3368 access types from TLS_TYPE, and symbol H (or a local symbol if H
3369 is NULL). */
3370
3371 static int
3372 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3373 struct elf_link_hash_entry *h)
3374 {
3375 int indx = 0;
3376 bool need_relocs = false;
3377 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3378
3379 if (h != NULL
3380 && h->dynindx != -1
3381 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3382 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3383 indx = h->dynindx;
3384
3385 if ((bfd_link_dll (info) || indx != 0)
3386 && (h == NULL
3387 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3388 || h->root.type != bfd_link_hash_undefweak))
3389 need_relocs = true;
3390
3391 if (!need_relocs)
3392 return 0;
3393
3394 switch (tls_type)
3395 {
3396 case GOT_TLS_GD:
3397 return indx != 0 ? 2 : 1;
3398
3399 case GOT_TLS_IE:
3400 return 1;
3401
3402 case GOT_TLS_LDM:
3403 return bfd_link_dll (info) ? 1 : 0;
3404
3405 default:
3406 return 0;
3407 }
3408 }
3409
3410 /* Add the number of GOT entries and TLS relocations required by ENTRY
3411 to G. */
3412
3413 static void
3414 mips_elf_count_got_entry (struct bfd_link_info *info,
3415 struct mips_got_info *g,
3416 struct mips_got_entry *entry)
3417 {
3418 if (entry->tls_type)
3419 {
3420 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3421 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3422 entry->symndx < 0
3423 ? &entry->d.h->root : NULL);
3424 }
3425 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3426 g->local_gotno += 1;
3427 else
3428 g->global_gotno += 1;
3429 }
3430
3431 /* Output a simple dynamic relocation into SRELOC. */
3432
3433 static void
3434 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3435 asection *sreloc,
3436 unsigned long reloc_index,
3437 unsigned long indx,
3438 int r_type,
3439 bfd_vma offset)
3440 {
3441 Elf_Internal_Rela rel[3];
3442
3443 memset (rel, 0, sizeof (rel));
3444
3445 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3446 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3447
3448 if (ABI_64_P (output_bfd))
3449 {
3450 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3451 (output_bfd, &rel[0],
3452 (sreloc->contents
3453 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3454 }
3455 else
3456 bfd_elf32_swap_reloc_out
3457 (output_bfd, &rel[0],
3458 (sreloc->contents
3459 + reloc_index * sizeof (Elf32_External_Rel)));
3460 }
3461
3462 /* Initialize a set of TLS GOT entries for one symbol. */
3463
3464 static void
3465 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3466 struct mips_got_entry *entry,
3467 struct mips_elf_link_hash_entry *h,
3468 bfd_vma value)
3469 {
3470 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3471 struct mips_elf_link_hash_table *htab;
3472 int indx;
3473 asection *sreloc, *sgot;
3474 bfd_vma got_offset, got_offset2;
3475 bool need_relocs = false;
3476
3477 htab = mips_elf_hash_table (info);
3478 if (htab == NULL)
3479 return;
3480
3481 sgot = htab->root.sgot;
3482
3483 indx = 0;
3484 if (h != NULL
3485 && h->root.dynindx != -1
3486 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3487 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3488 indx = h->root.dynindx;
3489
3490 if (entry->tls_initialized)
3491 return;
3492
3493 if ((bfd_link_dll (info) || indx != 0)
3494 && (h == NULL
3495 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3496 || h->root.type != bfd_link_hash_undefweak))
3497 need_relocs = true;
3498
3499 /* MINUS_ONE means the symbol is not defined in this object. It may not
3500 be defined at all; assume that the value doesn't matter in that
3501 case. Otherwise complain if we would use the value. */
3502 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3503 || h->root.root.type == bfd_link_hash_undefweak);
3504
3505 /* Emit necessary relocations. */
3506 sreloc = mips_elf_rel_dyn_section (info, false);
3507 got_offset = entry->gotidx;
3508
3509 switch (entry->tls_type)
3510 {
3511 case GOT_TLS_GD:
3512 /* General Dynamic. */
3513 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3514
3515 if (need_relocs)
3516 {
3517 mips_elf_output_dynamic_relocation
3518 (abfd, sreloc, sreloc->reloc_count++, indx,
3519 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3520 sgot->output_offset + sgot->output_section->vma + got_offset);
3521
3522 if (indx)
3523 mips_elf_output_dynamic_relocation
3524 (abfd, sreloc, sreloc->reloc_count++, indx,
3525 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3526 sgot->output_offset + sgot->output_section->vma + got_offset2);
3527 else
3528 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3529 sgot->contents + got_offset2);
3530 }
3531 else
3532 {
3533 MIPS_ELF_PUT_WORD (abfd, 1,
3534 sgot->contents + got_offset);
3535 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3536 sgot->contents + got_offset2);
3537 }
3538 break;
3539
3540 case GOT_TLS_IE:
3541 /* Initial Exec model. */
3542 if (need_relocs)
3543 {
3544 if (indx == 0)
3545 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3546 sgot->contents + got_offset);
3547 else
3548 MIPS_ELF_PUT_WORD (abfd, 0,
3549 sgot->contents + got_offset);
3550
3551 mips_elf_output_dynamic_relocation
3552 (abfd, sreloc, sreloc->reloc_count++, indx,
3553 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3554 sgot->output_offset + sgot->output_section->vma + got_offset);
3555 }
3556 else
3557 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3558 sgot->contents + got_offset);
3559 break;
3560
3561 case GOT_TLS_LDM:
3562 /* The initial offset is zero, and the LD offsets will include the
3563 bias by DTP_OFFSET. */
3564 MIPS_ELF_PUT_WORD (abfd, 0,
3565 sgot->contents + got_offset
3566 + MIPS_ELF_GOT_SIZE (abfd));
3567
3568 if (!bfd_link_dll (info))
3569 MIPS_ELF_PUT_WORD (abfd, 1,
3570 sgot->contents + got_offset);
3571 else
3572 mips_elf_output_dynamic_relocation
3573 (abfd, sreloc, sreloc->reloc_count++, indx,
3574 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3575 sgot->output_offset + sgot->output_section->vma + got_offset);
3576 break;
3577
3578 default:
3579 abort ();
3580 }
3581
3582 entry->tls_initialized = true;
3583 }
3584
3585 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3586 for global symbol H. .got.plt comes before the GOT, so the offset
3587 will be negative. */
3588
3589 static bfd_vma
3590 mips_elf_gotplt_index (struct bfd_link_info *info,
3591 struct elf_link_hash_entry *h)
3592 {
3593 bfd_vma got_address, got_value;
3594 struct mips_elf_link_hash_table *htab;
3595
3596 htab = mips_elf_hash_table (info);
3597 BFD_ASSERT (htab != NULL);
3598
3599 BFD_ASSERT (h->plt.plist != NULL);
3600 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3601
3602 /* Calculate the address of the associated .got.plt entry. */
3603 got_address = (htab->root.sgotplt->output_section->vma
3604 + htab->root.sgotplt->output_offset
3605 + (h->plt.plist->gotplt_index
3606 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3607
3608 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3609 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3610 + htab->root.hgot->root.u.def.section->output_offset
3611 + htab->root.hgot->root.u.def.value);
3612
3613 return got_address - got_value;
3614 }
3615
3616 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3617 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3618 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3619 offset can be found. */
3620
3621 static bfd_vma
3622 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3623 bfd_vma value, unsigned long r_symndx,
3624 struct mips_elf_link_hash_entry *h, int r_type)
3625 {
3626 struct mips_elf_link_hash_table *htab;
3627 struct mips_got_entry *entry;
3628
3629 htab = mips_elf_hash_table (info);
3630 BFD_ASSERT (htab != NULL);
3631
3632 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3633 r_symndx, h, r_type);
3634 if (!entry)
3635 return MINUS_ONE;
3636
3637 if (entry->tls_type)
3638 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3639 return entry->gotidx;
3640 }
3641
3642 /* Return the GOT index of global symbol H in the primary GOT. */
3643
3644 static bfd_vma
3645 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3646 struct elf_link_hash_entry *h)
3647 {
3648 struct mips_elf_link_hash_table *htab;
3649 long global_got_dynindx;
3650 struct mips_got_info *g;
3651 bfd_vma got_index;
3652
3653 htab = mips_elf_hash_table (info);
3654 BFD_ASSERT (htab != NULL);
3655
3656 global_got_dynindx = 0;
3657 if (htab->global_gotsym != NULL)
3658 global_got_dynindx = htab->global_gotsym->dynindx;
3659
3660 /* Once we determine the global GOT entry with the lowest dynamic
3661 symbol table index, we must put all dynamic symbols with greater
3662 indices into the primary GOT. That makes it easy to calculate the
3663 GOT offset. */
3664 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3665 g = mips_elf_bfd_got (obfd, false);
3666 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3667 * MIPS_ELF_GOT_SIZE (obfd));
3668 BFD_ASSERT (got_index < htab->root.sgot->size);
3669
3670 return got_index;
3671 }
3672
3673 /* Return the GOT index for the global symbol indicated by H, which is
3674 referenced by a relocation of type R_TYPE in IBFD. */
3675
3676 static bfd_vma
3677 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3678 struct elf_link_hash_entry *h, int r_type)
3679 {
3680 struct mips_elf_link_hash_table *htab;
3681 struct mips_got_info *g;
3682 struct mips_got_entry lookup, *entry;
3683 bfd_vma gotidx;
3684
3685 htab = mips_elf_hash_table (info);
3686 BFD_ASSERT (htab != NULL);
3687
3688 g = mips_elf_bfd_got (ibfd, false);
3689 BFD_ASSERT (g);
3690
3691 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3692 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3693 return mips_elf_primary_global_got_index (obfd, info, h);
3694
3695 lookup.abfd = ibfd;
3696 lookup.symndx = -1;
3697 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3698 entry = htab_find (g->got_entries, &lookup);
3699 BFD_ASSERT (entry);
3700
3701 gotidx = entry->gotidx;
3702 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3703
3704 if (lookup.tls_type)
3705 {
3706 bfd_vma value = MINUS_ONE;
3707
3708 if ((h->root.type == bfd_link_hash_defined
3709 || h->root.type == bfd_link_hash_defweak)
3710 && h->root.u.def.section->output_section)
3711 value = (h->root.u.def.value
3712 + h->root.u.def.section->output_offset
3713 + h->root.u.def.section->output_section->vma);
3714
3715 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3716 }
3717 return gotidx;
3718 }
3719
3720 /* Find a GOT page entry that points to within 32KB of VALUE. These
3721 entries are supposed to be placed at small offsets in the GOT, i.e.,
3722 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3723 entry could be created. If OFFSETP is nonnull, use it to return the
3724 offset of the GOT entry from VALUE. */
3725
3726 static bfd_vma
3727 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3728 bfd_vma value, bfd_vma *offsetp)
3729 {
3730 bfd_vma page, got_index;
3731 struct mips_got_entry *entry;
3732
3733 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3734 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3735 NULL, R_MIPS_GOT_PAGE);
3736
3737 if (!entry)
3738 return MINUS_ONE;
3739
3740 got_index = entry->gotidx;
3741
3742 if (offsetp)
3743 *offsetp = value - entry->d.address;
3744
3745 return got_index;
3746 }
3747
3748 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3749 EXTERNAL is true if the relocation was originally against a global
3750 symbol that binds locally. */
3751
3752 static bfd_vma
3753 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3754 bfd_vma value, bool external)
3755 {
3756 struct mips_got_entry *entry;
3757
3758 /* GOT16 relocations against local symbols are followed by a LO16
3759 relocation; those against global symbols are not. Thus if the
3760 symbol was originally local, the GOT16 relocation should load the
3761 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3762 if (! external)
3763 value = mips_elf_high (value) << 16;
3764
3765 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3766 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3767 same in all cases. */
3768 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3769 NULL, R_MIPS_GOT16);
3770 if (entry)
3771 return entry->gotidx;
3772 else
3773 return MINUS_ONE;
3774 }
3775
3776 /* Returns the offset for the entry at the INDEXth position
3777 in the GOT. */
3778
3779 static bfd_vma
3780 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3781 bfd *input_bfd, bfd_vma got_index)
3782 {
3783 struct mips_elf_link_hash_table *htab;
3784 asection *sgot;
3785 bfd_vma gp;
3786
3787 htab = mips_elf_hash_table (info);
3788 BFD_ASSERT (htab != NULL);
3789
3790 sgot = htab->root.sgot;
3791 gp = _bfd_get_gp_value (output_bfd)
3792 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3793
3794 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3795 }
3796
3797 /* Create and return a local GOT entry for VALUE, which was calculated
3798 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3799 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3800 instead. */
3801
3802 static struct mips_got_entry *
3803 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3804 bfd *ibfd, bfd_vma value,
3805 unsigned long r_symndx,
3806 struct mips_elf_link_hash_entry *h,
3807 int r_type)
3808 {
3809 struct mips_got_entry lookup, *entry;
3810 void **loc;
3811 struct mips_got_info *g;
3812 struct mips_elf_link_hash_table *htab;
3813 bfd_vma gotidx;
3814
3815 htab = mips_elf_hash_table (info);
3816 BFD_ASSERT (htab != NULL);
3817
3818 g = mips_elf_bfd_got (ibfd, false);
3819 if (g == NULL)
3820 {
3821 g = mips_elf_bfd_got (abfd, false);
3822 BFD_ASSERT (g != NULL);
3823 }
3824
3825 /* This function shouldn't be called for symbols that live in the global
3826 area of the GOT. */
3827 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3828
3829 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3830 if (lookup.tls_type)
3831 {
3832 lookup.abfd = ibfd;
3833 if (tls_ldm_reloc_p (r_type))
3834 {
3835 lookup.symndx = 0;
3836 lookup.d.addend = 0;
3837 }
3838 else if (h == NULL)
3839 {
3840 lookup.symndx = r_symndx;
3841 lookup.d.addend = 0;
3842 }
3843 else
3844 {
3845 lookup.symndx = -1;
3846 lookup.d.h = h;
3847 }
3848
3849 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3850 BFD_ASSERT (entry);
3851
3852 gotidx = entry->gotidx;
3853 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3854
3855 return entry;
3856 }
3857
3858 lookup.abfd = NULL;
3859 lookup.symndx = -1;
3860 lookup.d.address = value;
3861 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3862 if (!loc)
3863 return NULL;
3864
3865 entry = (struct mips_got_entry *) *loc;
3866 if (entry)
3867 return entry;
3868
3869 if (g->assigned_low_gotno > g->assigned_high_gotno)
3870 {
3871 /* We didn't allocate enough space in the GOT. */
3872 _bfd_error_handler
3873 (_("not enough GOT space for local GOT entries"));
3874 bfd_set_error (bfd_error_bad_value);
3875 return NULL;
3876 }
3877
3878 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3879 if (!entry)
3880 return NULL;
3881
3882 if (got16_reloc_p (r_type)
3883 || call16_reloc_p (r_type)
3884 || got_page_reloc_p (r_type)
3885 || got_disp_reloc_p (r_type))
3886 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3887 else
3888 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3889
3890 *entry = lookup;
3891 *loc = entry;
3892
3893 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3894
3895 /* These GOT entries need a dynamic relocation on VxWorks. */
3896 if (htab->root.target_os == is_vxworks)
3897 {
3898 Elf_Internal_Rela outrel;
3899 asection *s;
3900 bfd_byte *rloc;
3901 bfd_vma got_address;
3902
3903 s = mips_elf_rel_dyn_section (info, false);
3904 got_address = (htab->root.sgot->output_section->vma
3905 + htab->root.sgot->output_offset
3906 + entry->gotidx);
3907
3908 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3909 outrel.r_offset = got_address;
3910 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3911 outrel.r_addend = value;
3912 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3913 }
3914
3915 return entry;
3916 }
3917
3918 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3919 The number might be exact or a worst-case estimate, depending on how
3920 much information is available to elf_backend_omit_section_dynsym at
3921 the current linking stage. */
3922
3923 static bfd_size_type
3924 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3925 {
3926 bfd_size_type count;
3927
3928 count = 0;
3929 if (bfd_link_pic (info)
3930 || elf_hash_table (info)->is_relocatable_executable)
3931 {
3932 asection *p;
3933 const struct elf_backend_data *bed;
3934
3935 bed = get_elf_backend_data (output_bfd);
3936 for (p = output_bfd->sections; p ; p = p->next)
3937 if ((p->flags & SEC_EXCLUDE) == 0
3938 && (p->flags & SEC_ALLOC) != 0
3939 && elf_hash_table (info)->dynamic_relocs
3940 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3941 ++count;
3942 }
3943 return count;
3944 }
3945
3946 /* Sort the dynamic symbol table so that symbols that need GOT entries
3947 appear towards the end. */
3948
3949 static bool
3950 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3951 {
3952 struct mips_elf_link_hash_table *htab;
3953 struct mips_elf_hash_sort_data hsd;
3954 struct mips_got_info *g;
3955
3956 htab = mips_elf_hash_table (info);
3957 BFD_ASSERT (htab != NULL);
3958
3959 if (htab->root.dynsymcount == 0)
3960 return true;
3961
3962 g = htab->got_info;
3963 if (g == NULL)
3964 return true;
3965
3966 hsd.low = NULL;
3967 hsd.max_unref_got_dynindx
3968 = hsd.min_got_dynindx
3969 = (htab->root.dynsymcount - g->reloc_only_gotno);
3970 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3971 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3972 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3973 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3974 hsd.output_bfd = abfd;
3975 if (htab->root.dynobj != NULL
3976 && htab->root.dynamic_sections_created
3977 && info->emit_gnu_hash)
3978 {
3979 asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3980 BFD_ASSERT (s != NULL);
3981 hsd.mipsxhash = s->contents;
3982 BFD_ASSERT (hsd.mipsxhash != NULL);
3983 }
3984 else
3985 hsd.mipsxhash = NULL;
3986 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3987
3988 /* There should have been enough room in the symbol table to
3989 accommodate both the GOT and non-GOT symbols. */
3990 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3991 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3992 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3993 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3994
3995 /* Now we know which dynamic symbol has the lowest dynamic symbol
3996 table index in the GOT. */
3997 htab->global_gotsym = hsd.low;
3998
3999 return true;
4000 }
4001
4002 /* If H needs a GOT entry, assign it the highest available dynamic
4003 index. Otherwise, assign it the lowest available dynamic
4004 index. */
4005
4006 static bool
4007 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
4008 {
4009 struct mips_elf_hash_sort_data *hsd = data;
4010
4011 /* Symbols without dynamic symbol table entries aren't interesting
4012 at all. */
4013 if (h->root.dynindx == -1)
4014 return true;
4015
4016 switch (h->global_got_area)
4017 {
4018 case GGA_NONE:
4019 if (h->root.forced_local)
4020 h->root.dynindx = hsd->max_local_dynindx++;
4021 else
4022 h->root.dynindx = hsd->max_non_got_dynindx++;
4023 break;
4024
4025 case GGA_NORMAL:
4026 h->root.dynindx = --hsd->min_got_dynindx;
4027 hsd->low = (struct elf_link_hash_entry *) h;
4028 break;
4029
4030 case GGA_RELOC_ONLY:
4031 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4032 hsd->low = (struct elf_link_hash_entry *) h;
4033 h->root.dynindx = hsd->max_unref_got_dynindx++;
4034 break;
4035 }
4036
4037 /* Populate the .MIPS.xhash translation table entry with
4038 the symbol dynindx. */
4039 if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4040 bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4041 hsd->mipsxhash + h->mipsxhash_loc);
4042
4043 return true;
4044 }
4045
4046 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4047 (which is owned by the caller and shouldn't be added to the
4048 hash table directly). */
4049
4050 static bool
4051 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4052 struct mips_got_entry *lookup)
4053 {
4054 struct mips_elf_link_hash_table *htab;
4055 struct mips_got_entry *entry;
4056 struct mips_got_info *g;
4057 void **loc, **bfd_loc;
4058
4059 /* Make sure there's a slot for this entry in the master GOT. */
4060 htab = mips_elf_hash_table (info);
4061 g = htab->got_info;
4062 loc = htab_find_slot (g->got_entries, lookup, INSERT);
4063 if (!loc)
4064 return false;
4065
4066 /* Populate the entry if it isn't already. */
4067 entry = (struct mips_got_entry *) *loc;
4068 if (!entry)
4069 {
4070 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4071 if (!entry)
4072 return false;
4073
4074 lookup->tls_initialized = false;
4075 lookup->gotidx = -1;
4076 *entry = *lookup;
4077 *loc = entry;
4078 }
4079
4080 /* Reuse the same GOT entry for the BFD's GOT. */
4081 g = mips_elf_bfd_got (abfd, true);
4082 if (!g)
4083 return false;
4084
4085 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4086 if (!bfd_loc)
4087 return false;
4088
4089 if (!*bfd_loc)
4090 *bfd_loc = entry;
4091 return true;
4092 }
4093
4094 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
4095 entry for it. FOR_CALL is true if the caller is only interested in
4096 using the GOT entry for calls. */
4097
4098 static bool
4099 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4100 bfd *abfd, struct bfd_link_info *info,
4101 bool for_call, int r_type)
4102 {
4103 struct mips_elf_link_hash_table *htab;
4104 struct mips_elf_link_hash_entry *hmips;
4105 struct mips_got_entry entry;
4106 unsigned char tls_type;
4107
4108 htab = mips_elf_hash_table (info);
4109 BFD_ASSERT (htab != NULL);
4110
4111 hmips = (struct mips_elf_link_hash_entry *) h;
4112 if (!for_call)
4113 hmips->got_only_for_calls = false;
4114
4115 /* A global symbol in the GOT must also be in the dynamic symbol
4116 table. */
4117 if (h->dynindx == -1)
4118 {
4119 switch (ELF_ST_VISIBILITY (h->other))
4120 {
4121 case STV_INTERNAL:
4122 case STV_HIDDEN:
4123 _bfd_mips_elf_hide_symbol (info, h, true);
4124 break;
4125 }
4126 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4127 return false;
4128 }
4129
4130 tls_type = mips_elf_reloc_tls_type (r_type);
4131 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4132 hmips->global_got_area = GGA_NORMAL;
4133
4134 entry.abfd = abfd;
4135 entry.symndx = -1;
4136 entry.d.h = (struct mips_elf_link_hash_entry *) h;
4137 entry.tls_type = tls_type;
4138 return mips_elf_record_got_entry (info, abfd, &entry);
4139 }
4140
4141 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4142 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
4143
4144 static bool
4145 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4146 struct bfd_link_info *info, int r_type)
4147 {
4148 struct mips_elf_link_hash_table *htab;
4149 struct mips_got_info *g;
4150 struct mips_got_entry entry;
4151
4152 htab = mips_elf_hash_table (info);
4153 BFD_ASSERT (htab != NULL);
4154
4155 g = htab->got_info;
4156 BFD_ASSERT (g != NULL);
4157
4158 entry.abfd = abfd;
4159 entry.symndx = symndx;
4160 entry.d.addend = addend;
4161 entry.tls_type = mips_elf_reloc_tls_type (r_type);
4162 return mips_elf_record_got_entry (info, abfd, &entry);
4163 }
4164
4165 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4166 H is the symbol's hash table entry, or null if SYMNDX is local
4167 to ABFD. */
4168
4169 static bool
4170 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4171 long symndx, struct elf_link_hash_entry *h,
4172 bfd_signed_vma addend)
4173 {
4174 struct mips_elf_link_hash_table *htab;
4175 struct mips_got_info *g1, *g2;
4176 struct mips_got_page_ref lookup, *entry;
4177 void **loc, **bfd_loc;
4178
4179 htab = mips_elf_hash_table (info);
4180 BFD_ASSERT (htab != NULL);
4181
4182 g1 = htab->got_info;
4183 BFD_ASSERT (g1 != NULL);
4184
4185 if (h)
4186 {
4187 lookup.symndx = -1;
4188 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4189 }
4190 else
4191 {
4192 lookup.symndx = symndx;
4193 lookup.u.abfd = abfd;
4194 }
4195 lookup.addend = addend;
4196 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4197 if (loc == NULL)
4198 return false;
4199
4200 entry = (struct mips_got_page_ref *) *loc;
4201 if (!entry)
4202 {
4203 entry = bfd_alloc (abfd, sizeof (*entry));
4204 if (!entry)
4205 return false;
4206
4207 *entry = lookup;
4208 *loc = entry;
4209 }
4210
4211 /* Add the same entry to the BFD's GOT. */
4212 g2 = mips_elf_bfd_got (abfd, true);
4213 if (!g2)
4214 return false;
4215
4216 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4217 if (!bfd_loc)
4218 return false;
4219
4220 if (!*bfd_loc)
4221 *bfd_loc = entry;
4222
4223 return true;
4224 }
4225
4226 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4227
4228 static void
4229 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4230 unsigned int n)
4231 {
4232 asection *s;
4233 struct mips_elf_link_hash_table *htab;
4234
4235 htab = mips_elf_hash_table (info);
4236 BFD_ASSERT (htab != NULL);
4237
4238 s = mips_elf_rel_dyn_section (info, false);
4239 BFD_ASSERT (s != NULL);
4240
4241 if (htab->root.target_os == is_vxworks)
4242 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4243 else
4244 {
4245 if (s->size == 0)
4246 {
4247 /* Make room for a null element. */
4248 s->size += MIPS_ELF_REL_SIZE (abfd);
4249 ++s->reloc_count;
4250 }
4251 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4252 }
4253 }
4254 \f
4255 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4256 mips_elf_traverse_got_arg structure. Count the number of GOT
4257 entries and TLS relocs. Set DATA->value to true if we need
4258 to resolve indirect or warning symbols and then recreate the GOT. */
4259
4260 static int
4261 mips_elf_check_recreate_got (void **entryp, void *data)
4262 {
4263 struct mips_got_entry *entry;
4264 struct mips_elf_traverse_got_arg *arg;
4265
4266 entry = (struct mips_got_entry *) *entryp;
4267 arg = (struct mips_elf_traverse_got_arg *) data;
4268 if (entry->abfd != NULL && entry->symndx == -1)
4269 {
4270 struct mips_elf_link_hash_entry *h;
4271
4272 h = entry->d.h;
4273 if (h->root.root.type == bfd_link_hash_indirect
4274 || h->root.root.type == bfd_link_hash_warning)
4275 {
4276 arg->value = true;
4277 return 0;
4278 }
4279 }
4280 mips_elf_count_got_entry (arg->info, arg->g, entry);
4281 return 1;
4282 }
4283
4284 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4285 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4286 converting entries for indirect and warning symbols into entries
4287 for the target symbol. Set DATA->g to null on error. */
4288
4289 static int
4290 mips_elf_recreate_got (void **entryp, void *data)
4291 {
4292 struct mips_got_entry new_entry, *entry;
4293 struct mips_elf_traverse_got_arg *arg;
4294 void **slot;
4295
4296 entry = (struct mips_got_entry *) *entryp;
4297 arg = (struct mips_elf_traverse_got_arg *) data;
4298 if (entry->abfd != NULL
4299 && entry->symndx == -1
4300 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4301 || entry->d.h->root.root.type == bfd_link_hash_warning))
4302 {
4303 struct mips_elf_link_hash_entry *h;
4304
4305 new_entry = *entry;
4306 entry = &new_entry;
4307 h = entry->d.h;
4308 do
4309 {
4310 BFD_ASSERT (h->global_got_area == GGA_NONE);
4311 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4312 }
4313 while (h->root.root.type == bfd_link_hash_indirect
4314 || h->root.root.type == bfd_link_hash_warning);
4315 entry->d.h = h;
4316 }
4317 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4318 if (slot == NULL)
4319 {
4320 arg->g = NULL;
4321 return 0;
4322 }
4323 if (*slot == NULL)
4324 {
4325 if (entry == &new_entry)
4326 {
4327 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4328 if (!entry)
4329 {
4330 arg->g = NULL;
4331 return 0;
4332 }
4333 *entry = new_entry;
4334 }
4335 *slot = entry;
4336 mips_elf_count_got_entry (arg->info, arg->g, entry);
4337 }
4338 return 1;
4339 }
4340
4341 /* Return the maximum number of GOT page entries required for RANGE. */
4342
4343 static bfd_vma
4344 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4345 {
4346 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4347 }
4348
4349 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4350
4351 static bool
4352 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4353 asection *sec, bfd_signed_vma addend)
4354 {
4355 struct mips_got_info *g = arg->g;
4356 struct mips_got_page_entry lookup, *entry;
4357 struct mips_got_page_range **range_ptr, *range;
4358 bfd_vma old_pages, new_pages;
4359 void **loc;
4360
4361 /* Find the mips_got_page_entry hash table entry for this section. */
4362 lookup.sec = sec;
4363 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4364 if (loc == NULL)
4365 return false;
4366
4367 /* Create a mips_got_page_entry if this is the first time we've
4368 seen the section. */
4369 entry = (struct mips_got_page_entry *) *loc;
4370 if (!entry)
4371 {
4372 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4373 if (!entry)
4374 return false;
4375
4376 entry->sec = sec;
4377 *loc = entry;
4378 }
4379
4380 /* Skip over ranges whose maximum extent cannot share a page entry
4381 with ADDEND. */
4382 range_ptr = &entry->ranges;
4383 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4384 range_ptr = &(*range_ptr)->next;
4385
4386 /* If we scanned to the end of the list, or found a range whose
4387 minimum extent cannot share a page entry with ADDEND, create
4388 a new singleton range. */
4389 range = *range_ptr;
4390 if (!range || addend < range->min_addend - 0xffff)
4391 {
4392 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4393 if (!range)
4394 return false;
4395
4396 range->next = *range_ptr;
4397 range->min_addend = addend;
4398 range->max_addend = addend;
4399
4400 *range_ptr = range;
4401 entry->num_pages++;
4402 g->page_gotno++;
4403 return true;
4404 }
4405
4406 /* Remember how many pages the old range contributed. */
4407 old_pages = mips_elf_pages_for_range (range);
4408
4409 /* Update the ranges. */
4410 if (addend < range->min_addend)
4411 range->min_addend = addend;
4412 else if (addend > range->max_addend)
4413 {
4414 if (range->next && addend >= range->next->min_addend - 0xffff)
4415 {
4416 old_pages += mips_elf_pages_for_range (range->next);
4417 range->max_addend = range->next->max_addend;
4418 range->next = range->next->next;
4419 }
4420 else
4421 range->max_addend = addend;
4422 }
4423
4424 /* Record any change in the total estimate. */
4425 new_pages = mips_elf_pages_for_range (range);
4426 if (old_pages != new_pages)
4427 {
4428 entry->num_pages += new_pages - old_pages;
4429 g->page_gotno += new_pages - old_pages;
4430 }
4431
4432 return true;
4433 }
4434
4435 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4436 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4437 whether the page reference described by *REFP needs a GOT page entry,
4438 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4439
4440 static int
4441 mips_elf_resolve_got_page_ref (void **refp, void *data)
4442 {
4443 struct mips_got_page_ref *ref;
4444 struct mips_elf_traverse_got_arg *arg;
4445 struct mips_elf_link_hash_table *htab;
4446 asection *sec;
4447 bfd_vma addend;
4448
4449 ref = (struct mips_got_page_ref *) *refp;
4450 arg = (struct mips_elf_traverse_got_arg *) data;
4451 htab = mips_elf_hash_table (arg->info);
4452
4453 if (ref->symndx < 0)
4454 {
4455 struct mips_elf_link_hash_entry *h;
4456
4457 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4458 h = ref->u.h;
4459 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4460 return 1;
4461
4462 /* Ignore undefined symbols; we'll issue an error later if
4463 appropriate. */
4464 if (!((h->root.root.type == bfd_link_hash_defined
4465 || h->root.root.type == bfd_link_hash_defweak)
4466 && h->root.root.u.def.section))
4467 return 1;
4468
4469 sec = h->root.root.u.def.section;
4470 addend = h->root.root.u.def.value + ref->addend;
4471 }
4472 else
4473 {
4474 Elf_Internal_Sym *isym;
4475
4476 /* Read in the symbol. */
4477 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4478 ref->symndx);
4479 if (isym == NULL)
4480 {
4481 arg->g = NULL;
4482 return 0;
4483 }
4484
4485 /* Get the associated input section. */
4486 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4487 if (sec == NULL)
4488 {
4489 arg->g = NULL;
4490 return 0;
4491 }
4492
4493 /* If this is a mergable section, work out the section and offset
4494 of the merged data. For section symbols, the addend specifies
4495 of the offset _of_ the first byte in the data, otherwise it
4496 specifies the offset _from_ the first byte. */
4497 if (sec->flags & SEC_MERGE)
4498 {
4499 void *secinfo;
4500
4501 secinfo = elf_section_data (sec)->sec_info;
4502 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4503 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4504 isym->st_value + ref->addend);
4505 else
4506 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4507 isym->st_value) + ref->addend;
4508 }
4509 else
4510 addend = isym->st_value + ref->addend;
4511 }
4512 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4513 {
4514 arg->g = NULL;
4515 return 0;
4516 }
4517 return 1;
4518 }
4519
4520 /* If any entries in G->got_entries are for indirect or warning symbols,
4521 replace them with entries for the target symbol. Convert g->got_page_refs
4522 into got_page_entry structures and estimate the number of page entries
4523 that they require. */
4524
4525 static bool
4526 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4527 struct mips_got_info *g)
4528 {
4529 struct mips_elf_traverse_got_arg tga;
4530 struct mips_got_info oldg;
4531
4532 oldg = *g;
4533
4534 tga.info = info;
4535 tga.g = g;
4536 tga.value = false;
4537 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4538 if (tga.value)
4539 {
4540 *g = oldg;
4541 g->got_entries = htab_create (htab_size (oldg.got_entries),
4542 mips_elf_got_entry_hash,
4543 mips_elf_got_entry_eq, NULL);
4544 if (!g->got_entries)
4545 return false;
4546
4547 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4548 if (!tga.g)
4549 return false;
4550
4551 htab_delete (oldg.got_entries);
4552 }
4553
4554 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4555 mips_got_page_entry_eq, NULL);
4556 if (g->got_page_entries == NULL)
4557 return false;
4558
4559 tga.info = info;
4560 tga.g = g;
4561 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4562
4563 return true;
4564 }
4565
4566 /* Return true if a GOT entry for H should live in the local rather than
4567 global GOT area. */
4568
4569 static bool
4570 mips_use_local_got_p (struct bfd_link_info *info,
4571 struct mips_elf_link_hash_entry *h)
4572 {
4573 /* Symbols that aren't in the dynamic symbol table must live in the
4574 local GOT. This includes symbols that are completely undefined
4575 and which therefore don't bind locally. We'll report undefined
4576 symbols later if appropriate. */
4577 if (h->root.dynindx == -1)
4578 return true;
4579
4580 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4581 to the local GOT, as they would be implicitly relocated by the
4582 base address by the dynamic loader. */
4583 if (bfd_is_abs_symbol (&h->root.root))
4584 return false;
4585
4586 /* Symbols that bind locally can (and in the case of forced-local
4587 symbols, must) live in the local GOT. */
4588 if (h->got_only_for_calls
4589 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4590 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4591 return true;
4592
4593 /* If this is an executable that must provide a definition of the symbol,
4594 either though PLTs or copy relocations, then that address should go in
4595 the local rather than global GOT. */
4596 if (bfd_link_executable (info) && h->has_static_relocs)
4597 return true;
4598
4599 return false;
4600 }
4601
4602 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4603 link_info structure. Decide whether the hash entry needs an entry in
4604 the global part of the primary GOT, setting global_got_area accordingly.
4605 Count the number of global symbols that are in the primary GOT only
4606 because they have relocations against them (reloc_only_gotno). */
4607
4608 static bool
4609 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4610 {
4611 struct bfd_link_info *info;
4612 struct mips_elf_link_hash_table *htab;
4613 struct mips_got_info *g;
4614
4615 info = (struct bfd_link_info *) data;
4616 htab = mips_elf_hash_table (info);
4617 g = htab->got_info;
4618 if (h->global_got_area != GGA_NONE)
4619 {
4620 /* Make a final decision about whether the symbol belongs in the
4621 local or global GOT. */
4622 if (mips_use_local_got_p (info, h))
4623 /* The symbol belongs in the local GOT. We no longer need this
4624 entry if it was only used for relocations; those relocations
4625 will be against the null or section symbol instead of H. */
4626 h->global_got_area = GGA_NONE;
4627 else if (htab->root.target_os == is_vxworks
4628 && h->got_only_for_calls
4629 && h->root.plt.plist->mips_offset != MINUS_ONE)
4630 /* On VxWorks, calls can refer directly to the .got.plt entry;
4631 they don't need entries in the regular GOT. .got.plt entries
4632 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4633 h->global_got_area = GGA_NONE;
4634 else if (h->global_got_area == GGA_RELOC_ONLY)
4635 {
4636 g->reloc_only_gotno++;
4637 g->global_gotno++;
4638 }
4639 }
4640 return 1;
4641 }
4642 \f
4643 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4644 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4645
4646 static int
4647 mips_elf_add_got_entry (void **entryp, void *data)
4648 {
4649 struct mips_got_entry *entry;
4650 struct mips_elf_traverse_got_arg *arg;
4651 void **slot;
4652
4653 entry = (struct mips_got_entry *) *entryp;
4654 arg = (struct mips_elf_traverse_got_arg *) data;
4655 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4656 if (!slot)
4657 {
4658 arg->g = NULL;
4659 return 0;
4660 }
4661 if (!*slot)
4662 {
4663 *slot = entry;
4664 mips_elf_count_got_entry (arg->info, arg->g, entry);
4665 }
4666 return 1;
4667 }
4668
4669 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4670 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4671
4672 static int
4673 mips_elf_add_got_page_entry (void **entryp, void *data)
4674 {
4675 struct mips_got_page_entry *entry;
4676 struct mips_elf_traverse_got_arg *arg;
4677 void **slot;
4678
4679 entry = (struct mips_got_page_entry *) *entryp;
4680 arg = (struct mips_elf_traverse_got_arg *) data;
4681 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4682 if (!slot)
4683 {
4684 arg->g = NULL;
4685 return 0;
4686 }
4687 if (!*slot)
4688 {
4689 *slot = entry;
4690 arg->g->page_gotno += entry->num_pages;
4691 }
4692 return 1;
4693 }
4694
4695 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4696 this would lead to overflow, 1 if they were merged successfully,
4697 and 0 if a merge failed due to lack of memory. (These values are chosen
4698 so that nonnegative return values can be returned by a htab_traverse
4699 callback.) */
4700
4701 static int
4702 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4703 struct mips_got_info *to,
4704 struct mips_elf_got_per_bfd_arg *arg)
4705 {
4706 struct mips_elf_traverse_got_arg tga;
4707 unsigned int estimate;
4708
4709 /* Work out how many page entries we would need for the combined GOT. */
4710 estimate = arg->max_pages;
4711 if (estimate >= from->page_gotno + to->page_gotno)
4712 estimate = from->page_gotno + to->page_gotno;
4713
4714 /* And conservatively estimate how many local and TLS entries
4715 would be needed. */
4716 estimate += from->local_gotno + to->local_gotno;
4717 estimate += from->tls_gotno + to->tls_gotno;
4718
4719 /* If we're merging with the primary got, any TLS relocations will
4720 come after the full set of global entries. Otherwise estimate those
4721 conservatively as well. */
4722 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4723 estimate += arg->global_count;
4724 else
4725 estimate += from->global_gotno + to->global_gotno;
4726
4727 /* Bail out if the combined GOT might be too big. */
4728 if (estimate > arg->max_count)
4729 return -1;
4730
4731 /* Transfer the bfd's got information from FROM to TO. */
4732 tga.info = arg->info;
4733 tga.g = to;
4734 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4735 if (!tga.g)
4736 return 0;
4737
4738 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4739 if (!tga.g)
4740 return 0;
4741
4742 mips_elf_replace_bfd_got (abfd, to);
4743 return 1;
4744 }
4745
4746 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4747 as possible of the primary got, since it doesn't require explicit
4748 dynamic relocations, but don't use bfds that would reference global
4749 symbols out of the addressable range. Failing the primary got,
4750 attempt to merge with the current got, or finish the current got
4751 and then make make the new got current. */
4752
4753 static bool
4754 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4755 struct mips_elf_got_per_bfd_arg *arg)
4756 {
4757 unsigned int estimate;
4758 int result;
4759
4760 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4761 return false;
4762
4763 /* Work out the number of page, local and TLS entries. */
4764 estimate = arg->max_pages;
4765 if (estimate > g->page_gotno)
4766 estimate = g->page_gotno;
4767 estimate += g->local_gotno + g->tls_gotno;
4768
4769 /* We place TLS GOT entries after both locals and globals. The globals
4770 for the primary GOT may overflow the normal GOT size limit, so be
4771 sure not to merge a GOT which requires TLS with the primary GOT in that
4772 case. This doesn't affect non-primary GOTs. */
4773 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4774
4775 if (estimate <= arg->max_count)
4776 {
4777 /* If we don't have a primary GOT, use it as
4778 a starting point for the primary GOT. */
4779 if (!arg->primary)
4780 {
4781 arg->primary = g;
4782 return true;
4783 }
4784
4785 /* Try merging with the primary GOT. */
4786 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4787 if (result >= 0)
4788 return result;
4789 }
4790
4791 /* If we can merge with the last-created got, do it. */
4792 if (arg->current)
4793 {
4794 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4795 if (result >= 0)
4796 return result;
4797 }
4798
4799 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4800 fits; if it turns out that it doesn't, we'll get relocation
4801 overflows anyway. */
4802 g->next = arg->current;
4803 arg->current = g;
4804
4805 return true;
4806 }
4807
4808 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4809 to GOTIDX, duplicating the entry if it has already been assigned
4810 an index in a different GOT. */
4811
4812 static bool
4813 mips_elf_set_gotidx (void **entryp, long gotidx)
4814 {
4815 struct mips_got_entry *entry;
4816
4817 entry = (struct mips_got_entry *) *entryp;
4818 if (entry->gotidx > 0)
4819 {
4820 struct mips_got_entry *new_entry;
4821
4822 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4823 if (!new_entry)
4824 return false;
4825
4826 *new_entry = *entry;
4827 *entryp = new_entry;
4828 entry = new_entry;
4829 }
4830 entry->gotidx = gotidx;
4831 return true;
4832 }
4833
4834 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4835 mips_elf_traverse_got_arg in which DATA->value is the size of one
4836 GOT entry. Set DATA->g to null on failure. */
4837
4838 static int
4839 mips_elf_initialize_tls_index (void **entryp, void *data)
4840 {
4841 struct mips_got_entry *entry;
4842 struct mips_elf_traverse_got_arg *arg;
4843
4844 /* We're only interested in TLS symbols. */
4845 entry = (struct mips_got_entry *) *entryp;
4846 if (entry->tls_type == GOT_TLS_NONE)
4847 return 1;
4848
4849 arg = (struct mips_elf_traverse_got_arg *) data;
4850 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4851 {
4852 arg->g = NULL;
4853 return 0;
4854 }
4855
4856 /* Account for the entries we've just allocated. */
4857 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4858 return 1;
4859 }
4860
4861 /* A htab_traverse callback for GOT entries, where DATA points to a
4862 mips_elf_traverse_got_arg. Set the global_got_area of each global
4863 symbol to DATA->value. */
4864
4865 static int
4866 mips_elf_set_global_got_area (void **entryp, void *data)
4867 {
4868 struct mips_got_entry *entry;
4869 struct mips_elf_traverse_got_arg *arg;
4870
4871 entry = (struct mips_got_entry *) *entryp;
4872 arg = (struct mips_elf_traverse_got_arg *) data;
4873 if (entry->abfd != NULL
4874 && entry->symndx == -1
4875 && entry->d.h->global_got_area != GGA_NONE)
4876 entry->d.h->global_got_area = arg->value;
4877 return 1;
4878 }
4879
4880 /* A htab_traverse callback for secondary GOT entries, where DATA points
4881 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4882 and record the number of relocations they require. DATA->value is
4883 the size of one GOT entry. Set DATA->g to null on failure. */
4884
4885 static int
4886 mips_elf_set_global_gotidx (void **entryp, void *data)
4887 {
4888 struct mips_got_entry *entry;
4889 struct mips_elf_traverse_got_arg *arg;
4890
4891 entry = (struct mips_got_entry *) *entryp;
4892 arg = (struct mips_elf_traverse_got_arg *) data;
4893 if (entry->abfd != NULL
4894 && entry->symndx == -1
4895 && entry->d.h->global_got_area != GGA_NONE)
4896 {
4897 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4898 {
4899 arg->g = NULL;
4900 return 0;
4901 }
4902 arg->g->assigned_low_gotno += 1;
4903
4904 if (bfd_link_pic (arg->info)
4905 || (elf_hash_table (arg->info)->dynamic_sections_created
4906 && entry->d.h->root.def_dynamic
4907 && !entry->d.h->root.def_regular))
4908 arg->g->relocs += 1;
4909 }
4910
4911 return 1;
4912 }
4913
4914 /* A htab_traverse callback for GOT entries for which DATA is the
4915 bfd_link_info. Forbid any global symbols from having traditional
4916 lazy-binding stubs. */
4917
4918 static int
4919 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4920 {
4921 struct bfd_link_info *info;
4922 struct mips_elf_link_hash_table *htab;
4923 struct mips_got_entry *entry;
4924
4925 entry = (struct mips_got_entry *) *entryp;
4926 info = (struct bfd_link_info *) data;
4927 htab = mips_elf_hash_table (info);
4928 BFD_ASSERT (htab != NULL);
4929
4930 if (entry->abfd != NULL
4931 && entry->symndx == -1
4932 && entry->d.h->needs_lazy_stub)
4933 {
4934 entry->d.h->needs_lazy_stub = false;
4935 htab->lazy_stub_count--;
4936 }
4937
4938 return 1;
4939 }
4940
4941 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4942 the primary GOT. */
4943 static bfd_vma
4944 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4945 {
4946 if (!g->next)
4947 return 0;
4948
4949 g = mips_elf_bfd_got (ibfd, false);
4950 if (! g)
4951 return 0;
4952
4953 BFD_ASSERT (g->next);
4954
4955 g = g->next;
4956
4957 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4958 * MIPS_ELF_GOT_SIZE (abfd);
4959 }
4960
4961 /* Turn a single GOT that is too big for 16-bit addressing into
4962 a sequence of GOTs, each one 16-bit addressable. */
4963
4964 static bool
4965 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4966 asection *got, bfd_size_type pages)
4967 {
4968 struct mips_elf_link_hash_table *htab;
4969 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4970 struct mips_elf_traverse_got_arg tga;
4971 struct mips_got_info *g, *gg;
4972 unsigned int assign, needed_relocs;
4973 bfd *dynobj, *ibfd;
4974
4975 dynobj = elf_hash_table (info)->dynobj;
4976 htab = mips_elf_hash_table (info);
4977 BFD_ASSERT (htab != NULL);
4978
4979 g = htab->got_info;
4980
4981 got_per_bfd_arg.obfd = abfd;
4982 got_per_bfd_arg.info = info;
4983 got_per_bfd_arg.current = NULL;
4984 got_per_bfd_arg.primary = NULL;
4985 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4986 / MIPS_ELF_GOT_SIZE (abfd))
4987 - htab->reserved_gotno);
4988 got_per_bfd_arg.max_pages = pages;
4989 /* The number of globals that will be included in the primary GOT.
4990 See the calls to mips_elf_set_global_got_area below for more
4991 information. */
4992 got_per_bfd_arg.global_count = g->global_gotno;
4993
4994 /* Try to merge the GOTs of input bfds together, as long as they
4995 don't seem to exceed the maximum GOT size, choosing one of them
4996 to be the primary GOT. */
4997 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4998 {
4999 gg = mips_elf_bfd_got (ibfd, false);
5000 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
5001 return false;
5002 }
5003
5004 /* If we do not find any suitable primary GOT, create an empty one. */
5005 if (got_per_bfd_arg.primary == NULL)
5006 g->next = mips_elf_create_got_info (abfd);
5007 else
5008 g->next = got_per_bfd_arg.primary;
5009 g->next->next = got_per_bfd_arg.current;
5010
5011 /* GG is now the master GOT, and G is the primary GOT. */
5012 gg = g;
5013 g = g->next;
5014
5015 /* Map the output bfd to the primary got. That's what we're going
5016 to use for bfds that use GOT16 or GOT_PAGE relocations that we
5017 didn't mark in check_relocs, and we want a quick way to find it.
5018 We can't just use gg->next because we're going to reverse the
5019 list. */
5020 mips_elf_replace_bfd_got (abfd, g);
5021
5022 /* Every symbol that is referenced in a dynamic relocation must be
5023 present in the primary GOT, so arrange for them to appear after
5024 those that are actually referenced. */
5025 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5026 g->global_gotno = gg->global_gotno;
5027
5028 tga.info = info;
5029 tga.value = GGA_RELOC_ONLY;
5030 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5031 tga.value = GGA_NORMAL;
5032 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5033
5034 /* Now go through the GOTs assigning them offset ranges.
5035 [assigned_low_gotno, local_gotno[ will be set to the range of local
5036 entries in each GOT. We can then compute the end of a GOT by
5037 adding local_gotno to global_gotno. We reverse the list and make
5038 it circular since then we'll be able to quickly compute the
5039 beginning of a GOT, by computing the end of its predecessor. To
5040 avoid special cases for the primary GOT, while still preserving
5041 assertions that are valid for both single- and multi-got links,
5042 we arrange for the main got struct to have the right number of
5043 global entries, but set its local_gotno such that the initial
5044 offset of the primary GOT is zero. Remember that the primary GOT
5045 will become the last item in the circular linked list, so it
5046 points back to the master GOT. */
5047 gg->local_gotno = -g->global_gotno;
5048 gg->global_gotno = g->global_gotno;
5049 gg->tls_gotno = 0;
5050 assign = 0;
5051 gg->next = gg;
5052
5053 do
5054 {
5055 struct mips_got_info *gn;
5056
5057 assign += htab->reserved_gotno;
5058 g->assigned_low_gotno = assign;
5059 g->local_gotno += assign;
5060 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5061 g->assigned_high_gotno = g->local_gotno - 1;
5062 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5063
5064 /* Take g out of the direct list, and push it onto the reversed
5065 list that gg points to. g->next is guaranteed to be nonnull after
5066 this operation, as required by mips_elf_initialize_tls_index. */
5067 gn = g->next;
5068 g->next = gg->next;
5069 gg->next = g;
5070
5071 /* Set up any TLS entries. We always place the TLS entries after
5072 all non-TLS entries. */
5073 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5074 tga.g = g;
5075 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5076 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5077 if (!tga.g)
5078 return false;
5079 BFD_ASSERT (g->tls_assigned_gotno == assign);
5080
5081 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
5082 g = gn;
5083
5084 /* Forbid global symbols in every non-primary GOT from having
5085 lazy-binding stubs. */
5086 if (g)
5087 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5088 }
5089 while (g);
5090
5091 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5092
5093 needed_relocs = 0;
5094 for (g = gg->next; g && g->next != gg; g = g->next)
5095 {
5096 unsigned int save_assign;
5097
5098 /* Assign offsets to global GOT entries and count how many
5099 relocations they need. */
5100 save_assign = g->assigned_low_gotno;
5101 g->assigned_low_gotno = g->local_gotno;
5102 tga.info = info;
5103 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5104 tga.g = g;
5105 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5106 if (!tga.g)
5107 return false;
5108 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5109 g->assigned_low_gotno = save_assign;
5110
5111 if (bfd_link_pic (info))
5112 {
5113 g->relocs += g->local_gotno - g->assigned_low_gotno;
5114 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5115 + g->next->global_gotno
5116 + g->next->tls_gotno
5117 + htab->reserved_gotno);
5118 }
5119 needed_relocs += g->relocs;
5120 }
5121 needed_relocs += g->relocs;
5122
5123 if (needed_relocs)
5124 mips_elf_allocate_dynamic_relocations (dynobj, info,
5125 needed_relocs);
5126
5127 return true;
5128 }
5129
5130 \f
5131 /* Returns the first relocation of type r_type found, beginning with
5132 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5133
5134 static const Elf_Internal_Rela *
5135 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5136 const Elf_Internal_Rela *relocation,
5137 const Elf_Internal_Rela *relend)
5138 {
5139 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5140
5141 while (relocation < relend)
5142 {
5143 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5144 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5145 return relocation;
5146
5147 ++relocation;
5148 }
5149
5150 /* We didn't find it. */
5151 return NULL;
5152 }
5153
5154 /* Return whether an input relocation is against a local symbol. */
5155
5156 static bool
5157 mips_elf_local_relocation_p (bfd *input_bfd,
5158 const Elf_Internal_Rela *relocation,
5159 asection **local_sections)
5160 {
5161 unsigned long r_symndx;
5162 Elf_Internal_Shdr *symtab_hdr;
5163 size_t extsymoff;
5164
5165 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5166 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5167 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5168
5169 if (r_symndx < extsymoff)
5170 return true;
5171 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5172 return true;
5173
5174 return false;
5175 }
5176 \f
5177 /* Sign-extend VALUE, which has the indicated number of BITS. */
5178
5179 bfd_vma
5180 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5181 {
5182 if (value & ((bfd_vma) 1 << (bits - 1)))
5183 /* VALUE is negative. */
5184 value |= ((bfd_vma) - 1) << bits;
5185
5186 return value;
5187 }
5188
5189 /* Return non-zero if the indicated VALUE has overflowed the maximum
5190 range expressible by a signed number with the indicated number of
5191 BITS. */
5192
5193 static bool
5194 mips_elf_overflow_p (bfd_vma value, int bits)
5195 {
5196 bfd_signed_vma svalue = (bfd_signed_vma) value;
5197
5198 if (svalue > (1 << (bits - 1)) - 1)
5199 /* The value is too big. */
5200 return true;
5201 else if (svalue < -(1 << (bits - 1)))
5202 /* The value is too small. */
5203 return true;
5204
5205 /* All is well. */
5206 return false;
5207 }
5208
5209 /* Calculate the %high function. */
5210
5211 static bfd_vma
5212 mips_elf_high (bfd_vma value)
5213 {
5214 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5215 }
5216
5217 /* Calculate the %higher function. */
5218
5219 static bfd_vma
5220 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5221 {
5222 #ifdef BFD64
5223 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5224 #else
5225 abort ();
5226 return MINUS_ONE;
5227 #endif
5228 }
5229
5230 /* Calculate the %highest function. */
5231
5232 static bfd_vma
5233 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5234 {
5235 #ifdef BFD64
5236 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5237 #else
5238 abort ();
5239 return MINUS_ONE;
5240 #endif
5241 }
5242 \f
5243 /* Create the .compact_rel section. */
5244
5245 static bool
5246 mips_elf_create_compact_rel_section
5247 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5248 {
5249 flagword flags;
5250 register asection *s;
5251
5252 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5253 {
5254 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5255 | SEC_READONLY);
5256
5257 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5258 if (s == NULL
5259 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5260 return false;
5261
5262 s->size = sizeof (Elf32_External_compact_rel);
5263 }
5264
5265 return true;
5266 }
5267
5268 /* Create the .got section to hold the global offset table. */
5269
5270 static bool
5271 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5272 {
5273 flagword flags;
5274 register asection *s;
5275 struct elf_link_hash_entry *h;
5276 struct bfd_link_hash_entry *bh;
5277 struct mips_elf_link_hash_table *htab;
5278
5279 htab = mips_elf_hash_table (info);
5280 BFD_ASSERT (htab != NULL);
5281
5282 /* This function may be called more than once. */
5283 if (htab->root.sgot)
5284 return true;
5285
5286 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5287 | SEC_LINKER_CREATED);
5288
5289 /* We have to use an alignment of 2**4 here because this is hardcoded
5290 in the function stub generation and in the linker script. */
5291 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5292 if (s == NULL
5293 || !bfd_set_section_alignment (s, 4))
5294 return false;
5295 htab->root.sgot = s;
5296
5297 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5298 linker script because we don't want to define the symbol if we
5299 are not creating a global offset table. */
5300 bh = NULL;
5301 if (! (_bfd_generic_link_add_one_symbol
5302 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5303 0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5304 return false;
5305
5306 h = (struct elf_link_hash_entry *) bh;
5307 h->non_elf = 0;
5308 h->def_regular = 1;
5309 h->type = STT_OBJECT;
5310 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5311 elf_hash_table (info)->hgot = h;
5312
5313 if (bfd_link_pic (info)
5314 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5315 return false;
5316
5317 htab->got_info = mips_elf_create_got_info (abfd);
5318 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5319 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5320
5321 /* We also need a .got.plt section when generating PLTs. */
5322 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5323 SEC_ALLOC | SEC_LOAD
5324 | SEC_HAS_CONTENTS
5325 | SEC_IN_MEMORY
5326 | SEC_LINKER_CREATED);
5327 if (s == NULL)
5328 return false;
5329 htab->root.sgotplt = s;
5330
5331 return true;
5332 }
5333 \f
5334 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5335 __GOTT_INDEX__ symbols. These symbols are only special for
5336 shared objects; they are not used in executables. */
5337
5338 static bool
5339 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5340 {
5341 return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5342 && bfd_link_pic (info)
5343 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5344 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5345 }
5346
5347 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5348 require an la25 stub. See also mips_elf_local_pic_function_p,
5349 which determines whether the destination function ever requires a
5350 stub. */
5351
5352 static bool
5353 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5354 bool target_is_16_bit_code_p)
5355 {
5356 /* We specifically ignore branches and jumps from EF_PIC objects,
5357 where the onus is on the compiler or programmer to perform any
5358 necessary initialization of $25. Sometimes such initialization
5359 is unnecessary; for example, -mno-shared functions do not use
5360 the incoming value of $25, and may therefore be called directly. */
5361 if (PIC_OBJECT_P (input_bfd))
5362 return false;
5363
5364 switch (r_type)
5365 {
5366 case R_MIPS_26:
5367 case R_MIPS_PC16:
5368 case R_MIPS_PC21_S2:
5369 case R_MIPS_PC26_S2:
5370 case R_MICROMIPS_26_S1:
5371 case R_MICROMIPS_PC7_S1:
5372 case R_MICROMIPS_PC10_S1:
5373 case R_MICROMIPS_PC16_S1:
5374 case R_MICROMIPS_PC23_S2:
5375 return true;
5376
5377 case R_MIPS16_26:
5378 return !target_is_16_bit_code_p;
5379
5380 default:
5381 return false;
5382 }
5383 }
5384 \f
5385 /* Obtain the field relocated by RELOCATION. */
5386
5387 static bfd_vma
5388 mips_elf_obtain_contents (reloc_howto_type *howto,
5389 const Elf_Internal_Rela *relocation,
5390 bfd *input_bfd, bfd_byte *contents)
5391 {
5392 bfd_vma x = 0;
5393 bfd_byte *location = contents + relocation->r_offset;
5394 unsigned int size = bfd_get_reloc_size (howto);
5395
5396 /* Obtain the bytes. */
5397 if (size != 0)
5398 x = bfd_get (8 * size, input_bfd, location);
5399
5400 return x;
5401 }
5402
5403 /* Store the field relocated by RELOCATION. */
5404
5405 static void
5406 mips_elf_store_contents (reloc_howto_type *howto,
5407 const Elf_Internal_Rela *relocation,
5408 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5409 {
5410 bfd_byte *location = contents + relocation->r_offset;
5411 unsigned int size = bfd_get_reloc_size (howto);
5412
5413 /* Put the value into the output. */
5414 if (size != 0)
5415 bfd_put (8 * size, input_bfd, x, location);
5416 }
5417
5418 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
5419 RELOCATION described by HOWTO, with a move of 0 to the load target
5420 register, returning TRUE if that is successful and FALSE otherwise.
5421 If DOIT is FALSE, then only determine it patching is possible and
5422 return status without actually changing CONTENTS.
5423 */
5424
5425 static bool
5426 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5427 const Elf_Internal_Rela *relocation,
5428 reloc_howto_type *howto, bool doit)
5429 {
5430 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5431 bfd_byte *location = contents + relocation->r_offset;
5432 bool nullified = true;
5433 bfd_vma x;
5434
5435 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5436
5437 /* Obtain the current value. */
5438 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5439
5440 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5441 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5442 if (mips16_reloc_p (r_type)
5443 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5444 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
5445 x = (0x3cdU << 22) | (x & (7 << 16)) << 3; /* LI */
5446 else if (micromips_reloc_p (r_type)
5447 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5448 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5449 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5450 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5451 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5452 else
5453 nullified = false;
5454
5455 /* Put the value into the output. */
5456 if (doit && nullified)
5457 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5458
5459 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5460
5461 return nullified;
5462 }
5463
5464 /* Calculate the value produced by the RELOCATION (which comes from
5465 the INPUT_BFD). The ADDEND is the addend to use for this
5466 RELOCATION; RELOCATION->R_ADDEND is ignored.
5467
5468 The result of the relocation calculation is stored in VALUEP.
5469 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5470 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5471
5472 This function returns bfd_reloc_continue if the caller need take no
5473 further action regarding this relocation, bfd_reloc_notsupported if
5474 something goes dramatically wrong, bfd_reloc_overflow if an
5475 overflow occurs, and bfd_reloc_ok to indicate success. */
5476
5477 static bfd_reloc_status_type
5478 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5479 asection *input_section, bfd_byte *contents,
5480 struct bfd_link_info *info,
5481 const Elf_Internal_Rela *relocation,
5482 bfd_vma addend, reloc_howto_type *howto,
5483 Elf_Internal_Sym *local_syms,
5484 asection **local_sections, bfd_vma *valuep,
5485 const char **namep,
5486 bool *cross_mode_jump_p,
5487 bool save_addend)
5488 {
5489 /* The eventual value we will return. */
5490 bfd_vma value;
5491 /* The address of the symbol against which the relocation is
5492 occurring. */
5493 bfd_vma symbol = 0;
5494 /* The final GP value to be used for the relocatable, executable, or
5495 shared object file being produced. */
5496 bfd_vma gp;
5497 /* The place (section offset or address) of the storage unit being
5498 relocated. */
5499 bfd_vma p;
5500 /* The value of GP used to create the relocatable object. */
5501 bfd_vma gp0;
5502 /* The offset into the global offset table at which the address of
5503 the relocation entry symbol, adjusted by the addend, resides
5504 during execution. */
5505 bfd_vma g = MINUS_ONE;
5506 /* The section in which the symbol referenced by the relocation is
5507 located. */
5508 asection *sec = NULL;
5509 struct mips_elf_link_hash_entry *h = NULL;
5510 /* TRUE if the symbol referred to by this relocation is a local
5511 symbol. */
5512 bool local_p, was_local_p;
5513 /* TRUE if the symbol referred to by this relocation is a section
5514 symbol. */
5515 bool section_p = false;
5516 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5517 bool gp_disp_p = false;
5518 /* TRUE if the symbol referred to by this relocation is
5519 "__gnu_local_gp". */
5520 bool gnu_local_gp_p = false;
5521 Elf_Internal_Shdr *symtab_hdr;
5522 size_t extsymoff;
5523 unsigned long r_symndx;
5524 int r_type;
5525 /* TRUE if overflow occurred during the calculation of the
5526 relocation value. */
5527 bool overflowed_p;
5528 /* TRUE if this relocation refers to a MIPS16 function. */
5529 bool target_is_16_bit_code_p = false;
5530 bool target_is_micromips_code_p = false;
5531 struct mips_elf_link_hash_table *htab;
5532 bfd *dynobj;
5533 bool resolved_to_zero;
5534
5535 dynobj = elf_hash_table (info)->dynobj;
5536 htab = mips_elf_hash_table (info);
5537 BFD_ASSERT (htab != NULL);
5538
5539 /* Parse the relocation. */
5540 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5541 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5542 p = (input_section->output_section->vma
5543 + input_section->output_offset
5544 + relocation->r_offset);
5545
5546 /* Assume that there will be no overflow. */
5547 overflowed_p = false;
5548
5549 /* Figure out whether or not the symbol is local, and get the offset
5550 used in the array of hash table entries. */
5551 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5552 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5553 local_sections);
5554 was_local_p = local_p;
5555 if (! elf_bad_symtab (input_bfd))
5556 extsymoff = symtab_hdr->sh_info;
5557 else
5558 {
5559 /* The symbol table does not follow the rule that local symbols
5560 must come before globals. */
5561 extsymoff = 0;
5562 }
5563
5564 /* Figure out the value of the symbol. */
5565 if (local_p)
5566 {
5567 bool micromips_p = MICROMIPS_P (abfd);
5568 Elf_Internal_Sym *sym;
5569
5570 sym = local_syms + r_symndx;
5571 sec = local_sections[r_symndx];
5572
5573 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5574
5575 symbol = sec->output_section->vma + sec->output_offset;
5576 if (!section_p || (sec->flags & SEC_MERGE))
5577 symbol += sym->st_value;
5578 if ((sec->flags & SEC_MERGE) && section_p)
5579 {
5580 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5581 addend -= symbol;
5582 addend += sec->output_section->vma + sec->output_offset;
5583 }
5584
5585 /* MIPS16/microMIPS text labels should be treated as odd. */
5586 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5587 ++symbol;
5588
5589 /* Record the name of this symbol, for our caller. */
5590 *namep = bfd_elf_string_from_elf_section (input_bfd,
5591 symtab_hdr->sh_link,
5592 sym->st_name);
5593 if (*namep == NULL || **namep == '\0')
5594 *namep = bfd_section_name (sec);
5595
5596 /* For relocations against a section symbol and ones against no
5597 symbol (absolute relocations) infer the ISA mode from the addend. */
5598 if (section_p || r_symndx == STN_UNDEF)
5599 {
5600 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5601 target_is_micromips_code_p = (addend & 1) && micromips_p;
5602 }
5603 /* For relocations against an absolute symbol infer the ISA mode
5604 from the value of the symbol plus addend. */
5605 else if (bfd_is_abs_section (sec))
5606 {
5607 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5608 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5609 }
5610 /* Otherwise just use the regular symbol annotation available. */
5611 else
5612 {
5613 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5614 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5615 }
5616 }
5617 else
5618 {
5619 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5620
5621 /* For global symbols we look up the symbol in the hash-table. */
5622 h = ((struct mips_elf_link_hash_entry *)
5623 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5624 /* Find the real hash-table entry for this symbol. */
5625 while (h->root.root.type == bfd_link_hash_indirect
5626 || h->root.root.type == bfd_link_hash_warning)
5627 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5628
5629 /* Record the name of this symbol, for our caller. */
5630 *namep = h->root.root.root.string;
5631
5632 /* See if this is the special _gp_disp symbol. Note that such a
5633 symbol must always be a global symbol. */
5634 if (strcmp (*namep, "_gp_disp") == 0
5635 && ! NEWABI_P (input_bfd))
5636 {
5637 /* Relocations against _gp_disp are permitted only with
5638 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5639 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5640 return bfd_reloc_notsupported;
5641
5642 gp_disp_p = true;
5643 }
5644 /* See if this is the special _gp symbol. Note that such a
5645 symbol must always be a global symbol. */
5646 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5647 gnu_local_gp_p = true;
5648
5649
5650 /* If this symbol is defined, calculate its address. Note that
5651 _gp_disp is a magic symbol, always implicitly defined by the
5652 linker, so it's inappropriate to check to see whether or not
5653 its defined. */
5654 else if ((h->root.root.type == bfd_link_hash_defined
5655 || h->root.root.type == bfd_link_hash_defweak)
5656 && h->root.root.u.def.section)
5657 {
5658 sec = h->root.root.u.def.section;
5659 if (sec->output_section)
5660 symbol = (h->root.root.u.def.value
5661 + sec->output_section->vma
5662 + sec->output_offset);
5663 else
5664 symbol = h->root.root.u.def.value;
5665 }
5666 else if (h->root.root.type == bfd_link_hash_undefweak)
5667 /* We allow relocations against undefined weak symbols, giving
5668 it the value zero, so that you can undefined weak functions
5669 and check to see if they exist by looking at their
5670 addresses. */
5671 symbol = 0;
5672 else if (info->unresolved_syms_in_objects == RM_IGNORE
5673 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5674 symbol = 0;
5675 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5676 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5677 {
5678 /* If this is a dynamic link, we should have created a
5679 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5680 in _bfd_mips_elf_create_dynamic_sections.
5681 Otherwise, we should define the symbol with a value of 0.
5682 FIXME: It should probably get into the symbol table
5683 somehow as well. */
5684 BFD_ASSERT (! bfd_link_pic (info));
5685 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5686 symbol = 0;
5687 }
5688 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5689 {
5690 /* This is an optional symbol - an Irix specific extension to the
5691 ELF spec. Ignore it for now.
5692 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5693 than simply ignoring them, but we do not handle this for now.
5694 For information see the "64-bit ELF Object File Specification"
5695 which is available from here:
5696 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5697 symbol = 0;
5698 }
5699 else
5700 {
5701 bool reject_undefined
5702 = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5703 && !info->warn_unresolved_syms)
5704 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5705
5706 info->callbacks->undefined_symbol
5707 (info, h->root.root.root.string, input_bfd,
5708 input_section, relocation->r_offset, reject_undefined);
5709
5710 if (reject_undefined)
5711 return bfd_reloc_undefined;
5712
5713 symbol = 0;
5714 }
5715
5716 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5717 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5718 }
5719
5720 /* If this is a reference to a 16-bit function with a stub, we need
5721 to redirect the relocation to the stub unless:
5722
5723 (a) the relocation is for a MIPS16 JAL;
5724
5725 (b) the relocation is for a MIPS16 PIC call, and there are no
5726 non-MIPS16 uses of the GOT slot; or
5727
5728 (c) the section allows direct references to MIPS16 functions. */
5729 if (r_type != R_MIPS16_26
5730 && !bfd_link_relocatable (info)
5731 && ((h != NULL
5732 && h->fn_stub != NULL
5733 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5734 || (local_p
5735 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5736 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5737 && !section_allows_mips16_refs_p (input_section))
5738 {
5739 /* This is a 32- or 64-bit call to a 16-bit function. We should
5740 have already noticed that we were going to need the
5741 stub. */
5742 if (local_p)
5743 {
5744 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5745 value = 0;
5746 }
5747 else
5748 {
5749 BFD_ASSERT (h->need_fn_stub);
5750 if (h->la25_stub)
5751 {
5752 /* If a LA25 header for the stub itself exists, point to the
5753 prepended LUI/ADDIU sequence. */
5754 sec = h->la25_stub->stub_section;
5755 value = h->la25_stub->offset;
5756 }
5757 else
5758 {
5759 sec = h->fn_stub;
5760 value = 0;
5761 }
5762 }
5763
5764 symbol = sec->output_section->vma + sec->output_offset + value;
5765 /* The target is 16-bit, but the stub isn't. */
5766 target_is_16_bit_code_p = false;
5767 }
5768 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5769 to a standard MIPS function, we need to redirect the call to the stub.
5770 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5771 indirect calls should use an indirect stub instead. */
5772 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5773 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5774 || (local_p
5775 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5776 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5777 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5778 {
5779 if (local_p)
5780 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5781 else
5782 {
5783 /* If both call_stub and call_fp_stub are defined, we can figure
5784 out which one to use by checking which one appears in the input
5785 file. */
5786 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5787 {
5788 asection *o;
5789
5790 sec = NULL;
5791 for (o = input_bfd->sections; o != NULL; o = o->next)
5792 {
5793 if (CALL_FP_STUB_P (bfd_section_name (o)))
5794 {
5795 sec = h->call_fp_stub;
5796 break;
5797 }
5798 }
5799 if (sec == NULL)
5800 sec = h->call_stub;
5801 }
5802 else if (h->call_stub != NULL)
5803 sec = h->call_stub;
5804 else
5805 sec = h->call_fp_stub;
5806 }
5807
5808 BFD_ASSERT (sec->size > 0);
5809 symbol = sec->output_section->vma + sec->output_offset;
5810 }
5811 /* If this is a direct call to a PIC function, redirect to the
5812 non-PIC stub. */
5813 else if (h != NULL && h->la25_stub
5814 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5815 target_is_16_bit_code_p))
5816 {
5817 symbol = (h->la25_stub->stub_section->output_section->vma
5818 + h->la25_stub->stub_section->output_offset
5819 + h->la25_stub->offset);
5820 if (ELF_ST_IS_MICROMIPS (h->root.other))
5821 symbol |= 1;
5822 }
5823 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5824 entry is used if a standard PLT entry has also been made. In this
5825 case the symbol will have been set by mips_elf_set_plt_sym_value
5826 to point to the standard PLT entry, so redirect to the compressed
5827 one. */
5828 else if ((mips16_branch_reloc_p (r_type)
5829 || micromips_branch_reloc_p (r_type))
5830 && !bfd_link_relocatable (info)
5831 && h != NULL
5832 && h->use_plt_entry
5833 && h->root.plt.plist->comp_offset != MINUS_ONE
5834 && h->root.plt.plist->mips_offset != MINUS_ONE)
5835 {
5836 bool micromips_p = MICROMIPS_P (abfd);
5837
5838 sec = htab->root.splt;
5839 symbol = (sec->output_section->vma
5840 + sec->output_offset
5841 + htab->plt_header_size
5842 + htab->plt_mips_offset
5843 + h->root.plt.plist->comp_offset
5844 + 1);
5845
5846 target_is_16_bit_code_p = !micromips_p;
5847 target_is_micromips_code_p = micromips_p;
5848 }
5849
5850 /* Make sure MIPS16 and microMIPS are not used together. */
5851 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5852 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5853 {
5854 _bfd_error_handler
5855 (_("MIPS16 and microMIPS functions cannot call each other"));
5856 return bfd_reloc_notsupported;
5857 }
5858
5859 /* Calls from 16-bit code to 32-bit code and vice versa require the
5860 mode change. However, we can ignore calls to undefined weak symbols,
5861 which should never be executed at runtime. This exception is important
5862 because the assembly writer may have "known" that any definition of the
5863 symbol would be 16-bit code, and that direct jumps were therefore
5864 acceptable. */
5865 *cross_mode_jump_p = (!bfd_link_relocatable (info)
5866 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5867 && ((mips16_branch_reloc_p (r_type)
5868 && !target_is_16_bit_code_p)
5869 || (micromips_branch_reloc_p (r_type)
5870 && !target_is_micromips_code_p)
5871 || ((branch_reloc_p (r_type)
5872 || r_type == R_MIPS_JALR)
5873 && (target_is_16_bit_code_p
5874 || target_is_micromips_code_p))));
5875
5876 resolved_to_zero = (h != NULL
5877 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5878
5879 switch (r_type)
5880 {
5881 case R_MIPS16_CALL16:
5882 case R_MIPS16_GOT16:
5883 case R_MIPS_CALL16:
5884 case R_MIPS_GOT16:
5885 case R_MIPS_GOT_PAGE:
5886 case R_MIPS_GOT_DISP:
5887 case R_MIPS_GOT_LO16:
5888 case R_MIPS_CALL_LO16:
5889 case R_MICROMIPS_CALL16:
5890 case R_MICROMIPS_GOT16:
5891 case R_MICROMIPS_GOT_PAGE:
5892 case R_MICROMIPS_GOT_DISP:
5893 case R_MICROMIPS_GOT_LO16:
5894 case R_MICROMIPS_CALL_LO16:
5895 if (resolved_to_zero
5896 && !bfd_link_relocatable (info)
5897 && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5898 relocation->r_offset)
5899 && mips_elf_nullify_got_load (input_bfd, contents,
5900 relocation, howto, true))
5901 return bfd_reloc_continue;
5902
5903 /* Fall through. */
5904 case R_MIPS_GOT_HI16:
5905 case R_MIPS_CALL_HI16:
5906 case R_MICROMIPS_GOT_HI16:
5907 case R_MICROMIPS_CALL_HI16:
5908 if (resolved_to_zero
5909 && htab->use_absolute_zero
5910 && bfd_link_pic (info))
5911 {
5912 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5913 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5914 false, false, false);
5915 BFD_ASSERT (h != NULL);
5916 }
5917 break;
5918 }
5919
5920 local_p = (h == NULL || mips_use_local_got_p (info, h));
5921
5922 gp0 = _bfd_get_gp_value (input_bfd);
5923 gp = _bfd_get_gp_value (abfd);
5924 if (htab->got_info)
5925 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5926
5927 if (gnu_local_gp_p)
5928 symbol = gp;
5929
5930 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5931 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5932 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5933 if (got_page_reloc_p (r_type) && !local_p)
5934 {
5935 r_type = (micromips_reloc_p (r_type)
5936 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5937 addend = 0;
5938 }
5939
5940 /* If we haven't already determined the GOT offset, and we're going
5941 to need it, get it now. */
5942 switch (r_type)
5943 {
5944 case R_MIPS16_CALL16:
5945 case R_MIPS16_GOT16:
5946 case R_MIPS_CALL16:
5947 case R_MIPS_GOT16:
5948 case R_MIPS_GOT_DISP:
5949 case R_MIPS_GOT_HI16:
5950 case R_MIPS_CALL_HI16:
5951 case R_MIPS_GOT_LO16:
5952 case R_MIPS_CALL_LO16:
5953 case R_MICROMIPS_CALL16:
5954 case R_MICROMIPS_GOT16:
5955 case R_MICROMIPS_GOT_DISP:
5956 case R_MICROMIPS_GOT_HI16:
5957 case R_MICROMIPS_CALL_HI16:
5958 case R_MICROMIPS_GOT_LO16:
5959 case R_MICROMIPS_CALL_LO16:
5960 case R_MIPS_TLS_GD:
5961 case R_MIPS_TLS_GOTTPREL:
5962 case R_MIPS_TLS_LDM:
5963 case R_MIPS16_TLS_GD:
5964 case R_MIPS16_TLS_GOTTPREL:
5965 case R_MIPS16_TLS_LDM:
5966 case R_MICROMIPS_TLS_GD:
5967 case R_MICROMIPS_TLS_GOTTPREL:
5968 case R_MICROMIPS_TLS_LDM:
5969 /* Find the index into the GOT where this value is located. */
5970 if (tls_ldm_reloc_p (r_type))
5971 {
5972 g = mips_elf_local_got_index (abfd, input_bfd, info,
5973 0, 0, NULL, r_type);
5974 if (g == MINUS_ONE)
5975 return bfd_reloc_outofrange;
5976 }
5977 else if (!local_p)
5978 {
5979 /* On VxWorks, CALL relocations should refer to the .got.plt
5980 entry, which is initialized to point at the PLT stub. */
5981 if (htab->root.target_os == is_vxworks
5982 && (call_hi16_reloc_p (r_type)
5983 || call_lo16_reloc_p (r_type)
5984 || call16_reloc_p (r_type)))
5985 {
5986 BFD_ASSERT (addend == 0);
5987 BFD_ASSERT (h->root.needs_plt);
5988 g = mips_elf_gotplt_index (info, &h->root);
5989 }
5990 else
5991 {
5992 BFD_ASSERT (addend == 0);
5993 g = mips_elf_global_got_index (abfd, info, input_bfd,
5994 &h->root, r_type);
5995 if (!TLS_RELOC_P (r_type)
5996 && !elf_hash_table (info)->dynamic_sections_created)
5997 /* This is a static link. We must initialize the GOT entry. */
5998 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
5999 }
6000 }
6001 else if (htab->root.target_os != is_vxworks
6002 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
6003 /* The calculation below does not involve "g". */
6004 break;
6005 else
6006 {
6007 g = mips_elf_local_got_index (abfd, input_bfd, info,
6008 symbol + addend, r_symndx, h, r_type);
6009 if (g == MINUS_ONE)
6010 return bfd_reloc_outofrange;
6011 }
6012
6013 /* Convert GOT indices to actual offsets. */
6014 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
6015 break;
6016 }
6017
6018 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
6019 symbols are resolved by the loader. Add them to .rela.dyn. */
6020 if (h != NULL && is_gott_symbol (info, &h->root))
6021 {
6022 Elf_Internal_Rela outrel;
6023 bfd_byte *loc;
6024 asection *s;
6025
6026 s = mips_elf_rel_dyn_section (info, false);
6027 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6028
6029 outrel.r_offset = (input_section->output_section->vma
6030 + input_section->output_offset
6031 + relocation->r_offset);
6032 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6033 outrel.r_addend = addend;
6034 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6035
6036 /* If we've written this relocation for a readonly section,
6037 we need to set DF_TEXTREL again, so that we do not delete the
6038 DT_TEXTREL tag. */
6039 if (MIPS_ELF_READONLY_SECTION (input_section))
6040 info->flags |= DF_TEXTREL;
6041
6042 *valuep = 0;
6043 return bfd_reloc_ok;
6044 }
6045
6046 /* Figure out what kind of relocation is being performed. */
6047 switch (r_type)
6048 {
6049 case R_MIPS_NONE:
6050 return bfd_reloc_continue;
6051
6052 case R_MIPS_16:
6053 if (howto->partial_inplace)
6054 addend = _bfd_mips_elf_sign_extend (addend, 16);
6055 value = symbol + addend;
6056 overflowed_p = mips_elf_overflow_p (value, 16);
6057 break;
6058
6059 case R_MIPS_32:
6060 case R_MIPS_REL32:
6061 case R_MIPS_64:
6062 if ((bfd_link_pic (info)
6063 || (htab->root.dynamic_sections_created
6064 && h != NULL
6065 && h->root.def_dynamic
6066 && !h->root.def_regular
6067 && !h->has_static_relocs))
6068 && r_symndx != STN_UNDEF
6069 && (h == NULL
6070 || h->root.root.type != bfd_link_hash_undefweak
6071 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6072 && !resolved_to_zero))
6073 && (input_section->flags & SEC_ALLOC) != 0)
6074 {
6075 /* If we're creating a shared library, then we can't know
6076 where the symbol will end up. So, we create a relocation
6077 record in the output, and leave the job up to the dynamic
6078 linker. We must do the same for executable references to
6079 shared library symbols, unless we've decided to use copy
6080 relocs or PLTs instead. */
6081 value = addend;
6082 if (!mips_elf_create_dynamic_relocation (abfd,
6083 info,
6084 relocation,
6085 h,
6086 sec,
6087 symbol,
6088 &value,
6089 input_section))
6090 return bfd_reloc_undefined;
6091 }
6092 else
6093 {
6094 if (r_type != R_MIPS_REL32)
6095 value = symbol + addend;
6096 else
6097 value = addend;
6098 }
6099 value &= howto->dst_mask;
6100 break;
6101
6102 case R_MIPS_PC32:
6103 value = symbol + addend - p;
6104 value &= howto->dst_mask;
6105 break;
6106
6107 case R_MIPS16_26:
6108 /* The calculation for R_MIPS16_26 is just the same as for an
6109 R_MIPS_26. It's only the storage of the relocated field into
6110 the output file that's different. That's handled in
6111 mips_elf_perform_relocation. So, we just fall through to the
6112 R_MIPS_26 case here. */
6113 case R_MIPS_26:
6114 case R_MICROMIPS_26_S1:
6115 {
6116 unsigned int shift;
6117
6118 /* Shift is 2, unusually, for microMIPS JALX. */
6119 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6120
6121 if (howto->partial_inplace && !section_p)
6122 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6123 else
6124 value = addend;
6125 value += symbol;
6126
6127 /* Make sure the target of a jump is suitably aligned. Bit 0 must
6128 be the correct ISA mode selector except for weak undefined
6129 symbols. */
6130 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6131 && (*cross_mode_jump_p
6132 ? (value & 3) != (r_type == R_MIPS_26)
6133 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6134 return bfd_reloc_outofrange;
6135
6136 value >>= shift;
6137 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6138 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6139 value &= howto->dst_mask;
6140 }
6141 break;
6142
6143 case R_MIPS_TLS_DTPREL_HI16:
6144 case R_MIPS16_TLS_DTPREL_HI16:
6145 case R_MICROMIPS_TLS_DTPREL_HI16:
6146 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6147 & howto->dst_mask);
6148 break;
6149
6150 case R_MIPS_TLS_DTPREL_LO16:
6151 case R_MIPS_TLS_DTPREL32:
6152 case R_MIPS_TLS_DTPREL64:
6153 case R_MIPS16_TLS_DTPREL_LO16:
6154 case R_MICROMIPS_TLS_DTPREL_LO16:
6155 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6156 break;
6157
6158 case R_MIPS_TLS_TPREL_HI16:
6159 case R_MIPS16_TLS_TPREL_HI16:
6160 case R_MICROMIPS_TLS_TPREL_HI16:
6161 value = (mips_elf_high (addend + symbol - tprel_base (info))
6162 & howto->dst_mask);
6163 break;
6164
6165 case R_MIPS_TLS_TPREL_LO16:
6166 case R_MIPS_TLS_TPREL32:
6167 case R_MIPS_TLS_TPREL64:
6168 case R_MIPS16_TLS_TPREL_LO16:
6169 case R_MICROMIPS_TLS_TPREL_LO16:
6170 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6171 break;
6172
6173 case R_MIPS_HI16:
6174 case R_MIPS16_HI16:
6175 case R_MICROMIPS_HI16:
6176 if (!gp_disp_p)
6177 {
6178 value = mips_elf_high (addend + symbol);
6179 value &= howto->dst_mask;
6180 }
6181 else
6182 {
6183 /* For MIPS16 ABI code we generate this sequence
6184 0: li $v0,%hi(_gp_disp)
6185 4: addiupc $v1,%lo(_gp_disp)
6186 8: sll $v0,16
6187 12: addu $v0,$v1
6188 14: move $gp,$v0
6189 So the offsets of hi and lo relocs are the same, but the
6190 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6191 ADDIUPC clears the low two bits of the instruction address,
6192 so the base is ($t9 + 4) & ~3. */
6193 if (r_type == R_MIPS16_HI16)
6194 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6195 /* The microMIPS .cpload sequence uses the same assembly
6196 instructions as the traditional psABI version, but the
6197 incoming $t9 has the low bit set. */
6198 else if (r_type == R_MICROMIPS_HI16)
6199 value = mips_elf_high (addend + gp - p - 1);
6200 else
6201 value = mips_elf_high (addend + gp - p);
6202 }
6203 break;
6204
6205 case R_MIPS_LO16:
6206 case R_MIPS16_LO16:
6207 case R_MICROMIPS_LO16:
6208 case R_MICROMIPS_HI0_LO16:
6209 if (!gp_disp_p)
6210 value = (symbol + addend) & howto->dst_mask;
6211 else
6212 {
6213 /* See the comment for R_MIPS16_HI16 above for the reason
6214 for this conditional. */
6215 if (r_type == R_MIPS16_LO16)
6216 value = addend + gp - (p & ~(bfd_vma) 0x3);
6217 else if (r_type == R_MICROMIPS_LO16
6218 || r_type == R_MICROMIPS_HI0_LO16)
6219 value = addend + gp - p + 3;
6220 else
6221 value = addend + gp - p + 4;
6222 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6223 for overflow. But, on, say, IRIX5, relocations against
6224 _gp_disp are normally generated from the .cpload
6225 pseudo-op. It generates code that normally looks like
6226 this:
6227
6228 lui $gp,%hi(_gp_disp)
6229 addiu $gp,$gp,%lo(_gp_disp)
6230 addu $gp,$gp,$t9
6231
6232 Here $t9 holds the address of the function being called,
6233 as required by the MIPS ELF ABI. The R_MIPS_LO16
6234 relocation can easily overflow in this situation, but the
6235 R_MIPS_HI16 relocation will handle the overflow.
6236 Therefore, we consider this a bug in the MIPS ABI, and do
6237 not check for overflow here. */
6238 }
6239 break;
6240
6241 case R_MIPS_LITERAL:
6242 case R_MICROMIPS_LITERAL:
6243 /* Because we don't merge literal sections, we can handle this
6244 just like R_MIPS_GPREL16. In the long run, we should merge
6245 shared literals, and then we will need to additional work
6246 here. */
6247
6248 /* Fall through. */
6249
6250 case R_MIPS16_GPREL:
6251 /* The R_MIPS16_GPREL performs the same calculation as
6252 R_MIPS_GPREL16, but stores the relocated bits in a different
6253 order. We don't need to do anything special here; the
6254 differences are handled in mips_elf_perform_relocation. */
6255 case R_MIPS_GPREL16:
6256 case R_MICROMIPS_GPREL7_S2:
6257 case R_MICROMIPS_GPREL16:
6258 /* Only sign-extend the addend if it was extracted from the
6259 instruction. If the addend was separate, leave it alone,
6260 otherwise we may lose significant bits. */
6261 if (howto->partial_inplace)
6262 addend = _bfd_mips_elf_sign_extend (addend, 16);
6263 value = symbol + addend - gp;
6264 /* If the symbol was local, any earlier relocatable links will
6265 have adjusted its addend with the gp offset, so compensate
6266 for that now. Don't do it for symbols forced local in this
6267 link, though, since they won't have had the gp offset applied
6268 to them before. */
6269 if (was_local_p)
6270 value += gp0;
6271 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6272 overflowed_p = mips_elf_overflow_p (value, 16);
6273 break;
6274
6275 case R_MIPS16_GOT16:
6276 case R_MIPS16_CALL16:
6277 case R_MIPS_GOT16:
6278 case R_MIPS_CALL16:
6279 case R_MICROMIPS_GOT16:
6280 case R_MICROMIPS_CALL16:
6281 /* VxWorks does not have separate local and global semantics for
6282 R_MIPS*_GOT16; every relocation evaluates to "G". */
6283 if (htab->root.target_os != is_vxworks && local_p)
6284 {
6285 value = mips_elf_got16_entry (abfd, input_bfd, info,
6286 symbol + addend, !was_local_p);
6287 if (value == MINUS_ONE)
6288 return bfd_reloc_outofrange;
6289 value
6290 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6291 overflowed_p = mips_elf_overflow_p (value, 16);
6292 break;
6293 }
6294
6295 /* Fall through. */
6296
6297 case R_MIPS_TLS_GD:
6298 case R_MIPS_TLS_GOTTPREL:
6299 case R_MIPS_TLS_LDM:
6300 case R_MIPS_GOT_DISP:
6301 case R_MIPS16_TLS_GD:
6302 case R_MIPS16_TLS_GOTTPREL:
6303 case R_MIPS16_TLS_LDM:
6304 case R_MICROMIPS_TLS_GD:
6305 case R_MICROMIPS_TLS_GOTTPREL:
6306 case R_MICROMIPS_TLS_LDM:
6307 case R_MICROMIPS_GOT_DISP:
6308 value = g;
6309 overflowed_p = mips_elf_overflow_p (value, 16);
6310 break;
6311
6312 case R_MIPS_GPREL32:
6313 value = (addend + symbol + gp0 - gp);
6314 if (!save_addend)
6315 value &= howto->dst_mask;
6316 break;
6317
6318 case R_MIPS_PC16:
6319 case R_MIPS_GNU_REL16_S2:
6320 if (howto->partial_inplace)
6321 addend = _bfd_mips_elf_sign_extend (addend, 18);
6322
6323 /* No need to exclude weak undefined symbols here as they resolve
6324 to 0 and never set `*cross_mode_jump_p', so this alignment check
6325 will never trigger for them. */
6326 if (*cross_mode_jump_p
6327 ? ((symbol + addend) & 3) != 1
6328 : ((symbol + addend) & 3) != 0)
6329 return bfd_reloc_outofrange;
6330
6331 value = symbol + addend - p;
6332 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6333 overflowed_p = mips_elf_overflow_p (value, 18);
6334 value >>= howto->rightshift;
6335 value &= howto->dst_mask;
6336 break;
6337
6338 case R_MIPS16_PC16_S1:
6339 if (howto->partial_inplace)
6340 addend = _bfd_mips_elf_sign_extend (addend, 17);
6341
6342 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6343 && (*cross_mode_jump_p
6344 ? ((symbol + addend) & 3) != 0
6345 : ((symbol + addend) & 1) == 0))
6346 return bfd_reloc_outofrange;
6347
6348 value = symbol + addend - p;
6349 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6350 overflowed_p = mips_elf_overflow_p (value, 17);
6351 value >>= howto->rightshift;
6352 value &= howto->dst_mask;
6353 break;
6354
6355 case R_MIPS_PC21_S2:
6356 if (howto->partial_inplace)
6357 addend = _bfd_mips_elf_sign_extend (addend, 23);
6358
6359 if ((symbol + addend) & 3)
6360 return bfd_reloc_outofrange;
6361
6362 value = symbol + addend - p;
6363 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6364 overflowed_p = mips_elf_overflow_p (value, 23);
6365 value >>= howto->rightshift;
6366 value &= howto->dst_mask;
6367 break;
6368
6369 case R_MIPS_PC26_S2:
6370 if (howto->partial_inplace)
6371 addend = _bfd_mips_elf_sign_extend (addend, 28);
6372
6373 if ((symbol + addend) & 3)
6374 return bfd_reloc_outofrange;
6375
6376 value = symbol + addend - p;
6377 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6378 overflowed_p = mips_elf_overflow_p (value, 28);
6379 value >>= howto->rightshift;
6380 value &= howto->dst_mask;
6381 break;
6382
6383 case R_MIPS_PC18_S3:
6384 if (howto->partial_inplace)
6385 addend = _bfd_mips_elf_sign_extend (addend, 21);
6386
6387 if ((symbol + addend) & 7)
6388 return bfd_reloc_outofrange;
6389
6390 value = symbol + addend - ((p | 7) ^ 7);
6391 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6392 overflowed_p = mips_elf_overflow_p (value, 21);
6393 value >>= howto->rightshift;
6394 value &= howto->dst_mask;
6395 break;
6396
6397 case R_MIPS_PC19_S2:
6398 if (howto->partial_inplace)
6399 addend = _bfd_mips_elf_sign_extend (addend, 21);
6400
6401 if ((symbol + addend) & 3)
6402 return bfd_reloc_outofrange;
6403
6404 value = symbol + addend - p;
6405 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6406 overflowed_p = mips_elf_overflow_p (value, 21);
6407 value >>= howto->rightshift;
6408 value &= howto->dst_mask;
6409 break;
6410
6411 case R_MIPS_PCHI16:
6412 value = mips_elf_high (symbol + addend - p);
6413 value &= howto->dst_mask;
6414 break;
6415
6416 case R_MIPS_PCLO16:
6417 if (howto->partial_inplace)
6418 addend = _bfd_mips_elf_sign_extend (addend, 16);
6419 value = symbol + addend - p;
6420 value &= howto->dst_mask;
6421 break;
6422
6423 case R_MICROMIPS_PC7_S1:
6424 if (howto->partial_inplace)
6425 addend = _bfd_mips_elf_sign_extend (addend, 8);
6426
6427 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6428 && (*cross_mode_jump_p
6429 ? ((symbol + addend + 2) & 3) != 0
6430 : ((symbol + addend + 2) & 1) == 0))
6431 return bfd_reloc_outofrange;
6432
6433 value = symbol + addend - p;
6434 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6435 overflowed_p = mips_elf_overflow_p (value, 8);
6436 value >>= howto->rightshift;
6437 value &= howto->dst_mask;
6438 break;
6439
6440 case R_MICROMIPS_PC10_S1:
6441 if (howto->partial_inplace)
6442 addend = _bfd_mips_elf_sign_extend (addend, 11);
6443
6444 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6445 && (*cross_mode_jump_p
6446 ? ((symbol + addend + 2) & 3) != 0
6447 : ((symbol + addend + 2) & 1) == 0))
6448 return bfd_reloc_outofrange;
6449
6450 value = symbol + addend - p;
6451 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6452 overflowed_p = mips_elf_overflow_p (value, 11);
6453 value >>= howto->rightshift;
6454 value &= howto->dst_mask;
6455 break;
6456
6457 case R_MICROMIPS_PC16_S1:
6458 if (howto->partial_inplace)
6459 addend = _bfd_mips_elf_sign_extend (addend, 17);
6460
6461 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6462 && (*cross_mode_jump_p
6463 ? ((symbol + addend) & 3) != 0
6464 : ((symbol + addend) & 1) == 0))
6465 return bfd_reloc_outofrange;
6466
6467 value = symbol + addend - p;
6468 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6469 overflowed_p = mips_elf_overflow_p (value, 17);
6470 value >>= howto->rightshift;
6471 value &= howto->dst_mask;
6472 break;
6473
6474 case R_MICROMIPS_PC23_S2:
6475 if (howto->partial_inplace)
6476 addend = _bfd_mips_elf_sign_extend (addend, 25);
6477 value = symbol + addend - ((p | 3) ^ 3);
6478 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6479 overflowed_p = mips_elf_overflow_p (value, 25);
6480 value >>= howto->rightshift;
6481 value &= howto->dst_mask;
6482 break;
6483
6484 case R_MIPS_GOT_HI16:
6485 case R_MIPS_CALL_HI16:
6486 case R_MICROMIPS_GOT_HI16:
6487 case R_MICROMIPS_CALL_HI16:
6488 /* We're allowed to handle these two relocations identically.
6489 The dynamic linker is allowed to handle the CALL relocations
6490 differently by creating a lazy evaluation stub. */
6491 value = g;
6492 value = mips_elf_high (value);
6493 value &= howto->dst_mask;
6494 break;
6495
6496 case R_MIPS_GOT_LO16:
6497 case R_MIPS_CALL_LO16:
6498 case R_MICROMIPS_GOT_LO16:
6499 case R_MICROMIPS_CALL_LO16:
6500 value = g & howto->dst_mask;
6501 break;
6502
6503 case R_MIPS_GOT_PAGE:
6504 case R_MICROMIPS_GOT_PAGE:
6505 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6506 if (value == MINUS_ONE)
6507 return bfd_reloc_outofrange;
6508 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6509 overflowed_p = mips_elf_overflow_p (value, 16);
6510 break;
6511
6512 case R_MIPS_GOT_OFST:
6513 case R_MICROMIPS_GOT_OFST:
6514 if (local_p)
6515 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6516 else
6517 value = addend;
6518 overflowed_p = mips_elf_overflow_p (value, 16);
6519 break;
6520
6521 case R_MIPS_SUB:
6522 case R_MICROMIPS_SUB:
6523 value = symbol - addend;
6524 value &= howto->dst_mask;
6525 break;
6526
6527 case R_MIPS_HIGHER:
6528 case R_MICROMIPS_HIGHER:
6529 value = mips_elf_higher (addend + symbol);
6530 value &= howto->dst_mask;
6531 break;
6532
6533 case R_MIPS_HIGHEST:
6534 case R_MICROMIPS_HIGHEST:
6535 value = mips_elf_highest (addend + symbol);
6536 value &= howto->dst_mask;
6537 break;
6538
6539 case R_MIPS_SCN_DISP:
6540 case R_MICROMIPS_SCN_DISP:
6541 value = symbol + addend - sec->output_offset;
6542 value &= howto->dst_mask;
6543 break;
6544
6545 case R_MIPS_JALR:
6546 case R_MICROMIPS_JALR:
6547 /* This relocation is only a hint. In some cases, we optimize
6548 it into a bal instruction. But we don't try to optimize
6549 when the symbol does not resolve locally. */
6550 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6551 return bfd_reloc_continue;
6552 /* We can't optimize cross-mode jumps either. */
6553 if (*cross_mode_jump_p)
6554 return bfd_reloc_continue;
6555 value = symbol + addend;
6556 /* Neither we can non-instruction-aligned targets. */
6557 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6558 return bfd_reloc_continue;
6559 break;
6560
6561 case R_MIPS_PJUMP:
6562 case R_MIPS_GNU_VTINHERIT:
6563 case R_MIPS_GNU_VTENTRY:
6564 /* We don't do anything with these at present. */
6565 return bfd_reloc_continue;
6566
6567 default:
6568 /* An unrecognized relocation type. */
6569 return bfd_reloc_notsupported;
6570 }
6571
6572 /* Store the VALUE for our caller. */
6573 *valuep = value;
6574 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6575 }
6576
6577 /* It has been determined that the result of the RELOCATION is the
6578 VALUE. Use HOWTO to place VALUE into the output file at the
6579 appropriate position. The SECTION is the section to which the
6580 relocation applies.
6581 CROSS_MODE_JUMP_P is true if the relocation field
6582 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6583
6584 Returns FALSE if anything goes wrong. */
6585
6586 static bool
6587 mips_elf_perform_relocation (struct bfd_link_info *info,
6588 reloc_howto_type *howto,
6589 const Elf_Internal_Rela *relocation,
6590 bfd_vma value, bfd *input_bfd,
6591 asection *input_section, bfd_byte *contents,
6592 bool cross_mode_jump_p)
6593 {
6594 bfd_vma x;
6595 bfd_byte *location;
6596 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6597
6598 /* Figure out where the relocation is occurring. */
6599 location = contents + relocation->r_offset;
6600
6601 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6602
6603 /* Obtain the current value. */
6604 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6605
6606 /* Clear the field we are setting. */
6607 x &= ~howto->dst_mask;
6608
6609 /* Set the field. */
6610 x |= (value & howto->dst_mask);
6611
6612 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
6613 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6614 {
6615 bfd_vma opcode = x >> 26;
6616
6617 if (r_type == R_MIPS16_26 ? opcode == 0x7
6618 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6619 : opcode == 0x1d)
6620 {
6621 info->callbacks->einfo
6622 (_("%X%H: unsupported JALX to the same ISA mode\n"),
6623 input_bfd, input_section, relocation->r_offset);
6624 return true;
6625 }
6626 }
6627 if (cross_mode_jump_p && jal_reloc_p (r_type))
6628 {
6629 bool ok;
6630 bfd_vma opcode = x >> 26;
6631 bfd_vma jalx_opcode;
6632
6633 /* Check to see if the opcode is already JAL or JALX. */
6634 if (r_type == R_MIPS16_26)
6635 {
6636 ok = ((opcode == 0x6) || (opcode == 0x7));
6637 jalx_opcode = 0x7;
6638 }
6639 else if (r_type == R_MICROMIPS_26_S1)
6640 {
6641 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6642 jalx_opcode = 0x3c;
6643 }
6644 else
6645 {
6646 ok = ((opcode == 0x3) || (opcode == 0x1d));
6647 jalx_opcode = 0x1d;
6648 }
6649
6650 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6651 convert J or JALS to JALX. */
6652 if (!ok)
6653 {
6654 info->callbacks->einfo
6655 (_("%X%H: unsupported jump between ISA modes; "
6656 "consider recompiling with interlinking enabled\n"),
6657 input_bfd, input_section, relocation->r_offset);
6658 return true;
6659 }
6660
6661 /* Make this the JALX opcode. */
6662 x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6663 }
6664 else if (cross_mode_jump_p && b_reloc_p (r_type))
6665 {
6666 bool ok = false;
6667 bfd_vma opcode = x >> 16;
6668 bfd_vma jalx_opcode = 0;
6669 bfd_vma sign_bit = 0;
6670 bfd_vma addr;
6671 bfd_vma dest;
6672
6673 if (r_type == R_MICROMIPS_PC16_S1)
6674 {
6675 ok = opcode == 0x4060;
6676 jalx_opcode = 0x3c;
6677 sign_bit = 0x10000;
6678 value <<= 1;
6679 }
6680 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6681 {
6682 ok = opcode == 0x411;
6683 jalx_opcode = 0x1d;
6684 sign_bit = 0x20000;
6685 value <<= 2;
6686 }
6687
6688 if (ok && !bfd_link_pic (info))
6689 {
6690 addr = (input_section->output_section->vma
6691 + input_section->output_offset
6692 + relocation->r_offset
6693 + 4);
6694 dest = (addr
6695 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6696
6697 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6698 {
6699 info->callbacks->einfo
6700 (_("%X%H: cannot convert branch between ISA modes "
6701 "to JALX: relocation out of range\n"),
6702 input_bfd, input_section, relocation->r_offset);
6703 return true;
6704 }
6705
6706 /* Make this the JALX opcode. */
6707 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6708 }
6709 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6710 {
6711 info->callbacks->einfo
6712 (_("%X%H: unsupported branch between ISA modes\n"),
6713 input_bfd, input_section, relocation->r_offset);
6714 return true;
6715 }
6716 }
6717
6718 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6719 range. */
6720 if (!bfd_link_relocatable (info)
6721 && !cross_mode_jump_p
6722 && ((JAL_TO_BAL_P (input_bfd)
6723 && r_type == R_MIPS_26
6724 && (x >> 26) == 0x3) /* jal addr */
6725 || (JALR_TO_BAL_P (input_bfd)
6726 && r_type == R_MIPS_JALR
6727 && x == 0x0320f809) /* jalr t9 */
6728 || (JR_TO_B_P (input_bfd)
6729 && r_type == R_MIPS_JALR
6730 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6731 {
6732 bfd_vma addr;
6733 bfd_vma dest;
6734 bfd_signed_vma off;
6735
6736 addr = (input_section->output_section->vma
6737 + input_section->output_offset
6738 + relocation->r_offset
6739 + 4);
6740 if (r_type == R_MIPS_26)
6741 dest = (value << 2) | ((addr >> 28) << 28);
6742 else
6743 dest = value;
6744 off = dest - addr;
6745 if (off <= 0x1ffff && off >= -0x20000)
6746 {
6747 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
6748 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6749 else
6750 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6751 }
6752 }
6753
6754 /* Put the value into the output. */
6755 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6756
6757 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6758 location);
6759
6760 return true;
6761 }
6762 \f
6763 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6764 is the original relocation, which is now being transformed into a
6765 dynamic relocation. The ADDENDP is adjusted if necessary; the
6766 caller should store the result in place of the original addend. */
6767
6768 static bool
6769 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6770 struct bfd_link_info *info,
6771 const Elf_Internal_Rela *rel,
6772 struct mips_elf_link_hash_entry *h,
6773 asection *sec, bfd_vma symbol,
6774 bfd_vma *addendp, asection *input_section)
6775 {
6776 Elf_Internal_Rela outrel[3];
6777 asection *sreloc;
6778 bfd *dynobj;
6779 int r_type;
6780 long indx;
6781 bool defined_p;
6782 struct mips_elf_link_hash_table *htab;
6783
6784 htab = mips_elf_hash_table (info);
6785 BFD_ASSERT (htab != NULL);
6786
6787 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6788 dynobj = elf_hash_table (info)->dynobj;
6789 sreloc = mips_elf_rel_dyn_section (info, false);
6790 BFD_ASSERT (sreloc != NULL);
6791 BFD_ASSERT (sreloc->contents != NULL);
6792 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6793 < sreloc->size);
6794
6795 outrel[0].r_offset =
6796 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6797 if (ABI_64_P (output_bfd))
6798 {
6799 outrel[1].r_offset =
6800 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6801 outrel[2].r_offset =
6802 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6803 }
6804
6805 if (outrel[0].r_offset == MINUS_ONE)
6806 /* The relocation field has been deleted. */
6807 return true;
6808
6809 if (outrel[0].r_offset == MINUS_TWO)
6810 {
6811 /* The relocation field has been converted into a relative value of
6812 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6813 the field to be fully relocated, so add in the symbol's value. */
6814 *addendp += symbol;
6815 return true;
6816 }
6817
6818 /* We must now calculate the dynamic symbol table index to use
6819 in the relocation. */
6820 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6821 {
6822 BFD_ASSERT (htab->root.target_os == is_vxworks
6823 || h->global_got_area != GGA_NONE);
6824 indx = h->root.dynindx;
6825 if (SGI_COMPAT (output_bfd))
6826 defined_p = h->root.def_regular;
6827 else
6828 /* ??? glibc's ld.so just adds the final GOT entry to the
6829 relocation field. It therefore treats relocs against
6830 defined symbols in the same way as relocs against
6831 undefined symbols. */
6832 defined_p = false;
6833 }
6834 else
6835 {
6836 if (sec != NULL && bfd_is_abs_section (sec))
6837 indx = 0;
6838 else if (sec == NULL || sec->owner == NULL)
6839 {
6840 bfd_set_error (bfd_error_bad_value);
6841 return false;
6842 }
6843 else
6844 {
6845 indx = elf_section_data (sec->output_section)->dynindx;
6846 if (indx == 0)
6847 {
6848 asection *osec = htab->root.text_index_section;
6849 indx = elf_section_data (osec)->dynindx;
6850 }
6851 if (indx == 0)
6852 abort ();
6853 }
6854
6855 /* Instead of generating a relocation using the section
6856 symbol, we may as well make it a fully relative
6857 relocation. We want to avoid generating relocations to
6858 local symbols because we used to generate them
6859 incorrectly, without adding the original symbol value,
6860 which is mandated by the ABI for section symbols. In
6861 order to give dynamic loaders and applications time to
6862 phase out the incorrect use, we refrain from emitting
6863 section-relative relocations. It's not like they're
6864 useful, after all. This should be a bit more efficient
6865 as well. */
6866 /* ??? Although this behavior is compatible with glibc's ld.so,
6867 the ABI says that relocations against STN_UNDEF should have
6868 a symbol value of 0. Irix rld honors this, so relocations
6869 against STN_UNDEF have no effect. */
6870 if (!SGI_COMPAT (output_bfd))
6871 indx = 0;
6872 defined_p = true;
6873 }
6874
6875 /* If the relocation was previously an absolute relocation and
6876 this symbol will not be referred to by the relocation, we must
6877 adjust it by the value we give it in the dynamic symbol table.
6878 Otherwise leave the job up to the dynamic linker. */
6879 if (defined_p && r_type != R_MIPS_REL32)
6880 *addendp += symbol;
6881
6882 if (htab->root.target_os == is_vxworks)
6883 /* VxWorks uses non-relative relocations for this. */
6884 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6885 else
6886 /* The relocation is always an REL32 relocation because we don't
6887 know where the shared library will wind up at load-time. */
6888 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6889 R_MIPS_REL32);
6890
6891 /* For strict adherence to the ABI specification, we should
6892 generate a R_MIPS_64 relocation record by itself before the
6893 _REL32/_64 record as well, such that the addend is read in as
6894 a 64-bit value (REL32 is a 32-bit relocation, after all).
6895 However, since none of the existing ELF64 MIPS dynamic
6896 loaders seems to care, we don't waste space with these
6897 artificial relocations. If this turns out to not be true,
6898 mips_elf_allocate_dynamic_relocation() should be tweaked so
6899 as to make room for a pair of dynamic relocations per
6900 invocation if ABI_64_P, and here we should generate an
6901 additional relocation record with R_MIPS_64 by itself for a
6902 NULL symbol before this relocation record. */
6903 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6904 ABI_64_P (output_bfd)
6905 ? R_MIPS_64
6906 : R_MIPS_NONE);
6907 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6908
6909 /* Adjust the output offset of the relocation to reference the
6910 correct location in the output file. */
6911 outrel[0].r_offset += (input_section->output_section->vma
6912 + input_section->output_offset);
6913 outrel[1].r_offset += (input_section->output_section->vma
6914 + input_section->output_offset);
6915 outrel[2].r_offset += (input_section->output_section->vma
6916 + input_section->output_offset);
6917
6918 /* Put the relocation back out. We have to use the special
6919 relocation outputter in the 64-bit case since the 64-bit
6920 relocation format is non-standard. */
6921 if (ABI_64_P (output_bfd))
6922 {
6923 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6924 (output_bfd, &outrel[0],
6925 (sreloc->contents
6926 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6927 }
6928 else if (htab->root.target_os == is_vxworks)
6929 {
6930 /* VxWorks uses RELA rather than REL dynamic relocations. */
6931 outrel[0].r_addend = *addendp;
6932 bfd_elf32_swap_reloca_out
6933 (output_bfd, &outrel[0],
6934 (sreloc->contents
6935 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6936 }
6937 else
6938 bfd_elf32_swap_reloc_out
6939 (output_bfd, &outrel[0],
6940 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6941
6942 /* We've now added another relocation. */
6943 ++sreloc->reloc_count;
6944
6945 /* Make sure the output section is writable. The dynamic linker
6946 will be writing to it. */
6947 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6948 |= SHF_WRITE;
6949
6950 /* On IRIX5, make an entry of compact relocation info. */
6951 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6952 {
6953 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6954 bfd_byte *cr;
6955
6956 if (scpt)
6957 {
6958 Elf32_crinfo cptrel;
6959
6960 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6961 cptrel.vaddr = (rel->r_offset
6962 + input_section->output_section->vma
6963 + input_section->output_offset);
6964 if (r_type == R_MIPS_REL32)
6965 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6966 else
6967 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6968 mips_elf_set_cr_dist2to (cptrel, 0);
6969 cptrel.konst = *addendp;
6970
6971 cr = (scpt->contents
6972 + sizeof (Elf32_External_compact_rel));
6973 mips_elf_set_cr_relvaddr (cptrel, 0);
6974 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6975 ((Elf32_External_crinfo *) cr
6976 + scpt->reloc_count));
6977 ++scpt->reloc_count;
6978 }
6979 }
6980
6981 /* If we've written this relocation for a readonly section,
6982 we need to set DF_TEXTREL again, so that we do not delete the
6983 DT_TEXTREL tag. */
6984 if (MIPS_ELF_READONLY_SECTION (input_section))
6985 info->flags |= DF_TEXTREL;
6986
6987 return true;
6988 }
6989 \f
6990 /* Return the MACH for a MIPS e_flags value. */
6991
6992 unsigned long
6993 _bfd_elf_mips_mach (flagword flags)
6994 {
6995 switch (flags & EF_MIPS_MACH)
6996 {
6997 case E_MIPS_MACH_3900:
6998 return bfd_mach_mips3900;
6999
7000 case E_MIPS_MACH_4010:
7001 return bfd_mach_mips4010;
7002
7003 case E_MIPS_MACH_ALLEGREX:
7004 return bfd_mach_mips_allegrex;
7005
7006 case E_MIPS_MACH_4100:
7007 return bfd_mach_mips4100;
7008
7009 case E_MIPS_MACH_4111:
7010 return bfd_mach_mips4111;
7011
7012 case E_MIPS_MACH_4120:
7013 return bfd_mach_mips4120;
7014
7015 case E_MIPS_MACH_4650:
7016 return bfd_mach_mips4650;
7017
7018 case E_MIPS_MACH_5400:
7019 return bfd_mach_mips5400;
7020
7021 case E_MIPS_MACH_5500:
7022 return bfd_mach_mips5500;
7023
7024 case E_MIPS_MACH_5900:
7025 return bfd_mach_mips5900;
7026
7027 case E_MIPS_MACH_9000:
7028 return bfd_mach_mips9000;
7029
7030 case E_MIPS_MACH_SB1:
7031 return bfd_mach_mips_sb1;
7032
7033 case E_MIPS_MACH_LS2E:
7034 return bfd_mach_mips_loongson_2e;
7035
7036 case E_MIPS_MACH_LS2F:
7037 return bfd_mach_mips_loongson_2f;
7038
7039 case E_MIPS_MACH_GS464:
7040 return bfd_mach_mips_gs464;
7041
7042 case E_MIPS_MACH_GS464E:
7043 return bfd_mach_mips_gs464e;
7044
7045 case E_MIPS_MACH_GS264E:
7046 return bfd_mach_mips_gs264e;
7047
7048 case E_MIPS_MACH_OCTEON3:
7049 return bfd_mach_mips_octeon3;
7050
7051 case E_MIPS_MACH_OCTEON2:
7052 return bfd_mach_mips_octeon2;
7053
7054 case E_MIPS_MACH_OCTEON:
7055 return bfd_mach_mips_octeon;
7056
7057 case E_MIPS_MACH_XLR:
7058 return bfd_mach_mips_xlr;
7059
7060 case E_MIPS_MACH_IAMR2:
7061 return bfd_mach_mips_interaptiv_mr2;
7062
7063 default:
7064 switch (flags & EF_MIPS_ARCH)
7065 {
7066 default:
7067 case E_MIPS_ARCH_1:
7068 return bfd_mach_mips3000;
7069
7070 case E_MIPS_ARCH_2:
7071 return bfd_mach_mips6000;
7072
7073 case E_MIPS_ARCH_3:
7074 return bfd_mach_mips4000;
7075
7076 case E_MIPS_ARCH_4:
7077 return bfd_mach_mips8000;
7078
7079 case E_MIPS_ARCH_5:
7080 return bfd_mach_mips5;
7081
7082 case E_MIPS_ARCH_32:
7083 return bfd_mach_mipsisa32;
7084
7085 case E_MIPS_ARCH_64:
7086 return bfd_mach_mipsisa64;
7087
7088 case E_MIPS_ARCH_32R2:
7089 return bfd_mach_mipsisa32r2;
7090
7091 case E_MIPS_ARCH_64R2:
7092 return bfd_mach_mipsisa64r2;
7093
7094 case E_MIPS_ARCH_32R6:
7095 return bfd_mach_mipsisa32r6;
7096
7097 case E_MIPS_ARCH_64R6:
7098 return bfd_mach_mipsisa64r6;
7099 }
7100 }
7101
7102 return 0;
7103 }
7104
7105 /* Return printable name for ABI. */
7106
7107 static inline char *
7108 elf_mips_abi_name (bfd *abfd)
7109 {
7110 flagword flags;
7111
7112 flags = elf_elfheader (abfd)->e_flags;
7113 switch (flags & EF_MIPS_ABI)
7114 {
7115 case 0:
7116 if (ABI_N32_P (abfd))
7117 return "N32";
7118 else if (ABI_64_P (abfd))
7119 return "64";
7120 else
7121 return "none";
7122 case E_MIPS_ABI_O32:
7123 return "O32";
7124 case E_MIPS_ABI_O64:
7125 return "O64";
7126 case E_MIPS_ABI_EABI32:
7127 return "EABI32";
7128 case E_MIPS_ABI_EABI64:
7129 return "EABI64";
7130 default:
7131 return "unknown abi";
7132 }
7133 }
7134 \f
7135 /* MIPS ELF uses two common sections. One is the usual one, and the
7136 other is for small objects. All the small objects are kept
7137 together, and then referenced via the gp pointer, which yields
7138 faster assembler code. This is what we use for the small common
7139 section. This approach is copied from ecoff.c. */
7140 static asection mips_elf_scom_section;
7141 static const asymbol mips_elf_scom_symbol =
7142 GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7143 static asection mips_elf_scom_section =
7144 BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7145 ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7146
7147 /* MIPS ELF also uses an acommon section, which represents an
7148 allocated common symbol which may be overridden by a
7149 definition in a shared library. */
7150 static asection mips_elf_acom_section;
7151 static const asymbol mips_elf_acom_symbol =
7152 GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7153 static asection mips_elf_acom_section =
7154 BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7155 ".acommon", 0, SEC_ALLOC);
7156
7157 /* This is used for both the 32-bit and the 64-bit ABI. */
7158
7159 void
7160 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7161 {
7162 elf_symbol_type *elfsym;
7163
7164 /* Handle the special MIPS section numbers that a symbol may use. */
7165 elfsym = (elf_symbol_type *) asym;
7166 switch (elfsym->internal_elf_sym.st_shndx)
7167 {
7168 case SHN_MIPS_ACOMMON:
7169 /* This section is used in a dynamically linked executable file.
7170 It is an allocated common section. The dynamic linker can
7171 either resolve these symbols to something in a shared
7172 library, or it can just leave them here. For our purposes,
7173 we can consider these symbols to be in a new section. */
7174 asym->section = &mips_elf_acom_section;
7175 break;
7176
7177 case SHN_COMMON:
7178 /* Common symbols less than the GP size are automatically
7179 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
7180 if (asym->value > elf_gp_size (abfd)
7181 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7182 || IRIX_COMPAT (abfd) == ict_irix6)
7183 break;
7184 /* Fall through. */
7185 case SHN_MIPS_SCOMMON:
7186 asym->section = &mips_elf_scom_section;
7187 asym->value = elfsym->internal_elf_sym.st_size;
7188 break;
7189
7190 case SHN_MIPS_SUNDEFINED:
7191 asym->section = bfd_und_section_ptr;
7192 break;
7193
7194 case SHN_MIPS_TEXT:
7195 {
7196 asection *section = bfd_get_section_by_name (abfd, ".text");
7197
7198 if (section != NULL)
7199 {
7200 asym->section = section;
7201 /* MIPS_TEXT is a bit special, the address is not an offset
7202 to the base of the .text section. So subtract the section
7203 base address to make it an offset. */
7204 asym->value -= section->vma;
7205 }
7206 }
7207 break;
7208
7209 case SHN_MIPS_DATA:
7210 {
7211 asection *section = bfd_get_section_by_name (abfd, ".data");
7212
7213 if (section != NULL)
7214 {
7215 asym->section = section;
7216 /* MIPS_DATA is a bit special, the address is not an offset
7217 to the base of the .data section. So subtract the section
7218 base address to make it an offset. */
7219 asym->value -= section->vma;
7220 }
7221 }
7222 break;
7223 }
7224
7225 /* If this is an odd-valued function symbol, assume it's a MIPS16
7226 or microMIPS one. */
7227 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7228 && (asym->value & 1) != 0)
7229 {
7230 asym->value--;
7231 if (MICROMIPS_P (abfd))
7232 elfsym->internal_elf_sym.st_other
7233 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7234 else
7235 elfsym->internal_elf_sym.st_other
7236 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7237 }
7238 }
7239 \f
7240 /* Implement elf_backend_eh_frame_address_size. This differs from
7241 the default in the way it handles EABI64.
7242
7243 EABI64 was originally specified as an LP64 ABI, and that is what
7244 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7245 historically accepted the combination of -mabi=eabi and -mlong32,
7246 and this ILP32 variation has become semi-official over time.
7247 Both forms use elf32 and have pointer-sized FDE addresses.
7248
7249 If an EABI object was generated by GCC 4.0 or above, it will have
7250 an empty .gcc_compiled_longXX section, where XX is the size of longs
7251 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7252 have no special marking to distinguish them from LP64 objects.
7253
7254 We don't want users of the official LP64 ABI to be punished for the
7255 existence of the ILP32 variant, but at the same time, we don't want
7256 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7257 We therefore take the following approach:
7258
7259 - If ABFD contains a .gcc_compiled_longXX section, use it to
7260 determine the pointer size.
7261
7262 - Otherwise check the type of the first relocation. Assume that
7263 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7264
7265 - Otherwise punt.
7266
7267 The second check is enough to detect LP64 objects generated by pre-4.0
7268 compilers because, in the kind of output generated by those compilers,
7269 the first relocation will be associated with either a CIE personality
7270 routine or an FDE start address. Furthermore, the compilers never
7271 used a special (non-pointer) encoding for this ABI.
7272
7273 Checking the relocation type should also be safe because there is no
7274 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7275 did so. */
7276
7277 unsigned int
7278 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7279 {
7280 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7281 return 8;
7282 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7283 {
7284 bool long32_p, long64_p;
7285
7286 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7287 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7288 if (long32_p && long64_p)
7289 return 0;
7290 if (long32_p)
7291 return 4;
7292 if (long64_p)
7293 return 8;
7294
7295 if (sec->reloc_count > 0
7296 && elf_section_data (sec)->relocs != NULL
7297 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7298 == R_MIPS_64))
7299 return 8;
7300
7301 return 0;
7302 }
7303 return 4;
7304 }
7305 \f
7306 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7307 relocations against two unnamed section symbols to resolve to the
7308 same address. For example, if we have code like:
7309
7310 lw $4,%got_disp(.data)($gp)
7311 lw $25,%got_disp(.text)($gp)
7312 jalr $25
7313
7314 then the linker will resolve both relocations to .data and the program
7315 will jump there rather than to .text.
7316
7317 We can work around this problem by giving names to local section symbols.
7318 This is also what the MIPSpro tools do. */
7319
7320 bool
7321 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7322 {
7323 return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7324 }
7325 \f
7326 /* Work over a section just before writing it out. This routine is
7327 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7328 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7329 a better way. */
7330
7331 bool
7332 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7333 {
7334 if (hdr->sh_type == SHT_MIPS_REGINFO
7335 && hdr->sh_size > 0)
7336 {
7337 bfd_byte buf[4];
7338
7339 BFD_ASSERT (hdr->contents == NULL);
7340
7341 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7342 {
7343 _bfd_error_handler
7344 (_("%pB: incorrect `.reginfo' section size; "
7345 "expected %" PRIu64 ", got %" PRIu64),
7346 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7347 (uint64_t) hdr->sh_size);
7348 bfd_set_error (bfd_error_bad_value);
7349 return false;
7350 }
7351
7352 if (bfd_seek (abfd,
7353 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7354 SEEK_SET) != 0)
7355 return false;
7356 H_PUT_32 (abfd, elf_gp (abfd), buf);
7357 if (bfd_bwrite (buf, 4, abfd) != 4)
7358 return false;
7359 }
7360
7361 if (hdr->sh_type == SHT_MIPS_OPTIONS
7362 && hdr->bfd_section != NULL
7363 && mips_elf_section_data (hdr->bfd_section) != NULL
7364 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7365 {
7366 bfd_byte *contents, *l, *lend;
7367
7368 /* We stored the section contents in the tdata field in the
7369 set_section_contents routine. We save the section contents
7370 so that we don't have to read them again.
7371 At this point we know that elf_gp is set, so we can look
7372 through the section contents to see if there is an
7373 ODK_REGINFO structure. */
7374
7375 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7376 l = contents;
7377 lend = contents + hdr->sh_size;
7378 while (l + sizeof (Elf_External_Options) <= lend)
7379 {
7380 Elf_Internal_Options intopt;
7381
7382 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7383 &intopt);
7384 if (intopt.size < sizeof (Elf_External_Options))
7385 {
7386 _bfd_error_handler
7387 /* xgettext:c-format */
7388 (_("%pB: warning: bad `%s' option size %u smaller than"
7389 " its header"),
7390 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7391 break;
7392 }
7393 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7394 {
7395 bfd_byte buf[8];
7396
7397 if (bfd_seek (abfd,
7398 (hdr->sh_offset
7399 + (l - contents)
7400 + sizeof (Elf_External_Options)
7401 + (sizeof (Elf64_External_RegInfo) - 8)),
7402 SEEK_SET) != 0)
7403 return false;
7404 H_PUT_64 (abfd, elf_gp (abfd), buf);
7405 if (bfd_bwrite (buf, 8, abfd) != 8)
7406 return false;
7407 }
7408 else if (intopt.kind == ODK_REGINFO)
7409 {
7410 bfd_byte buf[4];
7411
7412 if (bfd_seek (abfd,
7413 (hdr->sh_offset
7414 + (l - contents)
7415 + sizeof (Elf_External_Options)
7416 + (sizeof (Elf32_External_RegInfo) - 4)),
7417 SEEK_SET) != 0)
7418 return false;
7419 H_PUT_32 (abfd, elf_gp (abfd), buf);
7420 if (bfd_bwrite (buf, 4, abfd) != 4)
7421 return false;
7422 }
7423 l += intopt.size;
7424 }
7425 }
7426
7427 if (hdr->bfd_section != NULL)
7428 {
7429 const char *name = bfd_section_name (hdr->bfd_section);
7430
7431 /* .sbss is not handled specially here because the GNU/Linux
7432 prelinker can convert .sbss from NOBITS to PROGBITS and
7433 changing it back to NOBITS breaks the binary. The entry in
7434 _bfd_mips_elf_special_sections will ensure the correct flags
7435 are set on .sbss if BFD creates it without reading it from an
7436 input file, and without special handling here the flags set
7437 on it in an input file will be followed. */
7438 if (strcmp (name, ".sdata") == 0
7439 || strcmp (name, ".lit8") == 0
7440 || strcmp (name, ".lit4") == 0)
7441 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7442 else if (strcmp (name, ".srdata") == 0)
7443 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7444 else if (strcmp (name, ".compact_rel") == 0)
7445 hdr->sh_flags = 0;
7446 else if (strcmp (name, ".rtproc") == 0)
7447 {
7448 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7449 {
7450 unsigned int adjust;
7451
7452 adjust = hdr->sh_size % hdr->sh_addralign;
7453 if (adjust != 0)
7454 hdr->sh_size += hdr->sh_addralign - adjust;
7455 }
7456 }
7457 }
7458
7459 return true;
7460 }
7461
7462 /* Handle a MIPS specific section when reading an object file. This
7463 is called when elfcode.h finds a section with an unknown type.
7464 This routine supports both the 32-bit and 64-bit ELF ABI. */
7465
7466 bool
7467 _bfd_mips_elf_section_from_shdr (bfd *abfd,
7468 Elf_Internal_Shdr *hdr,
7469 const char *name,
7470 int shindex)
7471 {
7472 flagword flags = 0;
7473
7474 /* There ought to be a place to keep ELF backend specific flags, but
7475 at the moment there isn't one. We just keep track of the
7476 sections by their name, instead. Fortunately, the ABI gives
7477 suggested names for all the MIPS specific sections, so we will
7478 probably get away with this. */
7479 switch (hdr->sh_type)
7480 {
7481 case SHT_MIPS_LIBLIST:
7482 if (strcmp (name, ".liblist") != 0)
7483 return false;
7484 break;
7485 case SHT_MIPS_MSYM:
7486 if (strcmp (name, ".msym") != 0)
7487 return false;
7488 break;
7489 case SHT_MIPS_CONFLICT:
7490 if (strcmp (name, ".conflict") != 0)
7491 return false;
7492 break;
7493 case SHT_MIPS_GPTAB:
7494 if (! startswith (name, ".gptab."))
7495 return false;
7496 break;
7497 case SHT_MIPS_UCODE:
7498 if (strcmp (name, ".ucode") != 0)
7499 return false;
7500 break;
7501 case SHT_MIPS_DEBUG:
7502 if (strcmp (name, ".mdebug") != 0)
7503 return false;
7504 flags = SEC_DEBUGGING;
7505 break;
7506 case SHT_MIPS_REGINFO:
7507 if (strcmp (name, ".reginfo") != 0
7508 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7509 return false;
7510 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7511 break;
7512 case SHT_MIPS_IFACE:
7513 if (strcmp (name, ".MIPS.interfaces") != 0)
7514 return false;
7515 break;
7516 case SHT_MIPS_CONTENT:
7517 if (! startswith (name, ".MIPS.content"))
7518 return false;
7519 break;
7520 case SHT_MIPS_OPTIONS:
7521 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7522 return false;
7523 break;
7524 case SHT_MIPS_ABIFLAGS:
7525 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7526 return false;
7527 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7528 break;
7529 case SHT_MIPS_DWARF:
7530 if (! startswith (name, ".debug_")
7531 && ! startswith (name, ".gnu.debuglto_.debug_")
7532 && ! startswith (name, ".zdebug_")
7533 && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7534 return false;
7535 break;
7536 case SHT_MIPS_SYMBOL_LIB:
7537 if (strcmp (name, ".MIPS.symlib") != 0)
7538 return false;
7539 break;
7540 case SHT_MIPS_EVENTS:
7541 if (! startswith (name, ".MIPS.events")
7542 && ! startswith (name, ".MIPS.post_rel"))
7543 return false;
7544 break;
7545 case SHT_MIPS_XHASH:
7546 if (strcmp (name, ".MIPS.xhash") != 0)
7547 return false;
7548 default:
7549 break;
7550 }
7551
7552 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7553 return false;
7554
7555 if (hdr->sh_flags & SHF_MIPS_GPREL)
7556 flags |= SEC_SMALL_DATA;
7557
7558 if (flags)
7559 {
7560 if (!bfd_set_section_flags (hdr->bfd_section,
7561 (bfd_section_flags (hdr->bfd_section)
7562 | flags)))
7563 return false;
7564 }
7565
7566 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7567 {
7568 Elf_External_ABIFlags_v0 ext;
7569
7570 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7571 &ext, 0, sizeof ext))
7572 return false;
7573 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7574 &mips_elf_tdata (abfd)->abiflags);
7575 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7576 return false;
7577 mips_elf_tdata (abfd)->abiflags_valid = true;
7578 }
7579
7580 /* FIXME: We should record sh_info for a .gptab section. */
7581
7582 /* For a .reginfo section, set the gp value in the tdata information
7583 from the contents of this section. We need the gp value while
7584 processing relocs, so we just get it now. The .reginfo section
7585 is not used in the 64-bit MIPS ELF ABI. */
7586 if (hdr->sh_type == SHT_MIPS_REGINFO)
7587 {
7588 Elf32_External_RegInfo ext;
7589 Elf32_RegInfo s;
7590
7591 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7592 &ext, 0, sizeof ext))
7593 return false;
7594 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7595 elf_gp (abfd) = s.ri_gp_value;
7596 }
7597
7598 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7599 set the gp value based on what we find. We may see both
7600 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7601 they should agree. */
7602 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7603 {
7604 bfd_byte *contents, *l, *lend;
7605
7606 if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7607 {
7608 free (contents);
7609 return false;
7610 }
7611 l = contents;
7612 lend = contents + hdr->sh_size;
7613 while (l + sizeof (Elf_External_Options) <= lend)
7614 {
7615 Elf_Internal_Options intopt;
7616
7617 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7618 &intopt);
7619 if (intopt.size < sizeof (Elf_External_Options))
7620 {
7621 bad_opt:
7622 _bfd_error_handler
7623 /* xgettext:c-format */
7624 (_("%pB: warning: truncated `%s' option"),
7625 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7626 break;
7627 }
7628 if (intopt.kind == ODK_REGINFO)
7629 {
7630 if (ABI_64_P (abfd))
7631 {
7632 Elf64_Internal_RegInfo intreg;
7633 size_t needed = (sizeof (Elf_External_Options)
7634 + sizeof (Elf64_External_RegInfo));
7635 if (intopt.size < needed || (size_t) (lend - l) < needed)
7636 goto bad_opt;
7637 bfd_mips_elf64_swap_reginfo_in
7638 (abfd,
7639 ((Elf64_External_RegInfo *)
7640 (l + sizeof (Elf_External_Options))),
7641 &intreg);
7642 elf_gp (abfd) = intreg.ri_gp_value;
7643 }
7644 else
7645 {
7646 Elf32_RegInfo intreg;
7647 size_t needed = (sizeof (Elf_External_Options)
7648 + sizeof (Elf32_External_RegInfo));
7649 if (intopt.size < needed || (size_t) (lend - l) < needed)
7650 goto bad_opt;
7651 bfd_mips_elf32_swap_reginfo_in
7652 (abfd,
7653 ((Elf32_External_RegInfo *)
7654 (l + sizeof (Elf_External_Options))),
7655 &intreg);
7656 elf_gp (abfd) = intreg.ri_gp_value;
7657 }
7658 }
7659 l += intopt.size;
7660 }
7661 free (contents);
7662 }
7663
7664 return true;
7665 }
7666
7667 /* Set the correct type for a MIPS ELF section. We do this by the
7668 section name, which is a hack, but ought to work. This routine is
7669 used by both the 32-bit and the 64-bit ABI. */
7670
7671 bool
7672 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7673 {
7674 const char *name = bfd_section_name (sec);
7675
7676 if (strcmp (name, ".liblist") == 0)
7677 {
7678 hdr->sh_type = SHT_MIPS_LIBLIST;
7679 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7680 /* The sh_link field is set in final_write_processing. */
7681 }
7682 else if (strcmp (name, ".conflict") == 0)
7683 hdr->sh_type = SHT_MIPS_CONFLICT;
7684 else if (startswith (name, ".gptab."))
7685 {
7686 hdr->sh_type = SHT_MIPS_GPTAB;
7687 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7688 /* The sh_info field is set in final_write_processing. */
7689 }
7690 else if (strcmp (name, ".ucode") == 0)
7691 hdr->sh_type = SHT_MIPS_UCODE;
7692 else if (strcmp (name, ".mdebug") == 0)
7693 {
7694 hdr->sh_type = SHT_MIPS_DEBUG;
7695 /* In a shared object on IRIX 5.3, the .mdebug section has an
7696 entsize of 0. FIXME: Does this matter? */
7697 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7698 hdr->sh_entsize = 0;
7699 else
7700 hdr->sh_entsize = 1;
7701 }
7702 else if (strcmp (name, ".reginfo") == 0)
7703 {
7704 hdr->sh_type = SHT_MIPS_REGINFO;
7705 /* In a shared object on IRIX 5.3, the .reginfo section has an
7706 entsize of 0x18. FIXME: Does this matter? */
7707 if (SGI_COMPAT (abfd))
7708 {
7709 if ((abfd->flags & DYNAMIC) != 0)
7710 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7711 else
7712 hdr->sh_entsize = 1;
7713 }
7714 else
7715 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7716 }
7717 else if (SGI_COMPAT (abfd)
7718 && (strcmp (name, ".hash") == 0
7719 || strcmp (name, ".dynamic") == 0
7720 || strcmp (name, ".dynstr") == 0))
7721 {
7722 if (SGI_COMPAT (abfd))
7723 hdr->sh_entsize = 0;
7724 #if 0
7725 /* This isn't how the IRIX6 linker behaves. */
7726 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7727 #endif
7728 }
7729 else if (strcmp (name, ".got") == 0
7730 || strcmp (name, ".srdata") == 0
7731 || strcmp (name, ".sdata") == 0
7732 || strcmp (name, ".sbss") == 0
7733 || strcmp (name, ".lit4") == 0
7734 || strcmp (name, ".lit8") == 0)
7735 hdr->sh_flags |= SHF_MIPS_GPREL;
7736 else if (strcmp (name, ".MIPS.interfaces") == 0)
7737 {
7738 hdr->sh_type = SHT_MIPS_IFACE;
7739 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7740 }
7741 else if (startswith (name, ".MIPS.content"))
7742 {
7743 hdr->sh_type = SHT_MIPS_CONTENT;
7744 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7745 /* The sh_info field is set in final_write_processing. */
7746 }
7747 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7748 {
7749 hdr->sh_type = SHT_MIPS_OPTIONS;
7750 hdr->sh_entsize = 1;
7751 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7752 }
7753 else if (startswith (name, ".MIPS.abiflags"))
7754 {
7755 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7756 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7757 }
7758 else if (startswith (name, ".debug_")
7759 || startswith (name, ".gnu.debuglto_.debug_")
7760 || startswith (name, ".zdebug_")
7761 || startswith (name, ".gnu.debuglto_.zdebug_"))
7762 {
7763 hdr->sh_type = SHT_MIPS_DWARF;
7764
7765 /* Irix facilities such as libexc expect a single .debug_frame
7766 per executable, the system ones have NOSTRIP set and the linker
7767 doesn't merge sections with different flags so ... */
7768 if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7769 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7770 }
7771 else if (strcmp (name, ".MIPS.symlib") == 0)
7772 {
7773 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7774 /* The sh_link and sh_info fields are set in
7775 final_write_processing. */
7776 }
7777 else if (startswith (name, ".MIPS.events")
7778 || startswith (name, ".MIPS.post_rel"))
7779 {
7780 hdr->sh_type = SHT_MIPS_EVENTS;
7781 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7782 /* The sh_link field is set in final_write_processing. */
7783 }
7784 else if (strcmp (name, ".msym") == 0)
7785 {
7786 hdr->sh_type = SHT_MIPS_MSYM;
7787 hdr->sh_flags |= SHF_ALLOC;
7788 hdr->sh_entsize = 8;
7789 }
7790 else if (strcmp (name, ".MIPS.xhash") == 0)
7791 {
7792 hdr->sh_type = SHT_MIPS_XHASH;
7793 hdr->sh_flags |= SHF_ALLOC;
7794 hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7795 }
7796
7797 /* The generic elf_fake_sections will set up REL_HDR using the default
7798 kind of relocations. We used to set up a second header for the
7799 non-default kind of relocations here, but only NewABI would use
7800 these, and the IRIX ld doesn't like resulting empty RELA sections.
7801 Thus we create those header only on demand now. */
7802
7803 return true;
7804 }
7805
7806 /* Given a BFD section, try to locate the corresponding ELF section
7807 index. This is used by both the 32-bit and the 64-bit ABI.
7808 Actually, it's not clear to me that the 64-bit ABI supports these,
7809 but for non-PIC objects we will certainly want support for at least
7810 the .scommon section. */
7811
7812 bool
7813 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7814 asection *sec, int *retval)
7815 {
7816 if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7817 {
7818 *retval = SHN_MIPS_SCOMMON;
7819 return true;
7820 }
7821 if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7822 {
7823 *retval = SHN_MIPS_ACOMMON;
7824 return true;
7825 }
7826 return false;
7827 }
7828 \f
7829 /* Hook called by the linker routine which adds symbols from an object
7830 file. We must handle the special MIPS section numbers here. */
7831
7832 bool
7833 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7834 Elf_Internal_Sym *sym, const char **namep,
7835 flagword *flagsp ATTRIBUTE_UNUSED,
7836 asection **secp, bfd_vma *valp)
7837 {
7838 if (SGI_COMPAT (abfd)
7839 && (abfd->flags & DYNAMIC) != 0
7840 && strcmp (*namep, "_rld_new_interface") == 0)
7841 {
7842 /* Skip IRIX5 rld entry name. */
7843 *namep = NULL;
7844 return true;
7845 }
7846
7847 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7848 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7849 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7850 a magic symbol resolved by the linker, we ignore this bogus definition
7851 of _gp_disp. New ABI objects do not suffer from this problem so this
7852 is not done for them. */
7853 if (!NEWABI_P(abfd)
7854 && (sym->st_shndx == SHN_ABS)
7855 && (strcmp (*namep, "_gp_disp") == 0))
7856 {
7857 *namep = NULL;
7858 return true;
7859 }
7860
7861 switch (sym->st_shndx)
7862 {
7863 case SHN_COMMON:
7864 /* Common symbols less than the GP size are automatically
7865 treated as SHN_MIPS_SCOMMON symbols. */
7866 if (sym->st_size > elf_gp_size (abfd)
7867 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7868 || IRIX_COMPAT (abfd) == ict_irix6)
7869 break;
7870 /* Fall through. */
7871 case SHN_MIPS_SCOMMON:
7872 *secp = bfd_make_section_old_way (abfd, ".scommon");
7873 (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7874 *valp = sym->st_size;
7875 break;
7876
7877 case SHN_MIPS_TEXT:
7878 /* This section is used in a shared object. */
7879 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7880 {
7881 asymbol *elf_text_symbol;
7882 asection *elf_text_section;
7883 size_t amt = sizeof (asection);
7884
7885 elf_text_section = bfd_zalloc (abfd, amt);
7886 if (elf_text_section == NULL)
7887 return false;
7888
7889 amt = sizeof (asymbol);
7890 elf_text_symbol = bfd_zalloc (abfd, amt);
7891 if (elf_text_symbol == NULL)
7892 return false;
7893
7894 /* Initialize the section. */
7895
7896 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7897 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7898
7899 elf_text_section->symbol = elf_text_symbol;
7900 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7901
7902 elf_text_section->name = ".text";
7903 elf_text_section->flags = SEC_NO_FLAGS;
7904 elf_text_section->output_section = NULL;
7905 elf_text_section->owner = abfd;
7906 elf_text_symbol->name = ".text";
7907 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7908 elf_text_symbol->section = elf_text_section;
7909 }
7910 /* This code used to do *secp = bfd_und_section_ptr if
7911 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7912 so I took it out. */
7913 *secp = mips_elf_tdata (abfd)->elf_text_section;
7914 break;
7915
7916 case SHN_MIPS_ACOMMON:
7917 /* Fall through. XXX Can we treat this as allocated data? */
7918 case SHN_MIPS_DATA:
7919 /* This section is used in a shared object. */
7920 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7921 {
7922 asymbol *elf_data_symbol;
7923 asection *elf_data_section;
7924 size_t amt = sizeof (asection);
7925
7926 elf_data_section = bfd_zalloc (abfd, amt);
7927 if (elf_data_section == NULL)
7928 return false;
7929
7930 amt = sizeof (asymbol);
7931 elf_data_symbol = bfd_zalloc (abfd, amt);
7932 if (elf_data_symbol == NULL)
7933 return false;
7934
7935 /* Initialize the section. */
7936
7937 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7938 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7939
7940 elf_data_section->symbol = elf_data_symbol;
7941 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7942
7943 elf_data_section->name = ".data";
7944 elf_data_section->flags = SEC_NO_FLAGS;
7945 elf_data_section->output_section = NULL;
7946 elf_data_section->owner = abfd;
7947 elf_data_symbol->name = ".data";
7948 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7949 elf_data_symbol->section = elf_data_section;
7950 }
7951 /* This code used to do *secp = bfd_und_section_ptr if
7952 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7953 so I took it out. */
7954 *secp = mips_elf_tdata (abfd)->elf_data_section;
7955 break;
7956
7957 case SHN_MIPS_SUNDEFINED:
7958 *secp = bfd_und_section_ptr;
7959 break;
7960 }
7961
7962 if (SGI_COMPAT (abfd)
7963 && ! bfd_link_pic (info)
7964 && info->output_bfd->xvec == abfd->xvec
7965 && strcmp (*namep, "__rld_obj_head") == 0)
7966 {
7967 struct elf_link_hash_entry *h;
7968 struct bfd_link_hash_entry *bh;
7969
7970 /* Mark __rld_obj_head as dynamic. */
7971 bh = NULL;
7972 if (! (_bfd_generic_link_add_one_symbol
7973 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
7974 get_elf_backend_data (abfd)->collect, &bh)))
7975 return false;
7976
7977 h = (struct elf_link_hash_entry *) bh;
7978 h->non_elf = 0;
7979 h->def_regular = 1;
7980 h->type = STT_OBJECT;
7981
7982 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7983 return false;
7984
7985 mips_elf_hash_table (info)->use_rld_obj_head = true;
7986 mips_elf_hash_table (info)->rld_symbol = h;
7987 }
7988
7989 /* If this is a mips16 text symbol, add 1 to the value to make it
7990 odd. This will cause something like .word SYM to come up with
7991 the right value when it is loaded into the PC. */
7992 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7993 ++*valp;
7994
7995 return true;
7996 }
7997
7998 /* This hook function is called before the linker writes out a global
7999 symbol. We mark symbols as small common if appropriate. This is
8000 also where we undo the increment of the value for a mips16 symbol. */
8001
8002 int
8003 _bfd_mips_elf_link_output_symbol_hook
8004 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
8005 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
8006 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
8007 {
8008 /* If we see a common symbol, which implies a relocatable link, then
8009 if a symbol was small common in an input file, mark it as small
8010 common in the output file. */
8011 if (sym->st_shndx == SHN_COMMON
8012 && strcmp (input_sec->name, ".scommon") == 0)
8013 sym->st_shndx = SHN_MIPS_SCOMMON;
8014
8015 if (ELF_ST_IS_COMPRESSED (sym->st_other))
8016 sym->st_value &= ~1;
8017
8018 return 1;
8019 }
8020 \f
8021 /* Functions for the dynamic linker. */
8022
8023 /* Create dynamic sections when linking against a dynamic object. */
8024
8025 bool
8026 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8027 {
8028 struct elf_link_hash_entry *h;
8029 struct bfd_link_hash_entry *bh;
8030 flagword flags;
8031 register asection *s;
8032 const char * const *namep;
8033 struct mips_elf_link_hash_table *htab;
8034
8035 htab = mips_elf_hash_table (info);
8036 BFD_ASSERT (htab != NULL);
8037
8038 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8039 | SEC_LINKER_CREATED | SEC_READONLY);
8040
8041 /* The psABI requires a read-only .dynamic section, but the VxWorks
8042 EABI doesn't. */
8043 if (htab->root.target_os != is_vxworks)
8044 {
8045 s = bfd_get_linker_section (abfd, ".dynamic");
8046 if (s != NULL)
8047 {
8048 if (!bfd_set_section_flags (s, flags))
8049 return false;
8050 }
8051 }
8052
8053 /* We need to create .got section. */
8054 if (!mips_elf_create_got_section (abfd, info))
8055 return false;
8056
8057 if (! mips_elf_rel_dyn_section (info, true))
8058 return false;
8059
8060 /* Create .stub section. */
8061 s = bfd_make_section_anyway_with_flags (abfd,
8062 MIPS_ELF_STUB_SECTION_NAME (abfd),
8063 flags | SEC_CODE);
8064 if (s == NULL
8065 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8066 return false;
8067 htab->sstubs = s;
8068
8069 if (!mips_elf_hash_table (info)->use_rld_obj_head
8070 && bfd_link_executable (info)
8071 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8072 {
8073 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8074 flags &~ (flagword) SEC_READONLY);
8075 if (s == NULL
8076 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8077 return false;
8078 }
8079
8080 /* Create .MIPS.xhash section. */
8081 if (info->emit_gnu_hash)
8082 s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8083 flags | SEC_READONLY);
8084
8085 /* On IRIX5, we adjust add some additional symbols and change the
8086 alignments of several sections. There is no ABI documentation
8087 indicating that this is necessary on IRIX6, nor any evidence that
8088 the linker takes such action. */
8089 if (IRIX_COMPAT (abfd) == ict_irix5)
8090 {
8091 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8092 {
8093 bh = NULL;
8094 if (! (_bfd_generic_link_add_one_symbol
8095 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8096 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8097 return false;
8098
8099 h = (struct elf_link_hash_entry *) bh;
8100 h->mark = 1;
8101 h->non_elf = 0;
8102 h->def_regular = 1;
8103 h->type = STT_SECTION;
8104
8105 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8106 return false;
8107 }
8108
8109 /* We need to create a .compact_rel section. */
8110 if (SGI_COMPAT (abfd))
8111 {
8112 if (!mips_elf_create_compact_rel_section (abfd, info))
8113 return false;
8114 }
8115
8116 /* Change alignments of some sections. */
8117 s = bfd_get_linker_section (abfd, ".hash");
8118 if (s != NULL)
8119 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8120
8121 s = bfd_get_linker_section (abfd, ".dynsym");
8122 if (s != NULL)
8123 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8124
8125 s = bfd_get_linker_section (abfd, ".dynstr");
8126 if (s != NULL)
8127 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8128
8129 /* ??? */
8130 s = bfd_get_section_by_name (abfd, ".reginfo");
8131 if (s != NULL)
8132 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8133
8134 s = bfd_get_linker_section (abfd, ".dynamic");
8135 if (s != NULL)
8136 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8137 }
8138
8139 if (bfd_link_executable (info))
8140 {
8141 const char *name;
8142
8143 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8144 bh = NULL;
8145 if (!(_bfd_generic_link_add_one_symbol
8146 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8147 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8148 return false;
8149
8150 h = (struct elf_link_hash_entry *) bh;
8151 h->non_elf = 0;
8152 h->def_regular = 1;
8153 h->type = STT_SECTION;
8154
8155 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8156 return false;
8157
8158 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8159 {
8160 /* __rld_map is a four byte word located in the .data section
8161 and is filled in by the rtld to contain a pointer to
8162 the _r_debug structure. Its symbol value will be set in
8163 _bfd_mips_elf_finish_dynamic_symbol. */
8164 s = bfd_get_linker_section (abfd, ".rld_map");
8165 BFD_ASSERT (s != NULL);
8166
8167 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8168 bh = NULL;
8169 if (!(_bfd_generic_link_add_one_symbol
8170 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8171 get_elf_backend_data (abfd)->collect, &bh)))
8172 return false;
8173
8174 h = (struct elf_link_hash_entry *) bh;
8175 h->non_elf = 0;
8176 h->def_regular = 1;
8177 h->type = STT_OBJECT;
8178
8179 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8180 return false;
8181 mips_elf_hash_table (info)->rld_symbol = h;
8182 }
8183 }
8184
8185 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8186 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
8187 if (!_bfd_elf_create_dynamic_sections (abfd, info))
8188 return false;
8189
8190 /* Do the usual VxWorks handling. */
8191 if (htab->root.target_os == is_vxworks
8192 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8193 return false;
8194
8195 return true;
8196 }
8197 \f
8198 /* Return true if relocation REL against section SEC is a REL rather than
8199 RELA relocation. RELOCS is the first relocation in the section and
8200 ABFD is the bfd that contains SEC. */
8201
8202 static bool
8203 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8204 const Elf_Internal_Rela *relocs,
8205 const Elf_Internal_Rela *rel)
8206 {
8207 Elf_Internal_Shdr *rel_hdr;
8208 const struct elf_backend_data *bed;
8209
8210 /* To determine which flavor of relocation this is, we depend on the
8211 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8212 rel_hdr = elf_section_data (sec)->rel.hdr;
8213 if (rel_hdr == NULL)
8214 return false;
8215 bed = get_elf_backend_data (abfd);
8216 return ((size_t) (rel - relocs)
8217 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8218 }
8219
8220 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8221 HOWTO is the relocation's howto and CONTENTS points to the contents
8222 of the section that REL is against. */
8223
8224 static bfd_vma
8225 mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8226 const Elf_Internal_Rela *rel,
8227 reloc_howto_type *howto, bfd_byte *contents)
8228 {
8229 bfd_byte *location;
8230 unsigned int r_type;
8231 bfd_vma addend;
8232 bfd_vma bytes;
8233
8234 if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8235 return 0;
8236
8237 r_type = ELF_R_TYPE (abfd, rel->r_info);
8238 location = contents + rel->r_offset;
8239
8240 /* Get the addend, which is stored in the input file. */
8241 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8242 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8243 _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8244
8245 addend = bytes & howto->src_mask;
8246
8247 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8248 accordingly. */
8249 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8250 addend <<= 1;
8251
8252 return addend;
8253 }
8254
8255 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
8256 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8257 and update *ADDEND with the final addend. Return true on success
8258 or false if the LO16 could not be found. RELEND is the exclusive
8259 upper bound on the relocations for REL's section. */
8260
8261 static bool
8262 mips_elf_add_lo16_rel_addend (bfd *abfd,
8263 asection *sec,
8264 const Elf_Internal_Rela *rel,
8265 const Elf_Internal_Rela *relend,
8266 bfd_byte *contents, bfd_vma *addend)
8267 {
8268 unsigned int r_type, lo16_type;
8269 const Elf_Internal_Rela *lo16_relocation;
8270 reloc_howto_type *lo16_howto;
8271 bfd_vma l;
8272
8273 r_type = ELF_R_TYPE (abfd, rel->r_info);
8274 if (mips16_reloc_p (r_type))
8275 lo16_type = R_MIPS16_LO16;
8276 else if (micromips_reloc_p (r_type))
8277 lo16_type = R_MICROMIPS_LO16;
8278 else if (r_type == R_MIPS_PCHI16)
8279 lo16_type = R_MIPS_PCLO16;
8280 else
8281 lo16_type = R_MIPS_LO16;
8282
8283 /* The combined value is the sum of the HI16 addend, left-shifted by
8284 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8285 code does a `lui' of the HI16 value, and then an `addiu' of the
8286 LO16 value.)
8287
8288 Scan ahead to find a matching LO16 relocation.
8289
8290 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8291 be immediately following. However, for the IRIX6 ABI, the next
8292 relocation may be a composed relocation consisting of several
8293 relocations for the same address. In that case, the R_MIPS_LO16
8294 relocation may occur as one of these. We permit a similar
8295 extension in general, as that is useful for GCC.
8296
8297 In some cases GCC dead code elimination removes the LO16 but keeps
8298 the corresponding HI16. This is strictly speaking a violation of
8299 the ABI but not immediately harmful. */
8300 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8301 if (lo16_relocation == NULL)
8302 return false;
8303
8304 /* Obtain the addend kept there. */
8305 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8306 l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8307 contents);
8308
8309 l <<= lo16_howto->rightshift;
8310 l = _bfd_mips_elf_sign_extend (l, 16);
8311
8312 *addend <<= 16;
8313 *addend += l;
8314 return true;
8315 }
8316
8317 /* Try to read the contents of section SEC in bfd ABFD. Return true and
8318 store the contents in *CONTENTS on success. Assume that *CONTENTS
8319 already holds the contents if it is nonull on entry. */
8320
8321 static bool
8322 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8323 {
8324 if (*contents)
8325 return true;
8326
8327 /* Get cached copy if it exists. */
8328 if (elf_section_data (sec)->this_hdr.contents != NULL)
8329 {
8330 *contents = elf_section_data (sec)->this_hdr.contents;
8331 return true;
8332 }
8333
8334 return bfd_malloc_and_get_section (abfd, sec, contents);
8335 }
8336
8337 /* Make a new PLT record to keep internal data. */
8338
8339 static struct plt_entry *
8340 mips_elf_make_plt_record (bfd *abfd)
8341 {
8342 struct plt_entry *entry;
8343
8344 entry = bfd_zalloc (abfd, sizeof (*entry));
8345 if (entry == NULL)
8346 return NULL;
8347
8348 entry->stub_offset = MINUS_ONE;
8349 entry->mips_offset = MINUS_ONE;
8350 entry->comp_offset = MINUS_ONE;
8351 entry->gotplt_index = MINUS_ONE;
8352 return entry;
8353 }
8354
8355 /* Define the special `__gnu_absolute_zero' symbol. We only need this
8356 for PIC code, as otherwise there is no load-time relocation involved
8357 and local GOT entries whose value is zero at static link time will
8358 retain their value at load time. */
8359
8360 static bool
8361 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8362 struct mips_elf_link_hash_table *htab,
8363 unsigned int r_type)
8364 {
8365 union
8366 {
8367 struct elf_link_hash_entry *eh;
8368 struct bfd_link_hash_entry *bh;
8369 }
8370 hzero;
8371
8372 BFD_ASSERT (!htab->use_absolute_zero);
8373 BFD_ASSERT (bfd_link_pic (info));
8374
8375 hzero.bh = NULL;
8376 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8377 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8378 NULL, false, false, &hzero.bh))
8379 return false;
8380
8381 BFD_ASSERT (hzero.bh != NULL);
8382 hzero.eh->size = 0;
8383 hzero.eh->type = STT_NOTYPE;
8384 hzero.eh->other = STV_PROTECTED;
8385 hzero.eh->def_regular = 1;
8386 hzero.eh->non_elf = 0;
8387
8388 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8389 return false;
8390
8391 htab->use_absolute_zero = true;
8392
8393 return true;
8394 }
8395
8396 /* Look through the relocs for a section during the first phase, and
8397 allocate space in the global offset table and record the need for
8398 standard MIPS and compressed procedure linkage table entries. */
8399
8400 bool
8401 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8402 asection *sec, const Elf_Internal_Rela *relocs)
8403 {
8404 const char *name;
8405 bfd *dynobj;
8406 Elf_Internal_Shdr *symtab_hdr;
8407 struct elf_link_hash_entry **sym_hashes;
8408 size_t extsymoff;
8409 const Elf_Internal_Rela *rel;
8410 const Elf_Internal_Rela *rel_end;
8411 asection *sreloc;
8412 const struct elf_backend_data *bed;
8413 struct mips_elf_link_hash_table *htab;
8414 bfd_byte *contents;
8415 bfd_vma addend;
8416 reloc_howto_type *howto;
8417
8418 if (bfd_link_relocatable (info))
8419 return true;
8420
8421 htab = mips_elf_hash_table (info);
8422 BFD_ASSERT (htab != NULL);
8423
8424 dynobj = elf_hash_table (info)->dynobj;
8425 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8426 sym_hashes = elf_sym_hashes (abfd);
8427 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8428
8429 bed = get_elf_backend_data (abfd);
8430 rel_end = relocs + sec->reloc_count;
8431
8432 /* Check for the mips16 stub sections. */
8433
8434 name = bfd_section_name (sec);
8435 if (FN_STUB_P (name))
8436 {
8437 unsigned long r_symndx;
8438
8439 /* Look at the relocation information to figure out which symbol
8440 this is for. */
8441
8442 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8443 if (r_symndx == 0)
8444 {
8445 _bfd_error_handler
8446 /* xgettext:c-format */
8447 (_("%pB: warning: cannot determine the target function for"
8448 " stub section `%s'"),
8449 abfd, name);
8450 bfd_set_error (bfd_error_bad_value);
8451 return false;
8452 }
8453
8454 if (r_symndx < extsymoff
8455 || sym_hashes[r_symndx - extsymoff] == NULL)
8456 {
8457 asection *o;
8458
8459 /* This stub is for a local symbol. This stub will only be
8460 needed if there is some relocation in this BFD, other
8461 than a 16 bit function call, which refers to this symbol. */
8462 for (o = abfd->sections; o != NULL; o = o->next)
8463 {
8464 Elf_Internal_Rela *sec_relocs;
8465 const Elf_Internal_Rela *r, *rend;
8466
8467 /* We can ignore stub sections when looking for relocs. */
8468 if ((o->flags & SEC_RELOC) == 0
8469 || o->reloc_count == 0
8470 || section_allows_mips16_refs_p (o))
8471 continue;
8472
8473 sec_relocs
8474 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8475 info->keep_memory);
8476 if (sec_relocs == NULL)
8477 return false;
8478
8479 rend = sec_relocs + o->reloc_count;
8480 for (r = sec_relocs; r < rend; r++)
8481 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8482 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8483 break;
8484
8485 if (elf_section_data (o)->relocs != sec_relocs)
8486 free (sec_relocs);
8487
8488 if (r < rend)
8489 break;
8490 }
8491
8492 if (o == NULL)
8493 {
8494 /* There is no non-call reloc for this stub, so we do
8495 not need it. Since this function is called before
8496 the linker maps input sections to output sections, we
8497 can easily discard it by setting the SEC_EXCLUDE
8498 flag. */
8499 sec->flags |= SEC_EXCLUDE;
8500 return true;
8501 }
8502
8503 /* Record this stub in an array of local symbol stubs for
8504 this BFD. */
8505 if (mips_elf_tdata (abfd)->local_stubs == NULL)
8506 {
8507 unsigned long symcount;
8508 asection **n;
8509 bfd_size_type amt;
8510
8511 if (elf_bad_symtab (abfd))
8512 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8513 else
8514 symcount = symtab_hdr->sh_info;
8515 amt = symcount * sizeof (asection *);
8516 n = bfd_zalloc (abfd, amt);
8517 if (n == NULL)
8518 return false;
8519 mips_elf_tdata (abfd)->local_stubs = n;
8520 }
8521
8522 sec->flags |= SEC_KEEP;
8523 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8524
8525 /* We don't need to set mips16_stubs_seen in this case.
8526 That flag is used to see whether we need to look through
8527 the global symbol table for stubs. We don't need to set
8528 it here, because we just have a local stub. */
8529 }
8530 else
8531 {
8532 struct mips_elf_link_hash_entry *h;
8533
8534 h = ((struct mips_elf_link_hash_entry *)
8535 sym_hashes[r_symndx - extsymoff]);
8536
8537 while (h->root.root.type == bfd_link_hash_indirect
8538 || h->root.root.type == bfd_link_hash_warning)
8539 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8540
8541 /* H is the symbol this stub is for. */
8542
8543 /* If we already have an appropriate stub for this function, we
8544 don't need another one, so we can discard this one. Since
8545 this function is called before the linker maps input sections
8546 to output sections, we can easily discard it by setting the
8547 SEC_EXCLUDE flag. */
8548 if (h->fn_stub != NULL)
8549 {
8550 sec->flags |= SEC_EXCLUDE;
8551 return true;
8552 }
8553
8554 sec->flags |= SEC_KEEP;
8555 h->fn_stub = sec;
8556 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8557 }
8558 }
8559 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8560 {
8561 unsigned long r_symndx;
8562 struct mips_elf_link_hash_entry *h;
8563 asection **loc;
8564
8565 /* Look at the relocation information to figure out which symbol
8566 this is for. */
8567
8568 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8569 if (r_symndx == 0)
8570 {
8571 _bfd_error_handler
8572 /* xgettext:c-format */
8573 (_("%pB: warning: cannot determine the target function for"
8574 " stub section `%s'"),
8575 abfd, name);
8576 bfd_set_error (bfd_error_bad_value);
8577 return false;
8578 }
8579
8580 if (r_symndx < extsymoff
8581 || sym_hashes[r_symndx - extsymoff] == NULL)
8582 {
8583 asection *o;
8584
8585 /* This stub is for a local symbol. This stub will only be
8586 needed if there is some relocation (R_MIPS16_26) in this BFD
8587 that refers to this symbol. */
8588 for (o = abfd->sections; o != NULL; o = o->next)
8589 {
8590 Elf_Internal_Rela *sec_relocs;
8591 const Elf_Internal_Rela *r, *rend;
8592
8593 /* We can ignore stub sections when looking for relocs. */
8594 if ((o->flags & SEC_RELOC) == 0
8595 || o->reloc_count == 0
8596 || section_allows_mips16_refs_p (o))
8597 continue;
8598
8599 sec_relocs
8600 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8601 info->keep_memory);
8602 if (sec_relocs == NULL)
8603 return false;
8604
8605 rend = sec_relocs + o->reloc_count;
8606 for (r = sec_relocs; r < rend; r++)
8607 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8608 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8609 break;
8610
8611 if (elf_section_data (o)->relocs != sec_relocs)
8612 free (sec_relocs);
8613
8614 if (r < rend)
8615 break;
8616 }
8617
8618 if (o == NULL)
8619 {
8620 /* There is no non-call reloc for this stub, so we do
8621 not need it. Since this function is called before
8622 the linker maps input sections to output sections, we
8623 can easily discard it by setting the SEC_EXCLUDE
8624 flag. */
8625 sec->flags |= SEC_EXCLUDE;
8626 return true;
8627 }
8628
8629 /* Record this stub in an array of local symbol call_stubs for
8630 this BFD. */
8631 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8632 {
8633 unsigned long symcount;
8634 asection **n;
8635 bfd_size_type amt;
8636
8637 if (elf_bad_symtab (abfd))
8638 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8639 else
8640 symcount = symtab_hdr->sh_info;
8641 amt = symcount * sizeof (asection *);
8642 n = bfd_zalloc (abfd, amt);
8643 if (n == NULL)
8644 return false;
8645 mips_elf_tdata (abfd)->local_call_stubs = n;
8646 }
8647
8648 sec->flags |= SEC_KEEP;
8649 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8650
8651 /* We don't need to set mips16_stubs_seen in this case.
8652 That flag is used to see whether we need to look through
8653 the global symbol table for stubs. We don't need to set
8654 it here, because we just have a local stub. */
8655 }
8656 else
8657 {
8658 h = ((struct mips_elf_link_hash_entry *)
8659 sym_hashes[r_symndx - extsymoff]);
8660
8661 /* H is the symbol this stub is for. */
8662
8663 if (CALL_FP_STUB_P (name))
8664 loc = &h->call_fp_stub;
8665 else
8666 loc = &h->call_stub;
8667
8668 /* If we already have an appropriate stub for this function, we
8669 don't need another one, so we can discard this one. Since
8670 this function is called before the linker maps input sections
8671 to output sections, we can easily discard it by setting the
8672 SEC_EXCLUDE flag. */
8673 if (*loc != NULL)
8674 {
8675 sec->flags |= SEC_EXCLUDE;
8676 return true;
8677 }
8678
8679 sec->flags |= SEC_KEEP;
8680 *loc = sec;
8681 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8682 }
8683 }
8684
8685 sreloc = NULL;
8686 contents = NULL;
8687 for (rel = relocs; rel < rel_end; ++rel)
8688 {
8689 unsigned long r_symndx;
8690 unsigned int r_type;
8691 struct elf_link_hash_entry *h;
8692 bool can_make_dynamic_p;
8693 bool call_reloc_p;
8694 bool constrain_symbol_p;
8695
8696 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8697 r_type = ELF_R_TYPE (abfd, rel->r_info);
8698
8699 if (r_symndx < extsymoff)
8700 h = NULL;
8701 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8702 {
8703 _bfd_error_handler
8704 /* xgettext:c-format */
8705 (_("%pB: malformed reloc detected for section %s"),
8706 abfd, name);
8707 bfd_set_error (bfd_error_bad_value);
8708 return false;
8709 }
8710 else
8711 {
8712 h = sym_hashes[r_symndx - extsymoff];
8713 if (h != NULL)
8714 {
8715 while (h->root.type == bfd_link_hash_indirect
8716 || h->root.type == bfd_link_hash_warning)
8717 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8718 }
8719 }
8720
8721 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8722 relocation into a dynamic one. */
8723 can_make_dynamic_p = false;
8724
8725 /* Set CALL_RELOC_P to true if the relocation is for a call,
8726 and if pointer equality therefore doesn't matter. */
8727 call_reloc_p = false;
8728
8729 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8730 into account when deciding how to define the symbol. */
8731 constrain_symbol_p = true;
8732
8733 switch (r_type)
8734 {
8735 case R_MIPS_CALL16:
8736 case R_MIPS_CALL_HI16:
8737 case R_MIPS_CALL_LO16:
8738 case R_MIPS16_CALL16:
8739 case R_MICROMIPS_CALL16:
8740 case R_MICROMIPS_CALL_HI16:
8741 case R_MICROMIPS_CALL_LO16:
8742 call_reloc_p = true;
8743 /* Fall through. */
8744
8745 case R_MIPS_GOT16:
8746 case R_MIPS_GOT_LO16:
8747 case R_MIPS_GOT_PAGE:
8748 case R_MIPS_GOT_DISP:
8749 case R_MIPS16_GOT16:
8750 case R_MICROMIPS_GOT16:
8751 case R_MICROMIPS_GOT_LO16:
8752 case R_MICROMIPS_GOT_PAGE:
8753 case R_MICROMIPS_GOT_DISP:
8754 /* If we have a symbol that will resolve to zero at static link
8755 time and it is used by a GOT relocation applied to code we
8756 cannot relax to an immediate zero load, then we will be using
8757 the special `__gnu_absolute_zero' symbol whose value is zero
8758 at dynamic load time. We ignore HI16-type GOT relocations at
8759 this stage, because their handling will depend entirely on
8760 the corresponding LO16-type GOT relocation. */
8761 if (!call_hi16_reloc_p (r_type)
8762 && h != NULL
8763 && bfd_link_pic (info)
8764 && !htab->use_absolute_zero
8765 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8766 {
8767 bool rel_reloc;
8768
8769 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8770 return false;
8771
8772 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8773 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8774 if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8775 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8776 false))
8777 if (!mips_elf_define_absolute_zero (abfd, info, htab,
8778 r_type))
8779 return false;
8780 }
8781
8782 /* Fall through. */
8783 case R_MIPS_GOT_HI16:
8784 case R_MIPS_GOT_OFST:
8785 case R_MIPS_TLS_GOTTPREL:
8786 case R_MIPS_TLS_GD:
8787 case R_MIPS_TLS_LDM:
8788 case R_MIPS16_TLS_GOTTPREL:
8789 case R_MIPS16_TLS_GD:
8790 case R_MIPS16_TLS_LDM:
8791 case R_MICROMIPS_GOT_HI16:
8792 case R_MICROMIPS_GOT_OFST:
8793 case R_MICROMIPS_TLS_GOTTPREL:
8794 case R_MICROMIPS_TLS_GD:
8795 case R_MICROMIPS_TLS_LDM:
8796 if (dynobj == NULL)
8797 elf_hash_table (info)->dynobj = dynobj = abfd;
8798 if (!mips_elf_create_got_section (dynobj, info))
8799 return false;
8800 if (htab->root.target_os == is_vxworks
8801 && !bfd_link_pic (info))
8802 {
8803 _bfd_error_handler
8804 /* xgettext:c-format */
8805 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8806 abfd, (uint64_t) rel->r_offset);
8807 bfd_set_error (bfd_error_bad_value);
8808 return false;
8809 }
8810 can_make_dynamic_p = true;
8811 break;
8812
8813 case R_MIPS_NONE:
8814 case R_MIPS_JALR:
8815 case R_MICROMIPS_JALR:
8816 /* These relocations have empty fields and are purely there to
8817 provide link information. The symbol value doesn't matter. */
8818 constrain_symbol_p = false;
8819 break;
8820
8821 case R_MIPS_GPREL16:
8822 case R_MIPS_GPREL32:
8823 case R_MIPS16_GPREL:
8824 case R_MICROMIPS_GPREL16:
8825 /* GP-relative relocations always resolve to a definition in a
8826 regular input file, ignoring the one-definition rule. This is
8827 important for the GP setup sequence in NewABI code, which
8828 always resolves to a local function even if other relocations
8829 against the symbol wouldn't. */
8830 constrain_symbol_p = false;
8831 break;
8832
8833 case R_MIPS_32:
8834 case R_MIPS_REL32:
8835 case R_MIPS_64:
8836 /* In VxWorks executables, references to external symbols
8837 must be handled using copy relocs or PLT entries; it is not
8838 possible to convert this relocation into a dynamic one.
8839
8840 For executables that use PLTs and copy-relocs, we have a
8841 choice between converting the relocation into a dynamic
8842 one or using copy relocations or PLT entries. It is
8843 usually better to do the former, unless the relocation is
8844 against a read-only section. */
8845 if ((bfd_link_pic (info)
8846 || (h != NULL
8847 && htab->root.target_os != is_vxworks
8848 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8849 && !(!info->nocopyreloc
8850 && !PIC_OBJECT_P (abfd)
8851 && MIPS_ELF_READONLY_SECTION (sec))))
8852 && (sec->flags & SEC_ALLOC) != 0)
8853 {
8854 can_make_dynamic_p = true;
8855 if (dynobj == NULL)
8856 elf_hash_table (info)->dynobj = dynobj = abfd;
8857 }
8858 break;
8859
8860 case R_MIPS_26:
8861 case R_MIPS_PC16:
8862 case R_MIPS_PC21_S2:
8863 case R_MIPS_PC26_S2:
8864 case R_MIPS16_26:
8865 case R_MIPS16_PC16_S1:
8866 case R_MICROMIPS_26_S1:
8867 case R_MICROMIPS_PC7_S1:
8868 case R_MICROMIPS_PC10_S1:
8869 case R_MICROMIPS_PC16_S1:
8870 case R_MICROMIPS_PC23_S2:
8871 call_reloc_p = true;
8872 break;
8873 }
8874
8875 if (h)
8876 {
8877 if (constrain_symbol_p)
8878 {
8879 if (!can_make_dynamic_p)
8880 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8881
8882 if (!call_reloc_p)
8883 h->pointer_equality_needed = 1;
8884
8885 /* We must not create a stub for a symbol that has
8886 relocations related to taking the function's address.
8887 This doesn't apply to VxWorks, where CALL relocs refer
8888 to a .got.plt entry instead of a normal .got entry. */
8889 if (htab->root.target_os != is_vxworks
8890 && (!can_make_dynamic_p || !call_reloc_p))
8891 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8892 }
8893
8894 /* Relocations against the special VxWorks __GOTT_BASE__ and
8895 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8896 room for them in .rela.dyn. */
8897 if (is_gott_symbol (info, h))
8898 {
8899 if (sreloc == NULL)
8900 {
8901 sreloc = mips_elf_rel_dyn_section (info, true);
8902 if (sreloc == NULL)
8903 return false;
8904 }
8905 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8906 if (MIPS_ELF_READONLY_SECTION (sec))
8907 /* We tell the dynamic linker that there are
8908 relocations against the text segment. */
8909 info->flags |= DF_TEXTREL;
8910 }
8911 }
8912 else if (call_lo16_reloc_p (r_type)
8913 || got_lo16_reloc_p (r_type)
8914 || got_disp_reloc_p (r_type)
8915 || (got16_reloc_p (r_type)
8916 && htab->root.target_os == is_vxworks))
8917 {
8918 /* We may need a local GOT entry for this relocation. We
8919 don't count R_MIPS_GOT_PAGE because we can estimate the
8920 maximum number of pages needed by looking at the size of
8921 the segment. Similar comments apply to R_MIPS*_GOT16 and
8922 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8923 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8924 R_MIPS_CALL_HI16 because these are always followed by an
8925 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8926 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8927 rel->r_addend, info, r_type))
8928 return false;
8929 }
8930
8931 if (h != NULL
8932 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8933 ELF_ST_IS_MIPS16 (h->other)))
8934 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8935
8936 switch (r_type)
8937 {
8938 case R_MIPS_CALL16:
8939 case R_MIPS16_CALL16:
8940 case R_MICROMIPS_CALL16:
8941 if (h == NULL)
8942 {
8943 _bfd_error_handler
8944 /* xgettext:c-format */
8945 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8946 abfd, (uint64_t) rel->r_offset);
8947 bfd_set_error (bfd_error_bad_value);
8948 return false;
8949 }
8950 /* Fall through. */
8951
8952 case R_MIPS_CALL_HI16:
8953 case R_MIPS_CALL_LO16:
8954 case R_MICROMIPS_CALL_HI16:
8955 case R_MICROMIPS_CALL_LO16:
8956 if (h != NULL)
8957 {
8958 /* Make sure there is room in the regular GOT to hold the
8959 function's address. We may eliminate it in favour of
8960 a .got.plt entry later; see mips_elf_count_got_symbols. */
8961 if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8962 r_type))
8963 return false;
8964
8965 /* We need a stub, not a plt entry for the undefined
8966 function. But we record it as if it needs plt. See
8967 _bfd_elf_adjust_dynamic_symbol. */
8968 h->needs_plt = 1;
8969 h->type = STT_FUNC;
8970 }
8971 break;
8972
8973 case R_MIPS_GOT_PAGE:
8974 case R_MICROMIPS_GOT_PAGE:
8975 case R_MIPS16_GOT16:
8976 case R_MIPS_GOT16:
8977 case R_MIPS_GOT_HI16:
8978 case R_MIPS_GOT_LO16:
8979 case R_MICROMIPS_GOT16:
8980 case R_MICROMIPS_GOT_HI16:
8981 case R_MICROMIPS_GOT_LO16:
8982 if (!h || got_page_reloc_p (r_type))
8983 {
8984 /* This relocation needs (or may need, if h != NULL) a
8985 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8986 know for sure until we know whether the symbol is
8987 preemptible. */
8988 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8989 {
8990 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8991 return false;
8992 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
8993 addend = mips_elf_read_rel_addend (abfd, sec, rel,
8994 howto, contents);
8995 if (got16_reloc_p (r_type))
8996 mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
8997 contents, &addend);
8998 else
8999 addend <<= howto->rightshift;
9000 }
9001 else
9002 addend = rel->r_addend;
9003 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
9004 h, addend))
9005 return false;
9006
9007 if (h)
9008 {
9009 struct mips_elf_link_hash_entry *hmips =
9010 (struct mips_elf_link_hash_entry *) h;
9011
9012 /* This symbol is definitely not overridable. */
9013 if (hmips->root.def_regular
9014 && ! (bfd_link_pic (info) && ! info->symbolic
9015 && ! hmips->root.forced_local))
9016 h = NULL;
9017 }
9018 }
9019 /* If this is a global, overridable symbol, GOT_PAGE will
9020 decay to GOT_DISP, so we'll need a GOT entry for it. */
9021 /* Fall through. */
9022
9023 case R_MIPS_GOT_DISP:
9024 case R_MICROMIPS_GOT_DISP:
9025 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9026 false, r_type))
9027 return false;
9028 break;
9029
9030 case R_MIPS_TLS_GOTTPREL:
9031 case R_MIPS16_TLS_GOTTPREL:
9032 case R_MICROMIPS_TLS_GOTTPREL:
9033 if (bfd_link_pic (info))
9034 info->flags |= DF_STATIC_TLS;
9035 /* Fall through */
9036
9037 case R_MIPS_TLS_LDM:
9038 case R_MIPS16_TLS_LDM:
9039 case R_MICROMIPS_TLS_LDM:
9040 if (tls_ldm_reloc_p (r_type))
9041 {
9042 r_symndx = STN_UNDEF;
9043 h = NULL;
9044 }
9045 /* Fall through */
9046
9047 case R_MIPS_TLS_GD:
9048 case R_MIPS16_TLS_GD:
9049 case R_MICROMIPS_TLS_GD:
9050 /* This symbol requires a global offset table entry, or two
9051 for TLS GD relocations. */
9052 if (h != NULL)
9053 {
9054 if (!mips_elf_record_global_got_symbol (h, abfd, info,
9055 false, r_type))
9056 return false;
9057 }
9058 else
9059 {
9060 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9061 rel->r_addend,
9062 info, r_type))
9063 return false;
9064 }
9065 break;
9066
9067 case R_MIPS_32:
9068 case R_MIPS_REL32:
9069 case R_MIPS_64:
9070 /* In VxWorks executables, references to external symbols
9071 are handled using copy relocs or PLT stubs, so there's
9072 no need to add a .rela.dyn entry for this relocation. */
9073 if (can_make_dynamic_p)
9074 {
9075 if (sreloc == NULL)
9076 {
9077 sreloc = mips_elf_rel_dyn_section (info, true);
9078 if (sreloc == NULL)
9079 return false;
9080 }
9081 if (bfd_link_pic (info) && h == NULL)
9082 {
9083 /* When creating a shared object, we must copy these
9084 reloc types into the output file as R_MIPS_REL32
9085 relocs. Make room for this reloc in .rel(a).dyn. */
9086 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9087 if (MIPS_ELF_READONLY_SECTION (sec))
9088 /* We tell the dynamic linker that there are
9089 relocations against the text segment. */
9090 info->flags |= DF_TEXTREL;
9091 }
9092 else
9093 {
9094 struct mips_elf_link_hash_entry *hmips;
9095
9096 /* For a shared object, we must copy this relocation
9097 unless the symbol turns out to be undefined and
9098 weak with non-default visibility, in which case
9099 it will be left as zero.
9100
9101 We could elide R_MIPS_REL32 for locally binding symbols
9102 in shared libraries, but do not yet do so.
9103
9104 For an executable, we only need to copy this
9105 reloc if the symbol is defined in a dynamic
9106 object. */
9107 hmips = (struct mips_elf_link_hash_entry *) h;
9108 ++hmips->possibly_dynamic_relocs;
9109 if (MIPS_ELF_READONLY_SECTION (sec))
9110 /* We need it to tell the dynamic linker if there
9111 are relocations against the text segment. */
9112 hmips->readonly_reloc = true;
9113 }
9114 }
9115
9116 if (SGI_COMPAT (abfd))
9117 mips_elf_hash_table (info)->compact_rel_size +=
9118 sizeof (Elf32_External_crinfo);
9119 break;
9120
9121 case R_MIPS_26:
9122 case R_MIPS_GPREL16:
9123 case R_MIPS_LITERAL:
9124 case R_MIPS_GPREL32:
9125 case R_MICROMIPS_26_S1:
9126 case R_MICROMIPS_GPREL16:
9127 case R_MICROMIPS_LITERAL:
9128 case R_MICROMIPS_GPREL7_S2:
9129 if (SGI_COMPAT (abfd))
9130 mips_elf_hash_table (info)->compact_rel_size +=
9131 sizeof (Elf32_External_crinfo);
9132 break;
9133
9134 /* This relocation describes the C++ object vtable hierarchy.
9135 Reconstruct it for later use during GC. */
9136 case R_MIPS_GNU_VTINHERIT:
9137 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9138 return false;
9139 break;
9140
9141 /* This relocation describes which C++ vtable entries are actually
9142 used. Record for later use during GC. */
9143 case R_MIPS_GNU_VTENTRY:
9144 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9145 return false;
9146 break;
9147
9148 default:
9149 break;
9150 }
9151
9152 /* Record the need for a PLT entry. At this point we don't know
9153 yet if we are going to create a PLT in the first place, but
9154 we only record whether the relocation requires a standard MIPS
9155 or a compressed code entry anyway. If we don't make a PLT after
9156 all, then we'll just ignore these arrangements. Likewise if
9157 a PLT entry is not created because the symbol is satisfied
9158 locally. */
9159 if (h != NULL
9160 && (branch_reloc_p (r_type)
9161 || mips16_branch_reloc_p (r_type)
9162 || micromips_branch_reloc_p (r_type))
9163 && !SYMBOL_CALLS_LOCAL (info, h))
9164 {
9165 if (h->plt.plist == NULL)
9166 h->plt.plist = mips_elf_make_plt_record (abfd);
9167 if (h->plt.plist == NULL)
9168 return false;
9169
9170 if (branch_reloc_p (r_type))
9171 h->plt.plist->need_mips = true;
9172 else
9173 h->plt.plist->need_comp = true;
9174 }
9175
9176 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9177 if there is one. We only need to handle global symbols here;
9178 we decide whether to keep or delete stubs for local symbols
9179 when processing the stub's relocations. */
9180 if (h != NULL
9181 && !mips16_call_reloc_p (r_type)
9182 && !section_allows_mips16_refs_p (sec))
9183 {
9184 struct mips_elf_link_hash_entry *mh;
9185
9186 mh = (struct mips_elf_link_hash_entry *) h;
9187 mh->need_fn_stub = true;
9188 }
9189
9190 /* Refuse some position-dependent relocations when creating a
9191 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9192 not PIC, but we can create dynamic relocations and the result
9193 will be fine. Also do not refuse R_MIPS_LO16, which can be
9194 combined with R_MIPS_GOT16. */
9195 if (bfd_link_pic (info))
9196 {
9197 switch (r_type)
9198 {
9199 case R_MIPS_TLS_TPREL_HI16:
9200 case R_MIPS16_TLS_TPREL_HI16:
9201 case R_MICROMIPS_TLS_TPREL_HI16:
9202 case R_MIPS_TLS_TPREL_LO16:
9203 case R_MIPS16_TLS_TPREL_LO16:
9204 case R_MICROMIPS_TLS_TPREL_LO16:
9205 /* These are okay in PIE, but not in a shared library. */
9206 if (bfd_link_executable (info))
9207 break;
9208
9209 /* FALLTHROUGH */
9210
9211 case R_MIPS16_HI16:
9212 case R_MIPS_HI16:
9213 case R_MIPS_HIGHER:
9214 case R_MIPS_HIGHEST:
9215 case R_MICROMIPS_HI16:
9216 case R_MICROMIPS_HIGHER:
9217 case R_MICROMIPS_HIGHEST:
9218 /* Don't refuse a high part relocation if it's against
9219 no symbol (e.g. part of a compound relocation). */
9220 if (r_symndx == STN_UNDEF)
9221 break;
9222
9223 /* Likewise an absolute symbol. */
9224 if (h != NULL && bfd_is_abs_symbol (&h->root))
9225 break;
9226
9227 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9228 and has a special meaning. */
9229 if (!NEWABI_P (abfd) && h != NULL
9230 && strcmp (h->root.root.string, "_gp_disp") == 0)
9231 break;
9232
9233 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9234 if (is_gott_symbol (info, h))
9235 break;
9236
9237 /* FALLTHROUGH */
9238
9239 case R_MIPS16_26:
9240 case R_MIPS_26:
9241 case R_MICROMIPS_26_S1:
9242 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9243 /* An error for unsupported relocations is raised as part
9244 of the above search, so we can skip the following. */
9245 if (howto != NULL)
9246 info->callbacks->einfo
9247 /* xgettext:c-format */
9248 (_("%X%H: relocation %s against `%s' cannot be used"
9249 " when making a shared object; recompile with -fPIC\n"),
9250 abfd, sec, rel->r_offset, howto->name,
9251 (h) ? h->root.root.string : "a local symbol");
9252 break;
9253 default:
9254 break;
9255 }
9256 }
9257 }
9258
9259 return true;
9260 }
9261 \f
9262 /* Allocate space for global sym dynamic relocs. */
9263
9264 static bool
9265 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9266 {
9267 struct bfd_link_info *info = inf;
9268 bfd *dynobj;
9269 struct mips_elf_link_hash_entry *hmips;
9270 struct mips_elf_link_hash_table *htab;
9271
9272 htab = mips_elf_hash_table (info);
9273 BFD_ASSERT (htab != NULL);
9274
9275 dynobj = elf_hash_table (info)->dynobj;
9276 hmips = (struct mips_elf_link_hash_entry *) h;
9277
9278 /* VxWorks executables are handled elsewhere; we only need to
9279 allocate relocations in shared objects. */
9280 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9281 return true;
9282
9283 /* Ignore indirect symbols. All relocations against such symbols
9284 will be redirected to the target symbol. */
9285 if (h->root.type == bfd_link_hash_indirect)
9286 return true;
9287
9288 /* If this symbol is defined in a dynamic object, or we are creating
9289 a shared library, we will need to copy any R_MIPS_32 or
9290 R_MIPS_REL32 relocs against it into the output file. */
9291 if (! bfd_link_relocatable (info)
9292 && hmips->possibly_dynamic_relocs != 0
9293 && (h->root.type == bfd_link_hash_defweak
9294 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9295 || bfd_link_pic (info)))
9296 {
9297 bool do_copy = true;
9298
9299 if (h->root.type == bfd_link_hash_undefweak)
9300 {
9301 /* Do not copy relocations for undefined weak symbols that
9302 we are not going to export. */
9303 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9304 do_copy = false;
9305
9306 /* Make sure undefined weak symbols are output as a dynamic
9307 symbol in PIEs. */
9308 else if (h->dynindx == -1 && !h->forced_local)
9309 {
9310 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9311 return false;
9312 }
9313 }
9314
9315 if (do_copy)
9316 {
9317 /* Even though we don't directly need a GOT entry for this symbol,
9318 the SVR4 psABI requires it to have a dynamic symbol table
9319 index greater that DT_MIPS_GOTSYM if there are dynamic
9320 relocations against it.
9321
9322 VxWorks does not enforce the same mapping between the GOT
9323 and the symbol table, so the same requirement does not
9324 apply there. */
9325 if (htab->root.target_os != is_vxworks)
9326 {
9327 if (hmips->global_got_area > GGA_RELOC_ONLY)
9328 hmips->global_got_area = GGA_RELOC_ONLY;
9329 hmips->got_only_for_calls = false;
9330 }
9331
9332 mips_elf_allocate_dynamic_relocations
9333 (dynobj, info, hmips->possibly_dynamic_relocs);
9334 if (hmips->readonly_reloc)
9335 /* We tell the dynamic linker that there are relocations
9336 against the text segment. */
9337 info->flags |= DF_TEXTREL;
9338 }
9339 }
9340
9341 return true;
9342 }
9343
9344 /* Adjust a symbol defined by a dynamic object and referenced by a
9345 regular object. The current definition is in some section of the
9346 dynamic object, but we're not including those sections. We have to
9347 change the definition to something the rest of the link can
9348 understand. */
9349
9350 bool
9351 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9352 struct elf_link_hash_entry *h)
9353 {
9354 bfd *dynobj;
9355 struct mips_elf_link_hash_entry *hmips;
9356 struct mips_elf_link_hash_table *htab;
9357 asection *s, *srel;
9358
9359 htab = mips_elf_hash_table (info);
9360 BFD_ASSERT (htab != NULL);
9361
9362 dynobj = elf_hash_table (info)->dynobj;
9363 hmips = (struct mips_elf_link_hash_entry *) h;
9364
9365 /* Make sure we know what is going on here. */
9366 if (dynobj == NULL
9367 || (! h->needs_plt
9368 && ! h->is_weakalias
9369 && (! h->def_dynamic
9370 || ! h->ref_regular
9371 || h->def_regular)))
9372 {
9373 if (h->type == STT_GNU_IFUNC)
9374 _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9375 h->root.root.string);
9376 else
9377 _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9378 h->root.root.string);
9379 return true;
9380 }
9381
9382 hmips = (struct mips_elf_link_hash_entry *) h;
9383
9384 /* If there are call relocations against an externally-defined symbol,
9385 see whether we can create a MIPS lazy-binding stub for it. We can
9386 only do this if all references to the function are through call
9387 relocations, and in that case, the traditional lazy-binding stubs
9388 are much more efficient than PLT entries.
9389
9390 Traditional stubs are only available on SVR4 psABI-based systems;
9391 VxWorks always uses PLTs instead. */
9392 if (htab->root.target_os != is_vxworks
9393 && h->needs_plt
9394 && !hmips->no_fn_stub)
9395 {
9396 if (! elf_hash_table (info)->dynamic_sections_created)
9397 return true;
9398
9399 /* If this symbol is not defined in a regular file, then set
9400 the symbol to the stub location. This is required to make
9401 function pointers compare as equal between the normal
9402 executable and the shared library. */
9403 if (!h->def_regular
9404 && !bfd_is_abs_section (htab->sstubs->output_section))
9405 {
9406 hmips->needs_lazy_stub = true;
9407 htab->lazy_stub_count++;
9408 return true;
9409 }
9410 }
9411 /* As above, VxWorks requires PLT entries for externally-defined
9412 functions that are only accessed through call relocations.
9413
9414 Both VxWorks and non-VxWorks targets also need PLT entries if there
9415 are static-only relocations against an externally-defined function.
9416 This can technically occur for shared libraries if there are
9417 branches to the symbol, although it is unlikely that this will be
9418 used in practice due to the short ranges involved. It can occur
9419 for any relative or absolute relocation in executables; in that
9420 case, the PLT entry becomes the function's canonical address. */
9421 else if (((h->needs_plt && !hmips->no_fn_stub)
9422 || (h->type == STT_FUNC && hmips->has_static_relocs))
9423 && htab->use_plts_and_copy_relocs
9424 && !SYMBOL_CALLS_LOCAL (info, h)
9425 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9426 && h->root.type == bfd_link_hash_undefweak))
9427 {
9428 bool micromips_p = MICROMIPS_P (info->output_bfd);
9429 bool newabi_p = NEWABI_P (info->output_bfd);
9430
9431 /* If this is the first symbol to need a PLT entry, then make some
9432 basic setup. Also work out PLT entry sizes. We'll need them
9433 for PLT offset calculations. */
9434 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9435 {
9436 BFD_ASSERT (htab->root.sgotplt->size == 0);
9437 BFD_ASSERT (htab->plt_got_index == 0);
9438
9439 /* If we're using the PLT additions to the psABI, each PLT
9440 entry is 16 bytes and the PLT0 entry is 32 bytes.
9441 Encourage better cache usage by aligning. We do this
9442 lazily to avoid pessimizing traditional objects. */
9443 if (htab->root.target_os != is_vxworks
9444 && !bfd_set_section_alignment (htab->root.splt, 5))
9445 return false;
9446
9447 /* Make sure that .got.plt is word-aligned. We do this lazily
9448 for the same reason as above. */
9449 if (!bfd_set_section_alignment (htab->root.sgotplt,
9450 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9451 return false;
9452
9453 /* On non-VxWorks targets, the first two entries in .got.plt
9454 are reserved. */
9455 if (htab->root.target_os != is_vxworks)
9456 htab->plt_got_index
9457 += (get_elf_backend_data (dynobj)->got_header_size
9458 / MIPS_ELF_GOT_SIZE (dynobj));
9459
9460 /* On VxWorks, also allocate room for the header's
9461 .rela.plt.unloaded entries. */
9462 if (htab->root.target_os == is_vxworks
9463 && !bfd_link_pic (info))
9464 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9465
9466 /* Now work out the sizes of individual PLT entries. */
9467 if (htab->root.target_os == is_vxworks
9468 && bfd_link_pic (info))
9469 htab->plt_mips_entry_size
9470 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9471 else if (htab->root.target_os == is_vxworks)
9472 htab->plt_mips_entry_size
9473 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9474 else if (newabi_p)
9475 htab->plt_mips_entry_size
9476 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9477 else if (!micromips_p)
9478 {
9479 htab->plt_mips_entry_size
9480 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9481 htab->plt_comp_entry_size
9482 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9483 }
9484 else if (htab->insn32)
9485 {
9486 htab->plt_mips_entry_size
9487 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9488 htab->plt_comp_entry_size
9489 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9490 }
9491 else
9492 {
9493 htab->plt_mips_entry_size
9494 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9495 htab->plt_comp_entry_size
9496 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9497 }
9498 }
9499
9500 if (h->plt.plist == NULL)
9501 h->plt.plist = mips_elf_make_plt_record (dynobj);
9502 if (h->plt.plist == NULL)
9503 return false;
9504
9505 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9506 n32 or n64, so always use a standard entry there.
9507
9508 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9509 all MIPS16 calls will go via that stub, and there is no benefit
9510 to having a MIPS16 entry. And in the case of call_stub a
9511 standard entry actually has to be used as the stub ends with a J
9512 instruction. */
9513 if (newabi_p
9514 || htab->root.target_os == is_vxworks
9515 || hmips->call_stub
9516 || hmips->call_fp_stub)
9517 {
9518 h->plt.plist->need_mips = true;
9519 h->plt.plist->need_comp = false;
9520 }
9521
9522 /* Otherwise, if there are no direct calls to the function, we
9523 have a free choice of whether to use standard or compressed
9524 entries. Prefer microMIPS entries if the object is known to
9525 contain microMIPS code, so that it becomes possible to create
9526 pure microMIPS binaries. Prefer standard entries otherwise,
9527 because MIPS16 ones are no smaller and are usually slower. */
9528 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9529 {
9530 if (micromips_p)
9531 h->plt.plist->need_comp = true;
9532 else
9533 h->plt.plist->need_mips = true;
9534 }
9535
9536 if (h->plt.plist->need_mips)
9537 {
9538 h->plt.plist->mips_offset = htab->plt_mips_offset;
9539 htab->plt_mips_offset += htab->plt_mips_entry_size;
9540 }
9541 if (h->plt.plist->need_comp)
9542 {
9543 h->plt.plist->comp_offset = htab->plt_comp_offset;
9544 htab->plt_comp_offset += htab->plt_comp_entry_size;
9545 }
9546
9547 /* Reserve the corresponding .got.plt entry now too. */
9548 h->plt.plist->gotplt_index = htab->plt_got_index++;
9549
9550 /* If the output file has no definition of the symbol, set the
9551 symbol's value to the address of the stub. */
9552 if (!bfd_link_pic (info) && !h->def_regular)
9553 hmips->use_plt_entry = true;
9554
9555 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
9556 htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9557 ? MIPS_ELF_RELA_SIZE (dynobj)
9558 : MIPS_ELF_REL_SIZE (dynobj));
9559
9560 /* Make room for the .rela.plt.unloaded relocations. */
9561 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9562 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9563
9564 /* All relocations against this symbol that could have been made
9565 dynamic will now refer to the PLT entry instead. */
9566 hmips->possibly_dynamic_relocs = 0;
9567
9568 return true;
9569 }
9570
9571 /* If this is a weak symbol, and there is a real definition, the
9572 processor independent code will have arranged for us to see the
9573 real definition first, and we can just use the same value. */
9574 if (h->is_weakalias)
9575 {
9576 struct elf_link_hash_entry *def = weakdef (h);
9577 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9578 h->root.u.def.section = def->root.u.def.section;
9579 h->root.u.def.value = def->root.u.def.value;
9580 return true;
9581 }
9582
9583 /* Otherwise, there is nothing further to do for symbols defined
9584 in regular objects. */
9585 if (h->def_regular)
9586 return true;
9587
9588 /* There's also nothing more to do if we'll convert all relocations
9589 against this symbol into dynamic relocations. */
9590 if (!hmips->has_static_relocs)
9591 return true;
9592
9593 /* We're now relying on copy relocations. Complain if we have
9594 some that we can't convert. */
9595 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9596 {
9597 _bfd_error_handler (_("non-dynamic relocations refer to "
9598 "dynamic symbol %s"),
9599 h->root.root.string);
9600 bfd_set_error (bfd_error_bad_value);
9601 return false;
9602 }
9603
9604 /* We must allocate the symbol in our .dynbss section, which will
9605 become part of the .bss section of the executable. There will be
9606 an entry for this symbol in the .dynsym section. The dynamic
9607 object will contain position independent code, so all references
9608 from the dynamic object to this symbol will go through the global
9609 offset table. The dynamic linker will use the .dynsym entry to
9610 determine the address it must put in the global offset table, so
9611 both the dynamic object and the regular object will refer to the
9612 same memory location for the variable. */
9613
9614 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9615 {
9616 s = htab->root.sdynrelro;
9617 srel = htab->root.sreldynrelro;
9618 }
9619 else
9620 {
9621 s = htab->root.sdynbss;
9622 srel = htab->root.srelbss;
9623 }
9624 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9625 {
9626 if (htab->root.target_os == is_vxworks)
9627 srel->size += sizeof (Elf32_External_Rela);
9628 else
9629 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9630 h->needs_copy = 1;
9631 }
9632
9633 /* All relocations against this symbol that could have been made
9634 dynamic will now refer to the local copy instead. */
9635 hmips->possibly_dynamic_relocs = 0;
9636
9637 return _bfd_elf_adjust_dynamic_copy (info, h, s);
9638 }
9639 \f
9640 /* This function is called after all the input files have been read,
9641 and the input sections have been assigned to output sections. We
9642 check for any mips16 stub sections that we can discard. */
9643
9644 bool
9645 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9646 struct bfd_link_info *info)
9647 {
9648 asection *sect;
9649 struct mips_elf_link_hash_table *htab;
9650 struct mips_htab_traverse_info hti;
9651
9652 htab = mips_elf_hash_table (info);
9653 BFD_ASSERT (htab != NULL);
9654
9655 /* The .reginfo section has a fixed size. */
9656 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9657 if (sect != NULL)
9658 {
9659 bfd_set_section_size (sect, sizeof (Elf32_External_RegInfo));
9660 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9661 }
9662
9663 /* The .MIPS.abiflags section has a fixed size. */
9664 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9665 if (sect != NULL)
9666 {
9667 bfd_set_section_size (sect, sizeof (Elf_External_ABIFlags_v0));
9668 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9669 }
9670
9671 hti.info = info;
9672 hti.output_bfd = output_bfd;
9673 hti.error = false;
9674 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9675 mips_elf_check_symbols, &hti);
9676 if (hti.error)
9677 return false;
9678
9679 return true;
9680 }
9681
9682 /* If the link uses a GOT, lay it out and work out its size. */
9683
9684 static bool
9685 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9686 {
9687 bfd *dynobj;
9688 asection *s;
9689 struct mips_got_info *g;
9690 bfd_size_type loadable_size = 0;
9691 bfd_size_type page_gotno;
9692 bfd *ibfd;
9693 struct mips_elf_traverse_got_arg tga;
9694 struct mips_elf_link_hash_table *htab;
9695
9696 htab = mips_elf_hash_table (info);
9697 BFD_ASSERT (htab != NULL);
9698
9699 s = htab->root.sgot;
9700 if (s == NULL)
9701 return true;
9702
9703 dynobj = elf_hash_table (info)->dynobj;
9704 g = htab->got_info;
9705
9706 /* Allocate room for the reserved entries. VxWorks always reserves
9707 3 entries; other objects only reserve 2 entries. */
9708 BFD_ASSERT (g->assigned_low_gotno == 0);
9709 if (htab->root.target_os == is_vxworks)
9710 htab->reserved_gotno = 3;
9711 else
9712 htab->reserved_gotno = 2;
9713 g->local_gotno += htab->reserved_gotno;
9714 g->assigned_low_gotno = htab->reserved_gotno;
9715
9716 /* Decide which symbols need to go in the global part of the GOT and
9717 count the number of reloc-only GOT symbols. */
9718 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9719
9720 if (!mips_elf_resolve_final_got_entries (info, g))
9721 return false;
9722
9723 /* Calculate the total loadable size of the output. That
9724 will give us the maximum number of GOT_PAGE entries
9725 required. */
9726 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9727 {
9728 asection *subsection;
9729
9730 for (subsection = ibfd->sections;
9731 subsection;
9732 subsection = subsection->next)
9733 {
9734 if ((subsection->flags & SEC_ALLOC) == 0)
9735 continue;
9736 loadable_size += ((subsection->size + 0xf)
9737 &~ (bfd_size_type) 0xf);
9738 }
9739 }
9740
9741 if (htab->root.target_os == is_vxworks)
9742 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9743 relocations against local symbols evaluate to "G", and the EABI does
9744 not include R_MIPS_GOT_PAGE. */
9745 page_gotno = 0;
9746 else
9747 /* Assume there are two loadable segments consisting of contiguous
9748 sections. Is 5 enough? */
9749 page_gotno = (loadable_size >> 16) + 5;
9750
9751 /* Choose the smaller of the two page estimates; both are intended to be
9752 conservative. */
9753 if (page_gotno > g->page_gotno)
9754 page_gotno = g->page_gotno;
9755
9756 g->local_gotno += page_gotno;
9757 g->assigned_high_gotno = g->local_gotno - 1;
9758
9759 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9760 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9761 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9762
9763 /* VxWorks does not support multiple GOTs. It initializes $gp to
9764 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9765 dynamic loader. */
9766 if (htab->root.target_os != is_vxworks
9767 && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9768 {
9769 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9770 return false;
9771 }
9772 else
9773 {
9774 /* Record that all bfds use G. This also has the effect of freeing
9775 the per-bfd GOTs, which we no longer need. */
9776 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9777 if (mips_elf_bfd_got (ibfd, false))
9778 mips_elf_replace_bfd_got (ibfd, g);
9779 mips_elf_replace_bfd_got (output_bfd, g);
9780
9781 /* Set up TLS entries. */
9782 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9783 tga.info = info;
9784 tga.g = g;
9785 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9786 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9787 if (!tga.g)
9788 return false;
9789 BFD_ASSERT (g->tls_assigned_gotno
9790 == g->global_gotno + g->local_gotno + g->tls_gotno);
9791
9792 /* Each VxWorks GOT entry needs an explicit relocation. */
9793 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9794 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9795
9796 /* Allocate room for the TLS relocations. */
9797 if (g->relocs)
9798 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9799 }
9800
9801 return true;
9802 }
9803
9804 /* Estimate the size of the .MIPS.stubs section. */
9805
9806 static void
9807 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9808 {
9809 struct mips_elf_link_hash_table *htab;
9810 bfd_size_type dynsymcount;
9811
9812 htab = mips_elf_hash_table (info);
9813 BFD_ASSERT (htab != NULL);
9814
9815 if (htab->lazy_stub_count == 0)
9816 return;
9817
9818 /* IRIX rld assumes that a function stub isn't at the end of the .text
9819 section, so add a dummy entry to the end. */
9820 htab->lazy_stub_count++;
9821
9822 /* Get a worst-case estimate of the number of dynamic symbols needed.
9823 At this point, dynsymcount does not account for section symbols
9824 and count_section_dynsyms may overestimate the number that will
9825 be needed. */
9826 dynsymcount = (elf_hash_table (info)->dynsymcount
9827 + count_section_dynsyms (output_bfd, info));
9828
9829 /* Determine the size of one stub entry. There's no disadvantage
9830 from using microMIPS code here, so for the sake of pure-microMIPS
9831 binaries we prefer it whenever there's any microMIPS code in
9832 output produced at all. This has a benefit of stubs being
9833 shorter by 4 bytes each too, unless in the insn32 mode. */
9834 if (!MICROMIPS_P (output_bfd))
9835 htab->function_stub_size = (dynsymcount > 0x10000
9836 ? MIPS_FUNCTION_STUB_BIG_SIZE
9837 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9838 else if (htab->insn32)
9839 htab->function_stub_size = (dynsymcount > 0x10000
9840 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9841 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9842 else
9843 htab->function_stub_size = (dynsymcount > 0x10000
9844 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9845 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9846
9847 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9848 }
9849
9850 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9851 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9852 stub, allocate an entry in the stubs section. */
9853
9854 static bool
9855 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9856 {
9857 struct mips_htab_traverse_info *hti = data;
9858 struct mips_elf_link_hash_table *htab;
9859 struct bfd_link_info *info;
9860 bfd *output_bfd;
9861
9862 info = hti->info;
9863 output_bfd = hti->output_bfd;
9864 htab = mips_elf_hash_table (info);
9865 BFD_ASSERT (htab != NULL);
9866
9867 if (h->needs_lazy_stub)
9868 {
9869 bool micromips_p = MICROMIPS_P (output_bfd);
9870 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9871 bfd_vma isa_bit = micromips_p;
9872
9873 BFD_ASSERT (htab->root.dynobj != NULL);
9874 if (h->root.plt.plist == NULL)
9875 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9876 if (h->root.plt.plist == NULL)
9877 {
9878 hti->error = true;
9879 return false;
9880 }
9881 h->root.root.u.def.section = htab->sstubs;
9882 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9883 h->root.plt.plist->stub_offset = htab->sstubs->size;
9884 h->root.other = other;
9885 htab->sstubs->size += htab->function_stub_size;
9886 }
9887 return true;
9888 }
9889
9890 /* Allocate offsets in the stubs section to each symbol that needs one.
9891 Set the final size of the .MIPS.stub section. */
9892
9893 static bool
9894 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9895 {
9896 bfd *output_bfd = info->output_bfd;
9897 bool micromips_p = MICROMIPS_P (output_bfd);
9898 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9899 bfd_vma isa_bit = micromips_p;
9900 struct mips_elf_link_hash_table *htab;
9901 struct mips_htab_traverse_info hti;
9902 struct elf_link_hash_entry *h;
9903 bfd *dynobj;
9904
9905 htab = mips_elf_hash_table (info);
9906 BFD_ASSERT (htab != NULL);
9907
9908 if (htab->lazy_stub_count == 0)
9909 return true;
9910
9911 htab->sstubs->size = 0;
9912 hti.info = info;
9913 hti.output_bfd = output_bfd;
9914 hti.error = false;
9915 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9916 if (hti.error)
9917 return false;
9918 htab->sstubs->size += htab->function_stub_size;
9919 BFD_ASSERT (htab->sstubs->size
9920 == htab->lazy_stub_count * htab->function_stub_size);
9921
9922 dynobj = elf_hash_table (info)->dynobj;
9923 BFD_ASSERT (dynobj != NULL);
9924 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9925 if (h == NULL)
9926 return false;
9927 h->root.u.def.value = isa_bit;
9928 h->other = other;
9929 h->type = STT_FUNC;
9930
9931 return true;
9932 }
9933
9934 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9935 bfd_link_info. If H uses the address of a PLT entry as the value
9936 of the symbol, then set the entry in the symbol table now. Prefer
9937 a standard MIPS PLT entry. */
9938
9939 static bool
9940 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9941 {
9942 struct bfd_link_info *info = data;
9943 bool micromips_p = MICROMIPS_P (info->output_bfd);
9944 struct mips_elf_link_hash_table *htab;
9945 unsigned int other;
9946 bfd_vma isa_bit;
9947 bfd_vma val;
9948
9949 htab = mips_elf_hash_table (info);
9950 BFD_ASSERT (htab != NULL);
9951
9952 if (h->use_plt_entry)
9953 {
9954 BFD_ASSERT (h->root.plt.plist != NULL);
9955 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9956 || h->root.plt.plist->comp_offset != MINUS_ONE);
9957
9958 val = htab->plt_header_size;
9959 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9960 {
9961 isa_bit = 0;
9962 val += h->root.plt.plist->mips_offset;
9963 other = 0;
9964 }
9965 else
9966 {
9967 isa_bit = 1;
9968 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9969 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9970 }
9971 val += isa_bit;
9972 /* For VxWorks, point at the PLT load stub rather than the lazy
9973 resolution stub; this stub will become the canonical function
9974 address. */
9975 if (htab->root.target_os == is_vxworks)
9976 val += 8;
9977
9978 h->root.root.u.def.section = htab->root.splt;
9979 h->root.root.u.def.value = val;
9980 h->root.other = other;
9981 }
9982
9983 return true;
9984 }
9985
9986 /* Set the sizes of the dynamic sections. */
9987
9988 bool
9989 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9990 struct bfd_link_info *info)
9991 {
9992 bfd *dynobj;
9993 asection *s, *sreldyn;
9994 bool reltext;
9995 struct mips_elf_link_hash_table *htab;
9996
9997 htab = mips_elf_hash_table (info);
9998 BFD_ASSERT (htab != NULL);
9999 dynobj = elf_hash_table (info)->dynobj;
10000 BFD_ASSERT (dynobj != NULL);
10001
10002 if (elf_hash_table (info)->dynamic_sections_created)
10003 {
10004 /* Set the contents of the .interp section to the interpreter. */
10005 if (bfd_link_executable (info) && !info->nointerp)
10006 {
10007 s = bfd_get_linker_section (dynobj, ".interp");
10008 BFD_ASSERT (s != NULL);
10009 s->size
10010 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
10011 s->contents
10012 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
10013 }
10014
10015 /* Figure out the size of the PLT header if we know that we
10016 are using it. For the sake of cache alignment always use
10017 a standard header whenever any standard entries are present
10018 even if microMIPS entries are present as well. This also
10019 lets the microMIPS header rely on the value of $v0 only set
10020 by microMIPS entries, for a small size reduction.
10021
10022 Set symbol table entry values for symbols that use the
10023 address of their PLT entry now that we can calculate it.
10024
10025 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10026 haven't already in _bfd_elf_create_dynamic_sections. */
10027 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10028 {
10029 bool micromips_p = (MICROMIPS_P (output_bfd)
10030 && !htab->plt_mips_offset);
10031 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10032 bfd_vma isa_bit = micromips_p;
10033 struct elf_link_hash_entry *h;
10034 bfd_vma size;
10035
10036 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10037 BFD_ASSERT (htab->root.sgotplt->size == 0);
10038 BFD_ASSERT (htab->root.splt->size == 0);
10039
10040 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10041 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10042 else if (htab->root.target_os == is_vxworks)
10043 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10044 else if (ABI_64_P (output_bfd))
10045 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10046 else if (ABI_N32_P (output_bfd))
10047 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10048 else if (!micromips_p)
10049 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10050 else if (htab->insn32)
10051 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10052 else
10053 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10054
10055 htab->plt_header_is_comp = micromips_p;
10056 htab->plt_header_size = size;
10057 htab->root.splt->size = (size
10058 + htab->plt_mips_offset
10059 + htab->plt_comp_offset);
10060 htab->root.sgotplt->size = (htab->plt_got_index
10061 * MIPS_ELF_GOT_SIZE (dynobj));
10062
10063 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10064
10065 if (htab->root.hplt == NULL)
10066 {
10067 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10068 "_PROCEDURE_LINKAGE_TABLE_");
10069 htab->root.hplt = h;
10070 if (h == NULL)
10071 return false;
10072 }
10073
10074 h = htab->root.hplt;
10075 h->root.u.def.value = isa_bit;
10076 h->other = other;
10077 h->type = STT_FUNC;
10078 }
10079 }
10080
10081 /* Allocate space for global sym dynamic relocs. */
10082 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10083
10084 mips_elf_estimate_stub_size (output_bfd, info);
10085
10086 if (!mips_elf_lay_out_got (output_bfd, info))
10087 return false;
10088
10089 mips_elf_lay_out_lazy_stubs (info);
10090
10091 /* The check_relocs and adjust_dynamic_symbol entry points have
10092 determined the sizes of the various dynamic sections. Allocate
10093 memory for them. */
10094 reltext = false;
10095 for (s = dynobj->sections; s != NULL; s = s->next)
10096 {
10097 const char *name;
10098
10099 /* It's OK to base decisions on the section name, because none
10100 of the dynobj section names depend upon the input files. */
10101 name = bfd_section_name (s);
10102
10103 if ((s->flags & SEC_LINKER_CREATED) == 0)
10104 continue;
10105
10106 if (startswith (name, ".rel"))
10107 {
10108 if (s->size != 0)
10109 {
10110 const char *outname;
10111 asection *target;
10112
10113 /* If this relocation section applies to a read only
10114 section, then we probably need a DT_TEXTREL entry.
10115 If the relocation section is .rel(a).dyn, we always
10116 assert a DT_TEXTREL entry rather than testing whether
10117 there exists a relocation to a read only section or
10118 not. */
10119 outname = bfd_section_name (s->output_section);
10120 target = bfd_get_section_by_name (output_bfd, outname + 4);
10121 if ((target != NULL
10122 && (target->flags & SEC_READONLY) != 0
10123 && (target->flags & SEC_ALLOC) != 0)
10124 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10125 reltext = true;
10126
10127 /* We use the reloc_count field as a counter if we need
10128 to copy relocs into the output file. */
10129 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10130 s->reloc_count = 0;
10131
10132 /* If combreloc is enabled, elf_link_sort_relocs() will
10133 sort relocations, but in a different way than we do,
10134 and before we're done creating relocations. Also, it
10135 will move them around between input sections'
10136 relocation's contents, so our sorting would be
10137 broken, so don't let it run. */
10138 info->combreloc = 0;
10139 }
10140 }
10141 else if (bfd_link_executable (info)
10142 && ! mips_elf_hash_table (info)->use_rld_obj_head
10143 && startswith (name, ".rld_map"))
10144 {
10145 /* We add a room for __rld_map. It will be filled in by the
10146 rtld to contain a pointer to the _r_debug structure. */
10147 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10148 }
10149 else if (SGI_COMPAT (output_bfd)
10150 && startswith (name, ".compact_rel"))
10151 s->size += mips_elf_hash_table (info)->compact_rel_size;
10152 else if (s == htab->root.splt)
10153 {
10154 /* If the last PLT entry has a branch delay slot, allocate
10155 room for an extra nop to fill the delay slot. This is
10156 for CPUs without load interlocking. */
10157 if (! LOAD_INTERLOCKS_P (output_bfd)
10158 && htab->root.target_os != is_vxworks
10159 && s->size > 0)
10160 s->size += 4;
10161 }
10162 else if (! startswith (name, ".init")
10163 && s != htab->root.sgot
10164 && s != htab->root.sgotplt
10165 && s != htab->sstubs
10166 && s != htab->root.sdynbss
10167 && s != htab->root.sdynrelro)
10168 {
10169 /* It's not one of our sections, so don't allocate space. */
10170 continue;
10171 }
10172
10173 if (s->size == 0)
10174 {
10175 s->flags |= SEC_EXCLUDE;
10176 continue;
10177 }
10178
10179 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10180 continue;
10181
10182 /* Allocate memory for the section contents. */
10183 s->contents = bfd_zalloc (dynobj, s->size);
10184 if (s->contents == NULL)
10185 {
10186 bfd_set_error (bfd_error_no_memory);
10187 return false;
10188 }
10189 }
10190
10191 if (elf_hash_table (info)->dynamic_sections_created)
10192 {
10193 /* Add some entries to the .dynamic section. We fill in the
10194 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10195 must add the entries now so that we get the correct size for
10196 the .dynamic section. */
10197
10198 /* SGI object has the equivalence of DT_DEBUG in the
10199 DT_MIPS_RLD_MAP entry. This must come first because glibc
10200 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10201 may only look at the first one they see. */
10202 if (!bfd_link_pic (info)
10203 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10204 return false;
10205
10206 if (bfd_link_executable (info)
10207 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10208 return false;
10209
10210 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10211 used by the debugger. */
10212 if (bfd_link_executable (info)
10213 && !SGI_COMPAT (output_bfd)
10214 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10215 return false;
10216
10217 if (reltext
10218 && (SGI_COMPAT (output_bfd)
10219 || htab->root.target_os == is_vxworks))
10220 info->flags |= DF_TEXTREL;
10221
10222 if ((info->flags & DF_TEXTREL) != 0)
10223 {
10224 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10225 return false;
10226
10227 /* Clear the DF_TEXTREL flag. It will be set again if we
10228 write out an actual text relocation; we may not, because
10229 at this point we do not know whether e.g. any .eh_frame
10230 absolute relocations have been converted to PC-relative. */
10231 info->flags &= ~DF_TEXTREL;
10232 }
10233
10234 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10235 return false;
10236
10237 sreldyn = mips_elf_rel_dyn_section (info, false);
10238 if (htab->root.target_os == is_vxworks)
10239 {
10240 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10241 use any of the DT_MIPS_* tags. */
10242 if (sreldyn && sreldyn->size > 0)
10243 {
10244 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10245 return false;
10246
10247 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10248 return false;
10249
10250 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10251 return false;
10252 }
10253 }
10254 else
10255 {
10256 if (sreldyn && sreldyn->size > 0
10257 && !bfd_is_abs_section (sreldyn->output_section))
10258 {
10259 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10260 return false;
10261
10262 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10263 return false;
10264
10265 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10266 return false;
10267 }
10268
10269 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10270 return false;
10271
10272 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10273 return false;
10274
10275 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10276 return false;
10277
10278 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10279 return false;
10280
10281 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10282 return false;
10283
10284 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10285 return false;
10286
10287 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10288 return false;
10289
10290 if (info->emit_gnu_hash
10291 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10292 return false;
10293
10294 if (IRIX_COMPAT (dynobj) == ict_irix5
10295 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10296 return false;
10297
10298 if (IRIX_COMPAT (dynobj) == ict_irix6
10299 && (bfd_get_section_by_name
10300 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10301 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10302 return false;
10303 }
10304 if (htab->root.splt->size > 0)
10305 {
10306 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10307 return false;
10308
10309 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10310 return false;
10311
10312 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10313 return false;
10314
10315 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10316 return false;
10317 }
10318 if (htab->root.target_os == is_vxworks
10319 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10320 return false;
10321 }
10322
10323 return true;
10324 }
10325 \f
10326 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10327 Adjust its R_ADDEND field so that it is correct for the output file.
10328 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10329 and sections respectively; both use symbol indexes. */
10330
10331 static void
10332 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10333 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10334 asection **local_sections, Elf_Internal_Rela *rel)
10335 {
10336 unsigned int r_type, r_symndx;
10337 Elf_Internal_Sym *sym;
10338 asection *sec;
10339
10340 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10341 {
10342 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10343 if (gprel16_reloc_p (r_type)
10344 || r_type == R_MIPS_GPREL32
10345 || literal_reloc_p (r_type))
10346 {
10347 rel->r_addend += _bfd_get_gp_value (input_bfd);
10348 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10349 }
10350
10351 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10352 sym = local_syms + r_symndx;
10353
10354 /* Adjust REL's addend to account for section merging. */
10355 if (!bfd_link_relocatable (info))
10356 {
10357 sec = local_sections[r_symndx];
10358 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10359 }
10360
10361 /* This would normally be done by the rela_normal code in elflink.c. */
10362 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10363 rel->r_addend += local_sections[r_symndx]->output_offset;
10364 }
10365 }
10366
10367 /* Handle relocations against symbols from removed linkonce sections,
10368 or sections discarded by a linker script. We use this wrapper around
10369 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10370 on 64-bit ELF targets. In this case for any relocation handled, which
10371 always be the first in a triplet, the remaining two have to be processed
10372 together with the first, even if they are R_MIPS_NONE. It is the symbol
10373 index referred by the first reloc that applies to all the three and the
10374 remaining two never refer to an object symbol. And it is the final
10375 relocation (the last non-null one) that determines the output field of
10376 the whole relocation so retrieve the corresponding howto structure for
10377 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10378
10379 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10380 and therefore requires to be pasted in a loop. It also defines a block
10381 and does not protect any of its arguments, hence the extra brackets. */
10382
10383 static void
10384 mips_reloc_against_discarded_section (bfd *output_bfd,
10385 struct bfd_link_info *info,
10386 bfd *input_bfd, asection *input_section,
10387 Elf_Internal_Rela **rel,
10388 const Elf_Internal_Rela **relend,
10389 bool rel_reloc,
10390 reloc_howto_type *howto,
10391 bfd_byte *contents)
10392 {
10393 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10394 int count = bed->s->int_rels_per_ext_rel;
10395 unsigned int r_type;
10396 int i;
10397
10398 for (i = count - 1; i > 0; i--)
10399 {
10400 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10401 if (r_type != R_MIPS_NONE)
10402 {
10403 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10404 break;
10405 }
10406 }
10407 do
10408 {
10409 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10410 (*rel), count, (*relend),
10411 howto, i, contents);
10412 }
10413 while (0);
10414 }
10415
10416 /* Relocate a MIPS ELF section. */
10417
10418 int
10419 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10420 bfd *input_bfd, asection *input_section,
10421 bfd_byte *contents, Elf_Internal_Rela *relocs,
10422 Elf_Internal_Sym *local_syms,
10423 asection **local_sections)
10424 {
10425 Elf_Internal_Rela *rel;
10426 const Elf_Internal_Rela *relend;
10427 bfd_vma addend = 0;
10428 bool use_saved_addend_p = false;
10429
10430 relend = relocs + input_section->reloc_count;
10431 for (rel = relocs; rel < relend; ++rel)
10432 {
10433 const char *name;
10434 bfd_vma value = 0;
10435 reloc_howto_type *howto;
10436 bool cross_mode_jump_p = false;
10437 /* TRUE if the relocation is a RELA relocation, rather than a
10438 REL relocation. */
10439 bool rela_relocation_p = true;
10440 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10441 const char *msg;
10442 unsigned long r_symndx;
10443 asection *sec;
10444 Elf_Internal_Shdr *symtab_hdr;
10445 struct elf_link_hash_entry *h;
10446 bool rel_reloc;
10447
10448 rel_reloc = (NEWABI_P (input_bfd)
10449 && mips_elf_rel_relocation_p (input_bfd, input_section,
10450 relocs, rel));
10451 /* Find the relocation howto for this relocation. */
10452 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10453
10454 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10455 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10456 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10457 {
10458 sec = local_sections[r_symndx];
10459 h = NULL;
10460 }
10461 else
10462 {
10463 unsigned long extsymoff;
10464
10465 extsymoff = 0;
10466 if (!elf_bad_symtab (input_bfd))
10467 extsymoff = symtab_hdr->sh_info;
10468 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10469 while (h->root.type == bfd_link_hash_indirect
10470 || h->root.type == bfd_link_hash_warning)
10471 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10472
10473 sec = NULL;
10474 if (h->root.type == bfd_link_hash_defined
10475 || h->root.type == bfd_link_hash_defweak)
10476 sec = h->root.u.def.section;
10477 }
10478
10479 if (sec != NULL && discarded_section (sec))
10480 {
10481 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10482 input_section, &rel, &relend,
10483 rel_reloc, howto, contents);
10484 continue;
10485 }
10486
10487 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10488 {
10489 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10490 64-bit code, but make sure all their addresses are in the
10491 lowermost or uppermost 32-bit section of the 64-bit address
10492 space. Thus, when they use an R_MIPS_64 they mean what is
10493 usually meant by R_MIPS_32, with the exception that the
10494 stored value is sign-extended to 64 bits. */
10495 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10496
10497 /* On big-endian systems, we need to lie about the position
10498 of the reloc. */
10499 if (bfd_big_endian (input_bfd))
10500 rel->r_offset += 4;
10501 }
10502
10503 if (!use_saved_addend_p)
10504 {
10505 /* If these relocations were originally of the REL variety,
10506 we must pull the addend out of the field that will be
10507 relocated. Otherwise, we simply use the contents of the
10508 RELA relocation. */
10509 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10510 relocs, rel))
10511 {
10512 rela_relocation_p = false;
10513 addend = mips_elf_read_rel_addend (input_bfd, input_section,
10514 rel, howto, contents);
10515 if (hi16_reloc_p (r_type)
10516 || (got16_reloc_p (r_type)
10517 && mips_elf_local_relocation_p (input_bfd, rel,
10518 local_sections)))
10519 {
10520 if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10521 rel, relend,
10522 contents, &addend))
10523 {
10524 if (h)
10525 name = h->root.root.string;
10526 else
10527 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10528 local_syms + r_symndx,
10529 sec);
10530 _bfd_error_handler
10531 /* xgettext:c-format */
10532 (_("%pB: can't find matching LO16 reloc against `%s'"
10533 " for %s at %#" PRIx64 " in section `%pA'"),
10534 input_bfd, name,
10535 howto->name, (uint64_t) rel->r_offset, input_section);
10536 }
10537 }
10538 else
10539 addend <<= howto->rightshift;
10540 }
10541 else
10542 addend = rel->r_addend;
10543 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10544 local_syms, local_sections, rel);
10545 }
10546
10547 if (bfd_link_relocatable (info))
10548 {
10549 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10550 && bfd_big_endian (input_bfd))
10551 rel->r_offset -= 4;
10552
10553 if (!rela_relocation_p && rel->r_addend)
10554 {
10555 addend += rel->r_addend;
10556 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10557 addend = mips_elf_high (addend);
10558 else if (r_type == R_MIPS_HIGHER)
10559 addend = mips_elf_higher (addend);
10560 else if (r_type == R_MIPS_HIGHEST)
10561 addend = mips_elf_highest (addend);
10562 else
10563 addend >>= howto->rightshift;
10564
10565 /* We use the source mask, rather than the destination
10566 mask because the place to which we are writing will be
10567 source of the addend in the final link. */
10568 addend &= howto->src_mask;
10569
10570 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10571 /* See the comment above about using R_MIPS_64 in the 32-bit
10572 ABI. Here, we need to update the addend. It would be
10573 possible to get away with just using the R_MIPS_32 reloc
10574 but for endianness. */
10575 {
10576 bfd_vma sign_bits;
10577 bfd_vma low_bits;
10578 bfd_vma high_bits;
10579
10580 if (addend & ((bfd_vma) 1 << 31))
10581 #ifdef BFD64
10582 sign_bits = ((bfd_vma) 1 << 32) - 1;
10583 #else
10584 sign_bits = -1;
10585 #endif
10586 else
10587 sign_bits = 0;
10588
10589 /* If we don't know that we have a 64-bit type,
10590 do two separate stores. */
10591 if (bfd_big_endian (input_bfd))
10592 {
10593 /* Store the sign-bits (which are most significant)
10594 first. */
10595 low_bits = sign_bits;
10596 high_bits = addend;
10597 }
10598 else
10599 {
10600 low_bits = addend;
10601 high_bits = sign_bits;
10602 }
10603 bfd_put_32 (input_bfd, low_bits,
10604 contents + rel->r_offset);
10605 bfd_put_32 (input_bfd, high_bits,
10606 contents + rel->r_offset + 4);
10607 continue;
10608 }
10609
10610 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10611 input_bfd, input_section,
10612 contents, false))
10613 return false;
10614 }
10615
10616 /* Go on to the next relocation. */
10617 continue;
10618 }
10619
10620 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10621 relocations for the same offset. In that case we are
10622 supposed to treat the output of each relocation as the addend
10623 for the next. */
10624 if (rel + 1 < relend
10625 && rel->r_offset == rel[1].r_offset
10626 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10627 use_saved_addend_p = true;
10628 else
10629 use_saved_addend_p = false;
10630
10631 /* Figure out what value we are supposed to relocate. */
10632 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10633 input_section, contents,
10634 info, rel, addend, howto,
10635 local_syms, local_sections,
10636 &value, &name, &cross_mode_jump_p,
10637 use_saved_addend_p))
10638 {
10639 case bfd_reloc_continue:
10640 /* There's nothing to do. */
10641 continue;
10642
10643 case bfd_reloc_undefined:
10644 /* mips_elf_calculate_relocation already called the
10645 undefined_symbol callback. There's no real point in
10646 trying to perform the relocation at this point, so we
10647 just skip ahead to the next relocation. */
10648 continue;
10649
10650 case bfd_reloc_notsupported:
10651 msg = _("internal error: unsupported relocation error");
10652 info->callbacks->warning
10653 (info, msg, name, input_bfd, input_section, rel->r_offset);
10654 return false;
10655
10656 case bfd_reloc_overflow:
10657 if (use_saved_addend_p)
10658 /* Ignore overflow until we reach the last relocation for
10659 a given location. */
10660 ;
10661 else
10662 {
10663 struct mips_elf_link_hash_table *htab;
10664
10665 htab = mips_elf_hash_table (info);
10666 BFD_ASSERT (htab != NULL);
10667 BFD_ASSERT (name != NULL);
10668 if (!htab->small_data_overflow_reported
10669 && (gprel16_reloc_p (howto->type)
10670 || literal_reloc_p (howto->type)))
10671 {
10672 msg = _("small-data section exceeds 64KB;"
10673 " lower small-data size limit (see option -G)");
10674
10675 htab->small_data_overflow_reported = true;
10676 (*info->callbacks->einfo) ("%P: %s\n", msg);
10677 }
10678 (*info->callbacks->reloc_overflow)
10679 (info, NULL, name, howto->name, (bfd_vma) 0,
10680 input_bfd, input_section, rel->r_offset);
10681 }
10682 break;
10683
10684 case bfd_reloc_ok:
10685 break;
10686
10687 case bfd_reloc_outofrange:
10688 msg = NULL;
10689 if (jal_reloc_p (howto->type))
10690 msg = (cross_mode_jump_p
10691 ? _("cannot convert a jump to JALX "
10692 "for a non-word-aligned address")
10693 : (howto->type == R_MIPS16_26
10694 ? _("jump to a non-word-aligned address")
10695 : _("jump to a non-instruction-aligned address")));
10696 else if (b_reloc_p (howto->type))
10697 msg = (cross_mode_jump_p
10698 ? _("cannot convert a branch to JALX "
10699 "for a non-word-aligned address")
10700 : _("branch to a non-instruction-aligned address"));
10701 else if (aligned_pcrel_reloc_p (howto->type))
10702 msg = _("PC-relative load from unaligned address");
10703 if (msg)
10704 {
10705 info->callbacks->einfo
10706 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10707 break;
10708 }
10709 /* Fall through. */
10710
10711 default:
10712 abort ();
10713 break;
10714 }
10715
10716 /* If we've got another relocation for the address, keep going
10717 until we reach the last one. */
10718 if (use_saved_addend_p)
10719 {
10720 addend = value;
10721 continue;
10722 }
10723
10724 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10725 /* See the comment above about using R_MIPS_64 in the 32-bit
10726 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10727 that calculated the right value. Now, however, we
10728 sign-extend the 32-bit result to 64-bits, and store it as a
10729 64-bit value. We are especially generous here in that we
10730 go to extreme lengths to support this usage on systems with
10731 only a 32-bit VMA. */
10732 {
10733 bfd_vma sign_bits;
10734 bfd_vma low_bits;
10735 bfd_vma high_bits;
10736
10737 if (value & ((bfd_vma) 1 << 31))
10738 #ifdef BFD64
10739 sign_bits = ((bfd_vma) 1 << 32) - 1;
10740 #else
10741 sign_bits = -1;
10742 #endif
10743 else
10744 sign_bits = 0;
10745
10746 /* If we don't know that we have a 64-bit type,
10747 do two separate stores. */
10748 if (bfd_big_endian (input_bfd))
10749 {
10750 /* Undo what we did above. */
10751 rel->r_offset -= 4;
10752 /* Store the sign-bits (which are most significant)
10753 first. */
10754 low_bits = sign_bits;
10755 high_bits = value;
10756 }
10757 else
10758 {
10759 low_bits = value;
10760 high_bits = sign_bits;
10761 }
10762 bfd_put_32 (input_bfd, low_bits,
10763 contents + rel->r_offset);
10764 bfd_put_32 (input_bfd, high_bits,
10765 contents + rel->r_offset + 4);
10766 continue;
10767 }
10768
10769 /* Actually perform the relocation. */
10770 if (! mips_elf_perform_relocation (info, howto, rel, value,
10771 input_bfd, input_section,
10772 contents, cross_mode_jump_p))
10773 return false;
10774 }
10775
10776 return true;
10777 }
10778 \f
10779 /* A function that iterates over each entry in la25_stubs and fills
10780 in the code for each one. DATA points to a mips_htab_traverse_info. */
10781
10782 static int
10783 mips_elf_create_la25_stub (void **slot, void *data)
10784 {
10785 struct mips_htab_traverse_info *hti;
10786 struct mips_elf_link_hash_table *htab;
10787 struct mips_elf_la25_stub *stub;
10788 asection *s;
10789 bfd_byte *loc;
10790 bfd_vma offset, target, target_high, target_low;
10791 bfd_vma branch_pc;
10792 bfd_signed_vma pcrel_offset = 0;
10793
10794 stub = (struct mips_elf_la25_stub *) *slot;
10795 hti = (struct mips_htab_traverse_info *) data;
10796 htab = mips_elf_hash_table (hti->info);
10797 BFD_ASSERT (htab != NULL);
10798
10799 /* Create the section contents, if we haven't already. */
10800 s = stub->stub_section;
10801 loc = s->contents;
10802 if (loc == NULL)
10803 {
10804 loc = bfd_malloc (s->size);
10805 if (loc == NULL)
10806 {
10807 hti->error = true;
10808 return false;
10809 }
10810 s->contents = loc;
10811 }
10812
10813 /* Work out where in the section this stub should go. */
10814 offset = stub->offset;
10815
10816 /* We add 8 here to account for the LUI/ADDIU instructions
10817 before the branch instruction. This cannot be moved down to
10818 where pcrel_offset is calculated as 's' is updated in
10819 mips_elf_get_la25_target. */
10820 branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10821
10822 /* Work out the target address. */
10823 target = mips_elf_get_la25_target (stub, &s);
10824 target += s->output_section->vma + s->output_offset;
10825
10826 target_high = ((target + 0x8000) >> 16) & 0xffff;
10827 target_low = (target & 0xffff);
10828
10829 /* Calculate the PC of the compact branch instruction (for the case where
10830 compact branches are used for either microMIPSR6 or MIPSR6 with
10831 compact branches. Add 4-bytes to account for BC using the PC of the
10832 next instruction as the base. */
10833 pcrel_offset = target - (branch_pc + 4);
10834
10835 if (stub->stub_section != htab->strampoline)
10836 {
10837 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10838 of the section and write the two instructions at the end. */
10839 memset (loc, 0, offset);
10840 loc += offset;
10841 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10842 {
10843 bfd_put_micromips_32 (hti->output_bfd,
10844 LA25_LUI_MICROMIPS (target_high),
10845 loc);
10846 bfd_put_micromips_32 (hti->output_bfd,
10847 LA25_ADDIU_MICROMIPS (target_low),
10848 loc + 4);
10849 }
10850 else
10851 {
10852 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10853 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10854 }
10855 }
10856 else
10857 {
10858 /* This is trampoline. */
10859 loc += offset;
10860 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10861 {
10862 bfd_put_micromips_32 (hti->output_bfd,
10863 LA25_LUI_MICROMIPS (target_high), loc);
10864 bfd_put_micromips_32 (hti->output_bfd,
10865 LA25_J_MICROMIPS (target), loc + 4);
10866 bfd_put_micromips_32 (hti->output_bfd,
10867 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10868 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10869 }
10870 else
10871 {
10872 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10873 if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10874 {
10875 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10876 bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10877 }
10878 else
10879 {
10880 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10881 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10882 }
10883 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10884 }
10885 }
10886 return true;
10887 }
10888
10889 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10890 adjust it appropriately now. */
10891
10892 static void
10893 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10894 const char *name, Elf_Internal_Sym *sym)
10895 {
10896 /* The linker script takes care of providing names and values for
10897 these, but we must place them into the right sections. */
10898 static const char* const text_section_symbols[] = {
10899 "_ftext",
10900 "_etext",
10901 "__dso_displacement",
10902 "__elf_header",
10903 "__program_header_table",
10904 NULL
10905 };
10906
10907 static const char* const data_section_symbols[] = {
10908 "_fdata",
10909 "_edata",
10910 "_end",
10911 "_fbss",
10912 NULL
10913 };
10914
10915 const char* const *p;
10916 int i;
10917
10918 for (i = 0; i < 2; ++i)
10919 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10920 *p;
10921 ++p)
10922 if (strcmp (*p, name) == 0)
10923 {
10924 /* All of these symbols are given type STT_SECTION by the
10925 IRIX6 linker. */
10926 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10927 sym->st_other = STO_PROTECTED;
10928
10929 /* The IRIX linker puts these symbols in special sections. */
10930 if (i == 0)
10931 sym->st_shndx = SHN_MIPS_TEXT;
10932 else
10933 sym->st_shndx = SHN_MIPS_DATA;
10934
10935 break;
10936 }
10937 }
10938
10939 /* Finish up dynamic symbol handling. We set the contents of various
10940 dynamic sections here. */
10941
10942 bool
10943 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10944 struct bfd_link_info *info,
10945 struct elf_link_hash_entry *h,
10946 Elf_Internal_Sym *sym)
10947 {
10948 bfd *dynobj;
10949 asection *sgot;
10950 struct mips_got_info *g, *gg;
10951 const char *name;
10952 int idx;
10953 struct mips_elf_link_hash_table *htab;
10954 struct mips_elf_link_hash_entry *hmips;
10955
10956 htab = mips_elf_hash_table (info);
10957 BFD_ASSERT (htab != NULL);
10958 dynobj = elf_hash_table (info)->dynobj;
10959 hmips = (struct mips_elf_link_hash_entry *) h;
10960
10961 BFD_ASSERT (htab->root.target_os != is_vxworks);
10962
10963 if (h->plt.plist != NULL
10964 && (h->plt.plist->mips_offset != MINUS_ONE
10965 || h->plt.plist->comp_offset != MINUS_ONE))
10966 {
10967 /* We've decided to create a PLT entry for this symbol. */
10968 bfd_byte *loc;
10969 bfd_vma header_address, got_address;
10970 bfd_vma got_address_high, got_address_low, load;
10971 bfd_vma got_index;
10972 bfd_vma isa_bit;
10973
10974 got_index = h->plt.plist->gotplt_index;
10975
10976 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10977 BFD_ASSERT (h->dynindx != -1);
10978 BFD_ASSERT (htab->root.splt != NULL);
10979 BFD_ASSERT (got_index != MINUS_ONE);
10980 BFD_ASSERT (!h->def_regular);
10981
10982 /* Calculate the address of the PLT header. */
10983 isa_bit = htab->plt_header_is_comp;
10984 header_address = (htab->root.splt->output_section->vma
10985 + htab->root.splt->output_offset + isa_bit);
10986
10987 /* Calculate the address of the .got.plt entry. */
10988 got_address = (htab->root.sgotplt->output_section->vma
10989 + htab->root.sgotplt->output_offset
10990 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10991
10992 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10993 got_address_low = got_address & 0xffff;
10994
10995 /* The PLT sequence is not safe for N64 if .got.plt entry's address
10996 cannot be loaded in two instructions. */
10997 if (ABI_64_P (output_bfd)
10998 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
10999 {
11000 _bfd_error_handler
11001 /* xgettext:c-format */
11002 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
11003 "supported; consider using `-Ttext-segment=...'"),
11004 output_bfd,
11005 htab->root.sgotplt->output_section,
11006 (int64_t) got_address);
11007 bfd_set_error (bfd_error_no_error);
11008 return false;
11009 }
11010
11011 /* Initially point the .got.plt entry at the PLT header. */
11012 loc = (htab->root.sgotplt->contents
11013 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
11014 if (ABI_64_P (output_bfd))
11015 bfd_put_64 (output_bfd, header_address, loc);
11016 else
11017 bfd_put_32 (output_bfd, header_address, loc);
11018
11019 /* Now handle the PLT itself. First the standard entry (the order
11020 does not matter, we just have to pick one). */
11021 if (h->plt.plist->mips_offset != MINUS_ONE)
11022 {
11023 const bfd_vma *plt_entry;
11024 bfd_vma plt_offset;
11025
11026 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11027
11028 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11029
11030 /* Find out where the .plt entry should go. */
11031 loc = htab->root.splt->contents + plt_offset;
11032
11033 /* Pick the load opcode. */
11034 load = MIPS_ELF_LOAD_WORD (output_bfd);
11035
11036 /* Fill in the PLT entry itself. */
11037
11038 if (MIPSR6_P (output_bfd))
11039 plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11040 : mipsr6_exec_plt_entry;
11041 else
11042 plt_entry = mips_exec_plt_entry;
11043 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11044 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11045 loc + 4);
11046
11047 if (! LOAD_INTERLOCKS_P (output_bfd)
11048 || (MIPSR6_P (output_bfd) && htab->compact_branches))
11049 {
11050 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11051 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11052 }
11053 else
11054 {
11055 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11056 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11057 loc + 12);
11058 }
11059 }
11060
11061 /* Now the compressed entry. They come after any standard ones. */
11062 if (h->plt.plist->comp_offset != MINUS_ONE)
11063 {
11064 bfd_vma plt_offset;
11065
11066 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11067 + h->plt.plist->comp_offset);
11068
11069 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11070
11071 /* Find out where the .plt entry should go. */
11072 loc = htab->root.splt->contents + plt_offset;
11073
11074 /* Fill in the PLT entry itself. */
11075 if (!MICROMIPS_P (output_bfd))
11076 {
11077 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11078
11079 bfd_put_16 (output_bfd, plt_entry[0], loc);
11080 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11081 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11082 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11083 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11084 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11085 bfd_put_32 (output_bfd, got_address, loc + 12);
11086 }
11087 else if (htab->insn32)
11088 {
11089 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11090
11091 bfd_put_16 (output_bfd, plt_entry[0], loc);
11092 bfd_put_16 (output_bfd, got_address_high, loc + 2);
11093 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11094 bfd_put_16 (output_bfd, got_address_low, loc + 6);
11095 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11096 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11097 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11098 bfd_put_16 (output_bfd, got_address_low, loc + 14);
11099 }
11100 else
11101 {
11102 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11103 bfd_signed_vma gotpc_offset;
11104 bfd_vma loc_address;
11105
11106 BFD_ASSERT (got_address % 4 == 0);
11107
11108 loc_address = (htab->root.splt->output_section->vma
11109 + htab->root.splt->output_offset + plt_offset);
11110 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11111
11112 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11113 if (gotpc_offset + 0x1000000 >= 0x2000000)
11114 {
11115 _bfd_error_handler
11116 /* xgettext:c-format */
11117 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11118 "beyond the range of ADDIUPC"),
11119 output_bfd,
11120 htab->root.sgotplt->output_section,
11121 (int64_t) gotpc_offset,
11122 htab->root.splt->output_section);
11123 bfd_set_error (bfd_error_no_error);
11124 return false;
11125 }
11126 bfd_put_16 (output_bfd,
11127 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11128 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11129 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11130 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11131 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11132 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11133 }
11134 }
11135
11136 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11137 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11138 got_index - 2, h->dynindx,
11139 R_MIPS_JUMP_SLOT, got_address);
11140
11141 /* We distinguish between PLT entries and lazy-binding stubs by
11142 giving the former an st_other value of STO_MIPS_PLT. Set the
11143 flag and leave the value if there are any relocations in the
11144 binary where pointer equality matters. */
11145 sym->st_shndx = SHN_UNDEF;
11146 if (h->pointer_equality_needed)
11147 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11148 else
11149 {
11150 sym->st_value = 0;
11151 sym->st_other = 0;
11152 }
11153 }
11154
11155 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11156 {
11157 /* We've decided to create a lazy-binding stub. */
11158 bool micromips_p = MICROMIPS_P (output_bfd);
11159 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11160 bfd_vma stub_size = htab->function_stub_size;
11161 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11162 bfd_vma isa_bit = micromips_p;
11163 bfd_vma stub_big_size;
11164
11165 if (!micromips_p)
11166 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11167 else if (htab->insn32)
11168 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11169 else
11170 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11171
11172 /* This symbol has a stub. Set it up. */
11173
11174 BFD_ASSERT (h->dynindx != -1);
11175
11176 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11177
11178 /* Values up to 2^31 - 1 are allowed. Larger values would cause
11179 sign extension at runtime in the stub, resulting in a negative
11180 index value. */
11181 if (h->dynindx & ~0x7fffffff)
11182 return false;
11183
11184 /* Fill the stub. */
11185 if (micromips_p)
11186 {
11187 idx = 0;
11188 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11189 stub + idx);
11190 idx += 4;
11191 if (htab->insn32)
11192 {
11193 bfd_put_micromips_32 (output_bfd,
11194 STUB_MOVE32_MICROMIPS, stub + idx);
11195 idx += 4;
11196 }
11197 else
11198 {
11199 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11200 idx += 2;
11201 }
11202 if (stub_size == stub_big_size)
11203 {
11204 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11205
11206 bfd_put_micromips_32 (output_bfd,
11207 STUB_LUI_MICROMIPS (dynindx_hi),
11208 stub + idx);
11209 idx += 4;
11210 }
11211 if (htab->insn32)
11212 {
11213 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11214 stub + idx);
11215 idx += 4;
11216 }
11217 else
11218 {
11219 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11220 idx += 2;
11221 }
11222
11223 /* If a large stub is not required and sign extension is not a
11224 problem, then use legacy code in the stub. */
11225 if (stub_size == stub_big_size)
11226 bfd_put_micromips_32 (output_bfd,
11227 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11228 stub + idx);
11229 else if (h->dynindx & ~0x7fff)
11230 bfd_put_micromips_32 (output_bfd,
11231 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11232 stub + idx);
11233 else
11234 bfd_put_micromips_32 (output_bfd,
11235 STUB_LI16S_MICROMIPS (output_bfd,
11236 h->dynindx),
11237 stub + idx);
11238 }
11239 else
11240 {
11241 idx = 0;
11242 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11243 idx += 4;
11244 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11245 idx += 4;
11246 if (stub_size == stub_big_size)
11247 {
11248 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11249 stub + idx);
11250 idx += 4;
11251 }
11252
11253 if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11254 {
11255 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11256 idx += 4;
11257 }
11258
11259 /* If a large stub is not required and sign extension is not a
11260 problem, then use legacy code in the stub. */
11261 if (stub_size == stub_big_size)
11262 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11263 stub + idx);
11264 else if (h->dynindx & ~0x7fff)
11265 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11266 stub + idx);
11267 else
11268 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11269 stub + idx);
11270 idx += 4;
11271
11272 if (MIPSR6_P (output_bfd) && htab->compact_branches)
11273 bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11274 }
11275
11276 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11277 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11278 stub, stub_size);
11279
11280 /* Mark the symbol as undefined. stub_offset != -1 occurs
11281 only for the referenced symbol. */
11282 sym->st_shndx = SHN_UNDEF;
11283
11284 /* The run-time linker uses the st_value field of the symbol
11285 to reset the global offset table entry for this external
11286 to its stub address when unlinking a shared object. */
11287 sym->st_value = (htab->sstubs->output_section->vma
11288 + htab->sstubs->output_offset
11289 + h->plt.plist->stub_offset
11290 + isa_bit);
11291 sym->st_other = other;
11292 }
11293
11294 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11295 refer to the stub, since only the stub uses the standard calling
11296 conventions. */
11297 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11298 {
11299 BFD_ASSERT (hmips->need_fn_stub);
11300 sym->st_value = (hmips->fn_stub->output_section->vma
11301 + hmips->fn_stub->output_offset);
11302 sym->st_size = hmips->fn_stub->size;
11303 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11304 }
11305
11306 BFD_ASSERT (h->dynindx != -1
11307 || h->forced_local);
11308
11309 sgot = htab->root.sgot;
11310 g = htab->got_info;
11311 BFD_ASSERT (g != NULL);
11312
11313 /* Run through the global symbol table, creating GOT entries for all
11314 the symbols that need them. */
11315 if (hmips->global_got_area != GGA_NONE)
11316 {
11317 bfd_vma offset;
11318 bfd_vma value;
11319
11320 value = sym->st_value;
11321 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11322 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11323 }
11324
11325 if (hmips->global_got_area != GGA_NONE && g->next)
11326 {
11327 struct mips_got_entry e, *p;
11328 bfd_vma entry;
11329 bfd_vma offset;
11330
11331 gg = g;
11332
11333 e.abfd = output_bfd;
11334 e.symndx = -1;
11335 e.d.h = hmips;
11336 e.tls_type = GOT_TLS_NONE;
11337
11338 for (g = g->next; g->next != gg; g = g->next)
11339 {
11340 if (g->got_entries
11341 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11342 &e)))
11343 {
11344 offset = p->gotidx;
11345 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11346 if (bfd_link_pic (info)
11347 || (elf_hash_table (info)->dynamic_sections_created
11348 && p->d.h != NULL
11349 && p->d.h->root.def_dynamic
11350 && !p->d.h->root.def_regular))
11351 {
11352 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11353 the various compatibility problems, it's easier to mock
11354 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11355 mips_elf_create_dynamic_relocation to calculate the
11356 appropriate addend. */
11357 Elf_Internal_Rela rel[3];
11358
11359 memset (rel, 0, sizeof (rel));
11360 if (ABI_64_P (output_bfd))
11361 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11362 else
11363 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11364 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11365
11366 entry = 0;
11367 if (! (mips_elf_create_dynamic_relocation
11368 (output_bfd, info, rel,
11369 e.d.h, NULL, sym->st_value, &entry, sgot)))
11370 return false;
11371 }
11372 else
11373 entry = sym->st_value;
11374 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11375 }
11376 }
11377 }
11378
11379 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11380 name = h->root.root.string;
11381 if (h == elf_hash_table (info)->hdynamic
11382 || h == elf_hash_table (info)->hgot)
11383 sym->st_shndx = SHN_ABS;
11384 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11385 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11386 {
11387 sym->st_shndx = SHN_ABS;
11388 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11389 sym->st_value = 1;
11390 }
11391 else if (SGI_COMPAT (output_bfd))
11392 {
11393 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11394 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11395 {
11396 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11397 sym->st_other = STO_PROTECTED;
11398 sym->st_value = 0;
11399 sym->st_shndx = SHN_MIPS_DATA;
11400 }
11401 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11402 {
11403 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11404 sym->st_other = STO_PROTECTED;
11405 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11406 sym->st_shndx = SHN_ABS;
11407 }
11408 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11409 {
11410 if (h->type == STT_FUNC)
11411 sym->st_shndx = SHN_MIPS_TEXT;
11412 else if (h->type == STT_OBJECT)
11413 sym->st_shndx = SHN_MIPS_DATA;
11414 }
11415 }
11416
11417 /* Emit a copy reloc, if needed. */
11418 if (h->needs_copy)
11419 {
11420 asection *s;
11421 bfd_vma symval;
11422
11423 BFD_ASSERT (h->dynindx != -1);
11424 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11425
11426 s = mips_elf_rel_dyn_section (info, false);
11427 symval = (h->root.u.def.section->output_section->vma
11428 + h->root.u.def.section->output_offset
11429 + h->root.u.def.value);
11430 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11431 h->dynindx, R_MIPS_COPY, symval);
11432 }
11433
11434 /* Handle the IRIX6-specific symbols. */
11435 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11436 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11437
11438 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11439 to treat compressed symbols like any other. */
11440 if (ELF_ST_IS_MIPS16 (sym->st_other))
11441 {
11442 BFD_ASSERT (sym->st_value & 1);
11443 sym->st_other -= STO_MIPS16;
11444 }
11445 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11446 {
11447 BFD_ASSERT (sym->st_value & 1);
11448 sym->st_other -= STO_MICROMIPS;
11449 }
11450
11451 return true;
11452 }
11453
11454 /* Likewise, for VxWorks. */
11455
11456 bool
11457 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11458 struct bfd_link_info *info,
11459 struct elf_link_hash_entry *h,
11460 Elf_Internal_Sym *sym)
11461 {
11462 bfd *dynobj;
11463 asection *sgot;
11464 struct mips_got_info *g;
11465 struct mips_elf_link_hash_table *htab;
11466 struct mips_elf_link_hash_entry *hmips;
11467
11468 htab = mips_elf_hash_table (info);
11469 BFD_ASSERT (htab != NULL);
11470 dynobj = elf_hash_table (info)->dynobj;
11471 hmips = (struct mips_elf_link_hash_entry *) h;
11472
11473 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11474 {
11475 bfd_byte *loc;
11476 bfd_vma plt_address, got_address, got_offset, branch_offset;
11477 Elf_Internal_Rela rel;
11478 static const bfd_vma *plt_entry;
11479 bfd_vma gotplt_index;
11480 bfd_vma plt_offset;
11481
11482 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11483 gotplt_index = h->plt.plist->gotplt_index;
11484
11485 BFD_ASSERT (h->dynindx != -1);
11486 BFD_ASSERT (htab->root.splt != NULL);
11487 BFD_ASSERT (gotplt_index != MINUS_ONE);
11488 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11489
11490 /* Calculate the address of the .plt entry. */
11491 plt_address = (htab->root.splt->output_section->vma
11492 + htab->root.splt->output_offset
11493 + plt_offset);
11494
11495 /* Calculate the address of the .got.plt entry. */
11496 got_address = (htab->root.sgotplt->output_section->vma
11497 + htab->root.sgotplt->output_offset
11498 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11499
11500 /* Calculate the offset of the .got.plt entry from
11501 _GLOBAL_OFFSET_TABLE_. */
11502 got_offset = mips_elf_gotplt_index (info, h);
11503
11504 /* Calculate the offset for the branch at the start of the PLT
11505 entry. The branch jumps to the beginning of .plt. */
11506 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11507
11508 /* Fill in the initial value of the .got.plt entry. */
11509 bfd_put_32 (output_bfd, plt_address,
11510 (htab->root.sgotplt->contents
11511 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11512
11513 /* Find out where the .plt entry should go. */
11514 loc = htab->root.splt->contents + plt_offset;
11515
11516 if (bfd_link_pic (info))
11517 {
11518 plt_entry = mips_vxworks_shared_plt_entry;
11519 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11520 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11521 }
11522 else
11523 {
11524 bfd_vma got_address_high, got_address_low;
11525
11526 plt_entry = mips_vxworks_exec_plt_entry;
11527 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11528 got_address_low = got_address & 0xffff;
11529
11530 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11531 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11532 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11533 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11534 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11535 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11536 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11537 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11538
11539 loc = (htab->srelplt2->contents
11540 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11541
11542 /* Emit a relocation for the .got.plt entry. */
11543 rel.r_offset = got_address;
11544 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11545 rel.r_addend = plt_offset;
11546 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11547
11548 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11549 loc += sizeof (Elf32_External_Rela);
11550 rel.r_offset = plt_address + 8;
11551 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11552 rel.r_addend = got_offset;
11553 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11554
11555 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11556 loc += sizeof (Elf32_External_Rela);
11557 rel.r_offset += 4;
11558 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11559 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11560 }
11561
11562 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11563 loc = (htab->root.srelplt->contents
11564 + gotplt_index * sizeof (Elf32_External_Rela));
11565 rel.r_offset = got_address;
11566 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11567 rel.r_addend = 0;
11568 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11569
11570 if (!h->def_regular)
11571 sym->st_shndx = SHN_UNDEF;
11572 }
11573
11574 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11575
11576 sgot = htab->root.sgot;
11577 g = htab->got_info;
11578 BFD_ASSERT (g != NULL);
11579
11580 /* See if this symbol has an entry in the GOT. */
11581 if (hmips->global_got_area != GGA_NONE)
11582 {
11583 bfd_vma offset;
11584 Elf_Internal_Rela outrel;
11585 bfd_byte *loc;
11586 asection *s;
11587
11588 /* Install the symbol value in the GOT. */
11589 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11590 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11591
11592 /* Add a dynamic relocation for it. */
11593 s = mips_elf_rel_dyn_section (info, false);
11594 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11595 outrel.r_offset = (sgot->output_section->vma
11596 + sgot->output_offset
11597 + offset);
11598 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11599 outrel.r_addend = 0;
11600 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11601 }
11602
11603 /* Emit a copy reloc, if needed. */
11604 if (h->needs_copy)
11605 {
11606 Elf_Internal_Rela rel;
11607 asection *srel;
11608 bfd_byte *loc;
11609
11610 BFD_ASSERT (h->dynindx != -1);
11611
11612 rel.r_offset = (h->root.u.def.section->output_section->vma
11613 + h->root.u.def.section->output_offset
11614 + h->root.u.def.value);
11615 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11616 rel.r_addend = 0;
11617 if (h->root.u.def.section == htab->root.sdynrelro)
11618 srel = htab->root.sreldynrelro;
11619 else
11620 srel = htab->root.srelbss;
11621 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11622 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11623 ++srel->reloc_count;
11624 }
11625
11626 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11627 if (ELF_ST_IS_COMPRESSED (sym->st_other))
11628 sym->st_value &= ~1;
11629
11630 return true;
11631 }
11632
11633 /* Write out a plt0 entry to the beginning of .plt. */
11634
11635 static bool
11636 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11637 {
11638 bfd_byte *loc;
11639 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11640 static const bfd_vma *plt_entry;
11641 struct mips_elf_link_hash_table *htab;
11642
11643 htab = mips_elf_hash_table (info);
11644 BFD_ASSERT (htab != NULL);
11645
11646 if (ABI_64_P (output_bfd))
11647 plt_entry = (htab->compact_branches
11648 ? mipsr6_n64_exec_plt0_entry_compact
11649 : mips_n64_exec_plt0_entry);
11650 else if (ABI_N32_P (output_bfd))
11651 plt_entry = (htab->compact_branches
11652 ? mipsr6_n32_exec_plt0_entry_compact
11653 : mips_n32_exec_plt0_entry);
11654 else if (!htab->plt_header_is_comp)
11655 plt_entry = (htab->compact_branches
11656 ? mipsr6_o32_exec_plt0_entry_compact
11657 : mips_o32_exec_plt0_entry);
11658 else if (htab->insn32)
11659 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11660 else
11661 plt_entry = micromips_o32_exec_plt0_entry;
11662
11663 /* Calculate the value of .got.plt. */
11664 gotplt_value = (htab->root.sgotplt->output_section->vma
11665 + htab->root.sgotplt->output_offset);
11666 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11667 gotplt_value_low = gotplt_value & 0xffff;
11668
11669 /* The PLT sequence is not safe for N64 if .got.plt's address can
11670 not be loaded in two instructions. */
11671 if (ABI_64_P (output_bfd)
11672 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11673 {
11674 _bfd_error_handler
11675 /* xgettext:c-format */
11676 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11677 "supported; consider using `-Ttext-segment=...'"),
11678 output_bfd,
11679 htab->root.sgotplt->output_section,
11680 (int64_t) gotplt_value);
11681 bfd_set_error (bfd_error_no_error);
11682 return false;
11683 }
11684
11685 /* Install the PLT header. */
11686 loc = htab->root.splt->contents;
11687 if (plt_entry == micromips_o32_exec_plt0_entry)
11688 {
11689 bfd_vma gotpc_offset;
11690 bfd_vma loc_address;
11691 size_t i;
11692
11693 BFD_ASSERT (gotplt_value % 4 == 0);
11694
11695 loc_address = (htab->root.splt->output_section->vma
11696 + htab->root.splt->output_offset);
11697 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11698
11699 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11700 if (gotpc_offset + 0x1000000 >= 0x2000000)
11701 {
11702 _bfd_error_handler
11703 /* xgettext:c-format */
11704 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11705 "beyond the range of ADDIUPC"),
11706 output_bfd,
11707 htab->root.sgotplt->output_section,
11708 (int64_t) gotpc_offset,
11709 htab->root.splt->output_section);
11710 bfd_set_error (bfd_error_no_error);
11711 return false;
11712 }
11713 bfd_put_16 (output_bfd,
11714 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11715 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11716 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11717 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11718 }
11719 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11720 {
11721 size_t i;
11722
11723 bfd_put_16 (output_bfd, plt_entry[0], loc);
11724 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11725 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11726 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11727 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11728 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11729 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11730 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11731 }
11732 else
11733 {
11734 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11735 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11736 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11737 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11738 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11739 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11740 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11741 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11742 }
11743
11744 return true;
11745 }
11746
11747 /* Install the PLT header for a VxWorks executable and finalize the
11748 contents of .rela.plt.unloaded. */
11749
11750 static void
11751 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11752 {
11753 Elf_Internal_Rela rela;
11754 bfd_byte *loc;
11755 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11756 static const bfd_vma *plt_entry;
11757 struct mips_elf_link_hash_table *htab;
11758
11759 htab = mips_elf_hash_table (info);
11760 BFD_ASSERT (htab != NULL);
11761
11762 plt_entry = mips_vxworks_exec_plt0_entry;
11763
11764 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11765 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11766 + htab->root.hgot->root.u.def.section->output_offset
11767 + htab->root.hgot->root.u.def.value);
11768
11769 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11770 got_value_low = got_value & 0xffff;
11771
11772 /* Calculate the address of the PLT header. */
11773 plt_address = (htab->root.splt->output_section->vma
11774 + htab->root.splt->output_offset);
11775
11776 /* Install the PLT header. */
11777 loc = htab->root.splt->contents;
11778 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11779 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11780 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11781 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11782 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11783 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11784
11785 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11786 loc = htab->srelplt2->contents;
11787 rela.r_offset = plt_address;
11788 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11789 rela.r_addend = 0;
11790 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11791 loc += sizeof (Elf32_External_Rela);
11792
11793 /* Output the relocation for the following addiu of
11794 %lo(_GLOBAL_OFFSET_TABLE_). */
11795 rela.r_offset += 4;
11796 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11797 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11798 loc += sizeof (Elf32_External_Rela);
11799
11800 /* Fix up the remaining relocations. They may have the wrong
11801 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11802 in which symbols were output. */
11803 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11804 {
11805 Elf_Internal_Rela rel;
11806
11807 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11808 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11809 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11810 loc += sizeof (Elf32_External_Rela);
11811
11812 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11813 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11814 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11815 loc += sizeof (Elf32_External_Rela);
11816
11817 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11818 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11819 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11820 loc += sizeof (Elf32_External_Rela);
11821 }
11822 }
11823
11824 /* Install the PLT header for a VxWorks shared library. */
11825
11826 static void
11827 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11828 {
11829 unsigned int i;
11830 struct mips_elf_link_hash_table *htab;
11831
11832 htab = mips_elf_hash_table (info);
11833 BFD_ASSERT (htab != NULL);
11834
11835 /* We just need to copy the entry byte-by-byte. */
11836 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11837 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11838 htab->root.splt->contents + i * 4);
11839 }
11840
11841 /* Finish up the dynamic sections. */
11842
11843 bool
11844 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11845 struct bfd_link_info *info)
11846 {
11847 bfd *dynobj;
11848 asection *sdyn;
11849 asection *sgot;
11850 struct mips_got_info *gg, *g;
11851 struct mips_elf_link_hash_table *htab;
11852
11853 htab = mips_elf_hash_table (info);
11854 BFD_ASSERT (htab != NULL);
11855
11856 dynobj = elf_hash_table (info)->dynobj;
11857
11858 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11859
11860 sgot = htab->root.sgot;
11861 gg = htab->got_info;
11862
11863 if (elf_hash_table (info)->dynamic_sections_created)
11864 {
11865 bfd_byte *b;
11866 int dyn_to_skip = 0, dyn_skipped = 0;
11867
11868 BFD_ASSERT (sdyn != NULL);
11869 BFD_ASSERT (gg != NULL);
11870
11871 g = mips_elf_bfd_got (output_bfd, false);
11872 BFD_ASSERT (g != NULL);
11873
11874 for (b = sdyn->contents;
11875 b < sdyn->contents + sdyn->size;
11876 b += MIPS_ELF_DYN_SIZE (dynobj))
11877 {
11878 Elf_Internal_Dyn dyn;
11879 const char *name;
11880 size_t elemsize;
11881 asection *s;
11882 bool swap_out_p;
11883
11884 /* Read in the current dynamic entry. */
11885 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11886
11887 /* Assume that we're going to modify it and write it out. */
11888 swap_out_p = true;
11889
11890 switch (dyn.d_tag)
11891 {
11892 case DT_RELENT:
11893 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11894 break;
11895
11896 case DT_RELAENT:
11897 BFD_ASSERT (htab->root.target_os == is_vxworks);
11898 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11899 break;
11900
11901 case DT_STRSZ:
11902 /* Rewrite DT_STRSZ. */
11903 dyn.d_un.d_val =
11904 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11905 break;
11906
11907 case DT_PLTGOT:
11908 s = htab->root.sgot;
11909 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11910 break;
11911
11912 case DT_MIPS_PLTGOT:
11913 s = htab->root.sgotplt;
11914 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11915 break;
11916
11917 case DT_MIPS_RLD_VERSION:
11918 dyn.d_un.d_val = 1; /* XXX */
11919 break;
11920
11921 case DT_MIPS_FLAGS:
11922 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11923 break;
11924
11925 case DT_MIPS_TIME_STAMP:
11926 {
11927 time_t t;
11928 time (&t);
11929 dyn.d_un.d_val = t;
11930 }
11931 break;
11932
11933 case DT_MIPS_ICHECKSUM:
11934 /* XXX FIXME: */
11935 swap_out_p = false;
11936 break;
11937
11938 case DT_MIPS_IVERSION:
11939 /* XXX FIXME: */
11940 swap_out_p = false;
11941 break;
11942
11943 case DT_MIPS_BASE_ADDRESS:
11944 s = output_bfd->sections;
11945 BFD_ASSERT (s != NULL);
11946 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11947 break;
11948
11949 case DT_MIPS_LOCAL_GOTNO:
11950 dyn.d_un.d_val = g->local_gotno;
11951 break;
11952
11953 case DT_MIPS_UNREFEXTNO:
11954 /* The index into the dynamic symbol table which is the
11955 entry of the first external symbol that is not
11956 referenced within the same object. */
11957 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11958 break;
11959
11960 case DT_MIPS_GOTSYM:
11961 if (htab->global_gotsym)
11962 {
11963 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11964 break;
11965 }
11966 /* In case if we don't have global got symbols we default
11967 to setting DT_MIPS_GOTSYM to the same value as
11968 DT_MIPS_SYMTABNO. */
11969 /* Fall through. */
11970
11971 case DT_MIPS_SYMTABNO:
11972 name = ".dynsym";
11973 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11974 s = bfd_get_linker_section (dynobj, name);
11975
11976 if (s != NULL)
11977 dyn.d_un.d_val = s->size / elemsize;
11978 else
11979 dyn.d_un.d_val = 0;
11980 break;
11981
11982 case DT_MIPS_HIPAGENO:
11983 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11984 break;
11985
11986 case DT_MIPS_RLD_MAP:
11987 {
11988 struct elf_link_hash_entry *h;
11989 h = mips_elf_hash_table (info)->rld_symbol;
11990 if (!h)
11991 {
11992 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11993 swap_out_p = false;
11994 break;
11995 }
11996 s = h->root.u.def.section;
11997
11998 /* The MIPS_RLD_MAP tag stores the absolute address of the
11999 debug pointer. */
12000 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
12001 + h->root.u.def.value);
12002 }
12003 break;
12004
12005 case DT_MIPS_RLD_MAP_REL:
12006 {
12007 struct elf_link_hash_entry *h;
12008 bfd_vma dt_addr, rld_addr;
12009 h = mips_elf_hash_table (info)->rld_symbol;
12010 if (!h)
12011 {
12012 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12013 swap_out_p = false;
12014 break;
12015 }
12016 s = h->root.u.def.section;
12017
12018 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
12019 pointer, relative to the address of the tag. */
12020 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
12021 + (b - sdyn->contents));
12022 rld_addr = (s->output_section->vma + s->output_offset
12023 + h->root.u.def.value);
12024 dyn.d_un.d_ptr = rld_addr - dt_addr;
12025 }
12026 break;
12027
12028 case DT_MIPS_OPTIONS:
12029 s = (bfd_get_section_by_name
12030 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12031 dyn.d_un.d_ptr = s->vma;
12032 break;
12033
12034 case DT_PLTREL:
12035 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12036 if (htab->root.target_os == is_vxworks)
12037 dyn.d_un.d_val = DT_RELA;
12038 else
12039 dyn.d_un.d_val = DT_REL;
12040 break;
12041
12042 case DT_PLTRELSZ:
12043 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12044 dyn.d_un.d_val = htab->root.srelplt->size;
12045 break;
12046
12047 case DT_JMPREL:
12048 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12049 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12050 + htab->root.srelplt->output_offset);
12051 break;
12052
12053 case DT_TEXTREL:
12054 /* If we didn't need any text relocations after all, delete
12055 the dynamic tag. */
12056 if (!(info->flags & DF_TEXTREL))
12057 {
12058 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12059 swap_out_p = false;
12060 }
12061 break;
12062
12063 case DT_FLAGS:
12064 /* If we didn't need any text relocations after all, clear
12065 DF_TEXTREL from DT_FLAGS. */
12066 if (!(info->flags & DF_TEXTREL))
12067 dyn.d_un.d_val &= ~DF_TEXTREL;
12068 else
12069 swap_out_p = false;
12070 break;
12071
12072 case DT_MIPS_XHASH:
12073 name = ".MIPS.xhash";
12074 s = bfd_get_linker_section (dynobj, name);
12075 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12076 break;
12077
12078 default:
12079 swap_out_p = false;
12080 if (htab->root.target_os == is_vxworks
12081 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12082 swap_out_p = true;
12083 break;
12084 }
12085
12086 if (swap_out_p || dyn_skipped)
12087 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12088 (dynobj, &dyn, b - dyn_skipped);
12089
12090 if (dyn_to_skip)
12091 {
12092 dyn_skipped += dyn_to_skip;
12093 dyn_to_skip = 0;
12094 }
12095 }
12096
12097 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
12098 if (dyn_skipped > 0)
12099 memset (b - dyn_skipped, 0, dyn_skipped);
12100 }
12101
12102 if (sgot != NULL && sgot->size > 0
12103 && !bfd_is_abs_section (sgot->output_section))
12104 {
12105 if (htab->root.target_os == is_vxworks)
12106 {
12107 /* The first entry of the global offset table points to the
12108 ".dynamic" section. The second is initialized by the
12109 loader and contains the shared library identifier.
12110 The third is also initialized by the loader and points
12111 to the lazy resolution stub. */
12112 MIPS_ELF_PUT_WORD (output_bfd,
12113 sdyn->output_offset + sdyn->output_section->vma,
12114 sgot->contents);
12115 MIPS_ELF_PUT_WORD (output_bfd, 0,
12116 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12117 MIPS_ELF_PUT_WORD (output_bfd, 0,
12118 sgot->contents
12119 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12120 }
12121 else
12122 {
12123 /* The first entry of the global offset table will be filled at
12124 runtime. The second entry will be used by some runtime loaders.
12125 This isn't the case of IRIX rld. */
12126 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12127 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12128 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12129 }
12130
12131 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12132 = MIPS_ELF_GOT_SIZE (output_bfd);
12133 }
12134
12135 /* Generate dynamic relocations for the non-primary gots. */
12136 if (gg != NULL && gg->next)
12137 {
12138 Elf_Internal_Rela rel[3];
12139 bfd_vma addend = 0;
12140
12141 memset (rel, 0, sizeof (rel));
12142 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12143
12144 for (g = gg->next; g->next != gg; g = g->next)
12145 {
12146 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12147 + g->next->tls_gotno;
12148
12149 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12150 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12151 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12152 sgot->contents
12153 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12154
12155 if (! bfd_link_pic (info))
12156 continue;
12157
12158 for (; got_index < g->local_gotno; got_index++)
12159 {
12160 if (got_index >= g->assigned_low_gotno
12161 && got_index <= g->assigned_high_gotno)
12162 continue;
12163
12164 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12165 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12166 if (!(mips_elf_create_dynamic_relocation
12167 (output_bfd, info, rel, NULL,
12168 bfd_abs_section_ptr,
12169 0, &addend, sgot)))
12170 return false;
12171 BFD_ASSERT (addend == 0);
12172 }
12173 }
12174 }
12175
12176 /* The generation of dynamic relocations for the non-primary gots
12177 adds more dynamic relocations. We cannot count them until
12178 here. */
12179
12180 if (elf_hash_table (info)->dynamic_sections_created)
12181 {
12182 bfd_byte *b;
12183 bool swap_out_p;
12184
12185 BFD_ASSERT (sdyn != NULL);
12186
12187 for (b = sdyn->contents;
12188 b < sdyn->contents + sdyn->size;
12189 b += MIPS_ELF_DYN_SIZE (dynobj))
12190 {
12191 Elf_Internal_Dyn dyn;
12192 asection *s;
12193
12194 /* Read in the current dynamic entry. */
12195 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12196
12197 /* Assume that we're going to modify it and write it out. */
12198 swap_out_p = true;
12199
12200 switch (dyn.d_tag)
12201 {
12202 case DT_RELSZ:
12203 /* Reduce DT_RELSZ to account for any relocations we
12204 decided not to make. This is for the n64 irix rld,
12205 which doesn't seem to apply any relocations if there
12206 are trailing null entries. */
12207 s = mips_elf_rel_dyn_section (info, false);
12208 dyn.d_un.d_val = (s->reloc_count
12209 * (ABI_64_P (output_bfd)
12210 ? sizeof (Elf64_Mips_External_Rel)
12211 : sizeof (Elf32_External_Rel)));
12212 /* Adjust the section size too. Tools like the prelinker
12213 can reasonably expect the values to the same. */
12214 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12215 elf_section_data (s->output_section)->this_hdr.sh_size
12216 = dyn.d_un.d_val;
12217 break;
12218
12219 default:
12220 swap_out_p = false;
12221 break;
12222 }
12223
12224 if (swap_out_p)
12225 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12226 (dynobj, &dyn, b);
12227 }
12228 }
12229
12230 {
12231 asection *s;
12232 Elf32_compact_rel cpt;
12233
12234 if (SGI_COMPAT (output_bfd))
12235 {
12236 /* Write .compact_rel section out. */
12237 s = bfd_get_linker_section (dynobj, ".compact_rel");
12238 if (s != NULL)
12239 {
12240 cpt.id1 = 1;
12241 cpt.num = s->reloc_count;
12242 cpt.id2 = 2;
12243 cpt.offset = (s->output_section->filepos
12244 + sizeof (Elf32_External_compact_rel));
12245 cpt.reserved0 = 0;
12246 cpt.reserved1 = 0;
12247 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12248 ((Elf32_External_compact_rel *)
12249 s->contents));
12250
12251 /* Clean up a dummy stub function entry in .text. */
12252 if (htab->sstubs != NULL
12253 && htab->sstubs->contents != NULL)
12254 {
12255 file_ptr dummy_offset;
12256
12257 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12258 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12259 memset (htab->sstubs->contents + dummy_offset, 0,
12260 htab->function_stub_size);
12261 }
12262 }
12263 }
12264
12265 /* The psABI says that the dynamic relocations must be sorted in
12266 increasing order of r_symndx. The VxWorks EABI doesn't require
12267 this, and because the code below handles REL rather than RELA
12268 relocations, using it for VxWorks would be outright harmful. */
12269 if (htab->root.target_os != is_vxworks)
12270 {
12271 s = mips_elf_rel_dyn_section (info, false);
12272 if (s != NULL
12273 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12274 {
12275 reldyn_sorting_bfd = output_bfd;
12276
12277 if (ABI_64_P (output_bfd))
12278 qsort ((Elf64_External_Rel *) s->contents + 1,
12279 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12280 sort_dynamic_relocs_64);
12281 else
12282 qsort ((Elf32_External_Rel *) s->contents + 1,
12283 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12284 sort_dynamic_relocs);
12285 }
12286 }
12287 }
12288
12289 if (htab->root.splt && htab->root.splt->size > 0)
12290 {
12291 if (htab->root.target_os == is_vxworks)
12292 {
12293 if (bfd_link_pic (info))
12294 mips_vxworks_finish_shared_plt (output_bfd, info);
12295 else
12296 mips_vxworks_finish_exec_plt (output_bfd, info);
12297 }
12298 else
12299 {
12300 BFD_ASSERT (!bfd_link_pic (info));
12301 if (!mips_finish_exec_plt (output_bfd, info))
12302 return false;
12303 }
12304 }
12305 return true;
12306 }
12307
12308
12309 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12310
12311 static void
12312 mips_set_isa_flags (bfd *abfd)
12313 {
12314 flagword val;
12315
12316 switch (bfd_get_mach (abfd))
12317 {
12318 default:
12319 if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12320 val = MIPS_DEFAULT_R6 ? E_MIPS_ARCH_64R6 : E_MIPS_ARCH_3;
12321 else
12322 val = MIPS_DEFAULT_R6 ? E_MIPS_ARCH_32R6 : E_MIPS_ARCH_1;
12323 break;
12324
12325 case bfd_mach_mips3000:
12326 val = E_MIPS_ARCH_1;
12327 break;
12328
12329 case bfd_mach_mips3900:
12330 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12331 break;
12332
12333 case bfd_mach_mips6000:
12334 val = E_MIPS_ARCH_2;
12335 break;
12336
12337 case bfd_mach_mips4010:
12338 val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12339 break;
12340
12341 case bfd_mach_mips_allegrex:
12342 val = E_MIPS_ARCH_2 | E_MIPS_MACH_ALLEGREX;
12343 break;
12344
12345 case bfd_mach_mips4000:
12346 case bfd_mach_mips4300:
12347 case bfd_mach_mips4400:
12348 case bfd_mach_mips4600:
12349 val = E_MIPS_ARCH_3;
12350 break;
12351
12352 case bfd_mach_mips4100:
12353 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12354 break;
12355
12356 case bfd_mach_mips4111:
12357 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12358 break;
12359
12360 case bfd_mach_mips4120:
12361 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12362 break;
12363
12364 case bfd_mach_mips4650:
12365 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12366 break;
12367
12368 case bfd_mach_mips5400:
12369 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12370 break;
12371
12372 case bfd_mach_mips5500:
12373 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12374 break;
12375
12376 case bfd_mach_mips5900:
12377 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12378 break;
12379
12380 case bfd_mach_mips9000:
12381 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12382 break;
12383
12384 case bfd_mach_mips5000:
12385 case bfd_mach_mips7000:
12386 case bfd_mach_mips8000:
12387 case bfd_mach_mips10000:
12388 case bfd_mach_mips12000:
12389 case bfd_mach_mips14000:
12390 case bfd_mach_mips16000:
12391 val = E_MIPS_ARCH_4;
12392 break;
12393
12394 case bfd_mach_mips5:
12395 val = E_MIPS_ARCH_5;
12396 break;
12397
12398 case bfd_mach_mips_loongson_2e:
12399 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12400 break;
12401
12402 case bfd_mach_mips_loongson_2f:
12403 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12404 break;
12405
12406 case bfd_mach_mips_sb1:
12407 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12408 break;
12409
12410 case bfd_mach_mips_gs464:
12411 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
12412 break;
12413
12414 case bfd_mach_mips_gs464e:
12415 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12416 break;
12417
12418 case bfd_mach_mips_gs264e:
12419 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12420 break;
12421
12422 case bfd_mach_mips_octeon:
12423 case bfd_mach_mips_octeonp:
12424 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12425 break;
12426
12427 case bfd_mach_mips_octeon3:
12428 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12429 break;
12430
12431 case bfd_mach_mips_xlr:
12432 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12433 break;
12434
12435 case bfd_mach_mips_octeon2:
12436 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12437 break;
12438
12439 case bfd_mach_mipsisa32:
12440 val = E_MIPS_ARCH_32;
12441 break;
12442
12443 case bfd_mach_mipsisa64:
12444 val = E_MIPS_ARCH_64;
12445 break;
12446
12447 case bfd_mach_mipsisa32r2:
12448 case bfd_mach_mipsisa32r3:
12449 case bfd_mach_mipsisa32r5:
12450 val = E_MIPS_ARCH_32R2;
12451 break;
12452
12453 case bfd_mach_mips_interaptiv_mr2:
12454 val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12455 break;
12456
12457 case bfd_mach_mipsisa64r2:
12458 case bfd_mach_mipsisa64r3:
12459 case bfd_mach_mipsisa64r5:
12460 val = E_MIPS_ARCH_64R2;
12461 break;
12462
12463 case bfd_mach_mipsisa32r6:
12464 val = E_MIPS_ARCH_32R6;
12465 break;
12466
12467 case bfd_mach_mipsisa64r6:
12468 val = E_MIPS_ARCH_64R6;
12469 break;
12470 }
12471 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12472 elf_elfheader (abfd)->e_flags |= val;
12473
12474 }
12475
12476
12477 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12478 Don't do so for code sections. We want to keep ordering of HI16/LO16
12479 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12480 relocs to be sorted. */
12481
12482 bool
12483 _bfd_mips_elf_sort_relocs_p (asection *sec)
12484 {
12485 return (sec->flags & SEC_CODE) == 0;
12486 }
12487
12488
12489 /* The final processing done just before writing out a MIPS ELF object
12490 file. This gets the MIPS architecture right based on the machine
12491 number. This is used by both the 32-bit and the 64-bit ABI. */
12492
12493 void
12494 _bfd_mips_final_write_processing (bfd *abfd)
12495 {
12496 unsigned int i;
12497 Elf_Internal_Shdr **hdrpp;
12498 const char *name;
12499 asection *sec;
12500
12501 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12502 is nonzero. This is for compatibility with old objects, which used
12503 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12504 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12505 mips_set_isa_flags (abfd);
12506
12507 /* Set the sh_info field for .gptab sections and other appropriate
12508 info for each special section. */
12509 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12510 i < elf_numsections (abfd);
12511 i++, hdrpp++)
12512 {
12513 switch ((*hdrpp)->sh_type)
12514 {
12515 case SHT_MIPS_MSYM:
12516 case SHT_MIPS_LIBLIST:
12517 sec = bfd_get_section_by_name (abfd, ".dynstr");
12518 if (sec != NULL)
12519 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12520 break;
12521
12522 case SHT_MIPS_GPTAB:
12523 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12524 name = bfd_section_name ((*hdrpp)->bfd_section);
12525 BFD_ASSERT (name != NULL
12526 && startswith (name, ".gptab."));
12527 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12528 BFD_ASSERT (sec != NULL);
12529 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12530 break;
12531
12532 case SHT_MIPS_CONTENT:
12533 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12534 name = bfd_section_name ((*hdrpp)->bfd_section);
12535 BFD_ASSERT (name != NULL
12536 && startswith (name, ".MIPS.content"));
12537 sec = bfd_get_section_by_name (abfd,
12538 name + sizeof ".MIPS.content" - 1);
12539 BFD_ASSERT (sec != NULL);
12540 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12541 break;
12542
12543 case SHT_MIPS_SYMBOL_LIB:
12544 sec = bfd_get_section_by_name (abfd, ".dynsym");
12545 if (sec != NULL)
12546 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12547 sec = bfd_get_section_by_name (abfd, ".liblist");
12548 if (sec != NULL)
12549 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12550 break;
12551
12552 case SHT_MIPS_EVENTS:
12553 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12554 name = bfd_section_name ((*hdrpp)->bfd_section);
12555 BFD_ASSERT (name != NULL);
12556 if (startswith (name, ".MIPS.events"))
12557 sec = bfd_get_section_by_name (abfd,
12558 name + sizeof ".MIPS.events" - 1);
12559 else
12560 {
12561 BFD_ASSERT (startswith (name, ".MIPS.post_rel"));
12562 sec = bfd_get_section_by_name (abfd,
12563 (name
12564 + sizeof ".MIPS.post_rel" - 1));
12565 }
12566 BFD_ASSERT (sec != NULL);
12567 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12568 break;
12569
12570 case SHT_MIPS_XHASH:
12571 sec = bfd_get_section_by_name (abfd, ".dynsym");
12572 if (sec != NULL)
12573 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12574 }
12575 }
12576 }
12577
12578 bool
12579 _bfd_mips_elf_final_write_processing (bfd *abfd)
12580 {
12581 _bfd_mips_final_write_processing (abfd);
12582 return _bfd_elf_final_write_processing (abfd);
12583 }
12584 \f
12585 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
12586 segments. */
12587
12588 int
12589 _bfd_mips_elf_additional_program_headers (bfd *abfd,
12590 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12591 {
12592 asection *s;
12593 int ret = 0;
12594
12595 /* See if we need a PT_MIPS_REGINFO segment. */
12596 s = bfd_get_section_by_name (abfd, ".reginfo");
12597 if (s && (s->flags & SEC_LOAD))
12598 ++ret;
12599
12600 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12601 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12602 ++ret;
12603
12604 /* See if we need a PT_MIPS_OPTIONS segment. */
12605 if (IRIX_COMPAT (abfd) == ict_irix6
12606 && bfd_get_section_by_name (abfd,
12607 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12608 ++ret;
12609
12610 /* See if we need a PT_MIPS_RTPROC segment. */
12611 if (IRIX_COMPAT (abfd) == ict_irix5
12612 && bfd_get_section_by_name (abfd, ".dynamic")
12613 && bfd_get_section_by_name (abfd, ".mdebug"))
12614 ++ret;
12615
12616 /* Allocate a PT_NULL header in dynamic objects. See
12617 _bfd_mips_elf_modify_segment_map for details. */
12618 if (!SGI_COMPAT (abfd)
12619 && bfd_get_section_by_name (abfd, ".dynamic"))
12620 ++ret;
12621
12622 return ret;
12623 }
12624
12625 /* Modify the segment map for an IRIX5 executable. */
12626
12627 bool
12628 _bfd_mips_elf_modify_segment_map (bfd *abfd,
12629 struct bfd_link_info *info)
12630 {
12631 asection *s;
12632 struct elf_segment_map *m, **pm;
12633 size_t amt;
12634
12635 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12636 segment. */
12637 s = bfd_get_section_by_name (abfd, ".reginfo");
12638 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12639 {
12640 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12641 if (m->p_type == PT_MIPS_REGINFO)
12642 break;
12643 if (m == NULL)
12644 {
12645 amt = sizeof *m;
12646 m = bfd_zalloc (abfd, amt);
12647 if (m == NULL)
12648 return false;
12649
12650 m->p_type = PT_MIPS_REGINFO;
12651 m->count = 1;
12652 m->sections[0] = s;
12653
12654 /* We want to put it after the PHDR and INTERP segments. */
12655 pm = &elf_seg_map (abfd);
12656 while (*pm != NULL
12657 && ((*pm)->p_type == PT_PHDR
12658 || (*pm)->p_type == PT_INTERP))
12659 pm = &(*pm)->next;
12660
12661 m->next = *pm;
12662 *pm = m;
12663 }
12664 }
12665
12666 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12667 segment. */
12668 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12669 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12670 {
12671 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12672 if (m->p_type == PT_MIPS_ABIFLAGS)
12673 break;
12674 if (m == NULL)
12675 {
12676 amt = sizeof *m;
12677 m = bfd_zalloc (abfd, amt);
12678 if (m == NULL)
12679 return false;
12680
12681 m->p_type = PT_MIPS_ABIFLAGS;
12682 m->count = 1;
12683 m->sections[0] = s;
12684
12685 /* We want to put it after the PHDR and INTERP segments. */
12686 pm = &elf_seg_map (abfd);
12687 while (*pm != NULL
12688 && ((*pm)->p_type == PT_PHDR
12689 || (*pm)->p_type == PT_INTERP))
12690 pm = &(*pm)->next;
12691
12692 m->next = *pm;
12693 *pm = m;
12694 }
12695 }
12696
12697 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12698 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
12699 PT_MIPS_OPTIONS segment immediately following the program header
12700 table. */
12701 if (NEWABI_P (abfd)
12702 /* On non-IRIX6 new abi, we'll have already created a segment
12703 for this section, so don't create another. I'm not sure this
12704 is not also the case for IRIX 6, but I can't test it right
12705 now. */
12706 && IRIX_COMPAT (abfd) == ict_irix6)
12707 {
12708 for (s = abfd->sections; s; s = s->next)
12709 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12710 break;
12711
12712 if (s)
12713 {
12714 struct elf_segment_map *options_segment;
12715
12716 pm = &elf_seg_map (abfd);
12717 while (*pm != NULL
12718 && ((*pm)->p_type == PT_PHDR
12719 || (*pm)->p_type == PT_INTERP))
12720 pm = &(*pm)->next;
12721
12722 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12723 {
12724 amt = sizeof (struct elf_segment_map);
12725 options_segment = bfd_zalloc (abfd, amt);
12726 options_segment->next = *pm;
12727 options_segment->p_type = PT_MIPS_OPTIONS;
12728 options_segment->p_flags = PF_R;
12729 options_segment->p_flags_valid = true;
12730 options_segment->count = 1;
12731 options_segment->sections[0] = s;
12732 *pm = options_segment;
12733 }
12734 }
12735 }
12736 else
12737 {
12738 if (IRIX_COMPAT (abfd) == ict_irix5)
12739 {
12740 /* If there are .dynamic and .mdebug sections, we make a room
12741 for the RTPROC header. FIXME: Rewrite without section names. */
12742 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12743 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12744 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12745 {
12746 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12747 if (m->p_type == PT_MIPS_RTPROC)
12748 break;
12749 if (m == NULL)
12750 {
12751 amt = sizeof *m;
12752 m = bfd_zalloc (abfd, amt);
12753 if (m == NULL)
12754 return false;
12755
12756 m->p_type = PT_MIPS_RTPROC;
12757
12758 s = bfd_get_section_by_name (abfd, ".rtproc");
12759 if (s == NULL)
12760 {
12761 m->count = 0;
12762 m->p_flags = 0;
12763 m->p_flags_valid = 1;
12764 }
12765 else
12766 {
12767 m->count = 1;
12768 m->sections[0] = s;
12769 }
12770
12771 /* We want to put it after the DYNAMIC segment. */
12772 pm = &elf_seg_map (abfd);
12773 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12774 pm = &(*pm)->next;
12775 if (*pm != NULL)
12776 pm = &(*pm)->next;
12777
12778 m->next = *pm;
12779 *pm = m;
12780 }
12781 }
12782 }
12783 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12784 .dynstr, .dynsym, and .hash sections, and everything in
12785 between. */
12786 for (pm = &elf_seg_map (abfd); *pm != NULL;
12787 pm = &(*pm)->next)
12788 if ((*pm)->p_type == PT_DYNAMIC)
12789 break;
12790 m = *pm;
12791 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12792 glibc's dynamic linker has traditionally derived the number of
12793 tags from the p_filesz field, and sometimes allocates stack
12794 arrays of that size. An overly-big PT_DYNAMIC segment can
12795 be actively harmful in such cases. Making PT_DYNAMIC contain
12796 other sections can also make life hard for the prelinker,
12797 which might move one of the other sections to a different
12798 PT_LOAD segment. */
12799 if (SGI_COMPAT (abfd)
12800 && m != NULL
12801 && m->count == 1
12802 && strcmp (m->sections[0]->name, ".dynamic") == 0)
12803 {
12804 static const char *sec_names[] =
12805 {
12806 ".dynamic", ".dynstr", ".dynsym", ".hash"
12807 };
12808 bfd_vma low, high;
12809 unsigned int i, c;
12810 struct elf_segment_map *n;
12811
12812 low = ~(bfd_vma) 0;
12813 high = 0;
12814 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12815 {
12816 s = bfd_get_section_by_name (abfd, sec_names[i]);
12817 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12818 {
12819 bfd_size_type sz;
12820
12821 if (low > s->vma)
12822 low = s->vma;
12823 sz = s->size;
12824 if (high < s->vma + sz)
12825 high = s->vma + sz;
12826 }
12827 }
12828
12829 c = 0;
12830 for (s = abfd->sections; s != NULL; s = s->next)
12831 if ((s->flags & SEC_LOAD) != 0
12832 && s->vma >= low
12833 && s->vma + s->size <= high)
12834 ++c;
12835
12836 amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12837 n = bfd_zalloc (abfd, amt);
12838 if (n == NULL)
12839 return false;
12840 *n = *m;
12841 n->count = c;
12842
12843 i = 0;
12844 for (s = abfd->sections; s != NULL; s = s->next)
12845 {
12846 if ((s->flags & SEC_LOAD) != 0
12847 && s->vma >= low
12848 && s->vma + s->size <= high)
12849 {
12850 n->sections[i] = s;
12851 ++i;
12852 }
12853 }
12854
12855 *pm = n;
12856 }
12857 }
12858
12859 /* Allocate a spare program header in dynamic objects so that tools
12860 like the prelinker can add an extra PT_LOAD entry.
12861
12862 If the prelinker needs to make room for a new PT_LOAD entry, its
12863 standard procedure is to move the first (read-only) sections into
12864 the new (writable) segment. However, the MIPS ABI requires
12865 .dynamic to be in a read-only segment, and the section will often
12866 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12867
12868 Although the prelinker could in principle move .dynamic to a
12869 writable segment, it seems better to allocate a spare program
12870 header instead, and avoid the need to move any sections.
12871 There is a long tradition of allocating spare dynamic tags,
12872 so allocating a spare program header seems like a natural
12873 extension.
12874
12875 If INFO is NULL, we may be copying an already prelinked binary
12876 with objcopy or strip, so do not add this header. */
12877 if (info != NULL
12878 && !SGI_COMPAT (abfd)
12879 && bfd_get_section_by_name (abfd, ".dynamic"))
12880 {
12881 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12882 if ((*pm)->p_type == PT_NULL)
12883 break;
12884 if (*pm == NULL)
12885 {
12886 m = bfd_zalloc (abfd, sizeof (*m));
12887 if (m == NULL)
12888 return false;
12889
12890 m->p_type = PT_NULL;
12891 *pm = m;
12892 }
12893 }
12894
12895 return true;
12896 }
12897 \f
12898 /* Return the section that should be marked against GC for a given
12899 relocation. */
12900
12901 asection *
12902 _bfd_mips_elf_gc_mark_hook (asection *sec,
12903 struct bfd_link_info *info,
12904 Elf_Internal_Rela *rel,
12905 struct elf_link_hash_entry *h,
12906 Elf_Internal_Sym *sym)
12907 {
12908 /* ??? Do mips16 stub sections need to be handled special? */
12909
12910 if (h != NULL)
12911 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12912 {
12913 case R_MIPS_GNU_VTINHERIT:
12914 case R_MIPS_GNU_VTENTRY:
12915 return NULL;
12916 }
12917
12918 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12919 }
12920
12921 /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12922
12923 bool
12924 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12925 elf_gc_mark_hook_fn gc_mark_hook)
12926 {
12927 bfd *sub;
12928
12929 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12930
12931 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12932 {
12933 asection *o;
12934
12935 if (! is_mips_elf (sub))
12936 continue;
12937
12938 for (o = sub->sections; o != NULL; o = o->next)
12939 if (!o->gc_mark
12940 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12941 {
12942 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12943 return false;
12944 }
12945 }
12946
12947 return true;
12948 }
12949 \f
12950 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12951 hiding the old indirect symbol. Process additional relocation
12952 information. Also called for weakdefs, in which case we just let
12953 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12954
12955 void
12956 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12957 struct elf_link_hash_entry *dir,
12958 struct elf_link_hash_entry *ind)
12959 {
12960 struct mips_elf_link_hash_entry *dirmips, *indmips;
12961
12962 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12963
12964 dirmips = (struct mips_elf_link_hash_entry *) dir;
12965 indmips = (struct mips_elf_link_hash_entry *) ind;
12966 /* Any absolute non-dynamic relocations against an indirect or weak
12967 definition will be against the target symbol. */
12968 if (indmips->has_static_relocs)
12969 dirmips->has_static_relocs = true;
12970
12971 if (ind->root.type != bfd_link_hash_indirect)
12972 return;
12973
12974 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12975 if (indmips->readonly_reloc)
12976 dirmips->readonly_reloc = true;
12977 if (indmips->no_fn_stub)
12978 dirmips->no_fn_stub = true;
12979 if (indmips->fn_stub)
12980 {
12981 dirmips->fn_stub = indmips->fn_stub;
12982 indmips->fn_stub = NULL;
12983 }
12984 if (indmips->need_fn_stub)
12985 {
12986 dirmips->need_fn_stub = true;
12987 indmips->need_fn_stub = false;
12988 }
12989 if (indmips->call_stub)
12990 {
12991 dirmips->call_stub = indmips->call_stub;
12992 indmips->call_stub = NULL;
12993 }
12994 if (indmips->call_fp_stub)
12995 {
12996 dirmips->call_fp_stub = indmips->call_fp_stub;
12997 indmips->call_fp_stub = NULL;
12998 }
12999 if (indmips->global_got_area < dirmips->global_got_area)
13000 dirmips->global_got_area = indmips->global_got_area;
13001 if (indmips->global_got_area < GGA_NONE)
13002 indmips->global_got_area = GGA_NONE;
13003 if (indmips->has_nonpic_branches)
13004 dirmips->has_nonpic_branches = true;
13005 }
13006
13007 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
13008 to hide it. It has to remain global (it will also be protected) so as to
13009 be assigned a global GOT entry, which will then remain unchanged at load
13010 time. */
13011
13012 void
13013 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
13014 struct elf_link_hash_entry *entry,
13015 bool force_local)
13016 {
13017 struct mips_elf_link_hash_table *htab;
13018
13019 htab = mips_elf_hash_table (info);
13020 BFD_ASSERT (htab != NULL);
13021 if (htab->use_absolute_zero
13022 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
13023 return;
13024
13025 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
13026 }
13027 \f
13028 #define PDR_SIZE 32
13029
13030 bool
13031 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13032 struct bfd_link_info *info)
13033 {
13034 asection *o;
13035 bool ret = false;
13036 unsigned char *tdata;
13037 size_t i, skip;
13038
13039 o = bfd_get_section_by_name (abfd, ".pdr");
13040 if (! o)
13041 return false;
13042 if (o->size == 0)
13043 return false;
13044 if (o->size % PDR_SIZE != 0)
13045 return false;
13046 if (o->output_section != NULL
13047 && bfd_is_abs_section (o->output_section))
13048 return false;
13049
13050 tdata = bfd_zmalloc (o->size / PDR_SIZE);
13051 if (! tdata)
13052 return false;
13053
13054 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13055 info->keep_memory);
13056 if (!cookie->rels)
13057 {
13058 free (tdata);
13059 return false;
13060 }
13061
13062 cookie->rel = cookie->rels;
13063 cookie->relend = cookie->rels + o->reloc_count;
13064
13065 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13066 {
13067 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13068 {
13069 tdata[i] = 1;
13070 skip ++;
13071 }
13072 }
13073
13074 if (skip != 0)
13075 {
13076 mips_elf_section_data (o)->u.tdata = tdata;
13077 if (o->rawsize == 0)
13078 o->rawsize = o->size;
13079 o->size -= skip * PDR_SIZE;
13080 ret = true;
13081 }
13082 else
13083 free (tdata);
13084
13085 if (! info->keep_memory)
13086 free (cookie->rels);
13087
13088 return ret;
13089 }
13090
13091 bool
13092 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13093 {
13094 if (strcmp (sec->name, ".pdr") == 0)
13095 return true;
13096 return false;
13097 }
13098
13099 bool
13100 _bfd_mips_elf_write_section (bfd *output_bfd,
13101 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13102 asection *sec, bfd_byte *contents)
13103 {
13104 bfd_byte *to, *from, *end;
13105 int i;
13106
13107 if (strcmp (sec->name, ".pdr") != 0)
13108 return false;
13109
13110 if (mips_elf_section_data (sec)->u.tdata == NULL)
13111 return false;
13112
13113 to = contents;
13114 end = contents + sec->size;
13115 for (from = contents, i = 0;
13116 from < end;
13117 from += PDR_SIZE, i++)
13118 {
13119 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13120 continue;
13121 if (to != from)
13122 memcpy (to, from, PDR_SIZE);
13123 to += PDR_SIZE;
13124 }
13125 bfd_set_section_contents (output_bfd, sec->output_section, contents,
13126 sec->output_offset, sec->size);
13127 return true;
13128 }
13129 \f
13130 /* microMIPS code retains local labels for linker relaxation. Omit them
13131 from output by default for clarity. */
13132
13133 bool
13134 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13135 {
13136 return _bfd_elf_is_local_label_name (abfd, sym->name);
13137 }
13138
13139 bool
13140 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13141 asection *section, bfd_vma offset,
13142 const char **filename_ptr,
13143 const char **functionname_ptr,
13144 unsigned int *line_ptr,
13145 unsigned int *discriminator_ptr)
13146 {
13147 asection *msec;
13148
13149 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13150 filename_ptr, functionname_ptr,
13151 line_ptr, discriminator_ptr,
13152 dwarf_debug_sections,
13153 &elf_tdata (abfd)->dwarf2_find_line_info)
13154 == 1)
13155 return true;
13156
13157 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13158 filename_ptr, functionname_ptr,
13159 line_ptr))
13160 {
13161 if (!*functionname_ptr)
13162 _bfd_elf_find_function (abfd, symbols, section, offset,
13163 *filename_ptr ? NULL : filename_ptr,
13164 functionname_ptr);
13165 return true;
13166 }
13167
13168 msec = bfd_get_section_by_name (abfd, ".mdebug");
13169 if (msec != NULL)
13170 {
13171 flagword origflags;
13172 struct mips_elf_find_line *fi;
13173 const struct ecoff_debug_swap * const swap =
13174 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13175
13176 /* If we are called during a link, mips_elf_final_link may have
13177 cleared the SEC_HAS_CONTENTS field. We force it back on here
13178 if appropriate (which it normally will be). */
13179 origflags = msec->flags;
13180 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13181 msec->flags |= SEC_HAS_CONTENTS;
13182
13183 fi = mips_elf_tdata (abfd)->find_line_info;
13184 if (fi == NULL)
13185 {
13186 bfd_size_type external_fdr_size;
13187 char *fraw_src;
13188 char *fraw_end;
13189 struct fdr *fdr_ptr;
13190 bfd_size_type amt = sizeof (struct mips_elf_find_line);
13191
13192 fi = bfd_zalloc (abfd, amt);
13193 if (fi == NULL)
13194 {
13195 msec->flags = origflags;
13196 return false;
13197 }
13198
13199 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13200 {
13201 msec->flags = origflags;
13202 return false;
13203 }
13204
13205 /* Swap in the FDR information. */
13206 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13207 fi->d.fdr = bfd_alloc (abfd, amt);
13208 if (fi->d.fdr == NULL)
13209 {
13210 _bfd_ecoff_free_ecoff_debug_info (&fi->d);
13211 msec->flags = origflags;
13212 return false;
13213 }
13214 external_fdr_size = swap->external_fdr_size;
13215 fdr_ptr = fi->d.fdr;
13216 fraw_src = (char *) fi->d.external_fdr;
13217 fraw_end = (fraw_src
13218 + fi->d.symbolic_header.ifdMax * external_fdr_size);
13219 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13220 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13221
13222 mips_elf_tdata (abfd)->find_line_info = fi;
13223 }
13224
13225 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13226 &fi->i, filename_ptr, functionname_ptr,
13227 line_ptr))
13228 {
13229 msec->flags = origflags;
13230 return true;
13231 }
13232
13233 msec->flags = origflags;
13234 }
13235
13236 /* Fall back on the generic ELF find_nearest_line routine. */
13237
13238 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13239 filename_ptr, functionname_ptr,
13240 line_ptr, discriminator_ptr);
13241 }
13242
13243 bool
13244 _bfd_mips_elf_find_inliner_info (bfd *abfd,
13245 const char **filename_ptr,
13246 const char **functionname_ptr,
13247 unsigned int *line_ptr)
13248 {
13249 bool found;
13250 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13251 functionname_ptr, line_ptr,
13252 & elf_tdata (abfd)->dwarf2_find_line_info);
13253 return found;
13254 }
13255
13256 \f
13257 /* When are writing out the .options or .MIPS.options section,
13258 remember the bytes we are writing out, so that we can install the
13259 GP value in the section_processing routine. */
13260
13261 bool
13262 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13263 const void *location,
13264 file_ptr offset, bfd_size_type count)
13265 {
13266 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13267 {
13268 bfd_byte *c;
13269
13270 if (elf_section_data (section) == NULL)
13271 {
13272 size_t amt = sizeof (struct bfd_elf_section_data);
13273 section->used_by_bfd = bfd_zalloc (abfd, amt);
13274 if (elf_section_data (section) == NULL)
13275 return false;
13276 }
13277 c = mips_elf_section_data (section)->u.tdata;
13278 if (c == NULL)
13279 {
13280 c = bfd_zalloc (abfd, section->size);
13281 if (c == NULL)
13282 return false;
13283 mips_elf_section_data (section)->u.tdata = c;
13284 }
13285
13286 memcpy (c + offset, location, count);
13287 }
13288
13289 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13290 count);
13291 }
13292
13293 /* This is almost identical to bfd_generic_get_... except that some
13294 MIPS relocations need to be handled specially. Sigh. */
13295
13296 bfd_byte *
13297 _bfd_elf_mips_get_relocated_section_contents
13298 (bfd *abfd,
13299 struct bfd_link_info *link_info,
13300 struct bfd_link_order *link_order,
13301 bfd_byte *data,
13302 bool relocatable,
13303 asymbol **symbols)
13304 {
13305 bfd *input_bfd = link_order->u.indirect.section->owner;
13306 asection *input_section = link_order->u.indirect.section;
13307 long reloc_size;
13308 arelent **reloc_vector;
13309 long reloc_count;
13310
13311 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13312 if (reloc_size < 0)
13313 return NULL;
13314
13315 /* Read in the section. */
13316 bfd_byte *orig_data = data;
13317 if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13318 return NULL;
13319
13320 if (data == NULL)
13321 return NULL;
13322
13323 if (reloc_size == 0)
13324 return data;
13325
13326 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13327 if (reloc_vector == NULL)
13328 {
13329 struct mips_elf_obj_tdata *tdata;
13330 struct mips_hi16 **hip, *hi;
13331 error_return:
13332 /* If we are going to return an error, remove entries on
13333 mips_hi16_list that point into this section's data. Data
13334 will typically be freed on return from this function. */
13335 tdata = mips_elf_tdata (abfd);
13336 hip = &tdata->mips_hi16_list;
13337 while ((hi = *hip) != NULL)
13338 {
13339 if (hi->input_section == input_section)
13340 {
13341 *hip = hi->next;
13342 free (hi);
13343 }
13344 else
13345 hip = &hi->next;
13346 }
13347 if (orig_data == NULL)
13348 free (data);
13349 data = NULL;
13350 goto out;
13351 }
13352
13353 reloc_count = bfd_canonicalize_reloc (input_bfd,
13354 input_section,
13355 reloc_vector,
13356 symbols);
13357 if (reloc_count < 0)
13358 goto error_return;
13359
13360 if (reloc_count > 0)
13361 {
13362 arelent **parent;
13363 /* for mips */
13364 int gp_found;
13365 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13366
13367 {
13368 struct bfd_hash_entry *h;
13369 struct bfd_link_hash_entry *lh;
13370 /* Skip all this stuff if we aren't mixing formats. */
13371 if (abfd && input_bfd
13372 && abfd->xvec == input_bfd->xvec)
13373 lh = 0;
13374 else
13375 {
13376 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13377 lh = (struct bfd_link_hash_entry *) h;
13378 }
13379 lookup:
13380 if (lh)
13381 {
13382 switch (lh->type)
13383 {
13384 case bfd_link_hash_undefined:
13385 case bfd_link_hash_undefweak:
13386 case bfd_link_hash_common:
13387 gp_found = 0;
13388 break;
13389 case bfd_link_hash_defined:
13390 case bfd_link_hash_defweak:
13391 gp_found = 1;
13392 gp = lh->u.def.value;
13393 break;
13394 case bfd_link_hash_indirect:
13395 case bfd_link_hash_warning:
13396 lh = lh->u.i.link;
13397 /* @@FIXME ignoring warning for now */
13398 goto lookup;
13399 case bfd_link_hash_new:
13400 default:
13401 abort ();
13402 }
13403 }
13404 else
13405 gp_found = 0;
13406 }
13407 /* end mips */
13408
13409 for (parent = reloc_vector; *parent != NULL; parent++)
13410 {
13411 char *error_message = NULL;
13412 asymbol *symbol;
13413 bfd_reloc_status_type r;
13414
13415 symbol = *(*parent)->sym_ptr_ptr;
13416 /* PR ld/19628: A specially crafted input file
13417 can result in a NULL symbol pointer here. */
13418 if (symbol == NULL)
13419 {
13420 link_info->callbacks->einfo
13421 /* xgettext:c-format */
13422 (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13423 abfd, input_section, (* parent)->address);
13424 goto error_return;
13425 }
13426
13427 /* Zap reloc field when the symbol is from a discarded
13428 section, ignoring any addend. Do the same when called
13429 from bfd_simple_get_relocated_section_contents for
13430 undefined symbols in debug sections. This is to keep
13431 debug info reasonably sane, in particular so that
13432 DW_FORM_ref_addr to another file's .debug_info isn't
13433 confused with an offset into the current file's
13434 .debug_info. */
13435 if ((symbol->section != NULL && discarded_section (symbol->section))
13436 || (symbol->section == bfd_und_section_ptr
13437 && (input_section->flags & SEC_DEBUGGING) != 0
13438 && link_info->input_bfds == link_info->output_bfd))
13439 {
13440 bfd_vma off;
13441 static reloc_howto_type none_howto
13442 = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13443 "unused", false, 0, 0, false);
13444
13445 off = ((*parent)->address
13446 * bfd_octets_per_byte (input_bfd, input_section));
13447 _bfd_clear_contents ((*parent)->howto, input_bfd,
13448 input_section, data, off);
13449 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13450 (*parent)->addend = 0;
13451 (*parent)->howto = &none_howto;
13452 r = bfd_reloc_ok;
13453 }
13454
13455 /* Specific to MIPS: Deal with relocation types that require
13456 knowing the gp of the output bfd. */
13457
13458 /* If we've managed to find the gp and have a special
13459 function for the relocation then go ahead, else default
13460 to the generic handling. */
13461 else if (gp_found
13462 && ((*parent)->howto->special_function
13463 == _bfd_mips_elf32_gprel16_reloc))
13464 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13465 input_section, relocatable,
13466 data, gp);
13467 else
13468 r = bfd_perform_relocation (input_bfd,
13469 *parent,
13470 data,
13471 input_section,
13472 relocatable ? abfd : NULL,
13473 &error_message);
13474
13475 if (relocatable)
13476 {
13477 asection *os = input_section->output_section;
13478
13479 /* A partial link, so keep the relocs. */
13480 os->orelocation[os->reloc_count] = *parent;
13481 os->reloc_count++;
13482 }
13483
13484 if (r != bfd_reloc_ok)
13485 {
13486 switch (r)
13487 {
13488 case bfd_reloc_undefined:
13489 (*link_info->callbacks->undefined_symbol)
13490 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13491 input_bfd, input_section, (*parent)->address, true);
13492 break;
13493 case bfd_reloc_dangerous:
13494 BFD_ASSERT (error_message != NULL);
13495 (*link_info->callbacks->reloc_dangerous)
13496 (link_info, error_message,
13497 input_bfd, input_section, (*parent)->address);
13498 break;
13499 case bfd_reloc_overflow:
13500 (*link_info->callbacks->reloc_overflow)
13501 (link_info, NULL,
13502 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13503 (*parent)->howto->name, (*parent)->addend,
13504 input_bfd, input_section, (*parent)->address);
13505 break;
13506 case bfd_reloc_outofrange:
13507 /* PR ld/13730:
13508 This error can result when processing some partially
13509 complete binaries. Do not abort, but issue an error
13510 message instead. */
13511 link_info->callbacks->einfo
13512 /* xgettext:c-format */
13513 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13514 abfd, input_section, * parent);
13515 goto error_return;
13516
13517 case bfd_reloc_notsupported:
13518 /* PR ld/17512
13519 This error can result when processing a corrupt binary.
13520 Do not abort. Issue an error message instead. */
13521 link_info->callbacks->einfo
13522 /* xgettext:c-format */
13523 (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13524 abfd, input_section, * parent);
13525 goto error_return;
13526
13527 default:
13528 /* PR 17512; file: 90c2a92e.
13529 Report unexpected results, without aborting. */
13530 link_info->callbacks->einfo
13531 /* xgettext:c-format */
13532 (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13533 abfd, input_section, * parent, r);
13534 break;
13535 }
13536
13537 }
13538 }
13539 }
13540
13541 out:
13542 free (reloc_vector);
13543 return data;
13544 }
13545 \f
13546 static bool
13547 mips_elf_relax_delete_bytes (bfd *abfd,
13548 asection *sec, bfd_vma addr, int count)
13549 {
13550 Elf_Internal_Shdr *symtab_hdr;
13551 unsigned int sec_shndx;
13552 bfd_byte *contents;
13553 Elf_Internal_Rela *irel, *irelend;
13554 Elf_Internal_Sym *isym;
13555 Elf_Internal_Sym *isymend;
13556 struct elf_link_hash_entry **sym_hashes;
13557 struct elf_link_hash_entry **end_hashes;
13558 struct elf_link_hash_entry **start_hashes;
13559 unsigned int symcount;
13560
13561 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13562 contents = elf_section_data (sec)->this_hdr.contents;
13563
13564 irel = elf_section_data (sec)->relocs;
13565 irelend = irel + sec->reloc_count;
13566
13567 /* Actually delete the bytes. */
13568 memmove (contents + addr, contents + addr + count,
13569 (size_t) (sec->size - addr - count));
13570 sec->size -= count;
13571
13572 /* Adjust all the relocs. */
13573 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13574 {
13575 /* Get the new reloc address. */
13576 if (irel->r_offset > addr)
13577 irel->r_offset -= count;
13578 }
13579
13580 BFD_ASSERT (addr % 2 == 0);
13581 BFD_ASSERT (count % 2 == 0);
13582
13583 /* Adjust the local symbols defined in this section. */
13584 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13585 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13586 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13587 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13588 isym->st_value -= count;
13589
13590 /* Now adjust the global symbols defined in this section. */
13591 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13592 - symtab_hdr->sh_info);
13593 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13594 end_hashes = sym_hashes + symcount;
13595
13596 for (; sym_hashes < end_hashes; sym_hashes++)
13597 {
13598 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13599
13600 if ((sym_hash->root.type == bfd_link_hash_defined
13601 || sym_hash->root.type == bfd_link_hash_defweak)
13602 && sym_hash->root.u.def.section == sec)
13603 {
13604 bfd_vma value = sym_hash->root.u.def.value;
13605
13606 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13607 value &= MINUS_TWO;
13608 if (value > addr)
13609 sym_hash->root.u.def.value -= count;
13610 }
13611 }
13612
13613 return true;
13614 }
13615
13616
13617 /* Opcodes needed for microMIPS relaxation as found in
13618 opcodes/micromips-opc.c. */
13619
13620 struct opcode_descriptor {
13621 unsigned long match;
13622 unsigned long mask;
13623 };
13624
13625 /* The $ra register aka $31. */
13626
13627 #define RA 31
13628
13629 /* 32-bit instruction format register fields. */
13630
13631 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13632 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13633
13634 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
13635
13636 #define OP16_VALID_REG(r) \
13637 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13638
13639
13640 /* 32-bit and 16-bit branches. */
13641
13642 static const struct opcode_descriptor b_insns_32[] = {
13643 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13644 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13645 { 0, 0 } /* End marker for find_match(). */
13646 };
13647
13648 static const struct opcode_descriptor bc_insn_32 =
13649 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13650
13651 static const struct opcode_descriptor bz_insn_32 =
13652 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13653
13654 static const struct opcode_descriptor bzal_insn_32 =
13655 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13656
13657 static const struct opcode_descriptor beq_insn_32 =
13658 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13659
13660 static const struct opcode_descriptor b_insn_16 =
13661 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13662
13663 static const struct opcode_descriptor bz_insn_16 =
13664 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
13665
13666
13667 /* 32-bit and 16-bit branch EQ and NE zero. */
13668
13669 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13670 eq and second the ne. This convention is used when replacing a
13671 32-bit BEQ/BNE with the 16-bit version. */
13672
13673 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13674
13675 static const struct opcode_descriptor bz_rs_insns_32[] = {
13676 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13677 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13678 { 0, 0 } /* End marker for find_match(). */
13679 };
13680
13681 static const struct opcode_descriptor bz_rt_insns_32[] = {
13682 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13683 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13684 { 0, 0 } /* End marker for find_match(). */
13685 };
13686
13687 static const struct opcode_descriptor bzc_insns_32[] = {
13688 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13689 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13690 { 0, 0 } /* End marker for find_match(). */
13691 };
13692
13693 static const struct opcode_descriptor bz_insns_16[] = {
13694 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13695 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13696 { 0, 0 } /* End marker for find_match(). */
13697 };
13698
13699 /* Switch between a 5-bit register index and its 3-bit shorthand. */
13700
13701 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13702 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13703
13704
13705 /* 32-bit instructions with a delay slot. */
13706
13707 static const struct opcode_descriptor jal_insn_32_bd16 =
13708 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13709
13710 static const struct opcode_descriptor jal_insn_32_bd32 =
13711 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13712
13713 static const struct opcode_descriptor jal_x_insn_32_bd32 =
13714 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13715
13716 static const struct opcode_descriptor j_insn_32 =
13717 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13718
13719 static const struct opcode_descriptor jalr_insn_32 =
13720 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13721
13722 /* This table can be compacted, because no opcode replacement is made. */
13723
13724 static const struct opcode_descriptor ds_insns_32_bd16[] = {
13725 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13726
13727 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13728 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13729
13730 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13731 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13732 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13733 { 0, 0 } /* End marker for find_match(). */
13734 };
13735
13736 /* This table can be compacted, because no opcode replacement is made. */
13737
13738 static const struct opcode_descriptor ds_insns_32_bd32[] = {
13739 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13740
13741 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13742 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13743 { 0, 0 } /* End marker for find_match(). */
13744 };
13745
13746
13747 /* 16-bit instructions with a delay slot. */
13748
13749 static const struct opcode_descriptor jalr_insn_16_bd16 =
13750 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13751
13752 static const struct opcode_descriptor jalr_insn_16_bd32 =
13753 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13754
13755 static const struct opcode_descriptor jr_insn_16 =
13756 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13757
13758 #define JR16_REG(opcode) ((opcode) & 0x1f)
13759
13760 /* This table can be compacted, because no opcode replacement is made. */
13761
13762 static const struct opcode_descriptor ds_insns_16_bd16[] = {
13763 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13764
13765 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13766 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13767 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13768 { 0, 0 } /* End marker for find_match(). */
13769 };
13770
13771
13772 /* LUI instruction. */
13773
13774 static const struct opcode_descriptor lui_insn =
13775 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13776
13777
13778 /* ADDIU instruction. */
13779
13780 static const struct opcode_descriptor addiu_insn =
13781 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13782
13783 static const struct opcode_descriptor addiupc_insn =
13784 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13785
13786 #define ADDIUPC_REG_FIELD(r) \
13787 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13788
13789
13790 /* Relaxable instructions in a JAL delay slot: MOVE. */
13791
13792 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13793 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13794 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13795 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13796
13797 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13798 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13799
13800 static const struct opcode_descriptor move_insns_32[] = {
13801 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
13802 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13803 { 0, 0 } /* End marker for find_match(). */
13804 };
13805
13806 static const struct opcode_descriptor move_insn_16 =
13807 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13808
13809
13810 /* NOP instructions. */
13811
13812 static const struct opcode_descriptor nop_insn_32 =
13813 { /* "nop", "", */ 0x00000000, 0xffffffff };
13814
13815 static const struct opcode_descriptor nop_insn_16 =
13816 { /* "nop", "", */ 0x0c00, 0xffff };
13817
13818
13819 /* Instruction match support. */
13820
13821 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13822
13823 static int
13824 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13825 {
13826 unsigned long indx;
13827
13828 for (indx = 0; insn[indx].mask != 0; indx++)
13829 if (MATCH (opcode, insn[indx]))
13830 return indx;
13831
13832 return -1;
13833 }
13834
13835
13836 /* Branch and delay slot decoding support. */
13837
13838 /* If PTR points to what *might* be a 16-bit branch or jump, then
13839 return the minimum length of its delay slot, otherwise return 0.
13840 Non-zero results are not definitive as we might be checking against
13841 the second half of another instruction. */
13842
13843 static int
13844 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13845 {
13846 unsigned long opcode;
13847 int bdsize;
13848
13849 opcode = bfd_get_16 (abfd, ptr);
13850 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13851 /* 16-bit branch/jump with a 32-bit delay slot. */
13852 bdsize = 4;
13853 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13854 || find_match (opcode, ds_insns_16_bd16) >= 0)
13855 /* 16-bit branch/jump with a 16-bit delay slot. */
13856 bdsize = 2;
13857 else
13858 /* No delay slot. */
13859 bdsize = 0;
13860
13861 return bdsize;
13862 }
13863
13864 /* If PTR points to what *might* be a 32-bit branch or jump, then
13865 return the minimum length of its delay slot, otherwise return 0.
13866 Non-zero results are not definitive as we might be checking against
13867 the second half of another instruction. */
13868
13869 static int
13870 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13871 {
13872 unsigned long opcode;
13873 int bdsize;
13874
13875 opcode = bfd_get_micromips_32 (abfd, ptr);
13876 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13877 /* 32-bit branch/jump with a 32-bit delay slot. */
13878 bdsize = 4;
13879 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13880 /* 32-bit branch/jump with a 16-bit delay slot. */
13881 bdsize = 2;
13882 else
13883 /* No delay slot. */
13884 bdsize = 0;
13885
13886 return bdsize;
13887 }
13888
13889 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13890 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13891
13892 static bool
13893 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13894 {
13895 unsigned long opcode;
13896
13897 opcode = bfd_get_16 (abfd, ptr);
13898 if (MATCH (opcode, b_insn_16)
13899 /* B16 */
13900 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13901 /* JR16 */
13902 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13903 /* BEQZ16, BNEZ16 */
13904 || (MATCH (opcode, jalr_insn_16_bd32)
13905 /* JALR16 */
13906 && reg != JR16_REG (opcode) && reg != RA))
13907 return true;
13908
13909 return false;
13910 }
13911
13912 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13913 then return TRUE, otherwise FALSE. */
13914
13915 static bool
13916 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13917 {
13918 unsigned long opcode;
13919
13920 opcode = bfd_get_micromips_32 (abfd, ptr);
13921 if (MATCH (opcode, j_insn_32)
13922 /* J */
13923 || MATCH (opcode, bc_insn_32)
13924 /* BC1F, BC1T, BC2F, BC2T */
13925 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13926 /* JAL, JALX */
13927 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13928 /* BGEZ, BGTZ, BLEZ, BLTZ */
13929 || (MATCH (opcode, bzal_insn_32)
13930 /* BGEZAL, BLTZAL */
13931 && reg != OP32_SREG (opcode) && reg != RA)
13932 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13933 /* JALR, JALR.HB, BEQ, BNE */
13934 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13935 return true;
13936
13937 return false;
13938 }
13939
13940 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13941 IRELEND) at OFFSET indicate that there must be a compact branch there,
13942 then return TRUE, otherwise FALSE. */
13943
13944 static bool
13945 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13946 const Elf_Internal_Rela *internal_relocs,
13947 const Elf_Internal_Rela *irelend)
13948 {
13949 const Elf_Internal_Rela *irel;
13950 unsigned long opcode;
13951
13952 opcode = bfd_get_micromips_32 (abfd, ptr);
13953 if (find_match (opcode, bzc_insns_32) < 0)
13954 return false;
13955
13956 for (irel = internal_relocs; irel < irelend; irel++)
13957 if (irel->r_offset == offset
13958 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13959 return true;
13960
13961 return false;
13962 }
13963
13964 /* Bitsize checking. */
13965 #define IS_BITSIZE(val, N) \
13966 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13967 - (1ULL << ((N) - 1))) == (val))
13968
13969 \f
13970 bool
13971 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13972 struct bfd_link_info *link_info,
13973 bool *again)
13974 {
13975 bool insn32 = mips_elf_hash_table (link_info)->insn32;
13976 Elf_Internal_Shdr *symtab_hdr;
13977 Elf_Internal_Rela *internal_relocs;
13978 Elf_Internal_Rela *irel, *irelend;
13979 bfd_byte *contents = NULL;
13980 Elf_Internal_Sym *isymbuf = NULL;
13981
13982 /* Assume nothing changes. */
13983 *again = false;
13984
13985 /* We don't have to do anything for a relocatable link, if
13986 this section does not have relocs, or if this is not a
13987 code section. */
13988
13989 if (bfd_link_relocatable (link_info)
13990 || sec->reloc_count == 0
13991 || (sec->flags & SEC_RELOC) == 0
13992 || (sec->flags & SEC_HAS_CONTENTS) == 0
13993 || (sec->flags & SEC_CODE) == 0)
13994 return true;
13995
13996 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13997
13998 /* Get a copy of the native relocations. */
13999 internal_relocs = (_bfd_elf_link_read_relocs
14000 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
14001 link_info->keep_memory));
14002 if (internal_relocs == NULL)
14003 goto error_return;
14004
14005 /* Walk through them looking for relaxing opportunities. */
14006 irelend = internal_relocs + sec->reloc_count;
14007 for (irel = internal_relocs; irel < irelend; irel++)
14008 {
14009 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
14010 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
14011 bool target_is_micromips_code_p;
14012 unsigned long opcode;
14013 bfd_vma symval;
14014 bfd_vma pcrval;
14015 bfd_byte *ptr;
14016 int fndopc;
14017
14018 /* The number of bytes to delete for relaxation and from where
14019 to delete these bytes starting at irel->r_offset. */
14020 int delcnt = 0;
14021 int deloff = 0;
14022
14023 /* If this isn't something that can be relaxed, then ignore
14024 this reloc. */
14025 if (r_type != R_MICROMIPS_HI16
14026 && r_type != R_MICROMIPS_PC16_S1
14027 && r_type != R_MICROMIPS_26_S1)
14028 continue;
14029
14030 /* Get the section contents if we haven't done so already. */
14031 if (contents == NULL)
14032 {
14033 /* Get cached copy if it exists. */
14034 if (elf_section_data (sec)->this_hdr.contents != NULL)
14035 contents = elf_section_data (sec)->this_hdr.contents;
14036 /* Go get them off disk. */
14037 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14038 goto error_return;
14039 }
14040 ptr = contents + irel->r_offset;
14041
14042 /* Read this BFD's local symbols if we haven't done so already. */
14043 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14044 {
14045 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14046 if (isymbuf == NULL)
14047 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14048 symtab_hdr->sh_info, 0,
14049 NULL, NULL, NULL);
14050 if (isymbuf == NULL)
14051 goto error_return;
14052 }
14053
14054 /* Get the value of the symbol referred to by the reloc. */
14055 if (r_symndx < symtab_hdr->sh_info)
14056 {
14057 /* A local symbol. */
14058 Elf_Internal_Sym *isym;
14059 asection *sym_sec;
14060
14061 isym = isymbuf + r_symndx;
14062 if (isym->st_shndx == SHN_UNDEF)
14063 sym_sec = bfd_und_section_ptr;
14064 else if (isym->st_shndx == SHN_ABS)
14065 sym_sec = bfd_abs_section_ptr;
14066 else if (isym->st_shndx == SHN_COMMON)
14067 sym_sec = bfd_com_section_ptr;
14068 else
14069 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14070 symval = (isym->st_value
14071 + sym_sec->output_section->vma
14072 + sym_sec->output_offset);
14073 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14074 }
14075 else
14076 {
14077 unsigned long indx;
14078 struct elf_link_hash_entry *h;
14079
14080 /* An external symbol. */
14081 indx = r_symndx - symtab_hdr->sh_info;
14082 h = elf_sym_hashes (abfd)[indx];
14083 BFD_ASSERT (h != NULL);
14084
14085 if (h->root.type != bfd_link_hash_defined
14086 && h->root.type != bfd_link_hash_defweak)
14087 /* This appears to be a reference to an undefined
14088 symbol. Just ignore it -- it will be caught by the
14089 regular reloc processing. */
14090 continue;
14091
14092 symval = (h->root.u.def.value
14093 + h->root.u.def.section->output_section->vma
14094 + h->root.u.def.section->output_offset);
14095 target_is_micromips_code_p = (!h->needs_plt
14096 && ELF_ST_IS_MICROMIPS (h->other));
14097 }
14098
14099
14100 /* For simplicity of coding, we are going to modify the
14101 section contents, the section relocs, and the BFD symbol
14102 table. We must tell the rest of the code not to free up this
14103 information. It would be possible to instead create a table
14104 of changes which have to be made, as is done in coff-mips.c;
14105 that would be more work, but would require less memory when
14106 the linker is run. */
14107
14108 /* Only 32-bit instructions relaxed. */
14109 if (irel->r_offset + 4 > sec->size)
14110 continue;
14111
14112 opcode = bfd_get_micromips_32 (abfd, ptr);
14113
14114 /* This is the pc-relative distance from the instruction the
14115 relocation is applied to, to the symbol referred. */
14116 pcrval = (symval
14117 - (sec->output_section->vma + sec->output_offset)
14118 - irel->r_offset);
14119
14120 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14121 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14122 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
14123
14124 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14125
14126 where pcrval has first to be adjusted to apply against the LO16
14127 location (we make the adjustment later on, when we have figured
14128 out the offset). */
14129 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14130 {
14131 bool bzc = false;
14132 unsigned long nextopc;
14133 unsigned long reg;
14134 bfd_vma offset;
14135
14136 /* Give up if the previous reloc was a HI16 against this symbol
14137 too. */
14138 if (irel > internal_relocs
14139 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14140 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14141 continue;
14142
14143 /* Or if the next reloc is not a LO16 against this symbol. */
14144 if (irel + 1 >= irelend
14145 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14146 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14147 continue;
14148
14149 /* Or if the second next reloc is a LO16 against this symbol too. */
14150 if (irel + 2 >= irelend
14151 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14152 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14153 continue;
14154
14155 /* See if the LUI instruction *might* be in a branch delay slot.
14156 We check whether what looks like a 16-bit branch or jump is
14157 actually an immediate argument to a compact branch, and let
14158 it through if so. */
14159 if (irel->r_offset >= 2
14160 && check_br16_dslot (abfd, ptr - 2)
14161 && !(irel->r_offset >= 4
14162 && (bzc = check_relocated_bzc (abfd,
14163 ptr - 4, irel->r_offset - 4,
14164 internal_relocs, irelend))))
14165 continue;
14166 if (irel->r_offset >= 4
14167 && !bzc
14168 && check_br32_dslot (abfd, ptr - 4))
14169 continue;
14170
14171 reg = OP32_SREG (opcode);
14172
14173 /* We only relax adjacent instructions or ones separated with
14174 a branch or jump that has a delay slot. The branch or jump
14175 must not fiddle with the register used to hold the address.
14176 Subtract 4 for the LUI itself. */
14177 offset = irel[1].r_offset - irel[0].r_offset;
14178 switch (offset - 4)
14179 {
14180 case 0:
14181 break;
14182 case 2:
14183 if (check_br16 (abfd, ptr + 4, reg))
14184 break;
14185 continue;
14186 case 4:
14187 if (check_br32 (abfd, ptr + 4, reg))
14188 break;
14189 continue;
14190 default:
14191 continue;
14192 }
14193
14194 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14195
14196 /* Give up unless the same register is used with both
14197 relocations. */
14198 if (OP32_SREG (nextopc) != reg)
14199 continue;
14200
14201 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14202 and rounding up to take masking of the two LSBs into account. */
14203 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14204
14205 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
14206 if (IS_BITSIZE (symval, 16))
14207 {
14208 /* Fix the relocation's type. */
14209 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14210
14211 /* Instructions using R_MICROMIPS_LO16 have the base or
14212 source register in bits 20:16. This register becomes $0
14213 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
14214 nextopc &= ~0x001f0000;
14215 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14216 contents + irel[1].r_offset);
14217 }
14218
14219 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14220 We add 4 to take LUI deletion into account while checking
14221 the PC-relative distance. */
14222 else if (symval % 4 == 0
14223 && IS_BITSIZE (pcrval + 4, 25)
14224 && MATCH (nextopc, addiu_insn)
14225 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14226 && OP16_VALID_REG (OP32_TREG (nextopc)))
14227 {
14228 /* Fix the relocation's type. */
14229 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14230
14231 /* Replace ADDIU with the ADDIUPC version. */
14232 nextopc = (addiupc_insn.match
14233 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14234
14235 bfd_put_micromips_32 (abfd, nextopc,
14236 contents + irel[1].r_offset);
14237 }
14238
14239 /* Can't do anything, give up, sigh... */
14240 else
14241 continue;
14242
14243 /* Fix the relocation's type. */
14244 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14245
14246 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
14247 delcnt = 4;
14248 deloff = 0;
14249 }
14250
14251 /* Compact branch relaxation -- due to the multitude of macros
14252 employed by the compiler/assembler, compact branches are not
14253 always generated. Obviously, this can/will be fixed elsewhere,
14254 but there is no drawback in double checking it here. */
14255 else if (r_type == R_MICROMIPS_PC16_S1
14256 && irel->r_offset + 5 < sec->size
14257 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14258 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14259 && ((!insn32
14260 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14261 nop_insn_16) ? 2 : 0))
14262 || (irel->r_offset + 7 < sec->size
14263 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14264 ptr + 4),
14265 nop_insn_32) ? 4 : 0))))
14266 {
14267 unsigned long reg;
14268
14269 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14270
14271 /* Replace BEQZ/BNEZ with the compact version. */
14272 opcode = (bzc_insns_32[fndopc].match
14273 | BZC32_REG_FIELD (reg)
14274 | (opcode & 0xffff)); /* Addend value. */
14275
14276 bfd_put_micromips_32 (abfd, opcode, ptr);
14277
14278 /* Delete the delay slot NOP: two or four bytes from
14279 irel->offset + 4; delcnt has already been set above. */
14280 deloff = 4;
14281 }
14282
14283 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
14284 to check the distance from the next instruction, so subtract 2. */
14285 else if (!insn32
14286 && r_type == R_MICROMIPS_PC16_S1
14287 && IS_BITSIZE (pcrval - 2, 11)
14288 && find_match (opcode, b_insns_32) >= 0)
14289 {
14290 /* Fix the relocation's type. */
14291 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14292
14293 /* Replace the 32-bit opcode with a 16-bit opcode. */
14294 bfd_put_16 (abfd,
14295 (b_insn_16.match
14296 | (opcode & 0x3ff)), /* Addend value. */
14297 ptr);
14298
14299 /* Delete 2 bytes from irel->r_offset + 2. */
14300 delcnt = 2;
14301 deloff = 2;
14302 }
14303
14304 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
14305 to check the distance from the next instruction, so subtract 2. */
14306 else if (!insn32
14307 && r_type == R_MICROMIPS_PC16_S1
14308 && IS_BITSIZE (pcrval - 2, 8)
14309 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14310 && OP16_VALID_REG (OP32_SREG (opcode)))
14311 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14312 && OP16_VALID_REG (OP32_TREG (opcode)))))
14313 {
14314 unsigned long reg;
14315
14316 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14317
14318 /* Fix the relocation's type. */
14319 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14320
14321 /* Replace the 32-bit opcode with a 16-bit opcode. */
14322 bfd_put_16 (abfd,
14323 (bz_insns_16[fndopc].match
14324 | BZ16_REG_FIELD (reg)
14325 | (opcode & 0x7f)), /* Addend value. */
14326 ptr);
14327
14328 /* Delete 2 bytes from irel->r_offset + 2. */
14329 delcnt = 2;
14330 deloff = 2;
14331 }
14332
14333 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
14334 else if (!insn32
14335 && r_type == R_MICROMIPS_26_S1
14336 && target_is_micromips_code_p
14337 && irel->r_offset + 7 < sec->size
14338 && MATCH (opcode, jal_insn_32_bd32))
14339 {
14340 unsigned long n32opc;
14341 bool relaxed = false;
14342
14343 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14344
14345 if (MATCH (n32opc, nop_insn_32))
14346 {
14347 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
14348 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14349
14350 relaxed = true;
14351 }
14352 else if (find_match (n32opc, move_insns_32) >= 0)
14353 {
14354 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14355 bfd_put_16 (abfd,
14356 (move_insn_16.match
14357 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14358 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14359 ptr + 4);
14360
14361 relaxed = true;
14362 }
14363 /* Other 32-bit instructions relaxable to 16-bit
14364 instructions will be handled here later. */
14365
14366 if (relaxed)
14367 {
14368 /* JAL with 32-bit delay slot that is changed to a JALS
14369 with 16-bit delay slot. */
14370 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14371
14372 /* Delete 2 bytes from irel->r_offset + 6. */
14373 delcnt = 2;
14374 deloff = 6;
14375 }
14376 }
14377
14378 if (delcnt != 0)
14379 {
14380 /* Note that we've changed the relocs, section contents, etc. */
14381 elf_section_data (sec)->relocs = internal_relocs;
14382 elf_section_data (sec)->this_hdr.contents = contents;
14383 symtab_hdr->contents = (unsigned char *) isymbuf;
14384
14385 /* Delete bytes depending on the delcnt and deloff. */
14386 if (!mips_elf_relax_delete_bytes (abfd, sec,
14387 irel->r_offset + deloff, delcnt))
14388 goto error_return;
14389
14390 /* That will change things, so we should relax again.
14391 Note that this is not required, and it may be slow. */
14392 *again = true;
14393 }
14394 }
14395
14396 if (isymbuf != NULL
14397 && symtab_hdr->contents != (unsigned char *) isymbuf)
14398 {
14399 if (! link_info->keep_memory)
14400 free (isymbuf);
14401 else
14402 {
14403 /* Cache the symbols for elf_link_input_bfd. */
14404 symtab_hdr->contents = (unsigned char *) isymbuf;
14405 }
14406 }
14407
14408 if (contents != NULL
14409 && elf_section_data (sec)->this_hdr.contents != contents)
14410 {
14411 if (! link_info->keep_memory)
14412 free (contents);
14413 else
14414 {
14415 /* Cache the section contents for elf_link_input_bfd. */
14416 elf_section_data (sec)->this_hdr.contents = contents;
14417 }
14418 }
14419
14420 if (elf_section_data (sec)->relocs != internal_relocs)
14421 free (internal_relocs);
14422
14423 return true;
14424
14425 error_return:
14426 if (symtab_hdr->contents != (unsigned char *) isymbuf)
14427 free (isymbuf);
14428 if (elf_section_data (sec)->this_hdr.contents != contents)
14429 free (contents);
14430 if (elf_section_data (sec)->relocs != internal_relocs)
14431 free (internal_relocs);
14432
14433 return false;
14434 }
14435 \f
14436 /* Create a MIPS ELF linker hash table. */
14437
14438 struct bfd_link_hash_table *
14439 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
14440 {
14441 struct mips_elf_link_hash_table *ret;
14442 size_t amt = sizeof (struct mips_elf_link_hash_table);
14443
14444 ret = bfd_zmalloc (amt);
14445 if (ret == NULL)
14446 return NULL;
14447
14448 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14449 mips_elf_link_hash_newfunc,
14450 sizeof (struct mips_elf_link_hash_entry),
14451 MIPS_ELF_DATA))
14452 {
14453 free (ret);
14454 return NULL;
14455 }
14456 ret->root.init_plt_refcount.plist = NULL;
14457 ret->root.init_plt_offset.plist = NULL;
14458
14459 return &ret->root.root;
14460 }
14461
14462 /* Likewise, but indicate that the target is VxWorks. */
14463
14464 struct bfd_link_hash_table *
14465 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14466 {
14467 struct bfd_link_hash_table *ret;
14468
14469 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14470 if (ret)
14471 {
14472 struct mips_elf_link_hash_table *htab;
14473
14474 htab = (struct mips_elf_link_hash_table *) ret;
14475 htab->use_plts_and_copy_relocs = true;
14476 }
14477 return ret;
14478 }
14479
14480 /* A function that the linker calls if we are allowed to use PLTs
14481 and copy relocs. */
14482
14483 void
14484 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14485 {
14486 mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14487 }
14488
14489 /* A function that the linker calls to select between all or only
14490 32-bit microMIPS instructions, and between making or ignoring
14491 branch relocation checks for invalid transitions between ISA modes.
14492 Also record whether we have been configured for a GNU target. */
14493
14494 void
14495 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14496 bool ignore_branch_isa,
14497 bool gnu_target)
14498 {
14499 mips_elf_hash_table (info)->insn32 = insn32;
14500 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14501 mips_elf_hash_table (info)->gnu_target = gnu_target;
14502 }
14503
14504 /* A function that the linker calls to enable use of compact branches in
14505 linker generated code for MIPSR6. */
14506
14507 void
14508 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14509 {
14510 mips_elf_hash_table (info)->compact_branches = on;
14511 }
14512
14513 \f
14514 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14515
14516 struct mips_mach_extension
14517 {
14518 unsigned long extension, base;
14519 };
14520
14521 /* An array that maps 64-bit architectures to the corresponding 32-bit
14522 architectures. */
14523 static const struct mips_mach_extension mips_mach_32_64[] =
14524 {
14525 { bfd_mach_mipsisa64r6, bfd_mach_mipsisa32r6 },
14526 { bfd_mach_mipsisa64r5, bfd_mach_mipsisa32r5 },
14527 { bfd_mach_mipsisa64r3, bfd_mach_mipsisa32r3 },
14528 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa32r2 },
14529 { bfd_mach_mipsisa64, bfd_mach_mipsisa32 }
14530 };
14531
14532 /* An array describing how BFD machines relate to one another. The entries
14533 are ordered topologically with MIPS I extensions listed last. */
14534
14535 static const struct mips_mach_extension mips_mach_extensions[] =
14536 {
14537 /* MIPS64r2 extensions. */
14538 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14539 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14540 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14541 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14542 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14543 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14544 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14545
14546 /* MIPS64 extensions. */
14547 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14548 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14549 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14550
14551 /* MIPS V extensions. */
14552 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14553
14554 /* R10000 extensions. */
14555 { bfd_mach_mips12000, bfd_mach_mips10000 },
14556 { bfd_mach_mips14000, bfd_mach_mips10000 },
14557 { bfd_mach_mips16000, bfd_mach_mips10000 },
14558
14559 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14560 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14561 better to allow vr5400 and vr5500 code to be merged anyway, since
14562 many libraries will just use the core ISA. Perhaps we could add
14563 some sort of ASE flag if this ever proves a problem. */
14564 { bfd_mach_mips5500, bfd_mach_mips5400 },
14565 { bfd_mach_mips5400, bfd_mach_mips5000 },
14566
14567 /* MIPS IV extensions. */
14568 { bfd_mach_mips5, bfd_mach_mips8000 },
14569 { bfd_mach_mips10000, bfd_mach_mips8000 },
14570 { bfd_mach_mips5000, bfd_mach_mips8000 },
14571 { bfd_mach_mips7000, bfd_mach_mips8000 },
14572 { bfd_mach_mips9000, bfd_mach_mips8000 },
14573
14574 /* VR4100 extensions. */
14575 { bfd_mach_mips4120, bfd_mach_mips4100 },
14576 { bfd_mach_mips4111, bfd_mach_mips4100 },
14577
14578 /* MIPS III extensions. */
14579 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14580 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14581 { bfd_mach_mips8000, bfd_mach_mips4000 },
14582 { bfd_mach_mips4650, bfd_mach_mips4000 },
14583 { bfd_mach_mips4600, bfd_mach_mips4000 },
14584 { bfd_mach_mips4400, bfd_mach_mips4000 },
14585 { bfd_mach_mips4300, bfd_mach_mips4000 },
14586 { bfd_mach_mips4100, bfd_mach_mips4000 },
14587 { bfd_mach_mips5900, bfd_mach_mips4000 },
14588
14589 /* MIPS32r3 extensions. */
14590 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14591
14592 /* MIPS32r2 extensions. */
14593 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14594
14595 /* MIPS32 extensions. */
14596 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14597
14598 /* MIPS II extensions. */
14599 { bfd_mach_mips4000, bfd_mach_mips6000 },
14600 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14601 { bfd_mach_mips4010, bfd_mach_mips6000 },
14602 { bfd_mach_mips_allegrex, bfd_mach_mips6000 },
14603
14604 /* MIPS I extensions. */
14605 { bfd_mach_mips6000, bfd_mach_mips3000 },
14606 { bfd_mach_mips3900, bfd_mach_mips3000 }
14607 };
14608
14609 /* Return true if bfd machine EXTENSION is the same as BASE, or if
14610 EXTENSION is the 64-bit equivalent of a 32-bit BASE. */
14611
14612 static bool
14613 mips_mach_extends_32_64 (unsigned long base, unsigned long extension)
14614 {
14615 size_t i;
14616
14617 if (extension == base)
14618 return true;
14619
14620 for (i = 0; i < ARRAY_SIZE (mips_mach_32_64); i++)
14621 if (extension == mips_mach_32_64[i].extension)
14622 return base == mips_mach_32_64[i].base;
14623
14624 return false;
14625 }
14626
14627 static bool
14628 mips_mach_extends_p (unsigned long base, unsigned long extension)
14629 {
14630 size_t i;
14631
14632 if (mips_mach_extends_32_64 (base, extension))
14633 return true;
14634
14635 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14636 if (extension == mips_mach_extensions[i].extension)
14637 {
14638 extension = mips_mach_extensions[i].base;
14639 if (mips_mach_extends_32_64 (base, extension))
14640 return true;
14641 }
14642
14643 return false;
14644 }
14645
14646 /* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14647
14648 static unsigned long
14649 bfd_mips_isa_ext_mach (unsigned int isa_ext)
14650 {
14651 switch (isa_ext)
14652 {
14653 case AFL_EXT_3900: return bfd_mach_mips3900;
14654 case AFL_EXT_4010: return bfd_mach_mips4010;
14655 case AFL_EXT_4100: return bfd_mach_mips4100;
14656 case AFL_EXT_4111: return bfd_mach_mips4111;
14657 case AFL_EXT_4120: return bfd_mach_mips4120;
14658 case AFL_EXT_4650: return bfd_mach_mips4650;
14659 case AFL_EXT_5400: return bfd_mach_mips5400;
14660 case AFL_EXT_5500: return bfd_mach_mips5500;
14661 case AFL_EXT_5900: return bfd_mach_mips5900;
14662 case AFL_EXT_10000: return bfd_mach_mips10000;
14663 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14664 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14665 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
14666 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14667 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14668 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
14669 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14670 default: return bfd_mach_mips3000;
14671 }
14672 }
14673
14674 /* Return the .MIPS.abiflags value representing each ISA Extension. */
14675
14676 unsigned int
14677 bfd_mips_isa_ext (bfd *abfd)
14678 {
14679 switch (bfd_get_mach (abfd))
14680 {
14681 case bfd_mach_mips3900: return AFL_EXT_3900;
14682 case bfd_mach_mips4010: return AFL_EXT_4010;
14683 case bfd_mach_mips4100: return AFL_EXT_4100;
14684 case bfd_mach_mips4111: return AFL_EXT_4111;
14685 case bfd_mach_mips4120: return AFL_EXT_4120;
14686 case bfd_mach_mips4650: return AFL_EXT_4650;
14687 case bfd_mach_mips5400: return AFL_EXT_5400;
14688 case bfd_mach_mips5500: return AFL_EXT_5500;
14689 case bfd_mach_mips5900: return AFL_EXT_5900;
14690 case bfd_mach_mips10000: return AFL_EXT_10000;
14691 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14692 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14693 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14694 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14695 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14696 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14697 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14698 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
14699 case bfd_mach_mips_interaptiv_mr2:
14700 return AFL_EXT_INTERAPTIV_MR2;
14701 default: return 0;
14702 }
14703 }
14704
14705 /* Encode ISA level and revision as a single value. */
14706 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14707
14708 /* Decode a single value into level and revision. */
14709 #define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14710 #define ISA_REV(LEVREV) ((LEVREV) & 0x7)
14711
14712 /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14713
14714 static void
14715 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14716 {
14717 int new_isa = 0;
14718 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14719 {
14720 case E_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14721 case E_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14722 case E_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14723 case E_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14724 case E_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14725 case E_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14726 case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14727 case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14728 case E_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14729 case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14730 case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14731 default:
14732 _bfd_error_handler
14733 /* xgettext:c-format */
14734 (_("%pB: unknown architecture %s"),
14735 abfd, bfd_printable_name (abfd));
14736 }
14737
14738 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14739 {
14740 abiflags->isa_level = ISA_LEVEL (new_isa);
14741 abiflags->isa_rev = ISA_REV (new_isa);
14742 }
14743
14744 /* Update the isa_ext if ABFD describes a further extension. */
14745 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14746 bfd_get_mach (abfd)))
14747 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14748 }
14749
14750 /* Return true if the given ELF header flags describe a 32-bit binary. */
14751
14752 static bool
14753 mips_32bit_flags_p (flagword flags)
14754 {
14755 return ((flags & EF_MIPS_32BITMODE) != 0
14756 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14757 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14758 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14759 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14760 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14761 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14762 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
14763 }
14764
14765 /* Infer the content of the ABI flags based on the elf header. */
14766
14767 static void
14768 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14769 {
14770 obj_attribute *in_attr;
14771
14772 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14773 update_mips_abiflags_isa (abfd, abiflags);
14774
14775 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14776 abiflags->gpr_size = AFL_REG_32;
14777 else
14778 abiflags->gpr_size = AFL_REG_64;
14779
14780 abiflags->cpr1_size = AFL_REG_NONE;
14781
14782 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14783 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14784
14785 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14786 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14787 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14788 && abiflags->gpr_size == AFL_REG_32))
14789 abiflags->cpr1_size = AFL_REG_32;
14790 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14791 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14792 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14793 abiflags->cpr1_size = AFL_REG_64;
14794
14795 abiflags->cpr2_size = AFL_REG_NONE;
14796
14797 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14798 abiflags->ases |= AFL_ASE_MDMX;
14799 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14800 abiflags->ases |= AFL_ASE_MIPS16;
14801 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14802 abiflags->ases |= AFL_ASE_MICROMIPS;
14803
14804 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14805 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14806 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14807 && abiflags->isa_level >= 32
14808 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14809 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14810 }
14811
14812 /* We need to use a special link routine to handle the .reginfo and
14813 the .mdebug sections. We need to merge all instances of these
14814 sections together, not write them all out sequentially. */
14815
14816 bool
14817 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14818 {
14819 asection *o;
14820 struct bfd_link_order *p;
14821 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14822 asection *rtproc_sec, *abiflags_sec;
14823 Elf32_RegInfo reginfo;
14824 struct ecoff_debug_info debug;
14825 struct mips_htab_traverse_info hti;
14826 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14827 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14828 HDRR *symhdr = &debug.symbolic_header;
14829 void *mdebug_handle = NULL;
14830 asection *s;
14831 EXTR esym;
14832 unsigned int i;
14833 bfd_size_type amt;
14834 struct mips_elf_link_hash_table *htab;
14835
14836 static const char * const secname[] =
14837 {
14838 ".text", ".init", ".fini", ".data",
14839 ".rodata", ".sdata", ".sbss", ".bss"
14840 };
14841 static const int sc[] =
14842 {
14843 scText, scInit, scFini, scData,
14844 scRData, scSData, scSBss, scBss
14845 };
14846
14847 htab = mips_elf_hash_table (info);
14848 BFD_ASSERT (htab != NULL);
14849
14850 /* Sort the dynamic symbols so that those with GOT entries come after
14851 those without. */
14852 if (!mips_elf_sort_hash_table (abfd, info))
14853 return false;
14854
14855 /* Create any scheduled LA25 stubs. */
14856 hti.info = info;
14857 hti.output_bfd = abfd;
14858 hti.error = false;
14859 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14860 if (hti.error)
14861 return false;
14862
14863 /* Get a value for the GP register. */
14864 if (elf_gp (abfd) == 0)
14865 {
14866 struct bfd_link_hash_entry *h;
14867
14868 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14869 if (h != NULL && h->type == bfd_link_hash_defined)
14870 elf_gp (abfd) = (h->u.def.value
14871 + h->u.def.section->output_section->vma
14872 + h->u.def.section->output_offset);
14873 else if (htab->root.target_os == is_vxworks
14874 && (h = bfd_link_hash_lookup (info->hash,
14875 "_GLOBAL_OFFSET_TABLE_",
14876 false, false, true))
14877 && h->type == bfd_link_hash_defined)
14878 elf_gp (abfd) = (h->u.def.section->output_section->vma
14879 + h->u.def.section->output_offset
14880 + h->u.def.value);
14881 else if (bfd_link_relocatable (info))
14882 {
14883 bfd_vma lo = MINUS_ONE;
14884
14885 /* Find the GP-relative section with the lowest offset. */
14886 for (o = abfd->sections; o != NULL; o = o->next)
14887 if (o->vma < lo
14888 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14889 lo = o->vma;
14890
14891 /* And calculate GP relative to that. */
14892 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14893 }
14894 else
14895 {
14896 /* If the relocate_section function needs to do a reloc
14897 involving the GP value, it should make a reloc_dangerous
14898 callback to warn that GP is not defined. */
14899 }
14900 }
14901
14902 /* Go through the sections and collect the .reginfo and .mdebug
14903 information. */
14904 abiflags_sec = NULL;
14905 reginfo_sec = NULL;
14906 mdebug_sec = NULL;
14907 gptab_data_sec = NULL;
14908 gptab_bss_sec = NULL;
14909 for (o = abfd->sections; o != NULL; o = o->next)
14910 {
14911 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14912 {
14913 /* We have found the .MIPS.abiflags section in the output file.
14914 Look through all the link_orders comprising it and remove them.
14915 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14916 for (p = o->map_head.link_order; p != NULL; p = p->next)
14917 {
14918 asection *input_section;
14919
14920 if (p->type != bfd_indirect_link_order)
14921 {
14922 if (p->type == bfd_data_link_order)
14923 continue;
14924 abort ();
14925 }
14926
14927 input_section = p->u.indirect.section;
14928
14929 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14930 elf_link_input_bfd ignores this section. */
14931 input_section->flags &= ~SEC_HAS_CONTENTS;
14932 }
14933
14934 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14935 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14936
14937 /* Skip this section later on (I don't think this currently
14938 matters, but someday it might). */
14939 o->map_head.link_order = NULL;
14940
14941 abiflags_sec = o;
14942 }
14943
14944 if (strcmp (o->name, ".reginfo") == 0)
14945 {
14946 memset (&reginfo, 0, sizeof reginfo);
14947
14948 /* We have found the .reginfo section in the output file.
14949 Look through all the link_orders comprising it and merge
14950 the information together. */
14951 for (p = o->map_head.link_order; p != NULL; p = p->next)
14952 {
14953 asection *input_section;
14954 bfd *input_bfd;
14955 Elf32_External_RegInfo ext;
14956 Elf32_RegInfo sub;
14957 bfd_size_type sz;
14958
14959 if (p->type != bfd_indirect_link_order)
14960 {
14961 if (p->type == bfd_data_link_order)
14962 continue;
14963 abort ();
14964 }
14965
14966 input_section = p->u.indirect.section;
14967 input_bfd = input_section->owner;
14968
14969 sz = (input_section->size < sizeof (ext)
14970 ? input_section->size : sizeof (ext));
14971 memset (&ext, 0, sizeof (ext));
14972 if (! bfd_get_section_contents (input_bfd, input_section,
14973 &ext, 0, sz))
14974 return false;
14975
14976 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14977
14978 reginfo.ri_gprmask |= sub.ri_gprmask;
14979 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14980 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14981 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14982 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14983
14984 /* ri_gp_value is set by the function
14985 `_bfd_mips_elf_section_processing' when the section is
14986 finally written out. */
14987
14988 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14989 elf_link_input_bfd ignores this section. */
14990 input_section->flags &= ~SEC_HAS_CONTENTS;
14991 }
14992
14993 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14994 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14995
14996 /* Skip this section later on (I don't think this currently
14997 matters, but someday it might). */
14998 o->map_head.link_order = NULL;
14999
15000 reginfo_sec = o;
15001 }
15002
15003 if (strcmp (o->name, ".mdebug") == 0)
15004 {
15005 struct extsym_info einfo;
15006 bfd_vma last;
15007
15008 /* We have found the .mdebug section in the output file.
15009 Look through all the link_orders comprising it and merge
15010 the information together. */
15011 symhdr->magic = swap->sym_magic;
15012 /* FIXME: What should the version stamp be? */
15013 symhdr->vstamp = 0;
15014 symhdr->ilineMax = 0;
15015 symhdr->cbLine = 0;
15016 symhdr->idnMax = 0;
15017 symhdr->ipdMax = 0;
15018 symhdr->isymMax = 0;
15019 symhdr->ioptMax = 0;
15020 symhdr->iauxMax = 0;
15021 symhdr->issMax = 0;
15022 symhdr->issExtMax = 0;
15023 symhdr->ifdMax = 0;
15024 symhdr->crfd = 0;
15025 symhdr->iextMax = 0;
15026
15027 /* We accumulate the debugging information itself in the
15028 debug_info structure. */
15029 debug.alloc_syments = false;
15030 debug.line = NULL;
15031 debug.external_dnr = NULL;
15032 debug.external_pdr = NULL;
15033 debug.external_sym = NULL;
15034 debug.external_opt = NULL;
15035 debug.external_aux = NULL;
15036 debug.ss = NULL;
15037 debug.ssext = debug.ssext_end = NULL;
15038 debug.external_fdr = NULL;
15039 debug.external_rfd = NULL;
15040 debug.external_ext = debug.external_ext_end = NULL;
15041
15042 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15043 if (mdebug_handle == NULL)
15044 return false;
15045
15046 esym.jmptbl = 0;
15047 esym.cobol_main = 0;
15048 esym.weakext = 0;
15049 esym.reserved = 0;
15050 esym.ifd = ifdNil;
15051 esym.asym.iss = issNil;
15052 esym.asym.st = stLocal;
15053 esym.asym.reserved = 0;
15054 esym.asym.index = indexNil;
15055 last = 0;
15056 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15057 {
15058 esym.asym.sc = sc[i];
15059 s = bfd_get_section_by_name (abfd, secname[i]);
15060 if (s != NULL)
15061 {
15062 esym.asym.value = s->vma;
15063 last = s->vma + s->size;
15064 }
15065 else
15066 esym.asym.value = last;
15067 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15068 secname[i], &esym))
15069 return false;
15070 }
15071
15072 for (p = o->map_head.link_order; p != NULL; p = p->next)
15073 {
15074 asection *input_section;
15075 bfd *input_bfd;
15076 const struct ecoff_debug_swap *input_swap;
15077 struct ecoff_debug_info input_debug;
15078 char *eraw_src;
15079 char *eraw_end;
15080
15081 if (p->type != bfd_indirect_link_order)
15082 {
15083 if (p->type == bfd_data_link_order)
15084 continue;
15085 abort ();
15086 }
15087
15088 input_section = p->u.indirect.section;
15089 input_bfd = input_section->owner;
15090
15091 if (!is_mips_elf (input_bfd))
15092 {
15093 /* I don't know what a non MIPS ELF bfd would be
15094 doing with a .mdebug section, but I don't really
15095 want to deal with it. */
15096 continue;
15097 }
15098
15099 input_swap = (get_elf_backend_data (input_bfd)
15100 ->elf_backend_ecoff_debug_swap);
15101
15102 BFD_ASSERT (p->size == input_section->size);
15103
15104 /* The ECOFF linking code expects that we have already
15105 read in the debugging information and set up an
15106 ecoff_debug_info structure, so we do that now. */
15107 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15108 &input_debug))
15109 return false;
15110
15111 if (! (bfd_ecoff_debug_accumulate
15112 (mdebug_handle, abfd, &debug, swap, input_bfd,
15113 &input_debug, input_swap, info)))
15114 {
15115 _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15116 return false;
15117 }
15118
15119 /* Loop through the external symbols. For each one with
15120 interesting information, try to find the symbol in
15121 the linker global hash table and save the information
15122 for the output external symbols. */
15123 eraw_src = input_debug.external_ext;
15124 eraw_end = (eraw_src
15125 + (input_debug.symbolic_header.iextMax
15126 * input_swap->external_ext_size));
15127 for (;
15128 eraw_src < eraw_end;
15129 eraw_src += input_swap->external_ext_size)
15130 {
15131 EXTR ext;
15132 const char *name;
15133 struct mips_elf_link_hash_entry *h;
15134
15135 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15136 if (ext.asym.sc == scNil
15137 || ext.asym.sc == scUndefined
15138 || ext.asym.sc == scSUndefined)
15139 continue;
15140
15141 name = input_debug.ssext + ext.asym.iss;
15142 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15143 name, false, false, true);
15144 if (h == NULL || h->esym.ifd != -2)
15145 continue;
15146
15147 if (ext.ifd != -1)
15148 {
15149 BFD_ASSERT (ext.ifd
15150 < input_debug.symbolic_header.ifdMax);
15151 ext.ifd = input_debug.ifdmap[ext.ifd];
15152 }
15153
15154 h->esym = ext;
15155 }
15156
15157 /* Free up the information we just read. */
15158 _bfd_ecoff_free_ecoff_debug_info (&input_debug);
15159
15160 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15161 elf_link_input_bfd ignores this section. */
15162 input_section->flags &= ~SEC_HAS_CONTENTS;
15163 }
15164
15165 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15166 {
15167 /* Create .rtproc section. */
15168 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15169 if (rtproc_sec == NULL)
15170 {
15171 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15172 | SEC_LINKER_CREATED | SEC_READONLY);
15173
15174 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15175 ".rtproc",
15176 flags);
15177 if (rtproc_sec == NULL
15178 || !bfd_set_section_alignment (rtproc_sec, 4))
15179 return false;
15180 }
15181
15182 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15183 info, rtproc_sec,
15184 &debug))
15185 return false;
15186 }
15187
15188 /* Build the external symbol information. */
15189 einfo.abfd = abfd;
15190 einfo.info = info;
15191 einfo.debug = &debug;
15192 einfo.swap = swap;
15193 einfo.failed = false;
15194 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15195 mips_elf_output_extsym, &einfo);
15196 if (einfo.failed)
15197 return false;
15198
15199 /* Set the size of the .mdebug section. */
15200 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15201
15202 /* Skip this section later on (I don't think this currently
15203 matters, but someday it might). */
15204 o->map_head.link_order = NULL;
15205
15206 mdebug_sec = o;
15207 }
15208
15209 if (startswith (o->name, ".gptab."))
15210 {
15211 const char *subname;
15212 unsigned int c;
15213 Elf32_gptab *tab;
15214 Elf32_External_gptab *ext_tab;
15215 unsigned int j;
15216
15217 /* The .gptab.sdata and .gptab.sbss sections hold
15218 information describing how the small data area would
15219 change depending upon the -G switch. These sections
15220 not used in executables files. */
15221 if (! bfd_link_relocatable (info))
15222 {
15223 for (p = o->map_head.link_order; p != NULL; p = p->next)
15224 {
15225 asection *input_section;
15226
15227 if (p->type != bfd_indirect_link_order)
15228 {
15229 if (p->type == bfd_data_link_order)
15230 continue;
15231 abort ();
15232 }
15233
15234 input_section = p->u.indirect.section;
15235
15236 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15237 elf_link_input_bfd ignores this section. */
15238 input_section->flags &= ~SEC_HAS_CONTENTS;
15239 }
15240
15241 /* Skip this section later on (I don't think this
15242 currently matters, but someday it might). */
15243 o->map_head.link_order = NULL;
15244
15245 /* Really remove the section. */
15246 bfd_section_list_remove (abfd, o);
15247 --abfd->section_count;
15248
15249 continue;
15250 }
15251
15252 /* There is one gptab for initialized data, and one for
15253 uninitialized data. */
15254 if (strcmp (o->name, ".gptab.sdata") == 0)
15255 gptab_data_sec = o;
15256 else if (strcmp (o->name, ".gptab.sbss") == 0)
15257 gptab_bss_sec = o;
15258 else
15259 {
15260 _bfd_error_handler
15261 /* xgettext:c-format */
15262 (_("%pB: illegal section name `%pA'"), abfd, o);
15263 bfd_set_error (bfd_error_nonrepresentable_section);
15264 return false;
15265 }
15266
15267 /* The linker script always combines .gptab.data and
15268 .gptab.sdata into .gptab.sdata, and likewise for
15269 .gptab.bss and .gptab.sbss. It is possible that there is
15270 no .sdata or .sbss section in the output file, in which
15271 case we must change the name of the output section. */
15272 subname = o->name + sizeof ".gptab" - 1;
15273 if (bfd_get_section_by_name (abfd, subname) == NULL)
15274 {
15275 if (o == gptab_data_sec)
15276 o->name = ".gptab.data";
15277 else
15278 o->name = ".gptab.bss";
15279 subname = o->name + sizeof ".gptab" - 1;
15280 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15281 }
15282
15283 /* Set up the first entry. */
15284 c = 1;
15285 amt = c * sizeof (Elf32_gptab);
15286 tab = bfd_malloc (amt);
15287 if (tab == NULL)
15288 return false;
15289 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15290 tab[0].gt_header.gt_unused = 0;
15291
15292 /* Combine the input sections. */
15293 for (p = o->map_head.link_order; p != NULL; p = p->next)
15294 {
15295 asection *input_section;
15296 bfd *input_bfd;
15297 bfd_size_type size;
15298 unsigned long last;
15299 bfd_size_type gpentry;
15300
15301 if (p->type != bfd_indirect_link_order)
15302 {
15303 if (p->type == bfd_data_link_order)
15304 continue;
15305 abort ();
15306 }
15307
15308 input_section = p->u.indirect.section;
15309 input_bfd = input_section->owner;
15310
15311 /* Combine the gptab entries for this input section one
15312 by one. We know that the input gptab entries are
15313 sorted by ascending -G value. */
15314 size = input_section->size;
15315 last = 0;
15316 for (gpentry = sizeof (Elf32_External_gptab);
15317 gpentry < size;
15318 gpentry += sizeof (Elf32_External_gptab))
15319 {
15320 Elf32_External_gptab ext_gptab;
15321 Elf32_gptab int_gptab;
15322 unsigned long val;
15323 unsigned long add;
15324 bool exact;
15325 unsigned int look;
15326
15327 if (! (bfd_get_section_contents
15328 (input_bfd, input_section, &ext_gptab, gpentry,
15329 sizeof (Elf32_External_gptab))))
15330 {
15331 free (tab);
15332 return false;
15333 }
15334
15335 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15336 &int_gptab);
15337 val = int_gptab.gt_entry.gt_g_value;
15338 add = int_gptab.gt_entry.gt_bytes - last;
15339
15340 exact = false;
15341 for (look = 1; look < c; look++)
15342 {
15343 if (tab[look].gt_entry.gt_g_value >= val)
15344 tab[look].gt_entry.gt_bytes += add;
15345
15346 if (tab[look].gt_entry.gt_g_value == val)
15347 exact = true;
15348 }
15349
15350 if (! exact)
15351 {
15352 Elf32_gptab *new_tab;
15353 unsigned int max;
15354
15355 /* We need a new table entry. */
15356 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15357 new_tab = bfd_realloc (tab, amt);
15358 if (new_tab == NULL)
15359 {
15360 free (tab);
15361 return false;
15362 }
15363 tab = new_tab;
15364 tab[c].gt_entry.gt_g_value = val;
15365 tab[c].gt_entry.gt_bytes = add;
15366
15367 /* Merge in the size for the next smallest -G
15368 value, since that will be implied by this new
15369 value. */
15370 max = 0;
15371 for (look = 1; look < c; look++)
15372 {
15373 if (tab[look].gt_entry.gt_g_value < val
15374 && (max == 0
15375 || (tab[look].gt_entry.gt_g_value
15376 > tab[max].gt_entry.gt_g_value)))
15377 max = look;
15378 }
15379 if (max != 0)
15380 tab[c].gt_entry.gt_bytes +=
15381 tab[max].gt_entry.gt_bytes;
15382
15383 ++c;
15384 }
15385
15386 last = int_gptab.gt_entry.gt_bytes;
15387 }
15388
15389 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15390 elf_link_input_bfd ignores this section. */
15391 input_section->flags &= ~SEC_HAS_CONTENTS;
15392 }
15393
15394 /* The table must be sorted by -G value. */
15395 if (c > 2)
15396 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15397
15398 /* Swap out the table. */
15399 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15400 ext_tab = bfd_alloc (abfd, amt);
15401 if (ext_tab == NULL)
15402 {
15403 free (tab);
15404 return false;
15405 }
15406
15407 for (j = 0; j < c; j++)
15408 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15409 free (tab);
15410
15411 o->size = c * sizeof (Elf32_External_gptab);
15412 o->contents = (bfd_byte *) ext_tab;
15413
15414 /* Skip this section later on (I don't think this currently
15415 matters, but someday it might). */
15416 o->map_head.link_order = NULL;
15417 }
15418 }
15419
15420 /* Invoke the regular ELF backend linker to do all the work. */
15421 if (!bfd_elf_final_link (abfd, info))
15422 return false;
15423
15424 /* Now write out the computed sections. */
15425
15426 if (abiflags_sec != NULL)
15427 {
15428 Elf_External_ABIFlags_v0 ext;
15429 Elf_Internal_ABIFlags_v0 *abiflags;
15430
15431 abiflags = &mips_elf_tdata (abfd)->abiflags;
15432
15433 /* Set up the abiflags if no valid input sections were found. */
15434 if (!mips_elf_tdata (abfd)->abiflags_valid)
15435 {
15436 infer_mips_abiflags (abfd, abiflags);
15437 mips_elf_tdata (abfd)->abiflags_valid = true;
15438 }
15439 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15440 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15441 return false;
15442 }
15443
15444 if (reginfo_sec != NULL)
15445 {
15446 Elf32_External_RegInfo ext;
15447
15448 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15449 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15450 return false;
15451 }
15452
15453 if (mdebug_sec != NULL)
15454 {
15455 BFD_ASSERT (abfd->output_has_begun);
15456 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15457 swap, info,
15458 mdebug_sec->filepos))
15459 return false;
15460
15461 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15462 }
15463
15464 if (gptab_data_sec != NULL)
15465 {
15466 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15467 gptab_data_sec->contents,
15468 0, gptab_data_sec->size))
15469 return false;
15470 }
15471
15472 if (gptab_bss_sec != NULL)
15473 {
15474 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15475 gptab_bss_sec->contents,
15476 0, gptab_bss_sec->size))
15477 return false;
15478 }
15479
15480 if (SGI_COMPAT (abfd))
15481 {
15482 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15483 if (rtproc_sec != NULL)
15484 {
15485 if (! bfd_set_section_contents (abfd, rtproc_sec,
15486 rtproc_sec->contents,
15487 0, rtproc_sec->size))
15488 return false;
15489 }
15490 }
15491
15492 return true;
15493 }
15494 \f
15495 /* Merge object file header flags from IBFD into OBFD. Raise an error
15496 if there are conflicting settings. */
15497
15498 static bool
15499 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15500 {
15501 bfd *obfd = info->output_bfd;
15502 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15503 flagword old_flags;
15504 flagword new_flags;
15505 bool ok;
15506
15507 new_flags = elf_elfheader (ibfd)->e_flags;
15508 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15509 old_flags = elf_elfheader (obfd)->e_flags;
15510
15511 /* Check flag compatibility. */
15512
15513 new_flags &= ~EF_MIPS_NOREORDER;
15514 old_flags &= ~EF_MIPS_NOREORDER;
15515
15516 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15517 doesn't seem to matter. */
15518 new_flags &= ~EF_MIPS_XGOT;
15519 old_flags &= ~EF_MIPS_XGOT;
15520
15521 /* MIPSpro generates ucode info in n64 objects. Again, we should
15522 just be able to ignore this. */
15523 new_flags &= ~EF_MIPS_UCODE;
15524 old_flags &= ~EF_MIPS_UCODE;
15525
15526 /* DSOs should only be linked with CPIC code. */
15527 if ((ibfd->flags & DYNAMIC) != 0)
15528 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15529
15530 if (new_flags == old_flags)
15531 return true;
15532
15533 ok = true;
15534
15535 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15536 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15537 {
15538 _bfd_error_handler
15539 (_("%pB: warning: linking abicalls files with non-abicalls files"),
15540 ibfd);
15541 ok = true;
15542 }
15543
15544 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15545 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15546 if (! (new_flags & EF_MIPS_PIC))
15547 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15548
15549 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15550 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15551
15552 /* Compare the ISAs. */
15553 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15554 {
15555 _bfd_error_handler
15556 (_("%pB: linking 32-bit code with 64-bit code"),
15557 ibfd);
15558 ok = false;
15559 }
15560 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15561 {
15562 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15563 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15564 {
15565 /* Copy the architecture info from IBFD to OBFD. Also copy
15566 the 32-bit flag (if set) so that we continue to recognise
15567 OBFD as a 32-bit binary. */
15568 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15569 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15570 elf_elfheader (obfd)->e_flags
15571 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15572
15573 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15574 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15575
15576 /* Copy across the ABI flags if OBFD doesn't use them
15577 and if that was what caused us to treat IBFD as 32-bit. */
15578 if ((old_flags & EF_MIPS_ABI) == 0
15579 && mips_32bit_flags_p (new_flags)
15580 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15581 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15582 }
15583 else
15584 {
15585 /* The ISAs aren't compatible. */
15586 _bfd_error_handler
15587 /* xgettext:c-format */
15588 (_("%pB: linking %s module with previous %s modules"),
15589 ibfd,
15590 bfd_printable_name (ibfd),
15591 bfd_printable_name (obfd));
15592 ok = false;
15593 }
15594 }
15595
15596 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15597 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15598
15599 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15600 does set EI_CLASS differently from any 32-bit ABI. */
15601 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15602 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15603 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15604 {
15605 /* Only error if both are set (to different values). */
15606 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15607 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15608 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15609 {
15610 _bfd_error_handler
15611 /* xgettext:c-format */
15612 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15613 ibfd,
15614 elf_mips_abi_name (ibfd),
15615 elf_mips_abi_name (obfd));
15616 ok = false;
15617 }
15618 new_flags &= ~EF_MIPS_ABI;
15619 old_flags &= ~EF_MIPS_ABI;
15620 }
15621
15622 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15623 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15624 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15625 {
15626 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15627 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15628 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15629 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15630 int micro_mis = old_m16 && new_micro;
15631 int m16_mis = old_micro && new_m16;
15632
15633 if (m16_mis || micro_mis)
15634 {
15635 _bfd_error_handler
15636 /* xgettext:c-format */
15637 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15638 ibfd,
15639 m16_mis ? "MIPS16" : "microMIPS",
15640 m16_mis ? "microMIPS" : "MIPS16");
15641 ok = false;
15642 }
15643
15644 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15645
15646 new_flags &= ~ EF_MIPS_ARCH_ASE;
15647 old_flags &= ~ EF_MIPS_ARCH_ASE;
15648 }
15649
15650 /* Compare NaN encodings. */
15651 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15652 {
15653 /* xgettext:c-format */
15654 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15655 ibfd,
15656 (new_flags & EF_MIPS_NAN2008
15657 ? "-mnan=2008" : "-mnan=legacy"),
15658 (old_flags & EF_MIPS_NAN2008
15659 ? "-mnan=2008" : "-mnan=legacy"));
15660 ok = false;
15661 new_flags &= ~EF_MIPS_NAN2008;
15662 old_flags &= ~EF_MIPS_NAN2008;
15663 }
15664
15665 /* Compare FP64 state. */
15666 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15667 {
15668 /* xgettext:c-format */
15669 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15670 ibfd,
15671 (new_flags & EF_MIPS_FP64
15672 ? "-mfp64" : "-mfp32"),
15673 (old_flags & EF_MIPS_FP64
15674 ? "-mfp64" : "-mfp32"));
15675 ok = false;
15676 new_flags &= ~EF_MIPS_FP64;
15677 old_flags &= ~EF_MIPS_FP64;
15678 }
15679
15680 /* Warn about any other mismatches */
15681 if (new_flags != old_flags)
15682 {
15683 /* xgettext:c-format */
15684 _bfd_error_handler
15685 (_("%pB: uses different e_flags (%#x) fields than previous modules "
15686 "(%#x)"),
15687 ibfd, new_flags, old_flags);
15688 ok = false;
15689 }
15690
15691 return ok;
15692 }
15693
15694 /* Merge object attributes from IBFD into OBFD. Raise an error if
15695 there are conflicting attributes. */
15696 static bool
15697 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15698 {
15699 bfd *obfd = info->output_bfd;
15700 obj_attribute *in_attr;
15701 obj_attribute *out_attr;
15702 bfd *abi_fp_bfd;
15703 bfd *abi_msa_bfd;
15704
15705 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15706 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15707 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15708 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15709
15710 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15711 if (!abi_msa_bfd
15712 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15713 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15714
15715 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15716 {
15717 /* This is the first object. Copy the attributes. */
15718 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15719
15720 /* Use the Tag_null value to indicate the attributes have been
15721 initialized. */
15722 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15723
15724 return true;
15725 }
15726
15727 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15728 non-conflicting ones. */
15729 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15730 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15731 {
15732 int out_fp, in_fp;
15733
15734 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15735 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15736 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15737 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15738 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15739 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15740 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15741 || in_fp == Val_GNU_MIPS_ABI_FP_64
15742 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15743 {
15744 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15745 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15746 }
15747 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15748 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15749 || out_fp == Val_GNU_MIPS_ABI_FP_64
15750 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15751 /* Keep the current setting. */;
15752 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15753 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15754 {
15755 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15756 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15757 }
15758 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15759 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15760 /* Keep the current setting. */;
15761 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15762 {
15763 const char *out_string, *in_string;
15764
15765 out_string = _bfd_mips_fp_abi_string (out_fp);
15766 in_string = _bfd_mips_fp_abi_string (in_fp);
15767 /* First warn about cases involving unrecognised ABIs. */
15768 if (!out_string && !in_string)
15769 /* xgettext:c-format */
15770 _bfd_error_handler
15771 (_("warning: %pB uses unknown floating point ABI %d "
15772 "(set by %pB), %pB uses unknown floating point ABI %d"),
15773 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15774 else if (!out_string)
15775 _bfd_error_handler
15776 /* xgettext:c-format */
15777 (_("warning: %pB uses unknown floating point ABI %d "
15778 "(set by %pB), %pB uses %s"),
15779 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15780 else if (!in_string)
15781 _bfd_error_handler
15782 /* xgettext:c-format */
15783 (_("warning: %pB uses %s (set by %pB), "
15784 "%pB uses unknown floating point ABI %d"),
15785 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15786 else
15787 {
15788 /* If one of the bfds is soft-float, the other must be
15789 hard-float. The exact choice of hard-float ABI isn't
15790 really relevant to the error message. */
15791 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15792 out_string = "-mhard-float";
15793 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15794 in_string = "-mhard-float";
15795 _bfd_error_handler
15796 /* xgettext:c-format */
15797 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15798 obfd, out_string, abi_fp_bfd, ibfd, in_string);
15799 }
15800 }
15801 }
15802
15803 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15804 non-conflicting ones. */
15805 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15806 {
15807 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15808 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15809 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15810 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15811 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15812 {
15813 case Val_GNU_MIPS_ABI_MSA_128:
15814 _bfd_error_handler
15815 /* xgettext:c-format */
15816 (_("warning: %pB uses %s (set by %pB), "
15817 "%pB uses unknown MSA ABI %d"),
15818 obfd, "-mmsa", abi_msa_bfd,
15819 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15820 break;
15821
15822 default:
15823 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15824 {
15825 case Val_GNU_MIPS_ABI_MSA_128:
15826 _bfd_error_handler
15827 /* xgettext:c-format */
15828 (_("warning: %pB uses unknown MSA ABI %d "
15829 "(set by %pB), %pB uses %s"),
15830 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15831 abi_msa_bfd, ibfd, "-mmsa");
15832 break;
15833
15834 default:
15835 _bfd_error_handler
15836 /* xgettext:c-format */
15837 (_("warning: %pB uses unknown MSA ABI %d "
15838 "(set by %pB), %pB uses unknown MSA ABI %d"),
15839 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15840 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15841 break;
15842 }
15843 }
15844 }
15845
15846 /* Merge Tag_compatibility attributes and any common GNU ones. */
15847 return _bfd_elf_merge_object_attributes (ibfd, info);
15848 }
15849
15850 /* Merge object ABI flags from IBFD into OBFD. Raise an error if
15851 there are conflicting settings. */
15852
15853 static bool
15854 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15855 {
15856 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15857 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15858 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15859
15860 /* Update the output abiflags fp_abi using the computed fp_abi. */
15861 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15862
15863 #define max(a, b) ((a) > (b) ? (a) : (b))
15864 /* Merge abiflags. */
15865 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15866 in_tdata->abiflags.isa_level);
15867 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15868 in_tdata->abiflags.isa_rev);
15869 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15870 in_tdata->abiflags.gpr_size);
15871 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15872 in_tdata->abiflags.cpr1_size);
15873 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15874 in_tdata->abiflags.cpr2_size);
15875 #undef max
15876 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15877 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15878
15879 return true;
15880 }
15881
15882 /* Merge backend specific data from an object file to the output
15883 object file when linking. */
15884
15885 bool
15886 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15887 {
15888 bfd *obfd = info->output_bfd;
15889 struct mips_elf_obj_tdata *out_tdata;
15890 struct mips_elf_obj_tdata *in_tdata;
15891 bool null_input_bfd = true;
15892 asection *sec;
15893 bool ok;
15894
15895 /* Check if we have the same endianness. */
15896 if (! _bfd_generic_verify_endian_match (ibfd, info))
15897 {
15898 _bfd_error_handler
15899 (_("%pB: endianness incompatible with that of the selected emulation"),
15900 ibfd);
15901 return false;
15902 }
15903
15904 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15905 return true;
15906
15907 in_tdata = mips_elf_tdata (ibfd);
15908 out_tdata = mips_elf_tdata (obfd);
15909
15910 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15911 {
15912 _bfd_error_handler
15913 (_("%pB: ABI is incompatible with that of the selected emulation"),
15914 ibfd);
15915 return false;
15916 }
15917
15918 /* Check to see if the input BFD actually contains any sections. If not,
15919 then it has no attributes, and its flags may not have been initialized
15920 either, but it cannot actually cause any incompatibility. */
15921 /* FIXME: This excludes any input shared library from consideration. */
15922 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15923 {
15924 /* Ignore synthetic sections and empty .text, .data and .bss sections
15925 which are automatically generated by gas. Also ignore fake
15926 (s)common sections, since merely defining a common symbol does
15927 not affect compatibility. */
15928 if ((sec->flags & SEC_IS_COMMON) == 0
15929 && strcmp (sec->name, ".reginfo")
15930 && strcmp (sec->name, ".mdebug")
15931 && (sec->size != 0
15932 || (strcmp (sec->name, ".text")
15933 && strcmp (sec->name, ".data")
15934 && strcmp (sec->name, ".bss"))))
15935 {
15936 null_input_bfd = false;
15937 break;
15938 }
15939 }
15940 if (null_input_bfd)
15941 return true;
15942
15943 /* Populate abiflags using existing information. */
15944 if (in_tdata->abiflags_valid)
15945 {
15946 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15947 Elf_Internal_ABIFlags_v0 in_abiflags;
15948 Elf_Internal_ABIFlags_v0 abiflags;
15949
15950 /* Set up the FP ABI attribute from the abiflags if it is not already
15951 set. */
15952 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15953 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15954
15955 infer_mips_abiflags (ibfd, &abiflags);
15956 in_abiflags = in_tdata->abiflags;
15957
15958 /* It is not possible to infer the correct ISA revision
15959 for R3 or R5 so drop down to R2 for the checks. */
15960 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15961 in_abiflags.isa_rev = 2;
15962
15963 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15964 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15965 _bfd_error_handler
15966 (_("%pB: warning: inconsistent ISA between e_flags and "
15967 ".MIPS.abiflags"), ibfd);
15968 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15969 && in_abiflags.fp_abi != abiflags.fp_abi)
15970 _bfd_error_handler
15971 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15972 ".MIPS.abiflags"), ibfd);
15973 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15974 _bfd_error_handler
15975 (_("%pB: warning: inconsistent ASEs between e_flags and "
15976 ".MIPS.abiflags"), ibfd);
15977 /* The isa_ext is allowed to be an extension of what can be inferred
15978 from e_flags. */
15979 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15980 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15981 _bfd_error_handler
15982 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15983 ".MIPS.abiflags"), ibfd);
15984 if (in_abiflags.flags2 != 0)
15985 _bfd_error_handler
15986 (_("%pB: warning: unexpected flag in the flags2 field of "
15987 ".MIPS.abiflags (0x%lx)"), ibfd,
15988 in_abiflags.flags2);
15989 }
15990 else
15991 {
15992 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15993 in_tdata->abiflags_valid = true;
15994 }
15995
15996 if (!out_tdata->abiflags_valid)
15997 {
15998 /* Copy input abiflags if output abiflags are not already valid. */
15999 out_tdata->abiflags = in_tdata->abiflags;
16000 out_tdata->abiflags_valid = true;
16001 }
16002
16003 if (! elf_flags_init (obfd))
16004 {
16005 elf_flags_init (obfd) = true;
16006 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
16007 elf_elfheader (obfd)->e_ident[EI_CLASS]
16008 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
16009
16010 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
16011 && (bfd_get_arch_info (obfd)->the_default
16012 || mips_mach_extends_p (bfd_get_mach (obfd),
16013 bfd_get_mach (ibfd))))
16014 {
16015 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
16016 bfd_get_mach (ibfd)))
16017 return false;
16018
16019 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
16020 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
16021 }
16022
16023 ok = true;
16024 }
16025 else
16026 ok = mips_elf_merge_obj_e_flags (ibfd, info);
16027
16028 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16029
16030 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16031
16032 if (!ok)
16033 {
16034 bfd_set_error (bfd_error_bad_value);
16035 return false;
16036 }
16037
16038 return true;
16039 }
16040
16041 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
16042
16043 bool
16044 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16045 {
16046 BFD_ASSERT (!elf_flags_init (abfd)
16047 || elf_elfheader (abfd)->e_flags == flags);
16048
16049 elf_elfheader (abfd)->e_flags = flags;
16050 elf_flags_init (abfd) = true;
16051 return true;
16052 }
16053
16054 char *
16055 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16056 {
16057 switch (dtag)
16058 {
16059 default: return "";
16060 case DT_MIPS_RLD_VERSION:
16061 return "MIPS_RLD_VERSION";
16062 case DT_MIPS_TIME_STAMP:
16063 return "MIPS_TIME_STAMP";
16064 case DT_MIPS_ICHECKSUM:
16065 return "MIPS_ICHECKSUM";
16066 case DT_MIPS_IVERSION:
16067 return "MIPS_IVERSION";
16068 case DT_MIPS_FLAGS:
16069 return "MIPS_FLAGS";
16070 case DT_MIPS_BASE_ADDRESS:
16071 return "MIPS_BASE_ADDRESS";
16072 case DT_MIPS_MSYM:
16073 return "MIPS_MSYM";
16074 case DT_MIPS_CONFLICT:
16075 return "MIPS_CONFLICT";
16076 case DT_MIPS_LIBLIST:
16077 return "MIPS_LIBLIST";
16078 case DT_MIPS_LOCAL_GOTNO:
16079 return "MIPS_LOCAL_GOTNO";
16080 case DT_MIPS_CONFLICTNO:
16081 return "MIPS_CONFLICTNO";
16082 case DT_MIPS_LIBLISTNO:
16083 return "MIPS_LIBLISTNO";
16084 case DT_MIPS_SYMTABNO:
16085 return "MIPS_SYMTABNO";
16086 case DT_MIPS_UNREFEXTNO:
16087 return "MIPS_UNREFEXTNO";
16088 case DT_MIPS_GOTSYM:
16089 return "MIPS_GOTSYM";
16090 case DT_MIPS_HIPAGENO:
16091 return "MIPS_HIPAGENO";
16092 case DT_MIPS_RLD_MAP:
16093 return "MIPS_RLD_MAP";
16094 case DT_MIPS_RLD_MAP_REL:
16095 return "MIPS_RLD_MAP_REL";
16096 case DT_MIPS_DELTA_CLASS:
16097 return "MIPS_DELTA_CLASS";
16098 case DT_MIPS_DELTA_CLASS_NO:
16099 return "MIPS_DELTA_CLASS_NO";
16100 case DT_MIPS_DELTA_INSTANCE:
16101 return "MIPS_DELTA_INSTANCE";
16102 case DT_MIPS_DELTA_INSTANCE_NO:
16103 return "MIPS_DELTA_INSTANCE_NO";
16104 case DT_MIPS_DELTA_RELOC:
16105 return "MIPS_DELTA_RELOC";
16106 case DT_MIPS_DELTA_RELOC_NO:
16107 return "MIPS_DELTA_RELOC_NO";
16108 case DT_MIPS_DELTA_SYM:
16109 return "MIPS_DELTA_SYM";
16110 case DT_MIPS_DELTA_SYM_NO:
16111 return "MIPS_DELTA_SYM_NO";
16112 case DT_MIPS_DELTA_CLASSSYM:
16113 return "MIPS_DELTA_CLASSSYM";
16114 case DT_MIPS_DELTA_CLASSSYM_NO:
16115 return "MIPS_DELTA_CLASSSYM_NO";
16116 case DT_MIPS_CXX_FLAGS:
16117 return "MIPS_CXX_FLAGS";
16118 case DT_MIPS_PIXIE_INIT:
16119 return "MIPS_PIXIE_INIT";
16120 case DT_MIPS_SYMBOL_LIB:
16121 return "MIPS_SYMBOL_LIB";
16122 case DT_MIPS_LOCALPAGE_GOTIDX:
16123 return "MIPS_LOCALPAGE_GOTIDX";
16124 case DT_MIPS_LOCAL_GOTIDX:
16125 return "MIPS_LOCAL_GOTIDX";
16126 case DT_MIPS_HIDDEN_GOTIDX:
16127 return "MIPS_HIDDEN_GOTIDX";
16128 case DT_MIPS_PROTECTED_GOTIDX:
16129 return "MIPS_PROTECTED_GOT_IDX";
16130 case DT_MIPS_OPTIONS:
16131 return "MIPS_OPTIONS";
16132 case DT_MIPS_INTERFACE:
16133 return "MIPS_INTERFACE";
16134 case DT_MIPS_DYNSTR_ALIGN:
16135 return "DT_MIPS_DYNSTR_ALIGN";
16136 case DT_MIPS_INTERFACE_SIZE:
16137 return "DT_MIPS_INTERFACE_SIZE";
16138 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16139 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16140 case DT_MIPS_PERF_SUFFIX:
16141 return "DT_MIPS_PERF_SUFFIX";
16142 case DT_MIPS_COMPACT_SIZE:
16143 return "DT_MIPS_COMPACT_SIZE";
16144 case DT_MIPS_GP_VALUE:
16145 return "DT_MIPS_GP_VALUE";
16146 case DT_MIPS_AUX_DYNAMIC:
16147 return "DT_MIPS_AUX_DYNAMIC";
16148 case DT_MIPS_PLTGOT:
16149 return "DT_MIPS_PLTGOT";
16150 case DT_MIPS_RWPLT:
16151 return "DT_MIPS_RWPLT";
16152 case DT_MIPS_XHASH:
16153 return "DT_MIPS_XHASH";
16154 }
16155 }
16156
16157 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16158 not known. */
16159
16160 const char *
16161 _bfd_mips_fp_abi_string (int fp)
16162 {
16163 switch (fp)
16164 {
16165 /* These strings aren't translated because they're simply
16166 option lists. */
16167 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16168 return "-mdouble-float";
16169
16170 case Val_GNU_MIPS_ABI_FP_SINGLE:
16171 return "-msingle-float";
16172
16173 case Val_GNU_MIPS_ABI_FP_SOFT:
16174 return "-msoft-float";
16175
16176 case Val_GNU_MIPS_ABI_FP_OLD_64:
16177 return _("-mips32r2 -mfp64 (12 callee-saved)");
16178
16179 case Val_GNU_MIPS_ABI_FP_XX:
16180 return "-mfpxx";
16181
16182 case Val_GNU_MIPS_ABI_FP_64:
16183 return "-mgp32 -mfp64";
16184
16185 case Val_GNU_MIPS_ABI_FP_64A:
16186 return "-mgp32 -mfp64 -mno-odd-spreg";
16187
16188 default:
16189 return 0;
16190 }
16191 }
16192
16193 static void
16194 print_mips_ases (FILE *file, unsigned int mask)
16195 {
16196 if (mask & AFL_ASE_DSP)
16197 fputs ("\n\tDSP ASE", file);
16198 if (mask & AFL_ASE_DSPR2)
16199 fputs ("\n\tDSP R2 ASE", file);
16200 if (mask & AFL_ASE_DSPR3)
16201 fputs ("\n\tDSP R3 ASE", file);
16202 if (mask & AFL_ASE_EVA)
16203 fputs ("\n\tEnhanced VA Scheme", file);
16204 if (mask & AFL_ASE_MCU)
16205 fputs ("\n\tMCU (MicroController) ASE", file);
16206 if (mask & AFL_ASE_MDMX)
16207 fputs ("\n\tMDMX ASE", file);
16208 if (mask & AFL_ASE_MIPS3D)
16209 fputs ("\n\tMIPS-3D ASE", file);
16210 if (mask & AFL_ASE_MT)
16211 fputs ("\n\tMT ASE", file);
16212 if (mask & AFL_ASE_SMARTMIPS)
16213 fputs ("\n\tSmartMIPS ASE", file);
16214 if (mask & AFL_ASE_VIRT)
16215 fputs ("\n\tVZ ASE", file);
16216 if (mask & AFL_ASE_MSA)
16217 fputs ("\n\tMSA ASE", file);
16218 if (mask & AFL_ASE_MIPS16)
16219 fputs ("\n\tMIPS16 ASE", file);
16220 if (mask & AFL_ASE_MICROMIPS)
16221 fputs ("\n\tMICROMIPS ASE", file);
16222 if (mask & AFL_ASE_XPA)
16223 fputs ("\n\tXPA ASE", file);
16224 if (mask & AFL_ASE_MIPS16E2)
16225 fputs ("\n\tMIPS16e2 ASE", file);
16226 if (mask & AFL_ASE_CRC)
16227 fputs ("\n\tCRC ASE", file);
16228 if (mask & AFL_ASE_GINV)
16229 fputs ("\n\tGINV ASE", file);
16230 if (mask & AFL_ASE_LOONGSON_MMI)
16231 fputs ("\n\tLoongson MMI ASE", file);
16232 if (mask & AFL_ASE_LOONGSON_CAM)
16233 fputs ("\n\tLoongson CAM ASE", file);
16234 if (mask & AFL_ASE_LOONGSON_EXT)
16235 fputs ("\n\tLoongson EXT ASE", file);
16236 if (mask & AFL_ASE_LOONGSON_EXT2)
16237 fputs ("\n\tLoongson EXT2 ASE", file);
16238 if (mask == 0)
16239 fprintf (file, "\n\t%s", _("None"));
16240 else if ((mask & ~AFL_ASE_MASK) != 0)
16241 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16242 }
16243
16244 static void
16245 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16246 {
16247 switch (isa_ext)
16248 {
16249 case 0:
16250 fputs (_("None"), file);
16251 break;
16252 case AFL_EXT_XLR:
16253 fputs ("RMI XLR", file);
16254 break;
16255 case AFL_EXT_OCTEON3:
16256 fputs ("Cavium Networks Octeon3", file);
16257 break;
16258 case AFL_EXT_OCTEON2:
16259 fputs ("Cavium Networks Octeon2", file);
16260 break;
16261 case AFL_EXT_OCTEONP:
16262 fputs ("Cavium Networks OcteonP", file);
16263 break;
16264 case AFL_EXT_OCTEON:
16265 fputs ("Cavium Networks Octeon", file);
16266 break;
16267 case AFL_EXT_5900:
16268 fputs ("Toshiba R5900", file);
16269 break;
16270 case AFL_EXT_4650:
16271 fputs ("MIPS R4650", file);
16272 break;
16273 case AFL_EXT_4010:
16274 fputs ("LSI R4010", file);
16275 break;
16276 case AFL_EXT_4100:
16277 fputs ("NEC VR4100", file);
16278 break;
16279 case AFL_EXT_3900:
16280 fputs ("Toshiba R3900", file);
16281 break;
16282 case AFL_EXT_10000:
16283 fputs ("MIPS R10000", file);
16284 break;
16285 case AFL_EXT_SB1:
16286 fputs ("Broadcom SB-1", file);
16287 break;
16288 case AFL_EXT_4111:
16289 fputs ("NEC VR4111/VR4181", file);
16290 break;
16291 case AFL_EXT_4120:
16292 fputs ("NEC VR4120", file);
16293 break;
16294 case AFL_EXT_5400:
16295 fputs ("NEC VR5400", file);
16296 break;
16297 case AFL_EXT_5500:
16298 fputs ("NEC VR5500", file);
16299 break;
16300 case AFL_EXT_LOONGSON_2E:
16301 fputs ("ST Microelectronics Loongson 2E", file);
16302 break;
16303 case AFL_EXT_LOONGSON_2F:
16304 fputs ("ST Microelectronics Loongson 2F", file);
16305 break;
16306 case AFL_EXT_INTERAPTIV_MR2:
16307 fputs ("Imagination interAptiv MR2", file);
16308 break;
16309 default:
16310 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16311 break;
16312 }
16313 }
16314
16315 static void
16316 print_mips_fp_abi_value (FILE *file, int val)
16317 {
16318 switch (val)
16319 {
16320 case Val_GNU_MIPS_ABI_FP_ANY:
16321 fprintf (file, _("Hard or soft float\n"));
16322 break;
16323 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16324 fprintf (file, _("Hard float (double precision)\n"));
16325 break;
16326 case Val_GNU_MIPS_ABI_FP_SINGLE:
16327 fprintf (file, _("Hard float (single precision)\n"));
16328 break;
16329 case Val_GNU_MIPS_ABI_FP_SOFT:
16330 fprintf (file, _("Soft float\n"));
16331 break;
16332 case Val_GNU_MIPS_ABI_FP_OLD_64:
16333 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16334 break;
16335 case Val_GNU_MIPS_ABI_FP_XX:
16336 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16337 break;
16338 case Val_GNU_MIPS_ABI_FP_64:
16339 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16340 break;
16341 case Val_GNU_MIPS_ABI_FP_64A:
16342 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16343 break;
16344 default:
16345 fprintf (file, "??? (%d)\n", val);
16346 break;
16347 }
16348 }
16349
16350 static int
16351 get_mips_reg_size (int reg_size)
16352 {
16353 return (reg_size == AFL_REG_NONE) ? 0
16354 : (reg_size == AFL_REG_32) ? 32
16355 : (reg_size == AFL_REG_64) ? 64
16356 : (reg_size == AFL_REG_128) ? 128
16357 : -1;
16358 }
16359
16360 bool
16361 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16362 {
16363 FILE *file = ptr;
16364
16365 BFD_ASSERT (abfd != NULL && ptr != NULL);
16366
16367 /* Print normal ELF private data. */
16368 _bfd_elf_print_private_bfd_data (abfd, ptr);
16369
16370 /* xgettext:c-format */
16371 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16372
16373 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16374 fprintf (file, _(" [abi=O32]"));
16375 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16376 fprintf (file, _(" [abi=O64]"));
16377 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16378 fprintf (file, _(" [abi=EABI32]"));
16379 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16380 fprintf (file, _(" [abi=EABI64]"));
16381 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16382 fprintf (file, _(" [abi unknown]"));
16383 else if (ABI_N32_P (abfd))
16384 fprintf (file, _(" [abi=N32]"));
16385 else if (ABI_64_P (abfd))
16386 fprintf (file, _(" [abi=64]"));
16387 else
16388 fprintf (file, _(" [no abi set]"));
16389
16390 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
16391 fprintf (file, " [mips1]");
16392 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
16393 fprintf (file, " [mips2]");
16394 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
16395 fprintf (file, " [mips3]");
16396 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
16397 fprintf (file, " [mips4]");
16398 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
16399 fprintf (file, " [mips5]");
16400 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
16401 fprintf (file, " [mips32]");
16402 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
16403 fprintf (file, " [mips64]");
16404 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
16405 fprintf (file, " [mips32r2]");
16406 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
16407 fprintf (file, " [mips64r2]");
16408 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16409 fprintf (file, " [mips32r6]");
16410 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16411 fprintf (file, " [mips64r6]");
16412 else
16413 fprintf (file, _(" [unknown ISA]"));
16414
16415 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16416 fprintf (file, " [mdmx]");
16417
16418 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16419 fprintf (file, " [mips16]");
16420
16421 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16422 fprintf (file, " [micromips]");
16423
16424 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16425 fprintf (file, " [nan2008]");
16426
16427 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16428 fprintf (file, " [old fp64]");
16429
16430 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16431 fprintf (file, " [32bitmode]");
16432 else
16433 fprintf (file, _(" [not 32bitmode]"));
16434
16435 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16436 fprintf (file, " [noreorder]");
16437
16438 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16439 fprintf (file, " [PIC]");
16440
16441 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16442 fprintf (file, " [CPIC]");
16443
16444 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16445 fprintf (file, " [XGOT]");
16446
16447 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16448 fprintf (file, " [UCODE]");
16449
16450 fputc ('\n', file);
16451
16452 if (mips_elf_tdata (abfd)->abiflags_valid)
16453 {
16454 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16455 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16456 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16457 if (abiflags->isa_rev > 1)
16458 fprintf (file, "r%d", abiflags->isa_rev);
16459 fprintf (file, "\nGPR size: %d",
16460 get_mips_reg_size (abiflags->gpr_size));
16461 fprintf (file, "\nCPR1 size: %d",
16462 get_mips_reg_size (abiflags->cpr1_size));
16463 fprintf (file, "\nCPR2 size: %d",
16464 get_mips_reg_size (abiflags->cpr2_size));
16465 fputs ("\nFP ABI: ", file);
16466 print_mips_fp_abi_value (file, abiflags->fp_abi);
16467 fputs ("ISA Extension: ", file);
16468 print_mips_isa_ext (file, abiflags->isa_ext);
16469 fputs ("\nASEs:", file);
16470 print_mips_ases (file, abiflags->ases);
16471 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16472 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16473 fputc ('\n', file);
16474 }
16475
16476 return true;
16477 }
16478
16479 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16480 {
16481 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16482 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16483 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16484 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16485 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16486 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
16487 { STRING_COMMA_LEN (".MIPS.xhash"), 0, SHT_MIPS_XHASH, SHF_ALLOC },
16488 { NULL, 0, 0, 0, 0 }
16489 };
16490
16491 /* Merge non visibility st_other attributes. Ensure that the
16492 STO_OPTIONAL flag is copied into h->other, even if this is not a
16493 definiton of the symbol. */
16494 void
16495 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16496 unsigned int st_other,
16497 bool definition,
16498 bool dynamic ATTRIBUTE_UNUSED)
16499 {
16500 if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16501 {
16502 unsigned char other;
16503
16504 other = (definition ? st_other : h->other);
16505 other &= ~ELF_ST_VISIBILITY (-1);
16506 h->other = other | ELF_ST_VISIBILITY (h->other);
16507 }
16508
16509 if (!definition
16510 && ELF_MIPS_IS_OPTIONAL (st_other))
16511 h->other |= STO_OPTIONAL;
16512 }
16513
16514 /* Decide whether an undefined symbol is special and can be ignored.
16515 This is the case for OPTIONAL symbols on IRIX. */
16516 bool
16517 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16518 {
16519 return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16520 }
16521
16522 bool
16523 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16524 {
16525 return (sym->st_shndx == SHN_COMMON
16526 || sym->st_shndx == SHN_MIPS_ACOMMON
16527 || sym->st_shndx == SHN_MIPS_SCOMMON);
16528 }
16529
16530 /* Return address for Ith PLT stub in section PLT, for relocation REL
16531 or (bfd_vma) -1 if it should not be included. */
16532
16533 bfd_vma
16534 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16535 const arelent *rel ATTRIBUTE_UNUSED)
16536 {
16537 return (plt->vma
16538 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16539 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16540 }
16541
16542 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16543 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16544 and .got.plt and also the slots may be of a different size each we walk
16545 the PLT manually fetching instructions and matching them against known
16546 patterns. To make things easier standard MIPS slots, if any, always come
16547 first. As we don't create proper ELF symbols we use the UDATA.I member
16548 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16549 with the ST_OTHER member of the ELF symbol. */
16550
16551 long
16552 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16553 long symcount ATTRIBUTE_UNUSED,
16554 asymbol **syms ATTRIBUTE_UNUSED,
16555 long dynsymcount, asymbol **dynsyms,
16556 asymbol **ret)
16557 {
16558 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16559 static const char microsuffix[] = "@micromipsplt";
16560 static const char m16suffix[] = "@mips16plt";
16561 static const char mipssuffix[] = "@plt";
16562
16563 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16564 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16565 bool micromips_p = MICROMIPS_P (abfd);
16566 Elf_Internal_Shdr *hdr;
16567 bfd_byte *plt_data;
16568 bfd_vma plt_offset;
16569 unsigned int other;
16570 bfd_vma entry_size;
16571 bfd_vma plt0_size;
16572 asection *relplt;
16573 bfd_vma opcode;
16574 asection *plt;
16575 asymbol *send;
16576 size_t size;
16577 char *names;
16578 long counti;
16579 arelent *p;
16580 asymbol *s;
16581 char *nend;
16582 long count;
16583 long pi;
16584 long i;
16585 long n;
16586
16587 *ret = NULL;
16588
16589 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16590 return 0;
16591
16592 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16593 if (relplt == NULL)
16594 return 0;
16595
16596 hdr = &elf_section_data (relplt)->this_hdr;
16597 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16598 return 0;
16599
16600 plt = bfd_get_section_by_name (abfd, ".plt");
16601 if (plt == NULL || (plt->flags & SEC_HAS_CONTENTS) == 0)
16602 return 0;
16603
16604 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16605 if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16606 return -1;
16607 p = relplt->relocation;
16608
16609 /* Calculating the exact amount of space required for symbols would
16610 require two passes over the PLT, so just pessimise assuming two
16611 PLT slots per relocation. */
16612 count = NUM_SHDR_ENTRIES (hdr);
16613 counti = count * bed->s->int_rels_per_ext_rel;
16614 size = 2 * count * sizeof (asymbol);
16615 size += count * (sizeof (mipssuffix) +
16616 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16617 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16618 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16619
16620 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16621 size += sizeof (asymbol) + sizeof (pltname);
16622
16623 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16624 return -1;
16625
16626 if (plt->size < 16)
16627 return -1;
16628
16629 s = *ret = bfd_malloc (size);
16630 if (s == NULL)
16631 return -1;
16632 send = s + 2 * count + 1;
16633
16634 names = (char *) send;
16635 nend = (char *) s + size;
16636 n = 0;
16637
16638 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16639 if (opcode == 0x3302fffe)
16640 {
16641 if (!micromips_p)
16642 return -1;
16643 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16644 other = STO_MICROMIPS;
16645 }
16646 else if (opcode == 0x0398c1d0)
16647 {
16648 if (!micromips_p)
16649 return -1;
16650 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16651 other = STO_MICROMIPS;
16652 }
16653 else
16654 {
16655 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16656 other = 0;
16657 }
16658
16659 s->the_bfd = abfd;
16660 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16661 s->section = plt;
16662 s->value = 0;
16663 s->name = names;
16664 s->udata.i = other;
16665 memcpy (names, pltname, sizeof (pltname));
16666 names += sizeof (pltname);
16667 ++s, ++n;
16668
16669 pi = 0;
16670 for (plt_offset = plt0_size;
16671 plt_offset + 8 <= plt->size && s < send;
16672 plt_offset += entry_size)
16673 {
16674 bfd_vma gotplt_addr;
16675 const char *suffix;
16676 bfd_vma gotplt_hi;
16677 bfd_vma gotplt_lo;
16678 size_t suffixlen;
16679
16680 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16681
16682 /* Check if the second word matches the expected MIPS16 instruction. */
16683 if (opcode == 0x651aeb00)
16684 {
16685 if (micromips_p)
16686 return -1;
16687 /* Truncated table??? */
16688 if (plt_offset + 16 > plt->size)
16689 break;
16690 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16691 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16692 suffixlen = sizeof (m16suffix);
16693 suffix = m16suffix;
16694 other = STO_MIPS16;
16695 }
16696 /* Likewise the expected microMIPS instruction (no insn32 mode). */
16697 else if (opcode == 0xff220000)
16698 {
16699 if (!micromips_p)
16700 return -1;
16701 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16702 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16703 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16704 gotplt_lo <<= 2;
16705 gotplt_addr = gotplt_hi + gotplt_lo;
16706 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16707 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16708 suffixlen = sizeof (microsuffix);
16709 suffix = microsuffix;
16710 other = STO_MICROMIPS;
16711 }
16712 /* Likewise the expected microMIPS instruction (insn32 mode). */
16713 else if ((opcode & 0xffff0000) == 0xff2f0000)
16714 {
16715 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16716 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16717 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16718 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16719 gotplt_addr = gotplt_hi + gotplt_lo;
16720 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16721 suffixlen = sizeof (microsuffix);
16722 suffix = microsuffix;
16723 other = STO_MICROMIPS;
16724 }
16725 /* Otherwise assume standard MIPS code. */
16726 else
16727 {
16728 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16729 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16730 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16731 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16732 gotplt_addr = gotplt_hi + gotplt_lo;
16733 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16734 suffixlen = sizeof (mipssuffix);
16735 suffix = mipssuffix;
16736 other = 0;
16737 }
16738 /* Truncated table??? */
16739 if (plt_offset + entry_size > plt->size)
16740 break;
16741
16742 for (i = 0;
16743 i < count && p[pi].address != gotplt_addr;
16744 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16745
16746 if (i < count)
16747 {
16748 size_t namelen;
16749 size_t len;
16750
16751 *s = **p[pi].sym_ptr_ptr;
16752 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16753 we are defining a symbol, ensure one of them is set. */
16754 if ((s->flags & BSF_LOCAL) == 0)
16755 s->flags |= BSF_GLOBAL;
16756 s->flags |= BSF_SYNTHETIC;
16757 s->section = plt;
16758 s->value = plt_offset;
16759 s->name = names;
16760 s->udata.i = other;
16761
16762 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16763 namelen = len + suffixlen;
16764 if (names + namelen > nend)
16765 break;
16766
16767 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16768 names += len;
16769 memcpy (names, suffix, suffixlen);
16770 names += suffixlen;
16771
16772 ++s, ++n;
16773 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16774 }
16775 }
16776
16777 free (plt_data);
16778
16779 return n;
16780 }
16781
16782 /* Return the ABI flags associated with ABFD if available. */
16783
16784 Elf_Internal_ABIFlags_v0 *
16785 bfd_mips_elf_get_abiflags (bfd *abfd)
16786 {
16787 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16788
16789 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16790 }
16791
16792 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16793 field. Taken from `libc-abis.h' generated at GNU libc build time.
16794 Using a MIPS_ prefix as other libc targets use different values. */
16795 enum
16796 {
16797 MIPS_LIBC_ABI_DEFAULT = 0,
16798 MIPS_LIBC_ABI_MIPS_PLT,
16799 MIPS_LIBC_ABI_UNIQUE,
16800 MIPS_LIBC_ABI_MIPS_O32_FP64,
16801 MIPS_LIBC_ABI_ABSOLUTE,
16802 MIPS_LIBC_ABI_XHASH,
16803 MIPS_LIBC_ABI_MAX
16804 };
16805
16806 bool
16807 _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16808 {
16809 struct mips_elf_link_hash_table *htab = NULL;
16810 Elf_Internal_Ehdr *i_ehdrp;
16811
16812 if (!_bfd_elf_init_file_header (abfd, link_info))
16813 return false;
16814
16815 i_ehdrp = elf_elfheader (abfd);
16816 if (link_info)
16817 {
16818 htab = mips_elf_hash_table (link_info);
16819 BFD_ASSERT (htab != NULL);
16820 }
16821
16822 if (htab != NULL
16823 && htab->use_plts_and_copy_relocs
16824 && htab->root.target_os != is_vxworks)
16825 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16826
16827 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16828 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16829 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16830
16831 /* Mark that we need support for absolute symbols in the dynamic loader. */
16832 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16833 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16834
16835 /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16836 if it is the only hash section that will be created. */
16837 if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16838 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16839 return true;
16840 }
16841
16842 int
16843 _bfd_mips_elf_compact_eh_encoding
16844 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16845 {
16846 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16847 }
16848
16849 /* Return the opcode for can't unwind. */
16850
16851 int
16852 _bfd_mips_elf_cant_unwind_opcode
16853 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16854 {
16855 return COMPACT_EH_CANT_UNWIND_OPCODE;
16856 }
16857
16858 /* Record a position XLAT_LOC in the xlat translation table, associated with
16859 the hash entry H. The entry in the translation table will later be
16860 populated with the real symbol dynindx. */
16861
16862 void
16863 _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16864 bfd_vma xlat_loc)
16865 {
16866 struct mips_elf_link_hash_entry *hmips;
16867
16868 hmips = (struct mips_elf_link_hash_entry *) h;
16869 hmips->mipsxhash_loc = xlat_loc;
16870 }