PR26389, nm prints "c" for a common symbol with -flto and -fcommon
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2020 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 bfd_boolean 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 bfd_boolean mips16_stubs_seen;
452
453 /* True if we can generate copy relocs and PLTs. */
454 bfd_boolean use_plts_and_copy_relocs;
455
456 /* True if we can only use 32-bit microMIPS instructions. */
457 bfd_boolean insn32;
458
459 /* True if we suppress checks for invalid branches between ISA modes. */
460 bfd_boolean ignore_branch_isa;
461
462 /* True if we are targetting R6 compact branches. */
463 bfd_boolean compact_branches;
464
465 /* True if we already reported the small-data section overflow. */
466 bfd_boolean small_data_overflow_reported;
467
468 /* True if we use the special `__gnu_absolute_zero' symbol. */
469 bfd_boolean use_absolute_zero;
470
471 /* True if we have been configured for a GNU target. */
472 bfd_boolean 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 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
538 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
539
540 /* A structure used to communicate with htab_traverse callbacks. */
541 struct mips_htab_traverse_info
542 {
543 /* The usual link-wide information. */
544 struct bfd_link_info *info;
545 bfd *output_bfd;
546
547 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
548 bfd_boolean error;
549 };
550
551 /* MIPS ELF private object data. */
552
553 struct mips_elf_obj_tdata
554 {
555 /* Generic ELF private object data. */
556 struct elf_obj_tdata root;
557
558 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
559 bfd *abi_fp_bfd;
560
561 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
562 bfd *abi_msa_bfd;
563
564 /* The abiflags for this object. */
565 Elf_Internal_ABIFlags_v0 abiflags;
566 bfd_boolean abiflags_valid;
567
568 /* The GOT requirements of input bfds. */
569 struct mips_got_info *got;
570
571 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
572 included directly in this one, but there's no point to wasting
573 the memory just for the infrequently called find_nearest_line. */
574 struct mips_elf_find_line *find_line_info;
575
576 /* An array of stub sections indexed by symbol number. */
577 asection **local_stubs;
578 asection **local_call_stubs;
579
580 /* The Irix 5 support uses two virtual sections, which represent
581 text/data symbols defined in dynamic objects. */
582 asymbol *elf_data_symbol;
583 asymbol *elf_text_symbol;
584 asection *elf_data_section;
585 asection *elf_text_section;
586 };
587
588 /* Get MIPS ELF private object data from BFD's tdata. */
589
590 #define mips_elf_tdata(bfd) \
591 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
592
593 #define TLS_RELOC_P(r_type) \
594 (r_type == R_MIPS_TLS_DTPMOD32 \
595 || r_type == R_MIPS_TLS_DTPMOD64 \
596 || r_type == R_MIPS_TLS_DTPREL32 \
597 || r_type == R_MIPS_TLS_DTPREL64 \
598 || r_type == R_MIPS_TLS_GD \
599 || r_type == R_MIPS_TLS_LDM \
600 || r_type == R_MIPS_TLS_DTPREL_HI16 \
601 || r_type == R_MIPS_TLS_DTPREL_LO16 \
602 || r_type == R_MIPS_TLS_GOTTPREL \
603 || r_type == R_MIPS_TLS_TPREL32 \
604 || r_type == R_MIPS_TLS_TPREL64 \
605 || r_type == R_MIPS_TLS_TPREL_HI16 \
606 || r_type == R_MIPS_TLS_TPREL_LO16 \
607 || r_type == R_MIPS16_TLS_GD \
608 || r_type == R_MIPS16_TLS_LDM \
609 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
610 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
611 || r_type == R_MIPS16_TLS_GOTTPREL \
612 || r_type == R_MIPS16_TLS_TPREL_HI16 \
613 || r_type == R_MIPS16_TLS_TPREL_LO16 \
614 || r_type == R_MICROMIPS_TLS_GD \
615 || r_type == R_MICROMIPS_TLS_LDM \
616 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
617 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
618 || r_type == R_MICROMIPS_TLS_GOTTPREL \
619 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
620 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
621
622 /* Structure used to pass information to mips_elf_output_extsym. */
623
624 struct extsym_info
625 {
626 bfd *abfd;
627 struct bfd_link_info *info;
628 struct ecoff_debug_info *debug;
629 const struct ecoff_debug_swap *swap;
630 bfd_boolean failed;
631 };
632
633 /* The names of the runtime procedure table symbols used on IRIX5. */
634
635 static const char * const mips_elf_dynsym_rtproc_names[] =
636 {
637 "_procedure_table",
638 "_procedure_string_table",
639 "_procedure_table_size",
640 NULL
641 };
642
643 /* These structures are used to generate the .compact_rel section on
644 IRIX5. */
645
646 typedef struct
647 {
648 unsigned long id1; /* Always one? */
649 unsigned long num; /* Number of compact relocation entries. */
650 unsigned long id2; /* Always two? */
651 unsigned long offset; /* The file offset of the first relocation. */
652 unsigned long reserved0; /* Zero? */
653 unsigned long reserved1; /* Zero? */
654 } Elf32_compact_rel;
655
656 typedef struct
657 {
658 bfd_byte id1[4];
659 bfd_byte num[4];
660 bfd_byte id2[4];
661 bfd_byte offset[4];
662 bfd_byte reserved0[4];
663 bfd_byte reserved1[4];
664 } Elf32_External_compact_rel;
665
666 typedef struct
667 {
668 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
669 unsigned int rtype : 4; /* Relocation types. See below. */
670 unsigned int dist2to : 8;
671 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
672 unsigned long konst; /* KONST field. See below. */
673 unsigned long vaddr; /* VADDR to be relocated. */
674 } Elf32_crinfo;
675
676 typedef struct
677 {
678 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
679 unsigned int rtype : 4; /* Relocation types. See below. */
680 unsigned int dist2to : 8;
681 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
682 unsigned long konst; /* KONST field. See below. */
683 } Elf32_crinfo2;
684
685 typedef struct
686 {
687 bfd_byte info[4];
688 bfd_byte konst[4];
689 bfd_byte vaddr[4];
690 } Elf32_External_crinfo;
691
692 typedef struct
693 {
694 bfd_byte info[4];
695 bfd_byte konst[4];
696 } Elf32_External_crinfo2;
697
698 /* These are the constants used to swap the bitfields in a crinfo. */
699
700 #define CRINFO_CTYPE (0x1)
701 #define CRINFO_CTYPE_SH (31)
702 #define CRINFO_RTYPE (0xf)
703 #define CRINFO_RTYPE_SH (27)
704 #define CRINFO_DIST2TO (0xff)
705 #define CRINFO_DIST2TO_SH (19)
706 #define CRINFO_RELVADDR (0x7ffff)
707 #define CRINFO_RELVADDR_SH (0)
708
709 /* A compact relocation info has long (3 words) or short (2 words)
710 formats. A short format doesn't have VADDR field and relvaddr
711 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
712 #define CRF_MIPS_LONG 1
713 #define CRF_MIPS_SHORT 0
714
715 /* There are 4 types of compact relocation at least. The value KONST
716 has different meaning for each type:
717
718 (type) (konst)
719 CT_MIPS_REL32 Address in data
720 CT_MIPS_WORD Address in word (XXX)
721 CT_MIPS_GPHI_LO GP - vaddr
722 CT_MIPS_JMPAD Address to jump
723 */
724
725 #define CRT_MIPS_REL32 0xa
726 #define CRT_MIPS_WORD 0xb
727 #define CRT_MIPS_GPHI_LO 0xc
728 #define CRT_MIPS_JMPAD 0xd
729
730 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
731 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
732 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
733 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
734 \f
735 /* The structure of the runtime procedure descriptor created by the
736 loader for use by the static exception system. */
737
738 typedef struct runtime_pdr {
739 bfd_vma adr; /* Memory address of start of procedure. */
740 long regmask; /* Save register mask. */
741 long regoffset; /* Save register offset. */
742 long fregmask; /* Save floating point register mask. */
743 long fregoffset; /* Save floating point register offset. */
744 long frameoffset; /* Frame size. */
745 short framereg; /* Frame pointer register. */
746 short pcreg; /* Offset or reg of return pc. */
747 long irpss; /* Index into the runtime string table. */
748 long reserved;
749 struct exception_info *exception_info;/* Pointer to exception array. */
750 } RPDR, *pRPDR;
751 #define cbRPDR sizeof (RPDR)
752 #define rpdNil ((pRPDR) 0)
753 \f
754 static struct mips_got_entry *mips_elf_create_local_got_entry
755 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
756 struct mips_elf_link_hash_entry *, int);
757 static bfd_boolean mips_elf_sort_hash_table_f
758 (struct mips_elf_link_hash_entry *, void *);
759 static bfd_vma mips_elf_high
760 (bfd_vma);
761 static bfd_boolean mips_elf_create_dynamic_relocation
762 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
763 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
764 bfd_vma *, asection *);
765 static bfd_vma mips_elf_adjust_gp
766 (bfd *, struct mips_got_info *, bfd *);
767
768 /* This will be used when we sort the dynamic relocation records. */
769 static bfd *reldyn_sorting_bfd;
770
771 /* True if ABFD is for CPUs with load interlocking that include
772 non-MIPS1 CPUs and R3900. */
773 #define LOAD_INTERLOCKS_P(abfd) \
774 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
775 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
776
777 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
778 This should be safe for all architectures. We enable this predicate
779 for RM9000 for now. */
780 #define JAL_TO_BAL_P(abfd) \
781 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
782
783 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
784 This should be safe for all architectures. We enable this predicate for
785 all CPUs. */
786 #define JALR_TO_BAL_P(abfd) 1
787
788 /* True if ABFD is for CPUs that are faster if JR is converted to B.
789 This should be safe for all architectures. We enable this predicate for
790 all CPUs. */
791 #define JR_TO_B_P(abfd) 1
792
793 /* True if ABFD is a PIC object. */
794 #define PIC_OBJECT_P(abfd) \
795 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
796
797 /* Nonzero if ABFD is using the O32 ABI. */
798 #define ABI_O32_P(abfd) \
799 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
800
801 /* Nonzero if ABFD is using the N32 ABI. */
802 #define ABI_N32_P(abfd) \
803 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
804
805 /* Nonzero if ABFD is using the N64 ABI. */
806 #define ABI_64_P(abfd) \
807 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
808
809 /* Nonzero if ABFD is using NewABI conventions. */
810 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
811
812 /* Nonzero if ABFD has microMIPS code. */
813 #define MICROMIPS_P(abfd) \
814 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
815
816 /* Nonzero if ABFD is MIPS R6. */
817 #define MIPSR6_P(abfd) \
818 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
819 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
820
821 /* The IRIX compatibility level we are striving for. */
822 #define IRIX_COMPAT(abfd) \
823 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
824
825 /* Whether we are trying to be compatible with IRIX at all. */
826 #define SGI_COMPAT(abfd) \
827 (IRIX_COMPAT (abfd) != ict_none)
828
829 /* The name of the options section. */
830 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
831 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
832
833 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
834 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
835 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
836 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
837
838 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
839 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
840 (strcmp (NAME, ".MIPS.abiflags") == 0)
841
842 /* Whether the section is readonly. */
843 #define MIPS_ELF_READONLY_SECTION(sec) \
844 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
845 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
846
847 /* The name of the stub section. */
848 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
849
850 /* The size of an external REL relocation. */
851 #define MIPS_ELF_REL_SIZE(abfd) \
852 (get_elf_backend_data (abfd)->s->sizeof_rel)
853
854 /* The size of an external RELA relocation. */
855 #define MIPS_ELF_RELA_SIZE(abfd) \
856 (get_elf_backend_data (abfd)->s->sizeof_rela)
857
858 /* The size of an external dynamic table entry. */
859 #define MIPS_ELF_DYN_SIZE(abfd) \
860 (get_elf_backend_data (abfd)->s->sizeof_dyn)
861
862 /* The size of a GOT entry. */
863 #define MIPS_ELF_GOT_SIZE(abfd) \
864 (get_elf_backend_data (abfd)->s->arch_size / 8)
865
866 /* The size of the .rld_map section. */
867 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
868 (get_elf_backend_data (abfd)->s->arch_size / 8)
869
870 /* The size of a symbol-table entry. */
871 #define MIPS_ELF_SYM_SIZE(abfd) \
872 (get_elf_backend_data (abfd)->s->sizeof_sym)
873
874 /* The default alignment for sections, as a power of two. */
875 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
876 (get_elf_backend_data (abfd)->s->log_file_align)
877
878 /* Get word-sized data. */
879 #define MIPS_ELF_GET_WORD(abfd, ptr) \
880 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
881
882 /* Put out word-sized data. */
883 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
884 (ABI_64_P (abfd) \
885 ? bfd_put_64 (abfd, val, ptr) \
886 : bfd_put_32 (abfd, val, ptr))
887
888 /* The opcode for word-sized loads (LW or LD). */
889 #define MIPS_ELF_LOAD_WORD(abfd) \
890 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
891
892 /* Add a dynamic symbol table-entry. */
893 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
894 _bfd_elf_add_dynamic_entry (info, tag, val)
895
896 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
897 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
898
899 /* The name of the dynamic relocation section. */
900 #define MIPS_ELF_REL_DYN_NAME(INFO) \
901 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
902 ? ".rela.dyn" : ".rel.dyn")
903
904 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
905 from smaller values. Start with zero, widen, *then* decrement. */
906 #define MINUS_ONE (((bfd_vma)0) - 1)
907 #define MINUS_TWO (((bfd_vma)0) - 2)
908
909 /* The value to write into got[1] for SVR4 targets, to identify it is
910 a GNU object. The dynamic linker can then use got[1] to store the
911 module pointer. */
912 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
913 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
914
915 /* The offset of $gp from the beginning of the .got section. */
916 #define ELF_MIPS_GP_OFFSET(INFO) \
917 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918 ? 0x0 : 0x7ff0)
919
920 /* The maximum size of the GOT for it to be addressable using 16-bit
921 offsets from $gp. */
922 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
923
924 /* Instructions which appear in a stub. */
925 #define STUB_LW(abfd) \
926 ((ABI_64_P (abfd) \
927 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
928 : 0x8f998010)) /* lw t9,0x8010(gp) */
929 #define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
930 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
931 #define STUB_JALR 0x0320f809 /* jalr ra,t9 */
932 #define STUB_JALRC 0xf8190000 /* jalrc ra,t9 */
933 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
934 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
935 #define STUB_LI16S(abfd, VAL) \
936 ((ABI_64_P (abfd) \
937 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
938 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
939
940 /* Likewise for the microMIPS ASE. */
941 #define STUB_LW_MICROMIPS(abfd) \
942 (ABI_64_P (abfd) \
943 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
944 : 0xff3c8010) /* lw t9,0x8010(gp) */
945 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
946 #define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
947 #define STUB_LUI_MICROMIPS(VAL) \
948 (0x41b80000 + (VAL)) /* lui t8,VAL */
949 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
950 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
951 #define STUB_ORI_MICROMIPS(VAL) \
952 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
953 #define STUB_LI16U_MICROMIPS(VAL) \
954 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
955 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
956 (ABI_64_P (abfd) \
957 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
958 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
959
960 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
961 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
962 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
963 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
964 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
965 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
966
967 /* The name of the dynamic interpreter. This is put in the .interp
968 section. */
969
970 #define ELF_DYNAMIC_INTERPRETER(abfd) \
971 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
972 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
973 : "/usr/lib/libc.so.1")
974
975 #ifdef BFD64
976 #define MNAME(bfd,pre,pos) \
977 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
978 #define ELF_R_SYM(bfd, i) \
979 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
980 #define ELF_R_TYPE(bfd, i) \
981 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
982 #define ELF_R_INFO(bfd, s, t) \
983 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
984 #else
985 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
986 #define ELF_R_SYM(bfd, i) \
987 (ELF32_R_SYM (i))
988 #define ELF_R_TYPE(bfd, i) \
989 (ELF32_R_TYPE (i))
990 #define ELF_R_INFO(bfd, s, t) \
991 (ELF32_R_INFO (s, t))
992 #endif
993 \f
994 /* The mips16 compiler uses a couple of special sections to handle
995 floating point arguments.
996
997 Section names that look like .mips16.fn.FNNAME contain stubs that
998 copy floating point arguments from the fp regs to the gp regs and
999 then jump to FNNAME. If any 32 bit function calls FNNAME, the
1000 call should be redirected to the stub instead. If no 32 bit
1001 function calls FNNAME, the stub should be discarded. We need to
1002 consider any reference to the function, not just a call, because
1003 if the address of the function is taken we will need the stub,
1004 since the address might be passed to a 32 bit function.
1005
1006 Section names that look like .mips16.call.FNNAME contain stubs
1007 that copy floating point arguments from the gp regs to the fp
1008 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1009 then any 16 bit function that calls FNNAME should be redirected
1010 to the stub instead. If FNNAME is not a 32 bit function, the
1011 stub should be discarded.
1012
1013 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1014 which call FNNAME and then copy the return value from the fp regs
1015 to the gp regs. These stubs store the return value in $18 while
1016 calling FNNAME; any function which might call one of these stubs
1017 must arrange to save $18 around the call. (This case is not
1018 needed for 32 bit functions that call 16 bit functions, because
1019 16 bit functions always return floating point values in both
1020 $f0/$f1 and $2/$3.)
1021
1022 Note that in all cases FNNAME might be defined statically.
1023 Therefore, FNNAME is not used literally. Instead, the relocation
1024 information will indicate which symbol the section is for.
1025
1026 We record any stubs that we find in the symbol table. */
1027
1028 #define FN_STUB ".mips16.fn."
1029 #define CALL_STUB ".mips16.call."
1030 #define CALL_FP_STUB ".mips16.call.fp."
1031
1032 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1033 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1034 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
1035 \f
1036 /* The format of the first PLT entry in an O32 executable. */
1037 static const bfd_vma mips_o32_exec_plt0_entry[] =
1038 {
1039 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1040 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1041 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1042 0x031cc023, /* subu $24, $24, $28 */
1043 0x03e07825, /* or t7, ra, zero */
1044 0x0018c082, /* srl $24, $24, 2 */
1045 0x0320f809, /* jalr $25 */
1046 0x2718fffe /* subu $24, $24, 2 */
1047 };
1048
1049 /* The format of the first PLT entry in an O32 executable using compact
1050 jumps. */
1051 static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1052 {
1053 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1054 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1055 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1056 0x031cc023, /* subu $24, $24, $28 */
1057 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1058 0x0018c082, /* srl $24, $24, 2 */
1059 0x2718fffe, /* subu $24, $24, 2 */
1060 0xf8190000 /* jalrc $25 */
1061 };
1062
1063 /* The format of the first PLT entry in an N32 executable. Different
1064 because gp ($28) is not available; we use t2 ($14) instead. */
1065 static const bfd_vma mips_n32_exec_plt0_entry[] =
1066 {
1067 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1068 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1069 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1070 0x030ec023, /* subu $24, $24, $14 */
1071 0x03e07825, /* or t7, ra, zero */
1072 0x0018c082, /* srl $24, $24, 2 */
1073 0x0320f809, /* jalr $25 */
1074 0x2718fffe /* subu $24, $24, 2 */
1075 };
1076
1077 /* The format of the first PLT entry in an N32 executable using compact
1078 jumps. Different because gp ($28) is not available; we use t2 ($14)
1079 instead. */
1080 static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1081 {
1082 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1083 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1084 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1085 0x030ec023, /* subu $24, $24, $14 */
1086 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1087 0x0018c082, /* srl $24, $24, 2 */
1088 0x2718fffe, /* subu $24, $24, 2 */
1089 0xf8190000 /* jalrc $25 */
1090 };
1091
1092 /* The format of the first PLT entry in an N64 executable. Different
1093 from N32 because of the increased size of GOT entries. */
1094 static const bfd_vma mips_n64_exec_plt0_entry[] =
1095 {
1096 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1097 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1098 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1099 0x030ec023, /* subu $24, $24, $14 */
1100 0x03e07825, /* or t7, ra, zero */
1101 0x0018c0c2, /* srl $24, $24, 3 */
1102 0x0320f809, /* jalr $25 */
1103 0x2718fffe /* subu $24, $24, 2 */
1104 };
1105
1106 /* The format of the first PLT entry in an N64 executable using compact
1107 jumps. Different from N32 because of the increased size of GOT
1108 entries. */
1109 static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1110 {
1111 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1112 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1113 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1114 0x030ec023, /* subu $24, $24, $14 */
1115 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1116 0x0018c0c2, /* srl $24, $24, 3 */
1117 0x2718fffe, /* subu $24, $24, 2 */
1118 0xf8190000 /* jalrc $25 */
1119 };
1120
1121
1122 /* The format of the microMIPS first PLT entry in an O32 executable.
1123 We rely on v0 ($2) rather than t8 ($24) to contain the address
1124 of the GOTPLT entry handled, so this stub may only be used when
1125 all the subsequent PLT entries are microMIPS code too.
1126
1127 The trailing NOP is for alignment and correct disassembly only. */
1128 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1129 {
1130 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1131 0xff23, 0x0000, /* lw $25, 0($3) */
1132 0x0535, /* subu $2, $2, $3 */
1133 0x2525, /* srl $2, $2, 2 */
1134 0x3302, 0xfffe, /* subu $24, $2, 2 */
1135 0x0dff, /* move $15, $31 */
1136 0x45f9, /* jalrs $25 */
1137 0x0f83, /* move $28, $3 */
1138 0x0c00 /* nop */
1139 };
1140
1141 /* The format of the microMIPS first PLT entry in an O32 executable
1142 in the insn32 mode. */
1143 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1144 {
1145 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1146 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1147 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1148 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1149 0x001f, 0x7a90, /* or $15, $31, zero */
1150 0x0318, 0x1040, /* srl $24, $24, 2 */
1151 0x03f9, 0x0f3c, /* jalr $25 */
1152 0x3318, 0xfffe /* subu $24, $24, 2 */
1153 };
1154
1155 /* The format of subsequent standard PLT entries. */
1156 static const bfd_vma mips_exec_plt_entry[] =
1157 {
1158 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1159 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1160 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1161 0x03200008 /* jr $25 */
1162 };
1163
1164 static const bfd_vma mipsr6_exec_plt_entry[] =
1165 {
1166 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1167 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1168 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1169 0x03200009 /* jr $25 */
1170 };
1171
1172 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
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 0xd8190000 /* jic $25, 0 */
1178 };
1179
1180 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1181 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1182 directly addressable. */
1183 static const bfd_vma mips16_o32_exec_plt_entry[] =
1184 {
1185 0xb203, /* lw $2, 12($pc) */
1186 0x9a60, /* lw $3, 0($2) */
1187 0x651a, /* move $24, $2 */
1188 0xeb00, /* jr $3 */
1189 0x653b, /* move $25, $3 */
1190 0x6500, /* nop */
1191 0x0000, 0x0000 /* .word (.got.plt entry) */
1192 };
1193
1194 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1195 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1196 static const bfd_vma micromips_o32_exec_plt_entry[] =
1197 {
1198 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1199 0xff22, 0x0000, /* lw $25, 0($2) */
1200 0x4599, /* jr $25 */
1201 0x0f02 /* move $24, $2 */
1202 };
1203
1204 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1205 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1206 {
1207 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1208 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1209 0x0019, 0x0f3c, /* jr $25 */
1210 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1211 };
1212
1213 /* The format of the first PLT entry in a VxWorks executable. */
1214 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1215 {
1216 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1217 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1218 0x8f390008, /* lw t9, 8(t9) */
1219 0x00000000, /* nop */
1220 0x03200008, /* jr t9 */
1221 0x00000000 /* nop */
1222 };
1223
1224 /* The format of subsequent PLT entries. */
1225 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1226 {
1227 0x10000000, /* b .PLT_resolver */
1228 0x24180000, /* li t8, <pltindex> */
1229 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1230 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1231 0x8f390000, /* lw t9, 0(t9) */
1232 0x00000000, /* nop */
1233 0x03200008, /* jr t9 */
1234 0x00000000 /* nop */
1235 };
1236
1237 /* The format of the first PLT entry in a VxWorks shared object. */
1238 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1239 {
1240 0x8f990008, /* lw t9, 8(gp) */
1241 0x00000000, /* nop */
1242 0x03200008, /* jr t9 */
1243 0x00000000, /* nop */
1244 0x00000000, /* nop */
1245 0x00000000 /* nop */
1246 };
1247
1248 /* The format of subsequent PLT entries. */
1249 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1250 {
1251 0x10000000, /* b .PLT_resolver */
1252 0x24180000 /* li t8, <pltindex> */
1253 };
1254 \f
1255 /* microMIPS 32-bit opcode helper installer. */
1256
1257 static void
1258 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1259 {
1260 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1261 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1262 }
1263
1264 /* microMIPS 32-bit opcode helper retriever. */
1265
1266 static bfd_vma
1267 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1268 {
1269 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1270 }
1271 \f
1272 /* Look up an entry in a MIPS ELF linker hash table. */
1273
1274 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1275 ((struct mips_elf_link_hash_entry *) \
1276 elf_link_hash_lookup (&(table)->root, (string), (create), \
1277 (copy), (follow)))
1278
1279 /* Traverse a MIPS ELF linker hash table. */
1280
1281 #define mips_elf_link_hash_traverse(table, func, info) \
1282 (elf_link_hash_traverse \
1283 (&(table)->root, \
1284 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1285 (info)))
1286
1287 /* Find the base offsets for thread-local storage in this object,
1288 for GD/LD and IE/LE respectively. */
1289
1290 #define TP_OFFSET 0x7000
1291 #define DTP_OFFSET 0x8000
1292
1293 static bfd_vma
1294 dtprel_base (struct bfd_link_info *info)
1295 {
1296 /* If tls_sec is NULL, we should have signalled an error already. */
1297 if (elf_hash_table (info)->tls_sec == NULL)
1298 return 0;
1299 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1300 }
1301
1302 static bfd_vma
1303 tprel_base (struct bfd_link_info *info)
1304 {
1305 /* If tls_sec is NULL, we should have signalled an error already. */
1306 if (elf_hash_table (info)->tls_sec == NULL)
1307 return 0;
1308 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1309 }
1310
1311 /* Create an entry in a MIPS ELF linker hash table. */
1312
1313 static struct bfd_hash_entry *
1314 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1315 struct bfd_hash_table *table, const char *string)
1316 {
1317 struct mips_elf_link_hash_entry *ret =
1318 (struct mips_elf_link_hash_entry *) entry;
1319
1320 /* Allocate the structure if it has not already been allocated by a
1321 subclass. */
1322 if (ret == NULL)
1323 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1324 if (ret == NULL)
1325 return (struct bfd_hash_entry *) ret;
1326
1327 /* Call the allocation method of the superclass. */
1328 ret = ((struct mips_elf_link_hash_entry *)
1329 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1330 table, string));
1331 if (ret != NULL)
1332 {
1333 /* Set local fields. */
1334 memset (&ret->esym, 0, sizeof (EXTR));
1335 /* We use -2 as a marker to indicate that the information has
1336 not been set. -1 means there is no associated ifd. */
1337 ret->esym.ifd = -2;
1338 ret->la25_stub = 0;
1339 ret->possibly_dynamic_relocs = 0;
1340 ret->fn_stub = NULL;
1341 ret->call_stub = NULL;
1342 ret->call_fp_stub = NULL;
1343 ret->mipsxhash_loc = 0;
1344 ret->global_got_area = GGA_NONE;
1345 ret->got_only_for_calls = TRUE;
1346 ret->readonly_reloc = FALSE;
1347 ret->has_static_relocs = FALSE;
1348 ret->no_fn_stub = FALSE;
1349 ret->need_fn_stub = FALSE;
1350 ret->has_nonpic_branches = FALSE;
1351 ret->needs_lazy_stub = FALSE;
1352 ret->use_plt_entry = FALSE;
1353 }
1354
1355 return (struct bfd_hash_entry *) ret;
1356 }
1357
1358 /* Allocate MIPS ELF private object data. */
1359
1360 bfd_boolean
1361 _bfd_mips_elf_mkobject (bfd *abfd)
1362 {
1363 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1364 MIPS_ELF_DATA);
1365 }
1366
1367 bfd_boolean
1368 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1369 {
1370 if (!sec->used_by_bfd)
1371 {
1372 struct _mips_elf_section_data *sdata;
1373 size_t amt = sizeof (*sdata);
1374
1375 sdata = bfd_zalloc (abfd, amt);
1376 if (sdata == NULL)
1377 return FALSE;
1378 sec->used_by_bfd = sdata;
1379 }
1380
1381 return _bfd_elf_new_section_hook (abfd, sec);
1382 }
1383 \f
1384 /* Read ECOFF debugging information from a .mdebug section into a
1385 ecoff_debug_info structure. */
1386
1387 bfd_boolean
1388 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1389 struct ecoff_debug_info *debug)
1390 {
1391 HDRR *symhdr;
1392 const struct ecoff_debug_swap *swap;
1393 char *ext_hdr;
1394
1395 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1396 memset (debug, 0, sizeof (*debug));
1397
1398 ext_hdr = bfd_malloc (swap->external_hdr_size);
1399 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1400 goto error_return;
1401
1402 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1403 swap->external_hdr_size))
1404 goto error_return;
1405
1406 symhdr = &debug->symbolic_header;
1407 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1408
1409 /* The symbolic header contains absolute file offsets and sizes to
1410 read. */
1411 #define READ(ptr, offset, count, size, type) \
1412 do \
1413 { \
1414 size_t amt; \
1415 debug->ptr = NULL; \
1416 if (symhdr->count == 0) \
1417 break; \
1418 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
1419 { \
1420 bfd_set_error (bfd_error_file_too_big); \
1421 goto error_return; \
1422 } \
1423 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
1424 goto error_return; \
1425 debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \
1426 if (debug->ptr == NULL) \
1427 goto error_return; \
1428 } while (0)
1429
1430 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1431 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1432 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1433 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1434 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1435 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1436 union aux_ext *);
1437 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1438 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1439 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1440 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1441 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1442 #undef READ
1443
1444 debug->fdr = NULL;
1445
1446 return TRUE;
1447
1448 error_return:
1449 free (ext_hdr);
1450 free (debug->line);
1451 free (debug->external_dnr);
1452 free (debug->external_pdr);
1453 free (debug->external_sym);
1454 free (debug->external_opt);
1455 free (debug->external_aux);
1456 free (debug->ss);
1457 free (debug->ssext);
1458 free (debug->external_fdr);
1459 free (debug->external_rfd);
1460 free (debug->external_ext);
1461 return FALSE;
1462 }
1463 \f
1464 /* Swap RPDR (runtime procedure table entry) for output. */
1465
1466 static void
1467 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1468 {
1469 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1470 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1471 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1472 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1473 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1474 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1475
1476 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1477 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1478
1479 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1480 }
1481
1482 /* Create a runtime procedure table from the .mdebug section. */
1483
1484 static bfd_boolean
1485 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1486 struct bfd_link_info *info, asection *s,
1487 struct ecoff_debug_info *debug)
1488 {
1489 const struct ecoff_debug_swap *swap;
1490 HDRR *hdr = &debug->symbolic_header;
1491 RPDR *rpdr, *rp;
1492 struct rpdr_ext *erp;
1493 void *rtproc;
1494 struct pdr_ext *epdr;
1495 struct sym_ext *esym;
1496 char *ss, **sv;
1497 char *str;
1498 bfd_size_type size;
1499 bfd_size_type count;
1500 unsigned long sindex;
1501 unsigned long i;
1502 PDR pdr;
1503 SYMR sym;
1504 const char *no_name_func = _("static procedure (no name)");
1505
1506 epdr = NULL;
1507 rpdr = NULL;
1508 esym = NULL;
1509 ss = NULL;
1510 sv = NULL;
1511
1512 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1513
1514 sindex = strlen (no_name_func) + 1;
1515 count = hdr->ipdMax;
1516 if (count > 0)
1517 {
1518 size = swap->external_pdr_size;
1519
1520 epdr = bfd_malloc (size * count);
1521 if (epdr == NULL)
1522 goto error_return;
1523
1524 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1525 goto error_return;
1526
1527 size = sizeof (RPDR);
1528 rp = rpdr = bfd_malloc (size * count);
1529 if (rpdr == NULL)
1530 goto error_return;
1531
1532 size = sizeof (char *);
1533 sv = bfd_malloc (size * count);
1534 if (sv == NULL)
1535 goto error_return;
1536
1537 count = hdr->isymMax;
1538 size = swap->external_sym_size;
1539 esym = bfd_malloc (size * count);
1540 if (esym == NULL)
1541 goto error_return;
1542
1543 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1544 goto error_return;
1545
1546 count = hdr->issMax;
1547 ss = bfd_malloc (count);
1548 if (ss == NULL)
1549 goto error_return;
1550 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1551 goto error_return;
1552
1553 count = hdr->ipdMax;
1554 for (i = 0; i < (unsigned long) count; i++, rp++)
1555 {
1556 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1557 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1558 rp->adr = sym.value;
1559 rp->regmask = pdr.regmask;
1560 rp->regoffset = pdr.regoffset;
1561 rp->fregmask = pdr.fregmask;
1562 rp->fregoffset = pdr.fregoffset;
1563 rp->frameoffset = pdr.frameoffset;
1564 rp->framereg = pdr.framereg;
1565 rp->pcreg = pdr.pcreg;
1566 rp->irpss = sindex;
1567 sv[i] = ss + sym.iss;
1568 sindex += strlen (sv[i]) + 1;
1569 }
1570 }
1571
1572 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1573 size = BFD_ALIGN (size, 16);
1574 rtproc = bfd_alloc (abfd, size);
1575 if (rtproc == NULL)
1576 {
1577 mips_elf_hash_table (info)->procedure_count = 0;
1578 goto error_return;
1579 }
1580
1581 mips_elf_hash_table (info)->procedure_count = count + 2;
1582
1583 erp = rtproc;
1584 memset (erp, 0, sizeof (struct rpdr_ext));
1585 erp++;
1586 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1587 strcpy (str, no_name_func);
1588 str += strlen (no_name_func) + 1;
1589 for (i = 0; i < count; i++)
1590 {
1591 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1592 strcpy (str, sv[i]);
1593 str += strlen (sv[i]) + 1;
1594 }
1595 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1596
1597 /* Set the size and contents of .rtproc section. */
1598 s->size = size;
1599 s->contents = rtproc;
1600
1601 /* Skip this section later on (I don't think this currently
1602 matters, but someday it might). */
1603 s->map_head.link_order = NULL;
1604
1605 free (epdr);
1606 free (rpdr);
1607 free (esym);
1608 free (ss);
1609 free (sv);
1610 return TRUE;
1611
1612 error_return:
1613 free (epdr);
1614 free (rpdr);
1615 free (esym);
1616 free (ss);
1617 free (sv);
1618 return FALSE;
1619 }
1620 \f
1621 /* We're going to create a stub for H. Create a symbol for the stub's
1622 value and size, to help make the disassembly easier to read. */
1623
1624 static bfd_boolean
1625 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1626 struct mips_elf_link_hash_entry *h,
1627 const char *prefix, asection *s, bfd_vma value,
1628 bfd_vma size)
1629 {
1630 bfd_boolean micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
1631 struct bfd_link_hash_entry *bh;
1632 struct elf_link_hash_entry *elfh;
1633 char *name;
1634 bfd_boolean res;
1635
1636 if (micromips_p)
1637 value |= 1;
1638
1639 /* Create a new symbol. */
1640 name = concat (prefix, h->root.root.root.string, NULL);
1641 bh = NULL;
1642 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1643 BSF_LOCAL, s, value, NULL,
1644 TRUE, FALSE, &bh);
1645 free (name);
1646 if (! res)
1647 return FALSE;
1648
1649 /* Make it a local function. */
1650 elfh = (struct elf_link_hash_entry *) bh;
1651 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1652 elfh->size = size;
1653 elfh->forced_local = 1;
1654 if (micromips_p)
1655 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
1656 return TRUE;
1657 }
1658
1659 /* We're about to redefine H. Create a symbol to represent H's
1660 current value and size, to help make the disassembly easier
1661 to read. */
1662
1663 static bfd_boolean
1664 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1665 struct mips_elf_link_hash_entry *h,
1666 const char *prefix)
1667 {
1668 struct bfd_link_hash_entry *bh;
1669 struct elf_link_hash_entry *elfh;
1670 char *name;
1671 asection *s;
1672 bfd_vma value;
1673 bfd_boolean res;
1674
1675 /* Read the symbol's value. */
1676 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1677 || h->root.root.type == bfd_link_hash_defweak);
1678 s = h->root.root.u.def.section;
1679 value = h->root.root.u.def.value;
1680
1681 /* Create a new symbol. */
1682 name = concat (prefix, h->root.root.root.string, NULL);
1683 bh = NULL;
1684 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1685 BSF_LOCAL, s, value, NULL,
1686 TRUE, FALSE, &bh);
1687 free (name);
1688 if (! res)
1689 return FALSE;
1690
1691 /* Make it local and copy the other attributes from H. */
1692 elfh = (struct elf_link_hash_entry *) bh;
1693 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1694 elfh->other = h->root.other;
1695 elfh->size = h->root.size;
1696 elfh->forced_local = 1;
1697 return TRUE;
1698 }
1699
1700 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1701 function rather than to a hard-float stub. */
1702
1703 static bfd_boolean
1704 section_allows_mips16_refs_p (asection *section)
1705 {
1706 const char *name;
1707
1708 name = bfd_section_name (section);
1709 return (FN_STUB_P (name)
1710 || CALL_STUB_P (name)
1711 || CALL_FP_STUB_P (name)
1712 || strcmp (name, ".pdr") == 0);
1713 }
1714
1715 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1716 stub section of some kind. Return the R_SYMNDX of the target
1717 function, or 0 if we can't decide which function that is. */
1718
1719 static unsigned long
1720 mips16_stub_symndx (const struct elf_backend_data *bed,
1721 asection *sec ATTRIBUTE_UNUSED,
1722 const Elf_Internal_Rela *relocs,
1723 const Elf_Internal_Rela *relend)
1724 {
1725 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1726 const Elf_Internal_Rela *rel;
1727
1728 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1729 one in a compound relocation. */
1730 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1731 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1732 return ELF_R_SYM (sec->owner, rel->r_info);
1733
1734 /* Otherwise trust the first relocation, whatever its kind. This is
1735 the traditional behavior. */
1736 if (relocs < relend)
1737 return ELF_R_SYM (sec->owner, relocs->r_info);
1738
1739 return 0;
1740 }
1741
1742 /* Check the mips16 stubs for a particular symbol, and see if we can
1743 discard them. */
1744
1745 static void
1746 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1747 struct mips_elf_link_hash_entry *h)
1748 {
1749 /* Dynamic symbols must use the standard call interface, in case other
1750 objects try to call them. */
1751 if (h->fn_stub != NULL
1752 && h->root.dynindx != -1)
1753 {
1754 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1755 h->need_fn_stub = TRUE;
1756 }
1757
1758 if (h->fn_stub != NULL
1759 && ! h->need_fn_stub)
1760 {
1761 /* We don't need the fn_stub; the only references to this symbol
1762 are 16 bit calls. Clobber the size to 0 to prevent it from
1763 being included in the link. */
1764 h->fn_stub->size = 0;
1765 h->fn_stub->flags &= ~SEC_RELOC;
1766 h->fn_stub->reloc_count = 0;
1767 h->fn_stub->flags |= SEC_EXCLUDE;
1768 h->fn_stub->output_section = bfd_abs_section_ptr;
1769 }
1770
1771 if (h->call_stub != NULL
1772 && ELF_ST_IS_MIPS16 (h->root.other))
1773 {
1774 /* We don't need the call_stub; this is a 16 bit function, so
1775 calls from other 16 bit functions are OK. Clobber the size
1776 to 0 to prevent it from being included in the link. */
1777 h->call_stub->size = 0;
1778 h->call_stub->flags &= ~SEC_RELOC;
1779 h->call_stub->reloc_count = 0;
1780 h->call_stub->flags |= SEC_EXCLUDE;
1781 h->call_stub->output_section = bfd_abs_section_ptr;
1782 }
1783
1784 if (h->call_fp_stub != NULL
1785 && ELF_ST_IS_MIPS16 (h->root.other))
1786 {
1787 /* We don't need the call_stub; this is a 16 bit function, so
1788 calls from other 16 bit functions are OK. Clobber the size
1789 to 0 to prevent it from being included in the link. */
1790 h->call_fp_stub->size = 0;
1791 h->call_fp_stub->flags &= ~SEC_RELOC;
1792 h->call_fp_stub->reloc_count = 0;
1793 h->call_fp_stub->flags |= SEC_EXCLUDE;
1794 h->call_fp_stub->output_section = bfd_abs_section_ptr;
1795 }
1796 }
1797
1798 /* Hashtable callbacks for mips_elf_la25_stubs. */
1799
1800 static hashval_t
1801 mips_elf_la25_stub_hash (const void *entry_)
1802 {
1803 const struct mips_elf_la25_stub *entry;
1804
1805 entry = (struct mips_elf_la25_stub *) entry_;
1806 return entry->h->root.root.u.def.section->id
1807 + entry->h->root.root.u.def.value;
1808 }
1809
1810 static int
1811 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1812 {
1813 const struct mips_elf_la25_stub *entry1, *entry2;
1814
1815 entry1 = (struct mips_elf_la25_stub *) entry1_;
1816 entry2 = (struct mips_elf_la25_stub *) entry2_;
1817 return ((entry1->h->root.root.u.def.section
1818 == entry2->h->root.root.u.def.section)
1819 && (entry1->h->root.root.u.def.value
1820 == entry2->h->root.root.u.def.value));
1821 }
1822
1823 /* Called by the linker to set up the la25 stub-creation code. FN is
1824 the linker's implementation of add_stub_function. Return true on
1825 success. */
1826
1827 bfd_boolean
1828 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1829 asection *(*fn) (const char *, asection *,
1830 asection *))
1831 {
1832 struct mips_elf_link_hash_table *htab;
1833
1834 htab = mips_elf_hash_table (info);
1835 if (htab == NULL)
1836 return FALSE;
1837
1838 htab->add_stub_section = fn;
1839 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1840 mips_elf_la25_stub_eq, NULL);
1841 if (htab->la25_stubs == NULL)
1842 return FALSE;
1843
1844 return TRUE;
1845 }
1846
1847 /* Return true if H is a locally-defined PIC function, in the sense
1848 that it or its fn_stub might need $25 to be valid on entry.
1849 Note that MIPS16 functions set up $gp using PC-relative instructions,
1850 so they themselves never need $25 to be valid. Only non-MIPS16
1851 entry points are of interest here. */
1852
1853 static bfd_boolean
1854 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1855 {
1856 return ((h->root.root.type == bfd_link_hash_defined
1857 || h->root.root.type == bfd_link_hash_defweak)
1858 && h->root.def_regular
1859 && !bfd_is_abs_section (h->root.root.u.def.section)
1860 && !bfd_is_und_section (h->root.root.u.def.section)
1861 && (!ELF_ST_IS_MIPS16 (h->root.other)
1862 || (h->fn_stub && h->need_fn_stub))
1863 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1864 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1865 }
1866
1867 /* Set *SEC to the input section that contains the target of STUB.
1868 Return the offset of the target from the start of that section. */
1869
1870 static bfd_vma
1871 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1872 asection **sec)
1873 {
1874 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1875 {
1876 BFD_ASSERT (stub->h->need_fn_stub);
1877 *sec = stub->h->fn_stub;
1878 return 0;
1879 }
1880 else
1881 {
1882 *sec = stub->h->root.root.u.def.section;
1883 return stub->h->root.root.u.def.value;
1884 }
1885 }
1886
1887 /* STUB describes an la25 stub that we have decided to implement
1888 by inserting an LUI/ADDIU pair before the target function.
1889 Create the section and redirect the function symbol to it. */
1890
1891 static bfd_boolean
1892 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1893 struct bfd_link_info *info)
1894 {
1895 struct mips_elf_link_hash_table *htab;
1896 char *name;
1897 asection *s, *input_section;
1898 unsigned int align;
1899
1900 htab = mips_elf_hash_table (info);
1901 if (htab == NULL)
1902 return FALSE;
1903
1904 /* Create a unique name for the new section. */
1905 name = bfd_malloc (11 + sizeof (".text.stub."));
1906 if (name == NULL)
1907 return FALSE;
1908 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1909
1910 /* Create the section. */
1911 mips_elf_get_la25_target (stub, &input_section);
1912 s = htab->add_stub_section (name, input_section,
1913 input_section->output_section);
1914 if (s == NULL)
1915 return FALSE;
1916
1917 /* Make sure that any padding goes before the stub. */
1918 align = input_section->alignment_power;
1919 if (!bfd_set_section_alignment (s, align))
1920 return FALSE;
1921 if (align > 3)
1922 s->size = (1 << align) - 8;
1923
1924 /* Create a symbol for the stub. */
1925 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1926 stub->stub_section = s;
1927 stub->offset = s->size;
1928
1929 /* Allocate room for it. */
1930 s->size += 8;
1931 return TRUE;
1932 }
1933
1934 /* STUB describes an la25 stub that we have decided to implement
1935 with a separate trampoline. Allocate room for it and redirect
1936 the function symbol to it. */
1937
1938 static bfd_boolean
1939 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1940 struct bfd_link_info *info)
1941 {
1942 struct mips_elf_link_hash_table *htab;
1943 asection *s;
1944
1945 htab = mips_elf_hash_table (info);
1946 if (htab == NULL)
1947 return FALSE;
1948
1949 /* Create a trampoline section, if we haven't already. */
1950 s = htab->strampoline;
1951 if (s == NULL)
1952 {
1953 asection *input_section = stub->h->root.root.u.def.section;
1954 s = htab->add_stub_section (".text", NULL,
1955 input_section->output_section);
1956 if (s == NULL || !bfd_set_section_alignment (s, 4))
1957 return FALSE;
1958 htab->strampoline = s;
1959 }
1960
1961 /* Create a symbol for the stub. */
1962 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1963 stub->stub_section = s;
1964 stub->offset = s->size;
1965
1966 /* Allocate room for it. */
1967 s->size += 16;
1968 return TRUE;
1969 }
1970
1971 /* H describes a symbol that needs an la25 stub. Make sure that an
1972 appropriate stub exists and point H at it. */
1973
1974 static bfd_boolean
1975 mips_elf_add_la25_stub (struct bfd_link_info *info,
1976 struct mips_elf_link_hash_entry *h)
1977 {
1978 struct mips_elf_link_hash_table *htab;
1979 struct mips_elf_la25_stub search, *stub;
1980 bfd_boolean use_trampoline_p;
1981 asection *s;
1982 bfd_vma value;
1983 void **slot;
1984
1985 /* Describe the stub we want. */
1986 search.stub_section = NULL;
1987 search.offset = 0;
1988 search.h = h;
1989
1990 /* See if we've already created an equivalent stub. */
1991 htab = mips_elf_hash_table (info);
1992 if (htab == NULL)
1993 return FALSE;
1994
1995 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1996 if (slot == NULL)
1997 return FALSE;
1998
1999 stub = (struct mips_elf_la25_stub *) *slot;
2000 if (stub != NULL)
2001 {
2002 /* We can reuse the existing stub. */
2003 h->la25_stub = stub;
2004 return TRUE;
2005 }
2006
2007 /* Create a permanent copy of ENTRY and add it to the hash table. */
2008 stub = bfd_malloc (sizeof (search));
2009 if (stub == NULL)
2010 return FALSE;
2011 *stub = search;
2012 *slot = stub;
2013
2014 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2015 of the section and if we would need no more than 2 nops. */
2016 value = mips_elf_get_la25_target (stub, &s);
2017 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2018 value &= ~1;
2019 use_trampoline_p = (value != 0 || s->alignment_power > 4);
2020
2021 h->la25_stub = stub;
2022 return (use_trampoline_p
2023 ? mips_elf_add_la25_trampoline (stub, info)
2024 : mips_elf_add_la25_intro (stub, info));
2025 }
2026
2027 /* A mips_elf_link_hash_traverse callback that is called before sizing
2028 sections. DATA points to a mips_htab_traverse_info structure. */
2029
2030 static bfd_boolean
2031 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2032 {
2033 struct mips_htab_traverse_info *hti;
2034
2035 hti = (struct mips_htab_traverse_info *) data;
2036 if (!bfd_link_relocatable (hti->info))
2037 mips_elf_check_mips16_stubs (hti->info, h);
2038
2039 if (mips_elf_local_pic_function_p (h))
2040 {
2041 /* PR 12845: If H is in a section that has been garbage
2042 collected it will have its output section set to *ABS*. */
2043 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2044 return TRUE;
2045
2046 /* H is a function that might need $25 to be valid on entry.
2047 If we're creating a non-PIC relocatable object, mark H as
2048 being PIC. If we're creating a non-relocatable object with
2049 non-PIC branches and jumps to H, make sure that H has an la25
2050 stub. */
2051 if (bfd_link_relocatable (hti->info))
2052 {
2053 if (!PIC_OBJECT_P (hti->output_bfd))
2054 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2055 }
2056 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2057 {
2058 hti->error = TRUE;
2059 return FALSE;
2060 }
2061 }
2062 return TRUE;
2063 }
2064 \f
2065 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2066 Most mips16 instructions are 16 bits, but these instructions
2067 are 32 bits.
2068
2069 The format of these instructions is:
2070
2071 +--------------+--------------------------------+
2072 | JALX | X| Imm 20:16 | Imm 25:21 |
2073 +--------------+--------------------------------+
2074 | Immediate 15:0 |
2075 +-----------------------------------------------+
2076
2077 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2078 Note that the immediate value in the first word is swapped.
2079
2080 When producing a relocatable object file, R_MIPS16_26 is
2081 handled mostly like R_MIPS_26. In particular, the addend is
2082 stored as a straight 26-bit value in a 32-bit instruction.
2083 (gas makes life simpler for itself by never adjusting a
2084 R_MIPS16_26 reloc to be against a section, so the addend is
2085 always zero). However, the 32 bit instruction is stored as 2
2086 16-bit values, rather than a single 32-bit value. In a
2087 big-endian file, the result is the same; in a little-endian
2088 file, the two 16-bit halves of the 32 bit value are swapped.
2089 This is so that a disassembler can recognize the jal
2090 instruction.
2091
2092 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2093 instruction stored as two 16-bit values. The addend A is the
2094 contents of the targ26 field. The calculation is the same as
2095 R_MIPS_26. When storing the calculated value, reorder the
2096 immediate value as shown above, and don't forget to store the
2097 value as two 16-bit values.
2098
2099 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2100 defined as
2101
2102 big-endian:
2103 +--------+----------------------+
2104 | | |
2105 | | targ26-16 |
2106 |31 26|25 0|
2107 +--------+----------------------+
2108
2109 little-endian:
2110 +----------+------+-------------+
2111 | | | |
2112 | sub1 | | sub2 |
2113 |0 9|10 15|16 31|
2114 +----------+--------------------+
2115 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2116 ((sub1 << 16) | sub2)).
2117
2118 When producing a relocatable object file, the calculation is
2119 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2120 When producing a fully linked file, the calculation is
2121 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2122 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2123
2124 The table below lists the other MIPS16 instruction relocations.
2125 Each one is calculated in the same way as the non-MIPS16 relocation
2126 given on the right, but using the extended MIPS16 layout of 16-bit
2127 immediate fields:
2128
2129 R_MIPS16_GPREL R_MIPS_GPREL16
2130 R_MIPS16_GOT16 R_MIPS_GOT16
2131 R_MIPS16_CALL16 R_MIPS_CALL16
2132 R_MIPS16_HI16 R_MIPS_HI16
2133 R_MIPS16_LO16 R_MIPS_LO16
2134
2135 A typical instruction will have a format like this:
2136
2137 +--------------+--------------------------------+
2138 | EXTEND | Imm 10:5 | Imm 15:11 |
2139 +--------------+--------------------------------+
2140 | Major | rx | ry | Imm 4:0 |
2141 +--------------+--------------------------------+
2142
2143 EXTEND is the five bit value 11110. Major is the instruction
2144 opcode.
2145
2146 All we need to do here is shuffle the bits appropriately.
2147 As above, the two 16-bit halves must be swapped on a
2148 little-endian system.
2149
2150 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2151 relocatable field is shifted by 1 rather than 2 and the same bit
2152 shuffling is done as with the relocations above. */
2153
2154 static inline bfd_boolean
2155 mips16_reloc_p (int r_type)
2156 {
2157 switch (r_type)
2158 {
2159 case R_MIPS16_26:
2160 case R_MIPS16_GPREL:
2161 case R_MIPS16_GOT16:
2162 case R_MIPS16_CALL16:
2163 case R_MIPS16_HI16:
2164 case R_MIPS16_LO16:
2165 case R_MIPS16_TLS_GD:
2166 case R_MIPS16_TLS_LDM:
2167 case R_MIPS16_TLS_DTPREL_HI16:
2168 case R_MIPS16_TLS_DTPREL_LO16:
2169 case R_MIPS16_TLS_GOTTPREL:
2170 case R_MIPS16_TLS_TPREL_HI16:
2171 case R_MIPS16_TLS_TPREL_LO16:
2172 case R_MIPS16_PC16_S1:
2173 return TRUE;
2174
2175 default:
2176 return FALSE;
2177 }
2178 }
2179
2180 /* Check if a microMIPS reloc. */
2181
2182 static inline bfd_boolean
2183 micromips_reloc_p (unsigned int r_type)
2184 {
2185 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2186 }
2187
2188 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2189 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2190 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2191
2192 static inline bfd_boolean
2193 micromips_reloc_shuffle_p (unsigned int r_type)
2194 {
2195 return (micromips_reloc_p (r_type)
2196 && r_type != R_MICROMIPS_PC7_S1
2197 && r_type != R_MICROMIPS_PC10_S1);
2198 }
2199
2200 static inline bfd_boolean
2201 got16_reloc_p (int r_type)
2202 {
2203 return (r_type == R_MIPS_GOT16
2204 || r_type == R_MIPS16_GOT16
2205 || r_type == R_MICROMIPS_GOT16);
2206 }
2207
2208 static inline bfd_boolean
2209 call16_reloc_p (int r_type)
2210 {
2211 return (r_type == R_MIPS_CALL16
2212 || r_type == R_MIPS16_CALL16
2213 || r_type == R_MICROMIPS_CALL16);
2214 }
2215
2216 static inline bfd_boolean
2217 got_disp_reloc_p (unsigned int r_type)
2218 {
2219 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2220 }
2221
2222 static inline bfd_boolean
2223 got_page_reloc_p (unsigned int r_type)
2224 {
2225 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2226 }
2227
2228 static inline bfd_boolean
2229 got_lo16_reloc_p (unsigned int r_type)
2230 {
2231 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2232 }
2233
2234 static inline bfd_boolean
2235 call_hi16_reloc_p (unsigned int r_type)
2236 {
2237 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2238 }
2239
2240 static inline bfd_boolean
2241 call_lo16_reloc_p (unsigned int r_type)
2242 {
2243 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2244 }
2245
2246 static inline bfd_boolean
2247 hi16_reloc_p (int r_type)
2248 {
2249 return (r_type == R_MIPS_HI16
2250 || r_type == R_MIPS16_HI16
2251 || r_type == R_MICROMIPS_HI16
2252 || r_type == R_MIPS_PCHI16);
2253 }
2254
2255 static inline bfd_boolean
2256 lo16_reloc_p (int r_type)
2257 {
2258 return (r_type == R_MIPS_LO16
2259 || r_type == R_MIPS16_LO16
2260 || r_type == R_MICROMIPS_LO16
2261 || r_type == R_MIPS_PCLO16);
2262 }
2263
2264 static inline bfd_boolean
2265 mips16_call_reloc_p (int r_type)
2266 {
2267 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2268 }
2269
2270 static inline bfd_boolean
2271 jal_reloc_p (int r_type)
2272 {
2273 return (r_type == R_MIPS_26
2274 || r_type == R_MIPS16_26
2275 || r_type == R_MICROMIPS_26_S1);
2276 }
2277
2278 static inline bfd_boolean
2279 b_reloc_p (int r_type)
2280 {
2281 return (r_type == R_MIPS_PC26_S2
2282 || r_type == R_MIPS_PC21_S2
2283 || r_type == R_MIPS_PC16
2284 || r_type == R_MIPS_GNU_REL16_S2
2285 || r_type == R_MIPS16_PC16_S1
2286 || r_type == R_MICROMIPS_PC16_S1
2287 || r_type == R_MICROMIPS_PC10_S1
2288 || r_type == R_MICROMIPS_PC7_S1);
2289 }
2290
2291 static inline bfd_boolean
2292 aligned_pcrel_reloc_p (int r_type)
2293 {
2294 return (r_type == R_MIPS_PC18_S3
2295 || r_type == R_MIPS_PC19_S2);
2296 }
2297
2298 static inline bfd_boolean
2299 branch_reloc_p (int r_type)
2300 {
2301 return (r_type == R_MIPS_26
2302 || r_type == R_MIPS_PC26_S2
2303 || r_type == R_MIPS_PC21_S2
2304 || r_type == R_MIPS_PC16
2305 || r_type == R_MIPS_GNU_REL16_S2);
2306 }
2307
2308 static inline bfd_boolean
2309 mips16_branch_reloc_p (int r_type)
2310 {
2311 return (r_type == R_MIPS16_26
2312 || r_type == R_MIPS16_PC16_S1);
2313 }
2314
2315 static inline bfd_boolean
2316 micromips_branch_reloc_p (int r_type)
2317 {
2318 return (r_type == R_MICROMIPS_26_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 bfd_boolean
2325 tls_gd_reloc_p (unsigned int r_type)
2326 {
2327 return (r_type == R_MIPS_TLS_GD
2328 || r_type == R_MIPS16_TLS_GD
2329 || r_type == R_MICROMIPS_TLS_GD);
2330 }
2331
2332 static inline bfd_boolean
2333 tls_ldm_reloc_p (unsigned int r_type)
2334 {
2335 return (r_type == R_MIPS_TLS_LDM
2336 || r_type == R_MIPS16_TLS_LDM
2337 || r_type == R_MICROMIPS_TLS_LDM);
2338 }
2339
2340 static inline bfd_boolean
2341 tls_gottprel_reloc_p (unsigned int r_type)
2342 {
2343 return (r_type == R_MIPS_TLS_GOTTPREL
2344 || r_type == R_MIPS16_TLS_GOTTPREL
2345 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2346 }
2347
2348 void
2349 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2350 bfd_boolean jal_shuffle, bfd_byte *data)
2351 {
2352 bfd_vma first, second, val;
2353
2354 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2355 return;
2356
2357 /* Pick up the first and second halfwords of the instruction. */
2358 first = bfd_get_16 (abfd, data);
2359 second = bfd_get_16 (abfd, data + 2);
2360 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2361 val = first << 16 | second;
2362 else if (r_type != R_MIPS16_26)
2363 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2364 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2365 else
2366 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2367 | ((first & 0x1f) << 21) | second);
2368 bfd_put_32 (abfd, val, data);
2369 }
2370
2371 void
2372 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2373 bfd_boolean jal_shuffle, bfd_byte *data)
2374 {
2375 bfd_vma first, second, val;
2376
2377 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2378 return;
2379
2380 val = bfd_get_32 (abfd, data);
2381 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2382 {
2383 second = val & 0xffff;
2384 first = val >> 16;
2385 }
2386 else if (r_type != R_MIPS16_26)
2387 {
2388 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2389 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2390 }
2391 else
2392 {
2393 second = val & 0xffff;
2394 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2395 | ((val >> 21) & 0x1f);
2396 }
2397 bfd_put_16 (abfd, second, data + 2);
2398 bfd_put_16 (abfd, first, data);
2399 }
2400
2401 bfd_reloc_status_type
2402 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2403 arelent *reloc_entry, asection *input_section,
2404 bfd_boolean relocatable, void *data, bfd_vma gp)
2405 {
2406 bfd_vma relocation;
2407 bfd_signed_vma val;
2408 bfd_reloc_status_type status;
2409
2410 if (bfd_is_com_section (symbol->section))
2411 relocation = 0;
2412 else
2413 relocation = symbol->value;
2414
2415 relocation += symbol->section->output_section->vma;
2416 relocation += symbol->section->output_offset;
2417
2418 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2419 return bfd_reloc_outofrange;
2420
2421 /* Set val to the offset into the section or symbol. */
2422 val = reloc_entry->addend;
2423
2424 _bfd_mips_elf_sign_extend (val, 16);
2425
2426 /* Adjust val for the final section location and GP value. If we
2427 are producing relocatable output, we don't want to do this for
2428 an external symbol. */
2429 if (! relocatable
2430 || (symbol->flags & BSF_SECTION_SYM) != 0)
2431 val += relocation - gp;
2432
2433 if (reloc_entry->howto->partial_inplace)
2434 {
2435 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2436 (bfd_byte *) data
2437 + reloc_entry->address);
2438 if (status != bfd_reloc_ok)
2439 return status;
2440 }
2441 else
2442 reloc_entry->addend = val;
2443
2444 if (relocatable)
2445 reloc_entry->address += input_section->output_offset;
2446
2447 return bfd_reloc_ok;
2448 }
2449
2450 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2451 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2452 that contains the relocation field and DATA points to the start of
2453 INPUT_SECTION. */
2454
2455 struct mips_hi16
2456 {
2457 struct mips_hi16 *next;
2458 bfd_byte *data;
2459 asection *input_section;
2460 arelent rel;
2461 };
2462
2463 /* FIXME: This should not be a static variable. */
2464
2465 static struct mips_hi16 *mips_hi16_list;
2466
2467 /* A howto special_function for REL *HI16 relocations. We can only
2468 calculate the correct value once we've seen the partnering
2469 *LO16 relocation, so just save the information for later.
2470
2471 The ABI requires that the *LO16 immediately follow the *HI16.
2472 However, as a GNU extension, we permit an arbitrary number of
2473 *HI16s to be associated with a single *LO16. This significantly
2474 simplies the relocation handling in gcc. */
2475
2476 bfd_reloc_status_type
2477 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2478 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2479 asection *input_section, bfd *output_bfd,
2480 char **error_message ATTRIBUTE_UNUSED)
2481 {
2482 struct mips_hi16 *n;
2483
2484 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2485 return bfd_reloc_outofrange;
2486
2487 n = bfd_malloc (sizeof *n);
2488 if (n == NULL)
2489 return bfd_reloc_outofrange;
2490
2491 n->next = mips_hi16_list;
2492 n->data = data;
2493 n->input_section = input_section;
2494 n->rel = *reloc_entry;
2495 mips_hi16_list = n;
2496
2497 if (output_bfd != NULL)
2498 reloc_entry->address += input_section->output_offset;
2499
2500 return bfd_reloc_ok;
2501 }
2502
2503 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2504 like any other 16-bit relocation when applied to global symbols, but is
2505 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2506
2507 bfd_reloc_status_type
2508 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2509 void *data, asection *input_section,
2510 bfd *output_bfd, char **error_message)
2511 {
2512 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2513 || bfd_is_und_section (bfd_asymbol_section (symbol))
2514 || bfd_is_com_section (bfd_asymbol_section (symbol)))
2515 /* The relocation is against a global symbol. */
2516 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2517 input_section, output_bfd,
2518 error_message);
2519
2520 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2521 input_section, output_bfd, error_message);
2522 }
2523
2524 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2525 is a straightforward 16 bit inplace relocation, but we must deal with
2526 any partnering high-part relocations as well. */
2527
2528 bfd_reloc_status_type
2529 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2530 void *data, asection *input_section,
2531 bfd *output_bfd, char **error_message)
2532 {
2533 bfd_vma vallo;
2534 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2535
2536 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2537 return bfd_reloc_outofrange;
2538
2539 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2540 location);
2541 vallo = bfd_get_32 (abfd, location);
2542 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2543 location);
2544
2545 while (mips_hi16_list != NULL)
2546 {
2547 bfd_reloc_status_type ret;
2548 struct mips_hi16 *hi;
2549
2550 hi = mips_hi16_list;
2551
2552 /* R_MIPS*_GOT16 relocations are something of a special case. We
2553 want to install the addend in the same way as for a R_MIPS*_HI16
2554 relocation (with a rightshift of 16). However, since GOT16
2555 relocations can also be used with global symbols, their howto
2556 has a rightshift of 0. */
2557 if (hi->rel.howto->type == R_MIPS_GOT16)
2558 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2559 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2560 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2561 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2562 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2563
2564 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2565 carry or borrow will induce a change of +1 or -1 in the high part. */
2566 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2567
2568 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2569 hi->input_section, output_bfd,
2570 error_message);
2571 if (ret != bfd_reloc_ok)
2572 return ret;
2573
2574 mips_hi16_list = hi->next;
2575 free (hi);
2576 }
2577
2578 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2579 input_section, output_bfd,
2580 error_message);
2581 }
2582
2583 /* A generic howto special_function. This calculates and installs the
2584 relocation itself, thus avoiding the oft-discussed problems in
2585 bfd_perform_relocation and bfd_install_relocation. */
2586
2587 bfd_reloc_status_type
2588 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2589 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2590 asection *input_section, bfd *output_bfd,
2591 char **error_message ATTRIBUTE_UNUSED)
2592 {
2593 bfd_signed_vma val;
2594 bfd_reloc_status_type status;
2595 bfd_boolean relocatable;
2596
2597 relocatable = (output_bfd != NULL);
2598
2599 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2600 return bfd_reloc_outofrange;
2601
2602 /* Build up the field adjustment in VAL. */
2603 val = 0;
2604 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2605 {
2606 /* Either we're calculating the final field value or we have a
2607 relocation against a section symbol. Add in the section's
2608 offset or address. */
2609 val += symbol->section->output_section->vma;
2610 val += symbol->section->output_offset;
2611 }
2612
2613 if (!relocatable)
2614 {
2615 /* We're calculating the final field value. Add in the symbol's value
2616 and, if pc-relative, subtract the address of the field itself. */
2617 val += symbol->value;
2618 if (reloc_entry->howto->pc_relative)
2619 {
2620 val -= input_section->output_section->vma;
2621 val -= input_section->output_offset;
2622 val -= reloc_entry->address;
2623 }
2624 }
2625
2626 /* VAL is now the final adjustment. If we're keeping this relocation
2627 in the output file, and if the relocation uses a separate addend,
2628 we just need to add VAL to that addend. Otherwise we need to add
2629 VAL to the relocation field itself. */
2630 if (relocatable && !reloc_entry->howto->partial_inplace)
2631 reloc_entry->addend += val;
2632 else
2633 {
2634 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2635
2636 /* Add in the separate addend, if any. */
2637 val += reloc_entry->addend;
2638
2639 /* Add VAL to the relocation field. */
2640 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2641 location);
2642 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2643 location);
2644 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2645 location);
2646
2647 if (status != bfd_reloc_ok)
2648 return status;
2649 }
2650
2651 if (relocatable)
2652 reloc_entry->address += input_section->output_offset;
2653
2654 return bfd_reloc_ok;
2655 }
2656 \f
2657 /* Swap an entry in a .gptab section. Note that these routines rely
2658 on the equivalence of the two elements of the union. */
2659
2660 static void
2661 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2662 Elf32_gptab *in)
2663 {
2664 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2665 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2666 }
2667
2668 static void
2669 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2670 Elf32_External_gptab *ex)
2671 {
2672 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2673 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2674 }
2675
2676 static void
2677 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2678 Elf32_External_compact_rel *ex)
2679 {
2680 H_PUT_32 (abfd, in->id1, ex->id1);
2681 H_PUT_32 (abfd, in->num, ex->num);
2682 H_PUT_32 (abfd, in->id2, ex->id2);
2683 H_PUT_32 (abfd, in->offset, ex->offset);
2684 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2685 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2686 }
2687
2688 static void
2689 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2690 Elf32_External_crinfo *ex)
2691 {
2692 unsigned long l;
2693
2694 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2695 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2696 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2697 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2698 H_PUT_32 (abfd, l, ex->info);
2699 H_PUT_32 (abfd, in->konst, ex->konst);
2700 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2701 }
2702 \f
2703 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2704 routines swap this structure in and out. They are used outside of
2705 BFD, so they are globally visible. */
2706
2707 void
2708 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2709 Elf32_RegInfo *in)
2710 {
2711 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2712 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2713 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2714 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2715 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2716 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2717 }
2718
2719 void
2720 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2721 Elf32_External_RegInfo *ex)
2722 {
2723 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2724 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2725 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2726 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2727 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2728 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2729 }
2730
2731 /* In the 64 bit ABI, the .MIPS.options section holds register
2732 information in an Elf64_Reginfo structure. These routines swap
2733 them in and out. They are globally visible because they are used
2734 outside of BFD. These routines are here so that gas can call them
2735 without worrying about whether the 64 bit ABI has been included. */
2736
2737 void
2738 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2739 Elf64_Internal_RegInfo *in)
2740 {
2741 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2742 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2743 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2744 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2745 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2746 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2747 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2748 }
2749
2750 void
2751 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2752 Elf64_External_RegInfo *ex)
2753 {
2754 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2755 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2756 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2757 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2758 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2759 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2760 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2761 }
2762
2763 /* Swap in an options header. */
2764
2765 void
2766 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2767 Elf_Internal_Options *in)
2768 {
2769 in->kind = H_GET_8 (abfd, ex->kind);
2770 in->size = H_GET_8 (abfd, ex->size);
2771 in->section = H_GET_16 (abfd, ex->section);
2772 in->info = H_GET_32 (abfd, ex->info);
2773 }
2774
2775 /* Swap out an options header. */
2776
2777 void
2778 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2779 Elf_External_Options *ex)
2780 {
2781 H_PUT_8 (abfd, in->kind, ex->kind);
2782 H_PUT_8 (abfd, in->size, ex->size);
2783 H_PUT_16 (abfd, in->section, ex->section);
2784 H_PUT_32 (abfd, in->info, ex->info);
2785 }
2786
2787 /* Swap in an abiflags structure. */
2788
2789 void
2790 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2791 const Elf_External_ABIFlags_v0 *ex,
2792 Elf_Internal_ABIFlags_v0 *in)
2793 {
2794 in->version = H_GET_16 (abfd, ex->version);
2795 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2796 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2797 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2798 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2799 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2800 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2801 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2802 in->ases = H_GET_32 (abfd, ex->ases);
2803 in->flags1 = H_GET_32 (abfd, ex->flags1);
2804 in->flags2 = H_GET_32 (abfd, ex->flags2);
2805 }
2806
2807 /* Swap out an abiflags structure. */
2808
2809 void
2810 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2811 const Elf_Internal_ABIFlags_v0 *in,
2812 Elf_External_ABIFlags_v0 *ex)
2813 {
2814 H_PUT_16 (abfd, in->version, ex->version);
2815 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2816 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2817 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2818 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2819 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2820 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2821 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2822 H_PUT_32 (abfd, in->ases, ex->ases);
2823 H_PUT_32 (abfd, in->flags1, ex->flags1);
2824 H_PUT_32 (abfd, in->flags2, ex->flags2);
2825 }
2826 \f
2827 /* This function is called via qsort() to sort the dynamic relocation
2828 entries by increasing r_symndx value. */
2829
2830 static int
2831 sort_dynamic_relocs (const void *arg1, const void *arg2)
2832 {
2833 Elf_Internal_Rela int_reloc1;
2834 Elf_Internal_Rela int_reloc2;
2835 int diff;
2836
2837 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2838 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2839
2840 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2841 if (diff != 0)
2842 return diff;
2843
2844 if (int_reloc1.r_offset < int_reloc2.r_offset)
2845 return -1;
2846 if (int_reloc1.r_offset > int_reloc2.r_offset)
2847 return 1;
2848 return 0;
2849 }
2850
2851 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2852
2853 static int
2854 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2855 const void *arg2 ATTRIBUTE_UNUSED)
2856 {
2857 #ifdef BFD64
2858 Elf_Internal_Rela int_reloc1[3];
2859 Elf_Internal_Rela int_reloc2[3];
2860
2861 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2862 (reldyn_sorting_bfd, arg1, int_reloc1);
2863 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2864 (reldyn_sorting_bfd, arg2, int_reloc2);
2865
2866 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2867 return -1;
2868 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2869 return 1;
2870
2871 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2872 return -1;
2873 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2874 return 1;
2875 return 0;
2876 #else
2877 abort ();
2878 #endif
2879 }
2880
2881
2882 /* This routine is used to write out ECOFF debugging external symbol
2883 information. It is called via mips_elf_link_hash_traverse. The
2884 ECOFF external symbol information must match the ELF external
2885 symbol information. Unfortunately, at this point we don't know
2886 whether a symbol is required by reloc information, so the two
2887 tables may wind up being different. We must sort out the external
2888 symbol information before we can set the final size of the .mdebug
2889 section, and we must set the size of the .mdebug section before we
2890 can relocate any sections, and we can't know which symbols are
2891 required by relocation until we relocate the sections.
2892 Fortunately, it is relatively unlikely that any symbol will be
2893 stripped but required by a reloc. In particular, it can not happen
2894 when generating a final executable. */
2895
2896 static bfd_boolean
2897 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2898 {
2899 struct extsym_info *einfo = data;
2900 bfd_boolean strip;
2901 asection *sec, *output_section;
2902
2903 if (h->root.indx == -2)
2904 strip = FALSE;
2905 else if ((h->root.def_dynamic
2906 || h->root.ref_dynamic
2907 || h->root.type == bfd_link_hash_new)
2908 && !h->root.def_regular
2909 && !h->root.ref_regular)
2910 strip = TRUE;
2911 else if (einfo->info->strip == strip_all
2912 || (einfo->info->strip == strip_some
2913 && bfd_hash_lookup (einfo->info->keep_hash,
2914 h->root.root.root.string,
2915 FALSE, FALSE) == NULL))
2916 strip = TRUE;
2917 else
2918 strip = FALSE;
2919
2920 if (strip)
2921 return TRUE;
2922
2923 if (h->esym.ifd == -2)
2924 {
2925 h->esym.jmptbl = 0;
2926 h->esym.cobol_main = 0;
2927 h->esym.weakext = 0;
2928 h->esym.reserved = 0;
2929 h->esym.ifd = ifdNil;
2930 h->esym.asym.value = 0;
2931 h->esym.asym.st = stGlobal;
2932
2933 if (h->root.root.type == bfd_link_hash_undefined
2934 || h->root.root.type == bfd_link_hash_undefweak)
2935 {
2936 const char *name;
2937
2938 /* Use undefined class. Also, set class and type for some
2939 special symbols. */
2940 name = h->root.root.root.string;
2941 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2942 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2943 {
2944 h->esym.asym.sc = scData;
2945 h->esym.asym.st = stLabel;
2946 h->esym.asym.value = 0;
2947 }
2948 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2949 {
2950 h->esym.asym.sc = scAbs;
2951 h->esym.asym.st = stLabel;
2952 h->esym.asym.value =
2953 mips_elf_hash_table (einfo->info)->procedure_count;
2954 }
2955 else
2956 h->esym.asym.sc = scUndefined;
2957 }
2958 else if (h->root.root.type != bfd_link_hash_defined
2959 && h->root.root.type != bfd_link_hash_defweak)
2960 h->esym.asym.sc = scAbs;
2961 else
2962 {
2963 const char *name;
2964
2965 sec = h->root.root.u.def.section;
2966 output_section = sec->output_section;
2967
2968 /* When making a shared library and symbol h is the one from
2969 the another shared library, OUTPUT_SECTION may be null. */
2970 if (output_section == NULL)
2971 h->esym.asym.sc = scUndefined;
2972 else
2973 {
2974 name = bfd_section_name (output_section);
2975
2976 if (strcmp (name, ".text") == 0)
2977 h->esym.asym.sc = scText;
2978 else if (strcmp (name, ".data") == 0)
2979 h->esym.asym.sc = scData;
2980 else if (strcmp (name, ".sdata") == 0)
2981 h->esym.asym.sc = scSData;
2982 else if (strcmp (name, ".rodata") == 0
2983 || strcmp (name, ".rdata") == 0)
2984 h->esym.asym.sc = scRData;
2985 else if (strcmp (name, ".bss") == 0)
2986 h->esym.asym.sc = scBss;
2987 else if (strcmp (name, ".sbss") == 0)
2988 h->esym.asym.sc = scSBss;
2989 else if (strcmp (name, ".init") == 0)
2990 h->esym.asym.sc = scInit;
2991 else if (strcmp (name, ".fini") == 0)
2992 h->esym.asym.sc = scFini;
2993 else
2994 h->esym.asym.sc = scAbs;
2995 }
2996 }
2997
2998 h->esym.asym.reserved = 0;
2999 h->esym.asym.index = indexNil;
3000 }
3001
3002 if (h->root.root.type == bfd_link_hash_common)
3003 h->esym.asym.value = h->root.root.u.c.size;
3004 else if (h->root.root.type == bfd_link_hash_defined
3005 || h->root.root.type == bfd_link_hash_defweak)
3006 {
3007 if (h->esym.asym.sc == scCommon)
3008 h->esym.asym.sc = scBss;
3009 else if (h->esym.asym.sc == scSCommon)
3010 h->esym.asym.sc = scSBss;
3011
3012 sec = h->root.root.u.def.section;
3013 output_section = sec->output_section;
3014 if (output_section != NULL)
3015 h->esym.asym.value = (h->root.root.u.def.value
3016 + sec->output_offset
3017 + output_section->vma);
3018 else
3019 h->esym.asym.value = 0;
3020 }
3021 else
3022 {
3023 struct mips_elf_link_hash_entry *hd = h;
3024
3025 while (hd->root.root.type == bfd_link_hash_indirect)
3026 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
3027
3028 if (hd->needs_lazy_stub)
3029 {
3030 BFD_ASSERT (hd->root.plt.plist != NULL);
3031 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
3032 /* Set type and value for a symbol with a function stub. */
3033 h->esym.asym.st = stProc;
3034 sec = hd->root.root.u.def.section;
3035 if (sec == NULL)
3036 h->esym.asym.value = 0;
3037 else
3038 {
3039 output_section = sec->output_section;
3040 if (output_section != NULL)
3041 h->esym.asym.value = (hd->root.plt.plist->stub_offset
3042 + sec->output_offset
3043 + output_section->vma);
3044 else
3045 h->esym.asym.value = 0;
3046 }
3047 }
3048 }
3049
3050 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3051 h->root.root.root.string,
3052 &h->esym))
3053 {
3054 einfo->failed = TRUE;
3055 return FALSE;
3056 }
3057
3058 return TRUE;
3059 }
3060
3061 /* A comparison routine used to sort .gptab entries. */
3062
3063 static int
3064 gptab_compare (const void *p1, const void *p2)
3065 {
3066 const Elf32_gptab *a1 = p1;
3067 const Elf32_gptab *a2 = p2;
3068
3069 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3070 }
3071 \f
3072 /* Functions to manage the got entry hash table. */
3073
3074 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3075 hash number. */
3076
3077 static INLINE hashval_t
3078 mips_elf_hash_bfd_vma (bfd_vma addr)
3079 {
3080 #ifdef BFD64
3081 return addr + (addr >> 32);
3082 #else
3083 return addr;
3084 #endif
3085 }
3086
3087 static hashval_t
3088 mips_elf_got_entry_hash (const void *entry_)
3089 {
3090 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3091
3092 return (entry->symndx
3093 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3094 + (entry->tls_type == GOT_TLS_LDM ? 0
3095 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3096 : entry->symndx >= 0 ? (entry->abfd->id
3097 + mips_elf_hash_bfd_vma (entry->d.addend))
3098 : entry->d.h->root.root.root.hash));
3099 }
3100
3101 static int
3102 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3103 {
3104 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3105 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3106
3107 return (e1->symndx == e2->symndx
3108 && e1->tls_type == e2->tls_type
3109 && (e1->tls_type == GOT_TLS_LDM ? TRUE
3110 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3111 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3112 && e1->d.addend == e2->d.addend)
3113 : e2->abfd && e1->d.h == e2->d.h));
3114 }
3115
3116 static hashval_t
3117 mips_got_page_ref_hash (const void *ref_)
3118 {
3119 const struct mips_got_page_ref *ref;
3120
3121 ref = (const struct mips_got_page_ref *) ref_;
3122 return ((ref->symndx >= 0
3123 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3124 : ref->u.h->root.root.root.hash)
3125 + mips_elf_hash_bfd_vma (ref->addend));
3126 }
3127
3128 static int
3129 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3130 {
3131 const struct mips_got_page_ref *ref1, *ref2;
3132
3133 ref1 = (const struct mips_got_page_ref *) ref1_;
3134 ref2 = (const struct mips_got_page_ref *) ref2_;
3135 return (ref1->symndx == ref2->symndx
3136 && (ref1->symndx < 0
3137 ? ref1->u.h == ref2->u.h
3138 : ref1->u.abfd == ref2->u.abfd)
3139 && ref1->addend == ref2->addend);
3140 }
3141
3142 static hashval_t
3143 mips_got_page_entry_hash (const void *entry_)
3144 {
3145 const struct mips_got_page_entry *entry;
3146
3147 entry = (const struct mips_got_page_entry *) entry_;
3148 return entry->sec->id;
3149 }
3150
3151 static int
3152 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3153 {
3154 const struct mips_got_page_entry *entry1, *entry2;
3155
3156 entry1 = (const struct mips_got_page_entry *) entry1_;
3157 entry2 = (const struct mips_got_page_entry *) entry2_;
3158 return entry1->sec == entry2->sec;
3159 }
3160 \f
3161 /* Create and return a new mips_got_info structure. */
3162
3163 static struct mips_got_info *
3164 mips_elf_create_got_info (bfd *abfd)
3165 {
3166 struct mips_got_info *g;
3167
3168 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3169 if (g == NULL)
3170 return NULL;
3171
3172 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3173 mips_elf_got_entry_eq, NULL);
3174 if (g->got_entries == NULL)
3175 return NULL;
3176
3177 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3178 mips_got_page_ref_eq, NULL);
3179 if (g->got_page_refs == NULL)
3180 return NULL;
3181
3182 return g;
3183 }
3184
3185 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3186 CREATE_P and if ABFD doesn't already have a GOT. */
3187
3188 static struct mips_got_info *
3189 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3190 {
3191 struct mips_elf_obj_tdata *tdata;
3192
3193 if (!is_mips_elf (abfd))
3194 return NULL;
3195
3196 tdata = mips_elf_tdata (abfd);
3197 if (!tdata->got && create_p)
3198 tdata->got = mips_elf_create_got_info (abfd);
3199 return tdata->got;
3200 }
3201
3202 /* Record that ABFD should use output GOT G. */
3203
3204 static void
3205 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3206 {
3207 struct mips_elf_obj_tdata *tdata;
3208
3209 BFD_ASSERT (is_mips_elf (abfd));
3210 tdata = mips_elf_tdata (abfd);
3211 if (tdata->got)
3212 {
3213 /* The GOT structure itself and the hash table entries are
3214 allocated to a bfd, but the hash tables aren't. */
3215 htab_delete (tdata->got->got_entries);
3216 htab_delete (tdata->got->got_page_refs);
3217 if (tdata->got->got_page_entries)
3218 htab_delete (tdata->got->got_page_entries);
3219 }
3220 tdata->got = g;
3221 }
3222
3223 /* Return the dynamic relocation section. If it doesn't exist, try to
3224 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3225 if creation fails. */
3226
3227 static asection *
3228 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
3229 {
3230 const char *dname;
3231 asection *sreloc;
3232 bfd *dynobj;
3233
3234 dname = MIPS_ELF_REL_DYN_NAME (info);
3235 dynobj = elf_hash_table (info)->dynobj;
3236 sreloc = bfd_get_linker_section (dynobj, dname);
3237 if (sreloc == NULL && create_p)
3238 {
3239 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3240 (SEC_ALLOC
3241 | SEC_LOAD
3242 | SEC_HAS_CONTENTS
3243 | SEC_IN_MEMORY
3244 | SEC_LINKER_CREATED
3245 | SEC_READONLY));
3246 if (sreloc == NULL
3247 || !bfd_set_section_alignment (sreloc,
3248 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3249 return NULL;
3250 }
3251 return sreloc;
3252 }
3253
3254 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3255
3256 static int
3257 mips_elf_reloc_tls_type (unsigned int r_type)
3258 {
3259 if (tls_gd_reloc_p (r_type))
3260 return GOT_TLS_GD;
3261
3262 if (tls_ldm_reloc_p (r_type))
3263 return GOT_TLS_LDM;
3264
3265 if (tls_gottprel_reloc_p (r_type))
3266 return GOT_TLS_IE;
3267
3268 return GOT_TLS_NONE;
3269 }
3270
3271 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3272
3273 static int
3274 mips_tls_got_entries (unsigned int type)
3275 {
3276 switch (type)
3277 {
3278 case GOT_TLS_GD:
3279 case GOT_TLS_LDM:
3280 return 2;
3281
3282 case GOT_TLS_IE:
3283 return 1;
3284
3285 case GOT_TLS_NONE:
3286 return 0;
3287 }
3288 abort ();
3289 }
3290
3291 /* Count the number of relocations needed for a TLS GOT entry, with
3292 access types from TLS_TYPE, and symbol H (or a local symbol if H
3293 is NULL). */
3294
3295 static int
3296 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3297 struct elf_link_hash_entry *h)
3298 {
3299 int indx = 0;
3300 bfd_boolean need_relocs = FALSE;
3301 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3302
3303 if (h != NULL
3304 && h->dynindx != -1
3305 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3306 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
3307 indx = h->dynindx;
3308
3309 if ((bfd_link_dll (info) || indx != 0)
3310 && (h == NULL
3311 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3312 || h->root.type != bfd_link_hash_undefweak))
3313 need_relocs = TRUE;
3314
3315 if (!need_relocs)
3316 return 0;
3317
3318 switch (tls_type)
3319 {
3320 case GOT_TLS_GD:
3321 return indx != 0 ? 2 : 1;
3322
3323 case GOT_TLS_IE:
3324 return 1;
3325
3326 case GOT_TLS_LDM:
3327 return bfd_link_dll (info) ? 1 : 0;
3328
3329 default:
3330 return 0;
3331 }
3332 }
3333
3334 /* Add the number of GOT entries and TLS relocations required by ENTRY
3335 to G. */
3336
3337 static void
3338 mips_elf_count_got_entry (struct bfd_link_info *info,
3339 struct mips_got_info *g,
3340 struct mips_got_entry *entry)
3341 {
3342 if (entry->tls_type)
3343 {
3344 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3345 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3346 entry->symndx < 0
3347 ? &entry->d.h->root : NULL);
3348 }
3349 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3350 g->local_gotno += 1;
3351 else
3352 g->global_gotno += 1;
3353 }
3354
3355 /* Output a simple dynamic relocation into SRELOC. */
3356
3357 static void
3358 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3359 asection *sreloc,
3360 unsigned long reloc_index,
3361 unsigned long indx,
3362 int r_type,
3363 bfd_vma offset)
3364 {
3365 Elf_Internal_Rela rel[3];
3366
3367 memset (rel, 0, sizeof (rel));
3368
3369 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3370 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3371
3372 if (ABI_64_P (output_bfd))
3373 {
3374 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3375 (output_bfd, &rel[0],
3376 (sreloc->contents
3377 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3378 }
3379 else
3380 bfd_elf32_swap_reloc_out
3381 (output_bfd, &rel[0],
3382 (sreloc->contents
3383 + reloc_index * sizeof (Elf32_External_Rel)));
3384 }
3385
3386 /* Initialize a set of TLS GOT entries for one symbol. */
3387
3388 static void
3389 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3390 struct mips_got_entry *entry,
3391 struct mips_elf_link_hash_entry *h,
3392 bfd_vma value)
3393 {
3394 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3395 struct mips_elf_link_hash_table *htab;
3396 int indx;
3397 asection *sreloc, *sgot;
3398 bfd_vma got_offset, got_offset2;
3399 bfd_boolean need_relocs = FALSE;
3400
3401 htab = mips_elf_hash_table (info);
3402 if (htab == NULL)
3403 return;
3404
3405 sgot = htab->root.sgot;
3406
3407 indx = 0;
3408 if (h != NULL
3409 && h->root.dynindx != -1
3410 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3411 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3412 indx = h->root.dynindx;
3413
3414 if (entry->tls_initialized)
3415 return;
3416
3417 if ((bfd_link_dll (info) || indx != 0)
3418 && (h == NULL
3419 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3420 || h->root.type != bfd_link_hash_undefweak))
3421 need_relocs = TRUE;
3422
3423 /* MINUS_ONE means the symbol is not defined in this object. It may not
3424 be defined at all; assume that the value doesn't matter in that
3425 case. Otherwise complain if we would use the value. */
3426 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3427 || h->root.root.type == bfd_link_hash_undefweak);
3428
3429 /* Emit necessary relocations. */
3430 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3431 got_offset = entry->gotidx;
3432
3433 switch (entry->tls_type)
3434 {
3435 case GOT_TLS_GD:
3436 /* General Dynamic. */
3437 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3438
3439 if (need_relocs)
3440 {
3441 mips_elf_output_dynamic_relocation
3442 (abfd, sreloc, sreloc->reloc_count++, indx,
3443 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3444 sgot->output_offset + sgot->output_section->vma + got_offset);
3445
3446 if (indx)
3447 mips_elf_output_dynamic_relocation
3448 (abfd, sreloc, sreloc->reloc_count++, indx,
3449 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3450 sgot->output_offset + sgot->output_section->vma + got_offset2);
3451 else
3452 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3453 sgot->contents + got_offset2);
3454 }
3455 else
3456 {
3457 MIPS_ELF_PUT_WORD (abfd, 1,
3458 sgot->contents + got_offset);
3459 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3460 sgot->contents + got_offset2);
3461 }
3462 break;
3463
3464 case GOT_TLS_IE:
3465 /* Initial Exec model. */
3466 if (need_relocs)
3467 {
3468 if (indx == 0)
3469 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3470 sgot->contents + got_offset);
3471 else
3472 MIPS_ELF_PUT_WORD (abfd, 0,
3473 sgot->contents + got_offset);
3474
3475 mips_elf_output_dynamic_relocation
3476 (abfd, sreloc, sreloc->reloc_count++, indx,
3477 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3478 sgot->output_offset + sgot->output_section->vma + got_offset);
3479 }
3480 else
3481 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3482 sgot->contents + got_offset);
3483 break;
3484
3485 case GOT_TLS_LDM:
3486 /* The initial offset is zero, and the LD offsets will include the
3487 bias by DTP_OFFSET. */
3488 MIPS_ELF_PUT_WORD (abfd, 0,
3489 sgot->contents + got_offset
3490 + MIPS_ELF_GOT_SIZE (abfd));
3491
3492 if (!bfd_link_dll (info))
3493 MIPS_ELF_PUT_WORD (abfd, 1,
3494 sgot->contents + got_offset);
3495 else
3496 mips_elf_output_dynamic_relocation
3497 (abfd, sreloc, sreloc->reloc_count++, indx,
3498 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3499 sgot->output_offset + sgot->output_section->vma + got_offset);
3500 break;
3501
3502 default:
3503 abort ();
3504 }
3505
3506 entry->tls_initialized = TRUE;
3507 }
3508
3509 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3510 for global symbol H. .got.plt comes before the GOT, so the offset
3511 will be negative. */
3512
3513 static bfd_vma
3514 mips_elf_gotplt_index (struct bfd_link_info *info,
3515 struct elf_link_hash_entry *h)
3516 {
3517 bfd_vma got_address, got_value;
3518 struct mips_elf_link_hash_table *htab;
3519
3520 htab = mips_elf_hash_table (info);
3521 BFD_ASSERT (htab != NULL);
3522
3523 BFD_ASSERT (h->plt.plist != NULL);
3524 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3525
3526 /* Calculate the address of the associated .got.plt entry. */
3527 got_address = (htab->root.sgotplt->output_section->vma
3528 + htab->root.sgotplt->output_offset
3529 + (h->plt.plist->gotplt_index
3530 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3531
3532 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3533 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3534 + htab->root.hgot->root.u.def.section->output_offset
3535 + htab->root.hgot->root.u.def.value);
3536
3537 return got_address - got_value;
3538 }
3539
3540 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3541 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3542 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3543 offset can be found. */
3544
3545 static bfd_vma
3546 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3547 bfd_vma value, unsigned long r_symndx,
3548 struct mips_elf_link_hash_entry *h, int r_type)
3549 {
3550 struct mips_elf_link_hash_table *htab;
3551 struct mips_got_entry *entry;
3552
3553 htab = mips_elf_hash_table (info);
3554 BFD_ASSERT (htab != NULL);
3555
3556 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3557 r_symndx, h, r_type);
3558 if (!entry)
3559 return MINUS_ONE;
3560
3561 if (entry->tls_type)
3562 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3563 return entry->gotidx;
3564 }
3565
3566 /* Return the GOT index of global symbol H in the primary GOT. */
3567
3568 static bfd_vma
3569 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3570 struct elf_link_hash_entry *h)
3571 {
3572 struct mips_elf_link_hash_table *htab;
3573 long global_got_dynindx;
3574 struct mips_got_info *g;
3575 bfd_vma got_index;
3576
3577 htab = mips_elf_hash_table (info);
3578 BFD_ASSERT (htab != NULL);
3579
3580 global_got_dynindx = 0;
3581 if (htab->global_gotsym != NULL)
3582 global_got_dynindx = htab->global_gotsym->dynindx;
3583
3584 /* Once we determine the global GOT entry with the lowest dynamic
3585 symbol table index, we must put all dynamic symbols with greater
3586 indices into the primary GOT. That makes it easy to calculate the
3587 GOT offset. */
3588 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3589 g = mips_elf_bfd_got (obfd, FALSE);
3590 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3591 * MIPS_ELF_GOT_SIZE (obfd));
3592 BFD_ASSERT (got_index < htab->root.sgot->size);
3593
3594 return got_index;
3595 }
3596
3597 /* Return the GOT index for the global symbol indicated by H, which is
3598 referenced by a relocation of type R_TYPE in IBFD. */
3599
3600 static bfd_vma
3601 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3602 struct elf_link_hash_entry *h, int r_type)
3603 {
3604 struct mips_elf_link_hash_table *htab;
3605 struct mips_got_info *g;
3606 struct mips_got_entry lookup, *entry;
3607 bfd_vma gotidx;
3608
3609 htab = mips_elf_hash_table (info);
3610 BFD_ASSERT (htab != NULL);
3611
3612 g = mips_elf_bfd_got (ibfd, FALSE);
3613 BFD_ASSERT (g);
3614
3615 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3616 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3617 return mips_elf_primary_global_got_index (obfd, info, h);
3618
3619 lookup.abfd = ibfd;
3620 lookup.symndx = -1;
3621 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3622 entry = htab_find (g->got_entries, &lookup);
3623 BFD_ASSERT (entry);
3624
3625 gotidx = entry->gotidx;
3626 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3627
3628 if (lookup.tls_type)
3629 {
3630 bfd_vma value = MINUS_ONE;
3631
3632 if ((h->root.type == bfd_link_hash_defined
3633 || h->root.type == bfd_link_hash_defweak)
3634 && h->root.u.def.section->output_section)
3635 value = (h->root.u.def.value
3636 + h->root.u.def.section->output_offset
3637 + h->root.u.def.section->output_section->vma);
3638
3639 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3640 }
3641 return gotidx;
3642 }
3643
3644 /* Find a GOT page entry that points to within 32KB of VALUE. These
3645 entries are supposed to be placed at small offsets in the GOT, i.e.,
3646 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3647 entry could be created. If OFFSETP is nonnull, use it to return the
3648 offset of the GOT entry from VALUE. */
3649
3650 static bfd_vma
3651 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3652 bfd_vma value, bfd_vma *offsetp)
3653 {
3654 bfd_vma page, got_index;
3655 struct mips_got_entry *entry;
3656
3657 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3658 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3659 NULL, R_MIPS_GOT_PAGE);
3660
3661 if (!entry)
3662 return MINUS_ONE;
3663
3664 got_index = entry->gotidx;
3665
3666 if (offsetp)
3667 *offsetp = value - entry->d.address;
3668
3669 return got_index;
3670 }
3671
3672 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3673 EXTERNAL is true if the relocation was originally against a global
3674 symbol that binds locally. */
3675
3676 static bfd_vma
3677 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3678 bfd_vma value, bfd_boolean external)
3679 {
3680 struct mips_got_entry *entry;
3681
3682 /* GOT16 relocations against local symbols are followed by a LO16
3683 relocation; those against global symbols are not. Thus if the
3684 symbol was originally local, the GOT16 relocation should load the
3685 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3686 if (! external)
3687 value = mips_elf_high (value) << 16;
3688
3689 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3690 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3691 same in all cases. */
3692 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3693 NULL, R_MIPS_GOT16);
3694 if (entry)
3695 return entry->gotidx;
3696 else
3697 return MINUS_ONE;
3698 }
3699
3700 /* Returns the offset for the entry at the INDEXth position
3701 in the GOT. */
3702
3703 static bfd_vma
3704 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3705 bfd *input_bfd, bfd_vma got_index)
3706 {
3707 struct mips_elf_link_hash_table *htab;
3708 asection *sgot;
3709 bfd_vma gp;
3710
3711 htab = mips_elf_hash_table (info);
3712 BFD_ASSERT (htab != NULL);
3713
3714 sgot = htab->root.sgot;
3715 gp = _bfd_get_gp_value (output_bfd)
3716 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3717
3718 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3719 }
3720
3721 /* Create and return a local GOT entry for VALUE, which was calculated
3722 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3723 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3724 instead. */
3725
3726 static struct mips_got_entry *
3727 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3728 bfd *ibfd, bfd_vma value,
3729 unsigned long r_symndx,
3730 struct mips_elf_link_hash_entry *h,
3731 int r_type)
3732 {
3733 struct mips_got_entry lookup, *entry;
3734 void **loc;
3735 struct mips_got_info *g;
3736 struct mips_elf_link_hash_table *htab;
3737 bfd_vma gotidx;
3738
3739 htab = mips_elf_hash_table (info);
3740 BFD_ASSERT (htab != NULL);
3741
3742 g = mips_elf_bfd_got (ibfd, FALSE);
3743 if (g == NULL)
3744 {
3745 g = mips_elf_bfd_got (abfd, FALSE);
3746 BFD_ASSERT (g != NULL);
3747 }
3748
3749 /* This function shouldn't be called for symbols that live in the global
3750 area of the GOT. */
3751 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3752
3753 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3754 if (lookup.tls_type)
3755 {
3756 lookup.abfd = ibfd;
3757 if (tls_ldm_reloc_p (r_type))
3758 {
3759 lookup.symndx = 0;
3760 lookup.d.addend = 0;
3761 }
3762 else if (h == NULL)
3763 {
3764 lookup.symndx = r_symndx;
3765 lookup.d.addend = 0;
3766 }
3767 else
3768 {
3769 lookup.symndx = -1;
3770 lookup.d.h = h;
3771 }
3772
3773 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3774 BFD_ASSERT (entry);
3775
3776 gotidx = entry->gotidx;
3777 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
3778
3779 return entry;
3780 }
3781
3782 lookup.abfd = NULL;
3783 lookup.symndx = -1;
3784 lookup.d.address = value;
3785 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3786 if (!loc)
3787 return NULL;
3788
3789 entry = (struct mips_got_entry *) *loc;
3790 if (entry)
3791 return entry;
3792
3793 if (g->assigned_low_gotno > g->assigned_high_gotno)
3794 {
3795 /* We didn't allocate enough space in the GOT. */
3796 _bfd_error_handler
3797 (_("not enough GOT space for local GOT entries"));
3798 bfd_set_error (bfd_error_bad_value);
3799 return NULL;
3800 }
3801
3802 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3803 if (!entry)
3804 return NULL;
3805
3806 if (got16_reloc_p (r_type)
3807 || call16_reloc_p (r_type)
3808 || got_page_reloc_p (r_type)
3809 || got_disp_reloc_p (r_type))
3810 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3811 else
3812 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3813
3814 *entry = lookup;
3815 *loc = entry;
3816
3817 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
3818
3819 /* These GOT entries need a dynamic relocation on VxWorks. */
3820 if (htab->root.target_os == is_vxworks)
3821 {
3822 Elf_Internal_Rela outrel;
3823 asection *s;
3824 bfd_byte *rloc;
3825 bfd_vma got_address;
3826
3827 s = mips_elf_rel_dyn_section (info, FALSE);
3828 got_address = (htab->root.sgot->output_section->vma
3829 + htab->root.sgot->output_offset
3830 + entry->gotidx);
3831
3832 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3833 outrel.r_offset = got_address;
3834 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3835 outrel.r_addend = value;
3836 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3837 }
3838
3839 return entry;
3840 }
3841
3842 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3843 The number might be exact or a worst-case estimate, depending on how
3844 much information is available to elf_backend_omit_section_dynsym at
3845 the current linking stage. */
3846
3847 static bfd_size_type
3848 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3849 {
3850 bfd_size_type count;
3851
3852 count = 0;
3853 if (bfd_link_pic (info)
3854 || elf_hash_table (info)->is_relocatable_executable)
3855 {
3856 asection *p;
3857 const struct elf_backend_data *bed;
3858
3859 bed = get_elf_backend_data (output_bfd);
3860 for (p = output_bfd->sections; p ; p = p->next)
3861 if ((p->flags & SEC_EXCLUDE) == 0
3862 && (p->flags & SEC_ALLOC) != 0
3863 && elf_hash_table (info)->dynamic_relocs
3864 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3865 ++count;
3866 }
3867 return count;
3868 }
3869
3870 /* Sort the dynamic symbol table so that symbols that need GOT entries
3871 appear towards the end. */
3872
3873 static bfd_boolean
3874 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3875 {
3876 struct mips_elf_link_hash_table *htab;
3877 struct mips_elf_hash_sort_data hsd;
3878 struct mips_got_info *g;
3879
3880 htab = mips_elf_hash_table (info);
3881 BFD_ASSERT (htab != NULL);
3882
3883 if (htab->root.dynsymcount == 0)
3884 return TRUE;
3885
3886 g = htab->got_info;
3887 if (g == NULL)
3888 return TRUE;
3889
3890 hsd.low = NULL;
3891 hsd.max_unref_got_dynindx
3892 = hsd.min_got_dynindx
3893 = (htab->root.dynsymcount - g->reloc_only_gotno);
3894 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3895 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3896 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3897 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
3898 hsd.output_bfd = abfd;
3899 if (htab->root.dynobj != NULL
3900 && htab->root.dynamic_sections_created
3901 && info->emit_gnu_hash)
3902 {
3903 asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3904 BFD_ASSERT (s != NULL);
3905 hsd.mipsxhash = s->contents;
3906 BFD_ASSERT (hsd.mipsxhash != NULL);
3907 }
3908 else
3909 hsd.mipsxhash = NULL;
3910 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
3911
3912 /* There should have been enough room in the symbol table to
3913 accommodate both the GOT and non-GOT symbols. */
3914 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
3915 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3916 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
3917 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
3918
3919 /* Now we know which dynamic symbol has the lowest dynamic symbol
3920 table index in the GOT. */
3921 htab->global_gotsym = hsd.low;
3922
3923 return TRUE;
3924 }
3925
3926 /* If H needs a GOT entry, assign it the highest available dynamic
3927 index. Otherwise, assign it the lowest available dynamic
3928 index. */
3929
3930 static bfd_boolean
3931 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3932 {
3933 struct mips_elf_hash_sort_data *hsd = data;
3934
3935 /* Symbols without dynamic symbol table entries aren't interesting
3936 at all. */
3937 if (h->root.dynindx == -1)
3938 return TRUE;
3939
3940 switch (h->global_got_area)
3941 {
3942 case GGA_NONE:
3943 if (h->root.forced_local)
3944 h->root.dynindx = hsd->max_local_dynindx++;
3945 else
3946 h->root.dynindx = hsd->max_non_got_dynindx++;
3947 break;
3948
3949 case GGA_NORMAL:
3950 h->root.dynindx = --hsd->min_got_dynindx;
3951 hsd->low = (struct elf_link_hash_entry *) h;
3952 break;
3953
3954 case GGA_RELOC_ONLY:
3955 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3956 hsd->low = (struct elf_link_hash_entry *) h;
3957 h->root.dynindx = hsd->max_unref_got_dynindx++;
3958 break;
3959 }
3960
3961 /* Populate the .MIPS.xhash translation table entry with
3962 the symbol dynindx. */
3963 if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
3964 bfd_put_32 (hsd->output_bfd, h->root.dynindx,
3965 hsd->mipsxhash + h->mipsxhash_loc);
3966
3967 return TRUE;
3968 }
3969
3970 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3971 (which is owned by the caller and shouldn't be added to the
3972 hash table directly). */
3973
3974 static bfd_boolean
3975 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3976 struct mips_got_entry *lookup)
3977 {
3978 struct mips_elf_link_hash_table *htab;
3979 struct mips_got_entry *entry;
3980 struct mips_got_info *g;
3981 void **loc, **bfd_loc;
3982
3983 /* Make sure there's a slot for this entry in the master GOT. */
3984 htab = mips_elf_hash_table (info);
3985 g = htab->got_info;
3986 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3987 if (!loc)
3988 return FALSE;
3989
3990 /* Populate the entry if it isn't already. */
3991 entry = (struct mips_got_entry *) *loc;
3992 if (!entry)
3993 {
3994 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3995 if (!entry)
3996 return FALSE;
3997
3998 lookup->tls_initialized = FALSE;
3999 lookup->gotidx = -1;
4000 *entry = *lookup;
4001 *loc = entry;
4002 }
4003
4004 /* Reuse the same GOT entry for the BFD's GOT. */
4005 g = mips_elf_bfd_got (abfd, TRUE);
4006 if (!g)
4007 return FALSE;
4008
4009 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4010 if (!bfd_loc)
4011 return FALSE;
4012
4013 if (!*bfd_loc)
4014 *bfd_loc = entry;
4015 return TRUE;
4016 }
4017
4018 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
4019 entry for it. FOR_CALL is true if the caller is only interested in
4020 using the GOT entry for calls. */
4021
4022 static bfd_boolean
4023 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4024 bfd *abfd, struct bfd_link_info *info,
4025 bfd_boolean for_call, int r_type)
4026 {
4027 struct mips_elf_link_hash_table *htab;
4028 struct mips_elf_link_hash_entry *hmips;
4029 struct mips_got_entry entry;
4030 unsigned char tls_type;
4031
4032 htab = mips_elf_hash_table (info);
4033 BFD_ASSERT (htab != NULL);
4034
4035 hmips = (struct mips_elf_link_hash_entry *) h;
4036 if (!for_call)
4037 hmips->got_only_for_calls = FALSE;
4038
4039 /* A global symbol in the GOT must also be in the dynamic symbol
4040 table. */
4041 if (h->dynindx == -1)
4042 {
4043 switch (ELF_ST_VISIBILITY (h->other))
4044 {
4045 case STV_INTERNAL:
4046 case STV_HIDDEN:
4047 _bfd_mips_elf_hide_symbol (info, h, TRUE);
4048 break;
4049 }
4050 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4051 return FALSE;
4052 }
4053
4054 tls_type = mips_elf_reloc_tls_type (r_type);
4055 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
4056 hmips->global_got_area = GGA_NORMAL;
4057
4058 entry.abfd = abfd;
4059 entry.symndx = -1;
4060 entry.d.h = (struct mips_elf_link_hash_entry *) h;
4061 entry.tls_type = tls_type;
4062 return mips_elf_record_got_entry (info, abfd, &entry);
4063 }
4064
4065 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4066 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
4067
4068 static bfd_boolean
4069 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
4070 struct bfd_link_info *info, int r_type)
4071 {
4072 struct mips_elf_link_hash_table *htab;
4073 struct mips_got_info *g;
4074 struct mips_got_entry entry;
4075
4076 htab = mips_elf_hash_table (info);
4077 BFD_ASSERT (htab != NULL);
4078
4079 g = htab->got_info;
4080 BFD_ASSERT (g != NULL);
4081
4082 entry.abfd = abfd;
4083 entry.symndx = symndx;
4084 entry.d.addend = addend;
4085 entry.tls_type = mips_elf_reloc_tls_type (r_type);
4086 return mips_elf_record_got_entry (info, abfd, &entry);
4087 }
4088
4089 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4090 H is the symbol's hash table entry, or null if SYMNDX is local
4091 to ABFD. */
4092
4093 static bfd_boolean
4094 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4095 long symndx, struct elf_link_hash_entry *h,
4096 bfd_signed_vma addend)
4097 {
4098 struct mips_elf_link_hash_table *htab;
4099 struct mips_got_info *g1, *g2;
4100 struct mips_got_page_ref lookup, *entry;
4101 void **loc, **bfd_loc;
4102
4103 htab = mips_elf_hash_table (info);
4104 BFD_ASSERT (htab != NULL);
4105
4106 g1 = htab->got_info;
4107 BFD_ASSERT (g1 != NULL);
4108
4109 if (h)
4110 {
4111 lookup.symndx = -1;
4112 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4113 }
4114 else
4115 {
4116 lookup.symndx = symndx;
4117 lookup.u.abfd = abfd;
4118 }
4119 lookup.addend = addend;
4120 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4121 if (loc == NULL)
4122 return FALSE;
4123
4124 entry = (struct mips_got_page_ref *) *loc;
4125 if (!entry)
4126 {
4127 entry = bfd_alloc (abfd, sizeof (*entry));
4128 if (!entry)
4129 return FALSE;
4130
4131 *entry = lookup;
4132 *loc = entry;
4133 }
4134
4135 /* Add the same entry to the BFD's GOT. */
4136 g2 = mips_elf_bfd_got (abfd, TRUE);
4137 if (!g2)
4138 return FALSE;
4139
4140 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4141 if (!bfd_loc)
4142 return FALSE;
4143
4144 if (!*bfd_loc)
4145 *bfd_loc = entry;
4146
4147 return TRUE;
4148 }
4149
4150 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4151
4152 static void
4153 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4154 unsigned int n)
4155 {
4156 asection *s;
4157 struct mips_elf_link_hash_table *htab;
4158
4159 htab = mips_elf_hash_table (info);
4160 BFD_ASSERT (htab != NULL);
4161
4162 s = mips_elf_rel_dyn_section (info, FALSE);
4163 BFD_ASSERT (s != NULL);
4164
4165 if (htab->root.target_os == is_vxworks)
4166 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4167 else
4168 {
4169 if (s->size == 0)
4170 {
4171 /* Make room for a null element. */
4172 s->size += MIPS_ELF_REL_SIZE (abfd);
4173 ++s->reloc_count;
4174 }
4175 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4176 }
4177 }
4178 \f
4179 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4180 mips_elf_traverse_got_arg structure. Count the number of GOT
4181 entries and TLS relocs. Set DATA->value to true if we need
4182 to resolve indirect or warning symbols and then recreate the GOT. */
4183
4184 static int
4185 mips_elf_check_recreate_got (void **entryp, void *data)
4186 {
4187 struct mips_got_entry *entry;
4188 struct mips_elf_traverse_got_arg *arg;
4189
4190 entry = (struct mips_got_entry *) *entryp;
4191 arg = (struct mips_elf_traverse_got_arg *) data;
4192 if (entry->abfd != NULL && entry->symndx == -1)
4193 {
4194 struct mips_elf_link_hash_entry *h;
4195
4196 h = entry->d.h;
4197 if (h->root.root.type == bfd_link_hash_indirect
4198 || h->root.root.type == bfd_link_hash_warning)
4199 {
4200 arg->value = TRUE;
4201 return 0;
4202 }
4203 }
4204 mips_elf_count_got_entry (arg->info, arg->g, entry);
4205 return 1;
4206 }
4207
4208 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4209 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4210 converting entries for indirect and warning symbols into entries
4211 for the target symbol. Set DATA->g to null on error. */
4212
4213 static int
4214 mips_elf_recreate_got (void **entryp, void *data)
4215 {
4216 struct mips_got_entry new_entry, *entry;
4217 struct mips_elf_traverse_got_arg *arg;
4218 void **slot;
4219
4220 entry = (struct mips_got_entry *) *entryp;
4221 arg = (struct mips_elf_traverse_got_arg *) data;
4222 if (entry->abfd != NULL
4223 && entry->symndx == -1
4224 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4225 || entry->d.h->root.root.type == bfd_link_hash_warning))
4226 {
4227 struct mips_elf_link_hash_entry *h;
4228
4229 new_entry = *entry;
4230 entry = &new_entry;
4231 h = entry->d.h;
4232 do
4233 {
4234 BFD_ASSERT (h->global_got_area == GGA_NONE);
4235 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4236 }
4237 while (h->root.root.type == bfd_link_hash_indirect
4238 || h->root.root.type == bfd_link_hash_warning);
4239 entry->d.h = h;
4240 }
4241 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4242 if (slot == NULL)
4243 {
4244 arg->g = NULL;
4245 return 0;
4246 }
4247 if (*slot == NULL)
4248 {
4249 if (entry == &new_entry)
4250 {
4251 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4252 if (!entry)
4253 {
4254 arg->g = NULL;
4255 return 0;
4256 }
4257 *entry = new_entry;
4258 }
4259 *slot = entry;
4260 mips_elf_count_got_entry (arg->info, arg->g, entry);
4261 }
4262 return 1;
4263 }
4264
4265 /* Return the maximum number of GOT page entries required for RANGE. */
4266
4267 static bfd_vma
4268 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4269 {
4270 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4271 }
4272
4273 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4274
4275 static bfd_boolean
4276 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4277 asection *sec, bfd_signed_vma addend)
4278 {
4279 struct mips_got_info *g = arg->g;
4280 struct mips_got_page_entry lookup, *entry;
4281 struct mips_got_page_range **range_ptr, *range;
4282 bfd_vma old_pages, new_pages;
4283 void **loc;
4284
4285 /* Find the mips_got_page_entry hash table entry for this section. */
4286 lookup.sec = sec;
4287 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4288 if (loc == NULL)
4289 return FALSE;
4290
4291 /* Create a mips_got_page_entry if this is the first time we've
4292 seen the section. */
4293 entry = (struct mips_got_page_entry *) *loc;
4294 if (!entry)
4295 {
4296 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4297 if (!entry)
4298 return FALSE;
4299
4300 entry->sec = sec;
4301 *loc = entry;
4302 }
4303
4304 /* Skip over ranges whose maximum extent cannot share a page entry
4305 with ADDEND. */
4306 range_ptr = &entry->ranges;
4307 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4308 range_ptr = &(*range_ptr)->next;
4309
4310 /* If we scanned to the end of the list, or found a range whose
4311 minimum extent cannot share a page entry with ADDEND, create
4312 a new singleton range. */
4313 range = *range_ptr;
4314 if (!range || addend < range->min_addend - 0xffff)
4315 {
4316 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4317 if (!range)
4318 return FALSE;
4319
4320 range->next = *range_ptr;
4321 range->min_addend = addend;
4322 range->max_addend = addend;
4323
4324 *range_ptr = range;
4325 entry->num_pages++;
4326 g->page_gotno++;
4327 return TRUE;
4328 }
4329
4330 /* Remember how many pages the old range contributed. */
4331 old_pages = mips_elf_pages_for_range (range);
4332
4333 /* Update the ranges. */
4334 if (addend < range->min_addend)
4335 range->min_addend = addend;
4336 else if (addend > range->max_addend)
4337 {
4338 if (range->next && addend >= range->next->min_addend - 0xffff)
4339 {
4340 old_pages += mips_elf_pages_for_range (range->next);
4341 range->max_addend = range->next->max_addend;
4342 range->next = range->next->next;
4343 }
4344 else
4345 range->max_addend = addend;
4346 }
4347
4348 /* Record any change in the total estimate. */
4349 new_pages = mips_elf_pages_for_range (range);
4350 if (old_pages != new_pages)
4351 {
4352 entry->num_pages += new_pages - old_pages;
4353 g->page_gotno += new_pages - old_pages;
4354 }
4355
4356 return TRUE;
4357 }
4358
4359 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4360 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4361 whether the page reference described by *REFP needs a GOT page entry,
4362 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4363
4364 static bfd_boolean
4365 mips_elf_resolve_got_page_ref (void **refp, void *data)
4366 {
4367 struct mips_got_page_ref *ref;
4368 struct mips_elf_traverse_got_arg *arg;
4369 struct mips_elf_link_hash_table *htab;
4370 asection *sec;
4371 bfd_vma addend;
4372
4373 ref = (struct mips_got_page_ref *) *refp;
4374 arg = (struct mips_elf_traverse_got_arg *) data;
4375 htab = mips_elf_hash_table (arg->info);
4376
4377 if (ref->symndx < 0)
4378 {
4379 struct mips_elf_link_hash_entry *h;
4380
4381 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4382 h = ref->u.h;
4383 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4384 return 1;
4385
4386 /* Ignore undefined symbols; we'll issue an error later if
4387 appropriate. */
4388 if (!((h->root.root.type == bfd_link_hash_defined
4389 || h->root.root.type == bfd_link_hash_defweak)
4390 && h->root.root.u.def.section))
4391 return 1;
4392
4393 sec = h->root.root.u.def.section;
4394 addend = h->root.root.u.def.value + ref->addend;
4395 }
4396 else
4397 {
4398 Elf_Internal_Sym *isym;
4399
4400 /* Read in the symbol. */
4401 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
4402 ref->symndx);
4403 if (isym == NULL)
4404 {
4405 arg->g = NULL;
4406 return 0;
4407 }
4408
4409 /* Get the associated input section. */
4410 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4411 if (sec == NULL)
4412 {
4413 arg->g = NULL;
4414 return 0;
4415 }
4416
4417 /* If this is a mergable section, work out the section and offset
4418 of the merged data. For section symbols, the addend specifies
4419 of the offset _of_ the first byte in the data, otherwise it
4420 specifies the offset _from_ the first byte. */
4421 if (sec->flags & SEC_MERGE)
4422 {
4423 void *secinfo;
4424
4425 secinfo = elf_section_data (sec)->sec_info;
4426 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4427 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4428 isym->st_value + ref->addend);
4429 else
4430 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4431 isym->st_value) + ref->addend;
4432 }
4433 else
4434 addend = isym->st_value + ref->addend;
4435 }
4436 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4437 {
4438 arg->g = NULL;
4439 return 0;
4440 }
4441 return 1;
4442 }
4443
4444 /* If any entries in G->got_entries are for indirect or warning symbols,
4445 replace them with entries for the target symbol. Convert g->got_page_refs
4446 into got_page_entry structures and estimate the number of page entries
4447 that they require. */
4448
4449 static bfd_boolean
4450 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4451 struct mips_got_info *g)
4452 {
4453 struct mips_elf_traverse_got_arg tga;
4454 struct mips_got_info oldg;
4455
4456 oldg = *g;
4457
4458 tga.info = info;
4459 tga.g = g;
4460 tga.value = FALSE;
4461 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4462 if (tga.value)
4463 {
4464 *g = oldg;
4465 g->got_entries = htab_create (htab_size (oldg.got_entries),
4466 mips_elf_got_entry_hash,
4467 mips_elf_got_entry_eq, NULL);
4468 if (!g->got_entries)
4469 return FALSE;
4470
4471 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4472 if (!tga.g)
4473 return FALSE;
4474
4475 htab_delete (oldg.got_entries);
4476 }
4477
4478 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4479 mips_got_page_entry_eq, NULL);
4480 if (g->got_page_entries == NULL)
4481 return FALSE;
4482
4483 tga.info = info;
4484 tga.g = g;
4485 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4486
4487 return TRUE;
4488 }
4489
4490 /* Return true if a GOT entry for H should live in the local rather than
4491 global GOT area. */
4492
4493 static bfd_boolean
4494 mips_use_local_got_p (struct bfd_link_info *info,
4495 struct mips_elf_link_hash_entry *h)
4496 {
4497 /* Symbols that aren't in the dynamic symbol table must live in the
4498 local GOT. This includes symbols that are completely undefined
4499 and which therefore don't bind locally. We'll report undefined
4500 symbols later if appropriate. */
4501 if (h->root.dynindx == -1)
4502 return TRUE;
4503
4504 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4505 to the local GOT, as they would be implicitly relocated by the
4506 base address by the dynamic loader. */
4507 if (bfd_is_abs_symbol (&h->root.root))
4508 return FALSE;
4509
4510 /* Symbols that bind locally can (and in the case of forced-local
4511 symbols, must) live in the local GOT. */
4512 if (h->got_only_for_calls
4513 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4514 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4515 return TRUE;
4516
4517 /* If this is an executable that must provide a definition of the symbol,
4518 either though PLTs or copy relocations, then that address should go in
4519 the local rather than global GOT. */
4520 if (bfd_link_executable (info) && h->has_static_relocs)
4521 return TRUE;
4522
4523 return FALSE;
4524 }
4525
4526 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4527 link_info structure. Decide whether the hash entry needs an entry in
4528 the global part of the primary GOT, setting global_got_area accordingly.
4529 Count the number of global symbols that are in the primary GOT only
4530 because they have relocations against them (reloc_only_gotno). */
4531
4532 static int
4533 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4534 {
4535 struct bfd_link_info *info;
4536 struct mips_elf_link_hash_table *htab;
4537 struct mips_got_info *g;
4538
4539 info = (struct bfd_link_info *) data;
4540 htab = mips_elf_hash_table (info);
4541 g = htab->got_info;
4542 if (h->global_got_area != GGA_NONE)
4543 {
4544 /* Make a final decision about whether the symbol belongs in the
4545 local or global GOT. */
4546 if (mips_use_local_got_p (info, h))
4547 /* The symbol belongs in the local GOT. We no longer need this
4548 entry if it was only used for relocations; those relocations
4549 will be against the null or section symbol instead of H. */
4550 h->global_got_area = GGA_NONE;
4551 else if (htab->root.target_os == is_vxworks
4552 && h->got_only_for_calls
4553 && h->root.plt.plist->mips_offset != MINUS_ONE)
4554 /* On VxWorks, calls can refer directly to the .got.plt entry;
4555 they don't need entries in the regular GOT. .got.plt entries
4556 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4557 h->global_got_area = GGA_NONE;
4558 else if (h->global_got_area == GGA_RELOC_ONLY)
4559 {
4560 g->reloc_only_gotno++;
4561 g->global_gotno++;
4562 }
4563 }
4564 return 1;
4565 }
4566 \f
4567 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4568 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4569
4570 static int
4571 mips_elf_add_got_entry (void **entryp, void *data)
4572 {
4573 struct mips_got_entry *entry;
4574 struct mips_elf_traverse_got_arg *arg;
4575 void **slot;
4576
4577 entry = (struct mips_got_entry *) *entryp;
4578 arg = (struct mips_elf_traverse_got_arg *) data;
4579 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4580 if (!slot)
4581 {
4582 arg->g = NULL;
4583 return 0;
4584 }
4585 if (!*slot)
4586 {
4587 *slot = entry;
4588 mips_elf_count_got_entry (arg->info, arg->g, entry);
4589 }
4590 return 1;
4591 }
4592
4593 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4594 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4595
4596 static int
4597 mips_elf_add_got_page_entry (void **entryp, void *data)
4598 {
4599 struct mips_got_page_entry *entry;
4600 struct mips_elf_traverse_got_arg *arg;
4601 void **slot;
4602
4603 entry = (struct mips_got_page_entry *) *entryp;
4604 arg = (struct mips_elf_traverse_got_arg *) data;
4605 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4606 if (!slot)
4607 {
4608 arg->g = NULL;
4609 return 0;
4610 }
4611 if (!*slot)
4612 {
4613 *slot = entry;
4614 arg->g->page_gotno += entry->num_pages;
4615 }
4616 return 1;
4617 }
4618
4619 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4620 this would lead to overflow, 1 if they were merged successfully,
4621 and 0 if a merge failed due to lack of memory. (These values are chosen
4622 so that nonnegative return values can be returned by a htab_traverse
4623 callback.) */
4624
4625 static int
4626 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4627 struct mips_got_info *to,
4628 struct mips_elf_got_per_bfd_arg *arg)
4629 {
4630 struct mips_elf_traverse_got_arg tga;
4631 unsigned int estimate;
4632
4633 /* Work out how many page entries we would need for the combined GOT. */
4634 estimate = arg->max_pages;
4635 if (estimate >= from->page_gotno + to->page_gotno)
4636 estimate = from->page_gotno + to->page_gotno;
4637
4638 /* And conservatively estimate how many local and TLS entries
4639 would be needed. */
4640 estimate += from->local_gotno + to->local_gotno;
4641 estimate += from->tls_gotno + to->tls_gotno;
4642
4643 /* If we're merging with the primary got, any TLS relocations will
4644 come after the full set of global entries. Otherwise estimate those
4645 conservatively as well. */
4646 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4647 estimate += arg->global_count;
4648 else
4649 estimate += from->global_gotno + to->global_gotno;
4650
4651 /* Bail out if the combined GOT might be too big. */
4652 if (estimate > arg->max_count)
4653 return -1;
4654
4655 /* Transfer the bfd's got information from FROM to TO. */
4656 tga.info = arg->info;
4657 tga.g = to;
4658 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4659 if (!tga.g)
4660 return 0;
4661
4662 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4663 if (!tga.g)
4664 return 0;
4665
4666 mips_elf_replace_bfd_got (abfd, to);
4667 return 1;
4668 }
4669
4670 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4671 as possible of the primary got, since it doesn't require explicit
4672 dynamic relocations, but don't use bfds that would reference global
4673 symbols out of the addressable range. Failing the primary got,
4674 attempt to merge with the current got, or finish the current got
4675 and then make make the new got current. */
4676
4677 static bfd_boolean
4678 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4679 struct mips_elf_got_per_bfd_arg *arg)
4680 {
4681 unsigned int estimate;
4682 int result;
4683
4684 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4685 return FALSE;
4686
4687 /* Work out the number of page, local and TLS entries. */
4688 estimate = arg->max_pages;
4689 if (estimate > g->page_gotno)
4690 estimate = g->page_gotno;
4691 estimate += g->local_gotno + g->tls_gotno;
4692
4693 /* We place TLS GOT entries after both locals and globals. The globals
4694 for the primary GOT may overflow the normal GOT size limit, so be
4695 sure not to merge a GOT which requires TLS with the primary GOT in that
4696 case. This doesn't affect non-primary GOTs. */
4697 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4698
4699 if (estimate <= arg->max_count)
4700 {
4701 /* If we don't have a primary GOT, use it as
4702 a starting point for the primary GOT. */
4703 if (!arg->primary)
4704 {
4705 arg->primary = g;
4706 return TRUE;
4707 }
4708
4709 /* Try merging with the primary GOT. */
4710 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4711 if (result >= 0)
4712 return result;
4713 }
4714
4715 /* If we can merge with the last-created got, do it. */
4716 if (arg->current)
4717 {
4718 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4719 if (result >= 0)
4720 return result;
4721 }
4722
4723 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4724 fits; if it turns out that it doesn't, we'll get relocation
4725 overflows anyway. */
4726 g->next = arg->current;
4727 arg->current = g;
4728
4729 return TRUE;
4730 }
4731
4732 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4733 to GOTIDX, duplicating the entry if it has already been assigned
4734 an index in a different GOT. */
4735
4736 static bfd_boolean
4737 mips_elf_set_gotidx (void **entryp, long gotidx)
4738 {
4739 struct mips_got_entry *entry;
4740
4741 entry = (struct mips_got_entry *) *entryp;
4742 if (entry->gotidx > 0)
4743 {
4744 struct mips_got_entry *new_entry;
4745
4746 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4747 if (!new_entry)
4748 return FALSE;
4749
4750 *new_entry = *entry;
4751 *entryp = new_entry;
4752 entry = new_entry;
4753 }
4754 entry->gotidx = gotidx;
4755 return TRUE;
4756 }
4757
4758 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4759 mips_elf_traverse_got_arg in which DATA->value is the size of one
4760 GOT entry. Set DATA->g to null on failure. */
4761
4762 static int
4763 mips_elf_initialize_tls_index (void **entryp, void *data)
4764 {
4765 struct mips_got_entry *entry;
4766 struct mips_elf_traverse_got_arg *arg;
4767
4768 /* We're only interested in TLS symbols. */
4769 entry = (struct mips_got_entry *) *entryp;
4770 if (entry->tls_type == GOT_TLS_NONE)
4771 return 1;
4772
4773 arg = (struct mips_elf_traverse_got_arg *) data;
4774 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4775 {
4776 arg->g = NULL;
4777 return 0;
4778 }
4779
4780 /* Account for the entries we've just allocated. */
4781 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4782 return 1;
4783 }
4784
4785 /* A htab_traverse callback for GOT entries, where DATA points to a
4786 mips_elf_traverse_got_arg. Set the global_got_area of each global
4787 symbol to DATA->value. */
4788
4789 static int
4790 mips_elf_set_global_got_area (void **entryp, void *data)
4791 {
4792 struct mips_got_entry *entry;
4793 struct mips_elf_traverse_got_arg *arg;
4794
4795 entry = (struct mips_got_entry *) *entryp;
4796 arg = (struct mips_elf_traverse_got_arg *) data;
4797 if (entry->abfd != NULL
4798 && entry->symndx == -1
4799 && entry->d.h->global_got_area != GGA_NONE)
4800 entry->d.h->global_got_area = arg->value;
4801 return 1;
4802 }
4803
4804 /* A htab_traverse callback for secondary GOT entries, where DATA points
4805 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4806 and record the number of relocations they require. DATA->value is
4807 the size of one GOT entry. Set DATA->g to null on failure. */
4808
4809 static int
4810 mips_elf_set_global_gotidx (void **entryp, void *data)
4811 {
4812 struct mips_got_entry *entry;
4813 struct mips_elf_traverse_got_arg *arg;
4814
4815 entry = (struct mips_got_entry *) *entryp;
4816 arg = (struct mips_elf_traverse_got_arg *) data;
4817 if (entry->abfd != NULL
4818 && entry->symndx == -1
4819 && entry->d.h->global_got_area != GGA_NONE)
4820 {
4821 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4822 {
4823 arg->g = NULL;
4824 return 0;
4825 }
4826 arg->g->assigned_low_gotno += 1;
4827
4828 if (bfd_link_pic (arg->info)
4829 || (elf_hash_table (arg->info)->dynamic_sections_created
4830 && entry->d.h->root.def_dynamic
4831 && !entry->d.h->root.def_regular))
4832 arg->g->relocs += 1;
4833 }
4834
4835 return 1;
4836 }
4837
4838 /* A htab_traverse callback for GOT entries for which DATA is the
4839 bfd_link_info. Forbid any global symbols from having traditional
4840 lazy-binding stubs. */
4841
4842 static int
4843 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4844 {
4845 struct bfd_link_info *info;
4846 struct mips_elf_link_hash_table *htab;
4847 struct mips_got_entry *entry;
4848
4849 entry = (struct mips_got_entry *) *entryp;
4850 info = (struct bfd_link_info *) data;
4851 htab = mips_elf_hash_table (info);
4852 BFD_ASSERT (htab != NULL);
4853
4854 if (entry->abfd != NULL
4855 && entry->symndx == -1
4856 && entry->d.h->needs_lazy_stub)
4857 {
4858 entry->d.h->needs_lazy_stub = FALSE;
4859 htab->lazy_stub_count--;
4860 }
4861
4862 return 1;
4863 }
4864
4865 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4866 the primary GOT. */
4867 static bfd_vma
4868 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4869 {
4870 if (!g->next)
4871 return 0;
4872
4873 g = mips_elf_bfd_got (ibfd, FALSE);
4874 if (! g)
4875 return 0;
4876
4877 BFD_ASSERT (g->next);
4878
4879 g = g->next;
4880
4881 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4882 * MIPS_ELF_GOT_SIZE (abfd);
4883 }
4884
4885 /* Turn a single GOT that is too big for 16-bit addressing into
4886 a sequence of GOTs, each one 16-bit addressable. */
4887
4888 static bfd_boolean
4889 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4890 asection *got, bfd_size_type pages)
4891 {
4892 struct mips_elf_link_hash_table *htab;
4893 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4894 struct mips_elf_traverse_got_arg tga;
4895 struct mips_got_info *g, *gg;
4896 unsigned int assign, needed_relocs;
4897 bfd *dynobj, *ibfd;
4898
4899 dynobj = elf_hash_table (info)->dynobj;
4900 htab = mips_elf_hash_table (info);
4901 BFD_ASSERT (htab != NULL);
4902
4903 g = htab->got_info;
4904
4905 got_per_bfd_arg.obfd = abfd;
4906 got_per_bfd_arg.info = info;
4907 got_per_bfd_arg.current = NULL;
4908 got_per_bfd_arg.primary = NULL;
4909 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4910 / MIPS_ELF_GOT_SIZE (abfd))
4911 - htab->reserved_gotno);
4912 got_per_bfd_arg.max_pages = pages;
4913 /* The number of globals that will be included in the primary GOT.
4914 See the calls to mips_elf_set_global_got_area below for more
4915 information. */
4916 got_per_bfd_arg.global_count = g->global_gotno;
4917
4918 /* Try to merge the GOTs of input bfds together, as long as they
4919 don't seem to exceed the maximum GOT size, choosing one of them
4920 to be the primary GOT. */
4921 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4922 {
4923 gg = mips_elf_bfd_got (ibfd, FALSE);
4924 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4925 return FALSE;
4926 }
4927
4928 /* If we do not find any suitable primary GOT, create an empty one. */
4929 if (got_per_bfd_arg.primary == NULL)
4930 g->next = mips_elf_create_got_info (abfd);
4931 else
4932 g->next = got_per_bfd_arg.primary;
4933 g->next->next = got_per_bfd_arg.current;
4934
4935 /* GG is now the master GOT, and G is the primary GOT. */
4936 gg = g;
4937 g = g->next;
4938
4939 /* Map the output bfd to the primary got. That's what we're going
4940 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4941 didn't mark in check_relocs, and we want a quick way to find it.
4942 We can't just use gg->next because we're going to reverse the
4943 list. */
4944 mips_elf_replace_bfd_got (abfd, g);
4945
4946 /* Every symbol that is referenced in a dynamic relocation must be
4947 present in the primary GOT, so arrange for them to appear after
4948 those that are actually referenced. */
4949 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4950 g->global_gotno = gg->global_gotno;
4951
4952 tga.info = info;
4953 tga.value = GGA_RELOC_ONLY;
4954 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4955 tga.value = GGA_NORMAL;
4956 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
4957
4958 /* Now go through the GOTs assigning them offset ranges.
4959 [assigned_low_gotno, local_gotno[ will be set to the range of local
4960 entries in each GOT. We can then compute the end of a GOT by
4961 adding local_gotno to global_gotno. We reverse the list and make
4962 it circular since then we'll be able to quickly compute the
4963 beginning of a GOT, by computing the end of its predecessor. To
4964 avoid special cases for the primary GOT, while still preserving
4965 assertions that are valid for both single- and multi-got links,
4966 we arrange for the main got struct to have the right number of
4967 global entries, but set its local_gotno such that the initial
4968 offset of the primary GOT is zero. Remember that the primary GOT
4969 will become the last item in the circular linked list, so it
4970 points back to the master GOT. */
4971 gg->local_gotno = -g->global_gotno;
4972 gg->global_gotno = g->global_gotno;
4973 gg->tls_gotno = 0;
4974 assign = 0;
4975 gg->next = gg;
4976
4977 do
4978 {
4979 struct mips_got_info *gn;
4980
4981 assign += htab->reserved_gotno;
4982 g->assigned_low_gotno = assign;
4983 g->local_gotno += assign;
4984 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4985 g->assigned_high_gotno = g->local_gotno - 1;
4986 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4987
4988 /* Take g out of the direct list, and push it onto the reversed
4989 list that gg points to. g->next is guaranteed to be nonnull after
4990 this operation, as required by mips_elf_initialize_tls_index. */
4991 gn = g->next;
4992 g->next = gg->next;
4993 gg->next = g;
4994
4995 /* Set up any TLS entries. We always place the TLS entries after
4996 all non-TLS entries. */
4997 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4998 tga.g = g;
4999 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5000 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5001 if (!tga.g)
5002 return FALSE;
5003 BFD_ASSERT (g->tls_assigned_gotno == assign);
5004
5005 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
5006 g = gn;
5007
5008 /* Forbid global symbols in every non-primary GOT from having
5009 lazy-binding stubs. */
5010 if (g)
5011 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
5012 }
5013 while (g);
5014
5015 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
5016
5017 needed_relocs = 0;
5018 for (g = gg->next; g && g->next != gg; g = g->next)
5019 {
5020 unsigned int save_assign;
5021
5022 /* Assign offsets to global GOT entries and count how many
5023 relocations they need. */
5024 save_assign = g->assigned_low_gotno;
5025 g->assigned_low_gotno = g->local_gotno;
5026 tga.info = info;
5027 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5028 tga.g = g;
5029 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
5030 if (!tga.g)
5031 return FALSE;
5032 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5033 g->assigned_low_gotno = save_assign;
5034
5035 if (bfd_link_pic (info))
5036 {
5037 g->relocs += g->local_gotno - g->assigned_low_gotno;
5038 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
5039 + g->next->global_gotno
5040 + g->next->tls_gotno
5041 + htab->reserved_gotno);
5042 }
5043 needed_relocs += g->relocs;
5044 }
5045 needed_relocs += g->relocs;
5046
5047 if (needed_relocs)
5048 mips_elf_allocate_dynamic_relocations (dynobj, info,
5049 needed_relocs);
5050
5051 return TRUE;
5052 }
5053
5054 \f
5055 /* Returns the first relocation of type r_type found, beginning with
5056 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5057
5058 static const Elf_Internal_Rela *
5059 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5060 const Elf_Internal_Rela *relocation,
5061 const Elf_Internal_Rela *relend)
5062 {
5063 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5064
5065 while (relocation < relend)
5066 {
5067 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5068 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
5069 return relocation;
5070
5071 ++relocation;
5072 }
5073
5074 /* We didn't find it. */
5075 return NULL;
5076 }
5077
5078 /* Return whether an input relocation is against a local symbol. */
5079
5080 static bfd_boolean
5081 mips_elf_local_relocation_p (bfd *input_bfd,
5082 const Elf_Internal_Rela *relocation,
5083 asection **local_sections)
5084 {
5085 unsigned long r_symndx;
5086 Elf_Internal_Shdr *symtab_hdr;
5087 size_t extsymoff;
5088
5089 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5090 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5091 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5092
5093 if (r_symndx < extsymoff)
5094 return TRUE;
5095 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5096 return TRUE;
5097
5098 return FALSE;
5099 }
5100 \f
5101 /* Sign-extend VALUE, which has the indicated number of BITS. */
5102
5103 bfd_vma
5104 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5105 {
5106 if (value & ((bfd_vma) 1 << (bits - 1)))
5107 /* VALUE is negative. */
5108 value |= ((bfd_vma) - 1) << bits;
5109
5110 return value;
5111 }
5112
5113 /* Return non-zero if the indicated VALUE has overflowed the maximum
5114 range expressible by a signed number with the indicated number of
5115 BITS. */
5116
5117 static bfd_boolean
5118 mips_elf_overflow_p (bfd_vma value, int bits)
5119 {
5120 bfd_signed_vma svalue = (bfd_signed_vma) value;
5121
5122 if (svalue > (1 << (bits - 1)) - 1)
5123 /* The value is too big. */
5124 return TRUE;
5125 else if (svalue < -(1 << (bits - 1)))
5126 /* The value is too small. */
5127 return TRUE;
5128
5129 /* All is well. */
5130 return FALSE;
5131 }
5132
5133 /* Calculate the %high function. */
5134
5135 static bfd_vma
5136 mips_elf_high (bfd_vma value)
5137 {
5138 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5139 }
5140
5141 /* Calculate the %higher function. */
5142
5143 static bfd_vma
5144 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5145 {
5146 #ifdef BFD64
5147 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5148 #else
5149 abort ();
5150 return MINUS_ONE;
5151 #endif
5152 }
5153
5154 /* Calculate the %highest function. */
5155
5156 static bfd_vma
5157 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5158 {
5159 #ifdef BFD64
5160 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5161 #else
5162 abort ();
5163 return MINUS_ONE;
5164 #endif
5165 }
5166 \f
5167 /* Create the .compact_rel section. */
5168
5169 static bfd_boolean
5170 mips_elf_create_compact_rel_section
5171 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5172 {
5173 flagword flags;
5174 register asection *s;
5175
5176 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5177 {
5178 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5179 | SEC_READONLY);
5180
5181 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5182 if (s == NULL
5183 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5184 return FALSE;
5185
5186 s->size = sizeof (Elf32_External_compact_rel);
5187 }
5188
5189 return TRUE;
5190 }
5191
5192 /* Create the .got section to hold the global offset table. */
5193
5194 static bfd_boolean
5195 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5196 {
5197 flagword flags;
5198 register asection *s;
5199 struct elf_link_hash_entry *h;
5200 struct bfd_link_hash_entry *bh;
5201 struct mips_elf_link_hash_table *htab;
5202
5203 htab = mips_elf_hash_table (info);
5204 BFD_ASSERT (htab != NULL);
5205
5206 /* This function may be called more than once. */
5207 if (htab->root.sgot)
5208 return TRUE;
5209
5210 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5211 | SEC_LINKER_CREATED);
5212
5213 /* We have to use an alignment of 2**4 here because this is hardcoded
5214 in the function stub generation and in the linker script. */
5215 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5216 if (s == NULL
5217 || !bfd_set_section_alignment (s, 4))
5218 return FALSE;
5219 htab->root.sgot = s;
5220
5221 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5222 linker script because we don't want to define the symbol if we
5223 are not creating a global offset table. */
5224 bh = NULL;
5225 if (! (_bfd_generic_link_add_one_symbol
5226 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5227 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5228 return FALSE;
5229
5230 h = (struct elf_link_hash_entry *) bh;
5231 h->non_elf = 0;
5232 h->def_regular = 1;
5233 h->type = STT_OBJECT;
5234 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5235 elf_hash_table (info)->hgot = h;
5236
5237 if (bfd_link_pic (info)
5238 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5239 return FALSE;
5240
5241 htab->got_info = mips_elf_create_got_info (abfd);
5242 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5243 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5244
5245 /* We also need a .got.plt section when generating PLTs. */
5246 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5247 SEC_ALLOC | SEC_LOAD
5248 | SEC_HAS_CONTENTS
5249 | SEC_IN_MEMORY
5250 | SEC_LINKER_CREATED);
5251 if (s == NULL)
5252 return FALSE;
5253 htab->root.sgotplt = s;
5254
5255 return TRUE;
5256 }
5257 \f
5258 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5259 __GOTT_INDEX__ symbols. These symbols are only special for
5260 shared objects; they are not used in executables. */
5261
5262 static bfd_boolean
5263 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5264 {
5265 return (mips_elf_hash_table (info)->root.target_os == is_vxworks
5266 && bfd_link_pic (info)
5267 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5268 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5269 }
5270
5271 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5272 require an la25 stub. See also mips_elf_local_pic_function_p,
5273 which determines whether the destination function ever requires a
5274 stub. */
5275
5276 static bfd_boolean
5277 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5278 bfd_boolean target_is_16_bit_code_p)
5279 {
5280 /* We specifically ignore branches and jumps from EF_PIC objects,
5281 where the onus is on the compiler or programmer to perform any
5282 necessary initialization of $25. Sometimes such initialization
5283 is unnecessary; for example, -mno-shared functions do not use
5284 the incoming value of $25, and may therefore be called directly. */
5285 if (PIC_OBJECT_P (input_bfd))
5286 return FALSE;
5287
5288 switch (r_type)
5289 {
5290 case R_MIPS_26:
5291 case R_MIPS_PC16:
5292 case R_MIPS_PC21_S2:
5293 case R_MIPS_PC26_S2:
5294 case R_MICROMIPS_26_S1:
5295 case R_MICROMIPS_PC7_S1:
5296 case R_MICROMIPS_PC10_S1:
5297 case R_MICROMIPS_PC16_S1:
5298 case R_MICROMIPS_PC23_S2:
5299 return TRUE;
5300
5301 case R_MIPS16_26:
5302 return !target_is_16_bit_code_p;
5303
5304 default:
5305 return FALSE;
5306 }
5307 }
5308 \f
5309 /* Obtain the field relocated by RELOCATION. */
5310
5311 static bfd_vma
5312 mips_elf_obtain_contents (reloc_howto_type *howto,
5313 const Elf_Internal_Rela *relocation,
5314 bfd *input_bfd, bfd_byte *contents)
5315 {
5316 bfd_vma x = 0;
5317 bfd_byte *location = contents + relocation->r_offset;
5318 unsigned int size = bfd_get_reloc_size (howto);
5319
5320 /* Obtain the bytes. */
5321 if (size != 0)
5322 x = bfd_get (8 * size, input_bfd, location);
5323
5324 return x;
5325 }
5326
5327 /* Store the field relocated by RELOCATION. */
5328
5329 static void
5330 mips_elf_store_contents (reloc_howto_type *howto,
5331 const Elf_Internal_Rela *relocation,
5332 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5333 {
5334 bfd_byte *location = contents + relocation->r_offset;
5335 unsigned int size = bfd_get_reloc_size (howto);
5336
5337 /* Put the value into the output. */
5338 if (size != 0)
5339 bfd_put (8 * size, input_bfd, x, location);
5340 }
5341
5342 /* Try to patch a load from GOT instruction in CONTENTS pointed to by
5343 RELOCATION described by HOWTO, with a move of 0 to the load target
5344 register, returning TRUE if that is successful and FALSE otherwise.
5345 If DOIT is FALSE, then only determine it patching is possible and
5346 return status without actually changing CONTENTS.
5347 */
5348
5349 static bfd_boolean
5350 mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5351 const Elf_Internal_Rela *relocation,
5352 reloc_howto_type *howto, bfd_boolean doit)
5353 {
5354 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5355 bfd_byte *location = contents + relocation->r_offset;
5356 bfd_boolean nullified = TRUE;
5357 bfd_vma x;
5358
5359 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5360
5361 /* Obtain the current value. */
5362 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5363
5364 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5365 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5366 if (mips16_reloc_p (r_type)
5367 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5368 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
5369 x = (0x3cd << 22) | (x & (7 << 16)) << 3; /* LI */
5370 else if (micromips_reloc_p (r_type)
5371 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5372 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5373 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5374 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5375 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5376 else
5377 nullified = FALSE;
5378
5379 /* Put the value into the output. */
5380 if (doit && nullified)
5381 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5382
5383 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, FALSE, location);
5384
5385 return nullified;
5386 }
5387
5388 /* Calculate the value produced by the RELOCATION (which comes from
5389 the INPUT_BFD). The ADDEND is the addend to use for this
5390 RELOCATION; RELOCATION->R_ADDEND is ignored.
5391
5392 The result of the relocation calculation is stored in VALUEP.
5393 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5394 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5395
5396 This function returns bfd_reloc_continue if the caller need take no
5397 further action regarding this relocation, bfd_reloc_notsupported if
5398 something goes dramatically wrong, bfd_reloc_overflow if an
5399 overflow occurs, and bfd_reloc_ok to indicate success. */
5400
5401 static bfd_reloc_status_type
5402 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5403 asection *input_section, bfd_byte *contents,
5404 struct bfd_link_info *info,
5405 const Elf_Internal_Rela *relocation,
5406 bfd_vma addend, reloc_howto_type *howto,
5407 Elf_Internal_Sym *local_syms,
5408 asection **local_sections, bfd_vma *valuep,
5409 const char **namep,
5410 bfd_boolean *cross_mode_jump_p,
5411 bfd_boolean save_addend)
5412 {
5413 /* The eventual value we will return. */
5414 bfd_vma value;
5415 /* The address of the symbol against which the relocation is
5416 occurring. */
5417 bfd_vma symbol = 0;
5418 /* The final GP value to be used for the relocatable, executable, or
5419 shared object file being produced. */
5420 bfd_vma gp;
5421 /* The place (section offset or address) of the storage unit being
5422 relocated. */
5423 bfd_vma p;
5424 /* The value of GP used to create the relocatable object. */
5425 bfd_vma gp0;
5426 /* The offset into the global offset table at which the address of
5427 the relocation entry symbol, adjusted by the addend, resides
5428 during execution. */
5429 bfd_vma g = MINUS_ONE;
5430 /* The section in which the symbol referenced by the relocation is
5431 located. */
5432 asection *sec = NULL;
5433 struct mips_elf_link_hash_entry *h = NULL;
5434 /* TRUE if the symbol referred to by this relocation is a local
5435 symbol. */
5436 bfd_boolean local_p, was_local_p;
5437 /* TRUE if the symbol referred to by this relocation is a section
5438 symbol. */
5439 bfd_boolean section_p = FALSE;
5440 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5441 bfd_boolean gp_disp_p = FALSE;
5442 /* TRUE if the symbol referred to by this relocation is
5443 "__gnu_local_gp". */
5444 bfd_boolean gnu_local_gp_p = FALSE;
5445 Elf_Internal_Shdr *symtab_hdr;
5446 size_t extsymoff;
5447 unsigned long r_symndx;
5448 int r_type;
5449 /* TRUE if overflow occurred during the calculation of the
5450 relocation value. */
5451 bfd_boolean overflowed_p;
5452 /* TRUE if this relocation refers to a MIPS16 function. */
5453 bfd_boolean target_is_16_bit_code_p = FALSE;
5454 bfd_boolean target_is_micromips_code_p = FALSE;
5455 struct mips_elf_link_hash_table *htab;
5456 bfd *dynobj;
5457 bfd_boolean resolved_to_zero;
5458
5459 dynobj = elf_hash_table (info)->dynobj;
5460 htab = mips_elf_hash_table (info);
5461 BFD_ASSERT (htab != NULL);
5462
5463 /* Parse the relocation. */
5464 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5465 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5466 p = (input_section->output_section->vma
5467 + input_section->output_offset
5468 + relocation->r_offset);
5469
5470 /* Assume that there will be no overflow. */
5471 overflowed_p = FALSE;
5472
5473 /* Figure out whether or not the symbol is local, and get the offset
5474 used in the array of hash table entries. */
5475 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5476 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5477 local_sections);
5478 was_local_p = local_p;
5479 if (! elf_bad_symtab (input_bfd))
5480 extsymoff = symtab_hdr->sh_info;
5481 else
5482 {
5483 /* The symbol table does not follow the rule that local symbols
5484 must come before globals. */
5485 extsymoff = 0;
5486 }
5487
5488 /* Figure out the value of the symbol. */
5489 if (local_p)
5490 {
5491 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5492 Elf_Internal_Sym *sym;
5493
5494 sym = local_syms + r_symndx;
5495 sec = local_sections[r_symndx];
5496
5497 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5498
5499 symbol = sec->output_section->vma + sec->output_offset;
5500 if (!section_p || (sec->flags & SEC_MERGE))
5501 symbol += sym->st_value;
5502 if ((sec->flags & SEC_MERGE) && section_p)
5503 {
5504 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5505 addend -= symbol;
5506 addend += sec->output_section->vma + sec->output_offset;
5507 }
5508
5509 /* MIPS16/microMIPS text labels should be treated as odd. */
5510 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5511 ++symbol;
5512
5513 /* Record the name of this symbol, for our caller. */
5514 *namep = bfd_elf_string_from_elf_section (input_bfd,
5515 symtab_hdr->sh_link,
5516 sym->st_name);
5517 if (*namep == NULL || **namep == '\0')
5518 *namep = bfd_section_name (sec);
5519
5520 /* For relocations against a section symbol and ones against no
5521 symbol (absolute relocations) infer the ISA mode from the addend. */
5522 if (section_p || r_symndx == STN_UNDEF)
5523 {
5524 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5525 target_is_micromips_code_p = (addend & 1) && micromips_p;
5526 }
5527 /* For relocations against an absolute symbol infer the ISA mode
5528 from the value of the symbol plus addend. */
5529 else if (bfd_is_abs_section (sec))
5530 {
5531 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5532 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5533 }
5534 /* Otherwise just use the regular symbol annotation available. */
5535 else
5536 {
5537 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5538 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5539 }
5540 }
5541 else
5542 {
5543 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5544
5545 /* For global symbols we look up the symbol in the hash-table. */
5546 h = ((struct mips_elf_link_hash_entry *)
5547 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5548 /* Find the real hash-table entry for this symbol. */
5549 while (h->root.root.type == bfd_link_hash_indirect
5550 || h->root.root.type == bfd_link_hash_warning)
5551 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5552
5553 /* Record the name of this symbol, for our caller. */
5554 *namep = h->root.root.root.string;
5555
5556 /* See if this is the special _gp_disp symbol. Note that such a
5557 symbol must always be a global symbol. */
5558 if (strcmp (*namep, "_gp_disp") == 0
5559 && ! NEWABI_P (input_bfd))
5560 {
5561 /* Relocations against _gp_disp are permitted only with
5562 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5563 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5564 return bfd_reloc_notsupported;
5565
5566 gp_disp_p = TRUE;
5567 }
5568 /* See if this is the special _gp symbol. Note that such a
5569 symbol must always be a global symbol. */
5570 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5571 gnu_local_gp_p = TRUE;
5572
5573
5574 /* If this symbol is defined, calculate its address. Note that
5575 _gp_disp is a magic symbol, always implicitly defined by the
5576 linker, so it's inappropriate to check to see whether or not
5577 its defined. */
5578 else if ((h->root.root.type == bfd_link_hash_defined
5579 || h->root.root.type == bfd_link_hash_defweak)
5580 && h->root.root.u.def.section)
5581 {
5582 sec = h->root.root.u.def.section;
5583 if (sec->output_section)
5584 symbol = (h->root.root.u.def.value
5585 + sec->output_section->vma
5586 + sec->output_offset);
5587 else
5588 symbol = h->root.root.u.def.value;
5589 }
5590 else if (h->root.root.type == bfd_link_hash_undefweak)
5591 /* We allow relocations against undefined weak symbols, giving
5592 it the value zero, so that you can undefined weak functions
5593 and check to see if they exist by looking at their
5594 addresses. */
5595 symbol = 0;
5596 else if (info->unresolved_syms_in_objects == RM_IGNORE
5597 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5598 symbol = 0;
5599 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5600 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5601 {
5602 /* If this is a dynamic link, we should have created a
5603 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5604 in _bfd_mips_elf_create_dynamic_sections.
5605 Otherwise, we should define the symbol with a value of 0.
5606 FIXME: It should probably get into the symbol table
5607 somehow as well. */
5608 BFD_ASSERT (! bfd_link_pic (info));
5609 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5610 symbol = 0;
5611 }
5612 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5613 {
5614 /* This is an optional symbol - an Irix specific extension to the
5615 ELF spec. Ignore it for now.
5616 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5617 than simply ignoring them, but we do not handle this for now.
5618 For information see the "64-bit ELF Object File Specification"
5619 which is available from here:
5620 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5621 symbol = 0;
5622 }
5623 else
5624 {
5625 bfd_boolean reject_undefined
5626 = (info->unresolved_syms_in_objects == RM_DIAGNOSE
5627 && !info->warn_unresolved_syms)
5628 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT;
5629
5630 info->callbacks->undefined_symbol
5631 (info, h->root.root.root.string, input_bfd,
5632 input_section, relocation->r_offset, reject_undefined);
5633
5634 if (reject_undefined)
5635 return bfd_reloc_undefined;
5636
5637 symbol = 0;
5638 }
5639
5640 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5641 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5642 }
5643
5644 /* If this is a reference to a 16-bit function with a stub, we need
5645 to redirect the relocation to the stub unless:
5646
5647 (a) the relocation is for a MIPS16 JAL;
5648
5649 (b) the relocation is for a MIPS16 PIC call, and there are no
5650 non-MIPS16 uses of the GOT slot; or
5651
5652 (c) the section allows direct references to MIPS16 functions. */
5653 if (r_type != R_MIPS16_26
5654 && !bfd_link_relocatable (info)
5655 && ((h != NULL
5656 && h->fn_stub != NULL
5657 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5658 || (local_p
5659 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5660 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5661 && !section_allows_mips16_refs_p (input_section))
5662 {
5663 /* This is a 32- or 64-bit call to a 16-bit function. We should
5664 have already noticed that we were going to need the
5665 stub. */
5666 if (local_p)
5667 {
5668 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5669 value = 0;
5670 }
5671 else
5672 {
5673 BFD_ASSERT (h->need_fn_stub);
5674 if (h->la25_stub)
5675 {
5676 /* If a LA25 header for the stub itself exists, point to the
5677 prepended LUI/ADDIU sequence. */
5678 sec = h->la25_stub->stub_section;
5679 value = h->la25_stub->offset;
5680 }
5681 else
5682 {
5683 sec = h->fn_stub;
5684 value = 0;
5685 }
5686 }
5687
5688 symbol = sec->output_section->vma + sec->output_offset + value;
5689 /* The target is 16-bit, but the stub isn't. */
5690 target_is_16_bit_code_p = FALSE;
5691 }
5692 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5693 to a standard MIPS function, we need to redirect the call to the stub.
5694 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5695 indirect calls should use an indirect stub instead. */
5696 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
5697 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5698 || (local_p
5699 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5700 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5701 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5702 {
5703 if (local_p)
5704 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5705 else
5706 {
5707 /* If both call_stub and call_fp_stub are defined, we can figure
5708 out which one to use by checking which one appears in the input
5709 file. */
5710 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5711 {
5712 asection *o;
5713
5714 sec = NULL;
5715 for (o = input_bfd->sections; o != NULL; o = o->next)
5716 {
5717 if (CALL_FP_STUB_P (bfd_section_name (o)))
5718 {
5719 sec = h->call_fp_stub;
5720 break;
5721 }
5722 }
5723 if (sec == NULL)
5724 sec = h->call_stub;
5725 }
5726 else if (h->call_stub != NULL)
5727 sec = h->call_stub;
5728 else
5729 sec = h->call_fp_stub;
5730 }
5731
5732 BFD_ASSERT (sec->size > 0);
5733 symbol = sec->output_section->vma + sec->output_offset;
5734 }
5735 /* If this is a direct call to a PIC function, redirect to the
5736 non-PIC stub. */
5737 else if (h != NULL && h->la25_stub
5738 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5739 target_is_16_bit_code_p))
5740 {
5741 symbol = (h->la25_stub->stub_section->output_section->vma
5742 + h->la25_stub->stub_section->output_offset
5743 + h->la25_stub->offset);
5744 if (ELF_ST_IS_MICROMIPS (h->root.other))
5745 symbol |= 1;
5746 }
5747 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5748 entry is used if a standard PLT entry has also been made. In this
5749 case the symbol will have been set by mips_elf_set_plt_sym_value
5750 to point to the standard PLT entry, so redirect to the compressed
5751 one. */
5752 else if ((mips16_branch_reloc_p (r_type)
5753 || micromips_branch_reloc_p (r_type))
5754 && !bfd_link_relocatable (info)
5755 && h != NULL
5756 && h->use_plt_entry
5757 && h->root.plt.plist->comp_offset != MINUS_ONE
5758 && h->root.plt.plist->mips_offset != MINUS_ONE)
5759 {
5760 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5761
5762 sec = htab->root.splt;
5763 symbol = (sec->output_section->vma
5764 + sec->output_offset
5765 + htab->plt_header_size
5766 + htab->plt_mips_offset
5767 + h->root.plt.plist->comp_offset
5768 + 1);
5769
5770 target_is_16_bit_code_p = !micromips_p;
5771 target_is_micromips_code_p = micromips_p;
5772 }
5773
5774 /* Make sure MIPS16 and microMIPS are not used together. */
5775 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
5776 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5777 {
5778 _bfd_error_handler
5779 (_("MIPS16 and microMIPS functions cannot call each other"));
5780 return bfd_reloc_notsupported;
5781 }
5782
5783 /* Calls from 16-bit code to 32-bit code and vice versa require the
5784 mode change. However, we can ignore calls to undefined weak symbols,
5785 which should never be executed at runtime. This exception is important
5786 because the assembly writer may have "known" that any definition of the
5787 symbol would be 16-bit code, and that direct jumps were therefore
5788 acceptable. */
5789 *cross_mode_jump_p = (!bfd_link_relocatable (info)
5790 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5791 && ((mips16_branch_reloc_p (r_type)
5792 && !target_is_16_bit_code_p)
5793 || (micromips_branch_reloc_p (r_type)
5794 && !target_is_micromips_code_p)
5795 || ((branch_reloc_p (r_type)
5796 || r_type == R_MIPS_JALR)
5797 && (target_is_16_bit_code_p
5798 || target_is_micromips_code_p))));
5799
5800 resolved_to_zero = (h != NULL
5801 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5802
5803 switch (r_type)
5804 {
5805 case R_MIPS16_CALL16:
5806 case R_MIPS16_GOT16:
5807 case R_MIPS_CALL16:
5808 case R_MIPS_GOT16:
5809 case R_MIPS_GOT_PAGE:
5810 case R_MIPS_GOT_DISP:
5811 case R_MIPS_GOT_LO16:
5812 case R_MIPS_CALL_LO16:
5813 case R_MICROMIPS_CALL16:
5814 case R_MICROMIPS_GOT16:
5815 case R_MICROMIPS_GOT_PAGE:
5816 case R_MICROMIPS_GOT_DISP:
5817 case R_MICROMIPS_GOT_LO16:
5818 case R_MICROMIPS_CALL_LO16:
5819 if (resolved_to_zero
5820 && !bfd_link_relocatable (info)
5821 && mips_elf_nullify_got_load (input_bfd, contents,
5822 relocation, howto, TRUE))
5823 return bfd_reloc_continue;
5824
5825 /* Fall through. */
5826 case R_MIPS_GOT_HI16:
5827 case R_MIPS_CALL_HI16:
5828 case R_MICROMIPS_GOT_HI16:
5829 case R_MICROMIPS_CALL_HI16:
5830 if (resolved_to_zero
5831 && htab->use_absolute_zero
5832 && bfd_link_pic (info))
5833 {
5834 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5835 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5836 FALSE, FALSE, FALSE);
5837 BFD_ASSERT (h != NULL);
5838 }
5839 break;
5840 }
5841
5842 local_p = (h == NULL || mips_use_local_got_p (info, h));
5843
5844 gp0 = _bfd_get_gp_value (input_bfd);
5845 gp = _bfd_get_gp_value (abfd);
5846 if (htab->got_info)
5847 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5848
5849 if (gnu_local_gp_p)
5850 symbol = gp;
5851
5852 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5853 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5854 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5855 if (got_page_reloc_p (r_type) && !local_p)
5856 {
5857 r_type = (micromips_reloc_p (r_type)
5858 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5859 addend = 0;
5860 }
5861
5862 /* If we haven't already determined the GOT offset, and we're going
5863 to need it, get it now. */
5864 switch (r_type)
5865 {
5866 case R_MIPS16_CALL16:
5867 case R_MIPS16_GOT16:
5868 case R_MIPS_CALL16:
5869 case R_MIPS_GOT16:
5870 case R_MIPS_GOT_DISP:
5871 case R_MIPS_GOT_HI16:
5872 case R_MIPS_CALL_HI16:
5873 case R_MIPS_GOT_LO16:
5874 case R_MIPS_CALL_LO16:
5875 case R_MICROMIPS_CALL16:
5876 case R_MICROMIPS_GOT16:
5877 case R_MICROMIPS_GOT_DISP:
5878 case R_MICROMIPS_GOT_HI16:
5879 case R_MICROMIPS_CALL_HI16:
5880 case R_MICROMIPS_GOT_LO16:
5881 case R_MICROMIPS_CALL_LO16:
5882 case R_MIPS_TLS_GD:
5883 case R_MIPS_TLS_GOTTPREL:
5884 case R_MIPS_TLS_LDM:
5885 case R_MIPS16_TLS_GD:
5886 case R_MIPS16_TLS_GOTTPREL:
5887 case R_MIPS16_TLS_LDM:
5888 case R_MICROMIPS_TLS_GD:
5889 case R_MICROMIPS_TLS_GOTTPREL:
5890 case R_MICROMIPS_TLS_LDM:
5891 /* Find the index into the GOT where this value is located. */
5892 if (tls_ldm_reloc_p (r_type))
5893 {
5894 g = mips_elf_local_got_index (abfd, input_bfd, info,
5895 0, 0, NULL, r_type);
5896 if (g == MINUS_ONE)
5897 return bfd_reloc_outofrange;
5898 }
5899 else if (!local_p)
5900 {
5901 /* On VxWorks, CALL relocations should refer to the .got.plt
5902 entry, which is initialized to point at the PLT stub. */
5903 if (htab->root.target_os == is_vxworks
5904 && (call_hi16_reloc_p (r_type)
5905 || call_lo16_reloc_p (r_type)
5906 || call16_reloc_p (r_type)))
5907 {
5908 BFD_ASSERT (addend == 0);
5909 BFD_ASSERT (h->root.needs_plt);
5910 g = mips_elf_gotplt_index (info, &h->root);
5911 }
5912 else
5913 {
5914 BFD_ASSERT (addend == 0);
5915 g = mips_elf_global_got_index (abfd, info, input_bfd,
5916 &h->root, r_type);
5917 if (!TLS_RELOC_P (r_type)
5918 && !elf_hash_table (info)->dynamic_sections_created)
5919 /* This is a static link. We must initialize the GOT entry. */
5920 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
5921 }
5922 }
5923 else if (htab->root.target_os != is_vxworks
5924 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5925 /* The calculation below does not involve "g". */
5926 break;
5927 else
5928 {
5929 g = mips_elf_local_got_index (abfd, input_bfd, info,
5930 symbol + addend, r_symndx, h, r_type);
5931 if (g == MINUS_ONE)
5932 return bfd_reloc_outofrange;
5933 }
5934
5935 /* Convert GOT indices to actual offsets. */
5936 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5937 break;
5938 }
5939
5940 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5941 symbols are resolved by the loader. Add them to .rela.dyn. */
5942 if (h != NULL && is_gott_symbol (info, &h->root))
5943 {
5944 Elf_Internal_Rela outrel;
5945 bfd_byte *loc;
5946 asection *s;
5947
5948 s = mips_elf_rel_dyn_section (info, FALSE);
5949 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5950
5951 outrel.r_offset = (input_section->output_section->vma
5952 + input_section->output_offset
5953 + relocation->r_offset);
5954 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5955 outrel.r_addend = addend;
5956 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5957
5958 /* If we've written this relocation for a readonly section,
5959 we need to set DF_TEXTREL again, so that we do not delete the
5960 DT_TEXTREL tag. */
5961 if (MIPS_ELF_READONLY_SECTION (input_section))
5962 info->flags |= DF_TEXTREL;
5963
5964 *valuep = 0;
5965 return bfd_reloc_ok;
5966 }
5967
5968 /* Figure out what kind of relocation is being performed. */
5969 switch (r_type)
5970 {
5971 case R_MIPS_NONE:
5972 return bfd_reloc_continue;
5973
5974 case R_MIPS_16:
5975 if (howto->partial_inplace)
5976 addend = _bfd_mips_elf_sign_extend (addend, 16);
5977 value = symbol + addend;
5978 overflowed_p = mips_elf_overflow_p (value, 16);
5979 break;
5980
5981 case R_MIPS_32:
5982 case R_MIPS_REL32:
5983 case R_MIPS_64:
5984 if ((bfd_link_pic (info)
5985 || (htab->root.dynamic_sections_created
5986 && h != NULL
5987 && h->root.def_dynamic
5988 && !h->root.def_regular
5989 && !h->has_static_relocs))
5990 && r_symndx != STN_UNDEF
5991 && (h == NULL
5992 || h->root.root.type != bfd_link_hash_undefweak
5993 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
5994 && !resolved_to_zero))
5995 && (input_section->flags & SEC_ALLOC) != 0)
5996 {
5997 /* If we're creating a shared library, then we can't know
5998 where the symbol will end up. So, we create a relocation
5999 record in the output, and leave the job up to the dynamic
6000 linker. We must do the same for executable references to
6001 shared library symbols, unless we've decided to use copy
6002 relocs or PLTs instead. */
6003 value = addend;
6004 if (!mips_elf_create_dynamic_relocation (abfd,
6005 info,
6006 relocation,
6007 h,
6008 sec,
6009 symbol,
6010 &value,
6011 input_section))
6012 return bfd_reloc_undefined;
6013 }
6014 else
6015 {
6016 if (r_type != R_MIPS_REL32)
6017 value = symbol + addend;
6018 else
6019 value = addend;
6020 }
6021 value &= howto->dst_mask;
6022 break;
6023
6024 case R_MIPS_PC32:
6025 value = symbol + addend - p;
6026 value &= howto->dst_mask;
6027 break;
6028
6029 case R_MIPS16_26:
6030 /* The calculation for R_MIPS16_26 is just the same as for an
6031 R_MIPS_26. It's only the storage of the relocated field into
6032 the output file that's different. That's handled in
6033 mips_elf_perform_relocation. So, we just fall through to the
6034 R_MIPS_26 case here. */
6035 case R_MIPS_26:
6036 case R_MICROMIPS_26_S1:
6037 {
6038 unsigned int shift;
6039
6040 /* Shift is 2, unusually, for microMIPS JALX. */
6041 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6042
6043 if (howto->partial_inplace && !section_p)
6044 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
6045 else
6046 value = addend;
6047 value += symbol;
6048
6049 /* Make sure the target of a jump is suitably aligned. Bit 0 must
6050 be the correct ISA mode selector except for weak undefined
6051 symbols. */
6052 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6053 && (*cross_mode_jump_p
6054 ? (value & 3) != (r_type == R_MIPS_26)
6055 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
6056 return bfd_reloc_outofrange;
6057
6058 value >>= shift;
6059 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6060 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6061 value &= howto->dst_mask;
6062 }
6063 break;
6064
6065 case R_MIPS_TLS_DTPREL_HI16:
6066 case R_MIPS16_TLS_DTPREL_HI16:
6067 case R_MICROMIPS_TLS_DTPREL_HI16:
6068 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6069 & howto->dst_mask);
6070 break;
6071
6072 case R_MIPS_TLS_DTPREL_LO16:
6073 case R_MIPS_TLS_DTPREL32:
6074 case R_MIPS_TLS_DTPREL64:
6075 case R_MIPS16_TLS_DTPREL_LO16:
6076 case R_MICROMIPS_TLS_DTPREL_LO16:
6077 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6078 break;
6079
6080 case R_MIPS_TLS_TPREL_HI16:
6081 case R_MIPS16_TLS_TPREL_HI16:
6082 case R_MICROMIPS_TLS_TPREL_HI16:
6083 value = (mips_elf_high (addend + symbol - tprel_base (info))
6084 & howto->dst_mask);
6085 break;
6086
6087 case R_MIPS_TLS_TPREL_LO16:
6088 case R_MIPS_TLS_TPREL32:
6089 case R_MIPS_TLS_TPREL64:
6090 case R_MIPS16_TLS_TPREL_LO16:
6091 case R_MICROMIPS_TLS_TPREL_LO16:
6092 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6093 break;
6094
6095 case R_MIPS_HI16:
6096 case R_MIPS16_HI16:
6097 case R_MICROMIPS_HI16:
6098 if (!gp_disp_p)
6099 {
6100 value = mips_elf_high (addend + symbol);
6101 value &= howto->dst_mask;
6102 }
6103 else
6104 {
6105 /* For MIPS16 ABI code we generate this sequence
6106 0: li $v0,%hi(_gp_disp)
6107 4: addiupc $v1,%lo(_gp_disp)
6108 8: sll $v0,16
6109 12: addu $v0,$v1
6110 14: move $gp,$v0
6111 So the offsets of hi and lo relocs are the same, but the
6112 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6113 ADDIUPC clears the low two bits of the instruction address,
6114 so the base is ($t9 + 4) & ~3. */
6115 if (r_type == R_MIPS16_HI16)
6116 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
6117 /* The microMIPS .cpload sequence uses the same assembly
6118 instructions as the traditional psABI version, but the
6119 incoming $t9 has the low bit set. */
6120 else if (r_type == R_MICROMIPS_HI16)
6121 value = mips_elf_high (addend + gp - p - 1);
6122 else
6123 value = mips_elf_high (addend + gp - p);
6124 }
6125 break;
6126
6127 case R_MIPS_LO16:
6128 case R_MIPS16_LO16:
6129 case R_MICROMIPS_LO16:
6130 case R_MICROMIPS_HI0_LO16:
6131 if (!gp_disp_p)
6132 value = (symbol + addend) & howto->dst_mask;
6133 else
6134 {
6135 /* See the comment for R_MIPS16_HI16 above for the reason
6136 for this conditional. */
6137 if (r_type == R_MIPS16_LO16)
6138 value = addend + gp - (p & ~(bfd_vma) 0x3);
6139 else if (r_type == R_MICROMIPS_LO16
6140 || r_type == R_MICROMIPS_HI0_LO16)
6141 value = addend + gp - p + 3;
6142 else
6143 value = addend + gp - p + 4;
6144 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
6145 for overflow. But, on, say, IRIX5, relocations against
6146 _gp_disp are normally generated from the .cpload
6147 pseudo-op. It generates code that normally looks like
6148 this:
6149
6150 lui $gp,%hi(_gp_disp)
6151 addiu $gp,$gp,%lo(_gp_disp)
6152 addu $gp,$gp,$t9
6153
6154 Here $t9 holds the address of the function being called,
6155 as required by the MIPS ELF ABI. The R_MIPS_LO16
6156 relocation can easily overflow in this situation, but the
6157 R_MIPS_HI16 relocation will handle the overflow.
6158 Therefore, we consider this a bug in the MIPS ABI, and do
6159 not check for overflow here. */
6160 }
6161 break;
6162
6163 case R_MIPS_LITERAL:
6164 case R_MICROMIPS_LITERAL:
6165 /* Because we don't merge literal sections, we can handle this
6166 just like R_MIPS_GPREL16. In the long run, we should merge
6167 shared literals, and then we will need to additional work
6168 here. */
6169
6170 /* Fall through. */
6171
6172 case R_MIPS16_GPREL:
6173 /* The R_MIPS16_GPREL performs the same calculation as
6174 R_MIPS_GPREL16, but stores the relocated bits in a different
6175 order. We don't need to do anything special here; the
6176 differences are handled in mips_elf_perform_relocation. */
6177 case R_MIPS_GPREL16:
6178 case R_MICROMIPS_GPREL7_S2:
6179 case R_MICROMIPS_GPREL16:
6180 /* Only sign-extend the addend if it was extracted from the
6181 instruction. If the addend was separate, leave it alone,
6182 otherwise we may lose significant bits. */
6183 if (howto->partial_inplace)
6184 addend = _bfd_mips_elf_sign_extend (addend, 16);
6185 value = symbol + addend - gp;
6186 /* If the symbol was local, any earlier relocatable links will
6187 have adjusted its addend with the gp offset, so compensate
6188 for that now. Don't do it for symbols forced local in this
6189 link, though, since they won't have had the gp offset applied
6190 to them before. */
6191 if (was_local_p)
6192 value += gp0;
6193 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6194 overflowed_p = mips_elf_overflow_p (value, 16);
6195 break;
6196
6197 case R_MIPS16_GOT16:
6198 case R_MIPS16_CALL16:
6199 case R_MIPS_GOT16:
6200 case R_MIPS_CALL16:
6201 case R_MICROMIPS_GOT16:
6202 case R_MICROMIPS_CALL16:
6203 /* VxWorks does not have separate local and global semantics for
6204 R_MIPS*_GOT16; every relocation evaluates to "G". */
6205 if (htab->root.target_os != is_vxworks && local_p)
6206 {
6207 value = mips_elf_got16_entry (abfd, input_bfd, info,
6208 symbol + addend, !was_local_p);
6209 if (value == MINUS_ONE)
6210 return bfd_reloc_outofrange;
6211 value
6212 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6213 overflowed_p = mips_elf_overflow_p (value, 16);
6214 break;
6215 }
6216
6217 /* Fall through. */
6218
6219 case R_MIPS_TLS_GD:
6220 case R_MIPS_TLS_GOTTPREL:
6221 case R_MIPS_TLS_LDM:
6222 case R_MIPS_GOT_DISP:
6223 case R_MIPS16_TLS_GD:
6224 case R_MIPS16_TLS_GOTTPREL:
6225 case R_MIPS16_TLS_LDM:
6226 case R_MICROMIPS_TLS_GD:
6227 case R_MICROMIPS_TLS_GOTTPREL:
6228 case R_MICROMIPS_TLS_LDM:
6229 case R_MICROMIPS_GOT_DISP:
6230 value = g;
6231 overflowed_p = mips_elf_overflow_p (value, 16);
6232 break;
6233
6234 case R_MIPS_GPREL32:
6235 value = (addend + symbol + gp0 - gp);
6236 if (!save_addend)
6237 value &= howto->dst_mask;
6238 break;
6239
6240 case R_MIPS_PC16:
6241 case R_MIPS_GNU_REL16_S2:
6242 if (howto->partial_inplace)
6243 addend = _bfd_mips_elf_sign_extend (addend, 18);
6244
6245 /* No need to exclude weak undefined symbols here as they resolve
6246 to 0 and never set `*cross_mode_jump_p', so this alignment check
6247 will never trigger for them. */
6248 if (*cross_mode_jump_p
6249 ? ((symbol + addend) & 3) != 1
6250 : ((symbol + addend) & 3) != 0)
6251 return bfd_reloc_outofrange;
6252
6253 value = symbol + addend - p;
6254 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6255 overflowed_p = mips_elf_overflow_p (value, 18);
6256 value >>= howto->rightshift;
6257 value &= howto->dst_mask;
6258 break;
6259
6260 case R_MIPS16_PC16_S1:
6261 if (howto->partial_inplace)
6262 addend = _bfd_mips_elf_sign_extend (addend, 17);
6263
6264 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6265 && (*cross_mode_jump_p
6266 ? ((symbol + addend) & 3) != 0
6267 : ((symbol + addend) & 1) == 0))
6268 return bfd_reloc_outofrange;
6269
6270 value = symbol + addend - p;
6271 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6272 overflowed_p = mips_elf_overflow_p (value, 17);
6273 value >>= howto->rightshift;
6274 value &= howto->dst_mask;
6275 break;
6276
6277 case R_MIPS_PC21_S2:
6278 if (howto->partial_inplace)
6279 addend = _bfd_mips_elf_sign_extend (addend, 23);
6280
6281 if ((symbol + addend) & 3)
6282 return bfd_reloc_outofrange;
6283
6284 value = symbol + addend - p;
6285 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6286 overflowed_p = mips_elf_overflow_p (value, 23);
6287 value >>= howto->rightshift;
6288 value &= howto->dst_mask;
6289 break;
6290
6291 case R_MIPS_PC26_S2:
6292 if (howto->partial_inplace)
6293 addend = _bfd_mips_elf_sign_extend (addend, 28);
6294
6295 if ((symbol + addend) & 3)
6296 return bfd_reloc_outofrange;
6297
6298 value = symbol + addend - p;
6299 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6300 overflowed_p = mips_elf_overflow_p (value, 28);
6301 value >>= howto->rightshift;
6302 value &= howto->dst_mask;
6303 break;
6304
6305 case R_MIPS_PC18_S3:
6306 if (howto->partial_inplace)
6307 addend = _bfd_mips_elf_sign_extend (addend, 21);
6308
6309 if ((symbol + addend) & 7)
6310 return bfd_reloc_outofrange;
6311
6312 value = symbol + addend - ((p | 7) ^ 7);
6313 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6314 overflowed_p = mips_elf_overflow_p (value, 21);
6315 value >>= howto->rightshift;
6316 value &= howto->dst_mask;
6317 break;
6318
6319 case R_MIPS_PC19_S2:
6320 if (howto->partial_inplace)
6321 addend = _bfd_mips_elf_sign_extend (addend, 21);
6322
6323 if ((symbol + addend) & 3)
6324 return bfd_reloc_outofrange;
6325
6326 value = symbol + addend - p;
6327 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6328 overflowed_p = mips_elf_overflow_p (value, 21);
6329 value >>= howto->rightshift;
6330 value &= howto->dst_mask;
6331 break;
6332
6333 case R_MIPS_PCHI16:
6334 value = mips_elf_high (symbol + addend - p);
6335 value &= howto->dst_mask;
6336 break;
6337
6338 case R_MIPS_PCLO16:
6339 if (howto->partial_inplace)
6340 addend = _bfd_mips_elf_sign_extend (addend, 16);
6341 value = symbol + addend - p;
6342 value &= howto->dst_mask;
6343 break;
6344
6345 case R_MICROMIPS_PC7_S1:
6346 if (howto->partial_inplace)
6347 addend = _bfd_mips_elf_sign_extend (addend, 8);
6348
6349 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6350 && (*cross_mode_jump_p
6351 ? ((symbol + addend + 2) & 3) != 0
6352 : ((symbol + addend + 2) & 1) == 0))
6353 return bfd_reloc_outofrange;
6354
6355 value = symbol + addend - p;
6356 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6357 overflowed_p = mips_elf_overflow_p (value, 8);
6358 value >>= howto->rightshift;
6359 value &= howto->dst_mask;
6360 break;
6361
6362 case R_MICROMIPS_PC10_S1:
6363 if (howto->partial_inplace)
6364 addend = _bfd_mips_elf_sign_extend (addend, 11);
6365
6366 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6367 && (*cross_mode_jump_p
6368 ? ((symbol + addend + 2) & 3) != 0
6369 : ((symbol + addend + 2) & 1) == 0))
6370 return bfd_reloc_outofrange;
6371
6372 value = symbol + addend - p;
6373 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6374 overflowed_p = mips_elf_overflow_p (value, 11);
6375 value >>= howto->rightshift;
6376 value &= howto->dst_mask;
6377 break;
6378
6379 case R_MICROMIPS_PC16_S1:
6380 if (howto->partial_inplace)
6381 addend = _bfd_mips_elf_sign_extend (addend, 17);
6382
6383 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6384 && (*cross_mode_jump_p
6385 ? ((symbol + addend) & 3) != 0
6386 : ((symbol + addend) & 1) == 0))
6387 return bfd_reloc_outofrange;
6388
6389 value = symbol + addend - p;
6390 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6391 overflowed_p = mips_elf_overflow_p (value, 17);
6392 value >>= howto->rightshift;
6393 value &= howto->dst_mask;
6394 break;
6395
6396 case R_MICROMIPS_PC23_S2:
6397 if (howto->partial_inplace)
6398 addend = _bfd_mips_elf_sign_extend (addend, 25);
6399 value = symbol + addend - ((p | 3) ^ 3);
6400 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6401 overflowed_p = mips_elf_overflow_p (value, 25);
6402 value >>= howto->rightshift;
6403 value &= howto->dst_mask;
6404 break;
6405
6406 case R_MIPS_GOT_HI16:
6407 case R_MIPS_CALL_HI16:
6408 case R_MICROMIPS_GOT_HI16:
6409 case R_MICROMIPS_CALL_HI16:
6410 /* We're allowed to handle these two relocations identically.
6411 The dynamic linker is allowed to handle the CALL relocations
6412 differently by creating a lazy evaluation stub. */
6413 value = g;
6414 value = mips_elf_high (value);
6415 value &= howto->dst_mask;
6416 break;
6417
6418 case R_MIPS_GOT_LO16:
6419 case R_MIPS_CALL_LO16:
6420 case R_MICROMIPS_GOT_LO16:
6421 case R_MICROMIPS_CALL_LO16:
6422 value = g & howto->dst_mask;
6423 break;
6424
6425 case R_MIPS_GOT_PAGE:
6426 case R_MICROMIPS_GOT_PAGE:
6427 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6428 if (value == MINUS_ONE)
6429 return bfd_reloc_outofrange;
6430 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6431 overflowed_p = mips_elf_overflow_p (value, 16);
6432 break;
6433
6434 case R_MIPS_GOT_OFST:
6435 case R_MICROMIPS_GOT_OFST:
6436 if (local_p)
6437 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6438 else
6439 value = addend;
6440 overflowed_p = mips_elf_overflow_p (value, 16);
6441 break;
6442
6443 case R_MIPS_SUB:
6444 case R_MICROMIPS_SUB:
6445 value = symbol - addend;
6446 value &= howto->dst_mask;
6447 break;
6448
6449 case R_MIPS_HIGHER:
6450 case R_MICROMIPS_HIGHER:
6451 value = mips_elf_higher (addend + symbol);
6452 value &= howto->dst_mask;
6453 break;
6454
6455 case R_MIPS_HIGHEST:
6456 case R_MICROMIPS_HIGHEST:
6457 value = mips_elf_highest (addend + symbol);
6458 value &= howto->dst_mask;
6459 break;
6460
6461 case R_MIPS_SCN_DISP:
6462 case R_MICROMIPS_SCN_DISP:
6463 value = symbol + addend - sec->output_offset;
6464 value &= howto->dst_mask;
6465 break;
6466
6467 case R_MIPS_JALR:
6468 case R_MICROMIPS_JALR:
6469 /* This relocation is only a hint. In some cases, we optimize
6470 it into a bal instruction. But we don't try to optimize
6471 when the symbol does not resolve locally. */
6472 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6473 return bfd_reloc_continue;
6474 /* We can't optimize cross-mode jumps either. */
6475 if (*cross_mode_jump_p)
6476 return bfd_reloc_continue;
6477 value = symbol + addend;
6478 /* Neither we can non-instruction-aligned targets. */
6479 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6480 return bfd_reloc_continue;
6481 break;
6482
6483 case R_MIPS_PJUMP:
6484 case R_MIPS_GNU_VTINHERIT:
6485 case R_MIPS_GNU_VTENTRY:
6486 /* We don't do anything with these at present. */
6487 return bfd_reloc_continue;
6488
6489 default:
6490 /* An unrecognized relocation type. */
6491 return bfd_reloc_notsupported;
6492 }
6493
6494 /* Store the VALUE for our caller. */
6495 *valuep = value;
6496 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6497 }
6498
6499 /* It has been determined that the result of the RELOCATION is the
6500 VALUE. Use HOWTO to place VALUE into the output file at the
6501 appropriate position. The SECTION is the section to which the
6502 relocation applies.
6503 CROSS_MODE_JUMP_P is true if the relocation field
6504 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6505
6506 Returns FALSE if anything goes wrong. */
6507
6508 static bfd_boolean
6509 mips_elf_perform_relocation (struct bfd_link_info *info,
6510 reloc_howto_type *howto,
6511 const Elf_Internal_Rela *relocation,
6512 bfd_vma value, bfd *input_bfd,
6513 asection *input_section, bfd_byte *contents,
6514 bfd_boolean cross_mode_jump_p)
6515 {
6516 bfd_vma x;
6517 bfd_byte *location;
6518 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6519
6520 /* Figure out where the relocation is occurring. */
6521 location = contents + relocation->r_offset;
6522
6523 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
6524
6525 /* Obtain the current value. */
6526 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6527
6528 /* Clear the field we are setting. */
6529 x &= ~howto->dst_mask;
6530
6531 /* Set the field. */
6532 x |= (value & howto->dst_mask);
6533
6534 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
6535 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6536 {
6537 bfd_vma opcode = x >> 26;
6538
6539 if (r_type == R_MIPS16_26 ? opcode == 0x7
6540 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6541 : opcode == 0x1d)
6542 {
6543 info->callbacks->einfo
6544 (_("%X%H: unsupported JALX to the same ISA mode\n"),
6545 input_bfd, input_section, relocation->r_offset);
6546 return TRUE;
6547 }
6548 }
6549 if (cross_mode_jump_p && jal_reloc_p (r_type))
6550 {
6551 bfd_boolean ok;
6552 bfd_vma opcode = x >> 26;
6553 bfd_vma jalx_opcode;
6554
6555 /* Check to see if the opcode is already JAL or JALX. */
6556 if (r_type == R_MIPS16_26)
6557 {
6558 ok = ((opcode == 0x6) || (opcode == 0x7));
6559 jalx_opcode = 0x7;
6560 }
6561 else if (r_type == R_MICROMIPS_26_S1)
6562 {
6563 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6564 jalx_opcode = 0x3c;
6565 }
6566 else
6567 {
6568 ok = ((opcode == 0x3) || (opcode == 0x1d));
6569 jalx_opcode = 0x1d;
6570 }
6571
6572 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6573 convert J or JALS to JALX. */
6574 if (!ok)
6575 {
6576 info->callbacks->einfo
6577 (_("%X%H: unsupported jump between ISA modes; "
6578 "consider recompiling with interlinking enabled\n"),
6579 input_bfd, input_section, relocation->r_offset);
6580 return TRUE;
6581 }
6582
6583 /* Make this the JALX opcode. */
6584 x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
6585 }
6586 else if (cross_mode_jump_p && b_reloc_p (r_type))
6587 {
6588 bfd_boolean ok = FALSE;
6589 bfd_vma opcode = x >> 16;
6590 bfd_vma jalx_opcode = 0;
6591 bfd_vma sign_bit = 0;
6592 bfd_vma addr;
6593 bfd_vma dest;
6594
6595 if (r_type == R_MICROMIPS_PC16_S1)
6596 {
6597 ok = opcode == 0x4060;
6598 jalx_opcode = 0x3c;
6599 sign_bit = 0x10000;
6600 value <<= 1;
6601 }
6602 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6603 {
6604 ok = opcode == 0x411;
6605 jalx_opcode = 0x1d;
6606 sign_bit = 0x20000;
6607 value <<= 2;
6608 }
6609
6610 if (ok && !bfd_link_pic (info))
6611 {
6612 addr = (input_section->output_section->vma
6613 + input_section->output_offset
6614 + relocation->r_offset
6615 + 4);
6616 dest = (addr
6617 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
6618
6619 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6620 {
6621 info->callbacks->einfo
6622 (_("%X%H: cannot convert branch between ISA modes "
6623 "to JALX: relocation out of range\n"),
6624 input_bfd, input_section, relocation->r_offset);
6625 return TRUE;
6626 }
6627
6628 /* Make this the JALX opcode. */
6629 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6630 }
6631 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
6632 {
6633 info->callbacks->einfo
6634 (_("%X%H: unsupported branch between ISA modes\n"),
6635 input_bfd, input_section, relocation->r_offset);
6636 return TRUE;
6637 }
6638 }
6639
6640 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6641 range. */
6642 if (!bfd_link_relocatable (info)
6643 && !cross_mode_jump_p
6644 && ((JAL_TO_BAL_P (input_bfd)
6645 && r_type == R_MIPS_26
6646 && (x >> 26) == 0x3) /* jal addr */
6647 || (JALR_TO_BAL_P (input_bfd)
6648 && r_type == R_MIPS_JALR
6649 && x == 0x0320f809) /* jalr t9 */
6650 || (JR_TO_B_P (input_bfd)
6651 && r_type == R_MIPS_JALR
6652 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
6653 {
6654 bfd_vma addr;
6655 bfd_vma dest;
6656 bfd_signed_vma off;
6657
6658 addr = (input_section->output_section->vma
6659 + input_section->output_offset
6660 + relocation->r_offset
6661 + 4);
6662 if (r_type == R_MIPS_26)
6663 dest = (value << 2) | ((addr >> 28) << 28);
6664 else
6665 dest = value;
6666 off = dest - addr;
6667 if (off <= 0x1ffff && off >= -0x20000)
6668 {
6669 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
6670 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6671 else
6672 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6673 }
6674 }
6675
6676 /* Put the value into the output. */
6677 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
6678
6679 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
6680 location);
6681
6682 return TRUE;
6683 }
6684 \f
6685 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6686 is the original relocation, which is now being transformed into a
6687 dynamic relocation. The ADDENDP is adjusted if necessary; the
6688 caller should store the result in place of the original addend. */
6689
6690 static bfd_boolean
6691 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6692 struct bfd_link_info *info,
6693 const Elf_Internal_Rela *rel,
6694 struct mips_elf_link_hash_entry *h,
6695 asection *sec, bfd_vma symbol,
6696 bfd_vma *addendp, asection *input_section)
6697 {
6698 Elf_Internal_Rela outrel[3];
6699 asection *sreloc;
6700 bfd *dynobj;
6701 int r_type;
6702 long indx;
6703 bfd_boolean defined_p;
6704 struct mips_elf_link_hash_table *htab;
6705
6706 htab = mips_elf_hash_table (info);
6707 BFD_ASSERT (htab != NULL);
6708
6709 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6710 dynobj = elf_hash_table (info)->dynobj;
6711 sreloc = mips_elf_rel_dyn_section (info, FALSE);
6712 BFD_ASSERT (sreloc != NULL);
6713 BFD_ASSERT (sreloc->contents != NULL);
6714 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6715 < sreloc->size);
6716
6717 outrel[0].r_offset =
6718 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6719 if (ABI_64_P (output_bfd))
6720 {
6721 outrel[1].r_offset =
6722 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6723 outrel[2].r_offset =
6724 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6725 }
6726
6727 if (outrel[0].r_offset == MINUS_ONE)
6728 /* The relocation field has been deleted. */
6729 return TRUE;
6730
6731 if (outrel[0].r_offset == MINUS_TWO)
6732 {
6733 /* The relocation field has been converted into a relative value of
6734 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6735 the field to be fully relocated, so add in the symbol's value. */
6736 *addendp += symbol;
6737 return TRUE;
6738 }
6739
6740 /* We must now calculate the dynamic symbol table index to use
6741 in the relocation. */
6742 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6743 {
6744 BFD_ASSERT (htab->root.target_os == is_vxworks
6745 || h->global_got_area != GGA_NONE);
6746 indx = h->root.dynindx;
6747 if (SGI_COMPAT (output_bfd))
6748 defined_p = h->root.def_regular;
6749 else
6750 /* ??? glibc's ld.so just adds the final GOT entry to the
6751 relocation field. It therefore treats relocs against
6752 defined symbols in the same way as relocs against
6753 undefined symbols. */
6754 defined_p = FALSE;
6755 }
6756 else
6757 {
6758 if (sec != NULL && bfd_is_abs_section (sec))
6759 indx = 0;
6760 else if (sec == NULL || sec->owner == NULL)
6761 {
6762 bfd_set_error (bfd_error_bad_value);
6763 return FALSE;
6764 }
6765 else
6766 {
6767 indx = elf_section_data (sec->output_section)->dynindx;
6768 if (indx == 0)
6769 {
6770 asection *osec = htab->root.text_index_section;
6771 indx = elf_section_data (osec)->dynindx;
6772 }
6773 if (indx == 0)
6774 abort ();
6775 }
6776
6777 /* Instead of generating a relocation using the section
6778 symbol, we may as well make it a fully relative
6779 relocation. We want to avoid generating relocations to
6780 local symbols because we used to generate them
6781 incorrectly, without adding the original symbol value,
6782 which is mandated by the ABI for section symbols. In
6783 order to give dynamic loaders and applications time to
6784 phase out the incorrect use, we refrain from emitting
6785 section-relative relocations. It's not like they're
6786 useful, after all. This should be a bit more efficient
6787 as well. */
6788 /* ??? Although this behavior is compatible with glibc's ld.so,
6789 the ABI says that relocations against STN_UNDEF should have
6790 a symbol value of 0. Irix rld honors this, so relocations
6791 against STN_UNDEF have no effect. */
6792 if (!SGI_COMPAT (output_bfd))
6793 indx = 0;
6794 defined_p = TRUE;
6795 }
6796
6797 /* If the relocation was previously an absolute relocation and
6798 this symbol will not be referred to by the relocation, we must
6799 adjust it by the value we give it in the dynamic symbol table.
6800 Otherwise leave the job up to the dynamic linker. */
6801 if (defined_p && r_type != R_MIPS_REL32)
6802 *addendp += symbol;
6803
6804 if (htab->root.target_os == is_vxworks)
6805 /* VxWorks uses non-relative relocations for this. */
6806 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6807 else
6808 /* The relocation is always an REL32 relocation because we don't
6809 know where the shared library will wind up at load-time. */
6810 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6811 R_MIPS_REL32);
6812
6813 /* For strict adherence to the ABI specification, we should
6814 generate a R_MIPS_64 relocation record by itself before the
6815 _REL32/_64 record as well, such that the addend is read in as
6816 a 64-bit value (REL32 is a 32-bit relocation, after all).
6817 However, since none of the existing ELF64 MIPS dynamic
6818 loaders seems to care, we don't waste space with these
6819 artificial relocations. If this turns out to not be true,
6820 mips_elf_allocate_dynamic_relocation() should be tweaked so
6821 as to make room for a pair of dynamic relocations per
6822 invocation if ABI_64_P, and here we should generate an
6823 additional relocation record with R_MIPS_64 by itself for a
6824 NULL symbol before this relocation record. */
6825 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6826 ABI_64_P (output_bfd)
6827 ? R_MIPS_64
6828 : R_MIPS_NONE);
6829 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6830
6831 /* Adjust the output offset of the relocation to reference the
6832 correct location in the output file. */
6833 outrel[0].r_offset += (input_section->output_section->vma
6834 + input_section->output_offset);
6835 outrel[1].r_offset += (input_section->output_section->vma
6836 + input_section->output_offset);
6837 outrel[2].r_offset += (input_section->output_section->vma
6838 + input_section->output_offset);
6839
6840 /* Put the relocation back out. We have to use the special
6841 relocation outputter in the 64-bit case since the 64-bit
6842 relocation format is non-standard. */
6843 if (ABI_64_P (output_bfd))
6844 {
6845 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6846 (output_bfd, &outrel[0],
6847 (sreloc->contents
6848 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6849 }
6850 else if (htab->root.target_os == is_vxworks)
6851 {
6852 /* VxWorks uses RELA rather than REL dynamic relocations. */
6853 outrel[0].r_addend = *addendp;
6854 bfd_elf32_swap_reloca_out
6855 (output_bfd, &outrel[0],
6856 (sreloc->contents
6857 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6858 }
6859 else
6860 bfd_elf32_swap_reloc_out
6861 (output_bfd, &outrel[0],
6862 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6863
6864 /* We've now added another relocation. */
6865 ++sreloc->reloc_count;
6866
6867 /* Make sure the output section is writable. The dynamic linker
6868 will be writing to it. */
6869 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6870 |= SHF_WRITE;
6871
6872 /* On IRIX5, make an entry of compact relocation info. */
6873 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6874 {
6875 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6876 bfd_byte *cr;
6877
6878 if (scpt)
6879 {
6880 Elf32_crinfo cptrel;
6881
6882 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6883 cptrel.vaddr = (rel->r_offset
6884 + input_section->output_section->vma
6885 + input_section->output_offset);
6886 if (r_type == R_MIPS_REL32)
6887 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6888 else
6889 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6890 mips_elf_set_cr_dist2to (cptrel, 0);
6891 cptrel.konst = *addendp;
6892
6893 cr = (scpt->contents
6894 + sizeof (Elf32_External_compact_rel));
6895 mips_elf_set_cr_relvaddr (cptrel, 0);
6896 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6897 ((Elf32_External_crinfo *) cr
6898 + scpt->reloc_count));
6899 ++scpt->reloc_count;
6900 }
6901 }
6902
6903 /* If we've written this relocation for a readonly section,
6904 we need to set DF_TEXTREL again, so that we do not delete the
6905 DT_TEXTREL tag. */
6906 if (MIPS_ELF_READONLY_SECTION (input_section))
6907 info->flags |= DF_TEXTREL;
6908
6909 return TRUE;
6910 }
6911 \f
6912 /* Return the MACH for a MIPS e_flags value. */
6913
6914 unsigned long
6915 _bfd_elf_mips_mach (flagword flags)
6916 {
6917 switch (flags & EF_MIPS_MACH)
6918 {
6919 case E_MIPS_MACH_3900:
6920 return bfd_mach_mips3900;
6921
6922 case E_MIPS_MACH_4010:
6923 return bfd_mach_mips4010;
6924
6925 case E_MIPS_MACH_4100:
6926 return bfd_mach_mips4100;
6927
6928 case E_MIPS_MACH_4111:
6929 return bfd_mach_mips4111;
6930
6931 case E_MIPS_MACH_4120:
6932 return bfd_mach_mips4120;
6933
6934 case E_MIPS_MACH_4650:
6935 return bfd_mach_mips4650;
6936
6937 case E_MIPS_MACH_5400:
6938 return bfd_mach_mips5400;
6939
6940 case E_MIPS_MACH_5500:
6941 return bfd_mach_mips5500;
6942
6943 case E_MIPS_MACH_5900:
6944 return bfd_mach_mips5900;
6945
6946 case E_MIPS_MACH_9000:
6947 return bfd_mach_mips9000;
6948
6949 case E_MIPS_MACH_SB1:
6950 return bfd_mach_mips_sb1;
6951
6952 case E_MIPS_MACH_LS2E:
6953 return bfd_mach_mips_loongson_2e;
6954
6955 case E_MIPS_MACH_LS2F:
6956 return bfd_mach_mips_loongson_2f;
6957
6958 case E_MIPS_MACH_GS464:
6959 return bfd_mach_mips_gs464;
6960
6961 case E_MIPS_MACH_GS464E:
6962 return bfd_mach_mips_gs464e;
6963
6964 case E_MIPS_MACH_GS264E:
6965 return bfd_mach_mips_gs264e;
6966
6967 case E_MIPS_MACH_OCTEON3:
6968 return bfd_mach_mips_octeon3;
6969
6970 case E_MIPS_MACH_OCTEON2:
6971 return bfd_mach_mips_octeon2;
6972
6973 case E_MIPS_MACH_OCTEON:
6974 return bfd_mach_mips_octeon;
6975
6976 case E_MIPS_MACH_XLR:
6977 return bfd_mach_mips_xlr;
6978
6979 case E_MIPS_MACH_IAMR2:
6980 return bfd_mach_mips_interaptiv_mr2;
6981
6982 default:
6983 switch (flags & EF_MIPS_ARCH)
6984 {
6985 default:
6986 case E_MIPS_ARCH_1:
6987 return bfd_mach_mips3000;
6988
6989 case E_MIPS_ARCH_2:
6990 return bfd_mach_mips6000;
6991
6992 case E_MIPS_ARCH_3:
6993 return bfd_mach_mips4000;
6994
6995 case E_MIPS_ARCH_4:
6996 return bfd_mach_mips8000;
6997
6998 case E_MIPS_ARCH_5:
6999 return bfd_mach_mips5;
7000
7001 case E_MIPS_ARCH_32:
7002 return bfd_mach_mipsisa32;
7003
7004 case E_MIPS_ARCH_64:
7005 return bfd_mach_mipsisa64;
7006
7007 case E_MIPS_ARCH_32R2:
7008 return bfd_mach_mipsisa32r2;
7009
7010 case E_MIPS_ARCH_64R2:
7011 return bfd_mach_mipsisa64r2;
7012
7013 case E_MIPS_ARCH_32R6:
7014 return bfd_mach_mipsisa32r6;
7015
7016 case E_MIPS_ARCH_64R6:
7017 return bfd_mach_mipsisa64r6;
7018 }
7019 }
7020
7021 return 0;
7022 }
7023
7024 /* Return printable name for ABI. */
7025
7026 static INLINE char *
7027 elf_mips_abi_name (bfd *abfd)
7028 {
7029 flagword flags;
7030
7031 flags = elf_elfheader (abfd)->e_flags;
7032 switch (flags & EF_MIPS_ABI)
7033 {
7034 case 0:
7035 if (ABI_N32_P (abfd))
7036 return "N32";
7037 else if (ABI_64_P (abfd))
7038 return "64";
7039 else
7040 return "none";
7041 case E_MIPS_ABI_O32:
7042 return "O32";
7043 case E_MIPS_ABI_O64:
7044 return "O64";
7045 case E_MIPS_ABI_EABI32:
7046 return "EABI32";
7047 case E_MIPS_ABI_EABI64:
7048 return "EABI64";
7049 default:
7050 return "unknown abi";
7051 }
7052 }
7053 \f
7054 /* MIPS ELF uses two common sections. One is the usual one, and the
7055 other is for small objects. All the small objects are kept
7056 together, and then referenced via the gp pointer, which yields
7057 faster assembler code. This is what we use for the small common
7058 section. This approach is copied from ecoff.c. */
7059 static asection mips_elf_scom_section;
7060 static asymbol mips_elf_scom_symbol;
7061 static asymbol *mips_elf_scom_symbol_ptr;
7062
7063 /* MIPS ELF also uses an acommon section, which represents an
7064 allocated common symbol which may be overridden by a
7065 definition in a shared library. */
7066 static asection mips_elf_acom_section;
7067 static asymbol mips_elf_acom_symbol;
7068 static asymbol *mips_elf_acom_symbol_ptr;
7069
7070 /* This is used for both the 32-bit and the 64-bit ABI. */
7071
7072 void
7073 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
7074 {
7075 elf_symbol_type *elfsym;
7076
7077 /* Handle the special MIPS section numbers that a symbol may use. */
7078 elfsym = (elf_symbol_type *) asym;
7079 switch (elfsym->internal_elf_sym.st_shndx)
7080 {
7081 case SHN_MIPS_ACOMMON:
7082 /* This section is used in a dynamically linked executable file.
7083 It is an allocated common section. The dynamic linker can
7084 either resolve these symbols to something in a shared
7085 library, or it can just leave them here. For our purposes,
7086 we can consider these symbols to be in a new section. */
7087 if (mips_elf_acom_section.name == NULL)
7088 {
7089 /* Initialize the acommon section. */
7090 mips_elf_acom_section.name = ".acommon";
7091 mips_elf_acom_section.flags = SEC_ALLOC;
7092 mips_elf_acom_section.output_section = &mips_elf_acom_section;
7093 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
7094 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
7095 mips_elf_acom_symbol.name = ".acommon";
7096 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
7097 mips_elf_acom_symbol.section = &mips_elf_acom_section;
7098 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
7099 }
7100 asym->section = &mips_elf_acom_section;
7101 break;
7102
7103 case SHN_COMMON:
7104 /* Common symbols less than the GP size are automatically
7105 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
7106 if (asym->value > elf_gp_size (abfd)
7107 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
7108 || IRIX_COMPAT (abfd) == ict_irix6)
7109 break;
7110 /* Fall through. */
7111 case SHN_MIPS_SCOMMON:
7112 if (mips_elf_scom_section.name == NULL)
7113 {
7114 /* Initialize the small common section. */
7115 mips_elf_scom_section.name = ".scommon";
7116 mips_elf_scom_section.flags = SEC_IS_COMMON | SEC_SMALL_DATA;
7117 mips_elf_scom_section.output_section = &mips_elf_scom_section;
7118 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
7119 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
7120 mips_elf_scom_symbol.name = ".scommon";
7121 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
7122 mips_elf_scom_symbol.section = &mips_elf_scom_section;
7123 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
7124 }
7125 asym->section = &mips_elf_scom_section;
7126 asym->value = elfsym->internal_elf_sym.st_size;
7127 break;
7128
7129 case SHN_MIPS_SUNDEFINED:
7130 asym->section = bfd_und_section_ptr;
7131 break;
7132
7133 case SHN_MIPS_TEXT:
7134 {
7135 asection *section = bfd_get_section_by_name (abfd, ".text");
7136
7137 if (section != NULL)
7138 {
7139 asym->section = section;
7140 /* MIPS_TEXT is a bit special, the address is not an offset
7141 to the base of the .text section. So subtract the section
7142 base address to make it an offset. */
7143 asym->value -= section->vma;
7144 }
7145 }
7146 break;
7147
7148 case SHN_MIPS_DATA:
7149 {
7150 asection *section = bfd_get_section_by_name (abfd, ".data");
7151
7152 if (section != NULL)
7153 {
7154 asym->section = section;
7155 /* MIPS_DATA is a bit special, the address is not an offset
7156 to the base of the .data section. So subtract the section
7157 base address to make it an offset. */
7158 asym->value -= section->vma;
7159 }
7160 }
7161 break;
7162 }
7163
7164 /* If this is an odd-valued function symbol, assume it's a MIPS16
7165 or microMIPS one. */
7166 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7167 && (asym->value & 1) != 0)
7168 {
7169 asym->value--;
7170 if (MICROMIPS_P (abfd))
7171 elfsym->internal_elf_sym.st_other
7172 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7173 else
7174 elfsym->internal_elf_sym.st_other
7175 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
7176 }
7177 }
7178 \f
7179 /* Implement elf_backend_eh_frame_address_size. This differs from
7180 the default in the way it handles EABI64.
7181
7182 EABI64 was originally specified as an LP64 ABI, and that is what
7183 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7184 historically accepted the combination of -mabi=eabi and -mlong32,
7185 and this ILP32 variation has become semi-official over time.
7186 Both forms use elf32 and have pointer-sized FDE addresses.
7187
7188 If an EABI object was generated by GCC 4.0 or above, it will have
7189 an empty .gcc_compiled_longXX section, where XX is the size of longs
7190 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7191 have no special marking to distinguish them from LP64 objects.
7192
7193 We don't want users of the official LP64 ABI to be punished for the
7194 existence of the ILP32 variant, but at the same time, we don't want
7195 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7196 We therefore take the following approach:
7197
7198 - If ABFD contains a .gcc_compiled_longXX section, use it to
7199 determine the pointer size.
7200
7201 - Otherwise check the type of the first relocation. Assume that
7202 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
7203
7204 - Otherwise punt.
7205
7206 The second check is enough to detect LP64 objects generated by pre-4.0
7207 compilers because, in the kind of output generated by those compilers,
7208 the first relocation will be associated with either a CIE personality
7209 routine or an FDE start address. Furthermore, the compilers never
7210 used a special (non-pointer) encoding for this ABI.
7211
7212 Checking the relocation type should also be safe because there is no
7213 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7214 did so. */
7215
7216 unsigned int
7217 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
7218 {
7219 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7220 return 8;
7221 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7222 {
7223 bfd_boolean long32_p, long64_p;
7224
7225 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7226 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7227 if (long32_p && long64_p)
7228 return 0;
7229 if (long32_p)
7230 return 4;
7231 if (long64_p)
7232 return 8;
7233
7234 if (sec->reloc_count > 0
7235 && elf_section_data (sec)->relocs != NULL
7236 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7237 == R_MIPS_64))
7238 return 8;
7239
7240 return 0;
7241 }
7242 return 4;
7243 }
7244 \f
7245 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7246 relocations against two unnamed section symbols to resolve to the
7247 same address. For example, if we have code like:
7248
7249 lw $4,%got_disp(.data)($gp)
7250 lw $25,%got_disp(.text)($gp)
7251 jalr $25
7252
7253 then the linker will resolve both relocations to .data and the program
7254 will jump there rather than to .text.
7255
7256 We can work around this problem by giving names to local section symbols.
7257 This is also what the MIPSpro tools do. */
7258
7259 bfd_boolean
7260 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7261 {
7262 return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
7263 }
7264 \f
7265 /* Work over a section just before writing it out. This routine is
7266 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7267 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7268 a better way. */
7269
7270 bfd_boolean
7271 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
7272 {
7273 if (hdr->sh_type == SHT_MIPS_REGINFO
7274 && hdr->sh_size > 0)
7275 {
7276 bfd_byte buf[4];
7277
7278 BFD_ASSERT (hdr->contents == NULL);
7279
7280 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7281 {
7282 _bfd_error_handler
7283 (_("%pB: incorrect `.reginfo' section size; "
7284 "expected %" PRIu64 ", got %" PRIu64),
7285 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7286 (uint64_t) hdr->sh_size);
7287 bfd_set_error (bfd_error_bad_value);
7288 return FALSE;
7289 }
7290
7291 if (bfd_seek (abfd,
7292 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7293 SEEK_SET) != 0)
7294 return FALSE;
7295 H_PUT_32 (abfd, elf_gp (abfd), buf);
7296 if (bfd_bwrite (buf, 4, abfd) != 4)
7297 return FALSE;
7298 }
7299
7300 if (hdr->sh_type == SHT_MIPS_OPTIONS
7301 && hdr->bfd_section != NULL
7302 && mips_elf_section_data (hdr->bfd_section) != NULL
7303 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
7304 {
7305 bfd_byte *contents, *l, *lend;
7306
7307 /* We stored the section contents in the tdata field in the
7308 set_section_contents routine. We save the section contents
7309 so that we don't have to read them again.
7310 At this point we know that elf_gp is set, so we can look
7311 through the section contents to see if there is an
7312 ODK_REGINFO structure. */
7313
7314 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
7315 l = contents;
7316 lend = contents + hdr->sh_size;
7317 while (l + sizeof (Elf_External_Options) <= lend)
7318 {
7319 Elf_Internal_Options intopt;
7320
7321 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7322 &intopt);
7323 if (intopt.size < sizeof (Elf_External_Options))
7324 {
7325 _bfd_error_handler
7326 /* xgettext:c-format */
7327 (_("%pB: warning: bad `%s' option size %u smaller than"
7328 " its header"),
7329 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7330 break;
7331 }
7332 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7333 {
7334 bfd_byte buf[8];
7335
7336 if (bfd_seek (abfd,
7337 (hdr->sh_offset
7338 + (l - contents)
7339 + sizeof (Elf_External_Options)
7340 + (sizeof (Elf64_External_RegInfo) - 8)),
7341 SEEK_SET) != 0)
7342 return FALSE;
7343 H_PUT_64 (abfd, elf_gp (abfd), buf);
7344 if (bfd_bwrite (buf, 8, abfd) != 8)
7345 return FALSE;
7346 }
7347 else if (intopt.kind == ODK_REGINFO)
7348 {
7349 bfd_byte buf[4];
7350
7351 if (bfd_seek (abfd,
7352 (hdr->sh_offset
7353 + (l - contents)
7354 + sizeof (Elf_External_Options)
7355 + (sizeof (Elf32_External_RegInfo) - 4)),
7356 SEEK_SET) != 0)
7357 return FALSE;
7358 H_PUT_32 (abfd, elf_gp (abfd), buf);
7359 if (bfd_bwrite (buf, 4, abfd) != 4)
7360 return FALSE;
7361 }
7362 l += intopt.size;
7363 }
7364 }
7365
7366 if (hdr->bfd_section != NULL)
7367 {
7368 const char *name = bfd_section_name (hdr->bfd_section);
7369
7370 /* .sbss is not handled specially here because the GNU/Linux
7371 prelinker can convert .sbss from NOBITS to PROGBITS and
7372 changing it back to NOBITS breaks the binary. The entry in
7373 _bfd_mips_elf_special_sections will ensure the correct flags
7374 are set on .sbss if BFD creates it without reading it from an
7375 input file, and without special handling here the flags set
7376 on it in an input file will be followed. */
7377 if (strcmp (name, ".sdata") == 0
7378 || strcmp (name, ".lit8") == 0
7379 || strcmp (name, ".lit4") == 0)
7380 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7381 else if (strcmp (name, ".srdata") == 0)
7382 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7383 else if (strcmp (name, ".compact_rel") == 0)
7384 hdr->sh_flags = 0;
7385 else if (strcmp (name, ".rtproc") == 0)
7386 {
7387 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7388 {
7389 unsigned int adjust;
7390
7391 adjust = hdr->sh_size % hdr->sh_addralign;
7392 if (adjust != 0)
7393 hdr->sh_size += hdr->sh_addralign - adjust;
7394 }
7395 }
7396 }
7397
7398 return TRUE;
7399 }
7400
7401 /* Handle a MIPS specific section when reading an object file. This
7402 is called when elfcode.h finds a section with an unknown type.
7403 This routine supports both the 32-bit and 64-bit ELF ABI. */
7404
7405 bfd_boolean
7406 _bfd_mips_elf_section_from_shdr (bfd *abfd,
7407 Elf_Internal_Shdr *hdr,
7408 const char *name,
7409 int shindex)
7410 {
7411 flagword flags = 0;
7412
7413 /* There ought to be a place to keep ELF backend specific flags, but
7414 at the moment there isn't one. We just keep track of the
7415 sections by their name, instead. Fortunately, the ABI gives
7416 suggested names for all the MIPS specific sections, so we will
7417 probably get away with this. */
7418 switch (hdr->sh_type)
7419 {
7420 case SHT_MIPS_LIBLIST:
7421 if (strcmp (name, ".liblist") != 0)
7422 return FALSE;
7423 break;
7424 case SHT_MIPS_MSYM:
7425 if (strcmp (name, ".msym") != 0)
7426 return FALSE;
7427 break;
7428 case SHT_MIPS_CONFLICT:
7429 if (strcmp (name, ".conflict") != 0)
7430 return FALSE;
7431 break;
7432 case SHT_MIPS_GPTAB:
7433 if (! CONST_STRNEQ (name, ".gptab."))
7434 return FALSE;
7435 break;
7436 case SHT_MIPS_UCODE:
7437 if (strcmp (name, ".ucode") != 0)
7438 return FALSE;
7439 break;
7440 case SHT_MIPS_DEBUG:
7441 if (strcmp (name, ".mdebug") != 0)
7442 return FALSE;
7443 flags = SEC_DEBUGGING;
7444 break;
7445 case SHT_MIPS_REGINFO:
7446 if (strcmp (name, ".reginfo") != 0
7447 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7448 return FALSE;
7449 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7450 break;
7451 case SHT_MIPS_IFACE:
7452 if (strcmp (name, ".MIPS.interfaces") != 0)
7453 return FALSE;
7454 break;
7455 case SHT_MIPS_CONTENT:
7456 if (! CONST_STRNEQ (name, ".MIPS.content"))
7457 return FALSE;
7458 break;
7459 case SHT_MIPS_OPTIONS:
7460 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7461 return FALSE;
7462 break;
7463 case SHT_MIPS_ABIFLAGS:
7464 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7465 return FALSE;
7466 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7467 break;
7468 case SHT_MIPS_DWARF:
7469 if (! CONST_STRNEQ (name, ".debug_")
7470 && ! CONST_STRNEQ (name, ".zdebug_"))
7471 return FALSE;
7472 break;
7473 case SHT_MIPS_SYMBOL_LIB:
7474 if (strcmp (name, ".MIPS.symlib") != 0)
7475 return FALSE;
7476 break;
7477 case SHT_MIPS_EVENTS:
7478 if (! CONST_STRNEQ (name, ".MIPS.events")
7479 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
7480 return FALSE;
7481 break;
7482 case SHT_MIPS_XHASH:
7483 if (strcmp (name, ".MIPS.xhash") != 0)
7484 return FALSE;
7485 default:
7486 break;
7487 }
7488
7489 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7490 return FALSE;
7491
7492 if (hdr->sh_flags & SHF_MIPS_GPREL)
7493 flags |= SEC_SMALL_DATA;
7494
7495 if (flags)
7496 {
7497 if (!bfd_set_section_flags (hdr->bfd_section,
7498 (bfd_section_flags (hdr->bfd_section)
7499 | flags)))
7500 return FALSE;
7501 }
7502
7503 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7504 {
7505 Elf_External_ABIFlags_v0 ext;
7506
7507 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7508 &ext, 0, sizeof ext))
7509 return FALSE;
7510 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7511 &mips_elf_tdata (abfd)->abiflags);
7512 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7513 return FALSE;
7514 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
7515 }
7516
7517 /* FIXME: We should record sh_info for a .gptab section. */
7518
7519 /* For a .reginfo section, set the gp value in the tdata information
7520 from the contents of this section. We need the gp value while
7521 processing relocs, so we just get it now. The .reginfo section
7522 is not used in the 64-bit MIPS ELF ABI. */
7523 if (hdr->sh_type == SHT_MIPS_REGINFO)
7524 {
7525 Elf32_External_RegInfo ext;
7526 Elf32_RegInfo s;
7527
7528 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7529 &ext, 0, sizeof ext))
7530 return FALSE;
7531 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7532 elf_gp (abfd) = s.ri_gp_value;
7533 }
7534
7535 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7536 set the gp value based on what we find. We may see both
7537 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7538 they should agree. */
7539 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7540 {
7541 bfd_byte *contents, *l, *lend;
7542
7543 contents = bfd_malloc (hdr->sh_size);
7544 if (contents == NULL)
7545 return FALSE;
7546 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
7547 0, hdr->sh_size))
7548 {
7549 free (contents);
7550 return FALSE;
7551 }
7552 l = contents;
7553 lend = contents + hdr->sh_size;
7554 while (l + sizeof (Elf_External_Options) <= lend)
7555 {
7556 Elf_Internal_Options intopt;
7557
7558 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7559 &intopt);
7560 if (intopt.size < sizeof (Elf_External_Options))
7561 {
7562 _bfd_error_handler
7563 /* xgettext:c-format */
7564 (_("%pB: warning: bad `%s' option size %u smaller than"
7565 " its header"),
7566 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7567 break;
7568 }
7569 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7570 {
7571 Elf64_Internal_RegInfo intreg;
7572
7573 bfd_mips_elf64_swap_reginfo_in
7574 (abfd,
7575 ((Elf64_External_RegInfo *)
7576 (l + sizeof (Elf_External_Options))),
7577 &intreg);
7578 elf_gp (abfd) = intreg.ri_gp_value;
7579 }
7580 else if (intopt.kind == ODK_REGINFO)
7581 {
7582 Elf32_RegInfo intreg;
7583
7584 bfd_mips_elf32_swap_reginfo_in
7585 (abfd,
7586 ((Elf32_External_RegInfo *)
7587 (l + sizeof (Elf_External_Options))),
7588 &intreg);
7589 elf_gp (abfd) = intreg.ri_gp_value;
7590 }
7591 l += intopt.size;
7592 }
7593 free (contents);
7594 }
7595
7596 return TRUE;
7597 }
7598
7599 /* Set the correct type for a MIPS ELF section. We do this by the
7600 section name, which is a hack, but ought to work. This routine is
7601 used by both the 32-bit and the 64-bit ABI. */
7602
7603 bfd_boolean
7604 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7605 {
7606 const char *name = bfd_section_name (sec);
7607
7608 if (strcmp (name, ".liblist") == 0)
7609 {
7610 hdr->sh_type = SHT_MIPS_LIBLIST;
7611 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7612 /* The sh_link field is set in final_write_processing. */
7613 }
7614 else if (strcmp (name, ".conflict") == 0)
7615 hdr->sh_type = SHT_MIPS_CONFLICT;
7616 else if (CONST_STRNEQ (name, ".gptab."))
7617 {
7618 hdr->sh_type = SHT_MIPS_GPTAB;
7619 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7620 /* The sh_info field is set in final_write_processing. */
7621 }
7622 else if (strcmp (name, ".ucode") == 0)
7623 hdr->sh_type = SHT_MIPS_UCODE;
7624 else if (strcmp (name, ".mdebug") == 0)
7625 {
7626 hdr->sh_type = SHT_MIPS_DEBUG;
7627 /* In a shared object on IRIX 5.3, the .mdebug section has an
7628 entsize of 0. FIXME: Does this matter? */
7629 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7630 hdr->sh_entsize = 0;
7631 else
7632 hdr->sh_entsize = 1;
7633 }
7634 else if (strcmp (name, ".reginfo") == 0)
7635 {
7636 hdr->sh_type = SHT_MIPS_REGINFO;
7637 /* In a shared object on IRIX 5.3, the .reginfo section has an
7638 entsize of 0x18. FIXME: Does this matter? */
7639 if (SGI_COMPAT (abfd))
7640 {
7641 if ((abfd->flags & DYNAMIC) != 0)
7642 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7643 else
7644 hdr->sh_entsize = 1;
7645 }
7646 else
7647 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7648 }
7649 else if (SGI_COMPAT (abfd)
7650 && (strcmp (name, ".hash") == 0
7651 || strcmp (name, ".dynamic") == 0
7652 || strcmp (name, ".dynstr") == 0))
7653 {
7654 if (SGI_COMPAT (abfd))
7655 hdr->sh_entsize = 0;
7656 #if 0
7657 /* This isn't how the IRIX6 linker behaves. */
7658 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7659 #endif
7660 }
7661 else if (strcmp (name, ".got") == 0
7662 || strcmp (name, ".srdata") == 0
7663 || strcmp (name, ".sdata") == 0
7664 || strcmp (name, ".sbss") == 0
7665 || strcmp (name, ".lit4") == 0
7666 || strcmp (name, ".lit8") == 0)
7667 hdr->sh_flags |= SHF_MIPS_GPREL;
7668 else if (strcmp (name, ".MIPS.interfaces") == 0)
7669 {
7670 hdr->sh_type = SHT_MIPS_IFACE;
7671 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7672 }
7673 else if (CONST_STRNEQ (name, ".MIPS.content"))
7674 {
7675 hdr->sh_type = SHT_MIPS_CONTENT;
7676 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7677 /* The sh_info field is set in final_write_processing. */
7678 }
7679 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7680 {
7681 hdr->sh_type = SHT_MIPS_OPTIONS;
7682 hdr->sh_entsize = 1;
7683 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7684 }
7685 else if (CONST_STRNEQ (name, ".MIPS.abiflags"))
7686 {
7687 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7688 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7689 }
7690 else if (CONST_STRNEQ (name, ".debug_")
7691 || CONST_STRNEQ (name, ".zdebug_"))
7692 {
7693 hdr->sh_type = SHT_MIPS_DWARF;
7694
7695 /* Irix facilities such as libexc expect a single .debug_frame
7696 per executable, the system ones have NOSTRIP set and the linker
7697 doesn't merge sections with different flags so ... */
7698 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7699 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7700 }
7701 else if (strcmp (name, ".MIPS.symlib") == 0)
7702 {
7703 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7704 /* The sh_link and sh_info fields are set in
7705 final_write_processing. */
7706 }
7707 else if (CONST_STRNEQ (name, ".MIPS.events")
7708 || CONST_STRNEQ (name, ".MIPS.post_rel"))
7709 {
7710 hdr->sh_type = SHT_MIPS_EVENTS;
7711 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7712 /* The sh_link field is set in final_write_processing. */
7713 }
7714 else if (strcmp (name, ".msym") == 0)
7715 {
7716 hdr->sh_type = SHT_MIPS_MSYM;
7717 hdr->sh_flags |= SHF_ALLOC;
7718 hdr->sh_entsize = 8;
7719 }
7720 else if (strcmp (name, ".MIPS.xhash") == 0)
7721 {
7722 hdr->sh_type = SHT_MIPS_XHASH;
7723 hdr->sh_flags |= SHF_ALLOC;
7724 hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7725 }
7726
7727 /* The generic elf_fake_sections will set up REL_HDR using the default
7728 kind of relocations. We used to set up a second header for the
7729 non-default kind of relocations here, but only NewABI would use
7730 these, and the IRIX ld doesn't like resulting empty RELA sections.
7731 Thus we create those header only on demand now. */
7732
7733 return TRUE;
7734 }
7735
7736 /* Given a BFD section, try to locate the corresponding ELF section
7737 index. This is used by both the 32-bit and the 64-bit ABI.
7738 Actually, it's not clear to me that the 64-bit ABI supports these,
7739 but for non-PIC objects we will certainly want support for at least
7740 the .scommon section. */
7741
7742 bfd_boolean
7743 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7744 asection *sec, int *retval)
7745 {
7746 if (strcmp (bfd_section_name (sec), ".scommon") == 0)
7747 {
7748 *retval = SHN_MIPS_SCOMMON;
7749 return TRUE;
7750 }
7751 if (strcmp (bfd_section_name (sec), ".acommon") == 0)
7752 {
7753 *retval = SHN_MIPS_ACOMMON;
7754 return TRUE;
7755 }
7756 return FALSE;
7757 }
7758 \f
7759 /* Hook called by the linker routine which adds symbols from an object
7760 file. We must handle the special MIPS section numbers here. */
7761
7762 bfd_boolean
7763 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7764 Elf_Internal_Sym *sym, const char **namep,
7765 flagword *flagsp ATTRIBUTE_UNUSED,
7766 asection **secp, bfd_vma *valp)
7767 {
7768 if (SGI_COMPAT (abfd)
7769 && (abfd->flags & DYNAMIC) != 0
7770 && strcmp (*namep, "_rld_new_interface") == 0)
7771 {
7772 /* Skip IRIX5 rld entry name. */
7773 *namep = NULL;
7774 return TRUE;
7775 }
7776
7777 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7778 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7779 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7780 a magic symbol resolved by the linker, we ignore this bogus definition
7781 of _gp_disp. New ABI objects do not suffer from this problem so this
7782 is not done for them. */
7783 if (!NEWABI_P(abfd)
7784 && (sym->st_shndx == SHN_ABS)
7785 && (strcmp (*namep, "_gp_disp") == 0))
7786 {
7787 *namep = NULL;
7788 return TRUE;
7789 }
7790
7791 switch (sym->st_shndx)
7792 {
7793 case SHN_COMMON:
7794 /* Common symbols less than the GP size are automatically
7795 treated as SHN_MIPS_SCOMMON symbols. */
7796 if (sym->st_size > elf_gp_size (abfd)
7797 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7798 || IRIX_COMPAT (abfd) == ict_irix6)
7799 break;
7800 /* Fall through. */
7801 case SHN_MIPS_SCOMMON:
7802 *secp = bfd_make_section_old_way (abfd, ".scommon");
7803 (*secp)->flags |= SEC_IS_COMMON;
7804 *valp = sym->st_size;
7805 break;
7806
7807 case SHN_MIPS_TEXT:
7808 /* This section is used in a shared object. */
7809 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7810 {
7811 asymbol *elf_text_symbol;
7812 asection *elf_text_section;
7813 size_t amt = sizeof (asection);
7814
7815 elf_text_section = bfd_zalloc (abfd, amt);
7816 if (elf_text_section == NULL)
7817 return FALSE;
7818
7819 amt = sizeof (asymbol);
7820 elf_text_symbol = bfd_zalloc (abfd, amt);
7821 if (elf_text_symbol == NULL)
7822 return FALSE;
7823
7824 /* Initialize the section. */
7825
7826 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7827 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7828
7829 elf_text_section->symbol = elf_text_symbol;
7830 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7831
7832 elf_text_section->name = ".text";
7833 elf_text_section->flags = SEC_NO_FLAGS;
7834 elf_text_section->output_section = NULL;
7835 elf_text_section->owner = abfd;
7836 elf_text_symbol->name = ".text";
7837 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7838 elf_text_symbol->section = elf_text_section;
7839 }
7840 /* This code used to do *secp = bfd_und_section_ptr if
7841 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7842 so I took it out. */
7843 *secp = mips_elf_tdata (abfd)->elf_text_section;
7844 break;
7845
7846 case SHN_MIPS_ACOMMON:
7847 /* Fall through. XXX Can we treat this as allocated data? */
7848 case SHN_MIPS_DATA:
7849 /* This section is used in a shared object. */
7850 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7851 {
7852 asymbol *elf_data_symbol;
7853 asection *elf_data_section;
7854 size_t amt = sizeof (asection);
7855
7856 elf_data_section = bfd_zalloc (abfd, amt);
7857 if (elf_data_section == NULL)
7858 return FALSE;
7859
7860 amt = sizeof (asymbol);
7861 elf_data_symbol = bfd_zalloc (abfd, amt);
7862 if (elf_data_symbol == NULL)
7863 return FALSE;
7864
7865 /* Initialize the section. */
7866
7867 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7868 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7869
7870 elf_data_section->symbol = elf_data_symbol;
7871 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7872
7873 elf_data_section->name = ".data";
7874 elf_data_section->flags = SEC_NO_FLAGS;
7875 elf_data_section->output_section = NULL;
7876 elf_data_section->owner = abfd;
7877 elf_data_symbol->name = ".data";
7878 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7879 elf_data_symbol->section = elf_data_section;
7880 }
7881 /* This code used to do *secp = bfd_und_section_ptr if
7882 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7883 so I took it out. */
7884 *secp = mips_elf_tdata (abfd)->elf_data_section;
7885 break;
7886
7887 case SHN_MIPS_SUNDEFINED:
7888 *secp = bfd_und_section_ptr;
7889 break;
7890 }
7891
7892 if (SGI_COMPAT (abfd)
7893 && ! bfd_link_pic (info)
7894 && info->output_bfd->xvec == abfd->xvec
7895 && strcmp (*namep, "__rld_obj_head") == 0)
7896 {
7897 struct elf_link_hash_entry *h;
7898 struct bfd_link_hash_entry *bh;
7899
7900 /* Mark __rld_obj_head as dynamic. */
7901 bh = NULL;
7902 if (! (_bfd_generic_link_add_one_symbol
7903 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7904 get_elf_backend_data (abfd)->collect, &bh)))
7905 return FALSE;
7906
7907 h = (struct elf_link_hash_entry *) bh;
7908 h->non_elf = 0;
7909 h->def_regular = 1;
7910 h->type = STT_OBJECT;
7911
7912 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7913 return FALSE;
7914
7915 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7916 mips_elf_hash_table (info)->rld_symbol = h;
7917 }
7918
7919 /* If this is a mips16 text symbol, add 1 to the value to make it
7920 odd. This will cause something like .word SYM to come up with
7921 the right value when it is loaded into the PC. */
7922 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7923 ++*valp;
7924
7925 return TRUE;
7926 }
7927
7928 /* This hook function is called before the linker writes out a global
7929 symbol. We mark symbols as small common if appropriate. This is
7930 also where we undo the increment of the value for a mips16 symbol. */
7931
7932 int
7933 _bfd_mips_elf_link_output_symbol_hook
7934 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7935 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7936 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7937 {
7938 /* If we see a common symbol, which implies a relocatable link, then
7939 if a symbol was small common in an input file, mark it as small
7940 common in the output file. */
7941 if (sym->st_shndx == SHN_COMMON
7942 && strcmp (input_sec->name, ".scommon") == 0)
7943 sym->st_shndx = SHN_MIPS_SCOMMON;
7944
7945 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7946 sym->st_value &= ~1;
7947
7948 return 1;
7949 }
7950 \f
7951 /* Functions for the dynamic linker. */
7952
7953 /* Create dynamic sections when linking against a dynamic object. */
7954
7955 bfd_boolean
7956 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7957 {
7958 struct elf_link_hash_entry *h;
7959 struct bfd_link_hash_entry *bh;
7960 flagword flags;
7961 register asection *s;
7962 const char * const *namep;
7963 struct mips_elf_link_hash_table *htab;
7964
7965 htab = mips_elf_hash_table (info);
7966 BFD_ASSERT (htab != NULL);
7967
7968 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7969 | SEC_LINKER_CREATED | SEC_READONLY);
7970
7971 /* The psABI requires a read-only .dynamic section, but the VxWorks
7972 EABI doesn't. */
7973 if (htab->root.target_os != is_vxworks)
7974 {
7975 s = bfd_get_linker_section (abfd, ".dynamic");
7976 if (s != NULL)
7977 {
7978 if (!bfd_set_section_flags (s, flags))
7979 return FALSE;
7980 }
7981 }
7982
7983 /* We need to create .got section. */
7984 if (!mips_elf_create_got_section (abfd, info))
7985 return FALSE;
7986
7987 if (! mips_elf_rel_dyn_section (info, TRUE))
7988 return FALSE;
7989
7990 /* Create .stub section. */
7991 s = bfd_make_section_anyway_with_flags (abfd,
7992 MIPS_ELF_STUB_SECTION_NAME (abfd),
7993 flags | SEC_CODE);
7994 if (s == NULL
7995 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7996 return FALSE;
7997 htab->sstubs = s;
7998
7999 if (!mips_elf_hash_table (info)->use_rld_obj_head
8000 && bfd_link_executable (info)
8001 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
8002 {
8003 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8004 flags &~ (flagword) SEC_READONLY);
8005 if (s == NULL
8006 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
8007 return FALSE;
8008 }
8009
8010 /* Create .MIPS.xhash section. */
8011 if (info->emit_gnu_hash)
8012 s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8013 flags | SEC_READONLY);
8014
8015 /* On IRIX5, we adjust add some additional symbols and change the
8016 alignments of several sections. There is no ABI documentation
8017 indicating that this is necessary on IRIX6, nor any evidence that
8018 the linker takes such action. */
8019 if (IRIX_COMPAT (abfd) == ict_irix5)
8020 {
8021 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8022 {
8023 bh = NULL;
8024 if (! (_bfd_generic_link_add_one_symbol
8025 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8026 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
8027 return FALSE;
8028
8029 h = (struct elf_link_hash_entry *) bh;
8030 h->mark = 1;
8031 h->non_elf = 0;
8032 h->def_regular = 1;
8033 h->type = STT_SECTION;
8034
8035 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8036 return FALSE;
8037 }
8038
8039 /* We need to create a .compact_rel section. */
8040 if (SGI_COMPAT (abfd))
8041 {
8042 if (!mips_elf_create_compact_rel_section (abfd, info))
8043 return FALSE;
8044 }
8045
8046 /* Change alignments of some sections. */
8047 s = bfd_get_linker_section (abfd, ".hash");
8048 if (s != NULL)
8049 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8050
8051 s = bfd_get_linker_section (abfd, ".dynsym");
8052 if (s != NULL)
8053 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8054
8055 s = bfd_get_linker_section (abfd, ".dynstr");
8056 if (s != NULL)
8057 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8058
8059 /* ??? */
8060 s = bfd_get_section_by_name (abfd, ".reginfo");
8061 if (s != NULL)
8062 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8063
8064 s = bfd_get_linker_section (abfd, ".dynamic");
8065 if (s != NULL)
8066 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
8067 }
8068
8069 if (bfd_link_executable (info))
8070 {
8071 const char *name;
8072
8073 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8074 bh = NULL;
8075 if (!(_bfd_generic_link_add_one_symbol
8076 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8077 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
8078 return FALSE;
8079
8080 h = (struct elf_link_hash_entry *) bh;
8081 h->non_elf = 0;
8082 h->def_regular = 1;
8083 h->type = STT_SECTION;
8084
8085 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8086 return FALSE;
8087
8088 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8089 {
8090 /* __rld_map is a four byte word located in the .data section
8091 and is filled in by the rtld to contain a pointer to
8092 the _r_debug structure. Its symbol value will be set in
8093 _bfd_mips_elf_finish_dynamic_symbol. */
8094 s = bfd_get_linker_section (abfd, ".rld_map");
8095 BFD_ASSERT (s != NULL);
8096
8097 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8098 bh = NULL;
8099 if (!(_bfd_generic_link_add_one_symbol
8100 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
8101 get_elf_backend_data (abfd)->collect, &bh)))
8102 return FALSE;
8103
8104 h = (struct elf_link_hash_entry *) bh;
8105 h->non_elf = 0;
8106 h->def_regular = 1;
8107 h->type = STT_OBJECT;
8108
8109 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8110 return FALSE;
8111 mips_elf_hash_table (info)->rld_symbol = h;
8112 }
8113 }
8114
8115 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
8116 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
8117 if (!_bfd_elf_create_dynamic_sections (abfd, info))
8118 return FALSE;
8119
8120 /* Do the usual VxWorks handling. */
8121 if (htab->root.target_os == is_vxworks
8122 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8123 return FALSE;
8124
8125 return TRUE;
8126 }
8127 \f
8128 /* Return true if relocation REL against section SEC is a REL rather than
8129 RELA relocation. RELOCS is the first relocation in the section and
8130 ABFD is the bfd that contains SEC. */
8131
8132 static bfd_boolean
8133 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8134 const Elf_Internal_Rela *relocs,
8135 const Elf_Internal_Rela *rel)
8136 {
8137 Elf_Internal_Shdr *rel_hdr;
8138 const struct elf_backend_data *bed;
8139
8140 /* To determine which flavor of relocation this is, we depend on the
8141 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8142 rel_hdr = elf_section_data (sec)->rel.hdr;
8143 if (rel_hdr == NULL)
8144 return FALSE;
8145 bed = get_elf_backend_data (abfd);
8146 return ((size_t) (rel - relocs)
8147 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
8148 }
8149
8150 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8151 HOWTO is the relocation's howto and CONTENTS points to the contents
8152 of the section that REL is against. */
8153
8154 static bfd_vma
8155 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
8156 reloc_howto_type *howto, bfd_byte *contents)
8157 {
8158 bfd_byte *location;
8159 unsigned int r_type;
8160 bfd_vma addend;
8161 bfd_vma bytes;
8162
8163 r_type = ELF_R_TYPE (abfd, rel->r_info);
8164 location = contents + rel->r_offset;
8165
8166 /* Get the addend, which is stored in the input file. */
8167 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
8168 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
8169 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
8170
8171 addend = bytes & howto->src_mask;
8172
8173 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8174 accordingly. */
8175 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8176 addend <<= 1;
8177
8178 return addend;
8179 }
8180
8181 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
8182 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8183 and update *ADDEND with the final addend. Return true on success
8184 or false if the LO16 could not be found. RELEND is the exclusive
8185 upper bound on the relocations for REL's section. */
8186
8187 static bfd_boolean
8188 mips_elf_add_lo16_rel_addend (bfd *abfd,
8189 const Elf_Internal_Rela *rel,
8190 const Elf_Internal_Rela *relend,
8191 bfd_byte *contents, bfd_vma *addend)
8192 {
8193 unsigned int r_type, lo16_type;
8194 const Elf_Internal_Rela *lo16_relocation;
8195 reloc_howto_type *lo16_howto;
8196 bfd_vma l;
8197
8198 r_type = ELF_R_TYPE (abfd, rel->r_info);
8199 if (mips16_reloc_p (r_type))
8200 lo16_type = R_MIPS16_LO16;
8201 else if (micromips_reloc_p (r_type))
8202 lo16_type = R_MICROMIPS_LO16;
8203 else if (r_type == R_MIPS_PCHI16)
8204 lo16_type = R_MIPS_PCLO16;
8205 else
8206 lo16_type = R_MIPS_LO16;
8207
8208 /* The combined value is the sum of the HI16 addend, left-shifted by
8209 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8210 code does a `lui' of the HI16 value, and then an `addiu' of the
8211 LO16 value.)
8212
8213 Scan ahead to find a matching LO16 relocation.
8214
8215 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8216 be immediately following. However, for the IRIX6 ABI, the next
8217 relocation may be a composed relocation consisting of several
8218 relocations for the same address. In that case, the R_MIPS_LO16
8219 relocation may occur as one of these. We permit a similar
8220 extension in general, as that is useful for GCC.
8221
8222 In some cases GCC dead code elimination removes the LO16 but keeps
8223 the corresponding HI16. This is strictly speaking a violation of
8224 the ABI but not immediately harmful. */
8225 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8226 if (lo16_relocation == NULL)
8227 return FALSE;
8228
8229 /* Obtain the addend kept there. */
8230 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
8231 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
8232
8233 l <<= lo16_howto->rightshift;
8234 l = _bfd_mips_elf_sign_extend (l, 16);
8235
8236 *addend <<= 16;
8237 *addend += l;
8238 return TRUE;
8239 }
8240
8241 /* Try to read the contents of section SEC in bfd ABFD. Return true and
8242 store the contents in *CONTENTS on success. Assume that *CONTENTS
8243 already holds the contents if it is nonull on entry. */
8244
8245 static bfd_boolean
8246 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8247 {
8248 if (*contents)
8249 return TRUE;
8250
8251 /* Get cached copy if it exists. */
8252 if (elf_section_data (sec)->this_hdr.contents != NULL)
8253 {
8254 *contents = elf_section_data (sec)->this_hdr.contents;
8255 return TRUE;
8256 }
8257
8258 return bfd_malloc_and_get_section (abfd, sec, contents);
8259 }
8260
8261 /* Make a new PLT record to keep internal data. */
8262
8263 static struct plt_entry *
8264 mips_elf_make_plt_record (bfd *abfd)
8265 {
8266 struct plt_entry *entry;
8267
8268 entry = bfd_zalloc (abfd, sizeof (*entry));
8269 if (entry == NULL)
8270 return NULL;
8271
8272 entry->stub_offset = MINUS_ONE;
8273 entry->mips_offset = MINUS_ONE;
8274 entry->comp_offset = MINUS_ONE;
8275 entry->gotplt_index = MINUS_ONE;
8276 return entry;
8277 }
8278
8279 /* Define the special `__gnu_absolute_zero' symbol. We only need this
8280 for PIC code, as otherwise there is no load-time relocation involved
8281 and local GOT entries whose value is zero at static link time will
8282 retain their value at load time. */
8283
8284 static bfd_boolean
8285 mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8286 struct mips_elf_link_hash_table *htab,
8287 unsigned int r_type)
8288 {
8289 union
8290 {
8291 struct elf_link_hash_entry *eh;
8292 struct bfd_link_hash_entry *bh;
8293 }
8294 hzero;
8295
8296 BFD_ASSERT (!htab->use_absolute_zero);
8297 BFD_ASSERT (bfd_link_pic (info));
8298
8299 hzero.bh = NULL;
8300 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8301 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8302 NULL, FALSE, FALSE, &hzero.bh))
8303 return FALSE;
8304
8305 BFD_ASSERT (hzero.bh != NULL);
8306 hzero.eh->size = 0;
8307 hzero.eh->type = STT_NOTYPE;
8308 hzero.eh->other = STV_PROTECTED;
8309 hzero.eh->def_regular = 1;
8310 hzero.eh->non_elf = 0;
8311
8312 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, TRUE, r_type))
8313 return FALSE;
8314
8315 htab->use_absolute_zero = TRUE;
8316
8317 return TRUE;
8318 }
8319
8320 /* Look through the relocs for a section during the first phase, and
8321 allocate space in the global offset table and record the need for
8322 standard MIPS and compressed procedure linkage table entries. */
8323
8324 bfd_boolean
8325 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8326 asection *sec, const Elf_Internal_Rela *relocs)
8327 {
8328 const char *name;
8329 bfd *dynobj;
8330 Elf_Internal_Shdr *symtab_hdr;
8331 struct elf_link_hash_entry **sym_hashes;
8332 size_t extsymoff;
8333 const Elf_Internal_Rela *rel;
8334 const Elf_Internal_Rela *rel_end;
8335 asection *sreloc;
8336 const struct elf_backend_data *bed;
8337 struct mips_elf_link_hash_table *htab;
8338 bfd_byte *contents;
8339 bfd_vma addend;
8340 reloc_howto_type *howto;
8341
8342 if (bfd_link_relocatable (info))
8343 return TRUE;
8344
8345 htab = mips_elf_hash_table (info);
8346 BFD_ASSERT (htab != NULL);
8347
8348 dynobj = elf_hash_table (info)->dynobj;
8349 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8350 sym_hashes = elf_sym_hashes (abfd);
8351 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8352
8353 bed = get_elf_backend_data (abfd);
8354 rel_end = relocs + sec->reloc_count;
8355
8356 /* Check for the mips16 stub sections. */
8357
8358 name = bfd_section_name (sec);
8359 if (FN_STUB_P (name))
8360 {
8361 unsigned long r_symndx;
8362
8363 /* Look at the relocation information to figure out which symbol
8364 this is for. */
8365
8366 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8367 if (r_symndx == 0)
8368 {
8369 _bfd_error_handler
8370 /* xgettext:c-format */
8371 (_("%pB: warning: cannot determine the target function for"
8372 " stub section `%s'"),
8373 abfd, name);
8374 bfd_set_error (bfd_error_bad_value);
8375 return FALSE;
8376 }
8377
8378 if (r_symndx < extsymoff
8379 || sym_hashes[r_symndx - extsymoff] == NULL)
8380 {
8381 asection *o;
8382
8383 /* This stub is for a local symbol. This stub will only be
8384 needed if there is some relocation in this BFD, other
8385 than a 16 bit function call, which refers to this symbol. */
8386 for (o = abfd->sections; o != NULL; o = o->next)
8387 {
8388 Elf_Internal_Rela *sec_relocs;
8389 const Elf_Internal_Rela *r, *rend;
8390
8391 /* We can ignore stub sections when looking for relocs. */
8392 if ((o->flags & SEC_RELOC) == 0
8393 || o->reloc_count == 0
8394 || section_allows_mips16_refs_p (o))
8395 continue;
8396
8397 sec_relocs
8398 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8399 info->keep_memory);
8400 if (sec_relocs == NULL)
8401 return FALSE;
8402
8403 rend = sec_relocs + o->reloc_count;
8404 for (r = sec_relocs; r < rend; r++)
8405 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8406 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
8407 break;
8408
8409 if (elf_section_data (o)->relocs != sec_relocs)
8410 free (sec_relocs);
8411
8412 if (r < rend)
8413 break;
8414 }
8415
8416 if (o == NULL)
8417 {
8418 /* There is no non-call reloc for this stub, so we do
8419 not need it. Since this function is called before
8420 the linker maps input sections to output sections, we
8421 can easily discard it by setting the SEC_EXCLUDE
8422 flag. */
8423 sec->flags |= SEC_EXCLUDE;
8424 return TRUE;
8425 }
8426
8427 /* Record this stub in an array of local symbol stubs for
8428 this BFD. */
8429 if (mips_elf_tdata (abfd)->local_stubs == NULL)
8430 {
8431 unsigned long symcount;
8432 asection **n;
8433 bfd_size_type amt;
8434
8435 if (elf_bad_symtab (abfd))
8436 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8437 else
8438 symcount = symtab_hdr->sh_info;
8439 amt = symcount * sizeof (asection *);
8440 n = bfd_zalloc (abfd, amt);
8441 if (n == NULL)
8442 return FALSE;
8443 mips_elf_tdata (abfd)->local_stubs = n;
8444 }
8445
8446 sec->flags |= SEC_KEEP;
8447 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8448
8449 /* We don't need to set mips16_stubs_seen in this case.
8450 That flag is used to see whether we need to look through
8451 the global symbol table for stubs. We don't need to set
8452 it here, because we just have a local stub. */
8453 }
8454 else
8455 {
8456 struct mips_elf_link_hash_entry *h;
8457
8458 h = ((struct mips_elf_link_hash_entry *)
8459 sym_hashes[r_symndx - extsymoff]);
8460
8461 while (h->root.root.type == bfd_link_hash_indirect
8462 || h->root.root.type == bfd_link_hash_warning)
8463 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8464
8465 /* H is the symbol this stub is for. */
8466
8467 /* If we already have an appropriate stub for this function, we
8468 don't need another one, so we can discard this one. Since
8469 this function is called before the linker maps input sections
8470 to output sections, we can easily discard it by setting the
8471 SEC_EXCLUDE flag. */
8472 if (h->fn_stub != NULL)
8473 {
8474 sec->flags |= SEC_EXCLUDE;
8475 return TRUE;
8476 }
8477
8478 sec->flags |= SEC_KEEP;
8479 h->fn_stub = sec;
8480 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
8481 }
8482 }
8483 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8484 {
8485 unsigned long r_symndx;
8486 struct mips_elf_link_hash_entry *h;
8487 asection **loc;
8488
8489 /* Look at the relocation information to figure out which symbol
8490 this is for. */
8491
8492 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8493 if (r_symndx == 0)
8494 {
8495 _bfd_error_handler
8496 /* xgettext:c-format */
8497 (_("%pB: warning: cannot determine the target function for"
8498 " stub section `%s'"),
8499 abfd, name);
8500 bfd_set_error (bfd_error_bad_value);
8501 return FALSE;
8502 }
8503
8504 if (r_symndx < extsymoff
8505 || sym_hashes[r_symndx - extsymoff] == NULL)
8506 {
8507 asection *o;
8508
8509 /* This stub is for a local symbol. This stub will only be
8510 needed if there is some relocation (R_MIPS16_26) in this BFD
8511 that refers to this symbol. */
8512 for (o = abfd->sections; o != NULL; o = o->next)
8513 {
8514 Elf_Internal_Rela *sec_relocs;
8515 const Elf_Internal_Rela *r, *rend;
8516
8517 /* We can ignore stub sections when looking for relocs. */
8518 if ((o->flags & SEC_RELOC) == 0
8519 || o->reloc_count == 0
8520 || section_allows_mips16_refs_p (o))
8521 continue;
8522
8523 sec_relocs
8524 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8525 info->keep_memory);
8526 if (sec_relocs == NULL)
8527 return FALSE;
8528
8529 rend = sec_relocs + o->reloc_count;
8530 for (r = sec_relocs; r < rend; r++)
8531 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8532 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8533 break;
8534
8535 if (elf_section_data (o)->relocs != sec_relocs)
8536 free (sec_relocs);
8537
8538 if (r < rend)
8539 break;
8540 }
8541
8542 if (o == NULL)
8543 {
8544 /* There is no non-call reloc for this stub, so we do
8545 not need it. Since this function is called before
8546 the linker maps input sections to output sections, we
8547 can easily discard it by setting the SEC_EXCLUDE
8548 flag. */
8549 sec->flags |= SEC_EXCLUDE;
8550 return TRUE;
8551 }
8552
8553 /* Record this stub in an array of local symbol call_stubs for
8554 this BFD. */
8555 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8556 {
8557 unsigned long symcount;
8558 asection **n;
8559 bfd_size_type amt;
8560
8561 if (elf_bad_symtab (abfd))
8562 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8563 else
8564 symcount = symtab_hdr->sh_info;
8565 amt = symcount * sizeof (asection *);
8566 n = bfd_zalloc (abfd, amt);
8567 if (n == NULL)
8568 return FALSE;
8569 mips_elf_tdata (abfd)->local_call_stubs = n;
8570 }
8571
8572 sec->flags |= SEC_KEEP;
8573 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8574
8575 /* We don't need to set mips16_stubs_seen in this case.
8576 That flag is used to see whether we need to look through
8577 the global symbol table for stubs. We don't need to set
8578 it here, because we just have a local stub. */
8579 }
8580 else
8581 {
8582 h = ((struct mips_elf_link_hash_entry *)
8583 sym_hashes[r_symndx - extsymoff]);
8584
8585 /* H is the symbol this stub is for. */
8586
8587 if (CALL_FP_STUB_P (name))
8588 loc = &h->call_fp_stub;
8589 else
8590 loc = &h->call_stub;
8591
8592 /* If we already have an appropriate stub for this function, we
8593 don't need another one, so we can discard this one. Since
8594 this function is called before the linker maps input sections
8595 to output sections, we can easily discard it by setting the
8596 SEC_EXCLUDE flag. */
8597 if (*loc != NULL)
8598 {
8599 sec->flags |= SEC_EXCLUDE;
8600 return TRUE;
8601 }
8602
8603 sec->flags |= SEC_KEEP;
8604 *loc = sec;
8605 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
8606 }
8607 }
8608
8609 sreloc = NULL;
8610 contents = NULL;
8611 for (rel = relocs; rel < rel_end; ++rel)
8612 {
8613 unsigned long r_symndx;
8614 unsigned int r_type;
8615 struct elf_link_hash_entry *h;
8616 bfd_boolean can_make_dynamic_p;
8617 bfd_boolean call_reloc_p;
8618 bfd_boolean constrain_symbol_p;
8619
8620 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8621 r_type = ELF_R_TYPE (abfd, rel->r_info);
8622
8623 if (r_symndx < extsymoff)
8624 h = NULL;
8625 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8626 {
8627 _bfd_error_handler
8628 /* xgettext:c-format */
8629 (_("%pB: malformed reloc detected for section %s"),
8630 abfd, name);
8631 bfd_set_error (bfd_error_bad_value);
8632 return FALSE;
8633 }
8634 else
8635 {
8636 h = sym_hashes[r_symndx - extsymoff];
8637 if (h != NULL)
8638 {
8639 while (h->root.type == bfd_link_hash_indirect
8640 || h->root.type == bfd_link_hash_warning)
8641 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8642 }
8643 }
8644
8645 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8646 relocation into a dynamic one. */
8647 can_make_dynamic_p = FALSE;
8648
8649 /* Set CALL_RELOC_P to true if the relocation is for a call,
8650 and if pointer equality therefore doesn't matter. */
8651 call_reloc_p = FALSE;
8652
8653 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8654 into account when deciding how to define the symbol. */
8655 constrain_symbol_p = TRUE;
8656
8657 switch (r_type)
8658 {
8659 case R_MIPS_CALL16:
8660 case R_MIPS_CALL_HI16:
8661 case R_MIPS_CALL_LO16:
8662 case R_MIPS16_CALL16:
8663 case R_MICROMIPS_CALL16:
8664 case R_MICROMIPS_CALL_HI16:
8665 case R_MICROMIPS_CALL_LO16:
8666 call_reloc_p = TRUE;
8667 /* Fall through. */
8668
8669 case R_MIPS_GOT16:
8670 case R_MIPS_GOT_LO16:
8671 case R_MIPS_GOT_PAGE:
8672 case R_MIPS_GOT_DISP:
8673 case R_MIPS16_GOT16:
8674 case R_MICROMIPS_GOT16:
8675 case R_MICROMIPS_GOT_LO16:
8676 case R_MICROMIPS_GOT_PAGE:
8677 case R_MICROMIPS_GOT_DISP:
8678 /* If we have a symbol that will resolve to zero at static link
8679 time and it is used by a GOT relocation applied to code we
8680 cannot relax to an immediate zero load, then we will be using
8681 the special `__gnu_absolute_zero' symbol whose value is zero
8682 at dynamic load time. We ignore HI16-type GOT relocations at
8683 this stage, because their handling will depend entirely on
8684 the corresponding LO16-type GOT relocation. */
8685 if (!call_hi16_reloc_p (r_type)
8686 && h != NULL
8687 && bfd_link_pic (info)
8688 && !htab->use_absolute_zero
8689 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8690 {
8691 bfd_boolean rel_reloc;
8692
8693 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8694 return FALSE;
8695
8696 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8697 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8698
8699 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8700 FALSE))
8701 if (!mips_elf_define_absolute_zero (abfd, info, htab, r_type))
8702 return FALSE;
8703 }
8704
8705 /* Fall through. */
8706 case R_MIPS_GOT_HI16:
8707 case R_MIPS_GOT_OFST:
8708 case R_MIPS_TLS_GOTTPREL:
8709 case R_MIPS_TLS_GD:
8710 case R_MIPS_TLS_LDM:
8711 case R_MIPS16_TLS_GOTTPREL:
8712 case R_MIPS16_TLS_GD:
8713 case R_MIPS16_TLS_LDM:
8714 case R_MICROMIPS_GOT_HI16:
8715 case R_MICROMIPS_GOT_OFST:
8716 case R_MICROMIPS_TLS_GOTTPREL:
8717 case R_MICROMIPS_TLS_GD:
8718 case R_MICROMIPS_TLS_LDM:
8719 if (dynobj == NULL)
8720 elf_hash_table (info)->dynobj = dynobj = abfd;
8721 if (!mips_elf_create_got_section (dynobj, info))
8722 return FALSE;
8723 if (htab->root.target_os == is_vxworks
8724 && !bfd_link_pic (info))
8725 {
8726 _bfd_error_handler
8727 /* xgettext:c-format */
8728 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8729 abfd, (uint64_t) rel->r_offset);
8730 bfd_set_error (bfd_error_bad_value);
8731 return FALSE;
8732 }
8733 can_make_dynamic_p = TRUE;
8734 break;
8735
8736 case R_MIPS_NONE:
8737 case R_MIPS_JALR:
8738 case R_MICROMIPS_JALR:
8739 /* These relocations have empty fields and are purely there to
8740 provide link information. The symbol value doesn't matter. */
8741 constrain_symbol_p = FALSE;
8742 break;
8743
8744 case R_MIPS_GPREL16:
8745 case R_MIPS_GPREL32:
8746 case R_MIPS16_GPREL:
8747 case R_MICROMIPS_GPREL16:
8748 /* GP-relative relocations always resolve to a definition in a
8749 regular input file, ignoring the one-definition rule. This is
8750 important for the GP setup sequence in NewABI code, which
8751 always resolves to a local function even if other relocations
8752 against the symbol wouldn't. */
8753 constrain_symbol_p = FALSE;
8754 break;
8755
8756 case R_MIPS_32:
8757 case R_MIPS_REL32:
8758 case R_MIPS_64:
8759 /* In VxWorks executables, references to external symbols
8760 must be handled using copy relocs or PLT entries; it is not
8761 possible to convert this relocation into a dynamic one.
8762
8763 For executables that use PLTs and copy-relocs, we have a
8764 choice between converting the relocation into a dynamic
8765 one or using copy relocations or PLT entries. It is
8766 usually better to do the former, unless the relocation is
8767 against a read-only section. */
8768 if ((bfd_link_pic (info)
8769 || (h != NULL
8770 && htab->root.target_os != is_vxworks
8771 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8772 && !(!info->nocopyreloc
8773 && !PIC_OBJECT_P (abfd)
8774 && MIPS_ELF_READONLY_SECTION (sec))))
8775 && (sec->flags & SEC_ALLOC) != 0)
8776 {
8777 can_make_dynamic_p = TRUE;
8778 if (dynobj == NULL)
8779 elf_hash_table (info)->dynobj = dynobj = abfd;
8780 }
8781 break;
8782
8783 case R_MIPS_26:
8784 case R_MIPS_PC16:
8785 case R_MIPS_PC21_S2:
8786 case R_MIPS_PC26_S2:
8787 case R_MIPS16_26:
8788 case R_MIPS16_PC16_S1:
8789 case R_MICROMIPS_26_S1:
8790 case R_MICROMIPS_PC7_S1:
8791 case R_MICROMIPS_PC10_S1:
8792 case R_MICROMIPS_PC16_S1:
8793 case R_MICROMIPS_PC23_S2:
8794 call_reloc_p = TRUE;
8795 break;
8796 }
8797
8798 if (h)
8799 {
8800 if (constrain_symbol_p)
8801 {
8802 if (!can_make_dynamic_p)
8803 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8804
8805 if (!call_reloc_p)
8806 h->pointer_equality_needed = 1;
8807
8808 /* We must not create a stub for a symbol that has
8809 relocations related to taking the function's address.
8810 This doesn't apply to VxWorks, where CALL relocs refer
8811 to a .got.plt entry instead of a normal .got entry. */
8812 if (htab->root.target_os != is_vxworks
8813 && (!can_make_dynamic_p || !call_reloc_p))
8814 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8815 }
8816
8817 /* Relocations against the special VxWorks __GOTT_BASE__ and
8818 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8819 room for them in .rela.dyn. */
8820 if (is_gott_symbol (info, h))
8821 {
8822 if (sreloc == NULL)
8823 {
8824 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8825 if (sreloc == NULL)
8826 return FALSE;
8827 }
8828 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8829 if (MIPS_ELF_READONLY_SECTION (sec))
8830 /* We tell the dynamic linker that there are
8831 relocations against the text segment. */
8832 info->flags |= DF_TEXTREL;
8833 }
8834 }
8835 else if (call_lo16_reloc_p (r_type)
8836 || got_lo16_reloc_p (r_type)
8837 || got_disp_reloc_p (r_type)
8838 || (got16_reloc_p (r_type)
8839 && htab->root.target_os == is_vxworks))
8840 {
8841 /* We may need a local GOT entry for this relocation. We
8842 don't count R_MIPS_GOT_PAGE because we can estimate the
8843 maximum number of pages needed by looking at the size of
8844 the segment. Similar comments apply to R_MIPS*_GOT16 and
8845 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8846 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8847 R_MIPS_CALL_HI16 because these are always followed by an
8848 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8849 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8850 rel->r_addend, info, r_type))
8851 return FALSE;
8852 }
8853
8854 if (h != NULL
8855 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8856 ELF_ST_IS_MIPS16 (h->other)))
8857 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8858
8859 switch (r_type)
8860 {
8861 case R_MIPS_CALL16:
8862 case R_MIPS16_CALL16:
8863 case R_MICROMIPS_CALL16:
8864 if (h == NULL)
8865 {
8866 _bfd_error_handler
8867 /* xgettext:c-format */
8868 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8869 abfd, (uint64_t) rel->r_offset);
8870 bfd_set_error (bfd_error_bad_value);
8871 return FALSE;
8872 }
8873 /* Fall through. */
8874
8875 case R_MIPS_CALL_HI16:
8876 case R_MIPS_CALL_LO16:
8877 case R_MICROMIPS_CALL_HI16:
8878 case R_MICROMIPS_CALL_LO16:
8879 if (h != NULL)
8880 {
8881 /* Make sure there is room in the regular GOT to hold the
8882 function's address. We may eliminate it in favour of
8883 a .got.plt entry later; see mips_elf_count_got_symbols. */
8884 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8885 r_type))
8886 return FALSE;
8887
8888 /* We need a stub, not a plt entry for the undefined
8889 function. But we record it as if it needs plt. See
8890 _bfd_elf_adjust_dynamic_symbol. */
8891 h->needs_plt = 1;
8892 h->type = STT_FUNC;
8893 }
8894 break;
8895
8896 case R_MIPS_GOT_PAGE:
8897 case R_MICROMIPS_GOT_PAGE:
8898 case R_MIPS16_GOT16:
8899 case R_MIPS_GOT16:
8900 case R_MIPS_GOT_HI16:
8901 case R_MIPS_GOT_LO16:
8902 case R_MICROMIPS_GOT16:
8903 case R_MICROMIPS_GOT_HI16:
8904 case R_MICROMIPS_GOT_LO16:
8905 if (!h || got_page_reloc_p (r_type))
8906 {
8907 /* This relocation needs (or may need, if h != NULL) a
8908 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8909 know for sure until we know whether the symbol is
8910 preemptible. */
8911 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8912 {
8913 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8914 return FALSE;
8915 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8916 addend = mips_elf_read_rel_addend (abfd, rel,
8917 howto, contents);
8918 if (got16_reloc_p (r_type))
8919 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8920 contents, &addend);
8921 else
8922 addend <<= howto->rightshift;
8923 }
8924 else
8925 addend = rel->r_addend;
8926 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8927 h, addend))
8928 return FALSE;
8929
8930 if (h)
8931 {
8932 struct mips_elf_link_hash_entry *hmips =
8933 (struct mips_elf_link_hash_entry *) h;
8934
8935 /* This symbol is definitely not overridable. */
8936 if (hmips->root.def_regular
8937 && ! (bfd_link_pic (info) && ! info->symbolic
8938 && ! hmips->root.forced_local))
8939 h = NULL;
8940 }
8941 }
8942 /* If this is a global, overridable symbol, GOT_PAGE will
8943 decay to GOT_DISP, so we'll need a GOT entry for it. */
8944 /* Fall through. */
8945
8946 case R_MIPS_GOT_DISP:
8947 case R_MICROMIPS_GOT_DISP:
8948 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8949 FALSE, r_type))
8950 return FALSE;
8951 break;
8952
8953 case R_MIPS_TLS_GOTTPREL:
8954 case R_MIPS16_TLS_GOTTPREL:
8955 case R_MICROMIPS_TLS_GOTTPREL:
8956 if (bfd_link_pic (info))
8957 info->flags |= DF_STATIC_TLS;
8958 /* Fall through */
8959
8960 case R_MIPS_TLS_LDM:
8961 case R_MIPS16_TLS_LDM:
8962 case R_MICROMIPS_TLS_LDM:
8963 if (tls_ldm_reloc_p (r_type))
8964 {
8965 r_symndx = STN_UNDEF;
8966 h = NULL;
8967 }
8968 /* Fall through */
8969
8970 case R_MIPS_TLS_GD:
8971 case R_MIPS16_TLS_GD:
8972 case R_MICROMIPS_TLS_GD:
8973 /* This symbol requires a global offset table entry, or two
8974 for TLS GD relocations. */
8975 if (h != NULL)
8976 {
8977 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8978 FALSE, r_type))
8979 return FALSE;
8980 }
8981 else
8982 {
8983 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8984 rel->r_addend,
8985 info, r_type))
8986 return FALSE;
8987 }
8988 break;
8989
8990 case R_MIPS_32:
8991 case R_MIPS_REL32:
8992 case R_MIPS_64:
8993 /* In VxWorks executables, references to external symbols
8994 are handled using copy relocs or PLT stubs, so there's
8995 no need to add a .rela.dyn entry for this relocation. */
8996 if (can_make_dynamic_p)
8997 {
8998 if (sreloc == NULL)
8999 {
9000 sreloc = mips_elf_rel_dyn_section (info, TRUE);
9001 if (sreloc == NULL)
9002 return FALSE;
9003 }
9004 if (bfd_link_pic (info) && h == NULL)
9005 {
9006 /* When creating a shared object, we must copy these
9007 reloc types into the output file as R_MIPS_REL32
9008 relocs. Make room for this reloc in .rel(a).dyn. */
9009 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9010 if (MIPS_ELF_READONLY_SECTION (sec))
9011 /* We tell the dynamic linker that there are
9012 relocations against the text segment. */
9013 info->flags |= DF_TEXTREL;
9014 }
9015 else
9016 {
9017 struct mips_elf_link_hash_entry *hmips;
9018
9019 /* For a shared object, we must copy this relocation
9020 unless the symbol turns out to be undefined and
9021 weak with non-default visibility, in which case
9022 it will be left as zero.
9023
9024 We could elide R_MIPS_REL32 for locally binding symbols
9025 in shared libraries, but do not yet do so.
9026
9027 For an executable, we only need to copy this
9028 reloc if the symbol is defined in a dynamic
9029 object. */
9030 hmips = (struct mips_elf_link_hash_entry *) h;
9031 ++hmips->possibly_dynamic_relocs;
9032 if (MIPS_ELF_READONLY_SECTION (sec))
9033 /* We need it to tell the dynamic linker if there
9034 are relocations against the text segment. */
9035 hmips->readonly_reloc = TRUE;
9036 }
9037 }
9038
9039 if (SGI_COMPAT (abfd))
9040 mips_elf_hash_table (info)->compact_rel_size +=
9041 sizeof (Elf32_External_crinfo);
9042 break;
9043
9044 case R_MIPS_26:
9045 case R_MIPS_GPREL16:
9046 case R_MIPS_LITERAL:
9047 case R_MIPS_GPREL32:
9048 case R_MICROMIPS_26_S1:
9049 case R_MICROMIPS_GPREL16:
9050 case R_MICROMIPS_LITERAL:
9051 case R_MICROMIPS_GPREL7_S2:
9052 if (SGI_COMPAT (abfd))
9053 mips_elf_hash_table (info)->compact_rel_size +=
9054 sizeof (Elf32_External_crinfo);
9055 break;
9056
9057 /* This relocation describes the C++ object vtable hierarchy.
9058 Reconstruct it for later use during GC. */
9059 case R_MIPS_GNU_VTINHERIT:
9060 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
9061 return FALSE;
9062 break;
9063
9064 /* This relocation describes which C++ vtable entries are actually
9065 used. Record for later use during GC. */
9066 case R_MIPS_GNU_VTENTRY:
9067 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
9068 return FALSE;
9069 break;
9070
9071 default:
9072 break;
9073 }
9074
9075 /* Record the need for a PLT entry. At this point we don't know
9076 yet if we are going to create a PLT in the first place, but
9077 we only record whether the relocation requires a standard MIPS
9078 or a compressed code entry anyway. If we don't make a PLT after
9079 all, then we'll just ignore these arrangements. Likewise if
9080 a PLT entry is not created because the symbol is satisfied
9081 locally. */
9082 if (h != NULL
9083 && (branch_reloc_p (r_type)
9084 || mips16_branch_reloc_p (r_type)
9085 || micromips_branch_reloc_p (r_type))
9086 && !SYMBOL_CALLS_LOCAL (info, h))
9087 {
9088 if (h->plt.plist == NULL)
9089 h->plt.plist = mips_elf_make_plt_record (abfd);
9090 if (h->plt.plist == NULL)
9091 return FALSE;
9092
9093 if (branch_reloc_p (r_type))
9094 h->plt.plist->need_mips = TRUE;
9095 else
9096 h->plt.plist->need_comp = TRUE;
9097 }
9098
9099 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9100 if there is one. We only need to handle global symbols here;
9101 we decide whether to keep or delete stubs for local symbols
9102 when processing the stub's relocations. */
9103 if (h != NULL
9104 && !mips16_call_reloc_p (r_type)
9105 && !section_allows_mips16_refs_p (sec))
9106 {
9107 struct mips_elf_link_hash_entry *mh;
9108
9109 mh = (struct mips_elf_link_hash_entry *) h;
9110 mh->need_fn_stub = TRUE;
9111 }
9112
9113 /* Refuse some position-dependent relocations when creating a
9114 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9115 not PIC, but we can create dynamic relocations and the result
9116 will be fine. Also do not refuse R_MIPS_LO16, which can be
9117 combined with R_MIPS_GOT16. */
9118 if (bfd_link_pic (info))
9119 {
9120 switch (r_type)
9121 {
9122 case R_MIPS_TLS_TPREL_HI16:
9123 case R_MIPS16_TLS_TPREL_HI16:
9124 case R_MICROMIPS_TLS_TPREL_HI16:
9125 case R_MIPS_TLS_TPREL_LO16:
9126 case R_MIPS16_TLS_TPREL_LO16:
9127 case R_MICROMIPS_TLS_TPREL_LO16:
9128 /* These are okay in PIE, but not in a shared library. */
9129 if (bfd_link_executable (info))
9130 break;
9131
9132 /* FALLTHROUGH */
9133
9134 case R_MIPS16_HI16:
9135 case R_MIPS_HI16:
9136 case R_MIPS_HIGHER:
9137 case R_MIPS_HIGHEST:
9138 case R_MICROMIPS_HI16:
9139 case R_MICROMIPS_HIGHER:
9140 case R_MICROMIPS_HIGHEST:
9141 /* Don't refuse a high part relocation if it's against
9142 no symbol (e.g. part of a compound relocation). */
9143 if (r_symndx == STN_UNDEF)
9144 break;
9145
9146 /* Likewise an absolute symbol. */
9147 if (h != NULL && bfd_is_abs_symbol (&h->root))
9148 break;
9149
9150 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9151 and has a special meaning. */
9152 if (!NEWABI_P (abfd) && h != NULL
9153 && strcmp (h->root.root.string, "_gp_disp") == 0)
9154 break;
9155
9156 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9157 if (is_gott_symbol (info, h))
9158 break;
9159
9160 /* FALLTHROUGH */
9161
9162 case R_MIPS16_26:
9163 case R_MIPS_26:
9164 case R_MICROMIPS_26_S1:
9165 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9166 /* An error for unsupported relocations is raised as part
9167 of the above search, so we can skip the following. */
9168 if (howto != NULL)
9169 info->callbacks->einfo
9170 /* xgettext:c-format */
9171 (_("%X%H: relocation %s against `%s' cannot be used"
9172 " when making a shared object; recompile with -fPIC\n"),
9173 abfd, sec, rel->r_offset, howto->name,
9174 (h) ? h->root.root.string : "a local symbol");
9175 break;
9176 default:
9177 break;
9178 }
9179 }
9180 }
9181
9182 return TRUE;
9183 }
9184 \f
9185 /* Allocate space for global sym dynamic relocs. */
9186
9187 static bfd_boolean
9188 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9189 {
9190 struct bfd_link_info *info = inf;
9191 bfd *dynobj;
9192 struct mips_elf_link_hash_entry *hmips;
9193 struct mips_elf_link_hash_table *htab;
9194
9195 htab = mips_elf_hash_table (info);
9196 BFD_ASSERT (htab != NULL);
9197
9198 dynobj = elf_hash_table (info)->dynobj;
9199 hmips = (struct mips_elf_link_hash_entry *) h;
9200
9201 /* VxWorks executables are handled elsewhere; we only need to
9202 allocate relocations in shared objects. */
9203 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9204 return TRUE;
9205
9206 /* Ignore indirect symbols. All relocations against such symbols
9207 will be redirected to the target symbol. */
9208 if (h->root.type == bfd_link_hash_indirect)
9209 return TRUE;
9210
9211 /* If this symbol is defined in a dynamic object, or we are creating
9212 a shared library, we will need to copy any R_MIPS_32 or
9213 R_MIPS_REL32 relocs against it into the output file. */
9214 if (! bfd_link_relocatable (info)
9215 && hmips->possibly_dynamic_relocs != 0
9216 && (h->root.type == bfd_link_hash_defweak
9217 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
9218 || bfd_link_pic (info)))
9219 {
9220 bfd_boolean do_copy = TRUE;
9221
9222 if (h->root.type == bfd_link_hash_undefweak)
9223 {
9224 /* Do not copy relocations for undefined weak symbols that
9225 we are not going to export. */
9226 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9227 do_copy = FALSE;
9228
9229 /* Make sure undefined weak symbols are output as a dynamic
9230 symbol in PIEs. */
9231 else if (h->dynindx == -1 && !h->forced_local)
9232 {
9233 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9234 return FALSE;
9235 }
9236 }
9237
9238 if (do_copy)
9239 {
9240 /* Even though we don't directly need a GOT entry for this symbol,
9241 the SVR4 psABI requires it to have a dynamic symbol table
9242 index greater that DT_MIPS_GOTSYM if there are dynamic
9243 relocations against it.
9244
9245 VxWorks does not enforce the same mapping between the GOT
9246 and the symbol table, so the same requirement does not
9247 apply there. */
9248 if (htab->root.target_os != is_vxworks)
9249 {
9250 if (hmips->global_got_area > GGA_RELOC_ONLY)
9251 hmips->global_got_area = GGA_RELOC_ONLY;
9252 hmips->got_only_for_calls = FALSE;
9253 }
9254
9255 mips_elf_allocate_dynamic_relocations
9256 (dynobj, info, hmips->possibly_dynamic_relocs);
9257 if (hmips->readonly_reloc)
9258 /* We tell the dynamic linker that there are relocations
9259 against the text segment. */
9260 info->flags |= DF_TEXTREL;
9261 }
9262 }
9263
9264 return TRUE;
9265 }
9266
9267 /* Adjust a symbol defined by a dynamic object and referenced by a
9268 regular object. The current definition is in some section of the
9269 dynamic object, but we're not including those sections. We have to
9270 change the definition to something the rest of the link can
9271 understand. */
9272
9273 bfd_boolean
9274 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9275 struct elf_link_hash_entry *h)
9276 {
9277 bfd *dynobj;
9278 struct mips_elf_link_hash_entry *hmips;
9279 struct mips_elf_link_hash_table *htab;
9280 asection *s, *srel;
9281
9282 htab = mips_elf_hash_table (info);
9283 BFD_ASSERT (htab != NULL);
9284
9285 dynobj = elf_hash_table (info)->dynobj;
9286 hmips = (struct mips_elf_link_hash_entry *) h;
9287
9288 /* Make sure we know what is going on here. */
9289 if (dynobj == NULL
9290 || (! h->needs_plt
9291 && ! h->is_weakalias
9292 && (! h->def_dynamic
9293 || ! h->ref_regular
9294 || h->def_regular)))
9295 {
9296 if (h->type == STT_GNU_IFUNC)
9297 _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9298 h->root.root.string);
9299 else
9300 _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9301 h->root.root.string);
9302 return TRUE;
9303 }
9304
9305 hmips = (struct mips_elf_link_hash_entry *) h;
9306
9307 /* If there are call relocations against an externally-defined symbol,
9308 see whether we can create a MIPS lazy-binding stub for it. We can
9309 only do this if all references to the function are through call
9310 relocations, and in that case, the traditional lazy-binding stubs
9311 are much more efficient than PLT entries.
9312
9313 Traditional stubs are only available on SVR4 psABI-based systems;
9314 VxWorks always uses PLTs instead. */
9315 if (htab->root.target_os != is_vxworks
9316 && h->needs_plt
9317 && !hmips->no_fn_stub)
9318 {
9319 if (! elf_hash_table (info)->dynamic_sections_created)
9320 return TRUE;
9321
9322 /* If this symbol is not defined in a regular file, then set
9323 the symbol to the stub location. This is required to make
9324 function pointers compare as equal between the normal
9325 executable and the shared library. */
9326 if (!h->def_regular
9327 && !bfd_is_abs_section (htab->sstubs->output_section))
9328 {
9329 hmips->needs_lazy_stub = TRUE;
9330 htab->lazy_stub_count++;
9331 return TRUE;
9332 }
9333 }
9334 /* As above, VxWorks requires PLT entries for externally-defined
9335 functions that are only accessed through call relocations.
9336
9337 Both VxWorks and non-VxWorks targets also need PLT entries if there
9338 are static-only relocations against an externally-defined function.
9339 This can technically occur for shared libraries if there are
9340 branches to the symbol, although it is unlikely that this will be
9341 used in practice due to the short ranges involved. It can occur
9342 for any relative or absolute relocation in executables; in that
9343 case, the PLT entry becomes the function's canonical address. */
9344 else if (((h->needs_plt && !hmips->no_fn_stub)
9345 || (h->type == STT_FUNC && hmips->has_static_relocs))
9346 && htab->use_plts_and_copy_relocs
9347 && !SYMBOL_CALLS_LOCAL (info, h)
9348 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9349 && h->root.type == bfd_link_hash_undefweak))
9350 {
9351 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9352 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
9353
9354 /* If this is the first symbol to need a PLT entry, then make some
9355 basic setup. Also work out PLT entry sizes. We'll need them
9356 for PLT offset calculations. */
9357 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9358 {
9359 BFD_ASSERT (htab->root.sgotplt->size == 0);
9360 BFD_ASSERT (htab->plt_got_index == 0);
9361
9362 /* If we're using the PLT additions to the psABI, each PLT
9363 entry is 16 bytes and the PLT0 entry is 32 bytes.
9364 Encourage better cache usage by aligning. We do this
9365 lazily to avoid pessimizing traditional objects. */
9366 if (htab->root.target_os != is_vxworks
9367 && !bfd_set_section_alignment (htab->root.splt, 5))
9368 return FALSE;
9369
9370 /* Make sure that .got.plt is word-aligned. We do this lazily
9371 for the same reason as above. */
9372 if (!bfd_set_section_alignment (htab->root.sgotplt,
9373 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9374 return FALSE;
9375
9376 /* On non-VxWorks targets, the first two entries in .got.plt
9377 are reserved. */
9378 if (htab->root.target_os != is_vxworks)
9379 htab->plt_got_index
9380 += (get_elf_backend_data (dynobj)->got_header_size
9381 / MIPS_ELF_GOT_SIZE (dynobj));
9382
9383 /* On VxWorks, also allocate room for the header's
9384 .rela.plt.unloaded entries. */
9385 if (htab->root.target_os == is_vxworks
9386 && !bfd_link_pic (info))
9387 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9388
9389 /* Now work out the sizes of individual PLT entries. */
9390 if (htab->root.target_os == is_vxworks
9391 && bfd_link_pic (info))
9392 htab->plt_mips_entry_size
9393 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9394 else if (htab->root.target_os == is_vxworks)
9395 htab->plt_mips_entry_size
9396 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9397 else if (newabi_p)
9398 htab->plt_mips_entry_size
9399 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9400 else if (!micromips_p)
9401 {
9402 htab->plt_mips_entry_size
9403 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9404 htab->plt_comp_entry_size
9405 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9406 }
9407 else if (htab->insn32)
9408 {
9409 htab->plt_mips_entry_size
9410 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9411 htab->plt_comp_entry_size
9412 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9413 }
9414 else
9415 {
9416 htab->plt_mips_entry_size
9417 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9418 htab->plt_comp_entry_size
9419 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9420 }
9421 }
9422
9423 if (h->plt.plist == NULL)
9424 h->plt.plist = mips_elf_make_plt_record (dynobj);
9425 if (h->plt.plist == NULL)
9426 return FALSE;
9427
9428 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9429 n32 or n64, so always use a standard entry there.
9430
9431 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9432 all MIPS16 calls will go via that stub, and there is no benefit
9433 to having a MIPS16 entry. And in the case of call_stub a
9434 standard entry actually has to be used as the stub ends with a J
9435 instruction. */
9436 if (newabi_p
9437 || htab->root.target_os == is_vxworks
9438 || hmips->call_stub
9439 || hmips->call_fp_stub)
9440 {
9441 h->plt.plist->need_mips = TRUE;
9442 h->plt.plist->need_comp = FALSE;
9443 }
9444
9445 /* Otherwise, if there are no direct calls to the function, we
9446 have a free choice of whether to use standard or compressed
9447 entries. Prefer microMIPS entries if the object is known to
9448 contain microMIPS code, so that it becomes possible to create
9449 pure microMIPS binaries. Prefer standard entries otherwise,
9450 because MIPS16 ones are no smaller and are usually slower. */
9451 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9452 {
9453 if (micromips_p)
9454 h->plt.plist->need_comp = TRUE;
9455 else
9456 h->plt.plist->need_mips = TRUE;
9457 }
9458
9459 if (h->plt.plist->need_mips)
9460 {
9461 h->plt.plist->mips_offset = htab->plt_mips_offset;
9462 htab->plt_mips_offset += htab->plt_mips_entry_size;
9463 }
9464 if (h->plt.plist->need_comp)
9465 {
9466 h->plt.plist->comp_offset = htab->plt_comp_offset;
9467 htab->plt_comp_offset += htab->plt_comp_entry_size;
9468 }
9469
9470 /* Reserve the corresponding .got.plt entry now too. */
9471 h->plt.plist->gotplt_index = htab->plt_got_index++;
9472
9473 /* If the output file has no definition of the symbol, set the
9474 symbol's value to the address of the stub. */
9475 if (!bfd_link_pic (info) && !h->def_regular)
9476 hmips->use_plt_entry = TRUE;
9477
9478 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
9479 htab->root.srelplt->size += (htab->root.target_os == is_vxworks
9480 ? MIPS_ELF_RELA_SIZE (dynobj)
9481 : MIPS_ELF_REL_SIZE (dynobj));
9482
9483 /* Make room for the .rela.plt.unloaded relocations. */
9484 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9485 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9486
9487 /* All relocations against this symbol that could have been made
9488 dynamic will now refer to the PLT entry instead. */
9489 hmips->possibly_dynamic_relocs = 0;
9490
9491 return TRUE;
9492 }
9493
9494 /* If this is a weak symbol, and there is a real definition, the
9495 processor independent code will have arranged for us to see the
9496 real definition first, and we can just use the same value. */
9497 if (h->is_weakalias)
9498 {
9499 struct elf_link_hash_entry *def = weakdef (h);
9500 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9501 h->root.u.def.section = def->root.u.def.section;
9502 h->root.u.def.value = def->root.u.def.value;
9503 return TRUE;
9504 }
9505
9506 /* Otherwise, there is nothing further to do for symbols defined
9507 in regular objects. */
9508 if (h->def_regular)
9509 return TRUE;
9510
9511 /* There's also nothing more to do if we'll convert all relocations
9512 against this symbol into dynamic relocations. */
9513 if (!hmips->has_static_relocs)
9514 return TRUE;
9515
9516 /* We're now relying on copy relocations. Complain if we have
9517 some that we can't convert. */
9518 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
9519 {
9520 _bfd_error_handler (_("non-dynamic relocations refer to "
9521 "dynamic symbol %s"),
9522 h->root.root.string);
9523 bfd_set_error (bfd_error_bad_value);
9524 return FALSE;
9525 }
9526
9527 /* We must allocate the symbol in our .dynbss section, which will
9528 become part of the .bss section of the executable. There will be
9529 an entry for this symbol in the .dynsym section. The dynamic
9530 object will contain position independent code, so all references
9531 from the dynamic object to this symbol will go through the global
9532 offset table. The dynamic linker will use the .dynsym entry to
9533 determine the address it must put in the global offset table, so
9534 both the dynamic object and the regular object will refer to the
9535 same memory location for the variable. */
9536
9537 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9538 {
9539 s = htab->root.sdynrelro;
9540 srel = htab->root.sreldynrelro;
9541 }
9542 else
9543 {
9544 s = htab->root.sdynbss;
9545 srel = htab->root.srelbss;
9546 }
9547 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9548 {
9549 if (htab->root.target_os == is_vxworks)
9550 srel->size += sizeof (Elf32_External_Rela);
9551 else
9552 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9553 h->needs_copy = 1;
9554 }
9555
9556 /* All relocations against this symbol that could have been made
9557 dynamic will now refer to the local copy instead. */
9558 hmips->possibly_dynamic_relocs = 0;
9559
9560 return _bfd_elf_adjust_dynamic_copy (info, h, s);
9561 }
9562 \f
9563 /* This function is called after all the input files have been read,
9564 and the input sections have been assigned to output sections. We
9565 check for any mips16 stub sections that we can discard. */
9566
9567 bfd_boolean
9568 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9569 struct bfd_link_info *info)
9570 {
9571 asection *sect;
9572 struct mips_elf_link_hash_table *htab;
9573 struct mips_htab_traverse_info hti;
9574
9575 htab = mips_elf_hash_table (info);
9576 BFD_ASSERT (htab != NULL);
9577
9578 /* The .reginfo section has a fixed size. */
9579 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9580 if (sect != NULL)
9581 {
9582 bfd_set_section_size (sect, sizeof (Elf32_External_RegInfo));
9583 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9584 }
9585
9586 /* The .MIPS.abiflags section has a fixed size. */
9587 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9588 if (sect != NULL)
9589 {
9590 bfd_set_section_size (sect, sizeof (Elf_External_ABIFlags_v0));
9591 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9592 }
9593
9594 hti.info = info;
9595 hti.output_bfd = output_bfd;
9596 hti.error = FALSE;
9597 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9598 mips_elf_check_symbols, &hti);
9599 if (hti.error)
9600 return FALSE;
9601
9602 return TRUE;
9603 }
9604
9605 /* If the link uses a GOT, lay it out and work out its size. */
9606
9607 static bfd_boolean
9608 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9609 {
9610 bfd *dynobj;
9611 asection *s;
9612 struct mips_got_info *g;
9613 bfd_size_type loadable_size = 0;
9614 bfd_size_type page_gotno;
9615 bfd *ibfd;
9616 struct mips_elf_traverse_got_arg tga;
9617 struct mips_elf_link_hash_table *htab;
9618
9619 htab = mips_elf_hash_table (info);
9620 BFD_ASSERT (htab != NULL);
9621
9622 s = htab->root.sgot;
9623 if (s == NULL)
9624 return TRUE;
9625
9626 dynobj = elf_hash_table (info)->dynobj;
9627 g = htab->got_info;
9628
9629 /* Allocate room for the reserved entries. VxWorks always reserves
9630 3 entries; other objects only reserve 2 entries. */
9631 BFD_ASSERT (g->assigned_low_gotno == 0);
9632 if (htab->root.target_os == is_vxworks)
9633 htab->reserved_gotno = 3;
9634 else
9635 htab->reserved_gotno = 2;
9636 g->local_gotno += htab->reserved_gotno;
9637 g->assigned_low_gotno = htab->reserved_gotno;
9638
9639 /* Decide which symbols need to go in the global part of the GOT and
9640 count the number of reloc-only GOT symbols. */
9641 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9642
9643 if (!mips_elf_resolve_final_got_entries (info, g))
9644 return FALSE;
9645
9646 /* Calculate the total loadable size of the output. That
9647 will give us the maximum number of GOT_PAGE entries
9648 required. */
9649 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9650 {
9651 asection *subsection;
9652
9653 for (subsection = ibfd->sections;
9654 subsection;
9655 subsection = subsection->next)
9656 {
9657 if ((subsection->flags & SEC_ALLOC) == 0)
9658 continue;
9659 loadable_size += ((subsection->size + 0xf)
9660 &~ (bfd_size_type) 0xf);
9661 }
9662 }
9663
9664 if (htab->root.target_os == is_vxworks)
9665 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9666 relocations against local symbols evaluate to "G", and the EABI does
9667 not include R_MIPS_GOT_PAGE. */
9668 page_gotno = 0;
9669 else
9670 /* Assume there are two loadable segments consisting of contiguous
9671 sections. Is 5 enough? */
9672 page_gotno = (loadable_size >> 16) + 5;
9673
9674 /* Choose the smaller of the two page estimates; both are intended to be
9675 conservative. */
9676 if (page_gotno > g->page_gotno)
9677 page_gotno = g->page_gotno;
9678
9679 g->local_gotno += page_gotno;
9680 g->assigned_high_gotno = g->local_gotno - 1;
9681
9682 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9683 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9684 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9685
9686 /* VxWorks does not support multiple GOTs. It initializes $gp to
9687 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9688 dynamic loader. */
9689 if (htab->root.target_os != is_vxworks
9690 && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9691 {
9692 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9693 return FALSE;
9694 }
9695 else
9696 {
9697 /* Record that all bfds use G. This also has the effect of freeing
9698 the per-bfd GOTs, which we no longer need. */
9699 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9700 if (mips_elf_bfd_got (ibfd, FALSE))
9701 mips_elf_replace_bfd_got (ibfd, g);
9702 mips_elf_replace_bfd_got (output_bfd, g);
9703
9704 /* Set up TLS entries. */
9705 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9706 tga.info = info;
9707 tga.g = g;
9708 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9709 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9710 if (!tga.g)
9711 return FALSE;
9712 BFD_ASSERT (g->tls_assigned_gotno
9713 == g->global_gotno + g->local_gotno + g->tls_gotno);
9714
9715 /* Each VxWorks GOT entry needs an explicit relocation. */
9716 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9717 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9718
9719 /* Allocate room for the TLS relocations. */
9720 if (g->relocs)
9721 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9722 }
9723
9724 return TRUE;
9725 }
9726
9727 /* Estimate the size of the .MIPS.stubs section. */
9728
9729 static void
9730 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9731 {
9732 struct mips_elf_link_hash_table *htab;
9733 bfd_size_type dynsymcount;
9734
9735 htab = mips_elf_hash_table (info);
9736 BFD_ASSERT (htab != NULL);
9737
9738 if (htab->lazy_stub_count == 0)
9739 return;
9740
9741 /* IRIX rld assumes that a function stub isn't at the end of the .text
9742 section, so add a dummy entry to the end. */
9743 htab->lazy_stub_count++;
9744
9745 /* Get a worst-case estimate of the number of dynamic symbols needed.
9746 At this point, dynsymcount does not account for section symbols
9747 and count_section_dynsyms may overestimate the number that will
9748 be needed. */
9749 dynsymcount = (elf_hash_table (info)->dynsymcount
9750 + count_section_dynsyms (output_bfd, info));
9751
9752 /* Determine the size of one stub entry. There's no disadvantage
9753 from using microMIPS code here, so for the sake of pure-microMIPS
9754 binaries we prefer it whenever there's any microMIPS code in
9755 output produced at all. This has a benefit of stubs being
9756 shorter by 4 bytes each too, unless in the insn32 mode. */
9757 if (!MICROMIPS_P (output_bfd))
9758 htab->function_stub_size = (dynsymcount > 0x10000
9759 ? MIPS_FUNCTION_STUB_BIG_SIZE
9760 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9761 else if (htab->insn32)
9762 htab->function_stub_size = (dynsymcount > 0x10000
9763 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9764 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9765 else
9766 htab->function_stub_size = (dynsymcount > 0x10000
9767 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9768 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9769
9770 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9771 }
9772
9773 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9774 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9775 stub, allocate an entry in the stubs section. */
9776
9777 static bfd_boolean
9778 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9779 {
9780 struct mips_htab_traverse_info *hti = data;
9781 struct mips_elf_link_hash_table *htab;
9782 struct bfd_link_info *info;
9783 bfd *output_bfd;
9784
9785 info = hti->info;
9786 output_bfd = hti->output_bfd;
9787 htab = mips_elf_hash_table (info);
9788 BFD_ASSERT (htab != NULL);
9789
9790 if (h->needs_lazy_stub)
9791 {
9792 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9793 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9794 bfd_vma isa_bit = micromips_p;
9795
9796 BFD_ASSERT (htab->root.dynobj != NULL);
9797 if (h->root.plt.plist == NULL)
9798 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9799 if (h->root.plt.plist == NULL)
9800 {
9801 hti->error = TRUE;
9802 return FALSE;
9803 }
9804 h->root.root.u.def.section = htab->sstubs;
9805 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9806 h->root.plt.plist->stub_offset = htab->sstubs->size;
9807 h->root.other = other;
9808 htab->sstubs->size += htab->function_stub_size;
9809 }
9810 return TRUE;
9811 }
9812
9813 /* Allocate offsets in the stubs section to each symbol that needs one.
9814 Set the final size of the .MIPS.stub section. */
9815
9816 static bfd_boolean
9817 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9818 {
9819 bfd *output_bfd = info->output_bfd;
9820 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9821 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9822 bfd_vma isa_bit = micromips_p;
9823 struct mips_elf_link_hash_table *htab;
9824 struct mips_htab_traverse_info hti;
9825 struct elf_link_hash_entry *h;
9826 bfd *dynobj;
9827
9828 htab = mips_elf_hash_table (info);
9829 BFD_ASSERT (htab != NULL);
9830
9831 if (htab->lazy_stub_count == 0)
9832 return TRUE;
9833
9834 htab->sstubs->size = 0;
9835 hti.info = info;
9836 hti.output_bfd = output_bfd;
9837 hti.error = FALSE;
9838 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9839 if (hti.error)
9840 return FALSE;
9841 htab->sstubs->size += htab->function_stub_size;
9842 BFD_ASSERT (htab->sstubs->size
9843 == htab->lazy_stub_count * htab->function_stub_size);
9844
9845 dynobj = elf_hash_table (info)->dynobj;
9846 BFD_ASSERT (dynobj != NULL);
9847 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9848 if (h == NULL)
9849 return FALSE;
9850 h->root.u.def.value = isa_bit;
9851 h->other = other;
9852 h->type = STT_FUNC;
9853
9854 return TRUE;
9855 }
9856
9857 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9858 bfd_link_info. If H uses the address of a PLT entry as the value
9859 of the symbol, then set the entry in the symbol table now. Prefer
9860 a standard MIPS PLT entry. */
9861
9862 static bfd_boolean
9863 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9864 {
9865 struct bfd_link_info *info = data;
9866 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9867 struct mips_elf_link_hash_table *htab;
9868 unsigned int other;
9869 bfd_vma isa_bit;
9870 bfd_vma val;
9871
9872 htab = mips_elf_hash_table (info);
9873 BFD_ASSERT (htab != NULL);
9874
9875 if (h->use_plt_entry)
9876 {
9877 BFD_ASSERT (h->root.plt.plist != NULL);
9878 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9879 || h->root.plt.plist->comp_offset != MINUS_ONE);
9880
9881 val = htab->plt_header_size;
9882 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9883 {
9884 isa_bit = 0;
9885 val += h->root.plt.plist->mips_offset;
9886 other = 0;
9887 }
9888 else
9889 {
9890 isa_bit = 1;
9891 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9892 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9893 }
9894 val += isa_bit;
9895 /* For VxWorks, point at the PLT load stub rather than the lazy
9896 resolution stub; this stub will become the canonical function
9897 address. */
9898 if (htab->root.target_os == is_vxworks)
9899 val += 8;
9900
9901 h->root.root.u.def.section = htab->root.splt;
9902 h->root.root.u.def.value = val;
9903 h->root.other = other;
9904 }
9905
9906 return TRUE;
9907 }
9908
9909 /* Set the sizes of the dynamic sections. */
9910
9911 bfd_boolean
9912 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9913 struct bfd_link_info *info)
9914 {
9915 bfd *dynobj;
9916 asection *s, *sreldyn;
9917 bfd_boolean reltext;
9918 struct mips_elf_link_hash_table *htab;
9919
9920 htab = mips_elf_hash_table (info);
9921 BFD_ASSERT (htab != NULL);
9922 dynobj = elf_hash_table (info)->dynobj;
9923 BFD_ASSERT (dynobj != NULL);
9924
9925 if (elf_hash_table (info)->dynamic_sections_created)
9926 {
9927 /* Set the contents of the .interp section to the interpreter. */
9928 if (bfd_link_executable (info) && !info->nointerp)
9929 {
9930 s = bfd_get_linker_section (dynobj, ".interp");
9931 BFD_ASSERT (s != NULL);
9932 s->size
9933 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9934 s->contents
9935 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9936 }
9937
9938 /* Figure out the size of the PLT header if we know that we
9939 are using it. For the sake of cache alignment always use
9940 a standard header whenever any standard entries are present
9941 even if microMIPS entries are present as well. This also
9942 lets the microMIPS header rely on the value of $v0 only set
9943 by microMIPS entries, for a small size reduction.
9944
9945 Set symbol table entry values for symbols that use the
9946 address of their PLT entry now that we can calculate it.
9947
9948 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9949 haven't already in _bfd_elf_create_dynamic_sections. */
9950 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
9951 {
9952 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9953 && !htab->plt_mips_offset);
9954 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9955 bfd_vma isa_bit = micromips_p;
9956 struct elf_link_hash_entry *h;
9957 bfd_vma size;
9958
9959 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9960 BFD_ASSERT (htab->root.sgotplt->size == 0);
9961 BFD_ASSERT (htab->root.splt->size == 0);
9962
9963 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
9964 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9965 else if (htab->root.target_os == is_vxworks)
9966 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9967 else if (ABI_64_P (output_bfd))
9968 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9969 else if (ABI_N32_P (output_bfd))
9970 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9971 else if (!micromips_p)
9972 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
9973 else if (htab->insn32)
9974 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
9975 else
9976 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
9977
9978 htab->plt_header_is_comp = micromips_p;
9979 htab->plt_header_size = size;
9980 htab->root.splt->size = (size
9981 + htab->plt_mips_offset
9982 + htab->plt_comp_offset);
9983 htab->root.sgotplt->size = (htab->plt_got_index
9984 * MIPS_ELF_GOT_SIZE (dynobj));
9985
9986 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9987
9988 if (htab->root.hplt == NULL)
9989 {
9990 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
9991 "_PROCEDURE_LINKAGE_TABLE_");
9992 htab->root.hplt = h;
9993 if (h == NULL)
9994 return FALSE;
9995 }
9996
9997 h = htab->root.hplt;
9998 h->root.u.def.value = isa_bit;
9999 h->other = other;
10000 h->type = STT_FUNC;
10001 }
10002 }
10003
10004 /* Allocate space for global sym dynamic relocs. */
10005 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
10006
10007 mips_elf_estimate_stub_size (output_bfd, info);
10008
10009 if (!mips_elf_lay_out_got (output_bfd, info))
10010 return FALSE;
10011
10012 mips_elf_lay_out_lazy_stubs (info);
10013
10014 /* The check_relocs and adjust_dynamic_symbol entry points have
10015 determined the sizes of the various dynamic sections. Allocate
10016 memory for them. */
10017 reltext = FALSE;
10018 for (s = dynobj->sections; s != NULL; s = s->next)
10019 {
10020 const char *name;
10021
10022 /* It's OK to base decisions on the section name, because none
10023 of the dynobj section names depend upon the input files. */
10024 name = bfd_section_name (s);
10025
10026 if ((s->flags & SEC_LINKER_CREATED) == 0)
10027 continue;
10028
10029 if (CONST_STRNEQ (name, ".rel"))
10030 {
10031 if (s->size != 0)
10032 {
10033 const char *outname;
10034 asection *target;
10035
10036 /* If this relocation section applies to a read only
10037 section, then we probably need a DT_TEXTREL entry.
10038 If the relocation section is .rel(a).dyn, we always
10039 assert a DT_TEXTREL entry rather than testing whether
10040 there exists a relocation to a read only section or
10041 not. */
10042 outname = bfd_section_name (s->output_section);
10043 target = bfd_get_section_by_name (output_bfd, outname + 4);
10044 if ((target != NULL
10045 && (target->flags & SEC_READONLY) != 0
10046 && (target->flags & SEC_ALLOC) != 0)
10047 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
10048 reltext = TRUE;
10049
10050 /* We use the reloc_count field as a counter if we need
10051 to copy relocs into the output file. */
10052 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
10053 s->reloc_count = 0;
10054
10055 /* If combreloc is enabled, elf_link_sort_relocs() will
10056 sort relocations, but in a different way than we do,
10057 and before we're done creating relocations. Also, it
10058 will move them around between input sections'
10059 relocation's contents, so our sorting would be
10060 broken, so don't let it run. */
10061 info->combreloc = 0;
10062 }
10063 }
10064 else if (bfd_link_executable (info)
10065 && ! mips_elf_hash_table (info)->use_rld_obj_head
10066 && CONST_STRNEQ (name, ".rld_map"))
10067 {
10068 /* We add a room for __rld_map. It will be filled in by the
10069 rtld to contain a pointer to the _r_debug structure. */
10070 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
10071 }
10072 else if (SGI_COMPAT (output_bfd)
10073 && CONST_STRNEQ (name, ".compact_rel"))
10074 s->size += mips_elf_hash_table (info)->compact_rel_size;
10075 else if (s == htab->root.splt)
10076 {
10077 /* If the last PLT entry has a branch delay slot, allocate
10078 room for an extra nop to fill the delay slot. This is
10079 for CPUs without load interlocking. */
10080 if (! LOAD_INTERLOCKS_P (output_bfd)
10081 && htab->root.target_os != is_vxworks
10082 && s->size > 0)
10083 s->size += 4;
10084 }
10085 else if (! CONST_STRNEQ (name, ".init")
10086 && s != htab->root.sgot
10087 && s != htab->root.sgotplt
10088 && s != htab->sstubs
10089 && s != htab->root.sdynbss
10090 && s != htab->root.sdynrelro)
10091 {
10092 /* It's not one of our sections, so don't allocate space. */
10093 continue;
10094 }
10095
10096 if (s->size == 0)
10097 {
10098 s->flags |= SEC_EXCLUDE;
10099 continue;
10100 }
10101
10102 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10103 continue;
10104
10105 /* Allocate memory for the section contents. */
10106 s->contents = bfd_zalloc (dynobj, s->size);
10107 if (s->contents == NULL)
10108 {
10109 bfd_set_error (bfd_error_no_memory);
10110 return FALSE;
10111 }
10112 }
10113
10114 if (elf_hash_table (info)->dynamic_sections_created)
10115 {
10116 /* Add some entries to the .dynamic section. We fill in the
10117 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10118 must add the entries now so that we get the correct size for
10119 the .dynamic section. */
10120
10121 /* SGI object has the equivalence of DT_DEBUG in the
10122 DT_MIPS_RLD_MAP entry. This must come first because glibc
10123 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10124 may only look at the first one they see. */
10125 if (!bfd_link_pic (info)
10126 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10127 return FALSE;
10128
10129 if (bfd_link_executable (info)
10130 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10131 return FALSE;
10132
10133 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10134 used by the debugger. */
10135 if (bfd_link_executable (info)
10136 && !SGI_COMPAT (output_bfd)
10137 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10138 return FALSE;
10139
10140 if (reltext
10141 && (SGI_COMPAT (output_bfd)
10142 || htab->root.target_os == is_vxworks))
10143 info->flags |= DF_TEXTREL;
10144
10145 if ((info->flags & DF_TEXTREL) != 0)
10146 {
10147 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
10148 return FALSE;
10149
10150 /* Clear the DF_TEXTREL flag. It will be set again if we
10151 write out an actual text relocation; we may not, because
10152 at this point we do not know whether e.g. any .eh_frame
10153 absolute relocations have been converted to PC-relative. */
10154 info->flags &= ~DF_TEXTREL;
10155 }
10156
10157 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
10158 return FALSE;
10159
10160 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
10161 if (htab->root.target_os == is_vxworks)
10162 {
10163 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10164 use any of the DT_MIPS_* tags. */
10165 if (sreldyn && sreldyn->size > 0)
10166 {
10167 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10168 return FALSE;
10169
10170 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10171 return FALSE;
10172
10173 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10174 return FALSE;
10175 }
10176 }
10177 else
10178 {
10179 if (sreldyn && sreldyn->size > 0
10180 && !bfd_is_abs_section (sreldyn->output_section))
10181 {
10182 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10183 return FALSE;
10184
10185 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10186 return FALSE;
10187
10188 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10189 return FALSE;
10190 }
10191
10192 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10193 return FALSE;
10194
10195 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10196 return FALSE;
10197
10198 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10199 return FALSE;
10200
10201 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10202 return FALSE;
10203
10204 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10205 return FALSE;
10206
10207 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10208 return FALSE;
10209
10210 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10211 return FALSE;
10212
10213 if (info->emit_gnu_hash
10214 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10215 return FALSE;
10216
10217 if (IRIX_COMPAT (dynobj) == ict_irix5
10218 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10219 return FALSE;
10220
10221 if (IRIX_COMPAT (dynobj) == ict_irix6
10222 && (bfd_get_section_by_name
10223 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
10224 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10225 return FALSE;
10226 }
10227 if (htab->root.splt->size > 0)
10228 {
10229 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10230 return FALSE;
10231
10232 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10233 return FALSE;
10234
10235 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10236 return FALSE;
10237
10238 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10239 return FALSE;
10240 }
10241 if (htab->root.target_os == is_vxworks
10242 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10243 return FALSE;
10244 }
10245
10246 return TRUE;
10247 }
10248 \f
10249 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10250 Adjust its R_ADDEND field so that it is correct for the output file.
10251 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10252 and sections respectively; both use symbol indexes. */
10253
10254 static void
10255 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10256 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10257 asection **local_sections, Elf_Internal_Rela *rel)
10258 {
10259 unsigned int r_type, r_symndx;
10260 Elf_Internal_Sym *sym;
10261 asection *sec;
10262
10263 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10264 {
10265 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10266 if (gprel16_reloc_p (r_type)
10267 || r_type == R_MIPS_GPREL32
10268 || literal_reloc_p (r_type))
10269 {
10270 rel->r_addend += _bfd_get_gp_value (input_bfd);
10271 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10272 }
10273
10274 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10275 sym = local_syms + r_symndx;
10276
10277 /* Adjust REL's addend to account for section merging. */
10278 if (!bfd_link_relocatable (info))
10279 {
10280 sec = local_sections[r_symndx];
10281 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10282 }
10283
10284 /* This would normally be done by the rela_normal code in elflink.c. */
10285 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10286 rel->r_addend += local_sections[r_symndx]->output_offset;
10287 }
10288 }
10289
10290 /* Handle relocations against symbols from removed linkonce sections,
10291 or sections discarded by a linker script. We use this wrapper around
10292 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10293 on 64-bit ELF targets. In this case for any relocation handled, which
10294 always be the first in a triplet, the remaining two have to be processed
10295 together with the first, even if they are R_MIPS_NONE. It is the symbol
10296 index referred by the first reloc that applies to all the three and the
10297 remaining two never refer to an object symbol. And it is the final
10298 relocation (the last non-null one) that determines the output field of
10299 the whole relocation so retrieve the corresponding howto structure for
10300 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10301
10302 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10303 and therefore requires to be pasted in a loop. It also defines a block
10304 and does not protect any of its arguments, hence the extra brackets. */
10305
10306 static void
10307 mips_reloc_against_discarded_section (bfd *output_bfd,
10308 struct bfd_link_info *info,
10309 bfd *input_bfd, asection *input_section,
10310 Elf_Internal_Rela **rel,
10311 const Elf_Internal_Rela **relend,
10312 bfd_boolean rel_reloc,
10313 reloc_howto_type *howto,
10314 bfd_byte *contents)
10315 {
10316 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10317 int count = bed->s->int_rels_per_ext_rel;
10318 unsigned int r_type;
10319 int i;
10320
10321 for (i = count - 1; i > 0; i--)
10322 {
10323 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10324 if (r_type != R_MIPS_NONE)
10325 {
10326 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10327 break;
10328 }
10329 }
10330 do
10331 {
10332 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10333 (*rel), count, (*relend),
10334 howto, i, contents);
10335 }
10336 while (0);
10337 }
10338
10339 /* Relocate a MIPS ELF section. */
10340
10341 bfd_boolean
10342 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10343 bfd *input_bfd, asection *input_section,
10344 bfd_byte *contents, Elf_Internal_Rela *relocs,
10345 Elf_Internal_Sym *local_syms,
10346 asection **local_sections)
10347 {
10348 Elf_Internal_Rela *rel;
10349 const Elf_Internal_Rela *relend;
10350 bfd_vma addend = 0;
10351 bfd_boolean use_saved_addend_p = FALSE;
10352
10353 relend = relocs + input_section->reloc_count;
10354 for (rel = relocs; rel < relend; ++rel)
10355 {
10356 const char *name;
10357 bfd_vma value = 0;
10358 reloc_howto_type *howto;
10359 bfd_boolean cross_mode_jump_p = FALSE;
10360 /* TRUE if the relocation is a RELA relocation, rather than a
10361 REL relocation. */
10362 bfd_boolean rela_relocation_p = TRUE;
10363 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10364 const char *msg;
10365 unsigned long r_symndx;
10366 asection *sec;
10367 Elf_Internal_Shdr *symtab_hdr;
10368 struct elf_link_hash_entry *h;
10369 bfd_boolean rel_reloc;
10370
10371 rel_reloc = (NEWABI_P (input_bfd)
10372 && mips_elf_rel_relocation_p (input_bfd, input_section,
10373 relocs, rel));
10374 /* Find the relocation howto for this relocation. */
10375 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10376
10377 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10378 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10379 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10380 {
10381 sec = local_sections[r_symndx];
10382 h = NULL;
10383 }
10384 else
10385 {
10386 unsigned long extsymoff;
10387
10388 extsymoff = 0;
10389 if (!elf_bad_symtab (input_bfd))
10390 extsymoff = symtab_hdr->sh_info;
10391 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10392 while (h->root.type == bfd_link_hash_indirect
10393 || h->root.type == bfd_link_hash_warning)
10394 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10395
10396 sec = NULL;
10397 if (h->root.type == bfd_link_hash_defined
10398 || h->root.type == bfd_link_hash_defweak)
10399 sec = h->root.u.def.section;
10400 }
10401
10402 if (sec != NULL && discarded_section (sec))
10403 {
10404 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10405 input_section, &rel, &relend,
10406 rel_reloc, howto, contents);
10407 continue;
10408 }
10409
10410 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10411 {
10412 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10413 64-bit code, but make sure all their addresses are in the
10414 lowermost or uppermost 32-bit section of the 64-bit address
10415 space. Thus, when they use an R_MIPS_64 they mean what is
10416 usually meant by R_MIPS_32, with the exception that the
10417 stored value is sign-extended to 64 bits. */
10418 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
10419
10420 /* On big-endian systems, we need to lie about the position
10421 of the reloc. */
10422 if (bfd_big_endian (input_bfd))
10423 rel->r_offset += 4;
10424 }
10425
10426 if (!use_saved_addend_p)
10427 {
10428 /* If these relocations were originally of the REL variety,
10429 we must pull the addend out of the field that will be
10430 relocated. Otherwise, we simply use the contents of the
10431 RELA relocation. */
10432 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10433 relocs, rel))
10434 {
10435 rela_relocation_p = FALSE;
10436 addend = mips_elf_read_rel_addend (input_bfd, rel,
10437 howto, contents);
10438 if (hi16_reloc_p (r_type)
10439 || (got16_reloc_p (r_type)
10440 && mips_elf_local_relocation_p (input_bfd, rel,
10441 local_sections)))
10442 {
10443 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
10444 contents, &addend))
10445 {
10446 if (h)
10447 name = h->root.root.string;
10448 else
10449 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10450 local_syms + r_symndx,
10451 sec);
10452 _bfd_error_handler
10453 /* xgettext:c-format */
10454 (_("%pB: can't find matching LO16 reloc against `%s'"
10455 " for %s at %#" PRIx64 " in section `%pA'"),
10456 input_bfd, name,
10457 howto->name, (uint64_t) rel->r_offset, input_section);
10458 }
10459 }
10460 else
10461 addend <<= howto->rightshift;
10462 }
10463 else
10464 addend = rel->r_addend;
10465 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10466 local_syms, local_sections, rel);
10467 }
10468
10469 if (bfd_link_relocatable (info))
10470 {
10471 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10472 && bfd_big_endian (input_bfd))
10473 rel->r_offset -= 4;
10474
10475 if (!rela_relocation_p && rel->r_addend)
10476 {
10477 addend += rel->r_addend;
10478 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10479 addend = mips_elf_high (addend);
10480 else if (r_type == R_MIPS_HIGHER)
10481 addend = mips_elf_higher (addend);
10482 else if (r_type == R_MIPS_HIGHEST)
10483 addend = mips_elf_highest (addend);
10484 else
10485 addend >>= howto->rightshift;
10486
10487 /* We use the source mask, rather than the destination
10488 mask because the place to which we are writing will be
10489 source of the addend in the final link. */
10490 addend &= howto->src_mask;
10491
10492 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10493 /* See the comment above about using R_MIPS_64 in the 32-bit
10494 ABI. Here, we need to update the addend. It would be
10495 possible to get away with just using the R_MIPS_32 reloc
10496 but for endianness. */
10497 {
10498 bfd_vma sign_bits;
10499 bfd_vma low_bits;
10500 bfd_vma high_bits;
10501
10502 if (addend & ((bfd_vma) 1 << 31))
10503 #ifdef BFD64
10504 sign_bits = ((bfd_vma) 1 << 32) - 1;
10505 #else
10506 sign_bits = -1;
10507 #endif
10508 else
10509 sign_bits = 0;
10510
10511 /* If we don't know that we have a 64-bit type,
10512 do two separate stores. */
10513 if (bfd_big_endian (input_bfd))
10514 {
10515 /* Store the sign-bits (which are most significant)
10516 first. */
10517 low_bits = sign_bits;
10518 high_bits = addend;
10519 }
10520 else
10521 {
10522 low_bits = addend;
10523 high_bits = sign_bits;
10524 }
10525 bfd_put_32 (input_bfd, low_bits,
10526 contents + rel->r_offset);
10527 bfd_put_32 (input_bfd, high_bits,
10528 contents + rel->r_offset + 4);
10529 continue;
10530 }
10531
10532 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10533 input_bfd, input_section,
10534 contents, FALSE))
10535 return FALSE;
10536 }
10537
10538 /* Go on to the next relocation. */
10539 continue;
10540 }
10541
10542 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10543 relocations for the same offset. In that case we are
10544 supposed to treat the output of each relocation as the addend
10545 for the next. */
10546 if (rel + 1 < relend
10547 && rel->r_offset == rel[1].r_offset
10548 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10549 use_saved_addend_p = TRUE;
10550 else
10551 use_saved_addend_p = FALSE;
10552
10553 /* Figure out what value we are supposed to relocate. */
10554 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10555 input_section, contents,
10556 info, rel, addend, howto,
10557 local_syms, local_sections,
10558 &value, &name, &cross_mode_jump_p,
10559 use_saved_addend_p))
10560 {
10561 case bfd_reloc_continue:
10562 /* There's nothing to do. */
10563 continue;
10564
10565 case bfd_reloc_undefined:
10566 /* mips_elf_calculate_relocation already called the
10567 undefined_symbol callback. There's no real point in
10568 trying to perform the relocation at this point, so we
10569 just skip ahead to the next relocation. */
10570 continue;
10571
10572 case bfd_reloc_notsupported:
10573 msg = _("internal error: unsupported relocation error");
10574 info->callbacks->warning
10575 (info, msg, name, input_bfd, input_section, rel->r_offset);
10576 return FALSE;
10577
10578 case bfd_reloc_overflow:
10579 if (use_saved_addend_p)
10580 /* Ignore overflow until we reach the last relocation for
10581 a given location. */
10582 ;
10583 else
10584 {
10585 struct mips_elf_link_hash_table *htab;
10586
10587 htab = mips_elf_hash_table (info);
10588 BFD_ASSERT (htab != NULL);
10589 BFD_ASSERT (name != NULL);
10590 if (!htab->small_data_overflow_reported
10591 && (gprel16_reloc_p (howto->type)
10592 || literal_reloc_p (howto->type)))
10593 {
10594 msg = _("small-data section exceeds 64KB;"
10595 " lower small-data size limit (see option -G)");
10596
10597 htab->small_data_overflow_reported = TRUE;
10598 (*info->callbacks->einfo) ("%P: %s\n", msg);
10599 }
10600 (*info->callbacks->reloc_overflow)
10601 (info, NULL, name, howto->name, (bfd_vma) 0,
10602 input_bfd, input_section, rel->r_offset);
10603 }
10604 break;
10605
10606 case bfd_reloc_ok:
10607 break;
10608
10609 case bfd_reloc_outofrange:
10610 msg = NULL;
10611 if (jal_reloc_p (howto->type))
10612 msg = (cross_mode_jump_p
10613 ? _("cannot convert a jump to JALX "
10614 "for a non-word-aligned address")
10615 : (howto->type == R_MIPS16_26
10616 ? _("jump to a non-word-aligned address")
10617 : _("jump to a non-instruction-aligned address")));
10618 else if (b_reloc_p (howto->type))
10619 msg = (cross_mode_jump_p
10620 ? _("cannot convert a branch to JALX "
10621 "for a non-word-aligned address")
10622 : _("branch to a non-instruction-aligned address"));
10623 else if (aligned_pcrel_reloc_p (howto->type))
10624 msg = _("PC-relative load from unaligned address");
10625 if (msg)
10626 {
10627 info->callbacks->einfo
10628 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10629 break;
10630 }
10631 /* Fall through. */
10632
10633 default:
10634 abort ();
10635 break;
10636 }
10637
10638 /* If we've got another relocation for the address, keep going
10639 until we reach the last one. */
10640 if (use_saved_addend_p)
10641 {
10642 addend = value;
10643 continue;
10644 }
10645
10646 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10647 /* See the comment above about using R_MIPS_64 in the 32-bit
10648 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10649 that calculated the right value. Now, however, we
10650 sign-extend the 32-bit result to 64-bits, and store it as a
10651 64-bit value. We are especially generous here in that we
10652 go to extreme lengths to support this usage on systems with
10653 only a 32-bit VMA. */
10654 {
10655 bfd_vma sign_bits;
10656 bfd_vma low_bits;
10657 bfd_vma high_bits;
10658
10659 if (value & ((bfd_vma) 1 << 31))
10660 #ifdef BFD64
10661 sign_bits = ((bfd_vma) 1 << 32) - 1;
10662 #else
10663 sign_bits = -1;
10664 #endif
10665 else
10666 sign_bits = 0;
10667
10668 /* If we don't know that we have a 64-bit type,
10669 do two separate stores. */
10670 if (bfd_big_endian (input_bfd))
10671 {
10672 /* Undo what we did above. */
10673 rel->r_offset -= 4;
10674 /* Store the sign-bits (which are most significant)
10675 first. */
10676 low_bits = sign_bits;
10677 high_bits = value;
10678 }
10679 else
10680 {
10681 low_bits = value;
10682 high_bits = sign_bits;
10683 }
10684 bfd_put_32 (input_bfd, low_bits,
10685 contents + rel->r_offset);
10686 bfd_put_32 (input_bfd, high_bits,
10687 contents + rel->r_offset + 4);
10688 continue;
10689 }
10690
10691 /* Actually perform the relocation. */
10692 if (! mips_elf_perform_relocation (info, howto, rel, value,
10693 input_bfd, input_section,
10694 contents, cross_mode_jump_p))
10695 return FALSE;
10696 }
10697
10698 return TRUE;
10699 }
10700 \f
10701 /* A function that iterates over each entry in la25_stubs and fills
10702 in the code for each one. DATA points to a mips_htab_traverse_info. */
10703
10704 static int
10705 mips_elf_create_la25_stub (void **slot, void *data)
10706 {
10707 struct mips_htab_traverse_info *hti;
10708 struct mips_elf_link_hash_table *htab;
10709 struct mips_elf_la25_stub *stub;
10710 asection *s;
10711 bfd_byte *loc;
10712 bfd_vma offset, target, target_high, target_low;
10713 bfd_vma branch_pc;
10714 bfd_signed_vma pcrel_offset = 0;
10715
10716 stub = (struct mips_elf_la25_stub *) *slot;
10717 hti = (struct mips_htab_traverse_info *) data;
10718 htab = mips_elf_hash_table (hti->info);
10719 BFD_ASSERT (htab != NULL);
10720
10721 /* Create the section contents, if we haven't already. */
10722 s = stub->stub_section;
10723 loc = s->contents;
10724 if (loc == NULL)
10725 {
10726 loc = bfd_malloc (s->size);
10727 if (loc == NULL)
10728 {
10729 hti->error = TRUE;
10730 return FALSE;
10731 }
10732 s->contents = loc;
10733 }
10734
10735 /* Work out where in the section this stub should go. */
10736 offset = stub->offset;
10737
10738 /* We add 8 here to account for the LUI/ADDIU instructions
10739 before the branch instruction. This cannot be moved down to
10740 where pcrel_offset is calculated as 's' is updated in
10741 mips_elf_get_la25_target. */
10742 branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10743
10744 /* Work out the target address. */
10745 target = mips_elf_get_la25_target (stub, &s);
10746 target += s->output_section->vma + s->output_offset;
10747
10748 target_high = ((target + 0x8000) >> 16) & 0xffff;
10749 target_low = (target & 0xffff);
10750
10751 /* Calculate the PC of the compact branch instruction (for the case where
10752 compact branches are used for either microMIPSR6 or MIPSR6 with
10753 compact branches. Add 4-bytes to account for BC using the PC of the
10754 next instruction as the base. */
10755 pcrel_offset = target - (branch_pc + 4);
10756
10757 if (stub->stub_section != htab->strampoline)
10758 {
10759 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10760 of the section and write the two instructions at the end. */
10761 memset (loc, 0, offset);
10762 loc += offset;
10763 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10764 {
10765 bfd_put_micromips_32 (hti->output_bfd,
10766 LA25_LUI_MICROMIPS (target_high),
10767 loc);
10768 bfd_put_micromips_32 (hti->output_bfd,
10769 LA25_ADDIU_MICROMIPS (target_low),
10770 loc + 4);
10771 }
10772 else
10773 {
10774 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10775 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10776 }
10777 }
10778 else
10779 {
10780 /* This is trampoline. */
10781 loc += offset;
10782 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10783 {
10784 bfd_put_micromips_32 (hti->output_bfd,
10785 LA25_LUI_MICROMIPS (target_high), loc);
10786 bfd_put_micromips_32 (hti->output_bfd,
10787 LA25_J_MICROMIPS (target), loc + 4);
10788 bfd_put_micromips_32 (hti->output_bfd,
10789 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10790 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10791 }
10792 else
10793 {
10794 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10795 if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10796 {
10797 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10798 bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10799 }
10800 else
10801 {
10802 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10803 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10804 }
10805 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10806 }
10807 }
10808 return TRUE;
10809 }
10810
10811 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10812 adjust it appropriately now. */
10813
10814 static void
10815 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10816 const char *name, Elf_Internal_Sym *sym)
10817 {
10818 /* The linker script takes care of providing names and values for
10819 these, but we must place them into the right sections. */
10820 static const char* const text_section_symbols[] = {
10821 "_ftext",
10822 "_etext",
10823 "__dso_displacement",
10824 "__elf_header",
10825 "__program_header_table",
10826 NULL
10827 };
10828
10829 static const char* const data_section_symbols[] = {
10830 "_fdata",
10831 "_edata",
10832 "_end",
10833 "_fbss",
10834 NULL
10835 };
10836
10837 const char* const *p;
10838 int i;
10839
10840 for (i = 0; i < 2; ++i)
10841 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10842 *p;
10843 ++p)
10844 if (strcmp (*p, name) == 0)
10845 {
10846 /* All of these symbols are given type STT_SECTION by the
10847 IRIX6 linker. */
10848 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10849 sym->st_other = STO_PROTECTED;
10850
10851 /* The IRIX linker puts these symbols in special sections. */
10852 if (i == 0)
10853 sym->st_shndx = SHN_MIPS_TEXT;
10854 else
10855 sym->st_shndx = SHN_MIPS_DATA;
10856
10857 break;
10858 }
10859 }
10860
10861 /* Finish up dynamic symbol handling. We set the contents of various
10862 dynamic sections here. */
10863
10864 bfd_boolean
10865 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10866 struct bfd_link_info *info,
10867 struct elf_link_hash_entry *h,
10868 Elf_Internal_Sym *sym)
10869 {
10870 bfd *dynobj;
10871 asection *sgot;
10872 struct mips_got_info *g, *gg;
10873 const char *name;
10874 int idx;
10875 struct mips_elf_link_hash_table *htab;
10876 struct mips_elf_link_hash_entry *hmips;
10877
10878 htab = mips_elf_hash_table (info);
10879 BFD_ASSERT (htab != NULL);
10880 dynobj = elf_hash_table (info)->dynobj;
10881 hmips = (struct mips_elf_link_hash_entry *) h;
10882
10883 BFD_ASSERT (htab->root.target_os != is_vxworks);
10884
10885 if (h->plt.plist != NULL
10886 && (h->plt.plist->mips_offset != MINUS_ONE
10887 || h->plt.plist->comp_offset != MINUS_ONE))
10888 {
10889 /* We've decided to create a PLT entry for this symbol. */
10890 bfd_byte *loc;
10891 bfd_vma header_address, got_address;
10892 bfd_vma got_address_high, got_address_low, load;
10893 bfd_vma got_index;
10894 bfd_vma isa_bit;
10895
10896 got_index = h->plt.plist->gotplt_index;
10897
10898 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10899 BFD_ASSERT (h->dynindx != -1);
10900 BFD_ASSERT (htab->root.splt != NULL);
10901 BFD_ASSERT (got_index != MINUS_ONE);
10902 BFD_ASSERT (!h->def_regular);
10903
10904 /* Calculate the address of the PLT header. */
10905 isa_bit = htab->plt_header_is_comp;
10906 header_address = (htab->root.splt->output_section->vma
10907 + htab->root.splt->output_offset + isa_bit);
10908
10909 /* Calculate the address of the .got.plt entry. */
10910 got_address = (htab->root.sgotplt->output_section->vma
10911 + htab->root.sgotplt->output_offset
10912 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10913
10914 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10915 got_address_low = got_address & 0xffff;
10916
10917 /* The PLT sequence is not safe for N64 if .got.plt entry's address
10918 cannot be loaded in two instructions. */
10919 if (ABI_64_P (output_bfd)
10920 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
10921 {
10922 _bfd_error_handler
10923 /* xgettext:c-format */
10924 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
10925 "supported; consider using `-Ttext-segment=...'"),
10926 output_bfd,
10927 htab->root.sgotplt->output_section,
10928 (int64_t) got_address);
10929 bfd_set_error (bfd_error_no_error);
10930 return FALSE;
10931 }
10932
10933 /* Initially point the .got.plt entry at the PLT header. */
10934 loc = (htab->root.sgotplt->contents
10935 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10936 if (ABI_64_P (output_bfd))
10937 bfd_put_64 (output_bfd, header_address, loc);
10938 else
10939 bfd_put_32 (output_bfd, header_address, loc);
10940
10941 /* Now handle the PLT itself. First the standard entry (the order
10942 does not matter, we just have to pick one). */
10943 if (h->plt.plist->mips_offset != MINUS_ONE)
10944 {
10945 const bfd_vma *plt_entry;
10946 bfd_vma plt_offset;
10947
10948 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10949
10950 BFD_ASSERT (plt_offset <= htab->root.splt->size);
10951
10952 /* Find out where the .plt entry should go. */
10953 loc = htab->root.splt->contents + plt_offset;
10954
10955 /* Pick the load opcode. */
10956 load = MIPS_ELF_LOAD_WORD (output_bfd);
10957
10958 /* Fill in the PLT entry itself. */
10959
10960 if (MIPSR6_P (output_bfd))
10961 plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
10962 : mipsr6_exec_plt_entry;
10963 else
10964 plt_entry = mips_exec_plt_entry;
10965 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10966 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10967 loc + 4);
10968
10969 if (! LOAD_INTERLOCKS_P (output_bfd)
10970 || (MIPSR6_P (output_bfd) && htab->compact_branches))
10971 {
10972 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10973 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10974 }
10975 else
10976 {
10977 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10978 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10979 loc + 12);
10980 }
10981 }
10982
10983 /* Now the compressed entry. They come after any standard ones. */
10984 if (h->plt.plist->comp_offset != MINUS_ONE)
10985 {
10986 bfd_vma plt_offset;
10987
10988 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10989 + h->plt.plist->comp_offset);
10990
10991 BFD_ASSERT (plt_offset <= htab->root.splt->size);
10992
10993 /* Find out where the .plt entry should go. */
10994 loc = htab->root.splt->contents + plt_offset;
10995
10996 /* Fill in the PLT entry itself. */
10997 if (!MICROMIPS_P (output_bfd))
10998 {
10999 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11000
11001 bfd_put_16 (output_bfd, plt_entry[0], loc);
11002 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11003 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11004 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11005 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11006 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11007 bfd_put_32 (output_bfd, got_address, loc + 12);
11008 }
11009 else if (htab->insn32)
11010 {
11011 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11012
11013 bfd_put_16 (output_bfd, plt_entry[0], loc);
11014 bfd_put_16 (output_bfd, got_address_high, loc + 2);
11015 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11016 bfd_put_16 (output_bfd, got_address_low, loc + 6);
11017 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11018 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11019 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11020 bfd_put_16 (output_bfd, got_address_low, loc + 14);
11021 }
11022 else
11023 {
11024 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11025 bfd_signed_vma gotpc_offset;
11026 bfd_vma loc_address;
11027
11028 BFD_ASSERT (got_address % 4 == 0);
11029
11030 loc_address = (htab->root.splt->output_section->vma
11031 + htab->root.splt->output_offset + plt_offset);
11032 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11033
11034 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11035 if (gotpc_offset + 0x1000000 >= 0x2000000)
11036 {
11037 _bfd_error_handler
11038 /* xgettext:c-format */
11039 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11040 "beyond the range of ADDIUPC"),
11041 output_bfd,
11042 htab->root.sgotplt->output_section,
11043 (int64_t) gotpc_offset,
11044 htab->root.splt->output_section);
11045 bfd_set_error (bfd_error_no_error);
11046 return FALSE;
11047 }
11048 bfd_put_16 (output_bfd,
11049 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11050 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11051 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11052 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11053 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11054 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11055 }
11056 }
11057
11058 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11059 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
11060 got_index - 2, h->dynindx,
11061 R_MIPS_JUMP_SLOT, got_address);
11062
11063 /* We distinguish between PLT entries and lazy-binding stubs by
11064 giving the former an st_other value of STO_MIPS_PLT. Set the
11065 flag and leave the value if there are any relocations in the
11066 binary where pointer equality matters. */
11067 sym->st_shndx = SHN_UNDEF;
11068 if (h->pointer_equality_needed)
11069 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
11070 else
11071 {
11072 sym->st_value = 0;
11073 sym->st_other = 0;
11074 }
11075 }
11076
11077 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
11078 {
11079 /* We've decided to create a lazy-binding stub. */
11080 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
11081 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11082 bfd_vma stub_size = htab->function_stub_size;
11083 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
11084 bfd_vma isa_bit = micromips_p;
11085 bfd_vma stub_big_size;
11086
11087 if (!micromips_p)
11088 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
11089 else if (htab->insn32)
11090 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11091 else
11092 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
11093
11094 /* This symbol has a stub. Set it up. */
11095
11096 BFD_ASSERT (h->dynindx != -1);
11097
11098 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
11099
11100 /* Values up to 2^31 - 1 are allowed. Larger values would cause
11101 sign extension at runtime in the stub, resulting in a negative
11102 index value. */
11103 if (h->dynindx & ~0x7fffffff)
11104 return FALSE;
11105
11106 /* Fill the stub. */
11107 if (micromips_p)
11108 {
11109 idx = 0;
11110 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11111 stub + idx);
11112 idx += 4;
11113 if (htab->insn32)
11114 {
11115 bfd_put_micromips_32 (output_bfd,
11116 STUB_MOVE32_MICROMIPS, stub + idx);
11117 idx += 4;
11118 }
11119 else
11120 {
11121 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11122 idx += 2;
11123 }
11124 if (stub_size == stub_big_size)
11125 {
11126 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11127
11128 bfd_put_micromips_32 (output_bfd,
11129 STUB_LUI_MICROMIPS (dynindx_hi),
11130 stub + idx);
11131 idx += 4;
11132 }
11133 if (htab->insn32)
11134 {
11135 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11136 stub + idx);
11137 idx += 4;
11138 }
11139 else
11140 {
11141 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11142 idx += 2;
11143 }
11144
11145 /* If a large stub is not required and sign extension is not a
11146 problem, then use legacy code in the stub. */
11147 if (stub_size == stub_big_size)
11148 bfd_put_micromips_32 (output_bfd,
11149 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11150 stub + idx);
11151 else if (h->dynindx & ~0x7fff)
11152 bfd_put_micromips_32 (output_bfd,
11153 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11154 stub + idx);
11155 else
11156 bfd_put_micromips_32 (output_bfd,
11157 STUB_LI16S_MICROMIPS (output_bfd,
11158 h->dynindx),
11159 stub + idx);
11160 }
11161 else
11162 {
11163 idx = 0;
11164 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11165 idx += 4;
11166 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
11167 idx += 4;
11168 if (stub_size == stub_big_size)
11169 {
11170 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11171 stub + idx);
11172 idx += 4;
11173 }
11174
11175 if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11176 {
11177 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11178 idx += 4;
11179 }
11180
11181 /* If a large stub is not required and sign extension is not a
11182 problem, then use legacy code in the stub. */
11183 if (stub_size == stub_big_size)
11184 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11185 stub + idx);
11186 else if (h->dynindx & ~0x7fff)
11187 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11188 stub + idx);
11189 else
11190 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11191 stub + idx);
11192 idx += 4;
11193
11194 if (MIPSR6_P (output_bfd) && htab->compact_branches)
11195 bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
11196 }
11197
11198 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11199 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11200 stub, stub_size);
11201
11202 /* Mark the symbol as undefined. stub_offset != -1 occurs
11203 only for the referenced symbol. */
11204 sym->st_shndx = SHN_UNDEF;
11205
11206 /* The run-time linker uses the st_value field of the symbol
11207 to reset the global offset table entry for this external
11208 to its stub address when unlinking a shared object. */
11209 sym->st_value = (htab->sstubs->output_section->vma
11210 + htab->sstubs->output_offset
11211 + h->plt.plist->stub_offset
11212 + isa_bit);
11213 sym->st_other = other;
11214 }
11215
11216 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11217 refer to the stub, since only the stub uses the standard calling
11218 conventions. */
11219 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11220 {
11221 BFD_ASSERT (hmips->need_fn_stub);
11222 sym->st_value = (hmips->fn_stub->output_section->vma
11223 + hmips->fn_stub->output_offset);
11224 sym->st_size = hmips->fn_stub->size;
11225 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11226 }
11227
11228 BFD_ASSERT (h->dynindx != -1
11229 || h->forced_local);
11230
11231 sgot = htab->root.sgot;
11232 g = htab->got_info;
11233 BFD_ASSERT (g != NULL);
11234
11235 /* Run through the global symbol table, creating GOT entries for all
11236 the symbols that need them. */
11237 if (hmips->global_got_area != GGA_NONE)
11238 {
11239 bfd_vma offset;
11240 bfd_vma value;
11241
11242 value = sym->st_value;
11243 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11244 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11245 }
11246
11247 if (hmips->global_got_area != GGA_NONE && g->next)
11248 {
11249 struct mips_got_entry e, *p;
11250 bfd_vma entry;
11251 bfd_vma offset;
11252
11253 gg = g;
11254
11255 e.abfd = output_bfd;
11256 e.symndx = -1;
11257 e.d.h = hmips;
11258 e.tls_type = GOT_TLS_NONE;
11259
11260 for (g = g->next; g->next != gg; g = g->next)
11261 {
11262 if (g->got_entries
11263 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11264 &e)))
11265 {
11266 offset = p->gotidx;
11267 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
11268 if (bfd_link_pic (info)
11269 || (elf_hash_table (info)->dynamic_sections_created
11270 && p->d.h != NULL
11271 && p->d.h->root.def_dynamic
11272 && !p->d.h->root.def_regular))
11273 {
11274 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11275 the various compatibility problems, it's easier to mock
11276 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11277 mips_elf_create_dynamic_relocation to calculate the
11278 appropriate addend. */
11279 Elf_Internal_Rela rel[3];
11280
11281 memset (rel, 0, sizeof (rel));
11282 if (ABI_64_P (output_bfd))
11283 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11284 else
11285 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11286 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11287
11288 entry = 0;
11289 if (! (mips_elf_create_dynamic_relocation
11290 (output_bfd, info, rel,
11291 e.d.h, NULL, sym->st_value, &entry, sgot)))
11292 return FALSE;
11293 }
11294 else
11295 entry = sym->st_value;
11296 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
11297 }
11298 }
11299 }
11300
11301 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11302 name = h->root.root.string;
11303 if (h == elf_hash_table (info)->hdynamic
11304 || h == elf_hash_table (info)->hgot)
11305 sym->st_shndx = SHN_ABS;
11306 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11307 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11308 {
11309 sym->st_shndx = SHN_ABS;
11310 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11311 sym->st_value = 1;
11312 }
11313 else if (SGI_COMPAT (output_bfd))
11314 {
11315 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11316 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11317 {
11318 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11319 sym->st_other = STO_PROTECTED;
11320 sym->st_value = 0;
11321 sym->st_shndx = SHN_MIPS_DATA;
11322 }
11323 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11324 {
11325 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11326 sym->st_other = STO_PROTECTED;
11327 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11328 sym->st_shndx = SHN_ABS;
11329 }
11330 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11331 {
11332 if (h->type == STT_FUNC)
11333 sym->st_shndx = SHN_MIPS_TEXT;
11334 else if (h->type == STT_OBJECT)
11335 sym->st_shndx = SHN_MIPS_DATA;
11336 }
11337 }
11338
11339 /* Emit a copy reloc, if needed. */
11340 if (h->needs_copy)
11341 {
11342 asection *s;
11343 bfd_vma symval;
11344
11345 BFD_ASSERT (h->dynindx != -1);
11346 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11347
11348 s = mips_elf_rel_dyn_section (info, FALSE);
11349 symval = (h->root.u.def.section->output_section->vma
11350 + h->root.u.def.section->output_offset
11351 + h->root.u.def.value);
11352 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11353 h->dynindx, R_MIPS_COPY, symval);
11354 }
11355
11356 /* Handle the IRIX6-specific symbols. */
11357 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11358 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11359
11360 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11361 to treat compressed symbols like any other. */
11362 if (ELF_ST_IS_MIPS16 (sym->st_other))
11363 {
11364 BFD_ASSERT (sym->st_value & 1);
11365 sym->st_other -= STO_MIPS16;
11366 }
11367 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11368 {
11369 BFD_ASSERT (sym->st_value & 1);
11370 sym->st_other -= STO_MICROMIPS;
11371 }
11372
11373 return TRUE;
11374 }
11375
11376 /* Likewise, for VxWorks. */
11377
11378 bfd_boolean
11379 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11380 struct bfd_link_info *info,
11381 struct elf_link_hash_entry *h,
11382 Elf_Internal_Sym *sym)
11383 {
11384 bfd *dynobj;
11385 asection *sgot;
11386 struct mips_got_info *g;
11387 struct mips_elf_link_hash_table *htab;
11388 struct mips_elf_link_hash_entry *hmips;
11389
11390 htab = mips_elf_hash_table (info);
11391 BFD_ASSERT (htab != NULL);
11392 dynobj = elf_hash_table (info)->dynobj;
11393 hmips = (struct mips_elf_link_hash_entry *) h;
11394
11395 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11396 {
11397 bfd_byte *loc;
11398 bfd_vma plt_address, got_address, got_offset, branch_offset;
11399 Elf_Internal_Rela rel;
11400 static const bfd_vma *plt_entry;
11401 bfd_vma gotplt_index;
11402 bfd_vma plt_offset;
11403
11404 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11405 gotplt_index = h->plt.plist->gotplt_index;
11406
11407 BFD_ASSERT (h->dynindx != -1);
11408 BFD_ASSERT (htab->root.splt != NULL);
11409 BFD_ASSERT (gotplt_index != MINUS_ONE);
11410 BFD_ASSERT (plt_offset <= htab->root.splt->size);
11411
11412 /* Calculate the address of the .plt entry. */
11413 plt_address = (htab->root.splt->output_section->vma
11414 + htab->root.splt->output_offset
11415 + plt_offset);
11416
11417 /* Calculate the address of the .got.plt entry. */
11418 got_address = (htab->root.sgotplt->output_section->vma
11419 + htab->root.sgotplt->output_offset
11420 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11421
11422 /* Calculate the offset of the .got.plt entry from
11423 _GLOBAL_OFFSET_TABLE_. */
11424 got_offset = mips_elf_gotplt_index (info, h);
11425
11426 /* Calculate the offset for the branch at the start of the PLT
11427 entry. The branch jumps to the beginning of .plt. */
11428 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11429
11430 /* Fill in the initial value of the .got.plt entry. */
11431 bfd_put_32 (output_bfd, plt_address,
11432 (htab->root.sgotplt->contents
11433 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11434
11435 /* Find out where the .plt entry should go. */
11436 loc = htab->root.splt->contents + plt_offset;
11437
11438 if (bfd_link_pic (info))
11439 {
11440 plt_entry = mips_vxworks_shared_plt_entry;
11441 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11442 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11443 }
11444 else
11445 {
11446 bfd_vma got_address_high, got_address_low;
11447
11448 plt_entry = mips_vxworks_exec_plt_entry;
11449 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11450 got_address_low = got_address & 0xffff;
11451
11452 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11453 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11454 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11455 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11456 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11457 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11458 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11459 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11460
11461 loc = (htab->srelplt2->contents
11462 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11463
11464 /* Emit a relocation for the .got.plt entry. */
11465 rel.r_offset = got_address;
11466 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11467 rel.r_addend = plt_offset;
11468 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11469
11470 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11471 loc += sizeof (Elf32_External_Rela);
11472 rel.r_offset = plt_address + 8;
11473 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11474 rel.r_addend = got_offset;
11475 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11476
11477 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11478 loc += sizeof (Elf32_External_Rela);
11479 rel.r_offset += 4;
11480 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11481 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11482 }
11483
11484 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11485 loc = (htab->root.srelplt->contents
11486 + gotplt_index * sizeof (Elf32_External_Rela));
11487 rel.r_offset = got_address;
11488 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11489 rel.r_addend = 0;
11490 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11491
11492 if (!h->def_regular)
11493 sym->st_shndx = SHN_UNDEF;
11494 }
11495
11496 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11497
11498 sgot = htab->root.sgot;
11499 g = htab->got_info;
11500 BFD_ASSERT (g != NULL);
11501
11502 /* See if this symbol has an entry in the GOT. */
11503 if (hmips->global_got_area != GGA_NONE)
11504 {
11505 bfd_vma offset;
11506 Elf_Internal_Rela outrel;
11507 bfd_byte *loc;
11508 asection *s;
11509
11510 /* Install the symbol value in the GOT. */
11511 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11512 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11513
11514 /* Add a dynamic relocation for it. */
11515 s = mips_elf_rel_dyn_section (info, FALSE);
11516 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11517 outrel.r_offset = (sgot->output_section->vma
11518 + sgot->output_offset
11519 + offset);
11520 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11521 outrel.r_addend = 0;
11522 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11523 }
11524
11525 /* Emit a copy reloc, if needed. */
11526 if (h->needs_copy)
11527 {
11528 Elf_Internal_Rela rel;
11529 asection *srel;
11530 bfd_byte *loc;
11531
11532 BFD_ASSERT (h->dynindx != -1);
11533
11534 rel.r_offset = (h->root.u.def.section->output_section->vma
11535 + h->root.u.def.section->output_offset
11536 + h->root.u.def.value);
11537 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11538 rel.r_addend = 0;
11539 if (h->root.u.def.section == htab->root.sdynrelro)
11540 srel = htab->root.sreldynrelro;
11541 else
11542 srel = htab->root.srelbss;
11543 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11544 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11545 ++srel->reloc_count;
11546 }
11547
11548 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11549 if (ELF_ST_IS_COMPRESSED (sym->st_other))
11550 sym->st_value &= ~1;
11551
11552 return TRUE;
11553 }
11554
11555 /* Write out a plt0 entry to the beginning of .plt. */
11556
11557 static bfd_boolean
11558 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11559 {
11560 bfd_byte *loc;
11561 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11562 static const bfd_vma *plt_entry;
11563 struct mips_elf_link_hash_table *htab;
11564
11565 htab = mips_elf_hash_table (info);
11566 BFD_ASSERT (htab != NULL);
11567
11568 if (ABI_64_P (output_bfd))
11569 plt_entry = (htab->compact_branches
11570 ? mipsr6_n64_exec_plt0_entry_compact
11571 : mips_n64_exec_plt0_entry);
11572 else if (ABI_N32_P (output_bfd))
11573 plt_entry = (htab->compact_branches
11574 ? mipsr6_n32_exec_plt0_entry_compact
11575 : mips_n32_exec_plt0_entry);
11576 else if (!htab->plt_header_is_comp)
11577 plt_entry = (htab->compact_branches
11578 ? mipsr6_o32_exec_plt0_entry_compact
11579 : mips_o32_exec_plt0_entry);
11580 else if (htab->insn32)
11581 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11582 else
11583 plt_entry = micromips_o32_exec_plt0_entry;
11584
11585 /* Calculate the value of .got.plt. */
11586 gotplt_value = (htab->root.sgotplt->output_section->vma
11587 + htab->root.sgotplt->output_offset);
11588 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11589 gotplt_value_low = gotplt_value & 0xffff;
11590
11591 /* The PLT sequence is not safe for N64 if .got.plt's address can
11592 not be loaded in two instructions. */
11593 if (ABI_64_P (output_bfd)
11594 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11595 {
11596 _bfd_error_handler
11597 /* xgettext:c-format */
11598 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11599 "supported; consider using `-Ttext-segment=...'"),
11600 output_bfd,
11601 htab->root.sgotplt->output_section,
11602 (int64_t) gotplt_value);
11603 bfd_set_error (bfd_error_no_error);
11604 return FALSE;
11605 }
11606
11607 /* Install the PLT header. */
11608 loc = htab->root.splt->contents;
11609 if (plt_entry == micromips_o32_exec_plt0_entry)
11610 {
11611 bfd_vma gotpc_offset;
11612 bfd_vma loc_address;
11613 size_t i;
11614
11615 BFD_ASSERT (gotplt_value % 4 == 0);
11616
11617 loc_address = (htab->root.splt->output_section->vma
11618 + htab->root.splt->output_offset);
11619 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11620
11621 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11622 if (gotpc_offset + 0x1000000 >= 0x2000000)
11623 {
11624 _bfd_error_handler
11625 /* xgettext:c-format */
11626 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11627 "beyond the range of ADDIUPC"),
11628 output_bfd,
11629 htab->root.sgotplt->output_section,
11630 (int64_t) gotpc_offset,
11631 htab->root.splt->output_section);
11632 bfd_set_error (bfd_error_no_error);
11633 return FALSE;
11634 }
11635 bfd_put_16 (output_bfd,
11636 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11637 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11638 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11639 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11640 }
11641 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11642 {
11643 size_t i;
11644
11645 bfd_put_16 (output_bfd, plt_entry[0], loc);
11646 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11647 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11648 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11649 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11650 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11651 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11652 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11653 }
11654 else
11655 {
11656 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11657 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11658 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11659 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11660 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11661 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11662 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11663 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11664 }
11665
11666 return TRUE;
11667 }
11668
11669 /* Install the PLT header for a VxWorks executable and finalize the
11670 contents of .rela.plt.unloaded. */
11671
11672 static void
11673 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11674 {
11675 Elf_Internal_Rela rela;
11676 bfd_byte *loc;
11677 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11678 static const bfd_vma *plt_entry;
11679 struct mips_elf_link_hash_table *htab;
11680
11681 htab = mips_elf_hash_table (info);
11682 BFD_ASSERT (htab != NULL);
11683
11684 plt_entry = mips_vxworks_exec_plt0_entry;
11685
11686 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11687 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11688 + htab->root.hgot->root.u.def.section->output_offset
11689 + htab->root.hgot->root.u.def.value);
11690
11691 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11692 got_value_low = got_value & 0xffff;
11693
11694 /* Calculate the address of the PLT header. */
11695 plt_address = (htab->root.splt->output_section->vma
11696 + htab->root.splt->output_offset);
11697
11698 /* Install the PLT header. */
11699 loc = htab->root.splt->contents;
11700 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11701 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11702 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11703 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11704 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11705 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11706
11707 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11708 loc = htab->srelplt2->contents;
11709 rela.r_offset = plt_address;
11710 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11711 rela.r_addend = 0;
11712 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11713 loc += sizeof (Elf32_External_Rela);
11714
11715 /* Output the relocation for the following addiu of
11716 %lo(_GLOBAL_OFFSET_TABLE_). */
11717 rela.r_offset += 4;
11718 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11719 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11720 loc += sizeof (Elf32_External_Rela);
11721
11722 /* Fix up the remaining relocations. They may have the wrong
11723 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11724 in which symbols were output. */
11725 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11726 {
11727 Elf_Internal_Rela rel;
11728
11729 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11730 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11731 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11732 loc += sizeof (Elf32_External_Rela);
11733
11734 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11735 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11736 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11737 loc += sizeof (Elf32_External_Rela);
11738
11739 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11740 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11741 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11742 loc += sizeof (Elf32_External_Rela);
11743 }
11744 }
11745
11746 /* Install the PLT header for a VxWorks shared library. */
11747
11748 static void
11749 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11750 {
11751 unsigned int i;
11752 struct mips_elf_link_hash_table *htab;
11753
11754 htab = mips_elf_hash_table (info);
11755 BFD_ASSERT (htab != NULL);
11756
11757 /* We just need to copy the entry byte-by-byte. */
11758 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11759 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11760 htab->root.splt->contents + i * 4);
11761 }
11762
11763 /* Finish up the dynamic sections. */
11764
11765 bfd_boolean
11766 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11767 struct bfd_link_info *info)
11768 {
11769 bfd *dynobj;
11770 asection *sdyn;
11771 asection *sgot;
11772 struct mips_got_info *gg, *g;
11773 struct mips_elf_link_hash_table *htab;
11774
11775 htab = mips_elf_hash_table (info);
11776 BFD_ASSERT (htab != NULL);
11777
11778 dynobj = elf_hash_table (info)->dynobj;
11779
11780 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11781
11782 sgot = htab->root.sgot;
11783 gg = htab->got_info;
11784
11785 if (elf_hash_table (info)->dynamic_sections_created)
11786 {
11787 bfd_byte *b;
11788 int dyn_to_skip = 0, dyn_skipped = 0;
11789
11790 BFD_ASSERT (sdyn != NULL);
11791 BFD_ASSERT (gg != NULL);
11792
11793 g = mips_elf_bfd_got (output_bfd, FALSE);
11794 BFD_ASSERT (g != NULL);
11795
11796 for (b = sdyn->contents;
11797 b < sdyn->contents + sdyn->size;
11798 b += MIPS_ELF_DYN_SIZE (dynobj))
11799 {
11800 Elf_Internal_Dyn dyn;
11801 const char *name;
11802 size_t elemsize;
11803 asection *s;
11804 bfd_boolean swap_out_p;
11805
11806 /* Read in the current dynamic entry. */
11807 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11808
11809 /* Assume that we're going to modify it and write it out. */
11810 swap_out_p = TRUE;
11811
11812 switch (dyn.d_tag)
11813 {
11814 case DT_RELENT:
11815 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11816 break;
11817
11818 case DT_RELAENT:
11819 BFD_ASSERT (htab->root.target_os == is_vxworks);
11820 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11821 break;
11822
11823 case DT_STRSZ:
11824 /* Rewrite DT_STRSZ. */
11825 dyn.d_un.d_val =
11826 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11827 break;
11828
11829 case DT_PLTGOT:
11830 s = htab->root.sgot;
11831 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11832 break;
11833
11834 case DT_MIPS_PLTGOT:
11835 s = htab->root.sgotplt;
11836 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11837 break;
11838
11839 case DT_MIPS_RLD_VERSION:
11840 dyn.d_un.d_val = 1; /* XXX */
11841 break;
11842
11843 case DT_MIPS_FLAGS:
11844 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11845 break;
11846
11847 case DT_MIPS_TIME_STAMP:
11848 {
11849 time_t t;
11850 time (&t);
11851 dyn.d_un.d_val = t;
11852 }
11853 break;
11854
11855 case DT_MIPS_ICHECKSUM:
11856 /* XXX FIXME: */
11857 swap_out_p = FALSE;
11858 break;
11859
11860 case DT_MIPS_IVERSION:
11861 /* XXX FIXME: */
11862 swap_out_p = FALSE;
11863 break;
11864
11865 case DT_MIPS_BASE_ADDRESS:
11866 s = output_bfd->sections;
11867 BFD_ASSERT (s != NULL);
11868 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11869 break;
11870
11871 case DT_MIPS_LOCAL_GOTNO:
11872 dyn.d_un.d_val = g->local_gotno;
11873 break;
11874
11875 case DT_MIPS_UNREFEXTNO:
11876 /* The index into the dynamic symbol table which is the
11877 entry of the first external symbol that is not
11878 referenced within the same object. */
11879 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11880 break;
11881
11882 case DT_MIPS_GOTSYM:
11883 if (htab->global_gotsym)
11884 {
11885 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11886 break;
11887 }
11888 /* In case if we don't have global got symbols we default
11889 to setting DT_MIPS_GOTSYM to the same value as
11890 DT_MIPS_SYMTABNO. */
11891 /* Fall through. */
11892
11893 case DT_MIPS_SYMTABNO:
11894 name = ".dynsym";
11895 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11896 s = bfd_get_linker_section (dynobj, name);
11897
11898 if (s != NULL)
11899 dyn.d_un.d_val = s->size / elemsize;
11900 else
11901 dyn.d_un.d_val = 0;
11902 break;
11903
11904 case DT_MIPS_HIPAGENO:
11905 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11906 break;
11907
11908 case DT_MIPS_RLD_MAP:
11909 {
11910 struct elf_link_hash_entry *h;
11911 h = mips_elf_hash_table (info)->rld_symbol;
11912 if (!h)
11913 {
11914 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11915 swap_out_p = FALSE;
11916 break;
11917 }
11918 s = h->root.u.def.section;
11919
11920 /* The MIPS_RLD_MAP tag stores the absolute address of the
11921 debug pointer. */
11922 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11923 + h->root.u.def.value);
11924 }
11925 break;
11926
11927 case DT_MIPS_RLD_MAP_REL:
11928 {
11929 struct elf_link_hash_entry *h;
11930 bfd_vma dt_addr, rld_addr;
11931 h = mips_elf_hash_table (info)->rld_symbol;
11932 if (!h)
11933 {
11934 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11935 swap_out_p = FALSE;
11936 break;
11937 }
11938 s = h->root.u.def.section;
11939
11940 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
11941 pointer, relative to the address of the tag. */
11942 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
11943 + (b - sdyn->contents));
11944 rld_addr = (s->output_section->vma + s->output_offset
11945 + h->root.u.def.value);
11946 dyn.d_un.d_ptr = rld_addr - dt_addr;
11947 }
11948 break;
11949
11950 case DT_MIPS_OPTIONS:
11951 s = (bfd_get_section_by_name
11952 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11953 dyn.d_un.d_ptr = s->vma;
11954 break;
11955
11956 case DT_PLTREL:
11957 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11958 if (htab->root.target_os == is_vxworks)
11959 dyn.d_un.d_val = DT_RELA;
11960 else
11961 dyn.d_un.d_val = DT_REL;
11962 break;
11963
11964 case DT_PLTRELSZ:
11965 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11966 dyn.d_un.d_val = htab->root.srelplt->size;
11967 break;
11968
11969 case DT_JMPREL:
11970 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11971 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
11972 + htab->root.srelplt->output_offset);
11973 break;
11974
11975 case DT_TEXTREL:
11976 /* If we didn't need any text relocations after all, delete
11977 the dynamic tag. */
11978 if (!(info->flags & DF_TEXTREL))
11979 {
11980 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11981 swap_out_p = FALSE;
11982 }
11983 break;
11984
11985 case DT_FLAGS:
11986 /* If we didn't need any text relocations after all, clear
11987 DF_TEXTREL from DT_FLAGS. */
11988 if (!(info->flags & DF_TEXTREL))
11989 dyn.d_un.d_val &= ~DF_TEXTREL;
11990 else
11991 swap_out_p = FALSE;
11992 break;
11993
11994 case DT_MIPS_XHASH:
11995 name = ".MIPS.xhash";
11996 s = bfd_get_linker_section (dynobj, name);
11997 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11998 break;
11999
12000 default:
12001 swap_out_p = FALSE;
12002 if (htab->root.target_os == is_vxworks
12003 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12004 swap_out_p = TRUE;
12005 break;
12006 }
12007
12008 if (swap_out_p || dyn_skipped)
12009 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12010 (dynobj, &dyn, b - dyn_skipped);
12011
12012 if (dyn_to_skip)
12013 {
12014 dyn_skipped += dyn_to_skip;
12015 dyn_to_skip = 0;
12016 }
12017 }
12018
12019 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
12020 if (dyn_skipped > 0)
12021 memset (b - dyn_skipped, 0, dyn_skipped);
12022 }
12023
12024 if (sgot != NULL && sgot->size > 0
12025 && !bfd_is_abs_section (sgot->output_section))
12026 {
12027 if (htab->root.target_os == is_vxworks)
12028 {
12029 /* The first entry of the global offset table points to the
12030 ".dynamic" section. The second is initialized by the
12031 loader and contains the shared library identifier.
12032 The third is also initialized by the loader and points
12033 to the lazy resolution stub. */
12034 MIPS_ELF_PUT_WORD (output_bfd,
12035 sdyn->output_offset + sdyn->output_section->vma,
12036 sgot->contents);
12037 MIPS_ELF_PUT_WORD (output_bfd, 0,
12038 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12039 MIPS_ELF_PUT_WORD (output_bfd, 0,
12040 sgot->contents
12041 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12042 }
12043 else
12044 {
12045 /* The first entry of the global offset table will be filled at
12046 runtime. The second entry will be used by some runtime loaders.
12047 This isn't the case of IRIX rld. */
12048 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
12049 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12050 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12051 }
12052
12053 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12054 = MIPS_ELF_GOT_SIZE (output_bfd);
12055 }
12056
12057 /* Generate dynamic relocations for the non-primary gots. */
12058 if (gg != NULL && gg->next)
12059 {
12060 Elf_Internal_Rela rel[3];
12061 bfd_vma addend = 0;
12062
12063 memset (rel, 0, sizeof (rel));
12064 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12065
12066 for (g = gg->next; g->next != gg; g = g->next)
12067 {
12068 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
12069 + g->next->tls_gotno;
12070
12071 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
12072 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12073 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12074 sgot->contents
12075 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
12076
12077 if (! bfd_link_pic (info))
12078 continue;
12079
12080 for (; got_index < g->local_gotno; got_index++)
12081 {
12082 if (got_index >= g->assigned_low_gotno
12083 && got_index <= g->assigned_high_gotno)
12084 continue;
12085
12086 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
12087 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
12088 if (!(mips_elf_create_dynamic_relocation
12089 (output_bfd, info, rel, NULL,
12090 bfd_abs_section_ptr,
12091 0, &addend, sgot)))
12092 return FALSE;
12093 BFD_ASSERT (addend == 0);
12094 }
12095 }
12096 }
12097
12098 /* The generation of dynamic relocations for the non-primary gots
12099 adds more dynamic relocations. We cannot count them until
12100 here. */
12101
12102 if (elf_hash_table (info)->dynamic_sections_created)
12103 {
12104 bfd_byte *b;
12105 bfd_boolean swap_out_p;
12106
12107 BFD_ASSERT (sdyn != NULL);
12108
12109 for (b = sdyn->contents;
12110 b < sdyn->contents + sdyn->size;
12111 b += MIPS_ELF_DYN_SIZE (dynobj))
12112 {
12113 Elf_Internal_Dyn dyn;
12114 asection *s;
12115
12116 /* Read in the current dynamic entry. */
12117 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12118
12119 /* Assume that we're going to modify it and write it out. */
12120 swap_out_p = TRUE;
12121
12122 switch (dyn.d_tag)
12123 {
12124 case DT_RELSZ:
12125 /* Reduce DT_RELSZ to account for any relocations we
12126 decided not to make. This is for the n64 irix rld,
12127 which doesn't seem to apply any relocations if there
12128 are trailing null entries. */
12129 s = mips_elf_rel_dyn_section (info, FALSE);
12130 dyn.d_un.d_val = (s->reloc_count
12131 * (ABI_64_P (output_bfd)
12132 ? sizeof (Elf64_Mips_External_Rel)
12133 : sizeof (Elf32_External_Rel)));
12134 /* Adjust the section size too. Tools like the prelinker
12135 can reasonably expect the values to the same. */
12136 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
12137 elf_section_data (s->output_section)->this_hdr.sh_size
12138 = dyn.d_un.d_val;
12139 break;
12140
12141 default:
12142 swap_out_p = FALSE;
12143 break;
12144 }
12145
12146 if (swap_out_p)
12147 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12148 (dynobj, &dyn, b);
12149 }
12150 }
12151
12152 {
12153 asection *s;
12154 Elf32_compact_rel cpt;
12155
12156 if (SGI_COMPAT (output_bfd))
12157 {
12158 /* Write .compact_rel section out. */
12159 s = bfd_get_linker_section (dynobj, ".compact_rel");
12160 if (s != NULL)
12161 {
12162 cpt.id1 = 1;
12163 cpt.num = s->reloc_count;
12164 cpt.id2 = 2;
12165 cpt.offset = (s->output_section->filepos
12166 + sizeof (Elf32_External_compact_rel));
12167 cpt.reserved0 = 0;
12168 cpt.reserved1 = 0;
12169 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12170 ((Elf32_External_compact_rel *)
12171 s->contents));
12172
12173 /* Clean up a dummy stub function entry in .text. */
12174 if (htab->sstubs != NULL)
12175 {
12176 file_ptr dummy_offset;
12177
12178 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12179 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12180 memset (htab->sstubs->contents + dummy_offset, 0,
12181 htab->function_stub_size);
12182 }
12183 }
12184 }
12185
12186 /* The psABI says that the dynamic relocations must be sorted in
12187 increasing order of r_symndx. The VxWorks EABI doesn't require
12188 this, and because the code below handles REL rather than RELA
12189 relocations, using it for VxWorks would be outright harmful. */
12190 if (htab->root.target_os != is_vxworks)
12191 {
12192 s = mips_elf_rel_dyn_section (info, FALSE);
12193 if (s != NULL
12194 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12195 {
12196 reldyn_sorting_bfd = output_bfd;
12197
12198 if (ABI_64_P (output_bfd))
12199 qsort ((Elf64_External_Rel *) s->contents + 1,
12200 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12201 sort_dynamic_relocs_64);
12202 else
12203 qsort ((Elf32_External_Rel *) s->contents + 1,
12204 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12205 sort_dynamic_relocs);
12206 }
12207 }
12208 }
12209
12210 if (htab->root.splt && htab->root.splt->size > 0)
12211 {
12212 if (htab->root.target_os == is_vxworks)
12213 {
12214 if (bfd_link_pic (info))
12215 mips_vxworks_finish_shared_plt (output_bfd, info);
12216 else
12217 mips_vxworks_finish_exec_plt (output_bfd, info);
12218 }
12219 else
12220 {
12221 BFD_ASSERT (!bfd_link_pic (info));
12222 if (!mips_finish_exec_plt (output_bfd, info))
12223 return FALSE;
12224 }
12225 }
12226 return TRUE;
12227 }
12228
12229
12230 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12231
12232 static void
12233 mips_set_isa_flags (bfd *abfd)
12234 {
12235 flagword val;
12236
12237 switch (bfd_get_mach (abfd))
12238 {
12239 default:
12240 if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12241 val = E_MIPS_ARCH_3;
12242 else
12243 val = E_MIPS_ARCH_1;
12244 break;
12245
12246 case bfd_mach_mips3000:
12247 val = E_MIPS_ARCH_1;
12248 break;
12249
12250 case bfd_mach_mips3900:
12251 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12252 break;
12253
12254 case bfd_mach_mips6000:
12255 val = E_MIPS_ARCH_2;
12256 break;
12257
12258 case bfd_mach_mips4010:
12259 val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12260 break;
12261
12262 case bfd_mach_mips4000:
12263 case bfd_mach_mips4300:
12264 case bfd_mach_mips4400:
12265 case bfd_mach_mips4600:
12266 val = E_MIPS_ARCH_3;
12267 break;
12268
12269 case bfd_mach_mips4100:
12270 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12271 break;
12272
12273 case bfd_mach_mips4111:
12274 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12275 break;
12276
12277 case bfd_mach_mips4120:
12278 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12279 break;
12280
12281 case bfd_mach_mips4650:
12282 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12283 break;
12284
12285 case bfd_mach_mips5400:
12286 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12287 break;
12288
12289 case bfd_mach_mips5500:
12290 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12291 break;
12292
12293 case bfd_mach_mips5900:
12294 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12295 break;
12296
12297 case bfd_mach_mips9000:
12298 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12299 break;
12300
12301 case bfd_mach_mips5000:
12302 case bfd_mach_mips7000:
12303 case bfd_mach_mips8000:
12304 case bfd_mach_mips10000:
12305 case bfd_mach_mips12000:
12306 case bfd_mach_mips14000:
12307 case bfd_mach_mips16000:
12308 val = E_MIPS_ARCH_4;
12309 break;
12310
12311 case bfd_mach_mips5:
12312 val = E_MIPS_ARCH_5;
12313 break;
12314
12315 case bfd_mach_mips_loongson_2e:
12316 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12317 break;
12318
12319 case bfd_mach_mips_loongson_2f:
12320 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12321 break;
12322
12323 case bfd_mach_mips_sb1:
12324 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12325 break;
12326
12327 case bfd_mach_mips_gs464:
12328 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
12329 break;
12330
12331 case bfd_mach_mips_gs464e:
12332 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12333 break;
12334
12335 case bfd_mach_mips_gs264e:
12336 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12337 break;
12338
12339 case bfd_mach_mips_octeon:
12340 case bfd_mach_mips_octeonp:
12341 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12342 break;
12343
12344 case bfd_mach_mips_octeon3:
12345 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12346 break;
12347
12348 case bfd_mach_mips_xlr:
12349 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12350 break;
12351
12352 case bfd_mach_mips_octeon2:
12353 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12354 break;
12355
12356 case bfd_mach_mipsisa32:
12357 val = E_MIPS_ARCH_32;
12358 break;
12359
12360 case bfd_mach_mipsisa64:
12361 val = E_MIPS_ARCH_64;
12362 break;
12363
12364 case bfd_mach_mipsisa32r2:
12365 case bfd_mach_mipsisa32r3:
12366 case bfd_mach_mipsisa32r5:
12367 val = E_MIPS_ARCH_32R2;
12368 break;
12369
12370 case bfd_mach_mips_interaptiv_mr2:
12371 val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12372 break;
12373
12374 case bfd_mach_mipsisa64r2:
12375 case bfd_mach_mipsisa64r3:
12376 case bfd_mach_mipsisa64r5:
12377 val = E_MIPS_ARCH_64R2;
12378 break;
12379
12380 case bfd_mach_mipsisa32r6:
12381 val = E_MIPS_ARCH_32R6;
12382 break;
12383
12384 case bfd_mach_mipsisa64r6:
12385 val = E_MIPS_ARCH_64R6;
12386 break;
12387 }
12388 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12389 elf_elfheader (abfd)->e_flags |= val;
12390
12391 }
12392
12393
12394 /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12395 Don't do so for code sections. We want to keep ordering of HI16/LO16
12396 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12397 relocs to be sorted. */
12398
12399 bfd_boolean
12400 _bfd_mips_elf_sort_relocs_p (asection *sec)
12401 {
12402 return (sec->flags & SEC_CODE) == 0;
12403 }
12404
12405
12406 /* The final processing done just before writing out a MIPS ELF object
12407 file. This gets the MIPS architecture right based on the machine
12408 number. This is used by both the 32-bit and the 64-bit ABI. */
12409
12410 void
12411 _bfd_mips_final_write_processing (bfd *abfd)
12412 {
12413 unsigned int i;
12414 Elf_Internal_Shdr **hdrpp;
12415 const char *name;
12416 asection *sec;
12417
12418 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12419 is nonzero. This is for compatibility with old objects, which used
12420 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12421 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12422 mips_set_isa_flags (abfd);
12423
12424 /* Set the sh_info field for .gptab sections and other appropriate
12425 info for each special section. */
12426 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12427 i < elf_numsections (abfd);
12428 i++, hdrpp++)
12429 {
12430 switch ((*hdrpp)->sh_type)
12431 {
12432 case SHT_MIPS_MSYM:
12433 case SHT_MIPS_LIBLIST:
12434 sec = bfd_get_section_by_name (abfd, ".dynstr");
12435 if (sec != NULL)
12436 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12437 break;
12438
12439 case SHT_MIPS_GPTAB:
12440 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12441 name = bfd_section_name ((*hdrpp)->bfd_section);
12442 BFD_ASSERT (name != NULL
12443 && CONST_STRNEQ (name, ".gptab."));
12444 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12445 BFD_ASSERT (sec != NULL);
12446 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12447 break;
12448
12449 case SHT_MIPS_CONTENT:
12450 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12451 name = bfd_section_name ((*hdrpp)->bfd_section);
12452 BFD_ASSERT (name != NULL
12453 && CONST_STRNEQ (name, ".MIPS.content"));
12454 sec = bfd_get_section_by_name (abfd,
12455 name + sizeof ".MIPS.content" - 1);
12456 BFD_ASSERT (sec != NULL);
12457 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12458 break;
12459
12460 case SHT_MIPS_SYMBOL_LIB:
12461 sec = bfd_get_section_by_name (abfd, ".dynsym");
12462 if (sec != NULL)
12463 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12464 sec = bfd_get_section_by_name (abfd, ".liblist");
12465 if (sec != NULL)
12466 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12467 break;
12468
12469 case SHT_MIPS_EVENTS:
12470 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12471 name = bfd_section_name ((*hdrpp)->bfd_section);
12472 BFD_ASSERT (name != NULL);
12473 if (CONST_STRNEQ (name, ".MIPS.events"))
12474 sec = bfd_get_section_by_name (abfd,
12475 name + sizeof ".MIPS.events" - 1);
12476 else
12477 {
12478 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
12479 sec = bfd_get_section_by_name (abfd,
12480 (name
12481 + sizeof ".MIPS.post_rel" - 1));
12482 }
12483 BFD_ASSERT (sec != NULL);
12484 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12485 break;
12486
12487 case SHT_MIPS_XHASH:
12488 sec = bfd_get_section_by_name (abfd, ".dynsym");
12489 if (sec != NULL)
12490 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12491 }
12492 }
12493 }
12494
12495 bfd_boolean
12496 _bfd_mips_elf_final_write_processing (bfd *abfd)
12497 {
12498 _bfd_mips_final_write_processing (abfd);
12499 return _bfd_elf_final_write_processing (abfd);
12500 }
12501 \f
12502 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
12503 segments. */
12504
12505 int
12506 _bfd_mips_elf_additional_program_headers (bfd *abfd,
12507 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12508 {
12509 asection *s;
12510 int ret = 0;
12511
12512 /* See if we need a PT_MIPS_REGINFO segment. */
12513 s = bfd_get_section_by_name (abfd, ".reginfo");
12514 if (s && (s->flags & SEC_LOAD))
12515 ++ret;
12516
12517 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12518 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12519 ++ret;
12520
12521 /* See if we need a PT_MIPS_OPTIONS segment. */
12522 if (IRIX_COMPAT (abfd) == ict_irix6
12523 && bfd_get_section_by_name (abfd,
12524 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12525 ++ret;
12526
12527 /* See if we need a PT_MIPS_RTPROC segment. */
12528 if (IRIX_COMPAT (abfd) == ict_irix5
12529 && bfd_get_section_by_name (abfd, ".dynamic")
12530 && bfd_get_section_by_name (abfd, ".mdebug"))
12531 ++ret;
12532
12533 /* Allocate a PT_NULL header in dynamic objects. See
12534 _bfd_mips_elf_modify_segment_map for details. */
12535 if (!SGI_COMPAT (abfd)
12536 && bfd_get_section_by_name (abfd, ".dynamic"))
12537 ++ret;
12538
12539 return ret;
12540 }
12541
12542 /* Modify the segment map for an IRIX5 executable. */
12543
12544 bfd_boolean
12545 _bfd_mips_elf_modify_segment_map (bfd *abfd,
12546 struct bfd_link_info *info)
12547 {
12548 asection *s;
12549 struct elf_segment_map *m, **pm;
12550 size_t amt;
12551
12552 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12553 segment. */
12554 s = bfd_get_section_by_name (abfd, ".reginfo");
12555 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12556 {
12557 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12558 if (m->p_type == PT_MIPS_REGINFO)
12559 break;
12560 if (m == NULL)
12561 {
12562 amt = sizeof *m;
12563 m = bfd_zalloc (abfd, amt);
12564 if (m == NULL)
12565 return FALSE;
12566
12567 m->p_type = PT_MIPS_REGINFO;
12568 m->count = 1;
12569 m->sections[0] = s;
12570
12571 /* We want to put it after the PHDR and INTERP segments. */
12572 pm = &elf_seg_map (abfd);
12573 while (*pm != NULL
12574 && ((*pm)->p_type == PT_PHDR
12575 || (*pm)->p_type == PT_INTERP))
12576 pm = &(*pm)->next;
12577
12578 m->next = *pm;
12579 *pm = m;
12580 }
12581 }
12582
12583 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12584 segment. */
12585 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12586 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12587 {
12588 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12589 if (m->p_type == PT_MIPS_ABIFLAGS)
12590 break;
12591 if (m == NULL)
12592 {
12593 amt = sizeof *m;
12594 m = bfd_zalloc (abfd, amt);
12595 if (m == NULL)
12596 return FALSE;
12597
12598 m->p_type = PT_MIPS_ABIFLAGS;
12599 m->count = 1;
12600 m->sections[0] = s;
12601
12602 /* We want to put it after the PHDR and INTERP segments. */
12603 pm = &elf_seg_map (abfd);
12604 while (*pm != NULL
12605 && ((*pm)->p_type == PT_PHDR
12606 || (*pm)->p_type == PT_INTERP))
12607 pm = &(*pm)->next;
12608
12609 m->next = *pm;
12610 *pm = m;
12611 }
12612 }
12613
12614 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12615 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
12616 PT_MIPS_OPTIONS segment immediately following the program header
12617 table. */
12618 if (NEWABI_P (abfd)
12619 /* On non-IRIX6 new abi, we'll have already created a segment
12620 for this section, so don't create another. I'm not sure this
12621 is not also the case for IRIX 6, but I can't test it right
12622 now. */
12623 && IRIX_COMPAT (abfd) == ict_irix6)
12624 {
12625 for (s = abfd->sections; s; s = s->next)
12626 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12627 break;
12628
12629 if (s)
12630 {
12631 struct elf_segment_map *options_segment;
12632
12633 pm = &elf_seg_map (abfd);
12634 while (*pm != NULL
12635 && ((*pm)->p_type == PT_PHDR
12636 || (*pm)->p_type == PT_INTERP))
12637 pm = &(*pm)->next;
12638
12639 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12640 {
12641 amt = sizeof (struct elf_segment_map);
12642 options_segment = bfd_zalloc (abfd, amt);
12643 options_segment->next = *pm;
12644 options_segment->p_type = PT_MIPS_OPTIONS;
12645 options_segment->p_flags = PF_R;
12646 options_segment->p_flags_valid = TRUE;
12647 options_segment->count = 1;
12648 options_segment->sections[0] = s;
12649 *pm = options_segment;
12650 }
12651 }
12652 }
12653 else
12654 {
12655 if (IRIX_COMPAT (abfd) == ict_irix5)
12656 {
12657 /* If there are .dynamic and .mdebug sections, we make a room
12658 for the RTPROC header. FIXME: Rewrite without section names. */
12659 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12660 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12661 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12662 {
12663 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12664 if (m->p_type == PT_MIPS_RTPROC)
12665 break;
12666 if (m == NULL)
12667 {
12668 amt = sizeof *m;
12669 m = bfd_zalloc (abfd, amt);
12670 if (m == NULL)
12671 return FALSE;
12672
12673 m->p_type = PT_MIPS_RTPROC;
12674
12675 s = bfd_get_section_by_name (abfd, ".rtproc");
12676 if (s == NULL)
12677 {
12678 m->count = 0;
12679 m->p_flags = 0;
12680 m->p_flags_valid = 1;
12681 }
12682 else
12683 {
12684 m->count = 1;
12685 m->sections[0] = s;
12686 }
12687
12688 /* We want to put it after the DYNAMIC segment. */
12689 pm = &elf_seg_map (abfd);
12690 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12691 pm = &(*pm)->next;
12692 if (*pm != NULL)
12693 pm = &(*pm)->next;
12694
12695 m->next = *pm;
12696 *pm = m;
12697 }
12698 }
12699 }
12700 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12701 .dynstr, .dynsym, and .hash sections, and everything in
12702 between. */
12703 for (pm = &elf_seg_map (abfd); *pm != NULL;
12704 pm = &(*pm)->next)
12705 if ((*pm)->p_type == PT_DYNAMIC)
12706 break;
12707 m = *pm;
12708 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12709 glibc's dynamic linker has traditionally derived the number of
12710 tags from the p_filesz field, and sometimes allocates stack
12711 arrays of that size. An overly-big PT_DYNAMIC segment can
12712 be actively harmful in such cases. Making PT_DYNAMIC contain
12713 other sections can also make life hard for the prelinker,
12714 which might move one of the other sections to a different
12715 PT_LOAD segment. */
12716 if (SGI_COMPAT (abfd)
12717 && m != NULL
12718 && m->count == 1
12719 && strcmp (m->sections[0]->name, ".dynamic") == 0)
12720 {
12721 static const char *sec_names[] =
12722 {
12723 ".dynamic", ".dynstr", ".dynsym", ".hash"
12724 };
12725 bfd_vma low, high;
12726 unsigned int i, c;
12727 struct elf_segment_map *n;
12728
12729 low = ~(bfd_vma) 0;
12730 high = 0;
12731 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12732 {
12733 s = bfd_get_section_by_name (abfd, sec_names[i]);
12734 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12735 {
12736 bfd_size_type sz;
12737
12738 if (low > s->vma)
12739 low = s->vma;
12740 sz = s->size;
12741 if (high < s->vma + sz)
12742 high = s->vma + sz;
12743 }
12744 }
12745
12746 c = 0;
12747 for (s = abfd->sections; s != NULL; s = s->next)
12748 if ((s->flags & SEC_LOAD) != 0
12749 && s->vma >= low
12750 && s->vma + s->size <= high)
12751 ++c;
12752
12753 amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
12754 n = bfd_zalloc (abfd, amt);
12755 if (n == NULL)
12756 return FALSE;
12757 *n = *m;
12758 n->count = c;
12759
12760 i = 0;
12761 for (s = abfd->sections; s != NULL; s = s->next)
12762 {
12763 if ((s->flags & SEC_LOAD) != 0
12764 && s->vma >= low
12765 && s->vma + s->size <= high)
12766 {
12767 n->sections[i] = s;
12768 ++i;
12769 }
12770 }
12771
12772 *pm = n;
12773 }
12774 }
12775
12776 /* Allocate a spare program header in dynamic objects so that tools
12777 like the prelinker can add an extra PT_LOAD entry.
12778
12779 If the prelinker needs to make room for a new PT_LOAD entry, its
12780 standard procedure is to move the first (read-only) sections into
12781 the new (writable) segment. However, the MIPS ABI requires
12782 .dynamic to be in a read-only segment, and the section will often
12783 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12784
12785 Although the prelinker could in principle move .dynamic to a
12786 writable segment, it seems better to allocate a spare program
12787 header instead, and avoid the need to move any sections.
12788 There is a long tradition of allocating spare dynamic tags,
12789 so allocating a spare program header seems like a natural
12790 extension.
12791
12792 If INFO is NULL, we may be copying an already prelinked binary
12793 with objcopy or strip, so do not add this header. */
12794 if (info != NULL
12795 && !SGI_COMPAT (abfd)
12796 && bfd_get_section_by_name (abfd, ".dynamic"))
12797 {
12798 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12799 if ((*pm)->p_type == PT_NULL)
12800 break;
12801 if (*pm == NULL)
12802 {
12803 m = bfd_zalloc (abfd, sizeof (*m));
12804 if (m == NULL)
12805 return FALSE;
12806
12807 m->p_type = PT_NULL;
12808 *pm = m;
12809 }
12810 }
12811
12812 return TRUE;
12813 }
12814 \f
12815 /* Return the section that should be marked against GC for a given
12816 relocation. */
12817
12818 asection *
12819 _bfd_mips_elf_gc_mark_hook (asection *sec,
12820 struct bfd_link_info *info,
12821 Elf_Internal_Rela *rel,
12822 struct elf_link_hash_entry *h,
12823 Elf_Internal_Sym *sym)
12824 {
12825 /* ??? Do mips16 stub sections need to be handled special? */
12826
12827 if (h != NULL)
12828 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12829 {
12830 case R_MIPS_GNU_VTINHERIT:
12831 case R_MIPS_GNU_VTENTRY:
12832 return NULL;
12833 }
12834
12835 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12836 }
12837
12838 /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12839
12840 bfd_boolean
12841 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12842 elf_gc_mark_hook_fn gc_mark_hook)
12843 {
12844 bfd *sub;
12845
12846 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12847
12848 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12849 {
12850 asection *o;
12851
12852 if (! is_mips_elf (sub))
12853 continue;
12854
12855 for (o = sub->sections; o != NULL; o = o->next)
12856 if (!o->gc_mark
12857 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
12858 {
12859 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12860 return FALSE;
12861 }
12862 }
12863
12864 return TRUE;
12865 }
12866 \f
12867 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12868 hiding the old indirect symbol. Process additional relocation
12869 information. Also called for weakdefs, in which case we just let
12870 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12871
12872 void
12873 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12874 struct elf_link_hash_entry *dir,
12875 struct elf_link_hash_entry *ind)
12876 {
12877 struct mips_elf_link_hash_entry *dirmips, *indmips;
12878
12879 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12880
12881 dirmips = (struct mips_elf_link_hash_entry *) dir;
12882 indmips = (struct mips_elf_link_hash_entry *) ind;
12883 /* Any absolute non-dynamic relocations against an indirect or weak
12884 definition will be against the target symbol. */
12885 if (indmips->has_static_relocs)
12886 dirmips->has_static_relocs = TRUE;
12887
12888 if (ind->root.type != bfd_link_hash_indirect)
12889 return;
12890
12891 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12892 if (indmips->readonly_reloc)
12893 dirmips->readonly_reloc = TRUE;
12894 if (indmips->no_fn_stub)
12895 dirmips->no_fn_stub = TRUE;
12896 if (indmips->fn_stub)
12897 {
12898 dirmips->fn_stub = indmips->fn_stub;
12899 indmips->fn_stub = NULL;
12900 }
12901 if (indmips->need_fn_stub)
12902 {
12903 dirmips->need_fn_stub = TRUE;
12904 indmips->need_fn_stub = FALSE;
12905 }
12906 if (indmips->call_stub)
12907 {
12908 dirmips->call_stub = indmips->call_stub;
12909 indmips->call_stub = NULL;
12910 }
12911 if (indmips->call_fp_stub)
12912 {
12913 dirmips->call_fp_stub = indmips->call_fp_stub;
12914 indmips->call_fp_stub = NULL;
12915 }
12916 if (indmips->global_got_area < dirmips->global_got_area)
12917 dirmips->global_got_area = indmips->global_got_area;
12918 if (indmips->global_got_area < GGA_NONE)
12919 indmips->global_got_area = GGA_NONE;
12920 if (indmips->has_nonpic_branches)
12921 dirmips->has_nonpic_branches = TRUE;
12922 }
12923
12924 /* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
12925 to hide it. It has to remain global (it will also be protected) so as to
12926 be assigned a global GOT entry, which will then remain unchanged at load
12927 time. */
12928
12929 void
12930 _bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
12931 struct elf_link_hash_entry *entry,
12932 bfd_boolean force_local)
12933 {
12934 struct mips_elf_link_hash_table *htab;
12935
12936 htab = mips_elf_hash_table (info);
12937 BFD_ASSERT (htab != NULL);
12938 if (htab->use_absolute_zero
12939 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
12940 return;
12941
12942 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
12943 }
12944 \f
12945 #define PDR_SIZE 32
12946
12947 bfd_boolean
12948 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12949 struct bfd_link_info *info)
12950 {
12951 asection *o;
12952 bfd_boolean ret = FALSE;
12953 unsigned char *tdata;
12954 size_t i, skip;
12955
12956 o = bfd_get_section_by_name (abfd, ".pdr");
12957 if (! o)
12958 return FALSE;
12959 if (o->size == 0)
12960 return FALSE;
12961 if (o->size % PDR_SIZE != 0)
12962 return FALSE;
12963 if (o->output_section != NULL
12964 && bfd_is_abs_section (o->output_section))
12965 return FALSE;
12966
12967 tdata = bfd_zmalloc (o->size / PDR_SIZE);
12968 if (! tdata)
12969 return FALSE;
12970
12971 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
12972 info->keep_memory);
12973 if (!cookie->rels)
12974 {
12975 free (tdata);
12976 return FALSE;
12977 }
12978
12979 cookie->rel = cookie->rels;
12980 cookie->relend = cookie->rels + o->reloc_count;
12981
12982 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
12983 {
12984 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
12985 {
12986 tdata[i] = 1;
12987 skip ++;
12988 }
12989 }
12990
12991 if (skip != 0)
12992 {
12993 mips_elf_section_data (o)->u.tdata = tdata;
12994 if (o->rawsize == 0)
12995 o->rawsize = o->size;
12996 o->size -= skip * PDR_SIZE;
12997 ret = TRUE;
12998 }
12999 else
13000 free (tdata);
13001
13002 if (! info->keep_memory)
13003 free (cookie->rels);
13004
13005 return ret;
13006 }
13007
13008 bfd_boolean
13009 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
13010 {
13011 if (strcmp (sec->name, ".pdr") == 0)
13012 return TRUE;
13013 return FALSE;
13014 }
13015
13016 bfd_boolean
13017 _bfd_mips_elf_write_section (bfd *output_bfd,
13018 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
13019 asection *sec, bfd_byte *contents)
13020 {
13021 bfd_byte *to, *from, *end;
13022 int i;
13023
13024 if (strcmp (sec->name, ".pdr") != 0)
13025 return FALSE;
13026
13027 if (mips_elf_section_data (sec)->u.tdata == NULL)
13028 return FALSE;
13029
13030 to = contents;
13031 end = contents + sec->size;
13032 for (from = contents, i = 0;
13033 from < end;
13034 from += PDR_SIZE, i++)
13035 {
13036 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
13037 continue;
13038 if (to != from)
13039 memcpy (to, from, PDR_SIZE);
13040 to += PDR_SIZE;
13041 }
13042 bfd_set_section_contents (output_bfd, sec->output_section, contents,
13043 sec->output_offset, sec->size);
13044 return TRUE;
13045 }
13046 \f
13047 /* microMIPS code retains local labels for linker relaxation. Omit them
13048 from output by default for clarity. */
13049
13050 bfd_boolean
13051 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13052 {
13053 return _bfd_elf_is_local_label_name (abfd, sym->name);
13054 }
13055
13056 /* MIPS ELF uses a special find_nearest_line routine in order the
13057 handle the ECOFF debugging information. */
13058
13059 struct mips_elf_find_line
13060 {
13061 struct ecoff_debug_info d;
13062 struct ecoff_find_line i;
13063 };
13064
13065 bfd_boolean
13066 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13067 asection *section, bfd_vma offset,
13068 const char **filename_ptr,
13069 const char **functionname_ptr,
13070 unsigned int *line_ptr,
13071 unsigned int *discriminator_ptr)
13072 {
13073 asection *msec;
13074
13075 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
13076 filename_ptr, functionname_ptr,
13077 line_ptr, discriminator_ptr,
13078 dwarf_debug_sections,
13079 &elf_tdata (abfd)->dwarf2_find_line_info)
13080 == 1)
13081 return TRUE;
13082
13083 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13084 filename_ptr, functionname_ptr,
13085 line_ptr))
13086 {
13087 if (!*functionname_ptr)
13088 _bfd_elf_find_function (abfd, symbols, section, offset,
13089 *filename_ptr ? NULL : filename_ptr,
13090 functionname_ptr);
13091 return TRUE;
13092 }
13093
13094 msec = bfd_get_section_by_name (abfd, ".mdebug");
13095 if (msec != NULL)
13096 {
13097 flagword origflags;
13098 struct mips_elf_find_line *fi;
13099 const struct ecoff_debug_swap * const swap =
13100 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13101
13102 /* If we are called during a link, mips_elf_final_link may have
13103 cleared the SEC_HAS_CONTENTS field. We force it back on here
13104 if appropriate (which it normally will be). */
13105 origflags = msec->flags;
13106 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13107 msec->flags |= SEC_HAS_CONTENTS;
13108
13109 fi = mips_elf_tdata (abfd)->find_line_info;
13110 if (fi == NULL)
13111 {
13112 bfd_size_type external_fdr_size;
13113 char *fraw_src;
13114 char *fraw_end;
13115 struct fdr *fdr_ptr;
13116 bfd_size_type amt = sizeof (struct mips_elf_find_line);
13117
13118 fi = bfd_zalloc (abfd, amt);
13119 if (fi == NULL)
13120 {
13121 msec->flags = origflags;
13122 return FALSE;
13123 }
13124
13125 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13126 {
13127 msec->flags = origflags;
13128 return FALSE;
13129 }
13130
13131 /* Swap in the FDR information. */
13132 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
13133 fi->d.fdr = bfd_alloc (abfd, amt);
13134 if (fi->d.fdr == NULL)
13135 {
13136 msec->flags = origflags;
13137 return FALSE;
13138 }
13139 external_fdr_size = swap->external_fdr_size;
13140 fdr_ptr = fi->d.fdr;
13141 fraw_src = (char *) fi->d.external_fdr;
13142 fraw_end = (fraw_src
13143 + fi->d.symbolic_header.ifdMax * external_fdr_size);
13144 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
13145 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
13146
13147 mips_elf_tdata (abfd)->find_line_info = fi;
13148
13149 /* Note that we don't bother to ever free this information.
13150 find_nearest_line is either called all the time, as in
13151 objdump -l, so the information should be saved, or it is
13152 rarely called, as in ld error messages, so the memory
13153 wasted is unimportant. Still, it would probably be a
13154 good idea for free_cached_info to throw it away. */
13155 }
13156
13157 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13158 &fi->i, filename_ptr, functionname_ptr,
13159 line_ptr))
13160 {
13161 msec->flags = origflags;
13162 return TRUE;
13163 }
13164
13165 msec->flags = origflags;
13166 }
13167
13168 /* Fall back on the generic ELF find_nearest_line routine. */
13169
13170 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
13171 filename_ptr, functionname_ptr,
13172 line_ptr, discriminator_ptr);
13173 }
13174
13175 bfd_boolean
13176 _bfd_mips_elf_find_inliner_info (bfd *abfd,
13177 const char **filename_ptr,
13178 const char **functionname_ptr,
13179 unsigned int *line_ptr)
13180 {
13181 bfd_boolean found;
13182 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13183 functionname_ptr, line_ptr,
13184 & elf_tdata (abfd)->dwarf2_find_line_info);
13185 return found;
13186 }
13187
13188 \f
13189 /* When are writing out the .options or .MIPS.options section,
13190 remember the bytes we are writing out, so that we can install the
13191 GP value in the section_processing routine. */
13192
13193 bfd_boolean
13194 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13195 const void *location,
13196 file_ptr offset, bfd_size_type count)
13197 {
13198 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
13199 {
13200 bfd_byte *c;
13201
13202 if (elf_section_data (section) == NULL)
13203 {
13204 size_t amt = sizeof (struct bfd_elf_section_data);
13205 section->used_by_bfd = bfd_zalloc (abfd, amt);
13206 if (elf_section_data (section) == NULL)
13207 return FALSE;
13208 }
13209 c = mips_elf_section_data (section)->u.tdata;
13210 if (c == NULL)
13211 {
13212 c = bfd_zalloc (abfd, section->size);
13213 if (c == NULL)
13214 return FALSE;
13215 mips_elf_section_data (section)->u.tdata = c;
13216 }
13217
13218 memcpy (c + offset, location, count);
13219 }
13220
13221 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13222 count);
13223 }
13224
13225 /* This is almost identical to bfd_generic_get_... except that some
13226 MIPS relocations need to be handled specially. Sigh. */
13227
13228 bfd_byte *
13229 _bfd_elf_mips_get_relocated_section_contents
13230 (bfd *abfd,
13231 struct bfd_link_info *link_info,
13232 struct bfd_link_order *link_order,
13233 bfd_byte *data,
13234 bfd_boolean relocatable,
13235 asymbol **symbols)
13236 {
13237 /* Get enough memory to hold the stuff */
13238 bfd *input_bfd = link_order->u.indirect.section->owner;
13239 asection *input_section = link_order->u.indirect.section;
13240 bfd_size_type sz;
13241
13242 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13243 arelent **reloc_vector = NULL;
13244 long reloc_count;
13245
13246 if (reloc_size < 0)
13247 goto error_return;
13248
13249 reloc_vector = bfd_malloc (reloc_size);
13250 if (reloc_vector == NULL && reloc_size != 0)
13251 goto error_return;
13252
13253 /* read in the section */
13254 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
13255 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
13256 goto error_return;
13257
13258 reloc_count = bfd_canonicalize_reloc (input_bfd,
13259 input_section,
13260 reloc_vector,
13261 symbols);
13262 if (reloc_count < 0)
13263 goto error_return;
13264
13265 if (reloc_count > 0)
13266 {
13267 arelent **parent;
13268 /* for mips */
13269 int gp_found;
13270 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13271
13272 {
13273 struct bfd_hash_entry *h;
13274 struct bfd_link_hash_entry *lh;
13275 /* Skip all this stuff if we aren't mixing formats. */
13276 if (abfd && input_bfd
13277 && abfd->xvec == input_bfd->xvec)
13278 lh = 0;
13279 else
13280 {
13281 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
13282 lh = (struct bfd_link_hash_entry *) h;
13283 }
13284 lookup:
13285 if (lh)
13286 {
13287 switch (lh->type)
13288 {
13289 case bfd_link_hash_undefined:
13290 case bfd_link_hash_undefweak:
13291 case bfd_link_hash_common:
13292 gp_found = 0;
13293 break;
13294 case bfd_link_hash_defined:
13295 case bfd_link_hash_defweak:
13296 gp_found = 1;
13297 gp = lh->u.def.value;
13298 break;
13299 case bfd_link_hash_indirect:
13300 case bfd_link_hash_warning:
13301 lh = lh->u.i.link;
13302 /* @@FIXME ignoring warning for now */
13303 goto lookup;
13304 case bfd_link_hash_new:
13305 default:
13306 abort ();
13307 }
13308 }
13309 else
13310 gp_found = 0;
13311 }
13312 /* end mips */
13313 for (parent = reloc_vector; *parent != NULL; parent++)
13314 {
13315 char *error_message = NULL;
13316 bfd_reloc_status_type r;
13317
13318 /* Specific to MIPS: Deal with relocation types that require
13319 knowing the gp of the output bfd. */
13320 asymbol *sym = *(*parent)->sym_ptr_ptr;
13321
13322 /* If we've managed to find the gp and have a special
13323 function for the relocation then go ahead, else default
13324 to the generic handling. */
13325 if (gp_found
13326 && (*parent)->howto->special_function
13327 == _bfd_mips_elf32_gprel16_reloc)
13328 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
13329 input_section, relocatable,
13330 data, gp);
13331 else
13332 r = bfd_perform_relocation (input_bfd, *parent, data,
13333 input_section,
13334 relocatable ? abfd : NULL,
13335 &error_message);
13336
13337 if (relocatable)
13338 {
13339 asection *os = input_section->output_section;
13340
13341 /* A partial link, so keep the relocs */
13342 os->orelocation[os->reloc_count] = *parent;
13343 os->reloc_count++;
13344 }
13345
13346 if (r != bfd_reloc_ok)
13347 {
13348 switch (r)
13349 {
13350 case bfd_reloc_undefined:
13351 (*link_info->callbacks->undefined_symbol)
13352 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13353 input_bfd, input_section, (*parent)->address, TRUE);
13354 break;
13355 case bfd_reloc_dangerous:
13356 BFD_ASSERT (error_message != NULL);
13357 (*link_info->callbacks->reloc_dangerous)
13358 (link_info, error_message,
13359 input_bfd, input_section, (*parent)->address);
13360 break;
13361 case bfd_reloc_overflow:
13362 (*link_info->callbacks->reloc_overflow)
13363 (link_info, NULL,
13364 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13365 (*parent)->howto->name, (*parent)->addend,
13366 input_bfd, input_section, (*parent)->address);
13367 break;
13368 case bfd_reloc_outofrange:
13369 default:
13370 abort ();
13371 break;
13372 }
13373
13374 }
13375 }
13376 }
13377 free (reloc_vector);
13378 return data;
13379
13380 error_return:
13381 free (reloc_vector);
13382 return NULL;
13383 }
13384 \f
13385 static bfd_boolean
13386 mips_elf_relax_delete_bytes (bfd *abfd,
13387 asection *sec, bfd_vma addr, int count)
13388 {
13389 Elf_Internal_Shdr *symtab_hdr;
13390 unsigned int sec_shndx;
13391 bfd_byte *contents;
13392 Elf_Internal_Rela *irel, *irelend;
13393 Elf_Internal_Sym *isym;
13394 Elf_Internal_Sym *isymend;
13395 struct elf_link_hash_entry **sym_hashes;
13396 struct elf_link_hash_entry **end_hashes;
13397 struct elf_link_hash_entry **start_hashes;
13398 unsigned int symcount;
13399
13400 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13401 contents = elf_section_data (sec)->this_hdr.contents;
13402
13403 irel = elf_section_data (sec)->relocs;
13404 irelend = irel + sec->reloc_count;
13405
13406 /* Actually delete the bytes. */
13407 memmove (contents + addr, contents + addr + count,
13408 (size_t) (sec->size - addr - count));
13409 sec->size -= count;
13410
13411 /* Adjust all the relocs. */
13412 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13413 {
13414 /* Get the new reloc address. */
13415 if (irel->r_offset > addr)
13416 irel->r_offset -= count;
13417 }
13418
13419 BFD_ASSERT (addr % 2 == 0);
13420 BFD_ASSERT (count % 2 == 0);
13421
13422 /* Adjust the local symbols defined in this section. */
13423 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13424 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13425 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13426 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13427 isym->st_value -= count;
13428
13429 /* Now adjust the global symbols defined in this section. */
13430 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13431 - symtab_hdr->sh_info);
13432 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13433 end_hashes = sym_hashes + symcount;
13434
13435 for (; sym_hashes < end_hashes; sym_hashes++)
13436 {
13437 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13438
13439 if ((sym_hash->root.type == bfd_link_hash_defined
13440 || sym_hash->root.type == bfd_link_hash_defweak)
13441 && sym_hash->root.u.def.section == sec)
13442 {
13443 bfd_vma value = sym_hash->root.u.def.value;
13444
13445 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13446 value &= MINUS_TWO;
13447 if (value > addr)
13448 sym_hash->root.u.def.value -= count;
13449 }
13450 }
13451
13452 return TRUE;
13453 }
13454
13455
13456 /* Opcodes needed for microMIPS relaxation as found in
13457 opcodes/micromips-opc.c. */
13458
13459 struct opcode_descriptor {
13460 unsigned long match;
13461 unsigned long mask;
13462 };
13463
13464 /* The $ra register aka $31. */
13465
13466 #define RA 31
13467
13468 /* 32-bit instruction format register fields. */
13469
13470 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13471 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13472
13473 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
13474
13475 #define OP16_VALID_REG(r) \
13476 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13477
13478
13479 /* 32-bit and 16-bit branches. */
13480
13481 static const struct opcode_descriptor b_insns_32[] = {
13482 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13483 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13484 { 0, 0 } /* End marker for find_match(). */
13485 };
13486
13487 static const struct opcode_descriptor bc_insn_32 =
13488 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13489
13490 static const struct opcode_descriptor bz_insn_32 =
13491 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13492
13493 static const struct opcode_descriptor bzal_insn_32 =
13494 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13495
13496 static const struct opcode_descriptor beq_insn_32 =
13497 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13498
13499 static const struct opcode_descriptor b_insn_16 =
13500 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13501
13502 static const struct opcode_descriptor bz_insn_16 =
13503 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
13504
13505
13506 /* 32-bit and 16-bit branch EQ and NE zero. */
13507
13508 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13509 eq and second the ne. This convention is used when replacing a
13510 32-bit BEQ/BNE with the 16-bit version. */
13511
13512 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13513
13514 static const struct opcode_descriptor bz_rs_insns_32[] = {
13515 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13516 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13517 { 0, 0 } /* End marker for find_match(). */
13518 };
13519
13520 static const struct opcode_descriptor bz_rt_insns_32[] = {
13521 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13522 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13523 { 0, 0 } /* End marker for find_match(). */
13524 };
13525
13526 static const struct opcode_descriptor bzc_insns_32[] = {
13527 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13528 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13529 { 0, 0 } /* End marker for find_match(). */
13530 };
13531
13532 static const struct opcode_descriptor bz_insns_16[] = {
13533 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13534 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13535 { 0, 0 } /* End marker for find_match(). */
13536 };
13537
13538 /* Switch between a 5-bit register index and its 3-bit shorthand. */
13539
13540 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
13541 #define BZ16_REG_FIELD(r) (((r) & 7) << 7)
13542
13543
13544 /* 32-bit instructions with a delay slot. */
13545
13546 static const struct opcode_descriptor jal_insn_32_bd16 =
13547 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13548
13549 static const struct opcode_descriptor jal_insn_32_bd32 =
13550 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13551
13552 static const struct opcode_descriptor jal_x_insn_32_bd32 =
13553 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13554
13555 static const struct opcode_descriptor j_insn_32 =
13556 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13557
13558 static const struct opcode_descriptor jalr_insn_32 =
13559 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13560
13561 /* This table can be compacted, because no opcode replacement is made. */
13562
13563 static const struct opcode_descriptor ds_insns_32_bd16[] = {
13564 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13565
13566 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13567 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13568
13569 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13570 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13571 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13572 { 0, 0 } /* End marker for find_match(). */
13573 };
13574
13575 /* This table can be compacted, because no opcode replacement is made. */
13576
13577 static const struct opcode_descriptor ds_insns_32_bd32[] = {
13578 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13579
13580 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13581 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13582 { 0, 0 } /* End marker for find_match(). */
13583 };
13584
13585
13586 /* 16-bit instructions with a delay slot. */
13587
13588 static const struct opcode_descriptor jalr_insn_16_bd16 =
13589 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13590
13591 static const struct opcode_descriptor jalr_insn_16_bd32 =
13592 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13593
13594 static const struct opcode_descriptor jr_insn_16 =
13595 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13596
13597 #define JR16_REG(opcode) ((opcode) & 0x1f)
13598
13599 /* This table can be compacted, because no opcode replacement is made. */
13600
13601 static const struct opcode_descriptor ds_insns_16_bd16[] = {
13602 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13603
13604 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13605 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13606 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13607 { 0, 0 } /* End marker for find_match(). */
13608 };
13609
13610
13611 /* LUI instruction. */
13612
13613 static const struct opcode_descriptor lui_insn =
13614 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13615
13616
13617 /* ADDIU instruction. */
13618
13619 static const struct opcode_descriptor addiu_insn =
13620 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13621
13622 static const struct opcode_descriptor addiupc_insn =
13623 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13624
13625 #define ADDIUPC_REG_FIELD(r) \
13626 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13627
13628
13629 /* Relaxable instructions in a JAL delay slot: MOVE. */
13630
13631 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13632 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13633 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13634 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13635
13636 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13637 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13638
13639 static const struct opcode_descriptor move_insns_32[] = {
13640 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
13641 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13642 { 0, 0 } /* End marker for find_match(). */
13643 };
13644
13645 static const struct opcode_descriptor move_insn_16 =
13646 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13647
13648
13649 /* NOP instructions. */
13650
13651 static const struct opcode_descriptor nop_insn_32 =
13652 { /* "nop", "", */ 0x00000000, 0xffffffff };
13653
13654 static const struct opcode_descriptor nop_insn_16 =
13655 { /* "nop", "", */ 0x0c00, 0xffff };
13656
13657
13658 /* Instruction match support. */
13659
13660 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13661
13662 static int
13663 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13664 {
13665 unsigned long indx;
13666
13667 for (indx = 0; insn[indx].mask != 0; indx++)
13668 if (MATCH (opcode, insn[indx]))
13669 return indx;
13670
13671 return -1;
13672 }
13673
13674
13675 /* Branch and delay slot decoding support. */
13676
13677 /* If PTR points to what *might* be a 16-bit branch or jump, then
13678 return the minimum length of its delay slot, otherwise return 0.
13679 Non-zero results are not definitive as we might be checking against
13680 the second half of another instruction. */
13681
13682 static int
13683 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13684 {
13685 unsigned long opcode;
13686 int bdsize;
13687
13688 opcode = bfd_get_16 (abfd, ptr);
13689 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13690 /* 16-bit branch/jump with a 32-bit delay slot. */
13691 bdsize = 4;
13692 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13693 || find_match (opcode, ds_insns_16_bd16) >= 0)
13694 /* 16-bit branch/jump with a 16-bit delay slot. */
13695 bdsize = 2;
13696 else
13697 /* No delay slot. */
13698 bdsize = 0;
13699
13700 return bdsize;
13701 }
13702
13703 /* If PTR points to what *might* be a 32-bit branch or jump, then
13704 return the minimum length of its delay slot, otherwise return 0.
13705 Non-zero results are not definitive as we might be checking against
13706 the second half of another instruction. */
13707
13708 static int
13709 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13710 {
13711 unsigned long opcode;
13712 int bdsize;
13713
13714 opcode = bfd_get_micromips_32 (abfd, ptr);
13715 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13716 /* 32-bit branch/jump with a 32-bit delay slot. */
13717 bdsize = 4;
13718 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13719 /* 32-bit branch/jump with a 16-bit delay slot. */
13720 bdsize = 2;
13721 else
13722 /* No delay slot. */
13723 bdsize = 0;
13724
13725 return bdsize;
13726 }
13727
13728 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13729 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13730
13731 static bfd_boolean
13732 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13733 {
13734 unsigned long opcode;
13735
13736 opcode = bfd_get_16 (abfd, ptr);
13737 if (MATCH (opcode, b_insn_16)
13738 /* B16 */
13739 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13740 /* JR16 */
13741 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13742 /* BEQZ16, BNEZ16 */
13743 || (MATCH (opcode, jalr_insn_16_bd32)
13744 /* JALR16 */
13745 && reg != JR16_REG (opcode) && reg != RA))
13746 return TRUE;
13747
13748 return FALSE;
13749 }
13750
13751 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13752 then return TRUE, otherwise FALSE. */
13753
13754 static bfd_boolean
13755 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13756 {
13757 unsigned long opcode;
13758
13759 opcode = bfd_get_micromips_32 (abfd, ptr);
13760 if (MATCH (opcode, j_insn_32)
13761 /* J */
13762 || MATCH (opcode, bc_insn_32)
13763 /* BC1F, BC1T, BC2F, BC2T */
13764 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13765 /* JAL, JALX */
13766 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13767 /* BGEZ, BGTZ, BLEZ, BLTZ */
13768 || (MATCH (opcode, bzal_insn_32)
13769 /* BGEZAL, BLTZAL */
13770 && reg != OP32_SREG (opcode) && reg != RA)
13771 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13772 /* JALR, JALR.HB, BEQ, BNE */
13773 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13774 return TRUE;
13775
13776 return FALSE;
13777 }
13778
13779 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13780 IRELEND) at OFFSET indicate that there must be a compact branch there,
13781 then return TRUE, otherwise FALSE. */
13782
13783 static bfd_boolean
13784 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13785 const Elf_Internal_Rela *internal_relocs,
13786 const Elf_Internal_Rela *irelend)
13787 {
13788 const Elf_Internal_Rela *irel;
13789 unsigned long opcode;
13790
13791 opcode = bfd_get_micromips_32 (abfd, ptr);
13792 if (find_match (opcode, bzc_insns_32) < 0)
13793 return FALSE;
13794
13795 for (irel = internal_relocs; irel < irelend; irel++)
13796 if (irel->r_offset == offset
13797 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13798 return TRUE;
13799
13800 return FALSE;
13801 }
13802
13803 /* Bitsize checking. */
13804 #define IS_BITSIZE(val, N) \
13805 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13806 - (1ULL << ((N) - 1))) == (val))
13807
13808 \f
13809 bfd_boolean
13810 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13811 struct bfd_link_info *link_info,
13812 bfd_boolean *again)
13813 {
13814 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
13815 Elf_Internal_Shdr *symtab_hdr;
13816 Elf_Internal_Rela *internal_relocs;
13817 Elf_Internal_Rela *irel, *irelend;
13818 bfd_byte *contents = NULL;
13819 Elf_Internal_Sym *isymbuf = NULL;
13820
13821 /* Assume nothing changes. */
13822 *again = FALSE;
13823
13824 /* We don't have to do anything for a relocatable link, if
13825 this section does not have relocs, or if this is not a
13826 code section. */
13827
13828 if (bfd_link_relocatable (link_info)
13829 || (sec->flags & SEC_RELOC) == 0
13830 || sec->reloc_count == 0
13831 || (sec->flags & SEC_CODE) == 0)
13832 return TRUE;
13833
13834 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13835
13836 /* Get a copy of the native relocations. */
13837 internal_relocs = (_bfd_elf_link_read_relocs
13838 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
13839 link_info->keep_memory));
13840 if (internal_relocs == NULL)
13841 goto error_return;
13842
13843 /* Walk through them looking for relaxing opportunities. */
13844 irelend = internal_relocs + sec->reloc_count;
13845 for (irel = internal_relocs; irel < irelend; irel++)
13846 {
13847 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13848 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13849 bfd_boolean target_is_micromips_code_p;
13850 unsigned long opcode;
13851 bfd_vma symval;
13852 bfd_vma pcrval;
13853 bfd_byte *ptr;
13854 int fndopc;
13855
13856 /* The number of bytes to delete for relaxation and from where
13857 to delete these bytes starting at irel->r_offset. */
13858 int delcnt = 0;
13859 int deloff = 0;
13860
13861 /* If this isn't something that can be relaxed, then ignore
13862 this reloc. */
13863 if (r_type != R_MICROMIPS_HI16
13864 && r_type != R_MICROMIPS_PC16_S1
13865 && r_type != R_MICROMIPS_26_S1)
13866 continue;
13867
13868 /* Get the section contents if we haven't done so already. */
13869 if (contents == NULL)
13870 {
13871 /* Get cached copy if it exists. */
13872 if (elf_section_data (sec)->this_hdr.contents != NULL)
13873 contents = elf_section_data (sec)->this_hdr.contents;
13874 /* Go get them off disk. */
13875 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13876 goto error_return;
13877 }
13878 ptr = contents + irel->r_offset;
13879
13880 /* Read this BFD's local symbols if we haven't done so already. */
13881 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13882 {
13883 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13884 if (isymbuf == NULL)
13885 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13886 symtab_hdr->sh_info, 0,
13887 NULL, NULL, NULL);
13888 if (isymbuf == NULL)
13889 goto error_return;
13890 }
13891
13892 /* Get the value of the symbol referred to by the reloc. */
13893 if (r_symndx < symtab_hdr->sh_info)
13894 {
13895 /* A local symbol. */
13896 Elf_Internal_Sym *isym;
13897 asection *sym_sec;
13898
13899 isym = isymbuf + r_symndx;
13900 if (isym->st_shndx == SHN_UNDEF)
13901 sym_sec = bfd_und_section_ptr;
13902 else if (isym->st_shndx == SHN_ABS)
13903 sym_sec = bfd_abs_section_ptr;
13904 else if (isym->st_shndx == SHN_COMMON)
13905 sym_sec = bfd_com_section_ptr;
13906 else
13907 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13908 symval = (isym->st_value
13909 + sym_sec->output_section->vma
13910 + sym_sec->output_offset);
13911 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13912 }
13913 else
13914 {
13915 unsigned long indx;
13916 struct elf_link_hash_entry *h;
13917
13918 /* An external symbol. */
13919 indx = r_symndx - symtab_hdr->sh_info;
13920 h = elf_sym_hashes (abfd)[indx];
13921 BFD_ASSERT (h != NULL);
13922
13923 if (h->root.type != bfd_link_hash_defined
13924 && h->root.type != bfd_link_hash_defweak)
13925 /* This appears to be a reference to an undefined
13926 symbol. Just ignore it -- it will be caught by the
13927 regular reloc processing. */
13928 continue;
13929
13930 symval = (h->root.u.def.value
13931 + h->root.u.def.section->output_section->vma
13932 + h->root.u.def.section->output_offset);
13933 target_is_micromips_code_p = (!h->needs_plt
13934 && ELF_ST_IS_MICROMIPS (h->other));
13935 }
13936
13937
13938 /* For simplicity of coding, we are going to modify the
13939 section contents, the section relocs, and the BFD symbol
13940 table. We must tell the rest of the code not to free up this
13941 information. It would be possible to instead create a table
13942 of changes which have to be made, as is done in coff-mips.c;
13943 that would be more work, but would require less memory when
13944 the linker is run. */
13945
13946 /* Only 32-bit instructions relaxed. */
13947 if (irel->r_offset + 4 > sec->size)
13948 continue;
13949
13950 opcode = bfd_get_micromips_32 (abfd, ptr);
13951
13952 /* This is the pc-relative distance from the instruction the
13953 relocation is applied to, to the symbol referred. */
13954 pcrval = (symval
13955 - (sec->output_section->vma + sec->output_offset)
13956 - irel->r_offset);
13957
13958 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13959 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13960 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13961
13962 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13963
13964 where pcrval has first to be adjusted to apply against the LO16
13965 location (we make the adjustment later on, when we have figured
13966 out the offset). */
13967 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13968 {
13969 bfd_boolean bzc = FALSE;
13970 unsigned long nextopc;
13971 unsigned long reg;
13972 bfd_vma offset;
13973
13974 /* Give up if the previous reloc was a HI16 against this symbol
13975 too. */
13976 if (irel > internal_relocs
13977 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13978 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13979 continue;
13980
13981 /* Or if the next reloc is not a LO16 against this symbol. */
13982 if (irel + 1 >= irelend
13983 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13984 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13985 continue;
13986
13987 /* Or if the second next reloc is a LO16 against this symbol too. */
13988 if (irel + 2 >= irelend
13989 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13990 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13991 continue;
13992
13993 /* See if the LUI instruction *might* be in a branch delay slot.
13994 We check whether what looks like a 16-bit branch or jump is
13995 actually an immediate argument to a compact branch, and let
13996 it through if so. */
13997 if (irel->r_offset >= 2
13998 && check_br16_dslot (abfd, ptr - 2)
13999 && !(irel->r_offset >= 4
14000 && (bzc = check_relocated_bzc (abfd,
14001 ptr - 4, irel->r_offset - 4,
14002 internal_relocs, irelend))))
14003 continue;
14004 if (irel->r_offset >= 4
14005 && !bzc
14006 && check_br32_dslot (abfd, ptr - 4))
14007 continue;
14008
14009 reg = OP32_SREG (opcode);
14010
14011 /* We only relax adjacent instructions or ones separated with
14012 a branch or jump that has a delay slot. The branch or jump
14013 must not fiddle with the register used to hold the address.
14014 Subtract 4 for the LUI itself. */
14015 offset = irel[1].r_offset - irel[0].r_offset;
14016 switch (offset - 4)
14017 {
14018 case 0:
14019 break;
14020 case 2:
14021 if (check_br16 (abfd, ptr + 4, reg))
14022 break;
14023 continue;
14024 case 4:
14025 if (check_br32 (abfd, ptr + 4, reg))
14026 break;
14027 continue;
14028 default:
14029 continue;
14030 }
14031
14032 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
14033
14034 /* Give up unless the same register is used with both
14035 relocations. */
14036 if (OP32_SREG (nextopc) != reg)
14037 continue;
14038
14039 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14040 and rounding up to take masking of the two LSBs into account. */
14041 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14042
14043 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
14044 if (IS_BITSIZE (symval, 16))
14045 {
14046 /* Fix the relocation's type. */
14047 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14048
14049 /* Instructions using R_MICROMIPS_LO16 have the base or
14050 source register in bits 20:16. This register becomes $0
14051 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
14052 nextopc &= ~0x001f0000;
14053 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14054 contents + irel[1].r_offset);
14055 }
14056
14057 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14058 We add 4 to take LUI deletion into account while checking
14059 the PC-relative distance. */
14060 else if (symval % 4 == 0
14061 && IS_BITSIZE (pcrval + 4, 25)
14062 && MATCH (nextopc, addiu_insn)
14063 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14064 && OP16_VALID_REG (OP32_TREG (nextopc)))
14065 {
14066 /* Fix the relocation's type. */
14067 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14068
14069 /* Replace ADDIU with the ADDIUPC version. */
14070 nextopc = (addiupc_insn.match
14071 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14072
14073 bfd_put_micromips_32 (abfd, nextopc,
14074 contents + irel[1].r_offset);
14075 }
14076
14077 /* Can't do anything, give up, sigh... */
14078 else
14079 continue;
14080
14081 /* Fix the relocation's type. */
14082 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14083
14084 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
14085 delcnt = 4;
14086 deloff = 0;
14087 }
14088
14089 /* Compact branch relaxation -- due to the multitude of macros
14090 employed by the compiler/assembler, compact branches are not
14091 always generated. Obviously, this can/will be fixed elsewhere,
14092 but there is no drawback in double checking it here. */
14093 else if (r_type == R_MICROMIPS_PC16_S1
14094 && irel->r_offset + 5 < sec->size
14095 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14096 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
14097 && ((!insn32
14098 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14099 nop_insn_16) ? 2 : 0))
14100 || (irel->r_offset + 7 < sec->size
14101 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14102 ptr + 4),
14103 nop_insn_32) ? 4 : 0))))
14104 {
14105 unsigned long reg;
14106
14107 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14108
14109 /* Replace BEQZ/BNEZ with the compact version. */
14110 opcode = (bzc_insns_32[fndopc].match
14111 | BZC32_REG_FIELD (reg)
14112 | (opcode & 0xffff)); /* Addend value. */
14113
14114 bfd_put_micromips_32 (abfd, opcode, ptr);
14115
14116 /* Delete the delay slot NOP: two or four bytes from
14117 irel->offset + 4; delcnt has already been set above. */
14118 deloff = 4;
14119 }
14120
14121 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
14122 to check the distance from the next instruction, so subtract 2. */
14123 else if (!insn32
14124 && r_type == R_MICROMIPS_PC16_S1
14125 && IS_BITSIZE (pcrval - 2, 11)
14126 && find_match (opcode, b_insns_32) >= 0)
14127 {
14128 /* Fix the relocation's type. */
14129 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14130
14131 /* Replace the 32-bit opcode with a 16-bit opcode. */
14132 bfd_put_16 (abfd,
14133 (b_insn_16.match
14134 | (opcode & 0x3ff)), /* Addend value. */
14135 ptr);
14136
14137 /* Delete 2 bytes from irel->r_offset + 2. */
14138 delcnt = 2;
14139 deloff = 2;
14140 }
14141
14142 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
14143 to check the distance from the next instruction, so subtract 2. */
14144 else if (!insn32
14145 && r_type == R_MICROMIPS_PC16_S1
14146 && IS_BITSIZE (pcrval - 2, 8)
14147 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14148 && OP16_VALID_REG (OP32_SREG (opcode)))
14149 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14150 && OP16_VALID_REG (OP32_TREG (opcode)))))
14151 {
14152 unsigned long reg;
14153
14154 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14155
14156 /* Fix the relocation's type. */
14157 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14158
14159 /* Replace the 32-bit opcode with a 16-bit opcode. */
14160 bfd_put_16 (abfd,
14161 (bz_insns_16[fndopc].match
14162 | BZ16_REG_FIELD (reg)
14163 | (opcode & 0x7f)), /* Addend value. */
14164 ptr);
14165
14166 /* Delete 2 bytes from irel->r_offset + 2. */
14167 delcnt = 2;
14168 deloff = 2;
14169 }
14170
14171 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
14172 else if (!insn32
14173 && r_type == R_MICROMIPS_26_S1
14174 && target_is_micromips_code_p
14175 && irel->r_offset + 7 < sec->size
14176 && MATCH (opcode, jal_insn_32_bd32))
14177 {
14178 unsigned long n32opc;
14179 bfd_boolean relaxed = FALSE;
14180
14181 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
14182
14183 if (MATCH (n32opc, nop_insn_32))
14184 {
14185 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
14186 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
14187
14188 relaxed = TRUE;
14189 }
14190 else if (find_match (n32opc, move_insns_32) >= 0)
14191 {
14192 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14193 bfd_put_16 (abfd,
14194 (move_insn_16.match
14195 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14196 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
14197 ptr + 4);
14198
14199 relaxed = TRUE;
14200 }
14201 /* Other 32-bit instructions relaxable to 16-bit
14202 instructions will be handled here later. */
14203
14204 if (relaxed)
14205 {
14206 /* JAL with 32-bit delay slot that is changed to a JALS
14207 with 16-bit delay slot. */
14208 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
14209
14210 /* Delete 2 bytes from irel->r_offset + 6. */
14211 delcnt = 2;
14212 deloff = 6;
14213 }
14214 }
14215
14216 if (delcnt != 0)
14217 {
14218 /* Note that we've changed the relocs, section contents, etc. */
14219 elf_section_data (sec)->relocs = internal_relocs;
14220 elf_section_data (sec)->this_hdr.contents = contents;
14221 symtab_hdr->contents = (unsigned char *) isymbuf;
14222
14223 /* Delete bytes depending on the delcnt and deloff. */
14224 if (!mips_elf_relax_delete_bytes (abfd, sec,
14225 irel->r_offset + deloff, delcnt))
14226 goto error_return;
14227
14228 /* That will change things, so we should relax again.
14229 Note that this is not required, and it may be slow. */
14230 *again = TRUE;
14231 }
14232 }
14233
14234 if (isymbuf != NULL
14235 && symtab_hdr->contents != (unsigned char *) isymbuf)
14236 {
14237 if (! link_info->keep_memory)
14238 free (isymbuf);
14239 else
14240 {
14241 /* Cache the symbols for elf_link_input_bfd. */
14242 symtab_hdr->contents = (unsigned char *) isymbuf;
14243 }
14244 }
14245
14246 if (contents != NULL
14247 && elf_section_data (sec)->this_hdr.contents != contents)
14248 {
14249 if (! link_info->keep_memory)
14250 free (contents);
14251 else
14252 {
14253 /* Cache the section contents for elf_link_input_bfd. */
14254 elf_section_data (sec)->this_hdr.contents = contents;
14255 }
14256 }
14257
14258 if (elf_section_data (sec)->relocs != internal_relocs)
14259 free (internal_relocs);
14260
14261 return TRUE;
14262
14263 error_return:
14264 if (symtab_hdr->contents != (unsigned char *) isymbuf)
14265 free (isymbuf);
14266 if (elf_section_data (sec)->this_hdr.contents != contents)
14267 free (contents);
14268 if (elf_section_data (sec)->relocs != internal_relocs)
14269 free (internal_relocs);
14270
14271 return FALSE;
14272 }
14273 \f
14274 /* Create a MIPS ELF linker hash table. */
14275
14276 struct bfd_link_hash_table *
14277 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
14278 {
14279 struct mips_elf_link_hash_table *ret;
14280 size_t amt = sizeof (struct mips_elf_link_hash_table);
14281
14282 ret = bfd_zmalloc (amt);
14283 if (ret == NULL)
14284 return NULL;
14285
14286 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14287 mips_elf_link_hash_newfunc,
14288 sizeof (struct mips_elf_link_hash_entry),
14289 MIPS_ELF_DATA))
14290 {
14291 free (ret);
14292 return NULL;
14293 }
14294 ret->root.init_plt_refcount.plist = NULL;
14295 ret->root.init_plt_offset.plist = NULL;
14296
14297 return &ret->root.root;
14298 }
14299
14300 /* Likewise, but indicate that the target is VxWorks. */
14301
14302 struct bfd_link_hash_table *
14303 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14304 {
14305 struct bfd_link_hash_table *ret;
14306
14307 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14308 if (ret)
14309 {
14310 struct mips_elf_link_hash_table *htab;
14311
14312 htab = (struct mips_elf_link_hash_table *) ret;
14313 htab->use_plts_and_copy_relocs = TRUE;
14314 }
14315 return ret;
14316 }
14317
14318 /* A function that the linker calls if we are allowed to use PLTs
14319 and copy relocs. */
14320
14321 void
14322 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14323 {
14324 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
14325 }
14326
14327 /* A function that the linker calls to select between all or only
14328 32-bit microMIPS instructions, and between making or ignoring
14329 branch relocation checks for invalid transitions between ISA modes.
14330 Also record whether we have been configured for a GNU target. */
14331
14332 void
14333 _bfd_mips_elf_linker_flags (struct bfd_link_info *info, bfd_boolean insn32,
14334 bfd_boolean ignore_branch_isa,
14335 bfd_boolean gnu_target)
14336 {
14337 mips_elf_hash_table (info)->insn32 = insn32;
14338 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
14339 mips_elf_hash_table (info)->gnu_target = gnu_target;
14340 }
14341
14342 /* A function that the linker calls to enable use of compact branches in
14343 linker generated code for MIPSR6. */
14344
14345 void
14346 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bfd_boolean on)
14347 {
14348 mips_elf_hash_table (info)->compact_branches = on;
14349 }
14350
14351 \f
14352 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14353
14354 struct mips_mach_extension
14355 {
14356 unsigned long extension, base;
14357 };
14358
14359
14360 /* An array describing how BFD machines relate to one another. The entries
14361 are ordered topologically with MIPS I extensions listed last. */
14362
14363 static const struct mips_mach_extension mips_mach_extensions[] =
14364 {
14365 /* MIPS64r2 extensions. */
14366 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14367 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14368 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14369 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14370 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
14371 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
14372 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
14373
14374 /* MIPS64 extensions. */
14375 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14376 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14377 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14378
14379 /* MIPS V extensions. */
14380 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14381
14382 /* R10000 extensions. */
14383 { bfd_mach_mips12000, bfd_mach_mips10000 },
14384 { bfd_mach_mips14000, bfd_mach_mips10000 },
14385 { bfd_mach_mips16000, bfd_mach_mips10000 },
14386
14387 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14388 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14389 better to allow vr5400 and vr5500 code to be merged anyway, since
14390 many libraries will just use the core ISA. Perhaps we could add
14391 some sort of ASE flag if this ever proves a problem. */
14392 { bfd_mach_mips5500, bfd_mach_mips5400 },
14393 { bfd_mach_mips5400, bfd_mach_mips5000 },
14394
14395 /* MIPS IV extensions. */
14396 { bfd_mach_mips5, bfd_mach_mips8000 },
14397 { bfd_mach_mips10000, bfd_mach_mips8000 },
14398 { bfd_mach_mips5000, bfd_mach_mips8000 },
14399 { bfd_mach_mips7000, bfd_mach_mips8000 },
14400 { bfd_mach_mips9000, bfd_mach_mips8000 },
14401
14402 /* VR4100 extensions. */
14403 { bfd_mach_mips4120, bfd_mach_mips4100 },
14404 { bfd_mach_mips4111, bfd_mach_mips4100 },
14405
14406 /* MIPS III extensions. */
14407 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14408 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14409 { bfd_mach_mips8000, bfd_mach_mips4000 },
14410 { bfd_mach_mips4650, bfd_mach_mips4000 },
14411 { bfd_mach_mips4600, bfd_mach_mips4000 },
14412 { bfd_mach_mips4400, bfd_mach_mips4000 },
14413 { bfd_mach_mips4300, bfd_mach_mips4000 },
14414 { bfd_mach_mips4100, bfd_mach_mips4000 },
14415 { bfd_mach_mips5900, bfd_mach_mips4000 },
14416
14417 /* MIPS32r3 extensions. */
14418 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14419
14420 /* MIPS32r2 extensions. */
14421 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14422
14423 /* MIPS32 extensions. */
14424 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14425
14426 /* MIPS II extensions. */
14427 { bfd_mach_mips4000, bfd_mach_mips6000 },
14428 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14429 { bfd_mach_mips4010, bfd_mach_mips6000 },
14430
14431 /* MIPS I extensions. */
14432 { bfd_mach_mips6000, bfd_mach_mips3000 },
14433 { bfd_mach_mips3900, bfd_mach_mips3000 }
14434 };
14435
14436 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14437
14438 static bfd_boolean
14439 mips_mach_extends_p (unsigned long base, unsigned long extension)
14440 {
14441 size_t i;
14442
14443 if (extension == base)
14444 return TRUE;
14445
14446 if (base == bfd_mach_mipsisa32
14447 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14448 return TRUE;
14449
14450 if (base == bfd_mach_mipsisa32r2
14451 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14452 return TRUE;
14453
14454 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14455 if (extension == mips_mach_extensions[i].extension)
14456 {
14457 extension = mips_mach_extensions[i].base;
14458 if (extension == base)
14459 return TRUE;
14460 }
14461
14462 return FALSE;
14463 }
14464
14465 /* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14466
14467 static unsigned long
14468 bfd_mips_isa_ext_mach (unsigned int isa_ext)
14469 {
14470 switch (isa_ext)
14471 {
14472 case AFL_EXT_3900: return bfd_mach_mips3900;
14473 case AFL_EXT_4010: return bfd_mach_mips4010;
14474 case AFL_EXT_4100: return bfd_mach_mips4100;
14475 case AFL_EXT_4111: return bfd_mach_mips4111;
14476 case AFL_EXT_4120: return bfd_mach_mips4120;
14477 case AFL_EXT_4650: return bfd_mach_mips4650;
14478 case AFL_EXT_5400: return bfd_mach_mips5400;
14479 case AFL_EXT_5500: return bfd_mach_mips5500;
14480 case AFL_EXT_5900: return bfd_mach_mips5900;
14481 case AFL_EXT_10000: return bfd_mach_mips10000;
14482 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14483 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
14484 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
14485 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14486 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14487 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
14488 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14489 default: return bfd_mach_mips3000;
14490 }
14491 }
14492
14493 /* Return the .MIPS.abiflags value representing each ISA Extension. */
14494
14495 unsigned int
14496 bfd_mips_isa_ext (bfd *abfd)
14497 {
14498 switch (bfd_get_mach (abfd))
14499 {
14500 case bfd_mach_mips3900: return AFL_EXT_3900;
14501 case bfd_mach_mips4010: return AFL_EXT_4010;
14502 case bfd_mach_mips4100: return AFL_EXT_4100;
14503 case bfd_mach_mips4111: return AFL_EXT_4111;
14504 case bfd_mach_mips4120: return AFL_EXT_4120;
14505 case bfd_mach_mips4650: return AFL_EXT_4650;
14506 case bfd_mach_mips5400: return AFL_EXT_5400;
14507 case bfd_mach_mips5500: return AFL_EXT_5500;
14508 case bfd_mach_mips5900: return AFL_EXT_5900;
14509 case bfd_mach_mips10000: return AFL_EXT_10000;
14510 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14511 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
14512 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14513 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14514 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14515 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14516 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14517 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
14518 case bfd_mach_mips_interaptiv_mr2:
14519 return AFL_EXT_INTERAPTIV_MR2;
14520 default: return 0;
14521 }
14522 }
14523
14524 /* Encode ISA level and revision as a single value. */
14525 #define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14526
14527 /* Decode a single value into level and revision. */
14528 #define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14529 #define ISA_REV(LEVREV) ((LEVREV) & 0x7)
14530
14531 /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14532
14533 static void
14534 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14535 {
14536 int new_isa = 0;
14537 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14538 {
14539 case E_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14540 case E_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14541 case E_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14542 case E_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14543 case E_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14544 case E_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14545 case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14546 case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14547 case E_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14548 case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14549 case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
14550 default:
14551 _bfd_error_handler
14552 /* xgettext:c-format */
14553 (_("%pB: unknown architecture %s"),
14554 abfd, bfd_printable_name (abfd));
14555 }
14556
14557 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14558 {
14559 abiflags->isa_level = ISA_LEVEL (new_isa);
14560 abiflags->isa_rev = ISA_REV (new_isa);
14561 }
14562
14563 /* Update the isa_ext if ABFD describes a further extension. */
14564 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14565 bfd_get_mach (abfd)))
14566 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14567 }
14568
14569 /* Return true if the given ELF header flags describe a 32-bit binary. */
14570
14571 static bfd_boolean
14572 mips_32bit_flags_p (flagword flags)
14573 {
14574 return ((flags & EF_MIPS_32BITMODE) != 0
14575 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14576 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14577 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14578 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14579 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14580 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14581 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
14582 }
14583
14584 /* Infer the content of the ABI flags based on the elf header. */
14585
14586 static void
14587 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14588 {
14589 obj_attribute *in_attr;
14590
14591 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14592 update_mips_abiflags_isa (abfd, abiflags);
14593
14594 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14595 abiflags->gpr_size = AFL_REG_32;
14596 else
14597 abiflags->gpr_size = AFL_REG_64;
14598
14599 abiflags->cpr1_size = AFL_REG_NONE;
14600
14601 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14602 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14603
14604 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14605 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14606 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14607 && abiflags->gpr_size == AFL_REG_32))
14608 abiflags->cpr1_size = AFL_REG_32;
14609 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14610 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14611 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14612 abiflags->cpr1_size = AFL_REG_64;
14613
14614 abiflags->cpr2_size = AFL_REG_NONE;
14615
14616 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14617 abiflags->ases |= AFL_ASE_MDMX;
14618 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14619 abiflags->ases |= AFL_ASE_MIPS16;
14620 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14621 abiflags->ases |= AFL_ASE_MICROMIPS;
14622
14623 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14624 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14625 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14626 && abiflags->isa_level >= 32
14627 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
14628 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14629 }
14630
14631 /* We need to use a special link routine to handle the .reginfo and
14632 the .mdebug sections. We need to merge all instances of these
14633 sections together, not write them all out sequentially. */
14634
14635 bfd_boolean
14636 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14637 {
14638 asection *o;
14639 struct bfd_link_order *p;
14640 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14641 asection *rtproc_sec, *abiflags_sec;
14642 Elf32_RegInfo reginfo;
14643 struct ecoff_debug_info debug;
14644 struct mips_htab_traverse_info hti;
14645 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14646 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14647 HDRR *symhdr = &debug.symbolic_header;
14648 void *mdebug_handle = NULL;
14649 asection *s;
14650 EXTR esym;
14651 unsigned int i;
14652 bfd_size_type amt;
14653 struct mips_elf_link_hash_table *htab;
14654
14655 static const char * const secname[] =
14656 {
14657 ".text", ".init", ".fini", ".data",
14658 ".rodata", ".sdata", ".sbss", ".bss"
14659 };
14660 static const int sc[] =
14661 {
14662 scText, scInit, scFini, scData,
14663 scRData, scSData, scSBss, scBss
14664 };
14665
14666 htab = mips_elf_hash_table (info);
14667 BFD_ASSERT (htab != NULL);
14668
14669 /* Sort the dynamic symbols so that those with GOT entries come after
14670 those without. */
14671 if (!mips_elf_sort_hash_table (abfd, info))
14672 return FALSE;
14673
14674 /* Create any scheduled LA25 stubs. */
14675 hti.info = info;
14676 hti.output_bfd = abfd;
14677 hti.error = FALSE;
14678 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14679 if (hti.error)
14680 return FALSE;
14681
14682 /* Get a value for the GP register. */
14683 if (elf_gp (abfd) == 0)
14684 {
14685 struct bfd_link_hash_entry *h;
14686
14687 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
14688 if (h != NULL && h->type == bfd_link_hash_defined)
14689 elf_gp (abfd) = (h->u.def.value
14690 + h->u.def.section->output_section->vma
14691 + h->u.def.section->output_offset);
14692 else if (htab->root.target_os == is_vxworks
14693 && (h = bfd_link_hash_lookup (info->hash,
14694 "_GLOBAL_OFFSET_TABLE_",
14695 FALSE, FALSE, TRUE))
14696 && h->type == bfd_link_hash_defined)
14697 elf_gp (abfd) = (h->u.def.section->output_section->vma
14698 + h->u.def.section->output_offset
14699 + h->u.def.value);
14700 else if (bfd_link_relocatable (info))
14701 {
14702 bfd_vma lo = MINUS_ONE;
14703
14704 /* Find the GP-relative section with the lowest offset. */
14705 for (o = abfd->sections; o != NULL; o = o->next)
14706 if (o->vma < lo
14707 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14708 lo = o->vma;
14709
14710 /* And calculate GP relative to that. */
14711 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14712 }
14713 else
14714 {
14715 /* If the relocate_section function needs to do a reloc
14716 involving the GP value, it should make a reloc_dangerous
14717 callback to warn that GP is not defined. */
14718 }
14719 }
14720
14721 /* Go through the sections and collect the .reginfo and .mdebug
14722 information. */
14723 abiflags_sec = NULL;
14724 reginfo_sec = NULL;
14725 mdebug_sec = NULL;
14726 gptab_data_sec = NULL;
14727 gptab_bss_sec = NULL;
14728 for (o = abfd->sections; o != NULL; o = o->next)
14729 {
14730 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14731 {
14732 /* We have found the .MIPS.abiflags section in the output file.
14733 Look through all the link_orders comprising it and remove them.
14734 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14735 for (p = o->map_head.link_order; p != NULL; p = p->next)
14736 {
14737 asection *input_section;
14738
14739 if (p->type != bfd_indirect_link_order)
14740 {
14741 if (p->type == bfd_data_link_order)
14742 continue;
14743 abort ();
14744 }
14745
14746 input_section = p->u.indirect.section;
14747
14748 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14749 elf_link_input_bfd ignores this section. */
14750 input_section->flags &= ~SEC_HAS_CONTENTS;
14751 }
14752
14753 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14754 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14755
14756 /* Skip this section later on (I don't think this currently
14757 matters, but someday it might). */
14758 o->map_head.link_order = NULL;
14759
14760 abiflags_sec = o;
14761 }
14762
14763 if (strcmp (o->name, ".reginfo") == 0)
14764 {
14765 memset (&reginfo, 0, sizeof reginfo);
14766
14767 /* We have found the .reginfo section in the output file.
14768 Look through all the link_orders comprising it and merge
14769 the information together. */
14770 for (p = o->map_head.link_order; p != NULL; p = p->next)
14771 {
14772 asection *input_section;
14773 bfd *input_bfd;
14774 Elf32_External_RegInfo ext;
14775 Elf32_RegInfo sub;
14776 bfd_size_type sz;
14777
14778 if (p->type != bfd_indirect_link_order)
14779 {
14780 if (p->type == bfd_data_link_order)
14781 continue;
14782 abort ();
14783 }
14784
14785 input_section = p->u.indirect.section;
14786 input_bfd = input_section->owner;
14787
14788 sz = (input_section->size < sizeof (ext)
14789 ? input_section->size : sizeof (ext));
14790 memset (&ext, 0, sizeof (ext));
14791 if (! bfd_get_section_contents (input_bfd, input_section,
14792 &ext, 0, sz))
14793 return FALSE;
14794
14795 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14796
14797 reginfo.ri_gprmask |= sub.ri_gprmask;
14798 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14799 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14800 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14801 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14802
14803 /* ri_gp_value is set by the function
14804 `_bfd_mips_elf_section_processing' when the section is
14805 finally written out. */
14806
14807 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14808 elf_link_input_bfd ignores this section. */
14809 input_section->flags &= ~SEC_HAS_CONTENTS;
14810 }
14811
14812 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14813 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14814
14815 /* Skip this section later on (I don't think this currently
14816 matters, but someday it might). */
14817 o->map_head.link_order = NULL;
14818
14819 reginfo_sec = o;
14820 }
14821
14822 if (strcmp (o->name, ".mdebug") == 0)
14823 {
14824 struct extsym_info einfo;
14825 bfd_vma last;
14826
14827 /* We have found the .mdebug section in the output file.
14828 Look through all the link_orders comprising it and merge
14829 the information together. */
14830 symhdr->magic = swap->sym_magic;
14831 /* FIXME: What should the version stamp be? */
14832 symhdr->vstamp = 0;
14833 symhdr->ilineMax = 0;
14834 symhdr->cbLine = 0;
14835 symhdr->idnMax = 0;
14836 symhdr->ipdMax = 0;
14837 symhdr->isymMax = 0;
14838 symhdr->ioptMax = 0;
14839 symhdr->iauxMax = 0;
14840 symhdr->issMax = 0;
14841 symhdr->issExtMax = 0;
14842 symhdr->ifdMax = 0;
14843 symhdr->crfd = 0;
14844 symhdr->iextMax = 0;
14845
14846 /* We accumulate the debugging information itself in the
14847 debug_info structure. */
14848 debug.line = NULL;
14849 debug.external_dnr = NULL;
14850 debug.external_pdr = NULL;
14851 debug.external_sym = NULL;
14852 debug.external_opt = NULL;
14853 debug.external_aux = NULL;
14854 debug.ss = NULL;
14855 debug.ssext = debug.ssext_end = NULL;
14856 debug.external_fdr = NULL;
14857 debug.external_rfd = NULL;
14858 debug.external_ext = debug.external_ext_end = NULL;
14859
14860 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
14861 if (mdebug_handle == NULL)
14862 return FALSE;
14863
14864 esym.jmptbl = 0;
14865 esym.cobol_main = 0;
14866 esym.weakext = 0;
14867 esym.reserved = 0;
14868 esym.ifd = ifdNil;
14869 esym.asym.iss = issNil;
14870 esym.asym.st = stLocal;
14871 esym.asym.reserved = 0;
14872 esym.asym.index = indexNil;
14873 last = 0;
14874 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
14875 {
14876 esym.asym.sc = sc[i];
14877 s = bfd_get_section_by_name (abfd, secname[i]);
14878 if (s != NULL)
14879 {
14880 esym.asym.value = s->vma;
14881 last = s->vma + s->size;
14882 }
14883 else
14884 esym.asym.value = last;
14885 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
14886 secname[i], &esym))
14887 return FALSE;
14888 }
14889
14890 for (p = o->map_head.link_order; p != NULL; p = p->next)
14891 {
14892 asection *input_section;
14893 bfd *input_bfd;
14894 const struct ecoff_debug_swap *input_swap;
14895 struct ecoff_debug_info input_debug;
14896 char *eraw_src;
14897 char *eraw_end;
14898
14899 if (p->type != bfd_indirect_link_order)
14900 {
14901 if (p->type == bfd_data_link_order)
14902 continue;
14903 abort ();
14904 }
14905
14906 input_section = p->u.indirect.section;
14907 input_bfd = input_section->owner;
14908
14909 if (!is_mips_elf (input_bfd))
14910 {
14911 /* I don't know what a non MIPS ELF bfd would be
14912 doing with a .mdebug section, but I don't really
14913 want to deal with it. */
14914 continue;
14915 }
14916
14917 input_swap = (get_elf_backend_data (input_bfd)
14918 ->elf_backend_ecoff_debug_swap);
14919
14920 BFD_ASSERT (p->size == input_section->size);
14921
14922 /* The ECOFF linking code expects that we have already
14923 read in the debugging information and set up an
14924 ecoff_debug_info structure, so we do that now. */
14925 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
14926 &input_debug))
14927 return FALSE;
14928
14929 if (! (bfd_ecoff_debug_accumulate
14930 (mdebug_handle, abfd, &debug, swap, input_bfd,
14931 &input_debug, input_swap, info)))
14932 return FALSE;
14933
14934 /* Loop through the external symbols. For each one with
14935 interesting information, try to find the symbol in
14936 the linker global hash table and save the information
14937 for the output external symbols. */
14938 eraw_src = input_debug.external_ext;
14939 eraw_end = (eraw_src
14940 + (input_debug.symbolic_header.iextMax
14941 * input_swap->external_ext_size));
14942 for (;
14943 eraw_src < eraw_end;
14944 eraw_src += input_swap->external_ext_size)
14945 {
14946 EXTR ext;
14947 const char *name;
14948 struct mips_elf_link_hash_entry *h;
14949
14950 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
14951 if (ext.asym.sc == scNil
14952 || ext.asym.sc == scUndefined
14953 || ext.asym.sc == scSUndefined)
14954 continue;
14955
14956 name = input_debug.ssext + ext.asym.iss;
14957 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
14958 name, FALSE, FALSE, TRUE);
14959 if (h == NULL || h->esym.ifd != -2)
14960 continue;
14961
14962 if (ext.ifd != -1)
14963 {
14964 BFD_ASSERT (ext.ifd
14965 < input_debug.symbolic_header.ifdMax);
14966 ext.ifd = input_debug.ifdmap[ext.ifd];
14967 }
14968
14969 h->esym = ext;
14970 }
14971
14972 /* Free up the information we just read. */
14973 free (input_debug.line);
14974 free (input_debug.external_dnr);
14975 free (input_debug.external_pdr);
14976 free (input_debug.external_sym);
14977 free (input_debug.external_opt);
14978 free (input_debug.external_aux);
14979 free (input_debug.ss);
14980 free (input_debug.ssext);
14981 free (input_debug.external_fdr);
14982 free (input_debug.external_rfd);
14983 free (input_debug.external_ext);
14984
14985 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14986 elf_link_input_bfd ignores this section. */
14987 input_section->flags &= ~SEC_HAS_CONTENTS;
14988 }
14989
14990 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
14991 {
14992 /* Create .rtproc section. */
14993 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
14994 if (rtproc_sec == NULL)
14995 {
14996 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
14997 | SEC_LINKER_CREATED | SEC_READONLY);
14998
14999 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15000 ".rtproc",
15001 flags);
15002 if (rtproc_sec == NULL
15003 || !bfd_set_section_alignment (rtproc_sec, 4))
15004 return FALSE;
15005 }
15006
15007 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15008 info, rtproc_sec,
15009 &debug))
15010 return FALSE;
15011 }
15012
15013 /* Build the external symbol information. */
15014 einfo.abfd = abfd;
15015 einfo.info = info;
15016 einfo.debug = &debug;
15017 einfo.swap = swap;
15018 einfo.failed = FALSE;
15019 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
15020 mips_elf_output_extsym, &einfo);
15021 if (einfo.failed)
15022 return FALSE;
15023
15024 /* Set the size of the .mdebug section. */
15025 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
15026
15027 /* Skip this section later on (I don't think this currently
15028 matters, but someday it might). */
15029 o->map_head.link_order = NULL;
15030
15031 mdebug_sec = o;
15032 }
15033
15034 if (CONST_STRNEQ (o->name, ".gptab."))
15035 {
15036 const char *subname;
15037 unsigned int c;
15038 Elf32_gptab *tab;
15039 Elf32_External_gptab *ext_tab;
15040 unsigned int j;
15041
15042 /* The .gptab.sdata and .gptab.sbss sections hold
15043 information describing how the small data area would
15044 change depending upon the -G switch. These sections
15045 not used in executables files. */
15046 if (! bfd_link_relocatable (info))
15047 {
15048 for (p = o->map_head.link_order; p != NULL; p = p->next)
15049 {
15050 asection *input_section;
15051
15052 if (p->type != bfd_indirect_link_order)
15053 {
15054 if (p->type == bfd_data_link_order)
15055 continue;
15056 abort ();
15057 }
15058
15059 input_section = p->u.indirect.section;
15060
15061 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15062 elf_link_input_bfd ignores this section. */
15063 input_section->flags &= ~SEC_HAS_CONTENTS;
15064 }
15065
15066 /* Skip this section later on (I don't think this
15067 currently matters, but someday it might). */
15068 o->map_head.link_order = NULL;
15069
15070 /* Really remove the section. */
15071 bfd_section_list_remove (abfd, o);
15072 --abfd->section_count;
15073
15074 continue;
15075 }
15076
15077 /* There is one gptab for initialized data, and one for
15078 uninitialized data. */
15079 if (strcmp (o->name, ".gptab.sdata") == 0)
15080 gptab_data_sec = o;
15081 else if (strcmp (o->name, ".gptab.sbss") == 0)
15082 gptab_bss_sec = o;
15083 else
15084 {
15085 _bfd_error_handler
15086 /* xgettext:c-format */
15087 (_("%pB: illegal section name `%pA'"), abfd, o);
15088 bfd_set_error (bfd_error_nonrepresentable_section);
15089 return FALSE;
15090 }
15091
15092 /* The linker script always combines .gptab.data and
15093 .gptab.sdata into .gptab.sdata, and likewise for
15094 .gptab.bss and .gptab.sbss. It is possible that there is
15095 no .sdata or .sbss section in the output file, in which
15096 case we must change the name of the output section. */
15097 subname = o->name + sizeof ".gptab" - 1;
15098 if (bfd_get_section_by_name (abfd, subname) == NULL)
15099 {
15100 if (o == gptab_data_sec)
15101 o->name = ".gptab.data";
15102 else
15103 o->name = ".gptab.bss";
15104 subname = o->name + sizeof ".gptab" - 1;
15105 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15106 }
15107
15108 /* Set up the first entry. */
15109 c = 1;
15110 amt = c * sizeof (Elf32_gptab);
15111 tab = bfd_malloc (amt);
15112 if (tab == NULL)
15113 return FALSE;
15114 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15115 tab[0].gt_header.gt_unused = 0;
15116
15117 /* Combine the input sections. */
15118 for (p = o->map_head.link_order; p != NULL; p = p->next)
15119 {
15120 asection *input_section;
15121 bfd *input_bfd;
15122 bfd_size_type size;
15123 unsigned long last;
15124 bfd_size_type gpentry;
15125
15126 if (p->type != bfd_indirect_link_order)
15127 {
15128 if (p->type == bfd_data_link_order)
15129 continue;
15130 abort ();
15131 }
15132
15133 input_section = p->u.indirect.section;
15134 input_bfd = input_section->owner;
15135
15136 /* Combine the gptab entries for this input section one
15137 by one. We know that the input gptab entries are
15138 sorted by ascending -G value. */
15139 size = input_section->size;
15140 last = 0;
15141 for (gpentry = sizeof (Elf32_External_gptab);
15142 gpentry < size;
15143 gpentry += sizeof (Elf32_External_gptab))
15144 {
15145 Elf32_External_gptab ext_gptab;
15146 Elf32_gptab int_gptab;
15147 unsigned long val;
15148 unsigned long add;
15149 bfd_boolean exact;
15150 unsigned int look;
15151
15152 if (! (bfd_get_section_contents
15153 (input_bfd, input_section, &ext_gptab, gpentry,
15154 sizeof (Elf32_External_gptab))))
15155 {
15156 free (tab);
15157 return FALSE;
15158 }
15159
15160 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15161 &int_gptab);
15162 val = int_gptab.gt_entry.gt_g_value;
15163 add = int_gptab.gt_entry.gt_bytes - last;
15164
15165 exact = FALSE;
15166 for (look = 1; look < c; look++)
15167 {
15168 if (tab[look].gt_entry.gt_g_value >= val)
15169 tab[look].gt_entry.gt_bytes += add;
15170
15171 if (tab[look].gt_entry.gt_g_value == val)
15172 exact = TRUE;
15173 }
15174
15175 if (! exact)
15176 {
15177 Elf32_gptab *new_tab;
15178 unsigned int max;
15179
15180 /* We need a new table entry. */
15181 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
15182 new_tab = bfd_realloc (tab, amt);
15183 if (new_tab == NULL)
15184 {
15185 free (tab);
15186 return FALSE;
15187 }
15188 tab = new_tab;
15189 tab[c].gt_entry.gt_g_value = val;
15190 tab[c].gt_entry.gt_bytes = add;
15191
15192 /* Merge in the size for the next smallest -G
15193 value, since that will be implied by this new
15194 value. */
15195 max = 0;
15196 for (look = 1; look < c; look++)
15197 {
15198 if (tab[look].gt_entry.gt_g_value < val
15199 && (max == 0
15200 || (tab[look].gt_entry.gt_g_value
15201 > tab[max].gt_entry.gt_g_value)))
15202 max = look;
15203 }
15204 if (max != 0)
15205 tab[c].gt_entry.gt_bytes +=
15206 tab[max].gt_entry.gt_bytes;
15207
15208 ++c;
15209 }
15210
15211 last = int_gptab.gt_entry.gt_bytes;
15212 }
15213
15214 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15215 elf_link_input_bfd ignores this section. */
15216 input_section->flags &= ~SEC_HAS_CONTENTS;
15217 }
15218
15219 /* The table must be sorted by -G value. */
15220 if (c > 2)
15221 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15222
15223 /* Swap out the table. */
15224 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
15225 ext_tab = bfd_alloc (abfd, amt);
15226 if (ext_tab == NULL)
15227 {
15228 free (tab);
15229 return FALSE;
15230 }
15231
15232 for (j = 0; j < c; j++)
15233 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15234 free (tab);
15235
15236 o->size = c * sizeof (Elf32_External_gptab);
15237 o->contents = (bfd_byte *) ext_tab;
15238
15239 /* Skip this section later on (I don't think this currently
15240 matters, but someday it might). */
15241 o->map_head.link_order = NULL;
15242 }
15243 }
15244
15245 /* Invoke the regular ELF backend linker to do all the work. */
15246 if (!bfd_elf_final_link (abfd, info))
15247 return FALSE;
15248
15249 /* Now write out the computed sections. */
15250
15251 if (abiflags_sec != NULL)
15252 {
15253 Elf_External_ABIFlags_v0 ext;
15254 Elf_Internal_ABIFlags_v0 *abiflags;
15255
15256 abiflags = &mips_elf_tdata (abfd)->abiflags;
15257
15258 /* Set up the abiflags if no valid input sections were found. */
15259 if (!mips_elf_tdata (abfd)->abiflags_valid)
15260 {
15261 infer_mips_abiflags (abfd, abiflags);
15262 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
15263 }
15264 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15265 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15266 return FALSE;
15267 }
15268
15269 if (reginfo_sec != NULL)
15270 {
15271 Elf32_External_RegInfo ext;
15272
15273 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
15274 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
15275 return FALSE;
15276 }
15277
15278 if (mdebug_sec != NULL)
15279 {
15280 BFD_ASSERT (abfd->output_has_begun);
15281 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15282 swap, info,
15283 mdebug_sec->filepos))
15284 return FALSE;
15285
15286 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15287 }
15288
15289 if (gptab_data_sec != NULL)
15290 {
15291 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15292 gptab_data_sec->contents,
15293 0, gptab_data_sec->size))
15294 return FALSE;
15295 }
15296
15297 if (gptab_bss_sec != NULL)
15298 {
15299 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15300 gptab_bss_sec->contents,
15301 0, gptab_bss_sec->size))
15302 return FALSE;
15303 }
15304
15305 if (SGI_COMPAT (abfd))
15306 {
15307 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15308 if (rtproc_sec != NULL)
15309 {
15310 if (! bfd_set_section_contents (abfd, rtproc_sec,
15311 rtproc_sec->contents,
15312 0, rtproc_sec->size))
15313 return FALSE;
15314 }
15315 }
15316
15317 return TRUE;
15318 }
15319 \f
15320 /* Merge object file header flags from IBFD into OBFD. Raise an error
15321 if there are conflicting settings. */
15322
15323 static bfd_boolean
15324 mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
15325 {
15326 bfd *obfd = info->output_bfd;
15327 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15328 flagword old_flags;
15329 flagword new_flags;
15330 bfd_boolean ok;
15331
15332 new_flags = elf_elfheader (ibfd)->e_flags;
15333 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15334 old_flags = elf_elfheader (obfd)->e_flags;
15335
15336 /* Check flag compatibility. */
15337
15338 new_flags &= ~EF_MIPS_NOREORDER;
15339 old_flags &= ~EF_MIPS_NOREORDER;
15340
15341 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15342 doesn't seem to matter. */
15343 new_flags &= ~EF_MIPS_XGOT;
15344 old_flags &= ~EF_MIPS_XGOT;
15345
15346 /* MIPSpro generates ucode info in n64 objects. Again, we should
15347 just be able to ignore this. */
15348 new_flags &= ~EF_MIPS_UCODE;
15349 old_flags &= ~EF_MIPS_UCODE;
15350
15351 /* DSOs should only be linked with CPIC code. */
15352 if ((ibfd->flags & DYNAMIC) != 0)
15353 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15354
15355 if (new_flags == old_flags)
15356 return TRUE;
15357
15358 ok = TRUE;
15359
15360 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15361 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15362 {
15363 _bfd_error_handler
15364 (_("%pB: warning: linking abicalls files with non-abicalls files"),
15365 ibfd);
15366 ok = TRUE;
15367 }
15368
15369 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15370 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15371 if (! (new_flags & EF_MIPS_PIC))
15372 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15373
15374 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15375 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15376
15377 /* Compare the ISAs. */
15378 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15379 {
15380 _bfd_error_handler
15381 (_("%pB: linking 32-bit code with 64-bit code"),
15382 ibfd);
15383 ok = FALSE;
15384 }
15385 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15386 {
15387 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15388 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15389 {
15390 /* Copy the architecture info from IBFD to OBFD. Also copy
15391 the 32-bit flag (if set) so that we continue to recognise
15392 OBFD as a 32-bit binary. */
15393 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15394 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15395 elf_elfheader (obfd)->e_flags
15396 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15397
15398 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15399 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15400
15401 /* Copy across the ABI flags if OBFD doesn't use them
15402 and if that was what caused us to treat IBFD as 32-bit. */
15403 if ((old_flags & EF_MIPS_ABI) == 0
15404 && mips_32bit_flags_p (new_flags)
15405 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15406 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15407 }
15408 else
15409 {
15410 /* The ISAs aren't compatible. */
15411 _bfd_error_handler
15412 /* xgettext:c-format */
15413 (_("%pB: linking %s module with previous %s modules"),
15414 ibfd,
15415 bfd_printable_name (ibfd),
15416 bfd_printable_name (obfd));
15417 ok = FALSE;
15418 }
15419 }
15420
15421 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15422 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15423
15424 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15425 does set EI_CLASS differently from any 32-bit ABI. */
15426 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15427 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15428 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15429 {
15430 /* Only error if both are set (to different values). */
15431 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15432 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15433 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15434 {
15435 _bfd_error_handler
15436 /* xgettext:c-format */
15437 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
15438 ibfd,
15439 elf_mips_abi_name (ibfd),
15440 elf_mips_abi_name (obfd));
15441 ok = FALSE;
15442 }
15443 new_flags &= ~EF_MIPS_ABI;
15444 old_flags &= ~EF_MIPS_ABI;
15445 }
15446
15447 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15448 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15449 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15450 {
15451 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15452 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15453 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15454 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15455 int micro_mis = old_m16 && new_micro;
15456 int m16_mis = old_micro && new_m16;
15457
15458 if (m16_mis || micro_mis)
15459 {
15460 _bfd_error_handler
15461 /* xgettext:c-format */
15462 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
15463 ibfd,
15464 m16_mis ? "MIPS16" : "microMIPS",
15465 m16_mis ? "microMIPS" : "MIPS16");
15466 ok = FALSE;
15467 }
15468
15469 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15470
15471 new_flags &= ~ EF_MIPS_ARCH_ASE;
15472 old_flags &= ~ EF_MIPS_ARCH_ASE;
15473 }
15474
15475 /* Compare NaN encodings. */
15476 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15477 {
15478 /* xgettext:c-format */
15479 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15480 ibfd,
15481 (new_flags & EF_MIPS_NAN2008
15482 ? "-mnan=2008" : "-mnan=legacy"),
15483 (old_flags & EF_MIPS_NAN2008
15484 ? "-mnan=2008" : "-mnan=legacy"));
15485 ok = FALSE;
15486 new_flags &= ~EF_MIPS_NAN2008;
15487 old_flags &= ~EF_MIPS_NAN2008;
15488 }
15489
15490 /* Compare FP64 state. */
15491 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15492 {
15493 /* xgettext:c-format */
15494 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
15495 ibfd,
15496 (new_flags & EF_MIPS_FP64
15497 ? "-mfp64" : "-mfp32"),
15498 (old_flags & EF_MIPS_FP64
15499 ? "-mfp64" : "-mfp32"));
15500 ok = FALSE;
15501 new_flags &= ~EF_MIPS_FP64;
15502 old_flags &= ~EF_MIPS_FP64;
15503 }
15504
15505 /* Warn about any other mismatches */
15506 if (new_flags != old_flags)
15507 {
15508 /* xgettext:c-format */
15509 _bfd_error_handler
15510 (_("%pB: uses different e_flags (%#x) fields than previous modules "
15511 "(%#x)"),
15512 ibfd, new_flags, old_flags);
15513 ok = FALSE;
15514 }
15515
15516 return ok;
15517 }
15518
15519 /* Merge object attributes from IBFD into OBFD. Raise an error if
15520 there are conflicting attributes. */
15521 static bfd_boolean
15522 mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
15523 {
15524 bfd *obfd = info->output_bfd;
15525 obj_attribute *in_attr;
15526 obj_attribute *out_attr;
15527 bfd *abi_fp_bfd;
15528 bfd *abi_msa_bfd;
15529
15530 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15531 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15532 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
15533 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15534
15535 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15536 if (!abi_msa_bfd
15537 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15538 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15539
15540 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15541 {
15542 /* This is the first object. Copy the attributes. */
15543 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15544
15545 /* Use the Tag_null value to indicate the attributes have been
15546 initialized. */
15547 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15548
15549 return TRUE;
15550 }
15551
15552 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15553 non-conflicting ones. */
15554 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15555 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15556 {
15557 int out_fp, in_fp;
15558
15559 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15560 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15561 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15562 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15563 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
15564 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15565 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15566 || in_fp == Val_GNU_MIPS_ABI_FP_64
15567 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15568 {
15569 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15570 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15571 }
15572 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15573 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15574 || out_fp == Val_GNU_MIPS_ABI_FP_64
15575 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15576 /* Keep the current setting. */;
15577 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15578 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15579 {
15580 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15581 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15582 }
15583 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15584 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15585 /* Keep the current setting. */;
15586 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15587 {
15588 const char *out_string, *in_string;
15589
15590 out_string = _bfd_mips_fp_abi_string (out_fp);
15591 in_string = _bfd_mips_fp_abi_string (in_fp);
15592 /* First warn about cases involving unrecognised ABIs. */
15593 if (!out_string && !in_string)
15594 /* xgettext:c-format */
15595 _bfd_error_handler
15596 (_("warning: %pB uses unknown floating point ABI %d "
15597 "(set by %pB), %pB uses unknown floating point ABI %d"),
15598 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
15599 else if (!out_string)
15600 _bfd_error_handler
15601 /* xgettext:c-format */
15602 (_("warning: %pB uses unknown floating point ABI %d "
15603 "(set by %pB), %pB uses %s"),
15604 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
15605 else if (!in_string)
15606 _bfd_error_handler
15607 /* xgettext:c-format */
15608 (_("warning: %pB uses %s (set by %pB), "
15609 "%pB uses unknown floating point ABI %d"),
15610 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
15611 else
15612 {
15613 /* If one of the bfds is soft-float, the other must be
15614 hard-float. The exact choice of hard-float ABI isn't
15615 really relevant to the error message. */
15616 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15617 out_string = "-mhard-float";
15618 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15619 in_string = "-mhard-float";
15620 _bfd_error_handler
15621 /* xgettext:c-format */
15622 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
15623 obfd, out_string, abi_fp_bfd, ibfd, in_string);
15624 }
15625 }
15626 }
15627
15628 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15629 non-conflicting ones. */
15630 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15631 {
15632 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15633 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15634 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15635 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15636 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15637 {
15638 case Val_GNU_MIPS_ABI_MSA_128:
15639 _bfd_error_handler
15640 /* xgettext:c-format */
15641 (_("warning: %pB uses %s (set by %pB), "
15642 "%pB uses unknown MSA ABI %d"),
15643 obfd, "-mmsa", abi_msa_bfd,
15644 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15645 break;
15646
15647 default:
15648 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15649 {
15650 case Val_GNU_MIPS_ABI_MSA_128:
15651 _bfd_error_handler
15652 /* xgettext:c-format */
15653 (_("warning: %pB uses unknown MSA ABI %d "
15654 "(set by %pB), %pB uses %s"),
15655 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15656 abi_msa_bfd, ibfd, "-mmsa");
15657 break;
15658
15659 default:
15660 _bfd_error_handler
15661 /* xgettext:c-format */
15662 (_("warning: %pB uses unknown MSA ABI %d "
15663 "(set by %pB), %pB uses unknown MSA ABI %d"),
15664 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15665 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15666 break;
15667 }
15668 }
15669 }
15670
15671 /* Merge Tag_compatibility attributes and any common GNU ones. */
15672 return _bfd_elf_merge_object_attributes (ibfd, info);
15673 }
15674
15675 /* Merge object ABI flags from IBFD into OBFD. Raise an error if
15676 there are conflicting settings. */
15677
15678 static bfd_boolean
15679 mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15680 {
15681 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15682 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15683 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15684
15685 /* Update the output abiflags fp_abi using the computed fp_abi. */
15686 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15687
15688 #define max(a, b) ((a) > (b) ? (a) : (b))
15689 /* Merge abiflags. */
15690 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15691 in_tdata->abiflags.isa_level);
15692 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15693 in_tdata->abiflags.isa_rev);
15694 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15695 in_tdata->abiflags.gpr_size);
15696 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15697 in_tdata->abiflags.cpr1_size);
15698 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15699 in_tdata->abiflags.cpr2_size);
15700 #undef max
15701 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15702 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15703
15704 return TRUE;
15705 }
15706
15707 /* Merge backend specific data from an object file to the output
15708 object file when linking. */
15709
15710 bfd_boolean
15711 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
15712 {
15713 bfd *obfd = info->output_bfd;
15714 struct mips_elf_obj_tdata *out_tdata;
15715 struct mips_elf_obj_tdata *in_tdata;
15716 bfd_boolean null_input_bfd = TRUE;
15717 asection *sec;
15718 bfd_boolean ok;
15719
15720 /* Check if we have the same endianness. */
15721 if (! _bfd_generic_verify_endian_match (ibfd, info))
15722 {
15723 _bfd_error_handler
15724 (_("%pB: endianness incompatible with that of the selected emulation"),
15725 ibfd);
15726 return FALSE;
15727 }
15728
15729 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15730 return TRUE;
15731
15732 in_tdata = mips_elf_tdata (ibfd);
15733 out_tdata = mips_elf_tdata (obfd);
15734
15735 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15736 {
15737 _bfd_error_handler
15738 (_("%pB: ABI is incompatible with that of the selected emulation"),
15739 ibfd);
15740 return FALSE;
15741 }
15742
15743 /* Check to see if the input BFD actually contains any sections. If not,
15744 then it has no attributes, and its flags may not have been initialized
15745 either, but it cannot actually cause any incompatibility. */
15746 /* FIXME: This excludes any input shared library from consideration. */
15747 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15748 {
15749 /* Ignore synthetic sections and empty .text, .data and .bss sections
15750 which are automatically generated by gas. Also ignore fake
15751 (s)common sections, since merely defining a common symbol does
15752 not affect compatibility. */
15753 if ((sec->flags & SEC_IS_COMMON) == 0
15754 && strcmp (sec->name, ".reginfo")
15755 && strcmp (sec->name, ".mdebug")
15756 && (sec->size != 0
15757 || (strcmp (sec->name, ".text")
15758 && strcmp (sec->name, ".data")
15759 && strcmp (sec->name, ".bss"))))
15760 {
15761 null_input_bfd = FALSE;
15762 break;
15763 }
15764 }
15765 if (null_input_bfd)
15766 return TRUE;
15767
15768 /* Populate abiflags using existing information. */
15769 if (in_tdata->abiflags_valid)
15770 {
15771 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15772 Elf_Internal_ABIFlags_v0 in_abiflags;
15773 Elf_Internal_ABIFlags_v0 abiflags;
15774
15775 /* Set up the FP ABI attribute from the abiflags if it is not already
15776 set. */
15777 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15778 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
15779
15780 infer_mips_abiflags (ibfd, &abiflags);
15781 in_abiflags = in_tdata->abiflags;
15782
15783 /* It is not possible to infer the correct ISA revision
15784 for R3 or R5 so drop down to R2 for the checks. */
15785 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15786 in_abiflags.isa_rev = 2;
15787
15788 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15789 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
15790 _bfd_error_handler
15791 (_("%pB: warning: inconsistent ISA between e_flags and "
15792 ".MIPS.abiflags"), ibfd);
15793 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15794 && in_abiflags.fp_abi != abiflags.fp_abi)
15795 _bfd_error_handler
15796 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
15797 ".MIPS.abiflags"), ibfd);
15798 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15799 _bfd_error_handler
15800 (_("%pB: warning: inconsistent ASEs between e_flags and "
15801 ".MIPS.abiflags"), ibfd);
15802 /* The isa_ext is allowed to be an extension of what can be inferred
15803 from e_flags. */
15804 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15805 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
15806 _bfd_error_handler
15807 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
15808 ".MIPS.abiflags"), ibfd);
15809 if (in_abiflags.flags2 != 0)
15810 _bfd_error_handler
15811 (_("%pB: warning: unexpected flag in the flags2 field of "
15812 ".MIPS.abiflags (0x%lx)"), ibfd,
15813 in_abiflags.flags2);
15814 }
15815 else
15816 {
15817 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15818 in_tdata->abiflags_valid = TRUE;
15819 }
15820
15821 if (!out_tdata->abiflags_valid)
15822 {
15823 /* Copy input abiflags if output abiflags are not already valid. */
15824 out_tdata->abiflags = in_tdata->abiflags;
15825 out_tdata->abiflags_valid = TRUE;
15826 }
15827
15828 if (! elf_flags_init (obfd))
15829 {
15830 elf_flags_init (obfd) = TRUE;
15831 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
15832 elf_elfheader (obfd)->e_ident[EI_CLASS]
15833 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
15834
15835 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
15836 && (bfd_get_arch_info (obfd)->the_default
15837 || mips_mach_extends_p (bfd_get_mach (obfd),
15838 bfd_get_mach (ibfd))))
15839 {
15840 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
15841 bfd_get_mach (ibfd)))
15842 return FALSE;
15843
15844 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
15845 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15846 }
15847
15848 ok = TRUE;
15849 }
15850 else
15851 ok = mips_elf_merge_obj_e_flags (ibfd, info);
15852
15853 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
15854
15855 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
15856
15857 if (!ok)
15858 {
15859 bfd_set_error (bfd_error_bad_value);
15860 return FALSE;
15861 }
15862
15863 return TRUE;
15864 }
15865
15866 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
15867
15868 bfd_boolean
15869 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
15870 {
15871 BFD_ASSERT (!elf_flags_init (abfd)
15872 || elf_elfheader (abfd)->e_flags == flags);
15873
15874 elf_elfheader (abfd)->e_flags = flags;
15875 elf_flags_init (abfd) = TRUE;
15876 return TRUE;
15877 }
15878
15879 char *
15880 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
15881 {
15882 switch (dtag)
15883 {
15884 default: return "";
15885 case DT_MIPS_RLD_VERSION:
15886 return "MIPS_RLD_VERSION";
15887 case DT_MIPS_TIME_STAMP:
15888 return "MIPS_TIME_STAMP";
15889 case DT_MIPS_ICHECKSUM:
15890 return "MIPS_ICHECKSUM";
15891 case DT_MIPS_IVERSION:
15892 return "MIPS_IVERSION";
15893 case DT_MIPS_FLAGS:
15894 return "MIPS_FLAGS";
15895 case DT_MIPS_BASE_ADDRESS:
15896 return "MIPS_BASE_ADDRESS";
15897 case DT_MIPS_MSYM:
15898 return "MIPS_MSYM";
15899 case DT_MIPS_CONFLICT:
15900 return "MIPS_CONFLICT";
15901 case DT_MIPS_LIBLIST:
15902 return "MIPS_LIBLIST";
15903 case DT_MIPS_LOCAL_GOTNO:
15904 return "MIPS_LOCAL_GOTNO";
15905 case DT_MIPS_CONFLICTNO:
15906 return "MIPS_CONFLICTNO";
15907 case DT_MIPS_LIBLISTNO:
15908 return "MIPS_LIBLISTNO";
15909 case DT_MIPS_SYMTABNO:
15910 return "MIPS_SYMTABNO";
15911 case DT_MIPS_UNREFEXTNO:
15912 return "MIPS_UNREFEXTNO";
15913 case DT_MIPS_GOTSYM:
15914 return "MIPS_GOTSYM";
15915 case DT_MIPS_HIPAGENO:
15916 return "MIPS_HIPAGENO";
15917 case DT_MIPS_RLD_MAP:
15918 return "MIPS_RLD_MAP";
15919 case DT_MIPS_RLD_MAP_REL:
15920 return "MIPS_RLD_MAP_REL";
15921 case DT_MIPS_DELTA_CLASS:
15922 return "MIPS_DELTA_CLASS";
15923 case DT_MIPS_DELTA_CLASS_NO:
15924 return "MIPS_DELTA_CLASS_NO";
15925 case DT_MIPS_DELTA_INSTANCE:
15926 return "MIPS_DELTA_INSTANCE";
15927 case DT_MIPS_DELTA_INSTANCE_NO:
15928 return "MIPS_DELTA_INSTANCE_NO";
15929 case DT_MIPS_DELTA_RELOC:
15930 return "MIPS_DELTA_RELOC";
15931 case DT_MIPS_DELTA_RELOC_NO:
15932 return "MIPS_DELTA_RELOC_NO";
15933 case DT_MIPS_DELTA_SYM:
15934 return "MIPS_DELTA_SYM";
15935 case DT_MIPS_DELTA_SYM_NO:
15936 return "MIPS_DELTA_SYM_NO";
15937 case DT_MIPS_DELTA_CLASSSYM:
15938 return "MIPS_DELTA_CLASSSYM";
15939 case DT_MIPS_DELTA_CLASSSYM_NO:
15940 return "MIPS_DELTA_CLASSSYM_NO";
15941 case DT_MIPS_CXX_FLAGS:
15942 return "MIPS_CXX_FLAGS";
15943 case DT_MIPS_PIXIE_INIT:
15944 return "MIPS_PIXIE_INIT";
15945 case DT_MIPS_SYMBOL_LIB:
15946 return "MIPS_SYMBOL_LIB";
15947 case DT_MIPS_LOCALPAGE_GOTIDX:
15948 return "MIPS_LOCALPAGE_GOTIDX";
15949 case DT_MIPS_LOCAL_GOTIDX:
15950 return "MIPS_LOCAL_GOTIDX";
15951 case DT_MIPS_HIDDEN_GOTIDX:
15952 return "MIPS_HIDDEN_GOTIDX";
15953 case DT_MIPS_PROTECTED_GOTIDX:
15954 return "MIPS_PROTECTED_GOT_IDX";
15955 case DT_MIPS_OPTIONS:
15956 return "MIPS_OPTIONS";
15957 case DT_MIPS_INTERFACE:
15958 return "MIPS_INTERFACE";
15959 case DT_MIPS_DYNSTR_ALIGN:
15960 return "DT_MIPS_DYNSTR_ALIGN";
15961 case DT_MIPS_INTERFACE_SIZE:
15962 return "DT_MIPS_INTERFACE_SIZE";
15963 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
15964 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
15965 case DT_MIPS_PERF_SUFFIX:
15966 return "DT_MIPS_PERF_SUFFIX";
15967 case DT_MIPS_COMPACT_SIZE:
15968 return "DT_MIPS_COMPACT_SIZE";
15969 case DT_MIPS_GP_VALUE:
15970 return "DT_MIPS_GP_VALUE";
15971 case DT_MIPS_AUX_DYNAMIC:
15972 return "DT_MIPS_AUX_DYNAMIC";
15973 case DT_MIPS_PLTGOT:
15974 return "DT_MIPS_PLTGOT";
15975 case DT_MIPS_RWPLT:
15976 return "DT_MIPS_RWPLT";
15977 case DT_MIPS_XHASH:
15978 return "DT_MIPS_XHASH";
15979 }
15980 }
15981
15982 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
15983 not known. */
15984
15985 const char *
15986 _bfd_mips_fp_abi_string (int fp)
15987 {
15988 switch (fp)
15989 {
15990 /* These strings aren't translated because they're simply
15991 option lists. */
15992 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15993 return "-mdouble-float";
15994
15995 case Val_GNU_MIPS_ABI_FP_SINGLE:
15996 return "-msingle-float";
15997
15998 case Val_GNU_MIPS_ABI_FP_SOFT:
15999 return "-msoft-float";
16000
16001 case Val_GNU_MIPS_ABI_FP_OLD_64:
16002 return _("-mips32r2 -mfp64 (12 callee-saved)");
16003
16004 case Val_GNU_MIPS_ABI_FP_XX:
16005 return "-mfpxx";
16006
16007 case Val_GNU_MIPS_ABI_FP_64:
16008 return "-mgp32 -mfp64";
16009
16010 case Val_GNU_MIPS_ABI_FP_64A:
16011 return "-mgp32 -mfp64 -mno-odd-spreg";
16012
16013 default:
16014 return 0;
16015 }
16016 }
16017
16018 static void
16019 print_mips_ases (FILE *file, unsigned int mask)
16020 {
16021 if (mask & AFL_ASE_DSP)
16022 fputs ("\n\tDSP ASE", file);
16023 if (mask & AFL_ASE_DSPR2)
16024 fputs ("\n\tDSP R2 ASE", file);
16025 if (mask & AFL_ASE_DSPR3)
16026 fputs ("\n\tDSP R3 ASE", file);
16027 if (mask & AFL_ASE_EVA)
16028 fputs ("\n\tEnhanced VA Scheme", file);
16029 if (mask & AFL_ASE_MCU)
16030 fputs ("\n\tMCU (MicroController) ASE", file);
16031 if (mask & AFL_ASE_MDMX)
16032 fputs ("\n\tMDMX ASE", file);
16033 if (mask & AFL_ASE_MIPS3D)
16034 fputs ("\n\tMIPS-3D ASE", file);
16035 if (mask & AFL_ASE_MT)
16036 fputs ("\n\tMT ASE", file);
16037 if (mask & AFL_ASE_SMARTMIPS)
16038 fputs ("\n\tSmartMIPS ASE", file);
16039 if (mask & AFL_ASE_VIRT)
16040 fputs ("\n\tVZ ASE", file);
16041 if (mask & AFL_ASE_MSA)
16042 fputs ("\n\tMSA ASE", file);
16043 if (mask & AFL_ASE_MIPS16)
16044 fputs ("\n\tMIPS16 ASE", file);
16045 if (mask & AFL_ASE_MICROMIPS)
16046 fputs ("\n\tMICROMIPS ASE", file);
16047 if (mask & AFL_ASE_XPA)
16048 fputs ("\n\tXPA ASE", file);
16049 if (mask & AFL_ASE_MIPS16E2)
16050 fputs ("\n\tMIPS16e2 ASE", file);
16051 if (mask & AFL_ASE_CRC)
16052 fputs ("\n\tCRC ASE", file);
16053 if (mask & AFL_ASE_GINV)
16054 fputs ("\n\tGINV ASE", file);
16055 if (mask & AFL_ASE_LOONGSON_MMI)
16056 fputs ("\n\tLoongson MMI ASE", file);
16057 if (mask & AFL_ASE_LOONGSON_CAM)
16058 fputs ("\n\tLoongson CAM ASE", file);
16059 if (mask & AFL_ASE_LOONGSON_EXT)
16060 fputs ("\n\tLoongson EXT ASE", file);
16061 if (mask & AFL_ASE_LOONGSON_EXT2)
16062 fputs ("\n\tLoongson EXT2 ASE", file);
16063 if (mask == 0)
16064 fprintf (file, "\n\t%s", _("None"));
16065 else if ((mask & ~AFL_ASE_MASK) != 0)
16066 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
16067 }
16068
16069 static void
16070 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16071 {
16072 switch (isa_ext)
16073 {
16074 case 0:
16075 fputs (_("None"), file);
16076 break;
16077 case AFL_EXT_XLR:
16078 fputs ("RMI XLR", file);
16079 break;
16080 case AFL_EXT_OCTEON3:
16081 fputs ("Cavium Networks Octeon3", file);
16082 break;
16083 case AFL_EXT_OCTEON2:
16084 fputs ("Cavium Networks Octeon2", file);
16085 break;
16086 case AFL_EXT_OCTEONP:
16087 fputs ("Cavium Networks OcteonP", file);
16088 break;
16089 case AFL_EXT_OCTEON:
16090 fputs ("Cavium Networks Octeon", file);
16091 break;
16092 case AFL_EXT_5900:
16093 fputs ("Toshiba R5900", file);
16094 break;
16095 case AFL_EXT_4650:
16096 fputs ("MIPS R4650", file);
16097 break;
16098 case AFL_EXT_4010:
16099 fputs ("LSI R4010", file);
16100 break;
16101 case AFL_EXT_4100:
16102 fputs ("NEC VR4100", file);
16103 break;
16104 case AFL_EXT_3900:
16105 fputs ("Toshiba R3900", file);
16106 break;
16107 case AFL_EXT_10000:
16108 fputs ("MIPS R10000", file);
16109 break;
16110 case AFL_EXT_SB1:
16111 fputs ("Broadcom SB-1", file);
16112 break;
16113 case AFL_EXT_4111:
16114 fputs ("NEC VR4111/VR4181", file);
16115 break;
16116 case AFL_EXT_4120:
16117 fputs ("NEC VR4120", file);
16118 break;
16119 case AFL_EXT_5400:
16120 fputs ("NEC VR5400", file);
16121 break;
16122 case AFL_EXT_5500:
16123 fputs ("NEC VR5500", file);
16124 break;
16125 case AFL_EXT_LOONGSON_2E:
16126 fputs ("ST Microelectronics Loongson 2E", file);
16127 break;
16128 case AFL_EXT_LOONGSON_2F:
16129 fputs ("ST Microelectronics Loongson 2F", file);
16130 break;
16131 case AFL_EXT_INTERAPTIV_MR2:
16132 fputs ("Imagination interAptiv MR2", file);
16133 break;
16134 default:
16135 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
16136 break;
16137 }
16138 }
16139
16140 static void
16141 print_mips_fp_abi_value (FILE *file, int val)
16142 {
16143 switch (val)
16144 {
16145 case Val_GNU_MIPS_ABI_FP_ANY:
16146 fprintf (file, _("Hard or soft float\n"));
16147 break;
16148 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16149 fprintf (file, _("Hard float (double precision)\n"));
16150 break;
16151 case Val_GNU_MIPS_ABI_FP_SINGLE:
16152 fprintf (file, _("Hard float (single precision)\n"));
16153 break;
16154 case Val_GNU_MIPS_ABI_FP_SOFT:
16155 fprintf (file, _("Soft float\n"));
16156 break;
16157 case Val_GNU_MIPS_ABI_FP_OLD_64:
16158 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16159 break;
16160 case Val_GNU_MIPS_ABI_FP_XX:
16161 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16162 break;
16163 case Val_GNU_MIPS_ABI_FP_64:
16164 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16165 break;
16166 case Val_GNU_MIPS_ABI_FP_64A:
16167 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16168 break;
16169 default:
16170 fprintf (file, "??? (%d)\n", val);
16171 break;
16172 }
16173 }
16174
16175 static int
16176 get_mips_reg_size (int reg_size)
16177 {
16178 return (reg_size == AFL_REG_NONE) ? 0
16179 : (reg_size == AFL_REG_32) ? 32
16180 : (reg_size == AFL_REG_64) ? 64
16181 : (reg_size == AFL_REG_128) ? 128
16182 : -1;
16183 }
16184
16185 bfd_boolean
16186 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
16187 {
16188 FILE *file = ptr;
16189
16190 BFD_ASSERT (abfd != NULL && ptr != NULL);
16191
16192 /* Print normal ELF private data. */
16193 _bfd_elf_print_private_bfd_data (abfd, ptr);
16194
16195 /* xgettext:c-format */
16196 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16197
16198 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16199 fprintf (file, _(" [abi=O32]"));
16200 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16201 fprintf (file, _(" [abi=O64]"));
16202 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16203 fprintf (file, _(" [abi=EABI32]"));
16204 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16205 fprintf (file, _(" [abi=EABI64]"));
16206 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16207 fprintf (file, _(" [abi unknown]"));
16208 else if (ABI_N32_P (abfd))
16209 fprintf (file, _(" [abi=N32]"));
16210 else if (ABI_64_P (abfd))
16211 fprintf (file, _(" [abi=64]"));
16212 else
16213 fprintf (file, _(" [no abi set]"));
16214
16215 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
16216 fprintf (file, " [mips1]");
16217 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
16218 fprintf (file, " [mips2]");
16219 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
16220 fprintf (file, " [mips3]");
16221 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
16222 fprintf (file, " [mips4]");
16223 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
16224 fprintf (file, " [mips5]");
16225 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
16226 fprintf (file, " [mips32]");
16227 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
16228 fprintf (file, " [mips64]");
16229 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
16230 fprintf (file, " [mips32r2]");
16231 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
16232 fprintf (file, " [mips64r2]");
16233 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16234 fprintf (file, " [mips32r6]");
16235 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16236 fprintf (file, " [mips64r6]");
16237 else
16238 fprintf (file, _(" [unknown ISA]"));
16239
16240 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
16241 fprintf (file, " [mdmx]");
16242
16243 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
16244 fprintf (file, " [mips16]");
16245
16246 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16247 fprintf (file, " [micromips]");
16248
16249 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16250 fprintf (file, " [nan2008]");
16251
16252 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
16253 fprintf (file, " [old fp64]");
16254
16255 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
16256 fprintf (file, " [32bitmode]");
16257 else
16258 fprintf (file, _(" [not 32bitmode]"));
16259
16260 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
16261 fprintf (file, " [noreorder]");
16262
16263 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
16264 fprintf (file, " [PIC]");
16265
16266 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
16267 fprintf (file, " [CPIC]");
16268
16269 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
16270 fprintf (file, " [XGOT]");
16271
16272 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
16273 fprintf (file, " [UCODE]");
16274
16275 fputc ('\n', file);
16276
16277 if (mips_elf_tdata (abfd)->abiflags_valid)
16278 {
16279 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16280 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16281 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16282 if (abiflags->isa_rev > 1)
16283 fprintf (file, "r%d", abiflags->isa_rev);
16284 fprintf (file, "\nGPR size: %d",
16285 get_mips_reg_size (abiflags->gpr_size));
16286 fprintf (file, "\nCPR1 size: %d",
16287 get_mips_reg_size (abiflags->cpr1_size));
16288 fprintf (file, "\nCPR2 size: %d",
16289 get_mips_reg_size (abiflags->cpr2_size));
16290 fputs ("\nFP ABI: ", file);
16291 print_mips_fp_abi_value (file, abiflags->fp_abi);
16292 fputs ("ISA Extension: ", file);
16293 print_mips_isa_ext (file, abiflags->isa_ext);
16294 fputs ("\nASEs:", file);
16295 print_mips_ases (file, abiflags->ases);
16296 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16297 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16298 fputc ('\n', file);
16299 }
16300
16301 return TRUE;
16302 }
16303
16304 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
16305 {
16306 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16307 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16308 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
16309 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16310 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16311 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
16312 { STRING_COMMA_LEN (".MIPS.xhash"), 0, SHT_MIPS_XHASH, SHF_ALLOC },
16313 { NULL, 0, 0, 0, 0 }
16314 };
16315
16316 /* Merge non visibility st_other attributes. Ensure that the
16317 STO_OPTIONAL flag is copied into h->other, even if this is not a
16318 definiton of the symbol. */
16319 void
16320 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16321 const Elf_Internal_Sym *isym,
16322 bfd_boolean definition,
16323 bfd_boolean dynamic ATTRIBUTE_UNUSED)
16324 {
16325 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16326 {
16327 unsigned char other;
16328
16329 other = (definition ? isym->st_other : h->other);
16330 other &= ~ELF_ST_VISIBILITY (-1);
16331 h->other = other | ELF_ST_VISIBILITY (h->other);
16332 }
16333
16334 if (!definition
16335 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
16336 h->other |= STO_OPTIONAL;
16337 }
16338
16339 /* Decide whether an undefined symbol is special and can be ignored.
16340 This is the case for OPTIONAL symbols on IRIX. */
16341 bfd_boolean
16342 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16343 {
16344 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
16345 }
16346
16347 bfd_boolean
16348 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16349 {
16350 return (sym->st_shndx == SHN_COMMON
16351 || sym->st_shndx == SHN_MIPS_ACOMMON
16352 || sym->st_shndx == SHN_MIPS_SCOMMON);
16353 }
16354
16355 /* Return address for Ith PLT stub in section PLT, for relocation REL
16356 or (bfd_vma) -1 if it should not be included. */
16357
16358 bfd_vma
16359 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16360 const arelent *rel ATTRIBUTE_UNUSED)
16361 {
16362 return (plt->vma
16363 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16364 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16365 }
16366
16367 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16368 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16369 and .got.plt and also the slots may be of a different size each we walk
16370 the PLT manually fetching instructions and matching them against known
16371 patterns. To make things easier standard MIPS slots, if any, always come
16372 first. As we don't create proper ELF symbols we use the UDATA.I member
16373 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16374 with the ST_OTHER member of the ELF symbol. */
16375
16376 long
16377 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16378 long symcount ATTRIBUTE_UNUSED,
16379 asymbol **syms ATTRIBUTE_UNUSED,
16380 long dynsymcount, asymbol **dynsyms,
16381 asymbol **ret)
16382 {
16383 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16384 static const char microsuffix[] = "@micromipsplt";
16385 static const char m16suffix[] = "@mips16plt";
16386 static const char mipssuffix[] = "@plt";
16387
16388 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
16389 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16390 bfd_boolean micromips_p = MICROMIPS_P (abfd);
16391 Elf_Internal_Shdr *hdr;
16392 bfd_byte *plt_data;
16393 bfd_vma plt_offset;
16394 unsigned int other;
16395 bfd_vma entry_size;
16396 bfd_vma plt0_size;
16397 asection *relplt;
16398 bfd_vma opcode;
16399 asection *plt;
16400 asymbol *send;
16401 size_t size;
16402 char *names;
16403 long counti;
16404 arelent *p;
16405 asymbol *s;
16406 char *nend;
16407 long count;
16408 long pi;
16409 long i;
16410 long n;
16411
16412 *ret = NULL;
16413
16414 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16415 return 0;
16416
16417 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16418 if (relplt == NULL)
16419 return 0;
16420
16421 hdr = &elf_section_data (relplt)->this_hdr;
16422 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16423 return 0;
16424
16425 plt = bfd_get_section_by_name (abfd, ".plt");
16426 if (plt == NULL)
16427 return 0;
16428
16429 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16430 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
16431 return -1;
16432 p = relplt->relocation;
16433
16434 /* Calculating the exact amount of space required for symbols would
16435 require two passes over the PLT, so just pessimise assuming two
16436 PLT slots per relocation. */
16437 count = relplt->size / hdr->sh_entsize;
16438 counti = count * bed->s->int_rels_per_ext_rel;
16439 size = 2 * count * sizeof (asymbol);
16440 size += count * (sizeof (mipssuffix) +
16441 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16442 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16443 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16444
16445 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16446 size += sizeof (asymbol) + sizeof (pltname);
16447
16448 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16449 return -1;
16450
16451 if (plt->size < 16)
16452 return -1;
16453
16454 s = *ret = bfd_malloc (size);
16455 if (s == NULL)
16456 return -1;
16457 send = s + 2 * count + 1;
16458
16459 names = (char *) send;
16460 nend = (char *) s + size;
16461 n = 0;
16462
16463 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16464 if (opcode == 0x3302fffe)
16465 {
16466 if (!micromips_p)
16467 return -1;
16468 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16469 other = STO_MICROMIPS;
16470 }
16471 else if (opcode == 0x0398c1d0)
16472 {
16473 if (!micromips_p)
16474 return -1;
16475 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16476 other = STO_MICROMIPS;
16477 }
16478 else
16479 {
16480 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16481 other = 0;
16482 }
16483
16484 s->the_bfd = abfd;
16485 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16486 s->section = plt;
16487 s->value = 0;
16488 s->name = names;
16489 s->udata.i = other;
16490 memcpy (names, pltname, sizeof (pltname));
16491 names += sizeof (pltname);
16492 ++s, ++n;
16493
16494 pi = 0;
16495 for (plt_offset = plt0_size;
16496 plt_offset + 8 <= plt->size && s < send;
16497 plt_offset += entry_size)
16498 {
16499 bfd_vma gotplt_addr;
16500 const char *suffix;
16501 bfd_vma gotplt_hi;
16502 bfd_vma gotplt_lo;
16503 size_t suffixlen;
16504
16505 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16506
16507 /* Check if the second word matches the expected MIPS16 instruction. */
16508 if (opcode == 0x651aeb00)
16509 {
16510 if (micromips_p)
16511 return -1;
16512 /* Truncated table??? */
16513 if (plt_offset + 16 > plt->size)
16514 break;
16515 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16516 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16517 suffixlen = sizeof (m16suffix);
16518 suffix = m16suffix;
16519 other = STO_MIPS16;
16520 }
16521 /* Likewise the expected microMIPS instruction (no insn32 mode). */
16522 else if (opcode == 0xff220000)
16523 {
16524 if (!micromips_p)
16525 return -1;
16526 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16527 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16528 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16529 gotplt_lo <<= 2;
16530 gotplt_addr = gotplt_hi + gotplt_lo;
16531 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16532 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16533 suffixlen = sizeof (microsuffix);
16534 suffix = microsuffix;
16535 other = STO_MICROMIPS;
16536 }
16537 /* Likewise the expected microMIPS instruction (insn32 mode). */
16538 else if ((opcode & 0xffff0000) == 0xff2f0000)
16539 {
16540 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16541 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16542 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16543 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16544 gotplt_addr = gotplt_hi + gotplt_lo;
16545 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16546 suffixlen = sizeof (microsuffix);
16547 suffix = microsuffix;
16548 other = STO_MICROMIPS;
16549 }
16550 /* Otherwise assume standard MIPS code. */
16551 else
16552 {
16553 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16554 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16555 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16556 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16557 gotplt_addr = gotplt_hi + gotplt_lo;
16558 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16559 suffixlen = sizeof (mipssuffix);
16560 suffix = mipssuffix;
16561 other = 0;
16562 }
16563 /* Truncated table??? */
16564 if (plt_offset + entry_size > plt->size)
16565 break;
16566
16567 for (i = 0;
16568 i < count && p[pi].address != gotplt_addr;
16569 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16570
16571 if (i < count)
16572 {
16573 size_t namelen;
16574 size_t len;
16575
16576 *s = **p[pi].sym_ptr_ptr;
16577 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16578 we are defining a symbol, ensure one of them is set. */
16579 if ((s->flags & BSF_LOCAL) == 0)
16580 s->flags |= BSF_GLOBAL;
16581 s->flags |= BSF_SYNTHETIC;
16582 s->section = plt;
16583 s->value = plt_offset;
16584 s->name = names;
16585 s->udata.i = other;
16586
16587 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16588 namelen = len + suffixlen;
16589 if (names + namelen > nend)
16590 break;
16591
16592 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16593 names += len;
16594 memcpy (names, suffix, suffixlen);
16595 names += suffixlen;
16596
16597 ++s, ++n;
16598 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16599 }
16600 }
16601
16602 free (plt_data);
16603
16604 return n;
16605 }
16606
16607 /* Return the ABI flags associated with ABFD if available. */
16608
16609 Elf_Internal_ABIFlags_v0 *
16610 bfd_mips_elf_get_abiflags (bfd *abfd)
16611 {
16612 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16613
16614 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16615 }
16616
16617 /* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16618 field. Taken from `libc-abis.h' generated at GNU libc build time.
16619 Using a MIPS_ prefix as other libc targets use different values. */
16620 enum
16621 {
16622 MIPS_LIBC_ABI_DEFAULT = 0,
16623 MIPS_LIBC_ABI_MIPS_PLT,
16624 MIPS_LIBC_ABI_UNIQUE,
16625 MIPS_LIBC_ABI_MIPS_O32_FP64,
16626 MIPS_LIBC_ABI_ABSOLUTE,
16627 MIPS_LIBC_ABI_XHASH,
16628 MIPS_LIBC_ABI_MAX
16629 };
16630
16631 bfd_boolean
16632 _bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
16633 {
16634 struct mips_elf_link_hash_table *htab = NULL;
16635 Elf_Internal_Ehdr *i_ehdrp;
16636
16637 if (!_bfd_elf_init_file_header (abfd, link_info))
16638 return FALSE;
16639
16640 i_ehdrp = elf_elfheader (abfd);
16641 if (link_info)
16642 {
16643 htab = mips_elf_hash_table (link_info);
16644 BFD_ASSERT (htab != NULL);
16645 }
16646
16647 if (htab != NULL
16648 && htab->use_plts_and_copy_relocs
16649 && htab->root.target_os != is_vxworks)
16650 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16651
16652 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16653 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16654 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
16655
16656 /* Mark that we need support for absolute symbols in the dynamic loader. */
16657 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16658 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16659
16660 /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16661 if it is the only hash section that will be created. */
16662 if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16663 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
16664 return TRUE;
16665 }
16666
16667 int
16668 _bfd_mips_elf_compact_eh_encoding
16669 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16670 {
16671 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16672 }
16673
16674 /* Return the opcode for can't unwind. */
16675
16676 int
16677 _bfd_mips_elf_cant_unwind_opcode
16678 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
16679 {
16680 return COMPACT_EH_CANT_UNWIND_OPCODE;
16681 }
16682
16683 /* Record a position XLAT_LOC in the xlat translation table, associated with
16684 the hash entry H. The entry in the translation table will later be
16685 populated with the real symbol dynindx. */
16686
16687 void
16688 _bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16689 bfd_vma xlat_loc)
16690 {
16691 struct mips_elf_link_hash_entry *hmips;
16692
16693 hmips = (struct mips_elf_link_hash_entry *) h;
16694 hmips->mipsxhash_loc = xlat_loc;
16695 }
This page took 0.360813 seconds and 5 git commands to generate.