]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elfxx-mips.c
bfd_get_relocated_section_contents allow NULL data buffer
[thirdparty/binutils-gdb.git] / bfd / elfxx-mips.c
1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2022 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 bool
1384 _bfd_mips_elf_close_and_cleanup (bfd *abfd)
1385 {
1386 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
1387 if (tdata != NULL && bfd_get_format (abfd) == bfd_object)
1388 {
1389 BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1390 while (tdata->mips_hi16_list != NULL)
1391 {
1392 struct mips_hi16 *hi = tdata->mips_hi16_list;
1393 tdata->mips_hi16_list = hi->next;
1394 free (hi);
1395 }
1396 }
1397 return _bfd_elf_close_and_cleanup (abfd);
1398 }
1399
1400 bool
1401 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1402 {
1403 if (!sec->used_by_bfd)
1404 {
1405 struct _mips_elf_section_data *sdata;
1406 size_t amt = sizeof (*sdata);
1407
1408 sdata = bfd_zalloc (abfd, amt);
1409 if (sdata == NULL)
1410 return false;
1411 sec->used_by_bfd = sdata;
1412 }
1413
1414 return _bfd_elf_new_section_hook (abfd, sec);
1415 }
1416 \f
1417 /* Read ECOFF debugging information from a .mdebug section into a
1418 ecoff_debug_info structure. */
1419
1420 bool
1421 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1422 struct ecoff_debug_info *debug)
1423 {
1424 HDRR *symhdr;
1425 const struct ecoff_debug_swap *swap;
1426 char *ext_hdr;
1427
1428 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1429 memset (debug, 0, sizeof (*debug));
1430
1431 ext_hdr = bfd_malloc (swap->external_hdr_size);
1432 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1433 goto error_return;
1434
1435 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1436 swap->external_hdr_size))
1437 goto error_return;
1438
1439 symhdr = &debug->symbolic_header;
1440 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1441
1442 /* The symbolic header contains absolute file offsets and sizes to
1443 read. */
1444 #define READ(ptr, offset, count, size, type) \
1445 do \
1446 { \
1447 size_t amt; \
1448 debug->ptr = NULL; \
1449 if (symhdr->count == 0) \
1450 break; \
1451 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
1452 { \
1453 bfd_set_error (bfd_error_file_too_big); \
1454 goto error_return; \
1455 } \
1456 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
1457 goto error_return; \
1458 debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \
1459 if (debug->ptr == NULL) \
1460 goto error_return; \
1461 } while (0)
1462
1463 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1464 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1465 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1466 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1467 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1468 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1469 union aux_ext *);
1470 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1471 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1472 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1473 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1474 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1475 #undef READ
1476
1477 debug->fdr = NULL;
1478
1479 return true;
1480
1481 error_return:
1482 free (ext_hdr);
1483 free (debug->line);
1484 free (debug->external_dnr);
1485 free (debug->external_pdr);
1486 free (debug->external_sym);
1487 free (debug->external_opt);
1488 free (debug->external_aux);
1489 free (debug->ss);
1490 free (debug->ssext);
1491 free (debug->external_fdr);
1492 free (debug->external_rfd);
1493 free (debug->external_ext);
1494 return false;
1495 }
1496 \f
1497 /* Swap RPDR (runtime procedure table entry) for output. */
1498
1499 static void
1500 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1501 {
1502 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1503 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1504 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1505 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1506 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1507 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1508
1509 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1510 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1511
1512 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1513 }
1514
1515 /* Create a runtime procedure table from the .mdebug section. */
1516
1517 static bool
1518 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1519 struct bfd_link_info *info, asection *s,
1520 struct ecoff_debug_info *debug)
1521 {
1522 const struct ecoff_debug_swap *swap;
1523 HDRR *hdr = &debug->symbolic_header;
1524 RPDR *rpdr, *rp;
1525 struct rpdr_ext *erp;
1526 void *rtproc;
1527 struct pdr_ext *epdr;
1528 struct sym_ext *esym;
1529 char *ss, **sv;
1530 char *str;
1531 bfd_size_type size;
1532 bfd_size_type count;
1533 unsigned long sindex;
1534 unsigned long i;
1535 PDR pdr;
1536 SYMR sym;
1537 const char *no_name_func = _("static procedure (no name)");
1538
1539 epdr = NULL;
1540 rpdr = NULL;
1541 esym = NULL;
1542 ss = NULL;
1543 sv = NULL;
1544
1545 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1546
1547 sindex = strlen (no_name_func) + 1;
1548 count = hdr->ipdMax;
1549 if (count > 0)
1550 {
1551 size = swap->external_pdr_size;
1552
1553 epdr = bfd_malloc (size * count);
1554 if (epdr == NULL)
1555 goto error_return;
1556
1557 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1558 goto error_return;
1559
1560 size = sizeof (RPDR);
1561 rp = rpdr = bfd_malloc (size * count);
1562 if (rpdr == NULL)
1563 goto error_return;
1564
1565 size = sizeof (char *);
1566 sv = bfd_malloc (size * count);
1567 if (sv == NULL)
1568 goto error_return;
1569
1570 count = hdr->isymMax;
1571 size = swap->external_sym_size;
1572 esym = bfd_malloc (size * count);
1573 if (esym == NULL)
1574 goto error_return;
1575
1576 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1577 goto error_return;
1578
1579 count = hdr->issMax;
1580 ss = bfd_malloc (count);
1581 if (ss == NULL)
1582 goto error_return;
1583 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1584 goto error_return;
1585
1586 count = hdr->ipdMax;
1587 for (i = 0; i < (unsigned long) count; i++, rp++)
1588 {
1589 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1590 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1591 rp->adr = sym.value;
1592 rp->regmask = pdr.regmask;
1593 rp->regoffset = pdr.regoffset;
1594 rp->fregmask = pdr.fregmask;
1595 rp->fregoffset = pdr.fregoffset;
1596 rp->frameoffset = pdr.frameoffset;
1597 rp->framereg = pdr.framereg;
1598 rp->pcreg = pdr.pcreg;
1599 rp->irpss = sindex;
1600 sv[i] = ss + sym.iss;
1601 sindex += strlen (sv[i]) + 1;
1602 }
1603 }
1604
1605 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1606 size = BFD_ALIGN (size, 16);
1607 rtproc = bfd_alloc (abfd, size);
1608 if (rtproc == NULL)
1609 {
1610 mips_elf_hash_table (info)->procedure_count = 0;
1611 goto error_return;
1612 }
1613
1614 mips_elf_hash_table (info)->procedure_count = count + 2;
1615
1616 erp = rtproc;
1617 memset (erp, 0, sizeof (struct rpdr_ext));
1618 erp++;
1619 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1620 strcpy (str, no_name_func);
1621 str += strlen (no_name_func) + 1;
1622 for (i = 0; i < count; i++)
1623 {
1624 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1625 strcpy (str, sv[i]);
1626 str += strlen (sv[i]) + 1;
1627 }
1628 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1629
1630 /* Set the size and contents of .rtproc section. */
1631 s->size = size;
1632 s->contents = rtproc;
1633
1634 /* Skip this section later on (I don't think this currently
1635 matters, but someday it might). */
1636 s->map_head.link_order = NULL;
1637
1638 free (epdr);
1639 free (rpdr);
1640 free (esym);
1641 free (ss);
1642 free (sv);
1643 return true;
1644
1645 error_return:
1646 free (epdr);
1647 free (rpdr);
1648 free (esym);
1649 free (ss);
1650 free (sv);
1651 return false;
1652 }
1653 \f
1654 /* We're going to create a stub for H. Create a symbol for the stub's
1655 value and size, to help make the disassembly easier to read. */
1656
1657 static bool
1658 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1659 struct mips_elf_link_hash_entry *h,
1660 const char *prefix, asection *s, bfd_vma value,
1661 bfd_vma size)
1662 {
1663 bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1664 struct bfd_link_hash_entry *bh;
1665 struct elf_link_hash_entry *elfh;
1666 char *name;
1667 bool res;
1668
1669 if (micromips_p)
1670 value |= 1;
1671
1672 /* Create a new symbol. */
1673 name = concat (prefix, h->root.root.root.string, NULL);
1674 bh = NULL;
1675 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1676 BSF_LOCAL, s, value, NULL,
1677 true, false, &bh);
1678 free (name);
1679 if (! res)
1680 return false;
1681
1682 /* Make it a local function. */
1683 elfh = (struct elf_link_hash_entry *) bh;
1684 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1685 elfh->size = size;
1686 elfh->forced_local = 1;
1687 if (micromips_p)
1688 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1689 return true;
1690 }
1691
1692 /* We're about to redefine H. Create a symbol to represent H's
1693 current value and size, to help make the disassembly easier
1694 to read. */
1695
1696 static bool
1697 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1698 struct mips_elf_link_hash_entry *h,
1699 const char *prefix)
1700 {
1701 struct bfd_link_hash_entry *bh;
1702 struct elf_link_hash_entry *elfh;
1703 char *name;
1704 asection *s;
1705 bfd_vma value;
1706 bool res;
1707
1708 /* Read the symbol's value. */
1709 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1710 || h->root.root.type == bfd_link_hash_defweak);
1711 s = h->root.root.u.def.section;
1712 value = h->root.root.u.def.value;
1713
1714 /* Create a new symbol. */
1715 name = concat (prefix, h->root.root.root.string, NULL);
1716 bh = NULL;
1717 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1718 BSF_LOCAL, s, value, NULL,
1719 true, false, &bh);
1720 free (name);
1721 if (! res)
1722 return false;
1723
1724 /* Make it local and copy the other attributes from H. */
1725 elfh = (struct elf_link_hash_entry *) bh;
1726 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1727 elfh->other = h->root.other;
1728 elfh->size = h->root.size;
1729 elfh->forced_local = 1;
1730 return true;
1731 }
1732
1733 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1734 function rather than to a hard-float stub. */
1735
1736 static bool
1737 section_allows_mips16_refs_p (asection *section)
1738 {
1739 const char *name;
1740
1741 name = bfd_section_name (section);
1742 return (FN_STUB_P (name)
1743 || CALL_STUB_P (name)
1744 || CALL_FP_STUB_P (name)
1745 || strcmp (name, ".pdr") == 0);
1746 }
1747
1748 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1749 stub section of some kind. Return the R_SYMNDX of the target
1750 function, or 0 if we can't decide which function that is. */
1751
1752 static unsigned long
1753 mips16_stub_symndx (const struct elf_backend_data *bed,
1754 asection *sec ATTRIBUTE_UNUSED,
1755 const Elf_Internal_Rela *relocs,
1756 const Elf_Internal_Rela *relend)
1757 {
1758 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1759 const Elf_Internal_Rela *rel;
1760
1761 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1762 one in a compound relocation. */
1763 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1764 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1765 return ELF_R_SYM (sec->owner, rel->r_info);
1766
1767 /* Otherwise trust the first relocation, whatever its kind. This is
1768 the traditional behavior. */
1769 if (relocs < relend)
1770 return ELF_R_SYM (sec->owner, relocs->r_info);
1771
1772 return 0;
1773 }
1774
1775 /* Check the mips16 stubs for a particular symbol, and see if we can
1776 discard them. */
1777
1778 static void
1779 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1780 struct mips_elf_link_hash_entry *h)
1781 {
1782 /* Dynamic symbols must use the standard call interface, in case other
1783 objects try to call them. */
1784 if (h->fn_stub != NULL
1785 && h->root.dynindx != -1)
1786 {
1787 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1788 h->need_fn_stub = true;
1789 }
1790
1791 if (h->fn_stub != NULL
1792 && ! h->need_fn_stub)
1793 {
1794 /* We don't need the fn_stub; the only references to this symbol
1795 are 16 bit calls. Clobber the size to 0 to prevent it from
1796 being included in the link. */
1797 h->fn_stub->size = 0;
1798 h->fn_stub->flags &= ~SEC_RELOC;
1799 h->fn_stub->reloc_count = 0;
1800 h->fn_stub->flags |= SEC_EXCLUDE;
1801 h->fn_stub->output_section = bfd_abs_section_ptr;
1802 }
1803
1804 if (h->call_stub != NULL
1805 && ELF_ST_IS_MIPS16 (h->root.other))
1806 {
1807 /* We don't need the call_stub; this is a 16 bit function, so
1808 calls from other 16 bit functions are OK. Clobber the size
1809 to 0 to prevent it from being included in the link. */
1810 h->call_stub->size = 0;
1811 h->call_stub->flags &= ~SEC_RELOC;
1812 h->call_stub->reloc_count = 0;
1813 h->call_stub->flags |= SEC_EXCLUDE;
1814 h->call_stub->output_section = bfd_abs_section_ptr;
1815 }
1816
1817 if (h->call_fp_stub != NULL
1818 && ELF_ST_IS_MIPS16 (h->root.other))
1819 {
1820 /* We don't need the call_stub; this is a 16 bit function, so
1821 calls from other 16 bit functions are OK. Clobber the size
1822 to 0 to prevent it from being included in the link. */
1823 h->call_fp_stub->size = 0;
1824 h->call_fp_stub->flags &= ~SEC_RELOC;
1825 h->call_fp_stub->reloc_count = 0;
1826 h->call_fp_stub->flags |= SEC_EXCLUDE;
1827 h->call_fp_stub->output_section = bfd_abs_section_ptr;
1828 }
1829 }
1830
1831 /* Hashtable callbacks for mips_elf_la25_stubs. */
1832
1833 static hashval_t
1834 mips_elf_la25_stub_hash (const void *entry_)
1835 {
1836 const struct mips_elf_la25_stub *entry;
1837
1838 entry = (struct mips_elf_la25_stub *) entry_;
1839 return entry->h->root.root.u.def.section->id
1840 + entry->h->root.root.u.def.value;
1841 }
1842
1843 static int
1844 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1845 {
1846 const struct mips_elf_la25_stub *entry1, *entry2;
1847
1848 entry1 = (struct mips_elf_la25_stub *) entry1_;
1849 entry2 = (struct mips_elf_la25_stub *) entry2_;
1850 return ((entry1->h->root.root.u.def.section
1851 == entry2->h->root.root.u.def.section)
1852 && (entry1->h->root.root.u.def.value
1853 == entry2->h->root.root.u.def.value));
1854 }
1855
1856 /* Called by the linker to set up the la25 stub-creation code. FN is
1857 the linker's implementation of add_stub_function. Return true on
1858 success. */
1859
1860 bool
1861 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1862 asection *(*fn) (const char *, asection *,
1863 asection *))
1864 {
1865 struct mips_elf_link_hash_table *htab;
1866
1867 htab = mips_elf_hash_table (info);
1868 if (htab == NULL)
1869 return false;
1870
1871 htab->add_stub_section = fn;
1872 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1873 mips_elf_la25_stub_eq, NULL);
1874 if (htab->la25_stubs == NULL)
1875 return false;
1876
1877 return true;
1878 }
1879
1880 /* Return true if H is a locally-defined PIC function, in the sense
1881 that it or its fn_stub might need $25 to be valid on entry.
1882 Note that MIPS16 functions set up $gp using PC-relative instructions,
1883 so they themselves never need $25 to be valid. Only non-MIPS16
1884 entry points are of interest here. */
1885
1886 static bool
1887 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1888 {
1889 return ((h->root.root.type == bfd_link_hash_defined
1890 || h->root.root.type == bfd_link_hash_defweak)
1891 && h->root.def_regular
1892 && !bfd_is_abs_section (h->root.root.u.def.section)
1893 && !bfd_is_und_section (h->root.root.u.def.section)
1894 && (!ELF_ST_IS_MIPS16 (h->root.other)
1895 || (h->fn_stub && h->need_fn_stub))
1896 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1897 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1898 }
1899
1900 /* Set *SEC to the input section that contains the target of STUB.
1901 Return the offset of the target from the start of that section. */
1902
1903 static bfd_vma
1904 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1905 asection **sec)
1906 {
1907 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1908 {
1909 BFD_ASSERT (stub->h->need_fn_stub);
1910 *sec = stub->h->fn_stub;
1911 return 0;
1912 }
1913 else
1914 {
1915 *sec = stub->h->root.root.u.def.section;
1916 return stub->h->root.root.u.def.value;
1917 }
1918 }
1919
1920 /* STUB describes an la25 stub that we have decided to implement
1921 by inserting an LUI/ADDIU pair before the target function.
1922 Create the section and redirect the function symbol to it. */
1923
1924 static bool
1925 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1926 struct bfd_link_info *info)
1927 {
1928 struct mips_elf_link_hash_table *htab;
1929 char *name;
1930 asection *s, *input_section;
1931 unsigned int align;
1932
1933 htab = mips_elf_hash_table (info);
1934 if (htab == NULL)
1935 return false;
1936
1937 /* Create a unique name for the new section. */
1938 name = bfd_malloc (11 + sizeof (".text.stub."));
1939 if (name == NULL)
1940 return false;
1941 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1942
1943 /* Create the section. */
1944 mips_elf_get_la25_target (stub, &input_section);
1945 s = htab->add_stub_section (name, input_section,
1946 input_section->output_section);
1947 if (s == NULL)
1948 return false;
1949
1950 /* Make sure that any padding goes before the stub. */
1951 align = input_section->alignment_power;
1952 if (!bfd_set_section_alignment (s, align))
1953 return false;
1954 if (align > 3)
1955 s->size = (1 << align) - 8;
1956
1957 /* Create a symbol for the stub. */
1958 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1959 stub->stub_section = s;
1960 stub->offset = s->size;
1961
1962 /* Allocate room for it. */
1963 s->size += 8;
1964 return true;
1965 }
1966
1967 /* STUB describes an la25 stub that we have decided to implement
1968 with a separate trampoline. Allocate room for it and redirect
1969 the function symbol to it. */
1970
1971 static bool
1972 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1973 struct bfd_link_info *info)
1974 {
1975 struct mips_elf_link_hash_table *htab;
1976 asection *s;
1977
1978 htab = mips_elf_hash_table (info);
1979 if (htab == NULL)
1980 return false;
1981
1982 /* Create a trampoline section, if we haven't already. */
1983 s = htab->strampoline;
1984 if (s == NULL)
1985 {
1986 asection *input_section = stub->h->root.root.u.def.section;
1987 s = htab->add_stub_section (".text", NULL,
1988 input_section->output_section);
1989 if (s == NULL || !bfd_set_section_alignment (s, 4))
1990 return false;
1991 htab->strampoline = s;
1992 }
1993
1994 /* Create a symbol for the stub. */
1995 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1996 stub->stub_section = s;
1997 stub->offset = s->size;
1998
1999 /* Allocate room for it. */
2000 s->size += 16;
2001 return true;
2002 }
2003
2004 /* H describes a symbol that needs an la25 stub. Make sure that an
2005 appropriate stub exists and point H at it. */
2006
2007 static bool
2008 mips_elf_add_la25_stub (struct bfd_link_info *info,
2009 struct mips_elf_link_hash_entry *h)
2010 {
2011 struct mips_elf_link_hash_table *htab;
2012 struct mips_elf_la25_stub search, *stub;
2013 bool use_trampoline_p;
2014 asection *s;
2015 bfd_vma value;
2016 void **slot;
2017
2018 /* Describe the stub we want. */
2019 search.stub_section = NULL;
2020 search.offset = 0;
2021 search.h = h;
2022
2023 /* See if we've already created an equivalent stub. */
2024 htab = mips_elf_hash_table (info);
2025 if (htab == NULL)
2026 return false;
2027
2028 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2029 if (slot == NULL)
2030 return false;
2031
2032 stub = (struct mips_elf_la25_stub *) *slot;
2033 if (stub != NULL)
2034 {
2035 /* We can reuse the existing stub. */
2036 h->la25_stub = stub;
2037 return true;
2038 }
2039
2040 /* Create a permanent copy of ENTRY and add it to the hash table. */
2041 stub = bfd_malloc (sizeof (search));
2042 if (stub == NULL)
2043 return false;
2044 *stub = search;
2045 *slot = stub;
2046
2047 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2048 of the section and if we would need no more than 2 nops. */
2049 value = mips_elf_get_la25_target (stub, &s);
2050 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2051 value &= ~1;
2052 use_trampoline_p = (value != 0 || s->alignment_power > 4);
2053
2054 h->la25_stub = stub;
2055 return (use_trampoline_p
2056 ? mips_elf_add_la25_trampoline (stub, info)
2057 : mips_elf_add_la25_intro (stub, info));
2058 }
2059
2060 /* A mips_elf_link_hash_traverse callback that is called before sizing
2061 sections. DATA points to a mips_htab_traverse_info structure. */
2062
2063 static bool
2064 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2065 {
2066 struct mips_htab_traverse_info *hti;
2067
2068 hti = (struct mips_htab_traverse_info *) data;
2069 if (!bfd_link_relocatable (hti->info))
2070 mips_elf_check_mips16_stubs (hti->info, h);
2071
2072 if (mips_elf_local_pic_function_p (h))
2073 {
2074 /* PR 12845: If H is in a section that has been garbage
2075 collected it will have its output section set to *ABS*. */
2076 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2077 return true;
2078
2079 /* H is a function that might need $25 to be valid on entry.
2080 If we're creating a non-PIC relocatable object, mark H as
2081 being PIC. If we're creating a non-relocatable object with
2082 non-PIC branches and jumps to H, make sure that H has an la25
2083 stub. */
2084 if (bfd_link_relocatable (hti->info))
2085 {
2086 if (!PIC_OBJECT_P (hti->output_bfd))
2087 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2088 }
2089 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2090 {
2091 hti->error = true;
2092 return false;
2093 }
2094 }
2095 return true;
2096 }
2097 \f
2098 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2099 Most mips16 instructions are 16 bits, but these instructions
2100 are 32 bits.
2101
2102 The format of these instructions is:
2103
2104 +--------------+--------------------------------+
2105 | JALX | X| Imm 20:16 | Imm 25:21 |
2106 +--------------+--------------------------------+
2107 | Immediate 15:0 |
2108 +-----------------------------------------------+
2109
2110 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2111 Note that the immediate value in the first word is swapped.
2112
2113 When producing a relocatable object file, R_MIPS16_26 is
2114 handled mostly like R_MIPS_26. In particular, the addend is
2115 stored as a straight 26-bit value in a 32-bit instruction.
2116 (gas makes life simpler for itself by never adjusting a
2117 R_MIPS16_26 reloc to be against a section, so the addend is
2118 always zero). However, the 32 bit instruction is stored as 2
2119 16-bit values, rather than a single 32-bit value. In a
2120 big-endian file, the result is the same; in a little-endian
2121 file, the two 16-bit halves of the 32 bit value are swapped.
2122 This is so that a disassembler can recognize the jal
2123 instruction.
2124
2125 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2126 instruction stored as two 16-bit values. The addend A is the
2127 contents of the targ26 field. The calculation is the same as
2128 R_MIPS_26. When storing the calculated value, reorder the
2129 immediate value as shown above, and don't forget to store the
2130 value as two 16-bit values.
2131
2132 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2133 defined as
2134
2135 big-endian:
2136 +--------+----------------------+
2137 | | |
2138 | | targ26-16 |
2139 |31 26|25 0|
2140 +--------+----------------------+
2141
2142 little-endian:
2143 +----------+------+-------------+
2144 | | | |
2145 | sub1 | | sub2 |
2146 |0 9|10 15|16 31|
2147 +----------+--------------------+
2148 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2149 ((sub1 << 16) | sub2)).
2150
2151 When producing a relocatable object file, the calculation is
2152 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2153 When producing a fully linked file, the calculation is
2154 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2155 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2156
2157 The table below lists the other MIPS16 instruction relocations.
2158 Each one is calculated in the same way as the non-MIPS16 relocation
2159 given on the right, but using the extended MIPS16 layout of 16-bit
2160 immediate fields:
2161
2162 R_MIPS16_GPREL R_MIPS_GPREL16
2163 R_MIPS16_GOT16 R_MIPS_GOT16
2164 R_MIPS16_CALL16 R_MIPS_CALL16
2165 R_MIPS16_HI16 R_MIPS_HI16
2166 R_MIPS16_LO16 R_MIPS_LO16
2167
2168 A typical instruction will have a format like this:
2169
2170 +--------------+--------------------------------+
2171 | EXTEND | Imm 10:5 | Imm 15:11 |
2172 +--------------+--------------------------------+
2173 | Major | rx | ry | Imm 4:0 |
2174 +--------------+--------------------------------+
2175
2176 EXTEND is the five bit value 11110. Major is the instruction
2177 opcode.
2178
2179 All we need to do here is shuffle the bits appropriately.
2180 As above, the two 16-bit halves must be swapped on a
2181 little-endian system.
2182
2183 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2184 relocatable field is shifted by 1 rather than 2 and the same bit
2185 shuffling is done as with the relocations above. */
2186
2187 static inline bool
2188 mips16_reloc_p (int r_type)
2189 {
2190 switch (r_type)
2191 {
2192 case R_MIPS16_26:
2193 case R_MIPS16_GPREL:
2194 case R_MIPS16_GOT16:
2195 case R_MIPS16_CALL16:
2196 case R_MIPS16_HI16:
2197 case R_MIPS16_LO16:
2198 case R_MIPS16_TLS_GD:
2199 case R_MIPS16_TLS_LDM:
2200 case R_MIPS16_TLS_DTPREL_HI16:
2201 case R_MIPS16_TLS_DTPREL_LO16:
2202 case R_MIPS16_TLS_GOTTPREL:
2203 case R_MIPS16_TLS_TPREL_HI16:
2204 case R_MIPS16_TLS_TPREL_LO16:
2205 case R_MIPS16_PC16_S1:
2206 return true;
2207
2208 default:
2209 return false;
2210 }
2211 }
2212
2213 /* Check if a microMIPS reloc. */
2214
2215 static inline bool
2216 micromips_reloc_p (unsigned int r_type)
2217 {
2218 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2219 }
2220
2221 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2222 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2223 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2224
2225 static inline bool
2226 micromips_reloc_shuffle_p (unsigned int r_type)
2227 {
2228 return (micromips_reloc_p (r_type)
2229 && r_type != R_MICROMIPS_PC7_S1
2230 && r_type != R_MICROMIPS_PC10_S1);
2231 }
2232
2233 static inline bool
2234 got16_reloc_p (int r_type)
2235 {
2236 return (r_type == R_MIPS_GOT16
2237 || r_type == R_MIPS16_GOT16
2238 || r_type == R_MICROMIPS_GOT16);
2239 }
2240
2241 static inline bool
2242 call16_reloc_p (int r_type)
2243 {
2244 return (r_type == R_MIPS_CALL16
2245 || r_type == R_MIPS16_CALL16
2246 || r_type == R_MICROMIPS_CALL16);
2247 }
2248
2249 static inline bool
2250 got_disp_reloc_p (unsigned int r_type)
2251 {
2252 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2253 }
2254
2255 static inline bool
2256 got_page_reloc_p (unsigned int r_type)
2257 {
2258 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2259 }
2260
2261 static inline bool
2262 got_lo16_reloc_p (unsigned int r_type)
2263 {
2264 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2265 }
2266
2267 static inline bool
2268 call_hi16_reloc_p (unsigned int r_type)
2269 {
2270 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2271 }
2272
2273 static inline bool
2274 call_lo16_reloc_p (unsigned int r_type)
2275 {
2276 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2277 }
2278
2279 static inline bool
2280 hi16_reloc_p (int r_type)
2281 {
2282 return (r_type == R_MIPS_HI16
2283 || r_type == R_MIPS16_HI16
2284 || r_type == R_MICROMIPS_HI16
2285 || r_type == R_MIPS_PCHI16);
2286 }
2287
2288 static inline bool
2289 lo16_reloc_p (int r_type)
2290 {
2291 return (r_type == R_MIPS_LO16
2292 || r_type == R_MIPS16_LO16
2293 || r_type == R_MICROMIPS_LO16
2294 || r_type == R_MIPS_PCLO16);
2295 }
2296
2297 static inline bool
2298 mips16_call_reloc_p (int r_type)
2299 {
2300 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2301 }
2302
2303 static inline bool
2304 jal_reloc_p (int r_type)
2305 {
2306 return (r_type == R_MIPS_26
2307 || r_type == R_MIPS16_26
2308 || r_type == R_MICROMIPS_26_S1);
2309 }
2310
2311 static inline bool
2312 b_reloc_p (int r_type)
2313 {
2314 return (r_type == R_MIPS_PC26_S2
2315 || r_type == R_MIPS_PC21_S2
2316 || r_type == R_MIPS_PC16
2317 || r_type == R_MIPS_GNU_REL16_S2
2318 || r_type == R_MIPS16_PC16_S1
2319 || r_type == R_MICROMIPS_PC16_S1
2320 || r_type == R_MICROMIPS_PC10_S1
2321 || r_type == R_MICROMIPS_PC7_S1);
2322 }
2323
2324 static inline bool
2325 aligned_pcrel_reloc_p (int r_type)
2326 {
2327 return (r_type == R_MIPS_PC18_S3
2328 || r_type == R_MIPS_PC19_S2);
2329 }
2330
2331 static inline bool
2332 branch_reloc_p (int r_type)
2333 {
2334 return (r_type == R_MIPS_26
2335 || r_type == R_MIPS_PC26_S2
2336 || r_type == R_MIPS_PC21_S2
2337 || r_type == R_MIPS_PC16
2338 || r_type == R_MIPS_GNU_REL16_S2);
2339 }
2340
2341 static inline bool
2342 mips16_branch_reloc_p (int r_type)
2343 {
2344 return (r_type == R_MIPS16_26
2345 || r_type == R_MIPS16_PC16_S1);
2346 }
2347
2348 static inline bool
2349 micromips_branch_reloc_p (int r_type)
2350 {
2351 return (r_type == R_MICROMIPS_26_S1
2352 || r_type == R_MICROMIPS_PC16_S1
2353 || r_type == R_MICROMIPS_PC10_S1
2354 || r_type == R_MICROMIPS_PC7_S1);
2355 }
2356
2357 static inline bool
2358 tls_gd_reloc_p (unsigned int r_type)
2359 {
2360 return (r_type == R_MIPS_TLS_GD
2361 || r_type == R_MIPS16_TLS_GD
2362 || r_type == R_MICROMIPS_TLS_GD);
2363 }
2364
2365 static inline bool
2366 tls_ldm_reloc_p (unsigned int r_type)
2367 {
2368 return (r_type == R_MIPS_TLS_LDM
2369 || r_type == R_MIPS16_TLS_LDM
2370 || r_type == R_MICROMIPS_TLS_LDM);
2371 }
2372
2373 static inline bool
2374 tls_gottprel_reloc_p (unsigned int r_type)
2375 {
2376 return (r_type == R_MIPS_TLS_GOTTPREL
2377 || r_type == R_MIPS16_TLS_GOTTPREL
2378 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2379 }
2380
2381 static inline bool
2382 needs_shuffle (int r_type)
2383 {
2384 return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2385 }
2386
2387 void
2388 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2389 bool jal_shuffle, bfd_byte *data)
2390 {
2391 bfd_vma first, second, val;
2392
2393 if (!needs_shuffle (r_type))
2394 return;
2395
2396 /* Pick up the first and second halfwords of the instruction. */
2397 first = bfd_get_16 (abfd, data);
2398 second = bfd_get_16 (abfd, data + 2);
2399 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2400 val = first << 16 | second;
2401 else if (r_type != R_MIPS16_26)
2402 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2403 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2404 else
2405 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2406 | ((first & 0x1f) << 21) | second);
2407 bfd_put_32 (abfd, val, data);
2408 }
2409
2410 void
2411 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2412 bool jal_shuffle, bfd_byte *data)
2413 {
2414 bfd_vma first, second, val;
2415
2416 if (!needs_shuffle (r_type))
2417 return;
2418
2419 val = bfd_get_32 (abfd, data);
2420 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2421 {
2422 second = val & 0xffff;
2423 first = val >> 16;
2424 }
2425 else if (r_type != R_MIPS16_26)
2426 {
2427 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2428 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2429 }
2430 else
2431 {
2432 second = val & 0xffff;
2433 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2434 | ((val >> 21) & 0x1f);
2435 }
2436 bfd_put_16 (abfd, second, data + 2);
2437 bfd_put_16 (abfd, first, data);
2438 }
2439
2440 /* Perform reloc offset checking.
2441 We can only use bfd_reloc_offset_in_range, which takes into account
2442 the size of the field being relocated, when section contents will
2443 be accessed because mips object files may use relocations that seem
2444 to access beyond section limits.
2445 gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2446 R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2447 section. The R_MIPS_SUB applies to the addend for the next reloc
2448 rather than the section contents.
2449
2450 CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2451 CHECK_INPLACE to only check partial_inplace relocs, and
2452 CHECK_SHUFFLE to only check relocs that shuffle/unshuffle. */
2453
2454 bool
2455 _bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2456 arelent *reloc_entry, enum reloc_check check)
2457 {
2458 if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2459 return true;
2460 if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2461 return true;
2462 return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2463 input_section, reloc_entry->address);
2464 }
2465
2466 bfd_reloc_status_type
2467 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2468 arelent *reloc_entry, asection *input_section,
2469 bool relocatable, void *data, bfd_vma gp)
2470 {
2471 bfd_vma relocation;
2472 bfd_signed_vma val;
2473 bfd_reloc_status_type status;
2474
2475 if (bfd_is_com_section (symbol->section))
2476 relocation = 0;
2477 else
2478 relocation = symbol->value;
2479
2480 relocation += symbol->section->output_section->vma;
2481 relocation += symbol->section->output_offset;
2482
2483 /* Set val to the offset into the section or symbol. */
2484 val = reloc_entry->addend;
2485
2486 _bfd_mips_elf_sign_extend (val, 16);
2487
2488 /* Adjust val for the final section location and GP value. If we
2489 are producing relocatable output, we don't want to do this for
2490 an external symbol. */
2491 if (! relocatable
2492 || (symbol->flags & BSF_SECTION_SYM) != 0)
2493 val += relocation - gp;
2494
2495 if (reloc_entry->howto->partial_inplace)
2496 {
2497 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2498 reloc_entry->address))
2499 return bfd_reloc_outofrange;
2500
2501 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2502 (bfd_byte *) data
2503 + reloc_entry->address);
2504 if (status != bfd_reloc_ok)
2505 return status;
2506 }
2507 else
2508 reloc_entry->addend = val;
2509
2510 if (relocatable)
2511 reloc_entry->address += input_section->output_offset;
2512
2513 return bfd_reloc_ok;
2514 }
2515
2516 /* A howto special_function for REL *HI16 relocations. We can only
2517 calculate the correct value once we've seen the partnering
2518 *LO16 relocation, so just save the information for later.
2519
2520 The ABI requires that the *LO16 immediately follow the *HI16.
2521 However, as a GNU extension, we permit an arbitrary number of
2522 *HI16s to be associated with a single *LO16. This significantly
2523 simplies the relocation handling in gcc. */
2524
2525 bfd_reloc_status_type
2526 _bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
2527 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2528 asection *input_section, bfd *output_bfd,
2529 char **error_message ATTRIBUTE_UNUSED)
2530 {
2531 struct mips_hi16 *n;
2532 struct mips_elf_obj_tdata *tdata;
2533
2534 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2535 return bfd_reloc_outofrange;
2536
2537 n = bfd_malloc (sizeof *n);
2538 if (n == NULL)
2539 return bfd_reloc_outofrange;
2540
2541 tdata = mips_elf_tdata (abfd);
2542 n->next = tdata->mips_hi16_list;
2543 n->data = data;
2544 n->input_section = input_section;
2545 n->rel = *reloc_entry;
2546 tdata->mips_hi16_list = n;
2547
2548 if (output_bfd != NULL)
2549 reloc_entry->address += input_section->output_offset;
2550
2551 return bfd_reloc_ok;
2552 }
2553
2554 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2555 like any other 16-bit relocation when applied to global symbols, but is
2556 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2557
2558 bfd_reloc_status_type
2559 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2560 void *data, asection *input_section,
2561 bfd *output_bfd, char **error_message)
2562 {
2563 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2564 || bfd_is_und_section (bfd_asymbol_section (symbol))
2565 || bfd_is_com_section (bfd_asymbol_section (symbol)))
2566 /* The relocation is against a global symbol. */
2567 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2568 input_section, output_bfd,
2569 error_message);
2570
2571 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2572 input_section, output_bfd, error_message);
2573 }
2574
2575 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2576 is a straightforward 16 bit inplace relocation, but we must deal with
2577 any partnering high-part relocations as well. */
2578
2579 bfd_reloc_status_type
2580 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2581 void *data, asection *input_section,
2582 bfd *output_bfd, char **error_message)
2583 {
2584 bfd_vma vallo;
2585 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2586 struct mips_elf_obj_tdata *tdata;
2587
2588 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2589 reloc_entry->address))
2590 return bfd_reloc_outofrange;
2591
2592 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2593 location);
2594 vallo = bfd_get_32 (abfd, location);
2595 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2596 location);
2597
2598 tdata = mips_elf_tdata (abfd);
2599 while (tdata->mips_hi16_list != NULL)
2600 {
2601 bfd_reloc_status_type ret;
2602 struct mips_hi16 *hi;
2603
2604 hi = tdata->mips_hi16_list;
2605
2606 /* R_MIPS*_GOT16 relocations are something of a special case. We
2607 want to install the addend in the same way as for a R_MIPS*_HI16
2608 relocation (with a rightshift of 16). However, since GOT16
2609 relocations can also be used with global symbols, their howto
2610 has a rightshift of 0. */
2611 if (hi->rel.howto->type == R_MIPS_GOT16)
2612 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
2613 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2614 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
2615 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2616 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
2617
2618 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2619 carry or borrow will induce a change of +1 or -1 in the high part. */
2620 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2621
2622 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2623 hi->input_section, output_bfd,
2624 error_message);
2625 if (ret != bfd_reloc_ok)
2626 return ret;
2627
2628 tdata->mips_hi16_list = hi->next;
2629 free (hi);
2630 }
2631
2632 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2633 input_section, output_bfd,
2634 error_message);
2635 }
2636
2637 /* A generic howto special_function. This calculates and installs the
2638 relocation itself, thus avoiding the oft-discussed problems in
2639 bfd_perform_relocation and bfd_install_relocation. */
2640
2641 bfd_reloc_status_type
2642 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2643 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2644 asection *input_section, bfd *output_bfd,
2645 char **error_message ATTRIBUTE_UNUSED)
2646 {
2647 bfd_signed_vma val;
2648 bfd_reloc_status_type status;
2649 bool relocatable;
2650
2651 relocatable = (output_bfd != NULL);
2652
2653 if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2654 (relocatable
2655 ? check_inplace : check_std)))
2656 return bfd_reloc_outofrange;
2657
2658 /* Build up the field adjustment in VAL. */
2659 val = 0;
2660 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2661 {
2662 /* Either we're calculating the final field value or we have a
2663 relocation against a section symbol. Add in the section's
2664 offset or address. */
2665 val += symbol->section->output_section->vma;
2666 val += symbol->section->output_offset;
2667 }
2668
2669 if (!relocatable)
2670 {
2671 /* We're calculating the final field value. Add in the symbol's value
2672 and, if pc-relative, subtract the address of the field itself. */
2673 val += symbol->value;
2674 if (reloc_entry->howto->pc_relative)
2675 {
2676 val -= input_section->output_section->vma;
2677 val -= input_section->output_offset;
2678 val -= reloc_entry->address;
2679 }
2680 }
2681
2682 /* VAL is now the final adjustment. If we're keeping this relocation
2683 in the output file, and if the relocation uses a separate addend,
2684 we just need to add VAL to that addend. Otherwise we need to add
2685 VAL to the relocation field itself. */
2686 if (relocatable && !reloc_entry->howto->partial_inplace)
2687 reloc_entry->addend += val;
2688 else
2689 {
2690 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2691
2692 /* Add in the separate addend, if any. */
2693 val += reloc_entry->addend;
2694
2695 /* Add VAL to the relocation field. */
2696 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
2697 location);
2698 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2699 location);
2700 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
2701 location);
2702
2703 if (status != bfd_reloc_ok)
2704 return status;
2705 }
2706
2707 if (relocatable)
2708 reloc_entry->address += input_section->output_offset;
2709
2710 return bfd_reloc_ok;
2711 }
2712 \f
2713 /* Swap an entry in a .gptab section. Note that these routines rely
2714 on the equivalence of the two elements of the union. */
2715
2716 static void
2717 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2718 Elf32_gptab *in)
2719 {
2720 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2721 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2722 }
2723
2724 static void
2725 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2726 Elf32_External_gptab *ex)
2727 {
2728 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2729 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2730 }
2731
2732 static void
2733 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2734 Elf32_External_compact_rel *ex)
2735 {
2736 H_PUT_32 (abfd, in->id1, ex->id1);
2737 H_PUT_32 (abfd, in->num, ex->num);
2738 H_PUT_32 (abfd, in->id2, ex->id2);
2739 H_PUT_32 (abfd, in->offset, ex->offset);
2740 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2741 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2742 }
2743
2744 static void
2745 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2746 Elf32_External_crinfo *ex)
2747 {
2748 unsigned long l;
2749
2750 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2751 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2752 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2753 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2754 H_PUT_32 (abfd, l, ex->info);
2755 H_PUT_32 (abfd, in->konst, ex->konst);
2756 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2757 }
2758 \f
2759 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2760 routines swap this structure in and out. They are used outside of
2761 BFD, so they are globally visible. */
2762
2763 void
2764 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2765 Elf32_RegInfo *in)
2766 {
2767 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2768 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2769 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2770 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2771 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2772 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2773 }
2774
2775 void
2776 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2777 Elf32_External_RegInfo *ex)
2778 {
2779 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2780 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2781 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2782 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2783 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2784 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2785 }
2786
2787 /* In the 64 bit ABI, the .MIPS.options section holds register
2788 information in an Elf64_Reginfo structure. These routines swap
2789 them in and out. They are globally visible because they are used
2790 outside of BFD. These routines are here so that gas can call them
2791 without worrying about whether the 64 bit ABI has been included. */
2792
2793 void
2794 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2795 Elf64_Internal_RegInfo *in)
2796 {
2797 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2798 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2799 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2800 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2801 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2802 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2803 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2804 }
2805
2806 void
2807 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2808 Elf64_External_RegInfo *ex)
2809 {
2810 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2811 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2812 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2813 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2814 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2815 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2816 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2817 }
2818
2819 /* Swap in an options header. */
2820
2821 void
2822 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2823 Elf_Internal_Options *in)
2824 {
2825 in->kind = H_GET_8 (abfd, ex->kind);
2826 in->size = H_GET_8 (abfd, ex->size);
2827 in->section = H_GET_16 (abfd, ex->section);
2828 in->info = H_GET_32 (abfd, ex->info);
2829 }
2830
2831 /* Swap out an options header. */
2832
2833 void
2834 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2835 Elf_External_Options *ex)
2836 {
2837 H_PUT_8 (abfd, in->kind, ex->kind);
2838 H_PUT_8 (abfd, in->size, ex->size);
2839 H_PUT_16 (abfd, in->section, ex->section);
2840 H_PUT_32 (abfd, in->info, ex->info);
2841 }
2842
2843 /* Swap in an abiflags structure. */
2844
2845 void
2846 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2847 const Elf_External_ABIFlags_v0 *ex,
2848 Elf_Internal_ABIFlags_v0 *in)
2849 {
2850 in->version = H_GET_16 (abfd, ex->version);
2851 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2852 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2853 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2854 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2855 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2856 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2857 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2858 in->ases = H_GET_32 (abfd, ex->ases);
2859 in->flags1 = H_GET_32 (abfd, ex->flags1);
2860 in->flags2 = H_GET_32 (abfd, ex->flags2);
2861 }
2862
2863 /* Swap out an abiflags structure. */
2864
2865 void
2866 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2867 const Elf_Internal_ABIFlags_v0 *in,
2868 Elf_External_ABIFlags_v0 *ex)
2869 {
2870 H_PUT_16 (abfd, in->version, ex->version);
2871 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2872 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2873 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2874 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2875 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2876 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2877 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2878 H_PUT_32 (abfd, in->ases, ex->ases);
2879 H_PUT_32 (abfd, in->flags1, ex->flags1);
2880 H_PUT_32 (abfd, in->flags2, ex->flags2);
2881 }
2882 \f
2883 /* This function is called via qsort() to sort the dynamic relocation
2884 entries by increasing r_symndx value. */
2885
2886 static int
2887 sort_dynamic_relocs (const void *arg1, const void *arg2)
2888 {
2889 Elf_Internal_Rela int_reloc1;
2890 Elf_Internal_Rela int_reloc2;
2891 int diff;
2892
2893 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2894 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2895
2896 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2897 if (diff != 0)
2898 return diff;
2899
2900 if (int_reloc1.r_offset < int_reloc2.r_offset)
2901 return -1;
2902 if (int_reloc1.r_offset > int_reloc2.r_offset)
2903 return 1;
2904 return 0;
2905 }
2906
2907 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2908
2909 static int
2910 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2911 const void *arg2 ATTRIBUTE_UNUSED)
2912 {
2913 #ifdef BFD64
2914 Elf_Internal_Rela int_reloc1[3];
2915 Elf_Internal_Rela int_reloc2[3];
2916
2917 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2918 (reldyn_sorting_bfd, arg1, int_reloc1);
2919 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2920 (reldyn_sorting_bfd, arg2, int_reloc2);
2921
2922 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2923 return -1;
2924 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2925 return 1;
2926
2927 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2928 return -1;
2929 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2930 return 1;
2931 return 0;
2932 #else
2933 abort ();
2934 #endif
2935 }
2936
2937
2938 /* This routine is used to write out ECOFF debugging external symbol
2939 information. It is called via mips_elf_link_hash_traverse. The
2940 ECOFF external symbol information must match the ELF external
2941 symbol information. Unfortunately, at this point we don't know
2942 whether a symbol is required by reloc information, so the two
2943 tables may wind up being different. We must sort out the external
2944 symbol information before we can set the final size of the .mdebug
2945 section, and we must set the size of the .mdebug section before we
2946 can relocate any sections, and we can't know which symbols are
2947 required by relocation until we relocate the sections.
2948 Fortunately, it is relatively unlikely that any symbol will be
2949 stripped but required by a reloc. In particular, it can not happen
2950 when generating a final executable. */
2951
2952 static bool
2953 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2954 {
2955 struct extsym_info *einfo = data;
2956 bool strip;
2957 asection *sec, *output_section;
2958
2959 if (h->root.indx == -2)
2960 strip = false;
2961 else if ((h->root.def_dynamic
2962 || h->root.ref_dynamic
2963 || h->root.type == bfd_link_hash_new)
2964 && !h->root.def_regular
2965 && !h->root.ref_regular)
2966 strip = true;
2967 else if (einfo->info->strip == strip_all
2968 || (einfo->info->strip == strip_some
2969 && bfd_hash_lookup (einfo->info->keep_hash,
2970 h->root.root.root.string,
2971 false, false) == NULL))
2972 strip = true;
2973 else
2974 strip = false;
2975
2976 if (strip)
2977 return true;
2978
2979 if (h->esym.ifd == -2)
2980 {
2981 h->esym.jmptbl = 0;
2982 h->esym.cobol_main = 0;
2983 h->esym.weakext = 0;
2984 h->esym.reserved = 0;
2985 h->esym.ifd = ifdNil;
2986 h->esym.asym.value = 0;
2987 h->esym.asym.st = stGlobal;
2988
2989 if (h->root.root.type == bfd_link_hash_undefined
2990 || h->root.root.type == bfd_link_hash_undefweak)
2991 {
2992 const char *name;
2993
2994 /* Use undefined class. Also, set class and type for some
2995 special symbols. */
2996 name = h->root.root.root.string;
2997 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2998 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2999 {
3000 h->esym.asym.sc = scData;
3001 h->esym.asym.st = stLabel;
3002 h->esym.asym.value = 0;
3003 }
3004 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3005 {
3006 h->esym.asym.sc = scAbs;
3007 h->esym.asym.st = stLabel;
3008 h->esym.asym.value =
3009 mips_elf_hash_table (einfo->info)->procedure_count;
3010 }
3011 else
3012 h->esym.asym.sc = scUndefined;
3013 }
3014 else if (h->root.root.type != bfd_link_hash_defined
3015 && h->root.root.type != bfd_link_hash_defweak)
3016 h->esym.asym.sc = scAbs;
3017 else
3018 {
3019 const char *name;
3020
3021 sec = h->root.root.u.def.section;
3022 output_section = sec->output_section;
3023
3024 /* When making a shared library and symbol h is the one from
3025 the another shared library, OUTPUT_SECTION may be null. */
3026 if (output_section == NULL)
3027 h->esym.asym.sc = scUndefined;
3028 else
3029 {
3030 name = bfd_section_name (output_section);
3031
3032 if (strcmp (name, ".text") == 0)
3033 h->esym.asym.sc = scText;
3034 else if (strcmp (name, ".data") == 0)
3035 h->esym.asym.sc = scData;
3036 else if (strcmp (name, ".sdata") == 0)
3037 h->esym.asym.sc = scSData;
3038 else if (strcmp (name, ".rodata") == 0
3039 || strcmp (name, ".rdata") == 0)
3040 h->esym.asym.sc = scRData;
3041 else if (strcmp (name, ".bss") == 0)
3042 h->esym.asym.sc = scBss;
3043 else if (strcmp (name, ".sbss") == 0)
3044 h->esym.asym.sc = scSBss;
3045 else if (strcmp (name, ".init") == 0)
3046 h->esym.asym.sc = scInit;
3047 else if (strcmp (name, ".fini") == 0)
3048 h->esym.asym.sc = scFini;
3049 else
3050 h->esym.asym.sc = scAbs;
3051 }
3052 }
3053
3054 h->esym.asym.reserved = 0;
3055 h->esym.asym.index = indexNil;
3056 }
3057
3058 if (h->root.root.type == bfd_link_hash_common)
3059 h->esym.asym.value = h->root.root.u.c.size;
3060 else if (h->root.root.type == bfd_link_hash_defined
3061 || h->root.root.type == bfd_link_hash_defweak)
3062 {
3063 if (h->esym.asym.sc == scCommon)
3064 h->esym.asym.sc = scBss;
3065 else if (h->esym.asym.sc == scSCommon)
3066 h->esym.asym.sc = scSBss;
3067
3068 sec = h->root.root.u.def.section;
3069 output_section = sec->output_section;
3070 if (output_section != NULL)
3071 h->esym.asym.value = (h->root.root.u.def.value
3072 + sec->output_offset
3073 + output_section->vma);
3074 else
3075 h->esym.asym.value = 0;
3076 }
3077 else
3078 {
3079 struct mips_elf_link_hash_entry *hd = h;
3080
3081 while (hd->root.root.type == bfd_link_hash_indirect)
3082 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3083
3084 if (hd->needs_lazy_stub)
3085 {
3086 BFD_ASSERT (hd->root.plt.plist != NULL);
3087 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3088 /* Set type and value for a symbol with a function stub. */
3089 h->esym.asym.st = stProc;
3090 sec = hd->root.root.u.def.section;
3091 if (sec == NULL)
3092 h->esym.asym.value = 0;
3093 else
3094 {
3095 output_section = sec->output_section;
3096 if (output_section != NULL)
3097 h->esym.asym.value = (hd->root.plt.plist->stub_offset
3098 + sec->output_offset
3099 + output_section->vma);
3100 else
3101 h->esym.asym.value = 0;
3102 }
3103 }
3104 }
3105
3106 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3107 h->root.root.root.string,
3108 &h->esym))
3109 {
3110 einfo->failed = true;
3111 return false;
3112 }
3113
3114 return true;
3115 }
3116
3117 /* A comparison routine used to sort .gptab entries. */
3118
3119 static int
3120 gptab_compare (const void *p1, const void *p2)
3121 {
3122 const Elf32_gptab *a1 = p1;
3123 const Elf32_gptab *a2 = p2;
3124
3125 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3126 }
3127 \f
3128 /* Functions to manage the got entry hash table. */
3129
3130 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3131 hash number. */
3132
3133 static inline hashval_t
3134 mips_elf_hash_bfd_vma (bfd_vma addr)
3135 {
3136 #ifdef BFD64
3137 return addr + (addr >> 32);
3138 #else
3139 return addr;
3140 #endif
3141 }
3142
3143 static hashval_t
3144 mips_elf_got_entry_hash (const void *entry_)
3145 {
3146 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3147
3148 return (entry->symndx
3149 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3150 + (entry->tls_type == GOT_TLS_LDM ? 0
3151 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3152 : entry->symndx >= 0 ? (entry->abfd->id
3153 + mips_elf_hash_bfd_vma (entry->d.addend))
3154 : entry->d.h->root.root.root.hash));
3155 }
3156
3157 static int
3158 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3159 {
3160 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3161 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3162
3163 return (e1->symndx == e2->symndx
3164 && e1->tls_type == e2->tls_type
3165 && (e1->tls_type == GOT_TLS_LDM ? true
3166 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3167 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3168 && e1->d.addend == e2->d.addend)
3169 : e2->abfd && e1->d.h == e2->d.h));
3170 }
3171
3172 static hashval_t
3173 mips_got_page_ref_hash (const void *ref_)
3174 {
3175 const struct mips_got_page_ref *ref;
3176
3177 ref = (const struct mips_got_page_ref *) ref_;
3178 return ((ref->symndx >= 0
3179 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3180 : ref->u.h->root.root.root.hash)
3181 + mips_elf_hash_bfd_vma (ref->addend));
3182 }
3183
3184 static int
3185 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3186 {
3187 const struct mips_got_page_ref *ref1, *ref2;
3188
3189 ref1 = (const struct mips_got_page_ref *) ref1_;
3190 ref2 = (const struct mips_got_page_ref *) ref2_;
3191 return (ref1->symndx == ref2->symndx
3192 && (ref1->symndx < 0
3193 ? ref1->u.h == ref2->u.h
3194 : ref1->u.abfd == ref2->u.abfd)
3195 && ref1->addend == ref2->addend);
3196 }
3197
3198 static hashval_t
3199 mips_got_page_entry_hash (const void *entry_)
3200 {
3201 const struct mips_got_page_entry *entry;
3202
3203 entry = (const struct mips_got_page_entry *) entry_;
3204 return entry->sec->id;
3205 }
3206
3207 static int
3208 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3209 {
3210 const struct mips_got_page_entry *entry1, *entry2;
3211
3212 entry1 = (const struct mips_got_page_entry *) entry1_;
3213 entry2 = (const struct mips_got_page_entry *) entry2_;
3214 return entry1->sec == entry2->sec;
3215 }
3216 \f
3217 /* Create and return a new mips_got_info structure. */
3218
3219 static struct mips_got_info *
3220 mips_elf_create_got_info (bfd *abfd)
3221 {
3222 struct mips_got_info *g;
3223
3224 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3225 if (g == NULL)
3226 return NULL;
3227
3228 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3229 mips_elf_got_entry_eq, NULL);
3230 if (g->got_entries == NULL)
3231 return NULL;
3232
3233 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3234 mips_got_page_ref_eq, NULL);
3235 if (g->got_page_refs == NULL)
3236 return NULL;
3237
3238 return g;
3239 }
3240
3241 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3242 CREATE_P and if ABFD doesn't already have a GOT. */
3243
3244 static struct mips_got_info *
3245 mips_elf_bfd_got (bfd *abfd, bool create_p)
3246 {
3247 struct mips_elf_obj_tdata *tdata;
3248
3249 if (!is_mips_elf (abfd))
3250 return NULL;
3251
3252 tdata = mips_elf_tdata (abfd);
3253 if (!tdata->got && create_p)
3254 tdata->got = mips_elf_create_got_info (abfd);
3255 return tdata->got;
3256 }
3257
3258 /* Record that ABFD should use output GOT G. */
3259
3260 static void
3261 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3262 {
3263 struct mips_elf_obj_tdata *tdata;
3264
3265 BFD_ASSERT (is_mips_elf (abfd));
3266 tdata = mips_elf_tdata (abfd);
3267 if (tdata->got)
3268 {
3269 /* The GOT structure itself and the hash table entries are
3270 allocated to a bfd, but the hash tables aren't. */
3271 htab_delete (tdata->got->got_entries);
3272 htab_delete (tdata->got->got_page_refs);
3273 if (tdata->got->got_page_entries)
3274 htab_delete (tdata->got->got_page_entries);
3275 }
3276 tdata->got = g;
3277 }
3278
3279 /* Return the dynamic relocation section. If it doesn't exist, try to
3280 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3281 if creation fails. */
3282
3283 static asection *
3284 mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
3285 {
3286 const char *dname;
3287 asection *sreloc;
3288 bfd *dynobj;
3289
3290 dname = MIPS_ELF_REL_DYN_NAME (info);
3291 dynobj = elf_hash_table (info)->dynobj;
3292 sreloc = bfd_get_linker_section (dynobj, dname);
3293 if (sreloc == NULL && create_p)
3294 {
3295 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3296 (SEC_ALLOC
3297 | SEC_LOAD
3298 | SEC_HAS_CONTENTS
3299 | SEC_IN_MEMORY
3300 | SEC_LINKER_CREATED
3301 | SEC_READONLY));
3302 if (sreloc == NULL
3303 || !bfd_set_section_alignment (sreloc,
3304 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3305 return NULL;
3306 }
3307 return sreloc;
3308 }
3309
3310 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3311
3312 static int
3313 mips_elf_reloc_tls_type (unsigned int r_type)
3314 {
3315 if (tls_gd_reloc_p (r_type))
3316 return GOT_TLS_GD;
3317
3318 if (tls_ldm_reloc_p (r_type))
3319 return GOT_TLS_LDM;
3320
3321 if (tls_gottprel_reloc_p (r_type))
3322 return GOT_TLS_IE;
3323
3324 return GOT_TLS_NONE;
3325 }
3326
3327 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3328
3329 static int
3330 mips_tls_got_entries (unsigned int type)
3331 {
3332 switch (type)
3333 {
3334 case GOT_TLS_GD:
3335 case GOT_TLS_LDM:
3336 return 2;
3337
3338 case GOT_TLS_IE:
3339 return 1;
3340
3341 case GOT_TLS_NONE:
3342 return 0;
3343 }
3344 abort ();
3345 }
3346
3347 /* Count the number of relocations needed for a TLS GOT entry, with
3348 access types from TLS_TYPE, and symbol H (or a local symbol if H
3349 is NULL). */
3350
3351 static int
3352 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3353 struct elf_link_hash_entry *h)
3354 {
3355 int indx = 0;
3356 bool need_relocs = false;
3357 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3358
3359 if (h != NULL
3360 && h->dynindx != -1
3361 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3362 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3363 indx = h->dynindx;
3364
3365 if ((bfd_link_dll (info) || indx != 0)
3366 && (h == NULL
3367 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3368 || h->root.type != bfd_link_hash_undefweak))
3369 need_relocs = true;
3370
3371 if (!need_relocs)
3372 return 0;
3373
3374 switch (tls_type)
3375 {
3376 case GOT_TLS_GD:
3377 return indx != 0 ? 2 : 1;
3378
3379 case GOT_TLS_IE:
3380 return 1;
3381
3382 case GOT_TLS_LDM:
3383 return bfd_link_dll (info) ? 1 : 0;
3384
3385 default:
3386 return 0;
3387 }
3388 }
3389
3390 /* Add the number of GOT entries and TLS relocations required by ENTRY
3391 to G. */
3392
3393 static void
3394 mips_elf_count_got_entry (struct bfd_link_info *info,
3395 struct mips_got_info *g,
3396 struct mips_got_entry *entry)
3397 {
3398 if (entry->tls_type)
3399 {
3400 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3401 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3402 entry->symndx < 0
3403 ? &entry->d.h->root : NULL);
3404 }
3405 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3406 g->local_gotno += 1;
3407 else
3408 g->global_gotno += 1;
3409 }
3410
3411 /* Output a simple dynamic relocation into SRELOC. */
3412
3413 static void
3414 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3415 asection *sreloc,
3416 unsigned long reloc_index,
3417 unsigned long indx,
3418 int r_type,
3419 bfd_vma offset)
3420 {
3421 Elf_Internal_Rela rel[3];
3422
3423 memset (rel, 0, sizeof (rel));
3424
3425 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3426 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3427
3428 if (ABI_64_P (output_bfd))
3429 {
3430 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3431 (output_bfd, &rel[0],
3432 (sreloc->contents
3433 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3434 }
3435 else
3436 bfd_elf32_swap_reloc_out
3437 (output_bfd, &rel[0],
3438 (sreloc->contents
3439 + reloc_index * sizeof (Elf32_External_Rel)));
3440 }
3441
3442 /* Initialize a set of TLS GOT entries for one symbol. */
3443
3444 static void
3445 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3446 struct mips_got_entry *entry,
3447 struct mips_elf_link_hash_entry *h,
3448 bfd_vma value)
3449 {
3450 bool dyn = elf_hash_table (info)->dynamic_sections_created;
3451 struct mips_elf_link_hash_table *htab;
3452 int indx;
3453 asection *sreloc, *sgot;
3454 bfd_vma got_offset, got_offset2;
3455 bool need_relocs = false;
3456
3457 htab = mips_elf_hash_table (info);
3458 if (htab == NULL)
3459 return;
3460
3461 sgot = htab->root.sgot;
3462
3463 indx = 0;
3464 if (h != NULL
3465 && h->root.dynindx != -1
3466 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3467 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3468 indx = h->root.dynindx;
3469
3470 if (entry->tls_initialized)
3471 return;
3472
3473 if ((bfd_link_dll (info) || indx != 0)
3474 && (h == NULL
3475 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3476 || h->root.type != bfd_link_hash_undefweak))
3477 need_relocs = true;
3478
3479 /* MINUS_ONE means the symbol is not defined in this object. It may not
3480 be defined at all; assume that the value doesn't matter in that
3481 case. Otherwise complain if we would use the value. */
3482 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3483 || h->root.root.type == bfd_link_hash_undefweak);
3484
3485 /* Emit necessary relocations. */
3486 sreloc = mips_elf_rel_dyn_section (info, false);
3487 got_offset = entry->gotidx;
3488
3489 switch (entry->tls_type)
3490 {
3491 case GOT_TLS_GD:
3492 /* General Dynamic. */
3493 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3494
3495 if (need_relocs)
3496 {
3497 mips_elf_output_dynamic_relocation
3498 (abfd, sreloc, sreloc->reloc_count++, indx,
3499 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3500 sgot->output_offset + sgot->output_section->vma + got_offset);
3501
3502 if (indx)
3503 mips_elf_output_dynamic_relocation
3504 (abfd, sreloc, sreloc->reloc_count++, indx,
3505 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3506 sgot->output_offset + sgot->output_section->vma + got_offset2);
3507 else
3508 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3509 sgot->contents + got_offset2);
3510 }
3511 else
3512 {
3513 MIPS_ELF_PUT_WORD (abfd, 1,
3514 sgot->contents + got_offset);
3515 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3516 sgot->contents + got_offset2);
3517 }
3518 break;
3519
3520 case GOT_TLS_IE:
3521 /* Initial Exec model. */
3522 if (need_relocs)
3523 {
3524 if (indx == 0)
3525 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3526 sgot->contents + got_offset);
3527 else
3528 MIPS_ELF_PUT_WORD (abfd, 0,
3529 sgot->contents + got_offset);
3530
3531 mips_elf_output_dynamic_relocation
3532 (abfd, sreloc, sreloc->reloc_count++, indx,
3533 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3534 sgot->output_offset + sgot->output_section->vma + got_offset);
3535 }
3536 else
3537 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3538 sgot->contents + got_offset);
3539 break;
3540
3541 case GOT_TLS_LDM:
3542 /* The initial offset is zero, and the LD offsets will include the
3543 bias by DTP_OFFSET. */
3544 MIPS_ELF_PUT_WORD (abfd, 0,
3545 sgot->contents + got_offset
3546 + MIPS_ELF_GOT_SIZE (abfd));
3547
3548 if (!bfd_link_dll (info))
3549 MIPS_ELF_PUT_WORD (abfd, 1,
3550 sgot->contents + got_offset);
3551 else
3552 mips_elf_output_dynamic_relocation
3553 (abfd, sreloc, sreloc->reloc_count++, indx,
3554 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3555 sgot->output_offset + sgot->output_section->vma + got_offset);
3556 break;
3557
3558 default:
3559 abort ();
3560 }
3561
3562 entry->tls_initialized = true;
3563 }
3564
3565 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3566 for global symbol H. .got.plt comes before the GOT, so the offset
3567 will be negative. */
3568
3569 static bfd_vma
3570 mips_elf_gotplt_index (struct bfd_link_info *info,
3571 struct elf_link_hash_entry *h)
3572 {
3573 bfd_vma got_address, got_value;
3574 struct mips_elf_link_hash_table *htab;
3575
3576 htab = mips_elf_hash_table (info);
3577 BFD_ASSERT (htab != NULL);
3578
3579 BFD_ASSERT (h->plt.plist != NULL);
3580 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3581
3582 /* Calculate the address of the associated .got.plt entry. */
3583 got_address = (htab->root.sgotplt->output_section->vma
3584 + htab->root.sgotplt->output_offset
3585 + (h->plt.plist->gotplt_index
3586 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3587
3588 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3589 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3590 + htab->root.hgot->root.u.def.section->output_offset
3591 + htab->root.hgot->root.u.def.value);
3592
3593 return got_address - got_value;
3594 }
3595
3596 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3597 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3598 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3599 offset can be found. */
3600
3601 static bfd_vma
3602 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3603 bfd_vma value, unsigned long r_symndx,
3604 struct mips_elf_link_hash_entry *h, int r_type)
3605 {
3606 struct mips_elf_link_hash_table *htab;
3607 struct mips_got_entry *entry;
3608
3609 htab = mips_elf_hash_table (info);
3610 BFD_ASSERT (htab != NULL);
3611
3612 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3613 r_symndx, h, r_type);
3614 if (!entry)
3615 return MINUS_ONE;
3616
3617 if (entry->tls_type)
3618 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3619 return entry->gotidx;
3620 }
3621
3622 /* Return the GOT index of global symbol H in the primary GOT. */
3623
3624 static bfd_vma
3625 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3626 struct elf_link_hash_entry *h)
3627 {
3628 struct mips_elf_link_hash_table *htab;
3629 long global_got_dynindx;
3630 struct mips_got_info *g;
3631 bfd_vma got_index;
3632
3633 htab = mips_elf_hash_table (info);
3634 BFD_ASSERT (htab != NULL);
3635
3636 global_got_dynindx = 0;
3637 if (htab->global_gotsym != NULL)
3638 global_got_dynindx = htab->global_gotsym->dynindx;
3639
3640 /* Once we determine the global GOT entry with the lowest dynamic
3641 symbol table index, we must put all dynamic symbols with greater
3642 indices into the primary GOT. That makes it easy to calculate the
3643 GOT offset. */
3644 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3645 g = mips_elf_bfd_got (obfd, false);
3646 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3647 * MIPS_ELF_GOT_SIZE (obfd));
3648 BFD_ASSERT (got_index < htab->root.sgot->size);
3649
3650 return got_index;
3651 }
3652
3653 /* Return the GOT index for the global symbol indicated by H, which is
3654 referenced by a relocation of type R_TYPE in IBFD. */
3655
3656 static bfd_vma
3657 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3658 struct elf_link_hash_entry *h, int r_type)
3659 {
3660 struct mips_elf_link_hash_table *htab;
3661 struct mips_got_info *g;
3662 struct mips_got_entry lookup, *entry;
3663 bfd_vma gotidx;
3664
3665 htab = mips_elf_hash_table (info);
3666 BFD_ASSERT (htab != NULL);
3667
3668 g = mips_elf_bfd_got (ibfd, false);
3669 BFD_ASSERT (g);
3670
3671 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3672 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
3673 return mips_elf_primary_global_got_index (obfd, info, h);
3674
3675 lookup.abfd = ibfd;
3676 lookup.symndx = -1;
3677 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3678 entry = htab_find (g->got_entries, &lookup);
3679 BFD_ASSERT (entry);
3680
3681 gotidx = entry->gotidx;
3682 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3683
3684 if (lookup.tls_type)
3685 {
3686 bfd_vma value = MINUS_ONE;
3687
3688 if ((h->root.type == bfd_link_hash_defined
3689 || h->root.type == bfd_link_hash_defweak)
3690 && h->root.u.def.section->output_section)
3691 value = (h->root.u.def.value
3692 + h->root.u.def.section->output_offset
3693 + h->root.u.def.section->output_section->vma);
3694
3695 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3696 }
3697 return gotidx;
3698 }
3699
3700 /* Find a GOT page entry that points to within 32KB of VALUE. These
3701 entries are supposed to be placed at small offsets in the GOT, i.e.,
3702 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3703 entry could be created. If OFFSETP is nonnull, use it to return the
3704 offset of the GOT entry from VALUE. */
3705
3706 static bfd_vma
3707 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3708 bfd_vma value, bfd_vma *offsetp)
3709 {
3710 bfd_vma page, got_index;
3711 struct mips_got_entry *entry;
3712
3713 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3714 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3715 NULL, R_MIPS_GOT_PAGE);
3716
3717 if (!entry)
3718 return MINUS_ONE;
3719
3720 got_index = entry->gotidx;
3721
3722 if (offsetp)
3723 *offsetp = value - entry->d.address;
3724
3725 return got_index;
3726 }
3727
3728 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3729 EXTERNAL is true if the relocation was originally against a global
3730 symbol that binds locally. */
3731
3732 static bfd_vma
3733 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3734 bfd_vma value, bool external)
3735 {
3736 struct mips_got_entry *entry;
3737
3738 /* GOT16 relocations against local symbols are followed by a LO16
3739 relocation; those against global symbols are not. Thus if the
3740 symbol was originally local, the GOT16 relocation should load the
3741 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3742 if (! external)
3743 value = mips_elf_high (value) << 16;
3744
3745 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3746 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3747 same in all cases. */
3748 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3749 NULL, R_MIPS_GOT16);
3750 if (entry)
3751 return entry->gotidx;
3752 else
3753 return MINUS_ONE;
3754 }
3755
3756 /* Returns the offset for the entry at the INDEXth position
3757 in the GOT. */
3758
3759 static bfd_vma
3760 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3761 bfd *input_bfd, bfd_vma got_index)
3762 {
3763 struct mips_elf_link_hash_table *htab;
3764 asection *sgot;
3765 bfd_vma gp;
3766
3767 htab = mips_elf_hash_table (info);
3768 BFD_ASSERT (htab != NULL);
3769
3770 sgot = htab->root.sgot;
3771 gp = _bfd_get_gp_value (output_bfd)
3772 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3773
3774 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3775 }
3776
3777 /* Create and return a local GOT entry for VALUE, which was calculated
3778 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3779 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3780 instead. */
3781
3782 static struct mips_got_entry *
3783 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3784 bfd *ibfd, bfd_vma value,
3785 unsigned long r_symndx,
3786 struct mips_elf_link_hash_entry *h,
3787 int r_type)
3788 {
3789 struct mips_got_entry lookup, *entry;
3790 void **loc;
3791 struct mips_got_info *g;
3792 struct mips_elf_link_hash_table *htab;
3793 bfd_vma gotidx;
3794
3795 htab = mips_elf_hash_table (info);
3796 BFD_ASSERT (htab != NULL);
3797
3798 g = mips_elf_bfd_got (ibfd, false);
3799 if (g == NULL)
3800 {
3801 g = mips_elf_bfd_got (abfd, false);
3802 BFD_ASSERT (g != NULL);
3803 }
3804
3805 /* This function shouldn't be called for symbols that live in the global
3806 area of the GOT. */
3807 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3808
3809 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3810 if (lookup.tls_type)
3811 {
3812 lookup.abfd = ibfd;
3813 if (tls_ldm_reloc_p (r_type))
3814 {
3815 lookup.symndx = 0;
3816 lookup.d.addend = 0;
3817 }
3818 else if (h == NULL)
3819 {
3820 lookup.symndx = r_symndx;
3821 lookup.d.addend = 0;
3822 }
3823 else
3824 {
3825 lookup.symndx = -1;
3826 lookup.d.h = h;
3827 }
3828
3829 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3830 BFD_ASSERT (entry);
3831
3832 gotidx = entry->gotidx;
3833 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3834
3835 return entry;
3836 }
3837
3838 lookup.abfd = NULL;
3839 lookup.symndx = -1;
3840 lookup.d.address = value;
3841 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3842 if (!loc)
3843 return NULL;
3844
3845 entry = (struct mips_got_entry *) *loc;
3846 if (entry)
3847 return entry;
3848
3849 if (g->assigned_low_gotno > g->assigned_high_gotno)
3850 {
3851 /* We didn't allocate enough space in the GOT. */
3852 _bfd_error_handler
3853 (_("not enough GOT space for local GOT entries"));
3854 bfd_set_error (bfd_error_bad_value);
3855 return NULL;
3856 }
3857
3858 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3859 if (!entry)
3860 return NULL;
3861
3862 if (got16_reloc_p (r_type)
3863 || call16_reloc_p (r_type)
3864 || got_page_reloc_p (r_type)
3865 || got_disp_reloc_p (r_type))
3866 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3867 else
3868 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3869
3870 *entry = lookup;
3871 *loc = entry;
3872
3873 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3874
3875 /* These GOT entries need a dynamic relocation on VxWorks. */
3876 if (htab->root.target_os == is_vxworks)
3877 {
3878 Elf_Internal_Rela outrel;
3879 asection *s;
3880 bfd_byte *rloc;
3881 bfd_vma got_address;
3882
3883 s = mips_elf_rel_dyn_section (info, false);
3884 got_address = (htab->root.sgot->output_section->vma
3885 + htab->root.sgot->output_offset
3886 + entry->gotidx);
3887
3888 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3889 outrel.r_offset = got_address;
3890 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3891 outrel.r_addend = value;
3892 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3893 }
3894
3895 return entry;
3896 }
3897
3898 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3899 The number might be exact or a worst-case estimate, depending on how
3900 much information is available to elf_backend_omit_section_dynsym at
3901 the current linking stage. */
3902
3903 static bfd_size_type
3904 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3905 {
3906 bfd_size_type count;
3907
3908 count = 0;
3909 if (bfd_link_pic (info)
3910 || elf_hash_table (info)->is_relocatable_executable)
3911 {
3912 asection *p;
3913 const struct elf_backend_data *bed;
3914
3915 bed = get_elf_backend_data (output_bfd);
3916 for (p = output_bfd->sections; p ; p = p->next)
3917 if ((p->flags & SEC_EXCLUDE) == 0
3918 && (p->flags & SEC_ALLOC) != 0
3919 && elf_hash_table (info)->dynamic_relocs
3920 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3921 ++count;
3922 }
3923 return count;
3924 }
3925
3926 /* Sort the dynamic symbol table so that symbols that need GOT entries
3927 appear towards the end. */
3928
3929 static bool
3930 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3931 {
3932 struct mips_elf_link_hash_table *htab;
3933 struct mips_elf_hash_sort_data hsd;
3934 struct mips_got_info *g;
3935
3936 htab = mips_elf_hash_table (info);
3937 BFD_ASSERT (htab != NULL);
3938
3939 if (htab->root.dynsymcount == 0)
3940 return true;
3941
3942 g = htab->got_info;
3943 if (g == NULL)
3944 return true;
3945
3946 hsd.low = NULL;
3947 hsd.max_unref_got_dynindx
3948 = hsd.min_got_dynindx
3949 = (htab->root.dynsymcount - g->reloc_only_gotno);
3950 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3951 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3952 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3953 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3954 hsd.output_bfd = abfd;
3955 if (htab->root.dynobj != NULL
3956 && htab->root.dynamic_sections_created
3957 && info->emit_gnu_hash)
3958 {
3959 asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3960 BFD_ASSERT (s != NULL);
3961 hsd.mipsxhash = s->contents;
3962 BFD_ASSERT (hsd.mipsxhash != NULL);
3963 }
3964 else
3965 hsd.mipsxhash = NULL;
3966 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3967
3968 /* There should have been enough room in the symbol table to
3969 accommodate both the GOT and non-GOT symbols. */
3970 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3971 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3972 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3973 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3974
3975 /* Now we know which dynamic symbol has the lowest dynamic symbol
3976 table index in the GOT. */
3977 htab->global_gotsym = hsd.low;
3978
3979 return true;
3980 }
3981
3982 /* If H needs a GOT entry, assign it the highest available dynamic
3983 index. Otherwise, assign it the lowest available dynamic
3984 index. */
3985
3986 static bool
3987 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3988 {
3989 struct mips_elf_hash_sort_data *hsd = data;
3990
3991 /* Symbols without dynamic symbol table entries aren't interesting
3992 at all. */
3993 if (h->root.dynindx == -1)
3994 return true;
3995
3996 switch (h->global_got_area)
3997 {
3998 case GGA_NONE:
3999 if (h->root.forced_local)
4000 h->root.dynindx = hsd->max_local_dynindx++;
4001 else
4002 h->root.dynindx = hsd->max_non_got_dynindx++;
4003 break;
4004
4005 case GGA_NORMAL:
4006 h->root.dynindx = --hsd->min_got_dynindx;
4007 hsd->low = (struct elf_link_hash_entry *) h;
4008 break;
4009
4010 case GGA_RELOC_ONLY:
4011 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4012 hsd->low = (struct elf_link_hash_entry *) h;
4013 h->root.dynindx = hsd->max_unref_got_dynindx++;
4014 break;
4015 }
4016
4017 /* Populate the .MIPS.xhash translation table entry with
4018 the symbol dynindx. */
4019 if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4020 bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4021 hsd->mipsxhash + h->mipsxhash_loc);
4022
4023 return true;
4024 }
4025
4026 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4027 (which is owned by the caller and shouldn't be added to the
4028 hash table directly). */
4029
4030 static bool
4031 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4032 struct mips_got_entry *lookup)
4033 {
4034 struct mips_elf_link_hash_table *htab;
4035 struct mips_got_entry *entry;
4036 struct mips_got_info *g;
4037 void **loc, **bfd_loc;
4038
4039 /* Make sure there's a slot for this entry in the master GOT. */
4040 htab = mips_elf_hash_table (info);
4041 g = htab->got_info;
4042 loc = htab_find_slot (g->got_entries, lookup, INSERT);
4043 if (!loc)
4044 return false;
4045
4046 /* Populate the entry if it isn't already. */
4047 entry = (struct mips_got_entry *) *loc;
4048 if (!entry)
4049 {
4050 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4051 if (!entry)
4052 return false;
4053
4054 lookup->tls_initialized = false;
4055 lookup->gotidx = -1;
4056 *entry = *lookup;
4057 *loc = entry;
4058 }
4059
4060 /* Reuse the same GOT entry for the BFD's GOT. */
4061 g = mips_elf_bfd_got (abfd, true);
4062 if (!g)
4063 return false;
4064
4065 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4066 if (!bfd_loc)
4067 return false;
4068
4069 if (!*bfd_loc)
4070 *bfd_loc = entry;
4071 return true;
4072 }
4073
4074 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
4075 entry for it. FOR_CALL is true if the caller is only interested in
4076 using the GOT entry for calls. */
4077
4078 static bool
4079 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4080 bfd *abfd, struct bfd_link_info *info,
4081 bool for_call, int r_type)
4082 {
4083 struct mips_elf_link_hash_table *htab;
4084 struct mips_elf_link_hash_entry *hmips;
4085 struct mips_got_entry entry;
4086 unsigned char tls_type;
4087
4088 htab = mips_elf_hash_table (info);
4089 BFD_ASSERT (htab != NULL);
4090
4091 hmips = (struct mips_elf_link_hash_entry *) h;
4092 if (!for_call)
4093 hmips->got_only_for_calls = false;
4094
4095 /* A global symbol in the GOT must also be in the dynamic symbol
4096 table. */
4097 if (h->dynindx == -1)
4098 {
4099 switch (ELF_ST_VISIBILITY (h->other))
4100 {
4101 case STV_INTERNAL:
4102 case STV_HIDDEN:
4103 _bfd_mips_elf_hide_symbol (info, h, true);
4104 break;
4105 }
4106 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4107 return false;
4108 }
4109
4110 tls_type = mips_elf_reloc_tls_type (r_type);
4111 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4112 hmips->global_got_area = GGA_NORMAL;
4113
4114 entry.abfd = abfd;
4115 entry.symndx = -1;
4116 entry.d.h = (struct mips_elf_link_hash_entry *) h;
4117 entry.tls_type = tls_type;
4118 return mips_elf_record_got_entry (info, abfd, &entry);
4119 }
4120
4121 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4122 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
4123
4124 static bool
4125 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4126 struct bfd_link_info *info, int r_type)
4127 {
4128 struct mips_elf_link_hash_table *htab;
4129 struct mips_got_info *g;
4130 struct mips_got_entry entry;
4131
4132 htab = mips_elf_hash_table (info);
4133 BFD_ASSERT (htab != NULL);
4134
4135 g = htab->got_info;
4136 BFD_ASSERT (g != NULL);
4137
4138 entry.abfd = abfd;
4139 entry.symndx = symndx;
4140 entry.d.addend = addend;
4141 entry.tls_type = mips_elf_reloc_tls_type (r_type);
4142 return mips_elf_record_got_entry (info, abfd, &entry);
4143 }
4144
4145 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4146 H is the symbol's hash table entry, or null if SYMNDX is local
4147 to ABFD. */
4148
4149 static bool
4150 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4151 long symndx, struct elf_link_hash_entry *h,
4152 bfd_signed_vma addend)
4153 {
4154 struct mips_elf_link_hash_table *htab;
4155 struct mips_got_info *g1, *g2;
4156 struct mips_got_page_ref lookup, *entry;
4157 void **loc, **bfd_loc;
4158
4159 htab = mips_elf_hash_table (info);
4160 BFD_ASSERT (htab != NULL);
4161
4162 g1 = htab->got_info;
4163 BFD_ASSERT (g1 != NULL);
4164
4165 if (h)
4166 {
4167 lookup.symndx = -1;
4168 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4169 }
4170 else
4171 {
4172 lookup.symndx = symndx;
4173 lookup.u.abfd = abfd;
4174 }
4175 lookup.addend = addend;
4176 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4177 if (loc == NULL)
4178 return false;
4179
4180 entry = (struct mips_got_page_ref *) *loc;
4181 if (!entry)
4182 {
4183 entry = bfd_alloc (abfd, sizeof (*entry));
4184 if (!entry)
4185 return false;
4186
4187 *entry = lookup;
4188 *loc = entry;
4189 }
4190
4191 /* Add the same entry to the BFD's GOT. */
4192 g2 = mips_elf_bfd_got (abfd, true);
4193 if (!g2)
4194 return false;
4195
4196 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4197 if (!bfd_loc)
4198 return false;
4199
4200 if (!*bfd_loc)
4201 *bfd_loc = entry;
4202
4203 return true;
4204 }
4205
4206 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4207
4208 static void
4209 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4210 unsigned int n)
4211 {
4212 asection *s;
4213 struct mips_elf_link_hash_table *htab;
4214
4215 htab = mips_elf_hash_table (info);
4216 BFD_ASSERT (htab != NULL);
4217
4218 s = mips_elf_rel_dyn_section (info, false);
4219 BFD_ASSERT (s != NULL);
4220
4221 if (htab->root.target_os == is_vxworks)
4222 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4223 else
4224 {
4225 if (s->size == 0)
4226 {
4227 /* Make room for a null element. */
4228 s->size += MIPS_ELF_REL_SIZE (abfd);
4229 ++s->reloc_count;
4230 }
4231 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4232 }
4233 }
4234 \f
4235 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4236 mips_elf_traverse_got_arg structure. Count the number of GOT
4237 entries and TLS relocs. Set DATA->value to true if we need
4238 to resolve indirect or warning symbols and then recreate the GOT. */
4239
4240 static int
4241 mips_elf_check_recreate_got (void **entryp, void *data)
4242 {
4243 struct mips_got_entry *entry;
4244 struct mips_elf_traverse_got_arg *arg;
4245
4246 entry = (struct mips_got_entry *) *entryp;
4247 arg = (struct mips_elf_traverse_got_arg *) data;
4248 if (entry->abfd != NULL && entry->symndx == -1)
4249 {
4250 struct mips_elf_link_hash_entry *h;
4251
4252 h = entry->d.h;
4253 if (h->root.root.type == bfd_link_hash_indirect
4254 || h->root.root.type == bfd_link_hash_warning)
4255 {
4256 arg->value = true;
4257 return 0;
4258 }
4259 }
4260 mips_elf_count_got_entry (arg->info, arg->g, entry);
4261 return 1;
4262 }
4263
4264 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4265 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4266 converting entries for indirect and warning symbols into entries
4267 for the target symbol. Set DATA->g to null on error. */
4268
4269 static int
4270 mips_elf_recreate_got (void **entryp, void *data)
4271 {
4272 struct mips_got_entry new_entry, *entry;
4273 struct mips_elf_traverse_got_arg *arg;
4274 void **slot;
4275
4276 entry = (struct mips_got_entry *) *entryp;
4277 arg = (struct mips_elf_traverse_got_arg *) data;
4278 if (entry->abfd != NULL
4279 && entry->symndx == -1
4280 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4281 || entry->d.h->root.root.type == bfd_link_hash_warning))
4282 {
4283 struct mips_elf_link_hash_entry *h;
4284
4285 new_entry = *entry;
4286 entry = &new_entry;
4287 h = entry->d.h;
4288 do
4289 {
4290 BFD_ASSERT (h->global_got_area == GGA_NONE);
4291 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4292 }
4293 while (h->root.root.type == bfd_link_hash_indirect
4294 || h->root.root.type == bfd_link_hash_warning);
4295 entry->d.h = h;
4296 }
4297 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4298 if (slot == NULL)
4299 {
4300 arg->g = NULL;
4301 return 0;
4302 }
4303 if (*slot == NULL)
4304 {
4305 if (entry == &new_entry)
4306 {
4307 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4308 if (!entry)
4309 {
4310 arg->g = NULL;
4311 return 0;
4312 }
4313 *entry = new_entry;
4314 }
4315 *slot = entry;
4316 mips_elf_count_got_entry (arg->info, arg->g, entry);
4317 }
4318 return 1;
4319 }
4320
4321 /* Return the maximum number of GOT page entries required for RANGE. */
4322
4323 static bfd_vma
4324 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4325 {
4326 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4327 }
4328
4329 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4330
4331 static bool
4332 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4333 asection *sec, bfd_signed_vma addend)
4334 {
4335 struct mips_got_info *g = arg->g;
4336 struct mips_got_page_entry lookup, *entry;
4337 struct mips_got_page_range **range_ptr, *range;
4338 bfd_vma old_pages, new_pages;
4339 void **loc;
4340
4341 /* Find the mips_got_page_entry hash table entry for this section. */
4342 lookup.sec = sec;
4343 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4344 if (loc == NULL)
4345 return false;
4346
4347 /* Create a mips_got_page_entry if this is the first time we've
4348 seen the section. */
4349 entry = (struct mips_got_page_entry *) *loc;
4350 if (!entry)
4351 {
4352 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4353 if (!entry)
4354 return false;
4355
4356 entry->sec = sec;
4357 *loc = entry;
4358 }
4359
4360 /* Skip over ranges whose maximum extent cannot share a page entry
4361 with ADDEND. */
4362 range_ptr = &entry->ranges;
4363 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4364 range_ptr = &(*range_ptr)->next;
4365
4366 /* If we scanned to the end of the list, or found a range whose
4367 minimum extent cannot share a page entry with ADDEND, create
4368 a new singleton range. */
4369 range = *range_ptr;
4370 if (!range || addend < range->min_addend - 0xffff)
4371 {
4372 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4373 if (!range)
4374 return false;
4375
4376 range->next = *range_ptr;
4377 range->min_addend = addend;
4378 range->max_addend = addend;
4379
4380 *range_ptr = range;
4381 entry->num_pages++;
4382 g->page_gotno++;
4383 return true;
4384 }
4385
4386 /* Remember how many pages the old range contributed. */
4387 old_pages = mips_elf_pages_for_range (range);
4388
4389 /* Update the ranges. */
4390 if (addend < range->min_addend)
4391 range->min_addend = addend;
4392 else if (addend > range->max_addend)
4393 {
4394 if (range->next && addend >= range->next->min_addend - 0xffff)
4395 {
4396 old_pages += mips_elf_pages_for_range (range->next);
4397 range->max_addend = range->next->max_addend;
4398 range->next = range->next->next;
4399 }
4400 else
4401 range->max_addend = addend;
4402 }
4403
4404 /* Record any change in the total estimate. */
4405 new_pages = mips_elf_pages_for_range (range);
4406 if (old_pages != new_pages)
4407 {
4408 entry->num_pages += new_pages - old_pages;
4409 g->page_gotno += new_pages - old_pages;
4410 }
4411
4412 return true;
4413 }
4414
4415 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4416 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4417 whether the page reference described by *REFP needs a GOT page entry,
4418 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4419
4420 static int
4421 mips_elf_resolve_got_page_ref (void **refp, void *data)
4422 {
4423 struct mips_got_page_ref *ref;
4424 struct mips_elf_traverse_got_arg *arg;
4425 struct mips_elf_link_hash_table *htab;
4426 asection *sec;
4427 bfd_vma addend;
4428
4429 ref = (struct mips_got_page_ref *) *refp;
4430 arg = (struct mips_elf_traverse_got_arg *) data;
4431 htab = mips_elf_hash_table (arg->info);
4432
4433 if (ref->symndx < 0)
4434 {
4435 struct mips_elf_link_hash_entry *h;
4436
4437 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4438 h = ref->u.h;
4439 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4440 return 1;
4441
4442 /* Ignore undefined symbols; we'll issue an error later if
4443 appropriate. */
4444 if (!((h->root.root.type == bfd_link_hash_defined
4445 || h->root.root.type == bfd_link_hash_defweak)
4446 && h->root.root.u.def.section))
4447 return 1;
4448
4449 sec = h->root.root.u.def.section;
4450 addend = h->root.root.u.def.value + ref->addend;
4451 }
4452 else
4453 {
4454 Elf_Internal_Sym *isym;
4455
4456 /* Read in the symbol. */
4457 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4458 ref->symndx);
4459 if (isym == NULL)
4460 {
4461 arg->g = NULL;
4462 return 0;
4463 }
4464
4465 /* Get the associated input section. */
4466 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4467 if (sec == NULL)
4468 {
4469 arg->g = NULL;
4470 return 0;
4471 }
4472
4473 /* If this is a mergable section, work out the section and offset
4474 of the merged data. For section symbols, the addend specifies
4475 of the offset _of_ the first byte in the data, otherwise it
4476 specifies the offset _from_ the first byte. */
4477 if (sec->flags & SEC_MERGE)
4478 {
4479 void *secinfo;
4480
4481 secinfo = elf_section_data (sec)->sec_info;
4482 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4483 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4484 isym->st_value + ref->addend);
4485 else
4486 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4487 isym->st_value) + ref->addend;
4488 }
4489 else
4490 addend = isym->st_value + ref->addend;
4491 }
4492 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4493 {
4494 arg->g = NULL;
4495 return 0;
4496 }
4497 return 1;
4498 }
4499
4500 /* If any entries in G->got_entries are for indirect or warning symbols,
4501 replace them with entries for the target symbol. Convert g->got_page_refs
4502 into got_page_entry structures and estimate the number of page entries
4503 that they require. */
4504
4505 static bool
4506 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4507 struct mips_got_info *g)
4508 {
4509 struct mips_elf_traverse_got_arg tga;
4510 struct mips_got_info oldg;
4511
4512 oldg = *g;
4513
4514 tga.info = info;
4515 tga.g = g;
4516 tga.value = false;
4517 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4518 if (tga.value)
4519 {
4520 *g = oldg;
4521 g->got_entries = htab_create (htab_size (oldg.got_entries),
4522 mips_elf_got_entry_hash,
4523 mips_elf_got_entry_eq, NULL);
4524 if (!g->got_entries)
4525 return false;
4526
4527 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4528 if (!tga.g)
4529 return false;
4530
4531 htab_delete (oldg.got_entries);
4532 }
4533
4534 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4535 mips_got_page_entry_eq, NULL);
4536 if (g->got_page_entries == NULL)
4537 return false;
4538
4539 tga.info = info;
4540 tga.g = g;
4541 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4542
4543 return true;
4544 }
4545
4546 /* Return true if a GOT entry for H should live in the local rather than
4547 global GOT area. */
4548
4549 static bool
4550 mips_use_local_got_p (struct bfd_link_info *info,
4551 struct mips_elf_link_hash_entry *h)
4552 {
4553 /* Symbols that aren't in the dynamic symbol table must live in the
4554 local GOT. This includes symbols that are completely undefined
4555 and which therefore don't bind locally. We'll report undefined
4556 symbols later if appropriate. */
4557 if (h->root.dynindx == -1)
4558 return true;
4559
4560 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4561 to the local GOT, as they would be implicitly relocated by the
4562 base address by the dynamic loader. */
4563 if (bfd_is_abs_symbol (&h->root.root))
4564 return false;
4565
4566 /* Symbols that bind locally can (and in the case of forced-local
4567 symbols, must) live in the local GOT. */
4568 if (h->got_only_for_calls
4569 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4570 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4571 return true;
4572
4573 /* If this is an executable that must provide a definition of the symbol,
4574 either though PLTs or copy relocations, then that address should go in
4575 the local rather than global GOT. */
4576 if (bfd_link_executable (info) && h->has_static_relocs)
4577 return true;
4578
4579 return false;
4580 }
4581
4582 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4583 link_info structure. Decide whether the hash entry needs an entry in
4584 the global part of the primary GOT, setting global_got_area accordingly.
4585 Count the number of global symbols that are in the primary GOT only
4586 because they have relocations against them (reloc_only_gotno). */
4587
4588 static bool
4589 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4590 {
4591 struct bfd_link_info *info;
4592 struct mips_elf_link_hash_table *htab;
4593 struct mips_got_info *g;
4594
4595 info = (struct bfd_link_info *) data;
4596 htab = mips_elf_hash_table (info);
4597 g = htab->got_info;
4598 if (h->global_got_area != GGA_NONE)
4599 {
4600 /* Make a final decision about whether the symbol belongs in the
4601 local or global GOT. */
4602 if (mips_use_local_got_p (info, h))
4603 /* The symbol belongs in the local GOT. We no longer need this
4604 entry if it was only used for relocations; those relocations
4605 will be against the null or section symbol instead of H. */
4606 h->global_got_area = GGA_NONE;
4607 else if (htab->root.target_os == is_vxworks
4608 && h->got_only_for_calls
4609 && h->root.plt.plist->mips_offset != MINUS_ONE)
4610 /* On VxWorks, calls can refer directly to the .got.plt entry;
4611 they don't need entries in the regular GOT. .got.plt entries
4612 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4613 h->global_got_area = GGA_NONE;
4614 else if (h->global_got_area == GGA_RELOC_ONLY)
4615 {
4616 g->reloc_only_gotno++;
4617 g->global_gotno++;
4618 }
4619 }
4620 return 1;
4621 }
4622 \f
4623 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4624 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4625
4626 static int
4627 mips_elf_add_got_entry (void **entryp, void *data)
4628 {
4629 struct mips_got_entry *entry;
4630 struct mips_elf_traverse_got_arg *arg;
4631 void **slot;
4632
4633 entry = (struct mips_got_entry *) *entryp;
4634 arg = (struct mips_elf_traverse_got_arg *) data;
4635 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4636 if (!slot)
4637 {
4638 arg->g = NULL;
4639 return 0;
4640 }
4641 if (!*slot)
4642 {
4643 *slot = entry;
4644 mips_elf_count_got_entry (arg->info, arg->g, entry);
4645 }
4646 return 1;
4647 }
4648
4649 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4650 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4651
4652 static int
4653 mips_elf_add_got_page_entry (void **entryp, void *data)
4654 {
4655 struct mips_got_page_entry *entry;
4656 struct mips_elf_traverse_got_arg *arg;
4657 void **slot;
4658
4659 entry = (struct mips_got_page_entry *) *entryp;
4660 arg = (struct mips_elf_traverse_got_arg *) data;
4661 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4662 if (!slot)
4663 {
4664 arg->g = NULL;
4665 return 0;
4666 }
4667 if (!*slot)
4668 {
4669 *slot = entry;
4670 arg->g->page_gotno += entry->num_pages;
4671 }
4672 return 1;
4673 }
4674
4675 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4676 this would lead to overflow, 1 if they were merged successfully,
4677 and 0 if a merge failed due to lack of memory. (These values are chosen
4678 so that nonnegative return values can be returned by a htab_traverse
4679 callback.) */
4680
4681 static int
4682 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4683 struct mips_got_info *to,
4684 struct mips_elf_got_per_bfd_arg *arg)
4685 {
4686 struct mips_elf_traverse_got_arg tga;
4687 unsigned int estimate;
4688
4689 /* Work out how many page entries we would need for the combined GOT. */
4690 estimate = arg->max_pages;
4691 if (estimate >= from->page_gotno + to->page_gotno)
4692 estimate = from->page_gotno + to->page_gotno;
4693
4694 /* And conservatively estimate how many local and TLS entries
4695 would be needed. */
4696 estimate += from->local_gotno + to->local_gotno;
4697 estimate += from->tls_gotno + to->tls_gotno;
4698
4699 /* If we're merging with the primary got, any TLS relocations will
4700 come after the full set of global entries. Otherwise estimate those
4701 conservatively as well. */
4702 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4703 estimate += arg->global_count;
4704 else
4705 estimate += from->global_gotno + to->global_gotno;
4706
4707 /* Bail out if the combined GOT might be too big. */
4708 if (estimate > arg->max_count)
4709 return -1;
4710
4711 /* Transfer the bfd's got information from FROM to TO. */
4712 tga.info = arg->info;
4713 tga.g = to;
4714 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4715 if (!tga.g)
4716 return 0;
4717
4718 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4719 if (!tga.g)
4720 return 0;
4721
4722 mips_elf_replace_bfd_got (abfd, to);
4723 return 1;
4724 }
4725
4726 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4727 as possible of the primary got, since it doesn't require explicit
4728 dynamic relocations, but don't use bfds that would reference global
4729 symbols out of the addressable range. Failing the primary got,
4730 attempt to merge with the current got, or finish the current got
4731 and then make make the new got current. */
4732
4733 static bool
4734 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4735 struct mips_elf_got_per_bfd_arg *arg)
4736 {
4737 unsigned int estimate;
4738 int result;
4739
4740 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4741 return false;
4742
4743 /* Work out the number of page, local and TLS entries. */
4744 estimate = arg->max_pages;
4745 if (estimate > g->page_gotno)
4746 estimate = g->page_gotno;
4747 estimate += g->local_gotno + g->tls_gotno;
4748
4749 /* We place TLS GOT entries after both locals and globals. The globals
4750 for the primary GOT may overflow the normal GOT size limit, so be
4751 sure not to merge a GOT which requires TLS with the primary GOT in that
4752 case. This doesn't affect non-primary GOTs. */
4753 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4754
4755 if (estimate <= arg->max_count)
4756 {
4757 /* If we don't have a primary GOT, use it as
4758 a starting point for the primary GOT. */
4759 if (!arg->primary)
4760 {
4761 arg->primary = g;
4762 return true;
4763 }
4764
4765 /* Try merging with the primary GOT. */
4766 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4767 if (result >= 0)
4768 return result;
4769 }
4770
4771 /* If we can merge with the last-created got, do it. */
4772 if (arg->current)
4773 {
4774 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4775 if (result >= 0)
4776 return result;
4777 }
4778
4779 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4780 fits; if it turns out that it doesn't, we'll get relocation
4781 overflows anyway. */
4782 g->next = arg->current;
4783 arg->current = g;
4784
4785 return true;
4786 }
4787
4788 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4789 to GOTIDX, duplicating the entry if it has already been assigned
4790 an index in a different GOT. */
4791
4792 static bool
4793 mips_elf_set_gotidx (void **entryp, long gotidx)
4794 {
4795 struct mips_got_entry *entry;
4796
4797 entry = (struct mips_got_entry *) *entryp;
4798 if (entry->gotidx > 0)
4799 {
4800 struct mips_got_entry *new_entry;
4801
4802 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4803 if (!new_entry)
4804 return false;
4805
4806 *new_entry = *entry;
4807 *entryp = new_entry;
4808 entry = new_entry;
4809 }
4810 entry->gotidx = gotidx;
4811 return true;
4812 }
4813
4814 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4815 mips_elf_traverse_got_arg in which DATA->value is the size of one
4816 GOT entry. Set DATA->g to null on failure. */
4817
4818 static int
4819 mips_elf_initialize_tls_index (void **entryp, void *data)
4820 {
4821 struct mips_got_entry *entry;
4822 struct mips_elf_traverse_got_arg *arg;
4823
4824 /* We're only interested in TLS symbols. */
4825 entry = (struct mips_got_entry *) *entryp;
4826 if (entry->tls_type == GOT_TLS_NONE)
4827 return 1;
4828
4829 arg = (struct mips_elf_traverse_got_arg *) data;
4830 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4831 {
4832 arg->g = NULL;
4833 return 0;
4834 }
4835
4836 /* Account for the entries we've just allocated. */
4837 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4838 return 1;
4839 }
4840
4841 /* A htab_traverse callback for GOT entries, where DATA points to a
4842 mips_elf_traverse_got_arg. Set the global_got_area of each global
4843 symbol to DATA->value. */
4844
4845 static int
4846 mips_elf_set_global_got_area (void **entryp, void *data)
4847 {
4848 struct mips_got_entry *entry;
4849 struct mips_elf_traverse_got_arg *arg;
4850
4851 entry = (struct mips_got_entry *) *entryp;
4852 arg = (struct mips_elf_traverse_got_arg *) data;
4853 if (entry->abfd != NULL
4854 && entry->symndx == -1
4855 && entry->d.h->global_got_area != GGA_NONE)
4856 entry->d.h->global_got_area = arg->value;
4857 return 1;
4858 }
4859
4860 /* A htab_traverse callback for secondary GOT entries, where DATA points
4861 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4862 and record the number of relocations they require. DATA->value is
4863 the size of one GOT entry. Set DATA->g to null on failure. */
4864
4865 static int
4866 mips_elf_set_global_gotidx (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 {
4877 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4878 {
4879 arg->g = NULL;
4880 return 0;
4881 }
4882 arg->g->assigned_low_gotno += 1;
4883
4884 if (bfd_link_pic (arg->info)
4885 || (elf_hash_table (arg->info)->dynamic_sections_created
4886 && entry->d.h->root.def_dynamic
4887 && !entry->d.h->root.def_regular))
4888 arg->g->relocs += 1;
4889 }
4890
4891 return 1;
4892 }
4893
4894 /* A htab_traverse callback for GOT entries for which DATA is the
4895 bfd_link_info. Forbid any global symbols from having traditional
4896 lazy-binding stubs. */
4897
4898 static int
4899 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4900 {
4901 struct bfd_link_info *info;
4902 struct mips_elf_link_hash_table *htab;
4903 struct mips_got_entry *entry;
4904
4905 entry = (struct mips_got_entry *) *entryp;
4906 info = (struct bfd_link_info *) data;
4907 htab = mips_elf_hash_table (info);
4908 BFD_ASSERT (htab != NULL);
4909
4910 if (entry->abfd != NULL
4911 && entry->symndx == -1
4912 && entry->d.h->needs_lazy_stub)
4913 {
4914 entry->d.h->needs_lazy_stub = false;
4915 htab->lazy_stub_count--;
4916 }
4917
4918 return 1;
4919 }
4920
4921 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4922 the primary GOT. */
4923 static bfd_vma
4924 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4925 {
4926 if (!g->next)
4927 return 0;
4928
4929 g = mips_elf_bfd_got (ibfd, false);
4930 if (! g)
4931 return 0;
4932
4933 BFD_ASSERT (g->next);
4934
4935 g = g->next;
4936
4937 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4938 * MIPS_ELF_GOT_SIZE (abfd);
4939 }
4940
4941 /* Turn a single GOT that is too big for 16-bit addressing into
4942 a sequence of GOTs, each one 16-bit addressable. */
4943
4944 static bool
4945 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4946 asection *got, bfd_size_type pages)
4947 {
4948 struct mips_elf_link_hash_table *htab;
4949 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4950 struct mips_elf_traverse_got_arg tga;
4951 struct mips_got_info *g, *gg;
4952 unsigned int assign, needed_relocs;
4953 bfd *dynobj, *ibfd;
4954
4955 dynobj = elf_hash_table (info)->dynobj;
4956 htab = mips_elf_hash_table (info);
4957 BFD_ASSERT (htab != NULL);
4958
4959 g = htab->got_info;
4960
4961 got_per_bfd_arg.obfd = abfd;
4962 got_per_bfd_arg.info = info;
4963 got_per_bfd_arg.current = NULL;
4964 got_per_bfd_arg.primary = NULL;
4965 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4966 / MIPS_ELF_GOT_SIZE (abfd))
4967 - htab->reserved_gotno);
4968 got_per_bfd_arg.max_pages = pages;
4969 /* The number of globals that will be included in the primary GOT.
4970 See the calls to mips_elf_set_global_got_area below for more
4971 information. */
4972 got_per_bfd_arg.global_count = g->global_gotno;
4973
4974 /* Try to merge the GOTs of input bfds together, as long as they
4975 don't seem to exceed the maximum GOT size, choosing one of them
4976 to be the primary GOT. */
4977 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4978 {
4979 gg = mips_elf_bfd_got (ibfd, false);
4980 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4981 return false;
4982 }
4983
4984 /* If we do not find any suitable primary GOT, create an empty one. */
4985 if (got_per_bfd_arg.primary == NULL)
4986 g->next = mips_elf_create_got_info (abfd);
4987 else
4988 g->next = got_per_bfd_arg.primary;
4989 g->next->next = got_per_bfd_arg.current;
4990
4991 /* GG is now the master GOT, and G is the primary GOT. */
4992 gg = g;
4993 g = g->next;
4994
4995 /* Map the output bfd to the primary got. That's what we're going
4996 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4997 didn't mark in check_relocs, and we want a quick way to find it.
4998 We can't just use gg->next because we're going to reverse the
4999 list. */
5000 mips_elf_replace_bfd_got (abfd, g);
5001
5002 /* Every symbol that is referenced in a dynamic relocation must be
5003 present in the primary GOT, so arrange for them to appear after
5004 those that are actually referenced. */
5005 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
5006 g->global_gotno = gg->global_gotno;
5007
5008 tga.info = info;
5009 tga.value = GGA_RELOC_ONLY;
5010 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5011 tga.value = GGA_NORMAL;
5012 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
5013
5014 /* Now go through the GOTs assigning them offset ranges.
5015 [assigned_low_gotno, local_gotno[ will be set to the range of local
5016 entries in each GOT. We can then compute the end of a GOT by
5017 adding local_gotno to global_gotno. We reverse the list and make
5018 it circular since then we'll be able to quickly compute the
5019 beginning of a GOT, by computing the end of its predecessor. To
5020 avoid special cases for the primary GOT, while still preserving
5021 assertions that are valid for both single- and multi-got links,
5022 we arrange for the main got struct to have the right number of
5023 global entries, but set its local_gotno such that the initial
5024 offset of the primary GOT is zero. Remember that the primary GOT
5025 will become the last item in the circular linked list, so it
5026 points back to the master GOT. */
5027 gg->local_gotno = -g->global_gotno;
5028 gg->global_gotno = g->global_gotno;
5029 gg->tls_gotno = 0;
5030 assign = 0;
5031 gg->next = gg;
5032
5033 do
5034 {
5035 struct mips_got_info *gn;
5036
5037 assign += htab->reserved_gotno;
5038 g->assigned_low_gotno = assign;
5039 g->local_gotno += assign;
5040 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
5041 g->assigned_high_gotno = g->local_gotno - 1;
5042 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5043
5044 /* Take g out of the direct list, and push it onto the reversed
5045 list that gg points to. g->next is guaranteed to be nonnull after
5046 this operation, as required by mips_elf_initialize_tls_index. */
5047 gn = g->next;
5048 g->next = gg->next;
5049 gg->next = g;
5050
5051 /* Set up any TLS entries. We always place the TLS entries after
5052 all non-TLS entries. */
5053 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
5054 tga.g = g;
5055 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5056 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5057 if (!tga.g)
5058 return false;
5059 BFD_ASSERT (g->tls_assigned_gotno == assign);
5060
5061 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
5062 g = gn;
5063
5064 /* Forbid global symbols in every non-primary GOT from having
5065 lazy-binding stubs. */
5066 if (g)
5067 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5068 }
5069 while (g);
5070
5071 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5072
5073 needed_relocs = 0;
5074 for (g = gg->next; g && g->next != gg; g = g->next)
5075 {
5076 unsigned int save_assign;
5077
5078 /* Assign offsets to global GOT entries and count how many
5079 relocations they need. */
5080 save_assign = g->assigned_low_gotno;
5081 g->assigned_low_gotno = g->local_gotno;
5082 tga.info = info;
5083 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5084 tga.g = g;
5085 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5086 if (!tga.g)
5087 return false;
5088 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5089 g->assigned_low_gotno = save_assign;
5090
5091 if (bfd_link_pic (info))
5092 {
5093 g->relocs += g->local_gotno - g->assigned_low_gotno;
5094 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5095 + g->next->global_gotno
5096 + g->next->tls_gotno
5097 + htab->reserved_gotno);
5098 }
5099 needed_relocs += g->relocs;
5100 }
5101 needed_relocs += g->relocs;
5102
5103 if (needed_relocs)
5104 mips_elf_allocate_dynamic_relocations (dynobj, info,
5105 needed_relocs);
5106
5107 return true;
5108 }
5109
5110 \f
5111 /* Returns the first relocation of type r_type found, beginning with
5112 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5113
5114 static const Elf_Internal_Rela *
5115 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5116 const Elf_Internal_Rela *relocation,
5117 const Elf_Internal_Rela *relend)
5118 {
5119 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5120
5121 while (relocation < relend)
5122 {
5123 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5124 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5125 return relocation;
5126
5127 ++relocation;
5128 }
5129
5130 /* We didn't find it. */
5131 return NULL;
5132 }
5133
5134 /* Return whether an input relocation is against a local symbol. */
5135
5136 static bool
5137 mips_elf_local_relocation_p (bfd *input_bfd,
5138 const Elf_Internal_Rela *relocation,
5139 asection **local_sections)
5140 {
5141 unsigned long r_symndx;
5142 Elf_Internal_Shdr *symtab_hdr;
5143 size_t extsymoff;
5144
5145 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5146 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5147 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5148
5149 if (r_symndx < extsymoff)
5150 return true;
5151 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5152 return true;
5153
5154 return false;
5155 }
5156 \f
5157 /* Sign-extend VALUE, which has the indicated number of BITS. */
5158
5159 bfd_vma
5160 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5161 {
5162 if (value & ((bfd_vma) 1 << (bits - 1)))
5163 /* VALUE is negative. */
5164 value |= ((bfd_vma) - 1) << bits;
5165
5166 return value;
5167 }
5168
5169 /* Return non-zero if the indicated VALUE has overflowed the maximum
5170 range expressible by a signed number with the indicated number of
5171 BITS. */
5172
5173 static bool
5174 mips_elf_overflow_p (bfd_vma value, int bits)
5175 {
5176 bfd_signed_vma svalue = (bfd_signed_vma) value;
5177
5178 if (svalue > (1 << (bits - 1)) - 1)
5179 /* The value is too big. */
5180 return true;
5181 else if (svalue < -(1 << (bits - 1)))
5182 /* The value is too small. */
5183 return true;
5184
5185 /* All is well. */
5186 return false;
5187 }
5188
5189 /* Calculate the %high function. */
5190
5191 static bfd_vma
5192 mips_elf_high (bfd_vma value)
5193 {
5194 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5195 }
5196
5197 /* Calculate the %higher function. */
5198
5199 static bfd_vma
5200 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5201 {
5202 #ifdef BFD64
5203 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5204 #else
5205 abort ();
5206 return MINUS_ONE;
5207 #endif
5208 }
5209
5210 /* Calculate the %highest function. */
5211
5212 static bfd_vma
5213 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5214 {
5215 #ifdef BFD64
5216 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5217 #else
5218 abort ();
5219 return MINUS_ONE;
5220 #endif
5221 }
5222 \f
5223 /* Create the .compact_rel section. */
5224
5225 static bool
5226 mips_elf_create_compact_rel_section
5227 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5228 {
5229 flagword flags;
5230 register asection *s;
5231
5232 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5233 {
5234 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5235 | SEC_READONLY);
5236
5237 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5238 if (s == NULL
5239 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5240 return false;
5241
5242 s->size = sizeof (Elf32_External_compact_rel);
5243 }
5244
5245 return true;
5246 }
5247
5248 /* Create the .got section to hold the global offset table. */
5249
5250 static bool
5251 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5252 {
5253 flagword flags;
5254 register asection *s;
5255 struct elf_link_hash_entry *h;
5256 struct bfd_link_hash_entry *bh;
5257 struct mips_elf_link_hash_table *htab;
5258
5259 htab = mips_elf_hash_table (info);
5260 BFD_ASSERT (htab != NULL);
5261
5262 /* This function may be called more than once. */
5263 if (htab->root.sgot)
5264 return true;
5265
5266 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5267 | SEC_LINKER_CREATED);
5268
5269 /* We have to use an alignment of 2**4 here because this is hardcoded
5270 in the function stub generation and in the linker script. */
5271 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5272 if (s == NULL
5273 || !bfd_set_section_alignment (s, 4))
5274 return false;
5275 htab->root.sgot = s;
5276
5277 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5278 linker script because we don't want to define the symbol if we
5279 are not creating a global offset table. */
5280 bh = NULL;
5281 if (! (_bfd_generic_link_add_one_symbol
5282 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5283 0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5284 return false;
5285
5286 h = (struct elf_link_hash_entry *) bh;
5287 h->non_elf = 0;
5288 h->def_regular = 1;
5289 h->type = STT_OBJECT;
5290 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5291 elf_hash_table (info)->hgot = h;
5292
5293 if (bfd_link_pic (info)
5294 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5295 return false;
5296
5297 htab->got_info = mips_elf_create_got_info (abfd);
5298 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5299 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5300
5301 /* We also need a .got.plt section when generating PLTs. */
5302 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5303 SEC_ALLOC | SEC_LOAD
5304 | SEC_HAS_CONTENTS
5305 | SEC_IN_MEMORY
5306 | SEC_LINKER_CREATED);
5307 if (s == NULL)
5308 return false;
5309 htab->root.sgotplt = s;
5310
5311 return true;
5312 }
5313 \f
5314 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5315 __GOTT_INDEX__ symbols. These symbols are only special for
5316 shared objects; they are not used in executables. */
5317
5318 static bool
5319 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5320 {
5321 return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5322 && bfd_link_pic (info)
5323 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5324 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5325 }
5326
5327 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5328 require an la25 stub. See also mips_elf_local_pic_function_p,
5329 which determines whether the destination function ever requires a
5330 stub. */
5331
5332 static bool
5333 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5334 bool target_is_16_bit_code_p)
5335 {
5336 /* We specifically ignore branches and jumps from EF_PIC objects,
5337 where the onus is on the compiler or programmer to perform any
5338 necessary initialization of $25. Sometimes such initialization
5339 is unnecessary; for example, -mno-shared functions do not use
5340 the incoming value of $25, and may therefore be called directly. */
5341 if (PIC_OBJECT_P (input_bfd))
5342 return false;
5343
5344 switch (r_type)
5345 {
5346 case R_MIPS_26:
5347 case R_MIPS_PC16:
5348 case R_MIPS_PC21_S2:
5349 case R_MIPS_PC26_S2:
5350 case R_MICROMIPS_26_S1:
5351 case R_MICROMIPS_PC7_S1:
5352 case R_MICROMIPS_PC10_S1:
5353 case R_MICROMIPS_PC16_S1:
5354 case R_MICROMIPS_PC23_S2:
5355 return true;
5356
5357 case R_MIPS16_26:
5358 return !target_is_16_bit_code_p;
5359
5360 default:
5361 return false;
5362 }
5363 }
5364 \f
5365 /* Obtain the field relocated by RELOCATION. */
5366
5367 static bfd_vma
5368 mips_elf_obtain_contents (reloc_howto_type *howto,
5369 const Elf_Internal_Rela *relocation,
5370 bfd *input_bfd, bfd_byte *contents)
5371 {
5372 bfd_vma x = 0;
5373 bfd_byte *location = contents + relocation->r_offset;
5374 unsigned int size = bfd_get_reloc_size (howto);
5375
5376 /* Obtain the bytes. */
5377 if (size != 0)
5378 x = bfd_get (8 * size, input_bfd, location);
5379
5380 return x;
5381 }
5382
5383 /* Store the field relocated by RELOCATION. */
5384
5385 static void
5386 mips_elf_store_contents (reloc_howto_type *howto,
5387 const Elf_Internal_Rela *relocation,
5388 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5389 {
5390 bfd_byte *location = contents + relocation->r_offset;
5391 unsigned int size = bfd_get_reloc_size (howto);
5392
5393 /* Put the value into the output. */
5394 if (size != 0)
5395 bfd_put (8 * size, input_bfd, x, location);
5396 }
5397
5398 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
5399 RELOCATION described by HOWTO, with a move of 0 to the load target
5400 register, returning TRUE if that is successful and FALSE otherwise.
5401 If DOIT is FALSE, then only determine it patching is possible and
5402 return status without actually changing CONTENTS.
5403 */
5404
5405 static bool
5406 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5407 const Elf_Internal_Rela *relocation,
5408 reloc_howto_type *howto, bool doit)
5409 {
5410 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5411 bfd_byte *location = contents + relocation->r_offset;
5412 bool nullified = true;
5413 bfd_vma x;
5414
5415 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
5416
5417 /* Obtain the current value. */
5418 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5419
5420 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5421 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5422 if (mips16_reloc_p (r_type)
5423 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5424 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
5425 x = (0x3cdU << 22) | (x & (7 << 16)) << 3; /* LI */
5426 else if (micromips_reloc_p (r_type)
5427 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5428 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5429 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5430 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5431 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5432 else
5433 nullified = false;
5434
5435 /* Put the value into the output. */
5436 if (doit && nullified)
5437 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5438
5439 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
5440
5441 return nullified;
5442 }
5443
5444 /* Calculate the value produced by the RELOCATION (which comes from
5445 the INPUT_BFD). The ADDEND is the addend to use for this
5446 RELOCATION; RELOCATION->R_ADDEND is ignored.
5447
5448 The result of the relocation calculation is stored in VALUEP.
5449 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5450 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5451
5452 This function returns bfd_reloc_continue if the caller need take no
5453 further action regarding this relocation, bfd_reloc_notsupported if
5454 something goes dramatically wrong, bfd_reloc_overflow if an
5455 overflow occurs, and bfd_reloc_ok to indicate success. */
5456
5457 static bfd_reloc_status_type
5458 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5459 asection *input_section, bfd_byte *contents,
5460 struct bfd_link_info *info,
5461 const Elf_Internal_Rela *relocation,
5462 bfd_vma addend, reloc_howto_type *howto,
5463 Elf_Internal_Sym *local_syms,
5464 asection **local_sections, bfd_vma *valuep,
5465 const char **namep,
5466 bool *cross_mode_jump_p,
5467 bool save_addend)
5468 {
5469 /* The eventual value we will return. */
5470 bfd_vma value;
5471 /* The address of the symbol against which the relocation is
5472 occurring. */
5473 bfd_vma symbol = 0;
5474 /* The final GP value to be used for the relocatable, executable, or
5475 shared object file being produced. */
5476 bfd_vma gp;
5477 /* The place (section offset or address) of the storage unit being
5478 relocated. */
5479 bfd_vma p;
5480 /* The value of GP used to create the relocatable object. */
5481 bfd_vma gp0;
5482 /* The offset into the global offset table at which the address of
5483 the relocation entry symbol, adjusted by the addend, resides
5484 during execution. */
5485 bfd_vma g = MINUS_ONE;
5486 /* The section in which the symbol referenced by the relocation is
5487 located. */
5488 asection *sec = NULL;
5489 struct mips_elf_link_hash_entry *h = NULL;
5490 /* TRUE if the symbol referred to by this relocation is a local
5491 symbol. */
5492 bool local_p, was_local_p;
5493 /* TRUE if the symbol referred to by this relocation is a section
5494 symbol. */
5495 bool section_p = false;
5496 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5497 bool gp_disp_p = false;
5498 /* TRUE if the symbol referred to by this relocation is
5499 "__gnu_local_gp". */
5500 bool gnu_local_gp_p = false;
5501 Elf_Internal_Shdr *symtab_hdr;
5502 size_t extsymoff;
5503 unsigned long r_symndx;
5504 int r_type;
5505 /* TRUE if overflow occurred during the calculation of the
5506 relocation value. */
5507 bool overflowed_p;
5508 /* TRUE if this relocation refers to a MIPS16 function. */
5509 bool target_is_16_bit_code_p = false;
5510 bool target_is_micromips_code_p = false;
5511 struct mips_elf_link_hash_table *htab;
5512 bfd *dynobj;
5513 bool resolved_to_zero;
5514
5515 dynobj = elf_hash_table (info)->dynobj;
5516 htab = mips_elf_hash_table (info);
5517 BFD_ASSERT (htab != NULL);
5518
5519 /* Parse the relocation. */
5520 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5521 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5522 p = (input_section->output_section->vma
5523 + input_section->output_offset
5524 + relocation->r_offset);
5525
5526 /* Assume that there will be no overflow. */
5527 overflowed_p = false;
5528
5529 /* Figure out whether or not the symbol is local, and get the offset
5530 used in the array of hash table entries. */
5531 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5532 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5533 local_sections);
5534 was_local_p = local_p;
5535 if (! elf_bad_symtab (input_bfd))
5536 extsymoff = symtab_hdr->sh_info;
5537 else
5538 {
5539 /* The symbol table does not follow the rule that local symbols
5540 must come before globals. */
5541 extsymoff = 0;
5542 }
5543
5544 /* Figure out the value of the symbol. */
5545 if (local_p)
5546 {
5547 bool micromips_p = MICROMIPS_P (abfd);
5548 Elf_Internal_Sym *sym;
5549
5550 sym = local_syms + r_symndx;
5551 sec = local_sections[r_symndx];
5552
5553 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5554
5555 symbol = sec->output_section->vma + sec->output_offset;
5556 if (!section_p || (sec->flags & SEC_MERGE))
5557 symbol += sym->st_value;
5558 if ((sec->flags & SEC_MERGE) && section_p)
5559 {
5560 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5561 addend -= symbol;
5562 addend += sec->output_section->vma + sec->output_offset;
5563 }
5564
5565 /* MIPS16/microMIPS text labels should be treated as odd. */
5566 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5567 ++symbol;
5568
5569 /* Record the name of this symbol, for our caller. */
5570 *namep = bfd_elf_string_from_elf_section (input_bfd,
5571 symtab_hdr->sh_link,
5572 sym->st_name);
5573 if (*namep == NULL || **namep == '\0')
5574 *namep = bfd_section_name (sec);
5575
5576 /* For relocations against a section symbol and ones against no
5577 symbol (absolute relocations) infer the ISA mode from the addend. */
5578 if (section_p || r_symndx == STN_UNDEF)
5579 {
5580 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5581 target_is_micromips_code_p = (addend & 1) && micromips_p;
5582 }
5583 /* For relocations against an absolute symbol infer the ISA mode
5584 from the value of the symbol plus addend. */
5585 else if (bfd_is_abs_section (sec))
5586 {
5587 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5588 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5589 }
5590 /* Otherwise just use the regular symbol annotation available. */
5591 else
5592 {
5593 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5594 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5595 }
5596 }
5597 else
5598 {
5599 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5600
5601 /* For global symbols we look up the symbol in the hash-table. */
5602 h = ((struct mips_elf_link_hash_entry *)
5603 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5604 /* Find the real hash-table entry for this symbol. */
5605 while (h->root.root.type == bfd_link_hash_indirect
5606 || h->root.root.type == bfd_link_hash_warning)
5607 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5608
5609 /* Record the name of this symbol, for our caller. */
5610 *namep = h->root.root.root.string;
5611
5612 /* See if this is the special _gp_disp symbol. Note that such a
5613 symbol must always be a global symbol. */
5614 if (strcmp (*namep, "_gp_disp") == 0
5615 && ! NEWABI_P (input_bfd))
5616 {
5617 /* Relocations against _gp_disp are permitted only with
5618 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5619 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5620 return bfd_reloc_notsupported;
5621
5622 gp_disp_p = true;
5623 }
5624 /* See if this is the special _gp symbol. Note that such a
5625 symbol must always be a global symbol. */
5626 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5627 gnu_local_gp_p = true;
5628
5629
5630 /* If this symbol is defined, calculate its address. Note that
5631 _gp_disp is a magic symbol, always implicitly defined by the
5632 linker, so it's inappropriate to check to see whether or not
5633 its defined. */
5634 else if ((h->root.root.type == bfd_link_hash_defined
5635 || h->root.root.type == bfd_link_hash_defweak)
5636 && h->root.root.u.def.section)
5637 {
5638 sec = h->root.root.u.def.section;
5639 if (sec->output_section)
5640 symbol = (h->root.root.u.def.value
5641 + sec->output_section->vma
5642 + sec->output_offset);
5643 else
5644 symbol = h->root.root.u.def.value;
5645 }
5646 else if (h->root.root.type == bfd_link_hash_undefweak)
5647 /* We allow relocations against undefined weak symbols, giving
5648 it the value zero, so that you can undefined weak functions
5649 and check to see if they exist by looking at their
5650 addresses. */
5651 symbol = 0;
5652 else if (info->unresolved_syms_in_objects == RM_IGNORE
5653 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5654 symbol = 0;
5655 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5656 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5657 {
5658 /* If this is a dynamic link, we should have created a
5659 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5660 in _bfd_mips_elf_create_dynamic_sections.
5661 Otherwise, we should define the symbol with a value of 0.
5662 FIXME: It should probably get into the symbol table
5663 somehow as well. */
5664 BFD_ASSERT (! bfd_link_pic (info));
5665 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5666 symbol = 0;
5667 }
5668 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5669 {
5670 /* This is an optional symbol - an Irix specific extension to the
5671 ELF spec. Ignore it for now.
5672 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5673 than simply ignoring them, but we do not handle this for now.
5674 For information see the "64-bit ELF Object File Specification"
5675 which is available from here:
5676 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5677 symbol = 0;
5678 }
5679 else
5680 {
5681 bool reject_undefined
5682 = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5683 && !info->warn_unresolved_syms)
5684 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5685
5686 info->callbacks->undefined_symbol
5687 (info, h->root.root.root.string, input_bfd,
5688 input_section, relocation->r_offset, reject_undefined);
5689
5690 if (reject_undefined)
5691 return bfd_reloc_undefined;
5692
5693 symbol = 0;
5694 }
5695
5696 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5697 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5698 }
5699
5700 /* If this is a reference to a 16-bit function with a stub, we need
5701 to redirect the relocation to the stub unless:
5702
5703 (a) the relocation is for a MIPS16 JAL;
5704
5705 (b) the relocation is for a MIPS16 PIC call, and there are no
5706 non-MIPS16 uses of the GOT slot; or
5707
5708 (c) the section allows direct references to MIPS16 functions. */
5709 if (r_type != R_MIPS16_26
5710 && !bfd_link_relocatable (info)
5711 && ((h != NULL
5712 && h->fn_stub != NULL
5713 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5714 || (local_p
5715 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5716 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5717 && !section_allows_mips16_refs_p (input_section))
5718 {
5719 /* This is a 32- or 64-bit call to a 16-bit function. We should
5720 have already noticed that we were going to need the
5721 stub. */
5722 if (local_p)
5723 {
5724 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5725 value = 0;
5726 }
5727 else
5728 {
5729 BFD_ASSERT (h->need_fn_stub);
5730 if (h->la25_stub)
5731 {
5732 /* If a LA25 header for the stub itself exists, point to the
5733 prepended LUI/ADDIU sequence. */
5734 sec = h->la25_stub->stub_section;
5735 value = h->la25_stub->offset;
5736 }
5737 else
5738 {
5739 sec = h->fn_stub;
5740 value = 0;
5741 }
5742 }
5743
5744 symbol = sec->output_section->vma + sec->output_offset + value;
5745 /* The target is 16-bit, but the stub isn't. */
5746 target_is_16_bit_code_p = false;
5747 }
5748 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5749 to a standard MIPS function, we need to redirect the call to the stub.
5750 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5751 indirect calls should use an indirect stub instead. */
5752 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5753 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5754 || (local_p
5755 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5756 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5757 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5758 {
5759 if (local_p)
5760 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5761 else
5762 {
5763 /* If both call_stub and call_fp_stub are defined, we can figure
5764 out which one to use by checking which one appears in the input
5765 file. */
5766 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5767 {
5768 asection *o;
5769
5770 sec = NULL;
5771 for (o = input_bfd->sections; o != NULL; o = o->next)
5772 {
5773 if (CALL_FP_STUB_P (bfd_section_name (o)))
5774 {
5775 sec = h->call_fp_stub;
5776 break;
5777 }
5778 }
5779 if (sec == NULL)
5780 sec = h->call_stub;
5781 }
5782 else if (h->call_stub != NULL)
5783 sec = h->call_stub;
5784 else
5785 sec = h->call_fp_stub;
5786 }
5787
5788 BFD_ASSERT (sec->size > 0);
5789 symbol = sec->output_section->vma + sec->output_offset;
5790 }
5791 /* If this is a direct call to a PIC function, redirect to the
5792 non-PIC stub. */
5793 else if (h != NULL && h->la25_stub
5794 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5795 target_is_16_bit_code_p))
5796 {
5797 symbol = (h->la25_stub->stub_section->output_section->vma
5798 + h->la25_stub->stub_section->output_offset
5799 + h->la25_stub->offset);
5800 if (ELF_ST_IS_MICROMIPS (h->root.other))
5801 symbol |= 1;
5802 }
5803 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5804 entry is used if a standard PLT entry has also been made. In this
5805 case the symbol will have been set by mips_elf_set_plt_sym_value
5806 to point to the standard PLT entry, so redirect to the compressed
5807 one. */
5808 else if ((mips16_branch_reloc_p (r_type)
5809 || micromips_branch_reloc_p (r_type))
5810 && !bfd_link_relocatable (info)
5811 && h != NULL
5812 && h->use_plt_entry
5813 && h->root.plt.plist->comp_offset != MINUS_ONE
5814 && h->root.plt.plist->mips_offset != MINUS_ONE)
5815 {
5816 bool micromips_p = MICROMIPS_P (abfd);
5817
5818 sec = htab->root.splt;
5819 symbol = (sec->output_section->vma
5820 + sec->output_offset
5821 + htab->plt_header_size
5822 + htab->plt_mips_offset
5823 + h->root.plt.plist->comp_offset
5824 + 1);
5825
5826 target_is_16_bit_code_p = !micromips_p;
5827 target_is_micromips_code_p = micromips_p;
5828 }
5829
5830 /* Make sure MIPS16 and microMIPS are not used together. */
5831 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5832 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5833 {
5834 _bfd_error_handler
5835 (_("MIPS16 and microMIPS functions cannot call each other"));
5836 return bfd_reloc_notsupported;
5837 }
5838
5839 /* Calls from 16-bit code to 32-bit code and vice versa require the
5840 mode change. However, we can ignore calls to undefined weak symbols,
5841 which should never be executed at runtime. This exception is important
5842 because the assembly writer may have "known" that any definition of the
5843 symbol would be 16-bit code, and that direct jumps were therefore
5844 acceptable. */
5845 *cross_mode_jump_p = (!bfd_link_relocatable (info)
5846 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5847 && ((mips16_branch_reloc_p (r_type)
5848 && !target_is_16_bit_code_p)
5849 || (micromips_branch_reloc_p (r_type)
5850 && !target_is_micromips_code_p)
5851 || ((branch_reloc_p (r_type)
5852 || r_type == R_MIPS_JALR)
5853 && (target_is_16_bit_code_p
5854 || target_is_micromips_code_p))));
5855
5856 resolved_to_zero = (h != NULL
5857 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5858
5859 switch (r_type)
5860 {
5861 case R_MIPS16_CALL16:
5862 case R_MIPS16_GOT16:
5863 case R_MIPS_CALL16:
5864 case R_MIPS_GOT16:
5865 case R_MIPS_GOT_PAGE:
5866 case R_MIPS_GOT_DISP:
5867 case R_MIPS_GOT_LO16:
5868 case R_MIPS_CALL_LO16:
5869 case R_MICROMIPS_CALL16:
5870 case R_MICROMIPS_GOT16:
5871 case R_MICROMIPS_GOT_PAGE:
5872 case R_MICROMIPS_GOT_DISP:
5873 case R_MICROMIPS_GOT_LO16:
5874 case R_MICROMIPS_CALL_LO16:
5875 if (resolved_to_zero
5876 && !bfd_link_relocatable (info)
5877 && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5878 relocation->r_offset)
5879 && mips_elf_nullify_got_load (input_bfd, contents,
5880 relocation, howto, true))
5881 return bfd_reloc_continue;
5882
5883 /* Fall through. */
5884 case R_MIPS_GOT_HI16:
5885 case R_MIPS_CALL_HI16:
5886 case R_MICROMIPS_GOT_HI16:
5887 case R_MICROMIPS_CALL_HI16:
5888 if (resolved_to_zero
5889 && htab->use_absolute_zero
5890 && bfd_link_pic (info))
5891 {
5892 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5893 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5894 false, false, false);
5895 BFD_ASSERT (h != NULL);
5896 }
5897 break;
5898 }
5899
5900 local_p = (h == NULL || mips_use_local_got_p (info, h));
5901
5902 gp0 = _bfd_get_gp_value (input_bfd);
5903 gp = _bfd_get_gp_value (abfd);
5904 if (htab->got_info)
5905 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5906
5907 if (gnu_local_gp_p)
5908 symbol = gp;
5909
5910 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5911 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5912 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5913 if (got_page_reloc_p (r_type) && !local_p)
5914 {
5915 r_type = (micromips_reloc_p (r_type)
5916 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5917 addend = 0;
5918 }
5919
5920 /* If we haven't already determined the GOT offset, and we're going
5921 to need it, get it now. */
5922 switch (r_type)
5923 {
5924 case R_MIPS16_CALL16:
5925 case R_MIPS16_GOT16:
5926 case R_MIPS_CALL16:
5927 case R_MIPS_GOT16:
5928 case R_MIPS_GOT_DISP:
5929 case R_MIPS_GOT_HI16:
5930 case R_MIPS_CALL_HI16:
5931 case R_MIPS_GOT_LO16:
5932 case R_MIPS_CALL_LO16:
5933 case R_MICROMIPS_CALL16:
5934 case R_MICROMIPS_GOT16:
5935 case R_MICROMIPS_GOT_DISP:
5936 case R_MICROMIPS_GOT_HI16:
5937 case R_MICROMIPS_CALL_HI16:
5938 case R_MICROMIPS_GOT_LO16:
5939 case R_MICROMIPS_CALL_LO16:
5940 case R_MIPS_TLS_GD:
5941 case R_MIPS_TLS_GOTTPREL:
5942 case R_MIPS_TLS_LDM:
5943 case R_MIPS16_TLS_GD:
5944 case R_MIPS16_TLS_GOTTPREL:
5945 case R_MIPS16_TLS_LDM:
5946 case R_MICROMIPS_TLS_GD:
5947 case R_MICROMIPS_TLS_GOTTPREL:
5948 case R_MICROMIPS_TLS_LDM:
5949 /* Find the index into the GOT where this value is located. */
5950 if (tls_ldm_reloc_p (r_type))
5951 {
5952 g = mips_elf_local_got_index (abfd, input_bfd, info,
5953 0, 0, NULL, r_type);
5954 if (g == MINUS_ONE)
5955 return bfd_reloc_outofrange;
5956 }
5957 else if (!local_p)
5958 {
5959 /* On VxWorks, CALL relocations should refer to the .got.plt
5960 entry, which is initialized to point at the PLT stub. */
5961 if (htab->root.target_os == is_vxworks
5962 && (call_hi16_reloc_p (r_type)
5963 || call_lo16_reloc_p (r_type)
5964 || call16_reloc_p (r_type)))
5965 {
5966 BFD_ASSERT (addend == 0);
5967 BFD_ASSERT (h->root.needs_plt);
5968 g = mips_elf_gotplt_index (info, &h->root);
5969 }
5970 else
5971 {
5972 BFD_ASSERT (addend == 0);
5973 g = mips_elf_global_got_index (abfd, info, input_bfd,
5974 &h->root, r_type);
5975 if (!TLS_RELOC_P (r_type)
5976 && !elf_hash_table (info)->dynamic_sections_created)
5977 /* This is a static link. We must initialize the GOT entry. */
5978 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
5979 }
5980 }
5981 else if (htab->root.target_os != is_vxworks
5982 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5983 /* The calculation below does not involve "g". */
5984 break;
5985 else
5986 {
5987 g = mips_elf_local_got_index (abfd, input_bfd, info,
5988 symbol + addend, r_symndx, h, r_type);
5989 if (g == MINUS_ONE)
5990 return bfd_reloc_outofrange;
5991 }
5992
5993 /* Convert GOT indices to actual offsets. */
5994 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5995 break;
5996 }
5997
5998 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5999 symbols are resolved by the loader. Add them to .rela.dyn. */
6000 if (h != NULL && is_gott_symbol (info, &h->root))
6001 {
6002 Elf_Internal_Rela outrel;
6003 bfd_byte *loc;
6004 asection *s;
6005
6006 s = mips_elf_rel_dyn_section (info, false);
6007 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6008
6009 outrel.r_offset = (input_section->output_section->vma
6010 + input_section->output_offset
6011 + relocation->r_offset);
6012 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6013 outrel.r_addend = addend;
6014 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
6015
6016 /* If we've written this relocation for a readonly section,
6017 we need to set DF_TEXTREL again, so that we do not delete the
6018 DT_TEXTREL tag. */
6019 if (MIPS_ELF_READONLY_SECTION (input_section))
6020 info->flags |= DF_TEXTREL;
6021
6022 *valuep = 0;
6023 return bfd_reloc_ok;
6024 }
6025
6026 /* Figure out what kind of relocation is being performed. */
6027 switch (r_type)
6028 {
6029 case R_MIPS_NONE:
6030 return bfd_reloc_continue;
6031
6032 case R_MIPS_16:
6033 if (howto->partial_inplace)
6034 addend = _bfd_mips_elf_sign_extend (addend, 16);
6035 value = symbol + addend;
6036 overflowed_p = mips_elf_overflow_p (value, 16);
6037 break;
6038
6039 case R_MIPS_32:
6040 case R_MIPS_REL32:
6041 case R_MIPS_64:
6042 if ((bfd_link_pic (info)
6043 || (htab->root.dynamic_sections_created
6044 && h != NULL
6045 && h->root.def_dynamic
6046 && !h->root.def_regular
6047 && !h->has_static_relocs))
6048 && r_symndx != STN_UNDEF
6049 && (h == NULL
6050 || h->root.root.type != bfd_link_hash_undefweak
6051 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6052 && !resolved_to_zero))
6053 && (input_section->flags & SEC_ALLOC) != 0)
6054 {
6055 /* If we're creating a shared library, then we can't know
6056 where the symbol will end up. So, we create a relocation
6057 record in the output, and leave the job up to the dynamic
6058 linker. We must do the same for executable references to
6059 shared library symbols, unless we've decided to use copy
6060 relocs or PLTs instead. */
6061 value = addend;
6062 if (!mips_elf_create_dynamic_relocation (abfd,
6063 info,
6064 relocation,
6065 h,
6066 sec,
6067 symbol,
6068 &value,
6069 input_section))
6070 return bfd_reloc_undefined;
6071 }
6072 else
6073 {
6074 if (r_type != R_MIPS_REL32)
6075 value = symbol + addend;
6076 else
6077 value = addend;
6078 }
6079 value &= howto->dst_mask;
6080 break;
6081
6082 case R_MIPS_PC32:
6083 value = symbol + addend - p;
6084 value &= howto->dst_mask;
6085 break;
6086
6087 case R_MIPS16_26:
6088 /* The calculation for R_MIPS16_26 is just the same as for an
6089 R_MIPS_26. It's only the storage of the relocated field into
6090 the output file that's different. That's handled in
6091 mips_elf_perform_relocation. So, we just fall through to the
6092 R_MIPS_26 case here. */
6093 case R_MIPS_26:
6094 case R_MICROMIPS_26_S1:
6095 {
6096 unsigned int shift;
6097
6098 /* Shift is 2, unusually, for microMIPS JALX. */
6099 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6100
6101 if (howto->partial_inplace && !section_p)
6102 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6103 else
6104 value = addend;
6105 value += symbol;
6106
6107 /* Make sure the target of a jump is suitably aligned. Bit 0 must
6108 be the correct ISA mode selector except for weak undefined
6109 symbols. */
6110 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6111 && (*cross_mode_jump_p
6112 ? (value & 3) != (r_type == R_MIPS_26)
6113 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6114 return bfd_reloc_outofrange;
6115
6116 value >>= shift;
6117 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6118 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6119 value &= howto->dst_mask;
6120 }
6121 break;
6122
6123 case R_MIPS_TLS_DTPREL_HI16:
6124 case R_MIPS16_TLS_DTPREL_HI16:
6125 case R_MICROMIPS_TLS_DTPREL_HI16:
6126 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6127 & howto->dst_mask);
6128 break;
6129
6130 case R_MIPS_TLS_DTPREL_LO16:
6131 case R_MIPS_TLS_DTPREL32:
6132 case R_MIPS_TLS_DTPREL64:
6133 case R_MIPS16_TLS_DTPREL_LO16:
6134 case R_MICROMIPS_TLS_DTPREL_LO16:
6135 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6136 break;
6137
6138 case R_MIPS_TLS_TPREL_HI16:
6139 case R_MIPS16_TLS_TPREL_HI16:
6140 case R_MICROMIPS_TLS_TPREL_HI16:
6141 value = (mips_elf_high (addend + symbol - tprel_base (info))
6142 & howto->dst_mask);
6143 break;
6144
6145 case R_MIPS_TLS_TPREL_LO16:
6146 case R_MIPS_TLS_TPREL32:
6147 case R_MIPS_TLS_TPREL64:
6148 case R_MIPS16_TLS_TPREL_LO16:
6149 case R_MICROMIPS_TLS_TPREL_LO16:
6150 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6151 break;
6152
6153 case R_MIPS_HI16:
6154 case R_MIPS16_HI16:
6155 case R_MICROMIPS_HI16:
6156 if (!gp_disp_p)
6157 {
6158 value = mips_elf_high (addend + symbol);
6159 value &= howto->dst_mask;
6160 }
6161 else
6162 {
6163 /* For MIPS16 ABI code we generate this sequence
6164 0: li $v0,%hi(_gp_disp)
6165 4: addiupc $v1,%lo(_gp_disp)
6166 8: sll $v0,16
6167 12: addu $v0,$v1
6168 14: move $gp,$v0
6169 So the offsets of hi and lo relocs are the same, but the
6170 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6171 ADDIUPC clears the low two bits of the instruction address,
6172 so the base is ($t9 + 4) & ~3. */
6173 if (r_type == R_MIPS16_HI16)
6174 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6175 /* The microMIPS .cpload sequence uses the same assembly
6176 instructions as the traditional psABI version, but the
6177 incoming $t9 has the low bit set. */
6178 else if (r_type == R_MICROMIPS_HI16)
6179 value = mips_elf_high (addend + gp - p - 1);
6180 else
6181 value = mips_elf_high (addend + gp - p);
6182 }
6183 break;
6184
6185 case R_MIPS_LO16:
6186 case R_MIPS16_LO16:
6187 case R_MICROMIPS_LO16:
6188 case R_MICROMIPS_HI0_LO16:
6189 if (!gp_disp_p)
6190 value = (symbol + addend) & howto->dst_mask;
6191 else
6192 {
6193 /* See the comment for R_MIPS16_HI16 above for the reason
6194 for this conditional. */
6195 if (r_type == R_MIPS16_LO16)
6196 value = addend + gp - (p & ~(bfd_vma) 0x3);
6197 else if (r_type == R_MICROMIPS_LO16
6198 || r_type == R_MICROMIPS_HI0_LO16)
6199 value = addend + gp - p + 3;
6200 else
6201 value = addend + gp - p + 4;
6202 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6203 for overflow. But, on, say, IRIX5, relocations against
6204 _gp_disp are normally generated from the .cpload
6205 pseudo-op. It generates code that normally looks like
6206 this:
6207
6208 lui $gp,%hi(_gp_disp)
6209 addiu $gp,$gp,%lo(_gp_disp)
6210 addu $gp,$gp,$t9
6211
6212 Here $t9 holds the address of the function being called,
6213 as required by the MIPS ELF ABI. The R_MIPS_LO16
6214 relocation can easily overflow in this situation, but the
6215 R_MIPS_HI16 relocation will handle the overflow.
6216 Therefore, we consider this a bug in the MIPS ABI, and do
6217 not check for overflow here. */
6218 }
6219 break;
6220
6221 case R_MIPS_LITERAL:
6222 case R_MICROMIPS_LITERAL:
6223 /* Because we don't merge literal sections, we can handle this
6224 just like R_MIPS_GPREL16. In the long run, we should merge
6225 shared literals, and then we will need to additional work
6226 here. */
6227
6228 /* Fall through. */
6229
6230 case R_MIPS16_GPREL:
6231 /* The R_MIPS16_GPREL performs the same calculation as
6232 R_MIPS_GPREL16, but stores the relocated bits in a different
6233 order. We don't need to do anything special here; the
6234 differences are handled in mips_elf_perform_relocation. */
6235 case R_MIPS_GPREL16:
6236 case R_MICROMIPS_GPREL7_S2:
6237 case R_MICROMIPS_GPREL16:
6238 /* Only sign-extend the addend if it was extracted from the
6239 instruction. If the addend was separate, leave it alone,
6240 otherwise we may lose significant bits. */
6241 if (howto->partial_inplace)
6242 addend = _bfd_mips_elf_sign_extend (addend, 16);
6243 value = symbol + addend - gp;
6244 /* If the symbol was local, any earlier relocatable links will
6245 have adjusted its addend with the gp offset, so compensate
6246 for that now. Don't do it for symbols forced local in this
6247 link, though, since they won't have had the gp offset applied
6248 to them before. */
6249 if (was_local_p)
6250 value += gp0;
6251 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6252 overflowed_p = mips_elf_overflow_p (value, 16);
6253 break;
6254
6255 case R_MIPS16_GOT16:
6256 case R_MIPS16_CALL16:
6257 case R_MIPS_GOT16:
6258 case R_MIPS_CALL16:
6259 case R_MICROMIPS_GOT16:
6260 case R_MICROMIPS_CALL16:
6261 /* VxWorks does not have separate local and global semantics for
6262 R_MIPS*_GOT16; every relocation evaluates to "G". */
6263 if (htab->root.target_os != is_vxworks && local_p)
6264 {
6265 value = mips_elf_got16_entry (abfd, input_bfd, info,
6266 symbol + addend, !was_local_p);
6267 if (value == MINUS_ONE)
6268 return bfd_reloc_outofrange;
6269 value
6270 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6271 overflowed_p = mips_elf_overflow_p (value, 16);
6272 break;
6273 }
6274
6275 /* Fall through. */
6276
6277 case R_MIPS_TLS_GD:
6278 case R_MIPS_TLS_GOTTPREL:
6279 case R_MIPS_TLS_LDM:
6280 case R_MIPS_GOT_DISP:
6281 case R_MIPS16_TLS_GD:
6282 case R_MIPS16_TLS_GOTTPREL:
6283 case R_MIPS16_TLS_LDM:
6284 case R_MICROMIPS_TLS_GD:
6285 case R_MICROMIPS_TLS_GOTTPREL:
6286 case R_MICROMIPS_TLS_LDM:
6287 case R_MICROMIPS_GOT_DISP:
6288 value = g;
6289 overflowed_p = mips_elf_overflow_p (value, 16);
6290 break;
6291
6292 case R_MIPS_GPREL32:
6293 value = (addend + symbol + gp0 - gp);
6294 if (!save_addend)
6295 value &= howto->dst_mask;
6296 break;
6297
6298 case R_MIPS_PC16:
6299 case R_MIPS_GNU_REL16_S2:
6300 if (howto->partial_inplace)
6301 addend = _bfd_mips_elf_sign_extend (addend, 18);
6302
6303 /* No need to exclude weak undefined symbols here as they resolve
6304 to 0 and never set `*cross_mode_jump_p', so this alignment check
6305 will never trigger for them. */
6306 if (*cross_mode_jump_p
6307 ? ((symbol + addend) & 3) != 1
6308 : ((symbol + addend) & 3) != 0)
6309 return bfd_reloc_outofrange;
6310
6311 value = symbol + addend - p;
6312 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6313 overflowed_p = mips_elf_overflow_p (value, 18);
6314 value >>= howto->rightshift;
6315 value &= howto->dst_mask;
6316 break;
6317
6318 case R_MIPS16_PC16_S1:
6319 if (howto->partial_inplace)
6320 addend = _bfd_mips_elf_sign_extend (addend, 17);
6321
6322 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6323 && (*cross_mode_jump_p
6324 ? ((symbol + addend) & 3) != 0
6325 : ((symbol + addend) & 1) == 0))
6326 return bfd_reloc_outofrange;
6327
6328 value = symbol + addend - p;
6329 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6330 overflowed_p = mips_elf_overflow_p (value, 17);
6331 value >>= howto->rightshift;
6332 value &= howto->dst_mask;
6333 break;
6334
6335 case R_MIPS_PC21_S2:
6336 if (howto->partial_inplace)
6337 addend = _bfd_mips_elf_sign_extend (addend, 23);
6338
6339 if ((symbol + addend) & 3)
6340 return bfd_reloc_outofrange;
6341
6342 value = symbol + addend - p;
6343 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6344 overflowed_p = mips_elf_overflow_p (value, 23);
6345 value >>= howto->rightshift;
6346 value &= howto->dst_mask;
6347 break;
6348
6349 case R_MIPS_PC26_S2:
6350 if (howto->partial_inplace)
6351 addend = _bfd_mips_elf_sign_extend (addend, 28);
6352
6353 if ((symbol + addend) & 3)
6354 return bfd_reloc_outofrange;
6355
6356 value = symbol + addend - p;
6357 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6358 overflowed_p = mips_elf_overflow_p (value, 28);
6359 value >>= howto->rightshift;
6360 value &= howto->dst_mask;
6361 break;
6362
6363 case R_MIPS_PC18_S3:
6364 if (howto->partial_inplace)
6365 addend = _bfd_mips_elf_sign_extend (addend, 21);
6366
6367 if ((symbol + addend) & 7)
6368 return bfd_reloc_outofrange;
6369
6370 value = symbol + addend - ((p | 7) ^ 7);
6371 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6372 overflowed_p = mips_elf_overflow_p (value, 21);
6373 value >>= howto->rightshift;
6374 value &= howto->dst_mask;
6375 break;
6376
6377 case R_MIPS_PC19_S2:
6378 if (howto->partial_inplace)
6379 addend = _bfd_mips_elf_sign_extend (addend, 21);
6380
6381 if ((symbol + addend) & 3)
6382 return bfd_reloc_outofrange;
6383
6384 value = symbol + addend - p;
6385 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6386 overflowed_p = mips_elf_overflow_p (value, 21);
6387 value >>= howto->rightshift;
6388 value &= howto->dst_mask;
6389 break;
6390
6391 case R_MIPS_PCHI16:
6392 value = mips_elf_high (symbol + addend - p);
6393 value &= howto->dst_mask;
6394 break;
6395
6396 case R_MIPS_PCLO16:
6397 if (howto->partial_inplace)
6398 addend = _bfd_mips_elf_sign_extend (addend, 16);
6399 value = symbol + addend - p;
6400 value &= howto->dst_mask;
6401 break;
6402
6403 case R_MICROMIPS_PC7_S1:
6404 if (howto->partial_inplace)
6405 addend = _bfd_mips_elf_sign_extend (addend, 8);
6406
6407 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6408 && (*cross_mode_jump_p
6409 ? ((symbol + addend + 2) & 3) != 0
6410 : ((symbol + addend + 2) & 1) == 0))
6411 return bfd_reloc_outofrange;
6412
6413 value = symbol + addend - p;
6414 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6415 overflowed_p = mips_elf_overflow_p (value, 8);
6416 value >>= howto->rightshift;
6417 value &= howto->dst_mask;
6418 break;
6419
6420 case R_MICROMIPS_PC10_S1:
6421 if (howto->partial_inplace)
6422 addend = _bfd_mips_elf_sign_extend (addend, 11);
6423
6424 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6425 && (*cross_mode_jump_p
6426 ? ((symbol + addend + 2) & 3) != 0
6427 : ((symbol + addend + 2) & 1) == 0))
6428 return bfd_reloc_outofrange;
6429
6430 value = symbol + addend - p;
6431 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6432 overflowed_p = mips_elf_overflow_p (value, 11);
6433 value >>= howto->rightshift;
6434 value &= howto->dst_mask;
6435 break;
6436
6437 case R_MICROMIPS_PC16_S1:
6438 if (howto->partial_inplace)
6439 addend = _bfd_mips_elf_sign_extend (addend, 17);
6440
6441 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6442 && (*cross_mode_jump_p
6443 ? ((symbol + addend) & 3) != 0
6444 : ((symbol + addend) & 1) == 0))
6445 return bfd_reloc_outofrange;
6446
6447 value = symbol + addend - p;
6448 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6449 overflowed_p = mips_elf_overflow_p (value, 17);
6450 value >>= howto->rightshift;
6451 value &= howto->dst_mask;
6452 break;
6453
6454 case R_MICROMIPS_PC23_S2:
6455 if (howto->partial_inplace)
6456 addend = _bfd_mips_elf_sign_extend (addend, 25);
6457 value = symbol + addend - ((p | 3) ^ 3);
6458 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6459 overflowed_p = mips_elf_overflow_p (value, 25);
6460 value >>= howto->rightshift;
6461 value &= howto->dst_mask;
6462 break;
6463
6464 case R_MIPS_GOT_HI16:
6465 case R_MIPS_CALL_HI16:
6466 case R_MICROMIPS_GOT_HI16:
6467 case R_MICROMIPS_CALL_HI16:
6468 /* We're allowed to handle these two relocations identically.
6469 The dynamic linker is allowed to handle the CALL relocations
6470 differently by creating a lazy evaluation stub. */
6471 value = g;
6472 value = mips_elf_high (value);
6473 value &= howto->dst_mask;
6474 break;
6475
6476 case R_MIPS_GOT_LO16:
6477 case R_MIPS_CALL_LO16:
6478 case R_MICROMIPS_GOT_LO16:
6479 case R_MICROMIPS_CALL_LO16:
6480 value = g & howto->dst_mask;
6481 break;
6482
6483 case R_MIPS_GOT_PAGE:
6484 case R_MICROMIPS_GOT_PAGE:
6485 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6486 if (value == MINUS_ONE)
6487 return bfd_reloc_outofrange;
6488 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6489 overflowed_p = mips_elf_overflow_p (value, 16);
6490 break;
6491
6492 case R_MIPS_GOT_OFST:
6493 case R_MICROMIPS_GOT_OFST:
6494 if (local_p)
6495 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6496 else
6497 value = addend;
6498 overflowed_p = mips_elf_overflow_p (value, 16);
6499 break;
6500
6501 case R_MIPS_SUB:
6502 case R_MICROMIPS_SUB:
6503 value = symbol - addend;
6504 value &= howto->dst_mask;
6505 break;
6506
6507 case R_MIPS_HIGHER:
6508 case R_MICROMIPS_HIGHER:
6509 value = mips_elf_higher (addend + symbol);
6510 value &= howto->dst_mask;
6511 break;
6512
6513 case R_MIPS_HIGHEST:
6514 case R_MICROMIPS_HIGHEST:
6515 value = mips_elf_highest (addend + symbol);
6516 value &= howto->dst_mask;
6517 break;
6518
6519 case R_MIPS_SCN_DISP:
6520 case R_MICROMIPS_SCN_DISP:
6521 value = symbol + addend - sec->output_offset;
6522 value &= howto->dst_mask;
6523 break;
6524
6525 case R_MIPS_JALR:
6526 case R_MICROMIPS_JALR:
6527 /* This relocation is only a hint. In some cases, we optimize
6528 it into a bal instruction. But we don't try to optimize
6529 when the symbol does not resolve locally. */
6530 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6531 return bfd_reloc_continue;
6532 /* We can't optimize cross-mode jumps either. */
6533 if (*cross_mode_jump_p)
6534 return bfd_reloc_continue;
6535 value = symbol + addend;
6536 /* Neither we can non-instruction-aligned targets. */
6537 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6538 return bfd_reloc_continue;
6539 break;
6540
6541 case R_MIPS_PJUMP:
6542 case R_MIPS_GNU_VTINHERIT:
6543 case R_MIPS_GNU_VTENTRY:
6544 /* We don't do anything with these at present. */
6545 return bfd_reloc_continue;
6546
6547 default:
6548 /* An unrecognized relocation type. */
6549 return bfd_reloc_notsupported;
6550 }
6551
6552 /* Store the VALUE for our caller. */
6553 *valuep = value;
6554 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6555 }
6556
6557 /* It has been determined that the result of the RELOCATION is the
6558 VALUE. Use HOWTO to place VALUE into the output file at the
6559 appropriate position. The SECTION is the section to which the
6560 relocation applies.
6561 CROSS_MODE_JUMP_P is true if the relocation field
6562 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6563
6564 Returns FALSE if anything goes wrong. */
6565
6566 static bool
6567 mips_elf_perform_relocation (struct bfd_link_info *info,
6568 reloc_howto_type *howto,
6569 const Elf_Internal_Rela *relocation,
6570 bfd_vma value, bfd *input_bfd,
6571 asection *input_section, bfd_byte *contents,
6572 bool cross_mode_jump_p)
6573 {
6574 bfd_vma x;
6575 bfd_byte *location;
6576 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6577
6578 /* Figure out where the relocation is occurring. */
6579 location = contents + relocation->r_offset;
6580
6581 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
6582
6583 /* Obtain the current value. */
6584 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6585
6586 /* Clear the field we are setting. */
6587 x &= ~howto->dst_mask;
6588
6589 /* Set the field. */
6590 x |= (value & howto->dst_mask);
6591
6592 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
6593 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6594 {
6595 bfd_vma opcode = x >> 26;
6596
6597 if (r_type == R_MIPS16_26 ? opcode == 0x7
6598 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6599 : opcode == 0x1d)
6600 {
6601 info->callbacks->einfo
6602 (_("%X%H: unsupported JALX to the same ISA mode\n"),
6603 input_bfd, input_section, relocation->r_offset);
6604 return true;
6605 }
6606 }
6607 if (cross_mode_jump_p && jal_reloc_p (r_type))
6608 {
6609 bool ok;
6610 bfd_vma opcode = x >> 26;
6611 bfd_vma jalx_opcode;
6612
6613 /* Check to see if the opcode is already JAL or JALX. */
6614 if (r_type == R_MIPS16_26)
6615 {
6616 ok = ((opcode == 0x6) || (opcode == 0x7));
6617 jalx_opcode = 0x7;
6618 }
6619 else if (r_type == R_MICROMIPS_26_S1)
6620 {
6621 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6622 jalx_opcode = 0x3c;
6623 }
6624 else
6625 {
6626 ok = ((opcode == 0x3) || (opcode == 0x1d));
6627 jalx_opcode = 0x1d;
6628 }
6629
6630 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6631 convert J or JALS to JALX. */
6632 if (!ok)
6633 {
6634 info->callbacks->einfo
6635 (_("%X%H: unsupported jump between ISA modes; "
6636 "consider recompiling with interlinking enabled\n"),
6637 input_bfd, input_section, relocation->r_offset);
6638 return true;
6639 }
6640
6641 /* Make this the JALX opcode. */
6642 x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6643 }
6644 else if (cross_mode_jump_p && b_reloc_p (r_type))
6645 {
6646 bool ok = false;
6647 bfd_vma opcode = x >> 16;
6648 bfd_vma jalx_opcode = 0;
6649 bfd_vma sign_bit = 0;
6650 bfd_vma addr;
6651 bfd_vma dest;
6652
6653 if (r_type == R_MICROMIPS_PC16_S1)
6654 {
6655 ok = opcode == 0x4060;
6656 jalx_opcode = 0x3c;
6657 sign_bit = 0x10000;
6658 value <<= 1;
6659 }
6660 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6661 {
6662 ok = opcode == 0x411;
6663 jalx_opcode = 0x1d;
6664 sign_bit = 0x20000;
6665 value <<= 2;
6666 }
6667
6668 if (ok && !bfd_link_pic (info))
6669 {
6670 addr = (input_section->output_section->vma
6671 + input_section->output_offset
6672 + relocation->r_offset
6673 + 4);
6674 dest = (addr
6675 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6676
6677 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6678 {
6679 info->callbacks->einfo
6680 (_("%X%H: cannot convert branch between ISA modes "
6681 "to JALX: relocation out of range\n"),
6682 input_bfd, input_section, relocation->r_offset);
6683 return true;
6684 }
6685
6686 /* Make this the JALX opcode. */
6687 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6688 }
6689 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6690 {
6691 info->callbacks->einfo
6692 (_("%X%H: unsupported branch between ISA modes\n"),
6693 input_bfd, input_section, relocation->r_offset);
6694 return true;
6695 }
6696 }
6697
6698 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6699 range. */
6700 if (!bfd_link_relocatable (info)
6701 && !cross_mode_jump_p
6702 && ((JAL_TO_BAL_P (input_bfd)
6703 && r_type == R_MIPS_26
6704 && (x >> 26) == 0x3) /* jal addr */
6705 || (JALR_TO_BAL_P (input_bfd)
6706 && r_type == R_MIPS_JALR
6707 && x == 0x0320f809) /* jalr t9 */
6708 || (JR_TO_B_P (input_bfd)
6709 && r_type == R_MIPS_JALR
6710 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6711 {
6712 bfd_vma addr;
6713 bfd_vma dest;
6714 bfd_signed_vma off;
6715
6716 addr = (input_section->output_section->vma
6717 + input_section->output_offset
6718 + relocation->r_offset
6719 + 4);
6720 if (r_type == R_MIPS_26)
6721 dest = (value << 2) | ((addr >> 28) << 28);
6722 else
6723 dest = value;
6724 off = dest - addr;
6725 if (off <= 0x1ffff && off >= -0x20000)
6726 {
6727 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
6728 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6729 else
6730 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6731 }
6732 }
6733
6734 /* Put the value into the output. */
6735 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6736
6737 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6738 location);
6739
6740 return true;
6741 }
6742 \f
6743 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6744 is the original relocation, which is now being transformed into a
6745 dynamic relocation. The ADDENDP is adjusted if necessary; the
6746 caller should store the result in place of the original addend. */
6747
6748 static bool
6749 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6750 struct bfd_link_info *info,
6751 const Elf_Internal_Rela *rel,
6752 struct mips_elf_link_hash_entry *h,
6753 asection *sec, bfd_vma symbol,
6754 bfd_vma *addendp, asection *input_section)
6755 {
6756 Elf_Internal_Rela outrel[3];
6757 asection *sreloc;
6758 bfd *dynobj;
6759 int r_type;
6760 long indx;
6761 bool defined_p;
6762 struct mips_elf_link_hash_table *htab;
6763
6764 htab = mips_elf_hash_table (info);
6765 BFD_ASSERT (htab != NULL);
6766
6767 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6768 dynobj = elf_hash_table (info)->dynobj;
6769 sreloc = mips_elf_rel_dyn_section (info, false);
6770 BFD_ASSERT (sreloc != NULL);
6771 BFD_ASSERT (sreloc->contents != NULL);
6772 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6773 < sreloc->size);
6774
6775 outrel[0].r_offset =
6776 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6777 if (ABI_64_P (output_bfd))
6778 {
6779 outrel[1].r_offset =
6780 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6781 outrel[2].r_offset =
6782 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6783 }
6784
6785 if (outrel[0].r_offset == MINUS_ONE)
6786 /* The relocation field has been deleted. */
6787 return true;
6788
6789 if (outrel[0].r_offset == MINUS_TWO)
6790 {
6791 /* The relocation field has been converted into a relative value of
6792 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6793 the field to be fully relocated, so add in the symbol's value. */
6794 *addendp += symbol;
6795 return true;
6796 }
6797
6798 /* We must now calculate the dynamic symbol table index to use
6799 in the relocation. */
6800 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6801 {
6802 BFD_ASSERT (htab->root.target_os == is_vxworks
6803 || h->global_got_area != GGA_NONE);
6804 indx = h->root.dynindx;
6805 if (SGI_COMPAT (output_bfd))
6806 defined_p = h->root.def_regular;
6807 else
6808 /* ??? glibc's ld.so just adds the final GOT entry to the
6809 relocation field. It therefore treats relocs against
6810 defined symbols in the same way as relocs against
6811 undefined symbols. */
6812 defined_p = false;
6813 }
6814 else
6815 {
6816 if (sec != NULL && bfd_is_abs_section (sec))
6817 indx = 0;
6818 else if (sec == NULL || sec->owner == NULL)
6819 {
6820 bfd_set_error (bfd_error_bad_value);
6821 return false;
6822 }
6823 else
6824 {
6825 indx = elf_section_data (sec->output_section)->dynindx;
6826 if (indx == 0)
6827 {
6828 asection *osec = htab->root.text_index_section;
6829 indx = elf_section_data (osec)->dynindx;
6830 }
6831 if (indx == 0)
6832 abort ();
6833 }
6834
6835 /* Instead of generating a relocation using the section
6836 symbol, we may as well make it a fully relative
6837 relocation. We want to avoid generating relocations to
6838 local symbols because we used to generate them
6839 incorrectly, without adding the original symbol value,
6840 which is mandated by the ABI for section symbols. In
6841 order to give dynamic loaders and applications time to
6842 phase out the incorrect use, we refrain from emitting
6843 section-relative relocations. It's not like they're
6844 useful, after all. This should be a bit more efficient
6845 as well. */
6846 /* ??? Although this behavior is compatible with glibc's ld.so,
6847 the ABI says that relocations against STN_UNDEF should have
6848 a symbol value of 0. Irix rld honors this, so relocations
6849 against STN_UNDEF have no effect. */
6850 if (!SGI_COMPAT (output_bfd))
6851 indx = 0;
6852 defined_p = true;
6853 }
6854
6855 /* If the relocation was previously an absolute relocation and
6856 this symbol will not be referred to by the relocation, we must
6857 adjust it by the value we give it in the dynamic symbol table.
6858 Otherwise leave the job up to the dynamic linker. */
6859 if (defined_p && r_type != R_MIPS_REL32)
6860 *addendp += symbol;
6861
6862 if (htab->root.target_os == is_vxworks)
6863 /* VxWorks uses non-relative relocations for this. */
6864 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6865 else
6866 /* The relocation is always an REL32 relocation because we don't
6867 know where the shared library will wind up at load-time. */
6868 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6869 R_MIPS_REL32);
6870
6871 /* For strict adherence to the ABI specification, we should
6872 generate a R_MIPS_64 relocation record by itself before the
6873 _REL32/_64 record as well, such that the addend is read in as
6874 a 64-bit value (REL32 is a 32-bit relocation, after all).
6875 However, since none of the existing ELF64 MIPS dynamic
6876 loaders seems to care, we don't waste space with these
6877 artificial relocations. If this turns out to not be true,
6878 mips_elf_allocate_dynamic_relocation() should be tweaked so
6879 as to make room for a pair of dynamic relocations per
6880 invocation if ABI_64_P, and here we should generate an
6881 additional relocation record with R_MIPS_64 by itself for a
6882 NULL symbol before this relocation record. */
6883 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6884 ABI_64_P (output_bfd)
6885 ? R_MIPS_64
6886 : R_MIPS_NONE);
6887 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6888
6889 /* Adjust the output offset of the relocation to reference the
6890 correct location in the output file. */
6891 outrel[0].r_offset += (input_section->output_section->vma
6892 + input_section->output_offset);
6893 outrel[1].r_offset += (input_section->output_section->vma
6894 + input_section->output_offset);
6895 outrel[2].r_offset += (input_section->output_section->vma
6896 + input_section->output_offset);
6897
6898 /* Put the relocation back out. We have to use the special
6899 relocation outputter in the 64-bit case since the 64-bit
6900 relocation format is non-standard. */
6901 if (ABI_64_P (output_bfd))
6902 {
6903 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6904 (output_bfd, &outrel[0],
6905 (sreloc->contents
6906 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6907 }
6908 else if (htab->root.target_os == is_vxworks)
6909 {
6910 /* VxWorks uses RELA rather than REL dynamic relocations. */
6911 outrel[0].r_addend = *addendp;
6912 bfd_elf32_swap_reloca_out
6913 (output_bfd, &outrel[0],
6914 (sreloc->contents
6915 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6916 }
6917 else
6918 bfd_elf32_swap_reloc_out
6919 (output_bfd, &outrel[0],
6920 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6921
6922 /* We've now added another relocation. */
6923 ++sreloc->reloc_count;
6924
6925 /* Make sure the output section is writable. The dynamic linker
6926 will be writing to it. */
6927 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6928 |= SHF_WRITE;
6929
6930 /* On IRIX5, make an entry of compact relocation info. */
6931 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6932 {
6933 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6934 bfd_byte *cr;
6935
6936 if (scpt)
6937 {
6938 Elf32_crinfo cptrel;
6939
6940 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6941 cptrel.vaddr = (rel->r_offset
6942 + input_section->output_section->vma
6943 + input_section->output_offset);
6944 if (r_type == R_MIPS_REL32)
6945 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6946 else
6947 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6948 mips_elf_set_cr_dist2to (cptrel, 0);
6949 cptrel.konst = *addendp;
6950
6951 cr = (scpt->contents
6952 + sizeof (Elf32_External_compact_rel));
6953 mips_elf_set_cr_relvaddr (cptrel, 0);
6954 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6955 ((Elf32_External_crinfo *) cr
6956 + scpt->reloc_count));
6957 ++scpt->reloc_count;
6958 }
6959 }
6960
6961 /* If we've written this relocation for a readonly section,
6962 we need to set DF_TEXTREL again, so that we do not delete the
6963 DT_TEXTREL tag. */
6964 if (MIPS_ELF_READONLY_SECTION (input_section))
6965 info->flags |= DF_TEXTREL;
6966
6967 return true;
6968 }
6969 \f
6970 /* Return the MACH for a MIPS e_flags value. */
6971
6972 unsigned long
6973 _bfd_elf_mips_mach (flagword flags)
6974 {
6975 switch (flags & EF_MIPS_MACH)
6976 {
6977 case E_MIPS_MACH_3900:
6978 return bfd_mach_mips3900;
6979
6980 case E_MIPS_MACH_4010:
6981 return bfd_mach_mips4010;
6982
6983 case E_MIPS_MACH_4100:
6984 return bfd_mach_mips4100;
6985
6986 case E_MIPS_MACH_4111:
6987 return bfd_mach_mips4111;
6988
6989 case E_MIPS_MACH_4120:
6990 return bfd_mach_mips4120;
6991
6992 case E_MIPS_MACH_4650:
6993 return bfd_mach_mips4650;
6994
6995 case E_MIPS_MACH_5400:
6996 return bfd_mach_mips5400;
6997
6998 case E_MIPS_MACH_5500:
6999 return bfd_mach_mips5500;
7000
7001 case E_MIPS_MACH_5900:
7002 return bfd_mach_mips5900;
7003
7004 case E_MIPS_MACH_9000:
7005 return bfd_mach_mips9000;
7006
7007 case E_MIPS_MACH_SB1:
7008 return bfd_mach_mips_sb1;
7009
7010 case E_MIPS_MACH_LS2E:
7011 return bfd_mach_mips_loongson_2e;
7012
7013 case E_MIPS_MACH_LS2F:
7014 return bfd_mach_mips_loongson_2f;
7015
7016 case E_MIPS_MACH_GS464:
7017 return bfd_mach_mips_gs464;
7018
7019 case E_MIPS_MACH_GS464E:
7020 return bfd_mach_mips_gs464e;
7021
7022 case E_MIPS_MACH_GS264E:
7023 return bfd_mach_mips_gs264e;
7024
7025 case E_MIPS_MACH_OCTEON3:
7026 return bfd_mach_mips_octeon3;
7027
7028 case E_MIPS_MACH_OCTEON2:
7029 return bfd_mach_mips_octeon2;
7030
7031 case E_MIPS_MACH_OCTEON:
7032 return bfd_mach_mips_octeon;
7033
7034 case E_MIPS_MACH_XLR:
7035 return bfd_mach_mips_xlr;
7036
7037 case E_MIPS_MACH_IAMR2:
7038 return bfd_mach_mips_interaptiv_mr2;
7039
7040 default:
7041 switch (flags & EF_MIPS_ARCH)
7042 {
7043 default:
7044 case E_MIPS_ARCH_1:
7045 return bfd_mach_mips3000;
7046
7047 case E_MIPS_ARCH_2:
7048 return bfd_mach_mips6000;
7049
7050 case E_MIPS_ARCH_3:
7051 return bfd_mach_mips4000;
7052
7053 case E_MIPS_ARCH_4:
7054 return bfd_mach_mips8000;
7055
7056 case E_MIPS_ARCH_5:
7057 return bfd_mach_mips5;
7058
7059 case E_MIPS_ARCH_32:
7060 return bfd_mach_mipsisa32;
7061
7062 case E_MIPS_ARCH_64:
7063 return bfd_mach_mipsisa64;
7064
7065 case E_MIPS_ARCH_32R2:
7066 return bfd_mach_mipsisa32r2;
7067
7068 case E_MIPS_ARCH_64R2:
7069 return bfd_mach_mipsisa64r2;
7070
7071 case E_MIPS_ARCH_32R6:
7072 return bfd_mach_mipsisa32r6;
7073
7074 case E_MIPS_ARCH_64R6:
7075 return bfd_mach_mipsisa64r6;
7076 }
7077 }
7078
7079 return 0;
7080 }
7081
7082 /* Return printable name for ABI. */
7083
7084 static inline char *
7085 elf_mips_abi_name (bfd *abfd)
7086 {
7087 flagword flags;
7088
7089 flags = elf_elfheader (abfd)->e_flags;
7090 switch (flags & EF_MIPS_ABI)
7091 {
7092 case 0:
7093 if (ABI_N32_P (abfd))
7094 return "N32";
7095 else if (ABI_64_P (abfd))
7096 return "64";
7097 else
7098 return "none";
7099 case E_MIPS_ABI_O32:
7100 return "O32";
7101 case E_MIPS_ABI_O64:
7102 return "O64";
7103 case E_MIPS_ABI_EABI32:
7104 return "EABI32";
7105 case E_MIPS_ABI_EABI64:
7106 return "EABI64";
7107 default:
7108 return "unknown abi";
7109 }
7110 }
7111 \f
7112 /* MIPS ELF uses two common sections. One is the usual one, and the
7113 other is for small objects. All the small objects are kept
7114 together, and then referenced via the gp pointer, which yields
7115 faster assembler code. This is what we use for the small common
7116 section. This approach is copied from ecoff.c. */
7117 static asection mips_elf_scom_section;
7118 static const asymbol mips_elf_scom_symbol =
7119 GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7120 static asection mips_elf_scom_section =
7121 BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7122 ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
7123
7124 /* MIPS ELF also uses an acommon section, which represents an
7125 allocated common symbol which may be overridden by a
7126 definition in a shared library. */
7127 static asection mips_elf_acom_section;
7128 static const asymbol mips_elf_acom_symbol =
7129 GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7130 static asection mips_elf_acom_section =
7131 BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7132 ".acommon", 0, SEC_ALLOC);
7133
7134 /* This is used for both the 32-bit and the 64-bit ABI. */
7135
7136 void
7137 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7138 {
7139 elf_symbol_type *elfsym;
7140
7141 /* Handle the special MIPS section numbers that a symbol may use. */
7142 elfsym = (elf_symbol_type *) asym;
7143 switch (elfsym->internal_elf_sym.st_shndx)
7144 {
7145 case SHN_MIPS_ACOMMON:
7146 /* This section is used in a dynamically linked executable file.
7147 It is an allocated common section. The dynamic linker can
7148 either resolve these symbols to something in a shared
7149 library, or it can just leave them here. For our purposes,
7150 we can consider these symbols to be in a new section. */
7151 asym->section = &mips_elf_acom_section;
7152 break;
7153
7154 case SHN_COMMON:
7155 /* Common symbols less than the GP size are automatically
7156 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
7157 if (asym->value > elf_gp_size (abfd)
7158 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7159 || IRIX_COMPAT (abfd) == ict_irix6)
7160 break;
7161 /* Fall through. */
7162 case SHN_MIPS_SCOMMON:
7163 asym->section = &mips_elf_scom_section;
7164 asym->value = elfsym->internal_elf_sym.st_size;
7165 break;
7166
7167 case SHN_MIPS_SUNDEFINED:
7168 asym->section = bfd_und_section_ptr;
7169 break;
7170
7171 case SHN_MIPS_TEXT:
7172 {
7173 asection *section = bfd_get_section_by_name (abfd, ".text");
7174
7175 if (section != NULL)
7176 {
7177 asym->section = section;
7178 /* MIPS_TEXT is a bit special, the address is not an offset
7179 to the base of the .text section. So subtract the section
7180 base address to make it an offset. */
7181 asym->value -= section->vma;
7182 }
7183 }
7184 break;
7185
7186 case SHN_MIPS_DATA:
7187 {
7188 asection *section = bfd_get_section_by_name (abfd, ".data");
7189
7190 if (section != NULL)
7191 {
7192 asym->section = section;
7193 /* MIPS_DATA is a bit special, the address is not an offset
7194 to the base of the .data section. So subtract the section
7195 base address to make it an offset. */
7196 asym->value -= section->vma;
7197 }
7198 }
7199 break;
7200 }
7201
7202 /* If this is an odd-valued function symbol, assume it's a MIPS16
7203 or microMIPS one. */
7204 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7205 && (asym->value & 1) != 0)
7206 {
7207 asym->value--;
7208 if (MICROMIPS_P (abfd))
7209 elfsym->internal_elf_sym.st_other
7210 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7211 else
7212 elfsym->internal_elf_sym.st_other
7213 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7214 }
7215 }
7216 \f
7217 /* Implement elf_backend_eh_frame_address_size. This differs from
7218 the default in the way it handles EABI64.
7219
7220 EABI64 was originally specified as an LP64 ABI, and that is what
7221 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7222 historically accepted the combination of -mabi=eabi and -mlong32,
7223 and this ILP32 variation has become semi-official over time.
7224 Both forms use elf32 and have pointer-sized FDE addresses.
7225
7226 If an EABI object was generated by GCC 4.0 or above, it will have
7227 an empty .gcc_compiled_longXX section, where XX is the size of longs
7228 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7229 have no special marking to distinguish them from LP64 objects.
7230
7231 We don't want users of the official LP64 ABI to be punished for the
7232 existence of the ILP32 variant, but at the same time, we don't want
7233 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7234 We therefore take the following approach:
7235
7236 - If ABFD contains a .gcc_compiled_longXX section, use it to
7237 determine the pointer size.
7238
7239 - Otherwise check the type of the first relocation. Assume that
7240 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7241
7242 - Otherwise punt.
7243
7244 The second check is enough to detect LP64 objects generated by pre-4.0
7245 compilers because, in the kind of output generated by those compilers,
7246 the first relocation will be associated with either a CIE personality
7247 routine or an FDE start address. Furthermore, the compilers never
7248 used a special (non-pointer) encoding for this ABI.
7249
7250 Checking the relocation type should also be safe because there is no
7251 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7252 did so. */
7253
7254 unsigned int
7255 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7256 {
7257 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7258 return 8;
7259 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7260 {
7261 bool long32_p, long64_p;
7262
7263 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7264 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7265 if (long32_p && long64_p)
7266 return 0;
7267 if (long32_p)
7268 return 4;
7269 if (long64_p)
7270 return 8;
7271
7272 if (sec->reloc_count > 0
7273 && elf_section_data (sec)->relocs != NULL
7274 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7275 == R_MIPS_64))
7276 return 8;
7277
7278 return 0;
7279 }
7280 return 4;
7281 }
7282 \f
7283 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7284 relocations against two unnamed section symbols to resolve to the
7285 same address. For example, if we have code like:
7286
7287 lw $4,%got_disp(.data)($gp)
7288 lw $25,%got_disp(.text)($gp)
7289 jalr $25
7290
7291 then the linker will resolve both relocations to .data and the program
7292 will jump there rather than to .text.
7293
7294 We can work around this problem by giving names to local section symbols.
7295 This is also what the MIPSpro tools do. */
7296
7297 bool
7298 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7299 {
7300 return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7301 }
7302 \f
7303 /* Work over a section just before writing it out. This routine is
7304 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7305 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7306 a better way. */
7307
7308 bool
7309 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7310 {
7311 if (hdr->sh_type == SHT_MIPS_REGINFO
7312 && hdr->sh_size > 0)
7313 {
7314 bfd_byte buf[4];
7315
7316 BFD_ASSERT (hdr->contents == NULL);
7317
7318 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7319 {
7320 _bfd_error_handler
7321 (_("%pB: incorrect `.reginfo' section size; "
7322 "expected %" PRIu64 ", got %" PRIu64),
7323 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7324 (uint64_t) hdr->sh_size);
7325 bfd_set_error (bfd_error_bad_value);
7326 return false;
7327 }
7328
7329 if (bfd_seek (abfd,
7330 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7331 SEEK_SET) != 0)
7332 return false;
7333 H_PUT_32 (abfd, elf_gp (abfd), buf);
7334 if (bfd_bwrite (buf, 4, abfd) != 4)
7335 return false;
7336 }
7337
7338 if (hdr->sh_type == SHT_MIPS_OPTIONS
7339 && hdr->bfd_section != NULL
7340 && mips_elf_section_data (hdr->bfd_section) != NULL
7341 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7342 {
7343 bfd_byte *contents, *l, *lend;
7344
7345 /* We stored the section contents in the tdata field in the
7346 set_section_contents routine. We save the section contents
7347 so that we don't have to read them again.
7348 At this point we know that elf_gp is set, so we can look
7349 through the section contents to see if there is an
7350 ODK_REGINFO structure. */
7351
7352 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7353 l = contents;
7354 lend = contents + hdr->sh_size;
7355 while (l + sizeof (Elf_External_Options) <= lend)
7356 {
7357 Elf_Internal_Options intopt;
7358
7359 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7360 &intopt);
7361 if (intopt.size < sizeof (Elf_External_Options))
7362 {
7363 _bfd_error_handler
7364 /* xgettext:c-format */
7365 (_("%pB: warning: bad `%s' option size %u smaller than"
7366 " its header"),
7367 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7368 break;
7369 }
7370 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7371 {
7372 bfd_byte buf[8];
7373
7374 if (bfd_seek (abfd,
7375 (hdr->sh_offset
7376 + (l - contents)
7377 + sizeof (Elf_External_Options)
7378 + (sizeof (Elf64_External_RegInfo) - 8)),
7379 SEEK_SET) != 0)
7380 return false;
7381 H_PUT_64 (abfd, elf_gp (abfd), buf);
7382 if (bfd_bwrite (buf, 8, abfd) != 8)
7383 return false;
7384 }
7385 else if (intopt.kind == ODK_REGINFO)
7386 {
7387 bfd_byte buf[4];
7388
7389 if (bfd_seek (abfd,
7390 (hdr->sh_offset
7391 + (l - contents)
7392 + sizeof (Elf_External_Options)
7393 + (sizeof (Elf32_External_RegInfo) - 4)),
7394 SEEK_SET) != 0)
7395 return false;
7396 H_PUT_32 (abfd, elf_gp (abfd), buf);
7397 if (bfd_bwrite (buf, 4, abfd) != 4)
7398 return false;
7399 }
7400 l += intopt.size;
7401 }
7402 }
7403
7404 if (hdr->bfd_section != NULL)
7405 {
7406 const char *name = bfd_section_name (hdr->bfd_section);
7407
7408 /* .sbss is not handled specially here because the GNU/Linux
7409 prelinker can convert .sbss from NOBITS to PROGBITS and
7410 changing it back to NOBITS breaks the binary. The entry in
7411 _bfd_mips_elf_special_sections will ensure the correct flags
7412 are set on .sbss if BFD creates it without reading it from an
7413 input file, and without special handling here the flags set
7414 on it in an input file will be followed. */
7415 if (strcmp (name, ".sdata") == 0
7416 || strcmp (name, ".lit8") == 0
7417 || strcmp (name, ".lit4") == 0)
7418 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7419 else if (strcmp (name, ".srdata") == 0)
7420 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7421 else if (strcmp (name, ".compact_rel") == 0)
7422 hdr->sh_flags = 0;
7423 else if (strcmp (name, ".rtproc") == 0)
7424 {
7425 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7426 {
7427 unsigned int adjust;
7428
7429 adjust = hdr->sh_size % hdr->sh_addralign;
7430 if (adjust != 0)
7431 hdr->sh_size += hdr->sh_addralign - adjust;
7432 }
7433 }
7434 }
7435
7436 return true;
7437 }
7438
7439 /* Handle a MIPS specific section when reading an object file. This
7440 is called when elfcode.h finds a section with an unknown type.
7441 This routine supports both the 32-bit and 64-bit ELF ABI. */
7442
7443 bool
7444 _bfd_mips_elf_section_from_shdr (bfd *abfd,
7445 Elf_Internal_Shdr *hdr,
7446 const char *name,
7447 int shindex)
7448 {
7449 flagword flags = 0;
7450
7451 /* There ought to be a place to keep ELF backend specific flags, but
7452 at the moment there isn't one. We just keep track of the
7453 sections by their name, instead. Fortunately, the ABI gives
7454 suggested names for all the MIPS specific sections, so we will
7455 probably get away with this. */
7456 switch (hdr->sh_type)
7457 {
7458 case SHT_MIPS_LIBLIST:
7459 if (strcmp (name, ".liblist") != 0)
7460 return false;
7461 break;
7462 case SHT_MIPS_MSYM:
7463 if (strcmp (name, ".msym") != 0)
7464 return false;
7465 break;
7466 case SHT_MIPS_CONFLICT:
7467 if (strcmp (name, ".conflict") != 0)
7468 return false;
7469 break;
7470 case SHT_MIPS_GPTAB:
7471 if (! startswith (name, ".gptab."))
7472 return false;
7473 break;
7474 case SHT_MIPS_UCODE:
7475 if (strcmp (name, ".ucode") != 0)
7476 return false;
7477 break;
7478 case SHT_MIPS_DEBUG:
7479 if (strcmp (name, ".mdebug") != 0)
7480 return false;
7481 flags = SEC_DEBUGGING;
7482 break;
7483 case SHT_MIPS_REGINFO:
7484 if (strcmp (name, ".reginfo") != 0
7485 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7486 return false;
7487 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7488 break;
7489 case SHT_MIPS_IFACE:
7490 if (strcmp (name, ".MIPS.interfaces") != 0)
7491 return false;
7492 break;
7493 case SHT_MIPS_CONTENT:
7494 if (! startswith (name, ".MIPS.content"))
7495 return false;
7496 break;
7497 case SHT_MIPS_OPTIONS:
7498 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7499 return false;
7500 break;
7501 case SHT_MIPS_ABIFLAGS:
7502 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7503 return false;
7504 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7505 break;
7506 case SHT_MIPS_DWARF:
7507 if (! startswith (name, ".debug_")
7508 && ! startswith (name, ".gnu.debuglto_.debug_")
7509 && ! startswith (name, ".zdebug_")
7510 && ! startswith (name, ".gnu.debuglto_.zdebug_"))
7511 return false;
7512 break;
7513 case SHT_MIPS_SYMBOL_LIB:
7514 if (strcmp (name, ".MIPS.symlib") != 0)
7515 return false;
7516 break;
7517 case SHT_MIPS_EVENTS:
7518 if (! startswith (name, ".MIPS.events")
7519 && ! startswith (name, ".MIPS.post_rel"))
7520 return false;
7521 break;
7522 case SHT_MIPS_XHASH:
7523 if (strcmp (name, ".MIPS.xhash") != 0)
7524 return false;
7525 default:
7526 break;
7527 }
7528
7529 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7530 return false;
7531
7532 if (hdr->sh_flags & SHF_MIPS_GPREL)
7533 flags |= SEC_SMALL_DATA;
7534
7535 if (flags)
7536 {
7537 if (!bfd_set_section_flags (hdr->bfd_section,
7538 (bfd_section_flags (hdr->bfd_section)
7539 | flags)))
7540 return false;
7541 }
7542
7543 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7544 {
7545 Elf_External_ABIFlags_v0 ext;
7546
7547 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7548 &ext, 0, sizeof ext))
7549 return false;
7550 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7551 &mips_elf_tdata (abfd)->abiflags);
7552 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7553 return false;
7554 mips_elf_tdata (abfd)->abiflags_valid = true;
7555 }
7556
7557 /* FIXME: We should record sh_info for a .gptab section. */
7558
7559 /* For a .reginfo section, set the gp value in the tdata information
7560 from the contents of this section. We need the gp value while
7561 processing relocs, so we just get it now. The .reginfo section
7562 is not used in the 64-bit MIPS ELF ABI. */
7563 if (hdr->sh_type == SHT_MIPS_REGINFO)
7564 {
7565 Elf32_External_RegInfo ext;
7566 Elf32_RegInfo s;
7567
7568 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7569 &ext, 0, sizeof ext))
7570 return false;
7571 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7572 elf_gp (abfd) = s.ri_gp_value;
7573 }
7574
7575 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7576 set the gp value based on what we find. We may see both
7577 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7578 they should agree. */
7579 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7580 {
7581 bfd_byte *contents, *l, *lend;
7582
7583 if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
7584 {
7585 free (contents);
7586 return false;
7587 }
7588 l = contents;
7589 lend = contents + hdr->sh_size;
7590 while (l + sizeof (Elf_External_Options) <= lend)
7591 {
7592 Elf_Internal_Options intopt;
7593
7594 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7595 &intopt);
7596 if (intopt.size < sizeof (Elf_External_Options))
7597 {
7598 bad_opt:
7599 _bfd_error_handler
7600 /* xgettext:c-format */
7601 (_("%pB: warning: truncated `%s' option"),
7602 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
7603 break;
7604 }
7605 if (intopt.kind == ODK_REGINFO)
7606 {
7607 if (ABI_64_P (abfd))
7608 {
7609 Elf64_Internal_RegInfo intreg;
7610 size_t needed = (sizeof (Elf_External_Options)
7611 + sizeof (Elf64_External_RegInfo));
7612 if (intopt.size < needed || (size_t) (lend - l) < needed)
7613 goto bad_opt;
7614 bfd_mips_elf64_swap_reginfo_in
7615 (abfd,
7616 ((Elf64_External_RegInfo *)
7617 (l + sizeof (Elf_External_Options))),
7618 &intreg);
7619 elf_gp (abfd) = intreg.ri_gp_value;
7620 }
7621 else
7622 {
7623 Elf32_RegInfo intreg;
7624 size_t needed = (sizeof (Elf_External_Options)
7625 + sizeof (Elf32_External_RegInfo));
7626 if (intopt.size < needed || (size_t) (lend - l) < needed)
7627 goto bad_opt;
7628 bfd_mips_elf32_swap_reginfo_in
7629 (abfd,
7630 ((Elf32_External_RegInfo *)
7631 (l + sizeof (Elf_External_Options))),
7632 &intreg);
7633 elf_gp (abfd) = intreg.ri_gp_value;
7634 }
7635 }
7636 l += intopt.size;
7637 }
7638 free (contents);
7639 }
7640
7641 return true;
7642 }
7643
7644 /* Set the correct type for a MIPS ELF section. We do this by the
7645 section name, which is a hack, but ought to work. This routine is
7646 used by both the 32-bit and the 64-bit ABI. */
7647
7648 bool
7649 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7650 {
7651 const char *name = bfd_section_name (sec);
7652
7653 if (strcmp (name, ".liblist") == 0)
7654 {
7655 hdr->sh_type = SHT_MIPS_LIBLIST;
7656 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7657 /* The sh_link field is set in final_write_processing. */
7658 }
7659 else if (strcmp (name, ".conflict") == 0)
7660 hdr->sh_type = SHT_MIPS_CONFLICT;
7661 else if (startswith (name, ".gptab."))
7662 {
7663 hdr->sh_type = SHT_MIPS_GPTAB;
7664 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7665 /* The sh_info field is set in final_write_processing. */
7666 }
7667 else if (strcmp (name, ".ucode") == 0)
7668 hdr->sh_type = SHT_MIPS_UCODE;
7669 else if (strcmp (name, ".mdebug") == 0)
7670 {
7671 hdr->sh_type = SHT_MIPS_DEBUG;
7672 /* In a shared object on IRIX 5.3, the .mdebug section has an
7673 entsize of 0. FIXME: Does this matter? */
7674 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7675 hdr->sh_entsize = 0;
7676 else
7677 hdr->sh_entsize = 1;
7678 }
7679 else if (strcmp (name, ".reginfo") == 0)
7680 {
7681 hdr->sh_type = SHT_MIPS_REGINFO;
7682 /* In a shared object on IRIX 5.3, the .reginfo section has an
7683 entsize of 0x18. FIXME: Does this matter? */
7684 if (SGI_COMPAT (abfd))
7685 {
7686 if ((abfd->flags & DYNAMIC) != 0)
7687 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7688 else
7689 hdr->sh_entsize = 1;
7690 }
7691 else
7692 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7693 }
7694 else if (SGI_COMPAT (abfd)
7695 && (strcmp (name, ".hash") == 0
7696 || strcmp (name, ".dynamic") == 0
7697 || strcmp (name, ".dynstr") == 0))
7698 {
7699 if (SGI_COMPAT (abfd))
7700 hdr->sh_entsize = 0;
7701 #if 0
7702 /* This isn't how the IRIX6 linker behaves. */
7703 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7704 #endif
7705 }
7706 else if (strcmp (name, ".got") == 0
7707 || strcmp (name, ".srdata") == 0
7708 || strcmp (name, ".sdata") == 0
7709 || strcmp (name, ".sbss") == 0
7710 || strcmp (name, ".lit4") == 0
7711 || strcmp (name, ".lit8") == 0)
7712 hdr->sh_flags |= SHF_MIPS_GPREL;
7713 else if (strcmp (name, ".MIPS.interfaces") == 0)
7714 {
7715 hdr->sh_type = SHT_MIPS_IFACE;
7716 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7717 }
7718 else if (startswith (name, ".MIPS.content"))
7719 {
7720 hdr->sh_type = SHT_MIPS_CONTENT;
7721 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7722 /* The sh_info field is set in final_write_processing. */
7723 }
7724 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7725 {
7726 hdr->sh_type = SHT_MIPS_OPTIONS;
7727 hdr->sh_entsize = 1;
7728 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7729 }
7730 else if (startswith (name, ".MIPS.abiflags"))
7731 {
7732 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7733 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7734 }
7735 else if (startswith (name, ".debug_")
7736 || startswith (name, ".gnu.debuglto_.debug_")
7737 || startswith (name, ".zdebug_")
7738 || startswith (name, ".gnu.debuglto_.zdebug_"))
7739 {
7740 hdr->sh_type = SHT_MIPS_DWARF;
7741
7742 /* Irix facilities such as libexc expect a single .debug_frame
7743 per executable, the system ones have NOSTRIP set and the linker
7744 doesn't merge sections with different flags so ... */
7745 if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
7746 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7747 }
7748 else if (strcmp (name, ".MIPS.symlib") == 0)
7749 {
7750 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7751 /* The sh_link and sh_info fields are set in
7752 final_write_processing. */
7753 }
7754 else if (startswith (name, ".MIPS.events")
7755 || startswith (name, ".MIPS.post_rel"))
7756 {
7757 hdr->sh_type = SHT_MIPS_EVENTS;
7758 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7759 /* The sh_link field is set in final_write_processing. */
7760 }
7761 else if (strcmp (name, ".msym") == 0)
7762 {
7763 hdr->sh_type = SHT_MIPS_MSYM;
7764 hdr->sh_flags |= SHF_ALLOC;
7765 hdr->sh_entsize = 8;
7766 }
7767 else if (strcmp (name, ".MIPS.xhash") == 0)
7768 {
7769 hdr->sh_type = SHT_MIPS_XHASH;
7770 hdr->sh_flags |= SHF_ALLOC;
7771 hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7772 }
7773
7774 /* The generic elf_fake_sections will set up REL_HDR using the default
7775 kind of relocations. We used to set up a second header for the
7776 non-default kind of relocations here, but only NewABI would use
7777 these, and the IRIX ld doesn't like resulting empty RELA sections.
7778 Thus we create those header only on demand now. */
7779
7780 return true;
7781 }
7782
7783 /* Given a BFD section, try to locate the corresponding ELF section
7784 index. This is used by both the 32-bit and the 64-bit ABI.
7785 Actually, it's not clear to me that the 64-bit ABI supports these,
7786 but for non-PIC objects we will certainly want support for at least
7787 the .scommon section. */
7788
7789 bool
7790 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7791 asection *sec, int *retval)
7792 {
7793 if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7794 {
7795 *retval = SHN_MIPS_SCOMMON;
7796 return true;
7797 }
7798 if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7799 {
7800 *retval = SHN_MIPS_ACOMMON;
7801 return true;
7802 }
7803 return false;
7804 }
7805 \f
7806 /* Hook called by the linker routine which adds symbols from an object
7807 file. We must handle the special MIPS section numbers here. */
7808
7809 bool
7810 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7811 Elf_Internal_Sym *sym, const char **namep,
7812 flagword *flagsp ATTRIBUTE_UNUSED,
7813 asection **secp, bfd_vma *valp)
7814 {
7815 if (SGI_COMPAT (abfd)
7816 && (abfd->flags & DYNAMIC) != 0
7817 && strcmp (*namep, "_rld_new_interface") == 0)
7818 {
7819 /* Skip IRIX5 rld entry name. */
7820 *namep = NULL;
7821 return true;
7822 }
7823
7824 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7825 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7826 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7827 a magic symbol resolved by the linker, we ignore this bogus definition
7828 of _gp_disp. New ABI objects do not suffer from this problem so this
7829 is not done for them. */
7830 if (!NEWABI_P(abfd)
7831 && (sym->st_shndx == SHN_ABS)
7832 && (strcmp (*namep, "_gp_disp") == 0))
7833 {
7834 *namep = NULL;
7835 return true;
7836 }
7837
7838 switch (sym->st_shndx)
7839 {
7840 case SHN_COMMON:
7841 /* Common symbols less than the GP size are automatically
7842 treated as SHN_MIPS_SCOMMON symbols. */
7843 if (sym->st_size > elf_gp_size (abfd)
7844 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7845 || IRIX_COMPAT (abfd) == ict_irix6)
7846 break;
7847 /* Fall through. */
7848 case SHN_MIPS_SCOMMON:
7849 *secp = bfd_make_section_old_way (abfd, ".scommon");
7850 (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
7851 *valp = sym->st_size;
7852 break;
7853
7854 case SHN_MIPS_TEXT:
7855 /* This section is used in a shared object. */
7856 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7857 {
7858 asymbol *elf_text_symbol;
7859 asection *elf_text_section;
7860 size_t amt = sizeof (asection);
7861
7862 elf_text_section = bfd_zalloc (abfd, amt);
7863 if (elf_text_section == NULL)
7864 return false;
7865
7866 amt = sizeof (asymbol);
7867 elf_text_symbol = bfd_zalloc (abfd, amt);
7868 if (elf_text_symbol == NULL)
7869 return false;
7870
7871 /* Initialize the section. */
7872
7873 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7874 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7875
7876 elf_text_section->symbol = elf_text_symbol;
7877 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7878
7879 elf_text_section->name = ".text";
7880 elf_text_section->flags = SEC_NO_FLAGS;
7881 elf_text_section->output_section = NULL;
7882 elf_text_section->owner = abfd;
7883 elf_text_symbol->name = ".text";
7884 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7885 elf_text_symbol->section = elf_text_section;
7886 }
7887 /* This code used to do *secp = bfd_und_section_ptr if
7888 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7889 so I took it out. */
7890 *secp = mips_elf_tdata (abfd)->elf_text_section;
7891 break;
7892
7893 case SHN_MIPS_ACOMMON:
7894 /* Fall through. XXX Can we treat this as allocated data? */
7895 case SHN_MIPS_DATA:
7896 /* This section is used in a shared object. */
7897 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7898 {
7899 asymbol *elf_data_symbol;
7900 asection *elf_data_section;
7901 size_t amt = sizeof (asection);
7902
7903 elf_data_section = bfd_zalloc (abfd, amt);
7904 if (elf_data_section == NULL)
7905 return false;
7906
7907 amt = sizeof (asymbol);
7908 elf_data_symbol = bfd_zalloc (abfd, amt);
7909 if (elf_data_symbol == NULL)
7910 return false;
7911
7912 /* Initialize the section. */
7913
7914 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7915 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7916
7917 elf_data_section->symbol = elf_data_symbol;
7918 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7919
7920 elf_data_section->name = ".data";
7921 elf_data_section->flags = SEC_NO_FLAGS;
7922 elf_data_section->output_section = NULL;
7923 elf_data_section->owner = abfd;
7924 elf_data_symbol->name = ".data";
7925 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7926 elf_data_symbol->section = elf_data_section;
7927 }
7928 /* This code used to do *secp = bfd_und_section_ptr if
7929 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7930 so I took it out. */
7931 *secp = mips_elf_tdata (abfd)->elf_data_section;
7932 break;
7933
7934 case SHN_MIPS_SUNDEFINED:
7935 *secp = bfd_und_section_ptr;
7936 break;
7937 }
7938
7939 if (SGI_COMPAT (abfd)
7940 && ! bfd_link_pic (info)
7941 && info->output_bfd->xvec == abfd->xvec
7942 && strcmp (*namep, "__rld_obj_head") == 0)
7943 {
7944 struct elf_link_hash_entry *h;
7945 struct bfd_link_hash_entry *bh;
7946
7947 /* Mark __rld_obj_head as dynamic. */
7948 bh = NULL;
7949 if (! (_bfd_generic_link_add_one_symbol
7950 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
7951 get_elf_backend_data (abfd)->collect, &bh)))
7952 return false;
7953
7954 h = (struct elf_link_hash_entry *) bh;
7955 h->non_elf = 0;
7956 h->def_regular = 1;
7957 h->type = STT_OBJECT;
7958
7959 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7960 return false;
7961
7962 mips_elf_hash_table (info)->use_rld_obj_head = true;
7963 mips_elf_hash_table (info)->rld_symbol = h;
7964 }
7965
7966 /* If this is a mips16 text symbol, add 1 to the value to make it
7967 odd. This will cause something like .word SYM to come up with
7968 the right value when it is loaded into the PC. */
7969 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7970 ++*valp;
7971
7972 return true;
7973 }
7974
7975 /* This hook function is called before the linker writes out a global
7976 symbol. We mark symbols as small common if appropriate. This is
7977 also where we undo the increment of the value for a mips16 symbol. */
7978
7979 int
7980 _bfd_mips_elf_link_output_symbol_hook
7981 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7982 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7983 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7984 {
7985 /* If we see a common symbol, which implies a relocatable link, then
7986 if a symbol was small common in an input file, mark it as small
7987 common in the output file. */
7988 if (sym->st_shndx == SHN_COMMON
7989 && strcmp (input_sec->name, ".scommon") == 0)
7990 sym->st_shndx = SHN_MIPS_SCOMMON;
7991
7992 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7993 sym->st_value &= ~1;
7994
7995 return 1;
7996 }
7997 \f
7998 /* Functions for the dynamic linker. */
7999
8000 /* Create dynamic sections when linking against a dynamic object. */
8001
8002 bool
8003 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
8004 {
8005 struct elf_link_hash_entry *h;
8006 struct bfd_link_hash_entry *bh;
8007 flagword flags;
8008 register asection *s;
8009 const char * const *namep;
8010 struct mips_elf_link_hash_table *htab;
8011
8012 htab = mips_elf_hash_table (info);
8013 BFD_ASSERT (htab != NULL);
8014
8015 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8016 | SEC_LINKER_CREATED | SEC_READONLY);
8017
8018 /* The psABI requires a read-only .dynamic section, but the VxWorks
8019 EABI doesn't. */
8020 if (htab->root.target_os != is_vxworks)
8021 {
8022 s = bfd_get_linker_section (abfd, ".dynamic");
8023 if (s != NULL)
8024 {
8025 if (!bfd_set_section_flags (s, flags))
8026 return false;
8027 }
8028 }
8029
8030 /* We need to create .got section. */
8031 if (!mips_elf_create_got_section (abfd, info))
8032 return false;
8033
8034 if (! mips_elf_rel_dyn_section (info, true))
8035 return false;
8036
8037 /* Create .stub section. */
8038 s = bfd_make_section_anyway_with_flags (abfd,
8039 MIPS_ELF_STUB_SECTION_NAME (abfd),
8040 flags | SEC_CODE);
8041 if (s == NULL
8042 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8043 return false;
8044 htab->sstubs = s;
8045
8046 if (!mips_elf_hash_table (info)->use_rld_obj_head
8047 && bfd_link_executable (info)
8048 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8049 {
8050 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8051 flags &~ (flagword) SEC_READONLY);
8052 if (s == NULL
8053 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8054 return false;
8055 }
8056
8057 /* Create .MIPS.xhash section. */
8058 if (info->emit_gnu_hash)
8059 s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8060 flags | SEC_READONLY);
8061
8062 /* On IRIX5, we adjust add some additional symbols and change the
8063 alignments of several sections. There is no ABI documentation
8064 indicating that this is necessary on IRIX6, nor any evidence that
8065 the linker takes such action. */
8066 if (IRIX_COMPAT (abfd) == ict_irix5)
8067 {
8068 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8069 {
8070 bh = NULL;
8071 if (! (_bfd_generic_link_add_one_symbol
8072 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8073 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8074 return false;
8075
8076 h = (struct elf_link_hash_entry *) bh;
8077 h->mark = 1;
8078 h->non_elf = 0;
8079 h->def_regular = 1;
8080 h->type = STT_SECTION;
8081
8082 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8083 return false;
8084 }
8085
8086 /* We need to create a .compact_rel section. */
8087 if (SGI_COMPAT (abfd))
8088 {
8089 if (!mips_elf_create_compact_rel_section (abfd, info))
8090 return false;
8091 }
8092
8093 /* Change alignments of some sections. */
8094 s = bfd_get_linker_section (abfd, ".hash");
8095 if (s != NULL)
8096 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8097
8098 s = bfd_get_linker_section (abfd, ".dynsym");
8099 if (s != NULL)
8100 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8101
8102 s = bfd_get_linker_section (abfd, ".dynstr");
8103 if (s != NULL)
8104 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8105
8106 /* ??? */
8107 s = bfd_get_section_by_name (abfd, ".reginfo");
8108 if (s != NULL)
8109 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8110
8111 s = bfd_get_linker_section (abfd, ".dynamic");
8112 if (s != NULL)
8113 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8114 }
8115
8116 if (bfd_link_executable (info))
8117 {
8118 const char *name;
8119
8120 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8121 bh = NULL;
8122 if (!(_bfd_generic_link_add_one_symbol
8123 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8124 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8125 return false;
8126
8127 h = (struct elf_link_hash_entry *) bh;
8128 h->non_elf = 0;
8129 h->def_regular = 1;
8130 h->type = STT_SECTION;
8131
8132 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8133 return false;
8134
8135 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8136 {
8137 /* __rld_map is a four byte word located in the .data section
8138 and is filled in by the rtld to contain a pointer to
8139 the _r_debug structure. Its symbol value will be set in
8140 _bfd_mips_elf_finish_dynamic_symbol. */
8141 s = bfd_get_linker_section (abfd, ".rld_map");
8142 BFD_ASSERT (s != NULL);
8143
8144 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8145 bh = NULL;
8146 if (!(_bfd_generic_link_add_one_symbol
8147 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
8148 get_elf_backend_data (abfd)->collect, &bh)))
8149 return false;
8150
8151 h = (struct elf_link_hash_entry *) bh;
8152 h->non_elf = 0;
8153 h->def_regular = 1;
8154 h->type = STT_OBJECT;
8155
8156 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8157 return false;
8158 mips_elf_hash_table (info)->rld_symbol = h;
8159 }
8160 }
8161
8162 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8163 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
8164 if (!_bfd_elf_create_dynamic_sections (abfd, info))
8165 return false;
8166
8167 /* Do the usual VxWorks handling. */
8168 if (htab->root.target_os == is_vxworks
8169 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8170 return false;
8171
8172 return true;
8173 }
8174 \f
8175 /* Return true if relocation REL against section SEC is a REL rather than
8176 RELA relocation. RELOCS is the first relocation in the section and
8177 ABFD is the bfd that contains SEC. */
8178
8179 static bool
8180 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8181 const Elf_Internal_Rela *relocs,
8182 const Elf_Internal_Rela *rel)
8183 {
8184 Elf_Internal_Shdr *rel_hdr;
8185 const struct elf_backend_data *bed;
8186
8187 /* To determine which flavor of relocation this is, we depend on the
8188 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8189 rel_hdr = elf_section_data (sec)->rel.hdr;
8190 if (rel_hdr == NULL)
8191 return false;
8192 bed = get_elf_backend_data (abfd);
8193 return ((size_t) (rel - relocs)
8194 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8195 }
8196
8197 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8198 HOWTO is the relocation's howto and CONTENTS points to the contents
8199 of the section that REL is against. */
8200
8201 static bfd_vma
8202 mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8203 const Elf_Internal_Rela *rel,
8204 reloc_howto_type *howto, bfd_byte *contents)
8205 {
8206 bfd_byte *location;
8207 unsigned int r_type;
8208 bfd_vma addend;
8209 bfd_vma bytes;
8210
8211 if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8212 return 0;
8213
8214 r_type = ELF_R_TYPE (abfd, rel->r_info);
8215 location = contents + rel->r_offset;
8216
8217 /* Get the addend, which is stored in the input file. */
8218 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
8219 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8220 _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
8221
8222 addend = bytes & howto->src_mask;
8223
8224 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8225 accordingly. */
8226 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8227 addend <<= 1;
8228
8229 return addend;
8230 }
8231
8232 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
8233 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8234 and update *ADDEND with the final addend. Return true on success
8235 or false if the LO16 could not be found. RELEND is the exclusive
8236 upper bound on the relocations for REL's section. */
8237
8238 static bool
8239 mips_elf_add_lo16_rel_addend (bfd *abfd,
8240 asection *sec,
8241 const Elf_Internal_Rela *rel,
8242 const Elf_Internal_Rela *relend,
8243 bfd_byte *contents, bfd_vma *addend)
8244 {
8245 unsigned int r_type, lo16_type;
8246 const Elf_Internal_Rela *lo16_relocation;
8247 reloc_howto_type *lo16_howto;
8248 bfd_vma l;
8249
8250 r_type = ELF_R_TYPE (abfd, rel->r_info);
8251 if (mips16_reloc_p (r_type))
8252 lo16_type = R_MIPS16_LO16;
8253 else if (micromips_reloc_p (r_type))
8254 lo16_type = R_MICROMIPS_LO16;
8255 else if (r_type == R_MIPS_PCHI16)
8256 lo16_type = R_MIPS_PCLO16;
8257 else
8258 lo16_type = R_MIPS_LO16;
8259
8260 /* The combined value is the sum of the HI16 addend, left-shifted by
8261 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8262 code does a `lui' of the HI16 value, and then an `addiu' of the
8263 LO16 value.)
8264
8265 Scan ahead to find a matching LO16 relocation.
8266
8267 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8268 be immediately following. However, for the IRIX6 ABI, the next
8269 relocation may be a composed relocation consisting of several
8270 relocations for the same address. In that case, the R_MIPS_LO16
8271 relocation may occur as one of these. We permit a similar
8272 extension in general, as that is useful for GCC.
8273
8274 In some cases GCC dead code elimination removes the LO16 but keeps
8275 the corresponding HI16. This is strictly speaking a violation of
8276 the ABI but not immediately harmful. */
8277 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8278 if (lo16_relocation == NULL)
8279 return false;
8280
8281 /* Obtain the addend kept there. */
8282 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
8283 l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8284 contents);
8285
8286 l <<= lo16_howto->rightshift;
8287 l = _bfd_mips_elf_sign_extend (l, 16);
8288
8289 *addend <<= 16;
8290 *addend += l;
8291 return true;
8292 }
8293
8294 /* Try to read the contents of section SEC in bfd ABFD. Return true and
8295 store the contents in *CONTENTS on success. Assume that *CONTENTS
8296 already holds the contents if it is nonull on entry. */
8297
8298 static bool
8299 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8300 {
8301 if (*contents)
8302 return true;
8303
8304 /* Get cached copy if it exists. */
8305 if (elf_section_data (sec)->this_hdr.contents != NULL)
8306 {
8307 *contents = elf_section_data (sec)->this_hdr.contents;
8308 return true;
8309 }
8310
8311 return bfd_malloc_and_get_section (abfd, sec, contents);
8312 }
8313
8314 /* Make a new PLT record to keep internal data. */
8315
8316 static struct plt_entry *
8317 mips_elf_make_plt_record (bfd *abfd)
8318 {
8319 struct plt_entry *entry;
8320
8321 entry = bfd_zalloc (abfd, sizeof (*entry));
8322 if (entry == NULL)
8323 return NULL;
8324
8325 entry->stub_offset = MINUS_ONE;
8326 entry->mips_offset = MINUS_ONE;
8327 entry->comp_offset = MINUS_ONE;
8328 entry->gotplt_index = MINUS_ONE;
8329 return entry;
8330 }
8331
8332 /* Define the special `__gnu_absolute_zero' symbol. We only need this
8333 for PIC code, as otherwise there is no load-time relocation involved
8334 and local GOT entries whose value is zero at static link time will
8335 retain their value at load time. */
8336
8337 static bool
8338 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8339 struct mips_elf_link_hash_table *htab,
8340 unsigned int r_type)
8341 {
8342 union
8343 {
8344 struct elf_link_hash_entry *eh;
8345 struct bfd_link_hash_entry *bh;
8346 }
8347 hzero;
8348
8349 BFD_ASSERT (!htab->use_absolute_zero);
8350 BFD_ASSERT (bfd_link_pic (info));
8351
8352 hzero.bh = NULL;
8353 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8354 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8355 NULL, false, false, &hzero.bh))
8356 return false;
8357
8358 BFD_ASSERT (hzero.bh != NULL);
8359 hzero.eh->size = 0;
8360 hzero.eh->type = STT_NOTYPE;
8361 hzero.eh->other = STV_PROTECTED;
8362 hzero.eh->def_regular = 1;
8363 hzero.eh->non_elf = 0;
8364
8365 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8366 return false;
8367
8368 htab->use_absolute_zero = true;
8369
8370 return true;
8371 }
8372
8373 /* Look through the relocs for a section during the first phase, and
8374 allocate space in the global offset table and record the need for
8375 standard MIPS and compressed procedure linkage table entries. */
8376
8377 bool
8378 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8379 asection *sec, const Elf_Internal_Rela *relocs)
8380 {
8381 const char *name;
8382 bfd *dynobj;
8383 Elf_Internal_Shdr *symtab_hdr;
8384 struct elf_link_hash_entry **sym_hashes;
8385 size_t extsymoff;
8386 const Elf_Internal_Rela *rel;
8387 const Elf_Internal_Rela *rel_end;
8388 asection *sreloc;
8389 const struct elf_backend_data *bed;
8390 struct mips_elf_link_hash_table *htab;
8391 bfd_byte *contents;
8392 bfd_vma addend;
8393 reloc_howto_type *howto;
8394
8395 if (bfd_link_relocatable (info))
8396 return true;
8397
8398 htab = mips_elf_hash_table (info);
8399 BFD_ASSERT (htab != NULL);
8400
8401 dynobj = elf_hash_table (info)->dynobj;
8402 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8403 sym_hashes = elf_sym_hashes (abfd);
8404 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8405
8406 bed = get_elf_backend_data (abfd);
8407 rel_end = relocs + sec->reloc_count;
8408
8409 /* Check for the mips16 stub sections. */
8410
8411 name = bfd_section_name (sec);
8412 if (FN_STUB_P (name))
8413 {
8414 unsigned long r_symndx;
8415
8416 /* Look at the relocation information to figure out which symbol
8417 this is for. */
8418
8419 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8420 if (r_symndx == 0)
8421 {
8422 _bfd_error_handler
8423 /* xgettext:c-format */
8424 (_("%pB: warning: cannot determine the target function for"
8425 " stub section `%s'"),
8426 abfd, name);
8427 bfd_set_error (bfd_error_bad_value);
8428 return false;
8429 }
8430
8431 if (r_symndx < extsymoff
8432 || sym_hashes[r_symndx - extsymoff] == NULL)
8433 {
8434 asection *o;
8435
8436 /* This stub is for a local symbol. This stub will only be
8437 needed if there is some relocation in this BFD, other
8438 than a 16 bit function call, which refers to this symbol. */
8439 for (o = abfd->sections; o != NULL; o = o->next)
8440 {
8441 Elf_Internal_Rela *sec_relocs;
8442 const Elf_Internal_Rela *r, *rend;
8443
8444 /* We can ignore stub sections when looking for relocs. */
8445 if ((o->flags & SEC_RELOC) == 0
8446 || o->reloc_count == 0
8447 || section_allows_mips16_refs_p (o))
8448 continue;
8449
8450 sec_relocs
8451 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8452 info->keep_memory);
8453 if (sec_relocs == NULL)
8454 return false;
8455
8456 rend = sec_relocs + o->reloc_count;
8457 for (r = sec_relocs; r < rend; r++)
8458 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8459 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8460 break;
8461
8462 if (elf_section_data (o)->relocs != sec_relocs)
8463 free (sec_relocs);
8464
8465 if (r < rend)
8466 break;
8467 }
8468
8469 if (o == NULL)
8470 {
8471 /* There is no non-call reloc for this stub, so we do
8472 not need it. Since this function is called before
8473 the linker maps input sections to output sections, we
8474 can easily discard it by setting the SEC_EXCLUDE
8475 flag. */
8476 sec->flags |= SEC_EXCLUDE;
8477 return true;
8478 }
8479
8480 /* Record this stub in an array of local symbol stubs for
8481 this BFD. */
8482 if (mips_elf_tdata (abfd)->local_stubs == NULL)
8483 {
8484 unsigned long symcount;
8485 asection **n;
8486 bfd_size_type amt;
8487
8488 if (elf_bad_symtab (abfd))
8489 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8490 else
8491 symcount = symtab_hdr->sh_info;
8492 amt = symcount * sizeof (asection *);
8493 n = bfd_zalloc (abfd, amt);
8494 if (n == NULL)
8495 return false;
8496 mips_elf_tdata (abfd)->local_stubs = n;
8497 }
8498
8499 sec->flags |= SEC_KEEP;
8500 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8501
8502 /* We don't need to set mips16_stubs_seen in this case.
8503 That flag is used to see whether we need to look through
8504 the global symbol table for stubs. We don't need to set
8505 it here, because we just have a local stub. */
8506 }
8507 else
8508 {
8509 struct mips_elf_link_hash_entry *h;
8510
8511 h = ((struct mips_elf_link_hash_entry *)
8512 sym_hashes[r_symndx - extsymoff]);
8513
8514 while (h->root.root.type == bfd_link_hash_indirect
8515 || h->root.root.type == bfd_link_hash_warning)
8516 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8517
8518 /* H is the symbol this stub is for. */
8519
8520 /* If we already have an appropriate stub for this function, we
8521 don't need another one, so we can discard this one. Since
8522 this function is called before the linker maps input sections
8523 to output sections, we can easily discard it by setting the
8524 SEC_EXCLUDE flag. */
8525 if (h->fn_stub != NULL)
8526 {
8527 sec->flags |= SEC_EXCLUDE;
8528 return true;
8529 }
8530
8531 sec->flags |= SEC_KEEP;
8532 h->fn_stub = sec;
8533 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8534 }
8535 }
8536 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8537 {
8538 unsigned long r_symndx;
8539 struct mips_elf_link_hash_entry *h;
8540 asection **loc;
8541
8542 /* Look at the relocation information to figure out which symbol
8543 this is for. */
8544
8545 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8546 if (r_symndx == 0)
8547 {
8548 _bfd_error_handler
8549 /* xgettext:c-format */
8550 (_("%pB: warning: cannot determine the target function for"
8551 " stub section `%s'"),
8552 abfd, name);
8553 bfd_set_error (bfd_error_bad_value);
8554 return false;
8555 }
8556
8557 if (r_symndx < extsymoff
8558 || sym_hashes[r_symndx - extsymoff] == NULL)
8559 {
8560 asection *o;
8561
8562 /* This stub is for a local symbol. This stub will only be
8563 needed if there is some relocation (R_MIPS16_26) in this BFD
8564 that refers to this symbol. */
8565 for (o = abfd->sections; o != NULL; o = o->next)
8566 {
8567 Elf_Internal_Rela *sec_relocs;
8568 const Elf_Internal_Rela *r, *rend;
8569
8570 /* We can ignore stub sections when looking for relocs. */
8571 if ((o->flags & SEC_RELOC) == 0
8572 || o->reloc_count == 0
8573 || section_allows_mips16_refs_p (o))
8574 continue;
8575
8576 sec_relocs
8577 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8578 info->keep_memory);
8579 if (sec_relocs == NULL)
8580 return false;
8581
8582 rend = sec_relocs + o->reloc_count;
8583 for (r = sec_relocs; r < rend; r++)
8584 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8585 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8586 break;
8587
8588 if (elf_section_data (o)->relocs != sec_relocs)
8589 free (sec_relocs);
8590
8591 if (r < rend)
8592 break;
8593 }
8594
8595 if (o == NULL)
8596 {
8597 /* There is no non-call reloc for this stub, so we do
8598 not need it. Since this function is called before
8599 the linker maps input sections to output sections, we
8600 can easily discard it by setting the SEC_EXCLUDE
8601 flag. */
8602 sec->flags |= SEC_EXCLUDE;
8603 return true;
8604 }
8605
8606 /* Record this stub in an array of local symbol call_stubs for
8607 this BFD. */
8608 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8609 {
8610 unsigned long symcount;
8611 asection **n;
8612 bfd_size_type amt;
8613
8614 if (elf_bad_symtab (abfd))
8615 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8616 else
8617 symcount = symtab_hdr->sh_info;
8618 amt = symcount * sizeof (asection *);
8619 n = bfd_zalloc (abfd, amt);
8620 if (n == NULL)
8621 return false;
8622 mips_elf_tdata (abfd)->local_call_stubs = n;
8623 }
8624
8625 sec->flags |= SEC_KEEP;
8626 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8627
8628 /* We don't need to set mips16_stubs_seen in this case.
8629 That flag is used to see whether we need to look through
8630 the global symbol table for stubs. We don't need to set
8631 it here, because we just have a local stub. */
8632 }
8633 else
8634 {
8635 h = ((struct mips_elf_link_hash_entry *)
8636 sym_hashes[r_symndx - extsymoff]);
8637
8638 /* H is the symbol this stub is for. */
8639
8640 if (CALL_FP_STUB_P (name))
8641 loc = &h->call_fp_stub;
8642 else
8643 loc = &h->call_stub;
8644
8645 /* If we already have an appropriate stub for this function, we
8646 don't need another one, so we can discard this one. Since
8647 this function is called before the linker maps input sections
8648 to output sections, we can easily discard it by setting the
8649 SEC_EXCLUDE flag. */
8650 if (*loc != NULL)
8651 {
8652 sec->flags |= SEC_EXCLUDE;
8653 return true;
8654 }
8655
8656 sec->flags |= SEC_KEEP;
8657 *loc = sec;
8658 mips_elf_hash_table (info)->mips16_stubs_seen = true;
8659 }
8660 }
8661
8662 sreloc = NULL;
8663 contents = NULL;
8664 for (rel = relocs; rel < rel_end; ++rel)
8665 {
8666 unsigned long r_symndx;
8667 unsigned int r_type;
8668 struct elf_link_hash_entry *h;
8669 bool can_make_dynamic_p;
8670 bool call_reloc_p;
8671 bool constrain_symbol_p;
8672
8673 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8674 r_type = ELF_R_TYPE (abfd, rel->r_info);
8675
8676 if (r_symndx < extsymoff)
8677 h = NULL;
8678 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8679 {
8680 _bfd_error_handler
8681 /* xgettext:c-format */
8682 (_("%pB: malformed reloc detected for section %s"),
8683 abfd, name);
8684 bfd_set_error (bfd_error_bad_value);
8685 return false;
8686 }
8687 else
8688 {
8689 h = sym_hashes[r_symndx - extsymoff];
8690 if (h != NULL)
8691 {
8692 while (h->root.type == bfd_link_hash_indirect
8693 || h->root.type == bfd_link_hash_warning)
8694 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8695 }
8696 }
8697
8698 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8699 relocation into a dynamic one. */
8700 can_make_dynamic_p = false;
8701
8702 /* Set CALL_RELOC_P to true if the relocation is for a call,
8703 and if pointer equality therefore doesn't matter. */
8704 call_reloc_p = false;
8705
8706 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8707 into account when deciding how to define the symbol. */
8708 constrain_symbol_p = true;
8709
8710 switch (r_type)
8711 {
8712 case R_MIPS_CALL16:
8713 case R_MIPS_CALL_HI16:
8714 case R_MIPS_CALL_LO16:
8715 case R_MIPS16_CALL16:
8716 case R_MICROMIPS_CALL16:
8717 case R_MICROMIPS_CALL_HI16:
8718 case R_MICROMIPS_CALL_LO16:
8719 call_reloc_p = true;
8720 /* Fall through. */
8721
8722 case R_MIPS_GOT16:
8723 case R_MIPS_GOT_LO16:
8724 case R_MIPS_GOT_PAGE:
8725 case R_MIPS_GOT_DISP:
8726 case R_MIPS16_GOT16:
8727 case R_MICROMIPS_GOT16:
8728 case R_MICROMIPS_GOT_LO16:
8729 case R_MICROMIPS_GOT_PAGE:
8730 case R_MICROMIPS_GOT_DISP:
8731 /* If we have a symbol that will resolve to zero at static link
8732 time and it is used by a GOT relocation applied to code we
8733 cannot relax to an immediate zero load, then we will be using
8734 the special `__gnu_absolute_zero' symbol whose value is zero
8735 at dynamic load time. We ignore HI16-type GOT relocations at
8736 this stage, because their handling will depend entirely on
8737 the corresponding LO16-type GOT relocation. */
8738 if (!call_hi16_reloc_p (r_type)
8739 && h != NULL
8740 && bfd_link_pic (info)
8741 && !htab->use_absolute_zero
8742 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8743 {
8744 bool rel_reloc;
8745
8746 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8747 return false;
8748
8749 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8750 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8751 if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8752 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8753 false))
8754 if (!mips_elf_define_absolute_zero (abfd, info, htab,
8755 r_type))
8756 return false;
8757 }
8758
8759 /* Fall through. */
8760 case R_MIPS_GOT_HI16:
8761 case R_MIPS_GOT_OFST:
8762 case R_MIPS_TLS_GOTTPREL:
8763 case R_MIPS_TLS_GD:
8764 case R_MIPS_TLS_LDM:
8765 case R_MIPS16_TLS_GOTTPREL:
8766 case R_MIPS16_TLS_GD:
8767 case R_MIPS16_TLS_LDM:
8768 case R_MICROMIPS_GOT_HI16:
8769 case R_MICROMIPS_GOT_OFST:
8770 case R_MICROMIPS_TLS_GOTTPREL:
8771 case R_MICROMIPS_TLS_GD:
8772 case R_MICROMIPS_TLS_LDM:
8773 if (dynobj == NULL)
8774 elf_hash_table (info)->dynobj = dynobj = abfd;
8775 if (!mips_elf_create_got_section (dynobj, info))
8776 return false;
8777 if (htab->root.target_os == is_vxworks
8778 && !bfd_link_pic (info))
8779 {
8780 _bfd_error_handler
8781 /* xgettext:c-format */
8782 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8783 abfd, (uint64_t) rel->r_offset);
8784 bfd_set_error (bfd_error_bad_value);
8785 return false;
8786 }
8787 can_make_dynamic_p = true;
8788 break;
8789
8790 case R_MIPS_NONE:
8791 case R_MIPS_JALR:
8792 case R_MICROMIPS_JALR:
8793 /* These relocations have empty fields and are purely there to
8794 provide link information. The symbol value doesn't matter. */
8795 constrain_symbol_p = false;
8796 break;
8797
8798 case R_MIPS_GPREL16:
8799 case R_MIPS_GPREL32:
8800 case R_MIPS16_GPREL:
8801 case R_MICROMIPS_GPREL16:
8802 /* GP-relative relocations always resolve to a definition in a
8803 regular input file, ignoring the one-definition rule. This is
8804 important for the GP setup sequence in NewABI code, which
8805 always resolves to a local function even if other relocations
8806 against the symbol wouldn't. */
8807 constrain_symbol_p = false;
8808 break;
8809
8810 case R_MIPS_32:
8811 case R_MIPS_REL32:
8812 case R_MIPS_64:
8813 /* In VxWorks executables, references to external symbols
8814 must be handled using copy relocs or PLT entries; it is not
8815 possible to convert this relocation into a dynamic one.
8816
8817 For executables that use PLTs and copy-relocs, we have a
8818 choice between converting the relocation into a dynamic
8819 one or using copy relocations or PLT entries. It is
8820 usually better to do the former, unless the relocation is
8821 against a read-only section. */
8822 if ((bfd_link_pic (info)
8823 || (h != NULL
8824 && htab->root.target_os != is_vxworks
8825 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8826 && !(!info->nocopyreloc
8827 && !PIC_OBJECT_P (abfd)
8828 && MIPS_ELF_READONLY_SECTION (sec))))
8829 && (sec->flags & SEC_ALLOC) != 0)
8830 {
8831 can_make_dynamic_p = true;
8832 if (dynobj == NULL)
8833 elf_hash_table (info)->dynobj = dynobj = abfd;
8834 }
8835 break;
8836
8837 case R_MIPS_26:
8838 case R_MIPS_PC16:
8839 case R_MIPS_PC21_S2:
8840 case R_MIPS_PC26_S2:
8841 case R_MIPS16_26:
8842 case R_MIPS16_PC16_S1:
8843 case R_MICROMIPS_26_S1:
8844 case R_MICROMIPS_PC7_S1:
8845 case R_MICROMIPS_PC10_S1:
8846 case R_MICROMIPS_PC16_S1:
8847 case R_MICROMIPS_PC23_S2:
8848 call_reloc_p = true;
8849 break;
8850 }
8851
8852 if (h)
8853 {
8854 if (constrain_symbol_p)
8855 {
8856 if (!can_make_dynamic_p)
8857 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8858
8859 if (!call_reloc_p)
8860 h->pointer_equality_needed = 1;
8861
8862 /* We must not create a stub for a symbol that has
8863 relocations related to taking the function's address.
8864 This doesn't apply to VxWorks, where CALL relocs refer
8865 to a .got.plt entry instead of a normal .got entry. */
8866 if (htab->root.target_os != is_vxworks
8867 && (!can_make_dynamic_p || !call_reloc_p))
8868 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
8869 }
8870
8871 /* Relocations against the special VxWorks __GOTT_BASE__ and
8872 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8873 room for them in .rela.dyn. */
8874 if (is_gott_symbol (info, h))
8875 {
8876 if (sreloc == NULL)
8877 {
8878 sreloc = mips_elf_rel_dyn_section (info, true);
8879 if (sreloc == NULL)
8880 return false;
8881 }
8882 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8883 if (MIPS_ELF_READONLY_SECTION (sec))
8884 /* We tell the dynamic linker that there are
8885 relocations against the text segment. */
8886 info->flags |= DF_TEXTREL;
8887 }
8888 }
8889 else if (call_lo16_reloc_p (r_type)
8890 || got_lo16_reloc_p (r_type)
8891 || got_disp_reloc_p (r_type)
8892 || (got16_reloc_p (r_type)
8893 && htab->root.target_os == is_vxworks))
8894 {
8895 /* We may need a local GOT entry for this relocation. We
8896 don't count R_MIPS_GOT_PAGE because we can estimate the
8897 maximum number of pages needed by looking at the size of
8898 the segment. Similar comments apply to R_MIPS*_GOT16 and
8899 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8900 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8901 R_MIPS_CALL_HI16 because these are always followed by an
8902 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8903 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8904 rel->r_addend, info, r_type))
8905 return false;
8906 }
8907
8908 if (h != NULL
8909 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8910 ELF_ST_IS_MIPS16 (h->other)))
8911 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
8912
8913 switch (r_type)
8914 {
8915 case R_MIPS_CALL16:
8916 case R_MIPS16_CALL16:
8917 case R_MICROMIPS_CALL16:
8918 if (h == NULL)
8919 {
8920 _bfd_error_handler
8921 /* xgettext:c-format */
8922 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8923 abfd, (uint64_t) rel->r_offset);
8924 bfd_set_error (bfd_error_bad_value);
8925 return false;
8926 }
8927 /* Fall through. */
8928
8929 case R_MIPS_CALL_HI16:
8930 case R_MIPS_CALL_LO16:
8931 case R_MICROMIPS_CALL_HI16:
8932 case R_MICROMIPS_CALL_LO16:
8933 if (h != NULL)
8934 {
8935 /* Make sure there is room in the regular GOT to hold the
8936 function's address. We may eliminate it in favour of
8937 a .got.plt entry later; see mips_elf_count_got_symbols. */
8938 if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
8939 r_type))
8940 return false;
8941
8942 /* We need a stub, not a plt entry for the undefined
8943 function. But we record it as if it needs plt. See
8944 _bfd_elf_adjust_dynamic_symbol. */
8945 h->needs_plt = 1;
8946 h->type = STT_FUNC;
8947 }
8948 break;
8949
8950 case R_MIPS_GOT_PAGE:
8951 case R_MICROMIPS_GOT_PAGE:
8952 case R_MIPS16_GOT16:
8953 case R_MIPS_GOT16:
8954 case R_MIPS_GOT_HI16:
8955 case R_MIPS_GOT_LO16:
8956 case R_MICROMIPS_GOT16:
8957 case R_MICROMIPS_GOT_HI16:
8958 case R_MICROMIPS_GOT_LO16:
8959 if (!h || got_page_reloc_p (r_type))
8960 {
8961 /* This relocation needs (or may need, if h != NULL) a
8962 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8963 know for sure until we know whether the symbol is
8964 preemptible. */
8965 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8966 {
8967 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8968 return false;
8969 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
8970 addend = mips_elf_read_rel_addend (abfd, sec, rel,
8971 howto, contents);
8972 if (got16_reloc_p (r_type))
8973 mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
8974 contents, &addend);
8975 else
8976 addend <<= howto->rightshift;
8977 }
8978 else
8979 addend = rel->r_addend;
8980 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8981 h, addend))
8982 return false;
8983
8984 if (h)
8985 {
8986 struct mips_elf_link_hash_entry *hmips =
8987 (struct mips_elf_link_hash_entry *) h;
8988
8989 /* This symbol is definitely not overridable. */
8990 if (hmips->root.def_regular
8991 && ! (bfd_link_pic (info) && ! info->symbolic
8992 && ! hmips->root.forced_local))
8993 h = NULL;
8994 }
8995 }
8996 /* If this is a global, overridable symbol, GOT_PAGE will
8997 decay to GOT_DISP, so we'll need a GOT entry for it. */
8998 /* Fall through. */
8999
9000 case R_MIPS_GOT_DISP:
9001 case R_MICROMIPS_GOT_DISP:
9002 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
9003 false, r_type))
9004 return false;
9005 break;
9006
9007 case R_MIPS_TLS_GOTTPREL:
9008 case R_MIPS16_TLS_GOTTPREL:
9009 case R_MICROMIPS_TLS_GOTTPREL:
9010 if (bfd_link_pic (info))
9011 info->flags |= DF_STATIC_TLS;
9012 /* Fall through */
9013
9014 case R_MIPS_TLS_LDM:
9015 case R_MIPS16_TLS_LDM:
9016 case R_MICROMIPS_TLS_LDM:
9017 if (tls_ldm_reloc_p (r_type))
9018 {
9019 r_symndx = STN_UNDEF;
9020 h = NULL;
9021 }
9022 /* Fall through */
9023
9024 case R_MIPS_TLS_GD:
9025 case R_MIPS16_TLS_GD:
9026 case R_MICROMIPS_TLS_GD:
9027 /* This symbol requires a global offset table entry, or two
9028 for TLS GD relocations. */
9029 if (h != NULL)
9030 {
9031 if (!mips_elf_record_global_got_symbol (h, abfd, info,
9032 false, r_type))
9033 return false;
9034 }
9035 else
9036 {
9037 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9038 rel->r_addend,
9039 info, r_type))
9040 return false;
9041 }
9042 break;
9043
9044 case R_MIPS_32:
9045 case R_MIPS_REL32:
9046 case R_MIPS_64:
9047 /* In VxWorks executables, references to external symbols
9048 are handled using copy relocs or PLT stubs, so there's
9049 no need to add a .rela.dyn entry for this relocation. */
9050 if (can_make_dynamic_p)
9051 {
9052 if (sreloc == NULL)
9053 {
9054 sreloc = mips_elf_rel_dyn_section (info, true);
9055 if (sreloc == NULL)
9056 return false;
9057 }
9058 if (bfd_link_pic (info) && h == NULL)
9059 {
9060 /* When creating a shared object, we must copy these
9061 reloc types into the output file as R_MIPS_REL32
9062 relocs. Make room for this reloc in .rel(a).dyn. */
9063 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9064 if (MIPS_ELF_READONLY_SECTION (sec))
9065 /* We tell the dynamic linker that there are
9066 relocations against the text segment. */
9067 info->flags |= DF_TEXTREL;
9068 }
9069 else
9070 {
9071 struct mips_elf_link_hash_entry *hmips;
9072
9073 /* For a shared object, we must copy this relocation
9074 unless the symbol turns out to be undefined and
9075 weak with non-default visibility, in which case
9076 it will be left as zero.
9077
9078 We could elide R_MIPS_REL32 for locally binding symbols
9079 in shared libraries, but do not yet do so.
9080
9081 For an executable, we only need to copy this
9082 reloc if the symbol is defined in a dynamic
9083 object. */
9084 hmips = (struct mips_elf_link_hash_entry *) h;
9085 ++hmips->possibly_dynamic_relocs;
9086 if (MIPS_ELF_READONLY_SECTION (sec))
9087 /* We need it to tell the dynamic linker if there
9088 are relocations against the text segment. */
9089 hmips->readonly_reloc = true;
9090 }
9091 }
9092
9093 if (SGI_COMPAT (abfd))
9094 mips_elf_hash_table (info)->compact_rel_size +=
9095 sizeof (Elf32_External_crinfo);
9096 break;
9097
9098 case R_MIPS_26:
9099 case R_MIPS_GPREL16:
9100 case R_MIPS_LITERAL:
9101 case R_MIPS_GPREL32:
9102 case R_MICROMIPS_26_S1:
9103 case R_MICROMIPS_GPREL16:
9104 case R_MICROMIPS_LITERAL:
9105 case R_MICROMIPS_GPREL7_S2:
9106 if (SGI_COMPAT (abfd))
9107 mips_elf_hash_table (info)->compact_rel_size +=
9108 sizeof (Elf32_External_crinfo);
9109 break;
9110
9111 /* This relocation describes the C++ object vtable hierarchy.
9112 Reconstruct it for later use during GC. */
9113 case R_MIPS_GNU_VTINHERIT:
9114 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9115 return false;
9116 break;
9117
9118 /* This relocation describes which C++ vtable entries are actually
9119 used. Record for later use during GC. */
9120 case R_MIPS_GNU_VTENTRY:
9121 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9122 return false;
9123 break;
9124
9125 default:
9126 break;
9127 }
9128
9129 /* Record the need for a PLT entry. At this point we don't know
9130 yet if we are going to create a PLT in the first place, but
9131 we only record whether the relocation requires a standard MIPS
9132 or a compressed code entry anyway. If we don't make a PLT after
9133 all, then we'll just ignore these arrangements. Likewise if
9134 a PLT entry is not created because the symbol is satisfied
9135 locally. */
9136 if (h != NULL
9137 && (branch_reloc_p (r_type)
9138 || mips16_branch_reloc_p (r_type)
9139 || micromips_branch_reloc_p (r_type))
9140 && !SYMBOL_CALLS_LOCAL (info, h))
9141 {
9142 if (h->plt.plist == NULL)
9143 h->plt.plist = mips_elf_make_plt_record (abfd);
9144 if (h->plt.plist == NULL)
9145 return false;
9146
9147 if (branch_reloc_p (r_type))
9148 h->plt.plist->need_mips = true;
9149 else
9150 h->plt.plist->need_comp = true;
9151 }
9152
9153 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9154 if there is one. We only need to handle global symbols here;
9155 we decide whether to keep or delete stubs for local symbols
9156 when processing the stub's relocations. */
9157 if (h != NULL
9158 && !mips16_call_reloc_p (r_type)
9159 && !section_allows_mips16_refs_p (sec))
9160 {
9161 struct mips_elf_link_hash_entry *mh;
9162
9163 mh = (struct mips_elf_link_hash_entry *) h;
9164 mh->need_fn_stub = true;
9165 }
9166
9167 /* Refuse some position-dependent relocations when creating a
9168 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9169 not PIC, but we can create dynamic relocations and the result
9170 will be fine. Also do not refuse R_MIPS_LO16, which can be
9171 combined with R_MIPS_GOT16. */
9172 if (bfd_link_pic (info))
9173 {
9174 switch (r_type)
9175 {
9176 case R_MIPS_TLS_TPREL_HI16:
9177 case R_MIPS16_TLS_TPREL_HI16:
9178 case R_MICROMIPS_TLS_TPREL_HI16:
9179 case R_MIPS_TLS_TPREL_LO16:
9180 case R_MIPS16_TLS_TPREL_LO16:
9181 case R_MICROMIPS_TLS_TPREL_LO16:
9182 /* These are okay in PIE, but not in a shared library. */
9183 if (bfd_link_executable (info))
9184 break;
9185
9186 /* FALLTHROUGH */
9187
9188 case R_MIPS16_HI16:
9189 case R_MIPS_HI16:
9190 case R_MIPS_HIGHER:
9191 case R_MIPS_HIGHEST:
9192 case R_MICROMIPS_HI16:
9193 case R_MICROMIPS_HIGHER:
9194 case R_MICROMIPS_HIGHEST:
9195 /* Don't refuse a high part relocation if it's against
9196 no symbol (e.g. part of a compound relocation). */
9197 if (r_symndx == STN_UNDEF)
9198 break;
9199
9200 /* Likewise an absolute symbol. */
9201 if (h != NULL && bfd_is_abs_symbol (&h->root))
9202 break;
9203
9204 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9205 and has a special meaning. */
9206 if (!NEWABI_P (abfd) && h != NULL
9207 && strcmp (h->root.root.string, "_gp_disp") == 0)
9208 break;
9209
9210 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9211 if (is_gott_symbol (info, h))
9212 break;
9213
9214 /* FALLTHROUGH */
9215
9216 case R_MIPS16_26:
9217 case R_MIPS_26:
9218 case R_MICROMIPS_26_S1:
9219 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9220 /* An error for unsupported relocations is raised as part
9221 of the above search, so we can skip the following. */
9222 if (howto != NULL)
9223 info->callbacks->einfo
9224 /* xgettext:c-format */
9225 (_("%X%H: relocation %s against `%s' cannot be used"
9226 " when making a shared object; recompile with -fPIC\n"),
9227 abfd, sec, rel->r_offset, howto->name,
9228 (h) ? h->root.root.string : "a local symbol");
9229 break;
9230 default:
9231 break;
9232 }
9233 }
9234 }
9235
9236 return true;
9237 }
9238 \f
9239 /* Allocate space for global sym dynamic relocs. */
9240
9241 static bool
9242 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9243 {
9244 struct bfd_link_info *info = inf;
9245 bfd *dynobj;
9246 struct mips_elf_link_hash_entry *hmips;
9247 struct mips_elf_link_hash_table *htab;
9248
9249 htab = mips_elf_hash_table (info);
9250 BFD_ASSERT (htab != NULL);
9251
9252 dynobj = elf_hash_table (info)->dynobj;
9253 hmips = (struct mips_elf_link_hash_entry *) h;
9254
9255 /* VxWorks executables are handled elsewhere; we only need to
9256 allocate relocations in shared objects. */
9257 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9258 return true;
9259
9260 /* Ignore indirect symbols. All relocations against such symbols
9261 will be redirected to the target symbol. */
9262 if (h->root.type == bfd_link_hash_indirect)
9263 return true;
9264
9265 /* If this symbol is defined in a dynamic object, or we are creating
9266 a shared library, we will need to copy any R_MIPS_32 or
9267 R_MIPS_REL32 relocs against it into the output file. */
9268 if (! bfd_link_relocatable (info)
9269 && hmips->possibly_dynamic_relocs != 0
9270 && (h->root.type == bfd_link_hash_defweak
9271 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9272 || bfd_link_pic (info)))
9273 {
9274 bool do_copy = true;
9275
9276 if (h->root.type == bfd_link_hash_undefweak)
9277 {
9278 /* Do not copy relocations for undefined weak symbols that
9279 we are not going to export. */
9280 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9281 do_copy = false;
9282
9283 /* Make sure undefined weak symbols are output as a dynamic
9284 symbol in PIEs. */
9285 else if (h->dynindx == -1 && !h->forced_local)
9286 {
9287 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9288 return false;
9289 }
9290 }
9291
9292 if (do_copy)
9293 {
9294 /* Even though we don't directly need a GOT entry for this symbol,
9295 the SVR4 psABI requires it to have a dynamic symbol table
9296 index greater that DT_MIPS_GOTSYM if there are dynamic
9297 relocations against it.
9298
9299 VxWorks does not enforce the same mapping between the GOT
9300 and the symbol table, so the same requirement does not
9301 apply there. */
9302 if (htab->root.target_os != is_vxworks)
9303 {
9304 if (hmips->global_got_area > GGA_RELOC_ONLY)
9305 hmips->global_got_area = GGA_RELOC_ONLY;
9306 hmips->got_only_for_calls = false;
9307 }
9308
9309 mips_elf_allocate_dynamic_relocations
9310 (dynobj, info, hmips->possibly_dynamic_relocs);
9311 if (hmips->readonly_reloc)
9312 /* We tell the dynamic linker that there are relocations
9313 against the text segment. */
9314 info->flags |= DF_TEXTREL;
9315 }
9316 }
9317
9318 return true;
9319 }
9320
9321 /* Adjust a symbol defined by a dynamic object and referenced by a
9322 regular object. The current definition is in some section of the
9323 dynamic object, but we're not including those sections. We have to
9324 change the definition to something the rest of the link can
9325 understand. */
9326
9327 bool
9328 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9329 struct elf_link_hash_entry *h)
9330 {
9331 bfd *dynobj;
9332 struct mips_elf_link_hash_entry *hmips;
9333 struct mips_elf_link_hash_table *htab;
9334 asection *s, *srel;
9335
9336 htab = mips_elf_hash_table (info);
9337 BFD_ASSERT (htab != NULL);
9338
9339 dynobj = elf_hash_table (info)->dynobj;
9340 hmips = (struct mips_elf_link_hash_entry *) h;
9341
9342 /* Make sure we know what is going on here. */
9343 if (dynobj == NULL
9344 || (! h->needs_plt
9345 && ! h->is_weakalias
9346 && (! h->def_dynamic
9347 || ! h->ref_regular
9348 || h->def_regular)))
9349 {
9350 if (h->type == STT_GNU_IFUNC)
9351 _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9352 h->root.root.string);
9353 else
9354 _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9355 h->root.root.string);
9356 return true;
9357 }
9358
9359 hmips = (struct mips_elf_link_hash_entry *) h;
9360
9361 /* If there are call relocations against an externally-defined symbol,
9362 see whether we can create a MIPS lazy-binding stub for it. We can
9363 only do this if all references to the function are through call
9364 relocations, and in that case, the traditional lazy-binding stubs
9365 are much more efficient than PLT entries.
9366
9367 Traditional stubs are only available on SVR4 psABI-based systems;
9368 VxWorks always uses PLTs instead. */
9369 if (htab->root.target_os != is_vxworks
9370 && h->needs_plt
9371 && !hmips->no_fn_stub)
9372 {
9373 if (! elf_hash_table (info)->dynamic_sections_created)
9374 return true;
9375
9376 /* If this symbol is not defined in a regular file, then set
9377 the symbol to the stub location. This is required to make
9378 function pointers compare as equal between the normal
9379 executable and the shared library. */
9380 if (!h->def_regular
9381 && !bfd_is_abs_section (htab->sstubs->output_section))
9382 {
9383 hmips->needs_lazy_stub = true;
9384 htab->lazy_stub_count++;
9385 return true;
9386 }
9387 }
9388 /* As above, VxWorks requires PLT entries for externally-defined
9389 functions that are only accessed through call relocations.
9390
9391 Both VxWorks and non-VxWorks targets also need PLT entries if there
9392 are static-only relocations against an externally-defined function.
9393 This can technically occur for shared libraries if there are
9394 branches to the symbol, although it is unlikely that this will be
9395 used in practice due to the short ranges involved. It can occur
9396 for any relative or absolute relocation in executables; in that
9397 case, the PLT entry becomes the function's canonical address. */
9398 else if (((h->needs_plt && !hmips->no_fn_stub)
9399 || (h->type == STT_FUNC && hmips->has_static_relocs))
9400 && htab->use_plts_and_copy_relocs
9401 && !SYMBOL_CALLS_LOCAL (info, h)
9402 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9403 && h->root.type == bfd_link_hash_undefweak))
9404 {
9405 bool micromips_p = MICROMIPS_P (info->output_bfd);
9406 bool newabi_p = NEWABI_P (info->output_bfd);
9407
9408 /* If this is the first symbol to need a PLT entry, then make some
9409 basic setup. Also work out PLT entry sizes. We'll need them
9410 for PLT offset calculations. */
9411 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9412 {
9413 BFD_ASSERT (htab->root.sgotplt->size == 0);
9414 BFD_ASSERT (htab->plt_got_index == 0);
9415
9416 /* If we're using the PLT additions to the psABI, each PLT
9417 entry is 16 bytes and the PLT0 entry is 32 bytes.
9418 Encourage better cache usage by aligning. We do this
9419 lazily to avoid pessimizing traditional objects. */
9420 if (htab->root.target_os != is_vxworks
9421 && !bfd_set_section_alignment (htab->root.splt, 5))
9422 return false;
9423
9424 /* Make sure that .got.plt is word-aligned. We do this lazily
9425 for the same reason as above. */
9426 if (!bfd_set_section_alignment (htab->root.sgotplt,
9427 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9428 return false;
9429
9430 /* On non-VxWorks targets, the first two entries in .got.plt
9431 are reserved. */
9432 if (htab->root.target_os != is_vxworks)
9433 htab->plt_got_index
9434 += (get_elf_backend_data (dynobj)->got_header_size
9435 / MIPS_ELF_GOT_SIZE (dynobj));
9436
9437 /* On VxWorks, also allocate room for the header's
9438 .rela.plt.unloaded entries. */
9439 if (htab->root.target_os == is_vxworks
9440 && !bfd_link_pic (info))
9441 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9442
9443 /* Now work out the sizes of individual PLT entries. */
9444 if (htab->root.target_os == is_vxworks
9445 && bfd_link_pic (info))
9446 htab->plt_mips_entry_size
9447 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9448 else if (htab->root.target_os == is_vxworks)
9449 htab->plt_mips_entry_size
9450 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9451 else if (newabi_p)
9452 htab->plt_mips_entry_size
9453 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9454 else if (!micromips_p)
9455 {
9456 htab->plt_mips_entry_size
9457 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9458 htab->plt_comp_entry_size
9459 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9460 }
9461 else if (htab->insn32)
9462 {
9463 htab->plt_mips_entry_size
9464 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9465 htab->plt_comp_entry_size
9466 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9467 }
9468 else
9469 {
9470 htab->plt_mips_entry_size
9471 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9472 htab->plt_comp_entry_size
9473 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9474 }
9475 }
9476
9477 if (h->plt.plist == NULL)
9478 h->plt.plist = mips_elf_make_plt_record (dynobj);
9479 if (h->plt.plist == NULL)
9480 return false;
9481
9482 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9483 n32 or n64, so always use a standard entry there.
9484
9485 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9486 all MIPS16 calls will go via that stub, and there is no benefit
9487 to having a MIPS16 entry. And in the case of call_stub a
9488 standard entry actually has to be used as the stub ends with a J
9489 instruction. */
9490 if (newabi_p
9491 || htab->root.target_os == is_vxworks
9492 || hmips->call_stub
9493 || hmips->call_fp_stub)
9494 {
9495 h->plt.plist->need_mips = true;
9496 h->plt.plist->need_comp = false;
9497 }
9498
9499 /* Otherwise, if there are no direct calls to the function, we
9500 have a free choice of whether to use standard or compressed
9501 entries. Prefer microMIPS entries if the object is known to
9502 contain microMIPS code, so that it becomes possible to create
9503 pure microMIPS binaries. Prefer standard entries otherwise,
9504 because MIPS16 ones are no smaller and are usually slower. */
9505 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9506 {
9507 if (micromips_p)
9508 h->plt.plist->need_comp = true;
9509 else
9510 h->plt.plist->need_mips = true;
9511 }
9512
9513 if (h->plt.plist->need_mips)
9514 {
9515 h->plt.plist->mips_offset = htab->plt_mips_offset;
9516 htab->plt_mips_offset += htab->plt_mips_entry_size;
9517 }
9518 if (h->plt.plist->need_comp)
9519 {
9520 h->plt.plist->comp_offset = htab->plt_comp_offset;
9521 htab->plt_comp_offset += htab->plt_comp_entry_size;
9522 }
9523
9524 /* Reserve the corresponding .got.plt entry now too. */
9525 h->plt.plist->gotplt_index = htab->plt_got_index++;
9526
9527 /* If the output file has no definition of the symbol, set the
9528 symbol's value to the address of the stub. */
9529 if (!bfd_link_pic (info) && !h->def_regular)
9530 hmips->use_plt_entry = true;
9531
9532 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
9533 htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9534 ? MIPS_ELF_RELA_SIZE (dynobj)
9535 : MIPS_ELF_REL_SIZE (dynobj));
9536
9537 /* Make room for the .rela.plt.unloaded relocations. */
9538 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9539 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9540
9541 /* All relocations against this symbol that could have been made
9542 dynamic will now refer to the PLT entry instead. */
9543 hmips->possibly_dynamic_relocs = 0;
9544
9545 return true;
9546 }
9547
9548 /* If this is a weak symbol, and there is a real definition, the
9549 processor independent code will have arranged for us to see the
9550 real definition first, and we can just use the same value. */
9551 if (h->is_weakalias)
9552 {
9553 struct elf_link_hash_entry *def = weakdef (h);
9554 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9555 h->root.u.def.section = def->root.u.def.section;
9556 h->root.u.def.value = def->root.u.def.value;
9557 return true;
9558 }
9559
9560 /* Otherwise, there is nothing further to do for symbols defined
9561 in regular objects. */
9562 if (h->def_regular)
9563 return true;
9564
9565 /* There's also nothing more to do if we'll convert all relocations
9566 against this symbol into dynamic relocations. */
9567 if (!hmips->has_static_relocs)
9568 return true;
9569
9570 /* We're now relying on copy relocations. Complain if we have
9571 some that we can't convert. */
9572 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9573 {
9574 _bfd_error_handler (_("non-dynamic relocations refer to "
9575 "dynamic symbol %s"),
9576 h->root.root.string);
9577 bfd_set_error (bfd_error_bad_value);
9578 return false;
9579 }
9580
9581 /* We must allocate the symbol in our .dynbss section, which will
9582 become part of the .bss section of the executable. There will be
9583 an entry for this symbol in the .dynsym section. The dynamic
9584 object will contain position independent code, so all references
9585 from the dynamic object to this symbol will go through the global
9586 offset table. The dynamic linker will use the .dynsym entry to
9587 determine the address it must put in the global offset table, so
9588 both the dynamic object and the regular object will refer to the
9589 same memory location for the variable. */
9590
9591 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9592 {
9593 s = htab->root.sdynrelro;
9594 srel = htab->root.sreldynrelro;
9595 }
9596 else
9597 {
9598 s = htab->root.sdynbss;
9599 srel = htab->root.srelbss;
9600 }
9601 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9602 {
9603 if (htab->root.target_os == is_vxworks)
9604 srel->size += sizeof (Elf32_External_Rela);
9605 else
9606 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9607 h->needs_copy = 1;
9608 }
9609
9610 /* All relocations against this symbol that could have been made
9611 dynamic will now refer to the local copy instead. */
9612 hmips->possibly_dynamic_relocs = 0;
9613
9614 return _bfd_elf_adjust_dynamic_copy (info, h, s);
9615 }
9616 \f
9617 /* This function is called after all the input files have been read,
9618 and the input sections have been assigned to output sections. We
9619 check for any mips16 stub sections that we can discard. */
9620
9621 bool
9622 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9623 struct bfd_link_info *info)
9624 {
9625 asection *sect;
9626 struct mips_elf_link_hash_table *htab;
9627 struct mips_htab_traverse_info hti;
9628
9629 htab = mips_elf_hash_table (info);
9630 BFD_ASSERT (htab != NULL);
9631
9632 /* The .reginfo section has a fixed size. */
9633 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9634 if (sect != NULL)
9635 {
9636 bfd_set_section_size (sect, sizeof (Elf32_External_RegInfo));
9637 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9638 }
9639
9640 /* The .MIPS.abiflags section has a fixed size. */
9641 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9642 if (sect != NULL)
9643 {
9644 bfd_set_section_size (sect, sizeof (Elf_External_ABIFlags_v0));
9645 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9646 }
9647
9648 hti.info = info;
9649 hti.output_bfd = output_bfd;
9650 hti.error = false;
9651 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9652 mips_elf_check_symbols, &hti);
9653 if (hti.error)
9654 return false;
9655
9656 return true;
9657 }
9658
9659 /* If the link uses a GOT, lay it out and work out its size. */
9660
9661 static bool
9662 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9663 {
9664 bfd *dynobj;
9665 asection *s;
9666 struct mips_got_info *g;
9667 bfd_size_type loadable_size = 0;
9668 bfd_size_type page_gotno;
9669 bfd *ibfd;
9670 struct mips_elf_traverse_got_arg tga;
9671 struct mips_elf_link_hash_table *htab;
9672
9673 htab = mips_elf_hash_table (info);
9674 BFD_ASSERT (htab != NULL);
9675
9676 s = htab->root.sgot;
9677 if (s == NULL)
9678 return true;
9679
9680 dynobj = elf_hash_table (info)->dynobj;
9681 g = htab->got_info;
9682
9683 /* Allocate room for the reserved entries. VxWorks always reserves
9684 3 entries; other objects only reserve 2 entries. */
9685 BFD_ASSERT (g->assigned_low_gotno == 0);
9686 if (htab->root.target_os == is_vxworks)
9687 htab->reserved_gotno = 3;
9688 else
9689 htab->reserved_gotno = 2;
9690 g->local_gotno += htab->reserved_gotno;
9691 g->assigned_low_gotno = htab->reserved_gotno;
9692
9693 /* Decide which symbols need to go in the global part of the GOT and
9694 count the number of reloc-only GOT symbols. */
9695 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9696
9697 if (!mips_elf_resolve_final_got_entries (info, g))
9698 return false;
9699
9700 /* Calculate the total loadable size of the output. That
9701 will give us the maximum number of GOT_PAGE entries
9702 required. */
9703 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9704 {
9705 asection *subsection;
9706
9707 for (subsection = ibfd->sections;
9708 subsection;
9709 subsection = subsection->next)
9710 {
9711 if ((subsection->flags & SEC_ALLOC) == 0)
9712 continue;
9713 loadable_size += ((subsection->size + 0xf)
9714 &~ (bfd_size_type) 0xf);
9715 }
9716 }
9717
9718 if (htab->root.target_os == is_vxworks)
9719 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9720 relocations against local symbols evaluate to "G", and the EABI does
9721 not include R_MIPS_GOT_PAGE. */
9722 page_gotno = 0;
9723 else
9724 /* Assume there are two loadable segments consisting of contiguous
9725 sections. Is 5 enough? */
9726 page_gotno = (loadable_size >> 16) + 5;
9727
9728 /* Choose the smaller of the two page estimates; both are intended to be
9729 conservative. */
9730 if (page_gotno > g->page_gotno)
9731 page_gotno = g->page_gotno;
9732
9733 g->local_gotno += page_gotno;
9734 g->assigned_high_gotno = g->local_gotno - 1;
9735
9736 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9737 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9738 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9739
9740 /* VxWorks does not support multiple GOTs. It initializes $gp to
9741 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9742 dynamic loader. */
9743 if (htab->root.target_os != is_vxworks
9744 && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9745 {
9746 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9747 return false;
9748 }
9749 else
9750 {
9751 /* Record that all bfds use G. This also has the effect of freeing
9752 the per-bfd GOTs, which we no longer need. */
9753 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9754 if (mips_elf_bfd_got (ibfd, false))
9755 mips_elf_replace_bfd_got (ibfd, g);
9756 mips_elf_replace_bfd_got (output_bfd, g);
9757
9758 /* Set up TLS entries. */
9759 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9760 tga.info = info;
9761 tga.g = g;
9762 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9763 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9764 if (!tga.g)
9765 return false;
9766 BFD_ASSERT (g->tls_assigned_gotno
9767 == g->global_gotno + g->local_gotno + g->tls_gotno);
9768
9769 /* Each VxWorks GOT entry needs an explicit relocation. */
9770 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9771 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9772
9773 /* Allocate room for the TLS relocations. */
9774 if (g->relocs)
9775 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9776 }
9777
9778 return true;
9779 }
9780
9781 /* Estimate the size of the .MIPS.stubs section. */
9782
9783 static void
9784 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9785 {
9786 struct mips_elf_link_hash_table *htab;
9787 bfd_size_type dynsymcount;
9788
9789 htab = mips_elf_hash_table (info);
9790 BFD_ASSERT (htab != NULL);
9791
9792 if (htab->lazy_stub_count == 0)
9793 return;
9794
9795 /* IRIX rld assumes that a function stub isn't at the end of the .text
9796 section, so add a dummy entry to the end. */
9797 htab->lazy_stub_count++;
9798
9799 /* Get a worst-case estimate of the number of dynamic symbols needed.
9800 At this point, dynsymcount does not account for section symbols
9801 and count_section_dynsyms may overestimate the number that will
9802 be needed. */
9803 dynsymcount = (elf_hash_table (info)->dynsymcount
9804 + count_section_dynsyms (output_bfd, info));
9805
9806 /* Determine the size of one stub entry. There's no disadvantage
9807 from using microMIPS code here, so for the sake of pure-microMIPS
9808 binaries we prefer it whenever there's any microMIPS code in
9809 output produced at all. This has a benefit of stubs being
9810 shorter by 4 bytes each too, unless in the insn32 mode. */
9811 if (!MICROMIPS_P (output_bfd))
9812 htab->function_stub_size = (dynsymcount > 0x10000
9813 ? MIPS_FUNCTION_STUB_BIG_SIZE
9814 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9815 else if (htab->insn32)
9816 htab->function_stub_size = (dynsymcount > 0x10000
9817 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9818 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9819 else
9820 htab->function_stub_size = (dynsymcount > 0x10000
9821 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9822 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9823
9824 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9825 }
9826
9827 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9828 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9829 stub, allocate an entry in the stubs section. */
9830
9831 static bool
9832 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9833 {
9834 struct mips_htab_traverse_info *hti = data;
9835 struct mips_elf_link_hash_table *htab;
9836 struct bfd_link_info *info;
9837 bfd *output_bfd;
9838
9839 info = hti->info;
9840 output_bfd = hti->output_bfd;
9841 htab = mips_elf_hash_table (info);
9842 BFD_ASSERT (htab != NULL);
9843
9844 if (h->needs_lazy_stub)
9845 {
9846 bool micromips_p = MICROMIPS_P (output_bfd);
9847 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9848 bfd_vma isa_bit = micromips_p;
9849
9850 BFD_ASSERT (htab->root.dynobj != NULL);
9851 if (h->root.plt.plist == NULL)
9852 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9853 if (h->root.plt.plist == NULL)
9854 {
9855 hti->error = true;
9856 return false;
9857 }
9858 h->root.root.u.def.section = htab->sstubs;
9859 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9860 h->root.plt.plist->stub_offset = htab->sstubs->size;
9861 h->root.other = other;
9862 htab->sstubs->size += htab->function_stub_size;
9863 }
9864 return true;
9865 }
9866
9867 /* Allocate offsets in the stubs section to each symbol that needs one.
9868 Set the final size of the .MIPS.stub section. */
9869
9870 static bool
9871 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9872 {
9873 bfd *output_bfd = info->output_bfd;
9874 bool micromips_p = MICROMIPS_P (output_bfd);
9875 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9876 bfd_vma isa_bit = micromips_p;
9877 struct mips_elf_link_hash_table *htab;
9878 struct mips_htab_traverse_info hti;
9879 struct elf_link_hash_entry *h;
9880 bfd *dynobj;
9881
9882 htab = mips_elf_hash_table (info);
9883 BFD_ASSERT (htab != NULL);
9884
9885 if (htab->lazy_stub_count == 0)
9886 return true;
9887
9888 htab->sstubs->size = 0;
9889 hti.info = info;
9890 hti.output_bfd = output_bfd;
9891 hti.error = false;
9892 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9893 if (hti.error)
9894 return false;
9895 htab->sstubs->size += htab->function_stub_size;
9896 BFD_ASSERT (htab->sstubs->size
9897 == htab->lazy_stub_count * htab->function_stub_size);
9898
9899 dynobj = elf_hash_table (info)->dynobj;
9900 BFD_ASSERT (dynobj != NULL);
9901 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9902 if (h == NULL)
9903 return false;
9904 h->root.u.def.value = isa_bit;
9905 h->other = other;
9906 h->type = STT_FUNC;
9907
9908 return true;
9909 }
9910
9911 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9912 bfd_link_info. If H uses the address of a PLT entry as the value
9913 of the symbol, then set the entry in the symbol table now. Prefer
9914 a standard MIPS PLT entry. */
9915
9916 static bool
9917 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9918 {
9919 struct bfd_link_info *info = data;
9920 bool micromips_p = MICROMIPS_P (info->output_bfd);
9921 struct mips_elf_link_hash_table *htab;
9922 unsigned int other;
9923 bfd_vma isa_bit;
9924 bfd_vma val;
9925
9926 htab = mips_elf_hash_table (info);
9927 BFD_ASSERT (htab != NULL);
9928
9929 if (h->use_plt_entry)
9930 {
9931 BFD_ASSERT (h->root.plt.plist != NULL);
9932 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9933 || h->root.plt.plist->comp_offset != MINUS_ONE);
9934
9935 val = htab->plt_header_size;
9936 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9937 {
9938 isa_bit = 0;
9939 val += h->root.plt.plist->mips_offset;
9940 other = 0;
9941 }
9942 else
9943 {
9944 isa_bit = 1;
9945 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9946 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9947 }
9948 val += isa_bit;
9949 /* For VxWorks, point at the PLT load stub rather than the lazy
9950 resolution stub; this stub will become the canonical function
9951 address. */
9952 if (htab->root.target_os == is_vxworks)
9953 val += 8;
9954
9955 h->root.root.u.def.section = htab->root.splt;
9956 h->root.root.u.def.value = val;
9957 h->root.other = other;
9958 }
9959
9960 return true;
9961 }
9962
9963 /* Set the sizes of the dynamic sections. */
9964
9965 bool
9966 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9967 struct bfd_link_info *info)
9968 {
9969 bfd *dynobj;
9970 asection *s, *sreldyn;
9971 bool reltext;
9972 struct mips_elf_link_hash_table *htab;
9973
9974 htab = mips_elf_hash_table (info);
9975 BFD_ASSERT (htab != NULL);
9976 dynobj = elf_hash_table (info)->dynobj;
9977 BFD_ASSERT (dynobj != NULL);
9978
9979 if (elf_hash_table (info)->dynamic_sections_created)
9980 {
9981 /* Set the contents of the .interp section to the interpreter. */
9982 if (bfd_link_executable (info) && !info->nointerp)
9983 {
9984 s = bfd_get_linker_section (dynobj, ".interp");
9985 BFD_ASSERT (s != NULL);
9986 s->size
9987 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9988 s->contents
9989 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9990 }
9991
9992 /* Figure out the size of the PLT header if we know that we
9993 are using it. For the sake of cache alignment always use
9994 a standard header whenever any standard entries are present
9995 even if microMIPS entries are present as well. This also
9996 lets the microMIPS header rely on the value of $v0 only set
9997 by microMIPS entries, for a small size reduction.
9998
9999 Set symbol table entry values for symbols that use the
10000 address of their PLT entry now that we can calculate it.
10001
10002 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10003 haven't already in _bfd_elf_create_dynamic_sections. */
10004 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
10005 {
10006 bool micromips_p = (MICROMIPS_P (output_bfd)
10007 && !htab->plt_mips_offset);
10008 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10009 bfd_vma isa_bit = micromips_p;
10010 struct elf_link_hash_entry *h;
10011 bfd_vma size;
10012
10013 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10014 BFD_ASSERT (htab->root.sgotplt->size == 0);
10015 BFD_ASSERT (htab->root.splt->size == 0);
10016
10017 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
10018 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
10019 else if (htab->root.target_os == is_vxworks)
10020 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10021 else if (ABI_64_P (output_bfd))
10022 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10023 else if (ABI_N32_P (output_bfd))
10024 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10025 else if (!micromips_p)
10026 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
10027 else if (htab->insn32)
10028 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
10029 else
10030 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
10031
10032 htab->plt_header_is_comp = micromips_p;
10033 htab->plt_header_size = size;
10034 htab->root.splt->size = (size
10035 + htab->plt_mips_offset
10036 + htab->plt_comp_offset);
10037 htab->root.sgotplt->size = (htab->plt_got_index
10038 * MIPS_ELF_GOT_SIZE (dynobj));
10039
10040 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10041
10042 if (htab->root.hplt == NULL)
10043 {
10044 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
10045 "_PROCEDURE_LINKAGE_TABLE_");
10046 htab->root.hplt = h;
10047 if (h == NULL)
10048 return false;
10049 }
10050
10051 h = htab->root.hplt;
10052 h->root.u.def.value = isa_bit;
10053 h->other = other;
10054 h->type = STT_FUNC;
10055 }
10056 }
10057
10058 /* Allocate space for global sym dynamic relocs. */
10059 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10060
10061 mips_elf_estimate_stub_size (output_bfd, info);
10062
10063 if (!mips_elf_lay_out_got (output_bfd, info))
10064 return false;
10065
10066 mips_elf_lay_out_lazy_stubs (info);
10067
10068 /* The check_relocs and adjust_dynamic_symbol entry points have
10069 determined the sizes of the various dynamic sections. Allocate
10070 memory for them. */
10071 reltext = false;
10072 for (s = dynobj->sections; s != NULL; s = s->next)
10073 {
10074 const char *name;
10075
10076 /* It's OK to base decisions on the section name, because none
10077 of the dynobj section names depend upon the input files. */
10078 name = bfd_section_name (s);
10079
10080 if ((s->flags & SEC_LINKER_CREATED) == 0)
10081 continue;
10082
10083 if (startswith (name, ".rel"))
10084 {
10085 if (s->size != 0)
10086 {
10087 const char *outname;
10088 asection *target;
10089
10090 /* If this relocation section applies to a read only
10091 section, then we probably need a DT_TEXTREL entry.
10092 If the relocation section is .rel(a).dyn, we always
10093 assert a DT_TEXTREL entry rather than testing whether
10094 there exists a relocation to a read only section or
10095 not. */
10096 outname = bfd_section_name (s->output_section);
10097 target = bfd_get_section_by_name (output_bfd, outname + 4);
10098 if ((target != NULL
10099 && (target->flags & SEC_READONLY) != 0
10100 && (target->flags & SEC_ALLOC) != 0)
10101 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10102 reltext = true;
10103
10104 /* We use the reloc_count field as a counter if we need
10105 to copy relocs into the output file. */
10106 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10107 s->reloc_count = 0;
10108
10109 /* If combreloc is enabled, elf_link_sort_relocs() will
10110 sort relocations, but in a different way than we do,
10111 and before we're done creating relocations. Also, it
10112 will move them around between input sections'
10113 relocation's contents, so our sorting would be
10114 broken, so don't let it run. */
10115 info->combreloc = 0;
10116 }
10117 }
10118 else if (bfd_link_executable (info)
10119 && ! mips_elf_hash_table (info)->use_rld_obj_head
10120 && startswith (name, ".rld_map"))
10121 {
10122 /* We add a room for __rld_map. It will be filled in by the
10123 rtld to contain a pointer to the _r_debug structure. */
10124 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10125 }
10126 else if (SGI_COMPAT (output_bfd)
10127 && startswith (name, ".compact_rel"))
10128 s->size += mips_elf_hash_table (info)->compact_rel_size;
10129 else if (s == htab->root.splt)
10130 {
10131 /* If the last PLT entry has a branch delay slot, allocate
10132 room for an extra nop to fill the delay slot. This is
10133 for CPUs without load interlocking. */
10134 if (! LOAD_INTERLOCKS_P (output_bfd)
10135 && htab->root.target_os != is_vxworks
10136 && s->size > 0)
10137 s->size += 4;
10138 }
10139 else if (! startswith (name, ".init")
10140 && s != htab->root.sgot
10141 && s != htab->root.sgotplt
10142 && s != htab->sstubs
10143 && s != htab->root.sdynbss
10144 && s != htab->root.sdynrelro)
10145 {
10146 /* It's not one of our sections, so don't allocate space. */
10147 continue;
10148 }
10149
10150 if (s->size == 0)
10151 {
10152 s->flags |= SEC_EXCLUDE;
10153 continue;
10154 }
10155
10156 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10157 continue;
10158
10159 /* Allocate memory for the section contents. */
10160 s->contents = bfd_zalloc (dynobj, s->size);
10161 if (s->contents == NULL)
10162 {
10163 bfd_set_error (bfd_error_no_memory);
10164 return false;
10165 }
10166 }
10167
10168 if (elf_hash_table (info)->dynamic_sections_created)
10169 {
10170 /* Add some entries to the .dynamic section. We fill in the
10171 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10172 must add the entries now so that we get the correct size for
10173 the .dynamic section. */
10174
10175 /* SGI object has the equivalence of DT_DEBUG in the
10176 DT_MIPS_RLD_MAP entry. This must come first because glibc
10177 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10178 may only look at the first one they see. */
10179 if (!bfd_link_pic (info)
10180 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10181 return false;
10182
10183 if (bfd_link_executable (info)
10184 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10185 return false;
10186
10187 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10188 used by the debugger. */
10189 if (bfd_link_executable (info)
10190 && !SGI_COMPAT (output_bfd)
10191 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10192 return false;
10193
10194 if (reltext
10195 && (SGI_COMPAT (output_bfd)
10196 || htab->root.target_os == is_vxworks))
10197 info->flags |= DF_TEXTREL;
10198
10199 if ((info->flags & DF_TEXTREL) != 0)
10200 {
10201 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10202 return false;
10203
10204 /* Clear the DF_TEXTREL flag. It will be set again if we
10205 write out an actual text relocation; we may not, because
10206 at this point we do not know whether e.g. any .eh_frame
10207 absolute relocations have been converted to PC-relative. */
10208 info->flags &= ~DF_TEXTREL;
10209 }
10210
10211 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10212 return false;
10213
10214 sreldyn = mips_elf_rel_dyn_section (info, false);
10215 if (htab->root.target_os == is_vxworks)
10216 {
10217 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10218 use any of the DT_MIPS_* tags. */
10219 if (sreldyn && sreldyn->size > 0)
10220 {
10221 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10222 return false;
10223
10224 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10225 return false;
10226
10227 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10228 return false;
10229 }
10230 }
10231 else
10232 {
10233 if (sreldyn && sreldyn->size > 0
10234 && !bfd_is_abs_section (sreldyn->output_section))
10235 {
10236 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10237 return false;
10238
10239 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10240 return false;
10241
10242 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10243 return false;
10244 }
10245
10246 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10247 return false;
10248
10249 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10250 return false;
10251
10252 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10253 return false;
10254
10255 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10256 return false;
10257
10258 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10259 return false;
10260
10261 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10262 return false;
10263
10264 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10265 return false;
10266
10267 if (info->emit_gnu_hash
10268 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10269 return false;
10270
10271 if (IRIX_COMPAT (dynobj) == ict_irix5
10272 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10273 return false;
10274
10275 if (IRIX_COMPAT (dynobj) == ict_irix6
10276 && (bfd_get_section_by_name
10277 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10278 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10279 return false;
10280 }
10281 if (htab->root.splt->size > 0)
10282 {
10283 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10284 return false;
10285
10286 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10287 return false;
10288
10289 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10290 return false;
10291
10292 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10293 return false;
10294 }
10295 if (htab->root.target_os == is_vxworks
10296 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10297 return false;
10298 }
10299
10300 return true;
10301 }
10302 \f
10303 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10304 Adjust its R_ADDEND field so that it is correct for the output file.
10305 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10306 and sections respectively; both use symbol indexes. */
10307
10308 static void
10309 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10310 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10311 asection **local_sections, Elf_Internal_Rela *rel)
10312 {
10313 unsigned int r_type, r_symndx;
10314 Elf_Internal_Sym *sym;
10315 asection *sec;
10316
10317 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10318 {
10319 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10320 if (gprel16_reloc_p (r_type)
10321 || r_type == R_MIPS_GPREL32
10322 || literal_reloc_p (r_type))
10323 {
10324 rel->r_addend += _bfd_get_gp_value (input_bfd);
10325 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10326 }
10327
10328 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10329 sym = local_syms + r_symndx;
10330
10331 /* Adjust REL's addend to account for section merging. */
10332 if (!bfd_link_relocatable (info))
10333 {
10334 sec = local_sections[r_symndx];
10335 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10336 }
10337
10338 /* This would normally be done by the rela_normal code in elflink.c. */
10339 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10340 rel->r_addend += local_sections[r_symndx]->output_offset;
10341 }
10342 }
10343
10344 /* Handle relocations against symbols from removed linkonce sections,
10345 or sections discarded by a linker script. We use this wrapper around
10346 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10347 on 64-bit ELF targets. In this case for any relocation handled, which
10348 always be the first in a triplet, the remaining two have to be processed
10349 together with the first, even if they are R_MIPS_NONE. It is the symbol
10350 index referred by the first reloc that applies to all the three and the
10351 remaining two never refer to an object symbol. And it is the final
10352 relocation (the last non-null one) that determines the output field of
10353 the whole relocation so retrieve the corresponding howto structure for
10354 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10355
10356 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10357 and therefore requires to be pasted in a loop. It also defines a block
10358 and does not protect any of its arguments, hence the extra brackets. */
10359
10360 static void
10361 mips_reloc_against_discarded_section (bfd *output_bfd,
10362 struct bfd_link_info *info,
10363 bfd *input_bfd, asection *input_section,
10364 Elf_Internal_Rela **rel,
10365 const Elf_Internal_Rela **relend,
10366 bool rel_reloc,
10367 reloc_howto_type *howto,
10368 bfd_byte *contents)
10369 {
10370 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10371 int count = bed->s->int_rels_per_ext_rel;
10372 unsigned int r_type;
10373 int i;
10374
10375 for (i = count - 1; i > 0; i--)
10376 {
10377 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10378 if (r_type != R_MIPS_NONE)
10379 {
10380 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10381 break;
10382 }
10383 }
10384 do
10385 {
10386 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10387 (*rel), count, (*relend),
10388 howto, i, contents);
10389 }
10390 while (0);
10391 }
10392
10393 /* Relocate a MIPS ELF section. */
10394
10395 int
10396 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10397 bfd *input_bfd, asection *input_section,
10398 bfd_byte *contents, Elf_Internal_Rela *relocs,
10399 Elf_Internal_Sym *local_syms,
10400 asection **local_sections)
10401 {
10402 Elf_Internal_Rela *rel;
10403 const Elf_Internal_Rela *relend;
10404 bfd_vma addend = 0;
10405 bool use_saved_addend_p = false;
10406
10407 relend = relocs + input_section->reloc_count;
10408 for (rel = relocs; rel < relend; ++rel)
10409 {
10410 const char *name;
10411 bfd_vma value = 0;
10412 reloc_howto_type *howto;
10413 bool cross_mode_jump_p = false;
10414 /* TRUE if the relocation is a RELA relocation, rather than a
10415 REL relocation. */
10416 bool rela_relocation_p = true;
10417 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10418 const char *msg;
10419 unsigned long r_symndx;
10420 asection *sec;
10421 Elf_Internal_Shdr *symtab_hdr;
10422 struct elf_link_hash_entry *h;
10423 bool rel_reloc;
10424
10425 rel_reloc = (NEWABI_P (input_bfd)
10426 && mips_elf_rel_relocation_p (input_bfd, input_section,
10427 relocs, rel));
10428 /* Find the relocation howto for this relocation. */
10429 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10430
10431 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10432 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10433 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10434 {
10435 sec = local_sections[r_symndx];
10436 h = NULL;
10437 }
10438 else
10439 {
10440 unsigned long extsymoff;
10441
10442 extsymoff = 0;
10443 if (!elf_bad_symtab (input_bfd))
10444 extsymoff = symtab_hdr->sh_info;
10445 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10446 while (h->root.type == bfd_link_hash_indirect
10447 || h->root.type == bfd_link_hash_warning)
10448 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10449
10450 sec = NULL;
10451 if (h->root.type == bfd_link_hash_defined
10452 || h->root.type == bfd_link_hash_defweak)
10453 sec = h->root.u.def.section;
10454 }
10455
10456 if (sec != NULL && discarded_section (sec))
10457 {
10458 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10459 input_section, &rel, &relend,
10460 rel_reloc, howto, contents);
10461 continue;
10462 }
10463
10464 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10465 {
10466 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10467 64-bit code, but make sure all their addresses are in the
10468 lowermost or uppermost 32-bit section of the 64-bit address
10469 space. Thus, when they use an R_MIPS_64 they mean what is
10470 usually meant by R_MIPS_32, with the exception that the
10471 stored value is sign-extended to 64 bits. */
10472 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
10473
10474 /* On big-endian systems, we need to lie about the position
10475 of the reloc. */
10476 if (bfd_big_endian (input_bfd))
10477 rel->r_offset += 4;
10478 }
10479
10480 if (!use_saved_addend_p)
10481 {
10482 /* If these relocations were originally of the REL variety,
10483 we must pull the addend out of the field that will be
10484 relocated. Otherwise, we simply use the contents of the
10485 RELA relocation. */
10486 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10487 relocs, rel))
10488 {
10489 rela_relocation_p = false;
10490 addend = mips_elf_read_rel_addend (input_bfd, input_section,
10491 rel, howto, contents);
10492 if (hi16_reloc_p (r_type)
10493 || (got16_reloc_p (r_type)
10494 && mips_elf_local_relocation_p (input_bfd, rel,
10495 local_sections)))
10496 {
10497 if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10498 rel, relend,
10499 contents, &addend))
10500 {
10501 if (h)
10502 name = h->root.root.string;
10503 else
10504 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10505 local_syms + r_symndx,
10506 sec);
10507 _bfd_error_handler
10508 /* xgettext:c-format */
10509 (_("%pB: can't find matching LO16 reloc against `%s'"
10510 " for %s at %#" PRIx64 " in section `%pA'"),
10511 input_bfd, name,
10512 howto->name, (uint64_t) rel->r_offset, input_section);
10513 }
10514 }
10515 else
10516 addend <<= howto->rightshift;
10517 }
10518 else
10519 addend = rel->r_addend;
10520 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10521 local_syms, local_sections, rel);
10522 }
10523
10524 if (bfd_link_relocatable (info))
10525 {
10526 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10527 && bfd_big_endian (input_bfd))
10528 rel->r_offset -= 4;
10529
10530 if (!rela_relocation_p && rel->r_addend)
10531 {
10532 addend += rel->r_addend;
10533 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10534 addend = mips_elf_high (addend);
10535 else if (r_type == R_MIPS_HIGHER)
10536 addend = mips_elf_higher (addend);
10537 else if (r_type == R_MIPS_HIGHEST)
10538 addend = mips_elf_highest (addend);
10539 else
10540 addend >>= howto->rightshift;
10541
10542 /* We use the source mask, rather than the destination
10543 mask because the place to which we are writing will be
10544 source of the addend in the final link. */
10545 addend &= howto->src_mask;
10546
10547 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10548 /* See the comment above about using R_MIPS_64 in the 32-bit
10549 ABI. Here, we need to update the addend. It would be
10550 possible to get away with just using the R_MIPS_32 reloc
10551 but for endianness. */
10552 {
10553 bfd_vma sign_bits;
10554 bfd_vma low_bits;
10555 bfd_vma high_bits;
10556
10557 if (addend & ((bfd_vma) 1 << 31))
10558 #ifdef BFD64
10559 sign_bits = ((bfd_vma) 1 << 32) - 1;
10560 #else
10561 sign_bits = -1;
10562 #endif
10563 else
10564 sign_bits = 0;
10565
10566 /* If we don't know that we have a 64-bit type,
10567 do two separate stores. */
10568 if (bfd_big_endian (input_bfd))
10569 {
10570 /* Store the sign-bits (which are most significant)
10571 first. */
10572 low_bits = sign_bits;
10573 high_bits = addend;
10574 }
10575 else
10576 {
10577 low_bits = addend;
10578 high_bits = sign_bits;
10579 }
10580 bfd_put_32 (input_bfd, low_bits,
10581 contents + rel->r_offset);
10582 bfd_put_32 (input_bfd, high_bits,
10583 contents + rel->r_offset + 4);
10584 continue;
10585 }
10586
10587 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10588 input_bfd, input_section,
10589 contents, false))
10590 return false;
10591 }
10592
10593 /* Go on to the next relocation. */
10594 continue;
10595 }
10596
10597 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10598 relocations for the same offset. In that case we are
10599 supposed to treat the output of each relocation as the addend
10600 for the next. */
10601 if (rel + 1 < relend
10602 && rel->r_offset == rel[1].r_offset
10603 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10604 use_saved_addend_p = true;
10605 else
10606 use_saved_addend_p = false;
10607
10608 /* Figure out what value we are supposed to relocate. */
10609 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10610 input_section, contents,
10611 info, rel, addend, howto,
10612 local_syms, local_sections,
10613 &value, &name, &cross_mode_jump_p,
10614 use_saved_addend_p))
10615 {
10616 case bfd_reloc_continue:
10617 /* There's nothing to do. */
10618 continue;
10619
10620 case bfd_reloc_undefined:
10621 /* mips_elf_calculate_relocation already called the
10622 undefined_symbol callback. There's no real point in
10623 trying to perform the relocation at this point, so we
10624 just skip ahead to the next relocation. */
10625 continue;
10626
10627 case bfd_reloc_notsupported:
10628 msg = _("internal error: unsupported relocation error");
10629 info->callbacks->warning
10630 (info, msg, name, input_bfd, input_section, rel->r_offset);
10631 return false;
10632
10633 case bfd_reloc_overflow:
10634 if (use_saved_addend_p)
10635 /* Ignore overflow until we reach the last relocation for
10636 a given location. */
10637 ;
10638 else
10639 {
10640 struct mips_elf_link_hash_table *htab;
10641
10642 htab = mips_elf_hash_table (info);
10643 BFD_ASSERT (htab != NULL);
10644 BFD_ASSERT (name != NULL);
10645 if (!htab->small_data_overflow_reported
10646 && (gprel16_reloc_p (howto->type)
10647 || literal_reloc_p (howto->type)))
10648 {
10649 msg = _("small-data section exceeds 64KB;"
10650 " lower small-data size limit (see option -G)");
10651
10652 htab->small_data_overflow_reported = true;
10653 (*info->callbacks->einfo) ("%P: %s\n", msg);
10654 }
10655 (*info->callbacks->reloc_overflow)
10656 (info, NULL, name, howto->name, (bfd_vma) 0,
10657 input_bfd, input_section, rel->r_offset);
10658 }
10659 break;
10660
10661 case bfd_reloc_ok:
10662 break;
10663
10664 case bfd_reloc_outofrange:
10665 msg = NULL;
10666 if (jal_reloc_p (howto->type))
10667 msg = (cross_mode_jump_p
10668 ? _("cannot convert a jump to JALX "
10669 "for a non-word-aligned address")
10670 : (howto->type == R_MIPS16_26
10671 ? _("jump to a non-word-aligned address")
10672 : _("jump to a non-instruction-aligned address")));
10673 else if (b_reloc_p (howto->type))
10674 msg = (cross_mode_jump_p
10675 ? _("cannot convert a branch to JALX "
10676 "for a non-word-aligned address")
10677 : _("branch to a non-instruction-aligned address"));
10678 else if (aligned_pcrel_reloc_p (howto->type))
10679 msg = _("PC-relative load from unaligned address");
10680 if (msg)
10681 {
10682 info->callbacks->einfo
10683 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10684 break;
10685 }
10686 /* Fall through. */
10687
10688 default:
10689 abort ();
10690 break;
10691 }
10692
10693 /* If we've got another relocation for the address, keep going
10694 until we reach the last one. */
10695 if (use_saved_addend_p)
10696 {
10697 addend = value;
10698 continue;
10699 }
10700
10701 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10702 /* See the comment above about using R_MIPS_64 in the 32-bit
10703 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10704 that calculated the right value. Now, however, we
10705 sign-extend the 32-bit result to 64-bits, and store it as a
10706 64-bit value. We are especially generous here in that we
10707 go to extreme lengths to support this usage on systems with
10708 only a 32-bit VMA. */
10709 {
10710 bfd_vma sign_bits;
10711 bfd_vma low_bits;
10712 bfd_vma high_bits;
10713
10714 if (value & ((bfd_vma) 1 << 31))
10715 #ifdef BFD64
10716 sign_bits = ((bfd_vma) 1 << 32) - 1;
10717 #else
10718 sign_bits = -1;
10719 #endif
10720 else
10721 sign_bits = 0;
10722
10723 /* If we don't know that we have a 64-bit type,
10724 do two separate stores. */
10725 if (bfd_big_endian (input_bfd))
10726 {
10727 /* Undo what we did above. */
10728 rel->r_offset -= 4;
10729 /* Store the sign-bits (which are most significant)
10730 first. */
10731 low_bits = sign_bits;
10732 high_bits = value;
10733 }
10734 else
10735 {
10736 low_bits = value;
10737 high_bits = sign_bits;
10738 }
10739 bfd_put_32 (input_bfd, low_bits,
10740 contents + rel->r_offset);
10741 bfd_put_32 (input_bfd, high_bits,
10742 contents + rel->r_offset + 4);
10743 continue;
10744 }
10745
10746 /* Actually perform the relocation. */
10747 if (! mips_elf_perform_relocation (info, howto, rel, value,
10748 input_bfd, input_section,
10749 contents, cross_mode_jump_p))
10750 return false;
10751 }
10752
10753 return true;
10754 }
10755 \f
10756 /* A function that iterates over each entry in la25_stubs and fills
10757 in the code for each one. DATA points to a mips_htab_traverse_info. */
10758
10759 static int
10760 mips_elf_create_la25_stub (void **slot, void *data)
10761 {
10762 struct mips_htab_traverse_info *hti;
10763 struct mips_elf_link_hash_table *htab;
10764 struct mips_elf_la25_stub *stub;
10765 asection *s;
10766 bfd_byte *loc;
10767 bfd_vma offset, target, target_high, target_low;
10768 bfd_vma branch_pc;
10769 bfd_signed_vma pcrel_offset = 0;
10770
10771 stub = (struct mips_elf_la25_stub *) *slot;
10772 hti = (struct mips_htab_traverse_info *) data;
10773 htab = mips_elf_hash_table (hti->info);
10774 BFD_ASSERT (htab != NULL);
10775
10776 /* Create the section contents, if we haven't already. */
10777 s = stub->stub_section;
10778 loc = s->contents;
10779 if (loc == NULL)
10780 {
10781 loc = bfd_malloc (s->size);
10782 if (loc == NULL)
10783 {
10784 hti->error = true;
10785 return false;
10786 }
10787 s->contents = loc;
10788 }
10789
10790 /* Work out where in the section this stub should go. */
10791 offset = stub->offset;
10792
10793 /* We add 8 here to account for the LUI/ADDIU instructions
10794 before the branch instruction. This cannot be moved down to
10795 where pcrel_offset is calculated as 's' is updated in
10796 mips_elf_get_la25_target. */
10797 branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10798
10799 /* Work out the target address. */
10800 target = mips_elf_get_la25_target (stub, &s);
10801 target += s->output_section->vma + s->output_offset;
10802
10803 target_high = ((target + 0x8000) >> 16) & 0xffff;
10804 target_low = (target & 0xffff);
10805
10806 /* Calculate the PC of the compact branch instruction (for the case where
10807 compact branches are used for either microMIPSR6 or MIPSR6 with
10808 compact branches. Add 4-bytes to account for BC using the PC of the
10809 next instruction as the base. */
10810 pcrel_offset = target - (branch_pc + 4);
10811
10812 if (stub->stub_section != htab->strampoline)
10813 {
10814 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10815 of the section and write the two instructions at the end. */
10816 memset (loc, 0, offset);
10817 loc += offset;
10818 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10819 {
10820 bfd_put_micromips_32 (hti->output_bfd,
10821 LA25_LUI_MICROMIPS (target_high),
10822 loc);
10823 bfd_put_micromips_32 (hti->output_bfd,
10824 LA25_ADDIU_MICROMIPS (target_low),
10825 loc + 4);
10826 }
10827 else
10828 {
10829 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10830 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10831 }
10832 }
10833 else
10834 {
10835 /* This is trampoline. */
10836 loc += offset;
10837 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10838 {
10839 bfd_put_micromips_32 (hti->output_bfd,
10840 LA25_LUI_MICROMIPS (target_high), loc);
10841 bfd_put_micromips_32 (hti->output_bfd,
10842 LA25_J_MICROMIPS (target), loc + 4);
10843 bfd_put_micromips_32 (hti->output_bfd,
10844 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10845 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10846 }
10847 else
10848 {
10849 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10850 if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10851 {
10852 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10853 bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10854 }
10855 else
10856 {
10857 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10858 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10859 }
10860 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10861 }
10862 }
10863 return true;
10864 }
10865
10866 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10867 adjust it appropriately now. */
10868
10869 static void
10870 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10871 const char *name, Elf_Internal_Sym *sym)
10872 {
10873 /* The linker script takes care of providing names and values for
10874 these, but we must place them into the right sections. */
10875 static const char* const text_section_symbols[] = {
10876 "_ftext",
10877 "_etext",
10878 "__dso_displacement",
10879 "__elf_header",
10880 "__program_header_table",
10881 NULL
10882 };
10883
10884 static const char* const data_section_symbols[] = {
10885 "_fdata",
10886 "_edata",
10887 "_end",
10888 "_fbss",
10889 NULL
10890 };
10891
10892 const char* const *p;
10893 int i;
10894
10895 for (i = 0; i < 2; ++i)
10896 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10897 *p;
10898 ++p)
10899 if (strcmp (*p, name) == 0)
10900 {
10901 /* All of these symbols are given type STT_SECTION by the
10902 IRIX6 linker. */
10903 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10904 sym->st_other = STO_PROTECTED;
10905
10906 /* The IRIX linker puts these symbols in special sections. */
10907 if (i == 0)
10908 sym->st_shndx = SHN_MIPS_TEXT;
10909 else
10910 sym->st_shndx = SHN_MIPS_DATA;
10911
10912 break;
10913 }
10914 }
10915
10916 /* Finish up dynamic symbol handling. We set the contents of various
10917 dynamic sections here. */
10918
10919 bool
10920 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10921 struct bfd_link_info *info,
10922 struct elf_link_hash_entry *h,
10923 Elf_Internal_Sym *sym)
10924 {
10925 bfd *dynobj;
10926 asection *sgot;
10927 struct mips_got_info *g, *gg;
10928 const char *name;
10929 int idx;
10930 struct mips_elf_link_hash_table *htab;
10931 struct mips_elf_link_hash_entry *hmips;
10932
10933 htab = mips_elf_hash_table (info);
10934 BFD_ASSERT (htab != NULL);
10935 dynobj = elf_hash_table (info)->dynobj;
10936 hmips = (struct mips_elf_link_hash_entry *) h;
10937
10938 BFD_ASSERT (htab->root.target_os != is_vxworks);
10939
10940 if (h->plt.plist != NULL
10941 && (h->plt.plist->mips_offset != MINUS_ONE
10942 || h->plt.plist->comp_offset != MINUS_ONE))
10943 {
10944 /* We've decided to create a PLT entry for this symbol. */
10945 bfd_byte *loc;
10946 bfd_vma header_address, got_address;
10947 bfd_vma got_address_high, got_address_low, load;
10948 bfd_vma got_index;
10949 bfd_vma isa_bit;
10950
10951 got_index = h->plt.plist->gotplt_index;
10952
10953 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10954 BFD_ASSERT (h->dynindx != -1);
10955 BFD_ASSERT (htab->root.splt != NULL);
10956 BFD_ASSERT (got_index != MINUS_ONE);
10957 BFD_ASSERT (!h->def_regular);
10958
10959 /* Calculate the address of the PLT header. */
10960 isa_bit = htab->plt_header_is_comp;
10961 header_address = (htab->root.splt->output_section->vma
10962 + htab->root.splt->output_offset + isa_bit);
10963
10964 /* Calculate the address of the .got.plt entry. */
10965 got_address = (htab->root.sgotplt->output_section->vma
10966 + htab->root.sgotplt->output_offset
10967 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10968
10969 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10970 got_address_low = got_address & 0xffff;
10971
10972 /* The PLT sequence is not safe for N64 if .got.plt entry's address
10973 cannot be loaded in two instructions. */
10974 if (ABI_64_P (output_bfd)
10975 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
10976 {
10977 _bfd_error_handler
10978 /* xgettext:c-format */
10979 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
10980 "supported; consider using `-Ttext-segment=...'"),
10981 output_bfd,
10982 htab->root.sgotplt->output_section,
10983 (int64_t) got_address);
10984 bfd_set_error (bfd_error_no_error);
10985 return false;
10986 }
10987
10988 /* Initially point the .got.plt entry at the PLT header. */
10989 loc = (htab->root.sgotplt->contents
10990 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10991 if (ABI_64_P (output_bfd))
10992 bfd_put_64 (output_bfd, header_address, loc);
10993 else
10994 bfd_put_32 (output_bfd, header_address, loc);
10995
10996 /* Now handle the PLT itself. First the standard entry (the order
10997 does not matter, we just have to pick one). */
10998 if (h->plt.plist->mips_offset != MINUS_ONE)
10999 {
11000 const bfd_vma *plt_entry;
11001 bfd_vma plt_offset;
11002
11003 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11004
11005 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11006
11007 /* Find out where the .plt entry should go. */
11008 loc = htab->root.splt->contents + plt_offset;
11009
11010 /* Pick the load opcode. */
11011 load = MIPS_ELF_LOAD_WORD (output_bfd);
11012
11013 /* Fill in the PLT entry itself. */
11014
11015 if (MIPSR6_P (output_bfd))
11016 plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11017 : mipsr6_exec_plt_entry;
11018 else
11019 plt_entry = mips_exec_plt_entry;
11020 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11021 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11022 loc + 4);
11023
11024 if (! LOAD_INTERLOCKS_P (output_bfd)
11025 || (MIPSR6_P (output_bfd) && htab->compact_branches))
11026 {
11027 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11028 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11029 }
11030 else
11031 {
11032 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11033 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11034 loc + 12);
11035 }
11036 }
11037
11038 /* Now the compressed entry. They come after any standard ones. */
11039 if (h->plt.plist->comp_offset != MINUS_ONE)
11040 {
11041 bfd_vma plt_offset;
11042
11043 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11044 + h->plt.plist->comp_offset);
11045
11046 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11047
11048 /* Find out where the .plt entry should go. */
11049 loc = htab->root.splt->contents + plt_offset;
11050
11051 /* Fill in the PLT entry itself. */
11052 if (!MICROMIPS_P (output_bfd))
11053 {
11054 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11055
11056 bfd_put_16 (output_bfd, plt_entry[0], loc);
11057 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11058 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11059 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11060 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11061 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11062 bfd_put_32 (output_bfd, got_address, loc + 12);
11063 }
11064 else if (htab->insn32)
11065 {
11066 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11067
11068 bfd_put_16 (output_bfd, plt_entry[0], loc);
11069 bfd_put_16 (output_bfd, got_address_high, loc + 2);
11070 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11071 bfd_put_16 (output_bfd, got_address_low, loc + 6);
11072 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11073 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11074 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11075 bfd_put_16 (output_bfd, got_address_low, loc + 14);
11076 }
11077 else
11078 {
11079 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11080 bfd_signed_vma gotpc_offset;
11081 bfd_vma loc_address;
11082
11083 BFD_ASSERT (got_address % 4 == 0);
11084
11085 loc_address = (htab->root.splt->output_section->vma
11086 + htab->root.splt->output_offset + plt_offset);
11087 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11088
11089 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11090 if (gotpc_offset + 0x1000000 >= 0x2000000)
11091 {
11092 _bfd_error_handler
11093 /* xgettext:c-format */
11094 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11095 "beyond the range of ADDIUPC"),
11096 output_bfd,
11097 htab->root.sgotplt->output_section,
11098 (int64_t) gotpc_offset,
11099 htab->root.splt->output_section);
11100 bfd_set_error (bfd_error_no_error);
11101 return false;
11102 }
11103 bfd_put_16 (output_bfd,
11104 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11105 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11106 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11107 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11108 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11109 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11110 }
11111 }
11112
11113 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11114 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11115 got_index - 2, h->dynindx,
11116 R_MIPS_JUMP_SLOT, got_address);
11117
11118 /* We distinguish between PLT entries and lazy-binding stubs by
11119 giving the former an st_other value of STO_MIPS_PLT. Set the
11120 flag and leave the value if there are any relocations in the
11121 binary where pointer equality matters. */
11122 sym->st_shndx = SHN_UNDEF;
11123 if (h->pointer_equality_needed)
11124 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11125 else
11126 {
11127 sym->st_value = 0;
11128 sym->st_other = 0;
11129 }
11130 }
11131
11132 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11133 {
11134 /* We've decided to create a lazy-binding stub. */
11135 bool micromips_p = MICROMIPS_P (output_bfd);
11136 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11137 bfd_vma stub_size = htab->function_stub_size;
11138 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11139 bfd_vma isa_bit = micromips_p;
11140 bfd_vma stub_big_size;
11141
11142 if (!micromips_p)
11143 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11144 else if (htab->insn32)
11145 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11146 else
11147 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11148
11149 /* This symbol has a stub. Set it up. */
11150
11151 BFD_ASSERT (h->dynindx != -1);
11152
11153 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11154
11155 /* Values up to 2^31 - 1 are allowed. Larger values would cause
11156 sign extension at runtime in the stub, resulting in a negative
11157 index value. */
11158 if (h->dynindx & ~0x7fffffff)
11159 return false;
11160
11161 /* Fill the stub. */
11162 if (micromips_p)
11163 {
11164 idx = 0;
11165 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11166 stub + idx);
11167 idx += 4;
11168 if (htab->insn32)
11169 {
11170 bfd_put_micromips_32 (output_bfd,
11171 STUB_MOVE32_MICROMIPS, stub + idx);
11172 idx += 4;
11173 }
11174 else
11175 {
11176 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11177 idx += 2;
11178 }
11179 if (stub_size == stub_big_size)
11180 {
11181 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11182
11183 bfd_put_micromips_32 (output_bfd,
11184 STUB_LUI_MICROMIPS (dynindx_hi),
11185 stub + idx);
11186 idx += 4;
11187 }
11188 if (htab->insn32)
11189 {
11190 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11191 stub + idx);
11192 idx += 4;
11193 }
11194 else
11195 {
11196 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11197 idx += 2;
11198 }
11199
11200 /* If a large stub is not required and sign extension is not a
11201 problem, then use legacy code in the stub. */
11202 if (stub_size == stub_big_size)
11203 bfd_put_micromips_32 (output_bfd,
11204 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11205 stub + idx);
11206 else if (h->dynindx & ~0x7fff)
11207 bfd_put_micromips_32 (output_bfd,
11208 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11209 stub + idx);
11210 else
11211 bfd_put_micromips_32 (output_bfd,
11212 STUB_LI16S_MICROMIPS (output_bfd,
11213 h->dynindx),
11214 stub + idx);
11215 }
11216 else
11217 {
11218 idx = 0;
11219 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11220 idx += 4;
11221 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11222 idx += 4;
11223 if (stub_size == stub_big_size)
11224 {
11225 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11226 stub + idx);
11227 idx += 4;
11228 }
11229
11230 if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11231 {
11232 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11233 idx += 4;
11234 }
11235
11236 /* If a large stub is not required and sign extension is not a
11237 problem, then use legacy code in the stub. */
11238 if (stub_size == stub_big_size)
11239 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11240 stub + idx);
11241 else if (h->dynindx & ~0x7fff)
11242 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11243 stub + idx);
11244 else
11245 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11246 stub + idx);
11247 idx += 4;
11248
11249 if (MIPSR6_P (output_bfd) && htab->compact_branches)
11250 bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11251 }
11252
11253 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11254 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11255 stub, stub_size);
11256
11257 /* Mark the symbol as undefined. stub_offset != -1 occurs
11258 only for the referenced symbol. */
11259 sym->st_shndx = SHN_UNDEF;
11260
11261 /* The run-time linker uses the st_value field of the symbol
11262 to reset the global offset table entry for this external
11263 to its stub address when unlinking a shared object. */
11264 sym->st_value = (htab->sstubs->output_section->vma
11265 + htab->sstubs->output_offset
11266 + h->plt.plist->stub_offset
11267 + isa_bit);
11268 sym->st_other = other;
11269 }
11270
11271 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11272 refer to the stub, since only the stub uses the standard calling
11273 conventions. */
11274 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11275 {
11276 BFD_ASSERT (hmips->need_fn_stub);
11277 sym->st_value = (hmips->fn_stub->output_section->vma
11278 + hmips->fn_stub->output_offset);
11279 sym->st_size = hmips->fn_stub->size;
11280 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11281 }
11282
11283 BFD_ASSERT (h->dynindx != -1
11284 || h->forced_local);
11285
11286 sgot = htab->root.sgot;
11287 g = htab->got_info;
11288 BFD_ASSERT (g != NULL);
11289
11290 /* Run through the global symbol table, creating GOT entries for all
11291 the symbols that need them. */
11292 if (hmips->global_got_area != GGA_NONE)
11293 {
11294 bfd_vma offset;
11295 bfd_vma value;
11296
11297 value = sym->st_value;
11298 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11299 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11300 }
11301
11302 if (hmips->global_got_area != GGA_NONE && g->next)
11303 {
11304 struct mips_got_entry e, *p;
11305 bfd_vma entry;
11306 bfd_vma offset;
11307
11308 gg = g;
11309
11310 e.abfd = output_bfd;
11311 e.symndx = -1;
11312 e.d.h = hmips;
11313 e.tls_type = GOT_TLS_NONE;
11314
11315 for (g = g->next; g->next != gg; g = g->next)
11316 {
11317 if (g->got_entries
11318 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11319 &e)))
11320 {
11321 offset = p->gotidx;
11322 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11323 if (bfd_link_pic (info)
11324 || (elf_hash_table (info)->dynamic_sections_created
11325 && p->d.h != NULL
11326 && p->d.h->root.def_dynamic
11327 && !p->d.h->root.def_regular))
11328 {
11329 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11330 the various compatibility problems, it's easier to mock
11331 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11332 mips_elf_create_dynamic_relocation to calculate the
11333 appropriate addend. */
11334 Elf_Internal_Rela rel[3];
11335
11336 memset (rel, 0, sizeof (rel));
11337 if (ABI_64_P (output_bfd))
11338 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11339 else
11340 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11341 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11342
11343 entry = 0;
11344 if (! (mips_elf_create_dynamic_relocation
11345 (output_bfd, info, rel,
11346 e.d.h, NULL, sym->st_value, &entry, sgot)))
11347 return false;
11348 }
11349 else
11350 entry = sym->st_value;
11351 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11352 }
11353 }
11354 }
11355
11356 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11357 name = h->root.root.string;
11358 if (h == elf_hash_table (info)->hdynamic
11359 || h == elf_hash_table (info)->hgot)
11360 sym->st_shndx = SHN_ABS;
11361 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11362 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11363 {
11364 sym->st_shndx = SHN_ABS;
11365 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11366 sym->st_value = 1;
11367 }
11368 else if (SGI_COMPAT (output_bfd))
11369 {
11370 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11371 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11372 {
11373 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11374 sym->st_other = STO_PROTECTED;
11375 sym->st_value = 0;
11376 sym->st_shndx = SHN_MIPS_DATA;
11377 }
11378 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11379 {
11380 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11381 sym->st_other = STO_PROTECTED;
11382 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11383 sym->st_shndx = SHN_ABS;
11384 }
11385 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11386 {
11387 if (h->type == STT_FUNC)
11388 sym->st_shndx = SHN_MIPS_TEXT;
11389 else if (h->type == STT_OBJECT)
11390 sym->st_shndx = SHN_MIPS_DATA;
11391 }
11392 }
11393
11394 /* Emit a copy reloc, if needed. */
11395 if (h->needs_copy)
11396 {
11397 asection *s;
11398 bfd_vma symval;
11399
11400 BFD_ASSERT (h->dynindx != -1);
11401 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11402
11403 s = mips_elf_rel_dyn_section (info, false);
11404 symval = (h->root.u.def.section->output_section->vma
11405 + h->root.u.def.section->output_offset
11406 + h->root.u.def.value);
11407 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11408 h->dynindx, R_MIPS_COPY, symval);
11409 }
11410
11411 /* Handle the IRIX6-specific symbols. */
11412 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11413 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11414
11415 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11416 to treat compressed symbols like any other. */
11417 if (ELF_ST_IS_MIPS16 (sym->st_other))
11418 {
11419 BFD_ASSERT (sym->st_value & 1);
11420 sym->st_other -= STO_MIPS16;
11421 }
11422 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11423 {
11424 BFD_ASSERT (sym->st_value & 1);
11425 sym->st_other -= STO_MICROMIPS;
11426 }
11427
11428 return true;
11429 }
11430
11431 /* Likewise, for VxWorks. */
11432
11433 bool
11434 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11435 struct bfd_link_info *info,
11436 struct elf_link_hash_entry *h,
11437 Elf_Internal_Sym *sym)
11438 {
11439 bfd *dynobj;
11440 asection *sgot;
11441 struct mips_got_info *g;
11442 struct mips_elf_link_hash_table *htab;
11443 struct mips_elf_link_hash_entry *hmips;
11444
11445 htab = mips_elf_hash_table (info);
11446 BFD_ASSERT (htab != NULL);
11447 dynobj = elf_hash_table (info)->dynobj;
11448 hmips = (struct mips_elf_link_hash_entry *) h;
11449
11450 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11451 {
11452 bfd_byte *loc;
11453 bfd_vma plt_address, got_address, got_offset, branch_offset;
11454 Elf_Internal_Rela rel;
11455 static const bfd_vma *plt_entry;
11456 bfd_vma gotplt_index;
11457 bfd_vma plt_offset;
11458
11459 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11460 gotplt_index = h->plt.plist->gotplt_index;
11461
11462 BFD_ASSERT (h->dynindx != -1);
11463 BFD_ASSERT (htab->root.splt != NULL);
11464 BFD_ASSERT (gotplt_index != MINUS_ONE);
11465 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11466
11467 /* Calculate the address of the .plt entry. */
11468 plt_address = (htab->root.splt->output_section->vma
11469 + htab->root.splt->output_offset
11470 + plt_offset);
11471
11472 /* Calculate the address of the .got.plt entry. */
11473 got_address = (htab->root.sgotplt->output_section->vma
11474 + htab->root.sgotplt->output_offset
11475 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11476
11477 /* Calculate the offset of the .got.plt entry from
11478 _GLOBAL_OFFSET_TABLE_. */
11479 got_offset = mips_elf_gotplt_index (info, h);
11480
11481 /* Calculate the offset for the branch at the start of the PLT
11482 entry. The branch jumps to the beginning of .plt. */
11483 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11484
11485 /* Fill in the initial value of the .got.plt entry. */
11486 bfd_put_32 (output_bfd, plt_address,
11487 (htab->root.sgotplt->contents
11488 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11489
11490 /* Find out where the .plt entry should go. */
11491 loc = htab->root.splt->contents + plt_offset;
11492
11493 if (bfd_link_pic (info))
11494 {
11495 plt_entry = mips_vxworks_shared_plt_entry;
11496 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11497 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11498 }
11499 else
11500 {
11501 bfd_vma got_address_high, got_address_low;
11502
11503 plt_entry = mips_vxworks_exec_plt_entry;
11504 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11505 got_address_low = got_address & 0xffff;
11506
11507 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11508 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11509 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11510 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11511 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11512 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11513 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11514 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11515
11516 loc = (htab->srelplt2->contents
11517 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11518
11519 /* Emit a relocation for the .got.plt entry. */
11520 rel.r_offset = got_address;
11521 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11522 rel.r_addend = plt_offset;
11523 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11524
11525 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11526 loc += sizeof (Elf32_External_Rela);
11527 rel.r_offset = plt_address + 8;
11528 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11529 rel.r_addend = got_offset;
11530 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11531
11532 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11533 loc += sizeof (Elf32_External_Rela);
11534 rel.r_offset += 4;
11535 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11536 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11537 }
11538
11539 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11540 loc = (htab->root.srelplt->contents
11541 + gotplt_index * sizeof (Elf32_External_Rela));
11542 rel.r_offset = got_address;
11543 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11544 rel.r_addend = 0;
11545 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11546
11547 if (!h->def_regular)
11548 sym->st_shndx = SHN_UNDEF;
11549 }
11550
11551 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11552
11553 sgot = htab->root.sgot;
11554 g = htab->got_info;
11555 BFD_ASSERT (g != NULL);
11556
11557 /* See if this symbol has an entry in the GOT. */
11558 if (hmips->global_got_area != GGA_NONE)
11559 {
11560 bfd_vma offset;
11561 Elf_Internal_Rela outrel;
11562 bfd_byte *loc;
11563 asection *s;
11564
11565 /* Install the symbol value in the GOT. */
11566 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11567 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11568
11569 /* Add a dynamic relocation for it. */
11570 s = mips_elf_rel_dyn_section (info, false);
11571 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11572 outrel.r_offset = (sgot->output_section->vma
11573 + sgot->output_offset
11574 + offset);
11575 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11576 outrel.r_addend = 0;
11577 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11578 }
11579
11580 /* Emit a copy reloc, if needed. */
11581 if (h->needs_copy)
11582 {
11583 Elf_Internal_Rela rel;
11584 asection *srel;
11585 bfd_byte *loc;
11586
11587 BFD_ASSERT (h->dynindx != -1);
11588
11589 rel.r_offset = (h->root.u.def.section->output_section->vma
11590 + h->root.u.def.section->output_offset
11591 + h->root.u.def.value);
11592 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11593 rel.r_addend = 0;
11594 if (h->root.u.def.section == htab->root.sdynrelro)
11595 srel = htab->root.sreldynrelro;
11596 else
11597 srel = htab->root.srelbss;
11598 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11599 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11600 ++srel->reloc_count;
11601 }
11602
11603 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11604 if (ELF_ST_IS_COMPRESSED (sym->st_other))
11605 sym->st_value &= ~1;
11606
11607 return true;
11608 }
11609
11610 /* Write out a plt0 entry to the beginning of .plt. */
11611
11612 static bool
11613 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11614 {
11615 bfd_byte *loc;
11616 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11617 static const bfd_vma *plt_entry;
11618 struct mips_elf_link_hash_table *htab;
11619
11620 htab = mips_elf_hash_table (info);
11621 BFD_ASSERT (htab != NULL);
11622
11623 if (ABI_64_P (output_bfd))
11624 plt_entry = (htab->compact_branches
11625 ? mipsr6_n64_exec_plt0_entry_compact
11626 : mips_n64_exec_plt0_entry);
11627 else if (ABI_N32_P (output_bfd))
11628 plt_entry = (htab->compact_branches
11629 ? mipsr6_n32_exec_plt0_entry_compact
11630 : mips_n32_exec_plt0_entry);
11631 else if (!htab->plt_header_is_comp)
11632 plt_entry = (htab->compact_branches
11633 ? mipsr6_o32_exec_plt0_entry_compact
11634 : mips_o32_exec_plt0_entry);
11635 else if (htab->insn32)
11636 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11637 else
11638 plt_entry = micromips_o32_exec_plt0_entry;
11639
11640 /* Calculate the value of .got.plt. */
11641 gotplt_value = (htab->root.sgotplt->output_section->vma
11642 + htab->root.sgotplt->output_offset);
11643 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11644 gotplt_value_low = gotplt_value & 0xffff;
11645
11646 /* The PLT sequence is not safe for N64 if .got.plt's address can
11647 not be loaded in two instructions. */
11648 if (ABI_64_P (output_bfd)
11649 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11650 {
11651 _bfd_error_handler
11652 /* xgettext:c-format */
11653 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11654 "supported; consider using `-Ttext-segment=...'"),
11655 output_bfd,
11656 htab->root.sgotplt->output_section,
11657 (int64_t) gotplt_value);
11658 bfd_set_error (bfd_error_no_error);
11659 return false;
11660 }
11661
11662 /* Install the PLT header. */
11663 loc = htab->root.splt->contents;
11664 if (plt_entry == micromips_o32_exec_plt0_entry)
11665 {
11666 bfd_vma gotpc_offset;
11667 bfd_vma loc_address;
11668 size_t i;
11669
11670 BFD_ASSERT (gotplt_value % 4 == 0);
11671
11672 loc_address = (htab->root.splt->output_section->vma
11673 + htab->root.splt->output_offset);
11674 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11675
11676 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11677 if (gotpc_offset + 0x1000000 >= 0x2000000)
11678 {
11679 _bfd_error_handler
11680 /* xgettext:c-format */
11681 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11682 "beyond the range of ADDIUPC"),
11683 output_bfd,
11684 htab->root.sgotplt->output_section,
11685 (int64_t) gotpc_offset,
11686 htab->root.splt->output_section);
11687 bfd_set_error (bfd_error_no_error);
11688 return false;
11689 }
11690 bfd_put_16 (output_bfd,
11691 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11692 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11693 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11694 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11695 }
11696 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11697 {
11698 size_t i;
11699
11700 bfd_put_16 (output_bfd, plt_entry[0], loc);
11701 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11702 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11703 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11704 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11705 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11706 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11707 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11708 }
11709 else
11710 {
11711 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11712 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11713 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11714 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11715 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11716 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11717 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11718 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11719 }
11720
11721 return true;
11722 }
11723
11724 /* Install the PLT header for a VxWorks executable and finalize the
11725 contents of .rela.plt.unloaded. */
11726
11727 static void
11728 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11729 {
11730 Elf_Internal_Rela rela;
11731 bfd_byte *loc;
11732 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11733 static const bfd_vma *plt_entry;
11734 struct mips_elf_link_hash_table *htab;
11735
11736 htab = mips_elf_hash_table (info);
11737 BFD_ASSERT (htab != NULL);
11738
11739 plt_entry = mips_vxworks_exec_plt0_entry;
11740
11741 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11742 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11743 + htab->root.hgot->root.u.def.section->output_offset
11744 + htab->root.hgot->root.u.def.value);
11745
11746 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11747 got_value_low = got_value & 0xffff;
11748
11749 /* Calculate the address of the PLT header. */
11750 plt_address = (htab->root.splt->output_section->vma
11751 + htab->root.splt->output_offset);
11752
11753 /* Install the PLT header. */
11754 loc = htab->root.splt->contents;
11755 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11756 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11757 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11758 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11759 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11760 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11761
11762 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11763 loc = htab->srelplt2->contents;
11764 rela.r_offset = plt_address;
11765 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11766 rela.r_addend = 0;
11767 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11768 loc += sizeof (Elf32_External_Rela);
11769
11770 /* Output the relocation for the following addiu of
11771 %lo(_GLOBAL_OFFSET_TABLE_). */
11772 rela.r_offset += 4;
11773 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11774 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11775 loc += sizeof (Elf32_External_Rela);
11776
11777 /* Fix up the remaining relocations. They may have the wrong
11778 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11779 in which symbols were output. */
11780 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11781 {
11782 Elf_Internal_Rela rel;
11783
11784 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11785 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11786 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11787 loc += sizeof (Elf32_External_Rela);
11788
11789 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11790 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11791 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11792 loc += sizeof (Elf32_External_Rela);
11793
11794 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11795 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11796 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11797 loc += sizeof (Elf32_External_Rela);
11798 }
11799 }
11800
11801 /* Install the PLT header for a VxWorks shared library. */
11802
11803 static void
11804 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11805 {
11806 unsigned int i;
11807 struct mips_elf_link_hash_table *htab;
11808
11809 htab = mips_elf_hash_table (info);
11810 BFD_ASSERT (htab != NULL);
11811
11812 /* We just need to copy the entry byte-by-byte. */
11813 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11814 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11815 htab->root.splt->contents + i * 4);
11816 }
11817
11818 /* Finish up the dynamic sections. */
11819
11820 bool
11821 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11822 struct bfd_link_info *info)
11823 {
11824 bfd *dynobj;
11825 asection *sdyn;
11826 asection *sgot;
11827 struct mips_got_info *gg, *g;
11828 struct mips_elf_link_hash_table *htab;
11829
11830 htab = mips_elf_hash_table (info);
11831 BFD_ASSERT (htab != NULL);
11832
11833 dynobj = elf_hash_table (info)->dynobj;
11834
11835 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11836
11837 sgot = htab->root.sgot;
11838 gg = htab->got_info;
11839
11840 if (elf_hash_table (info)->dynamic_sections_created)
11841 {
11842 bfd_byte *b;
11843 int dyn_to_skip = 0, dyn_skipped = 0;
11844
11845 BFD_ASSERT (sdyn != NULL);
11846 BFD_ASSERT (gg != NULL);
11847
11848 g = mips_elf_bfd_got (output_bfd, false);
11849 BFD_ASSERT (g != NULL);
11850
11851 for (b = sdyn->contents;
11852 b < sdyn->contents + sdyn->size;
11853 b += MIPS_ELF_DYN_SIZE (dynobj))
11854 {
11855 Elf_Internal_Dyn dyn;
11856 const char *name;
11857 size_t elemsize;
11858 asection *s;
11859 bool swap_out_p;
11860
11861 /* Read in the current dynamic entry. */
11862 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11863
11864 /* Assume that we're going to modify it and write it out. */
11865 swap_out_p = true;
11866
11867 switch (dyn.d_tag)
11868 {
11869 case DT_RELENT:
11870 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11871 break;
11872
11873 case DT_RELAENT:
11874 BFD_ASSERT (htab->root.target_os == is_vxworks);
11875 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11876 break;
11877
11878 case DT_STRSZ:
11879 /* Rewrite DT_STRSZ. */
11880 dyn.d_un.d_val =
11881 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11882 break;
11883
11884 case DT_PLTGOT:
11885 s = htab->root.sgot;
11886 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11887 break;
11888
11889 case DT_MIPS_PLTGOT:
11890 s = htab->root.sgotplt;
11891 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11892 break;
11893
11894 case DT_MIPS_RLD_VERSION:
11895 dyn.d_un.d_val = 1; /* XXX */
11896 break;
11897
11898 case DT_MIPS_FLAGS:
11899 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11900 break;
11901
11902 case DT_MIPS_TIME_STAMP:
11903 {
11904 time_t t;
11905 time (&t);
11906 dyn.d_un.d_val = t;
11907 }
11908 break;
11909
11910 case DT_MIPS_ICHECKSUM:
11911 /* XXX FIXME: */
11912 swap_out_p = false;
11913 break;
11914
11915 case DT_MIPS_IVERSION:
11916 /* XXX FIXME: */
11917 swap_out_p = false;
11918 break;
11919
11920 case DT_MIPS_BASE_ADDRESS:
11921 s = output_bfd->sections;
11922 BFD_ASSERT (s != NULL);
11923 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11924 break;
11925
11926 case DT_MIPS_LOCAL_GOTNO:
11927 dyn.d_un.d_val = g->local_gotno;
11928 break;
11929
11930 case DT_MIPS_UNREFEXTNO:
11931 /* The index into the dynamic symbol table which is the
11932 entry of the first external symbol that is not
11933 referenced within the same object. */
11934 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11935 break;
11936
11937 case DT_MIPS_GOTSYM:
11938 if (htab->global_gotsym)
11939 {
11940 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11941 break;
11942 }
11943 /* In case if we don't have global got symbols we default
11944 to setting DT_MIPS_GOTSYM to the same value as
11945 DT_MIPS_SYMTABNO. */
11946 /* Fall through. */
11947
11948 case DT_MIPS_SYMTABNO:
11949 name = ".dynsym";
11950 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11951 s = bfd_get_linker_section (dynobj, name);
11952
11953 if (s != NULL)
11954 dyn.d_un.d_val = s->size / elemsize;
11955 else
11956 dyn.d_un.d_val = 0;
11957 break;
11958
11959 case DT_MIPS_HIPAGENO:
11960 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11961 break;
11962
11963 case DT_MIPS_RLD_MAP:
11964 {
11965 struct elf_link_hash_entry *h;
11966 h = mips_elf_hash_table (info)->rld_symbol;
11967 if (!h)
11968 {
11969 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11970 swap_out_p = false;
11971 break;
11972 }
11973 s = h->root.u.def.section;
11974
11975 /* The MIPS_RLD_MAP tag stores the absolute address of the
11976 debug pointer. */
11977 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11978 + h->root.u.def.value);
11979 }
11980 break;
11981
11982 case DT_MIPS_RLD_MAP_REL:
11983 {
11984 struct elf_link_hash_entry *h;
11985 bfd_vma dt_addr, rld_addr;
11986 h = mips_elf_hash_table (info)->rld_symbol;
11987 if (!h)
11988 {
11989 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11990 swap_out_p = false;
11991 break;
11992 }
11993 s = h->root.u.def.section;
11994
11995 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
11996 pointer, relative to the address of the tag. */
11997 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
11998 + (b - sdyn->contents));
11999 rld_addr = (s->output_section->vma + s->output_offset
12000 + h->root.u.def.value);
12001 dyn.d_un.d_ptr = rld_addr - dt_addr;
12002 }
12003 break;
12004
12005 case DT_MIPS_OPTIONS:
12006 s = (bfd_get_section_by_name
12007 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12008 dyn.d_un.d_ptr = s->vma;
12009 break;
12010
12011 case DT_PLTREL:
12012 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12013 if (htab->root.target_os == is_vxworks)
12014 dyn.d_un.d_val = DT_RELA;
12015 else
12016 dyn.d_un.d_val = DT_REL;
12017 break;
12018
12019 case DT_PLTRELSZ:
12020 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12021 dyn.d_un.d_val = htab->root.srelplt->size;
12022 break;
12023
12024 case DT_JMPREL:
12025 BFD_ASSERT (htab->use_plts_and_copy_relocs);
12026 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12027 + htab->root.srelplt->output_offset);
12028 break;
12029
12030 case DT_TEXTREL:
12031 /* If we didn't need any text relocations after all, delete
12032 the dynamic tag. */
12033 if (!(info->flags & DF_TEXTREL))
12034 {
12035 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
12036 swap_out_p = false;
12037 }
12038 break;
12039
12040 case DT_FLAGS:
12041 /* If we didn't need any text relocations after all, clear
12042 DF_TEXTREL from DT_FLAGS. */
12043 if (!(info->flags & DF_TEXTREL))
12044 dyn.d_un.d_val &= ~DF_TEXTREL;
12045 else
12046 swap_out_p = false;
12047 break;
12048
12049 case DT_MIPS_XHASH:
12050 name = ".MIPS.xhash";
12051 s = bfd_get_linker_section (dynobj, name);
12052 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12053 break;
12054
12055 default:
12056 swap_out_p = false;
12057 if (htab->root.target_os == is_vxworks
12058 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12059 swap_out_p = true;
12060 break;
12061 }
12062
12063 if (swap_out_p || dyn_skipped)
12064 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12065 (dynobj, &dyn, b - dyn_skipped);
12066
12067 if (dyn_to_skip)
12068 {
12069 dyn_skipped += dyn_to_skip;
12070 dyn_to_skip = 0;
12071 }
12072 }
12073
12074 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
12075 if (dyn_skipped > 0)
12076 memset (b - dyn_skipped, 0, dyn_skipped);
12077 }
12078
12079 if (sgot != NULL && sgot->size > 0
12080 && !bfd_is_abs_section (sgot->output_section))
12081 {
12082 if (htab->root.target_os == is_vxworks)
12083 {
12084 /* The first entry of the global offset table points to the
12085 ".dynamic" section. The second is initialized by the
12086 loader and contains the shared library identifier.
12087 The third is also initialized by the loader and points
12088 to the lazy resolution stub. */
12089 MIPS_ELF_PUT_WORD (output_bfd,
12090 sdyn->output_offset + sdyn->output_section->vma,
12091 sgot->contents);
12092 MIPS_ELF_PUT_WORD (output_bfd, 0,
12093 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12094 MIPS_ELF_PUT_WORD (output_bfd, 0,
12095 sgot->contents
12096 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12097 }
12098 else
12099 {
12100 /* The first entry of the global offset table will be filled at
12101 runtime. The second entry will be used by some runtime loaders.
12102 This isn't the case of IRIX rld. */
12103 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12104 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12105 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12106 }
12107
12108 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12109 = MIPS_ELF_GOT_SIZE (output_bfd);
12110 }
12111
12112 /* Generate dynamic relocations for the non-primary gots. */
12113 if (gg != NULL && gg->next)
12114 {
12115 Elf_Internal_Rela rel[3];
12116 bfd_vma addend = 0;
12117
12118 memset (rel, 0, sizeof (rel));
12119 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12120
12121 for (g = gg->next; g->next != gg; g = g->next)
12122 {
12123 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12124 + g->next->tls_gotno;
12125
12126 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12127 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12128 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12129 sgot->contents
12130 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12131
12132 if (! bfd_link_pic (info))
12133 continue;
12134
12135 for (; got_index < g->local_gotno; got_index++)
12136 {
12137 if (got_index >= g->assigned_low_gotno
12138 && got_index <= g->assigned_high_gotno)
12139 continue;
12140
12141 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12142 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12143 if (!(mips_elf_create_dynamic_relocation
12144 (output_bfd, info, rel, NULL,
12145 bfd_abs_section_ptr,
12146 0, &addend, sgot)))
12147 return false;
12148 BFD_ASSERT (addend == 0);
12149 }
12150 }
12151 }
12152
12153 /* The generation of dynamic relocations for the non-primary gots
12154 adds more dynamic relocations. We cannot count them until
12155 here. */
12156
12157 if (elf_hash_table (info)->dynamic_sections_created)
12158 {
12159 bfd_byte *b;
12160 bool swap_out_p;
12161
12162 BFD_ASSERT (sdyn != NULL);
12163
12164 for (b = sdyn->contents;
12165 b < sdyn->contents + sdyn->size;
12166 b += MIPS_ELF_DYN_SIZE (dynobj))
12167 {
12168 Elf_Internal_Dyn dyn;
12169 asection *s;
12170
12171 /* Read in the current dynamic entry. */
12172 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12173
12174 /* Assume that we're going to modify it and write it out. */
12175 swap_out_p = true;
12176
12177 switch (dyn.d_tag)
12178 {
12179 case DT_RELSZ:
12180 /* Reduce DT_RELSZ to account for any relocations we
12181 decided not to make. This is for the n64 irix rld,
12182 which doesn't seem to apply any relocations if there
12183 are trailing null entries. */
12184 s = mips_elf_rel_dyn_section (info, false);
12185 dyn.d_un.d_val = (s->reloc_count
12186 * (ABI_64_P (output_bfd)
12187 ? sizeof (Elf64_Mips_External_Rel)
12188 : sizeof (Elf32_External_Rel)));
12189 /* Adjust the section size too. Tools like the prelinker
12190 can reasonably expect the values to the same. */
12191 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12192 elf_section_data (s->output_section)->this_hdr.sh_size
12193 = dyn.d_un.d_val;
12194 break;
12195
12196 default:
12197 swap_out_p = false;
12198 break;
12199 }
12200
12201 if (swap_out_p)
12202 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12203 (dynobj, &dyn, b);
12204 }
12205 }
12206
12207 {
12208 asection *s;
12209 Elf32_compact_rel cpt;
12210
12211 if (SGI_COMPAT (output_bfd))
12212 {
12213 /* Write .compact_rel section out. */
12214 s = bfd_get_linker_section (dynobj, ".compact_rel");
12215 if (s != NULL)
12216 {
12217 cpt.id1 = 1;
12218 cpt.num = s->reloc_count;
12219 cpt.id2 = 2;
12220 cpt.offset = (s->output_section->filepos
12221 + sizeof (Elf32_External_compact_rel));
12222 cpt.reserved0 = 0;
12223 cpt.reserved1 = 0;
12224 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12225 ((Elf32_External_compact_rel *)
12226 s->contents));
12227
12228 /* Clean up a dummy stub function entry in .text. */
12229 if (htab->sstubs != NULL
12230 && htab->sstubs->contents != NULL)
12231 {
12232 file_ptr dummy_offset;
12233
12234 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12235 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12236 memset (htab->sstubs->contents + dummy_offset, 0,
12237 htab->function_stub_size);
12238 }
12239 }
12240 }
12241
12242 /* The psABI says that the dynamic relocations must be sorted in
12243 increasing order of r_symndx. The VxWorks EABI doesn't require
12244 this, and because the code below handles REL rather than RELA
12245 relocations, using it for VxWorks would be outright harmful. */
12246 if (htab->root.target_os != is_vxworks)
12247 {
12248 s = mips_elf_rel_dyn_section (info, false);
12249 if (s != NULL
12250 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12251 {
12252 reldyn_sorting_bfd = output_bfd;
12253
12254 if (ABI_64_P (output_bfd))
12255 qsort ((Elf64_External_Rel *) s->contents + 1,
12256 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12257 sort_dynamic_relocs_64);
12258 else
12259 qsort ((Elf32_External_Rel *) s->contents + 1,
12260 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12261 sort_dynamic_relocs);
12262 }
12263 }
12264 }
12265
12266 if (htab->root.splt && htab->root.splt->size > 0)
12267 {
12268 if (htab->root.target_os == is_vxworks)
12269 {
12270 if (bfd_link_pic (info))
12271 mips_vxworks_finish_shared_plt (output_bfd, info);
12272 else
12273 mips_vxworks_finish_exec_plt (output_bfd, info);
12274 }
12275 else
12276 {
12277 BFD_ASSERT (!bfd_link_pic (info));
12278 if (!mips_finish_exec_plt (output_bfd, info))
12279 return false;
12280 }
12281 }
12282 return true;
12283 }
12284
12285
12286 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12287
12288 static void
12289 mips_set_isa_flags (bfd *abfd)
12290 {
12291 flagword val;
12292
12293 switch (bfd_get_mach (abfd))
12294 {
12295 default:
12296 if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12297 val = E_MIPS_ARCH_3;
12298 else
12299 val = E_MIPS_ARCH_1;
12300 break;
12301
12302 case bfd_mach_mips3000:
12303 val = E_MIPS_ARCH_1;
12304 break;
12305
12306 case bfd_mach_mips3900:
12307 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12308 break;
12309
12310 case bfd_mach_mips6000:
12311 val = E_MIPS_ARCH_2;
12312 break;
12313
12314 case bfd_mach_mips4010:
12315 val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12316 break;
12317
12318 case bfd_mach_mips4000:
12319 case bfd_mach_mips4300:
12320 case bfd_mach_mips4400:
12321 case bfd_mach_mips4600:
12322 val = E_MIPS_ARCH_3;
12323 break;
12324
12325 case bfd_mach_mips4100:
12326 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12327 break;
12328
12329 case bfd_mach_mips4111:
12330 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12331 break;
12332
12333 case bfd_mach_mips4120:
12334 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12335 break;
12336
12337 case bfd_mach_mips4650:
12338 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12339 break;
12340
12341 case bfd_mach_mips5400:
12342 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12343 break;
12344
12345 case bfd_mach_mips5500:
12346 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12347 break;
12348
12349 case bfd_mach_mips5900:
12350 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12351 break;
12352
12353 case bfd_mach_mips9000:
12354 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12355 break;
12356
12357 case bfd_mach_mips5000:
12358 case bfd_mach_mips7000:
12359 case bfd_mach_mips8000:
12360 case bfd_mach_mips10000:
12361 case bfd_mach_mips12000:
12362 case bfd_mach_mips14000:
12363 case bfd_mach_mips16000:
12364 val = E_MIPS_ARCH_4;
12365 break;
12366
12367 case bfd_mach_mips5:
12368 val = E_MIPS_ARCH_5;
12369 break;
12370
12371 case bfd_mach_mips_loongson_2e:
12372 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12373 break;
12374
12375 case bfd_mach_mips_loongson_2f:
12376 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12377 break;
12378
12379 case bfd_mach_mips_sb1:
12380 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12381 break;
12382
12383 case bfd_mach_mips_gs464:
12384 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
12385 break;
12386
12387 case bfd_mach_mips_gs464e:
12388 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12389 break;
12390
12391 case bfd_mach_mips_gs264e:
12392 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12393 break;
12394
12395 case bfd_mach_mips_octeon:
12396 case bfd_mach_mips_octeonp:
12397 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12398 break;
12399
12400 case bfd_mach_mips_octeon3:
12401 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12402 break;
12403
12404 case bfd_mach_mips_xlr:
12405 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12406 break;
12407
12408 case bfd_mach_mips_octeon2:
12409 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12410 break;
12411
12412 case bfd_mach_mipsisa32:
12413 val = E_MIPS_ARCH_32;
12414 break;
12415
12416 case bfd_mach_mipsisa64:
12417 val = E_MIPS_ARCH_64;
12418 break;
12419
12420 case bfd_mach_mipsisa32r2:
12421 case bfd_mach_mipsisa32r3:
12422 case bfd_mach_mipsisa32r5:
12423 val = E_MIPS_ARCH_32R2;
12424 break;
12425
12426 case bfd_mach_mips_interaptiv_mr2:
12427 val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12428 break;
12429
12430 case bfd_mach_mipsisa64r2:
12431 case bfd_mach_mipsisa64r3:
12432 case bfd_mach_mipsisa64r5:
12433 val = E_MIPS_ARCH_64R2;
12434 break;
12435
12436 case bfd_mach_mipsisa32r6:
12437 val = E_MIPS_ARCH_32R6;
12438 break;
12439
12440 case bfd_mach_mipsisa64r6:
12441 val = E_MIPS_ARCH_64R6;
12442 break;
12443 }
12444 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12445 elf_elfheader (abfd)->e_flags |= val;
12446
12447 }
12448
12449
12450 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12451 Don't do so for code sections. We want to keep ordering of HI16/LO16
12452 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12453 relocs to be sorted. */
12454
12455 bool
12456 _bfd_mips_elf_sort_relocs_p (asection *sec)
12457 {
12458 return (sec->flags & SEC_CODE) == 0;
12459 }
12460
12461
12462 /* The final processing done just before writing out a MIPS ELF object
12463 file. This gets the MIPS architecture right based on the machine
12464 number. This is used by both the 32-bit and the 64-bit ABI. */
12465
12466 void
12467 _bfd_mips_final_write_processing (bfd *abfd)
12468 {
12469 unsigned int i;
12470 Elf_Internal_Shdr **hdrpp;
12471 const char *name;
12472 asection *sec;
12473
12474 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12475 is nonzero. This is for compatibility with old objects, which used
12476 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12477 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12478 mips_set_isa_flags (abfd);
12479
12480 /* Set the sh_info field for .gptab sections and other appropriate
12481 info for each special section. */
12482 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12483 i < elf_numsections (abfd);
12484 i++, hdrpp++)
12485 {
12486 switch ((*hdrpp)->sh_type)
12487 {
12488 case SHT_MIPS_MSYM:
12489 case SHT_MIPS_LIBLIST:
12490 sec = bfd_get_section_by_name (abfd, ".dynstr");
12491 if (sec != NULL)
12492 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12493 break;
12494
12495 case SHT_MIPS_GPTAB:
12496 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12497 name = bfd_section_name ((*hdrpp)->bfd_section);
12498 BFD_ASSERT (name != NULL
12499 && startswith (name, ".gptab."));
12500 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12501 BFD_ASSERT (sec != NULL);
12502 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12503 break;
12504
12505 case SHT_MIPS_CONTENT:
12506 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12507 name = bfd_section_name ((*hdrpp)->bfd_section);
12508 BFD_ASSERT (name != NULL
12509 && startswith (name, ".MIPS.content"));
12510 sec = bfd_get_section_by_name (abfd,
12511 name + sizeof ".MIPS.content" - 1);
12512 BFD_ASSERT (sec != NULL);
12513 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12514 break;
12515
12516 case SHT_MIPS_SYMBOL_LIB:
12517 sec = bfd_get_section_by_name (abfd, ".dynsym");
12518 if (sec != NULL)
12519 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12520 sec = bfd_get_section_by_name (abfd, ".liblist");
12521 if (sec != NULL)
12522 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12523 break;
12524
12525 case SHT_MIPS_EVENTS:
12526 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12527 name = bfd_section_name ((*hdrpp)->bfd_section);
12528 BFD_ASSERT (name != NULL);
12529 if (startswith (name, ".MIPS.events"))
12530 sec = bfd_get_section_by_name (abfd,
12531 name + sizeof ".MIPS.events" - 1);
12532 else
12533 {
12534 BFD_ASSERT (startswith (name, ".MIPS.post_rel"));
12535 sec = bfd_get_section_by_name (abfd,
12536 (name
12537 + sizeof ".MIPS.post_rel" - 1));
12538 }
12539 BFD_ASSERT (sec != NULL);
12540 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12541 break;
12542
12543 case SHT_MIPS_XHASH:
12544 sec = bfd_get_section_by_name (abfd, ".dynsym");
12545 if (sec != NULL)
12546 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12547 }
12548 }
12549 }
12550
12551 bool
12552 _bfd_mips_elf_final_write_processing (bfd *abfd)
12553 {
12554 _bfd_mips_final_write_processing (abfd);
12555 return _bfd_elf_final_write_processing (abfd);
12556 }
12557 \f
12558 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
12559 segments. */
12560
12561 int
12562 _bfd_mips_elf_additional_program_headers (bfd *abfd,
12563 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12564 {
12565 asection *s;
12566 int ret = 0;
12567
12568 /* See if we need a PT_MIPS_REGINFO segment. */
12569 s = bfd_get_section_by_name (abfd, ".reginfo");
12570 if (s && (s->flags & SEC_LOAD))
12571 ++ret;
12572
12573 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12574 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12575 ++ret;
12576
12577 /* See if we need a PT_MIPS_OPTIONS segment. */
12578 if (IRIX_COMPAT (abfd) == ict_irix6
12579 && bfd_get_section_by_name (abfd,
12580 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12581 ++ret;
12582
12583 /* See if we need a PT_MIPS_RTPROC segment. */
12584 if (IRIX_COMPAT (abfd) == ict_irix5
12585 && bfd_get_section_by_name (abfd, ".dynamic")
12586 && bfd_get_section_by_name (abfd, ".mdebug"))
12587 ++ret;
12588
12589 /* Allocate a PT_NULL header in dynamic objects. See
12590 _bfd_mips_elf_modify_segment_map for details. */
12591 if (!SGI_COMPAT (abfd)
12592 && bfd_get_section_by_name (abfd, ".dynamic"))
12593 ++ret;
12594
12595 return ret;
12596 }
12597
12598 /* Modify the segment map for an IRIX5 executable. */
12599
12600 bool
12601 _bfd_mips_elf_modify_segment_map (bfd *abfd,
12602 struct bfd_link_info *info)
12603 {
12604 asection *s;
12605 struct elf_segment_map *m, **pm;
12606 size_t amt;
12607
12608 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12609 segment. */
12610 s = bfd_get_section_by_name (abfd, ".reginfo");
12611 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12612 {
12613 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12614 if (m->p_type == PT_MIPS_REGINFO)
12615 break;
12616 if (m == NULL)
12617 {
12618 amt = sizeof *m;
12619 m = bfd_zalloc (abfd, amt);
12620 if (m == NULL)
12621 return false;
12622
12623 m->p_type = PT_MIPS_REGINFO;
12624 m->count = 1;
12625 m->sections[0] = s;
12626
12627 /* We want to put it after the PHDR and INTERP segments. */
12628 pm = &elf_seg_map (abfd);
12629 while (*pm != NULL
12630 && ((*pm)->p_type == PT_PHDR
12631 || (*pm)->p_type == PT_INTERP))
12632 pm = &(*pm)->next;
12633
12634 m->next = *pm;
12635 *pm = m;
12636 }
12637 }
12638
12639 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12640 segment. */
12641 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12642 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12643 {
12644 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12645 if (m->p_type == PT_MIPS_ABIFLAGS)
12646 break;
12647 if (m == NULL)
12648 {
12649 amt = sizeof *m;
12650 m = bfd_zalloc (abfd, amt);
12651 if (m == NULL)
12652 return false;
12653
12654 m->p_type = PT_MIPS_ABIFLAGS;
12655 m->count = 1;
12656 m->sections[0] = s;
12657
12658 /* We want to put it after the PHDR and INTERP segments. */
12659 pm = &elf_seg_map (abfd);
12660 while (*pm != NULL
12661 && ((*pm)->p_type == PT_PHDR
12662 || (*pm)->p_type == PT_INTERP))
12663 pm = &(*pm)->next;
12664
12665 m->next = *pm;
12666 *pm = m;
12667 }
12668 }
12669
12670 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12671 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
12672 PT_MIPS_OPTIONS segment immediately following the program header
12673 table. */
12674 if (NEWABI_P (abfd)
12675 /* On non-IRIX6 new abi, we'll have already created a segment
12676 for this section, so don't create another. I'm not sure this
12677 is not also the case for IRIX 6, but I can't test it right
12678 now. */
12679 && IRIX_COMPAT (abfd) == ict_irix6)
12680 {
12681 for (s = abfd->sections; s; s = s->next)
12682 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12683 break;
12684
12685 if (s)
12686 {
12687 struct elf_segment_map *options_segment;
12688
12689 pm = &elf_seg_map (abfd);
12690 while (*pm != NULL
12691 && ((*pm)->p_type == PT_PHDR
12692 || (*pm)->p_type == PT_INTERP))
12693 pm = &(*pm)->next;
12694
12695 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12696 {
12697 amt = sizeof (struct elf_segment_map);
12698 options_segment = bfd_zalloc (abfd, amt);
12699 options_segment->next = *pm;
12700 options_segment->p_type = PT_MIPS_OPTIONS;
12701 options_segment->p_flags = PF_R;
12702 options_segment->p_flags_valid = true;
12703 options_segment->count = 1;
12704 options_segment->sections[0] = s;
12705 *pm = options_segment;
12706 }
12707 }
12708 }
12709 else
12710 {
12711 if (IRIX_COMPAT (abfd) == ict_irix5)
12712 {
12713 /* If there are .dynamic and .mdebug sections, we make a room
12714 for the RTPROC header. FIXME: Rewrite without section names. */
12715 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12716 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12717 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12718 {
12719 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12720 if (m->p_type == PT_MIPS_RTPROC)
12721 break;
12722 if (m == NULL)
12723 {
12724 amt = sizeof *m;
12725 m = bfd_zalloc (abfd, amt);
12726 if (m == NULL)
12727 return false;
12728
12729 m->p_type = PT_MIPS_RTPROC;
12730
12731 s = bfd_get_section_by_name (abfd, ".rtproc");
12732 if (s == NULL)
12733 {
12734 m->count = 0;
12735 m->p_flags = 0;
12736 m->p_flags_valid = 1;
12737 }
12738 else
12739 {
12740 m->count = 1;
12741 m->sections[0] = s;
12742 }
12743
12744 /* We want to put it after the DYNAMIC segment. */
12745 pm = &elf_seg_map (abfd);
12746 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12747 pm = &(*pm)->next;
12748 if (*pm != NULL)
12749 pm = &(*pm)->next;
12750
12751 m->next = *pm;
12752 *pm = m;
12753 }
12754 }
12755 }
12756 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12757 .dynstr, .dynsym, and .hash sections, and everything in
12758 between. */
12759 for (pm = &elf_seg_map (abfd); *pm != NULL;
12760 pm = &(*pm)->next)
12761 if ((*pm)->p_type == PT_DYNAMIC)
12762 break;
12763 m = *pm;
12764 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12765 glibc's dynamic linker has traditionally derived the number of
12766 tags from the p_filesz field, and sometimes allocates stack
12767 arrays of that size. An overly-big PT_DYNAMIC segment can
12768 be actively harmful in such cases. Making PT_DYNAMIC contain
12769 other sections can also make life hard for the prelinker,
12770 which might move one of the other sections to a different
12771 PT_LOAD segment. */
12772 if (SGI_COMPAT (abfd)
12773 && m != NULL
12774 && m->count == 1
12775 && strcmp (m->sections[0]->name, ".dynamic") == 0)
12776 {
12777 static const char *sec_names[] =
12778 {
12779 ".dynamic", ".dynstr", ".dynsym", ".hash"
12780 };
12781 bfd_vma low, high;
12782 unsigned int i, c;
12783 struct elf_segment_map *n;
12784
12785 low = ~(bfd_vma) 0;
12786 high = 0;
12787 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12788 {
12789 s = bfd_get_section_by_name (abfd, sec_names[i]);
12790 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12791 {
12792 bfd_size_type sz;
12793
12794 if (low > s->vma)
12795 low = s->vma;
12796 sz = s->size;
12797 if (high < s->vma + sz)
12798 high = s->vma + sz;
12799 }
12800 }
12801
12802 c = 0;
12803 for (s = abfd->sections; s != NULL; s = s->next)
12804 if ((s->flags & SEC_LOAD) != 0
12805 && s->vma >= low
12806 && s->vma + s->size <= high)
12807 ++c;
12808
12809 amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12810 n = bfd_zalloc (abfd, amt);
12811 if (n == NULL)
12812 return false;
12813 *n = *m;
12814 n->count = c;
12815
12816 i = 0;
12817 for (s = abfd->sections; s != NULL; s = s->next)
12818 {
12819 if ((s->flags & SEC_LOAD) != 0
12820 && s->vma >= low
12821 && s->vma + s->size <= high)
12822 {
12823 n->sections[i] = s;
12824 ++i;
12825 }
12826 }
12827
12828 *pm = n;
12829 }
12830 }
12831
12832 /* Allocate a spare program header in dynamic objects so that tools
12833 like the prelinker can add an extra PT_LOAD entry.
12834
12835 If the prelinker needs to make room for a new PT_LOAD entry, its
12836 standard procedure is to move the first (read-only) sections into
12837 the new (writable) segment. However, the MIPS ABI requires
12838 .dynamic to be in a read-only segment, and the section will often
12839 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12840
12841 Although the prelinker could in principle move .dynamic to a
12842 writable segment, it seems better to allocate a spare program
12843 header instead, and avoid the need to move any sections.
12844 There is a long tradition of allocating spare dynamic tags,
12845 so allocating a spare program header seems like a natural
12846 extension.
12847
12848 If INFO is NULL, we may be copying an already prelinked binary
12849 with objcopy or strip, so do not add this header. */
12850 if (info != NULL
12851 && !SGI_COMPAT (abfd)
12852 && bfd_get_section_by_name (abfd, ".dynamic"))
12853 {
12854 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12855 if ((*pm)->p_type == PT_NULL)
12856 break;
12857 if (*pm == NULL)
12858 {
12859 m = bfd_zalloc (abfd, sizeof (*m));
12860 if (m == NULL)
12861 return false;
12862
12863 m->p_type = PT_NULL;
12864 *pm = m;
12865 }
12866 }
12867
12868 return true;
12869 }
12870 \f
12871 /* Return the section that should be marked against GC for a given
12872 relocation. */
12873
12874 asection *
12875 _bfd_mips_elf_gc_mark_hook (asection *sec,
12876 struct bfd_link_info *info,
12877 Elf_Internal_Rela *rel,
12878 struct elf_link_hash_entry *h,
12879 Elf_Internal_Sym *sym)
12880 {
12881 /* ??? Do mips16 stub sections need to be handled special? */
12882
12883 if (h != NULL)
12884 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12885 {
12886 case R_MIPS_GNU_VTINHERIT:
12887 case R_MIPS_GNU_VTENTRY:
12888 return NULL;
12889 }
12890
12891 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12892 }
12893
12894 /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12895
12896 bool
12897 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12898 elf_gc_mark_hook_fn gc_mark_hook)
12899 {
12900 bfd *sub;
12901
12902 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12903
12904 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12905 {
12906 asection *o;
12907
12908 if (! is_mips_elf (sub))
12909 continue;
12910
12911 for (o = sub->sections; o != NULL; o = o->next)
12912 if (!o->gc_mark
12913 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12914 {
12915 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12916 return false;
12917 }
12918 }
12919
12920 return true;
12921 }
12922 \f
12923 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12924 hiding the old indirect symbol. Process additional relocation
12925 information. Also called for weakdefs, in which case we just let
12926 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12927
12928 void
12929 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12930 struct elf_link_hash_entry *dir,
12931 struct elf_link_hash_entry *ind)
12932 {
12933 struct mips_elf_link_hash_entry *dirmips, *indmips;
12934
12935 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12936
12937 dirmips = (struct mips_elf_link_hash_entry *) dir;
12938 indmips = (struct mips_elf_link_hash_entry *) ind;
12939 /* Any absolute non-dynamic relocations against an indirect or weak
12940 definition will be against the target symbol. */
12941 if (indmips->has_static_relocs)
12942 dirmips->has_static_relocs = true;
12943
12944 if (ind->root.type != bfd_link_hash_indirect)
12945 return;
12946
12947 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12948 if (indmips->readonly_reloc)
12949 dirmips->readonly_reloc = true;
12950 if (indmips->no_fn_stub)
12951 dirmips->no_fn_stub = true;
12952 if (indmips->fn_stub)
12953 {
12954 dirmips->fn_stub = indmips->fn_stub;
12955 indmips->fn_stub = NULL;
12956 }
12957 if (indmips->need_fn_stub)
12958 {
12959 dirmips->need_fn_stub = true;
12960 indmips->need_fn_stub = false;
12961 }
12962 if (indmips->call_stub)
12963 {
12964 dirmips->call_stub = indmips->call_stub;
12965 indmips->call_stub = NULL;
12966 }
12967 if (indmips->call_fp_stub)
12968 {
12969 dirmips->call_fp_stub = indmips->call_fp_stub;
12970 indmips->call_fp_stub = NULL;
12971 }
12972 if (indmips->global_got_area < dirmips->global_got_area)
12973 dirmips->global_got_area = indmips->global_got_area;
12974 if (indmips->global_got_area < GGA_NONE)
12975 indmips->global_got_area = GGA_NONE;
12976 if (indmips->has_nonpic_branches)
12977 dirmips->has_nonpic_branches = true;
12978 }
12979
12980 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
12981 to hide it. It has to remain global (it will also be protected) so as to
12982 be assigned a global GOT entry, which will then remain unchanged at load
12983 time. */
12984
12985 void
12986 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
12987 struct elf_link_hash_entry *entry,
12988 bool force_local)
12989 {
12990 struct mips_elf_link_hash_table *htab;
12991
12992 htab = mips_elf_hash_table (info);
12993 BFD_ASSERT (htab != NULL);
12994 if (htab->use_absolute_zero
12995 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
12996 return;
12997
12998 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
12999 }
13000 \f
13001 #define PDR_SIZE 32
13002
13003 bool
13004 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13005 struct bfd_link_info *info)
13006 {
13007 asection *o;
13008 bool ret = false;
13009 unsigned char *tdata;
13010 size_t i, skip;
13011
13012 o = bfd_get_section_by_name (abfd, ".pdr");
13013 if (! o)
13014 return false;
13015 if (o->size == 0)
13016 return false;
13017 if (o->size % PDR_SIZE != 0)
13018 return false;
13019 if (o->output_section != NULL
13020 && bfd_is_abs_section (o->output_section))
13021 return false;
13022
13023 tdata = bfd_zmalloc (o->size / PDR_SIZE);
13024 if (! tdata)
13025 return false;
13026
13027 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
13028 info->keep_memory);
13029 if (!cookie->rels)
13030 {
13031 free (tdata);
13032 return false;
13033 }
13034
13035 cookie->rel = cookie->rels;
13036 cookie->relend = cookie->rels + o->reloc_count;
13037
13038 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
13039 {
13040 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
13041 {
13042 tdata[i] = 1;
13043 skip ++;
13044 }
13045 }
13046
13047 if (skip != 0)
13048 {
13049 mips_elf_section_data (o)->u.tdata = tdata;
13050 if (o->rawsize == 0)
13051 o->rawsize = o->size;
13052 o->size -= skip * PDR_SIZE;
13053 ret = true;
13054 }
13055 else
13056 free (tdata);
13057
13058 if (! info->keep_memory)
13059 free (cookie->rels);
13060
13061 return ret;
13062 }
13063
13064 bool
13065 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13066 {
13067 if (strcmp (sec->name, ".pdr") == 0)
13068 return true;
13069 return false;
13070 }
13071
13072 bool
13073 _bfd_mips_elf_write_section (bfd *output_bfd,
13074 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13075 asection *sec, bfd_byte *contents)
13076 {
13077 bfd_byte *to, *from, *end;
13078 int i;
13079
13080 if (strcmp (sec->name, ".pdr") != 0)
13081 return false;
13082
13083 if (mips_elf_section_data (sec)->u.tdata == NULL)
13084 return false;
13085
13086 to = contents;
13087 end = contents + sec->size;
13088 for (from = contents, i = 0;
13089 from < end;
13090 from += PDR_SIZE, i++)
13091 {
13092 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13093 continue;
13094 if (to != from)
13095 memcpy (to, from, PDR_SIZE);
13096 to += PDR_SIZE;
13097 }
13098 bfd_set_section_contents (output_bfd, sec->output_section, contents,
13099 sec->output_offset, sec->size);
13100 return true;
13101 }
13102 \f
13103 /* microMIPS code retains local labels for linker relaxation. Omit them
13104 from output by default for clarity. */
13105
13106 bool
13107 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13108 {
13109 return _bfd_elf_is_local_label_name (abfd, sym->name);
13110 }
13111
13112 /* MIPS ELF uses a special find_nearest_line routine in order the
13113 handle the ECOFF debugging information. */
13114
13115 struct mips_elf_find_line
13116 {
13117 struct ecoff_debug_info d;
13118 struct ecoff_find_line i;
13119 };
13120
13121 bool
13122 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13123 asection *section, bfd_vma offset,
13124 const char **filename_ptr,
13125 const char **functionname_ptr,
13126 unsigned int *line_ptr,
13127 unsigned int *discriminator_ptr)
13128 {
13129 asection *msec;
13130
13131 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13132 filename_ptr, functionname_ptr,
13133 line_ptr, discriminator_ptr,
13134 dwarf_debug_sections,
13135 &elf_tdata (abfd)->dwarf2_find_line_info)
13136 == 1)
13137 return true;
13138
13139 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13140 filename_ptr, functionname_ptr,
13141 line_ptr))
13142 {
13143 if (!*functionname_ptr)
13144 _bfd_elf_find_function (abfd, symbols, section, offset,
13145 *filename_ptr ? NULL : filename_ptr,
13146 functionname_ptr);
13147 return true;
13148 }
13149
13150 msec = bfd_get_section_by_name (abfd, ".mdebug");
13151 if (msec != NULL)
13152 {
13153 flagword origflags;
13154 struct mips_elf_find_line *fi;
13155 const struct ecoff_debug_swap * const swap =
13156 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13157
13158 /* If we are called during a link, mips_elf_final_link may have
13159 cleared the SEC_HAS_CONTENTS field. We force it back on here
13160 if appropriate (which it normally will be). */
13161 origflags = msec->flags;
13162 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13163 msec->flags |= SEC_HAS_CONTENTS;
13164
13165 fi = mips_elf_tdata (abfd)->find_line_info;
13166 if (fi == NULL)
13167 {
13168 bfd_size_type external_fdr_size;
13169 char *fraw_src;
13170 char *fraw_end;
13171 struct fdr *fdr_ptr;
13172 bfd_size_type amt = sizeof (struct mips_elf_find_line);
13173
13174 fi = bfd_zalloc (abfd, amt);
13175 if (fi == NULL)
13176 {
13177 msec->flags = origflags;
13178 return false;
13179 }
13180
13181 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13182 {
13183 msec->flags = origflags;
13184 return false;
13185 }
13186
13187 /* Swap in the FDR information. */
13188 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13189 fi->d.fdr = bfd_alloc (abfd, amt);
13190 if (fi->d.fdr == NULL)
13191 {
13192 msec->flags = origflags;
13193 return false;
13194 }
13195 external_fdr_size = swap->external_fdr_size;
13196 fdr_ptr = fi->d.fdr;
13197 fraw_src = (char *) fi->d.external_fdr;
13198 fraw_end = (fraw_src
13199 + fi->d.symbolic_header.ifdMax * external_fdr_size);
13200 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13201 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13202
13203 mips_elf_tdata (abfd)->find_line_info = fi;
13204
13205 /* Note that we don't bother to ever free this information.
13206 find_nearest_line is either called all the time, as in
13207 objdump -l, so the information should be saved, or it is
13208 rarely called, as in ld error messages, so the memory
13209 wasted is unimportant. Still, it would probably be a
13210 good idea for free_cached_info to throw it away. */
13211 }
13212
13213 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13214 &fi->i, filename_ptr, functionname_ptr,
13215 line_ptr))
13216 {
13217 msec->flags = origflags;
13218 return true;
13219 }
13220
13221 msec->flags = origflags;
13222 }
13223
13224 /* Fall back on the generic ELF find_nearest_line routine. */
13225
13226 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13227 filename_ptr, functionname_ptr,
13228 line_ptr, discriminator_ptr);
13229 }
13230
13231 bool
13232 _bfd_mips_elf_find_inliner_info (bfd *abfd,
13233 const char **filename_ptr,
13234 const char **functionname_ptr,
13235 unsigned int *line_ptr)
13236 {
13237 bool found;
13238 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13239 functionname_ptr, line_ptr,
13240 & elf_tdata (abfd)->dwarf2_find_line_info);
13241 return found;
13242 }
13243
13244 \f
13245 /* When are writing out the .options or .MIPS.options section,
13246 remember the bytes we are writing out, so that we can install the
13247 GP value in the section_processing routine. */
13248
13249 bool
13250 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13251 const void *location,
13252 file_ptr offset, bfd_size_type count)
13253 {
13254 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13255 {
13256 bfd_byte *c;
13257
13258 if (elf_section_data (section) == NULL)
13259 {
13260 size_t amt = sizeof (struct bfd_elf_section_data);
13261 section->used_by_bfd = bfd_zalloc (abfd, amt);
13262 if (elf_section_data (section) == NULL)
13263 return false;
13264 }
13265 c = mips_elf_section_data (section)->u.tdata;
13266 if (c == NULL)
13267 {
13268 c = bfd_zalloc (abfd, section->size);
13269 if (c == NULL)
13270 return false;
13271 mips_elf_section_data (section)->u.tdata = c;
13272 }
13273
13274 memcpy (c + offset, location, count);
13275 }
13276
13277 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13278 count);
13279 }
13280
13281 /* This is almost identical to bfd_generic_get_... except that some
13282 MIPS relocations need to be handled specially. Sigh. */
13283
13284 bfd_byte *
13285 _bfd_elf_mips_get_relocated_section_contents
13286 (bfd *abfd,
13287 struct bfd_link_info *link_info,
13288 struct bfd_link_order *link_order,
13289 bfd_byte *data,
13290 bool relocatable,
13291 asymbol **symbols)
13292 {
13293 bfd *input_bfd = link_order->u.indirect.section->owner;
13294 asection *input_section = link_order->u.indirect.section;
13295 long reloc_size;
13296 arelent **reloc_vector;
13297 long reloc_count;
13298
13299 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13300 if (reloc_size < 0)
13301 return NULL;
13302
13303 /* Read in the section. */
13304 bfd_byte *orig_data = data;
13305 if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13306 return NULL;
13307
13308 if (data == NULL)
13309 return NULL;
13310
13311 if (reloc_size == 0)
13312 return data;
13313
13314 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13315 if (reloc_vector == NULL)
13316 {
13317 struct mips_elf_obj_tdata *tdata;
13318 struct mips_hi16 **hip, *hi;
13319 error_return:
13320 /* If we are going to return an error, remove entries on
13321 mips_hi16_list that point into this section's data. Data
13322 will typically be freed on return from this function. */
13323 tdata = mips_elf_tdata (abfd);
13324 hip = &tdata->mips_hi16_list;
13325 while ((hi = *hip) != NULL)
13326 {
13327 if (hi->input_section == input_section)
13328 {
13329 *hip = hi->next;
13330 free (hi);
13331 }
13332 else
13333 hip = &hi->next;
13334 }
13335 if (orig_data == NULL)
13336 free (data);
13337 data = NULL;
13338 goto out;
13339 }
13340
13341 reloc_count = bfd_canonicalize_reloc (input_bfd,
13342 input_section,
13343 reloc_vector,
13344 symbols);
13345 if (reloc_count < 0)
13346 goto error_return;
13347
13348 if (reloc_count > 0)
13349 {
13350 arelent **parent;
13351 /* for mips */
13352 int gp_found;
13353 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13354
13355 {
13356 struct bfd_hash_entry *h;
13357 struct bfd_link_hash_entry *lh;
13358 /* Skip all this stuff if we aren't mixing formats. */
13359 if (abfd && input_bfd
13360 && abfd->xvec == input_bfd->xvec)
13361 lh = 0;
13362 else
13363 {
13364 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
13365 lh = (struct bfd_link_hash_entry *) h;
13366 }
13367 lookup:
13368 if (lh)
13369 {
13370 switch (lh->type)
13371 {
13372 case bfd_link_hash_undefined:
13373 case bfd_link_hash_undefweak:
13374 case bfd_link_hash_common:
13375 gp_found = 0;
13376 break;
13377 case bfd_link_hash_defined:
13378 case bfd_link_hash_defweak:
13379 gp_found = 1;
13380 gp = lh->u.def.value;
13381 break;
13382 case bfd_link_hash_indirect:
13383 case bfd_link_hash_warning:
13384 lh = lh->u.i.link;
13385 /* @@FIXME ignoring warning for now */
13386 goto lookup;
13387 case bfd_link_hash_new:
13388 default:
13389 abort ();
13390 }
13391 }
13392 else
13393 gp_found = 0;
13394 }
13395 /* end mips */
13396
13397 for (parent = reloc_vector; *parent != NULL; parent++)
13398 {
13399 char *error_message = NULL;
13400 asymbol *symbol;
13401 bfd_reloc_status_type r;
13402
13403 symbol = *(*parent)->sym_ptr_ptr;
13404 /* PR ld/19628: A specially crafted input file
13405 can result in a NULL symbol pointer here. */
13406 if (symbol == NULL)
13407 {
13408 link_info->callbacks->einfo
13409 /* xgettext:c-format */
13410 (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13411 abfd, input_section, (* parent)->address);
13412 goto error_return;
13413 }
13414
13415 /* Zap reloc field when the symbol is from a discarded
13416 section, ignoring any addend. Do the same when called
13417 from bfd_simple_get_relocated_section_contents for
13418 undefined symbols in debug sections. This is to keep
13419 debug info reasonably sane, in particular so that
13420 DW_FORM_ref_addr to another file's .debug_info isn't
13421 confused with an offset into the current file's
13422 .debug_info. */
13423 if ((symbol->section != NULL && discarded_section (symbol->section))
13424 || (symbol->section == bfd_und_section_ptr
13425 && (input_section->flags & SEC_DEBUGGING) != 0
13426 && link_info->input_bfds == link_info->output_bfd))
13427 {
13428 bfd_vma off;
13429 static reloc_howto_type none_howto
13430 = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13431 "unused", false, 0, 0, false);
13432
13433 off = ((*parent)->address
13434 * bfd_octets_per_byte (input_bfd, input_section));
13435 _bfd_clear_contents ((*parent)->howto, input_bfd,
13436 input_section, data, off);
13437 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13438 (*parent)->addend = 0;
13439 (*parent)->howto = &none_howto;
13440 r = bfd_reloc_ok;
13441 }
13442
13443 /* Specific to MIPS: Deal with relocation types that require
13444 knowing the gp of the output bfd. */
13445
13446 /* If we've managed to find the gp and have a special
13447 function for the relocation then go ahead, else default
13448 to the generic handling. */
13449 else if (gp_found
13450 && ((*parent)->howto->special_function
13451 == _bfd_mips_elf32_gprel16_reloc))
13452 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
13453 input_section, relocatable,
13454 data, gp);
13455 else
13456 r = bfd_perform_relocation (input_bfd,
13457 *parent,
13458 data,
13459 input_section,
13460 relocatable ? abfd : NULL,
13461 &error_message);
13462
13463 if (relocatable)
13464 {
13465 asection *os = input_section->output_section;
13466
13467 /* A partial link, so keep the relocs. */
13468 os->orelocation[os->reloc_count] = *parent;
13469 os->reloc_count++;
13470 }
13471
13472 if (r != bfd_reloc_ok)
13473 {
13474 switch (r)
13475 {
13476 case bfd_reloc_undefined:
13477 (*link_info->callbacks->undefined_symbol)
13478 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13479 input_bfd, input_section, (*parent)->address, true);
13480 break;
13481 case bfd_reloc_dangerous:
13482 BFD_ASSERT (error_message != NULL);
13483 (*link_info->callbacks->reloc_dangerous)
13484 (link_info, error_message,
13485 input_bfd, input_section, (*parent)->address);
13486 break;
13487 case bfd_reloc_overflow:
13488 (*link_info->callbacks->reloc_overflow)
13489 (link_info, NULL,
13490 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13491 (*parent)->howto->name, (*parent)->addend,
13492 input_bfd, input_section, (*parent)->address);
13493 break;
13494 case bfd_reloc_outofrange:
13495 /* PR ld/13730:
13496 This error can result when processing some partially
13497 complete binaries. Do not abort, but issue an error
13498 message instead. */
13499 link_info->callbacks->einfo
13500 /* xgettext:c-format */
13501 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13502 abfd, input_section, * parent);
13503 goto error_return;
13504
13505 case bfd_reloc_notsupported:
13506 /* PR ld/17512
13507 This error can result when processing a corrupt binary.
13508 Do not abort. Issue an error message instead. */
13509 link_info->callbacks->einfo
13510 /* xgettext:c-format */
13511 (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13512 abfd, input_section, * parent);
13513 goto error_return;
13514
13515 default:
13516 /* PR 17512; file: 90c2a92e.
13517 Report unexpected results, without aborting. */
13518 link_info->callbacks->einfo
13519 /* xgettext:c-format */
13520 (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13521 abfd, input_section, * parent, r);
13522 break;
13523 }
13524
13525 }
13526 }
13527 }
13528
13529 out:
13530 free (reloc_vector);
13531 return data;
13532 }
13533 \f
13534 static bool
13535 mips_elf_relax_delete_bytes (bfd *abfd,
13536 asection *sec, bfd_vma addr, int count)
13537 {
13538 Elf_Internal_Shdr *symtab_hdr;
13539 unsigned int sec_shndx;
13540 bfd_byte *contents;
13541 Elf_Internal_Rela *irel, *irelend;
13542 Elf_Internal_Sym *isym;
13543 Elf_Internal_Sym *isymend;
13544 struct elf_link_hash_entry **sym_hashes;
13545 struct elf_link_hash_entry **end_hashes;
13546 struct elf_link_hash_entry **start_hashes;
13547 unsigned int symcount;
13548
13549 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13550 contents = elf_section_data (sec)->this_hdr.contents;
13551
13552 irel = elf_section_data (sec)->relocs;
13553 irelend = irel + sec->reloc_count;
13554
13555 /* Actually delete the bytes. */
13556 memmove (contents + addr, contents + addr + count,
13557 (size_t) (sec->size - addr - count));
13558 sec->size -= count;
13559
13560 /* Adjust all the relocs. */
13561 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13562 {
13563 /* Get the new reloc address. */
13564 if (irel->r_offset > addr)
13565 irel->r_offset -= count;
13566 }
13567
13568 BFD_ASSERT (addr % 2 == 0);
13569 BFD_ASSERT (count % 2 == 0);
13570
13571 /* Adjust the local symbols defined in this section. */
13572 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13573 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13574 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13575 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13576 isym->st_value -= count;
13577
13578 /* Now adjust the global symbols defined in this section. */
13579 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13580 - symtab_hdr->sh_info);
13581 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13582 end_hashes = sym_hashes + symcount;
13583
13584 for (; sym_hashes < end_hashes; sym_hashes++)
13585 {
13586 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13587
13588 if ((sym_hash->root.type == bfd_link_hash_defined
13589 || sym_hash->root.type == bfd_link_hash_defweak)
13590 && sym_hash->root.u.def.section == sec)
13591 {
13592 bfd_vma value = sym_hash->root.u.def.value;
13593
13594 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13595 value &= MINUS_TWO;
13596 if (value > addr)
13597 sym_hash->root.u.def.value -= count;
13598 }
13599 }
13600
13601 return true;
13602 }
13603
13604
13605 /* Opcodes needed for microMIPS relaxation as found in
13606 opcodes/micromips-opc.c. */
13607
13608 struct opcode_descriptor {
13609 unsigned long match;
13610 unsigned long mask;
13611 };
13612
13613 /* The $ra register aka $31. */
13614
13615 #define RA 31
13616
13617 /* 32-bit instruction format register fields. */
13618
13619 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13620 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13621
13622 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
13623
13624 #define OP16_VALID_REG(r) \
13625 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13626
13627
13628 /* 32-bit and 16-bit branches. */
13629
13630 static const struct opcode_descriptor b_insns_32[] = {
13631 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13632 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13633 { 0, 0 } /* End marker for find_match(). */
13634 };
13635
13636 static const struct opcode_descriptor bc_insn_32 =
13637 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13638
13639 static const struct opcode_descriptor bz_insn_32 =
13640 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13641
13642 static const struct opcode_descriptor bzal_insn_32 =
13643 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13644
13645 static const struct opcode_descriptor beq_insn_32 =
13646 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13647
13648 static const struct opcode_descriptor b_insn_16 =
13649 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13650
13651 static const struct opcode_descriptor bz_insn_16 =
13652 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
13653
13654
13655 /* 32-bit and 16-bit branch EQ and NE zero. */
13656
13657 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13658 eq and second the ne. This convention is used when replacing a
13659 32-bit BEQ/BNE with the 16-bit version. */
13660
13661 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13662
13663 static const struct opcode_descriptor bz_rs_insns_32[] = {
13664 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13665 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13666 { 0, 0 } /* End marker for find_match(). */
13667 };
13668
13669 static const struct opcode_descriptor bz_rt_insns_32[] = {
13670 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13671 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13672 { 0, 0 } /* End marker for find_match(). */
13673 };
13674
13675 static const struct opcode_descriptor bzc_insns_32[] = {
13676 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13677 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13678 { 0, 0 } /* End marker for find_match(). */
13679 };
13680
13681 static const struct opcode_descriptor bz_insns_16[] = {
13682 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13683 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13684 { 0, 0 } /* End marker for find_match(). */
13685 };
13686
13687 /* Switch between a 5-bit register index and its 3-bit shorthand. */
13688
13689 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13690 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13691
13692
13693 /* 32-bit instructions with a delay slot. */
13694
13695 static const struct opcode_descriptor jal_insn_32_bd16 =
13696 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13697
13698 static const struct opcode_descriptor jal_insn_32_bd32 =
13699 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13700
13701 static const struct opcode_descriptor jal_x_insn_32_bd32 =
13702 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13703
13704 static const struct opcode_descriptor j_insn_32 =
13705 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13706
13707 static const struct opcode_descriptor jalr_insn_32 =
13708 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13709
13710 /* This table can be compacted, because no opcode replacement is made. */
13711
13712 static const struct opcode_descriptor ds_insns_32_bd16[] = {
13713 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13714
13715 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13716 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13717
13718 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13719 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13720 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13721 { 0, 0 } /* End marker for find_match(). */
13722 };
13723
13724 /* This table can be compacted, because no opcode replacement is made. */
13725
13726 static const struct opcode_descriptor ds_insns_32_bd32[] = {
13727 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13728
13729 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13730 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13731 { 0, 0 } /* End marker for find_match(). */
13732 };
13733
13734
13735 /* 16-bit instructions with a delay slot. */
13736
13737 static const struct opcode_descriptor jalr_insn_16_bd16 =
13738 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13739
13740 static const struct opcode_descriptor jalr_insn_16_bd32 =
13741 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13742
13743 static const struct opcode_descriptor jr_insn_16 =
13744 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13745
13746 #define JR16_REG(opcode) ((opcode) & 0x1f)
13747
13748 /* This table can be compacted, because no opcode replacement is made. */
13749
13750 static const struct opcode_descriptor ds_insns_16_bd16[] = {
13751 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13752
13753 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13754 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13755 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13756 { 0, 0 } /* End marker for find_match(). */
13757 };
13758
13759
13760 /* LUI instruction. */
13761
13762 static const struct opcode_descriptor lui_insn =
13763 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13764
13765
13766 /* ADDIU instruction. */
13767
13768 static const struct opcode_descriptor addiu_insn =
13769 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13770
13771 static const struct opcode_descriptor addiupc_insn =
13772 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13773
13774 #define ADDIUPC_REG_FIELD(r) \
13775 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13776
13777
13778 /* Relaxable instructions in a JAL delay slot: MOVE. */
13779
13780 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13781 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13782 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13783 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13784
13785 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13786 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13787
13788 static const struct opcode_descriptor move_insns_32[] = {
13789 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
13790 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13791 { 0, 0 } /* End marker for find_match(). */
13792 };
13793
13794 static const struct opcode_descriptor move_insn_16 =
13795 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13796
13797
13798 /* NOP instructions. */
13799
13800 static const struct opcode_descriptor nop_insn_32 =
13801 { /* "nop", "", */ 0x00000000, 0xffffffff };
13802
13803 static const struct opcode_descriptor nop_insn_16 =
13804 { /* "nop", "", */ 0x0c00, 0xffff };
13805
13806
13807 /* Instruction match support. */
13808
13809 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13810
13811 static int
13812 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13813 {
13814 unsigned long indx;
13815
13816 for (indx = 0; insn[indx].mask != 0; indx++)
13817 if (MATCH (opcode, insn[indx]))
13818 return indx;
13819
13820 return -1;
13821 }
13822
13823
13824 /* Branch and delay slot decoding support. */
13825
13826 /* If PTR points to what *might* be a 16-bit branch or jump, then
13827 return the minimum length of its delay slot, otherwise return 0.
13828 Non-zero results are not definitive as we might be checking against
13829 the second half of another instruction. */
13830
13831 static int
13832 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13833 {
13834 unsigned long opcode;
13835 int bdsize;
13836
13837 opcode = bfd_get_16 (abfd, ptr);
13838 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13839 /* 16-bit branch/jump with a 32-bit delay slot. */
13840 bdsize = 4;
13841 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13842 || find_match (opcode, ds_insns_16_bd16) >= 0)
13843 /* 16-bit branch/jump with a 16-bit delay slot. */
13844 bdsize = 2;
13845 else
13846 /* No delay slot. */
13847 bdsize = 0;
13848
13849 return bdsize;
13850 }
13851
13852 /* If PTR points to what *might* be a 32-bit branch or jump, then
13853 return the minimum length of its delay slot, otherwise return 0.
13854 Non-zero results are not definitive as we might be checking against
13855 the second half of another instruction. */
13856
13857 static int
13858 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13859 {
13860 unsigned long opcode;
13861 int bdsize;
13862
13863 opcode = bfd_get_micromips_32 (abfd, ptr);
13864 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13865 /* 32-bit branch/jump with a 32-bit delay slot. */
13866 bdsize = 4;
13867 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13868 /* 32-bit branch/jump with a 16-bit delay slot. */
13869 bdsize = 2;
13870 else
13871 /* No delay slot. */
13872 bdsize = 0;
13873
13874 return bdsize;
13875 }
13876
13877 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13878 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13879
13880 static bool
13881 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13882 {
13883 unsigned long opcode;
13884
13885 opcode = bfd_get_16 (abfd, ptr);
13886 if (MATCH (opcode, b_insn_16)
13887 /* B16 */
13888 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13889 /* JR16 */
13890 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13891 /* BEQZ16, BNEZ16 */
13892 || (MATCH (opcode, jalr_insn_16_bd32)
13893 /* JALR16 */
13894 && reg != JR16_REG (opcode) && reg != RA))
13895 return true;
13896
13897 return false;
13898 }
13899
13900 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13901 then return TRUE, otherwise FALSE. */
13902
13903 static bool
13904 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13905 {
13906 unsigned long opcode;
13907
13908 opcode = bfd_get_micromips_32 (abfd, ptr);
13909 if (MATCH (opcode, j_insn_32)
13910 /* J */
13911 || MATCH (opcode, bc_insn_32)
13912 /* BC1F, BC1T, BC2F, BC2T */
13913 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13914 /* JAL, JALX */
13915 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13916 /* BGEZ, BGTZ, BLEZ, BLTZ */
13917 || (MATCH (opcode, bzal_insn_32)
13918 /* BGEZAL, BLTZAL */
13919 && reg != OP32_SREG (opcode) && reg != RA)
13920 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13921 /* JALR, JALR.HB, BEQ, BNE */
13922 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13923 return true;
13924
13925 return false;
13926 }
13927
13928 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13929 IRELEND) at OFFSET indicate that there must be a compact branch there,
13930 then return TRUE, otherwise FALSE. */
13931
13932 static bool
13933 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13934 const Elf_Internal_Rela *internal_relocs,
13935 const Elf_Internal_Rela *irelend)
13936 {
13937 const Elf_Internal_Rela *irel;
13938 unsigned long opcode;
13939
13940 opcode = bfd_get_micromips_32 (abfd, ptr);
13941 if (find_match (opcode, bzc_insns_32) < 0)
13942 return false;
13943
13944 for (irel = internal_relocs; irel < irelend; irel++)
13945 if (irel->r_offset == offset
13946 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13947 return true;
13948
13949 return false;
13950 }
13951
13952 /* Bitsize checking. */
13953 #define IS_BITSIZE(val, N) \
13954 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13955 - (1ULL << ((N) - 1))) == (val))
13956
13957 \f
13958 bool
13959 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13960 struct bfd_link_info *link_info,
13961 bool *again)
13962 {
13963 bool insn32 = mips_elf_hash_table (link_info)->insn32;
13964 Elf_Internal_Shdr *symtab_hdr;
13965 Elf_Internal_Rela *internal_relocs;
13966 Elf_Internal_Rela *irel, *irelend;
13967 bfd_byte *contents = NULL;
13968 Elf_Internal_Sym *isymbuf = NULL;
13969
13970 /* Assume nothing changes. */
13971 *again = false;
13972
13973 /* We don't have to do anything for a relocatable link, if
13974 this section does not have relocs, or if this is not a
13975 code section. */
13976
13977 if (bfd_link_relocatable (link_info)
13978 || (sec->flags & SEC_RELOC) == 0
13979 || sec->reloc_count == 0
13980 || (sec->flags & SEC_CODE) == 0)
13981 return true;
13982
13983 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13984
13985 /* Get a copy of the native relocations. */
13986 internal_relocs = (_bfd_elf_link_read_relocs
13987 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
13988 link_info->keep_memory));
13989 if (internal_relocs == NULL)
13990 goto error_return;
13991
13992 /* Walk through them looking for relaxing opportunities. */
13993 irelend = internal_relocs + sec->reloc_count;
13994 for (irel = internal_relocs; irel < irelend; irel++)
13995 {
13996 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13997 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13998 bool target_is_micromips_code_p;
13999 unsigned long opcode;
14000 bfd_vma symval;
14001 bfd_vma pcrval;
14002 bfd_byte *ptr;
14003 int fndopc;
14004
14005 /* The number of bytes to delete for relaxation and from where
14006 to delete these bytes starting at irel->r_offset. */
14007 int delcnt = 0;
14008 int deloff = 0;
14009
14010 /* If this isn't something that can be relaxed, then ignore
14011 this reloc. */
14012 if (r_type != R_MICROMIPS_HI16
14013 && r_type != R_MICROMIPS_PC16_S1
14014 && r_type != R_MICROMIPS_26_S1)
14015 continue;
14016
14017 /* Get the section contents if we haven't done so already. */
14018 if (contents == NULL)
14019 {
14020 /* Get cached copy if it exists. */
14021 if (elf_section_data (sec)->this_hdr.contents != NULL)
14022 contents = elf_section_data (sec)->this_hdr.contents;
14023 /* Go get them off disk. */
14024 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14025 goto error_return;
14026 }
14027 ptr = contents + irel->r_offset;
14028
14029 /* Read this BFD's local symbols if we haven't done so already. */
14030 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14031 {
14032 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14033 if (isymbuf == NULL)
14034 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14035 symtab_hdr->sh_info, 0,
14036 NULL, NULL, NULL);
14037 if (isymbuf == NULL)
14038 goto error_return;
14039 }
14040
14041 /* Get the value of the symbol referred to by the reloc. */
14042 if (r_symndx < symtab_hdr->sh_info)
14043 {
14044 /* A local symbol. */
14045 Elf_Internal_Sym *isym;
14046 asection *sym_sec;
14047
14048 isym = isymbuf + r_symndx;
14049 if (isym->st_shndx == SHN_UNDEF)
14050 sym_sec = bfd_und_section_ptr;
14051 else if (isym->st_shndx == SHN_ABS)
14052 sym_sec = bfd_abs_section_ptr;
14053 else if (isym->st_shndx == SHN_COMMON)
14054 sym_sec = bfd_com_section_ptr;
14055 else
14056 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14057 symval = (isym->st_value
14058 + sym_sec->output_section->vma
14059 + sym_sec->output_offset);
14060 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14061 }
14062 else
14063 {
14064 unsigned long indx;
14065 struct elf_link_hash_entry *h;
14066
14067 /* An external symbol. */
14068 indx = r_symndx - symtab_hdr->sh_info;
14069 h = elf_sym_hashes (abfd)[indx];
14070 BFD_ASSERT (h != NULL);
14071
14072 if (h->root.type != bfd_link_hash_defined
14073 && h->root.type != bfd_link_hash_defweak)
14074 /* This appears to be a reference to an undefined
14075 symbol. Just ignore it -- it will be caught by the
14076 regular reloc processing. */
14077 continue;
14078
14079 symval = (h->root.u.def.value
14080 + h->root.u.def.section->output_section->vma
14081 + h->root.u.def.section->output_offset);
14082 target_is_micromips_code_p = (!h->needs_plt
14083 && ELF_ST_IS_MICROMIPS (h->other));
14084 }
14085
14086
14087 /* For simplicity of coding, we are going to modify the
14088 section contents, the section relocs, and the BFD symbol
14089 table. We must tell the rest of the code not to free up this
14090 information. It would be possible to instead create a table
14091 of changes which have to be made, as is done in coff-mips.c;
14092 that would be more work, but would require less memory when
14093 the linker is run. */
14094
14095 /* Only 32-bit instructions relaxed. */
14096 if (irel->r_offset + 4 > sec->size)
14097 continue;
14098
14099 opcode = bfd_get_micromips_32 (abfd, ptr);
14100
14101 /* This is the pc-relative distance from the instruction the
14102 relocation is applied to, to the symbol referred. */
14103 pcrval = (symval
14104 - (sec->output_section->vma + sec->output_offset)
14105 - irel->r_offset);
14106
14107 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
14108 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14109 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
14110
14111 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
14112
14113 where pcrval has first to be adjusted to apply against the LO16
14114 location (we make the adjustment later on, when we have figured
14115 out the offset). */
14116 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14117 {
14118 bool bzc = false;
14119 unsigned long nextopc;
14120 unsigned long reg;
14121 bfd_vma offset;
14122
14123 /* Give up if the previous reloc was a HI16 against this symbol
14124 too. */
14125 if (irel > internal_relocs
14126 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14127 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14128 continue;
14129
14130 /* Or if the next reloc is not a LO16 against this symbol. */
14131 if (irel + 1 >= irelend
14132 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14133 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14134 continue;
14135
14136 /* Or if the second next reloc is a LO16 against this symbol too. */
14137 if (irel + 2 >= irelend
14138 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14139 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14140 continue;
14141
14142 /* See if the LUI instruction *might* be in a branch delay slot.
14143 We check whether what looks like a 16-bit branch or jump is
14144 actually an immediate argument to a compact branch, and let
14145 it through if so. */
14146 if (irel->r_offset >= 2
14147 && check_br16_dslot (abfd, ptr - 2)
14148 && !(irel->r_offset >= 4
14149 && (bzc = check_relocated_bzc (abfd,
14150 ptr - 4, irel->r_offset - 4,
14151 internal_relocs, irelend))))
14152 continue;
14153 if (irel->r_offset >= 4
14154 && !bzc
14155 && check_br32_dslot (abfd, ptr - 4))
14156 continue;
14157
14158 reg = OP32_SREG (opcode);
14159
14160 /* We only relax adjacent instructions or ones separated with
14161 a branch or jump that has a delay slot. The branch or jump
14162 must not fiddle with the register used to hold the address.
14163 Subtract 4 for the LUI itself. */
14164 offset = irel[1].r_offset - irel[0].r_offset;
14165 switch (offset - 4)
14166 {
14167 case 0:
14168 break;
14169 case 2:
14170 if (check_br16 (abfd, ptr + 4, reg))
14171 break;
14172 continue;
14173 case 4:
14174 if (check_br32 (abfd, ptr + 4, reg))
14175 break;
14176 continue;
14177 default:
14178 continue;
14179 }
14180
14181 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14182
14183 /* Give up unless the same register is used with both
14184 relocations. */
14185 if (OP32_SREG (nextopc) != reg)
14186 continue;
14187
14188 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14189 and rounding up to take masking of the two LSBs into account. */
14190 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14191
14192 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
14193 if (IS_BITSIZE (symval, 16))
14194 {
14195 /* Fix the relocation's type. */
14196 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14197
14198 /* Instructions using R_MICROMIPS_LO16 have the base or
14199 source register in bits 20:16. This register becomes $0
14200 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
14201 nextopc &= ~0x001f0000;
14202 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14203 contents + irel[1].r_offset);
14204 }
14205
14206 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14207 We add 4 to take LUI deletion into account while checking
14208 the PC-relative distance. */
14209 else if (symval % 4 == 0
14210 && IS_BITSIZE (pcrval + 4, 25)
14211 && MATCH (nextopc, addiu_insn)
14212 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14213 && OP16_VALID_REG (OP32_TREG (nextopc)))
14214 {
14215 /* Fix the relocation's type. */
14216 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14217
14218 /* Replace ADDIU with the ADDIUPC version. */
14219 nextopc = (addiupc_insn.match
14220 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14221
14222 bfd_put_micromips_32 (abfd, nextopc,
14223 contents + irel[1].r_offset);
14224 }
14225
14226 /* Can't do anything, give up, sigh... */
14227 else
14228 continue;
14229
14230 /* Fix the relocation's type. */
14231 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14232
14233 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
14234 delcnt = 4;
14235 deloff = 0;
14236 }
14237
14238 /* Compact branch relaxation -- due to the multitude of macros
14239 employed by the compiler/assembler, compact branches are not
14240 always generated. Obviously, this can/will be fixed elsewhere,
14241 but there is no drawback in double checking it here. */
14242 else if (r_type == R_MICROMIPS_PC16_S1
14243 && irel->r_offset + 5 < sec->size
14244 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14245 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14246 && ((!insn32
14247 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14248 nop_insn_16) ? 2 : 0))
14249 || (irel->r_offset + 7 < sec->size
14250 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14251 ptr + 4),
14252 nop_insn_32) ? 4 : 0))))
14253 {
14254 unsigned long reg;
14255
14256 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14257
14258 /* Replace BEQZ/BNEZ with the compact version. */
14259 opcode = (bzc_insns_32[fndopc].match
14260 | BZC32_REG_FIELD (reg)
14261 | (opcode & 0xffff)); /* Addend value. */
14262
14263 bfd_put_micromips_32 (abfd, opcode, ptr);
14264
14265 /* Delete the delay slot NOP: two or four bytes from
14266 irel->offset + 4; delcnt has already been set above. */
14267 deloff = 4;
14268 }
14269
14270 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
14271 to check the distance from the next instruction, so subtract 2. */
14272 else if (!insn32
14273 && r_type == R_MICROMIPS_PC16_S1
14274 && IS_BITSIZE (pcrval - 2, 11)
14275 && find_match (opcode, b_insns_32) >= 0)
14276 {
14277 /* Fix the relocation's type. */
14278 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14279
14280 /* Replace the 32-bit opcode with a 16-bit opcode. */
14281 bfd_put_16 (abfd,
14282 (b_insn_16.match
14283 | (opcode & 0x3ff)), /* Addend value. */
14284 ptr);
14285
14286 /* Delete 2 bytes from irel->r_offset + 2. */
14287 delcnt = 2;
14288 deloff = 2;
14289 }
14290
14291 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
14292 to check the distance from the next instruction, so subtract 2. */
14293 else if (!insn32
14294 && r_type == R_MICROMIPS_PC16_S1
14295 && IS_BITSIZE (pcrval - 2, 8)
14296 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14297 && OP16_VALID_REG (OP32_SREG (opcode)))
14298 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14299 && OP16_VALID_REG (OP32_TREG (opcode)))))
14300 {
14301 unsigned long reg;
14302
14303 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14304
14305 /* Fix the relocation's type. */
14306 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14307
14308 /* Replace the 32-bit opcode with a 16-bit opcode. */
14309 bfd_put_16 (abfd,
14310 (bz_insns_16[fndopc].match
14311 | BZ16_REG_FIELD (reg)
14312 | (opcode & 0x7f)), /* Addend value. */
14313 ptr);
14314
14315 /* Delete 2 bytes from irel->r_offset + 2. */
14316 delcnt = 2;
14317 deloff = 2;
14318 }
14319
14320 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
14321 else if (!insn32
14322 && r_type == R_MICROMIPS_26_S1
14323 && target_is_micromips_code_p
14324 && irel->r_offset + 7 < sec->size
14325 && MATCH (opcode, jal_insn_32_bd32))
14326 {
14327 unsigned long n32opc;
14328 bool relaxed = false;
14329
14330 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14331
14332 if (MATCH (n32opc, nop_insn_32))
14333 {
14334 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
14335 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14336
14337 relaxed = true;
14338 }
14339 else if (find_match (n32opc, move_insns_32) >= 0)
14340 {
14341 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14342 bfd_put_16 (abfd,
14343 (move_insn_16.match
14344 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14345 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14346 ptr + 4);
14347
14348 relaxed = true;
14349 }
14350 /* Other 32-bit instructions relaxable to 16-bit
14351 instructions will be handled here later. */
14352
14353 if (relaxed)
14354 {
14355 /* JAL with 32-bit delay slot that is changed to a JALS
14356 with 16-bit delay slot. */
14357 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14358
14359 /* Delete 2 bytes from irel->r_offset + 6. */
14360 delcnt = 2;
14361 deloff = 6;
14362 }
14363 }
14364
14365 if (delcnt != 0)
14366 {
14367 /* Note that we've changed the relocs, section contents, etc. */
14368 elf_section_data (sec)->relocs = internal_relocs;
14369 elf_section_data (sec)->this_hdr.contents = contents;
14370 symtab_hdr->contents = (unsigned char *) isymbuf;
14371
14372 /* Delete bytes depending on the delcnt and deloff. */
14373 if (!mips_elf_relax_delete_bytes (abfd, sec,
14374 irel->r_offset + deloff, delcnt))
14375 goto error_return;
14376
14377 /* That will change things, so we should relax again.
14378 Note that this is not required, and it may be slow. */
14379 *again = true;
14380 }
14381 }
14382
14383 if (isymbuf != NULL
14384 && symtab_hdr->contents != (unsigned char *) isymbuf)
14385 {
14386 if (! link_info->keep_memory)
14387 free (isymbuf);
14388 else
14389 {
14390 /* Cache the symbols for elf_link_input_bfd. */
14391 symtab_hdr->contents = (unsigned char *) isymbuf;
14392 }
14393 }
14394
14395 if (contents != NULL
14396 && elf_section_data (sec)->this_hdr.contents != contents)
14397 {
14398 if (! link_info->keep_memory)
14399 free (contents);
14400 else
14401 {
14402 /* Cache the section contents for elf_link_input_bfd. */
14403 elf_section_data (sec)->this_hdr.contents = contents;
14404 }
14405 }
14406
14407 if (elf_section_data (sec)->relocs != internal_relocs)
14408 free (internal_relocs);
14409
14410 return true;
14411
14412 error_return:
14413 if (symtab_hdr->contents != (unsigned char *) isymbuf)
14414 free (isymbuf);
14415 if (elf_section_data (sec)->this_hdr.contents != contents)
14416 free (contents);
14417 if (elf_section_data (sec)->relocs != internal_relocs)
14418 free (internal_relocs);
14419
14420 return false;
14421 }
14422 \f
14423 /* Create a MIPS ELF linker hash table. */
14424
14425 struct bfd_link_hash_table *
14426 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
14427 {
14428 struct mips_elf_link_hash_table *ret;
14429 size_t amt = sizeof (struct mips_elf_link_hash_table);
14430
14431 ret = bfd_zmalloc (amt);
14432 if (ret == NULL)
14433 return NULL;
14434
14435 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14436 mips_elf_link_hash_newfunc,
14437 sizeof (struct mips_elf_link_hash_entry),
14438 MIPS_ELF_DATA))
14439 {
14440 free (ret);
14441 return NULL;
14442 }
14443 ret->root.init_plt_refcount.plist = NULL;
14444 ret->root.init_plt_offset.plist = NULL;
14445
14446 return &ret->root.root;
14447 }
14448
14449 /* Likewise, but indicate that the target is VxWorks. */
14450
14451 struct bfd_link_hash_table *
14452 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14453 {
14454 struct bfd_link_hash_table *ret;
14455
14456 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14457 if (ret)
14458 {
14459 struct mips_elf_link_hash_table *htab;
14460
14461 htab = (struct mips_elf_link_hash_table *) ret;
14462 htab->use_plts_and_copy_relocs = true;
14463 }
14464 return ret;
14465 }
14466
14467 /* A function that the linker calls if we are allowed to use PLTs
14468 and copy relocs. */
14469
14470 void
14471 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14472 {
14473 mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
14474 }
14475
14476 /* A function that the linker calls to select between all or only
14477 32-bit microMIPS instructions, and between making or ignoring
14478 branch relocation checks for invalid transitions between ISA modes.
14479 Also record whether we have been configured for a GNU target. */
14480
14481 void
14482 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14483 bool ignore_branch_isa,
14484 bool gnu_target)
14485 {
14486 mips_elf_hash_table (info)->insn32 = insn32;
14487 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14488 mips_elf_hash_table (info)->gnu_target = gnu_target;
14489 }
14490
14491 /* A function that the linker calls to enable use of compact branches in
14492 linker generated code for MIPSR6. */
14493
14494 void
14495 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
14496 {
14497 mips_elf_hash_table (info)->compact_branches = on;
14498 }
14499
14500 \f
14501 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14502
14503 struct mips_mach_extension
14504 {
14505 unsigned long extension, base;
14506 };
14507
14508
14509 /* An array describing how BFD machines relate to one another. The entries
14510 are ordered topologically with MIPS I extensions listed last. */
14511
14512 static const struct mips_mach_extension mips_mach_extensions[] =
14513 {
14514 /* MIPS64r2 extensions. */
14515 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14516 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14517 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14518 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14519 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14520 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14521 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14522
14523 /* MIPS64 extensions. */
14524 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14525 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14526 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14527
14528 /* MIPS V extensions. */
14529 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14530
14531 /* R10000 extensions. */
14532 { bfd_mach_mips12000, bfd_mach_mips10000 },
14533 { bfd_mach_mips14000, bfd_mach_mips10000 },
14534 { bfd_mach_mips16000, bfd_mach_mips10000 },
14535
14536 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14537 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14538 better to allow vr5400 and vr5500 code to be merged anyway, since
14539 many libraries will just use the core ISA. Perhaps we could add
14540 some sort of ASE flag if this ever proves a problem. */
14541 { bfd_mach_mips5500, bfd_mach_mips5400 },
14542 { bfd_mach_mips5400, bfd_mach_mips5000 },
14543
14544 /* MIPS IV extensions. */
14545 { bfd_mach_mips5, bfd_mach_mips8000 },
14546 { bfd_mach_mips10000, bfd_mach_mips8000 },
14547 { bfd_mach_mips5000, bfd_mach_mips8000 },
14548 { bfd_mach_mips7000, bfd_mach_mips8000 },
14549 { bfd_mach_mips9000, bfd_mach_mips8000 },
14550
14551 /* VR4100 extensions. */
14552 { bfd_mach_mips4120, bfd_mach_mips4100 },
14553 { bfd_mach_mips4111, bfd_mach_mips4100 },
14554
14555 /* MIPS III extensions. */
14556 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14557 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14558 { bfd_mach_mips8000, bfd_mach_mips4000 },
14559 { bfd_mach_mips4650, bfd_mach_mips4000 },
14560 { bfd_mach_mips4600, bfd_mach_mips4000 },
14561 { bfd_mach_mips4400, bfd_mach_mips4000 },
14562 { bfd_mach_mips4300, bfd_mach_mips4000 },
14563 { bfd_mach_mips4100, bfd_mach_mips4000 },
14564 { bfd_mach_mips5900, bfd_mach_mips4000 },
14565
14566 /* MIPS32r3 extensions. */
14567 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14568
14569 /* MIPS32r2 extensions. */
14570 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14571
14572 /* MIPS32 extensions. */
14573 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14574
14575 /* MIPS II extensions. */
14576 { bfd_mach_mips4000, bfd_mach_mips6000 },
14577 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14578 { bfd_mach_mips4010, bfd_mach_mips6000 },
14579
14580 /* MIPS I extensions. */
14581 { bfd_mach_mips6000, bfd_mach_mips3000 },
14582 { bfd_mach_mips3900, bfd_mach_mips3000 }
14583 };
14584
14585 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14586
14587 static bool
14588 mips_mach_extends_p (unsigned long base, unsigned long extension)
14589 {
14590 size_t i;
14591
14592 if (extension == base)
14593 return true;
14594
14595 if (base == bfd_mach_mipsisa32
14596 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14597 return true;
14598
14599 if (base == bfd_mach_mipsisa32r2
14600 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14601 return true;
14602
14603 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14604 if (extension == mips_mach_extensions[i].extension)
14605 {
14606 extension = mips_mach_extensions[i].base;
14607 if (extension == base)
14608 return true;
14609 }
14610
14611 return false;
14612 }
14613
14614 /* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14615
14616 static unsigned long
14617 bfd_mips_isa_ext_mach (unsigned int isa_ext)
14618 {
14619 switch (isa_ext)
14620 {
14621 case AFL_EXT_3900: return bfd_mach_mips3900;
14622 case AFL_EXT_4010: return bfd_mach_mips4010;
14623 case AFL_EXT_4100: return bfd_mach_mips4100;
14624 case AFL_EXT_4111: return bfd_mach_mips4111;
14625 case AFL_EXT_4120: return bfd_mach_mips4120;
14626 case AFL_EXT_4650: return bfd_mach_mips4650;
14627 case AFL_EXT_5400: return bfd_mach_mips5400;
14628 case AFL_EXT_5500: return bfd_mach_mips5500;
14629 case AFL_EXT_5900: return bfd_mach_mips5900;
14630 case AFL_EXT_10000: return bfd_mach_mips10000;
14631 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14632 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14633 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
14634 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14635 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14636 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
14637 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14638 default: return bfd_mach_mips3000;
14639 }
14640 }
14641
14642 /* Return the .MIPS.abiflags value representing each ISA Extension. */
14643
14644 unsigned int
14645 bfd_mips_isa_ext (bfd *abfd)
14646 {
14647 switch (bfd_get_mach (abfd))
14648 {
14649 case bfd_mach_mips3900: return AFL_EXT_3900;
14650 case bfd_mach_mips4010: return AFL_EXT_4010;
14651 case bfd_mach_mips4100: return AFL_EXT_4100;
14652 case bfd_mach_mips4111: return AFL_EXT_4111;
14653 case bfd_mach_mips4120: return AFL_EXT_4120;
14654 case bfd_mach_mips4650: return AFL_EXT_4650;
14655 case bfd_mach_mips5400: return AFL_EXT_5400;
14656 case bfd_mach_mips5500: return AFL_EXT_5500;
14657 case bfd_mach_mips5900: return AFL_EXT_5900;
14658 case bfd_mach_mips10000: return AFL_EXT_10000;
14659 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14660 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14661 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14662 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14663 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14664 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14665 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14666 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
14667 case bfd_mach_mips_interaptiv_mr2:
14668 return AFL_EXT_INTERAPTIV_MR2;
14669 default: return 0;
14670 }
14671 }
14672
14673 /* Encode ISA level and revision as a single value. */
14674 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14675
14676 /* Decode a single value into level and revision. */
14677 #define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14678 #define ISA_REV(LEVREV) ((LEVREV) & 0x7)
14679
14680 /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14681
14682 static void
14683 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14684 {
14685 int new_isa = 0;
14686 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14687 {
14688 case E_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14689 case E_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14690 case E_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14691 case E_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14692 case E_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14693 case E_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14694 case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14695 case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14696 case E_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14697 case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14698 case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14699 default:
14700 _bfd_error_handler
14701 /* xgettext:c-format */
14702 (_("%pB: unknown architecture %s"),
14703 abfd, bfd_printable_name (abfd));
14704 }
14705
14706 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14707 {
14708 abiflags->isa_level = ISA_LEVEL (new_isa);
14709 abiflags->isa_rev = ISA_REV (new_isa);
14710 }
14711
14712 /* Update the isa_ext if ABFD describes a further extension. */
14713 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14714 bfd_get_mach (abfd)))
14715 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14716 }
14717
14718 /* Return true if the given ELF header flags describe a 32-bit binary. */
14719
14720 static bool
14721 mips_32bit_flags_p (flagword flags)
14722 {
14723 return ((flags & EF_MIPS_32BITMODE) != 0
14724 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14725 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14726 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14727 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14728 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14729 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14730 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
14731 }
14732
14733 /* Infer the content of the ABI flags based on the elf header. */
14734
14735 static void
14736 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14737 {
14738 obj_attribute *in_attr;
14739
14740 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14741 update_mips_abiflags_isa (abfd, abiflags);
14742
14743 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14744 abiflags->gpr_size = AFL_REG_32;
14745 else
14746 abiflags->gpr_size = AFL_REG_64;
14747
14748 abiflags->cpr1_size = AFL_REG_NONE;
14749
14750 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14751 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14752
14753 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14754 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14755 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14756 && abiflags->gpr_size == AFL_REG_32))
14757 abiflags->cpr1_size = AFL_REG_32;
14758 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14759 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14760 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14761 abiflags->cpr1_size = AFL_REG_64;
14762
14763 abiflags->cpr2_size = AFL_REG_NONE;
14764
14765 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14766 abiflags->ases |= AFL_ASE_MDMX;
14767 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14768 abiflags->ases |= AFL_ASE_MIPS16;
14769 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14770 abiflags->ases |= AFL_ASE_MICROMIPS;
14771
14772 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14773 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14774 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14775 && abiflags->isa_level >= 32
14776 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14777 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14778 }
14779
14780 /* We need to use a special link routine to handle the .reginfo and
14781 the .mdebug sections. We need to merge all instances of these
14782 sections together, not write them all out sequentially. */
14783
14784 bool
14785 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14786 {
14787 asection *o;
14788 struct bfd_link_order *p;
14789 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14790 asection *rtproc_sec, *abiflags_sec;
14791 Elf32_RegInfo reginfo;
14792 struct ecoff_debug_info debug;
14793 struct mips_htab_traverse_info hti;
14794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14795 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14796 HDRR *symhdr = &debug.symbolic_header;
14797 void *mdebug_handle = NULL;
14798 asection *s;
14799 EXTR esym;
14800 unsigned int i;
14801 bfd_size_type amt;
14802 struct mips_elf_link_hash_table *htab;
14803
14804 static const char * const secname[] =
14805 {
14806 ".text", ".init", ".fini", ".data",
14807 ".rodata", ".sdata", ".sbss", ".bss"
14808 };
14809 static const int sc[] =
14810 {
14811 scText, scInit, scFini, scData,
14812 scRData, scSData, scSBss, scBss
14813 };
14814
14815 htab = mips_elf_hash_table (info);
14816 BFD_ASSERT (htab != NULL);
14817
14818 /* Sort the dynamic symbols so that those with GOT entries come after
14819 those without. */
14820 if (!mips_elf_sort_hash_table (abfd, info))
14821 return false;
14822
14823 /* Create any scheduled LA25 stubs. */
14824 hti.info = info;
14825 hti.output_bfd = abfd;
14826 hti.error = false;
14827 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14828 if (hti.error)
14829 return false;
14830
14831 /* Get a value for the GP register. */
14832 if (elf_gp (abfd) == 0)
14833 {
14834 struct bfd_link_hash_entry *h;
14835
14836 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
14837 if (h != NULL && h->type == bfd_link_hash_defined)
14838 elf_gp (abfd) = (h->u.def.value
14839 + h->u.def.section->output_section->vma
14840 + h->u.def.section->output_offset);
14841 else if (htab->root.target_os == is_vxworks
14842 && (h = bfd_link_hash_lookup (info->hash,
14843 "_GLOBAL_OFFSET_TABLE_",
14844 false, false, true))
14845 && h->type == bfd_link_hash_defined)
14846 elf_gp (abfd) = (h->u.def.section->output_section->vma
14847 + h->u.def.section->output_offset
14848 + h->u.def.value);
14849 else if (bfd_link_relocatable (info))
14850 {
14851 bfd_vma lo = MINUS_ONE;
14852
14853 /* Find the GP-relative section with the lowest offset. */
14854 for (o = abfd->sections; o != NULL; o = o->next)
14855 if (o->vma < lo
14856 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14857 lo = o->vma;
14858
14859 /* And calculate GP relative to that. */
14860 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14861 }
14862 else
14863 {
14864 /* If the relocate_section function needs to do a reloc
14865 involving the GP value, it should make a reloc_dangerous
14866 callback to warn that GP is not defined. */
14867 }
14868 }
14869
14870 /* Go through the sections and collect the .reginfo and .mdebug
14871 information. */
14872 abiflags_sec = NULL;
14873 reginfo_sec = NULL;
14874 mdebug_sec = NULL;
14875 gptab_data_sec = NULL;
14876 gptab_bss_sec = NULL;
14877 for (o = abfd->sections; o != NULL; o = o->next)
14878 {
14879 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14880 {
14881 /* We have found the .MIPS.abiflags section in the output file.
14882 Look through all the link_orders comprising it and remove them.
14883 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14884 for (p = o->map_head.link_order; p != NULL; p = p->next)
14885 {
14886 asection *input_section;
14887
14888 if (p->type != bfd_indirect_link_order)
14889 {
14890 if (p->type == bfd_data_link_order)
14891 continue;
14892 abort ();
14893 }
14894
14895 input_section = p->u.indirect.section;
14896
14897 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14898 elf_link_input_bfd ignores this section. */
14899 input_section->flags &= ~SEC_HAS_CONTENTS;
14900 }
14901
14902 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14903 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14904
14905 /* Skip this section later on (I don't think this currently
14906 matters, but someday it might). */
14907 o->map_head.link_order = NULL;
14908
14909 abiflags_sec = o;
14910 }
14911
14912 if (strcmp (o->name, ".reginfo") == 0)
14913 {
14914 memset (&reginfo, 0, sizeof reginfo);
14915
14916 /* We have found the .reginfo section in the output file.
14917 Look through all the link_orders comprising it and merge
14918 the information together. */
14919 for (p = o->map_head.link_order; p != NULL; p = p->next)
14920 {
14921 asection *input_section;
14922 bfd *input_bfd;
14923 Elf32_External_RegInfo ext;
14924 Elf32_RegInfo sub;
14925 bfd_size_type sz;
14926
14927 if (p->type != bfd_indirect_link_order)
14928 {
14929 if (p->type == bfd_data_link_order)
14930 continue;
14931 abort ();
14932 }
14933
14934 input_section = p->u.indirect.section;
14935 input_bfd = input_section->owner;
14936
14937 sz = (input_section->size < sizeof (ext)
14938 ? input_section->size : sizeof (ext));
14939 memset (&ext, 0, sizeof (ext));
14940 if (! bfd_get_section_contents (input_bfd, input_section,
14941 &ext, 0, sz))
14942 return false;
14943
14944 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14945
14946 reginfo.ri_gprmask |= sub.ri_gprmask;
14947 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14948 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14949 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14950 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14951
14952 /* ri_gp_value is set by the function
14953 `_bfd_mips_elf_section_processing' when the section is
14954 finally written out. */
14955
14956 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14957 elf_link_input_bfd ignores this section. */
14958 input_section->flags &= ~SEC_HAS_CONTENTS;
14959 }
14960
14961 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14962 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14963
14964 /* Skip this section later on (I don't think this currently
14965 matters, but someday it might). */
14966 o->map_head.link_order = NULL;
14967
14968 reginfo_sec = o;
14969 }
14970
14971 if (strcmp (o->name, ".mdebug") == 0)
14972 {
14973 struct extsym_info einfo;
14974 bfd_vma last;
14975
14976 /* We have found the .mdebug section in the output file.
14977 Look through all the link_orders comprising it and merge
14978 the information together. */
14979 symhdr->magic = swap->sym_magic;
14980 /* FIXME: What should the version stamp be? */
14981 symhdr->vstamp = 0;
14982 symhdr->ilineMax = 0;
14983 symhdr->cbLine = 0;
14984 symhdr->idnMax = 0;
14985 symhdr->ipdMax = 0;
14986 symhdr->isymMax = 0;
14987 symhdr->ioptMax = 0;
14988 symhdr->iauxMax = 0;
14989 symhdr->issMax = 0;
14990 symhdr->issExtMax = 0;
14991 symhdr->ifdMax = 0;
14992 symhdr->crfd = 0;
14993 symhdr->iextMax = 0;
14994
14995 /* We accumulate the debugging information itself in the
14996 debug_info structure. */
14997 debug.line = NULL;
14998 debug.external_dnr = NULL;
14999 debug.external_pdr = NULL;
15000 debug.external_sym = NULL;
15001 debug.external_opt = NULL;
15002 debug.external_aux = NULL;
15003 debug.ss = NULL;
15004 debug.ssext = debug.ssext_end = NULL;
15005 debug.external_fdr = NULL;
15006 debug.external_rfd = NULL;
15007 debug.external_ext = debug.external_ext_end = NULL;
15008
15009 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
15010 if (mdebug_handle == NULL)
15011 return false;
15012
15013 esym.jmptbl = 0;
15014 esym.cobol_main = 0;
15015 esym.weakext = 0;
15016 esym.reserved = 0;
15017 esym.ifd = ifdNil;
15018 esym.asym.iss = issNil;
15019 esym.asym.st = stLocal;
15020 esym.asym.reserved = 0;
15021 esym.asym.index = indexNil;
15022 last = 0;
15023 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15024 {
15025 esym.asym.sc = sc[i];
15026 s = bfd_get_section_by_name (abfd, secname[i]);
15027 if (s != NULL)
15028 {
15029 esym.asym.value = s->vma;
15030 last = s->vma + s->size;
15031 }
15032 else
15033 esym.asym.value = last;
15034 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15035 secname[i], &esym))
15036 return false;
15037 }
15038
15039 for (p = o->map_head.link_order; p != NULL; p = p->next)
15040 {
15041 asection *input_section;
15042 bfd *input_bfd;
15043 const struct ecoff_debug_swap *input_swap;
15044 struct ecoff_debug_info input_debug;
15045 char *eraw_src;
15046 char *eraw_end;
15047
15048 if (p->type != bfd_indirect_link_order)
15049 {
15050 if (p->type == bfd_data_link_order)
15051 continue;
15052 abort ();
15053 }
15054
15055 input_section = p->u.indirect.section;
15056 input_bfd = input_section->owner;
15057
15058 if (!is_mips_elf (input_bfd))
15059 {
15060 /* I don't know what a non MIPS ELF bfd would be
15061 doing with a .mdebug section, but I don't really
15062 want to deal with it. */
15063 continue;
15064 }
15065
15066 input_swap = (get_elf_backend_data (input_bfd)
15067 ->elf_backend_ecoff_debug_swap);
15068
15069 BFD_ASSERT (p->size == input_section->size);
15070
15071 /* The ECOFF linking code expects that we have already
15072 read in the debugging information and set up an
15073 ecoff_debug_info structure, so we do that now. */
15074 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15075 &input_debug))
15076 return false;
15077
15078 if (! (bfd_ecoff_debug_accumulate
15079 (mdebug_handle, abfd, &debug, swap, input_bfd,
15080 &input_debug, input_swap, info)))
15081 return false;
15082
15083 /* Loop through the external symbols. For each one with
15084 interesting information, try to find the symbol in
15085 the linker global hash table and save the information
15086 for the output external symbols. */
15087 eraw_src = input_debug.external_ext;
15088 eraw_end = (eraw_src
15089 + (input_debug.symbolic_header.iextMax
15090 * input_swap->external_ext_size));
15091 for (;
15092 eraw_src < eraw_end;
15093 eraw_src += input_swap->external_ext_size)
15094 {
15095 EXTR ext;
15096 const char *name;
15097 struct mips_elf_link_hash_entry *h;
15098
15099 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
15100 if (ext.asym.sc == scNil
15101 || ext.asym.sc == scUndefined
15102 || ext.asym.sc == scSUndefined)
15103 continue;
15104
15105 name = input_debug.ssext + ext.asym.iss;
15106 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
15107 name, false, false, true);
15108 if (h == NULL || h->esym.ifd != -2)
15109 continue;
15110
15111 if (ext.ifd != -1)
15112 {
15113 BFD_ASSERT (ext.ifd
15114 < input_debug.symbolic_header.ifdMax);
15115 ext.ifd = input_debug.ifdmap[ext.ifd];
15116 }
15117
15118 h->esym = ext;
15119 }
15120
15121 /* Free up the information we just read. */
15122 free (input_debug.line);
15123 free (input_debug.external_dnr);
15124 free (input_debug.external_pdr);
15125 free (input_debug.external_sym);
15126 free (input_debug.external_opt);
15127 free (input_debug.external_aux);
15128 free (input_debug.ss);
15129 free (input_debug.ssext);
15130 free (input_debug.external_fdr);
15131 free (input_debug.external_rfd);
15132 free (input_debug.external_ext);
15133
15134 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15135 elf_link_input_bfd ignores this section. */
15136 input_section->flags &= ~SEC_HAS_CONTENTS;
15137 }
15138
15139 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
15140 {
15141 /* Create .rtproc section. */
15142 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
15143 if (rtproc_sec == NULL)
15144 {
15145 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15146 | SEC_LINKER_CREATED | SEC_READONLY);
15147
15148 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15149 ".rtproc",
15150 flags);
15151 if (rtproc_sec == NULL
15152 || !bfd_set_section_alignment (rtproc_sec, 4))
15153 return false;
15154 }
15155
15156 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15157 info, rtproc_sec,
15158 &debug))
15159 return false;
15160 }
15161
15162 /* Build the external symbol information. */
15163 einfo.abfd = abfd;
15164 einfo.info = info;
15165 einfo.debug = &debug;
15166 einfo.swap = swap;
15167 einfo.failed = false;
15168 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15169 mips_elf_output_extsym, &einfo);
15170 if (einfo.failed)
15171 return false;
15172
15173 /* Set the size of the .mdebug section. */
15174 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15175
15176 /* Skip this section later on (I don't think this currently
15177 matters, but someday it might). */
15178 o->map_head.link_order = NULL;
15179
15180 mdebug_sec = o;
15181 }
15182
15183 if (startswith (o->name, ".gptab."))
15184 {
15185 const char *subname;
15186 unsigned int c;
15187 Elf32_gptab *tab;
15188 Elf32_External_gptab *ext_tab;
15189 unsigned int j;
15190
15191 /* The .gptab.sdata and .gptab.sbss sections hold
15192 information describing how the small data area would
15193 change depending upon the -G switch. These sections
15194 not used in executables files. */
15195 if (! bfd_link_relocatable (info))
15196 {
15197 for (p = o->map_head.link_order; p != NULL; p = p->next)
15198 {
15199 asection *input_section;
15200
15201 if (p->type != bfd_indirect_link_order)
15202 {
15203 if (p->type == bfd_data_link_order)
15204 continue;
15205 abort ();
15206 }
15207
15208 input_section = p->u.indirect.section;
15209
15210 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15211 elf_link_input_bfd ignores this section. */
15212 input_section->flags &= ~SEC_HAS_CONTENTS;
15213 }
15214
15215 /* Skip this section later on (I don't think this
15216 currently matters, but someday it might). */
15217 o->map_head.link_order = NULL;
15218
15219 /* Really remove the section. */
15220 bfd_section_list_remove (abfd, o);
15221 --abfd->section_count;
15222
15223 continue;
15224 }
15225
15226 /* There is one gptab for initialized data, and one for
15227 uninitialized data. */
15228 if (strcmp (o->name, ".gptab.sdata") == 0)
15229 gptab_data_sec = o;
15230 else if (strcmp (o->name, ".gptab.sbss") == 0)
15231 gptab_bss_sec = o;
15232 else
15233 {
15234 _bfd_error_handler
15235 /* xgettext:c-format */
15236 (_("%pB: illegal section name `%pA'"), abfd, o);
15237 bfd_set_error (bfd_error_nonrepresentable_section);
15238 return false;
15239 }
15240
15241 /* The linker script always combines .gptab.data and
15242 .gptab.sdata into .gptab.sdata, and likewise for
15243 .gptab.bss and .gptab.sbss. It is possible that there is
15244 no .sdata or .sbss section in the output file, in which
15245 case we must change the name of the output section. */
15246 subname = o->name + sizeof ".gptab" - 1;
15247 if (bfd_get_section_by_name (abfd, subname) == NULL)
15248 {
15249 if (o == gptab_data_sec)
15250 o->name = ".gptab.data";
15251 else
15252 o->name = ".gptab.bss";
15253 subname = o->name + sizeof ".gptab" - 1;
15254 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15255 }
15256
15257 /* Set up the first entry. */
15258 c = 1;
15259 amt = c * sizeof (Elf32_gptab);
15260 tab = bfd_malloc (amt);
15261 if (tab == NULL)
15262 return false;
15263 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15264 tab[0].gt_header.gt_unused = 0;
15265
15266 /* Combine the input sections. */
15267 for (p = o->map_head.link_order; p != NULL; p = p->next)
15268 {
15269 asection *input_section;
15270 bfd *input_bfd;
15271 bfd_size_type size;
15272 unsigned long last;
15273 bfd_size_type gpentry;
15274
15275 if (p->type != bfd_indirect_link_order)
15276 {
15277 if (p->type == bfd_data_link_order)
15278 continue;
15279 abort ();
15280 }
15281
15282 input_section = p->u.indirect.section;
15283 input_bfd = input_section->owner;
15284
15285 /* Combine the gptab entries for this input section one
15286 by one. We know that the input gptab entries are
15287 sorted by ascending -G value. */
15288 size = input_section->size;
15289 last = 0;
15290 for (gpentry = sizeof (Elf32_External_gptab);
15291 gpentry < size;
15292 gpentry += sizeof (Elf32_External_gptab))
15293 {
15294 Elf32_External_gptab ext_gptab;
15295 Elf32_gptab int_gptab;
15296 unsigned long val;
15297 unsigned long add;
15298 bool exact;
15299 unsigned int look;
15300
15301 if (! (bfd_get_section_contents
15302 (input_bfd, input_section, &ext_gptab, gpentry,
15303 sizeof (Elf32_External_gptab))))
15304 {
15305 free (tab);
15306 return false;
15307 }
15308
15309 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15310 &int_gptab);
15311 val = int_gptab.gt_entry.gt_g_value;
15312 add = int_gptab.gt_entry.gt_bytes - last;
15313
15314 exact = false;
15315 for (look = 1; look < c; look++)
15316 {
15317 if (tab[look].gt_entry.gt_g_value >= val)
15318 tab[look].gt_entry.gt_bytes += add;
15319
15320 if (tab[look].gt_entry.gt_g_value == val)
15321 exact = true;
15322 }
15323
15324 if (! exact)
15325 {
15326 Elf32_gptab *new_tab;
15327 unsigned int max;
15328
15329 /* We need a new table entry. */
15330 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15331 new_tab = bfd_realloc (tab, amt);
15332 if (new_tab == NULL)
15333 {
15334 free (tab);
15335 return false;
15336 }
15337 tab = new_tab;
15338 tab[c].gt_entry.gt_g_value = val;
15339 tab[c].gt_entry.gt_bytes = add;
15340
15341 /* Merge in the size for the next smallest -G
15342 value, since that will be implied by this new
15343 value. */
15344 max = 0;
15345 for (look = 1; look < c; look++)
15346 {
15347 if (tab[look].gt_entry.gt_g_value < val
15348 && (max == 0
15349 || (tab[look].gt_entry.gt_g_value
15350 > tab[max].gt_entry.gt_g_value)))
15351 max = look;
15352 }
15353 if (max != 0)
15354 tab[c].gt_entry.gt_bytes +=
15355 tab[max].gt_entry.gt_bytes;
15356
15357 ++c;
15358 }
15359
15360 last = int_gptab.gt_entry.gt_bytes;
15361 }
15362
15363 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15364 elf_link_input_bfd ignores this section. */
15365 input_section->flags &= ~SEC_HAS_CONTENTS;
15366 }
15367
15368 /* The table must be sorted by -G value. */
15369 if (c > 2)
15370 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15371
15372 /* Swap out the table. */
15373 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15374 ext_tab = bfd_alloc (abfd, amt);
15375 if (ext_tab == NULL)
15376 {
15377 free (tab);
15378 return false;
15379 }
15380
15381 for (j = 0; j < c; j++)
15382 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15383 free (tab);
15384
15385 o->size = c * sizeof (Elf32_External_gptab);
15386 o->contents = (bfd_byte *) ext_tab;
15387
15388 /* Skip this section later on (I don't think this currently
15389 matters, but someday it might). */
15390 o->map_head.link_order = NULL;
15391 }
15392 }
15393
15394 /* Invoke the regular ELF backend linker to do all the work. */
15395 if (!bfd_elf_final_link (abfd, info))
15396 return false;
15397
15398 /* Now write out the computed sections. */
15399
15400 if (abiflags_sec != NULL)
15401 {
15402 Elf_External_ABIFlags_v0 ext;
15403 Elf_Internal_ABIFlags_v0 *abiflags;
15404
15405 abiflags = &mips_elf_tdata (abfd)->abiflags;
15406
15407 /* Set up the abiflags if no valid input sections were found. */
15408 if (!mips_elf_tdata (abfd)->abiflags_valid)
15409 {
15410 infer_mips_abiflags (abfd, abiflags);
15411 mips_elf_tdata (abfd)->abiflags_valid = true;
15412 }
15413 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15414 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15415 return false;
15416 }
15417
15418 if (reginfo_sec != NULL)
15419 {
15420 Elf32_External_RegInfo ext;
15421
15422 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15423 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15424 return false;
15425 }
15426
15427 if (mdebug_sec != NULL)
15428 {
15429 BFD_ASSERT (abfd->output_has_begun);
15430 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15431 swap, info,
15432 mdebug_sec->filepos))
15433 return false;
15434
15435 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15436 }
15437
15438 if (gptab_data_sec != NULL)
15439 {
15440 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15441 gptab_data_sec->contents,
15442 0, gptab_data_sec->size))
15443 return false;
15444 }
15445
15446 if (gptab_bss_sec != NULL)
15447 {
15448 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15449 gptab_bss_sec->contents,
15450 0, gptab_bss_sec->size))
15451 return false;
15452 }
15453
15454 if (SGI_COMPAT (abfd))
15455 {
15456 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15457 if (rtproc_sec != NULL)
15458 {
15459 if (! bfd_set_section_contents (abfd, rtproc_sec,
15460 rtproc_sec->contents,
15461 0, rtproc_sec->size))
15462 return false;
15463 }
15464 }
15465
15466 return true;
15467 }
15468 \f
15469 /* Merge object file header flags from IBFD into OBFD. Raise an error
15470 if there are conflicting settings. */
15471
15472 static bool
15473 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15474 {
15475 bfd *obfd = info->output_bfd;
15476 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15477 flagword old_flags;
15478 flagword new_flags;
15479 bool ok;
15480
15481 new_flags = elf_elfheader (ibfd)->e_flags;
15482 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15483 old_flags = elf_elfheader (obfd)->e_flags;
15484
15485 /* Check flag compatibility. */
15486
15487 new_flags &= ~EF_MIPS_NOREORDER;
15488 old_flags &= ~EF_MIPS_NOREORDER;
15489
15490 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15491 doesn't seem to matter. */
15492 new_flags &= ~EF_MIPS_XGOT;
15493 old_flags &= ~EF_MIPS_XGOT;
15494
15495 /* MIPSpro generates ucode info in n64 objects. Again, we should
15496 just be able to ignore this. */
15497 new_flags &= ~EF_MIPS_UCODE;
15498 old_flags &= ~EF_MIPS_UCODE;
15499
15500 /* DSOs should only be linked with CPIC code. */
15501 if ((ibfd->flags & DYNAMIC) != 0)
15502 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15503
15504 if (new_flags == old_flags)
15505 return true;
15506
15507 ok = true;
15508
15509 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15510 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15511 {
15512 _bfd_error_handler
15513 (_("%pB: warning: linking abicalls files with non-abicalls files"),
15514 ibfd);
15515 ok = true;
15516 }
15517
15518 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15519 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15520 if (! (new_flags & EF_MIPS_PIC))
15521 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15522
15523 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15524 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15525
15526 /* Compare the ISAs. */
15527 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15528 {
15529 _bfd_error_handler
15530 (_("%pB: linking 32-bit code with 64-bit code"),
15531 ibfd);
15532 ok = false;
15533 }
15534 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15535 {
15536 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15537 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15538 {
15539 /* Copy the architecture info from IBFD to OBFD. Also copy
15540 the 32-bit flag (if set) so that we continue to recognise
15541 OBFD as a 32-bit binary. */
15542 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15543 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15544 elf_elfheader (obfd)->e_flags
15545 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15546
15547 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15548 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15549
15550 /* Copy across the ABI flags if OBFD doesn't use them
15551 and if that was what caused us to treat IBFD as 32-bit. */
15552 if ((old_flags & EF_MIPS_ABI) == 0
15553 && mips_32bit_flags_p (new_flags)
15554 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15555 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15556 }
15557 else
15558 {
15559 /* The ISAs aren't compatible. */
15560 _bfd_error_handler
15561 /* xgettext:c-format */
15562 (_("%pB: linking %s module with previous %s modules"),
15563 ibfd,
15564 bfd_printable_name (ibfd),
15565 bfd_printable_name (obfd));
15566 ok = false;
15567 }
15568 }
15569
15570 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15571 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15572
15573 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15574 does set EI_CLASS differently from any 32-bit ABI. */
15575 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15576 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15577 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15578 {
15579 /* Only error if both are set (to different values). */
15580 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15581 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15582 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15583 {
15584 _bfd_error_handler
15585 /* xgettext:c-format */
15586 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15587 ibfd,
15588 elf_mips_abi_name (ibfd),
15589 elf_mips_abi_name (obfd));
15590 ok = false;
15591 }
15592 new_flags &= ~EF_MIPS_ABI;
15593 old_flags &= ~EF_MIPS_ABI;
15594 }
15595
15596 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15597 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15598 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15599 {
15600 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15601 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15602 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15603 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15604 int micro_mis = old_m16 && new_micro;
15605 int m16_mis = old_micro && new_m16;
15606
15607 if (m16_mis || micro_mis)
15608 {
15609 _bfd_error_handler
15610 /* xgettext:c-format */
15611 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15612 ibfd,
15613 m16_mis ? "MIPS16" : "microMIPS",
15614 m16_mis ? "microMIPS" : "MIPS16");
15615 ok = false;
15616 }
15617
15618 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15619
15620 new_flags &= ~ EF_MIPS_ARCH_ASE;
15621 old_flags &= ~ EF_MIPS_ARCH_ASE;
15622 }
15623
15624 /* Compare NaN encodings. */
15625 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15626 {
15627 /* xgettext:c-format */
15628 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15629 ibfd,
15630 (new_flags & EF_MIPS_NAN2008
15631 ? "-mnan=2008" : "-mnan=legacy"),
15632 (old_flags & EF_MIPS_NAN2008
15633 ? "-mnan=2008" : "-mnan=legacy"));
15634 ok = false;
15635 new_flags &= ~EF_MIPS_NAN2008;
15636 old_flags &= ~EF_MIPS_NAN2008;
15637 }
15638
15639 /* Compare FP64 state. */
15640 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15641 {
15642 /* xgettext:c-format */
15643 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15644 ibfd,
15645 (new_flags & EF_MIPS_FP64
15646 ? "-mfp64" : "-mfp32"),
15647 (old_flags & EF_MIPS_FP64
15648 ? "-mfp64" : "-mfp32"));
15649 ok = false;
15650 new_flags &= ~EF_MIPS_FP64;
15651 old_flags &= ~EF_MIPS_FP64;
15652 }
15653
15654 /* Warn about any other mismatches */
15655 if (new_flags != old_flags)
15656 {
15657 /* xgettext:c-format */
15658 _bfd_error_handler
15659 (_("%pB: uses different e_flags (%#x) fields than previous modules "
15660 "(%#x)"),
15661 ibfd, new_flags, old_flags);
15662 ok = false;
15663 }
15664
15665 return ok;
15666 }
15667
15668 /* Merge object attributes from IBFD into OBFD. Raise an error if
15669 there are conflicting attributes. */
15670 static bool
15671 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15672 {
15673 bfd *obfd = info->output_bfd;
15674 obj_attribute *in_attr;
15675 obj_attribute *out_attr;
15676 bfd *abi_fp_bfd;
15677 bfd *abi_msa_bfd;
15678
15679 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15680 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15681 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15682 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15683
15684 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15685 if (!abi_msa_bfd
15686 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15687 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15688
15689 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15690 {
15691 /* This is the first object. Copy the attributes. */
15692 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15693
15694 /* Use the Tag_null value to indicate the attributes have been
15695 initialized. */
15696 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15697
15698 return true;
15699 }
15700
15701 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15702 non-conflicting ones. */
15703 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15704 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15705 {
15706 int out_fp, in_fp;
15707
15708 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15709 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15710 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15711 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15712 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15713 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15714 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15715 || in_fp == Val_GNU_MIPS_ABI_FP_64
15716 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15717 {
15718 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15719 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15720 }
15721 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15722 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15723 || out_fp == Val_GNU_MIPS_ABI_FP_64
15724 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15725 /* Keep the current setting. */;
15726 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15727 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15728 {
15729 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15730 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15731 }
15732 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15733 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15734 /* Keep the current setting. */;
15735 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15736 {
15737 const char *out_string, *in_string;
15738
15739 out_string = _bfd_mips_fp_abi_string (out_fp);
15740 in_string = _bfd_mips_fp_abi_string (in_fp);
15741 /* First warn about cases involving unrecognised ABIs. */
15742 if (!out_string && !in_string)
15743 /* xgettext:c-format */
15744 _bfd_error_handler
15745 (_("warning: %pB uses unknown floating point ABI %d "
15746 "(set by %pB), %pB uses unknown floating point ABI %d"),
15747 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15748 else if (!out_string)
15749 _bfd_error_handler
15750 /* xgettext:c-format */
15751 (_("warning: %pB uses unknown floating point ABI %d "
15752 "(set by %pB), %pB uses %s"),
15753 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15754 else if (!in_string)
15755 _bfd_error_handler
15756 /* xgettext:c-format */
15757 (_("warning: %pB uses %s (set by %pB), "
15758 "%pB uses unknown floating point ABI %d"),
15759 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15760 else
15761 {
15762 /* If one of the bfds is soft-float, the other must be
15763 hard-float. The exact choice of hard-float ABI isn't
15764 really relevant to the error message. */
15765 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15766 out_string = "-mhard-float";
15767 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15768 in_string = "-mhard-float";
15769 _bfd_error_handler
15770 /* xgettext:c-format */
15771 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15772 obfd, out_string, abi_fp_bfd, ibfd, in_string);
15773 }
15774 }
15775 }
15776
15777 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15778 non-conflicting ones. */
15779 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15780 {
15781 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15782 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15783 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15784 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15785 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15786 {
15787 case Val_GNU_MIPS_ABI_MSA_128:
15788 _bfd_error_handler
15789 /* xgettext:c-format */
15790 (_("warning: %pB uses %s (set by %pB), "
15791 "%pB uses unknown MSA ABI %d"),
15792 obfd, "-mmsa", abi_msa_bfd,
15793 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15794 break;
15795
15796 default:
15797 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15798 {
15799 case Val_GNU_MIPS_ABI_MSA_128:
15800 _bfd_error_handler
15801 /* xgettext:c-format */
15802 (_("warning: %pB uses unknown MSA ABI %d "
15803 "(set by %pB), %pB uses %s"),
15804 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15805 abi_msa_bfd, ibfd, "-mmsa");
15806 break;
15807
15808 default:
15809 _bfd_error_handler
15810 /* xgettext:c-format */
15811 (_("warning: %pB uses unknown MSA ABI %d "
15812 "(set by %pB), %pB uses unknown MSA ABI %d"),
15813 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15814 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15815 break;
15816 }
15817 }
15818 }
15819
15820 /* Merge Tag_compatibility attributes and any common GNU ones. */
15821 return _bfd_elf_merge_object_attributes (ibfd, info);
15822 }
15823
15824 /* Merge object ABI flags from IBFD into OBFD. Raise an error if
15825 there are conflicting settings. */
15826
15827 static bool
15828 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15829 {
15830 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15831 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15832 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15833
15834 /* Update the output abiflags fp_abi using the computed fp_abi. */
15835 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15836
15837 #define max(a, b) ((a) > (b) ? (a) : (b))
15838 /* Merge abiflags. */
15839 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15840 in_tdata->abiflags.isa_level);
15841 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15842 in_tdata->abiflags.isa_rev);
15843 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15844 in_tdata->abiflags.gpr_size);
15845 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15846 in_tdata->abiflags.cpr1_size);
15847 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15848 in_tdata->abiflags.cpr2_size);
15849 #undef max
15850 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15851 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15852
15853 return true;
15854 }
15855
15856 /* Merge backend specific data from an object file to the output
15857 object file when linking. */
15858
15859 bool
15860 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15861 {
15862 bfd *obfd = info->output_bfd;
15863 struct mips_elf_obj_tdata *out_tdata;
15864 struct mips_elf_obj_tdata *in_tdata;
15865 bool null_input_bfd = true;
15866 asection *sec;
15867 bool ok;
15868
15869 /* Check if we have the same endianness. */
15870 if (! _bfd_generic_verify_endian_match (ibfd, info))
15871 {
15872 _bfd_error_handler
15873 (_("%pB: endianness incompatible with that of the selected emulation"),
15874 ibfd);
15875 return false;
15876 }
15877
15878 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15879 return true;
15880
15881 in_tdata = mips_elf_tdata (ibfd);
15882 out_tdata = mips_elf_tdata (obfd);
15883
15884 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15885 {
15886 _bfd_error_handler
15887 (_("%pB: ABI is incompatible with that of the selected emulation"),
15888 ibfd);
15889 return false;
15890 }
15891
15892 /* Check to see if the input BFD actually contains any sections. If not,
15893 then it has no attributes, and its flags may not have been initialized
15894 either, but it cannot actually cause any incompatibility. */
15895 /* FIXME: This excludes any input shared library from consideration. */
15896 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15897 {
15898 /* Ignore synthetic sections and empty .text, .data and .bss sections
15899 which are automatically generated by gas. Also ignore fake
15900 (s)common sections, since merely defining a common symbol does
15901 not affect compatibility. */
15902 if ((sec->flags & SEC_IS_COMMON) == 0
15903 && strcmp (sec->name, ".reginfo")
15904 && strcmp (sec->name, ".mdebug")
15905 && (sec->size != 0
15906 || (strcmp (sec->name, ".text")
15907 && strcmp (sec->name, ".data")
15908 && strcmp (sec->name, ".bss"))))
15909 {
15910 null_input_bfd = false;
15911 break;
15912 }
15913 }
15914 if (null_input_bfd)
15915 return true;
15916
15917 /* Populate abiflags using existing information. */
15918 if (in_tdata->abiflags_valid)
15919 {
15920 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15921 Elf_Internal_ABIFlags_v0 in_abiflags;
15922 Elf_Internal_ABIFlags_v0 abiflags;
15923
15924 /* Set up the FP ABI attribute from the abiflags if it is not already
15925 set. */
15926 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15927 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15928
15929 infer_mips_abiflags (ibfd, &abiflags);
15930 in_abiflags = in_tdata->abiflags;
15931
15932 /* It is not possible to infer the correct ISA revision
15933 for R3 or R5 so drop down to R2 for the checks. */
15934 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15935 in_abiflags.isa_rev = 2;
15936
15937 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15938 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15939 _bfd_error_handler
15940 (_("%pB: warning: inconsistent ISA between e_flags and "
15941 ".MIPS.abiflags"), ibfd);
15942 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15943 && in_abiflags.fp_abi != abiflags.fp_abi)
15944 _bfd_error_handler
15945 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15946 ".MIPS.abiflags"), ibfd);
15947 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15948 _bfd_error_handler
15949 (_("%pB: warning: inconsistent ASEs between e_flags and "
15950 ".MIPS.abiflags"), ibfd);
15951 /* The isa_ext is allowed to be an extension of what can be inferred
15952 from e_flags. */
15953 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15954 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15955 _bfd_error_handler
15956 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15957 ".MIPS.abiflags"), ibfd);
15958 if (in_abiflags.flags2 != 0)
15959 _bfd_error_handler
15960 (_("%pB: warning: unexpected flag in the flags2 field of "
15961 ".MIPS.abiflags (0x%lx)"), ibfd,
15962 in_abiflags.flags2);
15963 }
15964 else
15965 {
15966 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15967 in_tdata->abiflags_valid = true;
15968 }
15969
15970 if (!out_tdata->abiflags_valid)
15971 {
15972 /* Copy input abiflags if output abiflags are not already valid. */
15973 out_tdata->abiflags = in_tdata->abiflags;
15974 out_tdata->abiflags_valid = true;
15975 }
15976
15977 if (! elf_flags_init (obfd))
15978 {
15979 elf_flags_init (obfd) = true;
15980 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
15981 elf_elfheader (obfd)->e_ident[EI_CLASS]
15982 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
15983
15984 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
15985 && (bfd_get_arch_info (obfd)->the_default
15986 || mips_mach_extends_p (bfd_get_mach (obfd),
15987 bfd_get_mach (ibfd))))
15988 {
15989 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
15990 bfd_get_mach (ibfd)))
15991 return false;
15992
15993 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
15994 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15995 }
15996
15997 ok = true;
15998 }
15999 else
16000 ok = mips_elf_merge_obj_e_flags (ibfd, info);
16001
16002 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
16003
16004 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
16005
16006 if (!ok)
16007 {
16008 bfd_set_error (bfd_error_bad_value);
16009 return false;
16010 }
16011
16012 return true;
16013 }
16014
16015 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
16016
16017 bool
16018 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
16019 {
16020 BFD_ASSERT (!elf_flags_init (abfd)
16021 || elf_elfheader (abfd)->e_flags == flags);
16022
16023 elf_elfheader (abfd)->e_flags = flags;
16024 elf_flags_init (abfd) = true;
16025 return true;
16026 }
16027
16028 char *
16029 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16030 {
16031 switch (dtag)
16032 {
16033 default: return "";
16034 case DT_MIPS_RLD_VERSION:
16035 return "MIPS_RLD_VERSION";
16036 case DT_MIPS_TIME_STAMP:
16037 return "MIPS_TIME_STAMP";
16038 case DT_MIPS_ICHECKSUM:
16039 return "MIPS_ICHECKSUM";
16040 case DT_MIPS_IVERSION:
16041 return "MIPS_IVERSION";
16042 case DT_MIPS_FLAGS:
16043 return "MIPS_FLAGS";
16044 case DT_MIPS_BASE_ADDRESS:
16045 return "MIPS_BASE_ADDRESS";
16046 case DT_MIPS_MSYM:
16047 return "MIPS_MSYM";
16048 case DT_MIPS_CONFLICT:
16049 return "MIPS_CONFLICT";
16050 case DT_MIPS_LIBLIST:
16051 return "MIPS_LIBLIST";
16052 case DT_MIPS_LOCAL_GOTNO:
16053 return "MIPS_LOCAL_GOTNO";
16054 case DT_MIPS_CONFLICTNO:
16055 return "MIPS_CONFLICTNO";
16056 case DT_MIPS_LIBLISTNO:
16057 return "MIPS_LIBLISTNO";
16058 case DT_MIPS_SYMTABNO:
16059 return "MIPS_SYMTABNO";
16060 case DT_MIPS_UNREFEXTNO:
16061 return "MIPS_UNREFEXTNO";
16062 case DT_MIPS_GOTSYM:
16063 return "MIPS_GOTSYM";
16064 case DT_MIPS_HIPAGENO:
16065 return "MIPS_HIPAGENO";
16066 case DT_MIPS_RLD_MAP:
16067 return "MIPS_RLD_MAP";
16068 case DT_MIPS_RLD_MAP_REL:
16069 return "MIPS_RLD_MAP_REL";
16070 case DT_MIPS_DELTA_CLASS:
16071 return "MIPS_DELTA_CLASS";
16072 case DT_MIPS_DELTA_CLASS_NO:
16073 return "MIPS_DELTA_CLASS_NO";
16074 case DT_MIPS_DELTA_INSTANCE:
16075 return "MIPS_DELTA_INSTANCE";
16076 case DT_MIPS_DELTA_INSTANCE_NO:
16077 return "MIPS_DELTA_INSTANCE_NO";
16078 case DT_MIPS_DELTA_RELOC:
16079 return "MIPS_DELTA_RELOC";
16080 case DT_MIPS_DELTA_RELOC_NO:
16081 return "MIPS_DELTA_RELOC_NO";
16082 case DT_MIPS_DELTA_SYM:
16083 return "MIPS_DELTA_SYM";
16084 case DT_MIPS_DELTA_SYM_NO:
16085 return "MIPS_DELTA_SYM_NO";
16086 case DT_MIPS_DELTA_CLASSSYM:
16087 return "MIPS_DELTA_CLASSSYM";
16088 case DT_MIPS_DELTA_CLASSSYM_NO:
16089 return "MIPS_DELTA_CLASSSYM_NO";
16090 case DT_MIPS_CXX_FLAGS:
16091 return "MIPS_CXX_FLAGS";
16092 case DT_MIPS_PIXIE_INIT:
16093 return "MIPS_PIXIE_INIT";
16094 case DT_MIPS_SYMBOL_LIB:
16095 return "MIPS_SYMBOL_LIB";
16096 case DT_MIPS_LOCALPAGE_GOTIDX:
16097 return "MIPS_LOCALPAGE_GOTIDX";
16098 case DT_MIPS_LOCAL_GOTIDX:
16099 return "MIPS_LOCAL_GOTIDX";
16100 case DT_MIPS_HIDDEN_GOTIDX:
16101 return "MIPS_HIDDEN_GOTIDX";
16102 case DT_MIPS_PROTECTED_GOTIDX:
16103 return "MIPS_PROTECTED_GOT_IDX";
16104 case DT_MIPS_OPTIONS:
16105 return "MIPS_OPTIONS";
16106 case DT_MIPS_INTERFACE:
16107 return "MIPS_INTERFACE";
16108 case DT_MIPS_DYNSTR_ALIGN:
16109 return "DT_MIPS_DYNSTR_ALIGN";
16110 case DT_MIPS_INTERFACE_SIZE:
16111 return "DT_MIPS_INTERFACE_SIZE";
16112 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16113 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16114 case DT_MIPS_PERF_SUFFIX:
16115 return "DT_MIPS_PERF_SUFFIX";
16116 case DT_MIPS_COMPACT_SIZE:
16117 return "DT_MIPS_COMPACT_SIZE";
16118 case DT_MIPS_GP_VALUE:
16119 return "DT_MIPS_GP_VALUE";
16120 case DT_MIPS_AUX_DYNAMIC:
16121 return "DT_MIPS_AUX_DYNAMIC";
16122 case DT_MIPS_PLTGOT:
16123 return "DT_MIPS_PLTGOT";
16124 case DT_MIPS_RWPLT:
16125 return "DT_MIPS_RWPLT";
16126 case DT_MIPS_XHASH:
16127 return "DT_MIPS_XHASH";
16128 }
16129 }
16130
16131 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16132 not known. */
16133
16134 const char *
16135 _bfd_mips_fp_abi_string (int fp)
16136 {
16137 switch (fp)
16138 {
16139 /* These strings aren't translated because they're simply
16140 option lists. */
16141 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16142 return "-mdouble-float";
16143
16144 case Val_GNU_MIPS_ABI_FP_SINGLE:
16145 return "-msingle-float";
16146
16147 case Val_GNU_MIPS_ABI_FP_SOFT:
16148 return "-msoft-float";
16149
16150 case Val_GNU_MIPS_ABI_FP_OLD_64:
16151 return _("-mips32r2 -mfp64 (12 callee-saved)");
16152
16153 case Val_GNU_MIPS_ABI_FP_XX:
16154 return "-mfpxx";
16155
16156 case Val_GNU_MIPS_ABI_FP_64:
16157 return "-mgp32 -mfp64";
16158
16159 case Val_GNU_MIPS_ABI_FP_64A:
16160 return "-mgp32 -mfp64 -mno-odd-spreg";
16161
16162 default:
16163 return 0;
16164 }
16165 }
16166
16167 static void
16168 print_mips_ases (FILE *file, unsigned int mask)
16169 {
16170 if (mask & AFL_ASE_DSP)
16171 fputs ("\n\tDSP ASE", file);
16172 if (mask & AFL_ASE_DSPR2)
16173 fputs ("\n\tDSP R2 ASE", file);
16174 if (mask & AFL_ASE_DSPR3)
16175 fputs ("\n\tDSP R3 ASE", file);
16176 if (mask & AFL_ASE_EVA)
16177 fputs ("\n\tEnhanced VA Scheme", file);
16178 if (mask & AFL_ASE_MCU)
16179 fputs ("\n\tMCU (MicroController) ASE", file);
16180 if (mask & AFL_ASE_MDMX)
16181 fputs ("\n\tMDMX ASE", file);
16182 if (mask & AFL_ASE_MIPS3D)
16183 fputs ("\n\tMIPS-3D ASE", file);
16184 if (mask & AFL_ASE_MT)
16185 fputs ("\n\tMT ASE", file);
16186 if (mask & AFL_ASE_SMARTMIPS)
16187 fputs ("\n\tSmartMIPS ASE", file);
16188 if (mask & AFL_ASE_VIRT)
16189 fputs ("\n\tVZ ASE", file);
16190 if (mask & AFL_ASE_MSA)
16191 fputs ("\n\tMSA ASE", file);
16192 if (mask & AFL_ASE_MIPS16)
16193 fputs ("\n\tMIPS16 ASE", file);
16194 if (mask & AFL_ASE_MICROMIPS)
16195 fputs ("\n\tMICROMIPS ASE", file);
16196 if (mask & AFL_ASE_XPA)
16197 fputs ("\n\tXPA ASE", file);
16198 if (mask & AFL_ASE_MIPS16E2)
16199 fputs ("\n\tMIPS16e2 ASE", file);
16200 if (mask & AFL_ASE_CRC)
16201 fputs ("\n\tCRC ASE", file);
16202 if (mask & AFL_ASE_GINV)
16203 fputs ("\n\tGINV ASE", file);
16204 if (mask & AFL_ASE_LOONGSON_MMI)
16205 fputs ("\n\tLoongson MMI ASE", file);
16206 if (mask & AFL_ASE_LOONGSON_CAM)
16207 fputs ("\n\tLoongson CAM ASE", file);
16208 if (mask & AFL_ASE_LOONGSON_EXT)
16209 fputs ("\n\tLoongson EXT ASE", file);
16210 if (mask & AFL_ASE_LOONGSON_EXT2)
16211 fputs ("\n\tLoongson EXT2 ASE", file);
16212 if (mask == 0)
16213 fprintf (file, "\n\t%s", _("None"));
16214 else if ((mask & ~AFL_ASE_MASK) != 0)
16215 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16216 }
16217
16218 static void
16219 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16220 {
16221 switch (isa_ext)
16222 {
16223 case 0:
16224 fputs (_("None"), file);
16225 break;
16226 case AFL_EXT_XLR:
16227 fputs ("RMI XLR", file);
16228 break;
16229 case AFL_EXT_OCTEON3:
16230 fputs ("Cavium Networks Octeon3", file);
16231 break;
16232 case AFL_EXT_OCTEON2:
16233 fputs ("Cavium Networks Octeon2", file);
16234 break;
16235 case AFL_EXT_OCTEONP:
16236 fputs ("Cavium Networks OcteonP", file);
16237 break;
16238 case AFL_EXT_OCTEON:
16239 fputs ("Cavium Networks Octeon", file);
16240 break;
16241 case AFL_EXT_5900:
16242 fputs ("Toshiba R5900", file);
16243 break;
16244 case AFL_EXT_4650:
16245 fputs ("MIPS R4650", file);
16246 break;
16247 case AFL_EXT_4010:
16248 fputs ("LSI R4010", file);
16249 break;
16250 case AFL_EXT_4100:
16251 fputs ("NEC VR4100", file);
16252 break;
16253 case AFL_EXT_3900:
16254 fputs ("Toshiba R3900", file);
16255 break;
16256 case AFL_EXT_10000:
16257 fputs ("MIPS R10000", file);
16258 break;
16259 case AFL_EXT_SB1:
16260 fputs ("Broadcom SB-1", file);
16261 break;
16262 case AFL_EXT_4111:
16263 fputs ("NEC VR4111/VR4181", file);
16264 break;
16265 case AFL_EXT_4120:
16266 fputs ("NEC VR4120", file);
16267 break;
16268 case AFL_EXT_5400:
16269 fputs ("NEC VR5400", file);
16270 break;
16271 case AFL_EXT_5500:
16272 fputs ("NEC VR5500", file);
16273 break;
16274 case AFL_EXT_LOONGSON_2E:
16275 fputs ("ST Microelectronics Loongson 2E", file);
16276 break;
16277 case AFL_EXT_LOONGSON_2F:
16278 fputs ("ST Microelectronics Loongson 2F", file);
16279 break;
16280 case AFL_EXT_INTERAPTIV_MR2:
16281 fputs ("Imagination interAptiv MR2", file);
16282 break;
16283 default:
16284 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16285 break;
16286 }
16287 }
16288
16289 static void
16290 print_mips_fp_abi_value (FILE *file, int val)
16291 {
16292 switch (val)
16293 {
16294 case Val_GNU_MIPS_ABI_FP_ANY:
16295 fprintf (file, _("Hard or soft float\n"));
16296 break;
16297 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16298 fprintf (file, _("Hard float (double precision)\n"));
16299 break;
16300 case Val_GNU_MIPS_ABI_FP_SINGLE:
16301 fprintf (file, _("Hard float (single precision)\n"));
16302 break;
16303 case Val_GNU_MIPS_ABI_FP_SOFT:
16304 fprintf (file, _("Soft float\n"));
16305 break;
16306 case Val_GNU_MIPS_ABI_FP_OLD_64:
16307 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16308 break;
16309 case Val_GNU_MIPS_ABI_FP_XX:
16310 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16311 break;
16312 case Val_GNU_MIPS_ABI_FP_64:
16313 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16314 break;
16315 case Val_GNU_MIPS_ABI_FP_64A:
16316 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16317 break;
16318 default:
16319 fprintf (file, "??? (%d)\n", val);
16320 break;
16321 }
16322 }
16323
16324 static int
16325 get_mips_reg_size (int reg_size)
16326 {
16327 return (reg_size == AFL_REG_NONE) ? 0
16328 : (reg_size == AFL_REG_32) ? 32
16329 : (reg_size == AFL_REG_64) ? 64
16330 : (reg_size == AFL_REG_128) ? 128
16331 : -1;
16332 }
16333
16334 bool
16335 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16336 {
16337 FILE *file = ptr;
16338
16339 BFD_ASSERT (abfd != NULL && ptr != NULL);
16340
16341 /* Print normal ELF private data. */
16342 _bfd_elf_print_private_bfd_data (abfd, ptr);
16343
16344 /* xgettext:c-format */
16345 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16346
16347 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16348 fprintf (file, _(" [abi=O32]"));
16349 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16350 fprintf (file, _(" [abi=O64]"));
16351 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16352 fprintf (file, _(" [abi=EABI32]"));
16353 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16354 fprintf (file, _(" [abi=EABI64]"));
16355 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16356 fprintf (file, _(" [abi unknown]"));
16357 else if (ABI_N32_P (abfd))
16358 fprintf (file, _(" [abi=N32]"));
16359 else if (ABI_64_P (abfd))
16360 fprintf (file, _(" [abi=64]"));
16361 else
16362 fprintf (file, _(" [no abi set]"));
16363
16364 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
16365 fprintf (file, " [mips1]");
16366 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
16367 fprintf (file, " [mips2]");
16368 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
16369 fprintf (file, " [mips3]");
16370 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
16371 fprintf (file, " [mips4]");
16372 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
16373 fprintf (file, " [mips5]");
16374 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
16375 fprintf (file, " [mips32]");
16376 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
16377 fprintf (file, " [mips64]");
16378 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
16379 fprintf (file, " [mips32r2]");
16380 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
16381 fprintf (file, " [mips64r2]");
16382 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16383 fprintf (file, " [mips32r6]");
16384 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16385 fprintf (file, " [mips64r6]");
16386 else
16387 fprintf (file, _(" [unknown ISA]"));
16388
16389 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16390 fprintf (file, " [mdmx]");
16391
16392 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16393 fprintf (file, " [mips16]");
16394
16395 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16396 fprintf (file, " [micromips]");
16397
16398 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16399 fprintf (file, " [nan2008]");
16400
16401 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16402 fprintf (file, " [old fp64]");
16403
16404 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16405 fprintf (file, " [32bitmode]");
16406 else
16407 fprintf (file, _(" [not 32bitmode]"));
16408
16409 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16410 fprintf (file, " [noreorder]");
16411
16412 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16413 fprintf (file, " [PIC]");
16414
16415 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16416 fprintf (file, " [CPIC]");
16417
16418 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16419 fprintf (file, " [XGOT]");
16420
16421 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16422 fprintf (file, " [UCODE]");
16423
16424 fputc ('\n', file);
16425
16426 if (mips_elf_tdata (abfd)->abiflags_valid)
16427 {
16428 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16429 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16430 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16431 if (abiflags->isa_rev > 1)
16432 fprintf (file, "r%d", abiflags->isa_rev);
16433 fprintf (file, "\nGPR size: %d",
16434 get_mips_reg_size (abiflags->gpr_size));
16435 fprintf (file, "\nCPR1 size: %d",
16436 get_mips_reg_size (abiflags->cpr1_size));
16437 fprintf (file, "\nCPR2 size: %d",
16438 get_mips_reg_size (abiflags->cpr2_size));
16439 fputs ("\nFP ABI: ", file);
16440 print_mips_fp_abi_value (file, abiflags->fp_abi);
16441 fputs ("ISA Extension: ", file);
16442 print_mips_isa_ext (file, abiflags->isa_ext);
16443 fputs ("\nASEs:", file);
16444 print_mips_ases (file, abiflags->ases);
16445 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16446 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16447 fputc ('\n', file);
16448 }
16449
16450 return true;
16451 }
16452
16453 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16454 {
16455 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16456 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16457 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16458 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16459 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16460 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
16461 { STRING_COMMA_LEN (".MIPS.xhash"), 0, SHT_MIPS_XHASH, SHF_ALLOC },
16462 { NULL, 0, 0, 0, 0 }
16463 };
16464
16465 /* Merge non visibility st_other attributes. Ensure that the
16466 STO_OPTIONAL flag is copied into h->other, even if this is not a
16467 definiton of the symbol. */
16468 void
16469 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16470 unsigned int st_other,
16471 bool definition,
16472 bool dynamic ATTRIBUTE_UNUSED)
16473 {
16474 if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16475 {
16476 unsigned char other;
16477
16478 other = (definition ? st_other : h->other);
16479 other &= ~ELF_ST_VISIBILITY (-1);
16480 h->other = other | ELF_ST_VISIBILITY (h->other);
16481 }
16482
16483 if (!definition
16484 && ELF_MIPS_IS_OPTIONAL (st_other))
16485 h->other |= STO_OPTIONAL;
16486 }
16487
16488 /* Decide whether an undefined symbol is special and can be ignored.
16489 This is the case for OPTIONAL symbols on IRIX. */
16490 bool
16491 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16492 {
16493 return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
16494 }
16495
16496 bool
16497 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16498 {
16499 return (sym->st_shndx == SHN_COMMON
16500 || sym->st_shndx == SHN_MIPS_ACOMMON
16501 || sym->st_shndx == SHN_MIPS_SCOMMON);
16502 }
16503
16504 /* Return address for Ith PLT stub in section PLT, for relocation REL
16505 or (bfd_vma) -1 if it should not be included. */
16506
16507 bfd_vma
16508 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16509 const arelent *rel ATTRIBUTE_UNUSED)
16510 {
16511 return (plt->vma
16512 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16513 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16514 }
16515
16516 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16517 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16518 and .got.plt and also the slots may be of a different size each we walk
16519 the PLT manually fetching instructions and matching them against known
16520 patterns. To make things easier standard MIPS slots, if any, always come
16521 first. As we don't create proper ELF symbols we use the UDATA.I member
16522 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16523 with the ST_OTHER member of the ELF symbol. */
16524
16525 long
16526 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16527 long symcount ATTRIBUTE_UNUSED,
16528 asymbol **syms ATTRIBUTE_UNUSED,
16529 long dynsymcount, asymbol **dynsyms,
16530 asymbol **ret)
16531 {
16532 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16533 static const char microsuffix[] = "@micromipsplt";
16534 static const char m16suffix[] = "@mips16plt";
16535 static const char mipssuffix[] = "@plt";
16536
16537 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
16538 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16539 bool micromips_p = MICROMIPS_P (abfd);
16540 Elf_Internal_Shdr *hdr;
16541 bfd_byte *plt_data;
16542 bfd_vma plt_offset;
16543 unsigned int other;
16544 bfd_vma entry_size;
16545 bfd_vma plt0_size;
16546 asection *relplt;
16547 bfd_vma opcode;
16548 asection *plt;
16549 asymbol *send;
16550 size_t size;
16551 char *names;
16552 long counti;
16553 arelent *p;
16554 asymbol *s;
16555 char *nend;
16556 long count;
16557 long pi;
16558 long i;
16559 long n;
16560
16561 *ret = NULL;
16562
16563 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16564 return 0;
16565
16566 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16567 if (relplt == NULL)
16568 return 0;
16569
16570 hdr = &elf_section_data (relplt)->this_hdr;
16571 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16572 return 0;
16573
16574 plt = bfd_get_section_by_name (abfd, ".plt");
16575 if (plt == NULL)
16576 return 0;
16577
16578 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16579 if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
16580 return -1;
16581 p = relplt->relocation;
16582
16583 /* Calculating the exact amount of space required for symbols would
16584 require two passes over the PLT, so just pessimise assuming two
16585 PLT slots per relocation. */
16586 count = relplt->size / hdr->sh_entsize;
16587 counti = count * bed->s->int_rels_per_ext_rel;
16588 size = 2 * count * sizeof (asymbol);
16589 size += count * (sizeof (mipssuffix) +
16590 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16591 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16592 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16593
16594 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16595 size += sizeof (asymbol) + sizeof (pltname);
16596
16597 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16598 return -1;
16599
16600 if (plt->size < 16)
16601 return -1;
16602
16603 s = *ret = bfd_malloc (size);
16604 if (s == NULL)
16605 return -1;
16606 send = s + 2 * count + 1;
16607
16608 names = (char *) send;
16609 nend = (char *) s + size;
16610 n = 0;
16611
16612 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16613 if (opcode == 0x3302fffe)
16614 {
16615 if (!micromips_p)
16616 return -1;
16617 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16618 other = STO_MICROMIPS;
16619 }
16620 else if (opcode == 0x0398c1d0)
16621 {
16622 if (!micromips_p)
16623 return -1;
16624 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16625 other = STO_MICROMIPS;
16626 }
16627 else
16628 {
16629 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16630 other = 0;
16631 }
16632
16633 s->the_bfd = abfd;
16634 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16635 s->section = plt;
16636 s->value = 0;
16637 s->name = names;
16638 s->udata.i = other;
16639 memcpy (names, pltname, sizeof (pltname));
16640 names += sizeof (pltname);
16641 ++s, ++n;
16642
16643 pi = 0;
16644 for (plt_offset = plt0_size;
16645 plt_offset + 8 <= plt->size && s < send;
16646 plt_offset += entry_size)
16647 {
16648 bfd_vma gotplt_addr;
16649 const char *suffix;
16650 bfd_vma gotplt_hi;
16651 bfd_vma gotplt_lo;
16652 size_t suffixlen;
16653
16654 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16655
16656 /* Check if the second word matches the expected MIPS16 instruction. */
16657 if (opcode == 0x651aeb00)
16658 {
16659 if (micromips_p)
16660 return -1;
16661 /* Truncated table??? */
16662 if (plt_offset + 16 > plt->size)
16663 break;
16664 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16665 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16666 suffixlen = sizeof (m16suffix);
16667 suffix = m16suffix;
16668 other = STO_MIPS16;
16669 }
16670 /* Likewise the expected microMIPS instruction (no insn32 mode). */
16671 else if (opcode == 0xff220000)
16672 {
16673 if (!micromips_p)
16674 return -1;
16675 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16676 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16677 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16678 gotplt_lo <<= 2;
16679 gotplt_addr = gotplt_hi + gotplt_lo;
16680 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16681 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16682 suffixlen = sizeof (microsuffix);
16683 suffix = microsuffix;
16684 other = STO_MICROMIPS;
16685 }
16686 /* Likewise the expected microMIPS instruction (insn32 mode). */
16687 else if ((opcode & 0xffff0000) == 0xff2f0000)
16688 {
16689 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16690 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16691 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16692 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16693 gotplt_addr = gotplt_hi + gotplt_lo;
16694 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16695 suffixlen = sizeof (microsuffix);
16696 suffix = microsuffix;
16697 other = STO_MICROMIPS;
16698 }
16699 /* Otherwise assume standard MIPS code. */
16700 else
16701 {
16702 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16703 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16704 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16705 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16706 gotplt_addr = gotplt_hi + gotplt_lo;
16707 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16708 suffixlen = sizeof (mipssuffix);
16709 suffix = mipssuffix;
16710 other = 0;
16711 }
16712 /* Truncated table??? */
16713 if (plt_offset + entry_size > plt->size)
16714 break;
16715
16716 for (i = 0;
16717 i < count && p[pi].address != gotplt_addr;
16718 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16719
16720 if (i < count)
16721 {
16722 size_t namelen;
16723 size_t len;
16724
16725 *s = **p[pi].sym_ptr_ptr;
16726 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16727 we are defining a symbol, ensure one of them is set. */
16728 if ((s->flags & BSF_LOCAL) == 0)
16729 s->flags |= BSF_GLOBAL;
16730 s->flags |= BSF_SYNTHETIC;
16731 s->section = plt;
16732 s->value = plt_offset;
16733 s->name = names;
16734 s->udata.i = other;
16735
16736 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16737 namelen = len + suffixlen;
16738 if (names + namelen > nend)
16739 break;
16740
16741 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16742 names += len;
16743 memcpy (names, suffix, suffixlen);
16744 names += suffixlen;
16745
16746 ++s, ++n;
16747 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16748 }
16749 }
16750
16751 free (plt_data);
16752
16753 return n;
16754 }
16755
16756 /* Return the ABI flags associated with ABFD if available. */
16757
16758 Elf_Internal_ABIFlags_v0 *
16759 bfd_mips_elf_get_abiflags (bfd *abfd)
16760 {
16761 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16762
16763 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16764 }
16765
16766 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16767 field. Taken from `libc-abis.h' generated at GNU libc build time.
16768 Using a MIPS_ prefix as other libc targets use different values. */
16769 enum
16770 {
16771 MIPS_LIBC_ABI_DEFAULT = 0,
16772 MIPS_LIBC_ABI_MIPS_PLT,
16773 MIPS_LIBC_ABI_UNIQUE,
16774 MIPS_LIBC_ABI_MIPS_O32_FP64,
16775 MIPS_LIBC_ABI_ABSOLUTE,
16776 MIPS_LIBC_ABI_XHASH,
16777 MIPS_LIBC_ABI_MAX
16778 };
16779
16780 bool
16781 _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16782 {
16783 struct mips_elf_link_hash_table *htab = NULL;
16784 Elf_Internal_Ehdr *i_ehdrp;
16785
16786 if (!_bfd_elf_init_file_header (abfd, link_info))
16787 return false;
16788
16789 i_ehdrp = elf_elfheader (abfd);
16790 if (link_info)
16791 {
16792 htab = mips_elf_hash_table (link_info);
16793 BFD_ASSERT (htab != NULL);
16794 }
16795
16796 if (htab != NULL
16797 && htab->use_plts_and_copy_relocs
16798 && htab->root.target_os != is_vxworks)
16799 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16800
16801 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16802 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16803 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16804
16805 /* Mark that we need support for absolute symbols in the dynamic loader. */
16806 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16807 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16808
16809 /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16810 if it is the only hash section that will be created. */
16811 if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16812 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16813 return true;
16814 }
16815
16816 int
16817 _bfd_mips_elf_compact_eh_encoding
16818 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16819 {
16820 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16821 }
16822
16823 /* Return the opcode for can't unwind. */
16824
16825 int
16826 _bfd_mips_elf_cant_unwind_opcode
16827 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16828 {
16829 return COMPACT_EH_CANT_UNWIND_OPCODE;
16830 }
16831
16832 /* Record a position XLAT_LOC in the xlat translation table, associated with
16833 the hash entry H. The entry in the translation table will later be
16834 populated with the real symbol dynindx. */
16835
16836 void
16837 _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16838 bfd_vma xlat_loc)
16839 {
16840 struct mips_elf_link_hash_entry *hmips;
16841
16842 hmips = (struct mips_elf_link_hash_entry *) h;
16843 hmips->mipsxhash_loc = xlat_loc;
16844 }