1 /* MIPS-specific support for ELF
2 Copyright (C) 1993-2014 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 "elfxx-mips.h"
37 #include "elf/mips.h"
38 #include "elf-vxworks.h"
39
40 /* Get the ECOFF swapping routines. */
41 #include "coff/sym.h"
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
44 #include "coff/mips.h"
45
46 #include "hashtab.h"
47
48 /* Types of TLS GOT entry. */
49 enum mips_got_tls_type {
50 GOT_TLS_NONE,
51 GOT_TLS_GD,
52 GOT_TLS_LDM,
53 GOT_TLS_IE
54 };
55
56 /* This structure is used to hold information about one GOT entry.
57 There are four types of entry:
58
59 (1) an absolute address
60 requires: abfd == NULL
61 fields: d.address
62
63 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
64 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
65 fields: abfd, symndx, d.addend, tls_type
66
67 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
68 requires: abfd != NULL, symndx == -1
69 fields: d.h, tls_type
70
71 (4) a TLS LDM slot
72 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
73 fields: none; there's only one of these per GOT. */
74 struct mips_got_entry
75 {
76 /* One input bfd that needs the GOT entry. */
77 bfd *abfd;
78 /* The index of the symbol, as stored in the relocation r_info, if
79 we have a local symbol; -1 otherwise. */
80 long symndx;
81 union
82 {
83 /* If abfd == NULL, an address that must be stored in the got. */
84 bfd_vma address;
85 /* If abfd != NULL && symndx != -1, the addend of the relocation
86 that should be added to the symbol value. */
87 bfd_vma addend;
88 /* If abfd != NULL && symndx == -1, the hash table entry
89 corresponding to a symbol in the GOT. The symbol's entry
90 is in the local area if h->global_got_area is GGA_NONE,
91 otherwise it is in the global area. */
92 struct mips_elf_link_hash_entry *h;
93 } d;
94
95 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
96 symbol entry with r_symndx == 0. */
97 unsigned char tls_type;
98
99 /* True if we have filled in the GOT contents for a TLS entry,
100 and created the associated relocations. */
101 unsigned char tls_initialized;
102
103 /* The offset from the beginning of the .got section to the entry
104 corresponding to this symbol+addend. If it's a global symbol
105 whose offset is yet to be decided, it's going to be -1. */
106 long gotidx;
107 };
108
109 /* This structure represents a GOT page reference from an input bfd.
110 Each instance represents a symbol + ADDEND, where the representation
111 of the symbol depends on whether it is local to the input bfd.
112 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
113 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
114
115 Page references with SYMNDX >= 0 always become page references
116 in the output. Page references with SYMNDX < 0 only become page
117 references if the symbol binds locally; in other cases, the page
118 reference decays to a global GOT reference. */
119 struct mips_got_page_ref
120 {
121 long symndx;
122 union
123 {
124 struct mips_elf_link_hash_entry *h;
125 bfd *abfd;
126 } u;
127 bfd_vma addend;
128 };
129
130 /* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
131 The structures form a non-overlapping list that is sorted by increasing
132 MIN_ADDEND. */
133 struct mips_got_page_range
134 {
135 struct mips_got_page_range *next;
136 bfd_signed_vma min_addend;
137 bfd_signed_vma max_addend;
138 };
139
140 /* This structure describes the range of addends that are applied to page
141 relocations against a given section. */
142 struct mips_got_page_entry
143 {
144 /* The section that these entries are based on. */
145 asection *sec;
146 /* The ranges for this page entry. */
147 struct mips_got_page_range *ranges;
148 /* The maximum number of page entries needed for RANGES. */
149 bfd_vma num_pages;
150 };
151
152 /* This structure is used to hold .got information when linking. */
153
154 struct mips_got_info
155 {
156 /* The number of global .got entries. */
157 unsigned int global_gotno;
158 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
159 unsigned int reloc_only_gotno;
160 /* The number of .got slots used for TLS. */
161 unsigned int tls_gotno;
162 /* The first unused TLS .got entry. Used only during
163 mips_elf_initialize_tls_index. */
164 unsigned int tls_assigned_gotno;
165 /* The number of local .got entries, eventually including page entries. */
166 unsigned int local_gotno;
167 /* The maximum number of page entries needed. */
168 unsigned int page_gotno;
169 /* The number of relocations needed for the GOT entries. */
170 unsigned int relocs;
171 /* The first unused local .got entry. */
172 unsigned int assigned_low_gotno;
173 /* The last unused local .got entry. */
174 unsigned int assigned_high_gotno;
175 /* A hash table holding members of the got. */
176 struct htab *got_entries;
177 /* A hash table holding mips_got_page_ref structures. */
178 struct htab *got_page_refs;
179 /* A hash table of mips_got_page_entry structures. */
180 struct htab *got_page_entries;
181 /* In multi-got links, a pointer to the next got (err, rather, most
182 of the time, it points to the previous got). */
183 struct mips_got_info *next;
184 };
185
186 /* Structure passed when merging bfds' gots. */
187
188 struct mips_elf_got_per_bfd_arg
189 {
190 /* The output bfd. */
191 bfd *obfd;
192 /* The link information. */
193 struct bfd_link_info *info;
194 /* A pointer to the primary got, i.e., the one that's going to get
195 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
196 DT_MIPS_GOTSYM. */
197 struct mips_got_info *primary;
198 /* A non-primary got we're trying to merge with other input bfd's
199 gots. */
200 struct mips_got_info *current;
201 /* The maximum number of got entries that can be addressed with a
202 16-bit offset. */
203 unsigned int max_count;
204 /* The maximum number of page entries needed by each got. */
205 unsigned int max_pages;
206 /* The total number of global entries which will live in the
207 primary got and be automatically relocated. This includes
208 those not referenced by the primary GOT but included in
209 the "master" GOT. */
210 unsigned int global_count;
211 };
212
213 /* A structure used to pass information to htab_traverse callbacks
214 when laying out the GOT. */
215
216 struct mips_elf_traverse_got_arg
217 {
218 struct bfd_link_info *info;
219 struct mips_got_info *g;
220 int value;
221 };
222
223 struct _mips_elf_section_data
224 {
225 struct bfd_elf_section_data elf;
226 union
227 {
228 bfd_byte *tdata;
229 } u;
230 };
231
232 #define mips_elf_section_data(sec) \
233 ((struct _mips_elf_section_data *) elf_section_data (sec))
234
235 #define is_mips_elf(bfd) \
236 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
237 && elf_tdata (bfd) != NULL \
238 && elf_object_id (bfd) == MIPS_ELF_DATA)
239
240 /* The ABI says that every symbol used by dynamic relocations must have
241 a global GOT entry. Among other things, this provides the dynamic
242 linker with a free, directly-indexed cache. The GOT can therefore
243 contain symbols that are not referenced by GOT relocations themselves
244 (in other words, it may have symbols that are not referenced by things
245 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
246
247 GOT relocations are less likely to overflow if we put the associated
248 GOT entries towards the beginning. We therefore divide the global
249 GOT entries into two areas: "normal" and "reloc-only". Entries in
250 the first area can be used for both dynamic relocations and GP-relative
251 accesses, while those in the "reloc-only" area are for dynamic
252 relocations only.
253
254 These GGA_* ("Global GOT Area") values are organised so that lower
255 values are more general than higher values. Also, non-GGA_NONE
256 values are ordered by the position of the area in the GOT. */
257 #define GGA_NORMAL 0
258 #define GGA_RELOC_ONLY 1
259 #define GGA_NONE 2
260
261 /* Information about a non-PIC interface to a PIC function. There are
262 two ways of creating these interfaces. The first is to add:
263
264 lui $25,%hi(func)
265 addiu $25,$25,%lo(func)
266
267 immediately before a PIC function "func". The second is to add:
268
269 lui $25,%hi(func)
270 j func
271 addiu $25,$25,%lo(func)
272
273 to a separate trampoline section.
274
275 Stubs of the first kind go in a new section immediately before the
276 target function. Stubs of the second kind go in a single section
277 pointed to by the hash table's "strampoline" field. */
278 struct mips_elf_la25_stub {
279 /* The generated section that contains this stub. */
280 asection *stub_section;
281
282 /* The offset of the stub from the start of STUB_SECTION. */
283 bfd_vma offset;
284
285 /* One symbol for the original function. Its location is available
286 in H->root.root.u.def. */
287 struct mips_elf_link_hash_entry *h;
288 };
289
290 /* Macros for populating a mips_elf_la25_stub. */
291
292 #define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
293 #define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
294 #define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
295 #define LA25_LUI_MICROMIPS(VAL) \
296 (0x41b90000 | (VAL)) /* lui t9,VAL */
297 #define LA25_J_MICROMIPS(VAL) \
298 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
299 #define LA25_ADDIU_MICROMIPS(VAL) \
300 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
301
302 /* This structure is passed to mips_elf_sort_hash_table_f when sorting
303 the dynamic symbols. */
304
305 struct mips_elf_hash_sort_data
306 {
307 /* The symbol in the global GOT with the lowest dynamic symbol table
308 index. */
309 struct elf_link_hash_entry *low;
310 /* The least dynamic symbol table index corresponding to a non-TLS
311 symbol with a GOT entry. */
312 long min_got_dynindx;
313 /* The greatest dynamic symbol table index corresponding to a symbol
314 with a GOT entry that is not referenced (e.g., a dynamic symbol
315 with dynamic relocations pointing to it from non-primary GOTs). */
316 long max_unref_got_dynindx;
317 /* The greatest dynamic symbol table index not corresponding to a
318 symbol without a GOT entry. */
319 long max_non_got_dynindx;
320 };
321
322 /* We make up to two PLT entries if needed, one for standard MIPS code
323 and one for compressed code, either a MIPS16 or microMIPS one. We
324 keep a separate record of traditional lazy-binding stubs, for easier
325 processing. */
326
327 struct plt_entry
328 {
329 /* Traditional SVR4 stub offset, or -1 if none. */
330 bfd_vma stub_offset;
331
332 /* Standard PLT entry offset, or -1 if none. */
333 bfd_vma mips_offset;
334
335 /* Compressed PLT entry offset, or -1 if none. */
336 bfd_vma comp_offset;
337
338 /* The corresponding .got.plt index, or -1 if none. */
339 bfd_vma gotplt_index;
340
341 /* Whether we need a standard PLT entry. */
342 unsigned int need_mips : 1;
343
344 /* Whether we need a compressed PLT entry. */
345 unsigned int need_comp : 1;
346 };
347
348 /* The MIPS ELF linker needs additional information for each symbol in
349 the global hash table. */
350
351 struct mips_elf_link_hash_entry
352 {
353 struct elf_link_hash_entry root;
354
355 /* External symbol information. */
356 EXTR esym;
357
358 /* The la25 stub we have created for ths symbol, if any. */
359 struct mips_elf_la25_stub *la25_stub;
360
361 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
362 this symbol. */
363 unsigned int possibly_dynamic_relocs;
364
365 /* If there is a stub that 32 bit functions should use to call this
366 16 bit function, this points to the section containing the stub. */
367 asection *fn_stub;
368
369 /* If there is a stub that 16 bit functions should use to call this
370 32 bit function, this points to the section containing the stub. */
371 asection *call_stub;
372
373 /* This is like the call_stub field, but it is used if the function
374 being called returns a floating point value. */
375 asection *call_fp_stub;
376
377 /* The highest GGA_* value that satisfies all references to this symbol. */
378 unsigned int global_got_area : 2;
379
380 /* True if all GOT relocations against this symbol are for calls. This is
381 a looser condition than no_fn_stub below, because there may be other
382 non-call non-GOT relocations against the symbol. */
383 unsigned int got_only_for_calls : 1;
384
385 /* True if one of the relocations described by possibly_dynamic_relocs
386 is against a readonly section. */
387 unsigned int readonly_reloc : 1;
388
389 /* True if there is a relocation against this symbol that must be
390 resolved by the static linker (in other words, if the relocation
391 cannot possibly be made dynamic). */
392 unsigned int has_static_relocs : 1;
393
394 /* True if we must not create a .MIPS.stubs entry for this symbol.
395 This is set, for example, if there are relocations related to
396 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
397 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
398 unsigned int no_fn_stub : 1;
399
400 /* Whether we need the fn_stub; this is true if this symbol appears
401 in any relocs other than a 16 bit call. */
402 unsigned int need_fn_stub : 1;
403
404 /* True if this symbol is referenced by branch relocations from
405 any non-PIC input file. This is used to determine whether an
406 la25 stub is required. */
407 unsigned int has_nonpic_branches : 1;
408
409 /* Does this symbol need a traditional MIPS lazy-binding stub
410 (as opposed to a PLT entry)? */
411 unsigned int needs_lazy_stub : 1;
412
413 /* Does this symbol resolve to a PLT entry? */
414 unsigned int use_plt_entry : 1;
415 };
416
417 /* MIPS ELF linker hash table. */
418
419 struct mips_elf_link_hash_table
420 {
421 struct elf_link_hash_table root;
422
423 /* The number of .rtproc entries. */
424 bfd_size_type procedure_count;
425
426 /* The size of the .compact_rel section (if SGI_COMPAT). */
427 bfd_size_type compact_rel_size;
428
429 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
430 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
431 bfd_boolean use_rld_obj_head;
432
433 /* The __rld_map or __rld_obj_head symbol. */
434 struct elf_link_hash_entry *rld_symbol;
435
436 /* This is set if we see any mips16 stub sections. */
437 bfd_boolean mips16_stubs_seen;
438
439 /* True if we can generate copy relocs and PLTs. */
440 bfd_boolean use_plts_and_copy_relocs;
441
442 /* True if we can only use 32-bit microMIPS instructions. */
443 bfd_boolean insn32;
444
445 /* True if we are targetting R6 compact branches. */
446 bfd_boolean compact_branches;
447
448 /* True if we're generating code for VxWorks. */
449 bfd_boolean is_vxworks;
450
451 /* True if we already reported the small-data section overflow. */
452 bfd_boolean small_data_overflow_reported;
453
454 /* Shortcuts to some dynamic sections, or NULL if they are not
455 being used. */
456 asection *srelbss;
457 asection *sdynbss;
458 asection *srelplt;
459 asection *srelplt2;
460 asection *sgotplt;
461 asection *splt;
462 asection *sstubs;
463 asection *sgot;
464
465 /* The master GOT information. */
466 struct mips_got_info *got_info;
467
468 /* The global symbol in the GOT with the lowest index in the dynamic
469 symbol table. */
470 struct elf_link_hash_entry *global_gotsym;
471
472 /* The size of the PLT header in bytes. */
473 bfd_vma plt_header_size;
474
475 /* The size of a standard PLT entry in bytes. */
476 bfd_vma plt_mips_entry_size;
477
478 /* The size of a compressed PLT entry in bytes. */
479 bfd_vma plt_comp_entry_size;
480
481 /* The offset of the next standard PLT entry to create. */
482 bfd_vma plt_mips_offset;
483
484 /* The offset of the next compressed PLT entry to create. */
485 bfd_vma plt_comp_offset;
486
487 /* The index of the next .got.plt entry to create. */
488 bfd_vma plt_got_index;
489
490 /* The number of functions that need a lazy-binding stub. */
491 bfd_vma lazy_stub_count;
492
493 /* The size of a function stub entry in bytes. */
494 bfd_vma function_stub_size;
495
496 /* The number of reserved entries at the beginning of the GOT. */
497 unsigned int reserved_gotno;
498
499 /* The section used for mips_elf_la25_stub trampolines.
500 See the comment above that structure for details. */
501 asection *strampoline;
502
503 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
504 pairs. */
505 htab_t la25_stubs;
506
507 /* A function FN (NAME, IS, OS) that creates a new input section
508 called NAME and links it to output section OS. If IS is nonnull,
509 the new section should go immediately before it, otherwise it
510 should go at the (current) beginning of OS.
511
512 The function returns the new section on success, otherwise it
513 returns null. */
514 asection *(*add_stub_section) (const char *, asection *, asection *);
515
516 /* Small local sym cache. */
517 struct sym_cache sym_cache;
518
519 /* Is the PLT header compressed? */
520 unsigned int plt_header_is_comp : 1;
521 };
522
523 /* Get the MIPS ELF linker hash table from a link_info structure. */
524
525 #define mips_elf_hash_table(p) \
526 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
527 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
528
529 /* A structure used to communicate with htab_traverse callbacks. */
530 struct mips_htab_traverse_info
531 {
532 /* The usual link-wide information. */
533 struct bfd_link_info *info;
534 bfd *output_bfd;
535
536 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
537 bfd_boolean error;
538 };
539
540 /* MIPS ELF private object data. */
541
542 struct mips_elf_obj_tdata
543 {
544 /* Generic ELF private object data. */
545 struct elf_obj_tdata root;
546
547 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
548 bfd *abi_fp_bfd;
549
550 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
551 bfd *abi_msa_bfd;
552
553 /* The abiflags for this object. */
554 Elf_Internal_ABIFlags_v0 abiflags;
555 bfd_boolean abiflags_valid;
556
557 /* The GOT requirements of input bfds. */
558 struct mips_got_info *got;
559
560 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
561 included directly in this one, but there's no point to wasting
562 the memory just for the infrequently called find_nearest_line. */
563 struct mips_elf_find_line *find_line_info;
564
565 /* An array of stub sections indexed by symbol number. */
566 asection **local_stubs;
567 asection **local_call_stubs;
568
569 /* The Irix 5 support uses two virtual sections, which represent
570 text/data symbols defined in dynamic objects. */
571 asymbol *elf_data_symbol;
572 asymbol *elf_text_symbol;
573 asection *elf_data_section;
574 asection *elf_text_section;
575 };
576
577 /* Get MIPS ELF private object data from BFD's tdata. */
578
579 #define mips_elf_tdata(bfd) \
580 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
581
582 #define TLS_RELOC_P(r_type) \
583 (r_type == R_MIPS_TLS_DTPMOD32 \
584 || r_type == R_MIPS_TLS_DTPMOD64 \
585 || r_type == R_MIPS_TLS_DTPREL32 \
586 || r_type == R_MIPS_TLS_DTPREL64 \
587 || r_type == R_MIPS_TLS_GD \
588 || r_type == R_MIPS_TLS_LDM \
589 || r_type == R_MIPS_TLS_DTPREL_HI16 \
590 || r_type == R_MIPS_TLS_DTPREL_LO16 \
591 || r_type == R_MIPS_TLS_GOTTPREL \
592 || r_type == R_MIPS_TLS_TPREL32 \
593 || r_type == R_MIPS_TLS_TPREL64 \
594 || r_type == R_MIPS_TLS_TPREL_HI16 \
595 || r_type == R_MIPS_TLS_TPREL_LO16 \
596 || r_type == R_MIPS16_TLS_GD \
597 || r_type == R_MIPS16_TLS_LDM \
598 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
599 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
600 || r_type == R_MIPS16_TLS_GOTTPREL \
601 || r_type == R_MIPS16_TLS_TPREL_HI16 \
602 || r_type == R_MIPS16_TLS_TPREL_LO16 \
603 || r_type == R_MICROMIPS_TLS_GD \
604 || r_type == R_MICROMIPS_TLS_LDM \
605 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
606 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
607 || r_type == R_MICROMIPS_TLS_GOTTPREL \
608 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
609 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
610
611 /* Structure used to pass information to mips_elf_output_extsym. */
612
613 struct extsym_info
614 {
615 bfd *abfd;
616 struct bfd_link_info *info;
617 struct ecoff_debug_info *debug;
618 const struct ecoff_debug_swap *swap;
619 bfd_boolean failed;
620 };
621
622 /* The names of the runtime procedure table symbols used on IRIX5. */
623
624 static const char * const mips_elf_dynsym_rtproc_names[] =
625 {
626 "_procedure_table",
627 "_procedure_string_table",
628 "_procedure_table_size",
629 NULL
630 };
631
632 /* These structures are used to generate the .compact_rel section on
633 IRIX5. */
634
635 typedef struct
636 {
637 unsigned long id1; /* Always one? */
638 unsigned long num; /* Number of compact relocation entries. */
639 unsigned long id2; /* Always two? */
640 unsigned long offset; /* The file offset of the first relocation. */
641 unsigned long reserved0; /* Zero? */
642 unsigned long reserved1; /* Zero? */
643 } Elf32_compact_rel;
644
645 typedef struct
646 {
647 bfd_byte id1[4];
648 bfd_byte num[4];
649 bfd_byte id2[4];
650 bfd_byte offset[4];
651 bfd_byte reserved0[4];
652 bfd_byte reserved1[4];
653 } Elf32_External_compact_rel;
654
655 typedef struct
656 {
657 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
658 unsigned int rtype : 4; /* Relocation types. See below. */
659 unsigned int dist2to : 8;
660 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
661 unsigned long konst; /* KONST field. See below. */
662 unsigned long vaddr; /* VADDR to be relocated. */
663 } Elf32_crinfo;
664
665 typedef struct
666 {
667 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
668 unsigned int rtype : 4; /* Relocation types. See below. */
669 unsigned int dist2to : 8;
670 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
671 unsigned long konst; /* KONST field. See below. */
672 } Elf32_crinfo2;
673
674 typedef struct
675 {
676 bfd_byte info[4];
677 bfd_byte konst[4];
678 bfd_byte vaddr[4];
679 } Elf32_External_crinfo;
680
681 typedef struct
682 {
683 bfd_byte info[4];
684 bfd_byte konst[4];
685 } Elf32_External_crinfo2;
686
687 /* These are the constants used to swap the bitfields in a crinfo. */
688
689 #define CRINFO_CTYPE (0x1)
690 #define CRINFO_CTYPE_SH (31)
691 #define CRINFO_RTYPE (0xf)
692 #define CRINFO_RTYPE_SH (27)
693 #define CRINFO_DIST2TO (0xff)
694 #define CRINFO_DIST2TO_SH (19)
695 #define CRINFO_RELVADDR (0x7ffff)
696 #define CRINFO_RELVADDR_SH (0)
697
698 /* A compact relocation info has long (3 words) or short (2 words)
699 formats. A short format doesn't have VADDR field and relvaddr
700 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
701 #define CRF_MIPS_LONG 1
702 #define CRF_MIPS_SHORT 0
703
704 /* There are 4 types of compact relocation at least. The value KONST
705 has different meaning for each type:
706
707 (type) (konst)
708 CT_MIPS_REL32 Address in data
709 CT_MIPS_WORD Address in word (XXX)
710 CT_MIPS_GPHI_LO GP - vaddr
711 CT_MIPS_JMPAD Address to jump
712 */
713
714 #define CRT_MIPS_REL32 0xa
715 #define CRT_MIPS_WORD 0xb
716 #define CRT_MIPS_GPHI_LO 0xc
717 #define CRT_MIPS_JMPAD 0xd
718
719 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
720 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
721 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
722 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
723
724 /* The structure of the runtime procedure descriptor created by the
725 loader for use by the static exception system. */
726
727 typedef struct runtime_pdr {
728 bfd_vma adr; /* Memory address of start of procedure. */
729 long regmask; /* Save register mask. */
730 long regoffset; /* Save register offset. */
731 long fregmask; /* Save floating point register mask. */
732 long fregoffset; /* Save floating point register offset. */
733 long frameoffset; /* Frame size. */
734 short framereg; /* Frame pointer register. */
735 short pcreg; /* Offset or reg of return pc. */
736 long irpss; /* Index into the runtime string table. */
737 long reserved;
738 struct exception_info *exception_info;/* Pointer to exception array. */
739 } RPDR, *pRPDR;
740 #define cbRPDR sizeof (RPDR)
741 #define rpdNil ((pRPDR) 0)
742
743 static struct mips_got_entry *mips_elf_create_local_got_entry
744 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
745 struct mips_elf_link_hash_entry *, int);
746 static bfd_boolean mips_elf_sort_hash_table_f
747 (struct mips_elf_link_hash_entry *, void *);
748 static bfd_vma mips_elf_high
749 (bfd_vma);
750 static bfd_boolean mips_elf_create_dynamic_relocation
751 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
752 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
753 bfd_vma *, asection *);
754 static bfd_vma mips_elf_adjust_gp
755 (bfd *, struct mips_got_info *, bfd *);
756
757 /* This will be used when we sort the dynamic relocation records. */
758 static bfd *reldyn_sorting_bfd;
759
760 /* True if ABFD is for CPUs with load interlocking that include
761 non-MIPS1 CPUs and R3900. */
762 #define LOAD_INTERLOCKS_P(abfd) \
763 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
764 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
765
766 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
767 This should be safe for all architectures. We enable this predicate
768 for RM9000 for now. */
769 #define JAL_TO_BAL_P(abfd) \
770 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
771
772 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
773 This should be safe for all architectures. We enable this predicate for
774 all CPUs. */
775 #define JALR_TO_BAL_P(abfd) 1
776
777 /* True if ABFD is for CPUs that are faster if JR is converted to B.
778 This should be safe for all architectures. We enable this predicate for
779 all CPUs. */
780 #define JR_TO_B_P(abfd) 1
781
782 /* True if ABFD is a PIC object. */
783 #define PIC_OBJECT_P(abfd) \
784 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
785
786 /* Nonzero if ABFD is using the O32 ABI. */
787 #define ABI_O32_P(abfd) \
788 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
789
790 /* Nonzero if ABFD is using the N32 ABI. */
791 #define ABI_N32_P(abfd) \
792 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
793
794 /* Nonzero if ABFD is using the N64 ABI. */
795 #define ABI_64_P(abfd) \
796 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
797
798 /* Nonzero if ABFD is using NewABI conventions. */
799 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
800
801 /* Nonzero if ABFD has microMIPS code. */
802 #define MICROMIPS_P(abfd) \
803 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
804
805 /* Nonzero if ABFD is MIPS R6. */
806 #define MIPSR6_P(abfd) \
807 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
808 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
809
810 /* The IRIX compatibility level we are striving for. */
811 #define IRIX_COMPAT(abfd) \
812 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
813
814 /* Whether we are trying to be compatible with IRIX at all. */
815 #define SGI_COMPAT(abfd) \
816 (IRIX_COMPAT (abfd) != ict_none)
817
818 /* The name of the options section. */
819 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
820 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
821
822 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
823 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
824 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
825 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
826
827 /* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
828 #define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
829 (strcmp (NAME, ".MIPS.abiflags") == 0)
830
831 /* Whether the section is readonly. */
832 #define MIPS_ELF_READONLY_SECTION(sec) \
833 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
834 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
835
836 /* The name of the stub section. */
837 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
838
839 /* The size of an external REL relocation. */
840 #define MIPS_ELF_REL_SIZE(abfd) \
841 (get_elf_backend_data (abfd)->s->sizeof_rel)
842
843 /* The size of an external RELA relocation. */
844 #define MIPS_ELF_RELA_SIZE(abfd) \
845 (get_elf_backend_data (abfd)->s->sizeof_rela)
846
847 /* The size of an external dynamic table entry. */
848 #define MIPS_ELF_DYN_SIZE(abfd) \
849 (get_elf_backend_data (abfd)->s->sizeof_dyn)
850
851 /* The size of a GOT entry. */
852 #define MIPS_ELF_GOT_SIZE(abfd) \
853 (get_elf_backend_data (abfd)->s->arch_size / 8)
854
855 /* The size of the .rld_map section. */
856 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
857 (get_elf_backend_data (abfd)->s->arch_size / 8)
858
859 /* The size of a symbol-table entry. */
860 #define MIPS_ELF_SYM_SIZE(abfd) \
861 (get_elf_backend_data (abfd)->s->sizeof_sym)
862
863 /* The default alignment for sections, as a power of two. */
864 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
865 (get_elf_backend_data (abfd)->s->log_file_align)
866
867 /* Get word-sized data. */
868 #define MIPS_ELF_GET_WORD(abfd, ptr) \
869 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
870
871 /* Put out word-sized data. */
872 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
873 (ABI_64_P (abfd) \
874 ? bfd_put_64 (abfd, val, ptr) \
875 : bfd_put_32 (abfd, val, ptr))
876
877 /* The opcode for word-sized loads (LW or LD). */
878 #define MIPS_ELF_LOAD_WORD(abfd) \
879 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
880
881 /* Add a dynamic symbol table-entry. */
882 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
883 _bfd_elf_add_dynamic_entry (info, tag, val)
884
885 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
886 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
887
888 /* The name of the dynamic relocation section. */
889 #define MIPS_ELF_REL_DYN_NAME(INFO) \
890 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
891
892 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
893 from smaller values. Start with zero, widen, *then* decrement. */
894 #define MINUS_ONE (((bfd_vma)0) - 1)
895 #define MINUS_TWO (((bfd_vma)0) - 2)
896
897 /* The value to write into got[1] for SVR4 targets, to identify it is
898 a GNU object. The dynamic linker can then use got[1] to store the
899 module pointer. */
900 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
901 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
902
903 /* The offset of $gp from the beginning of the .got section. */
904 #define ELF_MIPS_GP_OFFSET(INFO) \
905 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
906
907 /* The maximum size of the GOT for it to be addressable using 16-bit
908 offsets from $gp. */
909 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
910
911 /* Instructions which appear in a stub. */
912 #define STUB_LW(abfd) \
913 ((ABI_64_P (abfd) \
914 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
915 : 0x8f998010)) /* lw t9,0x8010(gp) */
916 #define STUB_MOVE(abfd) \
917 ((ABI_64_P (abfd) \
918 ? 0x03e0782d /* daddu t7,ra */ \
919 : 0x03e07821)) /* addu t7,ra */
920 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
921 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
922 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
923 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
924 #define STUB_LI16S(abfd, VAL) \
925 ((ABI_64_P (abfd) \
926 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
927 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
928
929 /* Likewise for the microMIPS ASE. */
930 #define STUB_LW_MICROMIPS(abfd) \
931 (ABI_64_P (abfd) \
932 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
933 : 0xff3c8010) /* lw t9,0x8010(gp) */
934 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
935 #define STUB_MOVE32_MICROMIPS(abfd) \
936 (ABI_64_P (abfd) \
937 ? 0x581f7950 /* daddu t7,ra,zero */ \
938 : 0x001f7950) /* addu t7,ra,zero */
939 #define STUB_LUI_MICROMIPS(VAL) \
940 (0x41b80000 + (VAL)) /* lui t8,VAL */
941 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
942 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
943 #define STUB_ORI_MICROMIPS(VAL) \
944 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
945 #define STUB_LI16U_MICROMIPS(VAL) \
946 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
947 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
948 (ABI_64_P (abfd) \
949 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
950 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
951
952 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
953 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
954 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
955 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
956 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
957 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
958
959 /* The name of the dynamic interpreter. This is put in the .interp
960 section. */
961
962 #define ELF_DYNAMIC_INTERPRETER(abfd) \
963 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
964 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
965 : "/usr/lib/libc.so.1")
966
967 #ifdef BFD64
968 #define MNAME(bfd,pre,pos) \
969 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
970 #define ELF_R_SYM(bfd, i) \
971 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
972 #define ELF_R_TYPE(bfd, i) \
973 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
974 #define ELF_R_INFO(bfd, s, t) \
975 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
976 #else
977 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
978 #define ELF_R_SYM(bfd, i) \
979 (ELF32_R_SYM (i))
980 #define ELF_R_TYPE(bfd, i) \
981 (ELF32_R_TYPE (i))
982 #define ELF_R_INFO(bfd, s, t) \
983 (ELF32_R_INFO (s, t))
984 #endif
985
986 /* The mips16 compiler uses a couple of special sections to handle
987 floating point arguments.
988
989 Section names that look like .mips16.fn.FNNAME contain stubs that
990 copy floating point arguments from the fp regs to the gp regs and
991 then jump to FNNAME. If any 32 bit function calls FNNAME, the
992 call should be redirected to the stub instead. If no 32 bit
993 function calls FNNAME, the stub should be discarded. We need to
994 consider any reference to the function, not just a call, because
995 if the address of the function is taken we will need the stub,
996 since the address might be passed to a 32 bit function.
997
998 Section names that look like .mips16.call.FNNAME contain stubs
999 that copy floating point arguments from the gp regs to the fp
1000 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1001 then any 16 bit function that calls FNNAME should be redirected
1002 to the stub instead. If FNNAME is not a 32 bit function, the
1003 stub should be discarded.
1004
1005 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1006 which call FNNAME and then copy the return value from the fp regs
1007 to the gp regs. These stubs store the return value in $18 while
1008 calling FNNAME; any function which might call one of these stubs
1009 must arrange to save $18 around the call. (This case is not
1010 needed for 32 bit functions that call 16 bit functions, because
1011 16 bit functions always return floating point values in both
1012 $f0/$f1 and $2/$3.)
1013
1014 Note that in all cases FNNAME might be defined statically.
1015 Therefore, FNNAME is not used literally. Instead, the relocation
1016 information will indicate which symbol the section is for.
1017
1018 We record any stubs that we find in the symbol table. */
1019
1020 #define FN_STUB ".mips16.fn."
1021 #define CALL_STUB ".mips16.call."
1022 #define CALL_FP_STUB ".mips16.call.fp."
1023
1024 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1025 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1026 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
1027
1028 /* The format of the first PLT entry in an O32 executable. */
1029 static const bfd_vma mips_o32_exec_plt0_entry[] =
1030 {
1031 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1032 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1033 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1034 0x031cc023, /* subu $24, $24, $28 */
1035 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1036 0x0018c082, /* srl $24, $24, 2 */
1037 0x0320f809, /* jalr $25 */
1038 0x2718fffe /* subu $24, $24, 2 */
1039 };
1040
1041 /* The format of the first PLT entry in an N32 executable. Different
1042 because gp ($28) is not available; we use t2 ($14) instead. */
1043 static const bfd_vma mips_n32_exec_plt0_entry[] =
1044 {
1045 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1046 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1047 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1048 0x030ec023, /* subu $24, $24, $14 */
1049 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1050 0x0018c082, /* srl $24, $24, 2 */
1051 0x0320f809, /* jalr $25 */
1052 0x2718fffe /* subu $24, $24, 2 */
1053 };
1054
1055 /* The format of the first PLT entry in an N64 executable. Different
1056 from N32 because of the increased size of GOT entries. */
1057 static const bfd_vma mips_n64_exec_plt0_entry[] =
1058 {
1059 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1060 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1061 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1062 0x030ec023, /* subu $24, $24, $14 */
1063 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1064 0x0018c0c2, /* srl $24, $24, 3 */
1065 0x0320f809, /* jalr $25 */
1066 0x2718fffe /* subu $24, $24, 2 */
1067 };
1068
1069 /* The format of the microMIPS first PLT entry in an O32 executable.
1070 We rely on v0 ($2) rather than t8 ($24) to contain the address
1071 of the GOTPLT entry handled, so this stub may only be used when
1072 all the subsequent PLT entries are microMIPS code too.
1073
1074 The trailing NOP is for alignment and correct disassembly only. */
1075 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1076 {
1077 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1078 0xff23, 0x0000, /* lw $25, 0($3) */
1079 0x0535, /* subu $2, $2, $3 */
1080 0x2525, /* srl $2, $2, 2 */
1081 0x3302, 0xfffe, /* subu $24, $2, 2 */
1082 0x0dff, /* move $15, $31 */
1083 0x45f9, /* jalrs $25 */
1084 0x0f83, /* move $28, $3 */
1085 0x0c00 /* nop */
1086 };
1087
1088 /* The format of the microMIPS first PLT entry in an O32 executable
1089 in the insn32 mode. */
1090 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1091 {
1092 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1093 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1094 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1095 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1096 0x001f, 0x7950, /* move $15, $31 */
1097 0x0318, 0x1040, /* srl $24, $24, 2 */
1098 0x03f9, 0x0f3c, /* jalr $25 */
1099 0x3318, 0xfffe /* subu $24, $24, 2 */
1100 };
1101
1102 /* The format of subsequent standard PLT entries. */
1103 static const bfd_vma mips_exec_plt_entry[] =
1104 {
1105 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1106 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1107 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1108 0x03200008 /* jr $25 */
1109 };
1110
1111 static const bfd_vma mipsr6_exec_plt_entry[] =
1112 {
1113 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1114 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1115 0x03200009, /* jr $25 */
1116 0x25f80000 /* addiu $24, $15, %lo(.got.plt entry) */
1117 };
1118
1119 static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1120 {
1121 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1122 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1123 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1124 0xd8190000 /* jic $25, 0 */
1125 };
1126
1127 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1128 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1129 directly addressable. */
1130 static const bfd_vma mips16_o32_exec_plt_entry[] =
1131 {
1132 0xb203, /* lw $2, 12($pc) */
1133 0x9a60, /* lw $3, 0($2) */
1134 0x651a, /* move $24, $2 */
1135 0xeb00, /* jr $3 */
1136 0x653b, /* move $25, $3 */
1137 0x6500, /* nop */
1138 0x0000, 0x0000 /* .word (.got.plt entry) */
1139 };
1140
1141 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1142 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1143 static const bfd_vma micromips_o32_exec_plt_entry[] =
1144 {
1145 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1146 0xff22, 0x0000, /* lw $25, 0($2) */
1147 0x4599, /* jr $25 */
1148 0x0f02 /* move $24, $2 */
1149 };
1150
1151 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1152 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1153 {
1154 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1155 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1156 0x0019, 0x0f3c, /* jr $25 */
1157 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1158 };
1159
1160 /* The format of the first PLT entry in a VxWorks executable. */
1161 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1162 {
1163 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1164 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1165 0x8f390008, /* lw t9, 8(t9) */
1166 0x00000000, /* nop */
1167 0x03200008, /* jr t9 */
1168 0x00000000 /* nop */
1169 };
1170
1171 /* The format of subsequent PLT entries. */
1172 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1173 {
1174 0x10000000, /* b .PLT_resolver */
1175 0x24180000, /* li t8, <pltindex> */
1176 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1177 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1178 0x8f390000, /* lw t9, 0(t9) */
1179 0x00000000, /* nop */
1180 0x03200008, /* jr t9 */
1181 0x00000000 /* nop */
1182 };
1183
1184 /* The format of the first PLT entry in a VxWorks shared object. */
1185 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1186 {
1187 0x8f990008, /* lw t9, 8(gp) */
1188 0x00000000, /* nop */
1189 0x03200008, /* jr t9 */
1190 0x00000000, /* nop */
1191 0x00000000, /* nop */
1192 0x00000000 /* nop */
1193 };
1194
1195 /* The format of subsequent PLT entries. */
1196 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1197 {
1198 0x10000000, /* b .PLT_resolver */
1199 0x24180000 /* li t8, <pltindex> */
1200 };
1201
1202 /* microMIPS 32-bit opcode helper installer. */
1203
1204 static void
bfd_put_micromips_32(const bfd * abfd,bfd_vma opcode,bfd_byte * ptr)1205 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1206 {
1207 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1208 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1209 }
1210
1211 /* microMIPS 32-bit opcode helper retriever. */
1212
1213 static bfd_vma
bfd_get_micromips_32(const bfd * abfd,const bfd_byte * ptr)1214 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1215 {
1216 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1217 }
1218
1219 /* Look up an entry in a MIPS ELF linker hash table. */
1220
1221 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1222 ((struct mips_elf_link_hash_entry *) \
1223 elf_link_hash_lookup (&(table)->root, (string), (create), \
1224 (copy), (follow)))
1225
1226 /* Traverse a MIPS ELF linker hash table. */
1227
1228 #define mips_elf_link_hash_traverse(table, func, info) \
1229 (elf_link_hash_traverse \
1230 (&(table)->root, \
1231 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1232 (info)))
1233
1234 /* Find the base offsets for thread-local storage in this object,
1235 for GD/LD and IE/LE respectively. */
1236
1237 #define TP_OFFSET 0x7000
1238 #define DTP_OFFSET 0x8000
1239
1240 static bfd_vma
dtprel_base(struct bfd_link_info * info)1241 dtprel_base (struct bfd_link_info *info)
1242 {
1243 /* If tls_sec is NULL, we should have signalled an error already. */
1244 if (elf_hash_table (info)->tls_sec == NULL)
1245 return 0;
1246 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1247 }
1248
1249 static bfd_vma
tprel_base(struct bfd_link_info * info)1250 tprel_base (struct bfd_link_info *info)
1251 {
1252 /* If tls_sec is NULL, we should have signalled an error already. */
1253 if (elf_hash_table (info)->tls_sec == NULL)
1254 return 0;
1255 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1256 }
1257
1258 /* Create an entry in a MIPS ELF linker hash table. */
1259
1260 static struct bfd_hash_entry *
mips_elf_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)1261 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1262 struct bfd_hash_table *table, const char *string)
1263 {
1264 struct mips_elf_link_hash_entry *ret =
1265 (struct mips_elf_link_hash_entry *) entry;
1266
1267 /* Allocate the structure if it has not already been allocated by a
1268 subclass. */
1269 if (ret == NULL)
1270 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1271 if (ret == NULL)
1272 return (struct bfd_hash_entry *) ret;
1273
1274 /* Call the allocation method of the superclass. */
1275 ret = ((struct mips_elf_link_hash_entry *)
1276 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1277 table, string));
1278 if (ret != NULL)
1279 {
1280 /* Set local fields. */
1281 memset (&ret->esym, 0, sizeof (EXTR));
1282 /* We use -2 as a marker to indicate that the information has
1283 not been set. -1 means there is no associated ifd. */
1284 ret->esym.ifd = -2;
1285 ret->la25_stub = 0;
1286 ret->possibly_dynamic_relocs = 0;
1287 ret->fn_stub = NULL;
1288 ret->call_stub = NULL;
1289 ret->call_fp_stub = NULL;
1290 ret->global_got_area = GGA_NONE;
1291 ret->got_only_for_calls = TRUE;
1292 ret->readonly_reloc = FALSE;
1293 ret->has_static_relocs = FALSE;
1294 ret->no_fn_stub = FALSE;
1295 ret->need_fn_stub = FALSE;
1296 ret->has_nonpic_branches = FALSE;
1297 ret->needs_lazy_stub = FALSE;
1298 ret->use_plt_entry = FALSE;
1299 }
1300
1301 return (struct bfd_hash_entry *) ret;
1302 }
1303
1304 /* Allocate MIPS ELF private object data. */
1305
1306 bfd_boolean
_bfd_mips_elf_mkobject(bfd * abfd)1307 _bfd_mips_elf_mkobject (bfd *abfd)
1308 {
1309 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1310 MIPS_ELF_DATA);
1311 }
1312
1313 bfd_boolean
_bfd_mips_elf_new_section_hook(bfd * abfd,asection * sec)1314 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1315 {
1316 if (!sec->used_by_bfd)
1317 {
1318 struct _mips_elf_section_data *sdata;
1319 bfd_size_type amt = sizeof (*sdata);
1320
1321 sdata = bfd_zalloc (abfd, amt);
1322 if (sdata == NULL)
1323 return FALSE;
1324 sec->used_by_bfd = sdata;
1325 }
1326
1327 return _bfd_elf_new_section_hook (abfd, sec);
1328 }
1329
1330 /* Read ECOFF debugging information from a .mdebug section into a
1331 ecoff_debug_info structure. */
1332
1333 bfd_boolean
_bfd_mips_elf_read_ecoff_info(bfd * abfd,asection * section,struct ecoff_debug_info * debug)1334 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1335 struct ecoff_debug_info *debug)
1336 {
1337 HDRR *symhdr;
1338 const struct ecoff_debug_swap *swap;
1339 char *ext_hdr;
1340
1341 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1342 memset (debug, 0, sizeof (*debug));
1343
1344 ext_hdr = bfd_malloc (swap->external_hdr_size);
1345 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1346 goto error_return;
1347
1348 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1349 swap->external_hdr_size))
1350 goto error_return;
1351
1352 symhdr = &debug->symbolic_header;
1353 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1354
1355 /* The symbolic header contains absolute file offsets and sizes to
1356 read. */
1357 #define READ(ptr, offset, count, size, type) \
1358 if (symhdr->count == 0) \
1359 debug->ptr = NULL; \
1360 else \
1361 { \
1362 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1363 debug->ptr = bfd_malloc (amt); \
1364 if (debug->ptr == NULL) \
1365 goto error_return; \
1366 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1367 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1368 goto error_return; \
1369 }
1370
1371 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1372 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1373 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1374 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1375 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1376 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1377 union aux_ext *);
1378 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1379 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1380 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1381 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1382 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1383 #undef READ
1384
1385 debug->fdr = NULL;
1386
1387 return TRUE;
1388
1389 error_return:
1390 if (ext_hdr != NULL)
1391 free (ext_hdr);
1392 if (debug->line != NULL)
1393 free (debug->line);
1394 if (debug->external_dnr != NULL)
1395 free (debug->external_dnr);
1396 if (debug->external_pdr != NULL)
1397 free (debug->external_pdr);
1398 if (debug->external_sym != NULL)
1399 free (debug->external_sym);
1400 if (debug->external_opt != NULL)
1401 free (debug->external_opt);
1402 if (debug->external_aux != NULL)
1403 free (debug->external_aux);
1404 if (debug->ss != NULL)
1405 free (debug->ss);
1406 if (debug->ssext != NULL)
1407 free (debug->ssext);
1408 if (debug->external_fdr != NULL)
1409 free (debug->external_fdr);
1410 if (debug->external_rfd != NULL)
1411 free (debug->external_rfd);
1412 if (debug->external_ext != NULL)
1413 free (debug->external_ext);
1414 return FALSE;
1415 }
1416
1417 /* Swap RPDR (runtime procedure table entry) for output. */
1418
1419 static void
ecoff_swap_rpdr_out(bfd * abfd,const RPDR * in,struct rpdr_ext * ex)1420 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1421 {
1422 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1423 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1424 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1425 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1426 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1427 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1428
1429 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1430 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1431
1432 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1433 }
1434
1435 /* Create a runtime procedure table from the .mdebug section. */
1436
1437 static bfd_boolean
mips_elf_create_procedure_table(void * handle,bfd * abfd,struct bfd_link_info * info,asection * s,struct ecoff_debug_info * debug)1438 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1439 struct bfd_link_info *info, asection *s,
1440 struct ecoff_debug_info *debug)
1441 {
1442 const struct ecoff_debug_swap *swap;
1443 HDRR *hdr = &debug->symbolic_header;
1444 RPDR *rpdr, *rp;
1445 struct rpdr_ext *erp;
1446 void *rtproc;
1447 struct pdr_ext *epdr;
1448 struct sym_ext *esym;
1449 char *ss, **sv;
1450 char *str;
1451 bfd_size_type size;
1452 bfd_size_type count;
1453 unsigned long sindex;
1454 unsigned long i;
1455 PDR pdr;
1456 SYMR sym;
1457 const char *no_name_func = _("static procedure (no name)");
1458
1459 epdr = NULL;
1460 rpdr = NULL;
1461 esym = NULL;
1462 ss = NULL;
1463 sv = NULL;
1464
1465 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1466
1467 sindex = strlen (no_name_func) + 1;
1468 count = hdr->ipdMax;
1469 if (count > 0)
1470 {
1471 size = swap->external_pdr_size;
1472
1473 epdr = bfd_malloc (size * count);
1474 if (epdr == NULL)
1475 goto error_return;
1476
1477 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1478 goto error_return;
1479
1480 size = sizeof (RPDR);
1481 rp = rpdr = bfd_malloc (size * count);
1482 if (rpdr == NULL)
1483 goto error_return;
1484
1485 size = sizeof (char *);
1486 sv = bfd_malloc (size * count);
1487 if (sv == NULL)
1488 goto error_return;
1489
1490 count = hdr->isymMax;
1491 size = swap->external_sym_size;
1492 esym = bfd_malloc (size * count);
1493 if (esym == NULL)
1494 goto error_return;
1495
1496 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1497 goto error_return;
1498
1499 count = hdr->issMax;
1500 ss = bfd_malloc (count);
1501 if (ss == NULL)
1502 goto error_return;
1503 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1504 goto error_return;
1505
1506 count = hdr->ipdMax;
1507 for (i = 0; i < (unsigned long) count; i++, rp++)
1508 {
1509 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1510 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1511 rp->adr = sym.value;
1512 rp->regmask = pdr.regmask;
1513 rp->regoffset = pdr.regoffset;
1514 rp->fregmask = pdr.fregmask;
1515 rp->fregoffset = pdr.fregoffset;
1516 rp->frameoffset = pdr.frameoffset;
1517 rp->framereg = pdr.framereg;
1518 rp->pcreg = pdr.pcreg;
1519 rp->irpss = sindex;
1520 sv[i] = ss + sym.iss;
1521 sindex += strlen (sv[i]) + 1;
1522 }
1523 }
1524
1525 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1526 size = BFD_ALIGN (size, 16);
1527 rtproc = bfd_alloc (abfd, size);
1528 if (rtproc == NULL)
1529 {
1530 mips_elf_hash_table (info)->procedure_count = 0;
1531 goto error_return;
1532 }
1533
1534 mips_elf_hash_table (info)->procedure_count = count + 2;
1535
1536 erp = rtproc;
1537 memset (erp, 0, sizeof (struct rpdr_ext));
1538 erp++;
1539 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1540 strcpy (str, no_name_func);
1541 str += strlen (no_name_func) + 1;
1542 for (i = 0; i < count; i++)
1543 {
1544 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1545 strcpy (str, sv[i]);
1546 str += strlen (sv[i]) + 1;
1547 }
1548 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1549
1550 /* Set the size and contents of .rtproc section. */
1551 s->size = size;
1552 s->contents = rtproc;
1553
1554 /* Skip this section later on (I don't think this currently
1555 matters, but someday it might). */
1556 s->map_head.link_order = NULL;
1557
1558 if (epdr != NULL)
1559 free (epdr);
1560 if (rpdr != NULL)
1561 free (rpdr);
1562 if (esym != NULL)
1563 free (esym);
1564 if (ss != NULL)
1565 free (ss);
1566 if (sv != NULL)
1567 free (sv);
1568
1569 return TRUE;
1570
1571 error_return:
1572 if (epdr != NULL)
1573 free (epdr);
1574 if (rpdr != NULL)
1575 free (rpdr);
1576 if (esym != NULL)
1577 free (esym);
1578 if (ss != NULL)
1579 free (ss);
1580 if (sv != NULL)
1581 free (sv);
1582 return FALSE;
1583 }
1584
1585 /* We're going to create a stub for H. Create a symbol for the stub's
1586 value and size, to help make the disassembly easier to read. */
1587
1588 static bfd_boolean
mips_elf_create_stub_symbol(struct bfd_link_info * info,struct mips_elf_link_hash_entry * h,const char * prefix,asection * s,bfd_vma value,bfd_vma size)1589 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1590 struct mips_elf_link_hash_entry *h,
1591 const char *prefix, asection *s, bfd_vma value,
1592 bfd_vma size)
1593 {
1594 struct bfd_link_hash_entry *bh;
1595 struct elf_link_hash_entry *elfh;
1596 const char *name;
1597
1598 if (ELF_ST_IS_MICROMIPS (h->root.other))
1599 value |= 1;
1600
1601 /* Create a new symbol. */
1602 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1603 bh = NULL;
1604 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1605 BSF_LOCAL, s, value, NULL,
1606 TRUE, FALSE, &bh))
1607 return FALSE;
1608
1609 /* Make it a local function. */
1610 elfh = (struct elf_link_hash_entry *) bh;
1611 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1612 elfh->size = size;
1613 elfh->forced_local = 1;
1614 return TRUE;
1615 }
1616
1617 /* We're about to redefine H. Create a symbol to represent H's
1618 current value and size, to help make the disassembly easier
1619 to read. */
1620
1621 static bfd_boolean
mips_elf_create_shadow_symbol(struct bfd_link_info * info,struct mips_elf_link_hash_entry * h,const char * prefix)1622 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1623 struct mips_elf_link_hash_entry *h,
1624 const char *prefix)
1625 {
1626 struct bfd_link_hash_entry *bh;
1627 struct elf_link_hash_entry *elfh;
1628 const char *name;
1629 asection *s;
1630 bfd_vma value;
1631
1632 /* Read the symbol's value. */
1633 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1634 || h->root.root.type == bfd_link_hash_defweak);
1635 s = h->root.root.u.def.section;
1636 value = h->root.root.u.def.value;
1637
1638 /* Create a new symbol. */
1639 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1640 bh = NULL;
1641 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1642 BSF_LOCAL, s, value, NULL,
1643 TRUE, FALSE, &bh))
1644 return FALSE;
1645
1646 /* Make it local and copy the other attributes from H. */
1647 elfh = (struct elf_link_hash_entry *) bh;
1648 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1649 elfh->other = h->root.other;
1650 elfh->size = h->root.size;
1651 elfh->forced_local = 1;
1652 return TRUE;
1653 }
1654
1655 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1656 function rather than to a hard-float stub. */
1657
1658 static bfd_boolean
section_allows_mips16_refs_p(asection * section)1659 section_allows_mips16_refs_p (asection *section)
1660 {
1661 const char *name;
1662
1663 name = bfd_get_section_name (section->owner, section);
1664 return (FN_STUB_P (name)
1665 || CALL_STUB_P (name)
1666 || CALL_FP_STUB_P (name)
1667 || strcmp (name, ".pdr") == 0);
1668 }
1669
1670 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1671 stub section of some kind. Return the R_SYMNDX of the target
1672 function, or 0 if we can't decide which function that is. */
1673
1674 static unsigned long
mips16_stub_symndx(const struct elf_backend_data * bed,asection * sec ATTRIBUTE_UNUSED,const Elf_Internal_Rela * relocs,const Elf_Internal_Rela * relend)1675 mips16_stub_symndx (const struct elf_backend_data *bed,
1676 asection *sec ATTRIBUTE_UNUSED,
1677 const Elf_Internal_Rela *relocs,
1678 const Elf_Internal_Rela *relend)
1679 {
1680 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1681 const Elf_Internal_Rela *rel;
1682
1683 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1684 one in a compound relocation. */
1685 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1686 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1687 return ELF_R_SYM (sec->owner, rel->r_info);
1688
1689 /* Otherwise trust the first relocation, whatever its kind. This is
1690 the traditional behavior. */
1691 if (relocs < relend)
1692 return ELF_R_SYM (sec->owner, relocs->r_info);
1693
1694 return 0;
1695 }
1696
1697 /* Check the mips16 stubs for a particular symbol, and see if we can
1698 discard them. */
1699
1700 static void
mips_elf_check_mips16_stubs(struct bfd_link_info * info,struct mips_elf_link_hash_entry * h)1701 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1702 struct mips_elf_link_hash_entry *h)
1703 {
1704 /* Dynamic symbols must use the standard call interface, in case other
1705 objects try to call them. */
1706 if (h->fn_stub != NULL
1707 && h->root.dynindx != -1)
1708 {
1709 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1710 h->need_fn_stub = TRUE;
1711 }
1712
1713 if (h->fn_stub != NULL
1714 && ! h->need_fn_stub)
1715 {
1716 /* We don't need the fn_stub; the only references to this symbol
1717 are 16 bit calls. Clobber the size to 0 to prevent it from
1718 being included in the link. */
1719 h->fn_stub->size = 0;
1720 h->fn_stub->flags &= ~SEC_RELOC;
1721 h->fn_stub->reloc_count = 0;
1722 h->fn_stub->flags |= SEC_EXCLUDE;
1723 }
1724
1725 if (h->call_stub != NULL
1726 && ELF_ST_IS_MIPS16 (h->root.other))
1727 {
1728 /* We don't need the call_stub; this is a 16 bit function, so
1729 calls from other 16 bit functions are OK. Clobber the size
1730 to 0 to prevent it from being included in the link. */
1731 h->call_stub->size = 0;
1732 h->call_stub->flags &= ~SEC_RELOC;
1733 h->call_stub->reloc_count = 0;
1734 h->call_stub->flags |= SEC_EXCLUDE;
1735 }
1736
1737 if (h->call_fp_stub != NULL
1738 && ELF_ST_IS_MIPS16 (h->root.other))
1739 {
1740 /* We don't need the call_stub; this is a 16 bit function, so
1741 calls from other 16 bit functions are OK. Clobber the size
1742 to 0 to prevent it from being included in the link. */
1743 h->call_fp_stub->size = 0;
1744 h->call_fp_stub->flags &= ~SEC_RELOC;
1745 h->call_fp_stub->reloc_count = 0;
1746 h->call_fp_stub->flags |= SEC_EXCLUDE;
1747 }
1748 }
1749
1750 /* Hashtable callbacks for mips_elf_la25_stubs. */
1751
1752 static hashval_t
mips_elf_la25_stub_hash(const void * entry_)1753 mips_elf_la25_stub_hash (const void *entry_)
1754 {
1755 const struct mips_elf_la25_stub *entry;
1756
1757 entry = (struct mips_elf_la25_stub *) entry_;
1758 return entry->h->root.root.u.def.section->id
1759 + entry->h->root.root.u.def.value;
1760 }
1761
1762 static int
mips_elf_la25_stub_eq(const void * entry1_,const void * entry2_)1763 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1764 {
1765 const struct mips_elf_la25_stub *entry1, *entry2;
1766
1767 entry1 = (struct mips_elf_la25_stub *) entry1_;
1768 entry2 = (struct mips_elf_la25_stub *) entry2_;
1769 return ((entry1->h->root.root.u.def.section
1770 == entry2->h->root.root.u.def.section)
1771 && (entry1->h->root.root.u.def.value
1772 == entry2->h->root.root.u.def.value));
1773 }
1774
1775 /* Called by the linker to set up the la25 stub-creation code. FN is
1776 the linker's implementation of add_stub_function. Return true on
1777 success. */
1778
1779 bfd_boolean
_bfd_mips_elf_init_stubs(struct bfd_link_info * info,asection * (* fn)(const char *,asection *,asection *))1780 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1781 asection *(*fn) (const char *, asection *,
1782 asection *))
1783 {
1784 struct mips_elf_link_hash_table *htab;
1785
1786 htab = mips_elf_hash_table (info);
1787 if (htab == NULL)
1788 return FALSE;
1789
1790 htab->add_stub_section = fn;
1791 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1792 mips_elf_la25_stub_eq, NULL);
1793 if (htab->la25_stubs == NULL)
1794 return FALSE;
1795
1796 return TRUE;
1797 }
1798
1799 /* Return true if H is a locally-defined PIC function, in the sense
1800 that it or its fn_stub might need $25 to be valid on entry.
1801 Note that MIPS16 functions set up $gp using PC-relative instructions,
1802 so they themselves never need $25 to be valid. Only non-MIPS16
1803 entry points are of interest here. */
1804
1805 static bfd_boolean
mips_elf_local_pic_function_p(struct mips_elf_link_hash_entry * h)1806 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1807 {
1808 return ((h->root.root.type == bfd_link_hash_defined
1809 || h->root.root.type == bfd_link_hash_defweak)
1810 && h->root.def_regular
1811 && !bfd_is_abs_section (h->root.root.u.def.section)
1812 && (!ELF_ST_IS_MIPS16 (h->root.other)
1813 || (h->fn_stub && h->need_fn_stub))
1814 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1815 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1816 }
1817
1818 /* Set *SEC to the input section that contains the target of STUB.
1819 Return the offset of the target from the start of that section. */
1820
1821 static bfd_vma
mips_elf_get_la25_target(struct mips_elf_la25_stub * stub,asection ** sec)1822 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1823 asection **sec)
1824 {
1825 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1826 {
1827 BFD_ASSERT (stub->h->need_fn_stub);
1828 *sec = stub->h->fn_stub;
1829 return 0;
1830 }
1831 else
1832 {
1833 *sec = stub->h->root.root.u.def.section;
1834 return stub->h->root.root.u.def.value;
1835 }
1836 }
1837
1838 /* STUB describes an la25 stub that we have decided to implement
1839 by inserting an LUI/ADDIU pair before the target function.
1840 Create the section and redirect the function symbol to it. */
1841
1842 static bfd_boolean
mips_elf_add_la25_intro(struct mips_elf_la25_stub * stub,struct bfd_link_info * info)1843 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1844 struct bfd_link_info *info)
1845 {
1846 struct mips_elf_link_hash_table *htab;
1847 char *name;
1848 asection *s, *input_section;
1849 unsigned int align;
1850
1851 htab = mips_elf_hash_table (info);
1852 if (htab == NULL)
1853 return FALSE;
1854
1855 /* Create a unique name for the new section. */
1856 name = bfd_malloc (11 + sizeof (".text.stub."));
1857 if (name == NULL)
1858 return FALSE;
1859 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1860
1861 /* Create the section. */
1862 mips_elf_get_la25_target (stub, &input_section);
1863 s = htab->add_stub_section (name, input_section,
1864 input_section->output_section);
1865 if (s == NULL)
1866 return FALSE;
1867
1868 /* Make sure that any padding goes before the stub. */
1869 align = input_section->alignment_power;
1870 if (!bfd_set_section_alignment (s->owner, s, align))
1871 return FALSE;
1872 if (align > 3)
1873 s->size = (1 << align) - 8;
1874
1875 /* Create a symbol for the stub. */
1876 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1877 stub->stub_section = s;
1878 stub->offset = s->size;
1879
1880 /* Allocate room for it. */
1881 s->size += 8;
1882 return TRUE;
1883 }
1884
1885 /* STUB describes an la25 stub that we have decided to implement
1886 with a separate trampoline. Allocate room for it and redirect
1887 the function symbol to it. */
1888
1889 static bfd_boolean
mips_elf_add_la25_trampoline(struct mips_elf_la25_stub * stub,struct bfd_link_info * info)1890 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1891 struct bfd_link_info *info)
1892 {
1893 struct mips_elf_link_hash_table *htab;
1894 asection *s;
1895
1896 htab = mips_elf_hash_table (info);
1897 if (htab == NULL)
1898 return FALSE;
1899
1900 /* Create a trampoline section, if we haven't already. */
1901 s = htab->strampoline;
1902 if (s == NULL)
1903 {
1904 asection *input_section = stub->h->root.root.u.def.section;
1905 s = htab->add_stub_section (".text", NULL,
1906 input_section->output_section);
1907 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1908 return FALSE;
1909 htab->strampoline = s;
1910 }
1911
1912 /* Create a symbol for the stub. */
1913 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1914 stub->stub_section = s;
1915 stub->offset = s->size;
1916
1917 /* Allocate room for it. */
1918 s->size += 16;
1919 return TRUE;
1920 }
1921
1922 /* H describes a symbol that needs an la25 stub. Make sure that an
1923 appropriate stub exists and point H at it. */
1924
1925 static bfd_boolean
mips_elf_add_la25_stub(struct bfd_link_info * info,struct mips_elf_link_hash_entry * h)1926 mips_elf_add_la25_stub (struct bfd_link_info *info,
1927 struct mips_elf_link_hash_entry *h)
1928 {
1929 struct mips_elf_link_hash_table *htab;
1930 struct mips_elf_la25_stub search, *stub;
1931 bfd_boolean use_trampoline_p;
1932 asection *s;
1933 bfd_vma value;
1934 void **slot;
1935
1936 /* Describe the stub we want. */
1937 search.stub_section = NULL;
1938 search.offset = 0;
1939 search.h = h;
1940
1941 /* See if we've already created an equivalent stub. */
1942 htab = mips_elf_hash_table (info);
1943 if (htab == NULL)
1944 return FALSE;
1945
1946 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1947 if (slot == NULL)
1948 return FALSE;
1949
1950 stub = (struct mips_elf_la25_stub *) *slot;
1951 if (stub != NULL)
1952 {
1953 /* We can reuse the existing stub. */
1954 h->la25_stub = stub;
1955 return TRUE;
1956 }
1957
1958 /* Create a permanent copy of ENTRY and add it to the hash table. */
1959 stub = bfd_malloc (sizeof (search));
1960 if (stub == NULL)
1961 return FALSE;
1962 *stub = search;
1963 *slot = stub;
1964
1965 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1966 of the section and if we would need no more than 2 nops. */
1967 value = mips_elf_get_la25_target (stub, &s);
1968 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1969
1970 h->la25_stub = stub;
1971 return (use_trampoline_p
1972 ? mips_elf_add_la25_trampoline (stub, info)
1973 : mips_elf_add_la25_intro (stub, info));
1974 }
1975
1976 /* A mips_elf_link_hash_traverse callback that is called before sizing
1977 sections. DATA points to a mips_htab_traverse_info structure. */
1978
1979 static bfd_boolean
mips_elf_check_symbols(struct mips_elf_link_hash_entry * h,void * data)1980 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1981 {
1982 struct mips_htab_traverse_info *hti;
1983
1984 hti = (struct mips_htab_traverse_info *) data;
1985 if (!hti->info->relocatable)
1986 mips_elf_check_mips16_stubs (hti->info, h);
1987
1988 if (mips_elf_local_pic_function_p (h))
1989 {
1990 /* PR 12845: If H is in a section that has been garbage
1991 collected it will have its output section set to *ABS*. */
1992 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1993 return TRUE;
1994
1995 /* H is a function that might need $25 to be valid on entry.
1996 If we're creating a non-PIC relocatable object, mark H as
1997 being PIC. If we're creating a non-relocatable object with
1998 non-PIC branches and jumps to H, make sure that H has an la25
1999 stub. */
2000 if (hti->info->relocatable)
2001 {
2002 if (!PIC_OBJECT_P (hti->output_bfd))
2003 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2004 }
2005 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2006 {
2007 hti->error = TRUE;
2008 return FALSE;
2009 }
2010 }
2011 return TRUE;
2012 }
2013
2014 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2015 Most mips16 instructions are 16 bits, but these instructions
2016 are 32 bits.
2017
2018 The format of these instructions is:
2019
2020 +--------------+--------------------------------+
2021 | JALX | X| Imm 20:16 | Imm 25:21 |
2022 +--------------+--------------------------------+
2023 | Immediate 15:0 |
2024 +-----------------------------------------------+
2025
2026 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2027 Note that the immediate value in the first word is swapped.
2028
2029 When producing a relocatable object file, R_MIPS16_26 is
2030 handled mostly like R_MIPS_26. In particular, the addend is
2031 stored as a straight 26-bit value in a 32-bit instruction.
2032 (gas makes life simpler for itself by never adjusting a
2033 R_MIPS16_26 reloc to be against a section, so the addend is
2034 always zero). However, the 32 bit instruction is stored as 2
2035 16-bit values, rather than a single 32-bit value. In a
2036 big-endian file, the result is the same; in a little-endian
2037 file, the two 16-bit halves of the 32 bit value are swapped.
2038 This is so that a disassembler can recognize the jal
2039 instruction.
2040
2041 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2042 instruction stored as two 16-bit values. The addend A is the
2043 contents of the targ26 field. The calculation is the same as
2044 R_MIPS_26. When storing the calculated value, reorder the
2045 immediate value as shown above, and don't forget to store the
2046 value as two 16-bit values.
2047
2048 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2049 defined as
2050
2051 big-endian:
2052 +--------+----------------------+
2053 | | |
2054 | | targ26-16 |
2055 |31 26|25 0|
2056 +--------+----------------------+
2057
2058 little-endian:
2059 +----------+------+-------------+
2060 | | | |
2061 | sub1 | | sub2 |
2062 |0 9|10 15|16 31|
2063 +----------+--------------------+
2064 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2065 ((sub1 << 16) | sub2)).
2066
2067 When producing a relocatable object file, the calculation is
2068 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2069 When producing a fully linked file, the calculation is
2070 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2071 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2072
2073 The table below lists the other MIPS16 instruction relocations.
2074 Each one is calculated in the same way as the non-MIPS16 relocation
2075 given on the right, but using the extended MIPS16 layout of 16-bit
2076 immediate fields:
2077
2078 R_MIPS16_GPREL R_MIPS_GPREL16
2079 R_MIPS16_GOT16 R_MIPS_GOT16
2080 R_MIPS16_CALL16 R_MIPS_CALL16
2081 R_MIPS16_HI16 R_MIPS_HI16
2082 R_MIPS16_LO16 R_MIPS_LO16
2083
2084 A typical instruction will have a format like this:
2085
2086 +--------------+--------------------------------+
2087 | EXTEND | Imm 10:5 | Imm 15:11 |
2088 +--------------+--------------------------------+
2089 | Major | rx | ry | Imm 4:0 |
2090 +--------------+--------------------------------+
2091
2092 EXTEND is the five bit value 11110. Major is the instruction
2093 opcode.
2094
2095 All we need to do here is shuffle the bits appropriately.
2096 As above, the two 16-bit halves must be swapped on a
2097 little-endian system. */
2098
2099 static inline bfd_boolean
mips16_reloc_p(int r_type)2100 mips16_reloc_p (int r_type)
2101 {
2102 switch (r_type)
2103 {
2104 case R_MIPS16_26:
2105 case R_MIPS16_GPREL:
2106 case R_MIPS16_GOT16:
2107 case R_MIPS16_CALL16:
2108 case R_MIPS16_HI16:
2109 case R_MIPS16_LO16:
2110 case R_MIPS16_TLS_GD:
2111 case R_MIPS16_TLS_LDM:
2112 case R_MIPS16_TLS_DTPREL_HI16:
2113 case R_MIPS16_TLS_DTPREL_LO16:
2114 case R_MIPS16_TLS_GOTTPREL:
2115 case R_MIPS16_TLS_TPREL_HI16:
2116 case R_MIPS16_TLS_TPREL_LO16:
2117 return TRUE;
2118
2119 default:
2120 return FALSE;
2121 }
2122 }
2123
2124 /* Check if a microMIPS reloc. */
2125
2126 static inline bfd_boolean
micromips_reloc_p(unsigned int r_type)2127 micromips_reloc_p (unsigned int r_type)
2128 {
2129 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2130 }
2131
2132 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2133 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2134 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2135
2136 static inline bfd_boolean
micromips_reloc_shuffle_p(unsigned int r_type)2137 micromips_reloc_shuffle_p (unsigned int r_type)
2138 {
2139 return (micromips_reloc_p (r_type)
2140 && r_type != R_MICROMIPS_PC7_S1
2141 && r_type != R_MICROMIPS_PC10_S1);
2142 }
2143
2144 static inline bfd_boolean
got16_reloc_p(int r_type)2145 got16_reloc_p (int r_type)
2146 {
2147 return (r_type == R_MIPS_GOT16
2148 || r_type == R_MIPS16_GOT16
2149 || r_type == R_MICROMIPS_GOT16);
2150 }
2151
2152 static inline bfd_boolean
call16_reloc_p(int r_type)2153 call16_reloc_p (int r_type)
2154 {
2155 return (r_type == R_MIPS_CALL16
2156 || r_type == R_MIPS16_CALL16
2157 || r_type == R_MICROMIPS_CALL16);
2158 }
2159
2160 static inline bfd_boolean
got_disp_reloc_p(unsigned int r_type)2161 got_disp_reloc_p (unsigned int r_type)
2162 {
2163 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2164 }
2165
2166 static inline bfd_boolean
got_page_reloc_p(unsigned int r_type)2167 got_page_reloc_p (unsigned int r_type)
2168 {
2169 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2170 }
2171
2172 static inline bfd_boolean
got_ofst_reloc_p(unsigned int r_type)2173 got_ofst_reloc_p (unsigned int r_type)
2174 {
2175 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
2176 }
2177
2178 static inline bfd_boolean
got_hi16_reloc_p(unsigned int r_type)2179 got_hi16_reloc_p (unsigned int r_type)
2180 {
2181 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
2182 }
2183
2184 static inline bfd_boolean
got_lo16_reloc_p(unsigned int r_type)2185 got_lo16_reloc_p (unsigned int r_type)
2186 {
2187 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2188 }
2189
2190 static inline bfd_boolean
call_hi16_reloc_p(unsigned int r_type)2191 call_hi16_reloc_p (unsigned int r_type)
2192 {
2193 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2194 }
2195
2196 static inline bfd_boolean
call_lo16_reloc_p(unsigned int r_type)2197 call_lo16_reloc_p (unsigned int r_type)
2198 {
2199 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2200 }
2201
2202 static inline bfd_boolean
hi16_reloc_p(int r_type)2203 hi16_reloc_p (int r_type)
2204 {
2205 return (r_type == R_MIPS_HI16
2206 || r_type == R_MIPS16_HI16
2207 || r_type == R_MICROMIPS_HI16
2208 || r_type == R_MIPS_PCHI16);
2209 }
2210
2211 static inline bfd_boolean
lo16_reloc_p(int r_type)2212 lo16_reloc_p (int r_type)
2213 {
2214 return (r_type == R_MIPS_LO16
2215 || r_type == R_MIPS16_LO16
2216 || r_type == R_MICROMIPS_LO16
2217 || r_type == R_MIPS_PCLO16);
2218 }
2219
2220 static inline bfd_boolean
mips16_call_reloc_p(int r_type)2221 mips16_call_reloc_p (int r_type)
2222 {
2223 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2224 }
2225
2226 static inline bfd_boolean
jal_reloc_p(int r_type)2227 jal_reloc_p (int r_type)
2228 {
2229 return (r_type == R_MIPS_26
2230 || r_type == R_MIPS16_26
2231 || r_type == R_MICROMIPS_26_S1);
2232 }
2233
2234 static inline bfd_boolean
aligned_pcrel_reloc_p(int r_type)2235 aligned_pcrel_reloc_p (int r_type)
2236 {
2237 return (r_type == R_MIPS_PC18_S3
2238 || r_type == R_MIPS_PC19_S2);
2239 }
2240
2241 static inline bfd_boolean
micromips_branch_reloc_p(int r_type)2242 micromips_branch_reloc_p (int r_type)
2243 {
2244 return (r_type == R_MICROMIPS_26_S1
2245 || r_type == R_MICROMIPS_PC16_S1
2246 || r_type == R_MICROMIPS_PC10_S1
2247 || r_type == R_MICROMIPS_PC7_S1);
2248 }
2249
2250 static inline bfd_boolean
tls_gd_reloc_p(unsigned int r_type)2251 tls_gd_reloc_p (unsigned int r_type)
2252 {
2253 return (r_type == R_MIPS_TLS_GD
2254 || r_type == R_MIPS16_TLS_GD
2255 || r_type == R_MICROMIPS_TLS_GD);
2256 }
2257
2258 static inline bfd_boolean
tls_ldm_reloc_p(unsigned int r_type)2259 tls_ldm_reloc_p (unsigned int r_type)
2260 {
2261 return (r_type == R_MIPS_TLS_LDM
2262 || r_type == R_MIPS16_TLS_LDM
2263 || r_type == R_MICROMIPS_TLS_LDM);
2264 }
2265
2266 static inline bfd_boolean
tls_gottprel_reloc_p(unsigned int r_type)2267 tls_gottprel_reloc_p (unsigned int r_type)
2268 {
2269 return (r_type == R_MIPS_TLS_GOTTPREL
2270 || r_type == R_MIPS16_TLS_GOTTPREL
2271 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2272 }
2273
2274 void
_bfd_mips_elf_reloc_unshuffle(bfd * abfd,int r_type,bfd_boolean jal_shuffle,bfd_byte * data)2275 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2276 bfd_boolean jal_shuffle, bfd_byte *data)
2277 {
2278 bfd_vma first, second, val;
2279
2280 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2281 return;
2282
2283 /* Pick up the first and second halfwords of the instruction. */
2284 first = bfd_get_16 (abfd, data);
2285 second = bfd_get_16 (abfd, data + 2);
2286 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2287 val = first << 16 | second;
2288 else if (r_type != R_MIPS16_26)
2289 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2290 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2291 else
2292 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2293 | ((first & 0x1f) << 21) | second);
2294 bfd_put_32 (abfd, val, data);
2295 }
2296
2297 void
_bfd_mips_elf_reloc_shuffle(bfd * abfd,int r_type,bfd_boolean jal_shuffle,bfd_byte * data)2298 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2299 bfd_boolean jal_shuffle, bfd_byte *data)
2300 {
2301 bfd_vma first, second, val;
2302
2303 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2304 return;
2305
2306 val = bfd_get_32 (abfd, data);
2307 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2308 {
2309 second = val & 0xffff;
2310 first = val >> 16;
2311 }
2312 else if (r_type != R_MIPS16_26)
2313 {
2314 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2315 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2316 }
2317 else
2318 {
2319 second = val & 0xffff;
2320 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2321 | ((val >> 21) & 0x1f);
2322 }
2323 bfd_put_16 (abfd, second, data + 2);
2324 bfd_put_16 (abfd, first, data);
2325 }
2326
2327 bfd_reloc_status_type
_bfd_mips_elf_gprel16_with_gp(bfd * abfd,asymbol * symbol,arelent * reloc_entry,asection * input_section,bfd_boolean relocatable,void * data,bfd_vma gp)2328 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2329 arelent *reloc_entry, asection *input_section,
2330 bfd_boolean relocatable, void *data, bfd_vma gp)
2331 {
2332 bfd_vma relocation;
2333 bfd_signed_vma val;
2334 bfd_reloc_status_type status;
2335
2336 if (bfd_is_com_section (symbol->section))
2337 relocation = 0;
2338 else
2339 relocation = symbol->value;
2340
2341 relocation += symbol->section->output_section->vma;
2342 relocation += symbol->section->output_offset;
2343
2344 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2345 return bfd_reloc_outofrange;
2346
2347 /* Set val to the offset into the section or symbol. */
2348 val = reloc_entry->addend;
2349
2350 _bfd_mips_elf_sign_extend (val, 16);
2351
2352 /* Adjust val for the final section location and GP value. If we
2353 are producing relocatable output, we don't want to do this for
2354 an external symbol. */
2355 if (! relocatable
2356 || (symbol->flags & BSF_SECTION_SYM) != 0)
2357 val += relocation - gp;
2358
2359 if (reloc_entry->howto->partial_inplace)
2360 {
2361 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2362 (bfd_byte *) data
2363 + reloc_entry->address);
2364 if (status != bfd_reloc_ok)
2365 return status;
2366 }
2367 else
2368 reloc_entry->addend = val;
2369
2370 if (relocatable)
2371 reloc_entry->address += input_section->output_offset;
2372
2373 return bfd_reloc_ok;
2374 }
2375
2376 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2377 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2378 that contains the relocation field and DATA points to the start of
2379 INPUT_SECTION. */
2380
2381 struct mips_hi16
2382 {
2383 struct mips_hi16 *next;
2384 bfd_byte *data;
2385 asection *input_section;
2386 arelent rel;
2387 };
2388
2389 /* FIXME: This should not be a static variable. */
2390
2391 static struct mips_hi16 *mips_hi16_list;
2392
2393 /* A howto special_function for REL *HI16 relocations. We can only
2394 calculate the correct value once we've seen the partnering
2395 *LO16 relocation, so just save the information for later.
2396
2397 The ABI requires that the *LO16 immediately follow the *HI16.
2398 However, as a GNU extension, we permit an arbitrary number of
2399 *HI16s to be associated with a single *LO16. This significantly
2400 simplies the relocation handling in gcc. */
2401
2402 bfd_reloc_status_type
_bfd_mips_elf_hi16_reloc(bfd * abfd ATTRIBUTE_UNUSED,arelent * reloc_entry,asymbol * symbol ATTRIBUTE_UNUSED,void * data,asection * input_section,bfd * output_bfd,char ** error_message ATTRIBUTE_UNUSED)2403 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2404 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2405 asection *input_section, bfd *output_bfd,
2406 char **error_message ATTRIBUTE_UNUSED)
2407 {
2408 struct mips_hi16 *n;
2409
2410 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2411 return bfd_reloc_outofrange;
2412
2413 n = bfd_malloc (sizeof *n);
2414 if (n == NULL)
2415 return bfd_reloc_outofrange;
2416
2417 n->next = mips_hi16_list;
2418 n->data = data;
2419 n->input_section = input_section;
2420 n->rel = *reloc_entry;
2421 mips_hi16_list = n;
2422
2423 if (output_bfd != NULL)
2424 reloc_entry->address += input_section->output_offset;
2425
2426 return bfd_reloc_ok;
2427 }
2428
2429 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2430 like any other 16-bit relocation when applied to global symbols, but is
2431 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2432
2433 bfd_reloc_status_type
_bfd_mips_elf_got16_reloc(bfd * abfd,arelent * reloc_entry,asymbol * symbol,void * data,asection * input_section,bfd * output_bfd,char ** error_message)2434 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2435 void *data, asection *input_section,
2436 bfd *output_bfd, char **error_message)
2437 {
2438 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2439 || bfd_is_und_section (bfd_get_section (symbol))
2440 || bfd_is_com_section (bfd_get_section (symbol)))
2441 /* The relocation is against a global symbol. */
2442 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2443 input_section, output_bfd,
2444 error_message);
2445
2446 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2447 input_section, output_bfd, error_message);
2448 }
2449
2450 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2451 is a straightforward 16 bit inplace relocation, but we must deal with
2452 any partnering high-part relocations as well. */
2453
2454 bfd_reloc_status_type
_bfd_mips_elf_lo16_reloc(bfd * abfd,arelent * reloc_entry,asymbol * symbol,void * data,asection * input_section,bfd * output_bfd,char ** error_message)2455 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2456 void *data, asection *input_section,
2457 bfd *output_bfd, char **error_message)
2458 {
2459 bfd_vma vallo;
2460 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2461
2462 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2463 return bfd_reloc_outofrange;
2464
2465 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2466 location);
2467 vallo = bfd_get_32 (abfd, location);
2468 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2469 location);
2470
2471 while (mips_hi16_list != NULL)
2472 {
2473 bfd_reloc_status_type ret;
2474 struct mips_hi16 *hi;
2475
2476 hi = mips_hi16_list;
2477
2478 /* R_MIPS*_GOT16 relocations are something of a special case. We
2479 want to install the addend in the same way as for a R_MIPS*_HI16
2480 relocation (with a rightshift of 16). However, since GOT16
2481 relocations can also be used with global symbols, their howto
2482 has a rightshift of 0. */
2483 if (hi->rel.howto->type == R_MIPS_GOT16)
2484 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2485 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2486 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2487 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2488 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2489
2490 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2491 carry or borrow will induce a change of +1 or -1 in the high part. */
2492 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2493
2494 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2495 hi->input_section, output_bfd,
2496 error_message);
2497 if (ret != bfd_reloc_ok)
2498 return ret;
2499
2500 mips_hi16_list = hi->next;
2501 free (hi);
2502 }
2503
2504 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2505 input_section, output_bfd,
2506 error_message);
2507 }
2508
2509 /* A generic howto special_function. This calculates and installs the
2510 relocation itself, thus avoiding the oft-discussed problems in
2511 bfd_perform_relocation and bfd_install_relocation. */
2512
2513 bfd_reloc_status_type
_bfd_mips_elf_generic_reloc(bfd * abfd ATTRIBUTE_UNUSED,arelent * reloc_entry,asymbol * symbol,void * data ATTRIBUTE_UNUSED,asection * input_section,bfd * output_bfd,char ** error_message ATTRIBUTE_UNUSED)2514 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2515 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2516 asection *input_section, bfd *output_bfd,
2517 char **error_message ATTRIBUTE_UNUSED)
2518 {
2519 bfd_signed_vma val;
2520 bfd_reloc_status_type status;
2521 bfd_boolean relocatable;
2522
2523 relocatable = (output_bfd != NULL);
2524
2525 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2526 return bfd_reloc_outofrange;
2527
2528 /* Build up the field adjustment in VAL. */
2529 val = 0;
2530 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2531 {
2532 /* Either we're calculating the final field value or we have a
2533 relocation against a section symbol. Add in the section's
2534 offset or address. */
2535 val += symbol->section->output_section->vma;
2536 val += symbol->section->output_offset;
2537 }
2538
2539 if (!relocatable)
2540 {
2541 /* We're calculating the final field value. Add in the symbol's value
2542 and, if pc-relative, subtract the address of the field itself. */
2543 val += symbol->value;
2544 if (reloc_entry->howto->pc_relative)
2545 {
2546 val -= input_section->output_section->vma;
2547 val -= input_section->output_offset;
2548 val -= reloc_entry->address;
2549 }
2550 }
2551
2552 /* VAL is now the final adjustment. If we're keeping this relocation
2553 in the output file, and if the relocation uses a separate addend,
2554 we just need to add VAL to that addend. Otherwise we need to add
2555 VAL to the relocation field itself. */
2556 if (relocatable && !reloc_entry->howto->partial_inplace)
2557 reloc_entry->addend += val;
2558 else
2559 {
2560 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2561
2562 /* Add in the separate addend, if any. */
2563 val += reloc_entry->addend;
2564
2565 /* Add VAL to the relocation field. */
2566 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2567 location);
2568 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2569 location);
2570 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2571 location);
2572
2573 if (status != bfd_reloc_ok)
2574 return status;
2575 }
2576
2577 if (relocatable)
2578 reloc_entry->address += input_section->output_offset;
2579
2580 return bfd_reloc_ok;
2581 }
2582
2583 /* Swap an entry in a .gptab section. Note that these routines rely
2584 on the equivalence of the two elements of the union. */
2585
2586 static void
bfd_mips_elf32_swap_gptab_in(bfd * abfd,const Elf32_External_gptab * ex,Elf32_gptab * in)2587 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2588 Elf32_gptab *in)
2589 {
2590 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2591 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2592 }
2593
2594 static void
bfd_mips_elf32_swap_gptab_out(bfd * abfd,const Elf32_gptab * in,Elf32_External_gptab * ex)2595 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2596 Elf32_External_gptab *ex)
2597 {
2598 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2599 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2600 }
2601
2602 static void
bfd_elf32_swap_compact_rel_out(bfd * abfd,const Elf32_compact_rel * in,Elf32_External_compact_rel * ex)2603 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2604 Elf32_External_compact_rel *ex)
2605 {
2606 H_PUT_32 (abfd, in->id1, ex->id1);
2607 H_PUT_32 (abfd, in->num, ex->num);
2608 H_PUT_32 (abfd, in->id2, ex->id2);
2609 H_PUT_32 (abfd, in->offset, ex->offset);
2610 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2611 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2612 }
2613
2614 static void
bfd_elf32_swap_crinfo_out(bfd * abfd,const Elf32_crinfo * in,Elf32_External_crinfo * ex)2615 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2616 Elf32_External_crinfo *ex)
2617 {
2618 unsigned long l;
2619
2620 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2621 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2622 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2623 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2624 H_PUT_32 (abfd, l, ex->info);
2625 H_PUT_32 (abfd, in->konst, ex->konst);
2626 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2627 }
2628
2629 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2630 routines swap this structure in and out. They are used outside of
2631 BFD, so they are globally visible. */
2632
2633 void
bfd_mips_elf32_swap_reginfo_in(bfd * abfd,const Elf32_External_RegInfo * ex,Elf32_RegInfo * in)2634 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2635 Elf32_RegInfo *in)
2636 {
2637 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2638 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2639 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2640 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2641 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2642 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2643 }
2644
2645 void
bfd_mips_elf32_swap_reginfo_out(bfd * abfd,const Elf32_RegInfo * in,Elf32_External_RegInfo * ex)2646 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2647 Elf32_External_RegInfo *ex)
2648 {
2649 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2650 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2651 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2652 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2653 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2654 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2655 }
2656
2657 /* In the 64 bit ABI, the .MIPS.options section holds register
2658 information in an Elf64_Reginfo structure. These routines swap
2659 them in and out. They are globally visible because they are used
2660 outside of BFD. These routines are here so that gas can call them
2661 without worrying about whether the 64 bit ABI has been included. */
2662
2663 void
bfd_mips_elf64_swap_reginfo_in(bfd * abfd,const Elf64_External_RegInfo * ex,Elf64_Internal_RegInfo * in)2664 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2665 Elf64_Internal_RegInfo *in)
2666 {
2667 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2668 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2669 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2670 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2671 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2672 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2673 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2674 }
2675
2676 void
bfd_mips_elf64_swap_reginfo_out(bfd * abfd,const Elf64_Internal_RegInfo * in,Elf64_External_RegInfo * ex)2677 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2678 Elf64_External_RegInfo *ex)
2679 {
2680 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2681 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2682 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2683 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2684 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2685 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2686 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2687 }
2688
2689 /* Swap in an options header. */
2690
2691 void
bfd_mips_elf_swap_options_in(bfd * abfd,const Elf_External_Options * ex,Elf_Internal_Options * in)2692 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2693 Elf_Internal_Options *in)
2694 {
2695 in->kind = H_GET_8 (abfd, ex->kind);
2696 in->size = H_GET_8 (abfd, ex->size);
2697 in->section = H_GET_16 (abfd, ex->section);
2698 in->info = H_GET_32 (abfd, ex->info);
2699 }
2700
2701 /* Swap out an options header. */
2702
2703 void
bfd_mips_elf_swap_options_out(bfd * abfd,const Elf_Internal_Options * in,Elf_External_Options * ex)2704 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2705 Elf_External_Options *ex)
2706 {
2707 H_PUT_8 (abfd, in->kind, ex->kind);
2708 H_PUT_8 (abfd, in->size, ex->size);
2709 H_PUT_16 (abfd, in->section, ex->section);
2710 H_PUT_32 (abfd, in->info, ex->info);
2711 }
2712
2713 /* Swap in an abiflags structure. */
2714
2715 void
bfd_mips_elf_swap_abiflags_v0_in(bfd * abfd,const Elf_External_ABIFlags_v0 * ex,Elf_Internal_ABIFlags_v0 * in)2716 bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2717 const Elf_External_ABIFlags_v0 *ex,
2718 Elf_Internal_ABIFlags_v0 *in)
2719 {
2720 in->version = H_GET_16 (abfd, ex->version);
2721 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2722 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2723 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2724 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2725 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2726 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2727 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2728 in->ases = H_GET_32 (abfd, ex->ases);
2729 in->flags1 = H_GET_32 (abfd, ex->flags1);
2730 in->flags2 = H_GET_32 (abfd, ex->flags2);
2731 }
2732
2733 /* Swap out an abiflags structure. */
2734
2735 void
bfd_mips_elf_swap_abiflags_v0_out(bfd * abfd,const Elf_Internal_ABIFlags_v0 * in,Elf_External_ABIFlags_v0 * ex)2736 bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2737 const Elf_Internal_ABIFlags_v0 *in,
2738 Elf_External_ABIFlags_v0 *ex)
2739 {
2740 H_PUT_16 (abfd, in->version, ex->version);
2741 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2742 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2743 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2744 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2745 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2746 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2747 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2748 H_PUT_32 (abfd, in->ases, ex->ases);
2749 H_PUT_32 (abfd, in->flags1, ex->flags1);
2750 H_PUT_32 (abfd, in->flags2, ex->flags2);
2751 }
2752
2753 /* This function is called via qsort() to sort the dynamic relocation
2754 entries by increasing r_symndx value. */
2755
2756 static int
sort_dynamic_relocs(const void * arg1,const void * arg2)2757 sort_dynamic_relocs (const void *arg1, const void *arg2)
2758 {
2759 Elf_Internal_Rela int_reloc1;
2760 Elf_Internal_Rela int_reloc2;
2761 int diff;
2762
2763 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2764 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2765
2766 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2767 if (diff != 0)
2768 return diff;
2769
2770 if (int_reloc1.r_offset < int_reloc2.r_offset)
2771 return -1;
2772 if (int_reloc1.r_offset > int_reloc2.r_offset)
2773 return 1;
2774 return 0;
2775 }
2776
2777 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2778
2779 static int
sort_dynamic_relocs_64(const void * arg1 ATTRIBUTE_UNUSED,const void * arg2 ATTRIBUTE_UNUSED)2780 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2781 const void *arg2 ATTRIBUTE_UNUSED)
2782 {
2783 #ifdef BFD64
2784 Elf_Internal_Rela int_reloc1[3];
2785 Elf_Internal_Rela int_reloc2[3];
2786
2787 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2788 (reldyn_sorting_bfd, arg1, int_reloc1);
2789 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2790 (reldyn_sorting_bfd, arg2, int_reloc2);
2791
2792 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2793 return -1;
2794 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2795 return 1;
2796
2797 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2798 return -1;
2799 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2800 return 1;
2801 return 0;
2802 #else
2803 abort ();
2804 #endif
2805 }
2806
2807
2808 /* This routine is used to write out ECOFF debugging external symbol
2809 information. It is called via mips_elf_link_hash_traverse. The
2810 ECOFF external symbol information must match the ELF external
2811 symbol information. Unfortunately, at this point we don't know
2812 whether a symbol is required by reloc information, so the two
2813 tables may wind up being different. We must sort out the external
2814 symbol information before we can set the final size of the .mdebug
2815 section, and we must set the size of the .mdebug section before we
2816 can relocate any sections, and we can't know which symbols are
2817 required by relocation until we relocate the sections.
2818 Fortunately, it is relatively unlikely that any symbol will be
2819 stripped but required by a reloc. In particular, it can not happen
2820 when generating a final executable. */
2821
2822 static bfd_boolean
mips_elf_output_extsym(struct mips_elf_link_hash_entry * h,void * data)2823 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2824 {
2825 struct extsym_info *einfo = data;
2826 bfd_boolean strip;
2827 asection *sec, *output_section;
2828
2829 if (h->root.indx == -2)
2830 strip = FALSE;
2831 else if ((h->root.def_dynamic
2832 || h->root.ref_dynamic
2833 || h->root.type == bfd_link_hash_new)
2834 && !h->root.def_regular
2835 && !h->root.ref_regular)
2836 strip = TRUE;
2837 else if (einfo->info->strip == strip_all
2838 || (einfo->info->strip == strip_some
2839 && bfd_hash_lookup (einfo->info->keep_hash,
2840 h->root.root.root.string,
2841 FALSE, FALSE) == NULL))
2842 strip = TRUE;
2843 else
2844 strip = FALSE;
2845
2846 if (strip)
2847 return TRUE;
2848
2849 if (h->esym.ifd == -2)
2850 {
2851 h->esym.jmptbl = 0;
2852 h->esym.cobol_main = 0;
2853 h->esym.weakext = 0;
2854 h->esym.reserved = 0;
2855 h->esym.ifd = ifdNil;
2856 h->esym.asym.value = 0;
2857 h->esym.asym.st = stGlobal;
2858
2859 if (h->root.root.type == bfd_link_hash_undefined
2860 || h->root.root.type == bfd_link_hash_undefweak)
2861 {
2862 const char *name;
2863
2864 /* Use undefined class. Also, set class and type for some
2865 special symbols. */
2866 name = h->root.root.root.string;
2867 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2868 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2869 {
2870 h->esym.asym.sc = scData;
2871 h->esym.asym.st = stLabel;
2872 h->esym.asym.value = 0;
2873 }
2874 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2875 {
2876 h->esym.asym.sc = scAbs;
2877 h->esym.asym.st = stLabel;
2878 h->esym.asym.value =
2879 mips_elf_hash_table (einfo->info)->procedure_count;
2880 }
2881 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2882 {
2883 h->esym.asym.sc = scAbs;
2884 h->esym.asym.st = stLabel;
2885 h->esym.asym.value = elf_gp (einfo->abfd);
2886 }
2887 else
2888 h->esym.asym.sc = scUndefined;
2889 }
2890 else if (h->root.root.type != bfd_link_hash_defined
2891 && h->root.root.type != bfd_link_hash_defweak)
2892 h->esym.asym.sc = scAbs;
2893 else
2894 {
2895 const char *name;
2896
2897 sec = h->root.root.u.def.section;
2898 output_section = sec->output_section;
2899
2900 /* When making a shared library and symbol h is the one from
2901 the another shared library, OUTPUT_SECTION may be null. */
2902 if (output_section == NULL)
2903 h->esym.asym.sc = scUndefined;
2904 else
2905 {
2906 name = bfd_section_name (output_section->owner, output_section);
2907
2908 if (strcmp (name, ".text") == 0)
2909 h->esym.asym.sc = scText;
2910 else if (strcmp (name, ".data") == 0)
2911 h->esym.asym.sc = scData;
2912 else if (strcmp (name, ".sdata") == 0)
2913 h->esym.asym.sc = scSData;
2914 else if (strcmp (name, ".rodata") == 0
2915 || strcmp (name, ".rdata") == 0)
2916 h->esym.asym.sc = scRData;
2917 else if (strcmp (name, ".bss") == 0)
2918 h->esym.asym.sc = scBss;
2919 else if (strcmp (name, ".sbss") == 0)
2920 h->esym.asym.sc = scSBss;
2921 else if (strcmp (name, ".init") == 0)
2922 h->esym.asym.sc = scInit;
2923 else if (strcmp (name, ".fini") == 0)
2924 h->esym.asym.sc = scFini;
2925 else
2926 h->esym.asym.sc = scAbs;
2927 }
2928 }
2929
2930 h->esym.asym.reserved = 0;
2931 h->esym.asym.index = indexNil;
2932 }
2933
2934 if (h->root.root.type == bfd_link_hash_common)
2935 h->esym.asym.value = h->root.root.u.c.size;
2936 else if (h->root.root.type == bfd_link_hash_defined
2937 || h->root.root.type == bfd_link_hash_defweak)
2938 {
2939 if (h->esym.asym.sc == scCommon)
2940 h->esym.asym.sc = scBss;
2941 else if (h->esym.asym.sc == scSCommon)
2942 h->esym.asym.sc = scSBss;
2943
2944 sec = h->root.root.u.def.section;
2945 output_section = sec->output_section;
2946 if (output_section != NULL)
2947 h->esym.asym.value = (h->root.root.u.def.value
2948 + sec->output_offset
2949 + output_section->vma);
2950 else
2951 h->esym.asym.value = 0;
2952 }
2953 else
2954 {
2955 struct mips_elf_link_hash_entry *hd = h;
2956
2957 while (hd->root.root.type == bfd_link_hash_indirect)
2958 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2959
2960 if (hd->needs_lazy_stub)
2961 {
2962 BFD_ASSERT (hd->root.plt.plist != NULL);
2963 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
2964 /* Set type and value for a symbol with a function stub. */
2965 h->esym.asym.st = stProc;
2966 sec = hd->root.root.u.def.section;
2967 if (sec == NULL)
2968 h->esym.asym.value = 0;
2969 else
2970 {
2971 output_section = sec->output_section;
2972 if (output_section != NULL)
2973 h->esym.asym.value = (hd->root.plt.plist->stub_offset
2974 + sec->output_offset
2975 + output_section->vma);
2976 else
2977 h->esym.asym.value = 0;
2978 }
2979 }
2980 }
2981
2982 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2983 h->root.root.root.string,
2984 &h->esym))
2985 {
2986 einfo->failed = TRUE;
2987 return FALSE;
2988 }
2989
2990 return TRUE;
2991 }
2992
2993 /* A comparison routine used to sort .gptab entries. */
2994
2995 static int
gptab_compare(const void * p1,const void * p2)2996 gptab_compare (const void *p1, const void *p2)
2997 {
2998 const Elf32_gptab *a1 = p1;
2999 const Elf32_gptab *a2 = p2;
3000
3001 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3002 }
3003
3004 /* Functions to manage the got entry hash table. */
3005
3006 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3007 hash number. */
3008
3009 static INLINE hashval_t
mips_elf_hash_bfd_vma(bfd_vma addr)3010 mips_elf_hash_bfd_vma (bfd_vma addr)
3011 {
3012 #ifdef BFD64
3013 return addr + (addr >> 32);
3014 #else
3015 return addr;
3016 #endif
3017 }
3018
3019 static hashval_t
mips_elf_got_entry_hash(const void * entry_)3020 mips_elf_got_entry_hash (const void *entry_)
3021 {
3022 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3023
3024 return (entry->symndx
3025 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3026 + (entry->tls_type == GOT_TLS_LDM ? 0
3027 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3028 : entry->symndx >= 0 ? (entry->abfd->id
3029 + mips_elf_hash_bfd_vma (entry->d.addend))
3030 : entry->d.h->root.root.root.hash));
3031 }
3032
3033 static int
mips_elf_got_entry_eq(const void * entry1,const void * entry2)3034 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
3035 {
3036 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3037 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3038
3039 return (e1->symndx == e2->symndx
3040 && e1->tls_type == e2->tls_type
3041 && (e1->tls_type == GOT_TLS_LDM ? TRUE
3042 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3043 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3044 && e1->d.addend == e2->d.addend)
3045 : e2->abfd && e1->d.h == e2->d.h));
3046 }
3047
3048 static hashval_t
mips_got_page_ref_hash(const void * ref_)3049 mips_got_page_ref_hash (const void *ref_)
3050 {
3051 const struct mips_got_page_ref *ref;
3052
3053 ref = (const struct mips_got_page_ref *) ref_;
3054 return ((ref->symndx >= 0
3055 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3056 : ref->u.h->root.root.root.hash)
3057 + mips_elf_hash_bfd_vma (ref->addend));
3058 }
3059
3060 static int
mips_got_page_ref_eq(const void * ref1_,const void * ref2_)3061 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3062 {
3063 const struct mips_got_page_ref *ref1, *ref2;
3064
3065 ref1 = (const struct mips_got_page_ref *) ref1_;
3066 ref2 = (const struct mips_got_page_ref *) ref2_;
3067 return (ref1->symndx == ref2->symndx
3068 && (ref1->symndx < 0
3069 ? ref1->u.h == ref2->u.h
3070 : ref1->u.abfd == ref2->u.abfd)
3071 && ref1->addend == ref2->addend);
3072 }
3073
3074 static hashval_t
mips_got_page_entry_hash(const void * entry_)3075 mips_got_page_entry_hash (const void *entry_)
3076 {
3077 const struct mips_got_page_entry *entry;
3078
3079 entry = (const struct mips_got_page_entry *) entry_;
3080 return entry->sec->id;
3081 }
3082
3083 static int
mips_got_page_entry_eq(const void * entry1_,const void * entry2_)3084 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3085 {
3086 const struct mips_got_page_entry *entry1, *entry2;
3087
3088 entry1 = (const struct mips_got_page_entry *) entry1_;
3089 entry2 = (const struct mips_got_page_entry *) entry2_;
3090 return entry1->sec == entry2->sec;
3091 }
3092
3093 /* Create and return a new mips_got_info structure. */
3094
3095 static struct mips_got_info *
mips_elf_create_got_info(bfd * abfd)3096 mips_elf_create_got_info (bfd *abfd)
3097 {
3098 struct mips_got_info *g;
3099
3100 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3101 if (g == NULL)
3102 return NULL;
3103
3104 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3105 mips_elf_got_entry_eq, NULL);
3106 if (g->got_entries == NULL)
3107 return NULL;
3108
3109 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3110 mips_got_page_ref_eq, NULL);
3111 if (g->got_page_refs == NULL)
3112 return NULL;
3113
3114 return g;
3115 }
3116
3117 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3118 CREATE_P and if ABFD doesn't already have a GOT. */
3119
3120 static struct mips_got_info *
mips_elf_bfd_got(bfd * abfd,bfd_boolean create_p)3121 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3122 {
3123 struct mips_elf_obj_tdata *tdata;
3124
3125 if (!is_mips_elf (abfd))
3126 return NULL;
3127
3128 tdata = mips_elf_tdata (abfd);
3129 if (!tdata->got && create_p)
3130 tdata->got = mips_elf_create_got_info (abfd);
3131 return tdata->got;
3132 }
3133
3134 /* Record that ABFD should use output GOT G. */
3135
3136 static void
mips_elf_replace_bfd_got(bfd * abfd,struct mips_got_info * g)3137 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3138 {
3139 struct mips_elf_obj_tdata *tdata;
3140
3141 BFD_ASSERT (is_mips_elf (abfd));
3142 tdata = mips_elf_tdata (abfd);
3143 if (tdata->got)
3144 {
3145 /* The GOT structure itself and the hash table entries are
3146 allocated to a bfd, but the hash tables aren't. */
3147 htab_delete (tdata->got->got_entries);
3148 htab_delete (tdata->got->got_page_refs);
3149 if (tdata->got->got_page_entries)
3150 htab_delete (tdata->got->got_page_entries);
3151 }
3152 tdata->got = g;
3153 }
3154
3155 /* Return the dynamic relocation section. If it doesn't exist, try to
3156 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3157 if creation fails. */
3158
3159 static asection *
mips_elf_rel_dyn_section(struct bfd_link_info * info,bfd_boolean create_p)3160 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
3161 {
3162 const char *dname;
3163 asection *sreloc;
3164 bfd *dynobj;
3165
3166 dname = MIPS_ELF_REL_DYN_NAME (info);
3167 dynobj = elf_hash_table (info)->dynobj;
3168 sreloc = bfd_get_linker_section (dynobj, dname);
3169 if (sreloc == NULL && create_p)
3170 {
3171 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3172 (SEC_ALLOC
3173 | SEC_LOAD
3174 | SEC_HAS_CONTENTS
3175 | SEC_IN_MEMORY
3176 | SEC_LINKER_CREATED
3177 | SEC_READONLY));
3178 if (sreloc == NULL
3179 || ! bfd_set_section_alignment (dynobj, sreloc,
3180 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3181 return NULL;
3182 }
3183 return sreloc;
3184 }
3185
3186 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3187
3188 static int
mips_elf_reloc_tls_type(unsigned int r_type)3189 mips_elf_reloc_tls_type (unsigned int r_type)
3190 {
3191 if (tls_gd_reloc_p (r_type))
3192 return GOT_TLS_GD;
3193
3194 if (tls_ldm_reloc_p (r_type))
3195 return GOT_TLS_LDM;
3196
3197 if (tls_gottprel_reloc_p (r_type))
3198 return GOT_TLS_IE;
3199
3200 return GOT_TLS_NONE;
3201 }
3202
3203 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3204
3205 static int
mips_tls_got_entries(unsigned int type)3206 mips_tls_got_entries (unsigned int type)
3207 {
3208 switch (type)
3209 {
3210 case GOT_TLS_GD:
3211 case GOT_TLS_LDM:
3212 return 2;
3213
3214 case GOT_TLS_IE:
3215 return 1;
3216
3217 case GOT_TLS_NONE:
3218 return 0;
3219 }
3220 abort ();
3221 }
3222
3223 /* Count the number of relocations needed for a TLS GOT entry, with
3224 access types from TLS_TYPE, and symbol H (or a local symbol if H
3225 is NULL). */
3226
3227 static int
mips_tls_got_relocs(struct bfd_link_info * info,unsigned char tls_type,struct elf_link_hash_entry * h)3228 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3229 struct elf_link_hash_entry *h)
3230 {
3231 int indx = 0;
3232 bfd_boolean need_relocs = FALSE;
3233 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3234
3235 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3236 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
3237 indx = h->dynindx;
3238
3239 if ((info->shared || indx != 0)
3240 && (h == NULL
3241 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3242 || h->root.type != bfd_link_hash_undefweak))
3243 need_relocs = TRUE;
3244
3245 if (!need_relocs)
3246 return 0;
3247
3248 switch (tls_type)
3249 {
3250 case GOT_TLS_GD:
3251 return indx != 0 ? 2 : 1;
3252
3253 case GOT_TLS_IE:
3254 return 1;
3255
3256 case GOT_TLS_LDM:
3257 return info->shared ? 1 : 0;
3258
3259 default:
3260 return 0;
3261 }
3262 }
3263
3264 /* Add the number of GOT entries and TLS relocations required by ENTRY
3265 to G. */
3266
3267 static void
mips_elf_count_got_entry(struct bfd_link_info * info,struct mips_got_info * g,struct mips_got_entry * entry)3268 mips_elf_count_got_entry (struct bfd_link_info *info,
3269 struct mips_got_info *g,
3270 struct mips_got_entry *entry)
3271 {
3272 if (entry->tls_type)
3273 {
3274 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3275 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3276 entry->symndx < 0
3277 ? &entry->d.h->root : NULL);
3278 }
3279 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3280 g->local_gotno += 1;
3281 else
3282 g->global_gotno += 1;
3283 }
3284
3285 /* Output a simple dynamic relocation into SRELOC. */
3286
3287 static void
mips_elf_output_dynamic_relocation(bfd * output_bfd,asection * sreloc,unsigned long reloc_index,unsigned long indx,int r_type,bfd_vma offset)3288 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3289 asection *sreloc,
3290 unsigned long reloc_index,
3291 unsigned long indx,
3292 int r_type,
3293 bfd_vma offset)
3294 {
3295 Elf_Internal_Rela rel[3];
3296
3297 memset (rel, 0, sizeof (rel));
3298
3299 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3300 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3301
3302 if (ABI_64_P (output_bfd))
3303 {
3304 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3305 (output_bfd, &rel[0],
3306 (sreloc->contents
3307 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3308 }
3309 else
3310 bfd_elf32_swap_reloc_out
3311 (output_bfd, &rel[0],
3312 (sreloc->contents
3313 + reloc_index * sizeof (Elf32_External_Rel)));
3314 }
3315
3316 /* Initialize a set of TLS GOT entries for one symbol. */
3317
3318 static void
mips_elf_initialize_tls_slots(bfd * abfd,struct bfd_link_info * info,struct mips_got_entry * entry,struct mips_elf_link_hash_entry * h,bfd_vma value)3319 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3320 struct mips_got_entry *entry,
3321 struct mips_elf_link_hash_entry *h,
3322 bfd_vma value)
3323 {
3324 struct mips_elf_link_hash_table *htab;
3325 int indx;
3326 asection *sreloc, *sgot;
3327 bfd_vma got_offset, got_offset2;
3328 bfd_boolean need_relocs = FALSE;
3329
3330 htab = mips_elf_hash_table (info);
3331 if (htab == NULL)
3332 return;
3333
3334 sgot = htab->sgot;
3335
3336 indx = 0;
3337 if (h != NULL)
3338 {
3339 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3340
3341 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3342 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3343 indx = h->root.dynindx;
3344 }
3345
3346 if (entry->tls_initialized)
3347 return;
3348
3349 if ((info->shared || indx != 0)
3350 && (h == NULL
3351 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3352 || h->root.type != bfd_link_hash_undefweak))
3353 need_relocs = TRUE;
3354
3355 /* MINUS_ONE means the symbol is not defined in this object. It may not
3356 be defined at all; assume that the value doesn't matter in that
3357 case. Otherwise complain if we would use the value. */
3358 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3359 || h->root.root.type == bfd_link_hash_undefweak);
3360
3361 /* Emit necessary relocations. */
3362 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3363 got_offset = entry->gotidx;
3364
3365 switch (entry->tls_type)
3366 {
3367 case GOT_TLS_GD:
3368 /* General Dynamic. */
3369 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3370
3371 if (need_relocs)
3372 {
3373 mips_elf_output_dynamic_relocation
3374 (abfd, sreloc, sreloc->reloc_count++, indx,
3375 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3376 sgot->output_offset + sgot->output_section->vma + got_offset);
3377
3378 if (indx)
3379 mips_elf_output_dynamic_relocation
3380 (abfd, sreloc, sreloc->reloc_count++, indx,
3381 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3382 sgot->output_offset + sgot->output_section->vma + got_offset2);
3383 else
3384 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3385 sgot->contents + got_offset2);
3386 }
3387 else
3388 {
3389 MIPS_ELF_PUT_WORD (abfd, 1,
3390 sgot->contents + got_offset);
3391 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3392 sgot->contents + got_offset2);
3393 }
3394 break;
3395
3396 case GOT_TLS_IE:
3397 /* Initial Exec model. */
3398 if (need_relocs)
3399 {
3400 if (indx == 0)
3401 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3402 sgot->contents + got_offset);
3403 else
3404 MIPS_ELF_PUT_WORD (abfd, 0,
3405 sgot->contents + got_offset);
3406
3407 mips_elf_output_dynamic_relocation
3408 (abfd, sreloc, sreloc->reloc_count++, indx,
3409 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3410 sgot->output_offset + sgot->output_section->vma + got_offset);
3411 }
3412 else
3413 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3414 sgot->contents + got_offset);
3415 break;
3416
3417 case GOT_TLS_LDM:
3418 /* The initial offset is zero, and the LD offsets will include the
3419 bias by DTP_OFFSET. */
3420 MIPS_ELF_PUT_WORD (abfd, 0,
3421 sgot->contents + got_offset
3422 + MIPS_ELF_GOT_SIZE (abfd));
3423
3424 if (!info->shared)
3425 MIPS_ELF_PUT_WORD (abfd, 1,
3426 sgot->contents + got_offset);
3427 else
3428 mips_elf_output_dynamic_relocation
3429 (abfd, sreloc, sreloc->reloc_count++, indx,
3430 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3431 sgot->output_offset + sgot->output_section->vma + got_offset);
3432 break;
3433
3434 default:
3435 abort ();
3436 }
3437
3438 entry->tls_initialized = TRUE;
3439 }
3440
3441 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3442 for global symbol H. .got.plt comes before the GOT, so the offset
3443 will be negative. */
3444
3445 static bfd_vma
mips_elf_gotplt_index(struct bfd_link_info * info,struct elf_link_hash_entry * h)3446 mips_elf_gotplt_index (struct bfd_link_info *info,
3447 struct elf_link_hash_entry *h)
3448 {
3449 bfd_vma got_address, got_value;
3450 struct mips_elf_link_hash_table *htab;
3451
3452 htab = mips_elf_hash_table (info);
3453 BFD_ASSERT (htab != NULL);
3454
3455 BFD_ASSERT (h->plt.plist != NULL);
3456 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3457
3458 /* Calculate the address of the associated .got.plt entry. */
3459 got_address = (htab->sgotplt->output_section->vma
3460 + htab->sgotplt->output_offset
3461 + (h->plt.plist->gotplt_index
3462 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3463
3464 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3465 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3466 + htab->root.hgot->root.u.def.section->output_offset
3467 + htab->root.hgot->root.u.def.value);
3468
3469 return got_address - got_value;
3470 }
3471
3472 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3473 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3474 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3475 offset can be found. */
3476
3477 static bfd_vma
mips_elf_local_got_index(bfd * abfd,bfd * ibfd,struct bfd_link_info * info,bfd_vma value,unsigned long r_symndx,struct mips_elf_link_hash_entry * h,int r_type)3478 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3479 bfd_vma value, unsigned long r_symndx,
3480 struct mips_elf_link_hash_entry *h, int r_type)
3481 {
3482 struct mips_elf_link_hash_table *htab;
3483 struct mips_got_entry *entry;
3484
3485 htab = mips_elf_hash_table (info);
3486 BFD_ASSERT (htab != NULL);
3487
3488 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3489 r_symndx, h, r_type);
3490 if (!entry)
3491 return MINUS_ONE;
3492
3493 if (entry->tls_type)
3494 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3495 return entry->gotidx;
3496 }
3497
3498 /* Return the GOT index of global symbol H in the primary GOT. */
3499
3500 static bfd_vma
mips_elf_primary_global_got_index(bfd * obfd,struct bfd_link_info * info,struct elf_link_hash_entry * h)3501 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3502 struct elf_link_hash_entry *h)
3503 {
3504 struct mips_elf_link_hash_table *htab;
3505 long global_got_dynindx;
3506 struct mips_got_info *g;
3507 bfd_vma got_index;
3508
3509 htab = mips_elf_hash_table (info);
3510 BFD_ASSERT (htab != NULL);
3511
3512 global_got_dynindx = 0;
3513 if (htab->global_gotsym != NULL)
3514 global_got_dynindx = htab->global_gotsym->dynindx;
3515
3516 /* Once we determine the global GOT entry with the lowest dynamic
3517 symbol table index, we must put all dynamic symbols with greater
3518 indices into the primary GOT. That makes it easy to calculate the
3519 GOT offset. */
3520 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3521 g = mips_elf_bfd_got (obfd, FALSE);
3522 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3523 * MIPS_ELF_GOT_SIZE (obfd));
3524 BFD_ASSERT (got_index < htab->sgot->size);
3525
3526 return got_index;
3527 }
3528
3529 /* Return the GOT index for the global symbol indicated by H, which is
3530 referenced by a relocation of type R_TYPE in IBFD. */
3531
3532 static bfd_vma
mips_elf_global_got_index(bfd * obfd,struct bfd_link_info * info,bfd * ibfd,struct elf_link_hash_entry * h,int r_type)3533 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3534 struct elf_link_hash_entry *h, int r_type)
3535 {
3536 struct mips_elf_link_hash_table *htab;
3537 struct mips_got_info *g;
3538 struct mips_got_entry lookup, *entry;
3539 bfd_vma gotidx;
3540
3541 htab = mips_elf_hash_table (info);
3542 BFD_ASSERT (htab != NULL);
3543
3544 g = mips_elf_bfd_got (ibfd, FALSE);
3545 BFD_ASSERT (g);
3546
3547 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3548 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3549 return mips_elf_primary_global_got_index (obfd, info, h);
3550
3551 lookup.abfd = ibfd;
3552 lookup.symndx = -1;
3553 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3554 entry = htab_find (g->got_entries, &lookup);
3555 BFD_ASSERT (entry);
3556
3557 gotidx = entry->gotidx;
3558 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3559
3560 if (lookup.tls_type)
3561 {
3562 bfd_vma value = MINUS_ONE;
3563
3564 if ((h->root.type == bfd_link_hash_defined
3565 || h->root.type == bfd_link_hash_defweak)
3566 && h->root.u.def.section->output_section)
3567 value = (h->root.u.def.value
3568 + h->root.u.def.section->output_offset
3569 + h->root.u.def.section->output_section->vma);
3570
3571 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3572 }
3573 return gotidx;
3574 }
3575
3576 /* Find a GOT page entry that points to within 32KB of VALUE. These
3577 entries are supposed to be placed at small offsets in the GOT, i.e.,
3578 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3579 entry could be created. If OFFSETP is nonnull, use it to return the
3580 offset of the GOT entry from VALUE. */
3581
3582 static bfd_vma
mips_elf_got_page(bfd * abfd,bfd * ibfd,struct bfd_link_info * info,bfd_vma value,bfd_vma * offsetp)3583 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3584 bfd_vma value, bfd_vma *offsetp)
3585 {
3586 bfd_vma page, got_index;
3587 struct mips_got_entry *entry;
3588
3589 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3590 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3591 NULL, R_MIPS_GOT_PAGE);
3592
3593 if (!entry)
3594 return MINUS_ONE;
3595
3596 got_index = entry->gotidx;
3597
3598 if (offsetp)
3599 *offsetp = value - entry->d.address;
3600
3601 return got_index;
3602 }
3603
3604 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3605 EXTERNAL is true if the relocation was originally against a global
3606 symbol that binds locally. */
3607
3608 static bfd_vma
mips_elf_got16_entry(bfd * abfd,bfd * ibfd,struct bfd_link_info * info,bfd_vma value,bfd_boolean external)3609 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3610 bfd_vma value, bfd_boolean external)
3611 {
3612 struct mips_got_entry *entry;
3613
3614 /* GOT16 relocations against local symbols are followed by a LO16
3615 relocation; those against global symbols are not. Thus if the
3616 symbol was originally local, the GOT16 relocation should load the
3617 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3618 if (! external)
3619 value = mips_elf_high (value) << 16;
3620
3621 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3622 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3623 same in all cases. */
3624 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3625 NULL, R_MIPS_GOT16);
3626 if (entry)
3627 return entry->gotidx;
3628 else
3629 return MINUS_ONE;
3630 }
3631
3632 /* Returns the offset for the entry at the INDEXth position
3633 in the GOT. */
3634
3635 static bfd_vma
mips_elf_got_offset_from_index(struct bfd_link_info * info,bfd * output_bfd,bfd * input_bfd,bfd_vma got_index)3636 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3637 bfd *input_bfd, bfd_vma got_index)
3638 {
3639 struct mips_elf_link_hash_table *htab;
3640 asection *sgot;
3641 bfd_vma gp;
3642
3643 htab = mips_elf_hash_table (info);
3644 BFD_ASSERT (htab != NULL);
3645
3646 sgot = htab->sgot;
3647 gp = _bfd_get_gp_value (output_bfd)
3648 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3649
3650 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3651 }
3652
3653 /* Create and return a local GOT entry for VALUE, which was calculated
3654 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3655 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3656 instead. */
3657
3658 static struct mips_got_entry *
mips_elf_create_local_got_entry(bfd * abfd,struct bfd_link_info * info,bfd * ibfd,bfd_vma value,unsigned long r_symndx,struct mips_elf_link_hash_entry * h,int r_type)3659 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3660 bfd *ibfd, bfd_vma value,
3661 unsigned long r_symndx,
3662 struct mips_elf_link_hash_entry *h,
3663 int r_type)
3664 {
3665 struct mips_got_entry lookup, *entry;
3666 void **loc;
3667 struct mips_got_info *g;
3668 struct mips_elf_link_hash_table *htab;
3669 bfd_vma gotidx;
3670
3671 htab = mips_elf_hash_table (info);
3672 BFD_ASSERT (htab != NULL);
3673
3674 g = mips_elf_bfd_got (ibfd, FALSE);
3675 if (g == NULL)
3676 {
3677 g = mips_elf_bfd_got (abfd, FALSE);
3678 BFD_ASSERT (g != NULL);
3679 }
3680
3681 /* This function shouldn't be called for symbols that live in the global
3682 area of the GOT. */
3683 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3684
3685 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3686 if (lookup.tls_type)
3687 {
3688 lookup.abfd = ibfd;
3689 if (tls_ldm_reloc_p (r_type))
3690 {
3691 lookup.symndx = 0;
3692 lookup.d.addend = 0;
3693 }
3694 else if (h == NULL)
3695 {
3696 lookup.symndx = r_symndx;
3697 lookup.d.addend = 0;
3698 }
3699 else
3700 {
3701 lookup.symndx = -1;
3702 lookup.d.h = h;
3703 }
3704
3705 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3706 BFD_ASSERT (entry);
3707
3708 gotidx = entry->gotidx;
3709 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3710
3711 return entry;
3712 }
3713
3714 lookup.abfd = NULL;
3715 lookup.symndx = -1;
3716 lookup.d.address = value;
3717 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3718 if (!loc)
3719 return NULL;
3720
3721 entry = (struct mips_got_entry *) *loc;
3722 if (entry)
3723 return entry;
3724
3725 if (g->assigned_low_gotno > g->assigned_high_gotno)
3726 {
3727 /* We didn't allocate enough space in the GOT. */
3728 (*_bfd_error_handler)
3729 (_("not enough GOT space for local GOT entries"));
3730 bfd_set_error (bfd_error_bad_value);
3731 return NULL;
3732 }
3733
3734 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3735 if (!entry)
3736 return NULL;
3737
3738 if (got16_reloc_p (r_type)
3739 || call16_reloc_p (r_type)
3740 || got_page_reloc_p (r_type)
3741 || got_disp_reloc_p (r_type))
3742 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3743 else
3744 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3745
3746 *entry = lookup;
3747 *loc = entry;
3748
3749 MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
3750
3751 /* These GOT entries need a dynamic relocation on VxWorks. */
3752 if (htab->is_vxworks)
3753 {
3754 Elf_Internal_Rela outrel;
3755 asection *s;
3756 bfd_byte *rloc;
3757 bfd_vma got_address;
3758
3759 s = mips_elf_rel_dyn_section (info, FALSE);
3760 got_address = (htab->sgot->output_section->vma
3761 + htab->sgot->output_offset
3762 + entry->gotidx);
3763
3764 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3765 outrel.r_offset = got_address;
3766 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3767 outrel.r_addend = value;
3768 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3769 }
3770
3771 return entry;
3772 }
3773
3774 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3775 The number might be exact or a worst-case estimate, depending on how
3776 much information is available to elf_backend_omit_section_dynsym at
3777 the current linking stage. */
3778
3779 static bfd_size_type
count_section_dynsyms(bfd * output_bfd,struct bfd_link_info * info)3780 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3781 {
3782 bfd_size_type count;
3783
3784 count = 0;
3785 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3786 {
3787 asection *p;
3788 const struct elf_backend_data *bed;
3789
3790 bed = get_elf_backend_data (output_bfd);
3791 for (p = output_bfd->sections; p ; p = p->next)
3792 if ((p->flags & SEC_EXCLUDE) == 0
3793 && (p->flags & SEC_ALLOC) != 0
3794 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3795 ++count;
3796 }
3797 return count;
3798 }
3799
3800 /* Sort the dynamic symbol table so that symbols that need GOT entries
3801 appear towards the end. */
3802
3803 static bfd_boolean
mips_elf_sort_hash_table(bfd * abfd,struct bfd_link_info * info)3804 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3805 {
3806 struct mips_elf_link_hash_table *htab;
3807 struct mips_elf_hash_sort_data hsd;
3808 struct mips_got_info *g;
3809
3810 if (elf_hash_table (info)->dynsymcount == 0)
3811 return TRUE;
3812
3813 htab = mips_elf_hash_table (info);
3814 BFD_ASSERT (htab != NULL);
3815
3816 g = htab->got_info;
3817 if (g == NULL)
3818 return TRUE;
3819
3820 hsd.low = NULL;
3821 hsd.max_unref_got_dynindx
3822 = hsd.min_got_dynindx
3823 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3824 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3825 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3826 elf_hash_table (info)),
3827 mips_elf_sort_hash_table_f,
3828 &hsd);
3829
3830 /* There should have been enough room in the symbol table to
3831 accommodate both the GOT and non-GOT symbols. */
3832 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3833 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3834 == elf_hash_table (info)->dynsymcount);
3835 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3836 == g->global_gotno);
3837
3838 /* Now we know which dynamic symbol has the lowest dynamic symbol
3839 table index in the GOT. */
3840 htab->global_gotsym = hsd.low;
3841
3842 return TRUE;
3843 }
3844
3845 /* If H needs a GOT entry, assign it the highest available dynamic
3846 index. Otherwise, assign it the lowest available dynamic
3847 index. */
3848
3849 static bfd_boolean
mips_elf_sort_hash_table_f(struct mips_elf_link_hash_entry * h,void * data)3850 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3851 {
3852 struct mips_elf_hash_sort_data *hsd = data;
3853
3854 /* Symbols without dynamic symbol table entries aren't interesting
3855 at all. */
3856 if (h->root.dynindx == -1)
3857 return TRUE;
3858
3859 switch (h->global_got_area)
3860 {
3861 case GGA_NONE:
3862 h->root.dynindx = hsd->max_non_got_dynindx++;
3863 break;
3864
3865 case GGA_NORMAL:
3866 h->root.dynindx = --hsd->min_got_dynindx;
3867 hsd->low = (struct elf_link_hash_entry *) h;
3868 break;
3869
3870 case GGA_RELOC_ONLY:
3871 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3872 hsd->low = (struct elf_link_hash_entry *) h;
3873 h->root.dynindx = hsd->max_unref_got_dynindx++;
3874 break;
3875 }
3876
3877 return TRUE;
3878 }
3879
3880 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3881 (which is owned by the caller and shouldn't be added to the
3882 hash table directly). */
3883
3884 static bfd_boolean
mips_elf_record_got_entry(struct bfd_link_info * info,bfd * abfd,struct mips_got_entry * lookup)3885 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3886 struct mips_got_entry *lookup)
3887 {
3888 struct mips_elf_link_hash_table *htab;
3889 struct mips_got_entry *entry;
3890 struct mips_got_info *g;
3891 void **loc, **bfd_loc;
3892
3893 /* Make sure there's a slot for this entry in the master GOT. */
3894 htab = mips_elf_hash_table (info);
3895 g = htab->got_info;
3896 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3897 if (!loc)
3898 return FALSE;
3899
3900 /* Populate the entry if it isn't already. */
3901 entry = (struct mips_got_entry *) *loc;
3902 if (!entry)
3903 {
3904 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3905 if (!entry)
3906 return FALSE;
3907
3908 lookup->tls_initialized = FALSE;
3909 lookup->gotidx = -1;
3910 *entry = *lookup;
3911 *loc = entry;
3912 }
3913
3914 /* Reuse the same GOT entry for the BFD's GOT. */
3915 g = mips_elf_bfd_got (abfd, TRUE);
3916 if (!g)
3917 return FALSE;
3918
3919 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3920 if (!bfd_loc)
3921 return FALSE;
3922
3923 if (!*bfd_loc)
3924 *bfd_loc = entry;
3925 return TRUE;
3926 }
3927
3928 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3929 entry for it. FOR_CALL is true if the caller is only interested in
3930 using the GOT entry for calls. */
3931
3932 static bfd_boolean
mips_elf_record_global_got_symbol(struct elf_link_hash_entry * h,bfd * abfd,struct bfd_link_info * info,bfd_boolean for_call,int r_type)3933 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3934 bfd *abfd, struct bfd_link_info *info,
3935 bfd_boolean for_call, int r_type)
3936 {
3937 struct mips_elf_link_hash_table *htab;
3938 struct mips_elf_link_hash_entry *hmips;
3939 struct mips_got_entry entry;
3940 unsigned char tls_type;
3941
3942 htab = mips_elf_hash_table (info);
3943 BFD_ASSERT (htab != NULL);
3944
3945 hmips = (struct mips_elf_link_hash_entry *) h;
3946 if (!for_call)
3947 hmips->got_only_for_calls = FALSE;
3948
3949 /* A global symbol in the GOT must also be in the dynamic symbol
3950 table. */
3951 if (h->dynindx == -1)
3952 {
3953 switch (ELF_ST_VISIBILITY (h->other))
3954 {
3955 case STV_INTERNAL:
3956 case STV_HIDDEN:
3957 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3958 break;
3959 }
3960 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3961 return FALSE;
3962 }
3963
3964 tls_type = mips_elf_reloc_tls_type (r_type);
3965 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
3966 hmips->global_got_area = GGA_NORMAL;
3967
3968 entry.abfd = abfd;
3969 entry.symndx = -1;
3970 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3971 entry.tls_type = tls_type;
3972 return mips_elf_record_got_entry (info, abfd, &entry);
3973 }
3974
3975 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3976 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
3977
3978 static bfd_boolean
mips_elf_record_local_got_symbol(bfd * abfd,long symndx,bfd_vma addend,struct bfd_link_info * info,int r_type)3979 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3980 struct bfd_link_info *info, int r_type)
3981 {
3982 struct mips_elf_link_hash_table *htab;
3983 struct mips_got_info *g;
3984 struct mips_got_entry entry;
3985
3986 htab = mips_elf_hash_table (info);
3987 BFD_ASSERT (htab != NULL);
3988
3989 g = htab->got_info;
3990 BFD_ASSERT (g != NULL);
3991
3992 entry.abfd = abfd;
3993 entry.symndx = symndx;
3994 entry.d.addend = addend;
3995 entry.tls_type = mips_elf_reloc_tls_type (r_type);
3996 return mips_elf_record_got_entry (info, abfd, &entry);
3997 }
3998
3999 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4000 H is the symbol's hash table entry, or null if SYMNDX is local
4001 to ABFD. */
4002
4003 static bfd_boolean
mips_elf_record_got_page_ref(struct bfd_link_info * info,bfd * abfd,long symndx,struct elf_link_hash_entry * h,bfd_signed_vma addend)4004 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4005 long symndx, struct elf_link_hash_entry *h,
4006 bfd_signed_vma addend)
4007 {
4008 struct mips_elf_link_hash_table *htab;
4009 struct mips_got_info *g1, *g2;
4010 struct mips_got_page_ref lookup, *entry;
4011 void **loc, **bfd_loc;
4012
4013 htab = mips_elf_hash_table (info);
4014 BFD_ASSERT (htab != NULL);
4015
4016 g1 = htab->got_info;
4017 BFD_ASSERT (g1 != NULL);
4018
4019 if (h)
4020 {
4021 lookup.symndx = -1;
4022 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4023 }
4024 else
4025 {
4026 lookup.symndx = symndx;
4027 lookup.u.abfd = abfd;
4028 }
4029 lookup.addend = addend;
4030 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
4031 if (loc == NULL)
4032 return FALSE;
4033
4034 entry = (struct mips_got_page_ref *) *loc;
4035 if (!entry)
4036 {
4037 entry = bfd_alloc (abfd, sizeof (*entry));
4038 if (!entry)
4039 return FALSE;
4040
4041 *entry = lookup;
4042 *loc = entry;
4043 }
4044
4045 /* Add the same entry to the BFD's GOT. */
4046 g2 = mips_elf_bfd_got (abfd, TRUE);
4047 if (!g2)
4048 return FALSE;
4049
4050 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
4051 if (!bfd_loc)
4052 return FALSE;
4053
4054 if (!*bfd_loc)
4055 *bfd_loc = entry;
4056
4057 return TRUE;
4058 }
4059
4060 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4061
4062 static void
mips_elf_allocate_dynamic_relocations(bfd * abfd,struct bfd_link_info * info,unsigned int n)4063 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4064 unsigned int n)
4065 {
4066 asection *s;
4067 struct mips_elf_link_hash_table *htab;
4068
4069 htab = mips_elf_hash_table (info);
4070 BFD_ASSERT (htab != NULL);
4071
4072 s = mips_elf_rel_dyn_section (info, FALSE);
4073 BFD_ASSERT (s != NULL);
4074
4075 if (htab->is_vxworks)
4076 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4077 else
4078 {
4079 if (s->size == 0)
4080 {
4081 /* Make room for a null element. */
4082 s->size += MIPS_ELF_REL_SIZE (abfd);
4083 ++s->reloc_count;
4084 }
4085 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4086 }
4087 }
4088
4089 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4090 mips_elf_traverse_got_arg structure. Count the number of GOT
4091 entries and TLS relocs. Set DATA->value to true if we need
4092 to resolve indirect or warning symbols and then recreate the GOT. */
4093
4094 static int
mips_elf_check_recreate_got(void ** entryp,void * data)4095 mips_elf_check_recreate_got (void **entryp, void *data)
4096 {
4097 struct mips_got_entry *entry;
4098 struct mips_elf_traverse_got_arg *arg;
4099
4100 entry = (struct mips_got_entry *) *entryp;
4101 arg = (struct mips_elf_traverse_got_arg *) data;
4102 if (entry->abfd != NULL && entry->symndx == -1)
4103 {
4104 struct mips_elf_link_hash_entry *h;
4105
4106 h = entry->d.h;
4107 if (h->root.root.type == bfd_link_hash_indirect
4108 || h->root.root.type == bfd_link_hash_warning)
4109 {
4110 arg->value = TRUE;
4111 return 0;
4112 }
4113 }
4114 mips_elf_count_got_entry (arg->info, arg->g, entry);
4115 return 1;
4116 }
4117
4118 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4119 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4120 converting entries for indirect and warning symbols into entries
4121 for the target symbol. Set DATA->g to null on error. */
4122
4123 static int
mips_elf_recreate_got(void ** entryp,void * data)4124 mips_elf_recreate_got (void **entryp, void *data)
4125 {
4126 struct mips_got_entry new_entry, *entry;
4127 struct mips_elf_traverse_got_arg *arg;
4128 void **slot;
4129
4130 entry = (struct mips_got_entry *) *entryp;
4131 arg = (struct mips_elf_traverse_got_arg *) data;
4132 if (entry->abfd != NULL
4133 && entry->symndx == -1
4134 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4135 || entry->d.h->root.root.type == bfd_link_hash_warning))
4136 {
4137 struct mips_elf_link_hash_entry *h;
4138
4139 new_entry = *entry;
4140 entry = &new_entry;
4141 h = entry->d.h;
4142 do
4143 {
4144 BFD_ASSERT (h->global_got_area == GGA_NONE);
4145 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4146 }
4147 while (h->root.root.type == bfd_link_hash_indirect
4148 || h->root.root.type == bfd_link_hash_warning);
4149 entry->d.h = h;
4150 }
4151 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4152 if (slot == NULL)
4153 {
4154 arg->g = NULL;
4155 return 0;
4156 }
4157 if (*slot == NULL)
4158 {
4159 if (entry == &new_entry)
4160 {
4161 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4162 if (!entry)
4163 {
4164 arg->g = NULL;
4165 return 0;
4166 }
4167 *entry = new_entry;
4168 }
4169 *slot = entry;
4170 mips_elf_count_got_entry (arg->info, arg->g, entry);
4171 }
4172 return 1;
4173 }
4174
4175 /* Return the maximum number of GOT page entries required for RANGE. */
4176
4177 static bfd_vma
mips_elf_pages_for_range(const struct mips_got_page_range * range)4178 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4179 {
4180 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4181 }
4182
4183 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4184
4185 static bfd_boolean
mips_elf_record_got_page_entry(struct mips_elf_traverse_got_arg * arg,asection * sec,bfd_signed_vma addend)4186 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4187 asection *sec, bfd_signed_vma addend)
4188 {
4189 struct mips_got_info *g = arg->g;
4190 struct mips_got_page_entry lookup, *entry;
4191 struct mips_got_page_range **range_ptr, *range;
4192 bfd_vma old_pages, new_pages;
4193 void **loc;
4194
4195 /* Find the mips_got_page_entry hash table entry for this section. */
4196 lookup.sec = sec;
4197 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4198 if (loc == NULL)
4199 return FALSE;
4200
4201 /* Create a mips_got_page_entry if this is the first time we've
4202 seen the section. */
4203 entry = (struct mips_got_page_entry *) *loc;
4204 if (!entry)
4205 {
4206 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4207 if (!entry)
4208 return FALSE;
4209
4210 entry->sec = sec;
4211 *loc = entry;
4212 }
4213
4214 /* Skip over ranges whose maximum extent cannot share a page entry
4215 with ADDEND. */
4216 range_ptr = &entry->ranges;
4217 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4218 range_ptr = &(*range_ptr)->next;
4219
4220 /* If we scanned to the end of the list, or found a range whose
4221 minimum extent cannot share a page entry with ADDEND, create
4222 a new singleton range. */
4223 range = *range_ptr;
4224 if (!range || addend < range->min_addend - 0xffff)
4225 {
4226 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4227 if (!range)
4228 return FALSE;
4229
4230 range->next = *range_ptr;
4231 range->min_addend = addend;
4232 range->max_addend = addend;
4233
4234 *range_ptr = range;
4235 entry->num_pages++;
4236 g->page_gotno++;
4237 return TRUE;
4238 }
4239
4240 /* Remember how many pages the old range contributed. */
4241 old_pages = mips_elf_pages_for_range (range);
4242
4243 /* Update the ranges. */
4244 if (addend < range->min_addend)
4245 range->min_addend = addend;
4246 else if (addend > range->max_addend)
4247 {
4248 if (range->next && addend >= range->next->min_addend - 0xffff)
4249 {
4250 old_pages += mips_elf_pages_for_range (range->next);
4251 range->max_addend = range->next->max_addend;
4252 range->next = range->next->next;
4253 }
4254 else
4255 range->max_addend = addend;
4256 }
4257
4258 /* Record any change in the total estimate. */
4259 new_pages = mips_elf_pages_for_range (range);
4260 if (old_pages != new_pages)
4261 {
4262 entry->num_pages += new_pages - old_pages;
4263 g->page_gotno += new_pages - old_pages;
4264 }
4265
4266 return TRUE;
4267 }
4268
4269 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4270 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4271 whether the page reference described by *REFP needs a GOT page entry,
4272 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4273
4274 static bfd_boolean
mips_elf_resolve_got_page_ref(void ** refp,void * data)4275 mips_elf_resolve_got_page_ref (void **refp, void *data)
4276 {
4277 struct mips_got_page_ref *ref;
4278 struct mips_elf_traverse_got_arg *arg;
4279 struct mips_elf_link_hash_table *htab;
4280 asection *sec;
4281 bfd_vma addend;
4282
4283 ref = (struct mips_got_page_ref *) *refp;
4284 arg = (struct mips_elf_traverse_got_arg *) data;
4285 htab = mips_elf_hash_table (arg->info);
4286
4287 if (ref->symndx < 0)
4288 {
4289 struct mips_elf_link_hash_entry *h;
4290
4291 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4292 h = ref->u.h;
4293 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4294 return 1;
4295
4296 /* Ignore undefined symbols; we'll issue an error later if
4297 appropriate. */
4298 if (!((h->root.root.type == bfd_link_hash_defined
4299 || h->root.root.type == bfd_link_hash_defweak)
4300 && h->root.root.u.def.section))
4301 return 1;
4302
4303 sec = h->root.root.u.def.section;
4304 addend = h->root.root.u.def.value + ref->addend;
4305 }
4306 else
4307 {
4308 Elf_Internal_Sym *isym;
4309
4310 /* Read in the symbol. */
4311 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4312 ref->symndx);
4313 if (isym == NULL)
4314 {
4315 arg->g = NULL;
4316 return 0;
4317 }
4318
4319 /* Get the associated input section. */
4320 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4321 if (sec == NULL)
4322 {
4323 arg->g = NULL;
4324 return 0;
4325 }
4326
4327 /* If this is a mergable section, work out the section and offset
4328 of the merged data. For section symbols, the addend specifies
4329 of the offset _of_ the first byte in the data, otherwise it
4330 specifies the offset _from_ the first byte. */
4331 if (sec->flags & SEC_MERGE)
4332 {
4333 void *secinfo;
4334
4335 secinfo = elf_section_data (sec)->sec_info;
4336 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4337 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4338 isym->st_value + ref->addend);
4339 else
4340 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4341 isym->st_value) + ref->addend;
4342 }
4343 else
4344 addend = isym->st_value + ref->addend;
4345 }
4346 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4347 {
4348 arg->g = NULL;
4349 return 0;
4350 }
4351 return 1;
4352 }
4353
4354 /* If any entries in G->got_entries are for indirect or warning symbols,
4355 replace them with entries for the target symbol. Convert g->got_page_refs
4356 into got_page_entry structures and estimate the number of page entries
4357 that they require. */
4358
4359 static bfd_boolean
mips_elf_resolve_final_got_entries(struct bfd_link_info * info,struct mips_got_info * g)4360 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4361 struct mips_got_info *g)
4362 {
4363 struct mips_elf_traverse_got_arg tga;
4364 struct mips_got_info oldg;
4365
4366 oldg = *g;
4367
4368 tga.info = info;
4369 tga.g = g;
4370 tga.value = FALSE;
4371 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4372 if (tga.value)
4373 {
4374 *g = oldg;
4375 g->got_entries = htab_create (htab_size (oldg.got_entries),
4376 mips_elf_got_entry_hash,
4377 mips_elf_got_entry_eq, NULL);
4378 if (!g->got_entries)
4379 return FALSE;
4380
4381 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4382 if (!tga.g)
4383 return FALSE;
4384
4385 htab_delete (oldg.got_entries);
4386 }
4387
4388 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4389 mips_got_page_entry_eq, NULL);
4390 if (g->got_page_entries == NULL)
4391 return FALSE;
4392
4393 tga.info = info;
4394 tga.g = g;
4395 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4396
4397 return TRUE;
4398 }
4399
4400 /* Return true if a GOT entry for H should live in the local rather than
4401 global GOT area. */
4402
4403 static bfd_boolean
mips_use_local_got_p(struct bfd_link_info * info,struct mips_elf_link_hash_entry * h)4404 mips_use_local_got_p (struct bfd_link_info *info,
4405 struct mips_elf_link_hash_entry *h)
4406 {
4407 /* Symbols that aren't in the dynamic symbol table must live in the
4408 local GOT. This includes symbols that are completely undefined
4409 and which therefore don't bind locally. We'll report undefined
4410 symbols later if appropriate. */
4411 if (h->root.dynindx == -1)
4412 return TRUE;
4413
4414 /* Symbols that bind locally can (and in the case of forced-local
4415 symbols, must) live in the local GOT. */
4416 if (h->got_only_for_calls
4417 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4418 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4419 return TRUE;
4420
4421 /* If this is an executable that must provide a definition of the symbol,
4422 either though PLTs or copy relocations, then that address should go in
4423 the local rather than global GOT. */
4424 if (info->executable && h->has_static_relocs)
4425 return TRUE;
4426
4427 return FALSE;
4428 }
4429
4430 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4431 link_info structure. Decide whether the hash entry needs an entry in
4432 the global part of the primary GOT, setting global_got_area accordingly.
4433 Count the number of global symbols that are in the primary GOT only
4434 because they have relocations against them (reloc_only_gotno). */
4435
4436 static int
mips_elf_count_got_symbols(struct mips_elf_link_hash_entry * h,void * data)4437 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4438 {
4439 struct bfd_link_info *info;
4440 struct mips_elf_link_hash_table *htab;
4441 struct mips_got_info *g;
4442
4443 info = (struct bfd_link_info *) data;
4444 htab = mips_elf_hash_table (info);
4445 g = htab->got_info;
4446 if (h->global_got_area != GGA_NONE)
4447 {
4448 /* Make a final decision about whether the symbol belongs in the
4449 local or global GOT. */
4450 if (mips_use_local_got_p (info, h))
4451 /* The symbol belongs in the local GOT. We no longer need this
4452 entry if it was only used for relocations; those relocations
4453 will be against the null or section symbol instead of H. */
4454 h->global_got_area = GGA_NONE;
4455 else if (htab->is_vxworks
4456 && h->got_only_for_calls
4457 && h->root.plt.plist->mips_offset != MINUS_ONE)
4458 /* On VxWorks, calls can refer directly to the .got.plt entry;
4459 they don't need entries in the regular GOT. .got.plt entries
4460 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4461 h->global_got_area = GGA_NONE;
4462 else if (h->global_got_area == GGA_RELOC_ONLY)
4463 {
4464 g->reloc_only_gotno++;
4465 g->global_gotno++;
4466 }
4467 }
4468 return 1;
4469 }
4470
4471 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4472 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4473
4474 static int
mips_elf_add_got_entry(void ** entryp,void * data)4475 mips_elf_add_got_entry (void **entryp, void *data)
4476 {
4477 struct mips_got_entry *entry;
4478 struct mips_elf_traverse_got_arg *arg;
4479 void **slot;
4480
4481 entry = (struct mips_got_entry *) *entryp;
4482 arg = (struct mips_elf_traverse_got_arg *) data;
4483 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4484 if (!slot)
4485 {
4486 arg->g = NULL;
4487 return 0;
4488 }
4489 if (!*slot)
4490 {
4491 *slot = entry;
4492 mips_elf_count_got_entry (arg->info, arg->g, entry);
4493 }
4494 return 1;
4495 }
4496
4497 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4498 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4499
4500 static int
mips_elf_add_got_page_entry(void ** entryp,void * data)4501 mips_elf_add_got_page_entry (void **entryp, void *data)
4502 {
4503 struct mips_got_page_entry *entry;
4504 struct mips_elf_traverse_got_arg *arg;
4505 void **slot;
4506
4507 entry = (struct mips_got_page_entry *) *entryp;
4508 arg = (struct mips_elf_traverse_got_arg *) data;
4509 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4510 if (!slot)
4511 {
4512 arg->g = NULL;
4513 return 0;
4514 }
4515 if (!*slot)
4516 {
4517 *slot = entry;
4518 arg->g->page_gotno += entry->num_pages;
4519 }
4520 return 1;
4521 }
4522
4523 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4524 this would lead to overflow, 1 if they were merged successfully,
4525 and 0 if a merge failed due to lack of memory. (These values are chosen
4526 so that nonnegative return values can be returned by a htab_traverse
4527 callback.) */
4528
4529 static int
mips_elf_merge_got_with(bfd * abfd,struct mips_got_info * from,struct mips_got_info * to,struct mips_elf_got_per_bfd_arg * arg)4530 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4531 struct mips_got_info *to,
4532 struct mips_elf_got_per_bfd_arg *arg)
4533 {
4534 struct mips_elf_traverse_got_arg tga;
4535 unsigned int estimate;
4536
4537 /* Work out how many page entries we would need for the combined GOT. */
4538 estimate = arg->max_pages;
4539 if (estimate >= from->page_gotno + to->page_gotno)
4540 estimate = from->page_gotno + to->page_gotno;
4541
4542 /* And conservatively estimate how many local and TLS entries
4543 would be needed. */
4544 estimate += from->local_gotno + to->local_gotno;
4545 estimate += from->tls_gotno + to->tls_gotno;
4546
4547 /* If we're merging with the primary got, any TLS relocations will
4548 come after the full set of global entries. Otherwise estimate those
4549 conservatively as well. */
4550 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4551 estimate += arg->global_count;
4552 else
4553 estimate += from->global_gotno + to->global_gotno;
4554
4555 /* Bail out if the combined GOT might be too big. */
4556 if (estimate > arg->max_count)
4557 return -1;
4558
4559 /* Transfer the bfd's got information from FROM to TO. */
4560 tga.info = arg->info;
4561 tga.g = to;
4562 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4563 if (!tga.g)
4564 return 0;
4565
4566 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4567 if (!tga.g)
4568 return 0;
4569
4570 mips_elf_replace_bfd_got (abfd, to);
4571 return 1;
4572 }
4573
4574 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4575 as possible of the primary got, since it doesn't require explicit
4576 dynamic relocations, but don't use bfds that would reference global
4577 symbols out of the addressable range. Failing the primary got,
4578 attempt to merge with the current got, or finish the current got
4579 and then make make the new got current. */
4580
4581 static bfd_boolean
mips_elf_merge_got(bfd * abfd,struct mips_got_info * g,struct mips_elf_got_per_bfd_arg * arg)4582 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4583 struct mips_elf_got_per_bfd_arg *arg)
4584 {
4585 unsigned int estimate;
4586 int result;
4587
4588 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4589 return FALSE;
4590
4591 /* Work out the number of page, local and TLS entries. */
4592 estimate = arg->max_pages;
4593 if (estimate > g->page_gotno)
4594 estimate = g->page_gotno;
4595 estimate += g->local_gotno + g->tls_gotno;
4596
4597 /* We place TLS GOT entries after both locals and globals. The globals
4598 for the primary GOT may overflow the normal GOT size limit, so be
4599 sure not to merge a GOT which requires TLS with the primary GOT in that
4600 case. This doesn't affect non-primary GOTs. */
4601 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4602
4603 if (estimate <= arg->max_count)
4604 {
4605 /* If we don't have a primary GOT, use it as
4606 a starting point for the primary GOT. */
4607 if (!arg->primary)
4608 {
4609 arg->primary = g;
4610 return TRUE;
4611 }
4612
4613 /* Try merging with the primary GOT. */
4614 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4615 if (result >= 0)
4616 return result;
4617 }
4618
4619 /* If we can merge with the last-created got, do it. */
4620 if (arg->current)
4621 {
4622 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4623 if (result >= 0)
4624 return result;
4625 }
4626
4627 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4628 fits; if it turns out that it doesn't, we'll get relocation
4629 overflows anyway. */
4630 g->next = arg->current;
4631 arg->current = g;
4632
4633 return TRUE;
4634 }
4635
4636 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4637 to GOTIDX, duplicating the entry if it has already been assigned
4638 an index in a different GOT. */
4639
4640 static bfd_boolean
mips_elf_set_gotidx(void ** entryp,long gotidx)4641 mips_elf_set_gotidx (void **entryp, long gotidx)
4642 {
4643 struct mips_got_entry *entry;
4644
4645 entry = (struct mips_got_entry *) *entryp;
4646 if (entry->gotidx > 0)
4647 {
4648 struct mips_got_entry *new_entry;
4649
4650 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4651 if (!new_entry)
4652 return FALSE;
4653
4654 *new_entry = *entry;
4655 *entryp = new_entry;
4656 entry = new_entry;
4657 }
4658 entry->gotidx = gotidx;
4659 return TRUE;
4660 }
4661
4662 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4663 mips_elf_traverse_got_arg in which DATA->value is the size of one
4664 GOT entry. Set DATA->g to null on failure. */
4665
4666 static int
mips_elf_initialize_tls_index(void ** entryp,void * data)4667 mips_elf_initialize_tls_index (void **entryp, void *data)
4668 {
4669 struct mips_got_entry *entry;
4670 struct mips_elf_traverse_got_arg *arg;
4671
4672 /* We're only interested in TLS symbols. */
4673 entry = (struct mips_got_entry *) *entryp;
4674 if (entry->tls_type == GOT_TLS_NONE)
4675 return 1;
4676
4677 arg = (struct mips_elf_traverse_got_arg *) data;
4678 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4679 {
4680 arg->g = NULL;
4681 return 0;
4682 }
4683
4684 /* Account for the entries we've just allocated. */
4685 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4686 return 1;
4687 }
4688
4689 /* A htab_traverse callback for GOT entries, where DATA points to a
4690 mips_elf_traverse_got_arg. Set the global_got_area of each global
4691 symbol to DATA->value. */
4692
4693 static int
mips_elf_set_global_got_area(void ** entryp,void * data)4694 mips_elf_set_global_got_area (void **entryp, void *data)
4695 {
4696 struct mips_got_entry *entry;
4697 struct mips_elf_traverse_got_arg *arg;
4698
4699 entry = (struct mips_got_entry *) *entryp;
4700 arg = (struct mips_elf_traverse_got_arg *) data;
4701 if (entry->abfd != NULL
4702 && entry->symndx == -1
4703 && entry->d.h->global_got_area != GGA_NONE)
4704 entry->d.h->global_got_area = arg->value;
4705 return 1;
4706 }
4707
4708 /* A htab_traverse callback for secondary GOT entries, where DATA points
4709 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4710 and record the number of relocations they require. DATA->value is
4711 the size of one GOT entry. Set DATA->g to null on failure. */
4712
4713 static int
mips_elf_set_global_gotidx(void ** entryp,void * data)4714 mips_elf_set_global_gotidx (void **entryp, void *data)
4715 {
4716 struct mips_got_entry *entry;
4717 struct mips_elf_traverse_got_arg *arg;
4718
4719 entry = (struct mips_got_entry *) *entryp;
4720 arg = (struct mips_elf_traverse_got_arg *) data;
4721 if (entry->abfd != NULL
4722 && entry->symndx == -1
4723 && entry->d.h->global_got_area != GGA_NONE)
4724 {
4725 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4726 {
4727 arg->g = NULL;
4728 return 0;
4729 }
4730 arg->g->assigned_low_gotno += 1;
4731
4732 if (arg->info->shared
4733 || (elf_hash_table (arg->info)->dynamic_sections_created
4734 && entry->d.h->root.def_dynamic
4735 && !entry->d.h->root.def_regular))
4736 arg->g->relocs += 1;
4737 }
4738
4739 return 1;
4740 }
4741
4742 /* A htab_traverse callback for GOT entries for which DATA is the
4743 bfd_link_info. Forbid any global symbols from having traditional
4744 lazy-binding stubs. */
4745
4746 static int
mips_elf_forbid_lazy_stubs(void ** entryp,void * data)4747 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4748 {
4749 struct bfd_link_info *info;
4750 struct mips_elf_link_hash_table *htab;
4751 struct mips_got_entry *entry;
4752
4753 entry = (struct mips_got_entry *) *entryp;
4754 info = (struct bfd_link_info *) data;
4755 htab = mips_elf_hash_table (info);
4756 BFD_ASSERT (htab != NULL);
4757
4758 if (entry->abfd != NULL
4759 && entry->symndx == -1
4760 && entry->d.h->needs_lazy_stub)
4761 {
4762 entry->d.h->needs_lazy_stub = FALSE;
4763 htab->lazy_stub_count--;
4764 }
4765
4766 return 1;
4767 }
4768
4769 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4770 the primary GOT. */
4771 static bfd_vma
mips_elf_adjust_gp(bfd * abfd,struct mips_got_info * g,bfd * ibfd)4772 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4773 {
4774 if (!g->next)
4775 return 0;
4776
4777 g = mips_elf_bfd_got (ibfd, FALSE);
4778 if (! g)
4779 return 0;
4780
4781 BFD_ASSERT (g->next);
4782
4783 g = g->next;
4784
4785 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4786 * MIPS_ELF_GOT_SIZE (abfd);
4787 }
4788
4789 /* Turn a single GOT that is too big for 16-bit addressing into
4790 a sequence of GOTs, each one 16-bit addressable. */
4791
4792 static bfd_boolean
mips_elf_multi_got(bfd * abfd,struct bfd_link_info * info,asection * got,bfd_size_type pages)4793 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4794 asection *got, bfd_size_type pages)
4795 {
4796 struct mips_elf_link_hash_table *htab;
4797 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4798 struct mips_elf_traverse_got_arg tga;
4799 struct mips_got_info *g, *gg;
4800 unsigned int assign, needed_relocs;
4801 bfd *dynobj, *ibfd;
4802
4803 dynobj = elf_hash_table (info)->dynobj;
4804 htab = mips_elf_hash_table (info);
4805 BFD_ASSERT (htab != NULL);
4806
4807 g = htab->got_info;
4808
4809 got_per_bfd_arg.obfd = abfd;
4810 got_per_bfd_arg.info = info;
4811 got_per_bfd_arg.current = NULL;
4812 got_per_bfd_arg.primary = NULL;
4813 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4814 / MIPS_ELF_GOT_SIZE (abfd))
4815 - htab->reserved_gotno);
4816 got_per_bfd_arg.max_pages = pages;
4817 /* The number of globals that will be included in the primary GOT.
4818 See the calls to mips_elf_set_global_got_area below for more
4819 information. */
4820 got_per_bfd_arg.global_count = g->global_gotno;
4821
4822 /* Try to merge the GOTs of input bfds together, as long as they
4823 don't seem to exceed the maximum GOT size, choosing one of them
4824 to be the primary GOT. */
4825 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
4826 {
4827 gg = mips_elf_bfd_got (ibfd, FALSE);
4828 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4829 return FALSE;
4830 }
4831
4832 /* If we do not find any suitable primary GOT, create an empty one. */
4833 if (got_per_bfd_arg.primary == NULL)
4834 g->next = mips_elf_create_got_info (abfd);
4835 else
4836 g->next = got_per_bfd_arg.primary;
4837 g->next->next = got_per_bfd_arg.current;
4838
4839 /* GG is now the master GOT, and G is the primary GOT. */
4840 gg = g;
4841 g = g->next;
4842
4843 /* Map the output bfd to the primary got. That's what we're going
4844 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4845 didn't mark in check_relocs, and we want a quick way to find it.
4846 We can't just use gg->next because we're going to reverse the
4847 list. */
4848 mips_elf_replace_bfd_got (abfd, g);
4849
4850 /* Every symbol that is referenced in a dynamic relocation must be
4851 present in the primary GOT, so arrange for them to appear after
4852 those that are actually referenced. */
4853 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4854 g->global_gotno = gg->global_gotno;
4855
4856 tga.info = info;
4857 tga.value = GGA_RELOC_ONLY;
4858 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4859 tga.value = GGA_NORMAL;
4860 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
4861
4862 /* Now go through the GOTs assigning them offset ranges.
4863 [assigned_low_gotno, local_gotno[ will be set to the range of local
4864 entries in each GOT. We can then compute the end of a GOT by
4865 adding local_gotno to global_gotno. We reverse the list and make
4866 it circular since then we'll be able to quickly compute the
4867 beginning of a GOT, by computing the end of its predecessor. To
4868 avoid special cases for the primary GOT, while still preserving
4869 assertions that are valid for both single- and multi-got links,
4870 we arrange for the main got struct to have the right number of
4871 global entries, but set its local_gotno such that the initial
4872 offset of the primary GOT is zero. Remember that the primary GOT
4873 will become the last item in the circular linked list, so it
4874 points back to the master GOT. */
4875 gg->local_gotno = -g->global_gotno;
4876 gg->global_gotno = g->global_gotno;
4877 gg->tls_gotno = 0;
4878 assign = 0;
4879 gg->next = gg;
4880
4881 do
4882 {
4883 struct mips_got_info *gn;
4884
4885 assign += htab->reserved_gotno;
4886 g->assigned_low_gotno = assign;
4887 g->local_gotno += assign;
4888 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4889 g->assigned_high_gotno = g->local_gotno - 1;
4890 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4891
4892 /* Take g out of the direct list, and push it onto the reversed
4893 list that gg points to. g->next is guaranteed to be nonnull after
4894 this operation, as required by mips_elf_initialize_tls_index. */
4895 gn = g->next;
4896 g->next = gg->next;
4897 gg->next = g;
4898
4899 /* Set up any TLS entries. We always place the TLS entries after
4900 all non-TLS entries. */
4901 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4902 tga.g = g;
4903 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4904 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4905 if (!tga.g)
4906 return FALSE;
4907 BFD_ASSERT (g->tls_assigned_gotno == assign);
4908
4909 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4910 g = gn;
4911
4912 /* Forbid global symbols in every non-primary GOT from having
4913 lazy-binding stubs. */
4914 if (g)
4915 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4916 }
4917 while (g);
4918
4919 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
4920
4921 needed_relocs = 0;
4922 for (g = gg->next; g && g->next != gg; g = g->next)
4923 {
4924 unsigned int save_assign;
4925
4926 /* Assign offsets to global GOT entries and count how many
4927 relocations they need. */
4928 save_assign = g->assigned_low_gotno;
4929 g->assigned_low_gotno = g->local_gotno;
4930 tga.info = info;
4931 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4932 tga.g = g;
4933 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
4934 if (!tga.g)
4935 return FALSE;
4936 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
4937 g->assigned_low_gotno = save_assign;
4938
4939 if (info->shared)
4940 {
4941 g->relocs += g->local_gotno - g->assigned_low_gotno;
4942 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
4943 + g->next->global_gotno
4944 + g->next->tls_gotno
4945 + htab->reserved_gotno);
4946 }
4947 needed_relocs += g->relocs;
4948 }
4949 needed_relocs += g->relocs;
4950
4951 if (needed_relocs)
4952 mips_elf_allocate_dynamic_relocations (dynobj, info,
4953 needed_relocs);
4954
4955 return TRUE;
4956 }
4957
4958
4959 /* Returns the first relocation of type r_type found, beginning with
4960 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4961
4962 static const Elf_Internal_Rela *
mips_elf_next_relocation(bfd * abfd ATTRIBUTE_UNUSED,unsigned int r_type,const Elf_Internal_Rela * relocation,const Elf_Internal_Rela * relend)4963 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4964 const Elf_Internal_Rela *relocation,
4965 const Elf_Internal_Rela *relend)
4966 {
4967 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4968
4969 while (relocation < relend)
4970 {
4971 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4972 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4973 return relocation;
4974
4975 ++relocation;
4976 }
4977
4978 /* We didn't find it. */
4979 return NULL;
4980 }
4981
4982 /* Return whether an input relocation is against a local symbol. */
4983
4984 static bfd_boolean
mips_elf_local_relocation_p(bfd * input_bfd,const Elf_Internal_Rela * relocation,asection ** local_sections)4985 mips_elf_local_relocation_p (bfd *input_bfd,
4986 const Elf_Internal_Rela *relocation,
4987 asection **local_sections)
4988 {
4989 unsigned long r_symndx;
4990 Elf_Internal_Shdr *symtab_hdr;
4991 size_t extsymoff;
4992
4993 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4994 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4995 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4996
4997 if (r_symndx < extsymoff)
4998 return TRUE;
4999 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
5000 return TRUE;
5001
5002 return FALSE;
5003 }
5004
5005 /* Sign-extend VALUE, which has the indicated number of BITS. */
5006
5007 bfd_vma
_bfd_mips_elf_sign_extend(bfd_vma value,int bits)5008 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
5009 {
5010 if (value & ((bfd_vma) 1 << (bits - 1)))
5011 /* VALUE is negative. */
5012 value |= ((bfd_vma) - 1) << bits;
5013
5014 return value;
5015 }
5016
5017 /* Return non-zero if the indicated VALUE has overflowed the maximum
5018 range expressible by a signed number with the indicated number of
5019 BITS. */
5020
5021 static bfd_boolean
mips_elf_overflow_p(bfd_vma value,int bits)5022 mips_elf_overflow_p (bfd_vma value, int bits)
5023 {
5024 bfd_signed_vma svalue = (bfd_signed_vma) value;
5025
5026 if (svalue > (1 << (bits - 1)) - 1)
5027 /* The value is too big. */
5028 return TRUE;
5029 else if (svalue < -(1 << (bits - 1)))
5030 /* The value is too small. */
5031 return TRUE;
5032
5033 /* All is well. */
5034 return FALSE;
5035 }
5036
5037 /* Calculate the %high function. */
5038
5039 static bfd_vma
mips_elf_high(bfd_vma value)5040 mips_elf_high (bfd_vma value)
5041 {
5042 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5043 }
5044
5045 /* Calculate the %higher function. */
5046
5047 static bfd_vma
mips_elf_higher(bfd_vma value ATTRIBUTE_UNUSED)5048 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
5049 {
5050 #ifdef BFD64
5051 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5052 #else
5053 abort ();
5054 return MINUS_ONE;
5055 #endif
5056 }
5057
5058 /* Calculate the %highest function. */
5059
5060 static bfd_vma
mips_elf_highest(bfd_vma value ATTRIBUTE_UNUSED)5061 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
5062 {
5063 #ifdef BFD64
5064 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
5065 #else
5066 abort ();
5067 return MINUS_ONE;
5068 #endif
5069 }
5070
5071 /* Create the .compact_rel section. */
5072
5073 static bfd_boolean
mips_elf_create_compact_rel_section(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED)5074 mips_elf_create_compact_rel_section
5075 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
5076 {
5077 flagword flags;
5078 register asection *s;
5079
5080 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
5081 {
5082 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5083 | SEC_READONLY);
5084
5085 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5086 if (s == NULL
5087 || ! bfd_set_section_alignment (abfd, s,
5088 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5089 return FALSE;
5090
5091 s->size = sizeof (Elf32_External_compact_rel);
5092 }
5093
5094 return TRUE;
5095 }
5096
5097 /* Create the .got section to hold the global offset table. */
5098
5099 static bfd_boolean
mips_elf_create_got_section(bfd * abfd,struct bfd_link_info * info)5100 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5101 {
5102 flagword flags;
5103 register asection *s;
5104 struct elf_link_hash_entry *h;
5105 struct bfd_link_hash_entry *bh;
5106 struct mips_elf_link_hash_table *htab;
5107
5108 htab = mips_elf_hash_table (info);
5109 BFD_ASSERT (htab != NULL);
5110
5111 /* This function may be called more than once. */
5112 if (htab->sgot)
5113 return TRUE;
5114
5115 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5116 | SEC_LINKER_CREATED);
5117
5118 /* We have to use an alignment of 2**4 here because this is hardcoded
5119 in the function stub generation and in the linker script. */
5120 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5121 if (s == NULL
5122 || ! bfd_set_section_alignment (abfd, s, 4))
5123 return FALSE;
5124 htab->sgot = s;
5125
5126 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5127 linker script because we don't want to define the symbol if we
5128 are not creating a global offset table. */
5129 bh = NULL;
5130 if (! (_bfd_generic_link_add_one_symbol
5131 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5132 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5133 return FALSE;
5134
5135 h = (struct elf_link_hash_entry *) bh;
5136 h->non_elf = 0;
5137 h->def_regular = 1;
5138 h->type = STT_OBJECT;
5139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5140 elf_hash_table (info)->hgot = h;
5141
5142 if (info->shared
5143 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5144 return FALSE;
5145
5146 htab->got_info = mips_elf_create_got_info (abfd);
5147 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5148 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5149
5150 /* We also need a .got.plt section when generating PLTs. */
5151 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5152 SEC_ALLOC | SEC_LOAD
5153 | SEC_HAS_CONTENTS
5154 | SEC_IN_MEMORY
5155 | SEC_LINKER_CREATED);
5156 if (s == NULL)
5157 return FALSE;
5158 htab->sgotplt = s;
5159
5160 return TRUE;
5161 }
5162
5163 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5164 __GOTT_INDEX__ symbols. These symbols are only special for
5165 shared objects; they are not used in executables. */
5166
5167 static bfd_boolean
is_gott_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)5168 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5169 {
5170 return (mips_elf_hash_table (info)->is_vxworks
5171 && info->shared
5172 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5173 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5174 }
5175
5176 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5177 require an la25 stub. See also mips_elf_local_pic_function_p,
5178 which determines whether the destination function ever requires a
5179 stub. */
5180
5181 static bfd_boolean
mips_elf_relocation_needs_la25_stub(bfd * input_bfd,int r_type,bfd_boolean target_is_16_bit_code_p)5182 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5183 bfd_boolean target_is_16_bit_code_p)
5184 {
5185 /* We specifically ignore branches and jumps from EF_PIC objects,
5186 where the onus is on the compiler or programmer to perform any
5187 necessary initialization of $25. Sometimes such initialization
5188 is unnecessary; for example, -mno-shared functions do not use
5189 the incoming value of $25, and may therefore be called directly. */
5190 if (PIC_OBJECT_P (input_bfd))
5191 return FALSE;
5192
5193 switch (r_type)
5194 {
5195 case R_MIPS_26:
5196 case R_MIPS_PC16:
5197 case R_MIPS_PC21_S2:
5198 case R_MIPS_PC26_S2:
5199 case R_MICROMIPS_26_S1:
5200 case R_MICROMIPS_PC7_S1:
5201 case R_MICROMIPS_PC10_S1:
5202 case R_MICROMIPS_PC16_S1:
5203 case R_MICROMIPS_PC23_S2:
5204 return TRUE;
5205
5206 case R_MIPS16_26:
5207 return !target_is_16_bit_code_p;
5208
5209 default:
5210 return FALSE;
5211 }
5212 }
5213
5214 /* Calculate the value produced by the RELOCATION (which comes from
5215 the INPUT_BFD). The ADDEND is the addend to use for this
5216 RELOCATION; RELOCATION->R_ADDEND is ignored.
5217
5218 The result of the relocation calculation is stored in VALUEP.
5219 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5220 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5221
5222 This function returns bfd_reloc_continue if the caller need take no
5223 further action regarding this relocation, bfd_reloc_notsupported if
5224 something goes dramatically wrong, bfd_reloc_overflow if an
5225 overflow occurs, and bfd_reloc_ok to indicate success. */
5226
5227 static bfd_reloc_status_type
mips_elf_calculate_relocation(bfd * abfd,bfd * input_bfd,asection * input_section,struct bfd_link_info * info,const Elf_Internal_Rela * relocation,bfd_vma addend,reloc_howto_type * howto,Elf_Internal_Sym * local_syms,asection ** local_sections,bfd_vma * valuep,const char ** namep,bfd_boolean * cross_mode_jump_p,bfd_boolean save_addend)5228 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5229 asection *input_section,
5230 struct bfd_link_info *info,
5231 const Elf_Internal_Rela *relocation,
5232 bfd_vma addend, reloc_howto_type *howto,
5233 Elf_Internal_Sym *local_syms,
5234 asection **local_sections, bfd_vma *valuep,
5235 const char **namep,
5236 bfd_boolean *cross_mode_jump_p,
5237 bfd_boolean save_addend)
5238 {
5239 /* The eventual value we will return. */
5240 bfd_vma value;
5241 /* The address of the symbol against which the relocation is
5242 occurring. */
5243 bfd_vma symbol = 0;
5244 /* The final GP value to be used for the relocatable, executable, or
5245 shared object file being produced. */
5246 bfd_vma gp;
5247 /* The place (section offset or address) of the storage unit being
5248 relocated. */
5249 bfd_vma p;
5250 /* The value of GP used to create the relocatable object. */
5251 bfd_vma gp0;
5252 /* The offset into the global offset table at which the address of
5253 the relocation entry symbol, adjusted by the addend, resides
5254 during execution. */
5255 bfd_vma g = MINUS_ONE;
5256 /* The section in which the symbol referenced by the relocation is
5257 located. */
5258 asection *sec = NULL;
5259 struct mips_elf_link_hash_entry *h = NULL;
5260 /* TRUE if the symbol referred to by this relocation is a local
5261 symbol. */
5262 bfd_boolean local_p, was_local_p;
5263 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5264 bfd_boolean gp_disp_p = FALSE;
5265 /* TRUE if the symbol referred to by this relocation is
5266 "__gnu_local_gp". */
5267 bfd_boolean gnu_local_gp_p = FALSE;
5268 Elf_Internal_Shdr *symtab_hdr;
5269 size_t extsymoff;
5270 unsigned long r_symndx;
5271 int r_type;
5272 /* TRUE if overflow occurred during the calculation of the
5273 relocation value. */
5274 bfd_boolean overflowed_p;
5275 /* TRUE if this relocation refers to a MIPS16 function. */
5276 bfd_boolean target_is_16_bit_code_p = FALSE;
5277 bfd_boolean target_is_micromips_code_p = FALSE;
5278 struct mips_elf_link_hash_table *htab;
5279 bfd *dynobj;
5280
5281 dynobj = elf_hash_table (info)->dynobj;
5282 htab = mips_elf_hash_table (info);
5283 BFD_ASSERT (htab != NULL);
5284
5285 /* Parse the relocation. */
5286 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5287 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5288 p = (input_section->output_section->vma
5289 + input_section->output_offset
5290 + relocation->r_offset);
5291
5292 /* Assume that there will be no overflow. */
5293 overflowed_p = FALSE;
5294
5295 /* Figure out whether or not the symbol is local, and get the offset
5296 used in the array of hash table entries. */
5297 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5298 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5299 local_sections);
5300 was_local_p = local_p;
5301 if (! elf_bad_symtab (input_bfd))
5302 extsymoff = symtab_hdr->sh_info;
5303 else
5304 {
5305 /* The symbol table does not follow the rule that local symbols
5306 must come before globals. */
5307 extsymoff = 0;
5308 }
5309
5310 /* Figure out the value of the symbol. */
5311 if (local_p)
5312 {
5313 Elf_Internal_Sym *sym;
5314
5315 sym = local_syms + r_symndx;
5316 sec = local_sections[r_symndx];
5317
5318 symbol = sec->output_section->vma + sec->output_offset;
5319 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5320 || (sec->flags & SEC_MERGE))
5321 symbol += sym->st_value;
5322 if ((sec->flags & SEC_MERGE)
5323 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5324 {
5325 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5326 addend -= symbol;
5327 addend += sec->output_section->vma + sec->output_offset;
5328 }
5329
5330 /* MIPS16/microMIPS text labels should be treated as odd. */
5331 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5332 ++symbol;
5333
5334 /* Record the name of this symbol, for our caller. */
5335 *namep = bfd_elf_string_from_elf_section (input_bfd,
5336 symtab_hdr->sh_link,
5337 sym->st_name);
5338 if (*namep == '\0')
5339 *namep = bfd_section_name (input_bfd, sec);
5340
5341 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5342 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5343 }
5344 else
5345 {
5346 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5347
5348 /* For global symbols we look up the symbol in the hash-table. */
5349 h = ((struct mips_elf_link_hash_entry *)
5350 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5351 /* Find the real hash-table entry for this symbol. */
5352 while (h->root.root.type == bfd_link_hash_indirect
5353 || h->root.root.type == bfd_link_hash_warning)
5354 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5355
5356 /* Record the name of this symbol, for our caller. */
5357 *namep = h->root.root.root.string;
5358
5359 /* See if this is the special _gp_disp symbol. Note that such a
5360 symbol must always be a global symbol. */
5361 if (strcmp (*namep, "_gp_disp") == 0
5362 && ! NEWABI_P (input_bfd))
5363 {
5364 /* Relocations against _gp_disp are permitted only with
5365 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5366 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5367 return bfd_reloc_notsupported;
5368
5369 gp_disp_p = TRUE;
5370 }
5371 /* See if this is the special _gp symbol. Note that such a
5372 symbol must always be a global symbol. */
5373 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5374 gnu_local_gp_p = TRUE;
5375
5376
5377 /* If this symbol is defined, calculate its address. Note that
5378 _gp_disp is a magic symbol, always implicitly defined by the
5379 linker, so it's inappropriate to check to see whether or not
5380 its defined. */
5381 else if ((h->root.root.type == bfd_link_hash_defined
5382 || h->root.root.type == bfd_link_hash_defweak)
5383 && h->root.root.u.def.section)
5384 {
5385 sec = h->root.root.u.def.section;
5386 if (sec->output_section)
5387 symbol = (h->root.root.u.def.value
5388 + sec->output_section->vma
5389 + sec->output_offset);
5390 else
5391 symbol = h->root.root.u.def.value;
5392 }
5393 else if (h->root.root.type == bfd_link_hash_undefweak)
5394 /* We allow relocations against undefined weak symbols, giving
5395 it the value zero, so that you can undefined weak functions
5396 and check to see if they exist by looking at their
5397 addresses. */
5398 symbol = 0;
5399 else if (info->unresolved_syms_in_objects == RM_IGNORE
5400 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5401 symbol = 0;
5402 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5403 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5404 {
5405 /* If this is a dynamic link, we should have created a
5406 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5407 in in _bfd_mips_elf_create_dynamic_sections.
5408 Otherwise, we should define the symbol with a value of 0.
5409 FIXME: It should probably get into the symbol table
5410 somehow as well. */
5411 BFD_ASSERT (! info->shared);
5412 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5413 symbol = 0;
5414 }
5415 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5416 {
5417 /* This is an optional symbol - an Irix specific extension to the
5418 ELF spec. Ignore it for now.
5419 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5420 than simply ignoring them, but we do not handle this for now.
5421 For information see the "64-bit ELF Object File Specification"
5422 which is available from here:
5423 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5424 symbol = 0;
5425 }
5426 else if ((*info->callbacks->undefined_symbol)
5427 (info, h->root.root.root.string, input_bfd,
5428 input_section, relocation->r_offset,
5429 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5430 || ELF_ST_VISIBILITY (h->root.other)))
5431 {
5432 return bfd_reloc_undefined;
5433 }
5434 else
5435 {
5436 return bfd_reloc_notsupported;
5437 }
5438
5439 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5440 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5441 }
5442
5443 /* If this is a reference to a 16-bit function with a stub, we need
5444 to redirect the relocation to the stub unless:
5445
5446 (a) the relocation is for a MIPS16 JAL;
5447
5448 (b) the relocation is for a MIPS16 PIC call, and there are no
5449 non-MIPS16 uses of the GOT slot; or
5450
5451 (c) the section allows direct references to MIPS16 functions. */
5452 if (r_type != R_MIPS16_26
5453 && !info->relocatable
5454 && ((h != NULL
5455 && h->fn_stub != NULL
5456 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5457 || (local_p
5458 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5459 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5460 && !section_allows_mips16_refs_p (input_section))
5461 {
5462 /* This is a 32- or 64-bit call to a 16-bit function. We should
5463 have already noticed that we were going to need the
5464 stub. */
5465 if (local_p)
5466 {
5467 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5468 value = 0;
5469 }
5470 else
5471 {
5472 BFD_ASSERT (h->need_fn_stub);
5473 if (h->la25_stub)
5474 {
5475 /* If a LA25 header for the stub itself exists, point to the
5476 prepended LUI/ADDIU sequence. */
5477 sec = h->la25_stub->stub_section;
5478 value = h->la25_stub->offset;
5479 }
5480 else
5481 {
5482 sec = h->fn_stub;
5483 value = 0;
5484 }
5485 }
5486
5487 symbol = sec->output_section->vma + sec->output_offset + value;
5488 /* The target is 16-bit, but the stub isn't. */
5489 target_is_16_bit_code_p = FALSE;
5490 }
5491 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5492 to a standard MIPS function, we need to redirect the call to the stub.
5493 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5494 indirect calls should use an indirect stub instead. */
5495 else if (r_type == R_MIPS16_26 && !info->relocatable
5496 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5497 || (local_p
5498 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5499 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5500 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5501 {
5502 if (local_p)
5503 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5504 else
5505 {
5506 /* If both call_stub and call_fp_stub are defined, we can figure
5507 out which one to use by checking which one appears in the input
5508 file. */
5509 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5510 {
5511 asection *o;
5512
5513 sec = NULL;
5514 for (o = input_bfd->sections; o != NULL; o = o->next)
5515 {
5516 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5517 {
5518 sec = h->call_fp_stub;
5519 break;
5520 }
5521 }
5522 if (sec == NULL)
5523 sec = h->call_stub;
5524 }
5525 else if (h->call_stub != NULL)
5526 sec = h->call_stub;
5527 else
5528 sec = h->call_fp_stub;
5529 }
5530
5531 BFD_ASSERT (sec->size > 0);
5532 symbol = sec->output_section->vma + sec->output_offset;
5533 }
5534 /* If this is a direct call to a PIC function, redirect to the
5535 non-PIC stub. */
5536 else if (h != NULL && h->la25_stub
5537 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5538 target_is_16_bit_code_p))
5539 symbol = (h->la25_stub->stub_section->output_section->vma
5540 + h->la25_stub->stub_section->output_offset
5541 + h->la25_stub->offset);
5542 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5543 entry is used if a standard PLT entry has also been made. In this
5544 case the symbol will have been set by mips_elf_set_plt_sym_value
5545 to point to the standard PLT entry, so redirect to the compressed
5546 one. */
5547 else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
5548 && !info->relocatable
5549 && h != NULL
5550 && h->use_plt_entry
5551 && h->root.plt.plist->comp_offset != MINUS_ONE
5552 && h->root.plt.plist->mips_offset != MINUS_ONE)
5553 {
5554 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5555
5556 sec = htab->splt;
5557 symbol = (sec->output_section->vma
5558 + sec->output_offset
5559 + htab->plt_header_size
5560 + htab->plt_mips_offset
5561 + h->root.plt.plist->comp_offset
5562 + 1);
5563
5564 target_is_16_bit_code_p = !micromips_p;
5565 target_is_micromips_code_p = micromips_p;
5566 }
5567
5568 /* Make sure MIPS16 and microMIPS are not used together. */
5569 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5570 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5571 {
5572 (*_bfd_error_handler)
5573 (_("MIPS16 and microMIPS functions cannot call each other"));
5574 return bfd_reloc_notsupported;
5575 }
5576
5577 /* Calls from 16-bit code to 32-bit code and vice versa require the
5578 mode change. However, we can ignore calls to undefined weak symbols,
5579 which should never be executed at runtime. This exception is important
5580 because the assembly writer may have "known" that any definition of the
5581 symbol would be 16-bit code, and that direct jumps were therefore
5582 acceptable. */
5583 *cross_mode_jump_p = (!info->relocatable
5584 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5585 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5586 || (r_type == R_MICROMIPS_26_S1
5587 && !target_is_micromips_code_p)
5588 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5589 && (target_is_16_bit_code_p
5590 || target_is_micromips_code_p))));
5591
5592 local_p = (h == NULL || mips_use_local_got_p (info, h));
5593
5594 gp0 = _bfd_get_gp_value (input_bfd);
5595 gp = _bfd_get_gp_value (abfd);
5596 if (htab->got_info)
5597 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5598
5599 if (gnu_local_gp_p)
5600 symbol = gp;
5601
5602 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5603 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5604 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5605 if (got_page_reloc_p (r_type) && !local_p)
5606 {
5607 r_type = (micromips_reloc_p (r_type)
5608 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5609 addend = 0;
5610 }
5611
5612 /* If we haven't already determined the GOT offset, and we're going
5613 to need it, get it now. */
5614 switch (r_type)
5615 {
5616 case R_MIPS16_CALL16:
5617 case R_MIPS16_GOT16:
5618 case R_MIPS_CALL16:
5619 case R_MIPS_GOT16:
5620 case R_MIPS_GOT_DISP:
5621 case R_MIPS_GOT_HI16:
5622 case R_MIPS_CALL_HI16:
5623 case R_MIPS_GOT_LO16:
5624 case R_MIPS_CALL_LO16:
5625 case R_MICROMIPS_CALL16:
5626 case R_MICROMIPS_GOT16:
5627 case R_MICROMIPS_GOT_DISP:
5628 case R_MICROMIPS_GOT_HI16:
5629 case R_MICROMIPS_CALL_HI16:
5630 case R_MICROMIPS_GOT_LO16:
5631 case R_MICROMIPS_CALL_LO16:
5632 case R_MIPS_TLS_GD:
5633 case R_MIPS_TLS_GOTTPREL:
5634 case R_MIPS_TLS_LDM:
5635 case R_MIPS16_TLS_GD:
5636 case R_MIPS16_TLS_GOTTPREL:
5637 case R_MIPS16_TLS_LDM:
5638 case R_MICROMIPS_TLS_GD:
5639 case R_MICROMIPS_TLS_GOTTPREL:
5640 case R_MICROMIPS_TLS_LDM:
5641 /* Find the index into the GOT where this value is located. */
5642 if (tls_ldm_reloc_p (r_type))
5643 {
5644 g = mips_elf_local_got_index (abfd, input_bfd, info,
5645 0, 0, NULL, r_type);
5646 if (g == MINUS_ONE)
5647 return bfd_reloc_outofrange;
5648 }
5649 else if (!local_p)
5650 {
5651 /* On VxWorks, CALL relocations should refer to the .got.plt
5652 entry, which is initialized to point at the PLT stub. */
5653 if (htab->is_vxworks
5654 && (call_hi16_reloc_p (r_type)
5655 || call_lo16_reloc_p (r_type)
5656 || call16_reloc_p (r_type)))
5657 {
5658 BFD_ASSERT (addend == 0);
5659 BFD_ASSERT (h->root.needs_plt);
5660 g = mips_elf_gotplt_index (info, &h->root);
5661 }
5662 else
5663 {
5664 BFD_ASSERT (addend == 0);
5665 g = mips_elf_global_got_index (abfd, info, input_bfd,
5666 &h->root, r_type);
5667 if (!TLS_RELOC_P (r_type)
5668 && !elf_hash_table (info)->dynamic_sections_created)
5669 /* This is a static link. We must initialize the GOT entry. */
5670 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5671 }
5672 }
5673 else if (!htab->is_vxworks
5674 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5675 /* The calculation below does not involve "g". */
5676 break;
5677 else
5678 {
5679 g = mips_elf_local_got_index (abfd, input_bfd, info,
5680 symbol + addend, r_symndx, h, r_type);
5681 if (g == MINUS_ONE)
5682 return bfd_reloc_outofrange;
5683 }
5684
5685 /* Convert GOT indices to actual offsets. */
5686 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5687 break;
5688 }
5689
5690 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5691 symbols are resolved by the loader. Add them to .rela.dyn. */
5692 if (h != NULL && is_gott_symbol (info, &h->root))
5693 {
5694 Elf_Internal_Rela outrel;
5695 bfd_byte *loc;
5696 asection *s;
5697
5698 s = mips_elf_rel_dyn_section (info, FALSE);
5699 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5700
5701 outrel.r_offset = (input_section->output_section->vma
5702 + input_section->output_offset
5703 + relocation->r_offset);
5704 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5705 outrel.r_addend = addend;
5706 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5707
5708 /* If we've written this relocation for a readonly section,
5709 we need to set DF_TEXTREL again, so that we do not delete the
5710 DT_TEXTREL tag. */
5711 if (MIPS_ELF_READONLY_SECTION (input_section))
5712 info->flags |= DF_TEXTREL;
5713
5714 *valuep = 0;
5715 return bfd_reloc_ok;
5716 }
5717
5718 /* Figure out what kind of relocation is being performed. */
5719 switch (r_type)
5720 {
5721 case R_MIPS_NONE:
5722 return bfd_reloc_continue;
5723
5724 case R_MIPS_16:
5725 if (howto->partial_inplace)
5726 addend = _bfd_mips_elf_sign_extend (addend, 16);
5727 value = symbol + addend;
5728 overflowed_p = mips_elf_overflow_p (value, 16);
5729 break;
5730
5731 case R_MIPS_32:
5732 case R_MIPS_REL32:
5733 case R_MIPS_64:
5734 if ((info->shared
5735 || (htab->root.dynamic_sections_created
5736 && h != NULL
5737 && h->root.def_dynamic
5738 && !h->root.def_regular
5739 && !h->has_static_relocs))
5740 && r_symndx != STN_UNDEF
5741 && (h == NULL
5742 || h->root.root.type != bfd_link_hash_undefweak
5743 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5744 && (input_section->flags & SEC_ALLOC) != 0)
5745 {
5746 /* If we're creating a shared library, then we can't know
5747 where the symbol will end up. So, we create a relocation
5748 record in the output, and leave the job up to the dynamic
5749 linker. We must do the same for executable references to
5750 shared library symbols, unless we've decided to use copy
5751 relocs or PLTs instead. */
5752 value = addend;
5753 if (!mips_elf_create_dynamic_relocation (abfd,
5754 info,
5755 relocation,
5756 h,
5757 sec,
5758 symbol,
5759 &value,
5760 input_section))
5761 return bfd_reloc_undefined;
5762 }
5763 else
5764 {
5765 if (r_type != R_MIPS_REL32)
5766 value = symbol + addend;
5767 else
5768 value = addend;
5769 }
5770 value &= howto->dst_mask;
5771 break;
5772
5773 case R_MIPS_PC32:
5774 value = symbol + addend - p;
5775 value &= howto->dst_mask;
5776 break;
5777
5778 case R_MIPS16_26:
5779 /* The calculation for R_MIPS16_26 is just the same as for an
5780 R_MIPS_26. It's only the storage of the relocated field into
5781 the output file that's different. That's handled in
5782 mips_elf_perform_relocation. So, we just fall through to the
5783 R_MIPS_26 case here. */
5784 case R_MIPS_26:
5785 case R_MICROMIPS_26_S1:
5786 {
5787 unsigned int shift;
5788
5789 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5790 the correct ISA mode selector and bit 1 must be 0. */
5791 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5792 return bfd_reloc_outofrange;
5793
5794 /* Shift is 2, unusually, for microMIPS JALX. */
5795 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5796
5797 if (was_local_p)
5798 value = addend | ((p + 4) & (0xfc000000 << shift));
5799 else if (howto->partial_inplace)
5800 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5801 else
5802 value = addend;
5803 value = (value + symbol) >> shift;
5804 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5805 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5806 value &= howto->dst_mask;
5807 }
5808 break;
5809
5810 case R_MIPS_TLS_DTPREL_HI16:
5811 case R_MIPS16_TLS_DTPREL_HI16:
5812 case R_MICROMIPS_TLS_DTPREL_HI16:
5813 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5814 & howto->dst_mask);
5815 break;
5816
5817 case R_MIPS_TLS_DTPREL_LO16:
5818 case R_MIPS_TLS_DTPREL32:
5819 case R_MIPS_TLS_DTPREL64:
5820 case R_MIPS16_TLS_DTPREL_LO16:
5821 case R_MICROMIPS_TLS_DTPREL_LO16:
5822 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5823 break;
5824
5825 case R_MIPS_TLS_TPREL_HI16:
5826 case R_MIPS16_TLS_TPREL_HI16:
5827 case R_MICROMIPS_TLS_TPREL_HI16:
5828 value = (mips_elf_high (addend + symbol - tprel_base (info))
5829 & howto->dst_mask);
5830 break;
5831
5832 case R_MIPS_TLS_TPREL_LO16:
5833 case R_MIPS_TLS_TPREL32:
5834 case R_MIPS_TLS_TPREL64:
5835 case R_MIPS16_TLS_TPREL_LO16:
5836 case R_MICROMIPS_TLS_TPREL_LO16:
5837 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5838 break;
5839
5840 case R_MIPS_HI16:
5841 case R_MIPS16_HI16:
5842 case R_MICROMIPS_HI16:
5843 if (!gp_disp_p)
5844 {
5845 value = mips_elf_high (addend + symbol);
5846 value &= howto->dst_mask;
5847 }
5848 else
5849 {
5850 /* For MIPS16 ABI code we generate this sequence
5851 0: li $v0,%hi(_gp_disp)
5852 4: addiupc $v1,%lo(_gp_disp)
5853 8: sll $v0,16
5854 12: addu $v0,$v1
5855 14: move $gp,$v0
5856 So the offsets of hi and lo relocs are the same, but the
5857 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5858 ADDIUPC clears the low two bits of the instruction address,
5859 so the base is ($t9 + 4) & ~3. */
5860 if (r_type == R_MIPS16_HI16)
5861 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
5862 /* The microMIPS .cpload sequence uses the same assembly
5863 instructions as the traditional psABI version, but the
5864 incoming $t9 has the low bit set. */
5865 else if (r_type == R_MICROMIPS_HI16)
5866 value = mips_elf_high (addend + gp - p - 1);
5867 else
5868 value = mips_elf_high (addend + gp - p);
5869 overflowed_p = mips_elf_overflow_p (value, 16);
5870 }
5871 break;
5872
5873 case R_MIPS_LO16:
5874 case R_MIPS16_LO16:
5875 case R_MICROMIPS_LO16:
5876 case R_MICROMIPS_HI0_LO16:
5877 if (!gp_disp_p)
5878 value = (symbol + addend) & howto->dst_mask;
5879 else
5880 {
5881 /* See the comment for R_MIPS16_HI16 above for the reason
5882 for this conditional. */
5883 if (r_type == R_MIPS16_LO16)
5884 value = addend + gp - (p & ~(bfd_vma) 0x3);
5885 else if (r_type == R_MICROMIPS_LO16
5886 || r_type == R_MICROMIPS_HI0_LO16)
5887 value = addend + gp - p + 3;
5888 else
5889 value = addend + gp - p + 4;
5890 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5891 for overflow. But, on, say, IRIX5, relocations against
5892 _gp_disp are normally generated from the .cpload
5893 pseudo-op. It generates code that normally looks like
5894 this:
5895
5896 lui $gp,%hi(_gp_disp)
5897 addiu $gp,$gp,%lo(_gp_disp)
5898 addu $gp,$gp,$t9
5899
5900 Here $t9 holds the address of the function being called,
5901 as required by the MIPS ELF ABI. The R_MIPS_LO16
5902 relocation can easily overflow in this situation, but the
5903 R_MIPS_HI16 relocation will handle the overflow.
5904 Therefore, we consider this a bug in the MIPS ABI, and do
5905 not check for overflow here. */
5906 }
5907 break;
5908
5909 case R_MIPS_LITERAL:
5910 case R_MICROMIPS_LITERAL:
5911 /* Because we don't merge literal sections, we can handle this
5912 just like R_MIPS_GPREL16. In the long run, we should merge
5913 shared literals, and then we will need to additional work
5914 here. */
5915
5916 /* Fall through. */
5917
5918 case R_MIPS16_GPREL:
5919 /* The R_MIPS16_GPREL performs the same calculation as
5920 R_MIPS_GPREL16, but stores the relocated bits in a different
5921 order. We don't need to do anything special here; the
5922 differences are handled in mips_elf_perform_relocation. */
5923 case R_MIPS_GPREL16:
5924 case R_MICROMIPS_GPREL7_S2:
5925 case R_MICROMIPS_GPREL16:
5926 /* Only sign-extend the addend if it was extracted from the
5927 instruction. If the addend was separate, leave it alone,
5928 otherwise we may lose significant bits. */
5929 if (howto->partial_inplace)
5930 addend = _bfd_mips_elf_sign_extend (addend, 16);
5931 value = symbol + addend - gp;
5932 /* If the symbol was local, any earlier relocatable links will
5933 have adjusted its addend with the gp offset, so compensate
5934 for that now. Don't do it for symbols forced local in this
5935 link, though, since they won't have had the gp offset applied
5936 to them before. */
5937 if (was_local_p)
5938 value += gp0;
5939 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
5940 overflowed_p = mips_elf_overflow_p (value, 16);
5941 break;
5942
5943 case R_MIPS16_GOT16:
5944 case R_MIPS16_CALL16:
5945 case R_MIPS_GOT16:
5946 case R_MIPS_CALL16:
5947 case R_MICROMIPS_GOT16:
5948 case R_MICROMIPS_CALL16:
5949 /* VxWorks does not have separate local and global semantics for
5950 R_MIPS*_GOT16; every relocation evaluates to "G". */
5951 if (!htab->is_vxworks && local_p)
5952 {
5953 value = mips_elf_got16_entry (abfd, input_bfd, info,
5954 symbol + addend, !was_local_p);
5955 if (value == MINUS_ONE)
5956 return bfd_reloc_outofrange;
5957 value
5958 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5959 overflowed_p = mips_elf_overflow_p (value, 16);
5960 break;
5961 }
5962
5963 /* Fall through. */
5964
5965 case R_MIPS_TLS_GD:
5966 case R_MIPS_TLS_GOTTPREL:
5967 case R_MIPS_TLS_LDM:
5968 case R_MIPS_GOT_DISP:
5969 case R_MIPS16_TLS_GD:
5970 case R_MIPS16_TLS_GOTTPREL:
5971 case R_MIPS16_TLS_LDM:
5972 case R_MICROMIPS_TLS_GD:
5973 case R_MICROMIPS_TLS_GOTTPREL:
5974 case R_MICROMIPS_TLS_LDM:
5975 case R_MICROMIPS_GOT_DISP:
5976 value = g;
5977 overflowed_p = mips_elf_overflow_p (value, 16);
5978 break;
5979
5980 case R_MIPS_GPREL32:
5981 value = (addend + symbol + gp0 - gp);
5982 if (!save_addend)
5983 value &= howto->dst_mask;
5984 break;
5985
5986 case R_MIPS_PC16:
5987 case R_MIPS_GNU_REL16_S2:
5988 if (howto->partial_inplace)
5989 addend = _bfd_mips_elf_sign_extend (addend, 18);
5990
5991 if ((symbol + addend) & 3)
5992 return bfd_reloc_outofrange;
5993
5994 value = symbol + addend - p;
5995 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
5996 overflowed_p = mips_elf_overflow_p (value, 18);
5997 value >>= howto->rightshift;
5998 value &= howto->dst_mask;
5999 break;
6000
6001 case R_MIPS_PC21_S2:
6002 if (howto->partial_inplace)
6003 addend = _bfd_mips_elf_sign_extend (addend, 23);
6004
6005 if ((symbol + addend) & 3)
6006 return bfd_reloc_outofrange;
6007
6008 value = symbol + addend - p;
6009 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6010 overflowed_p = mips_elf_overflow_p (value, 23);
6011 value >>= howto->rightshift;
6012 value &= howto->dst_mask;
6013 break;
6014
6015 case R_MIPS_PC26_S2:
6016 if (howto->partial_inplace)
6017 addend = _bfd_mips_elf_sign_extend (addend, 28);
6018
6019 if ((symbol + addend) & 3)
6020 return bfd_reloc_outofrange;
6021
6022 value = symbol + addend - p;
6023 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6024 overflowed_p = mips_elf_overflow_p (value, 28);
6025 value >>= howto->rightshift;
6026 value &= howto->dst_mask;
6027 break;
6028
6029 case R_MIPS_PC18_S3:
6030 if (howto->partial_inplace)
6031 addend = _bfd_mips_elf_sign_extend (addend, 21);
6032
6033 if ((symbol + addend) & 7)
6034 return bfd_reloc_outofrange;
6035
6036 value = symbol + addend - ((p | 7) ^ 7);
6037 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6038 overflowed_p = mips_elf_overflow_p (value, 21);
6039 value >>= howto->rightshift;
6040 value &= howto->dst_mask;
6041 break;
6042
6043 case R_MIPS_PC19_S2:
6044 if (howto->partial_inplace)
6045 addend = _bfd_mips_elf_sign_extend (addend, 21);
6046
6047 if ((symbol + addend) & 3)
6048 return bfd_reloc_outofrange;
6049
6050 value = symbol + addend - p;
6051 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6052 overflowed_p = mips_elf_overflow_p (value, 21);
6053 value >>= howto->rightshift;
6054 value &= howto->dst_mask;
6055 break;
6056
6057 case R_MIPS_PCHI16:
6058 value = mips_elf_high (symbol + addend - p);
6059 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6060 overflowed_p = mips_elf_overflow_p (value, 16);
6061 value &= howto->dst_mask;
6062 break;
6063
6064 case R_MIPS_PCLO16:
6065 if (howto->partial_inplace)
6066 addend = _bfd_mips_elf_sign_extend (addend, 16);
6067 value = symbol + addend - p;
6068 value &= howto->dst_mask;
6069 break;
6070
6071 case R_MICROMIPS_PC7_S1:
6072 if (howto->partial_inplace)
6073 addend = _bfd_mips_elf_sign_extend (addend, 8);
6074 value = symbol + addend - p;
6075 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6076 overflowed_p = mips_elf_overflow_p (value, 8);
6077 value >>= howto->rightshift;
6078 value &= howto->dst_mask;
6079 break;
6080
6081 case R_MICROMIPS_PC10_S1:
6082 if (howto->partial_inplace)
6083 addend = _bfd_mips_elf_sign_extend (addend, 11);
6084 value = symbol + addend - p;
6085 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6086 overflowed_p = mips_elf_overflow_p (value, 11);
6087 value >>= howto->rightshift;
6088 value &= howto->dst_mask;
6089 break;
6090
6091 case R_MICROMIPS_PC16_S1:
6092 if (howto->partial_inplace)
6093 addend = _bfd_mips_elf_sign_extend (addend, 17);
6094 value = symbol + addend - p;
6095 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6096 overflowed_p = mips_elf_overflow_p (value, 17);
6097 value >>= howto->rightshift;
6098 value &= howto->dst_mask;
6099 break;
6100
6101 case R_MICROMIPS_PC23_S2:
6102 if (howto->partial_inplace)
6103 addend = _bfd_mips_elf_sign_extend (addend, 25);
6104 value = symbol + addend - ((p | 3) ^ 3);
6105 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6106 overflowed_p = mips_elf_overflow_p (value, 25);
6107 value >>= howto->rightshift;
6108 value &= howto->dst_mask;
6109 break;
6110
6111 case R_MIPS_GOT_HI16:
6112 case R_MIPS_CALL_HI16:
6113 case R_MICROMIPS_GOT_HI16:
6114 case R_MICROMIPS_CALL_HI16:
6115 /* We're allowed to handle these two relocations identically.
6116 The dynamic linker is allowed to handle the CALL relocations
6117 differently by creating a lazy evaluation stub. */
6118 value = g;
6119 value = mips_elf_high (value);
6120 value &= howto->dst_mask;
6121 break;
6122
6123 case R_MIPS_GOT_LO16:
6124 case R_MIPS_CALL_LO16:
6125 case R_MICROMIPS_GOT_LO16:
6126 case R_MICROMIPS_CALL_LO16:
6127 value = g & howto->dst_mask;
6128 break;
6129
6130 case R_MIPS_GOT_PAGE:
6131 case R_MICROMIPS_GOT_PAGE:
6132 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
6133 if (value == MINUS_ONE)
6134 return bfd_reloc_outofrange;
6135 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
6136 overflowed_p = mips_elf_overflow_p (value, 16);
6137 break;
6138
6139 case R_MIPS_GOT_OFST:
6140 case R_MICROMIPS_GOT_OFST:
6141 if (local_p)
6142 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
6143 else
6144 value = addend;
6145 overflowed_p = mips_elf_overflow_p (value, 16);
6146 break;
6147
6148 case R_MIPS_SUB:
6149 case R_MICROMIPS_SUB:
6150 value = symbol - addend;
6151 value &= howto->dst_mask;
6152 break;
6153
6154 case R_MIPS_HIGHER:
6155 case R_MICROMIPS_HIGHER:
6156 value = mips_elf_higher (addend + symbol);
6157 value &= howto->dst_mask;
6158 break;
6159
6160 case R_MIPS_HIGHEST:
6161 case R_MICROMIPS_HIGHEST:
6162 value = mips_elf_highest (addend + symbol);
6163 value &= howto->dst_mask;
6164 break;
6165
6166 case R_MIPS_SCN_DISP:
6167 case R_MICROMIPS_SCN_DISP:
6168 value = symbol + addend - sec->output_offset;
6169 value &= howto->dst_mask;
6170 break;
6171
6172 case R_MIPS_JALR:
6173 case R_MICROMIPS_JALR:
6174 /* This relocation is only a hint. In some cases, we optimize
6175 it into a bal instruction. But we don't try to optimize
6176 when the symbol does not resolve locally. */
6177 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
6178 return bfd_reloc_continue;
6179 value = symbol + addend;
6180 break;
6181
6182 case R_MIPS_PJUMP:
6183 case R_MIPS_GNU_VTINHERIT:
6184 case R_MIPS_GNU_VTENTRY:
6185 /* We don't do anything with these at present. */
6186 return bfd_reloc_continue;
6187
6188 default:
6189 /* An unrecognized relocation type. */
6190 return bfd_reloc_notsupported;
6191 }
6192
6193 /* Store the VALUE for our caller. */
6194 *valuep = value;
6195 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6196 }
6197
6198 /* Obtain the field relocated by RELOCATION. */
6199
6200 static bfd_vma
mips_elf_obtain_contents(reloc_howto_type * howto,const Elf_Internal_Rela * relocation,bfd * input_bfd,bfd_byte * contents)6201 mips_elf_obtain_contents (reloc_howto_type *howto,
6202 const Elf_Internal_Rela *relocation,
6203 bfd *input_bfd, bfd_byte *contents)
6204 {
6205 bfd_vma x;
6206 bfd_byte *location = contents + relocation->r_offset;
6207
6208 /* Obtain the bytes. */
6209 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
6210
6211 return x;
6212 }
6213
6214 /* It has been determined that the result of the RELOCATION is the
6215 VALUE. Use HOWTO to place VALUE into the output file at the
6216 appropriate position. The SECTION is the section to which the
6217 relocation applies.
6218 CROSS_MODE_JUMP_P is true if the relocation field
6219 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6220
6221 Returns FALSE if anything goes wrong. */
6222
6223 static bfd_boolean
mips_elf_perform_relocation(struct bfd_link_info * info,reloc_howto_type * howto,const Elf_Internal_Rela * relocation,bfd_vma value,bfd * input_bfd,asection * input_section,bfd_byte * contents,bfd_boolean cross_mode_jump_p)6224 mips_elf_perform_relocation (struct bfd_link_info *info,
6225 reloc_howto_type *howto,
6226 const Elf_Internal_Rela *relocation,
6227 bfd_vma value, bfd *input_bfd,
6228 asection *input_section, bfd_byte *contents,
6229 bfd_boolean cross_mode_jump_p)
6230 {
6231 bfd_vma x;
6232 bfd_byte *location;
6233 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6234
6235 /* Figure out where the relocation is occurring. */
6236 location = contents + relocation->r_offset;
6237
6238 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
6239
6240 /* Obtain the current value. */
6241 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6242
6243 /* Clear the field we are setting. */
6244 x &= ~howto->dst_mask;
6245
6246 /* Set the field. */
6247 x |= (value & howto->dst_mask);
6248
6249 /* If required, turn JAL into JALX. */
6250 if (cross_mode_jump_p && jal_reloc_p (r_type))
6251 {
6252 bfd_boolean ok;
6253 bfd_vma opcode = x >> 26;
6254 bfd_vma jalx_opcode;
6255
6256 /* Check to see if the opcode is already JAL or JALX. */
6257 if (r_type == R_MIPS16_26)
6258 {
6259 ok = ((opcode == 0x6) || (opcode == 0x7));
6260 jalx_opcode = 0x7;
6261 }
6262 else if (r_type == R_MICROMIPS_26_S1)
6263 {
6264 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6265 jalx_opcode = 0x3c;
6266 }
6267 else
6268 {
6269 ok = ((opcode == 0x3) || (opcode == 0x1d));
6270 jalx_opcode = 0x1d;
6271 }
6272
6273 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6274 convert J or JALS to JALX. */
6275 if (!ok)
6276 {
6277 (*_bfd_error_handler)
6278 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
6279 input_bfd,
6280 input_section,
6281 (unsigned long) relocation->r_offset);
6282 bfd_set_error (bfd_error_bad_value);
6283 return FALSE;
6284 }
6285
6286 /* Make this the JALX opcode. */
6287 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6288 }
6289
6290 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6291 range. */
6292 if (!info->relocatable
6293 && !cross_mode_jump_p
6294 && ((JAL_TO_BAL_P (input_bfd)
6295 && r_type == R_MIPS_26
6296 && (x >> 26) == 0x3) /* jal addr */
6297 || (JALR_TO_BAL_P (input_bfd)
6298 && r_type == R_MIPS_JALR
6299 && x == 0x0320f809) /* jalr t9 */
6300 || (JR_TO_B_P (input_bfd)
6301 && r_type == R_MIPS_JALR
6302 && x == 0x03200008))) /* jr t9 */
6303 {
6304 bfd_vma addr;
6305 bfd_vma dest;
6306 bfd_signed_vma off;
6307
6308 addr = (input_section->output_section->vma
6309 + input_section->output_offset
6310 + relocation->r_offset
6311 + 4);
6312 if (r_type == R_MIPS_26)
6313 dest = (value << 2) | ((addr >> 28) << 28);
6314 else
6315 dest = value;
6316 off = dest - addr;
6317 if (off <= 0x1ffff && off >= -0x20000)
6318 {
6319 if (x == 0x03200008) /* jr t9 */
6320 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6321 else
6322 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6323 }
6324 }
6325
6326 /* Put the value into the output. */
6327 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6328
6329 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
6330 location);
6331
6332 return TRUE;
6333 }
6334
6335 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6336 is the original relocation, which is now being transformed into a
6337 dynamic relocation. The ADDENDP is adjusted if necessary; the
6338 caller should store the result in place of the original addend. */
6339
6340 static bfd_boolean
mips_elf_create_dynamic_relocation(bfd * output_bfd,struct bfd_link_info * info,const Elf_Internal_Rela * rel,struct mips_elf_link_hash_entry * h,asection * sec,bfd_vma symbol,bfd_vma * addendp,asection * input_section)6341 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6342 struct bfd_link_info *info,
6343 const Elf_Internal_Rela *rel,
6344 struct mips_elf_link_hash_entry *h,
6345 asection *sec, bfd_vma symbol,
6346 bfd_vma *addendp, asection *input_section)
6347 {
6348 Elf_Internal_Rela outrel[3];
6349 asection *sreloc;
6350 bfd *dynobj;
6351 int r_type;
6352 long indx;
6353 bfd_boolean defined_p;
6354 struct mips_elf_link_hash_table *htab;
6355
6356 htab = mips_elf_hash_table (info);
6357 BFD_ASSERT (htab != NULL);
6358
6359 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6360 dynobj = elf_hash_table (info)->dynobj;
6361 sreloc = mips_elf_rel_dyn_section (info, FALSE);
6362 BFD_ASSERT (sreloc != NULL);
6363 BFD_ASSERT (sreloc->contents != NULL);
6364 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6365 < sreloc->size);
6366
6367 outrel[0].r_offset =
6368 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6369 if (ABI_64_P (output_bfd))
6370 {
6371 outrel[1].r_offset =
6372 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6373 outrel[2].r_offset =
6374 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6375 }
6376
6377 if (outrel[0].r_offset == MINUS_ONE)
6378 /* The relocation field has been deleted. */
6379 return TRUE;
6380
6381 if (outrel[0].r_offset == MINUS_TWO)
6382 {
6383 /* The relocation field has been converted into a relative value of
6384 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6385 the field to be fully relocated, so add in the symbol's value. */
6386 *addendp += symbol;
6387 return TRUE;
6388 }
6389
6390 /* We must now calculate the dynamic symbol table index to use
6391 in the relocation. */
6392 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6393 {
6394 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
6395 indx = h->root.dynindx;
6396 if (SGI_COMPAT (output_bfd))
6397 defined_p = h->root.def_regular;
6398 else
6399 /* ??? glibc's ld.so just adds the final GOT entry to the
6400 relocation field. It therefore treats relocs against
6401 defined symbols in the same way as relocs against
6402 undefined symbols. */
6403 defined_p = FALSE;
6404 }
6405 else
6406 {
6407 if (sec != NULL && bfd_is_abs_section (sec))
6408 indx = 0;
6409 else if (sec == NULL || sec->owner == NULL)
6410 {
6411 bfd_set_error (bfd_error_bad_value);
6412 return FALSE;
6413 }
6414 else
6415 {
6416 indx = elf_section_data (sec->output_section)->dynindx;
6417 if (indx == 0)
6418 {
6419 asection *osec = htab->root.text_index_section;
6420 indx = elf_section_data (osec)->dynindx;
6421 }
6422 if (indx == 0)
6423 abort ();
6424 }
6425
6426 /* Instead of generating a relocation using the section
6427 symbol, we may as well make it a fully relative
6428 relocation. We want to avoid generating relocations to
6429 local symbols because we used to generate them
6430 incorrectly, without adding the original symbol value,
6431 which is mandated by the ABI for section symbols. In
6432 order to give dynamic loaders and applications time to
6433 phase out the incorrect use, we refrain from emitting
6434 section-relative relocations. It's not like they're
6435 useful, after all. This should be a bit more efficient
6436 as well. */
6437 /* ??? Although this behavior is compatible with glibc's ld.so,
6438 the ABI says that relocations against STN_UNDEF should have
6439 a symbol value of 0. Irix rld honors this, so relocations
6440 against STN_UNDEF have no effect. */
6441 if (!SGI_COMPAT (output_bfd))
6442 indx = 0;
6443 defined_p = TRUE;
6444 }
6445
6446 /* If the relocation was previously an absolute relocation and
6447 this symbol will not be referred to by the relocation, we must
6448 adjust it by the value we give it in the dynamic symbol table.
6449 Otherwise leave the job up to the dynamic linker. */
6450 if (defined_p && r_type != R_MIPS_REL32)
6451 *addendp += symbol;
6452
6453 if (htab->is_vxworks)
6454 /* VxWorks uses non-relative relocations for this. */
6455 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6456 else
6457 /* The relocation is always an REL32 relocation because we don't
6458 know where the shared library will wind up at load-time. */
6459 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6460 R_MIPS_REL32);
6461
6462 /* For strict adherence to the ABI specification, we should
6463 generate a R_MIPS_64 relocation record by itself before the
6464 _REL32/_64 record as well, such that the addend is read in as
6465 a 64-bit value (REL32 is a 32-bit relocation, after all).
6466 However, since none of the existing ELF64 MIPS dynamic
6467 loaders seems to care, we don't waste space with these
6468 artificial relocations. If this turns out to not be true,
6469 mips_elf_allocate_dynamic_relocation() should be tweaked so
6470 as to make room for a pair of dynamic relocations per
6471 invocation if ABI_64_P, and here we should generate an
6472 additional relocation record with R_MIPS_64 by itself for a
6473 NULL symbol before this relocation record. */
6474 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6475 ABI_64_P (output_bfd)
6476 ? R_MIPS_64
6477 : R_MIPS_NONE);
6478 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6479
6480 /* Adjust the output offset of the relocation to reference the
6481 correct location in the output file. */
6482 outrel[0].r_offset += (input_section->output_section->vma
6483 + input_section->output_offset);
6484 outrel[1].r_offset += (input_section->output_section->vma
6485 + input_section->output_offset);
6486 outrel[2].r_offset += (input_section->output_section->vma
6487 + input_section->output_offset);
6488
6489 /* Put the relocation back out. We have to use the special
6490 relocation outputter in the 64-bit case since the 64-bit
6491 relocation format is non-standard. */
6492 if (ABI_64_P (output_bfd))
6493 {
6494 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6495 (output_bfd, &outrel[0],
6496 (sreloc->contents
6497 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6498 }
6499 else if (htab->is_vxworks)
6500 {
6501 /* VxWorks uses RELA rather than REL dynamic relocations. */
6502 outrel[0].r_addend = *addendp;
6503 bfd_elf32_swap_reloca_out
6504 (output_bfd, &outrel[0],
6505 (sreloc->contents
6506 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6507 }
6508 else
6509 bfd_elf32_swap_reloc_out
6510 (output_bfd, &outrel[0],
6511 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6512
6513 /* We've now added another relocation. */
6514 ++sreloc->reloc_count;
6515
6516 /* Make sure the output section is writable. The dynamic linker
6517 will be writing to it. */
6518 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6519 |= SHF_WRITE;
6520
6521 /* On IRIX5, make an entry of compact relocation info. */
6522 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6523 {
6524 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6525 bfd_byte *cr;
6526
6527 if (scpt)
6528 {
6529 Elf32_crinfo cptrel;
6530
6531 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6532 cptrel.vaddr = (rel->r_offset
6533 + input_section->output_section->vma
6534 + input_section->output_offset);
6535 if (r_type == R_MIPS_REL32)
6536 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6537 else
6538 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6539 mips_elf_set_cr_dist2to (cptrel, 0);
6540 cptrel.konst = *addendp;
6541
6542 cr = (scpt->contents
6543 + sizeof (Elf32_External_compact_rel));
6544 mips_elf_set_cr_relvaddr (cptrel, 0);
6545 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6546 ((Elf32_External_crinfo *) cr
6547 + scpt->reloc_count));
6548 ++scpt->reloc_count;
6549 }
6550 }
6551
6552 /* If we've written this relocation for a readonly section,
6553 we need to set DF_TEXTREL again, so that we do not delete the
6554 DT_TEXTREL tag. */
6555 if (MIPS_ELF_READONLY_SECTION (input_section))
6556 info->flags |= DF_TEXTREL;
6557
6558 return TRUE;
6559 }
6560
6561 /* Return the MACH for a MIPS e_flags value. */
6562
6563 unsigned long
_bfd_elf_mips_mach(flagword flags)6564 _bfd_elf_mips_mach (flagword flags)
6565 {
6566 switch (flags & EF_MIPS_MACH)
6567 {
6568 case E_MIPS_MACH_3900:
6569 return bfd_mach_mips3900;
6570
6571 case E_MIPS_MACH_4010:
6572 return bfd_mach_mips4010;
6573
6574 case E_MIPS_MACH_4100:
6575 return bfd_mach_mips4100;
6576
6577 case E_MIPS_MACH_4111:
6578 return bfd_mach_mips4111;
6579
6580 case E_MIPS_MACH_4120:
6581 return bfd_mach_mips4120;
6582
6583 case E_MIPS_MACH_4650:
6584 return bfd_mach_mips4650;
6585
6586 case E_MIPS_MACH_5400:
6587 return bfd_mach_mips5400;
6588
6589 case E_MIPS_MACH_5500:
6590 return bfd_mach_mips5500;
6591
6592 case E_MIPS_MACH_5900:
6593 return bfd_mach_mips5900;
6594
6595 case E_MIPS_MACH_9000:
6596 return bfd_mach_mips9000;
6597
6598 case E_MIPS_MACH_SB1:
6599 return bfd_mach_mips_sb1;
6600
6601 case E_MIPS_MACH_LS2E:
6602 return bfd_mach_mips_loongson_2e;
6603
6604 case E_MIPS_MACH_LS2F:
6605 return bfd_mach_mips_loongson_2f;
6606
6607 case E_MIPS_MACH_LS3A:
6608 return bfd_mach_mips_loongson_3a;
6609
6610 case E_MIPS_MACH_OCTEON3:
6611 return bfd_mach_mips_octeon3;
6612
6613 case E_MIPS_MACH_OCTEON2:
6614 return bfd_mach_mips_octeon2;
6615
6616 case E_MIPS_MACH_OCTEON:
6617 return bfd_mach_mips_octeon;
6618
6619 case E_MIPS_MACH_XLR:
6620 return bfd_mach_mips_xlr;
6621
6622 default:
6623 switch (flags & EF_MIPS_ARCH)
6624 {
6625 default:
6626 case E_MIPS_ARCH_1:
6627 return bfd_mach_mips3000;
6628
6629 case E_MIPS_ARCH_2:
6630 return bfd_mach_mips6000;
6631
6632 case E_MIPS_ARCH_3:
6633 return bfd_mach_mips4000;
6634
6635 case E_MIPS_ARCH_4:
6636 return bfd_mach_mips8000;
6637
6638 case E_MIPS_ARCH_5:
6639 return bfd_mach_mips5;
6640
6641 case E_MIPS_ARCH_32:
6642 return bfd_mach_mipsisa32;
6643
6644 case E_MIPS_ARCH_64:
6645 return bfd_mach_mipsisa64;
6646
6647 case E_MIPS_ARCH_32R2:
6648 return bfd_mach_mipsisa32r2;
6649
6650 case E_MIPS_ARCH_64R2:
6651 return bfd_mach_mipsisa64r2;
6652
6653 case E_MIPS_ARCH_32R6:
6654 return bfd_mach_mipsisa32r6;
6655
6656 case E_MIPS_ARCH_64R6:
6657 return bfd_mach_mipsisa64r6;
6658 }
6659 }
6660
6661 return 0;
6662 }
6663
6664 /* Return printable name for ABI. */
6665
6666 static INLINE char *
elf_mips_abi_name(bfd * abfd)6667 elf_mips_abi_name (bfd *abfd)
6668 {
6669 flagword flags;
6670
6671 flags = elf_elfheader (abfd)->e_flags;
6672 switch (flags & EF_MIPS_ABI)
6673 {
6674 case 0:
6675 if (ABI_N32_P (abfd))
6676 return "N32";
6677 else if (ABI_64_P (abfd))
6678 return "64";
6679 else
6680 return "none";
6681 case E_MIPS_ABI_O32:
6682 return "O32";
6683 case E_MIPS_ABI_O64:
6684 return "O64";
6685 case E_MIPS_ABI_EABI32:
6686 return "EABI32";
6687 case E_MIPS_ABI_EABI64:
6688 return "EABI64";
6689 default:
6690 return "unknown abi";
6691 }
6692 }
6693
6694 /* MIPS ELF uses two common sections. One is the usual one, and the
6695 other is for small objects. All the small objects are kept
6696 together, and then referenced via the gp pointer, which yields
6697 faster assembler code. This is what we use for the small common
6698 section. This approach is copied from ecoff.c. */
6699 static asection mips_elf_scom_section;
6700 static asymbol mips_elf_scom_symbol;
6701 static asymbol *mips_elf_scom_symbol_ptr;
6702
6703 /* MIPS ELF also uses an acommon section, which represents an
6704 allocated common symbol which may be overridden by a
6705 definition in a shared library. */
6706 static asection mips_elf_acom_section;
6707 static asymbol mips_elf_acom_symbol;
6708 static asymbol *mips_elf_acom_symbol_ptr;
6709
6710 /* This is used for both the 32-bit and the 64-bit ABI. */
6711
6712 void
_bfd_mips_elf_symbol_processing(bfd * abfd,asymbol * asym)6713 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6714 {
6715 elf_symbol_type *elfsym;
6716
6717 /* Handle the special MIPS section numbers that a symbol may use. */
6718 elfsym = (elf_symbol_type *) asym;
6719 switch (elfsym->internal_elf_sym.st_shndx)
6720 {
6721 case SHN_MIPS_ACOMMON:
6722 /* This section is used in a dynamically linked executable file.
6723 It is an allocated common section. The dynamic linker can
6724 either resolve these symbols to something in a shared
6725 library, or it can just leave them here. For our purposes,
6726 we can consider these symbols to be in a new section. */
6727 if (mips_elf_acom_section.name == NULL)
6728 {
6729 /* Initialize the acommon section. */
6730 mips_elf_acom_section.name = ".acommon";
6731 mips_elf_acom_section.flags = SEC_ALLOC;
6732 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6733 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6734 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6735 mips_elf_acom_symbol.name = ".acommon";
6736 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6737 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6738 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6739 }
6740 asym->section = &mips_elf_acom_section;
6741 break;
6742
6743 case SHN_COMMON:
6744 /* Common symbols less than the GP size are automatically
6745 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6746 if (asym->value > elf_gp_size (abfd)
6747 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6748 || IRIX_COMPAT (abfd) == ict_irix6)
6749 break;
6750 /* Fall through. */
6751 case SHN_MIPS_SCOMMON:
6752 if (mips_elf_scom_section.name == NULL)
6753 {
6754 /* Initialize the small common section. */
6755 mips_elf_scom_section.name = ".scommon";
6756 mips_elf_scom_section.flags = SEC_IS_COMMON;
6757 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6758 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6759 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6760 mips_elf_scom_symbol.name = ".scommon";
6761 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6762 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6763 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6764 }
6765 asym->section = &mips_elf_scom_section;
6766 asym->value = elfsym->internal_elf_sym.st_size;
6767 break;
6768
6769 case SHN_MIPS_SUNDEFINED:
6770 asym->section = bfd_und_section_ptr;
6771 break;
6772
6773 case SHN_MIPS_TEXT:
6774 {
6775 asection *section = bfd_get_section_by_name (abfd, ".text");
6776
6777 if (section != NULL)
6778 {
6779 asym->section = section;
6780 /* MIPS_TEXT is a bit special, the address is not an offset
6781 to the base of the .text section. So substract the section
6782 base address to make it an offset. */
6783 asym->value -= section->vma;
6784 }
6785 }
6786 break;
6787
6788 case SHN_MIPS_DATA:
6789 {
6790 asection *section = bfd_get_section_by_name (abfd, ".data");
6791
6792 if (section != NULL)
6793 {
6794 asym->section = section;
6795 /* MIPS_DATA is a bit special, the address is not an offset
6796 to the base of the .data section. So substract the section
6797 base address to make it an offset. */
6798 asym->value -= section->vma;
6799 }
6800 }
6801 break;
6802 }
6803
6804 /* If this is an odd-valued function symbol, assume it's a MIPS16
6805 or microMIPS one. */
6806 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6807 && (asym->value & 1) != 0)
6808 {
6809 asym->value--;
6810 if (MICROMIPS_P (abfd))
6811 elfsym->internal_elf_sym.st_other
6812 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6813 else
6814 elfsym->internal_elf_sym.st_other
6815 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6816 }
6817 }
6818
6819 /* Implement elf_backend_eh_frame_address_size. This differs from
6820 the default in the way it handles EABI64.
6821
6822 EABI64 was originally specified as an LP64 ABI, and that is what
6823 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6824 historically accepted the combination of -mabi=eabi and -mlong32,
6825 and this ILP32 variation has become semi-official over time.
6826 Both forms use elf32 and have pointer-sized FDE addresses.
6827
6828 If an EABI object was generated by GCC 4.0 or above, it will have
6829 an empty .gcc_compiled_longXX section, where XX is the size of longs
6830 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6831 have no special marking to distinguish them from LP64 objects.
6832
6833 We don't want users of the official LP64 ABI to be punished for the
6834 existence of the ILP32 variant, but at the same time, we don't want
6835 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6836 We therefore take the following approach:
6837
6838 - If ABFD contains a .gcc_compiled_longXX section, use it to
6839 determine the pointer size.
6840
6841 - Otherwise check the type of the first relocation. Assume that
6842 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6843
6844 - Otherwise punt.
6845
6846 The second check is enough to detect LP64 objects generated by pre-4.0
6847 compilers because, in the kind of output generated by those compilers,
6848 the first relocation will be associated with either a CIE personality
6849 routine or an FDE start address. Furthermore, the compilers never
6850 used a special (non-pointer) encoding for this ABI.
6851
6852 Checking the relocation type should also be safe because there is no
6853 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6854 did so. */
6855
6856 unsigned int
_bfd_mips_elf_eh_frame_address_size(bfd * abfd,asection * sec)6857 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6858 {
6859 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6860 return 8;
6861 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6862 {
6863 bfd_boolean long32_p, long64_p;
6864
6865 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6866 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6867 if (long32_p && long64_p)
6868 return 0;
6869 if (long32_p)
6870 return 4;
6871 if (long64_p)
6872 return 8;
6873
6874 if (sec->reloc_count > 0
6875 && elf_section_data (sec)->relocs != NULL
6876 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6877 == R_MIPS_64))
6878 return 8;
6879
6880 return 0;
6881 }
6882 return 4;
6883 }
6884
6885 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6886 relocations against two unnamed section symbols to resolve to the
6887 same address. For example, if we have code like:
6888
6889 lw $4,%got_disp(.data)($gp)
6890 lw $25,%got_disp(.text)($gp)
6891 jalr $25
6892
6893 then the linker will resolve both relocations to .data and the program
6894 will jump there rather than to .text.
6895
6896 We can work around this problem by giving names to local section symbols.
6897 This is also what the MIPSpro tools do. */
6898
6899 bfd_boolean
_bfd_mips_elf_name_local_section_symbols(bfd * abfd)6900 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6901 {
6902 return SGI_COMPAT (abfd);
6903 }
6904
6905 /* Work over a section just before writing it out. This routine is
6906 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6907 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6908 a better way. */
6909
6910 bfd_boolean
_bfd_mips_elf_section_processing(bfd * abfd,Elf_Internal_Shdr * hdr)6911 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6912 {
6913 if (hdr->sh_type == SHT_MIPS_REGINFO
6914 && hdr->sh_size > 0)
6915 {
6916 bfd_byte buf[4];
6917
6918 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6919 BFD_ASSERT (hdr->contents == NULL);
6920
6921 if (bfd_seek (abfd,
6922 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6923 SEEK_SET) != 0)
6924 return FALSE;
6925 H_PUT_32 (abfd, elf_gp (abfd), buf);
6926 if (bfd_bwrite (buf, 4, abfd) != 4)
6927 return FALSE;
6928 }
6929
6930 if (hdr->sh_type == SHT_MIPS_OPTIONS
6931 && hdr->bfd_section != NULL
6932 && mips_elf_section_data (hdr->bfd_section) != NULL
6933 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6934 {
6935 bfd_byte *contents, *l, *lend;
6936
6937 /* We stored the section contents in the tdata field in the
6938 set_section_contents routine. We save the section contents
6939 so that we don't have to read them again.
6940 At this point we know that elf_gp is set, so we can look
6941 through the section contents to see if there is an
6942 ODK_REGINFO structure. */
6943
6944 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6945 l = contents;
6946 lend = contents + hdr->sh_size;
6947 while (l + sizeof (Elf_External_Options) <= lend)
6948 {
6949 Elf_Internal_Options intopt;
6950
6951 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6952 &intopt);
6953 if (intopt.size < sizeof (Elf_External_Options))
6954 {
6955 (*_bfd_error_handler)
6956 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6957 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6958 break;
6959 }
6960 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6961 {
6962 bfd_byte buf[8];
6963
6964 if (bfd_seek (abfd,
6965 (hdr->sh_offset
6966 + (l - contents)
6967 + sizeof (Elf_External_Options)
6968 + (sizeof (Elf64_External_RegInfo) - 8)),
6969 SEEK_SET) != 0)
6970 return FALSE;
6971 H_PUT_64 (abfd, elf_gp (abfd), buf);
6972 if (bfd_bwrite (buf, 8, abfd) != 8)
6973 return FALSE;
6974 }
6975 else if (intopt.kind == ODK_REGINFO)
6976 {
6977 bfd_byte buf[4];
6978
6979 if (bfd_seek (abfd,
6980 (hdr->sh_offset
6981 + (l - contents)
6982 + sizeof (Elf_External_Options)
6983 + (sizeof (Elf32_External_RegInfo) - 4)),
6984 SEEK_SET) != 0)
6985 return FALSE;
6986 H_PUT_32 (abfd, elf_gp (abfd), buf);
6987 if (bfd_bwrite (buf, 4, abfd) != 4)
6988 return FALSE;
6989 }
6990 l += intopt.size;
6991 }
6992 }
6993
6994 if (hdr->bfd_section != NULL)
6995 {
6996 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6997
6998 /* .sbss is not handled specially here because the GNU/Linux
6999 prelinker can convert .sbss from NOBITS to PROGBITS and
7000 changing it back to NOBITS breaks the binary. The entry in
7001 _bfd_mips_elf_special_sections will ensure the correct flags
7002 are set on .sbss if BFD creates it without reading it from an
7003 input file, and without special handling here the flags set
7004 on it in an input file will be followed. */
7005 if (strcmp (name, ".sdata") == 0
7006 || strcmp (name, ".lit8") == 0
7007 || strcmp (name, ".lit4") == 0)
7008 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
7009 else if (strcmp (name, ".srdata") == 0)
7010 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
7011 else if (strcmp (name, ".compact_rel") == 0)
7012 hdr->sh_flags = 0;
7013 else if (strcmp (name, ".rtproc") == 0)
7014 {
7015 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7016 {
7017 unsigned int adjust;
7018
7019 adjust = hdr->sh_size % hdr->sh_addralign;
7020 if (adjust != 0)
7021 hdr->sh_size += hdr->sh_addralign - adjust;
7022 }
7023 }
7024 }
7025
7026 return TRUE;
7027 }
7028
7029 /* Handle a MIPS specific section when reading an object file. This
7030 is called when elfcode.h finds a section with an unknown type.
7031 This routine supports both the 32-bit and 64-bit ELF ABI.
7032
7033 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
7034 how to. */
7035
7036 bfd_boolean
_bfd_mips_elf_section_from_shdr(bfd * abfd,Elf_Internal_Shdr * hdr,const char * name,int shindex)7037 _bfd_mips_elf_section_from_shdr (bfd *abfd,
7038 Elf_Internal_Shdr *hdr,
7039 const char *name,
7040 int shindex)
7041 {
7042 flagword flags = 0;
7043
7044 /* There ought to be a place to keep ELF backend specific flags, but
7045 at the moment there isn't one. We just keep track of the
7046 sections by their name, instead. Fortunately, the ABI gives
7047 suggested names for all the MIPS specific sections, so we will
7048 probably get away with this. */
7049 switch (hdr->sh_type)
7050 {
7051 case SHT_MIPS_LIBLIST:
7052 if (strcmp (name, ".liblist") != 0)
7053 return FALSE;
7054 break;
7055 case SHT_MIPS_MSYM:
7056 if (strcmp (name, ".msym") != 0)
7057 return FALSE;
7058 break;
7059 case SHT_MIPS_CONFLICT:
7060 if (strcmp (name, ".conflict") != 0)
7061 return FALSE;
7062 break;
7063 case SHT_MIPS_GPTAB:
7064 if (! CONST_STRNEQ (name, ".gptab."))
7065 return FALSE;
7066 break;
7067 case SHT_MIPS_UCODE:
7068 if (strcmp (name, ".ucode") != 0)
7069 return FALSE;
7070 break;
7071 case SHT_MIPS_DEBUG:
7072 if (strcmp (name, ".mdebug") != 0)
7073 return FALSE;
7074 flags = SEC_DEBUGGING;
7075 break;
7076 case SHT_MIPS_REGINFO:
7077 if (strcmp (name, ".reginfo") != 0
7078 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
7079 return FALSE;
7080 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7081 break;
7082 case SHT_MIPS_IFACE:
7083 if (strcmp (name, ".MIPS.interfaces") != 0)
7084 return FALSE;
7085 break;
7086 case SHT_MIPS_CONTENT:
7087 if (! CONST_STRNEQ (name, ".MIPS.content"))
7088 return FALSE;
7089 break;
7090 case SHT_MIPS_OPTIONS:
7091 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7092 return FALSE;
7093 break;
7094 case SHT_MIPS_ABIFLAGS:
7095 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7096 return FALSE;
7097 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7098 break;
7099 case SHT_MIPS_DWARF:
7100 if (! CONST_STRNEQ (name, ".debug_")
7101 && ! CONST_STRNEQ (name, ".zdebug_"))
7102 return FALSE;
7103 break;
7104 case SHT_MIPS_SYMBOL_LIB:
7105 if (strcmp (name, ".MIPS.symlib") != 0)
7106 return FALSE;
7107 break;
7108 case SHT_MIPS_EVENTS:
7109 if (! CONST_STRNEQ (name, ".MIPS.events")
7110 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
7111 return FALSE;
7112 break;
7113 default:
7114 break;
7115 }
7116
7117 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
7118 return FALSE;
7119
7120 if (flags)
7121 {
7122 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
7123 (bfd_get_section_flags (abfd,
7124 hdr->bfd_section)
7125 | flags)))
7126 return FALSE;
7127 }
7128
7129 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7130 {
7131 Elf_External_ABIFlags_v0 ext;
7132
7133 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7134 &ext, 0, sizeof ext))
7135 return FALSE;
7136 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7137 &mips_elf_tdata (abfd)->abiflags);
7138 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7139 return FALSE;
7140 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
7141 }
7142
7143 /* FIXME: We should record sh_info for a .gptab section. */
7144
7145 /* For a .reginfo section, set the gp value in the tdata information
7146 from the contents of this section. We need the gp value while
7147 processing relocs, so we just get it now. The .reginfo section
7148 is not used in the 64-bit MIPS ELF ABI. */
7149 if (hdr->sh_type == SHT_MIPS_REGINFO)
7150 {
7151 Elf32_External_RegInfo ext;
7152 Elf32_RegInfo s;
7153
7154 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7155 &ext, 0, sizeof ext))
7156 return FALSE;
7157 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7158 elf_gp (abfd) = s.ri_gp_value;
7159 }
7160
7161 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7162 set the gp value based on what we find. We may see both
7163 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7164 they should agree. */
7165 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7166 {
7167 bfd_byte *contents, *l, *lend;
7168
7169 contents = bfd_malloc (hdr->sh_size);
7170 if (contents == NULL)
7171 return FALSE;
7172 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
7173 0, hdr->sh_size))
7174 {
7175 free (contents);
7176 return FALSE;
7177 }
7178 l = contents;
7179 lend = contents + hdr->sh_size;
7180 while (l + sizeof (Elf_External_Options) <= lend)
7181 {
7182 Elf_Internal_Options intopt;
7183
7184 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7185 &intopt);
7186 if (intopt.size < sizeof (Elf_External_Options))
7187 {
7188 (*_bfd_error_handler)
7189 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
7190 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7191 break;
7192 }
7193 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7194 {
7195 Elf64_Internal_RegInfo intreg;
7196
7197 bfd_mips_elf64_swap_reginfo_in
7198 (abfd,
7199 ((Elf64_External_RegInfo *)
7200 (l + sizeof (Elf_External_Options))),
7201 &intreg);
7202 elf_gp (abfd) = intreg.ri_gp_value;
7203 }
7204 else if (intopt.kind == ODK_REGINFO)
7205 {
7206 Elf32_RegInfo intreg;
7207
7208 bfd_mips_elf32_swap_reginfo_in
7209 (abfd,
7210 ((Elf32_External_RegInfo *)
7211 (l + sizeof (Elf_External_Options))),
7212 &intreg);
7213 elf_gp (abfd) = intreg.ri_gp_value;
7214 }
7215 l += intopt.size;
7216 }
7217 free (contents);
7218 }
7219
7220 return TRUE;
7221 }
7222
7223 /* Set the correct type for a MIPS ELF section. We do this by the
7224 section name, which is a hack, but ought to work. This routine is
7225 used by both the 32-bit and the 64-bit ABI. */
7226
7227 bfd_boolean
_bfd_mips_elf_fake_sections(bfd * abfd,Elf_Internal_Shdr * hdr,asection * sec)7228 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7229 {
7230 const char *name = bfd_get_section_name (abfd, sec);
7231
7232 if (strcmp (name, ".liblist") == 0)
7233 {
7234 hdr->sh_type = SHT_MIPS_LIBLIST;
7235 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7236 /* The sh_link field is set in final_write_processing. */
7237 }
7238 else if (strcmp (name, ".conflict") == 0)
7239 hdr->sh_type = SHT_MIPS_CONFLICT;
7240 else if (CONST_STRNEQ (name, ".gptab."))
7241 {
7242 hdr->sh_type = SHT_MIPS_GPTAB;
7243 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7244 /* The sh_info field is set in final_write_processing. */
7245 }
7246 else if (strcmp (name, ".ucode") == 0)
7247 hdr->sh_type = SHT_MIPS_UCODE;
7248 else if (strcmp (name, ".mdebug") == 0)
7249 {
7250 hdr->sh_type = SHT_MIPS_DEBUG;
7251 /* In a shared object on IRIX 5.3, the .mdebug section has an
7252 entsize of 0. FIXME: Does this matter? */
7253 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7254 hdr->sh_entsize = 0;
7255 else
7256 hdr->sh_entsize = 1;
7257 }
7258 else if (strcmp (name, ".reginfo") == 0)
7259 {
7260 hdr->sh_type = SHT_MIPS_REGINFO;
7261 /* In a shared object on IRIX 5.3, the .reginfo section has an
7262 entsize of 0x18. FIXME: Does this matter? */
7263 if (SGI_COMPAT (abfd))
7264 {
7265 if ((abfd->flags & DYNAMIC) != 0)
7266 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7267 else
7268 hdr->sh_entsize = 1;
7269 }
7270 else
7271 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7272 }
7273 else if (SGI_COMPAT (abfd)
7274 && (strcmp (name, ".hash") == 0
7275 || strcmp (name, ".dynamic") == 0
7276 || strcmp (name, ".dynstr") == 0))
7277 {
7278 if (SGI_COMPAT (abfd))
7279 hdr->sh_entsize = 0;
7280 #if 0
7281 /* This isn't how the IRIX6 linker behaves. */
7282 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7283 #endif
7284 }
7285 else if (strcmp (name, ".got") == 0
7286 || strcmp (name, ".srdata") == 0
7287 || strcmp (name, ".sdata") == 0
7288 || strcmp (name, ".sbss") == 0
7289 || strcmp (name, ".lit4") == 0
7290 || strcmp (name, ".lit8") == 0)
7291 hdr->sh_flags |= SHF_MIPS_GPREL;
7292 else if (strcmp (name, ".MIPS.interfaces") == 0)
7293 {
7294 hdr->sh_type = SHT_MIPS_IFACE;
7295 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7296 }
7297 else if (CONST_STRNEQ (name, ".MIPS.content"))
7298 {
7299 hdr->sh_type = SHT_MIPS_CONTENT;
7300 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7301 /* The sh_info field is set in final_write_processing. */
7302 }
7303 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7304 {
7305 hdr->sh_type = SHT_MIPS_OPTIONS;
7306 hdr->sh_entsize = 1;
7307 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7308 }
7309 else if (CONST_STRNEQ (name, ".MIPS.abiflags"))
7310 {
7311 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7312 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7313 }
7314 else if (CONST_STRNEQ (name, ".debug_")
7315 || CONST_STRNEQ (name, ".zdebug_"))
7316 {
7317 hdr->sh_type = SHT_MIPS_DWARF;
7318
7319 /* Irix facilities such as libexc expect a single .debug_frame
7320 per executable, the system ones have NOSTRIP set and the linker
7321 doesn't merge sections with different flags so ... */
7322 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7323 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7324 }
7325 else if (strcmp (name, ".MIPS.symlib") == 0)
7326 {
7327 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7328 /* The sh_link and sh_info fields are set in
7329 final_write_processing. */
7330 }
7331 else if (CONST_STRNEQ (name, ".MIPS.events")
7332 || CONST_STRNEQ (name, ".MIPS.post_rel"))
7333 {
7334 hdr->sh_type = SHT_MIPS_EVENTS;
7335 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7336 /* The sh_link field is set in final_write_processing. */
7337 }
7338 else if (strcmp (name, ".msym") == 0)
7339 {
7340 hdr->sh_type = SHT_MIPS_MSYM;
7341 hdr->sh_flags |= SHF_ALLOC;
7342 hdr->sh_entsize = 8;
7343 }
7344
7345 /* The generic elf_fake_sections will set up REL_HDR using the default
7346 kind of relocations. We used to set up a second header for the
7347 non-default kind of relocations here, but only NewABI would use
7348 these, and the IRIX ld doesn't like resulting empty RELA sections.
7349 Thus we create those header only on demand now. */
7350
7351 return TRUE;
7352 }
7353
7354 /* Given a BFD section, try to locate the corresponding ELF section
7355 index. This is used by both the 32-bit and the 64-bit ABI.
7356 Actually, it's not clear to me that the 64-bit ABI supports these,
7357 but for non-PIC objects we will certainly want support for at least
7358 the .scommon section. */
7359
7360 bfd_boolean
_bfd_mips_elf_section_from_bfd_section(bfd * abfd ATTRIBUTE_UNUSED,asection * sec,int * retval)7361 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7362 asection *sec, int *retval)
7363 {
7364 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7365 {
7366 *retval = SHN_MIPS_SCOMMON;
7367 return TRUE;
7368 }
7369 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7370 {
7371 *retval = SHN_MIPS_ACOMMON;
7372 return TRUE;
7373 }
7374 return FALSE;
7375 }
7376
7377 /* Hook called by the linker routine which adds symbols from an object
7378 file. We must handle the special MIPS section numbers here. */
7379
7380 bfd_boolean
_bfd_mips_elf_add_symbol_hook(bfd * abfd,struct bfd_link_info * info,Elf_Internal_Sym * sym,const char ** namep,flagword * flagsp ATTRIBUTE_UNUSED,asection ** secp,bfd_vma * valp)7381 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7382 Elf_Internal_Sym *sym, const char **namep,
7383 flagword *flagsp ATTRIBUTE_UNUSED,
7384 asection **secp, bfd_vma *valp)
7385 {
7386 if (SGI_COMPAT (abfd)
7387 && (abfd->flags & DYNAMIC) != 0
7388 && strcmp (*namep, "_rld_new_interface") == 0)
7389 {
7390 /* Skip IRIX5 rld entry name. */
7391 *namep = NULL;
7392 return TRUE;
7393 }
7394
7395 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7396 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7397 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7398 a magic symbol resolved by the linker, we ignore this bogus definition
7399 of _gp_disp. New ABI objects do not suffer from this problem so this
7400 is not done for them. */
7401 if (!NEWABI_P(abfd)
7402 && (sym->st_shndx == SHN_ABS)
7403 && (strcmp (*namep, "_gp_disp") == 0))
7404 {
7405 *namep = NULL;
7406 return TRUE;
7407 }
7408
7409 switch (sym->st_shndx)
7410 {
7411 case SHN_COMMON:
7412 /* Common symbols less than the GP size are automatically
7413 treated as SHN_MIPS_SCOMMON symbols. */
7414 if (sym->st_size > elf_gp_size (abfd)
7415 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7416 || IRIX_COMPAT (abfd) == ict_irix6)
7417 break;
7418 /* Fall through. */
7419 case SHN_MIPS_SCOMMON:
7420 *secp = bfd_make_section_old_way (abfd, ".scommon");
7421 (*secp)->flags |= SEC_IS_COMMON;
7422 *valp = sym->st_size;
7423 break;
7424
7425 case SHN_MIPS_TEXT:
7426 /* This section is used in a shared object. */
7427 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7428 {
7429 asymbol *elf_text_symbol;
7430 asection *elf_text_section;
7431 bfd_size_type amt = sizeof (asection);
7432
7433 elf_text_section = bfd_zalloc (abfd, amt);
7434 if (elf_text_section == NULL)
7435 return FALSE;
7436
7437 amt = sizeof (asymbol);
7438 elf_text_symbol = bfd_zalloc (abfd, amt);
7439 if (elf_text_symbol == NULL)
7440 return FALSE;
7441
7442 /* Initialize the section. */
7443
7444 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7445 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7446
7447 elf_text_section->symbol = elf_text_symbol;
7448 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7449
7450 elf_text_section->name = ".text";
7451 elf_text_section->flags = SEC_NO_FLAGS;
7452 elf_text_section->output_section = NULL;
7453 elf_text_section->owner = abfd;
7454 elf_text_symbol->name = ".text";
7455 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7456 elf_text_symbol->section = elf_text_section;
7457 }
7458 /* This code used to do *secp = bfd_und_section_ptr if
7459 info->shared. I don't know why, and that doesn't make sense,
7460 so I took it out. */
7461 *secp = mips_elf_tdata (abfd)->elf_text_section;
7462 break;
7463
7464 case SHN_MIPS_ACOMMON:
7465 /* Fall through. XXX Can we treat this as allocated data? */
7466 case SHN_MIPS_DATA:
7467 /* This section is used in a shared object. */
7468 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7469 {
7470 asymbol *elf_data_symbol;
7471 asection *elf_data_section;
7472 bfd_size_type amt = sizeof (asection);
7473
7474 elf_data_section = bfd_zalloc (abfd, amt);
7475 if (elf_data_section == NULL)
7476 return FALSE;
7477
7478 amt = sizeof (asymbol);
7479 elf_data_symbol = bfd_zalloc (abfd, amt);
7480 if (elf_data_symbol == NULL)
7481 return FALSE;
7482
7483 /* Initialize the section. */
7484
7485 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7486 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7487
7488 elf_data_section->symbol = elf_data_symbol;
7489 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7490
7491 elf_data_section->name = ".data";
7492 elf_data_section->flags = SEC_NO_FLAGS;
7493 elf_data_section->output_section = NULL;
7494 elf_data_section->owner = abfd;
7495 elf_data_symbol->name = ".data";
7496 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7497 elf_data_symbol->section = elf_data_section;
7498 }
7499 /* This code used to do *secp = bfd_und_section_ptr if
7500 info->shared. I don't know why, and that doesn't make sense,
7501 so I took it out. */
7502 *secp = mips_elf_tdata (abfd)->elf_data_section;
7503 break;
7504
7505 case SHN_MIPS_SUNDEFINED:
7506 *secp = bfd_und_section_ptr;
7507 break;
7508 }
7509
7510 if (SGI_COMPAT (abfd)
7511 && ! info->shared
7512 && info->output_bfd->xvec == abfd->xvec
7513 && strcmp (*namep, "__rld_obj_head") == 0)
7514 {
7515 struct elf_link_hash_entry *h;
7516 struct bfd_link_hash_entry *bh;
7517
7518 /* Mark __rld_obj_head as dynamic. */
7519 bh = NULL;
7520 if (! (_bfd_generic_link_add_one_symbol
7521 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7522 get_elf_backend_data (abfd)->collect, &bh)))
7523 return FALSE;
7524
7525 h = (struct elf_link_hash_entry *) bh;
7526 h->non_elf = 0;
7527 h->def_regular = 1;
7528 h->type = STT_OBJECT;
7529
7530 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7531 return FALSE;
7532
7533 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7534 mips_elf_hash_table (info)->rld_symbol = h;
7535 }
7536
7537 /* If this is a mips16 text symbol, add 1 to the value to make it
7538 odd. This will cause something like .word SYM to come up with
7539 the right value when it is loaded into the PC. */
7540 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7541 ++*valp;
7542
7543 return TRUE;
7544 }
7545
7546 /* This hook function is called before the linker writes out a global
7547 symbol. We mark symbols as small common if appropriate. This is
7548 also where we undo the increment of the value for a mips16 symbol. */
7549
7550 int
_bfd_mips_elf_link_output_symbol_hook(struct bfd_link_info * info ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,Elf_Internal_Sym * sym,asection * input_sec,struct elf_link_hash_entry * h ATTRIBUTE_UNUSED)7551 _bfd_mips_elf_link_output_symbol_hook
7552 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7553 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7554 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7555 {
7556 /* If we see a common symbol, which implies a relocatable link, then
7557 if a symbol was small common in an input file, mark it as small
7558 common in the output file. */
7559 if (sym->st_shndx == SHN_COMMON
7560 && strcmp (input_sec->name, ".scommon") == 0)
7561 sym->st_shndx = SHN_MIPS_SCOMMON;
7562
7563 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7564 sym->st_value &= ~1;
7565
7566 return 1;
7567 }
7568
7569 /* Functions for the dynamic linker. */
7570
7571 /* Create dynamic sections when linking against a dynamic object. */
7572
7573 bfd_boolean
_bfd_mips_elf_create_dynamic_sections(bfd * abfd,struct bfd_link_info * info)7574 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7575 {
7576 struct elf_link_hash_entry *h;
7577 struct bfd_link_hash_entry *bh;
7578 flagword flags;
7579 register asection *s;
7580 const char * const *namep;
7581 struct mips_elf_link_hash_table *htab;
7582
7583 htab = mips_elf_hash_table (info);
7584 BFD_ASSERT (htab != NULL);
7585
7586 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7587 | SEC_LINKER_CREATED | SEC_READONLY);
7588
7589 /* The psABI requires a read-only .dynamic section, but the VxWorks
7590 EABI doesn't. */
7591 if (!htab->is_vxworks)
7592 {
7593 s = bfd_get_linker_section (abfd, ".dynamic");
7594 if (s != NULL)
7595 {
7596 if (! bfd_set_section_flags (abfd, s, flags))
7597 return FALSE;
7598 }
7599 }
7600
7601 /* We need to create .got section. */
7602 if (!mips_elf_create_got_section (abfd, info))
7603 return FALSE;
7604
7605 if (! mips_elf_rel_dyn_section (info, TRUE))
7606 return FALSE;
7607
7608 /* Create .stub section. */
7609 s = bfd_make_section_anyway_with_flags (abfd,
7610 MIPS_ELF_STUB_SECTION_NAME (abfd),
7611 flags | SEC_CODE);
7612 if (s == NULL
7613 || ! bfd_set_section_alignment (abfd, s,
7614 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7615 return FALSE;
7616 htab->sstubs = s;
7617
7618 if (!mips_elf_hash_table (info)->use_rld_obj_head
7619 && info->executable
7620 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
7621 {
7622 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7623 flags &~ (flagword) SEC_READONLY);
7624 if (s == NULL
7625 || ! bfd_set_section_alignment (abfd, s,
7626 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7627 return FALSE;
7628 }
7629
7630 /* On IRIX5, we adjust add some additional symbols and change the
7631 alignments of several sections. There is no ABI documentation
7632 indicating that this is necessary on IRIX6, nor any evidence that
7633 the linker takes such action. */
7634 if (IRIX_COMPAT (abfd) == ict_irix5)
7635 {
7636 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7637 {
7638 bh = NULL;
7639 if (! (_bfd_generic_link_add_one_symbol
7640 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7641 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7642 return FALSE;
7643
7644 h = (struct elf_link_hash_entry *) bh;
7645 h->non_elf = 0;
7646 h->def_regular = 1;
7647 h->type = STT_SECTION;
7648
7649 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7650 return FALSE;
7651 }
7652
7653 /* We need to create a .compact_rel section. */
7654 if (SGI_COMPAT (abfd))
7655 {
7656 if (!mips_elf_create_compact_rel_section (abfd, info))
7657 return FALSE;
7658 }
7659
7660 /* Change alignments of some sections. */
7661 s = bfd_get_linker_section (abfd, ".hash");
7662 if (s != NULL)
7663 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7664
7665 s = bfd_get_linker_section (abfd, ".dynsym");
7666 if (s != NULL)
7667 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7668
7669 s = bfd_get_linker_section (abfd, ".dynstr");
7670 if (s != NULL)
7671 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7672
7673 /* ??? */
7674 s = bfd_get_section_by_name (abfd, ".reginfo");
7675 if (s != NULL)
7676 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7677
7678 s = bfd_get_linker_section (abfd, ".dynamic");
7679 if (s != NULL)
7680 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7681 }
7682
7683 if (info->executable)
7684 {
7685 const char *name;
7686
7687 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7688 bh = NULL;
7689 if (!(_bfd_generic_link_add_one_symbol
7690 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7691 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7692 return FALSE;
7693
7694 h = (struct elf_link_hash_entry *) bh;
7695 h->non_elf = 0;
7696 h->def_regular = 1;
7697 h->type = STT_SECTION;
7698
7699 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7700 return FALSE;
7701
7702 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7703 {
7704 /* __rld_map is a four byte word located in the .data section
7705 and is filled in by the rtld to contain a pointer to
7706 the _r_debug structure. Its symbol value will be set in
7707 _bfd_mips_elf_finish_dynamic_symbol. */
7708 s = bfd_get_linker_section (abfd, ".rld_map");
7709 BFD_ASSERT (s != NULL);
7710
7711 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7712 bh = NULL;
7713 if (!(_bfd_generic_link_add_one_symbol
7714 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7715 get_elf_backend_data (abfd)->collect, &bh)))
7716 return FALSE;
7717
7718 h = (struct elf_link_hash_entry *) bh;
7719 h->non_elf = 0;
7720 h->def_regular = 1;
7721 h->type = STT_OBJECT;
7722
7723 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7724 return FALSE;
7725 mips_elf_hash_table (info)->rld_symbol = h;
7726 }
7727 }
7728
7729 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7730 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
7731 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7732 return FALSE;
7733
7734 /* Cache the sections created above. */
7735 htab->splt = bfd_get_linker_section (abfd, ".plt");
7736 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
7737 if (htab->is_vxworks)
7738 {
7739 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7740 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
7741 }
7742 else
7743 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
7744 if (!htab->sdynbss
7745 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7746 || !htab->srelplt
7747 || !htab->splt)
7748 abort ();
7749
7750 /* Do the usual VxWorks handling. */
7751 if (htab->is_vxworks
7752 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7753 return FALSE;
7754
7755 return TRUE;
7756 }
7757
7758 /* Return true if relocation REL against section SEC is a REL rather than
7759 RELA relocation. RELOCS is the first relocation in the section and
7760 ABFD is the bfd that contains SEC. */
7761
7762 static bfd_boolean
mips_elf_rel_relocation_p(bfd * abfd,asection * sec,const Elf_Internal_Rela * relocs,const Elf_Internal_Rela * rel)7763 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7764 const Elf_Internal_Rela *relocs,
7765 const Elf_Internal_Rela *rel)
7766 {
7767 Elf_Internal_Shdr *rel_hdr;
7768 const struct elf_backend_data *bed;
7769
7770 /* To determine which flavor of relocation this is, we depend on the
7771 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7772 rel_hdr = elf_section_data (sec)->rel.hdr;
7773 if (rel_hdr == NULL)
7774 return FALSE;
7775 bed = get_elf_backend_data (abfd);
7776 return ((size_t) (rel - relocs)
7777 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7778 }
7779
7780 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7781 HOWTO is the relocation's howto and CONTENTS points to the contents
7782 of the section that REL is against. */
7783
7784 static bfd_vma
mips_elf_read_rel_addend(bfd * abfd,const Elf_Internal_Rela * rel,reloc_howto_type * howto,bfd_byte * contents)7785 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7786 reloc_howto_type *howto, bfd_byte *contents)
7787 {
7788 bfd_byte *location;
7789 unsigned int r_type;
7790 bfd_vma addend;
7791
7792 r_type = ELF_R_TYPE (abfd, rel->r_info);
7793 location = contents + rel->r_offset;
7794
7795 /* Get the addend, which is stored in the input file. */
7796 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7797 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7798 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7799
7800 return addend & howto->src_mask;
7801 }
7802
7803 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7804 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7805 and update *ADDEND with the final addend. Return true on success
7806 or false if the LO16 could not be found. RELEND is the exclusive
7807 upper bound on the relocations for REL's section. */
7808
7809 static bfd_boolean
mips_elf_add_lo16_rel_addend(bfd * abfd,const Elf_Internal_Rela * rel,const Elf_Internal_Rela * relend,bfd_byte * contents,bfd_vma * addend)7810 mips_elf_add_lo16_rel_addend (bfd *abfd,
7811 const Elf_Internal_Rela *rel,
7812 const Elf_Internal_Rela *relend,
7813 bfd_byte *contents, bfd_vma *addend)
7814 {
7815 unsigned int r_type, lo16_type;
7816 const Elf_Internal_Rela *lo16_relocation;
7817 reloc_howto_type *lo16_howto;
7818 bfd_vma l;
7819
7820 r_type = ELF_R_TYPE (abfd, rel->r_info);
7821 if (mips16_reloc_p (r_type))
7822 lo16_type = R_MIPS16_LO16;
7823 else if (micromips_reloc_p (r_type))
7824 lo16_type = R_MICROMIPS_LO16;
7825 else if (r_type == R_MIPS_PCHI16)
7826 lo16_type = R_MIPS_PCLO16;
7827 else
7828 lo16_type = R_MIPS_LO16;
7829
7830 /* The combined value is the sum of the HI16 addend, left-shifted by
7831 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7832 code does a `lui' of the HI16 value, and then an `addiu' of the
7833 LO16 value.)
7834
7835 Scan ahead to find a matching LO16 relocation.
7836
7837 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7838 be immediately following. However, for the IRIX6 ABI, the next
7839 relocation may be a composed relocation consisting of several
7840 relocations for the same address. In that case, the R_MIPS_LO16
7841 relocation may occur as one of these. We permit a similar
7842 extension in general, as that is useful for GCC.
7843
7844 In some cases GCC dead code elimination removes the LO16 but keeps
7845 the corresponding HI16. This is strictly speaking a violation of
7846 the ABI but not immediately harmful. */
7847 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7848 if (lo16_relocation == NULL)
7849 return FALSE;
7850
7851 /* Obtain the addend kept there. */
7852 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7853 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7854
7855 l <<= lo16_howto->rightshift;
7856 l = _bfd_mips_elf_sign_extend (l, 16);
7857
7858 *addend <<= 16;
7859 *addend += l;
7860 return TRUE;
7861 }
7862
7863 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7864 store the contents in *CONTENTS on success. Assume that *CONTENTS
7865 already holds the contents if it is nonull on entry. */
7866
7867 static bfd_boolean
mips_elf_get_section_contents(bfd * abfd,asection * sec,bfd_byte ** contents)7868 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7869 {
7870 if (*contents)
7871 return TRUE;
7872
7873 /* Get cached copy if it exists. */
7874 if (elf_section_data (sec)->this_hdr.contents != NULL)
7875 {
7876 *contents = elf_section_data (sec)->this_hdr.contents;
7877 return TRUE;
7878 }
7879
7880 return bfd_malloc_and_get_section (abfd, sec, contents);
7881 }
7882
7883 /* Make a new PLT record to keep internal data. */
7884
7885 static struct plt_entry *
mips_elf_make_plt_record(bfd * abfd)7886 mips_elf_make_plt_record (bfd *abfd)
7887 {
7888 struct plt_entry *entry;
7889
7890 entry = bfd_zalloc (abfd, sizeof (*entry));
7891 if (entry == NULL)
7892 return NULL;
7893
7894 entry->stub_offset = MINUS_ONE;
7895 entry->mips_offset = MINUS_ONE;
7896 entry->comp_offset = MINUS_ONE;
7897 entry->gotplt_index = MINUS_ONE;
7898 return entry;
7899 }
7900
7901 /* Look through the relocs for a section during the first phase, and
7902 allocate space in the global offset table and record the need for
7903 standard MIPS and compressed procedure linkage table entries. */
7904
7905 bfd_boolean
_bfd_mips_elf_check_relocs(bfd * abfd,struct bfd_link_info * info,asection * sec,const Elf_Internal_Rela * relocs)7906 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7907 asection *sec, const Elf_Internal_Rela *relocs)
7908 {
7909 const char *name;
7910 bfd *dynobj;
7911 Elf_Internal_Shdr *symtab_hdr;
7912 struct elf_link_hash_entry **sym_hashes;
7913 size_t extsymoff;
7914 const Elf_Internal_Rela *rel;
7915 const Elf_Internal_Rela *rel_end;
7916 asection *sreloc;
7917 const struct elf_backend_data *bed;
7918 struct mips_elf_link_hash_table *htab;
7919 bfd_byte *contents;
7920 bfd_vma addend;
7921 reloc_howto_type *howto;
7922
7923 if (info->relocatable)
7924 return TRUE;
7925
7926 htab = mips_elf_hash_table (info);
7927 BFD_ASSERT (htab != NULL);
7928
7929 dynobj = elf_hash_table (info)->dynobj;
7930 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7931 sym_hashes = elf_sym_hashes (abfd);
7932 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7933
7934 bed = get_elf_backend_data (abfd);
7935 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7936
7937 /* Check for the mips16 stub sections. */
7938
7939 name = bfd_get_section_name (abfd, sec);
7940 if (FN_STUB_P (name))
7941 {
7942 unsigned long r_symndx;
7943
7944 /* Look at the relocation information to figure out which symbol
7945 this is for. */
7946
7947 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7948 if (r_symndx == 0)
7949 {
7950 (*_bfd_error_handler)
7951 (_("%B: Warning: cannot determine the target function for"
7952 " stub section `%s'"),
7953 abfd, name);
7954 bfd_set_error (bfd_error_bad_value);
7955 return FALSE;
7956 }
7957
7958 if (r_symndx < extsymoff
7959 || sym_hashes[r_symndx - extsymoff] == NULL)
7960 {
7961 asection *o;
7962
7963 /* This stub is for a local symbol. This stub will only be
7964 needed if there is some relocation in this BFD, other
7965 than a 16 bit function call, which refers to this symbol. */
7966 for (o = abfd->sections; o != NULL; o = o->next)
7967 {
7968 Elf_Internal_Rela *sec_relocs;
7969 const Elf_Internal_Rela *r, *rend;
7970
7971 /* We can ignore stub sections when looking for relocs. */
7972 if ((o->flags & SEC_RELOC) == 0
7973 || o->reloc_count == 0
7974 || section_allows_mips16_refs_p (o))
7975 continue;
7976
7977 sec_relocs
7978 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7979 info->keep_memory);
7980 if (sec_relocs == NULL)
7981 return FALSE;
7982
7983 rend = sec_relocs + o->reloc_count;
7984 for (r = sec_relocs; r < rend; r++)
7985 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7986 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7987 break;
7988
7989 if (elf_section_data (o)->relocs != sec_relocs)
7990 free (sec_relocs);
7991
7992 if (r < rend)
7993 break;
7994 }
7995
7996 if (o == NULL)
7997 {
7998 /* There is no non-call reloc for this stub, so we do
7999 not need it. Since this function is called before
8000 the linker maps input sections to output sections, we
8001 can easily discard it by setting the SEC_EXCLUDE
8002 flag. */
8003 sec->flags |= SEC_EXCLUDE;
8004 return TRUE;
8005 }
8006
8007 /* Record this stub in an array of local symbol stubs for
8008 this BFD. */
8009 if (mips_elf_tdata (abfd)->local_stubs == NULL)
8010 {
8011 unsigned long symcount;
8012 asection **n;
8013 bfd_size_type amt;
8014
8015 if (elf_bad_symtab (abfd))
8016 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8017 else
8018 symcount = symtab_hdr->sh_info;
8019 amt = symcount * sizeof (asection *);
8020 n = bfd_zalloc (abfd, amt);
8021 if (n == NULL)
8022 return FALSE;
8023 mips_elf_tdata (abfd)->local_stubs = n;
8024 }
8025
8026 sec->flags |= SEC_KEEP;
8027 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
8028
8029 /* We don't need to set mips16_stubs_seen in this case.
8030 That flag is used to see whether we need to look through
8031 the global symbol table for stubs. We don't need to set
8032 it here, because we just have a local stub. */
8033 }
8034 else
8035 {
8036 struct mips_elf_link_hash_entry *h;
8037
8038 h = ((struct mips_elf_link_hash_entry *)
8039 sym_hashes[r_symndx - extsymoff]);
8040
8041 while (h->root.root.type == bfd_link_hash_indirect
8042 || h->root.root.type == bfd_link_hash_warning)
8043 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8044
8045 /* H is the symbol this stub is for. */
8046
8047 /* If we already have an appropriate stub for this function, we
8048 don't need another one, so we can discard this one. Since
8049 this function is called before the linker maps input sections
8050 to output sections, we can easily discard it by setting the
8051 SEC_EXCLUDE flag. */
8052 if (h->fn_stub != NULL)
8053 {
8054 sec->flags |= SEC_EXCLUDE;
8055 return TRUE;
8056 }
8057
8058 sec->flags |= SEC_KEEP;
8059 h->fn_stub = sec;
8060 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
8061 }
8062 }
8063 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
8064 {
8065 unsigned long r_symndx;
8066 struct mips_elf_link_hash_entry *h;
8067 asection **loc;
8068
8069 /* Look at the relocation information to figure out which symbol
8070 this is for. */
8071
8072 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
8073 if (r_symndx == 0)
8074 {
8075 (*_bfd_error_handler)
8076 (_("%B: Warning: cannot determine the target function for"
8077 " stub section `%s'"),
8078 abfd, name);
8079 bfd_set_error (bfd_error_bad_value);
8080 return FALSE;
8081 }
8082
8083 if (r_symndx < extsymoff
8084 || sym_hashes[r_symndx - extsymoff] == NULL)
8085 {
8086 asection *o;
8087
8088 /* This stub is for a local symbol. This stub will only be
8089 needed if there is some relocation (R_MIPS16_26) in this BFD
8090 that refers to this symbol. */
8091 for (o = abfd->sections; o != NULL; o = o->next)
8092 {
8093 Elf_Internal_Rela *sec_relocs;
8094 const Elf_Internal_Rela *r, *rend;
8095
8096 /* We can ignore stub sections when looking for relocs. */
8097 if ((o->flags & SEC_RELOC) == 0
8098 || o->reloc_count == 0
8099 || section_allows_mips16_refs_p (o))
8100 continue;
8101
8102 sec_relocs
8103 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8104 info->keep_memory);
8105 if (sec_relocs == NULL)
8106 return FALSE;
8107
8108 rend = sec_relocs + o->reloc_count;
8109 for (r = sec_relocs; r < rend; r++)
8110 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8111 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8112 break;
8113
8114 if (elf_section_data (o)->relocs != sec_relocs)
8115 free (sec_relocs);
8116
8117 if (r < rend)
8118 break;
8119 }
8120
8121 if (o == NULL)
8122 {
8123 /* There is no non-call reloc for this stub, so we do
8124 not need it. Since this function is called before
8125 the linker maps input sections to output sections, we
8126 can easily discard it by setting the SEC_EXCLUDE
8127 flag. */
8128 sec->flags |= SEC_EXCLUDE;
8129 return TRUE;
8130 }
8131
8132 /* Record this stub in an array of local symbol call_stubs for
8133 this BFD. */
8134 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
8135 {
8136 unsigned long symcount;
8137 asection **n;
8138 bfd_size_type amt;
8139
8140 if (elf_bad_symtab (abfd))
8141 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8142 else
8143 symcount = symtab_hdr->sh_info;
8144 amt = symcount * sizeof (asection *);
8145 n = bfd_zalloc (abfd, amt);
8146 if (n == NULL)
8147 return FALSE;
8148 mips_elf_tdata (abfd)->local_call_stubs = n;
8149 }
8150
8151 sec->flags |= SEC_KEEP;
8152 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
8153
8154 /* We don't need to set mips16_stubs_seen in this case.
8155 That flag is used to see whether we need to look through
8156 the global symbol table for stubs. We don't need to set
8157 it here, because we just have a local stub. */
8158 }
8159 else
8160 {
8161 h = ((struct mips_elf_link_hash_entry *)
8162 sym_hashes[r_symndx - extsymoff]);
8163
8164 /* H is the symbol this stub is for. */
8165
8166 if (CALL_FP_STUB_P (name))
8167 loc = &h->call_fp_stub;
8168 else
8169 loc = &h->call_stub;
8170
8171 /* If we already have an appropriate stub for this function, we
8172 don't need another one, so we can discard this one. Since
8173 this function is called before the linker maps input sections
8174 to output sections, we can easily discard it by setting the
8175 SEC_EXCLUDE flag. */
8176 if (*loc != NULL)
8177 {
8178 sec->flags |= SEC_EXCLUDE;
8179 return TRUE;
8180 }
8181
8182 sec->flags |= SEC_KEEP;
8183 *loc = sec;
8184 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
8185 }
8186 }
8187
8188 sreloc = NULL;
8189 contents = NULL;
8190 for (rel = relocs; rel < rel_end; ++rel)
8191 {
8192 unsigned long r_symndx;
8193 unsigned int r_type;
8194 struct elf_link_hash_entry *h;
8195 bfd_boolean can_make_dynamic_p;
8196 bfd_boolean call_reloc_p;
8197 bfd_boolean constrain_symbol_p;
8198
8199 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8200 r_type = ELF_R_TYPE (abfd, rel->r_info);
8201
8202 if (r_symndx < extsymoff)
8203 h = NULL;
8204 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8205 {
8206 (*_bfd_error_handler)
8207 (_("%B: Malformed reloc detected for section %s"),
8208 abfd, name);
8209 bfd_set_error (bfd_error_bad_value);
8210 return FALSE;
8211 }
8212 else
8213 {
8214 h = sym_hashes[r_symndx - extsymoff];
8215 if (h != NULL)
8216 {
8217 while (h->root.type == bfd_link_hash_indirect
8218 || h->root.type == bfd_link_hash_warning)
8219 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8220
8221 /* PR15323, ref flags aren't set for references in the
8222 same object. */
8223 h->root.non_ir_ref = 1;
8224 }
8225 }
8226
8227 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8228 relocation into a dynamic one. */
8229 can_make_dynamic_p = FALSE;
8230
8231 /* Set CALL_RELOC_P to true if the relocation is for a call,
8232 and if pointer equality therefore doesn't matter. */
8233 call_reloc_p = FALSE;
8234
8235 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8236 into account when deciding how to define the symbol.
8237 Relocations in nonallocatable sections such as .pdr and
8238 .debug* should have no effect. */
8239 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8240
8241 switch (r_type)
8242 {
8243 case R_MIPS_CALL16:
8244 case R_MIPS_CALL_HI16:
8245 case R_MIPS_CALL_LO16:
8246 case R_MIPS16_CALL16:
8247 case R_MICROMIPS_CALL16:
8248 case R_MICROMIPS_CALL_HI16:
8249 case R_MICROMIPS_CALL_LO16:
8250 call_reloc_p = TRUE;
8251 /* Fall through. */
8252
8253 case R_MIPS_GOT16:
8254 case R_MIPS_GOT_HI16:
8255 case R_MIPS_GOT_LO16:
8256 case R_MIPS_GOT_PAGE:
8257 case R_MIPS_GOT_OFST:
8258 case R_MIPS_GOT_DISP:
8259 case R_MIPS_TLS_GOTTPREL:
8260 case R_MIPS_TLS_GD:
8261 case R_MIPS_TLS_LDM:
8262 case R_MIPS16_GOT16:
8263 case R_MIPS16_TLS_GOTTPREL:
8264 case R_MIPS16_TLS_GD:
8265 case R_MIPS16_TLS_LDM:
8266 case R_MICROMIPS_GOT16:
8267 case R_MICROMIPS_GOT_HI16:
8268 case R_MICROMIPS_GOT_LO16:
8269 case R_MICROMIPS_GOT_PAGE:
8270 case R_MICROMIPS_GOT_OFST:
8271 case R_MICROMIPS_GOT_DISP:
8272 case R_MICROMIPS_TLS_GOTTPREL:
8273 case R_MICROMIPS_TLS_GD:
8274 case R_MICROMIPS_TLS_LDM:
8275 if (dynobj == NULL)
8276 elf_hash_table (info)->dynobj = dynobj = abfd;
8277 if (!mips_elf_create_got_section (dynobj, info))
8278 return FALSE;
8279 if (htab->is_vxworks && !info->shared)
8280 {
8281 (*_bfd_error_handler)
8282 (_("%B: GOT reloc at 0x%lx not expected in executables"),
8283 abfd, (unsigned long) rel->r_offset);
8284 bfd_set_error (bfd_error_bad_value);
8285 return FALSE;
8286 }
8287 can_make_dynamic_p = TRUE;
8288 break;
8289
8290 case R_MIPS_NONE:
8291 case R_MIPS_JALR:
8292 case R_MICROMIPS_JALR:
8293 /* These relocations have empty fields and are purely there to
8294 provide link information. The symbol value doesn't matter. */
8295 constrain_symbol_p = FALSE;
8296 break;
8297
8298 case R_MIPS_GPREL16:
8299 case R_MIPS_GPREL32:
8300 case R_MIPS16_GPREL:
8301 case R_MICROMIPS_GPREL16:
8302 /* GP-relative relocations always resolve to a definition in a
8303 regular input file, ignoring the one-definition rule. This is
8304 important for the GP setup sequence in NewABI code, which
8305 always resolves to a local function even if other relocations
8306 against the symbol wouldn't. */
8307 constrain_symbol_p = FALSE;
8308 break;
8309
8310 case R_MIPS_32:
8311 case R_MIPS_REL32:
8312 case R_MIPS_64:
8313 /* In VxWorks executables, references to external symbols
8314 must be handled using copy relocs or PLT entries; it is not
8315 possible to convert this relocation into a dynamic one.
8316
8317 For executables that use PLTs and copy-relocs, we have a
8318 choice between converting the relocation into a dynamic
8319 one or using copy relocations or PLT entries. It is
8320 usually better to do the former, unless the relocation is
8321 against a read-only section. */
8322 if ((info->shared
8323 || (h != NULL
8324 && !htab->is_vxworks
8325 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8326 && !(!info->nocopyreloc
8327 && !PIC_OBJECT_P (abfd)
8328 && MIPS_ELF_READONLY_SECTION (sec))))
8329 && (sec->flags & SEC_ALLOC) != 0)
8330 {
8331 can_make_dynamic_p = TRUE;
8332 if (dynobj == NULL)
8333 elf_hash_table (info)->dynobj = dynobj = abfd;
8334 }
8335 break;
8336
8337 case R_MIPS_26:
8338 case R_MIPS_PC16:
8339 case R_MIPS_PC21_S2:
8340 case R_MIPS_PC26_S2:
8341 case R_MIPS16_26:
8342 case R_MICROMIPS_26_S1:
8343 case R_MICROMIPS_PC7_S1:
8344 case R_MICROMIPS_PC10_S1:
8345 case R_MICROMIPS_PC16_S1:
8346 case R_MICROMIPS_PC23_S2:
8347 call_reloc_p = TRUE;
8348 break;
8349 }
8350
8351 if (h)
8352 {
8353 if (constrain_symbol_p)
8354 {
8355 if (!can_make_dynamic_p)
8356 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8357
8358 if (!call_reloc_p)
8359 h->pointer_equality_needed = 1;
8360
8361 /* We must not create a stub for a symbol that has
8362 relocations related to taking the function's address.
8363 This doesn't apply to VxWorks, where CALL relocs refer
8364 to a .got.plt entry instead of a normal .got entry. */
8365 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8366 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8367 }
8368
8369 /* Relocations against the special VxWorks __GOTT_BASE__ and
8370 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8371 room for them in .rela.dyn. */
8372 if (is_gott_symbol (info, h))
8373 {
8374 if (sreloc == NULL)
8375 {
8376 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8377 if (sreloc == NULL)
8378 return FALSE;
8379 }
8380 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8381 if (MIPS_ELF_READONLY_SECTION (sec))
8382 /* We tell the dynamic linker that there are
8383 relocations against the text segment. */
8384 info->flags |= DF_TEXTREL;
8385 }
8386 }
8387 else if (call_lo16_reloc_p (r_type)
8388 || got_lo16_reloc_p (r_type)
8389 || got_disp_reloc_p (r_type)
8390 || (got16_reloc_p (r_type) && htab->is_vxworks))
8391 {
8392 /* We may need a local GOT entry for this relocation. We
8393 don't count R_MIPS_GOT_PAGE because we can estimate the
8394 maximum number of pages needed by looking at the size of
8395 the segment. Similar comments apply to R_MIPS*_GOT16 and
8396 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8397 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8398 R_MIPS_CALL_HI16 because these are always followed by an
8399 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8400 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8401 rel->r_addend, info, r_type))
8402 return FALSE;
8403 }
8404
8405 if (h != NULL
8406 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8407 ELF_ST_IS_MIPS16 (h->other)))
8408 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8409
8410 switch (r_type)
8411 {
8412 case R_MIPS_CALL16:
8413 case R_MIPS16_CALL16:
8414 case R_MICROMIPS_CALL16:
8415 if (h == NULL)
8416 {
8417 (*_bfd_error_handler)
8418 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
8419 abfd, (unsigned long) rel->r_offset);
8420 bfd_set_error (bfd_error_bad_value);
8421 return FALSE;
8422 }
8423 /* Fall through. */
8424
8425 case R_MIPS_CALL_HI16:
8426 case R_MIPS_CALL_LO16:
8427 case R_MICROMIPS_CALL_HI16:
8428 case R_MICROMIPS_CALL_LO16:
8429 if (h != NULL)
8430 {
8431 /* Make sure there is room in the regular GOT to hold the
8432 function's address. We may eliminate it in favour of
8433 a .got.plt entry later; see mips_elf_count_got_symbols. */
8434 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8435 r_type))
8436 return FALSE;
8437
8438 /* We need a stub, not a plt entry for the undefined
8439 function. But we record it as if it needs plt. See
8440 _bfd_elf_adjust_dynamic_symbol. */
8441 h->needs_plt = 1;
8442 h->type = STT_FUNC;
8443 }
8444 break;
8445
8446 case R_MIPS_GOT_PAGE:
8447 case R_MICROMIPS_GOT_PAGE:
8448 case R_MIPS16_GOT16:
8449 case R_MIPS_GOT16:
8450 case R_MIPS_GOT_HI16:
8451 case R_MIPS_GOT_LO16:
8452 case R_MICROMIPS_GOT16:
8453 case R_MICROMIPS_GOT_HI16:
8454 case R_MICROMIPS_GOT_LO16:
8455 if (!h || got_page_reloc_p (r_type))
8456 {
8457 /* This relocation needs (or may need, if h != NULL) a
8458 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8459 know for sure until we know whether the symbol is
8460 preemptible. */
8461 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8462 {
8463 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8464 return FALSE;
8465 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8466 addend = mips_elf_read_rel_addend (abfd, rel,
8467 howto, contents);
8468 if (got16_reloc_p (r_type))
8469 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8470 contents, &addend);
8471 else
8472 addend <<= howto->rightshift;
8473 }
8474 else
8475 addend = rel->r_addend;
8476 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8477 h, addend))
8478 return FALSE;
8479
8480 if (h)
8481 {
8482 struct mips_elf_link_hash_entry *hmips =
8483 (struct mips_elf_link_hash_entry *) h;
8484
8485 /* This symbol is definitely not overridable. */
8486 if (hmips->root.def_regular
8487 && ! (info->shared && ! info->symbolic
8488 && ! hmips->root.forced_local))
8489 h = NULL;
8490 }
8491 }
8492 /* If this is a global, overridable symbol, GOT_PAGE will
8493 decay to GOT_DISP, so we'll need a GOT entry for it. */
8494 /* Fall through. */
8495
8496 case R_MIPS_GOT_DISP:
8497 case R_MICROMIPS_GOT_DISP:
8498 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8499 FALSE, r_type))
8500 return FALSE;
8501 break;
8502
8503 case R_MIPS_TLS_GOTTPREL:
8504 case R_MIPS16_TLS_GOTTPREL:
8505 case R_MICROMIPS_TLS_GOTTPREL:
8506 if (info->shared)
8507 info->flags |= DF_STATIC_TLS;
8508 /* Fall through */
8509
8510 case R_MIPS_TLS_LDM:
8511 case R_MIPS16_TLS_LDM:
8512 case R_MICROMIPS_TLS_LDM:
8513 if (tls_ldm_reloc_p (r_type))
8514 {
8515 r_symndx = STN_UNDEF;
8516 h = NULL;
8517 }
8518 /* Fall through */
8519
8520 case R_MIPS_TLS_GD:
8521 case R_MIPS16_TLS_GD:
8522 case R_MICROMIPS_TLS_GD:
8523 /* This symbol requires a global offset table entry, or two
8524 for TLS GD relocations. */
8525 if (h != NULL)
8526 {
8527 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8528 FALSE, r_type))
8529 return FALSE;
8530 }
8531 else
8532 {
8533 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8534 rel->r_addend,
8535 info, r_type))
8536 return FALSE;
8537 }
8538 break;
8539
8540 case R_MIPS_32:
8541 case R_MIPS_REL32:
8542 case R_MIPS_64:
8543 /* In VxWorks executables, references to external symbols
8544 are handled using copy relocs or PLT stubs, so there's
8545 no need to add a .rela.dyn entry for this relocation. */
8546 if (can_make_dynamic_p)
8547 {
8548 if (sreloc == NULL)
8549 {
8550 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8551 if (sreloc == NULL)
8552 return FALSE;
8553 }
8554 if (info->shared && h == NULL)
8555 {
8556 /* When creating a shared object, we must copy these
8557 reloc types into the output file as R_MIPS_REL32
8558 relocs. Make room for this reloc in .rel(a).dyn. */
8559 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8560 if (MIPS_ELF_READONLY_SECTION (sec))
8561 /* We tell the dynamic linker that there are
8562 relocations against the text segment. */
8563 info->flags |= DF_TEXTREL;
8564 }
8565 else
8566 {
8567 struct mips_elf_link_hash_entry *hmips;
8568
8569 /* For a shared object, we must copy this relocation
8570 unless the symbol turns out to be undefined and
8571 weak with non-default visibility, in which case
8572 it will be left as zero.
8573
8574 We could elide R_MIPS_REL32 for locally binding symbols
8575 in shared libraries, but do not yet do so.
8576
8577 For an executable, we only need to copy this
8578 reloc if the symbol is defined in a dynamic
8579 object. */
8580 hmips = (struct mips_elf_link_hash_entry *) h;
8581 ++hmips->possibly_dynamic_relocs;
8582 if (MIPS_ELF_READONLY_SECTION (sec))
8583 /* We need it to tell the dynamic linker if there
8584 are relocations against the text segment. */
8585 hmips->readonly_reloc = TRUE;
8586 }
8587 }
8588
8589 if (SGI_COMPAT (abfd))
8590 mips_elf_hash_table (info)->compact_rel_size +=
8591 sizeof (Elf32_External_crinfo);
8592 break;
8593
8594 case R_MIPS_26:
8595 case R_MIPS_GPREL16:
8596 case R_MIPS_LITERAL:
8597 case R_MIPS_GPREL32:
8598 case R_MICROMIPS_26_S1:
8599 case R_MICROMIPS_GPREL16:
8600 case R_MICROMIPS_LITERAL:
8601 case R_MICROMIPS_GPREL7_S2:
8602 if (SGI_COMPAT (abfd))
8603 mips_elf_hash_table (info)->compact_rel_size +=
8604 sizeof (Elf32_External_crinfo);
8605 break;
8606
8607 /* This relocation describes the C++ object vtable hierarchy.
8608 Reconstruct it for later use during GC. */
8609 case R_MIPS_GNU_VTINHERIT:
8610 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
8611 return FALSE;
8612 break;
8613
8614 /* This relocation describes which C++ vtable entries are actually
8615 used. Record for later use during GC. */
8616 case R_MIPS_GNU_VTENTRY:
8617 BFD_ASSERT (h != NULL);
8618 if (h != NULL
8619 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
8620 return FALSE;
8621 break;
8622
8623 default:
8624 break;
8625 }
8626
8627 /* Record the need for a PLT entry. At this point we don't know
8628 yet if we are going to create a PLT in the first place, but
8629 we only record whether the relocation requires a standard MIPS
8630 or a compressed code entry anyway. If we don't make a PLT after
8631 all, then we'll just ignore these arrangements. Likewise if
8632 a PLT entry is not created because the symbol is satisfied
8633 locally. */
8634 if (h != NULL
8635 && jal_reloc_p (r_type)
8636 && !SYMBOL_CALLS_LOCAL (info, h))
8637 {
8638 if (h->plt.plist == NULL)
8639 h->plt.plist = mips_elf_make_plt_record (abfd);
8640 if (h->plt.plist == NULL)
8641 return FALSE;
8642
8643 if (r_type == R_MIPS_26)
8644 h->plt.plist->need_mips = TRUE;
8645 else
8646 h->plt.plist->need_comp = TRUE;
8647 }
8648
8649 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8650 if there is one. We only need to handle global symbols here;
8651 we decide whether to keep or delete stubs for local symbols
8652 when processing the stub's relocations. */
8653 if (h != NULL
8654 && !mips16_call_reloc_p (r_type)
8655 && !section_allows_mips16_refs_p (sec))
8656 {
8657 struct mips_elf_link_hash_entry *mh;
8658
8659 mh = (struct mips_elf_link_hash_entry *) h;
8660 mh->need_fn_stub = TRUE;
8661 }
8662
8663 /* Refuse some position-dependent relocations when creating a
8664 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8665 not PIC, but we can create dynamic relocations and the result
8666 will be fine. Also do not refuse R_MIPS_LO16, which can be
8667 combined with R_MIPS_GOT16. */
8668 if (info->shared)
8669 {
8670 switch (r_type)
8671 {
8672 case R_MIPS16_HI16:
8673 case R_MIPS_HI16:
8674 case R_MIPS_HIGHER:
8675 case R_MIPS_HIGHEST:
8676 case R_MICROMIPS_HI16:
8677 case R_MICROMIPS_HIGHER:
8678 case R_MICROMIPS_HIGHEST:
8679 /* Don't refuse a high part relocation if it's against
8680 no symbol (e.g. part of a compound relocation). */
8681 if (r_symndx == STN_UNDEF)
8682 break;
8683
8684 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8685 and has a special meaning. */
8686 if (!NEWABI_P (abfd) && h != NULL
8687 && strcmp (h->root.root.string, "_gp_disp") == 0)
8688 break;
8689
8690 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8691 if (is_gott_symbol (info, h))
8692 break;
8693
8694 /* FALLTHROUGH */
8695
8696 case R_MIPS16_26:
8697 case R_MIPS_26:
8698 case R_MICROMIPS_26_S1:
8699 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8700 (*_bfd_error_handler)
8701 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8702 abfd, howto->name,
8703 (h) ? h->root.root.string : "a local symbol");
8704 bfd_set_error (bfd_error_bad_value);
8705 return FALSE;
8706 default:
8707 break;
8708 }
8709 }
8710 }
8711
8712 return TRUE;
8713 }
8714
8715 bfd_boolean
_bfd_mips_relax_section(bfd * abfd,asection * sec,struct bfd_link_info * link_info,bfd_boolean * again)8716 _bfd_mips_relax_section (bfd *abfd, asection *sec,
8717 struct bfd_link_info *link_info,
8718 bfd_boolean *again)
8719 {
8720 Elf_Internal_Rela *internal_relocs;
8721 Elf_Internal_Rela *irel, *irelend;
8722 Elf_Internal_Shdr *symtab_hdr;
8723 bfd_byte *contents = NULL;
8724 size_t extsymoff;
8725 bfd_boolean changed_contents = FALSE;
8726 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8727 Elf_Internal_Sym *isymbuf = NULL;
8728
8729 /* We are not currently changing any sizes, so only one pass. */
8730 *again = FALSE;
8731
8732 if (link_info->relocatable)
8733 return TRUE;
8734
8735 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8736 link_info->keep_memory);
8737 if (internal_relocs == NULL)
8738 return TRUE;
8739
8740 irelend = internal_relocs + sec->reloc_count
8741 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8742 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8743 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8744
8745 for (irel = internal_relocs; irel < irelend; irel++)
8746 {
8747 bfd_vma symval;
8748 bfd_signed_vma sym_offset;
8749 unsigned int r_type;
8750 unsigned long r_symndx;
8751 asection *sym_sec;
8752 unsigned long instruction;
8753
8754 /* Turn jalr into bgezal, and jr into beq, if they're marked
8755 with a JALR relocation, that indicate where they jump to.
8756 This saves some pipeline bubbles. */
8757 r_type = ELF_R_TYPE (abfd, irel->r_info);
8758 if (r_type != R_MIPS_JALR)
8759 continue;
8760
8761 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8762 /* Compute the address of the jump target. */
8763 if (r_symndx >= extsymoff)
8764 {
8765 struct mips_elf_link_hash_entry *h
8766 = ((struct mips_elf_link_hash_entry *)
8767 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8768
8769 while (h->root.root.type == bfd_link_hash_indirect
8770 || h->root.root.type == bfd_link_hash_warning)
8771 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8772
8773 /* If a symbol is undefined, or if it may be overridden,
8774 skip it. */
8775 if (! ((h->root.root.type == bfd_link_hash_defined
8776 || h->root.root.type == bfd_link_hash_defweak)
8777 && h->root.root.u.def.section)
8778 || (link_info->shared && ! link_info->symbolic
8779 && !h->root.forced_local))
8780 continue;
8781
8782 sym_sec = h->root.root.u.def.section;
8783 if (sym_sec->output_section)
8784 symval = (h->root.root.u.def.value
8785 + sym_sec->output_section->vma
8786 + sym_sec->output_offset);
8787 else
8788 symval = h->root.root.u.def.value;
8789 }
8790 else
8791 {
8792 Elf_Internal_Sym *isym;
8793
8794 /* Read this BFD's symbols if we haven't done so already. */
8795 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8796 {
8797 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8798 if (isymbuf == NULL)
8799 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8800 symtab_hdr->sh_info, 0,
8801 NULL, NULL, NULL);
8802 if (isymbuf == NULL)
8803 goto relax_return;
8804 }
8805
8806 isym = isymbuf + r_symndx;
8807 if (isym->st_shndx == SHN_UNDEF)
8808 continue;
8809 else if (isym->st_shndx == SHN_ABS)
8810 sym_sec = bfd_abs_section_ptr;
8811 else if (isym->st_shndx == SHN_COMMON)
8812 sym_sec = bfd_com_section_ptr;
8813 else
8814 sym_sec
8815 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8816 symval = isym->st_value
8817 + sym_sec->output_section->vma
8818 + sym_sec->output_offset;
8819 }
8820
8821 /* Compute branch offset, from delay slot of the jump to the
8822 branch target. */
8823 sym_offset = (symval + irel->r_addend)
8824 - (sec_start + irel->r_offset + 4);
8825
8826 /* Branch offset must be properly aligned. */
8827 if ((sym_offset & 3) != 0)
8828 continue;
8829
8830 sym_offset >>= 2;
8831
8832 /* Check that it's in range. */
8833 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8834 continue;
8835
8836 /* Get the section contents if we haven't done so already. */
8837 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8838 goto relax_return;
8839
8840 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8841
8842 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8843 if ((instruction & 0xfc1fffff) == 0x0000f809)
8844 instruction = 0x04110000;
8845 /* If it was jr <reg>, turn it into b <target>. */
8846 else if ((instruction & 0xfc1fffff) == 0x00000008)
8847 instruction = 0x10000000;
8848 else
8849 continue;
8850
8851 instruction |= (sym_offset & 0xffff);
8852 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8853 changed_contents = TRUE;
8854 }
8855
8856 if (contents != NULL
8857 && elf_section_data (sec)->this_hdr.contents != contents)
8858 {
8859 if (!changed_contents && !link_info->keep_memory)
8860 free (contents);
8861 else
8862 {
8863 /* Cache the section contents for elf_link_input_bfd. */
8864 elf_section_data (sec)->this_hdr.contents = contents;
8865 }
8866 }
8867 return TRUE;
8868
8869 relax_return:
8870 if (contents != NULL
8871 && elf_section_data (sec)->this_hdr.contents != contents)
8872 free (contents);
8873 return FALSE;
8874 }
8875
8876 /* Allocate space for global sym dynamic relocs. */
8877
8878 static bfd_boolean
allocate_dynrelocs(struct elf_link_hash_entry * h,void * inf)8879 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8880 {
8881 struct bfd_link_info *info = inf;
8882 bfd *dynobj;
8883 struct mips_elf_link_hash_entry *hmips;
8884 struct mips_elf_link_hash_table *htab;
8885
8886 htab = mips_elf_hash_table (info);
8887 BFD_ASSERT (htab != NULL);
8888
8889 dynobj = elf_hash_table (info)->dynobj;
8890 hmips = (struct mips_elf_link_hash_entry *) h;
8891
8892 /* VxWorks executables are handled elsewhere; we only need to
8893 allocate relocations in shared objects. */
8894 if (htab->is_vxworks && !info->shared)
8895 return TRUE;
8896
8897 /* Ignore indirect symbols. All relocations against such symbols
8898 will be redirected to the target symbol. */
8899 if (h->root.type == bfd_link_hash_indirect)
8900 return TRUE;
8901
8902 /* If this symbol is defined in a dynamic object, or we are creating
8903 a shared library, we will need to copy any R_MIPS_32 or
8904 R_MIPS_REL32 relocs against it into the output file. */
8905 if (! info->relocatable
8906 && hmips->possibly_dynamic_relocs != 0
8907 && (h->root.type == bfd_link_hash_defweak
8908 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
8909 || info->shared))
8910 {
8911 bfd_boolean do_copy = TRUE;
8912
8913 if (h->root.type == bfd_link_hash_undefweak)
8914 {
8915 /* Do not copy relocations for undefined weak symbols with
8916 non-default visibility. */
8917 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8918 do_copy = FALSE;
8919
8920 /* Make sure undefined weak symbols are output as a dynamic
8921 symbol in PIEs. */
8922 else if (h->dynindx == -1 && !h->forced_local)
8923 {
8924 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8925 return FALSE;
8926 }
8927 }
8928
8929 if (do_copy)
8930 {
8931 /* Even though we don't directly need a GOT entry for this symbol,
8932 the SVR4 psABI requires it to have a dynamic symbol table
8933 index greater that DT_MIPS_GOTSYM if there are dynamic
8934 relocations against it.
8935
8936 VxWorks does not enforce the same mapping between the GOT
8937 and the symbol table, so the same requirement does not
8938 apply there. */
8939 if (!htab->is_vxworks)
8940 {
8941 if (hmips->global_got_area > GGA_RELOC_ONLY)
8942 hmips->global_got_area = GGA_RELOC_ONLY;
8943 hmips->got_only_for_calls = FALSE;
8944 }
8945
8946 mips_elf_allocate_dynamic_relocations
8947 (dynobj, info, hmips->possibly_dynamic_relocs);
8948 if (hmips->readonly_reloc)
8949 /* We tell the dynamic linker that there are relocations
8950 against the text segment. */
8951 info->flags |= DF_TEXTREL;
8952 }
8953 }
8954
8955 return TRUE;
8956 }
8957
8958 /* Adjust a symbol defined by a dynamic object and referenced by a
8959 regular object. The current definition is in some section of the
8960 dynamic object, but we're not including those sections. We have to
8961 change the definition to something the rest of the link can
8962 understand. */
8963
8964 bfd_boolean
_bfd_mips_elf_adjust_dynamic_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * h)8965 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8966 struct elf_link_hash_entry *h)
8967 {
8968 bfd *dynobj;
8969 struct mips_elf_link_hash_entry *hmips;
8970 struct mips_elf_link_hash_table *htab;
8971
8972 htab = mips_elf_hash_table (info);
8973 BFD_ASSERT (htab != NULL);
8974
8975 dynobj = elf_hash_table (info)->dynobj;
8976 hmips = (struct mips_elf_link_hash_entry *) h;
8977
8978 /* Make sure we know what is going on here. */
8979 BFD_ASSERT (dynobj != NULL
8980 && (h->needs_plt
8981 || h->u.weakdef != NULL
8982 || (h->def_dynamic
8983 && h->ref_regular
8984 && !h->def_regular)));
8985
8986 hmips = (struct mips_elf_link_hash_entry *) h;
8987
8988 /* If there are call relocations against an externally-defined symbol,
8989 see whether we can create a MIPS lazy-binding stub for it. We can
8990 only do this if all references to the function are through call
8991 relocations, and in that case, the traditional lazy-binding stubs
8992 are much more efficient than PLT entries.
8993
8994 Traditional stubs are only available on SVR4 psABI-based systems;
8995 VxWorks always uses PLTs instead. */
8996 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8997 {
8998 if (! elf_hash_table (info)->dynamic_sections_created)
8999 return TRUE;
9000
9001 /* If this symbol is not defined in a regular file, then set
9002 the symbol to the stub location. This is required to make
9003 function pointers compare as equal between the normal
9004 executable and the shared library. */
9005 if (!h->def_regular)
9006 {
9007 hmips->needs_lazy_stub = TRUE;
9008 htab->lazy_stub_count++;
9009 return TRUE;
9010 }
9011 }
9012 /* As above, VxWorks requires PLT entries for externally-defined
9013 functions that are only accessed through call relocations.
9014
9015 Both VxWorks and non-VxWorks targets also need PLT entries if there
9016 are static-only relocations against an externally-defined function.
9017 This can technically occur for shared libraries if there are
9018 branches to the symbol, although it is unlikely that this will be
9019 used in practice due to the short ranges involved. It can occur
9020 for any relative or absolute relocation in executables; in that
9021 case, the PLT entry becomes the function's canonical address. */
9022 else if (((h->needs_plt && !hmips->no_fn_stub)
9023 || (h->type == STT_FUNC && hmips->has_static_relocs))
9024 && htab->use_plts_and_copy_relocs
9025 && !SYMBOL_CALLS_LOCAL (info, h)
9026 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9027 && h->root.type == bfd_link_hash_undefweak))
9028 {
9029 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9030 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
9031
9032 /* If this is the first symbol to need a PLT entry, then make some
9033 basic setup. Also work out PLT entry sizes. We'll need them
9034 for PLT offset calculations. */
9035 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
9036 {
9037 BFD_ASSERT (htab->sgotplt->size == 0);
9038 BFD_ASSERT (htab->plt_got_index == 0);
9039
9040 /* If we're using the PLT additions to the psABI, each PLT
9041 entry is 16 bytes and the PLT0 entry is 32 bytes.
9042 Encourage better cache usage by aligning. We do this
9043 lazily to avoid pessimizing traditional objects. */
9044 if (!htab->is_vxworks
9045 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
9046 return FALSE;
9047
9048 /* Make sure that .got.plt is word-aligned. We do this lazily
9049 for the same reason as above. */
9050 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
9051 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9052 return FALSE;
9053
9054 /* On non-VxWorks targets, the first two entries in .got.plt
9055 are reserved. */
9056 if (!htab->is_vxworks)
9057 htab->plt_got_index
9058 += (get_elf_backend_data (dynobj)->got_header_size
9059 / MIPS_ELF_GOT_SIZE (dynobj));
9060
9061 /* On VxWorks, also allocate room for the header's
9062 .rela.plt.unloaded entries. */
9063 if (htab->is_vxworks && !info->shared)
9064 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
9065
9066 /* Now work out the sizes of individual PLT entries. */
9067 if (htab->is_vxworks && info->shared)
9068 htab->plt_mips_entry_size
9069 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9070 else if (htab->is_vxworks)
9071 htab->plt_mips_entry_size
9072 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9073 else if (newabi_p)
9074 htab->plt_mips_entry_size
9075 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9076 else if (!micromips_p)
9077 {
9078 htab->plt_mips_entry_size
9079 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9080 htab->plt_comp_entry_size
9081 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9082 }
9083 else if (htab->insn32)
9084 {
9085 htab->plt_mips_entry_size
9086 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9087 htab->plt_comp_entry_size
9088 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
9089 }
9090 else
9091 {
9092 htab->plt_mips_entry_size
9093 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9094 htab->plt_comp_entry_size
9095 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
9096 }
9097 }
9098
9099 if (h->plt.plist == NULL)
9100 h->plt.plist = mips_elf_make_plt_record (dynobj);
9101 if (h->plt.plist == NULL)
9102 return FALSE;
9103
9104 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
9105 n32 or n64, so always use a standard entry there.
9106
9107 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9108 all MIPS16 calls will go via that stub, and there is no benefit
9109 to having a MIPS16 entry. And in the case of call_stub a
9110 standard entry actually has to be used as the stub ends with a J
9111 instruction. */
9112 if (newabi_p
9113 || htab->is_vxworks
9114 || hmips->call_stub
9115 || hmips->call_fp_stub)
9116 {
9117 h->plt.plist->need_mips = TRUE;
9118 h->plt.plist->need_comp = FALSE;
9119 }
9120
9121 /* Otherwise, if there are no direct calls to the function, we
9122 have a free choice of whether to use standard or compressed
9123 entries. Prefer microMIPS entries if the object is known to
9124 contain microMIPS code, so that it becomes possible to create
9125 pure microMIPS binaries. Prefer standard entries otherwise,
9126 because MIPS16 ones are no smaller and are usually slower. */
9127 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9128 {
9129 if (micromips_p)
9130 h->plt.plist->need_comp = TRUE;
9131 else
9132 h->plt.plist->need_mips = TRUE;
9133 }
9134
9135 if (h->plt.plist->need_mips)
9136 {
9137 h->plt.plist->mips_offset = htab->plt_mips_offset;
9138 htab->plt_mips_offset += htab->plt_mips_entry_size;
9139 }
9140 if (h->plt.plist->need_comp)
9141 {
9142 h->plt.plist->comp_offset = htab->plt_comp_offset;
9143 htab->plt_comp_offset += htab->plt_comp_entry_size;
9144 }
9145
9146 /* Reserve the corresponding .got.plt entry now too. */
9147 h->plt.plist->gotplt_index = htab->plt_got_index++;
9148
9149 /* If the output file has no definition of the symbol, set the
9150 symbol's value to the address of the stub. */
9151 if (!info->shared && !h->def_regular)
9152 hmips->use_plt_entry = TRUE;
9153
9154 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
9155 htab->srelplt->size += (htab->is_vxworks
9156 ? MIPS_ELF_RELA_SIZE (dynobj)
9157 : MIPS_ELF_REL_SIZE (dynobj));
9158
9159 /* Make room for the .rela.plt.unloaded relocations. */
9160 if (htab->is_vxworks && !info->shared)
9161 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9162
9163 /* All relocations against this symbol that could have been made
9164 dynamic will now refer to the PLT entry instead. */
9165 hmips->possibly_dynamic_relocs = 0;
9166
9167 return TRUE;
9168 }
9169
9170 /* If this is a weak symbol, and there is a real definition, the
9171 processor independent code will have arranged for us to see the
9172 real definition first, and we can just use the same value. */
9173 if (h->u.weakdef != NULL)
9174 {
9175 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
9176 || h->u.weakdef->root.type == bfd_link_hash_defweak);
9177 h->root.u.def.section = h->u.weakdef->root.u.def.section;
9178 h->root.u.def.value = h->u.weakdef->root.u.def.value;
9179 return TRUE;
9180 }
9181
9182 /* Otherwise, there is nothing further to do for symbols defined
9183 in regular objects. */
9184 if (h->def_regular)
9185 return TRUE;
9186
9187 /* There's also nothing more to do if we'll convert all relocations
9188 against this symbol into dynamic relocations. */
9189 if (!hmips->has_static_relocs)
9190 return TRUE;
9191
9192 /* We're now relying on copy relocations. Complain if we have
9193 some that we can't convert. */
9194 if (!htab->use_plts_and_copy_relocs || info->shared)
9195 {
9196 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
9197 "dynamic symbol %s"),
9198 h->root.root.string);
9199 bfd_set_error (bfd_error_bad_value);
9200 return FALSE;
9201 }
9202
9203 /* We must allocate the symbol in our .dynbss section, which will
9204 become part of the .bss section of the executable. There will be
9205 an entry for this symbol in the .dynsym section. The dynamic
9206 object will contain position independent code, so all references
9207 from the dynamic object to this symbol will go through the global
9208 offset table. The dynamic linker will use the .dynsym entry to
9209 determine the address it must put in the global offset table, so
9210 both the dynamic object and the regular object will refer to the
9211 same memory location for the variable. */
9212
9213 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9214 {
9215 if (htab->is_vxworks)
9216 htab->srelbss->size += sizeof (Elf32_External_Rela);
9217 else
9218 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9219 h->needs_copy = 1;
9220 }
9221
9222 /* All relocations against this symbol that could have been made
9223 dynamic will now refer to the local copy instead. */
9224 hmips->possibly_dynamic_relocs = 0;
9225
9226 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
9227 }
9228
9229 /* This function is called after all the input files have been read,
9230 and the input sections have been assigned to output sections. We
9231 check for any mips16 stub sections that we can discard. */
9232
9233 bfd_boolean
_bfd_mips_elf_always_size_sections(bfd * output_bfd,struct bfd_link_info * info)9234 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9235 struct bfd_link_info *info)
9236 {
9237 asection *sect;
9238 struct mips_elf_link_hash_table *htab;
9239 struct mips_htab_traverse_info hti;
9240
9241 htab = mips_elf_hash_table (info);
9242 BFD_ASSERT (htab != NULL);
9243
9244 /* The .reginfo section has a fixed size. */
9245 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9246 if (sect != NULL)
9247 bfd_set_section_size (output_bfd, sect, sizeof (Elf32_External_RegInfo));
9248
9249 /* The .MIPS.abiflags section has a fixed size. */
9250 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9251 if (sect != NULL)
9252 bfd_set_section_size (output_bfd, sect, sizeof (Elf_External_ABIFlags_v0));
9253
9254 hti.info = info;
9255 hti.output_bfd = output_bfd;
9256 hti.error = FALSE;
9257 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9258 mips_elf_check_symbols, &hti);
9259 if (hti.error)
9260 return FALSE;
9261
9262 return TRUE;
9263 }
9264
9265 /* If the link uses a GOT, lay it out and work out its size. */
9266
9267 static bfd_boolean
mips_elf_lay_out_got(bfd * output_bfd,struct bfd_link_info * info)9268 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9269 {
9270 bfd *dynobj;
9271 asection *s;
9272 struct mips_got_info *g;
9273 bfd_size_type loadable_size = 0;
9274 bfd_size_type page_gotno;
9275 bfd *ibfd;
9276 struct mips_elf_traverse_got_arg tga;
9277 struct mips_elf_link_hash_table *htab;
9278
9279 htab = mips_elf_hash_table (info);
9280 BFD_ASSERT (htab != NULL);
9281
9282 s = htab->sgot;
9283 if (s == NULL)
9284 return TRUE;
9285
9286 dynobj = elf_hash_table (info)->dynobj;
9287 g = htab->got_info;
9288
9289 /* Allocate room for the reserved entries. VxWorks always reserves
9290 3 entries; other objects only reserve 2 entries. */
9291 BFD_ASSERT (g->assigned_low_gotno == 0);
9292 if (htab->is_vxworks)
9293 htab->reserved_gotno = 3;
9294 else
9295 htab->reserved_gotno = 2;
9296 g->local_gotno += htab->reserved_gotno;
9297 g->assigned_low_gotno = htab->reserved_gotno;
9298
9299 /* Decide which symbols need to go in the global part of the GOT and
9300 count the number of reloc-only GOT symbols. */
9301 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9302
9303 if (!mips_elf_resolve_final_got_entries (info, g))
9304 return FALSE;
9305
9306 /* Calculate the total loadable size of the output. That
9307 will give us the maximum number of GOT_PAGE entries
9308 required. */
9309 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9310 {
9311 asection *subsection;
9312
9313 for (subsection = ibfd->sections;
9314 subsection;
9315 subsection = subsection->next)
9316 {
9317 if ((subsection->flags & SEC_ALLOC) == 0)
9318 continue;
9319 loadable_size += ((subsection->size + 0xf)
9320 &~ (bfd_size_type) 0xf);
9321 }
9322 }
9323
9324 if (htab->is_vxworks)
9325 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9326 relocations against local symbols evaluate to "G", and the EABI does
9327 not include R_MIPS_GOT_PAGE. */
9328 page_gotno = 0;
9329 else
9330 /* Assume there are two loadable segments consisting of contiguous
9331 sections. Is 5 enough? */
9332 page_gotno = (loadable_size >> 16) + 5;
9333
9334 /* Choose the smaller of the two page estimates; both are intended to be
9335 conservative. */
9336 if (page_gotno > g->page_gotno)
9337 page_gotno = g->page_gotno;
9338
9339 g->local_gotno += page_gotno;
9340 g->assigned_high_gotno = g->local_gotno - 1;
9341
9342 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9343 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9344 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9345
9346 /* VxWorks does not support multiple GOTs. It initializes $gp to
9347 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9348 dynamic loader. */
9349 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9350 {
9351 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9352 return FALSE;
9353 }
9354 else
9355 {
9356 /* Record that all bfds use G. This also has the effect of freeing
9357 the per-bfd GOTs, which we no longer need. */
9358 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
9359 if (mips_elf_bfd_got (ibfd, FALSE))
9360 mips_elf_replace_bfd_got (ibfd, g);
9361 mips_elf_replace_bfd_got (output_bfd, g);
9362
9363 /* Set up TLS entries. */
9364 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9365 tga.info = info;
9366 tga.g = g;
9367 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9368 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9369 if (!tga.g)
9370 return FALSE;
9371 BFD_ASSERT (g->tls_assigned_gotno
9372 == g->global_gotno + g->local_gotno + g->tls_gotno);
9373
9374 /* Each VxWorks GOT entry needs an explicit relocation. */
9375 if (htab->is_vxworks && info->shared)
9376 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9377
9378 /* Allocate room for the TLS relocations. */
9379 if (g->relocs)
9380 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9381 }
9382
9383 return TRUE;
9384 }
9385
9386 /* Estimate the size of the .MIPS.stubs section. */
9387
9388 static void
mips_elf_estimate_stub_size(bfd * output_bfd,struct bfd_link_info * info)9389 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9390 {
9391 struct mips_elf_link_hash_table *htab;
9392 bfd_size_type dynsymcount;
9393
9394 htab = mips_elf_hash_table (info);
9395 BFD_ASSERT (htab != NULL);
9396
9397 if (htab->lazy_stub_count == 0)
9398 return;
9399
9400 /* IRIX rld assumes that a function stub isn't at the end of the .text
9401 section, so add a dummy entry to the end. */
9402 htab->lazy_stub_count++;
9403
9404 /* Get a worst-case estimate of the number of dynamic symbols needed.
9405 At this point, dynsymcount does not account for section symbols
9406 and count_section_dynsyms may overestimate the number that will
9407 be needed. */
9408 dynsymcount = (elf_hash_table (info)->dynsymcount
9409 + count_section_dynsyms (output_bfd, info));
9410
9411 /* Determine the size of one stub entry. There's no disadvantage
9412 from using microMIPS code here, so for the sake of pure-microMIPS
9413 binaries we prefer it whenever there's any microMIPS code in
9414 output produced at all. This has a benefit of stubs being
9415 shorter by 4 bytes each too, unless in the insn32 mode. */
9416 if (!MICROMIPS_P (output_bfd))
9417 htab->function_stub_size = (dynsymcount > 0x10000
9418 ? MIPS_FUNCTION_STUB_BIG_SIZE
9419 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9420 else if (htab->insn32)
9421 htab->function_stub_size = (dynsymcount > 0x10000
9422 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9423 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9424 else
9425 htab->function_stub_size = (dynsymcount > 0x10000
9426 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9427 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9428
9429 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9430 }
9431
9432 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9433 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9434 stub, allocate an entry in the stubs section. */
9435
9436 static bfd_boolean
mips_elf_allocate_lazy_stub(struct mips_elf_link_hash_entry * h,void * data)9437 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9438 {
9439 struct mips_htab_traverse_info *hti = data;
9440 struct mips_elf_link_hash_table *htab;
9441 struct bfd_link_info *info;
9442 bfd *output_bfd;
9443
9444 info = hti->info;
9445 output_bfd = hti->output_bfd;
9446 htab = mips_elf_hash_table (info);
9447 BFD_ASSERT (htab != NULL);
9448
9449 if (h->needs_lazy_stub)
9450 {
9451 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9452 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9453 bfd_vma isa_bit = micromips_p;
9454
9455 BFD_ASSERT (htab->root.dynobj != NULL);
9456 if (h->root.plt.plist == NULL)
9457 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9458 if (h->root.plt.plist == NULL)
9459 {
9460 hti->error = TRUE;
9461 return FALSE;
9462 }
9463 h->root.root.u.def.section = htab->sstubs;
9464 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9465 h->root.plt.plist->stub_offset = htab->sstubs->size;
9466 h->root.other = other;
9467 htab->sstubs->size += htab->function_stub_size;
9468 }
9469 return TRUE;
9470 }
9471
9472 /* Allocate offsets in the stubs section to each symbol that needs one.
9473 Set the final size of the .MIPS.stub section. */
9474
9475 static bfd_boolean
mips_elf_lay_out_lazy_stubs(struct bfd_link_info * info)9476 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9477 {
9478 bfd *output_bfd = info->output_bfd;
9479 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9480 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9481 bfd_vma isa_bit = micromips_p;
9482 struct mips_elf_link_hash_table *htab;
9483 struct mips_htab_traverse_info hti;
9484 struct elf_link_hash_entry *h;
9485 bfd *dynobj;
9486
9487 htab = mips_elf_hash_table (info);
9488 BFD_ASSERT (htab != NULL);
9489
9490 if (htab->lazy_stub_count == 0)
9491 return TRUE;
9492
9493 htab->sstubs->size = 0;
9494 hti.info = info;
9495 hti.output_bfd = output_bfd;
9496 hti.error = FALSE;
9497 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9498 if (hti.error)
9499 return FALSE;
9500 htab->sstubs->size += htab->function_stub_size;
9501 BFD_ASSERT (htab->sstubs->size
9502 == htab->lazy_stub_count * htab->function_stub_size);
9503
9504 dynobj = elf_hash_table (info)->dynobj;
9505 BFD_ASSERT (dynobj != NULL);
9506 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9507 if (h == NULL)
9508 return FALSE;
9509 h->root.u.def.value = isa_bit;
9510 h->other = other;
9511 h->type = STT_FUNC;
9512
9513 return TRUE;
9514 }
9515
9516 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9517 bfd_link_info. If H uses the address of a PLT entry as the value
9518 of the symbol, then set the entry in the symbol table now. Prefer
9519 a standard MIPS PLT entry. */
9520
9521 static bfd_boolean
mips_elf_set_plt_sym_value(struct mips_elf_link_hash_entry * h,void * data)9522 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9523 {
9524 struct bfd_link_info *info = data;
9525 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9526 struct mips_elf_link_hash_table *htab;
9527 unsigned int other;
9528 bfd_vma isa_bit;
9529 bfd_vma val;
9530
9531 htab = mips_elf_hash_table (info);
9532 BFD_ASSERT (htab != NULL);
9533
9534 if (h->use_plt_entry)
9535 {
9536 BFD_ASSERT (h->root.plt.plist != NULL);
9537 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9538 || h->root.plt.plist->comp_offset != MINUS_ONE);
9539
9540 val = htab->plt_header_size;
9541 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9542 {
9543 isa_bit = 0;
9544 val += h->root.plt.plist->mips_offset;
9545 other = 0;
9546 }
9547 else
9548 {
9549 isa_bit = 1;
9550 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9551 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9552 }
9553 val += isa_bit;
9554 /* For VxWorks, point at the PLT load stub rather than the lazy
9555 resolution stub; this stub will become the canonical function
9556 address. */
9557 if (htab->is_vxworks)
9558 val += 8;
9559
9560 h->root.root.u.def.section = htab->splt;
9561 h->root.root.u.def.value = val;
9562 h->root.other = other;
9563 }
9564
9565 return TRUE;
9566 }
9567
9568 /* Set the sizes of the dynamic sections. */
9569
9570 bfd_boolean
_bfd_mips_elf_size_dynamic_sections(bfd * output_bfd,struct bfd_link_info * info)9571 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9572 struct bfd_link_info *info)
9573 {
9574 bfd *dynobj;
9575 asection *s, *sreldyn;
9576 bfd_boolean reltext;
9577 struct mips_elf_link_hash_table *htab;
9578
9579 htab = mips_elf_hash_table (info);
9580 BFD_ASSERT (htab != NULL);
9581 dynobj = elf_hash_table (info)->dynobj;
9582 BFD_ASSERT (dynobj != NULL);
9583
9584 if (elf_hash_table (info)->dynamic_sections_created)
9585 {
9586 /* Set the contents of the .interp section to the interpreter. */
9587 if (info->executable)
9588 {
9589 s = bfd_get_linker_section (dynobj, ".interp");
9590 BFD_ASSERT (s != NULL);
9591 s->size
9592 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9593 s->contents
9594 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9595 }
9596
9597 /* Figure out the size of the PLT header if we know that we
9598 are using it. For the sake of cache alignment always use
9599 a standard header whenever any standard entries are present
9600 even if microMIPS entries are present as well. This also
9601 lets the microMIPS header rely on the value of $v0 only set
9602 by microMIPS entries, for a small size reduction.
9603
9604 Set symbol table entry values for symbols that use the
9605 address of their PLT entry now that we can calculate it.
9606
9607 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9608 haven't already in _bfd_elf_create_dynamic_sections. */
9609 if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
9610 {
9611 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9612 && !htab->plt_mips_offset);
9613 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9614 bfd_vma isa_bit = micromips_p;
9615 struct elf_link_hash_entry *h;
9616 bfd_vma size;
9617
9618 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9619 BFD_ASSERT (htab->sgotplt->size == 0);
9620 BFD_ASSERT (htab->splt->size == 0);
9621
9622 if (htab->is_vxworks && info->shared)
9623 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9624 else if (htab->is_vxworks)
9625 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9626 else if (ABI_64_P (output_bfd))
9627 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9628 else if (ABI_N32_P (output_bfd))
9629 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9630 else if (!micromips_p)
9631 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
9632 else if (htab->insn32)
9633 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
9634 else
9635 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
9636
9637 htab->plt_header_is_comp = micromips_p;
9638 htab->plt_header_size = size;
9639 htab->splt->size = (size
9640 + htab->plt_mips_offset
9641 + htab->plt_comp_offset);
9642 htab->sgotplt->size = (htab->plt_got_index
9643 * MIPS_ELF_GOT_SIZE (dynobj));
9644
9645 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9646
9647 if (htab->root.hplt == NULL)
9648 {
9649 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
9650 "_PROCEDURE_LINKAGE_TABLE_");
9651 htab->root.hplt = h;
9652 if (h == NULL)
9653 return FALSE;
9654 }
9655
9656 h = htab->root.hplt;
9657 h->root.u.def.value = isa_bit;
9658 h->other = other;
9659 h->type = STT_FUNC;
9660 }
9661 }
9662
9663 /* Allocate space for global sym dynamic relocs. */
9664 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9665
9666 mips_elf_estimate_stub_size (output_bfd, info);
9667
9668 if (!mips_elf_lay_out_got (output_bfd, info))
9669 return FALSE;
9670
9671 mips_elf_lay_out_lazy_stubs (info);
9672
9673 /* The check_relocs and adjust_dynamic_symbol entry points have
9674 determined the sizes of the various dynamic sections. Allocate
9675 memory for them. */
9676 reltext = FALSE;
9677 for (s = dynobj->sections; s != NULL; s = s->next)
9678 {
9679 const char *name;
9680
9681 /* It's OK to base decisions on the section name, because none
9682 of the dynobj section names depend upon the input files. */
9683 name = bfd_get_section_name (dynobj, s);
9684
9685 if ((s->flags & SEC_LINKER_CREATED) == 0)
9686 continue;
9687
9688 if (CONST_STRNEQ (name, ".rel"))
9689 {
9690 if (s->size != 0)
9691 {
9692 const char *outname;
9693 asection *target;
9694
9695 /* If this relocation section applies to a read only
9696 section, then we probably need a DT_TEXTREL entry.
9697 If the relocation section is .rel(a).dyn, we always
9698 assert a DT_TEXTREL entry rather than testing whether
9699 there exists a relocation to a read only section or
9700 not. */
9701 outname = bfd_get_section_name (output_bfd,
9702 s->output_section);
9703 target = bfd_get_section_by_name (output_bfd, outname + 4);
9704 if ((target != NULL
9705 && (target->flags & SEC_READONLY) != 0
9706 && (target->flags & SEC_ALLOC) != 0)
9707 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
9708 reltext = TRUE;
9709
9710 /* We use the reloc_count field as a counter if we need
9711 to copy relocs into the output file. */
9712 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
9713 s->reloc_count = 0;
9714
9715 /* If combreloc is enabled, elf_link_sort_relocs() will
9716 sort relocations, but in a different way than we do,
9717 and before we're done creating relocations. Also, it
9718 will move them around between input sections'
9719 relocation's contents, so our sorting would be
9720 broken, so don't let it run. */
9721 info->combreloc = 0;
9722 }
9723 }
9724 else if (info->executable
9725 && ! mips_elf_hash_table (info)->use_rld_obj_head
9726 && CONST_STRNEQ (name, ".rld_map"))
9727 {
9728 /* We add a room for __rld_map. It will be filled in by the
9729 rtld to contain a pointer to the _r_debug structure. */
9730 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
9731 }
9732 else if (SGI_COMPAT (output_bfd)
9733 && CONST_STRNEQ (name, ".compact_rel"))
9734 s->size += mips_elf_hash_table (info)->compact_rel_size;
9735 else if (s == htab->splt)
9736 {
9737 /* If the last PLT entry has a branch delay slot, allocate
9738 room for an extra nop to fill the delay slot. This is
9739 for CPUs without load interlocking. */
9740 if (! LOAD_INTERLOCKS_P (output_bfd)
9741 && ! htab->is_vxworks && s->size > 0)
9742 s->size += 4;
9743 }
9744 else if (! CONST_STRNEQ (name, ".init")
9745 && s != htab->sgot
9746 && s != htab->sgotplt
9747 && s != htab->sstubs
9748 && s != htab->sdynbss)
9749 {
9750 /* It's not one of our sections, so don't allocate space. */
9751 continue;
9752 }
9753
9754 if (s->size == 0)
9755 {
9756 s->flags |= SEC_EXCLUDE;
9757 continue;
9758 }
9759
9760 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9761 continue;
9762
9763 /* Allocate memory for the section contents. */
9764 s->contents = bfd_zalloc (dynobj, s->size);
9765 if (s->contents == NULL)
9766 {
9767 bfd_set_error (bfd_error_no_memory);
9768 return FALSE;
9769 }
9770 }
9771
9772 if (elf_hash_table (info)->dynamic_sections_created)
9773 {
9774 /* Add some entries to the .dynamic section. We fill in the
9775 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9776 must add the entries now so that we get the correct size for
9777 the .dynamic section. */
9778
9779 /* SGI object has the equivalence of DT_DEBUG in the
9780 DT_MIPS_RLD_MAP entry. This must come first because glibc
9781 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9782 may only look at the first one they see. */
9783 if (!info->shared
9784 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9785 return FALSE;
9786
9787 if (info->executable
9788 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP2, 0))
9789 return FALSE;
9790
9791 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9792 used by the debugger. */
9793 if (info->executable
9794 && !SGI_COMPAT (output_bfd)
9795 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9796 return FALSE;
9797
9798 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
9799 info->flags |= DF_TEXTREL;
9800
9801 if ((info->flags & DF_TEXTREL) != 0)
9802 {
9803 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
9804 return FALSE;
9805
9806 /* Clear the DF_TEXTREL flag. It will be set again if we
9807 write out an actual text relocation; we may not, because
9808 at this point we do not know whether e.g. any .eh_frame
9809 absolute relocations have been converted to PC-relative. */
9810 info->flags &= ~DF_TEXTREL;
9811 }
9812
9813 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
9814 return FALSE;
9815
9816 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
9817 if (htab->is_vxworks)
9818 {
9819 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9820 use any of the DT_MIPS_* tags. */
9821 if (sreldyn && sreldyn->size > 0)
9822 {
9823 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9824 return FALSE;
9825
9826 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9827 return FALSE;
9828
9829 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9830 return FALSE;
9831 }
9832 }
9833 else
9834 {
9835 if (sreldyn && sreldyn->size > 0)
9836 {
9837 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9838 return FALSE;
9839
9840 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9841 return FALSE;
9842
9843 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9844 return FALSE;
9845 }
9846
9847 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9848 return FALSE;
9849
9850 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9851 return FALSE;
9852
9853 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9854 return FALSE;
9855
9856 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9857 return FALSE;
9858
9859 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9860 return FALSE;
9861
9862 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9863 return FALSE;
9864
9865 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9866 return FALSE;
9867
9868 if (IRIX_COMPAT (dynobj) == ict_irix5
9869 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9870 return FALSE;
9871
9872 if (IRIX_COMPAT (dynobj) == ict_irix6
9873 && (bfd_get_section_by_name
9874 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
9875 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9876 return FALSE;
9877 }
9878 if (htab->splt->size > 0)
9879 {
9880 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9881 return FALSE;
9882
9883 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9884 return FALSE;
9885
9886 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9887 return FALSE;
9888
9889 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9890 return FALSE;
9891 }
9892 if (htab->is_vxworks
9893 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9894 return FALSE;
9895 }
9896
9897 return TRUE;
9898 }
9899
9900 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9901 Adjust its R_ADDEND field so that it is correct for the output file.
9902 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9903 and sections respectively; both use symbol indexes. */
9904
9905 static void
mips_elf_adjust_addend(bfd * output_bfd,struct bfd_link_info * info,bfd * input_bfd,Elf_Internal_Sym * local_syms,asection ** local_sections,Elf_Internal_Rela * rel)9906 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9907 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9908 asection **local_sections, Elf_Internal_Rela *rel)
9909 {
9910 unsigned int r_type, r_symndx;
9911 Elf_Internal_Sym *sym;
9912 asection *sec;
9913
9914 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9915 {
9916 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9917 if (gprel16_reloc_p (r_type)
9918 || r_type == R_MIPS_GPREL32
9919 || literal_reloc_p (r_type))
9920 {
9921 rel->r_addend += _bfd_get_gp_value (input_bfd);
9922 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9923 }
9924
9925 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9926 sym = local_syms + r_symndx;
9927
9928 /* Adjust REL's addend to account for section merging. */
9929 if (!info->relocatable)
9930 {
9931 sec = local_sections[r_symndx];
9932 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9933 }
9934
9935 /* This would normally be done by the rela_normal code in elflink.c. */
9936 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9937 rel->r_addend += local_sections[r_symndx]->output_offset;
9938 }
9939 }
9940
9941 /* Handle relocations against symbols from removed linkonce sections,
9942 or sections discarded by a linker script. We use this wrapper around
9943 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9944 on 64-bit ELF targets. In this case for any relocation handled, which
9945 always be the first in a triplet, the remaining two have to be processed
9946 together with the first, even if they are R_MIPS_NONE. It is the symbol
9947 index referred by the first reloc that applies to all the three and the
9948 remaining two never refer to an object symbol. And it is the final
9949 relocation (the last non-null one) that determines the output field of
9950 the whole relocation so retrieve the corresponding howto structure for
9951 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9952
9953 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9954 and therefore requires to be pasted in a loop. It also defines a block
9955 and does not protect any of its arguments, hence the extra brackets. */
9956
9957 static void
mips_reloc_against_discarded_section(bfd * output_bfd,struct bfd_link_info * info,bfd * input_bfd,asection * input_section,Elf_Internal_Rela ** rel,const Elf_Internal_Rela ** relend,bfd_boolean rel_reloc,reloc_howto_type * howto,bfd_byte * contents)9958 mips_reloc_against_discarded_section (bfd *output_bfd,
9959 struct bfd_link_info *info,
9960 bfd *input_bfd, asection *input_section,
9961 Elf_Internal_Rela **rel,
9962 const Elf_Internal_Rela **relend,
9963 bfd_boolean rel_reloc,
9964 reloc_howto_type *howto,
9965 bfd_byte *contents)
9966 {
9967 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9968 int count = bed->s->int_rels_per_ext_rel;
9969 unsigned int r_type;
9970 int i;
9971
9972 for (i = count - 1; i > 0; i--)
9973 {
9974 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9975 if (r_type != R_MIPS_NONE)
9976 {
9977 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9978 break;
9979 }
9980 }
9981 do
9982 {
9983 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9984 (*rel), count, (*relend),
9985 howto, i, contents);
9986 }
9987 while (0);
9988 }
9989
9990 /* Relocate a MIPS ELF section. */
9991
9992 bfd_boolean
_bfd_mips_elf_relocate_section(bfd * output_bfd,struct bfd_link_info * info,bfd * input_bfd,asection * input_section,bfd_byte * contents,Elf_Internal_Rela * relocs,Elf_Internal_Sym * local_syms,asection ** local_sections)9993 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9994 bfd *input_bfd, asection *input_section,
9995 bfd_byte *contents, Elf_Internal_Rela *relocs,
9996 Elf_Internal_Sym *local_syms,
9997 asection **local_sections)
9998 {
9999 Elf_Internal_Rela *rel;
10000 const Elf_Internal_Rela *relend;
10001 bfd_vma addend = 0;
10002 bfd_boolean use_saved_addend_p = FALSE;
10003 const struct elf_backend_data *bed;
10004
10005 bed = get_elf_backend_data (output_bfd);
10006 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
10007 for (rel = relocs; rel < relend; ++rel)
10008 {
10009 const char *name;
10010 bfd_vma value = 0;
10011 reloc_howto_type *howto;
10012 bfd_boolean cross_mode_jump_p = FALSE;
10013 /* TRUE if the relocation is a RELA relocation, rather than a
10014 REL relocation. */
10015 bfd_boolean rela_relocation_p = TRUE;
10016 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
10017 const char *msg;
10018 unsigned long r_symndx;
10019 asection *sec;
10020 Elf_Internal_Shdr *symtab_hdr;
10021 struct elf_link_hash_entry *h;
10022 bfd_boolean rel_reloc;
10023
10024 rel_reloc = (NEWABI_P (input_bfd)
10025 && mips_elf_rel_relocation_p (input_bfd, input_section,
10026 relocs, rel));
10027 /* Find the relocation howto for this relocation. */
10028 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10029
10030 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
10031 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10032 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
10033 {
10034 sec = local_sections[r_symndx];
10035 h = NULL;
10036 }
10037 else
10038 {
10039 unsigned long extsymoff;
10040
10041 extsymoff = 0;
10042 if (!elf_bad_symtab (input_bfd))
10043 extsymoff = symtab_hdr->sh_info;
10044 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10045 while (h->root.type == bfd_link_hash_indirect
10046 || h->root.type == bfd_link_hash_warning)
10047 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10048
10049 sec = NULL;
10050 if (h->root.type == bfd_link_hash_defined
10051 || h->root.type == bfd_link_hash_defweak)
10052 sec = h->root.u.def.section;
10053 }
10054
10055 if (sec != NULL && discarded_section (sec))
10056 {
10057 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10058 input_section, &rel, &relend,
10059 rel_reloc, howto, contents);
10060 continue;
10061 }
10062
10063 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
10064 {
10065 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10066 64-bit code, but make sure all their addresses are in the
10067 lowermost or uppermost 32-bit section of the 64-bit address
10068 space. Thus, when they use an R_MIPS_64 they mean what is
10069 usually meant by R_MIPS_32, with the exception that the
10070 stored value is sign-extended to 64 bits. */
10071 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
10072
10073 /* On big-endian systems, we need to lie about the position
10074 of the reloc. */
10075 if (bfd_big_endian (input_bfd))
10076 rel->r_offset += 4;
10077 }
10078
10079 if (!use_saved_addend_p)
10080 {
10081 /* If these relocations were originally of the REL variety,
10082 we must pull the addend out of the field that will be
10083 relocated. Otherwise, we simply use the contents of the
10084 RELA relocation. */
10085 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10086 relocs, rel))
10087 {
10088 rela_relocation_p = FALSE;
10089 addend = mips_elf_read_rel_addend (input_bfd, rel,
10090 howto, contents);
10091 if (hi16_reloc_p (r_type)
10092 || (got16_reloc_p (r_type)
10093 && mips_elf_local_relocation_p (input_bfd, rel,
10094 local_sections)))
10095 {
10096 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
10097 contents, &addend))
10098 {
10099 if (h)
10100 name = h->root.root.string;
10101 else
10102 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10103 local_syms + r_symndx,
10104 sec);
10105 (*_bfd_error_handler)
10106 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
10107 input_bfd, input_section, name, howto->name,
10108 rel->r_offset);
10109 }
10110 }
10111 else
10112 addend <<= howto->rightshift;
10113 }
10114 else
10115 addend = rel->r_addend;
10116 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10117 local_syms, local_sections, rel);
10118 }
10119
10120 if (info->relocatable)
10121 {
10122 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
10123 && bfd_big_endian (input_bfd))
10124 rel->r_offset -= 4;
10125
10126 if (!rela_relocation_p && rel->r_addend)
10127 {
10128 addend += rel->r_addend;
10129 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
10130 addend = mips_elf_high (addend);
10131 else if (r_type == R_MIPS_HIGHER)
10132 addend = mips_elf_higher (addend);
10133 else if (r_type == R_MIPS_HIGHEST)
10134 addend = mips_elf_highest (addend);
10135 else
10136 addend >>= howto->rightshift;
10137
10138 /* We use the source mask, rather than the destination
10139 mask because the place to which we are writing will be
10140 source of the addend in the final link. */
10141 addend &= howto->src_mask;
10142
10143 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10144 /* See the comment above about using R_MIPS_64 in the 32-bit
10145 ABI. Here, we need to update the addend. It would be
10146 possible to get away with just using the R_MIPS_32 reloc
10147 but for endianness. */
10148 {
10149 bfd_vma sign_bits;
10150 bfd_vma low_bits;
10151 bfd_vma high_bits;
10152
10153 if (addend & ((bfd_vma) 1 << 31))
10154 #ifdef BFD64
10155 sign_bits = ((bfd_vma) 1 << 32) - 1;
10156 #else
10157 sign_bits = -1;
10158 #endif
10159 else
10160 sign_bits = 0;
10161
10162 /* If we don't know that we have a 64-bit type,
10163 do two separate stores. */
10164 if (bfd_big_endian (input_bfd))
10165 {
10166 /* Store the sign-bits (which are most significant)
10167 first. */
10168 low_bits = sign_bits;
10169 high_bits = addend;
10170 }
10171 else
10172 {
10173 low_bits = addend;
10174 high_bits = sign_bits;
10175 }
10176 bfd_put_32 (input_bfd, low_bits,
10177 contents + rel->r_offset);
10178 bfd_put_32 (input_bfd, high_bits,
10179 contents + rel->r_offset + 4);
10180 continue;
10181 }
10182
10183 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10184 input_bfd, input_section,
10185 contents, FALSE))
10186 return FALSE;
10187 }
10188
10189 /* Go on to the next relocation. */
10190 continue;
10191 }
10192
10193 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10194 relocations for the same offset. In that case we are
10195 supposed to treat the output of each relocation as the addend
10196 for the next. */
10197 if (rel + 1 < relend
10198 && rel->r_offset == rel[1].r_offset
10199 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
10200 use_saved_addend_p = TRUE;
10201 else
10202 use_saved_addend_p = FALSE;
10203
10204 /* Figure out what value we are supposed to relocate. */
10205 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
10206 input_section, info, rel,
10207 addend, howto, local_syms,
10208 local_sections, &value,
10209 &name, &cross_mode_jump_p,
10210 use_saved_addend_p))
10211 {
10212 case bfd_reloc_continue:
10213 /* There's nothing to do. */
10214 continue;
10215
10216 case bfd_reloc_undefined:
10217 /* mips_elf_calculate_relocation already called the
10218 undefined_symbol callback. There's no real point in
10219 trying to perform the relocation at this point, so we
10220 just skip ahead to the next relocation. */
10221 continue;
10222
10223 case bfd_reloc_notsupported:
10224 msg = _("internal error: unsupported relocation error");
10225 info->callbacks->warning
10226 (info, msg, name, input_bfd, input_section, rel->r_offset);
10227 return FALSE;
10228
10229 case bfd_reloc_overflow:
10230 if (use_saved_addend_p)
10231 /* Ignore overflow until we reach the last relocation for
10232 a given location. */
10233 ;
10234 else
10235 {
10236 struct mips_elf_link_hash_table *htab;
10237
10238 htab = mips_elf_hash_table (info);
10239 BFD_ASSERT (htab != NULL);
10240 BFD_ASSERT (name != NULL);
10241 if (!htab->small_data_overflow_reported
10242 && (gprel16_reloc_p (howto->type)
10243 || literal_reloc_p (howto->type)))
10244 {
10245 msg = _("small-data section exceeds 64KB;"
10246 " lower small-data size limit (see option -G)");
10247
10248 htab->small_data_overflow_reported = TRUE;
10249 (*info->callbacks->einfo) ("%P: %s\n", msg);
10250 }
10251 if (! ((*info->callbacks->reloc_overflow)
10252 (info, NULL, name, howto->name, (bfd_vma) 0,
10253 input_bfd, input_section, rel->r_offset)))
10254 return FALSE;
10255 }
10256 break;
10257
10258 case bfd_reloc_ok:
10259 break;
10260
10261 case bfd_reloc_outofrange:
10262 if (jal_reloc_p (howto->type))
10263 {
10264 msg = _("JALX to a non-word-aligned address");
10265 info->callbacks->warning
10266 (info, msg, name, input_bfd, input_section, rel->r_offset);
10267 return FALSE;
10268 }
10269 if (aligned_pcrel_reloc_p (howto->type))
10270 {
10271 msg = _("PC-relative load from unaligned address");
10272 info->callbacks->warning
10273 (info, msg, name, input_bfd, input_section, rel->r_offset);
10274 return FALSE;
10275 }
10276 /* Fall through. */
10277
10278 default:
10279 abort ();
10280 break;
10281 }
10282
10283 /* If we've got another relocation for the address, keep going
10284 until we reach the last one. */
10285 if (use_saved_addend_p)
10286 {
10287 addend = value;
10288 continue;
10289 }
10290
10291 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10292 /* See the comment above about using R_MIPS_64 in the 32-bit
10293 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10294 that calculated the right value. Now, however, we
10295 sign-extend the 32-bit result to 64-bits, and store it as a
10296 64-bit value. We are especially generous here in that we
10297 go to extreme lengths to support this usage on systems with
10298 only a 32-bit VMA. */
10299 {
10300 bfd_vma sign_bits;
10301 bfd_vma low_bits;
10302 bfd_vma high_bits;
10303
10304 if (value & ((bfd_vma) 1 << 31))
10305 #ifdef BFD64
10306 sign_bits = ((bfd_vma) 1 << 32) - 1;
10307 #else
10308 sign_bits = -1;
10309 #endif
10310 else
10311 sign_bits = 0;
10312
10313 /* If we don't know that we have a 64-bit type,
10314 do two separate stores. */
10315 if (bfd_big_endian (input_bfd))
10316 {
10317 /* Undo what we did above. */
10318 rel->r_offset -= 4;
10319 /* Store the sign-bits (which are most significant)
10320 first. */
10321 low_bits = sign_bits;
10322 high_bits = value;
10323 }
10324 else
10325 {
10326 low_bits = value;
10327 high_bits = sign_bits;
10328 }
10329 bfd_put_32 (input_bfd, low_bits,
10330 contents + rel->r_offset);
10331 bfd_put_32 (input_bfd, high_bits,
10332 contents + rel->r_offset + 4);
10333 continue;
10334 }
10335
10336 /* Actually perform the relocation. */
10337 if (! mips_elf_perform_relocation (info, howto, rel, value,
10338 input_bfd, input_section,
10339 contents, cross_mode_jump_p))
10340 return FALSE;
10341 }
10342
10343 return TRUE;
10344 }
10345
10346 /* A function that iterates over each entry in la25_stubs and fills
10347 in the code for each one. DATA points to a mips_htab_traverse_info. */
10348
10349 static int
mips_elf_create_la25_stub(void ** slot,void * data)10350 mips_elf_create_la25_stub (void **slot, void *data)
10351 {
10352 struct mips_htab_traverse_info *hti;
10353 struct mips_elf_link_hash_table *htab;
10354 struct mips_elf_la25_stub *stub;
10355 asection *s;
10356 bfd_byte *loc;
10357 bfd_vma offset, target, target_high, target_low;
10358
10359 stub = (struct mips_elf_la25_stub *) *slot;
10360 hti = (struct mips_htab_traverse_info *) data;
10361 htab = mips_elf_hash_table (hti->info);
10362 BFD_ASSERT (htab != NULL);
10363
10364 /* Create the section contents, if we haven't already. */
10365 s = stub->stub_section;
10366 loc = s->contents;
10367 if (loc == NULL)
10368 {
10369 loc = bfd_malloc (s->size);
10370 if (loc == NULL)
10371 {
10372 hti->error = TRUE;
10373 return FALSE;
10374 }
10375 s->contents = loc;
10376 }
10377
10378 /* Work out where in the section this stub should go. */
10379 offset = stub->offset;
10380
10381 /* Work out the target address. */
10382 target = mips_elf_get_la25_target (stub, &s);
10383 target += s->output_section->vma + s->output_offset;
10384
10385 target_high = ((target + 0x8000) >> 16) & 0xffff;
10386 target_low = (target & 0xffff);
10387
10388 if (stub->stub_section != htab->strampoline)
10389 {
10390 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10391 of the section and write the two instructions at the end. */
10392 memset (loc, 0, offset);
10393 loc += offset;
10394 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10395 {
10396 bfd_put_micromips_32 (hti->output_bfd,
10397 LA25_LUI_MICROMIPS (target_high),
10398 loc);
10399 bfd_put_micromips_32 (hti->output_bfd,
10400 LA25_ADDIU_MICROMIPS (target_low),
10401 loc + 4);
10402 }
10403 else
10404 {
10405 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10406 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10407 }
10408 }
10409 else
10410 {
10411 /* This is trampoline. */
10412 loc += offset;
10413 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10414 {
10415 bfd_put_micromips_32 (hti->output_bfd,
10416 LA25_LUI_MICROMIPS (target_high), loc);
10417 bfd_put_micromips_32 (hti->output_bfd,
10418 LA25_J_MICROMIPS (target), loc + 4);
10419 bfd_put_micromips_32 (hti->output_bfd,
10420 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10421 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10422 }
10423 else
10424 {
10425 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10426 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10427 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10428 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10429 }
10430 }
10431 return TRUE;
10432 }
10433
10434 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10435 adjust it appropriately now. */
10436
10437 static void
mips_elf_irix6_finish_dynamic_symbol(bfd * abfd ATTRIBUTE_UNUSED,const char * name,Elf_Internal_Sym * sym)10438 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10439 const char *name, Elf_Internal_Sym *sym)
10440 {
10441 /* The linker script takes care of providing names and values for
10442 these, but we must place them into the right sections. */
10443 static const char* const text_section_symbols[] = {
10444 "_ftext",
10445 "_etext",
10446 "__dso_displacement",
10447 "__elf_header",
10448 "__program_header_table",
10449 NULL
10450 };
10451
10452 static const char* const data_section_symbols[] = {
10453 "_fdata",
10454 "_edata",
10455 "_end",
10456 "_fbss",
10457 NULL
10458 };
10459
10460 const char* const *p;
10461 int i;
10462
10463 for (i = 0; i < 2; ++i)
10464 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10465 *p;
10466 ++p)
10467 if (strcmp (*p, name) == 0)
10468 {
10469 /* All of these symbols are given type STT_SECTION by the
10470 IRIX6 linker. */
10471 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10472 sym->st_other = STO_PROTECTED;
10473
10474 /* The IRIX linker puts these symbols in special sections. */
10475 if (i == 0)
10476 sym->st_shndx = SHN_MIPS_TEXT;
10477 else
10478 sym->st_shndx = SHN_MIPS_DATA;
10479
10480 break;
10481 }
10482 }
10483
10484 /* Finish up dynamic symbol handling. We set the contents of various
10485 dynamic sections here. */
10486
10487 bfd_boolean
_bfd_mips_elf_finish_dynamic_symbol(bfd * output_bfd,struct bfd_link_info * info,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)10488 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10489 struct bfd_link_info *info,
10490 struct elf_link_hash_entry *h,
10491 Elf_Internal_Sym *sym)
10492 {
10493 bfd *dynobj;
10494 asection *sgot;
10495 struct mips_got_info *g, *gg;
10496 const char *name;
10497 int idx;
10498 struct mips_elf_link_hash_table *htab;
10499 struct mips_elf_link_hash_entry *hmips;
10500
10501 htab = mips_elf_hash_table (info);
10502 BFD_ASSERT (htab != NULL);
10503 dynobj = elf_hash_table (info)->dynobj;
10504 hmips = (struct mips_elf_link_hash_entry *) h;
10505
10506 BFD_ASSERT (!htab->is_vxworks);
10507
10508 if (h->plt.plist != NULL
10509 && (h->plt.plist->mips_offset != MINUS_ONE
10510 || h->plt.plist->comp_offset != MINUS_ONE))
10511 {
10512 /* We've decided to create a PLT entry for this symbol. */
10513 bfd_byte *loc;
10514 bfd_vma header_address, got_address;
10515 bfd_vma got_address_high, got_address_low, load;
10516 bfd_vma got_index;
10517 bfd_vma isa_bit;
10518
10519 got_index = h->plt.plist->gotplt_index;
10520
10521 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10522 BFD_ASSERT (h->dynindx != -1);
10523 BFD_ASSERT (htab->splt != NULL);
10524 BFD_ASSERT (got_index != MINUS_ONE);
10525 BFD_ASSERT (!h->def_regular);
10526
10527 /* Calculate the address of the PLT header. */
10528 isa_bit = htab->plt_header_is_comp;
10529 header_address = (htab->splt->output_section->vma
10530 + htab->splt->output_offset + isa_bit);
10531
10532 /* Calculate the address of the .got.plt entry. */
10533 got_address = (htab->sgotplt->output_section->vma
10534 + htab->sgotplt->output_offset
10535 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10536
10537 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10538 got_address_low = got_address & 0xffff;
10539
10540 /* Initially point the .got.plt entry at the PLT header. */
10541 loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10542 if (ABI_64_P (output_bfd))
10543 bfd_put_64 (output_bfd, header_address, loc);
10544 else
10545 bfd_put_32 (output_bfd, header_address, loc);
10546
10547 /* Now handle the PLT itself. First the standard entry (the order
10548 does not matter, we just have to pick one). */
10549 if (h->plt.plist->mips_offset != MINUS_ONE)
10550 {
10551 const bfd_vma *plt_entry;
10552 bfd_vma plt_offset;
10553
10554 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10555
10556 BFD_ASSERT (plt_offset <= htab->splt->size);
10557
10558 /* Find out where the .plt entry should go. */
10559 loc = htab->splt->contents + plt_offset;
10560
10561 /* Pick the load opcode. */
10562 load = MIPS_ELF_LOAD_WORD (output_bfd);
10563
10564 /* Fill in the PLT entry itself. */
10565
10566 if (MIPSR6_P (output_bfd)
10567 && mips_elf_hash_table (info)->compact_branches)
10568 plt_entry = mipsr6_exec_plt_entry_compact;
10569 else if (MIPSR6_P (output_bfd))
10570 plt_entry = mipsr6_exec_plt_entry;
10571 else
10572 plt_entry = mips_exec_plt_entry;
10573 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10574 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10575 loc + 4);
10576
10577 if (! LOAD_INTERLOCKS_P (output_bfd) || MIPSR6_P (output_bfd))
10578 {
10579 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10580 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10581 }
10582 else
10583 {
10584 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10585 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10586 loc + 12);
10587 }
10588 }
10589
10590 /* Now the compressed entry. They come after any standard ones. */
10591 if (h->plt.plist->comp_offset != MINUS_ONE)
10592 {
10593 bfd_vma plt_offset;
10594
10595 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10596 + h->plt.plist->comp_offset);
10597
10598 BFD_ASSERT (plt_offset <= htab->splt->size);
10599
10600 /* Find out where the .plt entry should go. */
10601 loc = htab->splt->contents + plt_offset;
10602
10603 /* Fill in the PLT entry itself. */
10604 if (!MICROMIPS_P (output_bfd))
10605 {
10606 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10607
10608 bfd_put_16 (output_bfd, plt_entry[0], loc);
10609 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10610 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10611 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10612 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10613 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10614 bfd_put_32 (output_bfd, got_address, loc + 12);
10615 }
10616 else if (htab->insn32)
10617 {
10618 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10619
10620 bfd_put_16 (output_bfd, plt_entry[0], loc);
10621 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10622 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10623 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10624 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10625 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10626 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10627 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10628 }
10629 else
10630 {
10631 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10632 bfd_signed_vma gotpc_offset;
10633 bfd_vma loc_address;
10634
10635 BFD_ASSERT (got_address % 4 == 0);
10636
10637 loc_address = (htab->splt->output_section->vma
10638 + htab->splt->output_offset + plt_offset);
10639 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10640
10641 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10642 if (gotpc_offset + 0x1000000 >= 0x2000000)
10643 {
10644 (*_bfd_error_handler)
10645 (_("%B: `%A' offset of %ld from `%A' "
10646 "beyond the range of ADDIUPC"),
10647 output_bfd,
10648 htab->sgotplt->output_section,
10649 htab->splt->output_section,
10650 (long) gotpc_offset);
10651 bfd_set_error (bfd_error_no_error);
10652 return FALSE;
10653 }
10654 bfd_put_16 (output_bfd,
10655 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10656 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10657 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10658 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10659 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10660 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10661 }
10662 }
10663
10664 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10665 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
10666 got_index - 2, h->dynindx,
10667 R_MIPS_JUMP_SLOT, got_address);
10668
10669 /* We distinguish between PLT entries and lazy-binding stubs by
10670 giving the former an st_other value of STO_MIPS_PLT. Set the
10671 flag and leave the value if there are any relocations in the
10672 binary where pointer equality matters. */
10673 sym->st_shndx = SHN_UNDEF;
10674 if (h->pointer_equality_needed)
10675 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
10676 else
10677 {
10678 sym->st_value = 0;
10679 sym->st_other = 0;
10680 }
10681 }
10682
10683 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
10684 {
10685 /* We've decided to create a lazy-binding stub. */
10686 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10687 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10688 bfd_vma stub_size = htab->function_stub_size;
10689 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
10690 bfd_vma isa_bit = micromips_p;
10691 bfd_vma stub_big_size;
10692
10693 if (!micromips_p)
10694 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
10695 else if (htab->insn32)
10696 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10697 else
10698 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
10699
10700 /* This symbol has a stub. Set it up. */
10701
10702 BFD_ASSERT (h->dynindx != -1);
10703
10704 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
10705
10706 /* Values up to 2^31 - 1 are allowed. Larger values would cause
10707 sign extension at runtime in the stub, resulting in a negative
10708 index value. */
10709 if (h->dynindx & ~0x7fffffff)
10710 return FALSE;
10711
10712 /* Fill the stub. */
10713 if (micromips_p)
10714 {
10715 idx = 0;
10716 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
10717 stub + idx);
10718 idx += 4;
10719 if (htab->insn32)
10720 {
10721 bfd_put_micromips_32 (output_bfd,
10722 STUB_MOVE32_MICROMIPS (output_bfd),
10723 stub + idx);
10724 idx += 4;
10725 }
10726 else
10727 {
10728 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
10729 idx += 2;
10730 }
10731 if (stub_size == stub_big_size)
10732 {
10733 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
10734
10735 bfd_put_micromips_32 (output_bfd,
10736 STUB_LUI_MICROMIPS (dynindx_hi),
10737 stub + idx);
10738 idx += 4;
10739 }
10740 if (htab->insn32)
10741 {
10742 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
10743 stub + idx);
10744 idx += 4;
10745 }
10746 else
10747 {
10748 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
10749 idx += 2;
10750 }
10751
10752 /* If a large stub is not required and sign extension is not a
10753 problem, then use legacy code in the stub. */
10754 if (stub_size == stub_big_size)
10755 bfd_put_micromips_32 (output_bfd,
10756 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
10757 stub + idx);
10758 else if (h->dynindx & ~0x7fff)
10759 bfd_put_micromips_32 (output_bfd,
10760 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
10761 stub + idx);
10762 else
10763 bfd_put_micromips_32 (output_bfd,
10764 STUB_LI16S_MICROMIPS (output_bfd,
10765 h->dynindx),
10766 stub + idx);
10767 }
10768 else
10769 {
10770 idx = 0;
10771 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
10772 idx += 4;
10773 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
10774 idx += 4;
10775 if (stub_size == stub_big_size)
10776 {
10777 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
10778 stub + idx);
10779 idx += 4;
10780 }
10781 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
10782 idx += 4;
10783
10784 /* If a large stub is not required and sign extension is not a
10785 problem, then use legacy code in the stub. */
10786 if (stub_size == stub_big_size)
10787 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
10788 stub + idx);
10789 else if (h->dynindx & ~0x7fff)
10790 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
10791 stub + idx);
10792 else
10793 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
10794 stub + idx);
10795 }
10796
10797 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
10798 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
10799 stub, stub_size);
10800
10801 /* Mark the symbol as undefined. stub_offset != -1 occurs
10802 only for the referenced symbol. */
10803 sym->st_shndx = SHN_UNDEF;
10804
10805 /* The run-time linker uses the st_value field of the symbol
10806 to reset the global offset table entry for this external
10807 to its stub address when unlinking a shared object. */
10808 sym->st_value = (htab->sstubs->output_section->vma
10809 + htab->sstubs->output_offset
10810 + h->plt.plist->stub_offset
10811 + isa_bit);
10812 sym->st_other = other;
10813 }
10814
10815 /* If we have a MIPS16 function with a stub, the dynamic symbol must
10816 refer to the stub, since only the stub uses the standard calling
10817 conventions. */
10818 if (h->dynindx != -1 && hmips->fn_stub != NULL)
10819 {
10820 BFD_ASSERT (hmips->need_fn_stub);
10821 sym->st_value = (hmips->fn_stub->output_section->vma
10822 + hmips->fn_stub->output_offset);
10823 sym->st_size = hmips->fn_stub->size;
10824 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
10825 }
10826
10827 BFD_ASSERT (h->dynindx != -1
10828 || h->forced_local);
10829
10830 sgot = htab->sgot;
10831 g = htab->got_info;
10832 BFD_ASSERT (g != NULL);
10833
10834 /* Run through the global symbol table, creating GOT entries for all
10835 the symbols that need them. */
10836 if (hmips->global_got_area != GGA_NONE)
10837 {
10838 bfd_vma offset;
10839 bfd_vma value;
10840
10841 value = sym->st_value;
10842 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10843 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
10844 }
10845
10846 if (hmips->global_got_area != GGA_NONE && g->next)
10847 {
10848 struct mips_got_entry e, *p;
10849 bfd_vma entry;
10850 bfd_vma offset;
10851
10852 gg = g;
10853
10854 e.abfd = output_bfd;
10855 e.symndx = -1;
10856 e.d.h = hmips;
10857 e.tls_type = GOT_TLS_NONE;
10858
10859 for (g = g->next; g->next != gg; g = g->next)
10860 {
10861 if (g->got_entries
10862 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10863 &e)))
10864 {
10865 offset = p->gotidx;
10866 BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
10867 if (info->shared
10868 || (elf_hash_table (info)->dynamic_sections_created
10869 && p->d.h != NULL
10870 && p->d.h->root.def_dynamic
10871 && !p->d.h->root.def_regular))
10872 {
10873 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10874 the various compatibility problems, it's easier to mock
10875 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10876 mips_elf_create_dynamic_relocation to calculate the
10877 appropriate addend. */
10878 Elf_Internal_Rela rel[3];
10879
10880 memset (rel, 0, sizeof (rel));
10881 if (ABI_64_P (output_bfd))
10882 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10883 else
10884 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10885 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10886
10887 entry = 0;
10888 if (! (mips_elf_create_dynamic_relocation
10889 (output_bfd, info, rel,
10890 e.d.h, NULL, sym->st_value, &entry, sgot)))
10891 return FALSE;
10892 }
10893 else
10894 entry = sym->st_value;
10895 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
10896 }
10897 }
10898 }
10899
10900 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10901 name = h->root.root.string;
10902 if (h == elf_hash_table (info)->hdynamic
10903 || h == elf_hash_table (info)->hgot)
10904 sym->st_shndx = SHN_ABS;
10905 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10906 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10907 {
10908 sym->st_shndx = SHN_ABS;
10909 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10910 sym->st_value = 1;
10911 }
10912 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
10913 {
10914 sym->st_shndx = SHN_ABS;
10915 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10916 sym->st_value = elf_gp (output_bfd);
10917 }
10918 else if (SGI_COMPAT (output_bfd))
10919 {
10920 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10921 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10922 {
10923 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10924 sym->st_other = STO_PROTECTED;
10925 sym->st_value = 0;
10926 sym->st_shndx = SHN_MIPS_DATA;
10927 }
10928 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10929 {
10930 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10931 sym->st_other = STO_PROTECTED;
10932 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10933 sym->st_shndx = SHN_ABS;
10934 }
10935 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10936 {
10937 if (h->type == STT_FUNC)
10938 sym->st_shndx = SHN_MIPS_TEXT;
10939 else if (h->type == STT_OBJECT)
10940 sym->st_shndx = SHN_MIPS_DATA;
10941 }
10942 }
10943
10944 /* Emit a copy reloc, if needed. */
10945 if (h->needs_copy)
10946 {
10947 asection *s;
10948 bfd_vma symval;
10949
10950 BFD_ASSERT (h->dynindx != -1);
10951 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10952
10953 s = mips_elf_rel_dyn_section (info, FALSE);
10954 symval = (h->root.u.def.section->output_section->vma
10955 + h->root.u.def.section->output_offset
10956 + h->root.u.def.value);
10957 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10958 h->dynindx, R_MIPS_COPY, symval);
10959 }
10960
10961 /* Handle the IRIX6-specific symbols. */
10962 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10963 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10964
10965 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
10966 to treat compressed symbols like any other. */
10967 if (ELF_ST_IS_MIPS16 (sym->st_other))
10968 {
10969 BFD_ASSERT (sym->st_value & 1);
10970 sym->st_other -= STO_MIPS16;
10971 }
10972 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
10973 {
10974 BFD_ASSERT (sym->st_value & 1);
10975 sym->st_other -= STO_MICROMIPS;
10976 }
10977
10978 return TRUE;
10979 }
10980
10981 /* Likewise, for VxWorks. */
10982
10983 bfd_boolean
_bfd_mips_vxworks_finish_dynamic_symbol(bfd * output_bfd,struct bfd_link_info * info,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)10984 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10985 struct bfd_link_info *info,
10986 struct elf_link_hash_entry *h,
10987 Elf_Internal_Sym *sym)
10988 {
10989 bfd *dynobj;
10990 asection *sgot;
10991 struct mips_got_info *g;
10992 struct mips_elf_link_hash_table *htab;
10993 struct mips_elf_link_hash_entry *hmips;
10994
10995 htab = mips_elf_hash_table (info);
10996 BFD_ASSERT (htab != NULL);
10997 dynobj = elf_hash_table (info)->dynobj;
10998 hmips = (struct mips_elf_link_hash_entry *) h;
10999
11000 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
11001 {
11002 bfd_byte *loc;
11003 bfd_vma plt_address, got_address, got_offset, branch_offset;
11004 Elf_Internal_Rela rel;
11005 static const bfd_vma *plt_entry;
11006 bfd_vma gotplt_index;
11007 bfd_vma plt_offset;
11008
11009 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11010 gotplt_index = h->plt.plist->gotplt_index;
11011
11012 BFD_ASSERT (h->dynindx != -1);
11013 BFD_ASSERT (htab->splt != NULL);
11014 BFD_ASSERT (gotplt_index != MINUS_ONE);
11015 BFD_ASSERT (plt_offset <= htab->splt->size);
11016
11017 /* Calculate the address of the .plt entry. */
11018 plt_address = (htab->splt->output_section->vma
11019 + htab->splt->output_offset
11020 + plt_offset);
11021
11022 /* Calculate the address of the .got.plt entry. */
11023 got_address = (htab->sgotplt->output_section->vma
11024 + htab->sgotplt->output_offset
11025 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
11026
11027 /* Calculate the offset of the .got.plt entry from
11028 _GLOBAL_OFFSET_TABLE_. */
11029 got_offset = mips_elf_gotplt_index (info, h);
11030
11031 /* Calculate the offset for the branch at the start of the PLT
11032 entry. The branch jumps to the beginning of .plt. */
11033 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
11034
11035 /* Fill in the initial value of the .got.plt entry. */
11036 bfd_put_32 (output_bfd, plt_address,
11037 (htab->sgotplt->contents
11038 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
11039
11040 /* Find out where the .plt entry should go. */
11041 loc = htab->splt->contents + plt_offset;
11042
11043 if (info->shared)
11044 {
11045 plt_entry = mips_vxworks_shared_plt_entry;
11046 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11047 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11048 }
11049 else
11050 {
11051 bfd_vma got_address_high, got_address_low;
11052
11053 plt_entry = mips_vxworks_exec_plt_entry;
11054 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11055 got_address_low = got_address & 0xffff;
11056
11057 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
11058 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
11059 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11060 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11061 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11062 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11063 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11064 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11065
11066 loc = (htab->srelplt2->contents
11067 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
11068
11069 /* Emit a relocation for the .got.plt entry. */
11070 rel.r_offset = got_address;
11071 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11072 rel.r_addend = plt_offset;
11073 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11074
11075 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11076 loc += sizeof (Elf32_External_Rela);
11077 rel.r_offset = plt_address + 8;
11078 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11079 rel.r_addend = got_offset;
11080 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11081
11082 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11083 loc += sizeof (Elf32_External_Rela);
11084 rel.r_offset += 4;
11085 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11086 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11087 }
11088
11089 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
11090 loc = (htab->srelplt->contents
11091 + gotplt_index * sizeof (Elf32_External_Rela));
11092 rel.r_offset = got_address;
11093 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11094 rel.r_addend = 0;
11095 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11096
11097 if (!h->def_regular)
11098 sym->st_shndx = SHN_UNDEF;
11099 }
11100
11101 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11102
11103 sgot = htab->sgot;
11104 g = htab->got_info;
11105 BFD_ASSERT (g != NULL);
11106
11107 /* See if this symbol has an entry in the GOT. */
11108 if (hmips->global_got_area != GGA_NONE)
11109 {
11110 bfd_vma offset;
11111 Elf_Internal_Rela outrel;
11112 bfd_byte *loc;
11113 asection *s;
11114
11115 /* Install the symbol value in the GOT. */
11116 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
11117 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11118
11119 /* Add a dynamic relocation for it. */
11120 s = mips_elf_rel_dyn_section (info, FALSE);
11121 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11122 outrel.r_offset = (sgot->output_section->vma
11123 + sgot->output_offset
11124 + offset);
11125 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11126 outrel.r_addend = 0;
11127 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11128 }
11129
11130 /* Emit a copy reloc, if needed. */
11131 if (h->needs_copy)
11132 {
11133 Elf_Internal_Rela rel;
11134
11135 BFD_ASSERT (h->dynindx != -1);
11136
11137 rel.r_offset = (h->root.u.def.section->output_section->vma
11138 + h->root.u.def.section->output_offset
11139 + h->root.u.def.value);
11140 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11141 rel.r_addend = 0;
11142 bfd_elf32_swap_reloca_out (output_bfd, &rel,
11143 htab->srelbss->contents
11144 + (htab->srelbss->reloc_count
11145 * sizeof (Elf32_External_Rela)));
11146 ++htab->srelbss->reloc_count;
11147 }
11148
11149 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11150 if (ELF_ST_IS_COMPRESSED (sym->st_other))
11151 sym->st_value &= ~1;
11152
11153 return TRUE;
11154 }
11155
11156 /* Write out a plt0 entry to the beginning of .plt. */
11157
11158 static bfd_boolean
mips_finish_exec_plt(bfd * output_bfd,struct bfd_link_info * info)11159 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11160 {
11161 bfd_byte *loc;
11162 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11163 static const bfd_vma *plt_entry;
11164 struct mips_elf_link_hash_table *htab;
11165
11166 htab = mips_elf_hash_table (info);
11167 BFD_ASSERT (htab != NULL);
11168
11169 if (ABI_64_P (output_bfd))
11170 plt_entry = mips_n64_exec_plt0_entry;
11171 else if (ABI_N32_P (output_bfd))
11172 plt_entry = mips_n32_exec_plt0_entry;
11173 else if (!htab->plt_header_is_comp)
11174 plt_entry = mips_o32_exec_plt0_entry;
11175 else if (htab->insn32)
11176 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11177 else
11178 plt_entry = micromips_o32_exec_plt0_entry;
11179
11180 /* Calculate the value of .got.plt. */
11181 gotplt_value = (htab->sgotplt->output_section->vma
11182 + htab->sgotplt->output_offset);
11183 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11184 gotplt_value_low = gotplt_value & 0xffff;
11185
11186 /* The PLT sequence is not safe for N64 if .got.plt's address can
11187 not be loaded in two instructions. */
11188 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
11189 || ~(gotplt_value | 0x7fffffff) == 0);
11190
11191 /* Install the PLT header. */
11192 loc = htab->splt->contents;
11193 if (plt_entry == micromips_o32_exec_plt0_entry)
11194 {
11195 bfd_vma gotpc_offset;
11196 bfd_vma loc_address;
11197 size_t i;
11198
11199 BFD_ASSERT (gotplt_value % 4 == 0);
11200
11201 loc_address = (htab->splt->output_section->vma
11202 + htab->splt->output_offset);
11203 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11204
11205 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11206 if (gotpc_offset + 0x1000000 >= 0x2000000)
11207 {
11208 (*_bfd_error_handler)
11209 (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
11210 output_bfd,
11211 htab->sgotplt->output_section,
11212 htab->splt->output_section,
11213 (long) gotpc_offset);
11214 bfd_set_error (bfd_error_no_error);
11215 return FALSE;
11216 }
11217 bfd_put_16 (output_bfd,
11218 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11219 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11220 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11221 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11222 }
11223 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11224 {
11225 size_t i;
11226
11227 bfd_put_16 (output_bfd, plt_entry[0], loc);
11228 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11229 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11230 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11231 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11232 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11233 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11234 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11235 }
11236 else
11237 {
11238 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11239 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11240 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11241 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11242 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11243 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11244 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11245 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11246 }
11247
11248 return TRUE;
11249 }
11250
11251 /* Install the PLT header for a VxWorks executable and finalize the
11252 contents of .rela.plt.unloaded. */
11253
11254 static void
mips_vxworks_finish_exec_plt(bfd * output_bfd,struct bfd_link_info * info)11255 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11256 {
11257 Elf_Internal_Rela rela;
11258 bfd_byte *loc;
11259 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11260 static const bfd_vma *plt_entry;
11261 struct mips_elf_link_hash_table *htab;
11262
11263 htab = mips_elf_hash_table (info);
11264 BFD_ASSERT (htab != NULL);
11265
11266 plt_entry = mips_vxworks_exec_plt0_entry;
11267
11268 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11269 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11270 + htab->root.hgot->root.u.def.section->output_offset
11271 + htab->root.hgot->root.u.def.value);
11272
11273 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11274 got_value_low = got_value & 0xffff;
11275
11276 /* Calculate the address of the PLT header. */
11277 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
11278
11279 /* Install the PLT header. */
11280 loc = htab->splt->contents;
11281 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11282 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11283 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11284 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11285 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11286 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11287
11288 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11289 loc = htab->srelplt2->contents;
11290 rela.r_offset = plt_address;
11291 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11292 rela.r_addend = 0;
11293 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11294 loc += sizeof (Elf32_External_Rela);
11295
11296 /* Output the relocation for the following addiu of
11297 %lo(_GLOBAL_OFFSET_TABLE_). */
11298 rela.r_offset += 4;
11299 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11300 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11301 loc += sizeof (Elf32_External_Rela);
11302
11303 /* Fix up the remaining relocations. They may have the wrong
11304 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11305 in which symbols were output. */
11306 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11307 {
11308 Elf_Internal_Rela rel;
11309
11310 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11311 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11312 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11313 loc += sizeof (Elf32_External_Rela);
11314
11315 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11316 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11317 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11318 loc += sizeof (Elf32_External_Rela);
11319
11320 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11321 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11322 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11323 loc += sizeof (Elf32_External_Rela);
11324 }
11325 }
11326
11327 /* Install the PLT header for a VxWorks shared library. */
11328
11329 static void
mips_vxworks_finish_shared_plt(bfd * output_bfd,struct bfd_link_info * info)11330 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11331 {
11332 unsigned int i;
11333 struct mips_elf_link_hash_table *htab;
11334
11335 htab = mips_elf_hash_table (info);
11336 BFD_ASSERT (htab != NULL);
11337
11338 /* We just need to copy the entry byte-by-byte. */
11339 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11340 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11341 htab->splt->contents + i * 4);
11342 }
11343
11344 /* Finish up the dynamic sections. */
11345
11346 bfd_boolean
_bfd_mips_elf_finish_dynamic_sections(bfd * output_bfd,struct bfd_link_info * info)11347 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11348 struct bfd_link_info *info)
11349 {
11350 bfd *dynobj;
11351 asection *sdyn;
11352 asection *sgot;
11353 struct mips_got_info *gg, *g;
11354 struct mips_elf_link_hash_table *htab;
11355
11356 htab = mips_elf_hash_table (info);
11357 BFD_ASSERT (htab != NULL);
11358
11359 dynobj = elf_hash_table (info)->dynobj;
11360
11361 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11362
11363 sgot = htab->sgot;
11364 gg = htab->got_info;
11365
11366 if (elf_hash_table (info)->dynamic_sections_created)
11367 {
11368 bfd_byte *b;
11369 int dyn_to_skip = 0, dyn_skipped = 0;
11370
11371 BFD_ASSERT (sdyn != NULL);
11372 BFD_ASSERT (gg != NULL);
11373
11374 g = mips_elf_bfd_got (output_bfd, FALSE);
11375 BFD_ASSERT (g != NULL);
11376
11377 for (b = sdyn->contents;
11378 b < sdyn->contents + sdyn->size;
11379 b += MIPS_ELF_DYN_SIZE (dynobj))
11380 {
11381 Elf_Internal_Dyn dyn;
11382 const char *name;
11383 size_t elemsize;
11384 asection *s;
11385 bfd_boolean swap_out_p;
11386
11387 /* Read in the current dynamic entry. */
11388 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11389
11390 /* Assume that we're going to modify it and write it out. */
11391 swap_out_p = TRUE;
11392
11393 switch (dyn.d_tag)
11394 {
11395 case DT_RELENT:
11396 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11397 break;
11398
11399 case DT_RELAENT:
11400 BFD_ASSERT (htab->is_vxworks);
11401 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11402 break;
11403
11404 case DT_STRSZ:
11405 /* Rewrite DT_STRSZ. */
11406 dyn.d_un.d_val =
11407 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11408 break;
11409
11410 case DT_PLTGOT:
11411 s = htab->sgot;
11412 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11413 break;
11414
11415 case DT_MIPS_PLTGOT:
11416 s = htab->sgotplt;
11417 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11418 break;
11419
11420 case DT_MIPS_RLD_VERSION:
11421 dyn.d_un.d_val = 1; /* XXX */
11422 break;
11423
11424 case DT_MIPS_FLAGS:
11425 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11426 break;
11427
11428 case DT_MIPS_TIME_STAMP:
11429 {
11430 time_t t;
11431 time (&t);
11432 dyn.d_un.d_val = t;
11433 }
11434 break;
11435
11436 case DT_MIPS_ICHECKSUM:
11437 /* XXX FIXME: */
11438 swap_out_p = FALSE;
11439 break;
11440
11441 case DT_MIPS_IVERSION:
11442 /* XXX FIXME: */
11443 swap_out_p = FALSE;
11444 break;
11445
11446 case DT_MIPS_BASE_ADDRESS:
11447 s = output_bfd->sections;
11448 BFD_ASSERT (s != NULL);
11449 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11450 break;
11451
11452 case DT_MIPS_LOCAL_GOTNO:
11453 dyn.d_un.d_val = g->local_gotno;
11454 break;
11455
11456 case DT_MIPS_UNREFEXTNO:
11457 /* The index into the dynamic symbol table which is the
11458 entry of the first external symbol that is not
11459 referenced within the same object. */
11460 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11461 break;
11462
11463 case DT_MIPS_GOTSYM:
11464 if (htab->global_gotsym)
11465 {
11466 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11467 break;
11468 }
11469 /* In case if we don't have global got symbols we default
11470 to setting DT_MIPS_GOTSYM to the same value as
11471 DT_MIPS_SYMTABNO, so we just fall through. */
11472
11473 case DT_MIPS_SYMTABNO:
11474 name = ".dynsym";
11475 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11476 s = bfd_get_section_by_name (output_bfd, name);
11477
11478 if (s != NULL)
11479 dyn.d_un.d_val = s->size / elemsize;
11480 else
11481 dyn.d_un.d_val = 0;
11482 break;
11483
11484 case DT_MIPS_HIPAGENO:
11485 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11486 break;
11487
11488 case DT_MIPS_RLD_MAP:
11489 {
11490 struct elf_link_hash_entry *h;
11491 h = mips_elf_hash_table (info)->rld_symbol;
11492 if (!h)
11493 {
11494 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11495 swap_out_p = FALSE;
11496 break;
11497 }
11498 s = h->root.u.def.section;
11499 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11500 + h->root.u.def.value);
11501 }
11502 break;
11503
11504 case DT_MIPS_RLD_MAP2:
11505 {
11506 struct elf_link_hash_entry *h;
11507 bfd_vma dt_addr, rld_addr;
11508 h = mips_elf_hash_table (info)->rld_symbol;
11509 if (!h)
11510 {
11511 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11512 swap_out_p = FALSE;
11513 break;
11514 }
11515 s = h->root.u.def.section;
11516
11517 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
11518 + (b - sdyn->contents));
11519 rld_addr = (s->output_section->vma + s->output_offset
11520 + h->root.u.def.value);
11521 dyn.d_un.d_ptr = rld_addr - dt_addr;
11522 }
11523 break;
11524
11525 case DT_MIPS_OPTIONS:
11526 s = (bfd_get_section_by_name
11527 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11528 dyn.d_un.d_ptr = s->vma;
11529 break;
11530
11531 case DT_RELASZ:
11532 BFD_ASSERT (htab->is_vxworks);
11533 /* The count does not include the JUMP_SLOT relocations. */
11534 if (htab->srelplt)
11535 dyn.d_un.d_val -= htab->srelplt->size;
11536 break;
11537
11538 case DT_PLTREL:
11539 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11540 if (htab->is_vxworks)
11541 dyn.d_un.d_val = DT_RELA;
11542 else
11543 dyn.d_un.d_val = DT_REL;
11544 break;
11545
11546 case DT_PLTRELSZ:
11547 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11548 dyn.d_un.d_val = htab->srelplt->size;
11549 break;
11550
11551 case DT_JMPREL:
11552 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11553 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
11554 + htab->srelplt->output_offset);
11555 break;
11556
11557 case DT_TEXTREL:
11558 /* If we didn't need any text relocations after all, delete
11559 the dynamic tag. */
11560 if (!(info->flags & DF_TEXTREL))
11561 {
11562 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11563 swap_out_p = FALSE;
11564 }
11565 break;
11566
11567 case DT_FLAGS:
11568 /* If we didn't need any text relocations after all, clear
11569 DF_TEXTREL from DT_FLAGS. */
11570 if (!(info->flags & DF_TEXTREL))
11571 dyn.d_un.d_val &= ~DF_TEXTREL;
11572 else
11573 swap_out_p = FALSE;
11574 break;
11575
11576 default:
11577 swap_out_p = FALSE;
11578 if (htab->is_vxworks
11579 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11580 swap_out_p = TRUE;
11581 break;
11582 }
11583
11584 if (swap_out_p || dyn_skipped)
11585 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11586 (dynobj, &dyn, b - dyn_skipped);
11587
11588 if (dyn_to_skip)
11589 {
11590 dyn_skipped += dyn_to_skip;
11591 dyn_to_skip = 0;
11592 }
11593 }
11594
11595 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11596 if (dyn_skipped > 0)
11597 memset (b - dyn_skipped, 0, dyn_skipped);
11598 }
11599
11600 if (sgot != NULL && sgot->size > 0
11601 && !bfd_is_abs_section (sgot->output_section))
11602 {
11603 if (htab->is_vxworks)
11604 {
11605 /* The first entry of the global offset table points to the
11606 ".dynamic" section. The second is initialized by the
11607 loader and contains the shared library identifier.
11608 The third is also initialized by the loader and points
11609 to the lazy resolution stub. */
11610 MIPS_ELF_PUT_WORD (output_bfd,
11611 sdyn->output_offset + sdyn->output_section->vma,
11612 sgot->contents);
11613 MIPS_ELF_PUT_WORD (output_bfd, 0,
11614 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11615 MIPS_ELF_PUT_WORD (output_bfd, 0,
11616 sgot->contents
11617 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11618 }
11619 else
11620 {
11621 /* The first entry of the global offset table will be filled at
11622 runtime. The second entry will be used by some runtime loaders.
11623 This isn't the case of IRIX rld. */
11624 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
11625 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11626 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11627 }
11628
11629 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11630 = MIPS_ELF_GOT_SIZE (output_bfd);
11631 }
11632
11633 /* Generate dynamic relocations for the non-primary gots. */
11634 if (gg != NULL && gg->next)
11635 {
11636 Elf_Internal_Rela rel[3];
11637 bfd_vma addend = 0;
11638
11639 memset (rel, 0, sizeof (rel));
11640 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11641
11642 for (g = gg->next; g->next != gg; g = g->next)
11643 {
11644 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
11645 + g->next->tls_gotno;
11646
11647 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
11648 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11649 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11650 sgot->contents
11651 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11652
11653 if (! info->shared)
11654 continue;
11655
11656 for (; got_index < g->local_gotno; got_index++)
11657 {
11658 if (got_index >= g->assigned_low_gotno
11659 && got_index <= g->assigned_high_gotno)
11660 continue;
11661
11662 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
11663 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
11664 if (!(mips_elf_create_dynamic_relocation
11665 (output_bfd, info, rel, NULL,
11666 bfd_abs_section_ptr,
11667 0, &addend, sgot)))
11668 return FALSE;
11669 BFD_ASSERT (addend == 0);
11670 }
11671 }
11672 }
11673
11674 /* The generation of dynamic relocations for the non-primary gots
11675 adds more dynamic relocations. We cannot count them until
11676 here. */
11677
11678 if (elf_hash_table (info)->dynamic_sections_created)
11679 {
11680 bfd_byte *b;
11681 bfd_boolean swap_out_p;
11682
11683 BFD_ASSERT (sdyn != NULL);
11684
11685 for (b = sdyn->contents;
11686 b < sdyn->contents + sdyn->size;
11687 b += MIPS_ELF_DYN_SIZE (dynobj))
11688 {
11689 Elf_Internal_Dyn dyn;
11690 asection *s;
11691
11692 /* Read in the current dynamic entry. */
11693 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11694
11695 /* Assume that we're going to modify it and write it out. */
11696 swap_out_p = TRUE;
11697
11698 switch (dyn.d_tag)
11699 {
11700 case DT_RELSZ:
11701 /* Reduce DT_RELSZ to account for any relocations we
11702 decided not to make. This is for the n64 irix rld,
11703 which doesn't seem to apply any relocations if there
11704 are trailing null entries. */
11705 s = mips_elf_rel_dyn_section (info, FALSE);
11706 dyn.d_un.d_val = (s->reloc_count
11707 * (ABI_64_P (output_bfd)
11708 ? sizeof (Elf64_Mips_External_Rel)
11709 : sizeof (Elf32_External_Rel)));
11710 /* Adjust the section size too. Tools like the prelinker
11711 can reasonably expect the values to the same. */
11712 elf_section_data (s->output_section)->this_hdr.sh_size
11713 = dyn.d_un.d_val;
11714 break;
11715
11716 default:
11717 swap_out_p = FALSE;
11718 break;
11719 }
11720
11721 if (swap_out_p)
11722 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11723 (dynobj, &dyn, b);
11724 }
11725 }
11726
11727 {
11728 asection *s;
11729 Elf32_compact_rel cpt;
11730
11731 if (SGI_COMPAT (output_bfd))
11732 {
11733 /* Write .compact_rel section out. */
11734 s = bfd_get_linker_section (dynobj, ".compact_rel");
11735 if (s != NULL)
11736 {
11737 cpt.id1 = 1;
11738 cpt.num = s->reloc_count;
11739 cpt.id2 = 2;
11740 cpt.offset = (s->output_section->filepos
11741 + sizeof (Elf32_External_compact_rel));
11742 cpt.reserved0 = 0;
11743 cpt.reserved1 = 0;
11744 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
11745 ((Elf32_External_compact_rel *)
11746 s->contents));
11747
11748 /* Clean up a dummy stub function entry in .text. */
11749 if (htab->sstubs != NULL)
11750 {
11751 file_ptr dummy_offset;
11752
11753 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
11754 dummy_offset = htab->sstubs->size - htab->function_stub_size;
11755 memset (htab->sstubs->contents + dummy_offset, 0,
11756 htab->function_stub_size);
11757 }
11758 }
11759 }
11760
11761 /* The psABI says that the dynamic relocations must be sorted in
11762 increasing order of r_symndx. The VxWorks EABI doesn't require
11763 this, and because the code below handles REL rather than RELA
11764 relocations, using it for VxWorks would be outright harmful. */
11765 if (!htab->is_vxworks)
11766 {
11767 s = mips_elf_rel_dyn_section (info, FALSE);
11768 if (s != NULL
11769 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
11770 {
11771 reldyn_sorting_bfd = output_bfd;
11772
11773 if (ABI_64_P (output_bfd))
11774 qsort ((Elf64_External_Rel *) s->contents + 1,
11775 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
11776 sort_dynamic_relocs_64);
11777 else
11778 qsort ((Elf32_External_Rel *) s->contents + 1,
11779 s->reloc_count - 1, sizeof (Elf32_External_Rel),
11780 sort_dynamic_relocs);
11781 }
11782 }
11783 }
11784
11785 if (htab->splt && htab->splt->size > 0)
11786 {
11787 if (htab->is_vxworks)
11788 {
11789 if (info->shared)
11790 mips_vxworks_finish_shared_plt (output_bfd, info);
11791 else
11792 mips_vxworks_finish_exec_plt (output_bfd, info);
11793 }
11794 else
11795 {
11796 BFD_ASSERT (!info->shared);
11797 if (!mips_finish_exec_plt (output_bfd, info))
11798 return FALSE;
11799 }
11800 }
11801 return TRUE;
11802 }
11803
11804
11805 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
11806
11807 static void
mips_set_isa_flags(bfd * abfd)11808 mips_set_isa_flags (bfd *abfd)
11809 {
11810 flagword val;
11811
11812 switch (bfd_get_mach (abfd))
11813 {
11814 default:
11815 case bfd_mach_mips3000:
11816 val = E_MIPS_ARCH_1;
11817 break;
11818
11819 case bfd_mach_mips3900:
11820 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
11821 break;
11822
11823 case bfd_mach_mips6000:
11824 val = E_MIPS_ARCH_2;
11825 break;
11826
11827 case bfd_mach_mips4000:
11828 case bfd_mach_mips4300:
11829 case bfd_mach_mips4400:
11830 case bfd_mach_mips4600:
11831 val = E_MIPS_ARCH_3;
11832 break;
11833
11834 case bfd_mach_mips4010:
11835 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
11836 break;
11837
11838 case bfd_mach_mips4100:
11839 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
11840 break;
11841
11842 case bfd_mach_mips4111:
11843 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
11844 break;
11845
11846 case bfd_mach_mips4120:
11847 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
11848 break;
11849
11850 case bfd_mach_mips4650:
11851 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
11852 break;
11853
11854 case bfd_mach_mips5400:
11855 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
11856 break;
11857
11858 case bfd_mach_mips5500:
11859 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
11860 break;
11861
11862 case bfd_mach_mips5900:
11863 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
11864 break;
11865
11866 case bfd_mach_mips9000:
11867 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
11868 break;
11869
11870 case bfd_mach_mips5000:
11871 case bfd_mach_mips7000:
11872 case bfd_mach_mips8000:
11873 case bfd_mach_mips10000:
11874 case bfd_mach_mips12000:
11875 case bfd_mach_mips14000:
11876 case bfd_mach_mips16000:
11877 val = E_MIPS_ARCH_4;
11878 break;
11879
11880 case bfd_mach_mips5:
11881 val = E_MIPS_ARCH_5;
11882 break;
11883
11884 case bfd_mach_mips_loongson_2e:
11885 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
11886 break;
11887
11888 case bfd_mach_mips_loongson_2f:
11889 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
11890 break;
11891
11892 case bfd_mach_mips_sb1:
11893 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
11894 break;
11895
11896 case bfd_mach_mips_loongson_3a:
11897 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_LS3A;
11898 break;
11899
11900 case bfd_mach_mips_octeon:
11901 case bfd_mach_mips_octeonp:
11902 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
11903 break;
11904
11905 case bfd_mach_mips_octeon3:
11906 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
11907 break;
11908
11909 case bfd_mach_mips_xlr:
11910 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
11911 break;
11912
11913 case bfd_mach_mips_octeon2:
11914 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
11915 break;
11916
11917 case bfd_mach_mipsisa32:
11918 val = E_MIPS_ARCH_32;
11919 break;
11920
11921 case bfd_mach_mipsisa64:
11922 val = E_MIPS_ARCH_64;
11923 break;
11924
11925 case bfd_mach_mipsisa32r2:
11926 case bfd_mach_mipsisa32r3:
11927 case bfd_mach_mipsisa32r5:
11928 val = E_MIPS_ARCH_32R2;
11929 break;
11930
11931 case bfd_mach_mipsisa64r2:
11932 case bfd_mach_mipsisa64r3:
11933 case bfd_mach_mipsisa64r5:
11934 val = E_MIPS_ARCH_64R2;
11935 break;
11936
11937 case bfd_mach_mipsisa32r6:
11938 val = E_MIPS_ARCH_32R6;
11939 break;
11940
11941 case bfd_mach_mipsisa64r6:
11942 val = E_MIPS_ARCH_64R6;
11943 break;
11944 }
11945 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11946 elf_elfheader (abfd)->e_flags |= val;
11947
11948 }
11949
11950
11951 /* The final processing done just before writing out a MIPS ELF object
11952 file. This gets the MIPS architecture right based on the machine
11953 number. This is used by both the 32-bit and the 64-bit ABI. */
11954
11955 void
_bfd_mips_elf_final_write_processing(bfd * abfd,bfd_boolean linker ATTRIBUTE_UNUSED)11956 _bfd_mips_elf_final_write_processing (bfd *abfd,
11957 bfd_boolean linker ATTRIBUTE_UNUSED)
11958 {
11959 unsigned int i;
11960 Elf_Internal_Shdr **hdrpp;
11961 const char *name;
11962 asection *sec;
11963
11964 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
11965 is nonzero. This is for compatibility with old objects, which used
11966 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
11967 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
11968 mips_set_isa_flags (abfd);
11969
11970 /* Set the sh_info field for .gptab sections and other appropriate
11971 info for each special section. */
11972 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11973 i < elf_numsections (abfd);
11974 i++, hdrpp++)
11975 {
11976 switch ((*hdrpp)->sh_type)
11977 {
11978 case SHT_MIPS_MSYM:
11979 case SHT_MIPS_LIBLIST:
11980 sec = bfd_get_section_by_name (abfd, ".dynstr");
11981 if (sec != NULL)
11982 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11983 break;
11984
11985 case SHT_MIPS_GPTAB:
11986 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11987 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11988 BFD_ASSERT (name != NULL
11989 && CONST_STRNEQ (name, ".gptab."));
11990 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11991 BFD_ASSERT (sec != NULL);
11992 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11993 break;
11994
11995 case SHT_MIPS_CONTENT:
11996 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11997 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11998 BFD_ASSERT (name != NULL
11999 && CONST_STRNEQ (name, ".MIPS.content"));
12000 sec = bfd_get_section_by_name (abfd,
12001 name + sizeof ".MIPS.content" - 1);
12002 BFD_ASSERT (sec != NULL);
12003 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12004 break;
12005
12006 case SHT_MIPS_SYMBOL_LIB:
12007 sec = bfd_get_section_by_name (abfd, ".dynsym");
12008 if (sec != NULL)
12009 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12010 sec = bfd_get_section_by_name (abfd, ".liblist");
12011 if (sec != NULL)
12012 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12013 break;
12014
12015 case SHT_MIPS_EVENTS:
12016 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12017 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
12018 BFD_ASSERT (name != NULL);
12019 if (CONST_STRNEQ (name, ".MIPS.events"))
12020 sec = bfd_get_section_by_name (abfd,
12021 name + sizeof ".MIPS.events" - 1);
12022 else
12023 {
12024 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
12025 sec = bfd_get_section_by_name (abfd,
12026 (name
12027 + sizeof ".MIPS.post_rel" - 1));
12028 }
12029 BFD_ASSERT (sec != NULL);
12030 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12031 break;
12032
12033 }
12034 }
12035 }
12036
12037 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
12038 segments. */
12039
12040 int
_bfd_mips_elf_additional_program_headers(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED)12041 _bfd_mips_elf_additional_program_headers (bfd *abfd,
12042 struct bfd_link_info *info ATTRIBUTE_UNUSED)
12043 {
12044 asection *s;
12045 int ret = 0;
12046
12047 /* See if we need a PT_MIPS_REGINFO segment. */
12048 s = bfd_get_section_by_name (abfd, ".reginfo");
12049 if (s && (s->flags & SEC_LOAD))
12050 ++ret;
12051
12052 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12053 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12054 ++ret;
12055
12056 /* See if we need a PT_MIPS_OPTIONS segment. */
12057 if (IRIX_COMPAT (abfd) == ict_irix6
12058 && bfd_get_section_by_name (abfd,
12059 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12060 ++ret;
12061
12062 /* See if we need a PT_MIPS_RTPROC segment. */
12063 if (IRIX_COMPAT (abfd) == ict_irix5
12064 && bfd_get_section_by_name (abfd, ".dynamic")
12065 && bfd_get_section_by_name (abfd, ".mdebug"))
12066 ++ret;
12067
12068 /* Allocate a PT_NULL header in dynamic objects. See
12069 _bfd_mips_elf_modify_segment_map for details. */
12070 if (!SGI_COMPAT (abfd)
12071 && bfd_get_section_by_name (abfd, ".dynamic"))
12072 ++ret;
12073
12074 return ret;
12075 }
12076
12077 /* Modify the segment map for an IRIX5 executable. */
12078
12079 bfd_boolean
_bfd_mips_elf_modify_segment_map(bfd * abfd,struct bfd_link_info * info)12080 _bfd_mips_elf_modify_segment_map (bfd *abfd,
12081 struct bfd_link_info *info)
12082 {
12083 asection *s;
12084 struct elf_segment_map *m, **pm;
12085 bfd_size_type amt;
12086
12087 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12088 segment. */
12089 s = bfd_get_section_by_name (abfd, ".reginfo");
12090 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12091 {
12092 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12093 if (m->p_type == PT_MIPS_REGINFO)
12094 break;
12095 if (m == NULL)
12096 {
12097 amt = sizeof *m;
12098 m = bfd_zalloc (abfd, amt);
12099 if (m == NULL)
12100 return FALSE;
12101
12102 m->p_type = PT_MIPS_REGINFO;
12103 m->count = 1;
12104 m->sections[0] = s;
12105
12106 /* We want to put it after the PHDR and INTERP segments. */
12107 pm = &elf_seg_map (abfd);
12108 while (*pm != NULL
12109 && ((*pm)->p_type == PT_PHDR
12110 || (*pm)->p_type == PT_INTERP))
12111 pm = &(*pm)->next;
12112
12113 m->next = *pm;
12114 *pm = m;
12115 }
12116 }
12117
12118 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12119 segment. */
12120 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12121 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12122 {
12123 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12124 if (m->p_type == PT_MIPS_ABIFLAGS)
12125 break;
12126 if (m == NULL)
12127 {
12128 amt = sizeof *m;
12129 m = bfd_zalloc (abfd, amt);
12130 if (m == NULL)
12131 return FALSE;
12132
12133 m->p_type = PT_MIPS_ABIFLAGS;
12134 m->count = 1;
12135 m->sections[0] = s;
12136
12137 /* We want to put it after the PHDR and INTERP segments. */
12138 pm = &elf_seg_map (abfd);
12139 while (*pm != NULL
12140 && ((*pm)->p_type == PT_PHDR
12141 || (*pm)->p_type == PT_INTERP))
12142 pm = &(*pm)->next;
12143
12144 m->next = *pm;
12145 *pm = m;
12146 }
12147 }
12148
12149 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12150 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
12151 PT_MIPS_OPTIONS segment immediately following the program header
12152 table. */
12153 if (NEWABI_P (abfd)
12154 /* On non-IRIX6 new abi, we'll have already created a segment
12155 for this section, so don't create another. I'm not sure this
12156 is not also the case for IRIX 6, but I can't test it right
12157 now. */
12158 && IRIX_COMPAT (abfd) == ict_irix6)
12159 {
12160 for (s = abfd->sections; s; s = s->next)
12161 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12162 break;
12163
12164 if (s)
12165 {
12166 struct elf_segment_map *options_segment;
12167
12168 pm = &elf_seg_map (abfd);
12169 while (*pm != NULL
12170 && ((*pm)->p_type == PT_PHDR
12171 || (*pm)->p_type == PT_INTERP))
12172 pm = &(*pm)->next;
12173
12174 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12175 {
12176 amt = sizeof (struct elf_segment_map);
12177 options_segment = bfd_zalloc (abfd, amt);
12178 options_segment->next = *pm;
12179 options_segment->p_type = PT_MIPS_OPTIONS;
12180 options_segment->p_flags = PF_R;
12181 options_segment->p_flags_valid = TRUE;
12182 options_segment->count = 1;
12183 options_segment->sections[0] = s;
12184 *pm = options_segment;
12185 }
12186 }
12187 }
12188 else
12189 {
12190 if (IRIX_COMPAT (abfd) == ict_irix5)
12191 {
12192 /* If there are .dynamic and .mdebug sections, we make a room
12193 for the RTPROC header. FIXME: Rewrite without section names. */
12194 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12195 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12196 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12197 {
12198 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12199 if (m->p_type == PT_MIPS_RTPROC)
12200 break;
12201 if (m == NULL)
12202 {
12203 amt = sizeof *m;
12204 m = bfd_zalloc (abfd, amt);
12205 if (m == NULL)
12206 return FALSE;
12207
12208 m->p_type = PT_MIPS_RTPROC;
12209
12210 s = bfd_get_section_by_name (abfd, ".rtproc");
12211 if (s == NULL)
12212 {
12213 m->count = 0;
12214 m->p_flags = 0;
12215 m->p_flags_valid = 1;
12216 }
12217 else
12218 {
12219 m->count = 1;
12220 m->sections[0] = s;
12221 }
12222
12223 /* We want to put it after the DYNAMIC segment. */
12224 pm = &elf_seg_map (abfd);
12225 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12226 pm = &(*pm)->next;
12227 if (*pm != NULL)
12228 pm = &(*pm)->next;
12229
12230 m->next = *pm;
12231 *pm = m;
12232 }
12233 }
12234 }
12235 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
12236 .dynstr, .dynsym, and .hash sections, and everything in
12237 between. */
12238 for (pm = &elf_seg_map (abfd); *pm != NULL;
12239 pm = &(*pm)->next)
12240 if ((*pm)->p_type == PT_DYNAMIC)
12241 break;
12242 m = *pm;
12243 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12244 glibc's dynamic linker has traditionally derived the number of
12245 tags from the p_filesz field, and sometimes allocates stack
12246 arrays of that size. An overly-big PT_DYNAMIC segment can
12247 be actively harmful in such cases. Making PT_DYNAMIC contain
12248 other sections can also make life hard for the prelinker,
12249 which might move one of the other sections to a different
12250 PT_LOAD segment. */
12251 if (SGI_COMPAT (abfd)
12252 && m != NULL
12253 && m->count == 1
12254 && strcmp (m->sections[0]->name, ".dynamic") == 0)
12255 {
12256 static const char *sec_names[] =
12257 {
12258 ".dynamic", ".dynstr", ".dynsym", ".hash"
12259 };
12260 bfd_vma low, high;
12261 unsigned int i, c;
12262 struct elf_segment_map *n;
12263
12264 low = ~(bfd_vma) 0;
12265 high = 0;
12266 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12267 {
12268 s = bfd_get_section_by_name (abfd, sec_names[i]);
12269 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12270 {
12271 bfd_size_type sz;
12272
12273 if (low > s->vma)
12274 low = s->vma;
12275 sz = s->size;
12276 if (high < s->vma + sz)
12277 high = s->vma + sz;
12278 }
12279 }
12280
12281 c = 0;
12282 for (s = abfd->sections; s != NULL; s = s->next)
12283 if ((s->flags & SEC_LOAD) != 0
12284 && s->vma >= low
12285 && s->vma + s->size <= high)
12286 ++c;
12287
12288 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
12289 n = bfd_zalloc (abfd, amt);
12290 if (n == NULL)
12291 return FALSE;
12292 *n = *m;
12293 n->count = c;
12294
12295 i = 0;
12296 for (s = abfd->sections; s != NULL; s = s->next)
12297 {
12298 if ((s->flags & SEC_LOAD) != 0
12299 && s->vma >= low
12300 && s->vma + s->size <= high)
12301 {
12302 n->sections[i] = s;
12303 ++i;
12304 }
12305 }
12306
12307 *pm = n;
12308 }
12309 }
12310
12311 /* Allocate a spare program header in dynamic objects so that tools
12312 like the prelinker can add an extra PT_LOAD entry.
12313
12314 If the prelinker needs to make room for a new PT_LOAD entry, its
12315 standard procedure is to move the first (read-only) sections into
12316 the new (writable) segment. However, the MIPS ABI requires
12317 .dynamic to be in a read-only segment, and the section will often
12318 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12319
12320 Although the prelinker could in principle move .dynamic to a
12321 writable segment, it seems better to allocate a spare program
12322 header instead, and avoid the need to move any sections.
12323 There is a long tradition of allocating spare dynamic tags,
12324 so allocating a spare program header seems like a natural
12325 extension.
12326
12327 If INFO is NULL, we may be copying an already prelinked binary
12328 with objcopy or strip, so do not add this header. */
12329 if (info != NULL
12330 && !SGI_COMPAT (abfd)
12331 && bfd_get_section_by_name (abfd, ".dynamic"))
12332 {
12333 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12334 if ((*pm)->p_type == PT_NULL)
12335 break;
12336 if (*pm == NULL)
12337 {
12338 m = bfd_zalloc (abfd, sizeof (*m));
12339 if (m == NULL)
12340 return FALSE;
12341
12342 m->p_type = PT_NULL;
12343 *pm = m;
12344 }
12345 }
12346
12347 return TRUE;
12348 }
12349
12350 /* Return the section that should be marked against GC for a given
12351 relocation. */
12352
12353 asection *
_bfd_mips_elf_gc_mark_hook(asection * sec,struct bfd_link_info * info,Elf_Internal_Rela * rel,struct elf_link_hash_entry * h,Elf_Internal_Sym * sym)12354 _bfd_mips_elf_gc_mark_hook (asection *sec,
12355 struct bfd_link_info *info,
12356 Elf_Internal_Rela *rel,
12357 struct elf_link_hash_entry *h,
12358 Elf_Internal_Sym *sym)
12359 {
12360 /* ??? Do mips16 stub sections need to be handled special? */
12361
12362 if (h != NULL)
12363 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12364 {
12365 case R_MIPS_GNU_VTINHERIT:
12366 case R_MIPS_GNU_VTENTRY:
12367 return NULL;
12368 }
12369
12370 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12371 }
12372
12373 /* Update the got entry reference counts for the section being removed. */
12374
12375 bfd_boolean
_bfd_mips_elf_gc_sweep_hook(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED,const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)12376 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
12377 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12378 asection *sec ATTRIBUTE_UNUSED,
12379 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
12380 {
12381 #if 0
12382 Elf_Internal_Shdr *symtab_hdr;
12383 struct elf_link_hash_entry **sym_hashes;
12384 bfd_signed_vma *local_got_refcounts;
12385 const Elf_Internal_Rela *rel, *relend;
12386 unsigned long r_symndx;
12387 struct elf_link_hash_entry *h;
12388
12389 if (info->relocatable)
12390 return TRUE;
12391
12392 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12393 sym_hashes = elf_sym_hashes (abfd);
12394 local_got_refcounts = elf_local_got_refcounts (abfd);
12395
12396 relend = relocs + sec->reloc_count;
12397 for (rel = relocs; rel < relend; rel++)
12398 switch (ELF_R_TYPE (abfd, rel->r_info))
12399 {
12400 case R_MIPS16_GOT16:
12401 case R_MIPS16_CALL16:
12402 case R_MIPS_GOT16:
12403 case R_MIPS_CALL16:
12404 case R_MIPS_CALL_HI16:
12405 case R_MIPS_CALL_LO16:
12406 case R_MIPS_GOT_HI16:
12407 case R_MIPS_GOT_LO16:
12408 case R_MIPS_GOT_DISP:
12409 case R_MIPS_GOT_PAGE:
12410 case R_MIPS_GOT_OFST:
12411 case R_MICROMIPS_GOT16:
12412 case R_MICROMIPS_CALL16:
12413 case R_MICROMIPS_CALL_HI16:
12414 case R_MICROMIPS_CALL_LO16:
12415 case R_MICROMIPS_GOT_HI16:
12416 case R_MICROMIPS_GOT_LO16:
12417 case R_MICROMIPS_GOT_DISP:
12418 case R_MICROMIPS_GOT_PAGE:
12419 case R_MICROMIPS_GOT_OFST:
12420 /* ??? It would seem that the existing MIPS code does no sort
12421 of reference counting or whatnot on its GOT and PLT entries,
12422 so it is not possible to garbage collect them at this time. */
12423 break;
12424
12425 default:
12426 break;
12427 }
12428 #endif
12429
12430 return TRUE;
12431 }
12432
12433 /* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12434
12435 bfd_boolean
_bfd_mips_elf_gc_mark_extra_sections(struct bfd_link_info * info,elf_gc_mark_hook_fn gc_mark_hook)12436 _bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12437 elf_gc_mark_hook_fn gc_mark_hook)
12438 {
12439 bfd *sub;
12440
12441 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12442
12443 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12444 {
12445 asection *o;
12446
12447 if (! is_mips_elf (sub))
12448 continue;
12449
12450 for (o = sub->sections; o != NULL; o = o->next)
12451 if (!o->gc_mark
12452 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P
12453 (bfd_get_section_name (sub, o)))
12454 {
12455 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12456 return FALSE;
12457 }
12458 }
12459
12460 return TRUE;
12461 }
12462
12463 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12464 hiding the old indirect symbol. Process additional relocation
12465 information. Also called for weakdefs, in which case we just let
12466 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12467
12468 void
_bfd_mips_elf_copy_indirect_symbol(struct bfd_link_info * info,struct elf_link_hash_entry * dir,struct elf_link_hash_entry * ind)12469 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12470 struct elf_link_hash_entry *dir,
12471 struct elf_link_hash_entry *ind)
12472 {
12473 struct mips_elf_link_hash_entry *dirmips, *indmips;
12474
12475 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12476
12477 dirmips = (struct mips_elf_link_hash_entry *) dir;
12478 indmips = (struct mips_elf_link_hash_entry *) ind;
12479 /* Any absolute non-dynamic relocations against an indirect or weak
12480 definition will be against the target symbol. */
12481 if (indmips->has_static_relocs)
12482 dirmips->has_static_relocs = TRUE;
12483
12484 if (ind->root.type != bfd_link_hash_indirect)
12485 return;
12486
12487 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12488 if (indmips->readonly_reloc)
12489 dirmips->readonly_reloc = TRUE;
12490 if (indmips->no_fn_stub)
12491 dirmips->no_fn_stub = TRUE;
12492 if (indmips->fn_stub)
12493 {
12494 dirmips->fn_stub = indmips->fn_stub;
12495 indmips->fn_stub = NULL;
12496 }
12497 if (indmips->need_fn_stub)
12498 {
12499 dirmips->need_fn_stub = TRUE;
12500 indmips->need_fn_stub = FALSE;
12501 }
12502 if (indmips->call_stub)
12503 {
12504 dirmips->call_stub = indmips->call_stub;
12505 indmips->call_stub = NULL;
12506 }
12507 if (indmips->call_fp_stub)
12508 {
12509 dirmips->call_fp_stub = indmips->call_fp_stub;
12510 indmips->call_fp_stub = NULL;
12511 }
12512 if (indmips->global_got_area < dirmips->global_got_area)
12513 dirmips->global_got_area = indmips->global_got_area;
12514 if (indmips->global_got_area < GGA_NONE)
12515 indmips->global_got_area = GGA_NONE;
12516 if (indmips->has_nonpic_branches)
12517 dirmips->has_nonpic_branches = TRUE;
12518 }
12519
12520 #define PDR_SIZE 32
12521
12522 bfd_boolean
_bfd_mips_elf_discard_info(bfd * abfd,struct elf_reloc_cookie * cookie,struct bfd_link_info * info)12523 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12524 struct bfd_link_info *info)
12525 {
12526 asection *o;
12527 bfd_boolean ret = FALSE;
12528 unsigned char *tdata;
12529 size_t i, skip;
12530
12531 o = bfd_get_section_by_name (abfd, ".pdr");
12532 if (! o)
12533 return FALSE;
12534 if (o->size == 0)
12535 return FALSE;
12536 if (o->size % PDR_SIZE != 0)
12537 return FALSE;
12538 if (o->output_section != NULL
12539 && bfd_is_abs_section (o->output_section))
12540 return FALSE;
12541
12542 tdata = bfd_zmalloc (o->size / PDR_SIZE);
12543 if (! tdata)
12544 return FALSE;
12545
12546 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
12547 info->keep_memory);
12548 if (!cookie->rels)
12549 {
12550 free (tdata);
12551 return FALSE;
12552 }
12553
12554 cookie->rel = cookie->rels;
12555 cookie->relend = cookie->rels + o->reloc_count;
12556
12557 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
12558 {
12559 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
12560 {
12561 tdata[i] = 1;
12562 skip ++;
12563 }
12564 }
12565
12566 if (skip != 0)
12567 {
12568 mips_elf_section_data (o)->u.tdata = tdata;
12569 if (o->rawsize == 0)
12570 o->rawsize = o->size;
12571 o->size -= skip * PDR_SIZE;
12572 ret = TRUE;
12573 }
12574 else
12575 free (tdata);
12576
12577 if (! info->keep_memory)
12578 free (cookie->rels);
12579
12580 return ret;
12581 }
12582
12583 bfd_boolean
_bfd_mips_elf_ignore_discarded_relocs(asection * sec)12584 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
12585 {
12586 if (strcmp (sec->name, ".pdr") == 0)
12587 return TRUE;
12588 return FALSE;
12589 }
12590
12591 bfd_boolean
_bfd_mips_elf_write_section(bfd * output_bfd,struct bfd_link_info * link_info ATTRIBUTE_UNUSED,asection * sec,bfd_byte * contents)12592 _bfd_mips_elf_write_section (bfd *output_bfd,
12593 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
12594 asection *sec, bfd_byte *contents)
12595 {
12596 bfd_byte *to, *from, *end;
12597 int i;
12598
12599 if (strcmp (sec->name, ".pdr") != 0)
12600 return FALSE;
12601
12602 if (mips_elf_section_data (sec)->u.tdata == NULL)
12603 return FALSE;
12604
12605 to = contents;
12606 end = contents + sec->size;
12607 for (from = contents, i = 0;
12608 from < end;
12609 from += PDR_SIZE, i++)
12610 {
12611 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
12612 continue;
12613 if (to != from)
12614 memcpy (to, from, PDR_SIZE);
12615 to += PDR_SIZE;
12616 }
12617 bfd_set_section_contents (output_bfd, sec->output_section, contents,
12618 sec->output_offset, sec->size);
12619 return TRUE;
12620 }
12621
12622 /* microMIPS code retains local labels for linker relaxation. Omit them
12623 from output by default for clarity. */
12624
12625 bfd_boolean
_bfd_mips_elf_is_target_special_symbol(bfd * abfd,asymbol * sym)12626 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12627 {
12628 return _bfd_elf_is_local_label_name (abfd, sym->name);
12629 }
12630
12631 /* MIPS ELF uses a special find_nearest_line routine in order the
12632 handle the ECOFF debugging information. */
12633
12634 struct mips_elf_find_line
12635 {
12636 struct ecoff_debug_info d;
12637 struct ecoff_find_line i;
12638 };
12639
12640 bfd_boolean
_bfd_mips_elf_find_nearest_line(bfd * abfd,asymbol ** symbols,asection * section,bfd_vma offset,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * line_ptr,unsigned int * discriminator_ptr)12641 _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
12642 asection *section, bfd_vma offset,
12643 const char **filename_ptr,
12644 const char **functionname_ptr,
12645 unsigned int *line_ptr,
12646 unsigned int *discriminator_ptr)
12647 {
12648 asection *msec;
12649
12650 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
12651 filename_ptr, functionname_ptr,
12652 line_ptr, discriminator_ptr,
12653 dwarf_debug_sections,
12654 ABI_64_P (abfd) ? 8 : 0,
12655 &elf_tdata (abfd)->dwarf2_find_line_info))
12656 return TRUE;
12657
12658 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
12659 filename_ptr, functionname_ptr,
12660 line_ptr))
12661 return TRUE;
12662
12663 msec = bfd_get_section_by_name (abfd, ".mdebug");
12664 if (msec != NULL)
12665 {
12666 flagword origflags;
12667 struct mips_elf_find_line *fi;
12668 const struct ecoff_debug_swap * const swap =
12669 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12670
12671 /* If we are called during a link, mips_elf_final_link may have
12672 cleared the SEC_HAS_CONTENTS field. We force it back on here
12673 if appropriate (which it normally will be). */
12674 origflags = msec->flags;
12675 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12676 msec->flags |= SEC_HAS_CONTENTS;
12677
12678 fi = mips_elf_tdata (abfd)->find_line_info;
12679 if (fi == NULL)
12680 {
12681 bfd_size_type external_fdr_size;
12682 char *fraw_src;
12683 char *fraw_end;
12684 struct fdr *fdr_ptr;
12685 bfd_size_type amt = sizeof (struct mips_elf_find_line);
12686
12687 fi = bfd_zalloc (abfd, amt);
12688 if (fi == NULL)
12689 {
12690 msec->flags = origflags;
12691 return FALSE;
12692 }
12693
12694 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
12695 {
12696 msec->flags = origflags;
12697 return FALSE;
12698 }
12699
12700 /* Swap in the FDR information. */
12701 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
12702 fi->d.fdr = bfd_alloc (abfd, amt);
12703 if (fi->d.fdr == NULL)
12704 {
12705 msec->flags = origflags;
12706 return FALSE;
12707 }
12708 external_fdr_size = swap->external_fdr_size;
12709 fdr_ptr = fi->d.fdr;
12710 fraw_src = (char *) fi->d.external_fdr;
12711 fraw_end = (fraw_src
12712 + fi->d.symbolic_header.ifdMax * external_fdr_size);
12713 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
12714 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
12715
12716 mips_elf_tdata (abfd)->find_line_info = fi;
12717
12718 /* Note that we don't bother to ever free this information.
12719 find_nearest_line is either called all the time, as in
12720 objdump -l, so the information should be saved, or it is
12721 rarely called, as in ld error messages, so the memory
12722 wasted is unimportant. Still, it would probably be a
12723 good idea for free_cached_info to throw it away. */
12724 }
12725
12726 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
12727 &fi->i, filename_ptr, functionname_ptr,
12728 line_ptr))
12729 {
12730 msec->flags = origflags;
12731 return TRUE;
12732 }
12733
12734 msec->flags = origflags;
12735 }
12736
12737 /* Fall back on the generic ELF find_nearest_line routine. */
12738
12739 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
12740 filename_ptr, functionname_ptr,
12741 line_ptr, discriminator_ptr);
12742 }
12743
12744 bfd_boolean
_bfd_mips_elf_find_inliner_info(bfd * abfd,const char ** filename_ptr,const char ** functionname_ptr,unsigned int * line_ptr)12745 _bfd_mips_elf_find_inliner_info (bfd *abfd,
12746 const char **filename_ptr,
12747 const char **functionname_ptr,
12748 unsigned int *line_ptr)
12749 {
12750 bfd_boolean found;
12751 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
12752 functionname_ptr, line_ptr,
12753 & elf_tdata (abfd)->dwarf2_find_line_info);
12754 return found;
12755 }
12756
12757
12758 /* When are writing out the .options or .MIPS.options section,
12759 remember the bytes we are writing out, so that we can install the
12760 GP value in the section_processing routine. */
12761
12762 bfd_boolean
_bfd_mips_elf_set_section_contents(bfd * abfd,sec_ptr section,const void * location,file_ptr offset,bfd_size_type count)12763 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
12764 const void *location,
12765 file_ptr offset, bfd_size_type count)
12766 {
12767 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
12768 {
12769 bfd_byte *c;
12770
12771 if (elf_section_data (section) == NULL)
12772 {
12773 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
12774 section->used_by_bfd = bfd_zalloc (abfd, amt);
12775 if (elf_section_data (section) == NULL)
12776 return FALSE;
12777 }
12778 c = mips_elf_section_data (section)->u.tdata;
12779 if (c == NULL)
12780 {
12781 c = bfd_zalloc (abfd, section->size);
12782 if (c == NULL)
12783 return FALSE;
12784 mips_elf_section_data (section)->u.tdata = c;
12785 }
12786
12787 memcpy (c + offset, location, count);
12788 }
12789
12790 return _bfd_elf_set_section_contents (abfd, section, location, offset,
12791 count);
12792 }
12793
12794 /* This is almost identical to bfd_generic_get_... except that some
12795 MIPS relocations need to be handled specially. Sigh. */
12796
12797 bfd_byte *
_bfd_elf_mips_get_relocated_section_contents(bfd * abfd,struct bfd_link_info * link_info,struct bfd_link_order * link_order,bfd_byte * data,bfd_boolean relocatable,asymbol ** symbols)12798 _bfd_elf_mips_get_relocated_section_contents
12799 (bfd *abfd,
12800 struct bfd_link_info *link_info,
12801 struct bfd_link_order *link_order,
12802 bfd_byte *data,
12803 bfd_boolean relocatable,
12804 asymbol **symbols)
12805 {
12806 /* Get enough memory to hold the stuff */
12807 bfd *input_bfd = link_order->u.indirect.section->owner;
12808 asection *input_section = link_order->u.indirect.section;
12809 bfd_size_type sz;
12810
12811 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12812 arelent **reloc_vector = NULL;
12813 long reloc_count;
12814
12815 if (reloc_size < 0)
12816 goto error_return;
12817
12818 reloc_vector = bfd_malloc (reloc_size);
12819 if (reloc_vector == NULL && reloc_size != 0)
12820 goto error_return;
12821
12822 /* read in the section */
12823 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
12824 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
12825 goto error_return;
12826
12827 reloc_count = bfd_canonicalize_reloc (input_bfd,
12828 input_section,
12829 reloc_vector,
12830 symbols);
12831 if (reloc_count < 0)
12832 goto error_return;
12833
12834 if (reloc_count > 0)
12835 {
12836 arelent **parent;
12837 /* for mips */
12838 int gp_found;
12839 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
12840
12841 {
12842 struct bfd_hash_entry *h;
12843 struct bfd_link_hash_entry *lh;
12844 /* Skip all this stuff if we aren't mixing formats. */
12845 if (abfd && input_bfd
12846 && abfd->xvec == input_bfd->xvec)
12847 lh = 0;
12848 else
12849 {
12850 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
12851 lh = (struct bfd_link_hash_entry *) h;
12852 }
12853 lookup:
12854 if (lh)
12855 {
12856 switch (lh->type)
12857 {
12858 case bfd_link_hash_undefined:
12859 case bfd_link_hash_undefweak:
12860 case bfd_link_hash_common:
12861 gp_found = 0;
12862 break;
12863 case bfd_link_hash_defined:
12864 case bfd_link_hash_defweak:
12865 gp_found = 1;
12866 gp = lh->u.def.value;
12867 break;
12868 case bfd_link_hash_indirect:
12869 case bfd_link_hash_warning:
12870 lh = lh->u.i.link;
12871 /* @@FIXME ignoring warning for now */
12872 goto lookup;
12873 case bfd_link_hash_new:
12874 default:
12875 abort ();
12876 }
12877 }
12878 else
12879 gp_found = 0;
12880 }
12881 /* end mips */
12882 for (parent = reloc_vector; *parent != NULL; parent++)
12883 {
12884 char *error_message = NULL;
12885 bfd_reloc_status_type r;
12886
12887 /* Specific to MIPS: Deal with relocation types that require
12888 knowing the gp of the output bfd. */
12889 asymbol *sym = *(*parent)->sym_ptr_ptr;
12890
12891 /* If we've managed to find the gp and have a special
12892 function for the relocation then go ahead, else default
12893 to the generic handling. */
12894 if (gp_found
12895 && (*parent)->howto->special_function
12896 == _bfd_mips_elf32_gprel16_reloc)
12897 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
12898 input_section, relocatable,
12899 data, gp);
12900 else
12901 r = bfd_perform_relocation (input_bfd, *parent, data,
12902 input_section,
12903 relocatable ? abfd : NULL,
12904 &error_message);
12905
12906 if (relocatable)
12907 {
12908 asection *os = input_section->output_section;
12909
12910 /* A partial link, so keep the relocs */
12911 os->orelocation[os->reloc_count] = *parent;
12912 os->reloc_count++;
12913 }
12914
12915 if (r != bfd_reloc_ok)
12916 {
12917 switch (r)
12918 {
12919 case bfd_reloc_undefined:
12920 if (!((*link_info->callbacks->undefined_symbol)
12921 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12922 input_bfd, input_section, (*parent)->address, TRUE)))
12923 goto error_return;
12924 break;
12925 case bfd_reloc_dangerous:
12926 BFD_ASSERT (error_message != NULL);
12927 if (!((*link_info->callbacks->reloc_dangerous)
12928 (link_info, error_message, input_bfd, input_section,
12929 (*parent)->address)))
12930 goto error_return;
12931 break;
12932 case bfd_reloc_overflow:
12933 if (!((*link_info->callbacks->reloc_overflow)
12934 (link_info, NULL,
12935 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12936 (*parent)->howto->name, (*parent)->addend,
12937 input_bfd, input_section, (*parent)->address)))
12938 goto error_return;
12939 break;
12940 case bfd_reloc_outofrange:
12941 default:
12942 abort ();
12943 break;
12944 }
12945
12946 }
12947 }
12948 }
12949 if (reloc_vector != NULL)
12950 free (reloc_vector);
12951 return data;
12952
12953 error_return:
12954 if (reloc_vector != NULL)
12955 free (reloc_vector);
12956 return NULL;
12957 }
12958
12959 static bfd_boolean
mips_elf_relax_delete_bytes(bfd * abfd,asection * sec,bfd_vma addr,int count)12960 mips_elf_relax_delete_bytes (bfd *abfd,
12961 asection *sec, bfd_vma addr, int count)
12962 {
12963 Elf_Internal_Shdr *symtab_hdr;
12964 unsigned int sec_shndx;
12965 bfd_byte *contents;
12966 Elf_Internal_Rela *irel, *irelend;
12967 Elf_Internal_Sym *isym;
12968 Elf_Internal_Sym *isymend;
12969 struct elf_link_hash_entry **sym_hashes;
12970 struct elf_link_hash_entry **end_hashes;
12971 struct elf_link_hash_entry **start_hashes;
12972 unsigned int symcount;
12973
12974 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
12975 contents = elf_section_data (sec)->this_hdr.contents;
12976
12977 irel = elf_section_data (sec)->relocs;
12978 irelend = irel + sec->reloc_count;
12979
12980 /* Actually delete the bytes. */
12981 memmove (contents + addr, contents + addr + count,
12982 (size_t) (sec->size - addr - count));
12983 sec->size -= count;
12984
12985 /* Adjust all the relocs. */
12986 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
12987 {
12988 /* Get the new reloc address. */
12989 if (irel->r_offset > addr)
12990 irel->r_offset -= count;
12991 }
12992
12993 BFD_ASSERT (addr % 2 == 0);
12994 BFD_ASSERT (count % 2 == 0);
12995
12996 /* Adjust the local symbols defined in this section. */
12997 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12998 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
12999 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
13000 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
13001 isym->st_value -= count;
13002
13003 /* Now adjust the global symbols defined in this section. */
13004 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13005 - symtab_hdr->sh_info);
13006 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13007 end_hashes = sym_hashes + symcount;
13008
13009 for (; sym_hashes < end_hashes; sym_hashes++)
13010 {
13011 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13012
13013 if ((sym_hash->root.type == bfd_link_hash_defined
13014 || sym_hash->root.type == bfd_link_hash_defweak)
13015 && sym_hash->root.u.def.section == sec)
13016 {
13017 bfd_vma value = sym_hash->root.u.def.value;
13018
13019 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13020 value &= MINUS_TWO;
13021 if (value > addr)
13022 sym_hash->root.u.def.value -= count;
13023 }
13024 }
13025
13026 return TRUE;
13027 }
13028
13029
13030 /* Opcodes needed for microMIPS relaxation as found in
13031 opcodes/micromips-opc.c. */
13032
13033 struct opcode_descriptor {
13034 unsigned long match;
13035 unsigned long mask;
13036 };
13037
13038 /* The $ra register aka $31. */
13039
13040 #define RA 31
13041
13042 /* 32-bit instruction format register fields. */
13043
13044 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13045 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13046
13047 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
13048
13049 #define OP16_VALID_REG(r) \
13050 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13051
13052
13053 /* 32-bit and 16-bit branches. */
13054
13055 static const struct opcode_descriptor b_insns_32[] = {
13056 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13057 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13058 { 0, 0 } /* End marker for find_match(). */
13059 };
13060
13061 static const struct opcode_descriptor bc_insn_32 =
13062 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13063
13064 static const struct opcode_descriptor bz_insn_32 =
13065 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13066
13067 static const struct opcode_descriptor bzal_insn_32 =
13068 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13069
13070 static const struct opcode_descriptor beq_insn_32 =
13071 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13072
13073 static const struct opcode_descriptor b_insn_16 =
13074 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13075
13076 static const struct opcode_descriptor bz_insn_16 =
13077 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
13078
13079
13080 /* 32-bit and 16-bit branch EQ and NE zero. */
13081
13082 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13083 eq and second the ne. This convention is used when replacing a
13084 32-bit BEQ/BNE with the 16-bit version. */
13085
13086 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13087
13088 static const struct opcode_descriptor bz_rs_insns_32[] = {
13089 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13090 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13091 { 0, 0 } /* End marker for find_match(). */
13092 };
13093
13094 static const struct opcode_descriptor bz_rt_insns_32[] = {
13095 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13096 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13097 { 0, 0 } /* End marker for find_match(). */
13098 };
13099
13100 static const struct opcode_descriptor bzc_insns_32[] = {
13101 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13102 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13103 { 0, 0 } /* End marker for find_match(). */
13104 };
13105
13106 static const struct opcode_descriptor bz_insns_16[] = {
13107 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13108 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13109 { 0, 0 } /* End marker for find_match(). */
13110 };
13111
13112 /* Switch between a 5-bit register index and its 3-bit shorthand. */
13113
13114 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
13115 #define BZ16_REG_FIELD(r) \
13116 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
13117
13118
13119 /* 32-bit instructions with a delay slot. */
13120
13121 static const struct opcode_descriptor jal_insn_32_bd16 =
13122 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13123
13124 static const struct opcode_descriptor jal_insn_32_bd32 =
13125 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13126
13127 static const struct opcode_descriptor jal_x_insn_32_bd32 =
13128 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13129
13130 static const struct opcode_descriptor j_insn_32 =
13131 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13132
13133 static const struct opcode_descriptor jalr_insn_32 =
13134 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13135
13136 /* This table can be compacted, because no opcode replacement is made. */
13137
13138 static const struct opcode_descriptor ds_insns_32_bd16[] = {
13139 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13140
13141 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13142 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13143
13144 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13145 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13146 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13147 { 0, 0 } /* End marker for find_match(). */
13148 };
13149
13150 /* This table can be compacted, because no opcode replacement is made. */
13151
13152 static const struct opcode_descriptor ds_insns_32_bd32[] = {
13153 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13154
13155 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13156 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13157 { 0, 0 } /* End marker for find_match(). */
13158 };
13159
13160
13161 /* 16-bit instructions with a delay slot. */
13162
13163 static const struct opcode_descriptor jalr_insn_16_bd16 =
13164 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13165
13166 static const struct opcode_descriptor jalr_insn_16_bd32 =
13167 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13168
13169 static const struct opcode_descriptor jr_insn_16 =
13170 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13171
13172 #define JR16_REG(opcode) ((opcode) & 0x1f)
13173
13174 /* This table can be compacted, because no opcode replacement is made. */
13175
13176 static const struct opcode_descriptor ds_insns_16_bd16[] = {
13177 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13178
13179 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13180 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13181 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13182 { 0, 0 } /* End marker for find_match(). */
13183 };
13184
13185
13186 /* LUI instruction. */
13187
13188 static const struct opcode_descriptor lui_insn =
13189 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13190
13191
13192 /* ADDIU instruction. */
13193
13194 static const struct opcode_descriptor addiu_insn =
13195 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13196
13197 static const struct opcode_descriptor addiupc_insn =
13198 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13199
13200 #define ADDIUPC_REG_FIELD(r) \
13201 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13202
13203
13204 /* Relaxable instructions in a JAL delay slot: MOVE. */
13205
13206 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13207 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13208 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13209 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13210
13211 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13212 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13213
13214 static const struct opcode_descriptor move_insns_32[] = {
13215 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
13216 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
13217 { 0, 0 } /* End marker for find_match(). */
13218 };
13219
13220 static const struct opcode_descriptor move_insn_16 =
13221 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13222
13223
13224 /* NOP instructions. */
13225
13226 static const struct opcode_descriptor nop_insn_32 =
13227 { /* "nop", "", */ 0x00000000, 0xffffffff };
13228
13229 static const struct opcode_descriptor nop_insn_16 =
13230 { /* "nop", "", */ 0x0c00, 0xffff };
13231
13232
13233 /* Instruction match support. */
13234
13235 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13236
13237 static int
find_match(unsigned long opcode,const struct opcode_descriptor insn[])13238 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13239 {
13240 unsigned long indx;
13241
13242 for (indx = 0; insn[indx].mask != 0; indx++)
13243 if (MATCH (opcode, insn[indx]))
13244 return indx;
13245
13246 return -1;
13247 }
13248
13249
13250 /* Branch and delay slot decoding support. */
13251
13252 /* If PTR points to what *might* be a 16-bit branch or jump, then
13253 return the minimum length of its delay slot, otherwise return 0.
13254 Non-zero results are not definitive as we might be checking against
13255 the second half of another instruction. */
13256
13257 static int
check_br16_dslot(bfd * abfd,bfd_byte * ptr)13258 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13259 {
13260 unsigned long opcode;
13261 int bdsize;
13262
13263 opcode = bfd_get_16 (abfd, ptr);
13264 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13265 /* 16-bit branch/jump with a 32-bit delay slot. */
13266 bdsize = 4;
13267 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13268 || find_match (opcode, ds_insns_16_bd16) >= 0)
13269 /* 16-bit branch/jump with a 16-bit delay slot. */
13270 bdsize = 2;
13271 else
13272 /* No delay slot. */
13273 bdsize = 0;
13274
13275 return bdsize;
13276 }
13277
13278 /* If PTR points to what *might* be a 32-bit branch or jump, then
13279 return the minimum length of its delay slot, otherwise return 0.
13280 Non-zero results are not definitive as we might be checking against
13281 the second half of another instruction. */
13282
13283 static int
check_br32_dslot(bfd * abfd,bfd_byte * ptr)13284 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13285 {
13286 unsigned long opcode;
13287 int bdsize;
13288
13289 opcode = bfd_get_micromips_32 (abfd, ptr);
13290 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13291 /* 32-bit branch/jump with a 32-bit delay slot. */
13292 bdsize = 4;
13293 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13294 /* 32-bit branch/jump with a 16-bit delay slot. */
13295 bdsize = 2;
13296 else
13297 /* No delay slot. */
13298 bdsize = 0;
13299
13300 return bdsize;
13301 }
13302
13303 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13304 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13305
13306 static bfd_boolean
check_br16(bfd * abfd,bfd_byte * ptr,unsigned long reg)13307 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13308 {
13309 unsigned long opcode;
13310
13311 opcode = bfd_get_16 (abfd, ptr);
13312 if (MATCH (opcode, b_insn_16)
13313 /* B16 */
13314 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13315 /* JR16 */
13316 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13317 /* BEQZ16, BNEZ16 */
13318 || (MATCH (opcode, jalr_insn_16_bd32)
13319 /* JALR16 */
13320 && reg != JR16_REG (opcode) && reg != RA))
13321 return TRUE;
13322
13323 return FALSE;
13324 }
13325
13326 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13327 then return TRUE, otherwise FALSE. */
13328
13329 static bfd_boolean
check_br32(bfd * abfd,bfd_byte * ptr,unsigned long reg)13330 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13331 {
13332 unsigned long opcode;
13333
13334 opcode = bfd_get_micromips_32 (abfd, ptr);
13335 if (MATCH (opcode, j_insn_32)
13336 /* J */
13337 || MATCH (opcode, bc_insn_32)
13338 /* BC1F, BC1T, BC2F, BC2T */
13339 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13340 /* JAL, JALX */
13341 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13342 /* BGEZ, BGTZ, BLEZ, BLTZ */
13343 || (MATCH (opcode, bzal_insn_32)
13344 /* BGEZAL, BLTZAL */
13345 && reg != OP32_SREG (opcode) && reg != RA)
13346 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13347 /* JALR, JALR.HB, BEQ, BNE */
13348 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13349 return TRUE;
13350
13351 return FALSE;
13352 }
13353
13354 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13355 IRELEND) at OFFSET indicate that there must be a compact branch there,
13356 then return TRUE, otherwise FALSE. */
13357
13358 static bfd_boolean
check_relocated_bzc(bfd * abfd,const bfd_byte * ptr,bfd_vma offset,const Elf_Internal_Rela * internal_relocs,const Elf_Internal_Rela * irelend)13359 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13360 const Elf_Internal_Rela *internal_relocs,
13361 const Elf_Internal_Rela *irelend)
13362 {
13363 const Elf_Internal_Rela *irel;
13364 unsigned long opcode;
13365
13366 opcode = bfd_get_micromips_32 (abfd, ptr);
13367 if (find_match (opcode, bzc_insns_32) < 0)
13368 return FALSE;
13369
13370 for (irel = internal_relocs; irel < irelend; irel++)
13371 if (irel->r_offset == offset
13372 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13373 return TRUE;
13374
13375 return FALSE;
13376 }
13377
13378 /* Bitsize checking. */
13379 #define IS_BITSIZE(val, N) \
13380 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13381 - (1ULL << ((N) - 1))) == (val))
13382
13383
13384 bfd_boolean
_bfd_mips_elf_relax_section(bfd * abfd,asection * sec,struct bfd_link_info * link_info,bfd_boolean * again)13385 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13386 struct bfd_link_info *link_info,
13387 bfd_boolean *again)
13388 {
13389 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
13390 Elf_Internal_Shdr *symtab_hdr;
13391 Elf_Internal_Rela *internal_relocs;
13392 Elf_Internal_Rela *irel, *irelend;
13393 bfd_byte *contents = NULL;
13394 Elf_Internal_Sym *isymbuf = NULL;
13395
13396 /* Assume nothing changes. */
13397 *again = FALSE;
13398
13399 /* We don't have to do anything for a relocatable link, if
13400 this section does not have relocs, or if this is not a
13401 code section. */
13402
13403 if (link_info->relocatable
13404 || (sec->flags & SEC_RELOC) == 0
13405 || sec->reloc_count == 0
13406 || (sec->flags & SEC_CODE) == 0)
13407 return TRUE;
13408
13409 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13410
13411 /* Get a copy of the native relocations. */
13412 internal_relocs = (_bfd_elf_link_read_relocs
13413 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
13414 link_info->keep_memory));
13415 if (internal_relocs == NULL)
13416 goto error_return;
13417
13418 /* Walk through them looking for relaxing opportunities. */
13419 irelend = internal_relocs + sec->reloc_count;
13420 for (irel = internal_relocs; irel < irelend; irel++)
13421 {
13422 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13423 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13424 bfd_boolean target_is_micromips_code_p;
13425 unsigned long opcode;
13426 bfd_vma symval;
13427 bfd_vma pcrval;
13428 bfd_byte *ptr;
13429 int fndopc;
13430
13431 /* The number of bytes to delete for relaxation and from where
13432 to delete these bytes starting at irel->r_offset. */
13433 int delcnt = 0;
13434 int deloff = 0;
13435
13436 /* If this isn't something that can be relaxed, then ignore
13437 this reloc. */
13438 if (r_type != R_MICROMIPS_HI16
13439 && r_type != R_MICROMIPS_PC16_S1
13440 && r_type != R_MICROMIPS_26_S1)
13441 continue;
13442
13443 /* Get the section contents if we haven't done so already. */
13444 if (contents == NULL)
13445 {
13446 /* Get cached copy if it exists. */
13447 if (elf_section_data (sec)->this_hdr.contents != NULL)
13448 contents = elf_section_data (sec)->this_hdr.contents;
13449 /* Go get them off disk. */
13450 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13451 goto error_return;
13452 }
13453 ptr = contents + irel->r_offset;
13454
13455 /* Read this BFD's local symbols if we haven't done so already. */
13456 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13457 {
13458 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13459 if (isymbuf == NULL)
13460 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13461 symtab_hdr->sh_info, 0,
13462 NULL, NULL, NULL);
13463 if (isymbuf == NULL)
13464 goto error_return;
13465 }
13466
13467 /* Get the value of the symbol referred to by the reloc. */
13468 if (r_symndx < symtab_hdr->sh_info)
13469 {
13470 /* A local symbol. */
13471 Elf_Internal_Sym *isym;
13472 asection *sym_sec;
13473
13474 isym = isymbuf + r_symndx;
13475 if (isym->st_shndx == SHN_UNDEF)
13476 sym_sec = bfd_und_section_ptr;
13477 else if (isym->st_shndx == SHN_ABS)
13478 sym_sec = bfd_abs_section_ptr;
13479 else if (isym->st_shndx == SHN_COMMON)
13480 sym_sec = bfd_com_section_ptr;
13481 else
13482 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13483 symval = (isym->st_value
13484 + sym_sec->output_section->vma
13485 + sym_sec->output_offset);
13486 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13487 }
13488 else
13489 {
13490 unsigned long indx;
13491 struct elf_link_hash_entry *h;
13492
13493 /* An external symbol. */
13494 indx = r_symndx - symtab_hdr->sh_info;
13495 h = elf_sym_hashes (abfd)[indx];
13496 BFD_ASSERT (h != NULL);
13497
13498 if (h->root.type != bfd_link_hash_defined
13499 && h->root.type != bfd_link_hash_defweak)
13500 /* This appears to be a reference to an undefined
13501 symbol. Just ignore it -- it will be caught by the
13502 regular reloc processing. */
13503 continue;
13504
13505 symval = (h->root.u.def.value
13506 + h->root.u.def.section->output_section->vma
13507 + h->root.u.def.section->output_offset);
13508 target_is_micromips_code_p = (!h->needs_plt
13509 && ELF_ST_IS_MICROMIPS (h->other));
13510 }
13511
13512
13513 /* For simplicity of coding, we are going to modify the
13514 section contents, the section relocs, and the BFD symbol
13515 table. We must tell the rest of the code not to free up this
13516 information. It would be possible to instead create a table
13517 of changes which have to be made, as is done in coff-mips.c;
13518 that would be more work, but would require less memory when
13519 the linker is run. */
13520
13521 /* Only 32-bit instructions relaxed. */
13522 if (irel->r_offset + 4 > sec->size)
13523 continue;
13524
13525 opcode = bfd_get_micromips_32 (abfd, ptr);
13526
13527 /* This is the pc-relative distance from the instruction the
13528 relocation is applied to, to the symbol referred. */
13529 pcrval = (symval
13530 - (sec->output_section->vma + sec->output_offset)
13531 - irel->r_offset);
13532
13533 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13534 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13535 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13536
13537 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13538
13539 where pcrval has first to be adjusted to apply against the LO16
13540 location (we make the adjustment later on, when we have figured
13541 out the offset). */
13542 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13543 {
13544 bfd_boolean bzc = FALSE;
13545 unsigned long nextopc;
13546 unsigned long reg;
13547 bfd_vma offset;
13548
13549 /* Give up if the previous reloc was a HI16 against this symbol
13550 too. */
13551 if (irel > internal_relocs
13552 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13553 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13554 continue;
13555
13556 /* Or if the next reloc is not a LO16 against this symbol. */
13557 if (irel + 1 >= irelend
13558 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13559 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13560 continue;
13561
13562 /* Or if the second next reloc is a LO16 against this symbol too. */
13563 if (irel + 2 >= irelend
13564 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13565 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13566 continue;
13567
13568 /* See if the LUI instruction *might* be in a branch delay slot.
13569 We check whether what looks like a 16-bit branch or jump is
13570 actually an immediate argument to a compact branch, and let
13571 it through if so. */
13572 if (irel->r_offset >= 2
13573 && check_br16_dslot (abfd, ptr - 2)
13574 && !(irel->r_offset >= 4
13575 && (bzc = check_relocated_bzc (abfd,
13576 ptr - 4, irel->r_offset - 4,
13577 internal_relocs, irelend))))
13578 continue;
13579 if (irel->r_offset >= 4
13580 && !bzc
13581 && check_br32_dslot (abfd, ptr - 4))
13582 continue;
13583
13584 reg = OP32_SREG (opcode);
13585
13586 /* We only relax adjacent instructions or ones separated with
13587 a branch or jump that has a delay slot. The branch or jump
13588 must not fiddle with the register used to hold the address.
13589 Subtract 4 for the LUI itself. */
13590 offset = irel[1].r_offset - irel[0].r_offset;
13591 switch (offset - 4)
13592 {
13593 case 0:
13594 break;
13595 case 2:
13596 if (check_br16 (abfd, ptr + 4, reg))
13597 break;
13598 continue;
13599 case 4:
13600 if (check_br32 (abfd, ptr + 4, reg))
13601 break;
13602 continue;
13603 default:
13604 continue;
13605 }
13606
13607 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
13608
13609 /* Give up unless the same register is used with both
13610 relocations. */
13611 if (OP32_SREG (nextopc) != reg)
13612 continue;
13613
13614 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13615 and rounding up to take masking of the two LSBs into account. */
13616 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13617
13618 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13619 if (IS_BITSIZE (symval, 16))
13620 {
13621 /* Fix the relocation's type. */
13622 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13623
13624 /* Instructions using R_MICROMIPS_LO16 have the base or
13625 source register in bits 20:16. This register becomes $0
13626 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
13627 nextopc &= ~0x001f0000;
13628 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13629 contents + irel[1].r_offset);
13630 }
13631
13632 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13633 We add 4 to take LUI deletion into account while checking
13634 the PC-relative distance. */
13635 else if (symval % 4 == 0
13636 && IS_BITSIZE (pcrval + 4, 25)
13637 && MATCH (nextopc, addiu_insn)
13638 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13639 && OP16_VALID_REG (OP32_TREG (nextopc)))
13640 {
13641 /* Fix the relocation's type. */
13642 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13643
13644 /* Replace ADDIU with the ADDIUPC version. */
13645 nextopc = (addiupc_insn.match
13646 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13647
13648 bfd_put_micromips_32 (abfd, nextopc,
13649 contents + irel[1].r_offset);
13650 }
13651
13652 /* Can't do anything, give up, sigh... */
13653 else
13654 continue;
13655
13656 /* Fix the relocation's type. */
13657 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13658
13659 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13660 delcnt = 4;
13661 deloff = 0;
13662 }
13663
13664 /* Compact branch relaxation -- due to the multitude of macros
13665 employed by the compiler/assembler, compact branches are not
13666 always generated. Obviously, this can/will be fixed elsewhere,
13667 but there is no drawback in double checking it here. */
13668 else if (r_type == R_MICROMIPS_PC16_S1
13669 && irel->r_offset + 5 < sec->size
13670 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13671 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
13672 && ((!insn32
13673 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13674 nop_insn_16) ? 2 : 0))
13675 || (irel->r_offset + 7 < sec->size
13676 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13677 ptr + 4),
13678 nop_insn_32) ? 4 : 0))))
13679 {
13680 unsigned long reg;
13681
13682 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13683
13684 /* Replace BEQZ/BNEZ with the compact version. */
13685 opcode = (bzc_insns_32[fndopc].match
13686 | BZC32_REG_FIELD (reg)
13687 | (opcode & 0xffff)); /* Addend value. */
13688
13689 bfd_put_micromips_32 (abfd, opcode, ptr);
13690
13691 /* Delete the delay slot NOP: two or four bytes from
13692 irel->offset + 4; delcnt has already been set above. */
13693 deloff = 4;
13694 }
13695
13696 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
13697 to check the distance from the next instruction, so subtract 2. */
13698 else if (!insn32
13699 && r_type == R_MICROMIPS_PC16_S1
13700 && IS_BITSIZE (pcrval - 2, 11)
13701 && find_match (opcode, b_insns_32) >= 0)
13702 {
13703 /* Fix the relocation's type. */
13704 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
13705
13706 /* Replace the 32-bit opcode with a 16-bit opcode. */
13707 bfd_put_16 (abfd,
13708 (b_insn_16.match
13709 | (opcode & 0x3ff)), /* Addend value. */
13710 ptr);
13711
13712 /* Delete 2 bytes from irel->r_offset + 2. */
13713 delcnt = 2;
13714 deloff = 2;
13715 }
13716
13717 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
13718 to check the distance from the next instruction, so subtract 2. */
13719 else if (!insn32
13720 && r_type == R_MICROMIPS_PC16_S1
13721 && IS_BITSIZE (pcrval - 2, 8)
13722 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13723 && OP16_VALID_REG (OP32_SREG (opcode)))
13724 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
13725 && OP16_VALID_REG (OP32_TREG (opcode)))))
13726 {
13727 unsigned long reg;
13728
13729 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13730
13731 /* Fix the relocation's type. */
13732 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
13733
13734 /* Replace the 32-bit opcode with a 16-bit opcode. */
13735 bfd_put_16 (abfd,
13736 (bz_insns_16[fndopc].match
13737 | BZ16_REG_FIELD (reg)
13738 | (opcode & 0x7f)), /* Addend value. */
13739 ptr);
13740
13741 /* Delete 2 bytes from irel->r_offset + 2. */
13742 delcnt = 2;
13743 deloff = 2;
13744 }
13745
13746 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
13747 else if (!insn32
13748 && r_type == R_MICROMIPS_26_S1
13749 && target_is_micromips_code_p
13750 && irel->r_offset + 7 < sec->size
13751 && MATCH (opcode, jal_insn_32_bd32))
13752 {
13753 unsigned long n32opc;
13754 bfd_boolean relaxed = FALSE;
13755
13756 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
13757
13758 if (MATCH (n32opc, nop_insn_32))
13759 {
13760 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
13761 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
13762
13763 relaxed = TRUE;
13764 }
13765 else if (find_match (n32opc, move_insns_32) >= 0)
13766 {
13767 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
13768 bfd_put_16 (abfd,
13769 (move_insn_16.match
13770 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
13771 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
13772 ptr + 4);
13773
13774 relaxed = TRUE;
13775 }
13776 /* Other 32-bit instructions relaxable to 16-bit
13777 instructions will be handled here later. */
13778
13779 if (relaxed)
13780 {
13781 /* JAL with 32-bit delay slot that is changed to a JALS
13782 with 16-bit delay slot. */
13783 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
13784
13785 /* Delete 2 bytes from irel->r_offset + 6. */
13786 delcnt = 2;
13787 deloff = 6;
13788 }
13789 }
13790
13791 if (delcnt != 0)
13792 {
13793 /* Note that we've changed the relocs, section contents, etc. */
13794 elf_section_data (sec)->relocs = internal_relocs;
13795 elf_section_data (sec)->this_hdr.contents = contents;
13796 symtab_hdr->contents = (unsigned char *) isymbuf;
13797
13798 /* Delete bytes depending on the delcnt and deloff. */
13799 if (!mips_elf_relax_delete_bytes (abfd, sec,
13800 irel->r_offset + deloff, delcnt))
13801 goto error_return;
13802
13803 /* That will change things, so we should relax again.
13804 Note that this is not required, and it may be slow. */
13805 *again = TRUE;
13806 }
13807 }
13808
13809 if (isymbuf != NULL
13810 && symtab_hdr->contents != (unsigned char *) isymbuf)
13811 {
13812 if (! link_info->keep_memory)
13813 free (isymbuf);
13814 else
13815 {
13816 /* Cache the symbols for elf_link_input_bfd. */
13817 symtab_hdr->contents = (unsigned char *) isymbuf;
13818 }
13819 }
13820
13821 if (contents != NULL
13822 && elf_section_data (sec)->this_hdr.contents != contents)
13823 {
13824 if (! link_info->keep_memory)
13825 free (contents);
13826 else
13827 {
13828 /* Cache the section contents for elf_link_input_bfd. */
13829 elf_section_data (sec)->this_hdr.contents = contents;
13830 }
13831 }
13832
13833 if (internal_relocs != NULL
13834 && elf_section_data (sec)->relocs != internal_relocs)
13835 free (internal_relocs);
13836
13837 return TRUE;
13838
13839 error_return:
13840 if (isymbuf != NULL
13841 && symtab_hdr->contents != (unsigned char *) isymbuf)
13842 free (isymbuf);
13843 if (contents != NULL
13844 && elf_section_data (sec)->this_hdr.contents != contents)
13845 free (contents);
13846 if (internal_relocs != NULL
13847 && elf_section_data (sec)->relocs != internal_relocs)
13848 free (internal_relocs);
13849
13850 return FALSE;
13851 }
13852
13853 /* Create a MIPS ELF linker hash table. */
13854
13855 struct bfd_link_hash_table *
_bfd_mips_elf_link_hash_table_create(bfd * abfd)13856 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
13857 {
13858 struct mips_elf_link_hash_table *ret;
13859 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
13860
13861 ret = bfd_zmalloc (amt);
13862 if (ret == NULL)
13863 return NULL;
13864
13865 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
13866 mips_elf_link_hash_newfunc,
13867 sizeof (struct mips_elf_link_hash_entry),
13868 MIPS_ELF_DATA))
13869 {
13870 free (ret);
13871 return NULL;
13872 }
13873 ret->root.init_plt_refcount.plist = NULL;
13874 ret->root.init_plt_offset.plist = NULL;
13875
13876 return &ret->root.root;
13877 }
13878
13879 /* Likewise, but indicate that the target is VxWorks. */
13880
13881 struct bfd_link_hash_table *
_bfd_mips_vxworks_link_hash_table_create(bfd * abfd)13882 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
13883 {
13884 struct bfd_link_hash_table *ret;
13885
13886 ret = _bfd_mips_elf_link_hash_table_create (abfd);
13887 if (ret)
13888 {
13889 struct mips_elf_link_hash_table *htab;
13890
13891 htab = (struct mips_elf_link_hash_table *) ret;
13892 htab->use_plts_and_copy_relocs = TRUE;
13893 htab->is_vxworks = TRUE;
13894 }
13895 return ret;
13896 }
13897
13898 /* A function that the linker calls if we are allowed to use PLTs
13899 and copy relocs. */
13900
13901 void
_bfd_mips_elf_use_plts_and_copy_relocs(struct bfd_link_info * info)13902 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
13903 {
13904 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
13905 }
13906
13907 /* A function that the linker calls to select between all or only
13908 32-bit microMIPS instructions. */
13909
13910 void
_bfd_mips_elf_insn32(struct bfd_link_info * info,bfd_boolean on)13911 _bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
13912 {
13913 mips_elf_hash_table (info)->insn32 = on;
13914 }
13915
13916 void
_bfd_mips_elf_compact_branches(struct bfd_link_info * info,bfd_boolean on)13917 _bfd_mips_elf_compact_branches (struct bfd_link_info *info, bfd_boolean on)
13918 {
13919 mips_elf_hash_table (info)->compact_branches = on;
13920 }
13921
13922
13923 /* Return the .MIPS.abiflags value representing each ISA Extension. */
13924
13925 unsigned int
bfd_mips_isa_ext(bfd * abfd)13926 bfd_mips_isa_ext (bfd *abfd)
13927 {
13928 switch (bfd_get_mach (abfd))
13929 {
13930 case bfd_mach_mips3900:
13931 return AFL_EXT_3900;
13932 case bfd_mach_mips4010:
13933 return AFL_EXT_4010;
13934 case bfd_mach_mips4100:
13935 return AFL_EXT_4100;
13936 case bfd_mach_mips4111:
13937 return AFL_EXT_4111;
13938 case bfd_mach_mips4120:
13939 return AFL_EXT_4120;
13940 case bfd_mach_mips4650:
13941 return AFL_EXT_4650;
13942 case bfd_mach_mips5400:
13943 return AFL_EXT_5400;
13944 case bfd_mach_mips5500:
13945 return AFL_EXT_5500;
13946 case bfd_mach_mips5900:
13947 return AFL_EXT_5900;
13948 case bfd_mach_mips10000:
13949 return AFL_EXT_10000;
13950 case bfd_mach_mips_loongson_2e:
13951 return AFL_EXT_LOONGSON_2E;
13952 case bfd_mach_mips_loongson_2f:
13953 return AFL_EXT_LOONGSON_2F;
13954 case bfd_mach_mips_loongson_3a:
13955 return AFL_EXT_LOONGSON_3A;
13956 case bfd_mach_mips_sb1:
13957 return AFL_EXT_SB1;
13958 case bfd_mach_mips_octeon:
13959 return AFL_EXT_OCTEON;
13960 case bfd_mach_mips_octeonp:
13961 return AFL_EXT_OCTEONP;
13962 case bfd_mach_mips_octeon3:
13963 return AFL_EXT_OCTEON3;
13964 case bfd_mach_mips_octeon2:
13965 return AFL_EXT_OCTEON2;
13966 case bfd_mach_mips_xlr:
13967 return AFL_EXT_XLR;
13968 }
13969 return 0;
13970 }
13971
13972 /* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
13973
13974 static void
update_mips_abiflags_isa(bfd * abfd,Elf_Internal_ABIFlags_v0 * abiflags)13975 update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
13976 {
13977 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
13978 {
13979 case E_MIPS_ARCH_1:
13980 abiflags->isa_level = 1;
13981 abiflags->isa_rev = 0;
13982 break;
13983 case E_MIPS_ARCH_2:
13984 abiflags->isa_level = 2;
13985 abiflags->isa_rev = 0;
13986 break;
13987 case E_MIPS_ARCH_3:
13988 abiflags->isa_level = 3;
13989 abiflags->isa_rev = 0;
13990 break;
13991 case E_MIPS_ARCH_4:
13992 abiflags->isa_level = 4;
13993 abiflags->isa_rev = 0;
13994 break;
13995 case E_MIPS_ARCH_5:
13996 abiflags->isa_level = 5;
13997 abiflags->isa_rev = 0;
13998 break;
13999 case E_MIPS_ARCH_32:
14000 abiflags->isa_level = 32;
14001 abiflags->isa_rev = 1;
14002 break;
14003 case E_MIPS_ARCH_32R2:
14004 abiflags->isa_level = 32;
14005 /* Handle MIPS32r3 and MIPS32r5 which do not have a header flag. */
14006 if (abiflags->isa_rev < 2)
14007 abiflags->isa_rev = 2;
14008 break;
14009 case E_MIPS_ARCH_32R6:
14010 abiflags->isa_level = 32;
14011 abiflags->isa_rev = 6;
14012 break;
14013 case E_MIPS_ARCH_64:
14014 abiflags->isa_level = 64;
14015 abiflags->isa_rev = 1;
14016 break;
14017 case E_MIPS_ARCH_64R2:
14018 /* Handle MIPS64r3 and MIPS64r5 which do not have a header flag. */
14019 abiflags->isa_level = 64;
14020 if (abiflags->isa_rev < 2)
14021 abiflags->isa_rev = 2;
14022 break;
14023 case E_MIPS_ARCH_64R6:
14024 abiflags->isa_level = 64;
14025 abiflags->isa_rev = 6;
14026 break;
14027 default:
14028 (*_bfd_error_handler)
14029 (_("%B: Unknown architecture %s"),
14030 abfd, bfd_printable_name (abfd));
14031 }
14032
14033 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
14034 }
14035
14036 /* Return true if the given ELF header flags describe a 32-bit binary. */
14037
14038 static bfd_boolean
mips_32bit_flags_p(flagword flags)14039 mips_32bit_flags_p (flagword flags)
14040 {
14041 return ((flags & EF_MIPS_32BITMODE) != 0
14042 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14043 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14044 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14045 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14046 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14047 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14048 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
14049 }
14050
14051 /* Infer the content of the ABI flags based on the elf header. */
14052
14053 static void
infer_mips_abiflags(bfd * abfd,Elf_Internal_ABIFlags_v0 * abiflags)14054 infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14055 {
14056 obj_attribute *in_attr;
14057
14058 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14059 update_mips_abiflags_isa (abfd, abiflags);
14060
14061 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14062 abiflags->gpr_size = AFL_REG_32;
14063 else
14064 abiflags->gpr_size = AFL_REG_64;
14065
14066 abiflags->cpr1_size = AFL_REG_NONE;
14067
14068 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14069 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14070
14071 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14072 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14073 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14074 && abiflags->gpr_size == AFL_REG_32))
14075 abiflags->cpr1_size = AFL_REG_32;
14076 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14077 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14078 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14079 abiflags->cpr1_size = AFL_REG_64;
14080
14081 abiflags->cpr2_size = AFL_REG_NONE;
14082
14083 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14084 abiflags->ases |= AFL_ASE_MDMX;
14085 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14086 abiflags->ases |= AFL_ASE_MIPS16;
14087 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14088 abiflags->ases |= AFL_ASE_MICROMIPS;
14089
14090 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14091 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14092 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14093 && abiflags->isa_level >= 32
14094 && abiflags->isa_ext != AFL_EXT_LOONGSON_3A)
14095 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14096 }
14097
14098 /* We need to use a special link routine to handle the .reginfo and
14099 the .mdebug sections. We need to merge all instances of these
14100 sections together, not write them all out sequentially. */
14101
14102 bfd_boolean
_bfd_mips_elf_final_link(bfd * abfd,struct bfd_link_info * info)14103 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
14104 {
14105 asection *o;
14106 struct bfd_link_order *p;
14107 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
14108 asection *rtproc_sec, *abiflags_sec;
14109 Elf32_RegInfo reginfo;
14110 struct ecoff_debug_info debug;
14111 struct mips_htab_traverse_info hti;
14112 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14113 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
14114 HDRR *symhdr = &debug.symbolic_header;
14115 void *mdebug_handle = NULL;
14116 asection *s;
14117 EXTR esym;
14118 unsigned int i;
14119 bfd_size_type amt;
14120 struct mips_elf_link_hash_table *htab;
14121
14122 static const char * const secname[] =
14123 {
14124 ".text", ".init", ".fini", ".data",
14125 ".rodata", ".sdata", ".sbss", ".bss"
14126 };
14127 static const int sc[] =
14128 {
14129 scText, scInit, scFini, scData,
14130 scRData, scSData, scSBss, scBss
14131 };
14132
14133 /* Sort the dynamic symbols so that those with GOT entries come after
14134 those without. */
14135 htab = mips_elf_hash_table (info);
14136 BFD_ASSERT (htab != NULL);
14137
14138 if (!mips_elf_sort_hash_table (abfd, info))
14139 return FALSE;
14140
14141 /* Create any scheduled LA25 stubs. */
14142 hti.info = info;
14143 hti.output_bfd = abfd;
14144 hti.error = FALSE;
14145 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14146 if (hti.error)
14147 return FALSE;
14148
14149 /* Get a value for the GP register. */
14150 if (elf_gp (abfd) == 0)
14151 {
14152 struct bfd_link_hash_entry *h;
14153
14154 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
14155 if (h != NULL && h->type == bfd_link_hash_defined)
14156 elf_gp (abfd) = (h->u.def.value
14157 + h->u.def.section->output_section->vma
14158 + h->u.def.section->output_offset);
14159 else if (htab->is_vxworks
14160 && (h = bfd_link_hash_lookup (info->hash,
14161 "_GLOBAL_OFFSET_TABLE_",
14162 FALSE, FALSE, TRUE))
14163 && h->type == bfd_link_hash_defined)
14164 elf_gp (abfd) = (h->u.def.section->output_section->vma
14165 + h->u.def.section->output_offset
14166 + h->u.def.value);
14167 else if (info->relocatable)
14168 {
14169 bfd_vma lo = MINUS_ONE;
14170
14171 /* Find the GP-relative section with the lowest offset. */
14172 for (o = abfd->sections; o != NULL; o = o->next)
14173 if (o->vma < lo
14174 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14175 lo = o->vma;
14176
14177 /* And calculate GP relative to that. */
14178 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
14179 }
14180 else
14181 {
14182 /* If the relocate_section function needs to do a reloc
14183 involving the GP value, it should make a reloc_dangerous
14184 callback to warn that GP is not defined. */
14185 }
14186 }
14187
14188 /* Go through the sections and collect the .reginfo and .mdebug
14189 information. */
14190 abiflags_sec = NULL;
14191 reginfo_sec = NULL;
14192 mdebug_sec = NULL;
14193 gptab_data_sec = NULL;
14194 gptab_bss_sec = NULL;
14195 for (o = abfd->sections; o != NULL; o = o->next)
14196 {
14197 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14198 {
14199 /* We have found the .MIPS.abiflags section in the output file.
14200 Look through all the link_orders comprising it and remove them.
14201 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14202 for (p = o->map_head.link_order; p != NULL; p = p->next)
14203 {
14204 asection *input_section;
14205
14206 if (p->type != bfd_indirect_link_order)
14207 {
14208 if (p->type == bfd_data_link_order)
14209 continue;
14210 abort ();
14211 }
14212
14213 input_section = p->u.indirect.section;
14214
14215 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14216 elf_link_input_bfd ignores this section. */
14217 input_section->flags &= ~SEC_HAS_CONTENTS;
14218 }
14219
14220 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14221 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14222
14223 /* Skip this section later on (I don't think this currently
14224 matters, but someday it might). */
14225 o->map_head.link_order = NULL;
14226
14227 abiflags_sec = o;
14228 }
14229
14230 if (strcmp (o->name, ".reginfo") == 0)
14231 {
14232 memset (®info, 0, sizeof reginfo);
14233
14234 /* We have found the .reginfo section in the output file.
14235 Look through all the link_orders comprising it and merge
14236 the information together. */
14237 for (p = o->map_head.link_order; p != NULL; p = p->next)
14238 {
14239 asection *input_section;
14240 bfd *input_bfd;
14241 Elf32_External_RegInfo ext;
14242 Elf32_RegInfo sub;
14243
14244 if (p->type != bfd_indirect_link_order)
14245 {
14246 if (p->type == bfd_data_link_order)
14247 continue;
14248 abort ();
14249 }
14250
14251 input_section = p->u.indirect.section;
14252 input_bfd = input_section->owner;
14253
14254 if (! bfd_get_section_contents (input_bfd, input_section,
14255 &ext, 0, sizeof ext))
14256 return FALSE;
14257
14258 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14259
14260 reginfo.ri_gprmask |= sub.ri_gprmask;
14261 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14262 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14263 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14264 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14265
14266 /* ri_gp_value is set by the function
14267 mips_elf32_section_processing when the section is
14268 finally written out. */
14269
14270 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14271 elf_link_input_bfd ignores this section. */
14272 input_section->flags &= ~SEC_HAS_CONTENTS;
14273 }
14274
14275 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14276 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
14277
14278 /* Skip this section later on (I don't think this currently
14279 matters, but someday it might). */
14280 o->map_head.link_order = NULL;
14281
14282 reginfo_sec = o;
14283 }
14284
14285 if (strcmp (o->name, ".mdebug") == 0)
14286 {
14287 struct extsym_info einfo;
14288 bfd_vma last;
14289
14290 /* We have found the .mdebug section in the output file.
14291 Look through all the link_orders comprising it and merge
14292 the information together. */
14293 symhdr->magic = swap->sym_magic;
14294 /* FIXME: What should the version stamp be? */
14295 symhdr->vstamp = 0;
14296 symhdr->ilineMax = 0;
14297 symhdr->cbLine = 0;
14298 symhdr->idnMax = 0;
14299 symhdr->ipdMax = 0;
14300 symhdr->isymMax = 0;
14301 symhdr->ioptMax = 0;
14302 symhdr->iauxMax = 0;
14303 symhdr->issMax = 0;
14304 symhdr->issExtMax = 0;
14305 symhdr->ifdMax = 0;
14306 symhdr->crfd = 0;
14307 symhdr->iextMax = 0;
14308
14309 /* We accumulate the debugging information itself in the
14310 debug_info structure. */
14311 debug.line = NULL;
14312 debug.external_dnr = NULL;
14313 debug.external_pdr = NULL;
14314 debug.external_sym = NULL;
14315 debug.external_opt = NULL;
14316 debug.external_aux = NULL;
14317 debug.ss = NULL;
14318 debug.ssext = debug.ssext_end = NULL;
14319 debug.external_fdr = NULL;
14320 debug.external_rfd = NULL;
14321 debug.external_ext = debug.external_ext_end = NULL;
14322
14323 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
14324 if (mdebug_handle == NULL)
14325 return FALSE;
14326
14327 esym.jmptbl = 0;
14328 esym.cobol_main = 0;
14329 esym.weakext = 0;
14330 esym.reserved = 0;
14331 esym.ifd = ifdNil;
14332 esym.asym.iss = issNil;
14333 esym.asym.st = stLocal;
14334 esym.asym.reserved = 0;
14335 esym.asym.index = indexNil;
14336 last = 0;
14337 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
14338 {
14339 esym.asym.sc = sc[i];
14340 s = bfd_get_section_by_name (abfd, secname[i]);
14341 if (s != NULL)
14342 {
14343 esym.asym.value = s->vma;
14344 last = s->vma + s->size;
14345 }
14346 else
14347 esym.asym.value = last;
14348 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
14349 secname[i], &esym))
14350 return FALSE;
14351 }
14352
14353 for (p = o->map_head.link_order; p != NULL; p = p->next)
14354 {
14355 asection *input_section;
14356 bfd *input_bfd;
14357 const struct ecoff_debug_swap *input_swap;
14358 struct ecoff_debug_info input_debug;
14359 char *eraw_src;
14360 char *eraw_end;
14361
14362 if (p->type != bfd_indirect_link_order)
14363 {
14364 if (p->type == bfd_data_link_order)
14365 continue;
14366 abort ();
14367 }
14368
14369 input_section = p->u.indirect.section;
14370 input_bfd = input_section->owner;
14371
14372 if (!is_mips_elf (input_bfd))
14373 {
14374 /* I don't know what a non MIPS ELF bfd would be
14375 doing with a .mdebug section, but I don't really
14376 want to deal with it. */
14377 continue;
14378 }
14379
14380 input_swap = (get_elf_backend_data (input_bfd)
14381 ->elf_backend_ecoff_debug_swap);
14382
14383 BFD_ASSERT (p->size == input_section->size);
14384
14385 /* The ECOFF linking code expects that we have already
14386 read in the debugging information and set up an
14387 ecoff_debug_info structure, so we do that now. */
14388 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
14389 &input_debug))
14390 return FALSE;
14391
14392 if (! (bfd_ecoff_debug_accumulate
14393 (mdebug_handle, abfd, &debug, swap, input_bfd,
14394 &input_debug, input_swap, info)))
14395 return FALSE;
14396
14397 /* Loop through the external symbols. For each one with
14398 interesting information, try to find the symbol in
14399 the linker global hash table and save the information
14400 for the output external symbols. */
14401 eraw_src = input_debug.external_ext;
14402 eraw_end = (eraw_src
14403 + (input_debug.symbolic_header.iextMax
14404 * input_swap->external_ext_size));
14405 for (;
14406 eraw_src < eraw_end;
14407 eraw_src += input_swap->external_ext_size)
14408 {
14409 EXTR ext;
14410 const char *name;
14411 struct mips_elf_link_hash_entry *h;
14412
14413 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
14414 if (ext.asym.sc == scNil
14415 || ext.asym.sc == scUndefined
14416 || ext.asym.sc == scSUndefined)
14417 continue;
14418
14419 name = input_debug.ssext + ext.asym.iss;
14420 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
14421 name, FALSE, FALSE, TRUE);
14422 if (h == NULL || h->esym.ifd != -2)
14423 continue;
14424
14425 if (ext.ifd != -1)
14426 {
14427 BFD_ASSERT (ext.ifd
14428 < input_debug.symbolic_header.ifdMax);
14429 ext.ifd = input_debug.ifdmap[ext.ifd];
14430 }
14431
14432 h->esym = ext;
14433 }
14434
14435 /* Free up the information we just read. */
14436 free (input_debug.line);
14437 free (input_debug.external_dnr);
14438 free (input_debug.external_pdr);
14439 free (input_debug.external_sym);
14440 free (input_debug.external_opt);
14441 free (input_debug.external_aux);
14442 free (input_debug.ss);
14443 free (input_debug.ssext);
14444 free (input_debug.external_fdr);
14445 free (input_debug.external_rfd);
14446 free (input_debug.external_ext);
14447
14448 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14449 elf_link_input_bfd ignores this section. */
14450 input_section->flags &= ~SEC_HAS_CONTENTS;
14451 }
14452
14453 if (SGI_COMPAT (abfd) && info->shared)
14454 {
14455 /* Create .rtproc section. */
14456 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
14457 if (rtproc_sec == NULL)
14458 {
14459 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
14460 | SEC_LINKER_CREATED | SEC_READONLY);
14461
14462 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
14463 ".rtproc",
14464 flags);
14465 if (rtproc_sec == NULL
14466 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
14467 return FALSE;
14468 }
14469
14470 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
14471 info, rtproc_sec,
14472 &debug))
14473 return FALSE;
14474 }
14475
14476 /* Build the external symbol information. */
14477 einfo.abfd = abfd;
14478 einfo.info = info;
14479 einfo.debug = &debug;
14480 einfo.swap = swap;
14481 einfo.failed = FALSE;
14482 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
14483 mips_elf_output_extsym, &einfo);
14484 if (einfo.failed)
14485 return FALSE;
14486
14487 /* Set the size of the .mdebug section. */
14488 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
14489
14490 /* Skip this section later on (I don't think this currently
14491 matters, but someday it might). */
14492 o->map_head.link_order = NULL;
14493
14494 mdebug_sec = o;
14495 }
14496
14497 if (CONST_STRNEQ (o->name, ".gptab."))
14498 {
14499 const char *subname;
14500 unsigned int c;
14501 Elf32_gptab *tab;
14502 Elf32_External_gptab *ext_tab;
14503 unsigned int j;
14504
14505 /* The .gptab.sdata and .gptab.sbss sections hold
14506 information describing how the small data area would
14507 change depending upon the -G switch. These sections
14508 not used in executables files. */
14509 if (! info->relocatable)
14510 {
14511 for (p = o->map_head.link_order; p != NULL; p = p->next)
14512 {
14513 asection *input_section;
14514
14515 if (p->type != bfd_indirect_link_order)
14516 {
14517 if (p->type == bfd_data_link_order)
14518 continue;
14519 abort ();
14520 }
14521
14522 input_section = p->u.indirect.section;
14523
14524 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14525 elf_link_input_bfd ignores this section. */
14526 input_section->flags &= ~SEC_HAS_CONTENTS;
14527 }
14528
14529 /* Skip this section later on (I don't think this
14530 currently matters, but someday it might). */
14531 o->map_head.link_order = NULL;
14532
14533 /* Really remove the section. */
14534 bfd_section_list_remove (abfd, o);
14535 --abfd->section_count;
14536
14537 continue;
14538 }
14539
14540 /* There is one gptab for initialized data, and one for
14541 uninitialized data. */
14542 if (strcmp (o->name, ".gptab.sdata") == 0)
14543 gptab_data_sec = o;
14544 else if (strcmp (o->name, ".gptab.sbss") == 0)
14545 gptab_bss_sec = o;
14546 else
14547 {
14548 (*_bfd_error_handler)
14549 (_("%s: illegal section name `%s'"),
14550 bfd_get_filename (abfd), o->name);
14551 bfd_set_error (bfd_error_nonrepresentable_section);
14552 return FALSE;
14553 }
14554
14555 /* The linker script always combines .gptab.data and
14556 .gptab.sdata into .gptab.sdata, and likewise for
14557 .gptab.bss and .gptab.sbss. It is possible that there is
14558 no .sdata or .sbss section in the output file, in which
14559 case we must change the name of the output section. */
14560 subname = o->name + sizeof ".gptab" - 1;
14561 if (bfd_get_section_by_name (abfd, subname) == NULL)
14562 {
14563 if (o == gptab_data_sec)
14564 o->name = ".gptab.data";
14565 else
14566 o->name = ".gptab.bss";
14567 subname = o->name + sizeof ".gptab" - 1;
14568 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
14569 }
14570
14571 /* Set up the first entry. */
14572 c = 1;
14573 amt = c * sizeof (Elf32_gptab);
14574 tab = bfd_malloc (amt);
14575 if (tab == NULL)
14576 return FALSE;
14577 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
14578 tab[0].gt_header.gt_unused = 0;
14579
14580 /* Combine the input sections. */
14581 for (p = o->map_head.link_order; p != NULL; p = p->next)
14582 {
14583 asection *input_section;
14584 bfd *input_bfd;
14585 bfd_size_type size;
14586 unsigned long last;
14587 bfd_size_type gpentry;
14588
14589 if (p->type != bfd_indirect_link_order)
14590 {
14591 if (p->type == bfd_data_link_order)
14592 continue;
14593 abort ();
14594 }
14595
14596 input_section = p->u.indirect.section;
14597 input_bfd = input_section->owner;
14598
14599 /* Combine the gptab entries for this input section one
14600 by one. We know that the input gptab entries are
14601 sorted by ascending -G value. */
14602 size = input_section->size;
14603 last = 0;
14604 for (gpentry = sizeof (Elf32_External_gptab);
14605 gpentry < size;
14606 gpentry += sizeof (Elf32_External_gptab))
14607 {
14608 Elf32_External_gptab ext_gptab;
14609 Elf32_gptab int_gptab;
14610 unsigned long val;
14611 unsigned long add;
14612 bfd_boolean exact;
14613 unsigned int look;
14614
14615 if (! (bfd_get_section_contents
14616 (input_bfd, input_section, &ext_gptab, gpentry,
14617 sizeof (Elf32_External_gptab))))
14618 {
14619 free (tab);
14620 return FALSE;
14621 }
14622
14623 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
14624 &int_gptab);
14625 val = int_gptab.gt_entry.gt_g_value;
14626 add = int_gptab.gt_entry.gt_bytes - last;
14627
14628 exact = FALSE;
14629 for (look = 1; look < c; look++)
14630 {
14631 if (tab[look].gt_entry.gt_g_value >= val)
14632 tab[look].gt_entry.gt_bytes += add;
14633
14634 if (tab[look].gt_entry.gt_g_value == val)
14635 exact = TRUE;
14636 }
14637
14638 if (! exact)
14639 {
14640 Elf32_gptab *new_tab;
14641 unsigned int max;
14642
14643 /* We need a new table entry. */
14644 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
14645 new_tab = bfd_realloc (tab, amt);
14646 if (new_tab == NULL)
14647 {
14648 free (tab);
14649 return FALSE;
14650 }
14651 tab = new_tab;
14652 tab[c].gt_entry.gt_g_value = val;
14653 tab[c].gt_entry.gt_bytes = add;
14654
14655 /* Merge in the size for the next smallest -G
14656 value, since that will be implied by this new
14657 value. */
14658 max = 0;
14659 for (look = 1; look < c; look++)
14660 {
14661 if (tab[look].gt_entry.gt_g_value < val
14662 && (max == 0
14663 || (tab[look].gt_entry.gt_g_value
14664 > tab[max].gt_entry.gt_g_value)))
14665 max = look;
14666 }
14667 if (max != 0)
14668 tab[c].gt_entry.gt_bytes +=
14669 tab[max].gt_entry.gt_bytes;
14670
14671 ++c;
14672 }
14673
14674 last = int_gptab.gt_entry.gt_bytes;
14675 }
14676
14677 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14678 elf_link_input_bfd ignores this section. */
14679 input_section->flags &= ~SEC_HAS_CONTENTS;
14680 }
14681
14682 /* The table must be sorted by -G value. */
14683 if (c > 2)
14684 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
14685
14686 /* Swap out the table. */
14687 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
14688 ext_tab = bfd_alloc (abfd, amt);
14689 if (ext_tab == NULL)
14690 {
14691 free (tab);
14692 return FALSE;
14693 }
14694
14695 for (j = 0; j < c; j++)
14696 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
14697 free (tab);
14698
14699 o->size = c * sizeof (Elf32_External_gptab);
14700 o->contents = (bfd_byte *) ext_tab;
14701
14702 /* Skip this section later on (I don't think this currently
14703 matters, but someday it might). */
14704 o->map_head.link_order = NULL;
14705 }
14706 }
14707
14708 /* Invoke the regular ELF backend linker to do all the work. */
14709 if (!bfd_elf_final_link (abfd, info))
14710 return FALSE;
14711
14712 /* Now write out the computed sections. */
14713
14714 if (abiflags_sec != NULL)
14715 {
14716 Elf_External_ABIFlags_v0 ext;
14717 Elf_Internal_ABIFlags_v0 *abiflags;
14718
14719 abiflags = &mips_elf_tdata (abfd)->abiflags;
14720
14721 /* Set up the abiflags if no valid input sections were found. */
14722 if (!mips_elf_tdata (abfd)->abiflags_valid)
14723 {
14724 infer_mips_abiflags (abfd, abiflags);
14725 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
14726 }
14727 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
14728 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
14729 return FALSE;
14730 }
14731
14732 if (reginfo_sec != NULL)
14733 {
14734 Elf32_External_RegInfo ext;
14735
14736 bfd_mips_elf32_swap_reginfo_out (abfd, ®info, &ext);
14737 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
14738 return FALSE;
14739 }
14740
14741 if (mdebug_sec != NULL)
14742 {
14743 BFD_ASSERT (abfd->output_has_begun);
14744 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
14745 swap, info,
14746 mdebug_sec->filepos))
14747 return FALSE;
14748
14749 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
14750 }
14751
14752 if (gptab_data_sec != NULL)
14753 {
14754 if (! bfd_set_section_contents (abfd, gptab_data_sec,
14755 gptab_data_sec->contents,
14756 0, gptab_data_sec->size))
14757 return FALSE;
14758 }
14759
14760 if (gptab_bss_sec != NULL)
14761 {
14762 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
14763 gptab_bss_sec->contents,
14764 0, gptab_bss_sec->size))
14765 return FALSE;
14766 }
14767
14768 if (SGI_COMPAT (abfd))
14769 {
14770 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
14771 if (rtproc_sec != NULL)
14772 {
14773 if (! bfd_set_section_contents (abfd, rtproc_sec,
14774 rtproc_sec->contents,
14775 0, rtproc_sec->size))
14776 return FALSE;
14777 }
14778 }
14779
14780 return TRUE;
14781 }
14782
14783 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14784
14785 struct mips_mach_extension
14786 {
14787 unsigned long extension, base;
14788 };
14789
14790
14791 /* An array describing how BFD machines relate to one another. The entries
14792 are ordered topologically with MIPS I extensions listed last. */
14793
14794 static const struct mips_mach_extension mips_mach_extensions[] =
14795 {
14796 /* MIPS64r2 extensions. */
14797 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14798 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14799 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14800 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14801 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64r2 },
14802
14803 /* MIPS64 extensions. */
14804 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14805 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14806 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14807
14808 /* MIPS V extensions. */
14809 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14810
14811 /* R10000 extensions. */
14812 { bfd_mach_mips12000, bfd_mach_mips10000 },
14813 { bfd_mach_mips14000, bfd_mach_mips10000 },
14814 { bfd_mach_mips16000, bfd_mach_mips10000 },
14815
14816 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14817 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14818 better to allow vr5400 and vr5500 code to be merged anyway, since
14819 many libraries will just use the core ISA. Perhaps we could add
14820 some sort of ASE flag if this ever proves a problem. */
14821 { bfd_mach_mips5500, bfd_mach_mips5400 },
14822 { bfd_mach_mips5400, bfd_mach_mips5000 },
14823
14824 /* MIPS IV extensions. */
14825 { bfd_mach_mips5, bfd_mach_mips8000 },
14826 { bfd_mach_mips10000, bfd_mach_mips8000 },
14827 { bfd_mach_mips5000, bfd_mach_mips8000 },
14828 { bfd_mach_mips7000, bfd_mach_mips8000 },
14829 { bfd_mach_mips9000, bfd_mach_mips8000 },
14830
14831 /* VR4100 extensions. */
14832 { bfd_mach_mips4120, bfd_mach_mips4100 },
14833 { bfd_mach_mips4111, bfd_mach_mips4100 },
14834
14835 /* MIPS III extensions. */
14836 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14837 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14838 { bfd_mach_mips8000, bfd_mach_mips4000 },
14839 { bfd_mach_mips4650, bfd_mach_mips4000 },
14840 { bfd_mach_mips4600, bfd_mach_mips4000 },
14841 { bfd_mach_mips4400, bfd_mach_mips4000 },
14842 { bfd_mach_mips4300, bfd_mach_mips4000 },
14843 { bfd_mach_mips4100, bfd_mach_mips4000 },
14844 { bfd_mach_mips4010, bfd_mach_mips4000 },
14845 { bfd_mach_mips5900, bfd_mach_mips4000 },
14846
14847 /* MIPS32 extensions. */
14848 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14849
14850 /* MIPS II extensions. */
14851 { bfd_mach_mips4000, bfd_mach_mips6000 },
14852 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14853
14854 /* MIPS I extensions. */
14855 { bfd_mach_mips6000, bfd_mach_mips3000 },
14856 { bfd_mach_mips3900, bfd_mach_mips3000 }
14857 };
14858
14859
14860 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14861
14862 static bfd_boolean
mips_mach_extends_p(unsigned long base,unsigned long extension)14863 mips_mach_extends_p (unsigned long base, unsigned long extension)
14864 {
14865 size_t i;
14866
14867 if (extension == base)
14868 return TRUE;
14869
14870 if (base == bfd_mach_mipsisa32
14871 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14872 return TRUE;
14873
14874 if (base == bfd_mach_mipsisa32r2
14875 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14876 return TRUE;
14877
14878 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14879 if (extension == mips_mach_extensions[i].extension)
14880 {
14881 extension = mips_mach_extensions[i].base;
14882 if (extension == base)
14883 return TRUE;
14884 }
14885
14886 return FALSE;
14887 }
14888
14889
14890 /* Merge object attributes from IBFD into OBFD. Raise an error if
14891 there are conflicting attributes. */
14892 static bfd_boolean
mips_elf_merge_obj_attributes(bfd * ibfd,bfd * obfd)14893 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
14894 {
14895 obj_attribute *in_attr;
14896 obj_attribute *out_attr;
14897 bfd *abi_fp_bfd;
14898 bfd *abi_msa_bfd;
14899
14900 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
14901 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
14902 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
14903 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
14904
14905 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
14906 if (!abi_msa_bfd
14907 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14908 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
14909
14910 if (!elf_known_obj_attributes_proc (obfd)[0].i)
14911 {
14912 /* This is the first object. Copy the attributes. */
14913 _bfd_elf_copy_obj_attributes (ibfd, obfd);
14914
14915 /* Use the Tag_null value to indicate the attributes have been
14916 initialized. */
14917 elf_known_obj_attributes_proc (obfd)[0].i = 1;
14918
14919 return TRUE;
14920 }
14921
14922 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
14923 non-conflicting ones. */
14924 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
14925 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
14926 {
14927 int out_fp, in_fp;
14928
14929 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
14930 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14931 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
14932 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
14933 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
14934 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
14935 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
14936 || in_fp == Val_GNU_MIPS_ABI_FP_64
14937 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
14938 {
14939 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
14940 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14941 }
14942 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
14943 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
14944 || out_fp == Val_GNU_MIPS_ABI_FP_64
14945 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
14946 /* Keep the current setting. */;
14947 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
14948 && in_fp == Val_GNU_MIPS_ABI_FP_64)
14949 {
14950 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
14951 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14952 }
14953 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
14954 && out_fp == Val_GNU_MIPS_ABI_FP_64)
14955 /* Keep the current setting. */;
14956 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
14957 {
14958 const char *out_string, *in_string;
14959
14960 out_string = _bfd_mips_fp_abi_string (out_fp);
14961 in_string = _bfd_mips_fp_abi_string (in_fp);
14962 /* First warn about cases involving unrecognised ABIs. */
14963 if (!out_string && !in_string)
14964 _bfd_error_handler
14965 (_("Warning: %B uses unknown floating point ABI %d "
14966 "(set by %B), %B uses unknown floating point ABI %d"),
14967 obfd, abi_fp_bfd, ibfd, out_fp, in_fp);
14968 else if (!out_string)
14969 _bfd_error_handler
14970 (_("Warning: %B uses unknown floating point ABI %d "
14971 "(set by %B), %B uses %s"),
14972 obfd, abi_fp_bfd, ibfd, out_fp, in_string);
14973 else if (!in_string)
14974 _bfd_error_handler
14975 (_("Warning: %B uses %s (set by %B), "
14976 "%B uses unknown floating point ABI %d"),
14977 obfd, abi_fp_bfd, ibfd, out_string, in_fp);
14978 else
14979 {
14980 /* If one of the bfds is soft-float, the other must be
14981 hard-float. The exact choice of hard-float ABI isn't
14982 really relevant to the error message. */
14983 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
14984 out_string = "-mhard-float";
14985 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
14986 in_string = "-mhard-float";
14987 _bfd_error_handler
14988 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14989 obfd, abi_fp_bfd, ibfd, out_string, in_string);
14990 }
14991 }
14992 }
14993
14994 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
14995 non-conflicting ones. */
14996 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14997 {
14998 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
14999 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15000 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15001 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15002 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15003 {
15004 case Val_GNU_MIPS_ABI_MSA_128:
15005 _bfd_error_handler
15006 (_("Warning: %B uses %s (set by %B), "
15007 "%B uses unknown MSA ABI %d"),
15008 obfd, abi_msa_bfd, ibfd,
15009 "-mmsa", in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15010 break;
15011
15012 default:
15013 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15014 {
15015 case Val_GNU_MIPS_ABI_MSA_128:
15016 _bfd_error_handler
15017 (_("Warning: %B uses unknown MSA ABI %d "
15018 "(set by %B), %B uses %s"),
15019 obfd, abi_msa_bfd, ibfd,
15020 out_attr[Tag_GNU_MIPS_ABI_MSA].i, "-mmsa");
15021 break;
15022
15023 default:
15024 _bfd_error_handler
15025 (_("Warning: %B uses unknown MSA ABI %d "
15026 "(set by %B), %B uses unknown MSA ABI %d"),
15027 obfd, abi_msa_bfd, ibfd,
15028 out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15029 in_attr[Tag_GNU_MIPS_ABI_MSA].i);
15030 break;
15031 }
15032 }
15033 }
15034
15035 /* Merge Tag_compatibility attributes and any common GNU ones. */
15036 _bfd_elf_merge_object_attributes (ibfd, obfd);
15037
15038 return TRUE;
15039 }
15040
15041 /* Merge backend specific data from an object file to the output
15042 object file when linking. */
15043
15044 bfd_boolean
_bfd_mips_elf_merge_private_bfd_data(bfd * ibfd,bfd * obfd)15045 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
15046 {
15047 flagword old_flags;
15048 flagword new_flags;
15049 bfd_boolean ok;
15050 bfd_boolean null_input_bfd = TRUE;
15051 asection *sec;
15052 obj_attribute *out_attr;
15053
15054 /* Check if we have the same endianness. */
15055 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
15056 {
15057 (*_bfd_error_handler)
15058 (_("%B: endianness incompatible with that of the selected emulation"),
15059 ibfd);
15060 return FALSE;
15061 }
15062
15063 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
15064 return TRUE;
15065
15066 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15067 {
15068 (*_bfd_error_handler)
15069 (_("%B: ABI is incompatible with that of the selected emulation"),
15070 ibfd);
15071 return FALSE;
15072 }
15073
15074 /* Set up the FP ABI attribute from the abiflags if it is not already
15075 set. */
15076 if (mips_elf_tdata (ibfd)->abiflags_valid)
15077 {
15078 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
15079 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
15080 in_attr[Tag_GNU_MIPS_ABI_FP].i =
15081 mips_elf_tdata (ibfd)->abiflags.fp_abi;
15082 }
15083
15084 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
15085 return FALSE;
15086
15087 /* Check to see if the input BFD actually contains any sections.
15088 If not, its flags may not have been initialised either, but it cannot
15089 actually cause any incompatibility. */
15090 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15091 {
15092 /* Ignore synthetic sections and empty .text, .data and .bss sections
15093 which are automatically generated by gas. Also ignore fake
15094 (s)common sections, since merely defining a common symbol does
15095 not affect compatibility. */
15096 if ((sec->flags & SEC_IS_COMMON) == 0
15097 && strcmp (sec->name, ".reginfo")
15098 && strcmp (sec->name, ".mdebug")
15099 && (sec->size != 0
15100 || (strcmp (sec->name, ".text")
15101 && strcmp (sec->name, ".data")
15102 && strcmp (sec->name, ".bss"))))
15103 {
15104 null_input_bfd = FALSE;
15105 break;
15106 }
15107 }
15108 if (null_input_bfd)
15109 return TRUE;
15110
15111 /* Populate abiflags using existing information. */
15112 if (!mips_elf_tdata (ibfd)->abiflags_valid)
15113 {
15114 infer_mips_abiflags (ibfd, &mips_elf_tdata (ibfd)->abiflags);
15115 mips_elf_tdata (ibfd)->abiflags_valid = TRUE;
15116 }
15117 else
15118 {
15119 Elf_Internal_ABIFlags_v0 abiflags;
15120 Elf_Internal_ABIFlags_v0 in_abiflags;
15121 infer_mips_abiflags (ibfd, &abiflags);
15122 in_abiflags = mips_elf_tdata (ibfd)->abiflags;
15123
15124 /* It is not possible to infer the correct ISA revision
15125 for R3 or R5 so drop down to R2 for the checks. */
15126 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15127 in_abiflags.isa_rev = 2;
15128
15129 if (in_abiflags.isa_level != abiflags.isa_level
15130 || in_abiflags.isa_rev != abiflags.isa_rev
15131 || in_abiflags.isa_ext != abiflags.isa_ext)
15132 (*_bfd_error_handler)
15133 (_("%B: warning: Inconsistent ISA between e_flags and "
15134 ".MIPS.abiflags"), ibfd);
15135 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15136 && in_abiflags.fp_abi != abiflags.fp_abi)
15137 (*_bfd_error_handler)
15138 (_("%B: warning: Inconsistent FP ABI between e_flags and "
15139 ".MIPS.abiflags"), ibfd);
15140 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
15141 (*_bfd_error_handler)
15142 (_("%B: warning: Inconsistent ASEs between e_flags and "
15143 ".MIPS.abiflags"), ibfd);
15144 if (in_abiflags.isa_ext != abiflags.isa_ext)
15145 (*_bfd_error_handler)
15146 (_("%B: warning: Inconsistent ISA extensions between e_flags and "
15147 ".MIPS.abiflags"), ibfd);
15148 if (in_abiflags.flags2 != 0)
15149 (*_bfd_error_handler)
15150 (_("%B: warning: Unexpected flag in the flags2 field of "
15151 ".MIPS.abiflags (0x%lx)"), ibfd,
15152 (unsigned long) in_abiflags.flags2);
15153 }
15154
15155 if (!mips_elf_tdata (obfd)->abiflags_valid)
15156 {
15157 /* Copy input abiflags if output abiflags are not already valid. */
15158 mips_elf_tdata (obfd)->abiflags = mips_elf_tdata (ibfd)->abiflags;
15159 mips_elf_tdata (obfd)->abiflags_valid = TRUE;
15160 }
15161
15162 if (! elf_flags_init (obfd))
15163 {
15164 elf_flags_init (obfd) = TRUE;
15165 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
15166 elf_elfheader (obfd)->e_ident[EI_CLASS]
15167 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
15168
15169 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
15170 && (bfd_get_arch_info (obfd)->the_default
15171 || mips_mach_extends_p (bfd_get_mach (obfd),
15172 bfd_get_mach (ibfd))))
15173 {
15174 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
15175 bfd_get_mach (ibfd)))
15176 return FALSE;
15177
15178 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
15179 update_mips_abiflags_isa (obfd, &mips_elf_tdata (obfd)->abiflags);
15180 }
15181
15182 return TRUE;
15183 }
15184
15185 /* Update the output abiflags fp_abi using the computed fp_abi. */
15186 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15187 mips_elf_tdata (obfd)->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15188
15189 #define max(a,b) ((a) > (b) ? (a) : (b))
15190 /* Merge abiflags. */
15191 mips_elf_tdata (obfd)->abiflags.isa_rev
15192 = max (mips_elf_tdata (obfd)->abiflags.isa_rev,
15193 mips_elf_tdata (ibfd)->abiflags.isa_rev);
15194 mips_elf_tdata (obfd)->abiflags.gpr_size
15195 = max (mips_elf_tdata (obfd)->abiflags.gpr_size,
15196 mips_elf_tdata (ibfd)->abiflags.gpr_size);
15197 mips_elf_tdata (obfd)->abiflags.cpr1_size
15198 = max (mips_elf_tdata (obfd)->abiflags.cpr1_size,
15199 mips_elf_tdata (ibfd)->abiflags.cpr1_size);
15200 mips_elf_tdata (obfd)->abiflags.cpr2_size
15201 = max (mips_elf_tdata (obfd)->abiflags.cpr2_size,
15202 mips_elf_tdata (ibfd)->abiflags.cpr2_size);
15203 #undef max
15204 mips_elf_tdata (obfd)->abiflags.ases
15205 |= mips_elf_tdata (ibfd)->abiflags.ases;
15206 mips_elf_tdata (obfd)->abiflags.flags1
15207 |= mips_elf_tdata (ibfd)->abiflags.flags1;
15208
15209 new_flags = elf_elfheader (ibfd)->e_flags;
15210 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15211 old_flags = elf_elfheader (obfd)->e_flags;
15212
15213 /* Check flag compatibility. */
15214
15215 new_flags &= ~EF_MIPS_NOREORDER;
15216 old_flags &= ~EF_MIPS_NOREORDER;
15217
15218 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15219 doesn't seem to matter. */
15220 new_flags &= ~EF_MIPS_XGOT;
15221 old_flags &= ~EF_MIPS_XGOT;
15222
15223 /* MIPSpro generates ucode info in n64 objects. Again, we should
15224 just be able to ignore this. */
15225 new_flags &= ~EF_MIPS_UCODE;
15226 old_flags &= ~EF_MIPS_UCODE;
15227
15228 /* DSOs should only be linked with CPIC code. */
15229 if ((ibfd->flags & DYNAMIC) != 0)
15230 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15231
15232 if (new_flags == old_flags)
15233 return TRUE;
15234
15235 ok = TRUE;
15236
15237 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15238 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15239 {
15240 (*_bfd_error_handler)
15241 (_("%B: warning: linking abicalls files with non-abicalls files"),
15242 ibfd);
15243 ok = TRUE;
15244 }
15245
15246 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15247 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15248 if (! (new_flags & EF_MIPS_PIC))
15249 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15250
15251 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15252 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15253
15254 /* Compare the ISAs. */
15255 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15256 {
15257 (*_bfd_error_handler)
15258 (_("%B: linking 32-bit code with 64-bit code"),
15259 ibfd);
15260 ok = FALSE;
15261 }
15262 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15263 {
15264 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15265 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15266 {
15267 /* Copy the architecture info from IBFD to OBFD. Also copy
15268 the 32-bit flag (if set) so that we continue to recognise
15269 OBFD as a 32-bit binary. */
15270 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15271 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15272 elf_elfheader (obfd)->e_flags
15273 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15274
15275 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15276 update_mips_abiflags_isa (obfd, &mips_elf_tdata (obfd)->abiflags);
15277
15278 /* Copy across the ABI flags if OBFD doesn't use them
15279 and if that was what caused us to treat IBFD as 32-bit. */
15280 if ((old_flags & EF_MIPS_ABI) == 0
15281 && mips_32bit_flags_p (new_flags)
15282 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15283 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15284 }
15285 else
15286 {
15287 /* The ISAs aren't compatible. */
15288 (*_bfd_error_handler)
15289 (_("%B: linking %s module with previous %s modules"),
15290 ibfd,
15291 bfd_printable_name (ibfd),
15292 bfd_printable_name (obfd));
15293 ok = FALSE;
15294 }
15295 }
15296
15297 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15298 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15299
15300 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15301 does set EI_CLASS differently from any 32-bit ABI. */
15302 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15303 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15304 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15305 {
15306 /* Only error if both are set (to different values). */
15307 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15308 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15309 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15310 {
15311 (*_bfd_error_handler)
15312 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
15313 ibfd,
15314 elf_mips_abi_name (ibfd),
15315 elf_mips_abi_name (obfd));
15316 ok = FALSE;
15317 }
15318 new_flags &= ~EF_MIPS_ABI;
15319 old_flags &= ~EF_MIPS_ABI;
15320 }
15321
15322 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15323 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15324 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15325 {
15326 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15327 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15328 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15329 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15330 int micro_mis = old_m16 && new_micro;
15331 int m16_mis = old_micro && new_m16;
15332
15333 if (m16_mis || micro_mis)
15334 {
15335 (*_bfd_error_handler)
15336 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
15337 ibfd,
15338 m16_mis ? "MIPS16" : "microMIPS",
15339 m16_mis ? "microMIPS" : "MIPS16");
15340 ok = FALSE;
15341 }
15342
15343 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15344
15345 new_flags &= ~ EF_MIPS_ARCH_ASE;
15346 old_flags &= ~ EF_MIPS_ARCH_ASE;
15347 }
15348
15349 /* Compare NaN encodings. */
15350 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15351 {
15352 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
15353 ibfd,
15354 (new_flags & EF_MIPS_NAN2008
15355 ? "-mnan=2008" : "-mnan=legacy"),
15356 (old_flags & EF_MIPS_NAN2008
15357 ? "-mnan=2008" : "-mnan=legacy"));
15358 ok = FALSE;
15359 new_flags &= ~EF_MIPS_NAN2008;
15360 old_flags &= ~EF_MIPS_NAN2008;
15361 }
15362
15363 /* Compare FP64 state. */
15364 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15365 {
15366 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
15367 ibfd,
15368 (new_flags & EF_MIPS_FP64
15369 ? "-mfp64" : "-mfp32"),
15370 (old_flags & EF_MIPS_FP64
15371 ? "-mfp64" : "-mfp32"));
15372 ok = FALSE;
15373 new_flags &= ~EF_MIPS_FP64;
15374 old_flags &= ~EF_MIPS_FP64;
15375 }
15376
15377 /* Warn about any other mismatches */
15378 if (new_flags != old_flags)
15379 {
15380 (*_bfd_error_handler)
15381 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
15382 ibfd, (unsigned long) new_flags,
15383 (unsigned long) old_flags);
15384 ok = FALSE;
15385 }
15386
15387 if (! ok)
15388 {
15389 bfd_set_error (bfd_error_bad_value);
15390 return FALSE;
15391 }
15392
15393 return TRUE;
15394 }
15395
15396 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
15397
15398 bfd_boolean
_bfd_mips_elf_set_private_flags(bfd * abfd,flagword flags)15399 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
15400 {
15401 BFD_ASSERT (!elf_flags_init (abfd)
15402 || elf_elfheader (abfd)->e_flags == flags);
15403
15404 elf_elfheader (abfd)->e_flags = flags;
15405 elf_flags_init (abfd) = TRUE;
15406 return TRUE;
15407 }
15408
15409 char *
_bfd_mips_elf_get_target_dtag(bfd_vma dtag)15410 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
15411 {
15412 switch (dtag)
15413 {
15414 default: return "";
15415 case DT_MIPS_RLD_VERSION:
15416 return "MIPS_RLD_VERSION";
15417 case DT_MIPS_TIME_STAMP:
15418 return "MIPS_TIME_STAMP";
15419 case DT_MIPS_ICHECKSUM:
15420 return "MIPS_ICHECKSUM";
15421 case DT_MIPS_IVERSION:
15422 return "MIPS_IVERSION";
15423 case DT_MIPS_FLAGS:
15424 return "MIPS_FLAGS";
15425 case DT_MIPS_BASE_ADDRESS:
15426 return "MIPS_BASE_ADDRESS";
15427 case DT_MIPS_MSYM:
15428 return "MIPS_MSYM";
15429 case DT_MIPS_CONFLICT:
15430 return "MIPS_CONFLICT";
15431 case DT_MIPS_LIBLIST:
15432 return "MIPS_LIBLIST";
15433 case DT_MIPS_LOCAL_GOTNO:
15434 return "MIPS_LOCAL_GOTNO";
15435 case DT_MIPS_CONFLICTNO:
15436 return "MIPS_CONFLICTNO";
15437 case DT_MIPS_LIBLISTNO:
15438 return "MIPS_LIBLISTNO";
15439 case DT_MIPS_SYMTABNO:
15440 return "MIPS_SYMTABNO";
15441 case DT_MIPS_UNREFEXTNO:
15442 return "MIPS_UNREFEXTNO";
15443 case DT_MIPS_GOTSYM:
15444 return "MIPS_GOTSYM";
15445 case DT_MIPS_HIPAGENO:
15446 return "MIPS_HIPAGENO";
15447 case DT_MIPS_RLD_MAP:
15448 return "MIPS_RLD_MAP";
15449 case DT_MIPS_RLD_MAP2:
15450 return "MIPS_RLD_MAP2";
15451 case DT_MIPS_DELTA_CLASS:
15452 return "MIPS_DELTA_CLASS";
15453 case DT_MIPS_DELTA_CLASS_NO:
15454 return "MIPS_DELTA_CLASS_NO";
15455 case DT_MIPS_DELTA_INSTANCE:
15456 return "MIPS_DELTA_INSTANCE";
15457 case DT_MIPS_DELTA_INSTANCE_NO:
15458 return "MIPS_DELTA_INSTANCE_NO";
15459 case DT_MIPS_DELTA_RELOC:
15460 return "MIPS_DELTA_RELOC";
15461 case DT_MIPS_DELTA_RELOC_NO:
15462 return "MIPS_DELTA_RELOC_NO";
15463 case DT_MIPS_DELTA_SYM:
15464 return "MIPS_DELTA_SYM";
15465 case DT_MIPS_DELTA_SYM_NO:
15466 return "MIPS_DELTA_SYM_NO";
15467 case DT_MIPS_DELTA_CLASSSYM:
15468 return "MIPS_DELTA_CLASSSYM";
15469 case DT_MIPS_DELTA_CLASSSYM_NO:
15470 return "MIPS_DELTA_CLASSSYM_NO";
15471 case DT_MIPS_CXX_FLAGS:
15472 return "MIPS_CXX_FLAGS";
15473 case DT_MIPS_PIXIE_INIT:
15474 return "MIPS_PIXIE_INIT";
15475 case DT_MIPS_SYMBOL_LIB:
15476 return "MIPS_SYMBOL_LIB";
15477 case DT_MIPS_LOCALPAGE_GOTIDX:
15478 return "MIPS_LOCALPAGE_GOTIDX";
15479 case DT_MIPS_LOCAL_GOTIDX:
15480 return "MIPS_LOCAL_GOTIDX";
15481 case DT_MIPS_HIDDEN_GOTIDX:
15482 return "MIPS_HIDDEN_GOTIDX";
15483 case DT_MIPS_PROTECTED_GOTIDX:
15484 return "MIPS_PROTECTED_GOT_IDX";
15485 case DT_MIPS_OPTIONS:
15486 return "MIPS_OPTIONS";
15487 case DT_MIPS_INTERFACE:
15488 return "MIPS_INTERFACE";
15489 case DT_MIPS_DYNSTR_ALIGN:
15490 return "DT_MIPS_DYNSTR_ALIGN";
15491 case DT_MIPS_INTERFACE_SIZE:
15492 return "DT_MIPS_INTERFACE_SIZE";
15493 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
15494 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
15495 case DT_MIPS_PERF_SUFFIX:
15496 return "DT_MIPS_PERF_SUFFIX";
15497 case DT_MIPS_COMPACT_SIZE:
15498 return "DT_MIPS_COMPACT_SIZE";
15499 case DT_MIPS_GP_VALUE:
15500 return "DT_MIPS_GP_VALUE";
15501 case DT_MIPS_AUX_DYNAMIC:
15502 return "DT_MIPS_AUX_DYNAMIC";
15503 case DT_MIPS_PLTGOT:
15504 return "DT_MIPS_PLTGOT";
15505 case DT_MIPS_RWPLT:
15506 return "DT_MIPS_RWPLT";
15507 }
15508 }
15509
15510 /* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
15511 not known. */
15512
15513 const char *
_bfd_mips_fp_abi_string(int fp)15514 _bfd_mips_fp_abi_string (int fp)
15515 {
15516 switch (fp)
15517 {
15518 /* These strings aren't translated because they're simply
15519 option lists. */
15520 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15521 return "-mdouble-float";
15522
15523 case Val_GNU_MIPS_ABI_FP_SINGLE:
15524 return "-msingle-float";
15525
15526 case Val_GNU_MIPS_ABI_FP_SOFT:
15527 return "-msoft-float";
15528
15529 case Val_GNU_MIPS_ABI_FP_OLD_64:
15530 return _("-mips32r2 -mfp64 (12 callee-saved)");
15531
15532 case Val_GNU_MIPS_ABI_FP_XX:
15533 return "-mfpxx";
15534
15535 case Val_GNU_MIPS_ABI_FP_64:
15536 return "-mgp32 -mfp64";
15537
15538 case Val_GNU_MIPS_ABI_FP_64A:
15539 return "-mgp32 -mfp64 -mno-odd-spreg";
15540
15541 default:
15542 return 0;
15543 }
15544 }
15545
15546 static void
print_mips_ases(FILE * file,unsigned int mask)15547 print_mips_ases (FILE *file, unsigned int mask)
15548 {
15549 if (mask & AFL_ASE_DSP)
15550 fputs ("\n\tDSP ASE", file);
15551 if (mask & AFL_ASE_DSPR2)
15552 fputs ("\n\tDSP R2 ASE", file);
15553 if (mask & AFL_ASE_DSPR6)
15554 fputs ("\n\tDSP R6 ASE", file);
15555 if (mask & AFL_ASE_EVA)
15556 fputs ("\n\tEnhanced VA Scheme", file);
15557 if (mask & AFL_ASE_MCU)
15558 fputs ("\n\tMCU (MicroController) ASE", file);
15559 if (mask & AFL_ASE_MDMX)
15560 fputs ("\n\tMDMX ASE", file);
15561 if (mask & AFL_ASE_MIPS3D)
15562 fputs ("\n\tMIPS-3D ASE", file);
15563 if (mask & AFL_ASE_MT)
15564 fputs ("\n\tMT ASE", file);
15565 if (mask & AFL_ASE_SMARTMIPS)
15566 fputs ("\n\tSmartMIPS ASE", file);
15567 if (mask & AFL_ASE_VIRT)
15568 fputs ("\n\tVZ ASE", file);
15569 if (mask & AFL_ASE_MSA)
15570 fputs ("\n\tMSA ASE", file);
15571 if (mask & AFL_ASE_MIPS16)
15572 fputs ("\n\tMIPS16 ASE", file);
15573 if (mask & AFL_ASE_MICROMIPS)
15574 fputs ("\n\tMICROMIPS ASE", file);
15575 if (mask & AFL_ASE_XPA)
15576 fputs ("\n\tXPA ASE", file);
15577 if (mask == 0)
15578 fprintf (file, "\n\t%s", _("None"));
15579 else if ((mask & ~AFL_ASE_MASK) != 0)
15580 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
15581 }
15582
15583 static void
print_mips_isa_ext(FILE * file,unsigned int isa_ext)15584 print_mips_isa_ext (FILE *file, unsigned int isa_ext)
15585 {
15586 switch (isa_ext)
15587 {
15588 case 0:
15589 fputs (_("None"), file);
15590 break;
15591 case AFL_EXT_XLR:
15592 fputs ("RMI XLR", file);
15593 break;
15594 case AFL_EXT_OCTEON3:
15595 fputs ("Cavium Networks Octeon3", file);
15596 break;
15597 case AFL_EXT_OCTEON2:
15598 fputs ("Cavium Networks Octeon2", file);
15599 break;
15600 case AFL_EXT_OCTEONP:
15601 fputs ("Cavium Networks OcteonP", file);
15602 break;
15603 case AFL_EXT_LOONGSON_3A:
15604 fputs ("Loongson 3A", file);
15605 break;
15606 case AFL_EXT_OCTEON:
15607 fputs ("Cavium Networks Octeon", file);
15608 break;
15609 case AFL_EXT_5900:
15610 fputs ("Toshiba R5900", file);
15611 break;
15612 case AFL_EXT_4650:
15613 fputs ("MIPS R4650", file);
15614 break;
15615 case AFL_EXT_4010:
15616 fputs ("LSI R4010", file);
15617 break;
15618 case AFL_EXT_4100:
15619 fputs ("NEC VR4100", file);
15620 break;
15621 case AFL_EXT_3900:
15622 fputs ("Toshiba R3900", file);
15623 break;
15624 case AFL_EXT_10000:
15625 fputs ("MIPS R10000", file);
15626 break;
15627 case AFL_EXT_SB1:
15628 fputs ("Broadcom SB-1", file);
15629 break;
15630 case AFL_EXT_4111:
15631 fputs ("NEC VR4111/VR4181", file);
15632 break;
15633 case AFL_EXT_4120:
15634 fputs ("NEC VR4120", file);
15635 break;
15636 case AFL_EXT_5400:
15637 fputs ("NEC VR5400", file);
15638 break;
15639 case AFL_EXT_5500:
15640 fputs ("NEC VR5500", file);
15641 break;
15642 case AFL_EXT_LOONGSON_2E:
15643 fputs ("ST Microelectronics Loongson 2E", file);
15644 break;
15645 case AFL_EXT_LOONGSON_2F:
15646 fputs ("ST Microelectronics Loongson 2F", file);
15647 break;
15648 default:
15649 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
15650 break;
15651 }
15652 }
15653
15654 static void
print_mips_fp_abi_value(FILE * file,int val)15655 print_mips_fp_abi_value (FILE *file, int val)
15656 {
15657 switch (val)
15658 {
15659 case Val_GNU_MIPS_ABI_FP_ANY:
15660 fprintf (file, _("Hard or soft float\n"));
15661 break;
15662 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15663 fprintf (file, _("Hard float (double precision)\n"));
15664 break;
15665 case Val_GNU_MIPS_ABI_FP_SINGLE:
15666 fprintf (file, _("Hard float (single precision)\n"));
15667 break;
15668 case Val_GNU_MIPS_ABI_FP_SOFT:
15669 fprintf (file, _("Soft float\n"));
15670 break;
15671 case Val_GNU_MIPS_ABI_FP_OLD_64:
15672 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
15673 break;
15674 case Val_GNU_MIPS_ABI_FP_XX:
15675 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
15676 break;
15677 case Val_GNU_MIPS_ABI_FP_64:
15678 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
15679 break;
15680 case Val_GNU_MIPS_ABI_FP_64A:
15681 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
15682 break;
15683 default:
15684 fprintf (file, "??? (%d)\n", val);
15685 break;
15686 }
15687 }
15688
15689 static int
get_mips_reg_size(int reg_size)15690 get_mips_reg_size (int reg_size)
15691 {
15692 return (reg_size == AFL_REG_NONE) ? 0
15693 : (reg_size == AFL_REG_32) ? 32
15694 : (reg_size == AFL_REG_64) ? 64
15695 : (reg_size == AFL_REG_128) ? 128
15696 : -1;
15697 }
15698
15699 bfd_boolean
_bfd_mips_elf_print_private_bfd_data(bfd * abfd,void * ptr)15700 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
15701 {
15702 FILE *file = ptr;
15703
15704 BFD_ASSERT (abfd != NULL && ptr != NULL);
15705
15706 /* Print normal ELF private data. */
15707 _bfd_elf_print_private_bfd_data (abfd, ptr);
15708
15709 /* xgettext:c-format */
15710 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
15711
15712 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
15713 fprintf (file, _(" [abi=O32]"));
15714 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
15715 fprintf (file, _(" [abi=O64]"));
15716 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
15717 fprintf (file, _(" [abi=EABI32]"));
15718 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
15719 fprintf (file, _(" [abi=EABI64]"));
15720 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
15721 fprintf (file, _(" [abi unknown]"));
15722 else if (ABI_N32_P (abfd))
15723 fprintf (file, _(" [abi=N32]"));
15724 else if (ABI_64_P (abfd))
15725 fprintf (file, _(" [abi=64]"));
15726 else
15727 fprintf (file, _(" [no abi set]"));
15728
15729 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
15730 fprintf (file, " [mips1]");
15731 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
15732 fprintf (file, " [mips2]");
15733 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
15734 fprintf (file, " [mips3]");
15735 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
15736 fprintf (file, " [mips4]");
15737 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
15738 fprintf (file, " [mips5]");
15739 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
15740 fprintf (file, " [mips32]");
15741 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
15742 fprintf (file, " [mips64]");
15743 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
15744 fprintf (file, " [mips32r2]");
15745 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
15746 fprintf (file, " [mips64r2]");
15747 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
15748 fprintf (file, " [mips32r6]");
15749 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
15750 fprintf (file, " [mips64r6]");
15751 else
15752 fprintf (file, _(" [unknown ISA]"));
15753
15754 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
15755 fprintf (file, " [mdmx]");
15756
15757 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
15758 fprintf (file, " [mips16]");
15759
15760 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
15761 fprintf (file, " [micromips]");
15762
15763 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
15764 fprintf (file, " [nan2008]");
15765
15766 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
15767 fprintf (file, " [old fp64]");
15768
15769 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
15770 fprintf (file, " [32bitmode]");
15771 else
15772 fprintf (file, _(" [not 32bitmode]"));
15773
15774 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
15775 fprintf (file, " [noreorder]");
15776
15777 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
15778 fprintf (file, " [PIC]");
15779
15780 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
15781 fprintf (file, " [CPIC]");
15782
15783 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
15784 fprintf (file, " [XGOT]");
15785
15786 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
15787 fprintf (file, " [UCODE]");
15788
15789 fputc ('\n', file);
15790
15791 if (mips_elf_tdata (abfd)->abiflags_valid)
15792 {
15793 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
15794 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
15795 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
15796 if (abiflags->isa_rev > 1)
15797 fprintf (file, "r%d", abiflags->isa_rev);
15798 fprintf (file, "\nGPR size: %d",
15799 get_mips_reg_size (abiflags->gpr_size));
15800 fprintf (file, "\nCPR1 size: %d",
15801 get_mips_reg_size (abiflags->cpr1_size));
15802 fprintf (file, "\nCPR2 size: %d",
15803 get_mips_reg_size (abiflags->cpr2_size));
15804 fputs ("\nFP ABI: ", file);
15805 print_mips_fp_abi_value (file, abiflags->fp_abi);
15806 fputs ("ISA Extension: ", file);
15807 print_mips_isa_ext (file, abiflags->isa_ext);
15808 fputs ("\nASEs:", file);
15809 print_mips_ases (file, abiflags->ases);
15810 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
15811 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
15812 fputc ('\n', file);
15813 }
15814
15815 return TRUE;
15816 }
15817
15818 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
15819 {
15820 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15821 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15822 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
15823 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15824 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15825 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
15826 { NULL, 0, 0, 0, 0 }
15827 };
15828
15829 /* Merge non visibility st_other attributes. Ensure that the
15830 STO_OPTIONAL flag is copied into h->other, even if this is not a
15831 definiton of the symbol. */
15832 void
_bfd_mips_elf_merge_symbol_attribute(struct elf_link_hash_entry * h,const Elf_Internal_Sym * isym,bfd_boolean definition,bfd_boolean dynamic ATTRIBUTE_UNUSED)15833 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
15834 const Elf_Internal_Sym *isym,
15835 bfd_boolean definition,
15836 bfd_boolean dynamic ATTRIBUTE_UNUSED)
15837 {
15838 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
15839 {
15840 unsigned char other;
15841
15842 other = (definition ? isym->st_other : h->other);
15843 other &= ~ELF_ST_VISIBILITY (-1);
15844 h->other = other | ELF_ST_VISIBILITY (h->other);
15845 }
15846
15847 if (!definition
15848 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
15849 h->other |= STO_OPTIONAL;
15850 }
15851
15852 /* Decide whether an undefined symbol is special and can be ignored.
15853 This is the case for OPTIONAL symbols on IRIX. */
15854 bfd_boolean
_bfd_mips_elf_ignore_undef_symbol(struct elf_link_hash_entry * h)15855 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
15856 {
15857 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
15858 }
15859
15860 bfd_boolean
_bfd_mips_elf_common_definition(Elf_Internal_Sym * sym)15861 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
15862 {
15863 return (sym->st_shndx == SHN_COMMON
15864 || sym->st_shndx == SHN_MIPS_ACOMMON
15865 || sym->st_shndx == SHN_MIPS_SCOMMON);
15866 }
15867
15868 /* Return address for Ith PLT stub in section PLT, for relocation REL
15869 or (bfd_vma) -1 if it should not be included. */
15870
15871 bfd_vma
_bfd_mips_elf_plt_sym_val(bfd_vma i,const asection * plt,const arelent * rel ATTRIBUTE_UNUSED)15872 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
15873 const arelent *rel ATTRIBUTE_UNUSED)
15874 {
15875 return (plt->vma
15876 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
15877 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
15878 }
15879
15880 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
15881 and microMIPS PLT slots we may have a many-to-one mapping between .plt
15882 and .got.plt and also the slots may be of a different size each we walk
15883 the PLT manually fetching instructions and matching them against known
15884 patterns. To make things easier standard MIPS slots, if any, always come
15885 first. As we don't create proper ELF symbols we use the UDATA.I member
15886 of ASYMBOL to carry ISA annotation. The encoding used is the same as
15887 with the ST_OTHER member of the ELF symbol. */
15888
15889 long
_bfd_mips_elf_get_synthetic_symtab(bfd * abfd,long symcount ATTRIBUTE_UNUSED,asymbol ** syms ATTRIBUTE_UNUSED,long dynsymcount,asymbol ** dynsyms,asymbol ** ret)15890 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
15891 long symcount ATTRIBUTE_UNUSED,
15892 asymbol **syms ATTRIBUTE_UNUSED,
15893 long dynsymcount, asymbol **dynsyms,
15894 asymbol **ret)
15895 {
15896 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
15897 static const char microsuffix[] = "@micromipsplt";
15898 static const char m16suffix[] = "@mips16plt";
15899 static const char mipssuffix[] = "@plt";
15900
15901 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
15902 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15903 bfd_boolean micromips_p = MICROMIPS_P (abfd);
15904 Elf_Internal_Shdr *hdr;
15905 bfd_byte *plt_data;
15906 bfd_vma plt_offset;
15907 unsigned int other;
15908 bfd_vma entry_size;
15909 bfd_vma plt0_size;
15910 asection *relplt;
15911 bfd_vma opcode;
15912 asection *plt;
15913 asymbol *send;
15914 size_t size;
15915 char *names;
15916 long counti;
15917 arelent *p;
15918 asymbol *s;
15919 char *nend;
15920 long count;
15921 long pi;
15922 long i;
15923 long n;
15924
15925 *ret = NULL;
15926
15927 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
15928 return 0;
15929
15930 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
15931 if (relplt == NULL)
15932 return 0;
15933
15934 hdr = &elf_section_data (relplt)->this_hdr;
15935 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
15936 return 0;
15937
15938 plt = bfd_get_section_by_name (abfd, ".plt");
15939 if (plt == NULL)
15940 return 0;
15941
15942 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
15943 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
15944 return -1;
15945 p = relplt->relocation;
15946
15947 /* Calculating the exact amount of space required for symbols would
15948 require two passes over the PLT, so just pessimise assuming two
15949 PLT slots per relocation. */
15950 count = relplt->size / hdr->sh_entsize;
15951 counti = count * bed->s->int_rels_per_ext_rel;
15952 size = 2 * count * sizeof (asymbol);
15953 size += count * (sizeof (mipssuffix) +
15954 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
15955 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
15956 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
15957
15958 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
15959 size += sizeof (asymbol) + sizeof (pltname);
15960
15961 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
15962 return -1;
15963
15964 if (plt->size < 16)
15965 return -1;
15966
15967 s = *ret = bfd_malloc (size);
15968 if (s == NULL)
15969 return -1;
15970 send = s + 2 * count + 1;
15971
15972 names = (char *) send;
15973 nend = (char *) s + size;
15974 n = 0;
15975
15976 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
15977 if (opcode == 0x3302fffe)
15978 {
15979 if (!micromips_p)
15980 return -1;
15981 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
15982 other = STO_MICROMIPS;
15983 }
15984 else if (opcode == 0x0398c1d0)
15985 {
15986 if (!micromips_p)
15987 return -1;
15988 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
15989 other = STO_MICROMIPS;
15990 }
15991 else
15992 {
15993 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
15994 other = 0;
15995 }
15996
15997 s->the_bfd = abfd;
15998 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
15999 s->section = plt;
16000 s->value = 0;
16001 s->name = names;
16002 s->udata.i = other;
16003 memcpy (names, pltname, sizeof (pltname));
16004 names += sizeof (pltname);
16005 ++s, ++n;
16006
16007 pi = 0;
16008 for (plt_offset = plt0_size;
16009 plt_offset + 8 <= plt->size && s < send;
16010 plt_offset += entry_size)
16011 {
16012 bfd_vma gotplt_addr;
16013 const char *suffix;
16014 bfd_vma gotplt_hi;
16015 bfd_vma gotplt_lo;
16016 size_t suffixlen;
16017
16018 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16019
16020 /* Check if the second word matches the expected MIPS16 instruction. */
16021 if (opcode == 0x651aeb00)
16022 {
16023 if (micromips_p)
16024 return -1;
16025 /* Truncated table??? */
16026 if (plt_offset + 16 > plt->size)
16027 break;
16028 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16029 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16030 suffixlen = sizeof (m16suffix);
16031 suffix = m16suffix;
16032 other = STO_MIPS16;
16033 }
16034 /* Likewise the expected microMIPS instruction (no insn32 mode). */
16035 else if (opcode == 0xff220000)
16036 {
16037 if (!micromips_p)
16038 return -1;
16039 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16040 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16041 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16042 gotplt_lo <<= 2;
16043 gotplt_addr = gotplt_hi + gotplt_lo;
16044 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16045 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16046 suffixlen = sizeof (microsuffix);
16047 suffix = microsuffix;
16048 other = STO_MICROMIPS;
16049 }
16050 /* Likewise the expected microMIPS instruction (insn32 mode). */
16051 else if ((opcode & 0xffff0000) == 0xff2f0000)
16052 {
16053 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16054 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16055 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16056 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16057 gotplt_addr = gotplt_hi + gotplt_lo;
16058 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16059 suffixlen = sizeof (microsuffix);
16060 suffix = microsuffix;
16061 other = STO_MICROMIPS;
16062 }
16063 /* Otherwise assume standard MIPS code. */
16064 else
16065 {
16066 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16067 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16068 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16069 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16070 gotplt_addr = gotplt_hi + gotplt_lo;
16071 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16072 suffixlen = sizeof (mipssuffix);
16073 suffix = mipssuffix;
16074 other = 0;
16075 }
16076 /* Truncated table??? */
16077 if (plt_offset + entry_size > plt->size)
16078 break;
16079
16080 for (i = 0;
16081 i < count && p[pi].address != gotplt_addr;
16082 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16083
16084 if (i < count)
16085 {
16086 size_t namelen;
16087 size_t len;
16088
16089 *s = **p[pi].sym_ptr_ptr;
16090 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16091 we are defining a symbol, ensure one of them is set. */
16092 if ((s->flags & BSF_LOCAL) == 0)
16093 s->flags |= BSF_GLOBAL;
16094 s->flags |= BSF_SYNTHETIC;
16095 s->section = plt;
16096 s->value = plt_offset;
16097 s->name = names;
16098 s->udata.i = other;
16099
16100 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16101 namelen = len + suffixlen;
16102 if (names + namelen > nend)
16103 break;
16104
16105 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16106 names += len;
16107 memcpy (names, suffix, suffixlen);
16108 names += suffixlen;
16109
16110 ++s, ++n;
16111 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16112 }
16113 }
16114
16115 free (plt_data);
16116
16117 return n;
16118 }
16119
16120 void
_bfd_mips_post_process_headers(bfd * abfd,struct bfd_link_info * link_info)16121 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
16122 {
16123 struct mips_elf_link_hash_table *htab;
16124 Elf_Internal_Ehdr *i_ehdrp;
16125
16126 i_ehdrp = elf_elfheader (abfd);
16127 if (link_info)
16128 {
16129 htab = mips_elf_hash_table (link_info);
16130 BFD_ASSERT (htab != NULL);
16131
16132 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
16133 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
16134 }
16135
16136 _bfd_elf_post_process_headers (abfd, link_info);
16137
16138 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16139 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
16140 i_ehdrp->e_ident[EI_ABIVERSION] = 3;
16141 }
16142