This patch causes local GOT entries addressed via a 16-bit index to
[binutils-gdb.git] / bfd / elfxx-mips.c
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're generating code for VxWorks. */
446 bfd_boolean is_vxworks;
447
448 /* True if we already reported the small-data section overflow. */
449 bfd_boolean small_data_overflow_reported;
450
451 /* Shortcuts to some dynamic sections, or NULL if they are not
452 being used. */
453 asection *srelbss;
454 asection *sdynbss;
455 asection *srelplt;
456 asection *srelplt2;
457 asection *sgotplt;
458 asection *splt;
459 asection *sstubs;
460 asection *sgot;
461
462 /* The master GOT information. */
463 struct mips_got_info *got_info;
464
465 /* The global symbol in the GOT with the lowest index in the dynamic
466 symbol table. */
467 struct elf_link_hash_entry *global_gotsym;
468
469 /* The size of the PLT header in bytes. */
470 bfd_vma plt_header_size;
471
472 /* The size of a standard PLT entry in bytes. */
473 bfd_vma plt_mips_entry_size;
474
475 /* The size of a compressed PLT entry in bytes. */
476 bfd_vma plt_comp_entry_size;
477
478 /* The offset of the next standard PLT entry to create. */
479 bfd_vma plt_mips_offset;
480
481 /* The offset of the next compressed PLT entry to create. */
482 bfd_vma plt_comp_offset;
483
484 /* The index of the next .got.plt entry to create. */
485 bfd_vma plt_got_index;
486
487 /* The number of functions that need a lazy-binding stub. */
488 bfd_vma lazy_stub_count;
489
490 /* The size of a function stub entry in bytes. */
491 bfd_vma function_stub_size;
492
493 /* The number of reserved entries at the beginning of the GOT. */
494 unsigned int reserved_gotno;
495
496 /* The section used for mips_elf_la25_stub trampolines.
497 See the comment above that structure for details. */
498 asection *strampoline;
499
500 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
501 pairs. */
502 htab_t la25_stubs;
503
504 /* A function FN (NAME, IS, OS) that creates a new input section
505 called NAME and links it to output section OS. If IS is nonnull,
506 the new section should go immediately before it, otherwise it
507 should go at the (current) beginning of OS.
508
509 The function returns the new section on success, otherwise it
510 returns null. */
511 asection *(*add_stub_section) (const char *, asection *, asection *);
512
513 /* Small local sym cache. */
514 struct sym_cache sym_cache;
515
516 /* Is the PLT header compressed? */
517 unsigned int plt_header_is_comp : 1;
518 };
519
520 /* Get the MIPS ELF linker hash table from a link_info structure. */
521
522 #define mips_elf_hash_table(p) \
523 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
524 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
525
526 /* A structure used to communicate with htab_traverse callbacks. */
527 struct mips_htab_traverse_info
528 {
529 /* The usual link-wide information. */
530 struct bfd_link_info *info;
531 bfd *output_bfd;
532
533 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
534 bfd_boolean error;
535 };
536
537 /* MIPS ELF private object data. */
538
539 struct mips_elf_obj_tdata
540 {
541 /* Generic ELF private object data. */
542 struct elf_obj_tdata root;
543
544 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
545 bfd *abi_fp_bfd;
546
547 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
548 bfd *abi_msa_bfd;
549
550 /* The GOT requirements of input bfds. */
551 struct mips_got_info *got;
552
553 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
554 included directly in this one, but there's no point to wasting
555 the memory just for the infrequently called find_nearest_line. */
556 struct mips_elf_find_line *find_line_info;
557
558 /* An array of stub sections indexed by symbol number. */
559 asection **local_stubs;
560 asection **local_call_stubs;
561
562 /* The Irix 5 support uses two virtual sections, which represent
563 text/data symbols defined in dynamic objects. */
564 asymbol *elf_data_symbol;
565 asymbol *elf_text_symbol;
566 asection *elf_data_section;
567 asection *elf_text_section;
568 };
569
570 /* Get MIPS ELF private object data from BFD's tdata. */
571
572 #define mips_elf_tdata(bfd) \
573 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
574
575 #define TLS_RELOC_P(r_type) \
576 (r_type == R_MIPS_TLS_DTPMOD32 \
577 || r_type == R_MIPS_TLS_DTPMOD64 \
578 || r_type == R_MIPS_TLS_DTPREL32 \
579 || r_type == R_MIPS_TLS_DTPREL64 \
580 || r_type == R_MIPS_TLS_GD \
581 || r_type == R_MIPS_TLS_LDM \
582 || r_type == R_MIPS_TLS_DTPREL_HI16 \
583 || r_type == R_MIPS_TLS_DTPREL_LO16 \
584 || r_type == R_MIPS_TLS_GOTTPREL \
585 || r_type == R_MIPS_TLS_TPREL32 \
586 || r_type == R_MIPS_TLS_TPREL64 \
587 || r_type == R_MIPS_TLS_TPREL_HI16 \
588 || r_type == R_MIPS_TLS_TPREL_LO16 \
589 || r_type == R_MIPS16_TLS_GD \
590 || r_type == R_MIPS16_TLS_LDM \
591 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
592 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
593 || r_type == R_MIPS16_TLS_GOTTPREL \
594 || r_type == R_MIPS16_TLS_TPREL_HI16 \
595 || r_type == R_MIPS16_TLS_TPREL_LO16 \
596 || r_type == R_MICROMIPS_TLS_GD \
597 || r_type == R_MICROMIPS_TLS_LDM \
598 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
599 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
600 || r_type == R_MICROMIPS_TLS_GOTTPREL \
601 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
602 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
603
604 /* Structure used to pass information to mips_elf_output_extsym. */
605
606 struct extsym_info
607 {
608 bfd *abfd;
609 struct bfd_link_info *info;
610 struct ecoff_debug_info *debug;
611 const struct ecoff_debug_swap *swap;
612 bfd_boolean failed;
613 };
614
615 /* The names of the runtime procedure table symbols used on IRIX5. */
616
617 static const char * const mips_elf_dynsym_rtproc_names[] =
618 {
619 "_procedure_table",
620 "_procedure_string_table",
621 "_procedure_table_size",
622 NULL
623 };
624
625 /* These structures are used to generate the .compact_rel section on
626 IRIX5. */
627
628 typedef struct
629 {
630 unsigned long id1; /* Always one? */
631 unsigned long num; /* Number of compact relocation entries. */
632 unsigned long id2; /* Always two? */
633 unsigned long offset; /* The file offset of the first relocation. */
634 unsigned long reserved0; /* Zero? */
635 unsigned long reserved1; /* Zero? */
636 } Elf32_compact_rel;
637
638 typedef struct
639 {
640 bfd_byte id1[4];
641 bfd_byte num[4];
642 bfd_byte id2[4];
643 bfd_byte offset[4];
644 bfd_byte reserved0[4];
645 bfd_byte reserved1[4];
646 } Elf32_External_compact_rel;
647
648 typedef struct
649 {
650 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
651 unsigned int rtype : 4; /* Relocation types. See below. */
652 unsigned int dist2to : 8;
653 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
654 unsigned long konst; /* KONST field. See below. */
655 unsigned long vaddr; /* VADDR to be relocated. */
656 } Elf32_crinfo;
657
658 typedef struct
659 {
660 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
661 unsigned int rtype : 4; /* Relocation types. See below. */
662 unsigned int dist2to : 8;
663 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
664 unsigned long konst; /* KONST field. See below. */
665 } Elf32_crinfo2;
666
667 typedef struct
668 {
669 bfd_byte info[4];
670 bfd_byte konst[4];
671 bfd_byte vaddr[4];
672 } Elf32_External_crinfo;
673
674 typedef struct
675 {
676 bfd_byte info[4];
677 bfd_byte konst[4];
678 } Elf32_External_crinfo2;
679
680 /* These are the constants used to swap the bitfields in a crinfo. */
681
682 #define CRINFO_CTYPE (0x1)
683 #define CRINFO_CTYPE_SH (31)
684 #define CRINFO_RTYPE (0xf)
685 #define CRINFO_RTYPE_SH (27)
686 #define CRINFO_DIST2TO (0xff)
687 #define CRINFO_DIST2TO_SH (19)
688 #define CRINFO_RELVADDR (0x7ffff)
689 #define CRINFO_RELVADDR_SH (0)
690
691 /* A compact relocation info has long (3 words) or short (2 words)
692 formats. A short format doesn't have VADDR field and relvaddr
693 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
694 #define CRF_MIPS_LONG 1
695 #define CRF_MIPS_SHORT 0
696
697 /* There are 4 types of compact relocation at least. The value KONST
698 has different meaning for each type:
699
700 (type) (konst)
701 CT_MIPS_REL32 Address in data
702 CT_MIPS_WORD Address in word (XXX)
703 CT_MIPS_GPHI_LO GP - vaddr
704 CT_MIPS_JMPAD Address to jump
705 */
706
707 #define CRT_MIPS_REL32 0xa
708 #define CRT_MIPS_WORD 0xb
709 #define CRT_MIPS_GPHI_LO 0xc
710 #define CRT_MIPS_JMPAD 0xd
711
712 #define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
713 #define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
714 #define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
715 #define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
716 \f
717 /* The structure of the runtime procedure descriptor created by the
718 loader for use by the static exception system. */
719
720 typedef struct runtime_pdr {
721 bfd_vma adr; /* Memory address of start of procedure. */
722 long regmask; /* Save register mask. */
723 long regoffset; /* Save register offset. */
724 long fregmask; /* Save floating point register mask. */
725 long fregoffset; /* Save floating point register offset. */
726 long frameoffset; /* Frame size. */
727 short framereg; /* Frame pointer register. */
728 short pcreg; /* Offset or reg of return pc. */
729 long irpss; /* Index into the runtime string table. */
730 long reserved;
731 struct exception_info *exception_info;/* Pointer to exception array. */
732 } RPDR, *pRPDR;
733 #define cbRPDR sizeof (RPDR)
734 #define rpdNil ((pRPDR) 0)
735 \f
736 static struct mips_got_entry *mips_elf_create_local_got_entry
737 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
738 struct mips_elf_link_hash_entry *, int);
739 static bfd_boolean mips_elf_sort_hash_table_f
740 (struct mips_elf_link_hash_entry *, void *);
741 static bfd_vma mips_elf_high
742 (bfd_vma);
743 static bfd_boolean mips_elf_create_dynamic_relocation
744 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
745 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
746 bfd_vma *, asection *);
747 static bfd_vma mips_elf_adjust_gp
748 (bfd *, struct mips_got_info *, bfd *);
749
750 /* This will be used when we sort the dynamic relocation records. */
751 static bfd *reldyn_sorting_bfd;
752
753 /* True if ABFD is for CPUs with load interlocking that include
754 non-MIPS1 CPUs and R3900. */
755 #define LOAD_INTERLOCKS_P(abfd) \
756 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
757 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
758
759 /* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
760 This should be safe for all architectures. We enable this predicate
761 for RM9000 for now. */
762 #define JAL_TO_BAL_P(abfd) \
763 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
764
765 /* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
766 This should be safe for all architectures. We enable this predicate for
767 all CPUs. */
768 #define JALR_TO_BAL_P(abfd) 1
769
770 /* True if ABFD is for CPUs that are faster if JR is converted to B.
771 This should be safe for all architectures. We enable this predicate for
772 all CPUs. */
773 #define JR_TO_B_P(abfd) 1
774
775 /* True if ABFD is a PIC object. */
776 #define PIC_OBJECT_P(abfd) \
777 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
778
779 /* Nonzero if ABFD is using the N32 ABI. */
780 #define ABI_N32_P(abfd) \
781 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
782
783 /* Nonzero if ABFD is using the N64 ABI. */
784 #define ABI_64_P(abfd) \
785 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
786
787 /* Nonzero if ABFD is using NewABI conventions. */
788 #define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
789
790 /* Nonzero if ABFD has microMIPS code. */
791 #define MICROMIPS_P(abfd) \
792 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
793
794 /* The IRIX compatibility level we are striving for. */
795 #define IRIX_COMPAT(abfd) \
796 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
797
798 /* Whether we are trying to be compatible with IRIX at all. */
799 #define SGI_COMPAT(abfd) \
800 (IRIX_COMPAT (abfd) != ict_none)
801
802 /* The name of the options section. */
803 #define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
804 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
805
806 /* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
807 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
808 #define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
809 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
810
811 /* Whether the section is readonly. */
812 #define MIPS_ELF_READONLY_SECTION(sec) \
813 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
814 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
815
816 /* The name of the stub section. */
817 #define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
818
819 /* The size of an external REL relocation. */
820 #define MIPS_ELF_REL_SIZE(abfd) \
821 (get_elf_backend_data (abfd)->s->sizeof_rel)
822
823 /* The size of an external RELA relocation. */
824 #define MIPS_ELF_RELA_SIZE(abfd) \
825 (get_elf_backend_data (abfd)->s->sizeof_rela)
826
827 /* The size of an external dynamic table entry. */
828 #define MIPS_ELF_DYN_SIZE(abfd) \
829 (get_elf_backend_data (abfd)->s->sizeof_dyn)
830
831 /* The size of a GOT entry. */
832 #define MIPS_ELF_GOT_SIZE(abfd) \
833 (get_elf_backend_data (abfd)->s->arch_size / 8)
834
835 /* The size of the .rld_map section. */
836 #define MIPS_ELF_RLD_MAP_SIZE(abfd) \
837 (get_elf_backend_data (abfd)->s->arch_size / 8)
838
839 /* The size of a symbol-table entry. */
840 #define MIPS_ELF_SYM_SIZE(abfd) \
841 (get_elf_backend_data (abfd)->s->sizeof_sym)
842
843 /* The default alignment for sections, as a power of two. */
844 #define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
845 (get_elf_backend_data (abfd)->s->log_file_align)
846
847 /* Get word-sized data. */
848 #define MIPS_ELF_GET_WORD(abfd, ptr) \
849 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
850
851 /* Put out word-sized data. */
852 #define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
853 (ABI_64_P (abfd) \
854 ? bfd_put_64 (abfd, val, ptr) \
855 : bfd_put_32 (abfd, val, ptr))
856
857 /* The opcode for word-sized loads (LW or LD). */
858 #define MIPS_ELF_LOAD_WORD(abfd) \
859 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
860
861 /* Add a dynamic symbol table-entry. */
862 #define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
863 _bfd_elf_add_dynamic_entry (info, tag, val)
864
865 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
866 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
867
868 /* The name of the dynamic relocation section. */
869 #define MIPS_ELF_REL_DYN_NAME(INFO) \
870 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
871
872 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
873 from smaller values. Start with zero, widen, *then* decrement. */
874 #define MINUS_ONE (((bfd_vma)0) - 1)
875 #define MINUS_TWO (((bfd_vma)0) - 2)
876
877 /* The value to write into got[1] for SVR4 targets, to identify it is
878 a GNU object. The dynamic linker can then use got[1] to store the
879 module pointer. */
880 #define MIPS_ELF_GNU_GOT1_MASK(abfd) \
881 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
882
883 /* The offset of $gp from the beginning of the .got section. */
884 #define ELF_MIPS_GP_OFFSET(INFO) \
885 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
886
887 /* The maximum size of the GOT for it to be addressable using 16-bit
888 offsets from $gp. */
889 #define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
890
891 /* Instructions which appear in a stub. */
892 #define STUB_LW(abfd) \
893 ((ABI_64_P (abfd) \
894 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
895 : 0x8f998010)) /* lw t9,0x8010(gp) */
896 #define STUB_MOVE(abfd) \
897 ((ABI_64_P (abfd) \
898 ? 0x03e0782d /* daddu t7,ra */ \
899 : 0x03e07821)) /* addu t7,ra */
900 #define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
901 #define STUB_JALR 0x0320f809 /* jalr t9,ra */
902 #define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
903 #define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
904 #define STUB_LI16S(abfd, VAL) \
905 ((ABI_64_P (abfd) \
906 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
907 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
908
909 /* Likewise for the microMIPS ASE. */
910 #define STUB_LW_MICROMIPS(abfd) \
911 (ABI_64_P (abfd) \
912 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
913 : 0xff3c8010) /* lw t9,0x8010(gp) */
914 #define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
915 #define STUB_MOVE32_MICROMIPS(abfd) \
916 (ABI_64_P (abfd) \
917 ? 0x581f7950 /* daddu t7,ra,zero */ \
918 : 0x001f7950) /* addu t7,ra,zero */
919 #define STUB_LUI_MICROMIPS(VAL) \
920 (0x41b80000 + (VAL)) /* lui t8,VAL */
921 #define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
922 #define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
923 #define STUB_ORI_MICROMIPS(VAL) \
924 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
925 #define STUB_LI16U_MICROMIPS(VAL) \
926 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
927 #define STUB_LI16S_MICROMIPS(abfd, VAL) \
928 (ABI_64_P (abfd) \
929 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
930 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
931
932 #define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
933 #define MIPS_FUNCTION_STUB_BIG_SIZE 20
934 #define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
935 #define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
936 #define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
937 #define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
938
939 /* The name of the dynamic interpreter. This is put in the .interp
940 section. */
941
942 #define ELF_DYNAMIC_INTERPRETER(abfd) \
943 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
944 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
945 : "/usr/lib/libc.so.1")
946
947 #ifdef BFD64
948 #define MNAME(bfd,pre,pos) \
949 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
950 #define ELF_R_SYM(bfd, i) \
951 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
952 #define ELF_R_TYPE(bfd, i) \
953 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
954 #define ELF_R_INFO(bfd, s, t) \
955 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
956 #else
957 #define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
958 #define ELF_R_SYM(bfd, i) \
959 (ELF32_R_SYM (i))
960 #define ELF_R_TYPE(bfd, i) \
961 (ELF32_R_TYPE (i))
962 #define ELF_R_INFO(bfd, s, t) \
963 (ELF32_R_INFO (s, t))
964 #endif
965 \f
966 /* The mips16 compiler uses a couple of special sections to handle
967 floating point arguments.
968
969 Section names that look like .mips16.fn.FNNAME contain stubs that
970 copy floating point arguments from the fp regs to the gp regs and
971 then jump to FNNAME. If any 32 bit function calls FNNAME, the
972 call should be redirected to the stub instead. If no 32 bit
973 function calls FNNAME, the stub should be discarded. We need to
974 consider any reference to the function, not just a call, because
975 if the address of the function is taken we will need the stub,
976 since the address might be passed to a 32 bit function.
977
978 Section names that look like .mips16.call.FNNAME contain stubs
979 that copy floating point arguments from the gp regs to the fp
980 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
981 then any 16 bit function that calls FNNAME should be redirected
982 to the stub instead. If FNNAME is not a 32 bit function, the
983 stub should be discarded.
984
985 .mips16.call.fp.FNNAME sections are similar, but contain stubs
986 which call FNNAME and then copy the return value from the fp regs
987 to the gp regs. These stubs store the return value in $18 while
988 calling FNNAME; any function which might call one of these stubs
989 must arrange to save $18 around the call. (This case is not
990 needed for 32 bit functions that call 16 bit functions, because
991 16 bit functions always return floating point values in both
992 $f0/$f1 and $2/$3.)
993
994 Note that in all cases FNNAME might be defined statically.
995 Therefore, FNNAME is not used literally. Instead, the relocation
996 information will indicate which symbol the section is for.
997
998 We record any stubs that we find in the symbol table. */
999
1000 #define FN_STUB ".mips16.fn."
1001 #define CALL_STUB ".mips16.call."
1002 #define CALL_FP_STUB ".mips16.call.fp."
1003
1004 #define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1005 #define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1006 #define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
1007 \f
1008 /* The format of the first PLT entry in an O32 executable. */
1009 static const bfd_vma mips_o32_exec_plt0_entry[] =
1010 {
1011 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1012 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1013 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1014 0x031cc023, /* subu $24, $24, $28 */
1015 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1016 0x0018c082, /* srl $24, $24, 2 */
1017 0x0320f809, /* jalr $25 */
1018 0x2718fffe /* subu $24, $24, 2 */
1019 };
1020
1021 /* The format of the first PLT entry in an N32 executable. Different
1022 because gp ($28) is not available; we use t2 ($14) instead. */
1023 static const bfd_vma mips_n32_exec_plt0_entry[] =
1024 {
1025 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1026 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1027 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1028 0x030ec023, /* subu $24, $24, $14 */
1029 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1030 0x0018c082, /* srl $24, $24, 2 */
1031 0x0320f809, /* jalr $25 */
1032 0x2718fffe /* subu $24, $24, 2 */
1033 };
1034
1035 /* The format of the first PLT entry in an N64 executable. Different
1036 from N32 because of the increased size of GOT entries. */
1037 static const bfd_vma mips_n64_exec_plt0_entry[] =
1038 {
1039 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1040 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1041 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1042 0x030ec023, /* subu $24, $24, $14 */
1043 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1044 0x0018c0c2, /* srl $24, $24, 3 */
1045 0x0320f809, /* jalr $25 */
1046 0x2718fffe /* subu $24, $24, 2 */
1047 };
1048
1049 /* The format of the microMIPS first PLT entry in an O32 executable.
1050 We rely on v0 ($2) rather than t8 ($24) to contain the address
1051 of the GOTPLT entry handled, so this stub may only be used when
1052 all the subsequent PLT entries are microMIPS code too.
1053
1054 The trailing NOP is for alignment and correct disassembly only. */
1055 static const bfd_vma micromips_o32_exec_plt0_entry[] =
1056 {
1057 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1058 0xff23, 0x0000, /* lw $25, 0($3) */
1059 0x0535, /* subu $2, $2, $3 */
1060 0x2525, /* srl $2, $2, 2 */
1061 0x3302, 0xfffe, /* subu $24, $2, 2 */
1062 0x0dff, /* move $15, $31 */
1063 0x45f9, /* jalrs $25 */
1064 0x0f83, /* move $28, $3 */
1065 0x0c00 /* nop */
1066 };
1067
1068 /* The format of the microMIPS first PLT entry in an O32 executable
1069 in the insn32 mode. */
1070 static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1071 {
1072 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1073 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1074 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1075 0x0398, 0xc1d0, /* subu $24, $24, $28 */
1076 0x001f, 0x7950, /* move $15, $31 */
1077 0x0318, 0x1040, /* srl $24, $24, 2 */
1078 0x03f9, 0x0f3c, /* jalr $25 */
1079 0x3318, 0xfffe /* subu $24, $24, 2 */
1080 };
1081
1082 /* The format of subsequent standard PLT entries. */
1083 static const bfd_vma mips_exec_plt_entry[] =
1084 {
1085 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1086 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1087 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1088 0x03200008 /* jr $25 */
1089 };
1090
1091 /* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1092 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1093 directly addressable. */
1094 static const bfd_vma mips16_o32_exec_plt_entry[] =
1095 {
1096 0xb203, /* lw $2, 12($pc) */
1097 0x9a60, /* lw $3, 0($2) */
1098 0x651a, /* move $24, $2 */
1099 0xeb00, /* jr $3 */
1100 0x653b, /* move $25, $3 */
1101 0x6500, /* nop */
1102 0x0000, 0x0000 /* .word (.got.plt entry) */
1103 };
1104
1105 /* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1106 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1107 static const bfd_vma micromips_o32_exec_plt_entry[] =
1108 {
1109 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1110 0xff22, 0x0000, /* lw $25, 0($2) */
1111 0x4599, /* jr $25 */
1112 0x0f02 /* move $24, $2 */
1113 };
1114
1115 /* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1116 static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1117 {
1118 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1119 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1120 0x0019, 0x0f3c, /* jr $25 */
1121 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1122 };
1123
1124 /* The format of the first PLT entry in a VxWorks executable. */
1125 static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1126 {
1127 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1128 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1129 0x8f390008, /* lw t9, 8(t9) */
1130 0x00000000, /* nop */
1131 0x03200008, /* jr t9 */
1132 0x00000000 /* nop */
1133 };
1134
1135 /* The format of subsequent PLT entries. */
1136 static const bfd_vma mips_vxworks_exec_plt_entry[] =
1137 {
1138 0x10000000, /* b .PLT_resolver */
1139 0x24180000, /* li t8, <pltindex> */
1140 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1141 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1142 0x8f390000, /* lw t9, 0(t9) */
1143 0x00000000, /* nop */
1144 0x03200008, /* jr t9 */
1145 0x00000000 /* nop */
1146 };
1147
1148 /* The format of the first PLT entry in a VxWorks shared object. */
1149 static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1150 {
1151 0x8f990008, /* lw t9, 8(gp) */
1152 0x00000000, /* nop */
1153 0x03200008, /* jr t9 */
1154 0x00000000, /* nop */
1155 0x00000000, /* nop */
1156 0x00000000 /* nop */
1157 };
1158
1159 /* The format of subsequent PLT entries. */
1160 static const bfd_vma mips_vxworks_shared_plt_entry[] =
1161 {
1162 0x10000000, /* b .PLT_resolver */
1163 0x24180000 /* li t8, <pltindex> */
1164 };
1165 \f
1166 /* microMIPS 32-bit opcode helper installer. */
1167
1168 static void
1169 bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1170 {
1171 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
1172 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
1173 }
1174
1175 /* microMIPS 32-bit opcode helper retriever. */
1176
1177 static bfd_vma
1178 bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1179 {
1180 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1181 }
1182 \f
1183 /* Look up an entry in a MIPS ELF linker hash table. */
1184
1185 #define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1186 ((struct mips_elf_link_hash_entry *) \
1187 elf_link_hash_lookup (&(table)->root, (string), (create), \
1188 (copy), (follow)))
1189
1190 /* Traverse a MIPS ELF linker hash table. */
1191
1192 #define mips_elf_link_hash_traverse(table, func, info) \
1193 (elf_link_hash_traverse \
1194 (&(table)->root, \
1195 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
1196 (info)))
1197
1198 /* Find the base offsets for thread-local storage in this object,
1199 for GD/LD and IE/LE respectively. */
1200
1201 #define TP_OFFSET 0x7000
1202 #define DTP_OFFSET 0x8000
1203
1204 static bfd_vma
1205 dtprel_base (struct bfd_link_info *info)
1206 {
1207 /* If tls_sec is NULL, we should have signalled an error already. */
1208 if (elf_hash_table (info)->tls_sec == NULL)
1209 return 0;
1210 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1211 }
1212
1213 static bfd_vma
1214 tprel_base (struct bfd_link_info *info)
1215 {
1216 /* If tls_sec is NULL, we should have signalled an error already. */
1217 if (elf_hash_table (info)->tls_sec == NULL)
1218 return 0;
1219 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1220 }
1221
1222 /* Create an entry in a MIPS ELF linker hash table. */
1223
1224 static struct bfd_hash_entry *
1225 mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1226 struct bfd_hash_table *table, const char *string)
1227 {
1228 struct mips_elf_link_hash_entry *ret =
1229 (struct mips_elf_link_hash_entry *) entry;
1230
1231 /* Allocate the structure if it has not already been allocated by a
1232 subclass. */
1233 if (ret == NULL)
1234 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1235 if (ret == NULL)
1236 return (struct bfd_hash_entry *) ret;
1237
1238 /* Call the allocation method of the superclass. */
1239 ret = ((struct mips_elf_link_hash_entry *)
1240 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1241 table, string));
1242 if (ret != NULL)
1243 {
1244 /* Set local fields. */
1245 memset (&ret->esym, 0, sizeof (EXTR));
1246 /* We use -2 as a marker to indicate that the information has
1247 not been set. -1 means there is no associated ifd. */
1248 ret->esym.ifd = -2;
1249 ret->la25_stub = 0;
1250 ret->possibly_dynamic_relocs = 0;
1251 ret->fn_stub = NULL;
1252 ret->call_stub = NULL;
1253 ret->call_fp_stub = NULL;
1254 ret->global_got_area = GGA_NONE;
1255 ret->got_only_for_calls = TRUE;
1256 ret->readonly_reloc = FALSE;
1257 ret->has_static_relocs = FALSE;
1258 ret->no_fn_stub = FALSE;
1259 ret->need_fn_stub = FALSE;
1260 ret->has_nonpic_branches = FALSE;
1261 ret->needs_lazy_stub = FALSE;
1262 ret->use_plt_entry = FALSE;
1263 }
1264
1265 return (struct bfd_hash_entry *) ret;
1266 }
1267
1268 /* Allocate MIPS ELF private object data. */
1269
1270 bfd_boolean
1271 _bfd_mips_elf_mkobject (bfd *abfd)
1272 {
1273 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1274 MIPS_ELF_DATA);
1275 }
1276
1277 bfd_boolean
1278 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
1279 {
1280 if (!sec->used_by_bfd)
1281 {
1282 struct _mips_elf_section_data *sdata;
1283 bfd_size_type amt = sizeof (*sdata);
1284
1285 sdata = bfd_zalloc (abfd, amt);
1286 if (sdata == NULL)
1287 return FALSE;
1288 sec->used_by_bfd = sdata;
1289 }
1290
1291 return _bfd_elf_new_section_hook (abfd, sec);
1292 }
1293 \f
1294 /* Read ECOFF debugging information from a .mdebug section into a
1295 ecoff_debug_info structure. */
1296
1297 bfd_boolean
1298 _bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1299 struct ecoff_debug_info *debug)
1300 {
1301 HDRR *symhdr;
1302 const struct ecoff_debug_swap *swap;
1303 char *ext_hdr;
1304
1305 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1306 memset (debug, 0, sizeof (*debug));
1307
1308 ext_hdr = bfd_malloc (swap->external_hdr_size);
1309 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1310 goto error_return;
1311
1312 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
1313 swap->external_hdr_size))
1314 goto error_return;
1315
1316 symhdr = &debug->symbolic_header;
1317 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1318
1319 /* The symbolic header contains absolute file offsets and sizes to
1320 read. */
1321 #define READ(ptr, offset, count, size, type) \
1322 if (symhdr->count == 0) \
1323 debug->ptr = NULL; \
1324 else \
1325 { \
1326 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
1327 debug->ptr = bfd_malloc (amt); \
1328 if (debug->ptr == NULL) \
1329 goto error_return; \
1330 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
1331 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1332 goto error_return; \
1333 }
1334
1335 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
1336 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1337 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1338 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1339 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
1340 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1341 union aux_ext *);
1342 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1343 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
1344 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1345 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1346 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
1347 #undef READ
1348
1349 debug->fdr = NULL;
1350
1351 return TRUE;
1352
1353 error_return:
1354 if (ext_hdr != NULL)
1355 free (ext_hdr);
1356 if (debug->line != NULL)
1357 free (debug->line);
1358 if (debug->external_dnr != NULL)
1359 free (debug->external_dnr);
1360 if (debug->external_pdr != NULL)
1361 free (debug->external_pdr);
1362 if (debug->external_sym != NULL)
1363 free (debug->external_sym);
1364 if (debug->external_opt != NULL)
1365 free (debug->external_opt);
1366 if (debug->external_aux != NULL)
1367 free (debug->external_aux);
1368 if (debug->ss != NULL)
1369 free (debug->ss);
1370 if (debug->ssext != NULL)
1371 free (debug->ssext);
1372 if (debug->external_fdr != NULL)
1373 free (debug->external_fdr);
1374 if (debug->external_rfd != NULL)
1375 free (debug->external_rfd);
1376 if (debug->external_ext != NULL)
1377 free (debug->external_ext);
1378 return FALSE;
1379 }
1380 \f
1381 /* Swap RPDR (runtime procedure table entry) for output. */
1382
1383 static void
1384 ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
1385 {
1386 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1387 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1388 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1389 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1390 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1391 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1392
1393 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1394 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1395
1396 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
1397 }
1398
1399 /* Create a runtime procedure table from the .mdebug section. */
1400
1401 static bfd_boolean
1402 mips_elf_create_procedure_table (void *handle, bfd *abfd,
1403 struct bfd_link_info *info, asection *s,
1404 struct ecoff_debug_info *debug)
1405 {
1406 const struct ecoff_debug_swap *swap;
1407 HDRR *hdr = &debug->symbolic_header;
1408 RPDR *rpdr, *rp;
1409 struct rpdr_ext *erp;
1410 void *rtproc;
1411 struct pdr_ext *epdr;
1412 struct sym_ext *esym;
1413 char *ss, **sv;
1414 char *str;
1415 bfd_size_type size;
1416 bfd_size_type count;
1417 unsigned long sindex;
1418 unsigned long i;
1419 PDR pdr;
1420 SYMR sym;
1421 const char *no_name_func = _("static procedure (no name)");
1422
1423 epdr = NULL;
1424 rpdr = NULL;
1425 esym = NULL;
1426 ss = NULL;
1427 sv = NULL;
1428
1429 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1430
1431 sindex = strlen (no_name_func) + 1;
1432 count = hdr->ipdMax;
1433 if (count > 0)
1434 {
1435 size = swap->external_pdr_size;
1436
1437 epdr = bfd_malloc (size * count);
1438 if (epdr == NULL)
1439 goto error_return;
1440
1441 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
1442 goto error_return;
1443
1444 size = sizeof (RPDR);
1445 rp = rpdr = bfd_malloc (size * count);
1446 if (rpdr == NULL)
1447 goto error_return;
1448
1449 size = sizeof (char *);
1450 sv = bfd_malloc (size * count);
1451 if (sv == NULL)
1452 goto error_return;
1453
1454 count = hdr->isymMax;
1455 size = swap->external_sym_size;
1456 esym = bfd_malloc (size * count);
1457 if (esym == NULL)
1458 goto error_return;
1459
1460 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
1461 goto error_return;
1462
1463 count = hdr->issMax;
1464 ss = bfd_malloc (count);
1465 if (ss == NULL)
1466 goto error_return;
1467 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
1468 goto error_return;
1469
1470 count = hdr->ipdMax;
1471 for (i = 0; i < (unsigned long) count; i++, rp++)
1472 {
1473 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1474 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
1475 rp->adr = sym.value;
1476 rp->regmask = pdr.regmask;
1477 rp->regoffset = pdr.regoffset;
1478 rp->fregmask = pdr.fregmask;
1479 rp->fregoffset = pdr.fregoffset;
1480 rp->frameoffset = pdr.frameoffset;
1481 rp->framereg = pdr.framereg;
1482 rp->pcreg = pdr.pcreg;
1483 rp->irpss = sindex;
1484 sv[i] = ss + sym.iss;
1485 sindex += strlen (sv[i]) + 1;
1486 }
1487 }
1488
1489 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1490 size = BFD_ALIGN (size, 16);
1491 rtproc = bfd_alloc (abfd, size);
1492 if (rtproc == NULL)
1493 {
1494 mips_elf_hash_table (info)->procedure_count = 0;
1495 goto error_return;
1496 }
1497
1498 mips_elf_hash_table (info)->procedure_count = count + 2;
1499
1500 erp = rtproc;
1501 memset (erp, 0, sizeof (struct rpdr_ext));
1502 erp++;
1503 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1504 strcpy (str, no_name_func);
1505 str += strlen (no_name_func) + 1;
1506 for (i = 0; i < count; i++)
1507 {
1508 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1509 strcpy (str, sv[i]);
1510 str += strlen (sv[i]) + 1;
1511 }
1512 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1513
1514 /* Set the size and contents of .rtproc section. */
1515 s->size = size;
1516 s->contents = rtproc;
1517
1518 /* Skip this section later on (I don't think this currently
1519 matters, but someday it might). */
1520 s->map_head.link_order = NULL;
1521
1522 if (epdr != NULL)
1523 free (epdr);
1524 if (rpdr != NULL)
1525 free (rpdr);
1526 if (esym != NULL)
1527 free (esym);
1528 if (ss != NULL)
1529 free (ss);
1530 if (sv != NULL)
1531 free (sv);
1532
1533 return TRUE;
1534
1535 error_return:
1536 if (epdr != NULL)
1537 free (epdr);
1538 if (rpdr != NULL)
1539 free (rpdr);
1540 if (esym != NULL)
1541 free (esym);
1542 if (ss != NULL)
1543 free (ss);
1544 if (sv != NULL)
1545 free (sv);
1546 return FALSE;
1547 }
1548 \f
1549 /* We're going to create a stub for H. Create a symbol for the stub's
1550 value and size, to help make the disassembly easier to read. */
1551
1552 static bfd_boolean
1553 mips_elf_create_stub_symbol (struct bfd_link_info *info,
1554 struct mips_elf_link_hash_entry *h,
1555 const char *prefix, asection *s, bfd_vma value,
1556 bfd_vma size)
1557 {
1558 struct bfd_link_hash_entry *bh;
1559 struct elf_link_hash_entry *elfh;
1560 const char *name;
1561
1562 if (ELF_ST_IS_MICROMIPS (h->root.other))
1563 value |= 1;
1564
1565 /* Create a new symbol. */
1566 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1567 bh = NULL;
1568 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1569 BSF_LOCAL, s, value, NULL,
1570 TRUE, FALSE, &bh))
1571 return FALSE;
1572
1573 /* Make it a local function. */
1574 elfh = (struct elf_link_hash_entry *) bh;
1575 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1576 elfh->size = size;
1577 elfh->forced_local = 1;
1578 return TRUE;
1579 }
1580
1581 /* We're about to redefine H. Create a symbol to represent H's
1582 current value and size, to help make the disassembly easier
1583 to read. */
1584
1585 static bfd_boolean
1586 mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1587 struct mips_elf_link_hash_entry *h,
1588 const char *prefix)
1589 {
1590 struct bfd_link_hash_entry *bh;
1591 struct elf_link_hash_entry *elfh;
1592 const char *name;
1593 asection *s;
1594 bfd_vma value;
1595
1596 /* Read the symbol's value. */
1597 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1598 || h->root.root.type == bfd_link_hash_defweak);
1599 s = h->root.root.u.def.section;
1600 value = h->root.root.u.def.value;
1601
1602 /* Create a new symbol. */
1603 name = ACONCAT ((prefix, h->root.root.root.string, NULL));
1604 bh = NULL;
1605 if (!_bfd_generic_link_add_one_symbol (info, s->owner, name,
1606 BSF_LOCAL, s, value, NULL,
1607 TRUE, FALSE, &bh))
1608 return FALSE;
1609
1610 /* Make it local and copy the other attributes from H. */
1611 elfh = (struct elf_link_hash_entry *) bh;
1612 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1613 elfh->other = h->root.other;
1614 elfh->size = h->root.size;
1615 elfh->forced_local = 1;
1616 return TRUE;
1617 }
1618
1619 /* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1620 function rather than to a hard-float stub. */
1621
1622 static bfd_boolean
1623 section_allows_mips16_refs_p (asection *section)
1624 {
1625 const char *name;
1626
1627 name = bfd_get_section_name (section->owner, section);
1628 return (FN_STUB_P (name)
1629 || CALL_STUB_P (name)
1630 || CALL_FP_STUB_P (name)
1631 || strcmp (name, ".pdr") == 0);
1632 }
1633
1634 /* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1635 stub section of some kind. Return the R_SYMNDX of the target
1636 function, or 0 if we can't decide which function that is. */
1637
1638 static unsigned long
1639 mips16_stub_symndx (const struct elf_backend_data *bed,
1640 asection *sec ATTRIBUTE_UNUSED,
1641 const Elf_Internal_Rela *relocs,
1642 const Elf_Internal_Rela *relend)
1643 {
1644 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
1645 const Elf_Internal_Rela *rel;
1646
1647 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1648 one in a compound relocation. */
1649 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
1650 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1651 return ELF_R_SYM (sec->owner, rel->r_info);
1652
1653 /* Otherwise trust the first relocation, whatever its kind. This is
1654 the traditional behavior. */
1655 if (relocs < relend)
1656 return ELF_R_SYM (sec->owner, relocs->r_info);
1657
1658 return 0;
1659 }
1660
1661 /* Check the mips16 stubs for a particular symbol, and see if we can
1662 discard them. */
1663
1664 static void
1665 mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1666 struct mips_elf_link_hash_entry *h)
1667 {
1668 /* Dynamic symbols must use the standard call interface, in case other
1669 objects try to call them. */
1670 if (h->fn_stub != NULL
1671 && h->root.dynindx != -1)
1672 {
1673 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1674 h->need_fn_stub = TRUE;
1675 }
1676
1677 if (h->fn_stub != NULL
1678 && ! h->need_fn_stub)
1679 {
1680 /* We don't need the fn_stub; the only references to this symbol
1681 are 16 bit calls. Clobber the size to 0 to prevent it from
1682 being included in the link. */
1683 h->fn_stub->size = 0;
1684 h->fn_stub->flags &= ~SEC_RELOC;
1685 h->fn_stub->reloc_count = 0;
1686 h->fn_stub->flags |= SEC_EXCLUDE;
1687 }
1688
1689 if (h->call_stub != NULL
1690 && ELF_ST_IS_MIPS16 (h->root.other))
1691 {
1692 /* We don't need the call_stub; this is a 16 bit function, so
1693 calls from other 16 bit functions are OK. Clobber the size
1694 to 0 to prevent it from being included in the link. */
1695 h->call_stub->size = 0;
1696 h->call_stub->flags &= ~SEC_RELOC;
1697 h->call_stub->reloc_count = 0;
1698 h->call_stub->flags |= SEC_EXCLUDE;
1699 }
1700
1701 if (h->call_fp_stub != NULL
1702 && ELF_ST_IS_MIPS16 (h->root.other))
1703 {
1704 /* We don't need the call_stub; this is a 16 bit function, so
1705 calls from other 16 bit functions are OK. Clobber the size
1706 to 0 to prevent it from being included in the link. */
1707 h->call_fp_stub->size = 0;
1708 h->call_fp_stub->flags &= ~SEC_RELOC;
1709 h->call_fp_stub->reloc_count = 0;
1710 h->call_fp_stub->flags |= SEC_EXCLUDE;
1711 }
1712 }
1713
1714 /* Hashtable callbacks for mips_elf_la25_stubs. */
1715
1716 static hashval_t
1717 mips_elf_la25_stub_hash (const void *entry_)
1718 {
1719 const struct mips_elf_la25_stub *entry;
1720
1721 entry = (struct mips_elf_la25_stub *) entry_;
1722 return entry->h->root.root.u.def.section->id
1723 + entry->h->root.root.u.def.value;
1724 }
1725
1726 static int
1727 mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1728 {
1729 const struct mips_elf_la25_stub *entry1, *entry2;
1730
1731 entry1 = (struct mips_elf_la25_stub *) entry1_;
1732 entry2 = (struct mips_elf_la25_stub *) entry2_;
1733 return ((entry1->h->root.root.u.def.section
1734 == entry2->h->root.root.u.def.section)
1735 && (entry1->h->root.root.u.def.value
1736 == entry2->h->root.root.u.def.value));
1737 }
1738
1739 /* Called by the linker to set up the la25 stub-creation code. FN is
1740 the linker's implementation of add_stub_function. Return true on
1741 success. */
1742
1743 bfd_boolean
1744 _bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1745 asection *(*fn) (const char *, asection *,
1746 asection *))
1747 {
1748 struct mips_elf_link_hash_table *htab;
1749
1750 htab = mips_elf_hash_table (info);
1751 if (htab == NULL)
1752 return FALSE;
1753
1754 htab->add_stub_section = fn;
1755 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1756 mips_elf_la25_stub_eq, NULL);
1757 if (htab->la25_stubs == NULL)
1758 return FALSE;
1759
1760 return TRUE;
1761 }
1762
1763 /* Return true if H is a locally-defined PIC function, in the sense
1764 that it or its fn_stub might need $25 to be valid on entry.
1765 Note that MIPS16 functions set up $gp using PC-relative instructions,
1766 so they themselves never need $25 to be valid. Only non-MIPS16
1767 entry points are of interest here. */
1768
1769 static bfd_boolean
1770 mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1771 {
1772 return ((h->root.root.type == bfd_link_hash_defined
1773 || h->root.root.type == bfd_link_hash_defweak)
1774 && h->root.def_regular
1775 && !bfd_is_abs_section (h->root.root.u.def.section)
1776 && (!ELF_ST_IS_MIPS16 (h->root.other)
1777 || (h->fn_stub && h->need_fn_stub))
1778 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1779 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1780 }
1781
1782 /* Set *SEC to the input section that contains the target of STUB.
1783 Return the offset of the target from the start of that section. */
1784
1785 static bfd_vma
1786 mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1787 asection **sec)
1788 {
1789 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1790 {
1791 BFD_ASSERT (stub->h->need_fn_stub);
1792 *sec = stub->h->fn_stub;
1793 return 0;
1794 }
1795 else
1796 {
1797 *sec = stub->h->root.root.u.def.section;
1798 return stub->h->root.root.u.def.value;
1799 }
1800 }
1801
1802 /* STUB describes an la25 stub that we have decided to implement
1803 by inserting an LUI/ADDIU pair before the target function.
1804 Create the section and redirect the function symbol to it. */
1805
1806 static bfd_boolean
1807 mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1808 struct bfd_link_info *info)
1809 {
1810 struct mips_elf_link_hash_table *htab;
1811 char *name;
1812 asection *s, *input_section;
1813 unsigned int align;
1814
1815 htab = mips_elf_hash_table (info);
1816 if (htab == NULL)
1817 return FALSE;
1818
1819 /* Create a unique name for the new section. */
1820 name = bfd_malloc (11 + sizeof (".text.stub."));
1821 if (name == NULL)
1822 return FALSE;
1823 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1824
1825 /* Create the section. */
1826 mips_elf_get_la25_target (stub, &input_section);
1827 s = htab->add_stub_section (name, input_section,
1828 input_section->output_section);
1829 if (s == NULL)
1830 return FALSE;
1831
1832 /* Make sure that any padding goes before the stub. */
1833 align = input_section->alignment_power;
1834 if (!bfd_set_section_alignment (s->owner, s, align))
1835 return FALSE;
1836 if (align > 3)
1837 s->size = (1 << align) - 8;
1838
1839 /* Create a symbol for the stub. */
1840 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1841 stub->stub_section = s;
1842 stub->offset = s->size;
1843
1844 /* Allocate room for it. */
1845 s->size += 8;
1846 return TRUE;
1847 }
1848
1849 /* STUB describes an la25 stub that we have decided to implement
1850 with a separate trampoline. Allocate room for it and redirect
1851 the function symbol to it. */
1852
1853 static bfd_boolean
1854 mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1855 struct bfd_link_info *info)
1856 {
1857 struct mips_elf_link_hash_table *htab;
1858 asection *s;
1859
1860 htab = mips_elf_hash_table (info);
1861 if (htab == NULL)
1862 return FALSE;
1863
1864 /* Create a trampoline section, if we haven't already. */
1865 s = htab->strampoline;
1866 if (s == NULL)
1867 {
1868 asection *input_section = stub->h->root.root.u.def.section;
1869 s = htab->add_stub_section (".text", NULL,
1870 input_section->output_section);
1871 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1872 return FALSE;
1873 htab->strampoline = s;
1874 }
1875
1876 /* Create a symbol for the stub. */
1877 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1878 stub->stub_section = s;
1879 stub->offset = s->size;
1880
1881 /* Allocate room for it. */
1882 s->size += 16;
1883 return TRUE;
1884 }
1885
1886 /* H describes a symbol that needs an la25 stub. Make sure that an
1887 appropriate stub exists and point H at it. */
1888
1889 static bfd_boolean
1890 mips_elf_add_la25_stub (struct bfd_link_info *info,
1891 struct mips_elf_link_hash_entry *h)
1892 {
1893 struct mips_elf_link_hash_table *htab;
1894 struct mips_elf_la25_stub search, *stub;
1895 bfd_boolean use_trampoline_p;
1896 asection *s;
1897 bfd_vma value;
1898 void **slot;
1899
1900 /* Describe the stub we want. */
1901 search.stub_section = NULL;
1902 search.offset = 0;
1903 search.h = h;
1904
1905 /* See if we've already created an equivalent stub. */
1906 htab = mips_elf_hash_table (info);
1907 if (htab == NULL)
1908 return FALSE;
1909
1910 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1911 if (slot == NULL)
1912 return FALSE;
1913
1914 stub = (struct mips_elf_la25_stub *) *slot;
1915 if (stub != NULL)
1916 {
1917 /* We can reuse the existing stub. */
1918 h->la25_stub = stub;
1919 return TRUE;
1920 }
1921
1922 /* Create a permanent copy of ENTRY and add it to the hash table. */
1923 stub = bfd_malloc (sizeof (search));
1924 if (stub == NULL)
1925 return FALSE;
1926 *stub = search;
1927 *slot = stub;
1928
1929 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1930 of the section and if we would need no more than 2 nops. */
1931 value = mips_elf_get_la25_target (stub, &s);
1932 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1933
1934 h->la25_stub = stub;
1935 return (use_trampoline_p
1936 ? mips_elf_add_la25_trampoline (stub, info)
1937 : mips_elf_add_la25_intro (stub, info));
1938 }
1939
1940 /* A mips_elf_link_hash_traverse callback that is called before sizing
1941 sections. DATA points to a mips_htab_traverse_info structure. */
1942
1943 static bfd_boolean
1944 mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1945 {
1946 struct mips_htab_traverse_info *hti;
1947
1948 hti = (struct mips_htab_traverse_info *) data;
1949 if (!hti->info->relocatable)
1950 mips_elf_check_mips16_stubs (hti->info, h);
1951
1952 if (mips_elf_local_pic_function_p (h))
1953 {
1954 /* PR 12845: If H is in a section that has been garbage
1955 collected it will have its output section set to *ABS*. */
1956 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
1957 return TRUE;
1958
1959 /* H is a function that might need $25 to be valid on entry.
1960 If we're creating a non-PIC relocatable object, mark H as
1961 being PIC. If we're creating a non-relocatable object with
1962 non-PIC branches and jumps to H, make sure that H has an la25
1963 stub. */
1964 if (hti->info->relocatable)
1965 {
1966 if (!PIC_OBJECT_P (hti->output_bfd))
1967 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
1968 }
1969 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
1970 {
1971 hti->error = TRUE;
1972 return FALSE;
1973 }
1974 }
1975 return TRUE;
1976 }
1977 \f
1978 /* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
1979 Most mips16 instructions are 16 bits, but these instructions
1980 are 32 bits.
1981
1982 The format of these instructions is:
1983
1984 +--------------+--------------------------------+
1985 | JALX | X| Imm 20:16 | Imm 25:21 |
1986 +--------------+--------------------------------+
1987 | Immediate 15:0 |
1988 +-----------------------------------------------+
1989
1990 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
1991 Note that the immediate value in the first word is swapped.
1992
1993 When producing a relocatable object file, R_MIPS16_26 is
1994 handled mostly like R_MIPS_26. In particular, the addend is
1995 stored as a straight 26-bit value in a 32-bit instruction.
1996 (gas makes life simpler for itself by never adjusting a
1997 R_MIPS16_26 reloc to be against a section, so the addend is
1998 always zero). However, the 32 bit instruction is stored as 2
1999 16-bit values, rather than a single 32-bit value. In a
2000 big-endian file, the result is the same; in a little-endian
2001 file, the two 16-bit halves of the 32 bit value are swapped.
2002 This is so that a disassembler can recognize the jal
2003 instruction.
2004
2005 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2006 instruction stored as two 16-bit values. The addend A is the
2007 contents of the targ26 field. The calculation is the same as
2008 R_MIPS_26. When storing the calculated value, reorder the
2009 immediate value as shown above, and don't forget to store the
2010 value as two 16-bit values.
2011
2012 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2013 defined as
2014
2015 big-endian:
2016 +--------+----------------------+
2017 | | |
2018 | | targ26-16 |
2019 |31 26|25 0|
2020 +--------+----------------------+
2021
2022 little-endian:
2023 +----------+------+-------------+
2024 | | | |
2025 | sub1 | | sub2 |
2026 |0 9|10 15|16 31|
2027 +----------+--------------------+
2028 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2029 ((sub1 << 16) | sub2)).
2030
2031 When producing a relocatable object file, the calculation is
2032 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2033 When producing a fully linked file, the calculation is
2034 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2035 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2036
2037 The table below lists the other MIPS16 instruction relocations.
2038 Each one is calculated in the same way as the non-MIPS16 relocation
2039 given on the right, but using the extended MIPS16 layout of 16-bit
2040 immediate fields:
2041
2042 R_MIPS16_GPREL R_MIPS_GPREL16
2043 R_MIPS16_GOT16 R_MIPS_GOT16
2044 R_MIPS16_CALL16 R_MIPS_CALL16
2045 R_MIPS16_HI16 R_MIPS_HI16
2046 R_MIPS16_LO16 R_MIPS_LO16
2047
2048 A typical instruction will have a format like this:
2049
2050 +--------------+--------------------------------+
2051 | EXTEND | Imm 10:5 | Imm 15:11 |
2052 +--------------+--------------------------------+
2053 | Major | rx | ry | Imm 4:0 |
2054 +--------------+--------------------------------+
2055
2056 EXTEND is the five bit value 11110. Major is the instruction
2057 opcode.
2058
2059 All we need to do here is shuffle the bits appropriately.
2060 As above, the two 16-bit halves must be swapped on a
2061 little-endian system. */
2062
2063 static inline bfd_boolean
2064 mips16_reloc_p (int r_type)
2065 {
2066 switch (r_type)
2067 {
2068 case R_MIPS16_26:
2069 case R_MIPS16_GPREL:
2070 case R_MIPS16_GOT16:
2071 case R_MIPS16_CALL16:
2072 case R_MIPS16_HI16:
2073 case R_MIPS16_LO16:
2074 case R_MIPS16_TLS_GD:
2075 case R_MIPS16_TLS_LDM:
2076 case R_MIPS16_TLS_DTPREL_HI16:
2077 case R_MIPS16_TLS_DTPREL_LO16:
2078 case R_MIPS16_TLS_GOTTPREL:
2079 case R_MIPS16_TLS_TPREL_HI16:
2080 case R_MIPS16_TLS_TPREL_LO16:
2081 return TRUE;
2082
2083 default:
2084 return FALSE;
2085 }
2086 }
2087
2088 /* Check if a microMIPS reloc. */
2089
2090 static inline bfd_boolean
2091 micromips_reloc_p (unsigned int r_type)
2092 {
2093 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2094 }
2095
2096 /* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2097 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2098 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2099
2100 static inline bfd_boolean
2101 micromips_reloc_shuffle_p (unsigned int r_type)
2102 {
2103 return (micromips_reloc_p (r_type)
2104 && r_type != R_MICROMIPS_PC7_S1
2105 && r_type != R_MICROMIPS_PC10_S1);
2106 }
2107
2108 static inline bfd_boolean
2109 got16_reloc_p (int r_type)
2110 {
2111 return (r_type == R_MIPS_GOT16
2112 || r_type == R_MIPS16_GOT16
2113 || r_type == R_MICROMIPS_GOT16);
2114 }
2115
2116 static inline bfd_boolean
2117 call16_reloc_p (int r_type)
2118 {
2119 return (r_type == R_MIPS_CALL16
2120 || r_type == R_MIPS16_CALL16
2121 || r_type == R_MICROMIPS_CALL16);
2122 }
2123
2124 static inline bfd_boolean
2125 got_disp_reloc_p (unsigned int r_type)
2126 {
2127 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2128 }
2129
2130 static inline bfd_boolean
2131 got_page_reloc_p (unsigned int r_type)
2132 {
2133 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2134 }
2135
2136 static inline bfd_boolean
2137 got_ofst_reloc_p (unsigned int r_type)
2138 {
2139 return r_type == R_MIPS_GOT_OFST || r_type == R_MICROMIPS_GOT_OFST;
2140 }
2141
2142 static inline bfd_boolean
2143 got_hi16_reloc_p (unsigned int r_type)
2144 {
2145 return r_type == R_MIPS_GOT_HI16 || r_type == R_MICROMIPS_GOT_HI16;
2146 }
2147
2148 static inline bfd_boolean
2149 got_lo16_reloc_p (unsigned int r_type)
2150 {
2151 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2152 }
2153
2154 static inline bfd_boolean
2155 call_hi16_reloc_p (unsigned int r_type)
2156 {
2157 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2158 }
2159
2160 static inline bfd_boolean
2161 call_lo16_reloc_p (unsigned int r_type)
2162 {
2163 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
2164 }
2165
2166 static inline bfd_boolean
2167 hi16_reloc_p (int r_type)
2168 {
2169 return (r_type == R_MIPS_HI16
2170 || r_type == R_MIPS16_HI16
2171 || r_type == R_MICROMIPS_HI16);
2172 }
2173
2174 static inline bfd_boolean
2175 lo16_reloc_p (int r_type)
2176 {
2177 return (r_type == R_MIPS_LO16
2178 || r_type == R_MIPS16_LO16
2179 || r_type == R_MICROMIPS_LO16);
2180 }
2181
2182 static inline bfd_boolean
2183 mips16_call_reloc_p (int r_type)
2184 {
2185 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2186 }
2187
2188 static inline bfd_boolean
2189 jal_reloc_p (int r_type)
2190 {
2191 return (r_type == R_MIPS_26
2192 || r_type == R_MIPS16_26
2193 || r_type == R_MICROMIPS_26_S1);
2194 }
2195
2196 static inline bfd_boolean
2197 micromips_branch_reloc_p (int r_type)
2198 {
2199 return (r_type == R_MICROMIPS_26_S1
2200 || r_type == R_MICROMIPS_PC16_S1
2201 || r_type == R_MICROMIPS_PC10_S1
2202 || r_type == R_MICROMIPS_PC7_S1);
2203 }
2204
2205 static inline bfd_boolean
2206 tls_gd_reloc_p (unsigned int r_type)
2207 {
2208 return (r_type == R_MIPS_TLS_GD
2209 || r_type == R_MIPS16_TLS_GD
2210 || r_type == R_MICROMIPS_TLS_GD);
2211 }
2212
2213 static inline bfd_boolean
2214 tls_ldm_reloc_p (unsigned int r_type)
2215 {
2216 return (r_type == R_MIPS_TLS_LDM
2217 || r_type == R_MIPS16_TLS_LDM
2218 || r_type == R_MICROMIPS_TLS_LDM);
2219 }
2220
2221 static inline bfd_boolean
2222 tls_gottprel_reloc_p (unsigned int r_type)
2223 {
2224 return (r_type == R_MIPS_TLS_GOTTPREL
2225 || r_type == R_MIPS16_TLS_GOTTPREL
2226 || r_type == R_MICROMIPS_TLS_GOTTPREL);
2227 }
2228
2229 void
2230 _bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2231 bfd_boolean jal_shuffle, bfd_byte *data)
2232 {
2233 bfd_vma first, second, val;
2234
2235 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2236 return;
2237
2238 /* Pick up the first and second halfwords of the instruction. */
2239 first = bfd_get_16 (abfd, data);
2240 second = bfd_get_16 (abfd, data + 2);
2241 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2242 val = first << 16 | second;
2243 else if (r_type != R_MIPS16_26)
2244 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2245 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
2246 else
2247 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2248 | ((first & 0x1f) << 21) | second);
2249 bfd_put_32 (abfd, val, data);
2250 }
2251
2252 void
2253 _bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2254 bfd_boolean jal_shuffle, bfd_byte *data)
2255 {
2256 bfd_vma first, second, val;
2257
2258 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
2259 return;
2260
2261 val = bfd_get_32 (abfd, data);
2262 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2263 {
2264 second = val & 0xffff;
2265 first = val >> 16;
2266 }
2267 else if (r_type != R_MIPS16_26)
2268 {
2269 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2270 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
2271 }
2272 else
2273 {
2274 second = val & 0xffff;
2275 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2276 | ((val >> 21) & 0x1f);
2277 }
2278 bfd_put_16 (abfd, second, data + 2);
2279 bfd_put_16 (abfd, first, data);
2280 }
2281
2282 bfd_reloc_status_type
2283 _bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2284 arelent *reloc_entry, asection *input_section,
2285 bfd_boolean relocatable, void *data, bfd_vma gp)
2286 {
2287 bfd_vma relocation;
2288 bfd_signed_vma val;
2289 bfd_reloc_status_type status;
2290
2291 if (bfd_is_com_section (symbol->section))
2292 relocation = 0;
2293 else
2294 relocation = symbol->value;
2295
2296 relocation += symbol->section->output_section->vma;
2297 relocation += symbol->section->output_offset;
2298
2299 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2300 return bfd_reloc_outofrange;
2301
2302 /* Set val to the offset into the section or symbol. */
2303 val = reloc_entry->addend;
2304
2305 _bfd_mips_elf_sign_extend (val, 16);
2306
2307 /* Adjust val for the final section location and GP value. If we
2308 are producing relocatable output, we don't want to do this for
2309 an external symbol. */
2310 if (! relocatable
2311 || (symbol->flags & BSF_SECTION_SYM) != 0)
2312 val += relocation - gp;
2313
2314 if (reloc_entry->howto->partial_inplace)
2315 {
2316 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2317 (bfd_byte *) data
2318 + reloc_entry->address);
2319 if (status != bfd_reloc_ok)
2320 return status;
2321 }
2322 else
2323 reloc_entry->addend = val;
2324
2325 if (relocatable)
2326 reloc_entry->address += input_section->output_offset;
2327
2328 return bfd_reloc_ok;
2329 }
2330
2331 /* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2332 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2333 that contains the relocation field and DATA points to the start of
2334 INPUT_SECTION. */
2335
2336 struct mips_hi16
2337 {
2338 struct mips_hi16 *next;
2339 bfd_byte *data;
2340 asection *input_section;
2341 arelent rel;
2342 };
2343
2344 /* FIXME: This should not be a static variable. */
2345
2346 static struct mips_hi16 *mips_hi16_list;
2347
2348 /* A howto special_function for REL *HI16 relocations. We can only
2349 calculate the correct value once we've seen the partnering
2350 *LO16 relocation, so just save the information for later.
2351
2352 The ABI requires that the *LO16 immediately follow the *HI16.
2353 However, as a GNU extension, we permit an arbitrary number of
2354 *HI16s to be associated with a single *LO16. This significantly
2355 simplies the relocation handling in gcc. */
2356
2357 bfd_reloc_status_type
2358 _bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2359 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2360 asection *input_section, bfd *output_bfd,
2361 char **error_message ATTRIBUTE_UNUSED)
2362 {
2363 struct mips_hi16 *n;
2364
2365 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2366 return bfd_reloc_outofrange;
2367
2368 n = bfd_malloc (sizeof *n);
2369 if (n == NULL)
2370 return bfd_reloc_outofrange;
2371
2372 n->next = mips_hi16_list;
2373 n->data = data;
2374 n->input_section = input_section;
2375 n->rel = *reloc_entry;
2376 mips_hi16_list = n;
2377
2378 if (output_bfd != NULL)
2379 reloc_entry->address += input_section->output_offset;
2380
2381 return bfd_reloc_ok;
2382 }
2383
2384 /* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
2385 like any other 16-bit relocation when applied to global symbols, but is
2386 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2387
2388 bfd_reloc_status_type
2389 _bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2390 void *data, asection *input_section,
2391 bfd *output_bfd, char **error_message)
2392 {
2393 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2394 || bfd_is_und_section (bfd_get_section (symbol))
2395 || bfd_is_com_section (bfd_get_section (symbol)))
2396 /* The relocation is against a global symbol. */
2397 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2398 input_section, output_bfd,
2399 error_message);
2400
2401 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2402 input_section, output_bfd, error_message);
2403 }
2404
2405 /* A howto special_function for REL *LO16 relocations. The *LO16 itself
2406 is a straightforward 16 bit inplace relocation, but we must deal with
2407 any partnering high-part relocations as well. */
2408
2409 bfd_reloc_status_type
2410 _bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2411 void *data, asection *input_section,
2412 bfd *output_bfd, char **error_message)
2413 {
2414 bfd_vma vallo;
2415 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2416
2417 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2418 return bfd_reloc_outofrange;
2419
2420 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2421 location);
2422 vallo = bfd_get_32 (abfd, location);
2423 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2424 location);
2425
2426 while (mips_hi16_list != NULL)
2427 {
2428 bfd_reloc_status_type ret;
2429 struct mips_hi16 *hi;
2430
2431 hi = mips_hi16_list;
2432
2433 /* R_MIPS*_GOT16 relocations are something of a special case. We
2434 want to install the addend in the same way as for a R_MIPS*_HI16
2435 relocation (with a rightshift of 16). However, since GOT16
2436 relocations can also be used with global symbols, their howto
2437 has a rightshift of 0. */
2438 if (hi->rel.howto->type == R_MIPS_GOT16)
2439 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
2440 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2441 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
2442 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2443 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
2444
2445 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2446 carry or borrow will induce a change of +1 or -1 in the high part. */
2447 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2448
2449 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2450 hi->input_section, output_bfd,
2451 error_message);
2452 if (ret != bfd_reloc_ok)
2453 return ret;
2454
2455 mips_hi16_list = hi->next;
2456 free (hi);
2457 }
2458
2459 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2460 input_section, output_bfd,
2461 error_message);
2462 }
2463
2464 /* A generic howto special_function. This calculates and installs the
2465 relocation itself, thus avoiding the oft-discussed problems in
2466 bfd_perform_relocation and bfd_install_relocation. */
2467
2468 bfd_reloc_status_type
2469 _bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2470 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2471 asection *input_section, bfd *output_bfd,
2472 char **error_message ATTRIBUTE_UNUSED)
2473 {
2474 bfd_signed_vma val;
2475 bfd_reloc_status_type status;
2476 bfd_boolean relocatable;
2477
2478 relocatable = (output_bfd != NULL);
2479
2480 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2481 return bfd_reloc_outofrange;
2482
2483 /* Build up the field adjustment in VAL. */
2484 val = 0;
2485 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2486 {
2487 /* Either we're calculating the final field value or we have a
2488 relocation against a section symbol. Add in the section's
2489 offset or address. */
2490 val += symbol->section->output_section->vma;
2491 val += symbol->section->output_offset;
2492 }
2493
2494 if (!relocatable)
2495 {
2496 /* We're calculating the final field value. Add in the symbol's value
2497 and, if pc-relative, subtract the address of the field itself. */
2498 val += symbol->value;
2499 if (reloc_entry->howto->pc_relative)
2500 {
2501 val -= input_section->output_section->vma;
2502 val -= input_section->output_offset;
2503 val -= reloc_entry->address;
2504 }
2505 }
2506
2507 /* VAL is now the final adjustment. If we're keeping this relocation
2508 in the output file, and if the relocation uses a separate addend,
2509 we just need to add VAL to that addend. Otherwise we need to add
2510 VAL to the relocation field itself. */
2511 if (relocatable && !reloc_entry->howto->partial_inplace)
2512 reloc_entry->addend += val;
2513 else
2514 {
2515 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2516
2517 /* Add in the separate addend, if any. */
2518 val += reloc_entry->addend;
2519
2520 /* Add VAL to the relocation field. */
2521 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2522 location);
2523 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2524 location);
2525 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2526 location);
2527
2528 if (status != bfd_reloc_ok)
2529 return status;
2530 }
2531
2532 if (relocatable)
2533 reloc_entry->address += input_section->output_offset;
2534
2535 return bfd_reloc_ok;
2536 }
2537 \f
2538 /* Swap an entry in a .gptab section. Note that these routines rely
2539 on the equivalence of the two elements of the union. */
2540
2541 static void
2542 bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2543 Elf32_gptab *in)
2544 {
2545 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2546 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2547 }
2548
2549 static void
2550 bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2551 Elf32_External_gptab *ex)
2552 {
2553 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2554 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2555 }
2556
2557 static void
2558 bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2559 Elf32_External_compact_rel *ex)
2560 {
2561 H_PUT_32 (abfd, in->id1, ex->id1);
2562 H_PUT_32 (abfd, in->num, ex->num);
2563 H_PUT_32 (abfd, in->id2, ex->id2);
2564 H_PUT_32 (abfd, in->offset, ex->offset);
2565 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2566 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2567 }
2568
2569 static void
2570 bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2571 Elf32_External_crinfo *ex)
2572 {
2573 unsigned long l;
2574
2575 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2576 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2577 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2578 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2579 H_PUT_32 (abfd, l, ex->info);
2580 H_PUT_32 (abfd, in->konst, ex->konst);
2581 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2582 }
2583 \f
2584 /* A .reginfo section holds a single Elf32_RegInfo structure. These
2585 routines swap this structure in and out. They are used outside of
2586 BFD, so they are globally visible. */
2587
2588 void
2589 bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2590 Elf32_RegInfo *in)
2591 {
2592 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2593 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2594 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2595 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2596 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2597 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2598 }
2599
2600 void
2601 bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2602 Elf32_External_RegInfo *ex)
2603 {
2604 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2605 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2606 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2607 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2608 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2609 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2610 }
2611
2612 /* In the 64 bit ABI, the .MIPS.options section holds register
2613 information in an Elf64_Reginfo structure. These routines swap
2614 them in and out. They are globally visible because they are used
2615 outside of BFD. These routines are here so that gas can call them
2616 without worrying about whether the 64 bit ABI has been included. */
2617
2618 void
2619 bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2620 Elf64_Internal_RegInfo *in)
2621 {
2622 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2623 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2624 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2625 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2626 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2627 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2628 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2629 }
2630
2631 void
2632 bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2633 Elf64_External_RegInfo *ex)
2634 {
2635 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2636 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2637 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2638 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2639 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2640 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2641 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2642 }
2643
2644 /* Swap in an options header. */
2645
2646 void
2647 bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2648 Elf_Internal_Options *in)
2649 {
2650 in->kind = H_GET_8 (abfd, ex->kind);
2651 in->size = H_GET_8 (abfd, ex->size);
2652 in->section = H_GET_16 (abfd, ex->section);
2653 in->info = H_GET_32 (abfd, ex->info);
2654 }
2655
2656 /* Swap out an options header. */
2657
2658 void
2659 bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2660 Elf_External_Options *ex)
2661 {
2662 H_PUT_8 (abfd, in->kind, ex->kind);
2663 H_PUT_8 (abfd, in->size, ex->size);
2664 H_PUT_16 (abfd, in->section, ex->section);
2665 H_PUT_32 (abfd, in->info, ex->info);
2666 }
2667 \f
2668 /* This function is called via qsort() to sort the dynamic relocation
2669 entries by increasing r_symndx value. */
2670
2671 static int
2672 sort_dynamic_relocs (const void *arg1, const void *arg2)
2673 {
2674 Elf_Internal_Rela int_reloc1;
2675 Elf_Internal_Rela int_reloc2;
2676 int diff;
2677
2678 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2679 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
2680
2681 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2682 if (diff != 0)
2683 return diff;
2684
2685 if (int_reloc1.r_offset < int_reloc2.r_offset)
2686 return -1;
2687 if (int_reloc1.r_offset > int_reloc2.r_offset)
2688 return 1;
2689 return 0;
2690 }
2691
2692 /* Like sort_dynamic_relocs, but used for elf64 relocations. */
2693
2694 static int
2695 sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2696 const void *arg2 ATTRIBUTE_UNUSED)
2697 {
2698 #ifdef BFD64
2699 Elf_Internal_Rela int_reloc1[3];
2700 Elf_Internal_Rela int_reloc2[3];
2701
2702 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2703 (reldyn_sorting_bfd, arg1, int_reloc1);
2704 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2705 (reldyn_sorting_bfd, arg2, int_reloc2);
2706
2707 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2708 return -1;
2709 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2710 return 1;
2711
2712 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2713 return -1;
2714 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2715 return 1;
2716 return 0;
2717 #else
2718 abort ();
2719 #endif
2720 }
2721
2722
2723 /* This routine is used to write out ECOFF debugging external symbol
2724 information. It is called via mips_elf_link_hash_traverse. The
2725 ECOFF external symbol information must match the ELF external
2726 symbol information. Unfortunately, at this point we don't know
2727 whether a symbol is required by reloc information, so the two
2728 tables may wind up being different. We must sort out the external
2729 symbol information before we can set the final size of the .mdebug
2730 section, and we must set the size of the .mdebug section before we
2731 can relocate any sections, and we can't know which symbols are
2732 required by relocation until we relocate the sections.
2733 Fortunately, it is relatively unlikely that any symbol will be
2734 stripped but required by a reloc. In particular, it can not happen
2735 when generating a final executable. */
2736
2737 static bfd_boolean
2738 mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
2739 {
2740 struct extsym_info *einfo = data;
2741 bfd_boolean strip;
2742 asection *sec, *output_section;
2743
2744 if (h->root.indx == -2)
2745 strip = FALSE;
2746 else if ((h->root.def_dynamic
2747 || h->root.ref_dynamic
2748 || h->root.type == bfd_link_hash_new)
2749 && !h->root.def_regular
2750 && !h->root.ref_regular)
2751 strip = TRUE;
2752 else if (einfo->info->strip == strip_all
2753 || (einfo->info->strip == strip_some
2754 && bfd_hash_lookup (einfo->info->keep_hash,
2755 h->root.root.root.string,
2756 FALSE, FALSE) == NULL))
2757 strip = TRUE;
2758 else
2759 strip = FALSE;
2760
2761 if (strip)
2762 return TRUE;
2763
2764 if (h->esym.ifd == -2)
2765 {
2766 h->esym.jmptbl = 0;
2767 h->esym.cobol_main = 0;
2768 h->esym.weakext = 0;
2769 h->esym.reserved = 0;
2770 h->esym.ifd = ifdNil;
2771 h->esym.asym.value = 0;
2772 h->esym.asym.st = stGlobal;
2773
2774 if (h->root.root.type == bfd_link_hash_undefined
2775 || h->root.root.type == bfd_link_hash_undefweak)
2776 {
2777 const char *name;
2778
2779 /* Use undefined class. Also, set class and type for some
2780 special symbols. */
2781 name = h->root.root.root.string;
2782 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2783 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2784 {
2785 h->esym.asym.sc = scData;
2786 h->esym.asym.st = stLabel;
2787 h->esym.asym.value = 0;
2788 }
2789 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2790 {
2791 h->esym.asym.sc = scAbs;
2792 h->esym.asym.st = stLabel;
2793 h->esym.asym.value =
2794 mips_elf_hash_table (einfo->info)->procedure_count;
2795 }
2796 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (einfo->abfd))
2797 {
2798 h->esym.asym.sc = scAbs;
2799 h->esym.asym.st = stLabel;
2800 h->esym.asym.value = elf_gp (einfo->abfd);
2801 }
2802 else
2803 h->esym.asym.sc = scUndefined;
2804 }
2805 else if (h->root.root.type != bfd_link_hash_defined
2806 && h->root.root.type != bfd_link_hash_defweak)
2807 h->esym.asym.sc = scAbs;
2808 else
2809 {
2810 const char *name;
2811
2812 sec = h->root.root.u.def.section;
2813 output_section = sec->output_section;
2814
2815 /* When making a shared library and symbol h is the one from
2816 the another shared library, OUTPUT_SECTION may be null. */
2817 if (output_section == NULL)
2818 h->esym.asym.sc = scUndefined;
2819 else
2820 {
2821 name = bfd_section_name (output_section->owner, output_section);
2822
2823 if (strcmp (name, ".text") == 0)
2824 h->esym.asym.sc = scText;
2825 else if (strcmp (name, ".data") == 0)
2826 h->esym.asym.sc = scData;
2827 else if (strcmp (name, ".sdata") == 0)
2828 h->esym.asym.sc = scSData;
2829 else if (strcmp (name, ".rodata") == 0
2830 || strcmp (name, ".rdata") == 0)
2831 h->esym.asym.sc = scRData;
2832 else if (strcmp (name, ".bss") == 0)
2833 h->esym.asym.sc = scBss;
2834 else if (strcmp (name, ".sbss") == 0)
2835 h->esym.asym.sc = scSBss;
2836 else if (strcmp (name, ".init") == 0)
2837 h->esym.asym.sc = scInit;
2838 else if (strcmp (name, ".fini") == 0)
2839 h->esym.asym.sc = scFini;
2840 else
2841 h->esym.asym.sc = scAbs;
2842 }
2843 }
2844
2845 h->esym.asym.reserved = 0;
2846 h->esym.asym.index = indexNil;
2847 }
2848
2849 if (h->root.root.type == bfd_link_hash_common)
2850 h->esym.asym.value = h->root.root.u.c.size;
2851 else if (h->root.root.type == bfd_link_hash_defined
2852 || h->root.root.type == bfd_link_hash_defweak)
2853 {
2854 if (h->esym.asym.sc == scCommon)
2855 h->esym.asym.sc = scBss;
2856 else if (h->esym.asym.sc == scSCommon)
2857 h->esym.asym.sc = scSBss;
2858
2859 sec = h->root.root.u.def.section;
2860 output_section = sec->output_section;
2861 if (output_section != NULL)
2862 h->esym.asym.value = (h->root.root.u.def.value
2863 + sec->output_offset
2864 + output_section->vma);
2865 else
2866 h->esym.asym.value = 0;
2867 }
2868 else
2869 {
2870 struct mips_elf_link_hash_entry *hd = h;
2871
2872 while (hd->root.root.type == bfd_link_hash_indirect)
2873 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
2874
2875 if (hd->needs_lazy_stub)
2876 {
2877 BFD_ASSERT (hd->root.plt.plist != NULL);
2878 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
2879 /* Set type and value for a symbol with a function stub. */
2880 h->esym.asym.st = stProc;
2881 sec = hd->root.root.u.def.section;
2882 if (sec == NULL)
2883 h->esym.asym.value = 0;
2884 else
2885 {
2886 output_section = sec->output_section;
2887 if (output_section != NULL)
2888 h->esym.asym.value = (hd->root.plt.plist->stub_offset
2889 + sec->output_offset
2890 + output_section->vma);
2891 else
2892 h->esym.asym.value = 0;
2893 }
2894 }
2895 }
2896
2897 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
2898 h->root.root.root.string,
2899 &h->esym))
2900 {
2901 einfo->failed = TRUE;
2902 return FALSE;
2903 }
2904
2905 return TRUE;
2906 }
2907
2908 /* A comparison routine used to sort .gptab entries. */
2909
2910 static int
2911 gptab_compare (const void *p1, const void *p2)
2912 {
2913 const Elf32_gptab *a1 = p1;
2914 const Elf32_gptab *a2 = p2;
2915
2916 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
2917 }
2918 \f
2919 /* Functions to manage the got entry hash table. */
2920
2921 /* Use all 64 bits of a bfd_vma for the computation of a 32-bit
2922 hash number. */
2923
2924 static INLINE hashval_t
2925 mips_elf_hash_bfd_vma (bfd_vma addr)
2926 {
2927 #ifdef BFD64
2928 return addr + (addr >> 32);
2929 #else
2930 return addr;
2931 #endif
2932 }
2933
2934 static hashval_t
2935 mips_elf_got_entry_hash (const void *entry_)
2936 {
2937 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
2938
2939 return (entry->symndx
2940 + ((entry->tls_type == GOT_TLS_LDM) << 18)
2941 + (entry->tls_type == GOT_TLS_LDM ? 0
2942 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
2943 : entry->symndx >= 0 ? (entry->abfd->id
2944 + mips_elf_hash_bfd_vma (entry->d.addend))
2945 : entry->d.h->root.root.root.hash));
2946 }
2947
2948 static int
2949 mips_elf_got_entry_eq (const void *entry1, const void *entry2)
2950 {
2951 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
2952 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
2953
2954 return (e1->symndx == e2->symndx
2955 && e1->tls_type == e2->tls_type
2956 && (e1->tls_type == GOT_TLS_LDM ? TRUE
2957 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
2958 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
2959 && e1->d.addend == e2->d.addend)
2960 : e2->abfd && e1->d.h == e2->d.h));
2961 }
2962
2963 static hashval_t
2964 mips_got_page_ref_hash (const void *ref_)
2965 {
2966 const struct mips_got_page_ref *ref;
2967
2968 ref = (const struct mips_got_page_ref *) ref_;
2969 return ((ref->symndx >= 0
2970 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
2971 : ref->u.h->root.root.root.hash)
2972 + mips_elf_hash_bfd_vma (ref->addend));
2973 }
2974
2975 static int
2976 mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
2977 {
2978 const struct mips_got_page_ref *ref1, *ref2;
2979
2980 ref1 = (const struct mips_got_page_ref *) ref1_;
2981 ref2 = (const struct mips_got_page_ref *) ref2_;
2982 return (ref1->symndx == ref2->symndx
2983 && (ref1->symndx < 0
2984 ? ref1->u.h == ref2->u.h
2985 : ref1->u.abfd == ref2->u.abfd)
2986 && ref1->addend == ref2->addend);
2987 }
2988
2989 static hashval_t
2990 mips_got_page_entry_hash (const void *entry_)
2991 {
2992 const struct mips_got_page_entry *entry;
2993
2994 entry = (const struct mips_got_page_entry *) entry_;
2995 return entry->sec->id;
2996 }
2997
2998 static int
2999 mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3000 {
3001 const struct mips_got_page_entry *entry1, *entry2;
3002
3003 entry1 = (const struct mips_got_page_entry *) entry1_;
3004 entry2 = (const struct mips_got_page_entry *) entry2_;
3005 return entry1->sec == entry2->sec;
3006 }
3007 \f
3008 /* Create and return a new mips_got_info structure. */
3009
3010 static struct mips_got_info *
3011 mips_elf_create_got_info (bfd *abfd)
3012 {
3013 struct mips_got_info *g;
3014
3015 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3016 if (g == NULL)
3017 return NULL;
3018
3019 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3020 mips_elf_got_entry_eq, NULL);
3021 if (g->got_entries == NULL)
3022 return NULL;
3023
3024 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3025 mips_got_page_ref_eq, NULL);
3026 if (g->got_page_refs == NULL)
3027 return NULL;
3028
3029 return g;
3030 }
3031
3032 /* Return the GOT info for input bfd ABFD, trying to create a new one if
3033 CREATE_P and if ABFD doesn't already have a GOT. */
3034
3035 static struct mips_got_info *
3036 mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3037 {
3038 struct mips_elf_obj_tdata *tdata;
3039
3040 if (!is_mips_elf (abfd))
3041 return NULL;
3042
3043 tdata = mips_elf_tdata (abfd);
3044 if (!tdata->got && create_p)
3045 tdata->got = mips_elf_create_got_info (abfd);
3046 return tdata->got;
3047 }
3048
3049 /* Record that ABFD should use output GOT G. */
3050
3051 static void
3052 mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3053 {
3054 struct mips_elf_obj_tdata *tdata;
3055
3056 BFD_ASSERT (is_mips_elf (abfd));
3057 tdata = mips_elf_tdata (abfd);
3058 if (tdata->got)
3059 {
3060 /* The GOT structure itself and the hash table entries are
3061 allocated to a bfd, but the hash tables aren't. */
3062 htab_delete (tdata->got->got_entries);
3063 htab_delete (tdata->got->got_page_refs);
3064 if (tdata->got->got_page_entries)
3065 htab_delete (tdata->got->got_page_entries);
3066 }
3067 tdata->got = g;
3068 }
3069
3070 /* Return the dynamic relocation section. If it doesn't exist, try to
3071 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3072 if creation fails. */
3073
3074 static asection *
3075 mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
3076 {
3077 const char *dname;
3078 asection *sreloc;
3079 bfd *dynobj;
3080
3081 dname = MIPS_ELF_REL_DYN_NAME (info);
3082 dynobj = elf_hash_table (info)->dynobj;
3083 sreloc = bfd_get_linker_section (dynobj, dname);
3084 if (sreloc == NULL && create_p)
3085 {
3086 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3087 (SEC_ALLOC
3088 | SEC_LOAD
3089 | SEC_HAS_CONTENTS
3090 | SEC_IN_MEMORY
3091 | SEC_LINKER_CREATED
3092 | SEC_READONLY));
3093 if (sreloc == NULL
3094 || ! bfd_set_section_alignment (dynobj, sreloc,
3095 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
3096 return NULL;
3097 }
3098 return sreloc;
3099 }
3100
3101 /* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3102
3103 static int
3104 mips_elf_reloc_tls_type (unsigned int r_type)
3105 {
3106 if (tls_gd_reloc_p (r_type))
3107 return GOT_TLS_GD;
3108
3109 if (tls_ldm_reloc_p (r_type))
3110 return GOT_TLS_LDM;
3111
3112 if (tls_gottprel_reloc_p (r_type))
3113 return GOT_TLS_IE;
3114
3115 return GOT_TLS_NONE;
3116 }
3117
3118 /* Return the number of GOT slots needed for GOT TLS type TYPE. */
3119
3120 static int
3121 mips_tls_got_entries (unsigned int type)
3122 {
3123 switch (type)
3124 {
3125 case GOT_TLS_GD:
3126 case GOT_TLS_LDM:
3127 return 2;
3128
3129 case GOT_TLS_IE:
3130 return 1;
3131
3132 case GOT_TLS_NONE:
3133 return 0;
3134 }
3135 abort ();
3136 }
3137
3138 /* Count the number of relocations needed for a TLS GOT entry, with
3139 access types from TLS_TYPE, and symbol H (or a local symbol if H
3140 is NULL). */
3141
3142 static int
3143 mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3144 struct elf_link_hash_entry *h)
3145 {
3146 int indx = 0;
3147 bfd_boolean need_relocs = FALSE;
3148 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3149
3150 if (h && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3151 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, h)))
3152 indx = h->dynindx;
3153
3154 if ((info->shared || indx != 0)
3155 && (h == NULL
3156 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3157 || h->root.type != bfd_link_hash_undefweak))
3158 need_relocs = TRUE;
3159
3160 if (!need_relocs)
3161 return 0;
3162
3163 switch (tls_type)
3164 {
3165 case GOT_TLS_GD:
3166 return indx != 0 ? 2 : 1;
3167
3168 case GOT_TLS_IE:
3169 return 1;
3170
3171 case GOT_TLS_LDM:
3172 return info->shared ? 1 : 0;
3173
3174 default:
3175 return 0;
3176 }
3177 }
3178
3179 /* Add the number of GOT entries and TLS relocations required by ENTRY
3180 to G. */
3181
3182 static void
3183 mips_elf_count_got_entry (struct bfd_link_info *info,
3184 struct mips_got_info *g,
3185 struct mips_got_entry *entry)
3186 {
3187 if (entry->tls_type)
3188 {
3189 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3190 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
3191 entry->symndx < 0
3192 ? &entry->d.h->root : NULL);
3193 }
3194 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3195 g->local_gotno += 1;
3196 else
3197 g->global_gotno += 1;
3198 }
3199
3200 /* Output a simple dynamic relocation into SRELOC. */
3201
3202 static void
3203 mips_elf_output_dynamic_relocation (bfd *output_bfd,
3204 asection *sreloc,
3205 unsigned long reloc_index,
3206 unsigned long indx,
3207 int r_type,
3208 bfd_vma offset)
3209 {
3210 Elf_Internal_Rela rel[3];
3211
3212 memset (rel, 0, sizeof (rel));
3213
3214 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3215 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3216
3217 if (ABI_64_P (output_bfd))
3218 {
3219 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3220 (output_bfd, &rel[0],
3221 (sreloc->contents
3222 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
3223 }
3224 else
3225 bfd_elf32_swap_reloc_out
3226 (output_bfd, &rel[0],
3227 (sreloc->contents
3228 + reloc_index * sizeof (Elf32_External_Rel)));
3229 }
3230
3231 /* Initialize a set of TLS GOT entries for one symbol. */
3232
3233 static void
3234 mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3235 struct mips_got_entry *entry,
3236 struct mips_elf_link_hash_entry *h,
3237 bfd_vma value)
3238 {
3239 struct mips_elf_link_hash_table *htab;
3240 int indx;
3241 asection *sreloc, *sgot;
3242 bfd_vma got_offset, got_offset2;
3243 bfd_boolean need_relocs = FALSE;
3244
3245 htab = mips_elf_hash_table (info);
3246 if (htab == NULL)
3247 return;
3248
3249 sgot = htab->sgot;
3250
3251 indx = 0;
3252 if (h != NULL)
3253 {
3254 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3255
3256 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &h->root)
3257 && (!info->shared || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3258 indx = h->root.dynindx;
3259 }
3260
3261 if (entry->tls_initialized)
3262 return;
3263
3264 if ((info->shared || indx != 0)
3265 && (h == NULL
3266 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3267 || h->root.type != bfd_link_hash_undefweak))
3268 need_relocs = TRUE;
3269
3270 /* MINUS_ONE means the symbol is not defined in this object. It may not
3271 be defined at all; assume that the value doesn't matter in that
3272 case. Otherwise complain if we would use the value. */
3273 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3274 || h->root.root.type == bfd_link_hash_undefweak);
3275
3276 /* Emit necessary relocations. */
3277 sreloc = mips_elf_rel_dyn_section (info, FALSE);
3278 got_offset = entry->gotidx;
3279
3280 switch (entry->tls_type)
3281 {
3282 case GOT_TLS_GD:
3283 /* General Dynamic. */
3284 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
3285
3286 if (need_relocs)
3287 {
3288 mips_elf_output_dynamic_relocation
3289 (abfd, sreloc, sreloc->reloc_count++, indx,
3290 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3291 sgot->output_offset + sgot->output_section->vma + got_offset);
3292
3293 if (indx)
3294 mips_elf_output_dynamic_relocation
3295 (abfd, sreloc, sreloc->reloc_count++, indx,
3296 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
3297 sgot->output_offset + sgot->output_section->vma + got_offset2);
3298 else
3299 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3300 sgot->contents + got_offset2);
3301 }
3302 else
3303 {
3304 MIPS_ELF_PUT_WORD (abfd, 1,
3305 sgot->contents + got_offset);
3306 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
3307 sgot->contents + got_offset2);
3308 }
3309 break;
3310
3311 case GOT_TLS_IE:
3312 /* Initial Exec model. */
3313 if (need_relocs)
3314 {
3315 if (indx == 0)
3316 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
3317 sgot->contents + got_offset);
3318 else
3319 MIPS_ELF_PUT_WORD (abfd, 0,
3320 sgot->contents + got_offset);
3321
3322 mips_elf_output_dynamic_relocation
3323 (abfd, sreloc, sreloc->reloc_count++, indx,
3324 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
3325 sgot->output_offset + sgot->output_section->vma + got_offset);
3326 }
3327 else
3328 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
3329 sgot->contents + got_offset);
3330 break;
3331
3332 case GOT_TLS_LDM:
3333 /* The initial offset is zero, and the LD offsets will include the
3334 bias by DTP_OFFSET. */
3335 MIPS_ELF_PUT_WORD (abfd, 0,
3336 sgot->contents + got_offset
3337 + MIPS_ELF_GOT_SIZE (abfd));
3338
3339 if (!info->shared)
3340 MIPS_ELF_PUT_WORD (abfd, 1,
3341 sgot->contents + got_offset);
3342 else
3343 mips_elf_output_dynamic_relocation
3344 (abfd, sreloc, sreloc->reloc_count++, indx,
3345 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3346 sgot->output_offset + sgot->output_section->vma + got_offset);
3347 break;
3348
3349 default:
3350 abort ();
3351 }
3352
3353 entry->tls_initialized = TRUE;
3354 }
3355
3356 /* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3357 for global symbol H. .got.plt comes before the GOT, so the offset
3358 will be negative. */
3359
3360 static bfd_vma
3361 mips_elf_gotplt_index (struct bfd_link_info *info,
3362 struct elf_link_hash_entry *h)
3363 {
3364 bfd_vma got_address, got_value;
3365 struct mips_elf_link_hash_table *htab;
3366
3367 htab = mips_elf_hash_table (info);
3368 BFD_ASSERT (htab != NULL);
3369
3370 BFD_ASSERT (h->plt.plist != NULL);
3371 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
3372
3373 /* Calculate the address of the associated .got.plt entry. */
3374 got_address = (htab->sgotplt->output_section->vma
3375 + htab->sgotplt->output_offset
3376 + (h->plt.plist->gotplt_index
3377 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
3378
3379 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3380 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3381 + htab->root.hgot->root.u.def.section->output_offset
3382 + htab->root.hgot->root.u.def.value);
3383
3384 return got_address - got_value;
3385 }
3386
3387 /* Return the GOT offset for address VALUE. If there is not yet a GOT
3388 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3389 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3390 offset can be found. */
3391
3392 static bfd_vma
3393 mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3394 bfd_vma value, unsigned long r_symndx,
3395 struct mips_elf_link_hash_entry *h, int r_type)
3396 {
3397 struct mips_elf_link_hash_table *htab;
3398 struct mips_got_entry *entry;
3399
3400 htab = mips_elf_hash_table (info);
3401 BFD_ASSERT (htab != NULL);
3402
3403 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3404 r_symndx, h, r_type);
3405 if (!entry)
3406 return MINUS_ONE;
3407
3408 if (entry->tls_type)
3409 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3410 return entry->gotidx;
3411 }
3412
3413 /* Return the GOT index of global symbol H in the primary GOT. */
3414
3415 static bfd_vma
3416 mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3417 struct elf_link_hash_entry *h)
3418 {
3419 struct mips_elf_link_hash_table *htab;
3420 long global_got_dynindx;
3421 struct mips_got_info *g;
3422 bfd_vma got_index;
3423
3424 htab = mips_elf_hash_table (info);
3425 BFD_ASSERT (htab != NULL);
3426
3427 global_got_dynindx = 0;
3428 if (htab->global_gotsym != NULL)
3429 global_got_dynindx = htab->global_gotsym->dynindx;
3430
3431 /* Once we determine the global GOT entry with the lowest dynamic
3432 symbol table index, we must put all dynamic symbols with greater
3433 indices into the primary GOT. That makes it easy to calculate the
3434 GOT offset. */
3435 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3436 g = mips_elf_bfd_got (obfd, FALSE);
3437 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3438 * MIPS_ELF_GOT_SIZE (obfd));
3439 BFD_ASSERT (got_index < htab->sgot->size);
3440
3441 return got_index;
3442 }
3443
3444 /* Return the GOT index for the global symbol indicated by H, which is
3445 referenced by a relocation of type R_TYPE in IBFD. */
3446
3447 static bfd_vma
3448 mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3449 struct elf_link_hash_entry *h, int r_type)
3450 {
3451 struct mips_elf_link_hash_table *htab;
3452 struct mips_got_info *g;
3453 struct mips_got_entry lookup, *entry;
3454 bfd_vma gotidx;
3455
3456 htab = mips_elf_hash_table (info);
3457 BFD_ASSERT (htab != NULL);
3458
3459 g = mips_elf_bfd_got (ibfd, FALSE);
3460 BFD_ASSERT (g);
3461
3462 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3463 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3464 return mips_elf_primary_global_got_index (obfd, info, h);
3465
3466 lookup.abfd = ibfd;
3467 lookup.symndx = -1;
3468 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3469 entry = htab_find (g->got_entries, &lookup);
3470 BFD_ASSERT (entry);
3471
3472 gotidx = entry->gotidx;
3473 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3474
3475 if (lookup.tls_type)
3476 {
3477 bfd_vma value = MINUS_ONE;
3478
3479 if ((h->root.type == bfd_link_hash_defined
3480 || h->root.type == bfd_link_hash_defweak)
3481 && h->root.u.def.section->output_section)
3482 value = (h->root.u.def.value
3483 + h->root.u.def.section->output_offset
3484 + h->root.u.def.section->output_section->vma);
3485
3486 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
3487 }
3488 return gotidx;
3489 }
3490
3491 /* Find a GOT page entry that points to within 32KB of VALUE. These
3492 entries are supposed to be placed at small offsets in the GOT, i.e.,
3493 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3494 entry could be created. If OFFSETP is nonnull, use it to return the
3495 offset of the GOT entry from VALUE. */
3496
3497 static bfd_vma
3498 mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3499 bfd_vma value, bfd_vma *offsetp)
3500 {
3501 bfd_vma page, got_index;
3502 struct mips_got_entry *entry;
3503
3504 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
3505 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3506 NULL, R_MIPS_GOT_PAGE);
3507
3508 if (!entry)
3509 return MINUS_ONE;
3510
3511 got_index = entry->gotidx;
3512
3513 if (offsetp)
3514 *offsetp = value - entry->d.address;
3515
3516 return got_index;
3517 }
3518
3519 /* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
3520 EXTERNAL is true if the relocation was originally against a global
3521 symbol that binds locally. */
3522
3523 static bfd_vma
3524 mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
3525 bfd_vma value, bfd_boolean external)
3526 {
3527 struct mips_got_entry *entry;
3528
3529 /* GOT16 relocations against local symbols are followed by a LO16
3530 relocation; those against global symbols are not. Thus if the
3531 symbol was originally local, the GOT16 relocation should load the
3532 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
3533 if (! external)
3534 value = mips_elf_high (value) << 16;
3535
3536 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3537 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3538 same in all cases. */
3539 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3540 NULL, R_MIPS_GOT16);
3541 if (entry)
3542 return entry->gotidx;
3543 else
3544 return MINUS_ONE;
3545 }
3546
3547 /* Returns the offset for the entry at the INDEXth position
3548 in the GOT. */
3549
3550 static bfd_vma
3551 mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
3552 bfd *input_bfd, bfd_vma got_index)
3553 {
3554 struct mips_elf_link_hash_table *htab;
3555 asection *sgot;
3556 bfd_vma gp;
3557
3558 htab = mips_elf_hash_table (info);
3559 BFD_ASSERT (htab != NULL);
3560
3561 sgot = htab->sgot;
3562 gp = _bfd_get_gp_value (output_bfd)
3563 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
3564
3565 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
3566 }
3567
3568 /* Create and return a local GOT entry for VALUE, which was calculated
3569 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3570 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3571 instead. */
3572
3573 static struct mips_got_entry *
3574 mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
3575 bfd *ibfd, bfd_vma value,
3576 unsigned long r_symndx,
3577 struct mips_elf_link_hash_entry *h,
3578 int r_type)
3579 {
3580 struct mips_got_entry lookup, *entry;
3581 void **loc;
3582 struct mips_got_info *g;
3583 struct mips_elf_link_hash_table *htab;
3584 bfd_vma gotidx;
3585
3586 htab = mips_elf_hash_table (info);
3587 BFD_ASSERT (htab != NULL);
3588
3589 g = mips_elf_bfd_got (ibfd, FALSE);
3590 if (g == NULL)
3591 {
3592 g = mips_elf_bfd_got (abfd, FALSE);
3593 BFD_ASSERT (g != NULL);
3594 }
3595
3596 /* This function shouldn't be called for symbols that live in the global
3597 area of the GOT. */
3598 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
3599
3600 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3601 if (lookup.tls_type)
3602 {
3603 lookup.abfd = ibfd;
3604 if (tls_ldm_reloc_p (r_type))
3605 {
3606 lookup.symndx = 0;
3607 lookup.d.addend = 0;
3608 }
3609 else if (h == NULL)
3610 {
3611 lookup.symndx = r_symndx;
3612 lookup.d.addend = 0;
3613 }
3614 else
3615 {
3616 lookup.symndx = -1;
3617 lookup.d.h = h;
3618 }
3619
3620 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3621 BFD_ASSERT (entry);
3622
3623 gotidx = entry->gotidx;
3624 BFD_ASSERT (gotidx > 0 && gotidx < htab->sgot->size);
3625
3626 return entry;
3627 }
3628
3629 lookup.abfd = NULL;
3630 lookup.symndx = -1;
3631 lookup.d.address = value;
3632 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3633 if (!loc)
3634 return NULL;
3635
3636 entry = (struct mips_got_entry *) *loc;
3637 if (entry)
3638 return entry;
3639
3640 if (g->assigned_low_gotno > g->assigned_high_gotno)
3641 {
3642 /* We didn't allocate enough space in the GOT. */
3643 (*_bfd_error_handler)
3644 (_("not enough GOT space for local GOT entries"));
3645 bfd_set_error (bfd_error_bad_value);
3646 return NULL;
3647 }
3648
3649 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3650 if (!entry)
3651 return NULL;
3652
3653 if (got16_reloc_p (r_type)
3654 || call16_reloc_p (r_type)
3655 || got_page_reloc_p (r_type)
3656 || got_disp_reloc_p (r_type))
3657 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3658 else
3659 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3660
3661 *entry = lookup;
3662 *loc = entry;
3663
3664 MIPS_ELF_PUT_WORD (abfd, value, htab->sgot->contents + entry->gotidx);
3665
3666 /* These GOT entries need a dynamic relocation on VxWorks. */
3667 if (htab->is_vxworks)
3668 {
3669 Elf_Internal_Rela outrel;
3670 asection *s;
3671 bfd_byte *rloc;
3672 bfd_vma got_address;
3673
3674 s = mips_elf_rel_dyn_section (info, FALSE);
3675 got_address = (htab->sgot->output_section->vma
3676 + htab->sgot->output_offset
3677 + entry->gotidx);
3678
3679 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
3680 outrel.r_offset = got_address;
3681 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3682 outrel.r_addend = value;
3683 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
3684 }
3685
3686 return entry;
3687 }
3688
3689 /* Return the number of dynamic section symbols required by OUTPUT_BFD.
3690 The number might be exact or a worst-case estimate, depending on how
3691 much information is available to elf_backend_omit_section_dynsym at
3692 the current linking stage. */
3693
3694 static bfd_size_type
3695 count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3696 {
3697 bfd_size_type count;
3698
3699 count = 0;
3700 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
3701 {
3702 asection *p;
3703 const struct elf_backend_data *bed;
3704
3705 bed = get_elf_backend_data (output_bfd);
3706 for (p = output_bfd->sections; p ; p = p->next)
3707 if ((p->flags & SEC_EXCLUDE) == 0
3708 && (p->flags & SEC_ALLOC) != 0
3709 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3710 ++count;
3711 }
3712 return count;
3713 }
3714
3715 /* Sort the dynamic symbol table so that symbols that need GOT entries
3716 appear towards the end. */
3717
3718 static bfd_boolean
3719 mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
3720 {
3721 struct mips_elf_link_hash_table *htab;
3722 struct mips_elf_hash_sort_data hsd;
3723 struct mips_got_info *g;
3724
3725 if (elf_hash_table (info)->dynsymcount == 0)
3726 return TRUE;
3727
3728 htab = mips_elf_hash_table (info);
3729 BFD_ASSERT (htab != NULL);
3730
3731 g = htab->got_info;
3732 if (g == NULL)
3733 return TRUE;
3734
3735 hsd.low = NULL;
3736 hsd.max_unref_got_dynindx
3737 = hsd.min_got_dynindx
3738 = (elf_hash_table (info)->dynsymcount - g->reloc_only_gotno);
3739 hsd.max_non_got_dynindx = count_section_dynsyms (abfd, info) + 1;
3740 mips_elf_link_hash_traverse (((struct mips_elf_link_hash_table *)
3741 elf_hash_table (info)),
3742 mips_elf_sort_hash_table_f,
3743 &hsd);
3744
3745 /* There should have been enough room in the symbol table to
3746 accommodate both the GOT and non-GOT symbols. */
3747 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
3748 BFD_ASSERT ((unsigned long) hsd.max_unref_got_dynindx
3749 == elf_hash_table (info)->dynsymcount);
3750 BFD_ASSERT (elf_hash_table (info)->dynsymcount - hsd.min_got_dynindx
3751 == g->global_gotno);
3752
3753 /* Now we know which dynamic symbol has the lowest dynamic symbol
3754 table index in the GOT. */
3755 htab->global_gotsym = hsd.low;
3756
3757 return TRUE;
3758 }
3759
3760 /* If H needs a GOT entry, assign it the highest available dynamic
3761 index. Otherwise, assign it the lowest available dynamic
3762 index. */
3763
3764 static bfd_boolean
3765 mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
3766 {
3767 struct mips_elf_hash_sort_data *hsd = data;
3768
3769 /* Symbols without dynamic symbol table entries aren't interesting
3770 at all. */
3771 if (h->root.dynindx == -1)
3772 return TRUE;
3773
3774 switch (h->global_got_area)
3775 {
3776 case GGA_NONE:
3777 h->root.dynindx = hsd->max_non_got_dynindx++;
3778 break;
3779
3780 case GGA_NORMAL:
3781 h->root.dynindx = --hsd->min_got_dynindx;
3782 hsd->low = (struct elf_link_hash_entry *) h;
3783 break;
3784
3785 case GGA_RELOC_ONLY:
3786 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3787 hsd->low = (struct elf_link_hash_entry *) h;
3788 h->root.dynindx = hsd->max_unref_got_dynindx++;
3789 break;
3790 }
3791
3792 return TRUE;
3793 }
3794
3795 /* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3796 (which is owned by the caller and shouldn't be added to the
3797 hash table directly). */
3798
3799 static bfd_boolean
3800 mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3801 struct mips_got_entry *lookup)
3802 {
3803 struct mips_elf_link_hash_table *htab;
3804 struct mips_got_entry *entry;
3805 struct mips_got_info *g;
3806 void **loc, **bfd_loc;
3807
3808 /* Make sure there's a slot for this entry in the master GOT. */
3809 htab = mips_elf_hash_table (info);
3810 g = htab->got_info;
3811 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3812 if (!loc)
3813 return FALSE;
3814
3815 /* Populate the entry if it isn't already. */
3816 entry = (struct mips_got_entry *) *loc;
3817 if (!entry)
3818 {
3819 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3820 if (!entry)
3821 return FALSE;
3822
3823 lookup->tls_initialized = FALSE;
3824 lookup->gotidx = -1;
3825 *entry = *lookup;
3826 *loc = entry;
3827 }
3828
3829 /* Reuse the same GOT entry for the BFD's GOT. */
3830 g = mips_elf_bfd_got (abfd, TRUE);
3831 if (!g)
3832 return FALSE;
3833
3834 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3835 if (!bfd_loc)
3836 return FALSE;
3837
3838 if (!*bfd_loc)
3839 *bfd_loc = entry;
3840 return TRUE;
3841 }
3842
3843 /* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3844 entry for it. FOR_CALL is true if the caller is only interested in
3845 using the GOT entry for calls. */
3846
3847 static bfd_boolean
3848 mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3849 bfd *abfd, struct bfd_link_info *info,
3850 bfd_boolean for_call, int r_type)
3851 {
3852 struct mips_elf_link_hash_table *htab;
3853 struct mips_elf_link_hash_entry *hmips;
3854 struct mips_got_entry entry;
3855 unsigned char tls_type;
3856
3857 htab = mips_elf_hash_table (info);
3858 BFD_ASSERT (htab != NULL);
3859
3860 hmips = (struct mips_elf_link_hash_entry *) h;
3861 if (!for_call)
3862 hmips->got_only_for_calls = FALSE;
3863
3864 /* A global symbol in the GOT must also be in the dynamic symbol
3865 table. */
3866 if (h->dynindx == -1)
3867 {
3868 switch (ELF_ST_VISIBILITY (h->other))
3869 {
3870 case STV_INTERNAL:
3871 case STV_HIDDEN:
3872 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
3873 break;
3874 }
3875 if (!bfd_elf_link_record_dynamic_symbol (info, h))
3876 return FALSE;
3877 }
3878
3879 tls_type = mips_elf_reloc_tls_type (r_type);
3880 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
3881 hmips->global_got_area = GGA_NORMAL;
3882
3883 entry.abfd = abfd;
3884 entry.symndx = -1;
3885 entry.d.h = (struct mips_elf_link_hash_entry *) h;
3886 entry.tls_type = tls_type;
3887 return mips_elf_record_got_entry (info, abfd, &entry);
3888 }
3889
3890 /* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
3891 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
3892
3893 static bfd_boolean
3894 mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
3895 struct bfd_link_info *info, int r_type)
3896 {
3897 struct mips_elf_link_hash_table *htab;
3898 struct mips_got_info *g;
3899 struct mips_got_entry entry;
3900
3901 htab = mips_elf_hash_table (info);
3902 BFD_ASSERT (htab != NULL);
3903
3904 g = htab->got_info;
3905 BFD_ASSERT (g != NULL);
3906
3907 entry.abfd = abfd;
3908 entry.symndx = symndx;
3909 entry.d.addend = addend;
3910 entry.tls_type = mips_elf_reloc_tls_type (r_type);
3911 return mips_elf_record_got_entry (info, abfd, &entry);
3912 }
3913
3914 /* Record that ABFD has a page relocation against SYMNDX + ADDEND.
3915 H is the symbol's hash table entry, or null if SYMNDX is local
3916 to ABFD. */
3917
3918 static bfd_boolean
3919 mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
3920 long symndx, struct elf_link_hash_entry *h,
3921 bfd_signed_vma addend)
3922 {
3923 struct mips_elf_link_hash_table *htab;
3924 struct mips_got_info *g1, *g2;
3925 struct mips_got_page_ref lookup, *entry;
3926 void **loc, **bfd_loc;
3927
3928 htab = mips_elf_hash_table (info);
3929 BFD_ASSERT (htab != NULL);
3930
3931 g1 = htab->got_info;
3932 BFD_ASSERT (g1 != NULL);
3933
3934 if (h)
3935 {
3936 lookup.symndx = -1;
3937 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
3938 }
3939 else
3940 {
3941 lookup.symndx = symndx;
3942 lookup.u.abfd = abfd;
3943 }
3944 lookup.addend = addend;
3945 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
3946 if (loc == NULL)
3947 return FALSE;
3948
3949 entry = (struct mips_got_page_ref *) *loc;
3950 if (!entry)
3951 {
3952 entry = bfd_alloc (abfd, sizeof (*entry));
3953 if (!entry)
3954 return FALSE;
3955
3956 *entry = lookup;
3957 *loc = entry;
3958 }
3959
3960 /* Add the same entry to the BFD's GOT. */
3961 g2 = mips_elf_bfd_got (abfd, TRUE);
3962 if (!g2)
3963 return FALSE;
3964
3965 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
3966 if (!bfd_loc)
3967 return FALSE;
3968
3969 if (!*bfd_loc)
3970 *bfd_loc = entry;
3971
3972 return TRUE;
3973 }
3974
3975 /* Add room for N relocations to the .rel(a).dyn section in ABFD. */
3976
3977 static void
3978 mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
3979 unsigned int n)
3980 {
3981 asection *s;
3982 struct mips_elf_link_hash_table *htab;
3983
3984 htab = mips_elf_hash_table (info);
3985 BFD_ASSERT (htab != NULL);
3986
3987 s = mips_elf_rel_dyn_section (info, FALSE);
3988 BFD_ASSERT (s != NULL);
3989
3990 if (htab->is_vxworks)
3991 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
3992 else
3993 {
3994 if (s->size == 0)
3995 {
3996 /* Make room for a null element. */
3997 s->size += MIPS_ELF_REL_SIZE (abfd);
3998 ++s->reloc_count;
3999 }
4000 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4001 }
4002 }
4003 \f
4004 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4005 mips_elf_traverse_got_arg structure. Count the number of GOT
4006 entries and TLS relocs. Set DATA->value to true if we need
4007 to resolve indirect or warning symbols and then recreate the GOT. */
4008
4009 static int
4010 mips_elf_check_recreate_got (void **entryp, void *data)
4011 {
4012 struct mips_got_entry *entry;
4013 struct mips_elf_traverse_got_arg *arg;
4014
4015 entry = (struct mips_got_entry *) *entryp;
4016 arg = (struct mips_elf_traverse_got_arg *) data;
4017 if (entry->abfd != NULL && entry->symndx == -1)
4018 {
4019 struct mips_elf_link_hash_entry *h;
4020
4021 h = entry->d.h;
4022 if (h->root.root.type == bfd_link_hash_indirect
4023 || h->root.root.type == bfd_link_hash_warning)
4024 {
4025 arg->value = TRUE;
4026 return 0;
4027 }
4028 }
4029 mips_elf_count_got_entry (arg->info, arg->g, entry);
4030 return 1;
4031 }
4032
4033 /* A htab_traverse callback for GOT entries, with DATA pointing to a
4034 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4035 converting entries for indirect and warning symbols into entries
4036 for the target symbol. Set DATA->g to null on error. */
4037
4038 static int
4039 mips_elf_recreate_got (void **entryp, void *data)
4040 {
4041 struct mips_got_entry new_entry, *entry;
4042 struct mips_elf_traverse_got_arg *arg;
4043 void **slot;
4044
4045 entry = (struct mips_got_entry *) *entryp;
4046 arg = (struct mips_elf_traverse_got_arg *) data;
4047 if (entry->abfd != NULL
4048 && entry->symndx == -1
4049 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4050 || entry->d.h->root.root.type == bfd_link_hash_warning))
4051 {
4052 struct mips_elf_link_hash_entry *h;
4053
4054 new_entry = *entry;
4055 entry = &new_entry;
4056 h = entry->d.h;
4057 do
4058 {
4059 BFD_ASSERT (h->global_got_area == GGA_NONE);
4060 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4061 }
4062 while (h->root.root.type == bfd_link_hash_indirect
4063 || h->root.root.type == bfd_link_hash_warning);
4064 entry->d.h = h;
4065 }
4066 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4067 if (slot == NULL)
4068 {
4069 arg->g = NULL;
4070 return 0;
4071 }
4072 if (*slot == NULL)
4073 {
4074 if (entry == &new_entry)
4075 {
4076 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4077 if (!entry)
4078 {
4079 arg->g = NULL;
4080 return 0;
4081 }
4082 *entry = new_entry;
4083 }
4084 *slot = entry;
4085 mips_elf_count_got_entry (arg->info, arg->g, entry);
4086 }
4087 return 1;
4088 }
4089
4090 /* Return the maximum number of GOT page entries required for RANGE. */
4091
4092 static bfd_vma
4093 mips_elf_pages_for_range (const struct mips_got_page_range *range)
4094 {
4095 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4096 }
4097
4098 /* Record that G requires a page entry that can reach SEC + ADDEND. */
4099
4100 static bfd_boolean
4101 mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
4102 asection *sec, bfd_signed_vma addend)
4103 {
4104 struct mips_got_info *g = arg->g;
4105 struct mips_got_page_entry lookup, *entry;
4106 struct mips_got_page_range **range_ptr, *range;
4107 bfd_vma old_pages, new_pages;
4108 void **loc;
4109
4110 /* Find the mips_got_page_entry hash table entry for this section. */
4111 lookup.sec = sec;
4112 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4113 if (loc == NULL)
4114 return FALSE;
4115
4116 /* Create a mips_got_page_entry if this is the first time we've
4117 seen the section. */
4118 entry = (struct mips_got_page_entry *) *loc;
4119 if (!entry)
4120 {
4121 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
4122 if (!entry)
4123 return FALSE;
4124
4125 entry->sec = sec;
4126 *loc = entry;
4127 }
4128
4129 /* Skip over ranges whose maximum extent cannot share a page entry
4130 with ADDEND. */
4131 range_ptr = &entry->ranges;
4132 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4133 range_ptr = &(*range_ptr)->next;
4134
4135 /* If we scanned to the end of the list, or found a range whose
4136 minimum extent cannot share a page entry with ADDEND, create
4137 a new singleton range. */
4138 range = *range_ptr;
4139 if (!range || addend < range->min_addend - 0xffff)
4140 {
4141 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
4142 if (!range)
4143 return FALSE;
4144
4145 range->next = *range_ptr;
4146 range->min_addend = addend;
4147 range->max_addend = addend;
4148
4149 *range_ptr = range;
4150 entry->num_pages++;
4151 g->page_gotno++;
4152 return TRUE;
4153 }
4154
4155 /* Remember how many pages the old range contributed. */
4156 old_pages = mips_elf_pages_for_range (range);
4157
4158 /* Update the ranges. */
4159 if (addend < range->min_addend)
4160 range->min_addend = addend;
4161 else if (addend > range->max_addend)
4162 {
4163 if (range->next && addend >= range->next->min_addend - 0xffff)
4164 {
4165 old_pages += mips_elf_pages_for_range (range->next);
4166 range->max_addend = range->next->max_addend;
4167 range->next = range->next->next;
4168 }
4169 else
4170 range->max_addend = addend;
4171 }
4172
4173 /* Record any change in the total estimate. */
4174 new_pages = mips_elf_pages_for_range (range);
4175 if (old_pages != new_pages)
4176 {
4177 entry->num_pages += new_pages - old_pages;
4178 g->page_gotno += new_pages - old_pages;
4179 }
4180
4181 return TRUE;
4182 }
4183
4184 /* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4185 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4186 whether the page reference described by *REFP needs a GOT page entry,
4187 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4188
4189 static bfd_boolean
4190 mips_elf_resolve_got_page_ref (void **refp, void *data)
4191 {
4192 struct mips_got_page_ref *ref;
4193 struct mips_elf_traverse_got_arg *arg;
4194 struct mips_elf_link_hash_table *htab;
4195 asection *sec;
4196 bfd_vma addend;
4197
4198 ref = (struct mips_got_page_ref *) *refp;
4199 arg = (struct mips_elf_traverse_got_arg *) data;
4200 htab = mips_elf_hash_table (arg->info);
4201
4202 if (ref->symndx < 0)
4203 {
4204 struct mips_elf_link_hash_entry *h;
4205
4206 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4207 h = ref->u.h;
4208 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4209 return 1;
4210
4211 /* Ignore undefined symbols; we'll issue an error later if
4212 appropriate. */
4213 if (!((h->root.root.type == bfd_link_hash_defined
4214 || h->root.root.type == bfd_link_hash_defweak)
4215 && h->root.root.u.def.section))
4216 return 1;
4217
4218 sec = h->root.root.u.def.section;
4219 addend = h->root.root.u.def.value + ref->addend;
4220 }
4221 else
4222 {
4223 Elf_Internal_Sym *isym;
4224
4225 /* Read in the symbol. */
4226 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4227 ref->symndx);
4228 if (isym == NULL)
4229 {
4230 arg->g = NULL;
4231 return 0;
4232 }
4233
4234 /* Get the associated input section. */
4235 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4236 if (sec == NULL)
4237 {
4238 arg->g = NULL;
4239 return 0;
4240 }
4241
4242 /* If this is a mergable section, work out the section and offset
4243 of the merged data. For section symbols, the addend specifies
4244 of the offset _of_ the first byte in the data, otherwise it
4245 specifies the offset _from_ the first byte. */
4246 if (sec->flags & SEC_MERGE)
4247 {
4248 void *secinfo;
4249
4250 secinfo = elf_section_data (sec)->sec_info;
4251 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4252 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4253 isym->st_value + ref->addend);
4254 else
4255 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4256 isym->st_value) + ref->addend;
4257 }
4258 else
4259 addend = isym->st_value + ref->addend;
4260 }
4261 if (!mips_elf_record_got_page_entry (arg, sec, addend))
4262 {
4263 arg->g = NULL;
4264 return 0;
4265 }
4266 return 1;
4267 }
4268
4269 /* If any entries in G->got_entries are for indirect or warning symbols,
4270 replace them with entries for the target symbol. Convert g->got_page_refs
4271 into got_page_entry structures and estimate the number of page entries
4272 that they require. */
4273
4274 static bfd_boolean
4275 mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4276 struct mips_got_info *g)
4277 {
4278 struct mips_elf_traverse_got_arg tga;
4279 struct mips_got_info oldg;
4280
4281 oldg = *g;
4282
4283 tga.info = info;
4284 tga.g = g;
4285 tga.value = FALSE;
4286 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4287 if (tga.value)
4288 {
4289 *g = oldg;
4290 g->got_entries = htab_create (htab_size (oldg.got_entries),
4291 mips_elf_got_entry_hash,
4292 mips_elf_got_entry_eq, NULL);
4293 if (!g->got_entries)
4294 return FALSE;
4295
4296 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4297 if (!tga.g)
4298 return FALSE;
4299
4300 htab_delete (oldg.got_entries);
4301 }
4302
4303 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4304 mips_got_page_entry_eq, NULL);
4305 if (g->got_page_entries == NULL)
4306 return FALSE;
4307
4308 tga.info = info;
4309 tga.g = g;
4310 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4311
4312 return TRUE;
4313 }
4314
4315 /* Return true if a GOT entry for H should live in the local rather than
4316 global GOT area. */
4317
4318 static bfd_boolean
4319 mips_use_local_got_p (struct bfd_link_info *info,
4320 struct mips_elf_link_hash_entry *h)
4321 {
4322 /* Symbols that aren't in the dynamic symbol table must live in the
4323 local GOT. This includes symbols that are completely undefined
4324 and which therefore don't bind locally. We'll report undefined
4325 symbols later if appropriate. */
4326 if (h->root.dynindx == -1)
4327 return TRUE;
4328
4329 /* Symbols that bind locally can (and in the case of forced-local
4330 symbols, must) live in the local GOT. */
4331 if (h->got_only_for_calls
4332 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4333 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4334 return TRUE;
4335
4336 /* If this is an executable that must provide a definition of the symbol,
4337 either though PLTs or copy relocations, then that address should go in
4338 the local rather than global GOT. */
4339 if (info->executable && h->has_static_relocs)
4340 return TRUE;
4341
4342 return FALSE;
4343 }
4344
4345 /* A mips_elf_link_hash_traverse callback for which DATA points to the
4346 link_info structure. Decide whether the hash entry needs an entry in
4347 the global part of the primary GOT, setting global_got_area accordingly.
4348 Count the number of global symbols that are in the primary GOT only
4349 because they have relocations against them (reloc_only_gotno). */
4350
4351 static int
4352 mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
4353 {
4354 struct bfd_link_info *info;
4355 struct mips_elf_link_hash_table *htab;
4356 struct mips_got_info *g;
4357
4358 info = (struct bfd_link_info *) data;
4359 htab = mips_elf_hash_table (info);
4360 g = htab->got_info;
4361 if (h->global_got_area != GGA_NONE)
4362 {
4363 /* Make a final decision about whether the symbol belongs in the
4364 local or global GOT. */
4365 if (mips_use_local_got_p (info, h))
4366 /* The symbol belongs in the local GOT. We no longer need this
4367 entry if it was only used for relocations; those relocations
4368 will be against the null or section symbol instead of H. */
4369 h->global_got_area = GGA_NONE;
4370 else if (htab->is_vxworks
4371 && h->got_only_for_calls
4372 && h->root.plt.plist->mips_offset != MINUS_ONE)
4373 /* On VxWorks, calls can refer directly to the .got.plt entry;
4374 they don't need entries in the regular GOT. .got.plt entries
4375 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4376 h->global_got_area = GGA_NONE;
4377 else if (h->global_got_area == GGA_RELOC_ONLY)
4378 {
4379 g->reloc_only_gotno++;
4380 g->global_gotno++;
4381 }
4382 }
4383 return 1;
4384 }
4385 \f
4386 /* A htab_traverse callback for GOT entries. Add each one to the GOT
4387 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4388
4389 static int
4390 mips_elf_add_got_entry (void **entryp, void *data)
4391 {
4392 struct mips_got_entry *entry;
4393 struct mips_elf_traverse_got_arg *arg;
4394 void **slot;
4395
4396 entry = (struct mips_got_entry *) *entryp;
4397 arg = (struct mips_elf_traverse_got_arg *) data;
4398 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4399 if (!slot)
4400 {
4401 arg->g = NULL;
4402 return 0;
4403 }
4404 if (!*slot)
4405 {
4406 *slot = entry;
4407 mips_elf_count_got_entry (arg->info, arg->g, entry);
4408 }
4409 return 1;
4410 }
4411
4412 /* A htab_traverse callback for GOT page entries. Add each one to the GOT
4413 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
4414
4415 static int
4416 mips_elf_add_got_page_entry (void **entryp, void *data)
4417 {
4418 struct mips_got_page_entry *entry;
4419 struct mips_elf_traverse_got_arg *arg;
4420 void **slot;
4421
4422 entry = (struct mips_got_page_entry *) *entryp;
4423 arg = (struct mips_elf_traverse_got_arg *) data;
4424 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4425 if (!slot)
4426 {
4427 arg->g = NULL;
4428 return 0;
4429 }
4430 if (!*slot)
4431 {
4432 *slot = entry;
4433 arg->g->page_gotno += entry->num_pages;
4434 }
4435 return 1;
4436 }
4437
4438 /* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4439 this would lead to overflow, 1 if they were merged successfully,
4440 and 0 if a merge failed due to lack of memory. (These values are chosen
4441 so that nonnegative return values can be returned by a htab_traverse
4442 callback.) */
4443
4444 static int
4445 mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
4446 struct mips_got_info *to,
4447 struct mips_elf_got_per_bfd_arg *arg)
4448 {
4449 struct mips_elf_traverse_got_arg tga;
4450 unsigned int estimate;
4451
4452 /* Work out how many page entries we would need for the combined GOT. */
4453 estimate = arg->max_pages;
4454 if (estimate >= from->page_gotno + to->page_gotno)
4455 estimate = from->page_gotno + to->page_gotno;
4456
4457 /* And conservatively estimate how many local and TLS entries
4458 would be needed. */
4459 estimate += from->local_gotno + to->local_gotno;
4460 estimate += from->tls_gotno + to->tls_gotno;
4461
4462 /* If we're merging with the primary got, any TLS relocations will
4463 come after the full set of global entries. Otherwise estimate those
4464 conservatively as well. */
4465 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
4466 estimate += arg->global_count;
4467 else
4468 estimate += from->global_gotno + to->global_gotno;
4469
4470 /* Bail out if the combined GOT might be too big. */
4471 if (estimate > arg->max_count)
4472 return -1;
4473
4474 /* Transfer the bfd's got information from FROM to TO. */
4475 tga.info = arg->info;
4476 tga.g = to;
4477 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4478 if (!tga.g)
4479 return 0;
4480
4481 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4482 if (!tga.g)
4483 return 0;
4484
4485 mips_elf_replace_bfd_got (abfd, to);
4486 return 1;
4487 }
4488
4489 /* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
4490 as possible of the primary got, since it doesn't require explicit
4491 dynamic relocations, but don't use bfds that would reference global
4492 symbols out of the addressable range. Failing the primary got,
4493 attempt to merge with the current got, or finish the current got
4494 and then make make the new got current. */
4495
4496 static bfd_boolean
4497 mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4498 struct mips_elf_got_per_bfd_arg *arg)
4499 {
4500 unsigned int estimate;
4501 int result;
4502
4503 if (!mips_elf_resolve_final_got_entries (arg->info, g))
4504 return FALSE;
4505
4506 /* Work out the number of page, local and TLS entries. */
4507 estimate = arg->max_pages;
4508 if (estimate > g->page_gotno)
4509 estimate = g->page_gotno;
4510 estimate += g->local_gotno + g->tls_gotno;
4511
4512 /* We place TLS GOT entries after both locals and globals. The globals
4513 for the primary GOT may overflow the normal GOT size limit, so be
4514 sure not to merge a GOT which requires TLS with the primary GOT in that
4515 case. This doesn't affect non-primary GOTs. */
4516 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
4517
4518 if (estimate <= arg->max_count)
4519 {
4520 /* If we don't have a primary GOT, use it as
4521 a starting point for the primary GOT. */
4522 if (!arg->primary)
4523 {
4524 arg->primary = g;
4525 return TRUE;
4526 }
4527
4528 /* Try merging with the primary GOT. */
4529 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
4530 if (result >= 0)
4531 return result;
4532 }
4533
4534 /* If we can merge with the last-created got, do it. */
4535 if (arg->current)
4536 {
4537 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
4538 if (result >= 0)
4539 return result;
4540 }
4541
4542 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4543 fits; if it turns out that it doesn't, we'll get relocation
4544 overflows anyway. */
4545 g->next = arg->current;
4546 arg->current = g;
4547
4548 return TRUE;
4549 }
4550
4551 /* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4552 to GOTIDX, duplicating the entry if it has already been assigned
4553 an index in a different GOT. */
4554
4555 static bfd_boolean
4556 mips_elf_set_gotidx (void **entryp, long gotidx)
4557 {
4558 struct mips_got_entry *entry;
4559
4560 entry = (struct mips_got_entry *) *entryp;
4561 if (entry->gotidx > 0)
4562 {
4563 struct mips_got_entry *new_entry;
4564
4565 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4566 if (!new_entry)
4567 return FALSE;
4568
4569 *new_entry = *entry;
4570 *entryp = new_entry;
4571 entry = new_entry;
4572 }
4573 entry->gotidx = gotidx;
4574 return TRUE;
4575 }
4576
4577 /* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4578 mips_elf_traverse_got_arg in which DATA->value is the size of one
4579 GOT entry. Set DATA->g to null on failure. */
4580
4581 static int
4582 mips_elf_initialize_tls_index (void **entryp, void *data)
4583 {
4584 struct mips_got_entry *entry;
4585 struct mips_elf_traverse_got_arg *arg;
4586
4587 /* We're only interested in TLS symbols. */
4588 entry = (struct mips_got_entry *) *entryp;
4589 if (entry->tls_type == GOT_TLS_NONE)
4590 return 1;
4591
4592 arg = (struct mips_elf_traverse_got_arg *) data;
4593 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
4594 {
4595 arg->g = NULL;
4596 return 0;
4597 }
4598
4599 /* Account for the entries we've just allocated. */
4600 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
4601 return 1;
4602 }
4603
4604 /* A htab_traverse callback for GOT entries, where DATA points to a
4605 mips_elf_traverse_got_arg. Set the global_got_area of each global
4606 symbol to DATA->value. */
4607
4608 static int
4609 mips_elf_set_global_got_area (void **entryp, void *data)
4610 {
4611 struct mips_got_entry *entry;
4612 struct mips_elf_traverse_got_arg *arg;
4613
4614 entry = (struct mips_got_entry *) *entryp;
4615 arg = (struct mips_elf_traverse_got_arg *) data;
4616 if (entry->abfd != NULL
4617 && entry->symndx == -1
4618 && entry->d.h->global_got_area != GGA_NONE)
4619 entry->d.h->global_got_area = arg->value;
4620 return 1;
4621 }
4622
4623 /* A htab_traverse callback for secondary GOT entries, where DATA points
4624 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4625 and record the number of relocations they require. DATA->value is
4626 the size of one GOT entry. Set DATA->g to null on failure. */
4627
4628 static int
4629 mips_elf_set_global_gotidx (void **entryp, void *data)
4630 {
4631 struct mips_got_entry *entry;
4632 struct mips_elf_traverse_got_arg *arg;
4633
4634 entry = (struct mips_got_entry *) *entryp;
4635 arg = (struct mips_elf_traverse_got_arg *) data;
4636 if (entry->abfd != NULL
4637 && entry->symndx == -1
4638 && entry->d.h->global_got_area != GGA_NONE)
4639 {
4640 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
4641 {
4642 arg->g = NULL;
4643 return 0;
4644 }
4645 arg->g->assigned_low_gotno += 1;
4646
4647 if (arg->info->shared
4648 || (elf_hash_table (arg->info)->dynamic_sections_created
4649 && entry->d.h->root.def_dynamic
4650 && !entry->d.h->root.def_regular))
4651 arg->g->relocs += 1;
4652 }
4653
4654 return 1;
4655 }
4656
4657 /* A htab_traverse callback for GOT entries for which DATA is the
4658 bfd_link_info. Forbid any global symbols from having traditional
4659 lazy-binding stubs. */
4660
4661 static int
4662 mips_elf_forbid_lazy_stubs (void **entryp, void *data)
4663 {
4664 struct bfd_link_info *info;
4665 struct mips_elf_link_hash_table *htab;
4666 struct mips_got_entry *entry;
4667
4668 entry = (struct mips_got_entry *) *entryp;
4669 info = (struct bfd_link_info *) data;
4670 htab = mips_elf_hash_table (info);
4671 BFD_ASSERT (htab != NULL);
4672
4673 if (entry->abfd != NULL
4674 && entry->symndx == -1
4675 && entry->d.h->needs_lazy_stub)
4676 {
4677 entry->d.h->needs_lazy_stub = FALSE;
4678 htab->lazy_stub_count--;
4679 }
4680
4681 return 1;
4682 }
4683
4684 /* Return the offset of an input bfd IBFD's GOT from the beginning of
4685 the primary GOT. */
4686 static bfd_vma
4687 mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
4688 {
4689 if (!g->next)
4690 return 0;
4691
4692 g = mips_elf_bfd_got (ibfd, FALSE);
4693 if (! g)
4694 return 0;
4695
4696 BFD_ASSERT (g->next);
4697
4698 g = g->next;
4699
4700 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4701 * MIPS_ELF_GOT_SIZE (abfd);
4702 }
4703
4704 /* Turn a single GOT that is too big for 16-bit addressing into
4705 a sequence of GOTs, each one 16-bit addressable. */
4706
4707 static bfd_boolean
4708 mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
4709 asection *got, bfd_size_type pages)
4710 {
4711 struct mips_elf_link_hash_table *htab;
4712 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
4713 struct mips_elf_traverse_got_arg tga;
4714 struct mips_got_info *g, *gg;
4715 unsigned int assign, needed_relocs;
4716 bfd *dynobj, *ibfd;
4717
4718 dynobj = elf_hash_table (info)->dynobj;
4719 htab = mips_elf_hash_table (info);
4720 BFD_ASSERT (htab != NULL);
4721
4722 g = htab->got_info;
4723
4724 got_per_bfd_arg.obfd = abfd;
4725 got_per_bfd_arg.info = info;
4726 got_per_bfd_arg.current = NULL;
4727 got_per_bfd_arg.primary = NULL;
4728 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
4729 / MIPS_ELF_GOT_SIZE (abfd))
4730 - htab->reserved_gotno);
4731 got_per_bfd_arg.max_pages = pages;
4732 /* The number of globals that will be included in the primary GOT.
4733 See the calls to mips_elf_set_global_got_area below for more
4734 information. */
4735 got_per_bfd_arg.global_count = g->global_gotno;
4736
4737 /* Try to merge the GOTs of input bfds together, as long as they
4738 don't seem to exceed the maximum GOT size, choosing one of them
4739 to be the primary GOT. */
4740 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4741 {
4742 gg = mips_elf_bfd_got (ibfd, FALSE);
4743 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4744 return FALSE;
4745 }
4746
4747 /* If we do not find any suitable primary GOT, create an empty one. */
4748 if (got_per_bfd_arg.primary == NULL)
4749 g->next = mips_elf_create_got_info (abfd);
4750 else
4751 g->next = got_per_bfd_arg.primary;
4752 g->next->next = got_per_bfd_arg.current;
4753
4754 /* GG is now the master GOT, and G is the primary GOT. */
4755 gg = g;
4756 g = g->next;
4757
4758 /* Map the output bfd to the primary got. That's what we're going
4759 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4760 didn't mark in check_relocs, and we want a quick way to find it.
4761 We can't just use gg->next because we're going to reverse the
4762 list. */
4763 mips_elf_replace_bfd_got (abfd, g);
4764
4765 /* Every symbol that is referenced in a dynamic relocation must be
4766 present in the primary GOT, so arrange for them to appear after
4767 those that are actually referenced. */
4768 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
4769 g->global_gotno = gg->global_gotno;
4770
4771 tga.info = info;
4772 tga.value = GGA_RELOC_ONLY;
4773 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4774 tga.value = GGA_NORMAL;
4775 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
4776
4777 /* Now go through the GOTs assigning them offset ranges.
4778 [assigned_low_gotno, local_gotno[ will be set to the range of local
4779 entries in each GOT. We can then compute the end of a GOT by
4780 adding local_gotno to global_gotno. We reverse the list and make
4781 it circular since then we'll be able to quickly compute the
4782 beginning of a GOT, by computing the end of its predecessor. To
4783 avoid special cases for the primary GOT, while still preserving
4784 assertions that are valid for both single- and multi-got links,
4785 we arrange for the main got struct to have the right number of
4786 global entries, but set its local_gotno such that the initial
4787 offset of the primary GOT is zero. Remember that the primary GOT
4788 will become the last item in the circular linked list, so it
4789 points back to the master GOT. */
4790 gg->local_gotno = -g->global_gotno;
4791 gg->global_gotno = g->global_gotno;
4792 gg->tls_gotno = 0;
4793 assign = 0;
4794 gg->next = gg;
4795
4796 do
4797 {
4798 struct mips_got_info *gn;
4799
4800 assign += htab->reserved_gotno;
4801 g->assigned_low_gotno = assign;
4802 g->local_gotno += assign;
4803 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
4804 g->assigned_high_gotno = g->local_gotno - 1;
4805 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4806
4807 /* Take g out of the direct list, and push it onto the reversed
4808 list that gg points to. g->next is guaranteed to be nonnull after
4809 this operation, as required by mips_elf_initialize_tls_index. */
4810 gn = g->next;
4811 g->next = gg->next;
4812 gg->next = g;
4813
4814 /* Set up any TLS entries. We always place the TLS entries after
4815 all non-TLS entries. */
4816 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
4817 tga.g = g;
4818 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4819 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4820 if (!tga.g)
4821 return FALSE;
4822 BFD_ASSERT (g->tls_assigned_gotno == assign);
4823
4824 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
4825 g = gn;
4826
4827 /* Forbid global symbols in every non-primary GOT from having
4828 lazy-binding stubs. */
4829 if (g)
4830 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
4831 }
4832 while (g);
4833
4834 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
4835
4836 needed_relocs = 0;
4837 for (g = gg->next; g && g->next != gg; g = g->next)
4838 {
4839 unsigned int save_assign;
4840
4841 /* Assign offsets to global GOT entries and count how many
4842 relocations they need. */
4843 save_assign = g->assigned_low_gotno;
4844 g->assigned_low_gotno = g->local_gotno;
4845 tga.info = info;
4846 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4847 tga.g = g;
4848 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
4849 if (!tga.g)
4850 return FALSE;
4851 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
4852 g->assigned_low_gotno = save_assign;
4853
4854 if (info->shared)
4855 {
4856 g->relocs += g->local_gotno - g->assigned_low_gotno;
4857 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
4858 + g->next->global_gotno
4859 + g->next->tls_gotno
4860 + htab->reserved_gotno);
4861 }
4862 needed_relocs += g->relocs;
4863 }
4864 needed_relocs += g->relocs;
4865
4866 if (needed_relocs)
4867 mips_elf_allocate_dynamic_relocations (dynobj, info,
4868 needed_relocs);
4869
4870 return TRUE;
4871 }
4872
4873 \f
4874 /* Returns the first relocation of type r_type found, beginning with
4875 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4876
4877 static const Elf_Internal_Rela *
4878 mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4879 const Elf_Internal_Rela *relocation,
4880 const Elf_Internal_Rela *relend)
4881 {
4882 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
4883
4884 while (relocation < relend)
4885 {
4886 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
4887 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
4888 return relocation;
4889
4890 ++relocation;
4891 }
4892
4893 /* We didn't find it. */
4894 return NULL;
4895 }
4896
4897 /* Return whether an input relocation is against a local symbol. */
4898
4899 static bfd_boolean
4900 mips_elf_local_relocation_p (bfd *input_bfd,
4901 const Elf_Internal_Rela *relocation,
4902 asection **local_sections)
4903 {
4904 unsigned long r_symndx;
4905 Elf_Internal_Shdr *symtab_hdr;
4906 size_t extsymoff;
4907
4908 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
4909 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4910 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
4911
4912 if (r_symndx < extsymoff)
4913 return TRUE;
4914 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
4915 return TRUE;
4916
4917 return FALSE;
4918 }
4919 \f
4920 /* Sign-extend VALUE, which has the indicated number of BITS. */
4921
4922 bfd_vma
4923 _bfd_mips_elf_sign_extend (bfd_vma value, int bits)
4924 {
4925 if (value & ((bfd_vma) 1 << (bits - 1)))
4926 /* VALUE is negative. */
4927 value |= ((bfd_vma) - 1) << bits;
4928
4929 return value;
4930 }
4931
4932 /* Return non-zero if the indicated VALUE has overflowed the maximum
4933 range expressible by a signed number with the indicated number of
4934 BITS. */
4935
4936 static bfd_boolean
4937 mips_elf_overflow_p (bfd_vma value, int bits)
4938 {
4939 bfd_signed_vma svalue = (bfd_signed_vma) value;
4940
4941 if (svalue > (1 << (bits - 1)) - 1)
4942 /* The value is too big. */
4943 return TRUE;
4944 else if (svalue < -(1 << (bits - 1)))
4945 /* The value is too small. */
4946 return TRUE;
4947
4948 /* All is well. */
4949 return FALSE;
4950 }
4951
4952 /* Calculate the %high function. */
4953
4954 static bfd_vma
4955 mips_elf_high (bfd_vma value)
4956 {
4957 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
4958 }
4959
4960 /* Calculate the %higher function. */
4961
4962 static bfd_vma
4963 mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
4964 {
4965 #ifdef BFD64
4966 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
4967 #else
4968 abort ();
4969 return MINUS_ONE;
4970 #endif
4971 }
4972
4973 /* Calculate the %highest function. */
4974
4975 static bfd_vma
4976 mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
4977 {
4978 #ifdef BFD64
4979 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
4980 #else
4981 abort ();
4982 return MINUS_ONE;
4983 #endif
4984 }
4985 \f
4986 /* Create the .compact_rel section. */
4987
4988 static bfd_boolean
4989 mips_elf_create_compact_rel_section
4990 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
4991 {
4992 flagword flags;
4993 register asection *s;
4994
4995 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
4996 {
4997 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
4998 | SEC_READONLY);
4999
5000 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
5001 if (s == NULL
5002 || ! bfd_set_section_alignment (abfd, s,
5003 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
5004 return FALSE;
5005
5006 s->size = sizeof (Elf32_External_compact_rel);
5007 }
5008
5009 return TRUE;
5010 }
5011
5012 /* Create the .got section to hold the global offset table. */
5013
5014 static bfd_boolean
5015 mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
5016 {
5017 flagword flags;
5018 register asection *s;
5019 struct elf_link_hash_entry *h;
5020 struct bfd_link_hash_entry *bh;
5021 struct mips_elf_link_hash_table *htab;
5022
5023 htab = mips_elf_hash_table (info);
5024 BFD_ASSERT (htab != NULL);
5025
5026 /* This function may be called more than once. */
5027 if (htab->sgot)
5028 return TRUE;
5029
5030 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5031 | SEC_LINKER_CREATED);
5032
5033 /* We have to use an alignment of 2**4 here because this is hardcoded
5034 in the function stub generation and in the linker script. */
5035 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
5036 if (s == NULL
5037 || ! bfd_set_section_alignment (abfd, s, 4))
5038 return FALSE;
5039 htab->sgot = s;
5040
5041 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5042 linker script because we don't want to define the symbol if we
5043 are not creating a global offset table. */
5044 bh = NULL;
5045 if (! (_bfd_generic_link_add_one_symbol
5046 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
5047 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
5048 return FALSE;
5049
5050 h = (struct elf_link_hash_entry *) bh;
5051 h->non_elf = 0;
5052 h->def_regular = 1;
5053 h->type = STT_OBJECT;
5054 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
5055 elf_hash_table (info)->hgot = h;
5056
5057 if (info->shared
5058 && ! bfd_elf_link_record_dynamic_symbol (info, h))
5059 return FALSE;
5060
5061 htab->got_info = mips_elf_create_got_info (abfd);
5062 mips_elf_section_data (s)->elf.this_hdr.sh_flags
5063 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5064
5065 /* We also need a .got.plt section when generating PLTs. */
5066 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5067 SEC_ALLOC | SEC_LOAD
5068 | SEC_HAS_CONTENTS
5069 | SEC_IN_MEMORY
5070 | SEC_LINKER_CREATED);
5071 if (s == NULL)
5072 return FALSE;
5073 htab->sgotplt = s;
5074
5075 return TRUE;
5076 }
5077 \f
5078 /* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5079 __GOTT_INDEX__ symbols. These symbols are only special for
5080 shared objects; they are not used in executables. */
5081
5082 static bfd_boolean
5083 is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5084 {
5085 return (mips_elf_hash_table (info)->is_vxworks
5086 && info->shared
5087 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5088 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5089 }
5090
5091 /* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5092 require an la25 stub. See also mips_elf_local_pic_function_p,
5093 which determines whether the destination function ever requires a
5094 stub. */
5095
5096 static bfd_boolean
5097 mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5098 bfd_boolean target_is_16_bit_code_p)
5099 {
5100 /* We specifically ignore branches and jumps from EF_PIC objects,
5101 where the onus is on the compiler or programmer to perform any
5102 necessary initialization of $25. Sometimes such initialization
5103 is unnecessary; for example, -mno-shared functions do not use
5104 the incoming value of $25, and may therefore be called directly. */
5105 if (PIC_OBJECT_P (input_bfd))
5106 return FALSE;
5107
5108 switch (r_type)
5109 {
5110 case R_MIPS_26:
5111 case R_MIPS_PC16:
5112 case R_MICROMIPS_26_S1:
5113 case R_MICROMIPS_PC7_S1:
5114 case R_MICROMIPS_PC10_S1:
5115 case R_MICROMIPS_PC16_S1:
5116 case R_MICROMIPS_PC23_S2:
5117 return TRUE;
5118
5119 case R_MIPS16_26:
5120 return !target_is_16_bit_code_p;
5121
5122 default:
5123 return FALSE;
5124 }
5125 }
5126 \f
5127 /* Calculate the value produced by the RELOCATION (which comes from
5128 the INPUT_BFD). The ADDEND is the addend to use for this
5129 RELOCATION; RELOCATION->R_ADDEND is ignored.
5130
5131 The result of the relocation calculation is stored in VALUEP.
5132 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
5133 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
5134
5135 This function returns bfd_reloc_continue if the caller need take no
5136 further action regarding this relocation, bfd_reloc_notsupported if
5137 something goes dramatically wrong, bfd_reloc_overflow if an
5138 overflow occurs, and bfd_reloc_ok to indicate success. */
5139
5140 static bfd_reloc_status_type
5141 mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
5142 asection *input_section,
5143 struct bfd_link_info *info,
5144 const Elf_Internal_Rela *relocation,
5145 bfd_vma addend, reloc_howto_type *howto,
5146 Elf_Internal_Sym *local_syms,
5147 asection **local_sections, bfd_vma *valuep,
5148 const char **namep,
5149 bfd_boolean *cross_mode_jump_p,
5150 bfd_boolean save_addend)
5151 {
5152 /* The eventual value we will return. */
5153 bfd_vma value;
5154 /* The address of the symbol against which the relocation is
5155 occurring. */
5156 bfd_vma symbol = 0;
5157 /* The final GP value to be used for the relocatable, executable, or
5158 shared object file being produced. */
5159 bfd_vma gp;
5160 /* The place (section offset or address) of the storage unit being
5161 relocated. */
5162 bfd_vma p;
5163 /* The value of GP used to create the relocatable object. */
5164 bfd_vma gp0;
5165 /* The offset into the global offset table at which the address of
5166 the relocation entry symbol, adjusted by the addend, resides
5167 during execution. */
5168 bfd_vma g = MINUS_ONE;
5169 /* The section in which the symbol referenced by the relocation is
5170 located. */
5171 asection *sec = NULL;
5172 struct mips_elf_link_hash_entry *h = NULL;
5173 /* TRUE if the symbol referred to by this relocation is a local
5174 symbol. */
5175 bfd_boolean local_p, was_local_p;
5176 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5177 bfd_boolean gp_disp_p = FALSE;
5178 /* TRUE if the symbol referred to by this relocation is
5179 "__gnu_local_gp". */
5180 bfd_boolean gnu_local_gp_p = FALSE;
5181 Elf_Internal_Shdr *symtab_hdr;
5182 size_t extsymoff;
5183 unsigned long r_symndx;
5184 int r_type;
5185 /* TRUE if overflow occurred during the calculation of the
5186 relocation value. */
5187 bfd_boolean overflowed_p;
5188 /* TRUE if this relocation refers to a MIPS16 function. */
5189 bfd_boolean target_is_16_bit_code_p = FALSE;
5190 bfd_boolean target_is_micromips_code_p = FALSE;
5191 struct mips_elf_link_hash_table *htab;
5192 bfd *dynobj;
5193
5194 dynobj = elf_hash_table (info)->dynobj;
5195 htab = mips_elf_hash_table (info);
5196 BFD_ASSERT (htab != NULL);
5197
5198 /* Parse the relocation. */
5199 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5200 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5201 p = (input_section->output_section->vma
5202 + input_section->output_offset
5203 + relocation->r_offset);
5204
5205 /* Assume that there will be no overflow. */
5206 overflowed_p = FALSE;
5207
5208 /* Figure out whether or not the symbol is local, and get the offset
5209 used in the array of hash table entries. */
5210 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5211 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
5212 local_sections);
5213 was_local_p = local_p;
5214 if (! elf_bad_symtab (input_bfd))
5215 extsymoff = symtab_hdr->sh_info;
5216 else
5217 {
5218 /* The symbol table does not follow the rule that local symbols
5219 must come before globals. */
5220 extsymoff = 0;
5221 }
5222
5223 /* Figure out the value of the symbol. */
5224 if (local_p)
5225 {
5226 Elf_Internal_Sym *sym;
5227
5228 sym = local_syms + r_symndx;
5229 sec = local_sections[r_symndx];
5230
5231 symbol = sec->output_section->vma + sec->output_offset;
5232 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION
5233 || (sec->flags & SEC_MERGE))
5234 symbol += sym->st_value;
5235 if ((sec->flags & SEC_MERGE)
5236 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5237 {
5238 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5239 addend -= symbol;
5240 addend += sec->output_section->vma + sec->output_offset;
5241 }
5242
5243 /* MIPS16/microMIPS text labels should be treated as odd. */
5244 if (ELF_ST_IS_COMPRESSED (sym->st_other))
5245 ++symbol;
5246
5247 /* Record the name of this symbol, for our caller. */
5248 *namep = bfd_elf_string_from_elf_section (input_bfd,
5249 symtab_hdr->sh_link,
5250 sym->st_name);
5251 if (*namep == '\0')
5252 *namep = bfd_section_name (input_bfd, sec);
5253
5254 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5255 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5256 }
5257 else
5258 {
5259 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5260
5261 /* For global symbols we look up the symbol in the hash-table. */
5262 h = ((struct mips_elf_link_hash_entry *)
5263 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5264 /* Find the real hash-table entry for this symbol. */
5265 while (h->root.root.type == bfd_link_hash_indirect
5266 || h->root.root.type == bfd_link_hash_warning)
5267 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5268
5269 /* Record the name of this symbol, for our caller. */
5270 *namep = h->root.root.root.string;
5271
5272 /* See if this is the special _gp_disp symbol. Note that such a
5273 symbol must always be a global symbol. */
5274 if (strcmp (*namep, "_gp_disp") == 0
5275 && ! NEWABI_P (input_bfd))
5276 {
5277 /* Relocations against _gp_disp are permitted only with
5278 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
5279 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
5280 return bfd_reloc_notsupported;
5281
5282 gp_disp_p = TRUE;
5283 }
5284 /* See if this is the special _gp symbol. Note that such a
5285 symbol must always be a global symbol. */
5286 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5287 gnu_local_gp_p = TRUE;
5288
5289
5290 /* If this symbol is defined, calculate its address. Note that
5291 _gp_disp is a magic symbol, always implicitly defined by the
5292 linker, so it's inappropriate to check to see whether or not
5293 its defined. */
5294 else if ((h->root.root.type == bfd_link_hash_defined
5295 || h->root.root.type == bfd_link_hash_defweak)
5296 && h->root.root.u.def.section)
5297 {
5298 sec = h->root.root.u.def.section;
5299 if (sec->output_section)
5300 symbol = (h->root.root.u.def.value
5301 + sec->output_section->vma
5302 + sec->output_offset);
5303 else
5304 symbol = h->root.root.u.def.value;
5305 }
5306 else if (h->root.root.type == bfd_link_hash_undefweak)
5307 /* We allow relocations against undefined weak symbols, giving
5308 it the value zero, so that you can undefined weak functions
5309 and check to see if they exist by looking at their
5310 addresses. */
5311 symbol = 0;
5312 else if (info->unresolved_syms_in_objects == RM_IGNORE
5313 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5314 symbol = 0;
5315 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5316 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
5317 {
5318 /* If this is a dynamic link, we should have created a
5319 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
5320 in in _bfd_mips_elf_create_dynamic_sections.
5321 Otherwise, we should define the symbol with a value of 0.
5322 FIXME: It should probably get into the symbol table
5323 somehow as well. */
5324 BFD_ASSERT (! info->shared);
5325 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5326 symbol = 0;
5327 }
5328 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5329 {
5330 /* This is an optional symbol - an Irix specific extension to the
5331 ELF spec. Ignore it for now.
5332 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5333 than simply ignoring them, but we do not handle this for now.
5334 For information see the "64-bit ELF Object File Specification"
5335 which is available from here:
5336 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5337 symbol = 0;
5338 }
5339 else if ((*info->callbacks->undefined_symbol)
5340 (info, h->root.root.root.string, input_bfd,
5341 input_section, relocation->r_offset,
5342 (info->unresolved_syms_in_objects == RM_GENERATE_ERROR)
5343 || ELF_ST_VISIBILITY (h->root.other)))
5344 {
5345 return bfd_reloc_undefined;
5346 }
5347 else
5348 {
5349 return bfd_reloc_notsupported;
5350 }
5351
5352 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
5353 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
5354 }
5355
5356 /* If this is a reference to a 16-bit function with a stub, we need
5357 to redirect the relocation to the stub unless:
5358
5359 (a) the relocation is for a MIPS16 JAL;
5360
5361 (b) the relocation is for a MIPS16 PIC call, and there are no
5362 non-MIPS16 uses of the GOT slot; or
5363
5364 (c) the section allows direct references to MIPS16 functions. */
5365 if (r_type != R_MIPS16_26
5366 && !info->relocatable
5367 && ((h != NULL
5368 && h->fn_stub != NULL
5369 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
5370 || (local_p
5371 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5372 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
5373 && !section_allows_mips16_refs_p (input_section))
5374 {
5375 /* This is a 32- or 64-bit call to a 16-bit function. We should
5376 have already noticed that we were going to need the
5377 stub. */
5378 if (local_p)
5379 {
5380 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
5381 value = 0;
5382 }
5383 else
5384 {
5385 BFD_ASSERT (h->need_fn_stub);
5386 if (h->la25_stub)
5387 {
5388 /* If a LA25 header for the stub itself exists, point to the
5389 prepended LUI/ADDIU sequence. */
5390 sec = h->la25_stub->stub_section;
5391 value = h->la25_stub->offset;
5392 }
5393 else
5394 {
5395 sec = h->fn_stub;
5396 value = 0;
5397 }
5398 }
5399
5400 symbol = sec->output_section->vma + sec->output_offset + value;
5401 /* The target is 16-bit, but the stub isn't. */
5402 target_is_16_bit_code_p = FALSE;
5403 }
5404 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5405 to a standard MIPS function, we need to redirect the call to the stub.
5406 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5407 indirect calls should use an indirect stub instead. */
5408 else if (r_type == R_MIPS16_26 && !info->relocatable
5409 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
5410 || (local_p
5411 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5412 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
5413 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
5414 {
5415 if (local_p)
5416 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
5417 else
5418 {
5419 /* If both call_stub and call_fp_stub are defined, we can figure
5420 out which one to use by checking which one appears in the input
5421 file. */
5422 if (h->call_stub != NULL && h->call_fp_stub != NULL)
5423 {
5424 asection *o;
5425
5426 sec = NULL;
5427 for (o = input_bfd->sections; o != NULL; o = o->next)
5428 {
5429 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5430 {
5431 sec = h->call_fp_stub;
5432 break;
5433 }
5434 }
5435 if (sec == NULL)
5436 sec = h->call_stub;
5437 }
5438 else if (h->call_stub != NULL)
5439 sec = h->call_stub;
5440 else
5441 sec = h->call_fp_stub;
5442 }
5443
5444 BFD_ASSERT (sec->size > 0);
5445 symbol = sec->output_section->vma + sec->output_offset;
5446 }
5447 /* If this is a direct call to a PIC function, redirect to the
5448 non-PIC stub. */
5449 else if (h != NULL && h->la25_stub
5450 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5451 target_is_16_bit_code_p))
5452 symbol = (h->la25_stub->stub_section->output_section->vma
5453 + h->la25_stub->stub_section->output_offset
5454 + h->la25_stub->offset);
5455 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5456 entry is used if a standard PLT entry has also been made. In this
5457 case the symbol will have been set by mips_elf_set_plt_sym_value
5458 to point to the standard PLT entry, so redirect to the compressed
5459 one. */
5460 else if ((r_type == R_MIPS16_26 || r_type == R_MICROMIPS_26_S1)
5461 && !info->relocatable
5462 && h != NULL
5463 && h->use_plt_entry
5464 && h->root.plt.plist->comp_offset != MINUS_ONE
5465 && h->root.plt.plist->mips_offset != MINUS_ONE)
5466 {
5467 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5468
5469 sec = htab->splt;
5470 symbol = (sec->output_section->vma
5471 + sec->output_offset
5472 + htab->plt_header_size
5473 + htab->plt_mips_offset
5474 + h->root.plt.plist->comp_offset
5475 + 1);
5476
5477 target_is_16_bit_code_p = !micromips_p;
5478 target_is_micromips_code_p = micromips_p;
5479 }
5480
5481 /* Make sure MIPS16 and microMIPS are not used together. */
5482 if ((r_type == R_MIPS16_26 && target_is_micromips_code_p)
5483 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5484 {
5485 (*_bfd_error_handler)
5486 (_("MIPS16 and microMIPS functions cannot call each other"));
5487 return bfd_reloc_notsupported;
5488 }
5489
5490 /* Calls from 16-bit code to 32-bit code and vice versa require the
5491 mode change. However, we can ignore calls to undefined weak symbols,
5492 which should never be executed at runtime. This exception is important
5493 because the assembly writer may have "known" that any definition of the
5494 symbol would be 16-bit code, and that direct jumps were therefore
5495 acceptable. */
5496 *cross_mode_jump_p = (!info->relocatable
5497 && !(h && h->root.root.type == bfd_link_hash_undefweak)
5498 && ((r_type == R_MIPS16_26 && !target_is_16_bit_code_p)
5499 || (r_type == R_MICROMIPS_26_S1
5500 && !target_is_micromips_code_p)
5501 || ((r_type == R_MIPS_26 || r_type == R_MIPS_JALR)
5502 && (target_is_16_bit_code_p
5503 || target_is_micromips_code_p))));
5504
5505 local_p = (h == NULL || mips_use_local_got_p (info, h));
5506
5507 gp0 = _bfd_get_gp_value (input_bfd);
5508 gp = _bfd_get_gp_value (abfd);
5509 if (htab->got_info)
5510 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
5511
5512 if (gnu_local_gp_p)
5513 symbol = gp;
5514
5515 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5516 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5517 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5518 if (got_page_reloc_p (r_type) && !local_p)
5519 {
5520 r_type = (micromips_reloc_p (r_type)
5521 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
5522 addend = 0;
5523 }
5524
5525 /* If we haven't already determined the GOT offset, and we're going
5526 to need it, get it now. */
5527 switch (r_type)
5528 {
5529 case R_MIPS16_CALL16:
5530 case R_MIPS16_GOT16:
5531 case R_MIPS_CALL16:
5532 case R_MIPS_GOT16:
5533 case R_MIPS_GOT_DISP:
5534 case R_MIPS_GOT_HI16:
5535 case R_MIPS_CALL_HI16:
5536 case R_MIPS_GOT_LO16:
5537 case R_MIPS_CALL_LO16:
5538 case R_MICROMIPS_CALL16:
5539 case R_MICROMIPS_GOT16:
5540 case R_MICROMIPS_GOT_DISP:
5541 case R_MICROMIPS_GOT_HI16:
5542 case R_MICROMIPS_CALL_HI16:
5543 case R_MICROMIPS_GOT_LO16:
5544 case R_MICROMIPS_CALL_LO16:
5545 case R_MIPS_TLS_GD:
5546 case R_MIPS_TLS_GOTTPREL:
5547 case R_MIPS_TLS_LDM:
5548 case R_MIPS16_TLS_GD:
5549 case R_MIPS16_TLS_GOTTPREL:
5550 case R_MIPS16_TLS_LDM:
5551 case R_MICROMIPS_TLS_GD:
5552 case R_MICROMIPS_TLS_GOTTPREL:
5553 case R_MICROMIPS_TLS_LDM:
5554 /* Find the index into the GOT where this value is located. */
5555 if (tls_ldm_reloc_p (r_type))
5556 {
5557 g = mips_elf_local_got_index (abfd, input_bfd, info,
5558 0, 0, NULL, r_type);
5559 if (g == MINUS_ONE)
5560 return bfd_reloc_outofrange;
5561 }
5562 else if (!local_p)
5563 {
5564 /* On VxWorks, CALL relocations should refer to the .got.plt
5565 entry, which is initialized to point at the PLT stub. */
5566 if (htab->is_vxworks
5567 && (call_hi16_reloc_p (r_type)
5568 || call_lo16_reloc_p (r_type)
5569 || call16_reloc_p (r_type)))
5570 {
5571 BFD_ASSERT (addend == 0);
5572 BFD_ASSERT (h->root.needs_plt);
5573 g = mips_elf_gotplt_index (info, &h->root);
5574 }
5575 else
5576 {
5577 BFD_ASSERT (addend == 0);
5578 g = mips_elf_global_got_index (abfd, info, input_bfd,
5579 &h->root, r_type);
5580 if (!TLS_RELOC_P (r_type)
5581 && !elf_hash_table (info)->dynamic_sections_created)
5582 /* This is a static link. We must initialize the GOT entry. */
5583 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->sgot->contents + g);
5584 }
5585 }
5586 else if (!htab->is_vxworks
5587 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
5588 /* The calculation below does not involve "g". */
5589 break;
5590 else
5591 {
5592 g = mips_elf_local_got_index (abfd, input_bfd, info,
5593 symbol + addend, r_symndx, h, r_type);
5594 if (g == MINUS_ONE)
5595 return bfd_reloc_outofrange;
5596 }
5597
5598 /* Convert GOT indices to actual offsets. */
5599 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
5600 break;
5601 }
5602
5603 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5604 symbols are resolved by the loader. Add them to .rela.dyn. */
5605 if (h != NULL && is_gott_symbol (info, &h->root))
5606 {
5607 Elf_Internal_Rela outrel;
5608 bfd_byte *loc;
5609 asection *s;
5610
5611 s = mips_elf_rel_dyn_section (info, FALSE);
5612 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5613
5614 outrel.r_offset = (input_section->output_section->vma
5615 + input_section->output_offset
5616 + relocation->r_offset);
5617 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5618 outrel.r_addend = addend;
5619 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
5620
5621 /* If we've written this relocation for a readonly section,
5622 we need to set DF_TEXTREL again, so that we do not delete the
5623 DT_TEXTREL tag. */
5624 if (MIPS_ELF_READONLY_SECTION (input_section))
5625 info->flags |= DF_TEXTREL;
5626
5627 *valuep = 0;
5628 return bfd_reloc_ok;
5629 }
5630
5631 /* Figure out what kind of relocation is being performed. */
5632 switch (r_type)
5633 {
5634 case R_MIPS_NONE:
5635 return bfd_reloc_continue;
5636
5637 case R_MIPS_16:
5638 value = symbol + _bfd_mips_elf_sign_extend (addend, 16);
5639 overflowed_p = mips_elf_overflow_p (value, 16);
5640 break;
5641
5642 case R_MIPS_32:
5643 case R_MIPS_REL32:
5644 case R_MIPS_64:
5645 if ((info->shared
5646 || (htab->root.dynamic_sections_created
5647 && h != NULL
5648 && h->root.def_dynamic
5649 && !h->root.def_regular
5650 && !h->has_static_relocs))
5651 && r_symndx != STN_UNDEF
5652 && (h == NULL
5653 || h->root.root.type != bfd_link_hash_undefweak
5654 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5655 && (input_section->flags & SEC_ALLOC) != 0)
5656 {
5657 /* If we're creating a shared library, then we can't know
5658 where the symbol will end up. So, we create a relocation
5659 record in the output, and leave the job up to the dynamic
5660 linker. We must do the same for executable references to
5661 shared library symbols, unless we've decided to use copy
5662 relocs or PLTs instead. */
5663 value = addend;
5664 if (!mips_elf_create_dynamic_relocation (abfd,
5665 info,
5666 relocation,
5667 h,
5668 sec,
5669 symbol,
5670 &value,
5671 input_section))
5672 return bfd_reloc_undefined;
5673 }
5674 else
5675 {
5676 if (r_type != R_MIPS_REL32)
5677 value = symbol + addend;
5678 else
5679 value = addend;
5680 }
5681 value &= howto->dst_mask;
5682 break;
5683
5684 case R_MIPS_PC32:
5685 value = symbol + addend - p;
5686 value &= howto->dst_mask;
5687 break;
5688
5689 case R_MIPS16_26:
5690 /* The calculation for R_MIPS16_26 is just the same as for an
5691 R_MIPS_26. It's only the storage of the relocated field into
5692 the output file that's different. That's handled in
5693 mips_elf_perform_relocation. So, we just fall through to the
5694 R_MIPS_26 case here. */
5695 case R_MIPS_26:
5696 case R_MICROMIPS_26_S1:
5697 {
5698 unsigned int shift;
5699
5700 /* Make sure the target of JALX is word-aligned. Bit 0 must be
5701 the correct ISA mode selector and bit 1 must be 0. */
5702 if (*cross_mode_jump_p && (symbol & 3) != (r_type == R_MIPS_26))
5703 return bfd_reloc_outofrange;
5704
5705 /* Shift is 2, unusually, for microMIPS JALX. */
5706 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5707
5708 if (was_local_p)
5709 value = addend | ((p + 4) & (0xfc000000 << shift));
5710 else
5711 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
5712 value = (value + symbol) >> shift;
5713 if (!was_local_p && h->root.root.type != bfd_link_hash_undefweak)
5714 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
5715 value &= howto->dst_mask;
5716 }
5717 break;
5718
5719 case R_MIPS_TLS_DTPREL_HI16:
5720 case R_MIPS16_TLS_DTPREL_HI16:
5721 case R_MICROMIPS_TLS_DTPREL_HI16:
5722 value = (mips_elf_high (addend + symbol - dtprel_base (info))
5723 & howto->dst_mask);
5724 break;
5725
5726 case R_MIPS_TLS_DTPREL_LO16:
5727 case R_MIPS_TLS_DTPREL32:
5728 case R_MIPS_TLS_DTPREL64:
5729 case R_MIPS16_TLS_DTPREL_LO16:
5730 case R_MICROMIPS_TLS_DTPREL_LO16:
5731 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
5732 break;
5733
5734 case R_MIPS_TLS_TPREL_HI16:
5735 case R_MIPS16_TLS_TPREL_HI16:
5736 case R_MICROMIPS_TLS_TPREL_HI16:
5737 value = (mips_elf_high (addend + symbol - tprel_base (info))
5738 & howto->dst_mask);
5739 break;
5740
5741 case R_MIPS_TLS_TPREL_LO16:
5742 case R_MIPS_TLS_TPREL32:
5743 case R_MIPS_TLS_TPREL64:
5744 case R_MIPS16_TLS_TPREL_LO16:
5745 case R_MICROMIPS_TLS_TPREL_LO16:
5746 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
5747 break;
5748
5749 case R_MIPS_HI16:
5750 case R_MIPS16_HI16:
5751 case R_MICROMIPS_HI16:
5752 if (!gp_disp_p)
5753 {
5754 value = mips_elf_high (addend + symbol);
5755 value &= howto->dst_mask;
5756 }
5757 else
5758 {
5759 /* For MIPS16 ABI code we generate this sequence
5760 0: li $v0,%hi(_gp_disp)
5761 4: addiupc $v1,%lo(_gp_disp)
5762 8: sll $v0,16
5763 12: addu $v0,$v1
5764 14: move $gp,$v0
5765 So the offsets of hi and lo relocs are the same, but the
5766 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
5767 ADDIUPC clears the low two bits of the instruction address,
5768 so the base is ($t9 + 4) & ~3. */
5769 if (r_type == R_MIPS16_HI16)
5770 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
5771 /* The microMIPS .cpload sequence uses the same assembly
5772 instructions as the traditional psABI version, but the
5773 incoming $t9 has the low bit set. */
5774 else if (r_type == R_MICROMIPS_HI16)
5775 value = mips_elf_high (addend + gp - p - 1);
5776 else
5777 value = mips_elf_high (addend + gp - p);
5778 overflowed_p = mips_elf_overflow_p (value, 16);
5779 }
5780 break;
5781
5782 case R_MIPS_LO16:
5783 case R_MIPS16_LO16:
5784 case R_MICROMIPS_LO16:
5785 case R_MICROMIPS_HI0_LO16:
5786 if (!gp_disp_p)
5787 value = (symbol + addend) & howto->dst_mask;
5788 else
5789 {
5790 /* See the comment for R_MIPS16_HI16 above for the reason
5791 for this conditional. */
5792 if (r_type == R_MIPS16_LO16)
5793 value = addend + gp - (p & ~(bfd_vma) 0x3);
5794 else if (r_type == R_MICROMIPS_LO16
5795 || r_type == R_MICROMIPS_HI0_LO16)
5796 value = addend + gp - p + 3;
5797 else
5798 value = addend + gp - p + 4;
5799 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
5800 for overflow. But, on, say, IRIX5, relocations against
5801 _gp_disp are normally generated from the .cpload
5802 pseudo-op. It generates code that normally looks like
5803 this:
5804
5805 lui $gp,%hi(_gp_disp)
5806 addiu $gp,$gp,%lo(_gp_disp)
5807 addu $gp,$gp,$t9
5808
5809 Here $t9 holds the address of the function being called,
5810 as required by the MIPS ELF ABI. The R_MIPS_LO16
5811 relocation can easily overflow in this situation, but the
5812 R_MIPS_HI16 relocation will handle the overflow.
5813 Therefore, we consider this a bug in the MIPS ABI, and do
5814 not check for overflow here. */
5815 }
5816 break;
5817
5818 case R_MIPS_LITERAL:
5819 case R_MICROMIPS_LITERAL:
5820 /* Because we don't merge literal sections, we can handle this
5821 just like R_MIPS_GPREL16. In the long run, we should merge
5822 shared literals, and then we will need to additional work
5823 here. */
5824
5825 /* Fall through. */
5826
5827 case R_MIPS16_GPREL:
5828 /* The R_MIPS16_GPREL performs the same calculation as
5829 R_MIPS_GPREL16, but stores the relocated bits in a different
5830 order. We don't need to do anything special here; the
5831 differences are handled in mips_elf_perform_relocation. */
5832 case R_MIPS_GPREL16:
5833 case R_MICROMIPS_GPREL7_S2:
5834 case R_MICROMIPS_GPREL16:
5835 /* Only sign-extend the addend if it was extracted from the
5836 instruction. If the addend was separate, leave it alone,
5837 otherwise we may lose significant bits. */
5838 if (howto->partial_inplace)
5839 addend = _bfd_mips_elf_sign_extend (addend, 16);
5840 value = symbol + addend - gp;
5841 /* If the symbol was local, any earlier relocatable links will
5842 have adjusted its addend with the gp offset, so compensate
5843 for that now. Don't do it for symbols forced local in this
5844 link, though, since they won't have had the gp offset applied
5845 to them before. */
5846 if (was_local_p)
5847 value += gp0;
5848 overflowed_p = mips_elf_overflow_p (value, 16);
5849 break;
5850
5851 case R_MIPS16_GOT16:
5852 case R_MIPS16_CALL16:
5853 case R_MIPS_GOT16:
5854 case R_MIPS_CALL16:
5855 case R_MICROMIPS_GOT16:
5856 case R_MICROMIPS_CALL16:
5857 /* VxWorks does not have separate local and global semantics for
5858 R_MIPS*_GOT16; every relocation evaluates to "G". */
5859 if (!htab->is_vxworks && local_p)
5860 {
5861 value = mips_elf_got16_entry (abfd, input_bfd, info,
5862 symbol + addend, !was_local_p);
5863 if (value == MINUS_ONE)
5864 return bfd_reloc_outofrange;
5865 value
5866 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5867 overflowed_p = mips_elf_overflow_p (value, 16);
5868 break;
5869 }
5870
5871 /* Fall through. */
5872
5873 case R_MIPS_TLS_GD:
5874 case R_MIPS_TLS_GOTTPREL:
5875 case R_MIPS_TLS_LDM:
5876 case R_MIPS_GOT_DISP:
5877 case R_MIPS16_TLS_GD:
5878 case R_MIPS16_TLS_GOTTPREL:
5879 case R_MIPS16_TLS_LDM:
5880 case R_MICROMIPS_TLS_GD:
5881 case R_MICROMIPS_TLS_GOTTPREL:
5882 case R_MICROMIPS_TLS_LDM:
5883 case R_MICROMIPS_GOT_DISP:
5884 value = g;
5885 overflowed_p = mips_elf_overflow_p (value, 16);
5886 break;
5887
5888 case R_MIPS_GPREL32:
5889 value = (addend + symbol + gp0 - gp);
5890 if (!save_addend)
5891 value &= howto->dst_mask;
5892 break;
5893
5894 case R_MIPS_PC16:
5895 case R_MIPS_GNU_REL16_S2:
5896 value = symbol + _bfd_mips_elf_sign_extend (addend, 18) - p;
5897 overflowed_p = mips_elf_overflow_p (value, 18);
5898 value >>= howto->rightshift;
5899 value &= howto->dst_mask;
5900 break;
5901
5902 case R_MICROMIPS_PC7_S1:
5903 value = symbol + _bfd_mips_elf_sign_extend (addend, 8) - p;
5904 overflowed_p = mips_elf_overflow_p (value, 8);
5905 value >>= howto->rightshift;
5906 value &= howto->dst_mask;
5907 break;
5908
5909 case R_MICROMIPS_PC10_S1:
5910 value = symbol + _bfd_mips_elf_sign_extend (addend, 11) - p;
5911 overflowed_p = mips_elf_overflow_p (value, 11);
5912 value >>= howto->rightshift;
5913 value &= howto->dst_mask;
5914 break;
5915
5916 case R_MICROMIPS_PC16_S1:
5917 value = symbol + _bfd_mips_elf_sign_extend (addend, 17) - p;
5918 overflowed_p = mips_elf_overflow_p (value, 17);
5919 value >>= howto->rightshift;
5920 value &= howto->dst_mask;
5921 break;
5922
5923 case R_MICROMIPS_PC23_S2:
5924 value = symbol + _bfd_mips_elf_sign_extend (addend, 25) - ((p | 3) ^ 3);
5925 overflowed_p = mips_elf_overflow_p (value, 25);
5926 value >>= howto->rightshift;
5927 value &= howto->dst_mask;
5928 break;
5929
5930 case R_MIPS_GOT_HI16:
5931 case R_MIPS_CALL_HI16:
5932 case R_MICROMIPS_GOT_HI16:
5933 case R_MICROMIPS_CALL_HI16:
5934 /* We're allowed to handle these two relocations identically.
5935 The dynamic linker is allowed to handle the CALL relocations
5936 differently by creating a lazy evaluation stub. */
5937 value = g;
5938 value = mips_elf_high (value);
5939 value &= howto->dst_mask;
5940 break;
5941
5942 case R_MIPS_GOT_LO16:
5943 case R_MIPS_CALL_LO16:
5944 case R_MICROMIPS_GOT_LO16:
5945 case R_MICROMIPS_CALL_LO16:
5946 value = g & howto->dst_mask;
5947 break;
5948
5949 case R_MIPS_GOT_PAGE:
5950 case R_MICROMIPS_GOT_PAGE:
5951 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
5952 if (value == MINUS_ONE)
5953 return bfd_reloc_outofrange;
5954 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
5955 overflowed_p = mips_elf_overflow_p (value, 16);
5956 break;
5957
5958 case R_MIPS_GOT_OFST:
5959 case R_MICROMIPS_GOT_OFST:
5960 if (local_p)
5961 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
5962 else
5963 value = addend;
5964 overflowed_p = mips_elf_overflow_p (value, 16);
5965 break;
5966
5967 case R_MIPS_SUB:
5968 case R_MICROMIPS_SUB:
5969 value = symbol - addend;
5970 value &= howto->dst_mask;
5971 break;
5972
5973 case R_MIPS_HIGHER:
5974 case R_MICROMIPS_HIGHER:
5975 value = mips_elf_higher (addend + symbol);
5976 value &= howto->dst_mask;
5977 break;
5978
5979 case R_MIPS_HIGHEST:
5980 case R_MICROMIPS_HIGHEST:
5981 value = mips_elf_highest (addend + symbol);
5982 value &= howto->dst_mask;
5983 break;
5984
5985 case R_MIPS_SCN_DISP:
5986 case R_MICROMIPS_SCN_DISP:
5987 value = symbol + addend - sec->output_offset;
5988 value &= howto->dst_mask;
5989 break;
5990
5991 case R_MIPS_JALR:
5992 case R_MICROMIPS_JALR:
5993 /* This relocation is only a hint. In some cases, we optimize
5994 it into a bal instruction. But we don't try to optimize
5995 when the symbol does not resolve locally. */
5996 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
5997 return bfd_reloc_continue;
5998 value = symbol + addend;
5999 break;
6000
6001 case R_MIPS_PJUMP:
6002 case R_MIPS_GNU_VTINHERIT:
6003 case R_MIPS_GNU_VTENTRY:
6004 /* We don't do anything with these at present. */
6005 return bfd_reloc_continue;
6006
6007 default:
6008 /* An unrecognized relocation type. */
6009 return bfd_reloc_notsupported;
6010 }
6011
6012 /* Store the VALUE for our caller. */
6013 *valuep = value;
6014 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6015 }
6016
6017 /* Obtain the field relocated by RELOCATION. */
6018
6019 static bfd_vma
6020 mips_elf_obtain_contents (reloc_howto_type *howto,
6021 const Elf_Internal_Rela *relocation,
6022 bfd *input_bfd, bfd_byte *contents)
6023 {
6024 bfd_vma x;
6025 bfd_byte *location = contents + relocation->r_offset;
6026
6027 /* Obtain the bytes. */
6028 x = bfd_get ((8 * bfd_get_reloc_size (howto)), input_bfd, location);
6029
6030 return x;
6031 }
6032
6033 /* It has been determined that the result of the RELOCATION is the
6034 VALUE. Use HOWTO to place VALUE into the output file at the
6035 appropriate position. The SECTION is the section to which the
6036 relocation applies.
6037 CROSS_MODE_JUMP_P is true if the relocation field
6038 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
6039
6040 Returns FALSE if anything goes wrong. */
6041
6042 static bfd_boolean
6043 mips_elf_perform_relocation (struct bfd_link_info *info,
6044 reloc_howto_type *howto,
6045 const Elf_Internal_Rela *relocation,
6046 bfd_vma value, bfd *input_bfd,
6047 asection *input_section, bfd_byte *contents,
6048 bfd_boolean cross_mode_jump_p)
6049 {
6050 bfd_vma x;
6051 bfd_byte *location;
6052 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6053
6054 /* Figure out where the relocation is occurring. */
6055 location = contents + relocation->r_offset;
6056
6057 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
6058
6059 /* Obtain the current value. */
6060 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6061
6062 /* Clear the field we are setting. */
6063 x &= ~howto->dst_mask;
6064
6065 /* Set the field. */
6066 x |= (value & howto->dst_mask);
6067
6068 /* If required, turn JAL into JALX. */
6069 if (cross_mode_jump_p && jal_reloc_p (r_type))
6070 {
6071 bfd_boolean ok;
6072 bfd_vma opcode = x >> 26;
6073 bfd_vma jalx_opcode;
6074
6075 /* Check to see if the opcode is already JAL or JALX. */
6076 if (r_type == R_MIPS16_26)
6077 {
6078 ok = ((opcode == 0x6) || (opcode == 0x7));
6079 jalx_opcode = 0x7;
6080 }
6081 else if (r_type == R_MICROMIPS_26_S1)
6082 {
6083 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6084 jalx_opcode = 0x3c;
6085 }
6086 else
6087 {
6088 ok = ((opcode == 0x3) || (opcode == 0x1d));
6089 jalx_opcode = 0x1d;
6090 }
6091
6092 /* If the opcode is not JAL or JALX, there's a problem. We cannot
6093 convert J or JALS to JALX. */
6094 if (!ok)
6095 {
6096 (*_bfd_error_handler)
6097 (_("%B: %A+0x%lx: Unsupported jump between ISA modes; consider recompiling with interlinking enabled."),
6098 input_bfd,
6099 input_section,
6100 (unsigned long) relocation->r_offset);
6101 bfd_set_error (bfd_error_bad_value);
6102 return FALSE;
6103 }
6104
6105 /* Make this the JALX opcode. */
6106 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6107 }
6108
6109 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6110 range. */
6111 if (!info->relocatable
6112 && !cross_mode_jump_p
6113 && ((JAL_TO_BAL_P (input_bfd)
6114 && r_type == R_MIPS_26
6115 && (x >> 26) == 0x3) /* jal addr */
6116 || (JALR_TO_BAL_P (input_bfd)
6117 && r_type == R_MIPS_JALR
6118 && x == 0x0320f809) /* jalr t9 */
6119 || (JR_TO_B_P (input_bfd)
6120 && r_type == R_MIPS_JALR
6121 && x == 0x03200008))) /* jr t9 */
6122 {
6123 bfd_vma addr;
6124 bfd_vma dest;
6125 bfd_signed_vma off;
6126
6127 addr = (input_section->output_section->vma
6128 + input_section->output_offset
6129 + relocation->r_offset
6130 + 4);
6131 if (r_type == R_MIPS_26)
6132 dest = (value << 2) | ((addr >> 28) << 28);
6133 else
6134 dest = value;
6135 off = dest - addr;
6136 if (off <= 0x1ffff && off >= -0x20000)
6137 {
6138 if (x == 0x03200008) /* jr t9 */
6139 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6140 else
6141 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6142 }
6143 }
6144
6145 /* Put the value into the output. */
6146 bfd_put (8 * bfd_get_reloc_size (howto), input_bfd, x, location);
6147
6148 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !info->relocatable,
6149 location);
6150
6151 return TRUE;
6152 }
6153 \f
6154 /* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6155 is the original relocation, which is now being transformed into a
6156 dynamic relocation. The ADDENDP is adjusted if necessary; the
6157 caller should store the result in place of the original addend. */
6158
6159 static bfd_boolean
6160 mips_elf_create_dynamic_relocation (bfd *output_bfd,
6161 struct bfd_link_info *info,
6162 const Elf_Internal_Rela *rel,
6163 struct mips_elf_link_hash_entry *h,
6164 asection *sec, bfd_vma symbol,
6165 bfd_vma *addendp, asection *input_section)
6166 {
6167 Elf_Internal_Rela outrel[3];
6168 asection *sreloc;
6169 bfd *dynobj;
6170 int r_type;
6171 long indx;
6172 bfd_boolean defined_p;
6173 struct mips_elf_link_hash_table *htab;
6174
6175 htab = mips_elf_hash_table (info);
6176 BFD_ASSERT (htab != NULL);
6177
6178 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6179 dynobj = elf_hash_table (info)->dynobj;
6180 sreloc = mips_elf_rel_dyn_section (info, FALSE);
6181 BFD_ASSERT (sreloc != NULL);
6182 BFD_ASSERT (sreloc->contents != NULL);
6183 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
6184 < sreloc->size);
6185
6186 outrel[0].r_offset =
6187 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
6188 if (ABI_64_P (output_bfd))
6189 {
6190 outrel[1].r_offset =
6191 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6192 outrel[2].r_offset =
6193 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6194 }
6195
6196 if (outrel[0].r_offset == MINUS_ONE)
6197 /* The relocation field has been deleted. */
6198 return TRUE;
6199
6200 if (outrel[0].r_offset == MINUS_TWO)
6201 {
6202 /* The relocation field has been converted into a relative value of
6203 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6204 the field to be fully relocated, so add in the symbol's value. */
6205 *addendp += symbol;
6206 return TRUE;
6207 }
6208
6209 /* We must now calculate the dynamic symbol table index to use
6210 in the relocation. */
6211 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
6212 {
6213 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
6214 indx = h->root.dynindx;
6215 if (SGI_COMPAT (output_bfd))
6216 defined_p = h->root.def_regular;
6217 else
6218 /* ??? glibc's ld.so just adds the final GOT entry to the
6219 relocation field. It therefore treats relocs against
6220 defined symbols in the same way as relocs against
6221 undefined symbols. */
6222 defined_p = FALSE;
6223 }
6224 else
6225 {
6226 if (sec != NULL && bfd_is_abs_section (sec))
6227 indx = 0;
6228 else if (sec == NULL || sec->owner == NULL)
6229 {
6230 bfd_set_error (bfd_error_bad_value);
6231 return FALSE;
6232 }
6233 else
6234 {
6235 indx = elf_section_data (sec->output_section)->dynindx;
6236 if (indx == 0)
6237 {
6238 asection *osec = htab->root.text_index_section;
6239 indx = elf_section_data (osec)->dynindx;
6240 }
6241 if (indx == 0)
6242 abort ();
6243 }
6244
6245 /* Instead of generating a relocation using the section
6246 symbol, we may as well make it a fully relative
6247 relocation. We want to avoid generating relocations to
6248 local symbols because we used to generate them
6249 incorrectly, without adding the original symbol value,
6250 which is mandated by the ABI for section symbols. In
6251 order to give dynamic loaders and applications time to
6252 phase out the incorrect use, we refrain from emitting
6253 section-relative relocations. It's not like they're
6254 useful, after all. This should be a bit more efficient
6255 as well. */
6256 /* ??? Although this behavior is compatible with glibc's ld.so,
6257 the ABI says that relocations against STN_UNDEF should have
6258 a symbol value of 0. Irix rld honors this, so relocations
6259 against STN_UNDEF have no effect. */
6260 if (!SGI_COMPAT (output_bfd))
6261 indx = 0;
6262 defined_p = TRUE;
6263 }
6264
6265 /* If the relocation was previously an absolute relocation and
6266 this symbol will not be referred to by the relocation, we must
6267 adjust it by the value we give it in the dynamic symbol table.
6268 Otherwise leave the job up to the dynamic linker. */
6269 if (defined_p && r_type != R_MIPS_REL32)
6270 *addendp += symbol;
6271
6272 if (htab->is_vxworks)
6273 /* VxWorks uses non-relative relocations for this. */
6274 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6275 else
6276 /* The relocation is always an REL32 relocation because we don't
6277 know where the shared library will wind up at load-time. */
6278 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6279 R_MIPS_REL32);
6280
6281 /* For strict adherence to the ABI specification, we should
6282 generate a R_MIPS_64 relocation record by itself before the
6283 _REL32/_64 record as well, such that the addend is read in as
6284 a 64-bit value (REL32 is a 32-bit relocation, after all).
6285 However, since none of the existing ELF64 MIPS dynamic
6286 loaders seems to care, we don't waste space with these
6287 artificial relocations. If this turns out to not be true,
6288 mips_elf_allocate_dynamic_relocation() should be tweaked so
6289 as to make room for a pair of dynamic relocations per
6290 invocation if ABI_64_P, and here we should generate an
6291 additional relocation record with R_MIPS_64 by itself for a
6292 NULL symbol before this relocation record. */
6293 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6294 ABI_64_P (output_bfd)
6295 ? R_MIPS_64
6296 : R_MIPS_NONE);
6297 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6298
6299 /* Adjust the output offset of the relocation to reference the
6300 correct location in the output file. */
6301 outrel[0].r_offset += (input_section->output_section->vma
6302 + input_section->output_offset);
6303 outrel[1].r_offset += (input_section->output_section->vma
6304 + input_section->output_offset);
6305 outrel[2].r_offset += (input_section->output_section->vma
6306 + input_section->output_offset);
6307
6308 /* Put the relocation back out. We have to use the special
6309 relocation outputter in the 64-bit case since the 64-bit
6310 relocation format is non-standard. */
6311 if (ABI_64_P (output_bfd))
6312 {
6313 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6314 (output_bfd, &outrel[0],
6315 (sreloc->contents
6316 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6317 }
6318 else if (htab->is_vxworks)
6319 {
6320 /* VxWorks uses RELA rather than REL dynamic relocations. */
6321 outrel[0].r_addend = *addendp;
6322 bfd_elf32_swap_reloca_out
6323 (output_bfd, &outrel[0],
6324 (sreloc->contents
6325 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6326 }
6327 else
6328 bfd_elf32_swap_reloc_out
6329 (output_bfd, &outrel[0],
6330 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
6331
6332 /* We've now added another relocation. */
6333 ++sreloc->reloc_count;
6334
6335 /* Make sure the output section is writable. The dynamic linker
6336 will be writing to it. */
6337 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6338 |= SHF_WRITE;
6339
6340 /* On IRIX5, make an entry of compact relocation info. */
6341 if (IRIX_COMPAT (output_bfd) == ict_irix5)
6342 {
6343 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
6344 bfd_byte *cr;
6345
6346 if (scpt)
6347 {
6348 Elf32_crinfo cptrel;
6349
6350 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6351 cptrel.vaddr = (rel->r_offset
6352 + input_section->output_section->vma
6353 + input_section->output_offset);
6354 if (r_type == R_MIPS_REL32)
6355 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6356 else
6357 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6358 mips_elf_set_cr_dist2to (cptrel, 0);
6359 cptrel.konst = *addendp;
6360
6361 cr = (scpt->contents
6362 + sizeof (Elf32_External_compact_rel));
6363 mips_elf_set_cr_relvaddr (cptrel, 0);
6364 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6365 ((Elf32_External_crinfo *) cr
6366 + scpt->reloc_count));
6367 ++scpt->reloc_count;
6368 }
6369 }
6370
6371 /* If we've written this relocation for a readonly section,
6372 we need to set DF_TEXTREL again, so that we do not delete the
6373 DT_TEXTREL tag. */
6374 if (MIPS_ELF_READONLY_SECTION (input_section))
6375 info->flags |= DF_TEXTREL;
6376
6377 return TRUE;
6378 }
6379 \f
6380 /* Return the MACH for a MIPS e_flags value. */
6381
6382 unsigned long
6383 _bfd_elf_mips_mach (flagword flags)
6384 {
6385 switch (flags & EF_MIPS_MACH)
6386 {
6387 case E_MIPS_MACH_3900:
6388 return bfd_mach_mips3900;
6389
6390 case E_MIPS_MACH_4010:
6391 return bfd_mach_mips4010;
6392
6393 case E_MIPS_MACH_4100:
6394 return bfd_mach_mips4100;
6395
6396 case E_MIPS_MACH_4111:
6397 return bfd_mach_mips4111;
6398
6399 case E_MIPS_MACH_4120:
6400 return bfd_mach_mips4120;
6401
6402 case E_MIPS_MACH_4650:
6403 return bfd_mach_mips4650;
6404
6405 case E_MIPS_MACH_5400:
6406 return bfd_mach_mips5400;
6407
6408 case E_MIPS_MACH_5500:
6409 return bfd_mach_mips5500;
6410
6411 case E_MIPS_MACH_5900:
6412 return bfd_mach_mips5900;
6413
6414 case E_MIPS_MACH_9000:
6415 return bfd_mach_mips9000;
6416
6417 case E_MIPS_MACH_SB1:
6418 return bfd_mach_mips_sb1;
6419
6420 case E_MIPS_MACH_LS2E:
6421 return bfd_mach_mips_loongson_2e;
6422
6423 case E_MIPS_MACH_LS2F:
6424 return bfd_mach_mips_loongson_2f;
6425
6426 case E_MIPS_MACH_LS3A:
6427 return bfd_mach_mips_loongson_3a;
6428
6429 case E_MIPS_MACH_OCTEON2:
6430 return bfd_mach_mips_octeon2;
6431
6432 case E_MIPS_MACH_OCTEON:
6433 return bfd_mach_mips_octeon;
6434
6435 case E_MIPS_MACH_XLR:
6436 return bfd_mach_mips_xlr;
6437
6438 default:
6439 switch (flags & EF_MIPS_ARCH)
6440 {
6441 default:
6442 case E_MIPS_ARCH_1:
6443 return bfd_mach_mips3000;
6444
6445 case E_MIPS_ARCH_2:
6446 return bfd_mach_mips6000;
6447
6448 case E_MIPS_ARCH_3:
6449 return bfd_mach_mips4000;
6450
6451 case E_MIPS_ARCH_4:
6452 return bfd_mach_mips8000;
6453
6454 case E_MIPS_ARCH_5:
6455 return bfd_mach_mips5;
6456
6457 case E_MIPS_ARCH_32:
6458 return bfd_mach_mipsisa32;
6459
6460 case E_MIPS_ARCH_64:
6461 return bfd_mach_mipsisa64;
6462
6463 case E_MIPS_ARCH_32R2:
6464 return bfd_mach_mipsisa32r2;
6465
6466 case E_MIPS_ARCH_64R2:
6467 return bfd_mach_mipsisa64r2;
6468 }
6469 }
6470
6471 return 0;
6472 }
6473
6474 /* Return printable name for ABI. */
6475
6476 static INLINE char *
6477 elf_mips_abi_name (bfd *abfd)
6478 {
6479 flagword flags;
6480
6481 flags = elf_elfheader (abfd)->e_flags;
6482 switch (flags & EF_MIPS_ABI)
6483 {
6484 case 0:
6485 if (ABI_N32_P (abfd))
6486 return "N32";
6487 else if (ABI_64_P (abfd))
6488 return "64";
6489 else
6490 return "none";
6491 case E_MIPS_ABI_O32:
6492 return "O32";
6493 case E_MIPS_ABI_O64:
6494 return "O64";
6495 case E_MIPS_ABI_EABI32:
6496 return "EABI32";
6497 case E_MIPS_ABI_EABI64:
6498 return "EABI64";
6499 default:
6500 return "unknown abi";
6501 }
6502 }
6503 \f
6504 /* MIPS ELF uses two common sections. One is the usual one, and the
6505 other is for small objects. All the small objects are kept
6506 together, and then referenced via the gp pointer, which yields
6507 faster assembler code. This is what we use for the small common
6508 section. This approach is copied from ecoff.c. */
6509 static asection mips_elf_scom_section;
6510 static asymbol mips_elf_scom_symbol;
6511 static asymbol *mips_elf_scom_symbol_ptr;
6512
6513 /* MIPS ELF also uses an acommon section, which represents an
6514 allocated common symbol which may be overridden by a
6515 definition in a shared library. */
6516 static asection mips_elf_acom_section;
6517 static asymbol mips_elf_acom_symbol;
6518 static asymbol *mips_elf_acom_symbol_ptr;
6519
6520 /* This is used for both the 32-bit and the 64-bit ABI. */
6521
6522 void
6523 _bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
6524 {
6525 elf_symbol_type *elfsym;
6526
6527 /* Handle the special MIPS section numbers that a symbol may use. */
6528 elfsym = (elf_symbol_type *) asym;
6529 switch (elfsym->internal_elf_sym.st_shndx)
6530 {
6531 case SHN_MIPS_ACOMMON:
6532 /* This section is used in a dynamically linked executable file.
6533 It is an allocated common section. The dynamic linker can
6534 either resolve these symbols to something in a shared
6535 library, or it can just leave them here. For our purposes,
6536 we can consider these symbols to be in a new section. */
6537 if (mips_elf_acom_section.name == NULL)
6538 {
6539 /* Initialize the acommon section. */
6540 mips_elf_acom_section.name = ".acommon";
6541 mips_elf_acom_section.flags = SEC_ALLOC;
6542 mips_elf_acom_section.output_section = &mips_elf_acom_section;
6543 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
6544 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
6545 mips_elf_acom_symbol.name = ".acommon";
6546 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
6547 mips_elf_acom_symbol.section = &mips_elf_acom_section;
6548 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
6549 }
6550 asym->section = &mips_elf_acom_section;
6551 break;
6552
6553 case SHN_COMMON:
6554 /* Common symbols less than the GP size are automatically
6555 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
6556 if (asym->value > elf_gp_size (abfd)
6557 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
6558 || IRIX_COMPAT (abfd) == ict_irix6)
6559 break;
6560 /* Fall through. */
6561 case SHN_MIPS_SCOMMON:
6562 if (mips_elf_scom_section.name == NULL)
6563 {
6564 /* Initialize the small common section. */
6565 mips_elf_scom_section.name = ".scommon";
6566 mips_elf_scom_section.flags = SEC_IS_COMMON;
6567 mips_elf_scom_section.output_section = &mips_elf_scom_section;
6568 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
6569 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
6570 mips_elf_scom_symbol.name = ".scommon";
6571 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
6572 mips_elf_scom_symbol.section = &mips_elf_scom_section;
6573 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
6574 }
6575 asym->section = &mips_elf_scom_section;
6576 asym->value = elfsym->internal_elf_sym.st_size;
6577 break;
6578
6579 case SHN_MIPS_SUNDEFINED:
6580 asym->section = bfd_und_section_ptr;
6581 break;
6582
6583 case SHN_MIPS_TEXT:
6584 {
6585 asection *section = bfd_get_section_by_name (abfd, ".text");
6586
6587 if (section != NULL)
6588 {
6589 asym->section = section;
6590 /* MIPS_TEXT is a bit special, the address is not an offset
6591 to the base of the .text section. So substract the section
6592 base address to make it an offset. */
6593 asym->value -= section->vma;
6594 }
6595 }
6596 break;
6597
6598 case SHN_MIPS_DATA:
6599 {
6600 asection *section = bfd_get_section_by_name (abfd, ".data");
6601
6602 if (section != NULL)
6603 {
6604 asym->section = section;
6605 /* MIPS_DATA is a bit special, the address is not an offset
6606 to the base of the .data section. So substract the section
6607 base address to make it an offset. */
6608 asym->value -= section->vma;
6609 }
6610 }
6611 break;
6612 }
6613
6614 /* If this is an odd-valued function symbol, assume it's a MIPS16
6615 or microMIPS one. */
6616 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
6617 && (asym->value & 1) != 0)
6618 {
6619 asym->value--;
6620 if (MICROMIPS_P (abfd))
6621 elfsym->internal_elf_sym.st_other
6622 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
6623 else
6624 elfsym->internal_elf_sym.st_other
6625 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
6626 }
6627 }
6628 \f
6629 /* Implement elf_backend_eh_frame_address_size. This differs from
6630 the default in the way it handles EABI64.
6631
6632 EABI64 was originally specified as an LP64 ABI, and that is what
6633 -mabi=eabi normally gives on a 64-bit target. However, gcc has
6634 historically accepted the combination of -mabi=eabi and -mlong32,
6635 and this ILP32 variation has become semi-official over time.
6636 Both forms use elf32 and have pointer-sized FDE addresses.
6637
6638 If an EABI object was generated by GCC 4.0 or above, it will have
6639 an empty .gcc_compiled_longXX section, where XX is the size of longs
6640 in bits. Unfortunately, ILP32 objects generated by earlier compilers
6641 have no special marking to distinguish them from LP64 objects.
6642
6643 We don't want users of the official LP64 ABI to be punished for the
6644 existence of the ILP32 variant, but at the same time, we don't want
6645 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
6646 We therefore take the following approach:
6647
6648 - If ABFD contains a .gcc_compiled_longXX section, use it to
6649 determine the pointer size.
6650
6651 - Otherwise check the type of the first relocation. Assume that
6652 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
6653
6654 - Otherwise punt.
6655
6656 The second check is enough to detect LP64 objects generated by pre-4.0
6657 compilers because, in the kind of output generated by those compilers,
6658 the first relocation will be associated with either a CIE personality
6659 routine or an FDE start address. Furthermore, the compilers never
6660 used a special (non-pointer) encoding for this ABI.
6661
6662 Checking the relocation type should also be safe because there is no
6663 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
6664 did so. */
6665
6666 unsigned int
6667 _bfd_mips_elf_eh_frame_address_size (bfd *abfd, asection *sec)
6668 {
6669 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
6670 return 8;
6671 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
6672 {
6673 bfd_boolean long32_p, long64_p;
6674
6675 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
6676 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
6677 if (long32_p && long64_p)
6678 return 0;
6679 if (long32_p)
6680 return 4;
6681 if (long64_p)
6682 return 8;
6683
6684 if (sec->reloc_count > 0
6685 && elf_section_data (sec)->relocs != NULL
6686 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
6687 == R_MIPS_64))
6688 return 8;
6689
6690 return 0;
6691 }
6692 return 4;
6693 }
6694 \f
6695 /* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
6696 relocations against two unnamed section symbols to resolve to the
6697 same address. For example, if we have code like:
6698
6699 lw $4,%got_disp(.data)($gp)
6700 lw $25,%got_disp(.text)($gp)
6701 jalr $25
6702
6703 then the linker will resolve both relocations to .data and the program
6704 will jump there rather than to .text.
6705
6706 We can work around this problem by giving names to local section symbols.
6707 This is also what the MIPSpro tools do. */
6708
6709 bfd_boolean
6710 _bfd_mips_elf_name_local_section_symbols (bfd *abfd)
6711 {
6712 return SGI_COMPAT (abfd);
6713 }
6714 \f
6715 /* Work over a section just before writing it out. This routine is
6716 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
6717 sections that need the SHF_MIPS_GPREL flag by name; there has to be
6718 a better way. */
6719
6720 bfd_boolean
6721 _bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
6722 {
6723 if (hdr->sh_type == SHT_MIPS_REGINFO
6724 && hdr->sh_size > 0)
6725 {
6726 bfd_byte buf[4];
6727
6728 BFD_ASSERT (hdr->sh_size == sizeof (Elf32_External_RegInfo));
6729 BFD_ASSERT (hdr->contents == NULL);
6730
6731 if (bfd_seek (abfd,
6732 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
6733 SEEK_SET) != 0)
6734 return FALSE;
6735 H_PUT_32 (abfd, elf_gp (abfd), buf);
6736 if (bfd_bwrite (buf, 4, abfd) != 4)
6737 return FALSE;
6738 }
6739
6740 if (hdr->sh_type == SHT_MIPS_OPTIONS
6741 && hdr->bfd_section != NULL
6742 && mips_elf_section_data (hdr->bfd_section) != NULL
6743 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
6744 {
6745 bfd_byte *contents, *l, *lend;
6746
6747 /* We stored the section contents in the tdata field in the
6748 set_section_contents routine. We save the section contents
6749 so that we don't have to read them again.
6750 At this point we know that elf_gp is set, so we can look
6751 through the section contents to see if there is an
6752 ODK_REGINFO structure. */
6753
6754 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
6755 l = contents;
6756 lend = contents + hdr->sh_size;
6757 while (l + sizeof (Elf_External_Options) <= lend)
6758 {
6759 Elf_Internal_Options intopt;
6760
6761 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6762 &intopt);
6763 if (intopt.size < sizeof (Elf_External_Options))
6764 {
6765 (*_bfd_error_handler)
6766 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6767 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6768 break;
6769 }
6770 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6771 {
6772 bfd_byte buf[8];
6773
6774 if (bfd_seek (abfd,
6775 (hdr->sh_offset
6776 + (l - contents)
6777 + sizeof (Elf_External_Options)
6778 + (sizeof (Elf64_External_RegInfo) - 8)),
6779 SEEK_SET) != 0)
6780 return FALSE;
6781 H_PUT_64 (abfd, elf_gp (abfd), buf);
6782 if (bfd_bwrite (buf, 8, abfd) != 8)
6783 return FALSE;
6784 }
6785 else if (intopt.kind == ODK_REGINFO)
6786 {
6787 bfd_byte buf[4];
6788
6789 if (bfd_seek (abfd,
6790 (hdr->sh_offset
6791 + (l - contents)
6792 + sizeof (Elf_External_Options)
6793 + (sizeof (Elf32_External_RegInfo) - 4)),
6794 SEEK_SET) != 0)
6795 return FALSE;
6796 H_PUT_32 (abfd, elf_gp (abfd), buf);
6797 if (bfd_bwrite (buf, 4, abfd) != 4)
6798 return FALSE;
6799 }
6800 l += intopt.size;
6801 }
6802 }
6803
6804 if (hdr->bfd_section != NULL)
6805 {
6806 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
6807
6808 /* .sbss is not handled specially here because the GNU/Linux
6809 prelinker can convert .sbss from NOBITS to PROGBITS and
6810 changing it back to NOBITS breaks the binary. The entry in
6811 _bfd_mips_elf_special_sections will ensure the correct flags
6812 are set on .sbss if BFD creates it without reading it from an
6813 input file, and without special handling here the flags set
6814 on it in an input file will be followed. */
6815 if (strcmp (name, ".sdata") == 0
6816 || strcmp (name, ".lit8") == 0
6817 || strcmp (name, ".lit4") == 0)
6818 {
6819 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
6820 hdr->sh_type = SHT_PROGBITS;
6821 }
6822 else if (strcmp (name, ".srdata") == 0)
6823 {
6824 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
6825 hdr->sh_type = SHT_PROGBITS;
6826 }
6827 else if (strcmp (name, ".compact_rel") == 0)
6828 {
6829 hdr->sh_flags = 0;
6830 hdr->sh_type = SHT_PROGBITS;
6831 }
6832 else if (strcmp (name, ".rtproc") == 0)
6833 {
6834 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
6835 {
6836 unsigned int adjust;
6837
6838 adjust = hdr->sh_size % hdr->sh_addralign;
6839 if (adjust != 0)
6840 hdr->sh_size += hdr->sh_addralign - adjust;
6841 }
6842 }
6843 }
6844
6845 return TRUE;
6846 }
6847
6848 /* Handle a MIPS specific section when reading an object file. This
6849 is called when elfcode.h finds a section with an unknown type.
6850 This routine supports both the 32-bit and 64-bit ELF ABI.
6851
6852 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
6853 how to. */
6854
6855 bfd_boolean
6856 _bfd_mips_elf_section_from_shdr (bfd *abfd,
6857 Elf_Internal_Shdr *hdr,
6858 const char *name,
6859 int shindex)
6860 {
6861 flagword flags = 0;
6862
6863 /* There ought to be a place to keep ELF backend specific flags, but
6864 at the moment there isn't one. We just keep track of the
6865 sections by their name, instead. Fortunately, the ABI gives
6866 suggested names for all the MIPS specific sections, so we will
6867 probably get away with this. */
6868 switch (hdr->sh_type)
6869 {
6870 case SHT_MIPS_LIBLIST:
6871 if (strcmp (name, ".liblist") != 0)
6872 return FALSE;
6873 break;
6874 case SHT_MIPS_MSYM:
6875 if (strcmp (name, ".msym") != 0)
6876 return FALSE;
6877 break;
6878 case SHT_MIPS_CONFLICT:
6879 if (strcmp (name, ".conflict") != 0)
6880 return FALSE;
6881 break;
6882 case SHT_MIPS_GPTAB:
6883 if (! CONST_STRNEQ (name, ".gptab."))
6884 return FALSE;
6885 break;
6886 case SHT_MIPS_UCODE:
6887 if (strcmp (name, ".ucode") != 0)
6888 return FALSE;
6889 break;
6890 case SHT_MIPS_DEBUG:
6891 if (strcmp (name, ".mdebug") != 0)
6892 return FALSE;
6893 flags = SEC_DEBUGGING;
6894 break;
6895 case SHT_MIPS_REGINFO:
6896 if (strcmp (name, ".reginfo") != 0
6897 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
6898 return FALSE;
6899 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
6900 break;
6901 case SHT_MIPS_IFACE:
6902 if (strcmp (name, ".MIPS.interfaces") != 0)
6903 return FALSE;
6904 break;
6905 case SHT_MIPS_CONTENT:
6906 if (! CONST_STRNEQ (name, ".MIPS.content"))
6907 return FALSE;
6908 break;
6909 case SHT_MIPS_OPTIONS:
6910 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
6911 return FALSE;
6912 break;
6913 case SHT_MIPS_DWARF:
6914 if (! CONST_STRNEQ (name, ".debug_")
6915 && ! CONST_STRNEQ (name, ".zdebug_"))
6916 return FALSE;
6917 break;
6918 case SHT_MIPS_SYMBOL_LIB:
6919 if (strcmp (name, ".MIPS.symlib") != 0)
6920 return FALSE;
6921 break;
6922 case SHT_MIPS_EVENTS:
6923 if (! CONST_STRNEQ (name, ".MIPS.events")
6924 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
6925 return FALSE;
6926 break;
6927 default:
6928 break;
6929 }
6930
6931 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6932 return FALSE;
6933
6934 if (flags)
6935 {
6936 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
6937 (bfd_get_section_flags (abfd,
6938 hdr->bfd_section)
6939 | flags)))
6940 return FALSE;
6941 }
6942
6943 /* FIXME: We should record sh_info for a .gptab section. */
6944
6945 /* For a .reginfo section, set the gp value in the tdata information
6946 from the contents of this section. We need the gp value while
6947 processing relocs, so we just get it now. The .reginfo section
6948 is not used in the 64-bit MIPS ELF ABI. */
6949 if (hdr->sh_type == SHT_MIPS_REGINFO)
6950 {
6951 Elf32_External_RegInfo ext;
6952 Elf32_RegInfo s;
6953
6954 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
6955 &ext, 0, sizeof ext))
6956 return FALSE;
6957 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
6958 elf_gp (abfd) = s.ri_gp_value;
6959 }
6960
6961 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
6962 set the gp value based on what we find. We may see both
6963 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
6964 they should agree. */
6965 if (hdr->sh_type == SHT_MIPS_OPTIONS)
6966 {
6967 bfd_byte *contents, *l, *lend;
6968
6969 contents = bfd_malloc (hdr->sh_size);
6970 if (contents == NULL)
6971 return FALSE;
6972 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
6973 0, hdr->sh_size))
6974 {
6975 free (contents);
6976 return FALSE;
6977 }
6978 l = contents;
6979 lend = contents + hdr->sh_size;
6980 while (l + sizeof (Elf_External_Options) <= lend)
6981 {
6982 Elf_Internal_Options intopt;
6983
6984 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
6985 &intopt);
6986 if (intopt.size < sizeof (Elf_External_Options))
6987 {
6988 (*_bfd_error_handler)
6989 (_("%B: Warning: bad `%s' option size %u smaller than its header"),
6990 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
6991 break;
6992 }
6993 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
6994 {
6995 Elf64_Internal_RegInfo intreg;
6996
6997 bfd_mips_elf64_swap_reginfo_in
6998 (abfd,
6999 ((Elf64_External_RegInfo *)
7000 (l + sizeof (Elf_External_Options))),
7001 &intreg);
7002 elf_gp (abfd) = intreg.ri_gp_value;
7003 }
7004 else if (intopt.kind == ODK_REGINFO)
7005 {
7006 Elf32_RegInfo intreg;
7007
7008 bfd_mips_elf32_swap_reginfo_in
7009 (abfd,
7010 ((Elf32_External_RegInfo *)
7011 (l + sizeof (Elf_External_Options))),
7012 &intreg);
7013 elf_gp (abfd) = intreg.ri_gp_value;
7014 }
7015 l += intopt.size;
7016 }
7017 free (contents);
7018 }
7019
7020 return TRUE;
7021 }
7022
7023 /* Set the correct type for a MIPS ELF section. We do this by the
7024 section name, which is a hack, but ought to work. This routine is
7025 used by both the 32-bit and the 64-bit ABI. */
7026
7027 bfd_boolean
7028 _bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
7029 {
7030 const char *name = bfd_get_section_name (abfd, sec);
7031
7032 if (strcmp (name, ".liblist") == 0)
7033 {
7034 hdr->sh_type = SHT_MIPS_LIBLIST;
7035 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
7036 /* The sh_link field is set in final_write_processing. */
7037 }
7038 else if (strcmp (name, ".conflict") == 0)
7039 hdr->sh_type = SHT_MIPS_CONFLICT;
7040 else if (CONST_STRNEQ (name, ".gptab."))
7041 {
7042 hdr->sh_type = SHT_MIPS_GPTAB;
7043 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7044 /* The sh_info field is set in final_write_processing. */
7045 }
7046 else if (strcmp (name, ".ucode") == 0)
7047 hdr->sh_type = SHT_MIPS_UCODE;
7048 else if (strcmp (name, ".mdebug") == 0)
7049 {
7050 hdr->sh_type = SHT_MIPS_DEBUG;
7051 /* In a shared object on IRIX 5.3, the .mdebug section has an
7052 entsize of 0. FIXME: Does this matter? */
7053 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7054 hdr->sh_entsize = 0;
7055 else
7056 hdr->sh_entsize = 1;
7057 }
7058 else if (strcmp (name, ".reginfo") == 0)
7059 {
7060 hdr->sh_type = SHT_MIPS_REGINFO;
7061 /* In a shared object on IRIX 5.3, the .reginfo section has an
7062 entsize of 0x18. FIXME: Does this matter? */
7063 if (SGI_COMPAT (abfd))
7064 {
7065 if ((abfd->flags & DYNAMIC) != 0)
7066 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7067 else
7068 hdr->sh_entsize = 1;
7069 }
7070 else
7071 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7072 }
7073 else if (SGI_COMPAT (abfd)
7074 && (strcmp (name, ".hash") == 0
7075 || strcmp (name, ".dynamic") == 0
7076 || strcmp (name, ".dynstr") == 0))
7077 {
7078 if (SGI_COMPAT (abfd))
7079 hdr->sh_entsize = 0;
7080 #if 0
7081 /* This isn't how the IRIX6 linker behaves. */
7082 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7083 #endif
7084 }
7085 else if (strcmp (name, ".got") == 0
7086 || strcmp (name, ".srdata") == 0
7087 || strcmp (name, ".sdata") == 0
7088 || strcmp (name, ".sbss") == 0
7089 || strcmp (name, ".lit4") == 0
7090 || strcmp (name, ".lit8") == 0)
7091 hdr->sh_flags |= SHF_MIPS_GPREL;
7092 else if (strcmp (name, ".MIPS.interfaces") == 0)
7093 {
7094 hdr->sh_type = SHT_MIPS_IFACE;
7095 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7096 }
7097 else if (CONST_STRNEQ (name, ".MIPS.content"))
7098 {
7099 hdr->sh_type = SHT_MIPS_CONTENT;
7100 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7101 /* The sh_info field is set in final_write_processing. */
7102 }
7103 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
7104 {
7105 hdr->sh_type = SHT_MIPS_OPTIONS;
7106 hdr->sh_entsize = 1;
7107 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7108 }
7109 else if (CONST_STRNEQ (name, ".debug_")
7110 || CONST_STRNEQ (name, ".zdebug_"))
7111 {
7112 hdr->sh_type = SHT_MIPS_DWARF;
7113
7114 /* Irix facilities such as libexc expect a single .debug_frame
7115 per executable, the system ones have NOSTRIP set and the linker
7116 doesn't merge sections with different flags so ... */
7117 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7118 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7119 }
7120 else if (strcmp (name, ".MIPS.symlib") == 0)
7121 {
7122 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7123 /* The sh_link and sh_info fields are set in
7124 final_write_processing. */
7125 }
7126 else if (CONST_STRNEQ (name, ".MIPS.events")
7127 || CONST_STRNEQ (name, ".MIPS.post_rel"))
7128 {
7129 hdr->sh_type = SHT_MIPS_EVENTS;
7130 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7131 /* The sh_link field is set in final_write_processing. */
7132 }
7133 else if (strcmp (name, ".msym") == 0)
7134 {
7135 hdr->sh_type = SHT_MIPS_MSYM;
7136 hdr->sh_flags |= SHF_ALLOC;
7137 hdr->sh_entsize = 8;
7138 }
7139
7140 /* The generic elf_fake_sections will set up REL_HDR using the default
7141 kind of relocations. We used to set up a second header for the
7142 non-default kind of relocations here, but only NewABI would use
7143 these, and the IRIX ld doesn't like resulting empty RELA sections.
7144 Thus we create those header only on demand now. */
7145
7146 return TRUE;
7147 }
7148
7149 /* Given a BFD section, try to locate the corresponding ELF section
7150 index. This is used by both the 32-bit and the 64-bit ABI.
7151 Actually, it's not clear to me that the 64-bit ABI supports these,
7152 but for non-PIC objects we will certainly want support for at least
7153 the .scommon section. */
7154
7155 bfd_boolean
7156 _bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7157 asection *sec, int *retval)
7158 {
7159 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7160 {
7161 *retval = SHN_MIPS_SCOMMON;
7162 return TRUE;
7163 }
7164 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7165 {
7166 *retval = SHN_MIPS_ACOMMON;
7167 return TRUE;
7168 }
7169 return FALSE;
7170 }
7171 \f
7172 /* Hook called by the linker routine which adds symbols from an object
7173 file. We must handle the special MIPS section numbers here. */
7174
7175 bfd_boolean
7176 _bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
7177 Elf_Internal_Sym *sym, const char **namep,
7178 flagword *flagsp ATTRIBUTE_UNUSED,
7179 asection **secp, bfd_vma *valp)
7180 {
7181 if (SGI_COMPAT (abfd)
7182 && (abfd->flags & DYNAMIC) != 0
7183 && strcmp (*namep, "_rld_new_interface") == 0)
7184 {
7185 /* Skip IRIX5 rld entry name. */
7186 *namep = NULL;
7187 return TRUE;
7188 }
7189
7190 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7191 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7192 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7193 a magic symbol resolved by the linker, we ignore this bogus definition
7194 of _gp_disp. New ABI objects do not suffer from this problem so this
7195 is not done for them. */
7196 if (!NEWABI_P(abfd)
7197 && (sym->st_shndx == SHN_ABS)
7198 && (strcmp (*namep, "_gp_disp") == 0))
7199 {
7200 *namep = NULL;
7201 return TRUE;
7202 }
7203
7204 switch (sym->st_shndx)
7205 {
7206 case SHN_COMMON:
7207 /* Common symbols less than the GP size are automatically
7208 treated as SHN_MIPS_SCOMMON symbols. */
7209 if (sym->st_size > elf_gp_size (abfd)
7210 || ELF_ST_TYPE (sym->st_info) == STT_TLS
7211 || IRIX_COMPAT (abfd) == ict_irix6)
7212 break;
7213 /* Fall through. */
7214 case SHN_MIPS_SCOMMON:
7215 *secp = bfd_make_section_old_way (abfd, ".scommon");
7216 (*secp)->flags |= SEC_IS_COMMON;
7217 *valp = sym->st_size;
7218 break;
7219
7220 case SHN_MIPS_TEXT:
7221 /* This section is used in a shared object. */
7222 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
7223 {
7224 asymbol *elf_text_symbol;
7225 asection *elf_text_section;
7226 bfd_size_type amt = sizeof (asection);
7227
7228 elf_text_section = bfd_zalloc (abfd, amt);
7229 if (elf_text_section == NULL)
7230 return FALSE;
7231
7232 amt = sizeof (asymbol);
7233 elf_text_symbol = bfd_zalloc (abfd, amt);
7234 if (elf_text_symbol == NULL)
7235 return FALSE;
7236
7237 /* Initialize the section. */
7238
7239 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7240 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
7241
7242 elf_text_section->symbol = elf_text_symbol;
7243 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
7244
7245 elf_text_section->name = ".text";
7246 elf_text_section->flags = SEC_NO_FLAGS;
7247 elf_text_section->output_section = NULL;
7248 elf_text_section->owner = abfd;
7249 elf_text_symbol->name = ".text";
7250 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7251 elf_text_symbol->section = elf_text_section;
7252 }
7253 /* This code used to do *secp = bfd_und_section_ptr if
7254 info->shared. I don't know why, and that doesn't make sense,
7255 so I took it out. */
7256 *secp = mips_elf_tdata (abfd)->elf_text_section;
7257 break;
7258
7259 case SHN_MIPS_ACOMMON:
7260 /* Fall through. XXX Can we treat this as allocated data? */
7261 case SHN_MIPS_DATA:
7262 /* This section is used in a shared object. */
7263 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
7264 {
7265 asymbol *elf_data_symbol;
7266 asection *elf_data_section;
7267 bfd_size_type amt = sizeof (asection);
7268
7269 elf_data_section = bfd_zalloc (abfd, amt);
7270 if (elf_data_section == NULL)
7271 return FALSE;
7272
7273 amt = sizeof (asymbol);
7274 elf_data_symbol = bfd_zalloc (abfd, amt);
7275 if (elf_data_symbol == NULL)
7276 return FALSE;
7277
7278 /* Initialize the section. */
7279
7280 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7281 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
7282
7283 elf_data_section->symbol = elf_data_symbol;
7284 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
7285
7286 elf_data_section->name = ".data";
7287 elf_data_section->flags = SEC_NO_FLAGS;
7288 elf_data_section->output_section = NULL;
7289 elf_data_section->owner = abfd;
7290 elf_data_symbol->name = ".data";
7291 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7292 elf_data_symbol->section = elf_data_section;
7293 }
7294 /* This code used to do *secp = bfd_und_section_ptr if
7295 info->shared. I don't know why, and that doesn't make sense,
7296 so I took it out. */
7297 *secp = mips_elf_tdata (abfd)->elf_data_section;
7298 break;
7299
7300 case SHN_MIPS_SUNDEFINED:
7301 *secp = bfd_und_section_ptr;
7302 break;
7303 }
7304
7305 if (SGI_COMPAT (abfd)
7306 && ! info->shared
7307 && info->output_bfd->xvec == abfd->xvec
7308 && strcmp (*namep, "__rld_obj_head") == 0)
7309 {
7310 struct elf_link_hash_entry *h;
7311 struct bfd_link_hash_entry *bh;
7312
7313 /* Mark __rld_obj_head as dynamic. */
7314 bh = NULL;
7315 if (! (_bfd_generic_link_add_one_symbol
7316 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
7317 get_elf_backend_data (abfd)->collect, &bh)))
7318 return FALSE;
7319
7320 h = (struct elf_link_hash_entry *) bh;
7321 h->non_elf = 0;
7322 h->def_regular = 1;
7323 h->type = STT_OBJECT;
7324
7325 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7326 return FALSE;
7327
7328 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
7329 mips_elf_hash_table (info)->rld_symbol = h;
7330 }
7331
7332 /* If this is a mips16 text symbol, add 1 to the value to make it
7333 odd. This will cause something like .word SYM to come up with
7334 the right value when it is loaded into the PC. */
7335 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7336 ++*valp;
7337
7338 return TRUE;
7339 }
7340
7341 /* This hook function is called before the linker writes out a global
7342 symbol. We mark symbols as small common if appropriate. This is
7343 also where we undo the increment of the value for a mips16 symbol. */
7344
7345 int
7346 _bfd_mips_elf_link_output_symbol_hook
7347 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7348 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7349 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7350 {
7351 /* If we see a common symbol, which implies a relocatable link, then
7352 if a symbol was small common in an input file, mark it as small
7353 common in the output file. */
7354 if (sym->st_shndx == SHN_COMMON
7355 && strcmp (input_sec->name, ".scommon") == 0)
7356 sym->st_shndx = SHN_MIPS_SCOMMON;
7357
7358 if (ELF_ST_IS_COMPRESSED (sym->st_other))
7359 sym->st_value &= ~1;
7360
7361 return 1;
7362 }
7363 \f
7364 /* Functions for the dynamic linker. */
7365
7366 /* Create dynamic sections when linking against a dynamic object. */
7367
7368 bfd_boolean
7369 _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
7370 {
7371 struct elf_link_hash_entry *h;
7372 struct bfd_link_hash_entry *bh;
7373 flagword flags;
7374 register asection *s;
7375 const char * const *namep;
7376 struct mips_elf_link_hash_table *htab;
7377
7378 htab = mips_elf_hash_table (info);
7379 BFD_ASSERT (htab != NULL);
7380
7381 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7382 | SEC_LINKER_CREATED | SEC_READONLY);
7383
7384 /* The psABI requires a read-only .dynamic section, but the VxWorks
7385 EABI doesn't. */
7386 if (!htab->is_vxworks)
7387 {
7388 s = bfd_get_linker_section (abfd, ".dynamic");
7389 if (s != NULL)
7390 {
7391 if (! bfd_set_section_flags (abfd, s, flags))
7392 return FALSE;
7393 }
7394 }
7395
7396 /* We need to create .got section. */
7397 if (!mips_elf_create_got_section (abfd, info))
7398 return FALSE;
7399
7400 if (! mips_elf_rel_dyn_section (info, TRUE))
7401 return FALSE;
7402
7403 /* Create .stub section. */
7404 s = bfd_make_section_anyway_with_flags (abfd,
7405 MIPS_ELF_STUB_SECTION_NAME (abfd),
7406 flags | SEC_CODE);
7407 if (s == NULL
7408 || ! bfd_set_section_alignment (abfd, s,
7409 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7410 return FALSE;
7411 htab->sstubs = s;
7412
7413 if (!mips_elf_hash_table (info)->use_rld_obj_head
7414 && !info->shared
7415 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
7416 {
7417 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7418 flags &~ (flagword) SEC_READONLY);
7419 if (s == NULL
7420 || ! bfd_set_section_alignment (abfd, s,
7421 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7422 return FALSE;
7423 }
7424
7425 /* On IRIX5, we adjust add some additional symbols and change the
7426 alignments of several sections. There is no ABI documentation
7427 indicating that this is necessary on IRIX6, nor any evidence that
7428 the linker takes such action. */
7429 if (IRIX_COMPAT (abfd) == ict_irix5)
7430 {
7431 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7432 {
7433 bh = NULL;
7434 if (! (_bfd_generic_link_add_one_symbol
7435 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7436 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7437 return FALSE;
7438
7439 h = (struct elf_link_hash_entry *) bh;
7440 h->non_elf = 0;
7441 h->def_regular = 1;
7442 h->type = STT_SECTION;
7443
7444 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7445 return FALSE;
7446 }
7447
7448 /* We need to create a .compact_rel section. */
7449 if (SGI_COMPAT (abfd))
7450 {
7451 if (!mips_elf_create_compact_rel_section (abfd, info))
7452 return FALSE;
7453 }
7454
7455 /* Change alignments of some sections. */
7456 s = bfd_get_linker_section (abfd, ".hash");
7457 if (s != NULL)
7458 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7459
7460 s = bfd_get_linker_section (abfd, ".dynsym");
7461 if (s != NULL)
7462 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7463
7464 s = bfd_get_linker_section (abfd, ".dynstr");
7465 if (s != NULL)
7466 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7467
7468 /* ??? */
7469 s = bfd_get_section_by_name (abfd, ".reginfo");
7470 if (s != NULL)
7471 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7472
7473 s = bfd_get_linker_section (abfd, ".dynamic");
7474 if (s != NULL)
7475 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7476 }
7477
7478 if (!info->shared)
7479 {
7480 const char *name;
7481
7482 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
7483 bh = NULL;
7484 if (!(_bfd_generic_link_add_one_symbol
7485 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
7486 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
7487 return FALSE;
7488
7489 h = (struct elf_link_hash_entry *) bh;
7490 h->non_elf = 0;
7491 h->def_regular = 1;
7492 h->type = STT_SECTION;
7493
7494 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7495 return FALSE;
7496
7497 if (! mips_elf_hash_table (info)->use_rld_obj_head)
7498 {
7499 /* __rld_map is a four byte word located in the .data section
7500 and is filled in by the rtld to contain a pointer to
7501 the _r_debug structure. Its symbol value will be set in
7502 _bfd_mips_elf_finish_dynamic_symbol. */
7503 s = bfd_get_linker_section (abfd, ".rld_map");
7504 BFD_ASSERT (s != NULL);
7505
7506 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
7507 bh = NULL;
7508 if (!(_bfd_generic_link_add_one_symbol
7509 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
7510 get_elf_backend_data (abfd)->collect, &bh)))
7511 return FALSE;
7512
7513 h = (struct elf_link_hash_entry *) bh;
7514 h->non_elf = 0;
7515 h->def_regular = 1;
7516 h->type = STT_OBJECT;
7517
7518 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7519 return FALSE;
7520 mips_elf_hash_table (info)->rld_symbol = h;
7521 }
7522 }
7523
7524 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
7525 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
7526 if (!_bfd_elf_create_dynamic_sections (abfd, info))
7527 return FALSE;
7528
7529 /* Cache the sections created above. */
7530 htab->splt = bfd_get_linker_section (abfd, ".plt");
7531 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
7532 if (htab->is_vxworks)
7533 {
7534 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
7535 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
7536 }
7537 else
7538 htab->srelplt = bfd_get_linker_section (abfd, ".rel.plt");
7539 if (!htab->sdynbss
7540 || (htab->is_vxworks && !htab->srelbss && !info->shared)
7541 || !htab->srelplt
7542 || !htab->splt)
7543 abort ();
7544
7545 /* Do the usual VxWorks handling. */
7546 if (htab->is_vxworks
7547 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
7548 return FALSE;
7549
7550 return TRUE;
7551 }
7552 \f
7553 /* Return true if relocation REL against section SEC is a REL rather than
7554 RELA relocation. RELOCS is the first relocation in the section and
7555 ABFD is the bfd that contains SEC. */
7556
7557 static bfd_boolean
7558 mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
7559 const Elf_Internal_Rela *relocs,
7560 const Elf_Internal_Rela *rel)
7561 {
7562 Elf_Internal_Shdr *rel_hdr;
7563 const struct elf_backend_data *bed;
7564
7565 /* To determine which flavor of relocation this is, we depend on the
7566 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
7567 rel_hdr = elf_section_data (sec)->rel.hdr;
7568 if (rel_hdr == NULL)
7569 return FALSE;
7570 bed = get_elf_backend_data (abfd);
7571 return ((size_t) (rel - relocs)
7572 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
7573 }
7574
7575 /* Read the addend for REL relocation REL, which belongs to bfd ABFD.
7576 HOWTO is the relocation's howto and CONTENTS points to the contents
7577 of the section that REL is against. */
7578
7579 static bfd_vma
7580 mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
7581 reloc_howto_type *howto, bfd_byte *contents)
7582 {
7583 bfd_byte *location;
7584 unsigned int r_type;
7585 bfd_vma addend;
7586
7587 r_type = ELF_R_TYPE (abfd, rel->r_info);
7588 location = contents + rel->r_offset;
7589
7590 /* Get the addend, which is stored in the input file. */
7591 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
7592 addend = mips_elf_obtain_contents (howto, rel, abfd, contents);
7593 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
7594
7595 return addend & howto->src_mask;
7596 }
7597
7598 /* REL is a relocation in ABFD that needs a partnering LO16 relocation
7599 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
7600 and update *ADDEND with the final addend. Return true on success
7601 or false if the LO16 could not be found. RELEND is the exclusive
7602 upper bound on the relocations for REL's section. */
7603
7604 static bfd_boolean
7605 mips_elf_add_lo16_rel_addend (bfd *abfd,
7606 const Elf_Internal_Rela *rel,
7607 const Elf_Internal_Rela *relend,
7608 bfd_byte *contents, bfd_vma *addend)
7609 {
7610 unsigned int r_type, lo16_type;
7611 const Elf_Internal_Rela *lo16_relocation;
7612 reloc_howto_type *lo16_howto;
7613 bfd_vma l;
7614
7615 r_type = ELF_R_TYPE (abfd, rel->r_info);
7616 if (mips16_reloc_p (r_type))
7617 lo16_type = R_MIPS16_LO16;
7618 else if (micromips_reloc_p (r_type))
7619 lo16_type = R_MICROMIPS_LO16;
7620 else
7621 lo16_type = R_MIPS_LO16;
7622
7623 /* The combined value is the sum of the HI16 addend, left-shifted by
7624 sixteen bits, and the LO16 addend, sign extended. (Usually, the
7625 code does a `lui' of the HI16 value, and then an `addiu' of the
7626 LO16 value.)
7627
7628 Scan ahead to find a matching LO16 relocation.
7629
7630 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
7631 be immediately following. However, for the IRIX6 ABI, the next
7632 relocation may be a composed relocation consisting of several
7633 relocations for the same address. In that case, the R_MIPS_LO16
7634 relocation may occur as one of these. We permit a similar
7635 extension in general, as that is useful for GCC.
7636
7637 In some cases GCC dead code elimination removes the LO16 but keeps
7638 the corresponding HI16. This is strictly speaking a violation of
7639 the ABI but not immediately harmful. */
7640 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
7641 if (lo16_relocation == NULL)
7642 return FALSE;
7643
7644 /* Obtain the addend kept there. */
7645 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
7646 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
7647
7648 l <<= lo16_howto->rightshift;
7649 l = _bfd_mips_elf_sign_extend (l, 16);
7650
7651 *addend <<= 16;
7652 *addend += l;
7653 return TRUE;
7654 }
7655
7656 /* Try to read the contents of section SEC in bfd ABFD. Return true and
7657 store the contents in *CONTENTS on success. Assume that *CONTENTS
7658 already holds the contents if it is nonull on entry. */
7659
7660 static bfd_boolean
7661 mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
7662 {
7663 if (*contents)
7664 return TRUE;
7665
7666 /* Get cached copy if it exists. */
7667 if (elf_section_data (sec)->this_hdr.contents != NULL)
7668 {
7669 *contents = elf_section_data (sec)->this_hdr.contents;
7670 return TRUE;
7671 }
7672
7673 return bfd_malloc_and_get_section (abfd, sec, contents);
7674 }
7675
7676 /* Make a new PLT record to keep internal data. */
7677
7678 static struct plt_entry *
7679 mips_elf_make_plt_record (bfd *abfd)
7680 {
7681 struct plt_entry *entry;
7682
7683 entry = bfd_zalloc (abfd, sizeof (*entry));
7684 if (entry == NULL)
7685 return NULL;
7686
7687 entry->stub_offset = MINUS_ONE;
7688 entry->mips_offset = MINUS_ONE;
7689 entry->comp_offset = MINUS_ONE;
7690 entry->gotplt_index = MINUS_ONE;
7691 return entry;
7692 }
7693
7694 /* Look through the relocs for a section during the first phase, and
7695 allocate space in the global offset table and record the need for
7696 standard MIPS and compressed procedure linkage table entries. */
7697
7698 bfd_boolean
7699 _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7700 asection *sec, const Elf_Internal_Rela *relocs)
7701 {
7702 const char *name;
7703 bfd *dynobj;
7704 Elf_Internal_Shdr *symtab_hdr;
7705 struct elf_link_hash_entry **sym_hashes;
7706 size_t extsymoff;
7707 const Elf_Internal_Rela *rel;
7708 const Elf_Internal_Rela *rel_end;
7709 asection *sreloc;
7710 const struct elf_backend_data *bed;
7711 struct mips_elf_link_hash_table *htab;
7712 bfd_byte *contents;
7713 bfd_vma addend;
7714 reloc_howto_type *howto;
7715
7716 if (info->relocatable)
7717 return TRUE;
7718
7719 htab = mips_elf_hash_table (info);
7720 BFD_ASSERT (htab != NULL);
7721
7722 dynobj = elf_hash_table (info)->dynobj;
7723 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7724 sym_hashes = elf_sym_hashes (abfd);
7725 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
7726
7727 bed = get_elf_backend_data (abfd);
7728 rel_end = relocs + sec->reloc_count * bed->s->int_rels_per_ext_rel;
7729
7730 /* Check for the mips16 stub sections. */
7731
7732 name = bfd_get_section_name (abfd, sec);
7733 if (FN_STUB_P (name))
7734 {
7735 unsigned long r_symndx;
7736
7737 /* Look at the relocation information to figure out which symbol
7738 this is for. */
7739
7740 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7741 if (r_symndx == 0)
7742 {
7743 (*_bfd_error_handler)
7744 (_("%B: Warning: cannot determine the target function for"
7745 " stub section `%s'"),
7746 abfd, name);
7747 bfd_set_error (bfd_error_bad_value);
7748 return FALSE;
7749 }
7750
7751 if (r_symndx < extsymoff
7752 || sym_hashes[r_symndx - extsymoff] == NULL)
7753 {
7754 asection *o;
7755
7756 /* This stub is for a local symbol. This stub will only be
7757 needed if there is some relocation in this BFD, other
7758 than a 16 bit function call, which refers to this symbol. */
7759 for (o = abfd->sections; o != NULL; o = o->next)
7760 {
7761 Elf_Internal_Rela *sec_relocs;
7762 const Elf_Internal_Rela *r, *rend;
7763
7764 /* We can ignore stub sections when looking for relocs. */
7765 if ((o->flags & SEC_RELOC) == 0
7766 || o->reloc_count == 0
7767 || section_allows_mips16_refs_p (o))
7768 continue;
7769
7770 sec_relocs
7771 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7772 info->keep_memory);
7773 if (sec_relocs == NULL)
7774 return FALSE;
7775
7776 rend = sec_relocs + o->reloc_count;
7777 for (r = sec_relocs; r < rend; r++)
7778 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7779 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
7780 break;
7781
7782 if (elf_section_data (o)->relocs != sec_relocs)
7783 free (sec_relocs);
7784
7785 if (r < rend)
7786 break;
7787 }
7788
7789 if (o == NULL)
7790 {
7791 /* There is no non-call reloc for this stub, so we do
7792 not need it. Since this function is called before
7793 the linker maps input sections to output sections, we
7794 can easily discard it by setting the SEC_EXCLUDE
7795 flag. */
7796 sec->flags |= SEC_EXCLUDE;
7797 return TRUE;
7798 }
7799
7800 /* Record this stub in an array of local symbol stubs for
7801 this BFD. */
7802 if (mips_elf_tdata (abfd)->local_stubs == NULL)
7803 {
7804 unsigned long symcount;
7805 asection **n;
7806 bfd_size_type amt;
7807
7808 if (elf_bad_symtab (abfd))
7809 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7810 else
7811 symcount = symtab_hdr->sh_info;
7812 amt = symcount * sizeof (asection *);
7813 n = bfd_zalloc (abfd, amt);
7814 if (n == NULL)
7815 return FALSE;
7816 mips_elf_tdata (abfd)->local_stubs = n;
7817 }
7818
7819 sec->flags |= SEC_KEEP;
7820 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
7821
7822 /* We don't need to set mips16_stubs_seen in this case.
7823 That flag is used to see whether we need to look through
7824 the global symbol table for stubs. We don't need to set
7825 it here, because we just have a local stub. */
7826 }
7827 else
7828 {
7829 struct mips_elf_link_hash_entry *h;
7830
7831 h = ((struct mips_elf_link_hash_entry *)
7832 sym_hashes[r_symndx - extsymoff]);
7833
7834 while (h->root.root.type == bfd_link_hash_indirect
7835 || h->root.root.type == bfd_link_hash_warning)
7836 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
7837
7838 /* H is the symbol this stub is for. */
7839
7840 /* If we already have an appropriate stub for this function, we
7841 don't need another one, so we can discard this one. Since
7842 this function is called before the linker maps input sections
7843 to output sections, we can easily discard it by setting the
7844 SEC_EXCLUDE flag. */
7845 if (h->fn_stub != NULL)
7846 {
7847 sec->flags |= SEC_EXCLUDE;
7848 return TRUE;
7849 }
7850
7851 sec->flags |= SEC_KEEP;
7852 h->fn_stub = sec;
7853 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7854 }
7855 }
7856 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
7857 {
7858 unsigned long r_symndx;
7859 struct mips_elf_link_hash_entry *h;
7860 asection **loc;
7861
7862 /* Look at the relocation information to figure out which symbol
7863 this is for. */
7864
7865 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
7866 if (r_symndx == 0)
7867 {
7868 (*_bfd_error_handler)
7869 (_("%B: Warning: cannot determine the target function for"
7870 " stub section `%s'"),
7871 abfd, name);
7872 bfd_set_error (bfd_error_bad_value);
7873 return FALSE;
7874 }
7875
7876 if (r_symndx < extsymoff
7877 || sym_hashes[r_symndx - extsymoff] == NULL)
7878 {
7879 asection *o;
7880
7881 /* This stub is for a local symbol. This stub will only be
7882 needed if there is some relocation (R_MIPS16_26) in this BFD
7883 that refers to this symbol. */
7884 for (o = abfd->sections; o != NULL; o = o->next)
7885 {
7886 Elf_Internal_Rela *sec_relocs;
7887 const Elf_Internal_Rela *r, *rend;
7888
7889 /* We can ignore stub sections when looking for relocs. */
7890 if ((o->flags & SEC_RELOC) == 0
7891 || o->reloc_count == 0
7892 || section_allows_mips16_refs_p (o))
7893 continue;
7894
7895 sec_relocs
7896 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
7897 info->keep_memory);
7898 if (sec_relocs == NULL)
7899 return FALSE;
7900
7901 rend = sec_relocs + o->reloc_count;
7902 for (r = sec_relocs; r < rend; r++)
7903 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
7904 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
7905 break;
7906
7907 if (elf_section_data (o)->relocs != sec_relocs)
7908 free (sec_relocs);
7909
7910 if (r < rend)
7911 break;
7912 }
7913
7914 if (o == NULL)
7915 {
7916 /* There is no non-call reloc for this stub, so we do
7917 not need it. Since this function is called before
7918 the linker maps input sections to output sections, we
7919 can easily discard it by setting the SEC_EXCLUDE
7920 flag. */
7921 sec->flags |= SEC_EXCLUDE;
7922 return TRUE;
7923 }
7924
7925 /* Record this stub in an array of local symbol call_stubs for
7926 this BFD. */
7927 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
7928 {
7929 unsigned long symcount;
7930 asection **n;
7931 bfd_size_type amt;
7932
7933 if (elf_bad_symtab (abfd))
7934 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
7935 else
7936 symcount = symtab_hdr->sh_info;
7937 amt = symcount * sizeof (asection *);
7938 n = bfd_zalloc (abfd, amt);
7939 if (n == NULL)
7940 return FALSE;
7941 mips_elf_tdata (abfd)->local_call_stubs = n;
7942 }
7943
7944 sec->flags |= SEC_KEEP;
7945 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
7946
7947 /* We don't need to set mips16_stubs_seen in this case.
7948 That flag is used to see whether we need to look through
7949 the global symbol table for stubs. We don't need to set
7950 it here, because we just have a local stub. */
7951 }
7952 else
7953 {
7954 h = ((struct mips_elf_link_hash_entry *)
7955 sym_hashes[r_symndx - extsymoff]);
7956
7957 /* H is the symbol this stub is for. */
7958
7959 if (CALL_FP_STUB_P (name))
7960 loc = &h->call_fp_stub;
7961 else
7962 loc = &h->call_stub;
7963
7964 /* If we already have an appropriate stub for this function, we
7965 don't need another one, so we can discard this one. Since
7966 this function is called before the linker maps input sections
7967 to output sections, we can easily discard it by setting the
7968 SEC_EXCLUDE flag. */
7969 if (*loc != NULL)
7970 {
7971 sec->flags |= SEC_EXCLUDE;
7972 return TRUE;
7973 }
7974
7975 sec->flags |= SEC_KEEP;
7976 *loc = sec;
7977 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
7978 }
7979 }
7980
7981 sreloc = NULL;
7982 contents = NULL;
7983 for (rel = relocs; rel < rel_end; ++rel)
7984 {
7985 unsigned long r_symndx;
7986 unsigned int r_type;
7987 struct elf_link_hash_entry *h;
7988 bfd_boolean can_make_dynamic_p;
7989 bfd_boolean call_reloc_p;
7990 bfd_boolean constrain_symbol_p;
7991
7992 r_symndx = ELF_R_SYM (abfd, rel->r_info);
7993 r_type = ELF_R_TYPE (abfd, rel->r_info);
7994
7995 if (r_symndx < extsymoff)
7996 h = NULL;
7997 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
7998 {
7999 (*_bfd_error_handler)
8000 (_("%B: Malformed reloc detected for section %s"),
8001 abfd, name);
8002 bfd_set_error (bfd_error_bad_value);
8003 return FALSE;
8004 }
8005 else
8006 {
8007 h = sym_hashes[r_symndx - extsymoff];
8008 if (h != NULL)
8009 {
8010 while (h->root.type == bfd_link_hash_indirect
8011 || h->root.type == bfd_link_hash_warning)
8012 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8013
8014 /* PR15323, ref flags aren't set for references in the
8015 same object. */
8016 h->root.non_ir_ref = 1;
8017 }
8018 }
8019
8020 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8021 relocation into a dynamic one. */
8022 can_make_dynamic_p = FALSE;
8023
8024 /* Set CALL_RELOC_P to true if the relocation is for a call,
8025 and if pointer equality therefore doesn't matter. */
8026 call_reloc_p = FALSE;
8027
8028 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8029 into account when deciding how to define the symbol.
8030 Relocations in nonallocatable sections such as .pdr and
8031 .debug* should have no effect. */
8032 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8033
8034 switch (r_type)
8035 {
8036 case R_MIPS_CALL16:
8037 case R_MIPS_CALL_HI16:
8038 case R_MIPS_CALL_LO16:
8039 case R_MIPS16_CALL16:
8040 case R_MICROMIPS_CALL16:
8041 case R_MICROMIPS_CALL_HI16:
8042 case R_MICROMIPS_CALL_LO16:
8043 call_reloc_p = TRUE;
8044 /* Fall through. */
8045
8046 case R_MIPS_GOT16:
8047 case R_MIPS_GOT_HI16:
8048 case R_MIPS_GOT_LO16:
8049 case R_MIPS_GOT_PAGE:
8050 case R_MIPS_GOT_OFST:
8051 case R_MIPS_GOT_DISP:
8052 case R_MIPS_TLS_GOTTPREL:
8053 case R_MIPS_TLS_GD:
8054 case R_MIPS_TLS_LDM:
8055 case R_MIPS16_GOT16:
8056 case R_MIPS16_TLS_GOTTPREL:
8057 case R_MIPS16_TLS_GD:
8058 case R_MIPS16_TLS_LDM:
8059 case R_MICROMIPS_GOT16:
8060 case R_MICROMIPS_GOT_HI16:
8061 case R_MICROMIPS_GOT_LO16:
8062 case R_MICROMIPS_GOT_PAGE:
8063 case R_MICROMIPS_GOT_OFST:
8064 case R_MICROMIPS_GOT_DISP:
8065 case R_MICROMIPS_TLS_GOTTPREL:
8066 case R_MICROMIPS_TLS_GD:
8067 case R_MICROMIPS_TLS_LDM:
8068 if (dynobj == NULL)
8069 elf_hash_table (info)->dynobj = dynobj = abfd;
8070 if (!mips_elf_create_got_section (dynobj, info))
8071 return FALSE;
8072 if (htab->is_vxworks && !info->shared)
8073 {
8074 (*_bfd_error_handler)
8075 (_("%B: GOT reloc at 0x%lx not expected in executables"),
8076 abfd, (unsigned long) rel->r_offset);
8077 bfd_set_error (bfd_error_bad_value);
8078 return FALSE;
8079 }
8080 can_make_dynamic_p = TRUE;
8081 break;
8082
8083 case R_MIPS_NONE:
8084 case R_MIPS_JALR:
8085 case R_MICROMIPS_JALR:
8086 /* These relocations have empty fields and are purely there to
8087 provide link information. The symbol value doesn't matter. */
8088 constrain_symbol_p = FALSE;
8089 break;
8090
8091 case R_MIPS_GPREL16:
8092 case R_MIPS_GPREL32:
8093 case R_MIPS16_GPREL:
8094 case R_MICROMIPS_GPREL16:
8095 /* GP-relative relocations always resolve to a definition in a
8096 regular input file, ignoring the one-definition rule. This is
8097 important for the GP setup sequence in NewABI code, which
8098 always resolves to a local function even if other relocations
8099 against the symbol wouldn't. */
8100 constrain_symbol_p = FALSE;
8101 break;
8102
8103 case R_MIPS_32:
8104 case R_MIPS_REL32:
8105 case R_MIPS_64:
8106 /* In VxWorks executables, references to external symbols
8107 must be handled using copy relocs or PLT entries; it is not
8108 possible to convert this relocation into a dynamic one.
8109
8110 For executables that use PLTs and copy-relocs, we have a
8111 choice between converting the relocation into a dynamic
8112 one or using copy relocations or PLT entries. It is
8113 usually better to do the former, unless the relocation is
8114 against a read-only section. */
8115 if ((info->shared
8116 || (h != NULL
8117 && !htab->is_vxworks
8118 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8119 && !(!info->nocopyreloc
8120 && !PIC_OBJECT_P (abfd)
8121 && MIPS_ELF_READONLY_SECTION (sec))))
8122 && (sec->flags & SEC_ALLOC) != 0)
8123 {
8124 can_make_dynamic_p = TRUE;
8125 if (dynobj == NULL)
8126 elf_hash_table (info)->dynobj = dynobj = abfd;
8127 }
8128 break;
8129
8130 case R_MIPS_26:
8131 case R_MIPS_PC16:
8132 case R_MIPS16_26:
8133 case R_MICROMIPS_26_S1:
8134 case R_MICROMIPS_PC7_S1:
8135 case R_MICROMIPS_PC10_S1:
8136 case R_MICROMIPS_PC16_S1:
8137 case R_MICROMIPS_PC23_S2:
8138 call_reloc_p = TRUE;
8139 break;
8140 }
8141
8142 if (h)
8143 {
8144 if (constrain_symbol_p)
8145 {
8146 if (!can_make_dynamic_p)
8147 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8148
8149 if (!call_reloc_p)
8150 h->pointer_equality_needed = 1;
8151
8152 /* We must not create a stub for a symbol that has
8153 relocations related to taking the function's address.
8154 This doesn't apply to VxWorks, where CALL relocs refer
8155 to a .got.plt entry instead of a normal .got entry. */
8156 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8157 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8158 }
8159
8160 /* Relocations against the special VxWorks __GOTT_BASE__ and
8161 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8162 room for them in .rela.dyn. */
8163 if (is_gott_symbol (info, h))
8164 {
8165 if (sreloc == NULL)
8166 {
8167 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8168 if (sreloc == NULL)
8169 return FALSE;
8170 }
8171 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8172 if (MIPS_ELF_READONLY_SECTION (sec))
8173 /* We tell the dynamic linker that there are
8174 relocations against the text segment. */
8175 info->flags |= DF_TEXTREL;
8176 }
8177 }
8178 else if (call_lo16_reloc_p (r_type)
8179 || got_lo16_reloc_p (r_type)
8180 || got_disp_reloc_p (r_type)
8181 || (got16_reloc_p (r_type) && htab->is_vxworks))
8182 {
8183 /* We may need a local GOT entry for this relocation. We
8184 don't count R_MIPS_GOT_PAGE because we can estimate the
8185 maximum number of pages needed by looking at the size of
8186 the segment. Similar comments apply to R_MIPS*_GOT16 and
8187 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
8188 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
8189 R_MIPS_CALL_HI16 because these are always followed by an
8190 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
8191 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8192 rel->r_addend, info, r_type))
8193 return FALSE;
8194 }
8195
8196 if (h != NULL
8197 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8198 ELF_ST_IS_MIPS16 (h->other)))
8199 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8200
8201 switch (r_type)
8202 {
8203 case R_MIPS_CALL16:
8204 case R_MIPS16_CALL16:
8205 case R_MICROMIPS_CALL16:
8206 if (h == NULL)
8207 {
8208 (*_bfd_error_handler)
8209 (_("%B: CALL16 reloc at 0x%lx not against global symbol"),
8210 abfd, (unsigned long) rel->r_offset);
8211 bfd_set_error (bfd_error_bad_value);
8212 return FALSE;
8213 }
8214 /* Fall through. */
8215
8216 case R_MIPS_CALL_HI16:
8217 case R_MIPS_CALL_LO16:
8218 case R_MICROMIPS_CALL_HI16:
8219 case R_MICROMIPS_CALL_LO16:
8220 if (h != NULL)
8221 {
8222 /* Make sure there is room in the regular GOT to hold the
8223 function's address. We may eliminate it in favour of
8224 a .got.plt entry later; see mips_elf_count_got_symbols. */
8225 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8226 r_type))
8227 return FALSE;
8228
8229 /* We need a stub, not a plt entry for the undefined
8230 function. But we record it as if it needs plt. See
8231 _bfd_elf_adjust_dynamic_symbol. */
8232 h->needs_plt = 1;
8233 h->type = STT_FUNC;
8234 }
8235 break;
8236
8237 case R_MIPS_GOT_PAGE:
8238 case R_MICROMIPS_GOT_PAGE:
8239 case R_MIPS16_GOT16:
8240 case R_MIPS_GOT16:
8241 case R_MIPS_GOT_HI16:
8242 case R_MIPS_GOT_LO16:
8243 case R_MICROMIPS_GOT16:
8244 case R_MICROMIPS_GOT_HI16:
8245 case R_MICROMIPS_GOT_LO16:
8246 if (!h || got_page_reloc_p (r_type))
8247 {
8248 /* This relocation needs (or may need, if h != NULL) a
8249 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8250 know for sure until we know whether the symbol is
8251 preemptible. */
8252 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8253 {
8254 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8255 return FALSE;
8256 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8257 addend = mips_elf_read_rel_addend (abfd, rel,
8258 howto, contents);
8259 if (got16_reloc_p (r_type))
8260 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8261 contents, &addend);
8262 else
8263 addend <<= howto->rightshift;
8264 }
8265 else
8266 addend = rel->r_addend;
8267 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8268 h, addend))
8269 return FALSE;
8270
8271 if (h)
8272 {
8273 struct mips_elf_link_hash_entry *hmips =
8274 (struct mips_elf_link_hash_entry *) h;
8275
8276 /* This symbol is definitely not overridable. */
8277 if (hmips->root.def_regular
8278 && ! (info->shared && ! info->symbolic
8279 && ! hmips->root.forced_local))
8280 h = NULL;
8281 }
8282 }
8283 /* If this is a global, overridable symbol, GOT_PAGE will
8284 decay to GOT_DISP, so we'll need a GOT entry for it. */
8285 /* Fall through. */
8286
8287 case R_MIPS_GOT_DISP:
8288 case R_MICROMIPS_GOT_DISP:
8289 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
8290 FALSE, r_type))
8291 return FALSE;
8292 break;
8293
8294 case R_MIPS_TLS_GOTTPREL:
8295 case R_MIPS16_TLS_GOTTPREL:
8296 case R_MICROMIPS_TLS_GOTTPREL:
8297 if (info->shared)
8298 info->flags |= DF_STATIC_TLS;
8299 /* Fall through */
8300
8301 case R_MIPS_TLS_LDM:
8302 case R_MIPS16_TLS_LDM:
8303 case R_MICROMIPS_TLS_LDM:
8304 if (tls_ldm_reloc_p (r_type))
8305 {
8306 r_symndx = STN_UNDEF;
8307 h = NULL;
8308 }
8309 /* Fall through */
8310
8311 case R_MIPS_TLS_GD:
8312 case R_MIPS16_TLS_GD:
8313 case R_MICROMIPS_TLS_GD:
8314 /* This symbol requires a global offset table entry, or two
8315 for TLS GD relocations. */
8316 if (h != NULL)
8317 {
8318 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8319 FALSE, r_type))
8320 return FALSE;
8321 }
8322 else
8323 {
8324 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8325 rel->r_addend,
8326 info, r_type))
8327 return FALSE;
8328 }
8329 break;
8330
8331 case R_MIPS_32:
8332 case R_MIPS_REL32:
8333 case R_MIPS_64:
8334 /* In VxWorks executables, references to external symbols
8335 are handled using copy relocs or PLT stubs, so there's
8336 no need to add a .rela.dyn entry for this relocation. */
8337 if (can_make_dynamic_p)
8338 {
8339 if (sreloc == NULL)
8340 {
8341 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8342 if (sreloc == NULL)
8343 return FALSE;
8344 }
8345 if (info->shared && h == NULL)
8346 {
8347 /* When creating a shared object, we must copy these
8348 reloc types into the output file as R_MIPS_REL32
8349 relocs. Make room for this reloc in .rel(a).dyn. */
8350 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
8351 if (MIPS_ELF_READONLY_SECTION (sec))
8352 /* We tell the dynamic linker that there are
8353 relocations against the text segment. */
8354 info->flags |= DF_TEXTREL;
8355 }
8356 else
8357 {
8358 struct mips_elf_link_hash_entry *hmips;
8359
8360 /* For a shared object, we must copy this relocation
8361 unless the symbol turns out to be undefined and
8362 weak with non-default visibility, in which case
8363 it will be left as zero.
8364
8365 We could elide R_MIPS_REL32 for locally binding symbols
8366 in shared libraries, but do not yet do so.
8367
8368 For an executable, we only need to copy this
8369 reloc if the symbol is defined in a dynamic
8370 object. */
8371 hmips = (struct mips_elf_link_hash_entry *) h;
8372 ++hmips->possibly_dynamic_relocs;
8373 if (MIPS_ELF_READONLY_SECTION (sec))
8374 /* We need it to tell the dynamic linker if there
8375 are relocations against the text segment. */
8376 hmips->readonly_reloc = TRUE;
8377 }
8378 }
8379
8380 if (SGI_COMPAT (abfd))
8381 mips_elf_hash_table (info)->compact_rel_size +=
8382 sizeof (Elf32_External_crinfo);
8383 break;
8384
8385 case R_MIPS_26:
8386 case R_MIPS_GPREL16:
8387 case R_MIPS_LITERAL:
8388 case R_MIPS_GPREL32:
8389 case R_MICROMIPS_26_S1:
8390 case R_MICROMIPS_GPREL16:
8391 case R_MICROMIPS_LITERAL:
8392 case R_MICROMIPS_GPREL7_S2:
8393 if (SGI_COMPAT (abfd))
8394 mips_elf_hash_table (info)->compact_rel_size +=
8395 sizeof (Elf32_External_crinfo);
8396 break;
8397
8398 /* This relocation describes the C++ object vtable hierarchy.
8399 Reconstruct it for later use during GC. */
8400 case R_MIPS_GNU_VTINHERIT:
8401 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
8402 return FALSE;
8403 break;
8404
8405 /* This relocation describes which C++ vtable entries are actually
8406 used. Record for later use during GC. */
8407 case R_MIPS_GNU_VTENTRY:
8408 BFD_ASSERT (h != NULL);
8409 if (h != NULL
8410 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
8411 return FALSE;
8412 break;
8413
8414 default:
8415 break;
8416 }
8417
8418 /* Record the need for a PLT entry. At this point we don't know
8419 yet if we are going to create a PLT in the first place, but
8420 we only record whether the relocation requires a standard MIPS
8421 or a compressed code entry anyway. If we don't make a PLT after
8422 all, then we'll just ignore these arrangements. Likewise if
8423 a PLT entry is not created because the symbol is satisfied
8424 locally. */
8425 if (h != NULL
8426 && jal_reloc_p (r_type)
8427 && !SYMBOL_CALLS_LOCAL (info, h))
8428 {
8429 if (h->plt.plist == NULL)
8430 h->plt.plist = mips_elf_make_plt_record (abfd);
8431 if (h->plt.plist == NULL)
8432 return FALSE;
8433
8434 if (r_type == R_MIPS_26)
8435 h->plt.plist->need_mips = TRUE;
8436 else
8437 h->plt.plist->need_comp = TRUE;
8438 }
8439
8440 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
8441 if there is one. We only need to handle global symbols here;
8442 we decide whether to keep or delete stubs for local symbols
8443 when processing the stub's relocations. */
8444 if (h != NULL
8445 && !mips16_call_reloc_p (r_type)
8446 && !section_allows_mips16_refs_p (sec))
8447 {
8448 struct mips_elf_link_hash_entry *mh;
8449
8450 mh = (struct mips_elf_link_hash_entry *) h;
8451 mh->need_fn_stub = TRUE;
8452 }
8453
8454 /* Refuse some position-dependent relocations when creating a
8455 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
8456 not PIC, but we can create dynamic relocations and the result
8457 will be fine. Also do not refuse R_MIPS_LO16, which can be
8458 combined with R_MIPS_GOT16. */
8459 if (info->shared)
8460 {
8461 switch (r_type)
8462 {
8463 case R_MIPS16_HI16:
8464 case R_MIPS_HI16:
8465 case R_MIPS_HIGHER:
8466 case R_MIPS_HIGHEST:
8467 case R_MICROMIPS_HI16:
8468 case R_MICROMIPS_HIGHER:
8469 case R_MICROMIPS_HIGHEST:
8470 /* Don't refuse a high part relocation if it's against
8471 no symbol (e.g. part of a compound relocation). */
8472 if (r_symndx == STN_UNDEF)
8473 break;
8474
8475 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
8476 and has a special meaning. */
8477 if (!NEWABI_P (abfd) && h != NULL
8478 && strcmp (h->root.root.string, "_gp_disp") == 0)
8479 break;
8480
8481 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
8482 if (is_gott_symbol (info, h))
8483 break;
8484
8485 /* FALLTHROUGH */
8486
8487 case R_MIPS16_26:
8488 case R_MIPS_26:
8489 case R_MICROMIPS_26_S1:
8490 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8491 (*_bfd_error_handler)
8492 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
8493 abfd, howto->name,
8494 (h) ? h->root.root.string : "a local symbol");
8495 bfd_set_error (bfd_error_bad_value);
8496 return FALSE;
8497 default:
8498 break;
8499 }
8500 }
8501 }
8502
8503 return TRUE;
8504 }
8505 \f
8506 bfd_boolean
8507 _bfd_mips_relax_section (bfd *abfd, asection *sec,
8508 struct bfd_link_info *link_info,
8509 bfd_boolean *again)
8510 {
8511 Elf_Internal_Rela *internal_relocs;
8512 Elf_Internal_Rela *irel, *irelend;
8513 Elf_Internal_Shdr *symtab_hdr;
8514 bfd_byte *contents = NULL;
8515 size_t extsymoff;
8516 bfd_boolean changed_contents = FALSE;
8517 bfd_vma sec_start = sec->output_section->vma + sec->output_offset;
8518 Elf_Internal_Sym *isymbuf = NULL;
8519
8520 /* We are not currently changing any sizes, so only one pass. */
8521 *again = FALSE;
8522
8523 if (link_info->relocatable)
8524 return TRUE;
8525
8526 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
8527 link_info->keep_memory);
8528 if (internal_relocs == NULL)
8529 return TRUE;
8530
8531 irelend = internal_relocs + sec->reloc_count
8532 * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel;
8533 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8534 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8535
8536 for (irel = internal_relocs; irel < irelend; irel++)
8537 {
8538 bfd_vma symval;
8539 bfd_signed_vma sym_offset;
8540 unsigned int r_type;
8541 unsigned long r_symndx;
8542 asection *sym_sec;
8543 unsigned long instruction;
8544
8545 /* Turn jalr into bgezal, and jr into beq, if they're marked
8546 with a JALR relocation, that indicate where they jump to.
8547 This saves some pipeline bubbles. */
8548 r_type = ELF_R_TYPE (abfd, irel->r_info);
8549 if (r_type != R_MIPS_JALR)
8550 continue;
8551
8552 r_symndx = ELF_R_SYM (abfd, irel->r_info);
8553 /* Compute the address of the jump target. */
8554 if (r_symndx >= extsymoff)
8555 {
8556 struct mips_elf_link_hash_entry *h
8557 = ((struct mips_elf_link_hash_entry *)
8558 elf_sym_hashes (abfd) [r_symndx - extsymoff]);
8559
8560 while (h->root.root.type == bfd_link_hash_indirect
8561 || h->root.root.type == bfd_link_hash_warning)
8562 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8563
8564 /* If a symbol is undefined, or if it may be overridden,
8565 skip it. */
8566 if (! ((h->root.root.type == bfd_link_hash_defined
8567 || h->root.root.type == bfd_link_hash_defweak)
8568 && h->root.root.u.def.section)
8569 || (link_info->shared && ! link_info->symbolic
8570 && !h->root.forced_local))
8571 continue;
8572
8573 sym_sec = h->root.root.u.def.section;
8574 if (sym_sec->output_section)
8575 symval = (h->root.root.u.def.value
8576 + sym_sec->output_section->vma
8577 + sym_sec->output_offset);
8578 else
8579 symval = h->root.root.u.def.value;
8580 }
8581 else
8582 {
8583 Elf_Internal_Sym *isym;
8584
8585 /* Read this BFD's symbols if we haven't done so already. */
8586 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
8587 {
8588 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
8589 if (isymbuf == NULL)
8590 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8591 symtab_hdr->sh_info, 0,
8592 NULL, NULL, NULL);
8593 if (isymbuf == NULL)
8594 goto relax_return;
8595 }
8596
8597 isym = isymbuf + r_symndx;
8598 if (isym->st_shndx == SHN_UNDEF)
8599 continue;
8600 else if (isym->st_shndx == SHN_ABS)
8601 sym_sec = bfd_abs_section_ptr;
8602 else if (isym->st_shndx == SHN_COMMON)
8603 sym_sec = bfd_com_section_ptr;
8604 else
8605 sym_sec
8606 = bfd_section_from_elf_index (abfd, isym->st_shndx);
8607 symval = isym->st_value
8608 + sym_sec->output_section->vma
8609 + sym_sec->output_offset;
8610 }
8611
8612 /* Compute branch offset, from delay slot of the jump to the
8613 branch target. */
8614 sym_offset = (symval + irel->r_addend)
8615 - (sec_start + irel->r_offset + 4);
8616
8617 /* Branch offset must be properly aligned. */
8618 if ((sym_offset & 3) != 0)
8619 continue;
8620
8621 sym_offset >>= 2;
8622
8623 /* Check that it's in range. */
8624 if (sym_offset < -0x8000 || sym_offset >= 0x8000)
8625 continue;
8626
8627 /* Get the section contents if we haven't done so already. */
8628 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8629 goto relax_return;
8630
8631 instruction = bfd_get_32 (abfd, contents + irel->r_offset);
8632
8633 /* If it was jalr <reg>, turn it into bgezal $zero, <target>. */
8634 if ((instruction & 0xfc1fffff) == 0x0000f809)
8635 instruction = 0x04110000;
8636 /* If it was jr <reg>, turn it into b <target>. */
8637 else if ((instruction & 0xfc1fffff) == 0x00000008)
8638 instruction = 0x10000000;
8639 else
8640 continue;
8641
8642 instruction |= (sym_offset & 0xffff);
8643 bfd_put_32 (abfd, instruction, contents + irel->r_offset);
8644 changed_contents = TRUE;
8645 }
8646
8647 if (contents != NULL
8648 && elf_section_data (sec)->this_hdr.contents != contents)
8649 {
8650 if (!changed_contents && !link_info->keep_memory)
8651 free (contents);
8652 else
8653 {
8654 /* Cache the section contents for elf_link_input_bfd. */
8655 elf_section_data (sec)->this_hdr.contents = contents;
8656 }
8657 }
8658 return TRUE;
8659
8660 relax_return:
8661 if (contents != NULL
8662 && elf_section_data (sec)->this_hdr.contents != contents)
8663 free (contents);
8664 return FALSE;
8665 }
8666 \f
8667 /* Allocate space for global sym dynamic relocs. */
8668
8669 static bfd_boolean
8670 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8671 {
8672 struct bfd_link_info *info = inf;
8673 bfd *dynobj;
8674 struct mips_elf_link_hash_entry *hmips;
8675 struct mips_elf_link_hash_table *htab;
8676
8677 htab = mips_elf_hash_table (info);
8678 BFD_ASSERT (htab != NULL);
8679
8680 dynobj = elf_hash_table (info)->dynobj;
8681 hmips = (struct mips_elf_link_hash_entry *) h;
8682
8683 /* VxWorks executables are handled elsewhere; we only need to
8684 allocate relocations in shared objects. */
8685 if (htab->is_vxworks && !info->shared)
8686 return TRUE;
8687
8688 /* Ignore indirect symbols. All relocations against such symbols
8689 will be redirected to the target symbol. */
8690 if (h->root.type == bfd_link_hash_indirect)
8691 return TRUE;
8692
8693 /* If this symbol is defined in a dynamic object, or we are creating
8694 a shared library, we will need to copy any R_MIPS_32 or
8695 R_MIPS_REL32 relocs against it into the output file. */
8696 if (! info->relocatable
8697 && hmips->possibly_dynamic_relocs != 0
8698 && (h->root.type == bfd_link_hash_defweak
8699 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
8700 || info->shared))
8701 {
8702 bfd_boolean do_copy = TRUE;
8703
8704 if (h->root.type == bfd_link_hash_undefweak)
8705 {
8706 /* Do not copy relocations for undefined weak symbols with
8707 non-default visibility. */
8708 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8709 do_copy = FALSE;
8710
8711 /* Make sure undefined weak symbols are output as a dynamic
8712 symbol in PIEs. */
8713 else if (h->dynindx == -1 && !h->forced_local)
8714 {
8715 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8716 return FALSE;
8717 }
8718 }
8719
8720 if (do_copy)
8721 {
8722 /* Even though we don't directly need a GOT entry for this symbol,
8723 the SVR4 psABI requires it to have a dynamic symbol table
8724 index greater that DT_MIPS_GOTSYM if there are dynamic
8725 relocations against it.
8726
8727 VxWorks does not enforce the same mapping between the GOT
8728 and the symbol table, so the same requirement does not
8729 apply there. */
8730 if (!htab->is_vxworks)
8731 {
8732 if (hmips->global_got_area > GGA_RELOC_ONLY)
8733 hmips->global_got_area = GGA_RELOC_ONLY;
8734 hmips->got_only_for_calls = FALSE;
8735 }
8736
8737 mips_elf_allocate_dynamic_relocations
8738 (dynobj, info, hmips->possibly_dynamic_relocs);
8739 if (hmips->readonly_reloc)
8740 /* We tell the dynamic linker that there are relocations
8741 against the text segment. */
8742 info->flags |= DF_TEXTREL;
8743 }
8744 }
8745
8746 return TRUE;
8747 }
8748
8749 /* Adjust a symbol defined by a dynamic object and referenced by a
8750 regular object. The current definition is in some section of the
8751 dynamic object, but we're not including those sections. We have to
8752 change the definition to something the rest of the link can
8753 understand. */
8754
8755 bfd_boolean
8756 _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
8757 struct elf_link_hash_entry *h)
8758 {
8759 bfd *dynobj;
8760 struct mips_elf_link_hash_entry *hmips;
8761 struct mips_elf_link_hash_table *htab;
8762
8763 htab = mips_elf_hash_table (info);
8764 BFD_ASSERT (htab != NULL);
8765
8766 dynobj = elf_hash_table (info)->dynobj;
8767 hmips = (struct mips_elf_link_hash_entry *) h;
8768
8769 /* Make sure we know what is going on here. */
8770 BFD_ASSERT (dynobj != NULL
8771 && (h->needs_plt
8772 || h->u.weakdef != NULL
8773 || (h->def_dynamic
8774 && h->ref_regular
8775 && !h->def_regular)));
8776
8777 hmips = (struct mips_elf_link_hash_entry *) h;
8778
8779 /* If there are call relocations against an externally-defined symbol,
8780 see whether we can create a MIPS lazy-binding stub for it. We can
8781 only do this if all references to the function are through call
8782 relocations, and in that case, the traditional lazy-binding stubs
8783 are much more efficient than PLT entries.
8784
8785 Traditional stubs are only available on SVR4 psABI-based systems;
8786 VxWorks always uses PLTs instead. */
8787 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
8788 {
8789 if (! elf_hash_table (info)->dynamic_sections_created)
8790 return TRUE;
8791
8792 /* If this symbol is not defined in a regular file, then set
8793 the symbol to the stub location. This is required to make
8794 function pointers compare as equal between the normal
8795 executable and the shared library. */
8796 if (!h->def_regular)
8797 {
8798 hmips->needs_lazy_stub = TRUE;
8799 htab->lazy_stub_count++;
8800 return TRUE;
8801 }
8802 }
8803 /* As above, VxWorks requires PLT entries for externally-defined
8804 functions that are only accessed through call relocations.
8805
8806 Both VxWorks and non-VxWorks targets also need PLT entries if there
8807 are static-only relocations against an externally-defined function.
8808 This can technically occur for shared libraries if there are
8809 branches to the symbol, although it is unlikely that this will be
8810 used in practice due to the short ranges involved. It can occur
8811 for any relative or absolute relocation in executables; in that
8812 case, the PLT entry becomes the function's canonical address. */
8813 else if (((h->needs_plt && !hmips->no_fn_stub)
8814 || (h->type == STT_FUNC && hmips->has_static_relocs))
8815 && htab->use_plts_and_copy_relocs
8816 && !SYMBOL_CALLS_LOCAL (info, h)
8817 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
8818 && h->root.type == bfd_link_hash_undefweak))
8819 {
8820 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
8821 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
8822
8823 /* If this is the first symbol to need a PLT entry, then make some
8824 basic setup. Also work out PLT entry sizes. We'll need them
8825 for PLT offset calculations. */
8826 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
8827 {
8828 BFD_ASSERT (htab->sgotplt->size == 0);
8829 BFD_ASSERT (htab->plt_got_index == 0);
8830
8831 /* If we're using the PLT additions to the psABI, each PLT
8832 entry is 16 bytes and the PLT0 entry is 32 bytes.
8833 Encourage better cache usage by aligning. We do this
8834 lazily to avoid pessimizing traditional objects. */
8835 if (!htab->is_vxworks
8836 && !bfd_set_section_alignment (dynobj, htab->splt, 5))
8837 return FALSE;
8838
8839 /* Make sure that .got.plt is word-aligned. We do this lazily
8840 for the same reason as above. */
8841 if (!bfd_set_section_alignment (dynobj, htab->sgotplt,
8842 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
8843 return FALSE;
8844
8845 /* On non-VxWorks targets, the first two entries in .got.plt
8846 are reserved. */
8847 if (!htab->is_vxworks)
8848 htab->plt_got_index
8849 += (get_elf_backend_data (dynobj)->got_header_size
8850 / MIPS_ELF_GOT_SIZE (dynobj));
8851
8852 /* On VxWorks, also allocate room for the header's
8853 .rela.plt.unloaded entries. */
8854 if (htab->is_vxworks && !info->shared)
8855 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
8856
8857 /* Now work out the sizes of individual PLT entries. */
8858 if (htab->is_vxworks && info->shared)
8859 htab->plt_mips_entry_size
8860 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
8861 else if (htab->is_vxworks)
8862 htab->plt_mips_entry_size
8863 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
8864 else if (newabi_p)
8865 htab->plt_mips_entry_size
8866 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8867 else if (!micromips_p)
8868 {
8869 htab->plt_mips_entry_size
8870 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8871 htab->plt_comp_entry_size
8872 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
8873 }
8874 else if (htab->insn32)
8875 {
8876 htab->plt_mips_entry_size
8877 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8878 htab->plt_comp_entry_size
8879 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
8880 }
8881 else
8882 {
8883 htab->plt_mips_entry_size
8884 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
8885 htab->plt_comp_entry_size
8886 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
8887 }
8888 }
8889
8890 if (h->plt.plist == NULL)
8891 h->plt.plist = mips_elf_make_plt_record (dynobj);
8892 if (h->plt.plist == NULL)
8893 return FALSE;
8894
8895 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
8896 n32 or n64, so always use a standard entry there.
8897
8898 If the symbol has a MIPS16 call stub and gets a PLT entry, then
8899 all MIPS16 calls will go via that stub, and there is no benefit
8900 to having a MIPS16 entry. And in the case of call_stub a
8901 standard entry actually has to be used as the stub ends with a J
8902 instruction. */
8903 if (newabi_p
8904 || htab->is_vxworks
8905 || hmips->call_stub
8906 || hmips->call_fp_stub)
8907 {
8908 h->plt.plist->need_mips = TRUE;
8909 h->plt.plist->need_comp = FALSE;
8910 }
8911
8912 /* Otherwise, if there are no direct calls to the function, we
8913 have a free choice of whether to use standard or compressed
8914 entries. Prefer microMIPS entries if the object is known to
8915 contain microMIPS code, so that it becomes possible to create
8916 pure microMIPS binaries. Prefer standard entries otherwise,
8917 because MIPS16 ones are no smaller and are usually slower. */
8918 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
8919 {
8920 if (micromips_p)
8921 h->plt.plist->need_comp = TRUE;
8922 else
8923 h->plt.plist->need_mips = TRUE;
8924 }
8925
8926 if (h->plt.plist->need_mips)
8927 {
8928 h->plt.plist->mips_offset = htab->plt_mips_offset;
8929 htab->plt_mips_offset += htab->plt_mips_entry_size;
8930 }
8931 if (h->plt.plist->need_comp)
8932 {
8933 h->plt.plist->comp_offset = htab->plt_comp_offset;
8934 htab->plt_comp_offset += htab->plt_comp_entry_size;
8935 }
8936
8937 /* Reserve the corresponding .got.plt entry now too. */
8938 h->plt.plist->gotplt_index = htab->plt_got_index++;
8939
8940 /* If the output file has no definition of the symbol, set the
8941 symbol's value to the address of the stub. */
8942 if (!info->shared && !h->def_regular)
8943 hmips->use_plt_entry = TRUE;
8944
8945 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
8946 htab->srelplt->size += (htab->is_vxworks
8947 ? MIPS_ELF_RELA_SIZE (dynobj)
8948 : MIPS_ELF_REL_SIZE (dynobj));
8949
8950 /* Make room for the .rela.plt.unloaded relocations. */
8951 if (htab->is_vxworks && !info->shared)
8952 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
8953
8954 /* All relocations against this symbol that could have been made
8955 dynamic will now refer to the PLT entry instead. */
8956 hmips->possibly_dynamic_relocs = 0;
8957
8958 return TRUE;
8959 }
8960
8961 /* If this is a weak symbol, and there is a real definition, the
8962 processor independent code will have arranged for us to see the
8963 real definition first, and we can just use the same value. */
8964 if (h->u.weakdef != NULL)
8965 {
8966 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
8967 || h->u.weakdef->root.type == bfd_link_hash_defweak);
8968 h->root.u.def.section = h->u.weakdef->root.u.def.section;
8969 h->root.u.def.value = h->u.weakdef->root.u.def.value;
8970 return TRUE;
8971 }
8972
8973 /* Otherwise, there is nothing further to do for symbols defined
8974 in regular objects. */
8975 if (h->def_regular)
8976 return TRUE;
8977
8978 /* There's also nothing more to do if we'll convert all relocations
8979 against this symbol into dynamic relocations. */
8980 if (!hmips->has_static_relocs)
8981 return TRUE;
8982
8983 /* We're now relying on copy relocations. Complain if we have
8984 some that we can't convert. */
8985 if (!htab->use_plts_and_copy_relocs || info->shared)
8986 {
8987 (*_bfd_error_handler) (_("non-dynamic relocations refer to "
8988 "dynamic symbol %s"),
8989 h->root.root.string);
8990 bfd_set_error (bfd_error_bad_value);
8991 return FALSE;
8992 }
8993
8994 /* We must allocate the symbol in our .dynbss section, which will
8995 become part of the .bss section of the executable. There will be
8996 an entry for this symbol in the .dynsym section. The dynamic
8997 object will contain position independent code, so all references
8998 from the dynamic object to this symbol will go through the global
8999 offset table. The dynamic linker will use the .dynsym entry to
9000 determine the address it must put in the global offset table, so
9001 both the dynamic object and the regular object will refer to the
9002 same memory location for the variable. */
9003
9004 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9005 {
9006 if (htab->is_vxworks)
9007 htab->srelbss->size += sizeof (Elf32_External_Rela);
9008 else
9009 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9010 h->needs_copy = 1;
9011 }
9012
9013 /* All relocations against this symbol that could have been made
9014 dynamic will now refer to the local copy instead. */
9015 hmips->possibly_dynamic_relocs = 0;
9016
9017 return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
9018 }
9019 \f
9020 /* This function is called after all the input files have been read,
9021 and the input sections have been assigned to output sections. We
9022 check for any mips16 stub sections that we can discard. */
9023
9024 bfd_boolean
9025 _bfd_mips_elf_always_size_sections (bfd *output_bfd,
9026 struct bfd_link_info *info)
9027 {
9028 asection *ri;
9029 struct mips_elf_link_hash_table *htab;
9030 struct mips_htab_traverse_info hti;
9031
9032 htab = mips_elf_hash_table (info);
9033 BFD_ASSERT (htab != NULL);
9034
9035 /* The .reginfo section has a fixed size. */
9036 ri = bfd_get_section_by_name (output_bfd, ".reginfo");
9037 if (ri != NULL)
9038 bfd_set_section_size (output_bfd, ri, sizeof (Elf32_External_RegInfo));
9039
9040 hti.info = info;
9041 hti.output_bfd = output_bfd;
9042 hti.error = FALSE;
9043 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9044 mips_elf_check_symbols, &hti);
9045 if (hti.error)
9046 return FALSE;
9047
9048 return TRUE;
9049 }
9050
9051 /* If the link uses a GOT, lay it out and work out its size. */
9052
9053 static bfd_boolean
9054 mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9055 {
9056 bfd *dynobj;
9057 asection *s;
9058 struct mips_got_info *g;
9059 bfd_size_type loadable_size = 0;
9060 bfd_size_type page_gotno;
9061 bfd *ibfd;
9062 struct mips_elf_traverse_got_arg tga;
9063 struct mips_elf_link_hash_table *htab;
9064
9065 htab = mips_elf_hash_table (info);
9066 BFD_ASSERT (htab != NULL);
9067
9068 s = htab->sgot;
9069 if (s == NULL)
9070 return TRUE;
9071
9072 dynobj = elf_hash_table (info)->dynobj;
9073 g = htab->got_info;
9074
9075 /* Allocate room for the reserved entries. VxWorks always reserves
9076 3 entries; other objects only reserve 2 entries. */
9077 BFD_ASSERT (g->assigned_low_gotno == 0);
9078 if (htab->is_vxworks)
9079 htab->reserved_gotno = 3;
9080 else
9081 htab->reserved_gotno = 2;
9082 g->local_gotno += htab->reserved_gotno;
9083 g->assigned_low_gotno = htab->reserved_gotno;
9084
9085 /* Decide which symbols need to go in the global part of the GOT and
9086 count the number of reloc-only GOT symbols. */
9087 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
9088
9089 if (!mips_elf_resolve_final_got_entries (info, g))
9090 return FALSE;
9091
9092 /* Calculate the total loadable size of the output. That
9093 will give us the maximum number of GOT_PAGE entries
9094 required. */
9095 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
9096 {
9097 asection *subsection;
9098
9099 for (subsection = ibfd->sections;
9100 subsection;
9101 subsection = subsection->next)
9102 {
9103 if ((subsection->flags & SEC_ALLOC) == 0)
9104 continue;
9105 loadable_size += ((subsection->size + 0xf)
9106 &~ (bfd_size_type) 0xf);
9107 }
9108 }
9109
9110 if (htab->is_vxworks)
9111 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
9112 relocations against local symbols evaluate to "G", and the EABI does
9113 not include R_MIPS_GOT_PAGE. */
9114 page_gotno = 0;
9115 else
9116 /* Assume there are two loadable segments consisting of contiguous
9117 sections. Is 5 enough? */
9118 page_gotno = (loadable_size >> 16) + 5;
9119
9120 /* Choose the smaller of the two page estimates; both are intended to be
9121 conservative. */
9122 if (page_gotno > g->page_gotno)
9123 page_gotno = g->page_gotno;
9124
9125 g->local_gotno += page_gotno;
9126 g->assigned_high_gotno = g->local_gotno - 1;
9127
9128 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9129 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9130 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9131
9132 /* VxWorks does not support multiple GOTs. It initializes $gp to
9133 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9134 dynamic loader. */
9135 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
9136 {
9137 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
9138 return FALSE;
9139 }
9140 else
9141 {
9142 /* Record that all bfds use G. This also has the effect of freeing
9143 the per-bfd GOTs, which we no longer need. */
9144 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
9145 if (mips_elf_bfd_got (ibfd, FALSE))
9146 mips_elf_replace_bfd_got (ibfd, g);
9147 mips_elf_replace_bfd_got (output_bfd, g);
9148
9149 /* Set up TLS entries. */
9150 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
9151 tga.info = info;
9152 tga.g = g;
9153 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9154 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9155 if (!tga.g)
9156 return FALSE;
9157 BFD_ASSERT (g->tls_assigned_gotno
9158 == g->global_gotno + g->local_gotno + g->tls_gotno);
9159
9160 /* Each VxWorks GOT entry needs an explicit relocation. */
9161 if (htab->is_vxworks && info->shared)
9162 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9163
9164 /* Allocate room for the TLS relocations. */
9165 if (g->relocs)
9166 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
9167 }
9168
9169 return TRUE;
9170 }
9171
9172 /* Estimate the size of the .MIPS.stubs section. */
9173
9174 static void
9175 mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9176 {
9177 struct mips_elf_link_hash_table *htab;
9178 bfd_size_type dynsymcount;
9179
9180 htab = mips_elf_hash_table (info);
9181 BFD_ASSERT (htab != NULL);
9182
9183 if (htab->lazy_stub_count == 0)
9184 return;
9185
9186 /* IRIX rld assumes that a function stub isn't at the end of the .text
9187 section, so add a dummy entry to the end. */
9188 htab->lazy_stub_count++;
9189
9190 /* Get a worst-case estimate of the number of dynamic symbols needed.
9191 At this point, dynsymcount does not account for section symbols
9192 and count_section_dynsyms may overestimate the number that will
9193 be needed. */
9194 dynsymcount = (elf_hash_table (info)->dynsymcount
9195 + count_section_dynsyms (output_bfd, info));
9196
9197 /* Determine the size of one stub entry. There's no disadvantage
9198 from using microMIPS code here, so for the sake of pure-microMIPS
9199 binaries we prefer it whenever there's any microMIPS code in
9200 output produced at all. This has a benefit of stubs being
9201 shorter by 4 bytes each too, unless in the insn32 mode. */
9202 if (!MICROMIPS_P (output_bfd))
9203 htab->function_stub_size = (dynsymcount > 0x10000
9204 ? MIPS_FUNCTION_STUB_BIG_SIZE
9205 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
9206 else if (htab->insn32)
9207 htab->function_stub_size = (dynsymcount > 0x10000
9208 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9209 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9210 else
9211 htab->function_stub_size = (dynsymcount > 0x10000
9212 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9213 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
9214
9215 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9216 }
9217
9218 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9219 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9220 stub, allocate an entry in the stubs section. */
9221
9222 static bfd_boolean
9223 mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
9224 {
9225 struct mips_htab_traverse_info *hti = data;
9226 struct mips_elf_link_hash_table *htab;
9227 struct bfd_link_info *info;
9228 bfd *output_bfd;
9229
9230 info = hti->info;
9231 output_bfd = hti->output_bfd;
9232 htab = mips_elf_hash_table (info);
9233 BFD_ASSERT (htab != NULL);
9234
9235 if (h->needs_lazy_stub)
9236 {
9237 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9238 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9239 bfd_vma isa_bit = micromips_p;
9240
9241 BFD_ASSERT (htab->root.dynobj != NULL);
9242 if (h->root.plt.plist == NULL)
9243 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9244 if (h->root.plt.plist == NULL)
9245 {
9246 hti->error = TRUE;
9247 return FALSE;
9248 }
9249 h->root.root.u.def.section = htab->sstubs;
9250 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9251 h->root.plt.plist->stub_offset = htab->sstubs->size;
9252 h->root.other = other;
9253 htab->sstubs->size += htab->function_stub_size;
9254 }
9255 return TRUE;
9256 }
9257
9258 /* Allocate offsets in the stubs section to each symbol that needs one.
9259 Set the final size of the .MIPS.stub section. */
9260
9261 static bfd_boolean
9262 mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9263 {
9264 bfd *output_bfd = info->output_bfd;
9265 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9266 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9267 bfd_vma isa_bit = micromips_p;
9268 struct mips_elf_link_hash_table *htab;
9269 struct mips_htab_traverse_info hti;
9270 struct elf_link_hash_entry *h;
9271 bfd *dynobj;
9272
9273 htab = mips_elf_hash_table (info);
9274 BFD_ASSERT (htab != NULL);
9275
9276 if (htab->lazy_stub_count == 0)
9277 return TRUE;
9278
9279 htab->sstubs->size = 0;
9280 hti.info = info;
9281 hti.output_bfd = output_bfd;
9282 hti.error = FALSE;
9283 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9284 if (hti.error)
9285 return FALSE;
9286 htab->sstubs->size += htab->function_stub_size;
9287 BFD_ASSERT (htab->sstubs->size
9288 == htab->lazy_stub_count * htab->function_stub_size);
9289
9290 dynobj = elf_hash_table (info)->dynobj;
9291 BFD_ASSERT (dynobj != NULL);
9292 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9293 if (h == NULL)
9294 return FALSE;
9295 h->root.u.def.value = isa_bit;
9296 h->other = other;
9297 h->type = STT_FUNC;
9298
9299 return TRUE;
9300 }
9301
9302 /* A mips_elf_link_hash_traverse callback for which DATA points to a
9303 bfd_link_info. If H uses the address of a PLT entry as the value
9304 of the symbol, then set the entry in the symbol table now. Prefer
9305 a standard MIPS PLT entry. */
9306
9307 static bfd_boolean
9308 mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9309 {
9310 struct bfd_link_info *info = data;
9311 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9312 struct mips_elf_link_hash_table *htab;
9313 unsigned int other;
9314 bfd_vma isa_bit;
9315 bfd_vma val;
9316
9317 htab = mips_elf_hash_table (info);
9318 BFD_ASSERT (htab != NULL);
9319
9320 if (h->use_plt_entry)
9321 {
9322 BFD_ASSERT (h->root.plt.plist != NULL);
9323 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9324 || h->root.plt.plist->comp_offset != MINUS_ONE);
9325
9326 val = htab->plt_header_size;
9327 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9328 {
9329 isa_bit = 0;
9330 val += h->root.plt.plist->mips_offset;
9331 other = 0;
9332 }
9333 else
9334 {
9335 isa_bit = 1;
9336 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9337 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9338 }
9339 val += isa_bit;
9340 /* For VxWorks, point at the PLT load stub rather than the lazy
9341 resolution stub; this stub will become the canonical function
9342 address. */
9343 if (htab->is_vxworks)
9344 val += 8;
9345
9346 h->root.root.u.def.section = htab->splt;
9347 h->root.root.u.def.value = val;
9348 h->root.other = other;
9349 }
9350
9351 return TRUE;
9352 }
9353
9354 /* Set the sizes of the dynamic sections. */
9355
9356 bfd_boolean
9357 _bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9358 struct bfd_link_info *info)
9359 {
9360 bfd *dynobj;
9361 asection *s, *sreldyn;
9362 bfd_boolean reltext;
9363 struct mips_elf_link_hash_table *htab;
9364
9365 htab = mips_elf_hash_table (info);
9366 BFD_ASSERT (htab != NULL);
9367 dynobj = elf_hash_table (info)->dynobj;
9368 BFD_ASSERT (dynobj != NULL);
9369
9370 if (elf_hash_table (info)->dynamic_sections_created)
9371 {
9372 /* Set the contents of the .interp section to the interpreter. */
9373 if (info->executable)
9374 {
9375 s = bfd_get_linker_section (dynobj, ".interp");
9376 BFD_ASSERT (s != NULL);
9377 s->size
9378 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9379 s->contents
9380 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9381 }
9382
9383 /* Figure out the size of the PLT header if we know that we
9384 are using it. For the sake of cache alignment always use
9385 a standard header whenever any standard entries are present
9386 even if microMIPS entries are present as well. This also
9387 lets the microMIPS header rely on the value of $v0 only set
9388 by microMIPS entries, for a small size reduction.
9389
9390 Set symbol table entry values for symbols that use the
9391 address of their PLT entry now that we can calculate it.
9392
9393 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9394 haven't already in _bfd_elf_create_dynamic_sections. */
9395 if (htab->splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
9396 {
9397 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9398 && !htab->plt_mips_offset);
9399 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9400 bfd_vma isa_bit = micromips_p;
9401 struct elf_link_hash_entry *h;
9402 bfd_vma size;
9403
9404 BFD_ASSERT (htab->use_plts_and_copy_relocs);
9405 BFD_ASSERT (htab->sgotplt->size == 0);
9406 BFD_ASSERT (htab->splt->size == 0);
9407
9408 if (htab->is_vxworks && info->shared)
9409 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9410 else if (htab->is_vxworks)
9411 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9412 else if (ABI_64_P (output_bfd))
9413 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9414 else if (ABI_N32_P (output_bfd))
9415 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9416 else if (!micromips_p)
9417 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
9418 else if (htab->insn32)
9419 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
9420 else
9421 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
9422
9423 htab->plt_header_is_comp = micromips_p;
9424 htab->plt_header_size = size;
9425 htab->splt->size = (size
9426 + htab->plt_mips_offset
9427 + htab->plt_comp_offset);
9428 htab->sgotplt->size = (htab->plt_got_index
9429 * MIPS_ELF_GOT_SIZE (dynobj));
9430
9431 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9432
9433 if (htab->root.hplt == NULL)
9434 {
9435 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->splt,
9436 "_PROCEDURE_LINKAGE_TABLE_");
9437 htab->root.hplt = h;
9438 if (h == NULL)
9439 return FALSE;
9440 }
9441
9442 h = htab->root.hplt;
9443 h->root.u.def.value = isa_bit;
9444 h->other = other;
9445 h->type = STT_FUNC;
9446 }
9447 }
9448
9449 /* Allocate space for global sym dynamic relocs. */
9450 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9451
9452 mips_elf_estimate_stub_size (output_bfd, info);
9453
9454 if (!mips_elf_lay_out_got (output_bfd, info))
9455 return FALSE;
9456
9457 mips_elf_lay_out_lazy_stubs (info);
9458
9459 /* The check_relocs and adjust_dynamic_symbol entry points have
9460 determined the sizes of the various dynamic sections. Allocate
9461 memory for them. */
9462 reltext = FALSE;
9463 for (s = dynobj->sections; s != NULL; s = s->next)
9464 {
9465 const char *name;
9466
9467 /* It's OK to base decisions on the section name, because none
9468 of the dynobj section names depend upon the input files. */
9469 name = bfd_get_section_name (dynobj, s);
9470
9471 if ((s->flags & SEC_LINKER_CREATED) == 0)
9472 continue;
9473
9474 if (CONST_STRNEQ (name, ".rel"))
9475 {
9476 if (s->size != 0)
9477 {
9478 const char *outname;
9479 asection *target;
9480
9481 /* If this relocation section applies to a read only
9482 section, then we probably need a DT_TEXTREL entry.
9483 If the relocation section is .rel(a).dyn, we always
9484 assert a DT_TEXTREL entry rather than testing whether
9485 there exists a relocation to a read only section or
9486 not. */
9487 outname = bfd_get_section_name (output_bfd,
9488 s->output_section);
9489 target = bfd_get_section_by_name (output_bfd, outname + 4);
9490 if ((target != NULL
9491 && (target->flags & SEC_READONLY) != 0
9492 && (target->flags & SEC_ALLOC) != 0)
9493 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
9494 reltext = TRUE;
9495
9496 /* We use the reloc_count field as a counter if we need
9497 to copy relocs into the output file. */
9498 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
9499 s->reloc_count = 0;
9500
9501 /* If combreloc is enabled, elf_link_sort_relocs() will
9502 sort relocations, but in a different way than we do,
9503 and before we're done creating relocations. Also, it
9504 will move them around between input sections'
9505 relocation's contents, so our sorting would be
9506 broken, so don't let it run. */
9507 info->combreloc = 0;
9508 }
9509 }
9510 else if (! info->shared
9511 && ! mips_elf_hash_table (info)->use_rld_obj_head
9512 && CONST_STRNEQ (name, ".rld_map"))
9513 {
9514 /* We add a room for __rld_map. It will be filled in by the
9515 rtld to contain a pointer to the _r_debug structure. */
9516 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
9517 }
9518 else if (SGI_COMPAT (output_bfd)
9519 && CONST_STRNEQ (name, ".compact_rel"))
9520 s->size += mips_elf_hash_table (info)->compact_rel_size;
9521 else if (s == htab->splt)
9522 {
9523 /* If the last PLT entry has a branch delay slot, allocate
9524 room for an extra nop to fill the delay slot. This is
9525 for CPUs without load interlocking. */
9526 if (! LOAD_INTERLOCKS_P (output_bfd)
9527 && ! htab->is_vxworks && s->size > 0)
9528 s->size += 4;
9529 }
9530 else if (! CONST_STRNEQ (name, ".init")
9531 && s != htab->sgot
9532 && s != htab->sgotplt
9533 && s != htab->sstubs
9534 && s != htab->sdynbss)
9535 {
9536 /* It's not one of our sections, so don't allocate space. */
9537 continue;
9538 }
9539
9540 if (s->size == 0)
9541 {
9542 s->flags |= SEC_EXCLUDE;
9543 continue;
9544 }
9545
9546 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9547 continue;
9548
9549 /* Allocate memory for the section contents. */
9550 s->contents = bfd_zalloc (dynobj, s->size);
9551 if (s->contents == NULL)
9552 {
9553 bfd_set_error (bfd_error_no_memory);
9554 return FALSE;
9555 }
9556 }
9557
9558 if (elf_hash_table (info)->dynamic_sections_created)
9559 {
9560 /* Add some entries to the .dynamic section. We fill in the
9561 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
9562 must add the entries now so that we get the correct size for
9563 the .dynamic section. */
9564
9565 /* SGI object has the equivalence of DT_DEBUG in the
9566 DT_MIPS_RLD_MAP entry. This must come first because glibc
9567 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
9568 may only look at the first one they see. */
9569 if (!info->shared
9570 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
9571 return FALSE;
9572
9573 /* The DT_DEBUG entry may be filled in by the dynamic linker and
9574 used by the debugger. */
9575 if (info->executable
9576 && !SGI_COMPAT (output_bfd)
9577 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
9578 return FALSE;
9579
9580 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
9581 info->flags |= DF_TEXTREL;
9582
9583 if ((info->flags & DF_TEXTREL) != 0)
9584 {
9585 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
9586 return FALSE;
9587
9588 /* Clear the DF_TEXTREL flag. It will be set again if we
9589 write out an actual text relocation; we may not, because
9590 at this point we do not know whether e.g. any .eh_frame
9591 absolute relocations have been converted to PC-relative. */
9592 info->flags &= ~DF_TEXTREL;
9593 }
9594
9595 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
9596 return FALSE;
9597
9598 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
9599 if (htab->is_vxworks)
9600 {
9601 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
9602 use any of the DT_MIPS_* tags. */
9603 if (sreldyn && sreldyn->size > 0)
9604 {
9605 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
9606 return FALSE;
9607
9608 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
9609 return FALSE;
9610
9611 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
9612 return FALSE;
9613 }
9614 }
9615 else
9616 {
9617 if (sreldyn && sreldyn->size > 0)
9618 {
9619 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
9620 return FALSE;
9621
9622 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
9623 return FALSE;
9624
9625 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
9626 return FALSE;
9627 }
9628
9629 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
9630 return FALSE;
9631
9632 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
9633 return FALSE;
9634
9635 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
9636 return FALSE;
9637
9638 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
9639 return FALSE;
9640
9641 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
9642 return FALSE;
9643
9644 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
9645 return FALSE;
9646
9647 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
9648 return FALSE;
9649
9650 if (IRIX_COMPAT (dynobj) == ict_irix5
9651 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
9652 return FALSE;
9653
9654 if (IRIX_COMPAT (dynobj) == ict_irix6
9655 && (bfd_get_section_by_name
9656 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
9657 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
9658 return FALSE;
9659 }
9660 if (htab->splt->size > 0)
9661 {
9662 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
9663 return FALSE;
9664
9665 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
9666 return FALSE;
9667
9668 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
9669 return FALSE;
9670
9671 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
9672 return FALSE;
9673 }
9674 if (htab->is_vxworks
9675 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
9676 return FALSE;
9677 }
9678
9679 return TRUE;
9680 }
9681 \f
9682 /* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
9683 Adjust its R_ADDEND field so that it is correct for the output file.
9684 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
9685 and sections respectively; both use symbol indexes. */
9686
9687 static void
9688 mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
9689 bfd *input_bfd, Elf_Internal_Sym *local_syms,
9690 asection **local_sections, Elf_Internal_Rela *rel)
9691 {
9692 unsigned int r_type, r_symndx;
9693 Elf_Internal_Sym *sym;
9694 asection *sec;
9695
9696 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9697 {
9698 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9699 if (gprel16_reloc_p (r_type)
9700 || r_type == R_MIPS_GPREL32
9701 || literal_reloc_p (r_type))
9702 {
9703 rel->r_addend += _bfd_get_gp_value (input_bfd);
9704 rel->r_addend -= _bfd_get_gp_value (output_bfd);
9705 }
9706
9707 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
9708 sym = local_syms + r_symndx;
9709
9710 /* Adjust REL's addend to account for section merging. */
9711 if (!info->relocatable)
9712 {
9713 sec = local_sections[r_symndx];
9714 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9715 }
9716
9717 /* This would normally be done by the rela_normal code in elflink.c. */
9718 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9719 rel->r_addend += local_sections[r_symndx]->output_offset;
9720 }
9721 }
9722
9723 /* Handle relocations against symbols from removed linkonce sections,
9724 or sections discarded by a linker script. We use this wrapper around
9725 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
9726 on 64-bit ELF targets. In this case for any relocation handled, which
9727 always be the first in a triplet, the remaining two have to be processed
9728 together with the first, even if they are R_MIPS_NONE. It is the symbol
9729 index referred by the first reloc that applies to all the three and the
9730 remaining two never refer to an object symbol. And it is the final
9731 relocation (the last non-null one) that determines the output field of
9732 the whole relocation so retrieve the corresponding howto structure for
9733 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
9734
9735 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
9736 and therefore requires to be pasted in a loop. It also defines a block
9737 and does not protect any of its arguments, hence the extra brackets. */
9738
9739 static void
9740 mips_reloc_against_discarded_section (bfd *output_bfd,
9741 struct bfd_link_info *info,
9742 bfd *input_bfd, asection *input_section,
9743 Elf_Internal_Rela **rel,
9744 const Elf_Internal_Rela **relend,
9745 bfd_boolean rel_reloc,
9746 reloc_howto_type *howto,
9747 bfd_byte *contents)
9748 {
9749 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9750 int count = bed->s->int_rels_per_ext_rel;
9751 unsigned int r_type;
9752 int i;
9753
9754 for (i = count - 1; i > 0; i--)
9755 {
9756 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
9757 if (r_type != R_MIPS_NONE)
9758 {
9759 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9760 break;
9761 }
9762 }
9763 do
9764 {
9765 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
9766 (*rel), count, (*relend),
9767 howto, i, contents);
9768 }
9769 while (0);
9770 }
9771
9772 /* Relocate a MIPS ELF section. */
9773
9774 bfd_boolean
9775 _bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
9776 bfd *input_bfd, asection *input_section,
9777 bfd_byte *contents, Elf_Internal_Rela *relocs,
9778 Elf_Internal_Sym *local_syms,
9779 asection **local_sections)
9780 {
9781 Elf_Internal_Rela *rel;
9782 const Elf_Internal_Rela *relend;
9783 bfd_vma addend = 0;
9784 bfd_boolean use_saved_addend_p = FALSE;
9785 const struct elf_backend_data *bed;
9786
9787 bed = get_elf_backend_data (output_bfd);
9788 relend = relocs + input_section->reloc_count * bed->s->int_rels_per_ext_rel;
9789 for (rel = relocs; rel < relend; ++rel)
9790 {
9791 const char *name;
9792 bfd_vma value = 0;
9793 reloc_howto_type *howto;
9794 bfd_boolean cross_mode_jump_p = FALSE;
9795 /* TRUE if the relocation is a RELA relocation, rather than a
9796 REL relocation. */
9797 bfd_boolean rela_relocation_p = TRUE;
9798 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9799 const char *msg;
9800 unsigned long r_symndx;
9801 asection *sec;
9802 Elf_Internal_Shdr *symtab_hdr;
9803 struct elf_link_hash_entry *h;
9804 bfd_boolean rel_reloc;
9805
9806 rel_reloc = (NEWABI_P (input_bfd)
9807 && mips_elf_rel_relocation_p (input_bfd, input_section,
9808 relocs, rel));
9809 /* Find the relocation howto for this relocation. */
9810 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
9811
9812 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
9813 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9814 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
9815 {
9816 sec = local_sections[r_symndx];
9817 h = NULL;
9818 }
9819 else
9820 {
9821 unsigned long extsymoff;
9822
9823 extsymoff = 0;
9824 if (!elf_bad_symtab (input_bfd))
9825 extsymoff = symtab_hdr->sh_info;
9826 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
9827 while (h->root.type == bfd_link_hash_indirect
9828 || h->root.type == bfd_link_hash_warning)
9829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9830
9831 sec = NULL;
9832 if (h->root.type == bfd_link_hash_defined
9833 || h->root.type == bfd_link_hash_defweak)
9834 sec = h->root.u.def.section;
9835 }
9836
9837 if (sec != NULL && discarded_section (sec))
9838 {
9839 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
9840 input_section, &rel, &relend,
9841 rel_reloc, howto, contents);
9842 continue;
9843 }
9844
9845 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
9846 {
9847 /* Some 32-bit code uses R_MIPS_64. In particular, people use
9848 64-bit code, but make sure all their addresses are in the
9849 lowermost or uppermost 32-bit section of the 64-bit address
9850 space. Thus, when they use an R_MIPS_64 they mean what is
9851 usually meant by R_MIPS_32, with the exception that the
9852 stored value is sign-extended to 64 bits. */
9853 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
9854
9855 /* On big-endian systems, we need to lie about the position
9856 of the reloc. */
9857 if (bfd_big_endian (input_bfd))
9858 rel->r_offset += 4;
9859 }
9860
9861 if (!use_saved_addend_p)
9862 {
9863 /* If these relocations were originally of the REL variety,
9864 we must pull the addend out of the field that will be
9865 relocated. Otherwise, we simply use the contents of the
9866 RELA relocation. */
9867 if (mips_elf_rel_relocation_p (input_bfd, input_section,
9868 relocs, rel))
9869 {
9870 rela_relocation_p = FALSE;
9871 addend = mips_elf_read_rel_addend (input_bfd, rel,
9872 howto, contents);
9873 if (hi16_reloc_p (r_type)
9874 || (got16_reloc_p (r_type)
9875 && mips_elf_local_relocation_p (input_bfd, rel,
9876 local_sections)))
9877 {
9878 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
9879 contents, &addend))
9880 {
9881 if (h)
9882 name = h->root.root.string;
9883 else
9884 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9885 local_syms + r_symndx,
9886 sec);
9887 (*_bfd_error_handler)
9888 (_("%B: Can't find matching LO16 reloc against `%s' for %s at 0x%lx in section `%A'"),
9889 input_bfd, input_section, name, howto->name,
9890 rel->r_offset);
9891 }
9892 }
9893 else
9894 addend <<= howto->rightshift;
9895 }
9896 else
9897 addend = rel->r_addend;
9898 mips_elf_adjust_addend (output_bfd, info, input_bfd,
9899 local_syms, local_sections, rel);
9900 }
9901
9902 if (info->relocatable)
9903 {
9904 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
9905 && bfd_big_endian (input_bfd))
9906 rel->r_offset -= 4;
9907
9908 if (!rela_relocation_p && rel->r_addend)
9909 {
9910 addend += rel->r_addend;
9911 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
9912 addend = mips_elf_high (addend);
9913 else if (r_type == R_MIPS_HIGHER)
9914 addend = mips_elf_higher (addend);
9915 else if (r_type == R_MIPS_HIGHEST)
9916 addend = mips_elf_highest (addend);
9917 else
9918 addend >>= howto->rightshift;
9919
9920 /* We use the source mask, rather than the destination
9921 mask because the place to which we are writing will be
9922 source of the addend in the final link. */
9923 addend &= howto->src_mask;
9924
9925 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
9926 /* See the comment above about using R_MIPS_64 in the 32-bit
9927 ABI. Here, we need to update the addend. It would be
9928 possible to get away with just using the R_MIPS_32 reloc
9929 but for endianness. */
9930 {
9931 bfd_vma sign_bits;
9932 bfd_vma low_bits;
9933 bfd_vma high_bits;
9934
9935 if (addend & ((bfd_vma) 1 << 31))
9936 #ifdef BFD64
9937 sign_bits = ((bfd_vma) 1 << 32) - 1;
9938 #else
9939 sign_bits = -1;
9940 #endif
9941 else
9942 sign_bits = 0;
9943
9944 /* If we don't know that we have a 64-bit type,
9945 do two separate stores. */
9946 if (bfd_big_endian (input_bfd))
9947 {
9948 /* Store the sign-bits (which are most significant)
9949 first. */
9950 low_bits = sign_bits;
9951 high_bits = addend;
9952 }
9953 else
9954 {
9955 low_bits = addend;
9956 high_bits = sign_bits;
9957 }
9958 bfd_put_32 (input_bfd, low_bits,
9959 contents + rel->r_offset);
9960 bfd_put_32 (input_bfd, high_bits,
9961 contents + rel->r_offset + 4);
9962 continue;
9963 }
9964
9965 if (! mips_elf_perform_relocation (info, howto, rel, addend,
9966 input_bfd, input_section,
9967 contents, FALSE))
9968 return FALSE;
9969 }
9970
9971 /* Go on to the next relocation. */
9972 continue;
9973 }
9974
9975 /* In the N32 and 64-bit ABIs there may be multiple consecutive
9976 relocations for the same offset. In that case we are
9977 supposed to treat the output of each relocation as the addend
9978 for the next. */
9979 if (rel + 1 < relend
9980 && rel->r_offset == rel[1].r_offset
9981 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
9982 use_saved_addend_p = TRUE;
9983 else
9984 use_saved_addend_p = FALSE;
9985
9986 /* Figure out what value we are supposed to relocate. */
9987 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
9988 input_section, info, rel,
9989 addend, howto, local_syms,
9990 local_sections, &value,
9991 &name, &cross_mode_jump_p,
9992 use_saved_addend_p))
9993 {
9994 case bfd_reloc_continue:
9995 /* There's nothing to do. */
9996 continue;
9997
9998 case bfd_reloc_undefined:
9999 /* mips_elf_calculate_relocation already called the
10000 undefined_symbol callback. There's no real point in
10001 trying to perform the relocation at this point, so we
10002 just skip ahead to the next relocation. */
10003 continue;
10004
10005 case bfd_reloc_notsupported:
10006 msg = _("internal error: unsupported relocation error");
10007 info->callbacks->warning
10008 (info, msg, name, input_bfd, input_section, rel->r_offset);
10009 return FALSE;
10010
10011 case bfd_reloc_overflow:
10012 if (use_saved_addend_p)
10013 /* Ignore overflow until we reach the last relocation for
10014 a given location. */
10015 ;
10016 else
10017 {
10018 struct mips_elf_link_hash_table *htab;
10019
10020 htab = mips_elf_hash_table (info);
10021 BFD_ASSERT (htab != NULL);
10022 BFD_ASSERT (name != NULL);
10023 if (!htab->small_data_overflow_reported
10024 && (gprel16_reloc_p (howto->type)
10025 || literal_reloc_p (howto->type)))
10026 {
10027 msg = _("small-data section exceeds 64KB;"
10028 " lower small-data size limit (see option -G)");
10029
10030 htab->small_data_overflow_reported = TRUE;
10031 (*info->callbacks->einfo) ("%P: %s\n", msg);
10032 }
10033 if (! ((*info->callbacks->reloc_overflow)
10034 (info, NULL, name, howto->name, (bfd_vma) 0,
10035 input_bfd, input_section, rel->r_offset)))
10036 return FALSE;
10037 }
10038 break;
10039
10040 case bfd_reloc_ok:
10041 break;
10042
10043 case bfd_reloc_outofrange:
10044 if (jal_reloc_p (howto->type))
10045 {
10046 msg = _("JALX to a non-word-aligned address");
10047 info->callbacks->warning
10048 (info, msg, name, input_bfd, input_section, rel->r_offset);
10049 return FALSE;
10050 }
10051 /* Fall through. */
10052
10053 default:
10054 abort ();
10055 break;
10056 }
10057
10058 /* If we've got another relocation for the address, keep going
10059 until we reach the last one. */
10060 if (use_saved_addend_p)
10061 {
10062 addend = value;
10063 continue;
10064 }
10065
10066 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
10067 /* See the comment above about using R_MIPS_64 in the 32-bit
10068 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10069 that calculated the right value. Now, however, we
10070 sign-extend the 32-bit result to 64-bits, and store it as a
10071 64-bit value. We are especially generous here in that we
10072 go to extreme lengths to support this usage on systems with
10073 only a 32-bit VMA. */
10074 {
10075 bfd_vma sign_bits;
10076 bfd_vma low_bits;
10077 bfd_vma high_bits;
10078
10079 if (value & ((bfd_vma) 1 << 31))
10080 #ifdef BFD64
10081 sign_bits = ((bfd_vma) 1 << 32) - 1;
10082 #else
10083 sign_bits = -1;
10084 #endif
10085 else
10086 sign_bits = 0;
10087
10088 /* If we don't know that we have a 64-bit type,
10089 do two separate stores. */
10090 if (bfd_big_endian (input_bfd))
10091 {
10092 /* Undo what we did above. */
10093 rel->r_offset -= 4;
10094 /* Store the sign-bits (which are most significant)
10095 first. */
10096 low_bits = sign_bits;
10097 high_bits = value;
10098 }
10099 else
10100 {
10101 low_bits = value;
10102 high_bits = sign_bits;
10103 }
10104 bfd_put_32 (input_bfd, low_bits,
10105 contents + rel->r_offset);
10106 bfd_put_32 (input_bfd, high_bits,
10107 contents + rel->r_offset + 4);
10108 continue;
10109 }
10110
10111 /* Actually perform the relocation. */
10112 if (! mips_elf_perform_relocation (info, howto, rel, value,
10113 input_bfd, input_section,
10114 contents, cross_mode_jump_p))
10115 return FALSE;
10116 }
10117
10118 return TRUE;
10119 }
10120 \f
10121 /* A function that iterates over each entry in la25_stubs and fills
10122 in the code for each one. DATA points to a mips_htab_traverse_info. */
10123
10124 static int
10125 mips_elf_create_la25_stub (void **slot, void *data)
10126 {
10127 struct mips_htab_traverse_info *hti;
10128 struct mips_elf_link_hash_table *htab;
10129 struct mips_elf_la25_stub *stub;
10130 asection *s;
10131 bfd_byte *loc;
10132 bfd_vma offset, target, target_high, target_low;
10133
10134 stub = (struct mips_elf_la25_stub *) *slot;
10135 hti = (struct mips_htab_traverse_info *) data;
10136 htab = mips_elf_hash_table (hti->info);
10137 BFD_ASSERT (htab != NULL);
10138
10139 /* Create the section contents, if we haven't already. */
10140 s = stub->stub_section;
10141 loc = s->contents;
10142 if (loc == NULL)
10143 {
10144 loc = bfd_malloc (s->size);
10145 if (loc == NULL)
10146 {
10147 hti->error = TRUE;
10148 return FALSE;
10149 }
10150 s->contents = loc;
10151 }
10152
10153 /* Work out where in the section this stub should go. */
10154 offset = stub->offset;
10155
10156 /* Work out the target address. */
10157 target = mips_elf_get_la25_target (stub, &s);
10158 target += s->output_section->vma + s->output_offset;
10159
10160 target_high = ((target + 0x8000) >> 16) & 0xffff;
10161 target_low = (target & 0xffff);
10162
10163 if (stub->stub_section != htab->strampoline)
10164 {
10165 /* This is a simple LUI/ADDIU stub. Zero out the beginning
10166 of the section and write the two instructions at the end. */
10167 memset (loc, 0, offset);
10168 loc += offset;
10169 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10170 {
10171 bfd_put_micromips_32 (hti->output_bfd,
10172 LA25_LUI_MICROMIPS (target_high),
10173 loc);
10174 bfd_put_micromips_32 (hti->output_bfd,
10175 LA25_ADDIU_MICROMIPS (target_low),
10176 loc + 4);
10177 }
10178 else
10179 {
10180 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10181 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10182 }
10183 }
10184 else
10185 {
10186 /* This is trampoline. */
10187 loc += offset;
10188 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10189 {
10190 bfd_put_micromips_32 (hti->output_bfd,
10191 LA25_LUI_MICROMIPS (target_high), loc);
10192 bfd_put_micromips_32 (hti->output_bfd,
10193 LA25_J_MICROMIPS (target), loc + 4);
10194 bfd_put_micromips_32 (hti->output_bfd,
10195 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
10196 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10197 }
10198 else
10199 {
10200 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10201 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10202 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10203 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10204 }
10205 }
10206 return TRUE;
10207 }
10208
10209 /* If NAME is one of the special IRIX6 symbols defined by the linker,
10210 adjust it appropriately now. */
10211
10212 static void
10213 mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10214 const char *name, Elf_Internal_Sym *sym)
10215 {
10216 /* The linker script takes care of providing names and values for
10217 these, but we must place them into the right sections. */
10218 static const char* const text_section_symbols[] = {
10219 "_ftext",
10220 "_etext",
10221 "__dso_displacement",
10222 "__elf_header",
10223 "__program_header_table",
10224 NULL
10225 };
10226
10227 static const char* const data_section_symbols[] = {
10228 "_fdata",
10229 "_edata",
10230 "_end",
10231 "_fbss",
10232 NULL
10233 };
10234
10235 const char* const *p;
10236 int i;
10237
10238 for (i = 0; i < 2; ++i)
10239 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10240 *p;
10241 ++p)
10242 if (strcmp (*p, name) == 0)
10243 {
10244 /* All of these symbols are given type STT_SECTION by the
10245 IRIX6 linker. */
10246 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10247 sym->st_other = STO_PROTECTED;
10248
10249 /* The IRIX linker puts these symbols in special sections. */
10250 if (i == 0)
10251 sym->st_shndx = SHN_MIPS_TEXT;
10252 else
10253 sym->st_shndx = SHN_MIPS_DATA;
10254
10255 break;
10256 }
10257 }
10258
10259 /* Finish up dynamic symbol handling. We set the contents of various
10260 dynamic sections here. */
10261
10262 bfd_boolean
10263 _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10264 struct bfd_link_info *info,
10265 struct elf_link_hash_entry *h,
10266 Elf_Internal_Sym *sym)
10267 {
10268 bfd *dynobj;
10269 asection *sgot;
10270 struct mips_got_info *g, *gg;
10271 const char *name;
10272 int idx;
10273 struct mips_elf_link_hash_table *htab;
10274 struct mips_elf_link_hash_entry *hmips;
10275
10276 htab = mips_elf_hash_table (info);
10277 BFD_ASSERT (htab != NULL);
10278 dynobj = elf_hash_table (info)->dynobj;
10279 hmips = (struct mips_elf_link_hash_entry *) h;
10280
10281 BFD_ASSERT (!htab->is_vxworks);
10282
10283 if (h->plt.plist != NULL
10284 && (h->plt.plist->mips_offset != MINUS_ONE
10285 || h->plt.plist->comp_offset != MINUS_ONE))
10286 {
10287 /* We've decided to create a PLT entry for this symbol. */
10288 bfd_byte *loc;
10289 bfd_vma header_address, got_address;
10290 bfd_vma got_address_high, got_address_low, load;
10291 bfd_vma got_index;
10292 bfd_vma isa_bit;
10293
10294 got_index = h->plt.plist->gotplt_index;
10295
10296 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10297 BFD_ASSERT (h->dynindx != -1);
10298 BFD_ASSERT (htab->splt != NULL);
10299 BFD_ASSERT (got_index != MINUS_ONE);
10300 BFD_ASSERT (!h->def_regular);
10301
10302 /* Calculate the address of the PLT header. */
10303 isa_bit = htab->plt_header_is_comp;
10304 header_address = (htab->splt->output_section->vma
10305 + htab->splt->output_offset + isa_bit);
10306
10307 /* Calculate the address of the .got.plt entry. */
10308 got_address = (htab->sgotplt->output_section->vma
10309 + htab->sgotplt->output_offset
10310 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10311
10312 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10313 got_address_low = got_address & 0xffff;
10314
10315 /* Initially point the .got.plt entry at the PLT header. */
10316 loc = (htab->sgotplt->contents + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10317 if (ABI_64_P (output_bfd))
10318 bfd_put_64 (output_bfd, header_address, loc);
10319 else
10320 bfd_put_32 (output_bfd, header_address, loc);
10321
10322 /* Now handle the PLT itself. First the standard entry (the order
10323 does not matter, we just have to pick one). */
10324 if (h->plt.plist->mips_offset != MINUS_ONE)
10325 {
10326 const bfd_vma *plt_entry;
10327 bfd_vma plt_offset;
10328
10329 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10330
10331 BFD_ASSERT (plt_offset <= htab->splt->size);
10332
10333 /* Find out where the .plt entry should go. */
10334 loc = htab->splt->contents + plt_offset;
10335
10336 /* Pick the load opcode. */
10337 load = MIPS_ELF_LOAD_WORD (output_bfd);
10338
10339 /* Fill in the PLT entry itself. */
10340 plt_entry = mips_exec_plt_entry;
10341 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10342 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10343 loc + 4);
10344
10345 if (! LOAD_INTERLOCKS_P (output_bfd))
10346 {
10347 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10348 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10349 }
10350 else
10351 {
10352 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10353 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10354 loc + 12);
10355 }
10356 }
10357
10358 /* Now the compressed entry. They come after any standard ones. */
10359 if (h->plt.plist->comp_offset != MINUS_ONE)
10360 {
10361 bfd_vma plt_offset;
10362
10363 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10364 + h->plt.plist->comp_offset);
10365
10366 BFD_ASSERT (plt_offset <= htab->splt->size);
10367
10368 /* Find out where the .plt entry should go. */
10369 loc = htab->splt->contents + plt_offset;
10370
10371 /* Fill in the PLT entry itself. */
10372 if (!MICROMIPS_P (output_bfd))
10373 {
10374 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10375
10376 bfd_put_16 (output_bfd, plt_entry[0], loc);
10377 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10378 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10379 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10380 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10381 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10382 bfd_put_32 (output_bfd, got_address, loc + 12);
10383 }
10384 else if (htab->insn32)
10385 {
10386 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10387
10388 bfd_put_16 (output_bfd, plt_entry[0], loc);
10389 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10390 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10391 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10392 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10393 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10394 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10395 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10396 }
10397 else
10398 {
10399 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10400 bfd_signed_vma gotpc_offset;
10401 bfd_vma loc_address;
10402
10403 BFD_ASSERT (got_address % 4 == 0);
10404
10405 loc_address = (htab->splt->output_section->vma
10406 + htab->splt->output_offset + plt_offset);
10407 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10408
10409 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10410 if (gotpc_offset + 0x1000000 >= 0x2000000)
10411 {
10412 (*_bfd_error_handler)
10413 (_("%B: `%A' offset of %ld from `%A' "
10414 "beyond the range of ADDIUPC"),
10415 output_bfd,
10416 htab->sgotplt->output_section,
10417 htab->splt->output_section,
10418 (long) gotpc_offset);
10419 bfd_set_error (bfd_error_no_error);
10420 return FALSE;
10421 }
10422 bfd_put_16 (output_bfd,
10423 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10424 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10425 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10426 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10427 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10428 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10429 }
10430 }
10431
10432 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10433 mips_elf_output_dynamic_relocation (output_bfd, htab->srelplt,
10434 got_index - 2, h->dynindx,
10435 R_MIPS_JUMP_SLOT, got_address);
10436
10437 /* We distinguish between PLT entries and lazy-binding stubs by
10438 giving the former an st_other value of STO_MIPS_PLT. Set the
10439 flag and leave the value if there are any relocations in the
10440 binary where pointer equality matters. */
10441 sym->st_shndx = SHN_UNDEF;
10442 if (h->pointer_equality_needed)
10443 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
10444 else
10445 {
10446 sym->st_value = 0;
10447 sym->st_other = 0;
10448 }
10449 }
10450
10451 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
10452 {
10453 /* We've decided to create a lazy-binding stub. */
10454 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10455 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10456 bfd_vma stub_size = htab->function_stub_size;
10457 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
10458 bfd_vma isa_bit = micromips_p;
10459 bfd_vma stub_big_size;
10460
10461 if (!micromips_p)
10462 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
10463 else if (htab->insn32)
10464 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10465 else
10466 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
10467
10468 /* This symbol has a stub. Set it up. */
10469
10470 BFD_ASSERT (h->dynindx != -1);
10471
10472 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
10473
10474 /* Values up to 2^31 - 1 are allowed. Larger values would cause
10475 sign extension at runtime in the stub, resulting in a negative
10476 index value. */
10477 if (h->dynindx & ~0x7fffffff)
10478 return FALSE;
10479
10480 /* Fill the stub. */
10481 if (micromips_p)
10482 {
10483 idx = 0;
10484 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
10485 stub + idx);
10486 idx += 4;
10487 if (htab->insn32)
10488 {
10489 bfd_put_micromips_32 (output_bfd,
10490 STUB_MOVE32_MICROMIPS (output_bfd),
10491 stub + idx);
10492 idx += 4;
10493 }
10494 else
10495 {
10496 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
10497 idx += 2;
10498 }
10499 if (stub_size == stub_big_size)
10500 {
10501 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
10502
10503 bfd_put_micromips_32 (output_bfd,
10504 STUB_LUI_MICROMIPS (dynindx_hi),
10505 stub + idx);
10506 idx += 4;
10507 }
10508 if (htab->insn32)
10509 {
10510 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
10511 stub + idx);
10512 idx += 4;
10513 }
10514 else
10515 {
10516 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
10517 idx += 2;
10518 }
10519
10520 /* If a large stub is not required and sign extension is not a
10521 problem, then use legacy code in the stub. */
10522 if (stub_size == stub_big_size)
10523 bfd_put_micromips_32 (output_bfd,
10524 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
10525 stub + idx);
10526 else if (h->dynindx & ~0x7fff)
10527 bfd_put_micromips_32 (output_bfd,
10528 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
10529 stub + idx);
10530 else
10531 bfd_put_micromips_32 (output_bfd,
10532 STUB_LI16S_MICROMIPS (output_bfd,
10533 h->dynindx),
10534 stub + idx);
10535 }
10536 else
10537 {
10538 idx = 0;
10539 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
10540 idx += 4;
10541 bfd_put_32 (output_bfd, STUB_MOVE (output_bfd), stub + idx);
10542 idx += 4;
10543 if (stub_size == stub_big_size)
10544 {
10545 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
10546 stub + idx);
10547 idx += 4;
10548 }
10549 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
10550 idx += 4;
10551
10552 /* If a large stub is not required and sign extension is not a
10553 problem, then use legacy code in the stub. */
10554 if (stub_size == stub_big_size)
10555 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
10556 stub + idx);
10557 else if (h->dynindx & ~0x7fff)
10558 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
10559 stub + idx);
10560 else
10561 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
10562 stub + idx);
10563 }
10564
10565 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
10566 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
10567 stub, stub_size);
10568
10569 /* Mark the symbol as undefined. stub_offset != -1 occurs
10570 only for the referenced symbol. */
10571 sym->st_shndx = SHN_UNDEF;
10572
10573 /* The run-time linker uses the st_value field of the symbol
10574 to reset the global offset table entry for this external
10575 to its stub address when unlinking a shared object. */
10576 sym->st_value = (htab->sstubs->output_section->vma
10577 + htab->sstubs->output_offset
10578 + h->plt.plist->stub_offset
10579 + isa_bit);
10580 sym->st_other = other;
10581 }
10582
10583 /* If we have a MIPS16 function with a stub, the dynamic symbol must
10584 refer to the stub, since only the stub uses the standard calling
10585 conventions. */
10586 if (h->dynindx != -1 && hmips->fn_stub != NULL)
10587 {
10588 BFD_ASSERT (hmips->need_fn_stub);
10589 sym->st_value = (hmips->fn_stub->output_section->vma
10590 + hmips->fn_stub->output_offset);
10591 sym->st_size = hmips->fn_stub->size;
10592 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
10593 }
10594
10595 BFD_ASSERT (h->dynindx != -1
10596 || h->forced_local);
10597
10598 sgot = htab->sgot;
10599 g = htab->got_info;
10600 BFD_ASSERT (g != NULL);
10601
10602 /* Run through the global symbol table, creating GOT entries for all
10603 the symbols that need them. */
10604 if (hmips->global_got_area != GGA_NONE)
10605 {
10606 bfd_vma offset;
10607 bfd_vma value;
10608
10609 value = sym->st_value;
10610 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10611 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
10612 }
10613
10614 if (hmips->global_got_area != GGA_NONE && g->next)
10615 {
10616 struct mips_got_entry e, *p;
10617 bfd_vma entry;
10618 bfd_vma offset;
10619
10620 gg = g;
10621
10622 e.abfd = output_bfd;
10623 e.symndx = -1;
10624 e.d.h = hmips;
10625 e.tls_type = GOT_TLS_NONE;
10626
10627 for (g = g->next; g->next != gg; g = g->next)
10628 {
10629 if (g->got_entries
10630 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
10631 &e)))
10632 {
10633 offset = p->gotidx;
10634 BFD_ASSERT (offset > 0 && offset < htab->sgot->size);
10635 if (info->shared
10636 || (elf_hash_table (info)->dynamic_sections_created
10637 && p->d.h != NULL
10638 && p->d.h->root.def_dynamic
10639 && !p->d.h->root.def_regular))
10640 {
10641 /* Create an R_MIPS_REL32 relocation for this entry. Due to
10642 the various compatibility problems, it's easier to mock
10643 up an R_MIPS_32 or R_MIPS_64 relocation and leave
10644 mips_elf_create_dynamic_relocation to calculate the
10645 appropriate addend. */
10646 Elf_Internal_Rela rel[3];
10647
10648 memset (rel, 0, sizeof (rel));
10649 if (ABI_64_P (output_bfd))
10650 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
10651 else
10652 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
10653 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
10654
10655 entry = 0;
10656 if (! (mips_elf_create_dynamic_relocation
10657 (output_bfd, info, rel,
10658 e.d.h, NULL, sym->st_value, &entry, sgot)))
10659 return FALSE;
10660 }
10661 else
10662 entry = sym->st_value;
10663 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
10664 }
10665 }
10666 }
10667
10668 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
10669 name = h->root.root.string;
10670 if (h == elf_hash_table (info)->hdynamic
10671 || h == elf_hash_table (info)->hgot)
10672 sym->st_shndx = SHN_ABS;
10673 else if (strcmp (name, "_DYNAMIC_LINK") == 0
10674 || strcmp (name, "_DYNAMIC_LINKING") == 0)
10675 {
10676 sym->st_shndx = SHN_ABS;
10677 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10678 sym->st_value = 1;
10679 }
10680 else if (strcmp (name, "_gp_disp") == 0 && ! NEWABI_P (output_bfd))
10681 {
10682 sym->st_shndx = SHN_ABS;
10683 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10684 sym->st_value = elf_gp (output_bfd);
10685 }
10686 else if (SGI_COMPAT (output_bfd))
10687 {
10688 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
10689 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
10690 {
10691 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10692 sym->st_other = STO_PROTECTED;
10693 sym->st_value = 0;
10694 sym->st_shndx = SHN_MIPS_DATA;
10695 }
10696 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
10697 {
10698 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
10699 sym->st_other = STO_PROTECTED;
10700 sym->st_value = mips_elf_hash_table (info)->procedure_count;
10701 sym->st_shndx = SHN_ABS;
10702 }
10703 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
10704 {
10705 if (h->type == STT_FUNC)
10706 sym->st_shndx = SHN_MIPS_TEXT;
10707 else if (h->type == STT_OBJECT)
10708 sym->st_shndx = SHN_MIPS_DATA;
10709 }
10710 }
10711
10712 /* Emit a copy reloc, if needed. */
10713 if (h->needs_copy)
10714 {
10715 asection *s;
10716 bfd_vma symval;
10717
10718 BFD_ASSERT (h->dynindx != -1);
10719 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10720
10721 s = mips_elf_rel_dyn_section (info, FALSE);
10722 symval = (h->root.u.def.section->output_section->vma
10723 + h->root.u.def.section->output_offset
10724 + h->root.u.def.value);
10725 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
10726 h->dynindx, R_MIPS_COPY, symval);
10727 }
10728
10729 /* Handle the IRIX6-specific symbols. */
10730 if (IRIX_COMPAT (output_bfd) == ict_irix6)
10731 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
10732
10733 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
10734 to treat compressed symbols like any other. */
10735 if (ELF_ST_IS_MIPS16 (sym->st_other))
10736 {
10737 BFD_ASSERT (sym->st_value & 1);
10738 sym->st_other -= STO_MIPS16;
10739 }
10740 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
10741 {
10742 BFD_ASSERT (sym->st_value & 1);
10743 sym->st_other -= STO_MICROMIPS;
10744 }
10745
10746 return TRUE;
10747 }
10748
10749 /* Likewise, for VxWorks. */
10750
10751 bfd_boolean
10752 _bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
10753 struct bfd_link_info *info,
10754 struct elf_link_hash_entry *h,
10755 Elf_Internal_Sym *sym)
10756 {
10757 bfd *dynobj;
10758 asection *sgot;
10759 struct mips_got_info *g;
10760 struct mips_elf_link_hash_table *htab;
10761 struct mips_elf_link_hash_entry *hmips;
10762
10763 htab = mips_elf_hash_table (info);
10764 BFD_ASSERT (htab != NULL);
10765 dynobj = elf_hash_table (info)->dynobj;
10766 hmips = (struct mips_elf_link_hash_entry *) h;
10767
10768 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
10769 {
10770 bfd_byte *loc;
10771 bfd_vma plt_address, got_address, got_offset, branch_offset;
10772 Elf_Internal_Rela rel;
10773 static const bfd_vma *plt_entry;
10774 bfd_vma gotplt_index;
10775 bfd_vma plt_offset;
10776
10777 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
10778 gotplt_index = h->plt.plist->gotplt_index;
10779
10780 BFD_ASSERT (h->dynindx != -1);
10781 BFD_ASSERT (htab->splt != NULL);
10782 BFD_ASSERT (gotplt_index != MINUS_ONE);
10783 BFD_ASSERT (plt_offset <= htab->splt->size);
10784
10785 /* Calculate the address of the .plt entry. */
10786 plt_address = (htab->splt->output_section->vma
10787 + htab->splt->output_offset
10788 + plt_offset);
10789
10790 /* Calculate the address of the .got.plt entry. */
10791 got_address = (htab->sgotplt->output_section->vma
10792 + htab->sgotplt->output_offset
10793 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
10794
10795 /* Calculate the offset of the .got.plt entry from
10796 _GLOBAL_OFFSET_TABLE_. */
10797 got_offset = mips_elf_gotplt_index (info, h);
10798
10799 /* Calculate the offset for the branch at the start of the PLT
10800 entry. The branch jumps to the beginning of .plt. */
10801 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
10802
10803 /* Fill in the initial value of the .got.plt entry. */
10804 bfd_put_32 (output_bfd, plt_address,
10805 (htab->sgotplt->contents
10806 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
10807
10808 /* Find out where the .plt entry should go. */
10809 loc = htab->splt->contents + plt_offset;
10810
10811 if (info->shared)
10812 {
10813 plt_entry = mips_vxworks_shared_plt_entry;
10814 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10815 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
10816 }
10817 else
10818 {
10819 bfd_vma got_address_high, got_address_low;
10820
10821 plt_entry = mips_vxworks_exec_plt_entry;
10822 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10823 got_address_low = got_address & 0xffff;
10824
10825 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
10826 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
10827 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
10828 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
10829 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
10830 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
10831 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
10832 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
10833
10834 loc = (htab->srelplt2->contents
10835 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
10836
10837 /* Emit a relocation for the .got.plt entry. */
10838 rel.r_offset = got_address;
10839 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
10840 rel.r_addend = plt_offset;
10841 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10842
10843 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
10844 loc += sizeof (Elf32_External_Rela);
10845 rel.r_offset = plt_address + 8;
10846 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
10847 rel.r_addend = got_offset;
10848 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10849
10850 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
10851 loc += sizeof (Elf32_External_Rela);
10852 rel.r_offset += 4;
10853 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
10854 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10855 }
10856
10857 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
10858 loc = (htab->srelplt->contents
10859 + gotplt_index * sizeof (Elf32_External_Rela));
10860 rel.r_offset = got_address;
10861 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
10862 rel.r_addend = 0;
10863 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
10864
10865 if (!h->def_regular)
10866 sym->st_shndx = SHN_UNDEF;
10867 }
10868
10869 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
10870
10871 sgot = htab->sgot;
10872 g = htab->got_info;
10873 BFD_ASSERT (g != NULL);
10874
10875 /* See if this symbol has an entry in the GOT. */
10876 if (hmips->global_got_area != GGA_NONE)
10877 {
10878 bfd_vma offset;
10879 Elf_Internal_Rela outrel;
10880 bfd_byte *loc;
10881 asection *s;
10882
10883 /* Install the symbol value in the GOT. */
10884 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
10885 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
10886
10887 /* Add a dynamic relocation for it. */
10888 s = mips_elf_rel_dyn_section (info, FALSE);
10889 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
10890 outrel.r_offset = (sgot->output_section->vma
10891 + sgot->output_offset
10892 + offset);
10893 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
10894 outrel.r_addend = 0;
10895 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
10896 }
10897
10898 /* Emit a copy reloc, if needed. */
10899 if (h->needs_copy)
10900 {
10901 Elf_Internal_Rela rel;
10902
10903 BFD_ASSERT (h->dynindx != -1);
10904
10905 rel.r_offset = (h->root.u.def.section->output_section->vma
10906 + h->root.u.def.section->output_offset
10907 + h->root.u.def.value);
10908 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
10909 rel.r_addend = 0;
10910 bfd_elf32_swap_reloca_out (output_bfd, &rel,
10911 htab->srelbss->contents
10912 + (htab->srelbss->reloc_count
10913 * sizeof (Elf32_External_Rela)));
10914 ++htab->srelbss->reloc_count;
10915 }
10916
10917 /* If this is a mips16/microMIPS symbol, force the value to be even. */
10918 if (ELF_ST_IS_COMPRESSED (sym->st_other))
10919 sym->st_value &= ~1;
10920
10921 return TRUE;
10922 }
10923
10924 /* Write out a plt0 entry to the beginning of .plt. */
10925
10926 static bfd_boolean
10927 mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
10928 {
10929 bfd_byte *loc;
10930 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
10931 static const bfd_vma *plt_entry;
10932 struct mips_elf_link_hash_table *htab;
10933
10934 htab = mips_elf_hash_table (info);
10935 BFD_ASSERT (htab != NULL);
10936
10937 if (ABI_64_P (output_bfd))
10938 plt_entry = mips_n64_exec_plt0_entry;
10939 else if (ABI_N32_P (output_bfd))
10940 plt_entry = mips_n32_exec_plt0_entry;
10941 else if (!htab->plt_header_is_comp)
10942 plt_entry = mips_o32_exec_plt0_entry;
10943 else if (htab->insn32)
10944 plt_entry = micromips_insn32_o32_exec_plt0_entry;
10945 else
10946 plt_entry = micromips_o32_exec_plt0_entry;
10947
10948 /* Calculate the value of .got.plt. */
10949 gotplt_value = (htab->sgotplt->output_section->vma
10950 + htab->sgotplt->output_offset);
10951 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
10952 gotplt_value_low = gotplt_value & 0xffff;
10953
10954 /* The PLT sequence is not safe for N64 if .got.plt's address can
10955 not be loaded in two instructions. */
10956 BFD_ASSERT ((gotplt_value & ~(bfd_vma) 0x7fffffff) == 0
10957 || ~(gotplt_value | 0x7fffffff) == 0);
10958
10959 /* Install the PLT header. */
10960 loc = htab->splt->contents;
10961 if (plt_entry == micromips_o32_exec_plt0_entry)
10962 {
10963 bfd_vma gotpc_offset;
10964 bfd_vma loc_address;
10965 size_t i;
10966
10967 BFD_ASSERT (gotplt_value % 4 == 0);
10968
10969 loc_address = (htab->splt->output_section->vma
10970 + htab->splt->output_offset);
10971 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
10972
10973 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10974 if (gotpc_offset + 0x1000000 >= 0x2000000)
10975 {
10976 (*_bfd_error_handler)
10977 (_("%B: `%A' offset of %ld from `%A' beyond the range of ADDIUPC"),
10978 output_bfd,
10979 htab->sgotplt->output_section,
10980 htab->splt->output_section,
10981 (long) gotpc_offset);
10982 bfd_set_error (bfd_error_no_error);
10983 return FALSE;
10984 }
10985 bfd_put_16 (output_bfd,
10986 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10987 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10988 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
10989 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
10990 }
10991 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
10992 {
10993 size_t i;
10994
10995 bfd_put_16 (output_bfd, plt_entry[0], loc);
10996 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
10997 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10998 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
10999 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11000 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11001 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11002 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11003 }
11004 else
11005 {
11006 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11007 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11008 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11009 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11010 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11011 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11012 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11013 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11014 }
11015
11016 return TRUE;
11017 }
11018
11019 /* Install the PLT header for a VxWorks executable and finalize the
11020 contents of .rela.plt.unloaded. */
11021
11022 static void
11023 mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11024 {
11025 Elf_Internal_Rela rela;
11026 bfd_byte *loc;
11027 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11028 static const bfd_vma *plt_entry;
11029 struct mips_elf_link_hash_table *htab;
11030
11031 htab = mips_elf_hash_table (info);
11032 BFD_ASSERT (htab != NULL);
11033
11034 plt_entry = mips_vxworks_exec_plt0_entry;
11035
11036 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11037 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11038 + htab->root.hgot->root.u.def.section->output_offset
11039 + htab->root.hgot->root.u.def.value);
11040
11041 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11042 got_value_low = got_value & 0xffff;
11043
11044 /* Calculate the address of the PLT header. */
11045 plt_address = htab->splt->output_section->vma + htab->splt->output_offset;
11046
11047 /* Install the PLT header. */
11048 loc = htab->splt->contents;
11049 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11050 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11051 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11052 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11053 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11054 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11055
11056 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11057 loc = htab->srelplt2->contents;
11058 rela.r_offset = plt_address;
11059 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11060 rela.r_addend = 0;
11061 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11062 loc += sizeof (Elf32_External_Rela);
11063
11064 /* Output the relocation for the following addiu of
11065 %lo(_GLOBAL_OFFSET_TABLE_). */
11066 rela.r_offset += 4;
11067 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11068 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11069 loc += sizeof (Elf32_External_Rela);
11070
11071 /* Fix up the remaining relocations. They may have the wrong
11072 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11073 in which symbols were output. */
11074 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11075 {
11076 Elf_Internal_Rela rel;
11077
11078 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11079 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11080 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11081 loc += sizeof (Elf32_External_Rela);
11082
11083 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11084 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11085 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11086 loc += sizeof (Elf32_External_Rela);
11087
11088 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11089 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11090 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11091 loc += sizeof (Elf32_External_Rela);
11092 }
11093 }
11094
11095 /* Install the PLT header for a VxWorks shared library. */
11096
11097 static void
11098 mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11099 {
11100 unsigned int i;
11101 struct mips_elf_link_hash_table *htab;
11102
11103 htab = mips_elf_hash_table (info);
11104 BFD_ASSERT (htab != NULL);
11105
11106 /* We just need to copy the entry byte-by-byte. */
11107 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11108 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
11109 htab->splt->contents + i * 4);
11110 }
11111
11112 /* Finish up the dynamic sections. */
11113
11114 bfd_boolean
11115 _bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11116 struct bfd_link_info *info)
11117 {
11118 bfd *dynobj;
11119 asection *sdyn;
11120 asection *sgot;
11121 struct mips_got_info *gg, *g;
11122 struct mips_elf_link_hash_table *htab;
11123
11124 htab = mips_elf_hash_table (info);
11125 BFD_ASSERT (htab != NULL);
11126
11127 dynobj = elf_hash_table (info)->dynobj;
11128
11129 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
11130
11131 sgot = htab->sgot;
11132 gg = htab->got_info;
11133
11134 if (elf_hash_table (info)->dynamic_sections_created)
11135 {
11136 bfd_byte *b;
11137 int dyn_to_skip = 0, dyn_skipped = 0;
11138
11139 BFD_ASSERT (sdyn != NULL);
11140 BFD_ASSERT (gg != NULL);
11141
11142 g = mips_elf_bfd_got (output_bfd, FALSE);
11143 BFD_ASSERT (g != NULL);
11144
11145 for (b = sdyn->contents;
11146 b < sdyn->contents + sdyn->size;
11147 b += MIPS_ELF_DYN_SIZE (dynobj))
11148 {
11149 Elf_Internal_Dyn dyn;
11150 const char *name;
11151 size_t elemsize;
11152 asection *s;
11153 bfd_boolean swap_out_p;
11154
11155 /* Read in the current dynamic entry. */
11156 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11157
11158 /* Assume that we're going to modify it and write it out. */
11159 swap_out_p = TRUE;
11160
11161 switch (dyn.d_tag)
11162 {
11163 case DT_RELENT:
11164 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11165 break;
11166
11167 case DT_RELAENT:
11168 BFD_ASSERT (htab->is_vxworks);
11169 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11170 break;
11171
11172 case DT_STRSZ:
11173 /* Rewrite DT_STRSZ. */
11174 dyn.d_un.d_val =
11175 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11176 break;
11177
11178 case DT_PLTGOT:
11179 s = htab->sgot;
11180 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11181 break;
11182
11183 case DT_MIPS_PLTGOT:
11184 s = htab->sgotplt;
11185 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11186 break;
11187
11188 case DT_MIPS_RLD_VERSION:
11189 dyn.d_un.d_val = 1; /* XXX */
11190 break;
11191
11192 case DT_MIPS_FLAGS:
11193 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11194 break;
11195
11196 case DT_MIPS_TIME_STAMP:
11197 {
11198 time_t t;
11199 time (&t);
11200 dyn.d_un.d_val = t;
11201 }
11202 break;
11203
11204 case DT_MIPS_ICHECKSUM:
11205 /* XXX FIXME: */
11206 swap_out_p = FALSE;
11207 break;
11208
11209 case DT_MIPS_IVERSION:
11210 /* XXX FIXME: */
11211 swap_out_p = FALSE;
11212 break;
11213
11214 case DT_MIPS_BASE_ADDRESS:
11215 s = output_bfd->sections;
11216 BFD_ASSERT (s != NULL);
11217 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11218 break;
11219
11220 case DT_MIPS_LOCAL_GOTNO:
11221 dyn.d_un.d_val = g->local_gotno;
11222 break;
11223
11224 case DT_MIPS_UNREFEXTNO:
11225 /* The index into the dynamic symbol table which is the
11226 entry of the first external symbol that is not
11227 referenced within the same object. */
11228 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11229 break;
11230
11231 case DT_MIPS_GOTSYM:
11232 if (htab->global_gotsym)
11233 {
11234 dyn.d_un.d_val = htab->global_gotsym->dynindx;
11235 break;
11236 }
11237 /* In case if we don't have global got symbols we default
11238 to setting DT_MIPS_GOTSYM to the same value as
11239 DT_MIPS_SYMTABNO, so we just fall through. */
11240
11241 case DT_MIPS_SYMTABNO:
11242 name = ".dynsym";
11243 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
11244 s = bfd_get_section_by_name (output_bfd, name);
11245 BFD_ASSERT (s != NULL);
11246
11247 dyn.d_un.d_val = s->size / elemsize;
11248 break;
11249
11250 case DT_MIPS_HIPAGENO:
11251 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
11252 break;
11253
11254 case DT_MIPS_RLD_MAP:
11255 {
11256 struct elf_link_hash_entry *h;
11257 h = mips_elf_hash_table (info)->rld_symbol;
11258 if (!h)
11259 {
11260 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11261 swap_out_p = FALSE;
11262 break;
11263 }
11264 s = h->root.u.def.section;
11265 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11266 + h->root.u.def.value);
11267 }
11268 break;
11269
11270 case DT_MIPS_OPTIONS:
11271 s = (bfd_get_section_by_name
11272 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11273 dyn.d_un.d_ptr = s->vma;
11274 break;
11275
11276 case DT_RELASZ:
11277 BFD_ASSERT (htab->is_vxworks);
11278 /* The count does not include the JUMP_SLOT relocations. */
11279 if (htab->srelplt)
11280 dyn.d_un.d_val -= htab->srelplt->size;
11281 break;
11282
11283 case DT_PLTREL:
11284 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11285 if (htab->is_vxworks)
11286 dyn.d_un.d_val = DT_RELA;
11287 else
11288 dyn.d_un.d_val = DT_REL;
11289 break;
11290
11291 case DT_PLTRELSZ:
11292 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11293 dyn.d_un.d_val = htab->srelplt->size;
11294 break;
11295
11296 case DT_JMPREL:
11297 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11298 dyn.d_un.d_ptr = (htab->srelplt->output_section->vma
11299 + htab->srelplt->output_offset);
11300 break;
11301
11302 case DT_TEXTREL:
11303 /* If we didn't need any text relocations after all, delete
11304 the dynamic tag. */
11305 if (!(info->flags & DF_TEXTREL))
11306 {
11307 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11308 swap_out_p = FALSE;
11309 }
11310 break;
11311
11312 case DT_FLAGS:
11313 /* If we didn't need any text relocations after all, clear
11314 DF_TEXTREL from DT_FLAGS. */
11315 if (!(info->flags & DF_TEXTREL))
11316 dyn.d_un.d_val &= ~DF_TEXTREL;
11317 else
11318 swap_out_p = FALSE;
11319 break;
11320
11321 default:
11322 swap_out_p = FALSE;
11323 if (htab->is_vxworks
11324 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11325 swap_out_p = TRUE;
11326 break;
11327 }
11328
11329 if (swap_out_p || dyn_skipped)
11330 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11331 (dynobj, &dyn, b - dyn_skipped);
11332
11333 if (dyn_to_skip)
11334 {
11335 dyn_skipped += dyn_to_skip;
11336 dyn_to_skip = 0;
11337 }
11338 }
11339
11340 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11341 if (dyn_skipped > 0)
11342 memset (b - dyn_skipped, 0, dyn_skipped);
11343 }
11344
11345 if (sgot != NULL && sgot->size > 0
11346 && !bfd_is_abs_section (sgot->output_section))
11347 {
11348 if (htab->is_vxworks)
11349 {
11350 /* The first entry of the global offset table points to the
11351 ".dynamic" section. The second is initialized by the
11352 loader and contains the shared library identifier.
11353 The third is also initialized by the loader and points
11354 to the lazy resolution stub. */
11355 MIPS_ELF_PUT_WORD (output_bfd,
11356 sdyn->output_offset + sdyn->output_section->vma,
11357 sgot->contents);
11358 MIPS_ELF_PUT_WORD (output_bfd, 0,
11359 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11360 MIPS_ELF_PUT_WORD (output_bfd, 0,
11361 sgot->contents
11362 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11363 }
11364 else
11365 {
11366 /* The first entry of the global offset table will be filled at
11367 runtime. The second entry will be used by some runtime loaders.
11368 This isn't the case of IRIX rld. */
11369 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
11370 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11371 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11372 }
11373
11374 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11375 = MIPS_ELF_GOT_SIZE (output_bfd);
11376 }
11377
11378 /* Generate dynamic relocations for the non-primary gots. */
11379 if (gg != NULL && gg->next)
11380 {
11381 Elf_Internal_Rela rel[3];
11382 bfd_vma addend = 0;
11383
11384 memset (rel, 0, sizeof (rel));
11385 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11386
11387 for (g = gg->next; g->next != gg; g = g->next)
11388 {
11389 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
11390 + g->next->tls_gotno;
11391
11392 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
11393 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11394 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11395 sgot->contents
11396 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
11397
11398 if (! info->shared)
11399 continue;
11400
11401 for (; got_index < g->local_gotno; got_index++)
11402 {
11403 if (got_index >= g->assigned_low_gotno
11404 && got_index <= g->assigned_high_gotno)
11405 continue;
11406
11407 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
11408 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
11409 if (!(mips_elf_create_dynamic_relocation
11410 (output_bfd, info, rel, NULL,
11411 bfd_abs_section_ptr,
11412 0, &addend, sgot)))
11413 return FALSE;
11414 BFD_ASSERT (addend == 0);
11415 }
11416 }
11417 }
11418
11419 /* The generation of dynamic relocations for the non-primary gots
11420 adds more dynamic relocations. We cannot count them until
11421 here. */
11422
11423 if (elf_hash_table (info)->dynamic_sections_created)
11424 {
11425 bfd_byte *b;
11426 bfd_boolean swap_out_p;
11427
11428 BFD_ASSERT (sdyn != NULL);
11429
11430 for (b = sdyn->contents;
11431 b < sdyn->contents + sdyn->size;
11432 b += MIPS_ELF_DYN_SIZE (dynobj))
11433 {
11434 Elf_Internal_Dyn dyn;
11435 asection *s;
11436
11437 /* Read in the current dynamic entry. */
11438 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11439
11440 /* Assume that we're going to modify it and write it out. */
11441 swap_out_p = TRUE;
11442
11443 switch (dyn.d_tag)
11444 {
11445 case DT_RELSZ:
11446 /* Reduce DT_RELSZ to account for any relocations we
11447 decided not to make. This is for the n64 irix rld,
11448 which doesn't seem to apply any relocations if there
11449 are trailing null entries. */
11450 s = mips_elf_rel_dyn_section (info, FALSE);
11451 dyn.d_un.d_val = (s->reloc_count
11452 * (ABI_64_P (output_bfd)
11453 ? sizeof (Elf64_Mips_External_Rel)
11454 : sizeof (Elf32_External_Rel)));
11455 /* Adjust the section size too. Tools like the prelinker
11456 can reasonably expect the values to the same. */
11457 elf_section_data (s->output_section)->this_hdr.sh_size
11458 = dyn.d_un.d_val;
11459 break;
11460
11461 default:
11462 swap_out_p = FALSE;
11463 break;
11464 }
11465
11466 if (swap_out_p)
11467 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11468 (dynobj, &dyn, b);
11469 }
11470 }
11471
11472 {
11473 asection *s;
11474 Elf32_compact_rel cpt;
11475
11476 if (SGI_COMPAT (output_bfd))
11477 {
11478 /* Write .compact_rel section out. */
11479 s = bfd_get_linker_section (dynobj, ".compact_rel");
11480 if (s != NULL)
11481 {
11482 cpt.id1 = 1;
11483 cpt.num = s->reloc_count;
11484 cpt.id2 = 2;
11485 cpt.offset = (s->output_section->filepos
11486 + sizeof (Elf32_External_compact_rel));
11487 cpt.reserved0 = 0;
11488 cpt.reserved1 = 0;
11489 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
11490 ((Elf32_External_compact_rel *)
11491 s->contents));
11492
11493 /* Clean up a dummy stub function entry in .text. */
11494 if (htab->sstubs != NULL)
11495 {
11496 file_ptr dummy_offset;
11497
11498 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
11499 dummy_offset = htab->sstubs->size - htab->function_stub_size;
11500 memset (htab->sstubs->contents + dummy_offset, 0,
11501 htab->function_stub_size);
11502 }
11503 }
11504 }
11505
11506 /* The psABI says that the dynamic relocations must be sorted in
11507 increasing order of r_symndx. The VxWorks EABI doesn't require
11508 this, and because the code below handles REL rather than RELA
11509 relocations, using it for VxWorks would be outright harmful. */
11510 if (!htab->is_vxworks)
11511 {
11512 s = mips_elf_rel_dyn_section (info, FALSE);
11513 if (s != NULL
11514 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
11515 {
11516 reldyn_sorting_bfd = output_bfd;
11517
11518 if (ABI_64_P (output_bfd))
11519 qsort ((Elf64_External_Rel *) s->contents + 1,
11520 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
11521 sort_dynamic_relocs_64);
11522 else
11523 qsort ((Elf32_External_Rel *) s->contents + 1,
11524 s->reloc_count - 1, sizeof (Elf32_External_Rel),
11525 sort_dynamic_relocs);
11526 }
11527 }
11528 }
11529
11530 if (htab->splt && htab->splt->size > 0)
11531 {
11532 if (htab->is_vxworks)
11533 {
11534 if (info->shared)
11535 mips_vxworks_finish_shared_plt (output_bfd, info);
11536 else
11537 mips_vxworks_finish_exec_plt (output_bfd, info);
11538 }
11539 else
11540 {
11541 BFD_ASSERT (!info->shared);
11542 if (!mips_finish_exec_plt (output_bfd, info))
11543 return FALSE;
11544 }
11545 }
11546 return TRUE;
11547 }
11548
11549
11550 /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
11551
11552 static void
11553 mips_set_isa_flags (bfd *abfd)
11554 {
11555 flagword val;
11556
11557 switch (bfd_get_mach (abfd))
11558 {
11559 default:
11560 case bfd_mach_mips3000:
11561 val = E_MIPS_ARCH_1;
11562 break;
11563
11564 case bfd_mach_mips3900:
11565 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
11566 break;
11567
11568 case bfd_mach_mips6000:
11569 val = E_MIPS_ARCH_2;
11570 break;
11571
11572 case bfd_mach_mips4000:
11573 case bfd_mach_mips4300:
11574 case bfd_mach_mips4400:
11575 case bfd_mach_mips4600:
11576 val = E_MIPS_ARCH_3;
11577 break;
11578
11579 case bfd_mach_mips4010:
11580 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4010;
11581 break;
11582
11583 case bfd_mach_mips4100:
11584 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
11585 break;
11586
11587 case bfd_mach_mips4111:
11588 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
11589 break;
11590
11591 case bfd_mach_mips4120:
11592 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
11593 break;
11594
11595 case bfd_mach_mips4650:
11596 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
11597 break;
11598
11599 case bfd_mach_mips5400:
11600 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
11601 break;
11602
11603 case bfd_mach_mips5500:
11604 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
11605 break;
11606
11607 case bfd_mach_mips5900:
11608 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
11609 break;
11610
11611 case bfd_mach_mips9000:
11612 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
11613 break;
11614
11615 case bfd_mach_mips5000:
11616 case bfd_mach_mips7000:
11617 case bfd_mach_mips8000:
11618 case bfd_mach_mips10000:
11619 case bfd_mach_mips12000:
11620 case bfd_mach_mips14000:
11621 case bfd_mach_mips16000:
11622 val = E_MIPS_ARCH_4;
11623 break;
11624
11625 case bfd_mach_mips5:
11626 val = E_MIPS_ARCH_5;
11627 break;
11628
11629 case bfd_mach_mips_loongson_2e:
11630 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
11631 break;
11632
11633 case bfd_mach_mips_loongson_2f:
11634 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
11635 break;
11636
11637 case bfd_mach_mips_sb1:
11638 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
11639 break;
11640
11641 case bfd_mach_mips_loongson_3a:
11642 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_LS3A;
11643 break;
11644
11645 case bfd_mach_mips_octeon:
11646 case bfd_mach_mips_octeonp:
11647 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
11648 break;
11649
11650 case bfd_mach_mips_xlr:
11651 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
11652 break;
11653
11654 case bfd_mach_mips_octeon2:
11655 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
11656 break;
11657
11658 case bfd_mach_mipsisa32:
11659 val = E_MIPS_ARCH_32;
11660 break;
11661
11662 case bfd_mach_mipsisa64:
11663 val = E_MIPS_ARCH_64;
11664 break;
11665
11666 case bfd_mach_mipsisa32r2:
11667 val = E_MIPS_ARCH_32R2;
11668 break;
11669
11670 case bfd_mach_mipsisa64r2:
11671 val = E_MIPS_ARCH_64R2;
11672 break;
11673 }
11674 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
11675 elf_elfheader (abfd)->e_flags |= val;
11676
11677 }
11678
11679
11680 /* The final processing done just before writing out a MIPS ELF object
11681 file. This gets the MIPS architecture right based on the machine
11682 number. This is used by both the 32-bit and the 64-bit ABI. */
11683
11684 void
11685 _bfd_mips_elf_final_write_processing (bfd *abfd,
11686 bfd_boolean linker ATTRIBUTE_UNUSED)
11687 {
11688 unsigned int i;
11689 Elf_Internal_Shdr **hdrpp;
11690 const char *name;
11691 asection *sec;
11692
11693 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
11694 is nonzero. This is for compatibility with old objects, which used
11695 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
11696 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
11697 mips_set_isa_flags (abfd);
11698
11699 /* Set the sh_info field for .gptab sections and other appropriate
11700 info for each special section. */
11701 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
11702 i < elf_numsections (abfd);
11703 i++, hdrpp++)
11704 {
11705 switch ((*hdrpp)->sh_type)
11706 {
11707 case SHT_MIPS_MSYM:
11708 case SHT_MIPS_LIBLIST:
11709 sec = bfd_get_section_by_name (abfd, ".dynstr");
11710 if (sec != NULL)
11711 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11712 break;
11713
11714 case SHT_MIPS_GPTAB:
11715 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11716 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11717 BFD_ASSERT (name != NULL
11718 && CONST_STRNEQ (name, ".gptab."));
11719 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
11720 BFD_ASSERT (sec != NULL);
11721 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11722 break;
11723
11724 case SHT_MIPS_CONTENT:
11725 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11726 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11727 BFD_ASSERT (name != NULL
11728 && CONST_STRNEQ (name, ".MIPS.content"));
11729 sec = bfd_get_section_by_name (abfd,
11730 name + sizeof ".MIPS.content" - 1);
11731 BFD_ASSERT (sec != NULL);
11732 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11733 break;
11734
11735 case SHT_MIPS_SYMBOL_LIB:
11736 sec = bfd_get_section_by_name (abfd, ".dynsym");
11737 if (sec != NULL)
11738 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11739 sec = bfd_get_section_by_name (abfd, ".liblist");
11740 if (sec != NULL)
11741 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
11742 break;
11743
11744 case SHT_MIPS_EVENTS:
11745 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
11746 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
11747 BFD_ASSERT (name != NULL);
11748 if (CONST_STRNEQ (name, ".MIPS.events"))
11749 sec = bfd_get_section_by_name (abfd,
11750 name + sizeof ".MIPS.events" - 1);
11751 else
11752 {
11753 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
11754 sec = bfd_get_section_by_name (abfd,
11755 (name
11756 + sizeof ".MIPS.post_rel" - 1));
11757 }
11758 BFD_ASSERT (sec != NULL);
11759 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
11760 break;
11761
11762 }
11763 }
11764 }
11765 \f
11766 /* When creating an IRIX5 executable, we need REGINFO and RTPROC
11767 segments. */
11768
11769 int
11770 _bfd_mips_elf_additional_program_headers (bfd *abfd,
11771 struct bfd_link_info *info ATTRIBUTE_UNUSED)
11772 {
11773 asection *s;
11774 int ret = 0;
11775
11776 /* See if we need a PT_MIPS_REGINFO segment. */
11777 s = bfd_get_section_by_name (abfd, ".reginfo");
11778 if (s && (s->flags & SEC_LOAD))
11779 ++ret;
11780
11781 /* See if we need a PT_MIPS_OPTIONS segment. */
11782 if (IRIX_COMPAT (abfd) == ict_irix6
11783 && bfd_get_section_by_name (abfd,
11784 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
11785 ++ret;
11786
11787 /* See if we need a PT_MIPS_RTPROC segment. */
11788 if (IRIX_COMPAT (abfd) == ict_irix5
11789 && bfd_get_section_by_name (abfd, ".dynamic")
11790 && bfd_get_section_by_name (abfd, ".mdebug"))
11791 ++ret;
11792
11793 /* Allocate a PT_NULL header in dynamic objects. See
11794 _bfd_mips_elf_modify_segment_map for details. */
11795 if (!SGI_COMPAT (abfd)
11796 && bfd_get_section_by_name (abfd, ".dynamic"))
11797 ++ret;
11798
11799 return ret;
11800 }
11801
11802 /* Modify the segment map for an IRIX5 executable. */
11803
11804 bfd_boolean
11805 _bfd_mips_elf_modify_segment_map (bfd *abfd,
11806 struct bfd_link_info *info)
11807 {
11808 asection *s;
11809 struct elf_segment_map *m, **pm;
11810 bfd_size_type amt;
11811
11812 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
11813 segment. */
11814 s = bfd_get_section_by_name (abfd, ".reginfo");
11815 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11816 {
11817 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
11818 if (m->p_type == PT_MIPS_REGINFO)
11819 break;
11820 if (m == NULL)
11821 {
11822 amt = sizeof *m;
11823 m = bfd_zalloc (abfd, amt);
11824 if (m == NULL)
11825 return FALSE;
11826
11827 m->p_type = PT_MIPS_REGINFO;
11828 m->count = 1;
11829 m->sections[0] = s;
11830
11831 /* We want to put it after the PHDR and INTERP segments. */
11832 pm = &elf_seg_map (abfd);
11833 while (*pm != NULL
11834 && ((*pm)->p_type == PT_PHDR
11835 || (*pm)->p_type == PT_INTERP))
11836 pm = &(*pm)->next;
11837
11838 m->next = *pm;
11839 *pm = m;
11840 }
11841 }
11842
11843 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
11844 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
11845 PT_MIPS_OPTIONS segment immediately following the program header
11846 table. */
11847 if (NEWABI_P (abfd)
11848 /* On non-IRIX6 new abi, we'll have already created a segment
11849 for this section, so don't create another. I'm not sure this
11850 is not also the case for IRIX 6, but I can't test it right
11851 now. */
11852 && IRIX_COMPAT (abfd) == ict_irix6)
11853 {
11854 for (s = abfd->sections; s; s = s->next)
11855 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
11856 break;
11857
11858 if (s)
11859 {
11860 struct elf_segment_map *options_segment;
11861
11862 pm = &elf_seg_map (abfd);
11863 while (*pm != NULL
11864 && ((*pm)->p_type == PT_PHDR
11865 || (*pm)->p_type == PT_INTERP))
11866 pm = &(*pm)->next;
11867
11868 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
11869 {
11870 amt = sizeof (struct elf_segment_map);
11871 options_segment = bfd_zalloc (abfd, amt);
11872 options_segment->next = *pm;
11873 options_segment->p_type = PT_MIPS_OPTIONS;
11874 options_segment->p_flags = PF_R;
11875 options_segment->p_flags_valid = TRUE;
11876 options_segment->count = 1;
11877 options_segment->sections[0] = s;
11878 *pm = options_segment;
11879 }
11880 }
11881 }
11882 else
11883 {
11884 if (IRIX_COMPAT (abfd) == ict_irix5)
11885 {
11886 /* If there are .dynamic and .mdebug sections, we make a room
11887 for the RTPROC header. FIXME: Rewrite without section names. */
11888 if (bfd_get_section_by_name (abfd, ".interp") == NULL
11889 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
11890 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
11891 {
11892 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
11893 if (m->p_type == PT_MIPS_RTPROC)
11894 break;
11895 if (m == NULL)
11896 {
11897 amt = sizeof *m;
11898 m = bfd_zalloc (abfd, amt);
11899 if (m == NULL)
11900 return FALSE;
11901
11902 m->p_type = PT_MIPS_RTPROC;
11903
11904 s = bfd_get_section_by_name (abfd, ".rtproc");
11905 if (s == NULL)
11906 {
11907 m->count = 0;
11908 m->p_flags = 0;
11909 m->p_flags_valid = 1;
11910 }
11911 else
11912 {
11913 m->count = 1;
11914 m->sections[0] = s;
11915 }
11916
11917 /* We want to put it after the DYNAMIC segment. */
11918 pm = &elf_seg_map (abfd);
11919 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
11920 pm = &(*pm)->next;
11921 if (*pm != NULL)
11922 pm = &(*pm)->next;
11923
11924 m->next = *pm;
11925 *pm = m;
11926 }
11927 }
11928 }
11929 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
11930 .dynstr, .dynsym, and .hash sections, and everything in
11931 between. */
11932 for (pm = &elf_seg_map (abfd); *pm != NULL;
11933 pm = &(*pm)->next)
11934 if ((*pm)->p_type == PT_DYNAMIC)
11935 break;
11936 m = *pm;
11937 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
11938 glibc's dynamic linker has traditionally derived the number of
11939 tags from the p_filesz field, and sometimes allocates stack
11940 arrays of that size. An overly-big PT_DYNAMIC segment can
11941 be actively harmful in such cases. Making PT_DYNAMIC contain
11942 other sections can also make life hard for the prelinker,
11943 which might move one of the other sections to a different
11944 PT_LOAD segment. */
11945 if (SGI_COMPAT (abfd)
11946 && m != NULL
11947 && m->count == 1
11948 && strcmp (m->sections[0]->name, ".dynamic") == 0)
11949 {
11950 static const char *sec_names[] =
11951 {
11952 ".dynamic", ".dynstr", ".dynsym", ".hash"
11953 };
11954 bfd_vma low, high;
11955 unsigned int i, c;
11956 struct elf_segment_map *n;
11957
11958 low = ~(bfd_vma) 0;
11959 high = 0;
11960 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
11961 {
11962 s = bfd_get_section_by_name (abfd, sec_names[i]);
11963 if (s != NULL && (s->flags & SEC_LOAD) != 0)
11964 {
11965 bfd_size_type sz;
11966
11967 if (low > s->vma)
11968 low = s->vma;
11969 sz = s->size;
11970 if (high < s->vma + sz)
11971 high = s->vma + sz;
11972 }
11973 }
11974
11975 c = 0;
11976 for (s = abfd->sections; s != NULL; s = s->next)
11977 if ((s->flags & SEC_LOAD) != 0
11978 && s->vma >= low
11979 && s->vma + s->size <= high)
11980 ++c;
11981
11982 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
11983 n = bfd_zalloc (abfd, amt);
11984 if (n == NULL)
11985 return FALSE;
11986 *n = *m;
11987 n->count = c;
11988
11989 i = 0;
11990 for (s = abfd->sections; s != NULL; s = s->next)
11991 {
11992 if ((s->flags & SEC_LOAD) != 0
11993 && s->vma >= low
11994 && s->vma + s->size <= high)
11995 {
11996 n->sections[i] = s;
11997 ++i;
11998 }
11999 }
12000
12001 *pm = n;
12002 }
12003 }
12004
12005 /* Allocate a spare program header in dynamic objects so that tools
12006 like the prelinker can add an extra PT_LOAD entry.
12007
12008 If the prelinker needs to make room for a new PT_LOAD entry, its
12009 standard procedure is to move the first (read-only) sections into
12010 the new (writable) segment. However, the MIPS ABI requires
12011 .dynamic to be in a read-only segment, and the section will often
12012 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12013
12014 Although the prelinker could in principle move .dynamic to a
12015 writable segment, it seems better to allocate a spare program
12016 header instead, and avoid the need to move any sections.
12017 There is a long tradition of allocating spare dynamic tags,
12018 so allocating a spare program header seems like a natural
12019 extension.
12020
12021 If INFO is NULL, we may be copying an already prelinked binary
12022 with objcopy or strip, so do not add this header. */
12023 if (info != NULL
12024 && !SGI_COMPAT (abfd)
12025 && bfd_get_section_by_name (abfd, ".dynamic"))
12026 {
12027 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
12028 if ((*pm)->p_type == PT_NULL)
12029 break;
12030 if (*pm == NULL)
12031 {
12032 m = bfd_zalloc (abfd, sizeof (*m));
12033 if (m == NULL)
12034 return FALSE;
12035
12036 m->p_type = PT_NULL;
12037 *pm = m;
12038 }
12039 }
12040
12041 return TRUE;
12042 }
12043 \f
12044 /* Return the section that should be marked against GC for a given
12045 relocation. */
12046
12047 asection *
12048 _bfd_mips_elf_gc_mark_hook (asection *sec,
12049 struct bfd_link_info *info,
12050 Elf_Internal_Rela *rel,
12051 struct elf_link_hash_entry *h,
12052 Elf_Internal_Sym *sym)
12053 {
12054 /* ??? Do mips16 stub sections need to be handled special? */
12055
12056 if (h != NULL)
12057 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12058 {
12059 case R_MIPS_GNU_VTINHERIT:
12060 case R_MIPS_GNU_VTENTRY:
12061 return NULL;
12062 }
12063
12064 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
12065 }
12066
12067 /* Update the got entry reference counts for the section being removed. */
12068
12069 bfd_boolean
12070 _bfd_mips_elf_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
12071 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12072 asection *sec ATTRIBUTE_UNUSED,
12073 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
12074 {
12075 #if 0
12076 Elf_Internal_Shdr *symtab_hdr;
12077 struct elf_link_hash_entry **sym_hashes;
12078 bfd_signed_vma *local_got_refcounts;
12079 const Elf_Internal_Rela *rel, *relend;
12080 unsigned long r_symndx;
12081 struct elf_link_hash_entry *h;
12082
12083 if (info->relocatable)
12084 return TRUE;
12085
12086 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12087 sym_hashes = elf_sym_hashes (abfd);
12088 local_got_refcounts = elf_local_got_refcounts (abfd);
12089
12090 relend = relocs + sec->reloc_count;
12091 for (rel = relocs; rel < relend; rel++)
12092 switch (ELF_R_TYPE (abfd, rel->r_info))
12093 {
12094 case R_MIPS16_GOT16:
12095 case R_MIPS16_CALL16:
12096 case R_MIPS_GOT16:
12097 case R_MIPS_CALL16:
12098 case R_MIPS_CALL_HI16:
12099 case R_MIPS_CALL_LO16:
12100 case R_MIPS_GOT_HI16:
12101 case R_MIPS_GOT_LO16:
12102 case R_MIPS_GOT_DISP:
12103 case R_MIPS_GOT_PAGE:
12104 case R_MIPS_GOT_OFST:
12105 case R_MICROMIPS_GOT16:
12106 case R_MICROMIPS_CALL16:
12107 case R_MICROMIPS_CALL_HI16:
12108 case R_MICROMIPS_CALL_LO16:
12109 case R_MICROMIPS_GOT_HI16:
12110 case R_MICROMIPS_GOT_LO16:
12111 case R_MICROMIPS_GOT_DISP:
12112 case R_MICROMIPS_GOT_PAGE:
12113 case R_MICROMIPS_GOT_OFST:
12114 /* ??? It would seem that the existing MIPS code does no sort
12115 of reference counting or whatnot on its GOT and PLT entries,
12116 so it is not possible to garbage collect them at this time. */
12117 break;
12118
12119 default:
12120 break;
12121 }
12122 #endif
12123
12124 return TRUE;
12125 }
12126 \f
12127 /* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12128 hiding the old indirect symbol. Process additional relocation
12129 information. Also called for weakdefs, in which case we just let
12130 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12131
12132 void
12133 _bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
12134 struct elf_link_hash_entry *dir,
12135 struct elf_link_hash_entry *ind)
12136 {
12137 struct mips_elf_link_hash_entry *dirmips, *indmips;
12138
12139 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
12140
12141 dirmips = (struct mips_elf_link_hash_entry *) dir;
12142 indmips = (struct mips_elf_link_hash_entry *) ind;
12143 /* Any absolute non-dynamic relocations against an indirect or weak
12144 definition will be against the target symbol. */
12145 if (indmips->has_static_relocs)
12146 dirmips->has_static_relocs = TRUE;
12147
12148 if (ind->root.type != bfd_link_hash_indirect)
12149 return;
12150
12151 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12152 if (indmips->readonly_reloc)
12153 dirmips->readonly_reloc = TRUE;
12154 if (indmips->no_fn_stub)
12155 dirmips->no_fn_stub = TRUE;
12156 if (indmips->fn_stub)
12157 {
12158 dirmips->fn_stub = indmips->fn_stub;
12159 indmips->fn_stub = NULL;
12160 }
12161 if (indmips->need_fn_stub)
12162 {
12163 dirmips->need_fn_stub = TRUE;
12164 indmips->need_fn_stub = FALSE;
12165 }
12166 if (indmips->call_stub)
12167 {
12168 dirmips->call_stub = indmips->call_stub;
12169 indmips->call_stub = NULL;
12170 }
12171 if (indmips->call_fp_stub)
12172 {
12173 dirmips->call_fp_stub = indmips->call_fp_stub;
12174 indmips->call_fp_stub = NULL;
12175 }
12176 if (indmips->global_got_area < dirmips->global_got_area)
12177 dirmips->global_got_area = indmips->global_got_area;
12178 if (indmips->global_got_area < GGA_NONE)
12179 indmips->global_got_area = GGA_NONE;
12180 if (indmips->has_nonpic_branches)
12181 dirmips->has_nonpic_branches = TRUE;
12182 }
12183 \f
12184 #define PDR_SIZE 32
12185
12186 bfd_boolean
12187 _bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12188 struct bfd_link_info *info)
12189 {
12190 asection *o;
12191 bfd_boolean ret = FALSE;
12192 unsigned char *tdata;
12193 size_t i, skip;
12194
12195 o = bfd_get_section_by_name (abfd, ".pdr");
12196 if (! o)
12197 return FALSE;
12198 if (o->size == 0)
12199 return FALSE;
12200 if (o->size % PDR_SIZE != 0)
12201 return FALSE;
12202 if (o->output_section != NULL
12203 && bfd_is_abs_section (o->output_section))
12204 return FALSE;
12205
12206 tdata = bfd_zmalloc (o->size / PDR_SIZE);
12207 if (! tdata)
12208 return FALSE;
12209
12210 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
12211 info->keep_memory);
12212 if (!cookie->rels)
12213 {
12214 free (tdata);
12215 return FALSE;
12216 }
12217
12218 cookie->rel = cookie->rels;
12219 cookie->relend = cookie->rels + o->reloc_count;
12220
12221 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
12222 {
12223 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
12224 {
12225 tdata[i] = 1;
12226 skip ++;
12227 }
12228 }
12229
12230 if (skip != 0)
12231 {
12232 mips_elf_section_data (o)->u.tdata = tdata;
12233 o->size -= skip * PDR_SIZE;
12234 ret = TRUE;
12235 }
12236 else
12237 free (tdata);
12238
12239 if (! info->keep_memory)
12240 free (cookie->rels);
12241
12242 return ret;
12243 }
12244
12245 bfd_boolean
12246 _bfd_mips_elf_ignore_discarded_relocs (asection *sec)
12247 {
12248 if (strcmp (sec->name, ".pdr") == 0)
12249 return TRUE;
12250 return FALSE;
12251 }
12252
12253 bfd_boolean
12254 _bfd_mips_elf_write_section (bfd *output_bfd,
12255 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
12256 asection *sec, bfd_byte *contents)
12257 {
12258 bfd_byte *to, *from, *end;
12259 int i;
12260
12261 if (strcmp (sec->name, ".pdr") != 0)
12262 return FALSE;
12263
12264 if (mips_elf_section_data (sec)->u.tdata == NULL)
12265 return FALSE;
12266
12267 to = contents;
12268 end = contents + sec->size;
12269 for (from = contents, i = 0;
12270 from < end;
12271 from += PDR_SIZE, i++)
12272 {
12273 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
12274 continue;
12275 if (to != from)
12276 memcpy (to, from, PDR_SIZE);
12277 to += PDR_SIZE;
12278 }
12279 bfd_set_section_contents (output_bfd, sec->output_section, contents,
12280 sec->output_offset, sec->size);
12281 return TRUE;
12282 }
12283 \f
12284 /* microMIPS code retains local labels for linker relaxation. Omit them
12285 from output by default for clarity. */
12286
12287 bfd_boolean
12288 _bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12289 {
12290 return _bfd_elf_is_local_label_name (abfd, sym->name);
12291 }
12292
12293 /* MIPS ELF uses a special find_nearest_line routine in order the
12294 handle the ECOFF debugging information. */
12295
12296 struct mips_elf_find_line
12297 {
12298 struct ecoff_debug_info d;
12299 struct ecoff_find_line i;
12300 };
12301
12302 bfd_boolean
12303 _bfd_mips_elf_find_nearest_line (bfd *abfd, asection *section,
12304 asymbol **symbols, bfd_vma offset,
12305 const char **filename_ptr,
12306 const char **functionname_ptr,
12307 unsigned int *line_ptr)
12308 {
12309 asection *msec;
12310
12311 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
12312 filename_ptr, functionname_ptr,
12313 line_ptr))
12314 return TRUE;
12315
12316 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
12317 section, symbols, offset,
12318 filename_ptr, functionname_ptr,
12319 line_ptr, NULL, ABI_64_P (abfd) ? 8 : 0,
12320 &elf_tdata (abfd)->dwarf2_find_line_info))
12321 return TRUE;
12322
12323 msec = bfd_get_section_by_name (abfd, ".mdebug");
12324 if (msec != NULL)
12325 {
12326 flagword origflags;
12327 struct mips_elf_find_line *fi;
12328 const struct ecoff_debug_swap * const swap =
12329 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12330
12331 /* If we are called during a link, mips_elf_final_link may have
12332 cleared the SEC_HAS_CONTENTS field. We force it back on here
12333 if appropriate (which it normally will be). */
12334 origflags = msec->flags;
12335 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12336 msec->flags |= SEC_HAS_CONTENTS;
12337
12338 fi = mips_elf_tdata (abfd)->find_line_info;
12339 if (fi == NULL)
12340 {
12341 bfd_size_type external_fdr_size;
12342 char *fraw_src;
12343 char *fraw_end;
12344 struct fdr *fdr_ptr;
12345 bfd_size_type amt = sizeof (struct mips_elf_find_line);
12346
12347 fi = bfd_zalloc (abfd, amt);
12348 if (fi == NULL)
12349 {
12350 msec->flags = origflags;
12351 return FALSE;
12352 }
12353
12354 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
12355 {
12356 msec->flags = origflags;
12357 return FALSE;
12358 }
12359
12360 /* Swap in the FDR information. */
12361 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
12362 fi->d.fdr = bfd_alloc (abfd, amt);
12363 if (fi->d.fdr == NULL)
12364 {
12365 msec->flags = origflags;
12366 return FALSE;
12367 }
12368 external_fdr_size = swap->external_fdr_size;
12369 fdr_ptr = fi->d.fdr;
12370 fraw_src = (char *) fi->d.external_fdr;
12371 fraw_end = (fraw_src
12372 + fi->d.symbolic_header.ifdMax * external_fdr_size);
12373 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
12374 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
12375
12376 mips_elf_tdata (abfd)->find_line_info = fi;
12377
12378 /* Note that we don't bother to ever free this information.
12379 find_nearest_line is either called all the time, as in
12380 objdump -l, so the information should be saved, or it is
12381 rarely called, as in ld error messages, so the memory
12382 wasted is unimportant. Still, it would probably be a
12383 good idea for free_cached_info to throw it away. */
12384 }
12385
12386 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
12387 &fi->i, filename_ptr, functionname_ptr,
12388 line_ptr))
12389 {
12390 msec->flags = origflags;
12391 return TRUE;
12392 }
12393
12394 msec->flags = origflags;
12395 }
12396
12397 /* Fall back on the generic ELF find_nearest_line routine. */
12398
12399 return _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
12400 filename_ptr, functionname_ptr,
12401 line_ptr);
12402 }
12403
12404 bfd_boolean
12405 _bfd_mips_elf_find_inliner_info (bfd *abfd,
12406 const char **filename_ptr,
12407 const char **functionname_ptr,
12408 unsigned int *line_ptr)
12409 {
12410 bfd_boolean found;
12411 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
12412 functionname_ptr, line_ptr,
12413 & elf_tdata (abfd)->dwarf2_find_line_info);
12414 return found;
12415 }
12416
12417 \f
12418 /* When are writing out the .options or .MIPS.options section,
12419 remember the bytes we are writing out, so that we can install the
12420 GP value in the section_processing routine. */
12421
12422 bfd_boolean
12423 _bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
12424 const void *location,
12425 file_ptr offset, bfd_size_type count)
12426 {
12427 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
12428 {
12429 bfd_byte *c;
12430
12431 if (elf_section_data (section) == NULL)
12432 {
12433 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
12434 section->used_by_bfd = bfd_zalloc (abfd, amt);
12435 if (elf_section_data (section) == NULL)
12436 return FALSE;
12437 }
12438 c = mips_elf_section_data (section)->u.tdata;
12439 if (c == NULL)
12440 {
12441 c = bfd_zalloc (abfd, section->size);
12442 if (c == NULL)
12443 return FALSE;
12444 mips_elf_section_data (section)->u.tdata = c;
12445 }
12446
12447 memcpy (c + offset, location, count);
12448 }
12449
12450 return _bfd_elf_set_section_contents (abfd, section, location, offset,
12451 count);
12452 }
12453
12454 /* This is almost identical to bfd_generic_get_... except that some
12455 MIPS relocations need to be handled specially. Sigh. */
12456
12457 bfd_byte *
12458 _bfd_elf_mips_get_relocated_section_contents
12459 (bfd *abfd,
12460 struct bfd_link_info *link_info,
12461 struct bfd_link_order *link_order,
12462 bfd_byte *data,
12463 bfd_boolean relocatable,
12464 asymbol **symbols)
12465 {
12466 /* Get enough memory to hold the stuff */
12467 bfd *input_bfd = link_order->u.indirect.section->owner;
12468 asection *input_section = link_order->u.indirect.section;
12469 bfd_size_type sz;
12470
12471 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12472 arelent **reloc_vector = NULL;
12473 long reloc_count;
12474
12475 if (reloc_size < 0)
12476 goto error_return;
12477
12478 reloc_vector = bfd_malloc (reloc_size);
12479 if (reloc_vector == NULL && reloc_size != 0)
12480 goto error_return;
12481
12482 /* read in the section */
12483 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
12484 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
12485 goto error_return;
12486
12487 reloc_count = bfd_canonicalize_reloc (input_bfd,
12488 input_section,
12489 reloc_vector,
12490 symbols);
12491 if (reloc_count < 0)
12492 goto error_return;
12493
12494 if (reloc_count > 0)
12495 {
12496 arelent **parent;
12497 /* for mips */
12498 int gp_found;
12499 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
12500
12501 {
12502 struct bfd_hash_entry *h;
12503 struct bfd_link_hash_entry *lh;
12504 /* Skip all this stuff if we aren't mixing formats. */
12505 if (abfd && input_bfd
12506 && abfd->xvec == input_bfd->xvec)
12507 lh = 0;
12508 else
12509 {
12510 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
12511 lh = (struct bfd_link_hash_entry *) h;
12512 }
12513 lookup:
12514 if (lh)
12515 {
12516 switch (lh->type)
12517 {
12518 case bfd_link_hash_undefined:
12519 case bfd_link_hash_undefweak:
12520 case bfd_link_hash_common:
12521 gp_found = 0;
12522 break;
12523 case bfd_link_hash_defined:
12524 case bfd_link_hash_defweak:
12525 gp_found = 1;
12526 gp = lh->u.def.value;
12527 break;
12528 case bfd_link_hash_indirect:
12529 case bfd_link_hash_warning:
12530 lh = lh->u.i.link;
12531 /* @@FIXME ignoring warning for now */
12532 goto lookup;
12533 case bfd_link_hash_new:
12534 default:
12535 abort ();
12536 }
12537 }
12538 else
12539 gp_found = 0;
12540 }
12541 /* end mips */
12542 for (parent = reloc_vector; *parent != NULL; parent++)
12543 {
12544 char *error_message = NULL;
12545 bfd_reloc_status_type r;
12546
12547 /* Specific to MIPS: Deal with relocation types that require
12548 knowing the gp of the output bfd. */
12549 asymbol *sym = *(*parent)->sym_ptr_ptr;
12550
12551 /* If we've managed to find the gp and have a special
12552 function for the relocation then go ahead, else default
12553 to the generic handling. */
12554 if (gp_found
12555 && (*parent)->howto->special_function
12556 == _bfd_mips_elf32_gprel16_reloc)
12557 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
12558 input_section, relocatable,
12559 data, gp);
12560 else
12561 r = bfd_perform_relocation (input_bfd, *parent, data,
12562 input_section,
12563 relocatable ? abfd : NULL,
12564 &error_message);
12565
12566 if (relocatable)
12567 {
12568 asection *os = input_section->output_section;
12569
12570 /* A partial link, so keep the relocs */
12571 os->orelocation[os->reloc_count] = *parent;
12572 os->reloc_count++;
12573 }
12574
12575 if (r != bfd_reloc_ok)
12576 {
12577 switch (r)
12578 {
12579 case bfd_reloc_undefined:
12580 if (!((*link_info->callbacks->undefined_symbol)
12581 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12582 input_bfd, input_section, (*parent)->address, TRUE)))
12583 goto error_return;
12584 break;
12585 case bfd_reloc_dangerous:
12586 BFD_ASSERT (error_message != NULL);
12587 if (!((*link_info->callbacks->reloc_dangerous)
12588 (link_info, error_message, input_bfd, input_section,
12589 (*parent)->address)))
12590 goto error_return;
12591 break;
12592 case bfd_reloc_overflow:
12593 if (!((*link_info->callbacks->reloc_overflow)
12594 (link_info, NULL,
12595 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12596 (*parent)->howto->name, (*parent)->addend,
12597 input_bfd, input_section, (*parent)->address)))
12598 goto error_return;
12599 break;
12600 case bfd_reloc_outofrange:
12601 default:
12602 abort ();
12603 break;
12604 }
12605
12606 }
12607 }
12608 }
12609 if (reloc_vector != NULL)
12610 free (reloc_vector);
12611 return data;
12612
12613 error_return:
12614 if (reloc_vector != NULL)
12615 free (reloc_vector);
12616 return NULL;
12617 }
12618 \f
12619 static bfd_boolean
12620 mips_elf_relax_delete_bytes (bfd *abfd,
12621 asection *sec, bfd_vma addr, int count)
12622 {
12623 Elf_Internal_Shdr *symtab_hdr;
12624 unsigned int sec_shndx;
12625 bfd_byte *contents;
12626 Elf_Internal_Rela *irel, *irelend;
12627 Elf_Internal_Sym *isym;
12628 Elf_Internal_Sym *isymend;
12629 struct elf_link_hash_entry **sym_hashes;
12630 struct elf_link_hash_entry **end_hashes;
12631 struct elf_link_hash_entry **start_hashes;
12632 unsigned int symcount;
12633
12634 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
12635 contents = elf_section_data (sec)->this_hdr.contents;
12636
12637 irel = elf_section_data (sec)->relocs;
12638 irelend = irel + sec->reloc_count;
12639
12640 /* Actually delete the bytes. */
12641 memmove (contents + addr, contents + addr + count,
12642 (size_t) (sec->size - addr - count));
12643 sec->size -= count;
12644
12645 /* Adjust all the relocs. */
12646 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
12647 {
12648 /* Get the new reloc address. */
12649 if (irel->r_offset > addr)
12650 irel->r_offset -= count;
12651 }
12652
12653 BFD_ASSERT (addr % 2 == 0);
12654 BFD_ASSERT (count % 2 == 0);
12655
12656 /* Adjust the local symbols defined in this section. */
12657 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12658 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
12659 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
12660 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
12661 isym->st_value -= count;
12662
12663 /* Now adjust the global symbols defined in this section. */
12664 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
12665 - symtab_hdr->sh_info);
12666 sym_hashes = start_hashes = elf_sym_hashes (abfd);
12667 end_hashes = sym_hashes + symcount;
12668
12669 for (; sym_hashes < end_hashes; sym_hashes++)
12670 {
12671 struct elf_link_hash_entry *sym_hash = *sym_hashes;
12672
12673 if ((sym_hash->root.type == bfd_link_hash_defined
12674 || sym_hash->root.type == bfd_link_hash_defweak)
12675 && sym_hash->root.u.def.section == sec)
12676 {
12677 bfd_vma value = sym_hash->root.u.def.value;
12678
12679 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
12680 value &= MINUS_TWO;
12681 if (value > addr)
12682 sym_hash->root.u.def.value -= count;
12683 }
12684 }
12685
12686 return TRUE;
12687 }
12688
12689
12690 /* Opcodes needed for microMIPS relaxation as found in
12691 opcodes/micromips-opc.c. */
12692
12693 struct opcode_descriptor {
12694 unsigned long match;
12695 unsigned long mask;
12696 };
12697
12698 /* The $ra register aka $31. */
12699
12700 #define RA 31
12701
12702 /* 32-bit instruction format register fields. */
12703
12704 #define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
12705 #define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
12706
12707 /* Check if a 5-bit register index can be abbreviated to 3 bits. */
12708
12709 #define OP16_VALID_REG(r) \
12710 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
12711
12712
12713 /* 32-bit and 16-bit branches. */
12714
12715 static const struct opcode_descriptor b_insns_32[] = {
12716 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
12717 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
12718 { 0, 0 } /* End marker for find_match(). */
12719 };
12720
12721 static const struct opcode_descriptor bc_insn_32 =
12722 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
12723
12724 static const struct opcode_descriptor bz_insn_32 =
12725 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
12726
12727 static const struct opcode_descriptor bzal_insn_32 =
12728 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
12729
12730 static const struct opcode_descriptor beq_insn_32 =
12731 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
12732
12733 static const struct opcode_descriptor b_insn_16 =
12734 { /* "b", "mD", */ 0xcc00, 0xfc00 };
12735
12736 static const struct opcode_descriptor bz_insn_16 =
12737 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
12738
12739
12740 /* 32-bit and 16-bit branch EQ and NE zero. */
12741
12742 /* NOTE: All opcode tables have BEQ/BNE in the same order: first the
12743 eq and second the ne. This convention is used when replacing a
12744 32-bit BEQ/BNE with the 16-bit version. */
12745
12746 #define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
12747
12748 static const struct opcode_descriptor bz_rs_insns_32[] = {
12749 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
12750 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
12751 { 0, 0 } /* End marker for find_match(). */
12752 };
12753
12754 static const struct opcode_descriptor bz_rt_insns_32[] = {
12755 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
12756 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
12757 { 0, 0 } /* End marker for find_match(). */
12758 };
12759
12760 static const struct opcode_descriptor bzc_insns_32[] = {
12761 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
12762 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
12763 { 0, 0 } /* End marker for find_match(). */
12764 };
12765
12766 static const struct opcode_descriptor bz_insns_16[] = {
12767 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
12768 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
12769 { 0, 0 } /* End marker for find_match(). */
12770 };
12771
12772 /* Switch between a 5-bit register index and its 3-bit shorthand. */
12773
12774 #define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0x17) + 2)
12775 #define BZ16_REG_FIELD(r) \
12776 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 7)
12777
12778
12779 /* 32-bit instructions with a delay slot. */
12780
12781 static const struct opcode_descriptor jal_insn_32_bd16 =
12782 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
12783
12784 static const struct opcode_descriptor jal_insn_32_bd32 =
12785 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
12786
12787 static const struct opcode_descriptor jal_x_insn_32_bd32 =
12788 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
12789
12790 static const struct opcode_descriptor j_insn_32 =
12791 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
12792
12793 static const struct opcode_descriptor jalr_insn_32 =
12794 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
12795
12796 /* This table can be compacted, because no opcode replacement is made. */
12797
12798 static const struct opcode_descriptor ds_insns_32_bd16[] = {
12799 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
12800
12801 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
12802 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
12803
12804 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
12805 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
12806 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
12807 { 0, 0 } /* End marker for find_match(). */
12808 };
12809
12810 /* This table can be compacted, because no opcode replacement is made. */
12811
12812 static const struct opcode_descriptor ds_insns_32_bd32[] = {
12813 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
12814
12815 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
12816 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
12817 { 0, 0 } /* End marker for find_match(). */
12818 };
12819
12820
12821 /* 16-bit instructions with a delay slot. */
12822
12823 static const struct opcode_descriptor jalr_insn_16_bd16 =
12824 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
12825
12826 static const struct opcode_descriptor jalr_insn_16_bd32 =
12827 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
12828
12829 static const struct opcode_descriptor jr_insn_16 =
12830 { /* "jr", "mj", */ 0x4580, 0xffe0 };
12831
12832 #define JR16_REG(opcode) ((opcode) & 0x1f)
12833
12834 /* This table can be compacted, because no opcode replacement is made. */
12835
12836 static const struct opcode_descriptor ds_insns_16_bd16[] = {
12837 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
12838
12839 { /* "b", "mD", */ 0xcc00, 0xfc00 },
12840 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
12841 { /* "jr", "mj", */ 0x4580, 0xffe0 },
12842 { 0, 0 } /* End marker for find_match(). */
12843 };
12844
12845
12846 /* LUI instruction. */
12847
12848 static const struct opcode_descriptor lui_insn =
12849 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
12850
12851
12852 /* ADDIU instruction. */
12853
12854 static const struct opcode_descriptor addiu_insn =
12855 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
12856
12857 static const struct opcode_descriptor addiupc_insn =
12858 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
12859
12860 #define ADDIUPC_REG_FIELD(r) \
12861 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
12862
12863
12864 /* Relaxable instructions in a JAL delay slot: MOVE. */
12865
12866 /* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
12867 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
12868 #define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
12869 #define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
12870
12871 #define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
12872 #define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
12873
12874 static const struct opcode_descriptor move_insns_32[] = {
12875 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
12876 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
12877 { 0, 0 } /* End marker for find_match(). */
12878 };
12879
12880 static const struct opcode_descriptor move_insn_16 =
12881 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
12882
12883
12884 /* NOP instructions. */
12885
12886 static const struct opcode_descriptor nop_insn_32 =
12887 { /* "nop", "", */ 0x00000000, 0xffffffff };
12888
12889 static const struct opcode_descriptor nop_insn_16 =
12890 { /* "nop", "", */ 0x0c00, 0xffff };
12891
12892
12893 /* Instruction match support. */
12894
12895 #define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
12896
12897 static int
12898 find_match (unsigned long opcode, const struct opcode_descriptor insn[])
12899 {
12900 unsigned long indx;
12901
12902 for (indx = 0; insn[indx].mask != 0; indx++)
12903 if (MATCH (opcode, insn[indx]))
12904 return indx;
12905
12906 return -1;
12907 }
12908
12909
12910 /* Branch and delay slot decoding support. */
12911
12912 /* If PTR points to what *might* be a 16-bit branch or jump, then
12913 return the minimum length of its delay slot, otherwise return 0.
12914 Non-zero results are not definitive as we might be checking against
12915 the second half of another instruction. */
12916
12917 static int
12918 check_br16_dslot (bfd *abfd, bfd_byte *ptr)
12919 {
12920 unsigned long opcode;
12921 int bdsize;
12922
12923 opcode = bfd_get_16 (abfd, ptr);
12924 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
12925 /* 16-bit branch/jump with a 32-bit delay slot. */
12926 bdsize = 4;
12927 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
12928 || find_match (opcode, ds_insns_16_bd16) >= 0)
12929 /* 16-bit branch/jump with a 16-bit delay slot. */
12930 bdsize = 2;
12931 else
12932 /* No delay slot. */
12933 bdsize = 0;
12934
12935 return bdsize;
12936 }
12937
12938 /* If PTR points to what *might* be a 32-bit branch or jump, then
12939 return the minimum length of its delay slot, otherwise return 0.
12940 Non-zero results are not definitive as we might be checking against
12941 the second half of another instruction. */
12942
12943 static int
12944 check_br32_dslot (bfd *abfd, bfd_byte *ptr)
12945 {
12946 unsigned long opcode;
12947 int bdsize;
12948
12949 opcode = bfd_get_micromips_32 (abfd, ptr);
12950 if (find_match (opcode, ds_insns_32_bd32) >= 0)
12951 /* 32-bit branch/jump with a 32-bit delay slot. */
12952 bdsize = 4;
12953 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
12954 /* 32-bit branch/jump with a 16-bit delay slot. */
12955 bdsize = 2;
12956 else
12957 /* No delay slot. */
12958 bdsize = 0;
12959
12960 return bdsize;
12961 }
12962
12963 /* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
12964 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
12965
12966 static bfd_boolean
12967 check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12968 {
12969 unsigned long opcode;
12970
12971 opcode = bfd_get_16 (abfd, ptr);
12972 if (MATCH (opcode, b_insn_16)
12973 /* B16 */
12974 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
12975 /* JR16 */
12976 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
12977 /* BEQZ16, BNEZ16 */
12978 || (MATCH (opcode, jalr_insn_16_bd32)
12979 /* JALR16 */
12980 && reg != JR16_REG (opcode) && reg != RA))
12981 return TRUE;
12982
12983 return FALSE;
12984 }
12985
12986 /* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
12987 then return TRUE, otherwise FALSE. */
12988
12989 static bfd_boolean
12990 check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
12991 {
12992 unsigned long opcode;
12993
12994 opcode = bfd_get_micromips_32 (abfd, ptr);
12995 if (MATCH (opcode, j_insn_32)
12996 /* J */
12997 || MATCH (opcode, bc_insn_32)
12998 /* BC1F, BC1T, BC2F, BC2T */
12999 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13000 /* JAL, JALX */
13001 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13002 /* BGEZ, BGTZ, BLEZ, BLTZ */
13003 || (MATCH (opcode, bzal_insn_32)
13004 /* BGEZAL, BLTZAL */
13005 && reg != OP32_SREG (opcode) && reg != RA)
13006 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13007 /* JALR, JALR.HB, BEQ, BNE */
13008 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13009 return TRUE;
13010
13011 return FALSE;
13012 }
13013
13014 /* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13015 IRELEND) at OFFSET indicate that there must be a compact branch there,
13016 then return TRUE, otherwise FALSE. */
13017
13018 static bfd_boolean
13019 check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13020 const Elf_Internal_Rela *internal_relocs,
13021 const Elf_Internal_Rela *irelend)
13022 {
13023 const Elf_Internal_Rela *irel;
13024 unsigned long opcode;
13025
13026 opcode = bfd_get_micromips_32 (abfd, ptr);
13027 if (find_match (opcode, bzc_insns_32) < 0)
13028 return FALSE;
13029
13030 for (irel = internal_relocs; irel < irelend; irel++)
13031 if (irel->r_offset == offset
13032 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13033 return TRUE;
13034
13035 return FALSE;
13036 }
13037
13038 /* Bitsize checking. */
13039 #define IS_BITSIZE(val, N) \
13040 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13041 - (1ULL << ((N) - 1))) == (val))
13042
13043 \f
13044 bfd_boolean
13045 _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13046 struct bfd_link_info *link_info,
13047 bfd_boolean *again)
13048 {
13049 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
13050 Elf_Internal_Shdr *symtab_hdr;
13051 Elf_Internal_Rela *internal_relocs;
13052 Elf_Internal_Rela *irel, *irelend;
13053 bfd_byte *contents = NULL;
13054 Elf_Internal_Sym *isymbuf = NULL;
13055
13056 /* Assume nothing changes. */
13057 *again = FALSE;
13058
13059 /* We don't have to do anything for a relocatable link, if
13060 this section does not have relocs, or if this is not a
13061 code section. */
13062
13063 if (link_info->relocatable
13064 || (sec->flags & SEC_RELOC) == 0
13065 || sec->reloc_count == 0
13066 || (sec->flags & SEC_CODE) == 0)
13067 return TRUE;
13068
13069 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13070
13071 /* Get a copy of the native relocations. */
13072 internal_relocs = (_bfd_elf_link_read_relocs
13073 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
13074 link_info->keep_memory));
13075 if (internal_relocs == NULL)
13076 goto error_return;
13077
13078 /* Walk through them looking for relaxing opportunities. */
13079 irelend = internal_relocs + sec->reloc_count;
13080 for (irel = internal_relocs; irel < irelend; irel++)
13081 {
13082 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13083 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13084 bfd_boolean target_is_micromips_code_p;
13085 unsigned long opcode;
13086 bfd_vma symval;
13087 bfd_vma pcrval;
13088 bfd_byte *ptr;
13089 int fndopc;
13090
13091 /* The number of bytes to delete for relaxation and from where
13092 to delete these bytes starting at irel->r_offset. */
13093 int delcnt = 0;
13094 int deloff = 0;
13095
13096 /* If this isn't something that can be relaxed, then ignore
13097 this reloc. */
13098 if (r_type != R_MICROMIPS_HI16
13099 && r_type != R_MICROMIPS_PC16_S1
13100 && r_type != R_MICROMIPS_26_S1)
13101 continue;
13102
13103 /* Get the section contents if we haven't done so already. */
13104 if (contents == NULL)
13105 {
13106 /* Get cached copy if it exists. */
13107 if (elf_section_data (sec)->this_hdr.contents != NULL)
13108 contents = elf_section_data (sec)->this_hdr.contents;
13109 /* Go get them off disk. */
13110 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13111 goto error_return;
13112 }
13113 ptr = contents + irel->r_offset;
13114
13115 /* Read this BFD's local symbols if we haven't done so already. */
13116 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13117 {
13118 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13119 if (isymbuf == NULL)
13120 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13121 symtab_hdr->sh_info, 0,
13122 NULL, NULL, NULL);
13123 if (isymbuf == NULL)
13124 goto error_return;
13125 }
13126
13127 /* Get the value of the symbol referred to by the reloc. */
13128 if (r_symndx < symtab_hdr->sh_info)
13129 {
13130 /* A local symbol. */
13131 Elf_Internal_Sym *isym;
13132 asection *sym_sec;
13133
13134 isym = isymbuf + r_symndx;
13135 if (isym->st_shndx == SHN_UNDEF)
13136 sym_sec = bfd_und_section_ptr;
13137 else if (isym->st_shndx == SHN_ABS)
13138 sym_sec = bfd_abs_section_ptr;
13139 else if (isym->st_shndx == SHN_COMMON)
13140 sym_sec = bfd_com_section_ptr;
13141 else
13142 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13143 symval = (isym->st_value
13144 + sym_sec->output_section->vma
13145 + sym_sec->output_offset);
13146 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13147 }
13148 else
13149 {
13150 unsigned long indx;
13151 struct elf_link_hash_entry *h;
13152
13153 /* An external symbol. */
13154 indx = r_symndx - symtab_hdr->sh_info;
13155 h = elf_sym_hashes (abfd)[indx];
13156 BFD_ASSERT (h != NULL);
13157
13158 if (h->root.type != bfd_link_hash_defined
13159 && h->root.type != bfd_link_hash_defweak)
13160 /* This appears to be a reference to an undefined
13161 symbol. Just ignore it -- it will be caught by the
13162 regular reloc processing. */
13163 continue;
13164
13165 symval = (h->root.u.def.value
13166 + h->root.u.def.section->output_section->vma
13167 + h->root.u.def.section->output_offset);
13168 target_is_micromips_code_p = (!h->needs_plt
13169 && ELF_ST_IS_MICROMIPS (h->other));
13170 }
13171
13172
13173 /* For simplicity of coding, we are going to modify the
13174 section contents, the section relocs, and the BFD symbol
13175 table. We must tell the rest of the code not to free up this
13176 information. It would be possible to instead create a table
13177 of changes which have to be made, as is done in coff-mips.c;
13178 that would be more work, but would require less memory when
13179 the linker is run. */
13180
13181 /* Only 32-bit instructions relaxed. */
13182 if (irel->r_offset + 4 > sec->size)
13183 continue;
13184
13185 opcode = bfd_get_micromips_32 (abfd, ptr);
13186
13187 /* This is the pc-relative distance from the instruction the
13188 relocation is applied to, to the symbol referred. */
13189 pcrval = (symval
13190 - (sec->output_section->vma + sec->output_offset)
13191 - irel->r_offset);
13192
13193 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
13194 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13195 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
13196
13197 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
13198
13199 where pcrval has first to be adjusted to apply against the LO16
13200 location (we make the adjustment later on, when we have figured
13201 out the offset). */
13202 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13203 {
13204 bfd_boolean bzc = FALSE;
13205 unsigned long nextopc;
13206 unsigned long reg;
13207 bfd_vma offset;
13208
13209 /* Give up if the previous reloc was a HI16 against this symbol
13210 too. */
13211 if (irel > internal_relocs
13212 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13213 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13214 continue;
13215
13216 /* Or if the next reloc is not a LO16 against this symbol. */
13217 if (irel + 1 >= irelend
13218 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13219 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13220 continue;
13221
13222 /* Or if the second next reloc is a LO16 against this symbol too. */
13223 if (irel + 2 >= irelend
13224 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13225 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13226 continue;
13227
13228 /* See if the LUI instruction *might* be in a branch delay slot.
13229 We check whether what looks like a 16-bit branch or jump is
13230 actually an immediate argument to a compact branch, and let
13231 it through if so. */
13232 if (irel->r_offset >= 2
13233 && check_br16_dslot (abfd, ptr - 2)
13234 && !(irel->r_offset >= 4
13235 && (bzc = check_relocated_bzc (abfd,
13236 ptr - 4, irel->r_offset - 4,
13237 internal_relocs, irelend))))
13238 continue;
13239 if (irel->r_offset >= 4
13240 && !bzc
13241 && check_br32_dslot (abfd, ptr - 4))
13242 continue;
13243
13244 reg = OP32_SREG (opcode);
13245
13246 /* We only relax adjacent instructions or ones separated with
13247 a branch or jump that has a delay slot. The branch or jump
13248 must not fiddle with the register used to hold the address.
13249 Subtract 4 for the LUI itself. */
13250 offset = irel[1].r_offset - irel[0].r_offset;
13251 switch (offset - 4)
13252 {
13253 case 0:
13254 break;
13255 case 2:
13256 if (check_br16 (abfd, ptr + 4, reg))
13257 break;
13258 continue;
13259 case 4:
13260 if (check_br32 (abfd, ptr + 4, reg))
13261 break;
13262 continue;
13263 default:
13264 continue;
13265 }
13266
13267 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
13268
13269 /* Give up unless the same register is used with both
13270 relocations. */
13271 if (OP32_SREG (nextopc) != reg)
13272 continue;
13273
13274 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13275 and rounding up to take masking of the two LSBs into account. */
13276 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13277
13278 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13279 if (IS_BITSIZE (symval, 16))
13280 {
13281 /* Fix the relocation's type. */
13282 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13283
13284 /* Instructions using R_MICROMIPS_LO16 have the base or
13285 source register in bits 20:16. This register becomes $0
13286 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
13287 nextopc &= ~0x001f0000;
13288 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13289 contents + irel[1].r_offset);
13290 }
13291
13292 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13293 We add 4 to take LUI deletion into account while checking
13294 the PC-relative distance. */
13295 else if (symval % 4 == 0
13296 && IS_BITSIZE (pcrval + 4, 25)
13297 && MATCH (nextopc, addiu_insn)
13298 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13299 && OP16_VALID_REG (OP32_TREG (nextopc)))
13300 {
13301 /* Fix the relocation's type. */
13302 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13303
13304 /* Replace ADDIU with the ADDIUPC version. */
13305 nextopc = (addiupc_insn.match
13306 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13307
13308 bfd_put_micromips_32 (abfd, nextopc,
13309 contents + irel[1].r_offset);
13310 }
13311
13312 /* Can't do anything, give up, sigh... */
13313 else
13314 continue;
13315
13316 /* Fix the relocation's type. */
13317 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13318
13319 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13320 delcnt = 4;
13321 deloff = 0;
13322 }
13323
13324 /* Compact branch relaxation -- due to the multitude of macros
13325 employed by the compiler/assembler, compact branches are not
13326 always generated. Obviously, this can/will be fixed elsewhere,
13327 but there is no drawback in double checking it here. */
13328 else if (r_type == R_MICROMIPS_PC16_S1
13329 && irel->r_offset + 5 < sec->size
13330 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13331 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
13332 && ((!insn32
13333 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13334 nop_insn_16) ? 2 : 0))
13335 || (irel->r_offset + 7 < sec->size
13336 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13337 ptr + 4),
13338 nop_insn_32) ? 4 : 0))))
13339 {
13340 unsigned long reg;
13341
13342 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13343
13344 /* Replace BEQZ/BNEZ with the compact version. */
13345 opcode = (bzc_insns_32[fndopc].match
13346 | BZC32_REG_FIELD (reg)
13347 | (opcode & 0xffff)); /* Addend value. */
13348
13349 bfd_put_micromips_32 (abfd, opcode, ptr);
13350
13351 /* Delete the delay slot NOP: two or four bytes from
13352 irel->offset + 4; delcnt has already been set above. */
13353 deloff = 4;
13354 }
13355
13356 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
13357 to check the distance from the next instruction, so subtract 2. */
13358 else if (!insn32
13359 && r_type == R_MICROMIPS_PC16_S1
13360 && IS_BITSIZE (pcrval - 2, 11)
13361 && find_match (opcode, b_insns_32) >= 0)
13362 {
13363 /* Fix the relocation's type. */
13364 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
13365
13366 /* Replace the 32-bit opcode with a 16-bit opcode. */
13367 bfd_put_16 (abfd,
13368 (b_insn_16.match
13369 | (opcode & 0x3ff)), /* Addend value. */
13370 ptr);
13371
13372 /* Delete 2 bytes from irel->r_offset + 2. */
13373 delcnt = 2;
13374 deloff = 2;
13375 }
13376
13377 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
13378 to check the distance from the next instruction, so subtract 2. */
13379 else if (!insn32
13380 && r_type == R_MICROMIPS_PC16_S1
13381 && IS_BITSIZE (pcrval - 2, 8)
13382 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13383 && OP16_VALID_REG (OP32_SREG (opcode)))
13384 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
13385 && OP16_VALID_REG (OP32_TREG (opcode)))))
13386 {
13387 unsigned long reg;
13388
13389 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13390
13391 /* Fix the relocation's type. */
13392 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
13393
13394 /* Replace the 32-bit opcode with a 16-bit opcode. */
13395 bfd_put_16 (abfd,
13396 (bz_insns_16[fndopc].match
13397 | BZ16_REG_FIELD (reg)
13398 | (opcode & 0x7f)), /* Addend value. */
13399 ptr);
13400
13401 /* Delete 2 bytes from irel->r_offset + 2. */
13402 delcnt = 2;
13403 deloff = 2;
13404 }
13405
13406 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
13407 else if (!insn32
13408 && r_type == R_MICROMIPS_26_S1
13409 && target_is_micromips_code_p
13410 && irel->r_offset + 7 < sec->size
13411 && MATCH (opcode, jal_insn_32_bd32))
13412 {
13413 unsigned long n32opc;
13414 bfd_boolean relaxed = FALSE;
13415
13416 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
13417
13418 if (MATCH (n32opc, nop_insn_32))
13419 {
13420 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
13421 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
13422
13423 relaxed = TRUE;
13424 }
13425 else if (find_match (n32opc, move_insns_32) >= 0)
13426 {
13427 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
13428 bfd_put_16 (abfd,
13429 (move_insn_16.match
13430 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
13431 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
13432 ptr + 4);
13433
13434 relaxed = TRUE;
13435 }
13436 /* Other 32-bit instructions relaxable to 16-bit
13437 instructions will be handled here later. */
13438
13439 if (relaxed)
13440 {
13441 /* JAL with 32-bit delay slot that is changed to a JALS
13442 with 16-bit delay slot. */
13443 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
13444
13445 /* Delete 2 bytes from irel->r_offset + 6. */
13446 delcnt = 2;
13447 deloff = 6;
13448 }
13449 }
13450
13451 if (delcnt != 0)
13452 {
13453 /* Note that we've changed the relocs, section contents, etc. */
13454 elf_section_data (sec)->relocs = internal_relocs;
13455 elf_section_data (sec)->this_hdr.contents = contents;
13456 symtab_hdr->contents = (unsigned char *) isymbuf;
13457
13458 /* Delete bytes depending on the delcnt and deloff. */
13459 if (!mips_elf_relax_delete_bytes (abfd, sec,
13460 irel->r_offset + deloff, delcnt))
13461 goto error_return;
13462
13463 /* That will change things, so we should relax again.
13464 Note that this is not required, and it may be slow. */
13465 *again = TRUE;
13466 }
13467 }
13468
13469 if (isymbuf != NULL
13470 && symtab_hdr->contents != (unsigned char *) isymbuf)
13471 {
13472 if (! link_info->keep_memory)
13473 free (isymbuf);
13474 else
13475 {
13476 /* Cache the symbols for elf_link_input_bfd. */
13477 symtab_hdr->contents = (unsigned char *) isymbuf;
13478 }
13479 }
13480
13481 if (contents != NULL
13482 && elf_section_data (sec)->this_hdr.contents != contents)
13483 {
13484 if (! link_info->keep_memory)
13485 free (contents);
13486 else
13487 {
13488 /* Cache the section contents for elf_link_input_bfd. */
13489 elf_section_data (sec)->this_hdr.contents = contents;
13490 }
13491 }
13492
13493 if (internal_relocs != NULL
13494 && elf_section_data (sec)->relocs != internal_relocs)
13495 free (internal_relocs);
13496
13497 return TRUE;
13498
13499 error_return:
13500 if (isymbuf != NULL
13501 && symtab_hdr->contents != (unsigned char *) isymbuf)
13502 free (isymbuf);
13503 if (contents != NULL
13504 && elf_section_data (sec)->this_hdr.contents != contents)
13505 free (contents);
13506 if (internal_relocs != NULL
13507 && elf_section_data (sec)->relocs != internal_relocs)
13508 free (internal_relocs);
13509
13510 return FALSE;
13511 }
13512 \f
13513 /* Create a MIPS ELF linker hash table. */
13514
13515 struct bfd_link_hash_table *
13516 _bfd_mips_elf_link_hash_table_create (bfd *abfd)
13517 {
13518 struct mips_elf_link_hash_table *ret;
13519 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
13520
13521 ret = bfd_zmalloc (amt);
13522 if (ret == NULL)
13523 return NULL;
13524
13525 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
13526 mips_elf_link_hash_newfunc,
13527 sizeof (struct mips_elf_link_hash_entry),
13528 MIPS_ELF_DATA))
13529 {
13530 free (ret);
13531 return NULL;
13532 }
13533 ret->root.init_plt_refcount.plist = NULL;
13534 ret->root.init_plt_offset.plist = NULL;
13535
13536 return &ret->root.root;
13537 }
13538
13539 /* Likewise, but indicate that the target is VxWorks. */
13540
13541 struct bfd_link_hash_table *
13542 _bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
13543 {
13544 struct bfd_link_hash_table *ret;
13545
13546 ret = _bfd_mips_elf_link_hash_table_create (abfd);
13547 if (ret)
13548 {
13549 struct mips_elf_link_hash_table *htab;
13550
13551 htab = (struct mips_elf_link_hash_table *) ret;
13552 htab->use_plts_and_copy_relocs = TRUE;
13553 htab->is_vxworks = TRUE;
13554 }
13555 return ret;
13556 }
13557
13558 /* A function that the linker calls if we are allowed to use PLTs
13559 and copy relocs. */
13560
13561 void
13562 _bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
13563 {
13564 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
13565 }
13566
13567 /* A function that the linker calls to select between all or only
13568 32-bit microMIPS instructions. */
13569
13570 void
13571 _bfd_mips_elf_insn32 (struct bfd_link_info *info, bfd_boolean on)
13572 {
13573 mips_elf_hash_table (info)->insn32 = on;
13574 }
13575 \f
13576 /* We need to use a special link routine to handle the .reginfo and
13577 the .mdebug sections. We need to merge all instances of these
13578 sections together, not write them all out sequentially. */
13579
13580 bfd_boolean
13581 _bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
13582 {
13583 asection *o;
13584 struct bfd_link_order *p;
13585 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
13586 asection *rtproc_sec;
13587 Elf32_RegInfo reginfo;
13588 struct ecoff_debug_info debug;
13589 struct mips_htab_traverse_info hti;
13590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13591 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
13592 HDRR *symhdr = &debug.symbolic_header;
13593 void *mdebug_handle = NULL;
13594 asection *s;
13595 EXTR esym;
13596 unsigned int i;
13597 bfd_size_type amt;
13598 struct mips_elf_link_hash_table *htab;
13599
13600 static const char * const secname[] =
13601 {
13602 ".text", ".init", ".fini", ".data",
13603 ".rodata", ".sdata", ".sbss", ".bss"
13604 };
13605 static const int sc[] =
13606 {
13607 scText, scInit, scFini, scData,
13608 scRData, scSData, scSBss, scBss
13609 };
13610
13611 /* Sort the dynamic symbols so that those with GOT entries come after
13612 those without. */
13613 htab = mips_elf_hash_table (info);
13614 BFD_ASSERT (htab != NULL);
13615
13616 if (!mips_elf_sort_hash_table (abfd, info))
13617 return FALSE;
13618
13619 /* Create any scheduled LA25 stubs. */
13620 hti.info = info;
13621 hti.output_bfd = abfd;
13622 hti.error = FALSE;
13623 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
13624 if (hti.error)
13625 return FALSE;
13626
13627 /* Get a value for the GP register. */
13628 if (elf_gp (abfd) == 0)
13629 {
13630 struct bfd_link_hash_entry *h;
13631
13632 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
13633 if (h != NULL && h->type == bfd_link_hash_defined)
13634 elf_gp (abfd) = (h->u.def.value
13635 + h->u.def.section->output_section->vma
13636 + h->u.def.section->output_offset);
13637 else if (htab->is_vxworks
13638 && (h = bfd_link_hash_lookup (info->hash,
13639 "_GLOBAL_OFFSET_TABLE_",
13640 FALSE, FALSE, TRUE))
13641 && h->type == bfd_link_hash_defined)
13642 elf_gp (abfd) = (h->u.def.section->output_section->vma
13643 + h->u.def.section->output_offset
13644 + h->u.def.value);
13645 else if (info->relocatable)
13646 {
13647 bfd_vma lo = MINUS_ONE;
13648
13649 /* Find the GP-relative section with the lowest offset. */
13650 for (o = abfd->sections; o != NULL; o = o->next)
13651 if (o->vma < lo
13652 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
13653 lo = o->vma;
13654
13655 /* And calculate GP relative to that. */
13656 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
13657 }
13658 else
13659 {
13660 /* If the relocate_section function needs to do a reloc
13661 involving the GP value, it should make a reloc_dangerous
13662 callback to warn that GP is not defined. */
13663 }
13664 }
13665
13666 /* Go through the sections and collect the .reginfo and .mdebug
13667 information. */
13668 reginfo_sec = NULL;
13669 mdebug_sec = NULL;
13670 gptab_data_sec = NULL;
13671 gptab_bss_sec = NULL;
13672 for (o = abfd->sections; o != NULL; o = o->next)
13673 {
13674 if (strcmp (o->name, ".reginfo") == 0)
13675 {
13676 memset (&reginfo, 0, sizeof reginfo);
13677
13678 /* We have found the .reginfo section in the output file.
13679 Look through all the link_orders comprising it and merge
13680 the information together. */
13681 for (p = o->map_head.link_order; p != NULL; p = p->next)
13682 {
13683 asection *input_section;
13684 bfd *input_bfd;
13685 Elf32_External_RegInfo ext;
13686 Elf32_RegInfo sub;
13687
13688 if (p->type != bfd_indirect_link_order)
13689 {
13690 if (p->type == bfd_data_link_order)
13691 continue;
13692 abort ();
13693 }
13694
13695 input_section = p->u.indirect.section;
13696 input_bfd = input_section->owner;
13697
13698 if (! bfd_get_section_contents (input_bfd, input_section,
13699 &ext, 0, sizeof ext))
13700 return FALSE;
13701
13702 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
13703
13704 reginfo.ri_gprmask |= sub.ri_gprmask;
13705 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
13706 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
13707 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
13708 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
13709
13710 /* ri_gp_value is set by the function
13711 mips_elf32_section_processing when the section is
13712 finally written out. */
13713
13714 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13715 elf_link_input_bfd ignores this section. */
13716 input_section->flags &= ~SEC_HAS_CONTENTS;
13717 }
13718
13719 /* Size has been set in _bfd_mips_elf_always_size_sections. */
13720 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
13721
13722 /* Skip this section later on (I don't think this currently
13723 matters, but someday it might). */
13724 o->map_head.link_order = NULL;
13725
13726 reginfo_sec = o;
13727 }
13728
13729 if (strcmp (o->name, ".mdebug") == 0)
13730 {
13731 struct extsym_info einfo;
13732 bfd_vma last;
13733
13734 /* We have found the .mdebug section in the output file.
13735 Look through all the link_orders comprising it and merge
13736 the information together. */
13737 symhdr->magic = swap->sym_magic;
13738 /* FIXME: What should the version stamp be? */
13739 symhdr->vstamp = 0;
13740 symhdr->ilineMax = 0;
13741 symhdr->cbLine = 0;
13742 symhdr->idnMax = 0;
13743 symhdr->ipdMax = 0;
13744 symhdr->isymMax = 0;
13745 symhdr->ioptMax = 0;
13746 symhdr->iauxMax = 0;
13747 symhdr->issMax = 0;
13748 symhdr->issExtMax = 0;
13749 symhdr->ifdMax = 0;
13750 symhdr->crfd = 0;
13751 symhdr->iextMax = 0;
13752
13753 /* We accumulate the debugging information itself in the
13754 debug_info structure. */
13755 debug.line = NULL;
13756 debug.external_dnr = NULL;
13757 debug.external_pdr = NULL;
13758 debug.external_sym = NULL;
13759 debug.external_opt = NULL;
13760 debug.external_aux = NULL;
13761 debug.ss = NULL;
13762 debug.ssext = debug.ssext_end = NULL;
13763 debug.external_fdr = NULL;
13764 debug.external_rfd = NULL;
13765 debug.external_ext = debug.external_ext_end = NULL;
13766
13767 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
13768 if (mdebug_handle == NULL)
13769 return FALSE;
13770
13771 esym.jmptbl = 0;
13772 esym.cobol_main = 0;
13773 esym.weakext = 0;
13774 esym.reserved = 0;
13775 esym.ifd = ifdNil;
13776 esym.asym.iss = issNil;
13777 esym.asym.st = stLocal;
13778 esym.asym.reserved = 0;
13779 esym.asym.index = indexNil;
13780 last = 0;
13781 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
13782 {
13783 esym.asym.sc = sc[i];
13784 s = bfd_get_section_by_name (abfd, secname[i]);
13785 if (s != NULL)
13786 {
13787 esym.asym.value = s->vma;
13788 last = s->vma + s->size;
13789 }
13790 else
13791 esym.asym.value = last;
13792 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
13793 secname[i], &esym))
13794 return FALSE;
13795 }
13796
13797 for (p = o->map_head.link_order; p != NULL; p = p->next)
13798 {
13799 asection *input_section;
13800 bfd *input_bfd;
13801 const struct ecoff_debug_swap *input_swap;
13802 struct ecoff_debug_info input_debug;
13803 char *eraw_src;
13804 char *eraw_end;
13805
13806 if (p->type != bfd_indirect_link_order)
13807 {
13808 if (p->type == bfd_data_link_order)
13809 continue;
13810 abort ();
13811 }
13812
13813 input_section = p->u.indirect.section;
13814 input_bfd = input_section->owner;
13815
13816 if (!is_mips_elf (input_bfd))
13817 {
13818 /* I don't know what a non MIPS ELF bfd would be
13819 doing with a .mdebug section, but I don't really
13820 want to deal with it. */
13821 continue;
13822 }
13823
13824 input_swap = (get_elf_backend_data (input_bfd)
13825 ->elf_backend_ecoff_debug_swap);
13826
13827 BFD_ASSERT (p->size == input_section->size);
13828
13829 /* The ECOFF linking code expects that we have already
13830 read in the debugging information and set up an
13831 ecoff_debug_info structure, so we do that now. */
13832 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
13833 &input_debug))
13834 return FALSE;
13835
13836 if (! (bfd_ecoff_debug_accumulate
13837 (mdebug_handle, abfd, &debug, swap, input_bfd,
13838 &input_debug, input_swap, info)))
13839 return FALSE;
13840
13841 /* Loop through the external symbols. For each one with
13842 interesting information, try to find the symbol in
13843 the linker global hash table and save the information
13844 for the output external symbols. */
13845 eraw_src = input_debug.external_ext;
13846 eraw_end = (eraw_src
13847 + (input_debug.symbolic_header.iextMax
13848 * input_swap->external_ext_size));
13849 for (;
13850 eraw_src < eraw_end;
13851 eraw_src += input_swap->external_ext_size)
13852 {
13853 EXTR ext;
13854 const char *name;
13855 struct mips_elf_link_hash_entry *h;
13856
13857 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
13858 if (ext.asym.sc == scNil
13859 || ext.asym.sc == scUndefined
13860 || ext.asym.sc == scSUndefined)
13861 continue;
13862
13863 name = input_debug.ssext + ext.asym.iss;
13864 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
13865 name, FALSE, FALSE, TRUE);
13866 if (h == NULL || h->esym.ifd != -2)
13867 continue;
13868
13869 if (ext.ifd != -1)
13870 {
13871 BFD_ASSERT (ext.ifd
13872 < input_debug.symbolic_header.ifdMax);
13873 ext.ifd = input_debug.ifdmap[ext.ifd];
13874 }
13875
13876 h->esym = ext;
13877 }
13878
13879 /* Free up the information we just read. */
13880 free (input_debug.line);
13881 free (input_debug.external_dnr);
13882 free (input_debug.external_pdr);
13883 free (input_debug.external_sym);
13884 free (input_debug.external_opt);
13885 free (input_debug.external_aux);
13886 free (input_debug.ss);
13887 free (input_debug.ssext);
13888 free (input_debug.external_fdr);
13889 free (input_debug.external_rfd);
13890 free (input_debug.external_ext);
13891
13892 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13893 elf_link_input_bfd ignores this section. */
13894 input_section->flags &= ~SEC_HAS_CONTENTS;
13895 }
13896
13897 if (SGI_COMPAT (abfd) && info->shared)
13898 {
13899 /* Create .rtproc section. */
13900 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
13901 if (rtproc_sec == NULL)
13902 {
13903 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
13904 | SEC_LINKER_CREATED | SEC_READONLY);
13905
13906 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
13907 ".rtproc",
13908 flags);
13909 if (rtproc_sec == NULL
13910 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
13911 return FALSE;
13912 }
13913
13914 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
13915 info, rtproc_sec,
13916 &debug))
13917 return FALSE;
13918 }
13919
13920 /* Build the external symbol information. */
13921 einfo.abfd = abfd;
13922 einfo.info = info;
13923 einfo.debug = &debug;
13924 einfo.swap = swap;
13925 einfo.failed = FALSE;
13926 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
13927 mips_elf_output_extsym, &einfo);
13928 if (einfo.failed)
13929 return FALSE;
13930
13931 /* Set the size of the .mdebug section. */
13932 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
13933
13934 /* Skip this section later on (I don't think this currently
13935 matters, but someday it might). */
13936 o->map_head.link_order = NULL;
13937
13938 mdebug_sec = o;
13939 }
13940
13941 if (CONST_STRNEQ (o->name, ".gptab."))
13942 {
13943 const char *subname;
13944 unsigned int c;
13945 Elf32_gptab *tab;
13946 Elf32_External_gptab *ext_tab;
13947 unsigned int j;
13948
13949 /* The .gptab.sdata and .gptab.sbss sections hold
13950 information describing how the small data area would
13951 change depending upon the -G switch. These sections
13952 not used in executables files. */
13953 if (! info->relocatable)
13954 {
13955 for (p = o->map_head.link_order; p != NULL; p = p->next)
13956 {
13957 asection *input_section;
13958
13959 if (p->type != bfd_indirect_link_order)
13960 {
13961 if (p->type == bfd_data_link_order)
13962 continue;
13963 abort ();
13964 }
13965
13966 input_section = p->u.indirect.section;
13967
13968 /* Hack: reset the SEC_HAS_CONTENTS flag so that
13969 elf_link_input_bfd ignores this section. */
13970 input_section->flags &= ~SEC_HAS_CONTENTS;
13971 }
13972
13973 /* Skip this section later on (I don't think this
13974 currently matters, but someday it might). */
13975 o->map_head.link_order = NULL;
13976
13977 /* Really remove the section. */
13978 bfd_section_list_remove (abfd, o);
13979 --abfd->section_count;
13980
13981 continue;
13982 }
13983
13984 /* There is one gptab for initialized data, and one for
13985 uninitialized data. */
13986 if (strcmp (o->name, ".gptab.sdata") == 0)
13987 gptab_data_sec = o;
13988 else if (strcmp (o->name, ".gptab.sbss") == 0)
13989 gptab_bss_sec = o;
13990 else
13991 {
13992 (*_bfd_error_handler)
13993 (_("%s: illegal section name `%s'"),
13994 bfd_get_filename (abfd), o->name);
13995 bfd_set_error (bfd_error_nonrepresentable_section);
13996 return FALSE;
13997 }
13998
13999 /* The linker script always combines .gptab.data and
14000 .gptab.sdata into .gptab.sdata, and likewise for
14001 .gptab.bss and .gptab.sbss. It is possible that there is
14002 no .sdata or .sbss section in the output file, in which
14003 case we must change the name of the output section. */
14004 subname = o->name + sizeof ".gptab" - 1;
14005 if (bfd_get_section_by_name (abfd, subname) == NULL)
14006 {
14007 if (o == gptab_data_sec)
14008 o->name = ".gptab.data";
14009 else
14010 o->name = ".gptab.bss";
14011 subname = o->name + sizeof ".gptab" - 1;
14012 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
14013 }
14014
14015 /* Set up the first entry. */
14016 c = 1;
14017 amt = c * sizeof (Elf32_gptab);
14018 tab = bfd_malloc (amt);
14019 if (tab == NULL)
14020 return FALSE;
14021 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
14022 tab[0].gt_header.gt_unused = 0;
14023
14024 /* Combine the input sections. */
14025 for (p = o->map_head.link_order; p != NULL; p = p->next)
14026 {
14027 asection *input_section;
14028 bfd *input_bfd;
14029 bfd_size_type size;
14030 unsigned long last;
14031 bfd_size_type gpentry;
14032
14033 if (p->type != bfd_indirect_link_order)
14034 {
14035 if (p->type == bfd_data_link_order)
14036 continue;
14037 abort ();
14038 }
14039
14040 input_section = p->u.indirect.section;
14041 input_bfd = input_section->owner;
14042
14043 /* Combine the gptab entries for this input section one
14044 by one. We know that the input gptab entries are
14045 sorted by ascending -G value. */
14046 size = input_section->size;
14047 last = 0;
14048 for (gpentry = sizeof (Elf32_External_gptab);
14049 gpentry < size;
14050 gpentry += sizeof (Elf32_External_gptab))
14051 {
14052 Elf32_External_gptab ext_gptab;
14053 Elf32_gptab int_gptab;
14054 unsigned long val;
14055 unsigned long add;
14056 bfd_boolean exact;
14057 unsigned int look;
14058
14059 if (! (bfd_get_section_contents
14060 (input_bfd, input_section, &ext_gptab, gpentry,
14061 sizeof (Elf32_External_gptab))))
14062 {
14063 free (tab);
14064 return FALSE;
14065 }
14066
14067 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
14068 &int_gptab);
14069 val = int_gptab.gt_entry.gt_g_value;
14070 add = int_gptab.gt_entry.gt_bytes - last;
14071
14072 exact = FALSE;
14073 for (look = 1; look < c; look++)
14074 {
14075 if (tab[look].gt_entry.gt_g_value >= val)
14076 tab[look].gt_entry.gt_bytes += add;
14077
14078 if (tab[look].gt_entry.gt_g_value == val)
14079 exact = TRUE;
14080 }
14081
14082 if (! exact)
14083 {
14084 Elf32_gptab *new_tab;
14085 unsigned int max;
14086
14087 /* We need a new table entry. */
14088 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
14089 new_tab = bfd_realloc (tab, amt);
14090 if (new_tab == NULL)
14091 {
14092 free (tab);
14093 return FALSE;
14094 }
14095 tab = new_tab;
14096 tab[c].gt_entry.gt_g_value = val;
14097 tab[c].gt_entry.gt_bytes = add;
14098
14099 /* Merge in the size for the next smallest -G
14100 value, since that will be implied by this new
14101 value. */
14102 max = 0;
14103 for (look = 1; look < c; look++)
14104 {
14105 if (tab[look].gt_entry.gt_g_value < val
14106 && (max == 0
14107 || (tab[look].gt_entry.gt_g_value
14108 > tab[max].gt_entry.gt_g_value)))
14109 max = look;
14110 }
14111 if (max != 0)
14112 tab[c].gt_entry.gt_bytes +=
14113 tab[max].gt_entry.gt_bytes;
14114
14115 ++c;
14116 }
14117
14118 last = int_gptab.gt_entry.gt_bytes;
14119 }
14120
14121 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14122 elf_link_input_bfd ignores this section. */
14123 input_section->flags &= ~SEC_HAS_CONTENTS;
14124 }
14125
14126 /* The table must be sorted by -G value. */
14127 if (c > 2)
14128 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
14129
14130 /* Swap out the table. */
14131 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
14132 ext_tab = bfd_alloc (abfd, amt);
14133 if (ext_tab == NULL)
14134 {
14135 free (tab);
14136 return FALSE;
14137 }
14138
14139 for (j = 0; j < c; j++)
14140 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
14141 free (tab);
14142
14143 o->size = c * sizeof (Elf32_External_gptab);
14144 o->contents = (bfd_byte *) ext_tab;
14145
14146 /* Skip this section later on (I don't think this currently
14147 matters, but someday it might). */
14148 o->map_head.link_order = NULL;
14149 }
14150 }
14151
14152 /* Invoke the regular ELF backend linker to do all the work. */
14153 if (!bfd_elf_final_link (abfd, info))
14154 return FALSE;
14155
14156 /* Now write out the computed sections. */
14157
14158 if (reginfo_sec != NULL)
14159 {
14160 Elf32_External_RegInfo ext;
14161
14162 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
14163 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
14164 return FALSE;
14165 }
14166
14167 if (mdebug_sec != NULL)
14168 {
14169 BFD_ASSERT (abfd->output_has_begun);
14170 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
14171 swap, info,
14172 mdebug_sec->filepos))
14173 return FALSE;
14174
14175 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
14176 }
14177
14178 if (gptab_data_sec != NULL)
14179 {
14180 if (! bfd_set_section_contents (abfd, gptab_data_sec,
14181 gptab_data_sec->contents,
14182 0, gptab_data_sec->size))
14183 return FALSE;
14184 }
14185
14186 if (gptab_bss_sec != NULL)
14187 {
14188 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
14189 gptab_bss_sec->contents,
14190 0, gptab_bss_sec->size))
14191 return FALSE;
14192 }
14193
14194 if (SGI_COMPAT (abfd))
14195 {
14196 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
14197 if (rtproc_sec != NULL)
14198 {
14199 if (! bfd_set_section_contents (abfd, rtproc_sec,
14200 rtproc_sec->contents,
14201 0, rtproc_sec->size))
14202 return FALSE;
14203 }
14204 }
14205
14206 return TRUE;
14207 }
14208 \f
14209 /* Structure for saying that BFD machine EXTENSION extends BASE. */
14210
14211 struct mips_mach_extension
14212 {
14213 unsigned long extension, base;
14214 };
14215
14216
14217 /* An array describing how BFD machines relate to one another. The entries
14218 are ordered topologically with MIPS I extensions listed last. */
14219
14220 static const struct mips_mach_extension mips_mach_extensions[] =
14221 {
14222 /* MIPS64r2 extensions. */
14223 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14224 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14225 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
14226 { bfd_mach_mips_loongson_3a, bfd_mach_mipsisa64r2 },
14227
14228 /* MIPS64 extensions. */
14229 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14230 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14231 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14232
14233 /* MIPS V extensions. */
14234 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14235
14236 /* R10000 extensions. */
14237 { bfd_mach_mips12000, bfd_mach_mips10000 },
14238 { bfd_mach_mips14000, bfd_mach_mips10000 },
14239 { bfd_mach_mips16000, bfd_mach_mips10000 },
14240
14241 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14242 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14243 better to allow vr5400 and vr5500 code to be merged anyway, since
14244 many libraries will just use the core ISA. Perhaps we could add
14245 some sort of ASE flag if this ever proves a problem. */
14246 { bfd_mach_mips5500, bfd_mach_mips5400 },
14247 { bfd_mach_mips5400, bfd_mach_mips5000 },
14248
14249 /* MIPS IV extensions. */
14250 { bfd_mach_mips5, bfd_mach_mips8000 },
14251 { bfd_mach_mips10000, bfd_mach_mips8000 },
14252 { bfd_mach_mips5000, bfd_mach_mips8000 },
14253 { bfd_mach_mips7000, bfd_mach_mips8000 },
14254 { bfd_mach_mips9000, bfd_mach_mips8000 },
14255
14256 /* VR4100 extensions. */
14257 { bfd_mach_mips4120, bfd_mach_mips4100 },
14258 { bfd_mach_mips4111, bfd_mach_mips4100 },
14259
14260 /* MIPS III extensions. */
14261 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14262 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14263 { bfd_mach_mips8000, bfd_mach_mips4000 },
14264 { bfd_mach_mips4650, bfd_mach_mips4000 },
14265 { bfd_mach_mips4600, bfd_mach_mips4000 },
14266 { bfd_mach_mips4400, bfd_mach_mips4000 },
14267 { bfd_mach_mips4300, bfd_mach_mips4000 },
14268 { bfd_mach_mips4100, bfd_mach_mips4000 },
14269 { bfd_mach_mips4010, bfd_mach_mips4000 },
14270 { bfd_mach_mips5900, bfd_mach_mips4000 },
14271
14272 /* MIPS32 extensions. */
14273 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14274
14275 /* MIPS II extensions. */
14276 { bfd_mach_mips4000, bfd_mach_mips6000 },
14277 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
14278
14279 /* MIPS I extensions. */
14280 { bfd_mach_mips6000, bfd_mach_mips3000 },
14281 { bfd_mach_mips3900, bfd_mach_mips3000 }
14282 };
14283
14284
14285 /* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14286
14287 static bfd_boolean
14288 mips_mach_extends_p (unsigned long base, unsigned long extension)
14289 {
14290 size_t i;
14291
14292 if (extension == base)
14293 return TRUE;
14294
14295 if (base == bfd_mach_mipsisa32
14296 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14297 return TRUE;
14298
14299 if (base == bfd_mach_mipsisa32r2
14300 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14301 return TRUE;
14302
14303 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14304 if (extension == mips_mach_extensions[i].extension)
14305 {
14306 extension = mips_mach_extensions[i].base;
14307 if (extension == base)
14308 return TRUE;
14309 }
14310
14311 return FALSE;
14312 }
14313
14314
14315 /* Return true if the given ELF header flags describe a 32-bit binary. */
14316
14317 static bfd_boolean
14318 mips_32bit_flags_p (flagword flags)
14319 {
14320 return ((flags & EF_MIPS_32BITMODE) != 0
14321 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14322 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14323 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14324 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14325 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
14326 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2);
14327 }
14328
14329
14330 /* Merge object attributes from IBFD into OBFD. Raise an error if
14331 there are conflicting attributes. */
14332 static bfd_boolean
14333 mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
14334 {
14335 obj_attribute *in_attr;
14336 obj_attribute *out_attr;
14337 bfd *abi_fp_bfd;
14338 bfd *abi_msa_bfd;
14339
14340 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
14341 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
14342 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
14343 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
14344
14345 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
14346 if (!abi_msa_bfd
14347 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14348 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
14349
14350 if (!elf_known_obj_attributes_proc (obfd)[0].i)
14351 {
14352 /* This is the first object. Copy the attributes. */
14353 _bfd_elf_copy_obj_attributes (ibfd, obfd);
14354
14355 /* Use the Tag_null value to indicate the attributes have been
14356 initialized. */
14357 elf_known_obj_attributes_proc (obfd)[0].i = 1;
14358
14359 return TRUE;
14360 }
14361
14362 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
14363 non-conflicting ones. */
14364 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
14365 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
14366 {
14367 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
14368 if (out_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
14369 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14370 else if (in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
14371 switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
14372 {
14373 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14374 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14375 {
14376 case Val_GNU_MIPS_ABI_FP_SINGLE:
14377 _bfd_error_handler
14378 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14379 obfd, abi_fp_bfd, ibfd, "-mdouble-float", "-msingle-float");
14380 break;
14381
14382 case Val_GNU_MIPS_ABI_FP_SOFT:
14383 _bfd_error_handler
14384 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14385 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
14386 break;
14387
14388 case Val_GNU_MIPS_ABI_FP_64:
14389 _bfd_error_handler
14390 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14391 obfd, abi_fp_bfd, ibfd,
14392 "-mdouble-float", "-mips32r2 -mfp64");
14393 break;
14394
14395 default:
14396 _bfd_error_handler
14397 (_("Warning: %B uses %s (set by %B), "
14398 "%B uses unknown floating point ABI %d"),
14399 obfd, abi_fp_bfd, ibfd,
14400 "-mdouble-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14401 break;
14402 }
14403 break;
14404
14405 case Val_GNU_MIPS_ABI_FP_SINGLE:
14406 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14407 {
14408 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14409 _bfd_error_handler
14410 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14411 obfd, abi_fp_bfd, ibfd, "-msingle-float", "-mdouble-float");
14412 break;
14413
14414 case Val_GNU_MIPS_ABI_FP_SOFT:
14415 _bfd_error_handler
14416 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14417 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
14418 break;
14419
14420 case Val_GNU_MIPS_ABI_FP_64:
14421 _bfd_error_handler
14422 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14423 obfd, abi_fp_bfd, ibfd,
14424 "-msingle-float", "-mips32r2 -mfp64");
14425 break;
14426
14427 default:
14428 _bfd_error_handler
14429 (_("Warning: %B uses %s (set by %B), "
14430 "%B uses unknown floating point ABI %d"),
14431 obfd, abi_fp_bfd, ibfd,
14432 "-msingle-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14433 break;
14434 }
14435 break;
14436
14437 case Val_GNU_MIPS_ABI_FP_SOFT:
14438 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14439 {
14440 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14441 case Val_GNU_MIPS_ABI_FP_SINGLE:
14442 case Val_GNU_MIPS_ABI_FP_64:
14443 _bfd_error_handler
14444 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14445 obfd, abi_fp_bfd, ibfd, "-msoft-float", "-mhard-float");
14446 break;
14447
14448 default:
14449 _bfd_error_handler
14450 (_("Warning: %B uses %s (set by %B), "
14451 "%B uses unknown floating point ABI %d"),
14452 obfd, abi_fp_bfd, ibfd,
14453 "-msoft-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14454 break;
14455 }
14456 break;
14457
14458 case Val_GNU_MIPS_ABI_FP_64:
14459 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14460 {
14461 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14462 _bfd_error_handler
14463 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14464 obfd, abi_fp_bfd, ibfd,
14465 "-mips32r2 -mfp64", "-mdouble-float");
14466 break;
14467
14468 case Val_GNU_MIPS_ABI_FP_SINGLE:
14469 _bfd_error_handler
14470 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14471 obfd, abi_fp_bfd, ibfd,
14472 "-mips32r2 -mfp64", "-msingle-float");
14473 break;
14474
14475 case Val_GNU_MIPS_ABI_FP_SOFT:
14476 _bfd_error_handler
14477 (_("Warning: %B uses %s (set by %B), %B uses %s"),
14478 obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
14479 break;
14480
14481 default:
14482 _bfd_error_handler
14483 (_("Warning: %B uses %s (set by %B), "
14484 "%B uses unknown floating point ABI %d"),
14485 obfd, abi_fp_bfd, ibfd,
14486 "-mips32r2 -mfp64", in_attr[Tag_GNU_MIPS_ABI_FP].i);
14487 break;
14488 }
14489 break;
14490
14491 default:
14492 switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
14493 {
14494 case Val_GNU_MIPS_ABI_FP_DOUBLE:
14495 _bfd_error_handler
14496 (_("Warning: %B uses unknown floating point ABI %d "
14497 "(set by %B), %B uses %s"),
14498 obfd, abi_fp_bfd, ibfd,
14499 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mdouble-float");
14500 break;
14501
14502 case Val_GNU_MIPS_ABI_FP_SINGLE:
14503 _bfd_error_handler
14504 (_("Warning: %B uses unknown floating point ABI %d "
14505 "(set by %B), %B uses %s"),
14506 obfd, abi_fp_bfd, ibfd,
14507 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msingle-float");
14508 break;
14509
14510 case Val_GNU_MIPS_ABI_FP_SOFT:
14511 _bfd_error_handler
14512 (_("Warning: %B uses unknown floating point ABI %d "
14513 "(set by %B), %B uses %s"),
14514 obfd, abi_fp_bfd, ibfd,
14515 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msoft-float");
14516 break;
14517
14518 case Val_GNU_MIPS_ABI_FP_64:
14519 _bfd_error_handler
14520 (_("Warning: %B uses unknown floating point ABI %d "
14521 "(set by %B), %B uses %s"),
14522 obfd, abi_fp_bfd, ibfd,
14523 out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mips32r2 -mfp64");
14524 break;
14525
14526 default:
14527 _bfd_error_handler
14528 (_("Warning: %B uses unknown floating point ABI %d "
14529 "(set by %B), %B uses unknown floating point ABI %d"),
14530 obfd, abi_fp_bfd, ibfd,
14531 out_attr[Tag_GNU_MIPS_ABI_FP].i,
14532 in_attr[Tag_GNU_MIPS_ABI_FP].i);
14533 break;
14534 }
14535 break;
14536 }
14537 }
14538
14539 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
14540 non-conflicting ones. */
14541 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14542 {
14543 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
14544 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
14545 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
14546 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
14547 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
14548 {
14549 case Val_GNU_MIPS_ABI_MSA_128:
14550 _bfd_error_handler
14551 (_("Warning: %B uses %s (set by %B), "
14552 "%B uses unknown MSA ABI %d"),
14553 obfd, abi_msa_bfd, ibfd,
14554 "-mmsa", in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14555 break;
14556
14557 default:
14558 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
14559 {
14560 case Val_GNU_MIPS_ABI_MSA_128:
14561 _bfd_error_handler
14562 (_("Warning: %B uses unknown MSA ABI %d "
14563 "(set by %B), %B uses %s"),
14564 obfd, abi_msa_bfd, ibfd,
14565 out_attr[Tag_GNU_MIPS_ABI_MSA].i, "-mmsa");
14566 break;
14567
14568 default:
14569 _bfd_error_handler
14570 (_("Warning: %B uses unknown MSA ABI %d "
14571 "(set by %B), %B uses unknown MSA ABI %d"),
14572 obfd, abi_msa_bfd, ibfd,
14573 out_attr[Tag_GNU_MIPS_ABI_MSA].i,
14574 in_attr[Tag_GNU_MIPS_ABI_MSA].i);
14575 break;
14576 }
14577 }
14578 }
14579
14580 /* Merge Tag_compatibility attributes and any common GNU ones. */
14581 _bfd_elf_merge_object_attributes (ibfd, obfd);
14582
14583 return TRUE;
14584 }
14585
14586 /* Merge backend specific data from an object file to the output
14587 object file when linking. */
14588
14589 bfd_boolean
14590 _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
14591 {
14592 flagword old_flags;
14593 flagword new_flags;
14594 bfd_boolean ok;
14595 bfd_boolean null_input_bfd = TRUE;
14596 asection *sec;
14597
14598 /* Check if we have the same endianness. */
14599 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
14600 {
14601 (*_bfd_error_handler)
14602 (_("%B: endianness incompatible with that of the selected emulation"),
14603 ibfd);
14604 return FALSE;
14605 }
14606
14607 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
14608 return TRUE;
14609
14610 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
14611 {
14612 (*_bfd_error_handler)
14613 (_("%B: ABI is incompatible with that of the selected emulation"),
14614 ibfd);
14615 return FALSE;
14616 }
14617
14618 if (!mips_elf_merge_obj_attributes (ibfd, obfd))
14619 return FALSE;
14620
14621 new_flags = elf_elfheader (ibfd)->e_flags;
14622 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
14623 old_flags = elf_elfheader (obfd)->e_flags;
14624
14625 if (! elf_flags_init (obfd))
14626 {
14627 elf_flags_init (obfd) = TRUE;
14628 elf_elfheader (obfd)->e_flags = new_flags;
14629 elf_elfheader (obfd)->e_ident[EI_CLASS]
14630 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
14631
14632 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
14633 && (bfd_get_arch_info (obfd)->the_default
14634 || mips_mach_extends_p (bfd_get_mach (obfd),
14635 bfd_get_mach (ibfd))))
14636 {
14637 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
14638 bfd_get_mach (ibfd)))
14639 return FALSE;
14640 }
14641
14642 return TRUE;
14643 }
14644
14645 /* Check flag compatibility. */
14646
14647 new_flags &= ~EF_MIPS_NOREORDER;
14648 old_flags &= ~EF_MIPS_NOREORDER;
14649
14650 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
14651 doesn't seem to matter. */
14652 new_flags &= ~EF_MIPS_XGOT;
14653 old_flags &= ~EF_MIPS_XGOT;
14654
14655 /* MIPSpro generates ucode info in n64 objects. Again, we should
14656 just be able to ignore this. */
14657 new_flags &= ~EF_MIPS_UCODE;
14658 old_flags &= ~EF_MIPS_UCODE;
14659
14660 /* DSOs should only be linked with CPIC code. */
14661 if ((ibfd->flags & DYNAMIC) != 0)
14662 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
14663
14664 if (new_flags == old_flags)
14665 return TRUE;
14666
14667 /* Check to see if the input BFD actually contains any sections.
14668 If not, its flags may not have been initialised either, but it cannot
14669 actually cause any incompatibility. */
14670 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
14671 {
14672 /* Ignore synthetic sections and empty .text, .data and .bss sections
14673 which are automatically generated by gas. Also ignore fake
14674 (s)common sections, since merely defining a common symbol does
14675 not affect compatibility. */
14676 if ((sec->flags & SEC_IS_COMMON) == 0
14677 && strcmp (sec->name, ".reginfo")
14678 && strcmp (sec->name, ".mdebug")
14679 && (sec->size != 0
14680 || (strcmp (sec->name, ".text")
14681 && strcmp (sec->name, ".data")
14682 && strcmp (sec->name, ".bss"))))
14683 {
14684 null_input_bfd = FALSE;
14685 break;
14686 }
14687 }
14688 if (null_input_bfd)
14689 return TRUE;
14690
14691 ok = TRUE;
14692
14693 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
14694 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
14695 {
14696 (*_bfd_error_handler)
14697 (_("%B: warning: linking abicalls files with non-abicalls files"),
14698 ibfd);
14699 ok = TRUE;
14700 }
14701
14702 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
14703 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
14704 if (! (new_flags & EF_MIPS_PIC))
14705 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
14706
14707 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
14708 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
14709
14710 /* Compare the ISAs. */
14711 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
14712 {
14713 (*_bfd_error_handler)
14714 (_("%B: linking 32-bit code with 64-bit code"),
14715 ibfd);
14716 ok = FALSE;
14717 }
14718 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
14719 {
14720 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
14721 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
14722 {
14723 /* Copy the architecture info from IBFD to OBFD. Also copy
14724 the 32-bit flag (if set) so that we continue to recognise
14725 OBFD as a 32-bit binary. */
14726 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
14727 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
14728 elf_elfheader (obfd)->e_flags
14729 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14730
14731 /* Copy across the ABI flags if OBFD doesn't use them
14732 and if that was what caused us to treat IBFD as 32-bit. */
14733 if ((old_flags & EF_MIPS_ABI) == 0
14734 && mips_32bit_flags_p (new_flags)
14735 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
14736 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
14737 }
14738 else
14739 {
14740 /* The ISAs aren't compatible. */
14741 (*_bfd_error_handler)
14742 (_("%B: linking %s module with previous %s modules"),
14743 ibfd,
14744 bfd_printable_name (ibfd),
14745 bfd_printable_name (obfd));
14746 ok = FALSE;
14747 }
14748 }
14749
14750 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14751 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
14752
14753 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
14754 does set EI_CLASS differently from any 32-bit ABI. */
14755 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
14756 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14757 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14758 {
14759 /* Only error if both are set (to different values). */
14760 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
14761 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
14762 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
14763 {
14764 (*_bfd_error_handler)
14765 (_("%B: ABI mismatch: linking %s module with previous %s modules"),
14766 ibfd,
14767 elf_mips_abi_name (ibfd),
14768 elf_mips_abi_name (obfd));
14769 ok = FALSE;
14770 }
14771 new_flags &= ~EF_MIPS_ABI;
14772 old_flags &= ~EF_MIPS_ABI;
14773 }
14774
14775 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
14776 and allow arbitrary mixing of the remaining ASEs (retain the union). */
14777 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
14778 {
14779 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14780 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
14781 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
14782 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
14783 int micro_mis = old_m16 && new_micro;
14784 int m16_mis = old_micro && new_m16;
14785
14786 if (m16_mis || micro_mis)
14787 {
14788 (*_bfd_error_handler)
14789 (_("%B: ASE mismatch: linking %s module with previous %s modules"),
14790 ibfd,
14791 m16_mis ? "MIPS16" : "microMIPS",
14792 m16_mis ? "microMIPS" : "MIPS16");
14793 ok = FALSE;
14794 }
14795
14796 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
14797
14798 new_flags &= ~ EF_MIPS_ARCH_ASE;
14799 old_flags &= ~ EF_MIPS_ARCH_ASE;
14800 }
14801
14802 /* Compare NaN encodings. */
14803 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
14804 {
14805 _bfd_error_handler (_("%B: linking %s module with previous %s modules"),
14806 ibfd,
14807 (new_flags & EF_MIPS_NAN2008
14808 ? "-mnan=2008" : "-mnan=legacy"),
14809 (old_flags & EF_MIPS_NAN2008
14810 ? "-mnan=2008" : "-mnan=legacy"));
14811 ok = FALSE;
14812 new_flags &= ~EF_MIPS_NAN2008;
14813 old_flags &= ~EF_MIPS_NAN2008;
14814 }
14815
14816 /* Warn about any other mismatches */
14817 if (new_flags != old_flags)
14818 {
14819 (*_bfd_error_handler)
14820 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
14821 ibfd, (unsigned long) new_flags,
14822 (unsigned long) old_flags);
14823 ok = FALSE;
14824 }
14825
14826 if (! ok)
14827 {
14828 bfd_set_error (bfd_error_bad_value);
14829 return FALSE;
14830 }
14831
14832 return TRUE;
14833 }
14834
14835 /* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
14836
14837 bfd_boolean
14838 _bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
14839 {
14840 BFD_ASSERT (!elf_flags_init (abfd)
14841 || elf_elfheader (abfd)->e_flags == flags);
14842
14843 elf_elfheader (abfd)->e_flags = flags;
14844 elf_flags_init (abfd) = TRUE;
14845 return TRUE;
14846 }
14847
14848 char *
14849 _bfd_mips_elf_get_target_dtag (bfd_vma dtag)
14850 {
14851 switch (dtag)
14852 {
14853 default: return "";
14854 case DT_MIPS_RLD_VERSION:
14855 return "MIPS_RLD_VERSION";
14856 case DT_MIPS_TIME_STAMP:
14857 return "MIPS_TIME_STAMP";
14858 case DT_MIPS_ICHECKSUM:
14859 return "MIPS_ICHECKSUM";
14860 case DT_MIPS_IVERSION:
14861 return "MIPS_IVERSION";
14862 case DT_MIPS_FLAGS:
14863 return "MIPS_FLAGS";
14864 case DT_MIPS_BASE_ADDRESS:
14865 return "MIPS_BASE_ADDRESS";
14866 case DT_MIPS_MSYM:
14867 return "MIPS_MSYM";
14868 case DT_MIPS_CONFLICT:
14869 return "MIPS_CONFLICT";
14870 case DT_MIPS_LIBLIST:
14871 return "MIPS_LIBLIST";
14872 case DT_MIPS_LOCAL_GOTNO:
14873 return "MIPS_LOCAL_GOTNO";
14874 case DT_MIPS_CONFLICTNO:
14875 return "MIPS_CONFLICTNO";
14876 case DT_MIPS_LIBLISTNO:
14877 return "MIPS_LIBLISTNO";
14878 case DT_MIPS_SYMTABNO:
14879 return "MIPS_SYMTABNO";
14880 case DT_MIPS_UNREFEXTNO:
14881 return "MIPS_UNREFEXTNO";
14882 case DT_MIPS_GOTSYM:
14883 return "MIPS_GOTSYM";
14884 case DT_MIPS_HIPAGENO:
14885 return "MIPS_HIPAGENO";
14886 case DT_MIPS_RLD_MAP:
14887 return "MIPS_RLD_MAP";
14888 case DT_MIPS_DELTA_CLASS:
14889 return "MIPS_DELTA_CLASS";
14890 case DT_MIPS_DELTA_CLASS_NO:
14891 return "MIPS_DELTA_CLASS_NO";
14892 case DT_MIPS_DELTA_INSTANCE:
14893 return "MIPS_DELTA_INSTANCE";
14894 case DT_MIPS_DELTA_INSTANCE_NO:
14895 return "MIPS_DELTA_INSTANCE_NO";
14896 case DT_MIPS_DELTA_RELOC:
14897 return "MIPS_DELTA_RELOC";
14898 case DT_MIPS_DELTA_RELOC_NO:
14899 return "MIPS_DELTA_RELOC_NO";
14900 case DT_MIPS_DELTA_SYM:
14901 return "MIPS_DELTA_SYM";
14902 case DT_MIPS_DELTA_SYM_NO:
14903 return "MIPS_DELTA_SYM_NO";
14904 case DT_MIPS_DELTA_CLASSSYM:
14905 return "MIPS_DELTA_CLASSSYM";
14906 case DT_MIPS_DELTA_CLASSSYM_NO:
14907 return "MIPS_DELTA_CLASSSYM_NO";
14908 case DT_MIPS_CXX_FLAGS:
14909 return "MIPS_CXX_FLAGS";
14910 case DT_MIPS_PIXIE_INIT:
14911 return "MIPS_PIXIE_INIT";
14912 case DT_MIPS_SYMBOL_LIB:
14913 return "MIPS_SYMBOL_LIB";
14914 case DT_MIPS_LOCALPAGE_GOTIDX:
14915 return "MIPS_LOCALPAGE_GOTIDX";
14916 case DT_MIPS_LOCAL_GOTIDX:
14917 return "MIPS_LOCAL_GOTIDX";
14918 case DT_MIPS_HIDDEN_GOTIDX:
14919 return "MIPS_HIDDEN_GOTIDX";
14920 case DT_MIPS_PROTECTED_GOTIDX:
14921 return "MIPS_PROTECTED_GOT_IDX";
14922 case DT_MIPS_OPTIONS:
14923 return "MIPS_OPTIONS";
14924 case DT_MIPS_INTERFACE:
14925 return "MIPS_INTERFACE";
14926 case DT_MIPS_DYNSTR_ALIGN:
14927 return "DT_MIPS_DYNSTR_ALIGN";
14928 case DT_MIPS_INTERFACE_SIZE:
14929 return "DT_MIPS_INTERFACE_SIZE";
14930 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
14931 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
14932 case DT_MIPS_PERF_SUFFIX:
14933 return "DT_MIPS_PERF_SUFFIX";
14934 case DT_MIPS_COMPACT_SIZE:
14935 return "DT_MIPS_COMPACT_SIZE";
14936 case DT_MIPS_GP_VALUE:
14937 return "DT_MIPS_GP_VALUE";
14938 case DT_MIPS_AUX_DYNAMIC:
14939 return "DT_MIPS_AUX_DYNAMIC";
14940 case DT_MIPS_PLTGOT:
14941 return "DT_MIPS_PLTGOT";
14942 case DT_MIPS_RWPLT:
14943 return "DT_MIPS_RWPLT";
14944 }
14945 }
14946
14947 bfd_boolean
14948 _bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
14949 {
14950 FILE *file = ptr;
14951
14952 BFD_ASSERT (abfd != NULL && ptr != NULL);
14953
14954 /* Print normal ELF private data. */
14955 _bfd_elf_print_private_bfd_data (abfd, ptr);
14956
14957 /* xgettext:c-format */
14958 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
14959
14960 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
14961 fprintf (file, _(" [abi=O32]"));
14962 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
14963 fprintf (file, _(" [abi=O64]"));
14964 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
14965 fprintf (file, _(" [abi=EABI32]"));
14966 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
14967 fprintf (file, _(" [abi=EABI64]"));
14968 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
14969 fprintf (file, _(" [abi unknown]"));
14970 else if (ABI_N32_P (abfd))
14971 fprintf (file, _(" [abi=N32]"));
14972 else if (ABI_64_P (abfd))
14973 fprintf (file, _(" [abi=64]"));
14974 else
14975 fprintf (file, _(" [no abi set]"));
14976
14977 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
14978 fprintf (file, " [mips1]");
14979 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
14980 fprintf (file, " [mips2]");
14981 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
14982 fprintf (file, " [mips3]");
14983 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
14984 fprintf (file, " [mips4]");
14985 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
14986 fprintf (file, " [mips5]");
14987 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
14988 fprintf (file, " [mips32]");
14989 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
14990 fprintf (file, " [mips64]");
14991 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
14992 fprintf (file, " [mips32r2]");
14993 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
14994 fprintf (file, " [mips64r2]");
14995 else
14996 fprintf (file, _(" [unknown ISA]"));
14997
14998 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14999 fprintf (file, " [mdmx]");
15000
15001 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
15002 fprintf (file, " [mips16]");
15003
15004 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
15005 fprintf (file, " [micromips]");
15006
15007 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
15008 fprintf (file, " [nan2008]");
15009
15010 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
15011 fprintf (file, " [fp64]");
15012
15013 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
15014 fprintf (file, " [32bitmode]");
15015 else
15016 fprintf (file, _(" [not 32bitmode]"));
15017
15018 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
15019 fprintf (file, " [noreorder]");
15020
15021 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
15022 fprintf (file, " [PIC]");
15023
15024 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
15025 fprintf (file, " [CPIC]");
15026
15027 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
15028 fprintf (file, " [XGOT]");
15029
15030 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
15031 fprintf (file, " [UCODE]");
15032
15033 fputc ('\n', file);
15034
15035 return TRUE;
15036 }
15037
15038 const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
15039 {
15040 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15041 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15042 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
15043 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15044 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
15045 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
15046 { NULL, 0, 0, 0, 0 }
15047 };
15048
15049 /* Merge non visibility st_other attributes. Ensure that the
15050 STO_OPTIONAL flag is copied into h->other, even if this is not a
15051 definiton of the symbol. */
15052 void
15053 _bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
15054 const Elf_Internal_Sym *isym,
15055 bfd_boolean definition,
15056 bfd_boolean dynamic ATTRIBUTE_UNUSED)
15057 {
15058 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
15059 {
15060 unsigned char other;
15061
15062 other = (definition ? isym->st_other : h->other);
15063 other &= ~ELF_ST_VISIBILITY (-1);
15064 h->other = other | ELF_ST_VISIBILITY (h->other);
15065 }
15066
15067 if (!definition
15068 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
15069 h->other |= STO_OPTIONAL;
15070 }
15071
15072 /* Decide whether an undefined symbol is special and can be ignored.
15073 This is the case for OPTIONAL symbols on IRIX. */
15074 bfd_boolean
15075 _bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
15076 {
15077 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
15078 }
15079
15080 bfd_boolean
15081 _bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
15082 {
15083 return (sym->st_shndx == SHN_COMMON
15084 || sym->st_shndx == SHN_MIPS_ACOMMON
15085 || sym->st_shndx == SHN_MIPS_SCOMMON);
15086 }
15087
15088 /* Return address for Ith PLT stub in section PLT, for relocation REL
15089 or (bfd_vma) -1 if it should not be included. */
15090
15091 bfd_vma
15092 _bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
15093 const arelent *rel ATTRIBUTE_UNUSED)
15094 {
15095 return (plt->vma
15096 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
15097 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
15098 }
15099
15100 /* Build a table of synthetic symbols to represent the PLT. As with MIPS16
15101 and microMIPS PLT slots we may have a many-to-one mapping between .plt
15102 and .got.plt and also the slots may be of a different size each we walk
15103 the PLT manually fetching instructions and matching them against known
15104 patterns. To make things easier standard MIPS slots, if any, always come
15105 first. As we don't create proper ELF symbols we use the UDATA.I member
15106 of ASYMBOL to carry ISA annotation. The encoding used is the same as
15107 with the ST_OTHER member of the ELF symbol. */
15108
15109 long
15110 _bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
15111 long symcount ATTRIBUTE_UNUSED,
15112 asymbol **syms ATTRIBUTE_UNUSED,
15113 long dynsymcount, asymbol **dynsyms,
15114 asymbol **ret)
15115 {
15116 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
15117 static const char microsuffix[] = "@micromipsplt";
15118 static const char m16suffix[] = "@mips16plt";
15119 static const char mipssuffix[] = "@plt";
15120
15121 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
15122 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
15123 bfd_boolean micromips_p = MICROMIPS_P (abfd);
15124 Elf_Internal_Shdr *hdr;
15125 bfd_byte *plt_data;
15126 bfd_vma plt_offset;
15127 unsigned int other;
15128 bfd_vma entry_size;
15129 bfd_vma plt0_size;
15130 asection *relplt;
15131 bfd_vma opcode;
15132 asection *plt;
15133 asymbol *send;
15134 size_t size;
15135 char *names;
15136 long counti;
15137 arelent *p;
15138 asymbol *s;
15139 char *nend;
15140 long count;
15141 long pi;
15142 long i;
15143 long n;
15144
15145 *ret = NULL;
15146
15147 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
15148 return 0;
15149
15150 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
15151 if (relplt == NULL)
15152 return 0;
15153
15154 hdr = &elf_section_data (relplt)->this_hdr;
15155 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
15156 return 0;
15157
15158 plt = bfd_get_section_by_name (abfd, ".plt");
15159 if (plt == NULL)
15160 return 0;
15161
15162 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
15163 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
15164 return -1;
15165 p = relplt->relocation;
15166
15167 /* Calculating the exact amount of space required for symbols would
15168 require two passes over the PLT, so just pessimise assuming two
15169 PLT slots per relocation. */
15170 count = relplt->size / hdr->sh_entsize;
15171 counti = count * bed->s->int_rels_per_ext_rel;
15172 size = 2 * count * sizeof (asymbol);
15173 size += count * (sizeof (mipssuffix) +
15174 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
15175 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
15176 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
15177
15178 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
15179 size += sizeof (asymbol) + sizeof (pltname);
15180
15181 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
15182 return -1;
15183
15184 if (plt->size < 16)
15185 return -1;
15186
15187 s = *ret = bfd_malloc (size);
15188 if (s == NULL)
15189 return -1;
15190 send = s + 2 * count + 1;
15191
15192 names = (char *) send;
15193 nend = (char *) s + size;
15194 n = 0;
15195
15196 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
15197 if (opcode == 0x3302fffe)
15198 {
15199 if (!micromips_p)
15200 return -1;
15201 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
15202 other = STO_MICROMIPS;
15203 }
15204 else if (opcode == 0x0398c1d0)
15205 {
15206 if (!micromips_p)
15207 return -1;
15208 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
15209 other = STO_MICROMIPS;
15210 }
15211 else
15212 {
15213 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
15214 other = 0;
15215 }
15216
15217 s->the_bfd = abfd;
15218 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
15219 s->section = plt;
15220 s->value = 0;
15221 s->name = names;
15222 s->udata.i = other;
15223 memcpy (names, pltname, sizeof (pltname));
15224 names += sizeof (pltname);
15225 ++s, ++n;
15226
15227 pi = 0;
15228 for (plt_offset = plt0_size;
15229 plt_offset + 8 <= plt->size && s < send;
15230 plt_offset += entry_size)
15231 {
15232 bfd_vma gotplt_addr;
15233 const char *suffix;
15234 bfd_vma gotplt_hi;
15235 bfd_vma gotplt_lo;
15236 size_t suffixlen;
15237
15238 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
15239
15240 /* Check if the second word matches the expected MIPS16 instruction. */
15241 if (opcode == 0x651aeb00)
15242 {
15243 if (micromips_p)
15244 return -1;
15245 /* Truncated table??? */
15246 if (plt_offset + 16 > plt->size)
15247 break;
15248 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
15249 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
15250 suffixlen = sizeof (m16suffix);
15251 suffix = m16suffix;
15252 other = STO_MIPS16;
15253 }
15254 /* Likewise the expected microMIPS instruction (no insn32 mode). */
15255 else if (opcode == 0xff220000)
15256 {
15257 if (!micromips_p)
15258 return -1;
15259 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
15260 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15261 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
15262 gotplt_lo <<= 2;
15263 gotplt_addr = gotplt_hi + gotplt_lo;
15264 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
15265 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
15266 suffixlen = sizeof (microsuffix);
15267 suffix = microsuffix;
15268 other = STO_MICROMIPS;
15269 }
15270 /* Likewise the expected microMIPS instruction (insn32 mode). */
15271 else if ((opcode & 0xffff0000) == 0xff2f0000)
15272 {
15273 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
15274 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
15275 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15276 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15277 gotplt_addr = gotplt_hi + gotplt_lo;
15278 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
15279 suffixlen = sizeof (microsuffix);
15280 suffix = microsuffix;
15281 other = STO_MICROMIPS;
15282 }
15283 /* Otherwise assume standard MIPS code. */
15284 else
15285 {
15286 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
15287 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
15288 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
15289 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
15290 gotplt_addr = gotplt_hi + gotplt_lo;
15291 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
15292 suffixlen = sizeof (mipssuffix);
15293 suffix = mipssuffix;
15294 other = 0;
15295 }
15296 /* Truncated table??? */
15297 if (plt_offset + entry_size > plt->size)
15298 break;
15299
15300 for (i = 0;
15301 i < count && p[pi].address != gotplt_addr;
15302 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
15303
15304 if (i < count)
15305 {
15306 size_t namelen;
15307 size_t len;
15308
15309 *s = **p[pi].sym_ptr_ptr;
15310 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
15311 we are defining a symbol, ensure one of them is set. */
15312 if ((s->flags & BSF_LOCAL) == 0)
15313 s->flags |= BSF_GLOBAL;
15314 s->flags |= BSF_SYNTHETIC;
15315 s->section = plt;
15316 s->value = plt_offset;
15317 s->name = names;
15318 s->udata.i = other;
15319
15320 len = strlen ((*p[pi].sym_ptr_ptr)->name);
15321 namelen = len + suffixlen;
15322 if (names + namelen > nend)
15323 break;
15324
15325 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
15326 names += len;
15327 memcpy (names, suffix, suffixlen);
15328 names += suffixlen;
15329
15330 ++s, ++n;
15331 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
15332 }
15333 }
15334
15335 free (plt_data);
15336
15337 return n;
15338 }
15339
15340 void
15341 _bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
15342 {
15343 struct mips_elf_link_hash_table *htab;
15344 Elf_Internal_Ehdr *i_ehdrp;
15345
15346 i_ehdrp = elf_elfheader (abfd);
15347 if (link_info)
15348 {
15349 htab = mips_elf_hash_table (link_info);
15350 BFD_ASSERT (htab != NULL);
15351
15352 if (htab->use_plts_and_copy_relocs && !htab->is_vxworks)
15353 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
15354 }
15355
15356 _bfd_elf_post_process_headers (abfd, link_info);
15357 }