LD: Convert `%P: %H:' to `%H:' in error messages
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
219d1afa 2 Copyright (C) 1993-2018 Free Software Foundation, Inc.
b49e97c9
TS
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
ae9a127f 11 This file is part of BFD, the Binary File Descriptor library.
b49e97c9 12
ae9a127f
NC
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
cd123cb7 15 the Free Software Foundation; either version 3 of the License, or
ae9a127f 16 (at your option) any later version.
b49e97c9 17
ae9a127f
NC
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.
b49e97c9 22
ae9a127f
NC
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
cd123cb7
NC
25 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
26 MA 02110-1301, USA. */
27
b49e97c9
TS
28
29/* This file handles functionality common to the different MIPS ABI's. */
30
b49e97c9 31#include "sysdep.h"
3db64b00 32#include "bfd.h"
b49e97c9 33#include "libbfd.h"
64543e1a 34#include "libiberty.h"
b49e97c9
TS
35#include "elf-bfd.h"
36#include "elfxx-mips.h"
37#include "elf/mips.h"
0a44bf69 38#include "elf-vxworks.h"
2f0c68f2 39#include "dwarf2.h"
b49e97c9
TS
40
41/* Get the ECOFF swapping routines. */
42#include "coff/sym.h"
43#include "coff/symconst.h"
44#include "coff/ecoff.h"
45#include "coff/mips.h"
46
b15e6682
AO
47#include "hashtab.h"
48
9ab066b4
RS
49/* Types of TLS GOT entry. */
50enum mips_got_tls_type {
51 GOT_TLS_NONE,
52 GOT_TLS_GD,
53 GOT_TLS_LDM,
54 GOT_TLS_IE
55};
56
ead49a57 57/* This structure is used to hold information about one GOT entry.
3dff0dd1
RS
58 There are four types of entry:
59
60 (1) an absolute address
61 requires: abfd == NULL
62 fields: d.address
63
64 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
65 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
66 fields: abfd, symndx, d.addend, tls_type
67
68 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
69 requires: abfd != NULL, symndx == -1
70 fields: d.h, tls_type
71
72 (4) a TLS LDM slot
73 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
74 fields: none; there's only one of these per GOT. */
b15e6682
AO
75struct mips_got_entry
76{
3dff0dd1 77 /* One input bfd that needs the GOT entry. */
b15e6682 78 bfd *abfd;
f4416af6
AO
79 /* The index of the symbol, as stored in the relocation r_info, if
80 we have a local symbol; -1 otherwise. */
81 long symndx;
82 union
83 {
84 /* If abfd == NULL, an address that must be stored in the got. */
85 bfd_vma address;
86 /* If abfd != NULL && symndx != -1, the addend of the relocation
87 that should be added to the symbol value. */
88 bfd_vma addend;
89 /* If abfd != NULL && symndx == -1, the hash table entry
3dff0dd1 90 corresponding to a symbol in the GOT. The symbol's entry
020d7251
RS
91 is in the local area if h->global_got_area is GGA_NONE,
92 otherwise it is in the global area. */
f4416af6
AO
93 struct mips_elf_link_hash_entry *h;
94 } d;
0f20cc35 95
9ab066b4
RS
96 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
97 symbol entry with r_symndx == 0. */
0f20cc35
DJ
98 unsigned char tls_type;
99
9ab066b4
RS
100 /* True if we have filled in the GOT contents for a TLS entry,
101 and created the associated relocations. */
102 unsigned char tls_initialized;
103
b15e6682 104 /* The offset from the beginning of the .got section to the entry
f4416af6
AO
105 corresponding to this symbol+addend. If it's a global symbol
106 whose offset is yet to be decided, it's going to be -1. */
107 long gotidx;
b15e6682
AO
108};
109
13db6b44
RS
110/* This structure represents a GOT page reference from an input bfd.
111 Each instance represents a symbol + ADDEND, where the representation
112 of the symbol depends on whether it is local to the input bfd.
113 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
114 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
115
116 Page references with SYMNDX >= 0 always become page references
117 in the output. Page references with SYMNDX < 0 only become page
118 references if the symbol binds locally; in other cases, the page
119 reference decays to a global GOT reference. */
120struct mips_got_page_ref
121{
122 long symndx;
123 union
124 {
125 struct mips_elf_link_hash_entry *h;
126 bfd *abfd;
127 } u;
128 bfd_vma addend;
129};
130
c224138d
RS
131/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
132 The structures form a non-overlapping list that is sorted by increasing
133 MIN_ADDEND. */
134struct mips_got_page_range
135{
136 struct mips_got_page_range *next;
137 bfd_signed_vma min_addend;
138 bfd_signed_vma max_addend;
139};
140
141/* This structure describes the range of addends that are applied to page
13db6b44 142 relocations against a given section. */
c224138d
RS
143struct mips_got_page_entry
144{
13db6b44
RS
145 /* The section that these entries are based on. */
146 asection *sec;
c224138d
RS
147 /* The ranges for this page entry. */
148 struct mips_got_page_range *ranges;
149 /* The maximum number of page entries needed for RANGES. */
150 bfd_vma num_pages;
151};
152
f0abc2a1 153/* This structure is used to hold .got information when linking. */
b49e97c9
TS
154
155struct mips_got_info
156{
b49e97c9
TS
157 /* The number of global .got entries. */
158 unsigned int global_gotno;
23cc69b6
RS
159 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
160 unsigned int reloc_only_gotno;
0f20cc35
DJ
161 /* The number of .got slots used for TLS. */
162 unsigned int tls_gotno;
163 /* The first unused TLS .got entry. Used only during
164 mips_elf_initialize_tls_index. */
165 unsigned int tls_assigned_gotno;
c224138d 166 /* The number of local .got entries, eventually including page entries. */
b49e97c9 167 unsigned int local_gotno;
c224138d
RS
168 /* The maximum number of page entries needed. */
169 unsigned int page_gotno;
ab361d49
RS
170 /* The number of relocations needed for the GOT entries. */
171 unsigned int relocs;
cb22ccf4
KCY
172 /* The first unused local .got entry. */
173 unsigned int assigned_low_gotno;
174 /* The last unused local .got entry. */
175 unsigned int assigned_high_gotno;
b15e6682
AO
176 /* A hash table holding members of the got. */
177 struct htab *got_entries;
13db6b44
RS
178 /* A hash table holding mips_got_page_ref structures. */
179 struct htab *got_page_refs;
c224138d
RS
180 /* A hash table of mips_got_page_entry structures. */
181 struct htab *got_page_entries;
f4416af6
AO
182 /* In multi-got links, a pointer to the next got (err, rather, most
183 of the time, it points to the previous got). */
184 struct mips_got_info *next;
185};
186
d7206569 187/* Structure passed when merging bfds' gots. */
f4416af6
AO
188
189struct mips_elf_got_per_bfd_arg
190{
f4416af6
AO
191 /* The output bfd. */
192 bfd *obfd;
193 /* The link information. */
194 struct bfd_link_info *info;
195 /* A pointer to the primary got, i.e., the one that's going to get
196 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
197 DT_MIPS_GOTSYM. */
198 struct mips_got_info *primary;
199 /* A non-primary got we're trying to merge with other input bfd's
200 gots. */
201 struct mips_got_info *current;
202 /* The maximum number of got entries that can be addressed with a
203 16-bit offset. */
204 unsigned int max_count;
c224138d
RS
205 /* The maximum number of page entries needed by each got. */
206 unsigned int max_pages;
0f20cc35
DJ
207 /* The total number of global entries which will live in the
208 primary got and be automatically relocated. This includes
209 those not referenced by the primary GOT but included in
210 the "master" GOT. */
211 unsigned int global_count;
f4416af6
AO
212};
213
ab361d49
RS
214/* A structure used to pass information to htab_traverse callbacks
215 when laying out the GOT. */
f4416af6 216
ab361d49 217struct mips_elf_traverse_got_arg
f4416af6 218{
ab361d49 219 struct bfd_link_info *info;
f4416af6
AO
220 struct mips_got_info *g;
221 int value;
0f20cc35
DJ
222};
223
f0abc2a1
AM
224struct _mips_elf_section_data
225{
226 struct bfd_elf_section_data elf;
227 union
228 {
f0abc2a1
AM
229 bfd_byte *tdata;
230 } u;
231};
232
233#define mips_elf_section_data(sec) \
68bfbfcc 234 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 235
d5eaccd7
RS
236#define is_mips_elf(bfd) \
237 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
238 && elf_tdata (bfd) != NULL \
4dfe6ac6 239 && elf_object_id (bfd) == MIPS_ELF_DATA)
d5eaccd7 240
634835ae
RS
241/* The ABI says that every symbol used by dynamic relocations must have
242 a global GOT entry. Among other things, this provides the dynamic
243 linker with a free, directly-indexed cache. The GOT can therefore
244 contain symbols that are not referenced by GOT relocations themselves
245 (in other words, it may have symbols that are not referenced by things
246 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
247
248 GOT relocations are less likely to overflow if we put the associated
249 GOT entries towards the beginning. We therefore divide the global
250 GOT entries into two areas: "normal" and "reloc-only". Entries in
251 the first area can be used for both dynamic relocations and GP-relative
252 accesses, while those in the "reloc-only" area are for dynamic
253 relocations only.
254
255 These GGA_* ("Global GOT Area") values are organised so that lower
256 values are more general than higher values. Also, non-GGA_NONE
257 values are ordered by the position of the area in the GOT. */
258#define GGA_NORMAL 0
259#define GGA_RELOC_ONLY 1
260#define GGA_NONE 2
261
861fb55a
DJ
262/* Information about a non-PIC interface to a PIC function. There are
263 two ways of creating these interfaces. The first is to add:
264
265 lui $25,%hi(func)
266 addiu $25,$25,%lo(func)
267
268 immediately before a PIC function "func". The second is to add:
269
270 lui $25,%hi(func)
271 j func
272 addiu $25,$25,%lo(func)
273
274 to a separate trampoline section.
275
276 Stubs of the first kind go in a new section immediately before the
277 target function. Stubs of the second kind go in a single section
278 pointed to by the hash table's "strampoline" field. */
279struct mips_elf_la25_stub {
280 /* The generated section that contains this stub. */
281 asection *stub_section;
282
283 /* The offset of the stub from the start of STUB_SECTION. */
284 bfd_vma offset;
285
286 /* One symbol for the original function. Its location is available
287 in H->root.root.u.def. */
288 struct mips_elf_link_hash_entry *h;
289};
290
291/* Macros for populating a mips_elf_la25_stub. */
292
293#define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
294#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
295#define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
d21911ea
MR
296#define LA25_LUI_MICROMIPS(VAL) \
297 (0x41b90000 | (VAL)) /* lui t9,VAL */
298#define LA25_J_MICROMIPS(VAL) \
299 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
300#define LA25_ADDIU_MICROMIPS(VAL) \
301 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
861fb55a 302
b49e97c9
TS
303/* This structure is passed to mips_elf_sort_hash_table_f when sorting
304 the dynamic symbols. */
305
306struct mips_elf_hash_sort_data
307{
308 /* The symbol in the global GOT with the lowest dynamic symbol table
309 index. */
310 struct elf_link_hash_entry *low;
0f20cc35
DJ
311 /* The least dynamic symbol table index corresponding to a non-TLS
312 symbol with a GOT entry. */
55f8b9d2 313 bfd_size_type min_got_dynindx;
f4416af6
AO
314 /* The greatest dynamic symbol table index corresponding to a symbol
315 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 316 with dynamic relocations pointing to it from non-primary GOTs). */
55f8b9d2 317 bfd_size_type max_unref_got_dynindx;
e17b0c35
MR
318 /* The greatest dynamic symbol table index corresponding to a local
319 symbol. */
320 bfd_size_type max_local_dynindx;
321 /* The greatest dynamic symbol table index corresponding to an external
b49e97c9 322 symbol without a GOT entry. */
55f8b9d2 323 bfd_size_type max_non_got_dynindx;
b49e97c9
TS
324};
325
1bbce132
MR
326/* We make up to two PLT entries if needed, one for standard MIPS code
327 and one for compressed code, either a MIPS16 or microMIPS one. We
328 keep a separate record of traditional lazy-binding stubs, for easier
329 processing. */
330
331struct plt_entry
332{
333 /* Traditional SVR4 stub offset, or -1 if none. */
334 bfd_vma stub_offset;
335
336 /* Standard PLT entry offset, or -1 if none. */
337 bfd_vma mips_offset;
338
339 /* Compressed PLT entry offset, or -1 if none. */
340 bfd_vma comp_offset;
341
342 /* The corresponding .got.plt index, or -1 if none. */
343 bfd_vma gotplt_index;
344
345 /* Whether we need a standard PLT entry. */
346 unsigned int need_mips : 1;
347
348 /* Whether we need a compressed PLT entry. */
349 unsigned int need_comp : 1;
350};
351
b49e97c9
TS
352/* The MIPS ELF linker needs additional information for each symbol in
353 the global hash table. */
354
355struct mips_elf_link_hash_entry
356{
357 struct elf_link_hash_entry root;
358
359 /* External symbol information. */
360 EXTR esym;
361
861fb55a
DJ
362 /* The la25 stub we have created for ths symbol, if any. */
363 struct mips_elf_la25_stub *la25_stub;
364
b49e97c9
TS
365 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
366 this symbol. */
367 unsigned int possibly_dynamic_relocs;
368
b49e97c9
TS
369 /* If there is a stub that 32 bit functions should use to call this
370 16 bit function, this points to the section containing the stub. */
371 asection *fn_stub;
372
b49e97c9
TS
373 /* If there is a stub that 16 bit functions should use to call this
374 32 bit function, this points to the section containing the stub. */
375 asection *call_stub;
376
377 /* This is like the call_stub field, but it is used if the function
378 being called returns a floating point value. */
379 asection *call_fp_stub;
7c5fcef7 380
634835ae
RS
381 /* The highest GGA_* value that satisfies all references to this symbol. */
382 unsigned int global_got_area : 2;
383
6ccf4795
RS
384 /* True if all GOT relocations against this symbol are for calls. This is
385 a looser condition than no_fn_stub below, because there may be other
386 non-call non-GOT relocations against the symbol. */
387 unsigned int got_only_for_calls : 1;
388
71782a75
RS
389 /* True if one of the relocations described by possibly_dynamic_relocs
390 is against a readonly section. */
391 unsigned int readonly_reloc : 1;
392
861fb55a
DJ
393 /* True if there is a relocation against this symbol that must be
394 resolved by the static linker (in other words, if the relocation
395 cannot possibly be made dynamic). */
396 unsigned int has_static_relocs : 1;
397
71782a75
RS
398 /* True if we must not create a .MIPS.stubs entry for this symbol.
399 This is set, for example, if there are relocations related to
400 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
401 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
402 unsigned int no_fn_stub : 1;
403
404 /* Whether we need the fn_stub; this is true if this symbol appears
405 in any relocs other than a 16 bit call. */
406 unsigned int need_fn_stub : 1;
407
861fb55a
DJ
408 /* True if this symbol is referenced by branch relocations from
409 any non-PIC input file. This is used to determine whether an
410 la25 stub is required. */
411 unsigned int has_nonpic_branches : 1;
33bb52fb
RS
412
413 /* Does this symbol need a traditional MIPS lazy-binding stub
414 (as opposed to a PLT entry)? */
415 unsigned int needs_lazy_stub : 1;
1bbce132
MR
416
417 /* Does this symbol resolve to a PLT entry? */
418 unsigned int use_plt_entry : 1;
b49e97c9
TS
419};
420
421/* MIPS ELF linker hash table. */
422
423struct mips_elf_link_hash_table
424{
425 struct elf_link_hash_table root;
861fb55a 426
b49e97c9
TS
427 /* The number of .rtproc entries. */
428 bfd_size_type procedure_count;
861fb55a 429
b49e97c9
TS
430 /* The size of the .compact_rel section (if SGI_COMPAT). */
431 bfd_size_type compact_rel_size;
861fb55a 432
e6aea42d
MR
433 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
434 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
b34976b6 435 bfd_boolean use_rld_obj_head;
861fb55a 436
b4082c70
DD
437 /* The __rld_map or __rld_obj_head symbol. */
438 struct elf_link_hash_entry *rld_symbol;
861fb55a 439
b49e97c9 440 /* This is set if we see any mips16 stub sections. */
b34976b6 441 bfd_boolean mips16_stubs_seen;
861fb55a
DJ
442
443 /* True if we can generate copy relocs and PLTs. */
444 bfd_boolean use_plts_and_copy_relocs;
445
833794fc
MR
446 /* True if we can only use 32-bit microMIPS instructions. */
447 bfd_boolean insn32;
448
8b10b0b3
MR
449 /* True if we suppress checks for invalid branches between ISA modes. */
450 bfd_boolean ignore_branch_isa;
451
0a44bf69
RS
452 /* True if we're generating code for VxWorks. */
453 bfd_boolean is_vxworks;
861fb55a 454
0e53d9da
AN
455 /* True if we already reported the small-data section overflow. */
456 bfd_boolean small_data_overflow_reported;
861fb55a 457
47275900
MR
458 /* True if we use the special `__gnu_absolute_zero' symbol. */
459 bfd_boolean use_absolute_zero;
460
461 /* True if we have been configured for a GNU target. */
462 bfd_boolean gnu_target;
463
0a44bf69
RS
464 /* Shortcuts to some dynamic sections, or NULL if they are not
465 being used. */
0a44bf69 466 asection *srelplt2;
4e41d0d7 467 asection *sstubs;
861fb55a 468
a8028dd0
RS
469 /* The master GOT information. */
470 struct mips_got_info *got_info;
861fb55a 471
d222d210
RS
472 /* The global symbol in the GOT with the lowest index in the dynamic
473 symbol table. */
474 struct elf_link_hash_entry *global_gotsym;
475
861fb55a 476 /* The size of the PLT header in bytes. */
0a44bf69 477 bfd_vma plt_header_size;
861fb55a 478
1bbce132
MR
479 /* The size of a standard PLT entry in bytes. */
480 bfd_vma plt_mips_entry_size;
481
482 /* The size of a compressed PLT entry in bytes. */
483 bfd_vma plt_comp_entry_size;
484
485 /* The offset of the next standard PLT entry to create. */
486 bfd_vma plt_mips_offset;
487
488 /* The offset of the next compressed PLT entry to create. */
489 bfd_vma plt_comp_offset;
490
491 /* The index of the next .got.plt entry to create. */
492 bfd_vma plt_got_index;
861fb55a 493
33bb52fb
RS
494 /* The number of functions that need a lazy-binding stub. */
495 bfd_vma lazy_stub_count;
861fb55a 496
5108fc1b
RS
497 /* The size of a function stub entry in bytes. */
498 bfd_vma function_stub_size;
861fb55a
DJ
499
500 /* The number of reserved entries at the beginning of the GOT. */
501 unsigned int reserved_gotno;
502
503 /* The section used for mips_elf_la25_stub trampolines.
504 See the comment above that structure for details. */
505 asection *strampoline;
506
507 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
508 pairs. */
509 htab_t la25_stubs;
510
511 /* A function FN (NAME, IS, OS) that creates a new input section
512 called NAME and links it to output section OS. If IS is nonnull,
513 the new section should go immediately before it, otherwise it
514 should go at the (current) beginning of OS.
515
516 The function returns the new section on success, otherwise it
517 returns null. */
518 asection *(*add_stub_section) (const char *, asection *, asection *);
13db6b44
RS
519
520 /* Small local sym cache. */
521 struct sym_cache sym_cache;
1bbce132
MR
522
523 /* Is the PLT header compressed? */
524 unsigned int plt_header_is_comp : 1;
861fb55a
DJ
525};
526
4dfe6ac6
NC
527/* Get the MIPS ELF linker hash table from a link_info structure. */
528
529#define mips_elf_hash_table(p) \
530 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
531 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
532
861fb55a 533/* A structure used to communicate with htab_traverse callbacks. */
4dfe6ac6
NC
534struct mips_htab_traverse_info
535{
861fb55a
DJ
536 /* The usual link-wide information. */
537 struct bfd_link_info *info;
538 bfd *output_bfd;
539
540 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
541 bfd_boolean error;
b49e97c9
TS
542};
543
6ae68ba3
MR
544/* MIPS ELF private object data. */
545
546struct mips_elf_obj_tdata
547{
548 /* Generic ELF private object data. */
549 struct elf_obj_tdata root;
550
551 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
552 bfd *abi_fp_bfd;
ee227692 553
b60bf9be
CF
554 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
555 bfd *abi_msa_bfd;
556
351cdf24
MF
557 /* The abiflags for this object. */
558 Elf_Internal_ABIFlags_v0 abiflags;
559 bfd_boolean abiflags_valid;
560
ee227692
RS
561 /* The GOT requirements of input bfds. */
562 struct mips_got_info *got;
698600e4
AM
563
564 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
565 included directly in this one, but there's no point to wasting
566 the memory just for the infrequently called find_nearest_line. */
567 struct mips_elf_find_line *find_line_info;
568
569 /* An array of stub sections indexed by symbol number. */
570 asection **local_stubs;
571 asection **local_call_stubs;
572
573 /* The Irix 5 support uses two virtual sections, which represent
574 text/data symbols defined in dynamic objects. */
575 asymbol *elf_data_symbol;
576 asymbol *elf_text_symbol;
577 asection *elf_data_section;
578 asection *elf_text_section;
6ae68ba3
MR
579};
580
581/* Get MIPS ELF private object data from BFD's tdata. */
582
583#define mips_elf_tdata(bfd) \
584 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
585
0f20cc35
DJ
586#define TLS_RELOC_P(r_type) \
587 (r_type == R_MIPS_TLS_DTPMOD32 \
588 || r_type == R_MIPS_TLS_DTPMOD64 \
589 || r_type == R_MIPS_TLS_DTPREL32 \
590 || r_type == R_MIPS_TLS_DTPREL64 \
591 || r_type == R_MIPS_TLS_GD \
592 || r_type == R_MIPS_TLS_LDM \
593 || r_type == R_MIPS_TLS_DTPREL_HI16 \
594 || r_type == R_MIPS_TLS_DTPREL_LO16 \
595 || r_type == R_MIPS_TLS_GOTTPREL \
596 || r_type == R_MIPS_TLS_TPREL32 \
597 || r_type == R_MIPS_TLS_TPREL64 \
598 || r_type == R_MIPS_TLS_TPREL_HI16 \
df58fc94 599 || r_type == R_MIPS_TLS_TPREL_LO16 \
d0f13682
CLT
600 || r_type == R_MIPS16_TLS_GD \
601 || r_type == R_MIPS16_TLS_LDM \
602 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
603 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
604 || r_type == R_MIPS16_TLS_GOTTPREL \
605 || r_type == R_MIPS16_TLS_TPREL_HI16 \
606 || r_type == R_MIPS16_TLS_TPREL_LO16 \
df58fc94
RS
607 || r_type == R_MICROMIPS_TLS_GD \
608 || r_type == R_MICROMIPS_TLS_LDM \
609 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
610 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
611 || r_type == R_MICROMIPS_TLS_GOTTPREL \
612 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
613 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
0f20cc35 614
b49e97c9
TS
615/* Structure used to pass information to mips_elf_output_extsym. */
616
617struct extsym_info
618{
9e4aeb93
RS
619 bfd *abfd;
620 struct bfd_link_info *info;
b49e97c9
TS
621 struct ecoff_debug_info *debug;
622 const struct ecoff_debug_swap *swap;
b34976b6 623 bfd_boolean failed;
b49e97c9
TS
624};
625
8dc1a139 626/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
627
628static const char * const mips_elf_dynsym_rtproc_names[] =
629{
630 "_procedure_table",
631 "_procedure_string_table",
632 "_procedure_table_size",
633 NULL
634};
635
636/* These structures are used to generate the .compact_rel section on
8dc1a139 637 IRIX5. */
b49e97c9
TS
638
639typedef struct
640{
641 unsigned long id1; /* Always one? */
642 unsigned long num; /* Number of compact relocation entries. */
643 unsigned long id2; /* Always two? */
644 unsigned long offset; /* The file offset of the first relocation. */
645 unsigned long reserved0; /* Zero? */
646 unsigned long reserved1; /* Zero? */
647} Elf32_compact_rel;
648
649typedef struct
650{
651 bfd_byte id1[4];
652 bfd_byte num[4];
653 bfd_byte id2[4];
654 bfd_byte offset[4];
655 bfd_byte reserved0[4];
656 bfd_byte reserved1[4];
657} Elf32_External_compact_rel;
658
659typedef struct
660{
661 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
662 unsigned int rtype : 4; /* Relocation types. See below. */
663 unsigned int dist2to : 8;
664 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
665 unsigned long konst; /* KONST field. See below. */
666 unsigned long vaddr; /* VADDR to be relocated. */
667} Elf32_crinfo;
668
669typedef struct
670{
671 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
672 unsigned int rtype : 4; /* Relocation types. See below. */
673 unsigned int dist2to : 8;
674 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
675 unsigned long konst; /* KONST field. See below. */
676} Elf32_crinfo2;
677
678typedef struct
679{
680 bfd_byte info[4];
681 bfd_byte konst[4];
682 bfd_byte vaddr[4];
683} Elf32_External_crinfo;
684
685typedef struct
686{
687 bfd_byte info[4];
688 bfd_byte konst[4];
689} Elf32_External_crinfo2;
690
691/* These are the constants used to swap the bitfields in a crinfo. */
692
693#define CRINFO_CTYPE (0x1)
694#define CRINFO_CTYPE_SH (31)
695#define CRINFO_RTYPE (0xf)
696#define CRINFO_RTYPE_SH (27)
697#define CRINFO_DIST2TO (0xff)
698#define CRINFO_DIST2TO_SH (19)
699#define CRINFO_RELVADDR (0x7ffff)
700#define CRINFO_RELVADDR_SH (0)
701
702/* A compact relocation info has long (3 words) or short (2 words)
703 formats. A short format doesn't have VADDR field and relvaddr
704 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
705#define CRF_MIPS_LONG 1
706#define CRF_MIPS_SHORT 0
707
708/* There are 4 types of compact relocation at least. The value KONST
709 has different meaning for each type:
710
711 (type) (konst)
712 CT_MIPS_REL32 Address in data
713 CT_MIPS_WORD Address in word (XXX)
714 CT_MIPS_GPHI_LO GP - vaddr
715 CT_MIPS_JMPAD Address to jump
716 */
717
718#define CRT_MIPS_REL32 0xa
719#define CRT_MIPS_WORD 0xb
720#define CRT_MIPS_GPHI_LO 0xc
721#define CRT_MIPS_JMPAD 0xd
722
723#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
724#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
725#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
726#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
727\f
728/* The structure of the runtime procedure descriptor created by the
729 loader for use by the static exception system. */
730
731typedef struct runtime_pdr {
ae9a127f
NC
732 bfd_vma adr; /* Memory address of start of procedure. */
733 long regmask; /* Save register mask. */
734 long regoffset; /* Save register offset. */
735 long fregmask; /* Save floating point register mask. */
736 long fregoffset; /* Save floating point register offset. */
737 long frameoffset; /* Frame size. */
738 short framereg; /* Frame pointer register. */
739 short pcreg; /* Offset or reg of return pc. */
740 long irpss; /* Index into the runtime string table. */
b49e97c9 741 long reserved;
ae9a127f 742 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
743} RPDR, *pRPDR;
744#define cbRPDR sizeof (RPDR)
745#define rpdNil ((pRPDR) 0)
746\f
b15e6682 747static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
748 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
749 struct mips_elf_link_hash_entry *, int);
b34976b6 750static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 751 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
752static bfd_vma mips_elf_high
753 (bfd_vma);
b34976b6 754static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
755 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
756 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
757 bfd_vma *, asection *);
f4416af6 758static bfd_vma mips_elf_adjust_gp
9719ad41 759 (bfd *, struct mips_got_info *, bfd *);
f4416af6 760
b49e97c9
TS
761/* This will be used when we sort the dynamic relocation records. */
762static bfd *reldyn_sorting_bfd;
763
6d30f5b2
NC
764/* True if ABFD is for CPUs with load interlocking that include
765 non-MIPS1 CPUs and R3900. */
766#define LOAD_INTERLOCKS_P(abfd) \
767 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
768 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
769
cd8d5a82
CF
770/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
771 This should be safe for all architectures. We enable this predicate
772 for RM9000 for now. */
773#define JAL_TO_BAL_P(abfd) \
774 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
775
776/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
777 This should be safe for all architectures. We enable this predicate for
778 all CPUs. */
779#define JALR_TO_BAL_P(abfd) 1
780
38a7df63
CF
781/* True if ABFD is for CPUs that are faster if JR is converted to B.
782 This should be safe for all architectures. We enable this predicate for
783 all CPUs. */
784#define JR_TO_B_P(abfd) 1
785
861fb55a
DJ
786/* True if ABFD is a PIC object. */
787#define PIC_OBJECT_P(abfd) \
788 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
789
351cdf24
MF
790/* Nonzero if ABFD is using the O32 ABI. */
791#define ABI_O32_P(abfd) \
792 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
793
b49e97c9 794/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
795#define ABI_N32_P(abfd) \
796 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
797
4a14403c 798/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 799#define ABI_64_P(abfd) \
141ff970 800 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 801
4a14403c
TS
802/* Nonzero if ABFD is using NewABI conventions. */
803#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
804
e8faf7d1
MR
805/* Nonzero if ABFD has microMIPS code. */
806#define MICROMIPS_P(abfd) \
807 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
808
7361da2c
AB
809/* Nonzero if ABFD is MIPS R6. */
810#define MIPSR6_P(abfd) \
811 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
812 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
813
4a14403c 814/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
815#define IRIX_COMPAT(abfd) \
816 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
817
b49e97c9
TS
818/* Whether we are trying to be compatible with IRIX at all. */
819#define SGI_COMPAT(abfd) \
820 (IRIX_COMPAT (abfd) != ict_none)
821
822/* The name of the options section. */
823#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 824 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 825
cc2e31b9
RS
826/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
827 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
828#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
829 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
830
351cdf24
MF
831/* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
832#define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
833 (strcmp (NAME, ".MIPS.abiflags") == 0)
834
943284cc
DJ
835/* Whether the section is readonly. */
836#define MIPS_ELF_READONLY_SECTION(sec) \
837 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
838 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
839
b49e97c9 840/* The name of the stub section. */
ca07892d 841#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
842
843/* The size of an external REL relocation. */
844#define MIPS_ELF_REL_SIZE(abfd) \
845 (get_elf_backend_data (abfd)->s->sizeof_rel)
846
0a44bf69
RS
847/* The size of an external RELA relocation. */
848#define MIPS_ELF_RELA_SIZE(abfd) \
849 (get_elf_backend_data (abfd)->s->sizeof_rela)
850
b49e97c9
TS
851/* The size of an external dynamic table entry. */
852#define MIPS_ELF_DYN_SIZE(abfd) \
853 (get_elf_backend_data (abfd)->s->sizeof_dyn)
854
855/* The size of a GOT entry. */
856#define MIPS_ELF_GOT_SIZE(abfd) \
857 (get_elf_backend_data (abfd)->s->arch_size / 8)
858
b4082c70
DD
859/* The size of the .rld_map section. */
860#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
861 (get_elf_backend_data (abfd)->s->arch_size / 8)
862
b49e97c9
TS
863/* The size of a symbol-table entry. */
864#define MIPS_ELF_SYM_SIZE(abfd) \
865 (get_elf_backend_data (abfd)->s->sizeof_sym)
866
867/* The default alignment for sections, as a power of two. */
868#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 869 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
870
871/* Get word-sized data. */
872#define MIPS_ELF_GET_WORD(abfd, ptr) \
873 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
874
875/* Put out word-sized data. */
876#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
07d6d2b8
AM
877 (ABI_64_P (abfd) \
878 ? bfd_put_64 (abfd, val, ptr) \
b49e97c9
TS
879 : bfd_put_32 (abfd, val, ptr))
880
861fb55a
DJ
881/* The opcode for word-sized loads (LW or LD). */
882#define MIPS_ELF_LOAD_WORD(abfd) \
883 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
884
b49e97c9 885/* Add a dynamic symbol table-entry. */
9719ad41 886#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 887 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
888
889#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
0aa13fee 890 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
b49e97c9 891
0a44bf69
RS
892/* The name of the dynamic relocation section. */
893#define MIPS_ELF_REL_DYN_NAME(INFO) \
894 (mips_elf_hash_table (INFO)->is_vxworks ? ".rela.dyn" : ".rel.dyn")
895
b49e97c9
TS
896/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
897 from smaller values. Start with zero, widen, *then* decrement. */
898#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 899#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 900
51e38d68
RS
901/* The value to write into got[1] for SVR4 targets, to identify it is
902 a GNU object. The dynamic linker can then use got[1] to store the
903 module pointer. */
904#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
905 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
906
f4416af6 907/* The offset of $gp from the beginning of the .got section. */
0a44bf69
RS
908#define ELF_MIPS_GP_OFFSET(INFO) \
909 (mips_elf_hash_table (INFO)->is_vxworks ? 0x0 : 0x7ff0)
f4416af6
AO
910
911/* The maximum size of the GOT for it to be addressable using 16-bit
912 offsets from $gp. */
0a44bf69 913#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 914
6a691779 915/* Instructions which appear in a stub. */
3d6746ca
DD
916#define STUB_LW(abfd) \
917 ((ABI_64_P (abfd) \
918 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
07d6d2b8 919 : 0x8f998010)) /* lw t9,0x8010(gp) */
40fc1451 920#define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
3d6746ca 921#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
a18a2a34 922#define STUB_JALR 0x0320f809 /* jalr ra,t9 */
5108fc1b
RS
923#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
924#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
925#define STUB_LI16S(abfd, VAL) \
926 ((ABI_64_P (abfd) \
927 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
928 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
929
1bbce132
MR
930/* Likewise for the microMIPS ASE. */
931#define STUB_LW_MICROMIPS(abfd) \
932 (ABI_64_P (abfd) \
933 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
934 : 0xff3c8010) /* lw t9,0x8010(gp) */
935#define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
40fc1451 936#define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
1bbce132
MR
937#define STUB_LUI_MICROMIPS(VAL) \
938 (0x41b80000 + (VAL)) /* lui t8,VAL */
939#define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
833794fc 940#define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
1bbce132
MR
941#define STUB_ORI_MICROMIPS(VAL) \
942 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
943#define STUB_LI16U_MICROMIPS(VAL) \
944 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
945#define STUB_LI16S_MICROMIPS(abfd, VAL) \
946 (ABI_64_P (abfd) \
947 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
948 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
949
5108fc1b
RS
950#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
951#define MIPS_FUNCTION_STUB_BIG_SIZE 20
1bbce132
MR
952#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
953#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
833794fc
MR
954#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
955#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
956
957/* The name of the dynamic interpreter. This is put in the .interp
958 section. */
959
07d6d2b8
AM
960#define ELF_DYNAMIC_INTERPRETER(abfd) \
961 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
962 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
b49e97c9
TS
963 : "/usr/lib/libc.so.1")
964
965#ifdef BFD64
ee6423ed
AO
966#define MNAME(bfd,pre,pos) \
967 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
968#define ELF_R_SYM(bfd, i) \
969 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
970#define ELF_R_TYPE(bfd, i) \
971 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
972#define ELF_R_INFO(bfd, s, t) \
973 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
974#else
ee6423ed 975#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
976#define ELF_R_SYM(bfd, i) \
977 (ELF32_R_SYM (i))
978#define ELF_R_TYPE(bfd, i) \
979 (ELF32_R_TYPE (i))
980#define ELF_R_INFO(bfd, s, t) \
981 (ELF32_R_INFO (s, t))
982#endif
983\f
984 /* The mips16 compiler uses a couple of special sections to handle
985 floating point arguments.
986
987 Section names that look like .mips16.fn.FNNAME contain stubs that
988 copy floating point arguments from the fp regs to the gp regs and
989 then jump to FNNAME. If any 32 bit function calls FNNAME, the
990 call should be redirected to the stub instead. If no 32 bit
991 function calls FNNAME, the stub should be discarded. We need to
992 consider any reference to the function, not just a call, because
993 if the address of the function is taken we will need the stub,
994 since the address might be passed to a 32 bit function.
995
996 Section names that look like .mips16.call.FNNAME contain stubs
997 that copy floating point arguments from the gp regs to the fp
998 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
999 then any 16 bit function that calls FNNAME should be redirected
1000 to the stub instead. If FNNAME is not a 32 bit function, the
1001 stub should be discarded.
1002
1003 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1004 which call FNNAME and then copy the return value from the fp regs
1005 to the gp regs. These stubs store the return value in $18 while
1006 calling FNNAME; any function which might call one of these stubs
1007 must arrange to save $18 around the call. (This case is not
1008 needed for 32 bit functions that call 16 bit functions, because
1009 16 bit functions always return floating point values in both
1010 $f0/$f1 and $2/$3.)
1011
1012 Note that in all cases FNNAME might be defined statically.
1013 Therefore, FNNAME is not used literally. Instead, the relocation
1014 information will indicate which symbol the section is for.
1015
1016 We record any stubs that we find in the symbol table. */
1017
1018#define FN_STUB ".mips16.fn."
1019#define CALL_STUB ".mips16.call."
1020#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
1021
1022#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1023#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1024#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 1025\f
861fb55a 1026/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
1027static const bfd_vma mips_o32_exec_plt0_entry[] =
1028{
861fb55a
DJ
1029 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1030 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1031 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1032 0x031cc023, /* subu $24, $24, $28 */
40fc1451 1033 0x03e07825, /* or t7, ra, zero */
861fb55a
DJ
1034 0x0018c082, /* srl $24, $24, 2 */
1035 0x0320f809, /* jalr $25 */
1036 0x2718fffe /* subu $24, $24, 2 */
1037};
1038
1039/* The format of the first PLT entry in an N32 executable. Different
1040 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
1041static const bfd_vma mips_n32_exec_plt0_entry[] =
1042{
861fb55a
DJ
1043 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1044 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1045 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1046 0x030ec023, /* subu $24, $24, $14 */
40fc1451 1047 0x03e07825, /* or t7, ra, zero */
861fb55a
DJ
1048 0x0018c082, /* srl $24, $24, 2 */
1049 0x0320f809, /* jalr $25 */
1050 0x2718fffe /* subu $24, $24, 2 */
1051};
1052
1053/* The format of the first PLT entry in an N64 executable. Different
1054 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
1055static const bfd_vma mips_n64_exec_plt0_entry[] =
1056{
861fb55a
DJ
1057 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1058 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1059 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1060 0x030ec023, /* subu $24, $24, $14 */
40fc1451 1061 0x03e07825, /* or t7, ra, zero */
861fb55a
DJ
1062 0x0018c0c2, /* srl $24, $24, 3 */
1063 0x0320f809, /* jalr $25 */
1064 0x2718fffe /* subu $24, $24, 2 */
1065};
1066
1bbce132
MR
1067/* The format of the microMIPS first PLT entry in an O32 executable.
1068 We rely on v0 ($2) rather than t8 ($24) to contain the address
1069 of the GOTPLT entry handled, so this stub may only be used when
1070 all the subsequent PLT entries are microMIPS code too.
1071
1072 The trailing NOP is for alignment and correct disassembly only. */
1073static const bfd_vma micromips_o32_exec_plt0_entry[] =
1074{
1075 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1076 0xff23, 0x0000, /* lw $25, 0($3) */
1077 0x0535, /* subu $2, $2, $3 */
1078 0x2525, /* srl $2, $2, 2 */
1079 0x3302, 0xfffe, /* subu $24, $2, 2 */
1080 0x0dff, /* move $15, $31 */
1081 0x45f9, /* jalrs $25 */
1082 0x0f83, /* move $28, $3 */
1083 0x0c00 /* nop */
1084};
1085
833794fc
MR
1086/* The format of the microMIPS first PLT entry in an O32 executable
1087 in the insn32 mode. */
1088static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1089{
1090 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1091 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1092 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1093 0x0398, 0xc1d0, /* subu $24, $24, $28 */
40fc1451 1094 0x001f, 0x7a90, /* or $15, $31, zero */
833794fc
MR
1095 0x0318, 0x1040, /* srl $24, $24, 2 */
1096 0x03f9, 0x0f3c, /* jalr $25 */
1097 0x3318, 0xfffe /* subu $24, $24, 2 */
1098};
1099
1bbce132 1100/* The format of subsequent standard PLT entries. */
6d30f5b2
NC
1101static const bfd_vma mips_exec_plt_entry[] =
1102{
861fb55a
DJ
1103 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1104 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1105 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1106 0x03200008 /* jr $25 */
1107};
1108
7361da2c
AB
1109/* In the following PLT entry the JR and ADDIU instructions will
1110 be swapped in _bfd_mips_elf_finish_dynamic_symbol because
1111 LOAD_INTERLOCKS_P will be true for MIPS R6. */
1112static const bfd_vma mipsr6_exec_plt_entry[] =
1113{
1114 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1115 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1116 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1117 0x03200009 /* jr $25 */
1118};
1119
1bbce132
MR
1120/* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1121 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1122 directly addressable. */
1123static const bfd_vma mips16_o32_exec_plt_entry[] =
1124{
1125 0xb203, /* lw $2, 12($pc) */
1126 0x9a60, /* lw $3, 0($2) */
1127 0x651a, /* move $24, $2 */
1128 0xeb00, /* jr $3 */
1129 0x653b, /* move $25, $3 */
1130 0x6500, /* nop */
1131 0x0000, 0x0000 /* .word (.got.plt entry) */
1132};
1133
1134/* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1135 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1136static const bfd_vma micromips_o32_exec_plt_entry[] =
1137{
1138 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1139 0xff22, 0x0000, /* lw $25, 0($2) */
1140 0x4599, /* jr $25 */
1141 0x0f02 /* move $24, $2 */
1142};
1143
833794fc
MR
1144/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1145static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1146{
1147 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1148 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1149 0x0019, 0x0f3c, /* jr $25 */
1150 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1151};
1152
0a44bf69 1153/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
1154static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1155{
0a44bf69
RS
1156 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1157 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1158 0x8f390008, /* lw t9, 8(t9) */
1159 0x00000000, /* nop */
1160 0x03200008, /* jr t9 */
1161 0x00000000 /* nop */
1162};
1163
1164/* The format of subsequent PLT entries. */
6d30f5b2
NC
1165static const bfd_vma mips_vxworks_exec_plt_entry[] =
1166{
0a44bf69
RS
1167 0x10000000, /* b .PLT_resolver */
1168 0x24180000, /* li t8, <pltindex> */
1169 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1170 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1171 0x8f390000, /* lw t9, 0(t9) */
1172 0x00000000, /* nop */
1173 0x03200008, /* jr t9 */
1174 0x00000000 /* nop */
1175};
1176
1177/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
1178static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1179{
0a44bf69
RS
1180 0x8f990008, /* lw t9, 8(gp) */
1181 0x00000000, /* nop */
1182 0x03200008, /* jr t9 */
1183 0x00000000, /* nop */
1184 0x00000000, /* nop */
1185 0x00000000 /* nop */
1186};
1187
1188/* The format of subsequent PLT entries. */
6d30f5b2
NC
1189static const bfd_vma mips_vxworks_shared_plt_entry[] =
1190{
0a44bf69
RS
1191 0x10000000, /* b .PLT_resolver */
1192 0x24180000 /* li t8, <pltindex> */
1193};
1194\f
d21911ea
MR
1195/* microMIPS 32-bit opcode helper installer. */
1196
1197static void
1198bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1199{
1200 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
07d6d2b8 1201 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
d21911ea
MR
1202}
1203
1204/* microMIPS 32-bit opcode helper retriever. */
1205
1206static bfd_vma
1207bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1208{
1209 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1210}
1211\f
b49e97c9
TS
1212/* Look up an entry in a MIPS ELF linker hash table. */
1213
1214#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1215 ((struct mips_elf_link_hash_entry *) \
1216 elf_link_hash_lookup (&(table)->root, (string), (create), \
1217 (copy), (follow)))
1218
1219/* Traverse a MIPS ELF linker hash table. */
1220
1221#define mips_elf_link_hash_traverse(table, func, info) \
1222 (elf_link_hash_traverse \
1223 (&(table)->root, \
9719ad41 1224 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1225 (info)))
1226
0f20cc35
DJ
1227/* Find the base offsets for thread-local storage in this object,
1228 for GD/LD and IE/LE respectively. */
1229
1230#define TP_OFFSET 0x7000
1231#define DTP_OFFSET 0x8000
1232
1233static bfd_vma
1234dtprel_base (struct bfd_link_info *info)
1235{
1236 /* If tls_sec is NULL, we should have signalled an error already. */
1237 if (elf_hash_table (info)->tls_sec == NULL)
1238 return 0;
1239 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1240}
1241
1242static bfd_vma
1243tprel_base (struct bfd_link_info *info)
1244{
1245 /* If tls_sec is NULL, we should have signalled an error already. */
1246 if (elf_hash_table (info)->tls_sec == NULL)
1247 return 0;
1248 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1249}
1250
b49e97c9
TS
1251/* Create an entry in a MIPS ELF linker hash table. */
1252
1253static struct bfd_hash_entry *
9719ad41
RS
1254mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1255 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1256{
1257 struct mips_elf_link_hash_entry *ret =
1258 (struct mips_elf_link_hash_entry *) entry;
1259
1260 /* Allocate the structure if it has not already been allocated by a
1261 subclass. */
9719ad41
RS
1262 if (ret == NULL)
1263 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1264 if (ret == NULL)
b49e97c9
TS
1265 return (struct bfd_hash_entry *) ret;
1266
1267 /* Call the allocation method of the superclass. */
1268 ret = ((struct mips_elf_link_hash_entry *)
1269 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1270 table, string));
9719ad41 1271 if (ret != NULL)
b49e97c9
TS
1272 {
1273 /* Set local fields. */
1274 memset (&ret->esym, 0, sizeof (EXTR));
1275 /* We use -2 as a marker to indicate that the information has
1276 not been set. -1 means there is no associated ifd. */
1277 ret->esym.ifd = -2;
861fb55a 1278 ret->la25_stub = 0;
b49e97c9 1279 ret->possibly_dynamic_relocs = 0;
b49e97c9 1280 ret->fn_stub = NULL;
b49e97c9
TS
1281 ret->call_stub = NULL;
1282 ret->call_fp_stub = NULL;
634835ae 1283 ret->global_got_area = GGA_NONE;
6ccf4795 1284 ret->got_only_for_calls = TRUE;
71782a75 1285 ret->readonly_reloc = FALSE;
861fb55a 1286 ret->has_static_relocs = FALSE;
71782a75
RS
1287 ret->no_fn_stub = FALSE;
1288 ret->need_fn_stub = FALSE;
861fb55a 1289 ret->has_nonpic_branches = FALSE;
33bb52fb 1290 ret->needs_lazy_stub = FALSE;
1bbce132 1291 ret->use_plt_entry = FALSE;
b49e97c9
TS
1292 }
1293
1294 return (struct bfd_hash_entry *) ret;
1295}
f0abc2a1 1296
6ae68ba3
MR
1297/* Allocate MIPS ELF private object data. */
1298
1299bfd_boolean
1300_bfd_mips_elf_mkobject (bfd *abfd)
1301{
1302 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1303 MIPS_ELF_DATA);
1304}
1305
f0abc2a1 1306bfd_boolean
9719ad41 1307_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1308{
f592407e
AM
1309 if (!sec->used_by_bfd)
1310 {
1311 struct _mips_elf_section_data *sdata;
1312 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 1313
f592407e
AM
1314 sdata = bfd_zalloc (abfd, amt);
1315 if (sdata == NULL)
1316 return FALSE;
1317 sec->used_by_bfd = sdata;
1318 }
f0abc2a1
AM
1319
1320 return _bfd_elf_new_section_hook (abfd, sec);
1321}
b49e97c9
TS
1322\f
1323/* Read ECOFF debugging information from a .mdebug section into a
1324 ecoff_debug_info structure. */
1325
b34976b6 1326bfd_boolean
9719ad41
RS
1327_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1328 struct ecoff_debug_info *debug)
b49e97c9
TS
1329{
1330 HDRR *symhdr;
1331 const struct ecoff_debug_swap *swap;
9719ad41 1332 char *ext_hdr;
b49e97c9
TS
1333
1334 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1335 memset (debug, 0, sizeof (*debug));
1336
9719ad41 1337 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1338 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1339 goto error_return;
1340
9719ad41 1341 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1342 swap->external_hdr_size))
b49e97c9
TS
1343 goto error_return;
1344
1345 symhdr = &debug->symbolic_header;
1346 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1347
1348 /* The symbolic header contains absolute file offsets and sizes to
1349 read. */
1350#define READ(ptr, offset, count, size, type) \
1351 if (symhdr->count == 0) \
1352 debug->ptr = NULL; \
1353 else \
1354 { \
1355 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
9719ad41 1356 debug->ptr = bfd_malloc (amt); \
b49e97c9
TS
1357 if (debug->ptr == NULL) \
1358 goto error_return; \
9719ad41 1359 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0 \
b49e97c9
TS
1360 || bfd_bread (debug->ptr, amt, abfd) != amt) \
1361 goto error_return; \
1362 }
1363
1364 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1365 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1366 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1367 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1368 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1369 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1370 union aux_ext *);
1371 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1372 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1373 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1374 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1375 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1376#undef READ
1377
1378 debug->fdr = NULL;
b49e97c9 1379
b34976b6 1380 return TRUE;
b49e97c9
TS
1381
1382 error_return:
1383 if (ext_hdr != NULL)
1384 free (ext_hdr);
1385 if (debug->line != NULL)
1386 free (debug->line);
1387 if (debug->external_dnr != NULL)
1388 free (debug->external_dnr);
1389 if (debug->external_pdr != NULL)
1390 free (debug->external_pdr);
1391 if (debug->external_sym != NULL)
1392 free (debug->external_sym);
1393 if (debug->external_opt != NULL)
1394 free (debug->external_opt);
1395 if (debug->external_aux != NULL)
1396 free (debug->external_aux);
1397 if (debug->ss != NULL)
1398 free (debug->ss);
1399 if (debug->ssext != NULL)
1400 free (debug->ssext);
1401 if (debug->external_fdr != NULL)
1402 free (debug->external_fdr);
1403 if (debug->external_rfd != NULL)
1404 free (debug->external_rfd);
1405 if (debug->external_ext != NULL)
1406 free (debug->external_ext);
b34976b6 1407 return FALSE;
b49e97c9
TS
1408}
1409\f
1410/* Swap RPDR (runtime procedure table entry) for output. */
1411
1412static void
9719ad41 1413ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1414{
1415 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1416 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1417 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1418 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1419 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1420 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1421
1422 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1423 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1424
1425 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1426}
1427
1428/* Create a runtime procedure table from the .mdebug section. */
1429
b34976b6 1430static bfd_boolean
9719ad41
RS
1431mips_elf_create_procedure_table (void *handle, bfd *abfd,
1432 struct bfd_link_info *info, asection *s,
1433 struct ecoff_debug_info *debug)
b49e97c9
TS
1434{
1435 const struct ecoff_debug_swap *swap;
1436 HDRR *hdr = &debug->symbolic_header;
1437 RPDR *rpdr, *rp;
1438 struct rpdr_ext *erp;
9719ad41 1439 void *rtproc;
b49e97c9
TS
1440 struct pdr_ext *epdr;
1441 struct sym_ext *esym;
1442 char *ss, **sv;
1443 char *str;
1444 bfd_size_type size;
1445 bfd_size_type count;
1446 unsigned long sindex;
1447 unsigned long i;
1448 PDR pdr;
1449 SYMR sym;
1450 const char *no_name_func = _("static procedure (no name)");
1451
1452 epdr = NULL;
1453 rpdr = NULL;
1454 esym = NULL;
1455 ss = NULL;
1456 sv = NULL;
1457
1458 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1459
1460 sindex = strlen (no_name_func) + 1;
1461 count = hdr->ipdMax;
1462 if (count > 0)
1463 {
1464 size = swap->external_pdr_size;
1465
9719ad41 1466 epdr = bfd_malloc (size * count);
b49e97c9
TS
1467 if (epdr == NULL)
1468 goto error_return;
1469
9719ad41 1470 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1471 goto error_return;
1472
1473 size = sizeof (RPDR);
9719ad41 1474 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1475 if (rpdr == NULL)
1476 goto error_return;
1477
1478 size = sizeof (char *);
9719ad41 1479 sv = bfd_malloc (size * count);
b49e97c9
TS
1480 if (sv == NULL)
1481 goto error_return;
1482
1483 count = hdr->isymMax;
1484 size = swap->external_sym_size;
9719ad41 1485 esym = bfd_malloc (size * count);
b49e97c9
TS
1486 if (esym == NULL)
1487 goto error_return;
1488
9719ad41 1489 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1490 goto error_return;
1491
1492 count = hdr->issMax;
9719ad41 1493 ss = bfd_malloc (count);
b49e97c9
TS
1494 if (ss == NULL)
1495 goto error_return;
f075ee0c 1496 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1497 goto error_return;
1498
1499 count = hdr->ipdMax;
1500 for (i = 0; i < (unsigned long) count; i++, rp++)
1501 {
9719ad41
RS
1502 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1503 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1504 rp->adr = sym.value;
1505 rp->regmask = pdr.regmask;
1506 rp->regoffset = pdr.regoffset;
1507 rp->fregmask = pdr.fregmask;
1508 rp->fregoffset = pdr.fregoffset;
1509 rp->frameoffset = pdr.frameoffset;
1510 rp->framereg = pdr.framereg;
1511 rp->pcreg = pdr.pcreg;
1512 rp->irpss = sindex;
1513 sv[i] = ss + sym.iss;
1514 sindex += strlen (sv[i]) + 1;
1515 }
1516 }
1517
1518 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1519 size = BFD_ALIGN (size, 16);
9719ad41 1520 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1521 if (rtproc == NULL)
1522 {
1523 mips_elf_hash_table (info)->procedure_count = 0;
1524 goto error_return;
1525 }
1526
1527 mips_elf_hash_table (info)->procedure_count = count + 2;
1528
9719ad41 1529 erp = rtproc;
b49e97c9
TS
1530 memset (erp, 0, sizeof (struct rpdr_ext));
1531 erp++;
1532 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1533 strcpy (str, no_name_func);
1534 str += strlen (no_name_func) + 1;
1535 for (i = 0; i < count; i++)
1536 {
1537 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1538 strcpy (str, sv[i]);
1539 str += strlen (sv[i]) + 1;
1540 }
1541 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1542
1543 /* Set the size and contents of .rtproc section. */
eea6121a 1544 s->size = size;
9719ad41 1545 s->contents = rtproc;
b49e97c9
TS
1546
1547 /* Skip this section later on (I don't think this currently
1548 matters, but someday it might). */
8423293d 1549 s->map_head.link_order = NULL;
b49e97c9
TS
1550
1551 if (epdr != NULL)
1552 free (epdr);
1553 if (rpdr != NULL)
1554 free (rpdr);
1555 if (esym != NULL)
1556 free (esym);
1557 if (ss != NULL)
1558 free (ss);
1559 if (sv != NULL)
1560 free (sv);
1561
b34976b6 1562 return TRUE;
b49e97c9
TS
1563
1564 error_return:
1565 if (epdr != NULL)
1566 free (epdr);
1567 if (rpdr != NULL)
1568 free (rpdr);
1569 if (esym != NULL)
1570 free (esym);
1571 if (ss != NULL)
1572 free (ss);
1573 if (sv != NULL)
1574 free (sv);
b34976b6 1575 return FALSE;
b49e97c9 1576}
738e5348 1577\f
861fb55a
DJ
1578/* We're going to create a stub for H. Create a symbol for the stub's
1579 value and size, to help make the disassembly easier to read. */
1580
1581static bfd_boolean
1582mips_elf_create_stub_symbol (struct bfd_link_info *info,
1583 struct mips_elf_link_hash_entry *h,
1584 const char *prefix, asection *s, bfd_vma value,
1585 bfd_vma size)
1586{
a848a227 1587 bfd_boolean micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
861fb55a
DJ
1588 struct bfd_link_hash_entry *bh;
1589 struct elf_link_hash_entry *elfh;
e1fa0163
NC
1590 char *name;
1591 bfd_boolean res;
861fb55a 1592
a848a227 1593 if (micromips_p)
df58fc94
RS
1594 value |= 1;
1595
861fb55a 1596 /* Create a new symbol. */
e1fa0163 1597 name = concat (prefix, h->root.root.root.string, NULL);
861fb55a 1598 bh = NULL;
e1fa0163
NC
1599 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1600 BSF_LOCAL, s, value, NULL,
1601 TRUE, FALSE, &bh);
1602 free (name);
1603 if (! res)
861fb55a
DJ
1604 return FALSE;
1605
1606 /* Make it a local function. */
1607 elfh = (struct elf_link_hash_entry *) bh;
1608 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1609 elfh->size = size;
1610 elfh->forced_local = 1;
a848a227
MR
1611 if (micromips_p)
1612 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
861fb55a
DJ
1613 return TRUE;
1614}
1615
738e5348
RS
1616/* We're about to redefine H. Create a symbol to represent H's
1617 current value and size, to help make the disassembly easier
1618 to read. */
1619
1620static bfd_boolean
1621mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1622 struct mips_elf_link_hash_entry *h,
1623 const char *prefix)
1624{
1625 struct bfd_link_hash_entry *bh;
1626 struct elf_link_hash_entry *elfh;
e1fa0163 1627 char *name;
738e5348
RS
1628 asection *s;
1629 bfd_vma value;
e1fa0163 1630 bfd_boolean res;
738e5348
RS
1631
1632 /* Read the symbol's value. */
1633 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1634 || h->root.root.type == bfd_link_hash_defweak);
1635 s = h->root.root.u.def.section;
1636 value = h->root.root.u.def.value;
1637
1638 /* Create a new symbol. */
e1fa0163 1639 name = concat (prefix, h->root.root.root.string, NULL);
738e5348 1640 bh = NULL;
e1fa0163
NC
1641 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1642 BSF_LOCAL, s, value, NULL,
1643 TRUE, FALSE, &bh);
1644 free (name);
1645 if (! res)
738e5348
RS
1646 return FALSE;
1647
1648 /* Make it local and copy the other attributes from H. */
1649 elfh = (struct elf_link_hash_entry *) bh;
1650 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1651 elfh->other = h->root.other;
1652 elfh->size = h->root.size;
1653 elfh->forced_local = 1;
1654 return TRUE;
1655}
1656
1657/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1658 function rather than to a hard-float stub. */
1659
1660static bfd_boolean
1661section_allows_mips16_refs_p (asection *section)
1662{
1663 const char *name;
1664
1665 name = bfd_get_section_name (section->owner, section);
1666 return (FN_STUB_P (name)
1667 || CALL_STUB_P (name)
1668 || CALL_FP_STUB_P (name)
1669 || strcmp (name, ".pdr") == 0);
1670}
1671
1672/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1673 stub section of some kind. Return the R_SYMNDX of the target
1674 function, or 0 if we can't decide which function that is. */
1675
1676static unsigned long
cb4437b8
MR
1677mips16_stub_symndx (const struct elf_backend_data *bed,
1678 asection *sec ATTRIBUTE_UNUSED,
502e814e 1679 const Elf_Internal_Rela *relocs,
738e5348
RS
1680 const Elf_Internal_Rela *relend)
1681{
cb4437b8 1682 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
738e5348
RS
1683 const Elf_Internal_Rela *rel;
1684
cb4437b8
MR
1685 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1686 one in a compound relocation. */
1687 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
738e5348
RS
1688 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1689 return ELF_R_SYM (sec->owner, rel->r_info);
1690
1691 /* Otherwise trust the first relocation, whatever its kind. This is
1692 the traditional behavior. */
1693 if (relocs < relend)
1694 return ELF_R_SYM (sec->owner, relocs->r_info);
1695
1696 return 0;
1697}
b49e97c9
TS
1698
1699/* Check the mips16 stubs for a particular symbol, and see if we can
1700 discard them. */
1701
861fb55a
DJ
1702static void
1703mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1704 struct mips_elf_link_hash_entry *h)
b49e97c9 1705{
738e5348
RS
1706 /* Dynamic symbols must use the standard call interface, in case other
1707 objects try to call them. */
1708 if (h->fn_stub != NULL
1709 && h->root.dynindx != -1)
1710 {
1711 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1712 h->need_fn_stub = TRUE;
1713 }
1714
b49e97c9
TS
1715 if (h->fn_stub != NULL
1716 && ! h->need_fn_stub)
1717 {
1718 /* We don't need the fn_stub; the only references to this symbol
07d6d2b8
AM
1719 are 16 bit calls. Clobber the size to 0 to prevent it from
1720 being included in the link. */
eea6121a 1721 h->fn_stub->size = 0;
b49e97c9
TS
1722 h->fn_stub->flags &= ~SEC_RELOC;
1723 h->fn_stub->reloc_count = 0;
1724 h->fn_stub->flags |= SEC_EXCLUDE;
ca9584fb 1725 h->fn_stub->output_section = bfd_abs_section_ptr;
b49e97c9
TS
1726 }
1727
1728 if (h->call_stub != NULL
30c09090 1729 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1730 {
1731 /* We don't need the call_stub; this is a 16 bit function, so
07d6d2b8
AM
1732 calls from other 16 bit functions are OK. Clobber the size
1733 to 0 to prevent it from being included in the link. */
eea6121a 1734 h->call_stub->size = 0;
b49e97c9
TS
1735 h->call_stub->flags &= ~SEC_RELOC;
1736 h->call_stub->reloc_count = 0;
1737 h->call_stub->flags |= SEC_EXCLUDE;
ca9584fb 1738 h->call_stub->output_section = bfd_abs_section_ptr;
b49e97c9
TS
1739 }
1740
1741 if (h->call_fp_stub != NULL
30c09090 1742 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1743 {
1744 /* We don't need the call_stub; this is a 16 bit function, so
07d6d2b8
AM
1745 calls from other 16 bit functions are OK. Clobber the size
1746 to 0 to prevent it from being included in the link. */
eea6121a 1747 h->call_fp_stub->size = 0;
b49e97c9
TS
1748 h->call_fp_stub->flags &= ~SEC_RELOC;
1749 h->call_fp_stub->reloc_count = 0;
1750 h->call_fp_stub->flags |= SEC_EXCLUDE;
ca9584fb 1751 h->call_fp_stub->output_section = bfd_abs_section_ptr;
b49e97c9 1752 }
861fb55a
DJ
1753}
1754
1755/* Hashtable callbacks for mips_elf_la25_stubs. */
1756
1757static hashval_t
1758mips_elf_la25_stub_hash (const void *entry_)
1759{
1760 const struct mips_elf_la25_stub *entry;
1761
1762 entry = (struct mips_elf_la25_stub *) entry_;
1763 return entry->h->root.root.u.def.section->id
1764 + entry->h->root.root.u.def.value;
1765}
1766
1767static int
1768mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1769{
1770 const struct mips_elf_la25_stub *entry1, *entry2;
1771
1772 entry1 = (struct mips_elf_la25_stub *) entry1_;
1773 entry2 = (struct mips_elf_la25_stub *) entry2_;
1774 return ((entry1->h->root.root.u.def.section
1775 == entry2->h->root.root.u.def.section)
1776 && (entry1->h->root.root.u.def.value
1777 == entry2->h->root.root.u.def.value));
1778}
1779
1780/* Called by the linker to set up the la25 stub-creation code. FN is
1781 the linker's implementation of add_stub_function. Return true on
1782 success. */
1783
1784bfd_boolean
1785_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1786 asection *(*fn) (const char *, asection *,
1787 asection *))
1788{
1789 struct mips_elf_link_hash_table *htab;
1790
1791 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1792 if (htab == NULL)
1793 return FALSE;
1794
861fb55a
DJ
1795 htab->add_stub_section = fn;
1796 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1797 mips_elf_la25_stub_eq, NULL);
1798 if (htab->la25_stubs == NULL)
1799 return FALSE;
1800
1801 return TRUE;
1802}
1803
1804/* Return true if H is a locally-defined PIC function, in the sense
8f0c309a
CLT
1805 that it or its fn_stub might need $25 to be valid on entry.
1806 Note that MIPS16 functions set up $gp using PC-relative instructions,
1807 so they themselves never need $25 to be valid. Only non-MIPS16
1808 entry points are of interest here. */
861fb55a
DJ
1809
1810static bfd_boolean
1811mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1812{
1813 return ((h->root.root.type == bfd_link_hash_defined
1814 || h->root.root.type == bfd_link_hash_defweak)
1815 && h->root.def_regular
1816 && !bfd_is_abs_section (h->root.root.u.def.section)
f02cb058 1817 && !bfd_is_und_section (h->root.root.u.def.section)
8f0c309a
CLT
1818 && (!ELF_ST_IS_MIPS16 (h->root.other)
1819 || (h->fn_stub && h->need_fn_stub))
861fb55a
DJ
1820 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1821 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1822}
1823
8f0c309a
CLT
1824/* Set *SEC to the input section that contains the target of STUB.
1825 Return the offset of the target from the start of that section. */
1826
1827static bfd_vma
1828mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1829 asection **sec)
1830{
1831 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1832 {
1833 BFD_ASSERT (stub->h->need_fn_stub);
1834 *sec = stub->h->fn_stub;
1835 return 0;
1836 }
1837 else
1838 {
1839 *sec = stub->h->root.root.u.def.section;
1840 return stub->h->root.root.u.def.value;
1841 }
1842}
1843
861fb55a
DJ
1844/* STUB describes an la25 stub that we have decided to implement
1845 by inserting an LUI/ADDIU pair before the target function.
1846 Create the section and redirect the function symbol to it. */
1847
1848static bfd_boolean
1849mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1850 struct bfd_link_info *info)
1851{
1852 struct mips_elf_link_hash_table *htab;
1853 char *name;
1854 asection *s, *input_section;
1855 unsigned int align;
1856
1857 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1858 if (htab == NULL)
1859 return FALSE;
861fb55a
DJ
1860
1861 /* Create a unique name for the new section. */
1862 name = bfd_malloc (11 + sizeof (".text.stub."));
1863 if (name == NULL)
1864 return FALSE;
1865 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1866
1867 /* Create the section. */
8f0c309a 1868 mips_elf_get_la25_target (stub, &input_section);
861fb55a
DJ
1869 s = htab->add_stub_section (name, input_section,
1870 input_section->output_section);
1871 if (s == NULL)
1872 return FALSE;
1873
1874 /* Make sure that any padding goes before the stub. */
1875 align = input_section->alignment_power;
1876 if (!bfd_set_section_alignment (s->owner, s, align))
1877 return FALSE;
1878 if (align > 3)
1879 s->size = (1 << align) - 8;
1880
1881 /* Create a symbol for the stub. */
1882 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1883 stub->stub_section = s;
1884 stub->offset = s->size;
1885
1886 /* Allocate room for it. */
1887 s->size += 8;
1888 return TRUE;
1889}
1890
1891/* STUB describes an la25 stub that we have decided to implement
1892 with a separate trampoline. Allocate room for it and redirect
1893 the function symbol to it. */
1894
1895static bfd_boolean
1896mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1897 struct bfd_link_info *info)
1898{
1899 struct mips_elf_link_hash_table *htab;
1900 asection *s;
1901
1902 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1903 if (htab == NULL)
1904 return FALSE;
861fb55a
DJ
1905
1906 /* Create a trampoline section, if we haven't already. */
1907 s = htab->strampoline;
1908 if (s == NULL)
1909 {
1910 asection *input_section = stub->h->root.root.u.def.section;
1911 s = htab->add_stub_section (".text", NULL,
1912 input_section->output_section);
1913 if (s == NULL || !bfd_set_section_alignment (s->owner, s, 4))
1914 return FALSE;
1915 htab->strampoline = s;
1916 }
1917
1918 /* Create a symbol for the stub. */
1919 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1920 stub->stub_section = s;
1921 stub->offset = s->size;
1922
1923 /* Allocate room for it. */
1924 s->size += 16;
1925 return TRUE;
1926}
1927
1928/* H describes a symbol that needs an la25 stub. Make sure that an
1929 appropriate stub exists and point H at it. */
1930
1931static bfd_boolean
1932mips_elf_add_la25_stub (struct bfd_link_info *info,
1933 struct mips_elf_link_hash_entry *h)
1934{
1935 struct mips_elf_link_hash_table *htab;
1936 struct mips_elf_la25_stub search, *stub;
1937 bfd_boolean use_trampoline_p;
1938 asection *s;
1939 bfd_vma value;
1940 void **slot;
1941
861fb55a
DJ
1942 /* Describe the stub we want. */
1943 search.stub_section = NULL;
1944 search.offset = 0;
1945 search.h = h;
1946
1947 /* See if we've already created an equivalent stub. */
1948 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1949 if (htab == NULL)
1950 return FALSE;
1951
861fb55a
DJ
1952 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1953 if (slot == NULL)
1954 return FALSE;
1955
1956 stub = (struct mips_elf_la25_stub *) *slot;
1957 if (stub != NULL)
1958 {
1959 /* We can reuse the existing stub. */
1960 h->la25_stub = stub;
1961 return TRUE;
1962 }
1963
1964 /* Create a permanent copy of ENTRY and add it to the hash table. */
1965 stub = bfd_malloc (sizeof (search));
1966 if (stub == NULL)
1967 return FALSE;
1968 *stub = search;
1969 *slot = stub;
1970
8f0c309a
CLT
1971 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
1972 of the section and if we would need no more than 2 nops. */
1973 value = mips_elf_get_la25_target (stub, &s);
fe152e64
MR
1974 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
1975 value &= ~1;
8f0c309a
CLT
1976 use_trampoline_p = (value != 0 || s->alignment_power > 4);
1977
861fb55a
DJ
1978 h->la25_stub = stub;
1979 return (use_trampoline_p
1980 ? mips_elf_add_la25_trampoline (stub, info)
1981 : mips_elf_add_la25_intro (stub, info));
1982}
1983
1984/* A mips_elf_link_hash_traverse callback that is called before sizing
1985 sections. DATA points to a mips_htab_traverse_info structure. */
1986
1987static bfd_boolean
1988mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
1989{
1990 struct mips_htab_traverse_info *hti;
1991
1992 hti = (struct mips_htab_traverse_info *) data;
0e1862bb 1993 if (!bfd_link_relocatable (hti->info))
861fb55a 1994 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 1995
861fb55a
DJ
1996 if (mips_elf_local_pic_function_p (h))
1997 {
ba85c43e
NC
1998 /* PR 12845: If H is in a section that has been garbage
1999 collected it will have its output section set to *ABS*. */
2000 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2001 return TRUE;
2002
861fb55a
DJ
2003 /* H is a function that might need $25 to be valid on entry.
2004 If we're creating a non-PIC relocatable object, mark H as
2005 being PIC. If we're creating a non-relocatable object with
2006 non-PIC branches and jumps to H, make sure that H has an la25
2007 stub. */
0e1862bb 2008 if (bfd_link_relocatable (hti->info))
861fb55a
DJ
2009 {
2010 if (!PIC_OBJECT_P (hti->output_bfd))
2011 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2012 }
2013 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2014 {
2015 hti->error = TRUE;
2016 return FALSE;
2017 }
2018 }
b34976b6 2019 return TRUE;
b49e97c9
TS
2020}
2021\f
d6f16593
MR
2022/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2023 Most mips16 instructions are 16 bits, but these instructions
2024 are 32 bits.
2025
2026 The format of these instructions is:
2027
2028 +--------------+--------------------------------+
2029 | JALX | X| Imm 20:16 | Imm 25:21 |
2030 +--------------+--------------------------------+
07d6d2b8 2031 | Immediate 15:0 |
d6f16593
MR
2032 +-----------------------------------------------+
2033
2034 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2035 Note that the immediate value in the first word is swapped.
2036
2037 When producing a relocatable object file, R_MIPS16_26 is
2038 handled mostly like R_MIPS_26. In particular, the addend is
2039 stored as a straight 26-bit value in a 32-bit instruction.
2040 (gas makes life simpler for itself by never adjusting a
2041 R_MIPS16_26 reloc to be against a section, so the addend is
2042 always zero). However, the 32 bit instruction is stored as 2
2043 16-bit values, rather than a single 32-bit value. In a
2044 big-endian file, the result is the same; in a little-endian
2045 file, the two 16-bit halves of the 32 bit value are swapped.
2046 This is so that a disassembler can recognize the jal
2047 instruction.
2048
2049 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2050 instruction stored as two 16-bit values. The addend A is the
2051 contents of the targ26 field. The calculation is the same as
2052 R_MIPS_26. When storing the calculated value, reorder the
2053 immediate value as shown above, and don't forget to store the
2054 value as two 16-bit values.
2055
2056 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2057 defined as
2058
2059 big-endian:
2060 +--------+----------------------+
07d6d2b8
AM
2061 | | |
2062 | | targ26-16 |
2063 |31 26|25 0|
d6f16593
MR
2064 +--------+----------------------+
2065
2066 little-endian:
2067 +----------+------+-------------+
07d6d2b8
AM
2068 | | | |
2069 | sub1 | | sub2 |
2070 |0 9|10 15|16 31|
d6f16593
MR
2071 +----------+--------------------+
2072 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2073 ((sub1 << 16) | sub2)).
2074
2075 When producing a relocatable object file, the calculation is
2076 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2077 When producing a fully linked file, the calculation is
2078 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2079 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2080
738e5348
RS
2081 The table below lists the other MIPS16 instruction relocations.
2082 Each one is calculated in the same way as the non-MIPS16 relocation
2083 given on the right, but using the extended MIPS16 layout of 16-bit
2084 immediate fields:
2085
2086 R_MIPS16_GPREL R_MIPS_GPREL16
2087 R_MIPS16_GOT16 R_MIPS_GOT16
2088 R_MIPS16_CALL16 R_MIPS_CALL16
2089 R_MIPS16_HI16 R_MIPS_HI16
2090 R_MIPS16_LO16 R_MIPS_LO16
2091
2092 A typical instruction will have a format like this:
d6f16593
MR
2093
2094 +--------------+--------------------------------+
2095 | EXTEND | Imm 10:5 | Imm 15:11 |
2096 +--------------+--------------------------------+
2097 | Major | rx | ry | Imm 4:0 |
2098 +--------------+--------------------------------+
2099
2100 EXTEND is the five bit value 11110. Major is the instruction
2101 opcode.
2102
738e5348
RS
2103 All we need to do here is shuffle the bits appropriately.
2104 As above, the two 16-bit halves must be swapped on a
c9775dde
MR
2105 little-endian system.
2106
2107 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2108 relocatable field is shifted by 1 rather than 2 and the same bit
2109 shuffling is done as with the relocations above. */
738e5348
RS
2110
2111static inline bfd_boolean
2112mips16_reloc_p (int r_type)
2113{
2114 switch (r_type)
2115 {
2116 case R_MIPS16_26:
2117 case R_MIPS16_GPREL:
2118 case R_MIPS16_GOT16:
2119 case R_MIPS16_CALL16:
2120 case R_MIPS16_HI16:
2121 case R_MIPS16_LO16:
d0f13682
CLT
2122 case R_MIPS16_TLS_GD:
2123 case R_MIPS16_TLS_LDM:
2124 case R_MIPS16_TLS_DTPREL_HI16:
2125 case R_MIPS16_TLS_DTPREL_LO16:
2126 case R_MIPS16_TLS_GOTTPREL:
2127 case R_MIPS16_TLS_TPREL_HI16:
2128 case R_MIPS16_TLS_TPREL_LO16:
c9775dde 2129 case R_MIPS16_PC16_S1:
738e5348
RS
2130 return TRUE;
2131
2132 default:
2133 return FALSE;
2134 }
2135}
2136
df58fc94
RS
2137/* Check if a microMIPS reloc. */
2138
2139static inline bfd_boolean
2140micromips_reloc_p (unsigned int r_type)
2141{
2142 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2143}
2144
2145/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2146 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2147 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2148
2149static inline bfd_boolean
2150micromips_reloc_shuffle_p (unsigned int r_type)
2151{
2152 return (micromips_reloc_p (r_type)
2153 && r_type != R_MICROMIPS_PC7_S1
2154 && r_type != R_MICROMIPS_PC10_S1);
2155}
2156
738e5348
RS
2157static inline bfd_boolean
2158got16_reloc_p (int r_type)
2159{
df58fc94
RS
2160 return (r_type == R_MIPS_GOT16
2161 || r_type == R_MIPS16_GOT16
2162 || r_type == R_MICROMIPS_GOT16);
738e5348
RS
2163}
2164
2165static inline bfd_boolean
2166call16_reloc_p (int r_type)
2167{
df58fc94
RS
2168 return (r_type == R_MIPS_CALL16
2169 || r_type == R_MIPS16_CALL16
2170 || r_type == R_MICROMIPS_CALL16);
2171}
2172
2173static inline bfd_boolean
2174got_disp_reloc_p (unsigned int r_type)
2175{
2176 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2177}
2178
2179static inline bfd_boolean
2180got_page_reloc_p (unsigned int r_type)
2181{
2182 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2183}
2184
df58fc94
RS
2185static inline bfd_boolean
2186got_lo16_reloc_p (unsigned int r_type)
2187{
2188 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2189}
2190
2191static inline bfd_boolean
2192call_hi16_reloc_p (unsigned int r_type)
2193{
2194 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2195}
2196
2197static inline bfd_boolean
2198call_lo16_reloc_p (unsigned int r_type)
2199{
2200 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
738e5348
RS
2201}
2202
2203static inline bfd_boolean
2204hi16_reloc_p (int r_type)
2205{
df58fc94
RS
2206 return (r_type == R_MIPS_HI16
2207 || r_type == R_MIPS16_HI16
7361da2c
AB
2208 || r_type == R_MICROMIPS_HI16
2209 || r_type == R_MIPS_PCHI16);
738e5348 2210}
d6f16593 2211
738e5348
RS
2212static inline bfd_boolean
2213lo16_reloc_p (int r_type)
2214{
df58fc94
RS
2215 return (r_type == R_MIPS_LO16
2216 || r_type == R_MIPS16_LO16
7361da2c
AB
2217 || r_type == R_MICROMIPS_LO16
2218 || r_type == R_MIPS_PCLO16);
738e5348
RS
2219}
2220
2221static inline bfd_boolean
2222mips16_call_reloc_p (int r_type)
2223{
2224 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2225}
d6f16593 2226
38a7df63
CF
2227static inline bfd_boolean
2228jal_reloc_p (int r_type)
2229{
df58fc94
RS
2230 return (r_type == R_MIPS_26
2231 || r_type == R_MIPS16_26
2232 || r_type == R_MICROMIPS_26_S1);
2233}
2234
99aefae6
MR
2235static inline bfd_boolean
2236b_reloc_p (int r_type)
2237{
2238 return (r_type == R_MIPS_PC26_S2
2239 || r_type == R_MIPS_PC21_S2
2240 || r_type == R_MIPS_PC16
c9775dde 2241 || r_type == R_MIPS_GNU_REL16_S2
9d862524
MR
2242 || r_type == R_MIPS16_PC16_S1
2243 || r_type == R_MICROMIPS_PC16_S1
2244 || r_type == R_MICROMIPS_PC10_S1
2245 || r_type == R_MICROMIPS_PC7_S1);
99aefae6
MR
2246}
2247
7361da2c
AB
2248static inline bfd_boolean
2249aligned_pcrel_reloc_p (int r_type)
2250{
2251 return (r_type == R_MIPS_PC18_S3
2252 || r_type == R_MIPS_PC19_S2);
2253}
2254
9d862524
MR
2255static inline bfd_boolean
2256branch_reloc_p (int r_type)
2257{
2258 return (r_type == R_MIPS_26
2259 || r_type == R_MIPS_PC26_S2
2260 || r_type == R_MIPS_PC21_S2
2261 || r_type == R_MIPS_PC16
2262 || r_type == R_MIPS_GNU_REL16_S2);
2263}
2264
c9775dde
MR
2265static inline bfd_boolean
2266mips16_branch_reloc_p (int r_type)
2267{
2268 return (r_type == R_MIPS16_26
2269 || r_type == R_MIPS16_PC16_S1);
2270}
2271
df58fc94
RS
2272static inline bfd_boolean
2273micromips_branch_reloc_p (int r_type)
2274{
2275 return (r_type == R_MICROMIPS_26_S1
2276 || r_type == R_MICROMIPS_PC16_S1
2277 || r_type == R_MICROMIPS_PC10_S1
2278 || r_type == R_MICROMIPS_PC7_S1);
2279}
2280
2281static inline bfd_boolean
2282tls_gd_reloc_p (unsigned int r_type)
2283{
d0f13682
CLT
2284 return (r_type == R_MIPS_TLS_GD
2285 || r_type == R_MIPS16_TLS_GD
2286 || r_type == R_MICROMIPS_TLS_GD);
df58fc94
RS
2287}
2288
2289static inline bfd_boolean
2290tls_ldm_reloc_p (unsigned int r_type)
2291{
d0f13682
CLT
2292 return (r_type == R_MIPS_TLS_LDM
2293 || r_type == R_MIPS16_TLS_LDM
2294 || r_type == R_MICROMIPS_TLS_LDM);
df58fc94
RS
2295}
2296
2297static inline bfd_boolean
2298tls_gottprel_reloc_p (unsigned int r_type)
2299{
d0f13682
CLT
2300 return (r_type == R_MIPS_TLS_GOTTPREL
2301 || r_type == R_MIPS16_TLS_GOTTPREL
2302 || r_type == R_MICROMIPS_TLS_GOTTPREL);
38a7df63
CF
2303}
2304
d6f16593 2305void
df58fc94
RS
2306_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2307 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2308{
df58fc94 2309 bfd_vma first, second, val;
d6f16593 2310
df58fc94 2311 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2312 return;
2313
df58fc94
RS
2314 /* Pick up the first and second halfwords of the instruction. */
2315 first = bfd_get_16 (abfd, data);
2316 second = bfd_get_16 (abfd, data + 2);
2317 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2318 val = first << 16 | second;
2319 else if (r_type != R_MIPS16_26)
2320 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2321 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
d6f16593 2322 else
df58fc94
RS
2323 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2324 | ((first & 0x1f) << 21) | second);
d6f16593
MR
2325 bfd_put_32 (abfd, val, data);
2326}
2327
2328void
df58fc94
RS
2329_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2330 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2331{
df58fc94 2332 bfd_vma first, second, val;
d6f16593 2333
df58fc94 2334 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2335 return;
2336
2337 val = bfd_get_32 (abfd, data);
df58fc94 2338 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
d6f16593 2339 {
df58fc94
RS
2340 second = val & 0xffff;
2341 first = val >> 16;
2342 }
2343 else if (r_type != R_MIPS16_26)
2344 {
2345 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2346 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
d6f16593
MR
2347 }
2348 else
2349 {
df58fc94
RS
2350 second = val & 0xffff;
2351 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2352 | ((val >> 21) & 0x1f);
d6f16593 2353 }
df58fc94
RS
2354 bfd_put_16 (abfd, second, data + 2);
2355 bfd_put_16 (abfd, first, data);
d6f16593
MR
2356}
2357
b49e97c9 2358bfd_reloc_status_type
9719ad41
RS
2359_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2360 arelent *reloc_entry, asection *input_section,
2361 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
2362{
2363 bfd_vma relocation;
a7ebbfdf 2364 bfd_signed_vma val;
30ac9238 2365 bfd_reloc_status_type status;
b49e97c9
TS
2366
2367 if (bfd_is_com_section (symbol->section))
2368 relocation = 0;
2369 else
2370 relocation = symbol->value;
2371
2372 relocation += symbol->section->output_section->vma;
2373 relocation += symbol->section->output_offset;
2374
07515404 2375 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
2376 return bfd_reloc_outofrange;
2377
b49e97c9 2378 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
2379 val = reloc_entry->addend;
2380
30ac9238 2381 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 2382
b49e97c9 2383 /* Adjust val for the final section location and GP value. If we
1049f94e 2384 are producing relocatable output, we don't want to do this for
b49e97c9 2385 an external symbol. */
1049f94e 2386 if (! relocatable
b49e97c9
TS
2387 || (symbol->flags & BSF_SECTION_SYM) != 0)
2388 val += relocation - gp;
2389
a7ebbfdf
TS
2390 if (reloc_entry->howto->partial_inplace)
2391 {
30ac9238
RS
2392 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2393 (bfd_byte *) data
2394 + reloc_entry->address);
2395 if (status != bfd_reloc_ok)
2396 return status;
a7ebbfdf
TS
2397 }
2398 else
2399 reloc_entry->addend = val;
b49e97c9 2400
1049f94e 2401 if (relocatable)
b49e97c9 2402 reloc_entry->address += input_section->output_offset;
30ac9238
RS
2403
2404 return bfd_reloc_ok;
2405}
2406
2407/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2408 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2409 that contains the relocation field and DATA points to the start of
2410 INPUT_SECTION. */
2411
2412struct mips_hi16
2413{
2414 struct mips_hi16 *next;
2415 bfd_byte *data;
2416 asection *input_section;
2417 arelent rel;
2418};
2419
2420/* FIXME: This should not be a static variable. */
2421
2422static struct mips_hi16 *mips_hi16_list;
2423
2424/* A howto special_function for REL *HI16 relocations. We can only
2425 calculate the correct value once we've seen the partnering
2426 *LO16 relocation, so just save the information for later.
2427
2428 The ABI requires that the *LO16 immediately follow the *HI16.
2429 However, as a GNU extension, we permit an arbitrary number of
2430 *HI16s to be associated with a single *LO16. This significantly
2431 simplies the relocation handling in gcc. */
2432
2433bfd_reloc_status_type
2434_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2435 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2436 asection *input_section, bfd *output_bfd,
2437 char **error_message ATTRIBUTE_UNUSED)
2438{
2439 struct mips_hi16 *n;
2440
07515404 2441 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2442 return bfd_reloc_outofrange;
2443
2444 n = bfd_malloc (sizeof *n);
2445 if (n == NULL)
2446 return bfd_reloc_outofrange;
2447
2448 n->next = mips_hi16_list;
2449 n->data = data;
2450 n->input_section = input_section;
2451 n->rel = *reloc_entry;
2452 mips_hi16_list = n;
2453
2454 if (output_bfd != NULL)
2455 reloc_entry->address += input_section->output_offset;
2456
2457 return bfd_reloc_ok;
2458}
2459
738e5348 2460/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2461 like any other 16-bit relocation when applied to global symbols, but is
2462 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2463
2464bfd_reloc_status_type
2465_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2466 void *data, asection *input_section,
2467 bfd *output_bfd, char **error_message)
2468{
2469 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2470 || bfd_is_und_section (bfd_get_section (symbol))
2471 || bfd_is_com_section (bfd_get_section (symbol)))
2472 /* The relocation is against a global symbol. */
2473 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2474 input_section, output_bfd,
2475 error_message);
2476
2477 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2478 input_section, output_bfd, error_message);
2479}
2480
2481/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2482 is a straightforward 16 bit inplace relocation, but we must deal with
2483 any partnering high-part relocations as well. */
2484
2485bfd_reloc_status_type
2486_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2487 void *data, asection *input_section,
2488 bfd *output_bfd, char **error_message)
2489{
2490 bfd_vma vallo;
d6f16593 2491 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2492
07515404 2493 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2494 return bfd_reloc_outofrange;
2495
df58fc94 2496 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
d6f16593 2497 location);
df58fc94
RS
2498 vallo = bfd_get_32 (abfd, location);
2499 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2500 location);
d6f16593 2501
30ac9238
RS
2502 while (mips_hi16_list != NULL)
2503 {
2504 bfd_reloc_status_type ret;
2505 struct mips_hi16 *hi;
2506
2507 hi = mips_hi16_list;
2508
738e5348
RS
2509 /* R_MIPS*_GOT16 relocations are something of a special case. We
2510 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2511 relocation (with a rightshift of 16). However, since GOT16
2512 relocations can also be used with global symbols, their howto
2513 has a rightshift of 0. */
2514 if (hi->rel.howto->type == R_MIPS_GOT16)
2515 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2516 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2517 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
df58fc94
RS
2518 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2519 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
30ac9238
RS
2520
2521 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2522 carry or borrow will induce a change of +1 or -1 in the high part. */
2523 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2524
30ac9238
RS
2525 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2526 hi->input_section, output_bfd,
2527 error_message);
2528 if (ret != bfd_reloc_ok)
2529 return ret;
2530
2531 mips_hi16_list = hi->next;
2532 free (hi);
2533 }
2534
2535 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2536 input_section, output_bfd,
2537 error_message);
2538}
2539
2540/* A generic howto special_function. This calculates and installs the
2541 relocation itself, thus avoiding the oft-discussed problems in
2542 bfd_perform_relocation and bfd_install_relocation. */
2543
2544bfd_reloc_status_type
2545_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2546 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2547 asection *input_section, bfd *output_bfd,
2548 char **error_message ATTRIBUTE_UNUSED)
2549{
2550 bfd_signed_vma val;
2551 bfd_reloc_status_type status;
2552 bfd_boolean relocatable;
2553
2554 relocatable = (output_bfd != NULL);
2555
07515404 2556 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2557 return bfd_reloc_outofrange;
2558
2559 /* Build up the field adjustment in VAL. */
2560 val = 0;
2561 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2562 {
2563 /* Either we're calculating the final field value or we have a
2564 relocation against a section symbol. Add in the section's
2565 offset or address. */
2566 val += symbol->section->output_section->vma;
2567 val += symbol->section->output_offset;
2568 }
2569
2570 if (!relocatable)
2571 {
2572 /* We're calculating the final field value. Add in the symbol's value
2573 and, if pc-relative, subtract the address of the field itself. */
2574 val += symbol->value;
2575 if (reloc_entry->howto->pc_relative)
2576 {
2577 val -= input_section->output_section->vma;
2578 val -= input_section->output_offset;
2579 val -= reloc_entry->address;
2580 }
2581 }
2582
2583 /* VAL is now the final adjustment. If we're keeping this relocation
2584 in the output file, and if the relocation uses a separate addend,
2585 we just need to add VAL to that addend. Otherwise we need to add
2586 VAL to the relocation field itself. */
2587 if (relocatable && !reloc_entry->howto->partial_inplace)
2588 reloc_entry->addend += val;
2589 else
2590 {
d6f16593
MR
2591 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2592
30ac9238
RS
2593 /* Add in the separate addend, if any. */
2594 val += reloc_entry->addend;
2595
2596 /* Add VAL to the relocation field. */
df58fc94
RS
2597 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2598 location);
30ac9238 2599 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593 2600 location);
df58fc94
RS
2601 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2602 location);
d6f16593 2603
30ac9238
RS
2604 if (status != bfd_reloc_ok)
2605 return status;
2606 }
2607
2608 if (relocatable)
2609 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2610
2611 return bfd_reloc_ok;
2612}
2613\f
2614/* Swap an entry in a .gptab section. Note that these routines rely
2615 on the equivalence of the two elements of the union. */
2616
2617static void
9719ad41
RS
2618bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2619 Elf32_gptab *in)
b49e97c9
TS
2620{
2621 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2622 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2623}
2624
2625static void
9719ad41
RS
2626bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2627 Elf32_External_gptab *ex)
b49e97c9
TS
2628{
2629 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2630 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2631}
2632
2633static void
9719ad41
RS
2634bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2635 Elf32_External_compact_rel *ex)
b49e97c9
TS
2636{
2637 H_PUT_32 (abfd, in->id1, ex->id1);
2638 H_PUT_32 (abfd, in->num, ex->num);
2639 H_PUT_32 (abfd, in->id2, ex->id2);
2640 H_PUT_32 (abfd, in->offset, ex->offset);
2641 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2642 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2643}
2644
2645static void
9719ad41
RS
2646bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2647 Elf32_External_crinfo *ex)
b49e97c9
TS
2648{
2649 unsigned long l;
2650
2651 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2652 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2653 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2654 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2655 H_PUT_32 (abfd, l, ex->info);
2656 H_PUT_32 (abfd, in->konst, ex->konst);
2657 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2658}
b49e97c9
TS
2659\f
2660/* A .reginfo section holds a single Elf32_RegInfo structure. These
2661 routines swap this structure in and out. They are used outside of
2662 BFD, so they are globally visible. */
2663
2664void
9719ad41
RS
2665bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2666 Elf32_RegInfo *in)
b49e97c9
TS
2667{
2668 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2669 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2670 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2671 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2672 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2673 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2674}
2675
2676void
9719ad41
RS
2677bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2678 Elf32_External_RegInfo *ex)
b49e97c9
TS
2679{
2680 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2681 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2682 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2683 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2684 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2685 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2686}
2687
2688/* In the 64 bit ABI, the .MIPS.options section holds register
2689 information in an Elf64_Reginfo structure. These routines swap
2690 them in and out. They are globally visible because they are used
2691 outside of BFD. These routines are here so that gas can call them
2692 without worrying about whether the 64 bit ABI has been included. */
2693
2694void
9719ad41
RS
2695bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2696 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2697{
2698 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2699 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2700 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2701 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2702 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2703 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2704 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2705}
2706
2707void
9719ad41
RS
2708bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2709 Elf64_External_RegInfo *ex)
b49e97c9
TS
2710{
2711 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2712 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2713 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2714 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2715 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2716 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2717 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2718}
2719
2720/* Swap in an options header. */
2721
2722void
9719ad41
RS
2723bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2724 Elf_Internal_Options *in)
b49e97c9
TS
2725{
2726 in->kind = H_GET_8 (abfd, ex->kind);
2727 in->size = H_GET_8 (abfd, ex->size);
2728 in->section = H_GET_16 (abfd, ex->section);
2729 in->info = H_GET_32 (abfd, ex->info);
2730}
2731
2732/* Swap out an options header. */
2733
2734void
9719ad41
RS
2735bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2736 Elf_External_Options *ex)
b49e97c9
TS
2737{
2738 H_PUT_8 (abfd, in->kind, ex->kind);
2739 H_PUT_8 (abfd, in->size, ex->size);
2740 H_PUT_16 (abfd, in->section, ex->section);
2741 H_PUT_32 (abfd, in->info, ex->info);
2742}
351cdf24
MF
2743
2744/* Swap in an abiflags structure. */
2745
2746void
2747bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2748 const Elf_External_ABIFlags_v0 *ex,
2749 Elf_Internal_ABIFlags_v0 *in)
2750{
2751 in->version = H_GET_16 (abfd, ex->version);
2752 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2753 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2754 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2755 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2756 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2757 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2758 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2759 in->ases = H_GET_32 (abfd, ex->ases);
2760 in->flags1 = H_GET_32 (abfd, ex->flags1);
2761 in->flags2 = H_GET_32 (abfd, ex->flags2);
2762}
2763
2764/* Swap out an abiflags structure. */
2765
2766void
2767bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2768 const Elf_Internal_ABIFlags_v0 *in,
2769 Elf_External_ABIFlags_v0 *ex)
2770{
2771 H_PUT_16 (abfd, in->version, ex->version);
2772 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2773 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2774 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2775 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2776 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2777 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2778 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2779 H_PUT_32 (abfd, in->ases, ex->ases);
2780 H_PUT_32 (abfd, in->flags1, ex->flags1);
2781 H_PUT_32 (abfd, in->flags2, ex->flags2);
2782}
b49e97c9
TS
2783\f
2784/* This function is called via qsort() to sort the dynamic relocation
2785 entries by increasing r_symndx value. */
2786
2787static int
9719ad41 2788sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2789{
947216bf
AM
2790 Elf_Internal_Rela int_reloc1;
2791 Elf_Internal_Rela int_reloc2;
6870500c 2792 int diff;
b49e97c9 2793
947216bf
AM
2794 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2795 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2796
6870500c
RS
2797 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2798 if (diff != 0)
2799 return diff;
2800
2801 if (int_reloc1.r_offset < int_reloc2.r_offset)
2802 return -1;
2803 if (int_reloc1.r_offset > int_reloc2.r_offset)
2804 return 1;
2805 return 0;
b49e97c9
TS
2806}
2807
f4416af6
AO
2808/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2809
2810static int
7e3102a7
AM
2811sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2812 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2813{
7e3102a7 2814#ifdef BFD64
f4416af6
AO
2815 Elf_Internal_Rela int_reloc1[3];
2816 Elf_Internal_Rela int_reloc2[3];
2817
2818 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2819 (reldyn_sorting_bfd, arg1, int_reloc1);
2820 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2821 (reldyn_sorting_bfd, arg2, int_reloc2);
2822
6870500c
RS
2823 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2824 return -1;
2825 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2826 return 1;
2827
2828 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2829 return -1;
2830 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2831 return 1;
2832 return 0;
7e3102a7
AM
2833#else
2834 abort ();
2835#endif
f4416af6
AO
2836}
2837
2838
b49e97c9
TS
2839/* This routine is used to write out ECOFF debugging external symbol
2840 information. It is called via mips_elf_link_hash_traverse. The
2841 ECOFF external symbol information must match the ELF external
2842 symbol information. Unfortunately, at this point we don't know
2843 whether a symbol is required by reloc information, so the two
2844 tables may wind up being different. We must sort out the external
2845 symbol information before we can set the final size of the .mdebug
2846 section, and we must set the size of the .mdebug section before we
2847 can relocate any sections, and we can't know which symbols are
2848 required by relocation until we relocate the sections.
2849 Fortunately, it is relatively unlikely that any symbol will be
2850 stripped but required by a reloc. In particular, it can not happen
2851 when generating a final executable. */
2852
b34976b6 2853static bfd_boolean
9719ad41 2854mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2855{
9719ad41 2856 struct extsym_info *einfo = data;
b34976b6 2857 bfd_boolean strip;
b49e97c9
TS
2858 asection *sec, *output_section;
2859
b49e97c9 2860 if (h->root.indx == -2)
b34976b6 2861 strip = FALSE;
f5385ebf 2862 else if ((h->root.def_dynamic
77cfaee6
AM
2863 || h->root.ref_dynamic
2864 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2865 && !h->root.def_regular
2866 && !h->root.ref_regular)
b34976b6 2867 strip = TRUE;
b49e97c9
TS
2868 else if (einfo->info->strip == strip_all
2869 || (einfo->info->strip == strip_some
2870 && bfd_hash_lookup (einfo->info->keep_hash,
2871 h->root.root.root.string,
b34976b6
AM
2872 FALSE, FALSE) == NULL))
2873 strip = TRUE;
b49e97c9 2874 else
b34976b6 2875 strip = FALSE;
b49e97c9
TS
2876
2877 if (strip)
b34976b6 2878 return TRUE;
b49e97c9
TS
2879
2880 if (h->esym.ifd == -2)
2881 {
2882 h->esym.jmptbl = 0;
2883 h->esym.cobol_main = 0;
2884 h->esym.weakext = 0;
2885 h->esym.reserved = 0;
2886 h->esym.ifd = ifdNil;
2887 h->esym.asym.value = 0;
2888 h->esym.asym.st = stGlobal;
2889
2890 if (h->root.root.type == bfd_link_hash_undefined
2891 || h->root.root.type == bfd_link_hash_undefweak)
2892 {
2893 const char *name;
2894
2895 /* Use undefined class. Also, set class and type for some
07d6d2b8 2896 special symbols. */
b49e97c9
TS
2897 name = h->root.root.root.string;
2898 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2899 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2900 {
2901 h->esym.asym.sc = scData;
2902 h->esym.asym.st = stLabel;
2903 h->esym.asym.value = 0;
2904 }
2905 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2906 {
2907 h->esym.asym.sc = scAbs;
2908 h->esym.asym.st = stLabel;
2909 h->esym.asym.value =
2910 mips_elf_hash_table (einfo->info)->procedure_count;
2911 }
b49e97c9
TS
2912 else
2913 h->esym.asym.sc = scUndefined;
2914 }
2915 else if (h->root.root.type != bfd_link_hash_defined
2916 && h->root.root.type != bfd_link_hash_defweak)
2917 h->esym.asym.sc = scAbs;
2918 else
2919 {
2920 const char *name;
2921
2922 sec = h->root.root.u.def.section;
2923 output_section = sec->output_section;
2924
2925 /* When making a shared library and symbol h is the one from
2926 the another shared library, OUTPUT_SECTION may be null. */
2927 if (output_section == NULL)
2928 h->esym.asym.sc = scUndefined;
2929 else
2930 {
2931 name = bfd_section_name (output_section->owner, output_section);
2932
2933 if (strcmp (name, ".text") == 0)
2934 h->esym.asym.sc = scText;
2935 else if (strcmp (name, ".data") == 0)
2936 h->esym.asym.sc = scData;
2937 else if (strcmp (name, ".sdata") == 0)
2938 h->esym.asym.sc = scSData;
2939 else if (strcmp (name, ".rodata") == 0
2940 || strcmp (name, ".rdata") == 0)
2941 h->esym.asym.sc = scRData;
2942 else if (strcmp (name, ".bss") == 0)
2943 h->esym.asym.sc = scBss;
2944 else if (strcmp (name, ".sbss") == 0)
2945 h->esym.asym.sc = scSBss;
2946 else if (strcmp (name, ".init") == 0)
2947 h->esym.asym.sc = scInit;
2948 else if (strcmp (name, ".fini") == 0)
2949 h->esym.asym.sc = scFini;
2950 else
2951 h->esym.asym.sc = scAbs;
2952 }
2953 }
2954
2955 h->esym.asym.reserved = 0;
2956 h->esym.asym.index = indexNil;
2957 }
2958
2959 if (h->root.root.type == bfd_link_hash_common)
2960 h->esym.asym.value = h->root.root.u.c.size;
2961 else if (h->root.root.type == bfd_link_hash_defined
2962 || h->root.root.type == bfd_link_hash_defweak)
2963 {
2964 if (h->esym.asym.sc == scCommon)
2965 h->esym.asym.sc = scBss;
2966 else if (h->esym.asym.sc == scSCommon)
2967 h->esym.asym.sc = scSBss;
2968
2969 sec = h->root.root.u.def.section;
2970 output_section = sec->output_section;
2971 if (output_section != NULL)
2972 h->esym.asym.value = (h->root.root.u.def.value
2973 + sec->output_offset
2974 + output_section->vma);
2975 else
2976 h->esym.asym.value = 0;
2977 }
33bb52fb 2978 else
b49e97c9
TS
2979 {
2980 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
2981
2982 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 2983 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 2984
33bb52fb 2985 if (hd->needs_lazy_stub)
b49e97c9 2986 {
1bbce132
MR
2987 BFD_ASSERT (hd->root.plt.plist != NULL);
2988 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
b49e97c9
TS
2989 /* Set type and value for a symbol with a function stub. */
2990 h->esym.asym.st = stProc;
2991 sec = hd->root.root.u.def.section;
2992 if (sec == NULL)
2993 h->esym.asym.value = 0;
2994 else
2995 {
2996 output_section = sec->output_section;
2997 if (output_section != NULL)
1bbce132 2998 h->esym.asym.value = (hd->root.plt.plist->stub_offset
b49e97c9
TS
2999 + sec->output_offset
3000 + output_section->vma);
3001 else
3002 h->esym.asym.value = 0;
3003 }
b49e97c9
TS
3004 }
3005 }
3006
3007 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3008 h->root.root.root.string,
3009 &h->esym))
3010 {
b34976b6
AM
3011 einfo->failed = TRUE;
3012 return FALSE;
b49e97c9
TS
3013 }
3014
b34976b6 3015 return TRUE;
b49e97c9
TS
3016}
3017
3018/* A comparison routine used to sort .gptab entries. */
3019
3020static int
9719ad41 3021gptab_compare (const void *p1, const void *p2)
b49e97c9 3022{
9719ad41
RS
3023 const Elf32_gptab *a1 = p1;
3024 const Elf32_gptab *a2 = p2;
b49e97c9
TS
3025
3026 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3027}
3028\f
b15e6682 3029/* Functions to manage the got entry hash table. */
f4416af6
AO
3030
3031/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3032 hash number. */
3033
3034static INLINE hashval_t
9719ad41 3035mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
3036{
3037#ifdef BFD64
3038 return addr + (addr >> 32);
3039#else
3040 return addr;
3041#endif
3042}
3043
f4416af6 3044static hashval_t
d9bf376d 3045mips_elf_got_entry_hash (const void *entry_)
f4416af6
AO
3046{
3047 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3048
e641e783 3049 return (entry->symndx
9ab066b4
RS
3050 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3051 + (entry->tls_type == GOT_TLS_LDM ? 0
e641e783
RS
3052 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3053 : entry->symndx >= 0 ? (entry->abfd->id
3054 + mips_elf_hash_bfd_vma (entry->d.addend))
3055 : entry->d.h->root.root.root.hash));
f4416af6
AO
3056}
3057
3058static int
3dff0dd1 3059mips_elf_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
3060{
3061 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3062 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3063
e641e783 3064 return (e1->symndx == e2->symndx
9ab066b4
RS
3065 && e1->tls_type == e2->tls_type
3066 && (e1->tls_type == GOT_TLS_LDM ? TRUE
e641e783
RS
3067 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3068 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3069 && e1->d.addend == e2->d.addend)
3070 : e2->abfd && e1->d.h == e2->d.h));
b15e6682 3071}
c224138d 3072
13db6b44
RS
3073static hashval_t
3074mips_got_page_ref_hash (const void *ref_)
3075{
3076 const struct mips_got_page_ref *ref;
3077
3078 ref = (const struct mips_got_page_ref *) ref_;
3079 return ((ref->symndx >= 0
3080 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3081 : ref->u.h->root.root.root.hash)
3082 + mips_elf_hash_bfd_vma (ref->addend));
3083}
3084
3085static int
3086mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3087{
3088 const struct mips_got_page_ref *ref1, *ref2;
3089
3090 ref1 = (const struct mips_got_page_ref *) ref1_;
3091 ref2 = (const struct mips_got_page_ref *) ref2_;
3092 return (ref1->symndx == ref2->symndx
3093 && (ref1->symndx < 0
3094 ? ref1->u.h == ref2->u.h
3095 : ref1->u.abfd == ref2->u.abfd)
3096 && ref1->addend == ref2->addend);
3097}
3098
c224138d
RS
3099static hashval_t
3100mips_got_page_entry_hash (const void *entry_)
3101{
3102 const struct mips_got_page_entry *entry;
3103
3104 entry = (const struct mips_got_page_entry *) entry_;
13db6b44 3105 return entry->sec->id;
c224138d
RS
3106}
3107
3108static int
3109mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3110{
3111 const struct mips_got_page_entry *entry1, *entry2;
3112
3113 entry1 = (const struct mips_got_page_entry *) entry1_;
3114 entry2 = (const struct mips_got_page_entry *) entry2_;
13db6b44 3115 return entry1->sec == entry2->sec;
c224138d 3116}
b15e6682 3117\f
3dff0dd1 3118/* Create and return a new mips_got_info structure. */
5334aa52
RS
3119
3120static struct mips_got_info *
3dff0dd1 3121mips_elf_create_got_info (bfd *abfd)
5334aa52
RS
3122{
3123 struct mips_got_info *g;
3124
3125 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3126 if (g == NULL)
3127 return NULL;
3128
3dff0dd1
RS
3129 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3130 mips_elf_got_entry_eq, NULL);
5334aa52
RS
3131 if (g->got_entries == NULL)
3132 return NULL;
3133
13db6b44
RS
3134 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3135 mips_got_page_ref_eq, NULL);
3136 if (g->got_page_refs == NULL)
5334aa52
RS
3137 return NULL;
3138
3139 return g;
3140}
3141
ee227692
RS
3142/* Return the GOT info for input bfd ABFD, trying to create a new one if
3143 CREATE_P and if ABFD doesn't already have a GOT. */
3144
3145static struct mips_got_info *
3146mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3147{
3148 struct mips_elf_obj_tdata *tdata;
3149
3150 if (!is_mips_elf (abfd))
3151 return NULL;
3152
3153 tdata = mips_elf_tdata (abfd);
3154 if (!tdata->got && create_p)
3dff0dd1 3155 tdata->got = mips_elf_create_got_info (abfd);
ee227692
RS
3156 return tdata->got;
3157}
3158
d7206569
RS
3159/* Record that ABFD should use output GOT G. */
3160
3161static void
3162mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3163{
3164 struct mips_elf_obj_tdata *tdata;
3165
3166 BFD_ASSERT (is_mips_elf (abfd));
3167 tdata = mips_elf_tdata (abfd);
3168 if (tdata->got)
3169 {
3170 /* The GOT structure itself and the hash table entries are
3171 allocated to a bfd, but the hash tables aren't. */
3172 htab_delete (tdata->got->got_entries);
13db6b44
RS
3173 htab_delete (tdata->got->got_page_refs);
3174 if (tdata->got->got_page_entries)
3175 htab_delete (tdata->got->got_page_entries);
d7206569
RS
3176 }
3177 tdata->got = g;
3178}
3179
0a44bf69
RS
3180/* Return the dynamic relocation section. If it doesn't exist, try to
3181 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3182 if creation fails. */
f4416af6
AO
3183
3184static asection *
0a44bf69 3185mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 3186{
0a44bf69 3187 const char *dname;
f4416af6 3188 asection *sreloc;
0a44bf69 3189 bfd *dynobj;
f4416af6 3190
0a44bf69
RS
3191 dname = MIPS_ELF_REL_DYN_NAME (info);
3192 dynobj = elf_hash_table (info)->dynobj;
3d4d4302 3193 sreloc = bfd_get_linker_section (dynobj, dname);
f4416af6
AO
3194 if (sreloc == NULL && create_p)
3195 {
3d4d4302
AM
3196 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3197 (SEC_ALLOC
3198 | SEC_LOAD
3199 | SEC_HAS_CONTENTS
3200 | SEC_IN_MEMORY
3201 | SEC_LINKER_CREATED
3202 | SEC_READONLY));
f4416af6 3203 if (sreloc == NULL
f4416af6 3204 || ! bfd_set_section_alignment (dynobj, sreloc,
d80dcc6a 3205 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
3206 return NULL;
3207 }
3208 return sreloc;
3209}
3210
e641e783
RS
3211/* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3212
3213static int
3214mips_elf_reloc_tls_type (unsigned int r_type)
3215{
3216 if (tls_gd_reloc_p (r_type))
3217 return GOT_TLS_GD;
3218
3219 if (tls_ldm_reloc_p (r_type))
3220 return GOT_TLS_LDM;
3221
3222 if (tls_gottprel_reloc_p (r_type))
3223 return GOT_TLS_IE;
3224
9ab066b4 3225 return GOT_TLS_NONE;
e641e783
RS
3226}
3227
3228/* Return the number of GOT slots needed for GOT TLS type TYPE. */
3229
3230static int
3231mips_tls_got_entries (unsigned int type)
3232{
3233 switch (type)
3234 {
3235 case GOT_TLS_GD:
3236 case GOT_TLS_LDM:
3237 return 2;
3238
3239 case GOT_TLS_IE:
3240 return 1;
3241
9ab066b4 3242 case GOT_TLS_NONE:
e641e783
RS
3243 return 0;
3244 }
3245 abort ();
3246}
3247
0f20cc35
DJ
3248/* Count the number of relocations needed for a TLS GOT entry, with
3249 access types from TLS_TYPE, and symbol H (or a local symbol if H
3250 is NULL). */
3251
3252static int
3253mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3254 struct elf_link_hash_entry *h)
3255{
3256 int indx = 0;
0f20cc35
DJ
3257 bfd_boolean need_relocs = FALSE;
3258 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3259
1cb83cac
MR
3260 if (h != NULL
3261 && h->dynindx != -1
3262 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3263 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
0f20cc35
DJ
3264 indx = h->dynindx;
3265
9143e72c 3266 if ((bfd_link_dll (info) || indx != 0)
0f20cc35
DJ
3267 && (h == NULL
3268 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3269 || h->root.type != bfd_link_hash_undefweak))
3270 need_relocs = TRUE;
3271
3272 if (!need_relocs)
e641e783 3273 return 0;
0f20cc35 3274
9ab066b4 3275 switch (tls_type)
0f20cc35 3276 {
e641e783
RS
3277 case GOT_TLS_GD:
3278 return indx != 0 ? 2 : 1;
0f20cc35 3279
e641e783
RS
3280 case GOT_TLS_IE:
3281 return 1;
0f20cc35 3282
e641e783 3283 case GOT_TLS_LDM:
9143e72c 3284 return bfd_link_dll (info) ? 1 : 0;
0f20cc35 3285
e641e783
RS
3286 default:
3287 return 0;
3288 }
0f20cc35
DJ
3289}
3290
ab361d49
RS
3291/* Add the number of GOT entries and TLS relocations required by ENTRY
3292 to G. */
0f20cc35 3293
ab361d49
RS
3294static void
3295mips_elf_count_got_entry (struct bfd_link_info *info,
3296 struct mips_got_info *g,
3297 struct mips_got_entry *entry)
0f20cc35 3298{
9ab066b4 3299 if (entry->tls_type)
ab361d49 3300 {
9ab066b4
RS
3301 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3302 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
ab361d49
RS
3303 entry->symndx < 0
3304 ? &entry->d.h->root : NULL);
3305 }
3306 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3307 g->local_gotno += 1;
3308 else
3309 g->global_gotno += 1;
0f20cc35
DJ
3310}
3311
0f20cc35
DJ
3312/* Output a simple dynamic relocation into SRELOC. */
3313
3314static void
3315mips_elf_output_dynamic_relocation (bfd *output_bfd,
3316 asection *sreloc,
861fb55a 3317 unsigned long reloc_index,
0f20cc35
DJ
3318 unsigned long indx,
3319 int r_type,
3320 bfd_vma offset)
3321{
3322 Elf_Internal_Rela rel[3];
3323
3324 memset (rel, 0, sizeof (rel));
3325
3326 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3327 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3328
3329 if (ABI_64_P (output_bfd))
3330 {
3331 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3332 (output_bfd, &rel[0],
3333 (sreloc->contents
861fb55a 3334 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
3335 }
3336 else
3337 bfd_elf32_swap_reloc_out
3338 (output_bfd, &rel[0],
3339 (sreloc->contents
861fb55a 3340 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
3341}
3342
3343/* Initialize a set of TLS GOT entries for one symbol. */
3344
3345static void
9ab066b4
RS
3346mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3347 struct mips_got_entry *entry,
0f20cc35
DJ
3348 struct mips_elf_link_hash_entry *h,
3349 bfd_vma value)
3350{
1cb83cac 3351 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
23cc69b6 3352 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
3353 int indx;
3354 asection *sreloc, *sgot;
9ab066b4 3355 bfd_vma got_offset, got_offset2;
0f20cc35
DJ
3356 bfd_boolean need_relocs = FALSE;
3357
23cc69b6 3358 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3359 if (htab == NULL)
3360 return;
3361
ce558b89 3362 sgot = htab->root.sgot;
0f20cc35
DJ
3363
3364 indx = 0;
1cb83cac
MR
3365 if (h != NULL
3366 && h->root.dynindx != -1
3367 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3368 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3369 indx = h->root.dynindx;
0f20cc35 3370
9ab066b4 3371 if (entry->tls_initialized)
0f20cc35
DJ
3372 return;
3373
9143e72c 3374 if ((bfd_link_dll (info) || indx != 0)
0f20cc35
DJ
3375 && (h == NULL
3376 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3377 || h->root.type != bfd_link_hash_undefweak))
3378 need_relocs = TRUE;
3379
3380 /* MINUS_ONE means the symbol is not defined in this object. It may not
3381 be defined at all; assume that the value doesn't matter in that
3382 case. Otherwise complain if we would use the value. */
3383 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3384 || h->root.root.type == bfd_link_hash_undefweak);
3385
3386 /* Emit necessary relocations. */
0a44bf69 3387 sreloc = mips_elf_rel_dyn_section (info, FALSE);
9ab066b4 3388 got_offset = entry->gotidx;
0f20cc35 3389
9ab066b4 3390 switch (entry->tls_type)
0f20cc35 3391 {
e641e783
RS
3392 case GOT_TLS_GD:
3393 /* General Dynamic. */
3394 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
0f20cc35
DJ
3395
3396 if (need_relocs)
3397 {
3398 mips_elf_output_dynamic_relocation
861fb55a 3399 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3400 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
e641e783 3401 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3402
3403 if (indx)
3404 mips_elf_output_dynamic_relocation
861fb55a 3405 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3406 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
e641e783 3407 sgot->output_offset + sgot->output_section->vma + got_offset2);
0f20cc35
DJ
3408 else
3409 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3410 sgot->contents + got_offset2);
0f20cc35
DJ
3411 }
3412 else
3413 {
3414 MIPS_ELF_PUT_WORD (abfd, 1,
e641e783 3415 sgot->contents + got_offset);
0f20cc35 3416 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3417 sgot->contents + got_offset2);
0f20cc35 3418 }
e641e783 3419 break;
0f20cc35 3420
e641e783
RS
3421 case GOT_TLS_IE:
3422 /* Initial Exec model. */
0f20cc35
DJ
3423 if (need_relocs)
3424 {
3425 if (indx == 0)
3426 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
e641e783 3427 sgot->contents + got_offset);
0f20cc35
DJ
3428 else
3429 MIPS_ELF_PUT_WORD (abfd, 0,
e641e783 3430 sgot->contents + got_offset);
0f20cc35
DJ
3431
3432 mips_elf_output_dynamic_relocation
861fb55a 3433 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3434 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
e641e783 3435 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3436 }
3437 else
3438 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
e641e783
RS
3439 sgot->contents + got_offset);
3440 break;
0f20cc35 3441
e641e783 3442 case GOT_TLS_LDM:
0f20cc35
DJ
3443 /* The initial offset is zero, and the LD offsets will include the
3444 bias by DTP_OFFSET. */
3445 MIPS_ELF_PUT_WORD (abfd, 0,
3446 sgot->contents + got_offset
3447 + MIPS_ELF_GOT_SIZE (abfd));
3448
9143e72c 3449 if (!bfd_link_dll (info))
0f20cc35
DJ
3450 MIPS_ELF_PUT_WORD (abfd, 1,
3451 sgot->contents + got_offset);
3452 else
3453 mips_elf_output_dynamic_relocation
861fb55a 3454 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3455 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3456 sgot->output_offset + sgot->output_section->vma + got_offset);
e641e783
RS
3457 break;
3458
3459 default:
3460 abort ();
0f20cc35
DJ
3461 }
3462
9ab066b4 3463 entry->tls_initialized = TRUE;
e641e783 3464}
0f20cc35 3465
0a44bf69
RS
3466/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3467 for global symbol H. .got.plt comes before the GOT, so the offset
3468 will be negative. */
3469
3470static bfd_vma
3471mips_elf_gotplt_index (struct bfd_link_info *info,
3472 struct elf_link_hash_entry *h)
3473{
1bbce132 3474 bfd_vma got_address, got_value;
0a44bf69
RS
3475 struct mips_elf_link_hash_table *htab;
3476
3477 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3478 BFD_ASSERT (htab != NULL);
3479
1bbce132
MR
3480 BFD_ASSERT (h->plt.plist != NULL);
3481 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
0a44bf69
RS
3482
3483 /* Calculate the address of the associated .got.plt entry. */
ce558b89
AM
3484 got_address = (htab->root.sgotplt->output_section->vma
3485 + htab->root.sgotplt->output_offset
1bbce132
MR
3486 + (h->plt.plist->gotplt_index
3487 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
0a44bf69
RS
3488
3489 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3490 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3491 + htab->root.hgot->root.u.def.section->output_offset
3492 + htab->root.hgot->root.u.def.value);
3493
3494 return got_address - got_value;
3495}
3496
5c18022e 3497/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3498 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3499 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3500 offset can be found. */
b49e97c9
TS
3501
3502static bfd_vma
9719ad41 3503mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3504 bfd_vma value, unsigned long r_symndx,
0f20cc35 3505 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3506{
a8028dd0 3507 struct mips_elf_link_hash_table *htab;
b15e6682 3508 struct mips_got_entry *entry;
b49e97c9 3509
a8028dd0 3510 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3511 BFD_ASSERT (htab != NULL);
3512
a8028dd0
RS
3513 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3514 r_symndx, h, r_type);
0f20cc35 3515 if (!entry)
b15e6682 3516 return MINUS_ONE;
0f20cc35 3517
e641e783 3518 if (entry->tls_type)
9ab066b4
RS
3519 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3520 return entry->gotidx;
b49e97c9
TS
3521}
3522
13fbec83 3523/* Return the GOT index of global symbol H in the primary GOT. */
b49e97c9
TS
3524
3525static bfd_vma
13fbec83
RS
3526mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3527 struct elf_link_hash_entry *h)
3528{
3529 struct mips_elf_link_hash_table *htab;
3530 long global_got_dynindx;
3531 struct mips_got_info *g;
3532 bfd_vma got_index;
3533
3534 htab = mips_elf_hash_table (info);
3535 BFD_ASSERT (htab != NULL);
3536
3537 global_got_dynindx = 0;
3538 if (htab->global_gotsym != NULL)
3539 global_got_dynindx = htab->global_gotsym->dynindx;
3540
3541 /* Once we determine the global GOT entry with the lowest dynamic
3542 symbol table index, we must put all dynamic symbols with greater
3543 indices into the primary GOT. That makes it easy to calculate the
3544 GOT offset. */
3545 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3546 g = mips_elf_bfd_got (obfd, FALSE);
3547 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3548 * MIPS_ELF_GOT_SIZE (obfd));
ce558b89 3549 BFD_ASSERT (got_index < htab->root.sgot->size);
13fbec83
RS
3550
3551 return got_index;
3552}
3553
3554/* Return the GOT index for the global symbol indicated by H, which is
3555 referenced by a relocation of type R_TYPE in IBFD. */
3556
3557static bfd_vma
3558mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3559 struct elf_link_hash_entry *h, int r_type)
b49e97c9 3560{
a8028dd0 3561 struct mips_elf_link_hash_table *htab;
6c42ddb9
RS
3562 struct mips_got_info *g;
3563 struct mips_got_entry lookup, *entry;
3564 bfd_vma gotidx;
b49e97c9 3565
a8028dd0 3566 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3567 BFD_ASSERT (htab != NULL);
3568
6c42ddb9
RS
3569 g = mips_elf_bfd_got (ibfd, FALSE);
3570 BFD_ASSERT (g);
f4416af6 3571
6c42ddb9
RS
3572 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3573 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3574 return mips_elf_primary_global_got_index (obfd, info, h);
f4416af6 3575
6c42ddb9
RS
3576 lookup.abfd = ibfd;
3577 lookup.symndx = -1;
3578 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3579 entry = htab_find (g->got_entries, &lookup);
3580 BFD_ASSERT (entry);
0f20cc35 3581
6c42ddb9 3582 gotidx = entry->gotidx;
ce558b89 3583 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
f4416af6 3584
6c42ddb9 3585 if (lookup.tls_type)
0f20cc35 3586 {
0f20cc35
DJ
3587 bfd_vma value = MINUS_ONE;
3588
3589 if ((h->root.type == bfd_link_hash_defined
3590 || h->root.type == bfd_link_hash_defweak)
3591 && h->root.u.def.section->output_section)
3592 value = (h->root.u.def.value
3593 + h->root.u.def.section->output_offset
3594 + h->root.u.def.section->output_section->vma);
3595
9ab066b4 3596 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
0f20cc35 3597 }
6c42ddb9 3598 return gotidx;
b49e97c9
TS
3599}
3600
5c18022e
RS
3601/* Find a GOT page entry that points to within 32KB of VALUE. These
3602 entries are supposed to be placed at small offsets in the GOT, i.e.,
3603 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3604 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3605 offset of the GOT entry from VALUE. */
b49e97c9
TS
3606
3607static bfd_vma
9719ad41 3608mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3609 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3610{
91d6fa6a 3611 bfd_vma page, got_index;
b15e6682 3612 struct mips_got_entry *entry;
b49e97c9 3613
0a44bf69 3614 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3615 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3616 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3617
b15e6682
AO
3618 if (!entry)
3619 return MINUS_ONE;
143d77c5 3620
91d6fa6a 3621 got_index = entry->gotidx;
b49e97c9
TS
3622
3623 if (offsetp)
f4416af6 3624 *offsetp = value - entry->d.address;
b49e97c9 3625
91d6fa6a 3626 return got_index;
b49e97c9
TS
3627}
3628
738e5348 3629/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
020d7251
RS
3630 EXTERNAL is true if the relocation was originally against a global
3631 symbol that binds locally. */
b49e97c9
TS
3632
3633static bfd_vma
9719ad41 3634mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3635 bfd_vma value, bfd_boolean external)
b49e97c9 3636{
b15e6682 3637 struct mips_got_entry *entry;
b49e97c9 3638
0a44bf69
RS
3639 /* GOT16 relocations against local symbols are followed by a LO16
3640 relocation; those against global symbols are not. Thus if the
3641 symbol was originally local, the GOT16 relocation should load the
3642 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3643 if (! external)
0a44bf69 3644 value = mips_elf_high (value) << 16;
b49e97c9 3645
738e5348
RS
3646 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3647 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3648 same in all cases. */
a8028dd0
RS
3649 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3650 NULL, R_MIPS_GOT16);
b15e6682
AO
3651 if (entry)
3652 return entry->gotidx;
3653 else
3654 return MINUS_ONE;
b49e97c9
TS
3655}
3656
3657/* Returns the offset for the entry at the INDEXth position
3658 in the GOT. */
3659
3660static bfd_vma
a8028dd0 3661mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3662 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3663{
a8028dd0 3664 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3665 asection *sgot;
3666 bfd_vma gp;
3667
a8028dd0 3668 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3669 BFD_ASSERT (htab != NULL);
3670
ce558b89 3671 sgot = htab->root.sgot;
f4416af6 3672 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3673 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3674
91d6fa6a 3675 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3676}
3677
0a44bf69
RS
3678/* Create and return a local GOT entry for VALUE, which was calculated
3679 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3680 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3681 instead. */
b49e97c9 3682
b15e6682 3683static struct mips_got_entry *
0a44bf69 3684mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3685 bfd *ibfd, bfd_vma value,
5c18022e 3686 unsigned long r_symndx,
0f20cc35
DJ
3687 struct mips_elf_link_hash_entry *h,
3688 int r_type)
b49e97c9 3689{
ebc53538
RS
3690 struct mips_got_entry lookup, *entry;
3691 void **loc;
f4416af6 3692 struct mips_got_info *g;
0a44bf69 3693 struct mips_elf_link_hash_table *htab;
6c42ddb9 3694 bfd_vma gotidx;
0a44bf69
RS
3695
3696 htab = mips_elf_hash_table (info);
4dfe6ac6 3697 BFD_ASSERT (htab != NULL);
b15e6682 3698
d7206569 3699 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
3700 if (g == NULL)
3701 {
d7206569 3702 g = mips_elf_bfd_got (abfd, FALSE);
f4416af6
AO
3703 BFD_ASSERT (g != NULL);
3704 }
b15e6682 3705
020d7251
RS
3706 /* This function shouldn't be called for symbols that live in the global
3707 area of the GOT. */
3708 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
0f20cc35 3709
ebc53538
RS
3710 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3711 if (lookup.tls_type)
3712 {
3713 lookup.abfd = ibfd;
df58fc94 3714 if (tls_ldm_reloc_p (r_type))
0f20cc35 3715 {
ebc53538
RS
3716 lookup.symndx = 0;
3717 lookup.d.addend = 0;
0f20cc35
DJ
3718 }
3719 else if (h == NULL)
3720 {
ebc53538
RS
3721 lookup.symndx = r_symndx;
3722 lookup.d.addend = 0;
0f20cc35
DJ
3723 }
3724 else
ebc53538
RS
3725 {
3726 lookup.symndx = -1;
3727 lookup.d.h = h;
3728 }
0f20cc35 3729
ebc53538
RS
3730 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3731 BFD_ASSERT (entry);
0f20cc35 3732
6c42ddb9 3733 gotidx = entry->gotidx;
ce558b89 3734 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
6c42ddb9 3735
ebc53538 3736 return entry;
0f20cc35
DJ
3737 }
3738
ebc53538
RS
3739 lookup.abfd = NULL;
3740 lookup.symndx = -1;
3741 lookup.d.address = value;
3742 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3743 if (!loc)
b15e6682 3744 return NULL;
143d77c5 3745
ebc53538
RS
3746 entry = (struct mips_got_entry *) *loc;
3747 if (entry)
3748 return entry;
b15e6682 3749
cb22ccf4 3750 if (g->assigned_low_gotno > g->assigned_high_gotno)
b49e97c9
TS
3751 {
3752 /* We didn't allocate enough space in the GOT. */
4eca0228 3753 _bfd_error_handler
b49e97c9
TS
3754 (_("not enough GOT space for local GOT entries"));
3755 bfd_set_error (bfd_error_bad_value);
b15e6682 3756 return NULL;
b49e97c9
TS
3757 }
3758
ebc53538
RS
3759 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3760 if (!entry)
3761 return NULL;
3762
cb22ccf4
KCY
3763 if (got16_reloc_p (r_type)
3764 || call16_reloc_p (r_type)
3765 || got_page_reloc_p (r_type)
3766 || got_disp_reloc_p (r_type))
3767 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3768 else
3769 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3770
ebc53538
RS
3771 *entry = lookup;
3772 *loc = entry;
3773
ce558b89 3774 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
b15e6682 3775
5c18022e 3776 /* These GOT entries need a dynamic relocation on VxWorks. */
0a44bf69
RS
3777 if (htab->is_vxworks)
3778 {
3779 Elf_Internal_Rela outrel;
5c18022e 3780 asection *s;
91d6fa6a 3781 bfd_byte *rloc;
0a44bf69 3782 bfd_vma got_address;
0a44bf69
RS
3783
3784 s = mips_elf_rel_dyn_section (info, FALSE);
ce558b89
AM
3785 got_address = (htab->root.sgot->output_section->vma
3786 + htab->root.sgot->output_offset
ebc53538 3787 + entry->gotidx);
0a44bf69 3788
91d6fa6a 3789 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3790 outrel.r_offset = got_address;
5c18022e
RS
3791 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3792 outrel.r_addend = value;
91d6fa6a 3793 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3794 }
3795
ebc53538 3796 return entry;
b49e97c9
TS
3797}
3798
d4596a51
RS
3799/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3800 The number might be exact or a worst-case estimate, depending on how
3801 much information is available to elf_backend_omit_section_dynsym at
3802 the current linking stage. */
3803
3804static bfd_size_type
3805count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3806{
3807 bfd_size_type count;
3808
3809 count = 0;
0e1862bb
L
3810 if (bfd_link_pic (info)
3811 || elf_hash_table (info)->is_relocatable_executable)
d4596a51
RS
3812 {
3813 asection *p;
3814 const struct elf_backend_data *bed;
3815
3816 bed = get_elf_backend_data (output_bfd);
3817 for (p = output_bfd->sections; p ; p = p->next)
3818 if ((p->flags & SEC_EXCLUDE) == 0
3819 && (p->flags & SEC_ALLOC) != 0
7f923b7f 3820 && elf_hash_table (info)->dynamic_relocs
d4596a51
RS
3821 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3822 ++count;
3823 }
3824 return count;
3825}
3826
b49e97c9 3827/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3828 appear towards the end. */
b49e97c9 3829
b34976b6 3830static bfd_boolean
d4596a51 3831mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3832{
a8028dd0 3833 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3834 struct mips_elf_hash_sort_data hsd;
3835 struct mips_got_info *g;
b49e97c9 3836
a8028dd0 3837 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3838 BFD_ASSERT (htab != NULL);
3839
0f8c4b60 3840 if (htab->root.dynsymcount == 0)
17a80fa8
MR
3841 return TRUE;
3842
a8028dd0 3843 g = htab->got_info;
d4596a51
RS
3844 if (g == NULL)
3845 return TRUE;
f4416af6 3846
b49e97c9 3847 hsd.low = NULL;
23cc69b6
RS
3848 hsd.max_unref_got_dynindx
3849 = hsd.min_got_dynindx
0f8c4b60 3850 = (htab->root.dynsymcount - g->reloc_only_gotno);
e17b0c35
MR
3851 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3852 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3853 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3854 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
0f8c4b60 3855 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
b49e97c9
TS
3856
3857 /* There should have been enough room in the symbol table to
44c410de 3858 accommodate both the GOT and non-GOT symbols. */
e17b0c35 3859 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
b49e97c9 3860 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
55f8b9d2 3861 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
0f8c4b60 3862 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
b49e97c9
TS
3863
3864 /* Now we know which dynamic symbol has the lowest dynamic symbol
3865 table index in the GOT. */
d222d210 3866 htab->global_gotsym = hsd.low;
b49e97c9 3867
b34976b6 3868 return TRUE;
b49e97c9
TS
3869}
3870
3871/* If H needs a GOT entry, assign it the highest available dynamic
3872 index. Otherwise, assign it the lowest available dynamic
3873 index. */
3874
b34976b6 3875static bfd_boolean
9719ad41 3876mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3877{
9719ad41 3878 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9 3879
b49e97c9
TS
3880 /* Symbols without dynamic symbol table entries aren't interesting
3881 at all. */
3882 if (h->root.dynindx == -1)
b34976b6 3883 return TRUE;
b49e97c9 3884
634835ae 3885 switch (h->global_got_area)
f4416af6 3886 {
634835ae 3887 case GGA_NONE:
e17b0c35
MR
3888 if (h->root.forced_local)
3889 h->root.dynindx = hsd->max_local_dynindx++;
3890 else
3891 h->root.dynindx = hsd->max_non_got_dynindx++;
634835ae 3892 break;
0f20cc35 3893
634835ae 3894 case GGA_NORMAL:
b49e97c9
TS
3895 h->root.dynindx = --hsd->min_got_dynindx;
3896 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3897 break;
3898
3899 case GGA_RELOC_ONLY:
634835ae
RS
3900 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3901 hsd->low = (struct elf_link_hash_entry *) h;
3902 h->root.dynindx = hsd->max_unref_got_dynindx++;
3903 break;
b49e97c9
TS
3904 }
3905
b34976b6 3906 return TRUE;
b49e97c9
TS
3907}
3908
ee227692
RS
3909/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3910 (which is owned by the caller and shouldn't be added to the
3911 hash table directly). */
3912
3913static bfd_boolean
3914mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3915 struct mips_got_entry *lookup)
3916{
3917 struct mips_elf_link_hash_table *htab;
3918 struct mips_got_entry *entry;
3919 struct mips_got_info *g;
3920 void **loc, **bfd_loc;
3921
3922 /* Make sure there's a slot for this entry in the master GOT. */
3923 htab = mips_elf_hash_table (info);
3924 g = htab->got_info;
3925 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3926 if (!loc)
3927 return FALSE;
3928
3929 /* Populate the entry if it isn't already. */
3930 entry = (struct mips_got_entry *) *loc;
3931 if (!entry)
3932 {
3933 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3934 if (!entry)
3935 return FALSE;
3936
9ab066b4 3937 lookup->tls_initialized = FALSE;
ee227692
RS
3938 lookup->gotidx = -1;
3939 *entry = *lookup;
3940 *loc = entry;
3941 }
3942
3943 /* Reuse the same GOT entry for the BFD's GOT. */
3944 g = mips_elf_bfd_got (abfd, TRUE);
3945 if (!g)
3946 return FALSE;
3947
3948 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
3949 if (!bfd_loc)
3950 return FALSE;
3951
3952 if (!*bfd_loc)
3953 *bfd_loc = entry;
3954 return TRUE;
3955}
3956
e641e783
RS
3957/* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
3958 entry for it. FOR_CALL is true if the caller is only interested in
6ccf4795 3959 using the GOT entry for calls. */
b49e97c9 3960
b34976b6 3961static bfd_boolean
9719ad41
RS
3962mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
3963 bfd *abfd, struct bfd_link_info *info,
e641e783 3964 bfd_boolean for_call, int r_type)
b49e97c9 3965{
a8028dd0 3966 struct mips_elf_link_hash_table *htab;
634835ae 3967 struct mips_elf_link_hash_entry *hmips;
ee227692
RS
3968 struct mips_got_entry entry;
3969 unsigned char tls_type;
a8028dd0
RS
3970
3971 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3972 BFD_ASSERT (htab != NULL);
3973
634835ae 3974 hmips = (struct mips_elf_link_hash_entry *) h;
6ccf4795
RS
3975 if (!for_call)
3976 hmips->got_only_for_calls = FALSE;
f4416af6 3977
b49e97c9
TS
3978 /* A global symbol in the GOT must also be in the dynamic symbol
3979 table. */
7c5fcef7
L
3980 if (h->dynindx == -1)
3981 {
3982 switch (ELF_ST_VISIBILITY (h->other))
3983 {
3984 case STV_INTERNAL:
3985 case STV_HIDDEN:
47275900 3986 _bfd_mips_elf_hide_symbol (info, h, TRUE);
7c5fcef7
L
3987 break;
3988 }
c152c796 3989 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 3990 return FALSE;
7c5fcef7 3991 }
b49e97c9 3992
ee227692 3993 tls_type = mips_elf_reloc_tls_type (r_type);
9ab066b4 3994 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
ee227692 3995 hmips->global_got_area = GGA_NORMAL;
86324f90 3996
f4416af6
AO
3997 entry.abfd = abfd;
3998 entry.symndx = -1;
3999 entry.d.h = (struct mips_elf_link_hash_entry *) h;
ee227692
RS
4000 entry.tls_type = tls_type;
4001 return mips_elf_record_got_entry (info, abfd, &entry);
b49e97c9 4002}
f4416af6 4003
e641e783
RS
4004/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4005 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
f4416af6
AO
4006
4007static bfd_boolean
9719ad41 4008mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
e641e783 4009 struct bfd_link_info *info, int r_type)
f4416af6 4010{
a8028dd0
RS
4011 struct mips_elf_link_hash_table *htab;
4012 struct mips_got_info *g;
ee227692 4013 struct mips_got_entry entry;
f4416af6 4014
a8028dd0 4015 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4016 BFD_ASSERT (htab != NULL);
4017
a8028dd0
RS
4018 g = htab->got_info;
4019 BFD_ASSERT (g != NULL);
4020
f4416af6
AO
4021 entry.abfd = abfd;
4022 entry.symndx = symndx;
4023 entry.d.addend = addend;
e641e783 4024 entry.tls_type = mips_elf_reloc_tls_type (r_type);
ee227692 4025 return mips_elf_record_got_entry (info, abfd, &entry);
f4416af6 4026}
c224138d 4027
13db6b44
RS
4028/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4029 H is the symbol's hash table entry, or null if SYMNDX is local
4030 to ABFD. */
c224138d
RS
4031
4032static bfd_boolean
13db6b44
RS
4033mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4034 long symndx, struct elf_link_hash_entry *h,
4035 bfd_signed_vma addend)
c224138d 4036{
a8028dd0 4037 struct mips_elf_link_hash_table *htab;
ee227692 4038 struct mips_got_info *g1, *g2;
13db6b44 4039 struct mips_got_page_ref lookup, *entry;
ee227692 4040 void **loc, **bfd_loc;
c224138d 4041
a8028dd0 4042 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4043 BFD_ASSERT (htab != NULL);
4044
ee227692
RS
4045 g1 = htab->got_info;
4046 BFD_ASSERT (g1 != NULL);
a8028dd0 4047
13db6b44
RS
4048 if (h)
4049 {
4050 lookup.symndx = -1;
4051 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4052 }
4053 else
4054 {
4055 lookup.symndx = symndx;
4056 lookup.u.abfd = abfd;
4057 }
4058 lookup.addend = addend;
4059 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
c224138d
RS
4060 if (loc == NULL)
4061 return FALSE;
4062
13db6b44 4063 entry = (struct mips_got_page_ref *) *loc;
c224138d
RS
4064 if (!entry)
4065 {
4066 entry = bfd_alloc (abfd, sizeof (*entry));
4067 if (!entry)
4068 return FALSE;
4069
13db6b44 4070 *entry = lookup;
c224138d
RS
4071 *loc = entry;
4072 }
4073
ee227692
RS
4074 /* Add the same entry to the BFD's GOT. */
4075 g2 = mips_elf_bfd_got (abfd, TRUE);
4076 if (!g2)
4077 return FALSE;
4078
13db6b44 4079 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
ee227692
RS
4080 if (!bfd_loc)
4081 return FALSE;
4082
4083 if (!*bfd_loc)
4084 *bfd_loc = entry;
4085
c224138d
RS
4086 return TRUE;
4087}
33bb52fb
RS
4088
4089/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4090
4091static void
4092mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4093 unsigned int n)
4094{
4095 asection *s;
4096 struct mips_elf_link_hash_table *htab;
4097
4098 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4099 BFD_ASSERT (htab != NULL);
4100
33bb52fb
RS
4101 s = mips_elf_rel_dyn_section (info, FALSE);
4102 BFD_ASSERT (s != NULL);
4103
4104 if (htab->is_vxworks)
4105 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4106 else
4107 {
4108 if (s->size == 0)
4109 {
4110 /* Make room for a null element. */
4111 s->size += MIPS_ELF_REL_SIZE (abfd);
4112 ++s->reloc_count;
4113 }
4114 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4115 }
4116}
4117\f
476366af
RS
4118/* A htab_traverse callback for GOT entries, with DATA pointing to a
4119 mips_elf_traverse_got_arg structure. Count the number of GOT
4120 entries and TLS relocs. Set DATA->value to true if we need
4121 to resolve indirect or warning symbols and then recreate the GOT. */
33bb52fb
RS
4122
4123static int
4124mips_elf_check_recreate_got (void **entryp, void *data)
4125{
4126 struct mips_got_entry *entry;
476366af 4127 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4128
4129 entry = (struct mips_got_entry *) *entryp;
476366af 4130 arg = (struct mips_elf_traverse_got_arg *) data;
33bb52fb
RS
4131 if (entry->abfd != NULL && entry->symndx == -1)
4132 {
4133 struct mips_elf_link_hash_entry *h;
4134
4135 h = entry->d.h;
4136 if (h->root.root.type == bfd_link_hash_indirect
4137 || h->root.root.type == bfd_link_hash_warning)
4138 {
476366af 4139 arg->value = TRUE;
33bb52fb
RS
4140 return 0;
4141 }
4142 }
476366af 4143 mips_elf_count_got_entry (arg->info, arg->g, entry);
33bb52fb
RS
4144 return 1;
4145}
4146
476366af
RS
4147/* A htab_traverse callback for GOT entries, with DATA pointing to a
4148 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4149 converting entries for indirect and warning symbols into entries
4150 for the target symbol. Set DATA->g to null on error. */
33bb52fb
RS
4151
4152static int
4153mips_elf_recreate_got (void **entryp, void *data)
4154{
72e7511a 4155 struct mips_got_entry new_entry, *entry;
476366af 4156 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4157 void **slot;
4158
33bb52fb 4159 entry = (struct mips_got_entry *) *entryp;
476366af 4160 arg = (struct mips_elf_traverse_got_arg *) data;
72e7511a
RS
4161 if (entry->abfd != NULL
4162 && entry->symndx == -1
4163 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4164 || entry->d.h->root.root.type == bfd_link_hash_warning))
33bb52fb
RS
4165 {
4166 struct mips_elf_link_hash_entry *h;
4167
72e7511a
RS
4168 new_entry = *entry;
4169 entry = &new_entry;
33bb52fb 4170 h = entry->d.h;
72e7511a 4171 do
634835ae
RS
4172 {
4173 BFD_ASSERT (h->global_got_area == GGA_NONE);
4174 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4175 }
72e7511a
RS
4176 while (h->root.root.type == bfd_link_hash_indirect
4177 || h->root.root.type == bfd_link_hash_warning);
33bb52fb
RS
4178 entry->d.h = h;
4179 }
476366af 4180 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
33bb52fb
RS
4181 if (slot == NULL)
4182 {
476366af 4183 arg->g = NULL;
33bb52fb
RS
4184 return 0;
4185 }
4186 if (*slot == NULL)
72e7511a
RS
4187 {
4188 if (entry == &new_entry)
4189 {
4190 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4191 if (!entry)
4192 {
476366af 4193 arg->g = NULL;
72e7511a
RS
4194 return 0;
4195 }
4196 *entry = new_entry;
4197 }
4198 *slot = entry;
476366af 4199 mips_elf_count_got_entry (arg->info, arg->g, entry);
72e7511a 4200 }
33bb52fb
RS
4201 return 1;
4202}
4203
13db6b44
RS
4204/* Return the maximum number of GOT page entries required for RANGE. */
4205
4206static bfd_vma
4207mips_elf_pages_for_range (const struct mips_got_page_range *range)
4208{
4209 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4210}
4211
4212/* Record that G requires a page entry that can reach SEC + ADDEND. */
4213
4214static bfd_boolean
b75d42bc 4215mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
13db6b44
RS
4216 asection *sec, bfd_signed_vma addend)
4217{
b75d42bc 4218 struct mips_got_info *g = arg->g;
13db6b44
RS
4219 struct mips_got_page_entry lookup, *entry;
4220 struct mips_got_page_range **range_ptr, *range;
4221 bfd_vma old_pages, new_pages;
4222 void **loc;
4223
4224 /* Find the mips_got_page_entry hash table entry for this section. */
4225 lookup.sec = sec;
4226 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4227 if (loc == NULL)
4228 return FALSE;
4229
4230 /* Create a mips_got_page_entry if this is the first time we've
4231 seen the section. */
4232 entry = (struct mips_got_page_entry *) *loc;
4233 if (!entry)
4234 {
b75d42bc 4235 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
13db6b44
RS
4236 if (!entry)
4237 return FALSE;
4238
4239 entry->sec = sec;
4240 *loc = entry;
4241 }
4242
4243 /* Skip over ranges whose maximum extent cannot share a page entry
4244 with ADDEND. */
4245 range_ptr = &entry->ranges;
4246 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4247 range_ptr = &(*range_ptr)->next;
4248
4249 /* If we scanned to the end of the list, or found a range whose
4250 minimum extent cannot share a page entry with ADDEND, create
4251 a new singleton range. */
4252 range = *range_ptr;
4253 if (!range || addend < range->min_addend - 0xffff)
4254 {
b75d42bc 4255 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
13db6b44
RS
4256 if (!range)
4257 return FALSE;
4258
4259 range->next = *range_ptr;
4260 range->min_addend = addend;
4261 range->max_addend = addend;
4262
4263 *range_ptr = range;
4264 entry->num_pages++;
4265 g->page_gotno++;
4266 return TRUE;
4267 }
4268
4269 /* Remember how many pages the old range contributed. */
4270 old_pages = mips_elf_pages_for_range (range);
4271
4272 /* Update the ranges. */
4273 if (addend < range->min_addend)
4274 range->min_addend = addend;
4275 else if (addend > range->max_addend)
4276 {
4277 if (range->next && addend >= range->next->min_addend - 0xffff)
4278 {
4279 old_pages += mips_elf_pages_for_range (range->next);
4280 range->max_addend = range->next->max_addend;
4281 range->next = range->next->next;
4282 }
4283 else
4284 range->max_addend = addend;
4285 }
4286
4287 /* Record any change in the total estimate. */
4288 new_pages = mips_elf_pages_for_range (range);
4289 if (old_pages != new_pages)
4290 {
4291 entry->num_pages += new_pages - old_pages;
4292 g->page_gotno += new_pages - old_pages;
4293 }
4294
4295 return TRUE;
4296}
4297
4298/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4299 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4300 whether the page reference described by *REFP needs a GOT page entry,
4301 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4302
4303static bfd_boolean
4304mips_elf_resolve_got_page_ref (void **refp, void *data)
4305{
4306 struct mips_got_page_ref *ref;
4307 struct mips_elf_traverse_got_arg *arg;
4308 struct mips_elf_link_hash_table *htab;
4309 asection *sec;
4310 bfd_vma addend;
4311
4312 ref = (struct mips_got_page_ref *) *refp;
4313 arg = (struct mips_elf_traverse_got_arg *) data;
4314 htab = mips_elf_hash_table (arg->info);
4315
4316 if (ref->symndx < 0)
4317 {
4318 struct mips_elf_link_hash_entry *h;
4319
4320 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4321 h = ref->u.h;
4322 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4323 return 1;
4324
4325 /* Ignore undefined symbols; we'll issue an error later if
4326 appropriate. */
4327 if (!((h->root.root.type == bfd_link_hash_defined
4328 || h->root.root.type == bfd_link_hash_defweak)
4329 && h->root.root.u.def.section))
4330 return 1;
4331
4332 sec = h->root.root.u.def.section;
4333 addend = h->root.root.u.def.value + ref->addend;
4334 }
4335 else
4336 {
4337 Elf_Internal_Sym *isym;
4338
4339 /* Read in the symbol. */
4340 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ref->u.abfd,
4341 ref->symndx);
4342 if (isym == NULL)
4343 {
4344 arg->g = NULL;
4345 return 0;
4346 }
4347
4348 /* Get the associated input section. */
4349 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4350 if (sec == NULL)
4351 {
4352 arg->g = NULL;
4353 return 0;
4354 }
4355
4356 /* If this is a mergable section, work out the section and offset
4357 of the merged data. For section symbols, the addend specifies
4358 of the offset _of_ the first byte in the data, otherwise it
4359 specifies the offset _from_ the first byte. */
4360 if (sec->flags & SEC_MERGE)
4361 {
4362 void *secinfo;
4363
4364 secinfo = elf_section_data (sec)->sec_info;
4365 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4366 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4367 isym->st_value + ref->addend);
4368 else
4369 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4370 isym->st_value) + ref->addend;
4371 }
4372 else
4373 addend = isym->st_value + ref->addend;
4374 }
b75d42bc 4375 if (!mips_elf_record_got_page_entry (arg, sec, addend))
13db6b44
RS
4376 {
4377 arg->g = NULL;
4378 return 0;
4379 }
4380 return 1;
4381}
4382
33bb52fb 4383/* If any entries in G->got_entries are for indirect or warning symbols,
13db6b44
RS
4384 replace them with entries for the target symbol. Convert g->got_page_refs
4385 into got_page_entry structures and estimate the number of page entries
4386 that they require. */
33bb52fb
RS
4387
4388static bfd_boolean
476366af
RS
4389mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4390 struct mips_got_info *g)
33bb52fb 4391{
476366af
RS
4392 struct mips_elf_traverse_got_arg tga;
4393 struct mips_got_info oldg;
4394
4395 oldg = *g;
33bb52fb 4396
476366af
RS
4397 tga.info = info;
4398 tga.g = g;
4399 tga.value = FALSE;
4400 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4401 if (tga.value)
33bb52fb 4402 {
476366af
RS
4403 *g = oldg;
4404 g->got_entries = htab_create (htab_size (oldg.got_entries),
4405 mips_elf_got_entry_hash,
4406 mips_elf_got_entry_eq, NULL);
4407 if (!g->got_entries)
33bb52fb
RS
4408 return FALSE;
4409
476366af
RS
4410 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4411 if (!tga.g)
4412 return FALSE;
4413
4414 htab_delete (oldg.got_entries);
33bb52fb 4415 }
13db6b44
RS
4416
4417 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4418 mips_got_page_entry_eq, NULL);
4419 if (g->got_page_entries == NULL)
4420 return FALSE;
4421
4422 tga.info = info;
4423 tga.g = g;
4424 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4425
33bb52fb
RS
4426 return TRUE;
4427}
4428
c5d6fa44
RS
4429/* Return true if a GOT entry for H should live in the local rather than
4430 global GOT area. */
4431
4432static bfd_boolean
4433mips_use_local_got_p (struct bfd_link_info *info,
4434 struct mips_elf_link_hash_entry *h)
4435{
4436 /* Symbols that aren't in the dynamic symbol table must live in the
4437 local GOT. This includes symbols that are completely undefined
4438 and which therefore don't bind locally. We'll report undefined
4439 symbols later if appropriate. */
4440 if (h->root.dynindx == -1)
4441 return TRUE;
4442
47275900
MR
4443 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4444 to the local GOT, as they would be implicitly relocated by the
4445 base address by the dynamic loader. */
4446 if (bfd_is_abs_symbol (&h->root.root))
4447 return FALSE;
4448
c5d6fa44
RS
4449 /* Symbols that bind locally can (and in the case of forced-local
4450 symbols, must) live in the local GOT. */
4451 if (h->got_only_for_calls
4452 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4453 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4454 return TRUE;
4455
4456 /* If this is an executable that must provide a definition of the symbol,
4457 either though PLTs or copy relocations, then that address should go in
4458 the local rather than global GOT. */
0e1862bb 4459 if (bfd_link_executable (info) && h->has_static_relocs)
c5d6fa44
RS
4460 return TRUE;
4461
4462 return FALSE;
4463}
4464
6c42ddb9
RS
4465/* A mips_elf_link_hash_traverse callback for which DATA points to the
4466 link_info structure. Decide whether the hash entry needs an entry in
4467 the global part of the primary GOT, setting global_got_area accordingly.
4468 Count the number of global symbols that are in the primary GOT only
4469 because they have relocations against them (reloc_only_gotno). */
33bb52fb
RS
4470
4471static int
d4596a51 4472mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 4473{
020d7251 4474 struct bfd_link_info *info;
6ccf4795 4475 struct mips_elf_link_hash_table *htab;
33bb52fb
RS
4476 struct mips_got_info *g;
4477
020d7251 4478 info = (struct bfd_link_info *) data;
6ccf4795
RS
4479 htab = mips_elf_hash_table (info);
4480 g = htab->got_info;
d4596a51 4481 if (h->global_got_area != GGA_NONE)
33bb52fb 4482 {
020d7251 4483 /* Make a final decision about whether the symbol belongs in the
c5d6fa44
RS
4484 local or global GOT. */
4485 if (mips_use_local_got_p (info, h))
6c42ddb9
RS
4486 /* The symbol belongs in the local GOT. We no longer need this
4487 entry if it was only used for relocations; those relocations
4488 will be against the null or section symbol instead of H. */
4489 h->global_got_area = GGA_NONE;
6ccf4795
RS
4490 else if (htab->is_vxworks
4491 && h->got_only_for_calls
1bbce132 4492 && h->root.plt.plist->mips_offset != MINUS_ONE)
6ccf4795
RS
4493 /* On VxWorks, calls can refer directly to the .got.plt entry;
4494 they don't need entries in the regular GOT. .got.plt entries
4495 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4496 h->global_got_area = GGA_NONE;
6c42ddb9 4497 else if (h->global_got_area == GGA_RELOC_ONLY)
23cc69b6 4498 {
6c42ddb9 4499 g->reloc_only_gotno++;
23cc69b6 4500 g->global_gotno++;
23cc69b6 4501 }
33bb52fb
RS
4502 }
4503 return 1;
4504}
f4416af6 4505\f
d7206569
RS
4506/* A htab_traverse callback for GOT entries. Add each one to the GOT
4507 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
f4416af6
AO
4508
4509static int
d7206569 4510mips_elf_add_got_entry (void **entryp, void *data)
f4416af6 4511{
d7206569
RS
4512 struct mips_got_entry *entry;
4513 struct mips_elf_traverse_got_arg *arg;
4514 void **slot;
f4416af6 4515
d7206569
RS
4516 entry = (struct mips_got_entry *) *entryp;
4517 arg = (struct mips_elf_traverse_got_arg *) data;
4518 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4519 if (!slot)
f4416af6 4520 {
d7206569
RS
4521 arg->g = NULL;
4522 return 0;
f4416af6 4523 }
d7206569 4524 if (!*slot)
c224138d 4525 {
d7206569
RS
4526 *slot = entry;
4527 mips_elf_count_got_entry (arg->info, arg->g, entry);
c224138d 4528 }
f4416af6
AO
4529 return 1;
4530}
4531
d7206569
RS
4532/* A htab_traverse callback for GOT page entries. Add each one to the GOT
4533 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
c224138d
RS
4534
4535static int
d7206569 4536mips_elf_add_got_page_entry (void **entryp, void *data)
c224138d 4537{
d7206569
RS
4538 struct mips_got_page_entry *entry;
4539 struct mips_elf_traverse_got_arg *arg;
4540 void **slot;
c224138d 4541
d7206569
RS
4542 entry = (struct mips_got_page_entry *) *entryp;
4543 arg = (struct mips_elf_traverse_got_arg *) data;
4544 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4545 if (!slot)
c224138d 4546 {
d7206569 4547 arg->g = NULL;
c224138d
RS
4548 return 0;
4549 }
d7206569
RS
4550 if (!*slot)
4551 {
4552 *slot = entry;
4553 arg->g->page_gotno += entry->num_pages;
4554 }
c224138d
RS
4555 return 1;
4556}
4557
d7206569
RS
4558/* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4559 this would lead to overflow, 1 if they were merged successfully,
4560 and 0 if a merge failed due to lack of memory. (These values are chosen
4561 so that nonnegative return values can be returned by a htab_traverse
4562 callback.) */
c224138d
RS
4563
4564static int
d7206569 4565mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
c224138d
RS
4566 struct mips_got_info *to,
4567 struct mips_elf_got_per_bfd_arg *arg)
4568{
d7206569 4569 struct mips_elf_traverse_got_arg tga;
c224138d
RS
4570 unsigned int estimate;
4571
4572 /* Work out how many page entries we would need for the combined GOT. */
4573 estimate = arg->max_pages;
4574 if (estimate >= from->page_gotno + to->page_gotno)
4575 estimate = from->page_gotno + to->page_gotno;
4576
e2ece73c 4577 /* And conservatively estimate how many local and TLS entries
c224138d 4578 would be needed. */
e2ece73c
RS
4579 estimate += from->local_gotno + to->local_gotno;
4580 estimate += from->tls_gotno + to->tls_gotno;
4581
17214937
RS
4582 /* If we're merging with the primary got, any TLS relocations will
4583 come after the full set of global entries. Otherwise estimate those
e2ece73c 4584 conservatively as well. */
17214937 4585 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
e2ece73c
RS
4586 estimate += arg->global_count;
4587 else
4588 estimate += from->global_gotno + to->global_gotno;
c224138d
RS
4589
4590 /* Bail out if the combined GOT might be too big. */
4591 if (estimate > arg->max_count)
4592 return -1;
4593
c224138d 4594 /* Transfer the bfd's got information from FROM to TO. */
d7206569
RS
4595 tga.info = arg->info;
4596 tga.g = to;
4597 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4598 if (!tga.g)
c224138d
RS
4599 return 0;
4600
d7206569
RS
4601 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4602 if (!tga.g)
c224138d
RS
4603 return 0;
4604
d7206569 4605 mips_elf_replace_bfd_got (abfd, to);
c224138d
RS
4606 return 1;
4607}
4608
d7206569 4609/* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
f4416af6
AO
4610 as possible of the primary got, since it doesn't require explicit
4611 dynamic relocations, but don't use bfds that would reference global
4612 symbols out of the addressable range. Failing the primary got,
4613 attempt to merge with the current got, or finish the current got
4614 and then make make the new got current. */
4615
d7206569
RS
4616static bfd_boolean
4617mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4618 struct mips_elf_got_per_bfd_arg *arg)
f4416af6 4619{
c224138d
RS
4620 unsigned int estimate;
4621 int result;
4622
476366af 4623 if (!mips_elf_resolve_final_got_entries (arg->info, g))
d7206569
RS
4624 return FALSE;
4625
c224138d
RS
4626 /* Work out the number of page, local and TLS entries. */
4627 estimate = arg->max_pages;
4628 if (estimate > g->page_gotno)
4629 estimate = g->page_gotno;
4630 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4631
4632 /* We place TLS GOT entries after both locals and globals. The globals
4633 for the primary GOT may overflow the normal GOT size limit, so be
4634 sure not to merge a GOT which requires TLS with the primary GOT in that
4635 case. This doesn't affect non-primary GOTs. */
c224138d 4636 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4637
c224138d 4638 if (estimate <= arg->max_count)
f4416af6 4639 {
c224138d
RS
4640 /* If we don't have a primary GOT, use it as
4641 a starting point for the primary GOT. */
4642 if (!arg->primary)
4643 {
d7206569
RS
4644 arg->primary = g;
4645 return TRUE;
c224138d 4646 }
f4416af6 4647
c224138d 4648 /* Try merging with the primary GOT. */
d7206569 4649 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
c224138d
RS
4650 if (result >= 0)
4651 return result;
f4416af6 4652 }
c224138d 4653
f4416af6 4654 /* If we can merge with the last-created got, do it. */
c224138d 4655 if (arg->current)
f4416af6 4656 {
d7206569 4657 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
c224138d
RS
4658 if (result >= 0)
4659 return result;
f4416af6 4660 }
c224138d 4661
f4416af6
AO
4662 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4663 fits; if it turns out that it doesn't, we'll get relocation
4664 overflows anyway. */
c224138d
RS
4665 g->next = arg->current;
4666 arg->current = g;
0f20cc35 4667
d7206569 4668 return TRUE;
0f20cc35
DJ
4669}
4670
72e7511a
RS
4671/* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4672 to GOTIDX, duplicating the entry if it has already been assigned
4673 an index in a different GOT. */
4674
4675static bfd_boolean
4676mips_elf_set_gotidx (void **entryp, long gotidx)
4677{
4678 struct mips_got_entry *entry;
4679
4680 entry = (struct mips_got_entry *) *entryp;
4681 if (entry->gotidx > 0)
4682 {
4683 struct mips_got_entry *new_entry;
4684
4685 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4686 if (!new_entry)
4687 return FALSE;
4688
4689 *new_entry = *entry;
4690 *entryp = new_entry;
4691 entry = new_entry;
4692 }
4693 entry->gotidx = gotidx;
4694 return TRUE;
4695}
4696
4697/* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4698 mips_elf_traverse_got_arg in which DATA->value is the size of one
4699 GOT entry. Set DATA->g to null on failure. */
0f20cc35
DJ
4700
4701static int
72e7511a 4702mips_elf_initialize_tls_index (void **entryp, void *data)
0f20cc35 4703{
72e7511a
RS
4704 struct mips_got_entry *entry;
4705 struct mips_elf_traverse_got_arg *arg;
0f20cc35
DJ
4706
4707 /* We're only interested in TLS symbols. */
72e7511a 4708 entry = (struct mips_got_entry *) *entryp;
9ab066b4 4709 if (entry->tls_type == GOT_TLS_NONE)
0f20cc35
DJ
4710 return 1;
4711
72e7511a 4712 arg = (struct mips_elf_traverse_got_arg *) data;
6c42ddb9 4713 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
ead49a57 4714 {
6c42ddb9
RS
4715 arg->g = NULL;
4716 return 0;
f4416af6
AO
4717 }
4718
ead49a57 4719 /* Account for the entries we've just allocated. */
9ab066b4 4720 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
f4416af6
AO
4721 return 1;
4722}
4723
ab361d49
RS
4724/* A htab_traverse callback for GOT entries, where DATA points to a
4725 mips_elf_traverse_got_arg. Set the global_got_area of each global
4726 symbol to DATA->value. */
f4416af6 4727
f4416af6 4728static int
ab361d49 4729mips_elf_set_global_got_area (void **entryp, void *data)
f4416af6 4730{
ab361d49
RS
4731 struct mips_got_entry *entry;
4732 struct mips_elf_traverse_got_arg *arg;
f4416af6 4733
ab361d49
RS
4734 entry = (struct mips_got_entry *) *entryp;
4735 arg = (struct mips_elf_traverse_got_arg *) data;
4736 if (entry->abfd != NULL
4737 && entry->symndx == -1
4738 && entry->d.h->global_got_area != GGA_NONE)
4739 entry->d.h->global_got_area = arg->value;
4740 return 1;
4741}
4742
4743/* A htab_traverse callback for secondary GOT entries, where DATA points
4744 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4745 and record the number of relocations they require. DATA->value is
72e7511a 4746 the size of one GOT entry. Set DATA->g to null on failure. */
ab361d49
RS
4747
4748static int
4749mips_elf_set_global_gotidx (void **entryp, void *data)
4750{
4751 struct mips_got_entry *entry;
4752 struct mips_elf_traverse_got_arg *arg;
0f20cc35 4753
ab361d49
RS
4754 entry = (struct mips_got_entry *) *entryp;
4755 arg = (struct mips_elf_traverse_got_arg *) data;
634835ae
RS
4756 if (entry->abfd != NULL
4757 && entry->symndx == -1
4758 && entry->d.h->global_got_area != GGA_NONE)
f4416af6 4759 {
cb22ccf4 4760 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
72e7511a
RS
4761 {
4762 arg->g = NULL;
4763 return 0;
4764 }
cb22ccf4 4765 arg->g->assigned_low_gotno += 1;
72e7511a 4766
0e1862bb 4767 if (bfd_link_pic (arg->info)
ab361d49
RS
4768 || (elf_hash_table (arg->info)->dynamic_sections_created
4769 && entry->d.h->root.def_dynamic
4770 && !entry->d.h->root.def_regular))
4771 arg->g->relocs += 1;
f4416af6
AO
4772 }
4773
4774 return 1;
4775}
4776
33bb52fb
RS
4777/* A htab_traverse callback for GOT entries for which DATA is the
4778 bfd_link_info. Forbid any global symbols from having traditional
4779 lazy-binding stubs. */
4780
0626d451 4781static int
33bb52fb 4782mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4783{
33bb52fb
RS
4784 struct bfd_link_info *info;
4785 struct mips_elf_link_hash_table *htab;
4786 struct mips_got_entry *entry;
0626d451 4787
33bb52fb
RS
4788 entry = (struct mips_got_entry *) *entryp;
4789 info = (struct bfd_link_info *) data;
4790 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4791 BFD_ASSERT (htab != NULL);
4792
0626d451
RS
4793 if (entry->abfd != NULL
4794 && entry->symndx == -1
33bb52fb 4795 && entry->d.h->needs_lazy_stub)
f4416af6 4796 {
33bb52fb
RS
4797 entry->d.h->needs_lazy_stub = FALSE;
4798 htab->lazy_stub_count--;
f4416af6 4799 }
143d77c5 4800
f4416af6
AO
4801 return 1;
4802}
4803
f4416af6
AO
4804/* Return the offset of an input bfd IBFD's GOT from the beginning of
4805 the primary GOT. */
4806static bfd_vma
9719ad41 4807mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6 4808{
d7206569 4809 if (!g->next)
f4416af6
AO
4810 return 0;
4811
d7206569 4812 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
4813 if (! g)
4814 return 0;
4815
4816 BFD_ASSERT (g->next);
4817
4818 g = g->next;
143d77c5 4819
0f20cc35
DJ
4820 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4821 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4822}
4823
4824/* Turn a single GOT that is too big for 16-bit addressing into
4825 a sequence of GOTs, each one 16-bit addressable. */
4826
4827static bfd_boolean
9719ad41 4828mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4829 asection *got, bfd_size_type pages)
f4416af6 4830{
a8028dd0 4831 struct mips_elf_link_hash_table *htab;
f4416af6 4832 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
ab361d49 4833 struct mips_elf_traverse_got_arg tga;
a8028dd0 4834 struct mips_got_info *g, *gg;
33bb52fb 4835 unsigned int assign, needed_relocs;
d7206569 4836 bfd *dynobj, *ibfd;
f4416af6 4837
33bb52fb 4838 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4839 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4840 BFD_ASSERT (htab != NULL);
4841
a8028dd0 4842 g = htab->got_info;
f4416af6 4843
f4416af6
AO
4844 got_per_bfd_arg.obfd = abfd;
4845 got_per_bfd_arg.info = info;
f4416af6
AO
4846 got_per_bfd_arg.current = NULL;
4847 got_per_bfd_arg.primary = NULL;
0a44bf69 4848 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4849 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4850 - htab->reserved_gotno);
c224138d 4851 got_per_bfd_arg.max_pages = pages;
0f20cc35 4852 /* The number of globals that will be included in the primary GOT.
ab361d49 4853 See the calls to mips_elf_set_global_got_area below for more
0f20cc35
DJ
4854 information. */
4855 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4856
4857 /* Try to merge the GOTs of input bfds together, as long as they
4858 don't seem to exceed the maximum GOT size, choosing one of them
4859 to be the primary GOT. */
c72f2fb2 4860 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
d7206569
RS
4861 {
4862 gg = mips_elf_bfd_got (ibfd, FALSE);
4863 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4864 return FALSE;
4865 }
f4416af6 4866
0f20cc35 4867 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6 4868 if (got_per_bfd_arg.primary == NULL)
3dff0dd1 4869 g->next = mips_elf_create_got_info (abfd);
f4416af6
AO
4870 else
4871 g->next = got_per_bfd_arg.primary;
4872 g->next->next = got_per_bfd_arg.current;
4873
4874 /* GG is now the master GOT, and G is the primary GOT. */
4875 gg = g;
4876 g = g->next;
4877
4878 /* Map the output bfd to the primary got. That's what we're going
4879 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4880 didn't mark in check_relocs, and we want a quick way to find it.
4881 We can't just use gg->next because we're going to reverse the
4882 list. */
d7206569 4883 mips_elf_replace_bfd_got (abfd, g);
f4416af6 4884
634835ae
RS
4885 /* Every symbol that is referenced in a dynamic relocation must be
4886 present in the primary GOT, so arrange for them to appear after
4887 those that are actually referenced. */
23cc69b6 4888 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4889 g->global_gotno = gg->global_gotno;
f4416af6 4890
ab361d49
RS
4891 tga.info = info;
4892 tga.value = GGA_RELOC_ONLY;
4893 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4894 tga.value = GGA_NORMAL;
4895 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
f4416af6
AO
4896
4897 /* Now go through the GOTs assigning them offset ranges.
cb22ccf4 4898 [assigned_low_gotno, local_gotno[ will be set to the range of local
f4416af6
AO
4899 entries in each GOT. We can then compute the end of a GOT by
4900 adding local_gotno to global_gotno. We reverse the list and make
4901 it circular since then we'll be able to quickly compute the
4902 beginning of a GOT, by computing the end of its predecessor. To
4903 avoid special cases for the primary GOT, while still preserving
4904 assertions that are valid for both single- and multi-got links,
4905 we arrange for the main got struct to have the right number of
4906 global entries, but set its local_gotno such that the initial
4907 offset of the primary GOT is zero. Remember that the primary GOT
4908 will become the last item in the circular linked list, so it
4909 points back to the master GOT. */
4910 gg->local_gotno = -g->global_gotno;
4911 gg->global_gotno = g->global_gotno;
0f20cc35 4912 gg->tls_gotno = 0;
f4416af6
AO
4913 assign = 0;
4914 gg->next = gg;
4915
4916 do
4917 {
4918 struct mips_got_info *gn;
4919
861fb55a 4920 assign += htab->reserved_gotno;
cb22ccf4 4921 g->assigned_low_gotno = assign;
c224138d
RS
4922 g->local_gotno += assign;
4923 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
cb22ccf4 4924 g->assigned_high_gotno = g->local_gotno - 1;
0f20cc35
DJ
4925 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4926
ead49a57
RS
4927 /* Take g out of the direct list, and push it onto the reversed
4928 list that gg points to. g->next is guaranteed to be nonnull after
4929 this operation, as required by mips_elf_initialize_tls_index. */
4930 gn = g->next;
4931 g->next = gg->next;
4932 gg->next = g;
4933
0f20cc35
DJ
4934 /* Set up any TLS entries. We always place the TLS entries after
4935 all non-TLS entries. */
4936 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
72e7511a
RS
4937 tga.g = g;
4938 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4939 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
4940 if (!tga.g)
4941 return FALSE;
1fd20d70 4942 BFD_ASSERT (g->tls_assigned_gotno == assign);
f4416af6 4943
ead49a57 4944 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 4945 g = gn;
0626d451 4946
33bb52fb
RS
4947 /* Forbid global symbols in every non-primary GOT from having
4948 lazy-binding stubs. */
0626d451 4949 if (g)
33bb52fb 4950 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
4951 }
4952 while (g);
4953
59b08994 4954 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
33bb52fb
RS
4955
4956 needed_relocs = 0;
33bb52fb
RS
4957 for (g = gg->next; g && g->next != gg; g = g->next)
4958 {
4959 unsigned int save_assign;
4960
ab361d49
RS
4961 /* Assign offsets to global GOT entries and count how many
4962 relocations they need. */
cb22ccf4
KCY
4963 save_assign = g->assigned_low_gotno;
4964 g->assigned_low_gotno = g->local_gotno;
ab361d49
RS
4965 tga.info = info;
4966 tga.value = MIPS_ELF_GOT_SIZE (abfd);
4967 tga.g = g;
4968 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
72e7511a
RS
4969 if (!tga.g)
4970 return FALSE;
cb22ccf4
KCY
4971 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
4972 g->assigned_low_gotno = save_assign;
72e7511a 4973
0e1862bb 4974 if (bfd_link_pic (info))
33bb52fb 4975 {
cb22ccf4
KCY
4976 g->relocs += g->local_gotno - g->assigned_low_gotno;
4977 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
33bb52fb
RS
4978 + g->next->global_gotno
4979 + g->next->tls_gotno
861fb55a 4980 + htab->reserved_gotno);
33bb52fb 4981 }
ab361d49 4982 needed_relocs += g->relocs;
33bb52fb 4983 }
ab361d49 4984 needed_relocs += g->relocs;
33bb52fb
RS
4985
4986 if (needed_relocs)
4987 mips_elf_allocate_dynamic_relocations (dynobj, info,
4988 needed_relocs);
143d77c5 4989
f4416af6
AO
4990 return TRUE;
4991}
143d77c5 4992
b49e97c9
TS
4993\f
4994/* Returns the first relocation of type r_type found, beginning with
4995 RELOCATION. RELEND is one-past-the-end of the relocation table. */
4996
4997static const Elf_Internal_Rela *
9719ad41
RS
4998mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
4999 const Elf_Internal_Rela *relocation,
5000 const Elf_Internal_Rela *relend)
b49e97c9 5001{
c000e262
TS
5002 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5003
b49e97c9
TS
5004 while (relocation < relend)
5005 {
c000e262
TS
5006 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5007 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
5008 return relocation;
5009
5010 ++relocation;
5011 }
5012
5013 /* We didn't find it. */
b49e97c9
TS
5014 return NULL;
5015}
5016
020d7251 5017/* Return whether an input relocation is against a local symbol. */
b49e97c9 5018
b34976b6 5019static bfd_boolean
9719ad41
RS
5020mips_elf_local_relocation_p (bfd *input_bfd,
5021 const Elf_Internal_Rela *relocation,
020d7251 5022 asection **local_sections)
b49e97c9
TS
5023{
5024 unsigned long r_symndx;
5025 Elf_Internal_Shdr *symtab_hdr;
b49e97c9
TS
5026 size_t extsymoff;
5027
5028 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5029 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5030 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5031
5032 if (r_symndx < extsymoff)
b34976b6 5033 return TRUE;
b49e97c9 5034 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 5035 return TRUE;
b49e97c9 5036
b34976b6 5037 return FALSE;
b49e97c9
TS
5038}
5039\f
5040/* Sign-extend VALUE, which has the indicated number of BITS. */
5041
a7ebbfdf 5042bfd_vma
9719ad41 5043_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
5044{
5045 if (value & ((bfd_vma) 1 << (bits - 1)))
5046 /* VALUE is negative. */
5047 value |= ((bfd_vma) - 1) << bits;
5048
5049 return value;
5050}
5051
5052/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 5053 range expressible by a signed number with the indicated number of
b49e97c9
TS
5054 BITS. */
5055
b34976b6 5056static bfd_boolean
9719ad41 5057mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
5058{
5059 bfd_signed_vma svalue = (bfd_signed_vma) value;
5060
5061 if (svalue > (1 << (bits - 1)) - 1)
5062 /* The value is too big. */
b34976b6 5063 return TRUE;
b49e97c9
TS
5064 else if (svalue < -(1 << (bits - 1)))
5065 /* The value is too small. */
b34976b6 5066 return TRUE;
b49e97c9
TS
5067
5068 /* All is well. */
b34976b6 5069 return FALSE;
b49e97c9
TS
5070}
5071
5072/* Calculate the %high function. */
5073
5074static bfd_vma
9719ad41 5075mips_elf_high (bfd_vma value)
b49e97c9
TS
5076{
5077 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5078}
5079
5080/* Calculate the %higher function. */
5081
5082static bfd_vma
9719ad41 5083mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
5084{
5085#ifdef BFD64
5086 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5087#else
5088 abort ();
c5ae1840 5089 return MINUS_ONE;
b49e97c9
TS
5090#endif
5091}
5092
5093/* Calculate the %highest function. */
5094
5095static bfd_vma
9719ad41 5096mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
5097{
5098#ifdef BFD64
b15e6682 5099 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
5100#else
5101 abort ();
c5ae1840 5102 return MINUS_ONE;
b49e97c9
TS
5103#endif
5104}
5105\f
5106/* Create the .compact_rel section. */
5107
b34976b6 5108static bfd_boolean
9719ad41
RS
5109mips_elf_create_compact_rel_section
5110 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
5111{
5112 flagword flags;
5113 register asection *s;
5114
3d4d4302 5115 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
b49e97c9
TS
5116 {
5117 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5118 | SEC_READONLY);
5119
3d4d4302 5120 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
b49e97c9 5121 if (s == NULL
b49e97c9
TS
5122 || ! bfd_set_section_alignment (abfd, s,
5123 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 5124 return FALSE;
b49e97c9 5125
eea6121a 5126 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
5127 }
5128
b34976b6 5129 return TRUE;
b49e97c9
TS
5130}
5131
5132/* Create the .got section to hold the global offset table. */
5133
b34976b6 5134static bfd_boolean
23cc69b6 5135mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
5136{
5137 flagword flags;
5138 register asection *s;
5139 struct elf_link_hash_entry *h;
14a793b2 5140 struct bfd_link_hash_entry *bh;
0a44bf69
RS
5141 struct mips_elf_link_hash_table *htab;
5142
5143 htab = mips_elf_hash_table (info);
4dfe6ac6 5144 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5145
5146 /* This function may be called more than once. */
ce558b89 5147 if (htab->root.sgot)
23cc69b6 5148 return TRUE;
b49e97c9
TS
5149
5150 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5151 | SEC_LINKER_CREATED);
5152
72b4917c
TS
5153 /* We have to use an alignment of 2**4 here because this is hardcoded
5154 in the function stub generation and in the linker script. */
87e0a731 5155 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
b49e97c9 5156 if (s == NULL
72b4917c 5157 || ! bfd_set_section_alignment (abfd, s, 4))
b34976b6 5158 return FALSE;
ce558b89 5159 htab->root.sgot = s;
b49e97c9
TS
5160
5161 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5162 linker script because we don't want to define the symbol if we
5163 are not creating a global offset table. */
14a793b2 5164 bh = NULL;
b49e97c9
TS
5165 if (! (_bfd_generic_link_add_one_symbol
5166 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 5167 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 5168 return FALSE;
14a793b2
AM
5169
5170 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5171 h->non_elf = 0;
5172 h->def_regular = 1;
b49e97c9 5173 h->type = STT_OBJECT;
2f9efdfc 5174 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d329bcd1 5175 elf_hash_table (info)->hgot = h;
b49e97c9 5176
0e1862bb 5177 if (bfd_link_pic (info)
c152c796 5178 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 5179 return FALSE;
b49e97c9 5180
3dff0dd1 5181 htab->got_info = mips_elf_create_got_info (abfd);
f0abc2a1 5182 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
5183 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5184
861fb55a 5185 /* We also need a .got.plt section when generating PLTs. */
87e0a731
AM
5186 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5187 SEC_ALLOC | SEC_LOAD
5188 | SEC_HAS_CONTENTS
5189 | SEC_IN_MEMORY
5190 | SEC_LINKER_CREATED);
861fb55a
DJ
5191 if (s == NULL)
5192 return FALSE;
ce558b89 5193 htab->root.sgotplt = s;
0a44bf69 5194
b34976b6 5195 return TRUE;
b49e97c9 5196}
b49e97c9 5197\f
0a44bf69
RS
5198/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5199 __GOTT_INDEX__ symbols. These symbols are only special for
5200 shared objects; they are not used in executables. */
5201
5202static bfd_boolean
5203is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5204{
5205 return (mips_elf_hash_table (info)->is_vxworks
0e1862bb 5206 && bfd_link_pic (info)
0a44bf69
RS
5207 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5208 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5209}
861fb55a
DJ
5210
5211/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5212 require an la25 stub. See also mips_elf_local_pic_function_p,
5213 which determines whether the destination function ever requires a
5214 stub. */
5215
5216static bfd_boolean
8f0c309a
CLT
5217mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5218 bfd_boolean target_is_16_bit_code_p)
861fb55a
DJ
5219{
5220 /* We specifically ignore branches and jumps from EF_PIC objects,
5221 where the onus is on the compiler or programmer to perform any
5222 necessary initialization of $25. Sometimes such initialization
5223 is unnecessary; for example, -mno-shared functions do not use
5224 the incoming value of $25, and may therefore be called directly. */
5225 if (PIC_OBJECT_P (input_bfd))
5226 return FALSE;
5227
5228 switch (r_type)
5229 {
5230 case R_MIPS_26:
5231 case R_MIPS_PC16:
7361da2c
AB
5232 case R_MIPS_PC21_S2:
5233 case R_MIPS_PC26_S2:
df58fc94
RS
5234 case R_MICROMIPS_26_S1:
5235 case R_MICROMIPS_PC7_S1:
5236 case R_MICROMIPS_PC10_S1:
5237 case R_MICROMIPS_PC16_S1:
5238 case R_MICROMIPS_PC23_S2:
861fb55a
DJ
5239 return TRUE;
5240
8f0c309a
CLT
5241 case R_MIPS16_26:
5242 return !target_is_16_bit_code_p;
5243
861fb55a
DJ
5244 default:
5245 return FALSE;
5246 }
5247}
0a44bf69 5248\f
47275900
MR
5249/* Obtain the field relocated by RELOCATION. */
5250
5251static bfd_vma
5252mips_elf_obtain_contents (reloc_howto_type *howto,
5253 const Elf_Internal_Rela *relocation,
5254 bfd *input_bfd, bfd_byte *contents)
5255{
5256 bfd_vma x = 0;
5257 bfd_byte *location = contents + relocation->r_offset;
5258 unsigned int size = bfd_get_reloc_size (howto);
5259
5260 /* Obtain the bytes. */
5261 if (size != 0)
5262 x = bfd_get (8 * size, input_bfd, location);
5263
5264 return x;
5265}
5266
98e10ffa
MR
5267/* Store the field relocated by RELOCATION. */
5268
5269static void
5270mips_elf_store_contents (reloc_howto_type *howto,
5271 const Elf_Internal_Rela *relocation,
5272 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5273{
5274 bfd_byte *location = contents + relocation->r_offset;
5275 unsigned int size = bfd_get_reloc_size (howto);
5276
5277 /* Put the value into the output. */
5278 if (size != 0)
5279 bfd_put (8 * size, input_bfd, x, location);
5280}
5281
47275900
MR
5282/* Try to patch a load from GOT instruction in CONTENTS pointed to by
5283 RELOCATION described by HOWTO, with a move of 0 to the load target
5284 register, returning TRUE if that is successful and FALSE otherwise.
5285 If DOIT is FALSE, then only determine it patching is possible and
5286 return status without actually changing CONTENTS.
5287*/
5288
5289static bfd_boolean
5290mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5291 const Elf_Internal_Rela *relocation,
5292 reloc_howto_type *howto, bfd_boolean doit)
5293{
5294 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5295 bfd_byte *location = contents + relocation->r_offset;
5296 bfd_boolean nullified = TRUE;
5297 bfd_vma x;
5298
5299 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5300
5301 /* Obtain the current value. */
5302 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5303
5304 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5305 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5306 if (mips16_reloc_p (r_type)
5307 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5308 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
5309 x = (0x3cd << 22) | (x & (7 << 16)) << 3; /* LI */
5310 else if (micromips_reloc_p (r_type)
5311 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5312 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5313 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5314 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5315 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5316 else
5317 nullified = FALSE;
5318
5319 /* Put the value into the output. */
5320 if (doit && nullified)
5321 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5322
5323 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, FALSE, location);
5324
5325 return nullified;
5326}
5327
b49e97c9
TS
5328/* Calculate the value produced by the RELOCATION (which comes from
5329 the INPUT_BFD). The ADDEND is the addend to use for this
5330 RELOCATION; RELOCATION->R_ADDEND is ignored.
5331
5332 The result of the relocation calculation is stored in VALUEP.
38a7df63 5333 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
df58fc94 5334 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9
TS
5335
5336 This function returns bfd_reloc_continue if the caller need take no
5337 further action regarding this relocation, bfd_reloc_notsupported if
5338 something goes dramatically wrong, bfd_reloc_overflow if an
5339 overflow occurs, and bfd_reloc_ok to indicate success. */
5340
5341static bfd_reloc_status_type
9719ad41 5342mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
47275900 5343 asection *input_section, bfd_byte *contents,
9719ad41
RS
5344 struct bfd_link_info *info,
5345 const Elf_Internal_Rela *relocation,
5346 bfd_vma addend, reloc_howto_type *howto,
5347 Elf_Internal_Sym *local_syms,
5348 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
5349 const char **namep,
5350 bfd_boolean *cross_mode_jump_p,
9719ad41 5351 bfd_boolean save_addend)
b49e97c9
TS
5352{
5353 /* The eventual value we will return. */
5354 bfd_vma value;
5355 /* The address of the symbol against which the relocation is
5356 occurring. */
5357 bfd_vma symbol = 0;
5358 /* The final GP value to be used for the relocatable, executable, or
5359 shared object file being produced. */
0a61c8c2 5360 bfd_vma gp;
b49e97c9
TS
5361 /* The place (section offset or address) of the storage unit being
5362 relocated. */
5363 bfd_vma p;
5364 /* The value of GP used to create the relocatable object. */
0a61c8c2 5365 bfd_vma gp0;
b49e97c9
TS
5366 /* The offset into the global offset table at which the address of
5367 the relocation entry symbol, adjusted by the addend, resides
5368 during execution. */
5369 bfd_vma g = MINUS_ONE;
5370 /* The section in which the symbol referenced by the relocation is
5371 located. */
5372 asection *sec = NULL;
5373 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 5374 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 5375 symbol. */
b34976b6 5376 bfd_boolean local_p, was_local_p;
77434823
MR
5377 /* TRUE if the symbol referred to by this relocation is a section
5378 symbol. */
5379 bfd_boolean section_p = FALSE;
b34976b6
AM
5380 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5381 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
5382 /* TRUE if the symbol referred to by this relocation is
5383 "__gnu_local_gp". */
5384 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
5385 Elf_Internal_Shdr *symtab_hdr;
5386 size_t extsymoff;
5387 unsigned long r_symndx;
5388 int r_type;
b34976b6 5389 /* TRUE if overflow occurred during the calculation of the
b49e97c9 5390 relocation value. */
b34976b6
AM
5391 bfd_boolean overflowed_p;
5392 /* TRUE if this relocation refers to a MIPS16 function. */
5393 bfd_boolean target_is_16_bit_code_p = FALSE;
df58fc94 5394 bfd_boolean target_is_micromips_code_p = FALSE;
0a44bf69
RS
5395 struct mips_elf_link_hash_table *htab;
5396 bfd *dynobj;
ad951203 5397 bfd_boolean resolved_to_zero;
0a44bf69
RS
5398
5399 dynobj = elf_hash_table (info)->dynobj;
5400 htab = mips_elf_hash_table (info);
4dfe6ac6 5401 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5402
5403 /* Parse the relocation. */
5404 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5405 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5406 p = (input_section->output_section->vma
5407 + input_section->output_offset
5408 + relocation->r_offset);
5409
5410 /* Assume that there will be no overflow. */
b34976b6 5411 overflowed_p = FALSE;
b49e97c9
TS
5412
5413 /* Figure out whether or not the symbol is local, and get the offset
5414 used in the array of hash table entries. */
5415 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5416 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
020d7251 5417 local_sections);
bce03d3d 5418 was_local_p = local_p;
b49e97c9
TS
5419 if (! elf_bad_symtab (input_bfd))
5420 extsymoff = symtab_hdr->sh_info;
5421 else
5422 {
5423 /* The symbol table does not follow the rule that local symbols
5424 must come before globals. */
5425 extsymoff = 0;
5426 }
5427
5428 /* Figure out the value of the symbol. */
5429 if (local_p)
5430 {
9d862524 5431 bfd_boolean micromips_p = MICROMIPS_P (abfd);
b49e97c9
TS
5432 Elf_Internal_Sym *sym;
5433
5434 sym = local_syms + r_symndx;
5435 sec = local_sections[r_symndx];
5436
77434823
MR
5437 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5438
b49e97c9 5439 symbol = sec->output_section->vma + sec->output_offset;
77434823 5440 if (!section_p || (sec->flags & SEC_MERGE))
b49e97c9 5441 symbol += sym->st_value;
77434823 5442 if ((sec->flags & SEC_MERGE) && section_p)
d4df96e6
L
5443 {
5444 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5445 addend -= symbol;
5446 addend += sec->output_section->vma + sec->output_offset;
5447 }
b49e97c9 5448
df58fc94
RS
5449 /* MIPS16/microMIPS text labels should be treated as odd. */
5450 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
5451 ++symbol;
5452
5453 /* Record the name of this symbol, for our caller. */
5454 *namep = bfd_elf_string_from_elf_section (input_bfd,
5455 symtab_hdr->sh_link,
5456 sym->st_name);
ceab86af 5457 if (*namep == NULL || **namep == '\0')
b49e97c9
TS
5458 *namep = bfd_section_name (input_bfd, sec);
5459
9d862524 5460 /* For relocations against a section symbol and ones against no
07d6d2b8 5461 symbol (absolute relocations) infer the ISA mode from the addend. */
9d862524
MR
5462 if (section_p || r_symndx == STN_UNDEF)
5463 {
5464 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5465 target_is_micromips_code_p = (addend & 1) && micromips_p;
5466 }
5467 /* For relocations against an absolute symbol infer the ISA mode
07d6d2b8 5468 from the value of the symbol plus addend. */
9d862524
MR
5469 else if (bfd_is_abs_section (sec))
5470 {
5471 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5472 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5473 }
5474 /* Otherwise just use the regular symbol annotation available. */
5475 else
5476 {
5477 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5478 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5479 }
b49e97c9
TS
5480 }
5481 else
5482 {
560e09e9
NC
5483 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5484
b49e97c9
TS
5485 /* For global symbols we look up the symbol in the hash-table. */
5486 h = ((struct mips_elf_link_hash_entry *)
5487 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5488 /* Find the real hash-table entry for this symbol. */
5489 while (h->root.root.type == bfd_link_hash_indirect
5490 || h->root.root.type == bfd_link_hash_warning)
5491 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5492
5493 /* Record the name of this symbol, for our caller. */
5494 *namep = h->root.root.root.string;
5495
5496 /* See if this is the special _gp_disp symbol. Note that such a
5497 symbol must always be a global symbol. */
560e09e9 5498 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
5499 && ! NEWABI_P (input_bfd))
5500 {
5501 /* Relocations against _gp_disp are permitted only with
5502 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 5503 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
5504 return bfd_reloc_notsupported;
5505
b34976b6 5506 gp_disp_p = TRUE;
b49e97c9 5507 }
bbe506e8
TS
5508 /* See if this is the special _gp symbol. Note that such a
5509 symbol must always be a global symbol. */
5510 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5511 gnu_local_gp_p = TRUE;
5512
5513
b49e97c9
TS
5514 /* If this symbol is defined, calculate its address. Note that
5515 _gp_disp is a magic symbol, always implicitly defined by the
5516 linker, so it's inappropriate to check to see whether or not
5517 its defined. */
5518 else if ((h->root.root.type == bfd_link_hash_defined
5519 || h->root.root.type == bfd_link_hash_defweak)
5520 && h->root.root.u.def.section)
5521 {
5522 sec = h->root.root.u.def.section;
5523 if (sec->output_section)
5524 symbol = (h->root.root.u.def.value
5525 + sec->output_section->vma
5526 + sec->output_offset);
5527 else
5528 symbol = h->root.root.u.def.value;
5529 }
5530 else if (h->root.root.type == bfd_link_hash_undefweak)
5531 /* We allow relocations against undefined weak symbols, giving
5532 it the value zero, so that you can undefined weak functions
5533 and check to see if they exist by looking at their
5534 addresses. */
5535 symbol = 0;
59c2e50f 5536 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5537 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5538 symbol = 0;
a4d0f181
TS
5539 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5540 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5541 {
5542 /* If this is a dynamic link, we should have created a
5543 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
de194d85 5544 in _bfd_mips_elf_create_dynamic_sections.
b49e97c9
TS
5545 Otherwise, we should define the symbol with a value of 0.
5546 FIXME: It should probably get into the symbol table
5547 somehow as well. */
0e1862bb 5548 BFD_ASSERT (! bfd_link_pic (info));
b49e97c9
TS
5549 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5550 symbol = 0;
5551 }
5e2b0d47
NC
5552 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5553 {
5554 /* This is an optional symbol - an Irix specific extension to the
5555 ELF spec. Ignore it for now.
5556 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5557 than simply ignoring them, but we do not handle this for now.
5558 For information see the "64-bit ELF Object File Specification"
5559 which is available from here:
5560 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5561 symbol = 0;
5562 }
b49e97c9
TS
5563 else
5564 {
dfb93f11
JC
5565 bfd_boolean reject_undefined
5566 = (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
5567 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
5568
1a72702b
AM
5569 (*info->callbacks->undefined_symbol)
5570 (info, h->root.root.root.string, input_bfd,
dfb93f11
JC
5571 input_section, relocation->r_offset, reject_undefined);
5572
5573 if (reject_undefined)
5574 return bfd_reloc_undefined;
5575
5576 symbol = 0;
b49e97c9
TS
5577 }
5578
30c09090 5579 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
1bbce132 5580 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
b49e97c9
TS
5581 }
5582
738e5348
RS
5583 /* If this is a reference to a 16-bit function with a stub, we need
5584 to redirect the relocation to the stub unless:
5585
5586 (a) the relocation is for a MIPS16 JAL;
5587
5588 (b) the relocation is for a MIPS16 PIC call, and there are no
5589 non-MIPS16 uses of the GOT slot; or
5590
5591 (c) the section allows direct references to MIPS16 functions. */
5592 if (r_type != R_MIPS16_26
0e1862bb 5593 && !bfd_link_relocatable (info)
738e5348
RS
5594 && ((h != NULL
5595 && h->fn_stub != NULL
5596 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71 5597 || (local_p
698600e4
AM
5598 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5599 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5600 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5601 {
5602 /* This is a 32- or 64-bit call to a 16-bit function. We should
5603 have already noticed that we were going to need the
5604 stub. */
5605 if (local_p)
8f0c309a 5606 {
698600e4 5607 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
8f0c309a
CLT
5608 value = 0;
5609 }
b49e97c9
TS
5610 else
5611 {
5612 BFD_ASSERT (h->need_fn_stub);
8f0c309a
CLT
5613 if (h->la25_stub)
5614 {
5615 /* If a LA25 header for the stub itself exists, point to the
5616 prepended LUI/ADDIU sequence. */
5617 sec = h->la25_stub->stub_section;
5618 value = h->la25_stub->offset;
5619 }
5620 else
5621 {
5622 sec = h->fn_stub;
5623 value = 0;
5624 }
b49e97c9
TS
5625 }
5626
8f0c309a 5627 symbol = sec->output_section->vma + sec->output_offset + value;
f38c2df5
TS
5628 /* The target is 16-bit, but the stub isn't. */
5629 target_is_16_bit_code_p = FALSE;
b49e97c9 5630 }
1bbce132
MR
5631 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5632 to a standard MIPS function, we need to redirect the call to the stub.
5633 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5634 indirect calls should use an indirect stub instead. */
0e1862bb 5635 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
b314ec0e 5636 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71 5637 || (local_p
698600e4
AM
5638 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5639 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
1bbce132 5640 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
b49e97c9 5641 {
b9d58d71 5642 if (local_p)
698600e4 5643 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
b9d58d71 5644 else
b49e97c9 5645 {
b9d58d71
TS
5646 /* If both call_stub and call_fp_stub are defined, we can figure
5647 out which one to use by checking which one appears in the input
5648 file. */
5649 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5650 {
b9d58d71 5651 asection *o;
68ffbac6 5652
b9d58d71
TS
5653 sec = NULL;
5654 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5655 {
b9d58d71
TS
5656 if (CALL_FP_STUB_P (bfd_get_section_name (input_bfd, o)))
5657 {
5658 sec = h->call_fp_stub;
5659 break;
5660 }
b49e97c9 5661 }
b9d58d71
TS
5662 if (sec == NULL)
5663 sec = h->call_stub;
b49e97c9 5664 }
b9d58d71 5665 else if (h->call_stub != NULL)
b49e97c9 5666 sec = h->call_stub;
b9d58d71
TS
5667 else
5668 sec = h->call_fp_stub;
07d6d2b8 5669 }
b49e97c9 5670
eea6121a 5671 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5672 symbol = sec->output_section->vma + sec->output_offset;
5673 }
861fb55a
DJ
5674 /* If this is a direct call to a PIC function, redirect to the
5675 non-PIC stub. */
5676 else if (h != NULL && h->la25_stub
8f0c309a
CLT
5677 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5678 target_is_16_bit_code_p))
c7318def
MR
5679 {
5680 symbol = (h->la25_stub->stub_section->output_section->vma
5681 + h->la25_stub->stub_section->output_offset
5682 + h->la25_stub->offset);
5683 if (ELF_ST_IS_MICROMIPS (h->root.other))
5684 symbol |= 1;
5685 }
1bbce132
MR
5686 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5687 entry is used if a standard PLT entry has also been made. In this
5688 case the symbol will have been set by mips_elf_set_plt_sym_value
5689 to point to the standard PLT entry, so redirect to the compressed
5690 one. */
54806ffa
MR
5691 else if ((mips16_branch_reloc_p (r_type)
5692 || micromips_branch_reloc_p (r_type))
0e1862bb 5693 && !bfd_link_relocatable (info)
1bbce132
MR
5694 && h != NULL
5695 && h->use_plt_entry
5696 && h->root.plt.plist->comp_offset != MINUS_ONE
5697 && h->root.plt.plist->mips_offset != MINUS_ONE)
5698 {
5699 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5700
ce558b89 5701 sec = htab->root.splt;
1bbce132
MR
5702 symbol = (sec->output_section->vma
5703 + sec->output_offset
5704 + htab->plt_header_size
5705 + htab->plt_mips_offset
5706 + h->root.plt.plist->comp_offset
5707 + 1);
5708
5709 target_is_16_bit_code_p = !micromips_p;
5710 target_is_micromips_code_p = micromips_p;
5711 }
b49e97c9 5712
df58fc94 5713 /* Make sure MIPS16 and microMIPS are not used together. */
c9775dde 5714 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
df58fc94
RS
5715 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5716 {
4eca0228 5717 _bfd_error_handler
df58fc94
RS
5718 (_("MIPS16 and microMIPS functions cannot call each other"));
5719 return bfd_reloc_notsupported;
5720 }
5721
b49e97c9 5722 /* Calls from 16-bit code to 32-bit code and vice versa require the
df58fc94
RS
5723 mode change. However, we can ignore calls to undefined weak symbols,
5724 which should never be executed at runtime. This exception is important
5725 because the assembly writer may have "known" that any definition of the
5726 symbol would be 16-bit code, and that direct jumps were therefore
5727 acceptable. */
0e1862bb 5728 *cross_mode_jump_p = (!bfd_link_relocatable (info)
df58fc94 5729 && !(h && h->root.root.type == bfd_link_hash_undefweak)
9d862524
MR
5730 && ((mips16_branch_reloc_p (r_type)
5731 && !target_is_16_bit_code_p)
5732 || (micromips_branch_reloc_p (r_type)
df58fc94 5733 && !target_is_micromips_code_p)
9d862524
MR
5734 || ((branch_reloc_p (r_type)
5735 || r_type == R_MIPS_JALR)
df58fc94
RS
5736 && (target_is_16_bit_code_p
5737 || target_is_micromips_code_p))));
b49e97c9 5738
47275900
MR
5739 resolved_to_zero = (h != NULL
5740 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5741
5742 switch (r_type)
5743 {
5744 case R_MIPS16_CALL16:
5745 case R_MIPS16_GOT16:
5746 case R_MIPS_CALL16:
5747 case R_MIPS_GOT16:
5748 case R_MIPS_GOT_PAGE:
5749 case R_MIPS_GOT_DISP:
5750 case R_MIPS_GOT_LO16:
5751 case R_MIPS_CALL_LO16:
5752 case R_MICROMIPS_CALL16:
5753 case R_MICROMIPS_GOT16:
5754 case R_MICROMIPS_GOT_PAGE:
5755 case R_MICROMIPS_GOT_DISP:
5756 case R_MICROMIPS_GOT_LO16:
5757 case R_MICROMIPS_CALL_LO16:
5758 if (resolved_to_zero
5759 && !bfd_link_relocatable (info)
5760 && mips_elf_nullify_got_load (input_bfd, contents,
5761 relocation, howto, TRUE))
5762 return bfd_reloc_continue;
5763
5764 /* Fall through. */
5765 case R_MIPS_GOT_HI16:
5766 case R_MIPS_CALL_HI16:
5767 case R_MICROMIPS_GOT_HI16:
5768 case R_MICROMIPS_CALL_HI16:
5769 if (resolved_to_zero
5770 && htab->use_absolute_zero
5771 && bfd_link_pic (info))
5772 {
5773 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5774 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5775 FALSE, FALSE, FALSE);
5776 BFD_ASSERT (h != NULL);
5777 }
5778 break;
5779 }
5780
c5d6fa44 5781 local_p = (h == NULL || mips_use_local_got_p (info, h));
b49e97c9 5782
0a61c8c2
RS
5783 gp0 = _bfd_get_gp_value (input_bfd);
5784 gp = _bfd_get_gp_value (abfd);
23cc69b6 5785 if (htab->got_info)
a8028dd0 5786 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5787
5788 if (gnu_local_gp_p)
5789 symbol = gp;
5790
df58fc94
RS
5791 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5792 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5793 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5794 if (got_page_reloc_p (r_type) && !local_p)
020d7251 5795 {
df58fc94
RS
5796 r_type = (micromips_reloc_p (r_type)
5797 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
020d7251
RS
5798 addend = 0;
5799 }
5800
e77760d2 5801 /* If we haven't already determined the GOT offset, and we're going
0a61c8c2 5802 to need it, get it now. */
b49e97c9
TS
5803 switch (r_type)
5804 {
738e5348
RS
5805 case R_MIPS16_CALL16:
5806 case R_MIPS16_GOT16:
b49e97c9
TS
5807 case R_MIPS_CALL16:
5808 case R_MIPS_GOT16:
5809 case R_MIPS_GOT_DISP:
5810 case R_MIPS_GOT_HI16:
5811 case R_MIPS_CALL_HI16:
5812 case R_MIPS_GOT_LO16:
5813 case R_MIPS_CALL_LO16:
df58fc94
RS
5814 case R_MICROMIPS_CALL16:
5815 case R_MICROMIPS_GOT16:
5816 case R_MICROMIPS_GOT_DISP:
5817 case R_MICROMIPS_GOT_HI16:
5818 case R_MICROMIPS_CALL_HI16:
5819 case R_MICROMIPS_GOT_LO16:
5820 case R_MICROMIPS_CALL_LO16:
0f20cc35
DJ
5821 case R_MIPS_TLS_GD:
5822 case R_MIPS_TLS_GOTTPREL:
5823 case R_MIPS_TLS_LDM:
d0f13682
CLT
5824 case R_MIPS16_TLS_GD:
5825 case R_MIPS16_TLS_GOTTPREL:
5826 case R_MIPS16_TLS_LDM:
df58fc94
RS
5827 case R_MICROMIPS_TLS_GD:
5828 case R_MICROMIPS_TLS_GOTTPREL:
5829 case R_MICROMIPS_TLS_LDM:
b49e97c9 5830 /* Find the index into the GOT where this value is located. */
df58fc94 5831 if (tls_ldm_reloc_p (r_type))
0f20cc35 5832 {
0a44bf69 5833 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5834 0, 0, NULL, r_type);
0f20cc35
DJ
5835 if (g == MINUS_ONE)
5836 return bfd_reloc_outofrange;
5837 }
5838 else if (!local_p)
b49e97c9 5839 {
0a44bf69
RS
5840 /* On VxWorks, CALL relocations should refer to the .got.plt
5841 entry, which is initialized to point at the PLT stub. */
5842 if (htab->is_vxworks
df58fc94
RS
5843 && (call_hi16_reloc_p (r_type)
5844 || call_lo16_reloc_p (r_type)
738e5348 5845 || call16_reloc_p (r_type)))
0a44bf69
RS
5846 {
5847 BFD_ASSERT (addend == 0);
5848 BFD_ASSERT (h->root.needs_plt);
5849 g = mips_elf_gotplt_index (info, &h->root);
5850 }
5851 else
b49e97c9 5852 {
020d7251 5853 BFD_ASSERT (addend == 0);
13fbec83
RS
5854 g = mips_elf_global_got_index (abfd, info, input_bfd,
5855 &h->root, r_type);
e641e783 5856 if (!TLS_RELOC_P (r_type)
020d7251
RS
5857 && !elf_hash_table (info)->dynamic_sections_created)
5858 /* This is a static link. We must initialize the GOT entry. */
ce558b89 5859 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
b49e97c9
TS
5860 }
5861 }
0a44bf69 5862 else if (!htab->is_vxworks
738e5348 5863 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5864 /* The calculation below does not involve "g". */
b49e97c9
TS
5865 break;
5866 else
5867 {
5c18022e 5868 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5869 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5870 if (g == MINUS_ONE)
5871 return bfd_reloc_outofrange;
5872 }
5873
5874 /* Convert GOT indices to actual offsets. */
a8028dd0 5875 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5876 break;
b49e97c9
TS
5877 }
5878
0a44bf69
RS
5879 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5880 symbols are resolved by the loader. Add them to .rela.dyn. */
5881 if (h != NULL && is_gott_symbol (info, &h->root))
5882 {
5883 Elf_Internal_Rela outrel;
5884 bfd_byte *loc;
5885 asection *s;
5886
5887 s = mips_elf_rel_dyn_section (info, FALSE);
5888 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5889
5890 outrel.r_offset = (input_section->output_section->vma
5891 + input_section->output_offset
5892 + relocation->r_offset);
5893 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5894 outrel.r_addend = addend;
5895 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5896
5897 /* If we've written this relocation for a readonly section,
5898 we need to set DF_TEXTREL again, so that we do not delete the
5899 DT_TEXTREL tag. */
5900 if (MIPS_ELF_READONLY_SECTION (input_section))
5901 info->flags |= DF_TEXTREL;
5902
0a44bf69
RS
5903 *valuep = 0;
5904 return bfd_reloc_ok;
5905 }
5906
b49e97c9
TS
5907 /* Figure out what kind of relocation is being performed. */
5908 switch (r_type)
5909 {
5910 case R_MIPS_NONE:
5911 return bfd_reloc_continue;
5912
5913 case R_MIPS_16:
c3eb94b4
MF
5914 if (howto->partial_inplace)
5915 addend = _bfd_mips_elf_sign_extend (addend, 16);
5916 value = symbol + addend;
b49e97c9
TS
5917 overflowed_p = mips_elf_overflow_p (value, 16);
5918 break;
5919
5920 case R_MIPS_32:
5921 case R_MIPS_REL32:
5922 case R_MIPS_64:
0e1862bb 5923 if ((bfd_link_pic (info)
861fb55a 5924 || (htab->root.dynamic_sections_created
b49e97c9 5925 && h != NULL
f5385ebf 5926 && h->root.def_dynamic
861fb55a
DJ
5927 && !h->root.def_regular
5928 && !h->has_static_relocs))
cf35638d 5929 && r_symndx != STN_UNDEF
9a59ad6b
DJ
5930 && (h == NULL
5931 || h->root.root.type != bfd_link_hash_undefweak
ad951203
L
5932 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
5933 && !resolved_to_zero))
b49e97c9
TS
5934 && (input_section->flags & SEC_ALLOC) != 0)
5935 {
861fb55a 5936 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5937 where the symbol will end up. So, we create a relocation
5938 record in the output, and leave the job up to the dynamic
861fb55a
DJ
5939 linker. We must do the same for executable references to
5940 shared library symbols, unless we've decided to use copy
5941 relocs or PLTs instead. */
b49e97c9
TS
5942 value = addend;
5943 if (!mips_elf_create_dynamic_relocation (abfd,
5944 info,
5945 relocation,
5946 h,
5947 sec,
5948 symbol,
5949 &value,
5950 input_section))
5951 return bfd_reloc_undefined;
5952 }
5953 else
5954 {
5955 if (r_type != R_MIPS_REL32)
5956 value = symbol + addend;
5957 else
5958 value = addend;
5959 }
5960 value &= howto->dst_mask;
092dcd75
CD
5961 break;
5962
5963 case R_MIPS_PC32:
5964 value = symbol + addend - p;
5965 value &= howto->dst_mask;
b49e97c9
TS
5966 break;
5967
b49e97c9
TS
5968 case R_MIPS16_26:
5969 /* The calculation for R_MIPS16_26 is just the same as for an
5970 R_MIPS_26. It's only the storage of the relocated field into
5971 the output file that's different. That's handled in
5972 mips_elf_perform_relocation. So, we just fall through to the
5973 R_MIPS_26 case here. */
5974 case R_MIPS_26:
df58fc94
RS
5975 case R_MICROMIPS_26_S1:
5976 {
5977 unsigned int shift;
5978
df58fc94
RS
5979 /* Shift is 2, unusually, for microMIPS JALX. */
5980 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
5981
77434823 5982 if (howto->partial_inplace && !section_p)
df58fc94 5983 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
c3eb94b4
MF
5984 else
5985 value = addend;
bc27bb05
MR
5986 value += symbol;
5987
9d862524
MR
5988 /* Make sure the target of a jump is suitably aligned. Bit 0 must
5989 be the correct ISA mode selector except for weak undefined
5990 symbols. */
5991 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
5992 && (*cross_mode_jump_p
5993 ? (value & 3) != (r_type == R_MIPS_26)
07d6d2b8 5994 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
bc27bb05
MR
5995 return bfd_reloc_outofrange;
5996
5997 value >>= shift;
77434823 5998 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
df58fc94
RS
5999 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6000 value &= howto->dst_mask;
6001 }
b49e97c9
TS
6002 break;
6003
0f20cc35 6004 case R_MIPS_TLS_DTPREL_HI16:
d0f13682 6005 case R_MIPS16_TLS_DTPREL_HI16:
df58fc94 6006 case R_MICROMIPS_TLS_DTPREL_HI16:
0f20cc35
DJ
6007 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6008 & howto->dst_mask);
6009 break;
6010
6011 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
6012 case R_MIPS_TLS_DTPREL32:
6013 case R_MIPS_TLS_DTPREL64:
d0f13682 6014 case R_MIPS16_TLS_DTPREL_LO16:
df58fc94 6015 case R_MICROMIPS_TLS_DTPREL_LO16:
0f20cc35
DJ
6016 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6017 break;
6018
6019 case R_MIPS_TLS_TPREL_HI16:
d0f13682 6020 case R_MIPS16_TLS_TPREL_HI16:
df58fc94 6021 case R_MICROMIPS_TLS_TPREL_HI16:
0f20cc35
DJ
6022 value = (mips_elf_high (addend + symbol - tprel_base (info))
6023 & howto->dst_mask);
6024 break;
6025
6026 case R_MIPS_TLS_TPREL_LO16:
d0f13682
CLT
6027 case R_MIPS_TLS_TPREL32:
6028 case R_MIPS_TLS_TPREL64:
6029 case R_MIPS16_TLS_TPREL_LO16:
df58fc94 6030 case R_MICROMIPS_TLS_TPREL_LO16:
0f20cc35
DJ
6031 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6032 break;
6033
b49e97c9 6034 case R_MIPS_HI16:
d6f16593 6035 case R_MIPS16_HI16:
df58fc94 6036 case R_MICROMIPS_HI16:
b49e97c9
TS
6037 if (!gp_disp_p)
6038 {
6039 value = mips_elf_high (addend + symbol);
6040 value &= howto->dst_mask;
6041 }
6042 else
6043 {
d6f16593 6044 /* For MIPS16 ABI code we generate this sequence
07d6d2b8
AM
6045 0: li $v0,%hi(_gp_disp)
6046 4: addiupc $v1,%lo(_gp_disp)
6047 8: sll $v0,16
d6f16593
MR
6048 12: addu $v0,$v1
6049 14: move $gp,$v0
6050 So the offsets of hi and lo relocs are the same, but the
888b9c01
CLT
6051 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6052 ADDIUPC clears the low two bits of the instruction address,
6053 so the base is ($t9 + 4) & ~3. */
d6f16593 6054 if (r_type == R_MIPS16_HI16)
888b9c01 6055 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
df58fc94
RS
6056 /* The microMIPS .cpload sequence uses the same assembly
6057 instructions as the traditional psABI version, but the
6058 incoming $t9 has the low bit set. */
6059 else if (r_type == R_MICROMIPS_HI16)
6060 value = mips_elf_high (addend + gp - p - 1);
d6f16593
MR
6061 else
6062 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
6063 }
6064 break;
6065
6066 case R_MIPS_LO16:
d6f16593 6067 case R_MIPS16_LO16:
df58fc94
RS
6068 case R_MICROMIPS_LO16:
6069 case R_MICROMIPS_HI0_LO16:
b49e97c9
TS
6070 if (!gp_disp_p)
6071 value = (symbol + addend) & howto->dst_mask;
6072 else
6073 {
d6f16593
MR
6074 /* See the comment for R_MIPS16_HI16 above for the reason
6075 for this conditional. */
6076 if (r_type == R_MIPS16_LO16)
888b9c01 6077 value = addend + gp - (p & ~(bfd_vma) 0x3);
df58fc94
RS
6078 else if (r_type == R_MICROMIPS_LO16
6079 || r_type == R_MICROMIPS_HI0_LO16)
6080 value = addend + gp - p + 3;
d6f16593
MR
6081 else
6082 value = addend + gp - p + 4;
b49e97c9 6083 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 6084 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
6085 _gp_disp are normally generated from the .cpload
6086 pseudo-op. It generates code that normally looks like
6087 this:
6088
6089 lui $gp,%hi(_gp_disp)
6090 addiu $gp,$gp,%lo(_gp_disp)
6091 addu $gp,$gp,$t9
6092
6093 Here $t9 holds the address of the function being called,
6094 as required by the MIPS ELF ABI. The R_MIPS_LO16
6095 relocation can easily overflow in this situation, but the
6096 R_MIPS_HI16 relocation will handle the overflow.
6097 Therefore, we consider this a bug in the MIPS ABI, and do
6098 not check for overflow here. */
6099 }
6100 break;
6101
6102 case R_MIPS_LITERAL:
df58fc94 6103 case R_MICROMIPS_LITERAL:
b49e97c9
TS
6104 /* Because we don't merge literal sections, we can handle this
6105 just like R_MIPS_GPREL16. In the long run, we should merge
6106 shared literals, and then we will need to additional work
6107 here. */
6108
6109 /* Fall through. */
6110
6111 case R_MIPS16_GPREL:
6112 /* The R_MIPS16_GPREL performs the same calculation as
6113 R_MIPS_GPREL16, but stores the relocated bits in a different
6114 order. We don't need to do anything special here; the
6115 differences are handled in mips_elf_perform_relocation. */
6116 case R_MIPS_GPREL16:
df58fc94
RS
6117 case R_MICROMIPS_GPREL7_S2:
6118 case R_MICROMIPS_GPREL16:
bce03d3d
AO
6119 /* Only sign-extend the addend if it was extracted from the
6120 instruction. If the addend was separate, leave it alone,
6121 otherwise we may lose significant bits. */
6122 if (howto->partial_inplace)
a7ebbfdf 6123 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
6124 value = symbol + addend - gp;
6125 /* If the symbol was local, any earlier relocatable links will
6126 have adjusted its addend with the gp offset, so compensate
6127 for that now. Don't do it for symbols forced local in this
6128 link, though, since they won't have had the gp offset applied
6129 to them before. */
6130 if (was_local_p)
6131 value += gp0;
538baf8b
AB
6132 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6133 overflowed_p = mips_elf_overflow_p (value, 16);
b49e97c9
TS
6134 break;
6135
738e5348
RS
6136 case R_MIPS16_GOT16:
6137 case R_MIPS16_CALL16:
b49e97c9
TS
6138 case R_MIPS_GOT16:
6139 case R_MIPS_CALL16:
df58fc94
RS
6140 case R_MICROMIPS_GOT16:
6141 case R_MICROMIPS_CALL16:
0a44bf69 6142 /* VxWorks does not have separate local and global semantics for
738e5348 6143 R_MIPS*_GOT16; every relocation evaluates to "G". */
0a44bf69 6144 if (!htab->is_vxworks && local_p)
b49e97c9 6145 {
5c18022e 6146 value = mips_elf_got16_entry (abfd, input_bfd, info,
020d7251 6147 symbol + addend, !was_local_p);
b49e97c9
TS
6148 if (value == MINUS_ONE)
6149 return bfd_reloc_outofrange;
6150 value
a8028dd0 6151 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
6152 overflowed_p = mips_elf_overflow_p (value, 16);
6153 break;
6154 }
6155
6156 /* Fall through. */
6157
0f20cc35
DJ
6158 case R_MIPS_TLS_GD:
6159 case R_MIPS_TLS_GOTTPREL:
6160 case R_MIPS_TLS_LDM:
b49e97c9 6161 case R_MIPS_GOT_DISP:
d0f13682
CLT
6162 case R_MIPS16_TLS_GD:
6163 case R_MIPS16_TLS_GOTTPREL:
6164 case R_MIPS16_TLS_LDM:
df58fc94
RS
6165 case R_MICROMIPS_TLS_GD:
6166 case R_MICROMIPS_TLS_GOTTPREL:
6167 case R_MICROMIPS_TLS_LDM:
6168 case R_MICROMIPS_GOT_DISP:
b49e97c9
TS
6169 value = g;
6170 overflowed_p = mips_elf_overflow_p (value, 16);
6171 break;
6172
6173 case R_MIPS_GPREL32:
bce03d3d
AO
6174 value = (addend + symbol + gp0 - gp);
6175 if (!save_addend)
6176 value &= howto->dst_mask;
b49e97c9
TS
6177 break;
6178
6179 case R_MIPS_PC16:
bad36eac 6180 case R_MIPS_GNU_REL16_S2:
c3eb94b4
MF
6181 if (howto->partial_inplace)
6182 addend = _bfd_mips_elf_sign_extend (addend, 18);
6183
9d862524 6184 /* No need to exclude weak undefined symbols here as they resolve
07d6d2b8
AM
6185 to 0 and never set `*cross_mode_jump_p', so this alignment check
6186 will never trigger for them. */
9d862524
MR
6187 if (*cross_mode_jump_p
6188 ? ((symbol + addend) & 3) != 1
6189 : ((symbol + addend) & 3) != 0)
c3eb94b4
MF
6190 return bfd_reloc_outofrange;
6191
6192 value = symbol + addend - p;
538baf8b
AB
6193 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6194 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
6195 value >>= howto->rightshift;
6196 value &= howto->dst_mask;
b49e97c9
TS
6197 break;
6198
c9775dde
MR
6199 case R_MIPS16_PC16_S1:
6200 if (howto->partial_inplace)
6201 addend = _bfd_mips_elf_sign_extend (addend, 17);
6202
6203 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
9d862524
MR
6204 && (*cross_mode_jump_p
6205 ? ((symbol + addend) & 3) != 0
6206 : ((symbol + addend) & 1) == 0))
c9775dde
MR
6207 return bfd_reloc_outofrange;
6208
6209 value = symbol + addend - p;
6210 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6211 overflowed_p = mips_elf_overflow_p (value, 17);
6212 value >>= howto->rightshift;
6213 value &= howto->dst_mask;
6214 break;
6215
7361da2c
AB
6216 case R_MIPS_PC21_S2:
6217 if (howto->partial_inplace)
6218 addend = _bfd_mips_elf_sign_extend (addend, 23);
6219
6220 if ((symbol + addend) & 3)
6221 return bfd_reloc_outofrange;
6222
6223 value = symbol + addend - p;
538baf8b
AB
6224 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6225 overflowed_p = mips_elf_overflow_p (value, 23);
7361da2c
AB
6226 value >>= howto->rightshift;
6227 value &= howto->dst_mask;
6228 break;
6229
6230 case R_MIPS_PC26_S2:
6231 if (howto->partial_inplace)
6232 addend = _bfd_mips_elf_sign_extend (addend, 28);
6233
6234 if ((symbol + addend) & 3)
6235 return bfd_reloc_outofrange;
6236
6237 value = symbol + addend - p;
538baf8b
AB
6238 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6239 overflowed_p = mips_elf_overflow_p (value, 28);
7361da2c
AB
6240 value >>= howto->rightshift;
6241 value &= howto->dst_mask;
6242 break;
6243
6244 case R_MIPS_PC18_S3:
6245 if (howto->partial_inplace)
6246 addend = _bfd_mips_elf_sign_extend (addend, 21);
6247
6248 if ((symbol + addend) & 7)
6249 return bfd_reloc_outofrange;
6250
6251 value = symbol + addend - ((p | 7) ^ 7);
538baf8b
AB
6252 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6253 overflowed_p = mips_elf_overflow_p (value, 21);
7361da2c
AB
6254 value >>= howto->rightshift;
6255 value &= howto->dst_mask;
6256 break;
6257
6258 case R_MIPS_PC19_S2:
6259 if (howto->partial_inplace)
6260 addend = _bfd_mips_elf_sign_extend (addend, 21);
6261
6262 if ((symbol + addend) & 3)
6263 return bfd_reloc_outofrange;
6264
6265 value = symbol + addend - p;
538baf8b
AB
6266 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6267 overflowed_p = mips_elf_overflow_p (value, 21);
7361da2c
AB
6268 value >>= howto->rightshift;
6269 value &= howto->dst_mask;
6270 break;
6271
6272 case R_MIPS_PCHI16:
6273 value = mips_elf_high (symbol + addend - p);
538baf8b
AB
6274 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6275 overflowed_p = mips_elf_overflow_p (value, 16);
7361da2c
AB
6276 value &= howto->dst_mask;
6277 break;
6278
6279 case R_MIPS_PCLO16:
6280 if (howto->partial_inplace)
6281 addend = _bfd_mips_elf_sign_extend (addend, 16);
6282 value = symbol + addend - p;
6283 value &= howto->dst_mask;
6284 break;
6285
df58fc94 6286 case R_MICROMIPS_PC7_S1:
c3eb94b4
MF
6287 if (howto->partial_inplace)
6288 addend = _bfd_mips_elf_sign_extend (addend, 8);
9d862524
MR
6289
6290 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6291 && (*cross_mode_jump_p
6292 ? ((symbol + addend + 2) & 3) != 0
6293 : ((symbol + addend + 2) & 1) == 0))
6294 return bfd_reloc_outofrange;
6295
c3eb94b4 6296 value = symbol + addend - p;
538baf8b
AB
6297 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6298 overflowed_p = mips_elf_overflow_p (value, 8);
df58fc94
RS
6299 value >>= howto->rightshift;
6300 value &= howto->dst_mask;
6301 break;
6302
6303 case R_MICROMIPS_PC10_S1:
c3eb94b4
MF
6304 if (howto->partial_inplace)
6305 addend = _bfd_mips_elf_sign_extend (addend, 11);
9d862524
MR
6306
6307 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6308 && (*cross_mode_jump_p
6309 ? ((symbol + addend + 2) & 3) != 0
6310 : ((symbol + addend + 2) & 1) == 0))
6311 return bfd_reloc_outofrange;
6312
c3eb94b4 6313 value = symbol + addend - p;
538baf8b
AB
6314 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6315 overflowed_p = mips_elf_overflow_p (value, 11);
df58fc94
RS
6316 value >>= howto->rightshift;
6317 value &= howto->dst_mask;
6318 break;
6319
6320 case R_MICROMIPS_PC16_S1:
c3eb94b4
MF
6321 if (howto->partial_inplace)
6322 addend = _bfd_mips_elf_sign_extend (addend, 17);
9d862524
MR
6323
6324 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6325 && (*cross_mode_jump_p
6326 ? ((symbol + addend) & 3) != 0
6327 : ((symbol + addend) & 1) == 0))
6328 return bfd_reloc_outofrange;
6329
c3eb94b4 6330 value = symbol + addend - p;
538baf8b
AB
6331 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6332 overflowed_p = mips_elf_overflow_p (value, 17);
df58fc94
RS
6333 value >>= howto->rightshift;
6334 value &= howto->dst_mask;
6335 break;
6336
6337 case R_MICROMIPS_PC23_S2:
c3eb94b4
MF
6338 if (howto->partial_inplace)
6339 addend = _bfd_mips_elf_sign_extend (addend, 25);
6340 value = symbol + addend - ((p | 3) ^ 3);
538baf8b
AB
6341 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6342 overflowed_p = mips_elf_overflow_p (value, 25);
df58fc94
RS
6343 value >>= howto->rightshift;
6344 value &= howto->dst_mask;
6345 break;
6346
b49e97c9
TS
6347 case R_MIPS_GOT_HI16:
6348 case R_MIPS_CALL_HI16:
df58fc94
RS
6349 case R_MICROMIPS_GOT_HI16:
6350 case R_MICROMIPS_CALL_HI16:
b49e97c9
TS
6351 /* We're allowed to handle these two relocations identically.
6352 The dynamic linker is allowed to handle the CALL relocations
6353 differently by creating a lazy evaluation stub. */
6354 value = g;
6355 value = mips_elf_high (value);
6356 value &= howto->dst_mask;
6357 break;
6358
6359 case R_MIPS_GOT_LO16:
6360 case R_MIPS_CALL_LO16:
df58fc94
RS
6361 case R_MICROMIPS_GOT_LO16:
6362 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
6363 value = g & howto->dst_mask;
6364 break;
6365
6366 case R_MIPS_GOT_PAGE:
df58fc94 6367 case R_MICROMIPS_GOT_PAGE:
5c18022e 6368 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
6369 if (value == MINUS_ONE)
6370 return bfd_reloc_outofrange;
a8028dd0 6371 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
6372 overflowed_p = mips_elf_overflow_p (value, 16);
6373 break;
6374
6375 case R_MIPS_GOT_OFST:
df58fc94 6376 case R_MICROMIPS_GOT_OFST:
93a2b7ae 6377 if (local_p)
5c18022e 6378 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
6379 else
6380 value = addend;
b49e97c9
TS
6381 overflowed_p = mips_elf_overflow_p (value, 16);
6382 break;
6383
6384 case R_MIPS_SUB:
df58fc94 6385 case R_MICROMIPS_SUB:
b49e97c9
TS
6386 value = symbol - addend;
6387 value &= howto->dst_mask;
6388 break;
6389
6390 case R_MIPS_HIGHER:
df58fc94 6391 case R_MICROMIPS_HIGHER:
b49e97c9
TS
6392 value = mips_elf_higher (addend + symbol);
6393 value &= howto->dst_mask;
6394 break;
6395
6396 case R_MIPS_HIGHEST:
df58fc94 6397 case R_MICROMIPS_HIGHEST:
b49e97c9
TS
6398 value = mips_elf_highest (addend + symbol);
6399 value &= howto->dst_mask;
6400 break;
6401
6402 case R_MIPS_SCN_DISP:
df58fc94 6403 case R_MICROMIPS_SCN_DISP:
b49e97c9
TS
6404 value = symbol + addend - sec->output_offset;
6405 value &= howto->dst_mask;
6406 break;
6407
b49e97c9 6408 case R_MIPS_JALR:
df58fc94 6409 case R_MICROMIPS_JALR:
1367d393
ILT
6410 /* This relocation is only a hint. In some cases, we optimize
6411 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
6412 when the symbol does not resolve locally. */
6413 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393 6414 return bfd_reloc_continue;
c1556ecd
MR
6415 /* We can't optimize cross-mode jumps either. */
6416 if (*cross_mode_jump_p)
6417 return bfd_reloc_continue;
1367d393 6418 value = symbol + addend;
c1556ecd
MR
6419 /* Neither we can non-instruction-aligned targets. */
6420 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6421 return bfd_reloc_continue;
1367d393 6422 break;
b49e97c9 6423
1367d393 6424 case R_MIPS_PJUMP:
b49e97c9
TS
6425 case R_MIPS_GNU_VTINHERIT:
6426 case R_MIPS_GNU_VTENTRY:
6427 /* We don't do anything with these at present. */
6428 return bfd_reloc_continue;
6429
6430 default:
6431 /* An unrecognized relocation type. */
6432 return bfd_reloc_notsupported;
6433 }
6434
6435 /* Store the VALUE for our caller. */
6436 *valuep = value;
6437 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6438}
6439
b49e97c9
TS
6440/* It has been determined that the result of the RELOCATION is the
6441 VALUE. Use HOWTO to place VALUE into the output file at the
6442 appropriate position. The SECTION is the section to which the
68ffbac6 6443 relocation applies.
38a7df63 6444 CROSS_MODE_JUMP_P is true if the relocation field
df58fc94 6445 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9 6446
b34976b6 6447 Returns FALSE if anything goes wrong. */
b49e97c9 6448
b34976b6 6449static bfd_boolean
9719ad41
RS
6450mips_elf_perform_relocation (struct bfd_link_info *info,
6451 reloc_howto_type *howto,
6452 const Elf_Internal_Rela *relocation,
6453 bfd_vma value, bfd *input_bfd,
6454 asection *input_section, bfd_byte *contents,
38a7df63 6455 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
6456{
6457 bfd_vma x;
6458 bfd_byte *location;
6459 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6460
6461 /* Figure out where the relocation is occurring. */
6462 location = contents + relocation->r_offset;
6463
df58fc94 6464 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
d6f16593 6465
b49e97c9
TS
6466 /* Obtain the current value. */
6467 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6468
6469 /* Clear the field we are setting. */
6470 x &= ~howto->dst_mask;
6471
b49e97c9
TS
6472 /* Set the field. */
6473 x |= (value & howto->dst_mask);
6474
a6ebf616 6475 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
9d862524
MR
6476 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6477 {
6478 bfd_vma opcode = x >> 26;
6479
6480 if (r_type == R_MIPS16_26 ? opcode == 0x7
6481 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6482 : opcode == 0x1d)
6483 {
6484 info->callbacks->einfo
2c1c9679 6485 (_("%X%H: unsupported JALX to the same ISA mode\n"),
9d862524
MR
6486 input_bfd, input_section, relocation->r_offset);
6487 return TRUE;
6488 }
6489 }
38a7df63 6490 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 6491 {
b34976b6 6492 bfd_boolean ok;
b49e97c9
TS
6493 bfd_vma opcode = x >> 26;
6494 bfd_vma jalx_opcode;
6495
6496 /* Check to see if the opcode is already JAL or JALX. */
6497 if (r_type == R_MIPS16_26)
6498 {
6499 ok = ((opcode == 0x6) || (opcode == 0x7));
6500 jalx_opcode = 0x7;
6501 }
df58fc94
RS
6502 else if (r_type == R_MICROMIPS_26_S1)
6503 {
6504 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6505 jalx_opcode = 0x3c;
6506 }
b49e97c9
TS
6507 else
6508 {
6509 ok = ((opcode == 0x3) || (opcode == 0x1d));
6510 jalx_opcode = 0x1d;
6511 }
6512
3bdf9505 6513 /* If the opcode is not JAL or JALX, there's a problem. We cannot
07d6d2b8 6514 convert J or JALS to JALX. */
b49e97c9
TS
6515 if (!ok)
6516 {
5f68df25 6517 info->callbacks->einfo
2c1c9679 6518 (_("%X%H: unsupported jump between ISA modes; "
5f68df25
MR
6519 "consider recompiling with interlinking enabled\n"),
6520 input_bfd, input_section, relocation->r_offset);
6521 return TRUE;
b49e97c9
TS
6522 }
6523
6524 /* Make this the JALX opcode. */
6525 x = (x & ~(0x3f << 26)) | (jalx_opcode << 26);
6526 }
9d862524
MR
6527 else if (cross_mode_jump_p && b_reloc_p (r_type))
6528 {
a6ebf616
MR
6529 bfd_boolean ok = FALSE;
6530 bfd_vma opcode = x >> 16;
6531 bfd_vma jalx_opcode = 0;
70e65ca8 6532 bfd_vma sign_bit = 0;
a6ebf616
MR
6533 bfd_vma addr;
6534 bfd_vma dest;
6535
6536 if (r_type == R_MICROMIPS_PC16_S1)
6537 {
6538 ok = opcode == 0x4060;
6539 jalx_opcode = 0x3c;
70e65ca8 6540 sign_bit = 0x10000;
a6ebf616
MR
6541 value <<= 1;
6542 }
6543 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6544 {
6545 ok = opcode == 0x411;
6546 jalx_opcode = 0x1d;
70e65ca8 6547 sign_bit = 0x20000;
a6ebf616
MR
6548 value <<= 2;
6549 }
6550
8b10b0b3 6551 if (ok && !bfd_link_pic (info))
a6ebf616 6552 {
8b10b0b3
MR
6553 addr = (input_section->output_section->vma
6554 + input_section->output_offset
6555 + relocation->r_offset
6556 + 4);
70e65ca8
MR
6557 dest = (addr
6558 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
a6ebf616 6559
8b10b0b3
MR
6560 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6561 {
6562 info->callbacks->einfo
2c1c9679 6563 (_("%X%H: cannot convert branch between ISA modes "
8b10b0b3
MR
6564 "to JALX: relocation out of range\n"),
6565 input_bfd, input_section, relocation->r_offset);
6566 return TRUE;
6567 }
a6ebf616 6568
8b10b0b3
MR
6569 /* Make this the JALX opcode. */
6570 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6571 }
6572 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
a6ebf616
MR
6573 {
6574 info->callbacks->einfo
2c1c9679 6575 (_("%X%H: unsupported branch between ISA modes\n"),
a6ebf616
MR
6576 input_bfd, input_section, relocation->r_offset);
6577 return TRUE;
6578 }
9d862524 6579 }
b49e97c9 6580
38a7df63
CF
6581 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6582 range. */
0e1862bb 6583 if (!bfd_link_relocatable (info)
38a7df63 6584 && !cross_mode_jump_p
cd8d5a82
CF
6585 && ((JAL_TO_BAL_P (input_bfd)
6586 && r_type == R_MIPS_26
0e392101 6587 && (x >> 26) == 0x3) /* jal addr */
cd8d5a82
CF
6588 || (JALR_TO_BAL_P (input_bfd)
6589 && r_type == R_MIPS_JALR
0e392101 6590 && x == 0x0320f809) /* jalr t9 */
38a7df63
CF
6591 || (JR_TO_B_P (input_bfd)
6592 && r_type == R_MIPS_JALR
0e392101 6593 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
1367d393
ILT
6594 {
6595 bfd_vma addr;
6596 bfd_vma dest;
6597 bfd_signed_vma off;
6598
6599 addr = (input_section->output_section->vma
6600 + input_section->output_offset
6601 + relocation->r_offset
6602 + 4);
6603 if (r_type == R_MIPS_26)
6604 dest = (value << 2) | ((addr >> 28) << 28);
6605 else
6606 dest = value;
6607 off = dest - addr;
6608 if (off <= 0x1ffff && off >= -0x20000)
38a7df63 6609 {
0e392101 6610 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
38a7df63
CF
6611 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6612 else
6613 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6614 }
1367d393
ILT
6615 }
6616
b49e97c9 6617 /* Put the value into the output. */
98e10ffa 6618 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
d6f16593 6619
0e1862bb 6620 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
df58fc94 6621 location);
d6f16593 6622
b34976b6 6623 return TRUE;
b49e97c9 6624}
b49e97c9 6625\f
b49e97c9
TS
6626/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6627 is the original relocation, which is now being transformed into a
6628 dynamic relocation. The ADDENDP is adjusted if necessary; the
6629 caller should store the result in place of the original addend. */
6630
b34976b6 6631static bfd_boolean
9719ad41
RS
6632mips_elf_create_dynamic_relocation (bfd *output_bfd,
6633 struct bfd_link_info *info,
6634 const Elf_Internal_Rela *rel,
6635 struct mips_elf_link_hash_entry *h,
6636 asection *sec, bfd_vma symbol,
6637 bfd_vma *addendp, asection *input_section)
b49e97c9 6638{
947216bf 6639 Elf_Internal_Rela outrel[3];
b49e97c9
TS
6640 asection *sreloc;
6641 bfd *dynobj;
6642 int r_type;
5d41f0b6
RS
6643 long indx;
6644 bfd_boolean defined_p;
0a44bf69 6645 struct mips_elf_link_hash_table *htab;
b49e97c9 6646
0a44bf69 6647 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
6648 BFD_ASSERT (htab != NULL);
6649
b49e97c9
TS
6650 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6651 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 6652 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
6653 BFD_ASSERT (sreloc != NULL);
6654 BFD_ASSERT (sreloc->contents != NULL);
6655 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 6656 < sreloc->size);
b49e97c9 6657
b49e97c9
TS
6658 outrel[0].r_offset =
6659 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
6660 if (ABI_64_P (output_bfd))
6661 {
6662 outrel[1].r_offset =
6663 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6664 outrel[2].r_offset =
6665 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6666 }
b49e97c9 6667
c5ae1840 6668 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 6669 /* The relocation field has been deleted. */
5d41f0b6
RS
6670 return TRUE;
6671
6672 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
6673 {
6674 /* The relocation field has been converted into a relative value of
6675 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6676 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 6677 *addendp += symbol;
5d41f0b6 6678 return TRUE;
0d591ff7 6679 }
b49e97c9 6680
5d41f0b6
RS
6681 /* We must now calculate the dynamic symbol table index to use
6682 in the relocation. */
d4a77f3f 6683 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5d41f0b6 6684 {
020d7251 6685 BFD_ASSERT (htab->is_vxworks || h->global_got_area != GGA_NONE);
5d41f0b6
RS
6686 indx = h->root.dynindx;
6687 if (SGI_COMPAT (output_bfd))
6688 defined_p = h->root.def_regular;
6689 else
6690 /* ??? glibc's ld.so just adds the final GOT entry to the
6691 relocation field. It therefore treats relocs against
6692 defined symbols in the same way as relocs against
6693 undefined symbols. */
6694 defined_p = FALSE;
6695 }
b49e97c9
TS
6696 else
6697 {
5d41f0b6
RS
6698 if (sec != NULL && bfd_is_abs_section (sec))
6699 indx = 0;
6700 else if (sec == NULL || sec->owner == NULL)
fdd07405 6701 {
5d41f0b6
RS
6702 bfd_set_error (bfd_error_bad_value);
6703 return FALSE;
b49e97c9
TS
6704 }
6705 else
6706 {
5d41f0b6 6707 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
6708 if (indx == 0)
6709 {
6710 asection *osec = htab->root.text_index_section;
6711 indx = elf_section_data (osec)->dynindx;
6712 }
5d41f0b6
RS
6713 if (indx == 0)
6714 abort ();
b49e97c9
TS
6715 }
6716
5d41f0b6
RS
6717 /* Instead of generating a relocation using the section
6718 symbol, we may as well make it a fully relative
6719 relocation. We want to avoid generating relocations to
6720 local symbols because we used to generate them
6721 incorrectly, without adding the original symbol value,
6722 which is mandated by the ABI for section symbols. In
6723 order to give dynamic loaders and applications time to
6724 phase out the incorrect use, we refrain from emitting
6725 section-relative relocations. It's not like they're
6726 useful, after all. This should be a bit more efficient
6727 as well. */
6728 /* ??? Although this behavior is compatible with glibc's ld.so,
6729 the ABI says that relocations against STN_UNDEF should have
6730 a symbol value of 0. Irix rld honors this, so relocations
6731 against STN_UNDEF have no effect. */
6732 if (!SGI_COMPAT (output_bfd))
6733 indx = 0;
6734 defined_p = TRUE;
b49e97c9
TS
6735 }
6736
5d41f0b6
RS
6737 /* If the relocation was previously an absolute relocation and
6738 this symbol will not be referred to by the relocation, we must
6739 adjust it by the value we give it in the dynamic symbol table.
6740 Otherwise leave the job up to the dynamic linker. */
6741 if (defined_p && r_type != R_MIPS_REL32)
6742 *addendp += symbol;
6743
0a44bf69
RS
6744 if (htab->is_vxworks)
6745 /* VxWorks uses non-relative relocations for this. */
6746 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6747 else
6748 /* The relocation is always an REL32 relocation because we don't
6749 know where the shared library will wind up at load-time. */
6750 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6751 R_MIPS_REL32);
6752
5d41f0b6
RS
6753 /* For strict adherence to the ABI specification, we should
6754 generate a R_MIPS_64 relocation record by itself before the
6755 _REL32/_64 record as well, such that the addend is read in as
6756 a 64-bit value (REL32 is a 32-bit relocation, after all).
6757 However, since none of the existing ELF64 MIPS dynamic
6758 loaders seems to care, we don't waste space with these
6759 artificial relocations. If this turns out to not be true,
6760 mips_elf_allocate_dynamic_relocation() should be tweaked so
6761 as to make room for a pair of dynamic relocations per
6762 invocation if ABI_64_P, and here we should generate an
6763 additional relocation record with R_MIPS_64 by itself for a
6764 NULL symbol before this relocation record. */
6765 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6766 ABI_64_P (output_bfd)
6767 ? R_MIPS_64
6768 : R_MIPS_NONE);
6769 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6770
6771 /* Adjust the output offset of the relocation to reference the
6772 correct location in the output file. */
6773 outrel[0].r_offset += (input_section->output_section->vma
6774 + input_section->output_offset);
6775 outrel[1].r_offset += (input_section->output_section->vma
6776 + input_section->output_offset);
6777 outrel[2].r_offset += (input_section->output_section->vma
6778 + input_section->output_offset);
6779
b49e97c9
TS
6780 /* Put the relocation back out. We have to use the special
6781 relocation outputter in the 64-bit case since the 64-bit
6782 relocation format is non-standard. */
6783 if (ABI_64_P (output_bfd))
6784 {
6785 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6786 (output_bfd, &outrel[0],
6787 (sreloc->contents
6788 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6789 }
0a44bf69
RS
6790 else if (htab->is_vxworks)
6791 {
6792 /* VxWorks uses RELA rather than REL dynamic relocations. */
6793 outrel[0].r_addend = *addendp;
6794 bfd_elf32_swap_reloca_out
6795 (output_bfd, &outrel[0],
6796 (sreloc->contents
6797 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6798 }
b49e97c9 6799 else
947216bf
AM
6800 bfd_elf32_swap_reloc_out
6801 (output_bfd, &outrel[0],
6802 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 6803
b49e97c9
TS
6804 /* We've now added another relocation. */
6805 ++sreloc->reloc_count;
6806
6807 /* Make sure the output section is writable. The dynamic linker
6808 will be writing to it. */
6809 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6810 |= SHF_WRITE;
6811
6812 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 6813 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9 6814 {
3d4d4302 6815 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
6816 bfd_byte *cr;
6817
6818 if (scpt)
6819 {
6820 Elf32_crinfo cptrel;
6821
6822 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6823 cptrel.vaddr = (rel->r_offset
6824 + input_section->output_section->vma
6825 + input_section->output_offset);
6826 if (r_type == R_MIPS_REL32)
6827 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6828 else
6829 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6830 mips_elf_set_cr_dist2to (cptrel, 0);
6831 cptrel.konst = *addendp;
6832
6833 cr = (scpt->contents
6834 + sizeof (Elf32_External_compact_rel));
abc0f8d0 6835 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
6836 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6837 ((Elf32_External_crinfo *) cr
6838 + scpt->reloc_count));
6839 ++scpt->reloc_count;
6840 }
6841 }
6842
943284cc
DJ
6843 /* If we've written this relocation for a readonly section,
6844 we need to set DF_TEXTREL again, so that we do not delete the
6845 DT_TEXTREL tag. */
6846 if (MIPS_ELF_READONLY_SECTION (input_section))
6847 info->flags |= DF_TEXTREL;
6848
b34976b6 6849 return TRUE;
b49e97c9
TS
6850}
6851\f
b49e97c9
TS
6852/* Return the MACH for a MIPS e_flags value. */
6853
6854unsigned long
9719ad41 6855_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
6856{
6857 switch (flags & EF_MIPS_MACH)
6858 {
6859 case E_MIPS_MACH_3900:
6860 return bfd_mach_mips3900;
6861
6862 case E_MIPS_MACH_4010:
6863 return bfd_mach_mips4010;
6864
6865 case E_MIPS_MACH_4100:
6866 return bfd_mach_mips4100;
6867
6868 case E_MIPS_MACH_4111:
6869 return bfd_mach_mips4111;
6870
00707a0e
RS
6871 case E_MIPS_MACH_4120:
6872 return bfd_mach_mips4120;
6873
b49e97c9
TS
6874 case E_MIPS_MACH_4650:
6875 return bfd_mach_mips4650;
6876
00707a0e
RS
6877 case E_MIPS_MACH_5400:
6878 return bfd_mach_mips5400;
6879
6880 case E_MIPS_MACH_5500:
6881 return bfd_mach_mips5500;
6882
e407c74b
NC
6883 case E_MIPS_MACH_5900:
6884 return bfd_mach_mips5900;
6885
0d2e43ed
ILT
6886 case E_MIPS_MACH_9000:
6887 return bfd_mach_mips9000;
6888
b49e97c9
TS
6889 case E_MIPS_MACH_SB1:
6890 return bfd_mach_mips_sb1;
6891
350cc38d
MS
6892 case E_MIPS_MACH_LS2E:
6893 return bfd_mach_mips_loongson_2e;
6894
6895 case E_MIPS_MACH_LS2F:
6896 return bfd_mach_mips_loongson_2f;
6897
ac8cb70f
CX
6898 case E_MIPS_MACH_GS464:
6899 return bfd_mach_mips_gs464;
fd503541 6900
bd782c07
CX
6901 case E_MIPS_MACH_GS464E:
6902 return bfd_mach_mips_gs464e;
6903
9108bc33
CX
6904 case E_MIPS_MACH_GS264E:
6905 return bfd_mach_mips_gs264e;
6906
2c629856
N
6907 case E_MIPS_MACH_OCTEON3:
6908 return bfd_mach_mips_octeon3;
6909
432233b3
AP
6910 case E_MIPS_MACH_OCTEON2:
6911 return bfd_mach_mips_octeon2;
6912
6f179bd0
AN
6913 case E_MIPS_MACH_OCTEON:
6914 return bfd_mach_mips_octeon;
6915
52b6b6b9
JM
6916 case E_MIPS_MACH_XLR:
6917 return bfd_mach_mips_xlr;
6918
38bf472a
MR
6919 case E_MIPS_MACH_IAMR2:
6920 return bfd_mach_mips_interaptiv_mr2;
6921
b49e97c9
TS
6922 default:
6923 switch (flags & EF_MIPS_ARCH)
6924 {
6925 default:
6926 case E_MIPS_ARCH_1:
6927 return bfd_mach_mips3000;
b49e97c9
TS
6928
6929 case E_MIPS_ARCH_2:
6930 return bfd_mach_mips6000;
b49e97c9
TS
6931
6932 case E_MIPS_ARCH_3:
6933 return bfd_mach_mips4000;
b49e97c9
TS
6934
6935 case E_MIPS_ARCH_4:
6936 return bfd_mach_mips8000;
b49e97c9
TS
6937
6938 case E_MIPS_ARCH_5:
6939 return bfd_mach_mips5;
b49e97c9
TS
6940
6941 case E_MIPS_ARCH_32:
6942 return bfd_mach_mipsisa32;
b49e97c9
TS
6943
6944 case E_MIPS_ARCH_64:
6945 return bfd_mach_mipsisa64;
af7ee8bf
CD
6946
6947 case E_MIPS_ARCH_32R2:
6948 return bfd_mach_mipsisa32r2;
5f74bc13
CD
6949
6950 case E_MIPS_ARCH_64R2:
6951 return bfd_mach_mipsisa64r2;
7361da2c
AB
6952
6953 case E_MIPS_ARCH_32R6:
6954 return bfd_mach_mipsisa32r6;
6955
6956 case E_MIPS_ARCH_64R6:
6957 return bfd_mach_mipsisa64r6;
b49e97c9
TS
6958 }
6959 }
6960
6961 return 0;
6962}
6963
6964/* Return printable name for ABI. */
6965
6966static INLINE char *
9719ad41 6967elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
6968{
6969 flagword flags;
6970
6971 flags = elf_elfheader (abfd)->e_flags;
6972 switch (flags & EF_MIPS_ABI)
6973 {
6974 case 0:
6975 if (ABI_N32_P (abfd))
6976 return "N32";
6977 else if (ABI_64_P (abfd))
6978 return "64";
6979 else
6980 return "none";
6981 case E_MIPS_ABI_O32:
6982 return "O32";
6983 case E_MIPS_ABI_O64:
6984 return "O64";
6985 case E_MIPS_ABI_EABI32:
6986 return "EABI32";
6987 case E_MIPS_ABI_EABI64:
6988 return "EABI64";
6989 default:
6990 return "unknown abi";
6991 }
6992}
6993\f
6994/* MIPS ELF uses two common sections. One is the usual one, and the
6995 other is for small objects. All the small objects are kept
6996 together, and then referenced via the gp pointer, which yields
6997 faster assembler code. This is what we use for the small common
6998 section. This approach is copied from ecoff.c. */
6999static asection mips_elf_scom_section;
7000static asymbol mips_elf_scom_symbol;
7001static asymbol *mips_elf_scom_symbol_ptr;
7002
7003/* MIPS ELF also uses an acommon section, which represents an
7004 allocated common symbol which may be overridden by a
7005 definition in a shared library. */
7006static asection mips_elf_acom_section;
7007static asymbol mips_elf_acom_symbol;
7008static asymbol *mips_elf_acom_symbol_ptr;
7009
738e5348 7010/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
7011
7012void
9719ad41 7013_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
7014{
7015 elf_symbol_type *elfsym;
7016
738e5348 7017 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
7018 elfsym = (elf_symbol_type *) asym;
7019 switch (elfsym->internal_elf_sym.st_shndx)
7020 {
7021 case SHN_MIPS_ACOMMON:
7022 /* This section is used in a dynamically linked executable file.
7023 It is an allocated common section. The dynamic linker can
7024 either resolve these symbols to something in a shared
7025 library, or it can just leave them here. For our purposes,
7026 we can consider these symbols to be in a new section. */
7027 if (mips_elf_acom_section.name == NULL)
7028 {
7029 /* Initialize the acommon section. */
7030 mips_elf_acom_section.name = ".acommon";
7031 mips_elf_acom_section.flags = SEC_ALLOC;
7032 mips_elf_acom_section.output_section = &mips_elf_acom_section;
7033 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
7034 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
7035 mips_elf_acom_symbol.name = ".acommon";
7036 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
7037 mips_elf_acom_symbol.section = &mips_elf_acom_section;
7038 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
7039 }
7040 asym->section = &mips_elf_acom_section;
7041 break;
7042
7043 case SHN_COMMON:
7044 /* Common symbols less than the GP size are automatically
7045 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
7046 if (asym->value > elf_gp_size (abfd)
b59eed79 7047 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
7048 || IRIX_COMPAT (abfd) == ict_irix6)
7049 break;
7050 /* Fall through. */
7051 case SHN_MIPS_SCOMMON:
7052 if (mips_elf_scom_section.name == NULL)
7053 {
7054 /* Initialize the small common section. */
7055 mips_elf_scom_section.name = ".scommon";
7056 mips_elf_scom_section.flags = SEC_IS_COMMON;
7057 mips_elf_scom_section.output_section = &mips_elf_scom_section;
7058 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
7059 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
7060 mips_elf_scom_symbol.name = ".scommon";
7061 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
7062 mips_elf_scom_symbol.section = &mips_elf_scom_section;
7063 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
7064 }
7065 asym->section = &mips_elf_scom_section;
7066 asym->value = elfsym->internal_elf_sym.st_size;
7067 break;
7068
7069 case SHN_MIPS_SUNDEFINED:
7070 asym->section = bfd_und_section_ptr;
7071 break;
7072
b49e97c9 7073 case SHN_MIPS_TEXT:
00b4930b
TS
7074 {
7075 asection *section = bfd_get_section_by_name (abfd, ".text");
7076
00b4930b
TS
7077 if (section != NULL)
7078 {
7079 asym->section = section;
7080 /* MIPS_TEXT is a bit special, the address is not an offset
de194d85 7081 to the base of the .text section. So subtract the section
00b4930b
TS
7082 base address to make it an offset. */
7083 asym->value -= section->vma;
7084 }
7085 }
b49e97c9
TS
7086 break;
7087
7088 case SHN_MIPS_DATA:
00b4930b
TS
7089 {
7090 asection *section = bfd_get_section_by_name (abfd, ".data");
7091
00b4930b
TS
7092 if (section != NULL)
7093 {
7094 asym->section = section;
7095 /* MIPS_DATA is a bit special, the address is not an offset
de194d85 7096 to the base of the .data section. So subtract the section
00b4930b
TS
7097 base address to make it an offset. */
7098 asym->value -= section->vma;
7099 }
7100 }
b49e97c9 7101 break;
b49e97c9 7102 }
738e5348 7103
df58fc94
RS
7104 /* If this is an odd-valued function symbol, assume it's a MIPS16
7105 or microMIPS one. */
738e5348
RS
7106 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7107 && (asym->value & 1) != 0)
7108 {
7109 asym->value--;
e8faf7d1 7110 if (MICROMIPS_P (abfd))
df58fc94
RS
7111 elfsym->internal_elf_sym.st_other
7112 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7113 else
7114 elfsym->internal_elf_sym.st_other
7115 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
738e5348 7116 }
b49e97c9
TS
7117}
7118\f
8c946ed5
RS
7119/* Implement elf_backend_eh_frame_address_size. This differs from
7120 the default in the way it handles EABI64.
7121
7122 EABI64 was originally specified as an LP64 ABI, and that is what
7123 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7124 historically accepted the combination of -mabi=eabi and -mlong32,
7125 and this ILP32 variation has become semi-official over time.
7126 Both forms use elf32 and have pointer-sized FDE addresses.
7127
7128 If an EABI object was generated by GCC 4.0 or above, it will have
7129 an empty .gcc_compiled_longXX section, where XX is the size of longs
7130 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7131 have no special marking to distinguish them from LP64 objects.
7132
7133 We don't want users of the official LP64 ABI to be punished for the
7134 existence of the ILP32 variant, but at the same time, we don't want
7135 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7136 We therefore take the following approach:
7137
7138 - If ABFD contains a .gcc_compiled_longXX section, use it to
07d6d2b8 7139 determine the pointer size.
8c946ed5
RS
7140
7141 - Otherwise check the type of the first relocation. Assume that
07d6d2b8 7142 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
8c946ed5
RS
7143
7144 - Otherwise punt.
7145
7146 The second check is enough to detect LP64 objects generated by pre-4.0
7147 compilers because, in the kind of output generated by those compilers,
7148 the first relocation will be associated with either a CIE personality
7149 routine or an FDE start address. Furthermore, the compilers never
7150 used a special (non-pointer) encoding for this ABI.
7151
7152 Checking the relocation type should also be safe because there is no
7153 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7154 did so. */
7155
7156unsigned int
76c20d54 7157_bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
8c946ed5
RS
7158{
7159 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7160 return 8;
7161 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7162 {
7163 bfd_boolean long32_p, long64_p;
7164
7165 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7166 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7167 if (long32_p && long64_p)
7168 return 0;
7169 if (long32_p)
7170 return 4;
7171 if (long64_p)
7172 return 8;
7173
7174 if (sec->reloc_count > 0
7175 && elf_section_data (sec)->relocs != NULL
7176 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7177 == R_MIPS_64))
7178 return 8;
7179
7180 return 0;
7181 }
7182 return 4;
7183}
7184\f
174fd7f9
RS
7185/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7186 relocations against two unnamed section symbols to resolve to the
7187 same address. For example, if we have code like:
7188
7189 lw $4,%got_disp(.data)($gp)
7190 lw $25,%got_disp(.text)($gp)
7191 jalr $25
7192
7193 then the linker will resolve both relocations to .data and the program
7194 will jump there rather than to .text.
7195
7196 We can work around this problem by giving names to local section symbols.
7197 This is also what the MIPSpro tools do. */
7198
7199bfd_boolean
7200_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7201{
7202 return SGI_COMPAT (abfd);
7203}
7204\f
b49e97c9
TS
7205/* Work over a section just before writing it out. This routine is
7206 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7207 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7208 a better way. */
7209
b34976b6 7210bfd_boolean
9719ad41 7211_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
7212{
7213 if (hdr->sh_type == SHT_MIPS_REGINFO
7214 && hdr->sh_size > 0)
7215 {
7216 bfd_byte buf[4];
7217
b49e97c9
TS
7218 BFD_ASSERT (hdr->contents == NULL);
7219
2d6dda71
MR
7220 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7221 {
7222 _bfd_error_handler
2c1c9679 7223 (_("%pB: incorrect `.reginfo' section size; "
2dcf00ce
AM
7224 "expected %" PRIu64 ", got %" PRIu64),
7225 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7226 (uint64_t) hdr->sh_size);
2d6dda71
MR
7227 bfd_set_error (bfd_error_bad_value);
7228 return FALSE;
7229 }
7230
b49e97c9
TS
7231 if (bfd_seek (abfd,
7232 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7233 SEEK_SET) != 0)
b34976b6 7234 return FALSE;
b49e97c9 7235 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 7236 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 7237 return FALSE;
b49e97c9
TS
7238 }
7239
7240 if (hdr->sh_type == SHT_MIPS_OPTIONS
7241 && hdr->bfd_section != NULL
f0abc2a1
AM
7242 && mips_elf_section_data (hdr->bfd_section) != NULL
7243 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
7244 {
7245 bfd_byte *contents, *l, *lend;
7246
f0abc2a1
AM
7247 /* We stored the section contents in the tdata field in the
7248 set_section_contents routine. We save the section contents
7249 so that we don't have to read them again.
b49e97c9
TS
7250 At this point we know that elf_gp is set, so we can look
7251 through the section contents to see if there is an
7252 ODK_REGINFO structure. */
7253
f0abc2a1 7254 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
7255 l = contents;
7256 lend = contents + hdr->sh_size;
7257 while (l + sizeof (Elf_External_Options) <= lend)
7258 {
7259 Elf_Internal_Options intopt;
7260
7261 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7262 &intopt);
1bc8074d
MR
7263 if (intopt.size < sizeof (Elf_External_Options))
7264 {
4eca0228 7265 _bfd_error_handler
695344c0 7266 /* xgettext:c-format */
2c1c9679 7267 (_("%pB: warning: bad `%s' option size %u smaller than"
63a5468a 7268 " its header"),
1bc8074d
MR
7269 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7270 break;
7271 }
b49e97c9
TS
7272 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7273 {
7274 bfd_byte buf[8];
7275
7276 if (bfd_seek (abfd,
7277 (hdr->sh_offset
7278 + (l - contents)
7279 + sizeof (Elf_External_Options)
7280 + (sizeof (Elf64_External_RegInfo) - 8)),
7281 SEEK_SET) != 0)
b34976b6 7282 return FALSE;
b49e97c9 7283 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 7284 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 7285 return FALSE;
b49e97c9
TS
7286 }
7287 else if (intopt.kind == ODK_REGINFO)
7288 {
7289 bfd_byte buf[4];
7290
7291 if (bfd_seek (abfd,
7292 (hdr->sh_offset
7293 + (l - contents)
7294 + sizeof (Elf_External_Options)
7295 + (sizeof (Elf32_External_RegInfo) - 4)),
7296 SEEK_SET) != 0)
b34976b6 7297 return FALSE;
b49e97c9 7298 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 7299 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 7300 return FALSE;
b49e97c9
TS
7301 }
7302 l += intopt.size;
7303 }
7304 }
7305
7306 if (hdr->bfd_section != NULL)
7307 {
7308 const char *name = bfd_get_section_name (abfd, hdr->bfd_section);
7309
2d0f9ad9
JM
7310 /* .sbss is not handled specially here because the GNU/Linux
7311 prelinker can convert .sbss from NOBITS to PROGBITS and
7312 changing it back to NOBITS breaks the binary. The entry in
7313 _bfd_mips_elf_special_sections will ensure the correct flags
7314 are set on .sbss if BFD creates it without reading it from an
7315 input file, and without special handling here the flags set
7316 on it in an input file will be followed. */
b49e97c9
TS
7317 if (strcmp (name, ".sdata") == 0
7318 || strcmp (name, ".lit8") == 0
7319 || strcmp (name, ".lit4") == 0)
fd6f9d17 7320 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
b49e97c9 7321 else if (strcmp (name, ".srdata") == 0)
fd6f9d17 7322 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
b49e97c9 7323 else if (strcmp (name, ".compact_rel") == 0)
fd6f9d17 7324 hdr->sh_flags = 0;
b49e97c9
TS
7325 else if (strcmp (name, ".rtproc") == 0)
7326 {
7327 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7328 {
7329 unsigned int adjust;
7330
7331 adjust = hdr->sh_size % hdr->sh_addralign;
7332 if (adjust != 0)
7333 hdr->sh_size += hdr->sh_addralign - adjust;
7334 }
7335 }
7336 }
7337
b34976b6 7338 return TRUE;
b49e97c9
TS
7339}
7340
7341/* Handle a MIPS specific section when reading an object file. This
7342 is called when elfcode.h finds a section with an unknown type.
7343 This routine supports both the 32-bit and 64-bit ELF ABI.
7344
7345 FIXME: We need to handle the SHF_MIPS_GPREL flag, but I'm not sure
7346 how to. */
7347
b34976b6 7348bfd_boolean
6dc132d9
L
7349_bfd_mips_elf_section_from_shdr (bfd *abfd,
7350 Elf_Internal_Shdr *hdr,
7351 const char *name,
7352 int shindex)
b49e97c9
TS
7353{
7354 flagword flags = 0;
7355
7356 /* There ought to be a place to keep ELF backend specific flags, but
7357 at the moment there isn't one. We just keep track of the
7358 sections by their name, instead. Fortunately, the ABI gives
7359 suggested names for all the MIPS specific sections, so we will
7360 probably get away with this. */
7361 switch (hdr->sh_type)
7362 {
7363 case SHT_MIPS_LIBLIST:
7364 if (strcmp (name, ".liblist") != 0)
b34976b6 7365 return FALSE;
b49e97c9
TS
7366 break;
7367 case SHT_MIPS_MSYM:
7368 if (strcmp (name, ".msym") != 0)
b34976b6 7369 return FALSE;
b49e97c9
TS
7370 break;
7371 case SHT_MIPS_CONFLICT:
7372 if (strcmp (name, ".conflict") != 0)
b34976b6 7373 return FALSE;
b49e97c9
TS
7374 break;
7375 case SHT_MIPS_GPTAB:
0112cd26 7376 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 7377 return FALSE;
b49e97c9
TS
7378 break;
7379 case SHT_MIPS_UCODE:
7380 if (strcmp (name, ".ucode") != 0)
b34976b6 7381 return FALSE;
b49e97c9
TS
7382 break;
7383 case SHT_MIPS_DEBUG:
7384 if (strcmp (name, ".mdebug") != 0)
b34976b6 7385 return FALSE;
b49e97c9
TS
7386 flags = SEC_DEBUGGING;
7387 break;
7388 case SHT_MIPS_REGINFO:
7389 if (strcmp (name, ".reginfo") != 0
7390 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 7391 return FALSE;
b49e97c9
TS
7392 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7393 break;
7394 case SHT_MIPS_IFACE:
7395 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 7396 return FALSE;
b49e97c9
TS
7397 break;
7398 case SHT_MIPS_CONTENT:
0112cd26 7399 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 7400 return FALSE;
b49e97c9
TS
7401 break;
7402 case SHT_MIPS_OPTIONS:
cc2e31b9 7403 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 7404 return FALSE;
b49e97c9 7405 break;
351cdf24
MF
7406 case SHT_MIPS_ABIFLAGS:
7407 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7408 return FALSE;
7409 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7410 break;
b49e97c9 7411 case SHT_MIPS_DWARF:
1b315056 7412 if (! CONST_STRNEQ (name, ".debug_")
07d6d2b8 7413 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 7414 return FALSE;
b49e97c9
TS
7415 break;
7416 case SHT_MIPS_SYMBOL_LIB:
7417 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 7418 return FALSE;
b49e97c9
TS
7419 break;
7420 case SHT_MIPS_EVENTS:
0112cd26
NC
7421 if (! CONST_STRNEQ (name, ".MIPS.events")
7422 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 7423 return FALSE;
b49e97c9
TS
7424 break;
7425 default:
cc2e31b9 7426 break;
b49e97c9
TS
7427 }
7428
6dc132d9 7429 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 7430 return FALSE;
b49e97c9
TS
7431
7432 if (flags)
7433 {
7434 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
7435 (bfd_get_section_flags (abfd,
7436 hdr->bfd_section)
7437 | flags)))
b34976b6 7438 return FALSE;
b49e97c9
TS
7439 }
7440
351cdf24
MF
7441 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7442 {
7443 Elf_External_ABIFlags_v0 ext;
7444
7445 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7446 &ext, 0, sizeof ext))
7447 return FALSE;
7448 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7449 &mips_elf_tdata (abfd)->abiflags);
7450 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7451 return FALSE;
7452 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
7453 }
7454
b49e97c9
TS
7455 /* FIXME: We should record sh_info for a .gptab section. */
7456
7457 /* For a .reginfo section, set the gp value in the tdata information
7458 from the contents of this section. We need the gp value while
7459 processing relocs, so we just get it now. The .reginfo section
7460 is not used in the 64-bit MIPS ELF ABI. */
7461 if (hdr->sh_type == SHT_MIPS_REGINFO)
7462 {
7463 Elf32_External_RegInfo ext;
7464 Elf32_RegInfo s;
7465
9719ad41
RS
7466 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7467 &ext, 0, sizeof ext))
b34976b6 7468 return FALSE;
b49e97c9
TS
7469 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7470 elf_gp (abfd) = s.ri_gp_value;
7471 }
7472
7473 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7474 set the gp value based on what we find. We may see both
7475 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7476 they should agree. */
7477 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7478 {
7479 bfd_byte *contents, *l, *lend;
7480
9719ad41 7481 contents = bfd_malloc (hdr->sh_size);
b49e97c9 7482 if (contents == NULL)
b34976b6 7483 return FALSE;
b49e97c9 7484 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 7485 0, hdr->sh_size))
b49e97c9
TS
7486 {
7487 free (contents);
b34976b6 7488 return FALSE;
b49e97c9
TS
7489 }
7490 l = contents;
7491 lend = contents + hdr->sh_size;
7492 while (l + sizeof (Elf_External_Options) <= lend)
7493 {
7494 Elf_Internal_Options intopt;
7495
7496 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7497 &intopt);
1bc8074d
MR
7498 if (intopt.size < sizeof (Elf_External_Options))
7499 {
4eca0228 7500 _bfd_error_handler
695344c0 7501 /* xgettext:c-format */
2c1c9679 7502 (_("%pB: warning: bad `%s' option size %u smaller than"
63a5468a 7503 " its header"),
1bc8074d
MR
7504 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7505 break;
7506 }
b49e97c9
TS
7507 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7508 {
7509 Elf64_Internal_RegInfo intreg;
7510
7511 bfd_mips_elf64_swap_reginfo_in
7512 (abfd,
7513 ((Elf64_External_RegInfo *)
7514 (l + sizeof (Elf_External_Options))),
7515 &intreg);
7516 elf_gp (abfd) = intreg.ri_gp_value;
7517 }
7518 else if (intopt.kind == ODK_REGINFO)
7519 {
7520 Elf32_RegInfo intreg;
7521
7522 bfd_mips_elf32_swap_reginfo_in
7523 (abfd,
7524 ((Elf32_External_RegInfo *)
7525 (l + sizeof (Elf_External_Options))),
7526 &intreg);
7527 elf_gp (abfd) = intreg.ri_gp_value;
7528 }
7529 l += intopt.size;
7530 }
7531 free (contents);
7532 }
7533
b34976b6 7534 return TRUE;
b49e97c9
TS
7535}
7536
7537/* Set the correct type for a MIPS ELF section. We do this by the
7538 section name, which is a hack, but ought to work. This routine is
7539 used by both the 32-bit and the 64-bit ABI. */
7540
b34976b6 7541bfd_boolean
9719ad41 7542_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 7543{
0414f35b 7544 const char *name = bfd_get_section_name (abfd, sec);
b49e97c9
TS
7545
7546 if (strcmp (name, ".liblist") == 0)
7547 {
7548 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 7549 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
7550 /* The sh_link field is set in final_write_processing. */
7551 }
7552 else if (strcmp (name, ".conflict") == 0)
7553 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 7554 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
7555 {
7556 hdr->sh_type = SHT_MIPS_GPTAB;
7557 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7558 /* The sh_info field is set in final_write_processing. */
7559 }
7560 else if (strcmp (name, ".ucode") == 0)
7561 hdr->sh_type = SHT_MIPS_UCODE;
7562 else if (strcmp (name, ".mdebug") == 0)
7563 {
7564 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 7565 /* In a shared object on IRIX 5.3, the .mdebug section has an
07d6d2b8 7566 entsize of 0. FIXME: Does this matter? */
b49e97c9
TS
7567 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7568 hdr->sh_entsize = 0;
7569 else
7570 hdr->sh_entsize = 1;
7571 }
7572 else if (strcmp (name, ".reginfo") == 0)
7573 {
7574 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 7575 /* In a shared object on IRIX 5.3, the .reginfo section has an
07d6d2b8 7576 entsize of 0x18. FIXME: Does this matter? */
b49e97c9
TS
7577 if (SGI_COMPAT (abfd))
7578 {
7579 if ((abfd->flags & DYNAMIC) != 0)
7580 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7581 else
7582 hdr->sh_entsize = 1;
7583 }
7584 else
7585 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7586 }
7587 else if (SGI_COMPAT (abfd)
7588 && (strcmp (name, ".hash") == 0
7589 || strcmp (name, ".dynamic") == 0
7590 || strcmp (name, ".dynstr") == 0))
7591 {
7592 if (SGI_COMPAT (abfd))
7593 hdr->sh_entsize = 0;
7594#if 0
8dc1a139 7595 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
7596 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7597#endif
7598 }
7599 else if (strcmp (name, ".got") == 0
7600 || strcmp (name, ".srdata") == 0
7601 || strcmp (name, ".sdata") == 0
7602 || strcmp (name, ".sbss") == 0
7603 || strcmp (name, ".lit4") == 0
7604 || strcmp (name, ".lit8") == 0)
7605 hdr->sh_flags |= SHF_MIPS_GPREL;
7606 else if (strcmp (name, ".MIPS.interfaces") == 0)
7607 {
7608 hdr->sh_type = SHT_MIPS_IFACE;
7609 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7610 }
0112cd26 7611 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
7612 {
7613 hdr->sh_type = SHT_MIPS_CONTENT;
7614 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7615 /* The sh_info field is set in final_write_processing. */
7616 }
cc2e31b9 7617 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
7618 {
7619 hdr->sh_type = SHT_MIPS_OPTIONS;
7620 hdr->sh_entsize = 1;
7621 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7622 }
351cdf24
MF
7623 else if (CONST_STRNEQ (name, ".MIPS.abiflags"))
7624 {
7625 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7626 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7627 }
1b315056 7628 else if (CONST_STRNEQ (name, ".debug_")
07d6d2b8 7629 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
7630 {
7631 hdr->sh_type = SHT_MIPS_DWARF;
7632
7633 /* Irix facilities such as libexc expect a single .debug_frame
7634 per executable, the system ones have NOSTRIP set and the linker
7635 doesn't merge sections with different flags so ... */
7636 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7637 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7638 }
b49e97c9
TS
7639 else if (strcmp (name, ".MIPS.symlib") == 0)
7640 {
7641 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7642 /* The sh_link and sh_info fields are set in
07d6d2b8 7643 final_write_processing. */
b49e97c9 7644 }
0112cd26
NC
7645 else if (CONST_STRNEQ (name, ".MIPS.events")
7646 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
7647 {
7648 hdr->sh_type = SHT_MIPS_EVENTS;
7649 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7650 /* The sh_link field is set in final_write_processing. */
7651 }
7652 else if (strcmp (name, ".msym") == 0)
7653 {
7654 hdr->sh_type = SHT_MIPS_MSYM;
7655 hdr->sh_flags |= SHF_ALLOC;
7656 hdr->sh_entsize = 8;
7657 }
7658
7a79a000
TS
7659 /* The generic elf_fake_sections will set up REL_HDR using the default
7660 kind of relocations. We used to set up a second header for the
7661 non-default kind of relocations here, but only NewABI would use
7662 these, and the IRIX ld doesn't like resulting empty RELA sections.
7663 Thus we create those header only on demand now. */
b49e97c9 7664
b34976b6 7665 return TRUE;
b49e97c9
TS
7666}
7667
7668/* Given a BFD section, try to locate the corresponding ELF section
7669 index. This is used by both the 32-bit and the 64-bit ABI.
7670 Actually, it's not clear to me that the 64-bit ABI supports these,
7671 but for non-PIC objects we will certainly want support for at least
7672 the .scommon section. */
7673
b34976b6 7674bfd_boolean
9719ad41
RS
7675_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7676 asection *sec, int *retval)
b49e97c9
TS
7677{
7678 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
7679 {
7680 *retval = SHN_MIPS_SCOMMON;
b34976b6 7681 return TRUE;
b49e97c9
TS
7682 }
7683 if (strcmp (bfd_get_section_name (abfd, sec), ".acommon") == 0)
7684 {
7685 *retval = SHN_MIPS_ACOMMON;
b34976b6 7686 return TRUE;
b49e97c9 7687 }
b34976b6 7688 return FALSE;
b49e97c9
TS
7689}
7690\f
7691/* Hook called by the linker routine which adds symbols from an object
7692 file. We must handle the special MIPS section numbers here. */
7693
b34976b6 7694bfd_boolean
9719ad41 7695_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 7696 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
7697 flagword *flagsp ATTRIBUTE_UNUSED,
7698 asection **secp, bfd_vma *valp)
b49e97c9
TS
7699{
7700 if (SGI_COMPAT (abfd)
7701 && (abfd->flags & DYNAMIC) != 0
7702 && strcmp (*namep, "_rld_new_interface") == 0)
7703 {
8dc1a139 7704 /* Skip IRIX5 rld entry name. */
b49e97c9 7705 *namep = NULL;
b34976b6 7706 return TRUE;
b49e97c9
TS
7707 }
7708
eedecc07
DD
7709 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7710 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7711 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7712 a magic symbol resolved by the linker, we ignore this bogus definition
7713 of _gp_disp. New ABI objects do not suffer from this problem so this
7714 is not done for them. */
7715 if (!NEWABI_P(abfd)
7716 && (sym->st_shndx == SHN_ABS)
7717 && (strcmp (*namep, "_gp_disp") == 0))
7718 {
7719 *namep = NULL;
7720 return TRUE;
7721 }
7722
b49e97c9
TS
7723 switch (sym->st_shndx)
7724 {
7725 case SHN_COMMON:
7726 /* Common symbols less than the GP size are automatically
7727 treated as SHN_MIPS_SCOMMON symbols. */
7728 if (sym->st_size > elf_gp_size (abfd)
b59eed79 7729 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
7730 || IRIX_COMPAT (abfd) == ict_irix6)
7731 break;
7732 /* Fall through. */
7733 case SHN_MIPS_SCOMMON:
7734 *secp = bfd_make_section_old_way (abfd, ".scommon");
7735 (*secp)->flags |= SEC_IS_COMMON;
7736 *valp = sym->st_size;
7737 break;
7738
7739 case SHN_MIPS_TEXT:
7740 /* This section is used in a shared object. */
698600e4 7741 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
b49e97c9
TS
7742 {
7743 asymbol *elf_text_symbol;
7744 asection *elf_text_section;
7745 bfd_size_type amt = sizeof (asection);
7746
7747 elf_text_section = bfd_zalloc (abfd, amt);
7748 if (elf_text_section == NULL)
b34976b6 7749 return FALSE;
b49e97c9
TS
7750
7751 amt = sizeof (asymbol);
7752 elf_text_symbol = bfd_zalloc (abfd, amt);
7753 if (elf_text_symbol == NULL)
b34976b6 7754 return FALSE;
b49e97c9
TS
7755
7756 /* Initialize the section. */
7757
698600e4
AM
7758 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7759 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
b49e97c9
TS
7760
7761 elf_text_section->symbol = elf_text_symbol;
698600e4 7762 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
b49e97c9
TS
7763
7764 elf_text_section->name = ".text";
7765 elf_text_section->flags = SEC_NO_FLAGS;
7766 elf_text_section->output_section = NULL;
7767 elf_text_section->owner = abfd;
7768 elf_text_symbol->name = ".text";
7769 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7770 elf_text_symbol->section = elf_text_section;
7771 }
7772 /* This code used to do *secp = bfd_und_section_ptr if
07d6d2b8
AM
7773 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7774 so I took it out. */
698600e4 7775 *secp = mips_elf_tdata (abfd)->elf_text_section;
b49e97c9
TS
7776 break;
7777
7778 case SHN_MIPS_ACOMMON:
7779 /* Fall through. XXX Can we treat this as allocated data? */
7780 case SHN_MIPS_DATA:
7781 /* This section is used in a shared object. */
698600e4 7782 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
b49e97c9
TS
7783 {
7784 asymbol *elf_data_symbol;
7785 asection *elf_data_section;
7786 bfd_size_type amt = sizeof (asection);
7787
7788 elf_data_section = bfd_zalloc (abfd, amt);
7789 if (elf_data_section == NULL)
b34976b6 7790 return FALSE;
b49e97c9
TS
7791
7792 amt = sizeof (asymbol);
7793 elf_data_symbol = bfd_zalloc (abfd, amt);
7794 if (elf_data_symbol == NULL)
b34976b6 7795 return FALSE;
b49e97c9
TS
7796
7797 /* Initialize the section. */
7798
698600e4
AM
7799 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7800 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
b49e97c9
TS
7801
7802 elf_data_section->symbol = elf_data_symbol;
698600e4 7803 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
b49e97c9
TS
7804
7805 elf_data_section->name = ".data";
7806 elf_data_section->flags = SEC_NO_FLAGS;
7807 elf_data_section->output_section = NULL;
7808 elf_data_section->owner = abfd;
7809 elf_data_symbol->name = ".data";
7810 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7811 elf_data_symbol->section = elf_data_section;
7812 }
7813 /* This code used to do *secp = bfd_und_section_ptr if
07d6d2b8
AM
7814 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7815 so I took it out. */
698600e4 7816 *secp = mips_elf_tdata (abfd)->elf_data_section;
b49e97c9
TS
7817 break;
7818
7819 case SHN_MIPS_SUNDEFINED:
7820 *secp = bfd_und_section_ptr;
7821 break;
7822 }
7823
7824 if (SGI_COMPAT (abfd)
0e1862bb 7825 && ! bfd_link_pic (info)
f13a99db 7826 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
7827 && strcmp (*namep, "__rld_obj_head") == 0)
7828 {
7829 struct elf_link_hash_entry *h;
14a793b2 7830 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7831
7832 /* Mark __rld_obj_head as dynamic. */
14a793b2 7833 bh = NULL;
b49e97c9 7834 if (! (_bfd_generic_link_add_one_symbol
9719ad41 7835 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 7836 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7837 return FALSE;
14a793b2
AM
7838
7839 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7840 h->non_elf = 0;
7841 h->def_regular = 1;
b49e97c9
TS
7842 h->type = STT_OBJECT;
7843
c152c796 7844 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7845 return FALSE;
b49e97c9 7846
b34976b6 7847 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b4082c70 7848 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
7849 }
7850
7851 /* If this is a mips16 text symbol, add 1 to the value to make it
7852 odd. This will cause something like .word SYM to come up with
7853 the right value when it is loaded into the PC. */
df58fc94 7854 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
7855 ++*valp;
7856
b34976b6 7857 return TRUE;
b49e97c9
TS
7858}
7859
7860/* This hook function is called before the linker writes out a global
7861 symbol. We mark symbols as small common if appropriate. This is
7862 also where we undo the increment of the value for a mips16 symbol. */
7863
6e0b88f1 7864int
9719ad41
RS
7865_bfd_mips_elf_link_output_symbol_hook
7866 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7867 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7868 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
7869{
7870 /* If we see a common symbol, which implies a relocatable link, then
7871 if a symbol was small common in an input file, mark it as small
7872 common in the output file. */
7873 if (sym->st_shndx == SHN_COMMON
7874 && strcmp (input_sec->name, ".scommon") == 0)
7875 sym->st_shndx = SHN_MIPS_SCOMMON;
7876
df58fc94 7877 if (ELF_ST_IS_COMPRESSED (sym->st_other))
79cda7cf 7878 sym->st_value &= ~1;
b49e97c9 7879
6e0b88f1 7880 return 1;
b49e97c9
TS
7881}
7882\f
7883/* Functions for the dynamic linker. */
7884
7885/* Create dynamic sections when linking against a dynamic object. */
7886
b34976b6 7887bfd_boolean
9719ad41 7888_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
7889{
7890 struct elf_link_hash_entry *h;
14a793b2 7891 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7892 flagword flags;
7893 register asection *s;
7894 const char * const *namep;
0a44bf69 7895 struct mips_elf_link_hash_table *htab;
b49e97c9 7896
0a44bf69 7897 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7898 BFD_ASSERT (htab != NULL);
7899
b49e97c9
TS
7900 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7901 | SEC_LINKER_CREATED | SEC_READONLY);
7902
0a44bf69
RS
7903 /* The psABI requires a read-only .dynamic section, but the VxWorks
7904 EABI doesn't. */
7905 if (!htab->is_vxworks)
b49e97c9 7906 {
3d4d4302 7907 s = bfd_get_linker_section (abfd, ".dynamic");
0a44bf69
RS
7908 if (s != NULL)
7909 {
7910 if (! bfd_set_section_flags (abfd, s, flags))
7911 return FALSE;
7912 }
b49e97c9
TS
7913 }
7914
7915 /* We need to create .got section. */
23cc69b6 7916 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
7917 return FALSE;
7918
0a44bf69 7919 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 7920 return FALSE;
b49e97c9 7921
b49e97c9 7922 /* Create .stub section. */
3d4d4302
AM
7923 s = bfd_make_section_anyway_with_flags (abfd,
7924 MIPS_ELF_STUB_SECTION_NAME (abfd),
7925 flags | SEC_CODE);
4e41d0d7
RS
7926 if (s == NULL
7927 || ! bfd_set_section_alignment (abfd, s,
7928 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
7929 return FALSE;
7930 htab->sstubs = s;
b49e97c9 7931
e6aea42d 7932 if (!mips_elf_hash_table (info)->use_rld_obj_head
0e1862bb 7933 && bfd_link_executable (info)
3d4d4302 7934 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
b49e97c9 7935 {
3d4d4302
AM
7936 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
7937 flags &~ (flagword) SEC_READONLY);
b49e97c9 7938 if (s == NULL
b49e97c9
TS
7939 || ! bfd_set_section_alignment (abfd, s,
7940 MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 7941 return FALSE;
b49e97c9
TS
7942 }
7943
7944 /* On IRIX5, we adjust add some additional symbols and change the
7945 alignments of several sections. There is no ABI documentation
7946 indicating that this is necessary on IRIX6, nor any evidence that
7947 the linker takes such action. */
7948 if (IRIX_COMPAT (abfd) == ict_irix5)
7949 {
7950 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
7951 {
14a793b2 7952 bh = NULL;
b49e97c9 7953 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
7954 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
7955 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7956 return FALSE;
14a793b2
AM
7957
7958 h = (struct elf_link_hash_entry *) bh;
12f09816 7959 h->mark = 1;
f5385ebf
AM
7960 h->non_elf = 0;
7961 h->def_regular = 1;
b49e97c9
TS
7962 h->type = STT_SECTION;
7963
c152c796 7964 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7965 return FALSE;
b49e97c9
TS
7966 }
7967
7968 /* We need to create a .compact_rel section. */
7969 if (SGI_COMPAT (abfd))
7970 {
7971 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 7972 return FALSE;
b49e97c9
TS
7973 }
7974
44c410de 7975 /* Change alignments of some sections. */
3d4d4302 7976 s = bfd_get_linker_section (abfd, ".hash");
b49e97c9 7977 if (s != NULL)
a253d456
NC
7978 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7979
3d4d4302 7980 s = bfd_get_linker_section (abfd, ".dynsym");
b49e97c9 7981 if (s != NULL)
a253d456
NC
7982 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7983
3d4d4302 7984 s = bfd_get_linker_section (abfd, ".dynstr");
b49e97c9 7985 if (s != NULL)
a253d456
NC
7986 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7987
3d4d4302 7988 /* ??? */
b49e97c9
TS
7989 s = bfd_get_section_by_name (abfd, ".reginfo");
7990 if (s != NULL)
a253d456
NC
7991 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
7992
3d4d4302 7993 s = bfd_get_linker_section (abfd, ".dynamic");
b49e97c9 7994 if (s != NULL)
a253d456 7995 (void) bfd_set_section_alignment (abfd, s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
7996 }
7997
0e1862bb 7998 if (bfd_link_executable (info))
b49e97c9 7999 {
14a793b2
AM
8000 const char *name;
8001
8002 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8003 bh = NULL;
8004 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
8005 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8006 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 8007 return FALSE;
14a793b2
AM
8008
8009 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
8010 h->non_elf = 0;
8011 h->def_regular = 1;
b49e97c9
TS
8012 h->type = STT_SECTION;
8013
c152c796 8014 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 8015 return FALSE;
b49e97c9
TS
8016
8017 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8018 {
8019 /* __rld_map is a four byte word located in the .data section
8020 and is filled in by the rtld to contain a pointer to
8021 the _r_debug structure. Its symbol value will be set in
8022 _bfd_mips_elf_finish_dynamic_symbol. */
3d4d4302 8023 s = bfd_get_linker_section (abfd, ".rld_map");
0abfb97a 8024 BFD_ASSERT (s != NULL);
14a793b2 8025
0abfb97a
L
8026 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8027 bh = NULL;
8028 if (!(_bfd_generic_link_add_one_symbol
8029 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
8030 get_elf_backend_data (abfd)->collect, &bh)))
8031 return FALSE;
b49e97c9 8032
0abfb97a
L
8033 h = (struct elf_link_hash_entry *) bh;
8034 h->non_elf = 0;
8035 h->def_regular = 1;
8036 h->type = STT_OBJECT;
8037
8038 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8039 return FALSE;
b4082c70 8040 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
8041 }
8042 }
8043
861fb55a 8044 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
c164a95d 8045 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
861fb55a
DJ
8046 if (!_bfd_elf_create_dynamic_sections (abfd, info))
8047 return FALSE;
8048
1bbce132
MR
8049 /* Do the usual VxWorks handling. */
8050 if (htab->is_vxworks
8051 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8052 return FALSE;
0a44bf69 8053
b34976b6 8054 return TRUE;
b49e97c9
TS
8055}
8056\f
c224138d
RS
8057/* Return true if relocation REL against section SEC is a REL rather than
8058 RELA relocation. RELOCS is the first relocation in the section and
8059 ABFD is the bfd that contains SEC. */
8060
8061static bfd_boolean
8062mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8063 const Elf_Internal_Rela *relocs,
8064 const Elf_Internal_Rela *rel)
8065{
8066 Elf_Internal_Shdr *rel_hdr;
8067 const struct elf_backend_data *bed;
8068
d4730f92
BS
8069 /* To determine which flavor of relocation this is, we depend on the
8070 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8071 rel_hdr = elf_section_data (sec)->rel.hdr;
8072 if (rel_hdr == NULL)
8073 return FALSE;
c224138d 8074 bed = get_elf_backend_data (abfd);
d4730f92
BS
8075 return ((size_t) (rel - relocs)
8076 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
c224138d
RS
8077}
8078
8079/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8080 HOWTO is the relocation's howto and CONTENTS points to the contents
8081 of the section that REL is against. */
8082
8083static bfd_vma
8084mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
8085 reloc_howto_type *howto, bfd_byte *contents)
8086{
8087 bfd_byte *location;
8088 unsigned int r_type;
8089 bfd_vma addend;
17c6c9d9 8090 bfd_vma bytes;
c224138d
RS
8091
8092 r_type = ELF_R_TYPE (abfd, rel->r_info);
8093 location = contents + rel->r_offset;
8094
8095 /* Get the addend, which is stored in the input file. */
df58fc94 8096 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
17c6c9d9 8097 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
df58fc94 8098 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
c224138d 8099
17c6c9d9
MR
8100 addend = bytes & howto->src_mask;
8101
8102 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8103 accordingly. */
8104 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8105 addend <<= 1;
8106
8107 return addend;
c224138d
RS
8108}
8109
8110/* REL is a relocation in ABFD that needs a partnering LO16 relocation
8111 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8112 and update *ADDEND with the final addend. Return true on success
8113 or false if the LO16 could not be found. RELEND is the exclusive
8114 upper bound on the relocations for REL's section. */
8115
8116static bfd_boolean
8117mips_elf_add_lo16_rel_addend (bfd *abfd,
8118 const Elf_Internal_Rela *rel,
8119 const Elf_Internal_Rela *relend,
8120 bfd_byte *contents, bfd_vma *addend)
8121{
8122 unsigned int r_type, lo16_type;
8123 const Elf_Internal_Rela *lo16_relocation;
8124 reloc_howto_type *lo16_howto;
8125 bfd_vma l;
8126
8127 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 8128 if (mips16_reloc_p (r_type))
c224138d 8129 lo16_type = R_MIPS16_LO16;
df58fc94
RS
8130 else if (micromips_reloc_p (r_type))
8131 lo16_type = R_MICROMIPS_LO16;
7361da2c
AB
8132 else if (r_type == R_MIPS_PCHI16)
8133 lo16_type = R_MIPS_PCLO16;
c224138d
RS
8134 else
8135 lo16_type = R_MIPS_LO16;
8136
8137 /* The combined value is the sum of the HI16 addend, left-shifted by
8138 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8139 code does a `lui' of the HI16 value, and then an `addiu' of the
8140 LO16 value.)
8141
8142 Scan ahead to find a matching LO16 relocation.
8143
8144 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8145 be immediately following. However, for the IRIX6 ABI, the next
8146 relocation may be a composed relocation consisting of several
8147 relocations for the same address. In that case, the R_MIPS_LO16
8148 relocation may occur as one of these. We permit a similar
8149 extension in general, as that is useful for GCC.
8150
8151 In some cases GCC dead code elimination removes the LO16 but keeps
8152 the corresponding HI16. This is strictly speaking a violation of
8153 the ABI but not immediately harmful. */
8154 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8155 if (lo16_relocation == NULL)
8156 return FALSE;
8157
8158 /* Obtain the addend kept there. */
8159 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
8160 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
8161
8162 l <<= lo16_howto->rightshift;
8163 l = _bfd_mips_elf_sign_extend (l, 16);
8164
8165 *addend <<= 16;
8166 *addend += l;
8167 return TRUE;
8168}
8169
8170/* Try to read the contents of section SEC in bfd ABFD. Return true and
8171 store the contents in *CONTENTS on success. Assume that *CONTENTS
8172 already holds the contents if it is nonull on entry. */
8173
8174static bfd_boolean
8175mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8176{
8177 if (*contents)
8178 return TRUE;
8179
8180 /* Get cached copy if it exists. */
8181 if (elf_section_data (sec)->this_hdr.contents != NULL)
8182 {
8183 *contents = elf_section_data (sec)->this_hdr.contents;
8184 return TRUE;
8185 }
8186
8187 return bfd_malloc_and_get_section (abfd, sec, contents);
8188}
8189
1bbce132
MR
8190/* Make a new PLT record to keep internal data. */
8191
8192static struct plt_entry *
8193mips_elf_make_plt_record (bfd *abfd)
8194{
8195 struct plt_entry *entry;
8196
8197 entry = bfd_zalloc (abfd, sizeof (*entry));
8198 if (entry == NULL)
8199 return NULL;
8200
8201 entry->stub_offset = MINUS_ONE;
8202 entry->mips_offset = MINUS_ONE;
8203 entry->comp_offset = MINUS_ONE;
8204 entry->gotplt_index = MINUS_ONE;
8205 return entry;
8206}
8207
47275900
MR
8208/* Define the special `__gnu_absolute_zero' symbol. We only need this
8209 for PIC code, as otherwise there is no load-time relocation involved
8210 and local GOT entries whose value is zero at static link time will
8211 retain their value at load time. */
8212
8213static bfd_boolean
8214mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8215 struct mips_elf_link_hash_table *htab,
8216 unsigned int r_type)
8217{
8218 union
8219 {
8220 struct elf_link_hash_entry *eh;
8221 struct bfd_link_hash_entry *bh;
8222 }
8223 hzero;
8224
8225 BFD_ASSERT (!htab->use_absolute_zero);
8226 BFD_ASSERT (bfd_link_pic (info));
8227
8228 hzero.bh = NULL;
8229 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8230 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8231 NULL, FALSE, FALSE, &hzero.bh))
8232 return FALSE;
8233
8234 BFD_ASSERT (hzero.bh != NULL);
8235 hzero.eh->size = 0;
8236 hzero.eh->type = STT_NOTYPE;
8237 hzero.eh->other = STV_PROTECTED;
8238 hzero.eh->def_regular = 1;
8239 hzero.eh->non_elf = 0;
8240
8241 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, TRUE, r_type))
8242 return FALSE;
8243
8244 htab->use_absolute_zero = TRUE;
8245
8246 return TRUE;
8247}
8248
b49e97c9 8249/* Look through the relocs for a section during the first phase, and
1bbce132
MR
8250 allocate space in the global offset table and record the need for
8251 standard MIPS and compressed procedure linkage table entries. */
b49e97c9 8252
b34976b6 8253bfd_boolean
9719ad41
RS
8254_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8255 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
8256{
8257 const char *name;
8258 bfd *dynobj;
8259 Elf_Internal_Shdr *symtab_hdr;
8260 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
8261 size_t extsymoff;
8262 const Elf_Internal_Rela *rel;
8263 const Elf_Internal_Rela *rel_end;
b49e97c9 8264 asection *sreloc;
9c5bfbb7 8265 const struct elf_backend_data *bed;
0a44bf69 8266 struct mips_elf_link_hash_table *htab;
c224138d
RS
8267 bfd_byte *contents;
8268 bfd_vma addend;
8269 reloc_howto_type *howto;
b49e97c9 8270
0e1862bb 8271 if (bfd_link_relocatable (info))
b34976b6 8272 return TRUE;
b49e97c9 8273
0a44bf69 8274 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8275 BFD_ASSERT (htab != NULL);
8276
b49e97c9
TS
8277 dynobj = elf_hash_table (info)->dynobj;
8278 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8279 sym_hashes = elf_sym_hashes (abfd);
8280 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8281
738e5348 8282 bed = get_elf_backend_data (abfd);
056bafd4 8283 rel_end = relocs + sec->reloc_count;
738e5348 8284
b49e97c9
TS
8285 /* Check for the mips16 stub sections. */
8286
8287 name = bfd_get_section_name (abfd, sec);
b9d58d71 8288 if (FN_STUB_P (name))
b49e97c9
TS
8289 {
8290 unsigned long r_symndx;
8291
8292 /* Look at the relocation information to figure out which symbol
07d6d2b8 8293 this is for. */
b49e97c9 8294
cb4437b8 8295 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
8296 if (r_symndx == 0)
8297 {
4eca0228 8298 _bfd_error_handler
695344c0 8299 /* xgettext:c-format */
2c1c9679 8300 (_("%pB: warning: cannot determine the target function for"
738e5348
RS
8301 " stub section `%s'"),
8302 abfd, name);
8303 bfd_set_error (bfd_error_bad_value);
8304 return FALSE;
8305 }
b49e97c9
TS
8306
8307 if (r_symndx < extsymoff
8308 || sym_hashes[r_symndx - extsymoff] == NULL)
8309 {
8310 asection *o;
8311
8312 /* This stub is for a local symbol. This stub will only be
07d6d2b8
AM
8313 needed if there is some relocation in this BFD, other
8314 than a 16 bit function call, which refers to this symbol. */
b49e97c9
TS
8315 for (o = abfd->sections; o != NULL; o = o->next)
8316 {
8317 Elf_Internal_Rela *sec_relocs;
8318 const Elf_Internal_Rela *r, *rend;
8319
8320 /* We can ignore stub sections when looking for relocs. */
8321 if ((o->flags & SEC_RELOC) == 0
8322 || o->reloc_count == 0
738e5348 8323 || section_allows_mips16_refs_p (o))
b49e97c9
TS
8324 continue;
8325
45d6a902 8326 sec_relocs
9719ad41 8327 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 8328 info->keep_memory);
b49e97c9 8329 if (sec_relocs == NULL)
b34976b6 8330 return FALSE;
b49e97c9
TS
8331
8332 rend = sec_relocs + o->reloc_count;
8333 for (r = sec_relocs; r < rend; r++)
8334 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 8335 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
8336 break;
8337
6cdc0ccc 8338 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
8339 free (sec_relocs);
8340
8341 if (r < rend)
8342 break;
8343 }
8344
8345 if (o == NULL)
8346 {
8347 /* There is no non-call reloc for this stub, so we do
07d6d2b8
AM
8348 not need it. Since this function is called before
8349 the linker maps input sections to output sections, we
8350 can easily discard it by setting the SEC_EXCLUDE
8351 flag. */
b49e97c9 8352 sec->flags |= SEC_EXCLUDE;
b34976b6 8353 return TRUE;
b49e97c9
TS
8354 }
8355
8356 /* Record this stub in an array of local symbol stubs for
07d6d2b8 8357 this BFD. */
698600e4 8358 if (mips_elf_tdata (abfd)->local_stubs == NULL)
b49e97c9
TS
8359 {
8360 unsigned long symcount;
8361 asection **n;
8362 bfd_size_type amt;
8363
8364 if (elf_bad_symtab (abfd))
8365 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8366 else
8367 symcount = symtab_hdr->sh_info;
8368 amt = symcount * sizeof (asection *);
9719ad41 8369 n = bfd_zalloc (abfd, amt);
b49e97c9 8370 if (n == NULL)
b34976b6 8371 return FALSE;
698600e4 8372 mips_elf_tdata (abfd)->local_stubs = n;
b49e97c9
TS
8373 }
8374
b9d58d71 8375 sec->flags |= SEC_KEEP;
698600e4 8376 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
b49e97c9
TS
8377
8378 /* We don't need to set mips16_stubs_seen in this case.
07d6d2b8
AM
8379 That flag is used to see whether we need to look through
8380 the global symbol table for stubs. We don't need to set
8381 it here, because we just have a local stub. */
b49e97c9
TS
8382 }
8383 else
8384 {
8385 struct mips_elf_link_hash_entry *h;
8386
8387 h = ((struct mips_elf_link_hash_entry *)
8388 sym_hashes[r_symndx - extsymoff]);
8389
973a3492
L
8390 while (h->root.root.type == bfd_link_hash_indirect
8391 || h->root.root.type == bfd_link_hash_warning)
8392 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8393
b49e97c9
TS
8394 /* H is the symbol this stub is for. */
8395
b9d58d71
TS
8396 /* If we already have an appropriate stub for this function, we
8397 don't need another one, so we can discard this one. Since
8398 this function is called before the linker maps input sections
8399 to output sections, we can easily discard it by setting the
8400 SEC_EXCLUDE flag. */
8401 if (h->fn_stub != NULL)
8402 {
8403 sec->flags |= SEC_EXCLUDE;
8404 return TRUE;
8405 }
8406
8407 sec->flags |= SEC_KEEP;
b49e97c9 8408 h->fn_stub = sec;
b34976b6 8409 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
8410 }
8411 }
b9d58d71 8412 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
8413 {
8414 unsigned long r_symndx;
8415 struct mips_elf_link_hash_entry *h;
8416 asection **loc;
8417
8418 /* Look at the relocation information to figure out which symbol
07d6d2b8 8419 this is for. */
b49e97c9 8420
cb4437b8 8421 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
8422 if (r_symndx == 0)
8423 {
4eca0228 8424 _bfd_error_handler
695344c0 8425 /* xgettext:c-format */
2c1c9679 8426 (_("%pB: warning: cannot determine the target function for"
738e5348
RS
8427 " stub section `%s'"),
8428 abfd, name);
8429 bfd_set_error (bfd_error_bad_value);
8430 return FALSE;
8431 }
b49e97c9
TS
8432
8433 if (r_symndx < extsymoff
8434 || sym_hashes[r_symndx - extsymoff] == NULL)
8435 {
b9d58d71 8436 asection *o;
b49e97c9 8437
b9d58d71 8438 /* This stub is for a local symbol. This stub will only be
07d6d2b8
AM
8439 needed if there is some relocation (R_MIPS16_26) in this BFD
8440 that refers to this symbol. */
b9d58d71
TS
8441 for (o = abfd->sections; o != NULL; o = o->next)
8442 {
8443 Elf_Internal_Rela *sec_relocs;
8444 const Elf_Internal_Rela *r, *rend;
8445
8446 /* We can ignore stub sections when looking for relocs. */
8447 if ((o->flags & SEC_RELOC) == 0
8448 || o->reloc_count == 0
738e5348 8449 || section_allows_mips16_refs_p (o))
b9d58d71
TS
8450 continue;
8451
8452 sec_relocs
8453 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8454 info->keep_memory);
8455 if (sec_relocs == NULL)
8456 return FALSE;
8457
8458 rend = sec_relocs + o->reloc_count;
8459 for (r = sec_relocs; r < rend; r++)
8460 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8461 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8462 break;
8463
8464 if (elf_section_data (o)->relocs != sec_relocs)
8465 free (sec_relocs);
8466
8467 if (r < rend)
8468 break;
8469 }
8470
8471 if (o == NULL)
8472 {
8473 /* There is no non-call reloc for this stub, so we do
07d6d2b8
AM
8474 not need it. Since this function is called before
8475 the linker maps input sections to output sections, we
8476 can easily discard it by setting the SEC_EXCLUDE
8477 flag. */
b9d58d71
TS
8478 sec->flags |= SEC_EXCLUDE;
8479 return TRUE;
8480 }
8481
8482 /* Record this stub in an array of local symbol call_stubs for
07d6d2b8 8483 this BFD. */
698600e4 8484 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
b9d58d71
TS
8485 {
8486 unsigned long symcount;
8487 asection **n;
8488 bfd_size_type amt;
8489
8490 if (elf_bad_symtab (abfd))
8491 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8492 else
8493 symcount = symtab_hdr->sh_info;
8494 amt = symcount * sizeof (asection *);
8495 n = bfd_zalloc (abfd, amt);
8496 if (n == NULL)
8497 return FALSE;
698600e4 8498 mips_elf_tdata (abfd)->local_call_stubs = n;
b9d58d71 8499 }
b49e97c9 8500
b9d58d71 8501 sec->flags |= SEC_KEEP;
698600e4 8502 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 8503
b9d58d71 8504 /* We don't need to set mips16_stubs_seen in this case.
07d6d2b8
AM
8505 That flag is used to see whether we need to look through
8506 the global symbol table for stubs. We don't need to set
8507 it here, because we just have a local stub. */
b9d58d71 8508 }
b49e97c9 8509 else
b49e97c9 8510 {
b9d58d71
TS
8511 h = ((struct mips_elf_link_hash_entry *)
8512 sym_hashes[r_symndx - extsymoff]);
68ffbac6 8513
b9d58d71 8514 /* H is the symbol this stub is for. */
68ffbac6 8515
b9d58d71
TS
8516 if (CALL_FP_STUB_P (name))
8517 loc = &h->call_fp_stub;
8518 else
8519 loc = &h->call_stub;
68ffbac6 8520
b9d58d71
TS
8521 /* If we already have an appropriate stub for this function, we
8522 don't need another one, so we can discard this one. Since
8523 this function is called before the linker maps input sections
8524 to output sections, we can easily discard it by setting the
8525 SEC_EXCLUDE flag. */
8526 if (*loc != NULL)
8527 {
8528 sec->flags |= SEC_EXCLUDE;
8529 return TRUE;
8530 }
b49e97c9 8531
b9d58d71
TS
8532 sec->flags |= SEC_KEEP;
8533 *loc = sec;
8534 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
8535 }
b49e97c9
TS
8536 }
8537
b49e97c9 8538 sreloc = NULL;
c224138d 8539 contents = NULL;
b49e97c9
TS
8540 for (rel = relocs; rel < rel_end; ++rel)
8541 {
8542 unsigned long r_symndx;
8543 unsigned int r_type;
8544 struct elf_link_hash_entry *h;
861fb55a 8545 bfd_boolean can_make_dynamic_p;
c5d6fa44
RS
8546 bfd_boolean call_reloc_p;
8547 bfd_boolean constrain_symbol_p;
b49e97c9
TS
8548
8549 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8550 r_type = ELF_R_TYPE (abfd, rel->r_info);
8551
8552 if (r_symndx < extsymoff)
8553 h = NULL;
8554 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8555 {
4eca0228 8556 _bfd_error_handler
695344c0 8557 /* xgettext:c-format */
2c1c9679 8558 (_("%pB: malformed reloc detected for section %s"),
d003868e 8559 abfd, name);
b49e97c9 8560 bfd_set_error (bfd_error_bad_value);
b34976b6 8561 return FALSE;
b49e97c9
TS
8562 }
8563 else
8564 {
8565 h = sym_hashes[r_symndx - extsymoff];
81fbe831
AM
8566 if (h != NULL)
8567 {
8568 while (h->root.type == bfd_link_hash_indirect
8569 || h->root.type == bfd_link_hash_warning)
8570 h = (struct elf_link_hash_entry *) h->root.u.i.link;
81fbe831 8571 }
861fb55a 8572 }
b49e97c9 8573
861fb55a
DJ
8574 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8575 relocation into a dynamic one. */
8576 can_make_dynamic_p = FALSE;
c5d6fa44
RS
8577
8578 /* Set CALL_RELOC_P to true if the relocation is for a call,
8579 and if pointer equality therefore doesn't matter. */
8580 call_reloc_p = FALSE;
8581
8582 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
8583 into account when deciding how to define the symbol.
8584 Relocations in nonallocatable sections such as .pdr and
8585 .debug* should have no effect. */
8586 constrain_symbol_p = ((sec->flags & SEC_ALLOC) != 0);
8587
861fb55a
DJ
8588 switch (r_type)
8589 {
861fb55a
DJ
8590 case R_MIPS_CALL16:
8591 case R_MIPS_CALL_HI16:
8592 case R_MIPS_CALL_LO16:
c5d6fa44
RS
8593 case R_MIPS16_CALL16:
8594 case R_MICROMIPS_CALL16:
8595 case R_MICROMIPS_CALL_HI16:
8596 case R_MICROMIPS_CALL_LO16:
8597 call_reloc_p = TRUE;
8598 /* Fall through. */
8599
8600 case R_MIPS_GOT16:
861fb55a
DJ
8601 case R_MIPS_GOT_LO16:
8602 case R_MIPS_GOT_PAGE:
861fb55a 8603 case R_MIPS_GOT_DISP:
47275900
MR
8604 case R_MIPS16_GOT16:
8605 case R_MICROMIPS_GOT16:
8606 case R_MICROMIPS_GOT_LO16:
8607 case R_MICROMIPS_GOT_PAGE:
8608 case R_MICROMIPS_GOT_DISP:
8609 /* If we have a symbol that will resolve to zero at static link
8610 time and it is used by a GOT relocation applied to code we
8611 cannot relax to an immediate zero load, then we will be using
8612 the special `__gnu_absolute_zero' symbol whose value is zero
8613 at dynamic load time. We ignore HI16-type GOT relocations at
8614 this stage, because their handling will depend entirely on
8615 the corresponding LO16-type GOT relocation. */
8616 if (!call_hi16_reloc_p (r_type)
8617 && h != NULL
8618 && bfd_link_pic (info)
8619 && !htab->use_absolute_zero
8620 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8621 {
8622 bfd_boolean rel_reloc;
8623
8624 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8625 return FALSE;
8626
8627 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8628 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8629
8630 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8631 FALSE))
8632 if (!mips_elf_define_absolute_zero (abfd, info, htab, r_type))
8633 return FALSE;
8634 }
8635
8636 /* Fall through. */
8637 case R_MIPS_GOT_HI16:
8638 case R_MIPS_GOT_OFST:
861fb55a
DJ
8639 case R_MIPS_TLS_GOTTPREL:
8640 case R_MIPS_TLS_GD:
8641 case R_MIPS_TLS_LDM:
d0f13682
CLT
8642 case R_MIPS16_TLS_GOTTPREL:
8643 case R_MIPS16_TLS_GD:
8644 case R_MIPS16_TLS_LDM:
df58fc94 8645 case R_MICROMIPS_GOT_HI16:
df58fc94 8646 case R_MICROMIPS_GOT_OFST:
df58fc94
RS
8647 case R_MICROMIPS_TLS_GOTTPREL:
8648 case R_MICROMIPS_TLS_GD:
8649 case R_MICROMIPS_TLS_LDM:
861fb55a
DJ
8650 if (dynobj == NULL)
8651 elf_hash_table (info)->dynobj = dynobj = abfd;
8652 if (!mips_elf_create_got_section (dynobj, info))
8653 return FALSE;
0e1862bb 8654 if (htab->is_vxworks && !bfd_link_pic (info))
b49e97c9 8655 {
4eca0228 8656 _bfd_error_handler
695344c0 8657 /* xgettext:c-format */
2dcf00ce
AM
8658 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8659 abfd, (uint64_t) rel->r_offset);
861fb55a
DJ
8660 bfd_set_error (bfd_error_bad_value);
8661 return FALSE;
b49e97c9 8662 }
c5d6fa44 8663 can_make_dynamic_p = TRUE;
861fb55a 8664 break;
b49e97c9 8665
c5d6fa44 8666 case R_MIPS_NONE:
99da6b5f 8667 case R_MIPS_JALR:
df58fc94 8668 case R_MICROMIPS_JALR:
c5d6fa44
RS
8669 /* These relocations have empty fields and are purely there to
8670 provide link information. The symbol value doesn't matter. */
8671 constrain_symbol_p = FALSE;
8672 break;
8673
8674 case R_MIPS_GPREL16:
8675 case R_MIPS_GPREL32:
8676 case R_MIPS16_GPREL:
8677 case R_MICROMIPS_GPREL16:
8678 /* GP-relative relocations always resolve to a definition in a
8679 regular input file, ignoring the one-definition rule. This is
8680 important for the GP setup sequence in NewABI code, which
8681 always resolves to a local function even if other relocations
8682 against the symbol wouldn't. */
8683 constrain_symbol_p = FALSE;
99da6b5f
AN
8684 break;
8685
861fb55a
DJ
8686 case R_MIPS_32:
8687 case R_MIPS_REL32:
8688 case R_MIPS_64:
8689 /* In VxWorks executables, references to external symbols
8690 must be handled using copy relocs or PLT entries; it is not
8691 possible to convert this relocation into a dynamic one.
8692
8693 For executables that use PLTs and copy-relocs, we have a
8694 choice between converting the relocation into a dynamic
8695 one or using copy relocations or PLT entries. It is
8696 usually better to do the former, unless the relocation is
8697 against a read-only section. */
0e1862bb 8698 if ((bfd_link_pic (info)
861fb55a
DJ
8699 || (h != NULL
8700 && !htab->is_vxworks
8701 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8702 && !(!info->nocopyreloc
8703 && !PIC_OBJECT_P (abfd)
8704 && MIPS_ELF_READONLY_SECTION (sec))))
8705 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 8706 {
861fb55a 8707 can_make_dynamic_p = TRUE;
b49e97c9
TS
8708 if (dynobj == NULL)
8709 elf_hash_table (info)->dynobj = dynobj = abfd;
861fb55a 8710 }
c5d6fa44 8711 break;
b49e97c9 8712
861fb55a
DJ
8713 case R_MIPS_26:
8714 case R_MIPS_PC16:
7361da2c
AB
8715 case R_MIPS_PC21_S2:
8716 case R_MIPS_PC26_S2:
861fb55a 8717 case R_MIPS16_26:
c9775dde 8718 case R_MIPS16_PC16_S1:
df58fc94
RS
8719 case R_MICROMIPS_26_S1:
8720 case R_MICROMIPS_PC7_S1:
8721 case R_MICROMIPS_PC10_S1:
8722 case R_MICROMIPS_PC16_S1:
8723 case R_MICROMIPS_PC23_S2:
c5d6fa44 8724 call_reloc_p = TRUE;
861fb55a 8725 break;
b49e97c9
TS
8726 }
8727
0a44bf69
RS
8728 if (h)
8729 {
c5d6fa44
RS
8730 if (constrain_symbol_p)
8731 {
8732 if (!can_make_dynamic_p)
8733 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8734
8735 if (!call_reloc_p)
8736 h->pointer_equality_needed = 1;
8737
8738 /* We must not create a stub for a symbol that has
8739 relocations related to taking the function's address.
8740 This doesn't apply to VxWorks, where CALL relocs refer
8741 to a .got.plt entry instead of a normal .got entry. */
8742 if (!htab->is_vxworks && (!can_make_dynamic_p || !call_reloc_p))
8743 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8744 }
8745
0a44bf69
RS
8746 /* Relocations against the special VxWorks __GOTT_BASE__ and
8747 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8748 room for them in .rela.dyn. */
8749 if (is_gott_symbol (info, h))
8750 {
8751 if (sreloc == NULL)
8752 {
8753 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8754 if (sreloc == NULL)
8755 return FALSE;
8756 }
8757 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
8758 if (MIPS_ELF_READONLY_SECTION (sec))
8759 /* We tell the dynamic linker that there are
8760 relocations against the text segment. */
8761 info->flags |= DF_TEXTREL;
0a44bf69
RS
8762 }
8763 }
df58fc94
RS
8764 else if (call_lo16_reloc_p (r_type)
8765 || got_lo16_reloc_p (r_type)
8766 || got_disp_reloc_p (r_type)
738e5348 8767 || (got16_reloc_p (r_type) && htab->is_vxworks))
b49e97c9
TS
8768 {
8769 /* We may need a local GOT entry for this relocation. We
8770 don't count R_MIPS_GOT_PAGE because we can estimate the
8771 maximum number of pages needed by looking at the size of
738e5348
RS
8772 the segment. Similar comments apply to R_MIPS*_GOT16 and
8773 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 8774 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 8775 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 8776 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0 8777 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
e641e783 8778 rel->r_addend, info, r_type))
f4416af6 8779 return FALSE;
b49e97c9
TS
8780 }
8781
8f0c309a
CLT
8782 if (h != NULL
8783 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8784 ELF_ST_IS_MIPS16 (h->other)))
861fb55a
DJ
8785 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8786
b49e97c9
TS
8787 switch (r_type)
8788 {
8789 case R_MIPS_CALL16:
738e5348 8790 case R_MIPS16_CALL16:
df58fc94 8791 case R_MICROMIPS_CALL16:
b49e97c9
TS
8792 if (h == NULL)
8793 {
4eca0228 8794 _bfd_error_handler
695344c0 8795 /* xgettext:c-format */
2dcf00ce
AM
8796 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8797 abfd, (uint64_t) rel->r_offset);
b49e97c9 8798 bfd_set_error (bfd_error_bad_value);
b34976b6 8799 return FALSE;
b49e97c9
TS
8800 }
8801 /* Fall through. */
8802
8803 case R_MIPS_CALL_HI16:
8804 case R_MIPS_CALL_LO16:
df58fc94
RS
8805 case R_MICROMIPS_CALL_HI16:
8806 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
8807 if (h != NULL)
8808 {
6ccf4795
RS
8809 /* Make sure there is room in the regular GOT to hold the
8810 function's address. We may eliminate it in favour of
8811 a .got.plt entry later; see mips_elf_count_got_symbols. */
e641e783
RS
8812 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8813 r_type))
b34976b6 8814 return FALSE;
b49e97c9
TS
8815
8816 /* We need a stub, not a plt entry for the undefined
8817 function. But we record it as if it needs plt. See
c152c796 8818 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 8819 h->needs_plt = 1;
b49e97c9
TS
8820 h->type = STT_FUNC;
8821 }
8822 break;
8823
0fdc1bf1 8824 case R_MIPS_GOT_PAGE:
df58fc94 8825 case R_MICROMIPS_GOT_PAGE:
738e5348 8826 case R_MIPS16_GOT16:
b49e97c9
TS
8827 case R_MIPS_GOT16:
8828 case R_MIPS_GOT_HI16:
8829 case R_MIPS_GOT_LO16:
df58fc94
RS
8830 case R_MICROMIPS_GOT16:
8831 case R_MICROMIPS_GOT_HI16:
8832 case R_MICROMIPS_GOT_LO16:
8833 if (!h || got_page_reloc_p (r_type))
c224138d 8834 {
3a3b6725
DJ
8835 /* This relocation needs (or may need, if h != NULL) a
8836 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8837 know for sure until we know whether the symbol is
8838 preemptible. */
c224138d
RS
8839 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8840 {
8841 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8842 return FALSE;
8843 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8844 addend = mips_elf_read_rel_addend (abfd, rel,
8845 howto, contents);
9684f078 8846 if (got16_reloc_p (r_type))
c224138d
RS
8847 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8848 contents, &addend);
8849 else
8850 addend <<= howto->rightshift;
8851 }
8852 else
8853 addend = rel->r_addend;
13db6b44
RS
8854 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8855 h, addend))
c224138d 8856 return FALSE;
13db6b44
RS
8857
8858 if (h)
8859 {
8860 struct mips_elf_link_hash_entry *hmips =
8861 (struct mips_elf_link_hash_entry *) h;
8862
8863 /* This symbol is definitely not overridable. */
8864 if (hmips->root.def_regular
0e1862bb 8865 && ! (bfd_link_pic (info) && ! info->symbolic
13db6b44
RS
8866 && ! hmips->root.forced_local))
8867 h = NULL;
8868 }
c224138d 8869 }
13db6b44
RS
8870 /* If this is a global, overridable symbol, GOT_PAGE will
8871 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d
RS
8872 /* Fall through. */
8873
b49e97c9 8874 case R_MIPS_GOT_DISP:
df58fc94 8875 case R_MICROMIPS_GOT_DISP:
6ccf4795 8876 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
e641e783 8877 FALSE, r_type))
b34976b6 8878 return FALSE;
b49e97c9
TS
8879 break;
8880
0f20cc35 8881 case R_MIPS_TLS_GOTTPREL:
d0f13682 8882 case R_MIPS16_TLS_GOTTPREL:
df58fc94 8883 case R_MICROMIPS_TLS_GOTTPREL:
0e1862bb 8884 if (bfd_link_pic (info))
0f20cc35
DJ
8885 info->flags |= DF_STATIC_TLS;
8886 /* Fall through */
8887
8888 case R_MIPS_TLS_LDM:
d0f13682 8889 case R_MIPS16_TLS_LDM:
df58fc94
RS
8890 case R_MICROMIPS_TLS_LDM:
8891 if (tls_ldm_reloc_p (r_type))
0f20cc35 8892 {
cf35638d 8893 r_symndx = STN_UNDEF;
0f20cc35
DJ
8894 h = NULL;
8895 }
8896 /* Fall through */
8897
8898 case R_MIPS_TLS_GD:
d0f13682 8899 case R_MIPS16_TLS_GD:
df58fc94 8900 case R_MICROMIPS_TLS_GD:
0f20cc35
DJ
8901 /* This symbol requires a global offset table entry, or two
8902 for TLS GD relocations. */
e641e783
RS
8903 if (h != NULL)
8904 {
8905 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8906 FALSE, r_type))
8907 return FALSE;
8908 }
8909 else
8910 {
8911 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8912 rel->r_addend,
8913 info, r_type))
8914 return FALSE;
8915 }
0f20cc35
DJ
8916 break;
8917
b49e97c9
TS
8918 case R_MIPS_32:
8919 case R_MIPS_REL32:
8920 case R_MIPS_64:
0a44bf69
RS
8921 /* In VxWorks executables, references to external symbols
8922 are handled using copy relocs or PLT stubs, so there's
8923 no need to add a .rela.dyn entry for this relocation. */
861fb55a 8924 if (can_make_dynamic_p)
b49e97c9
TS
8925 {
8926 if (sreloc == NULL)
8927 {
0a44bf69 8928 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 8929 if (sreloc == NULL)
f4416af6 8930 return FALSE;
b49e97c9 8931 }
0e1862bb 8932 if (bfd_link_pic (info) && h == NULL)
82f0cfbd
EC
8933 {
8934 /* When creating a shared object, we must copy these
8935 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
8936 relocs. Make room for this reloc in .rel(a).dyn. */
8937 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 8938 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8939 /* We tell the dynamic linker that there are
8940 relocations against the text segment. */
8941 info->flags |= DF_TEXTREL;
8942 }
b49e97c9
TS
8943 else
8944 {
8945 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 8946
9a59ad6b
DJ
8947 /* For a shared object, we must copy this relocation
8948 unless the symbol turns out to be undefined and
8949 weak with non-default visibility, in which case
8950 it will be left as zero.
8951
8952 We could elide R_MIPS_REL32 for locally binding symbols
8953 in shared libraries, but do not yet do so.
8954
8955 For an executable, we only need to copy this
8956 reloc if the symbol is defined in a dynamic
8957 object. */
b49e97c9
TS
8958 hmips = (struct mips_elf_link_hash_entry *) h;
8959 ++hmips->possibly_dynamic_relocs;
943284cc 8960 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
8961 /* We need it to tell the dynamic linker if there
8962 are relocations against the text segment. */
8963 hmips->readonly_reloc = TRUE;
b49e97c9 8964 }
b49e97c9
TS
8965 }
8966
8967 if (SGI_COMPAT (abfd))
8968 mips_elf_hash_table (info)->compact_rel_size +=
8969 sizeof (Elf32_External_crinfo);
8970 break;
8971
8972 case R_MIPS_26:
8973 case R_MIPS_GPREL16:
8974 case R_MIPS_LITERAL:
8975 case R_MIPS_GPREL32:
df58fc94
RS
8976 case R_MICROMIPS_26_S1:
8977 case R_MICROMIPS_GPREL16:
8978 case R_MICROMIPS_LITERAL:
8979 case R_MICROMIPS_GPREL7_S2:
b49e97c9
TS
8980 if (SGI_COMPAT (abfd))
8981 mips_elf_hash_table (info)->compact_rel_size +=
8982 sizeof (Elf32_External_crinfo);
8983 break;
8984
8985 /* This relocation describes the C++ object vtable hierarchy.
8986 Reconstruct it for later use during GC. */
8987 case R_MIPS_GNU_VTINHERIT:
c152c796 8988 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 8989 return FALSE;
b49e97c9
TS
8990 break;
8991
8992 /* This relocation describes which C++ vtable entries are actually
8993 used. Record for later use during GC. */
8994 case R_MIPS_GNU_VTENTRY:
d17e0c6e
JB
8995 BFD_ASSERT (h != NULL);
8996 if (h != NULL
8997 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 8998 return FALSE;
b49e97c9
TS
8999 break;
9000
9001 default:
9002 break;
9003 }
9004
1bbce132 9005 /* Record the need for a PLT entry. At this point we don't know
07d6d2b8
AM
9006 yet if we are going to create a PLT in the first place, but
9007 we only record whether the relocation requires a standard MIPS
9008 or a compressed code entry anyway. If we don't make a PLT after
9009 all, then we'll just ignore these arrangements. Likewise if
9010 a PLT entry is not created because the symbol is satisfied
9011 locally. */
1bbce132 9012 if (h != NULL
54806ffa
MR
9013 && (branch_reloc_p (r_type)
9014 || mips16_branch_reloc_p (r_type)
9015 || micromips_branch_reloc_p (r_type))
1bbce132
MR
9016 && !SYMBOL_CALLS_LOCAL (info, h))
9017 {
9018 if (h->plt.plist == NULL)
9019 h->plt.plist = mips_elf_make_plt_record (abfd);
9020 if (h->plt.plist == NULL)
9021 return FALSE;
9022
54806ffa 9023 if (branch_reloc_p (r_type))
1bbce132
MR
9024 h->plt.plist->need_mips = TRUE;
9025 else
9026 h->plt.plist->need_comp = TRUE;
9027 }
9028
738e5348
RS
9029 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9030 if there is one. We only need to handle global symbols here;
9031 we decide whether to keep or delete stubs for local symbols
9032 when processing the stub's relocations. */
b49e97c9 9033 if (h != NULL
738e5348
RS
9034 && !mips16_call_reloc_p (r_type)
9035 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
9036 {
9037 struct mips_elf_link_hash_entry *mh;
9038
9039 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 9040 mh->need_fn_stub = TRUE;
b49e97c9 9041 }
861fb55a
DJ
9042
9043 /* Refuse some position-dependent relocations when creating a
9044 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9045 not PIC, but we can create dynamic relocations and the result
9046 will be fine. Also do not refuse R_MIPS_LO16, which can be
9047 combined with R_MIPS_GOT16. */
0e1862bb 9048 if (bfd_link_pic (info))
861fb55a
DJ
9049 {
9050 switch (r_type)
9051 {
9052 case R_MIPS16_HI16:
9053 case R_MIPS_HI16:
9054 case R_MIPS_HIGHER:
9055 case R_MIPS_HIGHEST:
df58fc94
RS
9056 case R_MICROMIPS_HI16:
9057 case R_MICROMIPS_HIGHER:
9058 case R_MICROMIPS_HIGHEST:
861fb55a
DJ
9059 /* Don't refuse a high part relocation if it's against
9060 no symbol (e.g. part of a compound relocation). */
cf35638d 9061 if (r_symndx == STN_UNDEF)
861fb55a
DJ
9062 break;
9063
9064 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9065 and has a special meaning. */
9066 if (!NEWABI_P (abfd) && h != NULL
9067 && strcmp (h->root.root.string, "_gp_disp") == 0)
9068 break;
9069
0fc1eb3c
RS
9070 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9071 if (is_gott_symbol (info, h))
9072 break;
9073
861fb55a
DJ
9074 /* FALLTHROUGH */
9075
9076 case R_MIPS16_26:
9077 case R_MIPS_26:
df58fc94 9078 case R_MICROMIPS_26_S1:
861fb55a 9079 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
4eca0228 9080 _bfd_error_handler
695344c0 9081 /* xgettext:c-format */
871b3ab2 9082 (_("%pB: relocation %s against `%s' can not be used"
63a5468a 9083 " when making a shared object; recompile with -fPIC"),
861fb55a
DJ
9084 abfd, howto->name,
9085 (h) ? h->root.root.string : "a local symbol");
9086 bfd_set_error (bfd_error_bad_value);
9087 return FALSE;
9088 default:
9089 break;
9090 }
9091 }
b49e97c9
TS
9092 }
9093
b34976b6 9094 return TRUE;
b49e97c9
TS
9095}
9096\f
9a59ad6b
DJ
9097/* Allocate space for global sym dynamic relocs. */
9098
9099static bfd_boolean
9100allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9101{
9102 struct bfd_link_info *info = inf;
9103 bfd *dynobj;
9104 struct mips_elf_link_hash_entry *hmips;
9105 struct mips_elf_link_hash_table *htab;
9106
9107 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9108 BFD_ASSERT (htab != NULL);
9109
9a59ad6b
DJ
9110 dynobj = elf_hash_table (info)->dynobj;
9111 hmips = (struct mips_elf_link_hash_entry *) h;
9112
9113 /* VxWorks executables are handled elsewhere; we only need to
9114 allocate relocations in shared objects. */
0e1862bb 9115 if (htab->is_vxworks && !bfd_link_pic (info))
9a59ad6b
DJ
9116 return TRUE;
9117
7686d77d
AM
9118 /* Ignore indirect symbols. All relocations against such symbols
9119 will be redirected to the target symbol. */
9120 if (h->root.type == bfd_link_hash_indirect)
63897e2c
RS
9121 return TRUE;
9122
9a59ad6b
DJ
9123 /* If this symbol is defined in a dynamic object, or we are creating
9124 a shared library, we will need to copy any R_MIPS_32 or
9125 R_MIPS_REL32 relocs against it into the output file. */
0e1862bb 9126 if (! bfd_link_relocatable (info)
9a59ad6b
DJ
9127 && hmips->possibly_dynamic_relocs != 0
9128 && (h->root.type == bfd_link_hash_defweak
625ef6dc 9129 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
0e1862bb 9130 || bfd_link_pic (info)))
9a59ad6b
DJ
9131 {
9132 bfd_boolean do_copy = TRUE;
9133
9134 if (h->root.type == bfd_link_hash_undefweak)
9135 {
262e07d0
MR
9136 /* Do not copy relocations for undefined weak symbols that
9137 we are not going to export. */
9138 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9a59ad6b
DJ
9139 do_copy = FALSE;
9140
9141 /* Make sure undefined weak symbols are output as a dynamic
9142 symbol in PIEs. */
9143 else if (h->dynindx == -1 && !h->forced_local)
9144 {
9145 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9146 return FALSE;
9147 }
9148 }
9149
9150 if (do_copy)
9151 {
aff469fa 9152 /* Even though we don't directly need a GOT entry for this symbol,
f7ff1106
RS
9153 the SVR4 psABI requires it to have a dynamic symbol table
9154 index greater that DT_MIPS_GOTSYM if there are dynamic
9155 relocations against it.
9156
9157 VxWorks does not enforce the same mapping between the GOT
9158 and the symbol table, so the same requirement does not
9159 apply there. */
6ccf4795
RS
9160 if (!htab->is_vxworks)
9161 {
9162 if (hmips->global_got_area > GGA_RELOC_ONLY)
9163 hmips->global_got_area = GGA_RELOC_ONLY;
9164 hmips->got_only_for_calls = FALSE;
9165 }
aff469fa 9166
9a59ad6b
DJ
9167 mips_elf_allocate_dynamic_relocations
9168 (dynobj, info, hmips->possibly_dynamic_relocs);
9169 if (hmips->readonly_reloc)
9170 /* We tell the dynamic linker that there are relocations
9171 against the text segment. */
9172 info->flags |= DF_TEXTREL;
9173 }
9174 }
9175
9176 return TRUE;
9177}
9178
b49e97c9
TS
9179/* Adjust a symbol defined by a dynamic object and referenced by a
9180 regular object. The current definition is in some section of the
9181 dynamic object, but we're not including those sections. We have to
9182 change the definition to something the rest of the link can
9183 understand. */
9184
b34976b6 9185bfd_boolean
9719ad41
RS
9186_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9187 struct elf_link_hash_entry *h)
b49e97c9
TS
9188{
9189 bfd *dynobj;
9190 struct mips_elf_link_hash_entry *hmips;
5108fc1b 9191 struct mips_elf_link_hash_table *htab;
5474d94f 9192 asection *s, *srel;
b49e97c9 9193
5108fc1b 9194 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9195 BFD_ASSERT (htab != NULL);
9196
b49e97c9 9197 dynobj = elf_hash_table (info)->dynobj;
861fb55a 9198 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
9199
9200 /* Make sure we know what is going on here. */
9201 BFD_ASSERT (dynobj != NULL
f5385ebf 9202 && (h->needs_plt
60d67dc8 9203 || h->is_weakalias
f5385ebf
AM
9204 || (h->def_dynamic
9205 && h->ref_regular
9206 && !h->def_regular)));
b49e97c9 9207
b49e97c9 9208 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9209
861fb55a
DJ
9210 /* If there are call relocations against an externally-defined symbol,
9211 see whether we can create a MIPS lazy-binding stub for it. We can
9212 only do this if all references to the function are through call
9213 relocations, and in that case, the traditional lazy-binding stubs
9214 are much more efficient than PLT entries.
9215
9216 Traditional stubs are only available on SVR4 psABI-based systems;
9217 VxWorks always uses PLTs instead. */
9218 if (!htab->is_vxworks && h->needs_plt && !hmips->no_fn_stub)
b49e97c9
TS
9219 {
9220 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 9221 return TRUE;
b49e97c9
TS
9222
9223 /* If this symbol is not defined in a regular file, then set
9224 the symbol to the stub location. This is required to make
9225 function pointers compare as equal between the normal
9226 executable and the shared library. */
4b8377e7
MR
9227 if (!h->def_regular
9228 && !bfd_is_abs_section (htab->sstubs->output_section))
b49e97c9 9229 {
33bb52fb
RS
9230 hmips->needs_lazy_stub = TRUE;
9231 htab->lazy_stub_count++;
b34976b6 9232 return TRUE;
b49e97c9
TS
9233 }
9234 }
861fb55a
DJ
9235 /* As above, VxWorks requires PLT entries for externally-defined
9236 functions that are only accessed through call relocations.
b49e97c9 9237
861fb55a
DJ
9238 Both VxWorks and non-VxWorks targets also need PLT entries if there
9239 are static-only relocations against an externally-defined function.
9240 This can technically occur for shared libraries if there are
9241 branches to the symbol, although it is unlikely that this will be
9242 used in practice due to the short ranges involved. It can occur
9243 for any relative or absolute relocation in executables; in that
9244 case, the PLT entry becomes the function's canonical address. */
9245 else if (((h->needs_plt && !hmips->no_fn_stub)
9246 || (h->type == STT_FUNC && hmips->has_static_relocs))
9247 && htab->use_plts_and_copy_relocs
9248 && !SYMBOL_CALLS_LOCAL (info, h)
9249 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9250 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 9251 {
1bbce132
MR
9252 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9253 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
9254
9255 /* If this is the first symbol to need a PLT entry, then make some
07d6d2b8
AM
9256 basic setup. Also work out PLT entry sizes. We'll need them
9257 for PLT offset calculations. */
1bbce132 9258 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
861fb55a 9259 {
ce558b89 9260 BFD_ASSERT (htab->root.sgotplt->size == 0);
1bbce132 9261 BFD_ASSERT (htab->plt_got_index == 0);
0a44bf69 9262
861fb55a
DJ
9263 /* If we're using the PLT additions to the psABI, each PLT
9264 entry is 16 bytes and the PLT0 entry is 32 bytes.
9265 Encourage better cache usage by aligning. We do this
9266 lazily to avoid pessimizing traditional objects. */
9267 if (!htab->is_vxworks
ce558b89 9268 && !bfd_set_section_alignment (dynobj, htab->root.splt, 5))
861fb55a 9269 return FALSE;
0a44bf69 9270
861fb55a
DJ
9271 /* Make sure that .got.plt is word-aligned. We do this lazily
9272 for the same reason as above. */
ce558b89 9273 if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt,
861fb55a
DJ
9274 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9275 return FALSE;
0a44bf69 9276
861fb55a
DJ
9277 /* On non-VxWorks targets, the first two entries in .got.plt
9278 are reserved. */
9279 if (!htab->is_vxworks)
1bbce132
MR
9280 htab->plt_got_index
9281 += (get_elf_backend_data (dynobj)->got_header_size
9282 / MIPS_ELF_GOT_SIZE (dynobj));
0a44bf69 9283
861fb55a
DJ
9284 /* On VxWorks, also allocate room for the header's
9285 .rela.plt.unloaded entries. */
0e1862bb 9286 if (htab->is_vxworks && !bfd_link_pic (info))
0a44bf69 9287 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
1bbce132
MR
9288
9289 /* Now work out the sizes of individual PLT entries. */
0e1862bb 9290 if (htab->is_vxworks && bfd_link_pic (info))
1bbce132
MR
9291 htab->plt_mips_entry_size
9292 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
9293 else if (htab->is_vxworks)
9294 htab->plt_mips_entry_size
9295 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9296 else if (newabi_p)
9297 htab->plt_mips_entry_size
9298 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
833794fc 9299 else if (!micromips_p)
1bbce132
MR
9300 {
9301 htab->plt_mips_entry_size
9302 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9303 htab->plt_comp_entry_size
833794fc
MR
9304 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9305 }
9306 else if (htab->insn32)
9307 {
9308 htab->plt_mips_entry_size
9309 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9310 htab->plt_comp_entry_size
9311 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
1bbce132
MR
9312 }
9313 else
9314 {
9315 htab->plt_mips_entry_size
9316 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9317 htab->plt_comp_entry_size
833794fc 9318 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
1bbce132 9319 }
0a44bf69
RS
9320 }
9321
1bbce132
MR
9322 if (h->plt.plist == NULL)
9323 h->plt.plist = mips_elf_make_plt_record (dynobj);
9324 if (h->plt.plist == NULL)
9325 return FALSE;
9326
9327 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
07d6d2b8 9328 n32 or n64, so always use a standard entry there.
1bbce132 9329
07d6d2b8
AM
9330 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9331 all MIPS16 calls will go via that stub, and there is no benefit
9332 to having a MIPS16 entry. And in the case of call_stub a
9333 standard entry actually has to be used as the stub ends with a J
9334 instruction. */
1bbce132
MR
9335 if (newabi_p
9336 || htab->is_vxworks
9337 || hmips->call_stub
9338 || hmips->call_fp_stub)
9339 {
9340 h->plt.plist->need_mips = TRUE;
9341 h->plt.plist->need_comp = FALSE;
9342 }
9343
9344 /* Otherwise, if there are no direct calls to the function, we
07d6d2b8
AM
9345 have a free choice of whether to use standard or compressed
9346 entries. Prefer microMIPS entries if the object is known to
9347 contain microMIPS code, so that it becomes possible to create
9348 pure microMIPS binaries. Prefer standard entries otherwise,
9349 because MIPS16 ones are no smaller and are usually slower. */
1bbce132
MR
9350 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9351 {
9352 if (micromips_p)
9353 h->plt.plist->need_comp = TRUE;
9354 else
9355 h->plt.plist->need_mips = TRUE;
9356 }
9357
9358 if (h->plt.plist->need_mips)
9359 {
9360 h->plt.plist->mips_offset = htab->plt_mips_offset;
9361 htab->plt_mips_offset += htab->plt_mips_entry_size;
9362 }
9363 if (h->plt.plist->need_comp)
9364 {
9365 h->plt.plist->comp_offset = htab->plt_comp_offset;
9366 htab->plt_comp_offset += htab->plt_comp_entry_size;
9367 }
9368
9369 /* Reserve the corresponding .got.plt entry now too. */
9370 h->plt.plist->gotplt_index = htab->plt_got_index++;
0a44bf69
RS
9371
9372 /* If the output file has no definition of the symbol, set the
861fb55a 9373 symbol's value to the address of the stub. */
0e1862bb 9374 if (!bfd_link_pic (info) && !h->def_regular)
1bbce132 9375 hmips->use_plt_entry = TRUE;
0a44bf69 9376
1bbce132 9377 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
ce558b89
AM
9378 htab->root.srelplt->size += (htab->is_vxworks
9379 ? MIPS_ELF_RELA_SIZE (dynobj)
9380 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
9381
9382 /* Make room for the .rela.plt.unloaded relocations. */
0e1862bb 9383 if (htab->is_vxworks && !bfd_link_pic (info))
0a44bf69
RS
9384 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9385
861fb55a
DJ
9386 /* All relocations against this symbol that could have been made
9387 dynamic will now refer to the PLT entry instead. */
9388 hmips->possibly_dynamic_relocs = 0;
0a44bf69 9389
0a44bf69
RS
9390 return TRUE;
9391 }
9392
9393 /* If this is a weak symbol, and there is a real definition, the
9394 processor independent code will have arranged for us to see the
9395 real definition first, and we can just use the same value. */
60d67dc8 9396 if (h->is_weakalias)
0a44bf69 9397 {
60d67dc8
AM
9398 struct elf_link_hash_entry *def = weakdef (h);
9399 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9400 h->root.u.def.section = def->root.u.def.section;
9401 h->root.u.def.value = def->root.u.def.value;
0a44bf69
RS
9402 return TRUE;
9403 }
9404
861fb55a
DJ
9405 /* Otherwise, there is nothing further to do for symbols defined
9406 in regular objects. */
9407 if (h->def_regular)
0a44bf69
RS
9408 return TRUE;
9409
861fb55a
DJ
9410 /* There's also nothing more to do if we'll convert all relocations
9411 against this symbol into dynamic relocations. */
9412 if (!hmips->has_static_relocs)
9413 return TRUE;
9414
9415 /* We're now relying on copy relocations. Complain if we have
9416 some that we can't convert. */
0e1862bb 9417 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
861fb55a 9418 {
4eca0228
AM
9419 _bfd_error_handler (_("non-dynamic relocations refer to "
9420 "dynamic symbol %s"),
9421 h->root.root.string);
861fb55a
DJ
9422 bfd_set_error (bfd_error_bad_value);
9423 return FALSE;
9424 }
9425
0a44bf69
RS
9426 /* We must allocate the symbol in our .dynbss section, which will
9427 become part of the .bss section of the executable. There will be
9428 an entry for this symbol in the .dynsym section. The dynamic
9429 object will contain position independent code, so all references
9430 from the dynamic object to this symbol will go through the global
9431 offset table. The dynamic linker will use the .dynsym entry to
9432 determine the address it must put in the global offset table, so
9433 both the dynamic object and the regular object will refer to the
9434 same memory location for the variable. */
9435
5474d94f
AM
9436 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9437 {
9438 s = htab->root.sdynrelro;
9439 srel = htab->root.sreldynrelro;
9440 }
9441 else
9442 {
9443 s = htab->root.sdynbss;
9444 srel = htab->root.srelbss;
9445 }
0a44bf69
RS
9446 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9447 {
861fb55a 9448 if (htab->is_vxworks)
5474d94f 9449 srel->size += sizeof (Elf32_External_Rela);
861fb55a
DJ
9450 else
9451 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
9452 h->needs_copy = 1;
9453 }
9454
861fb55a
DJ
9455 /* All relocations against this symbol that could have been made
9456 dynamic will now refer to the local copy instead. */
9457 hmips->possibly_dynamic_relocs = 0;
9458
5474d94f 9459 return _bfd_elf_adjust_dynamic_copy (info, h, s);
0a44bf69 9460}
b49e97c9
TS
9461\f
9462/* This function is called after all the input files have been read,
9463 and the input sections have been assigned to output sections. We
9464 check for any mips16 stub sections that we can discard. */
9465
b34976b6 9466bfd_boolean
9719ad41
RS
9467_bfd_mips_elf_always_size_sections (bfd *output_bfd,
9468 struct bfd_link_info *info)
b49e97c9 9469{
351cdf24 9470 asection *sect;
0a44bf69 9471 struct mips_elf_link_hash_table *htab;
861fb55a 9472 struct mips_htab_traverse_info hti;
0a44bf69
RS
9473
9474 htab = mips_elf_hash_table (info);
4dfe6ac6 9475 BFD_ASSERT (htab != NULL);
f4416af6 9476
b49e97c9 9477 /* The .reginfo section has a fixed size. */
351cdf24
MF
9478 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9479 if (sect != NULL)
6798f8bf
MR
9480 {
9481 bfd_set_section_size (output_bfd, sect, sizeof (Elf32_External_RegInfo));
9482 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9483 }
351cdf24
MF
9484
9485 /* The .MIPS.abiflags section has a fixed size. */
9486 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9487 if (sect != NULL)
6798f8bf
MR
9488 {
9489 bfd_set_section_size (output_bfd, sect,
9490 sizeof (Elf_External_ABIFlags_v0));
9491 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9492 }
b49e97c9 9493
861fb55a
DJ
9494 hti.info = info;
9495 hti.output_bfd = output_bfd;
9496 hti.error = FALSE;
9497 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9498 mips_elf_check_symbols, &hti);
9499 if (hti.error)
9500 return FALSE;
f4416af6 9501
33bb52fb
RS
9502 return TRUE;
9503}
9504
9505/* If the link uses a GOT, lay it out and work out its size. */
9506
9507static bfd_boolean
9508mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9509{
9510 bfd *dynobj;
9511 asection *s;
9512 struct mips_got_info *g;
33bb52fb
RS
9513 bfd_size_type loadable_size = 0;
9514 bfd_size_type page_gotno;
d7206569 9515 bfd *ibfd;
ab361d49 9516 struct mips_elf_traverse_got_arg tga;
33bb52fb
RS
9517 struct mips_elf_link_hash_table *htab;
9518
9519 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9520 BFD_ASSERT (htab != NULL);
9521
ce558b89 9522 s = htab->root.sgot;
f4416af6 9523 if (s == NULL)
b34976b6 9524 return TRUE;
b49e97c9 9525
33bb52fb 9526 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
9527 g = htab->got_info;
9528
861fb55a
DJ
9529 /* Allocate room for the reserved entries. VxWorks always reserves
9530 3 entries; other objects only reserve 2 entries. */
cb22ccf4 9531 BFD_ASSERT (g->assigned_low_gotno == 0);
861fb55a
DJ
9532 if (htab->is_vxworks)
9533 htab->reserved_gotno = 3;
9534 else
9535 htab->reserved_gotno = 2;
9536 g->local_gotno += htab->reserved_gotno;
cb22ccf4 9537 g->assigned_low_gotno = htab->reserved_gotno;
861fb55a 9538
6c42ddb9
RS
9539 /* Decide which symbols need to go in the global part of the GOT and
9540 count the number of reloc-only GOT symbols. */
020d7251 9541 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
f4416af6 9542
13db6b44
RS
9543 if (!mips_elf_resolve_final_got_entries (info, g))
9544 return FALSE;
9545
33bb52fb
RS
9546 /* Calculate the total loadable size of the output. That
9547 will give us the maximum number of GOT_PAGE entries
9548 required. */
c72f2fb2 9549 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
33bb52fb
RS
9550 {
9551 asection *subsection;
5108fc1b 9552
d7206569 9553 for (subsection = ibfd->sections;
33bb52fb
RS
9554 subsection;
9555 subsection = subsection->next)
9556 {
9557 if ((subsection->flags & SEC_ALLOC) == 0)
9558 continue;
9559 loadable_size += ((subsection->size + 0xf)
9560 &~ (bfd_size_type) 0xf);
9561 }
9562 }
f4416af6 9563
0a44bf69 9564 if (htab->is_vxworks)
738e5348 9565 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
9566 relocations against local symbols evaluate to "G", and the EABI does
9567 not include R_MIPS_GOT_PAGE. */
c224138d 9568 page_gotno = 0;
0a44bf69
RS
9569 else
9570 /* Assume there are two loadable segments consisting of contiguous
9571 sections. Is 5 enough? */
c224138d
RS
9572 page_gotno = (loadable_size >> 16) + 5;
9573
13db6b44 9574 /* Choose the smaller of the two page estimates; both are intended to be
c224138d
RS
9575 conservative. */
9576 if (page_gotno > g->page_gotno)
9577 page_gotno = g->page_gotno;
f4416af6 9578
c224138d 9579 g->local_gotno += page_gotno;
cb22ccf4 9580 g->assigned_high_gotno = g->local_gotno - 1;
ab361d49 9581
ab361d49
RS
9582 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9583 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
0f20cc35
DJ
9584 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9585
0a44bf69
RS
9586 /* VxWorks does not support multiple GOTs. It initializes $gp to
9587 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9588 dynamic loader. */
57093f5e 9589 if (!htab->is_vxworks && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 9590 {
a8028dd0 9591 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
9592 return FALSE;
9593 }
9594 else
9595 {
d7206569
RS
9596 /* Record that all bfds use G. This also has the effect of freeing
9597 the per-bfd GOTs, which we no longer need. */
c72f2fb2 9598 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
d7206569
RS
9599 if (mips_elf_bfd_got (ibfd, FALSE))
9600 mips_elf_replace_bfd_got (ibfd, g);
9601 mips_elf_replace_bfd_got (output_bfd, g);
9602
33bb52fb 9603 /* Set up TLS entries. */
0f20cc35 9604 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
72e7511a
RS
9605 tga.info = info;
9606 tga.g = g;
9607 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9608 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9609 if (!tga.g)
9610 return FALSE;
1fd20d70
RS
9611 BFD_ASSERT (g->tls_assigned_gotno
9612 == g->global_gotno + g->local_gotno + g->tls_gotno);
33bb52fb 9613
57093f5e 9614 /* Each VxWorks GOT entry needs an explicit relocation. */
0e1862bb 9615 if (htab->is_vxworks && bfd_link_pic (info))
57093f5e
RS
9616 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9617
33bb52fb 9618 /* Allocate room for the TLS relocations. */
ab361d49
RS
9619 if (g->relocs)
9620 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
0f20cc35 9621 }
b49e97c9 9622
b34976b6 9623 return TRUE;
b49e97c9
TS
9624}
9625
33bb52fb
RS
9626/* Estimate the size of the .MIPS.stubs section. */
9627
9628static void
9629mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9630{
9631 struct mips_elf_link_hash_table *htab;
9632 bfd_size_type dynsymcount;
9633
9634 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9635 BFD_ASSERT (htab != NULL);
9636
33bb52fb
RS
9637 if (htab->lazy_stub_count == 0)
9638 return;
9639
9640 /* IRIX rld assumes that a function stub isn't at the end of the .text
9641 section, so add a dummy entry to the end. */
9642 htab->lazy_stub_count++;
9643
9644 /* Get a worst-case estimate of the number of dynamic symbols needed.
9645 At this point, dynsymcount does not account for section symbols
9646 and count_section_dynsyms may overestimate the number that will
9647 be needed. */
9648 dynsymcount = (elf_hash_table (info)->dynsymcount
9649 + count_section_dynsyms (output_bfd, info));
9650
1bbce132
MR
9651 /* Determine the size of one stub entry. There's no disadvantage
9652 from using microMIPS code here, so for the sake of pure-microMIPS
9653 binaries we prefer it whenever there's any microMIPS code in
9654 output produced at all. This has a benefit of stubs being
833794fc
MR
9655 shorter by 4 bytes each too, unless in the insn32 mode. */
9656 if (!MICROMIPS_P (output_bfd))
1bbce132
MR
9657 htab->function_stub_size = (dynsymcount > 0x10000
9658 ? MIPS_FUNCTION_STUB_BIG_SIZE
9659 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
833794fc
MR
9660 else if (htab->insn32)
9661 htab->function_stub_size = (dynsymcount > 0x10000
9662 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9663 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9664 else
9665 htab->function_stub_size = (dynsymcount > 0x10000
9666 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9667 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
33bb52fb
RS
9668
9669 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9670}
9671
1bbce132
MR
9672/* A mips_elf_link_hash_traverse callback for which DATA points to a
9673 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9674 stub, allocate an entry in the stubs section. */
33bb52fb
RS
9675
9676static bfd_boolean
af924177 9677mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 9678{
1bbce132 9679 struct mips_htab_traverse_info *hti = data;
33bb52fb 9680 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9681 struct bfd_link_info *info;
9682 bfd *output_bfd;
9683
9684 info = hti->info;
9685 output_bfd = hti->output_bfd;
9686 htab = mips_elf_hash_table (info);
9687 BFD_ASSERT (htab != NULL);
33bb52fb 9688
33bb52fb
RS
9689 if (h->needs_lazy_stub)
9690 {
1bbce132
MR
9691 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9692 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9693 bfd_vma isa_bit = micromips_p;
9694
9695 BFD_ASSERT (htab->root.dynobj != NULL);
9696 if (h->root.plt.plist == NULL)
9697 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9698 if (h->root.plt.plist == NULL)
9699 {
9700 hti->error = TRUE;
9701 return FALSE;
9702 }
33bb52fb 9703 h->root.root.u.def.section = htab->sstubs;
1bbce132
MR
9704 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9705 h->root.plt.plist->stub_offset = htab->sstubs->size;
9706 h->root.other = other;
33bb52fb
RS
9707 htab->sstubs->size += htab->function_stub_size;
9708 }
9709 return TRUE;
9710}
9711
9712/* Allocate offsets in the stubs section to each symbol that needs one.
9713 Set the final size of the .MIPS.stub section. */
9714
1bbce132 9715static bfd_boolean
33bb52fb
RS
9716mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9717{
1bbce132
MR
9718 bfd *output_bfd = info->output_bfd;
9719 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9720 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9721 bfd_vma isa_bit = micromips_p;
33bb52fb 9722 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9723 struct mips_htab_traverse_info hti;
9724 struct elf_link_hash_entry *h;
9725 bfd *dynobj;
33bb52fb
RS
9726
9727 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9728 BFD_ASSERT (htab != NULL);
9729
33bb52fb 9730 if (htab->lazy_stub_count == 0)
1bbce132 9731 return TRUE;
33bb52fb
RS
9732
9733 htab->sstubs->size = 0;
1bbce132
MR
9734 hti.info = info;
9735 hti.output_bfd = output_bfd;
9736 hti.error = FALSE;
9737 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9738 if (hti.error)
9739 return FALSE;
33bb52fb
RS
9740 htab->sstubs->size += htab->function_stub_size;
9741 BFD_ASSERT (htab->sstubs->size
9742 == htab->lazy_stub_count * htab->function_stub_size);
1bbce132
MR
9743
9744 dynobj = elf_hash_table (info)->dynobj;
9745 BFD_ASSERT (dynobj != NULL);
9746 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9747 if (h == NULL)
9748 return FALSE;
9749 h->root.u.def.value = isa_bit;
9750 h->other = other;
9751 h->type = STT_FUNC;
9752
9753 return TRUE;
9754}
9755
9756/* A mips_elf_link_hash_traverse callback for which DATA points to a
9757 bfd_link_info. If H uses the address of a PLT entry as the value
9758 of the symbol, then set the entry in the symbol table now. Prefer
9759 a standard MIPS PLT entry. */
9760
9761static bfd_boolean
9762mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9763{
9764 struct bfd_link_info *info = data;
9765 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9766 struct mips_elf_link_hash_table *htab;
9767 unsigned int other;
9768 bfd_vma isa_bit;
9769 bfd_vma val;
9770
9771 htab = mips_elf_hash_table (info);
9772 BFD_ASSERT (htab != NULL);
9773
9774 if (h->use_plt_entry)
9775 {
9776 BFD_ASSERT (h->root.plt.plist != NULL);
9777 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9778 || h->root.plt.plist->comp_offset != MINUS_ONE);
9779
9780 val = htab->plt_header_size;
9781 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9782 {
9783 isa_bit = 0;
9784 val += h->root.plt.plist->mips_offset;
9785 other = 0;
9786 }
9787 else
9788 {
9789 isa_bit = 1;
9790 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9791 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9792 }
9793 val += isa_bit;
9794 /* For VxWorks, point at the PLT load stub rather than the lazy
07d6d2b8
AM
9795 resolution stub; this stub will become the canonical function
9796 address. */
1bbce132
MR
9797 if (htab->is_vxworks)
9798 val += 8;
9799
ce558b89 9800 h->root.root.u.def.section = htab->root.splt;
1bbce132
MR
9801 h->root.root.u.def.value = val;
9802 h->root.other = other;
9803 }
9804
9805 return TRUE;
33bb52fb
RS
9806}
9807
b49e97c9
TS
9808/* Set the sizes of the dynamic sections. */
9809
b34976b6 9810bfd_boolean
9719ad41
RS
9811_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9812 struct bfd_link_info *info)
b49e97c9
TS
9813{
9814 bfd *dynobj;
861fb55a 9815 asection *s, *sreldyn;
b34976b6 9816 bfd_boolean reltext;
0a44bf69 9817 struct mips_elf_link_hash_table *htab;
b49e97c9 9818
0a44bf69 9819 htab = mips_elf_hash_table (info);
4dfe6ac6 9820 BFD_ASSERT (htab != NULL);
b49e97c9
TS
9821 dynobj = elf_hash_table (info)->dynobj;
9822 BFD_ASSERT (dynobj != NULL);
9823
9824 if (elf_hash_table (info)->dynamic_sections_created)
9825 {
9826 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9827 if (bfd_link_executable (info) && !info->nointerp)
b49e97c9 9828 {
3d4d4302 9829 s = bfd_get_linker_section (dynobj, ".interp");
b49e97c9 9830 BFD_ASSERT (s != NULL);
eea6121a 9831 s->size
b49e97c9
TS
9832 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9833 s->contents
9834 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9835 }
861fb55a 9836
1bbce132 9837 /* Figure out the size of the PLT header if we know that we
07d6d2b8
AM
9838 are using it. For the sake of cache alignment always use
9839 a standard header whenever any standard entries are present
9840 even if microMIPS entries are present as well. This also
9841 lets the microMIPS header rely on the value of $v0 only set
9842 by microMIPS entries, for a small size reduction.
1bbce132 9843
07d6d2b8
AM
9844 Set symbol table entry values for symbols that use the
9845 address of their PLT entry now that we can calculate it.
1bbce132 9846
07d6d2b8
AM
9847 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9848 haven't already in _bfd_elf_create_dynamic_sections. */
ce558b89 9849 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
861fb55a 9850 {
1bbce132
MR
9851 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9852 && !htab->plt_mips_offset);
9853 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9854 bfd_vma isa_bit = micromips_p;
861fb55a 9855 struct elf_link_hash_entry *h;
1bbce132 9856 bfd_vma size;
861fb55a
DJ
9857
9858 BFD_ASSERT (htab->use_plts_and_copy_relocs);
ce558b89
AM
9859 BFD_ASSERT (htab->root.sgotplt->size == 0);
9860 BFD_ASSERT (htab->root.splt->size == 0);
1bbce132 9861
0e1862bb 9862 if (htab->is_vxworks && bfd_link_pic (info))
1bbce132
MR
9863 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
9864 else if (htab->is_vxworks)
9865 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9866 else if (ABI_64_P (output_bfd))
9867 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9868 else if (ABI_N32_P (output_bfd))
9869 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9870 else if (!micromips_p)
9871 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
833794fc
MR
9872 else if (htab->insn32)
9873 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
1bbce132
MR
9874 else
9875 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
861fb55a 9876
1bbce132
MR
9877 htab->plt_header_is_comp = micromips_p;
9878 htab->plt_header_size = size;
ce558b89
AM
9879 htab->root.splt->size = (size
9880 + htab->plt_mips_offset
9881 + htab->plt_comp_offset);
9882 htab->root.sgotplt->size = (htab->plt_got_index
9883 * MIPS_ELF_GOT_SIZE (dynobj));
1bbce132
MR
9884
9885 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9886
9887 if (htab->root.hplt == NULL)
9888 {
ce558b89 9889 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
1bbce132
MR
9890 "_PROCEDURE_LINKAGE_TABLE_");
9891 htab->root.hplt = h;
9892 if (h == NULL)
9893 return FALSE;
9894 }
9895
9896 h = htab->root.hplt;
9897 h->root.u.def.value = isa_bit;
9898 h->other = other;
861fb55a
DJ
9899 h->type = STT_FUNC;
9900 }
9901 }
4e41d0d7 9902
9a59ad6b 9903 /* Allocate space for global sym dynamic relocs. */
2c3fc389 9904 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9a59ad6b 9905
33bb52fb
RS
9906 mips_elf_estimate_stub_size (output_bfd, info);
9907
9908 if (!mips_elf_lay_out_got (output_bfd, info))
9909 return FALSE;
9910
9911 mips_elf_lay_out_lazy_stubs (info);
9912
b49e97c9
TS
9913 /* The check_relocs and adjust_dynamic_symbol entry points have
9914 determined the sizes of the various dynamic sections. Allocate
9915 memory for them. */
b34976b6 9916 reltext = FALSE;
b49e97c9
TS
9917 for (s = dynobj->sections; s != NULL; s = s->next)
9918 {
9919 const char *name;
b49e97c9
TS
9920
9921 /* It's OK to base decisions on the section name, because none
9922 of the dynobj section names depend upon the input files. */
9923 name = bfd_get_section_name (dynobj, s);
9924
9925 if ((s->flags & SEC_LINKER_CREATED) == 0)
9926 continue;
9927
0112cd26 9928 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 9929 {
c456f082 9930 if (s->size != 0)
b49e97c9
TS
9931 {
9932 const char *outname;
9933 asection *target;
9934
9935 /* If this relocation section applies to a read only
07d6d2b8
AM
9936 section, then we probably need a DT_TEXTREL entry.
9937 If the relocation section is .rel(a).dyn, we always
9938 assert a DT_TEXTREL entry rather than testing whether
9939 there exists a relocation to a read only section or
9940 not. */
b49e97c9
TS
9941 outname = bfd_get_section_name (output_bfd,
9942 s->output_section);
9943 target = bfd_get_section_by_name (output_bfd, outname + 4);
9944 if ((target != NULL
9945 && (target->flags & SEC_READONLY) != 0
9946 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 9947 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 9948 reltext = TRUE;
b49e97c9
TS
9949
9950 /* We use the reloc_count field as a counter if we need
9951 to copy relocs into the output file. */
0a44bf69 9952 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 9953 s->reloc_count = 0;
f4416af6
AO
9954
9955 /* If combreloc is enabled, elf_link_sort_relocs() will
9956 sort relocations, but in a different way than we do,
9957 and before we're done creating relocations. Also, it
9958 will move them around between input sections'
9959 relocation's contents, so our sorting would be
9960 broken, so don't let it run. */
9961 info->combreloc = 0;
b49e97c9
TS
9962 }
9963 }
0e1862bb 9964 else if (bfd_link_executable (info)
b49e97c9 9965 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 9966 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 9967 {
5108fc1b 9968 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 9969 rtld to contain a pointer to the _r_debug structure. */
b4082c70 9970 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
b49e97c9
TS
9971 }
9972 else if (SGI_COMPAT (output_bfd)
0112cd26 9973 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 9974 s->size += mips_elf_hash_table (info)->compact_rel_size;
ce558b89 9975 else if (s == htab->root.splt)
861fb55a
DJ
9976 {
9977 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
9978 room for an extra nop to fill the delay slot. This is
9979 for CPUs without load interlocking. */
9980 if (! LOAD_INTERLOCKS_P (output_bfd)
9981 && ! htab->is_vxworks && s->size > 0)
861fb55a
DJ
9982 s->size += 4;
9983 }
0112cd26 9984 else if (! CONST_STRNEQ (name, ".init")
ce558b89
AM
9985 && s != htab->root.sgot
9986 && s != htab->root.sgotplt
861fb55a 9987 && s != htab->sstubs
5474d94f
AM
9988 && s != htab->root.sdynbss
9989 && s != htab->root.sdynrelro)
b49e97c9
TS
9990 {
9991 /* It's not one of our sections, so don't allocate space. */
9992 continue;
9993 }
9994
c456f082 9995 if (s->size == 0)
b49e97c9 9996 {
8423293d 9997 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
9998 continue;
9999 }
10000
c456f082
AM
10001 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10002 continue;
10003
b49e97c9 10004 /* Allocate memory for the section contents. */
eea6121a 10005 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 10006 if (s->contents == NULL)
b49e97c9
TS
10007 {
10008 bfd_set_error (bfd_error_no_memory);
b34976b6 10009 return FALSE;
b49e97c9
TS
10010 }
10011 }
10012
10013 if (elf_hash_table (info)->dynamic_sections_created)
10014 {
10015 /* Add some entries to the .dynamic section. We fill in the
10016 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10017 must add the entries now so that we get the correct size for
5750dcec 10018 the .dynamic section. */
af5978fb
RS
10019
10020 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec 10021 DT_MIPS_RLD_MAP entry. This must come first because glibc
6e6be592
MR
10022 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10023 may only look at the first one they see. */
0e1862bb 10024 if (!bfd_link_pic (info)
af5978fb
RS
10025 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10026 return FALSE;
b49e97c9 10027
0e1862bb 10028 if (bfd_link_executable (info)
a5499fa4
MF
10029 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10030 return FALSE;
10031
5750dcec
DJ
10032 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10033 used by the debugger. */
0e1862bb 10034 if (bfd_link_executable (info)
5750dcec
DJ
10035 && !SGI_COMPAT (output_bfd)
10036 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10037 return FALSE;
10038
0a44bf69 10039 if (reltext && (SGI_COMPAT (output_bfd) || htab->is_vxworks))
b49e97c9
TS
10040 info->flags |= DF_TEXTREL;
10041
10042 if ((info->flags & DF_TEXTREL) != 0)
10043 {
10044 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 10045 return FALSE;
943284cc
DJ
10046
10047 /* Clear the DF_TEXTREL flag. It will be set again if we
10048 write out an actual text relocation; we may not, because
10049 at this point we do not know whether e.g. any .eh_frame
10050 absolute relocations have been converted to PC-relative. */
10051 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
10052 }
10053
10054 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 10055 return FALSE;
b49e97c9 10056
861fb55a 10057 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
0a44bf69 10058 if (htab->is_vxworks)
b49e97c9 10059 {
0a44bf69
RS
10060 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10061 use any of the DT_MIPS_* tags. */
861fb55a 10062 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
10063 {
10064 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10065 return FALSE;
b49e97c9 10066
0a44bf69
RS
10067 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10068 return FALSE;
b49e97c9 10069
0a44bf69
RS
10070 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10071 return FALSE;
10072 }
b49e97c9 10073 }
0a44bf69
RS
10074 else
10075 {
db841b6f
MR
10076 if (sreldyn && sreldyn->size > 0
10077 && !bfd_is_abs_section (sreldyn->output_section))
0a44bf69
RS
10078 {
10079 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10080 return FALSE;
b49e97c9 10081
0a44bf69
RS
10082 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10083 return FALSE;
b49e97c9 10084
0a44bf69
RS
10085 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10086 return FALSE;
10087 }
b49e97c9 10088
0a44bf69
RS
10089 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10090 return FALSE;
b49e97c9 10091
0a44bf69
RS
10092 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10093 return FALSE;
b49e97c9 10094
0a44bf69
RS
10095 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10096 return FALSE;
b49e97c9 10097
0a44bf69
RS
10098 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10099 return FALSE;
b49e97c9 10100
0a44bf69
RS
10101 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10102 return FALSE;
b49e97c9 10103
0a44bf69
RS
10104 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10105 return FALSE;
b49e97c9 10106
0a44bf69
RS
10107 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10108 return FALSE;
10109
10110 if (IRIX_COMPAT (dynobj) == ict_irix5
10111 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10112 return FALSE;
10113
10114 if (IRIX_COMPAT (dynobj) == ict_irix6
10115 && (bfd_get_section_by_name
af0edeb8 10116 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
0a44bf69
RS
10117 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10118 return FALSE;
10119 }
ce558b89 10120 if (htab->root.splt->size > 0)
861fb55a
DJ
10121 {
10122 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10123 return FALSE;
10124
10125 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10126 return FALSE;
10127
10128 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10129 return FALSE;
10130
10131 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10132 return FALSE;
10133 }
7a2b07ff
NS
10134 if (htab->is_vxworks
10135 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10136 return FALSE;
b49e97c9
TS
10137 }
10138
b34976b6 10139 return TRUE;
b49e97c9
TS
10140}
10141\f
81d43bff
RS
10142/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10143 Adjust its R_ADDEND field so that it is correct for the output file.
10144 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10145 and sections respectively; both use symbol indexes. */
10146
10147static void
10148mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10149 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10150 asection **local_sections, Elf_Internal_Rela *rel)
10151{
10152 unsigned int r_type, r_symndx;
10153 Elf_Internal_Sym *sym;
10154 asection *sec;
10155
020d7251 10156 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
81d43bff
RS
10157 {
10158 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
df58fc94 10159 if (gprel16_reloc_p (r_type)
81d43bff 10160 || r_type == R_MIPS_GPREL32
df58fc94 10161 || literal_reloc_p (r_type))
81d43bff
RS
10162 {
10163 rel->r_addend += _bfd_get_gp_value (input_bfd);
10164 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10165 }
10166
10167 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10168 sym = local_syms + r_symndx;
10169
10170 /* Adjust REL's addend to account for section merging. */
0e1862bb 10171 if (!bfd_link_relocatable (info))
81d43bff
RS
10172 {
10173 sec = local_sections[r_symndx];
10174 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10175 }
10176
10177 /* This would normally be done by the rela_normal code in elflink.c. */
10178 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10179 rel->r_addend += local_sections[r_symndx]->output_offset;
10180 }
10181}
10182
545fd46b
MR
10183/* Handle relocations against symbols from removed linkonce sections,
10184 or sections discarded by a linker script. We use this wrapper around
10185 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10186 on 64-bit ELF targets. In this case for any relocation handled, which
10187 always be the first in a triplet, the remaining two have to be processed
10188 together with the first, even if they are R_MIPS_NONE. It is the symbol
10189 index referred by the first reloc that applies to all the three and the
10190 remaining two never refer to an object symbol. And it is the final
10191 relocation (the last non-null one) that determines the output field of
10192 the whole relocation so retrieve the corresponding howto structure for
10193 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10194
10195 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10196 and therefore requires to be pasted in a loop. It also defines a block
10197 and does not protect any of its arguments, hence the extra brackets. */
10198
10199static void
10200mips_reloc_against_discarded_section (bfd *output_bfd,
10201 struct bfd_link_info *info,
10202 bfd *input_bfd, asection *input_section,
10203 Elf_Internal_Rela **rel,
10204 const Elf_Internal_Rela **relend,
10205 bfd_boolean rel_reloc,
10206 reloc_howto_type *howto,
10207 bfd_byte *contents)
10208{
10209 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10210 int count = bed->s->int_rels_per_ext_rel;
10211 unsigned int r_type;
10212 int i;
10213
10214 for (i = count - 1; i > 0; i--)
10215 {
10216 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10217 if (r_type != R_MIPS_NONE)
10218 {
10219 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10220 break;
10221 }
10222 }
10223 do
10224 {
10225 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10226 (*rel), count, (*relend),
10227 howto, i, contents);
10228 }
10229 while (0);
10230}
10231
b49e97c9
TS
10232/* Relocate a MIPS ELF section. */
10233
b34976b6 10234bfd_boolean
9719ad41
RS
10235_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10236 bfd *input_bfd, asection *input_section,
10237 bfd_byte *contents, Elf_Internal_Rela *relocs,
10238 Elf_Internal_Sym *local_syms,
10239 asection **local_sections)
b49e97c9
TS
10240{
10241 Elf_Internal_Rela *rel;
10242 const Elf_Internal_Rela *relend;
10243 bfd_vma addend = 0;
b34976b6 10244 bfd_boolean use_saved_addend_p = FALSE;
b49e97c9 10245
056bafd4 10246 relend = relocs + input_section->reloc_count;
b49e97c9
TS
10247 for (rel = relocs; rel < relend; ++rel)
10248 {
10249 const char *name;
c9adbffe 10250 bfd_vma value = 0;
b49e97c9 10251 reloc_howto_type *howto;
ad3d9127 10252 bfd_boolean cross_mode_jump_p = FALSE;
b34976b6 10253 /* TRUE if the relocation is a RELA relocation, rather than a
07d6d2b8 10254 REL relocation. */
b34976b6 10255 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 10256 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 10257 const char *msg;
ab96bf03
AM
10258 unsigned long r_symndx;
10259 asection *sec;
749b8d9d
L
10260 Elf_Internal_Shdr *symtab_hdr;
10261 struct elf_link_hash_entry *h;
d4730f92 10262 bfd_boolean rel_reloc;
b49e97c9 10263
d4730f92
BS
10264 rel_reloc = (NEWABI_P (input_bfd)
10265 && mips_elf_rel_relocation_p (input_bfd, input_section,
10266 relocs, rel));
b49e97c9 10267 /* Find the relocation howto for this relocation. */
d4730f92 10268 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
ab96bf03
AM
10269
10270 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 10271 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
020d7251 10272 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
749b8d9d
L
10273 {
10274 sec = local_sections[r_symndx];
10275 h = NULL;
10276 }
ab96bf03
AM
10277 else
10278 {
ab96bf03 10279 unsigned long extsymoff;
ab96bf03 10280
ab96bf03
AM
10281 extsymoff = 0;
10282 if (!elf_bad_symtab (input_bfd))
10283 extsymoff = symtab_hdr->sh_info;
10284 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10285 while (h->root.type == bfd_link_hash_indirect
10286 || h->root.type == bfd_link_hash_warning)
10287 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10288
10289 sec = NULL;
10290 if (h->root.type == bfd_link_hash_defined
10291 || h->root.type == bfd_link_hash_defweak)
10292 sec = h->root.u.def.section;
10293 }
10294
dbaa2011 10295 if (sec != NULL && discarded_section (sec))
545fd46b
MR
10296 {
10297 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10298 input_section, &rel, &relend,
10299 rel_reloc, howto, contents);
10300 continue;
10301 }
ab96bf03 10302
4a14403c 10303 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
10304 {
10305 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10306 64-bit code, but make sure all their addresses are in the
10307 lowermost or uppermost 32-bit section of the 64-bit address
10308 space. Thus, when they use an R_MIPS_64 they mean what is
10309 usually meant by R_MIPS_32, with the exception that the
10310 stored value is sign-extended to 64 bits. */
b34976b6 10311 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
10312
10313 /* On big-endian systems, we need to lie about the position
10314 of the reloc. */
10315 if (bfd_big_endian (input_bfd))
10316 rel->r_offset += 4;
10317 }
b49e97c9
TS
10318
10319 if (!use_saved_addend_p)
10320 {
b49e97c9
TS
10321 /* If these relocations were originally of the REL variety,
10322 we must pull the addend out of the field that will be
10323 relocated. Otherwise, we simply use the contents of the
c224138d
RS
10324 RELA relocation. */
10325 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10326 relocs, rel))
b49e97c9 10327 {
b34976b6 10328 rela_relocation_p = FALSE;
c224138d
RS
10329 addend = mips_elf_read_rel_addend (input_bfd, rel,
10330 howto, contents);
738e5348
RS
10331 if (hi16_reloc_p (r_type)
10332 || (got16_reloc_p (r_type)
b49e97c9 10333 && mips_elf_local_relocation_p (input_bfd, rel,
020d7251 10334 local_sections)))
b49e97c9 10335 {
c224138d
RS
10336 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
10337 contents, &addend))
749b8d9d 10338 {
749b8d9d
L
10339 if (h)
10340 name = h->root.root.string;
10341 else
10342 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10343 local_syms + r_symndx,
10344 sec);
4eca0228 10345 _bfd_error_handler
695344c0 10346 /* xgettext:c-format */
2c1c9679 10347 (_("%pB: can't find matching LO16 reloc against `%s'"
2dcf00ce 10348 " for %s at %#" PRIx64 " in section `%pA'"),
c08bb8dd 10349 input_bfd, name,
2dcf00ce 10350 howto->name, (uint64_t) rel->r_offset, input_section);
749b8d9d 10351 }
b49e97c9 10352 }
30ac9238
RS
10353 else
10354 addend <<= howto->rightshift;
b49e97c9
TS
10355 }
10356 else
10357 addend = rel->r_addend;
81d43bff
RS
10358 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10359 local_syms, local_sections, rel);
b49e97c9
TS
10360 }
10361
0e1862bb 10362 if (bfd_link_relocatable (info))
b49e97c9 10363 {
4a14403c 10364 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
10365 && bfd_big_endian (input_bfd))
10366 rel->r_offset -= 4;
10367
81d43bff 10368 if (!rela_relocation_p && rel->r_addend)
5a659663 10369 {
81d43bff 10370 addend += rel->r_addend;
738e5348 10371 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
10372 addend = mips_elf_high (addend);
10373 else if (r_type == R_MIPS_HIGHER)
10374 addend = mips_elf_higher (addend);
10375 else if (r_type == R_MIPS_HIGHEST)
10376 addend = mips_elf_highest (addend);
30ac9238
RS
10377 else
10378 addend >>= howto->rightshift;
b49e97c9 10379
30ac9238
RS
10380 /* We use the source mask, rather than the destination
10381 mask because the place to which we are writing will be
10382 source of the addend in the final link. */
b49e97c9
TS
10383 addend &= howto->src_mask;
10384
5a659663 10385 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10386 /* See the comment above about using R_MIPS_64 in the 32-bit
10387 ABI. Here, we need to update the addend. It would be
10388 possible to get away with just using the R_MIPS_32 reloc
10389 but for endianness. */
10390 {
10391 bfd_vma sign_bits;
10392 bfd_vma low_bits;
10393 bfd_vma high_bits;
10394
10395 if (addend & ((bfd_vma) 1 << 31))
10396#ifdef BFD64
10397 sign_bits = ((bfd_vma) 1 << 32) - 1;
10398#else
10399 sign_bits = -1;
10400#endif
10401 else
10402 sign_bits = 0;
10403
10404 /* If we don't know that we have a 64-bit type,
10405 do two separate stores. */
10406 if (bfd_big_endian (input_bfd))
10407 {
10408 /* Store the sign-bits (which are most significant)
10409 first. */
10410 low_bits = sign_bits;
10411 high_bits = addend;
10412 }
10413 else
10414 {
10415 low_bits = addend;
10416 high_bits = sign_bits;
10417 }
10418 bfd_put_32 (input_bfd, low_bits,
10419 contents + rel->r_offset);
10420 bfd_put_32 (input_bfd, high_bits,
10421 contents + rel->r_offset + 4);
10422 continue;
10423 }
10424
10425 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10426 input_bfd, input_section,
b34976b6
AM
10427 contents, FALSE))
10428 return FALSE;
b49e97c9
TS
10429 }
10430
10431 /* Go on to the next relocation. */
10432 continue;
10433 }
10434
10435 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10436 relocations for the same offset. In that case we are
10437 supposed to treat the output of each relocation as the addend
10438 for the next. */
10439 if (rel + 1 < relend
10440 && rel->r_offset == rel[1].r_offset
10441 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 10442 use_saved_addend_p = TRUE;
b49e97c9 10443 else
b34976b6 10444 use_saved_addend_p = FALSE;
b49e97c9
TS
10445
10446 /* Figure out what value we are supposed to relocate. */
10447 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
47275900
MR
10448 input_section, contents,
10449 info, rel, addend, howto,
10450 local_syms, local_sections,
10451 &value, &name, &cross_mode_jump_p,
bce03d3d 10452 use_saved_addend_p))
b49e97c9
TS
10453 {
10454 case bfd_reloc_continue:
10455 /* There's nothing to do. */
10456 continue;
10457
10458 case bfd_reloc_undefined:
10459 /* mips_elf_calculate_relocation already called the
10460 undefined_symbol callback. There's no real point in
10461 trying to perform the relocation at this point, so we
10462 just skip ahead to the next relocation. */
10463 continue;
10464
10465 case bfd_reloc_notsupported:
10466 msg = _("internal error: unsupported relocation error");
10467 info->callbacks->warning
10468 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 10469 return FALSE;
b49e97c9
TS
10470
10471 case bfd_reloc_overflow:
10472 if (use_saved_addend_p)
10473 /* Ignore overflow until we reach the last relocation for
10474 a given location. */
10475 ;
10476 else
10477 {
0e53d9da
AN
10478 struct mips_elf_link_hash_table *htab;
10479
10480 htab = mips_elf_hash_table (info);
4dfe6ac6 10481 BFD_ASSERT (htab != NULL);
b49e97c9 10482 BFD_ASSERT (name != NULL);
0e53d9da 10483 if (!htab->small_data_overflow_reported
9684f078 10484 && (gprel16_reloc_p (howto->type)
df58fc94 10485 || literal_reloc_p (howto->type)))
0e53d9da 10486 {
91d6fa6a
NC
10487 msg = _("small-data section exceeds 64KB;"
10488 " lower small-data size limit (see option -G)");
0e53d9da
AN
10489
10490 htab->small_data_overflow_reported = TRUE;
10491 (*info->callbacks->einfo) ("%P: %s\n", msg);
10492 }
1a72702b
AM
10493 (*info->callbacks->reloc_overflow)
10494 (info, NULL, name, howto->name, (bfd_vma) 0,
10495 input_bfd, input_section, rel->r_offset);
b49e97c9
TS
10496 }
10497 break;
10498
10499 case bfd_reloc_ok:
10500 break;
10501
df58fc94 10502 case bfd_reloc_outofrange:
7db9a74e 10503 msg = NULL;
df58fc94 10504 if (jal_reloc_p (howto->type))
9d862524 10505 msg = (cross_mode_jump_p
2c1c9679 10506 ? _("cannot convert a jump to JALX "
9d862524
MR
10507 "for a non-word-aligned address")
10508 : (howto->type == R_MIPS16_26
2c1c9679
AM
10509 ? _("jump to a non-word-aligned address")
10510 : _("jump to a non-instruction-aligned address")));
99aefae6 10511 else if (b_reloc_p (howto->type))
a6ebf616 10512 msg = (cross_mode_jump_p
2c1c9679 10513 ? _("cannot convert a branch to JALX "
a6ebf616 10514 "for a non-word-aligned address")
2c1c9679 10515 : _("branch to a non-instruction-aligned address"));
7db9a74e
MR
10516 else if (aligned_pcrel_reloc_p (howto->type))
10517 msg = _("PC-relative load from unaligned address");
10518 if (msg)
df58fc94 10519 {
de341542 10520 info->callbacks->einfo
ed53407e
MR
10521 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10522 break;
7361da2c 10523 }
df58fc94
RS
10524 /* Fall through. */
10525
b49e97c9
TS
10526 default:
10527 abort ();
10528 break;
10529 }
10530
10531 /* If we've got another relocation for the address, keep going
10532 until we reach the last one. */
10533 if (use_saved_addend_p)
10534 {
10535 addend = value;
10536 continue;
10537 }
10538
4a14403c 10539 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10540 /* See the comment above about using R_MIPS_64 in the 32-bit
10541 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10542 that calculated the right value. Now, however, we
10543 sign-extend the 32-bit result to 64-bits, and store it as a
10544 64-bit value. We are especially generous here in that we
10545 go to extreme lengths to support this usage on systems with
10546 only a 32-bit VMA. */
10547 {
10548 bfd_vma sign_bits;
10549 bfd_vma low_bits;
10550 bfd_vma high_bits;
10551
10552 if (value & ((bfd_vma) 1 << 31))
10553#ifdef BFD64
10554 sign_bits = ((bfd_vma) 1 << 32) - 1;
10555#else
10556 sign_bits = -1;
10557#endif
10558 else
10559 sign_bits = 0;
10560
10561 /* If we don't know that we have a 64-bit type,
10562 do two separate stores. */
10563 if (bfd_big_endian (input_bfd))
10564 {
10565 /* Undo what we did above. */
10566 rel->r_offset -= 4;
10567 /* Store the sign-bits (which are most significant)
10568 first. */
10569 low_bits = sign_bits;
10570 high_bits = value;
10571 }
10572 else
10573 {
10574 low_bits = value;
10575 high_bits = sign_bits;
10576 }
10577 bfd_put_32 (input_bfd, low_bits,
10578 contents + rel->r_offset);
10579 bfd_put_32 (input_bfd, high_bits,
10580 contents + rel->r_offset + 4);
10581 continue;
10582 }
10583
10584 /* Actually perform the relocation. */
10585 if (! mips_elf_perform_relocation (info, howto, rel, value,
10586 input_bfd, input_section,
38a7df63 10587 contents, cross_mode_jump_p))
b34976b6 10588 return FALSE;
b49e97c9
TS
10589 }
10590
b34976b6 10591 return TRUE;
b49e97c9
TS
10592}
10593\f
861fb55a
DJ
10594/* A function that iterates over each entry in la25_stubs and fills
10595 in the code for each one. DATA points to a mips_htab_traverse_info. */
10596
10597static int
10598mips_elf_create_la25_stub (void **slot, void *data)
10599{
10600 struct mips_htab_traverse_info *hti;
10601 struct mips_elf_link_hash_table *htab;
10602 struct mips_elf_la25_stub *stub;
10603 asection *s;
10604 bfd_byte *loc;
10605 bfd_vma offset, target, target_high, target_low;
10606
10607 stub = (struct mips_elf_la25_stub *) *slot;
10608 hti = (struct mips_htab_traverse_info *) data;
10609 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 10610 BFD_ASSERT (htab != NULL);
861fb55a
DJ
10611
10612 /* Create the section contents, if we haven't already. */
10613 s = stub->stub_section;
10614 loc = s->contents;
10615 if (loc == NULL)
10616 {
10617 loc = bfd_malloc (s->size);
10618 if (loc == NULL)
10619 {
10620 hti->error = TRUE;
10621 return FALSE;
10622 }
10623 s->contents = loc;
10624 }
10625
10626 /* Work out where in the section this stub should go. */
10627 offset = stub->offset;
10628
10629 /* Work out the target address. */
8f0c309a
CLT
10630 target = mips_elf_get_la25_target (stub, &s);
10631 target += s->output_section->vma + s->output_offset;
10632
861fb55a
DJ
10633 target_high = ((target + 0x8000) >> 16) & 0xffff;
10634 target_low = (target & 0xffff);
10635
10636 if (stub->stub_section != htab->strampoline)
10637 {
df58fc94 10638 /* This is a simple LUI/ADDIU stub. Zero out the beginning
861fb55a
DJ
10639 of the section and write the two instructions at the end. */
10640 memset (loc, 0, offset);
10641 loc += offset;
df58fc94
RS
10642 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10643 {
d21911ea
MR
10644 bfd_put_micromips_32 (hti->output_bfd,
10645 LA25_LUI_MICROMIPS (target_high),
10646 loc);
10647 bfd_put_micromips_32 (hti->output_bfd,
10648 LA25_ADDIU_MICROMIPS (target_low),
10649 loc + 4);
df58fc94
RS
10650 }
10651 else
10652 {
10653 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10654 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10655 }
861fb55a
DJ
10656 }
10657 else
10658 {
10659 /* This is trampoline. */
10660 loc += offset;
df58fc94
RS
10661 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10662 {
d21911ea
MR
10663 bfd_put_micromips_32 (hti->output_bfd,
10664 LA25_LUI_MICROMIPS (target_high), loc);
10665 bfd_put_micromips_32 (hti->output_bfd,
10666 LA25_J_MICROMIPS (target), loc + 4);
10667 bfd_put_micromips_32 (hti->output_bfd,
10668 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
df58fc94
RS
10669 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10670 }
10671 else
10672 {
10673 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10674 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10675 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10676 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10677 }
861fb55a
DJ
10678 }
10679 return TRUE;
10680}
10681
b49e97c9
TS
10682/* If NAME is one of the special IRIX6 symbols defined by the linker,
10683 adjust it appropriately now. */
10684
10685static void
9719ad41
RS
10686mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10687 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
10688{
10689 /* The linker script takes care of providing names and values for
10690 these, but we must place them into the right sections. */
10691 static const char* const text_section_symbols[] = {
10692 "_ftext",
10693 "_etext",
10694 "__dso_displacement",
10695 "__elf_header",
10696 "__program_header_table",
10697 NULL
10698 };
10699
10700 static const char* const data_section_symbols[] = {
10701 "_fdata",
10702 "_edata",
10703 "_end",
10704 "_fbss",
10705 NULL
10706 };
10707
10708 const char* const *p;
10709 int i;
10710
10711 for (i = 0; i < 2; ++i)
10712 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10713 *p;
10714 ++p)
10715 if (strcmp (*p, name) == 0)
10716 {
10717 /* All of these symbols are given type STT_SECTION by the
10718 IRIX6 linker. */
10719 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 10720 sym->st_other = STO_PROTECTED;
b49e97c9
TS
10721
10722 /* The IRIX linker puts these symbols in special sections. */
10723 if (i == 0)
10724 sym->st_shndx = SHN_MIPS_TEXT;
10725 else
10726 sym->st_shndx = SHN_MIPS_DATA;
10727
10728 break;
10729 }
10730}
10731
10732/* Finish up dynamic symbol handling. We set the contents of various
10733 dynamic sections here. */
10734
b34976b6 10735bfd_boolean
9719ad41
RS
10736_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10737 struct bfd_link_info *info,
10738 struct elf_link_hash_entry *h,
10739 Elf_Internal_Sym *sym)
b49e97c9
TS
10740{
10741 bfd *dynobj;
b49e97c9 10742 asection *sgot;
f4416af6 10743 struct mips_got_info *g, *gg;
b49e97c9 10744 const char *name;
3d6746ca 10745 int idx;
5108fc1b 10746 struct mips_elf_link_hash_table *htab;
738e5348 10747 struct mips_elf_link_hash_entry *hmips;
b49e97c9 10748
5108fc1b 10749 htab = mips_elf_hash_table (info);
4dfe6ac6 10750 BFD_ASSERT (htab != NULL);
b49e97c9 10751 dynobj = elf_hash_table (info)->dynobj;
738e5348 10752 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 10753
861fb55a
DJ
10754 BFD_ASSERT (!htab->is_vxworks);
10755
1bbce132
MR
10756 if (h->plt.plist != NULL
10757 && (h->plt.plist->mips_offset != MINUS_ONE
10758 || h->plt.plist->comp_offset != MINUS_ONE))
861fb55a
DJ
10759 {
10760 /* We've decided to create a PLT entry for this symbol. */
10761 bfd_byte *loc;
1bbce132 10762 bfd_vma header_address, got_address;
861fb55a 10763 bfd_vma got_address_high, got_address_low, load;
1bbce132
MR
10764 bfd_vma got_index;
10765 bfd_vma isa_bit;
10766
10767 got_index = h->plt.plist->gotplt_index;
861fb55a
DJ
10768
10769 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10770 BFD_ASSERT (h->dynindx != -1);
ce558b89 10771 BFD_ASSERT (htab->root.splt != NULL);
1bbce132 10772 BFD_ASSERT (got_index != MINUS_ONE);
861fb55a
DJ
10773 BFD_ASSERT (!h->def_regular);
10774
10775 /* Calculate the address of the PLT header. */
1bbce132 10776 isa_bit = htab->plt_header_is_comp;
ce558b89
AM
10777 header_address = (htab->root.splt->output_section->vma
10778 + htab->root.splt->output_offset + isa_bit);
861fb55a
DJ
10779
10780 /* Calculate the address of the .got.plt entry. */
ce558b89
AM
10781 got_address = (htab->root.sgotplt->output_section->vma
10782 + htab->root.sgotplt->output_offset
1bbce132
MR
10783 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10784
861fb55a
DJ
10785 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10786 got_address_low = got_address & 0xffff;
10787
789ff5b6
MR
10788 /* The PLT sequence is not safe for N64 if .got.plt entry's address
10789 cannot be loaded in two instructions. */
10790 if (ABI_64_P (output_bfd)
10791 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
10792 {
10793 _bfd_error_handler
10794 /* xgettext:c-format */
10795 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
10796 "supported; consider using `-Ttext-segment=...'"),
10797 output_bfd,
10798 htab->root.sgotplt->output_section,
10799 (int64_t) got_address);
10800 bfd_set_error (bfd_error_no_error);
10801 return FALSE;
10802 }
10803
861fb55a 10804 /* Initially point the .got.plt entry at the PLT header. */
6a382bce
MR
10805 loc = (htab->root.sgotplt->contents
10806 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
861fb55a
DJ
10807 if (ABI_64_P (output_bfd))
10808 bfd_put_64 (output_bfd, header_address, loc);
10809 else
10810 bfd_put_32 (output_bfd, header_address, loc);
10811
1bbce132 10812 /* Now handle the PLT itself. First the standard entry (the order
07d6d2b8 10813 does not matter, we just have to pick one). */
1bbce132
MR
10814 if (h->plt.plist->mips_offset != MINUS_ONE)
10815 {
10816 const bfd_vma *plt_entry;
10817 bfd_vma plt_offset;
861fb55a 10818
1bbce132 10819 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
861fb55a 10820
ce558b89 10821 BFD_ASSERT (plt_offset <= htab->root.splt->size);
6d30f5b2 10822
1bbce132 10823 /* Find out where the .plt entry should go. */
ce558b89 10824 loc = htab->root.splt->contents + plt_offset;
1bbce132
MR
10825
10826 /* Pick the load opcode. */
10827 load = MIPS_ELF_LOAD_WORD (output_bfd);
10828
10829 /* Fill in the PLT entry itself. */
7361da2c
AB
10830
10831 if (MIPSR6_P (output_bfd))
10832 plt_entry = mipsr6_exec_plt_entry;
10833 else
10834 plt_entry = mips_exec_plt_entry;
1bbce132
MR
10835 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10836 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10837 loc + 4);
10838
10839 if (! LOAD_INTERLOCKS_P (output_bfd))
10840 {
10841 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10842 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10843 }
10844 else
10845 {
10846 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10847 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10848 loc + 12);
10849 }
6d30f5b2 10850 }
1bbce132
MR
10851
10852 /* Now the compressed entry. They come after any standard ones. */
10853 if (h->plt.plist->comp_offset != MINUS_ONE)
6d30f5b2 10854 {
1bbce132
MR
10855 bfd_vma plt_offset;
10856
10857 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10858 + h->plt.plist->comp_offset);
10859
ce558b89 10860 BFD_ASSERT (plt_offset <= htab->root.splt->size);
1bbce132
MR
10861
10862 /* Find out where the .plt entry should go. */
ce558b89 10863 loc = htab->root.splt->contents + plt_offset;
1bbce132
MR
10864
10865 /* Fill in the PLT entry itself. */
833794fc
MR
10866 if (!MICROMIPS_P (output_bfd))
10867 {
10868 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
10869
10870 bfd_put_16 (output_bfd, plt_entry[0], loc);
10871 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
10872 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10873 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10874 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10875 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10876 bfd_put_32 (output_bfd, got_address, loc + 12);
10877 }
10878 else if (htab->insn32)
10879 {
10880 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
10881
10882 bfd_put_16 (output_bfd, plt_entry[0], loc);
10883 bfd_put_16 (output_bfd, got_address_high, loc + 2);
10884 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10885 bfd_put_16 (output_bfd, got_address_low, loc + 6);
10886 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10887 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10888 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
10889 bfd_put_16 (output_bfd, got_address_low, loc + 14);
10890 }
10891 else
1bbce132
MR
10892 {
10893 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
10894 bfd_signed_vma gotpc_offset;
10895 bfd_vma loc_address;
10896
10897 BFD_ASSERT (got_address % 4 == 0);
10898
ce558b89
AM
10899 loc_address = (htab->root.splt->output_section->vma
10900 + htab->root.splt->output_offset + plt_offset);
1bbce132
MR
10901 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
10902
10903 /* ADDIUPC has a span of +/-16MB, check we're in range. */
10904 if (gotpc_offset + 0x1000000 >= 0x2000000)
10905 {
4eca0228 10906 _bfd_error_handler
695344c0 10907 /* xgettext:c-format */
2dcf00ce 10908 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
1bbce132
MR
10909 "beyond the range of ADDIUPC"),
10910 output_bfd,
ce558b89 10911 htab->root.sgotplt->output_section,
2dcf00ce 10912 (int64_t) gotpc_offset,
c08bb8dd 10913 htab->root.splt->output_section);
1bbce132
MR
10914 bfd_set_error (bfd_error_no_error);
10915 return FALSE;
10916 }
10917 bfd_put_16 (output_bfd,
10918 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
10919 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
10920 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
10921 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
10922 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
10923 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
10924 }
6d30f5b2 10925 }
861fb55a
DJ
10926
10927 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
ce558b89 10928 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
1bbce132 10929 got_index - 2, h->dynindx,
861fb55a
DJ
10930 R_MIPS_JUMP_SLOT, got_address);
10931
10932 /* We distinguish between PLT entries and lazy-binding stubs by
10933 giving the former an st_other value of STO_MIPS_PLT. Set the
10934 flag and leave the value if there are any relocations in the
10935 binary where pointer equality matters. */
10936 sym->st_shndx = SHN_UNDEF;
10937 if (h->pointer_equality_needed)
1bbce132 10938 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
861fb55a 10939 else
1bbce132
MR
10940 {
10941 sym->st_value = 0;
10942 sym->st_other = 0;
10943 }
861fb55a 10944 }
1bbce132
MR
10945
10946 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
b49e97c9 10947 {
861fb55a 10948 /* We've decided to create a lazy-binding stub. */
1bbce132
MR
10949 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
10950 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10951 bfd_vma stub_size = htab->function_stub_size;
5108fc1b 10952 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
1bbce132
MR
10953 bfd_vma isa_bit = micromips_p;
10954 bfd_vma stub_big_size;
10955
833794fc 10956 if (!micromips_p)
1bbce132 10957 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
833794fc
MR
10958 else if (htab->insn32)
10959 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
10960 else
10961 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
b49e97c9
TS
10962
10963 /* This symbol has a stub. Set it up. */
10964
10965 BFD_ASSERT (h->dynindx != -1);
10966
1bbce132 10967 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
3d6746ca
DD
10968
10969 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
10970 sign extension at runtime in the stub, resulting in a negative
10971 index value. */
10972 if (h->dynindx & ~0x7fffffff)
b34976b6 10973 return FALSE;
b49e97c9
TS
10974
10975 /* Fill the stub. */
1bbce132
MR
10976 if (micromips_p)
10977 {
10978 idx = 0;
10979 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
10980 stub + idx);
10981 idx += 4;
833794fc
MR
10982 if (htab->insn32)
10983 {
10984 bfd_put_micromips_32 (output_bfd,
40fc1451 10985 STUB_MOVE32_MICROMIPS, stub + idx);
833794fc
MR
10986 idx += 4;
10987 }
10988 else
10989 {
10990 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
10991 idx += 2;
10992 }
1bbce132
MR
10993 if (stub_size == stub_big_size)
10994 {
10995 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
10996
10997 bfd_put_micromips_32 (output_bfd,
10998 STUB_LUI_MICROMIPS (dynindx_hi),
10999 stub + idx);
11000 idx += 4;
11001 }
833794fc
MR
11002 if (htab->insn32)
11003 {
11004 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11005 stub + idx);
11006 idx += 4;
11007 }
11008 else
11009 {
11010 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11011 idx += 2;
11012 }
1bbce132
MR
11013
11014 /* If a large stub is not required and sign extension is not a
11015 problem, then use legacy code in the stub. */
11016 if (stub_size == stub_big_size)
11017 bfd_put_micromips_32 (output_bfd,
11018 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11019 stub + idx);
11020 else if (h->dynindx & ~0x7fff)
11021 bfd_put_micromips_32 (output_bfd,
11022 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11023 stub + idx);
11024 else
11025 bfd_put_micromips_32 (output_bfd,
11026 STUB_LI16S_MICROMIPS (output_bfd,
11027 h->dynindx),
11028 stub + idx);
11029 }
3d6746ca 11030 else
1bbce132
MR
11031 {
11032 idx = 0;
11033 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11034 idx += 4;
40fc1451 11035 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
1bbce132
MR
11036 idx += 4;
11037 if (stub_size == stub_big_size)
11038 {
11039 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11040 stub + idx);
11041 idx += 4;
11042 }
11043 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11044 idx += 4;
11045
11046 /* If a large stub is not required and sign extension is not a
11047 problem, then use legacy code in the stub. */
11048 if (stub_size == stub_big_size)
11049 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11050 stub + idx);
11051 else if (h->dynindx & ~0x7fff)
11052 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11053 stub + idx);
11054 else
11055 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11056 stub + idx);
11057 }
5108fc1b 11058
1bbce132
MR
11059 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11060 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11061 stub, stub_size);
b49e97c9 11062
1bbce132 11063 /* Mark the symbol as undefined. stub_offset != -1 occurs
b49e97c9
TS
11064 only for the referenced symbol. */
11065 sym->st_shndx = SHN_UNDEF;
11066
11067 /* The run-time linker uses the st_value field of the symbol
11068 to reset the global offset table entry for this external
11069 to its stub address when unlinking a shared object. */
4e41d0d7
RS
11070 sym->st_value = (htab->sstubs->output_section->vma
11071 + htab->sstubs->output_offset
1bbce132
MR
11072 + h->plt.plist->stub_offset
11073 + isa_bit);
11074 sym->st_other = other;
b49e97c9
TS
11075 }
11076
738e5348
RS
11077 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11078 refer to the stub, since only the stub uses the standard calling
11079 conventions. */
11080 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11081 {
11082 BFD_ASSERT (hmips->need_fn_stub);
11083 sym->st_value = (hmips->fn_stub->output_section->vma
11084 + hmips->fn_stub->output_offset);
11085 sym->st_size = hmips->fn_stub->size;
11086 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11087 }
11088
b49e97c9 11089 BFD_ASSERT (h->dynindx != -1
f5385ebf 11090 || h->forced_local);
b49e97c9 11091
ce558b89 11092 sgot = htab->root.sgot;
a8028dd0 11093 g = htab->got_info;
b49e97c9
TS
11094 BFD_ASSERT (g != NULL);
11095
11096 /* Run through the global symbol table, creating GOT entries for all
11097 the symbols that need them. */
020d7251 11098 if (hmips->global_got_area != GGA_NONE)
b49e97c9
TS
11099 {
11100 bfd_vma offset;
11101 bfd_vma value;
11102
6eaa6adc 11103 value = sym->st_value;
13fbec83 11104 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
b49e97c9
TS
11105 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11106 }
11107
e641e783 11108 if (hmips->global_got_area != GGA_NONE && g->next)
f4416af6
AO
11109 {
11110 struct mips_got_entry e, *p;
0626d451 11111 bfd_vma entry;
f4416af6 11112 bfd_vma offset;
f4416af6
AO
11113
11114 gg = g;
11115
11116 e.abfd = output_bfd;
11117 e.symndx = -1;
738e5348 11118 e.d.h = hmips;
9ab066b4 11119 e.tls_type = GOT_TLS_NONE;
143d77c5 11120
f4416af6
AO
11121 for (g = g->next; g->next != gg; g = g->next)
11122 {
11123 if (g->got_entries
11124 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11125 &e)))
11126 {
11127 offset = p->gotidx;
ce558b89 11128 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
0e1862bb 11129 if (bfd_link_pic (info)
0626d451
RS
11130 || (elf_hash_table (info)->dynamic_sections_created
11131 && p->d.h != NULL
f5385ebf
AM
11132 && p->d.h->root.def_dynamic
11133 && !p->d.h->root.def_regular))
0626d451
RS
11134 {
11135 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11136 the various compatibility problems, it's easier to mock
11137 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11138 mips_elf_create_dynamic_relocation to calculate the
11139 appropriate addend. */
11140 Elf_Internal_Rela rel[3];
11141
11142 memset (rel, 0, sizeof (rel));
11143 if (ABI_64_P (output_bfd))
11144 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11145 else
11146 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11147 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11148
11149 entry = 0;
11150 if (! (mips_elf_create_dynamic_relocation
11151 (output_bfd, info, rel,
11152 e.d.h, NULL, sym->st_value, &entry, sgot)))
11153 return FALSE;
11154 }
11155 else
11156 entry = sym->st_value;
11157 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
11158 }
11159 }
11160 }
11161
b49e97c9
TS
11162 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11163 name = h->root.root.string;
9637f6ef 11164 if (h == elf_hash_table (info)->hdynamic
22edb2f1 11165 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
11166 sym->st_shndx = SHN_ABS;
11167 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11168 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11169 {
11170 sym->st_shndx = SHN_ABS;
11171 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11172 sym->st_value = 1;
11173 }
b49e97c9
TS
11174 else if (SGI_COMPAT (output_bfd))
11175 {
11176 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11177 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11178 {
11179 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11180 sym->st_other = STO_PROTECTED;
11181 sym->st_value = 0;
11182 sym->st_shndx = SHN_MIPS_DATA;
11183 }
11184 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11185 {
11186 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11187 sym->st_other = STO_PROTECTED;
11188 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11189 sym->st_shndx = SHN_ABS;
11190 }
11191 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11192 {
11193 if (h->type == STT_FUNC)
11194 sym->st_shndx = SHN_MIPS_TEXT;
11195 else if (h->type == STT_OBJECT)
11196 sym->st_shndx = SHN_MIPS_DATA;
11197 }
11198 }
11199
861fb55a
DJ
11200 /* Emit a copy reloc, if needed. */
11201 if (h->needs_copy)
11202 {
11203 asection *s;
11204 bfd_vma symval;
11205
11206 BFD_ASSERT (h->dynindx != -1);
11207 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11208
11209 s = mips_elf_rel_dyn_section (info, FALSE);
11210 symval = (h->root.u.def.section->output_section->vma
11211 + h->root.u.def.section->output_offset
11212 + h->root.u.def.value);
11213 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11214 h->dynindx, R_MIPS_COPY, symval);
11215 }
11216
b49e97c9
TS
11217 /* Handle the IRIX6-specific symbols. */
11218 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11219 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11220
cbf8d970
MR
11221 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11222 to treat compressed symbols like any other. */
30c09090 11223 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
11224 {
11225 BFD_ASSERT (sym->st_value & 1);
11226 sym->st_other -= STO_MIPS16;
11227 }
cbf8d970
MR
11228 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11229 {
11230 BFD_ASSERT (sym->st_value & 1);
11231 sym->st_other -= STO_MICROMIPS;
11232 }
b49e97c9 11233
b34976b6 11234 return TRUE;
b49e97c9
TS
11235}
11236
0a44bf69
RS
11237/* Likewise, for VxWorks. */
11238
11239bfd_boolean
11240_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11241 struct bfd_link_info *info,
11242 struct elf_link_hash_entry *h,
11243 Elf_Internal_Sym *sym)
11244{
11245 bfd *dynobj;
11246 asection *sgot;
11247 struct mips_got_info *g;
11248 struct mips_elf_link_hash_table *htab;
020d7251 11249 struct mips_elf_link_hash_entry *hmips;
0a44bf69
RS
11250
11251 htab = mips_elf_hash_table (info);
4dfe6ac6 11252 BFD_ASSERT (htab != NULL);
0a44bf69 11253 dynobj = elf_hash_table (info)->dynobj;
020d7251 11254 hmips = (struct mips_elf_link_hash_entry *) h;
0a44bf69 11255
1bbce132 11256 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
0a44bf69 11257 {
6d79d2ed 11258 bfd_byte *loc;
1bbce132 11259 bfd_vma plt_address, got_address, got_offset, branch_offset;
0a44bf69
RS
11260 Elf_Internal_Rela rel;
11261 static const bfd_vma *plt_entry;
1bbce132
MR
11262 bfd_vma gotplt_index;
11263 bfd_vma plt_offset;
11264
11265 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11266 gotplt_index = h->plt.plist->gotplt_index;
0a44bf69
RS
11267
11268 BFD_ASSERT (h->dynindx != -1);
ce558b89 11269 BFD_ASSERT (htab->root.splt != NULL);
1bbce132 11270 BFD_ASSERT (gotplt_index != MINUS_ONE);
ce558b89 11271 BFD_ASSERT (plt_offset <= htab->root.splt->size);
0a44bf69
RS
11272
11273 /* Calculate the address of the .plt entry. */
ce558b89
AM
11274 plt_address = (htab->root.splt->output_section->vma
11275 + htab->root.splt->output_offset
1bbce132 11276 + plt_offset);
0a44bf69
RS
11277
11278 /* Calculate the address of the .got.plt entry. */
ce558b89
AM
11279 got_address = (htab->root.sgotplt->output_section->vma
11280 + htab->root.sgotplt->output_offset
1bbce132 11281 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
0a44bf69
RS
11282
11283 /* Calculate the offset of the .got.plt entry from
11284 _GLOBAL_OFFSET_TABLE_. */
11285 got_offset = mips_elf_gotplt_index (info, h);
11286
11287 /* Calculate the offset for the branch at the start of the PLT
11288 entry. The branch jumps to the beginning of .plt. */
1bbce132 11289 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
0a44bf69
RS
11290
11291 /* Fill in the initial value of the .got.plt entry. */
11292 bfd_put_32 (output_bfd, plt_address,
ce558b89 11293 (htab->root.sgotplt->contents
1bbce132 11294 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
0a44bf69
RS
11295
11296 /* Find out where the .plt entry should go. */
ce558b89 11297 loc = htab->root.splt->contents + plt_offset;
0a44bf69 11298
0e1862bb 11299 if (bfd_link_pic (info))
0a44bf69
RS
11300 {
11301 plt_entry = mips_vxworks_shared_plt_entry;
11302 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 11303 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
11304 }
11305 else
11306 {
11307 bfd_vma got_address_high, got_address_low;
11308
11309 plt_entry = mips_vxworks_exec_plt_entry;
11310 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11311 got_address_low = got_address & 0xffff;
11312
11313 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 11314 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
11315 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11316 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11317 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11318 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11319 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11320 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11321
11322 loc = (htab->srelplt2->contents
1bbce132 11323 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
0a44bf69
RS
11324
11325 /* Emit a relocation for the .got.plt entry. */
11326 rel.r_offset = got_address;
11327 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
1bbce132 11328 rel.r_addend = plt_offset;
0a44bf69
RS
11329 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11330
11331 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11332 loc += sizeof (Elf32_External_Rela);
11333 rel.r_offset = plt_address + 8;
11334 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11335 rel.r_addend = got_offset;
11336 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11337
11338 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11339 loc += sizeof (Elf32_External_Rela);
11340 rel.r_offset += 4;
11341 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11342 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11343 }
11344
11345 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
ce558b89 11346 loc = (htab->root.srelplt->contents
1bbce132 11347 + gotplt_index * sizeof (Elf32_External_Rela));
0a44bf69
RS
11348 rel.r_offset = got_address;
11349 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11350 rel.r_addend = 0;
11351 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11352
11353 if (!h->def_regular)
11354 sym->st_shndx = SHN_UNDEF;
11355 }
11356
11357 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11358
ce558b89 11359 sgot = htab->root.sgot;
a8028dd0 11360 g = htab->got_info;
0a44bf69
RS
11361 BFD_ASSERT (g != NULL);
11362
11363 /* See if this symbol has an entry in the GOT. */
020d7251 11364 if (hmips->global_got_area != GGA_NONE)
0a44bf69
RS
11365 {
11366 bfd_vma offset;
11367 Elf_Internal_Rela outrel;
11368 bfd_byte *loc;
11369 asection *s;
11370
11371 /* Install the symbol value in the GOT. */
13fbec83 11372 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
0a44bf69
RS
11373 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11374
11375 /* Add a dynamic relocation for it. */
11376 s = mips_elf_rel_dyn_section (info, FALSE);
11377 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11378 outrel.r_offset = (sgot->output_section->vma
11379 + sgot->output_offset
11380 + offset);
11381 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11382 outrel.r_addend = 0;
11383 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11384 }
11385
11386 /* Emit a copy reloc, if needed. */
11387 if (h->needs_copy)
11388 {
11389 Elf_Internal_Rela rel;
5474d94f
AM
11390 asection *srel;
11391 bfd_byte *loc;
0a44bf69
RS
11392
11393 BFD_ASSERT (h->dynindx != -1);
11394
11395 rel.r_offset = (h->root.u.def.section->output_section->vma
11396 + h->root.u.def.section->output_offset
11397 + h->root.u.def.value);
11398 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11399 rel.r_addend = 0;
afbf7e8e 11400 if (h->root.u.def.section == htab->root.sdynrelro)
5474d94f
AM
11401 srel = htab->root.sreldynrelro;
11402 else
11403 srel = htab->root.srelbss;
11404 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11405 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11406 ++srel->reloc_count;
0a44bf69
RS
11407 }
11408
df58fc94
RS
11409 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11410 if (ELF_ST_IS_COMPRESSED (sym->st_other))
0a44bf69
RS
11411 sym->st_value &= ~1;
11412
11413 return TRUE;
11414}
11415
861fb55a
DJ
11416/* Write out a plt0 entry to the beginning of .plt. */
11417
1bbce132 11418static bfd_boolean
861fb55a
DJ
11419mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11420{
11421 bfd_byte *loc;
11422 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11423 static const bfd_vma *plt_entry;
11424 struct mips_elf_link_hash_table *htab;
11425
11426 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11427 BFD_ASSERT (htab != NULL);
11428
861fb55a
DJ
11429 if (ABI_64_P (output_bfd))
11430 plt_entry = mips_n64_exec_plt0_entry;
11431 else if (ABI_N32_P (output_bfd))
11432 plt_entry = mips_n32_exec_plt0_entry;
833794fc 11433 else if (!htab->plt_header_is_comp)
861fb55a 11434 plt_entry = mips_o32_exec_plt0_entry;
833794fc
MR
11435 else if (htab->insn32)
11436 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11437 else
11438 plt_entry = micromips_o32_exec_plt0_entry;
861fb55a
DJ
11439
11440 /* Calculate the value of .got.plt. */
ce558b89
AM
11441 gotplt_value = (htab->root.sgotplt->output_section->vma
11442 + htab->root.sgotplt->output_offset);
861fb55a
DJ
11443 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11444 gotplt_value_low = gotplt_value & 0xffff;
11445
11446 /* The PLT sequence is not safe for N64 if .got.plt's address can
11447 not be loaded in two instructions. */
789ff5b6
MR
11448 if (ABI_64_P (output_bfd)
11449 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11450 {
11451 _bfd_error_handler
11452 /* xgettext:c-format */
11453 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11454 "supported; consider using `-Ttext-segment=...'"),
11455 output_bfd,
11456 htab->root.sgotplt->output_section,
11457 (int64_t) gotplt_value);
11458 bfd_set_error (bfd_error_no_error);
11459 return FALSE;
11460 }
861fb55a
DJ
11461
11462 /* Install the PLT header. */
ce558b89 11463 loc = htab->root.splt->contents;
1bbce132
MR
11464 if (plt_entry == micromips_o32_exec_plt0_entry)
11465 {
11466 bfd_vma gotpc_offset;
11467 bfd_vma loc_address;
11468 size_t i;
11469
11470 BFD_ASSERT (gotplt_value % 4 == 0);
11471
ce558b89
AM
11472 loc_address = (htab->root.splt->output_section->vma
11473 + htab->root.splt->output_offset);
1bbce132
MR
11474 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11475
11476 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11477 if (gotpc_offset + 0x1000000 >= 0x2000000)
11478 {
4eca0228 11479 _bfd_error_handler
695344c0 11480 /* xgettext:c-format */
2dcf00ce
AM
11481 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11482 "beyond the range of ADDIUPC"),
1bbce132 11483 output_bfd,
ce558b89 11484 htab->root.sgotplt->output_section,
2dcf00ce 11485 (int64_t) gotpc_offset,
c08bb8dd 11486 htab->root.splt->output_section);
1bbce132
MR
11487 bfd_set_error (bfd_error_no_error);
11488 return FALSE;
11489 }
11490 bfd_put_16 (output_bfd,
11491 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11492 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11493 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11494 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11495 }
833794fc
MR
11496 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11497 {
11498 size_t i;
11499
11500 bfd_put_16 (output_bfd, plt_entry[0], loc);
11501 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11502 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11503 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11504 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11505 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11506 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11507 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11508 }
1bbce132
MR
11509 else
11510 {
11511 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11512 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11513 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11514 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11515 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11516 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11517 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11518 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11519 }
11520
11521 return TRUE;
861fb55a
DJ
11522}
11523
0a44bf69
RS
11524/* Install the PLT header for a VxWorks executable and finalize the
11525 contents of .rela.plt.unloaded. */
11526
11527static void
11528mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11529{
11530 Elf_Internal_Rela rela;
11531 bfd_byte *loc;
11532 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11533 static const bfd_vma *plt_entry;
11534 struct mips_elf_link_hash_table *htab;
11535
11536 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11537 BFD_ASSERT (htab != NULL);
11538
0a44bf69
RS
11539 plt_entry = mips_vxworks_exec_plt0_entry;
11540
11541 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11542 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11543 + htab->root.hgot->root.u.def.section->output_offset
11544 + htab->root.hgot->root.u.def.value);
11545
11546 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11547 got_value_low = got_value & 0xffff;
11548
11549 /* Calculate the address of the PLT header. */
ce558b89
AM
11550 plt_address = (htab->root.splt->output_section->vma
11551 + htab->root.splt->output_offset);
0a44bf69
RS
11552
11553 /* Install the PLT header. */
ce558b89 11554 loc = htab->root.splt->contents;
0a44bf69
RS
11555 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11556 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11557 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11558 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11559 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11560 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11561
11562 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11563 loc = htab->srelplt2->contents;
11564 rela.r_offset = plt_address;
11565 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11566 rela.r_addend = 0;
11567 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11568 loc += sizeof (Elf32_External_Rela);
11569
11570 /* Output the relocation for the following addiu of
11571 %lo(_GLOBAL_OFFSET_TABLE_). */
11572 rela.r_offset += 4;
11573 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11574 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11575 loc += sizeof (Elf32_External_Rela);
11576
11577 /* Fix up the remaining relocations. They may have the wrong
11578 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11579 in which symbols were output. */
11580 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11581 {
11582 Elf_Internal_Rela rel;
11583
11584 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11585 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11586 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11587 loc += sizeof (Elf32_External_Rela);
11588
11589 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11590 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11591 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11592 loc += sizeof (Elf32_External_Rela);
11593
11594 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11595 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11596 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11597 loc += sizeof (Elf32_External_Rela);
11598 }
11599}
11600
11601/* Install the PLT header for a VxWorks shared library. */
11602
11603static void
11604mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11605{
11606 unsigned int i;
11607 struct mips_elf_link_hash_table *htab;
11608
11609 htab = mips_elf_hash_table (info);
4dfe6ac6 11610 BFD_ASSERT (htab != NULL);
0a44bf69
RS
11611
11612 /* We just need to copy the entry byte-by-byte. */
11613 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11614 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
ce558b89 11615 htab->root.splt->contents + i * 4);
0a44bf69
RS
11616}
11617
b49e97c9
TS
11618/* Finish up the dynamic sections. */
11619
b34976b6 11620bfd_boolean
9719ad41
RS
11621_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11622 struct bfd_link_info *info)
b49e97c9
TS
11623{
11624 bfd *dynobj;
11625 asection *sdyn;
11626 asection *sgot;
f4416af6 11627 struct mips_got_info *gg, *g;
0a44bf69 11628 struct mips_elf_link_hash_table *htab;
b49e97c9 11629
0a44bf69 11630 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11631 BFD_ASSERT (htab != NULL);
11632
b49e97c9
TS
11633 dynobj = elf_hash_table (info)->dynobj;
11634
3d4d4302 11635 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
b49e97c9 11636
ce558b89 11637 sgot = htab->root.sgot;
23cc69b6 11638 gg = htab->got_info;
b49e97c9
TS
11639
11640 if (elf_hash_table (info)->dynamic_sections_created)
11641 {
11642 bfd_byte *b;
943284cc 11643 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
11644
11645 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
11646 BFD_ASSERT (gg != NULL);
11647
d7206569 11648 g = mips_elf_bfd_got (output_bfd, FALSE);
b49e97c9
TS
11649 BFD_ASSERT (g != NULL);
11650
11651 for (b = sdyn->contents;
eea6121a 11652 b < sdyn->contents + sdyn->size;
b49e97c9
TS
11653 b += MIPS_ELF_DYN_SIZE (dynobj))
11654 {
11655 Elf_Internal_Dyn dyn;
11656 const char *name;
11657 size_t elemsize;
11658 asection *s;
b34976b6 11659 bfd_boolean swap_out_p;
b49e97c9
TS
11660
11661 /* Read in the current dynamic entry. */
11662 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11663
11664 /* Assume that we're going to modify it and write it out. */
b34976b6 11665 swap_out_p = TRUE;
b49e97c9
TS
11666
11667 switch (dyn.d_tag)
11668 {
11669 case DT_RELENT:
b49e97c9
TS
11670 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11671 break;
11672
0a44bf69
RS
11673 case DT_RELAENT:
11674 BFD_ASSERT (htab->is_vxworks);
11675 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11676 break;
11677
b49e97c9
TS
11678 case DT_STRSZ:
11679 /* Rewrite DT_STRSZ. */
11680 dyn.d_un.d_val =
11681 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11682 break;
11683
11684 case DT_PLTGOT:
ce558b89 11685 s = htab->root.sgot;
861fb55a
DJ
11686 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11687 break;
11688
11689 case DT_MIPS_PLTGOT:
ce558b89 11690 s = htab->root.sgotplt;
861fb55a 11691 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
11692 break;
11693
11694 case DT_MIPS_RLD_VERSION:
11695 dyn.d_un.d_val = 1; /* XXX */
11696 break;
11697
11698 case DT_MIPS_FLAGS:
11699 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11700 break;
11701
b49e97c9 11702 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
11703 {
11704 time_t t;
11705 time (&t);
11706 dyn.d_un.d_val = t;
11707 }
b49e97c9
TS
11708 break;
11709
11710 case DT_MIPS_ICHECKSUM:
11711 /* XXX FIXME: */
b34976b6 11712 swap_out_p = FALSE;
b49e97c9
TS
11713 break;
11714
11715 case DT_MIPS_IVERSION:
11716 /* XXX FIXME: */
b34976b6 11717 swap_out_p = FALSE;
b49e97c9
TS
11718 break;
11719
11720 case DT_MIPS_BASE_ADDRESS:
11721 s = output_bfd->sections;
11722 BFD_ASSERT (s != NULL);
11723 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11724 break;
11725
11726 case DT_MIPS_LOCAL_GOTNO:
11727 dyn.d_un.d_val = g->local_gotno;
11728 break;
11729
11730 case DT_MIPS_UNREFEXTNO:
11731 /* The index into the dynamic symbol table which is the
11732 entry of the first external symbol that is not
11733 referenced within the same object. */
11734 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11735 break;
11736
11737 case DT_MIPS_GOTSYM:
d222d210 11738 if (htab->global_gotsym)
b49e97c9 11739 {
d222d210 11740 dyn.d_un.d_val = htab->global_gotsym->dynindx;
b49e97c9
TS
11741 break;
11742 }
11743 /* In case if we don't have global got symbols we default
11744 to setting DT_MIPS_GOTSYM to the same value as
1a0670f3
AM
11745 DT_MIPS_SYMTABNO. */
11746 /* Fall through. */
b49e97c9
TS
11747
11748 case DT_MIPS_SYMTABNO:
11749 name = ".dynsym";
11750 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
4ade44b7 11751 s = bfd_get_linker_section (dynobj, name);
b49e97c9 11752
131e2f8e
MF
11753 if (s != NULL)
11754 dyn.d_un.d_val = s->size / elemsize;
11755 else
11756 dyn.d_un.d_val = 0;
b49e97c9
TS
11757 break;
11758
11759 case DT_MIPS_HIPAGENO:
861fb55a 11760 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
11761 break;
11762
11763 case DT_MIPS_RLD_MAP:
b4082c70
DD
11764 {
11765 struct elf_link_hash_entry *h;
11766 h = mips_elf_hash_table (info)->rld_symbol;
11767 if (!h)
11768 {
11769 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11770 swap_out_p = FALSE;
11771 break;
11772 }
11773 s = h->root.u.def.section;
a5499fa4
MF
11774
11775 /* The MIPS_RLD_MAP tag stores the absolute address of the
11776 debug pointer. */
b4082c70
DD
11777 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11778 + h->root.u.def.value);
11779 }
b49e97c9
TS
11780 break;
11781
a5499fa4
MF
11782 case DT_MIPS_RLD_MAP_REL:
11783 {
11784 struct elf_link_hash_entry *h;
11785 bfd_vma dt_addr, rld_addr;
11786 h = mips_elf_hash_table (info)->rld_symbol;
11787 if (!h)
11788 {
11789 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11790 swap_out_p = FALSE;
11791 break;
11792 }
11793 s = h->root.u.def.section;
11794
11795 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
11796 pointer, relative to the address of the tag. */
11797 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
d5cff5df 11798 + (b - sdyn->contents));
a5499fa4
MF
11799 rld_addr = (s->output_section->vma + s->output_offset
11800 + h->root.u.def.value);
11801 dyn.d_un.d_ptr = rld_addr - dt_addr;
11802 }
11803 break;
11804
b49e97c9
TS
11805 case DT_MIPS_OPTIONS:
11806 s = (bfd_get_section_by_name
11807 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11808 dyn.d_un.d_ptr = s->vma;
11809 break;
11810
0a44bf69 11811 case DT_PLTREL:
861fb55a
DJ
11812 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11813 if (htab->is_vxworks)
11814 dyn.d_un.d_val = DT_RELA;
11815 else
11816 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
11817 break;
11818
11819 case DT_PLTRELSZ:
861fb55a 11820 BFD_ASSERT (htab->use_plts_and_copy_relocs);
ce558b89 11821 dyn.d_un.d_val = htab->root.srelplt->size;
0a44bf69
RS
11822 break;
11823
11824 case DT_JMPREL:
861fb55a 11825 BFD_ASSERT (htab->use_plts_and_copy_relocs);
ce558b89
AM
11826 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
11827 + htab->root.srelplt->output_offset);
0a44bf69
RS
11828 break;
11829
943284cc
DJ
11830 case DT_TEXTREL:
11831 /* If we didn't need any text relocations after all, delete
11832 the dynamic tag. */
11833 if (!(info->flags & DF_TEXTREL))
11834 {
11835 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11836 swap_out_p = FALSE;
11837 }
11838 break;
11839
11840 case DT_FLAGS:
11841 /* If we didn't need any text relocations after all, clear
11842 DF_TEXTREL from DT_FLAGS. */
11843 if (!(info->flags & DF_TEXTREL))
11844 dyn.d_un.d_val &= ~DF_TEXTREL;
11845 else
11846 swap_out_p = FALSE;
11847 break;
11848
b49e97c9 11849 default:
b34976b6 11850 swap_out_p = FALSE;
7a2b07ff
NS
11851 if (htab->is_vxworks
11852 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
11853 swap_out_p = TRUE;
b49e97c9
TS
11854 break;
11855 }
11856
943284cc 11857 if (swap_out_p || dyn_skipped)
b49e97c9 11858 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
11859 (dynobj, &dyn, b - dyn_skipped);
11860
11861 if (dyn_to_skip)
11862 {
11863 dyn_skipped += dyn_to_skip;
11864 dyn_to_skip = 0;
11865 }
b49e97c9 11866 }
943284cc
DJ
11867
11868 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
11869 if (dyn_skipped > 0)
11870 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
11871 }
11872
b55fd4d4
DJ
11873 if (sgot != NULL && sgot->size > 0
11874 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 11875 {
0a44bf69
RS
11876 if (htab->is_vxworks)
11877 {
11878 /* The first entry of the global offset table points to the
11879 ".dynamic" section. The second is initialized by the
11880 loader and contains the shared library identifier.
11881 The third is also initialized by the loader and points
11882 to the lazy resolution stub. */
11883 MIPS_ELF_PUT_WORD (output_bfd,
11884 sdyn->output_offset + sdyn->output_section->vma,
11885 sgot->contents);
11886 MIPS_ELF_PUT_WORD (output_bfd, 0,
11887 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11888 MIPS_ELF_PUT_WORD (output_bfd, 0,
11889 sgot->contents
11890 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
11891 }
11892 else
11893 {
11894 /* The first entry of the global offset table will be filled at
11895 runtime. The second entry will be used by some runtime loaders.
11896 This isn't the case of IRIX rld. */
11897 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 11898 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
11899 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
11900 }
b49e97c9 11901
54938e2a
TS
11902 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
11903 = MIPS_ELF_GOT_SIZE (output_bfd);
11904 }
b49e97c9 11905
f4416af6
AO
11906 /* Generate dynamic relocations for the non-primary gots. */
11907 if (gg != NULL && gg->next)
11908 {
11909 Elf_Internal_Rela rel[3];
11910 bfd_vma addend = 0;
11911
11912 memset (rel, 0, sizeof (rel));
11913 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
11914
11915 for (g = gg->next; g->next != gg; g = g->next)
11916 {
91d6fa6a 11917 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 11918 + g->next->tls_gotno;
f4416af6 11919
9719ad41 11920 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 11921 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
11922 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
11923 sgot->contents
91d6fa6a 11924 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6 11925
0e1862bb 11926 if (! bfd_link_pic (info))
f4416af6
AO
11927 continue;
11928
cb22ccf4 11929 for (; got_index < g->local_gotno; got_index++)
f4416af6 11930 {
cb22ccf4
KCY
11931 if (got_index >= g->assigned_low_gotno
11932 && got_index <= g->assigned_high_gotno)
11933 continue;
11934
f4416af6 11935 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
cb22ccf4 11936 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
11937 if (!(mips_elf_create_dynamic_relocation
11938 (output_bfd, info, rel, NULL,
11939 bfd_abs_section_ptr,
11940 0, &addend, sgot)))
11941 return FALSE;
11942 BFD_ASSERT (addend == 0);
11943 }
11944 }
11945 }
11946
3133ddbf
DJ
11947 /* The generation of dynamic relocations for the non-primary gots
11948 adds more dynamic relocations. We cannot count them until
11949 here. */
11950
11951 if (elf_hash_table (info)->dynamic_sections_created)
11952 {
11953 bfd_byte *b;
11954 bfd_boolean swap_out_p;
11955
11956 BFD_ASSERT (sdyn != NULL);
11957
11958 for (b = sdyn->contents;
11959 b < sdyn->contents + sdyn->size;
11960 b += MIPS_ELF_DYN_SIZE (dynobj))
11961 {
11962 Elf_Internal_Dyn dyn;
11963 asection *s;
11964
11965 /* Read in the current dynamic entry. */
11966 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11967
11968 /* Assume that we're going to modify it and write it out. */
11969 swap_out_p = TRUE;
11970
11971 switch (dyn.d_tag)
11972 {
11973 case DT_RELSZ:
11974 /* Reduce DT_RELSZ to account for any relocations we
11975 decided not to make. This is for the n64 irix rld,
11976 which doesn't seem to apply any relocations if there
11977 are trailing null entries. */
0a44bf69 11978 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
11979 dyn.d_un.d_val = (s->reloc_count
11980 * (ABI_64_P (output_bfd)
11981 ? sizeof (Elf64_Mips_External_Rel)
11982 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
11983 /* Adjust the section size too. Tools like the prelinker
11984 can reasonably expect the values to the same. */
db841b6f 11985 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
bcfdf036
RS
11986 elf_section_data (s->output_section)->this_hdr.sh_size
11987 = dyn.d_un.d_val;
3133ddbf
DJ
11988 break;
11989
11990 default:
11991 swap_out_p = FALSE;
11992 break;
11993 }
11994
11995 if (swap_out_p)
11996 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
11997 (dynobj, &dyn, b);
11998 }
11999 }
12000
b49e97c9 12001 {
b49e97c9
TS
12002 asection *s;
12003 Elf32_compact_rel cpt;
12004
b49e97c9
TS
12005 if (SGI_COMPAT (output_bfd))
12006 {
12007 /* Write .compact_rel section out. */
3d4d4302 12008 s = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
12009 if (s != NULL)
12010 {
12011 cpt.id1 = 1;
12012 cpt.num = s->reloc_count;
12013 cpt.id2 = 2;
12014 cpt.offset = (s->output_section->filepos
12015 + sizeof (Elf32_External_compact_rel));
12016 cpt.reserved0 = 0;
12017 cpt.reserved1 = 0;
12018 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12019 ((Elf32_External_compact_rel *)
12020 s->contents));
12021
12022 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 12023 if (htab->sstubs != NULL)
b49e97c9
TS
12024 {
12025 file_ptr dummy_offset;
12026
4e41d0d7
RS
12027 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12028 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12029 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 12030 htab->function_stub_size);
b49e97c9
TS
12031 }
12032 }
12033 }
12034
0a44bf69
RS
12035 /* The psABI says that the dynamic relocations must be sorted in
12036 increasing order of r_symndx. The VxWorks EABI doesn't require
12037 this, and because the code below handles REL rather than RELA
12038 relocations, using it for VxWorks would be outright harmful. */
12039 if (!htab->is_vxworks)
b49e97c9 12040 {
0a44bf69
RS
12041 s = mips_elf_rel_dyn_section (info, FALSE);
12042 if (s != NULL
12043 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12044 {
12045 reldyn_sorting_bfd = output_bfd;
b49e97c9 12046
0a44bf69
RS
12047 if (ABI_64_P (output_bfd))
12048 qsort ((Elf64_External_Rel *) s->contents + 1,
12049 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12050 sort_dynamic_relocs_64);
12051 else
12052 qsort ((Elf32_External_Rel *) s->contents + 1,
12053 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12054 sort_dynamic_relocs);
12055 }
b49e97c9 12056 }
b49e97c9
TS
12057 }
12058
ce558b89 12059 if (htab->root.splt && htab->root.splt->size > 0)
0a44bf69 12060 {
861fb55a
DJ
12061 if (htab->is_vxworks)
12062 {
0e1862bb 12063 if (bfd_link_pic (info))
861fb55a
DJ
12064 mips_vxworks_finish_shared_plt (output_bfd, info);
12065 else
12066 mips_vxworks_finish_exec_plt (output_bfd, info);
12067 }
0a44bf69 12068 else
861fb55a 12069 {
0e1862bb 12070 BFD_ASSERT (!bfd_link_pic (info));
1bbce132
MR
12071 if (!mips_finish_exec_plt (output_bfd, info))
12072 return FALSE;
861fb55a 12073 }
0a44bf69 12074 }
b34976b6 12075 return TRUE;
b49e97c9
TS
12076}
12077
b49e97c9 12078
64543e1a
RS
12079/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12080
12081static void
9719ad41 12082mips_set_isa_flags (bfd *abfd)
b49e97c9 12083{
64543e1a 12084 flagword val;
b49e97c9
TS
12085
12086 switch (bfd_get_mach (abfd))
12087 {
12088 default:
12089 case bfd_mach_mips3000:
12090 val = E_MIPS_ARCH_1;
12091 break;
12092
12093 case bfd_mach_mips3900:
12094 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12095 break;
12096
12097 case bfd_mach_mips6000:
12098 val = E_MIPS_ARCH_2;
12099 break;
12100
b417536f
MR
12101 case bfd_mach_mips4010:
12102 val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12103 break;
12104
b49e97c9
TS
12105 case bfd_mach_mips4000:
12106 case bfd_mach_mips4300:
12107 case bfd_mach_mips4400:
12108 case bfd_mach_mips4600:
12109 val = E_MIPS_ARCH_3;
12110 break;
12111
b49e97c9
TS
12112 case bfd_mach_mips4100:
12113 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12114 break;
12115
12116 case bfd_mach_mips4111:
12117 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12118 break;
12119
00707a0e
RS
12120 case bfd_mach_mips4120:
12121 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12122 break;
12123
b49e97c9
TS
12124 case bfd_mach_mips4650:
12125 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12126 break;
12127
00707a0e
RS
12128 case bfd_mach_mips5400:
12129 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12130 break;
12131
12132 case bfd_mach_mips5500:
12133 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12134 break;
12135
e407c74b
NC
12136 case bfd_mach_mips5900:
12137 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12138 break;
12139
0d2e43ed
ILT
12140 case bfd_mach_mips9000:
12141 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12142 break;
12143
b49e97c9 12144 case bfd_mach_mips5000:
5a7ea749 12145 case bfd_mach_mips7000:
b49e97c9
TS
12146 case bfd_mach_mips8000:
12147 case bfd_mach_mips10000:
12148 case bfd_mach_mips12000:
3aa3176b
TS
12149 case bfd_mach_mips14000:
12150 case bfd_mach_mips16000:
b49e97c9
TS
12151 val = E_MIPS_ARCH_4;
12152 break;
12153
12154 case bfd_mach_mips5:
12155 val = E_MIPS_ARCH_5;
12156 break;
12157
350cc38d
MS
12158 case bfd_mach_mips_loongson_2e:
12159 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12160 break;
12161
12162 case bfd_mach_mips_loongson_2f:
12163 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12164 break;
12165
b49e97c9
TS
12166 case bfd_mach_mips_sb1:
12167 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12168 break;
12169
ac8cb70f
CX
12170 case bfd_mach_mips_gs464:
12171 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
d051516a
NC
12172 break;
12173
bd782c07
CX
12174 case bfd_mach_mips_gs464e:
12175 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12176 break;
12177
9108bc33
CX
12178 case bfd_mach_mips_gs264e:
12179 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12180 break;
12181
6f179bd0 12182 case bfd_mach_mips_octeon:
dd6a37e7 12183 case bfd_mach_mips_octeonp:
6f179bd0
AN
12184 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12185 break;
12186
2c629856
N
12187 case bfd_mach_mips_octeon3:
12188 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12189 break;
12190
52b6b6b9
JM
12191 case bfd_mach_mips_xlr:
12192 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12193 break;
12194
432233b3
AP
12195 case bfd_mach_mips_octeon2:
12196 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12197 break;
12198
b49e97c9
TS
12199 case bfd_mach_mipsisa32:
12200 val = E_MIPS_ARCH_32;
12201 break;
12202
12203 case bfd_mach_mipsisa64:
12204 val = E_MIPS_ARCH_64;
af7ee8bf
CD
12205 break;
12206
12207 case bfd_mach_mipsisa32r2:
ae52f483
AB
12208 case bfd_mach_mipsisa32r3:
12209 case bfd_mach_mipsisa32r5:
af7ee8bf
CD
12210 val = E_MIPS_ARCH_32R2;
12211 break;
5f74bc13 12212
38bf472a
MR
12213 case bfd_mach_mips_interaptiv_mr2:
12214 val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12215 break;
12216
5f74bc13 12217 case bfd_mach_mipsisa64r2:
ae52f483
AB
12218 case bfd_mach_mipsisa64r3:
12219 case bfd_mach_mipsisa64r5:
5f74bc13
CD
12220 val = E_MIPS_ARCH_64R2;
12221 break;
7361da2c
AB
12222
12223 case bfd_mach_mipsisa32r6:
12224 val = E_MIPS_ARCH_32R6;
12225 break;
12226
12227 case bfd_mach_mipsisa64r6:
12228 val = E_MIPS_ARCH_64R6;
12229 break;
b49e97c9 12230 }
b49e97c9
TS
12231 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12232 elf_elfheader (abfd)->e_flags |= val;
12233
64543e1a
RS
12234}
12235
12236
28dbcedc
AM
12237/* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12238 Don't do so for code sections. We want to keep ordering of HI16/LO16
12239 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12240 relocs to be sorted. */
12241
12242bfd_boolean
12243_bfd_mips_elf_sort_relocs_p (asection *sec)
12244{
12245 return (sec->flags & SEC_CODE) == 0;
12246}
12247
12248
64543e1a
RS
12249/* The final processing done just before writing out a MIPS ELF object
12250 file. This gets the MIPS architecture right based on the machine
12251 number. This is used by both the 32-bit and the 64-bit ABI. */
12252
12253void
9719ad41
RS
12254_bfd_mips_elf_final_write_processing (bfd *abfd,
12255 bfd_boolean linker ATTRIBUTE_UNUSED)
64543e1a
RS
12256{
12257 unsigned int i;
12258 Elf_Internal_Shdr **hdrpp;
12259 const char *name;
12260 asection *sec;
12261
12262 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12263 is nonzero. This is for compatibility with old objects, which used
12264 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12265 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12266 mips_set_isa_flags (abfd);
12267
b49e97c9
TS
12268 /* Set the sh_info field for .gptab sections and other appropriate
12269 info for each special section. */
12270 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12271 i < elf_numsections (abfd);
12272 i++, hdrpp++)
12273 {
12274 switch ((*hdrpp)->sh_type)
12275 {
12276 case SHT_MIPS_MSYM:
12277 case SHT_MIPS_LIBLIST:
12278 sec = bfd_get_section_by_name (abfd, ".dynstr");
12279 if (sec != NULL)
12280 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12281 break;
12282
12283 case SHT_MIPS_GPTAB:
12284 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12285 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
12286 BFD_ASSERT (name != NULL
0112cd26 12287 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
12288 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12289 BFD_ASSERT (sec != NULL);
12290 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12291 break;
12292
12293 case SHT_MIPS_CONTENT:
12294 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12295 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
12296 BFD_ASSERT (name != NULL
0112cd26 12297 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
12298 sec = bfd_get_section_by_name (abfd,
12299 name + sizeof ".MIPS.content" - 1);
12300 BFD_ASSERT (sec != NULL);
12301 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12302 break;
12303
12304 case SHT_MIPS_SYMBOL_LIB:
12305 sec = bfd_get_section_by_name (abfd, ".dynsym");
12306 if (sec != NULL)
12307 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12308 sec = bfd_get_section_by_name (abfd, ".liblist");
12309 if (sec != NULL)
12310 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12311 break;
12312
12313 case SHT_MIPS_EVENTS:
12314 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
12315 name = bfd_get_section_name (abfd, (*hdrpp)->bfd_section);
12316 BFD_ASSERT (name != NULL);
0112cd26 12317 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
12318 sec = bfd_get_section_by_name (abfd,
12319 name + sizeof ".MIPS.events" - 1);
12320 else
12321 {
0112cd26 12322 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
12323 sec = bfd_get_section_by_name (abfd,
12324 (name
12325 + sizeof ".MIPS.post_rel" - 1));
12326 }
12327 BFD_ASSERT (sec != NULL);
12328 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12329 break;
12330
12331 }
12332 }
12333}
12334\f
8dc1a139 12335/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
12336 segments. */
12337
12338int
a6b96beb
AM
12339_bfd_mips_elf_additional_program_headers (bfd *abfd,
12340 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
12341{
12342 asection *s;
12343 int ret = 0;
12344
12345 /* See if we need a PT_MIPS_REGINFO segment. */
12346 s = bfd_get_section_by_name (abfd, ".reginfo");
12347 if (s && (s->flags & SEC_LOAD))
12348 ++ret;
12349
351cdf24
MF
12350 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12351 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12352 ++ret;
12353
b49e97c9
TS
12354 /* See if we need a PT_MIPS_OPTIONS segment. */
12355 if (IRIX_COMPAT (abfd) == ict_irix6
12356 && bfd_get_section_by_name (abfd,
12357 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12358 ++ret;
12359
12360 /* See if we need a PT_MIPS_RTPROC segment. */
12361 if (IRIX_COMPAT (abfd) == ict_irix5
12362 && bfd_get_section_by_name (abfd, ".dynamic")
12363 && bfd_get_section_by_name (abfd, ".mdebug"))
12364 ++ret;
12365
98c904a8
RS
12366 /* Allocate a PT_NULL header in dynamic objects. See
12367 _bfd_mips_elf_modify_segment_map for details. */
12368 if (!SGI_COMPAT (abfd)
12369 && bfd_get_section_by_name (abfd, ".dynamic"))
12370 ++ret;
12371
b49e97c9
TS
12372 return ret;
12373}
12374
8dc1a139 12375/* Modify the segment map for an IRIX5 executable. */
b49e97c9 12376
b34976b6 12377bfd_boolean
9719ad41 12378_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 12379 struct bfd_link_info *info)
b49e97c9
TS
12380{
12381 asection *s;
12382 struct elf_segment_map *m, **pm;
12383 bfd_size_type amt;
12384
12385 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12386 segment. */
12387 s = bfd_get_section_by_name (abfd, ".reginfo");
12388 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12389 {
12bd6957 12390 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
12391 if (m->p_type == PT_MIPS_REGINFO)
12392 break;
12393 if (m == NULL)
12394 {
12395 amt = sizeof *m;
9719ad41 12396 m = bfd_zalloc (abfd, amt);
b49e97c9 12397 if (m == NULL)
b34976b6 12398 return FALSE;
b49e97c9
TS
12399
12400 m->p_type = PT_MIPS_REGINFO;
12401 m->count = 1;
12402 m->sections[0] = s;
12403
12404 /* We want to put it after the PHDR and INTERP segments. */
12bd6957 12405 pm = &elf_seg_map (abfd);
b49e97c9
TS
12406 while (*pm != NULL
12407 && ((*pm)->p_type == PT_PHDR
12408 || (*pm)->p_type == PT_INTERP))
12409 pm = &(*pm)->next;
12410
12411 m->next = *pm;
12412 *pm = m;
12413 }
12414 }
12415
351cdf24
MF
12416 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12417 segment. */
12418 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12419 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12420 {
12421 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12422 if (m->p_type == PT_MIPS_ABIFLAGS)
12423 break;
12424 if (m == NULL)
12425 {
12426 amt = sizeof *m;
12427 m = bfd_zalloc (abfd, amt);
12428 if (m == NULL)
12429 return FALSE;
12430
12431 m->p_type = PT_MIPS_ABIFLAGS;
12432 m->count = 1;
12433 m->sections[0] = s;
12434
12435 /* We want to put it after the PHDR and INTERP segments. */
12436 pm = &elf_seg_map (abfd);
12437 while (*pm != NULL
12438 && ((*pm)->p_type == PT_PHDR
12439 || (*pm)->p_type == PT_INTERP))
12440 pm = &(*pm)->next;
12441
12442 m->next = *pm;
12443 *pm = m;
12444 }
12445 }
12446
b49e97c9
TS
12447 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12448 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 12449 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 12450 table. */
c1fd6598
AO
12451 if (NEWABI_P (abfd)
12452 /* On non-IRIX6 new abi, we'll have already created a segment
12453 for this section, so don't create another. I'm not sure this
12454 is not also the case for IRIX 6, but I can't test it right
12455 now. */
12456 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
12457 {
12458 for (s = abfd->sections; s; s = s->next)
12459 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12460 break;
12461
12462 if (s)
12463 {
12464 struct elf_segment_map *options_segment;
12465
12bd6957 12466 pm = &elf_seg_map (abfd);
98a8deaf
RS
12467 while (*pm != NULL
12468 && ((*pm)->p_type == PT_PHDR
12469 || (*pm)->p_type == PT_INTERP))
12470 pm = &(*pm)->next;
b49e97c9 12471
8ded5a0f
AM
12472 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12473 {
12474 amt = sizeof (struct elf_segment_map);
12475 options_segment = bfd_zalloc (abfd, amt);
12476 options_segment->next = *pm;
12477 options_segment->p_type = PT_MIPS_OPTIONS;
12478 options_segment->p_flags = PF_R;
12479 options_segment->p_flags_valid = TRUE;
12480 options_segment->count = 1;
12481 options_segment->sections[0] = s;
12482 *pm = options_segment;
12483 }
b49e97c9
TS
12484 }
12485 }
12486 else
12487 {
12488 if (IRIX_COMPAT (abfd) == ict_irix5)
12489 {
12490 /* If there are .dynamic and .mdebug sections, we make a room
12491 for the RTPROC header. FIXME: Rewrite without section names. */
12492 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12493 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12494 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12495 {
12bd6957 12496 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
12497 if (m->p_type == PT_MIPS_RTPROC)
12498 break;
12499 if (m == NULL)
12500 {
12501 amt = sizeof *m;
9719ad41 12502 m = bfd_zalloc (abfd, amt);
b49e97c9 12503 if (m == NULL)
b34976b6 12504 return FALSE;
b49e97c9
TS
12505
12506 m->p_type = PT_MIPS_RTPROC;
12507
12508 s = bfd_get_section_by_name (abfd, ".rtproc");
12509 if (s == NULL)
12510 {
12511 m->count = 0;
12512 m->p_flags = 0;
12513 m->p_flags_valid = 1;
12514 }
12515 else
12516 {
12517 m->count = 1;
12518 m->sections[0] = s;
12519 }
12520
12521 /* We want to put it after the DYNAMIC segment. */
12bd6957 12522 pm = &elf_seg_map (abfd);
b49e97c9
TS
12523 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12524 pm = &(*pm)->next;
12525 if (*pm != NULL)
12526 pm = &(*pm)->next;
12527
12528 m->next = *pm;
12529 *pm = m;
12530 }
12531 }
12532 }
8dc1a139 12533 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
12534 .dynstr, .dynsym, and .hash sections, and everything in
12535 between. */
12bd6957 12536 for (pm = &elf_seg_map (abfd); *pm != NULL;
b49e97c9
TS
12537 pm = &(*pm)->next)
12538 if ((*pm)->p_type == PT_DYNAMIC)
12539 break;
12540 m = *pm;
f6f62d6f
RS
12541 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12542 glibc's dynamic linker has traditionally derived the number of
12543 tags from the p_filesz field, and sometimes allocates stack
12544 arrays of that size. An overly-big PT_DYNAMIC segment can
12545 be actively harmful in such cases. Making PT_DYNAMIC contain
12546 other sections can also make life hard for the prelinker,
12547 which might move one of the other sections to a different
12548 PT_LOAD segment. */
12549 if (SGI_COMPAT (abfd)
12550 && m != NULL
12551 && m->count == 1
12552 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
12553 {
12554 static const char *sec_names[] =
12555 {
12556 ".dynamic", ".dynstr", ".dynsym", ".hash"
12557 };
12558 bfd_vma low, high;
12559 unsigned int i, c;
12560 struct elf_segment_map *n;
12561
792b4a53 12562 low = ~(bfd_vma) 0;
b49e97c9
TS
12563 high = 0;
12564 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12565 {
12566 s = bfd_get_section_by_name (abfd, sec_names[i]);
12567 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12568 {
12569 bfd_size_type sz;
12570
12571 if (low > s->vma)
12572 low = s->vma;
eea6121a 12573 sz = s->size;
b49e97c9
TS
12574 if (high < s->vma + sz)
12575 high = s->vma + sz;
12576 }
12577 }
12578
12579 c = 0;
12580 for (s = abfd->sections; s != NULL; s = s->next)
12581 if ((s->flags & SEC_LOAD) != 0
12582 && s->vma >= low
eea6121a 12583 && s->vma + s->size <= high)
b49e97c9
TS
12584 ++c;
12585
12586 amt = sizeof *n + (bfd_size_type) (c - 1) * sizeof (asection *);
9719ad41 12587 n = bfd_zalloc (abfd, amt);
b49e97c9 12588 if (n == NULL)
b34976b6 12589 return FALSE;
b49e97c9
TS
12590 *n = *m;
12591 n->count = c;
12592
12593 i = 0;
12594 for (s = abfd->sections; s != NULL; s = s->next)
12595 {
12596 if ((s->flags & SEC_LOAD) != 0
12597 && s->vma >= low
eea6121a 12598 && s->vma + s->size <= high)
b49e97c9
TS
12599 {
12600 n->sections[i] = s;
12601 ++i;
12602 }
12603 }
12604
12605 *pm = n;
12606 }
12607 }
12608
98c904a8
RS
12609 /* Allocate a spare program header in dynamic objects so that tools
12610 like the prelinker can add an extra PT_LOAD entry.
12611
12612 If the prelinker needs to make room for a new PT_LOAD entry, its
12613 standard procedure is to move the first (read-only) sections into
12614 the new (writable) segment. However, the MIPS ABI requires
12615 .dynamic to be in a read-only segment, and the section will often
12616 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12617
12618 Although the prelinker could in principle move .dynamic to a
12619 writable segment, it seems better to allocate a spare program
12620 header instead, and avoid the need to move any sections.
12621 There is a long tradition of allocating spare dynamic tags,
12622 so allocating a spare program header seems like a natural
7c8b76cc
JM
12623 extension.
12624
12625 If INFO is NULL, we may be copying an already prelinked binary
12626 with objcopy or strip, so do not add this header. */
12627 if (info != NULL
12628 && !SGI_COMPAT (abfd)
98c904a8
RS
12629 && bfd_get_section_by_name (abfd, ".dynamic"))
12630 {
12bd6957 12631 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
98c904a8
RS
12632 if ((*pm)->p_type == PT_NULL)
12633 break;
12634 if (*pm == NULL)
12635 {
12636 m = bfd_zalloc (abfd, sizeof (*m));
12637 if (m == NULL)
12638 return FALSE;
12639
12640 m->p_type = PT_NULL;
12641 *pm = m;
12642 }
12643 }
12644
b34976b6 12645 return TRUE;
b49e97c9
TS
12646}
12647\f
12648/* Return the section that should be marked against GC for a given
12649 relocation. */
12650
12651asection *
9719ad41 12652_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 12653 struct bfd_link_info *info,
9719ad41
RS
12654 Elf_Internal_Rela *rel,
12655 struct elf_link_hash_entry *h,
12656 Elf_Internal_Sym *sym)
b49e97c9
TS
12657{
12658 /* ??? Do mips16 stub sections need to be handled special? */
12659
12660 if (h != NULL)
07adf181
AM
12661 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12662 {
12663 case R_MIPS_GNU_VTINHERIT:
12664 case R_MIPS_GNU_VTENTRY:
12665 return NULL;
12666 }
b49e97c9 12667
07adf181 12668 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
12669}
12670
351cdf24
MF
12671/* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12672
12673bfd_boolean
12674_bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12675 elf_gc_mark_hook_fn gc_mark_hook)
12676{
12677 bfd *sub;
12678
12679 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12680
12681 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12682 {
12683 asection *o;
12684
12685 if (! is_mips_elf (sub))
12686 continue;
12687
12688 for (o = sub->sections; o != NULL; o = o->next)
12689 if (!o->gc_mark
12690 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P
12691 (bfd_get_section_name (sub, o)))
12692 {
12693 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12694 return FALSE;
12695 }
12696 }
12697
12698 return TRUE;
12699}
b49e97c9
TS
12700\f
12701/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12702 hiding the old indirect symbol. Process additional relocation
12703 information. Also called for weakdefs, in which case we just let
12704 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12705
12706void
fcfa13d2 12707_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
12708 struct elf_link_hash_entry *dir,
12709 struct elf_link_hash_entry *ind)
b49e97c9
TS
12710{
12711 struct mips_elf_link_hash_entry *dirmips, *indmips;
12712
fcfa13d2 12713 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 12714
861fb55a
DJ
12715 dirmips = (struct mips_elf_link_hash_entry *) dir;
12716 indmips = (struct mips_elf_link_hash_entry *) ind;
12717 /* Any absolute non-dynamic relocations against an indirect or weak
12718 definition will be against the target symbol. */
12719 if (indmips->has_static_relocs)
12720 dirmips->has_static_relocs = TRUE;
12721
b49e97c9
TS
12722 if (ind->root.type != bfd_link_hash_indirect)
12723 return;
12724
b49e97c9
TS
12725 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12726 if (indmips->readonly_reloc)
b34976b6 12727 dirmips->readonly_reloc = TRUE;
b49e97c9 12728 if (indmips->no_fn_stub)
b34976b6 12729 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
12730 if (indmips->fn_stub)
12731 {
12732 dirmips->fn_stub = indmips->fn_stub;
12733 indmips->fn_stub = NULL;
12734 }
12735 if (indmips->need_fn_stub)
12736 {
12737 dirmips->need_fn_stub = TRUE;
12738 indmips->need_fn_stub = FALSE;
12739 }
12740 if (indmips->call_stub)
12741 {
12742 dirmips->call_stub = indmips->call_stub;
12743 indmips->call_stub = NULL;
12744 }
12745 if (indmips->call_fp_stub)
12746 {
12747 dirmips->call_fp_stub = indmips->call_fp_stub;
12748 indmips->call_fp_stub = NULL;
12749 }
634835ae
RS
12750 if (indmips->global_got_area < dirmips->global_got_area)
12751 dirmips->global_got_area = indmips->global_got_area;
12752 if (indmips->global_got_area < GGA_NONE)
12753 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
12754 if (indmips->has_nonpic_branches)
12755 dirmips->has_nonpic_branches = TRUE;
b49e97c9 12756}
47275900
MR
12757
12758/* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
12759 to hide it. It has to remain global (it will also be protected) so as to
12760 be assigned a global GOT entry, which will then remain unchanged at load
12761 time. */
12762
12763void
12764_bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
12765 struct elf_link_hash_entry *entry,
12766 bfd_boolean force_local)
12767{
12768 struct mips_elf_link_hash_table *htab;
12769
12770 htab = mips_elf_hash_table (info);
12771 BFD_ASSERT (htab != NULL);
12772 if (htab->use_absolute_zero
12773 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
12774 return;
12775
12776 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
12777}
b49e97c9 12778\f
d01414a5
TS
12779#define PDR_SIZE 32
12780
b34976b6 12781bfd_boolean
9719ad41
RS
12782_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12783 struct bfd_link_info *info)
d01414a5
TS
12784{
12785 asection *o;
b34976b6 12786 bfd_boolean ret = FALSE;
d01414a5
TS
12787 unsigned char *tdata;
12788 size_t i, skip;
12789
12790 o = bfd_get_section_by_name (abfd, ".pdr");
12791 if (! o)
b34976b6 12792 return FALSE;
eea6121a 12793 if (o->size == 0)
b34976b6 12794 return FALSE;
eea6121a 12795 if (o->size % PDR_SIZE != 0)
b34976b6 12796 return FALSE;
d01414a5
TS
12797 if (o->output_section != NULL
12798 && bfd_is_abs_section (o->output_section))
b34976b6 12799 return FALSE;
d01414a5 12800
eea6121a 12801 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 12802 if (! tdata)
b34976b6 12803 return FALSE;
d01414a5 12804
9719ad41 12805 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 12806 info->keep_memory);
d01414a5
TS
12807 if (!cookie->rels)
12808 {
12809 free (tdata);
b34976b6 12810 return FALSE;
d01414a5
TS
12811 }
12812
12813 cookie->rel = cookie->rels;
12814 cookie->relend = cookie->rels + o->reloc_count;
12815
eea6121a 12816 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 12817 {
c152c796 12818 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
12819 {
12820 tdata[i] = 1;
12821 skip ++;
12822 }
12823 }
12824
12825 if (skip != 0)
12826 {
f0abc2a1 12827 mips_elf_section_data (o)->u.tdata = tdata;
e034b2cc
MR
12828 if (o->rawsize == 0)
12829 o->rawsize = o->size;
eea6121a 12830 o->size -= skip * PDR_SIZE;
b34976b6 12831 ret = TRUE;
d01414a5
TS
12832 }
12833 else
12834 free (tdata);
12835
12836 if (! info->keep_memory)
12837 free (cookie->rels);
12838
12839 return ret;
12840}
12841
b34976b6 12842bfd_boolean
9719ad41 12843_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
12844{
12845 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
12846 return TRUE;
12847 return FALSE;
53bfd6b4 12848}
d01414a5 12849
b34976b6 12850bfd_boolean
c7b8f16e
JB
12851_bfd_mips_elf_write_section (bfd *output_bfd,
12852 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
07d6d2b8 12853 asection *sec, bfd_byte *contents)
d01414a5
TS
12854{
12855 bfd_byte *to, *from, *end;
12856 int i;
12857
12858 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 12859 return FALSE;
d01414a5 12860
f0abc2a1 12861 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 12862 return FALSE;
d01414a5
TS
12863
12864 to = contents;
eea6121a 12865 end = contents + sec->size;
d01414a5
TS
12866 for (from = contents, i = 0;
12867 from < end;
12868 from += PDR_SIZE, i++)
12869 {
f0abc2a1 12870 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
12871 continue;
12872 if (to != from)
12873 memcpy (to, from, PDR_SIZE);
12874 to += PDR_SIZE;
12875 }
12876 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 12877 sec->output_offset, sec->size);
b34976b6 12878 return TRUE;
d01414a5 12879}
53bfd6b4 12880\f
df58fc94
RS
12881/* microMIPS code retains local labels for linker relaxation. Omit them
12882 from output by default for clarity. */
12883
12884bfd_boolean
12885_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
12886{
12887 return _bfd_elf_is_local_label_name (abfd, sym->name);
12888}
12889
b49e97c9
TS
12890/* MIPS ELF uses a special find_nearest_line routine in order the
12891 handle the ECOFF debugging information. */
12892
12893struct mips_elf_find_line
12894{
12895 struct ecoff_debug_info d;
12896 struct ecoff_find_line i;
12897};
12898
b34976b6 12899bfd_boolean
fb167eb2
AM
12900_bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
12901 asection *section, bfd_vma offset,
9719ad41
RS
12902 const char **filename_ptr,
12903 const char **functionname_ptr,
fb167eb2
AM
12904 unsigned int *line_ptr,
12905 unsigned int *discriminator_ptr)
b49e97c9
TS
12906{
12907 asection *msec;
12908
fb167eb2 12909 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
b49e97c9 12910 filename_ptr, functionname_ptr,
fb167eb2
AM
12911 line_ptr, discriminator_ptr,
12912 dwarf_debug_sections,
12913 ABI_64_P (abfd) ? 8 : 0,
46d09186
NC
12914 &elf_tdata (abfd)->dwarf2_find_line_info)
12915 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
12916 filename_ptr, functionname_ptr,
12917 line_ptr))
12918 {
12919 /* PR 22789: If the function name or filename was not found through
12920 the debug information, then try an ordinary lookup instead. */
12921 if ((functionname_ptr != NULL && *functionname_ptr == NULL)
12922 || (filename_ptr != NULL && *filename_ptr == NULL))
12923 {
12924 /* Do not override already discovered names. */
12925 if (functionname_ptr != NULL && *functionname_ptr != NULL)
12926 functionname_ptr = NULL;
b49e97c9 12927
46d09186
NC
12928 if (filename_ptr != NULL && *filename_ptr != NULL)
12929 filename_ptr = NULL;
12930
12931 _bfd_elf_find_function (abfd, symbols, section, offset,
12932 filename_ptr, functionname_ptr);
12933 }
12934
12935 return TRUE;
12936 }
b49e97c9
TS
12937
12938 msec = bfd_get_section_by_name (abfd, ".mdebug");
12939 if (msec != NULL)
12940 {
12941 flagword origflags;
12942 struct mips_elf_find_line *fi;
12943 const struct ecoff_debug_swap * const swap =
12944 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
12945
12946 /* If we are called during a link, mips_elf_final_link may have
12947 cleared the SEC_HAS_CONTENTS field. We force it back on here
12948 if appropriate (which it normally will be). */
12949 origflags = msec->flags;
12950 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
12951 msec->flags |= SEC_HAS_CONTENTS;
12952
698600e4 12953 fi = mips_elf_tdata (abfd)->find_line_info;
b49e97c9
TS
12954 if (fi == NULL)
12955 {
12956 bfd_size_type external_fdr_size;
12957 char *fraw_src;
12958 char *fraw_end;
12959 struct fdr *fdr_ptr;
12960 bfd_size_type amt = sizeof (struct mips_elf_find_line);
12961
9719ad41 12962 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
12963 if (fi == NULL)
12964 {
12965 msec->flags = origflags;
b34976b6 12966 return FALSE;
b49e97c9
TS
12967 }
12968
12969 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
12970 {
12971 msec->flags = origflags;
b34976b6 12972 return FALSE;
b49e97c9
TS
12973 }
12974
12975 /* Swap in the FDR information. */
12976 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 12977 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
12978 if (fi->d.fdr == NULL)
12979 {
12980 msec->flags = origflags;
b34976b6 12981 return FALSE;
b49e97c9
TS
12982 }
12983 external_fdr_size = swap->external_fdr_size;
12984 fdr_ptr = fi->d.fdr;
12985 fraw_src = (char *) fi->d.external_fdr;
12986 fraw_end = (fraw_src
12987 + fi->d.symbolic_header.ifdMax * external_fdr_size);
12988 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 12989 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9 12990
698600e4 12991 mips_elf_tdata (abfd)->find_line_info = fi;
b49e97c9
TS
12992
12993 /* Note that we don't bother to ever free this information.
07d6d2b8
AM
12994 find_nearest_line is either called all the time, as in
12995 objdump -l, so the information should be saved, or it is
12996 rarely called, as in ld error messages, so the memory
12997 wasted is unimportant. Still, it would probably be a
12998 good idea for free_cached_info to throw it away. */
b49e97c9
TS
12999 }
13000
13001 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13002 &fi->i, filename_ptr, functionname_ptr,
13003 line_ptr))
13004 {
13005 msec->flags = origflags;
b34976b6 13006 return TRUE;
b49e97c9
TS
13007 }
13008
13009 msec->flags = origflags;
13010 }
13011
13012 /* Fall back on the generic ELF find_nearest_line routine. */
13013
fb167eb2 13014 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
b49e97c9 13015 filename_ptr, functionname_ptr,
fb167eb2 13016 line_ptr, discriminator_ptr);
b49e97c9 13017}
4ab527b0
FF
13018
13019bfd_boolean
13020_bfd_mips_elf_find_inliner_info (bfd *abfd,
13021 const char **filename_ptr,
13022 const char **functionname_ptr,
13023 unsigned int *line_ptr)
13024{
13025 bfd_boolean found;
13026 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13027 functionname_ptr, line_ptr,
13028 & elf_tdata (abfd)->dwarf2_find_line_info);
13029 return found;
13030}
13031
b49e97c9
TS
13032\f
13033/* When are writing out the .options or .MIPS.options section,
13034 remember the bytes we are writing out, so that we can install the
13035 GP value in the section_processing routine. */
13036
b34976b6 13037bfd_boolean
9719ad41
RS
13038_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13039 const void *location,
13040 file_ptr offset, bfd_size_type count)
b49e97c9 13041{
cc2e31b9 13042 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
13043 {
13044 bfd_byte *c;
13045
13046 if (elf_section_data (section) == NULL)
13047 {
13048 bfd_size_type amt = sizeof (struct bfd_elf_section_data);
9719ad41 13049 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 13050 if (elf_section_data (section) == NULL)
b34976b6 13051 return FALSE;
b49e97c9 13052 }
f0abc2a1 13053 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
13054 if (c == NULL)
13055 {
eea6121a 13056 c = bfd_zalloc (abfd, section->size);
b49e97c9 13057 if (c == NULL)
b34976b6 13058 return FALSE;
f0abc2a1 13059 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
13060 }
13061
9719ad41 13062 memcpy (c + offset, location, count);
b49e97c9
TS
13063 }
13064
13065 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13066 count);
13067}
13068
13069/* This is almost identical to bfd_generic_get_... except that some
13070 MIPS relocations need to be handled specially. Sigh. */
13071
13072bfd_byte *
9719ad41
RS
13073_bfd_elf_mips_get_relocated_section_contents
13074 (bfd *abfd,
13075 struct bfd_link_info *link_info,
13076 struct bfd_link_order *link_order,
13077 bfd_byte *data,
13078 bfd_boolean relocatable,
13079 asymbol **symbols)
b49e97c9
TS
13080{
13081 /* Get enough memory to hold the stuff */
13082 bfd *input_bfd = link_order->u.indirect.section->owner;
13083 asection *input_section = link_order->u.indirect.section;
eea6121a 13084 bfd_size_type sz;
b49e97c9
TS
13085
13086 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13087 arelent **reloc_vector = NULL;
13088 long reloc_count;
13089
13090 if (reloc_size < 0)
13091 goto error_return;
13092
9719ad41 13093 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
13094 if (reloc_vector == NULL && reloc_size != 0)
13095 goto error_return;
13096
13097 /* read in the section */
eea6121a
AM
13098 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
13099 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
13100 goto error_return;
13101
b49e97c9
TS
13102 reloc_count = bfd_canonicalize_reloc (input_bfd,
13103 input_section,
13104 reloc_vector,
13105 symbols);
13106 if (reloc_count < 0)
13107 goto error_return;
13108
13109 if (reloc_count > 0)
13110 {
13111 arelent **parent;
13112 /* for mips */
13113 int gp_found;
13114 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13115
13116 {
13117 struct bfd_hash_entry *h;
13118 struct bfd_link_hash_entry *lh;
13119 /* Skip all this stuff if we aren't mixing formats. */
13120 if (abfd && input_bfd
13121 && abfd->xvec == input_bfd->xvec)
13122 lh = 0;
13123 else
13124 {
b34976b6 13125 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
13126 lh = (struct bfd_link_hash_entry *) h;
13127 }
13128 lookup:
13129 if (lh)
13130 {
13131 switch (lh->type)
13132 {
13133 case bfd_link_hash_undefined:
13134 case bfd_link_hash_undefweak:
13135 case bfd_link_hash_common:
13136 gp_found = 0;
13137 break;
13138 case bfd_link_hash_defined:
13139 case bfd_link_hash_defweak:
13140 gp_found = 1;
13141 gp = lh->u.def.value;
13142 break;
13143 case bfd_link_hash_indirect:
13144 case bfd_link_hash_warning:
13145 lh = lh->u.i.link;
13146 /* @@FIXME ignoring warning for now */
13147 goto lookup;
13148 case bfd_link_hash_new:
13149 default:
13150 abort ();
13151 }
13152 }
13153 else
13154 gp_found = 0;
13155 }
13156 /* end mips */
9719ad41 13157 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 13158 {
9719ad41 13159 char *error_message = NULL;
b49e97c9
TS
13160 bfd_reloc_status_type r;
13161
13162 /* Specific to MIPS: Deal with relocation types that require
13163 knowing the gp of the output bfd. */
13164 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 13165
8236346f
EC
13166 /* If we've managed to find the gp and have a special
13167 function for the relocation then go ahead, else default
13168 to the generic handling. */
13169 if (gp_found
13170 && (*parent)->howto->special_function
13171 == _bfd_mips_elf32_gprel16_reloc)
13172 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
13173 input_section, relocatable,
13174 data, gp);
13175 else
86324f90 13176 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
13177 input_section,
13178 relocatable ? abfd : NULL,
13179 &error_message);
b49e97c9 13180
1049f94e 13181 if (relocatable)
b49e97c9
TS
13182 {
13183 asection *os = input_section->output_section;
13184
13185 /* A partial link, so keep the relocs */
13186 os->orelocation[os->reloc_count] = *parent;
13187 os->reloc_count++;
13188 }
13189
13190 if (r != bfd_reloc_ok)
13191 {
13192 switch (r)
13193 {
13194 case bfd_reloc_undefined:
1a72702b
AM
13195 (*link_info->callbacks->undefined_symbol)
13196 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13197 input_bfd, input_section, (*parent)->address, TRUE);
b49e97c9
TS
13198 break;
13199 case bfd_reloc_dangerous:
9719ad41 13200 BFD_ASSERT (error_message != NULL);
1a72702b
AM
13201 (*link_info->callbacks->reloc_dangerous)
13202 (link_info, error_message,
13203 input_bfd, input_section, (*parent)->address);
b49e97c9
TS
13204 break;
13205 case bfd_reloc_overflow:
1a72702b
AM
13206 (*link_info->callbacks->reloc_overflow)
13207 (link_info, NULL,
13208 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13209 (*parent)->howto->name, (*parent)->addend,
13210 input_bfd, input_section, (*parent)->address);
b49e97c9
TS
13211 break;
13212 case bfd_reloc_outofrange:
13213 default:
13214 abort ();
13215 break;
13216 }
13217
13218 }
13219 }
13220 }
13221 if (reloc_vector != NULL)
13222 free (reloc_vector);
13223 return data;
13224
13225error_return:
13226 if (reloc_vector != NULL)
13227 free (reloc_vector);
13228 return NULL;
13229}
13230\f
df58fc94
RS
13231static bfd_boolean
13232mips_elf_relax_delete_bytes (bfd *abfd,
13233 asection *sec, bfd_vma addr, int count)
13234{
13235 Elf_Internal_Shdr *symtab_hdr;
13236 unsigned int sec_shndx;
13237 bfd_byte *contents;
13238 Elf_Internal_Rela *irel, *irelend;
13239 Elf_Internal_Sym *isym;
13240 Elf_Internal_Sym *isymend;
13241 struct elf_link_hash_entry **sym_hashes;
13242 struct elf_link_hash_entry **end_hashes;
13243 struct elf_link_hash_entry **start_hashes;
13244 unsigned int symcount;
13245
13246 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13247 contents = elf_section_data (sec)->this_hdr.contents;
13248
13249 irel = elf_section_data (sec)->relocs;
13250 irelend = irel + sec->reloc_count;
13251
13252 /* Actually delete the bytes. */
13253 memmove (contents + addr, contents + addr + count,
13254 (size_t) (sec->size - addr - count));
13255 sec->size -= count;
13256
13257 /* Adjust all the relocs. */
13258 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13259 {
13260 /* Get the new reloc address. */
13261 if (irel->r_offset > addr)
13262 irel->r_offset -= count;
13263 }
13264
13265 BFD_ASSERT (addr % 2 == 0);
13266 BFD_ASSERT (count % 2 == 0);
13267
13268 /* Adjust the local symbols defined in this section. */
13269 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13270 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13271 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2309ddf2 13272 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
df58fc94
RS
13273 isym->st_value -= count;
13274
13275 /* Now adjust the global symbols defined in this section. */
13276 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13277 - symtab_hdr->sh_info);
13278 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13279 end_hashes = sym_hashes + symcount;
13280
13281 for (; sym_hashes < end_hashes; sym_hashes++)
13282 {
13283 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13284
13285 if ((sym_hash->root.type == bfd_link_hash_defined
13286 || sym_hash->root.type == bfd_link_hash_defweak)
13287 && sym_hash->root.u.def.section == sec)
13288 {
2309ddf2 13289 bfd_vma value = sym_hash->root.u.def.value;
df58fc94 13290
df58fc94
RS
13291 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13292 value &= MINUS_TWO;
13293 if (value > addr)
13294 sym_hash->root.u.def.value -= count;
13295 }
13296 }
13297
13298 return TRUE;
13299}
13300
13301
13302/* Opcodes needed for microMIPS relaxation as found in
13303 opcodes/micromips-opc.c. */
13304
13305struct opcode_descriptor {
13306 unsigned long match;
13307 unsigned long mask;
13308};
13309
13310/* The $ra register aka $31. */
13311
13312#define RA 31
13313
13314/* 32-bit instruction format register fields. */
13315
13316#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13317#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13318
13319/* Check if a 5-bit register index can be abbreviated to 3 bits. */
13320
13321#define OP16_VALID_REG(r) \
13322 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13323
13324
13325/* 32-bit and 16-bit branches. */
13326
13327static const struct opcode_descriptor b_insns_32[] = {
13328 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13329 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13330 { 0, 0 } /* End marker for find_match(). */
13331};
13332
13333static const struct opcode_descriptor bc_insn_32 =
13334 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13335
13336static const struct opcode_descriptor bz_insn_32 =
13337 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13338
13339static const struct opcode_descriptor bzal_insn_32 =
13340 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13341
13342static const struct opcode_descriptor beq_insn_32 =
13343 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13344
13345static const struct opcode_descriptor b_insn_16 =
13346 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13347
13348static const struct opcode_descriptor bz_insn_16 =
c088dedf 13349 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
df58fc94
RS
13350
13351
13352/* 32-bit and 16-bit branch EQ and NE zero. */
13353
13354/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13355 eq and second the ne. This convention is used when replacing a
13356 32-bit BEQ/BNE with the 16-bit version. */
13357
13358#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13359
13360static const struct opcode_descriptor bz_rs_insns_32[] = {
13361 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13362 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13363 { 0, 0 } /* End marker for find_match(). */
13364};
13365
13366static const struct opcode_descriptor bz_rt_insns_32[] = {
13367 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13368 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13369 { 0, 0 } /* End marker for find_match(). */
13370};
13371
13372static const struct opcode_descriptor bzc_insns_32[] = {
13373 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13374 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13375 { 0, 0 } /* End marker for find_match(). */
13376};
13377
13378static const struct opcode_descriptor bz_insns_16[] = {
13379 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13380 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13381 { 0, 0 } /* End marker for find_match(). */
13382};
13383
13384/* Switch between a 5-bit register index and its 3-bit shorthand. */
13385
e67f83e5 13386#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
eb6b0cf4 13387#define BZ16_REG_FIELD(r) (((r) & 7) << 7)
df58fc94
RS
13388
13389
13390/* 32-bit instructions with a delay slot. */
13391
13392static const struct opcode_descriptor jal_insn_32_bd16 =
13393 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13394
13395static const struct opcode_descriptor jal_insn_32_bd32 =
13396 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13397
13398static const struct opcode_descriptor jal_x_insn_32_bd32 =
13399 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13400
13401static const struct opcode_descriptor j_insn_32 =
13402 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13403
13404static const struct opcode_descriptor jalr_insn_32 =
13405 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13406
13407/* This table can be compacted, because no opcode replacement is made. */
13408
13409static const struct opcode_descriptor ds_insns_32_bd16[] = {
13410 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13411
13412 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13413 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13414
13415 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13416 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13417 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13418 { 0, 0 } /* End marker for find_match(). */
13419};
13420
13421/* This table can be compacted, because no opcode replacement is made. */
13422
13423static const struct opcode_descriptor ds_insns_32_bd32[] = {
13424 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13425
13426 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13427 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13428 { 0, 0 } /* End marker for find_match(). */
13429};
13430
13431
13432/* 16-bit instructions with a delay slot. */
13433
13434static const struct opcode_descriptor jalr_insn_16_bd16 =
13435 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13436
13437static const struct opcode_descriptor jalr_insn_16_bd32 =
13438 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13439
13440static const struct opcode_descriptor jr_insn_16 =
13441 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13442
13443#define JR16_REG(opcode) ((opcode) & 0x1f)
13444
13445/* This table can be compacted, because no opcode replacement is made. */
13446
13447static const struct opcode_descriptor ds_insns_16_bd16[] = {
13448 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13449
13450 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13451 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13452 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13453 { 0, 0 } /* End marker for find_match(). */
13454};
13455
13456
13457/* LUI instruction. */
13458
13459static const struct opcode_descriptor lui_insn =
13460 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13461
13462
13463/* ADDIU instruction. */
13464
13465static const struct opcode_descriptor addiu_insn =
13466 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13467
13468static const struct opcode_descriptor addiupc_insn =
13469 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13470
13471#define ADDIUPC_REG_FIELD(r) \
13472 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13473
13474
13475/* Relaxable instructions in a JAL delay slot: MOVE. */
13476
13477/* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13478 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13479#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13480#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13481
13482#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13483#define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13484
13485static const struct opcode_descriptor move_insns_32[] = {
df58fc94 13486 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
40fc1451 13487 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
df58fc94
RS
13488 { 0, 0 } /* End marker for find_match(). */
13489};
13490
13491static const struct opcode_descriptor move_insn_16 =
13492 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13493
13494
13495/* NOP instructions. */
13496
13497static const struct opcode_descriptor nop_insn_32 =
13498 { /* "nop", "", */ 0x00000000, 0xffffffff };
13499
13500static const struct opcode_descriptor nop_insn_16 =
13501 { /* "nop", "", */ 0x0c00, 0xffff };
13502
13503
13504/* Instruction match support. */
13505
13506#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13507
13508static int
13509find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13510{
13511 unsigned long indx;
13512
13513 for (indx = 0; insn[indx].mask != 0; indx++)
13514 if (MATCH (opcode, insn[indx]))
13515 return indx;
13516
13517 return -1;
13518}
13519
13520
13521/* Branch and delay slot decoding support. */
13522
13523/* If PTR points to what *might* be a 16-bit branch or jump, then
13524 return the minimum length of its delay slot, otherwise return 0.
13525 Non-zero results are not definitive as we might be checking against
13526 the second half of another instruction. */
13527
13528static int
13529check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13530{
13531 unsigned long opcode;
13532 int bdsize;
13533
13534 opcode = bfd_get_16 (abfd, ptr);
13535 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13536 /* 16-bit branch/jump with a 32-bit delay slot. */
13537 bdsize = 4;
13538 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13539 || find_match (opcode, ds_insns_16_bd16) >= 0)
13540 /* 16-bit branch/jump with a 16-bit delay slot. */
13541 bdsize = 2;
13542 else
13543 /* No delay slot. */
13544 bdsize = 0;
13545
13546 return bdsize;
13547}
13548
13549/* If PTR points to what *might* be a 32-bit branch or jump, then
13550 return the minimum length of its delay slot, otherwise return 0.
13551 Non-zero results are not definitive as we might be checking against
13552 the second half of another instruction. */
13553
13554static int
13555check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13556{
13557 unsigned long opcode;
13558 int bdsize;
13559
d21911ea 13560 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13561 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13562 /* 32-bit branch/jump with a 32-bit delay slot. */
13563 bdsize = 4;
13564 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13565 /* 32-bit branch/jump with a 16-bit delay slot. */
13566 bdsize = 2;
13567 else
13568 /* No delay slot. */
13569 bdsize = 0;
13570
13571 return bdsize;
13572}
13573
13574/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13575 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13576
13577static bfd_boolean
13578check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13579{
13580 unsigned long opcode;
13581
13582 opcode = bfd_get_16 (abfd, ptr);
13583 if (MATCH (opcode, b_insn_16)
13584 /* B16 */
13585 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13586 /* JR16 */
13587 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13588 /* BEQZ16, BNEZ16 */
13589 || (MATCH (opcode, jalr_insn_16_bd32)
13590 /* JALR16 */
13591 && reg != JR16_REG (opcode) && reg != RA))
13592 return TRUE;
13593
13594 return FALSE;
13595}
13596
13597/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13598 then return TRUE, otherwise FALSE. */
13599
f41e5fcc 13600static bfd_boolean
df58fc94
RS
13601check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13602{
13603 unsigned long opcode;
13604
d21911ea 13605 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13606 if (MATCH (opcode, j_insn_32)
13607 /* J */
13608 || MATCH (opcode, bc_insn_32)
13609 /* BC1F, BC1T, BC2F, BC2T */
13610 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13611 /* JAL, JALX */
13612 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13613 /* BGEZ, BGTZ, BLEZ, BLTZ */
13614 || (MATCH (opcode, bzal_insn_32)
13615 /* BGEZAL, BLTZAL */
13616 && reg != OP32_SREG (opcode) && reg != RA)
13617 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13618 /* JALR, JALR.HB, BEQ, BNE */
13619 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13620 return TRUE;
13621
13622 return FALSE;
13623}
13624
80cab405
MR
13625/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13626 IRELEND) at OFFSET indicate that there must be a compact branch there,
13627 then return TRUE, otherwise FALSE. */
df58fc94
RS
13628
13629static bfd_boolean
80cab405
MR
13630check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13631 const Elf_Internal_Rela *internal_relocs,
13632 const Elf_Internal_Rela *irelend)
df58fc94 13633{
80cab405
MR
13634 const Elf_Internal_Rela *irel;
13635 unsigned long opcode;
13636
d21911ea 13637 opcode = bfd_get_micromips_32 (abfd, ptr);
80cab405
MR
13638 if (find_match (opcode, bzc_insns_32) < 0)
13639 return FALSE;
df58fc94
RS
13640
13641 for (irel = internal_relocs; irel < irelend; irel++)
80cab405
MR
13642 if (irel->r_offset == offset
13643 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13644 return TRUE;
13645
df58fc94
RS
13646 return FALSE;
13647}
80cab405
MR
13648
13649/* Bitsize checking. */
13650#define IS_BITSIZE(val, N) \
13651 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13652 - (1ULL << ((N) - 1))) == (val))
13653
df58fc94
RS
13654\f
13655bfd_boolean
13656_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13657 struct bfd_link_info *link_info,
13658 bfd_boolean *again)
13659{
833794fc 13660 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
df58fc94
RS
13661 Elf_Internal_Shdr *symtab_hdr;
13662 Elf_Internal_Rela *internal_relocs;
13663 Elf_Internal_Rela *irel, *irelend;
13664 bfd_byte *contents = NULL;
13665 Elf_Internal_Sym *isymbuf = NULL;
13666
13667 /* Assume nothing changes. */
13668 *again = FALSE;
13669
13670 /* We don't have to do anything for a relocatable link, if
13671 this section does not have relocs, or if this is not a
13672 code section. */
13673
0e1862bb 13674 if (bfd_link_relocatable (link_info)
df58fc94
RS
13675 || (sec->flags & SEC_RELOC) == 0
13676 || sec->reloc_count == 0
13677 || (sec->flags & SEC_CODE) == 0)
13678 return TRUE;
13679
13680 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13681
13682 /* Get a copy of the native relocations. */
13683 internal_relocs = (_bfd_elf_link_read_relocs
2c3fc389 13684 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
df58fc94
RS
13685 link_info->keep_memory));
13686 if (internal_relocs == NULL)
13687 goto error_return;
13688
13689 /* Walk through them looking for relaxing opportunities. */
13690 irelend = internal_relocs + sec->reloc_count;
13691 for (irel = internal_relocs; irel < irelend; irel++)
13692 {
13693 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13694 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13695 bfd_boolean target_is_micromips_code_p;
13696 unsigned long opcode;
13697 bfd_vma symval;
13698 bfd_vma pcrval;
2309ddf2 13699 bfd_byte *ptr;
df58fc94
RS
13700 int fndopc;
13701
13702 /* The number of bytes to delete for relaxation and from where
07d6d2b8 13703 to delete these bytes starting at irel->r_offset. */
df58fc94
RS
13704 int delcnt = 0;
13705 int deloff = 0;
13706
13707 /* If this isn't something that can be relaxed, then ignore
07d6d2b8 13708 this reloc. */
df58fc94
RS
13709 if (r_type != R_MICROMIPS_HI16
13710 && r_type != R_MICROMIPS_PC16_S1
2309ddf2 13711 && r_type != R_MICROMIPS_26_S1)
df58fc94
RS
13712 continue;
13713
13714 /* Get the section contents if we haven't done so already. */
13715 if (contents == NULL)
13716 {
13717 /* Get cached copy if it exists. */
13718 if (elf_section_data (sec)->this_hdr.contents != NULL)
13719 contents = elf_section_data (sec)->this_hdr.contents;
13720 /* Go get them off disk. */
13721 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13722 goto error_return;
13723 }
2309ddf2 13724 ptr = contents + irel->r_offset;
df58fc94
RS
13725
13726 /* Read this BFD's local symbols if we haven't done so already. */
13727 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13728 {
13729 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13730 if (isymbuf == NULL)
13731 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13732 symtab_hdr->sh_info, 0,
13733 NULL, NULL, NULL);
13734 if (isymbuf == NULL)
13735 goto error_return;
13736 }
13737
13738 /* Get the value of the symbol referred to by the reloc. */
13739 if (r_symndx < symtab_hdr->sh_info)
13740 {
13741 /* A local symbol. */
13742 Elf_Internal_Sym *isym;
13743 asection *sym_sec;
13744
13745 isym = isymbuf + r_symndx;
13746 if (isym->st_shndx == SHN_UNDEF)
13747 sym_sec = bfd_und_section_ptr;
13748 else if (isym->st_shndx == SHN_ABS)
13749 sym_sec = bfd_abs_section_ptr;
13750 else if (isym->st_shndx == SHN_COMMON)
13751 sym_sec = bfd_com_section_ptr;
13752 else
13753 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13754 symval = (isym->st_value
13755 + sym_sec->output_section->vma
13756 + sym_sec->output_offset);
13757 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13758 }
13759 else
13760 {
13761 unsigned long indx;
13762 struct elf_link_hash_entry *h;
13763
13764 /* An external symbol. */
13765 indx = r_symndx - symtab_hdr->sh_info;
13766 h = elf_sym_hashes (abfd)[indx];
13767 BFD_ASSERT (h != NULL);
13768
13769 if (h->root.type != bfd_link_hash_defined
13770 && h->root.type != bfd_link_hash_defweak)
13771 /* This appears to be a reference to an undefined
13772 symbol. Just ignore it -- it will be caught by the
13773 regular reloc processing. */
13774 continue;
13775
13776 symval = (h->root.u.def.value
13777 + h->root.u.def.section->output_section->vma
13778 + h->root.u.def.section->output_offset);
13779 target_is_micromips_code_p = (!h->needs_plt
13780 && ELF_ST_IS_MICROMIPS (h->other));
13781 }
13782
13783
13784 /* For simplicity of coding, we are going to modify the
07d6d2b8
AM
13785 section contents, the section relocs, and the BFD symbol
13786 table. We must tell the rest of the code not to free up this
13787 information. It would be possible to instead create a table
13788 of changes which have to be made, as is done in coff-mips.c;
13789 that would be more work, but would require less memory when
13790 the linker is run. */
df58fc94
RS
13791
13792 /* Only 32-bit instructions relaxed. */
13793 if (irel->r_offset + 4 > sec->size)
13794 continue;
13795
d21911ea 13796 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13797
13798 /* This is the pc-relative distance from the instruction the
07d6d2b8 13799 relocation is applied to, to the symbol referred. */
df58fc94
RS
13800 pcrval = (symval
13801 - (sec->output_section->vma + sec->output_offset)
13802 - irel->r_offset);
13803
13804 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
07d6d2b8
AM
13805 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13806 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
df58fc94 13807
07d6d2b8 13808 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
df58fc94 13809
07d6d2b8
AM
13810 where pcrval has first to be adjusted to apply against the LO16
13811 location (we make the adjustment later on, when we have figured
13812 out the offset). */
df58fc94
RS
13813 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13814 {
80cab405 13815 bfd_boolean bzc = FALSE;
df58fc94
RS
13816 unsigned long nextopc;
13817 unsigned long reg;
13818 bfd_vma offset;
13819
13820 /* Give up if the previous reloc was a HI16 against this symbol
13821 too. */
13822 if (irel > internal_relocs
13823 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13824 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13825 continue;
13826
13827 /* Or if the next reloc is not a LO16 against this symbol. */
13828 if (irel + 1 >= irelend
13829 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13830 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13831 continue;
13832
13833 /* Or if the second next reloc is a LO16 against this symbol too. */
13834 if (irel + 2 >= irelend
13835 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13836 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13837 continue;
13838
80cab405
MR
13839 /* See if the LUI instruction *might* be in a branch delay slot.
13840 We check whether what looks like a 16-bit branch or jump is
13841 actually an immediate argument to a compact branch, and let
13842 it through if so. */
df58fc94 13843 if (irel->r_offset >= 2
2309ddf2 13844 && check_br16_dslot (abfd, ptr - 2)
df58fc94 13845 && !(irel->r_offset >= 4
80cab405
MR
13846 && (bzc = check_relocated_bzc (abfd,
13847 ptr - 4, irel->r_offset - 4,
13848 internal_relocs, irelend))))
df58fc94
RS
13849 continue;
13850 if (irel->r_offset >= 4
80cab405 13851 && !bzc
2309ddf2 13852 && check_br32_dslot (abfd, ptr - 4))
df58fc94
RS
13853 continue;
13854
13855 reg = OP32_SREG (opcode);
13856
13857 /* We only relax adjacent instructions or ones separated with
13858 a branch or jump that has a delay slot. The branch or jump
13859 must not fiddle with the register used to hold the address.
13860 Subtract 4 for the LUI itself. */
13861 offset = irel[1].r_offset - irel[0].r_offset;
13862 switch (offset - 4)
13863 {
13864 case 0:
13865 break;
13866 case 2:
2309ddf2 13867 if (check_br16 (abfd, ptr + 4, reg))
df58fc94
RS
13868 break;
13869 continue;
13870 case 4:
2309ddf2 13871 if (check_br32 (abfd, ptr + 4, reg))
df58fc94
RS
13872 break;
13873 continue;
13874 default:
13875 continue;
13876 }
13877
d21911ea 13878 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
df58fc94
RS
13879
13880 /* Give up unless the same register is used with both
13881 relocations. */
13882 if (OP32_SREG (nextopc) != reg)
13883 continue;
13884
13885 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
13886 and rounding up to take masking of the two LSBs into account. */
13887 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
13888
13889 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
13890 if (IS_BITSIZE (symval, 16))
13891 {
13892 /* Fix the relocation's type. */
13893 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
13894
13895 /* Instructions using R_MICROMIPS_LO16 have the base or
07d6d2b8
AM
13896 source register in bits 20:16. This register becomes $0
13897 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
df58fc94
RS
13898 nextopc &= ~0x001f0000;
13899 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
13900 contents + irel[1].r_offset);
13901 }
13902
13903 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
13904 We add 4 to take LUI deletion into account while checking
13905 the PC-relative distance. */
13906 else if (symval % 4 == 0
13907 && IS_BITSIZE (pcrval + 4, 25)
13908 && MATCH (nextopc, addiu_insn)
13909 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
13910 && OP16_VALID_REG (OP32_TREG (nextopc)))
13911 {
13912 /* Fix the relocation's type. */
13913 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
13914
13915 /* Replace ADDIU with the ADDIUPC version. */
13916 nextopc = (addiupc_insn.match
13917 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
13918
d21911ea
MR
13919 bfd_put_micromips_32 (abfd, nextopc,
13920 contents + irel[1].r_offset);
df58fc94
RS
13921 }
13922
13923 /* Can't do anything, give up, sigh... */
13924 else
13925 continue;
13926
13927 /* Fix the relocation's type. */
13928 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
13929
13930 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
13931 delcnt = 4;
13932 deloff = 0;
13933 }
13934
13935 /* Compact branch relaxation -- due to the multitude of macros
07d6d2b8
AM
13936 employed by the compiler/assembler, compact branches are not
13937 always generated. Obviously, this can/will be fixed elsewhere,
13938 but there is no drawback in double checking it here. */
df58fc94
RS
13939 else if (r_type == R_MICROMIPS_PC16_S1
13940 && irel->r_offset + 5 < sec->size
13941 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13942 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
833794fc
MR
13943 && ((!insn32
13944 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
13945 nop_insn_16) ? 2 : 0))
13946 || (irel->r_offset + 7 < sec->size
13947 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
13948 ptr + 4),
13949 nop_insn_32) ? 4 : 0))))
df58fc94
RS
13950 {
13951 unsigned long reg;
13952
13953 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
13954
13955 /* Replace BEQZ/BNEZ with the compact version. */
13956 opcode = (bzc_insns_32[fndopc].match
13957 | BZC32_REG_FIELD (reg)
13958 | (opcode & 0xffff)); /* Addend value. */
13959
d21911ea 13960 bfd_put_micromips_32 (abfd, opcode, ptr);
df58fc94 13961
833794fc
MR
13962 /* Delete the delay slot NOP: two or four bytes from
13963 irel->offset + 4; delcnt has already been set above. */
df58fc94
RS
13964 deloff = 4;
13965 }
13966
13967 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
07d6d2b8 13968 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
13969 else if (!insn32
13970 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
13971 && IS_BITSIZE (pcrval - 2, 11)
13972 && find_match (opcode, b_insns_32) >= 0)
13973 {
13974 /* Fix the relocation's type. */
13975 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
13976
a8685210 13977 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
13978 bfd_put_16 (abfd,
13979 (b_insn_16.match
13980 | (opcode & 0x3ff)), /* Addend value. */
2309ddf2 13981 ptr);
df58fc94
RS
13982
13983 /* Delete 2 bytes from irel->r_offset + 2. */
13984 delcnt = 2;
13985 deloff = 2;
13986 }
13987
13988 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
07d6d2b8 13989 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
13990 else if (!insn32
13991 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
13992 && IS_BITSIZE (pcrval - 2, 8)
13993 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
13994 && OP16_VALID_REG (OP32_SREG (opcode)))
13995 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
13996 && OP16_VALID_REG (OP32_TREG (opcode)))))
13997 {
13998 unsigned long reg;
13999
14000 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14001
14002 /* Fix the relocation's type. */
14003 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14004
a8685210 14005 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
14006 bfd_put_16 (abfd,
14007 (bz_insns_16[fndopc].match
14008 | BZ16_REG_FIELD (reg)
14009 | (opcode & 0x7f)), /* Addend value. */
2309ddf2 14010 ptr);
df58fc94
RS
14011
14012 /* Delete 2 bytes from irel->r_offset + 2. */
14013 delcnt = 2;
14014 deloff = 2;
14015 }
14016
14017 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
833794fc
MR
14018 else if (!insn32
14019 && r_type == R_MICROMIPS_26_S1
df58fc94
RS
14020 && target_is_micromips_code_p
14021 && irel->r_offset + 7 < sec->size
14022 && MATCH (opcode, jal_insn_32_bd32))
14023 {
14024 unsigned long n32opc;
14025 bfd_boolean relaxed = FALSE;
14026
d21911ea 14027 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
df58fc94
RS
14028
14029 if (MATCH (n32opc, nop_insn_32))
14030 {
14031 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
2309ddf2 14032 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
df58fc94
RS
14033
14034 relaxed = TRUE;
14035 }
14036 else if (find_match (n32opc, move_insns_32) >= 0)
14037 {
14038 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14039 bfd_put_16 (abfd,
14040 (move_insn_16.match
14041 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14042 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
2309ddf2 14043 ptr + 4);
df58fc94
RS
14044
14045 relaxed = TRUE;
14046 }
14047 /* Other 32-bit instructions relaxable to 16-bit
14048 instructions will be handled here later. */
14049
14050 if (relaxed)
14051 {
14052 /* JAL with 32-bit delay slot that is changed to a JALS
07d6d2b8 14053 with 16-bit delay slot. */
d21911ea 14054 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
df58fc94
RS
14055
14056 /* Delete 2 bytes from irel->r_offset + 6. */
14057 delcnt = 2;
14058 deloff = 6;
14059 }
14060 }
14061
14062 if (delcnt != 0)
14063 {
14064 /* Note that we've changed the relocs, section contents, etc. */
14065 elf_section_data (sec)->relocs = internal_relocs;
14066 elf_section_data (sec)->this_hdr.contents = contents;
14067 symtab_hdr->contents = (unsigned char *) isymbuf;
14068
14069 /* Delete bytes depending on the delcnt and deloff. */
14070 if (!mips_elf_relax_delete_bytes (abfd, sec,
14071 irel->r_offset + deloff, delcnt))
14072 goto error_return;
14073
14074 /* That will change things, so we should relax again.
14075 Note that this is not required, and it may be slow. */
14076 *again = TRUE;
14077 }
14078 }
14079
14080 if (isymbuf != NULL
14081 && symtab_hdr->contents != (unsigned char *) isymbuf)
14082 {
14083 if (! link_info->keep_memory)
14084 free (isymbuf);
14085 else
14086 {
14087 /* Cache the symbols for elf_link_input_bfd. */
14088 symtab_hdr->contents = (unsigned char *) isymbuf;
14089 }
14090 }
14091
14092 if (contents != NULL
14093 && elf_section_data (sec)->this_hdr.contents != contents)
14094 {
14095 if (! link_info->keep_memory)
14096 free (contents);
14097 else
14098 {
14099 /* Cache the section contents for elf_link_input_bfd. */
14100 elf_section_data (sec)->this_hdr.contents = contents;
14101 }
14102 }
14103
14104 if (internal_relocs != NULL
14105 && elf_section_data (sec)->relocs != internal_relocs)
14106 free (internal_relocs);
14107
14108 return TRUE;
14109
14110 error_return:
14111 if (isymbuf != NULL
14112 && symtab_hdr->contents != (unsigned char *) isymbuf)
14113 free (isymbuf);
14114 if (contents != NULL
14115 && elf_section_data (sec)->this_hdr.contents != contents)
14116 free (contents);
14117 if (internal_relocs != NULL
14118 && elf_section_data (sec)->relocs != internal_relocs)
14119 free (internal_relocs);
14120
14121 return FALSE;
14122}
14123\f
b49e97c9
TS
14124/* Create a MIPS ELF linker hash table. */
14125
14126struct bfd_link_hash_table *
9719ad41 14127_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
14128{
14129 struct mips_elf_link_hash_table *ret;
14130 bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
14131
7bf52ea2 14132 ret = bfd_zmalloc (amt);
9719ad41 14133 if (ret == NULL)
b49e97c9
TS
14134 return NULL;
14135
66eb6687
AM
14136 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14137 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
14138 sizeof (struct mips_elf_link_hash_entry),
14139 MIPS_ELF_DATA))
b49e97c9 14140 {
e2d34d7d 14141 free (ret);
b49e97c9
TS
14142 return NULL;
14143 }
1bbce132
MR
14144 ret->root.init_plt_refcount.plist = NULL;
14145 ret->root.init_plt_offset.plist = NULL;
b49e97c9 14146
b49e97c9
TS
14147 return &ret->root.root;
14148}
0a44bf69
RS
14149
14150/* Likewise, but indicate that the target is VxWorks. */
14151
14152struct bfd_link_hash_table *
14153_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14154{
14155 struct bfd_link_hash_table *ret;
14156
14157 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14158 if (ret)
14159 {
14160 struct mips_elf_link_hash_table *htab;
14161
14162 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a
DJ
14163 htab->use_plts_and_copy_relocs = TRUE;
14164 htab->is_vxworks = TRUE;
0a44bf69
RS
14165 }
14166 return ret;
14167}
861fb55a
DJ
14168
14169/* A function that the linker calls if we are allowed to use PLTs
14170 and copy relocs. */
14171
14172void
14173_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14174{
14175 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
14176}
833794fc
MR
14177
14178/* A function that the linker calls to select between all or only
8b10b0b3 14179 32-bit microMIPS instructions, and between making or ignoring
47275900
MR
14180 branch relocation checks for invalid transitions between ISA modes.
14181 Also record whether we have been configured for a GNU target. */
833794fc
MR
14182
14183void
8b10b0b3 14184_bfd_mips_elf_linker_flags (struct bfd_link_info *info, bfd_boolean insn32,
47275900
MR
14185 bfd_boolean ignore_branch_isa,
14186 bfd_boolean gnu_target)
833794fc 14187{
8b10b0b3
MR
14188 mips_elf_hash_table (info)->insn32 = insn32;
14189 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
47275900 14190 mips_elf_hash_table (info)->gnu_target = gnu_target;
833794fc 14191}
b49e97c9 14192\f
c97c330b
MF
14193/* Structure for saying that BFD machine EXTENSION extends BASE. */
14194
14195struct mips_mach_extension
14196{
14197 unsigned long extension, base;
14198};
14199
14200
14201/* An array describing how BFD machines relate to one another. The entries
14202 are ordered topologically with MIPS I extensions listed last. */
14203
14204static const struct mips_mach_extension mips_mach_extensions[] =
14205{
14206 /* MIPS64r2 extensions. */
14207 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14208 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14209 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14210 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
9108bc33 14211 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
bd782c07 14212 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
ac8cb70f 14213 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
c97c330b
MF
14214
14215 /* MIPS64 extensions. */
14216 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14217 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14218 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14219
14220 /* MIPS V extensions. */
14221 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14222
14223 /* R10000 extensions. */
14224 { bfd_mach_mips12000, bfd_mach_mips10000 },
14225 { bfd_mach_mips14000, bfd_mach_mips10000 },
14226 { bfd_mach_mips16000, bfd_mach_mips10000 },
14227
14228 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14229 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14230 better to allow vr5400 and vr5500 code to be merged anyway, since
14231 many libraries will just use the core ISA. Perhaps we could add
14232 some sort of ASE flag if this ever proves a problem. */
14233 { bfd_mach_mips5500, bfd_mach_mips5400 },
14234 { bfd_mach_mips5400, bfd_mach_mips5000 },
14235
14236 /* MIPS IV extensions. */
14237 { bfd_mach_mips5, bfd_mach_mips8000 },
14238 { bfd_mach_mips10000, bfd_mach_mips8000 },
14239 { bfd_mach_mips5000, bfd_mach_mips8000 },
14240 { bfd_mach_mips7000, bfd_mach_mips8000 },
14241 { bfd_mach_mips9000, bfd_mach_mips8000 },
14242
14243 /* VR4100 extensions. */
14244 { bfd_mach_mips4120, bfd_mach_mips4100 },
14245 { bfd_mach_mips4111, bfd_mach_mips4100 },
14246
14247 /* MIPS III extensions. */
14248 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14249 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14250 { bfd_mach_mips8000, bfd_mach_mips4000 },
14251 { bfd_mach_mips4650, bfd_mach_mips4000 },
14252 { bfd_mach_mips4600, bfd_mach_mips4000 },
14253 { bfd_mach_mips4400, bfd_mach_mips4000 },
14254 { bfd_mach_mips4300, bfd_mach_mips4000 },
14255 { bfd_mach_mips4100, bfd_mach_mips4000 },
c97c330b
MF
14256 { bfd_mach_mips5900, bfd_mach_mips4000 },
14257
38bf472a
MR
14258 /* MIPS32r3 extensions. */
14259 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14260
14261 /* MIPS32r2 extensions. */
14262 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14263
c97c330b
MF
14264 /* MIPS32 extensions. */
14265 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14266
14267 /* MIPS II extensions. */
14268 { bfd_mach_mips4000, bfd_mach_mips6000 },
14269 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
b417536f 14270 { bfd_mach_mips4010, bfd_mach_mips6000 },
c97c330b
MF
14271
14272 /* MIPS I extensions. */
14273 { bfd_mach_mips6000, bfd_mach_mips3000 },
14274 { bfd_mach_mips3900, bfd_mach_mips3000 }
14275};
14276
14277/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14278
14279static bfd_boolean
14280mips_mach_extends_p (unsigned long base, unsigned long extension)
14281{
14282 size_t i;
14283
14284 if (extension == base)
14285 return TRUE;
14286
14287 if (base == bfd_mach_mipsisa32
14288 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14289 return TRUE;
14290
14291 if (base == bfd_mach_mipsisa32r2
14292 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14293 return TRUE;
14294
14295 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14296 if (extension == mips_mach_extensions[i].extension)
14297 {
14298 extension = mips_mach_extensions[i].base;
14299 if (extension == base)
14300 return TRUE;
14301 }
14302
14303 return FALSE;
14304}
14305
14306/* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14307
14308static unsigned long
14309bfd_mips_isa_ext_mach (unsigned int isa_ext)
14310{
14311 switch (isa_ext)
14312 {
07d6d2b8
AM
14313 case AFL_EXT_3900: return bfd_mach_mips3900;
14314 case AFL_EXT_4010: return bfd_mach_mips4010;
14315 case AFL_EXT_4100: return bfd_mach_mips4100;
14316 case AFL_EXT_4111: return bfd_mach_mips4111;
14317 case AFL_EXT_4120: return bfd_mach_mips4120;
14318 case AFL_EXT_4650: return bfd_mach_mips4650;
14319 case AFL_EXT_5400: return bfd_mach_mips5400;
14320 case AFL_EXT_5500: return bfd_mach_mips5500;
14321 case AFL_EXT_5900: return bfd_mach_mips5900;
14322 case AFL_EXT_10000: return bfd_mach_mips10000;
c97c330b
MF
14323 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14324 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
07d6d2b8 14325 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
c97c330b
MF
14326 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14327 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14328 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
07d6d2b8
AM
14329 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14330 default: return bfd_mach_mips3000;
c97c330b
MF
14331 }
14332}
14333
351cdf24
MF
14334/* Return the .MIPS.abiflags value representing each ISA Extension. */
14335
14336unsigned int
14337bfd_mips_isa_ext (bfd *abfd)
14338{
14339 switch (bfd_get_mach (abfd))
14340 {
07d6d2b8
AM
14341 case bfd_mach_mips3900: return AFL_EXT_3900;
14342 case bfd_mach_mips4010: return AFL_EXT_4010;
14343 case bfd_mach_mips4100: return AFL_EXT_4100;
14344 case bfd_mach_mips4111: return AFL_EXT_4111;
14345 case bfd_mach_mips4120: return AFL_EXT_4120;
14346 case bfd_mach_mips4650: return AFL_EXT_4650;
14347 case bfd_mach_mips5400: return AFL_EXT_5400;
14348 case bfd_mach_mips5500: return AFL_EXT_5500;
14349 case bfd_mach_mips5900: return AFL_EXT_5900;
14350 case bfd_mach_mips10000: return AFL_EXT_10000;
c97c330b
MF
14351 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14352 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
07d6d2b8
AM
14353 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14354 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14355 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14356 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14357 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14358 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
38bf472a
MR
14359 case bfd_mach_mips_interaptiv_mr2:
14360 return AFL_EXT_INTERAPTIV_MR2;
07d6d2b8 14361 default: return 0;
c97c330b
MF
14362 }
14363}
14364
14365/* Encode ISA level and revision as a single value. */
14366#define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14367
14368/* Decode a single value into level and revision. */
14369#define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14370#define ISA_REV(LEVREV) ((LEVREV) & 0x7)
351cdf24
MF
14371
14372/* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14373
14374static void
14375update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14376{
c97c330b 14377 int new_isa = 0;
351cdf24
MF
14378 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14379 {
c97c330b
MF
14380 case E_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14381 case E_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14382 case E_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14383 case E_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14384 case E_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14385 case E_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14386 case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14387 case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14388 case E_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14389 case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14390 case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
351cdf24 14391 default:
4eca0228 14392 _bfd_error_handler
695344c0 14393 /* xgettext:c-format */
2c1c9679 14394 (_("%pB: unknown architecture %s"),
351cdf24
MF
14395 abfd, bfd_printable_name (abfd));
14396 }
14397
c97c330b
MF
14398 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14399 {
14400 abiflags->isa_level = ISA_LEVEL (new_isa);
14401 abiflags->isa_rev = ISA_REV (new_isa);
14402 }
14403
14404 /* Update the isa_ext if ABFD describes a further extension. */
14405 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14406 bfd_get_mach (abfd)))
14407 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
351cdf24
MF
14408}
14409
14410/* Return true if the given ELF header flags describe a 32-bit binary. */
14411
14412static bfd_boolean
14413mips_32bit_flags_p (flagword flags)
14414{
14415 return ((flags & EF_MIPS_32BITMODE) != 0
14416 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14417 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14418 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14419 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14420 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
7361da2c
AB
14421 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14422 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
351cdf24
MF
14423}
14424
14425/* Infer the content of the ABI flags based on the elf header. */
14426
14427static void
14428infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14429{
14430 obj_attribute *in_attr;
14431
14432 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14433 update_mips_abiflags_isa (abfd, abiflags);
14434
14435 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14436 abiflags->gpr_size = AFL_REG_32;
14437 else
14438 abiflags->gpr_size = AFL_REG_64;
14439
14440 abiflags->cpr1_size = AFL_REG_NONE;
14441
14442 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14443 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14444
14445 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14446 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14447 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14448 && abiflags->gpr_size == AFL_REG_32))
14449 abiflags->cpr1_size = AFL_REG_32;
14450 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14451 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14452 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14453 abiflags->cpr1_size = AFL_REG_64;
14454
14455 abiflags->cpr2_size = AFL_REG_NONE;
14456
14457 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14458 abiflags->ases |= AFL_ASE_MDMX;
14459 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14460 abiflags->ases |= AFL_ASE_MIPS16;
14461 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14462 abiflags->ases |= AFL_ASE_MICROMIPS;
14463
14464 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14465 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14466 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14467 && abiflags->isa_level >= 32
bdc6c06e 14468 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
351cdf24
MF
14469 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14470}
14471
b49e97c9
TS
14472/* We need to use a special link routine to handle the .reginfo and
14473 the .mdebug sections. We need to merge all instances of these
14474 sections together, not write them all out sequentially. */
14475
b34976b6 14476bfd_boolean
9719ad41 14477_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 14478{
b49e97c9
TS
14479 asection *o;
14480 struct bfd_link_order *p;
14481 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
351cdf24 14482 asection *rtproc_sec, *abiflags_sec;
b49e97c9
TS
14483 Elf32_RegInfo reginfo;
14484 struct ecoff_debug_info debug;
861fb55a 14485 struct mips_htab_traverse_info hti;
7a2a6943
NC
14486 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14487 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 14488 HDRR *symhdr = &debug.symbolic_header;
9719ad41 14489 void *mdebug_handle = NULL;
b49e97c9
TS
14490 asection *s;
14491 EXTR esym;
14492 unsigned int i;
14493 bfd_size_type amt;
0a44bf69 14494 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
14495
14496 static const char * const secname[] =
14497 {
14498 ".text", ".init", ".fini", ".data",
14499 ".rodata", ".sdata", ".sbss", ".bss"
14500 };
14501 static const int sc[] =
14502 {
14503 scText, scInit, scFini, scData,
14504 scRData, scSData, scSBss, scBss
14505 };
14506
0a44bf69 14507 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
14508 BFD_ASSERT (htab != NULL);
14509
64575f78
MR
14510 /* Sort the dynamic symbols so that those with GOT entries come after
14511 those without. */
d4596a51
RS
14512 if (!mips_elf_sort_hash_table (abfd, info))
14513 return FALSE;
b49e97c9 14514
861fb55a
DJ
14515 /* Create any scheduled LA25 stubs. */
14516 hti.info = info;
14517 hti.output_bfd = abfd;
14518 hti.error = FALSE;
14519 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14520 if (hti.error)
14521 return FALSE;
14522
b49e97c9
TS
14523 /* Get a value for the GP register. */
14524 if (elf_gp (abfd) == 0)
14525 {
14526 struct bfd_link_hash_entry *h;
14527
b34976b6 14528 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 14529 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
14530 elf_gp (abfd) = (h->u.def.value
14531 + h->u.def.section->output_section->vma
14532 + h->u.def.section->output_offset);
0a44bf69
RS
14533 else if (htab->is_vxworks
14534 && (h = bfd_link_hash_lookup (info->hash,
14535 "_GLOBAL_OFFSET_TABLE_",
14536 FALSE, FALSE, TRUE))
14537 && h->type == bfd_link_hash_defined)
14538 elf_gp (abfd) = (h->u.def.section->output_section->vma
14539 + h->u.def.section->output_offset
14540 + h->u.def.value);
0e1862bb 14541 else if (bfd_link_relocatable (info))
b49e97c9
TS
14542 {
14543 bfd_vma lo = MINUS_ONE;
14544
14545 /* Find the GP-relative section with the lowest offset. */
9719ad41 14546 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
14547 if (o->vma < lo
14548 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14549 lo = o->vma;
14550
14551 /* And calculate GP relative to that. */
0a44bf69 14552 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
14553 }
14554 else
14555 {
14556 /* If the relocate_section function needs to do a reloc
14557 involving the GP value, it should make a reloc_dangerous
14558 callback to warn that GP is not defined. */
14559 }
14560 }
14561
14562 /* Go through the sections and collect the .reginfo and .mdebug
14563 information. */
351cdf24 14564 abiflags_sec = NULL;
b49e97c9
TS
14565 reginfo_sec = NULL;
14566 mdebug_sec = NULL;
14567 gptab_data_sec = NULL;
14568 gptab_bss_sec = NULL;
9719ad41 14569 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9 14570 {
351cdf24
MF
14571 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14572 {
14573 /* We have found the .MIPS.abiflags section in the output file.
14574 Look through all the link_orders comprising it and remove them.
14575 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14576 for (p = o->map_head.link_order; p != NULL; p = p->next)
14577 {
14578 asection *input_section;
14579
14580 if (p->type != bfd_indirect_link_order)
14581 {
14582 if (p->type == bfd_data_link_order)
14583 continue;
14584 abort ();
14585 }
14586
14587 input_section = p->u.indirect.section;
14588
14589 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14590 elf_link_input_bfd ignores this section. */
14591 input_section->flags &= ~SEC_HAS_CONTENTS;
14592 }
14593
14594 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14595 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14596
14597 /* Skip this section later on (I don't think this currently
14598 matters, but someday it might). */
14599 o->map_head.link_order = NULL;
14600
14601 abiflags_sec = o;
14602 }
14603
b49e97c9
TS
14604 if (strcmp (o->name, ".reginfo") == 0)
14605 {
14606 memset (&reginfo, 0, sizeof reginfo);
14607
14608 /* We have found the .reginfo section in the output file.
14609 Look through all the link_orders comprising it and merge
14610 the information together. */
8423293d 14611 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14612 {
14613 asection *input_section;
14614 bfd *input_bfd;
14615 Elf32_External_RegInfo ext;
14616 Elf32_RegInfo sub;
6798f8bf 14617 bfd_size_type sz;
b49e97c9
TS
14618
14619 if (p->type != bfd_indirect_link_order)
14620 {
14621 if (p->type == bfd_data_link_order)
14622 continue;
14623 abort ();
14624 }
14625
14626 input_section = p->u.indirect.section;
14627 input_bfd = input_section->owner;
14628
6798f8bf
MR
14629 sz = (input_section->size < sizeof (ext)
14630 ? input_section->size : sizeof (ext));
14631 memset (&ext, 0, sizeof (ext));
b49e97c9 14632 if (! bfd_get_section_contents (input_bfd, input_section,
6798f8bf 14633 &ext, 0, sz))
b34976b6 14634 return FALSE;
b49e97c9
TS
14635
14636 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14637
14638 reginfo.ri_gprmask |= sub.ri_gprmask;
14639 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14640 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14641 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14642 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14643
14644 /* ri_gp_value is set by the function
1c5e4ee9 14645 `_bfd_mips_elf_section_processing' when the section is
b49e97c9
TS
14646 finally written out. */
14647
14648 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14649 elf_link_input_bfd ignores this section. */
14650 input_section->flags &= ~SEC_HAS_CONTENTS;
14651 }
14652
14653 /* Size has been set in _bfd_mips_elf_always_size_sections. */
b248d650 14654 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
14655
14656 /* Skip this section later on (I don't think this currently
14657 matters, but someday it might). */
8423293d 14658 o->map_head.link_order = NULL;
b49e97c9
TS
14659
14660 reginfo_sec = o;
14661 }
14662
14663 if (strcmp (o->name, ".mdebug") == 0)
14664 {
14665 struct extsym_info einfo;
14666 bfd_vma last;
14667
14668 /* We have found the .mdebug section in the output file.
14669 Look through all the link_orders comprising it and merge
14670 the information together. */
14671 symhdr->magic = swap->sym_magic;
14672 /* FIXME: What should the version stamp be? */
14673 symhdr->vstamp = 0;
14674 symhdr->ilineMax = 0;
14675 symhdr->cbLine = 0;
14676 symhdr->idnMax = 0;
14677 symhdr->ipdMax = 0;
14678 symhdr->isymMax = 0;
14679 symhdr->ioptMax = 0;
14680 symhdr->iauxMax = 0;
14681 symhdr->issMax = 0;
14682 symhdr->issExtMax = 0;
14683 symhdr->ifdMax = 0;
14684 symhdr->crfd = 0;
14685 symhdr->iextMax = 0;
14686
14687 /* We accumulate the debugging information itself in the
14688 debug_info structure. */
14689 debug.line = NULL;
14690 debug.external_dnr = NULL;
14691 debug.external_pdr = NULL;
14692 debug.external_sym = NULL;
14693 debug.external_opt = NULL;
14694 debug.external_aux = NULL;
14695 debug.ss = NULL;
14696 debug.ssext = debug.ssext_end = NULL;
14697 debug.external_fdr = NULL;
14698 debug.external_rfd = NULL;
14699 debug.external_ext = debug.external_ext_end = NULL;
14700
14701 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 14702 if (mdebug_handle == NULL)
b34976b6 14703 return FALSE;
b49e97c9
TS
14704
14705 esym.jmptbl = 0;
14706 esym.cobol_main = 0;
14707 esym.weakext = 0;
14708 esym.reserved = 0;
14709 esym.ifd = ifdNil;
14710 esym.asym.iss = issNil;
14711 esym.asym.st = stLocal;
14712 esym.asym.reserved = 0;
14713 esym.asym.index = indexNil;
14714 last = 0;
14715 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
14716 {
14717 esym.asym.sc = sc[i];
14718 s = bfd_get_section_by_name (abfd, secname[i]);
14719 if (s != NULL)
14720 {
14721 esym.asym.value = s->vma;
eea6121a 14722 last = s->vma + s->size;
b49e97c9
TS
14723 }
14724 else
14725 esym.asym.value = last;
14726 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
14727 secname[i], &esym))
b34976b6 14728 return FALSE;
b49e97c9
TS
14729 }
14730
8423293d 14731 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14732 {
14733 asection *input_section;
14734 bfd *input_bfd;
14735 const struct ecoff_debug_swap *input_swap;
14736 struct ecoff_debug_info input_debug;
14737 char *eraw_src;
14738 char *eraw_end;
14739
14740 if (p->type != bfd_indirect_link_order)
14741 {
14742 if (p->type == bfd_data_link_order)
14743 continue;
14744 abort ();
14745 }
14746
14747 input_section = p->u.indirect.section;
14748 input_bfd = input_section->owner;
14749
d5eaccd7 14750 if (!is_mips_elf (input_bfd))
b49e97c9
TS
14751 {
14752 /* I don't know what a non MIPS ELF bfd would be
14753 doing with a .mdebug section, but I don't really
14754 want to deal with it. */
14755 continue;
14756 }
14757
14758 input_swap = (get_elf_backend_data (input_bfd)
14759 ->elf_backend_ecoff_debug_swap);
14760
eea6121a 14761 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
14762
14763 /* The ECOFF linking code expects that we have already
14764 read in the debugging information and set up an
14765 ecoff_debug_info structure, so we do that now. */
14766 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
14767 &input_debug))
b34976b6 14768 return FALSE;
b49e97c9
TS
14769
14770 if (! (bfd_ecoff_debug_accumulate
14771 (mdebug_handle, abfd, &debug, swap, input_bfd,
14772 &input_debug, input_swap, info)))
b34976b6 14773 return FALSE;
b49e97c9
TS
14774
14775 /* Loop through the external symbols. For each one with
14776 interesting information, try to find the symbol in
14777 the linker global hash table and save the information
14778 for the output external symbols. */
14779 eraw_src = input_debug.external_ext;
14780 eraw_end = (eraw_src
14781 + (input_debug.symbolic_header.iextMax
14782 * input_swap->external_ext_size));
14783 for (;
14784 eraw_src < eraw_end;
14785 eraw_src += input_swap->external_ext_size)
14786 {
14787 EXTR ext;
14788 const char *name;
14789 struct mips_elf_link_hash_entry *h;
14790
9719ad41 14791 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
14792 if (ext.asym.sc == scNil
14793 || ext.asym.sc == scUndefined
14794 || ext.asym.sc == scSUndefined)
14795 continue;
14796
14797 name = input_debug.ssext + ext.asym.iss;
14798 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 14799 name, FALSE, FALSE, TRUE);
b49e97c9
TS
14800 if (h == NULL || h->esym.ifd != -2)
14801 continue;
14802
14803 if (ext.ifd != -1)
14804 {
14805 BFD_ASSERT (ext.ifd
14806 < input_debug.symbolic_header.ifdMax);
14807 ext.ifd = input_debug.ifdmap[ext.ifd];
14808 }
14809
14810 h->esym = ext;
14811 }
14812
14813 /* Free up the information we just read. */
14814 free (input_debug.line);
14815 free (input_debug.external_dnr);
14816 free (input_debug.external_pdr);
14817 free (input_debug.external_sym);
14818 free (input_debug.external_opt);
14819 free (input_debug.external_aux);
14820 free (input_debug.ss);
14821 free (input_debug.ssext);
14822 free (input_debug.external_fdr);
14823 free (input_debug.external_rfd);
14824 free (input_debug.external_ext);
14825
14826 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14827 elf_link_input_bfd ignores this section. */
14828 input_section->flags &= ~SEC_HAS_CONTENTS;
14829 }
14830
0e1862bb 14831 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
b49e97c9
TS
14832 {
14833 /* Create .rtproc section. */
87e0a731 14834 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
b49e97c9
TS
14835 if (rtproc_sec == NULL)
14836 {
14837 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
14838 | SEC_LINKER_CREATED | SEC_READONLY);
14839
87e0a731
AM
14840 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
14841 ".rtproc",
14842 flags);
b49e97c9 14843 if (rtproc_sec == NULL
b49e97c9 14844 || ! bfd_set_section_alignment (abfd, rtproc_sec, 4))
b34976b6 14845 return FALSE;
b49e97c9
TS
14846 }
14847
14848 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
14849 info, rtproc_sec,
14850 &debug))
b34976b6 14851 return FALSE;
b49e97c9
TS
14852 }
14853
14854 /* Build the external symbol information. */
14855 einfo.abfd = abfd;
14856 einfo.info = info;
14857 einfo.debug = &debug;
14858 einfo.swap = swap;
b34976b6 14859 einfo.failed = FALSE;
b49e97c9 14860 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 14861 mips_elf_output_extsym, &einfo);
b49e97c9 14862 if (einfo.failed)
b34976b6 14863 return FALSE;
b49e97c9
TS
14864
14865 /* Set the size of the .mdebug section. */
eea6121a 14866 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
14867
14868 /* Skip this section later on (I don't think this currently
14869 matters, but someday it might). */
8423293d 14870 o->map_head.link_order = NULL;
b49e97c9
TS
14871
14872 mdebug_sec = o;
14873 }
14874
0112cd26 14875 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
14876 {
14877 const char *subname;
14878 unsigned int c;
14879 Elf32_gptab *tab;
14880 Elf32_External_gptab *ext_tab;
14881 unsigned int j;
14882
14883 /* The .gptab.sdata and .gptab.sbss sections hold
14884 information describing how the small data area would
14885 change depending upon the -G switch. These sections
14886 not used in executables files. */
0e1862bb 14887 if (! bfd_link_relocatable (info))
b49e97c9 14888 {
8423293d 14889 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14890 {
14891 asection *input_section;
14892
14893 if (p->type != bfd_indirect_link_order)
14894 {
14895 if (p->type == bfd_data_link_order)
14896 continue;
14897 abort ();
14898 }
14899
14900 input_section = p->u.indirect.section;
14901
14902 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14903 elf_link_input_bfd ignores this section. */
14904 input_section->flags &= ~SEC_HAS_CONTENTS;
14905 }
14906
14907 /* Skip this section later on (I don't think this
14908 currently matters, but someday it might). */
8423293d 14909 o->map_head.link_order = NULL;
b49e97c9
TS
14910
14911 /* Really remove the section. */
5daa8fe7 14912 bfd_section_list_remove (abfd, o);
b49e97c9
TS
14913 --abfd->section_count;
14914
14915 continue;
14916 }
14917
14918 /* There is one gptab for initialized data, and one for
14919 uninitialized data. */
14920 if (strcmp (o->name, ".gptab.sdata") == 0)
14921 gptab_data_sec = o;
14922 else if (strcmp (o->name, ".gptab.sbss") == 0)
14923 gptab_bss_sec = o;
14924 else
14925 {
4eca0228 14926 _bfd_error_handler
695344c0 14927 /* xgettext:c-format */
871b3ab2 14928 (_("%pB: illegal section name `%pA'"), abfd, o);
b49e97c9 14929 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 14930 return FALSE;
b49e97c9
TS
14931 }
14932
14933 /* The linker script always combines .gptab.data and
14934 .gptab.sdata into .gptab.sdata, and likewise for
14935 .gptab.bss and .gptab.sbss. It is possible that there is
14936 no .sdata or .sbss section in the output file, in which
14937 case we must change the name of the output section. */
14938 subname = o->name + sizeof ".gptab" - 1;
14939 if (bfd_get_section_by_name (abfd, subname) == NULL)
14940 {
14941 if (o == gptab_data_sec)
14942 o->name = ".gptab.data";
14943 else
14944 o->name = ".gptab.bss";
14945 subname = o->name + sizeof ".gptab" - 1;
14946 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
14947 }
14948
14949 /* Set up the first entry. */
14950 c = 1;
14951 amt = c * sizeof (Elf32_gptab);
9719ad41 14952 tab = bfd_malloc (amt);
b49e97c9 14953 if (tab == NULL)
b34976b6 14954 return FALSE;
b49e97c9
TS
14955 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
14956 tab[0].gt_header.gt_unused = 0;
14957
14958 /* Combine the input sections. */
8423293d 14959 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14960 {
14961 asection *input_section;
14962 bfd *input_bfd;
14963 bfd_size_type size;
14964 unsigned long last;
14965 bfd_size_type gpentry;
14966
14967 if (p->type != bfd_indirect_link_order)
14968 {
14969 if (p->type == bfd_data_link_order)
14970 continue;
14971 abort ();
14972 }
14973
14974 input_section = p->u.indirect.section;
14975 input_bfd = input_section->owner;
14976
14977 /* Combine the gptab entries for this input section one
14978 by one. We know that the input gptab entries are
14979 sorted by ascending -G value. */
eea6121a 14980 size = input_section->size;
b49e97c9
TS
14981 last = 0;
14982 for (gpentry = sizeof (Elf32_External_gptab);
14983 gpentry < size;
14984 gpentry += sizeof (Elf32_External_gptab))
14985 {
14986 Elf32_External_gptab ext_gptab;
14987 Elf32_gptab int_gptab;
14988 unsigned long val;
14989 unsigned long add;
b34976b6 14990 bfd_boolean exact;
b49e97c9
TS
14991 unsigned int look;
14992
14993 if (! (bfd_get_section_contents
9719ad41
RS
14994 (input_bfd, input_section, &ext_gptab, gpentry,
14995 sizeof (Elf32_External_gptab))))
b49e97c9
TS
14996 {
14997 free (tab);
b34976b6 14998 return FALSE;
b49e97c9
TS
14999 }
15000
15001 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15002 &int_gptab);
15003 val = int_gptab.gt_entry.gt_g_value;
15004 add = int_gptab.gt_entry.gt_bytes - last;
15005
b34976b6 15006 exact = FALSE;
b49e97c9
TS
15007 for (look = 1; look < c; look++)
15008 {
15009 if (tab[look].gt_entry.gt_g_value >= val)
15010 tab[look].gt_entry.gt_bytes += add;
15011
15012 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 15013 exact = TRUE;
b49e97c9
TS
15014 }
15015
15016 if (! exact)
15017 {
15018 Elf32_gptab *new_tab;
15019 unsigned int max;
15020
15021 /* We need a new table entry. */
15022 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 15023 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
15024 if (new_tab == NULL)
15025 {
15026 free (tab);
b34976b6 15027 return FALSE;
b49e97c9
TS
15028 }
15029 tab = new_tab;
15030 tab[c].gt_entry.gt_g_value = val;
15031 tab[c].gt_entry.gt_bytes = add;
15032
15033 /* Merge in the size for the next smallest -G
15034 value, since that will be implied by this new
15035 value. */
15036 max = 0;
15037 for (look = 1; look < c; look++)
15038 {
15039 if (tab[look].gt_entry.gt_g_value < val
15040 && (max == 0
15041 || (tab[look].gt_entry.gt_g_value
15042 > tab[max].gt_entry.gt_g_value)))
15043 max = look;
15044 }
15045 if (max != 0)
15046 tab[c].gt_entry.gt_bytes +=
15047 tab[max].gt_entry.gt_bytes;
15048
15049 ++c;
15050 }
15051
15052 last = int_gptab.gt_entry.gt_bytes;
15053 }
15054
15055 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15056 elf_link_input_bfd ignores this section. */
15057 input_section->flags &= ~SEC_HAS_CONTENTS;
15058 }
15059
15060 /* The table must be sorted by -G value. */
15061 if (c > 2)
15062 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15063
15064 /* Swap out the table. */
15065 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 15066 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
15067 if (ext_tab == NULL)
15068 {
15069 free (tab);
b34976b6 15070 return FALSE;
b49e97c9
TS
15071 }
15072
15073 for (j = 0; j < c; j++)
15074 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15075 free (tab);
15076
eea6121a 15077 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
15078 o->contents = (bfd_byte *) ext_tab;
15079
15080 /* Skip this section later on (I don't think this currently
15081 matters, but someday it might). */
8423293d 15082 o->map_head.link_order = NULL;
b49e97c9
TS
15083 }
15084 }
15085
15086 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 15087 if (!bfd_elf_final_link (abfd, info))
b34976b6 15088 return FALSE;
b49e97c9
TS
15089
15090 /* Now write out the computed sections. */
15091
351cdf24
MF
15092 if (abiflags_sec != NULL)
15093 {
15094 Elf_External_ABIFlags_v0 ext;
15095 Elf_Internal_ABIFlags_v0 *abiflags;
15096
15097 abiflags = &mips_elf_tdata (abfd)->abiflags;
15098
15099 /* Set up the abiflags if no valid input sections were found. */
15100 if (!mips_elf_tdata (abfd)->abiflags_valid)
15101 {
15102 infer_mips_abiflags (abfd, abiflags);
15103 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
15104 }
15105 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15106 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15107 return FALSE;
15108 }
15109
9719ad41 15110 if (reginfo_sec != NULL)
b49e97c9
TS
15111 {
15112 Elf32_External_RegInfo ext;
15113
15114 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 15115 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 15116 return FALSE;
b49e97c9
TS
15117 }
15118
9719ad41 15119 if (mdebug_sec != NULL)
b49e97c9
TS
15120 {
15121 BFD_ASSERT (abfd->output_has_begun);
15122 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15123 swap, info,
15124 mdebug_sec->filepos))
b34976b6 15125 return FALSE;
b49e97c9
TS
15126
15127 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15128 }
15129
9719ad41 15130 if (gptab_data_sec != NULL)
b49e97c9
TS
15131 {
15132 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15133 gptab_data_sec->contents,
eea6121a 15134 0, gptab_data_sec->size))
b34976b6 15135 return FALSE;
b49e97c9
TS
15136 }
15137
9719ad41 15138 if (gptab_bss_sec != NULL)
b49e97c9
TS
15139 {
15140 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15141 gptab_bss_sec->contents,
eea6121a 15142 0, gptab_bss_sec->size))
b34976b6 15143 return FALSE;
b49e97c9
TS
15144 }
15145
15146 if (SGI_COMPAT (abfd))
15147 {
15148 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15149 if (rtproc_sec != NULL)
15150 {
15151 if (! bfd_set_section_contents (abfd, rtproc_sec,
15152 rtproc_sec->contents,
eea6121a 15153 0, rtproc_sec->size))
b34976b6 15154 return FALSE;
b49e97c9
TS
15155 }
15156 }
15157
b34976b6 15158 return TRUE;
b49e97c9
TS
15159}
15160\f
b2e9744f
MR
15161/* Merge object file header flags from IBFD into OBFD. Raise an error
15162 if there are conflicting settings. */
15163
15164static bfd_boolean
50e03d47 15165mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
b2e9744f 15166{
50e03d47 15167 bfd *obfd = info->output_bfd;
b2e9744f
MR
15168 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15169 flagword old_flags;
15170 flagword new_flags;
15171 bfd_boolean ok;
15172
15173 new_flags = elf_elfheader (ibfd)->e_flags;
15174 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15175 old_flags = elf_elfheader (obfd)->e_flags;
15176
15177 /* Check flag compatibility. */
15178
15179 new_flags &= ~EF_MIPS_NOREORDER;
15180 old_flags &= ~EF_MIPS_NOREORDER;
15181
15182 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15183 doesn't seem to matter. */
15184 new_flags &= ~EF_MIPS_XGOT;
15185 old_flags &= ~EF_MIPS_XGOT;
15186
15187 /* MIPSpro generates ucode info in n64 objects. Again, we should
15188 just be able to ignore this. */
15189 new_flags &= ~EF_MIPS_UCODE;
15190 old_flags &= ~EF_MIPS_UCODE;
15191
15192 /* DSOs should only be linked with CPIC code. */
15193 if ((ibfd->flags & DYNAMIC) != 0)
15194 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15195
15196 if (new_flags == old_flags)
15197 return TRUE;
15198
15199 ok = TRUE;
15200
15201 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15202 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15203 {
4eca0228 15204 _bfd_error_handler
871b3ab2 15205 (_("%pB: warning: linking abicalls files with non-abicalls files"),
b2e9744f
MR
15206 ibfd);
15207 ok = TRUE;
15208 }
15209
15210 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15211 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15212 if (! (new_flags & EF_MIPS_PIC))
15213 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15214
15215 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15216 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15217
15218 /* Compare the ISAs. */
15219 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15220 {
4eca0228 15221 _bfd_error_handler
871b3ab2 15222 (_("%pB: linking 32-bit code with 64-bit code"),
b2e9744f
MR
15223 ibfd);
15224 ok = FALSE;
15225 }
15226 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15227 {
15228 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15229 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15230 {
15231 /* Copy the architecture info from IBFD to OBFD. Also copy
15232 the 32-bit flag (if set) so that we continue to recognise
15233 OBFD as a 32-bit binary. */
15234 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15235 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15236 elf_elfheader (obfd)->e_flags
15237 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15238
15239 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15240 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15241
15242 /* Copy across the ABI flags if OBFD doesn't use them
15243 and if that was what caused us to treat IBFD as 32-bit. */
15244 if ((old_flags & EF_MIPS_ABI) == 0
15245 && mips_32bit_flags_p (new_flags)
15246 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15247 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15248 }
15249 else
15250 {
15251 /* The ISAs aren't compatible. */
4eca0228 15252 _bfd_error_handler
695344c0 15253 /* xgettext:c-format */
871b3ab2 15254 (_("%pB: linking %s module with previous %s modules"),
b2e9744f
MR
15255 ibfd,
15256 bfd_printable_name (ibfd),
15257 bfd_printable_name (obfd));
15258 ok = FALSE;
15259 }
15260 }
15261
15262 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15263 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15264
15265 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15266 does set EI_CLASS differently from any 32-bit ABI. */
15267 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15268 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15269 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15270 {
15271 /* Only error if both are set (to different values). */
15272 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15273 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15274 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15275 {
4eca0228 15276 _bfd_error_handler
695344c0 15277 /* xgettext:c-format */
871b3ab2 15278 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
b2e9744f
MR
15279 ibfd,
15280 elf_mips_abi_name (ibfd),
15281 elf_mips_abi_name (obfd));
15282 ok = FALSE;
15283 }
15284 new_flags &= ~EF_MIPS_ABI;
15285 old_flags &= ~EF_MIPS_ABI;
15286 }
15287
15288 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15289 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15290 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15291 {
15292 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15293 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15294 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15295 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15296 int micro_mis = old_m16 && new_micro;
15297 int m16_mis = old_micro && new_m16;
15298
15299 if (m16_mis || micro_mis)
15300 {
4eca0228 15301 _bfd_error_handler
695344c0 15302 /* xgettext:c-format */
871b3ab2 15303 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
b2e9744f
MR
15304 ibfd,
15305 m16_mis ? "MIPS16" : "microMIPS",
15306 m16_mis ? "microMIPS" : "MIPS16");
15307 ok = FALSE;
15308 }
15309
15310 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15311
15312 new_flags &= ~ EF_MIPS_ARCH_ASE;
15313 old_flags &= ~ EF_MIPS_ARCH_ASE;
15314 }
15315
15316 /* Compare NaN encodings. */
15317 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15318 {
695344c0 15319 /* xgettext:c-format */
871b3ab2 15320 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
b2e9744f
MR
15321 ibfd,
15322 (new_flags & EF_MIPS_NAN2008
15323 ? "-mnan=2008" : "-mnan=legacy"),
15324 (old_flags & EF_MIPS_NAN2008
15325 ? "-mnan=2008" : "-mnan=legacy"));
15326 ok = FALSE;
15327 new_flags &= ~EF_MIPS_NAN2008;
15328 old_flags &= ~EF_MIPS_NAN2008;
15329 }
15330
15331 /* Compare FP64 state. */
15332 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15333 {
695344c0 15334 /* xgettext:c-format */
871b3ab2 15335 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
b2e9744f
MR
15336 ibfd,
15337 (new_flags & EF_MIPS_FP64
15338 ? "-mfp64" : "-mfp32"),
15339 (old_flags & EF_MIPS_FP64
15340 ? "-mfp64" : "-mfp32"));
15341 ok = FALSE;
15342 new_flags &= ~EF_MIPS_FP64;
15343 old_flags &= ~EF_MIPS_FP64;
15344 }
15345
15346 /* Warn about any other mismatches */
15347 if (new_flags != old_flags)
15348 {
695344c0 15349 /* xgettext:c-format */
4eca0228 15350 _bfd_error_handler
871b3ab2 15351 (_("%pB: uses different e_flags (%#x) fields than previous modules "
d42c267e
AM
15352 "(%#x)"),
15353 ibfd, new_flags, old_flags);
b2e9744f
MR
15354 ok = FALSE;
15355 }
15356
15357 return ok;
15358}
15359
2cf19d5c
JM
15360/* Merge object attributes from IBFD into OBFD. Raise an error if
15361 there are conflicting attributes. */
15362static bfd_boolean
50e03d47 15363mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
2cf19d5c 15364{
50e03d47 15365 bfd *obfd = info->output_bfd;
2cf19d5c
JM
15366 obj_attribute *in_attr;
15367 obj_attribute *out_attr;
6ae68ba3 15368 bfd *abi_fp_bfd;
b60bf9be 15369 bfd *abi_msa_bfd;
6ae68ba3
MR
15370
15371 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15372 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
d929bc19 15373 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
6ae68ba3 15374 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
2cf19d5c 15375
b60bf9be
CF
15376 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15377 if (!abi_msa_bfd
15378 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15379 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15380
2cf19d5c
JM
15381 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15382 {
15383 /* This is the first object. Copy the attributes. */
15384 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15385
15386 /* Use the Tag_null value to indicate the attributes have been
15387 initialized. */
15388 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15389
15390 return TRUE;
15391 }
15392
15393 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15394 non-conflicting ones. */
2cf19d5c
JM
15395 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15396 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15397 {
757a636f 15398 int out_fp, in_fp;
6ae68ba3 15399
757a636f
RS
15400 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15401 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15402 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15403 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15404 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
351cdf24
MF
15405 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15406 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15407 || in_fp == Val_GNU_MIPS_ABI_FP_64
15408 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15409 {
15410 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15411 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15412 }
15413 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15414 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15415 || out_fp == Val_GNU_MIPS_ABI_FP_64
15416 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15417 /* Keep the current setting. */;
15418 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15419 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15420 {
15421 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15422 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15423 }
15424 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15425 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15426 /* Keep the current setting. */;
757a636f
RS
15427 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15428 {
15429 const char *out_string, *in_string;
6ae68ba3 15430
757a636f
RS
15431 out_string = _bfd_mips_fp_abi_string (out_fp);
15432 in_string = _bfd_mips_fp_abi_string (in_fp);
15433 /* First warn about cases involving unrecognised ABIs. */
15434 if (!out_string && !in_string)
695344c0 15435 /* xgettext:c-format */
757a636f 15436 _bfd_error_handler
2c1c9679 15437 (_("warning: %pB uses unknown floating point ABI %d "
871b3ab2 15438 "(set by %pB), %pB uses unknown floating point ABI %d"),
c08bb8dd 15439 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
757a636f
RS
15440 else if (!out_string)
15441 _bfd_error_handler
695344c0 15442 /* xgettext:c-format */
2c1c9679 15443 (_("warning: %pB uses unknown floating point ABI %d "
871b3ab2 15444 "(set by %pB), %pB uses %s"),
c08bb8dd 15445 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
757a636f
RS
15446 else if (!in_string)
15447 _bfd_error_handler
695344c0 15448 /* xgettext:c-format */
2c1c9679 15449 (_("warning: %pB uses %s (set by %pB), "
871b3ab2 15450 "%pB uses unknown floating point ABI %d"),
c08bb8dd 15451 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
757a636f
RS
15452 else
15453 {
15454 /* If one of the bfds is soft-float, the other must be
15455 hard-float. The exact choice of hard-float ABI isn't
15456 really relevant to the error message. */
15457 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15458 out_string = "-mhard-float";
15459 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15460 in_string = "-mhard-float";
15461 _bfd_error_handler
695344c0 15462 /* xgettext:c-format */
2c1c9679 15463 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
c08bb8dd 15464 obfd, out_string, abi_fp_bfd, ibfd, in_string);
757a636f
RS
15465 }
15466 }
2cf19d5c
JM
15467 }
15468
b60bf9be
CF
15469 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15470 non-conflicting ones. */
15471 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15472 {
15473 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15474 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15475 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15476 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15477 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15478 {
15479 case Val_GNU_MIPS_ABI_MSA_128:
15480 _bfd_error_handler
695344c0 15481 /* xgettext:c-format */
2c1c9679 15482 (_("warning: %pB uses %s (set by %pB), "
871b3ab2 15483 "%pB uses unknown MSA ABI %d"),
c08bb8dd
AM
15484 obfd, "-mmsa", abi_msa_bfd,
15485 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
b60bf9be
CF
15486 break;
15487
15488 default:
15489 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15490 {
15491 case Val_GNU_MIPS_ABI_MSA_128:
15492 _bfd_error_handler
695344c0 15493 /* xgettext:c-format */
2c1c9679 15494 (_("warning: %pB uses unknown MSA ABI %d "
871b3ab2 15495 "(set by %pB), %pB uses %s"),
c08bb8dd
AM
15496 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15497 abi_msa_bfd, ibfd, "-mmsa");
b60bf9be
CF
15498 break;
15499
15500 default:
15501 _bfd_error_handler
695344c0 15502 /* xgettext:c-format */
2c1c9679 15503 (_("warning: %pB uses unknown MSA ABI %d "
871b3ab2 15504 "(set by %pB), %pB uses unknown MSA ABI %d"),
c08bb8dd
AM
15505 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15506 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
b60bf9be
CF
15507 break;
15508 }
15509 }
15510 }
15511
2cf19d5c 15512 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 15513 return _bfd_elf_merge_object_attributes (ibfd, info);
2cf19d5c
JM
15514}
15515
a3dc0a7f
MR
15516/* Merge object ABI flags from IBFD into OBFD. Raise an error if
15517 there are conflicting settings. */
15518
15519static bfd_boolean
15520mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15521{
15522 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15523 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15524 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15525
15526 /* Update the output abiflags fp_abi using the computed fp_abi. */
15527 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15528
15529#define max(a, b) ((a) > (b) ? (a) : (b))
15530 /* Merge abiflags. */
15531 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15532 in_tdata->abiflags.isa_level);
15533 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15534 in_tdata->abiflags.isa_rev);
15535 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15536 in_tdata->abiflags.gpr_size);
15537 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15538 in_tdata->abiflags.cpr1_size);
15539 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15540 in_tdata->abiflags.cpr2_size);
15541#undef max
15542 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15543 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15544
15545 return TRUE;
15546}
15547
b49e97c9
TS
15548/* Merge backend specific data from an object file to the output
15549 object file when linking. */
15550
b34976b6 15551bfd_boolean
50e03d47 15552_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
b49e97c9 15553{
50e03d47 15554 bfd *obfd = info->output_bfd;
cf8502c1
MR
15555 struct mips_elf_obj_tdata *out_tdata;
15556 struct mips_elf_obj_tdata *in_tdata;
b34976b6 15557 bfd_boolean null_input_bfd = TRUE;
b49e97c9 15558 asection *sec;
d537eeb5 15559 bfd_boolean ok;
b49e97c9 15560
58238693 15561 /* Check if we have the same endianness. */
50e03d47 15562 if (! _bfd_generic_verify_endian_match (ibfd, info))
aa701218 15563 {
4eca0228 15564 _bfd_error_handler
871b3ab2 15565 (_("%pB: endianness incompatible with that of the selected emulation"),
d003868e 15566 ibfd);
aa701218
AO
15567 return FALSE;
15568 }
b49e97c9 15569
d5eaccd7 15570 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 15571 return TRUE;
b49e97c9 15572
cf8502c1
MR
15573 in_tdata = mips_elf_tdata (ibfd);
15574 out_tdata = mips_elf_tdata (obfd);
15575
aa701218
AO
15576 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15577 {
4eca0228 15578 _bfd_error_handler
871b3ab2 15579 (_("%pB: ABI is incompatible with that of the selected emulation"),
d003868e 15580 ibfd);
aa701218
AO
15581 return FALSE;
15582 }
15583
23ba6f18
MR
15584 /* Check to see if the input BFD actually contains any sections. If not,
15585 then it has no attributes, and its flags may not have been initialized
15586 either, but it cannot actually cause any incompatibility. */
351cdf24
MF
15587 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15588 {
15589 /* Ignore synthetic sections and empty .text, .data and .bss sections
15590 which are automatically generated by gas. Also ignore fake
15591 (s)common sections, since merely defining a common symbol does
15592 not affect compatibility. */
15593 if ((sec->flags & SEC_IS_COMMON) == 0
15594 && strcmp (sec->name, ".reginfo")
15595 && strcmp (sec->name, ".mdebug")
15596 && (sec->size != 0
15597 || (strcmp (sec->name, ".text")
15598 && strcmp (sec->name, ".data")
15599 && strcmp (sec->name, ".bss"))))
15600 {
15601 null_input_bfd = FALSE;
15602 break;
15603 }
15604 }
15605 if (null_input_bfd)
15606 return TRUE;
15607
28d45e28 15608 /* Populate abiflags using existing information. */
23ba6f18
MR
15609 if (in_tdata->abiflags_valid)
15610 {
15611 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
28d45e28
MR
15612 Elf_Internal_ABIFlags_v0 in_abiflags;
15613 Elf_Internal_ABIFlags_v0 abiflags;
15614
15615 /* Set up the FP ABI attribute from the abiflags if it is not already
07d6d2b8 15616 set. */
23ba6f18 15617 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
07d6d2b8 15618 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
23ba6f18 15619
351cdf24 15620 infer_mips_abiflags (ibfd, &abiflags);
cf8502c1 15621 in_abiflags = in_tdata->abiflags;
351cdf24
MF
15622
15623 /* It is not possible to infer the correct ISA revision
07d6d2b8 15624 for R3 or R5 so drop down to R2 for the checks. */
351cdf24
MF
15625 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15626 in_abiflags.isa_rev = 2;
15627
c97c330b
MF
15628 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15629 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
4eca0228 15630 _bfd_error_handler
2c1c9679 15631 (_("%pB: warning: inconsistent ISA between e_flags and "
351cdf24
MF
15632 ".MIPS.abiflags"), ibfd);
15633 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15634 && in_abiflags.fp_abi != abiflags.fp_abi)
4eca0228 15635 _bfd_error_handler
2c1c9679 15636 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
351cdf24
MF
15637 ".MIPS.abiflags"), ibfd);
15638 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
4eca0228 15639 _bfd_error_handler
2c1c9679 15640 (_("%pB: warning: inconsistent ASEs between e_flags and "
351cdf24 15641 ".MIPS.abiflags"), ibfd);
c97c330b
MF
15642 /* The isa_ext is allowed to be an extension of what can be inferred
15643 from e_flags. */
15644 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15645 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
4eca0228 15646 _bfd_error_handler
2c1c9679 15647 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
351cdf24
MF
15648 ".MIPS.abiflags"), ibfd);
15649 if (in_abiflags.flags2 != 0)
4eca0228 15650 _bfd_error_handler
2c1c9679 15651 (_("%pB: warning: unexpected flag in the flags2 field of "
351cdf24 15652 ".MIPS.abiflags (0x%lx)"), ibfd,
d42c267e 15653 in_abiflags.flags2);
351cdf24 15654 }
28d45e28
MR
15655 else
15656 {
15657 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15658 in_tdata->abiflags_valid = TRUE;
15659 }
15660
cf8502c1 15661 if (!out_tdata->abiflags_valid)
351cdf24
MF
15662 {
15663 /* Copy input abiflags if output abiflags are not already valid. */
cf8502c1
MR
15664 out_tdata->abiflags = in_tdata->abiflags;
15665 out_tdata->abiflags_valid = TRUE;
351cdf24 15666 }
b49e97c9
TS
15667
15668 if (! elf_flags_init (obfd))
15669 {
b34976b6 15670 elf_flags_init (obfd) = TRUE;
351cdf24 15671 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
b49e97c9
TS
15672 elf_elfheader (obfd)->e_ident[EI_CLASS]
15673 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
15674
15675 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861 15676 && (bfd_get_arch_info (obfd)->the_default
68ffbac6 15677 || mips_mach_extends_p (bfd_get_mach (obfd),
2907b861 15678 bfd_get_mach (ibfd))))
b49e97c9
TS
15679 {
15680 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
15681 bfd_get_mach (ibfd)))
b34976b6 15682 return FALSE;
351cdf24
MF
15683
15684 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
cf8502c1 15685 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
b49e97c9
TS
15686 }
15687
d537eeb5 15688 ok = TRUE;
b49e97c9 15689 }
d537eeb5 15690 else
50e03d47 15691 ok = mips_elf_merge_obj_e_flags (ibfd, info);
d537eeb5 15692
50e03d47 15693 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
b49e97c9 15694
a3dc0a7f 15695 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
351cdf24 15696
d537eeb5 15697 if (!ok)
b49e97c9
TS
15698 {
15699 bfd_set_error (bfd_error_bad_value);
b34976b6 15700 return FALSE;
b49e97c9
TS
15701 }
15702
b34976b6 15703 return TRUE;
b49e97c9
TS
15704}
15705
15706/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
15707
b34976b6 15708bfd_boolean
9719ad41 15709_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
15710{
15711 BFD_ASSERT (!elf_flags_init (abfd)
15712 || elf_elfheader (abfd)->e_flags == flags);
15713
15714 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
15715 elf_flags_init (abfd) = TRUE;
15716 return TRUE;
b49e97c9
TS
15717}
15718
ad9563d6
CM
15719char *
15720_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
15721{
15722 switch (dtag)
15723 {
15724 default: return "";
15725 case DT_MIPS_RLD_VERSION:
15726 return "MIPS_RLD_VERSION";
15727 case DT_MIPS_TIME_STAMP:
15728 return "MIPS_TIME_STAMP";
15729 case DT_MIPS_ICHECKSUM:
15730 return "MIPS_ICHECKSUM";
15731 case DT_MIPS_IVERSION:
15732 return "MIPS_IVERSION";
15733 case DT_MIPS_FLAGS:
15734 return "MIPS_FLAGS";
15735 case DT_MIPS_BASE_ADDRESS:
15736 return "MIPS_BASE_ADDRESS";
15737 case DT_MIPS_MSYM:
15738 return "MIPS_MSYM";
15739 case DT_MIPS_CONFLICT:
15740 return "MIPS_CONFLICT";
15741 case DT_MIPS_LIBLIST:
15742 return "MIPS_LIBLIST";
15743 case DT_MIPS_LOCAL_GOTNO:
15744 return "MIPS_LOCAL_GOTNO";
15745 case DT_MIPS_CONFLICTNO:
15746 return "MIPS_CONFLICTNO";
15747 case DT_MIPS_LIBLISTNO:
15748 return "MIPS_LIBLISTNO";
15749 case DT_MIPS_SYMTABNO:
15750 return "MIPS_SYMTABNO";
15751 case DT_MIPS_UNREFEXTNO:
15752 return "MIPS_UNREFEXTNO";
15753 case DT_MIPS_GOTSYM:
15754 return "MIPS_GOTSYM";
15755 case DT_MIPS_HIPAGENO:
15756 return "MIPS_HIPAGENO";
15757 case DT_MIPS_RLD_MAP:
15758 return "MIPS_RLD_MAP";
a5499fa4
MF
15759 case DT_MIPS_RLD_MAP_REL:
15760 return "MIPS_RLD_MAP_REL";
ad9563d6
CM
15761 case DT_MIPS_DELTA_CLASS:
15762 return "MIPS_DELTA_CLASS";
15763 case DT_MIPS_DELTA_CLASS_NO:
15764 return "MIPS_DELTA_CLASS_NO";
15765 case DT_MIPS_DELTA_INSTANCE:
15766 return "MIPS_DELTA_INSTANCE";
15767 case DT_MIPS_DELTA_INSTANCE_NO:
15768 return "MIPS_DELTA_INSTANCE_NO";
15769 case DT_MIPS_DELTA_RELOC:
15770 return "MIPS_DELTA_RELOC";
15771 case DT_MIPS_DELTA_RELOC_NO:
15772 return "MIPS_DELTA_RELOC_NO";
15773 case DT_MIPS_DELTA_SYM:
15774 return "MIPS_DELTA_SYM";
15775 case DT_MIPS_DELTA_SYM_NO:
15776 return "MIPS_DELTA_SYM_NO";
15777 case DT_MIPS_DELTA_CLASSSYM:
15778 return "MIPS_DELTA_CLASSSYM";
15779 case DT_MIPS_DELTA_CLASSSYM_NO:
15780 return "MIPS_DELTA_CLASSSYM_NO";
15781 case DT_MIPS_CXX_FLAGS:
15782 return "MIPS_CXX_FLAGS";
15783 case DT_MIPS_PIXIE_INIT:
15784 return "MIPS_PIXIE_INIT";
15785 case DT_MIPS_SYMBOL_LIB:
15786 return "MIPS_SYMBOL_LIB";
15787 case DT_MIPS_LOCALPAGE_GOTIDX:
15788 return "MIPS_LOCALPAGE_GOTIDX";
15789 case DT_MIPS_LOCAL_GOTIDX:
15790 return "MIPS_LOCAL_GOTIDX";
15791 case DT_MIPS_HIDDEN_GOTIDX:
15792 return "MIPS_HIDDEN_GOTIDX";
15793 case DT_MIPS_PROTECTED_GOTIDX:
15794 return "MIPS_PROTECTED_GOT_IDX";
15795 case DT_MIPS_OPTIONS:
15796 return "MIPS_OPTIONS";
15797 case DT_MIPS_INTERFACE:
15798 return "MIPS_INTERFACE";
15799 case DT_MIPS_DYNSTR_ALIGN:
15800 return "DT_MIPS_DYNSTR_ALIGN";
15801 case DT_MIPS_INTERFACE_SIZE:
15802 return "DT_MIPS_INTERFACE_SIZE";
15803 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
15804 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
15805 case DT_MIPS_PERF_SUFFIX:
15806 return "DT_MIPS_PERF_SUFFIX";
15807 case DT_MIPS_COMPACT_SIZE:
15808 return "DT_MIPS_COMPACT_SIZE";
15809 case DT_MIPS_GP_VALUE:
15810 return "DT_MIPS_GP_VALUE";
15811 case DT_MIPS_AUX_DYNAMIC:
15812 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
15813 case DT_MIPS_PLTGOT:
15814 return "DT_MIPS_PLTGOT";
15815 case DT_MIPS_RWPLT:
15816 return "DT_MIPS_RWPLT";
ad9563d6
CM
15817 }
15818}
15819
757a636f
RS
15820/* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
15821 not known. */
15822
15823const char *
15824_bfd_mips_fp_abi_string (int fp)
15825{
15826 switch (fp)
15827 {
15828 /* These strings aren't translated because they're simply
15829 option lists. */
15830 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15831 return "-mdouble-float";
15832
15833 case Val_GNU_MIPS_ABI_FP_SINGLE:
15834 return "-msingle-float";
15835
15836 case Val_GNU_MIPS_ABI_FP_SOFT:
15837 return "-msoft-float";
15838
351cdf24
MF
15839 case Val_GNU_MIPS_ABI_FP_OLD_64:
15840 return _("-mips32r2 -mfp64 (12 callee-saved)");
15841
15842 case Val_GNU_MIPS_ABI_FP_XX:
15843 return "-mfpxx";
15844
757a636f 15845 case Val_GNU_MIPS_ABI_FP_64:
351cdf24
MF
15846 return "-mgp32 -mfp64";
15847
15848 case Val_GNU_MIPS_ABI_FP_64A:
15849 return "-mgp32 -mfp64 -mno-odd-spreg";
757a636f
RS
15850
15851 default:
15852 return 0;
15853 }
15854}
15855
351cdf24
MF
15856static void
15857print_mips_ases (FILE *file, unsigned int mask)
15858{
15859 if (mask & AFL_ASE_DSP)
15860 fputs ("\n\tDSP ASE", file);
15861 if (mask & AFL_ASE_DSPR2)
15862 fputs ("\n\tDSP R2 ASE", file);
8f4f9071
MF
15863 if (mask & AFL_ASE_DSPR3)
15864 fputs ("\n\tDSP R3 ASE", file);
351cdf24
MF
15865 if (mask & AFL_ASE_EVA)
15866 fputs ("\n\tEnhanced VA Scheme", file);
15867 if (mask & AFL_ASE_MCU)
15868 fputs ("\n\tMCU (MicroController) ASE", file);
15869 if (mask & AFL_ASE_MDMX)
15870 fputs ("\n\tMDMX ASE", file);
15871 if (mask & AFL_ASE_MIPS3D)
15872 fputs ("\n\tMIPS-3D ASE", file);
15873 if (mask & AFL_ASE_MT)
15874 fputs ("\n\tMT ASE", file);
15875 if (mask & AFL_ASE_SMARTMIPS)
15876 fputs ("\n\tSmartMIPS ASE", file);
15877 if (mask & AFL_ASE_VIRT)
15878 fputs ("\n\tVZ ASE", file);
15879 if (mask & AFL_ASE_MSA)
15880 fputs ("\n\tMSA ASE", file);
15881 if (mask & AFL_ASE_MIPS16)
15882 fputs ("\n\tMIPS16 ASE", file);
15883 if (mask & AFL_ASE_MICROMIPS)
15884 fputs ("\n\tMICROMIPS ASE", file);
15885 if (mask & AFL_ASE_XPA)
15886 fputs ("\n\tXPA ASE", file);
25499ac7
MR
15887 if (mask & AFL_ASE_MIPS16E2)
15888 fputs ("\n\tMIPS16e2 ASE", file);
730c3174
SE
15889 if (mask & AFL_ASE_CRC)
15890 fputs ("\n\tCRC ASE", file);
6f20c942
FS
15891 if (mask & AFL_ASE_GINV)
15892 fputs ("\n\tGINV ASE", file);
8095d2f7
CX
15893 if (mask & AFL_ASE_LOONGSON_MMI)
15894 fputs ("\n\tLoongson MMI ASE", file);
716c08de
CX
15895 if (mask & AFL_ASE_LOONGSON_CAM)
15896 fputs ("\n\tLoongson CAM ASE", file);
bdc6c06e
CX
15897 if (mask & AFL_ASE_LOONGSON_EXT)
15898 fputs ("\n\tLoongson EXT ASE", file);
a693765e
CX
15899 if (mask & AFL_ASE_LOONGSON_EXT2)
15900 fputs ("\n\tLoongson EXT2 ASE", file);
351cdf24
MF
15901 if (mask == 0)
15902 fprintf (file, "\n\t%s", _("None"));
00ac7aa0
MF
15903 else if ((mask & ~AFL_ASE_MASK) != 0)
15904 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
351cdf24
MF
15905}
15906
15907static void
15908print_mips_isa_ext (FILE *file, unsigned int isa_ext)
15909{
15910 switch (isa_ext)
15911 {
15912 case 0:
15913 fputs (_("None"), file);
15914 break;
15915 case AFL_EXT_XLR:
15916 fputs ("RMI XLR", file);
15917 break;
2c629856
N
15918 case AFL_EXT_OCTEON3:
15919 fputs ("Cavium Networks Octeon3", file);
15920 break;
351cdf24
MF
15921 case AFL_EXT_OCTEON2:
15922 fputs ("Cavium Networks Octeon2", file);
15923 break;
15924 case AFL_EXT_OCTEONP:
15925 fputs ("Cavium Networks OcteonP", file);
15926 break;
351cdf24
MF
15927 case AFL_EXT_OCTEON:
15928 fputs ("Cavium Networks Octeon", file);
15929 break;
15930 case AFL_EXT_5900:
15931 fputs ("Toshiba R5900", file);
15932 break;
15933 case AFL_EXT_4650:
15934 fputs ("MIPS R4650", file);
15935 break;
15936 case AFL_EXT_4010:
15937 fputs ("LSI R4010", file);
15938 break;
15939 case AFL_EXT_4100:
15940 fputs ("NEC VR4100", file);
15941 break;
15942 case AFL_EXT_3900:
15943 fputs ("Toshiba R3900", file);
15944 break;
15945 case AFL_EXT_10000:
15946 fputs ("MIPS R10000", file);
15947 break;
15948 case AFL_EXT_SB1:
15949 fputs ("Broadcom SB-1", file);
15950 break;
15951 case AFL_EXT_4111:
15952 fputs ("NEC VR4111/VR4181", file);
15953 break;
15954 case AFL_EXT_4120:
15955 fputs ("NEC VR4120", file);
15956 break;
15957 case AFL_EXT_5400:
15958 fputs ("NEC VR5400", file);
15959 break;
15960 case AFL_EXT_5500:
15961 fputs ("NEC VR5500", file);
15962 break;
15963 case AFL_EXT_LOONGSON_2E:
15964 fputs ("ST Microelectronics Loongson 2E", file);
15965 break;
15966 case AFL_EXT_LOONGSON_2F:
15967 fputs ("ST Microelectronics Loongson 2F", file);
15968 break;
38bf472a
MR
15969 case AFL_EXT_INTERAPTIV_MR2:
15970 fputs ("Imagination interAptiv MR2", file);
15971 break;
351cdf24 15972 default:
00ac7aa0 15973 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
351cdf24
MF
15974 break;
15975 }
15976}
15977
15978static void
15979print_mips_fp_abi_value (FILE *file, int val)
15980{
15981 switch (val)
15982 {
15983 case Val_GNU_MIPS_ABI_FP_ANY:
15984 fprintf (file, _("Hard or soft float\n"));
15985 break;
15986 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15987 fprintf (file, _("Hard float (double precision)\n"));
15988 break;
15989 case Val_GNU_MIPS_ABI_FP_SINGLE:
15990 fprintf (file, _("Hard float (single precision)\n"));
15991 break;
15992 case Val_GNU_MIPS_ABI_FP_SOFT:
15993 fprintf (file, _("Soft float\n"));
15994 break;
15995 case Val_GNU_MIPS_ABI_FP_OLD_64:
15996 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
15997 break;
15998 case Val_GNU_MIPS_ABI_FP_XX:
15999 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16000 break;
16001 case Val_GNU_MIPS_ABI_FP_64:
16002 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16003 break;
16004 case Val_GNU_MIPS_ABI_FP_64A:
16005 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16006 break;
16007 default:
16008 fprintf (file, "??? (%d)\n", val);
16009 break;
16010 }
16011}
16012
16013static int
16014get_mips_reg_size (int reg_size)
16015{
16016 return (reg_size == AFL_REG_NONE) ? 0
16017 : (reg_size == AFL_REG_32) ? 32
16018 : (reg_size == AFL_REG_64) ? 64
16019 : (reg_size == AFL_REG_128) ? 128
16020 : -1;
16021}
16022
b34976b6 16023bfd_boolean
9719ad41 16024_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 16025{
9719ad41 16026 FILE *file = ptr;
b49e97c9
TS
16027
16028 BFD_ASSERT (abfd != NULL && ptr != NULL);
16029
16030 /* Print normal ELF private data. */
16031 _bfd_elf_print_private_bfd_data (abfd, ptr);
16032
16033 /* xgettext:c-format */
16034 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16035
16036 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16037 fprintf (file, _(" [abi=O32]"));
16038 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16039 fprintf (file, _(" [abi=O64]"));
16040 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16041 fprintf (file, _(" [abi=EABI32]"));
16042 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16043 fprintf (file, _(" [abi=EABI64]"));
16044 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16045 fprintf (file, _(" [abi unknown]"));
16046 else if (ABI_N32_P (abfd))
16047 fprintf (file, _(" [abi=N32]"));
16048 else if (ABI_64_P (abfd))
16049 fprintf (file, _(" [abi=64]"));
16050 else
16051 fprintf (file, _(" [no abi set]"));
16052
16053 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 16054 fprintf (file, " [mips1]");
b49e97c9 16055 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 16056 fprintf (file, " [mips2]");
b49e97c9 16057 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 16058 fprintf (file, " [mips3]");
b49e97c9 16059 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 16060 fprintf (file, " [mips4]");
b49e97c9 16061 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 16062 fprintf (file, " [mips5]");
b49e97c9 16063 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 16064 fprintf (file, " [mips32]");
b49e97c9 16065 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 16066 fprintf (file, " [mips64]");
af7ee8bf 16067 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 16068 fprintf (file, " [mips32r2]");
5f74bc13 16069 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 16070 fprintf (file, " [mips64r2]");
7361da2c
AB
16071 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16072 fprintf (file, " [mips32r6]");
16073 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16074 fprintf (file, " [mips64r6]");
b49e97c9
TS
16075 else
16076 fprintf (file, _(" [unknown ISA]"));
16077
40d32fc6 16078 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 16079 fprintf (file, " [mdmx]");
40d32fc6
CD
16080
16081 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 16082 fprintf (file, " [mips16]");
40d32fc6 16083
df58fc94
RS
16084 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16085 fprintf (file, " [micromips]");
16086
ba92f887
MR
16087 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16088 fprintf (file, " [nan2008]");
16089
5baf5e34 16090 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
351cdf24 16091 fprintf (file, " [old fp64]");
5baf5e34 16092
b49e97c9 16093 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 16094 fprintf (file, " [32bitmode]");
b49e97c9
TS
16095 else
16096 fprintf (file, _(" [not 32bitmode]"));
16097
c0e3f241 16098 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 16099 fprintf (file, " [noreorder]");
c0e3f241
CD
16100
16101 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 16102 fprintf (file, " [PIC]");
c0e3f241
CD
16103
16104 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 16105 fprintf (file, " [CPIC]");
c0e3f241
CD
16106
16107 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 16108 fprintf (file, " [XGOT]");
c0e3f241
CD
16109
16110 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 16111 fprintf (file, " [UCODE]");
c0e3f241 16112
b49e97c9
TS
16113 fputc ('\n', file);
16114
351cdf24
MF
16115 if (mips_elf_tdata (abfd)->abiflags_valid)
16116 {
16117 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16118 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16119 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16120 if (abiflags->isa_rev > 1)
16121 fprintf (file, "r%d", abiflags->isa_rev);
16122 fprintf (file, "\nGPR size: %d",
16123 get_mips_reg_size (abiflags->gpr_size));
16124 fprintf (file, "\nCPR1 size: %d",
16125 get_mips_reg_size (abiflags->cpr1_size));
16126 fprintf (file, "\nCPR2 size: %d",
16127 get_mips_reg_size (abiflags->cpr2_size));
16128 fputs ("\nFP ABI: ", file);
16129 print_mips_fp_abi_value (file, abiflags->fp_abi);
16130 fputs ("ISA Extension: ", file);
16131 print_mips_isa_ext (file, abiflags->isa_ext);
16132 fputs ("\nASEs:", file);
16133 print_mips_ases (file, abiflags->ases);
16134 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16135 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16136 fputc ('\n', file);
16137 }
16138
b34976b6 16139 return TRUE;
b49e97c9 16140}
2f89ff8d 16141
b35d266b 16142const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 16143{
07d6d2b8
AM
16144 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16145 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
0112cd26 16146 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
07d6d2b8 16147 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
0112cd26
NC
16148 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16149 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
07d6d2b8 16150 { NULL, 0, 0, 0, 0 }
2f89ff8d 16151};
5e2b0d47 16152
8992f0d7
TS
16153/* Merge non visibility st_other attributes. Ensure that the
16154 STO_OPTIONAL flag is copied into h->other, even if this is not a
16155 definiton of the symbol. */
5e2b0d47
NC
16156void
16157_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16158 const Elf_Internal_Sym *isym,
16159 bfd_boolean definition,
16160 bfd_boolean dynamic ATTRIBUTE_UNUSED)
16161{
8992f0d7
TS
16162 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16163 {
16164 unsigned char other;
16165
16166 other = (definition ? isym->st_other : h->other);
16167 other &= ~ELF_ST_VISIBILITY (-1);
16168 h->other = other | ELF_ST_VISIBILITY (h->other);
16169 }
16170
16171 if (!definition
5e2b0d47
NC
16172 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
16173 h->other |= STO_OPTIONAL;
16174}
12ac1cf5
NC
16175
16176/* Decide whether an undefined symbol is special and can be ignored.
16177 This is the case for OPTIONAL symbols on IRIX. */
16178bfd_boolean
16179_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16180{
16181 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
16182}
e0764319
NC
16183
16184bfd_boolean
16185_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16186{
16187 return (sym->st_shndx == SHN_COMMON
16188 || sym->st_shndx == SHN_MIPS_ACOMMON
16189 || sym->st_shndx == SHN_MIPS_SCOMMON);
16190}
861fb55a
DJ
16191
16192/* Return address for Ith PLT stub in section PLT, for relocation REL
16193 or (bfd_vma) -1 if it should not be included. */
16194
16195bfd_vma
16196_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16197 const arelent *rel ATTRIBUTE_UNUSED)
16198{
16199 return (plt->vma
16200 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16201 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16202}
16203
1bbce132
MR
16204/* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16205 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16206 and .got.plt and also the slots may be of a different size each we walk
16207 the PLT manually fetching instructions and matching them against known
16208 patterns. To make things easier standard MIPS slots, if any, always come
16209 first. As we don't create proper ELF symbols we use the UDATA.I member
16210 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16211 with the ST_OTHER member of the ELF symbol. */
16212
16213long
16214_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16215 long symcount ATTRIBUTE_UNUSED,
16216 asymbol **syms ATTRIBUTE_UNUSED,
16217 long dynsymcount, asymbol **dynsyms,
16218 asymbol **ret)
16219{
16220 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16221 static const char microsuffix[] = "@micromipsplt";
16222 static const char m16suffix[] = "@mips16plt";
16223 static const char mipssuffix[] = "@plt";
16224
16225 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
16226 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16227 bfd_boolean micromips_p = MICROMIPS_P (abfd);
16228 Elf_Internal_Shdr *hdr;
16229 bfd_byte *plt_data;
16230 bfd_vma plt_offset;
16231 unsigned int other;
16232 bfd_vma entry_size;
16233 bfd_vma plt0_size;
16234 asection *relplt;
16235 bfd_vma opcode;
16236 asection *plt;
16237 asymbol *send;
16238 size_t size;
16239 char *names;
16240 long counti;
16241 arelent *p;
16242 asymbol *s;
16243 char *nend;
16244 long count;
16245 long pi;
16246 long i;
16247 long n;
16248
16249 *ret = NULL;
16250
16251 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16252 return 0;
16253
16254 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16255 if (relplt == NULL)
16256 return 0;
16257
16258 hdr = &elf_section_data (relplt)->this_hdr;
16259 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16260 return 0;
16261
16262 plt = bfd_get_section_by_name (abfd, ".plt");
16263 if (plt == NULL)
16264 return 0;
16265
16266 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16267 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
16268 return -1;
16269 p = relplt->relocation;
16270
16271 /* Calculating the exact amount of space required for symbols would
16272 require two passes over the PLT, so just pessimise assuming two
16273 PLT slots per relocation. */
16274 count = relplt->size / hdr->sh_entsize;
16275 counti = count * bed->s->int_rels_per_ext_rel;
16276 size = 2 * count * sizeof (asymbol);
16277 size += count * (sizeof (mipssuffix) +
16278 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16279 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16280 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16281
16282 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16283 size += sizeof (asymbol) + sizeof (pltname);
16284
16285 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16286 return -1;
16287
16288 if (plt->size < 16)
16289 return -1;
16290
16291 s = *ret = bfd_malloc (size);
16292 if (s == NULL)
16293 return -1;
16294 send = s + 2 * count + 1;
16295
16296 names = (char *) send;
16297 nend = (char *) s + size;
16298 n = 0;
16299
16300 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16301 if (opcode == 0x3302fffe)
16302 {
16303 if (!micromips_p)
16304 return -1;
16305 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16306 other = STO_MICROMIPS;
16307 }
833794fc
MR
16308 else if (opcode == 0x0398c1d0)
16309 {
16310 if (!micromips_p)
16311 return -1;
16312 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16313 other = STO_MICROMIPS;
16314 }
1bbce132
MR
16315 else
16316 {
16317 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16318 other = 0;
16319 }
16320
16321 s->the_bfd = abfd;
16322 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16323 s->section = plt;
16324 s->value = 0;
16325 s->name = names;
16326 s->udata.i = other;
16327 memcpy (names, pltname, sizeof (pltname));
16328 names += sizeof (pltname);
16329 ++s, ++n;
16330
16331 pi = 0;
16332 for (plt_offset = plt0_size;
16333 plt_offset + 8 <= plt->size && s < send;
16334 plt_offset += entry_size)
16335 {
16336 bfd_vma gotplt_addr;
16337 const char *suffix;
16338 bfd_vma gotplt_hi;
16339 bfd_vma gotplt_lo;
16340 size_t suffixlen;
16341
16342 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16343
16344 /* Check if the second word matches the expected MIPS16 instruction. */
16345 if (opcode == 0x651aeb00)
16346 {
16347 if (micromips_p)
16348 return -1;
16349 /* Truncated table??? */
16350 if (plt_offset + 16 > plt->size)
16351 break;
16352 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16353 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16354 suffixlen = sizeof (m16suffix);
16355 suffix = m16suffix;
16356 other = STO_MIPS16;
16357 }
833794fc 16358 /* Likewise the expected microMIPS instruction (no insn32 mode). */
1bbce132
MR
16359 else if (opcode == 0xff220000)
16360 {
16361 if (!micromips_p)
16362 return -1;
16363 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16364 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16365 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16366 gotplt_lo <<= 2;
16367 gotplt_addr = gotplt_hi + gotplt_lo;
16368 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16369 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16370 suffixlen = sizeof (microsuffix);
16371 suffix = microsuffix;
16372 other = STO_MICROMIPS;
16373 }
833794fc
MR
16374 /* Likewise the expected microMIPS instruction (insn32 mode). */
16375 else if ((opcode & 0xffff0000) == 0xff2f0000)
16376 {
16377 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16378 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16379 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16380 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16381 gotplt_addr = gotplt_hi + gotplt_lo;
16382 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16383 suffixlen = sizeof (microsuffix);
16384 suffix = microsuffix;
16385 other = STO_MICROMIPS;
16386 }
1bbce132
MR
16387 /* Otherwise assume standard MIPS code. */
16388 else
16389 {
16390 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16391 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16392 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16393 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16394 gotplt_addr = gotplt_hi + gotplt_lo;
16395 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16396 suffixlen = sizeof (mipssuffix);
16397 suffix = mipssuffix;
16398 other = 0;
16399 }
16400 /* Truncated table??? */
16401 if (plt_offset + entry_size > plt->size)
16402 break;
16403
16404 for (i = 0;
16405 i < count && p[pi].address != gotplt_addr;
16406 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16407
16408 if (i < count)
16409 {
16410 size_t namelen;
16411 size_t len;
16412
16413 *s = **p[pi].sym_ptr_ptr;
16414 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16415 we are defining a symbol, ensure one of them is set. */
16416 if ((s->flags & BSF_LOCAL) == 0)
16417 s->flags |= BSF_GLOBAL;
16418 s->flags |= BSF_SYNTHETIC;
16419 s->section = plt;
16420 s->value = plt_offset;
16421 s->name = names;
16422 s->udata.i = other;
16423
16424 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16425 namelen = len + suffixlen;
16426 if (names + namelen > nend)
16427 break;
16428
16429 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16430 names += len;
16431 memcpy (names, suffix, suffixlen);
16432 names += suffixlen;
16433
16434 ++s, ++n;
16435 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16436 }
16437 }
16438
16439 free (plt_data);
16440
16441 return n;
16442}
16443
5e7fc731
MR
16444/* Return the ABI flags associated with ABFD if available. */
16445
16446Elf_Internal_ABIFlags_v0 *
16447bfd_mips_elf_get_abiflags (bfd *abfd)
16448{
16449 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16450
16451 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16452}
16453
bb29b84d
MR
16454/* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16455 field. Taken from `libc-abis.h' generated at GNU libc build time.
16456 Using a MIPS_ prefix as other libc targets use different values. */
16457enum
16458{
16459 MIPS_LIBC_ABI_DEFAULT = 0,
16460 MIPS_LIBC_ABI_MIPS_PLT,
16461 MIPS_LIBC_ABI_UNIQUE,
16462 MIPS_LIBC_ABI_MIPS_O32_FP64,
47275900 16463 MIPS_LIBC_ABI_ABSOLUTE,
bb29b84d
MR
16464 MIPS_LIBC_ABI_MAX
16465};
16466
861fb55a
DJ
16467void
16468_bfd_mips_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
16469{
47275900 16470 struct mips_elf_link_hash_table *htab = NULL;
861fb55a
DJ
16471 Elf_Internal_Ehdr *i_ehdrp;
16472
16473 i_ehdrp = elf_elfheader (abfd);
16474 if (link_info)
16475 {
16476 htab = mips_elf_hash_table (link_info);
4dfe6ac6 16477 BFD_ASSERT (htab != NULL);
861fb55a 16478 }
0af03126 16479
47275900
MR
16480 if (htab != NULL && htab->use_plts_and_copy_relocs && !htab->is_vxworks)
16481 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16482
351cdf24
MF
16483 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16484 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
bb29b84d 16485 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
334cd8a7 16486
47275900
MR
16487 /* Mark that we need support for absolute symbols in the dynamic loader. */
16488 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16489 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16490
334cd8a7 16491 _bfd_elf_post_process_headers (abfd, link_info);
861fb55a 16492}
2f0c68f2
CM
16493
16494int
1ced1a5f
MR
16495_bfd_mips_elf_compact_eh_encoding
16496 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
2f0c68f2
CM
16497{
16498 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16499}
16500
16501/* Return the opcode for can't unwind. */
16502
16503int
1ced1a5f
MR
16504_bfd_mips_elf_cant_unwind_opcode
16505 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
2f0c68f2
CM
16506{
16507 return COMPACT_EH_CANT_UNWIND_OPCODE;
16508}
This page took 2.967058 seconds and 4 git commands to generate.