]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elfxx-mips.c
bfd_get_relocated_section_contents allow NULL data buffer
[thirdparty/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
a2c58332 2 Copyright (C) 1993-2022 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 35#include "elf-bfd.h"
0ba9378a 36#include "ecoff-bfd.h"
b49e97c9
TS
37#include "elfxx-mips.h"
38#include "elf/mips.h"
0a44bf69 39#include "elf-vxworks.h"
2f0c68f2 40#include "dwarf2.h"
b49e97c9
TS
41
42/* Get the ECOFF swapping routines. */
43#include "coff/sym.h"
44#include "coff/symconst.h"
45#include "coff/ecoff.h"
46#include "coff/mips.h"
47
b15e6682
AO
48#include "hashtab.h"
49
9ab066b4
RS
50/* Types of TLS GOT entry. */
51enum mips_got_tls_type {
52 GOT_TLS_NONE,
53 GOT_TLS_GD,
54 GOT_TLS_LDM,
55 GOT_TLS_IE
56};
57
ead49a57 58/* This structure is used to hold information about one GOT entry.
3dff0dd1
RS
59 There are four types of entry:
60
61 (1) an absolute address
62 requires: abfd == NULL
63 fields: d.address
64
65 (2) a SYMBOL + OFFSET address, where SYMBOL is local to an input bfd
66 requires: abfd != NULL, symndx >= 0, tls_type != GOT_TLS_LDM
67 fields: abfd, symndx, d.addend, tls_type
68
69 (3) a SYMBOL address, where SYMBOL is not local to an input bfd
70 requires: abfd != NULL, symndx == -1
71 fields: d.h, tls_type
72
73 (4) a TLS LDM slot
74 requires: abfd != NULL, symndx == 0, tls_type == GOT_TLS_LDM
75 fields: none; there's only one of these per GOT. */
b15e6682
AO
76struct mips_got_entry
77{
3dff0dd1 78 /* One input bfd that needs the GOT entry. */
b15e6682 79 bfd *abfd;
f4416af6
AO
80 /* The index of the symbol, as stored in the relocation r_info, if
81 we have a local symbol; -1 otherwise. */
82 long symndx;
83 union
84 {
85 /* If abfd == NULL, an address that must be stored in the got. */
86 bfd_vma address;
87 /* If abfd != NULL && symndx != -1, the addend of the relocation
88 that should be added to the symbol value. */
89 bfd_vma addend;
90 /* If abfd != NULL && symndx == -1, the hash table entry
3dff0dd1 91 corresponding to a symbol in the GOT. The symbol's entry
020d7251
RS
92 is in the local area if h->global_got_area is GGA_NONE,
93 otherwise it is in the global area. */
f4416af6
AO
94 struct mips_elf_link_hash_entry *h;
95 } d;
0f20cc35 96
9ab066b4
RS
97 /* The TLS type of this GOT entry. An LDM GOT entry will be a local
98 symbol entry with r_symndx == 0. */
0f20cc35
DJ
99 unsigned char tls_type;
100
9ab066b4
RS
101 /* True if we have filled in the GOT contents for a TLS entry,
102 and created the associated relocations. */
103 unsigned char tls_initialized;
104
b15e6682 105 /* The offset from the beginning of the .got section to the entry
f4416af6
AO
106 corresponding to this symbol+addend. If it's a global symbol
107 whose offset is yet to be decided, it's going to be -1. */
108 long gotidx;
b15e6682
AO
109};
110
13db6b44
RS
111/* This structure represents a GOT page reference from an input bfd.
112 Each instance represents a symbol + ADDEND, where the representation
113 of the symbol depends on whether it is local to the input bfd.
114 If it is, then SYMNDX >= 0, and the symbol has index SYMNDX in U.ABFD.
115 Otherwise, SYMNDX < 0 and U.H points to the symbol's hash table entry.
116
117 Page references with SYMNDX >= 0 always become page references
118 in the output. Page references with SYMNDX < 0 only become page
119 references if the symbol binds locally; in other cases, the page
120 reference decays to a global GOT reference. */
121struct mips_got_page_ref
122{
123 long symndx;
124 union
125 {
126 struct mips_elf_link_hash_entry *h;
127 bfd *abfd;
128 } u;
129 bfd_vma addend;
130};
131
c224138d
RS
132/* This structure describes a range of addends: [MIN_ADDEND, MAX_ADDEND].
133 The structures form a non-overlapping list that is sorted by increasing
134 MIN_ADDEND. */
135struct mips_got_page_range
136{
137 struct mips_got_page_range *next;
138 bfd_signed_vma min_addend;
139 bfd_signed_vma max_addend;
140};
141
142/* This structure describes the range of addends that are applied to page
13db6b44 143 relocations against a given section. */
c224138d
RS
144struct mips_got_page_entry
145{
13db6b44
RS
146 /* The section that these entries are based on. */
147 asection *sec;
c224138d
RS
148 /* The ranges for this page entry. */
149 struct mips_got_page_range *ranges;
150 /* The maximum number of page entries needed for RANGES. */
151 bfd_vma num_pages;
152};
153
f0abc2a1 154/* This structure is used to hold .got information when linking. */
b49e97c9
TS
155
156struct mips_got_info
157{
b49e97c9
TS
158 /* The number of global .got entries. */
159 unsigned int global_gotno;
23cc69b6
RS
160 /* The number of global .got entries that are in the GGA_RELOC_ONLY area. */
161 unsigned int reloc_only_gotno;
0f20cc35
DJ
162 /* The number of .got slots used for TLS. */
163 unsigned int tls_gotno;
164 /* The first unused TLS .got entry. Used only during
165 mips_elf_initialize_tls_index. */
166 unsigned int tls_assigned_gotno;
c224138d 167 /* The number of local .got entries, eventually including page entries. */
b49e97c9 168 unsigned int local_gotno;
c224138d
RS
169 /* The maximum number of page entries needed. */
170 unsigned int page_gotno;
ab361d49
RS
171 /* The number of relocations needed for the GOT entries. */
172 unsigned int relocs;
cb22ccf4
KCY
173 /* The first unused local .got entry. */
174 unsigned int assigned_low_gotno;
175 /* The last unused local .got entry. */
176 unsigned int assigned_high_gotno;
b15e6682
AO
177 /* A hash table holding members of the got. */
178 struct htab *got_entries;
13db6b44
RS
179 /* A hash table holding mips_got_page_ref structures. */
180 struct htab *got_page_refs;
c224138d
RS
181 /* A hash table of mips_got_page_entry structures. */
182 struct htab *got_page_entries;
f4416af6
AO
183 /* In multi-got links, a pointer to the next got (err, rather, most
184 of the time, it points to the previous got). */
185 struct mips_got_info *next;
186};
187
d7206569 188/* Structure passed when merging bfds' gots. */
f4416af6
AO
189
190struct mips_elf_got_per_bfd_arg
191{
f4416af6
AO
192 /* The output bfd. */
193 bfd *obfd;
194 /* The link information. */
195 struct bfd_link_info *info;
196 /* A pointer to the primary got, i.e., the one that's going to get
197 the implicit relocations from DT_MIPS_LOCAL_GOTNO and
198 DT_MIPS_GOTSYM. */
199 struct mips_got_info *primary;
200 /* A non-primary got we're trying to merge with other input bfd's
201 gots. */
202 struct mips_got_info *current;
203 /* The maximum number of got entries that can be addressed with a
204 16-bit offset. */
205 unsigned int max_count;
c224138d
RS
206 /* The maximum number of page entries needed by each got. */
207 unsigned int max_pages;
0f20cc35
DJ
208 /* The total number of global entries which will live in the
209 primary got and be automatically relocated. This includes
210 those not referenced by the primary GOT but included in
211 the "master" GOT. */
212 unsigned int global_count;
f4416af6
AO
213};
214
ab361d49
RS
215/* A structure used to pass information to htab_traverse callbacks
216 when laying out the GOT. */
f4416af6 217
ab361d49 218struct mips_elf_traverse_got_arg
f4416af6 219{
ab361d49 220 struct bfd_link_info *info;
f4416af6
AO
221 struct mips_got_info *g;
222 int value;
0f20cc35
DJ
223};
224
f0abc2a1
AM
225struct _mips_elf_section_data
226{
227 struct bfd_elf_section_data elf;
228 union
229 {
f0abc2a1
AM
230 bfd_byte *tdata;
231 } u;
232};
233
234#define mips_elf_section_data(sec) \
68bfbfcc 235 ((struct _mips_elf_section_data *) elf_section_data (sec))
f0abc2a1 236
d5eaccd7
RS
237#define is_mips_elf(bfd) \
238 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
239 && elf_tdata (bfd) != NULL \
4dfe6ac6 240 && elf_object_id (bfd) == MIPS_ELF_DATA)
d5eaccd7 241
634835ae
RS
242/* The ABI says that every symbol used by dynamic relocations must have
243 a global GOT entry. Among other things, this provides the dynamic
244 linker with a free, directly-indexed cache. The GOT can therefore
245 contain symbols that are not referenced by GOT relocations themselves
246 (in other words, it may have symbols that are not referenced by things
247 like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
248
249 GOT relocations are less likely to overflow if we put the associated
250 GOT entries towards the beginning. We therefore divide the global
251 GOT entries into two areas: "normal" and "reloc-only". Entries in
252 the first area can be used for both dynamic relocations and GP-relative
253 accesses, while those in the "reloc-only" area are for dynamic
254 relocations only.
255
256 These GGA_* ("Global GOT Area") values are organised so that lower
257 values are more general than higher values. Also, non-GGA_NONE
258 values are ordered by the position of the area in the GOT. */
259#define GGA_NORMAL 0
260#define GGA_RELOC_ONLY 1
261#define GGA_NONE 2
262
861fb55a
DJ
263/* Information about a non-PIC interface to a PIC function. There are
264 two ways of creating these interfaces. The first is to add:
265
266 lui $25,%hi(func)
267 addiu $25,$25,%lo(func)
268
269 immediately before a PIC function "func". The second is to add:
270
271 lui $25,%hi(func)
272 j func
273 addiu $25,$25,%lo(func)
274
275 to a separate trampoline section.
276
277 Stubs of the first kind go in a new section immediately before the
278 target function. Stubs of the second kind go in a single section
279 pointed to by the hash table's "strampoline" field. */
280struct mips_elf_la25_stub {
281 /* The generated section that contains this stub. */
282 asection *stub_section;
283
284 /* The offset of the stub from the start of STUB_SECTION. */
285 bfd_vma offset;
286
287 /* One symbol for the original function. Its location is available
288 in H->root.root.u.def. */
289 struct mips_elf_link_hash_entry *h;
290};
291
292/* Macros for populating a mips_elf_la25_stub. */
293
294#define LA25_LUI(VAL) (0x3c190000 | (VAL)) /* lui t9,VAL */
295#define LA25_J(VAL) (0x08000000 | (((VAL) >> 2) & 0x3ffffff)) /* j VAL */
3734320d 296#define LA25_BC(VAL) (0xc8000000 | (((VAL) >> 2) & 0x3ffffff)) /* bc VAL */
861fb55a 297#define LA25_ADDIU(VAL) (0x27390000 | (VAL)) /* addiu t9,t9,VAL */
d21911ea
MR
298#define LA25_LUI_MICROMIPS(VAL) \
299 (0x41b90000 | (VAL)) /* lui t9,VAL */
300#define LA25_J_MICROMIPS(VAL) \
301 (0xd4000000 | (((VAL) >> 1) & 0x3ffffff)) /* j VAL */
302#define LA25_ADDIU_MICROMIPS(VAL) \
303 (0x33390000 | (VAL)) /* addiu t9,t9,VAL */
861fb55a 304
b49e97c9
TS
305/* This structure is passed to mips_elf_sort_hash_table_f when sorting
306 the dynamic symbols. */
307
308struct mips_elf_hash_sort_data
309{
310 /* The symbol in the global GOT with the lowest dynamic symbol table
311 index. */
312 struct elf_link_hash_entry *low;
0f20cc35
DJ
313 /* The least dynamic symbol table index corresponding to a non-TLS
314 symbol with a GOT entry. */
55f8b9d2 315 bfd_size_type min_got_dynindx;
f4416af6
AO
316 /* The greatest dynamic symbol table index corresponding to a symbol
317 with a GOT entry that is not referenced (e.g., a dynamic symbol
9e4aeb93 318 with dynamic relocations pointing to it from non-primary GOTs). */
55f8b9d2 319 bfd_size_type max_unref_got_dynindx;
e17b0c35
MR
320 /* The greatest dynamic symbol table index corresponding to a local
321 symbol. */
322 bfd_size_type max_local_dynindx;
323 /* The greatest dynamic symbol table index corresponding to an external
b49e97c9 324 symbol without a GOT entry. */
55f8b9d2 325 bfd_size_type max_non_got_dynindx;
f16a9783
MS
326 /* If non-NULL, output BFD for .MIPS.xhash finalization. */
327 bfd *output_bfd;
328 /* If non-NULL, pointer to contents of .MIPS.xhash for filling in
329 real final dynindx. */
330 bfd_byte *mipsxhash;
b49e97c9
TS
331};
332
1bbce132
MR
333/* We make up to two PLT entries if needed, one for standard MIPS code
334 and one for compressed code, either a MIPS16 or microMIPS one. We
335 keep a separate record of traditional lazy-binding stubs, for easier
336 processing. */
337
338struct plt_entry
339{
340 /* Traditional SVR4 stub offset, or -1 if none. */
341 bfd_vma stub_offset;
342
343 /* Standard PLT entry offset, or -1 if none. */
344 bfd_vma mips_offset;
345
346 /* Compressed PLT entry offset, or -1 if none. */
347 bfd_vma comp_offset;
348
349 /* The corresponding .got.plt index, or -1 if none. */
350 bfd_vma gotplt_index;
351
352 /* Whether we need a standard PLT entry. */
353 unsigned int need_mips : 1;
354
355 /* Whether we need a compressed PLT entry. */
356 unsigned int need_comp : 1;
357};
358
b49e97c9
TS
359/* The MIPS ELF linker needs additional information for each symbol in
360 the global hash table. */
361
362struct mips_elf_link_hash_entry
363{
364 struct elf_link_hash_entry root;
365
366 /* External symbol information. */
367 EXTR esym;
368
861fb55a
DJ
369 /* The la25 stub we have created for ths symbol, if any. */
370 struct mips_elf_la25_stub *la25_stub;
371
b49e97c9
TS
372 /* Number of R_MIPS_32, R_MIPS_REL32, or R_MIPS_64 relocs against
373 this symbol. */
374 unsigned int possibly_dynamic_relocs;
375
b49e97c9
TS
376 /* If there is a stub that 32 bit functions should use to call this
377 16 bit function, this points to the section containing the stub. */
378 asection *fn_stub;
379
b49e97c9
TS
380 /* If there is a stub that 16 bit functions should use to call this
381 32 bit function, this points to the section containing the stub. */
382 asection *call_stub;
383
384 /* This is like the call_stub field, but it is used if the function
385 being called returns a floating point value. */
386 asection *call_fp_stub;
7c5fcef7 387
f16a9783
MS
388 /* If non-zero, location in .MIPS.xhash to write real final dynindx. */
389 bfd_vma mipsxhash_loc;
390
634835ae
RS
391 /* The highest GGA_* value that satisfies all references to this symbol. */
392 unsigned int global_got_area : 2;
393
6ccf4795
RS
394 /* True if all GOT relocations against this symbol are for calls. This is
395 a looser condition than no_fn_stub below, because there may be other
396 non-call non-GOT relocations against the symbol. */
397 unsigned int got_only_for_calls : 1;
398
71782a75
RS
399 /* True if one of the relocations described by possibly_dynamic_relocs
400 is against a readonly section. */
401 unsigned int readonly_reloc : 1;
402
861fb55a
DJ
403 /* True if there is a relocation against this symbol that must be
404 resolved by the static linker (in other words, if the relocation
405 cannot possibly be made dynamic). */
406 unsigned int has_static_relocs : 1;
407
71782a75
RS
408 /* True if we must not create a .MIPS.stubs entry for this symbol.
409 This is set, for example, if there are relocations related to
410 taking the function's address, i.e. any but R_MIPS_CALL*16 ones.
411 See "MIPS ABI Supplement, 3rd Edition", p. 4-20. */
412 unsigned int no_fn_stub : 1;
413
414 /* Whether we need the fn_stub; this is true if this symbol appears
415 in any relocs other than a 16 bit call. */
416 unsigned int need_fn_stub : 1;
417
861fb55a
DJ
418 /* True if this symbol is referenced by branch relocations from
419 any non-PIC input file. This is used to determine whether an
420 la25 stub is required. */
421 unsigned int has_nonpic_branches : 1;
33bb52fb
RS
422
423 /* Does this symbol need a traditional MIPS lazy-binding stub
424 (as opposed to a PLT entry)? */
425 unsigned int needs_lazy_stub : 1;
1bbce132
MR
426
427 /* Does this symbol resolve to a PLT entry? */
428 unsigned int use_plt_entry : 1;
b49e97c9
TS
429};
430
431/* MIPS ELF linker hash table. */
432
433struct mips_elf_link_hash_table
434{
435 struct elf_link_hash_table root;
861fb55a 436
b49e97c9
TS
437 /* The number of .rtproc entries. */
438 bfd_size_type procedure_count;
861fb55a 439
b49e97c9
TS
440 /* The size of the .compact_rel section (if SGI_COMPAT). */
441 bfd_size_type compact_rel_size;
861fb55a 442
e6aea42d
MR
443 /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
444 is set to the address of __rld_obj_head as in IRIX5 and IRIX6. */
0a1b45a2 445 bool use_rld_obj_head;
861fb55a 446
b4082c70
DD
447 /* The __rld_map or __rld_obj_head symbol. */
448 struct elf_link_hash_entry *rld_symbol;
861fb55a 449
b49e97c9 450 /* This is set if we see any mips16 stub sections. */
0a1b45a2 451 bool mips16_stubs_seen;
861fb55a
DJ
452
453 /* True if we can generate copy relocs and PLTs. */
0a1b45a2 454 bool use_plts_and_copy_relocs;
861fb55a 455
833794fc 456 /* True if we can only use 32-bit microMIPS instructions. */
0a1b45a2 457 bool insn32;
833794fc 458
8b10b0b3 459 /* True if we suppress checks for invalid branches between ISA modes. */
0a1b45a2 460 bool ignore_branch_isa;
8b10b0b3 461
3734320d 462 /* True if we are targetting R6 compact branches. */
0a1b45a2 463 bool compact_branches;
3734320d 464
0e53d9da 465 /* True if we already reported the small-data section overflow. */
0a1b45a2 466 bool small_data_overflow_reported;
861fb55a 467
47275900 468 /* True if we use the special `__gnu_absolute_zero' symbol. */
0a1b45a2 469 bool use_absolute_zero;
47275900
MR
470
471 /* True if we have been configured for a GNU target. */
0a1b45a2 472 bool gnu_target;
47275900 473
0a44bf69
RS
474 /* Shortcuts to some dynamic sections, or NULL if they are not
475 being used. */
0a44bf69 476 asection *srelplt2;
4e41d0d7 477 asection *sstubs;
861fb55a 478
a8028dd0
RS
479 /* The master GOT information. */
480 struct mips_got_info *got_info;
861fb55a 481
d222d210
RS
482 /* The global symbol in the GOT with the lowest index in the dynamic
483 symbol table. */
484 struct elf_link_hash_entry *global_gotsym;
485
861fb55a 486 /* The size of the PLT header in bytes. */
0a44bf69 487 bfd_vma plt_header_size;
861fb55a 488
1bbce132
MR
489 /* The size of a standard PLT entry in bytes. */
490 bfd_vma plt_mips_entry_size;
491
492 /* The size of a compressed PLT entry in bytes. */
493 bfd_vma plt_comp_entry_size;
494
495 /* The offset of the next standard PLT entry to create. */
496 bfd_vma plt_mips_offset;
497
498 /* The offset of the next compressed PLT entry to create. */
499 bfd_vma plt_comp_offset;
500
501 /* The index of the next .got.plt entry to create. */
502 bfd_vma plt_got_index;
861fb55a 503
33bb52fb
RS
504 /* The number of functions that need a lazy-binding stub. */
505 bfd_vma lazy_stub_count;
861fb55a 506
5108fc1b
RS
507 /* The size of a function stub entry in bytes. */
508 bfd_vma function_stub_size;
861fb55a
DJ
509
510 /* The number of reserved entries at the beginning of the GOT. */
511 unsigned int reserved_gotno;
512
513 /* The section used for mips_elf_la25_stub trampolines.
514 See the comment above that structure for details. */
515 asection *strampoline;
516
517 /* A table of mips_elf_la25_stubs, indexed by (input_section, offset)
518 pairs. */
519 htab_t la25_stubs;
520
521 /* A function FN (NAME, IS, OS) that creates a new input section
522 called NAME and links it to output section OS. If IS is nonnull,
523 the new section should go immediately before it, otherwise it
524 should go at the (current) beginning of OS.
525
526 The function returns the new section on success, otherwise it
527 returns null. */
528 asection *(*add_stub_section) (const char *, asection *, asection *);
13db6b44 529
1bbce132
MR
530 /* Is the PLT header compressed? */
531 unsigned int plt_header_is_comp : 1;
861fb55a
DJ
532};
533
4dfe6ac6
NC
534/* Get the MIPS ELF linker hash table from a link_info structure. */
535
536#define mips_elf_hash_table(p) \
0f55320b
AM
537 ((is_elf_hash_table ((p)->hash) \
538 && elf_hash_table_id (elf_hash_table (p)) == MIPS_ELF_DATA) \
539 ? (struct mips_elf_link_hash_table *) (p)->hash : NULL)
4dfe6ac6 540
861fb55a 541/* A structure used to communicate with htab_traverse callbacks. */
4dfe6ac6
NC
542struct mips_htab_traverse_info
543{
861fb55a
DJ
544 /* The usual link-wide information. */
545 struct bfd_link_info *info;
546 bfd *output_bfd;
547
548 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
0a1b45a2 549 bool error;
b49e97c9
TS
550};
551
c799eddb
AM
552/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
553 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
554 that contains the relocation field and DATA points to the start of
555 INPUT_SECTION. */
556
557struct mips_hi16
558{
559 struct mips_hi16 *next;
560 bfd_byte *data;
561 asection *input_section;
562 arelent rel;
563};
564
6ae68ba3
MR
565/* MIPS ELF private object data. */
566
567struct mips_elf_obj_tdata
568{
569 /* Generic ELF private object data. */
570 struct elf_obj_tdata root;
571
572 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
573 bfd *abi_fp_bfd;
ee227692 574
b60bf9be
CF
575 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
576 bfd *abi_msa_bfd;
577
351cdf24
MF
578 /* The abiflags for this object. */
579 Elf_Internal_ABIFlags_v0 abiflags;
0a1b45a2 580 bool abiflags_valid;
351cdf24 581
ee227692
RS
582 /* The GOT requirements of input bfds. */
583 struct mips_got_info *got;
698600e4
AM
584
585 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
586 included directly in this one, but there's no point to wasting
587 the memory just for the infrequently called find_nearest_line. */
588 struct mips_elf_find_line *find_line_info;
589
590 /* An array of stub sections indexed by symbol number. */
591 asection **local_stubs;
592 asection **local_call_stubs;
593
594 /* The Irix 5 support uses two virtual sections, which represent
595 text/data symbols defined in dynamic objects. */
596 asymbol *elf_data_symbol;
597 asymbol *elf_text_symbol;
598 asection *elf_data_section;
599 asection *elf_text_section;
c799eddb
AM
600
601 struct mips_hi16 *mips_hi16_list;
6ae68ba3
MR
602};
603
604/* Get MIPS ELF private object data from BFD's tdata. */
605
606#define mips_elf_tdata(bfd) \
607 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
608
0f20cc35
DJ
609#define TLS_RELOC_P(r_type) \
610 (r_type == R_MIPS_TLS_DTPMOD32 \
611 || r_type == R_MIPS_TLS_DTPMOD64 \
612 || r_type == R_MIPS_TLS_DTPREL32 \
613 || r_type == R_MIPS_TLS_DTPREL64 \
614 || r_type == R_MIPS_TLS_GD \
615 || r_type == R_MIPS_TLS_LDM \
616 || r_type == R_MIPS_TLS_DTPREL_HI16 \
617 || r_type == R_MIPS_TLS_DTPREL_LO16 \
618 || r_type == R_MIPS_TLS_GOTTPREL \
619 || r_type == R_MIPS_TLS_TPREL32 \
620 || r_type == R_MIPS_TLS_TPREL64 \
621 || r_type == R_MIPS_TLS_TPREL_HI16 \
df58fc94 622 || r_type == R_MIPS_TLS_TPREL_LO16 \
d0f13682
CLT
623 || r_type == R_MIPS16_TLS_GD \
624 || r_type == R_MIPS16_TLS_LDM \
625 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
626 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
627 || r_type == R_MIPS16_TLS_GOTTPREL \
628 || r_type == R_MIPS16_TLS_TPREL_HI16 \
629 || r_type == R_MIPS16_TLS_TPREL_LO16 \
df58fc94
RS
630 || r_type == R_MICROMIPS_TLS_GD \
631 || r_type == R_MICROMIPS_TLS_LDM \
632 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
633 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
634 || r_type == R_MICROMIPS_TLS_GOTTPREL \
635 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
636 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
0f20cc35 637
b49e97c9
TS
638/* Structure used to pass information to mips_elf_output_extsym. */
639
640struct extsym_info
641{
9e4aeb93
RS
642 bfd *abfd;
643 struct bfd_link_info *info;
b49e97c9
TS
644 struct ecoff_debug_info *debug;
645 const struct ecoff_debug_swap *swap;
0a1b45a2 646 bool failed;
b49e97c9
TS
647};
648
8dc1a139 649/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
650
651static const char * const mips_elf_dynsym_rtproc_names[] =
652{
653 "_procedure_table",
654 "_procedure_string_table",
655 "_procedure_table_size",
656 NULL
657};
658
659/* These structures are used to generate the .compact_rel section on
8dc1a139 660 IRIX5. */
b49e97c9
TS
661
662typedef struct
663{
664 unsigned long id1; /* Always one? */
665 unsigned long num; /* Number of compact relocation entries. */
666 unsigned long id2; /* Always two? */
667 unsigned long offset; /* The file offset of the first relocation. */
668 unsigned long reserved0; /* Zero? */
669 unsigned long reserved1; /* Zero? */
670} Elf32_compact_rel;
671
672typedef struct
673{
674 bfd_byte id1[4];
675 bfd_byte num[4];
676 bfd_byte id2[4];
677 bfd_byte offset[4];
678 bfd_byte reserved0[4];
679 bfd_byte reserved1[4];
680} Elf32_External_compact_rel;
681
682typedef struct
683{
684 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
685 unsigned int rtype : 4; /* Relocation types. See below. */
686 unsigned int dist2to : 8;
687 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
688 unsigned long konst; /* KONST field. See below. */
689 unsigned long vaddr; /* VADDR to be relocated. */
690} Elf32_crinfo;
691
692typedef struct
693{
694 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
695 unsigned int rtype : 4; /* Relocation types. See below. */
696 unsigned int dist2to : 8;
697 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
698 unsigned long konst; /* KONST field. See below. */
699} Elf32_crinfo2;
700
701typedef struct
702{
703 bfd_byte info[4];
704 bfd_byte konst[4];
705 bfd_byte vaddr[4];
706} Elf32_External_crinfo;
707
708typedef struct
709{
710 bfd_byte info[4];
711 bfd_byte konst[4];
712} Elf32_External_crinfo2;
713
714/* These are the constants used to swap the bitfields in a crinfo. */
715
227d539b 716#define CRINFO_CTYPE (0x1U)
b49e97c9 717#define CRINFO_CTYPE_SH (31)
227d539b 718#define CRINFO_RTYPE (0xfU)
b49e97c9 719#define CRINFO_RTYPE_SH (27)
227d539b 720#define CRINFO_DIST2TO (0xffU)
b49e97c9 721#define CRINFO_DIST2TO_SH (19)
227d539b 722#define CRINFO_RELVADDR (0x7ffffU)
b49e97c9
TS
723#define CRINFO_RELVADDR_SH (0)
724
725/* A compact relocation info has long (3 words) or short (2 words)
726 formats. A short format doesn't have VADDR field and relvaddr
727 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
728#define CRF_MIPS_LONG 1
729#define CRF_MIPS_SHORT 0
730
731/* There are 4 types of compact relocation at least. The value KONST
732 has different meaning for each type:
733
734 (type) (konst)
735 CT_MIPS_REL32 Address in data
736 CT_MIPS_WORD Address in word (XXX)
737 CT_MIPS_GPHI_LO GP - vaddr
738 CT_MIPS_JMPAD Address to jump
739 */
740
741#define CRT_MIPS_REL32 0xa
742#define CRT_MIPS_WORD 0xb
743#define CRT_MIPS_GPHI_LO 0xc
744#define CRT_MIPS_JMPAD 0xd
745
746#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
747#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
748#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
749#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
750\f
751/* The structure of the runtime procedure descriptor created by the
752 loader for use by the static exception system. */
753
754typedef struct runtime_pdr {
ae9a127f
NC
755 bfd_vma adr; /* Memory address of start of procedure. */
756 long regmask; /* Save register mask. */
757 long regoffset; /* Save register offset. */
758 long fregmask; /* Save floating point register mask. */
759 long fregoffset; /* Save floating point register offset. */
760 long frameoffset; /* Frame size. */
761 short framereg; /* Frame pointer register. */
762 short pcreg; /* Offset or reg of return pc. */
763 long irpss; /* Index into the runtime string table. */
b49e97c9 764 long reserved;
ae9a127f 765 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
766} RPDR, *pRPDR;
767#define cbRPDR sizeof (RPDR)
768#define rpdNil ((pRPDR) 0)
769\f
b15e6682 770static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
771 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
772 struct mips_elf_link_hash_entry *, int);
0a1b45a2 773static bool mips_elf_sort_hash_table_f
9719ad41 774 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
775static bfd_vma mips_elf_high
776 (bfd_vma);
0a1b45a2 777static bool mips_elf_create_dynamic_relocation
9719ad41
RS
778 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
779 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
780 bfd_vma *, asection *);
f4416af6 781static bfd_vma mips_elf_adjust_gp
9719ad41 782 (bfd *, struct mips_got_info *, bfd *);
f4416af6 783
b49e97c9
TS
784/* This will be used when we sort the dynamic relocation records. */
785static bfd *reldyn_sorting_bfd;
786
6d30f5b2
NC
787/* True if ABFD is for CPUs with load interlocking that include
788 non-MIPS1 CPUs and R3900. */
789#define LOAD_INTERLOCKS_P(abfd) \
790 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
791 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
792
cd8d5a82
CF
793/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
794 This should be safe for all architectures. We enable this predicate
795 for RM9000 for now. */
796#define JAL_TO_BAL_P(abfd) \
797 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
798
799/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
800 This should be safe for all architectures. We enable this predicate for
801 all CPUs. */
802#define JALR_TO_BAL_P(abfd) 1
803
38a7df63
CF
804/* True if ABFD is for CPUs that are faster if JR is converted to B.
805 This should be safe for all architectures. We enable this predicate for
806 all CPUs. */
807#define JR_TO_B_P(abfd) 1
808
861fb55a
DJ
809/* True if ABFD is a PIC object. */
810#define PIC_OBJECT_P(abfd) \
811 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
812
351cdf24
MF
813/* Nonzero if ABFD is using the O32 ABI. */
814#define ABI_O32_P(abfd) \
815 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
816
b49e97c9 817/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
818#define ABI_N32_P(abfd) \
819 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
820
4a14403c 821/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 822#define ABI_64_P(abfd) \
141ff970 823 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 824
4a14403c
TS
825/* Nonzero if ABFD is using NewABI conventions. */
826#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
827
e8faf7d1
MR
828/* Nonzero if ABFD has microMIPS code. */
829#define MICROMIPS_P(abfd) \
830 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
831
7361da2c
AB
832/* Nonzero if ABFD is MIPS R6. */
833#define MIPSR6_P(abfd) \
834 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
835 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
836
4a14403c 837/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
838#define IRIX_COMPAT(abfd) \
839 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
840
b49e97c9
TS
841/* Whether we are trying to be compatible with IRIX at all. */
842#define SGI_COMPAT(abfd) \
843 (IRIX_COMPAT (abfd) != ict_none)
844
845/* The name of the options section. */
846#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 847 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 848
cc2e31b9
RS
849/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
850 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
851#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
852 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
853
351cdf24
MF
854/* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
855#define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
856 (strcmp (NAME, ".MIPS.abiflags") == 0)
857
943284cc
DJ
858/* Whether the section is readonly. */
859#define MIPS_ELF_READONLY_SECTION(sec) \
860 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
861 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
862
b49e97c9 863/* The name of the stub section. */
ca07892d 864#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
865
866/* The size of an external REL relocation. */
867#define MIPS_ELF_REL_SIZE(abfd) \
868 (get_elf_backend_data (abfd)->s->sizeof_rel)
869
0a44bf69
RS
870/* The size of an external RELA relocation. */
871#define MIPS_ELF_RELA_SIZE(abfd) \
872 (get_elf_backend_data (abfd)->s->sizeof_rela)
873
b49e97c9
TS
874/* The size of an external dynamic table entry. */
875#define MIPS_ELF_DYN_SIZE(abfd) \
876 (get_elf_backend_data (abfd)->s->sizeof_dyn)
877
878/* The size of a GOT entry. */
879#define MIPS_ELF_GOT_SIZE(abfd) \
880 (get_elf_backend_data (abfd)->s->arch_size / 8)
881
b4082c70
DD
882/* The size of the .rld_map section. */
883#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
884 (get_elf_backend_data (abfd)->s->arch_size / 8)
885
b49e97c9
TS
886/* The size of a symbol-table entry. */
887#define MIPS_ELF_SYM_SIZE(abfd) \
888 (get_elf_backend_data (abfd)->s->sizeof_sym)
889
890/* The default alignment for sections, as a power of two. */
891#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 892 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
893
894/* Get word-sized data. */
895#define MIPS_ELF_GET_WORD(abfd, ptr) \
896 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
897
898/* Put out word-sized data. */
899#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
07d6d2b8
AM
900 (ABI_64_P (abfd) \
901 ? bfd_put_64 (abfd, val, ptr) \
b49e97c9
TS
902 : bfd_put_32 (abfd, val, ptr))
903
861fb55a
DJ
904/* The opcode for word-sized loads (LW or LD). */
905#define MIPS_ELF_LOAD_WORD(abfd) \
906 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
907
b49e97c9 908/* Add a dynamic symbol table-entry. */
9719ad41 909#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 910 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
911
912#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
0aa13fee 913 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
b49e97c9 914
0a44bf69
RS
915/* The name of the dynamic relocation section. */
916#define MIPS_ELF_REL_DYN_NAME(INFO) \
90c14f0c
L
917 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918 ? ".rela.dyn" : ".rel.dyn")
0a44bf69 919
b49e97c9
TS
920/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
921 from smaller values. Start with zero, widen, *then* decrement. */
922#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 923#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 924
51e38d68
RS
925/* The value to write into got[1] for SVR4 targets, to identify it is
926 a GNU object. The dynamic linker can then use got[1] to store the
927 module pointer. */
928#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
929 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
930
f4416af6 931/* The offset of $gp from the beginning of the .got section. */
0a44bf69 932#define ELF_MIPS_GP_OFFSET(INFO) \
90c14f0c
L
933 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
934 ? 0x0 : 0x7ff0)
f4416af6
AO
935
936/* The maximum size of the GOT for it to be addressable using 16-bit
937 offsets from $gp. */
0a44bf69 938#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 939
6a691779 940/* Instructions which appear in a stub. */
3d6746ca
DD
941#define STUB_LW(abfd) \
942 ((ABI_64_P (abfd) \
943 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
07d6d2b8 944 : 0x8f998010)) /* lw t9,0x8010(gp) */
40fc1451 945#define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
3d6746ca 946#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
a18a2a34 947#define STUB_JALR 0x0320f809 /* jalr ra,t9 */
3734320d 948#define STUB_JALRC 0xf8190000 /* jalrc ra,t9 */
5108fc1b
RS
949#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
950#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
951#define STUB_LI16S(abfd, VAL) \
952 ((ABI_64_P (abfd) \
953 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
954 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
955
1bbce132
MR
956/* Likewise for the microMIPS ASE. */
957#define STUB_LW_MICROMIPS(abfd) \
958 (ABI_64_P (abfd) \
959 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
960 : 0xff3c8010) /* lw t9,0x8010(gp) */
961#define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
40fc1451 962#define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
1bbce132
MR
963#define STUB_LUI_MICROMIPS(VAL) \
964 (0x41b80000 + (VAL)) /* lui t8,VAL */
965#define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
833794fc 966#define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
1bbce132
MR
967#define STUB_ORI_MICROMIPS(VAL) \
968 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
969#define STUB_LI16U_MICROMIPS(VAL) \
970 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
971#define STUB_LI16S_MICROMIPS(abfd, VAL) \
972 (ABI_64_P (abfd) \
973 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
974 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
975
5108fc1b
RS
976#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
977#define MIPS_FUNCTION_STUB_BIG_SIZE 20
1bbce132
MR
978#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
979#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
833794fc
MR
980#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
981#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
982
983/* The name of the dynamic interpreter. This is put in the .interp
984 section. */
985
07d6d2b8
AM
986#define ELF_DYNAMIC_INTERPRETER(abfd) \
987 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
988 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
b49e97c9
TS
989 : "/usr/lib/libc.so.1")
990
991#ifdef BFD64
ee6423ed
AO
992#define MNAME(bfd,pre,pos) \
993 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
994#define ELF_R_SYM(bfd, i) \
995 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
996#define ELF_R_TYPE(bfd, i) \
997 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
998#define ELF_R_INFO(bfd, s, t) \
999 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
1000#else
ee6423ed 1001#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
1002#define ELF_R_SYM(bfd, i) \
1003 (ELF32_R_SYM (i))
1004#define ELF_R_TYPE(bfd, i) \
1005 (ELF32_R_TYPE (i))
1006#define ELF_R_INFO(bfd, s, t) \
1007 (ELF32_R_INFO (s, t))
1008#endif
1009\f
1010 /* The mips16 compiler uses a couple of special sections to handle
1011 floating point arguments.
1012
1013 Section names that look like .mips16.fn.FNNAME contain stubs that
1014 copy floating point arguments from the fp regs to the gp regs and
1015 then jump to FNNAME. If any 32 bit function calls FNNAME, the
1016 call should be redirected to the stub instead. If no 32 bit
1017 function calls FNNAME, the stub should be discarded. We need to
1018 consider any reference to the function, not just a call, because
1019 if the address of the function is taken we will need the stub,
1020 since the address might be passed to a 32 bit function.
1021
1022 Section names that look like .mips16.call.FNNAME contain stubs
1023 that copy floating point arguments from the gp regs to the fp
1024 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1025 then any 16 bit function that calls FNNAME should be redirected
1026 to the stub instead. If FNNAME is not a 32 bit function, the
1027 stub should be discarded.
1028
1029 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1030 which call FNNAME and then copy the return value from the fp regs
1031 to the gp regs. These stubs store the return value in $18 while
1032 calling FNNAME; any function which might call one of these stubs
1033 must arrange to save $18 around the call. (This case is not
1034 needed for 32 bit functions that call 16 bit functions, because
1035 16 bit functions always return floating point values in both
1036 $f0/$f1 and $2/$3.)
1037
1038 Note that in all cases FNNAME might be defined statically.
1039 Therefore, FNNAME is not used literally. Instead, the relocation
1040 information will indicate which symbol the section is for.
1041
1042 We record any stubs that we find in the symbol table. */
1043
1044#define FN_STUB ".mips16.fn."
1045#define CALL_STUB ".mips16.call."
1046#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71 1047
08dedd66
ML
1048#define FN_STUB_P(name) startswith (name, FN_STUB)
1049#define CALL_STUB_P(name) startswith (name, CALL_STUB)
1050#define CALL_FP_STUB_P(name) startswith (name, CALL_FP_STUB)
b49e97c9 1051\f
861fb55a 1052/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
1053static const bfd_vma mips_o32_exec_plt0_entry[] =
1054{
861fb55a
DJ
1055 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1056 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1057 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1058 0x031cc023, /* subu $24, $24, $28 */
40fc1451 1059 0x03e07825, /* or t7, ra, zero */
861fb55a
DJ
1060 0x0018c082, /* srl $24, $24, 2 */
1061 0x0320f809, /* jalr $25 */
1062 0x2718fffe /* subu $24, $24, 2 */
1063};
1064
3734320d
MF
1065/* The format of the first PLT entry in an O32 executable using compact
1066 jumps. */
1067static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1068{
1069 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1070 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1071 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1072 0x031cc023, /* subu $24, $24, $28 */
1073 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1074 0x0018c082, /* srl $24, $24, 2 */
1075 0x2718fffe, /* subu $24, $24, 2 */
1076 0xf8190000 /* jalrc $25 */
1077};
1078
861fb55a
DJ
1079/* The format of the first PLT entry in an N32 executable. Different
1080 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
1081static const bfd_vma mips_n32_exec_plt0_entry[] =
1082{
861fb55a
DJ
1083 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1084 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1085 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1086 0x030ec023, /* subu $24, $24, $14 */
40fc1451 1087 0x03e07825, /* or t7, ra, zero */
861fb55a
DJ
1088 0x0018c082, /* srl $24, $24, 2 */
1089 0x0320f809, /* jalr $25 */
1090 0x2718fffe /* subu $24, $24, 2 */
1091};
1092
3734320d
MF
1093/* The format of the first PLT entry in an N32 executable using compact
1094 jumps. Different because gp ($28) is not available; we use t2 ($14)
1095 instead. */
1096static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1097{
1098 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1099 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1100 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1101 0x030ec023, /* subu $24, $24, $14 */
1102 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1103 0x0018c082, /* srl $24, $24, 2 */
1104 0x2718fffe, /* subu $24, $24, 2 */
1105 0xf8190000 /* jalrc $25 */
1106};
1107
861fb55a
DJ
1108/* The format of the first PLT entry in an N64 executable. Different
1109 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
1110static const bfd_vma mips_n64_exec_plt0_entry[] =
1111{
861fb55a
DJ
1112 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1113 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1114 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1115 0x030ec023, /* subu $24, $24, $14 */
40fc1451 1116 0x03e07825, /* or t7, ra, zero */
861fb55a
DJ
1117 0x0018c0c2, /* srl $24, $24, 3 */
1118 0x0320f809, /* jalr $25 */
1119 0x2718fffe /* subu $24, $24, 2 */
1120};
1121
3734320d
MF
1122/* The format of the first PLT entry in an N64 executable using compact
1123 jumps. Different from N32 because of the increased size of GOT
1124 entries. */
1125static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1126{
1127 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1128 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1129 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1130 0x030ec023, /* subu $24, $24, $14 */
1131 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1132 0x0018c0c2, /* srl $24, $24, 3 */
1133 0x2718fffe, /* subu $24, $24, 2 */
1134 0xf8190000 /* jalrc $25 */
1135};
1136
1137
1bbce132
MR
1138/* The format of the microMIPS first PLT entry in an O32 executable.
1139 We rely on v0 ($2) rather than t8 ($24) to contain the address
1140 of the GOTPLT entry handled, so this stub may only be used when
1141 all the subsequent PLT entries are microMIPS code too.
1142
1143 The trailing NOP is for alignment and correct disassembly only. */
1144static const bfd_vma micromips_o32_exec_plt0_entry[] =
1145{
1146 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1147 0xff23, 0x0000, /* lw $25, 0($3) */
1148 0x0535, /* subu $2, $2, $3 */
1149 0x2525, /* srl $2, $2, 2 */
1150 0x3302, 0xfffe, /* subu $24, $2, 2 */
1151 0x0dff, /* move $15, $31 */
1152 0x45f9, /* jalrs $25 */
1153 0x0f83, /* move $28, $3 */
1154 0x0c00 /* nop */
1155};
1156
833794fc
MR
1157/* The format of the microMIPS first PLT entry in an O32 executable
1158 in the insn32 mode. */
1159static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1160{
1161 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1162 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1163 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1164 0x0398, 0xc1d0, /* subu $24, $24, $28 */
40fc1451 1165 0x001f, 0x7a90, /* or $15, $31, zero */
833794fc
MR
1166 0x0318, 0x1040, /* srl $24, $24, 2 */
1167 0x03f9, 0x0f3c, /* jalr $25 */
1168 0x3318, 0xfffe /* subu $24, $24, 2 */
1169};
1170
1bbce132 1171/* The format of subsequent standard PLT entries. */
6d30f5b2
NC
1172static const bfd_vma mips_exec_plt_entry[] =
1173{
861fb55a
DJ
1174 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1175 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1176 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1177 0x03200008 /* jr $25 */
1178};
1179
7361da2c
AB
1180static const bfd_vma mipsr6_exec_plt_entry[] =
1181{
1182 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1183 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1184 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1185 0x03200009 /* jr $25 */
1186};
1187
3734320d
MF
1188static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1189{
1190 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1191 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1192 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1193 0xd8190000 /* jic $25, 0 */
1194};
1195
1bbce132
MR
1196/* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1197 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1198 directly addressable. */
1199static const bfd_vma mips16_o32_exec_plt_entry[] =
1200{
1201 0xb203, /* lw $2, 12($pc) */
1202 0x9a60, /* lw $3, 0($2) */
1203 0x651a, /* move $24, $2 */
1204 0xeb00, /* jr $3 */
1205 0x653b, /* move $25, $3 */
1206 0x6500, /* nop */
1207 0x0000, 0x0000 /* .word (.got.plt entry) */
1208};
1209
1210/* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1211 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1212static const bfd_vma micromips_o32_exec_plt_entry[] =
1213{
1214 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1215 0xff22, 0x0000, /* lw $25, 0($2) */
1216 0x4599, /* jr $25 */
1217 0x0f02 /* move $24, $2 */
1218};
1219
833794fc
MR
1220/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1221static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1222{
1223 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1224 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1225 0x0019, 0x0f3c, /* jr $25 */
1226 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1227};
1228
0a44bf69 1229/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
1230static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1231{
0a44bf69
RS
1232 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1233 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1234 0x8f390008, /* lw t9, 8(t9) */
1235 0x00000000, /* nop */
1236 0x03200008, /* jr t9 */
1237 0x00000000 /* nop */
1238};
1239
1240/* The format of subsequent PLT entries. */
6d30f5b2
NC
1241static const bfd_vma mips_vxworks_exec_plt_entry[] =
1242{
0a44bf69
RS
1243 0x10000000, /* b .PLT_resolver */
1244 0x24180000, /* li t8, <pltindex> */
1245 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1246 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1247 0x8f390000, /* lw t9, 0(t9) */
1248 0x00000000, /* nop */
1249 0x03200008, /* jr t9 */
1250 0x00000000 /* nop */
1251};
1252
1253/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
1254static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1255{
0a44bf69
RS
1256 0x8f990008, /* lw t9, 8(gp) */
1257 0x00000000, /* nop */
1258 0x03200008, /* jr t9 */
1259 0x00000000, /* nop */
1260 0x00000000, /* nop */
1261 0x00000000 /* nop */
1262};
1263
1264/* The format of subsequent PLT entries. */
6d30f5b2
NC
1265static const bfd_vma mips_vxworks_shared_plt_entry[] =
1266{
0a44bf69
RS
1267 0x10000000, /* b .PLT_resolver */
1268 0x24180000 /* li t8, <pltindex> */
1269};
1270\f
d21911ea
MR
1271/* microMIPS 32-bit opcode helper installer. */
1272
1273static void
1274bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1275{
1276 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
07d6d2b8 1277 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
d21911ea
MR
1278}
1279
1280/* microMIPS 32-bit opcode helper retriever. */
1281
1282static bfd_vma
1283bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1284{
1285 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1286}
1287\f
b49e97c9
TS
1288/* Look up an entry in a MIPS ELF linker hash table. */
1289
1290#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1291 ((struct mips_elf_link_hash_entry *) \
1292 elf_link_hash_lookup (&(table)->root, (string), (create), \
1293 (copy), (follow)))
1294
1295/* Traverse a MIPS ELF linker hash table. */
1296
1297#define mips_elf_link_hash_traverse(table, func, info) \
1298 (elf_link_hash_traverse \
1299 (&(table)->root, \
0a1b45a2 1300 (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1301 (info)))
1302
0f20cc35
DJ
1303/* Find the base offsets for thread-local storage in this object,
1304 for GD/LD and IE/LE respectively. */
1305
1306#define TP_OFFSET 0x7000
1307#define DTP_OFFSET 0x8000
1308
1309static bfd_vma
1310dtprel_base (struct bfd_link_info *info)
1311{
1312 /* If tls_sec is NULL, we should have signalled an error already. */
1313 if (elf_hash_table (info)->tls_sec == NULL)
1314 return 0;
1315 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1316}
1317
1318static bfd_vma
1319tprel_base (struct bfd_link_info *info)
1320{
1321 /* If tls_sec is NULL, we should have signalled an error already. */
1322 if (elf_hash_table (info)->tls_sec == NULL)
1323 return 0;
1324 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1325}
1326
b49e97c9
TS
1327/* Create an entry in a MIPS ELF linker hash table. */
1328
1329static struct bfd_hash_entry *
9719ad41
RS
1330mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1331 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1332{
1333 struct mips_elf_link_hash_entry *ret =
1334 (struct mips_elf_link_hash_entry *) entry;
1335
1336 /* Allocate the structure if it has not already been allocated by a
1337 subclass. */
9719ad41
RS
1338 if (ret == NULL)
1339 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1340 if (ret == NULL)
b49e97c9
TS
1341 return (struct bfd_hash_entry *) ret;
1342
1343 /* Call the allocation method of the superclass. */
1344 ret = ((struct mips_elf_link_hash_entry *)
1345 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1346 table, string));
9719ad41 1347 if (ret != NULL)
b49e97c9
TS
1348 {
1349 /* Set local fields. */
1350 memset (&ret->esym, 0, sizeof (EXTR));
1351 /* We use -2 as a marker to indicate that the information has
1352 not been set. -1 means there is no associated ifd. */
1353 ret->esym.ifd = -2;
861fb55a 1354 ret->la25_stub = 0;
b49e97c9 1355 ret->possibly_dynamic_relocs = 0;
b49e97c9 1356 ret->fn_stub = NULL;
b49e97c9
TS
1357 ret->call_stub = NULL;
1358 ret->call_fp_stub = NULL;
f16a9783 1359 ret->mipsxhash_loc = 0;
634835ae 1360 ret->global_got_area = GGA_NONE;
0a1b45a2
AM
1361 ret->got_only_for_calls = true;
1362 ret->readonly_reloc = false;
1363 ret->has_static_relocs = false;
1364 ret->no_fn_stub = false;
1365 ret->need_fn_stub = false;
1366 ret->has_nonpic_branches = false;
1367 ret->needs_lazy_stub = false;
1368 ret->use_plt_entry = false;
b49e97c9
TS
1369 }
1370
1371 return (struct bfd_hash_entry *) ret;
1372}
f0abc2a1 1373
6ae68ba3
MR
1374/* Allocate MIPS ELF private object data. */
1375
0a1b45a2 1376bool
6ae68ba3
MR
1377_bfd_mips_elf_mkobject (bfd *abfd)
1378{
1379 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1380 MIPS_ELF_DATA);
1381}
1382
c799eddb
AM
1383bool
1384_bfd_mips_elf_close_and_cleanup (bfd *abfd)
1385{
1386 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
1387 if (tdata != NULL && bfd_get_format (abfd) == bfd_object)
1388 {
1389 BFD_ASSERT (tdata->root.object_id == MIPS_ELF_DATA);
1390 while (tdata->mips_hi16_list != NULL)
1391 {
1392 struct mips_hi16 *hi = tdata->mips_hi16_list;
1393 tdata->mips_hi16_list = hi->next;
1394 free (hi);
1395 }
1396 }
1397 return _bfd_elf_close_and_cleanup (abfd);
1398}
1399
0a1b45a2 1400bool
9719ad41 1401_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1402{
f592407e
AM
1403 if (!sec->used_by_bfd)
1404 {
1405 struct _mips_elf_section_data *sdata;
986f0783 1406 size_t amt = sizeof (*sdata);
f0abc2a1 1407
f592407e
AM
1408 sdata = bfd_zalloc (abfd, amt);
1409 if (sdata == NULL)
0a1b45a2 1410 return false;
f592407e
AM
1411 sec->used_by_bfd = sdata;
1412 }
f0abc2a1
AM
1413
1414 return _bfd_elf_new_section_hook (abfd, sec);
1415}
b49e97c9
TS
1416\f
1417/* Read ECOFF debugging information from a .mdebug section into a
1418 ecoff_debug_info structure. */
1419
0a1b45a2 1420bool
9719ad41
RS
1421_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1422 struct ecoff_debug_info *debug)
b49e97c9
TS
1423{
1424 HDRR *symhdr;
1425 const struct ecoff_debug_swap *swap;
9719ad41 1426 char *ext_hdr;
b49e97c9
TS
1427
1428 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1429 memset (debug, 0, sizeof (*debug));
1430
9719ad41 1431 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1432 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1433 goto error_return;
1434
9719ad41 1435 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1436 swap->external_hdr_size))
b49e97c9
TS
1437 goto error_return;
1438
1439 symhdr = &debug->symbolic_header;
1440 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1441
1442 /* The symbolic header contains absolute file offsets and sizes to
1443 read. */
1444#define READ(ptr, offset, count, size, type) \
1f4361a7 1445 do \
b49e97c9 1446 { \
1f4361a7
AM
1447 size_t amt; \
1448 debug->ptr = NULL; \
1449 if (symhdr->count == 0) \
1450 break; \
1451 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
1452 { \
1453 bfd_set_error (bfd_error_file_too_big); \
1454 goto error_return; \
1455 } \
2bb3687b 1456 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
b49e97c9 1457 goto error_return; \
2bb3687b
AM
1458 debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \
1459 if (debug->ptr == NULL) \
b49e97c9 1460 goto error_return; \
1f4361a7 1461 } while (0)
b49e97c9
TS
1462
1463 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1464 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1465 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1466 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1467 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1468 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1469 union aux_ext *);
1470 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1471 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1472 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1473 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1474 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1475#undef READ
1476
1477 debug->fdr = NULL;
b49e97c9 1478
0a1b45a2 1479 return true;
b49e97c9
TS
1480
1481 error_return:
c9594989
AM
1482 free (ext_hdr);
1483 free (debug->line);
1484 free (debug->external_dnr);
1485 free (debug->external_pdr);
1486 free (debug->external_sym);
1487 free (debug->external_opt);
1488 free (debug->external_aux);
1489 free (debug->ss);
1490 free (debug->ssext);
1491 free (debug->external_fdr);
1492 free (debug->external_rfd);
1493 free (debug->external_ext);
0a1b45a2 1494 return false;
b49e97c9
TS
1495}
1496\f
1497/* Swap RPDR (runtime procedure table entry) for output. */
1498
1499static void
9719ad41 1500ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1501{
1502 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1503 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1504 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1505 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1506 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1507 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1508
1509 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1510 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1511
1512 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1513}
1514
1515/* Create a runtime procedure table from the .mdebug section. */
1516
0a1b45a2 1517static bool
9719ad41
RS
1518mips_elf_create_procedure_table (void *handle, bfd *abfd,
1519 struct bfd_link_info *info, asection *s,
1520 struct ecoff_debug_info *debug)
b49e97c9
TS
1521{
1522 const struct ecoff_debug_swap *swap;
1523 HDRR *hdr = &debug->symbolic_header;
1524 RPDR *rpdr, *rp;
1525 struct rpdr_ext *erp;
9719ad41 1526 void *rtproc;
b49e97c9
TS
1527 struct pdr_ext *epdr;
1528 struct sym_ext *esym;
1529 char *ss, **sv;
1530 char *str;
1531 bfd_size_type size;
1532 bfd_size_type count;
1533 unsigned long sindex;
1534 unsigned long i;
1535 PDR pdr;
1536 SYMR sym;
1537 const char *no_name_func = _("static procedure (no name)");
1538
1539 epdr = NULL;
1540 rpdr = NULL;
1541 esym = NULL;
1542 ss = NULL;
1543 sv = NULL;
1544
1545 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1546
1547 sindex = strlen (no_name_func) + 1;
1548 count = hdr->ipdMax;
1549 if (count > 0)
1550 {
1551 size = swap->external_pdr_size;
1552
9719ad41 1553 epdr = bfd_malloc (size * count);
b49e97c9
TS
1554 if (epdr == NULL)
1555 goto error_return;
1556
9719ad41 1557 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1558 goto error_return;
1559
1560 size = sizeof (RPDR);
9719ad41 1561 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1562 if (rpdr == NULL)
1563 goto error_return;
1564
1565 size = sizeof (char *);
9719ad41 1566 sv = bfd_malloc (size * count);
b49e97c9
TS
1567 if (sv == NULL)
1568 goto error_return;
1569
1570 count = hdr->isymMax;
1571 size = swap->external_sym_size;
9719ad41 1572 esym = bfd_malloc (size * count);
b49e97c9
TS
1573 if (esym == NULL)
1574 goto error_return;
1575
9719ad41 1576 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1577 goto error_return;
1578
1579 count = hdr->issMax;
9719ad41 1580 ss = bfd_malloc (count);
b49e97c9
TS
1581 if (ss == NULL)
1582 goto error_return;
f075ee0c 1583 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1584 goto error_return;
1585
1586 count = hdr->ipdMax;
1587 for (i = 0; i < (unsigned long) count; i++, rp++)
1588 {
9719ad41
RS
1589 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1590 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1591 rp->adr = sym.value;
1592 rp->regmask = pdr.regmask;
1593 rp->regoffset = pdr.regoffset;
1594 rp->fregmask = pdr.fregmask;
1595 rp->fregoffset = pdr.fregoffset;
1596 rp->frameoffset = pdr.frameoffset;
1597 rp->framereg = pdr.framereg;
1598 rp->pcreg = pdr.pcreg;
1599 rp->irpss = sindex;
1600 sv[i] = ss + sym.iss;
1601 sindex += strlen (sv[i]) + 1;
1602 }
1603 }
1604
1605 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1606 size = BFD_ALIGN (size, 16);
9719ad41 1607 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1608 if (rtproc == NULL)
1609 {
1610 mips_elf_hash_table (info)->procedure_count = 0;
1611 goto error_return;
1612 }
1613
1614 mips_elf_hash_table (info)->procedure_count = count + 2;
1615
9719ad41 1616 erp = rtproc;
b49e97c9
TS
1617 memset (erp, 0, sizeof (struct rpdr_ext));
1618 erp++;
1619 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1620 strcpy (str, no_name_func);
1621 str += strlen (no_name_func) + 1;
1622 for (i = 0; i < count; i++)
1623 {
1624 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1625 strcpy (str, sv[i]);
1626 str += strlen (sv[i]) + 1;
1627 }
1628 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1629
1630 /* Set the size and contents of .rtproc section. */
eea6121a 1631 s->size = size;
9719ad41 1632 s->contents = rtproc;
b49e97c9
TS
1633
1634 /* Skip this section later on (I don't think this currently
1635 matters, but someday it might). */
8423293d 1636 s->map_head.link_order = NULL;
b49e97c9 1637
c9594989
AM
1638 free (epdr);
1639 free (rpdr);
1640 free (esym);
1641 free (ss);
1642 free (sv);
0a1b45a2 1643 return true;
b49e97c9
TS
1644
1645 error_return:
c9594989
AM
1646 free (epdr);
1647 free (rpdr);
1648 free (esym);
1649 free (ss);
1650 free (sv);
0a1b45a2 1651 return false;
b49e97c9 1652}
738e5348 1653\f
861fb55a
DJ
1654/* We're going to create a stub for H. Create a symbol for the stub's
1655 value and size, to help make the disassembly easier to read. */
1656
0a1b45a2 1657static bool
861fb55a
DJ
1658mips_elf_create_stub_symbol (struct bfd_link_info *info,
1659 struct mips_elf_link_hash_entry *h,
1660 const char *prefix, asection *s, bfd_vma value,
1661 bfd_vma size)
1662{
0a1b45a2 1663 bool micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
861fb55a
DJ
1664 struct bfd_link_hash_entry *bh;
1665 struct elf_link_hash_entry *elfh;
e1fa0163 1666 char *name;
0a1b45a2 1667 bool res;
861fb55a 1668
a848a227 1669 if (micromips_p)
df58fc94
RS
1670 value |= 1;
1671
861fb55a 1672 /* Create a new symbol. */
e1fa0163 1673 name = concat (prefix, h->root.root.root.string, NULL);
861fb55a 1674 bh = NULL;
e1fa0163
NC
1675 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1676 BSF_LOCAL, s, value, NULL,
0a1b45a2 1677 true, false, &bh);
e1fa0163
NC
1678 free (name);
1679 if (! res)
0a1b45a2 1680 return false;
861fb55a
DJ
1681
1682 /* Make it a local function. */
1683 elfh = (struct elf_link_hash_entry *) bh;
1684 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1685 elfh->size = size;
1686 elfh->forced_local = 1;
a848a227
MR
1687 if (micromips_p)
1688 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
0a1b45a2 1689 return true;
861fb55a
DJ
1690}
1691
738e5348
RS
1692/* We're about to redefine H. Create a symbol to represent H's
1693 current value and size, to help make the disassembly easier
1694 to read. */
1695
0a1b45a2 1696static bool
738e5348
RS
1697mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1698 struct mips_elf_link_hash_entry *h,
1699 const char *prefix)
1700{
1701 struct bfd_link_hash_entry *bh;
1702 struct elf_link_hash_entry *elfh;
e1fa0163 1703 char *name;
738e5348
RS
1704 asection *s;
1705 bfd_vma value;
0a1b45a2 1706 bool res;
738e5348
RS
1707
1708 /* Read the symbol's value. */
1709 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1710 || h->root.root.type == bfd_link_hash_defweak);
1711 s = h->root.root.u.def.section;
1712 value = h->root.root.u.def.value;
1713
1714 /* Create a new symbol. */
e1fa0163 1715 name = concat (prefix, h->root.root.root.string, NULL);
738e5348 1716 bh = NULL;
e1fa0163
NC
1717 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1718 BSF_LOCAL, s, value, NULL,
0a1b45a2 1719 true, false, &bh);
e1fa0163
NC
1720 free (name);
1721 if (! res)
0a1b45a2 1722 return false;
738e5348
RS
1723
1724 /* Make it local and copy the other attributes from H. */
1725 elfh = (struct elf_link_hash_entry *) bh;
1726 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1727 elfh->other = h->root.other;
1728 elfh->size = h->root.size;
1729 elfh->forced_local = 1;
0a1b45a2 1730 return true;
738e5348
RS
1731}
1732
1733/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1734 function rather than to a hard-float stub. */
1735
0a1b45a2 1736static bool
738e5348
RS
1737section_allows_mips16_refs_p (asection *section)
1738{
1739 const char *name;
1740
fd361982 1741 name = bfd_section_name (section);
738e5348
RS
1742 return (FN_STUB_P (name)
1743 || CALL_STUB_P (name)
1744 || CALL_FP_STUB_P (name)
1745 || strcmp (name, ".pdr") == 0);
1746}
1747
1748/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1749 stub section of some kind. Return the R_SYMNDX of the target
1750 function, or 0 if we can't decide which function that is. */
1751
1752static unsigned long
cb4437b8
MR
1753mips16_stub_symndx (const struct elf_backend_data *bed,
1754 asection *sec ATTRIBUTE_UNUSED,
502e814e 1755 const Elf_Internal_Rela *relocs,
738e5348
RS
1756 const Elf_Internal_Rela *relend)
1757{
cb4437b8 1758 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
738e5348
RS
1759 const Elf_Internal_Rela *rel;
1760
cb4437b8
MR
1761 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1762 one in a compound relocation. */
1763 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
738e5348
RS
1764 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1765 return ELF_R_SYM (sec->owner, rel->r_info);
1766
1767 /* Otherwise trust the first relocation, whatever its kind. This is
1768 the traditional behavior. */
1769 if (relocs < relend)
1770 return ELF_R_SYM (sec->owner, relocs->r_info);
1771
1772 return 0;
1773}
b49e97c9
TS
1774
1775/* Check the mips16 stubs for a particular symbol, and see if we can
1776 discard them. */
1777
861fb55a
DJ
1778static void
1779mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1780 struct mips_elf_link_hash_entry *h)
b49e97c9 1781{
738e5348
RS
1782 /* Dynamic symbols must use the standard call interface, in case other
1783 objects try to call them. */
1784 if (h->fn_stub != NULL
1785 && h->root.dynindx != -1)
1786 {
1787 mips_elf_create_shadow_symbol (info, h, ".mips16.");
0a1b45a2 1788 h->need_fn_stub = true;
738e5348
RS
1789 }
1790
b49e97c9
TS
1791 if (h->fn_stub != NULL
1792 && ! h->need_fn_stub)
1793 {
1794 /* We don't need the fn_stub; the only references to this symbol
07d6d2b8
AM
1795 are 16 bit calls. Clobber the size to 0 to prevent it from
1796 being included in the link. */
eea6121a 1797 h->fn_stub->size = 0;
b49e97c9
TS
1798 h->fn_stub->flags &= ~SEC_RELOC;
1799 h->fn_stub->reloc_count = 0;
1800 h->fn_stub->flags |= SEC_EXCLUDE;
ca9584fb 1801 h->fn_stub->output_section = bfd_abs_section_ptr;
b49e97c9
TS
1802 }
1803
1804 if (h->call_stub != NULL
30c09090 1805 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1806 {
1807 /* We don't need the call_stub; this is a 16 bit function, so
07d6d2b8
AM
1808 calls from other 16 bit functions are OK. Clobber the size
1809 to 0 to prevent it from being included in the link. */
eea6121a 1810 h->call_stub->size = 0;
b49e97c9
TS
1811 h->call_stub->flags &= ~SEC_RELOC;
1812 h->call_stub->reloc_count = 0;
1813 h->call_stub->flags |= SEC_EXCLUDE;
ca9584fb 1814 h->call_stub->output_section = bfd_abs_section_ptr;
b49e97c9
TS
1815 }
1816
1817 if (h->call_fp_stub != NULL
30c09090 1818 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1819 {
1820 /* We don't need the call_stub; this is a 16 bit function, so
07d6d2b8
AM
1821 calls from other 16 bit functions are OK. Clobber the size
1822 to 0 to prevent it from being included in the link. */
eea6121a 1823 h->call_fp_stub->size = 0;
b49e97c9
TS
1824 h->call_fp_stub->flags &= ~SEC_RELOC;
1825 h->call_fp_stub->reloc_count = 0;
1826 h->call_fp_stub->flags |= SEC_EXCLUDE;
ca9584fb 1827 h->call_fp_stub->output_section = bfd_abs_section_ptr;
b49e97c9 1828 }
861fb55a
DJ
1829}
1830
1831/* Hashtable callbacks for mips_elf_la25_stubs. */
1832
1833static hashval_t
1834mips_elf_la25_stub_hash (const void *entry_)
1835{
1836 const struct mips_elf_la25_stub *entry;
1837
1838 entry = (struct mips_elf_la25_stub *) entry_;
1839 return entry->h->root.root.u.def.section->id
1840 + entry->h->root.root.u.def.value;
1841}
1842
1843static int
1844mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1845{
1846 const struct mips_elf_la25_stub *entry1, *entry2;
1847
1848 entry1 = (struct mips_elf_la25_stub *) entry1_;
1849 entry2 = (struct mips_elf_la25_stub *) entry2_;
1850 return ((entry1->h->root.root.u.def.section
1851 == entry2->h->root.root.u.def.section)
1852 && (entry1->h->root.root.u.def.value
1853 == entry2->h->root.root.u.def.value));
1854}
1855
1856/* Called by the linker to set up the la25 stub-creation code. FN is
1857 the linker's implementation of add_stub_function. Return true on
1858 success. */
1859
0a1b45a2 1860bool
861fb55a
DJ
1861_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1862 asection *(*fn) (const char *, asection *,
1863 asection *))
1864{
1865 struct mips_elf_link_hash_table *htab;
1866
1867 htab = mips_elf_hash_table (info);
4dfe6ac6 1868 if (htab == NULL)
0a1b45a2 1869 return false;
4dfe6ac6 1870
861fb55a
DJ
1871 htab->add_stub_section = fn;
1872 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1873 mips_elf_la25_stub_eq, NULL);
1874 if (htab->la25_stubs == NULL)
0a1b45a2 1875 return false;
861fb55a 1876
0a1b45a2 1877 return true;
861fb55a
DJ
1878}
1879
1880/* Return true if H is a locally-defined PIC function, in the sense
8f0c309a
CLT
1881 that it or its fn_stub might need $25 to be valid on entry.
1882 Note that MIPS16 functions set up $gp using PC-relative instructions,
1883 so they themselves never need $25 to be valid. Only non-MIPS16
1884 entry points are of interest here. */
861fb55a 1885
0a1b45a2 1886static bool
861fb55a
DJ
1887mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1888{
1889 return ((h->root.root.type == bfd_link_hash_defined
1890 || h->root.root.type == bfd_link_hash_defweak)
1891 && h->root.def_regular
1892 && !bfd_is_abs_section (h->root.root.u.def.section)
f02cb058 1893 && !bfd_is_und_section (h->root.root.u.def.section)
8f0c309a
CLT
1894 && (!ELF_ST_IS_MIPS16 (h->root.other)
1895 || (h->fn_stub && h->need_fn_stub))
861fb55a
DJ
1896 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1897 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1898}
1899
8f0c309a
CLT
1900/* Set *SEC to the input section that contains the target of STUB.
1901 Return the offset of the target from the start of that section. */
1902
1903static bfd_vma
1904mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1905 asection **sec)
1906{
1907 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1908 {
1909 BFD_ASSERT (stub->h->need_fn_stub);
1910 *sec = stub->h->fn_stub;
1911 return 0;
1912 }
1913 else
1914 {
1915 *sec = stub->h->root.root.u.def.section;
1916 return stub->h->root.root.u.def.value;
1917 }
1918}
1919
861fb55a
DJ
1920/* STUB describes an la25 stub that we have decided to implement
1921 by inserting an LUI/ADDIU pair before the target function.
1922 Create the section and redirect the function symbol to it. */
1923
0a1b45a2 1924static bool
861fb55a
DJ
1925mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1926 struct bfd_link_info *info)
1927{
1928 struct mips_elf_link_hash_table *htab;
1929 char *name;
1930 asection *s, *input_section;
1931 unsigned int align;
1932
1933 htab = mips_elf_hash_table (info);
4dfe6ac6 1934 if (htab == NULL)
0a1b45a2 1935 return false;
861fb55a
DJ
1936
1937 /* Create a unique name for the new section. */
1938 name = bfd_malloc (11 + sizeof (".text.stub."));
1939 if (name == NULL)
0a1b45a2 1940 return false;
861fb55a
DJ
1941 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1942
1943 /* Create the section. */
8f0c309a 1944 mips_elf_get_la25_target (stub, &input_section);
861fb55a
DJ
1945 s = htab->add_stub_section (name, input_section,
1946 input_section->output_section);
1947 if (s == NULL)
0a1b45a2 1948 return false;
861fb55a
DJ
1949
1950 /* Make sure that any padding goes before the stub. */
1951 align = input_section->alignment_power;
fd361982 1952 if (!bfd_set_section_alignment (s, align))
0a1b45a2 1953 return false;
861fb55a
DJ
1954 if (align > 3)
1955 s->size = (1 << align) - 8;
1956
1957 /* Create a symbol for the stub. */
1958 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1959 stub->stub_section = s;
1960 stub->offset = s->size;
1961
1962 /* Allocate room for it. */
1963 s->size += 8;
0a1b45a2 1964 return true;
861fb55a
DJ
1965}
1966
1967/* STUB describes an la25 stub that we have decided to implement
1968 with a separate trampoline. Allocate room for it and redirect
1969 the function symbol to it. */
1970
0a1b45a2 1971static bool
861fb55a
DJ
1972mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1973 struct bfd_link_info *info)
1974{
1975 struct mips_elf_link_hash_table *htab;
1976 asection *s;
1977
1978 htab = mips_elf_hash_table (info);
4dfe6ac6 1979 if (htab == NULL)
0a1b45a2 1980 return false;
861fb55a
DJ
1981
1982 /* Create a trampoline section, if we haven't already. */
1983 s = htab->strampoline;
1984 if (s == NULL)
1985 {
1986 asection *input_section = stub->h->root.root.u.def.section;
1987 s = htab->add_stub_section (".text", NULL,
1988 input_section->output_section);
fd361982 1989 if (s == NULL || !bfd_set_section_alignment (s, 4))
0a1b45a2 1990 return false;
861fb55a
DJ
1991 htab->strampoline = s;
1992 }
1993
1994 /* Create a symbol for the stub. */
1995 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1996 stub->stub_section = s;
1997 stub->offset = s->size;
1998
1999 /* Allocate room for it. */
2000 s->size += 16;
0a1b45a2 2001 return true;
861fb55a
DJ
2002}
2003
2004/* H describes a symbol that needs an la25 stub. Make sure that an
2005 appropriate stub exists and point H at it. */
2006
0a1b45a2 2007static bool
861fb55a
DJ
2008mips_elf_add_la25_stub (struct bfd_link_info *info,
2009 struct mips_elf_link_hash_entry *h)
2010{
2011 struct mips_elf_link_hash_table *htab;
2012 struct mips_elf_la25_stub search, *stub;
0a1b45a2 2013 bool use_trampoline_p;
861fb55a
DJ
2014 asection *s;
2015 bfd_vma value;
2016 void **slot;
2017
861fb55a
DJ
2018 /* Describe the stub we want. */
2019 search.stub_section = NULL;
2020 search.offset = 0;
2021 search.h = h;
2022
2023 /* See if we've already created an equivalent stub. */
2024 htab = mips_elf_hash_table (info);
4dfe6ac6 2025 if (htab == NULL)
0a1b45a2 2026 return false;
4dfe6ac6 2027
861fb55a
DJ
2028 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
2029 if (slot == NULL)
0a1b45a2 2030 return false;
861fb55a
DJ
2031
2032 stub = (struct mips_elf_la25_stub *) *slot;
2033 if (stub != NULL)
2034 {
2035 /* We can reuse the existing stub. */
2036 h->la25_stub = stub;
0a1b45a2 2037 return true;
861fb55a
DJ
2038 }
2039
2040 /* Create a permanent copy of ENTRY and add it to the hash table. */
2041 stub = bfd_malloc (sizeof (search));
2042 if (stub == NULL)
0a1b45a2 2043 return false;
861fb55a
DJ
2044 *stub = search;
2045 *slot = stub;
2046
8f0c309a
CLT
2047 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2048 of the section and if we would need no more than 2 nops. */
2049 value = mips_elf_get_la25_target (stub, &s);
fe152e64
MR
2050 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2051 value &= ~1;
8f0c309a
CLT
2052 use_trampoline_p = (value != 0 || s->alignment_power > 4);
2053
861fb55a
DJ
2054 h->la25_stub = stub;
2055 return (use_trampoline_p
2056 ? mips_elf_add_la25_trampoline (stub, info)
2057 : mips_elf_add_la25_intro (stub, info));
2058}
2059
2060/* A mips_elf_link_hash_traverse callback that is called before sizing
2061 sections. DATA points to a mips_htab_traverse_info structure. */
2062
0a1b45a2 2063static bool
861fb55a
DJ
2064mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2065{
2066 struct mips_htab_traverse_info *hti;
2067
2068 hti = (struct mips_htab_traverse_info *) data;
0e1862bb 2069 if (!bfd_link_relocatable (hti->info))
861fb55a 2070 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 2071
861fb55a
DJ
2072 if (mips_elf_local_pic_function_p (h))
2073 {
ba85c43e
NC
2074 /* PR 12845: If H is in a section that has been garbage
2075 collected it will have its output section set to *ABS*. */
2076 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
0a1b45a2 2077 return true;
ba85c43e 2078
861fb55a
DJ
2079 /* H is a function that might need $25 to be valid on entry.
2080 If we're creating a non-PIC relocatable object, mark H as
2081 being PIC. If we're creating a non-relocatable object with
2082 non-PIC branches and jumps to H, make sure that H has an la25
2083 stub. */
0e1862bb 2084 if (bfd_link_relocatable (hti->info))
861fb55a
DJ
2085 {
2086 if (!PIC_OBJECT_P (hti->output_bfd))
2087 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2088 }
2089 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2090 {
0a1b45a2
AM
2091 hti->error = true;
2092 return false;
861fb55a
DJ
2093 }
2094 }
0a1b45a2 2095 return true;
b49e97c9
TS
2096}
2097\f
d6f16593
MR
2098/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2099 Most mips16 instructions are 16 bits, but these instructions
2100 are 32 bits.
2101
2102 The format of these instructions is:
2103
2104 +--------------+--------------------------------+
2105 | JALX | X| Imm 20:16 | Imm 25:21 |
2106 +--------------+--------------------------------+
07d6d2b8 2107 | Immediate 15:0 |
d6f16593
MR
2108 +-----------------------------------------------+
2109
2110 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2111 Note that the immediate value in the first word is swapped.
2112
2113 When producing a relocatable object file, R_MIPS16_26 is
2114 handled mostly like R_MIPS_26. In particular, the addend is
2115 stored as a straight 26-bit value in a 32-bit instruction.
2116 (gas makes life simpler for itself by never adjusting a
2117 R_MIPS16_26 reloc to be against a section, so the addend is
2118 always zero). However, the 32 bit instruction is stored as 2
2119 16-bit values, rather than a single 32-bit value. In a
2120 big-endian file, the result is the same; in a little-endian
2121 file, the two 16-bit halves of the 32 bit value are swapped.
2122 This is so that a disassembler can recognize the jal
2123 instruction.
2124
2125 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2126 instruction stored as two 16-bit values. The addend A is the
2127 contents of the targ26 field. The calculation is the same as
2128 R_MIPS_26. When storing the calculated value, reorder the
2129 immediate value as shown above, and don't forget to store the
2130 value as two 16-bit values.
2131
2132 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2133 defined as
2134
2135 big-endian:
2136 +--------+----------------------+
07d6d2b8
AM
2137 | | |
2138 | | targ26-16 |
2139 |31 26|25 0|
d6f16593
MR
2140 +--------+----------------------+
2141
2142 little-endian:
2143 +----------+------+-------------+
07d6d2b8
AM
2144 | | | |
2145 | sub1 | | sub2 |
2146 |0 9|10 15|16 31|
d6f16593
MR
2147 +----------+--------------------+
2148 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2149 ((sub1 << 16) | sub2)).
2150
2151 When producing a relocatable object file, the calculation is
2152 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2153 When producing a fully linked file, the calculation is
2154 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2155 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2156
738e5348
RS
2157 The table below lists the other MIPS16 instruction relocations.
2158 Each one is calculated in the same way as the non-MIPS16 relocation
2159 given on the right, but using the extended MIPS16 layout of 16-bit
2160 immediate fields:
2161
2162 R_MIPS16_GPREL R_MIPS_GPREL16
2163 R_MIPS16_GOT16 R_MIPS_GOT16
2164 R_MIPS16_CALL16 R_MIPS_CALL16
2165 R_MIPS16_HI16 R_MIPS_HI16
2166 R_MIPS16_LO16 R_MIPS_LO16
2167
2168 A typical instruction will have a format like this:
d6f16593
MR
2169
2170 +--------------+--------------------------------+
2171 | EXTEND | Imm 10:5 | Imm 15:11 |
2172 +--------------+--------------------------------+
2173 | Major | rx | ry | Imm 4:0 |
2174 +--------------+--------------------------------+
2175
2176 EXTEND is the five bit value 11110. Major is the instruction
2177 opcode.
2178
738e5348
RS
2179 All we need to do here is shuffle the bits appropriately.
2180 As above, the two 16-bit halves must be swapped on a
c9775dde
MR
2181 little-endian system.
2182
2183 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2184 relocatable field is shifted by 1 rather than 2 and the same bit
2185 shuffling is done as with the relocations above. */
738e5348 2186
0a1b45a2 2187static inline bool
738e5348
RS
2188mips16_reloc_p (int r_type)
2189{
2190 switch (r_type)
2191 {
2192 case R_MIPS16_26:
2193 case R_MIPS16_GPREL:
2194 case R_MIPS16_GOT16:
2195 case R_MIPS16_CALL16:
2196 case R_MIPS16_HI16:
2197 case R_MIPS16_LO16:
d0f13682
CLT
2198 case R_MIPS16_TLS_GD:
2199 case R_MIPS16_TLS_LDM:
2200 case R_MIPS16_TLS_DTPREL_HI16:
2201 case R_MIPS16_TLS_DTPREL_LO16:
2202 case R_MIPS16_TLS_GOTTPREL:
2203 case R_MIPS16_TLS_TPREL_HI16:
2204 case R_MIPS16_TLS_TPREL_LO16:
c9775dde 2205 case R_MIPS16_PC16_S1:
0a1b45a2 2206 return true;
738e5348
RS
2207
2208 default:
0a1b45a2 2209 return false;
738e5348
RS
2210 }
2211}
2212
df58fc94
RS
2213/* Check if a microMIPS reloc. */
2214
0a1b45a2 2215static inline bool
df58fc94
RS
2216micromips_reloc_p (unsigned int r_type)
2217{
2218 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2219}
2220
2221/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2222 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2223 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2224
0a1b45a2 2225static inline bool
df58fc94
RS
2226micromips_reloc_shuffle_p (unsigned int r_type)
2227{
2228 return (micromips_reloc_p (r_type)
2229 && r_type != R_MICROMIPS_PC7_S1
2230 && r_type != R_MICROMIPS_PC10_S1);
2231}
2232
0a1b45a2 2233static inline bool
738e5348
RS
2234got16_reloc_p (int r_type)
2235{
df58fc94
RS
2236 return (r_type == R_MIPS_GOT16
2237 || r_type == R_MIPS16_GOT16
2238 || r_type == R_MICROMIPS_GOT16);
738e5348
RS
2239}
2240
0a1b45a2 2241static inline bool
738e5348
RS
2242call16_reloc_p (int r_type)
2243{
df58fc94
RS
2244 return (r_type == R_MIPS_CALL16
2245 || r_type == R_MIPS16_CALL16
2246 || r_type == R_MICROMIPS_CALL16);
2247}
2248
0a1b45a2 2249static inline bool
df58fc94
RS
2250got_disp_reloc_p (unsigned int r_type)
2251{
2252 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2253}
2254
0a1b45a2 2255static inline bool
df58fc94
RS
2256got_page_reloc_p (unsigned int r_type)
2257{
2258 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2259}
2260
0a1b45a2 2261static inline bool
df58fc94
RS
2262got_lo16_reloc_p (unsigned int r_type)
2263{
2264 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2265}
2266
0a1b45a2 2267static inline bool
df58fc94
RS
2268call_hi16_reloc_p (unsigned int r_type)
2269{
2270 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2271}
2272
0a1b45a2 2273static inline bool
df58fc94
RS
2274call_lo16_reloc_p (unsigned int r_type)
2275{
2276 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
738e5348
RS
2277}
2278
0a1b45a2 2279static inline bool
738e5348
RS
2280hi16_reloc_p (int r_type)
2281{
df58fc94
RS
2282 return (r_type == R_MIPS_HI16
2283 || r_type == R_MIPS16_HI16
7361da2c
AB
2284 || r_type == R_MICROMIPS_HI16
2285 || r_type == R_MIPS_PCHI16);
738e5348 2286}
d6f16593 2287
0a1b45a2 2288static inline bool
738e5348
RS
2289lo16_reloc_p (int r_type)
2290{
df58fc94
RS
2291 return (r_type == R_MIPS_LO16
2292 || r_type == R_MIPS16_LO16
7361da2c
AB
2293 || r_type == R_MICROMIPS_LO16
2294 || r_type == R_MIPS_PCLO16);
738e5348
RS
2295}
2296
0a1b45a2 2297static inline bool
738e5348
RS
2298mips16_call_reloc_p (int r_type)
2299{
2300 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2301}
d6f16593 2302
0a1b45a2 2303static inline bool
38a7df63
CF
2304jal_reloc_p (int r_type)
2305{
df58fc94
RS
2306 return (r_type == R_MIPS_26
2307 || r_type == R_MIPS16_26
2308 || r_type == R_MICROMIPS_26_S1);
2309}
2310
0a1b45a2 2311static inline bool
99aefae6
MR
2312b_reloc_p (int r_type)
2313{
2314 return (r_type == R_MIPS_PC26_S2
2315 || r_type == R_MIPS_PC21_S2
2316 || r_type == R_MIPS_PC16
c9775dde 2317 || r_type == R_MIPS_GNU_REL16_S2
9d862524
MR
2318 || r_type == R_MIPS16_PC16_S1
2319 || r_type == R_MICROMIPS_PC16_S1
2320 || r_type == R_MICROMIPS_PC10_S1
2321 || r_type == R_MICROMIPS_PC7_S1);
99aefae6
MR
2322}
2323
0a1b45a2 2324static inline bool
7361da2c
AB
2325aligned_pcrel_reloc_p (int r_type)
2326{
2327 return (r_type == R_MIPS_PC18_S3
2328 || r_type == R_MIPS_PC19_S2);
2329}
2330
0a1b45a2 2331static inline bool
9d862524
MR
2332branch_reloc_p (int r_type)
2333{
2334 return (r_type == R_MIPS_26
2335 || r_type == R_MIPS_PC26_S2
2336 || r_type == R_MIPS_PC21_S2
2337 || r_type == R_MIPS_PC16
2338 || r_type == R_MIPS_GNU_REL16_S2);
2339}
2340
0a1b45a2 2341static inline bool
c9775dde
MR
2342mips16_branch_reloc_p (int r_type)
2343{
2344 return (r_type == R_MIPS16_26
2345 || r_type == R_MIPS16_PC16_S1);
2346}
2347
0a1b45a2 2348static inline bool
df58fc94
RS
2349micromips_branch_reloc_p (int r_type)
2350{
2351 return (r_type == R_MICROMIPS_26_S1
2352 || r_type == R_MICROMIPS_PC16_S1
2353 || r_type == R_MICROMIPS_PC10_S1
2354 || r_type == R_MICROMIPS_PC7_S1);
2355}
2356
0a1b45a2 2357static inline bool
df58fc94
RS
2358tls_gd_reloc_p (unsigned int r_type)
2359{
d0f13682
CLT
2360 return (r_type == R_MIPS_TLS_GD
2361 || r_type == R_MIPS16_TLS_GD
2362 || r_type == R_MICROMIPS_TLS_GD);
df58fc94
RS
2363}
2364
0a1b45a2 2365static inline bool
df58fc94
RS
2366tls_ldm_reloc_p (unsigned int r_type)
2367{
d0f13682
CLT
2368 return (r_type == R_MIPS_TLS_LDM
2369 || r_type == R_MIPS16_TLS_LDM
2370 || r_type == R_MICROMIPS_TLS_LDM);
df58fc94
RS
2371}
2372
0a1b45a2 2373static inline bool
df58fc94
RS
2374tls_gottprel_reloc_p (unsigned int r_type)
2375{
d0f13682
CLT
2376 return (r_type == R_MIPS_TLS_GOTTPREL
2377 || r_type == R_MIPS16_TLS_GOTTPREL
2378 || r_type == R_MICROMIPS_TLS_GOTTPREL);
38a7df63
CF
2379}
2380
73425813
AM
2381static inline bool
2382needs_shuffle (int r_type)
2383{
2384 return mips16_reloc_p (r_type) || micromips_reloc_shuffle_p (r_type);
2385}
2386
d6f16593 2387void
df58fc94 2388_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
0a1b45a2 2389 bool jal_shuffle, bfd_byte *data)
d6f16593 2390{
df58fc94 2391 bfd_vma first, second, val;
d6f16593 2392
73425813 2393 if (!needs_shuffle (r_type))
d6f16593
MR
2394 return;
2395
df58fc94
RS
2396 /* Pick up the first and second halfwords of the instruction. */
2397 first = bfd_get_16 (abfd, data);
2398 second = bfd_get_16 (abfd, data + 2);
2399 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2400 val = first << 16 | second;
2401 else if (r_type != R_MIPS16_26)
2402 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2403 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
d6f16593 2404 else
df58fc94
RS
2405 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2406 | ((first & 0x1f) << 21) | second);
d6f16593
MR
2407 bfd_put_32 (abfd, val, data);
2408}
2409
2410void
df58fc94 2411_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
0a1b45a2 2412 bool jal_shuffle, bfd_byte *data)
d6f16593 2413{
df58fc94 2414 bfd_vma first, second, val;
d6f16593 2415
73425813 2416 if (!needs_shuffle (r_type))
d6f16593
MR
2417 return;
2418
2419 val = bfd_get_32 (abfd, data);
df58fc94 2420 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
d6f16593 2421 {
df58fc94
RS
2422 second = val & 0xffff;
2423 first = val >> 16;
2424 }
2425 else if (r_type != R_MIPS16_26)
2426 {
2427 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2428 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
d6f16593
MR
2429 }
2430 else
2431 {
df58fc94
RS
2432 second = val & 0xffff;
2433 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2434 | ((val >> 21) & 0x1f);
d6f16593 2435 }
df58fc94
RS
2436 bfd_put_16 (abfd, second, data + 2);
2437 bfd_put_16 (abfd, first, data);
d6f16593
MR
2438}
2439
73425813
AM
2440/* Perform reloc offset checking.
2441 We can only use bfd_reloc_offset_in_range, which takes into account
2442 the size of the field being relocated, when section contents will
2443 be accessed because mips object files may use relocations that seem
2444 to access beyond section limits.
2445 gas/testsuite/gas/mips/dla-reloc.s is an example that puts
2446 R_MIPS_SUB, a 64-bit relocation, on the last instruction in the
2447 section. The R_MIPS_SUB applies to the addend for the next reloc
2448 rather than the section contents.
2449
2450 CHECK is CHECK_STD for the standard bfd_reloc_offset_in_range check,
2451 CHECK_INPLACE to only check partial_inplace relocs, and
2452 CHECK_SHUFFLE to only check relocs that shuffle/unshuffle. */
2453
2454bool
2455_bfd_mips_reloc_offset_in_range (bfd *abfd, asection *input_section,
2456 arelent *reloc_entry, enum reloc_check check)
2457{
2458 if (check == check_inplace && !reloc_entry->howto->partial_inplace)
2459 return true;
2460 if (check == check_shuffle && !needs_shuffle (reloc_entry->howto->type))
2461 return true;
2462 return bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
2463 input_section, reloc_entry->address);
2464}
2465
b49e97c9 2466bfd_reloc_status_type
9719ad41
RS
2467_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2468 arelent *reloc_entry, asection *input_section,
0a1b45a2 2469 bool relocatable, void *data, bfd_vma gp)
b49e97c9
TS
2470{
2471 bfd_vma relocation;
a7ebbfdf 2472 bfd_signed_vma val;
30ac9238 2473 bfd_reloc_status_type status;
b49e97c9
TS
2474
2475 if (bfd_is_com_section (symbol->section))
2476 relocation = 0;
2477 else
2478 relocation = symbol->value;
2479
2480 relocation += symbol->section->output_section->vma;
2481 relocation += symbol->section->output_offset;
2482
b49e97c9 2483 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
2484 val = reloc_entry->addend;
2485
30ac9238 2486 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 2487
b49e97c9 2488 /* Adjust val for the final section location and GP value. If we
1049f94e 2489 are producing relocatable output, we don't want to do this for
b49e97c9 2490 an external symbol. */
1049f94e 2491 if (! relocatable
b49e97c9
TS
2492 || (symbol->flags & BSF_SECTION_SYM) != 0)
2493 val += relocation - gp;
2494
a7ebbfdf
TS
2495 if (reloc_entry->howto->partial_inplace)
2496 {
73425813
AM
2497 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2498 reloc_entry->address))
2499 return bfd_reloc_outofrange;
2500
30ac9238
RS
2501 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2502 (bfd_byte *) data
2503 + reloc_entry->address);
2504 if (status != bfd_reloc_ok)
2505 return status;
a7ebbfdf
TS
2506 }
2507 else
2508 reloc_entry->addend = val;
b49e97c9 2509
1049f94e 2510 if (relocatable)
b49e97c9 2511 reloc_entry->address += input_section->output_offset;
30ac9238
RS
2512
2513 return bfd_reloc_ok;
2514}
2515
30ac9238
RS
2516/* A howto special_function for REL *HI16 relocations. We can only
2517 calculate the correct value once we've seen the partnering
2518 *LO16 relocation, so just save the information for later.
2519
2520 The ABI requires that the *LO16 immediately follow the *HI16.
2521 However, as a GNU extension, we permit an arbitrary number of
2522 *HI16s to be associated with a single *LO16. This significantly
2523 simplies the relocation handling in gcc. */
2524
2525bfd_reloc_status_type
c799eddb 2526_bfd_mips_elf_hi16_reloc (bfd *abfd, arelent *reloc_entry,
30ac9238
RS
2527 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2528 asection *input_section, bfd *output_bfd,
2529 char **error_message ATTRIBUTE_UNUSED)
2530{
2531 struct mips_hi16 *n;
c799eddb 2532 struct mips_elf_obj_tdata *tdata;
30ac9238 2533
07515404 2534 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2535 return bfd_reloc_outofrange;
2536
2537 n = bfd_malloc (sizeof *n);
2538 if (n == NULL)
2539 return bfd_reloc_outofrange;
2540
c799eddb
AM
2541 tdata = mips_elf_tdata (abfd);
2542 n->next = tdata->mips_hi16_list;
30ac9238
RS
2543 n->data = data;
2544 n->input_section = input_section;
2545 n->rel = *reloc_entry;
c799eddb 2546 tdata->mips_hi16_list = n;
30ac9238
RS
2547
2548 if (output_bfd != NULL)
2549 reloc_entry->address += input_section->output_offset;
2550
2551 return bfd_reloc_ok;
2552}
2553
738e5348 2554/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2555 like any other 16-bit relocation when applied to global symbols, but is
2556 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2557
2558bfd_reloc_status_type
2559_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2560 void *data, asection *input_section,
2561 bfd *output_bfd, char **error_message)
2562{
2563 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
e6f7f6d1
AM
2564 || bfd_is_und_section (bfd_asymbol_section (symbol))
2565 || bfd_is_com_section (bfd_asymbol_section (symbol)))
30ac9238
RS
2566 /* The relocation is against a global symbol. */
2567 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2568 input_section, output_bfd,
2569 error_message);
2570
2571 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2572 input_section, output_bfd, error_message);
2573}
2574
2575/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2576 is a straightforward 16 bit inplace relocation, but we must deal with
2577 any partnering high-part relocations as well. */
2578
2579bfd_reloc_status_type
2580_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2581 void *data, asection *input_section,
2582 bfd *output_bfd, char **error_message)
2583{
2584 bfd_vma vallo;
d6f16593 2585 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
c799eddb 2586 struct mips_elf_obj_tdata *tdata;
30ac9238 2587
73425813
AM
2588 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
2589 reloc_entry->address))
30ac9238
RS
2590 return bfd_reloc_outofrange;
2591
0a1b45a2 2592 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
d6f16593 2593 location);
df58fc94 2594 vallo = bfd_get_32 (abfd, location);
0a1b45a2 2595 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
df58fc94 2596 location);
d6f16593 2597
c799eddb
AM
2598 tdata = mips_elf_tdata (abfd);
2599 while (tdata->mips_hi16_list != NULL)
30ac9238
RS
2600 {
2601 bfd_reloc_status_type ret;
2602 struct mips_hi16 *hi;
2603
c799eddb 2604 hi = tdata->mips_hi16_list;
30ac9238 2605
738e5348
RS
2606 /* R_MIPS*_GOT16 relocations are something of a special case. We
2607 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2608 relocation (with a rightshift of 16). However, since GOT16
2609 relocations can also be used with global symbols, their howto
2610 has a rightshift of 0. */
2611 if (hi->rel.howto->type == R_MIPS_GOT16)
0a1b45a2 2612 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, false);
738e5348 2613 else if (hi->rel.howto->type == R_MIPS16_GOT16)
0a1b45a2 2614 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, false);
df58fc94 2615 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
0a1b45a2 2616 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, false);
30ac9238
RS
2617
2618 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2619 carry or borrow will induce a change of +1 or -1 in the high part. */
2620 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2621
30ac9238
RS
2622 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2623 hi->input_section, output_bfd,
2624 error_message);
2625 if (ret != bfd_reloc_ok)
2626 return ret;
2627
c799eddb 2628 tdata->mips_hi16_list = hi->next;
30ac9238
RS
2629 free (hi);
2630 }
2631
2632 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2633 input_section, output_bfd,
2634 error_message);
2635}
2636
2637/* A generic howto special_function. This calculates and installs the
2638 relocation itself, thus avoiding the oft-discussed problems in
2639 bfd_perform_relocation and bfd_install_relocation. */
2640
2641bfd_reloc_status_type
2642_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2643 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2644 asection *input_section, bfd *output_bfd,
2645 char **error_message ATTRIBUTE_UNUSED)
2646{
2647 bfd_signed_vma val;
2648 bfd_reloc_status_type status;
0a1b45a2 2649 bool relocatable;
30ac9238
RS
2650
2651 relocatable = (output_bfd != NULL);
2652
73425813
AM
2653 if (!_bfd_mips_reloc_offset_in_range (abfd, input_section, reloc_entry,
2654 (relocatable
2655 ? check_inplace : check_std)))
30ac9238
RS
2656 return bfd_reloc_outofrange;
2657
2658 /* Build up the field adjustment in VAL. */
2659 val = 0;
2660 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2661 {
2662 /* Either we're calculating the final field value or we have a
2663 relocation against a section symbol. Add in the section's
2664 offset or address. */
2665 val += symbol->section->output_section->vma;
2666 val += symbol->section->output_offset;
2667 }
2668
2669 if (!relocatable)
2670 {
2671 /* We're calculating the final field value. Add in the symbol's value
2672 and, if pc-relative, subtract the address of the field itself. */
2673 val += symbol->value;
2674 if (reloc_entry->howto->pc_relative)
2675 {
2676 val -= input_section->output_section->vma;
2677 val -= input_section->output_offset;
2678 val -= reloc_entry->address;
2679 }
2680 }
2681
2682 /* VAL is now the final adjustment. If we're keeping this relocation
2683 in the output file, and if the relocation uses a separate addend,
2684 we just need to add VAL to that addend. Otherwise we need to add
2685 VAL to the relocation field itself. */
2686 if (relocatable && !reloc_entry->howto->partial_inplace)
2687 reloc_entry->addend += val;
2688 else
2689 {
d6f16593
MR
2690 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2691
30ac9238
RS
2692 /* Add in the separate addend, if any. */
2693 val += reloc_entry->addend;
2694
2695 /* Add VAL to the relocation field. */
0a1b45a2 2696 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, false,
df58fc94 2697 location);
30ac9238 2698 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593 2699 location);
0a1b45a2 2700 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, false,
df58fc94 2701 location);
d6f16593 2702
30ac9238
RS
2703 if (status != bfd_reloc_ok)
2704 return status;
2705 }
2706
2707 if (relocatable)
2708 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2709
2710 return bfd_reloc_ok;
2711}
2712\f
2713/* Swap an entry in a .gptab section. Note that these routines rely
2714 on the equivalence of the two elements of the union. */
2715
2716static void
9719ad41
RS
2717bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2718 Elf32_gptab *in)
b49e97c9
TS
2719{
2720 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2721 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2722}
2723
2724static void
9719ad41
RS
2725bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2726 Elf32_External_gptab *ex)
b49e97c9
TS
2727{
2728 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2729 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2730}
2731
2732static void
9719ad41
RS
2733bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2734 Elf32_External_compact_rel *ex)
b49e97c9
TS
2735{
2736 H_PUT_32 (abfd, in->id1, ex->id1);
2737 H_PUT_32 (abfd, in->num, ex->num);
2738 H_PUT_32 (abfd, in->id2, ex->id2);
2739 H_PUT_32 (abfd, in->offset, ex->offset);
2740 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2741 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2742}
2743
2744static void
9719ad41
RS
2745bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2746 Elf32_External_crinfo *ex)
b49e97c9
TS
2747{
2748 unsigned long l;
2749
2750 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2751 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2752 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2753 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2754 H_PUT_32 (abfd, l, ex->info);
2755 H_PUT_32 (abfd, in->konst, ex->konst);
2756 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2757}
b49e97c9
TS
2758\f
2759/* A .reginfo section holds a single Elf32_RegInfo structure. These
2760 routines swap this structure in and out. They are used outside of
2761 BFD, so they are globally visible. */
2762
2763void
9719ad41
RS
2764bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2765 Elf32_RegInfo *in)
b49e97c9
TS
2766{
2767 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2768 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2769 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2770 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2771 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2772 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2773}
2774
2775void
9719ad41
RS
2776bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2777 Elf32_External_RegInfo *ex)
b49e97c9
TS
2778{
2779 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2780 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2781 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2782 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2783 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2784 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2785}
2786
2787/* In the 64 bit ABI, the .MIPS.options section holds register
2788 information in an Elf64_Reginfo structure. These routines swap
2789 them in and out. They are globally visible because they are used
2790 outside of BFD. These routines are here so that gas can call them
2791 without worrying about whether the 64 bit ABI has been included. */
2792
2793void
9719ad41
RS
2794bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2795 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2796{
2797 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2798 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2799 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2800 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2801 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2802 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2803 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2804}
2805
2806void
9719ad41
RS
2807bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2808 Elf64_External_RegInfo *ex)
b49e97c9
TS
2809{
2810 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2811 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2812 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2813 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2814 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2815 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2816 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2817}
2818
2819/* Swap in an options header. */
2820
2821void
9719ad41
RS
2822bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2823 Elf_Internal_Options *in)
b49e97c9
TS
2824{
2825 in->kind = H_GET_8 (abfd, ex->kind);
2826 in->size = H_GET_8 (abfd, ex->size);
2827 in->section = H_GET_16 (abfd, ex->section);
2828 in->info = H_GET_32 (abfd, ex->info);
2829}
2830
2831/* Swap out an options header. */
2832
2833void
9719ad41
RS
2834bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2835 Elf_External_Options *ex)
b49e97c9
TS
2836{
2837 H_PUT_8 (abfd, in->kind, ex->kind);
2838 H_PUT_8 (abfd, in->size, ex->size);
2839 H_PUT_16 (abfd, in->section, ex->section);
2840 H_PUT_32 (abfd, in->info, ex->info);
2841}
351cdf24
MF
2842
2843/* Swap in an abiflags structure. */
2844
2845void
2846bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2847 const Elf_External_ABIFlags_v0 *ex,
2848 Elf_Internal_ABIFlags_v0 *in)
2849{
2850 in->version = H_GET_16 (abfd, ex->version);
2851 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2852 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2853 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2854 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2855 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2856 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2857 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2858 in->ases = H_GET_32 (abfd, ex->ases);
2859 in->flags1 = H_GET_32 (abfd, ex->flags1);
2860 in->flags2 = H_GET_32 (abfd, ex->flags2);
2861}
2862
2863/* Swap out an abiflags structure. */
2864
2865void
2866bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2867 const Elf_Internal_ABIFlags_v0 *in,
2868 Elf_External_ABIFlags_v0 *ex)
2869{
2870 H_PUT_16 (abfd, in->version, ex->version);
2871 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2872 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2873 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2874 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2875 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2876 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2877 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2878 H_PUT_32 (abfd, in->ases, ex->ases);
2879 H_PUT_32 (abfd, in->flags1, ex->flags1);
2880 H_PUT_32 (abfd, in->flags2, ex->flags2);
2881}
b49e97c9
TS
2882\f
2883/* This function is called via qsort() to sort the dynamic relocation
2884 entries by increasing r_symndx value. */
2885
2886static int
9719ad41 2887sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2888{
947216bf
AM
2889 Elf_Internal_Rela int_reloc1;
2890 Elf_Internal_Rela int_reloc2;
6870500c 2891 int diff;
b49e97c9 2892
947216bf
AM
2893 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2894 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2895
6870500c
RS
2896 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2897 if (diff != 0)
2898 return diff;
2899
2900 if (int_reloc1.r_offset < int_reloc2.r_offset)
2901 return -1;
2902 if (int_reloc1.r_offset > int_reloc2.r_offset)
2903 return 1;
2904 return 0;
b49e97c9
TS
2905}
2906
f4416af6
AO
2907/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2908
2909static int
7e3102a7
AM
2910sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2911 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2912{
7e3102a7 2913#ifdef BFD64
f4416af6
AO
2914 Elf_Internal_Rela int_reloc1[3];
2915 Elf_Internal_Rela int_reloc2[3];
2916
2917 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2918 (reldyn_sorting_bfd, arg1, int_reloc1);
2919 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2920 (reldyn_sorting_bfd, arg2, int_reloc2);
2921
6870500c
RS
2922 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2923 return -1;
2924 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2925 return 1;
2926
2927 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2928 return -1;
2929 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2930 return 1;
2931 return 0;
7e3102a7
AM
2932#else
2933 abort ();
2934#endif
f4416af6
AO
2935}
2936
2937
b49e97c9
TS
2938/* This routine is used to write out ECOFF debugging external symbol
2939 information. It is called via mips_elf_link_hash_traverse. The
2940 ECOFF external symbol information must match the ELF external
2941 symbol information. Unfortunately, at this point we don't know
2942 whether a symbol is required by reloc information, so the two
2943 tables may wind up being different. We must sort out the external
2944 symbol information before we can set the final size of the .mdebug
2945 section, and we must set the size of the .mdebug section before we
2946 can relocate any sections, and we can't know which symbols are
2947 required by relocation until we relocate the sections.
2948 Fortunately, it is relatively unlikely that any symbol will be
2949 stripped but required by a reloc. In particular, it can not happen
2950 when generating a final executable. */
2951
0a1b45a2 2952static bool
9719ad41 2953mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2954{
9719ad41 2955 struct extsym_info *einfo = data;
0a1b45a2 2956 bool strip;
b49e97c9
TS
2957 asection *sec, *output_section;
2958
b49e97c9 2959 if (h->root.indx == -2)
0a1b45a2 2960 strip = false;
f5385ebf 2961 else if ((h->root.def_dynamic
77cfaee6
AM
2962 || h->root.ref_dynamic
2963 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2964 && !h->root.def_regular
2965 && !h->root.ref_regular)
0a1b45a2 2966 strip = true;
b49e97c9
TS
2967 else if (einfo->info->strip == strip_all
2968 || (einfo->info->strip == strip_some
2969 && bfd_hash_lookup (einfo->info->keep_hash,
2970 h->root.root.root.string,
0a1b45a2
AM
2971 false, false) == NULL))
2972 strip = true;
b49e97c9 2973 else
0a1b45a2 2974 strip = false;
b49e97c9
TS
2975
2976 if (strip)
0a1b45a2 2977 return true;
b49e97c9
TS
2978
2979 if (h->esym.ifd == -2)
2980 {
2981 h->esym.jmptbl = 0;
2982 h->esym.cobol_main = 0;
2983 h->esym.weakext = 0;
2984 h->esym.reserved = 0;
2985 h->esym.ifd = ifdNil;
2986 h->esym.asym.value = 0;
2987 h->esym.asym.st = stGlobal;
2988
2989 if (h->root.root.type == bfd_link_hash_undefined
2990 || h->root.root.type == bfd_link_hash_undefweak)
2991 {
2992 const char *name;
2993
2994 /* Use undefined class. Also, set class and type for some
07d6d2b8 2995 special symbols. */
b49e97c9
TS
2996 name = h->root.root.root.string;
2997 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2998 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2999 {
3000 h->esym.asym.sc = scData;
3001 h->esym.asym.st = stLabel;
3002 h->esym.asym.value = 0;
3003 }
3004 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
3005 {
3006 h->esym.asym.sc = scAbs;
3007 h->esym.asym.st = stLabel;
3008 h->esym.asym.value =
3009 mips_elf_hash_table (einfo->info)->procedure_count;
3010 }
b49e97c9
TS
3011 else
3012 h->esym.asym.sc = scUndefined;
3013 }
3014 else if (h->root.root.type != bfd_link_hash_defined
3015 && h->root.root.type != bfd_link_hash_defweak)
3016 h->esym.asym.sc = scAbs;
3017 else
3018 {
3019 const char *name;
3020
3021 sec = h->root.root.u.def.section;
3022 output_section = sec->output_section;
3023
3024 /* When making a shared library and symbol h is the one from
3025 the another shared library, OUTPUT_SECTION may be null. */
3026 if (output_section == NULL)
3027 h->esym.asym.sc = scUndefined;
3028 else
3029 {
fd361982 3030 name = bfd_section_name (output_section);
b49e97c9
TS
3031
3032 if (strcmp (name, ".text") == 0)
3033 h->esym.asym.sc = scText;
3034 else if (strcmp (name, ".data") == 0)
3035 h->esym.asym.sc = scData;
3036 else if (strcmp (name, ".sdata") == 0)
3037 h->esym.asym.sc = scSData;
3038 else if (strcmp (name, ".rodata") == 0
3039 || strcmp (name, ".rdata") == 0)
3040 h->esym.asym.sc = scRData;
3041 else if (strcmp (name, ".bss") == 0)
3042 h->esym.asym.sc = scBss;
3043 else if (strcmp (name, ".sbss") == 0)
3044 h->esym.asym.sc = scSBss;
3045 else if (strcmp (name, ".init") == 0)
3046 h->esym.asym.sc = scInit;
3047 else if (strcmp (name, ".fini") == 0)
3048 h->esym.asym.sc = scFini;
3049 else
3050 h->esym.asym.sc = scAbs;
3051 }
3052 }
3053
3054 h->esym.asym.reserved = 0;
3055 h->esym.asym.index = indexNil;
3056 }
3057
3058 if (h->root.root.type == bfd_link_hash_common)
3059 h->esym.asym.value = h->root.root.u.c.size;
3060 else if (h->root.root.type == bfd_link_hash_defined
3061 || h->root.root.type == bfd_link_hash_defweak)
3062 {
3063 if (h->esym.asym.sc == scCommon)
3064 h->esym.asym.sc = scBss;
3065 else if (h->esym.asym.sc == scSCommon)
3066 h->esym.asym.sc = scSBss;
3067
3068 sec = h->root.root.u.def.section;
3069 output_section = sec->output_section;
3070 if (output_section != NULL)
3071 h->esym.asym.value = (h->root.root.u.def.value
3072 + sec->output_offset
3073 + output_section->vma);
3074 else
3075 h->esym.asym.value = 0;
3076 }
33bb52fb 3077 else
b49e97c9
TS
3078 {
3079 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
3080
3081 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 3082 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 3083
33bb52fb 3084 if (hd->needs_lazy_stub)
b49e97c9 3085 {
1bbce132
MR
3086 BFD_ASSERT (hd->root.plt.plist != NULL);
3087 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
b49e97c9
TS
3088 /* Set type and value for a symbol with a function stub. */
3089 h->esym.asym.st = stProc;
3090 sec = hd->root.root.u.def.section;
3091 if (sec == NULL)
3092 h->esym.asym.value = 0;
3093 else
3094 {
3095 output_section = sec->output_section;
3096 if (output_section != NULL)
1bbce132 3097 h->esym.asym.value = (hd->root.plt.plist->stub_offset
b49e97c9
TS
3098 + sec->output_offset
3099 + output_section->vma);
3100 else
3101 h->esym.asym.value = 0;
3102 }
b49e97c9
TS
3103 }
3104 }
3105
3106 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3107 h->root.root.root.string,
3108 &h->esym))
3109 {
0a1b45a2
AM
3110 einfo->failed = true;
3111 return false;
b49e97c9
TS
3112 }
3113
0a1b45a2 3114 return true;
b49e97c9
TS
3115}
3116
3117/* A comparison routine used to sort .gptab entries. */
3118
3119static int
9719ad41 3120gptab_compare (const void *p1, const void *p2)
b49e97c9 3121{
9719ad41
RS
3122 const Elf32_gptab *a1 = p1;
3123 const Elf32_gptab *a2 = p2;
b49e97c9
TS
3124
3125 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3126}
3127\f
b15e6682 3128/* Functions to manage the got entry hash table. */
f4416af6
AO
3129
3130/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3131 hash number. */
3132
42eec46f 3133static inline hashval_t
9719ad41 3134mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
3135{
3136#ifdef BFD64
3137 return addr + (addr >> 32);
3138#else
3139 return addr;
3140#endif
3141}
3142
f4416af6 3143static hashval_t
d9bf376d 3144mips_elf_got_entry_hash (const void *entry_)
f4416af6
AO
3145{
3146 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3147
e641e783 3148 return (entry->symndx
9ab066b4
RS
3149 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3150 + (entry->tls_type == GOT_TLS_LDM ? 0
e641e783
RS
3151 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3152 : entry->symndx >= 0 ? (entry->abfd->id
3153 + mips_elf_hash_bfd_vma (entry->d.addend))
3154 : entry->d.h->root.root.root.hash));
f4416af6
AO
3155}
3156
3157static int
3dff0dd1 3158mips_elf_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
3159{
3160 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3161 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3162
e641e783 3163 return (e1->symndx == e2->symndx
9ab066b4 3164 && e1->tls_type == e2->tls_type
0a1b45a2 3165 && (e1->tls_type == GOT_TLS_LDM ? true
e641e783
RS
3166 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3167 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3168 && e1->d.addend == e2->d.addend)
3169 : e2->abfd && e1->d.h == e2->d.h));
b15e6682 3170}
c224138d 3171
13db6b44
RS
3172static hashval_t
3173mips_got_page_ref_hash (const void *ref_)
3174{
3175 const struct mips_got_page_ref *ref;
3176
3177 ref = (const struct mips_got_page_ref *) ref_;
3178 return ((ref->symndx >= 0
3179 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3180 : ref->u.h->root.root.root.hash)
3181 + mips_elf_hash_bfd_vma (ref->addend));
3182}
3183
3184static int
3185mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3186{
3187 const struct mips_got_page_ref *ref1, *ref2;
3188
3189 ref1 = (const struct mips_got_page_ref *) ref1_;
3190 ref2 = (const struct mips_got_page_ref *) ref2_;
3191 return (ref1->symndx == ref2->symndx
3192 && (ref1->symndx < 0
3193 ? ref1->u.h == ref2->u.h
3194 : ref1->u.abfd == ref2->u.abfd)
3195 && ref1->addend == ref2->addend);
3196}
3197
c224138d
RS
3198static hashval_t
3199mips_got_page_entry_hash (const void *entry_)
3200{
3201 const struct mips_got_page_entry *entry;
3202
3203 entry = (const struct mips_got_page_entry *) entry_;
13db6b44 3204 return entry->sec->id;
c224138d
RS
3205}
3206
3207static int
3208mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3209{
3210 const struct mips_got_page_entry *entry1, *entry2;
3211
3212 entry1 = (const struct mips_got_page_entry *) entry1_;
3213 entry2 = (const struct mips_got_page_entry *) entry2_;
13db6b44 3214 return entry1->sec == entry2->sec;
c224138d 3215}
b15e6682 3216\f
3dff0dd1 3217/* Create and return a new mips_got_info structure. */
5334aa52
RS
3218
3219static struct mips_got_info *
3dff0dd1 3220mips_elf_create_got_info (bfd *abfd)
5334aa52
RS
3221{
3222 struct mips_got_info *g;
3223
3224 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3225 if (g == NULL)
3226 return NULL;
3227
3dff0dd1
RS
3228 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3229 mips_elf_got_entry_eq, NULL);
5334aa52
RS
3230 if (g->got_entries == NULL)
3231 return NULL;
3232
13db6b44
RS
3233 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3234 mips_got_page_ref_eq, NULL);
3235 if (g->got_page_refs == NULL)
5334aa52
RS
3236 return NULL;
3237
3238 return g;
3239}
3240
ee227692
RS
3241/* Return the GOT info for input bfd ABFD, trying to create a new one if
3242 CREATE_P and if ABFD doesn't already have a GOT. */
3243
3244static struct mips_got_info *
0a1b45a2 3245mips_elf_bfd_got (bfd *abfd, bool create_p)
ee227692
RS
3246{
3247 struct mips_elf_obj_tdata *tdata;
3248
3249 if (!is_mips_elf (abfd))
3250 return NULL;
3251
3252 tdata = mips_elf_tdata (abfd);
3253 if (!tdata->got && create_p)
3dff0dd1 3254 tdata->got = mips_elf_create_got_info (abfd);
ee227692
RS
3255 return tdata->got;
3256}
3257
d7206569
RS
3258/* Record that ABFD should use output GOT G. */
3259
3260static void
3261mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3262{
3263 struct mips_elf_obj_tdata *tdata;
3264
3265 BFD_ASSERT (is_mips_elf (abfd));
3266 tdata = mips_elf_tdata (abfd);
3267 if (tdata->got)
3268 {
3269 /* The GOT structure itself and the hash table entries are
3270 allocated to a bfd, but the hash tables aren't. */
3271 htab_delete (tdata->got->got_entries);
13db6b44
RS
3272 htab_delete (tdata->got->got_page_refs);
3273 if (tdata->got->got_page_entries)
3274 htab_delete (tdata->got->got_page_entries);
d7206569
RS
3275 }
3276 tdata->got = g;
3277}
3278
0a44bf69
RS
3279/* Return the dynamic relocation section. If it doesn't exist, try to
3280 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3281 if creation fails. */
f4416af6
AO
3282
3283static asection *
0a1b45a2 3284mips_elf_rel_dyn_section (struct bfd_link_info *info, bool create_p)
f4416af6 3285{
0a44bf69 3286 const char *dname;
f4416af6 3287 asection *sreloc;
0a44bf69 3288 bfd *dynobj;
f4416af6 3289
0a44bf69
RS
3290 dname = MIPS_ELF_REL_DYN_NAME (info);
3291 dynobj = elf_hash_table (info)->dynobj;
3d4d4302 3292 sreloc = bfd_get_linker_section (dynobj, dname);
f4416af6
AO
3293 if (sreloc == NULL && create_p)
3294 {
3d4d4302
AM
3295 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3296 (SEC_ALLOC
3297 | SEC_LOAD
3298 | SEC_HAS_CONTENTS
3299 | SEC_IN_MEMORY
3300 | SEC_LINKER_CREATED
3301 | SEC_READONLY));
f4416af6 3302 if (sreloc == NULL
fd361982
AM
3303 || !bfd_set_section_alignment (sreloc,
3304 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
3305 return NULL;
3306 }
3307 return sreloc;
3308}
3309
e641e783
RS
3310/* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3311
3312static int
3313mips_elf_reloc_tls_type (unsigned int r_type)
3314{
3315 if (tls_gd_reloc_p (r_type))
3316 return GOT_TLS_GD;
3317
3318 if (tls_ldm_reloc_p (r_type))
3319 return GOT_TLS_LDM;
3320
3321 if (tls_gottprel_reloc_p (r_type))
3322 return GOT_TLS_IE;
3323
9ab066b4 3324 return GOT_TLS_NONE;
e641e783
RS
3325}
3326
3327/* Return the number of GOT slots needed for GOT TLS type TYPE. */
3328
3329static int
3330mips_tls_got_entries (unsigned int type)
3331{
3332 switch (type)
3333 {
3334 case GOT_TLS_GD:
3335 case GOT_TLS_LDM:
3336 return 2;
3337
3338 case GOT_TLS_IE:
3339 return 1;
3340
9ab066b4 3341 case GOT_TLS_NONE:
e641e783
RS
3342 return 0;
3343 }
3344 abort ();
3345}
3346
0f20cc35
DJ
3347/* Count the number of relocations needed for a TLS GOT entry, with
3348 access types from TLS_TYPE, and symbol H (or a local symbol if H
3349 is NULL). */
3350
3351static int
3352mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3353 struct elf_link_hash_entry *h)
3354{
3355 int indx = 0;
0a1b45a2
AM
3356 bool need_relocs = false;
3357 bool dyn = elf_hash_table (info)->dynamic_sections_created;
0f20cc35 3358
1cb83cac
MR
3359 if (h != NULL
3360 && h->dynindx != -1
3361 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3362 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
0f20cc35
DJ
3363 indx = h->dynindx;
3364
9143e72c 3365 if ((bfd_link_dll (info) || indx != 0)
0f20cc35
DJ
3366 && (h == NULL
3367 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3368 || h->root.type != bfd_link_hash_undefweak))
0a1b45a2 3369 need_relocs = true;
0f20cc35
DJ
3370
3371 if (!need_relocs)
e641e783 3372 return 0;
0f20cc35 3373
9ab066b4 3374 switch (tls_type)
0f20cc35 3375 {
e641e783
RS
3376 case GOT_TLS_GD:
3377 return indx != 0 ? 2 : 1;
0f20cc35 3378
e641e783
RS
3379 case GOT_TLS_IE:
3380 return 1;
0f20cc35 3381
e641e783 3382 case GOT_TLS_LDM:
9143e72c 3383 return bfd_link_dll (info) ? 1 : 0;
0f20cc35 3384
e641e783
RS
3385 default:
3386 return 0;
3387 }
0f20cc35
DJ
3388}
3389
ab361d49
RS
3390/* Add the number of GOT entries and TLS relocations required by ENTRY
3391 to G. */
0f20cc35 3392
ab361d49
RS
3393static void
3394mips_elf_count_got_entry (struct bfd_link_info *info,
3395 struct mips_got_info *g,
3396 struct mips_got_entry *entry)
0f20cc35 3397{
9ab066b4 3398 if (entry->tls_type)
ab361d49 3399 {
9ab066b4
RS
3400 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3401 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
ab361d49
RS
3402 entry->symndx < 0
3403 ? &entry->d.h->root : NULL);
3404 }
3405 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3406 g->local_gotno += 1;
3407 else
3408 g->global_gotno += 1;
0f20cc35
DJ
3409}
3410
0f20cc35
DJ
3411/* Output a simple dynamic relocation into SRELOC. */
3412
3413static void
3414mips_elf_output_dynamic_relocation (bfd *output_bfd,
3415 asection *sreloc,
861fb55a 3416 unsigned long reloc_index,
0f20cc35
DJ
3417 unsigned long indx,
3418 int r_type,
3419 bfd_vma offset)
3420{
3421 Elf_Internal_Rela rel[3];
3422
3423 memset (rel, 0, sizeof (rel));
3424
3425 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3426 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3427
3428 if (ABI_64_P (output_bfd))
3429 {
3430 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3431 (output_bfd, &rel[0],
3432 (sreloc->contents
861fb55a 3433 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
3434 }
3435 else
3436 bfd_elf32_swap_reloc_out
3437 (output_bfd, &rel[0],
3438 (sreloc->contents
861fb55a 3439 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
3440}
3441
3442/* Initialize a set of TLS GOT entries for one symbol. */
3443
3444static void
9ab066b4
RS
3445mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3446 struct mips_got_entry *entry,
0f20cc35
DJ
3447 struct mips_elf_link_hash_entry *h,
3448 bfd_vma value)
3449{
0a1b45a2 3450 bool dyn = elf_hash_table (info)->dynamic_sections_created;
23cc69b6 3451 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
3452 int indx;
3453 asection *sreloc, *sgot;
9ab066b4 3454 bfd_vma got_offset, got_offset2;
0a1b45a2 3455 bool need_relocs = false;
0f20cc35 3456
23cc69b6 3457 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3458 if (htab == NULL)
3459 return;
3460
ce558b89 3461 sgot = htab->root.sgot;
0f20cc35
DJ
3462
3463 indx = 0;
1cb83cac
MR
3464 if (h != NULL
3465 && h->root.dynindx != -1
3466 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3467 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3468 indx = h->root.dynindx;
0f20cc35 3469
9ab066b4 3470 if (entry->tls_initialized)
0f20cc35
DJ
3471 return;
3472
9143e72c 3473 if ((bfd_link_dll (info) || indx != 0)
0f20cc35
DJ
3474 && (h == NULL
3475 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3476 || h->root.type != bfd_link_hash_undefweak))
0a1b45a2 3477 need_relocs = true;
0f20cc35
DJ
3478
3479 /* MINUS_ONE means the symbol is not defined in this object. It may not
3480 be defined at all; assume that the value doesn't matter in that
3481 case. Otherwise complain if we would use the value. */
3482 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3483 || h->root.root.type == bfd_link_hash_undefweak);
3484
3485 /* Emit necessary relocations. */
0a1b45a2 3486 sreloc = mips_elf_rel_dyn_section (info, false);
9ab066b4 3487 got_offset = entry->gotidx;
0f20cc35 3488
9ab066b4 3489 switch (entry->tls_type)
0f20cc35 3490 {
e641e783
RS
3491 case GOT_TLS_GD:
3492 /* General Dynamic. */
3493 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
0f20cc35
DJ
3494
3495 if (need_relocs)
3496 {
3497 mips_elf_output_dynamic_relocation
861fb55a 3498 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3499 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
e641e783 3500 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3501
3502 if (indx)
3503 mips_elf_output_dynamic_relocation
861fb55a 3504 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3505 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
e641e783 3506 sgot->output_offset + sgot->output_section->vma + got_offset2);
0f20cc35
DJ
3507 else
3508 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3509 sgot->contents + got_offset2);
0f20cc35
DJ
3510 }
3511 else
3512 {
3513 MIPS_ELF_PUT_WORD (abfd, 1,
e641e783 3514 sgot->contents + got_offset);
0f20cc35 3515 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3516 sgot->contents + got_offset2);
0f20cc35 3517 }
e641e783 3518 break;
0f20cc35 3519
e641e783
RS
3520 case GOT_TLS_IE:
3521 /* Initial Exec model. */
0f20cc35
DJ
3522 if (need_relocs)
3523 {
3524 if (indx == 0)
3525 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
e641e783 3526 sgot->contents + got_offset);
0f20cc35
DJ
3527 else
3528 MIPS_ELF_PUT_WORD (abfd, 0,
e641e783 3529 sgot->contents + got_offset);
0f20cc35
DJ
3530
3531 mips_elf_output_dynamic_relocation
861fb55a 3532 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3533 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
e641e783 3534 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3535 }
3536 else
3537 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
e641e783
RS
3538 sgot->contents + got_offset);
3539 break;
0f20cc35 3540
e641e783 3541 case GOT_TLS_LDM:
0f20cc35
DJ
3542 /* The initial offset is zero, and the LD offsets will include the
3543 bias by DTP_OFFSET. */
3544 MIPS_ELF_PUT_WORD (abfd, 0,
3545 sgot->contents + got_offset
3546 + MIPS_ELF_GOT_SIZE (abfd));
3547
9143e72c 3548 if (!bfd_link_dll (info))
0f20cc35
DJ
3549 MIPS_ELF_PUT_WORD (abfd, 1,
3550 sgot->contents + got_offset);
3551 else
3552 mips_elf_output_dynamic_relocation
861fb55a 3553 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3554 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3555 sgot->output_offset + sgot->output_section->vma + got_offset);
e641e783
RS
3556 break;
3557
3558 default:
3559 abort ();
0f20cc35
DJ
3560 }
3561
0a1b45a2 3562 entry->tls_initialized = true;
e641e783 3563}
0f20cc35 3564
0a44bf69
RS
3565/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3566 for global symbol H. .got.plt comes before the GOT, so the offset
3567 will be negative. */
3568
3569static bfd_vma
3570mips_elf_gotplt_index (struct bfd_link_info *info,
3571 struct elf_link_hash_entry *h)
3572{
1bbce132 3573 bfd_vma got_address, got_value;
0a44bf69
RS
3574 struct mips_elf_link_hash_table *htab;
3575
3576 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3577 BFD_ASSERT (htab != NULL);
3578
1bbce132
MR
3579 BFD_ASSERT (h->plt.plist != NULL);
3580 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
0a44bf69
RS
3581
3582 /* Calculate the address of the associated .got.plt entry. */
ce558b89
AM
3583 got_address = (htab->root.sgotplt->output_section->vma
3584 + htab->root.sgotplt->output_offset
1bbce132
MR
3585 + (h->plt.plist->gotplt_index
3586 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
0a44bf69
RS
3587
3588 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3589 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3590 + htab->root.hgot->root.u.def.section->output_offset
3591 + htab->root.hgot->root.u.def.value);
3592
3593 return got_address - got_value;
3594}
3595
5c18022e 3596/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3597 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3598 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3599 offset can be found. */
b49e97c9
TS
3600
3601static bfd_vma
9719ad41 3602mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3603 bfd_vma value, unsigned long r_symndx,
0f20cc35 3604 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3605{
a8028dd0 3606 struct mips_elf_link_hash_table *htab;
b15e6682 3607 struct mips_got_entry *entry;
b49e97c9 3608
a8028dd0 3609 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3610 BFD_ASSERT (htab != NULL);
3611
a8028dd0
RS
3612 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3613 r_symndx, h, r_type);
0f20cc35 3614 if (!entry)
b15e6682 3615 return MINUS_ONE;
0f20cc35 3616
e641e783 3617 if (entry->tls_type)
9ab066b4
RS
3618 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3619 return entry->gotidx;
b49e97c9
TS
3620}
3621
13fbec83 3622/* Return the GOT index of global symbol H in the primary GOT. */
b49e97c9
TS
3623
3624static bfd_vma
13fbec83
RS
3625mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3626 struct elf_link_hash_entry *h)
3627{
3628 struct mips_elf_link_hash_table *htab;
3629 long global_got_dynindx;
3630 struct mips_got_info *g;
3631 bfd_vma got_index;
3632
3633 htab = mips_elf_hash_table (info);
3634 BFD_ASSERT (htab != NULL);
3635
3636 global_got_dynindx = 0;
3637 if (htab->global_gotsym != NULL)
3638 global_got_dynindx = htab->global_gotsym->dynindx;
3639
3640 /* Once we determine the global GOT entry with the lowest dynamic
3641 symbol table index, we must put all dynamic symbols with greater
3642 indices into the primary GOT. That makes it easy to calculate the
3643 GOT offset. */
3644 BFD_ASSERT (h->dynindx >= global_got_dynindx);
0a1b45a2 3645 g = mips_elf_bfd_got (obfd, false);
13fbec83
RS
3646 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3647 * MIPS_ELF_GOT_SIZE (obfd));
ce558b89 3648 BFD_ASSERT (got_index < htab->root.sgot->size);
13fbec83
RS
3649
3650 return got_index;
3651}
3652
3653/* Return the GOT index for the global symbol indicated by H, which is
3654 referenced by a relocation of type R_TYPE in IBFD. */
3655
3656static bfd_vma
3657mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3658 struct elf_link_hash_entry *h, int r_type)
b49e97c9 3659{
a8028dd0 3660 struct mips_elf_link_hash_table *htab;
6c42ddb9
RS
3661 struct mips_got_info *g;
3662 struct mips_got_entry lookup, *entry;
3663 bfd_vma gotidx;
b49e97c9 3664
a8028dd0 3665 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3666 BFD_ASSERT (htab != NULL);
3667
0a1b45a2 3668 g = mips_elf_bfd_got (ibfd, false);
6c42ddb9 3669 BFD_ASSERT (g);
f4416af6 3670
6c42ddb9 3671 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
0a1b45a2 3672 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, false))
6c42ddb9 3673 return mips_elf_primary_global_got_index (obfd, info, h);
f4416af6 3674
6c42ddb9
RS
3675 lookup.abfd = ibfd;
3676 lookup.symndx = -1;
3677 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3678 entry = htab_find (g->got_entries, &lookup);
3679 BFD_ASSERT (entry);
0f20cc35 3680
6c42ddb9 3681 gotidx = entry->gotidx;
ce558b89 3682 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
f4416af6 3683
6c42ddb9 3684 if (lookup.tls_type)
0f20cc35 3685 {
0f20cc35
DJ
3686 bfd_vma value = MINUS_ONE;
3687
3688 if ((h->root.type == bfd_link_hash_defined
3689 || h->root.type == bfd_link_hash_defweak)
3690 && h->root.u.def.section->output_section)
3691 value = (h->root.u.def.value
3692 + h->root.u.def.section->output_offset
3693 + h->root.u.def.section->output_section->vma);
3694
9ab066b4 3695 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
0f20cc35 3696 }
6c42ddb9 3697 return gotidx;
b49e97c9
TS
3698}
3699
5c18022e
RS
3700/* Find a GOT page entry that points to within 32KB of VALUE. These
3701 entries are supposed to be placed at small offsets in the GOT, i.e.,
3702 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3703 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3704 offset of the GOT entry from VALUE. */
b49e97c9
TS
3705
3706static bfd_vma
9719ad41 3707mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3708 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3709{
91d6fa6a 3710 bfd_vma page, got_index;
b15e6682 3711 struct mips_got_entry *entry;
b49e97c9 3712
0a44bf69 3713 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3714 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3715 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3716
b15e6682
AO
3717 if (!entry)
3718 return MINUS_ONE;
143d77c5 3719
91d6fa6a 3720 got_index = entry->gotidx;
b49e97c9
TS
3721
3722 if (offsetp)
f4416af6 3723 *offsetp = value - entry->d.address;
b49e97c9 3724
91d6fa6a 3725 return got_index;
b49e97c9
TS
3726}
3727
738e5348 3728/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
020d7251
RS
3729 EXTERNAL is true if the relocation was originally against a global
3730 symbol that binds locally. */
b49e97c9
TS
3731
3732static bfd_vma
9719ad41 3733mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
0a1b45a2 3734 bfd_vma value, bool external)
b49e97c9 3735{
b15e6682 3736 struct mips_got_entry *entry;
b49e97c9 3737
0a44bf69
RS
3738 /* GOT16 relocations against local symbols are followed by a LO16
3739 relocation; those against global symbols are not. Thus if the
3740 symbol was originally local, the GOT16 relocation should load the
3741 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3742 if (! external)
0a44bf69 3743 value = mips_elf_high (value) << 16;
b49e97c9 3744
738e5348
RS
3745 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3746 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3747 same in all cases. */
a8028dd0
RS
3748 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3749 NULL, R_MIPS_GOT16);
b15e6682
AO
3750 if (entry)
3751 return entry->gotidx;
3752 else
3753 return MINUS_ONE;
b49e97c9
TS
3754}
3755
3756/* Returns the offset for the entry at the INDEXth position
3757 in the GOT. */
3758
3759static bfd_vma
a8028dd0 3760mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3761 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3762{
a8028dd0 3763 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3764 asection *sgot;
3765 bfd_vma gp;
3766
a8028dd0 3767 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3768 BFD_ASSERT (htab != NULL);
3769
ce558b89 3770 sgot = htab->root.sgot;
f4416af6 3771 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3772 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3773
91d6fa6a 3774 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3775}
3776
0a44bf69
RS
3777/* Create and return a local GOT entry for VALUE, which was calculated
3778 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3779 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3780 instead. */
b49e97c9 3781
b15e6682 3782static struct mips_got_entry *
0a44bf69 3783mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3784 bfd *ibfd, bfd_vma value,
5c18022e 3785 unsigned long r_symndx,
0f20cc35
DJ
3786 struct mips_elf_link_hash_entry *h,
3787 int r_type)
b49e97c9 3788{
ebc53538
RS
3789 struct mips_got_entry lookup, *entry;
3790 void **loc;
f4416af6 3791 struct mips_got_info *g;
0a44bf69 3792 struct mips_elf_link_hash_table *htab;
6c42ddb9 3793 bfd_vma gotidx;
0a44bf69
RS
3794
3795 htab = mips_elf_hash_table (info);
4dfe6ac6 3796 BFD_ASSERT (htab != NULL);
b15e6682 3797
0a1b45a2 3798 g = mips_elf_bfd_got (ibfd, false);
f4416af6
AO
3799 if (g == NULL)
3800 {
0a1b45a2 3801 g = mips_elf_bfd_got (abfd, false);
f4416af6
AO
3802 BFD_ASSERT (g != NULL);
3803 }
b15e6682 3804
020d7251
RS
3805 /* This function shouldn't be called for symbols that live in the global
3806 area of the GOT. */
3807 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
0f20cc35 3808
ebc53538
RS
3809 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3810 if (lookup.tls_type)
3811 {
3812 lookup.abfd = ibfd;
df58fc94 3813 if (tls_ldm_reloc_p (r_type))
0f20cc35 3814 {
ebc53538
RS
3815 lookup.symndx = 0;
3816 lookup.d.addend = 0;
0f20cc35
DJ
3817 }
3818 else if (h == NULL)
3819 {
ebc53538
RS
3820 lookup.symndx = r_symndx;
3821 lookup.d.addend = 0;
0f20cc35
DJ
3822 }
3823 else
ebc53538
RS
3824 {
3825 lookup.symndx = -1;
3826 lookup.d.h = h;
3827 }
0f20cc35 3828
ebc53538
RS
3829 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3830 BFD_ASSERT (entry);
0f20cc35 3831
6c42ddb9 3832 gotidx = entry->gotidx;
ce558b89 3833 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
6c42ddb9 3834
ebc53538 3835 return entry;
0f20cc35
DJ
3836 }
3837
ebc53538
RS
3838 lookup.abfd = NULL;
3839 lookup.symndx = -1;
3840 lookup.d.address = value;
3841 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3842 if (!loc)
b15e6682 3843 return NULL;
143d77c5 3844
ebc53538
RS
3845 entry = (struct mips_got_entry *) *loc;
3846 if (entry)
3847 return entry;
b15e6682 3848
cb22ccf4 3849 if (g->assigned_low_gotno > g->assigned_high_gotno)
b49e97c9
TS
3850 {
3851 /* We didn't allocate enough space in the GOT. */
4eca0228 3852 _bfd_error_handler
b49e97c9
TS
3853 (_("not enough GOT space for local GOT entries"));
3854 bfd_set_error (bfd_error_bad_value);
b15e6682 3855 return NULL;
b49e97c9
TS
3856 }
3857
ebc53538
RS
3858 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3859 if (!entry)
3860 return NULL;
3861
cb22ccf4
KCY
3862 if (got16_reloc_p (r_type)
3863 || call16_reloc_p (r_type)
3864 || got_page_reloc_p (r_type)
3865 || got_disp_reloc_p (r_type))
3866 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3867 else
3868 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3869
ebc53538
RS
3870 *entry = lookup;
3871 *loc = entry;
3872
ce558b89 3873 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
b15e6682 3874
5c18022e 3875 /* These GOT entries need a dynamic relocation on VxWorks. */
90c14f0c 3876 if (htab->root.target_os == is_vxworks)
0a44bf69
RS
3877 {
3878 Elf_Internal_Rela outrel;
5c18022e 3879 asection *s;
91d6fa6a 3880 bfd_byte *rloc;
0a44bf69 3881 bfd_vma got_address;
0a44bf69 3882
0a1b45a2 3883 s = mips_elf_rel_dyn_section (info, false);
ce558b89
AM
3884 got_address = (htab->root.sgot->output_section->vma
3885 + htab->root.sgot->output_offset
ebc53538 3886 + entry->gotidx);
0a44bf69 3887
91d6fa6a 3888 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3889 outrel.r_offset = got_address;
5c18022e
RS
3890 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3891 outrel.r_addend = value;
91d6fa6a 3892 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3893 }
3894
ebc53538 3895 return entry;
b49e97c9
TS
3896}
3897
d4596a51
RS
3898/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3899 The number might be exact or a worst-case estimate, depending on how
3900 much information is available to elf_backend_omit_section_dynsym at
3901 the current linking stage. */
3902
3903static bfd_size_type
3904count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3905{
3906 bfd_size_type count;
3907
3908 count = 0;
0e1862bb
L
3909 if (bfd_link_pic (info)
3910 || elf_hash_table (info)->is_relocatable_executable)
d4596a51
RS
3911 {
3912 asection *p;
3913 const struct elf_backend_data *bed;
3914
3915 bed = get_elf_backend_data (output_bfd);
3916 for (p = output_bfd->sections; p ; p = p->next)
3917 if ((p->flags & SEC_EXCLUDE) == 0
3918 && (p->flags & SEC_ALLOC) != 0
7f923b7f 3919 && elf_hash_table (info)->dynamic_relocs
d4596a51
RS
3920 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3921 ++count;
3922 }
3923 return count;
3924}
3925
b49e97c9 3926/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3927 appear towards the end. */
b49e97c9 3928
0a1b45a2 3929static bool
d4596a51 3930mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3931{
a8028dd0 3932 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3933 struct mips_elf_hash_sort_data hsd;
3934 struct mips_got_info *g;
b49e97c9 3935
a8028dd0 3936 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3937 BFD_ASSERT (htab != NULL);
3938
0f8c4b60 3939 if (htab->root.dynsymcount == 0)
0a1b45a2 3940 return true;
17a80fa8 3941
a8028dd0 3942 g = htab->got_info;
d4596a51 3943 if (g == NULL)
0a1b45a2 3944 return true;
f4416af6 3945
b49e97c9 3946 hsd.low = NULL;
23cc69b6
RS
3947 hsd.max_unref_got_dynindx
3948 = hsd.min_got_dynindx
0f8c4b60 3949 = (htab->root.dynsymcount - g->reloc_only_gotno);
e17b0c35
MR
3950 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3951 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3952 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3953 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
f16a9783
MS
3954 hsd.output_bfd = abfd;
3955 if (htab->root.dynobj != NULL
3956 && htab->root.dynamic_sections_created
3957 && info->emit_gnu_hash)
3958 {
3959 asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3960 BFD_ASSERT (s != NULL);
3961 hsd.mipsxhash = s->contents;
3962 BFD_ASSERT (hsd.mipsxhash != NULL);
3963 }
3964 else
3965 hsd.mipsxhash = NULL;
0f8c4b60 3966 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
b49e97c9
TS
3967
3968 /* There should have been enough room in the symbol table to
44c410de 3969 accommodate both the GOT and non-GOT symbols. */
e17b0c35 3970 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
b49e97c9 3971 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
55f8b9d2 3972 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
0f8c4b60 3973 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
b49e97c9
TS
3974
3975 /* Now we know which dynamic symbol has the lowest dynamic symbol
3976 table index in the GOT. */
d222d210 3977 htab->global_gotsym = hsd.low;
b49e97c9 3978
0a1b45a2 3979 return true;
b49e97c9
TS
3980}
3981
3982/* If H needs a GOT entry, assign it the highest available dynamic
3983 index. Otherwise, assign it the lowest available dynamic
3984 index. */
3985
0a1b45a2 3986static bool
9719ad41 3987mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3988{
9719ad41 3989 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9 3990
b49e97c9
TS
3991 /* Symbols without dynamic symbol table entries aren't interesting
3992 at all. */
3993 if (h->root.dynindx == -1)
0a1b45a2 3994 return true;
b49e97c9 3995
634835ae 3996 switch (h->global_got_area)
f4416af6 3997 {
634835ae 3998 case GGA_NONE:
e17b0c35
MR
3999 if (h->root.forced_local)
4000 h->root.dynindx = hsd->max_local_dynindx++;
4001 else
4002 h->root.dynindx = hsd->max_non_got_dynindx++;
634835ae 4003 break;
0f20cc35 4004
634835ae 4005 case GGA_NORMAL:
b49e97c9
TS
4006 h->root.dynindx = --hsd->min_got_dynindx;
4007 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
4008 break;
4009
4010 case GGA_RELOC_ONLY:
634835ae
RS
4011 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
4012 hsd->low = (struct elf_link_hash_entry *) h;
4013 h->root.dynindx = hsd->max_unref_got_dynindx++;
4014 break;
b49e97c9
TS
4015 }
4016
f16a9783
MS
4017 /* Populate the .MIPS.xhash translation table entry with
4018 the symbol dynindx. */
4019 if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
4020 bfd_put_32 (hsd->output_bfd, h->root.dynindx,
4021 hsd->mipsxhash + h->mipsxhash_loc);
4022
0a1b45a2 4023 return true;
b49e97c9
TS
4024}
4025
ee227692
RS
4026/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
4027 (which is owned by the caller and shouldn't be added to the
4028 hash table directly). */
4029
0a1b45a2 4030static bool
ee227692
RS
4031mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
4032 struct mips_got_entry *lookup)
4033{
4034 struct mips_elf_link_hash_table *htab;
4035 struct mips_got_entry *entry;
4036 struct mips_got_info *g;
4037 void **loc, **bfd_loc;
4038
4039 /* Make sure there's a slot for this entry in the master GOT. */
4040 htab = mips_elf_hash_table (info);
4041 g = htab->got_info;
4042 loc = htab_find_slot (g->got_entries, lookup, INSERT);
4043 if (!loc)
0a1b45a2 4044 return false;
ee227692
RS
4045
4046 /* Populate the entry if it isn't already. */
4047 entry = (struct mips_got_entry *) *loc;
4048 if (!entry)
4049 {
4050 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
4051 if (!entry)
0a1b45a2 4052 return false;
ee227692 4053
0a1b45a2 4054 lookup->tls_initialized = false;
ee227692
RS
4055 lookup->gotidx = -1;
4056 *entry = *lookup;
4057 *loc = entry;
4058 }
4059
4060 /* Reuse the same GOT entry for the BFD's GOT. */
0a1b45a2 4061 g = mips_elf_bfd_got (abfd, true);
ee227692 4062 if (!g)
0a1b45a2 4063 return false;
ee227692
RS
4064
4065 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4066 if (!bfd_loc)
0a1b45a2 4067 return false;
ee227692
RS
4068
4069 if (!*bfd_loc)
4070 *bfd_loc = entry;
0a1b45a2 4071 return true;
ee227692
RS
4072}
4073
e641e783
RS
4074/* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
4075 entry for it. FOR_CALL is true if the caller is only interested in
6ccf4795 4076 using the GOT entry for calls. */
b49e97c9 4077
0a1b45a2 4078static bool
9719ad41
RS
4079mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4080 bfd *abfd, struct bfd_link_info *info,
0a1b45a2 4081 bool for_call, int r_type)
b49e97c9 4082{
a8028dd0 4083 struct mips_elf_link_hash_table *htab;
634835ae 4084 struct mips_elf_link_hash_entry *hmips;
ee227692
RS
4085 struct mips_got_entry entry;
4086 unsigned char tls_type;
a8028dd0
RS
4087
4088 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4089 BFD_ASSERT (htab != NULL);
4090
634835ae 4091 hmips = (struct mips_elf_link_hash_entry *) h;
6ccf4795 4092 if (!for_call)
0a1b45a2 4093 hmips->got_only_for_calls = false;
f4416af6 4094
b49e97c9
TS
4095 /* A global symbol in the GOT must also be in the dynamic symbol
4096 table. */
7c5fcef7
L
4097 if (h->dynindx == -1)
4098 {
4099 switch (ELF_ST_VISIBILITY (h->other))
4100 {
4101 case STV_INTERNAL:
4102 case STV_HIDDEN:
0a1b45a2 4103 _bfd_mips_elf_hide_symbol (info, h, true);
7c5fcef7
L
4104 break;
4105 }
c152c796 4106 if (!bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 4107 return false;
7c5fcef7 4108 }
b49e97c9 4109
ee227692 4110 tls_type = mips_elf_reloc_tls_type (r_type);
9ab066b4 4111 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
ee227692 4112 hmips->global_got_area = GGA_NORMAL;
86324f90 4113
f4416af6
AO
4114 entry.abfd = abfd;
4115 entry.symndx = -1;
4116 entry.d.h = (struct mips_elf_link_hash_entry *) h;
ee227692
RS
4117 entry.tls_type = tls_type;
4118 return mips_elf_record_got_entry (info, abfd, &entry);
b49e97c9 4119}
f4416af6 4120
e641e783
RS
4121/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4122 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
f4416af6 4123
0a1b45a2 4124static bool
9719ad41 4125mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
e641e783 4126 struct bfd_link_info *info, int r_type)
f4416af6 4127{
a8028dd0
RS
4128 struct mips_elf_link_hash_table *htab;
4129 struct mips_got_info *g;
ee227692 4130 struct mips_got_entry entry;
f4416af6 4131
a8028dd0 4132 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4133 BFD_ASSERT (htab != NULL);
4134
a8028dd0
RS
4135 g = htab->got_info;
4136 BFD_ASSERT (g != NULL);
4137
f4416af6
AO
4138 entry.abfd = abfd;
4139 entry.symndx = symndx;
4140 entry.d.addend = addend;
e641e783 4141 entry.tls_type = mips_elf_reloc_tls_type (r_type);
ee227692 4142 return mips_elf_record_got_entry (info, abfd, &entry);
f4416af6 4143}
c224138d 4144
13db6b44
RS
4145/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4146 H is the symbol's hash table entry, or null if SYMNDX is local
4147 to ABFD. */
c224138d 4148
0a1b45a2 4149static bool
13db6b44
RS
4150mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4151 long symndx, struct elf_link_hash_entry *h,
4152 bfd_signed_vma addend)
c224138d 4153{
a8028dd0 4154 struct mips_elf_link_hash_table *htab;
ee227692 4155 struct mips_got_info *g1, *g2;
13db6b44 4156 struct mips_got_page_ref lookup, *entry;
ee227692 4157 void **loc, **bfd_loc;
c224138d 4158
a8028dd0 4159 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4160 BFD_ASSERT (htab != NULL);
4161
ee227692
RS
4162 g1 = htab->got_info;
4163 BFD_ASSERT (g1 != NULL);
a8028dd0 4164
13db6b44
RS
4165 if (h)
4166 {
4167 lookup.symndx = -1;
4168 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4169 }
4170 else
4171 {
4172 lookup.symndx = symndx;
4173 lookup.u.abfd = abfd;
4174 }
4175 lookup.addend = addend;
4176 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
c224138d 4177 if (loc == NULL)
0a1b45a2 4178 return false;
c224138d 4179
13db6b44 4180 entry = (struct mips_got_page_ref *) *loc;
c224138d
RS
4181 if (!entry)
4182 {
4183 entry = bfd_alloc (abfd, sizeof (*entry));
4184 if (!entry)
0a1b45a2 4185 return false;
c224138d 4186
13db6b44 4187 *entry = lookup;
c224138d
RS
4188 *loc = entry;
4189 }
4190
ee227692 4191 /* Add the same entry to the BFD's GOT. */
0a1b45a2 4192 g2 = mips_elf_bfd_got (abfd, true);
ee227692 4193 if (!g2)
0a1b45a2 4194 return false;
ee227692 4195
13db6b44 4196 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
ee227692 4197 if (!bfd_loc)
0a1b45a2 4198 return false;
ee227692
RS
4199
4200 if (!*bfd_loc)
4201 *bfd_loc = entry;
4202
0a1b45a2 4203 return true;
c224138d 4204}
33bb52fb
RS
4205
4206/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4207
4208static void
4209mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4210 unsigned int n)
4211{
4212 asection *s;
4213 struct mips_elf_link_hash_table *htab;
4214
4215 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4216 BFD_ASSERT (htab != NULL);
4217
0a1b45a2 4218 s = mips_elf_rel_dyn_section (info, false);
33bb52fb
RS
4219 BFD_ASSERT (s != NULL);
4220
90c14f0c 4221 if (htab->root.target_os == is_vxworks)
33bb52fb
RS
4222 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4223 else
4224 {
4225 if (s->size == 0)
4226 {
4227 /* Make room for a null element. */
4228 s->size += MIPS_ELF_REL_SIZE (abfd);
4229 ++s->reloc_count;
4230 }
4231 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4232 }
4233}
4234\f
476366af
RS
4235/* A htab_traverse callback for GOT entries, with DATA pointing to a
4236 mips_elf_traverse_got_arg structure. Count the number of GOT
4237 entries and TLS relocs. Set DATA->value to true if we need
4238 to resolve indirect or warning symbols and then recreate the GOT. */
33bb52fb
RS
4239
4240static int
4241mips_elf_check_recreate_got (void **entryp, void *data)
4242{
4243 struct mips_got_entry *entry;
476366af 4244 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4245
4246 entry = (struct mips_got_entry *) *entryp;
476366af 4247 arg = (struct mips_elf_traverse_got_arg *) data;
33bb52fb
RS
4248 if (entry->abfd != NULL && entry->symndx == -1)
4249 {
4250 struct mips_elf_link_hash_entry *h;
4251
4252 h = entry->d.h;
4253 if (h->root.root.type == bfd_link_hash_indirect
4254 || h->root.root.type == bfd_link_hash_warning)
4255 {
0a1b45a2 4256 arg->value = true;
33bb52fb
RS
4257 return 0;
4258 }
4259 }
476366af 4260 mips_elf_count_got_entry (arg->info, arg->g, entry);
33bb52fb
RS
4261 return 1;
4262}
4263
476366af
RS
4264/* A htab_traverse callback for GOT entries, with DATA pointing to a
4265 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4266 converting entries for indirect and warning symbols into entries
4267 for the target symbol. Set DATA->g to null on error. */
33bb52fb
RS
4268
4269static int
4270mips_elf_recreate_got (void **entryp, void *data)
4271{
72e7511a 4272 struct mips_got_entry new_entry, *entry;
476366af 4273 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4274 void **slot;
4275
33bb52fb 4276 entry = (struct mips_got_entry *) *entryp;
476366af 4277 arg = (struct mips_elf_traverse_got_arg *) data;
72e7511a
RS
4278 if (entry->abfd != NULL
4279 && entry->symndx == -1
4280 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4281 || entry->d.h->root.root.type == bfd_link_hash_warning))
33bb52fb
RS
4282 {
4283 struct mips_elf_link_hash_entry *h;
4284
72e7511a
RS
4285 new_entry = *entry;
4286 entry = &new_entry;
33bb52fb 4287 h = entry->d.h;
72e7511a 4288 do
634835ae
RS
4289 {
4290 BFD_ASSERT (h->global_got_area == GGA_NONE);
4291 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4292 }
72e7511a
RS
4293 while (h->root.root.type == bfd_link_hash_indirect
4294 || h->root.root.type == bfd_link_hash_warning);
33bb52fb
RS
4295 entry->d.h = h;
4296 }
476366af 4297 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
33bb52fb
RS
4298 if (slot == NULL)
4299 {
476366af 4300 arg->g = NULL;
33bb52fb
RS
4301 return 0;
4302 }
4303 if (*slot == NULL)
72e7511a
RS
4304 {
4305 if (entry == &new_entry)
4306 {
4307 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4308 if (!entry)
4309 {
476366af 4310 arg->g = NULL;
72e7511a
RS
4311 return 0;
4312 }
4313 *entry = new_entry;
4314 }
4315 *slot = entry;
476366af 4316 mips_elf_count_got_entry (arg->info, arg->g, entry);
72e7511a 4317 }
33bb52fb
RS
4318 return 1;
4319}
4320
13db6b44
RS
4321/* Return the maximum number of GOT page entries required for RANGE. */
4322
4323static bfd_vma
4324mips_elf_pages_for_range (const struct mips_got_page_range *range)
4325{
4326 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4327}
4328
4329/* Record that G requires a page entry that can reach SEC + ADDEND. */
4330
0a1b45a2 4331static bool
b75d42bc 4332mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
13db6b44
RS
4333 asection *sec, bfd_signed_vma addend)
4334{
b75d42bc 4335 struct mips_got_info *g = arg->g;
13db6b44
RS
4336 struct mips_got_page_entry lookup, *entry;
4337 struct mips_got_page_range **range_ptr, *range;
4338 bfd_vma old_pages, new_pages;
4339 void **loc;
4340
4341 /* Find the mips_got_page_entry hash table entry for this section. */
4342 lookup.sec = sec;
4343 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4344 if (loc == NULL)
0a1b45a2 4345 return false;
13db6b44
RS
4346
4347 /* Create a mips_got_page_entry if this is the first time we've
4348 seen the section. */
4349 entry = (struct mips_got_page_entry *) *loc;
4350 if (!entry)
4351 {
b75d42bc 4352 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
13db6b44 4353 if (!entry)
0a1b45a2 4354 return false;
13db6b44
RS
4355
4356 entry->sec = sec;
4357 *loc = entry;
4358 }
4359
4360 /* Skip over ranges whose maximum extent cannot share a page entry
4361 with ADDEND. */
4362 range_ptr = &entry->ranges;
4363 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4364 range_ptr = &(*range_ptr)->next;
4365
4366 /* If we scanned to the end of the list, or found a range whose
4367 minimum extent cannot share a page entry with ADDEND, create
4368 a new singleton range. */
4369 range = *range_ptr;
4370 if (!range || addend < range->min_addend - 0xffff)
4371 {
b75d42bc 4372 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
13db6b44 4373 if (!range)
0a1b45a2 4374 return false;
13db6b44
RS
4375
4376 range->next = *range_ptr;
4377 range->min_addend = addend;
4378 range->max_addend = addend;
4379
4380 *range_ptr = range;
4381 entry->num_pages++;
4382 g->page_gotno++;
0a1b45a2 4383 return true;
13db6b44
RS
4384 }
4385
4386 /* Remember how many pages the old range contributed. */
4387 old_pages = mips_elf_pages_for_range (range);
4388
4389 /* Update the ranges. */
4390 if (addend < range->min_addend)
4391 range->min_addend = addend;
4392 else if (addend > range->max_addend)
4393 {
4394 if (range->next && addend >= range->next->min_addend - 0xffff)
4395 {
4396 old_pages += mips_elf_pages_for_range (range->next);
4397 range->max_addend = range->next->max_addend;
4398 range->next = range->next->next;
4399 }
4400 else
4401 range->max_addend = addend;
4402 }
4403
4404 /* Record any change in the total estimate. */
4405 new_pages = mips_elf_pages_for_range (range);
4406 if (old_pages != new_pages)
4407 {
4408 entry->num_pages += new_pages - old_pages;
4409 g->page_gotno += new_pages - old_pages;
4410 }
4411
0a1b45a2 4412 return true;
13db6b44
RS
4413}
4414
4415/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4416 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4417 whether the page reference described by *REFP needs a GOT page entry,
4418 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4419
1201fda6 4420static int
13db6b44
RS
4421mips_elf_resolve_got_page_ref (void **refp, void *data)
4422{
4423 struct mips_got_page_ref *ref;
4424 struct mips_elf_traverse_got_arg *arg;
4425 struct mips_elf_link_hash_table *htab;
4426 asection *sec;
4427 bfd_vma addend;
4428
4429 ref = (struct mips_got_page_ref *) *refp;
4430 arg = (struct mips_elf_traverse_got_arg *) data;
4431 htab = mips_elf_hash_table (arg->info);
4432
4433 if (ref->symndx < 0)
4434 {
4435 struct mips_elf_link_hash_entry *h;
4436
4437 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4438 h = ref->u.h;
4439 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4440 return 1;
4441
4442 /* Ignore undefined symbols; we'll issue an error later if
4443 appropriate. */
4444 if (!((h->root.root.type == bfd_link_hash_defined
4445 || h->root.root.type == bfd_link_hash_defweak)
4446 && h->root.root.u.def.section))
4447 return 1;
4448
4449 sec = h->root.root.u.def.section;
4450 addend = h->root.root.u.def.value + ref->addend;
4451 }
4452 else
4453 {
4454 Elf_Internal_Sym *isym;
4455
4456 /* Read in the symbol. */
f1dfbfdb 4457 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
13db6b44
RS
4458 ref->symndx);
4459 if (isym == NULL)
4460 {
4461 arg->g = NULL;
4462 return 0;
4463 }
4464
4465 /* Get the associated input section. */
4466 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4467 if (sec == NULL)
4468 {
4469 arg->g = NULL;
4470 return 0;
4471 }
4472
4473 /* If this is a mergable section, work out the section and offset
4474 of the merged data. For section symbols, the addend specifies
4475 of the offset _of_ the first byte in the data, otherwise it
4476 specifies the offset _from_ the first byte. */
4477 if (sec->flags & SEC_MERGE)
4478 {
4479 void *secinfo;
4480
4481 secinfo = elf_section_data (sec)->sec_info;
4482 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4483 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4484 isym->st_value + ref->addend);
4485 else
4486 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4487 isym->st_value) + ref->addend;
4488 }
4489 else
4490 addend = isym->st_value + ref->addend;
4491 }
b75d42bc 4492 if (!mips_elf_record_got_page_entry (arg, sec, addend))
13db6b44
RS
4493 {
4494 arg->g = NULL;
4495 return 0;
4496 }
4497 return 1;
4498}
4499
33bb52fb 4500/* If any entries in G->got_entries are for indirect or warning symbols,
13db6b44
RS
4501 replace them with entries for the target symbol. Convert g->got_page_refs
4502 into got_page_entry structures and estimate the number of page entries
4503 that they require. */
33bb52fb 4504
0a1b45a2 4505static bool
476366af
RS
4506mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4507 struct mips_got_info *g)
33bb52fb 4508{
476366af
RS
4509 struct mips_elf_traverse_got_arg tga;
4510 struct mips_got_info oldg;
4511
4512 oldg = *g;
33bb52fb 4513
476366af
RS
4514 tga.info = info;
4515 tga.g = g;
0a1b45a2 4516 tga.value = false;
476366af
RS
4517 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4518 if (tga.value)
33bb52fb 4519 {
476366af
RS
4520 *g = oldg;
4521 g->got_entries = htab_create (htab_size (oldg.got_entries),
4522 mips_elf_got_entry_hash,
4523 mips_elf_got_entry_eq, NULL);
4524 if (!g->got_entries)
0a1b45a2 4525 return false;
33bb52fb 4526
476366af
RS
4527 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4528 if (!tga.g)
0a1b45a2 4529 return false;
476366af
RS
4530
4531 htab_delete (oldg.got_entries);
33bb52fb 4532 }
13db6b44
RS
4533
4534 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4535 mips_got_page_entry_eq, NULL);
4536 if (g->got_page_entries == NULL)
0a1b45a2 4537 return false;
13db6b44
RS
4538
4539 tga.info = info;
4540 tga.g = g;
4541 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4542
0a1b45a2 4543 return true;
33bb52fb
RS
4544}
4545
c5d6fa44
RS
4546/* Return true if a GOT entry for H should live in the local rather than
4547 global GOT area. */
4548
0a1b45a2 4549static bool
c5d6fa44
RS
4550mips_use_local_got_p (struct bfd_link_info *info,
4551 struct mips_elf_link_hash_entry *h)
4552{
4553 /* Symbols that aren't in the dynamic symbol table must live in the
4554 local GOT. This includes symbols that are completely undefined
4555 and which therefore don't bind locally. We'll report undefined
4556 symbols later if appropriate. */
4557 if (h->root.dynindx == -1)
0a1b45a2 4558 return true;
c5d6fa44 4559
47275900
MR
4560 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4561 to the local GOT, as they would be implicitly relocated by the
4562 base address by the dynamic loader. */
4563 if (bfd_is_abs_symbol (&h->root.root))
0a1b45a2 4564 return false;
47275900 4565
c5d6fa44
RS
4566 /* Symbols that bind locally can (and in the case of forced-local
4567 symbols, must) live in the local GOT. */
4568 if (h->got_only_for_calls
4569 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4570 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
0a1b45a2 4571 return true;
c5d6fa44
RS
4572
4573 /* If this is an executable that must provide a definition of the symbol,
4574 either though PLTs or copy relocations, then that address should go in
4575 the local rather than global GOT. */
0e1862bb 4576 if (bfd_link_executable (info) && h->has_static_relocs)
0a1b45a2 4577 return true;
c5d6fa44 4578
0a1b45a2 4579 return false;
c5d6fa44
RS
4580}
4581
6c42ddb9
RS
4582/* A mips_elf_link_hash_traverse callback for which DATA points to the
4583 link_info structure. Decide whether the hash entry needs an entry in
4584 the global part of the primary GOT, setting global_got_area accordingly.
4585 Count the number of global symbols that are in the primary GOT only
4586 because they have relocations against them (reloc_only_gotno). */
33bb52fb 4587
0a1b45a2 4588static bool
d4596a51 4589mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 4590{
020d7251 4591 struct bfd_link_info *info;
6ccf4795 4592 struct mips_elf_link_hash_table *htab;
33bb52fb
RS
4593 struct mips_got_info *g;
4594
020d7251 4595 info = (struct bfd_link_info *) data;
6ccf4795
RS
4596 htab = mips_elf_hash_table (info);
4597 g = htab->got_info;
d4596a51 4598 if (h->global_got_area != GGA_NONE)
33bb52fb 4599 {
020d7251 4600 /* Make a final decision about whether the symbol belongs in the
c5d6fa44
RS
4601 local or global GOT. */
4602 if (mips_use_local_got_p (info, h))
6c42ddb9
RS
4603 /* The symbol belongs in the local GOT. We no longer need this
4604 entry if it was only used for relocations; those relocations
4605 will be against the null or section symbol instead of H. */
4606 h->global_got_area = GGA_NONE;
90c14f0c 4607 else if (htab->root.target_os == is_vxworks
6ccf4795 4608 && h->got_only_for_calls
1bbce132 4609 && h->root.plt.plist->mips_offset != MINUS_ONE)
6ccf4795
RS
4610 /* On VxWorks, calls can refer directly to the .got.plt entry;
4611 they don't need entries in the regular GOT. .got.plt entries
4612 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4613 h->global_got_area = GGA_NONE;
6c42ddb9 4614 else if (h->global_got_area == GGA_RELOC_ONLY)
23cc69b6 4615 {
6c42ddb9 4616 g->reloc_only_gotno++;
23cc69b6 4617 g->global_gotno++;
23cc69b6 4618 }
33bb52fb
RS
4619 }
4620 return 1;
4621}
f4416af6 4622\f
d7206569
RS
4623/* A htab_traverse callback for GOT entries. Add each one to the GOT
4624 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
f4416af6
AO
4625
4626static int
d7206569 4627mips_elf_add_got_entry (void **entryp, void *data)
f4416af6 4628{
d7206569
RS
4629 struct mips_got_entry *entry;
4630 struct mips_elf_traverse_got_arg *arg;
4631 void **slot;
f4416af6 4632
d7206569
RS
4633 entry = (struct mips_got_entry *) *entryp;
4634 arg = (struct mips_elf_traverse_got_arg *) data;
4635 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4636 if (!slot)
f4416af6 4637 {
d7206569
RS
4638 arg->g = NULL;
4639 return 0;
f4416af6 4640 }
d7206569 4641 if (!*slot)
c224138d 4642 {
d7206569
RS
4643 *slot = entry;
4644 mips_elf_count_got_entry (arg->info, arg->g, entry);
c224138d 4645 }
f4416af6
AO
4646 return 1;
4647}
4648
d7206569
RS
4649/* A htab_traverse callback for GOT page entries. Add each one to the GOT
4650 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
c224138d
RS
4651
4652static int
d7206569 4653mips_elf_add_got_page_entry (void **entryp, void *data)
c224138d 4654{
d7206569
RS
4655 struct mips_got_page_entry *entry;
4656 struct mips_elf_traverse_got_arg *arg;
4657 void **slot;
c224138d 4658
d7206569
RS
4659 entry = (struct mips_got_page_entry *) *entryp;
4660 arg = (struct mips_elf_traverse_got_arg *) data;
4661 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4662 if (!slot)
c224138d 4663 {
d7206569 4664 arg->g = NULL;
c224138d
RS
4665 return 0;
4666 }
d7206569
RS
4667 if (!*slot)
4668 {
4669 *slot = entry;
4670 arg->g->page_gotno += entry->num_pages;
4671 }
c224138d
RS
4672 return 1;
4673}
4674
d7206569
RS
4675/* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4676 this would lead to overflow, 1 if they were merged successfully,
4677 and 0 if a merge failed due to lack of memory. (These values are chosen
4678 so that nonnegative return values can be returned by a htab_traverse
4679 callback.) */
c224138d
RS
4680
4681static int
d7206569 4682mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
c224138d
RS
4683 struct mips_got_info *to,
4684 struct mips_elf_got_per_bfd_arg *arg)
4685{
d7206569 4686 struct mips_elf_traverse_got_arg tga;
c224138d
RS
4687 unsigned int estimate;
4688
4689 /* Work out how many page entries we would need for the combined GOT. */
4690 estimate = arg->max_pages;
4691 if (estimate >= from->page_gotno + to->page_gotno)
4692 estimate = from->page_gotno + to->page_gotno;
4693
e2ece73c 4694 /* And conservatively estimate how many local and TLS entries
c224138d 4695 would be needed. */
e2ece73c
RS
4696 estimate += from->local_gotno + to->local_gotno;
4697 estimate += from->tls_gotno + to->tls_gotno;
4698
17214937
RS
4699 /* If we're merging with the primary got, any TLS relocations will
4700 come after the full set of global entries. Otherwise estimate those
e2ece73c 4701 conservatively as well. */
17214937 4702 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
e2ece73c
RS
4703 estimate += arg->global_count;
4704 else
4705 estimate += from->global_gotno + to->global_gotno;
c224138d
RS
4706
4707 /* Bail out if the combined GOT might be too big. */
4708 if (estimate > arg->max_count)
4709 return -1;
4710
c224138d 4711 /* Transfer the bfd's got information from FROM to TO. */
d7206569
RS
4712 tga.info = arg->info;
4713 tga.g = to;
4714 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4715 if (!tga.g)
c224138d
RS
4716 return 0;
4717
d7206569
RS
4718 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4719 if (!tga.g)
c224138d
RS
4720 return 0;
4721
d7206569 4722 mips_elf_replace_bfd_got (abfd, to);
c224138d
RS
4723 return 1;
4724}
4725
d7206569 4726/* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
f4416af6
AO
4727 as possible of the primary got, since it doesn't require explicit
4728 dynamic relocations, but don't use bfds that would reference global
4729 symbols out of the addressable range. Failing the primary got,
4730 attempt to merge with the current got, or finish the current got
4731 and then make make the new got current. */
4732
0a1b45a2 4733static bool
d7206569
RS
4734mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4735 struct mips_elf_got_per_bfd_arg *arg)
f4416af6 4736{
c224138d
RS
4737 unsigned int estimate;
4738 int result;
4739
476366af 4740 if (!mips_elf_resolve_final_got_entries (arg->info, g))
0a1b45a2 4741 return false;
d7206569 4742
c224138d
RS
4743 /* Work out the number of page, local and TLS entries. */
4744 estimate = arg->max_pages;
4745 if (estimate > g->page_gotno)
4746 estimate = g->page_gotno;
4747 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4748
4749 /* We place TLS GOT entries after both locals and globals. The globals
4750 for the primary GOT may overflow the normal GOT size limit, so be
4751 sure not to merge a GOT which requires TLS with the primary GOT in that
4752 case. This doesn't affect non-primary GOTs. */
c224138d 4753 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4754
c224138d 4755 if (estimate <= arg->max_count)
f4416af6 4756 {
c224138d
RS
4757 /* If we don't have a primary GOT, use it as
4758 a starting point for the primary GOT. */
4759 if (!arg->primary)
4760 {
d7206569 4761 arg->primary = g;
0a1b45a2 4762 return true;
c224138d 4763 }
f4416af6 4764
c224138d 4765 /* Try merging with the primary GOT. */
d7206569 4766 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
c224138d
RS
4767 if (result >= 0)
4768 return result;
f4416af6 4769 }
c224138d 4770
f4416af6 4771 /* If we can merge with the last-created got, do it. */
c224138d 4772 if (arg->current)
f4416af6 4773 {
d7206569 4774 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
c224138d
RS
4775 if (result >= 0)
4776 return result;
f4416af6 4777 }
c224138d 4778
f4416af6
AO
4779 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4780 fits; if it turns out that it doesn't, we'll get relocation
4781 overflows anyway. */
c224138d
RS
4782 g->next = arg->current;
4783 arg->current = g;
0f20cc35 4784
0a1b45a2 4785 return true;
0f20cc35
DJ
4786}
4787
72e7511a
RS
4788/* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4789 to GOTIDX, duplicating the entry if it has already been assigned
4790 an index in a different GOT. */
4791
0a1b45a2 4792static bool
72e7511a
RS
4793mips_elf_set_gotidx (void **entryp, long gotidx)
4794{
4795 struct mips_got_entry *entry;
4796
4797 entry = (struct mips_got_entry *) *entryp;
4798 if (entry->gotidx > 0)
4799 {
4800 struct mips_got_entry *new_entry;
4801
4802 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4803 if (!new_entry)
0a1b45a2 4804 return false;
72e7511a
RS
4805
4806 *new_entry = *entry;
4807 *entryp = new_entry;
4808 entry = new_entry;
4809 }
4810 entry->gotidx = gotidx;
0a1b45a2 4811 return true;
72e7511a
RS
4812}
4813
4814/* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4815 mips_elf_traverse_got_arg in which DATA->value is the size of one
4816 GOT entry. Set DATA->g to null on failure. */
0f20cc35
DJ
4817
4818static int
72e7511a 4819mips_elf_initialize_tls_index (void **entryp, void *data)
0f20cc35 4820{
72e7511a
RS
4821 struct mips_got_entry *entry;
4822 struct mips_elf_traverse_got_arg *arg;
0f20cc35
DJ
4823
4824 /* We're only interested in TLS symbols. */
72e7511a 4825 entry = (struct mips_got_entry *) *entryp;
9ab066b4 4826 if (entry->tls_type == GOT_TLS_NONE)
0f20cc35
DJ
4827 return 1;
4828
72e7511a 4829 arg = (struct mips_elf_traverse_got_arg *) data;
6c42ddb9 4830 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
ead49a57 4831 {
6c42ddb9
RS
4832 arg->g = NULL;
4833 return 0;
f4416af6
AO
4834 }
4835
ead49a57 4836 /* Account for the entries we've just allocated. */
9ab066b4 4837 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
f4416af6
AO
4838 return 1;
4839}
4840
ab361d49
RS
4841/* A htab_traverse callback for GOT entries, where DATA points to a
4842 mips_elf_traverse_got_arg. Set the global_got_area of each global
4843 symbol to DATA->value. */
f4416af6 4844
f4416af6 4845static int
ab361d49 4846mips_elf_set_global_got_area (void **entryp, void *data)
f4416af6 4847{
ab361d49
RS
4848 struct mips_got_entry *entry;
4849 struct mips_elf_traverse_got_arg *arg;
f4416af6 4850
ab361d49
RS
4851 entry = (struct mips_got_entry *) *entryp;
4852 arg = (struct mips_elf_traverse_got_arg *) data;
4853 if (entry->abfd != NULL
4854 && entry->symndx == -1
4855 && entry->d.h->global_got_area != GGA_NONE)
4856 entry->d.h->global_got_area = arg->value;
4857 return 1;
4858}
4859
4860/* A htab_traverse callback for secondary GOT entries, where DATA points
4861 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4862 and record the number of relocations they require. DATA->value is
72e7511a 4863 the size of one GOT entry. Set DATA->g to null on failure. */
ab361d49
RS
4864
4865static int
4866mips_elf_set_global_gotidx (void **entryp, void *data)
4867{
4868 struct mips_got_entry *entry;
4869 struct mips_elf_traverse_got_arg *arg;
0f20cc35 4870
ab361d49
RS
4871 entry = (struct mips_got_entry *) *entryp;
4872 arg = (struct mips_elf_traverse_got_arg *) data;
634835ae
RS
4873 if (entry->abfd != NULL
4874 && entry->symndx == -1
4875 && entry->d.h->global_got_area != GGA_NONE)
f4416af6 4876 {
cb22ccf4 4877 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
72e7511a
RS
4878 {
4879 arg->g = NULL;
4880 return 0;
4881 }
cb22ccf4 4882 arg->g->assigned_low_gotno += 1;
72e7511a 4883
0e1862bb 4884 if (bfd_link_pic (arg->info)
ab361d49
RS
4885 || (elf_hash_table (arg->info)->dynamic_sections_created
4886 && entry->d.h->root.def_dynamic
4887 && !entry->d.h->root.def_regular))
4888 arg->g->relocs += 1;
f4416af6
AO
4889 }
4890
4891 return 1;
4892}
4893
33bb52fb
RS
4894/* A htab_traverse callback for GOT entries for which DATA is the
4895 bfd_link_info. Forbid any global symbols from having traditional
4896 lazy-binding stubs. */
4897
0626d451 4898static int
33bb52fb 4899mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4900{
33bb52fb
RS
4901 struct bfd_link_info *info;
4902 struct mips_elf_link_hash_table *htab;
4903 struct mips_got_entry *entry;
0626d451 4904
33bb52fb
RS
4905 entry = (struct mips_got_entry *) *entryp;
4906 info = (struct bfd_link_info *) data;
4907 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4908 BFD_ASSERT (htab != NULL);
4909
0626d451
RS
4910 if (entry->abfd != NULL
4911 && entry->symndx == -1
33bb52fb 4912 && entry->d.h->needs_lazy_stub)
f4416af6 4913 {
0a1b45a2 4914 entry->d.h->needs_lazy_stub = false;
33bb52fb 4915 htab->lazy_stub_count--;
f4416af6 4916 }
143d77c5 4917
f4416af6
AO
4918 return 1;
4919}
4920
f4416af6
AO
4921/* Return the offset of an input bfd IBFD's GOT from the beginning of
4922 the primary GOT. */
4923static bfd_vma
9719ad41 4924mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6 4925{
d7206569 4926 if (!g->next)
f4416af6
AO
4927 return 0;
4928
0a1b45a2 4929 g = mips_elf_bfd_got (ibfd, false);
f4416af6
AO
4930 if (! g)
4931 return 0;
4932
4933 BFD_ASSERT (g->next);
4934
4935 g = g->next;
143d77c5 4936
0f20cc35
DJ
4937 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4938 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4939}
4940
4941/* Turn a single GOT that is too big for 16-bit addressing into
4942 a sequence of GOTs, each one 16-bit addressable. */
4943
0a1b45a2 4944static bool
9719ad41 4945mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4946 asection *got, bfd_size_type pages)
f4416af6 4947{
a8028dd0 4948 struct mips_elf_link_hash_table *htab;
f4416af6 4949 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
ab361d49 4950 struct mips_elf_traverse_got_arg tga;
a8028dd0 4951 struct mips_got_info *g, *gg;
33bb52fb 4952 unsigned int assign, needed_relocs;
d7206569 4953 bfd *dynobj, *ibfd;
f4416af6 4954
33bb52fb 4955 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4956 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4957 BFD_ASSERT (htab != NULL);
4958
a8028dd0 4959 g = htab->got_info;
f4416af6 4960
f4416af6
AO
4961 got_per_bfd_arg.obfd = abfd;
4962 got_per_bfd_arg.info = info;
f4416af6
AO
4963 got_per_bfd_arg.current = NULL;
4964 got_per_bfd_arg.primary = NULL;
0a44bf69 4965 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4966 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4967 - htab->reserved_gotno);
c224138d 4968 got_per_bfd_arg.max_pages = pages;
0f20cc35 4969 /* The number of globals that will be included in the primary GOT.
ab361d49 4970 See the calls to mips_elf_set_global_got_area below for more
0f20cc35
DJ
4971 information. */
4972 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4973
4974 /* Try to merge the GOTs of input bfds together, as long as they
4975 don't seem to exceed the maximum GOT size, choosing one of them
4976 to be the primary GOT. */
c72f2fb2 4977 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
d7206569 4978 {
0a1b45a2 4979 gg = mips_elf_bfd_got (ibfd, false);
d7206569 4980 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
0a1b45a2 4981 return false;
d7206569 4982 }
f4416af6 4983
0f20cc35 4984 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6 4985 if (got_per_bfd_arg.primary == NULL)
3dff0dd1 4986 g->next = mips_elf_create_got_info (abfd);
f4416af6
AO
4987 else
4988 g->next = got_per_bfd_arg.primary;
4989 g->next->next = got_per_bfd_arg.current;
4990
4991 /* GG is now the master GOT, and G is the primary GOT. */
4992 gg = g;
4993 g = g->next;
4994
4995 /* Map the output bfd to the primary got. That's what we're going
4996 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4997 didn't mark in check_relocs, and we want a quick way to find it.
4998 We can't just use gg->next because we're going to reverse the
4999 list. */
d7206569 5000 mips_elf_replace_bfd_got (abfd, g);
f4416af6 5001
634835ae
RS
5002 /* Every symbol that is referenced in a dynamic relocation must be
5003 present in the primary GOT, so arrange for them to appear after
5004 those that are actually referenced. */
23cc69b6 5005 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 5006 g->global_gotno = gg->global_gotno;
f4416af6 5007
ab361d49
RS
5008 tga.info = info;
5009 tga.value = GGA_RELOC_ONLY;
5010 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
5011 tga.value = GGA_NORMAL;
5012 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
f4416af6
AO
5013
5014 /* Now go through the GOTs assigning them offset ranges.
cb22ccf4 5015 [assigned_low_gotno, local_gotno[ will be set to the range of local
f4416af6
AO
5016 entries in each GOT. We can then compute the end of a GOT by
5017 adding local_gotno to global_gotno. We reverse the list and make
5018 it circular since then we'll be able to quickly compute the
5019 beginning of a GOT, by computing the end of its predecessor. To
5020 avoid special cases for the primary GOT, while still preserving
5021 assertions that are valid for both single- and multi-got links,
5022 we arrange for the main got struct to have the right number of
5023 global entries, but set its local_gotno such that the initial
5024 offset of the primary GOT is zero. Remember that the primary GOT
5025 will become the last item in the circular linked list, so it
5026 points back to the master GOT. */
5027 gg->local_gotno = -g->global_gotno;
5028 gg->global_gotno = g->global_gotno;
0f20cc35 5029 gg->tls_gotno = 0;
f4416af6
AO
5030 assign = 0;
5031 gg->next = gg;
5032
5033 do
5034 {
5035 struct mips_got_info *gn;
5036
861fb55a 5037 assign += htab->reserved_gotno;
cb22ccf4 5038 g->assigned_low_gotno = assign;
c224138d
RS
5039 g->local_gotno += assign;
5040 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
cb22ccf4 5041 g->assigned_high_gotno = g->local_gotno - 1;
0f20cc35
DJ
5042 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
5043
ead49a57
RS
5044 /* Take g out of the direct list, and push it onto the reversed
5045 list that gg points to. g->next is guaranteed to be nonnull after
5046 this operation, as required by mips_elf_initialize_tls_index. */
5047 gn = g->next;
5048 g->next = gg->next;
5049 gg->next = g;
5050
0f20cc35
DJ
5051 /* Set up any TLS entries. We always place the TLS entries after
5052 all non-TLS entries. */
5053 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
72e7511a
RS
5054 tga.g = g;
5055 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5056 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5057 if (!tga.g)
0a1b45a2 5058 return false;
1fd20d70 5059 BFD_ASSERT (g->tls_assigned_gotno == assign);
f4416af6 5060
ead49a57 5061 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 5062 g = gn;
0626d451 5063
33bb52fb
RS
5064 /* Forbid global symbols in every non-primary GOT from having
5065 lazy-binding stubs. */
0626d451 5066 if (g)
33bb52fb 5067 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
5068 }
5069 while (g);
5070
59b08994 5071 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
33bb52fb
RS
5072
5073 needed_relocs = 0;
33bb52fb
RS
5074 for (g = gg->next; g && g->next != gg; g = g->next)
5075 {
5076 unsigned int save_assign;
5077
ab361d49
RS
5078 /* Assign offsets to global GOT entries and count how many
5079 relocations they need. */
cb22ccf4
KCY
5080 save_assign = g->assigned_low_gotno;
5081 g->assigned_low_gotno = g->local_gotno;
ab361d49
RS
5082 tga.info = info;
5083 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5084 tga.g = g;
5085 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
72e7511a 5086 if (!tga.g)
0a1b45a2 5087 return false;
cb22ccf4
KCY
5088 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5089 g->assigned_low_gotno = save_assign;
72e7511a 5090
0e1862bb 5091 if (bfd_link_pic (info))
33bb52fb 5092 {
cb22ccf4
KCY
5093 g->relocs += g->local_gotno - g->assigned_low_gotno;
5094 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
33bb52fb
RS
5095 + g->next->global_gotno
5096 + g->next->tls_gotno
861fb55a 5097 + htab->reserved_gotno);
33bb52fb 5098 }
ab361d49 5099 needed_relocs += g->relocs;
33bb52fb 5100 }
ab361d49 5101 needed_relocs += g->relocs;
33bb52fb
RS
5102
5103 if (needed_relocs)
5104 mips_elf_allocate_dynamic_relocations (dynobj, info,
5105 needed_relocs);
143d77c5 5106
0a1b45a2 5107 return true;
f4416af6 5108}
143d77c5 5109
b49e97c9
TS
5110\f
5111/* Returns the first relocation of type r_type found, beginning with
5112 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5113
5114static const Elf_Internal_Rela *
9719ad41
RS
5115mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5116 const Elf_Internal_Rela *relocation,
5117 const Elf_Internal_Rela *relend)
b49e97c9 5118{
c000e262
TS
5119 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5120
b49e97c9
TS
5121 while (relocation < relend)
5122 {
c000e262
TS
5123 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5124 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
5125 return relocation;
5126
5127 ++relocation;
5128 }
5129
5130 /* We didn't find it. */
b49e97c9
TS
5131 return NULL;
5132}
5133
020d7251 5134/* Return whether an input relocation is against a local symbol. */
b49e97c9 5135
0a1b45a2 5136static bool
9719ad41
RS
5137mips_elf_local_relocation_p (bfd *input_bfd,
5138 const Elf_Internal_Rela *relocation,
020d7251 5139 asection **local_sections)
b49e97c9
TS
5140{
5141 unsigned long r_symndx;
5142 Elf_Internal_Shdr *symtab_hdr;
b49e97c9
TS
5143 size_t extsymoff;
5144
5145 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5146 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5147 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5148
5149 if (r_symndx < extsymoff)
0a1b45a2 5150 return true;
b49e97c9 5151 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
0a1b45a2 5152 return true;
b49e97c9 5153
0a1b45a2 5154 return false;
b49e97c9
TS
5155}
5156\f
5157/* Sign-extend VALUE, which has the indicated number of BITS. */
5158
a7ebbfdf 5159bfd_vma
9719ad41 5160_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
5161{
5162 if (value & ((bfd_vma) 1 << (bits - 1)))
5163 /* VALUE is negative. */
5164 value |= ((bfd_vma) - 1) << bits;
5165
5166 return value;
5167}
5168
5169/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 5170 range expressible by a signed number with the indicated number of
b49e97c9
TS
5171 BITS. */
5172
0a1b45a2 5173static bool
9719ad41 5174mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
5175{
5176 bfd_signed_vma svalue = (bfd_signed_vma) value;
5177
5178 if (svalue > (1 << (bits - 1)) - 1)
5179 /* The value is too big. */
0a1b45a2 5180 return true;
b49e97c9
TS
5181 else if (svalue < -(1 << (bits - 1)))
5182 /* The value is too small. */
0a1b45a2 5183 return true;
b49e97c9
TS
5184
5185 /* All is well. */
0a1b45a2 5186 return false;
b49e97c9
TS
5187}
5188
5189/* Calculate the %high function. */
5190
5191static bfd_vma
9719ad41 5192mips_elf_high (bfd_vma value)
b49e97c9
TS
5193{
5194 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5195}
5196
5197/* Calculate the %higher function. */
5198
5199static bfd_vma
9719ad41 5200mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
5201{
5202#ifdef BFD64
5203 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5204#else
5205 abort ();
c5ae1840 5206 return MINUS_ONE;
b49e97c9
TS
5207#endif
5208}
5209
5210/* Calculate the %highest function. */
5211
5212static bfd_vma
9719ad41 5213mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
5214{
5215#ifdef BFD64
b15e6682 5216 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
5217#else
5218 abort ();
c5ae1840 5219 return MINUS_ONE;
b49e97c9
TS
5220#endif
5221}
5222\f
5223/* Create the .compact_rel section. */
5224
0a1b45a2 5225static bool
9719ad41
RS
5226mips_elf_create_compact_rel_section
5227 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
5228{
5229 flagword flags;
5230 register asection *s;
5231
3d4d4302 5232 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
b49e97c9
TS
5233 {
5234 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5235 | SEC_READONLY);
5236
3d4d4302 5237 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
b49e97c9 5238 if (s == NULL
fd361982 5239 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
0a1b45a2 5240 return false;
b49e97c9 5241
eea6121a 5242 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
5243 }
5244
0a1b45a2 5245 return true;
b49e97c9
TS
5246}
5247
5248/* Create the .got section to hold the global offset table. */
5249
0a1b45a2 5250static bool
23cc69b6 5251mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
5252{
5253 flagword flags;
5254 register asection *s;
5255 struct elf_link_hash_entry *h;
14a793b2 5256 struct bfd_link_hash_entry *bh;
0a44bf69
RS
5257 struct mips_elf_link_hash_table *htab;
5258
5259 htab = mips_elf_hash_table (info);
4dfe6ac6 5260 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5261
5262 /* This function may be called more than once. */
ce558b89 5263 if (htab->root.sgot)
0a1b45a2 5264 return true;
b49e97c9
TS
5265
5266 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5267 | SEC_LINKER_CREATED);
5268
72b4917c
TS
5269 /* We have to use an alignment of 2**4 here because this is hardcoded
5270 in the function stub generation and in the linker script. */
87e0a731 5271 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
b49e97c9 5272 if (s == NULL
fd361982 5273 || !bfd_set_section_alignment (s, 4))
0a1b45a2 5274 return false;
ce558b89 5275 htab->root.sgot = s;
b49e97c9
TS
5276
5277 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5278 linker script because we don't want to define the symbol if we
5279 are not creating a global offset table. */
14a793b2 5280 bh = NULL;
b49e97c9
TS
5281 if (! (_bfd_generic_link_add_one_symbol
5282 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
0a1b45a2
AM
5283 0, NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
5284 return false;
14a793b2
AM
5285
5286 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5287 h->non_elf = 0;
5288 h->def_regular = 1;
b49e97c9 5289 h->type = STT_OBJECT;
2f9efdfc 5290 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d329bcd1 5291 elf_hash_table (info)->hgot = h;
b49e97c9 5292
0e1862bb 5293 if (bfd_link_pic (info)
c152c796 5294 && ! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 5295 return false;
b49e97c9 5296
3dff0dd1 5297 htab->got_info = mips_elf_create_got_info (abfd);
f0abc2a1 5298 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
5299 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5300
861fb55a 5301 /* We also need a .got.plt section when generating PLTs. */
87e0a731
AM
5302 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5303 SEC_ALLOC | SEC_LOAD
5304 | SEC_HAS_CONTENTS
5305 | SEC_IN_MEMORY
5306 | SEC_LINKER_CREATED);
861fb55a 5307 if (s == NULL)
0a1b45a2 5308 return false;
ce558b89 5309 htab->root.sgotplt = s;
0a44bf69 5310
0a1b45a2 5311 return true;
b49e97c9 5312}
b49e97c9 5313\f
0a44bf69
RS
5314/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5315 __GOTT_INDEX__ symbols. These symbols are only special for
5316 shared objects; they are not used in executables. */
5317
0a1b45a2 5318static bool
0a44bf69
RS
5319is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5320{
90c14f0c 5321 return (mips_elf_hash_table (info)->root.target_os == is_vxworks
0e1862bb 5322 && bfd_link_pic (info)
0a44bf69
RS
5323 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5324 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5325}
861fb55a
DJ
5326
5327/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5328 require an la25 stub. See also mips_elf_local_pic_function_p,
5329 which determines whether the destination function ever requires a
5330 stub. */
5331
0a1b45a2 5332static bool
8f0c309a 5333mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
0a1b45a2 5334 bool target_is_16_bit_code_p)
861fb55a
DJ
5335{
5336 /* We specifically ignore branches and jumps from EF_PIC objects,
5337 where the onus is on the compiler or programmer to perform any
5338 necessary initialization of $25. Sometimes such initialization
5339 is unnecessary; for example, -mno-shared functions do not use
5340 the incoming value of $25, and may therefore be called directly. */
5341 if (PIC_OBJECT_P (input_bfd))
0a1b45a2 5342 return false;
861fb55a
DJ
5343
5344 switch (r_type)
5345 {
5346 case R_MIPS_26:
5347 case R_MIPS_PC16:
7361da2c
AB
5348 case R_MIPS_PC21_S2:
5349 case R_MIPS_PC26_S2:
df58fc94
RS
5350 case R_MICROMIPS_26_S1:
5351 case R_MICROMIPS_PC7_S1:
5352 case R_MICROMIPS_PC10_S1:
5353 case R_MICROMIPS_PC16_S1:
5354 case R_MICROMIPS_PC23_S2:
0a1b45a2 5355 return true;
861fb55a 5356
8f0c309a
CLT
5357 case R_MIPS16_26:
5358 return !target_is_16_bit_code_p;
5359
861fb55a 5360 default:
0a1b45a2 5361 return false;
861fb55a
DJ
5362 }
5363}
0a44bf69 5364\f
47275900
MR
5365/* Obtain the field relocated by RELOCATION. */
5366
5367static bfd_vma
5368mips_elf_obtain_contents (reloc_howto_type *howto,
5369 const Elf_Internal_Rela *relocation,
5370 bfd *input_bfd, bfd_byte *contents)
5371{
5372 bfd_vma x = 0;
5373 bfd_byte *location = contents + relocation->r_offset;
5374 unsigned int size = bfd_get_reloc_size (howto);
5375
5376 /* Obtain the bytes. */
5377 if (size != 0)
5378 x = bfd_get (8 * size, input_bfd, location);
5379
5380 return x;
5381}
5382
98e10ffa
MR
5383/* Store the field relocated by RELOCATION. */
5384
5385static void
5386mips_elf_store_contents (reloc_howto_type *howto,
5387 const Elf_Internal_Rela *relocation,
5388 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5389{
5390 bfd_byte *location = contents + relocation->r_offset;
5391 unsigned int size = bfd_get_reloc_size (howto);
5392
5393 /* Put the value into the output. */
5394 if (size != 0)
5395 bfd_put (8 * size, input_bfd, x, location);
5396}
5397
47275900
MR
5398/* Try to patch a load from GOT instruction in CONTENTS pointed to by
5399 RELOCATION described by HOWTO, with a move of 0 to the load target
5400 register, returning TRUE if that is successful and FALSE otherwise.
5401 If DOIT is FALSE, then only determine it patching is possible and
5402 return status without actually changing CONTENTS.
5403*/
5404
0a1b45a2 5405static bool
47275900
MR
5406mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5407 const Elf_Internal_Rela *relocation,
0a1b45a2 5408 reloc_howto_type *howto, bool doit)
47275900
MR
5409{
5410 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5411 bfd_byte *location = contents + relocation->r_offset;
0a1b45a2 5412 bool nullified = true;
47275900
MR
5413 bfd_vma x;
5414
0a1b45a2 5415 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
47275900
MR
5416
5417 /* Obtain the current value. */
5418 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5419
5420 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5421 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5422 if (mips16_reloc_p (r_type)
5423 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5424 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
227d539b 5425 x = (0x3cdU << 22) | (x & (7 << 16)) << 3; /* LI */
47275900
MR
5426 else if (micromips_reloc_p (r_type)
5427 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5428 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5429 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5430 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5431 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5432 else
0a1b45a2 5433 nullified = false;
47275900
MR
5434
5435 /* Put the value into the output. */
5436 if (doit && nullified)
5437 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5438
0a1b45a2 5439 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, false, location);
47275900
MR
5440
5441 return nullified;
5442}
5443
b49e97c9
TS
5444/* Calculate the value produced by the RELOCATION (which comes from
5445 the INPUT_BFD). The ADDEND is the addend to use for this
5446 RELOCATION; RELOCATION->R_ADDEND is ignored.
5447
5448 The result of the relocation calculation is stored in VALUEP.
38a7df63 5449 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
df58fc94 5450 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9
TS
5451
5452 This function returns bfd_reloc_continue if the caller need take no
5453 further action regarding this relocation, bfd_reloc_notsupported if
5454 something goes dramatically wrong, bfd_reloc_overflow if an
5455 overflow occurs, and bfd_reloc_ok to indicate success. */
5456
5457static bfd_reloc_status_type
9719ad41 5458mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
47275900 5459 asection *input_section, bfd_byte *contents,
9719ad41
RS
5460 struct bfd_link_info *info,
5461 const Elf_Internal_Rela *relocation,
5462 bfd_vma addend, reloc_howto_type *howto,
5463 Elf_Internal_Sym *local_syms,
5464 asection **local_sections, bfd_vma *valuep,
38a7df63 5465 const char **namep,
0a1b45a2
AM
5466 bool *cross_mode_jump_p,
5467 bool save_addend)
b49e97c9
TS
5468{
5469 /* The eventual value we will return. */
5470 bfd_vma value;
5471 /* The address of the symbol against which the relocation is
5472 occurring. */
5473 bfd_vma symbol = 0;
5474 /* The final GP value to be used for the relocatable, executable, or
5475 shared object file being produced. */
0a61c8c2 5476 bfd_vma gp;
b49e97c9
TS
5477 /* The place (section offset or address) of the storage unit being
5478 relocated. */
5479 bfd_vma p;
5480 /* The value of GP used to create the relocatable object. */
0a61c8c2 5481 bfd_vma gp0;
b49e97c9
TS
5482 /* The offset into the global offset table at which the address of
5483 the relocation entry symbol, adjusted by the addend, resides
5484 during execution. */
5485 bfd_vma g = MINUS_ONE;
5486 /* The section in which the symbol referenced by the relocation is
5487 located. */
5488 asection *sec = NULL;
5489 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 5490 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 5491 symbol. */
0a1b45a2 5492 bool local_p, was_local_p;
77434823
MR
5493 /* TRUE if the symbol referred to by this relocation is a section
5494 symbol. */
0a1b45a2 5495 bool section_p = false;
b34976b6 5496 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
0a1b45a2 5497 bool gp_disp_p = false;
bbe506e8
TS
5498 /* TRUE if the symbol referred to by this relocation is
5499 "__gnu_local_gp". */
0a1b45a2 5500 bool gnu_local_gp_p = false;
b49e97c9
TS
5501 Elf_Internal_Shdr *symtab_hdr;
5502 size_t extsymoff;
5503 unsigned long r_symndx;
5504 int r_type;
b34976b6 5505 /* TRUE if overflow occurred during the calculation of the
b49e97c9 5506 relocation value. */
0a1b45a2 5507 bool overflowed_p;
b34976b6 5508 /* TRUE if this relocation refers to a MIPS16 function. */
0a1b45a2
AM
5509 bool target_is_16_bit_code_p = false;
5510 bool target_is_micromips_code_p = false;
0a44bf69
RS
5511 struct mips_elf_link_hash_table *htab;
5512 bfd *dynobj;
0a1b45a2 5513 bool resolved_to_zero;
0a44bf69
RS
5514
5515 dynobj = elf_hash_table (info)->dynobj;
5516 htab = mips_elf_hash_table (info);
4dfe6ac6 5517 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5518
5519 /* Parse the relocation. */
5520 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5521 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5522 p = (input_section->output_section->vma
5523 + input_section->output_offset
5524 + relocation->r_offset);
5525
5526 /* Assume that there will be no overflow. */
0a1b45a2 5527 overflowed_p = false;
b49e97c9
TS
5528
5529 /* Figure out whether or not the symbol is local, and get the offset
5530 used in the array of hash table entries. */
5531 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5532 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
020d7251 5533 local_sections);
bce03d3d 5534 was_local_p = local_p;
b49e97c9
TS
5535 if (! elf_bad_symtab (input_bfd))
5536 extsymoff = symtab_hdr->sh_info;
5537 else
5538 {
5539 /* The symbol table does not follow the rule that local symbols
5540 must come before globals. */
5541 extsymoff = 0;
5542 }
5543
5544 /* Figure out the value of the symbol. */
5545 if (local_p)
5546 {
0a1b45a2 5547 bool micromips_p = MICROMIPS_P (abfd);
b49e97c9
TS
5548 Elf_Internal_Sym *sym;
5549
5550 sym = local_syms + r_symndx;
5551 sec = local_sections[r_symndx];
5552
77434823
MR
5553 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5554
b49e97c9 5555 symbol = sec->output_section->vma + sec->output_offset;
77434823 5556 if (!section_p || (sec->flags & SEC_MERGE))
b49e97c9 5557 symbol += sym->st_value;
77434823 5558 if ((sec->flags & SEC_MERGE) && section_p)
d4df96e6
L
5559 {
5560 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5561 addend -= symbol;
5562 addend += sec->output_section->vma + sec->output_offset;
5563 }
b49e97c9 5564
df58fc94
RS
5565 /* MIPS16/microMIPS text labels should be treated as odd. */
5566 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
5567 ++symbol;
5568
5569 /* Record the name of this symbol, for our caller. */
5570 *namep = bfd_elf_string_from_elf_section (input_bfd,
5571 symtab_hdr->sh_link,
5572 sym->st_name);
ceab86af 5573 if (*namep == NULL || **namep == '\0')
fd361982 5574 *namep = bfd_section_name (sec);
b49e97c9 5575
9d862524 5576 /* For relocations against a section symbol and ones against no
07d6d2b8 5577 symbol (absolute relocations) infer the ISA mode from the addend. */
9d862524
MR
5578 if (section_p || r_symndx == STN_UNDEF)
5579 {
5580 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5581 target_is_micromips_code_p = (addend & 1) && micromips_p;
5582 }
5583 /* For relocations against an absolute symbol infer the ISA mode
07d6d2b8 5584 from the value of the symbol plus addend. */
9d862524
MR
5585 else if (bfd_is_abs_section (sec))
5586 {
5587 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5588 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5589 }
5590 /* Otherwise just use the regular symbol annotation available. */
5591 else
5592 {
5593 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5594 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5595 }
b49e97c9
TS
5596 }
5597 else
5598 {
560e09e9
NC
5599 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5600
b49e97c9
TS
5601 /* For global symbols we look up the symbol in the hash-table. */
5602 h = ((struct mips_elf_link_hash_entry *)
5603 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5604 /* Find the real hash-table entry for this symbol. */
5605 while (h->root.root.type == bfd_link_hash_indirect
5606 || h->root.root.type == bfd_link_hash_warning)
5607 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5608
5609 /* Record the name of this symbol, for our caller. */
5610 *namep = h->root.root.root.string;
5611
5612 /* See if this is the special _gp_disp symbol. Note that such a
5613 symbol must always be a global symbol. */
560e09e9 5614 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
5615 && ! NEWABI_P (input_bfd))
5616 {
5617 /* Relocations against _gp_disp are permitted only with
5618 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 5619 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
5620 return bfd_reloc_notsupported;
5621
0a1b45a2 5622 gp_disp_p = true;
b49e97c9 5623 }
bbe506e8
TS
5624 /* See if this is the special _gp symbol. Note that such a
5625 symbol must always be a global symbol. */
5626 else if (strcmp (*namep, "__gnu_local_gp") == 0)
0a1b45a2 5627 gnu_local_gp_p = true;
bbe506e8
TS
5628
5629
b49e97c9
TS
5630 /* If this symbol is defined, calculate its address. Note that
5631 _gp_disp is a magic symbol, always implicitly defined by the
5632 linker, so it's inappropriate to check to see whether or not
5633 its defined. */
5634 else if ((h->root.root.type == bfd_link_hash_defined
5635 || h->root.root.type == bfd_link_hash_defweak)
5636 && h->root.root.u.def.section)
5637 {
5638 sec = h->root.root.u.def.section;
5639 if (sec->output_section)
5640 symbol = (h->root.root.u.def.value
5641 + sec->output_section->vma
5642 + sec->output_offset);
5643 else
5644 symbol = h->root.root.u.def.value;
5645 }
5646 else if (h->root.root.type == bfd_link_hash_undefweak)
5647 /* We allow relocations against undefined weak symbols, giving
5648 it the value zero, so that you can undefined weak functions
5649 and check to see if they exist by looking at their
5650 addresses. */
5651 symbol = 0;
59c2e50f 5652 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5653 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5654 symbol = 0;
a4d0f181
TS
5655 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5656 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5657 {
5658 /* If this is a dynamic link, we should have created a
5659 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
de194d85 5660 in _bfd_mips_elf_create_dynamic_sections.
b49e97c9
TS
5661 Otherwise, we should define the symbol with a value of 0.
5662 FIXME: It should probably get into the symbol table
5663 somehow as well. */
0e1862bb 5664 BFD_ASSERT (! bfd_link_pic (info));
b49e97c9
TS
5665 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5666 symbol = 0;
5667 }
5e2b0d47
NC
5668 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5669 {
5670 /* This is an optional symbol - an Irix specific extension to the
5671 ELF spec. Ignore it for now.
5672 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5673 than simply ignoring them, but we do not handle this for now.
5674 For information see the "64-bit ELF Object File Specification"
5675 which is available from here:
5676 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5677 symbol = 0;
5678 }
b49e97c9
TS
5679 else
5680 {
0a1b45a2
AM
5681 bool reject_undefined
5682 = ((info->unresolved_syms_in_objects == RM_DIAGNOSE
5683 && !info->warn_unresolved_syms)
5684 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT);
dfb93f11 5685
95a51568 5686 info->callbacks->undefined_symbol
1a72702b 5687 (info, h->root.root.root.string, input_bfd,
dfb93f11
JC
5688 input_section, relocation->r_offset, reject_undefined);
5689
5690 if (reject_undefined)
5691 return bfd_reloc_undefined;
5692
5693 symbol = 0;
b49e97c9
TS
5694 }
5695
30c09090 5696 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
1bbce132 5697 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
b49e97c9
TS
5698 }
5699
738e5348
RS
5700 /* If this is a reference to a 16-bit function with a stub, we need
5701 to redirect the relocation to the stub unless:
5702
5703 (a) the relocation is for a MIPS16 JAL;
5704
5705 (b) the relocation is for a MIPS16 PIC call, and there are no
5706 non-MIPS16 uses of the GOT slot; or
5707
5708 (c) the section allows direct references to MIPS16 functions. */
5709 if (r_type != R_MIPS16_26
0e1862bb 5710 && !bfd_link_relocatable (info)
738e5348
RS
5711 && ((h != NULL
5712 && h->fn_stub != NULL
5713 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71 5714 || (local_p
698600e4
AM
5715 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5716 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5717 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5718 {
5719 /* This is a 32- or 64-bit call to a 16-bit function. We should
5720 have already noticed that we were going to need the
5721 stub. */
5722 if (local_p)
8f0c309a 5723 {
698600e4 5724 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
8f0c309a
CLT
5725 value = 0;
5726 }
b49e97c9
TS
5727 else
5728 {
5729 BFD_ASSERT (h->need_fn_stub);
8f0c309a
CLT
5730 if (h->la25_stub)
5731 {
5732 /* If a LA25 header for the stub itself exists, point to the
5733 prepended LUI/ADDIU sequence. */
5734 sec = h->la25_stub->stub_section;
5735 value = h->la25_stub->offset;
5736 }
5737 else
5738 {
5739 sec = h->fn_stub;
5740 value = 0;
5741 }
b49e97c9
TS
5742 }
5743
8f0c309a 5744 symbol = sec->output_section->vma + sec->output_offset + value;
f38c2df5 5745 /* The target is 16-bit, but the stub isn't. */
0a1b45a2 5746 target_is_16_bit_code_p = false;
b49e97c9 5747 }
1bbce132
MR
5748 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5749 to a standard MIPS function, we need to redirect the call to the stub.
5750 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5751 indirect calls should use an indirect stub instead. */
0e1862bb 5752 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
b314ec0e 5753 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71 5754 || (local_p
698600e4
AM
5755 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5756 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
1bbce132 5757 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
b49e97c9 5758 {
b9d58d71 5759 if (local_p)
698600e4 5760 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
b9d58d71 5761 else
b49e97c9 5762 {
b9d58d71
TS
5763 /* If both call_stub and call_fp_stub are defined, we can figure
5764 out which one to use by checking which one appears in the input
5765 file. */
5766 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5767 {
b9d58d71 5768 asection *o;
68ffbac6 5769
b9d58d71
TS
5770 sec = NULL;
5771 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5772 {
fd361982 5773 if (CALL_FP_STUB_P (bfd_section_name (o)))
b9d58d71
TS
5774 {
5775 sec = h->call_fp_stub;
5776 break;
5777 }
b49e97c9 5778 }
b9d58d71
TS
5779 if (sec == NULL)
5780 sec = h->call_stub;
b49e97c9 5781 }
b9d58d71 5782 else if (h->call_stub != NULL)
b49e97c9 5783 sec = h->call_stub;
b9d58d71
TS
5784 else
5785 sec = h->call_fp_stub;
07d6d2b8 5786 }
b49e97c9 5787
eea6121a 5788 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5789 symbol = sec->output_section->vma + sec->output_offset;
5790 }
861fb55a
DJ
5791 /* If this is a direct call to a PIC function, redirect to the
5792 non-PIC stub. */
5793 else if (h != NULL && h->la25_stub
8f0c309a
CLT
5794 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5795 target_is_16_bit_code_p))
c7318def
MR
5796 {
5797 symbol = (h->la25_stub->stub_section->output_section->vma
5798 + h->la25_stub->stub_section->output_offset
5799 + h->la25_stub->offset);
5800 if (ELF_ST_IS_MICROMIPS (h->root.other))
5801 symbol |= 1;
5802 }
1bbce132
MR
5803 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5804 entry is used if a standard PLT entry has also been made. In this
5805 case the symbol will have been set by mips_elf_set_plt_sym_value
5806 to point to the standard PLT entry, so redirect to the compressed
5807 one. */
54806ffa
MR
5808 else if ((mips16_branch_reloc_p (r_type)
5809 || micromips_branch_reloc_p (r_type))
0e1862bb 5810 && !bfd_link_relocatable (info)
1bbce132
MR
5811 && h != NULL
5812 && h->use_plt_entry
5813 && h->root.plt.plist->comp_offset != MINUS_ONE
5814 && h->root.plt.plist->mips_offset != MINUS_ONE)
5815 {
0a1b45a2 5816 bool micromips_p = MICROMIPS_P (abfd);
1bbce132 5817
ce558b89 5818 sec = htab->root.splt;
1bbce132
MR
5819 symbol = (sec->output_section->vma
5820 + sec->output_offset
5821 + htab->plt_header_size
5822 + htab->plt_mips_offset
5823 + h->root.plt.plist->comp_offset
5824 + 1);
5825
5826 target_is_16_bit_code_p = !micromips_p;
5827 target_is_micromips_code_p = micromips_p;
5828 }
b49e97c9 5829
df58fc94 5830 /* Make sure MIPS16 and microMIPS are not used together. */
c9775dde 5831 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
df58fc94
RS
5832 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5833 {
4eca0228 5834 _bfd_error_handler
df58fc94
RS
5835 (_("MIPS16 and microMIPS functions cannot call each other"));
5836 return bfd_reloc_notsupported;
5837 }
5838
b49e97c9 5839 /* Calls from 16-bit code to 32-bit code and vice versa require the
df58fc94
RS
5840 mode change. However, we can ignore calls to undefined weak symbols,
5841 which should never be executed at runtime. This exception is important
5842 because the assembly writer may have "known" that any definition of the
5843 symbol would be 16-bit code, and that direct jumps were therefore
5844 acceptable. */
0e1862bb 5845 *cross_mode_jump_p = (!bfd_link_relocatable (info)
df58fc94 5846 && !(h && h->root.root.type == bfd_link_hash_undefweak)
9d862524
MR
5847 && ((mips16_branch_reloc_p (r_type)
5848 && !target_is_16_bit_code_p)
5849 || (micromips_branch_reloc_p (r_type)
df58fc94 5850 && !target_is_micromips_code_p)
9d862524
MR
5851 || ((branch_reloc_p (r_type)
5852 || r_type == R_MIPS_JALR)
df58fc94
RS
5853 && (target_is_16_bit_code_p
5854 || target_is_micromips_code_p))));
b49e97c9 5855
47275900
MR
5856 resolved_to_zero = (h != NULL
5857 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5858
5859 switch (r_type)
5860 {
5861 case R_MIPS16_CALL16:
5862 case R_MIPS16_GOT16:
5863 case R_MIPS_CALL16:
5864 case R_MIPS_GOT16:
5865 case R_MIPS_GOT_PAGE:
5866 case R_MIPS_GOT_DISP:
5867 case R_MIPS_GOT_LO16:
5868 case R_MIPS_CALL_LO16:
5869 case R_MICROMIPS_CALL16:
5870 case R_MICROMIPS_GOT16:
5871 case R_MICROMIPS_GOT_PAGE:
5872 case R_MICROMIPS_GOT_DISP:
5873 case R_MICROMIPS_GOT_LO16:
5874 case R_MICROMIPS_CALL_LO16:
5875 if (resolved_to_zero
5876 && !bfd_link_relocatable (info)
73425813
AM
5877 && bfd_reloc_offset_in_range (howto, input_bfd, input_section,
5878 relocation->r_offset)
47275900 5879 && mips_elf_nullify_got_load (input_bfd, contents,
0a1b45a2 5880 relocation, howto, true))
47275900
MR
5881 return bfd_reloc_continue;
5882
5883 /* Fall through. */
5884 case R_MIPS_GOT_HI16:
5885 case R_MIPS_CALL_HI16:
5886 case R_MICROMIPS_GOT_HI16:
5887 case R_MICROMIPS_CALL_HI16:
5888 if (resolved_to_zero
5889 && htab->use_absolute_zero
5890 && bfd_link_pic (info))
5891 {
5892 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5893 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
0a1b45a2 5894 false, false, false);
47275900
MR
5895 BFD_ASSERT (h != NULL);
5896 }
5897 break;
5898 }
5899
c5d6fa44 5900 local_p = (h == NULL || mips_use_local_got_p (info, h));
b49e97c9 5901
0a61c8c2
RS
5902 gp0 = _bfd_get_gp_value (input_bfd);
5903 gp = _bfd_get_gp_value (abfd);
23cc69b6 5904 if (htab->got_info)
a8028dd0 5905 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5906
5907 if (gnu_local_gp_p)
5908 symbol = gp;
5909
df58fc94
RS
5910 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5911 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5912 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5913 if (got_page_reloc_p (r_type) && !local_p)
020d7251 5914 {
df58fc94
RS
5915 r_type = (micromips_reloc_p (r_type)
5916 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
020d7251
RS
5917 addend = 0;
5918 }
5919
e77760d2 5920 /* If we haven't already determined the GOT offset, and we're going
0a61c8c2 5921 to need it, get it now. */
b49e97c9
TS
5922 switch (r_type)
5923 {
738e5348
RS
5924 case R_MIPS16_CALL16:
5925 case R_MIPS16_GOT16:
b49e97c9
TS
5926 case R_MIPS_CALL16:
5927 case R_MIPS_GOT16:
5928 case R_MIPS_GOT_DISP:
5929 case R_MIPS_GOT_HI16:
5930 case R_MIPS_CALL_HI16:
5931 case R_MIPS_GOT_LO16:
5932 case R_MIPS_CALL_LO16:
df58fc94
RS
5933 case R_MICROMIPS_CALL16:
5934 case R_MICROMIPS_GOT16:
5935 case R_MICROMIPS_GOT_DISP:
5936 case R_MICROMIPS_GOT_HI16:
5937 case R_MICROMIPS_CALL_HI16:
5938 case R_MICROMIPS_GOT_LO16:
5939 case R_MICROMIPS_CALL_LO16:
0f20cc35
DJ
5940 case R_MIPS_TLS_GD:
5941 case R_MIPS_TLS_GOTTPREL:
5942 case R_MIPS_TLS_LDM:
d0f13682
CLT
5943 case R_MIPS16_TLS_GD:
5944 case R_MIPS16_TLS_GOTTPREL:
5945 case R_MIPS16_TLS_LDM:
df58fc94
RS
5946 case R_MICROMIPS_TLS_GD:
5947 case R_MICROMIPS_TLS_GOTTPREL:
5948 case R_MICROMIPS_TLS_LDM:
b49e97c9 5949 /* Find the index into the GOT where this value is located. */
df58fc94 5950 if (tls_ldm_reloc_p (r_type))
0f20cc35 5951 {
0a44bf69 5952 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5953 0, 0, NULL, r_type);
0f20cc35
DJ
5954 if (g == MINUS_ONE)
5955 return bfd_reloc_outofrange;
5956 }
5957 else if (!local_p)
b49e97c9 5958 {
0a44bf69
RS
5959 /* On VxWorks, CALL relocations should refer to the .got.plt
5960 entry, which is initialized to point at the PLT stub. */
90c14f0c 5961 if (htab->root.target_os == is_vxworks
df58fc94
RS
5962 && (call_hi16_reloc_p (r_type)
5963 || call_lo16_reloc_p (r_type)
738e5348 5964 || call16_reloc_p (r_type)))
0a44bf69
RS
5965 {
5966 BFD_ASSERT (addend == 0);
5967 BFD_ASSERT (h->root.needs_plt);
5968 g = mips_elf_gotplt_index (info, &h->root);
5969 }
5970 else
b49e97c9 5971 {
020d7251 5972 BFD_ASSERT (addend == 0);
13fbec83
RS
5973 g = mips_elf_global_got_index (abfd, info, input_bfd,
5974 &h->root, r_type);
e641e783 5975 if (!TLS_RELOC_P (r_type)
020d7251
RS
5976 && !elf_hash_table (info)->dynamic_sections_created)
5977 /* This is a static link. We must initialize the GOT entry. */
ce558b89 5978 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
b49e97c9
TS
5979 }
5980 }
90c14f0c 5981 else if (htab->root.target_os != is_vxworks
738e5348 5982 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5983 /* The calculation below does not involve "g". */
b49e97c9
TS
5984 break;
5985 else
5986 {
5c18022e 5987 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5988 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5989 if (g == MINUS_ONE)
5990 return bfd_reloc_outofrange;
5991 }
5992
5993 /* Convert GOT indices to actual offsets. */
a8028dd0 5994 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5995 break;
b49e97c9
TS
5996 }
5997
0a44bf69
RS
5998 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5999 symbols are resolved by the loader. Add them to .rela.dyn. */
6000 if (h != NULL && is_gott_symbol (info, &h->root))
6001 {
6002 Elf_Internal_Rela outrel;
6003 bfd_byte *loc;
6004 asection *s;
6005
0a1b45a2 6006 s = mips_elf_rel_dyn_section (info, false);
0a44bf69
RS
6007 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6008
6009 outrel.r_offset = (input_section->output_section->vma
6010 + input_section->output_offset
6011 + relocation->r_offset);
6012 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
6013 outrel.r_addend = addend;
6014 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
6015
6016 /* If we've written this relocation for a readonly section,
6017 we need to set DF_TEXTREL again, so that we do not delete the
6018 DT_TEXTREL tag. */
6019 if (MIPS_ELF_READONLY_SECTION (input_section))
6020 info->flags |= DF_TEXTREL;
6021
0a44bf69
RS
6022 *valuep = 0;
6023 return bfd_reloc_ok;
6024 }
6025
b49e97c9
TS
6026 /* Figure out what kind of relocation is being performed. */
6027 switch (r_type)
6028 {
6029 case R_MIPS_NONE:
6030 return bfd_reloc_continue;
6031
6032 case R_MIPS_16:
c3eb94b4
MF
6033 if (howto->partial_inplace)
6034 addend = _bfd_mips_elf_sign_extend (addend, 16);
6035 value = symbol + addend;
b49e97c9
TS
6036 overflowed_p = mips_elf_overflow_p (value, 16);
6037 break;
6038
6039 case R_MIPS_32:
6040 case R_MIPS_REL32:
6041 case R_MIPS_64:
0e1862bb 6042 if ((bfd_link_pic (info)
861fb55a 6043 || (htab->root.dynamic_sections_created
b49e97c9 6044 && h != NULL
f5385ebf 6045 && h->root.def_dynamic
861fb55a
DJ
6046 && !h->root.def_regular
6047 && !h->has_static_relocs))
cf35638d 6048 && r_symndx != STN_UNDEF
9a59ad6b
DJ
6049 && (h == NULL
6050 || h->root.root.type != bfd_link_hash_undefweak
ad951203
L
6051 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
6052 && !resolved_to_zero))
b49e97c9
TS
6053 && (input_section->flags & SEC_ALLOC) != 0)
6054 {
861fb55a 6055 /* If we're creating a shared library, then we can't know
b49e97c9
TS
6056 where the symbol will end up. So, we create a relocation
6057 record in the output, and leave the job up to the dynamic
861fb55a
DJ
6058 linker. We must do the same for executable references to
6059 shared library symbols, unless we've decided to use copy
6060 relocs or PLTs instead. */
b49e97c9
TS
6061 value = addend;
6062 if (!mips_elf_create_dynamic_relocation (abfd,
6063 info,
6064 relocation,
6065 h,
6066 sec,
6067 symbol,
6068 &value,
6069 input_section))
6070 return bfd_reloc_undefined;
6071 }
6072 else
6073 {
6074 if (r_type != R_MIPS_REL32)
6075 value = symbol + addend;
6076 else
6077 value = addend;
6078 }
6079 value &= howto->dst_mask;
092dcd75
CD
6080 break;
6081
6082 case R_MIPS_PC32:
6083 value = symbol + addend - p;
6084 value &= howto->dst_mask;
b49e97c9
TS
6085 break;
6086
b49e97c9
TS
6087 case R_MIPS16_26:
6088 /* The calculation for R_MIPS16_26 is just the same as for an
6089 R_MIPS_26. It's only the storage of the relocated field into
6090 the output file that's different. That's handled in
6091 mips_elf_perform_relocation. So, we just fall through to the
6092 R_MIPS_26 case here. */
6093 case R_MIPS_26:
df58fc94
RS
6094 case R_MICROMIPS_26_S1:
6095 {
6096 unsigned int shift;
6097
df58fc94
RS
6098 /* Shift is 2, unusually, for microMIPS JALX. */
6099 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6100
77434823 6101 if (howto->partial_inplace && !section_p)
df58fc94 6102 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
c3eb94b4
MF
6103 else
6104 value = addend;
bc27bb05
MR
6105 value += symbol;
6106
9d862524
MR
6107 /* Make sure the target of a jump is suitably aligned. Bit 0 must
6108 be the correct ISA mode selector except for weak undefined
6109 symbols. */
6110 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6111 && (*cross_mode_jump_p
6112 ? (value & 3) != (r_type == R_MIPS_26)
07d6d2b8 6113 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
bc27bb05
MR
6114 return bfd_reloc_outofrange;
6115
6116 value >>= shift;
77434823 6117 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
df58fc94
RS
6118 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6119 value &= howto->dst_mask;
6120 }
b49e97c9
TS
6121 break;
6122
0f20cc35 6123 case R_MIPS_TLS_DTPREL_HI16:
d0f13682 6124 case R_MIPS16_TLS_DTPREL_HI16:
df58fc94 6125 case R_MICROMIPS_TLS_DTPREL_HI16:
0f20cc35
DJ
6126 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6127 & howto->dst_mask);
6128 break;
6129
6130 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
6131 case R_MIPS_TLS_DTPREL32:
6132 case R_MIPS_TLS_DTPREL64:
d0f13682 6133 case R_MIPS16_TLS_DTPREL_LO16:
df58fc94 6134 case R_MICROMIPS_TLS_DTPREL_LO16:
0f20cc35
DJ
6135 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6136 break;
6137
6138 case R_MIPS_TLS_TPREL_HI16:
d0f13682 6139 case R_MIPS16_TLS_TPREL_HI16:
df58fc94 6140 case R_MICROMIPS_TLS_TPREL_HI16:
0f20cc35
DJ
6141 value = (mips_elf_high (addend + symbol - tprel_base (info))
6142 & howto->dst_mask);
6143 break;
6144
6145 case R_MIPS_TLS_TPREL_LO16:
d0f13682
CLT
6146 case R_MIPS_TLS_TPREL32:
6147 case R_MIPS_TLS_TPREL64:
6148 case R_MIPS16_TLS_TPREL_LO16:
df58fc94 6149 case R_MICROMIPS_TLS_TPREL_LO16:
0f20cc35
DJ
6150 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6151 break;
6152
b49e97c9 6153 case R_MIPS_HI16:
d6f16593 6154 case R_MIPS16_HI16:
df58fc94 6155 case R_MICROMIPS_HI16:
b49e97c9
TS
6156 if (!gp_disp_p)
6157 {
6158 value = mips_elf_high (addend + symbol);
6159 value &= howto->dst_mask;
6160 }
6161 else
6162 {
d6f16593 6163 /* For MIPS16 ABI code we generate this sequence
07d6d2b8
AM
6164 0: li $v0,%hi(_gp_disp)
6165 4: addiupc $v1,%lo(_gp_disp)
6166 8: sll $v0,16
d6f16593
MR
6167 12: addu $v0,$v1
6168 14: move $gp,$v0
6169 So the offsets of hi and lo relocs are the same, but the
888b9c01
CLT
6170 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6171 ADDIUPC clears the low two bits of the instruction address,
6172 so the base is ($t9 + 4) & ~3. */
d6f16593 6173 if (r_type == R_MIPS16_HI16)
888b9c01 6174 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
df58fc94
RS
6175 /* The microMIPS .cpload sequence uses the same assembly
6176 instructions as the traditional psABI version, but the
6177 incoming $t9 has the low bit set. */
6178 else if (r_type == R_MICROMIPS_HI16)
6179 value = mips_elf_high (addend + gp - p - 1);
d6f16593
MR
6180 else
6181 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
6182 }
6183 break;
6184
6185 case R_MIPS_LO16:
d6f16593 6186 case R_MIPS16_LO16:
df58fc94
RS
6187 case R_MICROMIPS_LO16:
6188 case R_MICROMIPS_HI0_LO16:
b49e97c9
TS
6189 if (!gp_disp_p)
6190 value = (symbol + addend) & howto->dst_mask;
6191 else
6192 {
d6f16593
MR
6193 /* See the comment for R_MIPS16_HI16 above for the reason
6194 for this conditional. */
6195 if (r_type == R_MIPS16_LO16)
888b9c01 6196 value = addend + gp - (p & ~(bfd_vma) 0x3);
df58fc94
RS
6197 else if (r_type == R_MICROMIPS_LO16
6198 || r_type == R_MICROMIPS_HI0_LO16)
6199 value = addend + gp - p + 3;
d6f16593
MR
6200 else
6201 value = addend + gp - p + 4;
b49e97c9 6202 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 6203 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
6204 _gp_disp are normally generated from the .cpload
6205 pseudo-op. It generates code that normally looks like
6206 this:
6207
6208 lui $gp,%hi(_gp_disp)
6209 addiu $gp,$gp,%lo(_gp_disp)
6210 addu $gp,$gp,$t9
6211
6212 Here $t9 holds the address of the function being called,
6213 as required by the MIPS ELF ABI. The R_MIPS_LO16
6214 relocation can easily overflow in this situation, but the
6215 R_MIPS_HI16 relocation will handle the overflow.
6216 Therefore, we consider this a bug in the MIPS ABI, and do
6217 not check for overflow here. */
6218 }
6219 break;
6220
6221 case R_MIPS_LITERAL:
df58fc94 6222 case R_MICROMIPS_LITERAL:
b49e97c9
TS
6223 /* Because we don't merge literal sections, we can handle this
6224 just like R_MIPS_GPREL16. In the long run, we should merge
6225 shared literals, and then we will need to additional work
6226 here. */
6227
6228 /* Fall through. */
6229
6230 case R_MIPS16_GPREL:
6231 /* The R_MIPS16_GPREL performs the same calculation as
6232 R_MIPS_GPREL16, but stores the relocated bits in a different
6233 order. We don't need to do anything special here; the
6234 differences are handled in mips_elf_perform_relocation. */
6235 case R_MIPS_GPREL16:
df58fc94
RS
6236 case R_MICROMIPS_GPREL7_S2:
6237 case R_MICROMIPS_GPREL16:
bce03d3d
AO
6238 /* Only sign-extend the addend if it was extracted from the
6239 instruction. If the addend was separate, leave it alone,
6240 otherwise we may lose significant bits. */
6241 if (howto->partial_inplace)
a7ebbfdf 6242 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
6243 value = symbol + addend - gp;
6244 /* If the symbol was local, any earlier relocatable links will
6245 have adjusted its addend with the gp offset, so compensate
6246 for that now. Don't do it for symbols forced local in this
6247 link, though, since they won't have had the gp offset applied
6248 to them before. */
6249 if (was_local_p)
6250 value += gp0;
538baf8b
AB
6251 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6252 overflowed_p = mips_elf_overflow_p (value, 16);
b49e97c9
TS
6253 break;
6254
738e5348
RS
6255 case R_MIPS16_GOT16:
6256 case R_MIPS16_CALL16:
b49e97c9
TS
6257 case R_MIPS_GOT16:
6258 case R_MIPS_CALL16:
df58fc94
RS
6259 case R_MICROMIPS_GOT16:
6260 case R_MICROMIPS_CALL16:
0a44bf69 6261 /* VxWorks does not have separate local and global semantics for
738e5348 6262 R_MIPS*_GOT16; every relocation evaluates to "G". */
90c14f0c 6263 if (htab->root.target_os != is_vxworks && local_p)
b49e97c9 6264 {
5c18022e 6265 value = mips_elf_got16_entry (abfd, input_bfd, info,
020d7251 6266 symbol + addend, !was_local_p);
b49e97c9
TS
6267 if (value == MINUS_ONE)
6268 return bfd_reloc_outofrange;
6269 value
a8028dd0 6270 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
6271 overflowed_p = mips_elf_overflow_p (value, 16);
6272 break;
6273 }
6274
6275 /* Fall through. */
6276
0f20cc35
DJ
6277 case R_MIPS_TLS_GD:
6278 case R_MIPS_TLS_GOTTPREL:
6279 case R_MIPS_TLS_LDM:
b49e97c9 6280 case R_MIPS_GOT_DISP:
d0f13682
CLT
6281 case R_MIPS16_TLS_GD:
6282 case R_MIPS16_TLS_GOTTPREL:
6283 case R_MIPS16_TLS_LDM:
df58fc94
RS
6284 case R_MICROMIPS_TLS_GD:
6285 case R_MICROMIPS_TLS_GOTTPREL:
6286 case R_MICROMIPS_TLS_LDM:
6287 case R_MICROMIPS_GOT_DISP:
b49e97c9
TS
6288 value = g;
6289 overflowed_p = mips_elf_overflow_p (value, 16);
6290 break;
6291
6292 case R_MIPS_GPREL32:
bce03d3d
AO
6293 value = (addend + symbol + gp0 - gp);
6294 if (!save_addend)
6295 value &= howto->dst_mask;
b49e97c9
TS
6296 break;
6297
6298 case R_MIPS_PC16:
bad36eac 6299 case R_MIPS_GNU_REL16_S2:
c3eb94b4
MF
6300 if (howto->partial_inplace)
6301 addend = _bfd_mips_elf_sign_extend (addend, 18);
6302
9d862524 6303 /* No need to exclude weak undefined symbols here as they resolve
07d6d2b8
AM
6304 to 0 and never set `*cross_mode_jump_p', so this alignment check
6305 will never trigger for them. */
9d862524
MR
6306 if (*cross_mode_jump_p
6307 ? ((symbol + addend) & 3) != 1
6308 : ((symbol + addend) & 3) != 0)
c3eb94b4
MF
6309 return bfd_reloc_outofrange;
6310
6311 value = symbol + addend - p;
538baf8b
AB
6312 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6313 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
6314 value >>= howto->rightshift;
6315 value &= howto->dst_mask;
b49e97c9
TS
6316 break;
6317
c9775dde
MR
6318 case R_MIPS16_PC16_S1:
6319 if (howto->partial_inplace)
6320 addend = _bfd_mips_elf_sign_extend (addend, 17);
6321
6322 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
9d862524
MR
6323 && (*cross_mode_jump_p
6324 ? ((symbol + addend) & 3) != 0
6325 : ((symbol + addend) & 1) == 0))
c9775dde
MR
6326 return bfd_reloc_outofrange;
6327
6328 value = symbol + addend - p;
6329 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6330 overflowed_p = mips_elf_overflow_p (value, 17);
6331 value >>= howto->rightshift;
6332 value &= howto->dst_mask;
6333 break;
6334
7361da2c
AB
6335 case R_MIPS_PC21_S2:
6336 if (howto->partial_inplace)
6337 addend = _bfd_mips_elf_sign_extend (addend, 23);
6338
6339 if ((symbol + addend) & 3)
6340 return bfd_reloc_outofrange;
6341
6342 value = symbol + addend - p;
538baf8b
AB
6343 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6344 overflowed_p = mips_elf_overflow_p (value, 23);
7361da2c
AB
6345 value >>= howto->rightshift;
6346 value &= howto->dst_mask;
6347 break;
6348
6349 case R_MIPS_PC26_S2:
6350 if (howto->partial_inplace)
6351 addend = _bfd_mips_elf_sign_extend (addend, 28);
6352
6353 if ((symbol + addend) & 3)
6354 return bfd_reloc_outofrange;
6355
6356 value = symbol + addend - p;
538baf8b
AB
6357 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6358 overflowed_p = mips_elf_overflow_p (value, 28);
7361da2c
AB
6359 value >>= howto->rightshift;
6360 value &= howto->dst_mask;
6361 break;
6362
6363 case R_MIPS_PC18_S3:
6364 if (howto->partial_inplace)
6365 addend = _bfd_mips_elf_sign_extend (addend, 21);
6366
6367 if ((symbol + addend) & 7)
6368 return bfd_reloc_outofrange;
6369
6370 value = symbol + addend - ((p | 7) ^ 7);
538baf8b
AB
6371 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6372 overflowed_p = mips_elf_overflow_p (value, 21);
7361da2c
AB
6373 value >>= howto->rightshift;
6374 value &= howto->dst_mask;
6375 break;
6376
6377 case R_MIPS_PC19_S2:
6378 if (howto->partial_inplace)
6379 addend = _bfd_mips_elf_sign_extend (addend, 21);
6380
6381 if ((symbol + addend) & 3)
6382 return bfd_reloc_outofrange;
6383
6384 value = symbol + addend - p;
538baf8b
AB
6385 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6386 overflowed_p = mips_elf_overflow_p (value, 21);
7361da2c
AB
6387 value >>= howto->rightshift;
6388 value &= howto->dst_mask;
6389 break;
6390
6391 case R_MIPS_PCHI16:
6392 value = mips_elf_high (symbol + addend - p);
7361da2c
AB
6393 value &= howto->dst_mask;
6394 break;
6395
6396 case R_MIPS_PCLO16:
6397 if (howto->partial_inplace)
6398 addend = _bfd_mips_elf_sign_extend (addend, 16);
6399 value = symbol + addend - p;
6400 value &= howto->dst_mask;
6401 break;
6402
df58fc94 6403 case R_MICROMIPS_PC7_S1:
c3eb94b4
MF
6404 if (howto->partial_inplace)
6405 addend = _bfd_mips_elf_sign_extend (addend, 8);
9d862524
MR
6406
6407 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6408 && (*cross_mode_jump_p
6409 ? ((symbol + addend + 2) & 3) != 0
6410 : ((symbol + addend + 2) & 1) == 0))
6411 return bfd_reloc_outofrange;
6412
c3eb94b4 6413 value = symbol + addend - p;
538baf8b
AB
6414 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6415 overflowed_p = mips_elf_overflow_p (value, 8);
df58fc94
RS
6416 value >>= howto->rightshift;
6417 value &= howto->dst_mask;
6418 break;
6419
6420 case R_MICROMIPS_PC10_S1:
c3eb94b4
MF
6421 if (howto->partial_inplace)
6422 addend = _bfd_mips_elf_sign_extend (addend, 11);
9d862524
MR
6423
6424 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6425 && (*cross_mode_jump_p
6426 ? ((symbol + addend + 2) & 3) != 0
6427 : ((symbol + addend + 2) & 1) == 0))
6428 return bfd_reloc_outofrange;
6429
c3eb94b4 6430 value = symbol + addend - p;
538baf8b
AB
6431 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6432 overflowed_p = mips_elf_overflow_p (value, 11);
df58fc94
RS
6433 value >>= howto->rightshift;
6434 value &= howto->dst_mask;
6435 break;
6436
6437 case R_MICROMIPS_PC16_S1:
c3eb94b4
MF
6438 if (howto->partial_inplace)
6439 addend = _bfd_mips_elf_sign_extend (addend, 17);
9d862524
MR
6440
6441 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6442 && (*cross_mode_jump_p
6443 ? ((symbol + addend) & 3) != 0
6444 : ((symbol + addend) & 1) == 0))
6445 return bfd_reloc_outofrange;
6446
c3eb94b4 6447 value = symbol + addend - p;
538baf8b
AB
6448 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6449 overflowed_p = mips_elf_overflow_p (value, 17);
df58fc94
RS
6450 value >>= howto->rightshift;
6451 value &= howto->dst_mask;
6452 break;
6453
6454 case R_MICROMIPS_PC23_S2:
c3eb94b4
MF
6455 if (howto->partial_inplace)
6456 addend = _bfd_mips_elf_sign_extend (addend, 25);
6457 value = symbol + addend - ((p | 3) ^ 3);
538baf8b
AB
6458 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6459 overflowed_p = mips_elf_overflow_p (value, 25);
df58fc94
RS
6460 value >>= howto->rightshift;
6461 value &= howto->dst_mask;
6462 break;
6463
b49e97c9
TS
6464 case R_MIPS_GOT_HI16:
6465 case R_MIPS_CALL_HI16:
df58fc94
RS
6466 case R_MICROMIPS_GOT_HI16:
6467 case R_MICROMIPS_CALL_HI16:
b49e97c9
TS
6468 /* We're allowed to handle these two relocations identically.
6469 The dynamic linker is allowed to handle the CALL relocations
6470 differently by creating a lazy evaluation stub. */
6471 value = g;
6472 value = mips_elf_high (value);
6473 value &= howto->dst_mask;
6474 break;
6475
6476 case R_MIPS_GOT_LO16:
6477 case R_MIPS_CALL_LO16:
df58fc94
RS
6478 case R_MICROMIPS_GOT_LO16:
6479 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
6480 value = g & howto->dst_mask;
6481 break;
6482
6483 case R_MIPS_GOT_PAGE:
df58fc94 6484 case R_MICROMIPS_GOT_PAGE:
5c18022e 6485 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
6486 if (value == MINUS_ONE)
6487 return bfd_reloc_outofrange;
a8028dd0 6488 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
6489 overflowed_p = mips_elf_overflow_p (value, 16);
6490 break;
6491
6492 case R_MIPS_GOT_OFST:
df58fc94 6493 case R_MICROMIPS_GOT_OFST:
93a2b7ae 6494 if (local_p)
5c18022e 6495 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
6496 else
6497 value = addend;
b49e97c9
TS
6498 overflowed_p = mips_elf_overflow_p (value, 16);
6499 break;
6500
6501 case R_MIPS_SUB:
df58fc94 6502 case R_MICROMIPS_SUB:
b49e97c9
TS
6503 value = symbol - addend;
6504 value &= howto->dst_mask;
6505 break;
6506
6507 case R_MIPS_HIGHER:
df58fc94 6508 case R_MICROMIPS_HIGHER:
b49e97c9
TS
6509 value = mips_elf_higher (addend + symbol);
6510 value &= howto->dst_mask;
6511 break;
6512
6513 case R_MIPS_HIGHEST:
df58fc94 6514 case R_MICROMIPS_HIGHEST:
b49e97c9
TS
6515 value = mips_elf_highest (addend + symbol);
6516 value &= howto->dst_mask;
6517 break;
6518
6519 case R_MIPS_SCN_DISP:
df58fc94 6520 case R_MICROMIPS_SCN_DISP:
b49e97c9
TS
6521 value = symbol + addend - sec->output_offset;
6522 value &= howto->dst_mask;
6523 break;
6524
b49e97c9 6525 case R_MIPS_JALR:
df58fc94 6526 case R_MICROMIPS_JALR:
1367d393
ILT
6527 /* This relocation is only a hint. In some cases, we optimize
6528 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
6529 when the symbol does not resolve locally. */
6530 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393 6531 return bfd_reloc_continue;
c1556ecd
MR
6532 /* We can't optimize cross-mode jumps either. */
6533 if (*cross_mode_jump_p)
6534 return bfd_reloc_continue;
1367d393 6535 value = symbol + addend;
c1556ecd
MR
6536 /* Neither we can non-instruction-aligned targets. */
6537 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6538 return bfd_reloc_continue;
1367d393 6539 break;
b49e97c9 6540
1367d393 6541 case R_MIPS_PJUMP:
b49e97c9
TS
6542 case R_MIPS_GNU_VTINHERIT:
6543 case R_MIPS_GNU_VTENTRY:
6544 /* We don't do anything with these at present. */
6545 return bfd_reloc_continue;
6546
6547 default:
6548 /* An unrecognized relocation type. */
6549 return bfd_reloc_notsupported;
6550 }
6551
6552 /* Store the VALUE for our caller. */
6553 *valuep = value;
6554 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6555}
6556
b49e97c9
TS
6557/* It has been determined that the result of the RELOCATION is the
6558 VALUE. Use HOWTO to place VALUE into the output file at the
6559 appropriate position. The SECTION is the section to which the
68ffbac6 6560 relocation applies.
38a7df63 6561 CROSS_MODE_JUMP_P is true if the relocation field
df58fc94 6562 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9 6563
b34976b6 6564 Returns FALSE if anything goes wrong. */
b49e97c9 6565
0a1b45a2 6566static bool
9719ad41
RS
6567mips_elf_perform_relocation (struct bfd_link_info *info,
6568 reloc_howto_type *howto,
6569 const Elf_Internal_Rela *relocation,
6570 bfd_vma value, bfd *input_bfd,
6571 asection *input_section, bfd_byte *contents,
0a1b45a2 6572 bool cross_mode_jump_p)
b49e97c9
TS
6573{
6574 bfd_vma x;
6575 bfd_byte *location;
6576 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6577
6578 /* Figure out where the relocation is occurring. */
6579 location = contents + relocation->r_offset;
6580
0a1b45a2 6581 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, false, location);
d6f16593 6582
b49e97c9
TS
6583 /* Obtain the current value. */
6584 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6585
6586 /* Clear the field we are setting. */
6587 x &= ~howto->dst_mask;
6588
b49e97c9
TS
6589 /* Set the field. */
6590 x |= (value & howto->dst_mask);
6591
a6ebf616 6592 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
9d862524
MR
6593 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6594 {
6595 bfd_vma opcode = x >> 26;
6596
6597 if (r_type == R_MIPS16_26 ? opcode == 0x7
6598 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6599 : opcode == 0x1d)
6600 {
6601 info->callbacks->einfo
2c1c9679 6602 (_("%X%H: unsupported JALX to the same ISA mode\n"),
9d862524 6603 input_bfd, input_section, relocation->r_offset);
0a1b45a2 6604 return true;
9d862524
MR
6605 }
6606 }
38a7df63 6607 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 6608 {
0a1b45a2 6609 bool ok;
b49e97c9
TS
6610 bfd_vma opcode = x >> 26;
6611 bfd_vma jalx_opcode;
6612
6613 /* Check to see if the opcode is already JAL or JALX. */
6614 if (r_type == R_MIPS16_26)
6615 {
6616 ok = ((opcode == 0x6) || (opcode == 0x7));
6617 jalx_opcode = 0x7;
6618 }
df58fc94
RS
6619 else if (r_type == R_MICROMIPS_26_S1)
6620 {
6621 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6622 jalx_opcode = 0x3c;
6623 }
b49e97c9
TS
6624 else
6625 {
6626 ok = ((opcode == 0x3) || (opcode == 0x1d));
6627 jalx_opcode = 0x1d;
6628 }
6629
3bdf9505 6630 /* If the opcode is not JAL or JALX, there's a problem. We cannot
07d6d2b8 6631 convert J or JALS to JALX. */
b49e97c9
TS
6632 if (!ok)
6633 {
5f68df25 6634 info->callbacks->einfo
2c1c9679 6635 (_("%X%H: unsupported jump between ISA modes; "
5f68df25
MR
6636 "consider recompiling with interlinking enabled\n"),
6637 input_bfd, input_section, relocation->r_offset);
0a1b45a2 6638 return true;
b49e97c9
TS
6639 }
6640
6641 /* Make this the JALX opcode. */
2365f8d7 6642 x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
b49e97c9 6643 }
9d862524
MR
6644 else if (cross_mode_jump_p && b_reloc_p (r_type))
6645 {
0a1b45a2 6646 bool ok = false;
a6ebf616
MR
6647 bfd_vma opcode = x >> 16;
6648 bfd_vma jalx_opcode = 0;
70e65ca8 6649 bfd_vma sign_bit = 0;
a6ebf616
MR
6650 bfd_vma addr;
6651 bfd_vma dest;
6652
6653 if (r_type == R_MICROMIPS_PC16_S1)
6654 {
6655 ok = opcode == 0x4060;
6656 jalx_opcode = 0x3c;
70e65ca8 6657 sign_bit = 0x10000;
a6ebf616
MR
6658 value <<= 1;
6659 }
6660 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6661 {
6662 ok = opcode == 0x411;
6663 jalx_opcode = 0x1d;
70e65ca8 6664 sign_bit = 0x20000;
a6ebf616
MR
6665 value <<= 2;
6666 }
6667
8b10b0b3 6668 if (ok && !bfd_link_pic (info))
a6ebf616 6669 {
8b10b0b3
MR
6670 addr = (input_section->output_section->vma
6671 + input_section->output_offset
6672 + relocation->r_offset
6673 + 4);
70e65ca8
MR
6674 dest = (addr
6675 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
a6ebf616 6676
8b10b0b3
MR
6677 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6678 {
6679 info->callbacks->einfo
2c1c9679 6680 (_("%X%H: cannot convert branch between ISA modes "
8b10b0b3
MR
6681 "to JALX: relocation out of range\n"),
6682 input_bfd, input_section, relocation->r_offset);
0a1b45a2 6683 return true;
8b10b0b3 6684 }
a6ebf616 6685
8b10b0b3
MR
6686 /* Make this the JALX opcode. */
6687 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6688 }
6689 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
a6ebf616
MR
6690 {
6691 info->callbacks->einfo
2c1c9679 6692 (_("%X%H: unsupported branch between ISA modes\n"),
a6ebf616 6693 input_bfd, input_section, relocation->r_offset);
0a1b45a2 6694 return true;
a6ebf616 6695 }
9d862524 6696 }
b49e97c9 6697
38a7df63
CF
6698 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6699 range. */
0e1862bb 6700 if (!bfd_link_relocatable (info)
38a7df63 6701 && !cross_mode_jump_p
cd8d5a82
CF
6702 && ((JAL_TO_BAL_P (input_bfd)
6703 && r_type == R_MIPS_26
0e392101 6704 && (x >> 26) == 0x3) /* jal addr */
cd8d5a82
CF
6705 || (JALR_TO_BAL_P (input_bfd)
6706 && r_type == R_MIPS_JALR
0e392101 6707 && x == 0x0320f809) /* jalr t9 */
38a7df63
CF
6708 || (JR_TO_B_P (input_bfd)
6709 && r_type == R_MIPS_JALR
0e392101 6710 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
1367d393
ILT
6711 {
6712 bfd_vma addr;
6713 bfd_vma dest;
6714 bfd_signed_vma off;
6715
6716 addr = (input_section->output_section->vma
6717 + input_section->output_offset
6718 + relocation->r_offset
6719 + 4);
6720 if (r_type == R_MIPS_26)
6721 dest = (value << 2) | ((addr >> 28) << 28);
6722 else
6723 dest = value;
6724 off = dest - addr;
6725 if (off <= 0x1ffff && off >= -0x20000)
38a7df63 6726 {
0e392101 6727 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
38a7df63
CF
6728 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6729 else
6730 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6731 }
1367d393
ILT
6732 }
6733
b49e97c9 6734 /* Put the value into the output. */
98e10ffa 6735 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
d6f16593 6736
0e1862bb 6737 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
df58fc94 6738 location);
d6f16593 6739
0a1b45a2 6740 return true;
b49e97c9 6741}
b49e97c9 6742\f
b49e97c9
TS
6743/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6744 is the original relocation, which is now being transformed into a
6745 dynamic relocation. The ADDENDP is adjusted if necessary; the
6746 caller should store the result in place of the original addend. */
6747
0a1b45a2 6748static bool
9719ad41
RS
6749mips_elf_create_dynamic_relocation (bfd *output_bfd,
6750 struct bfd_link_info *info,
6751 const Elf_Internal_Rela *rel,
6752 struct mips_elf_link_hash_entry *h,
6753 asection *sec, bfd_vma symbol,
6754 bfd_vma *addendp, asection *input_section)
b49e97c9 6755{
947216bf 6756 Elf_Internal_Rela outrel[3];
b49e97c9
TS
6757 asection *sreloc;
6758 bfd *dynobj;
6759 int r_type;
5d41f0b6 6760 long indx;
0a1b45a2 6761 bool defined_p;
0a44bf69 6762 struct mips_elf_link_hash_table *htab;
b49e97c9 6763
0a44bf69 6764 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
6765 BFD_ASSERT (htab != NULL);
6766
b49e97c9
TS
6767 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6768 dynobj = elf_hash_table (info)->dynobj;
0a1b45a2 6769 sreloc = mips_elf_rel_dyn_section (info, false);
b49e97c9
TS
6770 BFD_ASSERT (sreloc != NULL);
6771 BFD_ASSERT (sreloc->contents != NULL);
6772 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 6773 < sreloc->size);
b49e97c9 6774
b49e97c9
TS
6775 outrel[0].r_offset =
6776 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
6777 if (ABI_64_P (output_bfd))
6778 {
6779 outrel[1].r_offset =
6780 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6781 outrel[2].r_offset =
6782 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6783 }
b49e97c9 6784
c5ae1840 6785 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 6786 /* The relocation field has been deleted. */
0a1b45a2 6787 return true;
5d41f0b6
RS
6788
6789 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
6790 {
6791 /* The relocation field has been converted into a relative value of
6792 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6793 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 6794 *addendp += symbol;
0a1b45a2 6795 return true;
0d591ff7 6796 }
b49e97c9 6797
5d41f0b6
RS
6798 /* We must now calculate the dynamic symbol table index to use
6799 in the relocation. */
d4a77f3f 6800 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5d41f0b6 6801 {
90c14f0c
L
6802 BFD_ASSERT (htab->root.target_os == is_vxworks
6803 || h->global_got_area != GGA_NONE);
5d41f0b6
RS
6804 indx = h->root.dynindx;
6805 if (SGI_COMPAT (output_bfd))
6806 defined_p = h->root.def_regular;
6807 else
6808 /* ??? glibc's ld.so just adds the final GOT entry to the
6809 relocation field. It therefore treats relocs against
6810 defined symbols in the same way as relocs against
6811 undefined symbols. */
0a1b45a2 6812 defined_p = false;
5d41f0b6 6813 }
b49e97c9
TS
6814 else
6815 {
5d41f0b6
RS
6816 if (sec != NULL && bfd_is_abs_section (sec))
6817 indx = 0;
6818 else if (sec == NULL || sec->owner == NULL)
fdd07405 6819 {
5d41f0b6 6820 bfd_set_error (bfd_error_bad_value);
0a1b45a2 6821 return false;
b49e97c9
TS
6822 }
6823 else
6824 {
5d41f0b6 6825 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
6826 if (indx == 0)
6827 {
6828 asection *osec = htab->root.text_index_section;
6829 indx = elf_section_data (osec)->dynindx;
6830 }
5d41f0b6
RS
6831 if (indx == 0)
6832 abort ();
b49e97c9
TS
6833 }
6834
5d41f0b6
RS
6835 /* Instead of generating a relocation using the section
6836 symbol, we may as well make it a fully relative
6837 relocation. We want to avoid generating relocations to
6838 local symbols because we used to generate them
6839 incorrectly, without adding the original symbol value,
6840 which is mandated by the ABI for section symbols. In
6841 order to give dynamic loaders and applications time to
6842 phase out the incorrect use, we refrain from emitting
6843 section-relative relocations. It's not like they're
6844 useful, after all. This should be a bit more efficient
6845 as well. */
6846 /* ??? Although this behavior is compatible with glibc's ld.so,
6847 the ABI says that relocations against STN_UNDEF should have
6848 a symbol value of 0. Irix rld honors this, so relocations
6849 against STN_UNDEF have no effect. */
6850 if (!SGI_COMPAT (output_bfd))
6851 indx = 0;
0a1b45a2 6852 defined_p = true;
b49e97c9
TS
6853 }
6854
5d41f0b6
RS
6855 /* If the relocation was previously an absolute relocation and
6856 this symbol will not be referred to by the relocation, we must
6857 adjust it by the value we give it in the dynamic symbol table.
6858 Otherwise leave the job up to the dynamic linker. */
6859 if (defined_p && r_type != R_MIPS_REL32)
6860 *addendp += symbol;
6861
90c14f0c 6862 if (htab->root.target_os == is_vxworks)
0a44bf69
RS
6863 /* VxWorks uses non-relative relocations for this. */
6864 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6865 else
6866 /* The relocation is always an REL32 relocation because we don't
6867 know where the shared library will wind up at load-time. */
6868 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6869 R_MIPS_REL32);
6870
5d41f0b6
RS
6871 /* For strict adherence to the ABI specification, we should
6872 generate a R_MIPS_64 relocation record by itself before the
6873 _REL32/_64 record as well, such that the addend is read in as
6874 a 64-bit value (REL32 is a 32-bit relocation, after all).
6875 However, since none of the existing ELF64 MIPS dynamic
6876 loaders seems to care, we don't waste space with these
6877 artificial relocations. If this turns out to not be true,
6878 mips_elf_allocate_dynamic_relocation() should be tweaked so
6879 as to make room for a pair of dynamic relocations per
6880 invocation if ABI_64_P, and here we should generate an
6881 additional relocation record with R_MIPS_64 by itself for a
6882 NULL symbol before this relocation record. */
6883 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6884 ABI_64_P (output_bfd)
6885 ? R_MIPS_64
6886 : R_MIPS_NONE);
6887 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6888
6889 /* Adjust the output offset of the relocation to reference the
6890 correct location in the output file. */
6891 outrel[0].r_offset += (input_section->output_section->vma
6892 + input_section->output_offset);
6893 outrel[1].r_offset += (input_section->output_section->vma
6894 + input_section->output_offset);
6895 outrel[2].r_offset += (input_section->output_section->vma
6896 + input_section->output_offset);
6897
b49e97c9
TS
6898 /* Put the relocation back out. We have to use the special
6899 relocation outputter in the 64-bit case since the 64-bit
6900 relocation format is non-standard. */
6901 if (ABI_64_P (output_bfd))
6902 {
6903 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6904 (output_bfd, &outrel[0],
6905 (sreloc->contents
6906 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6907 }
90c14f0c 6908 else if (htab->root.target_os == is_vxworks)
0a44bf69
RS
6909 {
6910 /* VxWorks uses RELA rather than REL dynamic relocations. */
6911 outrel[0].r_addend = *addendp;
6912 bfd_elf32_swap_reloca_out
6913 (output_bfd, &outrel[0],
6914 (sreloc->contents
6915 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6916 }
b49e97c9 6917 else
947216bf
AM
6918 bfd_elf32_swap_reloc_out
6919 (output_bfd, &outrel[0],
6920 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 6921
b49e97c9
TS
6922 /* We've now added another relocation. */
6923 ++sreloc->reloc_count;
6924
6925 /* Make sure the output section is writable. The dynamic linker
6926 will be writing to it. */
6927 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6928 |= SHF_WRITE;
6929
6930 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 6931 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9 6932 {
3d4d4302 6933 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
6934 bfd_byte *cr;
6935
6936 if (scpt)
6937 {
6938 Elf32_crinfo cptrel;
6939
6940 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6941 cptrel.vaddr = (rel->r_offset
6942 + input_section->output_section->vma
6943 + input_section->output_offset);
6944 if (r_type == R_MIPS_REL32)
6945 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6946 else
6947 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6948 mips_elf_set_cr_dist2to (cptrel, 0);
6949 cptrel.konst = *addendp;
6950
6951 cr = (scpt->contents
6952 + sizeof (Elf32_External_compact_rel));
abc0f8d0 6953 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
6954 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6955 ((Elf32_External_crinfo *) cr
6956 + scpt->reloc_count));
6957 ++scpt->reloc_count;
6958 }
6959 }
6960
943284cc
DJ
6961 /* If we've written this relocation for a readonly section,
6962 we need to set DF_TEXTREL again, so that we do not delete the
6963 DT_TEXTREL tag. */
6964 if (MIPS_ELF_READONLY_SECTION (input_section))
6965 info->flags |= DF_TEXTREL;
6966
0a1b45a2 6967 return true;
b49e97c9
TS
6968}
6969\f
b49e97c9
TS
6970/* Return the MACH for a MIPS e_flags value. */
6971
6972unsigned long
9719ad41 6973_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
6974{
6975 switch (flags & EF_MIPS_MACH)
6976 {
6977 case E_MIPS_MACH_3900:
6978 return bfd_mach_mips3900;
6979
6980 case E_MIPS_MACH_4010:
6981 return bfd_mach_mips4010;
6982
6983 case E_MIPS_MACH_4100:
6984 return bfd_mach_mips4100;
6985
6986 case E_MIPS_MACH_4111:
6987 return bfd_mach_mips4111;
6988
00707a0e
RS
6989 case E_MIPS_MACH_4120:
6990 return bfd_mach_mips4120;
6991
b49e97c9
TS
6992 case E_MIPS_MACH_4650:
6993 return bfd_mach_mips4650;
6994
00707a0e
RS
6995 case E_MIPS_MACH_5400:
6996 return bfd_mach_mips5400;
6997
6998 case E_MIPS_MACH_5500:
6999 return bfd_mach_mips5500;
7000
e407c74b
NC
7001 case E_MIPS_MACH_5900:
7002 return bfd_mach_mips5900;
7003
0d2e43ed
ILT
7004 case E_MIPS_MACH_9000:
7005 return bfd_mach_mips9000;
7006
b49e97c9
TS
7007 case E_MIPS_MACH_SB1:
7008 return bfd_mach_mips_sb1;
7009
350cc38d
MS
7010 case E_MIPS_MACH_LS2E:
7011 return bfd_mach_mips_loongson_2e;
7012
7013 case E_MIPS_MACH_LS2F:
7014 return bfd_mach_mips_loongson_2f;
7015
ac8cb70f
CX
7016 case E_MIPS_MACH_GS464:
7017 return bfd_mach_mips_gs464;
fd503541 7018
bd782c07
CX
7019 case E_MIPS_MACH_GS464E:
7020 return bfd_mach_mips_gs464e;
7021
9108bc33
CX
7022 case E_MIPS_MACH_GS264E:
7023 return bfd_mach_mips_gs264e;
7024
2c629856
N
7025 case E_MIPS_MACH_OCTEON3:
7026 return bfd_mach_mips_octeon3;
7027
432233b3
AP
7028 case E_MIPS_MACH_OCTEON2:
7029 return bfd_mach_mips_octeon2;
7030
6f179bd0
AN
7031 case E_MIPS_MACH_OCTEON:
7032 return bfd_mach_mips_octeon;
7033
52b6b6b9
JM
7034 case E_MIPS_MACH_XLR:
7035 return bfd_mach_mips_xlr;
7036
38bf472a
MR
7037 case E_MIPS_MACH_IAMR2:
7038 return bfd_mach_mips_interaptiv_mr2;
7039
b49e97c9
TS
7040 default:
7041 switch (flags & EF_MIPS_ARCH)
7042 {
7043 default:
7044 case E_MIPS_ARCH_1:
7045 return bfd_mach_mips3000;
b49e97c9
TS
7046
7047 case E_MIPS_ARCH_2:
7048 return bfd_mach_mips6000;
b49e97c9
TS
7049
7050 case E_MIPS_ARCH_3:
7051 return bfd_mach_mips4000;
b49e97c9
TS
7052
7053 case E_MIPS_ARCH_4:
7054 return bfd_mach_mips8000;
b49e97c9
TS
7055
7056 case E_MIPS_ARCH_5:
7057 return bfd_mach_mips5;
b49e97c9
TS
7058
7059 case E_MIPS_ARCH_32:
7060 return bfd_mach_mipsisa32;
b49e97c9
TS
7061
7062 case E_MIPS_ARCH_64:
7063 return bfd_mach_mipsisa64;
af7ee8bf
CD
7064
7065 case E_MIPS_ARCH_32R2:
7066 return bfd_mach_mipsisa32r2;
5f74bc13
CD
7067
7068 case E_MIPS_ARCH_64R2:
7069 return bfd_mach_mipsisa64r2;
7361da2c
AB
7070
7071 case E_MIPS_ARCH_32R6:
7072 return bfd_mach_mipsisa32r6;
7073
7074 case E_MIPS_ARCH_64R6:
7075 return bfd_mach_mipsisa64r6;
b49e97c9
TS
7076 }
7077 }
7078
7079 return 0;
7080}
7081
7082/* Return printable name for ABI. */
7083
42eec46f 7084static inline char *
9719ad41 7085elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
7086{
7087 flagword flags;
7088
7089 flags = elf_elfheader (abfd)->e_flags;
7090 switch (flags & EF_MIPS_ABI)
7091 {
7092 case 0:
7093 if (ABI_N32_P (abfd))
7094 return "N32";
7095 else if (ABI_64_P (abfd))
7096 return "64";
7097 else
7098 return "none";
7099 case E_MIPS_ABI_O32:
7100 return "O32";
7101 case E_MIPS_ABI_O64:
7102 return "O64";
7103 case E_MIPS_ABI_EABI32:
7104 return "EABI32";
7105 case E_MIPS_ABI_EABI64:
7106 return "EABI64";
7107 default:
7108 return "unknown abi";
7109 }
7110}
7111\f
7112/* MIPS ELF uses two common sections. One is the usual one, and the
7113 other is for small objects. All the small objects are kept
7114 together, and then referenced via the gp pointer, which yields
7115 faster assembler code. This is what we use for the small common
7116 section. This approach is copied from ecoff.c. */
7117static asection mips_elf_scom_section;
7f3a18cf
AM
7118static const asymbol mips_elf_scom_symbol =
7119 GLOBAL_SYM_INIT (".scommon", &mips_elf_scom_section);
7120static asection mips_elf_scom_section =
7121 BFD_FAKE_SECTION (mips_elf_scom_section, &mips_elf_scom_symbol,
7122 ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
b49e97c9
TS
7123
7124/* MIPS ELF also uses an acommon section, which represents an
7125 allocated common symbol which may be overridden by a
7126 definition in a shared library. */
7127static asection mips_elf_acom_section;
7f3a18cf
AM
7128static const asymbol mips_elf_acom_symbol =
7129 GLOBAL_SYM_INIT (".acommon", &mips_elf_acom_section);
7130static asection mips_elf_acom_section =
7131 BFD_FAKE_SECTION (mips_elf_acom_section, &mips_elf_acom_symbol,
7132 ".acommon", 0, SEC_ALLOC);
b49e97c9 7133
738e5348 7134/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
7135
7136void
9719ad41 7137_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
7138{
7139 elf_symbol_type *elfsym;
7140
738e5348 7141 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
7142 elfsym = (elf_symbol_type *) asym;
7143 switch (elfsym->internal_elf_sym.st_shndx)
7144 {
7145 case SHN_MIPS_ACOMMON:
7146 /* This section is used in a dynamically linked executable file.
7147 It is an allocated common section. The dynamic linker can
7148 either resolve these symbols to something in a shared
7149 library, or it can just leave them here. For our purposes,
7150 we can consider these symbols to be in a new section. */
b49e97c9
TS
7151 asym->section = &mips_elf_acom_section;
7152 break;
7153
7154 case SHN_COMMON:
7155 /* Common symbols less than the GP size are automatically
7156 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
7157 if (asym->value > elf_gp_size (abfd)
b59eed79 7158 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
7159 || IRIX_COMPAT (abfd) == ict_irix6)
7160 break;
7161 /* Fall through. */
7162 case SHN_MIPS_SCOMMON:
b49e97c9
TS
7163 asym->section = &mips_elf_scom_section;
7164 asym->value = elfsym->internal_elf_sym.st_size;
7165 break;
7166
7167 case SHN_MIPS_SUNDEFINED:
7168 asym->section = bfd_und_section_ptr;
7169 break;
7170
b49e97c9 7171 case SHN_MIPS_TEXT:
00b4930b
TS
7172 {
7173 asection *section = bfd_get_section_by_name (abfd, ".text");
7174
00b4930b
TS
7175 if (section != NULL)
7176 {
7177 asym->section = section;
7178 /* MIPS_TEXT is a bit special, the address is not an offset
de194d85 7179 to the base of the .text section. So subtract the section
00b4930b
TS
7180 base address to make it an offset. */
7181 asym->value -= section->vma;
7182 }
7183 }
b49e97c9
TS
7184 break;
7185
7186 case SHN_MIPS_DATA:
00b4930b
TS
7187 {
7188 asection *section = bfd_get_section_by_name (abfd, ".data");
7189
00b4930b
TS
7190 if (section != NULL)
7191 {
7192 asym->section = section;
7193 /* MIPS_DATA is a bit special, the address is not an offset
de194d85 7194 to the base of the .data section. So subtract the section
00b4930b
TS
7195 base address to make it an offset. */
7196 asym->value -= section->vma;
7197 }
7198 }
b49e97c9 7199 break;
b49e97c9 7200 }
738e5348 7201
df58fc94
RS
7202 /* If this is an odd-valued function symbol, assume it's a MIPS16
7203 or microMIPS one. */
738e5348
RS
7204 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7205 && (asym->value & 1) != 0)
7206 {
7207 asym->value--;
e8faf7d1 7208 if (MICROMIPS_P (abfd))
df58fc94
RS
7209 elfsym->internal_elf_sym.st_other
7210 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7211 else
7212 elfsym->internal_elf_sym.st_other
7213 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
738e5348 7214 }
b49e97c9
TS
7215}
7216\f
8c946ed5
RS
7217/* Implement elf_backend_eh_frame_address_size. This differs from
7218 the default in the way it handles EABI64.
7219
7220 EABI64 was originally specified as an LP64 ABI, and that is what
7221 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7222 historically accepted the combination of -mabi=eabi and -mlong32,
7223 and this ILP32 variation has become semi-official over time.
7224 Both forms use elf32 and have pointer-sized FDE addresses.
7225
7226 If an EABI object was generated by GCC 4.0 or above, it will have
7227 an empty .gcc_compiled_longXX section, where XX is the size of longs
7228 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7229 have no special marking to distinguish them from LP64 objects.
7230
7231 We don't want users of the official LP64 ABI to be punished for the
7232 existence of the ILP32 variant, but at the same time, we don't want
7233 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7234 We therefore take the following approach:
7235
7236 - If ABFD contains a .gcc_compiled_longXX section, use it to
07d6d2b8 7237 determine the pointer size.
8c946ed5
RS
7238
7239 - Otherwise check the type of the first relocation. Assume that
07d6d2b8 7240 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
8c946ed5
RS
7241
7242 - Otherwise punt.
7243
7244 The second check is enough to detect LP64 objects generated by pre-4.0
7245 compilers because, in the kind of output generated by those compilers,
7246 the first relocation will be associated with either a CIE personality
7247 routine or an FDE start address. Furthermore, the compilers never
7248 used a special (non-pointer) encoding for this ABI.
7249
7250 Checking the relocation type should also be safe because there is no
7251 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7252 did so. */
7253
7254unsigned int
76c20d54 7255_bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
8c946ed5
RS
7256{
7257 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7258 return 8;
7259 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7260 {
0a1b45a2 7261 bool long32_p, long64_p;
8c946ed5
RS
7262
7263 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7264 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7265 if (long32_p && long64_p)
7266 return 0;
7267 if (long32_p)
7268 return 4;
7269 if (long64_p)
7270 return 8;
7271
7272 if (sec->reloc_count > 0
7273 && elf_section_data (sec)->relocs != NULL
7274 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7275 == R_MIPS_64))
7276 return 8;
7277
7278 return 0;
7279 }
7280 return 4;
7281}
7282\f
174fd7f9
RS
7283/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7284 relocations against two unnamed section symbols to resolve to the
7285 same address. For example, if we have code like:
7286
7287 lw $4,%got_disp(.data)($gp)
7288 lw $25,%got_disp(.text)($gp)
7289 jalr $25
7290
7291 then the linker will resolve both relocations to .data and the program
7292 will jump there rather than to .text.
7293
7294 We can work around this problem by giving names to local section symbols.
7295 This is also what the MIPSpro tools do. */
7296
0a1b45a2 7297bool
174fd7f9
RS
7298_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7299{
c77cb2a0 7300 return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
174fd7f9
RS
7301}
7302\f
b49e97c9
TS
7303/* Work over a section just before writing it out. This routine is
7304 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7305 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7306 a better way. */
7307
0a1b45a2 7308bool
9719ad41 7309_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
7310{
7311 if (hdr->sh_type == SHT_MIPS_REGINFO
7312 && hdr->sh_size > 0)
7313 {
7314 bfd_byte buf[4];
7315
b49e97c9
TS
7316 BFD_ASSERT (hdr->contents == NULL);
7317
2d6dda71
MR
7318 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7319 {
7320 _bfd_error_handler
2c1c9679 7321 (_("%pB: incorrect `.reginfo' section size; "
2dcf00ce
AM
7322 "expected %" PRIu64 ", got %" PRIu64),
7323 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7324 (uint64_t) hdr->sh_size);
2d6dda71 7325 bfd_set_error (bfd_error_bad_value);
0a1b45a2 7326 return false;
2d6dda71
MR
7327 }
7328
b49e97c9
TS
7329 if (bfd_seek (abfd,
7330 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7331 SEEK_SET) != 0)
0a1b45a2 7332 return false;
b49e97c9 7333 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 7334 if (bfd_bwrite (buf, 4, abfd) != 4)
0a1b45a2 7335 return false;
b49e97c9
TS
7336 }
7337
7338 if (hdr->sh_type == SHT_MIPS_OPTIONS
7339 && hdr->bfd_section != NULL
f0abc2a1
AM
7340 && mips_elf_section_data (hdr->bfd_section) != NULL
7341 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
7342 {
7343 bfd_byte *contents, *l, *lend;
7344
f0abc2a1
AM
7345 /* We stored the section contents in the tdata field in the
7346 set_section_contents routine. We save the section contents
7347 so that we don't have to read them again.
b49e97c9
TS
7348 At this point we know that elf_gp is set, so we can look
7349 through the section contents to see if there is an
7350 ODK_REGINFO structure. */
7351
f0abc2a1 7352 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
7353 l = contents;
7354 lend = contents + hdr->sh_size;
7355 while (l + sizeof (Elf_External_Options) <= lend)
7356 {
7357 Elf_Internal_Options intopt;
7358
7359 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7360 &intopt);
1bc8074d
MR
7361 if (intopt.size < sizeof (Elf_External_Options))
7362 {
4eca0228 7363 _bfd_error_handler
695344c0 7364 /* xgettext:c-format */
2c1c9679 7365 (_("%pB: warning: bad `%s' option size %u smaller than"
63a5468a 7366 " its header"),
1bc8074d
MR
7367 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7368 break;
7369 }
b49e97c9
TS
7370 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7371 {
7372 bfd_byte buf[8];
7373
7374 if (bfd_seek (abfd,
7375 (hdr->sh_offset
7376 + (l - contents)
7377 + sizeof (Elf_External_Options)
7378 + (sizeof (Elf64_External_RegInfo) - 8)),
7379 SEEK_SET) != 0)
0a1b45a2 7380 return false;
b49e97c9 7381 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 7382 if (bfd_bwrite (buf, 8, abfd) != 8)
0a1b45a2 7383 return false;
b49e97c9
TS
7384 }
7385 else if (intopt.kind == ODK_REGINFO)
7386 {
7387 bfd_byte buf[4];
7388
7389 if (bfd_seek (abfd,
7390 (hdr->sh_offset
7391 + (l - contents)
7392 + sizeof (Elf_External_Options)
7393 + (sizeof (Elf32_External_RegInfo) - 4)),
7394 SEEK_SET) != 0)
0a1b45a2 7395 return false;
b49e97c9 7396 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 7397 if (bfd_bwrite (buf, 4, abfd) != 4)
0a1b45a2 7398 return false;
b49e97c9
TS
7399 }
7400 l += intopt.size;
7401 }
7402 }
7403
7404 if (hdr->bfd_section != NULL)
7405 {
fd361982 7406 const char *name = bfd_section_name (hdr->bfd_section);
b49e97c9 7407
2d0f9ad9
JM
7408 /* .sbss is not handled specially here because the GNU/Linux
7409 prelinker can convert .sbss from NOBITS to PROGBITS and
7410 changing it back to NOBITS breaks the binary. The entry in
7411 _bfd_mips_elf_special_sections will ensure the correct flags
7412 are set on .sbss if BFD creates it without reading it from an
7413 input file, and without special handling here the flags set
7414 on it in an input file will be followed. */
b49e97c9
TS
7415 if (strcmp (name, ".sdata") == 0
7416 || strcmp (name, ".lit8") == 0
7417 || strcmp (name, ".lit4") == 0)
fd6f9d17 7418 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
b49e97c9 7419 else if (strcmp (name, ".srdata") == 0)
fd6f9d17 7420 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
b49e97c9 7421 else if (strcmp (name, ".compact_rel") == 0)
fd6f9d17 7422 hdr->sh_flags = 0;
b49e97c9
TS
7423 else if (strcmp (name, ".rtproc") == 0)
7424 {
7425 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7426 {
7427 unsigned int adjust;
7428
7429 adjust = hdr->sh_size % hdr->sh_addralign;
7430 if (adjust != 0)
7431 hdr->sh_size += hdr->sh_addralign - adjust;
7432 }
7433 }
7434 }
7435
0a1b45a2 7436 return true;
b49e97c9
TS
7437}
7438
7439/* Handle a MIPS specific section when reading an object file. This
7440 is called when elfcode.h finds a section with an unknown type.
bf577467 7441 This routine supports both the 32-bit and 64-bit ELF ABI. */
b49e97c9 7442
0a1b45a2 7443bool
6dc132d9
L
7444_bfd_mips_elf_section_from_shdr (bfd *abfd,
7445 Elf_Internal_Shdr *hdr,
7446 const char *name,
7447 int shindex)
b49e97c9
TS
7448{
7449 flagword flags = 0;
7450
7451 /* There ought to be a place to keep ELF backend specific flags, but
7452 at the moment there isn't one. We just keep track of the
7453 sections by their name, instead. Fortunately, the ABI gives
7454 suggested names for all the MIPS specific sections, so we will
7455 probably get away with this. */
7456 switch (hdr->sh_type)
7457 {
7458 case SHT_MIPS_LIBLIST:
7459 if (strcmp (name, ".liblist") != 0)
0a1b45a2 7460 return false;
b49e97c9
TS
7461 break;
7462 case SHT_MIPS_MSYM:
7463 if (strcmp (name, ".msym") != 0)
0a1b45a2 7464 return false;
b49e97c9
TS
7465 break;
7466 case SHT_MIPS_CONFLICT:
7467 if (strcmp (name, ".conflict") != 0)
0a1b45a2 7468 return false;
b49e97c9
TS
7469 break;
7470 case SHT_MIPS_GPTAB:
08dedd66 7471 if (! startswith (name, ".gptab."))
0a1b45a2 7472 return false;
b49e97c9
TS
7473 break;
7474 case SHT_MIPS_UCODE:
7475 if (strcmp (name, ".ucode") != 0)
0a1b45a2 7476 return false;
b49e97c9
TS
7477 break;
7478 case SHT_MIPS_DEBUG:
7479 if (strcmp (name, ".mdebug") != 0)
0a1b45a2 7480 return false;
b49e97c9
TS
7481 flags = SEC_DEBUGGING;
7482 break;
7483 case SHT_MIPS_REGINFO:
7484 if (strcmp (name, ".reginfo") != 0
7485 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
0a1b45a2 7486 return false;
b49e97c9
TS
7487 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7488 break;
7489 case SHT_MIPS_IFACE:
7490 if (strcmp (name, ".MIPS.interfaces") != 0)
0a1b45a2 7491 return false;
b49e97c9
TS
7492 break;
7493 case SHT_MIPS_CONTENT:
08dedd66 7494 if (! startswith (name, ".MIPS.content"))
0a1b45a2 7495 return false;
b49e97c9
TS
7496 break;
7497 case SHT_MIPS_OPTIONS:
cc2e31b9 7498 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
0a1b45a2 7499 return false;
b49e97c9 7500 break;
351cdf24
MF
7501 case SHT_MIPS_ABIFLAGS:
7502 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
0a1b45a2 7503 return false;
351cdf24
MF
7504 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7505 break;
b49e97c9 7506 case SHT_MIPS_DWARF:
08dedd66 7507 if (! startswith (name, ".debug_")
9a39f738
YS
7508 && ! startswith (name, ".gnu.debuglto_.debug_")
7509 && ! startswith (name, ".zdebug_")
7510 && ! startswith (name, ".gnu.debuglto_.zdebug_"))
0a1b45a2 7511 return false;
b49e97c9
TS
7512 break;
7513 case SHT_MIPS_SYMBOL_LIB:
7514 if (strcmp (name, ".MIPS.symlib") != 0)
0a1b45a2 7515 return false;
b49e97c9
TS
7516 break;
7517 case SHT_MIPS_EVENTS:
08dedd66
ML
7518 if (! startswith (name, ".MIPS.events")
7519 && ! startswith (name, ".MIPS.post_rel"))
0a1b45a2 7520 return false;
b49e97c9 7521 break;
f16a9783
MS
7522 case SHT_MIPS_XHASH:
7523 if (strcmp (name, ".MIPS.xhash") != 0)
0a1b45a2 7524 return false;
b49e97c9 7525 default:
cc2e31b9 7526 break;
b49e97c9
TS
7527 }
7528
6dc132d9 7529 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
0a1b45a2 7530 return false;
b49e97c9 7531
bf577467
AM
7532 if (hdr->sh_flags & SHF_MIPS_GPREL)
7533 flags |= SEC_SMALL_DATA;
7534
b49e97c9
TS
7535 if (flags)
7536 {
fd361982
AM
7537 if (!bfd_set_section_flags (hdr->bfd_section,
7538 (bfd_section_flags (hdr->bfd_section)
7539 | flags)))
0a1b45a2 7540 return false;
b49e97c9
TS
7541 }
7542
351cdf24
MF
7543 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7544 {
7545 Elf_External_ABIFlags_v0 ext;
7546
7547 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7548 &ext, 0, sizeof ext))
0a1b45a2 7549 return false;
351cdf24
MF
7550 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7551 &mips_elf_tdata (abfd)->abiflags);
7552 if (mips_elf_tdata (abfd)->abiflags.version != 0)
0a1b45a2
AM
7553 return false;
7554 mips_elf_tdata (abfd)->abiflags_valid = true;
351cdf24
MF
7555 }
7556
b49e97c9
TS
7557 /* FIXME: We should record sh_info for a .gptab section. */
7558
7559 /* For a .reginfo section, set the gp value in the tdata information
7560 from the contents of this section. We need the gp value while
7561 processing relocs, so we just get it now. The .reginfo section
7562 is not used in the 64-bit MIPS ELF ABI. */
7563 if (hdr->sh_type == SHT_MIPS_REGINFO)
7564 {
7565 Elf32_External_RegInfo ext;
7566 Elf32_RegInfo s;
7567
9719ad41
RS
7568 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7569 &ext, 0, sizeof ext))
0a1b45a2 7570 return false;
b49e97c9
TS
7571 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7572 elf_gp (abfd) = s.ri_gp_value;
7573 }
7574
7575 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7576 set the gp value based on what we find. We may see both
7577 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7578 they should agree. */
7579 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7580 {
7581 bfd_byte *contents, *l, *lend;
7582
d0e5049d 7583 if (!bfd_malloc_and_get_section (abfd, hdr->bfd_section, &contents))
b49e97c9
TS
7584 {
7585 free (contents);
0a1b45a2 7586 return false;
b49e97c9
TS
7587 }
7588 l = contents;
7589 lend = contents + hdr->sh_size;
7590 while (l + sizeof (Elf_External_Options) <= lend)
7591 {
7592 Elf_Internal_Options intopt;
7593
7594 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7595 &intopt);
1bc8074d
MR
7596 if (intopt.size < sizeof (Elf_External_Options))
7597 {
3dc8d5ea 7598 bad_opt:
4eca0228 7599 _bfd_error_handler
695344c0 7600 /* xgettext:c-format */
3dc8d5ea
AM
7601 (_("%pB: warning: truncated `%s' option"),
7602 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd));
1bc8074d
MR
7603 break;
7604 }
3dc8d5ea 7605 if (intopt.kind == ODK_REGINFO)
b49e97c9 7606 {
3dc8d5ea
AM
7607 if (ABI_64_P (abfd))
7608 {
7609 Elf64_Internal_RegInfo intreg;
7610 size_t needed = (sizeof (Elf_External_Options)
7611 + sizeof (Elf64_External_RegInfo));
7612 if (intopt.size < needed || (size_t) (lend - l) < needed)
7613 goto bad_opt;
7614 bfd_mips_elf64_swap_reginfo_in
7615 (abfd,
7616 ((Elf64_External_RegInfo *)
7617 (l + sizeof (Elf_External_Options))),
7618 &intreg);
7619 elf_gp (abfd) = intreg.ri_gp_value;
7620 }
7621 else
7622 {
7623 Elf32_RegInfo intreg;
7624 size_t needed = (sizeof (Elf_External_Options)
7625 + sizeof (Elf32_External_RegInfo));
7626 if (intopt.size < needed || (size_t) (lend - l) < needed)
7627 goto bad_opt;
7628 bfd_mips_elf32_swap_reginfo_in
7629 (abfd,
7630 ((Elf32_External_RegInfo *)
7631 (l + sizeof (Elf_External_Options))),
7632 &intreg);
7633 elf_gp (abfd) = intreg.ri_gp_value;
7634 }
b49e97c9
TS
7635 }
7636 l += intopt.size;
7637 }
7638 free (contents);
7639 }
7640
0a1b45a2 7641 return true;
b49e97c9
TS
7642}
7643
7644/* Set the correct type for a MIPS ELF section. We do this by the
7645 section name, which is a hack, but ought to work. This routine is
7646 used by both the 32-bit and the 64-bit ABI. */
7647
0a1b45a2 7648bool
9719ad41 7649_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 7650{
fd361982 7651 const char *name = bfd_section_name (sec);
b49e97c9
TS
7652
7653 if (strcmp (name, ".liblist") == 0)
7654 {
7655 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 7656 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
7657 /* The sh_link field is set in final_write_processing. */
7658 }
7659 else if (strcmp (name, ".conflict") == 0)
7660 hdr->sh_type = SHT_MIPS_CONFLICT;
08dedd66 7661 else if (startswith (name, ".gptab."))
b49e97c9
TS
7662 {
7663 hdr->sh_type = SHT_MIPS_GPTAB;
7664 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7665 /* The sh_info field is set in final_write_processing. */
7666 }
7667 else if (strcmp (name, ".ucode") == 0)
7668 hdr->sh_type = SHT_MIPS_UCODE;
7669 else if (strcmp (name, ".mdebug") == 0)
7670 {
7671 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 7672 /* In a shared object on IRIX 5.3, the .mdebug section has an
07d6d2b8 7673 entsize of 0. FIXME: Does this matter? */
b49e97c9
TS
7674 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7675 hdr->sh_entsize = 0;
7676 else
7677 hdr->sh_entsize = 1;
7678 }
7679 else if (strcmp (name, ".reginfo") == 0)
7680 {
7681 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 7682 /* In a shared object on IRIX 5.3, the .reginfo section has an
07d6d2b8 7683 entsize of 0x18. FIXME: Does this matter? */
b49e97c9
TS
7684 if (SGI_COMPAT (abfd))
7685 {
7686 if ((abfd->flags & DYNAMIC) != 0)
7687 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7688 else
7689 hdr->sh_entsize = 1;
7690 }
7691 else
7692 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7693 }
7694 else if (SGI_COMPAT (abfd)
7695 && (strcmp (name, ".hash") == 0
7696 || strcmp (name, ".dynamic") == 0
7697 || strcmp (name, ".dynstr") == 0))
7698 {
7699 if (SGI_COMPAT (abfd))
7700 hdr->sh_entsize = 0;
7701#if 0
8dc1a139 7702 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
7703 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7704#endif
7705 }
7706 else if (strcmp (name, ".got") == 0
7707 || strcmp (name, ".srdata") == 0
7708 || strcmp (name, ".sdata") == 0
7709 || strcmp (name, ".sbss") == 0
7710 || strcmp (name, ".lit4") == 0
7711 || strcmp (name, ".lit8") == 0)
7712 hdr->sh_flags |= SHF_MIPS_GPREL;
7713 else if (strcmp (name, ".MIPS.interfaces") == 0)
7714 {
7715 hdr->sh_type = SHT_MIPS_IFACE;
7716 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7717 }
08dedd66 7718 else if (startswith (name, ".MIPS.content"))
b49e97c9
TS
7719 {
7720 hdr->sh_type = SHT_MIPS_CONTENT;
7721 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7722 /* The sh_info field is set in final_write_processing. */
7723 }
cc2e31b9 7724 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
7725 {
7726 hdr->sh_type = SHT_MIPS_OPTIONS;
7727 hdr->sh_entsize = 1;
7728 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7729 }
08dedd66 7730 else if (startswith (name, ".MIPS.abiflags"))
351cdf24
MF
7731 {
7732 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7733 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7734 }
08dedd66 7735 else if (startswith (name, ".debug_")
9a39f738
YS
7736 || startswith (name, ".gnu.debuglto_.debug_")
7737 || startswith (name, ".zdebug_")
7738 || startswith (name, ".gnu.debuglto_.zdebug_"))
b5482f21
NC
7739 {
7740 hdr->sh_type = SHT_MIPS_DWARF;
7741
7742 /* Irix facilities such as libexc expect a single .debug_frame
7743 per executable, the system ones have NOSTRIP set and the linker
7744 doesn't merge sections with different flags so ... */
08dedd66 7745 if (SGI_COMPAT (abfd) && startswith (name, ".debug_frame"))
b5482f21
NC
7746 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7747 }
b49e97c9
TS
7748 else if (strcmp (name, ".MIPS.symlib") == 0)
7749 {
7750 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7751 /* The sh_link and sh_info fields are set in
07d6d2b8 7752 final_write_processing. */
b49e97c9 7753 }
08dedd66
ML
7754 else if (startswith (name, ".MIPS.events")
7755 || startswith (name, ".MIPS.post_rel"))
b49e97c9
TS
7756 {
7757 hdr->sh_type = SHT_MIPS_EVENTS;
7758 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7759 /* The sh_link field is set in final_write_processing. */
7760 }
7761 else if (strcmp (name, ".msym") == 0)
7762 {
7763 hdr->sh_type = SHT_MIPS_MSYM;
7764 hdr->sh_flags |= SHF_ALLOC;
7765 hdr->sh_entsize = 8;
7766 }
f16a9783
MS
7767 else if (strcmp (name, ".MIPS.xhash") == 0)
7768 {
7769 hdr->sh_type = SHT_MIPS_XHASH;
7770 hdr->sh_flags |= SHF_ALLOC;
7771 hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7772 }
b49e97c9 7773
7a79a000
TS
7774 /* The generic elf_fake_sections will set up REL_HDR using the default
7775 kind of relocations. We used to set up a second header for the
7776 non-default kind of relocations here, but only NewABI would use
7777 these, and the IRIX ld doesn't like resulting empty RELA sections.
7778 Thus we create those header only on demand now. */
b49e97c9 7779
0a1b45a2 7780 return true;
b49e97c9
TS
7781}
7782
7783/* Given a BFD section, try to locate the corresponding ELF section
7784 index. This is used by both the 32-bit and the 64-bit ABI.
7785 Actually, it's not clear to me that the 64-bit ABI supports these,
7786 but for non-PIC objects we will certainly want support for at least
7787 the .scommon section. */
7788
0a1b45a2 7789bool
9719ad41
RS
7790_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7791 asection *sec, int *retval)
b49e97c9 7792{
fd361982 7793 if (strcmp (bfd_section_name (sec), ".scommon") == 0)
b49e97c9
TS
7794 {
7795 *retval = SHN_MIPS_SCOMMON;
0a1b45a2 7796 return true;
b49e97c9 7797 }
fd361982 7798 if (strcmp (bfd_section_name (sec), ".acommon") == 0)
b49e97c9
TS
7799 {
7800 *retval = SHN_MIPS_ACOMMON;
0a1b45a2 7801 return true;
b49e97c9 7802 }
0a1b45a2 7803 return false;
b49e97c9
TS
7804}
7805\f
7806/* Hook called by the linker routine which adds symbols from an object
7807 file. We must handle the special MIPS section numbers here. */
7808
0a1b45a2 7809bool
9719ad41 7810_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 7811 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
7812 flagword *flagsp ATTRIBUTE_UNUSED,
7813 asection **secp, bfd_vma *valp)
b49e97c9
TS
7814{
7815 if (SGI_COMPAT (abfd)
7816 && (abfd->flags & DYNAMIC) != 0
7817 && strcmp (*namep, "_rld_new_interface") == 0)
7818 {
8dc1a139 7819 /* Skip IRIX5 rld entry name. */
b49e97c9 7820 *namep = NULL;
0a1b45a2 7821 return true;
b49e97c9
TS
7822 }
7823
eedecc07
DD
7824 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7825 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7826 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7827 a magic symbol resolved by the linker, we ignore this bogus definition
7828 of _gp_disp. New ABI objects do not suffer from this problem so this
7829 is not done for them. */
7830 if (!NEWABI_P(abfd)
7831 && (sym->st_shndx == SHN_ABS)
7832 && (strcmp (*namep, "_gp_disp") == 0))
7833 {
7834 *namep = NULL;
0a1b45a2 7835 return true;
eedecc07
DD
7836 }
7837
b49e97c9
TS
7838 switch (sym->st_shndx)
7839 {
7840 case SHN_COMMON:
7841 /* Common symbols less than the GP size are automatically
7842 treated as SHN_MIPS_SCOMMON symbols. */
7843 if (sym->st_size > elf_gp_size (abfd)
b59eed79 7844 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
7845 || IRIX_COMPAT (abfd) == ict_irix6)
7846 break;
7847 /* Fall through. */
7848 case SHN_MIPS_SCOMMON:
7849 *secp = bfd_make_section_old_way (abfd, ".scommon");
10885e24 7850 (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
b49e97c9
TS
7851 *valp = sym->st_size;
7852 break;
7853
7854 case SHN_MIPS_TEXT:
7855 /* This section is used in a shared object. */
698600e4 7856 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
b49e97c9
TS
7857 {
7858 asymbol *elf_text_symbol;
7859 asection *elf_text_section;
986f0783 7860 size_t amt = sizeof (asection);
b49e97c9
TS
7861
7862 elf_text_section = bfd_zalloc (abfd, amt);
7863 if (elf_text_section == NULL)
0a1b45a2 7864 return false;
b49e97c9
TS
7865
7866 amt = sizeof (asymbol);
7867 elf_text_symbol = bfd_zalloc (abfd, amt);
7868 if (elf_text_symbol == NULL)
0a1b45a2 7869 return false;
b49e97c9
TS
7870
7871 /* Initialize the section. */
7872
698600e4
AM
7873 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7874 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
b49e97c9
TS
7875
7876 elf_text_section->symbol = elf_text_symbol;
698600e4 7877 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
b49e97c9
TS
7878
7879 elf_text_section->name = ".text";
7880 elf_text_section->flags = SEC_NO_FLAGS;
7881 elf_text_section->output_section = NULL;
7882 elf_text_section->owner = abfd;
7883 elf_text_symbol->name = ".text";
7884 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7885 elf_text_symbol->section = elf_text_section;
7886 }
7887 /* This code used to do *secp = bfd_und_section_ptr if
07d6d2b8
AM
7888 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7889 so I took it out. */
698600e4 7890 *secp = mips_elf_tdata (abfd)->elf_text_section;
b49e97c9
TS
7891 break;
7892
7893 case SHN_MIPS_ACOMMON:
7894 /* Fall through. XXX Can we treat this as allocated data? */
7895 case SHN_MIPS_DATA:
7896 /* This section is used in a shared object. */
698600e4 7897 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
b49e97c9
TS
7898 {
7899 asymbol *elf_data_symbol;
7900 asection *elf_data_section;
986f0783 7901 size_t amt = sizeof (asection);
b49e97c9
TS
7902
7903 elf_data_section = bfd_zalloc (abfd, amt);
7904 if (elf_data_section == NULL)
0a1b45a2 7905 return false;
b49e97c9
TS
7906
7907 amt = sizeof (asymbol);
7908 elf_data_symbol = bfd_zalloc (abfd, amt);
7909 if (elf_data_symbol == NULL)
0a1b45a2 7910 return false;
b49e97c9
TS
7911
7912 /* Initialize the section. */
7913
698600e4
AM
7914 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7915 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
b49e97c9
TS
7916
7917 elf_data_section->symbol = elf_data_symbol;
698600e4 7918 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
b49e97c9
TS
7919
7920 elf_data_section->name = ".data";
7921 elf_data_section->flags = SEC_NO_FLAGS;
7922 elf_data_section->output_section = NULL;
7923 elf_data_section->owner = abfd;
7924 elf_data_symbol->name = ".data";
7925 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7926 elf_data_symbol->section = elf_data_section;
7927 }
7928 /* This code used to do *secp = bfd_und_section_ptr if
07d6d2b8
AM
7929 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7930 so I took it out. */
698600e4 7931 *secp = mips_elf_tdata (abfd)->elf_data_section;
b49e97c9
TS
7932 break;
7933
7934 case SHN_MIPS_SUNDEFINED:
7935 *secp = bfd_und_section_ptr;
7936 break;
7937 }
7938
7939 if (SGI_COMPAT (abfd)
0e1862bb 7940 && ! bfd_link_pic (info)
f13a99db 7941 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
7942 && strcmp (*namep, "__rld_obj_head") == 0)
7943 {
7944 struct elf_link_hash_entry *h;
14a793b2 7945 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7946
7947 /* Mark __rld_obj_head as dynamic. */
14a793b2 7948 bh = NULL;
b49e97c9 7949 if (! (_bfd_generic_link_add_one_symbol
0a1b45a2 7950 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, false,
14a793b2 7951 get_elf_backend_data (abfd)->collect, &bh)))
0a1b45a2 7952 return false;
14a793b2
AM
7953
7954 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7955 h->non_elf = 0;
7956 h->def_regular = 1;
b49e97c9
TS
7957 h->type = STT_OBJECT;
7958
c152c796 7959 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 7960 return false;
b49e97c9 7961
0a1b45a2 7962 mips_elf_hash_table (info)->use_rld_obj_head = true;
b4082c70 7963 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
7964 }
7965
7966 /* If this is a mips16 text symbol, add 1 to the value to make it
7967 odd. This will cause something like .word SYM to come up with
7968 the right value when it is loaded into the PC. */
df58fc94 7969 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
7970 ++*valp;
7971
0a1b45a2 7972 return true;
b49e97c9
TS
7973}
7974
7975/* This hook function is called before the linker writes out a global
7976 symbol. We mark symbols as small common if appropriate. This is
7977 also where we undo the increment of the value for a mips16 symbol. */
7978
6e0b88f1 7979int
9719ad41
RS
7980_bfd_mips_elf_link_output_symbol_hook
7981 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7982 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7983 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
7984{
7985 /* If we see a common symbol, which implies a relocatable link, then
7986 if a symbol was small common in an input file, mark it as small
7987 common in the output file. */
7988 if (sym->st_shndx == SHN_COMMON
7989 && strcmp (input_sec->name, ".scommon") == 0)
7990 sym->st_shndx = SHN_MIPS_SCOMMON;
7991
df58fc94 7992 if (ELF_ST_IS_COMPRESSED (sym->st_other))
79cda7cf 7993 sym->st_value &= ~1;
b49e97c9 7994
6e0b88f1 7995 return 1;
b49e97c9
TS
7996}
7997\f
7998/* Functions for the dynamic linker. */
7999
8000/* Create dynamic sections when linking against a dynamic object. */
8001
0a1b45a2 8002bool
9719ad41 8003_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
8004{
8005 struct elf_link_hash_entry *h;
14a793b2 8006 struct bfd_link_hash_entry *bh;
b49e97c9
TS
8007 flagword flags;
8008 register asection *s;
8009 const char * const *namep;
0a44bf69 8010 struct mips_elf_link_hash_table *htab;
b49e97c9 8011
0a44bf69 8012 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8013 BFD_ASSERT (htab != NULL);
8014
b49e97c9
TS
8015 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
8016 | SEC_LINKER_CREATED | SEC_READONLY);
8017
0a44bf69
RS
8018 /* The psABI requires a read-only .dynamic section, but the VxWorks
8019 EABI doesn't. */
90c14f0c 8020 if (htab->root.target_os != is_vxworks)
b49e97c9 8021 {
3d4d4302 8022 s = bfd_get_linker_section (abfd, ".dynamic");
0a44bf69
RS
8023 if (s != NULL)
8024 {
fd361982 8025 if (!bfd_set_section_flags (s, flags))
0a1b45a2 8026 return false;
0a44bf69 8027 }
b49e97c9
TS
8028 }
8029
8030 /* We need to create .got section. */
23cc69b6 8031 if (!mips_elf_create_got_section (abfd, info))
0a1b45a2 8032 return false;
f4416af6 8033
0a1b45a2
AM
8034 if (! mips_elf_rel_dyn_section (info, true))
8035 return false;
b49e97c9 8036
b49e97c9 8037 /* Create .stub section. */
3d4d4302
AM
8038 s = bfd_make_section_anyway_with_flags (abfd,
8039 MIPS_ELF_STUB_SECTION_NAME (abfd),
8040 flags | SEC_CODE);
4e41d0d7 8041 if (s == NULL
fd361982 8042 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
0a1b45a2 8043 return false;
4e41d0d7 8044 htab->sstubs = s;
b49e97c9 8045
e6aea42d 8046 if (!mips_elf_hash_table (info)->use_rld_obj_head
0e1862bb 8047 && bfd_link_executable (info)
3d4d4302 8048 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
b49e97c9 8049 {
3d4d4302
AM
8050 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8051 flags &~ (flagword) SEC_READONLY);
b49e97c9 8052 if (s == NULL
fd361982 8053 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
0a1b45a2 8054 return false;
b49e97c9
TS
8055 }
8056
f16a9783
MS
8057 /* Create .MIPS.xhash section. */
8058 if (info->emit_gnu_hash)
8059 s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8060 flags | SEC_READONLY);
8061
b49e97c9
TS
8062 /* On IRIX5, we adjust add some additional symbols and change the
8063 alignments of several sections. There is no ABI documentation
8064 indicating that this is necessary on IRIX6, nor any evidence that
8065 the linker takes such action. */
8066 if (IRIX_COMPAT (abfd) == ict_irix5)
8067 {
8068 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8069 {
14a793b2 8070 bh = NULL;
b49e97c9 8071 if (! (_bfd_generic_link_add_one_symbol
9719ad41 8072 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
0a1b45a2
AM
8073 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8074 return false;
14a793b2
AM
8075
8076 h = (struct elf_link_hash_entry *) bh;
12f09816 8077 h->mark = 1;
f5385ebf
AM
8078 h->non_elf = 0;
8079 h->def_regular = 1;
b49e97c9
TS
8080 h->type = STT_SECTION;
8081
c152c796 8082 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 8083 return false;
b49e97c9
TS
8084 }
8085
8086 /* We need to create a .compact_rel section. */
8087 if (SGI_COMPAT (abfd))
8088 {
8089 if (!mips_elf_create_compact_rel_section (abfd, info))
0a1b45a2 8090 return false;
b49e97c9
TS
8091 }
8092
44c410de 8093 /* Change alignments of some sections. */
3d4d4302 8094 s = bfd_get_linker_section (abfd, ".hash");
b49e97c9 8095 if (s != NULL)
fd361982 8096 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
a253d456 8097
3d4d4302 8098 s = bfd_get_linker_section (abfd, ".dynsym");
b49e97c9 8099 if (s != NULL)
fd361982 8100 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
a253d456 8101
3d4d4302 8102 s = bfd_get_linker_section (abfd, ".dynstr");
b49e97c9 8103 if (s != NULL)
fd361982 8104 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
a253d456 8105
3d4d4302 8106 /* ??? */
b49e97c9
TS
8107 s = bfd_get_section_by_name (abfd, ".reginfo");
8108 if (s != NULL)
fd361982 8109 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
a253d456 8110
3d4d4302 8111 s = bfd_get_linker_section (abfd, ".dynamic");
b49e97c9 8112 if (s != NULL)
fd361982 8113 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
8114 }
8115
0e1862bb 8116 if (bfd_link_executable (info))
b49e97c9 8117 {
14a793b2
AM
8118 const char *name;
8119
8120 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8121 bh = NULL;
8122 if (!(_bfd_generic_link_add_one_symbol
9719ad41 8123 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
0a1b45a2
AM
8124 NULL, false, get_elf_backend_data (abfd)->collect, &bh)))
8125 return false;
14a793b2
AM
8126
8127 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
8128 h->non_elf = 0;
8129 h->def_regular = 1;
b49e97c9
TS
8130 h->type = STT_SECTION;
8131
c152c796 8132 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 8133 return false;
b49e97c9
TS
8134
8135 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8136 {
8137 /* __rld_map is a four byte word located in the .data section
8138 and is filled in by the rtld to contain a pointer to
8139 the _r_debug structure. Its symbol value will be set in
8140 _bfd_mips_elf_finish_dynamic_symbol. */
3d4d4302 8141 s = bfd_get_linker_section (abfd, ".rld_map");
0abfb97a 8142 BFD_ASSERT (s != NULL);
14a793b2 8143
0abfb97a
L
8144 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8145 bh = NULL;
8146 if (!(_bfd_generic_link_add_one_symbol
0a1b45a2 8147 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, false,
0abfb97a 8148 get_elf_backend_data (abfd)->collect, &bh)))
0a1b45a2 8149 return false;
b49e97c9 8150
0abfb97a
L
8151 h = (struct elf_link_hash_entry *) bh;
8152 h->non_elf = 0;
8153 h->def_regular = 1;
8154 h->type = STT_OBJECT;
8155
8156 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 8157 return false;
b4082c70 8158 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
8159 }
8160 }
8161
861fb55a 8162 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
c164a95d 8163 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
861fb55a 8164 if (!_bfd_elf_create_dynamic_sections (abfd, info))
0a1b45a2 8165 return false;
861fb55a 8166
1bbce132 8167 /* Do the usual VxWorks handling. */
90c14f0c 8168 if (htab->root.target_os == is_vxworks
1bbce132 8169 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
0a1b45a2 8170 return false;
0a44bf69 8171
0a1b45a2 8172 return true;
b49e97c9
TS
8173}
8174\f
c224138d
RS
8175/* Return true if relocation REL against section SEC is a REL rather than
8176 RELA relocation. RELOCS is the first relocation in the section and
8177 ABFD is the bfd that contains SEC. */
8178
0a1b45a2 8179static bool
c224138d
RS
8180mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8181 const Elf_Internal_Rela *relocs,
8182 const Elf_Internal_Rela *rel)
8183{
8184 Elf_Internal_Shdr *rel_hdr;
8185 const struct elf_backend_data *bed;
8186
d4730f92
BS
8187 /* To determine which flavor of relocation this is, we depend on the
8188 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8189 rel_hdr = elf_section_data (sec)->rel.hdr;
8190 if (rel_hdr == NULL)
0a1b45a2 8191 return false;
c224138d 8192 bed = get_elf_backend_data (abfd);
d4730f92
BS
8193 return ((size_t) (rel - relocs)
8194 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
c224138d
RS
8195}
8196
8197/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8198 HOWTO is the relocation's howto and CONTENTS points to the contents
8199 of the section that REL is against. */
8200
8201static bfd_vma
73425813
AM
8202mips_elf_read_rel_addend (bfd *abfd, asection *sec,
8203 const Elf_Internal_Rela *rel,
c224138d
RS
8204 reloc_howto_type *howto, bfd_byte *contents)
8205{
8206 bfd_byte *location;
8207 unsigned int r_type;
8208 bfd_vma addend;
17c6c9d9 8209 bfd_vma bytes;
c224138d 8210
73425813
AM
8211 if (!bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8212 return 0;
8213
c224138d
RS
8214 r_type = ELF_R_TYPE (abfd, rel->r_info);
8215 location = contents + rel->r_offset;
8216
8217 /* Get the addend, which is stored in the input file. */
0a1b45a2 8218 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, false, location);
17c6c9d9 8219 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
0a1b45a2 8220 _bfd_mips_elf_reloc_shuffle (abfd, r_type, false, location);
c224138d 8221
17c6c9d9
MR
8222 addend = bytes & howto->src_mask;
8223
8224 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8225 accordingly. */
8226 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8227 addend <<= 1;
8228
8229 return addend;
c224138d
RS
8230}
8231
8232/* REL is a relocation in ABFD that needs a partnering LO16 relocation
8233 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8234 and update *ADDEND with the final addend. Return true on success
8235 or false if the LO16 could not be found. RELEND is the exclusive
8236 upper bound on the relocations for REL's section. */
8237
0a1b45a2 8238static bool
c224138d 8239mips_elf_add_lo16_rel_addend (bfd *abfd,
73425813 8240 asection *sec,
c224138d
RS
8241 const Elf_Internal_Rela *rel,
8242 const Elf_Internal_Rela *relend,
8243 bfd_byte *contents, bfd_vma *addend)
8244{
8245 unsigned int r_type, lo16_type;
8246 const Elf_Internal_Rela *lo16_relocation;
8247 reloc_howto_type *lo16_howto;
8248 bfd_vma l;
8249
8250 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 8251 if (mips16_reloc_p (r_type))
c224138d 8252 lo16_type = R_MIPS16_LO16;
df58fc94
RS
8253 else if (micromips_reloc_p (r_type))
8254 lo16_type = R_MICROMIPS_LO16;
7361da2c
AB
8255 else if (r_type == R_MIPS_PCHI16)
8256 lo16_type = R_MIPS_PCLO16;
c224138d
RS
8257 else
8258 lo16_type = R_MIPS_LO16;
8259
8260 /* The combined value is the sum of the HI16 addend, left-shifted by
8261 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8262 code does a `lui' of the HI16 value, and then an `addiu' of the
8263 LO16 value.)
8264
8265 Scan ahead to find a matching LO16 relocation.
8266
8267 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8268 be immediately following. However, for the IRIX6 ABI, the next
8269 relocation may be a composed relocation consisting of several
8270 relocations for the same address. In that case, the R_MIPS_LO16
8271 relocation may occur as one of these. We permit a similar
8272 extension in general, as that is useful for GCC.
8273
8274 In some cases GCC dead code elimination removes the LO16 but keeps
8275 the corresponding HI16. This is strictly speaking a violation of
8276 the ABI but not immediately harmful. */
8277 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8278 if (lo16_relocation == NULL)
0a1b45a2 8279 return false;
c224138d
RS
8280
8281 /* Obtain the addend kept there. */
0a1b45a2 8282 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, false);
73425813
AM
8283 l = mips_elf_read_rel_addend (abfd, sec, lo16_relocation, lo16_howto,
8284 contents);
c224138d
RS
8285
8286 l <<= lo16_howto->rightshift;
8287 l = _bfd_mips_elf_sign_extend (l, 16);
8288
8289 *addend <<= 16;
8290 *addend += l;
0a1b45a2 8291 return true;
c224138d
RS
8292}
8293
8294/* Try to read the contents of section SEC in bfd ABFD. Return true and
8295 store the contents in *CONTENTS on success. Assume that *CONTENTS
8296 already holds the contents if it is nonull on entry. */
8297
0a1b45a2 8298static bool
c224138d
RS
8299mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8300{
8301 if (*contents)
0a1b45a2 8302 return true;
c224138d
RS
8303
8304 /* Get cached copy if it exists. */
8305 if (elf_section_data (sec)->this_hdr.contents != NULL)
8306 {
8307 *contents = elf_section_data (sec)->this_hdr.contents;
0a1b45a2 8308 return true;
c224138d
RS
8309 }
8310
8311 return bfd_malloc_and_get_section (abfd, sec, contents);
8312}
8313
1bbce132
MR
8314/* Make a new PLT record to keep internal data. */
8315
8316static struct plt_entry *
8317mips_elf_make_plt_record (bfd *abfd)
8318{
8319 struct plt_entry *entry;
8320
8321 entry = bfd_zalloc (abfd, sizeof (*entry));
8322 if (entry == NULL)
8323 return NULL;
8324
8325 entry->stub_offset = MINUS_ONE;
8326 entry->mips_offset = MINUS_ONE;
8327 entry->comp_offset = MINUS_ONE;
8328 entry->gotplt_index = MINUS_ONE;
8329 return entry;
8330}
8331
47275900
MR
8332/* Define the special `__gnu_absolute_zero' symbol. We only need this
8333 for PIC code, as otherwise there is no load-time relocation involved
8334 and local GOT entries whose value is zero at static link time will
8335 retain their value at load time. */
8336
0a1b45a2 8337static bool
47275900
MR
8338mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8339 struct mips_elf_link_hash_table *htab,
8340 unsigned int r_type)
8341{
8342 union
8343 {
8344 struct elf_link_hash_entry *eh;
8345 struct bfd_link_hash_entry *bh;
8346 }
8347 hzero;
8348
8349 BFD_ASSERT (!htab->use_absolute_zero);
8350 BFD_ASSERT (bfd_link_pic (info));
8351
8352 hzero.bh = NULL;
8353 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8354 BSF_GLOBAL, bfd_abs_section_ptr, 0,
0a1b45a2
AM
8355 NULL, false, false, &hzero.bh))
8356 return false;
47275900
MR
8357
8358 BFD_ASSERT (hzero.bh != NULL);
8359 hzero.eh->size = 0;
8360 hzero.eh->type = STT_NOTYPE;
8361 hzero.eh->other = STV_PROTECTED;
8362 hzero.eh->def_regular = 1;
8363 hzero.eh->non_elf = 0;
8364
0a1b45a2
AM
8365 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, true, r_type))
8366 return false;
47275900 8367
0a1b45a2 8368 htab->use_absolute_zero = true;
47275900 8369
0a1b45a2 8370 return true;
47275900
MR
8371}
8372
b49e97c9 8373/* Look through the relocs for a section during the first phase, and
1bbce132
MR
8374 allocate space in the global offset table and record the need for
8375 standard MIPS and compressed procedure linkage table entries. */
b49e97c9 8376
0a1b45a2 8377bool
9719ad41
RS
8378_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8379 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
8380{
8381 const char *name;
8382 bfd *dynobj;
8383 Elf_Internal_Shdr *symtab_hdr;
8384 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
8385 size_t extsymoff;
8386 const Elf_Internal_Rela *rel;
8387 const Elf_Internal_Rela *rel_end;
b49e97c9 8388 asection *sreloc;
9c5bfbb7 8389 const struct elf_backend_data *bed;
0a44bf69 8390 struct mips_elf_link_hash_table *htab;
c224138d
RS
8391 bfd_byte *contents;
8392 bfd_vma addend;
8393 reloc_howto_type *howto;
b49e97c9 8394
0e1862bb 8395 if (bfd_link_relocatable (info))
0a1b45a2 8396 return true;
b49e97c9 8397
0a44bf69 8398 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8399 BFD_ASSERT (htab != NULL);
8400
b49e97c9
TS
8401 dynobj = elf_hash_table (info)->dynobj;
8402 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8403 sym_hashes = elf_sym_hashes (abfd);
8404 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8405
738e5348 8406 bed = get_elf_backend_data (abfd);
056bafd4 8407 rel_end = relocs + sec->reloc_count;
738e5348 8408
b49e97c9
TS
8409 /* Check for the mips16 stub sections. */
8410
fd361982 8411 name = bfd_section_name (sec);
b9d58d71 8412 if (FN_STUB_P (name))
b49e97c9
TS
8413 {
8414 unsigned long r_symndx;
8415
8416 /* Look at the relocation information to figure out which symbol
07d6d2b8 8417 this is for. */
b49e97c9 8418
cb4437b8 8419 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
8420 if (r_symndx == 0)
8421 {
4eca0228 8422 _bfd_error_handler
695344c0 8423 /* xgettext:c-format */
2c1c9679 8424 (_("%pB: warning: cannot determine the target function for"
738e5348
RS
8425 " stub section `%s'"),
8426 abfd, name);
8427 bfd_set_error (bfd_error_bad_value);
0a1b45a2 8428 return false;
738e5348 8429 }
b49e97c9
TS
8430
8431 if (r_symndx < extsymoff
8432 || sym_hashes[r_symndx - extsymoff] == NULL)
8433 {
8434 asection *o;
8435
8436 /* This stub is for a local symbol. This stub will only be
07d6d2b8
AM
8437 needed if there is some relocation in this BFD, other
8438 than a 16 bit function call, which refers to this symbol. */
b49e97c9
TS
8439 for (o = abfd->sections; o != NULL; o = o->next)
8440 {
8441 Elf_Internal_Rela *sec_relocs;
8442 const Elf_Internal_Rela *r, *rend;
8443
8444 /* We can ignore stub sections when looking for relocs. */
8445 if ((o->flags & SEC_RELOC) == 0
8446 || o->reloc_count == 0
738e5348 8447 || section_allows_mips16_refs_p (o))
b49e97c9
TS
8448 continue;
8449
45d6a902 8450 sec_relocs
9719ad41 8451 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 8452 info->keep_memory);
b49e97c9 8453 if (sec_relocs == NULL)
0a1b45a2 8454 return false;
b49e97c9
TS
8455
8456 rend = sec_relocs + o->reloc_count;
8457 for (r = sec_relocs; r < rend; r++)
8458 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 8459 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
8460 break;
8461
6cdc0ccc 8462 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
8463 free (sec_relocs);
8464
8465 if (r < rend)
8466 break;
8467 }
8468
8469 if (o == NULL)
8470 {
8471 /* There is no non-call reloc for this stub, so we do
07d6d2b8
AM
8472 not need it. Since this function is called before
8473 the linker maps input sections to output sections, we
8474 can easily discard it by setting the SEC_EXCLUDE
8475 flag. */
b49e97c9 8476 sec->flags |= SEC_EXCLUDE;
0a1b45a2 8477 return true;
b49e97c9
TS
8478 }
8479
8480 /* Record this stub in an array of local symbol stubs for
07d6d2b8 8481 this BFD. */
698600e4 8482 if (mips_elf_tdata (abfd)->local_stubs == NULL)
b49e97c9
TS
8483 {
8484 unsigned long symcount;
8485 asection **n;
8486 bfd_size_type amt;
8487
8488 if (elf_bad_symtab (abfd))
8489 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8490 else
8491 symcount = symtab_hdr->sh_info;
8492 amt = symcount * sizeof (asection *);
9719ad41 8493 n = bfd_zalloc (abfd, amt);
b49e97c9 8494 if (n == NULL)
0a1b45a2 8495 return false;
698600e4 8496 mips_elf_tdata (abfd)->local_stubs = n;
b49e97c9
TS
8497 }
8498
b9d58d71 8499 sec->flags |= SEC_KEEP;
698600e4 8500 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
b49e97c9
TS
8501
8502 /* We don't need to set mips16_stubs_seen in this case.
07d6d2b8
AM
8503 That flag is used to see whether we need to look through
8504 the global symbol table for stubs. We don't need to set
8505 it here, because we just have a local stub. */
b49e97c9
TS
8506 }
8507 else
8508 {
8509 struct mips_elf_link_hash_entry *h;
8510
8511 h = ((struct mips_elf_link_hash_entry *)
8512 sym_hashes[r_symndx - extsymoff]);
8513
973a3492
L
8514 while (h->root.root.type == bfd_link_hash_indirect
8515 || h->root.root.type == bfd_link_hash_warning)
8516 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8517
b49e97c9
TS
8518 /* H is the symbol this stub is for. */
8519
b9d58d71
TS
8520 /* If we already have an appropriate stub for this function, we
8521 don't need another one, so we can discard this one. Since
8522 this function is called before the linker maps input sections
8523 to output sections, we can easily discard it by setting the
8524 SEC_EXCLUDE flag. */
8525 if (h->fn_stub != NULL)
8526 {
8527 sec->flags |= SEC_EXCLUDE;
0a1b45a2 8528 return true;
b9d58d71
TS
8529 }
8530
8531 sec->flags |= SEC_KEEP;
b49e97c9 8532 h->fn_stub = sec;
0a1b45a2 8533 mips_elf_hash_table (info)->mips16_stubs_seen = true;
b49e97c9
TS
8534 }
8535 }
b9d58d71 8536 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
8537 {
8538 unsigned long r_symndx;
8539 struct mips_elf_link_hash_entry *h;
8540 asection **loc;
8541
8542 /* Look at the relocation information to figure out which symbol
07d6d2b8 8543 this is for. */
b49e97c9 8544
cb4437b8 8545 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
8546 if (r_symndx == 0)
8547 {
4eca0228 8548 _bfd_error_handler
695344c0 8549 /* xgettext:c-format */
2c1c9679 8550 (_("%pB: warning: cannot determine the target function for"
738e5348
RS
8551 " stub section `%s'"),
8552 abfd, name);
8553 bfd_set_error (bfd_error_bad_value);
0a1b45a2 8554 return false;
738e5348 8555 }
b49e97c9
TS
8556
8557 if (r_symndx < extsymoff
8558 || sym_hashes[r_symndx - extsymoff] == NULL)
8559 {
b9d58d71 8560 asection *o;
b49e97c9 8561
b9d58d71 8562 /* This stub is for a local symbol. This stub will only be
07d6d2b8
AM
8563 needed if there is some relocation (R_MIPS16_26) in this BFD
8564 that refers to this symbol. */
b9d58d71
TS
8565 for (o = abfd->sections; o != NULL; o = o->next)
8566 {
8567 Elf_Internal_Rela *sec_relocs;
8568 const Elf_Internal_Rela *r, *rend;
8569
8570 /* We can ignore stub sections when looking for relocs. */
8571 if ((o->flags & SEC_RELOC) == 0
8572 || o->reloc_count == 0
738e5348 8573 || section_allows_mips16_refs_p (o))
b9d58d71
TS
8574 continue;
8575
8576 sec_relocs
8577 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8578 info->keep_memory);
8579 if (sec_relocs == NULL)
0a1b45a2 8580 return false;
b9d58d71
TS
8581
8582 rend = sec_relocs + o->reloc_count;
8583 for (r = sec_relocs; r < rend; r++)
8584 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8585 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8586 break;
8587
8588 if (elf_section_data (o)->relocs != sec_relocs)
8589 free (sec_relocs);
8590
8591 if (r < rend)
8592 break;
8593 }
8594
8595 if (o == NULL)
8596 {
8597 /* There is no non-call reloc for this stub, so we do
07d6d2b8
AM
8598 not need it. Since this function is called before
8599 the linker maps input sections to output sections, we
8600 can easily discard it by setting the SEC_EXCLUDE
8601 flag. */
b9d58d71 8602 sec->flags |= SEC_EXCLUDE;
0a1b45a2 8603 return true;
b9d58d71
TS
8604 }
8605
8606 /* Record this stub in an array of local symbol call_stubs for
07d6d2b8 8607 this BFD. */
698600e4 8608 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
b9d58d71
TS
8609 {
8610 unsigned long symcount;
8611 asection **n;
8612 bfd_size_type amt;
8613
8614 if (elf_bad_symtab (abfd))
8615 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8616 else
8617 symcount = symtab_hdr->sh_info;
8618 amt = symcount * sizeof (asection *);
8619 n = bfd_zalloc (abfd, amt);
8620 if (n == NULL)
0a1b45a2 8621 return false;
698600e4 8622 mips_elf_tdata (abfd)->local_call_stubs = n;
b9d58d71 8623 }
b49e97c9 8624
b9d58d71 8625 sec->flags |= SEC_KEEP;
698600e4 8626 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 8627
b9d58d71 8628 /* We don't need to set mips16_stubs_seen in this case.
07d6d2b8
AM
8629 That flag is used to see whether we need to look through
8630 the global symbol table for stubs. We don't need to set
8631 it here, because we just have a local stub. */
b9d58d71 8632 }
b49e97c9 8633 else
b49e97c9 8634 {
b9d58d71
TS
8635 h = ((struct mips_elf_link_hash_entry *)
8636 sym_hashes[r_symndx - extsymoff]);
68ffbac6 8637
b9d58d71 8638 /* H is the symbol this stub is for. */
68ffbac6 8639
b9d58d71
TS
8640 if (CALL_FP_STUB_P (name))
8641 loc = &h->call_fp_stub;
8642 else
8643 loc = &h->call_stub;
68ffbac6 8644
b9d58d71
TS
8645 /* If we already have an appropriate stub for this function, we
8646 don't need another one, so we can discard this one. Since
8647 this function is called before the linker maps input sections
8648 to output sections, we can easily discard it by setting the
8649 SEC_EXCLUDE flag. */
8650 if (*loc != NULL)
8651 {
8652 sec->flags |= SEC_EXCLUDE;
0a1b45a2 8653 return true;
b9d58d71 8654 }
b49e97c9 8655
b9d58d71
TS
8656 sec->flags |= SEC_KEEP;
8657 *loc = sec;
0a1b45a2 8658 mips_elf_hash_table (info)->mips16_stubs_seen = true;
b9d58d71 8659 }
b49e97c9
TS
8660 }
8661
b49e97c9 8662 sreloc = NULL;
c224138d 8663 contents = NULL;
b49e97c9
TS
8664 for (rel = relocs; rel < rel_end; ++rel)
8665 {
8666 unsigned long r_symndx;
8667 unsigned int r_type;
8668 struct elf_link_hash_entry *h;
0a1b45a2
AM
8669 bool can_make_dynamic_p;
8670 bool call_reloc_p;
8671 bool constrain_symbol_p;
b49e97c9
TS
8672
8673 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8674 r_type = ELF_R_TYPE (abfd, rel->r_info);
8675
8676 if (r_symndx < extsymoff)
8677 h = NULL;
8678 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8679 {
4eca0228 8680 _bfd_error_handler
695344c0 8681 /* xgettext:c-format */
2c1c9679 8682 (_("%pB: malformed reloc detected for section %s"),
d003868e 8683 abfd, name);
b49e97c9 8684 bfd_set_error (bfd_error_bad_value);
0a1b45a2 8685 return false;
b49e97c9
TS
8686 }
8687 else
8688 {
8689 h = sym_hashes[r_symndx - extsymoff];
81fbe831
AM
8690 if (h != NULL)
8691 {
8692 while (h->root.type == bfd_link_hash_indirect
8693 || h->root.type == bfd_link_hash_warning)
8694 h = (struct elf_link_hash_entry *) h->root.u.i.link;
81fbe831 8695 }
861fb55a 8696 }
b49e97c9 8697
861fb55a
DJ
8698 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8699 relocation into a dynamic one. */
0a1b45a2 8700 can_make_dynamic_p = false;
c5d6fa44
RS
8701
8702 /* Set CALL_RELOC_P to true if the relocation is for a call,
8703 and if pointer equality therefore doesn't matter. */
0a1b45a2 8704 call_reloc_p = false;
c5d6fa44
RS
8705
8706 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
c4b126b8 8707 into account when deciding how to define the symbol. */
0a1b45a2 8708 constrain_symbol_p = true;
c5d6fa44 8709
861fb55a
DJ
8710 switch (r_type)
8711 {
861fb55a
DJ
8712 case R_MIPS_CALL16:
8713 case R_MIPS_CALL_HI16:
8714 case R_MIPS_CALL_LO16:
c5d6fa44
RS
8715 case R_MIPS16_CALL16:
8716 case R_MICROMIPS_CALL16:
8717 case R_MICROMIPS_CALL_HI16:
8718 case R_MICROMIPS_CALL_LO16:
0a1b45a2 8719 call_reloc_p = true;
c5d6fa44
RS
8720 /* Fall through. */
8721
8722 case R_MIPS_GOT16:
861fb55a
DJ
8723 case R_MIPS_GOT_LO16:
8724 case R_MIPS_GOT_PAGE:
861fb55a 8725 case R_MIPS_GOT_DISP:
47275900
MR
8726 case R_MIPS16_GOT16:
8727 case R_MICROMIPS_GOT16:
8728 case R_MICROMIPS_GOT_LO16:
8729 case R_MICROMIPS_GOT_PAGE:
8730 case R_MICROMIPS_GOT_DISP:
8731 /* If we have a symbol that will resolve to zero at static link
8732 time and it is used by a GOT relocation applied to code we
8733 cannot relax to an immediate zero load, then we will be using
8734 the special `__gnu_absolute_zero' symbol whose value is zero
8735 at dynamic load time. We ignore HI16-type GOT relocations at
8736 this stage, because their handling will depend entirely on
8737 the corresponding LO16-type GOT relocation. */
8738 if (!call_hi16_reloc_p (r_type)
8739 && h != NULL
8740 && bfd_link_pic (info)
8741 && !htab->use_absolute_zero
8742 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8743 {
0a1b45a2 8744 bool rel_reloc;
47275900
MR
8745
8746 if (!mips_elf_get_section_contents (abfd, sec, &contents))
0a1b45a2 8747 return false;
47275900
MR
8748
8749 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8750 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
73425813
AM
8751 if (bfd_reloc_offset_in_range (howto, abfd, sec, rel->r_offset))
8752 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8753 false))
8754 if (!mips_elf_define_absolute_zero (abfd, info, htab,
8755 r_type))
8756 return false;
47275900
MR
8757 }
8758
8759 /* Fall through. */
8760 case R_MIPS_GOT_HI16:
8761 case R_MIPS_GOT_OFST:
861fb55a
DJ
8762 case R_MIPS_TLS_GOTTPREL:
8763 case R_MIPS_TLS_GD:
8764 case R_MIPS_TLS_LDM:
d0f13682
CLT
8765 case R_MIPS16_TLS_GOTTPREL:
8766 case R_MIPS16_TLS_GD:
8767 case R_MIPS16_TLS_LDM:
df58fc94 8768 case R_MICROMIPS_GOT_HI16:
df58fc94 8769 case R_MICROMIPS_GOT_OFST:
df58fc94
RS
8770 case R_MICROMIPS_TLS_GOTTPREL:
8771 case R_MICROMIPS_TLS_GD:
8772 case R_MICROMIPS_TLS_LDM:
861fb55a
DJ
8773 if (dynobj == NULL)
8774 elf_hash_table (info)->dynobj = dynobj = abfd;
8775 if (!mips_elf_create_got_section (dynobj, info))
0a1b45a2 8776 return false;
90c14f0c
L
8777 if (htab->root.target_os == is_vxworks
8778 && !bfd_link_pic (info))
b49e97c9 8779 {
4eca0228 8780 _bfd_error_handler
695344c0 8781 /* xgettext:c-format */
2dcf00ce
AM
8782 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8783 abfd, (uint64_t) rel->r_offset);
861fb55a 8784 bfd_set_error (bfd_error_bad_value);
0a1b45a2 8785 return false;
b49e97c9 8786 }
0a1b45a2 8787 can_make_dynamic_p = true;
861fb55a 8788 break;
b49e97c9 8789
c5d6fa44 8790 case R_MIPS_NONE:
99da6b5f 8791 case R_MIPS_JALR:
df58fc94 8792 case R_MICROMIPS_JALR:
c5d6fa44
RS
8793 /* These relocations have empty fields and are purely there to
8794 provide link information. The symbol value doesn't matter. */
0a1b45a2 8795 constrain_symbol_p = false;
c5d6fa44
RS
8796 break;
8797
8798 case R_MIPS_GPREL16:
8799 case R_MIPS_GPREL32:
8800 case R_MIPS16_GPREL:
8801 case R_MICROMIPS_GPREL16:
8802 /* GP-relative relocations always resolve to a definition in a
8803 regular input file, ignoring the one-definition rule. This is
8804 important for the GP setup sequence in NewABI code, which
8805 always resolves to a local function even if other relocations
8806 against the symbol wouldn't. */
0a1b45a2 8807 constrain_symbol_p = false;
99da6b5f
AN
8808 break;
8809
861fb55a
DJ
8810 case R_MIPS_32:
8811 case R_MIPS_REL32:
8812 case R_MIPS_64:
8813 /* In VxWorks executables, references to external symbols
8814 must be handled using copy relocs or PLT entries; it is not
8815 possible to convert this relocation into a dynamic one.
8816
8817 For executables that use PLTs and copy-relocs, we have a
8818 choice between converting the relocation into a dynamic
8819 one or using copy relocations or PLT entries. It is
8820 usually better to do the former, unless the relocation is
8821 against a read-only section. */
0e1862bb 8822 if ((bfd_link_pic (info)
861fb55a 8823 || (h != NULL
90c14f0c 8824 && htab->root.target_os != is_vxworks
861fb55a
DJ
8825 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8826 && !(!info->nocopyreloc
8827 && !PIC_OBJECT_P (abfd)
8828 && MIPS_ELF_READONLY_SECTION (sec))))
8829 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 8830 {
0a1b45a2 8831 can_make_dynamic_p = true;
b49e97c9
TS
8832 if (dynobj == NULL)
8833 elf_hash_table (info)->dynobj = dynobj = abfd;
861fb55a 8834 }
c5d6fa44 8835 break;
b49e97c9 8836
861fb55a
DJ
8837 case R_MIPS_26:
8838 case R_MIPS_PC16:
7361da2c
AB
8839 case R_MIPS_PC21_S2:
8840 case R_MIPS_PC26_S2:
861fb55a 8841 case R_MIPS16_26:
c9775dde 8842 case R_MIPS16_PC16_S1:
df58fc94
RS
8843 case R_MICROMIPS_26_S1:
8844 case R_MICROMIPS_PC7_S1:
8845 case R_MICROMIPS_PC10_S1:
8846 case R_MICROMIPS_PC16_S1:
8847 case R_MICROMIPS_PC23_S2:
0a1b45a2 8848 call_reloc_p = true;
861fb55a 8849 break;
b49e97c9
TS
8850 }
8851
0a44bf69
RS
8852 if (h)
8853 {
c5d6fa44
RS
8854 if (constrain_symbol_p)
8855 {
8856 if (!can_make_dynamic_p)
8857 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8858
8859 if (!call_reloc_p)
8860 h->pointer_equality_needed = 1;
8861
8862 /* We must not create a stub for a symbol that has
8863 relocations related to taking the function's address.
8864 This doesn't apply to VxWorks, where CALL relocs refer
8865 to a .got.plt entry instead of a normal .got entry. */
90c14f0c
L
8866 if (htab->root.target_os != is_vxworks
8867 && (!can_make_dynamic_p || !call_reloc_p))
0a1b45a2 8868 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = true;
c5d6fa44
RS
8869 }
8870
0a44bf69
RS
8871 /* Relocations against the special VxWorks __GOTT_BASE__ and
8872 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8873 room for them in .rela.dyn. */
8874 if (is_gott_symbol (info, h))
8875 {
8876 if (sreloc == NULL)
8877 {
0a1b45a2 8878 sreloc = mips_elf_rel_dyn_section (info, true);
0a44bf69 8879 if (sreloc == NULL)
0a1b45a2 8880 return false;
0a44bf69
RS
8881 }
8882 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
8883 if (MIPS_ELF_READONLY_SECTION (sec))
8884 /* We tell the dynamic linker that there are
8885 relocations against the text segment. */
8886 info->flags |= DF_TEXTREL;
0a44bf69
RS
8887 }
8888 }
df58fc94
RS
8889 else if (call_lo16_reloc_p (r_type)
8890 || got_lo16_reloc_p (r_type)
8891 || got_disp_reloc_p (r_type)
90c14f0c
L
8892 || (got16_reloc_p (r_type)
8893 && htab->root.target_os == is_vxworks))
b49e97c9
TS
8894 {
8895 /* We may need a local GOT entry for this relocation. We
8896 don't count R_MIPS_GOT_PAGE because we can estimate the
8897 maximum number of pages needed by looking at the size of
738e5348
RS
8898 the segment. Similar comments apply to R_MIPS*_GOT16 and
8899 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 8900 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 8901 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 8902 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0 8903 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
e641e783 8904 rel->r_addend, info, r_type))
0a1b45a2 8905 return false;
b49e97c9
TS
8906 }
8907
8f0c309a
CLT
8908 if (h != NULL
8909 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8910 ELF_ST_IS_MIPS16 (h->other)))
0a1b45a2 8911 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = true;
861fb55a 8912
b49e97c9
TS
8913 switch (r_type)
8914 {
8915 case R_MIPS_CALL16:
738e5348 8916 case R_MIPS16_CALL16:
df58fc94 8917 case R_MICROMIPS_CALL16:
b49e97c9
TS
8918 if (h == NULL)
8919 {
4eca0228 8920 _bfd_error_handler
695344c0 8921 /* xgettext:c-format */
2dcf00ce
AM
8922 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8923 abfd, (uint64_t) rel->r_offset);
b49e97c9 8924 bfd_set_error (bfd_error_bad_value);
0a1b45a2 8925 return false;
b49e97c9
TS
8926 }
8927 /* Fall through. */
8928
8929 case R_MIPS_CALL_HI16:
8930 case R_MIPS_CALL_LO16:
df58fc94
RS
8931 case R_MICROMIPS_CALL_HI16:
8932 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
8933 if (h != NULL)
8934 {
6ccf4795
RS
8935 /* Make sure there is room in the regular GOT to hold the
8936 function's address. We may eliminate it in favour of
8937 a .got.plt entry later; see mips_elf_count_got_symbols. */
0a1b45a2 8938 if (!mips_elf_record_global_got_symbol (h, abfd, info, true,
e641e783 8939 r_type))
0a1b45a2 8940 return false;
b49e97c9
TS
8941
8942 /* We need a stub, not a plt entry for the undefined
8943 function. But we record it as if it needs plt. See
c152c796 8944 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 8945 h->needs_plt = 1;
b49e97c9
TS
8946 h->type = STT_FUNC;
8947 }
8948 break;
8949
0fdc1bf1 8950 case R_MIPS_GOT_PAGE:
df58fc94 8951 case R_MICROMIPS_GOT_PAGE:
738e5348 8952 case R_MIPS16_GOT16:
b49e97c9
TS
8953 case R_MIPS_GOT16:
8954 case R_MIPS_GOT_HI16:
8955 case R_MIPS_GOT_LO16:
df58fc94
RS
8956 case R_MICROMIPS_GOT16:
8957 case R_MICROMIPS_GOT_HI16:
8958 case R_MICROMIPS_GOT_LO16:
8959 if (!h || got_page_reloc_p (r_type))
c224138d 8960 {
3a3b6725
DJ
8961 /* This relocation needs (or may need, if h != NULL) a
8962 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8963 know for sure until we know whether the symbol is
8964 preemptible. */
c224138d
RS
8965 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8966 {
8967 if (!mips_elf_get_section_contents (abfd, sec, &contents))
0a1b45a2
AM
8968 return false;
8969 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, false);
73425813 8970 addend = mips_elf_read_rel_addend (abfd, sec, rel,
c224138d 8971 howto, contents);
9684f078 8972 if (got16_reloc_p (r_type))
73425813 8973 mips_elf_add_lo16_rel_addend (abfd, sec, rel, rel_end,
c224138d
RS
8974 contents, &addend);
8975 else
8976 addend <<= howto->rightshift;
8977 }
8978 else
8979 addend = rel->r_addend;
13db6b44
RS
8980 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8981 h, addend))
0a1b45a2 8982 return false;
13db6b44
RS
8983
8984 if (h)
8985 {
8986 struct mips_elf_link_hash_entry *hmips =
8987 (struct mips_elf_link_hash_entry *) h;
8988
8989 /* This symbol is definitely not overridable. */
8990 if (hmips->root.def_regular
0e1862bb 8991 && ! (bfd_link_pic (info) && ! info->symbolic
13db6b44
RS
8992 && ! hmips->root.forced_local))
8993 h = NULL;
8994 }
c224138d 8995 }
13db6b44
RS
8996 /* If this is a global, overridable symbol, GOT_PAGE will
8997 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d
RS
8998 /* Fall through. */
8999
b49e97c9 9000 case R_MIPS_GOT_DISP:
df58fc94 9001 case R_MICROMIPS_GOT_DISP:
6ccf4795 9002 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
0a1b45a2
AM
9003 false, r_type))
9004 return false;
b49e97c9
TS
9005 break;
9006
0f20cc35 9007 case R_MIPS_TLS_GOTTPREL:
d0f13682 9008 case R_MIPS16_TLS_GOTTPREL:
df58fc94 9009 case R_MICROMIPS_TLS_GOTTPREL:
0e1862bb 9010 if (bfd_link_pic (info))
0f20cc35
DJ
9011 info->flags |= DF_STATIC_TLS;
9012 /* Fall through */
9013
9014 case R_MIPS_TLS_LDM:
d0f13682 9015 case R_MIPS16_TLS_LDM:
df58fc94
RS
9016 case R_MICROMIPS_TLS_LDM:
9017 if (tls_ldm_reloc_p (r_type))
0f20cc35 9018 {
cf35638d 9019 r_symndx = STN_UNDEF;
0f20cc35
DJ
9020 h = NULL;
9021 }
9022 /* Fall through */
9023
9024 case R_MIPS_TLS_GD:
d0f13682 9025 case R_MIPS16_TLS_GD:
df58fc94 9026 case R_MICROMIPS_TLS_GD:
0f20cc35
DJ
9027 /* This symbol requires a global offset table entry, or two
9028 for TLS GD relocations. */
e641e783
RS
9029 if (h != NULL)
9030 {
9031 if (!mips_elf_record_global_got_symbol (h, abfd, info,
0a1b45a2
AM
9032 false, r_type))
9033 return false;
e641e783
RS
9034 }
9035 else
9036 {
9037 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
9038 rel->r_addend,
9039 info, r_type))
0a1b45a2 9040 return false;
e641e783 9041 }
0f20cc35
DJ
9042 break;
9043
b49e97c9
TS
9044 case R_MIPS_32:
9045 case R_MIPS_REL32:
9046 case R_MIPS_64:
0a44bf69
RS
9047 /* In VxWorks executables, references to external symbols
9048 are handled using copy relocs or PLT stubs, so there's
9049 no need to add a .rela.dyn entry for this relocation. */
861fb55a 9050 if (can_make_dynamic_p)
b49e97c9
TS
9051 {
9052 if (sreloc == NULL)
9053 {
0a1b45a2 9054 sreloc = mips_elf_rel_dyn_section (info, true);
b49e97c9 9055 if (sreloc == NULL)
0a1b45a2 9056 return false;
b49e97c9 9057 }
0e1862bb 9058 if (bfd_link_pic (info) && h == NULL)
82f0cfbd
EC
9059 {
9060 /* When creating a shared object, we must copy these
9061 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
9062 relocs. Make room for this reloc in .rel(a).dyn. */
9063 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 9064 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
9065 /* We tell the dynamic linker that there are
9066 relocations against the text segment. */
9067 info->flags |= DF_TEXTREL;
9068 }
b49e97c9
TS
9069 else
9070 {
9071 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 9072
9a59ad6b
DJ
9073 /* For a shared object, we must copy this relocation
9074 unless the symbol turns out to be undefined and
9075 weak with non-default visibility, in which case
9076 it will be left as zero.
9077
9078 We could elide R_MIPS_REL32 for locally binding symbols
9079 in shared libraries, but do not yet do so.
9080
9081 For an executable, we only need to copy this
9082 reloc if the symbol is defined in a dynamic
9083 object. */
b49e97c9
TS
9084 hmips = (struct mips_elf_link_hash_entry *) h;
9085 ++hmips->possibly_dynamic_relocs;
943284cc 9086 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
9087 /* We need it to tell the dynamic linker if there
9088 are relocations against the text segment. */
0a1b45a2 9089 hmips->readonly_reloc = true;
b49e97c9 9090 }
b49e97c9
TS
9091 }
9092
9093 if (SGI_COMPAT (abfd))
9094 mips_elf_hash_table (info)->compact_rel_size +=
9095 sizeof (Elf32_External_crinfo);
9096 break;
9097
9098 case R_MIPS_26:
9099 case R_MIPS_GPREL16:
9100 case R_MIPS_LITERAL:
9101 case R_MIPS_GPREL32:
df58fc94
RS
9102 case R_MICROMIPS_26_S1:
9103 case R_MICROMIPS_GPREL16:
9104 case R_MICROMIPS_LITERAL:
9105 case R_MICROMIPS_GPREL7_S2:
b49e97c9
TS
9106 if (SGI_COMPAT (abfd))
9107 mips_elf_hash_table (info)->compact_rel_size +=
9108 sizeof (Elf32_External_crinfo);
9109 break;
9110
9111 /* This relocation describes the C++ object vtable hierarchy.
9112 Reconstruct it for later use during GC. */
9113 case R_MIPS_GNU_VTINHERIT:
c152c796 9114 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
0a1b45a2 9115 return false;
b49e97c9
TS
9116 break;
9117
9118 /* This relocation describes which C++ vtable entries are actually
9119 used. Record for later use during GC. */
9120 case R_MIPS_GNU_VTENTRY:
a0ea3a14 9121 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
0a1b45a2 9122 return false;
b49e97c9
TS
9123 break;
9124
9125 default:
9126 break;
9127 }
9128
1bbce132 9129 /* Record the need for a PLT entry. At this point we don't know
07d6d2b8
AM
9130 yet if we are going to create a PLT in the first place, but
9131 we only record whether the relocation requires a standard MIPS
9132 or a compressed code entry anyway. If we don't make a PLT after
9133 all, then we'll just ignore these arrangements. Likewise if
9134 a PLT entry is not created because the symbol is satisfied
9135 locally. */
1bbce132 9136 if (h != NULL
54806ffa
MR
9137 && (branch_reloc_p (r_type)
9138 || mips16_branch_reloc_p (r_type)
9139 || micromips_branch_reloc_p (r_type))
1bbce132
MR
9140 && !SYMBOL_CALLS_LOCAL (info, h))
9141 {
9142 if (h->plt.plist == NULL)
9143 h->plt.plist = mips_elf_make_plt_record (abfd);
9144 if (h->plt.plist == NULL)
0a1b45a2 9145 return false;
1bbce132 9146
54806ffa 9147 if (branch_reloc_p (r_type))
0a1b45a2 9148 h->plt.plist->need_mips = true;
1bbce132 9149 else
0a1b45a2 9150 h->plt.plist->need_comp = true;
1bbce132
MR
9151 }
9152
738e5348
RS
9153 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9154 if there is one. We only need to handle global symbols here;
9155 we decide whether to keep or delete stubs for local symbols
9156 when processing the stub's relocations. */
b49e97c9 9157 if (h != NULL
738e5348
RS
9158 && !mips16_call_reloc_p (r_type)
9159 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
9160 {
9161 struct mips_elf_link_hash_entry *mh;
9162
9163 mh = (struct mips_elf_link_hash_entry *) h;
0a1b45a2 9164 mh->need_fn_stub = true;
b49e97c9 9165 }
861fb55a
DJ
9166
9167 /* Refuse some position-dependent relocations when creating a
9168 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9169 not PIC, but we can create dynamic relocations and the result
9170 will be fine. Also do not refuse R_MIPS_LO16, which can be
9171 combined with R_MIPS_GOT16. */
0e1862bb 9172 if (bfd_link_pic (info))
861fb55a
DJ
9173 {
9174 switch (r_type)
9175 {
b474a202
FS
9176 case R_MIPS_TLS_TPREL_HI16:
9177 case R_MIPS16_TLS_TPREL_HI16:
9178 case R_MICROMIPS_TLS_TPREL_HI16:
9179 case R_MIPS_TLS_TPREL_LO16:
9180 case R_MIPS16_TLS_TPREL_LO16:
9181 case R_MICROMIPS_TLS_TPREL_LO16:
9182 /* These are okay in PIE, but not in a shared library. */
9183 if (bfd_link_executable (info))
9184 break;
9185
9186 /* FALLTHROUGH */
9187
861fb55a
DJ
9188 case R_MIPS16_HI16:
9189 case R_MIPS_HI16:
9190 case R_MIPS_HIGHER:
9191 case R_MIPS_HIGHEST:
df58fc94
RS
9192 case R_MICROMIPS_HI16:
9193 case R_MICROMIPS_HIGHER:
9194 case R_MICROMIPS_HIGHEST:
861fb55a
DJ
9195 /* Don't refuse a high part relocation if it's against
9196 no symbol (e.g. part of a compound relocation). */
cf35638d 9197 if (r_symndx == STN_UNDEF)
861fb55a
DJ
9198 break;
9199
3c7687b9 9200 /* Likewise an absolute symbol. */
304f09d0 9201 if (h != NULL && bfd_is_abs_symbol (&h->root))
3c7687b9
MR
9202 break;
9203
861fb55a
DJ
9204 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9205 and has a special meaning. */
9206 if (!NEWABI_P (abfd) && h != NULL
9207 && strcmp (h->root.root.string, "_gp_disp") == 0)
9208 break;
9209
0fc1eb3c
RS
9210 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9211 if (is_gott_symbol (info, h))
9212 break;
9213
861fb55a
DJ
9214 /* FALLTHROUGH */
9215
9216 case R_MIPS16_26:
9217 case R_MIPS_26:
df58fc94 9218 case R_MICROMIPS_26_S1:
304f09d0
FS
9219 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9220 /* An error for unsupported relocations is raised as part
9221 of the above search, so we can skip the following. */
9222 if (howto != NULL)
9223 info->callbacks->einfo
9224 /* xgettext:c-format */
9225 (_("%X%H: relocation %s against `%s' cannot be used"
9226 " when making a shared object; recompile with -fPIC\n"),
9227 abfd, sec, rel->r_offset, howto->name,
9228 (h) ? h->root.root.string : "a local symbol");
aff68bd0 9229 break;
861fb55a
DJ
9230 default:
9231 break;
9232 }
9233 }
b49e97c9
TS
9234 }
9235
0a1b45a2 9236 return true;
b49e97c9
TS
9237}
9238\f
9a59ad6b
DJ
9239/* Allocate space for global sym dynamic relocs. */
9240
0a1b45a2 9241static bool
9a59ad6b
DJ
9242allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9243{
9244 struct bfd_link_info *info = inf;
9245 bfd *dynobj;
9246 struct mips_elf_link_hash_entry *hmips;
9247 struct mips_elf_link_hash_table *htab;
9248
9249 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9250 BFD_ASSERT (htab != NULL);
9251
9a59ad6b
DJ
9252 dynobj = elf_hash_table (info)->dynobj;
9253 hmips = (struct mips_elf_link_hash_entry *) h;
9254
9255 /* VxWorks executables are handled elsewhere; we only need to
9256 allocate relocations in shared objects. */
90c14f0c 9257 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
0a1b45a2 9258 return true;
9a59ad6b 9259
7686d77d
AM
9260 /* Ignore indirect symbols. All relocations against such symbols
9261 will be redirected to the target symbol. */
9262 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 9263 return true;
63897e2c 9264
9a59ad6b
DJ
9265 /* If this symbol is defined in a dynamic object, or we are creating
9266 a shared library, we will need to copy any R_MIPS_32 or
9267 R_MIPS_REL32 relocs against it into the output file. */
0e1862bb 9268 if (! bfd_link_relocatable (info)
9a59ad6b
DJ
9269 && hmips->possibly_dynamic_relocs != 0
9270 && (h->root.type == bfd_link_hash_defweak
625ef6dc 9271 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
0e1862bb 9272 || bfd_link_pic (info)))
9a59ad6b 9273 {
0a1b45a2 9274 bool do_copy = true;
9a59ad6b
DJ
9275
9276 if (h->root.type == bfd_link_hash_undefweak)
9277 {
262e07d0
MR
9278 /* Do not copy relocations for undefined weak symbols that
9279 we are not going to export. */
9280 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
0a1b45a2 9281 do_copy = false;
9a59ad6b
DJ
9282
9283 /* Make sure undefined weak symbols are output as a dynamic
9284 symbol in PIEs. */
9285 else if (h->dynindx == -1 && !h->forced_local)
9286 {
9287 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 9288 return false;
9a59ad6b
DJ
9289 }
9290 }
9291
9292 if (do_copy)
9293 {
aff469fa 9294 /* Even though we don't directly need a GOT entry for this symbol,
f7ff1106
RS
9295 the SVR4 psABI requires it to have a dynamic symbol table
9296 index greater that DT_MIPS_GOTSYM if there are dynamic
9297 relocations against it.
9298
9299 VxWorks does not enforce the same mapping between the GOT
9300 and the symbol table, so the same requirement does not
9301 apply there. */
90c14f0c 9302 if (htab->root.target_os != is_vxworks)
6ccf4795
RS
9303 {
9304 if (hmips->global_got_area > GGA_RELOC_ONLY)
9305 hmips->global_got_area = GGA_RELOC_ONLY;
0a1b45a2 9306 hmips->got_only_for_calls = false;
6ccf4795 9307 }
aff469fa 9308
9a59ad6b
DJ
9309 mips_elf_allocate_dynamic_relocations
9310 (dynobj, info, hmips->possibly_dynamic_relocs);
9311 if (hmips->readonly_reloc)
9312 /* We tell the dynamic linker that there are relocations
9313 against the text segment. */
9314 info->flags |= DF_TEXTREL;
9315 }
9316 }
9317
0a1b45a2 9318 return true;
9a59ad6b
DJ
9319}
9320
b49e97c9
TS
9321/* Adjust a symbol defined by a dynamic object and referenced by a
9322 regular object. The current definition is in some section of the
9323 dynamic object, but we're not including those sections. We have to
9324 change the definition to something the rest of the link can
9325 understand. */
9326
0a1b45a2 9327bool
9719ad41
RS
9328_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9329 struct elf_link_hash_entry *h)
b49e97c9
TS
9330{
9331 bfd *dynobj;
9332 struct mips_elf_link_hash_entry *hmips;
5108fc1b 9333 struct mips_elf_link_hash_table *htab;
5474d94f 9334 asection *s, *srel;
b49e97c9 9335
5108fc1b 9336 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9337 BFD_ASSERT (htab != NULL);
9338
b49e97c9 9339 dynobj = elf_hash_table (info)->dynobj;
861fb55a 9340 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
9341
9342 /* Make sure we know what is going on here. */
8e4979ac
NC
9343 if (dynobj == NULL
9344 || (! h->needs_plt
9345 && ! h->is_weakalias
9346 && (! h->def_dynamic
9347 || ! h->ref_regular
9348 || h->def_regular)))
9349 {
9350 if (h->type == STT_GNU_IFUNC)
9351 _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9352 h->root.root.string);
9353 else
9354 _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9355 h->root.root.string);
0a1b45a2 9356 return true;
8e4979ac 9357 }
b49e97c9 9358
b49e97c9 9359 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9360
861fb55a
DJ
9361 /* If there are call relocations against an externally-defined symbol,
9362 see whether we can create a MIPS lazy-binding stub for it. We can
9363 only do this if all references to the function are through call
9364 relocations, and in that case, the traditional lazy-binding stubs
9365 are much more efficient than PLT entries.
9366
9367 Traditional stubs are only available on SVR4 psABI-based systems;
9368 VxWorks always uses PLTs instead. */
90c14f0c
L
9369 if (htab->root.target_os != is_vxworks
9370 && h->needs_plt
9371 && !hmips->no_fn_stub)
b49e97c9
TS
9372 {
9373 if (! elf_hash_table (info)->dynamic_sections_created)
0a1b45a2 9374 return true;
b49e97c9
TS
9375
9376 /* If this symbol is not defined in a regular file, then set
9377 the symbol to the stub location. This is required to make
9378 function pointers compare as equal between the normal
9379 executable and the shared library. */
4b8377e7
MR
9380 if (!h->def_regular
9381 && !bfd_is_abs_section (htab->sstubs->output_section))
b49e97c9 9382 {
0a1b45a2 9383 hmips->needs_lazy_stub = true;
33bb52fb 9384 htab->lazy_stub_count++;
0a1b45a2 9385 return true;
b49e97c9
TS
9386 }
9387 }
861fb55a
DJ
9388 /* As above, VxWorks requires PLT entries for externally-defined
9389 functions that are only accessed through call relocations.
b49e97c9 9390
861fb55a
DJ
9391 Both VxWorks and non-VxWorks targets also need PLT entries if there
9392 are static-only relocations against an externally-defined function.
9393 This can technically occur for shared libraries if there are
9394 branches to the symbol, although it is unlikely that this will be
9395 used in practice due to the short ranges involved. It can occur
9396 for any relative or absolute relocation in executables; in that
9397 case, the PLT entry becomes the function's canonical address. */
9398 else if (((h->needs_plt && !hmips->no_fn_stub)
9399 || (h->type == STT_FUNC && hmips->has_static_relocs))
9400 && htab->use_plts_and_copy_relocs
9401 && !SYMBOL_CALLS_LOCAL (info, h)
9402 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9403 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 9404 {
0a1b45a2
AM
9405 bool micromips_p = MICROMIPS_P (info->output_bfd);
9406 bool newabi_p = NEWABI_P (info->output_bfd);
1bbce132
MR
9407
9408 /* If this is the first symbol to need a PLT entry, then make some
07d6d2b8
AM
9409 basic setup. Also work out PLT entry sizes. We'll need them
9410 for PLT offset calculations. */
1bbce132 9411 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
861fb55a 9412 {
ce558b89 9413 BFD_ASSERT (htab->root.sgotplt->size == 0);
1bbce132 9414 BFD_ASSERT (htab->plt_got_index == 0);
0a44bf69 9415
861fb55a
DJ
9416 /* If we're using the PLT additions to the psABI, each PLT
9417 entry is 16 bytes and the PLT0 entry is 32 bytes.
9418 Encourage better cache usage by aligning. We do this
9419 lazily to avoid pessimizing traditional objects. */
90c14f0c 9420 if (htab->root.target_os != is_vxworks
fd361982 9421 && !bfd_set_section_alignment (htab->root.splt, 5))
0a1b45a2 9422 return false;
0a44bf69 9423
861fb55a
DJ
9424 /* Make sure that .got.plt is word-aligned. We do this lazily
9425 for the same reason as above. */
fd361982 9426 if (!bfd_set_section_alignment (htab->root.sgotplt,
861fb55a 9427 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
0a1b45a2 9428 return false;
0a44bf69 9429
861fb55a
DJ
9430 /* On non-VxWorks targets, the first two entries in .got.plt
9431 are reserved. */
90c14f0c 9432 if (htab->root.target_os != is_vxworks)
1bbce132
MR
9433 htab->plt_got_index
9434 += (get_elf_backend_data (dynobj)->got_header_size
9435 / MIPS_ELF_GOT_SIZE (dynobj));
0a44bf69 9436
861fb55a
DJ
9437 /* On VxWorks, also allocate room for the header's
9438 .rela.plt.unloaded entries. */
90c14f0c
L
9439 if (htab->root.target_os == is_vxworks
9440 && !bfd_link_pic (info))
0a44bf69 9441 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
1bbce132
MR
9442
9443 /* Now work out the sizes of individual PLT entries. */
90c14f0c
L
9444 if (htab->root.target_os == is_vxworks
9445 && bfd_link_pic (info))
1bbce132
MR
9446 htab->plt_mips_entry_size
9447 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
90c14f0c 9448 else if (htab->root.target_os == is_vxworks)
1bbce132
MR
9449 htab->plt_mips_entry_size
9450 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9451 else if (newabi_p)
9452 htab->plt_mips_entry_size
9453 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
833794fc 9454 else if (!micromips_p)
1bbce132
MR
9455 {
9456 htab->plt_mips_entry_size
9457 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9458 htab->plt_comp_entry_size
833794fc
MR
9459 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9460 }
9461 else if (htab->insn32)
9462 {
9463 htab->plt_mips_entry_size
9464 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9465 htab->plt_comp_entry_size
9466 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
1bbce132
MR
9467 }
9468 else
9469 {
9470 htab->plt_mips_entry_size
9471 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9472 htab->plt_comp_entry_size
833794fc 9473 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
1bbce132 9474 }
0a44bf69
RS
9475 }
9476
1bbce132
MR
9477 if (h->plt.plist == NULL)
9478 h->plt.plist = mips_elf_make_plt_record (dynobj);
9479 if (h->plt.plist == NULL)
0a1b45a2 9480 return false;
1bbce132
MR
9481
9482 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
07d6d2b8 9483 n32 or n64, so always use a standard entry there.
1bbce132 9484
07d6d2b8
AM
9485 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9486 all MIPS16 calls will go via that stub, and there is no benefit
9487 to having a MIPS16 entry. And in the case of call_stub a
9488 standard entry actually has to be used as the stub ends with a J
9489 instruction. */
1bbce132 9490 if (newabi_p
90c14f0c 9491 || htab->root.target_os == is_vxworks
1bbce132
MR
9492 || hmips->call_stub
9493 || hmips->call_fp_stub)
9494 {
0a1b45a2
AM
9495 h->plt.plist->need_mips = true;
9496 h->plt.plist->need_comp = false;
1bbce132
MR
9497 }
9498
9499 /* Otherwise, if there are no direct calls to the function, we
07d6d2b8
AM
9500 have a free choice of whether to use standard or compressed
9501 entries. Prefer microMIPS entries if the object is known to
9502 contain microMIPS code, so that it becomes possible to create
9503 pure microMIPS binaries. Prefer standard entries otherwise,
9504 because MIPS16 ones are no smaller and are usually slower. */
1bbce132
MR
9505 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9506 {
9507 if (micromips_p)
0a1b45a2 9508 h->plt.plist->need_comp = true;
1bbce132 9509 else
0a1b45a2 9510 h->plt.plist->need_mips = true;
1bbce132
MR
9511 }
9512
9513 if (h->plt.plist->need_mips)
9514 {
9515 h->plt.plist->mips_offset = htab->plt_mips_offset;
9516 htab->plt_mips_offset += htab->plt_mips_entry_size;
9517 }
9518 if (h->plt.plist->need_comp)
9519 {
9520 h->plt.plist->comp_offset = htab->plt_comp_offset;
9521 htab->plt_comp_offset += htab->plt_comp_entry_size;
9522 }
9523
9524 /* Reserve the corresponding .got.plt entry now too. */
9525 h->plt.plist->gotplt_index = htab->plt_got_index++;
0a44bf69
RS
9526
9527 /* If the output file has no definition of the symbol, set the
861fb55a 9528 symbol's value to the address of the stub. */
0e1862bb 9529 if (!bfd_link_pic (info) && !h->def_regular)
0a1b45a2 9530 hmips->use_plt_entry = true;
0a44bf69 9531
1bbce132 9532 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
90c14f0c 9533 htab->root.srelplt->size += (htab->root.target_os == is_vxworks
ce558b89
AM
9534 ? MIPS_ELF_RELA_SIZE (dynobj)
9535 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
9536
9537 /* Make room for the .rela.plt.unloaded relocations. */
90c14f0c 9538 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
0a44bf69
RS
9539 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9540
861fb55a
DJ
9541 /* All relocations against this symbol that could have been made
9542 dynamic will now refer to the PLT entry instead. */
9543 hmips->possibly_dynamic_relocs = 0;
0a44bf69 9544
0a1b45a2 9545 return true;
0a44bf69
RS
9546 }
9547
9548 /* If this is a weak symbol, and there is a real definition, the
9549 processor independent code will have arranged for us to see the
9550 real definition first, and we can just use the same value. */
60d67dc8 9551 if (h->is_weakalias)
0a44bf69 9552 {
60d67dc8
AM
9553 struct elf_link_hash_entry *def = weakdef (h);
9554 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9555 h->root.u.def.section = def->root.u.def.section;
9556 h->root.u.def.value = def->root.u.def.value;
0a1b45a2 9557 return true;
0a44bf69
RS
9558 }
9559
861fb55a
DJ
9560 /* Otherwise, there is nothing further to do for symbols defined
9561 in regular objects. */
9562 if (h->def_regular)
0a1b45a2 9563 return true;
0a44bf69 9564
861fb55a
DJ
9565 /* There's also nothing more to do if we'll convert all relocations
9566 against this symbol into dynamic relocations. */
9567 if (!hmips->has_static_relocs)
0a1b45a2 9568 return true;
861fb55a
DJ
9569
9570 /* We're now relying on copy relocations. Complain if we have
9571 some that we can't convert. */
0e1862bb 9572 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
861fb55a 9573 {
4eca0228
AM
9574 _bfd_error_handler (_("non-dynamic relocations refer to "
9575 "dynamic symbol %s"),
9576 h->root.root.string);
861fb55a 9577 bfd_set_error (bfd_error_bad_value);
0a1b45a2 9578 return false;
861fb55a
DJ
9579 }
9580
0a44bf69
RS
9581 /* We must allocate the symbol in our .dynbss section, which will
9582 become part of the .bss section of the executable. There will be
9583 an entry for this symbol in the .dynsym section. The dynamic
9584 object will contain position independent code, so all references
9585 from the dynamic object to this symbol will go through the global
9586 offset table. The dynamic linker will use the .dynsym entry to
9587 determine the address it must put in the global offset table, so
9588 both the dynamic object and the regular object will refer to the
9589 same memory location for the variable. */
9590
5474d94f
AM
9591 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9592 {
9593 s = htab->root.sdynrelro;
9594 srel = htab->root.sreldynrelro;
9595 }
9596 else
9597 {
9598 s = htab->root.sdynbss;
9599 srel = htab->root.srelbss;
9600 }
0a44bf69
RS
9601 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9602 {
90c14f0c 9603 if (htab->root.target_os == is_vxworks)
5474d94f 9604 srel->size += sizeof (Elf32_External_Rela);
861fb55a
DJ
9605 else
9606 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
9607 h->needs_copy = 1;
9608 }
9609
861fb55a
DJ
9610 /* All relocations against this symbol that could have been made
9611 dynamic will now refer to the local copy instead. */
9612 hmips->possibly_dynamic_relocs = 0;
9613
5474d94f 9614 return _bfd_elf_adjust_dynamic_copy (info, h, s);
0a44bf69 9615}
b49e97c9
TS
9616\f
9617/* This function is called after all the input files have been read,
9618 and the input sections have been assigned to output sections. We
9619 check for any mips16 stub sections that we can discard. */
9620
0a1b45a2 9621bool
9719ad41
RS
9622_bfd_mips_elf_always_size_sections (bfd *output_bfd,
9623 struct bfd_link_info *info)
b49e97c9 9624{
351cdf24 9625 asection *sect;
0a44bf69 9626 struct mips_elf_link_hash_table *htab;
861fb55a 9627 struct mips_htab_traverse_info hti;
0a44bf69
RS
9628
9629 htab = mips_elf_hash_table (info);
4dfe6ac6 9630 BFD_ASSERT (htab != NULL);
f4416af6 9631
b49e97c9 9632 /* The .reginfo section has a fixed size. */
351cdf24
MF
9633 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9634 if (sect != NULL)
6798f8bf 9635 {
fd361982 9636 bfd_set_section_size (sect, sizeof (Elf32_External_RegInfo));
6798f8bf
MR
9637 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9638 }
351cdf24
MF
9639
9640 /* The .MIPS.abiflags section has a fixed size. */
9641 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9642 if (sect != NULL)
6798f8bf 9643 {
fd361982 9644 bfd_set_section_size (sect, sizeof (Elf_External_ABIFlags_v0));
6798f8bf
MR
9645 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9646 }
b49e97c9 9647
861fb55a
DJ
9648 hti.info = info;
9649 hti.output_bfd = output_bfd;
0a1b45a2 9650 hti.error = false;
861fb55a
DJ
9651 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9652 mips_elf_check_symbols, &hti);
9653 if (hti.error)
0a1b45a2 9654 return false;
f4416af6 9655
0a1b45a2 9656 return true;
33bb52fb
RS
9657}
9658
9659/* If the link uses a GOT, lay it out and work out its size. */
9660
0a1b45a2 9661static bool
33bb52fb
RS
9662mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9663{
9664 bfd *dynobj;
9665 asection *s;
9666 struct mips_got_info *g;
33bb52fb
RS
9667 bfd_size_type loadable_size = 0;
9668 bfd_size_type page_gotno;
d7206569 9669 bfd *ibfd;
ab361d49 9670 struct mips_elf_traverse_got_arg tga;
33bb52fb
RS
9671 struct mips_elf_link_hash_table *htab;
9672
9673 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9674 BFD_ASSERT (htab != NULL);
9675
ce558b89 9676 s = htab->root.sgot;
f4416af6 9677 if (s == NULL)
0a1b45a2 9678 return true;
b49e97c9 9679
33bb52fb 9680 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
9681 g = htab->got_info;
9682
861fb55a
DJ
9683 /* Allocate room for the reserved entries. VxWorks always reserves
9684 3 entries; other objects only reserve 2 entries. */
cb22ccf4 9685 BFD_ASSERT (g->assigned_low_gotno == 0);
90c14f0c 9686 if (htab->root.target_os == is_vxworks)
861fb55a
DJ
9687 htab->reserved_gotno = 3;
9688 else
9689 htab->reserved_gotno = 2;
9690 g->local_gotno += htab->reserved_gotno;
cb22ccf4 9691 g->assigned_low_gotno = htab->reserved_gotno;
861fb55a 9692
6c42ddb9
RS
9693 /* Decide which symbols need to go in the global part of the GOT and
9694 count the number of reloc-only GOT symbols. */
020d7251 9695 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
f4416af6 9696
13db6b44 9697 if (!mips_elf_resolve_final_got_entries (info, g))
0a1b45a2 9698 return false;
13db6b44 9699
33bb52fb
RS
9700 /* Calculate the total loadable size of the output. That
9701 will give us the maximum number of GOT_PAGE entries
9702 required. */
c72f2fb2 9703 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
33bb52fb
RS
9704 {
9705 asection *subsection;
5108fc1b 9706
d7206569 9707 for (subsection = ibfd->sections;
33bb52fb
RS
9708 subsection;
9709 subsection = subsection->next)
9710 {
9711 if ((subsection->flags & SEC_ALLOC) == 0)
9712 continue;
9713 loadable_size += ((subsection->size + 0xf)
9714 &~ (bfd_size_type) 0xf);
9715 }
9716 }
f4416af6 9717
90c14f0c 9718 if (htab->root.target_os == is_vxworks)
738e5348 9719 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
9720 relocations against local symbols evaluate to "G", and the EABI does
9721 not include R_MIPS_GOT_PAGE. */
c224138d 9722 page_gotno = 0;
0a44bf69
RS
9723 else
9724 /* Assume there are two loadable segments consisting of contiguous
9725 sections. Is 5 enough? */
c224138d
RS
9726 page_gotno = (loadable_size >> 16) + 5;
9727
13db6b44 9728 /* Choose the smaller of the two page estimates; both are intended to be
c224138d
RS
9729 conservative. */
9730 if (page_gotno > g->page_gotno)
9731 page_gotno = g->page_gotno;
f4416af6 9732
c224138d 9733 g->local_gotno += page_gotno;
cb22ccf4 9734 g->assigned_high_gotno = g->local_gotno - 1;
ab361d49 9735
ab361d49
RS
9736 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9737 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
0f20cc35
DJ
9738 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9739
0a44bf69
RS
9740 /* VxWorks does not support multiple GOTs. It initializes $gp to
9741 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9742 dynamic loader. */
90c14f0c
L
9743 if (htab->root.target_os != is_vxworks
9744 && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 9745 {
a8028dd0 9746 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0a1b45a2 9747 return false;
0f20cc35
DJ
9748 }
9749 else
9750 {
d7206569
RS
9751 /* Record that all bfds use G. This also has the effect of freeing
9752 the per-bfd GOTs, which we no longer need. */
c72f2fb2 9753 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
0a1b45a2 9754 if (mips_elf_bfd_got (ibfd, false))
d7206569
RS
9755 mips_elf_replace_bfd_got (ibfd, g);
9756 mips_elf_replace_bfd_got (output_bfd, g);
9757
33bb52fb 9758 /* Set up TLS entries. */
0f20cc35 9759 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
72e7511a
RS
9760 tga.info = info;
9761 tga.g = g;
9762 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9763 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9764 if (!tga.g)
0a1b45a2 9765 return false;
1fd20d70
RS
9766 BFD_ASSERT (g->tls_assigned_gotno
9767 == g->global_gotno + g->local_gotno + g->tls_gotno);
33bb52fb 9768
57093f5e 9769 /* Each VxWorks GOT entry needs an explicit relocation. */
90c14f0c 9770 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
57093f5e
RS
9771 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9772
33bb52fb 9773 /* Allocate room for the TLS relocations. */
ab361d49
RS
9774 if (g->relocs)
9775 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
0f20cc35 9776 }
b49e97c9 9777
0a1b45a2 9778 return true;
b49e97c9
TS
9779}
9780
33bb52fb
RS
9781/* Estimate the size of the .MIPS.stubs section. */
9782
9783static void
9784mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9785{
9786 struct mips_elf_link_hash_table *htab;
9787 bfd_size_type dynsymcount;
9788
9789 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9790 BFD_ASSERT (htab != NULL);
9791
33bb52fb
RS
9792 if (htab->lazy_stub_count == 0)
9793 return;
9794
9795 /* IRIX rld assumes that a function stub isn't at the end of the .text
9796 section, so add a dummy entry to the end. */
9797 htab->lazy_stub_count++;
9798
9799 /* Get a worst-case estimate of the number of dynamic symbols needed.
9800 At this point, dynsymcount does not account for section symbols
9801 and count_section_dynsyms may overestimate the number that will
9802 be needed. */
9803 dynsymcount = (elf_hash_table (info)->dynsymcount
9804 + count_section_dynsyms (output_bfd, info));
9805
1bbce132
MR
9806 /* Determine the size of one stub entry. There's no disadvantage
9807 from using microMIPS code here, so for the sake of pure-microMIPS
9808 binaries we prefer it whenever there's any microMIPS code in
9809 output produced at all. This has a benefit of stubs being
833794fc
MR
9810 shorter by 4 bytes each too, unless in the insn32 mode. */
9811 if (!MICROMIPS_P (output_bfd))
1bbce132
MR
9812 htab->function_stub_size = (dynsymcount > 0x10000
9813 ? MIPS_FUNCTION_STUB_BIG_SIZE
9814 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
833794fc
MR
9815 else if (htab->insn32)
9816 htab->function_stub_size = (dynsymcount > 0x10000
9817 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9818 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9819 else
9820 htab->function_stub_size = (dynsymcount > 0x10000
9821 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9822 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
33bb52fb
RS
9823
9824 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9825}
9826
1bbce132
MR
9827/* A mips_elf_link_hash_traverse callback for which DATA points to a
9828 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9829 stub, allocate an entry in the stubs section. */
33bb52fb 9830
0a1b45a2 9831static bool
af924177 9832mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 9833{
1bbce132 9834 struct mips_htab_traverse_info *hti = data;
33bb52fb 9835 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9836 struct bfd_link_info *info;
9837 bfd *output_bfd;
9838
9839 info = hti->info;
9840 output_bfd = hti->output_bfd;
9841 htab = mips_elf_hash_table (info);
9842 BFD_ASSERT (htab != NULL);
33bb52fb 9843
33bb52fb
RS
9844 if (h->needs_lazy_stub)
9845 {
0a1b45a2 9846 bool micromips_p = MICROMIPS_P (output_bfd);
1bbce132
MR
9847 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9848 bfd_vma isa_bit = micromips_p;
9849
9850 BFD_ASSERT (htab->root.dynobj != NULL);
9851 if (h->root.plt.plist == NULL)
9852 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9853 if (h->root.plt.plist == NULL)
9854 {
0a1b45a2
AM
9855 hti->error = true;
9856 return false;
1bbce132 9857 }
33bb52fb 9858 h->root.root.u.def.section = htab->sstubs;
1bbce132
MR
9859 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9860 h->root.plt.plist->stub_offset = htab->sstubs->size;
9861 h->root.other = other;
33bb52fb
RS
9862 htab->sstubs->size += htab->function_stub_size;
9863 }
0a1b45a2 9864 return true;
33bb52fb
RS
9865}
9866
9867/* Allocate offsets in the stubs section to each symbol that needs one.
9868 Set the final size of the .MIPS.stub section. */
9869
0a1b45a2 9870static bool
33bb52fb
RS
9871mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9872{
1bbce132 9873 bfd *output_bfd = info->output_bfd;
0a1b45a2 9874 bool micromips_p = MICROMIPS_P (output_bfd);
1bbce132
MR
9875 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9876 bfd_vma isa_bit = micromips_p;
33bb52fb 9877 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9878 struct mips_htab_traverse_info hti;
9879 struct elf_link_hash_entry *h;
9880 bfd *dynobj;
33bb52fb
RS
9881
9882 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9883 BFD_ASSERT (htab != NULL);
9884
33bb52fb 9885 if (htab->lazy_stub_count == 0)
0a1b45a2 9886 return true;
33bb52fb
RS
9887
9888 htab->sstubs->size = 0;
1bbce132
MR
9889 hti.info = info;
9890 hti.output_bfd = output_bfd;
0a1b45a2 9891 hti.error = false;
1bbce132
MR
9892 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9893 if (hti.error)
0a1b45a2 9894 return false;
33bb52fb
RS
9895 htab->sstubs->size += htab->function_stub_size;
9896 BFD_ASSERT (htab->sstubs->size
9897 == htab->lazy_stub_count * htab->function_stub_size);
1bbce132
MR
9898
9899 dynobj = elf_hash_table (info)->dynobj;
9900 BFD_ASSERT (dynobj != NULL);
9901 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9902 if (h == NULL)
0a1b45a2 9903 return false;
1bbce132
MR
9904 h->root.u.def.value = isa_bit;
9905 h->other = other;
9906 h->type = STT_FUNC;
9907
0a1b45a2 9908 return true;
1bbce132
MR
9909}
9910
9911/* A mips_elf_link_hash_traverse callback for which DATA points to a
9912 bfd_link_info. If H uses the address of a PLT entry as the value
9913 of the symbol, then set the entry in the symbol table now. Prefer
9914 a standard MIPS PLT entry. */
9915
0a1b45a2 9916static bool
1bbce132
MR
9917mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9918{
9919 struct bfd_link_info *info = data;
0a1b45a2 9920 bool micromips_p = MICROMIPS_P (info->output_bfd);
1bbce132
MR
9921 struct mips_elf_link_hash_table *htab;
9922 unsigned int other;
9923 bfd_vma isa_bit;
9924 bfd_vma val;
9925
9926 htab = mips_elf_hash_table (info);
9927 BFD_ASSERT (htab != NULL);
9928
9929 if (h->use_plt_entry)
9930 {
9931 BFD_ASSERT (h->root.plt.plist != NULL);
9932 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9933 || h->root.plt.plist->comp_offset != MINUS_ONE);
9934
9935 val = htab->plt_header_size;
9936 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9937 {
9938 isa_bit = 0;
9939 val += h->root.plt.plist->mips_offset;
9940 other = 0;
9941 }
9942 else
9943 {
9944 isa_bit = 1;
9945 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9946 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9947 }
9948 val += isa_bit;
9949 /* For VxWorks, point at the PLT load stub rather than the lazy
07d6d2b8
AM
9950 resolution stub; this stub will become the canonical function
9951 address. */
90c14f0c 9952 if (htab->root.target_os == is_vxworks)
1bbce132
MR
9953 val += 8;
9954
ce558b89 9955 h->root.root.u.def.section = htab->root.splt;
1bbce132
MR
9956 h->root.root.u.def.value = val;
9957 h->root.other = other;
9958 }
9959
0a1b45a2 9960 return true;
33bb52fb
RS
9961}
9962
b49e97c9
TS
9963/* Set the sizes of the dynamic sections. */
9964
0a1b45a2 9965bool
9719ad41
RS
9966_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9967 struct bfd_link_info *info)
b49e97c9
TS
9968{
9969 bfd *dynobj;
861fb55a 9970 asection *s, *sreldyn;
0a1b45a2 9971 bool reltext;
0a44bf69 9972 struct mips_elf_link_hash_table *htab;
b49e97c9 9973
0a44bf69 9974 htab = mips_elf_hash_table (info);
4dfe6ac6 9975 BFD_ASSERT (htab != NULL);
b49e97c9
TS
9976 dynobj = elf_hash_table (info)->dynobj;
9977 BFD_ASSERT (dynobj != NULL);
9978
9979 if (elf_hash_table (info)->dynamic_sections_created)
9980 {
9981 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9982 if (bfd_link_executable (info) && !info->nointerp)
b49e97c9 9983 {
3d4d4302 9984 s = bfd_get_linker_section (dynobj, ".interp");
b49e97c9 9985 BFD_ASSERT (s != NULL);
eea6121a 9986 s->size
b49e97c9
TS
9987 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9988 s->contents
9989 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9990 }
861fb55a 9991
1bbce132 9992 /* Figure out the size of the PLT header if we know that we
07d6d2b8
AM
9993 are using it. For the sake of cache alignment always use
9994 a standard header whenever any standard entries are present
9995 even if microMIPS entries are present as well. This also
9996 lets the microMIPS header rely on the value of $v0 only set
9997 by microMIPS entries, for a small size reduction.
1bbce132 9998
07d6d2b8
AM
9999 Set symbol table entry values for symbols that use the
10000 address of their PLT entry now that we can calculate it.
1bbce132 10001
07d6d2b8
AM
10002 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
10003 haven't already in _bfd_elf_create_dynamic_sections. */
ce558b89 10004 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
861fb55a 10005 {
0a1b45a2 10006 bool micromips_p = (MICROMIPS_P (output_bfd)
1bbce132
MR
10007 && !htab->plt_mips_offset);
10008 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
10009 bfd_vma isa_bit = micromips_p;
861fb55a 10010 struct elf_link_hash_entry *h;
1bbce132 10011 bfd_vma size;
861fb55a
DJ
10012
10013 BFD_ASSERT (htab->use_plts_and_copy_relocs);
ce558b89
AM
10014 BFD_ASSERT (htab->root.sgotplt->size == 0);
10015 BFD_ASSERT (htab->root.splt->size == 0);
1bbce132 10016
90c14f0c 10017 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
1bbce132 10018 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
90c14f0c 10019 else if (htab->root.target_os == is_vxworks)
1bbce132
MR
10020 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
10021 else if (ABI_64_P (output_bfd))
10022 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
10023 else if (ABI_N32_P (output_bfd))
10024 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
10025 else if (!micromips_p)
10026 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
833794fc
MR
10027 else if (htab->insn32)
10028 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
1bbce132
MR
10029 else
10030 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
861fb55a 10031
1bbce132
MR
10032 htab->plt_header_is_comp = micromips_p;
10033 htab->plt_header_size = size;
ce558b89
AM
10034 htab->root.splt->size = (size
10035 + htab->plt_mips_offset
10036 + htab->plt_comp_offset);
10037 htab->root.sgotplt->size = (htab->plt_got_index
10038 * MIPS_ELF_GOT_SIZE (dynobj));
1bbce132
MR
10039
10040 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
10041
10042 if (htab->root.hplt == NULL)
10043 {
ce558b89 10044 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
1bbce132
MR
10045 "_PROCEDURE_LINKAGE_TABLE_");
10046 htab->root.hplt = h;
10047 if (h == NULL)
0a1b45a2 10048 return false;
1bbce132
MR
10049 }
10050
10051 h = htab->root.hplt;
10052 h->root.u.def.value = isa_bit;
10053 h->other = other;
861fb55a
DJ
10054 h->type = STT_FUNC;
10055 }
10056 }
4e41d0d7 10057
9a59ad6b 10058 /* Allocate space for global sym dynamic relocs. */
2c3fc389 10059 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9a59ad6b 10060
33bb52fb
RS
10061 mips_elf_estimate_stub_size (output_bfd, info);
10062
10063 if (!mips_elf_lay_out_got (output_bfd, info))
0a1b45a2 10064 return false;
33bb52fb
RS
10065
10066 mips_elf_lay_out_lazy_stubs (info);
10067
b49e97c9
TS
10068 /* The check_relocs and adjust_dynamic_symbol entry points have
10069 determined the sizes of the various dynamic sections. Allocate
10070 memory for them. */
0a1b45a2 10071 reltext = false;
b49e97c9
TS
10072 for (s = dynobj->sections; s != NULL; s = s->next)
10073 {
10074 const char *name;
b49e97c9
TS
10075
10076 /* It's OK to base decisions on the section name, because none
10077 of the dynobj section names depend upon the input files. */
fd361982 10078 name = bfd_section_name (s);
b49e97c9
TS
10079
10080 if ((s->flags & SEC_LINKER_CREATED) == 0)
10081 continue;
10082
08dedd66 10083 if (startswith (name, ".rel"))
b49e97c9 10084 {
c456f082 10085 if (s->size != 0)
b49e97c9
TS
10086 {
10087 const char *outname;
10088 asection *target;
10089
10090 /* If this relocation section applies to a read only
07d6d2b8
AM
10091 section, then we probably need a DT_TEXTREL entry.
10092 If the relocation section is .rel(a).dyn, we always
10093 assert a DT_TEXTREL entry rather than testing whether
10094 there exists a relocation to a read only section or
10095 not. */
fd361982 10096 outname = bfd_section_name (s->output_section);
b49e97c9
TS
10097 target = bfd_get_section_by_name (output_bfd, outname + 4);
10098 if ((target != NULL
10099 && (target->flags & SEC_READONLY) != 0
10100 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 10101 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
0a1b45a2 10102 reltext = true;
b49e97c9
TS
10103
10104 /* We use the reloc_count field as a counter if we need
10105 to copy relocs into the output file. */
0a44bf69 10106 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 10107 s->reloc_count = 0;
f4416af6
AO
10108
10109 /* If combreloc is enabled, elf_link_sort_relocs() will
10110 sort relocations, but in a different way than we do,
10111 and before we're done creating relocations. Also, it
10112 will move them around between input sections'
10113 relocation's contents, so our sorting would be
10114 broken, so don't let it run. */
10115 info->combreloc = 0;
b49e97c9
TS
10116 }
10117 }
0e1862bb 10118 else if (bfd_link_executable (info)
b49e97c9 10119 && ! mips_elf_hash_table (info)->use_rld_obj_head
08dedd66 10120 && startswith (name, ".rld_map"))
b49e97c9 10121 {
5108fc1b 10122 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 10123 rtld to contain a pointer to the _r_debug structure. */
b4082c70 10124 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
b49e97c9
TS
10125 }
10126 else if (SGI_COMPAT (output_bfd)
08dedd66 10127 && startswith (name, ".compact_rel"))
eea6121a 10128 s->size += mips_elf_hash_table (info)->compact_rel_size;
ce558b89 10129 else if (s == htab->root.splt)
861fb55a
DJ
10130 {
10131 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
10132 room for an extra nop to fill the delay slot. This is
10133 for CPUs without load interlocking. */
10134 if (! LOAD_INTERLOCKS_P (output_bfd)
90c14f0c
L
10135 && htab->root.target_os != is_vxworks
10136 && s->size > 0)
861fb55a
DJ
10137 s->size += 4;
10138 }
08dedd66 10139 else if (! startswith (name, ".init")
ce558b89
AM
10140 && s != htab->root.sgot
10141 && s != htab->root.sgotplt
861fb55a 10142 && s != htab->sstubs
5474d94f
AM
10143 && s != htab->root.sdynbss
10144 && s != htab->root.sdynrelro)
b49e97c9
TS
10145 {
10146 /* It's not one of our sections, so don't allocate space. */
10147 continue;
10148 }
10149
c456f082 10150 if (s->size == 0)
b49e97c9 10151 {
8423293d 10152 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
10153 continue;
10154 }
10155
c456f082
AM
10156 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10157 continue;
10158
b49e97c9 10159 /* Allocate memory for the section contents. */
eea6121a 10160 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 10161 if (s->contents == NULL)
b49e97c9
TS
10162 {
10163 bfd_set_error (bfd_error_no_memory);
0a1b45a2 10164 return false;
b49e97c9
TS
10165 }
10166 }
10167
10168 if (elf_hash_table (info)->dynamic_sections_created)
10169 {
10170 /* Add some entries to the .dynamic section. We fill in the
10171 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10172 must add the entries now so that we get the correct size for
5750dcec 10173 the .dynamic section. */
af5978fb
RS
10174
10175 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec 10176 DT_MIPS_RLD_MAP entry. This must come first because glibc
6e6be592
MR
10177 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10178 may only look at the first one they see. */
0e1862bb 10179 if (!bfd_link_pic (info)
af5978fb 10180 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
0a1b45a2 10181 return false;
b49e97c9 10182
0e1862bb 10183 if (bfd_link_executable (info)
a5499fa4 10184 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
0a1b45a2 10185 return false;
a5499fa4 10186
5750dcec
DJ
10187 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10188 used by the debugger. */
0e1862bb 10189 if (bfd_link_executable (info)
5750dcec
DJ
10190 && !SGI_COMPAT (output_bfd)
10191 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
0a1b45a2 10192 return false;
5750dcec 10193
90c14f0c
L
10194 if (reltext
10195 && (SGI_COMPAT (output_bfd)
10196 || htab->root.target_os == is_vxworks))
b49e97c9
TS
10197 info->flags |= DF_TEXTREL;
10198
10199 if ((info->flags & DF_TEXTREL) != 0)
10200 {
10201 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
0a1b45a2 10202 return false;
943284cc
DJ
10203
10204 /* Clear the DF_TEXTREL flag. It will be set again if we
10205 write out an actual text relocation; we may not, because
10206 at this point we do not know whether e.g. any .eh_frame
10207 absolute relocations have been converted to PC-relative. */
10208 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
10209 }
10210
10211 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
0a1b45a2 10212 return false;
b49e97c9 10213
0a1b45a2 10214 sreldyn = mips_elf_rel_dyn_section (info, false);
90c14f0c 10215 if (htab->root.target_os == is_vxworks)
b49e97c9 10216 {
0a44bf69
RS
10217 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10218 use any of the DT_MIPS_* tags. */
861fb55a 10219 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
10220 {
10221 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
0a1b45a2 10222 return false;
b49e97c9 10223
0a44bf69 10224 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
0a1b45a2 10225 return false;
b49e97c9 10226
0a44bf69 10227 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
0a1b45a2 10228 return false;
0a44bf69 10229 }
b49e97c9 10230 }
0a44bf69
RS
10231 else
10232 {
db841b6f
MR
10233 if (sreldyn && sreldyn->size > 0
10234 && !bfd_is_abs_section (sreldyn->output_section))
0a44bf69
RS
10235 {
10236 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
0a1b45a2 10237 return false;
b49e97c9 10238
0a44bf69 10239 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
0a1b45a2 10240 return false;
b49e97c9 10241
0a44bf69 10242 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
0a1b45a2 10243 return false;
0a44bf69 10244 }
b49e97c9 10245
0a44bf69 10246 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
0a1b45a2 10247 return false;
b49e97c9 10248
0a44bf69 10249 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
0a1b45a2 10250 return false;
b49e97c9 10251
0a44bf69 10252 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
0a1b45a2 10253 return false;
b49e97c9 10254
0a44bf69 10255 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
0a1b45a2 10256 return false;
b49e97c9 10257
0a44bf69 10258 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
0a1b45a2 10259 return false;
b49e97c9 10260
0a44bf69 10261 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
0a1b45a2 10262 return false;
b49e97c9 10263
0a44bf69 10264 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
0a1b45a2 10265 return false;
0a44bf69 10266
f16a9783
MS
10267 if (info->emit_gnu_hash
10268 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
0a1b45a2 10269 return false;
f16a9783 10270
0a44bf69
RS
10271 if (IRIX_COMPAT (dynobj) == ict_irix5
10272 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
0a1b45a2 10273 return false;
0a44bf69
RS
10274
10275 if (IRIX_COMPAT (dynobj) == ict_irix6
10276 && (bfd_get_section_by_name
af0edeb8 10277 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
0a44bf69 10278 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
0a1b45a2 10279 return false;
0a44bf69 10280 }
ce558b89 10281 if (htab->root.splt->size > 0)
861fb55a
DJ
10282 {
10283 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
0a1b45a2 10284 return false;
861fb55a
DJ
10285
10286 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
0a1b45a2 10287 return false;
861fb55a
DJ
10288
10289 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
0a1b45a2 10290 return false;
861fb55a
DJ
10291
10292 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
0a1b45a2 10293 return false;
861fb55a 10294 }
90c14f0c 10295 if (htab->root.target_os == is_vxworks
7a2b07ff 10296 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
0a1b45a2 10297 return false;
b49e97c9
TS
10298 }
10299
0a1b45a2 10300 return true;
b49e97c9
TS
10301}
10302\f
81d43bff
RS
10303/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10304 Adjust its R_ADDEND field so that it is correct for the output file.
10305 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10306 and sections respectively; both use symbol indexes. */
10307
10308static void
10309mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10310 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10311 asection **local_sections, Elf_Internal_Rela *rel)
10312{
10313 unsigned int r_type, r_symndx;
10314 Elf_Internal_Sym *sym;
10315 asection *sec;
10316
020d7251 10317 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
81d43bff
RS
10318 {
10319 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
df58fc94 10320 if (gprel16_reloc_p (r_type)
81d43bff 10321 || r_type == R_MIPS_GPREL32
df58fc94 10322 || literal_reloc_p (r_type))
81d43bff
RS
10323 {
10324 rel->r_addend += _bfd_get_gp_value (input_bfd);
10325 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10326 }
10327
10328 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10329 sym = local_syms + r_symndx;
10330
10331 /* Adjust REL's addend to account for section merging. */
0e1862bb 10332 if (!bfd_link_relocatable (info))
81d43bff
RS
10333 {
10334 sec = local_sections[r_symndx];
10335 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10336 }
10337
10338 /* This would normally be done by the rela_normal code in elflink.c. */
10339 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10340 rel->r_addend += local_sections[r_symndx]->output_offset;
10341 }
10342}
10343
545fd46b
MR
10344/* Handle relocations against symbols from removed linkonce sections,
10345 or sections discarded by a linker script. We use this wrapper around
10346 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10347 on 64-bit ELF targets. In this case for any relocation handled, which
10348 always be the first in a triplet, the remaining two have to be processed
10349 together with the first, even if they are R_MIPS_NONE. It is the symbol
10350 index referred by the first reloc that applies to all the three and the
10351 remaining two never refer to an object symbol. And it is the final
10352 relocation (the last non-null one) that determines the output field of
10353 the whole relocation so retrieve the corresponding howto structure for
10354 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10355
10356 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10357 and therefore requires to be pasted in a loop. It also defines a block
10358 and does not protect any of its arguments, hence the extra brackets. */
10359
10360static void
10361mips_reloc_against_discarded_section (bfd *output_bfd,
10362 struct bfd_link_info *info,
10363 bfd *input_bfd, asection *input_section,
10364 Elf_Internal_Rela **rel,
10365 const Elf_Internal_Rela **relend,
0a1b45a2 10366 bool rel_reloc,
545fd46b
MR
10367 reloc_howto_type *howto,
10368 bfd_byte *contents)
10369{
10370 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10371 int count = bed->s->int_rels_per_ext_rel;
10372 unsigned int r_type;
10373 int i;
10374
10375 for (i = count - 1; i > 0; i--)
10376 {
10377 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10378 if (r_type != R_MIPS_NONE)
10379 {
10380 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10381 break;
10382 }
10383 }
10384 do
10385 {
10386 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10387 (*rel), count, (*relend),
10388 howto, i, contents);
10389 }
10390 while (0);
10391}
10392
b49e97c9
TS
10393/* Relocate a MIPS ELF section. */
10394
0f684201 10395int
9719ad41
RS
10396_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10397 bfd *input_bfd, asection *input_section,
10398 bfd_byte *contents, Elf_Internal_Rela *relocs,
10399 Elf_Internal_Sym *local_syms,
10400 asection **local_sections)
b49e97c9
TS
10401{
10402 Elf_Internal_Rela *rel;
10403 const Elf_Internal_Rela *relend;
10404 bfd_vma addend = 0;
0a1b45a2 10405 bool use_saved_addend_p = false;
b49e97c9 10406
056bafd4 10407 relend = relocs + input_section->reloc_count;
b49e97c9
TS
10408 for (rel = relocs; rel < relend; ++rel)
10409 {
10410 const char *name;
c9adbffe 10411 bfd_vma value = 0;
b49e97c9 10412 reloc_howto_type *howto;
0a1b45a2 10413 bool cross_mode_jump_p = false;
b34976b6 10414 /* TRUE if the relocation is a RELA relocation, rather than a
07d6d2b8 10415 REL relocation. */
0a1b45a2 10416 bool rela_relocation_p = true;
b49e97c9 10417 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 10418 const char *msg;
ab96bf03
AM
10419 unsigned long r_symndx;
10420 asection *sec;
749b8d9d
L
10421 Elf_Internal_Shdr *symtab_hdr;
10422 struct elf_link_hash_entry *h;
0a1b45a2 10423 bool rel_reloc;
b49e97c9 10424
d4730f92
BS
10425 rel_reloc = (NEWABI_P (input_bfd)
10426 && mips_elf_rel_relocation_p (input_bfd, input_section,
10427 relocs, rel));
b49e97c9 10428 /* Find the relocation howto for this relocation. */
d4730f92 10429 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
ab96bf03
AM
10430
10431 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 10432 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
020d7251 10433 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
749b8d9d
L
10434 {
10435 sec = local_sections[r_symndx];
10436 h = NULL;
10437 }
ab96bf03
AM
10438 else
10439 {
ab96bf03 10440 unsigned long extsymoff;
ab96bf03 10441
ab96bf03
AM
10442 extsymoff = 0;
10443 if (!elf_bad_symtab (input_bfd))
10444 extsymoff = symtab_hdr->sh_info;
10445 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10446 while (h->root.type == bfd_link_hash_indirect
10447 || h->root.type == bfd_link_hash_warning)
10448 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10449
10450 sec = NULL;
10451 if (h->root.type == bfd_link_hash_defined
10452 || h->root.type == bfd_link_hash_defweak)
10453 sec = h->root.u.def.section;
10454 }
10455
dbaa2011 10456 if (sec != NULL && discarded_section (sec))
545fd46b
MR
10457 {
10458 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10459 input_section, &rel, &relend,
10460 rel_reloc, howto, contents);
10461 continue;
10462 }
ab96bf03 10463
4a14403c 10464 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
10465 {
10466 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10467 64-bit code, but make sure all their addresses are in the
10468 lowermost or uppermost 32-bit section of the 64-bit address
10469 space. Thus, when they use an R_MIPS_64 they mean what is
10470 usually meant by R_MIPS_32, with the exception that the
10471 stored value is sign-extended to 64 bits. */
0a1b45a2 10472 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, false);
b49e97c9
TS
10473
10474 /* On big-endian systems, we need to lie about the position
10475 of the reloc. */
10476 if (bfd_big_endian (input_bfd))
10477 rel->r_offset += 4;
10478 }
b49e97c9
TS
10479
10480 if (!use_saved_addend_p)
10481 {
b49e97c9
TS
10482 /* If these relocations were originally of the REL variety,
10483 we must pull the addend out of the field that will be
10484 relocated. Otherwise, we simply use the contents of the
c224138d
RS
10485 RELA relocation. */
10486 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10487 relocs, rel))
b49e97c9 10488 {
0a1b45a2 10489 rela_relocation_p = false;
73425813
AM
10490 addend = mips_elf_read_rel_addend (input_bfd, input_section,
10491 rel, howto, contents);
738e5348
RS
10492 if (hi16_reloc_p (r_type)
10493 || (got16_reloc_p (r_type)
b49e97c9 10494 && mips_elf_local_relocation_p (input_bfd, rel,
020d7251 10495 local_sections)))
b49e97c9 10496 {
73425813
AM
10497 if (!mips_elf_add_lo16_rel_addend (input_bfd, input_section,
10498 rel, relend,
c224138d 10499 contents, &addend))
749b8d9d 10500 {
749b8d9d
L
10501 if (h)
10502 name = h->root.root.string;
10503 else
10504 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10505 local_syms + r_symndx,
10506 sec);
4eca0228 10507 _bfd_error_handler
695344c0 10508 /* xgettext:c-format */
2c1c9679 10509 (_("%pB: can't find matching LO16 reloc against `%s'"
2dcf00ce 10510 " for %s at %#" PRIx64 " in section `%pA'"),
c08bb8dd 10511 input_bfd, name,
2dcf00ce 10512 howto->name, (uint64_t) rel->r_offset, input_section);
749b8d9d 10513 }
b49e97c9 10514 }
30ac9238
RS
10515 else
10516 addend <<= howto->rightshift;
b49e97c9
TS
10517 }
10518 else
10519 addend = rel->r_addend;
81d43bff
RS
10520 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10521 local_syms, local_sections, rel);
b49e97c9
TS
10522 }
10523
0e1862bb 10524 if (bfd_link_relocatable (info))
b49e97c9 10525 {
4a14403c 10526 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
10527 && bfd_big_endian (input_bfd))
10528 rel->r_offset -= 4;
10529
81d43bff 10530 if (!rela_relocation_p && rel->r_addend)
5a659663 10531 {
81d43bff 10532 addend += rel->r_addend;
738e5348 10533 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
10534 addend = mips_elf_high (addend);
10535 else if (r_type == R_MIPS_HIGHER)
10536 addend = mips_elf_higher (addend);
10537 else if (r_type == R_MIPS_HIGHEST)
10538 addend = mips_elf_highest (addend);
30ac9238
RS
10539 else
10540 addend >>= howto->rightshift;
b49e97c9 10541
30ac9238
RS
10542 /* We use the source mask, rather than the destination
10543 mask because the place to which we are writing will be
10544 source of the addend in the final link. */
b49e97c9
TS
10545 addend &= howto->src_mask;
10546
5a659663 10547 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10548 /* See the comment above about using R_MIPS_64 in the 32-bit
10549 ABI. Here, we need to update the addend. It would be
10550 possible to get away with just using the R_MIPS_32 reloc
10551 but for endianness. */
10552 {
10553 bfd_vma sign_bits;
10554 bfd_vma low_bits;
10555 bfd_vma high_bits;
10556
10557 if (addend & ((bfd_vma) 1 << 31))
10558#ifdef BFD64
10559 sign_bits = ((bfd_vma) 1 << 32) - 1;
10560#else
10561 sign_bits = -1;
10562#endif
10563 else
10564 sign_bits = 0;
10565
10566 /* If we don't know that we have a 64-bit type,
10567 do two separate stores. */
10568 if (bfd_big_endian (input_bfd))
10569 {
10570 /* Store the sign-bits (which are most significant)
10571 first. */
10572 low_bits = sign_bits;
10573 high_bits = addend;
10574 }
10575 else
10576 {
10577 low_bits = addend;
10578 high_bits = sign_bits;
10579 }
10580 bfd_put_32 (input_bfd, low_bits,
10581 contents + rel->r_offset);
10582 bfd_put_32 (input_bfd, high_bits,
10583 contents + rel->r_offset + 4);
10584 continue;
10585 }
10586
10587 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10588 input_bfd, input_section,
0a1b45a2
AM
10589 contents, false))
10590 return false;
b49e97c9
TS
10591 }
10592
10593 /* Go on to the next relocation. */
10594 continue;
10595 }
10596
10597 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10598 relocations for the same offset. In that case we are
10599 supposed to treat the output of each relocation as the addend
10600 for the next. */
10601 if (rel + 1 < relend
10602 && rel->r_offset == rel[1].r_offset
10603 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
0a1b45a2 10604 use_saved_addend_p = true;
b49e97c9 10605 else
0a1b45a2 10606 use_saved_addend_p = false;
b49e97c9
TS
10607
10608 /* Figure out what value we are supposed to relocate. */
10609 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
47275900
MR
10610 input_section, contents,
10611 info, rel, addend, howto,
10612 local_syms, local_sections,
10613 &value, &name, &cross_mode_jump_p,
bce03d3d 10614 use_saved_addend_p))
b49e97c9
TS
10615 {
10616 case bfd_reloc_continue:
10617 /* There's nothing to do. */
10618 continue;
10619
10620 case bfd_reloc_undefined:
10621 /* mips_elf_calculate_relocation already called the
10622 undefined_symbol callback. There's no real point in
10623 trying to perform the relocation at this point, so we
10624 just skip ahead to the next relocation. */
10625 continue;
10626
10627 case bfd_reloc_notsupported:
10628 msg = _("internal error: unsupported relocation error");
10629 info->callbacks->warning
10630 (info, msg, name, input_bfd, input_section, rel->r_offset);
0a1b45a2 10631 return false;
b49e97c9
TS
10632
10633 case bfd_reloc_overflow:
10634 if (use_saved_addend_p)
10635 /* Ignore overflow until we reach the last relocation for
10636 a given location. */
10637 ;
10638 else
10639 {
0e53d9da
AN
10640 struct mips_elf_link_hash_table *htab;
10641
10642 htab = mips_elf_hash_table (info);
4dfe6ac6 10643 BFD_ASSERT (htab != NULL);
b49e97c9 10644 BFD_ASSERT (name != NULL);
0e53d9da 10645 if (!htab->small_data_overflow_reported
9684f078 10646 && (gprel16_reloc_p (howto->type)
df58fc94 10647 || literal_reloc_p (howto->type)))
0e53d9da 10648 {
91d6fa6a
NC
10649 msg = _("small-data section exceeds 64KB;"
10650 " lower small-data size limit (see option -G)");
0e53d9da 10651
0a1b45a2 10652 htab->small_data_overflow_reported = true;
0e53d9da
AN
10653 (*info->callbacks->einfo) ("%P: %s\n", msg);
10654 }
1a72702b
AM
10655 (*info->callbacks->reloc_overflow)
10656 (info, NULL, name, howto->name, (bfd_vma) 0,
10657 input_bfd, input_section, rel->r_offset);
b49e97c9
TS
10658 }
10659 break;
10660
10661 case bfd_reloc_ok:
10662 break;
10663
df58fc94 10664 case bfd_reloc_outofrange:
7db9a74e 10665 msg = NULL;
df58fc94 10666 if (jal_reloc_p (howto->type))
9d862524 10667 msg = (cross_mode_jump_p
2c1c9679 10668 ? _("cannot convert a jump to JALX "
9d862524
MR
10669 "for a non-word-aligned address")
10670 : (howto->type == R_MIPS16_26
2c1c9679
AM
10671 ? _("jump to a non-word-aligned address")
10672 : _("jump to a non-instruction-aligned address")));
99aefae6 10673 else if (b_reloc_p (howto->type))
a6ebf616 10674 msg = (cross_mode_jump_p
2c1c9679 10675 ? _("cannot convert a branch to JALX "
a6ebf616 10676 "for a non-word-aligned address")
2c1c9679 10677 : _("branch to a non-instruction-aligned address"));
7db9a74e
MR
10678 else if (aligned_pcrel_reloc_p (howto->type))
10679 msg = _("PC-relative load from unaligned address");
10680 if (msg)
df58fc94 10681 {
de341542 10682 info->callbacks->einfo
ed53407e
MR
10683 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10684 break;
7361da2c 10685 }
df58fc94
RS
10686 /* Fall through. */
10687
b49e97c9
TS
10688 default:
10689 abort ();
10690 break;
10691 }
10692
10693 /* If we've got another relocation for the address, keep going
10694 until we reach the last one. */
10695 if (use_saved_addend_p)
10696 {
10697 addend = value;
10698 continue;
10699 }
10700
4a14403c 10701 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10702 /* See the comment above about using R_MIPS_64 in the 32-bit
10703 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10704 that calculated the right value. Now, however, we
10705 sign-extend the 32-bit result to 64-bits, and store it as a
10706 64-bit value. We are especially generous here in that we
10707 go to extreme lengths to support this usage on systems with
10708 only a 32-bit VMA. */
10709 {
10710 bfd_vma sign_bits;
10711 bfd_vma low_bits;
10712 bfd_vma high_bits;
10713
10714 if (value & ((bfd_vma) 1 << 31))
10715#ifdef BFD64
10716 sign_bits = ((bfd_vma) 1 << 32) - 1;
10717#else
10718 sign_bits = -1;
10719#endif
10720 else
10721 sign_bits = 0;
10722
10723 /* If we don't know that we have a 64-bit type,
10724 do two separate stores. */
10725 if (bfd_big_endian (input_bfd))
10726 {
10727 /* Undo what we did above. */
10728 rel->r_offset -= 4;
10729 /* Store the sign-bits (which are most significant)
10730 first. */
10731 low_bits = sign_bits;
10732 high_bits = value;
10733 }
10734 else
10735 {
10736 low_bits = value;
10737 high_bits = sign_bits;
10738 }
10739 bfd_put_32 (input_bfd, low_bits,
10740 contents + rel->r_offset);
10741 bfd_put_32 (input_bfd, high_bits,
10742 contents + rel->r_offset + 4);
10743 continue;
10744 }
10745
10746 /* Actually perform the relocation. */
10747 if (! mips_elf_perform_relocation (info, howto, rel, value,
10748 input_bfd, input_section,
38a7df63 10749 contents, cross_mode_jump_p))
0a1b45a2 10750 return false;
b49e97c9
TS
10751 }
10752
0a1b45a2 10753 return true;
b49e97c9
TS
10754}
10755\f
861fb55a
DJ
10756/* A function that iterates over each entry in la25_stubs and fills
10757 in the code for each one. DATA points to a mips_htab_traverse_info. */
10758
10759static int
10760mips_elf_create_la25_stub (void **slot, void *data)
10761{
10762 struct mips_htab_traverse_info *hti;
10763 struct mips_elf_link_hash_table *htab;
10764 struct mips_elf_la25_stub *stub;
10765 asection *s;
10766 bfd_byte *loc;
10767 bfd_vma offset, target, target_high, target_low;
3734320d
MF
10768 bfd_vma branch_pc;
10769 bfd_signed_vma pcrel_offset = 0;
861fb55a
DJ
10770
10771 stub = (struct mips_elf_la25_stub *) *slot;
10772 hti = (struct mips_htab_traverse_info *) data;
10773 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 10774 BFD_ASSERT (htab != NULL);
861fb55a
DJ
10775
10776 /* Create the section contents, if we haven't already. */
10777 s = stub->stub_section;
10778 loc = s->contents;
10779 if (loc == NULL)
10780 {
10781 loc = bfd_malloc (s->size);
10782 if (loc == NULL)
10783 {
0a1b45a2
AM
10784 hti->error = true;
10785 return false;
861fb55a
DJ
10786 }
10787 s->contents = loc;
10788 }
10789
10790 /* Work out where in the section this stub should go. */
10791 offset = stub->offset;
10792
3734320d
MF
10793 /* We add 8 here to account for the LUI/ADDIU instructions
10794 before the branch instruction. This cannot be moved down to
10795 where pcrel_offset is calculated as 's' is updated in
10796 mips_elf_get_la25_target. */
10797 branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10798
861fb55a 10799 /* Work out the target address. */
8f0c309a
CLT
10800 target = mips_elf_get_la25_target (stub, &s);
10801 target += s->output_section->vma + s->output_offset;
10802
861fb55a
DJ
10803 target_high = ((target + 0x8000) >> 16) & 0xffff;
10804 target_low = (target & 0xffff);
10805
3734320d
MF
10806 /* Calculate the PC of the compact branch instruction (for the case where
10807 compact branches are used for either microMIPSR6 or MIPSR6 with
10808 compact branches. Add 4-bytes to account for BC using the PC of the
10809 next instruction as the base. */
10810 pcrel_offset = target - (branch_pc + 4);
10811
861fb55a
DJ
10812 if (stub->stub_section != htab->strampoline)
10813 {
df58fc94 10814 /* This is a simple LUI/ADDIU stub. Zero out the beginning
861fb55a
DJ
10815 of the section and write the two instructions at the end. */
10816 memset (loc, 0, offset);
10817 loc += offset;
df58fc94
RS
10818 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10819 {
d21911ea
MR
10820 bfd_put_micromips_32 (hti->output_bfd,
10821 LA25_LUI_MICROMIPS (target_high),
10822 loc);
10823 bfd_put_micromips_32 (hti->output_bfd,
10824 LA25_ADDIU_MICROMIPS (target_low),
10825 loc + 4);
df58fc94
RS
10826 }
10827 else
10828 {
10829 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10830 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10831 }
861fb55a
DJ
10832 }
10833 else
10834 {
10835 /* This is trampoline. */
10836 loc += offset;
df58fc94
RS
10837 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10838 {
d21911ea
MR
10839 bfd_put_micromips_32 (hti->output_bfd,
10840 LA25_LUI_MICROMIPS (target_high), loc);
10841 bfd_put_micromips_32 (hti->output_bfd,
10842 LA25_J_MICROMIPS (target), loc + 4);
10843 bfd_put_micromips_32 (hti->output_bfd,
10844 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
df58fc94
RS
10845 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10846 }
10847 else
10848 {
10849 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
3734320d
MF
10850 if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10851 {
10852 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10853 bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10854 }
10855 else
10856 {
10857 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10858 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10859 }
df58fc94
RS
10860 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10861 }
861fb55a 10862 }
0a1b45a2 10863 return true;
861fb55a
DJ
10864}
10865
b49e97c9
TS
10866/* If NAME is one of the special IRIX6 symbols defined by the linker,
10867 adjust it appropriately now. */
10868
10869static void
9719ad41
RS
10870mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10871 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
10872{
10873 /* The linker script takes care of providing names and values for
10874 these, but we must place them into the right sections. */
10875 static const char* const text_section_symbols[] = {
10876 "_ftext",
10877 "_etext",
10878 "__dso_displacement",
10879 "__elf_header",
10880 "__program_header_table",
10881 NULL
10882 };
10883
10884 static const char* const data_section_symbols[] = {
10885 "_fdata",
10886 "_edata",
10887 "_end",
10888 "_fbss",
10889 NULL
10890 };
10891
10892 const char* const *p;
10893 int i;
10894
10895 for (i = 0; i < 2; ++i)
10896 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10897 *p;
10898 ++p)
10899 if (strcmp (*p, name) == 0)
10900 {
10901 /* All of these symbols are given type STT_SECTION by the
10902 IRIX6 linker. */
10903 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 10904 sym->st_other = STO_PROTECTED;
b49e97c9
TS
10905
10906 /* The IRIX linker puts these symbols in special sections. */
10907 if (i == 0)
10908 sym->st_shndx = SHN_MIPS_TEXT;
10909 else
10910 sym->st_shndx = SHN_MIPS_DATA;
10911
10912 break;
10913 }
10914}
10915
10916/* Finish up dynamic symbol handling. We set the contents of various
10917 dynamic sections here. */
10918
0a1b45a2 10919bool
9719ad41
RS
10920_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10921 struct bfd_link_info *info,
10922 struct elf_link_hash_entry *h,
10923 Elf_Internal_Sym *sym)
b49e97c9
TS
10924{
10925 bfd *dynobj;
b49e97c9 10926 asection *sgot;
f4416af6 10927 struct mips_got_info *g, *gg;
b49e97c9 10928 const char *name;
3d6746ca 10929 int idx;
5108fc1b 10930 struct mips_elf_link_hash_table *htab;
738e5348 10931 struct mips_elf_link_hash_entry *hmips;
b49e97c9 10932
5108fc1b 10933 htab = mips_elf_hash_table (info);
4dfe6ac6 10934 BFD_ASSERT (htab != NULL);
b49e97c9 10935 dynobj = elf_hash_table (info)->dynobj;
738e5348 10936 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 10937
90c14f0c 10938 BFD_ASSERT (htab->root.target_os != is_vxworks);
861fb55a 10939
1bbce132
MR
10940 if (h->plt.plist != NULL
10941 && (h->plt.plist->mips_offset != MINUS_ONE
10942 || h->plt.plist->comp_offset != MINUS_ONE))
861fb55a
DJ
10943 {
10944 /* We've decided to create a PLT entry for this symbol. */
10945 bfd_byte *loc;
1bbce132 10946 bfd_vma header_address, got_address;
861fb55a 10947 bfd_vma got_address_high, got_address_low, load;
1bbce132
MR
10948 bfd_vma got_index;
10949 bfd_vma isa_bit;
10950
10951 got_index = h->plt.plist->gotplt_index;
861fb55a
DJ
10952
10953 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10954 BFD_ASSERT (h->dynindx != -1);
ce558b89 10955 BFD_ASSERT (htab->root.splt != NULL);
1bbce132 10956 BFD_ASSERT (got_index != MINUS_ONE);
861fb55a
DJ
10957 BFD_ASSERT (!h->def_regular);
10958
10959 /* Calculate the address of the PLT header. */
1bbce132 10960 isa_bit = htab->plt_header_is_comp;
ce558b89
AM
10961 header_address = (htab->root.splt->output_section->vma
10962 + htab->root.splt->output_offset + isa_bit);
861fb55a
DJ
10963
10964 /* Calculate the address of the .got.plt entry. */
ce558b89
AM
10965 got_address = (htab->root.sgotplt->output_section->vma
10966 + htab->root.sgotplt->output_offset
1bbce132
MR
10967 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10968
861fb55a
DJ
10969 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10970 got_address_low = got_address & 0xffff;
10971
789ff5b6
MR
10972 /* The PLT sequence is not safe for N64 if .got.plt entry's address
10973 cannot be loaded in two instructions. */
10974 if (ABI_64_P (output_bfd)
10975 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
10976 {
10977 _bfd_error_handler
10978 /* xgettext:c-format */
10979 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
10980 "supported; consider using `-Ttext-segment=...'"),
10981 output_bfd,
10982 htab->root.sgotplt->output_section,
10983 (int64_t) got_address);
10984 bfd_set_error (bfd_error_no_error);
0a1b45a2 10985 return false;
789ff5b6
MR
10986 }
10987
861fb55a 10988 /* Initially point the .got.plt entry at the PLT header. */
6a382bce
MR
10989 loc = (htab->root.sgotplt->contents
10990 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
861fb55a
DJ
10991 if (ABI_64_P (output_bfd))
10992 bfd_put_64 (output_bfd, header_address, loc);
10993 else
10994 bfd_put_32 (output_bfd, header_address, loc);
10995
1bbce132 10996 /* Now handle the PLT itself. First the standard entry (the order
07d6d2b8 10997 does not matter, we just have to pick one). */
1bbce132
MR
10998 if (h->plt.plist->mips_offset != MINUS_ONE)
10999 {
11000 const bfd_vma *plt_entry;
11001 bfd_vma plt_offset;
861fb55a 11002
1bbce132 11003 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
861fb55a 11004
ce558b89 11005 BFD_ASSERT (plt_offset <= htab->root.splt->size);
6d30f5b2 11006
1bbce132 11007 /* Find out where the .plt entry should go. */
ce558b89 11008 loc = htab->root.splt->contents + plt_offset;
1bbce132
MR
11009
11010 /* Pick the load opcode. */
11011 load = MIPS_ELF_LOAD_WORD (output_bfd);
11012
11013 /* Fill in the PLT entry itself. */
7361da2c
AB
11014
11015 if (MIPSR6_P (output_bfd))
3734320d
MF
11016 plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
11017 : mipsr6_exec_plt_entry;
7361da2c
AB
11018 else
11019 plt_entry = mips_exec_plt_entry;
1bbce132
MR
11020 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
11021 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
11022 loc + 4);
11023
3734320d
MF
11024 if (! LOAD_INTERLOCKS_P (output_bfd)
11025 || (MIPSR6_P (output_bfd) && htab->compact_branches))
1bbce132
MR
11026 {
11027 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
11028 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11029 }
11030 else
11031 {
11032 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
11033 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
11034 loc + 12);
11035 }
6d30f5b2 11036 }
1bbce132
MR
11037
11038 /* Now the compressed entry. They come after any standard ones. */
11039 if (h->plt.plist->comp_offset != MINUS_ONE)
6d30f5b2 11040 {
1bbce132
MR
11041 bfd_vma plt_offset;
11042
11043 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
11044 + h->plt.plist->comp_offset);
11045
ce558b89 11046 BFD_ASSERT (plt_offset <= htab->root.splt->size);
1bbce132
MR
11047
11048 /* Find out where the .plt entry should go. */
ce558b89 11049 loc = htab->root.splt->contents + plt_offset;
1bbce132
MR
11050
11051 /* Fill in the PLT entry itself. */
833794fc
MR
11052 if (!MICROMIPS_P (output_bfd))
11053 {
11054 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11055
11056 bfd_put_16 (output_bfd, plt_entry[0], loc);
11057 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11058 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11059 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11060 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11061 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11062 bfd_put_32 (output_bfd, got_address, loc + 12);
11063 }
11064 else if (htab->insn32)
11065 {
11066 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11067
11068 bfd_put_16 (output_bfd, plt_entry[0], loc);
11069 bfd_put_16 (output_bfd, got_address_high, loc + 2);
11070 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11071 bfd_put_16 (output_bfd, got_address_low, loc + 6);
11072 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11073 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11074 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11075 bfd_put_16 (output_bfd, got_address_low, loc + 14);
11076 }
11077 else
1bbce132
MR
11078 {
11079 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11080 bfd_signed_vma gotpc_offset;
11081 bfd_vma loc_address;
11082
11083 BFD_ASSERT (got_address % 4 == 0);
11084
ce558b89
AM
11085 loc_address = (htab->root.splt->output_section->vma
11086 + htab->root.splt->output_offset + plt_offset);
1bbce132
MR
11087 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11088
11089 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11090 if (gotpc_offset + 0x1000000 >= 0x2000000)
11091 {
4eca0228 11092 _bfd_error_handler
695344c0 11093 /* xgettext:c-format */
2dcf00ce 11094 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
1bbce132
MR
11095 "beyond the range of ADDIUPC"),
11096 output_bfd,
ce558b89 11097 htab->root.sgotplt->output_section,
2dcf00ce 11098 (int64_t) gotpc_offset,
c08bb8dd 11099 htab->root.splt->output_section);
1bbce132 11100 bfd_set_error (bfd_error_no_error);
0a1b45a2 11101 return false;
1bbce132
MR
11102 }
11103 bfd_put_16 (output_bfd,
11104 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11105 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11106 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11107 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11108 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11109 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11110 }
6d30f5b2 11111 }
861fb55a
DJ
11112
11113 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
ce558b89 11114 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
1bbce132 11115 got_index - 2, h->dynindx,
861fb55a
DJ
11116 R_MIPS_JUMP_SLOT, got_address);
11117
11118 /* We distinguish between PLT entries and lazy-binding stubs by
11119 giving the former an st_other value of STO_MIPS_PLT. Set the
11120 flag and leave the value if there are any relocations in the
11121 binary where pointer equality matters. */
11122 sym->st_shndx = SHN_UNDEF;
11123 if (h->pointer_equality_needed)
1bbce132 11124 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
861fb55a 11125 else
1bbce132
MR
11126 {
11127 sym->st_value = 0;
11128 sym->st_other = 0;
11129 }
861fb55a 11130 }
1bbce132
MR
11131
11132 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
b49e97c9 11133 {
861fb55a 11134 /* We've decided to create a lazy-binding stub. */
0a1b45a2 11135 bool micromips_p = MICROMIPS_P (output_bfd);
1bbce132
MR
11136 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11137 bfd_vma stub_size = htab->function_stub_size;
5108fc1b 11138 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
1bbce132
MR
11139 bfd_vma isa_bit = micromips_p;
11140 bfd_vma stub_big_size;
11141
833794fc 11142 if (!micromips_p)
1bbce132 11143 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
833794fc
MR
11144 else if (htab->insn32)
11145 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11146 else
11147 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
b49e97c9
TS
11148
11149 /* This symbol has a stub. Set it up. */
11150
11151 BFD_ASSERT (h->dynindx != -1);
11152
1bbce132 11153 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
3d6746ca
DD
11154
11155 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
11156 sign extension at runtime in the stub, resulting in a negative
11157 index value. */
11158 if (h->dynindx & ~0x7fffffff)
0a1b45a2 11159 return false;
b49e97c9
TS
11160
11161 /* Fill the stub. */
1bbce132
MR
11162 if (micromips_p)
11163 {
11164 idx = 0;
11165 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11166 stub + idx);
11167 idx += 4;
833794fc
MR
11168 if (htab->insn32)
11169 {
11170 bfd_put_micromips_32 (output_bfd,
40fc1451 11171 STUB_MOVE32_MICROMIPS, stub + idx);
833794fc
MR
11172 idx += 4;
11173 }
11174 else
11175 {
11176 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11177 idx += 2;
11178 }
1bbce132
MR
11179 if (stub_size == stub_big_size)
11180 {
11181 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11182
11183 bfd_put_micromips_32 (output_bfd,
11184 STUB_LUI_MICROMIPS (dynindx_hi),
11185 stub + idx);
11186 idx += 4;
11187 }
833794fc
MR
11188 if (htab->insn32)
11189 {
11190 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11191 stub + idx);
11192 idx += 4;
11193 }
11194 else
11195 {
11196 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11197 idx += 2;
11198 }
1bbce132
MR
11199
11200 /* If a large stub is not required and sign extension is not a
11201 problem, then use legacy code in the stub. */
11202 if (stub_size == stub_big_size)
11203 bfd_put_micromips_32 (output_bfd,
11204 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11205 stub + idx);
11206 else if (h->dynindx & ~0x7fff)
11207 bfd_put_micromips_32 (output_bfd,
11208 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11209 stub + idx);
11210 else
11211 bfd_put_micromips_32 (output_bfd,
11212 STUB_LI16S_MICROMIPS (output_bfd,
11213 h->dynindx),
11214 stub + idx);
11215 }
3d6746ca 11216 else
1bbce132
MR
11217 {
11218 idx = 0;
11219 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11220 idx += 4;
40fc1451 11221 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
1bbce132
MR
11222 idx += 4;
11223 if (stub_size == stub_big_size)
11224 {
11225 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11226 stub + idx);
11227 idx += 4;
11228 }
3734320d
MF
11229
11230 if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11231 {
11232 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11233 idx += 4;
11234 }
1bbce132
MR
11235
11236 /* If a large stub is not required and sign extension is not a
11237 problem, then use legacy code in the stub. */
11238 if (stub_size == stub_big_size)
11239 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11240 stub + idx);
11241 else if (h->dynindx & ~0x7fff)
11242 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11243 stub + idx);
11244 else
11245 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11246 stub + idx);
3734320d
MF
11247 idx += 4;
11248
11249 if (MIPSR6_P (output_bfd) && htab->compact_branches)
11250 bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
1bbce132 11251 }
5108fc1b 11252
1bbce132
MR
11253 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11254 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11255 stub, stub_size);
b49e97c9 11256
1bbce132 11257 /* Mark the symbol as undefined. stub_offset != -1 occurs
b49e97c9
TS
11258 only for the referenced symbol. */
11259 sym->st_shndx = SHN_UNDEF;
11260
11261 /* The run-time linker uses the st_value field of the symbol
11262 to reset the global offset table entry for this external
11263 to its stub address when unlinking a shared object. */
4e41d0d7
RS
11264 sym->st_value = (htab->sstubs->output_section->vma
11265 + htab->sstubs->output_offset
1bbce132
MR
11266 + h->plt.plist->stub_offset
11267 + isa_bit);
11268 sym->st_other = other;
b49e97c9
TS
11269 }
11270
738e5348
RS
11271 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11272 refer to the stub, since only the stub uses the standard calling
11273 conventions. */
11274 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11275 {
11276 BFD_ASSERT (hmips->need_fn_stub);
11277 sym->st_value = (hmips->fn_stub->output_section->vma
11278 + hmips->fn_stub->output_offset);
11279 sym->st_size = hmips->fn_stub->size;
11280 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11281 }
11282
b49e97c9 11283 BFD_ASSERT (h->dynindx != -1
f5385ebf 11284 || h->forced_local);
b49e97c9 11285
ce558b89 11286 sgot = htab->root.sgot;
a8028dd0 11287 g = htab->got_info;
b49e97c9
TS
11288 BFD_ASSERT (g != NULL);
11289
11290 /* Run through the global symbol table, creating GOT entries for all
11291 the symbols that need them. */
020d7251 11292 if (hmips->global_got_area != GGA_NONE)
b49e97c9
TS
11293 {
11294 bfd_vma offset;
11295 bfd_vma value;
11296
6eaa6adc 11297 value = sym->st_value;
13fbec83 11298 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
b49e97c9
TS
11299 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11300 }
11301
e641e783 11302 if (hmips->global_got_area != GGA_NONE && g->next)
f4416af6
AO
11303 {
11304 struct mips_got_entry e, *p;
0626d451 11305 bfd_vma entry;
f4416af6 11306 bfd_vma offset;
f4416af6
AO
11307
11308 gg = g;
11309
11310 e.abfd = output_bfd;
11311 e.symndx = -1;
738e5348 11312 e.d.h = hmips;
9ab066b4 11313 e.tls_type = GOT_TLS_NONE;
143d77c5 11314
f4416af6
AO
11315 for (g = g->next; g->next != gg; g = g->next)
11316 {
11317 if (g->got_entries
11318 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11319 &e)))
11320 {
11321 offset = p->gotidx;
ce558b89 11322 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
0e1862bb 11323 if (bfd_link_pic (info)
0626d451
RS
11324 || (elf_hash_table (info)->dynamic_sections_created
11325 && p->d.h != NULL
f5385ebf
AM
11326 && p->d.h->root.def_dynamic
11327 && !p->d.h->root.def_regular))
0626d451
RS
11328 {
11329 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11330 the various compatibility problems, it's easier to mock
11331 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11332 mips_elf_create_dynamic_relocation to calculate the
11333 appropriate addend. */
11334 Elf_Internal_Rela rel[3];
11335
11336 memset (rel, 0, sizeof (rel));
11337 if (ABI_64_P (output_bfd))
11338 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11339 else
11340 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11341 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11342
11343 entry = 0;
11344 if (! (mips_elf_create_dynamic_relocation
11345 (output_bfd, info, rel,
11346 e.d.h, NULL, sym->st_value, &entry, sgot)))
0a1b45a2 11347 return false;
0626d451
RS
11348 }
11349 else
11350 entry = sym->st_value;
11351 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
11352 }
11353 }
11354 }
11355
b49e97c9
TS
11356 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11357 name = h->root.root.string;
9637f6ef 11358 if (h == elf_hash_table (info)->hdynamic
22edb2f1 11359 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
11360 sym->st_shndx = SHN_ABS;
11361 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11362 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11363 {
11364 sym->st_shndx = SHN_ABS;
11365 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11366 sym->st_value = 1;
11367 }
b49e97c9
TS
11368 else if (SGI_COMPAT (output_bfd))
11369 {
11370 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11371 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11372 {
11373 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11374 sym->st_other = STO_PROTECTED;
11375 sym->st_value = 0;
11376 sym->st_shndx = SHN_MIPS_DATA;
11377 }
11378 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11379 {
11380 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11381 sym->st_other = STO_PROTECTED;
11382 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11383 sym->st_shndx = SHN_ABS;
11384 }
11385 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11386 {
11387 if (h->type == STT_FUNC)
11388 sym->st_shndx = SHN_MIPS_TEXT;
11389 else if (h->type == STT_OBJECT)
11390 sym->st_shndx = SHN_MIPS_DATA;
11391 }
11392 }
11393
861fb55a
DJ
11394 /* Emit a copy reloc, if needed. */
11395 if (h->needs_copy)
11396 {
11397 asection *s;
11398 bfd_vma symval;
11399
11400 BFD_ASSERT (h->dynindx != -1);
11401 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11402
0a1b45a2 11403 s = mips_elf_rel_dyn_section (info, false);
861fb55a
DJ
11404 symval = (h->root.u.def.section->output_section->vma
11405 + h->root.u.def.section->output_offset
11406 + h->root.u.def.value);
11407 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11408 h->dynindx, R_MIPS_COPY, symval);
11409 }
11410
b49e97c9
TS
11411 /* Handle the IRIX6-specific symbols. */
11412 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11413 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11414
cbf8d970
MR
11415 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11416 to treat compressed symbols like any other. */
30c09090 11417 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
11418 {
11419 BFD_ASSERT (sym->st_value & 1);
11420 sym->st_other -= STO_MIPS16;
11421 }
cbf8d970
MR
11422 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11423 {
11424 BFD_ASSERT (sym->st_value & 1);
11425 sym->st_other -= STO_MICROMIPS;
11426 }
b49e97c9 11427
0a1b45a2 11428 return true;
b49e97c9
TS
11429}
11430
0a44bf69
RS
11431/* Likewise, for VxWorks. */
11432
0a1b45a2 11433bool
0a44bf69
RS
11434_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11435 struct bfd_link_info *info,
11436 struct elf_link_hash_entry *h,
11437 Elf_Internal_Sym *sym)
11438{
11439 bfd *dynobj;
11440 asection *sgot;
11441 struct mips_got_info *g;
11442 struct mips_elf_link_hash_table *htab;
020d7251 11443 struct mips_elf_link_hash_entry *hmips;
0a44bf69
RS
11444
11445 htab = mips_elf_hash_table (info);
4dfe6ac6 11446 BFD_ASSERT (htab != NULL);
0a44bf69 11447 dynobj = elf_hash_table (info)->dynobj;
020d7251 11448 hmips = (struct mips_elf_link_hash_entry *) h;
0a44bf69 11449
1bbce132 11450 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
0a44bf69 11451 {
6d79d2ed 11452 bfd_byte *loc;
1bbce132 11453 bfd_vma plt_address, got_address, got_offset, branch_offset;
0a44bf69
RS
11454 Elf_Internal_Rela rel;
11455 static const bfd_vma *plt_entry;
1bbce132
MR
11456 bfd_vma gotplt_index;
11457 bfd_vma plt_offset;
11458
11459 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11460 gotplt_index = h->plt.plist->gotplt_index;
0a44bf69
RS
11461
11462 BFD_ASSERT (h->dynindx != -1);
ce558b89 11463 BFD_ASSERT (htab->root.splt != NULL);
1bbce132 11464 BFD_ASSERT (gotplt_index != MINUS_ONE);
ce558b89 11465 BFD_ASSERT (plt_offset <= htab->root.splt->size);
0a44bf69
RS
11466
11467 /* Calculate the address of the .plt entry. */
ce558b89
AM
11468 plt_address = (htab->root.splt->output_section->vma
11469 + htab->root.splt->output_offset
1bbce132 11470 + plt_offset);
0a44bf69
RS
11471
11472 /* Calculate the address of the .got.plt entry. */
ce558b89
AM
11473 got_address = (htab->root.sgotplt->output_section->vma
11474 + htab->root.sgotplt->output_offset
1bbce132 11475 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
0a44bf69
RS
11476
11477 /* Calculate the offset of the .got.plt entry from
11478 _GLOBAL_OFFSET_TABLE_. */
11479 got_offset = mips_elf_gotplt_index (info, h);
11480
11481 /* Calculate the offset for the branch at the start of the PLT
11482 entry. The branch jumps to the beginning of .plt. */
1bbce132 11483 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
0a44bf69
RS
11484
11485 /* Fill in the initial value of the .got.plt entry. */
11486 bfd_put_32 (output_bfd, plt_address,
ce558b89 11487 (htab->root.sgotplt->contents
1bbce132 11488 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
0a44bf69
RS
11489
11490 /* Find out where the .plt entry should go. */
ce558b89 11491 loc = htab->root.splt->contents + plt_offset;
0a44bf69 11492
0e1862bb 11493 if (bfd_link_pic (info))
0a44bf69
RS
11494 {
11495 plt_entry = mips_vxworks_shared_plt_entry;
11496 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 11497 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
11498 }
11499 else
11500 {
11501 bfd_vma got_address_high, got_address_low;
11502
11503 plt_entry = mips_vxworks_exec_plt_entry;
11504 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11505 got_address_low = got_address & 0xffff;
11506
11507 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 11508 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
11509 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11510 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11511 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11512 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11513 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11514 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11515
11516 loc = (htab->srelplt2->contents
1bbce132 11517 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
0a44bf69
RS
11518
11519 /* Emit a relocation for the .got.plt entry. */
11520 rel.r_offset = got_address;
11521 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
1bbce132 11522 rel.r_addend = plt_offset;
0a44bf69
RS
11523 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11524
11525 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11526 loc += sizeof (Elf32_External_Rela);
11527 rel.r_offset = plt_address + 8;
11528 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11529 rel.r_addend = got_offset;
11530 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11531
11532 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11533 loc += sizeof (Elf32_External_Rela);
11534 rel.r_offset += 4;
11535 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11536 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11537 }
11538
11539 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
ce558b89 11540 loc = (htab->root.srelplt->contents
1bbce132 11541 + gotplt_index * sizeof (Elf32_External_Rela));
0a44bf69
RS
11542 rel.r_offset = got_address;
11543 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11544 rel.r_addend = 0;
11545 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11546
11547 if (!h->def_regular)
11548 sym->st_shndx = SHN_UNDEF;
11549 }
11550
11551 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11552
ce558b89 11553 sgot = htab->root.sgot;
a8028dd0 11554 g = htab->got_info;
0a44bf69
RS
11555 BFD_ASSERT (g != NULL);
11556
11557 /* See if this symbol has an entry in the GOT. */
020d7251 11558 if (hmips->global_got_area != GGA_NONE)
0a44bf69
RS
11559 {
11560 bfd_vma offset;
11561 Elf_Internal_Rela outrel;
11562 bfd_byte *loc;
11563 asection *s;
11564
11565 /* Install the symbol value in the GOT. */
13fbec83 11566 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
0a44bf69
RS
11567 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11568
11569 /* Add a dynamic relocation for it. */
0a1b45a2 11570 s = mips_elf_rel_dyn_section (info, false);
0a44bf69
RS
11571 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11572 outrel.r_offset = (sgot->output_section->vma
11573 + sgot->output_offset
11574 + offset);
11575 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11576 outrel.r_addend = 0;
11577 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11578 }
11579
11580 /* Emit a copy reloc, if needed. */
11581 if (h->needs_copy)
11582 {
11583 Elf_Internal_Rela rel;
5474d94f
AM
11584 asection *srel;
11585 bfd_byte *loc;
0a44bf69
RS
11586
11587 BFD_ASSERT (h->dynindx != -1);
11588
11589 rel.r_offset = (h->root.u.def.section->output_section->vma
11590 + h->root.u.def.section->output_offset
11591 + h->root.u.def.value);
11592 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11593 rel.r_addend = 0;
afbf7e8e 11594 if (h->root.u.def.section == htab->root.sdynrelro)
5474d94f
AM
11595 srel = htab->root.sreldynrelro;
11596 else
11597 srel = htab->root.srelbss;
11598 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11599 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11600 ++srel->reloc_count;
0a44bf69
RS
11601 }
11602
df58fc94
RS
11603 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11604 if (ELF_ST_IS_COMPRESSED (sym->st_other))
0a44bf69
RS
11605 sym->st_value &= ~1;
11606
0a1b45a2 11607 return true;
0a44bf69
RS
11608}
11609
861fb55a
DJ
11610/* Write out a plt0 entry to the beginning of .plt. */
11611
0a1b45a2 11612static bool
861fb55a
DJ
11613mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11614{
11615 bfd_byte *loc;
11616 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11617 static const bfd_vma *plt_entry;
11618 struct mips_elf_link_hash_table *htab;
11619
11620 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11621 BFD_ASSERT (htab != NULL);
11622
861fb55a 11623 if (ABI_64_P (output_bfd))
3734320d
MF
11624 plt_entry = (htab->compact_branches
11625 ? mipsr6_n64_exec_plt0_entry_compact
11626 : mips_n64_exec_plt0_entry);
861fb55a 11627 else if (ABI_N32_P (output_bfd))
3734320d
MF
11628 plt_entry = (htab->compact_branches
11629 ? mipsr6_n32_exec_plt0_entry_compact
11630 : mips_n32_exec_plt0_entry);
833794fc 11631 else if (!htab->plt_header_is_comp)
3734320d
MF
11632 plt_entry = (htab->compact_branches
11633 ? mipsr6_o32_exec_plt0_entry_compact
11634 : mips_o32_exec_plt0_entry);
833794fc
MR
11635 else if (htab->insn32)
11636 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11637 else
11638 plt_entry = micromips_o32_exec_plt0_entry;
861fb55a
DJ
11639
11640 /* Calculate the value of .got.plt. */
ce558b89
AM
11641 gotplt_value = (htab->root.sgotplt->output_section->vma
11642 + htab->root.sgotplt->output_offset);
861fb55a
DJ
11643 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11644 gotplt_value_low = gotplt_value & 0xffff;
11645
11646 /* The PLT sequence is not safe for N64 if .got.plt's address can
11647 not be loaded in two instructions. */
789ff5b6
MR
11648 if (ABI_64_P (output_bfd)
11649 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11650 {
11651 _bfd_error_handler
11652 /* xgettext:c-format */
11653 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11654 "supported; consider using `-Ttext-segment=...'"),
11655 output_bfd,
11656 htab->root.sgotplt->output_section,
11657 (int64_t) gotplt_value);
11658 bfd_set_error (bfd_error_no_error);
0a1b45a2 11659 return false;
789ff5b6 11660 }
861fb55a
DJ
11661
11662 /* Install the PLT header. */
ce558b89 11663 loc = htab->root.splt->contents;
1bbce132
MR
11664 if (plt_entry == micromips_o32_exec_plt0_entry)
11665 {
11666 bfd_vma gotpc_offset;
11667 bfd_vma loc_address;
11668 size_t i;
11669
11670 BFD_ASSERT (gotplt_value % 4 == 0);
11671
ce558b89
AM
11672 loc_address = (htab->root.splt->output_section->vma
11673 + htab->root.splt->output_offset);
1bbce132
MR
11674 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11675
11676 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11677 if (gotpc_offset + 0x1000000 >= 0x2000000)
11678 {
4eca0228 11679 _bfd_error_handler
695344c0 11680 /* xgettext:c-format */
2dcf00ce
AM
11681 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11682 "beyond the range of ADDIUPC"),
1bbce132 11683 output_bfd,
ce558b89 11684 htab->root.sgotplt->output_section,
2dcf00ce 11685 (int64_t) gotpc_offset,
c08bb8dd 11686 htab->root.splt->output_section);
1bbce132 11687 bfd_set_error (bfd_error_no_error);
0a1b45a2 11688 return false;
1bbce132
MR
11689 }
11690 bfd_put_16 (output_bfd,
11691 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11692 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11693 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11694 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11695 }
833794fc
MR
11696 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11697 {
11698 size_t i;
11699
11700 bfd_put_16 (output_bfd, plt_entry[0], loc);
11701 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11702 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11703 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11704 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11705 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11706 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11707 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11708 }
1bbce132
MR
11709 else
11710 {
11711 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11712 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11713 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11714 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11715 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11716 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11717 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11718 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11719 }
11720
0a1b45a2 11721 return true;
861fb55a
DJ
11722}
11723
0a44bf69
RS
11724/* Install the PLT header for a VxWorks executable and finalize the
11725 contents of .rela.plt.unloaded. */
11726
11727static void
11728mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11729{
11730 Elf_Internal_Rela rela;
11731 bfd_byte *loc;
11732 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11733 static const bfd_vma *plt_entry;
11734 struct mips_elf_link_hash_table *htab;
11735
11736 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11737 BFD_ASSERT (htab != NULL);
11738
0a44bf69
RS
11739 plt_entry = mips_vxworks_exec_plt0_entry;
11740
11741 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11742 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11743 + htab->root.hgot->root.u.def.section->output_offset
11744 + htab->root.hgot->root.u.def.value);
11745
11746 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11747 got_value_low = got_value & 0xffff;
11748
11749 /* Calculate the address of the PLT header. */
ce558b89
AM
11750 plt_address = (htab->root.splt->output_section->vma
11751 + htab->root.splt->output_offset);
0a44bf69
RS
11752
11753 /* Install the PLT header. */
ce558b89 11754 loc = htab->root.splt->contents;
0a44bf69
RS
11755 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11756 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11757 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11758 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11759 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11760 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11761
11762 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11763 loc = htab->srelplt2->contents;
11764 rela.r_offset = plt_address;
11765 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11766 rela.r_addend = 0;
11767 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11768 loc += sizeof (Elf32_External_Rela);
11769
11770 /* Output the relocation for the following addiu of
11771 %lo(_GLOBAL_OFFSET_TABLE_). */
11772 rela.r_offset += 4;
11773 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11774 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11775 loc += sizeof (Elf32_External_Rela);
11776
11777 /* Fix up the remaining relocations. They may have the wrong
11778 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11779 in which symbols were output. */
11780 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11781 {
11782 Elf_Internal_Rela rel;
11783
11784 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11785 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11786 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11787 loc += sizeof (Elf32_External_Rela);
11788
11789 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11790 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11791 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11792 loc += sizeof (Elf32_External_Rela);
11793
11794 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11795 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11796 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11797 loc += sizeof (Elf32_External_Rela);
11798 }
11799}
11800
11801/* Install the PLT header for a VxWorks shared library. */
11802
11803static void
11804mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11805{
11806 unsigned int i;
11807 struct mips_elf_link_hash_table *htab;
11808
11809 htab = mips_elf_hash_table (info);
4dfe6ac6 11810 BFD_ASSERT (htab != NULL);
0a44bf69
RS
11811
11812 /* We just need to copy the entry byte-by-byte. */
11813 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11814 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
ce558b89 11815 htab->root.splt->contents + i * 4);
0a44bf69
RS
11816}
11817
b49e97c9
TS
11818/* Finish up the dynamic sections. */
11819
0a1b45a2 11820bool
9719ad41
RS
11821_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11822 struct bfd_link_info *info)
b49e97c9
TS
11823{
11824 bfd *dynobj;
11825 asection *sdyn;
11826 asection *sgot;
f4416af6 11827 struct mips_got_info *gg, *g;
0a44bf69 11828 struct mips_elf_link_hash_table *htab;
b49e97c9 11829
0a44bf69 11830 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11831 BFD_ASSERT (htab != NULL);
11832
b49e97c9
TS
11833 dynobj = elf_hash_table (info)->dynobj;
11834
3d4d4302 11835 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
b49e97c9 11836
ce558b89 11837 sgot = htab->root.sgot;
23cc69b6 11838 gg = htab->got_info;
b49e97c9
TS
11839
11840 if (elf_hash_table (info)->dynamic_sections_created)
11841 {
11842 bfd_byte *b;
943284cc 11843 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
11844
11845 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
11846 BFD_ASSERT (gg != NULL);
11847
0a1b45a2 11848 g = mips_elf_bfd_got (output_bfd, false);
b49e97c9
TS
11849 BFD_ASSERT (g != NULL);
11850
11851 for (b = sdyn->contents;
eea6121a 11852 b < sdyn->contents + sdyn->size;
b49e97c9
TS
11853 b += MIPS_ELF_DYN_SIZE (dynobj))
11854 {
11855 Elf_Internal_Dyn dyn;
11856 const char *name;
11857 size_t elemsize;
11858 asection *s;
0a1b45a2 11859 bool swap_out_p;
b49e97c9
TS
11860
11861 /* Read in the current dynamic entry. */
11862 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11863
11864 /* Assume that we're going to modify it and write it out. */
0a1b45a2 11865 swap_out_p = true;
b49e97c9
TS
11866
11867 switch (dyn.d_tag)
11868 {
11869 case DT_RELENT:
b49e97c9
TS
11870 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11871 break;
11872
0a44bf69 11873 case DT_RELAENT:
90c14f0c 11874 BFD_ASSERT (htab->root.target_os == is_vxworks);
0a44bf69
RS
11875 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11876 break;
11877
b49e97c9
TS
11878 case DT_STRSZ:
11879 /* Rewrite DT_STRSZ. */
11880 dyn.d_un.d_val =
11881 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11882 break;
11883
11884 case DT_PLTGOT:
ce558b89 11885 s = htab->root.sgot;
861fb55a
DJ
11886 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11887 break;
11888
11889 case DT_MIPS_PLTGOT:
ce558b89 11890 s = htab->root.sgotplt;
861fb55a 11891 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
11892 break;
11893
11894 case DT_MIPS_RLD_VERSION:
11895 dyn.d_un.d_val = 1; /* XXX */
11896 break;
11897
11898 case DT_MIPS_FLAGS:
11899 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11900 break;
11901
b49e97c9 11902 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
11903 {
11904 time_t t;
11905 time (&t);
11906 dyn.d_un.d_val = t;
11907 }
b49e97c9
TS
11908 break;
11909
11910 case DT_MIPS_ICHECKSUM:
11911 /* XXX FIXME: */
0a1b45a2 11912 swap_out_p = false;
b49e97c9
TS
11913 break;
11914
11915 case DT_MIPS_IVERSION:
11916 /* XXX FIXME: */
0a1b45a2 11917 swap_out_p = false;
b49e97c9
TS
11918 break;
11919
11920 case DT_MIPS_BASE_ADDRESS:
11921 s = output_bfd->sections;
11922 BFD_ASSERT (s != NULL);
11923 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11924 break;
11925
11926 case DT_MIPS_LOCAL_GOTNO:
11927 dyn.d_un.d_val = g->local_gotno;
11928 break;
11929
11930 case DT_MIPS_UNREFEXTNO:
11931 /* The index into the dynamic symbol table which is the
11932 entry of the first external symbol that is not
11933 referenced within the same object. */
11934 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11935 break;
11936
11937 case DT_MIPS_GOTSYM:
d222d210 11938 if (htab->global_gotsym)
b49e97c9 11939 {
d222d210 11940 dyn.d_un.d_val = htab->global_gotsym->dynindx;
b49e97c9
TS
11941 break;
11942 }
11943 /* In case if we don't have global got symbols we default
11944 to setting DT_MIPS_GOTSYM to the same value as
1a0670f3
AM
11945 DT_MIPS_SYMTABNO. */
11946 /* Fall through. */
b49e97c9
TS
11947
11948 case DT_MIPS_SYMTABNO:
11949 name = ".dynsym";
11950 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
4ade44b7 11951 s = bfd_get_linker_section (dynobj, name);
b49e97c9 11952
131e2f8e
MF
11953 if (s != NULL)
11954 dyn.d_un.d_val = s->size / elemsize;
11955 else
11956 dyn.d_un.d_val = 0;
b49e97c9
TS
11957 break;
11958
11959 case DT_MIPS_HIPAGENO:
861fb55a 11960 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
11961 break;
11962
11963 case DT_MIPS_RLD_MAP:
b4082c70
DD
11964 {
11965 struct elf_link_hash_entry *h;
11966 h = mips_elf_hash_table (info)->rld_symbol;
11967 if (!h)
11968 {
11969 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
0a1b45a2 11970 swap_out_p = false;
b4082c70
DD
11971 break;
11972 }
11973 s = h->root.u.def.section;
a5499fa4
MF
11974
11975 /* The MIPS_RLD_MAP tag stores the absolute address of the
11976 debug pointer. */
b4082c70
DD
11977 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11978 + h->root.u.def.value);
11979 }
b49e97c9
TS
11980 break;
11981
a5499fa4
MF
11982 case DT_MIPS_RLD_MAP_REL:
11983 {
11984 struct elf_link_hash_entry *h;
11985 bfd_vma dt_addr, rld_addr;
11986 h = mips_elf_hash_table (info)->rld_symbol;
11987 if (!h)
11988 {
11989 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
0a1b45a2 11990 swap_out_p = false;
a5499fa4
MF
11991 break;
11992 }
11993 s = h->root.u.def.section;
11994
11995 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
11996 pointer, relative to the address of the tag. */
11997 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
d5cff5df 11998 + (b - sdyn->contents));
a5499fa4
MF
11999 rld_addr = (s->output_section->vma + s->output_offset
12000 + h->root.u.def.value);
12001 dyn.d_un.d_ptr = rld_addr - dt_addr;
12002 }
12003 break;
12004
b49e97c9
TS
12005 case DT_MIPS_OPTIONS:
12006 s = (bfd_get_section_by_name
12007 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
12008 dyn.d_un.d_ptr = s->vma;
12009 break;
12010
0a44bf69 12011 case DT_PLTREL:
861fb55a 12012 BFD_ASSERT (htab->use_plts_and_copy_relocs);
90c14f0c 12013 if (htab->root.target_os == is_vxworks)
861fb55a
DJ
12014 dyn.d_un.d_val = DT_RELA;
12015 else
12016 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
12017 break;
12018
12019 case DT_PLTRELSZ:
861fb55a 12020 BFD_ASSERT (htab->use_plts_and_copy_relocs);
ce558b89 12021 dyn.d_un.d_val = htab->root.srelplt->size;
0a44bf69
RS
12022 break;
12023
12024 case DT_JMPREL:
861fb55a 12025 BFD_ASSERT (htab->use_plts_and_copy_relocs);
ce558b89
AM
12026 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
12027 + htab->root.srelplt->output_offset);
0a44bf69
RS
12028 break;
12029
943284cc
DJ
12030 case DT_TEXTREL:
12031 /* If we didn't need any text relocations after all, delete
12032 the dynamic tag. */
12033 if (!(info->flags & DF_TEXTREL))
12034 {
12035 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
0a1b45a2 12036 swap_out_p = false;
943284cc
DJ
12037 }
12038 break;
12039
12040 case DT_FLAGS:
12041 /* If we didn't need any text relocations after all, clear
12042 DF_TEXTREL from DT_FLAGS. */
12043 if (!(info->flags & DF_TEXTREL))
12044 dyn.d_un.d_val &= ~DF_TEXTREL;
12045 else
0a1b45a2 12046 swap_out_p = false;
943284cc
DJ
12047 break;
12048
f16a9783
MS
12049 case DT_MIPS_XHASH:
12050 name = ".MIPS.xhash";
12051 s = bfd_get_linker_section (dynobj, name);
12052 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
12053 break;
12054
b49e97c9 12055 default:
0a1b45a2 12056 swap_out_p = false;
90c14f0c 12057 if (htab->root.target_os == is_vxworks
7a2b07ff 12058 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
0a1b45a2 12059 swap_out_p = true;
b49e97c9
TS
12060 break;
12061 }
12062
943284cc 12063 if (swap_out_p || dyn_skipped)
b49e97c9 12064 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
12065 (dynobj, &dyn, b - dyn_skipped);
12066
12067 if (dyn_to_skip)
12068 {
12069 dyn_skipped += dyn_to_skip;
12070 dyn_to_skip = 0;
12071 }
b49e97c9 12072 }
943284cc
DJ
12073
12074 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
12075 if (dyn_skipped > 0)
12076 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
12077 }
12078
b55fd4d4
DJ
12079 if (sgot != NULL && sgot->size > 0
12080 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 12081 {
90c14f0c 12082 if (htab->root.target_os == is_vxworks)
0a44bf69
RS
12083 {
12084 /* The first entry of the global offset table points to the
12085 ".dynamic" section. The second is initialized by the
12086 loader and contains the shared library identifier.
12087 The third is also initialized by the loader and points
12088 to the lazy resolution stub. */
12089 MIPS_ELF_PUT_WORD (output_bfd,
12090 sdyn->output_offset + sdyn->output_section->vma,
12091 sgot->contents);
12092 MIPS_ELF_PUT_WORD (output_bfd, 0,
12093 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12094 MIPS_ELF_PUT_WORD (output_bfd, 0,
12095 sgot->contents
12096 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12097 }
12098 else
12099 {
12100 /* The first entry of the global offset table will be filled at
12101 runtime. The second entry will be used by some runtime loaders.
12102 This isn't the case of IRIX rld. */
12103 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 12104 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
12105 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12106 }
b49e97c9 12107
54938e2a
TS
12108 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12109 = MIPS_ELF_GOT_SIZE (output_bfd);
12110 }
b49e97c9 12111
f4416af6
AO
12112 /* Generate dynamic relocations for the non-primary gots. */
12113 if (gg != NULL && gg->next)
12114 {
12115 Elf_Internal_Rela rel[3];
12116 bfd_vma addend = 0;
12117
12118 memset (rel, 0, sizeof (rel));
12119 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12120
12121 for (g = gg->next; g->next != gg; g = g->next)
12122 {
91d6fa6a 12123 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 12124 + g->next->tls_gotno;
f4416af6 12125
9719ad41 12126 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 12127 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
12128 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12129 sgot->contents
91d6fa6a 12130 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6 12131
0e1862bb 12132 if (! bfd_link_pic (info))
f4416af6
AO
12133 continue;
12134
cb22ccf4 12135 for (; got_index < g->local_gotno; got_index++)
f4416af6 12136 {
cb22ccf4
KCY
12137 if (got_index >= g->assigned_low_gotno
12138 && got_index <= g->assigned_high_gotno)
12139 continue;
12140
f4416af6 12141 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
cb22ccf4 12142 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
12143 if (!(mips_elf_create_dynamic_relocation
12144 (output_bfd, info, rel, NULL,
12145 bfd_abs_section_ptr,
12146 0, &addend, sgot)))
0a1b45a2 12147 return false;
f4416af6
AO
12148 BFD_ASSERT (addend == 0);
12149 }
12150 }
12151 }
12152
3133ddbf
DJ
12153 /* The generation of dynamic relocations for the non-primary gots
12154 adds more dynamic relocations. We cannot count them until
12155 here. */
12156
12157 if (elf_hash_table (info)->dynamic_sections_created)
12158 {
12159 bfd_byte *b;
0a1b45a2 12160 bool swap_out_p;
3133ddbf
DJ
12161
12162 BFD_ASSERT (sdyn != NULL);
12163
12164 for (b = sdyn->contents;
12165 b < sdyn->contents + sdyn->size;
12166 b += MIPS_ELF_DYN_SIZE (dynobj))
12167 {
12168 Elf_Internal_Dyn dyn;
12169 asection *s;
12170
12171 /* Read in the current dynamic entry. */
12172 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12173
12174 /* Assume that we're going to modify it and write it out. */
0a1b45a2 12175 swap_out_p = true;
3133ddbf
DJ
12176
12177 switch (dyn.d_tag)
12178 {
12179 case DT_RELSZ:
12180 /* Reduce DT_RELSZ to account for any relocations we
12181 decided not to make. This is for the n64 irix rld,
12182 which doesn't seem to apply any relocations if there
12183 are trailing null entries. */
0a1b45a2 12184 s = mips_elf_rel_dyn_section (info, false);
3133ddbf
DJ
12185 dyn.d_un.d_val = (s->reloc_count
12186 * (ABI_64_P (output_bfd)
12187 ? sizeof (Elf64_Mips_External_Rel)
12188 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
12189 /* Adjust the section size too. Tools like the prelinker
12190 can reasonably expect the values to the same. */
db841b6f 12191 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
bcfdf036
RS
12192 elf_section_data (s->output_section)->this_hdr.sh_size
12193 = dyn.d_un.d_val;
3133ddbf
DJ
12194 break;
12195
12196 default:
0a1b45a2 12197 swap_out_p = false;
3133ddbf
DJ
12198 break;
12199 }
12200
12201 if (swap_out_p)
12202 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12203 (dynobj, &dyn, b);
12204 }
12205 }
12206
b49e97c9 12207 {
b49e97c9
TS
12208 asection *s;
12209 Elf32_compact_rel cpt;
12210
b49e97c9
TS
12211 if (SGI_COMPAT (output_bfd))
12212 {
12213 /* Write .compact_rel section out. */
3d4d4302 12214 s = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
12215 if (s != NULL)
12216 {
12217 cpt.id1 = 1;
12218 cpt.num = s->reloc_count;
12219 cpt.id2 = 2;
12220 cpt.offset = (s->output_section->filepos
12221 + sizeof (Elf32_External_compact_rel));
12222 cpt.reserved0 = 0;
12223 cpt.reserved1 = 0;
12224 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12225 ((Elf32_External_compact_rel *)
12226 s->contents));
12227
12228 /* Clean up a dummy stub function entry in .text. */
55e61b8a
AM
12229 if (htab->sstubs != NULL
12230 && htab->sstubs->contents != NULL)
b49e97c9
TS
12231 {
12232 file_ptr dummy_offset;
12233
4e41d0d7
RS
12234 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12235 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12236 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 12237 htab->function_stub_size);
b49e97c9
TS
12238 }
12239 }
12240 }
12241
0a44bf69
RS
12242 /* The psABI says that the dynamic relocations must be sorted in
12243 increasing order of r_symndx. The VxWorks EABI doesn't require
12244 this, and because the code below handles REL rather than RELA
12245 relocations, using it for VxWorks would be outright harmful. */
90c14f0c 12246 if (htab->root.target_os != is_vxworks)
b49e97c9 12247 {
0a1b45a2 12248 s = mips_elf_rel_dyn_section (info, false);
0a44bf69
RS
12249 if (s != NULL
12250 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12251 {
12252 reldyn_sorting_bfd = output_bfd;
b49e97c9 12253
0a44bf69
RS
12254 if (ABI_64_P (output_bfd))
12255 qsort ((Elf64_External_Rel *) s->contents + 1,
12256 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12257 sort_dynamic_relocs_64);
12258 else
12259 qsort ((Elf32_External_Rel *) s->contents + 1,
12260 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12261 sort_dynamic_relocs);
12262 }
b49e97c9 12263 }
b49e97c9
TS
12264 }
12265
ce558b89 12266 if (htab->root.splt && htab->root.splt->size > 0)
0a44bf69 12267 {
90c14f0c 12268 if (htab->root.target_os == is_vxworks)
861fb55a 12269 {
0e1862bb 12270 if (bfd_link_pic (info))
861fb55a
DJ
12271 mips_vxworks_finish_shared_plt (output_bfd, info);
12272 else
12273 mips_vxworks_finish_exec_plt (output_bfd, info);
12274 }
0a44bf69 12275 else
861fb55a 12276 {
0e1862bb 12277 BFD_ASSERT (!bfd_link_pic (info));
1bbce132 12278 if (!mips_finish_exec_plt (output_bfd, info))
0a1b45a2 12279 return false;
861fb55a 12280 }
0a44bf69 12281 }
0a1b45a2 12282 return true;
b49e97c9
TS
12283}
12284
b49e97c9 12285
64543e1a
RS
12286/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12287
12288static void
9719ad41 12289mips_set_isa_flags (bfd *abfd)
b49e97c9 12290{
64543e1a 12291 flagword val;
b49e97c9
TS
12292
12293 switch (bfd_get_mach (abfd))
12294 {
12295 default:
c7c860d2
YS
12296 if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12297 val = E_MIPS_ARCH_3;
12298 else
12299 val = E_MIPS_ARCH_1;
12300 break;
12301
b49e97c9
TS
12302 case bfd_mach_mips3000:
12303 val = E_MIPS_ARCH_1;
12304 break;
12305
12306 case bfd_mach_mips3900:
12307 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12308 break;
12309
12310 case bfd_mach_mips6000:
12311 val = E_MIPS_ARCH_2;
12312 break;
12313
b417536f
MR
12314 case bfd_mach_mips4010:
12315 val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12316 break;
12317
b49e97c9
TS
12318 case bfd_mach_mips4000:
12319 case bfd_mach_mips4300:
12320 case bfd_mach_mips4400:
12321 case bfd_mach_mips4600:
12322 val = E_MIPS_ARCH_3;
12323 break;
12324
b49e97c9
TS
12325 case bfd_mach_mips4100:
12326 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12327 break;
12328
12329 case bfd_mach_mips4111:
12330 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12331 break;
12332
00707a0e
RS
12333 case bfd_mach_mips4120:
12334 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12335 break;
12336
b49e97c9
TS
12337 case bfd_mach_mips4650:
12338 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12339 break;
12340
00707a0e
RS
12341 case bfd_mach_mips5400:
12342 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12343 break;
12344
12345 case bfd_mach_mips5500:
12346 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12347 break;
12348
e407c74b
NC
12349 case bfd_mach_mips5900:
12350 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12351 break;
12352
0d2e43ed
ILT
12353 case bfd_mach_mips9000:
12354 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12355 break;
12356
b49e97c9 12357 case bfd_mach_mips5000:
5a7ea749 12358 case bfd_mach_mips7000:
b49e97c9
TS
12359 case bfd_mach_mips8000:
12360 case bfd_mach_mips10000:
12361 case bfd_mach_mips12000:
3aa3176b
TS
12362 case bfd_mach_mips14000:
12363 case bfd_mach_mips16000:
b49e97c9
TS
12364 val = E_MIPS_ARCH_4;
12365 break;
12366
12367 case bfd_mach_mips5:
12368 val = E_MIPS_ARCH_5;
12369 break;
12370
350cc38d
MS
12371 case bfd_mach_mips_loongson_2e:
12372 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12373 break;
12374
12375 case bfd_mach_mips_loongson_2f:
12376 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12377 break;
12378
b49e97c9
TS
12379 case bfd_mach_mips_sb1:
12380 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12381 break;
12382
ac8cb70f
CX
12383 case bfd_mach_mips_gs464:
12384 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
d051516a
NC
12385 break;
12386
bd782c07
CX
12387 case bfd_mach_mips_gs464e:
12388 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12389 break;
12390
9108bc33
CX
12391 case bfd_mach_mips_gs264e:
12392 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12393 break;
12394
6f179bd0 12395 case bfd_mach_mips_octeon:
dd6a37e7 12396 case bfd_mach_mips_octeonp:
6f179bd0
AN
12397 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12398 break;
12399
2c629856
N
12400 case bfd_mach_mips_octeon3:
12401 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12402 break;
12403
52b6b6b9
JM
12404 case bfd_mach_mips_xlr:
12405 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12406 break;
12407
432233b3
AP
12408 case bfd_mach_mips_octeon2:
12409 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12410 break;
12411
b49e97c9
TS
12412 case bfd_mach_mipsisa32:
12413 val = E_MIPS_ARCH_32;
12414 break;
12415
12416 case bfd_mach_mipsisa64:
12417 val = E_MIPS_ARCH_64;
af7ee8bf
CD
12418 break;
12419
12420 case bfd_mach_mipsisa32r2:
ae52f483
AB
12421 case bfd_mach_mipsisa32r3:
12422 case bfd_mach_mipsisa32r5:
af7ee8bf
CD
12423 val = E_MIPS_ARCH_32R2;
12424 break;
5f74bc13 12425
38bf472a
MR
12426 case bfd_mach_mips_interaptiv_mr2:
12427 val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12428 break;
12429
5f74bc13 12430 case bfd_mach_mipsisa64r2:
ae52f483
AB
12431 case bfd_mach_mipsisa64r3:
12432 case bfd_mach_mipsisa64r5:
5f74bc13
CD
12433 val = E_MIPS_ARCH_64R2;
12434 break;
7361da2c
AB
12435
12436 case bfd_mach_mipsisa32r6:
12437 val = E_MIPS_ARCH_32R6;
12438 break;
12439
12440 case bfd_mach_mipsisa64r6:
12441 val = E_MIPS_ARCH_64R6;
12442 break;
b49e97c9 12443 }
b49e97c9
TS
12444 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12445 elf_elfheader (abfd)->e_flags |= val;
12446
64543e1a
RS
12447}
12448
12449
28dbcedc
AM
12450/* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12451 Don't do so for code sections. We want to keep ordering of HI16/LO16
12452 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12453 relocs to be sorted. */
12454
0a1b45a2 12455bool
28dbcedc
AM
12456_bfd_mips_elf_sort_relocs_p (asection *sec)
12457{
12458 return (sec->flags & SEC_CODE) == 0;
12459}
12460
12461
64543e1a
RS
12462/* The final processing done just before writing out a MIPS ELF object
12463 file. This gets the MIPS architecture right based on the machine
12464 number. This is used by both the 32-bit and the 64-bit ABI. */
12465
12466void
cc364be6 12467_bfd_mips_final_write_processing (bfd *abfd)
64543e1a
RS
12468{
12469 unsigned int i;
12470 Elf_Internal_Shdr **hdrpp;
12471 const char *name;
12472 asection *sec;
12473
12474 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12475 is nonzero. This is for compatibility with old objects, which used
12476 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12477 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12478 mips_set_isa_flags (abfd);
12479
b49e97c9
TS
12480 /* Set the sh_info field for .gptab sections and other appropriate
12481 info for each special section. */
12482 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12483 i < elf_numsections (abfd);
12484 i++, hdrpp++)
12485 {
12486 switch ((*hdrpp)->sh_type)
12487 {
12488 case SHT_MIPS_MSYM:
12489 case SHT_MIPS_LIBLIST:
12490 sec = bfd_get_section_by_name (abfd, ".dynstr");
12491 if (sec != NULL)
12492 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12493 break;
12494
12495 case SHT_MIPS_GPTAB:
12496 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
fd361982 12497 name = bfd_section_name ((*hdrpp)->bfd_section);
b49e97c9 12498 BFD_ASSERT (name != NULL
08dedd66 12499 && startswith (name, ".gptab."));
b49e97c9
TS
12500 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12501 BFD_ASSERT (sec != NULL);
12502 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12503 break;
12504
12505 case SHT_MIPS_CONTENT:
12506 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
fd361982 12507 name = bfd_section_name ((*hdrpp)->bfd_section);
b49e97c9 12508 BFD_ASSERT (name != NULL
08dedd66 12509 && startswith (name, ".MIPS.content"));
b49e97c9
TS
12510 sec = bfd_get_section_by_name (abfd,
12511 name + sizeof ".MIPS.content" - 1);
12512 BFD_ASSERT (sec != NULL);
12513 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12514 break;
12515
12516 case SHT_MIPS_SYMBOL_LIB:
12517 sec = bfd_get_section_by_name (abfd, ".dynsym");
12518 if (sec != NULL)
12519 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12520 sec = bfd_get_section_by_name (abfd, ".liblist");
12521 if (sec != NULL)
12522 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12523 break;
12524
12525 case SHT_MIPS_EVENTS:
12526 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
fd361982 12527 name = bfd_section_name ((*hdrpp)->bfd_section);
b49e97c9 12528 BFD_ASSERT (name != NULL);
08dedd66 12529 if (startswith (name, ".MIPS.events"))
b49e97c9
TS
12530 sec = bfd_get_section_by_name (abfd,
12531 name + sizeof ".MIPS.events" - 1);
12532 else
12533 {
08dedd66 12534 BFD_ASSERT (startswith (name, ".MIPS.post_rel"));
b49e97c9
TS
12535 sec = bfd_get_section_by_name (abfd,
12536 (name
12537 + sizeof ".MIPS.post_rel" - 1));
12538 }
12539 BFD_ASSERT (sec != NULL);
12540 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12541 break;
12542
f16a9783
MS
12543 case SHT_MIPS_XHASH:
12544 sec = bfd_get_section_by_name (abfd, ".dynsym");
12545 if (sec != NULL)
12546 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
b49e97c9
TS
12547 }
12548 }
12549}
06f44071 12550
0a1b45a2 12551bool
cc364be6 12552_bfd_mips_elf_final_write_processing (bfd *abfd)
06f44071 12553{
cc364be6
AM
12554 _bfd_mips_final_write_processing (abfd);
12555 return _bfd_elf_final_write_processing (abfd);
06f44071 12556}
b49e97c9 12557\f
8dc1a139 12558/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
12559 segments. */
12560
12561int
a6b96beb
AM
12562_bfd_mips_elf_additional_program_headers (bfd *abfd,
12563 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
12564{
12565 asection *s;
12566 int ret = 0;
12567
12568 /* See if we need a PT_MIPS_REGINFO segment. */
12569 s = bfd_get_section_by_name (abfd, ".reginfo");
12570 if (s && (s->flags & SEC_LOAD))
12571 ++ret;
12572
351cdf24
MF
12573 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12574 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12575 ++ret;
12576
b49e97c9
TS
12577 /* See if we need a PT_MIPS_OPTIONS segment. */
12578 if (IRIX_COMPAT (abfd) == ict_irix6
12579 && bfd_get_section_by_name (abfd,
12580 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12581 ++ret;
12582
12583 /* See if we need a PT_MIPS_RTPROC segment. */
12584 if (IRIX_COMPAT (abfd) == ict_irix5
12585 && bfd_get_section_by_name (abfd, ".dynamic")
12586 && bfd_get_section_by_name (abfd, ".mdebug"))
12587 ++ret;
12588
98c904a8
RS
12589 /* Allocate a PT_NULL header in dynamic objects. See
12590 _bfd_mips_elf_modify_segment_map for details. */
12591 if (!SGI_COMPAT (abfd)
12592 && bfd_get_section_by_name (abfd, ".dynamic"))
12593 ++ret;
12594
b49e97c9
TS
12595 return ret;
12596}
12597
8dc1a139 12598/* Modify the segment map for an IRIX5 executable. */
b49e97c9 12599
0a1b45a2 12600bool
9719ad41 12601_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 12602 struct bfd_link_info *info)
b49e97c9
TS
12603{
12604 asection *s;
12605 struct elf_segment_map *m, **pm;
986f0783 12606 size_t amt;
b49e97c9
TS
12607
12608 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12609 segment. */
12610 s = bfd_get_section_by_name (abfd, ".reginfo");
12611 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12612 {
12bd6957 12613 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
12614 if (m->p_type == PT_MIPS_REGINFO)
12615 break;
12616 if (m == NULL)
12617 {
12618 amt = sizeof *m;
9719ad41 12619 m = bfd_zalloc (abfd, amt);
b49e97c9 12620 if (m == NULL)
0a1b45a2 12621 return false;
b49e97c9
TS
12622
12623 m->p_type = PT_MIPS_REGINFO;
12624 m->count = 1;
12625 m->sections[0] = s;
12626
12627 /* We want to put it after the PHDR and INTERP segments. */
12bd6957 12628 pm = &elf_seg_map (abfd);
b49e97c9
TS
12629 while (*pm != NULL
12630 && ((*pm)->p_type == PT_PHDR
12631 || (*pm)->p_type == PT_INTERP))
12632 pm = &(*pm)->next;
12633
12634 m->next = *pm;
12635 *pm = m;
12636 }
12637 }
12638
351cdf24
MF
12639 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12640 segment. */
12641 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12642 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12643 {
12644 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12645 if (m->p_type == PT_MIPS_ABIFLAGS)
12646 break;
12647 if (m == NULL)
12648 {
12649 amt = sizeof *m;
12650 m = bfd_zalloc (abfd, amt);
12651 if (m == NULL)
0a1b45a2 12652 return false;
351cdf24
MF
12653
12654 m->p_type = PT_MIPS_ABIFLAGS;
12655 m->count = 1;
12656 m->sections[0] = s;
12657
12658 /* We want to put it after the PHDR and INTERP segments. */
12659 pm = &elf_seg_map (abfd);
12660 while (*pm != NULL
12661 && ((*pm)->p_type == PT_PHDR
12662 || (*pm)->p_type == PT_INTERP))
12663 pm = &(*pm)->next;
12664
12665 m->next = *pm;
12666 *pm = m;
12667 }
12668 }
12669
b49e97c9
TS
12670 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12671 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 12672 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 12673 table. */
c1fd6598
AO
12674 if (NEWABI_P (abfd)
12675 /* On non-IRIX6 new abi, we'll have already created a segment
12676 for this section, so don't create another. I'm not sure this
12677 is not also the case for IRIX 6, but I can't test it right
12678 now. */
12679 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
12680 {
12681 for (s = abfd->sections; s; s = s->next)
12682 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12683 break;
12684
12685 if (s)
12686 {
12687 struct elf_segment_map *options_segment;
12688
12bd6957 12689 pm = &elf_seg_map (abfd);
98a8deaf
RS
12690 while (*pm != NULL
12691 && ((*pm)->p_type == PT_PHDR
12692 || (*pm)->p_type == PT_INTERP))
12693 pm = &(*pm)->next;
b49e97c9 12694
8ded5a0f
AM
12695 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12696 {
12697 amt = sizeof (struct elf_segment_map);
12698 options_segment = bfd_zalloc (abfd, amt);
12699 options_segment->next = *pm;
12700 options_segment->p_type = PT_MIPS_OPTIONS;
12701 options_segment->p_flags = PF_R;
0a1b45a2 12702 options_segment->p_flags_valid = true;
8ded5a0f
AM
12703 options_segment->count = 1;
12704 options_segment->sections[0] = s;
12705 *pm = options_segment;
12706 }
b49e97c9
TS
12707 }
12708 }
12709 else
12710 {
12711 if (IRIX_COMPAT (abfd) == ict_irix5)
12712 {
12713 /* If there are .dynamic and .mdebug sections, we make a room
12714 for the RTPROC header. FIXME: Rewrite without section names. */
12715 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12716 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12717 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12718 {
12bd6957 12719 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
12720 if (m->p_type == PT_MIPS_RTPROC)
12721 break;
12722 if (m == NULL)
12723 {
12724 amt = sizeof *m;
9719ad41 12725 m = bfd_zalloc (abfd, amt);
b49e97c9 12726 if (m == NULL)
0a1b45a2 12727 return false;
b49e97c9
TS
12728
12729 m->p_type = PT_MIPS_RTPROC;
12730
12731 s = bfd_get_section_by_name (abfd, ".rtproc");
12732 if (s == NULL)
12733 {
12734 m->count = 0;
12735 m->p_flags = 0;
12736 m->p_flags_valid = 1;
12737 }
12738 else
12739 {
12740 m->count = 1;
12741 m->sections[0] = s;
12742 }
12743
12744 /* We want to put it after the DYNAMIC segment. */
12bd6957 12745 pm = &elf_seg_map (abfd);
b49e97c9
TS
12746 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12747 pm = &(*pm)->next;
12748 if (*pm != NULL)
12749 pm = &(*pm)->next;
12750
12751 m->next = *pm;
12752 *pm = m;
12753 }
12754 }
12755 }
8dc1a139 12756 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
12757 .dynstr, .dynsym, and .hash sections, and everything in
12758 between. */
12bd6957 12759 for (pm = &elf_seg_map (abfd); *pm != NULL;
b49e97c9
TS
12760 pm = &(*pm)->next)
12761 if ((*pm)->p_type == PT_DYNAMIC)
12762 break;
12763 m = *pm;
f6f62d6f
RS
12764 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12765 glibc's dynamic linker has traditionally derived the number of
12766 tags from the p_filesz field, and sometimes allocates stack
12767 arrays of that size. An overly-big PT_DYNAMIC segment can
12768 be actively harmful in such cases. Making PT_DYNAMIC contain
12769 other sections can also make life hard for the prelinker,
12770 which might move one of the other sections to a different
12771 PT_LOAD segment. */
12772 if (SGI_COMPAT (abfd)
12773 && m != NULL
12774 && m->count == 1
12775 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
12776 {
12777 static const char *sec_names[] =
12778 {
12779 ".dynamic", ".dynstr", ".dynsym", ".hash"
12780 };
12781 bfd_vma low, high;
12782 unsigned int i, c;
12783 struct elf_segment_map *n;
12784
792b4a53 12785 low = ~(bfd_vma) 0;
b49e97c9
TS
12786 high = 0;
12787 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12788 {
12789 s = bfd_get_section_by_name (abfd, sec_names[i]);
12790 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12791 {
12792 bfd_size_type sz;
12793
12794 if (low > s->vma)
12795 low = s->vma;
eea6121a 12796 sz = s->size;
b49e97c9
TS
12797 if (high < s->vma + sz)
12798 high = s->vma + sz;
12799 }
12800 }
12801
12802 c = 0;
12803 for (s = abfd->sections; s != NULL; s = s->next)
12804 if ((s->flags & SEC_LOAD) != 0
12805 && s->vma >= low
eea6121a 12806 && s->vma + s->size <= high)
b49e97c9
TS
12807 ++c;
12808
986f0783 12809 amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
9719ad41 12810 n = bfd_zalloc (abfd, amt);
b49e97c9 12811 if (n == NULL)
0a1b45a2 12812 return false;
b49e97c9
TS
12813 *n = *m;
12814 n->count = c;
12815
12816 i = 0;
12817 for (s = abfd->sections; s != NULL; s = s->next)
12818 {
12819 if ((s->flags & SEC_LOAD) != 0
12820 && s->vma >= low
eea6121a 12821 && s->vma + s->size <= high)
b49e97c9
TS
12822 {
12823 n->sections[i] = s;
12824 ++i;
12825 }
12826 }
12827
12828 *pm = n;
12829 }
12830 }
12831
98c904a8
RS
12832 /* Allocate a spare program header in dynamic objects so that tools
12833 like the prelinker can add an extra PT_LOAD entry.
12834
12835 If the prelinker needs to make room for a new PT_LOAD entry, its
12836 standard procedure is to move the first (read-only) sections into
12837 the new (writable) segment. However, the MIPS ABI requires
12838 .dynamic to be in a read-only segment, and the section will often
12839 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12840
12841 Although the prelinker could in principle move .dynamic to a
12842 writable segment, it seems better to allocate a spare program
12843 header instead, and avoid the need to move any sections.
12844 There is a long tradition of allocating spare dynamic tags,
12845 so allocating a spare program header seems like a natural
7c8b76cc
JM
12846 extension.
12847
12848 If INFO is NULL, we may be copying an already prelinked binary
12849 with objcopy or strip, so do not add this header. */
12850 if (info != NULL
12851 && !SGI_COMPAT (abfd)
98c904a8
RS
12852 && bfd_get_section_by_name (abfd, ".dynamic"))
12853 {
12bd6957 12854 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
98c904a8
RS
12855 if ((*pm)->p_type == PT_NULL)
12856 break;
12857 if (*pm == NULL)
12858 {
12859 m = bfd_zalloc (abfd, sizeof (*m));
12860 if (m == NULL)
0a1b45a2 12861 return false;
98c904a8
RS
12862
12863 m->p_type = PT_NULL;
12864 *pm = m;
12865 }
12866 }
12867
0a1b45a2 12868 return true;
b49e97c9
TS
12869}
12870\f
12871/* Return the section that should be marked against GC for a given
12872 relocation. */
12873
12874asection *
9719ad41 12875_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 12876 struct bfd_link_info *info,
9719ad41
RS
12877 Elf_Internal_Rela *rel,
12878 struct elf_link_hash_entry *h,
12879 Elf_Internal_Sym *sym)
b49e97c9
TS
12880{
12881 /* ??? Do mips16 stub sections need to be handled special? */
12882
12883 if (h != NULL)
07adf181
AM
12884 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12885 {
12886 case R_MIPS_GNU_VTINHERIT:
12887 case R_MIPS_GNU_VTENTRY:
12888 return NULL;
12889 }
b49e97c9 12890
07adf181 12891 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
12892}
12893
351cdf24
MF
12894/* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12895
0a1b45a2 12896bool
351cdf24
MF
12897_bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12898 elf_gc_mark_hook_fn gc_mark_hook)
12899{
12900 bfd *sub;
12901
12902 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12903
12904 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12905 {
12906 asection *o;
12907
12908 if (! is_mips_elf (sub))
12909 continue;
12910
12911 for (o = sub->sections; o != NULL; o = o->next)
12912 if (!o->gc_mark
fd361982 12913 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
351cdf24
MF
12914 {
12915 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
0a1b45a2 12916 return false;
351cdf24
MF
12917 }
12918 }
12919
0a1b45a2 12920 return true;
351cdf24 12921}
b49e97c9
TS
12922\f
12923/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12924 hiding the old indirect symbol. Process additional relocation
12925 information. Also called for weakdefs, in which case we just let
12926 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12927
12928void
fcfa13d2 12929_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
12930 struct elf_link_hash_entry *dir,
12931 struct elf_link_hash_entry *ind)
b49e97c9
TS
12932{
12933 struct mips_elf_link_hash_entry *dirmips, *indmips;
12934
fcfa13d2 12935 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 12936
861fb55a
DJ
12937 dirmips = (struct mips_elf_link_hash_entry *) dir;
12938 indmips = (struct mips_elf_link_hash_entry *) ind;
12939 /* Any absolute non-dynamic relocations against an indirect or weak
12940 definition will be against the target symbol. */
12941 if (indmips->has_static_relocs)
0a1b45a2 12942 dirmips->has_static_relocs = true;
861fb55a 12943
b49e97c9
TS
12944 if (ind->root.type != bfd_link_hash_indirect)
12945 return;
12946
b49e97c9
TS
12947 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12948 if (indmips->readonly_reloc)
0a1b45a2 12949 dirmips->readonly_reloc = true;
b49e97c9 12950 if (indmips->no_fn_stub)
0a1b45a2 12951 dirmips->no_fn_stub = true;
61b0a4af
RS
12952 if (indmips->fn_stub)
12953 {
12954 dirmips->fn_stub = indmips->fn_stub;
12955 indmips->fn_stub = NULL;
12956 }
12957 if (indmips->need_fn_stub)
12958 {
0a1b45a2
AM
12959 dirmips->need_fn_stub = true;
12960 indmips->need_fn_stub = false;
61b0a4af
RS
12961 }
12962 if (indmips->call_stub)
12963 {
12964 dirmips->call_stub = indmips->call_stub;
12965 indmips->call_stub = NULL;
12966 }
12967 if (indmips->call_fp_stub)
12968 {
12969 dirmips->call_fp_stub = indmips->call_fp_stub;
12970 indmips->call_fp_stub = NULL;
12971 }
634835ae
RS
12972 if (indmips->global_got_area < dirmips->global_got_area)
12973 dirmips->global_got_area = indmips->global_got_area;
12974 if (indmips->global_got_area < GGA_NONE)
12975 indmips->global_got_area = GGA_NONE;
861fb55a 12976 if (indmips->has_nonpic_branches)
0a1b45a2 12977 dirmips->has_nonpic_branches = true;
b49e97c9 12978}
47275900
MR
12979
12980/* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
12981 to hide it. It has to remain global (it will also be protected) so as to
12982 be assigned a global GOT entry, which will then remain unchanged at load
12983 time. */
12984
12985void
12986_bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
12987 struct elf_link_hash_entry *entry,
0a1b45a2 12988 bool force_local)
47275900
MR
12989{
12990 struct mips_elf_link_hash_table *htab;
12991
12992 htab = mips_elf_hash_table (info);
12993 BFD_ASSERT (htab != NULL);
12994 if (htab->use_absolute_zero
12995 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
12996 return;
12997
12998 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
12999}
b49e97c9 13000\f
d01414a5
TS
13001#define PDR_SIZE 32
13002
0a1b45a2 13003bool
9719ad41
RS
13004_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
13005 struct bfd_link_info *info)
d01414a5
TS
13006{
13007 asection *o;
0a1b45a2 13008 bool ret = false;
d01414a5
TS
13009 unsigned char *tdata;
13010 size_t i, skip;
13011
13012 o = bfd_get_section_by_name (abfd, ".pdr");
13013 if (! o)
0a1b45a2 13014 return false;
eea6121a 13015 if (o->size == 0)
0a1b45a2 13016 return false;
eea6121a 13017 if (o->size % PDR_SIZE != 0)
0a1b45a2 13018 return false;
d01414a5
TS
13019 if (o->output_section != NULL
13020 && bfd_is_abs_section (o->output_section))
0a1b45a2 13021 return false;
d01414a5 13022
eea6121a 13023 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 13024 if (! tdata)
0a1b45a2 13025 return false;
d01414a5 13026
9719ad41 13027 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 13028 info->keep_memory);
d01414a5
TS
13029 if (!cookie->rels)
13030 {
13031 free (tdata);
0a1b45a2 13032 return false;
d01414a5
TS
13033 }
13034
13035 cookie->rel = cookie->rels;
13036 cookie->relend = cookie->rels + o->reloc_count;
13037
eea6121a 13038 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 13039 {
c152c796 13040 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
13041 {
13042 tdata[i] = 1;
13043 skip ++;
13044 }
13045 }
13046
13047 if (skip != 0)
13048 {
f0abc2a1 13049 mips_elf_section_data (o)->u.tdata = tdata;
e034b2cc
MR
13050 if (o->rawsize == 0)
13051 o->rawsize = o->size;
eea6121a 13052 o->size -= skip * PDR_SIZE;
0a1b45a2 13053 ret = true;
d01414a5
TS
13054 }
13055 else
13056 free (tdata);
13057
13058 if (! info->keep_memory)
13059 free (cookie->rels);
13060
13061 return ret;
13062}
13063
0a1b45a2 13064bool
9719ad41 13065_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
13066{
13067 if (strcmp (sec->name, ".pdr") == 0)
0a1b45a2
AM
13068 return true;
13069 return false;
53bfd6b4 13070}
d01414a5 13071
0a1b45a2 13072bool
c7b8f16e
JB
13073_bfd_mips_elf_write_section (bfd *output_bfd,
13074 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
07d6d2b8 13075 asection *sec, bfd_byte *contents)
d01414a5
TS
13076{
13077 bfd_byte *to, *from, *end;
13078 int i;
13079
13080 if (strcmp (sec->name, ".pdr") != 0)
0a1b45a2 13081 return false;
d01414a5 13082
f0abc2a1 13083 if (mips_elf_section_data (sec)->u.tdata == NULL)
0a1b45a2 13084 return false;
d01414a5
TS
13085
13086 to = contents;
eea6121a 13087 end = contents + sec->size;
d01414a5
TS
13088 for (from = contents, i = 0;
13089 from < end;
13090 from += PDR_SIZE, i++)
13091 {
f0abc2a1 13092 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
13093 continue;
13094 if (to != from)
13095 memcpy (to, from, PDR_SIZE);
13096 to += PDR_SIZE;
13097 }
13098 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 13099 sec->output_offset, sec->size);
0a1b45a2 13100 return true;
d01414a5 13101}
53bfd6b4 13102\f
df58fc94
RS
13103/* microMIPS code retains local labels for linker relaxation. Omit them
13104 from output by default for clarity. */
13105
0a1b45a2 13106bool
df58fc94
RS
13107_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13108{
13109 return _bfd_elf_is_local_label_name (abfd, sym->name);
13110}
13111
b49e97c9
TS
13112/* MIPS ELF uses a special find_nearest_line routine in order the
13113 handle the ECOFF debugging information. */
13114
13115struct mips_elf_find_line
13116{
13117 struct ecoff_debug_info d;
13118 struct ecoff_find_line i;
13119};
13120
0a1b45a2 13121bool
fb167eb2
AM
13122_bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13123 asection *section, bfd_vma offset,
9719ad41
RS
13124 const char **filename_ptr,
13125 const char **functionname_ptr,
fb167eb2
AM
13126 unsigned int *line_ptr,
13127 unsigned int *discriminator_ptr)
b49e97c9
TS
13128{
13129 asection *msec;
13130
fb167eb2 13131 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
b49e97c9 13132 filename_ptr, functionname_ptr,
fb167eb2
AM
13133 line_ptr, discriminator_ptr,
13134 dwarf_debug_sections,
7f3bf384
AM
13135 &elf_tdata (abfd)->dwarf2_find_line_info)
13136 == 1)
0a1b45a2 13137 return true;
46d09186 13138
e7679060
AM
13139 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13140 filename_ptr, functionname_ptr,
13141 line_ptr))
13142 {
13143 if (!*functionname_ptr)
13144 _bfd_elf_find_function (abfd, symbols, section, offset,
13145 *filename_ptr ? NULL : filename_ptr,
13146 functionname_ptr);
0a1b45a2 13147 return true;
46d09186 13148 }
b49e97c9
TS
13149
13150 msec = bfd_get_section_by_name (abfd, ".mdebug");
13151 if (msec != NULL)
13152 {
13153 flagword origflags;
13154 struct mips_elf_find_line *fi;
13155 const struct ecoff_debug_swap * const swap =
13156 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13157
13158 /* If we are called during a link, mips_elf_final_link may have
13159 cleared the SEC_HAS_CONTENTS field. We force it back on here
13160 if appropriate (which it normally will be). */
13161 origflags = msec->flags;
13162 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13163 msec->flags |= SEC_HAS_CONTENTS;
13164
698600e4 13165 fi = mips_elf_tdata (abfd)->find_line_info;
b49e97c9
TS
13166 if (fi == NULL)
13167 {
13168 bfd_size_type external_fdr_size;
13169 char *fraw_src;
13170 char *fraw_end;
13171 struct fdr *fdr_ptr;
13172 bfd_size_type amt = sizeof (struct mips_elf_find_line);
13173
9719ad41 13174 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
13175 if (fi == NULL)
13176 {
13177 msec->flags = origflags;
0a1b45a2 13178 return false;
b49e97c9
TS
13179 }
13180
13181 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13182 {
13183 msec->flags = origflags;
0a1b45a2 13184 return false;
b49e97c9
TS
13185 }
13186
13187 /* Swap in the FDR information. */
13188 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 13189 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
13190 if (fi->d.fdr == NULL)
13191 {
13192 msec->flags = origflags;
0a1b45a2 13193 return false;
b49e97c9
TS
13194 }
13195 external_fdr_size = swap->external_fdr_size;
13196 fdr_ptr = fi->d.fdr;
13197 fraw_src = (char *) fi->d.external_fdr;
13198 fraw_end = (fraw_src
13199 + fi->d.symbolic_header.ifdMax * external_fdr_size);
13200 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 13201 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9 13202
698600e4 13203 mips_elf_tdata (abfd)->find_line_info = fi;
b49e97c9
TS
13204
13205 /* Note that we don't bother to ever free this information.
07d6d2b8
AM
13206 find_nearest_line is either called all the time, as in
13207 objdump -l, so the information should be saved, or it is
13208 rarely called, as in ld error messages, so the memory
13209 wasted is unimportant. Still, it would probably be a
13210 good idea for free_cached_info to throw it away. */
b49e97c9
TS
13211 }
13212
13213 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13214 &fi->i, filename_ptr, functionname_ptr,
13215 line_ptr))
13216 {
13217 msec->flags = origflags;
0a1b45a2 13218 return true;
b49e97c9
TS
13219 }
13220
13221 msec->flags = origflags;
13222 }
13223
13224 /* Fall back on the generic ELF find_nearest_line routine. */
13225
fb167eb2 13226 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
b49e97c9 13227 filename_ptr, functionname_ptr,
fb167eb2 13228 line_ptr, discriminator_ptr);
b49e97c9 13229}
4ab527b0 13230
0a1b45a2 13231bool
4ab527b0
FF
13232_bfd_mips_elf_find_inliner_info (bfd *abfd,
13233 const char **filename_ptr,
13234 const char **functionname_ptr,
13235 unsigned int *line_ptr)
13236{
0a1b45a2 13237 bool found;
4ab527b0
FF
13238 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13239 functionname_ptr, line_ptr,
13240 & elf_tdata (abfd)->dwarf2_find_line_info);
13241 return found;
13242}
13243
b49e97c9
TS
13244\f
13245/* When are writing out the .options or .MIPS.options section,
13246 remember the bytes we are writing out, so that we can install the
13247 GP value in the section_processing routine. */
13248
0a1b45a2 13249bool
9719ad41
RS
13250_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13251 const void *location,
13252 file_ptr offset, bfd_size_type count)
b49e97c9 13253{
cc2e31b9 13254 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
13255 {
13256 bfd_byte *c;
13257
13258 if (elf_section_data (section) == NULL)
13259 {
986f0783 13260 size_t amt = sizeof (struct bfd_elf_section_data);
9719ad41 13261 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 13262 if (elf_section_data (section) == NULL)
0a1b45a2 13263 return false;
b49e97c9 13264 }
f0abc2a1 13265 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
13266 if (c == NULL)
13267 {
eea6121a 13268 c = bfd_zalloc (abfd, section->size);
b49e97c9 13269 if (c == NULL)
0a1b45a2 13270 return false;
f0abc2a1 13271 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
13272 }
13273
9719ad41 13274 memcpy (c + offset, location, count);
b49e97c9
TS
13275 }
13276
13277 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13278 count);
13279}
13280
13281/* This is almost identical to bfd_generic_get_... except that some
13282 MIPS relocations need to be handled specially. Sigh. */
13283
13284bfd_byte *
9719ad41
RS
13285_bfd_elf_mips_get_relocated_section_contents
13286 (bfd *abfd,
13287 struct bfd_link_info *link_info,
13288 struct bfd_link_order *link_order,
13289 bfd_byte *data,
0a1b45a2 13290 bool relocatable,
9719ad41 13291 asymbol **symbols)
b49e97c9 13292{
b49e97c9
TS
13293 bfd *input_bfd = link_order->u.indirect.section->owner;
13294 asection *input_section = link_order->u.indirect.section;
d7a7af8f
AM
13295 long reloc_size;
13296 arelent **reloc_vector;
b49e97c9
TS
13297 long reloc_count;
13298
d7a7af8f 13299 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
b49e97c9 13300 if (reloc_size < 0)
d7a7af8f 13301 return NULL;
b49e97c9 13302
d7a7af8f 13303 /* Read in the section. */
56ba7527 13304 bfd_byte *orig_data = data;
d7a7af8f
AM
13305 if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
13306 return NULL;
b49e97c9 13307
d7a7af8f
AM
13308 if (data == NULL)
13309 return NULL;
13310
13311 if (reloc_size == 0)
13312 return data;
13313
13314 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13315 if (reloc_vector == NULL)
7ebf6ed0 13316 {
c799eddb 13317 struct mips_elf_obj_tdata *tdata;
7ebf6ed0
AM
13318 struct mips_hi16 **hip, *hi;
13319 error_return:
13320 /* If we are going to return an error, remove entries on
13321 mips_hi16_list that point into this section's data. Data
13322 will typically be freed on return from this function. */
c799eddb
AM
13323 tdata = mips_elf_tdata (abfd);
13324 hip = &tdata->mips_hi16_list;
7ebf6ed0
AM
13325 while ((hi = *hip) != NULL)
13326 {
13327 if (hi->input_section == input_section)
13328 {
13329 *hip = hi->next;
13330 free (hi);
13331 }
13332 else
13333 hip = &hi->next;
13334 }
56ba7527
AM
13335 if (orig_data == NULL)
13336 free (data);
7ebf6ed0
AM
13337 data = NULL;
13338 goto out;
13339 }
b49e97c9 13340
b49e97c9
TS
13341 reloc_count = bfd_canonicalize_reloc (input_bfd,
13342 input_section,
13343 reloc_vector,
13344 symbols);
13345 if (reloc_count < 0)
13346 goto error_return;
13347
13348 if (reloc_count > 0)
13349 {
13350 arelent **parent;
13351 /* for mips */
13352 int gp_found;
13353 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13354
13355 {
13356 struct bfd_hash_entry *h;
13357 struct bfd_link_hash_entry *lh;
13358 /* Skip all this stuff if we aren't mixing formats. */
13359 if (abfd && input_bfd
13360 && abfd->xvec == input_bfd->xvec)
13361 lh = 0;
13362 else
13363 {
0a1b45a2 13364 h = bfd_hash_lookup (&link_info->hash->table, "_gp", false, false);
b49e97c9
TS
13365 lh = (struct bfd_link_hash_entry *) h;
13366 }
13367 lookup:
13368 if (lh)
13369 {
13370 switch (lh->type)
13371 {
13372 case bfd_link_hash_undefined:
13373 case bfd_link_hash_undefweak:
13374 case bfd_link_hash_common:
13375 gp_found = 0;
13376 break;
13377 case bfd_link_hash_defined:
13378 case bfd_link_hash_defweak:
13379 gp_found = 1;
13380 gp = lh->u.def.value;
13381 break;
13382 case bfd_link_hash_indirect:
13383 case bfd_link_hash_warning:
13384 lh = lh->u.i.link;
13385 /* @@FIXME ignoring warning for now */
13386 goto lookup;
13387 case bfd_link_hash_new:
13388 default:
13389 abort ();
13390 }
13391 }
13392 else
13393 gp_found = 0;
13394 }
13395 /* end mips */
d7a7af8f 13396
9719ad41 13397 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 13398 {
9719ad41 13399 char *error_message = NULL;
d7a7af8f 13400 asymbol *symbol;
b49e97c9
TS
13401 bfd_reloc_status_type r;
13402
d7a7af8f
AM
13403 symbol = *(*parent)->sym_ptr_ptr;
13404 /* PR ld/19628: A specially crafted input file
13405 can result in a NULL symbol pointer here. */
13406 if (symbol == NULL)
13407 {
13408 link_info->callbacks->einfo
13409 /* xgettext:c-format */
13410 (_("%X%P: %pB(%pA): error: relocation for offset %V has no value\n"),
13411 abfd, input_section, (* parent)->address);
13412 goto error_return;
13413 }
13414
13415 /* Zap reloc field when the symbol is from a discarded
13416 section, ignoring any addend. Do the same when called
13417 from bfd_simple_get_relocated_section_contents for
13418 undefined symbols in debug sections. This is to keep
13419 debug info reasonably sane, in particular so that
13420 DW_FORM_ref_addr to another file's .debug_info isn't
13421 confused with an offset into the current file's
13422 .debug_info. */
13423 if ((symbol->section != NULL && discarded_section (symbol->section))
13424 || (symbol->section == bfd_und_section_ptr
13425 && (input_section->flags & SEC_DEBUGGING) != 0
13426 && link_info->input_bfds == link_info->output_bfd))
13427 {
13428 bfd_vma off;
13429 static reloc_howto_type none_howto
c94cb026 13430 = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
0a1b45a2 13431 "unused", false, 0, 0, false);
d7a7af8f
AM
13432
13433 off = ((*parent)->address
13434 * bfd_octets_per_byte (input_bfd, input_section));
13435 _bfd_clear_contents ((*parent)->howto, input_bfd,
13436 input_section, data, off);
13437 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13438 (*parent)->addend = 0;
13439 (*parent)->howto = &none_howto;
13440 r = bfd_reloc_ok;
13441 }
13442
b49e97c9
TS
13443 /* Specific to MIPS: Deal with relocation types that require
13444 knowing the gp of the output bfd. */
b49e97c9 13445
8236346f
EC
13446 /* If we've managed to find the gp and have a special
13447 function for the relocation then go ahead, else default
13448 to the generic handling. */
d7a7af8f
AM
13449 else if (gp_found
13450 && ((*parent)->howto->special_function
13451 == _bfd_mips_elf32_gprel16_reloc))
13452 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, symbol, *parent,
8236346f
EC
13453 input_section, relocatable,
13454 data, gp);
13455 else
d7a7af8f
AM
13456 r = bfd_perform_relocation (input_bfd,
13457 *parent,
13458 data,
8236346f
EC
13459 input_section,
13460 relocatable ? abfd : NULL,
13461 &error_message);
b49e97c9 13462
1049f94e 13463 if (relocatable)
b49e97c9
TS
13464 {
13465 asection *os = input_section->output_section;
13466
d7a7af8f 13467 /* A partial link, so keep the relocs. */
b49e97c9
TS
13468 os->orelocation[os->reloc_count] = *parent;
13469 os->reloc_count++;
13470 }
13471
13472 if (r != bfd_reloc_ok)
13473 {
13474 switch (r)
13475 {
13476 case bfd_reloc_undefined:
1a72702b
AM
13477 (*link_info->callbacks->undefined_symbol)
13478 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
0a1b45a2 13479 input_bfd, input_section, (*parent)->address, true);
b49e97c9
TS
13480 break;
13481 case bfd_reloc_dangerous:
9719ad41 13482 BFD_ASSERT (error_message != NULL);
1a72702b
AM
13483 (*link_info->callbacks->reloc_dangerous)
13484 (link_info, error_message,
13485 input_bfd, input_section, (*parent)->address);
b49e97c9
TS
13486 break;
13487 case bfd_reloc_overflow:
1a72702b
AM
13488 (*link_info->callbacks->reloc_overflow)
13489 (link_info, NULL,
13490 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13491 (*parent)->howto->name, (*parent)->addend,
13492 input_bfd, input_section, (*parent)->address);
b49e97c9
TS
13493 break;
13494 case bfd_reloc_outofrange:
d7a7af8f
AM
13495 /* PR ld/13730:
13496 This error can result when processing some partially
13497 complete binaries. Do not abort, but issue an error
13498 message instead. */
13499 link_info->callbacks->einfo
13500 /* xgettext:c-format */
13501 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13502 abfd, input_section, * parent);
13503 goto error_return;
13504
13505 case bfd_reloc_notsupported:
13506 /* PR ld/17512
13507 This error can result when processing a corrupt binary.
13508 Do not abort. Issue an error message instead. */
13509 link_info->callbacks->einfo
13510 /* xgettext:c-format */
13511 (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
13512 abfd, input_section, * parent);
13513 goto error_return;
13514
b49e97c9 13515 default:
d7a7af8f
AM
13516 /* PR 17512; file: 90c2a92e.
13517 Report unexpected results, without aborting. */
13518 link_info->callbacks->einfo
13519 /* xgettext:c-format */
13520 (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
13521 abfd, input_section, * parent, r);
b49e97c9
TS
13522 break;
13523 }
13524
13525 }
13526 }
13527 }
d7a7af8f 13528
7ebf6ed0 13529 out:
c9594989 13530 free (reloc_vector);
b49e97c9 13531 return data;
b49e97c9
TS
13532}
13533\f
0a1b45a2 13534static bool
df58fc94
RS
13535mips_elf_relax_delete_bytes (bfd *abfd,
13536 asection *sec, bfd_vma addr, int count)
13537{
13538 Elf_Internal_Shdr *symtab_hdr;
13539 unsigned int sec_shndx;
13540 bfd_byte *contents;
13541 Elf_Internal_Rela *irel, *irelend;
13542 Elf_Internal_Sym *isym;
13543 Elf_Internal_Sym *isymend;
13544 struct elf_link_hash_entry **sym_hashes;
13545 struct elf_link_hash_entry **end_hashes;
13546 struct elf_link_hash_entry **start_hashes;
13547 unsigned int symcount;
13548
13549 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13550 contents = elf_section_data (sec)->this_hdr.contents;
13551
13552 irel = elf_section_data (sec)->relocs;
13553 irelend = irel + sec->reloc_count;
13554
13555 /* Actually delete the bytes. */
13556 memmove (contents + addr, contents + addr + count,
13557 (size_t) (sec->size - addr - count));
13558 sec->size -= count;
13559
13560 /* Adjust all the relocs. */
13561 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13562 {
13563 /* Get the new reloc address. */
13564 if (irel->r_offset > addr)
13565 irel->r_offset -= count;
13566 }
13567
13568 BFD_ASSERT (addr % 2 == 0);
13569 BFD_ASSERT (count % 2 == 0);
13570
13571 /* Adjust the local symbols defined in this section. */
13572 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13573 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13574 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2309ddf2 13575 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
df58fc94
RS
13576 isym->st_value -= count;
13577
13578 /* Now adjust the global symbols defined in this section. */
13579 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13580 - symtab_hdr->sh_info);
13581 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13582 end_hashes = sym_hashes + symcount;
13583
13584 for (; sym_hashes < end_hashes; sym_hashes++)
13585 {
13586 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13587
13588 if ((sym_hash->root.type == bfd_link_hash_defined
13589 || sym_hash->root.type == bfd_link_hash_defweak)
13590 && sym_hash->root.u.def.section == sec)
13591 {
2309ddf2 13592 bfd_vma value = sym_hash->root.u.def.value;
df58fc94 13593
df58fc94
RS
13594 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13595 value &= MINUS_TWO;
13596 if (value > addr)
13597 sym_hash->root.u.def.value -= count;
13598 }
13599 }
13600
0a1b45a2 13601 return true;
df58fc94
RS
13602}
13603
13604
13605/* Opcodes needed for microMIPS relaxation as found in
13606 opcodes/micromips-opc.c. */
13607
13608struct opcode_descriptor {
13609 unsigned long match;
13610 unsigned long mask;
13611};
13612
13613/* The $ra register aka $31. */
13614
13615#define RA 31
13616
13617/* 32-bit instruction format register fields. */
13618
13619#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13620#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13621
13622/* Check if a 5-bit register index can be abbreviated to 3 bits. */
13623
13624#define OP16_VALID_REG(r) \
13625 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13626
13627
13628/* 32-bit and 16-bit branches. */
13629
13630static const struct opcode_descriptor b_insns_32[] = {
13631 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13632 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13633 { 0, 0 } /* End marker for find_match(). */
13634};
13635
13636static const struct opcode_descriptor bc_insn_32 =
13637 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13638
13639static const struct opcode_descriptor bz_insn_32 =
13640 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13641
13642static const struct opcode_descriptor bzal_insn_32 =
13643 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13644
13645static const struct opcode_descriptor beq_insn_32 =
13646 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13647
13648static const struct opcode_descriptor b_insn_16 =
13649 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13650
13651static const struct opcode_descriptor bz_insn_16 =
c088dedf 13652 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
df58fc94
RS
13653
13654
13655/* 32-bit and 16-bit branch EQ and NE zero. */
13656
13657/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13658 eq and second the ne. This convention is used when replacing a
13659 32-bit BEQ/BNE with the 16-bit version. */
13660
13661#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13662
13663static const struct opcode_descriptor bz_rs_insns_32[] = {
13664 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13665 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13666 { 0, 0 } /* End marker for find_match(). */
13667};
13668
13669static const struct opcode_descriptor bz_rt_insns_32[] = {
13670 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13671 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13672 { 0, 0 } /* End marker for find_match(). */
13673};
13674
13675static const struct opcode_descriptor bzc_insns_32[] = {
13676 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13677 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13678 { 0, 0 } /* End marker for find_match(). */
13679};
13680
13681static const struct opcode_descriptor bz_insns_16[] = {
13682 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13683 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13684 { 0, 0 } /* End marker for find_match(). */
13685};
13686
13687/* Switch between a 5-bit register index and its 3-bit shorthand. */
13688
e67f83e5 13689#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
eb6b0cf4 13690#define BZ16_REG_FIELD(r) (((r) & 7) << 7)
df58fc94
RS
13691
13692
13693/* 32-bit instructions with a delay slot. */
13694
13695static const struct opcode_descriptor jal_insn_32_bd16 =
13696 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13697
13698static const struct opcode_descriptor jal_insn_32_bd32 =
13699 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13700
13701static const struct opcode_descriptor jal_x_insn_32_bd32 =
13702 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13703
13704static const struct opcode_descriptor j_insn_32 =
13705 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13706
13707static const struct opcode_descriptor jalr_insn_32 =
13708 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13709
13710/* This table can be compacted, because no opcode replacement is made. */
13711
13712static const struct opcode_descriptor ds_insns_32_bd16[] = {
13713 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13714
13715 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13716 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13717
13718 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13719 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13720 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13721 { 0, 0 } /* End marker for find_match(). */
13722};
13723
13724/* This table can be compacted, because no opcode replacement is made. */
13725
13726static const struct opcode_descriptor ds_insns_32_bd32[] = {
13727 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13728
13729 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13730 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13731 { 0, 0 } /* End marker for find_match(). */
13732};
13733
13734
13735/* 16-bit instructions with a delay slot. */
13736
13737static const struct opcode_descriptor jalr_insn_16_bd16 =
13738 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13739
13740static const struct opcode_descriptor jalr_insn_16_bd32 =
13741 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13742
13743static const struct opcode_descriptor jr_insn_16 =
13744 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13745
13746#define JR16_REG(opcode) ((opcode) & 0x1f)
13747
13748/* This table can be compacted, because no opcode replacement is made. */
13749
13750static const struct opcode_descriptor ds_insns_16_bd16[] = {
13751 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13752
13753 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13754 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13755 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13756 { 0, 0 } /* End marker for find_match(). */
13757};
13758
13759
13760/* LUI instruction. */
13761
13762static const struct opcode_descriptor lui_insn =
13763 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13764
13765
13766/* ADDIU instruction. */
13767
13768static const struct opcode_descriptor addiu_insn =
13769 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13770
13771static const struct opcode_descriptor addiupc_insn =
13772 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13773
13774#define ADDIUPC_REG_FIELD(r) \
13775 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13776
13777
13778/* Relaxable instructions in a JAL delay slot: MOVE. */
13779
13780/* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13781 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13782#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13783#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13784
13785#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13786#define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13787
13788static const struct opcode_descriptor move_insns_32[] = {
df58fc94 13789 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
40fc1451 13790 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
df58fc94
RS
13791 { 0, 0 } /* End marker for find_match(). */
13792};
13793
13794static const struct opcode_descriptor move_insn_16 =
13795 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13796
13797
13798/* NOP instructions. */
13799
13800static const struct opcode_descriptor nop_insn_32 =
13801 { /* "nop", "", */ 0x00000000, 0xffffffff };
13802
13803static const struct opcode_descriptor nop_insn_16 =
13804 { /* "nop", "", */ 0x0c00, 0xffff };
13805
13806
13807/* Instruction match support. */
13808
13809#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13810
13811static int
13812find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13813{
13814 unsigned long indx;
13815
13816 for (indx = 0; insn[indx].mask != 0; indx++)
13817 if (MATCH (opcode, insn[indx]))
13818 return indx;
13819
13820 return -1;
13821}
13822
13823
13824/* Branch and delay slot decoding support. */
13825
13826/* If PTR points to what *might* be a 16-bit branch or jump, then
13827 return the minimum length of its delay slot, otherwise return 0.
13828 Non-zero results are not definitive as we might be checking against
13829 the second half of another instruction. */
13830
13831static int
13832check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13833{
13834 unsigned long opcode;
13835 int bdsize;
13836
13837 opcode = bfd_get_16 (abfd, ptr);
13838 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13839 /* 16-bit branch/jump with a 32-bit delay slot. */
13840 bdsize = 4;
13841 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13842 || find_match (opcode, ds_insns_16_bd16) >= 0)
13843 /* 16-bit branch/jump with a 16-bit delay slot. */
13844 bdsize = 2;
13845 else
13846 /* No delay slot. */
13847 bdsize = 0;
13848
13849 return bdsize;
13850}
13851
13852/* If PTR points to what *might* be a 32-bit branch or jump, then
13853 return the minimum length of its delay slot, otherwise return 0.
13854 Non-zero results are not definitive as we might be checking against
13855 the second half of another instruction. */
13856
13857static int
13858check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13859{
13860 unsigned long opcode;
13861 int bdsize;
13862
d21911ea 13863 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13864 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13865 /* 32-bit branch/jump with a 32-bit delay slot. */
13866 bdsize = 4;
13867 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13868 /* 32-bit branch/jump with a 16-bit delay slot. */
13869 bdsize = 2;
13870 else
13871 /* No delay slot. */
13872 bdsize = 0;
13873
13874 return bdsize;
13875}
13876
13877/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13878 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13879
0a1b45a2 13880static bool
df58fc94
RS
13881check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13882{
13883 unsigned long opcode;
13884
13885 opcode = bfd_get_16 (abfd, ptr);
13886 if (MATCH (opcode, b_insn_16)
13887 /* B16 */
13888 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13889 /* JR16 */
13890 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13891 /* BEQZ16, BNEZ16 */
13892 || (MATCH (opcode, jalr_insn_16_bd32)
13893 /* JALR16 */
13894 && reg != JR16_REG (opcode) && reg != RA))
0a1b45a2 13895 return true;
df58fc94 13896
0a1b45a2 13897 return false;
df58fc94
RS
13898}
13899
13900/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13901 then return TRUE, otherwise FALSE. */
13902
0a1b45a2 13903static bool
df58fc94
RS
13904check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13905{
13906 unsigned long opcode;
13907
d21911ea 13908 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13909 if (MATCH (opcode, j_insn_32)
13910 /* J */
13911 || MATCH (opcode, bc_insn_32)
13912 /* BC1F, BC1T, BC2F, BC2T */
13913 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13914 /* JAL, JALX */
13915 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13916 /* BGEZ, BGTZ, BLEZ, BLTZ */
13917 || (MATCH (opcode, bzal_insn_32)
13918 /* BGEZAL, BLTZAL */
13919 && reg != OP32_SREG (opcode) && reg != RA)
13920 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13921 /* JALR, JALR.HB, BEQ, BNE */
13922 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
0a1b45a2 13923 return true;
df58fc94 13924
0a1b45a2 13925 return false;
df58fc94
RS
13926}
13927
80cab405
MR
13928/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13929 IRELEND) at OFFSET indicate that there must be a compact branch there,
13930 then return TRUE, otherwise FALSE. */
df58fc94 13931
0a1b45a2 13932static bool
80cab405
MR
13933check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13934 const Elf_Internal_Rela *internal_relocs,
13935 const Elf_Internal_Rela *irelend)
df58fc94 13936{
80cab405
MR
13937 const Elf_Internal_Rela *irel;
13938 unsigned long opcode;
13939
d21911ea 13940 opcode = bfd_get_micromips_32 (abfd, ptr);
80cab405 13941 if (find_match (opcode, bzc_insns_32) < 0)
0a1b45a2 13942 return false;
df58fc94
RS
13943
13944 for (irel = internal_relocs; irel < irelend; irel++)
80cab405
MR
13945 if (irel->r_offset == offset
13946 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
0a1b45a2 13947 return true;
80cab405 13948
0a1b45a2 13949 return false;
df58fc94 13950}
80cab405
MR
13951
13952/* Bitsize checking. */
13953#define IS_BITSIZE(val, N) \
13954 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13955 - (1ULL << ((N) - 1))) == (val))
13956
df58fc94 13957\f
0a1b45a2 13958bool
df58fc94
RS
13959_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13960 struct bfd_link_info *link_info,
0a1b45a2 13961 bool *again)
df58fc94 13962{
0a1b45a2 13963 bool insn32 = mips_elf_hash_table (link_info)->insn32;
df58fc94
RS
13964 Elf_Internal_Shdr *symtab_hdr;
13965 Elf_Internal_Rela *internal_relocs;
13966 Elf_Internal_Rela *irel, *irelend;
13967 bfd_byte *contents = NULL;
13968 Elf_Internal_Sym *isymbuf = NULL;
13969
13970 /* Assume nothing changes. */
0a1b45a2 13971 *again = false;
df58fc94
RS
13972
13973 /* We don't have to do anything for a relocatable link, if
13974 this section does not have relocs, or if this is not a
13975 code section. */
13976
0e1862bb 13977 if (bfd_link_relocatable (link_info)
df58fc94
RS
13978 || (sec->flags & SEC_RELOC) == 0
13979 || sec->reloc_count == 0
13980 || (sec->flags & SEC_CODE) == 0)
0a1b45a2 13981 return true;
df58fc94
RS
13982
13983 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13984
13985 /* Get a copy of the native relocations. */
13986 internal_relocs = (_bfd_elf_link_read_relocs
2c3fc389 13987 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
df58fc94
RS
13988 link_info->keep_memory));
13989 if (internal_relocs == NULL)
13990 goto error_return;
13991
13992 /* Walk through them looking for relaxing opportunities. */
13993 irelend = internal_relocs + sec->reloc_count;
13994 for (irel = internal_relocs; irel < irelend; irel++)
13995 {
13996 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13997 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
0a1b45a2 13998 bool target_is_micromips_code_p;
df58fc94
RS
13999 unsigned long opcode;
14000 bfd_vma symval;
14001 bfd_vma pcrval;
2309ddf2 14002 bfd_byte *ptr;
df58fc94
RS
14003 int fndopc;
14004
14005 /* The number of bytes to delete for relaxation and from where
07d6d2b8 14006 to delete these bytes starting at irel->r_offset. */
df58fc94
RS
14007 int delcnt = 0;
14008 int deloff = 0;
14009
14010 /* If this isn't something that can be relaxed, then ignore
07d6d2b8 14011 this reloc. */
df58fc94
RS
14012 if (r_type != R_MICROMIPS_HI16
14013 && r_type != R_MICROMIPS_PC16_S1
2309ddf2 14014 && r_type != R_MICROMIPS_26_S1)
df58fc94
RS
14015 continue;
14016
14017 /* Get the section contents if we haven't done so already. */
14018 if (contents == NULL)
14019 {
14020 /* Get cached copy if it exists. */
14021 if (elf_section_data (sec)->this_hdr.contents != NULL)
14022 contents = elf_section_data (sec)->this_hdr.contents;
14023 /* Go get them off disk. */
14024 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
14025 goto error_return;
14026 }
2309ddf2 14027 ptr = contents + irel->r_offset;
df58fc94
RS
14028
14029 /* Read this BFD's local symbols if we haven't done so already. */
14030 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
14031 {
14032 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
14033 if (isymbuf == NULL)
14034 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
14035 symtab_hdr->sh_info, 0,
14036 NULL, NULL, NULL);
14037 if (isymbuf == NULL)
14038 goto error_return;
14039 }
14040
14041 /* Get the value of the symbol referred to by the reloc. */
14042 if (r_symndx < symtab_hdr->sh_info)
14043 {
14044 /* A local symbol. */
14045 Elf_Internal_Sym *isym;
14046 asection *sym_sec;
14047
14048 isym = isymbuf + r_symndx;
14049 if (isym->st_shndx == SHN_UNDEF)
14050 sym_sec = bfd_und_section_ptr;
14051 else if (isym->st_shndx == SHN_ABS)
14052 sym_sec = bfd_abs_section_ptr;
14053 else if (isym->st_shndx == SHN_COMMON)
14054 sym_sec = bfd_com_section_ptr;
14055 else
14056 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14057 symval = (isym->st_value
14058 + sym_sec->output_section->vma
14059 + sym_sec->output_offset);
14060 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
14061 }
14062 else
14063 {
14064 unsigned long indx;
14065 struct elf_link_hash_entry *h;
14066
14067 /* An external symbol. */
14068 indx = r_symndx - symtab_hdr->sh_info;
14069 h = elf_sym_hashes (abfd)[indx];
14070 BFD_ASSERT (h != NULL);
14071
14072 if (h->root.type != bfd_link_hash_defined
14073 && h->root.type != bfd_link_hash_defweak)
14074 /* This appears to be a reference to an undefined
14075 symbol. Just ignore it -- it will be caught by the
14076 regular reloc processing. */
14077 continue;
14078
14079 symval = (h->root.u.def.value
14080 + h->root.u.def.section->output_section->vma
14081 + h->root.u.def.section->output_offset);
14082 target_is_micromips_code_p = (!h->needs_plt
14083 && ELF_ST_IS_MICROMIPS (h->other));
14084 }
14085
14086
14087 /* For simplicity of coding, we are going to modify the
07d6d2b8
AM
14088 section contents, the section relocs, and the BFD symbol
14089 table. We must tell the rest of the code not to free up this
14090 information. It would be possible to instead create a table
14091 of changes which have to be made, as is done in coff-mips.c;
14092 that would be more work, but would require less memory when
14093 the linker is run. */
df58fc94
RS
14094
14095 /* Only 32-bit instructions relaxed. */
14096 if (irel->r_offset + 4 > sec->size)
14097 continue;
14098
d21911ea 14099 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
14100
14101 /* This is the pc-relative distance from the instruction the
07d6d2b8 14102 relocation is applied to, to the symbol referred. */
df58fc94
RS
14103 pcrval = (symval
14104 - (sec->output_section->vma + sec->output_offset)
14105 - irel->r_offset);
14106
14107 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
07d6d2b8
AM
14108 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
14109 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
df58fc94 14110
07d6d2b8 14111 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
df58fc94 14112
07d6d2b8
AM
14113 where pcrval has first to be adjusted to apply against the LO16
14114 location (we make the adjustment later on, when we have figured
14115 out the offset). */
df58fc94
RS
14116 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
14117 {
0a1b45a2 14118 bool bzc = false;
df58fc94
RS
14119 unsigned long nextopc;
14120 unsigned long reg;
14121 bfd_vma offset;
14122
14123 /* Give up if the previous reloc was a HI16 against this symbol
14124 too. */
14125 if (irel > internal_relocs
14126 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
14127 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
14128 continue;
14129
14130 /* Or if the next reloc is not a LO16 against this symbol. */
14131 if (irel + 1 >= irelend
14132 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
14133 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
14134 continue;
14135
14136 /* Or if the second next reloc is a LO16 against this symbol too. */
14137 if (irel + 2 >= irelend
14138 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
14139 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
14140 continue;
14141
80cab405
MR
14142 /* See if the LUI instruction *might* be in a branch delay slot.
14143 We check whether what looks like a 16-bit branch or jump is
14144 actually an immediate argument to a compact branch, and let
14145 it through if so. */
df58fc94 14146 if (irel->r_offset >= 2
2309ddf2 14147 && check_br16_dslot (abfd, ptr - 2)
df58fc94 14148 && !(irel->r_offset >= 4
80cab405
MR
14149 && (bzc = check_relocated_bzc (abfd,
14150 ptr - 4, irel->r_offset - 4,
14151 internal_relocs, irelend))))
df58fc94
RS
14152 continue;
14153 if (irel->r_offset >= 4
80cab405 14154 && !bzc
2309ddf2 14155 && check_br32_dslot (abfd, ptr - 4))
df58fc94
RS
14156 continue;
14157
14158 reg = OP32_SREG (opcode);
14159
14160 /* We only relax adjacent instructions or ones separated with
14161 a branch or jump that has a delay slot. The branch or jump
14162 must not fiddle with the register used to hold the address.
14163 Subtract 4 for the LUI itself. */
14164 offset = irel[1].r_offset - irel[0].r_offset;
14165 switch (offset - 4)
14166 {
14167 case 0:
14168 break;
14169 case 2:
2309ddf2 14170 if (check_br16 (abfd, ptr + 4, reg))
df58fc94
RS
14171 break;
14172 continue;
14173 case 4:
2309ddf2 14174 if (check_br32 (abfd, ptr + 4, reg))
df58fc94
RS
14175 break;
14176 continue;
14177 default:
14178 continue;
14179 }
14180
d21911ea 14181 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
df58fc94
RS
14182
14183 /* Give up unless the same register is used with both
14184 relocations. */
14185 if (OP32_SREG (nextopc) != reg)
14186 continue;
14187
14188 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14189 and rounding up to take masking of the two LSBs into account. */
14190 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14191
14192 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
14193 if (IS_BITSIZE (symval, 16))
14194 {
14195 /* Fix the relocation's type. */
14196 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14197
14198 /* Instructions using R_MICROMIPS_LO16 have the base or
07d6d2b8
AM
14199 source register in bits 20:16. This register becomes $0
14200 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
df58fc94
RS
14201 nextopc &= ~0x001f0000;
14202 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14203 contents + irel[1].r_offset);
14204 }
14205
14206 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14207 We add 4 to take LUI deletion into account while checking
14208 the PC-relative distance. */
14209 else if (symval % 4 == 0
14210 && IS_BITSIZE (pcrval + 4, 25)
14211 && MATCH (nextopc, addiu_insn)
14212 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14213 && OP16_VALID_REG (OP32_TREG (nextopc)))
14214 {
14215 /* Fix the relocation's type. */
14216 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14217
14218 /* Replace ADDIU with the ADDIUPC version. */
14219 nextopc = (addiupc_insn.match
14220 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14221
d21911ea
MR
14222 bfd_put_micromips_32 (abfd, nextopc,
14223 contents + irel[1].r_offset);
df58fc94
RS
14224 }
14225
14226 /* Can't do anything, give up, sigh... */
14227 else
14228 continue;
14229
14230 /* Fix the relocation's type. */
14231 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14232
14233 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
14234 delcnt = 4;
14235 deloff = 0;
14236 }
14237
14238 /* Compact branch relaxation -- due to the multitude of macros
07d6d2b8
AM
14239 employed by the compiler/assembler, compact branches are not
14240 always generated. Obviously, this can/will be fixed elsewhere,
14241 but there is no drawback in double checking it here. */
df58fc94
RS
14242 else if (r_type == R_MICROMIPS_PC16_S1
14243 && irel->r_offset + 5 < sec->size
14244 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14245 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
833794fc
MR
14246 && ((!insn32
14247 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14248 nop_insn_16) ? 2 : 0))
14249 || (irel->r_offset + 7 < sec->size
14250 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14251 ptr + 4),
14252 nop_insn_32) ? 4 : 0))))
df58fc94
RS
14253 {
14254 unsigned long reg;
14255
14256 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14257
14258 /* Replace BEQZ/BNEZ with the compact version. */
14259 opcode = (bzc_insns_32[fndopc].match
14260 | BZC32_REG_FIELD (reg)
14261 | (opcode & 0xffff)); /* Addend value. */
14262
d21911ea 14263 bfd_put_micromips_32 (abfd, opcode, ptr);
df58fc94 14264
833794fc
MR
14265 /* Delete the delay slot NOP: two or four bytes from
14266 irel->offset + 4; delcnt has already been set above. */
df58fc94
RS
14267 deloff = 4;
14268 }
14269
14270 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
07d6d2b8 14271 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
14272 else if (!insn32
14273 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
14274 && IS_BITSIZE (pcrval - 2, 11)
14275 && find_match (opcode, b_insns_32) >= 0)
14276 {
14277 /* Fix the relocation's type. */
14278 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14279
a8685210 14280 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
14281 bfd_put_16 (abfd,
14282 (b_insn_16.match
14283 | (opcode & 0x3ff)), /* Addend value. */
2309ddf2 14284 ptr);
df58fc94
RS
14285
14286 /* Delete 2 bytes from irel->r_offset + 2. */
14287 delcnt = 2;
14288 deloff = 2;
14289 }
14290
14291 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
07d6d2b8 14292 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
14293 else if (!insn32
14294 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
14295 && IS_BITSIZE (pcrval - 2, 8)
14296 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14297 && OP16_VALID_REG (OP32_SREG (opcode)))
14298 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14299 && OP16_VALID_REG (OP32_TREG (opcode)))))
14300 {
14301 unsigned long reg;
14302
14303 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14304
14305 /* Fix the relocation's type. */
14306 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14307
a8685210 14308 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
14309 bfd_put_16 (abfd,
14310 (bz_insns_16[fndopc].match
14311 | BZ16_REG_FIELD (reg)
14312 | (opcode & 0x7f)), /* Addend value. */
2309ddf2 14313 ptr);
df58fc94
RS
14314
14315 /* Delete 2 bytes from irel->r_offset + 2. */
14316 delcnt = 2;
14317 deloff = 2;
14318 }
14319
14320 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
833794fc
MR
14321 else if (!insn32
14322 && r_type == R_MICROMIPS_26_S1
df58fc94
RS
14323 && target_is_micromips_code_p
14324 && irel->r_offset + 7 < sec->size
14325 && MATCH (opcode, jal_insn_32_bd32))
14326 {
14327 unsigned long n32opc;
0a1b45a2 14328 bool relaxed = false;
df58fc94 14329
d21911ea 14330 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
df58fc94
RS
14331
14332 if (MATCH (n32opc, nop_insn_32))
14333 {
14334 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
2309ddf2 14335 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
df58fc94 14336
0a1b45a2 14337 relaxed = true;
df58fc94
RS
14338 }
14339 else if (find_match (n32opc, move_insns_32) >= 0)
14340 {
14341 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14342 bfd_put_16 (abfd,
14343 (move_insn_16.match
14344 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14345 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
2309ddf2 14346 ptr + 4);
df58fc94 14347
0a1b45a2 14348 relaxed = true;
df58fc94
RS
14349 }
14350 /* Other 32-bit instructions relaxable to 16-bit
14351 instructions will be handled here later. */
14352
14353 if (relaxed)
14354 {
14355 /* JAL with 32-bit delay slot that is changed to a JALS
07d6d2b8 14356 with 16-bit delay slot. */
d21911ea 14357 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
df58fc94
RS
14358
14359 /* Delete 2 bytes from irel->r_offset + 6. */
14360 delcnt = 2;
14361 deloff = 6;
14362 }
14363 }
14364
14365 if (delcnt != 0)
14366 {
14367 /* Note that we've changed the relocs, section contents, etc. */
14368 elf_section_data (sec)->relocs = internal_relocs;
14369 elf_section_data (sec)->this_hdr.contents = contents;
14370 symtab_hdr->contents = (unsigned char *) isymbuf;
14371
14372 /* Delete bytes depending on the delcnt and deloff. */
14373 if (!mips_elf_relax_delete_bytes (abfd, sec,
14374 irel->r_offset + deloff, delcnt))
14375 goto error_return;
14376
14377 /* That will change things, so we should relax again.
14378 Note that this is not required, and it may be slow. */
0a1b45a2 14379 *again = true;
df58fc94
RS
14380 }
14381 }
14382
14383 if (isymbuf != NULL
14384 && symtab_hdr->contents != (unsigned char *) isymbuf)
14385 {
14386 if (! link_info->keep_memory)
14387 free (isymbuf);
14388 else
14389 {
14390 /* Cache the symbols for elf_link_input_bfd. */
14391 symtab_hdr->contents = (unsigned char *) isymbuf;
14392 }
14393 }
14394
14395 if (contents != NULL
14396 && elf_section_data (sec)->this_hdr.contents != contents)
14397 {
14398 if (! link_info->keep_memory)
14399 free (contents);
14400 else
14401 {
14402 /* Cache the section contents for elf_link_input_bfd. */
14403 elf_section_data (sec)->this_hdr.contents = contents;
14404 }
14405 }
14406
c9594989 14407 if (elf_section_data (sec)->relocs != internal_relocs)
df58fc94
RS
14408 free (internal_relocs);
14409
0a1b45a2 14410 return true;
df58fc94
RS
14411
14412 error_return:
c9594989 14413 if (symtab_hdr->contents != (unsigned char *) isymbuf)
df58fc94 14414 free (isymbuf);
c9594989 14415 if (elf_section_data (sec)->this_hdr.contents != contents)
df58fc94 14416 free (contents);
c9594989 14417 if (elf_section_data (sec)->relocs != internal_relocs)
df58fc94
RS
14418 free (internal_relocs);
14419
0a1b45a2 14420 return false;
df58fc94
RS
14421}
14422\f
b49e97c9
TS
14423/* Create a MIPS ELF linker hash table. */
14424
14425struct bfd_link_hash_table *
9719ad41 14426_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
14427{
14428 struct mips_elf_link_hash_table *ret;
986f0783 14429 size_t amt = sizeof (struct mips_elf_link_hash_table);
b49e97c9 14430
7bf52ea2 14431 ret = bfd_zmalloc (amt);
9719ad41 14432 if (ret == NULL)
b49e97c9
TS
14433 return NULL;
14434
66eb6687
AM
14435 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14436 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
14437 sizeof (struct mips_elf_link_hash_entry),
14438 MIPS_ELF_DATA))
b49e97c9 14439 {
e2d34d7d 14440 free (ret);
b49e97c9
TS
14441 return NULL;
14442 }
1bbce132
MR
14443 ret->root.init_plt_refcount.plist = NULL;
14444 ret->root.init_plt_offset.plist = NULL;
b49e97c9 14445
b49e97c9
TS
14446 return &ret->root.root;
14447}
0a44bf69
RS
14448
14449/* Likewise, but indicate that the target is VxWorks. */
14450
14451struct bfd_link_hash_table *
14452_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14453{
14454 struct bfd_link_hash_table *ret;
14455
14456 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14457 if (ret)
14458 {
14459 struct mips_elf_link_hash_table *htab;
14460
14461 htab = (struct mips_elf_link_hash_table *) ret;
0a1b45a2 14462 htab->use_plts_and_copy_relocs = true;
0a44bf69
RS
14463 }
14464 return ret;
14465}
861fb55a
DJ
14466
14467/* A function that the linker calls if we are allowed to use PLTs
14468 and copy relocs. */
14469
14470void
14471_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14472{
0a1b45a2 14473 mips_elf_hash_table (info)->use_plts_and_copy_relocs = true;
861fb55a 14474}
833794fc
MR
14475
14476/* A function that the linker calls to select between all or only
8b10b0b3 14477 32-bit microMIPS instructions, and between making or ignoring
47275900
MR
14478 branch relocation checks for invalid transitions between ISA modes.
14479 Also record whether we have been configured for a GNU target. */
833794fc
MR
14480
14481void
0a1b45a2
AM
14482_bfd_mips_elf_linker_flags (struct bfd_link_info *info, bool insn32,
14483 bool ignore_branch_isa,
14484 bool gnu_target)
833794fc 14485{
8b10b0b3
MR
14486 mips_elf_hash_table (info)->insn32 = insn32;
14487 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
47275900 14488 mips_elf_hash_table (info)->gnu_target = gnu_target;
833794fc 14489}
3734320d
MF
14490
14491/* A function that the linker calls to enable use of compact branches in
14492 linker generated code for MIPSR6. */
14493
14494void
0a1b45a2 14495_bfd_mips_elf_compact_branches (struct bfd_link_info *info, bool on)
3734320d
MF
14496{
14497 mips_elf_hash_table (info)->compact_branches = on;
14498}
14499
b49e97c9 14500\f
c97c330b
MF
14501/* Structure for saying that BFD machine EXTENSION extends BASE. */
14502
14503struct mips_mach_extension
14504{
14505 unsigned long extension, base;
14506};
14507
14508
14509/* An array describing how BFD machines relate to one another. The entries
14510 are ordered topologically with MIPS I extensions listed last. */
14511
14512static const struct mips_mach_extension mips_mach_extensions[] =
14513{
14514 /* MIPS64r2 extensions. */
14515 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14516 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14517 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14518 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
9108bc33 14519 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
bd782c07 14520 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
ac8cb70f 14521 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
c97c330b
MF
14522
14523 /* MIPS64 extensions. */
14524 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14525 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14526 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14527
14528 /* MIPS V extensions. */
14529 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14530
14531 /* R10000 extensions. */
14532 { bfd_mach_mips12000, bfd_mach_mips10000 },
14533 { bfd_mach_mips14000, bfd_mach_mips10000 },
14534 { bfd_mach_mips16000, bfd_mach_mips10000 },
14535
14536 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14537 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14538 better to allow vr5400 and vr5500 code to be merged anyway, since
14539 many libraries will just use the core ISA. Perhaps we could add
14540 some sort of ASE flag if this ever proves a problem. */
14541 { bfd_mach_mips5500, bfd_mach_mips5400 },
14542 { bfd_mach_mips5400, bfd_mach_mips5000 },
14543
14544 /* MIPS IV extensions. */
14545 { bfd_mach_mips5, bfd_mach_mips8000 },
14546 { bfd_mach_mips10000, bfd_mach_mips8000 },
14547 { bfd_mach_mips5000, bfd_mach_mips8000 },
14548 { bfd_mach_mips7000, bfd_mach_mips8000 },
14549 { bfd_mach_mips9000, bfd_mach_mips8000 },
14550
14551 /* VR4100 extensions. */
14552 { bfd_mach_mips4120, bfd_mach_mips4100 },
14553 { bfd_mach_mips4111, bfd_mach_mips4100 },
14554
14555 /* MIPS III extensions. */
14556 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14557 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14558 { bfd_mach_mips8000, bfd_mach_mips4000 },
14559 { bfd_mach_mips4650, bfd_mach_mips4000 },
14560 { bfd_mach_mips4600, bfd_mach_mips4000 },
14561 { bfd_mach_mips4400, bfd_mach_mips4000 },
14562 { bfd_mach_mips4300, bfd_mach_mips4000 },
14563 { bfd_mach_mips4100, bfd_mach_mips4000 },
c97c330b
MF
14564 { bfd_mach_mips5900, bfd_mach_mips4000 },
14565
38bf472a
MR
14566 /* MIPS32r3 extensions. */
14567 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14568
14569 /* MIPS32r2 extensions. */
14570 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14571
c97c330b
MF
14572 /* MIPS32 extensions. */
14573 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14574
14575 /* MIPS II extensions. */
14576 { bfd_mach_mips4000, bfd_mach_mips6000 },
14577 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
b417536f 14578 { bfd_mach_mips4010, bfd_mach_mips6000 },
c97c330b
MF
14579
14580 /* MIPS I extensions. */
14581 { bfd_mach_mips6000, bfd_mach_mips3000 },
14582 { bfd_mach_mips3900, bfd_mach_mips3000 }
14583};
14584
14585/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14586
0a1b45a2 14587static bool
c97c330b
MF
14588mips_mach_extends_p (unsigned long base, unsigned long extension)
14589{
14590 size_t i;
14591
14592 if (extension == base)
0a1b45a2 14593 return true;
c97c330b
MF
14594
14595 if (base == bfd_mach_mipsisa32
14596 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
0a1b45a2 14597 return true;
c97c330b
MF
14598
14599 if (base == bfd_mach_mipsisa32r2
14600 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
0a1b45a2 14601 return true;
c97c330b
MF
14602
14603 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14604 if (extension == mips_mach_extensions[i].extension)
14605 {
14606 extension = mips_mach_extensions[i].base;
14607 if (extension == base)
0a1b45a2 14608 return true;
c97c330b
MF
14609 }
14610
0a1b45a2 14611 return false;
c97c330b
MF
14612}
14613
14614/* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14615
14616static unsigned long
14617bfd_mips_isa_ext_mach (unsigned int isa_ext)
14618{
14619 switch (isa_ext)
14620 {
07d6d2b8
AM
14621 case AFL_EXT_3900: return bfd_mach_mips3900;
14622 case AFL_EXT_4010: return bfd_mach_mips4010;
14623 case AFL_EXT_4100: return bfd_mach_mips4100;
14624 case AFL_EXT_4111: return bfd_mach_mips4111;
14625 case AFL_EXT_4120: return bfd_mach_mips4120;
14626 case AFL_EXT_4650: return bfd_mach_mips4650;
14627 case AFL_EXT_5400: return bfd_mach_mips5400;
14628 case AFL_EXT_5500: return bfd_mach_mips5500;
14629 case AFL_EXT_5900: return bfd_mach_mips5900;
14630 case AFL_EXT_10000: return bfd_mach_mips10000;
c97c330b
MF
14631 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14632 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
07d6d2b8 14633 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
c97c330b
MF
14634 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14635 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14636 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
07d6d2b8
AM
14637 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14638 default: return bfd_mach_mips3000;
c97c330b
MF
14639 }
14640}
14641
351cdf24
MF
14642/* Return the .MIPS.abiflags value representing each ISA Extension. */
14643
14644unsigned int
14645bfd_mips_isa_ext (bfd *abfd)
14646{
14647 switch (bfd_get_mach (abfd))
14648 {
07d6d2b8
AM
14649 case bfd_mach_mips3900: return AFL_EXT_3900;
14650 case bfd_mach_mips4010: return AFL_EXT_4010;
14651 case bfd_mach_mips4100: return AFL_EXT_4100;
14652 case bfd_mach_mips4111: return AFL_EXT_4111;
14653 case bfd_mach_mips4120: return AFL_EXT_4120;
14654 case bfd_mach_mips4650: return AFL_EXT_4650;
14655 case bfd_mach_mips5400: return AFL_EXT_5400;
14656 case bfd_mach_mips5500: return AFL_EXT_5500;
14657 case bfd_mach_mips5900: return AFL_EXT_5900;
14658 case bfd_mach_mips10000: return AFL_EXT_10000;
c97c330b
MF
14659 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14660 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
07d6d2b8
AM
14661 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14662 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14663 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14664 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14665 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14666 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
38bf472a
MR
14667 case bfd_mach_mips_interaptiv_mr2:
14668 return AFL_EXT_INTERAPTIV_MR2;
07d6d2b8 14669 default: return 0;
c97c330b
MF
14670 }
14671}
14672
14673/* Encode ISA level and revision as a single value. */
14674#define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14675
14676/* Decode a single value into level and revision. */
14677#define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14678#define ISA_REV(LEVREV) ((LEVREV) & 0x7)
351cdf24
MF
14679
14680/* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14681
14682static void
14683update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14684{
c97c330b 14685 int new_isa = 0;
351cdf24
MF
14686 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14687 {
c97c330b
MF
14688 case E_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14689 case E_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14690 case E_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14691 case E_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14692 case E_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14693 case E_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14694 case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14695 case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14696 case E_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14697 case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14698 case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
351cdf24 14699 default:
4eca0228 14700 _bfd_error_handler
695344c0 14701 /* xgettext:c-format */
2c1c9679 14702 (_("%pB: unknown architecture %s"),
351cdf24
MF
14703 abfd, bfd_printable_name (abfd));
14704 }
14705
c97c330b
MF
14706 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14707 {
14708 abiflags->isa_level = ISA_LEVEL (new_isa);
14709 abiflags->isa_rev = ISA_REV (new_isa);
14710 }
14711
14712 /* Update the isa_ext if ABFD describes a further extension. */
14713 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14714 bfd_get_mach (abfd)))
14715 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
351cdf24
MF
14716}
14717
14718/* Return true if the given ELF header flags describe a 32-bit binary. */
14719
0a1b45a2 14720static bool
351cdf24
MF
14721mips_32bit_flags_p (flagword flags)
14722{
14723 return ((flags & EF_MIPS_32BITMODE) != 0
14724 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14725 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14726 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14727 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14728 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
7361da2c
AB
14729 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14730 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
351cdf24
MF
14731}
14732
14733/* Infer the content of the ABI flags based on the elf header. */
14734
14735static void
14736infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14737{
14738 obj_attribute *in_attr;
14739
14740 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14741 update_mips_abiflags_isa (abfd, abiflags);
14742
14743 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14744 abiflags->gpr_size = AFL_REG_32;
14745 else
14746 abiflags->gpr_size = AFL_REG_64;
14747
14748 abiflags->cpr1_size = AFL_REG_NONE;
14749
14750 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14751 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14752
14753 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14754 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14755 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14756 && abiflags->gpr_size == AFL_REG_32))
14757 abiflags->cpr1_size = AFL_REG_32;
14758 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14759 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14760 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14761 abiflags->cpr1_size = AFL_REG_64;
14762
14763 abiflags->cpr2_size = AFL_REG_NONE;
14764
14765 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14766 abiflags->ases |= AFL_ASE_MDMX;
14767 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14768 abiflags->ases |= AFL_ASE_MIPS16;
14769 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14770 abiflags->ases |= AFL_ASE_MICROMIPS;
14771
14772 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14773 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14774 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14775 && abiflags->isa_level >= 32
bdc6c06e 14776 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
351cdf24
MF
14777 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14778}
14779
b49e97c9
TS
14780/* We need to use a special link routine to handle the .reginfo and
14781 the .mdebug sections. We need to merge all instances of these
14782 sections together, not write them all out sequentially. */
14783
0a1b45a2 14784bool
9719ad41 14785_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 14786{
b49e97c9
TS
14787 asection *o;
14788 struct bfd_link_order *p;
14789 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
351cdf24 14790 asection *rtproc_sec, *abiflags_sec;
b49e97c9
TS
14791 Elf32_RegInfo reginfo;
14792 struct ecoff_debug_info debug;
861fb55a 14793 struct mips_htab_traverse_info hti;
7a2a6943
NC
14794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14795 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 14796 HDRR *symhdr = &debug.symbolic_header;
9719ad41 14797 void *mdebug_handle = NULL;
b49e97c9
TS
14798 asection *s;
14799 EXTR esym;
14800 unsigned int i;
14801 bfd_size_type amt;
0a44bf69 14802 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
14803
14804 static const char * const secname[] =
14805 {
14806 ".text", ".init", ".fini", ".data",
14807 ".rodata", ".sdata", ".sbss", ".bss"
14808 };
14809 static const int sc[] =
14810 {
14811 scText, scInit, scFini, scData,
14812 scRData, scSData, scSBss, scBss
14813 };
14814
0a44bf69 14815 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
14816 BFD_ASSERT (htab != NULL);
14817
64575f78
MR
14818 /* Sort the dynamic symbols so that those with GOT entries come after
14819 those without. */
d4596a51 14820 if (!mips_elf_sort_hash_table (abfd, info))
0a1b45a2 14821 return false;
b49e97c9 14822
861fb55a
DJ
14823 /* Create any scheduled LA25 stubs. */
14824 hti.info = info;
14825 hti.output_bfd = abfd;
0a1b45a2 14826 hti.error = false;
861fb55a
DJ
14827 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14828 if (hti.error)
0a1b45a2 14829 return false;
861fb55a 14830
b49e97c9
TS
14831 /* Get a value for the GP register. */
14832 if (elf_gp (abfd) == 0)
14833 {
14834 struct bfd_link_hash_entry *h;
14835
0a1b45a2 14836 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
9719ad41 14837 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
14838 elf_gp (abfd) = (h->u.def.value
14839 + h->u.def.section->output_section->vma
14840 + h->u.def.section->output_offset);
90c14f0c 14841 else if (htab->root.target_os == is_vxworks
0a44bf69
RS
14842 && (h = bfd_link_hash_lookup (info->hash,
14843 "_GLOBAL_OFFSET_TABLE_",
0a1b45a2 14844 false, false, true))
0a44bf69
RS
14845 && h->type == bfd_link_hash_defined)
14846 elf_gp (abfd) = (h->u.def.section->output_section->vma
14847 + h->u.def.section->output_offset
14848 + h->u.def.value);
0e1862bb 14849 else if (bfd_link_relocatable (info))
b49e97c9
TS
14850 {
14851 bfd_vma lo = MINUS_ONE;
14852
14853 /* Find the GP-relative section with the lowest offset. */
9719ad41 14854 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
14855 if (o->vma < lo
14856 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14857 lo = o->vma;
14858
14859 /* And calculate GP relative to that. */
0a44bf69 14860 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
14861 }
14862 else
14863 {
14864 /* If the relocate_section function needs to do a reloc
14865 involving the GP value, it should make a reloc_dangerous
14866 callback to warn that GP is not defined. */
14867 }
14868 }
14869
14870 /* Go through the sections and collect the .reginfo and .mdebug
14871 information. */
351cdf24 14872 abiflags_sec = NULL;
b49e97c9
TS
14873 reginfo_sec = NULL;
14874 mdebug_sec = NULL;
14875 gptab_data_sec = NULL;
14876 gptab_bss_sec = NULL;
9719ad41 14877 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9 14878 {
351cdf24
MF
14879 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14880 {
14881 /* We have found the .MIPS.abiflags section in the output file.
14882 Look through all the link_orders comprising it and remove them.
14883 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14884 for (p = o->map_head.link_order; p != NULL; p = p->next)
14885 {
14886 asection *input_section;
14887
14888 if (p->type != bfd_indirect_link_order)
14889 {
14890 if (p->type == bfd_data_link_order)
14891 continue;
14892 abort ();
14893 }
14894
14895 input_section = p->u.indirect.section;
14896
14897 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14898 elf_link_input_bfd ignores this section. */
14899 input_section->flags &= ~SEC_HAS_CONTENTS;
14900 }
14901
14902 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14903 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14904
14905 /* Skip this section later on (I don't think this currently
14906 matters, but someday it might). */
14907 o->map_head.link_order = NULL;
14908
14909 abiflags_sec = o;
14910 }
14911
b49e97c9
TS
14912 if (strcmp (o->name, ".reginfo") == 0)
14913 {
14914 memset (&reginfo, 0, sizeof reginfo);
14915
14916 /* We have found the .reginfo section in the output file.
14917 Look through all the link_orders comprising it and merge
14918 the information together. */
8423293d 14919 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14920 {
14921 asection *input_section;
14922 bfd *input_bfd;
14923 Elf32_External_RegInfo ext;
14924 Elf32_RegInfo sub;
6798f8bf 14925 bfd_size_type sz;
b49e97c9
TS
14926
14927 if (p->type != bfd_indirect_link_order)
14928 {
14929 if (p->type == bfd_data_link_order)
14930 continue;
14931 abort ();
14932 }
14933
14934 input_section = p->u.indirect.section;
14935 input_bfd = input_section->owner;
14936
6798f8bf
MR
14937 sz = (input_section->size < sizeof (ext)
14938 ? input_section->size : sizeof (ext));
14939 memset (&ext, 0, sizeof (ext));
b49e97c9 14940 if (! bfd_get_section_contents (input_bfd, input_section,
6798f8bf 14941 &ext, 0, sz))
0a1b45a2 14942 return false;
b49e97c9
TS
14943
14944 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14945
14946 reginfo.ri_gprmask |= sub.ri_gprmask;
14947 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14948 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14949 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14950 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14951
14952 /* ri_gp_value is set by the function
1c5e4ee9 14953 `_bfd_mips_elf_section_processing' when the section is
b49e97c9
TS
14954 finally written out. */
14955
14956 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14957 elf_link_input_bfd ignores this section. */
14958 input_section->flags &= ~SEC_HAS_CONTENTS;
14959 }
14960
14961 /* Size has been set in _bfd_mips_elf_always_size_sections. */
b248d650 14962 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
14963
14964 /* Skip this section later on (I don't think this currently
14965 matters, but someday it might). */
8423293d 14966 o->map_head.link_order = NULL;
b49e97c9
TS
14967
14968 reginfo_sec = o;
14969 }
14970
14971 if (strcmp (o->name, ".mdebug") == 0)
14972 {
14973 struct extsym_info einfo;
14974 bfd_vma last;
14975
14976 /* We have found the .mdebug section in the output file.
14977 Look through all the link_orders comprising it and merge
14978 the information together. */
14979 symhdr->magic = swap->sym_magic;
14980 /* FIXME: What should the version stamp be? */
14981 symhdr->vstamp = 0;
14982 symhdr->ilineMax = 0;
14983 symhdr->cbLine = 0;
14984 symhdr->idnMax = 0;
14985 symhdr->ipdMax = 0;
14986 symhdr->isymMax = 0;
14987 symhdr->ioptMax = 0;
14988 symhdr->iauxMax = 0;
14989 symhdr->issMax = 0;
14990 symhdr->issExtMax = 0;
14991 symhdr->ifdMax = 0;
14992 symhdr->crfd = 0;
14993 symhdr->iextMax = 0;
14994
14995 /* We accumulate the debugging information itself in the
14996 debug_info structure. */
14997 debug.line = NULL;
14998 debug.external_dnr = NULL;
14999 debug.external_pdr = NULL;
15000 debug.external_sym = NULL;
15001 debug.external_opt = NULL;
15002 debug.external_aux = NULL;
15003 debug.ss = NULL;
15004 debug.ssext = debug.ssext_end = NULL;
15005 debug.external_fdr = NULL;
15006 debug.external_rfd = NULL;
15007 debug.external_ext = debug.external_ext_end = NULL;
15008
15009 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 15010 if (mdebug_handle == NULL)
0a1b45a2 15011 return false;
b49e97c9
TS
15012
15013 esym.jmptbl = 0;
15014 esym.cobol_main = 0;
15015 esym.weakext = 0;
15016 esym.reserved = 0;
15017 esym.ifd = ifdNil;
15018 esym.asym.iss = issNil;
15019 esym.asym.st = stLocal;
15020 esym.asym.reserved = 0;
15021 esym.asym.index = indexNil;
15022 last = 0;
15023 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
15024 {
15025 esym.asym.sc = sc[i];
15026 s = bfd_get_section_by_name (abfd, secname[i]);
15027 if (s != NULL)
15028 {
15029 esym.asym.value = s->vma;
eea6121a 15030 last = s->vma + s->size;
b49e97c9
TS
15031 }
15032 else
15033 esym.asym.value = last;
15034 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
15035 secname[i], &esym))
0a1b45a2 15036 return false;
b49e97c9
TS
15037 }
15038
8423293d 15039 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
15040 {
15041 asection *input_section;
15042 bfd *input_bfd;
15043 const struct ecoff_debug_swap *input_swap;
15044 struct ecoff_debug_info input_debug;
15045 char *eraw_src;
15046 char *eraw_end;
15047
15048 if (p->type != bfd_indirect_link_order)
15049 {
15050 if (p->type == bfd_data_link_order)
15051 continue;
15052 abort ();
15053 }
15054
15055 input_section = p->u.indirect.section;
15056 input_bfd = input_section->owner;
15057
d5eaccd7 15058 if (!is_mips_elf (input_bfd))
b49e97c9
TS
15059 {
15060 /* I don't know what a non MIPS ELF bfd would be
15061 doing with a .mdebug section, but I don't really
15062 want to deal with it. */
15063 continue;
15064 }
15065
15066 input_swap = (get_elf_backend_data (input_bfd)
15067 ->elf_backend_ecoff_debug_swap);
15068
eea6121a 15069 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
15070
15071 /* The ECOFF linking code expects that we have already
15072 read in the debugging information and set up an
15073 ecoff_debug_info structure, so we do that now. */
15074 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
15075 &input_debug))
0a1b45a2 15076 return false;
b49e97c9
TS
15077
15078 if (! (bfd_ecoff_debug_accumulate
15079 (mdebug_handle, abfd, &debug, swap, input_bfd,
15080 &input_debug, input_swap, info)))
0a1b45a2 15081 return false;
b49e97c9
TS
15082
15083 /* Loop through the external symbols. For each one with
15084 interesting information, try to find the symbol in
15085 the linker global hash table and save the information
15086 for the output external symbols. */
15087 eraw_src = input_debug.external_ext;
15088 eraw_end = (eraw_src
15089 + (input_debug.symbolic_header.iextMax
15090 * input_swap->external_ext_size));
15091 for (;
15092 eraw_src < eraw_end;
15093 eraw_src += input_swap->external_ext_size)
15094 {
15095 EXTR ext;
15096 const char *name;
15097 struct mips_elf_link_hash_entry *h;
15098
9719ad41 15099 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
15100 if (ext.asym.sc == scNil
15101 || ext.asym.sc == scUndefined
15102 || ext.asym.sc == scSUndefined)
15103 continue;
15104
15105 name = input_debug.ssext + ext.asym.iss;
15106 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
0a1b45a2 15107 name, false, false, true);
b49e97c9
TS
15108 if (h == NULL || h->esym.ifd != -2)
15109 continue;
15110
15111 if (ext.ifd != -1)
15112 {
15113 BFD_ASSERT (ext.ifd
15114 < input_debug.symbolic_header.ifdMax);
15115 ext.ifd = input_debug.ifdmap[ext.ifd];
15116 }
15117
15118 h->esym = ext;
15119 }
15120
15121 /* Free up the information we just read. */
15122 free (input_debug.line);
15123 free (input_debug.external_dnr);
15124 free (input_debug.external_pdr);
15125 free (input_debug.external_sym);
15126 free (input_debug.external_opt);
15127 free (input_debug.external_aux);
15128 free (input_debug.ss);
15129 free (input_debug.ssext);
15130 free (input_debug.external_fdr);
15131 free (input_debug.external_rfd);
15132 free (input_debug.external_ext);
15133
15134 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15135 elf_link_input_bfd ignores this section. */
15136 input_section->flags &= ~SEC_HAS_CONTENTS;
15137 }
15138
0e1862bb 15139 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
b49e97c9
TS
15140 {
15141 /* Create .rtproc section. */
87e0a731 15142 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
b49e97c9
TS
15143 if (rtproc_sec == NULL)
15144 {
15145 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
15146 | SEC_LINKER_CREATED | SEC_READONLY);
15147
87e0a731
AM
15148 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15149 ".rtproc",
15150 flags);
b49e97c9 15151 if (rtproc_sec == NULL
fd361982 15152 || !bfd_set_section_alignment (rtproc_sec, 4))
0a1b45a2 15153 return false;
b49e97c9
TS
15154 }
15155
15156 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15157 info, rtproc_sec,
15158 &debug))
0a1b45a2 15159 return false;
b49e97c9
TS
15160 }
15161
15162 /* Build the external symbol information. */
15163 einfo.abfd = abfd;
15164 einfo.info = info;
15165 einfo.debug = &debug;
15166 einfo.swap = swap;
0a1b45a2 15167 einfo.failed = false;
b49e97c9 15168 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 15169 mips_elf_output_extsym, &einfo);
b49e97c9 15170 if (einfo.failed)
0a1b45a2 15171 return false;
b49e97c9
TS
15172
15173 /* Set the size of the .mdebug section. */
eea6121a 15174 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
15175
15176 /* Skip this section later on (I don't think this currently
15177 matters, but someday it might). */
8423293d 15178 o->map_head.link_order = NULL;
b49e97c9
TS
15179
15180 mdebug_sec = o;
15181 }
15182
08dedd66 15183 if (startswith (o->name, ".gptab."))
b49e97c9
TS
15184 {
15185 const char *subname;
15186 unsigned int c;
15187 Elf32_gptab *tab;
15188 Elf32_External_gptab *ext_tab;
15189 unsigned int j;
15190
15191 /* The .gptab.sdata and .gptab.sbss sections hold
15192 information describing how the small data area would
15193 change depending upon the -G switch. These sections
15194 not used in executables files. */
0e1862bb 15195 if (! bfd_link_relocatable (info))
b49e97c9 15196 {
8423293d 15197 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
15198 {
15199 asection *input_section;
15200
15201 if (p->type != bfd_indirect_link_order)
15202 {
15203 if (p->type == bfd_data_link_order)
15204 continue;
15205 abort ();
15206 }
15207
15208 input_section = p->u.indirect.section;
15209
15210 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15211 elf_link_input_bfd ignores this section. */
15212 input_section->flags &= ~SEC_HAS_CONTENTS;
15213 }
15214
15215 /* Skip this section later on (I don't think this
15216 currently matters, but someday it might). */
8423293d 15217 o->map_head.link_order = NULL;
b49e97c9
TS
15218
15219 /* Really remove the section. */
5daa8fe7 15220 bfd_section_list_remove (abfd, o);
b49e97c9
TS
15221 --abfd->section_count;
15222
15223 continue;
15224 }
15225
15226 /* There is one gptab for initialized data, and one for
15227 uninitialized data. */
15228 if (strcmp (o->name, ".gptab.sdata") == 0)
15229 gptab_data_sec = o;
15230 else if (strcmp (o->name, ".gptab.sbss") == 0)
15231 gptab_bss_sec = o;
15232 else
15233 {
4eca0228 15234 _bfd_error_handler
695344c0 15235 /* xgettext:c-format */
871b3ab2 15236 (_("%pB: illegal section name `%pA'"), abfd, o);
b49e97c9 15237 bfd_set_error (bfd_error_nonrepresentable_section);
0a1b45a2 15238 return false;
b49e97c9
TS
15239 }
15240
15241 /* The linker script always combines .gptab.data and
15242 .gptab.sdata into .gptab.sdata, and likewise for
15243 .gptab.bss and .gptab.sbss. It is possible that there is
15244 no .sdata or .sbss section in the output file, in which
15245 case we must change the name of the output section. */
15246 subname = o->name + sizeof ".gptab" - 1;
15247 if (bfd_get_section_by_name (abfd, subname) == NULL)
15248 {
15249 if (o == gptab_data_sec)
15250 o->name = ".gptab.data";
15251 else
15252 o->name = ".gptab.bss";
15253 subname = o->name + sizeof ".gptab" - 1;
15254 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15255 }
15256
15257 /* Set up the first entry. */
15258 c = 1;
15259 amt = c * sizeof (Elf32_gptab);
9719ad41 15260 tab = bfd_malloc (amt);
b49e97c9 15261 if (tab == NULL)
0a1b45a2 15262 return false;
b49e97c9
TS
15263 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15264 tab[0].gt_header.gt_unused = 0;
15265
15266 /* Combine the input sections. */
8423293d 15267 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
15268 {
15269 asection *input_section;
15270 bfd *input_bfd;
15271 bfd_size_type size;
15272 unsigned long last;
15273 bfd_size_type gpentry;
15274
15275 if (p->type != bfd_indirect_link_order)
15276 {
15277 if (p->type == bfd_data_link_order)
15278 continue;
15279 abort ();
15280 }
15281
15282 input_section = p->u.indirect.section;
15283 input_bfd = input_section->owner;
15284
15285 /* Combine the gptab entries for this input section one
15286 by one. We know that the input gptab entries are
15287 sorted by ascending -G value. */
eea6121a 15288 size = input_section->size;
b49e97c9
TS
15289 last = 0;
15290 for (gpentry = sizeof (Elf32_External_gptab);
15291 gpentry < size;
15292 gpentry += sizeof (Elf32_External_gptab))
15293 {
15294 Elf32_External_gptab ext_gptab;
15295 Elf32_gptab int_gptab;
15296 unsigned long val;
15297 unsigned long add;
0a1b45a2 15298 bool exact;
b49e97c9
TS
15299 unsigned int look;
15300
15301 if (! (bfd_get_section_contents
9719ad41
RS
15302 (input_bfd, input_section, &ext_gptab, gpentry,
15303 sizeof (Elf32_External_gptab))))
b49e97c9
TS
15304 {
15305 free (tab);
0a1b45a2 15306 return false;
b49e97c9
TS
15307 }
15308
15309 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15310 &int_gptab);
15311 val = int_gptab.gt_entry.gt_g_value;
15312 add = int_gptab.gt_entry.gt_bytes - last;
15313
0a1b45a2 15314 exact = false;
b49e97c9
TS
15315 for (look = 1; look < c; look++)
15316 {
15317 if (tab[look].gt_entry.gt_g_value >= val)
15318 tab[look].gt_entry.gt_bytes += add;
15319
15320 if (tab[look].gt_entry.gt_g_value == val)
0a1b45a2 15321 exact = true;
b49e97c9
TS
15322 }
15323
15324 if (! exact)
15325 {
15326 Elf32_gptab *new_tab;
15327 unsigned int max;
15328
15329 /* We need a new table entry. */
15330 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 15331 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
15332 if (new_tab == NULL)
15333 {
15334 free (tab);
0a1b45a2 15335 return false;
b49e97c9
TS
15336 }
15337 tab = new_tab;
15338 tab[c].gt_entry.gt_g_value = val;
15339 tab[c].gt_entry.gt_bytes = add;
15340
15341 /* Merge in the size for the next smallest -G
15342 value, since that will be implied by this new
15343 value. */
15344 max = 0;
15345 for (look = 1; look < c; look++)
15346 {
15347 if (tab[look].gt_entry.gt_g_value < val
15348 && (max == 0
15349 || (tab[look].gt_entry.gt_g_value
15350 > tab[max].gt_entry.gt_g_value)))
15351 max = look;
15352 }
15353 if (max != 0)
15354 tab[c].gt_entry.gt_bytes +=
15355 tab[max].gt_entry.gt_bytes;
15356
15357 ++c;
15358 }
15359
15360 last = int_gptab.gt_entry.gt_bytes;
15361 }
15362
15363 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15364 elf_link_input_bfd ignores this section. */
15365 input_section->flags &= ~SEC_HAS_CONTENTS;
15366 }
15367
15368 /* The table must be sorted by -G value. */
15369 if (c > 2)
15370 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15371
15372 /* Swap out the table. */
15373 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 15374 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
15375 if (ext_tab == NULL)
15376 {
15377 free (tab);
0a1b45a2 15378 return false;
b49e97c9
TS
15379 }
15380
15381 for (j = 0; j < c; j++)
15382 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15383 free (tab);
15384
eea6121a 15385 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
15386 o->contents = (bfd_byte *) ext_tab;
15387
15388 /* Skip this section later on (I don't think this currently
15389 matters, but someday it might). */
8423293d 15390 o->map_head.link_order = NULL;
b49e97c9
TS
15391 }
15392 }
15393
15394 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 15395 if (!bfd_elf_final_link (abfd, info))
0a1b45a2 15396 return false;
b49e97c9
TS
15397
15398 /* Now write out the computed sections. */
15399
351cdf24
MF
15400 if (abiflags_sec != NULL)
15401 {
15402 Elf_External_ABIFlags_v0 ext;
15403 Elf_Internal_ABIFlags_v0 *abiflags;
15404
15405 abiflags = &mips_elf_tdata (abfd)->abiflags;
15406
15407 /* Set up the abiflags if no valid input sections were found. */
15408 if (!mips_elf_tdata (abfd)->abiflags_valid)
15409 {
15410 infer_mips_abiflags (abfd, abiflags);
0a1b45a2 15411 mips_elf_tdata (abfd)->abiflags_valid = true;
351cdf24
MF
15412 }
15413 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15414 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
0a1b45a2 15415 return false;
351cdf24
MF
15416 }
15417
9719ad41 15418 if (reginfo_sec != NULL)
b49e97c9
TS
15419 {
15420 Elf32_External_RegInfo ext;
15421
15422 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 15423 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
0a1b45a2 15424 return false;
b49e97c9
TS
15425 }
15426
9719ad41 15427 if (mdebug_sec != NULL)
b49e97c9
TS
15428 {
15429 BFD_ASSERT (abfd->output_has_begun);
15430 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15431 swap, info,
15432 mdebug_sec->filepos))
0a1b45a2 15433 return false;
b49e97c9
TS
15434
15435 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15436 }
15437
9719ad41 15438 if (gptab_data_sec != NULL)
b49e97c9
TS
15439 {
15440 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15441 gptab_data_sec->contents,
eea6121a 15442 0, gptab_data_sec->size))
0a1b45a2 15443 return false;
b49e97c9
TS
15444 }
15445
9719ad41 15446 if (gptab_bss_sec != NULL)
b49e97c9
TS
15447 {
15448 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15449 gptab_bss_sec->contents,
eea6121a 15450 0, gptab_bss_sec->size))
0a1b45a2 15451 return false;
b49e97c9
TS
15452 }
15453
15454 if (SGI_COMPAT (abfd))
15455 {
15456 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15457 if (rtproc_sec != NULL)
15458 {
15459 if (! bfd_set_section_contents (abfd, rtproc_sec,
15460 rtproc_sec->contents,
eea6121a 15461 0, rtproc_sec->size))
0a1b45a2 15462 return false;
b49e97c9
TS
15463 }
15464 }
15465
0a1b45a2 15466 return true;
b49e97c9
TS
15467}
15468\f
b2e9744f
MR
15469/* Merge object file header flags from IBFD into OBFD. Raise an error
15470 if there are conflicting settings. */
15471
0a1b45a2 15472static bool
50e03d47 15473mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
b2e9744f 15474{
50e03d47 15475 bfd *obfd = info->output_bfd;
b2e9744f
MR
15476 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15477 flagword old_flags;
15478 flagword new_flags;
0a1b45a2 15479 bool ok;
b2e9744f
MR
15480
15481 new_flags = elf_elfheader (ibfd)->e_flags;
15482 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15483 old_flags = elf_elfheader (obfd)->e_flags;
15484
15485 /* Check flag compatibility. */
15486
15487 new_flags &= ~EF_MIPS_NOREORDER;
15488 old_flags &= ~EF_MIPS_NOREORDER;
15489
15490 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15491 doesn't seem to matter. */
15492 new_flags &= ~EF_MIPS_XGOT;
15493 old_flags &= ~EF_MIPS_XGOT;
15494
15495 /* MIPSpro generates ucode info in n64 objects. Again, we should
15496 just be able to ignore this. */
15497 new_flags &= ~EF_MIPS_UCODE;
15498 old_flags &= ~EF_MIPS_UCODE;
15499
15500 /* DSOs should only be linked with CPIC code. */
15501 if ((ibfd->flags & DYNAMIC) != 0)
15502 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15503
15504 if (new_flags == old_flags)
0a1b45a2 15505 return true;
b2e9744f 15506
0a1b45a2 15507 ok = true;
b2e9744f
MR
15508
15509 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15510 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15511 {
4eca0228 15512 _bfd_error_handler
871b3ab2 15513 (_("%pB: warning: linking abicalls files with non-abicalls files"),
b2e9744f 15514 ibfd);
0a1b45a2 15515 ok = true;
b2e9744f
MR
15516 }
15517
15518 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15519 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15520 if (! (new_flags & EF_MIPS_PIC))
15521 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15522
15523 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15524 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15525
15526 /* Compare the ISAs. */
15527 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15528 {
4eca0228 15529 _bfd_error_handler
871b3ab2 15530 (_("%pB: linking 32-bit code with 64-bit code"),
b2e9744f 15531 ibfd);
0a1b45a2 15532 ok = false;
b2e9744f
MR
15533 }
15534 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15535 {
15536 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15537 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15538 {
15539 /* Copy the architecture info from IBFD to OBFD. Also copy
15540 the 32-bit flag (if set) so that we continue to recognise
15541 OBFD as a 32-bit binary. */
15542 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15543 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15544 elf_elfheader (obfd)->e_flags
15545 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15546
15547 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15548 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15549
15550 /* Copy across the ABI flags if OBFD doesn't use them
15551 and if that was what caused us to treat IBFD as 32-bit. */
15552 if ((old_flags & EF_MIPS_ABI) == 0
15553 && mips_32bit_flags_p (new_flags)
15554 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15555 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15556 }
15557 else
15558 {
15559 /* The ISAs aren't compatible. */
4eca0228 15560 _bfd_error_handler
695344c0 15561 /* xgettext:c-format */
871b3ab2 15562 (_("%pB: linking %s module with previous %s modules"),
b2e9744f
MR
15563 ibfd,
15564 bfd_printable_name (ibfd),
15565 bfd_printable_name (obfd));
0a1b45a2 15566 ok = false;
b2e9744f
MR
15567 }
15568 }
15569
15570 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15571 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15572
15573 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15574 does set EI_CLASS differently from any 32-bit ABI. */
15575 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15576 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15577 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15578 {
15579 /* Only error if both are set (to different values). */
15580 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15581 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15582 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15583 {
4eca0228 15584 _bfd_error_handler
695344c0 15585 /* xgettext:c-format */
871b3ab2 15586 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
b2e9744f
MR
15587 ibfd,
15588 elf_mips_abi_name (ibfd),
15589 elf_mips_abi_name (obfd));
0a1b45a2 15590 ok = false;
b2e9744f
MR
15591 }
15592 new_flags &= ~EF_MIPS_ABI;
15593 old_flags &= ~EF_MIPS_ABI;
15594 }
15595
15596 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15597 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15598 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15599 {
15600 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15601 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15602 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15603 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15604 int micro_mis = old_m16 && new_micro;
15605 int m16_mis = old_micro && new_m16;
15606
15607 if (m16_mis || micro_mis)
15608 {
4eca0228 15609 _bfd_error_handler
695344c0 15610 /* xgettext:c-format */
871b3ab2 15611 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
b2e9744f
MR
15612 ibfd,
15613 m16_mis ? "MIPS16" : "microMIPS",
15614 m16_mis ? "microMIPS" : "MIPS16");
0a1b45a2 15615 ok = false;
b2e9744f
MR
15616 }
15617
15618 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15619
15620 new_flags &= ~ EF_MIPS_ARCH_ASE;
15621 old_flags &= ~ EF_MIPS_ARCH_ASE;
15622 }
15623
15624 /* Compare NaN encodings. */
15625 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15626 {
695344c0 15627 /* xgettext:c-format */
871b3ab2 15628 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
b2e9744f
MR
15629 ibfd,
15630 (new_flags & EF_MIPS_NAN2008
15631 ? "-mnan=2008" : "-mnan=legacy"),
15632 (old_flags & EF_MIPS_NAN2008
15633 ? "-mnan=2008" : "-mnan=legacy"));
0a1b45a2 15634 ok = false;
b2e9744f
MR
15635 new_flags &= ~EF_MIPS_NAN2008;
15636 old_flags &= ~EF_MIPS_NAN2008;
15637 }
15638
15639 /* Compare FP64 state. */
15640 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15641 {
695344c0 15642 /* xgettext:c-format */
871b3ab2 15643 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
b2e9744f
MR
15644 ibfd,
15645 (new_flags & EF_MIPS_FP64
15646 ? "-mfp64" : "-mfp32"),
15647 (old_flags & EF_MIPS_FP64
15648 ? "-mfp64" : "-mfp32"));
0a1b45a2 15649 ok = false;
b2e9744f
MR
15650 new_flags &= ~EF_MIPS_FP64;
15651 old_flags &= ~EF_MIPS_FP64;
15652 }
15653
15654 /* Warn about any other mismatches */
15655 if (new_flags != old_flags)
15656 {
695344c0 15657 /* xgettext:c-format */
4eca0228 15658 _bfd_error_handler
871b3ab2 15659 (_("%pB: uses different e_flags (%#x) fields than previous modules "
d42c267e
AM
15660 "(%#x)"),
15661 ibfd, new_flags, old_flags);
0a1b45a2 15662 ok = false;
b2e9744f
MR
15663 }
15664
15665 return ok;
15666}
15667
2cf19d5c
JM
15668/* Merge object attributes from IBFD into OBFD. Raise an error if
15669 there are conflicting attributes. */
0a1b45a2 15670static bool
50e03d47 15671mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
2cf19d5c 15672{
50e03d47 15673 bfd *obfd = info->output_bfd;
2cf19d5c
JM
15674 obj_attribute *in_attr;
15675 obj_attribute *out_attr;
6ae68ba3 15676 bfd *abi_fp_bfd;
b60bf9be 15677 bfd *abi_msa_bfd;
6ae68ba3
MR
15678
15679 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15680 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
d929bc19 15681 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
6ae68ba3 15682 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
2cf19d5c 15683
b60bf9be
CF
15684 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15685 if (!abi_msa_bfd
15686 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15687 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15688
2cf19d5c
JM
15689 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15690 {
15691 /* This is the first object. Copy the attributes. */
15692 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15693
15694 /* Use the Tag_null value to indicate the attributes have been
15695 initialized. */
15696 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15697
0a1b45a2 15698 return true;
2cf19d5c
JM
15699 }
15700
15701 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15702 non-conflicting ones. */
2cf19d5c
JM
15703 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15704 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15705 {
757a636f 15706 int out_fp, in_fp;
6ae68ba3 15707
757a636f
RS
15708 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15709 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15710 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15711 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15712 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
351cdf24
MF
15713 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15714 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15715 || in_fp == Val_GNU_MIPS_ABI_FP_64
15716 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15717 {
15718 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15719 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15720 }
15721 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15722 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15723 || out_fp == Val_GNU_MIPS_ABI_FP_64
15724 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15725 /* Keep the current setting. */;
15726 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15727 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15728 {
15729 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15730 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15731 }
15732 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15733 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15734 /* Keep the current setting. */;
757a636f
RS
15735 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15736 {
15737 const char *out_string, *in_string;
6ae68ba3 15738
757a636f
RS
15739 out_string = _bfd_mips_fp_abi_string (out_fp);
15740 in_string = _bfd_mips_fp_abi_string (in_fp);
15741 /* First warn about cases involving unrecognised ABIs. */
15742 if (!out_string && !in_string)
695344c0 15743 /* xgettext:c-format */
757a636f 15744 _bfd_error_handler
2c1c9679 15745 (_("warning: %pB uses unknown floating point ABI %d "
871b3ab2 15746 "(set by %pB), %pB uses unknown floating point ABI %d"),
c08bb8dd 15747 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
757a636f
RS
15748 else if (!out_string)
15749 _bfd_error_handler
695344c0 15750 /* xgettext:c-format */
2c1c9679 15751 (_("warning: %pB uses unknown floating point ABI %d "
871b3ab2 15752 "(set by %pB), %pB uses %s"),
c08bb8dd 15753 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
757a636f
RS
15754 else if (!in_string)
15755 _bfd_error_handler
695344c0 15756 /* xgettext:c-format */
2c1c9679 15757 (_("warning: %pB uses %s (set by %pB), "
871b3ab2 15758 "%pB uses unknown floating point ABI %d"),
c08bb8dd 15759 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
757a636f
RS
15760 else
15761 {
15762 /* If one of the bfds is soft-float, the other must be
15763 hard-float. The exact choice of hard-float ABI isn't
15764 really relevant to the error message. */
15765 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15766 out_string = "-mhard-float";
15767 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15768 in_string = "-mhard-float";
15769 _bfd_error_handler
695344c0 15770 /* xgettext:c-format */
2c1c9679 15771 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
c08bb8dd 15772 obfd, out_string, abi_fp_bfd, ibfd, in_string);
757a636f
RS
15773 }
15774 }
2cf19d5c
JM
15775 }
15776
b60bf9be
CF
15777 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15778 non-conflicting ones. */
15779 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15780 {
15781 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15782 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15783 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15784 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15785 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15786 {
15787 case Val_GNU_MIPS_ABI_MSA_128:
15788 _bfd_error_handler
695344c0 15789 /* xgettext:c-format */
2c1c9679 15790 (_("warning: %pB uses %s (set by %pB), "
871b3ab2 15791 "%pB uses unknown MSA ABI %d"),
c08bb8dd
AM
15792 obfd, "-mmsa", abi_msa_bfd,
15793 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
b60bf9be
CF
15794 break;
15795
15796 default:
15797 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15798 {
15799 case Val_GNU_MIPS_ABI_MSA_128:
15800 _bfd_error_handler
695344c0 15801 /* xgettext:c-format */
2c1c9679 15802 (_("warning: %pB uses unknown MSA ABI %d "
871b3ab2 15803 "(set by %pB), %pB uses %s"),
c08bb8dd
AM
15804 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15805 abi_msa_bfd, ibfd, "-mmsa");
b60bf9be
CF
15806 break;
15807
15808 default:
15809 _bfd_error_handler
695344c0 15810 /* xgettext:c-format */
2c1c9679 15811 (_("warning: %pB uses unknown MSA ABI %d "
871b3ab2 15812 "(set by %pB), %pB uses unknown MSA ABI %d"),
c08bb8dd
AM
15813 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15814 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
b60bf9be
CF
15815 break;
15816 }
15817 }
15818 }
15819
2cf19d5c 15820 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 15821 return _bfd_elf_merge_object_attributes (ibfd, info);
2cf19d5c
JM
15822}
15823
a3dc0a7f
MR
15824/* Merge object ABI flags from IBFD into OBFD. Raise an error if
15825 there are conflicting settings. */
15826
0a1b45a2 15827static bool
a3dc0a7f
MR
15828mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15829{
15830 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15831 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15832 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15833
15834 /* Update the output abiflags fp_abi using the computed fp_abi. */
15835 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15836
15837#define max(a, b) ((a) > (b) ? (a) : (b))
15838 /* Merge abiflags. */
15839 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15840 in_tdata->abiflags.isa_level);
15841 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15842 in_tdata->abiflags.isa_rev);
15843 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15844 in_tdata->abiflags.gpr_size);
15845 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15846 in_tdata->abiflags.cpr1_size);
15847 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15848 in_tdata->abiflags.cpr2_size);
15849#undef max
15850 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15851 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15852
0a1b45a2 15853 return true;
a3dc0a7f
MR
15854}
15855
b49e97c9
TS
15856/* Merge backend specific data from an object file to the output
15857 object file when linking. */
15858
0a1b45a2 15859bool
50e03d47 15860_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
b49e97c9 15861{
50e03d47 15862 bfd *obfd = info->output_bfd;
cf8502c1
MR
15863 struct mips_elf_obj_tdata *out_tdata;
15864 struct mips_elf_obj_tdata *in_tdata;
0a1b45a2 15865 bool null_input_bfd = true;
b49e97c9 15866 asection *sec;
0a1b45a2 15867 bool ok;
b49e97c9 15868
58238693 15869 /* Check if we have the same endianness. */
50e03d47 15870 if (! _bfd_generic_verify_endian_match (ibfd, info))
aa701218 15871 {
4eca0228 15872 _bfd_error_handler
871b3ab2 15873 (_("%pB: endianness incompatible with that of the selected emulation"),
d003868e 15874 ibfd);
0a1b45a2 15875 return false;
aa701218 15876 }
b49e97c9 15877
d5eaccd7 15878 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
0a1b45a2 15879 return true;
b49e97c9 15880
cf8502c1
MR
15881 in_tdata = mips_elf_tdata (ibfd);
15882 out_tdata = mips_elf_tdata (obfd);
15883
aa701218
AO
15884 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15885 {
4eca0228 15886 _bfd_error_handler
871b3ab2 15887 (_("%pB: ABI is incompatible with that of the selected emulation"),
d003868e 15888 ibfd);
0a1b45a2 15889 return false;
aa701218
AO
15890 }
15891
23ba6f18
MR
15892 /* Check to see if the input BFD actually contains any sections. If not,
15893 then it has no attributes, and its flags may not have been initialized
15894 either, but it cannot actually cause any incompatibility. */
6b728d32 15895 /* FIXME: This excludes any input shared library from consideration. */
351cdf24
MF
15896 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15897 {
15898 /* Ignore synthetic sections and empty .text, .data and .bss sections
15899 which are automatically generated by gas. Also ignore fake
15900 (s)common sections, since merely defining a common symbol does
15901 not affect compatibility. */
15902 if ((sec->flags & SEC_IS_COMMON) == 0
15903 && strcmp (sec->name, ".reginfo")
15904 && strcmp (sec->name, ".mdebug")
15905 && (sec->size != 0
15906 || (strcmp (sec->name, ".text")
15907 && strcmp (sec->name, ".data")
15908 && strcmp (sec->name, ".bss"))))
15909 {
0a1b45a2 15910 null_input_bfd = false;
351cdf24
MF
15911 break;
15912 }
15913 }
15914 if (null_input_bfd)
0a1b45a2 15915 return true;
351cdf24 15916
28d45e28 15917 /* Populate abiflags using existing information. */
23ba6f18
MR
15918 if (in_tdata->abiflags_valid)
15919 {
15920 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
28d45e28
MR
15921 Elf_Internal_ABIFlags_v0 in_abiflags;
15922 Elf_Internal_ABIFlags_v0 abiflags;
15923
15924 /* Set up the FP ABI attribute from the abiflags if it is not already
07d6d2b8 15925 set. */
23ba6f18 15926 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
07d6d2b8 15927 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
23ba6f18 15928
351cdf24 15929 infer_mips_abiflags (ibfd, &abiflags);
cf8502c1 15930 in_abiflags = in_tdata->abiflags;
351cdf24
MF
15931
15932 /* It is not possible to infer the correct ISA revision
07d6d2b8 15933 for R3 or R5 so drop down to R2 for the checks. */
351cdf24
MF
15934 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15935 in_abiflags.isa_rev = 2;
15936
c97c330b
MF
15937 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15938 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
4eca0228 15939 _bfd_error_handler
2c1c9679 15940 (_("%pB: warning: inconsistent ISA between e_flags and "
351cdf24
MF
15941 ".MIPS.abiflags"), ibfd);
15942 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15943 && in_abiflags.fp_abi != abiflags.fp_abi)
4eca0228 15944 _bfd_error_handler
2c1c9679 15945 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
351cdf24
MF
15946 ".MIPS.abiflags"), ibfd);
15947 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
4eca0228 15948 _bfd_error_handler
2c1c9679 15949 (_("%pB: warning: inconsistent ASEs between e_flags and "
351cdf24 15950 ".MIPS.abiflags"), ibfd);
c97c330b
MF
15951 /* The isa_ext is allowed to be an extension of what can be inferred
15952 from e_flags. */
15953 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15954 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
4eca0228 15955 _bfd_error_handler
2c1c9679 15956 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
351cdf24
MF
15957 ".MIPS.abiflags"), ibfd);
15958 if (in_abiflags.flags2 != 0)
4eca0228 15959 _bfd_error_handler
2c1c9679 15960 (_("%pB: warning: unexpected flag in the flags2 field of "
351cdf24 15961 ".MIPS.abiflags (0x%lx)"), ibfd,
d42c267e 15962 in_abiflags.flags2);
351cdf24 15963 }
28d45e28
MR
15964 else
15965 {
15966 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
0a1b45a2 15967 in_tdata->abiflags_valid = true;
28d45e28
MR
15968 }
15969
cf8502c1 15970 if (!out_tdata->abiflags_valid)
351cdf24
MF
15971 {
15972 /* Copy input abiflags if output abiflags are not already valid. */
cf8502c1 15973 out_tdata->abiflags = in_tdata->abiflags;
0a1b45a2 15974 out_tdata->abiflags_valid = true;
351cdf24 15975 }
b49e97c9
TS
15976
15977 if (! elf_flags_init (obfd))
15978 {
0a1b45a2 15979 elf_flags_init (obfd) = true;
351cdf24 15980 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
b49e97c9
TS
15981 elf_elfheader (obfd)->e_ident[EI_CLASS]
15982 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
15983
15984 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861 15985 && (bfd_get_arch_info (obfd)->the_default
68ffbac6 15986 || mips_mach_extends_p (bfd_get_mach (obfd),
2907b861 15987 bfd_get_mach (ibfd))))
b49e97c9
TS
15988 {
15989 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
15990 bfd_get_mach (ibfd)))
0a1b45a2 15991 return false;
351cdf24
MF
15992
15993 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
cf8502c1 15994 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
b49e97c9
TS
15995 }
15996
0a1b45a2 15997 ok = true;
b49e97c9 15998 }
d537eeb5 15999 else
50e03d47 16000 ok = mips_elf_merge_obj_e_flags (ibfd, info);
d537eeb5 16001
50e03d47 16002 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
b49e97c9 16003
a3dc0a7f 16004 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
351cdf24 16005
d537eeb5 16006 if (!ok)
b49e97c9
TS
16007 {
16008 bfd_set_error (bfd_error_bad_value);
0a1b45a2 16009 return false;
b49e97c9
TS
16010 }
16011
0a1b45a2 16012 return true;
b49e97c9
TS
16013}
16014
16015/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
16016
0a1b45a2 16017bool
9719ad41 16018_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
16019{
16020 BFD_ASSERT (!elf_flags_init (abfd)
16021 || elf_elfheader (abfd)->e_flags == flags);
16022
16023 elf_elfheader (abfd)->e_flags = flags;
0a1b45a2
AM
16024 elf_flags_init (abfd) = true;
16025 return true;
b49e97c9
TS
16026}
16027
ad9563d6
CM
16028char *
16029_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
16030{
16031 switch (dtag)
16032 {
16033 default: return "";
16034 case DT_MIPS_RLD_VERSION:
16035 return "MIPS_RLD_VERSION";
16036 case DT_MIPS_TIME_STAMP:
16037 return "MIPS_TIME_STAMP";
16038 case DT_MIPS_ICHECKSUM:
16039 return "MIPS_ICHECKSUM";
16040 case DT_MIPS_IVERSION:
16041 return "MIPS_IVERSION";
16042 case DT_MIPS_FLAGS:
16043 return "MIPS_FLAGS";
16044 case DT_MIPS_BASE_ADDRESS:
16045 return "MIPS_BASE_ADDRESS";
16046 case DT_MIPS_MSYM:
16047 return "MIPS_MSYM";
16048 case DT_MIPS_CONFLICT:
16049 return "MIPS_CONFLICT";
16050 case DT_MIPS_LIBLIST:
16051 return "MIPS_LIBLIST";
16052 case DT_MIPS_LOCAL_GOTNO:
16053 return "MIPS_LOCAL_GOTNO";
16054 case DT_MIPS_CONFLICTNO:
16055 return "MIPS_CONFLICTNO";
16056 case DT_MIPS_LIBLISTNO:
16057 return "MIPS_LIBLISTNO";
16058 case DT_MIPS_SYMTABNO:
16059 return "MIPS_SYMTABNO";
16060 case DT_MIPS_UNREFEXTNO:
16061 return "MIPS_UNREFEXTNO";
16062 case DT_MIPS_GOTSYM:
16063 return "MIPS_GOTSYM";
16064 case DT_MIPS_HIPAGENO:
16065 return "MIPS_HIPAGENO";
16066 case DT_MIPS_RLD_MAP:
16067 return "MIPS_RLD_MAP";
a5499fa4
MF
16068 case DT_MIPS_RLD_MAP_REL:
16069 return "MIPS_RLD_MAP_REL";
ad9563d6
CM
16070 case DT_MIPS_DELTA_CLASS:
16071 return "MIPS_DELTA_CLASS";
16072 case DT_MIPS_DELTA_CLASS_NO:
16073 return "MIPS_DELTA_CLASS_NO";
16074 case DT_MIPS_DELTA_INSTANCE:
16075 return "MIPS_DELTA_INSTANCE";
16076 case DT_MIPS_DELTA_INSTANCE_NO:
16077 return "MIPS_DELTA_INSTANCE_NO";
16078 case DT_MIPS_DELTA_RELOC:
16079 return "MIPS_DELTA_RELOC";
16080 case DT_MIPS_DELTA_RELOC_NO:
16081 return "MIPS_DELTA_RELOC_NO";
16082 case DT_MIPS_DELTA_SYM:
16083 return "MIPS_DELTA_SYM";
16084 case DT_MIPS_DELTA_SYM_NO:
16085 return "MIPS_DELTA_SYM_NO";
16086 case DT_MIPS_DELTA_CLASSSYM:
16087 return "MIPS_DELTA_CLASSSYM";
16088 case DT_MIPS_DELTA_CLASSSYM_NO:
16089 return "MIPS_DELTA_CLASSSYM_NO";
16090 case DT_MIPS_CXX_FLAGS:
16091 return "MIPS_CXX_FLAGS";
16092 case DT_MIPS_PIXIE_INIT:
16093 return "MIPS_PIXIE_INIT";
16094 case DT_MIPS_SYMBOL_LIB:
16095 return "MIPS_SYMBOL_LIB";
16096 case DT_MIPS_LOCALPAGE_GOTIDX:
16097 return "MIPS_LOCALPAGE_GOTIDX";
16098 case DT_MIPS_LOCAL_GOTIDX:
16099 return "MIPS_LOCAL_GOTIDX";
16100 case DT_MIPS_HIDDEN_GOTIDX:
16101 return "MIPS_HIDDEN_GOTIDX";
16102 case DT_MIPS_PROTECTED_GOTIDX:
16103 return "MIPS_PROTECTED_GOT_IDX";
16104 case DT_MIPS_OPTIONS:
16105 return "MIPS_OPTIONS";
16106 case DT_MIPS_INTERFACE:
16107 return "MIPS_INTERFACE";
16108 case DT_MIPS_DYNSTR_ALIGN:
16109 return "DT_MIPS_DYNSTR_ALIGN";
16110 case DT_MIPS_INTERFACE_SIZE:
16111 return "DT_MIPS_INTERFACE_SIZE";
16112 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
16113 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
16114 case DT_MIPS_PERF_SUFFIX:
16115 return "DT_MIPS_PERF_SUFFIX";
16116 case DT_MIPS_COMPACT_SIZE:
16117 return "DT_MIPS_COMPACT_SIZE";
16118 case DT_MIPS_GP_VALUE:
16119 return "DT_MIPS_GP_VALUE";
16120 case DT_MIPS_AUX_DYNAMIC:
16121 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
16122 case DT_MIPS_PLTGOT:
16123 return "DT_MIPS_PLTGOT";
16124 case DT_MIPS_RWPLT:
16125 return "DT_MIPS_RWPLT";
f16a9783
MS
16126 case DT_MIPS_XHASH:
16127 return "DT_MIPS_XHASH";
ad9563d6
CM
16128 }
16129}
16130
757a636f
RS
16131/* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
16132 not known. */
16133
16134const char *
16135_bfd_mips_fp_abi_string (int fp)
16136{
16137 switch (fp)
16138 {
16139 /* These strings aren't translated because they're simply
16140 option lists. */
16141 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16142 return "-mdouble-float";
16143
16144 case Val_GNU_MIPS_ABI_FP_SINGLE:
16145 return "-msingle-float";
16146
16147 case Val_GNU_MIPS_ABI_FP_SOFT:
16148 return "-msoft-float";
16149
351cdf24
MF
16150 case Val_GNU_MIPS_ABI_FP_OLD_64:
16151 return _("-mips32r2 -mfp64 (12 callee-saved)");
16152
16153 case Val_GNU_MIPS_ABI_FP_XX:
16154 return "-mfpxx";
16155
757a636f 16156 case Val_GNU_MIPS_ABI_FP_64:
351cdf24
MF
16157 return "-mgp32 -mfp64";
16158
16159 case Val_GNU_MIPS_ABI_FP_64A:
16160 return "-mgp32 -mfp64 -mno-odd-spreg";
757a636f
RS
16161
16162 default:
16163 return 0;
16164 }
16165}
16166
351cdf24
MF
16167static void
16168print_mips_ases (FILE *file, unsigned int mask)
16169{
16170 if (mask & AFL_ASE_DSP)
16171 fputs ("\n\tDSP ASE", file);
16172 if (mask & AFL_ASE_DSPR2)
16173 fputs ("\n\tDSP R2 ASE", file);
8f4f9071
MF
16174 if (mask & AFL_ASE_DSPR3)
16175 fputs ("\n\tDSP R3 ASE", file);
351cdf24
MF
16176 if (mask & AFL_ASE_EVA)
16177 fputs ("\n\tEnhanced VA Scheme", file);
16178 if (mask & AFL_ASE_MCU)
16179 fputs ("\n\tMCU (MicroController) ASE", file);
16180 if (mask & AFL_ASE_MDMX)
16181 fputs ("\n\tMDMX ASE", file);
16182 if (mask & AFL_ASE_MIPS3D)
16183 fputs ("\n\tMIPS-3D ASE", file);
16184 if (mask & AFL_ASE_MT)
16185 fputs ("\n\tMT ASE", file);
16186 if (mask & AFL_ASE_SMARTMIPS)
16187 fputs ("\n\tSmartMIPS ASE", file);
16188 if (mask & AFL_ASE_VIRT)
16189 fputs ("\n\tVZ ASE", file);
16190 if (mask & AFL_ASE_MSA)
16191 fputs ("\n\tMSA ASE", file);
16192 if (mask & AFL_ASE_MIPS16)
16193 fputs ("\n\tMIPS16 ASE", file);
16194 if (mask & AFL_ASE_MICROMIPS)
16195 fputs ("\n\tMICROMIPS ASE", file);
16196 if (mask & AFL_ASE_XPA)
16197 fputs ("\n\tXPA ASE", file);
25499ac7
MR
16198 if (mask & AFL_ASE_MIPS16E2)
16199 fputs ("\n\tMIPS16e2 ASE", file);
730c3174
SE
16200 if (mask & AFL_ASE_CRC)
16201 fputs ("\n\tCRC ASE", file);
6f20c942
FS
16202 if (mask & AFL_ASE_GINV)
16203 fputs ("\n\tGINV ASE", file);
8095d2f7
CX
16204 if (mask & AFL_ASE_LOONGSON_MMI)
16205 fputs ("\n\tLoongson MMI ASE", file);
716c08de
CX
16206 if (mask & AFL_ASE_LOONGSON_CAM)
16207 fputs ("\n\tLoongson CAM ASE", file);
bdc6c06e
CX
16208 if (mask & AFL_ASE_LOONGSON_EXT)
16209 fputs ("\n\tLoongson EXT ASE", file);
a693765e
CX
16210 if (mask & AFL_ASE_LOONGSON_EXT2)
16211 fputs ("\n\tLoongson EXT2 ASE", file);
351cdf24
MF
16212 if (mask == 0)
16213 fprintf (file, "\n\t%s", _("None"));
00ac7aa0
MF
16214 else if ((mask & ~AFL_ASE_MASK) != 0)
16215 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
351cdf24
MF
16216}
16217
16218static void
16219print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16220{
16221 switch (isa_ext)
16222 {
16223 case 0:
16224 fputs (_("None"), file);
16225 break;
16226 case AFL_EXT_XLR:
16227 fputs ("RMI XLR", file);
16228 break;
2c629856
N
16229 case AFL_EXT_OCTEON3:
16230 fputs ("Cavium Networks Octeon3", file);
16231 break;
351cdf24
MF
16232 case AFL_EXT_OCTEON2:
16233 fputs ("Cavium Networks Octeon2", file);
16234 break;
16235 case AFL_EXT_OCTEONP:
16236 fputs ("Cavium Networks OcteonP", file);
16237 break;
351cdf24
MF
16238 case AFL_EXT_OCTEON:
16239 fputs ("Cavium Networks Octeon", file);
16240 break;
16241 case AFL_EXT_5900:
16242 fputs ("Toshiba R5900", file);
16243 break;
16244 case AFL_EXT_4650:
16245 fputs ("MIPS R4650", file);
16246 break;
16247 case AFL_EXT_4010:
16248 fputs ("LSI R4010", file);
16249 break;
16250 case AFL_EXT_4100:
16251 fputs ("NEC VR4100", file);
16252 break;
16253 case AFL_EXT_3900:
16254 fputs ("Toshiba R3900", file);
16255 break;
16256 case AFL_EXT_10000:
16257 fputs ("MIPS R10000", file);
16258 break;
16259 case AFL_EXT_SB1:
16260 fputs ("Broadcom SB-1", file);
16261 break;
16262 case AFL_EXT_4111:
16263 fputs ("NEC VR4111/VR4181", file);
16264 break;
16265 case AFL_EXT_4120:
16266 fputs ("NEC VR4120", file);
16267 break;
16268 case AFL_EXT_5400:
16269 fputs ("NEC VR5400", file);
16270 break;
16271 case AFL_EXT_5500:
16272 fputs ("NEC VR5500", file);
16273 break;
16274 case AFL_EXT_LOONGSON_2E:
16275 fputs ("ST Microelectronics Loongson 2E", file);
16276 break;
16277 case AFL_EXT_LOONGSON_2F:
16278 fputs ("ST Microelectronics Loongson 2F", file);
16279 break;
38bf472a
MR
16280 case AFL_EXT_INTERAPTIV_MR2:
16281 fputs ("Imagination interAptiv MR2", file);
16282 break;
351cdf24 16283 default:
00ac7aa0 16284 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
351cdf24
MF
16285 break;
16286 }
16287}
16288
16289static void
16290print_mips_fp_abi_value (FILE *file, int val)
16291{
16292 switch (val)
16293 {
16294 case Val_GNU_MIPS_ABI_FP_ANY:
16295 fprintf (file, _("Hard or soft float\n"));
16296 break;
16297 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16298 fprintf (file, _("Hard float (double precision)\n"));
16299 break;
16300 case Val_GNU_MIPS_ABI_FP_SINGLE:
16301 fprintf (file, _("Hard float (single precision)\n"));
16302 break;
16303 case Val_GNU_MIPS_ABI_FP_SOFT:
16304 fprintf (file, _("Soft float\n"));
16305 break;
16306 case Val_GNU_MIPS_ABI_FP_OLD_64:
16307 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16308 break;
16309 case Val_GNU_MIPS_ABI_FP_XX:
16310 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16311 break;
16312 case Val_GNU_MIPS_ABI_FP_64:
16313 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16314 break;
16315 case Val_GNU_MIPS_ABI_FP_64A:
16316 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16317 break;
16318 default:
16319 fprintf (file, "??? (%d)\n", val);
16320 break;
16321 }
16322}
16323
16324static int
16325get_mips_reg_size (int reg_size)
16326{
16327 return (reg_size == AFL_REG_NONE) ? 0
16328 : (reg_size == AFL_REG_32) ? 32
16329 : (reg_size == AFL_REG_64) ? 64
16330 : (reg_size == AFL_REG_128) ? 128
16331 : -1;
16332}
16333
0a1b45a2 16334bool
9719ad41 16335_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 16336{
9719ad41 16337 FILE *file = ptr;
b49e97c9
TS
16338
16339 BFD_ASSERT (abfd != NULL && ptr != NULL);
16340
16341 /* Print normal ELF private data. */
16342 _bfd_elf_print_private_bfd_data (abfd, ptr);
16343
16344 /* xgettext:c-format */
16345 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16346
16347 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16348 fprintf (file, _(" [abi=O32]"));
16349 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16350 fprintf (file, _(" [abi=O64]"));
16351 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16352 fprintf (file, _(" [abi=EABI32]"));
16353 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16354 fprintf (file, _(" [abi=EABI64]"));
16355 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16356 fprintf (file, _(" [abi unknown]"));
16357 else if (ABI_N32_P (abfd))
16358 fprintf (file, _(" [abi=N32]"));
16359 else if (ABI_64_P (abfd))
16360 fprintf (file, _(" [abi=64]"));
16361 else
16362 fprintf (file, _(" [no abi set]"));
16363
16364 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 16365 fprintf (file, " [mips1]");
b49e97c9 16366 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 16367 fprintf (file, " [mips2]");
b49e97c9 16368 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 16369 fprintf (file, " [mips3]");
b49e97c9 16370 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 16371 fprintf (file, " [mips4]");
b49e97c9 16372 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 16373 fprintf (file, " [mips5]");
b49e97c9 16374 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 16375 fprintf (file, " [mips32]");
b49e97c9 16376 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 16377 fprintf (file, " [mips64]");
af7ee8bf 16378 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 16379 fprintf (file, " [mips32r2]");
5f74bc13 16380 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 16381 fprintf (file, " [mips64r2]");
7361da2c
AB
16382 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16383 fprintf (file, " [mips32r6]");
16384 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16385 fprintf (file, " [mips64r6]");
b49e97c9
TS
16386 else
16387 fprintf (file, _(" [unknown ISA]"));
16388
40d32fc6 16389 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 16390 fprintf (file, " [mdmx]");
40d32fc6
CD
16391
16392 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 16393 fprintf (file, " [mips16]");
40d32fc6 16394
df58fc94
RS
16395 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16396 fprintf (file, " [micromips]");
16397
ba92f887
MR
16398 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16399 fprintf (file, " [nan2008]");
16400
5baf5e34 16401 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
351cdf24 16402 fprintf (file, " [old fp64]");
5baf5e34 16403
b49e97c9 16404 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 16405 fprintf (file, " [32bitmode]");
b49e97c9
TS
16406 else
16407 fprintf (file, _(" [not 32bitmode]"));
16408
c0e3f241 16409 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 16410 fprintf (file, " [noreorder]");
c0e3f241
CD
16411
16412 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 16413 fprintf (file, " [PIC]");
c0e3f241
CD
16414
16415 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 16416 fprintf (file, " [CPIC]");
c0e3f241
CD
16417
16418 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 16419 fprintf (file, " [XGOT]");
c0e3f241
CD
16420
16421 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 16422 fprintf (file, " [UCODE]");
c0e3f241 16423
b49e97c9
TS
16424 fputc ('\n', file);
16425
351cdf24
MF
16426 if (mips_elf_tdata (abfd)->abiflags_valid)
16427 {
16428 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16429 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16430 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16431 if (abiflags->isa_rev > 1)
16432 fprintf (file, "r%d", abiflags->isa_rev);
16433 fprintf (file, "\nGPR size: %d",
16434 get_mips_reg_size (abiflags->gpr_size));
16435 fprintf (file, "\nCPR1 size: %d",
16436 get_mips_reg_size (abiflags->cpr1_size));
16437 fprintf (file, "\nCPR2 size: %d",
16438 get_mips_reg_size (abiflags->cpr2_size));
16439 fputs ("\nFP ABI: ", file);
16440 print_mips_fp_abi_value (file, abiflags->fp_abi);
16441 fputs ("ISA Extension: ", file);
16442 print_mips_isa_ext (file, abiflags->isa_ext);
16443 fputs ("\nASEs:", file);
16444 print_mips_ases (file, abiflags->ases);
16445 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16446 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16447 fputc ('\n', file);
16448 }
16449
0a1b45a2 16450 return true;
b49e97c9 16451}
2f89ff8d 16452
b35d266b 16453const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 16454{
07d6d2b8
AM
16455 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16456 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
0112cd26 16457 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
07d6d2b8 16458 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
0112cd26
NC
16459 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16460 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
f16a9783 16461 { STRING_COMMA_LEN (".MIPS.xhash"), 0, SHT_MIPS_XHASH, SHF_ALLOC },
07d6d2b8 16462 { NULL, 0, 0, 0, 0 }
2f89ff8d 16463};
5e2b0d47 16464
8992f0d7
TS
16465/* Merge non visibility st_other attributes. Ensure that the
16466 STO_OPTIONAL flag is copied into h->other, even if this is not a
16467 definiton of the symbol. */
5e2b0d47
NC
16468void
16469_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5160d0f3 16470 unsigned int st_other,
0a1b45a2
AM
16471 bool definition,
16472 bool dynamic ATTRIBUTE_UNUSED)
5e2b0d47 16473{
5160d0f3 16474 if ((st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
8992f0d7
TS
16475 {
16476 unsigned char other;
16477
5160d0f3 16478 other = (definition ? st_other : h->other);
8992f0d7
TS
16479 other &= ~ELF_ST_VISIBILITY (-1);
16480 h->other = other | ELF_ST_VISIBILITY (h->other);
16481 }
16482
16483 if (!definition
5160d0f3 16484 && ELF_MIPS_IS_OPTIONAL (st_other))
5e2b0d47
NC
16485 h->other |= STO_OPTIONAL;
16486}
12ac1cf5
NC
16487
16488/* Decide whether an undefined symbol is special and can be ignored.
16489 This is the case for OPTIONAL symbols on IRIX. */
0a1b45a2 16490bool
12ac1cf5
NC
16491_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16492{
63b4cc53 16493 return ELF_MIPS_IS_OPTIONAL (h->other) != 0;
12ac1cf5 16494}
e0764319 16495
0a1b45a2 16496bool
e0764319
NC
16497_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16498{
16499 return (sym->st_shndx == SHN_COMMON
16500 || sym->st_shndx == SHN_MIPS_ACOMMON
16501 || sym->st_shndx == SHN_MIPS_SCOMMON);
16502}
861fb55a
DJ
16503
16504/* Return address for Ith PLT stub in section PLT, for relocation REL
16505 or (bfd_vma) -1 if it should not be included. */
16506
16507bfd_vma
16508_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16509 const arelent *rel ATTRIBUTE_UNUSED)
16510{
16511 return (plt->vma
16512 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16513 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16514}
16515
1bbce132
MR
16516/* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16517 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16518 and .got.plt and also the slots may be of a different size each we walk
16519 the PLT manually fetching instructions and matching them against known
16520 patterns. To make things easier standard MIPS slots, if any, always come
16521 first. As we don't create proper ELF symbols we use the UDATA.I member
16522 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16523 with the ST_OTHER member of the ELF symbol. */
16524
16525long
16526_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16527 long symcount ATTRIBUTE_UNUSED,
16528 asymbol **syms ATTRIBUTE_UNUSED,
16529 long dynsymcount, asymbol **dynsyms,
16530 asymbol **ret)
16531{
16532 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16533 static const char microsuffix[] = "@micromipsplt";
16534 static const char m16suffix[] = "@mips16plt";
16535 static const char mipssuffix[] = "@plt";
16536
0a1b45a2 16537 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
1bbce132 16538 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0a1b45a2 16539 bool micromips_p = MICROMIPS_P (abfd);
1bbce132
MR
16540 Elf_Internal_Shdr *hdr;
16541 bfd_byte *plt_data;
16542 bfd_vma plt_offset;
16543 unsigned int other;
16544 bfd_vma entry_size;
16545 bfd_vma plt0_size;
16546 asection *relplt;
16547 bfd_vma opcode;
16548 asection *plt;
16549 asymbol *send;
16550 size_t size;
16551 char *names;
16552 long counti;
16553 arelent *p;
16554 asymbol *s;
16555 char *nend;
16556 long count;
16557 long pi;
16558 long i;
16559 long n;
16560
16561 *ret = NULL;
16562
16563 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16564 return 0;
16565
16566 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16567 if (relplt == NULL)
16568 return 0;
16569
16570 hdr = &elf_section_data (relplt)->this_hdr;
16571 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16572 return 0;
16573
16574 plt = bfd_get_section_by_name (abfd, ".plt");
16575 if (plt == NULL)
16576 return 0;
16577
16578 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
0a1b45a2 16579 if (!(*slurp_relocs) (abfd, relplt, dynsyms, true))
1bbce132
MR
16580 return -1;
16581 p = relplt->relocation;
16582
16583 /* Calculating the exact amount of space required for symbols would
16584 require two passes over the PLT, so just pessimise assuming two
16585 PLT slots per relocation. */
16586 count = relplt->size / hdr->sh_entsize;
16587 counti = count * bed->s->int_rels_per_ext_rel;
16588 size = 2 * count * sizeof (asymbol);
16589 size += count * (sizeof (mipssuffix) +
16590 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16591 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16592 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16593
16594 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16595 size += sizeof (asymbol) + sizeof (pltname);
16596
16597 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16598 return -1;
16599
16600 if (plt->size < 16)
16601 return -1;
16602
16603 s = *ret = bfd_malloc (size);
16604 if (s == NULL)
16605 return -1;
16606 send = s + 2 * count + 1;
16607
16608 names = (char *) send;
16609 nend = (char *) s + size;
16610 n = 0;
16611
16612 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16613 if (opcode == 0x3302fffe)
16614 {
16615 if (!micromips_p)
16616 return -1;
16617 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16618 other = STO_MICROMIPS;
16619 }
833794fc
MR
16620 else if (opcode == 0x0398c1d0)
16621 {
16622 if (!micromips_p)
16623 return -1;
16624 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16625 other = STO_MICROMIPS;
16626 }
1bbce132
MR
16627 else
16628 {
16629 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16630 other = 0;
16631 }
16632
16633 s->the_bfd = abfd;
16634 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16635 s->section = plt;
16636 s->value = 0;
16637 s->name = names;
16638 s->udata.i = other;
16639 memcpy (names, pltname, sizeof (pltname));
16640 names += sizeof (pltname);
16641 ++s, ++n;
16642
16643 pi = 0;
16644 for (plt_offset = plt0_size;
16645 plt_offset + 8 <= plt->size && s < send;
16646 plt_offset += entry_size)
16647 {
16648 bfd_vma gotplt_addr;
16649 const char *suffix;
16650 bfd_vma gotplt_hi;
16651 bfd_vma gotplt_lo;
16652 size_t suffixlen;
16653
16654 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16655
16656 /* Check if the second word matches the expected MIPS16 instruction. */
16657 if (opcode == 0x651aeb00)
16658 {
16659 if (micromips_p)
16660 return -1;
16661 /* Truncated table??? */
16662 if (plt_offset + 16 > plt->size)
16663 break;
16664 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16665 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16666 suffixlen = sizeof (m16suffix);
16667 suffix = m16suffix;
16668 other = STO_MIPS16;
16669 }
833794fc 16670 /* Likewise the expected microMIPS instruction (no insn32 mode). */
1bbce132
MR
16671 else if (opcode == 0xff220000)
16672 {
16673 if (!micromips_p)
16674 return -1;
16675 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16676 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16677 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16678 gotplt_lo <<= 2;
16679 gotplt_addr = gotplt_hi + gotplt_lo;
16680 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16681 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16682 suffixlen = sizeof (microsuffix);
16683 suffix = microsuffix;
16684 other = STO_MICROMIPS;
16685 }
833794fc
MR
16686 /* Likewise the expected microMIPS instruction (insn32 mode). */
16687 else if ((opcode & 0xffff0000) == 0xff2f0000)
16688 {
16689 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16690 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16691 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16692 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16693 gotplt_addr = gotplt_hi + gotplt_lo;
16694 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16695 suffixlen = sizeof (microsuffix);
16696 suffix = microsuffix;
16697 other = STO_MICROMIPS;
16698 }
1bbce132
MR
16699 /* Otherwise assume standard MIPS code. */
16700 else
16701 {
16702 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16703 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16704 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16705 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16706 gotplt_addr = gotplt_hi + gotplt_lo;
16707 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16708 suffixlen = sizeof (mipssuffix);
16709 suffix = mipssuffix;
16710 other = 0;
16711 }
16712 /* Truncated table??? */
16713 if (plt_offset + entry_size > plt->size)
16714 break;
16715
16716 for (i = 0;
16717 i < count && p[pi].address != gotplt_addr;
16718 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16719
16720 if (i < count)
16721 {
16722 size_t namelen;
16723 size_t len;
16724
16725 *s = **p[pi].sym_ptr_ptr;
16726 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16727 we are defining a symbol, ensure one of them is set. */
16728 if ((s->flags & BSF_LOCAL) == 0)
16729 s->flags |= BSF_GLOBAL;
16730 s->flags |= BSF_SYNTHETIC;
16731 s->section = plt;
16732 s->value = plt_offset;
16733 s->name = names;
16734 s->udata.i = other;
16735
16736 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16737 namelen = len + suffixlen;
16738 if (names + namelen > nend)
16739 break;
16740
16741 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16742 names += len;
16743 memcpy (names, suffix, suffixlen);
16744 names += suffixlen;
16745
16746 ++s, ++n;
16747 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16748 }
16749 }
16750
16751 free (plt_data);
16752
16753 return n;
16754}
16755
5e7fc731
MR
16756/* Return the ABI flags associated with ABFD if available. */
16757
16758Elf_Internal_ABIFlags_v0 *
16759bfd_mips_elf_get_abiflags (bfd *abfd)
16760{
16761 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16762
16763 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16764}
16765
bb29b84d
MR
16766/* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16767 field. Taken from `libc-abis.h' generated at GNU libc build time.
16768 Using a MIPS_ prefix as other libc targets use different values. */
16769enum
16770{
16771 MIPS_LIBC_ABI_DEFAULT = 0,
16772 MIPS_LIBC_ABI_MIPS_PLT,
16773 MIPS_LIBC_ABI_UNIQUE,
16774 MIPS_LIBC_ABI_MIPS_O32_FP64,
47275900 16775 MIPS_LIBC_ABI_ABSOLUTE,
f16a9783 16776 MIPS_LIBC_ABI_XHASH,
bb29b84d
MR
16777 MIPS_LIBC_ABI_MAX
16778};
16779
0a1b45a2 16780bool
ed7e9d0b 16781_bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
861fb55a 16782{
47275900 16783 struct mips_elf_link_hash_table *htab = NULL;
861fb55a
DJ
16784 Elf_Internal_Ehdr *i_ehdrp;
16785
ed7e9d0b 16786 if (!_bfd_elf_init_file_header (abfd, link_info))
0a1b45a2 16787 return false;
ed7e9d0b 16788
861fb55a
DJ
16789 i_ehdrp = elf_elfheader (abfd);
16790 if (link_info)
16791 {
16792 htab = mips_elf_hash_table (link_info);
4dfe6ac6 16793 BFD_ASSERT (htab != NULL);
861fb55a 16794 }
0af03126 16795
90c14f0c
L
16796 if (htab != NULL
16797 && htab->use_plts_and_copy_relocs
16798 && htab->root.target_os != is_vxworks)
47275900
MR
16799 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16800
351cdf24
MF
16801 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16802 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
bb29b84d 16803 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
334cd8a7 16804
47275900
MR
16805 /* Mark that we need support for absolute symbols in the dynamic loader. */
16806 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16807 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16808
f16a9783
MS
16809 /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16810 if it is the only hash section that will be created. */
16811 if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16812 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
0a1b45a2 16813 return true;
861fb55a 16814}
2f0c68f2
CM
16815
16816int
1ced1a5f
MR
16817_bfd_mips_elf_compact_eh_encoding
16818 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
2f0c68f2
CM
16819{
16820 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16821}
16822
16823/* Return the opcode for can't unwind. */
16824
16825int
1ced1a5f
MR
16826_bfd_mips_elf_cant_unwind_opcode
16827 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
2f0c68f2
CM
16828{
16829 return COMPACT_EH_CANT_UNWIND_OPCODE;
16830}
f16a9783
MS
16831
16832/* Record a position XLAT_LOC in the xlat translation table, associated with
16833 the hash entry H. The entry in the translation table will later be
16834 populated with the real symbol dynindx. */
16835
16836void
16837_bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16838 bfd_vma xlat_loc)
16839{
16840 struct mips_elf_link_hash_entry *hmips;
16841
16842 hmips = (struct mips_elf_link_hash_entry *) h;
16843 hmips->mipsxhash_loc = xlat_loc;
16844}