]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elfxx-mips.c
Wrap xfer_partial and enable_btrace for Ravenscar
[thirdparty/binutils-gdb.git] / bfd / elfxx-mips.c
CommitLineData
b49e97c9 1/* MIPS-specific support for ELF
b3adc24a 2 Copyright (C) 1993-2020 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. */
b34976b6 445 bfd_boolean 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. */
b34976b6 451 bfd_boolean mips16_stubs_seen;
861fb55a
DJ
452
453 /* True if we can generate copy relocs and PLTs. */
454 bfd_boolean use_plts_and_copy_relocs;
455
833794fc
MR
456 /* True if we can only use 32-bit microMIPS instructions. */
457 bfd_boolean insn32;
458
8b10b0b3
MR
459 /* True if we suppress checks for invalid branches between ISA modes. */
460 bfd_boolean ignore_branch_isa;
461
3734320d
MF
462 /* True if we are targetting R6 compact branches. */
463 bfd_boolean compact_branches;
464
0e53d9da
AN
465 /* True if we already reported the small-data section overflow. */
466 bfd_boolean small_data_overflow_reported;
861fb55a 467
47275900
MR
468 /* True if we use the special `__gnu_absolute_zero' symbol. */
469 bfd_boolean use_absolute_zero;
470
471 /* True if we have been configured for a GNU target. */
472 bfd_boolean gnu_target;
473
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) \
537 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
538 == MIPS_ELF_DATA ? ((struct mips_elf_link_hash_table *) ((p)->hash)) : NULL)
539
861fb55a 540/* A structure used to communicate with htab_traverse callbacks. */
4dfe6ac6
NC
541struct mips_htab_traverse_info
542{
861fb55a
DJ
543 /* The usual link-wide information. */
544 struct bfd_link_info *info;
545 bfd *output_bfd;
546
547 /* Starts off FALSE and is set to TRUE if the link should be aborted. */
548 bfd_boolean error;
b49e97c9
TS
549};
550
6ae68ba3
MR
551/* MIPS ELF private object data. */
552
553struct mips_elf_obj_tdata
554{
555 /* Generic ELF private object data. */
556 struct elf_obj_tdata root;
557
558 /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output. */
559 bfd *abi_fp_bfd;
ee227692 560
b60bf9be
CF
561 /* Input BFD providing Tag_GNU_MIPS_ABI_MSA attribute for output. */
562 bfd *abi_msa_bfd;
563
351cdf24
MF
564 /* The abiflags for this object. */
565 Elf_Internal_ABIFlags_v0 abiflags;
566 bfd_boolean abiflags_valid;
567
ee227692
RS
568 /* The GOT requirements of input bfds. */
569 struct mips_got_info *got;
698600e4
AM
570
571 /* Used by _bfd_mips_elf_find_nearest_line. The structure could be
572 included directly in this one, but there's no point to wasting
573 the memory just for the infrequently called find_nearest_line. */
574 struct mips_elf_find_line *find_line_info;
575
576 /* An array of stub sections indexed by symbol number. */
577 asection **local_stubs;
578 asection **local_call_stubs;
579
580 /* The Irix 5 support uses two virtual sections, which represent
581 text/data symbols defined in dynamic objects. */
582 asymbol *elf_data_symbol;
583 asymbol *elf_text_symbol;
584 asection *elf_data_section;
585 asection *elf_text_section;
6ae68ba3
MR
586};
587
588/* Get MIPS ELF private object data from BFD's tdata. */
589
590#define mips_elf_tdata(bfd) \
591 ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
592
0f20cc35
DJ
593#define TLS_RELOC_P(r_type) \
594 (r_type == R_MIPS_TLS_DTPMOD32 \
595 || r_type == R_MIPS_TLS_DTPMOD64 \
596 || r_type == R_MIPS_TLS_DTPREL32 \
597 || r_type == R_MIPS_TLS_DTPREL64 \
598 || r_type == R_MIPS_TLS_GD \
599 || r_type == R_MIPS_TLS_LDM \
600 || r_type == R_MIPS_TLS_DTPREL_HI16 \
601 || r_type == R_MIPS_TLS_DTPREL_LO16 \
602 || r_type == R_MIPS_TLS_GOTTPREL \
603 || r_type == R_MIPS_TLS_TPREL32 \
604 || r_type == R_MIPS_TLS_TPREL64 \
605 || r_type == R_MIPS_TLS_TPREL_HI16 \
df58fc94 606 || r_type == R_MIPS_TLS_TPREL_LO16 \
d0f13682
CLT
607 || r_type == R_MIPS16_TLS_GD \
608 || r_type == R_MIPS16_TLS_LDM \
609 || r_type == R_MIPS16_TLS_DTPREL_HI16 \
610 || r_type == R_MIPS16_TLS_DTPREL_LO16 \
611 || r_type == R_MIPS16_TLS_GOTTPREL \
612 || r_type == R_MIPS16_TLS_TPREL_HI16 \
613 || r_type == R_MIPS16_TLS_TPREL_LO16 \
df58fc94
RS
614 || r_type == R_MICROMIPS_TLS_GD \
615 || r_type == R_MICROMIPS_TLS_LDM \
616 || r_type == R_MICROMIPS_TLS_DTPREL_HI16 \
617 || r_type == R_MICROMIPS_TLS_DTPREL_LO16 \
618 || r_type == R_MICROMIPS_TLS_GOTTPREL \
619 || r_type == R_MICROMIPS_TLS_TPREL_HI16 \
620 || r_type == R_MICROMIPS_TLS_TPREL_LO16)
0f20cc35 621
b49e97c9
TS
622/* Structure used to pass information to mips_elf_output_extsym. */
623
624struct extsym_info
625{
9e4aeb93
RS
626 bfd *abfd;
627 struct bfd_link_info *info;
b49e97c9
TS
628 struct ecoff_debug_info *debug;
629 const struct ecoff_debug_swap *swap;
b34976b6 630 bfd_boolean failed;
b49e97c9
TS
631};
632
8dc1a139 633/* The names of the runtime procedure table symbols used on IRIX5. */
b49e97c9
TS
634
635static const char * const mips_elf_dynsym_rtproc_names[] =
636{
637 "_procedure_table",
638 "_procedure_string_table",
639 "_procedure_table_size",
640 NULL
641};
642
643/* These structures are used to generate the .compact_rel section on
8dc1a139 644 IRIX5. */
b49e97c9
TS
645
646typedef struct
647{
648 unsigned long id1; /* Always one? */
649 unsigned long num; /* Number of compact relocation entries. */
650 unsigned long id2; /* Always two? */
651 unsigned long offset; /* The file offset of the first relocation. */
652 unsigned long reserved0; /* Zero? */
653 unsigned long reserved1; /* Zero? */
654} Elf32_compact_rel;
655
656typedef struct
657{
658 bfd_byte id1[4];
659 bfd_byte num[4];
660 bfd_byte id2[4];
661 bfd_byte offset[4];
662 bfd_byte reserved0[4];
663 bfd_byte reserved1[4];
664} Elf32_External_compact_rel;
665
666typedef struct
667{
668 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
669 unsigned int rtype : 4; /* Relocation types. See below. */
670 unsigned int dist2to : 8;
671 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
672 unsigned long konst; /* KONST field. See below. */
673 unsigned long vaddr; /* VADDR to be relocated. */
674} Elf32_crinfo;
675
676typedef struct
677{
678 unsigned int ctype : 1; /* 1: long 0: short format. See below. */
679 unsigned int rtype : 4; /* Relocation types. See below. */
680 unsigned int dist2to : 8;
681 unsigned int relvaddr : 19; /* (VADDR - vaddr of the previous entry)/ 4 */
682 unsigned long konst; /* KONST field. See below. */
683} Elf32_crinfo2;
684
685typedef struct
686{
687 bfd_byte info[4];
688 bfd_byte konst[4];
689 bfd_byte vaddr[4];
690} Elf32_External_crinfo;
691
692typedef struct
693{
694 bfd_byte info[4];
695 bfd_byte konst[4];
696} Elf32_External_crinfo2;
697
698/* These are the constants used to swap the bitfields in a crinfo. */
699
700#define CRINFO_CTYPE (0x1)
701#define CRINFO_CTYPE_SH (31)
702#define CRINFO_RTYPE (0xf)
703#define CRINFO_RTYPE_SH (27)
704#define CRINFO_DIST2TO (0xff)
705#define CRINFO_DIST2TO_SH (19)
706#define CRINFO_RELVADDR (0x7ffff)
707#define CRINFO_RELVADDR_SH (0)
708
709/* A compact relocation info has long (3 words) or short (2 words)
710 formats. A short format doesn't have VADDR field and relvaddr
711 fields contains ((VADDR - vaddr of the previous entry) >> 2). */
712#define CRF_MIPS_LONG 1
713#define CRF_MIPS_SHORT 0
714
715/* There are 4 types of compact relocation at least. The value KONST
716 has different meaning for each type:
717
718 (type) (konst)
719 CT_MIPS_REL32 Address in data
720 CT_MIPS_WORD Address in word (XXX)
721 CT_MIPS_GPHI_LO GP - vaddr
722 CT_MIPS_JMPAD Address to jump
723 */
724
725#define CRT_MIPS_REL32 0xa
726#define CRT_MIPS_WORD 0xb
727#define CRT_MIPS_GPHI_LO 0xc
728#define CRT_MIPS_JMPAD 0xd
729
730#define mips_elf_set_cr_format(x,format) ((x).ctype = (format))
731#define mips_elf_set_cr_type(x,type) ((x).rtype = (type))
732#define mips_elf_set_cr_dist2to(x,v) ((x).dist2to = (v))
733#define mips_elf_set_cr_relvaddr(x,d) ((x).relvaddr = (d)<<2)
734\f
735/* The structure of the runtime procedure descriptor created by the
736 loader for use by the static exception system. */
737
738typedef struct runtime_pdr {
ae9a127f
NC
739 bfd_vma adr; /* Memory address of start of procedure. */
740 long regmask; /* Save register mask. */
741 long regoffset; /* Save register offset. */
742 long fregmask; /* Save floating point register mask. */
743 long fregoffset; /* Save floating point register offset. */
744 long frameoffset; /* Frame size. */
745 short framereg; /* Frame pointer register. */
746 short pcreg; /* Offset or reg of return pc. */
747 long irpss; /* Index into the runtime string table. */
b49e97c9 748 long reserved;
ae9a127f 749 struct exception_info *exception_info;/* Pointer to exception array. */
b49e97c9
TS
750} RPDR, *pRPDR;
751#define cbRPDR sizeof (RPDR)
752#define rpdNil ((pRPDR) 0)
753\f
b15e6682 754static struct mips_got_entry *mips_elf_create_local_got_entry
a8028dd0
RS
755 (bfd *, struct bfd_link_info *, bfd *, bfd_vma, unsigned long,
756 struct mips_elf_link_hash_entry *, int);
b34976b6 757static bfd_boolean mips_elf_sort_hash_table_f
9719ad41 758 (struct mips_elf_link_hash_entry *, void *);
9719ad41
RS
759static bfd_vma mips_elf_high
760 (bfd_vma);
b34976b6 761static bfd_boolean mips_elf_create_dynamic_relocation
9719ad41
RS
762 (bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
763 struct mips_elf_link_hash_entry *, asection *, bfd_vma,
764 bfd_vma *, asection *);
f4416af6 765static bfd_vma mips_elf_adjust_gp
9719ad41 766 (bfd *, struct mips_got_info *, bfd *);
f4416af6 767
b49e97c9
TS
768/* This will be used when we sort the dynamic relocation records. */
769static bfd *reldyn_sorting_bfd;
770
6d30f5b2
NC
771/* True if ABFD is for CPUs with load interlocking that include
772 non-MIPS1 CPUs and R3900. */
773#define LOAD_INTERLOCKS_P(abfd) \
774 ( ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) != E_MIPS_ARCH_1) \
775 || ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_3900))
776
cd8d5a82
CF
777/* True if ABFD is for CPUs that are faster if JAL is converted to BAL.
778 This should be safe for all architectures. We enable this predicate
779 for RM9000 for now. */
780#define JAL_TO_BAL_P(abfd) \
781 ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == E_MIPS_MACH_9000)
782
783/* True if ABFD is for CPUs that are faster if JALR is converted to BAL.
784 This should be safe for all architectures. We enable this predicate for
785 all CPUs. */
786#define JALR_TO_BAL_P(abfd) 1
787
38a7df63
CF
788/* True if ABFD is for CPUs that are faster if JR is converted to B.
789 This should be safe for all architectures. We enable this predicate for
790 all CPUs. */
791#define JR_TO_B_P(abfd) 1
792
861fb55a
DJ
793/* True if ABFD is a PIC object. */
794#define PIC_OBJECT_P(abfd) \
795 ((elf_elfheader (abfd)->e_flags & EF_MIPS_PIC) != 0)
796
351cdf24
MF
797/* Nonzero if ABFD is using the O32 ABI. */
798#define ABI_O32_P(abfd) \
799 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
800
b49e97c9 801/* Nonzero if ABFD is using the N32 ABI. */
b49e97c9
TS
802#define ABI_N32_P(abfd) \
803 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI2) != 0)
804
4a14403c 805/* Nonzero if ABFD is using the N64 ABI. */
b49e97c9 806#define ABI_64_P(abfd) \
141ff970 807 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
b49e97c9 808
4a14403c
TS
809/* Nonzero if ABFD is using NewABI conventions. */
810#define NEWABI_P(abfd) (ABI_N32_P (abfd) || ABI_64_P (abfd))
811
e8faf7d1
MR
812/* Nonzero if ABFD has microMIPS code. */
813#define MICROMIPS_P(abfd) \
814 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS) != 0)
815
7361da2c
AB
816/* Nonzero if ABFD is MIPS R6. */
817#define MIPSR6_P(abfd) \
818 ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6 \
819 || (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
820
4a14403c 821/* The IRIX compatibility level we are striving for. */
b49e97c9
TS
822#define IRIX_COMPAT(abfd) \
823 (get_elf_backend_data (abfd)->elf_backend_mips_irix_compat (abfd))
824
b49e97c9
TS
825/* Whether we are trying to be compatible with IRIX at all. */
826#define SGI_COMPAT(abfd) \
827 (IRIX_COMPAT (abfd) != ict_none)
828
829/* The name of the options section. */
830#define MIPS_ELF_OPTIONS_SECTION_NAME(abfd) \
d80dcc6a 831 (NEWABI_P (abfd) ? ".MIPS.options" : ".options")
b49e97c9 832
cc2e31b9
RS
833/* True if NAME is the recognized name of any SHT_MIPS_OPTIONS section.
834 Some IRIX system files do not use MIPS_ELF_OPTIONS_SECTION_NAME. */
835#define MIPS_ELF_OPTIONS_SECTION_NAME_P(NAME) \
836 (strcmp (NAME, ".MIPS.options") == 0 || strcmp (NAME, ".options") == 0)
837
351cdf24
MF
838/* True if NAME is the recognized name of any SHT_MIPS_ABIFLAGS section. */
839#define MIPS_ELF_ABIFLAGS_SECTION_NAME_P(NAME) \
840 (strcmp (NAME, ".MIPS.abiflags") == 0)
841
943284cc
DJ
842/* Whether the section is readonly. */
843#define MIPS_ELF_READONLY_SECTION(sec) \
844 ((sec->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY)) \
845 == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
846
b49e97c9 847/* The name of the stub section. */
ca07892d 848#define MIPS_ELF_STUB_SECTION_NAME(abfd) ".MIPS.stubs"
b49e97c9
TS
849
850/* The size of an external REL relocation. */
851#define MIPS_ELF_REL_SIZE(abfd) \
852 (get_elf_backend_data (abfd)->s->sizeof_rel)
853
0a44bf69
RS
854/* The size of an external RELA relocation. */
855#define MIPS_ELF_RELA_SIZE(abfd) \
856 (get_elf_backend_data (abfd)->s->sizeof_rela)
857
b49e97c9
TS
858/* The size of an external dynamic table entry. */
859#define MIPS_ELF_DYN_SIZE(abfd) \
860 (get_elf_backend_data (abfd)->s->sizeof_dyn)
861
862/* The size of a GOT entry. */
863#define MIPS_ELF_GOT_SIZE(abfd) \
864 (get_elf_backend_data (abfd)->s->arch_size / 8)
865
b4082c70
DD
866/* The size of the .rld_map section. */
867#define MIPS_ELF_RLD_MAP_SIZE(abfd) \
868 (get_elf_backend_data (abfd)->s->arch_size / 8)
869
b49e97c9
TS
870/* The size of a symbol-table entry. */
871#define MIPS_ELF_SYM_SIZE(abfd) \
872 (get_elf_backend_data (abfd)->s->sizeof_sym)
873
874/* The default alignment for sections, as a power of two. */
875#define MIPS_ELF_LOG_FILE_ALIGN(abfd) \
45d6a902 876 (get_elf_backend_data (abfd)->s->log_file_align)
b49e97c9
TS
877
878/* Get word-sized data. */
879#define MIPS_ELF_GET_WORD(abfd, ptr) \
880 (ABI_64_P (abfd) ? bfd_get_64 (abfd, ptr) : bfd_get_32 (abfd, ptr))
881
882/* Put out word-sized data. */
883#define MIPS_ELF_PUT_WORD(abfd, val, ptr) \
07d6d2b8
AM
884 (ABI_64_P (abfd) \
885 ? bfd_put_64 (abfd, val, ptr) \
b49e97c9
TS
886 : bfd_put_32 (abfd, val, ptr))
887
861fb55a
DJ
888/* The opcode for word-sized loads (LW or LD). */
889#define MIPS_ELF_LOAD_WORD(abfd) \
890 (ABI_64_P (abfd) ? 0xdc000000 : 0x8c000000)
891
b49e97c9 892/* Add a dynamic symbol table-entry. */
9719ad41 893#define MIPS_ELF_ADD_DYNAMIC_ENTRY(info, tag, val) \
5a580b3a 894 _bfd_elf_add_dynamic_entry (info, tag, val)
b49e97c9
TS
895
896#define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela) \
0aa13fee 897 (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (abfd, rtype, rela))
b49e97c9 898
0a44bf69
RS
899/* The name of the dynamic relocation section. */
900#define MIPS_ELF_REL_DYN_NAME(INFO) \
90c14f0c
L
901 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
902 ? ".rela.dyn" : ".rel.dyn")
0a44bf69 903
b49e97c9
TS
904/* In case we're on a 32-bit machine, construct a 64-bit "-1" value
905 from smaller values. Start with zero, widen, *then* decrement. */
906#define MINUS_ONE (((bfd_vma)0) - 1)
c5ae1840 907#define MINUS_TWO (((bfd_vma)0) - 2)
b49e97c9 908
51e38d68
RS
909/* The value to write into got[1] for SVR4 targets, to identify it is
910 a GNU object. The dynamic linker can then use got[1] to store the
911 module pointer. */
912#define MIPS_ELF_GNU_GOT1_MASK(abfd) \
913 ((bfd_vma) 1 << (ABI_64_P (abfd) ? 63 : 31))
914
f4416af6 915/* The offset of $gp from the beginning of the .got section. */
0a44bf69 916#define ELF_MIPS_GP_OFFSET(INFO) \
90c14f0c
L
917 (mips_elf_hash_table (INFO)->root.target_os == is_vxworks \
918 ? 0x0 : 0x7ff0)
f4416af6
AO
919
920/* The maximum size of the GOT for it to be addressable using 16-bit
921 offsets from $gp. */
0a44bf69 922#define MIPS_ELF_GOT_MAX_SIZE(INFO) (ELF_MIPS_GP_OFFSET (INFO) + 0x7fff)
f4416af6 923
6a691779 924/* Instructions which appear in a stub. */
3d6746ca
DD
925#define STUB_LW(abfd) \
926 ((ABI_64_P (abfd) \
927 ? 0xdf998010 /* ld t9,0x8010(gp) */ \
07d6d2b8 928 : 0x8f998010)) /* lw t9,0x8010(gp) */
40fc1451 929#define STUB_MOVE 0x03e07825 /* or t7,ra,zero */
3d6746ca 930#define STUB_LUI(VAL) (0x3c180000 + (VAL)) /* lui t8,VAL */
a18a2a34 931#define STUB_JALR 0x0320f809 /* jalr ra,t9 */
3734320d 932#define STUB_JALRC 0xf8190000 /* jalrc ra,t9 */
5108fc1b
RS
933#define STUB_ORI(VAL) (0x37180000 + (VAL)) /* ori t8,t8,VAL */
934#define STUB_LI16U(VAL) (0x34180000 + (VAL)) /* ori t8,zero,VAL unsigned */
3d6746ca
DD
935#define STUB_LI16S(abfd, VAL) \
936 ((ABI_64_P (abfd) \
937 ? (0x64180000 + (VAL)) /* daddiu t8,zero,VAL sign extended */ \
938 : (0x24180000 + (VAL)))) /* addiu t8,zero,VAL sign extended */
939
1bbce132
MR
940/* Likewise for the microMIPS ASE. */
941#define STUB_LW_MICROMIPS(abfd) \
942 (ABI_64_P (abfd) \
943 ? 0xdf3c8010 /* ld t9,0x8010(gp) */ \
944 : 0xff3c8010) /* lw t9,0x8010(gp) */
945#define STUB_MOVE_MICROMIPS 0x0dff /* move t7,ra */
40fc1451 946#define STUB_MOVE32_MICROMIPS 0x001f7a90 /* or t7,ra,zero */
1bbce132
MR
947#define STUB_LUI_MICROMIPS(VAL) \
948 (0x41b80000 + (VAL)) /* lui t8,VAL */
949#define STUB_JALR_MICROMIPS 0x45d9 /* jalr t9 */
833794fc 950#define STUB_JALR32_MICROMIPS 0x03f90f3c /* jalr ra,t9 */
1bbce132
MR
951#define STUB_ORI_MICROMIPS(VAL) \
952 (0x53180000 + (VAL)) /* ori t8,t8,VAL */
953#define STUB_LI16U_MICROMIPS(VAL) \
954 (0x53000000 + (VAL)) /* ori t8,zero,VAL unsigned */
955#define STUB_LI16S_MICROMIPS(abfd, VAL) \
956 (ABI_64_P (abfd) \
957 ? 0x5f000000 + (VAL) /* daddiu t8,zero,VAL sign extended */ \
958 : 0x33000000 + (VAL)) /* addiu t8,zero,VAL sign extended */
959
5108fc1b
RS
960#define MIPS_FUNCTION_STUB_NORMAL_SIZE 16
961#define MIPS_FUNCTION_STUB_BIG_SIZE 20
1bbce132
MR
962#define MICROMIPS_FUNCTION_STUB_NORMAL_SIZE 12
963#define MICROMIPS_FUNCTION_STUB_BIG_SIZE 16
833794fc
MR
964#define MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE 16
965#define MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE 20
b49e97c9
TS
966
967/* The name of the dynamic interpreter. This is put in the .interp
968 section. */
969
07d6d2b8
AM
970#define ELF_DYNAMIC_INTERPRETER(abfd) \
971 (ABI_N32_P (abfd) ? "/usr/lib32/libc.so.1" \
972 : ABI_64_P (abfd) ? "/usr/lib64/libc.so.1" \
b49e97c9
TS
973 : "/usr/lib/libc.so.1")
974
975#ifdef BFD64
ee6423ed
AO
976#define MNAME(bfd,pre,pos) \
977 (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
b49e97c9
TS
978#define ELF_R_SYM(bfd, i) \
979 (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
980#define ELF_R_TYPE(bfd, i) \
981 (ABI_64_P (bfd) ? ELF64_MIPS_R_TYPE (i) : ELF32_R_TYPE (i))
982#define ELF_R_INFO(bfd, s, t) \
983 (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
984#else
ee6423ed 985#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
b49e97c9
TS
986#define ELF_R_SYM(bfd, i) \
987 (ELF32_R_SYM (i))
988#define ELF_R_TYPE(bfd, i) \
989 (ELF32_R_TYPE (i))
990#define ELF_R_INFO(bfd, s, t) \
991 (ELF32_R_INFO (s, t))
992#endif
993\f
994 /* The mips16 compiler uses a couple of special sections to handle
995 floating point arguments.
996
997 Section names that look like .mips16.fn.FNNAME contain stubs that
998 copy floating point arguments from the fp regs to the gp regs and
999 then jump to FNNAME. If any 32 bit function calls FNNAME, the
1000 call should be redirected to the stub instead. If no 32 bit
1001 function calls FNNAME, the stub should be discarded. We need to
1002 consider any reference to the function, not just a call, because
1003 if the address of the function is taken we will need the stub,
1004 since the address might be passed to a 32 bit function.
1005
1006 Section names that look like .mips16.call.FNNAME contain stubs
1007 that copy floating point arguments from the gp regs to the fp
1008 regs and then jump to FNNAME. If FNNAME is a 32 bit function,
1009 then any 16 bit function that calls FNNAME should be redirected
1010 to the stub instead. If FNNAME is not a 32 bit function, the
1011 stub should be discarded.
1012
1013 .mips16.call.fp.FNNAME sections are similar, but contain stubs
1014 which call FNNAME and then copy the return value from the fp regs
1015 to the gp regs. These stubs store the return value in $18 while
1016 calling FNNAME; any function which might call one of these stubs
1017 must arrange to save $18 around the call. (This case is not
1018 needed for 32 bit functions that call 16 bit functions, because
1019 16 bit functions always return floating point values in both
1020 $f0/$f1 and $2/$3.)
1021
1022 Note that in all cases FNNAME might be defined statically.
1023 Therefore, FNNAME is not used literally. Instead, the relocation
1024 information will indicate which symbol the section is for.
1025
1026 We record any stubs that we find in the symbol table. */
1027
1028#define FN_STUB ".mips16.fn."
1029#define CALL_STUB ".mips16.call."
1030#define CALL_FP_STUB ".mips16.call.fp."
b9d58d71
TS
1031
1032#define FN_STUB_P(name) CONST_STRNEQ (name, FN_STUB)
1033#define CALL_STUB_P(name) CONST_STRNEQ (name, CALL_STUB)
1034#define CALL_FP_STUB_P(name) CONST_STRNEQ (name, CALL_FP_STUB)
b49e97c9 1035\f
861fb55a 1036/* The format of the first PLT entry in an O32 executable. */
6d30f5b2
NC
1037static const bfd_vma mips_o32_exec_plt0_entry[] =
1038{
861fb55a
DJ
1039 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1040 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1041 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1042 0x031cc023, /* subu $24, $24, $28 */
40fc1451 1043 0x03e07825, /* or t7, ra, zero */
861fb55a
DJ
1044 0x0018c082, /* srl $24, $24, 2 */
1045 0x0320f809, /* jalr $25 */
1046 0x2718fffe /* subu $24, $24, 2 */
1047};
1048
3734320d
MF
1049/* The format of the first PLT entry in an O32 executable using compact
1050 jumps. */
1051static const bfd_vma mipsr6_o32_exec_plt0_entry_compact[] =
1052{
1053 0x3c1c0000, /* lui $28, %hi(&GOTPLT[0]) */
1054 0x8f990000, /* lw $25, %lo(&GOTPLT[0])($28) */
1055 0x279c0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1056 0x031cc023, /* subu $24, $24, $28 */
1057 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1058 0x0018c082, /* srl $24, $24, 2 */
1059 0x2718fffe, /* subu $24, $24, 2 */
1060 0xf8190000 /* jalrc $25 */
1061};
1062
861fb55a
DJ
1063/* The format of the first PLT entry in an N32 executable. Different
1064 because gp ($28) is not available; we use t2 ($14) instead. */
6d30f5b2
NC
1065static const bfd_vma mips_n32_exec_plt0_entry[] =
1066{
861fb55a
DJ
1067 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1068 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1069 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1070 0x030ec023, /* subu $24, $24, $14 */
40fc1451 1071 0x03e07825, /* or t7, ra, zero */
861fb55a
DJ
1072 0x0018c082, /* srl $24, $24, 2 */
1073 0x0320f809, /* jalr $25 */
1074 0x2718fffe /* subu $24, $24, 2 */
1075};
1076
3734320d
MF
1077/* The format of the first PLT entry in an N32 executable using compact
1078 jumps. Different because gp ($28) is not available; we use t2 ($14)
1079 instead. */
1080static const bfd_vma mipsr6_n32_exec_plt0_entry_compact[] =
1081{
1082 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1083 0x8dd90000, /* lw $25, %lo(&GOTPLT[0])($14) */
1084 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1085 0x030ec023, /* subu $24, $24, $14 */
1086 0x03e07821, /* move $15, $31 # 32-bit move (addu) */
1087 0x0018c082, /* srl $24, $24, 2 */
1088 0x2718fffe, /* subu $24, $24, 2 */
1089 0xf8190000 /* jalrc $25 */
1090};
1091
861fb55a
DJ
1092/* The format of the first PLT entry in an N64 executable. Different
1093 from N32 because of the increased size of GOT entries. */
6d30f5b2
NC
1094static const bfd_vma mips_n64_exec_plt0_entry[] =
1095{
861fb55a
DJ
1096 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1097 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1098 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1099 0x030ec023, /* subu $24, $24, $14 */
40fc1451 1100 0x03e07825, /* or t7, ra, zero */
861fb55a
DJ
1101 0x0018c0c2, /* srl $24, $24, 3 */
1102 0x0320f809, /* jalr $25 */
1103 0x2718fffe /* subu $24, $24, 2 */
1104};
1105
3734320d
MF
1106/* The format of the first PLT entry in an N64 executable using compact
1107 jumps. Different from N32 because of the increased size of GOT
1108 entries. */
1109static const bfd_vma mipsr6_n64_exec_plt0_entry_compact[] =
1110{
1111 0x3c0e0000, /* lui $14, %hi(&GOTPLT[0]) */
1112 0xddd90000, /* ld $25, %lo(&GOTPLT[0])($14) */
1113 0x25ce0000, /* addiu $14, $14, %lo(&GOTPLT[0]) */
1114 0x030ec023, /* subu $24, $24, $14 */
1115 0x03e0782d, /* move $15, $31 # 64-bit move (daddu) */
1116 0x0018c0c2, /* srl $24, $24, 3 */
1117 0x2718fffe, /* subu $24, $24, 2 */
1118 0xf8190000 /* jalrc $25 */
1119};
1120
1121
1bbce132
MR
1122/* The format of the microMIPS first PLT entry in an O32 executable.
1123 We rely on v0 ($2) rather than t8 ($24) to contain the address
1124 of the GOTPLT entry handled, so this stub may only be used when
1125 all the subsequent PLT entries are microMIPS code too.
1126
1127 The trailing NOP is for alignment and correct disassembly only. */
1128static const bfd_vma micromips_o32_exec_plt0_entry[] =
1129{
1130 0x7980, 0x0000, /* addiupc $3, (&GOTPLT[0]) - . */
1131 0xff23, 0x0000, /* lw $25, 0($3) */
1132 0x0535, /* subu $2, $2, $3 */
1133 0x2525, /* srl $2, $2, 2 */
1134 0x3302, 0xfffe, /* subu $24, $2, 2 */
1135 0x0dff, /* move $15, $31 */
1136 0x45f9, /* jalrs $25 */
1137 0x0f83, /* move $28, $3 */
1138 0x0c00 /* nop */
1139};
1140
833794fc
MR
1141/* The format of the microMIPS first PLT entry in an O32 executable
1142 in the insn32 mode. */
1143static const bfd_vma micromips_insn32_o32_exec_plt0_entry[] =
1144{
1145 0x41bc, 0x0000, /* lui $28, %hi(&GOTPLT[0]) */
1146 0xff3c, 0x0000, /* lw $25, %lo(&GOTPLT[0])($28) */
1147 0x339c, 0x0000, /* addiu $28, $28, %lo(&GOTPLT[0]) */
1148 0x0398, 0xc1d0, /* subu $24, $24, $28 */
40fc1451 1149 0x001f, 0x7a90, /* or $15, $31, zero */
833794fc
MR
1150 0x0318, 0x1040, /* srl $24, $24, 2 */
1151 0x03f9, 0x0f3c, /* jalr $25 */
1152 0x3318, 0xfffe /* subu $24, $24, 2 */
1153};
1154
1bbce132 1155/* The format of subsequent standard PLT entries. */
6d30f5b2
NC
1156static const bfd_vma mips_exec_plt_entry[] =
1157{
861fb55a
DJ
1158 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1159 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1160 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1161 0x03200008 /* jr $25 */
1162};
1163
7361da2c
AB
1164static const bfd_vma mipsr6_exec_plt_entry[] =
1165{
1166 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1167 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1168 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1169 0x03200009 /* jr $25 */
1170};
1171
3734320d
MF
1172static const bfd_vma mipsr6_exec_plt_entry_compact[] =
1173{
1174 0x3c0f0000, /* lui $15, %hi(.got.plt entry) */
1175 0x01f90000, /* l[wd] $25, %lo(.got.plt entry)($15) */
1176 0x25f80000, /* addiu $24, $15, %lo(.got.plt entry) */
1177 0xd8190000 /* jic $25, 0 */
1178};
1179
1bbce132
MR
1180/* The format of subsequent MIPS16 o32 PLT entries. We use v0 ($2)
1181 and v1 ($3) as temporaries because t8 ($24) and t9 ($25) are not
1182 directly addressable. */
1183static const bfd_vma mips16_o32_exec_plt_entry[] =
1184{
1185 0xb203, /* lw $2, 12($pc) */
1186 0x9a60, /* lw $3, 0($2) */
1187 0x651a, /* move $24, $2 */
1188 0xeb00, /* jr $3 */
1189 0x653b, /* move $25, $3 */
1190 0x6500, /* nop */
1191 0x0000, 0x0000 /* .word (.got.plt entry) */
1192};
1193
1194/* The format of subsequent microMIPS o32 PLT entries. We use v0 ($2)
1195 as a temporary because t8 ($24) is not addressable with ADDIUPC. */
1196static const bfd_vma micromips_o32_exec_plt_entry[] =
1197{
1198 0x7900, 0x0000, /* addiupc $2, (.got.plt entry) - . */
1199 0xff22, 0x0000, /* lw $25, 0($2) */
1200 0x4599, /* jr $25 */
1201 0x0f02 /* move $24, $2 */
1202};
1203
833794fc
MR
1204/* The format of subsequent microMIPS o32 PLT entries in the insn32 mode. */
1205static const bfd_vma micromips_insn32_o32_exec_plt_entry[] =
1206{
1207 0x41af, 0x0000, /* lui $15, %hi(.got.plt entry) */
1208 0xff2f, 0x0000, /* lw $25, %lo(.got.plt entry)($15) */
1209 0x0019, 0x0f3c, /* jr $25 */
1210 0x330f, 0x0000 /* addiu $24, $15, %lo(.got.plt entry) */
1211};
1212
0a44bf69 1213/* The format of the first PLT entry in a VxWorks executable. */
6d30f5b2
NC
1214static const bfd_vma mips_vxworks_exec_plt0_entry[] =
1215{
0a44bf69
RS
1216 0x3c190000, /* lui t9, %hi(_GLOBAL_OFFSET_TABLE_) */
1217 0x27390000, /* addiu t9, t9, %lo(_GLOBAL_OFFSET_TABLE_) */
1218 0x8f390008, /* lw t9, 8(t9) */
1219 0x00000000, /* nop */
1220 0x03200008, /* jr t9 */
1221 0x00000000 /* nop */
1222};
1223
1224/* The format of subsequent PLT entries. */
6d30f5b2
NC
1225static const bfd_vma mips_vxworks_exec_plt_entry[] =
1226{
0a44bf69
RS
1227 0x10000000, /* b .PLT_resolver */
1228 0x24180000, /* li t8, <pltindex> */
1229 0x3c190000, /* lui t9, %hi(<.got.plt slot>) */
1230 0x27390000, /* addiu t9, t9, %lo(<.got.plt slot>) */
1231 0x8f390000, /* lw t9, 0(t9) */
1232 0x00000000, /* nop */
1233 0x03200008, /* jr t9 */
1234 0x00000000 /* nop */
1235};
1236
1237/* The format of the first PLT entry in a VxWorks shared object. */
6d30f5b2
NC
1238static const bfd_vma mips_vxworks_shared_plt0_entry[] =
1239{
0a44bf69
RS
1240 0x8f990008, /* lw t9, 8(gp) */
1241 0x00000000, /* nop */
1242 0x03200008, /* jr t9 */
1243 0x00000000, /* nop */
1244 0x00000000, /* nop */
1245 0x00000000 /* nop */
1246};
1247
1248/* The format of subsequent PLT entries. */
6d30f5b2
NC
1249static const bfd_vma mips_vxworks_shared_plt_entry[] =
1250{
0a44bf69
RS
1251 0x10000000, /* b .PLT_resolver */
1252 0x24180000 /* li t8, <pltindex> */
1253};
1254\f
d21911ea
MR
1255/* microMIPS 32-bit opcode helper installer. */
1256
1257static void
1258bfd_put_micromips_32 (const bfd *abfd, bfd_vma opcode, bfd_byte *ptr)
1259{
1260 bfd_put_16 (abfd, (opcode >> 16) & 0xffff, ptr);
07d6d2b8 1261 bfd_put_16 (abfd, opcode & 0xffff, ptr + 2);
d21911ea
MR
1262}
1263
1264/* microMIPS 32-bit opcode helper retriever. */
1265
1266static bfd_vma
1267bfd_get_micromips_32 (const bfd *abfd, const bfd_byte *ptr)
1268{
1269 return (bfd_get_16 (abfd, ptr) << 16) | bfd_get_16 (abfd, ptr + 2);
1270}
1271\f
b49e97c9
TS
1272/* Look up an entry in a MIPS ELF linker hash table. */
1273
1274#define mips_elf_link_hash_lookup(table, string, create, copy, follow) \
1275 ((struct mips_elf_link_hash_entry *) \
1276 elf_link_hash_lookup (&(table)->root, (string), (create), \
1277 (copy), (follow)))
1278
1279/* Traverse a MIPS ELF linker hash table. */
1280
1281#define mips_elf_link_hash_traverse(table, func, info) \
1282 (elf_link_hash_traverse \
1283 (&(table)->root, \
9719ad41 1284 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
b49e97c9
TS
1285 (info)))
1286
0f20cc35
DJ
1287/* Find the base offsets for thread-local storage in this object,
1288 for GD/LD and IE/LE respectively. */
1289
1290#define TP_OFFSET 0x7000
1291#define DTP_OFFSET 0x8000
1292
1293static bfd_vma
1294dtprel_base (struct bfd_link_info *info)
1295{
1296 /* If tls_sec is NULL, we should have signalled an error already. */
1297 if (elf_hash_table (info)->tls_sec == NULL)
1298 return 0;
1299 return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
1300}
1301
1302static bfd_vma
1303tprel_base (struct bfd_link_info *info)
1304{
1305 /* If tls_sec is NULL, we should have signalled an error already. */
1306 if (elf_hash_table (info)->tls_sec == NULL)
1307 return 0;
1308 return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
1309}
1310
b49e97c9
TS
1311/* Create an entry in a MIPS ELF linker hash table. */
1312
1313static struct bfd_hash_entry *
9719ad41
RS
1314mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1315 struct bfd_hash_table *table, const char *string)
b49e97c9
TS
1316{
1317 struct mips_elf_link_hash_entry *ret =
1318 (struct mips_elf_link_hash_entry *) entry;
1319
1320 /* Allocate the structure if it has not already been allocated by a
1321 subclass. */
9719ad41
RS
1322 if (ret == NULL)
1323 ret = bfd_hash_allocate (table, sizeof (struct mips_elf_link_hash_entry));
1324 if (ret == NULL)
b49e97c9
TS
1325 return (struct bfd_hash_entry *) ret;
1326
1327 /* Call the allocation method of the superclass. */
1328 ret = ((struct mips_elf_link_hash_entry *)
1329 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
1330 table, string));
9719ad41 1331 if (ret != NULL)
b49e97c9
TS
1332 {
1333 /* Set local fields. */
1334 memset (&ret->esym, 0, sizeof (EXTR));
1335 /* We use -2 as a marker to indicate that the information has
1336 not been set. -1 means there is no associated ifd. */
1337 ret->esym.ifd = -2;
861fb55a 1338 ret->la25_stub = 0;
b49e97c9 1339 ret->possibly_dynamic_relocs = 0;
b49e97c9 1340 ret->fn_stub = NULL;
b49e97c9
TS
1341 ret->call_stub = NULL;
1342 ret->call_fp_stub = NULL;
f16a9783 1343 ret->mipsxhash_loc = 0;
634835ae 1344 ret->global_got_area = GGA_NONE;
6ccf4795 1345 ret->got_only_for_calls = TRUE;
71782a75 1346 ret->readonly_reloc = FALSE;
861fb55a 1347 ret->has_static_relocs = FALSE;
71782a75
RS
1348 ret->no_fn_stub = FALSE;
1349 ret->need_fn_stub = FALSE;
861fb55a 1350 ret->has_nonpic_branches = FALSE;
33bb52fb 1351 ret->needs_lazy_stub = FALSE;
1bbce132 1352 ret->use_plt_entry = FALSE;
b49e97c9
TS
1353 }
1354
1355 return (struct bfd_hash_entry *) ret;
1356}
f0abc2a1 1357
6ae68ba3
MR
1358/* Allocate MIPS ELF private object data. */
1359
1360bfd_boolean
1361_bfd_mips_elf_mkobject (bfd *abfd)
1362{
1363 return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
1364 MIPS_ELF_DATA);
1365}
1366
f0abc2a1 1367bfd_boolean
9719ad41 1368_bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 1369{
f592407e
AM
1370 if (!sec->used_by_bfd)
1371 {
1372 struct _mips_elf_section_data *sdata;
986f0783 1373 size_t amt = sizeof (*sdata);
f0abc2a1 1374
f592407e
AM
1375 sdata = bfd_zalloc (abfd, amt);
1376 if (sdata == NULL)
1377 return FALSE;
1378 sec->used_by_bfd = sdata;
1379 }
f0abc2a1
AM
1380
1381 return _bfd_elf_new_section_hook (abfd, sec);
1382}
b49e97c9
TS
1383\f
1384/* Read ECOFF debugging information from a .mdebug section into a
1385 ecoff_debug_info structure. */
1386
b34976b6 1387bfd_boolean
9719ad41
RS
1388_bfd_mips_elf_read_ecoff_info (bfd *abfd, asection *section,
1389 struct ecoff_debug_info *debug)
b49e97c9
TS
1390{
1391 HDRR *symhdr;
1392 const struct ecoff_debug_swap *swap;
9719ad41 1393 char *ext_hdr;
b49e97c9
TS
1394
1395 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1396 memset (debug, 0, sizeof (*debug));
1397
9719ad41 1398 ext_hdr = bfd_malloc (swap->external_hdr_size);
b49e97c9
TS
1399 if (ext_hdr == NULL && swap->external_hdr_size != 0)
1400 goto error_return;
1401
9719ad41 1402 if (! bfd_get_section_contents (abfd, section, ext_hdr, 0,
82e51918 1403 swap->external_hdr_size))
b49e97c9
TS
1404 goto error_return;
1405
1406 symhdr = &debug->symbolic_header;
1407 (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
1408
1409 /* The symbolic header contains absolute file offsets and sizes to
1410 read. */
1411#define READ(ptr, offset, count, size, type) \
1f4361a7 1412 do \
b49e97c9 1413 { \
1f4361a7
AM
1414 size_t amt; \
1415 debug->ptr = NULL; \
1416 if (symhdr->count == 0) \
1417 break; \
1418 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
1419 { \
1420 bfd_set_error (bfd_error_file_too_big); \
1421 goto error_return; \
1422 } \
2bb3687b 1423 if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
b49e97c9 1424 goto error_return; \
2bb3687b
AM
1425 debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \
1426 if (debug->ptr == NULL) \
b49e97c9 1427 goto error_return; \
1f4361a7 1428 } while (0)
b49e97c9
TS
1429
1430 READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
9719ad41
RS
1431 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
1432 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
1433 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
1434 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
b49e97c9
TS
1435 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
1436 union aux_ext *);
1437 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
1438 READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
9719ad41
RS
1439 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
1440 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
1441 READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
b49e97c9
TS
1442#undef READ
1443
1444 debug->fdr = NULL;
b49e97c9 1445
b34976b6 1446 return TRUE;
b49e97c9
TS
1447
1448 error_return:
c9594989
AM
1449 free (ext_hdr);
1450 free (debug->line);
1451 free (debug->external_dnr);
1452 free (debug->external_pdr);
1453 free (debug->external_sym);
1454 free (debug->external_opt);
1455 free (debug->external_aux);
1456 free (debug->ss);
1457 free (debug->ssext);
1458 free (debug->external_fdr);
1459 free (debug->external_rfd);
1460 free (debug->external_ext);
b34976b6 1461 return FALSE;
b49e97c9
TS
1462}
1463\f
1464/* Swap RPDR (runtime procedure table entry) for output. */
1465
1466static void
9719ad41 1467ecoff_swap_rpdr_out (bfd *abfd, const RPDR *in, struct rpdr_ext *ex)
b49e97c9
TS
1468{
1469 H_PUT_S32 (abfd, in->adr, ex->p_adr);
1470 H_PUT_32 (abfd, in->regmask, ex->p_regmask);
1471 H_PUT_32 (abfd, in->regoffset, ex->p_regoffset);
1472 H_PUT_32 (abfd, in->fregmask, ex->p_fregmask);
1473 H_PUT_32 (abfd, in->fregoffset, ex->p_fregoffset);
1474 H_PUT_32 (abfd, in->frameoffset, ex->p_frameoffset);
1475
1476 H_PUT_16 (abfd, in->framereg, ex->p_framereg);
1477 H_PUT_16 (abfd, in->pcreg, ex->p_pcreg);
1478
1479 H_PUT_32 (abfd, in->irpss, ex->p_irpss);
b49e97c9
TS
1480}
1481
1482/* Create a runtime procedure table from the .mdebug section. */
1483
b34976b6 1484static bfd_boolean
9719ad41
RS
1485mips_elf_create_procedure_table (void *handle, bfd *abfd,
1486 struct bfd_link_info *info, asection *s,
1487 struct ecoff_debug_info *debug)
b49e97c9
TS
1488{
1489 const struct ecoff_debug_swap *swap;
1490 HDRR *hdr = &debug->symbolic_header;
1491 RPDR *rpdr, *rp;
1492 struct rpdr_ext *erp;
9719ad41 1493 void *rtproc;
b49e97c9
TS
1494 struct pdr_ext *epdr;
1495 struct sym_ext *esym;
1496 char *ss, **sv;
1497 char *str;
1498 bfd_size_type size;
1499 bfd_size_type count;
1500 unsigned long sindex;
1501 unsigned long i;
1502 PDR pdr;
1503 SYMR sym;
1504 const char *no_name_func = _("static procedure (no name)");
1505
1506 epdr = NULL;
1507 rpdr = NULL;
1508 esym = NULL;
1509 ss = NULL;
1510 sv = NULL;
1511
1512 swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
1513
1514 sindex = strlen (no_name_func) + 1;
1515 count = hdr->ipdMax;
1516 if (count > 0)
1517 {
1518 size = swap->external_pdr_size;
1519
9719ad41 1520 epdr = bfd_malloc (size * count);
b49e97c9
TS
1521 if (epdr == NULL)
1522 goto error_return;
1523
9719ad41 1524 if (! _bfd_ecoff_get_accumulated_pdr (handle, (bfd_byte *) epdr))
b49e97c9
TS
1525 goto error_return;
1526
1527 size = sizeof (RPDR);
9719ad41 1528 rp = rpdr = bfd_malloc (size * count);
b49e97c9
TS
1529 if (rpdr == NULL)
1530 goto error_return;
1531
1532 size = sizeof (char *);
9719ad41 1533 sv = bfd_malloc (size * count);
b49e97c9
TS
1534 if (sv == NULL)
1535 goto error_return;
1536
1537 count = hdr->isymMax;
1538 size = swap->external_sym_size;
9719ad41 1539 esym = bfd_malloc (size * count);
b49e97c9
TS
1540 if (esym == NULL)
1541 goto error_return;
1542
9719ad41 1543 if (! _bfd_ecoff_get_accumulated_sym (handle, (bfd_byte *) esym))
b49e97c9
TS
1544 goto error_return;
1545
1546 count = hdr->issMax;
9719ad41 1547 ss = bfd_malloc (count);
b49e97c9
TS
1548 if (ss == NULL)
1549 goto error_return;
f075ee0c 1550 if (! _bfd_ecoff_get_accumulated_ss (handle, (bfd_byte *) ss))
b49e97c9
TS
1551 goto error_return;
1552
1553 count = hdr->ipdMax;
1554 for (i = 0; i < (unsigned long) count; i++, rp++)
1555 {
9719ad41
RS
1556 (*swap->swap_pdr_in) (abfd, epdr + i, &pdr);
1557 (*swap->swap_sym_in) (abfd, &esym[pdr.isym], &sym);
b49e97c9
TS
1558 rp->adr = sym.value;
1559 rp->regmask = pdr.regmask;
1560 rp->regoffset = pdr.regoffset;
1561 rp->fregmask = pdr.fregmask;
1562 rp->fregoffset = pdr.fregoffset;
1563 rp->frameoffset = pdr.frameoffset;
1564 rp->framereg = pdr.framereg;
1565 rp->pcreg = pdr.pcreg;
1566 rp->irpss = sindex;
1567 sv[i] = ss + sym.iss;
1568 sindex += strlen (sv[i]) + 1;
1569 }
1570 }
1571
1572 size = sizeof (struct rpdr_ext) * (count + 2) + sindex;
1573 size = BFD_ALIGN (size, 16);
9719ad41 1574 rtproc = bfd_alloc (abfd, size);
b49e97c9
TS
1575 if (rtproc == NULL)
1576 {
1577 mips_elf_hash_table (info)->procedure_count = 0;
1578 goto error_return;
1579 }
1580
1581 mips_elf_hash_table (info)->procedure_count = count + 2;
1582
9719ad41 1583 erp = rtproc;
b49e97c9
TS
1584 memset (erp, 0, sizeof (struct rpdr_ext));
1585 erp++;
1586 str = (char *) rtproc + sizeof (struct rpdr_ext) * (count + 2);
1587 strcpy (str, no_name_func);
1588 str += strlen (no_name_func) + 1;
1589 for (i = 0; i < count; i++)
1590 {
1591 ecoff_swap_rpdr_out (abfd, rpdr + i, erp + i);
1592 strcpy (str, sv[i]);
1593 str += strlen (sv[i]) + 1;
1594 }
1595 H_PUT_S32 (abfd, -1, (erp + count)->p_adr);
1596
1597 /* Set the size and contents of .rtproc section. */
eea6121a 1598 s->size = size;
9719ad41 1599 s->contents = rtproc;
b49e97c9
TS
1600
1601 /* Skip this section later on (I don't think this currently
1602 matters, but someday it might). */
8423293d 1603 s->map_head.link_order = NULL;
b49e97c9 1604
c9594989
AM
1605 free (epdr);
1606 free (rpdr);
1607 free (esym);
1608 free (ss);
1609 free (sv);
b34976b6 1610 return TRUE;
b49e97c9
TS
1611
1612 error_return:
c9594989
AM
1613 free (epdr);
1614 free (rpdr);
1615 free (esym);
1616 free (ss);
1617 free (sv);
b34976b6 1618 return FALSE;
b49e97c9 1619}
738e5348 1620\f
861fb55a
DJ
1621/* We're going to create a stub for H. Create a symbol for the stub's
1622 value and size, to help make the disassembly easier to read. */
1623
1624static bfd_boolean
1625mips_elf_create_stub_symbol (struct bfd_link_info *info,
1626 struct mips_elf_link_hash_entry *h,
1627 const char *prefix, asection *s, bfd_vma value,
1628 bfd_vma size)
1629{
a848a227 1630 bfd_boolean micromips_p = ELF_ST_IS_MICROMIPS (h->root.other);
861fb55a
DJ
1631 struct bfd_link_hash_entry *bh;
1632 struct elf_link_hash_entry *elfh;
e1fa0163
NC
1633 char *name;
1634 bfd_boolean res;
861fb55a 1635
a848a227 1636 if (micromips_p)
df58fc94
RS
1637 value |= 1;
1638
861fb55a 1639 /* Create a new symbol. */
e1fa0163 1640 name = concat (prefix, h->root.root.root.string, NULL);
861fb55a 1641 bh = NULL;
e1fa0163
NC
1642 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1643 BSF_LOCAL, s, value, NULL,
1644 TRUE, FALSE, &bh);
1645 free (name);
1646 if (! res)
861fb55a
DJ
1647 return FALSE;
1648
1649 /* Make it a local function. */
1650 elfh = (struct elf_link_hash_entry *) bh;
1651 elfh->type = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
1652 elfh->size = size;
1653 elfh->forced_local = 1;
a848a227
MR
1654 if (micromips_p)
1655 elfh->other = ELF_ST_SET_MICROMIPS (elfh->other);
861fb55a
DJ
1656 return TRUE;
1657}
1658
738e5348
RS
1659/* We're about to redefine H. Create a symbol to represent H's
1660 current value and size, to help make the disassembly easier
1661 to read. */
1662
1663static bfd_boolean
1664mips_elf_create_shadow_symbol (struct bfd_link_info *info,
1665 struct mips_elf_link_hash_entry *h,
1666 const char *prefix)
1667{
1668 struct bfd_link_hash_entry *bh;
1669 struct elf_link_hash_entry *elfh;
e1fa0163 1670 char *name;
738e5348
RS
1671 asection *s;
1672 bfd_vma value;
e1fa0163 1673 bfd_boolean res;
738e5348
RS
1674
1675 /* Read the symbol's value. */
1676 BFD_ASSERT (h->root.root.type == bfd_link_hash_defined
1677 || h->root.root.type == bfd_link_hash_defweak);
1678 s = h->root.root.u.def.section;
1679 value = h->root.root.u.def.value;
1680
1681 /* Create a new symbol. */
e1fa0163 1682 name = concat (prefix, h->root.root.root.string, NULL);
738e5348 1683 bh = NULL;
e1fa0163
NC
1684 res = _bfd_generic_link_add_one_symbol (info, s->owner, name,
1685 BSF_LOCAL, s, value, NULL,
1686 TRUE, FALSE, &bh);
1687 free (name);
1688 if (! res)
738e5348
RS
1689 return FALSE;
1690
1691 /* Make it local and copy the other attributes from H. */
1692 elfh = (struct elf_link_hash_entry *) bh;
1693 elfh->type = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (h->root.type));
1694 elfh->other = h->root.other;
1695 elfh->size = h->root.size;
1696 elfh->forced_local = 1;
1697 return TRUE;
1698}
1699
1700/* Return TRUE if relocations in SECTION can refer directly to a MIPS16
1701 function rather than to a hard-float stub. */
1702
1703static bfd_boolean
1704section_allows_mips16_refs_p (asection *section)
1705{
1706 const char *name;
1707
fd361982 1708 name = bfd_section_name (section);
738e5348
RS
1709 return (FN_STUB_P (name)
1710 || CALL_STUB_P (name)
1711 || CALL_FP_STUB_P (name)
1712 || strcmp (name, ".pdr") == 0);
1713}
1714
1715/* [RELOCS, RELEND) are the relocations against SEC, which is a MIPS16
1716 stub section of some kind. Return the R_SYMNDX of the target
1717 function, or 0 if we can't decide which function that is. */
1718
1719static unsigned long
cb4437b8
MR
1720mips16_stub_symndx (const struct elf_backend_data *bed,
1721 asection *sec ATTRIBUTE_UNUSED,
502e814e 1722 const Elf_Internal_Rela *relocs,
738e5348
RS
1723 const Elf_Internal_Rela *relend)
1724{
cb4437b8 1725 int int_rels_per_ext_rel = bed->s->int_rels_per_ext_rel;
738e5348
RS
1726 const Elf_Internal_Rela *rel;
1727
cb4437b8
MR
1728 /* Trust the first R_MIPS_NONE relocation, if any, but not a subsequent
1729 one in a compound relocation. */
1730 for (rel = relocs; rel < relend; rel += int_rels_per_ext_rel)
738e5348
RS
1731 if (ELF_R_TYPE (sec->owner, rel->r_info) == R_MIPS_NONE)
1732 return ELF_R_SYM (sec->owner, rel->r_info);
1733
1734 /* Otherwise trust the first relocation, whatever its kind. This is
1735 the traditional behavior. */
1736 if (relocs < relend)
1737 return ELF_R_SYM (sec->owner, relocs->r_info);
1738
1739 return 0;
1740}
b49e97c9
TS
1741
1742/* Check the mips16 stubs for a particular symbol, and see if we can
1743 discard them. */
1744
861fb55a
DJ
1745static void
1746mips_elf_check_mips16_stubs (struct bfd_link_info *info,
1747 struct mips_elf_link_hash_entry *h)
b49e97c9 1748{
738e5348
RS
1749 /* Dynamic symbols must use the standard call interface, in case other
1750 objects try to call them. */
1751 if (h->fn_stub != NULL
1752 && h->root.dynindx != -1)
1753 {
1754 mips_elf_create_shadow_symbol (info, h, ".mips16.");
1755 h->need_fn_stub = TRUE;
1756 }
1757
b49e97c9
TS
1758 if (h->fn_stub != NULL
1759 && ! h->need_fn_stub)
1760 {
1761 /* We don't need the fn_stub; the only references to this symbol
07d6d2b8
AM
1762 are 16 bit calls. Clobber the size to 0 to prevent it from
1763 being included in the link. */
eea6121a 1764 h->fn_stub->size = 0;
b49e97c9
TS
1765 h->fn_stub->flags &= ~SEC_RELOC;
1766 h->fn_stub->reloc_count = 0;
1767 h->fn_stub->flags |= SEC_EXCLUDE;
ca9584fb 1768 h->fn_stub->output_section = bfd_abs_section_ptr;
b49e97c9
TS
1769 }
1770
1771 if (h->call_stub != NULL
30c09090 1772 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1773 {
1774 /* We don't need the call_stub; this is a 16 bit function, so
07d6d2b8
AM
1775 calls from other 16 bit functions are OK. Clobber the size
1776 to 0 to prevent it from being included in the link. */
eea6121a 1777 h->call_stub->size = 0;
b49e97c9
TS
1778 h->call_stub->flags &= ~SEC_RELOC;
1779 h->call_stub->reloc_count = 0;
1780 h->call_stub->flags |= SEC_EXCLUDE;
ca9584fb 1781 h->call_stub->output_section = bfd_abs_section_ptr;
b49e97c9
TS
1782 }
1783
1784 if (h->call_fp_stub != NULL
30c09090 1785 && ELF_ST_IS_MIPS16 (h->root.other))
b49e97c9
TS
1786 {
1787 /* We don't need the call_stub; this is a 16 bit function, so
07d6d2b8
AM
1788 calls from other 16 bit functions are OK. Clobber the size
1789 to 0 to prevent it from being included in the link. */
eea6121a 1790 h->call_fp_stub->size = 0;
b49e97c9
TS
1791 h->call_fp_stub->flags &= ~SEC_RELOC;
1792 h->call_fp_stub->reloc_count = 0;
1793 h->call_fp_stub->flags |= SEC_EXCLUDE;
ca9584fb 1794 h->call_fp_stub->output_section = bfd_abs_section_ptr;
b49e97c9 1795 }
861fb55a
DJ
1796}
1797
1798/* Hashtable callbacks for mips_elf_la25_stubs. */
1799
1800static hashval_t
1801mips_elf_la25_stub_hash (const void *entry_)
1802{
1803 const struct mips_elf_la25_stub *entry;
1804
1805 entry = (struct mips_elf_la25_stub *) entry_;
1806 return entry->h->root.root.u.def.section->id
1807 + entry->h->root.root.u.def.value;
1808}
1809
1810static int
1811mips_elf_la25_stub_eq (const void *entry1_, const void *entry2_)
1812{
1813 const struct mips_elf_la25_stub *entry1, *entry2;
1814
1815 entry1 = (struct mips_elf_la25_stub *) entry1_;
1816 entry2 = (struct mips_elf_la25_stub *) entry2_;
1817 return ((entry1->h->root.root.u.def.section
1818 == entry2->h->root.root.u.def.section)
1819 && (entry1->h->root.root.u.def.value
1820 == entry2->h->root.root.u.def.value));
1821}
1822
1823/* Called by the linker to set up the la25 stub-creation code. FN is
1824 the linker's implementation of add_stub_function. Return true on
1825 success. */
1826
1827bfd_boolean
1828_bfd_mips_elf_init_stubs (struct bfd_link_info *info,
1829 asection *(*fn) (const char *, asection *,
1830 asection *))
1831{
1832 struct mips_elf_link_hash_table *htab;
1833
1834 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1835 if (htab == NULL)
1836 return FALSE;
1837
861fb55a
DJ
1838 htab->add_stub_section = fn;
1839 htab->la25_stubs = htab_try_create (1, mips_elf_la25_stub_hash,
1840 mips_elf_la25_stub_eq, NULL);
1841 if (htab->la25_stubs == NULL)
1842 return FALSE;
1843
1844 return TRUE;
1845}
1846
1847/* Return true if H is a locally-defined PIC function, in the sense
8f0c309a
CLT
1848 that it or its fn_stub might need $25 to be valid on entry.
1849 Note that MIPS16 functions set up $gp using PC-relative instructions,
1850 so they themselves never need $25 to be valid. Only non-MIPS16
1851 entry points are of interest here. */
861fb55a
DJ
1852
1853static bfd_boolean
1854mips_elf_local_pic_function_p (struct mips_elf_link_hash_entry *h)
1855{
1856 return ((h->root.root.type == bfd_link_hash_defined
1857 || h->root.root.type == bfd_link_hash_defweak)
1858 && h->root.def_regular
1859 && !bfd_is_abs_section (h->root.root.u.def.section)
f02cb058 1860 && !bfd_is_und_section (h->root.root.u.def.section)
8f0c309a
CLT
1861 && (!ELF_ST_IS_MIPS16 (h->root.other)
1862 || (h->fn_stub && h->need_fn_stub))
861fb55a
DJ
1863 && (PIC_OBJECT_P (h->root.root.u.def.section->owner)
1864 || ELF_ST_IS_MIPS_PIC (h->root.other)));
1865}
1866
8f0c309a
CLT
1867/* Set *SEC to the input section that contains the target of STUB.
1868 Return the offset of the target from the start of that section. */
1869
1870static bfd_vma
1871mips_elf_get_la25_target (struct mips_elf_la25_stub *stub,
1872 asection **sec)
1873{
1874 if (ELF_ST_IS_MIPS16 (stub->h->root.other))
1875 {
1876 BFD_ASSERT (stub->h->need_fn_stub);
1877 *sec = stub->h->fn_stub;
1878 return 0;
1879 }
1880 else
1881 {
1882 *sec = stub->h->root.root.u.def.section;
1883 return stub->h->root.root.u.def.value;
1884 }
1885}
1886
861fb55a
DJ
1887/* STUB describes an la25 stub that we have decided to implement
1888 by inserting an LUI/ADDIU pair before the target function.
1889 Create the section and redirect the function symbol to it. */
1890
1891static bfd_boolean
1892mips_elf_add_la25_intro (struct mips_elf_la25_stub *stub,
1893 struct bfd_link_info *info)
1894{
1895 struct mips_elf_link_hash_table *htab;
1896 char *name;
1897 asection *s, *input_section;
1898 unsigned int align;
1899
1900 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1901 if (htab == NULL)
1902 return FALSE;
861fb55a
DJ
1903
1904 /* Create a unique name for the new section. */
1905 name = bfd_malloc (11 + sizeof (".text.stub."));
1906 if (name == NULL)
1907 return FALSE;
1908 sprintf (name, ".text.stub.%d", (int) htab_elements (htab->la25_stubs));
1909
1910 /* Create the section. */
8f0c309a 1911 mips_elf_get_la25_target (stub, &input_section);
861fb55a
DJ
1912 s = htab->add_stub_section (name, input_section,
1913 input_section->output_section);
1914 if (s == NULL)
1915 return FALSE;
1916
1917 /* Make sure that any padding goes before the stub. */
1918 align = input_section->alignment_power;
fd361982 1919 if (!bfd_set_section_alignment (s, align))
861fb55a
DJ
1920 return FALSE;
1921 if (align > 3)
1922 s->size = (1 << align) - 8;
1923
1924 /* Create a symbol for the stub. */
1925 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 8);
1926 stub->stub_section = s;
1927 stub->offset = s->size;
1928
1929 /* Allocate room for it. */
1930 s->size += 8;
1931 return TRUE;
1932}
1933
1934/* STUB describes an la25 stub that we have decided to implement
1935 with a separate trampoline. Allocate room for it and redirect
1936 the function symbol to it. */
1937
1938static bfd_boolean
1939mips_elf_add_la25_trampoline (struct mips_elf_la25_stub *stub,
1940 struct bfd_link_info *info)
1941{
1942 struct mips_elf_link_hash_table *htab;
1943 asection *s;
1944
1945 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1946 if (htab == NULL)
1947 return FALSE;
861fb55a
DJ
1948
1949 /* Create a trampoline section, if we haven't already. */
1950 s = htab->strampoline;
1951 if (s == NULL)
1952 {
1953 asection *input_section = stub->h->root.root.u.def.section;
1954 s = htab->add_stub_section (".text", NULL,
1955 input_section->output_section);
fd361982 1956 if (s == NULL || !bfd_set_section_alignment (s, 4))
861fb55a
DJ
1957 return FALSE;
1958 htab->strampoline = s;
1959 }
1960
1961 /* Create a symbol for the stub. */
1962 mips_elf_create_stub_symbol (info, stub->h, ".pic.", s, s->size, 16);
1963 stub->stub_section = s;
1964 stub->offset = s->size;
1965
1966 /* Allocate room for it. */
1967 s->size += 16;
1968 return TRUE;
1969}
1970
1971/* H describes a symbol that needs an la25 stub. Make sure that an
1972 appropriate stub exists and point H at it. */
1973
1974static bfd_boolean
1975mips_elf_add_la25_stub (struct bfd_link_info *info,
1976 struct mips_elf_link_hash_entry *h)
1977{
1978 struct mips_elf_link_hash_table *htab;
1979 struct mips_elf_la25_stub search, *stub;
1980 bfd_boolean use_trampoline_p;
1981 asection *s;
1982 bfd_vma value;
1983 void **slot;
1984
861fb55a
DJ
1985 /* Describe the stub we want. */
1986 search.stub_section = NULL;
1987 search.offset = 0;
1988 search.h = h;
1989
1990 /* See if we've already created an equivalent stub. */
1991 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
1992 if (htab == NULL)
1993 return FALSE;
1994
861fb55a
DJ
1995 slot = htab_find_slot (htab->la25_stubs, &search, INSERT);
1996 if (slot == NULL)
1997 return FALSE;
1998
1999 stub = (struct mips_elf_la25_stub *) *slot;
2000 if (stub != NULL)
2001 {
2002 /* We can reuse the existing stub. */
2003 h->la25_stub = stub;
2004 return TRUE;
2005 }
2006
2007 /* Create a permanent copy of ENTRY and add it to the hash table. */
2008 stub = bfd_malloc (sizeof (search));
2009 if (stub == NULL)
2010 return FALSE;
2011 *stub = search;
2012 *slot = stub;
2013
8f0c309a
CLT
2014 /* Prefer to use LUI/ADDIU stubs if the function is at the beginning
2015 of the section and if we would need no more than 2 nops. */
2016 value = mips_elf_get_la25_target (stub, &s);
fe152e64
MR
2017 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
2018 value &= ~1;
8f0c309a
CLT
2019 use_trampoline_p = (value != 0 || s->alignment_power > 4);
2020
861fb55a
DJ
2021 h->la25_stub = stub;
2022 return (use_trampoline_p
2023 ? mips_elf_add_la25_trampoline (stub, info)
2024 : mips_elf_add_la25_intro (stub, info));
2025}
2026
2027/* A mips_elf_link_hash_traverse callback that is called before sizing
2028 sections. DATA points to a mips_htab_traverse_info structure. */
2029
2030static bfd_boolean
2031mips_elf_check_symbols (struct mips_elf_link_hash_entry *h, void *data)
2032{
2033 struct mips_htab_traverse_info *hti;
2034
2035 hti = (struct mips_htab_traverse_info *) data;
0e1862bb 2036 if (!bfd_link_relocatable (hti->info))
861fb55a 2037 mips_elf_check_mips16_stubs (hti->info, h);
b49e97c9 2038
861fb55a
DJ
2039 if (mips_elf_local_pic_function_p (h))
2040 {
ba85c43e
NC
2041 /* PR 12845: If H is in a section that has been garbage
2042 collected it will have its output section set to *ABS*. */
2043 if (bfd_is_abs_section (h->root.root.u.def.section->output_section))
2044 return TRUE;
2045
861fb55a
DJ
2046 /* H is a function that might need $25 to be valid on entry.
2047 If we're creating a non-PIC relocatable object, mark H as
2048 being PIC. If we're creating a non-relocatable object with
2049 non-PIC branches and jumps to H, make sure that H has an la25
2050 stub. */
0e1862bb 2051 if (bfd_link_relocatable (hti->info))
861fb55a
DJ
2052 {
2053 if (!PIC_OBJECT_P (hti->output_bfd))
2054 h->root.other = ELF_ST_SET_MIPS_PIC (h->root.other);
2055 }
2056 else if (h->has_nonpic_branches && !mips_elf_add_la25_stub (hti->info, h))
2057 {
2058 hti->error = TRUE;
2059 return FALSE;
2060 }
2061 }
b34976b6 2062 return TRUE;
b49e97c9
TS
2063}
2064\f
d6f16593
MR
2065/* R_MIPS16_26 is used for the mips16 jal and jalx instructions.
2066 Most mips16 instructions are 16 bits, but these instructions
2067 are 32 bits.
2068
2069 The format of these instructions is:
2070
2071 +--------------+--------------------------------+
2072 | JALX | X| Imm 20:16 | Imm 25:21 |
2073 +--------------+--------------------------------+
07d6d2b8 2074 | Immediate 15:0 |
d6f16593
MR
2075 +-----------------------------------------------+
2076
2077 JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
2078 Note that the immediate value in the first word is swapped.
2079
2080 When producing a relocatable object file, R_MIPS16_26 is
2081 handled mostly like R_MIPS_26. In particular, the addend is
2082 stored as a straight 26-bit value in a 32-bit instruction.
2083 (gas makes life simpler for itself by never adjusting a
2084 R_MIPS16_26 reloc to be against a section, so the addend is
2085 always zero). However, the 32 bit instruction is stored as 2
2086 16-bit values, rather than a single 32-bit value. In a
2087 big-endian file, the result is the same; in a little-endian
2088 file, the two 16-bit halves of the 32 bit value are swapped.
2089 This is so that a disassembler can recognize the jal
2090 instruction.
2091
2092 When doing a final link, R_MIPS16_26 is treated as a 32 bit
2093 instruction stored as two 16-bit values. The addend A is the
2094 contents of the targ26 field. The calculation is the same as
2095 R_MIPS_26. When storing the calculated value, reorder the
2096 immediate value as shown above, and don't forget to store the
2097 value as two 16-bit values.
2098
2099 To put it in MIPS ABI terms, the relocation field is T-targ26-16,
2100 defined as
2101
2102 big-endian:
2103 +--------+----------------------+
07d6d2b8
AM
2104 | | |
2105 | | targ26-16 |
2106 |31 26|25 0|
d6f16593
MR
2107 +--------+----------------------+
2108
2109 little-endian:
2110 +----------+------+-------------+
07d6d2b8
AM
2111 | | | |
2112 | sub1 | | sub2 |
2113 |0 9|10 15|16 31|
d6f16593
MR
2114 +----------+--------------------+
2115 where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
2116 ((sub1 << 16) | sub2)).
2117
2118 When producing a relocatable object file, the calculation is
2119 (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2120 When producing a fully linked file, the calculation is
2121 let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
2122 ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
2123
738e5348
RS
2124 The table below lists the other MIPS16 instruction relocations.
2125 Each one is calculated in the same way as the non-MIPS16 relocation
2126 given on the right, but using the extended MIPS16 layout of 16-bit
2127 immediate fields:
2128
2129 R_MIPS16_GPREL R_MIPS_GPREL16
2130 R_MIPS16_GOT16 R_MIPS_GOT16
2131 R_MIPS16_CALL16 R_MIPS_CALL16
2132 R_MIPS16_HI16 R_MIPS_HI16
2133 R_MIPS16_LO16 R_MIPS_LO16
2134
2135 A typical instruction will have a format like this:
d6f16593
MR
2136
2137 +--------------+--------------------------------+
2138 | EXTEND | Imm 10:5 | Imm 15:11 |
2139 +--------------+--------------------------------+
2140 | Major | rx | ry | Imm 4:0 |
2141 +--------------+--------------------------------+
2142
2143 EXTEND is the five bit value 11110. Major is the instruction
2144 opcode.
2145
738e5348
RS
2146 All we need to do here is shuffle the bits appropriately.
2147 As above, the two 16-bit halves must be swapped on a
c9775dde
MR
2148 little-endian system.
2149
2150 Finally R_MIPS16_PC16_S1 corresponds to R_MIPS_PC16, however the
2151 relocatable field is shifted by 1 rather than 2 and the same bit
2152 shuffling is done as with the relocations above. */
738e5348
RS
2153
2154static inline bfd_boolean
2155mips16_reloc_p (int r_type)
2156{
2157 switch (r_type)
2158 {
2159 case R_MIPS16_26:
2160 case R_MIPS16_GPREL:
2161 case R_MIPS16_GOT16:
2162 case R_MIPS16_CALL16:
2163 case R_MIPS16_HI16:
2164 case R_MIPS16_LO16:
d0f13682
CLT
2165 case R_MIPS16_TLS_GD:
2166 case R_MIPS16_TLS_LDM:
2167 case R_MIPS16_TLS_DTPREL_HI16:
2168 case R_MIPS16_TLS_DTPREL_LO16:
2169 case R_MIPS16_TLS_GOTTPREL:
2170 case R_MIPS16_TLS_TPREL_HI16:
2171 case R_MIPS16_TLS_TPREL_LO16:
c9775dde 2172 case R_MIPS16_PC16_S1:
738e5348
RS
2173 return TRUE;
2174
2175 default:
2176 return FALSE;
2177 }
2178}
2179
df58fc94
RS
2180/* Check if a microMIPS reloc. */
2181
2182static inline bfd_boolean
2183micromips_reloc_p (unsigned int r_type)
2184{
2185 return r_type >= R_MICROMIPS_min && r_type < R_MICROMIPS_max;
2186}
2187
2188/* Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
2189 on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
2190 and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions. */
2191
2192static inline bfd_boolean
2193micromips_reloc_shuffle_p (unsigned int r_type)
2194{
2195 return (micromips_reloc_p (r_type)
2196 && r_type != R_MICROMIPS_PC7_S1
2197 && r_type != R_MICROMIPS_PC10_S1);
2198}
2199
738e5348
RS
2200static inline bfd_boolean
2201got16_reloc_p (int r_type)
2202{
df58fc94
RS
2203 return (r_type == R_MIPS_GOT16
2204 || r_type == R_MIPS16_GOT16
2205 || r_type == R_MICROMIPS_GOT16);
738e5348
RS
2206}
2207
2208static inline bfd_boolean
2209call16_reloc_p (int r_type)
2210{
df58fc94
RS
2211 return (r_type == R_MIPS_CALL16
2212 || r_type == R_MIPS16_CALL16
2213 || r_type == R_MICROMIPS_CALL16);
2214}
2215
2216static inline bfd_boolean
2217got_disp_reloc_p (unsigned int r_type)
2218{
2219 return r_type == R_MIPS_GOT_DISP || r_type == R_MICROMIPS_GOT_DISP;
2220}
2221
2222static inline bfd_boolean
2223got_page_reloc_p (unsigned int r_type)
2224{
2225 return r_type == R_MIPS_GOT_PAGE || r_type == R_MICROMIPS_GOT_PAGE;
2226}
2227
df58fc94
RS
2228static inline bfd_boolean
2229got_lo16_reloc_p (unsigned int r_type)
2230{
2231 return r_type == R_MIPS_GOT_LO16 || r_type == R_MICROMIPS_GOT_LO16;
2232}
2233
2234static inline bfd_boolean
2235call_hi16_reloc_p (unsigned int r_type)
2236{
2237 return r_type == R_MIPS_CALL_HI16 || r_type == R_MICROMIPS_CALL_HI16;
2238}
2239
2240static inline bfd_boolean
2241call_lo16_reloc_p (unsigned int r_type)
2242{
2243 return r_type == R_MIPS_CALL_LO16 || r_type == R_MICROMIPS_CALL_LO16;
738e5348
RS
2244}
2245
2246static inline bfd_boolean
2247hi16_reloc_p (int r_type)
2248{
df58fc94
RS
2249 return (r_type == R_MIPS_HI16
2250 || r_type == R_MIPS16_HI16
7361da2c
AB
2251 || r_type == R_MICROMIPS_HI16
2252 || r_type == R_MIPS_PCHI16);
738e5348 2253}
d6f16593 2254
738e5348
RS
2255static inline bfd_boolean
2256lo16_reloc_p (int r_type)
2257{
df58fc94
RS
2258 return (r_type == R_MIPS_LO16
2259 || r_type == R_MIPS16_LO16
7361da2c
AB
2260 || r_type == R_MICROMIPS_LO16
2261 || r_type == R_MIPS_PCLO16);
738e5348
RS
2262}
2263
2264static inline bfd_boolean
2265mips16_call_reloc_p (int r_type)
2266{
2267 return r_type == R_MIPS16_26 || r_type == R_MIPS16_CALL16;
2268}
d6f16593 2269
38a7df63
CF
2270static inline bfd_boolean
2271jal_reloc_p (int r_type)
2272{
df58fc94
RS
2273 return (r_type == R_MIPS_26
2274 || r_type == R_MIPS16_26
2275 || r_type == R_MICROMIPS_26_S1);
2276}
2277
99aefae6
MR
2278static inline bfd_boolean
2279b_reloc_p (int r_type)
2280{
2281 return (r_type == R_MIPS_PC26_S2
2282 || r_type == R_MIPS_PC21_S2
2283 || r_type == R_MIPS_PC16
c9775dde 2284 || r_type == R_MIPS_GNU_REL16_S2
9d862524
MR
2285 || r_type == R_MIPS16_PC16_S1
2286 || r_type == R_MICROMIPS_PC16_S1
2287 || r_type == R_MICROMIPS_PC10_S1
2288 || r_type == R_MICROMIPS_PC7_S1);
99aefae6
MR
2289}
2290
7361da2c
AB
2291static inline bfd_boolean
2292aligned_pcrel_reloc_p (int r_type)
2293{
2294 return (r_type == R_MIPS_PC18_S3
2295 || r_type == R_MIPS_PC19_S2);
2296}
2297
9d862524
MR
2298static inline bfd_boolean
2299branch_reloc_p (int r_type)
2300{
2301 return (r_type == R_MIPS_26
2302 || r_type == R_MIPS_PC26_S2
2303 || r_type == R_MIPS_PC21_S2
2304 || r_type == R_MIPS_PC16
2305 || r_type == R_MIPS_GNU_REL16_S2);
2306}
2307
c9775dde
MR
2308static inline bfd_boolean
2309mips16_branch_reloc_p (int r_type)
2310{
2311 return (r_type == R_MIPS16_26
2312 || r_type == R_MIPS16_PC16_S1);
2313}
2314
df58fc94
RS
2315static inline bfd_boolean
2316micromips_branch_reloc_p (int r_type)
2317{
2318 return (r_type == R_MICROMIPS_26_S1
2319 || r_type == R_MICROMIPS_PC16_S1
2320 || r_type == R_MICROMIPS_PC10_S1
2321 || r_type == R_MICROMIPS_PC7_S1);
2322}
2323
2324static inline bfd_boolean
2325tls_gd_reloc_p (unsigned int r_type)
2326{
d0f13682
CLT
2327 return (r_type == R_MIPS_TLS_GD
2328 || r_type == R_MIPS16_TLS_GD
2329 || r_type == R_MICROMIPS_TLS_GD);
df58fc94
RS
2330}
2331
2332static inline bfd_boolean
2333tls_ldm_reloc_p (unsigned int r_type)
2334{
d0f13682
CLT
2335 return (r_type == R_MIPS_TLS_LDM
2336 || r_type == R_MIPS16_TLS_LDM
2337 || r_type == R_MICROMIPS_TLS_LDM);
df58fc94
RS
2338}
2339
2340static inline bfd_boolean
2341tls_gottprel_reloc_p (unsigned int r_type)
2342{
d0f13682
CLT
2343 return (r_type == R_MIPS_TLS_GOTTPREL
2344 || r_type == R_MIPS16_TLS_GOTTPREL
2345 || r_type == R_MICROMIPS_TLS_GOTTPREL);
38a7df63
CF
2346}
2347
d6f16593 2348void
df58fc94
RS
2349_bfd_mips_elf_reloc_unshuffle (bfd *abfd, int r_type,
2350 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2351{
df58fc94 2352 bfd_vma first, second, val;
d6f16593 2353
df58fc94 2354 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2355 return;
2356
df58fc94
RS
2357 /* Pick up the first and second halfwords of the instruction. */
2358 first = bfd_get_16 (abfd, data);
2359 second = bfd_get_16 (abfd, data + 2);
2360 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
2361 val = first << 16 | second;
2362 else if (r_type != R_MIPS16_26)
2363 val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
2364 | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
d6f16593 2365 else
df58fc94
RS
2366 val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
2367 | ((first & 0x1f) << 21) | second);
d6f16593
MR
2368 bfd_put_32 (abfd, val, data);
2369}
2370
2371void
df58fc94
RS
2372_bfd_mips_elf_reloc_shuffle (bfd *abfd, int r_type,
2373 bfd_boolean jal_shuffle, bfd_byte *data)
d6f16593 2374{
df58fc94 2375 bfd_vma first, second, val;
d6f16593 2376
df58fc94 2377 if (!mips16_reloc_p (r_type) && !micromips_reloc_shuffle_p (r_type))
d6f16593
MR
2378 return;
2379
2380 val = bfd_get_32 (abfd, data);
df58fc94 2381 if (micromips_reloc_p (r_type) || (r_type == R_MIPS16_26 && !jal_shuffle))
d6f16593 2382 {
df58fc94
RS
2383 second = val & 0xffff;
2384 first = val >> 16;
2385 }
2386 else if (r_type != R_MIPS16_26)
2387 {
2388 second = ((val >> 11) & 0xffe0) | (val & 0x1f);
2389 first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
d6f16593
MR
2390 }
2391 else
2392 {
df58fc94
RS
2393 second = val & 0xffff;
2394 first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
2395 | ((val >> 21) & 0x1f);
d6f16593 2396 }
df58fc94
RS
2397 bfd_put_16 (abfd, second, data + 2);
2398 bfd_put_16 (abfd, first, data);
d6f16593
MR
2399}
2400
b49e97c9 2401bfd_reloc_status_type
9719ad41
RS
2402_bfd_mips_elf_gprel16_with_gp (bfd *abfd, asymbol *symbol,
2403 arelent *reloc_entry, asection *input_section,
2404 bfd_boolean relocatable, void *data, bfd_vma gp)
b49e97c9
TS
2405{
2406 bfd_vma relocation;
a7ebbfdf 2407 bfd_signed_vma val;
30ac9238 2408 bfd_reloc_status_type status;
b49e97c9
TS
2409
2410 if (bfd_is_com_section (symbol->section))
2411 relocation = 0;
2412 else
2413 relocation = symbol->value;
2414
2415 relocation += symbol->section->output_section->vma;
2416 relocation += symbol->section->output_offset;
2417
07515404 2418 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
b49e97c9
TS
2419 return bfd_reloc_outofrange;
2420
b49e97c9 2421 /* Set val to the offset into the section or symbol. */
a7ebbfdf
TS
2422 val = reloc_entry->addend;
2423
30ac9238 2424 _bfd_mips_elf_sign_extend (val, 16);
a7ebbfdf 2425
b49e97c9 2426 /* Adjust val for the final section location and GP value. If we
1049f94e 2427 are producing relocatable output, we don't want to do this for
b49e97c9 2428 an external symbol. */
1049f94e 2429 if (! relocatable
b49e97c9
TS
2430 || (symbol->flags & BSF_SECTION_SYM) != 0)
2431 val += relocation - gp;
2432
a7ebbfdf
TS
2433 if (reloc_entry->howto->partial_inplace)
2434 {
30ac9238
RS
2435 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
2436 (bfd_byte *) data
2437 + reloc_entry->address);
2438 if (status != bfd_reloc_ok)
2439 return status;
a7ebbfdf
TS
2440 }
2441 else
2442 reloc_entry->addend = val;
b49e97c9 2443
1049f94e 2444 if (relocatable)
b49e97c9 2445 reloc_entry->address += input_section->output_offset;
30ac9238
RS
2446
2447 return bfd_reloc_ok;
2448}
2449
2450/* Used to store a REL high-part relocation such as R_MIPS_HI16 or
2451 R_MIPS_GOT16. REL is the relocation, INPUT_SECTION is the section
2452 that contains the relocation field and DATA points to the start of
2453 INPUT_SECTION. */
2454
2455struct mips_hi16
2456{
2457 struct mips_hi16 *next;
2458 bfd_byte *data;
2459 asection *input_section;
2460 arelent rel;
2461};
2462
2463/* FIXME: This should not be a static variable. */
2464
2465static struct mips_hi16 *mips_hi16_list;
2466
2467/* A howto special_function for REL *HI16 relocations. We can only
2468 calculate the correct value once we've seen the partnering
2469 *LO16 relocation, so just save the information for later.
2470
2471 The ABI requires that the *LO16 immediately follow the *HI16.
2472 However, as a GNU extension, we permit an arbitrary number of
2473 *HI16s to be associated with a single *LO16. This significantly
2474 simplies the relocation handling in gcc. */
2475
2476bfd_reloc_status_type
2477_bfd_mips_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2478 asymbol *symbol ATTRIBUTE_UNUSED, void *data,
2479 asection *input_section, bfd *output_bfd,
2480 char **error_message ATTRIBUTE_UNUSED)
2481{
2482 struct mips_hi16 *n;
2483
07515404 2484 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2485 return bfd_reloc_outofrange;
2486
2487 n = bfd_malloc (sizeof *n);
2488 if (n == NULL)
2489 return bfd_reloc_outofrange;
2490
2491 n->next = mips_hi16_list;
2492 n->data = data;
2493 n->input_section = input_section;
2494 n->rel = *reloc_entry;
2495 mips_hi16_list = n;
2496
2497 if (output_bfd != NULL)
2498 reloc_entry->address += input_section->output_offset;
2499
2500 return bfd_reloc_ok;
2501}
2502
738e5348 2503/* A howto special_function for REL R_MIPS*_GOT16 relocations. This is just
30ac9238
RS
2504 like any other 16-bit relocation when applied to global symbols, but is
2505 treated in the same as R_MIPS_HI16 when applied to local symbols. */
2506
2507bfd_reloc_status_type
2508_bfd_mips_elf_got16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2509 void *data, asection *input_section,
2510 bfd *output_bfd, char **error_message)
2511{
2512 if ((symbol->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
e6f7f6d1
AM
2513 || bfd_is_und_section (bfd_asymbol_section (symbol))
2514 || bfd_is_com_section (bfd_asymbol_section (symbol)))
30ac9238
RS
2515 /* The relocation is against a global symbol. */
2516 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2517 input_section, output_bfd,
2518 error_message);
2519
2520 return _bfd_mips_elf_hi16_reloc (abfd, reloc_entry, symbol, data,
2521 input_section, output_bfd, error_message);
2522}
2523
2524/* A howto special_function for REL *LO16 relocations. The *LO16 itself
2525 is a straightforward 16 bit inplace relocation, but we must deal with
2526 any partnering high-part relocations as well. */
2527
2528bfd_reloc_status_type
2529_bfd_mips_elf_lo16_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2530 void *data, asection *input_section,
2531 bfd *output_bfd, char **error_message)
2532{
2533 bfd_vma vallo;
d6f16593 2534 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
30ac9238 2535
07515404 2536 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2537 return bfd_reloc_outofrange;
2538
df58fc94 2539 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
d6f16593 2540 location);
df58fc94
RS
2541 vallo = bfd_get_32 (abfd, location);
2542 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2543 location);
d6f16593 2544
30ac9238
RS
2545 while (mips_hi16_list != NULL)
2546 {
2547 bfd_reloc_status_type ret;
2548 struct mips_hi16 *hi;
2549
2550 hi = mips_hi16_list;
2551
738e5348
RS
2552 /* R_MIPS*_GOT16 relocations are something of a special case. We
2553 want to install the addend in the same way as for a R_MIPS*_HI16
30ac9238
RS
2554 relocation (with a rightshift of 16). However, since GOT16
2555 relocations can also be used with global symbols, their howto
2556 has a rightshift of 0. */
2557 if (hi->rel.howto->type == R_MIPS_GOT16)
2558 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS_HI16, FALSE);
738e5348
RS
2559 else if (hi->rel.howto->type == R_MIPS16_GOT16)
2560 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MIPS16_HI16, FALSE);
df58fc94
RS
2561 else if (hi->rel.howto->type == R_MICROMIPS_GOT16)
2562 hi->rel.howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, R_MICROMIPS_HI16, FALSE);
30ac9238
RS
2563
2564 /* VALLO is a signed 16-bit number. Bias it by 0x8000 so that any
2565 carry or borrow will induce a change of +1 or -1 in the high part. */
2566 hi->rel.addend += (vallo + 0x8000) & 0xffff;
2567
30ac9238
RS
2568 ret = _bfd_mips_elf_generic_reloc (abfd, &hi->rel, symbol, hi->data,
2569 hi->input_section, output_bfd,
2570 error_message);
2571 if (ret != bfd_reloc_ok)
2572 return ret;
2573
2574 mips_hi16_list = hi->next;
2575 free (hi);
2576 }
2577
2578 return _bfd_mips_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2579 input_section, output_bfd,
2580 error_message);
2581}
2582
2583/* A generic howto special_function. This calculates and installs the
2584 relocation itself, thus avoiding the oft-discussed problems in
2585 bfd_perform_relocation and bfd_install_relocation. */
2586
2587bfd_reloc_status_type
2588_bfd_mips_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2589 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2590 asection *input_section, bfd *output_bfd,
2591 char **error_message ATTRIBUTE_UNUSED)
2592{
2593 bfd_signed_vma val;
2594 bfd_reloc_status_type status;
2595 bfd_boolean relocatable;
2596
2597 relocatable = (output_bfd != NULL);
2598
07515404 2599 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
30ac9238
RS
2600 return bfd_reloc_outofrange;
2601
2602 /* Build up the field adjustment in VAL. */
2603 val = 0;
2604 if (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)
2605 {
2606 /* Either we're calculating the final field value or we have a
2607 relocation against a section symbol. Add in the section's
2608 offset or address. */
2609 val += symbol->section->output_section->vma;
2610 val += symbol->section->output_offset;
2611 }
2612
2613 if (!relocatable)
2614 {
2615 /* We're calculating the final field value. Add in the symbol's value
2616 and, if pc-relative, subtract the address of the field itself. */
2617 val += symbol->value;
2618 if (reloc_entry->howto->pc_relative)
2619 {
2620 val -= input_section->output_section->vma;
2621 val -= input_section->output_offset;
2622 val -= reloc_entry->address;
2623 }
2624 }
2625
2626 /* VAL is now the final adjustment. If we're keeping this relocation
2627 in the output file, and if the relocation uses a separate addend,
2628 we just need to add VAL to that addend. Otherwise we need to add
2629 VAL to the relocation field itself. */
2630 if (relocatable && !reloc_entry->howto->partial_inplace)
2631 reloc_entry->addend += val;
2632 else
2633 {
d6f16593
MR
2634 bfd_byte *location = (bfd_byte *) data + reloc_entry->address;
2635
30ac9238
RS
2636 /* Add in the separate addend, if any. */
2637 val += reloc_entry->addend;
2638
2639 /* Add VAL to the relocation field. */
df58fc94
RS
2640 _bfd_mips_elf_reloc_unshuffle (abfd, reloc_entry->howto->type, FALSE,
2641 location);
30ac9238 2642 status = _bfd_relocate_contents (reloc_entry->howto, abfd, val,
d6f16593 2643 location);
df58fc94
RS
2644 _bfd_mips_elf_reloc_shuffle (abfd, reloc_entry->howto->type, FALSE,
2645 location);
d6f16593 2646
30ac9238
RS
2647 if (status != bfd_reloc_ok)
2648 return status;
2649 }
2650
2651 if (relocatable)
2652 reloc_entry->address += input_section->output_offset;
b49e97c9
TS
2653
2654 return bfd_reloc_ok;
2655}
2656\f
2657/* Swap an entry in a .gptab section. Note that these routines rely
2658 on the equivalence of the two elements of the union. */
2659
2660static void
9719ad41
RS
2661bfd_mips_elf32_swap_gptab_in (bfd *abfd, const Elf32_External_gptab *ex,
2662 Elf32_gptab *in)
b49e97c9
TS
2663{
2664 in->gt_entry.gt_g_value = H_GET_32 (abfd, ex->gt_entry.gt_g_value);
2665 in->gt_entry.gt_bytes = H_GET_32 (abfd, ex->gt_entry.gt_bytes);
2666}
2667
2668static void
9719ad41
RS
2669bfd_mips_elf32_swap_gptab_out (bfd *abfd, const Elf32_gptab *in,
2670 Elf32_External_gptab *ex)
b49e97c9
TS
2671{
2672 H_PUT_32 (abfd, in->gt_entry.gt_g_value, ex->gt_entry.gt_g_value);
2673 H_PUT_32 (abfd, in->gt_entry.gt_bytes, ex->gt_entry.gt_bytes);
2674}
2675
2676static void
9719ad41
RS
2677bfd_elf32_swap_compact_rel_out (bfd *abfd, const Elf32_compact_rel *in,
2678 Elf32_External_compact_rel *ex)
b49e97c9
TS
2679{
2680 H_PUT_32 (abfd, in->id1, ex->id1);
2681 H_PUT_32 (abfd, in->num, ex->num);
2682 H_PUT_32 (abfd, in->id2, ex->id2);
2683 H_PUT_32 (abfd, in->offset, ex->offset);
2684 H_PUT_32 (abfd, in->reserved0, ex->reserved0);
2685 H_PUT_32 (abfd, in->reserved1, ex->reserved1);
2686}
2687
2688static void
9719ad41
RS
2689bfd_elf32_swap_crinfo_out (bfd *abfd, const Elf32_crinfo *in,
2690 Elf32_External_crinfo *ex)
b49e97c9
TS
2691{
2692 unsigned long l;
2693
2694 l = (((in->ctype & CRINFO_CTYPE) << CRINFO_CTYPE_SH)
2695 | ((in->rtype & CRINFO_RTYPE) << CRINFO_RTYPE_SH)
2696 | ((in->dist2to & CRINFO_DIST2TO) << CRINFO_DIST2TO_SH)
2697 | ((in->relvaddr & CRINFO_RELVADDR) << CRINFO_RELVADDR_SH));
2698 H_PUT_32 (abfd, l, ex->info);
2699 H_PUT_32 (abfd, in->konst, ex->konst);
2700 H_PUT_32 (abfd, in->vaddr, ex->vaddr);
2701}
b49e97c9
TS
2702\f
2703/* A .reginfo section holds a single Elf32_RegInfo structure. These
2704 routines swap this structure in and out. They are used outside of
2705 BFD, so they are globally visible. */
2706
2707void
9719ad41
RS
2708bfd_mips_elf32_swap_reginfo_in (bfd *abfd, const Elf32_External_RegInfo *ex,
2709 Elf32_RegInfo *in)
b49e97c9
TS
2710{
2711 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2712 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2713 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2714 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2715 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2716 in->ri_gp_value = H_GET_32 (abfd, ex->ri_gp_value);
2717}
2718
2719void
9719ad41
RS
2720bfd_mips_elf32_swap_reginfo_out (bfd *abfd, const Elf32_RegInfo *in,
2721 Elf32_External_RegInfo *ex)
b49e97c9
TS
2722{
2723 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2724 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2725 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2726 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2727 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2728 H_PUT_32 (abfd, in->ri_gp_value, ex->ri_gp_value);
2729}
2730
2731/* In the 64 bit ABI, the .MIPS.options section holds register
2732 information in an Elf64_Reginfo structure. These routines swap
2733 them in and out. They are globally visible because they are used
2734 outside of BFD. These routines are here so that gas can call them
2735 without worrying about whether the 64 bit ABI has been included. */
2736
2737void
9719ad41
RS
2738bfd_mips_elf64_swap_reginfo_in (bfd *abfd, const Elf64_External_RegInfo *ex,
2739 Elf64_Internal_RegInfo *in)
b49e97c9
TS
2740{
2741 in->ri_gprmask = H_GET_32 (abfd, ex->ri_gprmask);
2742 in->ri_pad = H_GET_32 (abfd, ex->ri_pad);
2743 in->ri_cprmask[0] = H_GET_32 (abfd, ex->ri_cprmask[0]);
2744 in->ri_cprmask[1] = H_GET_32 (abfd, ex->ri_cprmask[1]);
2745 in->ri_cprmask[2] = H_GET_32 (abfd, ex->ri_cprmask[2]);
2746 in->ri_cprmask[3] = H_GET_32 (abfd, ex->ri_cprmask[3]);
2747 in->ri_gp_value = H_GET_64 (abfd, ex->ri_gp_value);
2748}
2749
2750void
9719ad41
RS
2751bfd_mips_elf64_swap_reginfo_out (bfd *abfd, const Elf64_Internal_RegInfo *in,
2752 Elf64_External_RegInfo *ex)
b49e97c9
TS
2753{
2754 H_PUT_32 (abfd, in->ri_gprmask, ex->ri_gprmask);
2755 H_PUT_32 (abfd, in->ri_pad, ex->ri_pad);
2756 H_PUT_32 (abfd, in->ri_cprmask[0], ex->ri_cprmask[0]);
2757 H_PUT_32 (abfd, in->ri_cprmask[1], ex->ri_cprmask[1]);
2758 H_PUT_32 (abfd, in->ri_cprmask[2], ex->ri_cprmask[2]);
2759 H_PUT_32 (abfd, in->ri_cprmask[3], ex->ri_cprmask[3]);
2760 H_PUT_64 (abfd, in->ri_gp_value, ex->ri_gp_value);
2761}
2762
2763/* Swap in an options header. */
2764
2765void
9719ad41
RS
2766bfd_mips_elf_swap_options_in (bfd *abfd, const Elf_External_Options *ex,
2767 Elf_Internal_Options *in)
b49e97c9
TS
2768{
2769 in->kind = H_GET_8 (abfd, ex->kind);
2770 in->size = H_GET_8 (abfd, ex->size);
2771 in->section = H_GET_16 (abfd, ex->section);
2772 in->info = H_GET_32 (abfd, ex->info);
2773}
2774
2775/* Swap out an options header. */
2776
2777void
9719ad41
RS
2778bfd_mips_elf_swap_options_out (bfd *abfd, const Elf_Internal_Options *in,
2779 Elf_External_Options *ex)
b49e97c9
TS
2780{
2781 H_PUT_8 (abfd, in->kind, ex->kind);
2782 H_PUT_8 (abfd, in->size, ex->size);
2783 H_PUT_16 (abfd, in->section, ex->section);
2784 H_PUT_32 (abfd, in->info, ex->info);
2785}
351cdf24
MF
2786
2787/* Swap in an abiflags structure. */
2788
2789void
2790bfd_mips_elf_swap_abiflags_v0_in (bfd *abfd,
2791 const Elf_External_ABIFlags_v0 *ex,
2792 Elf_Internal_ABIFlags_v0 *in)
2793{
2794 in->version = H_GET_16 (abfd, ex->version);
2795 in->isa_level = H_GET_8 (abfd, ex->isa_level);
2796 in->isa_rev = H_GET_8 (abfd, ex->isa_rev);
2797 in->gpr_size = H_GET_8 (abfd, ex->gpr_size);
2798 in->cpr1_size = H_GET_8 (abfd, ex->cpr1_size);
2799 in->cpr2_size = H_GET_8 (abfd, ex->cpr2_size);
2800 in->fp_abi = H_GET_8 (abfd, ex->fp_abi);
2801 in->isa_ext = H_GET_32 (abfd, ex->isa_ext);
2802 in->ases = H_GET_32 (abfd, ex->ases);
2803 in->flags1 = H_GET_32 (abfd, ex->flags1);
2804 in->flags2 = H_GET_32 (abfd, ex->flags2);
2805}
2806
2807/* Swap out an abiflags structure. */
2808
2809void
2810bfd_mips_elf_swap_abiflags_v0_out (bfd *abfd,
2811 const Elf_Internal_ABIFlags_v0 *in,
2812 Elf_External_ABIFlags_v0 *ex)
2813{
2814 H_PUT_16 (abfd, in->version, ex->version);
2815 H_PUT_8 (abfd, in->isa_level, ex->isa_level);
2816 H_PUT_8 (abfd, in->isa_rev, ex->isa_rev);
2817 H_PUT_8 (abfd, in->gpr_size, ex->gpr_size);
2818 H_PUT_8 (abfd, in->cpr1_size, ex->cpr1_size);
2819 H_PUT_8 (abfd, in->cpr2_size, ex->cpr2_size);
2820 H_PUT_8 (abfd, in->fp_abi, ex->fp_abi);
2821 H_PUT_32 (abfd, in->isa_ext, ex->isa_ext);
2822 H_PUT_32 (abfd, in->ases, ex->ases);
2823 H_PUT_32 (abfd, in->flags1, ex->flags1);
2824 H_PUT_32 (abfd, in->flags2, ex->flags2);
2825}
b49e97c9
TS
2826\f
2827/* This function is called via qsort() to sort the dynamic relocation
2828 entries by increasing r_symndx value. */
2829
2830static int
9719ad41 2831sort_dynamic_relocs (const void *arg1, const void *arg2)
b49e97c9 2832{
947216bf
AM
2833 Elf_Internal_Rela int_reloc1;
2834 Elf_Internal_Rela int_reloc2;
6870500c 2835 int diff;
b49e97c9 2836
947216bf
AM
2837 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg1, &int_reloc1);
2838 bfd_elf32_swap_reloc_in (reldyn_sorting_bfd, arg2, &int_reloc2);
b49e97c9 2839
6870500c
RS
2840 diff = ELF32_R_SYM (int_reloc1.r_info) - ELF32_R_SYM (int_reloc2.r_info);
2841 if (diff != 0)
2842 return diff;
2843
2844 if (int_reloc1.r_offset < int_reloc2.r_offset)
2845 return -1;
2846 if (int_reloc1.r_offset > int_reloc2.r_offset)
2847 return 1;
2848 return 0;
b49e97c9
TS
2849}
2850
f4416af6
AO
2851/* Like sort_dynamic_relocs, but used for elf64 relocations. */
2852
2853static int
7e3102a7
AM
2854sort_dynamic_relocs_64 (const void *arg1 ATTRIBUTE_UNUSED,
2855 const void *arg2 ATTRIBUTE_UNUSED)
f4416af6 2856{
7e3102a7 2857#ifdef BFD64
f4416af6
AO
2858 Elf_Internal_Rela int_reloc1[3];
2859 Elf_Internal_Rela int_reloc2[3];
2860
2861 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2862 (reldyn_sorting_bfd, arg1, int_reloc1);
2863 (*get_elf_backend_data (reldyn_sorting_bfd)->s->swap_reloc_in)
2864 (reldyn_sorting_bfd, arg2, int_reloc2);
2865
6870500c
RS
2866 if (ELF64_R_SYM (int_reloc1[0].r_info) < ELF64_R_SYM (int_reloc2[0].r_info))
2867 return -1;
2868 if (ELF64_R_SYM (int_reloc1[0].r_info) > ELF64_R_SYM (int_reloc2[0].r_info))
2869 return 1;
2870
2871 if (int_reloc1[0].r_offset < int_reloc2[0].r_offset)
2872 return -1;
2873 if (int_reloc1[0].r_offset > int_reloc2[0].r_offset)
2874 return 1;
2875 return 0;
7e3102a7
AM
2876#else
2877 abort ();
2878#endif
f4416af6
AO
2879}
2880
2881
b49e97c9
TS
2882/* This routine is used to write out ECOFF debugging external symbol
2883 information. It is called via mips_elf_link_hash_traverse. The
2884 ECOFF external symbol information must match the ELF external
2885 symbol information. Unfortunately, at this point we don't know
2886 whether a symbol is required by reloc information, so the two
2887 tables may wind up being different. We must sort out the external
2888 symbol information before we can set the final size of the .mdebug
2889 section, and we must set the size of the .mdebug section before we
2890 can relocate any sections, and we can't know which symbols are
2891 required by relocation until we relocate the sections.
2892 Fortunately, it is relatively unlikely that any symbol will be
2893 stripped but required by a reloc. In particular, it can not happen
2894 when generating a final executable. */
2895
b34976b6 2896static bfd_boolean
9719ad41 2897mips_elf_output_extsym (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 2898{
9719ad41 2899 struct extsym_info *einfo = data;
b34976b6 2900 bfd_boolean strip;
b49e97c9
TS
2901 asection *sec, *output_section;
2902
b49e97c9 2903 if (h->root.indx == -2)
b34976b6 2904 strip = FALSE;
f5385ebf 2905 else if ((h->root.def_dynamic
77cfaee6
AM
2906 || h->root.ref_dynamic
2907 || h->root.type == bfd_link_hash_new)
f5385ebf
AM
2908 && !h->root.def_regular
2909 && !h->root.ref_regular)
b34976b6 2910 strip = TRUE;
b49e97c9
TS
2911 else if (einfo->info->strip == strip_all
2912 || (einfo->info->strip == strip_some
2913 && bfd_hash_lookup (einfo->info->keep_hash,
2914 h->root.root.root.string,
b34976b6
AM
2915 FALSE, FALSE) == NULL))
2916 strip = TRUE;
b49e97c9 2917 else
b34976b6 2918 strip = FALSE;
b49e97c9
TS
2919
2920 if (strip)
b34976b6 2921 return TRUE;
b49e97c9
TS
2922
2923 if (h->esym.ifd == -2)
2924 {
2925 h->esym.jmptbl = 0;
2926 h->esym.cobol_main = 0;
2927 h->esym.weakext = 0;
2928 h->esym.reserved = 0;
2929 h->esym.ifd = ifdNil;
2930 h->esym.asym.value = 0;
2931 h->esym.asym.st = stGlobal;
2932
2933 if (h->root.root.type == bfd_link_hash_undefined
2934 || h->root.root.type == bfd_link_hash_undefweak)
2935 {
2936 const char *name;
2937
2938 /* Use undefined class. Also, set class and type for some
07d6d2b8 2939 special symbols. */
b49e97c9
TS
2940 name = h->root.root.root.string;
2941 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
2942 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
2943 {
2944 h->esym.asym.sc = scData;
2945 h->esym.asym.st = stLabel;
2946 h->esym.asym.value = 0;
2947 }
2948 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
2949 {
2950 h->esym.asym.sc = scAbs;
2951 h->esym.asym.st = stLabel;
2952 h->esym.asym.value =
2953 mips_elf_hash_table (einfo->info)->procedure_count;
2954 }
b49e97c9
TS
2955 else
2956 h->esym.asym.sc = scUndefined;
2957 }
2958 else if (h->root.root.type != bfd_link_hash_defined
2959 && h->root.root.type != bfd_link_hash_defweak)
2960 h->esym.asym.sc = scAbs;
2961 else
2962 {
2963 const char *name;
2964
2965 sec = h->root.root.u.def.section;
2966 output_section = sec->output_section;
2967
2968 /* When making a shared library and symbol h is the one from
2969 the another shared library, OUTPUT_SECTION may be null. */
2970 if (output_section == NULL)
2971 h->esym.asym.sc = scUndefined;
2972 else
2973 {
fd361982 2974 name = bfd_section_name (output_section);
b49e97c9
TS
2975
2976 if (strcmp (name, ".text") == 0)
2977 h->esym.asym.sc = scText;
2978 else if (strcmp (name, ".data") == 0)
2979 h->esym.asym.sc = scData;
2980 else if (strcmp (name, ".sdata") == 0)
2981 h->esym.asym.sc = scSData;
2982 else if (strcmp (name, ".rodata") == 0
2983 || strcmp (name, ".rdata") == 0)
2984 h->esym.asym.sc = scRData;
2985 else if (strcmp (name, ".bss") == 0)
2986 h->esym.asym.sc = scBss;
2987 else if (strcmp (name, ".sbss") == 0)
2988 h->esym.asym.sc = scSBss;
2989 else if (strcmp (name, ".init") == 0)
2990 h->esym.asym.sc = scInit;
2991 else if (strcmp (name, ".fini") == 0)
2992 h->esym.asym.sc = scFini;
2993 else
2994 h->esym.asym.sc = scAbs;
2995 }
2996 }
2997
2998 h->esym.asym.reserved = 0;
2999 h->esym.asym.index = indexNil;
3000 }
3001
3002 if (h->root.root.type == bfd_link_hash_common)
3003 h->esym.asym.value = h->root.root.u.c.size;
3004 else if (h->root.root.type == bfd_link_hash_defined
3005 || h->root.root.type == bfd_link_hash_defweak)
3006 {
3007 if (h->esym.asym.sc == scCommon)
3008 h->esym.asym.sc = scBss;
3009 else if (h->esym.asym.sc == scSCommon)
3010 h->esym.asym.sc = scSBss;
3011
3012 sec = h->root.root.u.def.section;
3013 output_section = sec->output_section;
3014 if (output_section != NULL)
3015 h->esym.asym.value = (h->root.root.u.def.value
3016 + sec->output_offset
3017 + output_section->vma);
3018 else
3019 h->esym.asym.value = 0;
3020 }
33bb52fb 3021 else
b49e97c9
TS
3022 {
3023 struct mips_elf_link_hash_entry *hd = h;
b49e97c9
TS
3024
3025 while (hd->root.root.type == bfd_link_hash_indirect)
33bb52fb 3026 hd = (struct mips_elf_link_hash_entry *)h->root.root.u.i.link;
b49e97c9 3027
33bb52fb 3028 if (hd->needs_lazy_stub)
b49e97c9 3029 {
1bbce132
MR
3030 BFD_ASSERT (hd->root.plt.plist != NULL);
3031 BFD_ASSERT (hd->root.plt.plist->stub_offset != MINUS_ONE);
b49e97c9
TS
3032 /* Set type and value for a symbol with a function stub. */
3033 h->esym.asym.st = stProc;
3034 sec = hd->root.root.u.def.section;
3035 if (sec == NULL)
3036 h->esym.asym.value = 0;
3037 else
3038 {
3039 output_section = sec->output_section;
3040 if (output_section != NULL)
1bbce132 3041 h->esym.asym.value = (hd->root.plt.plist->stub_offset
b49e97c9
TS
3042 + sec->output_offset
3043 + output_section->vma);
3044 else
3045 h->esym.asym.value = 0;
3046 }
b49e97c9
TS
3047 }
3048 }
3049
3050 if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
3051 h->root.root.root.string,
3052 &h->esym))
3053 {
b34976b6
AM
3054 einfo->failed = TRUE;
3055 return FALSE;
b49e97c9
TS
3056 }
3057
b34976b6 3058 return TRUE;
b49e97c9
TS
3059}
3060
3061/* A comparison routine used to sort .gptab entries. */
3062
3063static int
9719ad41 3064gptab_compare (const void *p1, const void *p2)
b49e97c9 3065{
9719ad41
RS
3066 const Elf32_gptab *a1 = p1;
3067 const Elf32_gptab *a2 = p2;
b49e97c9
TS
3068
3069 return a1->gt_entry.gt_g_value - a2->gt_entry.gt_g_value;
3070}
3071\f
b15e6682 3072/* Functions to manage the got entry hash table. */
f4416af6
AO
3073
3074/* Use all 64 bits of a bfd_vma for the computation of a 32-bit
3075 hash number. */
3076
3077static INLINE hashval_t
9719ad41 3078mips_elf_hash_bfd_vma (bfd_vma addr)
f4416af6
AO
3079{
3080#ifdef BFD64
3081 return addr + (addr >> 32);
3082#else
3083 return addr;
3084#endif
3085}
3086
f4416af6 3087static hashval_t
d9bf376d 3088mips_elf_got_entry_hash (const void *entry_)
f4416af6
AO
3089{
3090 const struct mips_got_entry *entry = (struct mips_got_entry *)entry_;
3091
e641e783 3092 return (entry->symndx
9ab066b4
RS
3093 + ((entry->tls_type == GOT_TLS_LDM) << 18)
3094 + (entry->tls_type == GOT_TLS_LDM ? 0
e641e783
RS
3095 : !entry->abfd ? mips_elf_hash_bfd_vma (entry->d.address)
3096 : entry->symndx >= 0 ? (entry->abfd->id
3097 + mips_elf_hash_bfd_vma (entry->d.addend))
3098 : entry->d.h->root.root.root.hash));
f4416af6
AO
3099}
3100
3101static int
3dff0dd1 3102mips_elf_got_entry_eq (const void *entry1, const void *entry2)
f4416af6
AO
3103{
3104 const struct mips_got_entry *e1 = (struct mips_got_entry *)entry1;
3105 const struct mips_got_entry *e2 = (struct mips_got_entry *)entry2;
3106
e641e783 3107 return (e1->symndx == e2->symndx
9ab066b4
RS
3108 && e1->tls_type == e2->tls_type
3109 && (e1->tls_type == GOT_TLS_LDM ? TRUE
e641e783
RS
3110 : !e1->abfd ? !e2->abfd && e1->d.address == e2->d.address
3111 : e1->symndx >= 0 ? (e1->abfd == e2->abfd
3112 && e1->d.addend == e2->d.addend)
3113 : e2->abfd && e1->d.h == e2->d.h));
b15e6682 3114}
c224138d 3115
13db6b44
RS
3116static hashval_t
3117mips_got_page_ref_hash (const void *ref_)
3118{
3119 const struct mips_got_page_ref *ref;
3120
3121 ref = (const struct mips_got_page_ref *) ref_;
3122 return ((ref->symndx >= 0
3123 ? (hashval_t) (ref->u.abfd->id + ref->symndx)
3124 : ref->u.h->root.root.root.hash)
3125 + mips_elf_hash_bfd_vma (ref->addend));
3126}
3127
3128static int
3129mips_got_page_ref_eq (const void *ref1_, const void *ref2_)
3130{
3131 const struct mips_got_page_ref *ref1, *ref2;
3132
3133 ref1 = (const struct mips_got_page_ref *) ref1_;
3134 ref2 = (const struct mips_got_page_ref *) ref2_;
3135 return (ref1->symndx == ref2->symndx
3136 && (ref1->symndx < 0
3137 ? ref1->u.h == ref2->u.h
3138 : ref1->u.abfd == ref2->u.abfd)
3139 && ref1->addend == ref2->addend);
3140}
3141
c224138d
RS
3142static hashval_t
3143mips_got_page_entry_hash (const void *entry_)
3144{
3145 const struct mips_got_page_entry *entry;
3146
3147 entry = (const struct mips_got_page_entry *) entry_;
13db6b44 3148 return entry->sec->id;
c224138d
RS
3149}
3150
3151static int
3152mips_got_page_entry_eq (const void *entry1_, const void *entry2_)
3153{
3154 const struct mips_got_page_entry *entry1, *entry2;
3155
3156 entry1 = (const struct mips_got_page_entry *) entry1_;
3157 entry2 = (const struct mips_got_page_entry *) entry2_;
13db6b44 3158 return entry1->sec == entry2->sec;
c224138d 3159}
b15e6682 3160\f
3dff0dd1 3161/* Create and return a new mips_got_info structure. */
5334aa52
RS
3162
3163static struct mips_got_info *
3dff0dd1 3164mips_elf_create_got_info (bfd *abfd)
5334aa52
RS
3165{
3166 struct mips_got_info *g;
3167
3168 g = bfd_zalloc (abfd, sizeof (struct mips_got_info));
3169 if (g == NULL)
3170 return NULL;
3171
3dff0dd1
RS
3172 g->got_entries = htab_try_create (1, mips_elf_got_entry_hash,
3173 mips_elf_got_entry_eq, NULL);
5334aa52
RS
3174 if (g->got_entries == NULL)
3175 return NULL;
3176
13db6b44
RS
3177 g->got_page_refs = htab_try_create (1, mips_got_page_ref_hash,
3178 mips_got_page_ref_eq, NULL);
3179 if (g->got_page_refs == NULL)
5334aa52
RS
3180 return NULL;
3181
3182 return g;
3183}
3184
ee227692
RS
3185/* Return the GOT info for input bfd ABFD, trying to create a new one if
3186 CREATE_P and if ABFD doesn't already have a GOT. */
3187
3188static struct mips_got_info *
3189mips_elf_bfd_got (bfd *abfd, bfd_boolean create_p)
3190{
3191 struct mips_elf_obj_tdata *tdata;
3192
3193 if (!is_mips_elf (abfd))
3194 return NULL;
3195
3196 tdata = mips_elf_tdata (abfd);
3197 if (!tdata->got && create_p)
3dff0dd1 3198 tdata->got = mips_elf_create_got_info (abfd);
ee227692
RS
3199 return tdata->got;
3200}
3201
d7206569
RS
3202/* Record that ABFD should use output GOT G. */
3203
3204static void
3205mips_elf_replace_bfd_got (bfd *abfd, struct mips_got_info *g)
3206{
3207 struct mips_elf_obj_tdata *tdata;
3208
3209 BFD_ASSERT (is_mips_elf (abfd));
3210 tdata = mips_elf_tdata (abfd);
3211 if (tdata->got)
3212 {
3213 /* The GOT structure itself and the hash table entries are
3214 allocated to a bfd, but the hash tables aren't. */
3215 htab_delete (tdata->got->got_entries);
13db6b44
RS
3216 htab_delete (tdata->got->got_page_refs);
3217 if (tdata->got->got_page_entries)
3218 htab_delete (tdata->got->got_page_entries);
d7206569
RS
3219 }
3220 tdata->got = g;
3221}
3222
0a44bf69
RS
3223/* Return the dynamic relocation section. If it doesn't exist, try to
3224 create a new it if CREATE_P, otherwise return NULL. Also return NULL
3225 if creation fails. */
f4416af6
AO
3226
3227static asection *
0a44bf69 3228mips_elf_rel_dyn_section (struct bfd_link_info *info, bfd_boolean create_p)
f4416af6 3229{
0a44bf69 3230 const char *dname;
f4416af6 3231 asection *sreloc;
0a44bf69 3232 bfd *dynobj;
f4416af6 3233
0a44bf69
RS
3234 dname = MIPS_ELF_REL_DYN_NAME (info);
3235 dynobj = elf_hash_table (info)->dynobj;
3d4d4302 3236 sreloc = bfd_get_linker_section (dynobj, dname);
f4416af6
AO
3237 if (sreloc == NULL && create_p)
3238 {
3d4d4302
AM
3239 sreloc = bfd_make_section_anyway_with_flags (dynobj, dname,
3240 (SEC_ALLOC
3241 | SEC_LOAD
3242 | SEC_HAS_CONTENTS
3243 | SEC_IN_MEMORY
3244 | SEC_LINKER_CREATED
3245 | SEC_READONLY));
f4416af6 3246 if (sreloc == NULL
fd361982
AM
3247 || !bfd_set_section_alignment (sreloc,
3248 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
f4416af6
AO
3249 return NULL;
3250 }
3251 return sreloc;
3252}
3253
e641e783
RS
3254/* Return the GOT_TLS_* type required by relocation type R_TYPE. */
3255
3256static int
3257mips_elf_reloc_tls_type (unsigned int r_type)
3258{
3259 if (tls_gd_reloc_p (r_type))
3260 return GOT_TLS_GD;
3261
3262 if (tls_ldm_reloc_p (r_type))
3263 return GOT_TLS_LDM;
3264
3265 if (tls_gottprel_reloc_p (r_type))
3266 return GOT_TLS_IE;
3267
9ab066b4 3268 return GOT_TLS_NONE;
e641e783
RS
3269}
3270
3271/* Return the number of GOT slots needed for GOT TLS type TYPE. */
3272
3273static int
3274mips_tls_got_entries (unsigned int type)
3275{
3276 switch (type)
3277 {
3278 case GOT_TLS_GD:
3279 case GOT_TLS_LDM:
3280 return 2;
3281
3282 case GOT_TLS_IE:
3283 return 1;
3284
9ab066b4 3285 case GOT_TLS_NONE:
e641e783
RS
3286 return 0;
3287 }
3288 abort ();
3289}
3290
0f20cc35
DJ
3291/* Count the number of relocations needed for a TLS GOT entry, with
3292 access types from TLS_TYPE, and symbol H (or a local symbol if H
3293 is NULL). */
3294
3295static int
3296mips_tls_got_relocs (struct bfd_link_info *info, unsigned char tls_type,
3297 struct elf_link_hash_entry *h)
3298{
3299 int indx = 0;
0f20cc35
DJ
3300 bfd_boolean need_relocs = FALSE;
3301 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
3302
1cb83cac
MR
3303 if (h != NULL
3304 && h->dynindx != -1
3305 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
3306 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
0f20cc35
DJ
3307 indx = h->dynindx;
3308
9143e72c 3309 if ((bfd_link_dll (info) || indx != 0)
0f20cc35
DJ
3310 && (h == NULL
3311 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3312 || h->root.type != bfd_link_hash_undefweak))
3313 need_relocs = TRUE;
3314
3315 if (!need_relocs)
e641e783 3316 return 0;
0f20cc35 3317
9ab066b4 3318 switch (tls_type)
0f20cc35 3319 {
e641e783
RS
3320 case GOT_TLS_GD:
3321 return indx != 0 ? 2 : 1;
0f20cc35 3322
e641e783
RS
3323 case GOT_TLS_IE:
3324 return 1;
0f20cc35 3325
e641e783 3326 case GOT_TLS_LDM:
9143e72c 3327 return bfd_link_dll (info) ? 1 : 0;
0f20cc35 3328
e641e783
RS
3329 default:
3330 return 0;
3331 }
0f20cc35
DJ
3332}
3333
ab361d49
RS
3334/* Add the number of GOT entries and TLS relocations required by ENTRY
3335 to G. */
0f20cc35 3336
ab361d49
RS
3337static void
3338mips_elf_count_got_entry (struct bfd_link_info *info,
3339 struct mips_got_info *g,
3340 struct mips_got_entry *entry)
0f20cc35 3341{
9ab066b4 3342 if (entry->tls_type)
ab361d49 3343 {
9ab066b4
RS
3344 g->tls_gotno += mips_tls_got_entries (entry->tls_type);
3345 g->relocs += mips_tls_got_relocs (info, entry->tls_type,
ab361d49
RS
3346 entry->symndx < 0
3347 ? &entry->d.h->root : NULL);
3348 }
3349 else if (entry->symndx >= 0 || entry->d.h->global_got_area == GGA_NONE)
3350 g->local_gotno += 1;
3351 else
3352 g->global_gotno += 1;
0f20cc35
DJ
3353}
3354
0f20cc35
DJ
3355/* Output a simple dynamic relocation into SRELOC. */
3356
3357static void
3358mips_elf_output_dynamic_relocation (bfd *output_bfd,
3359 asection *sreloc,
861fb55a 3360 unsigned long reloc_index,
0f20cc35
DJ
3361 unsigned long indx,
3362 int r_type,
3363 bfd_vma offset)
3364{
3365 Elf_Internal_Rela rel[3];
3366
3367 memset (rel, 0, sizeof (rel));
3368
3369 rel[0].r_info = ELF_R_INFO (output_bfd, indx, r_type);
3370 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
3371
3372 if (ABI_64_P (output_bfd))
3373 {
3374 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
3375 (output_bfd, &rel[0],
3376 (sreloc->contents
861fb55a 3377 + reloc_index * sizeof (Elf64_Mips_External_Rel)));
0f20cc35
DJ
3378 }
3379 else
3380 bfd_elf32_swap_reloc_out
3381 (output_bfd, &rel[0],
3382 (sreloc->contents
861fb55a 3383 + reloc_index * sizeof (Elf32_External_Rel)));
0f20cc35
DJ
3384}
3385
3386/* Initialize a set of TLS GOT entries for one symbol. */
3387
3388static void
9ab066b4
RS
3389mips_elf_initialize_tls_slots (bfd *abfd, struct bfd_link_info *info,
3390 struct mips_got_entry *entry,
0f20cc35
DJ
3391 struct mips_elf_link_hash_entry *h,
3392 bfd_vma value)
3393{
1cb83cac 3394 bfd_boolean dyn = elf_hash_table (info)->dynamic_sections_created;
23cc69b6 3395 struct mips_elf_link_hash_table *htab;
0f20cc35
DJ
3396 int indx;
3397 asection *sreloc, *sgot;
9ab066b4 3398 bfd_vma got_offset, got_offset2;
0f20cc35
DJ
3399 bfd_boolean need_relocs = FALSE;
3400
23cc69b6 3401 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3402 if (htab == NULL)
3403 return;
3404
ce558b89 3405 sgot = htab->root.sgot;
0f20cc35
DJ
3406
3407 indx = 0;
1cb83cac
MR
3408 if (h != NULL
3409 && h->root.dynindx != -1
3410 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), &h->root)
3411 && (bfd_link_dll (info) || !SYMBOL_REFERENCES_LOCAL (info, &h->root)))
3412 indx = h->root.dynindx;
0f20cc35 3413
9ab066b4 3414 if (entry->tls_initialized)
0f20cc35
DJ
3415 return;
3416
9143e72c 3417 if ((bfd_link_dll (info) || indx != 0)
0f20cc35
DJ
3418 && (h == NULL
3419 || ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
3420 || h->root.type != bfd_link_hash_undefweak))
3421 need_relocs = TRUE;
3422
3423 /* MINUS_ONE means the symbol is not defined in this object. It may not
3424 be defined at all; assume that the value doesn't matter in that
3425 case. Otherwise complain if we would use the value. */
3426 BFD_ASSERT (value != MINUS_ONE || (indx != 0 && need_relocs)
3427 || h->root.root.type == bfd_link_hash_undefweak);
3428
3429 /* Emit necessary relocations. */
0a44bf69 3430 sreloc = mips_elf_rel_dyn_section (info, FALSE);
9ab066b4 3431 got_offset = entry->gotidx;
0f20cc35 3432
9ab066b4 3433 switch (entry->tls_type)
0f20cc35 3434 {
e641e783
RS
3435 case GOT_TLS_GD:
3436 /* General Dynamic. */
3437 got_offset2 = got_offset + MIPS_ELF_GOT_SIZE (abfd);
0f20cc35
DJ
3438
3439 if (need_relocs)
3440 {
3441 mips_elf_output_dynamic_relocation
861fb55a 3442 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3443 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
e641e783 3444 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3445
3446 if (indx)
3447 mips_elf_output_dynamic_relocation
861fb55a 3448 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3449 ABI_64_P (abfd) ? R_MIPS_TLS_DTPREL64 : R_MIPS_TLS_DTPREL32,
e641e783 3450 sgot->output_offset + sgot->output_section->vma + got_offset2);
0f20cc35
DJ
3451 else
3452 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3453 sgot->contents + got_offset2);
0f20cc35
DJ
3454 }
3455 else
3456 {
3457 MIPS_ELF_PUT_WORD (abfd, 1,
e641e783 3458 sgot->contents + got_offset);
0f20cc35 3459 MIPS_ELF_PUT_WORD (abfd, value - dtprel_base (info),
e641e783 3460 sgot->contents + got_offset2);
0f20cc35 3461 }
e641e783 3462 break;
0f20cc35 3463
e641e783
RS
3464 case GOT_TLS_IE:
3465 /* Initial Exec model. */
0f20cc35
DJ
3466 if (need_relocs)
3467 {
3468 if (indx == 0)
3469 MIPS_ELF_PUT_WORD (abfd, value - elf_hash_table (info)->tls_sec->vma,
e641e783 3470 sgot->contents + got_offset);
0f20cc35
DJ
3471 else
3472 MIPS_ELF_PUT_WORD (abfd, 0,
e641e783 3473 sgot->contents + got_offset);
0f20cc35
DJ
3474
3475 mips_elf_output_dynamic_relocation
861fb55a 3476 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35 3477 ABI_64_P (abfd) ? R_MIPS_TLS_TPREL64 : R_MIPS_TLS_TPREL32,
e641e783 3478 sgot->output_offset + sgot->output_section->vma + got_offset);
0f20cc35
DJ
3479 }
3480 else
3481 MIPS_ELF_PUT_WORD (abfd, value - tprel_base (info),
e641e783
RS
3482 sgot->contents + got_offset);
3483 break;
0f20cc35 3484
e641e783 3485 case GOT_TLS_LDM:
0f20cc35
DJ
3486 /* The initial offset is zero, and the LD offsets will include the
3487 bias by DTP_OFFSET. */
3488 MIPS_ELF_PUT_WORD (abfd, 0,
3489 sgot->contents + got_offset
3490 + MIPS_ELF_GOT_SIZE (abfd));
3491
9143e72c 3492 if (!bfd_link_dll (info))
0f20cc35
DJ
3493 MIPS_ELF_PUT_WORD (abfd, 1,
3494 sgot->contents + got_offset);
3495 else
3496 mips_elf_output_dynamic_relocation
861fb55a 3497 (abfd, sreloc, sreloc->reloc_count++, indx,
0f20cc35
DJ
3498 ABI_64_P (abfd) ? R_MIPS_TLS_DTPMOD64 : R_MIPS_TLS_DTPMOD32,
3499 sgot->output_offset + sgot->output_section->vma + got_offset);
e641e783
RS
3500 break;
3501
3502 default:
3503 abort ();
0f20cc35
DJ
3504 }
3505
9ab066b4 3506 entry->tls_initialized = TRUE;
e641e783 3507}
0f20cc35 3508
0a44bf69
RS
3509/* Return the offset from _GLOBAL_OFFSET_TABLE_ of the .got.plt entry
3510 for global symbol H. .got.plt comes before the GOT, so the offset
3511 will be negative. */
3512
3513static bfd_vma
3514mips_elf_gotplt_index (struct bfd_link_info *info,
3515 struct elf_link_hash_entry *h)
3516{
1bbce132 3517 bfd_vma got_address, got_value;
0a44bf69
RS
3518 struct mips_elf_link_hash_table *htab;
3519
3520 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3521 BFD_ASSERT (htab != NULL);
3522
1bbce132
MR
3523 BFD_ASSERT (h->plt.plist != NULL);
3524 BFD_ASSERT (h->plt.plist->gotplt_index != MINUS_ONE);
0a44bf69
RS
3525
3526 /* Calculate the address of the associated .got.plt entry. */
ce558b89
AM
3527 got_address = (htab->root.sgotplt->output_section->vma
3528 + htab->root.sgotplt->output_offset
1bbce132
MR
3529 + (h->plt.plist->gotplt_index
3530 * MIPS_ELF_GOT_SIZE (info->output_bfd)));
0a44bf69
RS
3531
3532 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
3533 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
3534 + htab->root.hgot->root.u.def.section->output_offset
3535 + htab->root.hgot->root.u.def.value);
3536
3537 return got_address - got_value;
3538}
3539
5c18022e 3540/* Return the GOT offset for address VALUE. If there is not yet a GOT
0a44bf69
RS
3541 entry for this value, create one. If R_SYMNDX refers to a TLS symbol,
3542 create a TLS GOT entry instead. Return -1 if no satisfactory GOT
3543 offset can be found. */
b49e97c9
TS
3544
3545static bfd_vma
9719ad41 3546mips_elf_local_got_index (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3547 bfd_vma value, unsigned long r_symndx,
0f20cc35 3548 struct mips_elf_link_hash_entry *h, int r_type)
b49e97c9 3549{
a8028dd0 3550 struct mips_elf_link_hash_table *htab;
b15e6682 3551 struct mips_got_entry *entry;
b49e97c9 3552
a8028dd0 3553 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3554 BFD_ASSERT (htab != NULL);
3555
a8028dd0
RS
3556 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value,
3557 r_symndx, h, r_type);
0f20cc35 3558 if (!entry)
b15e6682 3559 return MINUS_ONE;
0f20cc35 3560
e641e783 3561 if (entry->tls_type)
9ab066b4
RS
3562 mips_elf_initialize_tls_slots (abfd, info, entry, h, value);
3563 return entry->gotidx;
b49e97c9
TS
3564}
3565
13fbec83 3566/* Return the GOT index of global symbol H in the primary GOT. */
b49e97c9
TS
3567
3568static bfd_vma
13fbec83
RS
3569mips_elf_primary_global_got_index (bfd *obfd, struct bfd_link_info *info,
3570 struct elf_link_hash_entry *h)
3571{
3572 struct mips_elf_link_hash_table *htab;
3573 long global_got_dynindx;
3574 struct mips_got_info *g;
3575 bfd_vma got_index;
3576
3577 htab = mips_elf_hash_table (info);
3578 BFD_ASSERT (htab != NULL);
3579
3580 global_got_dynindx = 0;
3581 if (htab->global_gotsym != NULL)
3582 global_got_dynindx = htab->global_gotsym->dynindx;
3583
3584 /* Once we determine the global GOT entry with the lowest dynamic
3585 symbol table index, we must put all dynamic symbols with greater
3586 indices into the primary GOT. That makes it easy to calculate the
3587 GOT offset. */
3588 BFD_ASSERT (h->dynindx >= global_got_dynindx);
3589 g = mips_elf_bfd_got (obfd, FALSE);
3590 got_index = ((h->dynindx - global_got_dynindx + g->local_gotno)
3591 * MIPS_ELF_GOT_SIZE (obfd));
ce558b89 3592 BFD_ASSERT (got_index < htab->root.sgot->size);
13fbec83
RS
3593
3594 return got_index;
3595}
3596
3597/* Return the GOT index for the global symbol indicated by H, which is
3598 referenced by a relocation of type R_TYPE in IBFD. */
3599
3600static bfd_vma
3601mips_elf_global_got_index (bfd *obfd, struct bfd_link_info *info, bfd *ibfd,
3602 struct elf_link_hash_entry *h, int r_type)
b49e97c9 3603{
a8028dd0 3604 struct mips_elf_link_hash_table *htab;
6c42ddb9
RS
3605 struct mips_got_info *g;
3606 struct mips_got_entry lookup, *entry;
3607 bfd_vma gotidx;
b49e97c9 3608
a8028dd0 3609 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3610 BFD_ASSERT (htab != NULL);
3611
6c42ddb9
RS
3612 g = mips_elf_bfd_got (ibfd, FALSE);
3613 BFD_ASSERT (g);
f4416af6 3614
6c42ddb9
RS
3615 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3616 if (!lookup.tls_type && g == mips_elf_bfd_got (obfd, FALSE))
3617 return mips_elf_primary_global_got_index (obfd, info, h);
f4416af6 3618
6c42ddb9
RS
3619 lookup.abfd = ibfd;
3620 lookup.symndx = -1;
3621 lookup.d.h = (struct mips_elf_link_hash_entry *) h;
3622 entry = htab_find (g->got_entries, &lookup);
3623 BFD_ASSERT (entry);
0f20cc35 3624
6c42ddb9 3625 gotidx = entry->gotidx;
ce558b89 3626 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
f4416af6 3627
6c42ddb9 3628 if (lookup.tls_type)
0f20cc35 3629 {
0f20cc35
DJ
3630 bfd_vma value = MINUS_ONE;
3631
3632 if ((h->root.type == bfd_link_hash_defined
3633 || h->root.type == bfd_link_hash_defweak)
3634 && h->root.u.def.section->output_section)
3635 value = (h->root.u.def.value
3636 + h->root.u.def.section->output_offset
3637 + h->root.u.def.section->output_section->vma);
3638
9ab066b4 3639 mips_elf_initialize_tls_slots (obfd, info, entry, lookup.d.h, value);
0f20cc35 3640 }
6c42ddb9 3641 return gotidx;
b49e97c9
TS
3642}
3643
5c18022e
RS
3644/* Find a GOT page entry that points to within 32KB of VALUE. These
3645 entries are supposed to be placed at small offsets in the GOT, i.e.,
3646 within 32KB of GP. Return the index of the GOT entry, or -1 if no
3647 entry could be created. If OFFSETP is nonnull, use it to return the
0a44bf69 3648 offset of the GOT entry from VALUE. */
b49e97c9
TS
3649
3650static bfd_vma
9719ad41 3651mips_elf_got_page (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3652 bfd_vma value, bfd_vma *offsetp)
b49e97c9 3653{
91d6fa6a 3654 bfd_vma page, got_index;
b15e6682 3655 struct mips_got_entry *entry;
b49e97c9 3656
0a44bf69 3657 page = (value + 0x8000) & ~(bfd_vma) 0xffff;
a8028dd0
RS
3658 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, page, 0,
3659 NULL, R_MIPS_GOT_PAGE);
b49e97c9 3660
b15e6682
AO
3661 if (!entry)
3662 return MINUS_ONE;
143d77c5 3663
91d6fa6a 3664 got_index = entry->gotidx;
b49e97c9
TS
3665
3666 if (offsetp)
f4416af6 3667 *offsetp = value - entry->d.address;
b49e97c9 3668
91d6fa6a 3669 return got_index;
b49e97c9
TS
3670}
3671
738e5348 3672/* Find a local GOT entry for an R_MIPS*_GOT16 relocation against VALUE.
020d7251
RS
3673 EXTERNAL is true if the relocation was originally against a global
3674 symbol that binds locally. */
b49e97c9
TS
3675
3676static bfd_vma
9719ad41 3677mips_elf_got16_entry (bfd *abfd, bfd *ibfd, struct bfd_link_info *info,
5c18022e 3678 bfd_vma value, bfd_boolean external)
b49e97c9 3679{
b15e6682 3680 struct mips_got_entry *entry;
b49e97c9 3681
0a44bf69
RS
3682 /* GOT16 relocations against local symbols are followed by a LO16
3683 relocation; those against global symbols are not. Thus if the
3684 symbol was originally local, the GOT16 relocation should load the
3685 equivalent of %hi(VALUE), otherwise it should load VALUE itself. */
b49e97c9 3686 if (! external)
0a44bf69 3687 value = mips_elf_high (value) << 16;
b49e97c9 3688
738e5348
RS
3689 /* It doesn't matter whether the original relocation was R_MIPS_GOT16,
3690 R_MIPS16_GOT16, R_MIPS_CALL16, etc. The format of the entry is the
3691 same in all cases. */
a8028dd0
RS
3692 entry = mips_elf_create_local_got_entry (abfd, info, ibfd, value, 0,
3693 NULL, R_MIPS_GOT16);
b15e6682
AO
3694 if (entry)
3695 return entry->gotidx;
3696 else
3697 return MINUS_ONE;
b49e97c9
TS
3698}
3699
3700/* Returns the offset for the entry at the INDEXth position
3701 in the GOT. */
3702
3703static bfd_vma
a8028dd0 3704mips_elf_got_offset_from_index (struct bfd_link_info *info, bfd *output_bfd,
91d6fa6a 3705 bfd *input_bfd, bfd_vma got_index)
b49e97c9 3706{
a8028dd0 3707 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3708 asection *sgot;
3709 bfd_vma gp;
3710
a8028dd0 3711 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3712 BFD_ASSERT (htab != NULL);
3713
ce558b89 3714 sgot = htab->root.sgot;
f4416af6 3715 gp = _bfd_get_gp_value (output_bfd)
a8028dd0 3716 + mips_elf_adjust_gp (output_bfd, htab->got_info, input_bfd);
143d77c5 3717
91d6fa6a 3718 return sgot->output_section->vma + sgot->output_offset + got_index - gp;
b49e97c9
TS
3719}
3720
0a44bf69
RS
3721/* Create and return a local GOT entry for VALUE, which was calculated
3722 from a symbol belonging to INPUT_SECTON. Return NULL if it could not
3723 be created. If R_SYMNDX refers to a TLS symbol, create a TLS entry
3724 instead. */
b49e97c9 3725
b15e6682 3726static struct mips_got_entry *
0a44bf69 3727mips_elf_create_local_got_entry (bfd *abfd, struct bfd_link_info *info,
a8028dd0 3728 bfd *ibfd, bfd_vma value,
5c18022e 3729 unsigned long r_symndx,
0f20cc35
DJ
3730 struct mips_elf_link_hash_entry *h,
3731 int r_type)
b49e97c9 3732{
ebc53538
RS
3733 struct mips_got_entry lookup, *entry;
3734 void **loc;
f4416af6 3735 struct mips_got_info *g;
0a44bf69 3736 struct mips_elf_link_hash_table *htab;
6c42ddb9 3737 bfd_vma gotidx;
0a44bf69
RS
3738
3739 htab = mips_elf_hash_table (info);
4dfe6ac6 3740 BFD_ASSERT (htab != NULL);
b15e6682 3741
d7206569 3742 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
3743 if (g == NULL)
3744 {
d7206569 3745 g = mips_elf_bfd_got (abfd, FALSE);
f4416af6
AO
3746 BFD_ASSERT (g != NULL);
3747 }
b15e6682 3748
020d7251
RS
3749 /* This function shouldn't be called for symbols that live in the global
3750 area of the GOT. */
3751 BFD_ASSERT (h == NULL || h->global_got_area == GGA_NONE);
0f20cc35 3752
ebc53538
RS
3753 lookup.tls_type = mips_elf_reloc_tls_type (r_type);
3754 if (lookup.tls_type)
3755 {
3756 lookup.abfd = ibfd;
df58fc94 3757 if (tls_ldm_reloc_p (r_type))
0f20cc35 3758 {
ebc53538
RS
3759 lookup.symndx = 0;
3760 lookup.d.addend = 0;
0f20cc35
DJ
3761 }
3762 else if (h == NULL)
3763 {
ebc53538
RS
3764 lookup.symndx = r_symndx;
3765 lookup.d.addend = 0;
0f20cc35
DJ
3766 }
3767 else
ebc53538
RS
3768 {
3769 lookup.symndx = -1;
3770 lookup.d.h = h;
3771 }
0f20cc35 3772
ebc53538
RS
3773 entry = (struct mips_got_entry *) htab_find (g->got_entries, &lookup);
3774 BFD_ASSERT (entry);
0f20cc35 3775
6c42ddb9 3776 gotidx = entry->gotidx;
ce558b89 3777 BFD_ASSERT (gotidx > 0 && gotidx < htab->root.sgot->size);
6c42ddb9 3778
ebc53538 3779 return entry;
0f20cc35
DJ
3780 }
3781
ebc53538
RS
3782 lookup.abfd = NULL;
3783 lookup.symndx = -1;
3784 lookup.d.address = value;
3785 loc = htab_find_slot (g->got_entries, &lookup, INSERT);
3786 if (!loc)
b15e6682 3787 return NULL;
143d77c5 3788
ebc53538
RS
3789 entry = (struct mips_got_entry *) *loc;
3790 if (entry)
3791 return entry;
b15e6682 3792
cb22ccf4 3793 if (g->assigned_low_gotno > g->assigned_high_gotno)
b49e97c9
TS
3794 {
3795 /* We didn't allocate enough space in the GOT. */
4eca0228 3796 _bfd_error_handler
b49e97c9
TS
3797 (_("not enough GOT space for local GOT entries"));
3798 bfd_set_error (bfd_error_bad_value);
b15e6682 3799 return NULL;
b49e97c9
TS
3800 }
3801
ebc53538
RS
3802 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3803 if (!entry)
3804 return NULL;
3805
cb22ccf4
KCY
3806 if (got16_reloc_p (r_type)
3807 || call16_reloc_p (r_type)
3808 || got_page_reloc_p (r_type)
3809 || got_disp_reloc_p (r_type))
3810 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_low_gotno++;
3811 else
3812 lookup.gotidx = MIPS_ELF_GOT_SIZE (abfd) * g->assigned_high_gotno--;
3813
ebc53538
RS
3814 *entry = lookup;
3815 *loc = entry;
3816
ce558b89 3817 MIPS_ELF_PUT_WORD (abfd, value, htab->root.sgot->contents + entry->gotidx);
b15e6682 3818
5c18022e 3819 /* These GOT entries need a dynamic relocation on VxWorks. */
90c14f0c 3820 if (htab->root.target_os == is_vxworks)
0a44bf69
RS
3821 {
3822 Elf_Internal_Rela outrel;
5c18022e 3823 asection *s;
91d6fa6a 3824 bfd_byte *rloc;
0a44bf69 3825 bfd_vma got_address;
0a44bf69
RS
3826
3827 s = mips_elf_rel_dyn_section (info, FALSE);
ce558b89
AM
3828 got_address = (htab->root.sgot->output_section->vma
3829 + htab->root.sgot->output_offset
ebc53538 3830 + entry->gotidx);
0a44bf69 3831
91d6fa6a 3832 rloc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
0a44bf69 3833 outrel.r_offset = got_address;
5c18022e
RS
3834 outrel.r_info = ELF32_R_INFO (STN_UNDEF, R_MIPS_32);
3835 outrel.r_addend = value;
91d6fa6a 3836 bfd_elf32_swap_reloca_out (abfd, &outrel, rloc);
0a44bf69
RS
3837 }
3838
ebc53538 3839 return entry;
b49e97c9
TS
3840}
3841
d4596a51
RS
3842/* Return the number of dynamic section symbols required by OUTPUT_BFD.
3843 The number might be exact or a worst-case estimate, depending on how
3844 much information is available to elf_backend_omit_section_dynsym at
3845 the current linking stage. */
3846
3847static bfd_size_type
3848count_section_dynsyms (bfd *output_bfd, struct bfd_link_info *info)
3849{
3850 bfd_size_type count;
3851
3852 count = 0;
0e1862bb
L
3853 if (bfd_link_pic (info)
3854 || elf_hash_table (info)->is_relocatable_executable)
d4596a51
RS
3855 {
3856 asection *p;
3857 const struct elf_backend_data *bed;
3858
3859 bed = get_elf_backend_data (output_bfd);
3860 for (p = output_bfd->sections; p ; p = p->next)
3861 if ((p->flags & SEC_EXCLUDE) == 0
3862 && (p->flags & SEC_ALLOC) != 0
7f923b7f 3863 && elf_hash_table (info)->dynamic_relocs
d4596a51
RS
3864 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
3865 ++count;
3866 }
3867 return count;
3868}
3869
b49e97c9 3870/* Sort the dynamic symbol table so that symbols that need GOT entries
d4596a51 3871 appear towards the end. */
b49e97c9 3872
b34976b6 3873static bfd_boolean
d4596a51 3874mips_elf_sort_hash_table (bfd *abfd, struct bfd_link_info *info)
b49e97c9 3875{
a8028dd0 3876 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
3877 struct mips_elf_hash_sort_data hsd;
3878 struct mips_got_info *g;
b49e97c9 3879
a8028dd0 3880 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
3881 BFD_ASSERT (htab != NULL);
3882
0f8c4b60 3883 if (htab->root.dynsymcount == 0)
17a80fa8
MR
3884 return TRUE;
3885
a8028dd0 3886 g = htab->got_info;
d4596a51
RS
3887 if (g == NULL)
3888 return TRUE;
f4416af6 3889
b49e97c9 3890 hsd.low = NULL;
23cc69b6
RS
3891 hsd.max_unref_got_dynindx
3892 = hsd.min_got_dynindx
0f8c4b60 3893 = (htab->root.dynsymcount - g->reloc_only_gotno);
e17b0c35
MR
3894 /* Add 1 to local symbol indices to account for the mandatory NULL entry
3895 at the head of the table; see `_bfd_elf_link_renumber_dynsyms'. */
3896 hsd.max_local_dynindx = count_section_dynsyms (abfd, info) + 1;
3897 hsd.max_non_got_dynindx = htab->root.local_dynsymcount + 1;
f16a9783
MS
3898 hsd.output_bfd = abfd;
3899 if (htab->root.dynobj != NULL
3900 && htab->root.dynamic_sections_created
3901 && info->emit_gnu_hash)
3902 {
3903 asection *s = bfd_get_linker_section (htab->root.dynobj, ".MIPS.xhash");
3904 BFD_ASSERT (s != NULL);
3905 hsd.mipsxhash = s->contents;
3906 BFD_ASSERT (hsd.mipsxhash != NULL);
3907 }
3908 else
3909 hsd.mipsxhash = NULL;
0f8c4b60 3910 mips_elf_link_hash_traverse (htab, mips_elf_sort_hash_table_f, &hsd);
b49e97c9
TS
3911
3912 /* There should have been enough room in the symbol table to
44c410de 3913 accommodate both the GOT and non-GOT symbols. */
e17b0c35 3914 BFD_ASSERT (hsd.max_local_dynindx <= htab->root.local_dynsymcount + 1);
b49e97c9 3915 BFD_ASSERT (hsd.max_non_got_dynindx <= hsd.min_got_dynindx);
55f8b9d2 3916 BFD_ASSERT (hsd.max_unref_got_dynindx == htab->root.dynsymcount);
0f8c4b60 3917 BFD_ASSERT (htab->root.dynsymcount - hsd.min_got_dynindx == g->global_gotno);
b49e97c9
TS
3918
3919 /* Now we know which dynamic symbol has the lowest dynamic symbol
3920 table index in the GOT. */
d222d210 3921 htab->global_gotsym = hsd.low;
b49e97c9 3922
b34976b6 3923 return TRUE;
b49e97c9
TS
3924}
3925
3926/* If H needs a GOT entry, assign it the highest available dynamic
3927 index. Otherwise, assign it the lowest available dynamic
3928 index. */
3929
b34976b6 3930static bfd_boolean
9719ad41 3931mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
b49e97c9 3932{
9719ad41 3933 struct mips_elf_hash_sort_data *hsd = data;
b49e97c9 3934
b49e97c9
TS
3935 /* Symbols without dynamic symbol table entries aren't interesting
3936 at all. */
3937 if (h->root.dynindx == -1)
b34976b6 3938 return TRUE;
b49e97c9 3939
634835ae 3940 switch (h->global_got_area)
f4416af6 3941 {
634835ae 3942 case GGA_NONE:
e17b0c35
MR
3943 if (h->root.forced_local)
3944 h->root.dynindx = hsd->max_local_dynindx++;
3945 else
3946 h->root.dynindx = hsd->max_non_got_dynindx++;
634835ae 3947 break;
0f20cc35 3948
634835ae 3949 case GGA_NORMAL:
b49e97c9
TS
3950 h->root.dynindx = --hsd->min_got_dynindx;
3951 hsd->low = (struct elf_link_hash_entry *) h;
634835ae
RS
3952 break;
3953
3954 case GGA_RELOC_ONLY:
634835ae
RS
3955 if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
3956 hsd->low = (struct elf_link_hash_entry *) h;
3957 h->root.dynindx = hsd->max_unref_got_dynindx++;
3958 break;
b49e97c9
TS
3959 }
3960
f16a9783
MS
3961 /* Populate the .MIPS.xhash translation table entry with
3962 the symbol dynindx. */
3963 if (h->mipsxhash_loc != 0 && hsd->mipsxhash != NULL)
3964 bfd_put_32 (hsd->output_bfd, h->root.dynindx,
3965 hsd->mipsxhash + h->mipsxhash_loc);
3966
b34976b6 3967 return TRUE;
b49e97c9
TS
3968}
3969
ee227692
RS
3970/* Record that input bfd ABFD requires a GOT entry like *LOOKUP
3971 (which is owned by the caller and shouldn't be added to the
3972 hash table directly). */
3973
3974static bfd_boolean
3975mips_elf_record_got_entry (struct bfd_link_info *info, bfd *abfd,
3976 struct mips_got_entry *lookup)
3977{
3978 struct mips_elf_link_hash_table *htab;
3979 struct mips_got_entry *entry;
3980 struct mips_got_info *g;
3981 void **loc, **bfd_loc;
3982
3983 /* Make sure there's a slot for this entry in the master GOT. */
3984 htab = mips_elf_hash_table (info);
3985 g = htab->got_info;
3986 loc = htab_find_slot (g->got_entries, lookup, INSERT);
3987 if (!loc)
3988 return FALSE;
3989
3990 /* Populate the entry if it isn't already. */
3991 entry = (struct mips_got_entry *) *loc;
3992 if (!entry)
3993 {
3994 entry = (struct mips_got_entry *) bfd_alloc (abfd, sizeof (*entry));
3995 if (!entry)
3996 return FALSE;
3997
9ab066b4 3998 lookup->tls_initialized = FALSE;
ee227692
RS
3999 lookup->gotidx = -1;
4000 *entry = *lookup;
4001 *loc = entry;
4002 }
4003
4004 /* Reuse the same GOT entry for the BFD's GOT. */
4005 g = mips_elf_bfd_got (abfd, TRUE);
4006 if (!g)
4007 return FALSE;
4008
4009 bfd_loc = htab_find_slot (g->got_entries, lookup, INSERT);
4010 if (!bfd_loc)
4011 return FALSE;
4012
4013 if (!*bfd_loc)
4014 *bfd_loc = entry;
4015 return TRUE;
4016}
4017
e641e783
RS
4018/* ABFD has a GOT relocation of type R_TYPE against H. Reserve a GOT
4019 entry for it. FOR_CALL is true if the caller is only interested in
6ccf4795 4020 using the GOT entry for calls. */
b49e97c9 4021
b34976b6 4022static bfd_boolean
9719ad41
RS
4023mips_elf_record_global_got_symbol (struct elf_link_hash_entry *h,
4024 bfd *abfd, struct bfd_link_info *info,
e641e783 4025 bfd_boolean for_call, int r_type)
b49e97c9 4026{
a8028dd0 4027 struct mips_elf_link_hash_table *htab;
634835ae 4028 struct mips_elf_link_hash_entry *hmips;
ee227692
RS
4029 struct mips_got_entry entry;
4030 unsigned char tls_type;
a8028dd0
RS
4031
4032 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4033 BFD_ASSERT (htab != NULL);
4034
634835ae 4035 hmips = (struct mips_elf_link_hash_entry *) h;
6ccf4795
RS
4036 if (!for_call)
4037 hmips->got_only_for_calls = FALSE;
f4416af6 4038
b49e97c9
TS
4039 /* A global symbol in the GOT must also be in the dynamic symbol
4040 table. */
7c5fcef7
L
4041 if (h->dynindx == -1)
4042 {
4043 switch (ELF_ST_VISIBILITY (h->other))
4044 {
4045 case STV_INTERNAL:
4046 case STV_HIDDEN:
47275900 4047 _bfd_mips_elf_hide_symbol (info, h, TRUE);
7c5fcef7
L
4048 break;
4049 }
c152c796 4050 if (!bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 4051 return FALSE;
7c5fcef7 4052 }
b49e97c9 4053
ee227692 4054 tls_type = mips_elf_reloc_tls_type (r_type);
9ab066b4 4055 if (tls_type == GOT_TLS_NONE && hmips->global_got_area > GGA_NORMAL)
ee227692 4056 hmips->global_got_area = GGA_NORMAL;
86324f90 4057
f4416af6
AO
4058 entry.abfd = abfd;
4059 entry.symndx = -1;
4060 entry.d.h = (struct mips_elf_link_hash_entry *) h;
ee227692
RS
4061 entry.tls_type = tls_type;
4062 return mips_elf_record_got_entry (info, abfd, &entry);
b49e97c9 4063}
f4416af6 4064
e641e783
RS
4065/* ABFD has a GOT relocation of type R_TYPE against symbol SYMNDX + ADDEND,
4066 where SYMNDX is a local symbol. Reserve a GOT entry for it. */
f4416af6
AO
4067
4068static bfd_boolean
9719ad41 4069mips_elf_record_local_got_symbol (bfd *abfd, long symndx, bfd_vma addend,
e641e783 4070 struct bfd_link_info *info, int r_type)
f4416af6 4071{
a8028dd0
RS
4072 struct mips_elf_link_hash_table *htab;
4073 struct mips_got_info *g;
ee227692 4074 struct mips_got_entry entry;
f4416af6 4075
a8028dd0 4076 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4077 BFD_ASSERT (htab != NULL);
4078
a8028dd0
RS
4079 g = htab->got_info;
4080 BFD_ASSERT (g != NULL);
4081
f4416af6
AO
4082 entry.abfd = abfd;
4083 entry.symndx = symndx;
4084 entry.d.addend = addend;
e641e783 4085 entry.tls_type = mips_elf_reloc_tls_type (r_type);
ee227692 4086 return mips_elf_record_got_entry (info, abfd, &entry);
f4416af6 4087}
c224138d 4088
13db6b44
RS
4089/* Record that ABFD has a page relocation against SYMNDX + ADDEND.
4090 H is the symbol's hash table entry, or null if SYMNDX is local
4091 to ABFD. */
c224138d
RS
4092
4093static bfd_boolean
13db6b44
RS
4094mips_elf_record_got_page_ref (struct bfd_link_info *info, bfd *abfd,
4095 long symndx, struct elf_link_hash_entry *h,
4096 bfd_signed_vma addend)
c224138d 4097{
a8028dd0 4098 struct mips_elf_link_hash_table *htab;
ee227692 4099 struct mips_got_info *g1, *g2;
13db6b44 4100 struct mips_got_page_ref lookup, *entry;
ee227692 4101 void **loc, **bfd_loc;
c224138d 4102
a8028dd0 4103 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4104 BFD_ASSERT (htab != NULL);
4105
ee227692
RS
4106 g1 = htab->got_info;
4107 BFD_ASSERT (g1 != NULL);
a8028dd0 4108
13db6b44
RS
4109 if (h)
4110 {
4111 lookup.symndx = -1;
4112 lookup.u.h = (struct mips_elf_link_hash_entry *) h;
4113 }
4114 else
4115 {
4116 lookup.symndx = symndx;
4117 lookup.u.abfd = abfd;
4118 }
4119 lookup.addend = addend;
4120 loc = htab_find_slot (g1->got_page_refs, &lookup, INSERT);
c224138d
RS
4121 if (loc == NULL)
4122 return FALSE;
4123
13db6b44 4124 entry = (struct mips_got_page_ref *) *loc;
c224138d
RS
4125 if (!entry)
4126 {
4127 entry = bfd_alloc (abfd, sizeof (*entry));
4128 if (!entry)
4129 return FALSE;
4130
13db6b44 4131 *entry = lookup;
c224138d
RS
4132 *loc = entry;
4133 }
4134
ee227692
RS
4135 /* Add the same entry to the BFD's GOT. */
4136 g2 = mips_elf_bfd_got (abfd, TRUE);
4137 if (!g2)
4138 return FALSE;
4139
13db6b44 4140 bfd_loc = htab_find_slot (g2->got_page_refs, &lookup, INSERT);
ee227692
RS
4141 if (!bfd_loc)
4142 return FALSE;
4143
4144 if (!*bfd_loc)
4145 *bfd_loc = entry;
4146
c224138d
RS
4147 return TRUE;
4148}
33bb52fb
RS
4149
4150/* Add room for N relocations to the .rel(a).dyn section in ABFD. */
4151
4152static void
4153mips_elf_allocate_dynamic_relocations (bfd *abfd, struct bfd_link_info *info,
4154 unsigned int n)
4155{
4156 asection *s;
4157 struct mips_elf_link_hash_table *htab;
4158
4159 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4160 BFD_ASSERT (htab != NULL);
4161
33bb52fb
RS
4162 s = mips_elf_rel_dyn_section (info, FALSE);
4163 BFD_ASSERT (s != NULL);
4164
90c14f0c 4165 if (htab->root.target_os == is_vxworks)
33bb52fb
RS
4166 s->size += n * MIPS_ELF_RELA_SIZE (abfd);
4167 else
4168 {
4169 if (s->size == 0)
4170 {
4171 /* Make room for a null element. */
4172 s->size += MIPS_ELF_REL_SIZE (abfd);
4173 ++s->reloc_count;
4174 }
4175 s->size += n * MIPS_ELF_REL_SIZE (abfd);
4176 }
4177}
4178\f
476366af
RS
4179/* A htab_traverse callback for GOT entries, with DATA pointing to a
4180 mips_elf_traverse_got_arg structure. Count the number of GOT
4181 entries and TLS relocs. Set DATA->value to true if we need
4182 to resolve indirect or warning symbols and then recreate the GOT. */
33bb52fb
RS
4183
4184static int
4185mips_elf_check_recreate_got (void **entryp, void *data)
4186{
4187 struct mips_got_entry *entry;
476366af 4188 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4189
4190 entry = (struct mips_got_entry *) *entryp;
476366af 4191 arg = (struct mips_elf_traverse_got_arg *) data;
33bb52fb
RS
4192 if (entry->abfd != NULL && entry->symndx == -1)
4193 {
4194 struct mips_elf_link_hash_entry *h;
4195
4196 h = entry->d.h;
4197 if (h->root.root.type == bfd_link_hash_indirect
4198 || h->root.root.type == bfd_link_hash_warning)
4199 {
476366af 4200 arg->value = TRUE;
33bb52fb
RS
4201 return 0;
4202 }
4203 }
476366af 4204 mips_elf_count_got_entry (arg->info, arg->g, entry);
33bb52fb
RS
4205 return 1;
4206}
4207
476366af
RS
4208/* A htab_traverse callback for GOT entries, with DATA pointing to a
4209 mips_elf_traverse_got_arg structure. Add all entries to DATA->g,
4210 converting entries for indirect and warning symbols into entries
4211 for the target symbol. Set DATA->g to null on error. */
33bb52fb
RS
4212
4213static int
4214mips_elf_recreate_got (void **entryp, void *data)
4215{
72e7511a 4216 struct mips_got_entry new_entry, *entry;
476366af 4217 struct mips_elf_traverse_got_arg *arg;
33bb52fb
RS
4218 void **slot;
4219
33bb52fb 4220 entry = (struct mips_got_entry *) *entryp;
476366af 4221 arg = (struct mips_elf_traverse_got_arg *) data;
72e7511a
RS
4222 if (entry->abfd != NULL
4223 && entry->symndx == -1
4224 && (entry->d.h->root.root.type == bfd_link_hash_indirect
4225 || entry->d.h->root.root.type == bfd_link_hash_warning))
33bb52fb
RS
4226 {
4227 struct mips_elf_link_hash_entry *h;
4228
72e7511a
RS
4229 new_entry = *entry;
4230 entry = &new_entry;
33bb52fb 4231 h = entry->d.h;
72e7511a 4232 do
634835ae
RS
4233 {
4234 BFD_ASSERT (h->global_got_area == GGA_NONE);
4235 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
4236 }
72e7511a
RS
4237 while (h->root.root.type == bfd_link_hash_indirect
4238 || h->root.root.type == bfd_link_hash_warning);
33bb52fb
RS
4239 entry->d.h = h;
4240 }
476366af 4241 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
33bb52fb
RS
4242 if (slot == NULL)
4243 {
476366af 4244 arg->g = NULL;
33bb52fb
RS
4245 return 0;
4246 }
4247 if (*slot == NULL)
72e7511a
RS
4248 {
4249 if (entry == &new_entry)
4250 {
4251 entry = bfd_alloc (entry->abfd, sizeof (*entry));
4252 if (!entry)
4253 {
476366af 4254 arg->g = NULL;
72e7511a
RS
4255 return 0;
4256 }
4257 *entry = new_entry;
4258 }
4259 *slot = entry;
476366af 4260 mips_elf_count_got_entry (arg->info, arg->g, entry);
72e7511a 4261 }
33bb52fb
RS
4262 return 1;
4263}
4264
13db6b44
RS
4265/* Return the maximum number of GOT page entries required for RANGE. */
4266
4267static bfd_vma
4268mips_elf_pages_for_range (const struct mips_got_page_range *range)
4269{
4270 return (range->max_addend - range->min_addend + 0x1ffff) >> 16;
4271}
4272
4273/* Record that G requires a page entry that can reach SEC + ADDEND. */
4274
4275static bfd_boolean
b75d42bc 4276mips_elf_record_got_page_entry (struct mips_elf_traverse_got_arg *arg,
13db6b44
RS
4277 asection *sec, bfd_signed_vma addend)
4278{
b75d42bc 4279 struct mips_got_info *g = arg->g;
13db6b44
RS
4280 struct mips_got_page_entry lookup, *entry;
4281 struct mips_got_page_range **range_ptr, *range;
4282 bfd_vma old_pages, new_pages;
4283 void **loc;
4284
4285 /* Find the mips_got_page_entry hash table entry for this section. */
4286 lookup.sec = sec;
4287 loc = htab_find_slot (g->got_page_entries, &lookup, INSERT);
4288 if (loc == NULL)
4289 return FALSE;
4290
4291 /* Create a mips_got_page_entry if this is the first time we've
4292 seen the section. */
4293 entry = (struct mips_got_page_entry *) *loc;
4294 if (!entry)
4295 {
b75d42bc 4296 entry = bfd_zalloc (arg->info->output_bfd, sizeof (*entry));
13db6b44
RS
4297 if (!entry)
4298 return FALSE;
4299
4300 entry->sec = sec;
4301 *loc = entry;
4302 }
4303
4304 /* Skip over ranges whose maximum extent cannot share a page entry
4305 with ADDEND. */
4306 range_ptr = &entry->ranges;
4307 while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
4308 range_ptr = &(*range_ptr)->next;
4309
4310 /* If we scanned to the end of the list, or found a range whose
4311 minimum extent cannot share a page entry with ADDEND, create
4312 a new singleton range. */
4313 range = *range_ptr;
4314 if (!range || addend < range->min_addend - 0xffff)
4315 {
b75d42bc 4316 range = bfd_zalloc (arg->info->output_bfd, sizeof (*range));
13db6b44
RS
4317 if (!range)
4318 return FALSE;
4319
4320 range->next = *range_ptr;
4321 range->min_addend = addend;
4322 range->max_addend = addend;
4323
4324 *range_ptr = range;
4325 entry->num_pages++;
4326 g->page_gotno++;
4327 return TRUE;
4328 }
4329
4330 /* Remember how many pages the old range contributed. */
4331 old_pages = mips_elf_pages_for_range (range);
4332
4333 /* Update the ranges. */
4334 if (addend < range->min_addend)
4335 range->min_addend = addend;
4336 else if (addend > range->max_addend)
4337 {
4338 if (range->next && addend >= range->next->min_addend - 0xffff)
4339 {
4340 old_pages += mips_elf_pages_for_range (range->next);
4341 range->max_addend = range->next->max_addend;
4342 range->next = range->next->next;
4343 }
4344 else
4345 range->max_addend = addend;
4346 }
4347
4348 /* Record any change in the total estimate. */
4349 new_pages = mips_elf_pages_for_range (range);
4350 if (old_pages != new_pages)
4351 {
4352 entry->num_pages += new_pages - old_pages;
4353 g->page_gotno += new_pages - old_pages;
4354 }
4355
4356 return TRUE;
4357}
4358
4359/* A htab_traverse callback for which *REFP points to a mips_got_page_ref
4360 and for which DATA points to a mips_elf_traverse_got_arg. Work out
4361 whether the page reference described by *REFP needs a GOT page entry,
4362 and record that entry in DATA->g if so. Set DATA->g to null on failure. */
4363
4364static bfd_boolean
4365mips_elf_resolve_got_page_ref (void **refp, void *data)
4366{
4367 struct mips_got_page_ref *ref;
4368 struct mips_elf_traverse_got_arg *arg;
4369 struct mips_elf_link_hash_table *htab;
4370 asection *sec;
4371 bfd_vma addend;
4372
4373 ref = (struct mips_got_page_ref *) *refp;
4374 arg = (struct mips_elf_traverse_got_arg *) data;
4375 htab = mips_elf_hash_table (arg->info);
4376
4377 if (ref->symndx < 0)
4378 {
4379 struct mips_elf_link_hash_entry *h;
4380
4381 /* Global GOT_PAGEs decay to GOT_DISP and so don't need page entries. */
4382 h = ref->u.h;
4383 if (!SYMBOL_REFERENCES_LOCAL (arg->info, &h->root))
4384 return 1;
4385
4386 /* Ignore undefined symbols; we'll issue an error later if
4387 appropriate. */
4388 if (!((h->root.root.type == bfd_link_hash_defined
4389 || h->root.root.type == bfd_link_hash_defweak)
4390 && h->root.root.u.def.section))
4391 return 1;
4392
4393 sec = h->root.root.u.def.section;
4394 addend = h->root.root.u.def.value + ref->addend;
4395 }
4396 else
4397 {
4398 Elf_Internal_Sym *isym;
4399
4400 /* Read in the symbol. */
f1dfbfdb 4401 isym = bfd_sym_from_r_symndx (&htab->root.sym_cache, ref->u.abfd,
13db6b44
RS
4402 ref->symndx);
4403 if (isym == NULL)
4404 {
4405 arg->g = NULL;
4406 return 0;
4407 }
4408
4409 /* Get the associated input section. */
4410 sec = bfd_section_from_elf_index (ref->u.abfd, isym->st_shndx);
4411 if (sec == NULL)
4412 {
4413 arg->g = NULL;
4414 return 0;
4415 }
4416
4417 /* If this is a mergable section, work out the section and offset
4418 of the merged data. For section symbols, the addend specifies
4419 of the offset _of_ the first byte in the data, otherwise it
4420 specifies the offset _from_ the first byte. */
4421 if (sec->flags & SEC_MERGE)
4422 {
4423 void *secinfo;
4424
4425 secinfo = elf_section_data (sec)->sec_info;
4426 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
4427 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4428 isym->st_value + ref->addend);
4429 else
4430 addend = _bfd_merged_section_offset (ref->u.abfd, &sec, secinfo,
4431 isym->st_value) + ref->addend;
4432 }
4433 else
4434 addend = isym->st_value + ref->addend;
4435 }
b75d42bc 4436 if (!mips_elf_record_got_page_entry (arg, sec, addend))
13db6b44
RS
4437 {
4438 arg->g = NULL;
4439 return 0;
4440 }
4441 return 1;
4442}
4443
33bb52fb 4444/* If any entries in G->got_entries are for indirect or warning symbols,
13db6b44
RS
4445 replace them with entries for the target symbol. Convert g->got_page_refs
4446 into got_page_entry structures and estimate the number of page entries
4447 that they require. */
33bb52fb
RS
4448
4449static bfd_boolean
476366af
RS
4450mips_elf_resolve_final_got_entries (struct bfd_link_info *info,
4451 struct mips_got_info *g)
33bb52fb 4452{
476366af
RS
4453 struct mips_elf_traverse_got_arg tga;
4454 struct mips_got_info oldg;
4455
4456 oldg = *g;
33bb52fb 4457
476366af
RS
4458 tga.info = info;
4459 tga.g = g;
4460 tga.value = FALSE;
4461 htab_traverse (g->got_entries, mips_elf_check_recreate_got, &tga);
4462 if (tga.value)
33bb52fb 4463 {
476366af
RS
4464 *g = oldg;
4465 g->got_entries = htab_create (htab_size (oldg.got_entries),
4466 mips_elf_got_entry_hash,
4467 mips_elf_got_entry_eq, NULL);
4468 if (!g->got_entries)
33bb52fb
RS
4469 return FALSE;
4470
476366af
RS
4471 htab_traverse (oldg.got_entries, mips_elf_recreate_got, &tga);
4472 if (!tga.g)
4473 return FALSE;
4474
4475 htab_delete (oldg.got_entries);
33bb52fb 4476 }
13db6b44
RS
4477
4478 g->got_page_entries = htab_try_create (1, mips_got_page_entry_hash,
4479 mips_got_page_entry_eq, NULL);
4480 if (g->got_page_entries == NULL)
4481 return FALSE;
4482
4483 tga.info = info;
4484 tga.g = g;
4485 htab_traverse (g->got_page_refs, mips_elf_resolve_got_page_ref, &tga);
4486
33bb52fb
RS
4487 return TRUE;
4488}
4489
c5d6fa44
RS
4490/* Return true if a GOT entry for H should live in the local rather than
4491 global GOT area. */
4492
4493static bfd_boolean
4494mips_use_local_got_p (struct bfd_link_info *info,
4495 struct mips_elf_link_hash_entry *h)
4496{
4497 /* Symbols that aren't in the dynamic symbol table must live in the
4498 local GOT. This includes symbols that are completely undefined
4499 and which therefore don't bind locally. We'll report undefined
4500 symbols later if appropriate. */
4501 if (h->root.dynindx == -1)
4502 return TRUE;
4503
47275900
MR
4504 /* Absolute symbols, if ever they need a GOT entry, cannot ever go
4505 to the local GOT, as they would be implicitly relocated by the
4506 base address by the dynamic loader. */
4507 if (bfd_is_abs_symbol (&h->root.root))
4508 return FALSE;
4509
c5d6fa44
RS
4510 /* Symbols that bind locally can (and in the case of forced-local
4511 symbols, must) live in the local GOT. */
4512 if (h->got_only_for_calls
4513 ? SYMBOL_CALLS_LOCAL (info, &h->root)
4514 : SYMBOL_REFERENCES_LOCAL (info, &h->root))
4515 return TRUE;
4516
4517 /* If this is an executable that must provide a definition of the symbol,
4518 either though PLTs or copy relocations, then that address should go in
4519 the local rather than global GOT. */
0e1862bb 4520 if (bfd_link_executable (info) && h->has_static_relocs)
c5d6fa44
RS
4521 return TRUE;
4522
4523 return FALSE;
4524}
4525
6c42ddb9
RS
4526/* A mips_elf_link_hash_traverse callback for which DATA points to the
4527 link_info structure. Decide whether the hash entry needs an entry in
4528 the global part of the primary GOT, setting global_got_area accordingly.
4529 Count the number of global symbols that are in the primary GOT only
4530 because they have relocations against them (reloc_only_gotno). */
33bb52fb
RS
4531
4532static int
d4596a51 4533mips_elf_count_got_symbols (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 4534{
020d7251 4535 struct bfd_link_info *info;
6ccf4795 4536 struct mips_elf_link_hash_table *htab;
33bb52fb
RS
4537 struct mips_got_info *g;
4538
020d7251 4539 info = (struct bfd_link_info *) data;
6ccf4795
RS
4540 htab = mips_elf_hash_table (info);
4541 g = htab->got_info;
d4596a51 4542 if (h->global_got_area != GGA_NONE)
33bb52fb 4543 {
020d7251 4544 /* Make a final decision about whether the symbol belongs in the
c5d6fa44
RS
4545 local or global GOT. */
4546 if (mips_use_local_got_p (info, h))
6c42ddb9
RS
4547 /* The symbol belongs in the local GOT. We no longer need this
4548 entry if it was only used for relocations; those relocations
4549 will be against the null or section symbol instead of H. */
4550 h->global_got_area = GGA_NONE;
90c14f0c 4551 else if (htab->root.target_os == is_vxworks
6ccf4795 4552 && h->got_only_for_calls
1bbce132 4553 && h->root.plt.plist->mips_offset != MINUS_ONE)
6ccf4795
RS
4554 /* On VxWorks, calls can refer directly to the .got.plt entry;
4555 they don't need entries in the regular GOT. .got.plt entries
4556 will be allocated by _bfd_mips_elf_adjust_dynamic_symbol. */
4557 h->global_got_area = GGA_NONE;
6c42ddb9 4558 else if (h->global_got_area == GGA_RELOC_ONLY)
23cc69b6 4559 {
6c42ddb9 4560 g->reloc_only_gotno++;
23cc69b6 4561 g->global_gotno++;
23cc69b6 4562 }
33bb52fb
RS
4563 }
4564 return 1;
4565}
f4416af6 4566\f
d7206569
RS
4567/* A htab_traverse callback for GOT entries. Add each one to the GOT
4568 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
f4416af6
AO
4569
4570static int
d7206569 4571mips_elf_add_got_entry (void **entryp, void *data)
f4416af6 4572{
d7206569
RS
4573 struct mips_got_entry *entry;
4574 struct mips_elf_traverse_got_arg *arg;
4575 void **slot;
f4416af6 4576
d7206569
RS
4577 entry = (struct mips_got_entry *) *entryp;
4578 arg = (struct mips_elf_traverse_got_arg *) data;
4579 slot = htab_find_slot (arg->g->got_entries, entry, INSERT);
4580 if (!slot)
f4416af6 4581 {
d7206569
RS
4582 arg->g = NULL;
4583 return 0;
f4416af6 4584 }
d7206569 4585 if (!*slot)
c224138d 4586 {
d7206569
RS
4587 *slot = entry;
4588 mips_elf_count_got_entry (arg->info, arg->g, entry);
c224138d 4589 }
f4416af6
AO
4590 return 1;
4591}
4592
d7206569
RS
4593/* A htab_traverse callback for GOT page entries. Add each one to the GOT
4594 given in mips_elf_traverse_got_arg DATA. Clear DATA->G on error. */
c224138d
RS
4595
4596static int
d7206569 4597mips_elf_add_got_page_entry (void **entryp, void *data)
c224138d 4598{
d7206569
RS
4599 struct mips_got_page_entry *entry;
4600 struct mips_elf_traverse_got_arg *arg;
4601 void **slot;
c224138d 4602
d7206569
RS
4603 entry = (struct mips_got_page_entry *) *entryp;
4604 arg = (struct mips_elf_traverse_got_arg *) data;
4605 slot = htab_find_slot (arg->g->got_page_entries, entry, INSERT);
4606 if (!slot)
c224138d 4607 {
d7206569 4608 arg->g = NULL;
c224138d
RS
4609 return 0;
4610 }
d7206569
RS
4611 if (!*slot)
4612 {
4613 *slot = entry;
4614 arg->g->page_gotno += entry->num_pages;
4615 }
c224138d
RS
4616 return 1;
4617}
4618
d7206569
RS
4619/* Consider merging FROM, which is ABFD's GOT, into TO. Return -1 if
4620 this would lead to overflow, 1 if they were merged successfully,
4621 and 0 if a merge failed due to lack of memory. (These values are chosen
4622 so that nonnegative return values can be returned by a htab_traverse
4623 callback.) */
c224138d
RS
4624
4625static int
d7206569 4626mips_elf_merge_got_with (bfd *abfd, struct mips_got_info *from,
c224138d
RS
4627 struct mips_got_info *to,
4628 struct mips_elf_got_per_bfd_arg *arg)
4629{
d7206569 4630 struct mips_elf_traverse_got_arg tga;
c224138d
RS
4631 unsigned int estimate;
4632
4633 /* Work out how many page entries we would need for the combined GOT. */
4634 estimate = arg->max_pages;
4635 if (estimate >= from->page_gotno + to->page_gotno)
4636 estimate = from->page_gotno + to->page_gotno;
4637
e2ece73c 4638 /* And conservatively estimate how many local and TLS entries
c224138d 4639 would be needed. */
e2ece73c
RS
4640 estimate += from->local_gotno + to->local_gotno;
4641 estimate += from->tls_gotno + to->tls_gotno;
4642
17214937
RS
4643 /* If we're merging with the primary got, any TLS relocations will
4644 come after the full set of global entries. Otherwise estimate those
e2ece73c 4645 conservatively as well. */
17214937 4646 if (to == arg->primary && from->tls_gotno + to->tls_gotno)
e2ece73c
RS
4647 estimate += arg->global_count;
4648 else
4649 estimate += from->global_gotno + to->global_gotno;
c224138d
RS
4650
4651 /* Bail out if the combined GOT might be too big. */
4652 if (estimate > arg->max_count)
4653 return -1;
4654
c224138d 4655 /* Transfer the bfd's got information from FROM to TO. */
d7206569
RS
4656 tga.info = arg->info;
4657 tga.g = to;
4658 htab_traverse (from->got_entries, mips_elf_add_got_entry, &tga);
4659 if (!tga.g)
c224138d
RS
4660 return 0;
4661
d7206569
RS
4662 htab_traverse (from->got_page_entries, mips_elf_add_got_page_entry, &tga);
4663 if (!tga.g)
c224138d
RS
4664 return 0;
4665
d7206569 4666 mips_elf_replace_bfd_got (abfd, to);
c224138d
RS
4667 return 1;
4668}
4669
d7206569 4670/* Attempt to merge GOT G, which belongs to ABFD. Try to use as much
f4416af6
AO
4671 as possible of the primary got, since it doesn't require explicit
4672 dynamic relocations, but don't use bfds that would reference global
4673 symbols out of the addressable range. Failing the primary got,
4674 attempt to merge with the current got, or finish the current got
4675 and then make make the new got current. */
4676
d7206569
RS
4677static bfd_boolean
4678mips_elf_merge_got (bfd *abfd, struct mips_got_info *g,
4679 struct mips_elf_got_per_bfd_arg *arg)
f4416af6 4680{
c224138d
RS
4681 unsigned int estimate;
4682 int result;
4683
476366af 4684 if (!mips_elf_resolve_final_got_entries (arg->info, g))
d7206569
RS
4685 return FALSE;
4686
c224138d
RS
4687 /* Work out the number of page, local and TLS entries. */
4688 estimate = arg->max_pages;
4689 if (estimate > g->page_gotno)
4690 estimate = g->page_gotno;
4691 estimate += g->local_gotno + g->tls_gotno;
0f20cc35
DJ
4692
4693 /* We place TLS GOT entries after both locals and globals. The globals
4694 for the primary GOT may overflow the normal GOT size limit, so be
4695 sure not to merge a GOT which requires TLS with the primary GOT in that
4696 case. This doesn't affect non-primary GOTs. */
c224138d 4697 estimate += (g->tls_gotno > 0 ? arg->global_count : g->global_gotno);
143d77c5 4698
c224138d 4699 if (estimate <= arg->max_count)
f4416af6 4700 {
c224138d
RS
4701 /* If we don't have a primary GOT, use it as
4702 a starting point for the primary GOT. */
4703 if (!arg->primary)
4704 {
d7206569
RS
4705 arg->primary = g;
4706 return TRUE;
c224138d 4707 }
f4416af6 4708
c224138d 4709 /* Try merging with the primary GOT. */
d7206569 4710 result = mips_elf_merge_got_with (abfd, g, arg->primary, arg);
c224138d
RS
4711 if (result >= 0)
4712 return result;
f4416af6 4713 }
c224138d 4714
f4416af6 4715 /* If we can merge with the last-created got, do it. */
c224138d 4716 if (arg->current)
f4416af6 4717 {
d7206569 4718 result = mips_elf_merge_got_with (abfd, g, arg->current, arg);
c224138d
RS
4719 if (result >= 0)
4720 return result;
f4416af6 4721 }
c224138d 4722
f4416af6
AO
4723 /* Well, we couldn't merge, so create a new GOT. Don't check if it
4724 fits; if it turns out that it doesn't, we'll get relocation
4725 overflows anyway. */
c224138d
RS
4726 g->next = arg->current;
4727 arg->current = g;
0f20cc35 4728
d7206569 4729 return TRUE;
0f20cc35
DJ
4730}
4731
72e7511a
RS
4732/* ENTRYP is a hash table entry for a mips_got_entry. Set its gotidx
4733 to GOTIDX, duplicating the entry if it has already been assigned
4734 an index in a different GOT. */
4735
4736static bfd_boolean
4737mips_elf_set_gotidx (void **entryp, long gotidx)
4738{
4739 struct mips_got_entry *entry;
4740
4741 entry = (struct mips_got_entry *) *entryp;
4742 if (entry->gotidx > 0)
4743 {
4744 struct mips_got_entry *new_entry;
4745
4746 new_entry = bfd_alloc (entry->abfd, sizeof (*entry));
4747 if (!new_entry)
4748 return FALSE;
4749
4750 *new_entry = *entry;
4751 *entryp = new_entry;
4752 entry = new_entry;
4753 }
4754 entry->gotidx = gotidx;
4755 return TRUE;
4756}
4757
4758/* Set the TLS GOT index for the GOT entry in ENTRYP. DATA points to a
4759 mips_elf_traverse_got_arg in which DATA->value is the size of one
4760 GOT entry. Set DATA->g to null on failure. */
0f20cc35
DJ
4761
4762static int
72e7511a 4763mips_elf_initialize_tls_index (void **entryp, void *data)
0f20cc35 4764{
72e7511a
RS
4765 struct mips_got_entry *entry;
4766 struct mips_elf_traverse_got_arg *arg;
0f20cc35
DJ
4767
4768 /* We're only interested in TLS symbols. */
72e7511a 4769 entry = (struct mips_got_entry *) *entryp;
9ab066b4 4770 if (entry->tls_type == GOT_TLS_NONE)
0f20cc35
DJ
4771 return 1;
4772
72e7511a 4773 arg = (struct mips_elf_traverse_got_arg *) data;
6c42ddb9 4774 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->tls_assigned_gotno))
ead49a57 4775 {
6c42ddb9
RS
4776 arg->g = NULL;
4777 return 0;
f4416af6
AO
4778 }
4779
ead49a57 4780 /* Account for the entries we've just allocated. */
9ab066b4 4781 arg->g->tls_assigned_gotno += mips_tls_got_entries (entry->tls_type);
f4416af6
AO
4782 return 1;
4783}
4784
ab361d49
RS
4785/* A htab_traverse callback for GOT entries, where DATA points to a
4786 mips_elf_traverse_got_arg. Set the global_got_area of each global
4787 symbol to DATA->value. */
f4416af6 4788
f4416af6 4789static int
ab361d49 4790mips_elf_set_global_got_area (void **entryp, void *data)
f4416af6 4791{
ab361d49
RS
4792 struct mips_got_entry *entry;
4793 struct mips_elf_traverse_got_arg *arg;
f4416af6 4794
ab361d49
RS
4795 entry = (struct mips_got_entry *) *entryp;
4796 arg = (struct mips_elf_traverse_got_arg *) data;
4797 if (entry->abfd != NULL
4798 && entry->symndx == -1
4799 && entry->d.h->global_got_area != GGA_NONE)
4800 entry->d.h->global_got_area = arg->value;
4801 return 1;
4802}
4803
4804/* A htab_traverse callback for secondary GOT entries, where DATA points
4805 to a mips_elf_traverse_got_arg. Assign GOT indices to global entries
4806 and record the number of relocations they require. DATA->value is
72e7511a 4807 the size of one GOT entry. Set DATA->g to null on failure. */
ab361d49
RS
4808
4809static int
4810mips_elf_set_global_gotidx (void **entryp, void *data)
4811{
4812 struct mips_got_entry *entry;
4813 struct mips_elf_traverse_got_arg *arg;
0f20cc35 4814
ab361d49
RS
4815 entry = (struct mips_got_entry *) *entryp;
4816 arg = (struct mips_elf_traverse_got_arg *) data;
634835ae
RS
4817 if (entry->abfd != NULL
4818 && entry->symndx == -1
4819 && entry->d.h->global_got_area != GGA_NONE)
f4416af6 4820 {
cb22ccf4 4821 if (!mips_elf_set_gotidx (entryp, arg->value * arg->g->assigned_low_gotno))
72e7511a
RS
4822 {
4823 arg->g = NULL;
4824 return 0;
4825 }
cb22ccf4 4826 arg->g->assigned_low_gotno += 1;
72e7511a 4827
0e1862bb 4828 if (bfd_link_pic (arg->info)
ab361d49
RS
4829 || (elf_hash_table (arg->info)->dynamic_sections_created
4830 && entry->d.h->root.def_dynamic
4831 && !entry->d.h->root.def_regular))
4832 arg->g->relocs += 1;
f4416af6
AO
4833 }
4834
4835 return 1;
4836}
4837
33bb52fb
RS
4838/* A htab_traverse callback for GOT entries for which DATA is the
4839 bfd_link_info. Forbid any global symbols from having traditional
4840 lazy-binding stubs. */
4841
0626d451 4842static int
33bb52fb 4843mips_elf_forbid_lazy_stubs (void **entryp, void *data)
0626d451 4844{
33bb52fb
RS
4845 struct bfd_link_info *info;
4846 struct mips_elf_link_hash_table *htab;
4847 struct mips_got_entry *entry;
0626d451 4848
33bb52fb
RS
4849 entry = (struct mips_got_entry *) *entryp;
4850 info = (struct bfd_link_info *) data;
4851 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4852 BFD_ASSERT (htab != NULL);
4853
0626d451
RS
4854 if (entry->abfd != NULL
4855 && entry->symndx == -1
33bb52fb 4856 && entry->d.h->needs_lazy_stub)
f4416af6 4857 {
33bb52fb
RS
4858 entry->d.h->needs_lazy_stub = FALSE;
4859 htab->lazy_stub_count--;
f4416af6 4860 }
143d77c5 4861
f4416af6
AO
4862 return 1;
4863}
4864
f4416af6
AO
4865/* Return the offset of an input bfd IBFD's GOT from the beginning of
4866 the primary GOT. */
4867static bfd_vma
9719ad41 4868mips_elf_adjust_gp (bfd *abfd, struct mips_got_info *g, bfd *ibfd)
f4416af6 4869{
d7206569 4870 if (!g->next)
f4416af6
AO
4871 return 0;
4872
d7206569 4873 g = mips_elf_bfd_got (ibfd, FALSE);
f4416af6
AO
4874 if (! g)
4875 return 0;
4876
4877 BFD_ASSERT (g->next);
4878
4879 g = g->next;
143d77c5 4880
0f20cc35
DJ
4881 return (g->local_gotno + g->global_gotno + g->tls_gotno)
4882 * MIPS_ELF_GOT_SIZE (abfd);
f4416af6
AO
4883}
4884
4885/* Turn a single GOT that is too big for 16-bit addressing into
4886 a sequence of GOTs, each one 16-bit addressable. */
4887
4888static bfd_boolean
9719ad41 4889mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
a8028dd0 4890 asection *got, bfd_size_type pages)
f4416af6 4891{
a8028dd0 4892 struct mips_elf_link_hash_table *htab;
f4416af6 4893 struct mips_elf_got_per_bfd_arg got_per_bfd_arg;
ab361d49 4894 struct mips_elf_traverse_got_arg tga;
a8028dd0 4895 struct mips_got_info *g, *gg;
33bb52fb 4896 unsigned int assign, needed_relocs;
d7206569 4897 bfd *dynobj, *ibfd;
f4416af6 4898
33bb52fb 4899 dynobj = elf_hash_table (info)->dynobj;
a8028dd0 4900 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
4901 BFD_ASSERT (htab != NULL);
4902
a8028dd0 4903 g = htab->got_info;
f4416af6 4904
f4416af6
AO
4905 got_per_bfd_arg.obfd = abfd;
4906 got_per_bfd_arg.info = info;
f4416af6
AO
4907 got_per_bfd_arg.current = NULL;
4908 got_per_bfd_arg.primary = NULL;
0a44bf69 4909 got_per_bfd_arg.max_count = ((MIPS_ELF_GOT_MAX_SIZE (info)
f4416af6 4910 / MIPS_ELF_GOT_SIZE (abfd))
861fb55a 4911 - htab->reserved_gotno);
c224138d 4912 got_per_bfd_arg.max_pages = pages;
0f20cc35 4913 /* The number of globals that will be included in the primary GOT.
ab361d49 4914 See the calls to mips_elf_set_global_got_area below for more
0f20cc35
DJ
4915 information. */
4916 got_per_bfd_arg.global_count = g->global_gotno;
f4416af6
AO
4917
4918 /* Try to merge the GOTs of input bfds together, as long as they
4919 don't seem to exceed the maximum GOT size, choosing one of them
4920 to be the primary GOT. */
c72f2fb2 4921 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
d7206569
RS
4922 {
4923 gg = mips_elf_bfd_got (ibfd, FALSE);
4924 if (gg && !mips_elf_merge_got (ibfd, gg, &got_per_bfd_arg))
4925 return FALSE;
4926 }
f4416af6 4927
0f20cc35 4928 /* If we do not find any suitable primary GOT, create an empty one. */
f4416af6 4929 if (got_per_bfd_arg.primary == NULL)
3dff0dd1 4930 g->next = mips_elf_create_got_info (abfd);
f4416af6
AO
4931 else
4932 g->next = got_per_bfd_arg.primary;
4933 g->next->next = got_per_bfd_arg.current;
4934
4935 /* GG is now the master GOT, and G is the primary GOT. */
4936 gg = g;
4937 g = g->next;
4938
4939 /* Map the output bfd to the primary got. That's what we're going
4940 to use for bfds that use GOT16 or GOT_PAGE relocations that we
4941 didn't mark in check_relocs, and we want a quick way to find it.
4942 We can't just use gg->next because we're going to reverse the
4943 list. */
d7206569 4944 mips_elf_replace_bfd_got (abfd, g);
f4416af6 4945
634835ae
RS
4946 /* Every symbol that is referenced in a dynamic relocation must be
4947 present in the primary GOT, so arrange for them to appear after
4948 those that are actually referenced. */
23cc69b6 4949 gg->reloc_only_gotno = gg->global_gotno - g->global_gotno;
634835ae 4950 g->global_gotno = gg->global_gotno;
f4416af6 4951
ab361d49
RS
4952 tga.info = info;
4953 tga.value = GGA_RELOC_ONLY;
4954 htab_traverse (gg->got_entries, mips_elf_set_global_got_area, &tga);
4955 tga.value = GGA_NORMAL;
4956 htab_traverse (g->got_entries, mips_elf_set_global_got_area, &tga);
f4416af6
AO
4957
4958 /* Now go through the GOTs assigning them offset ranges.
cb22ccf4 4959 [assigned_low_gotno, local_gotno[ will be set to the range of local
f4416af6
AO
4960 entries in each GOT. We can then compute the end of a GOT by
4961 adding local_gotno to global_gotno. We reverse the list and make
4962 it circular since then we'll be able to quickly compute the
4963 beginning of a GOT, by computing the end of its predecessor. To
4964 avoid special cases for the primary GOT, while still preserving
4965 assertions that are valid for both single- and multi-got links,
4966 we arrange for the main got struct to have the right number of
4967 global entries, but set its local_gotno such that the initial
4968 offset of the primary GOT is zero. Remember that the primary GOT
4969 will become the last item in the circular linked list, so it
4970 points back to the master GOT. */
4971 gg->local_gotno = -g->global_gotno;
4972 gg->global_gotno = g->global_gotno;
0f20cc35 4973 gg->tls_gotno = 0;
f4416af6
AO
4974 assign = 0;
4975 gg->next = gg;
4976
4977 do
4978 {
4979 struct mips_got_info *gn;
4980
861fb55a 4981 assign += htab->reserved_gotno;
cb22ccf4 4982 g->assigned_low_gotno = assign;
c224138d
RS
4983 g->local_gotno += assign;
4984 g->local_gotno += (pages < g->page_gotno ? pages : g->page_gotno);
cb22ccf4 4985 g->assigned_high_gotno = g->local_gotno - 1;
0f20cc35
DJ
4986 assign = g->local_gotno + g->global_gotno + g->tls_gotno;
4987
ead49a57
RS
4988 /* Take g out of the direct list, and push it onto the reversed
4989 list that gg points to. g->next is guaranteed to be nonnull after
4990 this operation, as required by mips_elf_initialize_tls_index. */
4991 gn = g->next;
4992 g->next = gg->next;
4993 gg->next = g;
4994
0f20cc35
DJ
4995 /* Set up any TLS entries. We always place the TLS entries after
4996 all non-TLS entries. */
4997 g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
72e7511a
RS
4998 tga.g = g;
4999 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5000 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
5001 if (!tga.g)
5002 return FALSE;
1fd20d70 5003 BFD_ASSERT (g->tls_assigned_gotno == assign);
f4416af6 5004
ead49a57 5005 /* Move onto the next GOT. It will be a secondary GOT if nonull. */
f4416af6 5006 g = gn;
0626d451 5007
33bb52fb
RS
5008 /* Forbid global symbols in every non-primary GOT from having
5009 lazy-binding stubs. */
0626d451 5010 if (g)
33bb52fb 5011 htab_traverse (g->got_entries, mips_elf_forbid_lazy_stubs, info);
f4416af6
AO
5012 }
5013 while (g);
5014
59b08994 5015 got->size = assign * MIPS_ELF_GOT_SIZE (abfd);
33bb52fb
RS
5016
5017 needed_relocs = 0;
33bb52fb
RS
5018 for (g = gg->next; g && g->next != gg; g = g->next)
5019 {
5020 unsigned int save_assign;
5021
ab361d49
RS
5022 /* Assign offsets to global GOT entries and count how many
5023 relocations they need. */
cb22ccf4
KCY
5024 save_assign = g->assigned_low_gotno;
5025 g->assigned_low_gotno = g->local_gotno;
ab361d49
RS
5026 tga.info = info;
5027 tga.value = MIPS_ELF_GOT_SIZE (abfd);
5028 tga.g = g;
5029 htab_traverse (g->got_entries, mips_elf_set_global_gotidx, &tga);
72e7511a
RS
5030 if (!tga.g)
5031 return FALSE;
cb22ccf4
KCY
5032 BFD_ASSERT (g->assigned_low_gotno == g->local_gotno + g->global_gotno);
5033 g->assigned_low_gotno = save_assign;
72e7511a 5034
0e1862bb 5035 if (bfd_link_pic (info))
33bb52fb 5036 {
cb22ccf4
KCY
5037 g->relocs += g->local_gotno - g->assigned_low_gotno;
5038 BFD_ASSERT (g->assigned_low_gotno == g->next->local_gotno
33bb52fb
RS
5039 + g->next->global_gotno
5040 + g->next->tls_gotno
861fb55a 5041 + htab->reserved_gotno);
33bb52fb 5042 }
ab361d49 5043 needed_relocs += g->relocs;
33bb52fb 5044 }
ab361d49 5045 needed_relocs += g->relocs;
33bb52fb
RS
5046
5047 if (needed_relocs)
5048 mips_elf_allocate_dynamic_relocations (dynobj, info,
5049 needed_relocs);
143d77c5 5050
f4416af6
AO
5051 return TRUE;
5052}
143d77c5 5053
b49e97c9
TS
5054\f
5055/* Returns the first relocation of type r_type found, beginning with
5056 RELOCATION. RELEND is one-past-the-end of the relocation table. */
5057
5058static const Elf_Internal_Rela *
9719ad41
RS
5059mips_elf_next_relocation (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type,
5060 const Elf_Internal_Rela *relocation,
5061 const Elf_Internal_Rela *relend)
b49e97c9 5062{
c000e262
TS
5063 unsigned long r_symndx = ELF_R_SYM (abfd, relocation->r_info);
5064
b49e97c9
TS
5065 while (relocation < relend)
5066 {
c000e262
TS
5067 if (ELF_R_TYPE (abfd, relocation->r_info) == r_type
5068 && ELF_R_SYM (abfd, relocation->r_info) == r_symndx)
b49e97c9
TS
5069 return relocation;
5070
5071 ++relocation;
5072 }
5073
5074 /* We didn't find it. */
b49e97c9
TS
5075 return NULL;
5076}
5077
020d7251 5078/* Return whether an input relocation is against a local symbol. */
b49e97c9 5079
b34976b6 5080static bfd_boolean
9719ad41
RS
5081mips_elf_local_relocation_p (bfd *input_bfd,
5082 const Elf_Internal_Rela *relocation,
020d7251 5083 asection **local_sections)
b49e97c9
TS
5084{
5085 unsigned long r_symndx;
5086 Elf_Internal_Shdr *symtab_hdr;
b49e97c9
TS
5087 size_t extsymoff;
5088
5089 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5090 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5091 extsymoff = (elf_bad_symtab (input_bfd)) ? 0 : symtab_hdr->sh_info;
5092
5093 if (r_symndx < extsymoff)
b34976b6 5094 return TRUE;
b49e97c9 5095 if (elf_bad_symtab (input_bfd) && local_sections[r_symndx] != NULL)
b34976b6 5096 return TRUE;
b49e97c9 5097
b34976b6 5098 return FALSE;
b49e97c9
TS
5099}
5100\f
5101/* Sign-extend VALUE, which has the indicated number of BITS. */
5102
a7ebbfdf 5103bfd_vma
9719ad41 5104_bfd_mips_elf_sign_extend (bfd_vma value, int bits)
b49e97c9
TS
5105{
5106 if (value & ((bfd_vma) 1 << (bits - 1)))
5107 /* VALUE is negative. */
5108 value |= ((bfd_vma) - 1) << bits;
5109
5110 return value;
5111}
5112
5113/* Return non-zero if the indicated VALUE has overflowed the maximum
4cc11e76 5114 range expressible by a signed number with the indicated number of
b49e97c9
TS
5115 BITS. */
5116
b34976b6 5117static bfd_boolean
9719ad41 5118mips_elf_overflow_p (bfd_vma value, int bits)
b49e97c9
TS
5119{
5120 bfd_signed_vma svalue = (bfd_signed_vma) value;
5121
5122 if (svalue > (1 << (bits - 1)) - 1)
5123 /* The value is too big. */
b34976b6 5124 return TRUE;
b49e97c9
TS
5125 else if (svalue < -(1 << (bits - 1)))
5126 /* The value is too small. */
b34976b6 5127 return TRUE;
b49e97c9
TS
5128
5129 /* All is well. */
b34976b6 5130 return FALSE;
b49e97c9
TS
5131}
5132
5133/* Calculate the %high function. */
5134
5135static bfd_vma
9719ad41 5136mips_elf_high (bfd_vma value)
b49e97c9
TS
5137{
5138 return ((value + (bfd_vma) 0x8000) >> 16) & 0xffff;
5139}
5140
5141/* Calculate the %higher function. */
5142
5143static bfd_vma
9719ad41 5144mips_elf_higher (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
5145{
5146#ifdef BFD64
5147 return ((value + (bfd_vma) 0x80008000) >> 32) & 0xffff;
5148#else
5149 abort ();
c5ae1840 5150 return MINUS_ONE;
b49e97c9
TS
5151#endif
5152}
5153
5154/* Calculate the %highest function. */
5155
5156static bfd_vma
9719ad41 5157mips_elf_highest (bfd_vma value ATTRIBUTE_UNUSED)
b49e97c9
TS
5158{
5159#ifdef BFD64
b15e6682 5160 return ((value + (((bfd_vma) 0x8000 << 32) | 0x80008000)) >> 48) & 0xffff;
b49e97c9
TS
5161#else
5162 abort ();
c5ae1840 5163 return MINUS_ONE;
b49e97c9
TS
5164#endif
5165}
5166\f
5167/* Create the .compact_rel section. */
5168
b34976b6 5169static bfd_boolean
9719ad41
RS
5170mips_elf_create_compact_rel_section
5171 (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
5172{
5173 flagword flags;
5174 register asection *s;
5175
3d4d4302 5176 if (bfd_get_linker_section (abfd, ".compact_rel") == NULL)
b49e97c9
TS
5177 {
5178 flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED
5179 | SEC_READONLY);
5180
3d4d4302 5181 s = bfd_make_section_anyway_with_flags (abfd, ".compact_rel", flags);
b49e97c9 5182 if (s == NULL
fd361982 5183 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 5184 return FALSE;
b49e97c9 5185
eea6121a 5186 s->size = sizeof (Elf32_External_compact_rel);
b49e97c9
TS
5187 }
5188
b34976b6 5189 return TRUE;
b49e97c9
TS
5190}
5191
5192/* Create the .got section to hold the global offset table. */
5193
b34976b6 5194static bfd_boolean
23cc69b6 5195mips_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
5196{
5197 flagword flags;
5198 register asection *s;
5199 struct elf_link_hash_entry *h;
14a793b2 5200 struct bfd_link_hash_entry *bh;
0a44bf69
RS
5201 struct mips_elf_link_hash_table *htab;
5202
5203 htab = mips_elf_hash_table (info);
4dfe6ac6 5204 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5205
5206 /* This function may be called more than once. */
ce558b89 5207 if (htab->root.sgot)
23cc69b6 5208 return TRUE;
b49e97c9
TS
5209
5210 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
5211 | SEC_LINKER_CREATED);
5212
72b4917c
TS
5213 /* We have to use an alignment of 2**4 here because this is hardcoded
5214 in the function stub generation and in the linker script. */
87e0a731 5215 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
b49e97c9 5216 if (s == NULL
fd361982 5217 || !bfd_set_section_alignment (s, 4))
b34976b6 5218 return FALSE;
ce558b89 5219 htab->root.sgot = s;
b49e97c9
TS
5220
5221 /* Define the symbol _GLOBAL_OFFSET_TABLE_. We don't do this in the
5222 linker script because we don't want to define the symbol if we
5223 are not creating a global offset table. */
14a793b2 5224 bh = NULL;
b49e97c9
TS
5225 if (! (_bfd_generic_link_add_one_symbol
5226 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
9719ad41 5227 0, NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 5228 return FALSE;
14a793b2
AM
5229
5230 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
5231 h->non_elf = 0;
5232 h->def_regular = 1;
b49e97c9 5233 h->type = STT_OBJECT;
2f9efdfc 5234 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
d329bcd1 5235 elf_hash_table (info)->hgot = h;
b49e97c9 5236
0e1862bb 5237 if (bfd_link_pic (info)
c152c796 5238 && ! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 5239 return FALSE;
b49e97c9 5240
3dff0dd1 5241 htab->got_info = mips_elf_create_got_info (abfd);
f0abc2a1 5242 mips_elf_section_data (s)->elf.this_hdr.sh_flags
b49e97c9
TS
5243 |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
5244
861fb55a 5245 /* We also need a .got.plt section when generating PLTs. */
87e0a731
AM
5246 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt",
5247 SEC_ALLOC | SEC_LOAD
5248 | SEC_HAS_CONTENTS
5249 | SEC_IN_MEMORY
5250 | SEC_LINKER_CREATED);
861fb55a
DJ
5251 if (s == NULL)
5252 return FALSE;
ce558b89 5253 htab->root.sgotplt = s;
0a44bf69 5254
b34976b6 5255 return TRUE;
b49e97c9 5256}
b49e97c9 5257\f
0a44bf69
RS
5258/* Return true if H refers to the special VxWorks __GOTT_BASE__ or
5259 __GOTT_INDEX__ symbols. These symbols are only special for
5260 shared objects; they are not used in executables. */
5261
5262static bfd_boolean
5263is_gott_symbol (struct bfd_link_info *info, struct elf_link_hash_entry *h)
5264{
90c14f0c 5265 return (mips_elf_hash_table (info)->root.target_os == is_vxworks
0e1862bb 5266 && bfd_link_pic (info)
0a44bf69
RS
5267 && (strcmp (h->root.root.string, "__GOTT_BASE__") == 0
5268 || strcmp (h->root.root.string, "__GOTT_INDEX__") == 0));
5269}
861fb55a
DJ
5270
5271/* Return TRUE if a relocation of type R_TYPE from INPUT_BFD might
5272 require an la25 stub. See also mips_elf_local_pic_function_p,
5273 which determines whether the destination function ever requires a
5274 stub. */
5275
5276static bfd_boolean
8f0c309a
CLT
5277mips_elf_relocation_needs_la25_stub (bfd *input_bfd, int r_type,
5278 bfd_boolean target_is_16_bit_code_p)
861fb55a
DJ
5279{
5280 /* We specifically ignore branches and jumps from EF_PIC objects,
5281 where the onus is on the compiler or programmer to perform any
5282 necessary initialization of $25. Sometimes such initialization
5283 is unnecessary; for example, -mno-shared functions do not use
5284 the incoming value of $25, and may therefore be called directly. */
5285 if (PIC_OBJECT_P (input_bfd))
5286 return FALSE;
5287
5288 switch (r_type)
5289 {
5290 case R_MIPS_26:
5291 case R_MIPS_PC16:
7361da2c
AB
5292 case R_MIPS_PC21_S2:
5293 case R_MIPS_PC26_S2:
df58fc94
RS
5294 case R_MICROMIPS_26_S1:
5295 case R_MICROMIPS_PC7_S1:
5296 case R_MICROMIPS_PC10_S1:
5297 case R_MICROMIPS_PC16_S1:
5298 case R_MICROMIPS_PC23_S2:
861fb55a
DJ
5299 return TRUE;
5300
8f0c309a
CLT
5301 case R_MIPS16_26:
5302 return !target_is_16_bit_code_p;
5303
861fb55a
DJ
5304 default:
5305 return FALSE;
5306 }
5307}
0a44bf69 5308\f
47275900
MR
5309/* Obtain the field relocated by RELOCATION. */
5310
5311static bfd_vma
5312mips_elf_obtain_contents (reloc_howto_type *howto,
5313 const Elf_Internal_Rela *relocation,
5314 bfd *input_bfd, bfd_byte *contents)
5315{
5316 bfd_vma x = 0;
5317 bfd_byte *location = contents + relocation->r_offset;
5318 unsigned int size = bfd_get_reloc_size (howto);
5319
5320 /* Obtain the bytes. */
5321 if (size != 0)
5322 x = bfd_get (8 * size, input_bfd, location);
5323
5324 return x;
5325}
5326
98e10ffa
MR
5327/* Store the field relocated by RELOCATION. */
5328
5329static void
5330mips_elf_store_contents (reloc_howto_type *howto,
5331 const Elf_Internal_Rela *relocation,
5332 bfd *input_bfd, bfd_byte *contents, bfd_vma x)
5333{
5334 bfd_byte *location = contents + relocation->r_offset;
5335 unsigned int size = bfd_get_reloc_size (howto);
5336
5337 /* Put the value into the output. */
5338 if (size != 0)
5339 bfd_put (8 * size, input_bfd, x, location);
5340}
5341
47275900
MR
5342/* Try to patch a load from GOT instruction in CONTENTS pointed to by
5343 RELOCATION described by HOWTO, with a move of 0 to the load target
5344 register, returning TRUE if that is successful and FALSE otherwise.
5345 If DOIT is FALSE, then only determine it patching is possible and
5346 return status without actually changing CONTENTS.
5347*/
5348
5349static bfd_boolean
5350mips_elf_nullify_got_load (bfd *input_bfd, bfd_byte *contents,
5351 const Elf_Internal_Rela *relocation,
5352 reloc_howto_type *howto, bfd_boolean doit)
5353{
5354 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5355 bfd_byte *location = contents + relocation->r_offset;
5356 bfd_boolean nullified = TRUE;
5357 bfd_vma x;
5358
5359 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
5360
5361 /* Obtain the current value. */
5362 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
5363
5364 /* Note that in the unshuffled MIPS16 encoding RX is at bits [21:19]
5365 while RY is at bits [18:16] of the combined 32-bit instruction word. */
5366 if (mips16_reloc_p (r_type)
5367 && (((x >> 22) & 0x3ff) == 0x3d3 /* LW */
5368 || ((x >> 22) & 0x3ff) == 0x3c7)) /* LD */
5369 x = (0x3cd << 22) | (x & (7 << 16)) << 3; /* LI */
5370 else if (micromips_reloc_p (r_type)
5371 && ((x >> 26) & 0x37) == 0x37) /* LW/LD */
5372 x = (0xc << 26) | (x & (0x1f << 21)); /* ADDIU */
5373 else if (((x >> 26) & 0x3f) == 0x23 /* LW */
5374 || ((x >> 26) & 0x3f) == 0x37) /* LD */
5375 x = (0x9 << 26) | (x & (0x1f << 16)); /* ADDIU */
5376 else
5377 nullified = FALSE;
5378
5379 /* Put the value into the output. */
5380 if (doit && nullified)
5381 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
5382
5383 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, FALSE, location);
5384
5385 return nullified;
5386}
5387
b49e97c9
TS
5388/* Calculate the value produced by the RELOCATION (which comes from
5389 the INPUT_BFD). The ADDEND is the addend to use for this
5390 RELOCATION; RELOCATION->R_ADDEND is ignored.
5391
5392 The result of the relocation calculation is stored in VALUEP.
38a7df63 5393 On exit, set *CROSS_MODE_JUMP_P to true if the relocation field
df58fc94 5394 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9
TS
5395
5396 This function returns bfd_reloc_continue if the caller need take no
5397 further action regarding this relocation, bfd_reloc_notsupported if
5398 something goes dramatically wrong, bfd_reloc_overflow if an
5399 overflow occurs, and bfd_reloc_ok to indicate success. */
5400
5401static bfd_reloc_status_type
9719ad41 5402mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
47275900 5403 asection *input_section, bfd_byte *contents,
9719ad41
RS
5404 struct bfd_link_info *info,
5405 const Elf_Internal_Rela *relocation,
5406 bfd_vma addend, reloc_howto_type *howto,
5407 Elf_Internal_Sym *local_syms,
5408 asection **local_sections, bfd_vma *valuep,
38a7df63
CF
5409 const char **namep,
5410 bfd_boolean *cross_mode_jump_p,
9719ad41 5411 bfd_boolean save_addend)
b49e97c9
TS
5412{
5413 /* The eventual value we will return. */
5414 bfd_vma value;
5415 /* The address of the symbol against which the relocation is
5416 occurring. */
5417 bfd_vma symbol = 0;
5418 /* The final GP value to be used for the relocatable, executable, or
5419 shared object file being produced. */
0a61c8c2 5420 bfd_vma gp;
b49e97c9
TS
5421 /* The place (section offset or address) of the storage unit being
5422 relocated. */
5423 bfd_vma p;
5424 /* The value of GP used to create the relocatable object. */
0a61c8c2 5425 bfd_vma gp0;
b49e97c9
TS
5426 /* The offset into the global offset table at which the address of
5427 the relocation entry symbol, adjusted by the addend, resides
5428 during execution. */
5429 bfd_vma g = MINUS_ONE;
5430 /* The section in which the symbol referenced by the relocation is
5431 located. */
5432 asection *sec = NULL;
5433 struct mips_elf_link_hash_entry *h = NULL;
b34976b6 5434 /* TRUE if the symbol referred to by this relocation is a local
b49e97c9 5435 symbol. */
b34976b6 5436 bfd_boolean local_p, was_local_p;
77434823
MR
5437 /* TRUE if the symbol referred to by this relocation is a section
5438 symbol. */
5439 bfd_boolean section_p = FALSE;
b34976b6
AM
5440 /* TRUE if the symbol referred to by this relocation is "_gp_disp". */
5441 bfd_boolean gp_disp_p = FALSE;
bbe506e8
TS
5442 /* TRUE if the symbol referred to by this relocation is
5443 "__gnu_local_gp". */
5444 bfd_boolean gnu_local_gp_p = FALSE;
b49e97c9
TS
5445 Elf_Internal_Shdr *symtab_hdr;
5446 size_t extsymoff;
5447 unsigned long r_symndx;
5448 int r_type;
b34976b6 5449 /* TRUE if overflow occurred during the calculation of the
b49e97c9 5450 relocation value. */
b34976b6
AM
5451 bfd_boolean overflowed_p;
5452 /* TRUE if this relocation refers to a MIPS16 function. */
5453 bfd_boolean target_is_16_bit_code_p = FALSE;
df58fc94 5454 bfd_boolean target_is_micromips_code_p = FALSE;
0a44bf69
RS
5455 struct mips_elf_link_hash_table *htab;
5456 bfd *dynobj;
ad951203 5457 bfd_boolean resolved_to_zero;
0a44bf69
RS
5458
5459 dynobj = elf_hash_table (info)->dynobj;
5460 htab = mips_elf_hash_table (info);
4dfe6ac6 5461 BFD_ASSERT (htab != NULL);
b49e97c9
TS
5462
5463 /* Parse the relocation. */
5464 r_symndx = ELF_R_SYM (input_bfd, relocation->r_info);
5465 r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
5466 p = (input_section->output_section->vma
5467 + input_section->output_offset
5468 + relocation->r_offset);
5469
5470 /* Assume that there will be no overflow. */
b34976b6 5471 overflowed_p = FALSE;
b49e97c9
TS
5472
5473 /* Figure out whether or not the symbol is local, and get the offset
5474 used in the array of hash table entries. */
5475 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5476 local_p = mips_elf_local_relocation_p (input_bfd, relocation,
020d7251 5477 local_sections);
bce03d3d 5478 was_local_p = local_p;
b49e97c9
TS
5479 if (! elf_bad_symtab (input_bfd))
5480 extsymoff = symtab_hdr->sh_info;
5481 else
5482 {
5483 /* The symbol table does not follow the rule that local symbols
5484 must come before globals. */
5485 extsymoff = 0;
5486 }
5487
5488 /* Figure out the value of the symbol. */
5489 if (local_p)
5490 {
9d862524 5491 bfd_boolean micromips_p = MICROMIPS_P (abfd);
b49e97c9
TS
5492 Elf_Internal_Sym *sym;
5493
5494 sym = local_syms + r_symndx;
5495 sec = local_sections[r_symndx];
5496
77434823
MR
5497 section_p = ELF_ST_TYPE (sym->st_info) == STT_SECTION;
5498
b49e97c9 5499 symbol = sec->output_section->vma + sec->output_offset;
77434823 5500 if (!section_p || (sec->flags & SEC_MERGE))
b49e97c9 5501 symbol += sym->st_value;
77434823 5502 if ((sec->flags & SEC_MERGE) && section_p)
d4df96e6
L
5503 {
5504 addend = _bfd_elf_rel_local_sym (abfd, sym, &sec, addend);
5505 addend -= symbol;
5506 addend += sec->output_section->vma + sec->output_offset;
5507 }
b49e97c9 5508
df58fc94
RS
5509 /* MIPS16/microMIPS text labels should be treated as odd. */
5510 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
5511 ++symbol;
5512
5513 /* Record the name of this symbol, for our caller. */
5514 *namep = bfd_elf_string_from_elf_section (input_bfd,
5515 symtab_hdr->sh_link,
5516 sym->st_name);
ceab86af 5517 if (*namep == NULL || **namep == '\0')
fd361982 5518 *namep = bfd_section_name (sec);
b49e97c9 5519
9d862524 5520 /* For relocations against a section symbol and ones against no
07d6d2b8 5521 symbol (absolute relocations) infer the ISA mode from the addend. */
9d862524
MR
5522 if (section_p || r_symndx == STN_UNDEF)
5523 {
5524 target_is_16_bit_code_p = (addend & 1) && !micromips_p;
5525 target_is_micromips_code_p = (addend & 1) && micromips_p;
5526 }
5527 /* For relocations against an absolute symbol infer the ISA mode
07d6d2b8 5528 from the value of the symbol plus addend. */
9d862524
MR
5529 else if (bfd_is_abs_section (sec))
5530 {
5531 target_is_16_bit_code_p = ((symbol + addend) & 1) && !micromips_p;
5532 target_is_micromips_code_p = ((symbol + addend) & 1) && micromips_p;
5533 }
5534 /* Otherwise just use the regular symbol annotation available. */
5535 else
5536 {
5537 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (sym->st_other);
5538 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (sym->st_other);
5539 }
b49e97c9
TS
5540 }
5541 else
5542 {
560e09e9
NC
5543 /* ??? Could we use RELOC_FOR_GLOBAL_SYMBOL here ? */
5544
b49e97c9
TS
5545 /* For global symbols we look up the symbol in the hash-table. */
5546 h = ((struct mips_elf_link_hash_entry *)
5547 elf_sym_hashes (input_bfd) [r_symndx - extsymoff]);
5548 /* Find the real hash-table entry for this symbol. */
5549 while (h->root.root.type == bfd_link_hash_indirect
5550 || h->root.root.type == bfd_link_hash_warning)
5551 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
5552
5553 /* Record the name of this symbol, for our caller. */
5554 *namep = h->root.root.root.string;
5555
5556 /* See if this is the special _gp_disp symbol. Note that such a
5557 symbol must always be a global symbol. */
560e09e9 5558 if (strcmp (*namep, "_gp_disp") == 0
b49e97c9
TS
5559 && ! NEWABI_P (input_bfd))
5560 {
5561 /* Relocations against _gp_disp are permitted only with
5562 R_MIPS_HI16 and R_MIPS_LO16 relocations. */
738e5348 5563 if (!hi16_reloc_p (r_type) && !lo16_reloc_p (r_type))
b49e97c9
TS
5564 return bfd_reloc_notsupported;
5565
b34976b6 5566 gp_disp_p = TRUE;
b49e97c9 5567 }
bbe506e8
TS
5568 /* See if this is the special _gp symbol. Note that such a
5569 symbol must always be a global symbol. */
5570 else if (strcmp (*namep, "__gnu_local_gp") == 0)
5571 gnu_local_gp_p = TRUE;
5572
5573
b49e97c9
TS
5574 /* If this symbol is defined, calculate its address. Note that
5575 _gp_disp is a magic symbol, always implicitly defined by the
5576 linker, so it's inappropriate to check to see whether or not
5577 its defined. */
5578 else if ((h->root.root.type == bfd_link_hash_defined
5579 || h->root.root.type == bfd_link_hash_defweak)
5580 && h->root.root.u.def.section)
5581 {
5582 sec = h->root.root.u.def.section;
5583 if (sec->output_section)
5584 symbol = (h->root.root.u.def.value
5585 + sec->output_section->vma
5586 + sec->output_offset);
5587 else
5588 symbol = h->root.root.u.def.value;
5589 }
5590 else if (h->root.root.type == bfd_link_hash_undefweak)
5591 /* We allow relocations against undefined weak symbols, giving
5592 it the value zero, so that you can undefined weak functions
5593 and check to see if they exist by looking at their
5594 addresses. */
5595 symbol = 0;
59c2e50f 5596 else if (info->unresolved_syms_in_objects == RM_IGNORE
b49e97c9
TS
5597 && ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT)
5598 symbol = 0;
a4d0f181
TS
5599 else if (strcmp (*namep, SGI_COMPAT (input_bfd)
5600 ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING") == 0)
b49e97c9
TS
5601 {
5602 /* If this is a dynamic link, we should have created a
5603 _DYNAMIC_LINK symbol or _DYNAMIC_LINKING(for normal mips) symbol
de194d85 5604 in _bfd_mips_elf_create_dynamic_sections.
b49e97c9
TS
5605 Otherwise, we should define the symbol with a value of 0.
5606 FIXME: It should probably get into the symbol table
5607 somehow as well. */
0e1862bb 5608 BFD_ASSERT (! bfd_link_pic (info));
b49e97c9
TS
5609 BFD_ASSERT (bfd_get_section_by_name (abfd, ".dynamic") == NULL);
5610 symbol = 0;
5611 }
5e2b0d47
NC
5612 else if (ELF_MIPS_IS_OPTIONAL (h->root.other))
5613 {
5614 /* This is an optional symbol - an Irix specific extension to the
5615 ELF spec. Ignore it for now.
5616 XXX - FIXME - there is more to the spec for OPTIONAL symbols
5617 than simply ignoring them, but we do not handle this for now.
5618 For information see the "64-bit ELF Object File Specification"
5619 which is available from here:
5620 http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf */
5621 symbol = 0;
5622 }
b49e97c9
TS
5623 else
5624 {
95a51568
FS
5625 bfd_boolean reject_undefined
5626 = (info->unresolved_syms_in_objects == RM_DIAGNOSE
5627 && !info->warn_unresolved_syms)
5628 || ELF_ST_VISIBILITY (h->root.other) != STV_DEFAULT;
dfb93f11 5629
95a51568 5630 info->callbacks->undefined_symbol
1a72702b 5631 (info, h->root.root.root.string, input_bfd,
dfb93f11
JC
5632 input_section, relocation->r_offset, reject_undefined);
5633
5634 if (reject_undefined)
5635 return bfd_reloc_undefined;
5636
5637 symbol = 0;
b49e97c9
TS
5638 }
5639
30c09090 5640 target_is_16_bit_code_p = ELF_ST_IS_MIPS16 (h->root.other);
1bbce132 5641 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (h->root.other);
b49e97c9
TS
5642 }
5643
738e5348
RS
5644 /* If this is a reference to a 16-bit function with a stub, we need
5645 to redirect the relocation to the stub unless:
5646
5647 (a) the relocation is for a MIPS16 JAL;
5648
5649 (b) the relocation is for a MIPS16 PIC call, and there are no
5650 non-MIPS16 uses of the GOT slot; or
5651
5652 (c) the section allows direct references to MIPS16 functions. */
5653 if (r_type != R_MIPS16_26
0e1862bb 5654 && !bfd_link_relocatable (info)
738e5348
RS
5655 && ((h != NULL
5656 && h->fn_stub != NULL
5657 && (r_type != R_MIPS16_CALL16 || h->need_fn_stub))
b9d58d71 5658 || (local_p
698600e4
AM
5659 && mips_elf_tdata (input_bfd)->local_stubs != NULL
5660 && mips_elf_tdata (input_bfd)->local_stubs[r_symndx] != NULL))
738e5348 5661 && !section_allows_mips16_refs_p (input_section))
b49e97c9
TS
5662 {
5663 /* This is a 32- or 64-bit call to a 16-bit function. We should
5664 have already noticed that we were going to need the
5665 stub. */
5666 if (local_p)
8f0c309a 5667 {
698600e4 5668 sec = mips_elf_tdata (input_bfd)->local_stubs[r_symndx];
8f0c309a
CLT
5669 value = 0;
5670 }
b49e97c9
TS
5671 else
5672 {
5673 BFD_ASSERT (h->need_fn_stub);
8f0c309a
CLT
5674 if (h->la25_stub)
5675 {
5676 /* If a LA25 header for the stub itself exists, point to the
5677 prepended LUI/ADDIU sequence. */
5678 sec = h->la25_stub->stub_section;
5679 value = h->la25_stub->offset;
5680 }
5681 else
5682 {
5683 sec = h->fn_stub;
5684 value = 0;
5685 }
b49e97c9
TS
5686 }
5687
8f0c309a 5688 symbol = sec->output_section->vma + sec->output_offset + value;
f38c2df5
TS
5689 /* The target is 16-bit, but the stub isn't. */
5690 target_is_16_bit_code_p = FALSE;
b49e97c9 5691 }
1bbce132
MR
5692 /* If this is a MIPS16 call with a stub, that is made through the PLT or
5693 to a standard MIPS function, we need to redirect the call to the stub.
5694 Note that we specifically exclude R_MIPS16_CALL16 from this behavior;
5695 indirect calls should use an indirect stub instead. */
0e1862bb 5696 else if (r_type == R_MIPS16_26 && !bfd_link_relocatable (info)
b314ec0e 5697 && ((h != NULL && (h->call_stub != NULL || h->call_fp_stub != NULL))
b9d58d71 5698 || (local_p
698600e4
AM
5699 && mips_elf_tdata (input_bfd)->local_call_stubs != NULL
5700 && mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx] != NULL))
1bbce132 5701 && ((h != NULL && h->use_plt_entry) || !target_is_16_bit_code_p))
b49e97c9 5702 {
b9d58d71 5703 if (local_p)
698600e4 5704 sec = mips_elf_tdata (input_bfd)->local_call_stubs[r_symndx];
b9d58d71 5705 else
b49e97c9 5706 {
b9d58d71
TS
5707 /* If both call_stub and call_fp_stub are defined, we can figure
5708 out which one to use by checking which one appears in the input
5709 file. */
5710 if (h->call_stub != NULL && h->call_fp_stub != NULL)
b49e97c9 5711 {
b9d58d71 5712 asection *o;
68ffbac6 5713
b9d58d71
TS
5714 sec = NULL;
5715 for (o = input_bfd->sections; o != NULL; o = o->next)
b49e97c9 5716 {
fd361982 5717 if (CALL_FP_STUB_P (bfd_section_name (o)))
b9d58d71
TS
5718 {
5719 sec = h->call_fp_stub;
5720 break;
5721 }
b49e97c9 5722 }
b9d58d71
TS
5723 if (sec == NULL)
5724 sec = h->call_stub;
b49e97c9 5725 }
b9d58d71 5726 else if (h->call_stub != NULL)
b49e97c9 5727 sec = h->call_stub;
b9d58d71
TS
5728 else
5729 sec = h->call_fp_stub;
07d6d2b8 5730 }
b49e97c9 5731
eea6121a 5732 BFD_ASSERT (sec->size > 0);
b49e97c9
TS
5733 symbol = sec->output_section->vma + sec->output_offset;
5734 }
861fb55a
DJ
5735 /* If this is a direct call to a PIC function, redirect to the
5736 non-PIC stub. */
5737 else if (h != NULL && h->la25_stub
8f0c309a
CLT
5738 && mips_elf_relocation_needs_la25_stub (input_bfd, r_type,
5739 target_is_16_bit_code_p))
c7318def
MR
5740 {
5741 symbol = (h->la25_stub->stub_section->output_section->vma
5742 + h->la25_stub->stub_section->output_offset
5743 + h->la25_stub->offset);
5744 if (ELF_ST_IS_MICROMIPS (h->root.other))
5745 symbol |= 1;
5746 }
1bbce132
MR
5747 /* For direct MIPS16 and microMIPS calls make sure the compressed PLT
5748 entry is used if a standard PLT entry has also been made. In this
5749 case the symbol will have been set by mips_elf_set_plt_sym_value
5750 to point to the standard PLT entry, so redirect to the compressed
5751 one. */
54806ffa
MR
5752 else if ((mips16_branch_reloc_p (r_type)
5753 || micromips_branch_reloc_p (r_type))
0e1862bb 5754 && !bfd_link_relocatable (info)
1bbce132
MR
5755 && h != NULL
5756 && h->use_plt_entry
5757 && h->root.plt.plist->comp_offset != MINUS_ONE
5758 && h->root.plt.plist->mips_offset != MINUS_ONE)
5759 {
5760 bfd_boolean micromips_p = MICROMIPS_P (abfd);
5761
ce558b89 5762 sec = htab->root.splt;
1bbce132
MR
5763 symbol = (sec->output_section->vma
5764 + sec->output_offset
5765 + htab->plt_header_size
5766 + htab->plt_mips_offset
5767 + h->root.plt.plist->comp_offset
5768 + 1);
5769
5770 target_is_16_bit_code_p = !micromips_p;
5771 target_is_micromips_code_p = micromips_p;
5772 }
b49e97c9 5773
df58fc94 5774 /* Make sure MIPS16 and microMIPS are not used together. */
c9775dde 5775 if ((mips16_branch_reloc_p (r_type) && target_is_micromips_code_p)
df58fc94
RS
5776 || (micromips_branch_reloc_p (r_type) && target_is_16_bit_code_p))
5777 {
4eca0228 5778 _bfd_error_handler
df58fc94
RS
5779 (_("MIPS16 and microMIPS functions cannot call each other"));
5780 return bfd_reloc_notsupported;
5781 }
5782
b49e97c9 5783 /* Calls from 16-bit code to 32-bit code and vice versa require the
df58fc94
RS
5784 mode change. However, we can ignore calls to undefined weak symbols,
5785 which should never be executed at runtime. This exception is important
5786 because the assembly writer may have "known" that any definition of the
5787 symbol would be 16-bit code, and that direct jumps were therefore
5788 acceptable. */
0e1862bb 5789 *cross_mode_jump_p = (!bfd_link_relocatable (info)
df58fc94 5790 && !(h && h->root.root.type == bfd_link_hash_undefweak)
9d862524
MR
5791 && ((mips16_branch_reloc_p (r_type)
5792 && !target_is_16_bit_code_p)
5793 || (micromips_branch_reloc_p (r_type)
df58fc94 5794 && !target_is_micromips_code_p)
9d862524
MR
5795 || ((branch_reloc_p (r_type)
5796 || r_type == R_MIPS_JALR)
df58fc94
RS
5797 && (target_is_16_bit_code_p
5798 || target_is_micromips_code_p))));
b49e97c9 5799
47275900
MR
5800 resolved_to_zero = (h != NULL
5801 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->root));
5802
5803 switch (r_type)
5804 {
5805 case R_MIPS16_CALL16:
5806 case R_MIPS16_GOT16:
5807 case R_MIPS_CALL16:
5808 case R_MIPS_GOT16:
5809 case R_MIPS_GOT_PAGE:
5810 case R_MIPS_GOT_DISP:
5811 case R_MIPS_GOT_LO16:
5812 case R_MIPS_CALL_LO16:
5813 case R_MICROMIPS_CALL16:
5814 case R_MICROMIPS_GOT16:
5815 case R_MICROMIPS_GOT_PAGE:
5816 case R_MICROMIPS_GOT_DISP:
5817 case R_MICROMIPS_GOT_LO16:
5818 case R_MICROMIPS_CALL_LO16:
5819 if (resolved_to_zero
5820 && !bfd_link_relocatable (info)
5821 && mips_elf_nullify_got_load (input_bfd, contents,
5822 relocation, howto, TRUE))
5823 return bfd_reloc_continue;
5824
5825 /* Fall through. */
5826 case R_MIPS_GOT_HI16:
5827 case R_MIPS_CALL_HI16:
5828 case R_MICROMIPS_GOT_HI16:
5829 case R_MICROMIPS_CALL_HI16:
5830 if (resolved_to_zero
5831 && htab->use_absolute_zero
5832 && bfd_link_pic (info))
5833 {
5834 /* Redirect to the special `__gnu_absolute_zero' symbol. */
5835 h = mips_elf_link_hash_lookup (htab, "__gnu_absolute_zero",
5836 FALSE, FALSE, FALSE);
5837 BFD_ASSERT (h != NULL);
5838 }
5839 break;
5840 }
5841
c5d6fa44 5842 local_p = (h == NULL || mips_use_local_got_p (info, h));
b49e97c9 5843
0a61c8c2
RS
5844 gp0 = _bfd_get_gp_value (input_bfd);
5845 gp = _bfd_get_gp_value (abfd);
23cc69b6 5846 if (htab->got_info)
a8028dd0 5847 gp += mips_elf_adjust_gp (abfd, htab->got_info, input_bfd);
0a61c8c2
RS
5848
5849 if (gnu_local_gp_p)
5850 symbol = gp;
5851
df58fc94
RS
5852 /* Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
5853 to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP. The addend is applied by the
5854 corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST. */
5855 if (got_page_reloc_p (r_type) && !local_p)
020d7251 5856 {
df58fc94
RS
5857 r_type = (micromips_reloc_p (r_type)
5858 ? R_MICROMIPS_GOT_DISP : R_MIPS_GOT_DISP);
020d7251
RS
5859 addend = 0;
5860 }
5861
e77760d2 5862 /* If we haven't already determined the GOT offset, and we're going
0a61c8c2 5863 to need it, get it now. */
b49e97c9
TS
5864 switch (r_type)
5865 {
738e5348
RS
5866 case R_MIPS16_CALL16:
5867 case R_MIPS16_GOT16:
b49e97c9
TS
5868 case R_MIPS_CALL16:
5869 case R_MIPS_GOT16:
5870 case R_MIPS_GOT_DISP:
5871 case R_MIPS_GOT_HI16:
5872 case R_MIPS_CALL_HI16:
5873 case R_MIPS_GOT_LO16:
5874 case R_MIPS_CALL_LO16:
df58fc94
RS
5875 case R_MICROMIPS_CALL16:
5876 case R_MICROMIPS_GOT16:
5877 case R_MICROMIPS_GOT_DISP:
5878 case R_MICROMIPS_GOT_HI16:
5879 case R_MICROMIPS_CALL_HI16:
5880 case R_MICROMIPS_GOT_LO16:
5881 case R_MICROMIPS_CALL_LO16:
0f20cc35
DJ
5882 case R_MIPS_TLS_GD:
5883 case R_MIPS_TLS_GOTTPREL:
5884 case R_MIPS_TLS_LDM:
d0f13682
CLT
5885 case R_MIPS16_TLS_GD:
5886 case R_MIPS16_TLS_GOTTPREL:
5887 case R_MIPS16_TLS_LDM:
df58fc94
RS
5888 case R_MICROMIPS_TLS_GD:
5889 case R_MICROMIPS_TLS_GOTTPREL:
5890 case R_MICROMIPS_TLS_LDM:
b49e97c9 5891 /* Find the index into the GOT where this value is located. */
df58fc94 5892 if (tls_ldm_reloc_p (r_type))
0f20cc35 5893 {
0a44bf69 5894 g = mips_elf_local_got_index (abfd, input_bfd, info,
5c18022e 5895 0, 0, NULL, r_type);
0f20cc35
DJ
5896 if (g == MINUS_ONE)
5897 return bfd_reloc_outofrange;
5898 }
5899 else if (!local_p)
b49e97c9 5900 {
0a44bf69
RS
5901 /* On VxWorks, CALL relocations should refer to the .got.plt
5902 entry, which is initialized to point at the PLT stub. */
90c14f0c 5903 if (htab->root.target_os == is_vxworks
df58fc94
RS
5904 && (call_hi16_reloc_p (r_type)
5905 || call_lo16_reloc_p (r_type)
738e5348 5906 || call16_reloc_p (r_type)))
0a44bf69
RS
5907 {
5908 BFD_ASSERT (addend == 0);
5909 BFD_ASSERT (h->root.needs_plt);
5910 g = mips_elf_gotplt_index (info, &h->root);
5911 }
5912 else
b49e97c9 5913 {
020d7251 5914 BFD_ASSERT (addend == 0);
13fbec83
RS
5915 g = mips_elf_global_got_index (abfd, info, input_bfd,
5916 &h->root, r_type);
e641e783 5917 if (!TLS_RELOC_P (r_type)
020d7251
RS
5918 && !elf_hash_table (info)->dynamic_sections_created)
5919 /* This is a static link. We must initialize the GOT entry. */
ce558b89 5920 MIPS_ELF_PUT_WORD (dynobj, symbol, htab->root.sgot->contents + g);
b49e97c9
TS
5921 }
5922 }
90c14f0c 5923 else if (htab->root.target_os != is_vxworks
738e5348 5924 && (call16_reloc_p (r_type) || got16_reloc_p (r_type)))
0a44bf69 5925 /* The calculation below does not involve "g". */
b49e97c9
TS
5926 break;
5927 else
5928 {
5c18022e 5929 g = mips_elf_local_got_index (abfd, input_bfd, info,
0a44bf69 5930 symbol + addend, r_symndx, h, r_type);
b49e97c9
TS
5931 if (g == MINUS_ONE)
5932 return bfd_reloc_outofrange;
5933 }
5934
5935 /* Convert GOT indices to actual offsets. */
a8028dd0 5936 g = mips_elf_got_offset_from_index (info, abfd, input_bfd, g);
b49e97c9 5937 break;
b49e97c9
TS
5938 }
5939
0a44bf69
RS
5940 /* Relocations against the VxWorks __GOTT_BASE__ and __GOTT_INDEX__
5941 symbols are resolved by the loader. Add them to .rela.dyn. */
5942 if (h != NULL && is_gott_symbol (info, &h->root))
5943 {
5944 Elf_Internal_Rela outrel;
5945 bfd_byte *loc;
5946 asection *s;
5947
5948 s = mips_elf_rel_dyn_section (info, FALSE);
5949 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
5950
5951 outrel.r_offset = (input_section->output_section->vma
5952 + input_section->output_offset
5953 + relocation->r_offset);
5954 outrel.r_info = ELF32_R_INFO (h->root.dynindx, r_type);
5955 outrel.r_addend = addend;
5956 bfd_elf32_swap_reloca_out (abfd, &outrel, loc);
9e3313ae
RS
5957
5958 /* If we've written this relocation for a readonly section,
5959 we need to set DF_TEXTREL again, so that we do not delete the
5960 DT_TEXTREL tag. */
5961 if (MIPS_ELF_READONLY_SECTION (input_section))
5962 info->flags |= DF_TEXTREL;
5963
0a44bf69
RS
5964 *valuep = 0;
5965 return bfd_reloc_ok;
5966 }
5967
b49e97c9
TS
5968 /* Figure out what kind of relocation is being performed. */
5969 switch (r_type)
5970 {
5971 case R_MIPS_NONE:
5972 return bfd_reloc_continue;
5973
5974 case R_MIPS_16:
c3eb94b4
MF
5975 if (howto->partial_inplace)
5976 addend = _bfd_mips_elf_sign_extend (addend, 16);
5977 value = symbol + addend;
b49e97c9
TS
5978 overflowed_p = mips_elf_overflow_p (value, 16);
5979 break;
5980
5981 case R_MIPS_32:
5982 case R_MIPS_REL32:
5983 case R_MIPS_64:
0e1862bb 5984 if ((bfd_link_pic (info)
861fb55a 5985 || (htab->root.dynamic_sections_created
b49e97c9 5986 && h != NULL
f5385ebf 5987 && h->root.def_dynamic
861fb55a
DJ
5988 && !h->root.def_regular
5989 && !h->has_static_relocs))
cf35638d 5990 && r_symndx != STN_UNDEF
9a59ad6b
DJ
5991 && (h == NULL
5992 || h->root.root.type != bfd_link_hash_undefweak
ad951203
L
5993 || (ELF_ST_VISIBILITY (h->root.other) == STV_DEFAULT
5994 && !resolved_to_zero))
b49e97c9
TS
5995 && (input_section->flags & SEC_ALLOC) != 0)
5996 {
861fb55a 5997 /* If we're creating a shared library, then we can't know
b49e97c9
TS
5998 where the symbol will end up. So, we create a relocation
5999 record in the output, and leave the job up to the dynamic
861fb55a
DJ
6000 linker. We must do the same for executable references to
6001 shared library symbols, unless we've decided to use copy
6002 relocs or PLTs instead. */
b49e97c9
TS
6003 value = addend;
6004 if (!mips_elf_create_dynamic_relocation (abfd,
6005 info,
6006 relocation,
6007 h,
6008 sec,
6009 symbol,
6010 &value,
6011 input_section))
6012 return bfd_reloc_undefined;
6013 }
6014 else
6015 {
6016 if (r_type != R_MIPS_REL32)
6017 value = symbol + addend;
6018 else
6019 value = addend;
6020 }
6021 value &= howto->dst_mask;
092dcd75
CD
6022 break;
6023
6024 case R_MIPS_PC32:
6025 value = symbol + addend - p;
6026 value &= howto->dst_mask;
b49e97c9
TS
6027 break;
6028
b49e97c9
TS
6029 case R_MIPS16_26:
6030 /* The calculation for R_MIPS16_26 is just the same as for an
6031 R_MIPS_26. It's only the storage of the relocated field into
6032 the output file that's different. That's handled in
6033 mips_elf_perform_relocation. So, we just fall through to the
6034 R_MIPS_26 case here. */
6035 case R_MIPS_26:
df58fc94
RS
6036 case R_MICROMIPS_26_S1:
6037 {
6038 unsigned int shift;
6039
df58fc94
RS
6040 /* Shift is 2, unusually, for microMIPS JALX. */
6041 shift = (!*cross_mode_jump_p && r_type == R_MICROMIPS_26_S1) ? 1 : 2;
6042
77434823 6043 if (howto->partial_inplace && !section_p)
df58fc94 6044 value = _bfd_mips_elf_sign_extend (addend, 26 + shift);
c3eb94b4
MF
6045 else
6046 value = addend;
bc27bb05
MR
6047 value += symbol;
6048
9d862524
MR
6049 /* Make sure the target of a jump is suitably aligned. Bit 0 must
6050 be the correct ISA mode selector except for weak undefined
6051 symbols. */
6052 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6053 && (*cross_mode_jump_p
6054 ? (value & 3) != (r_type == R_MIPS_26)
07d6d2b8 6055 : (value & ((1 << shift) - 1)) != (r_type != R_MIPS_26)))
bc27bb05
MR
6056 return bfd_reloc_outofrange;
6057
6058 value >>= shift;
77434823 6059 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
df58fc94
RS
6060 overflowed_p = (value >> 26) != ((p + 4) >> (26 + shift));
6061 value &= howto->dst_mask;
6062 }
b49e97c9
TS
6063 break;
6064
0f20cc35 6065 case R_MIPS_TLS_DTPREL_HI16:
d0f13682 6066 case R_MIPS16_TLS_DTPREL_HI16:
df58fc94 6067 case R_MICROMIPS_TLS_DTPREL_HI16:
0f20cc35
DJ
6068 value = (mips_elf_high (addend + symbol - dtprel_base (info))
6069 & howto->dst_mask);
6070 break;
6071
6072 case R_MIPS_TLS_DTPREL_LO16:
741d6ea8
JM
6073 case R_MIPS_TLS_DTPREL32:
6074 case R_MIPS_TLS_DTPREL64:
d0f13682 6075 case R_MIPS16_TLS_DTPREL_LO16:
df58fc94 6076 case R_MICROMIPS_TLS_DTPREL_LO16:
0f20cc35
DJ
6077 value = (symbol + addend - dtprel_base (info)) & howto->dst_mask;
6078 break;
6079
6080 case R_MIPS_TLS_TPREL_HI16:
d0f13682 6081 case R_MIPS16_TLS_TPREL_HI16:
df58fc94 6082 case R_MICROMIPS_TLS_TPREL_HI16:
0f20cc35
DJ
6083 value = (mips_elf_high (addend + symbol - tprel_base (info))
6084 & howto->dst_mask);
6085 break;
6086
6087 case R_MIPS_TLS_TPREL_LO16:
d0f13682
CLT
6088 case R_MIPS_TLS_TPREL32:
6089 case R_MIPS_TLS_TPREL64:
6090 case R_MIPS16_TLS_TPREL_LO16:
df58fc94 6091 case R_MICROMIPS_TLS_TPREL_LO16:
0f20cc35
DJ
6092 value = (symbol + addend - tprel_base (info)) & howto->dst_mask;
6093 break;
6094
b49e97c9 6095 case R_MIPS_HI16:
d6f16593 6096 case R_MIPS16_HI16:
df58fc94 6097 case R_MICROMIPS_HI16:
b49e97c9
TS
6098 if (!gp_disp_p)
6099 {
6100 value = mips_elf_high (addend + symbol);
6101 value &= howto->dst_mask;
6102 }
6103 else
6104 {
d6f16593 6105 /* For MIPS16 ABI code we generate this sequence
07d6d2b8
AM
6106 0: li $v0,%hi(_gp_disp)
6107 4: addiupc $v1,%lo(_gp_disp)
6108 8: sll $v0,16
d6f16593
MR
6109 12: addu $v0,$v1
6110 14: move $gp,$v0
6111 So the offsets of hi and lo relocs are the same, but the
888b9c01
CLT
6112 base $pc is that used by the ADDIUPC instruction at $t9 + 4.
6113 ADDIUPC clears the low two bits of the instruction address,
6114 so the base is ($t9 + 4) & ~3. */
d6f16593 6115 if (r_type == R_MIPS16_HI16)
888b9c01 6116 value = mips_elf_high (addend + gp - ((p + 4) & ~(bfd_vma) 0x3));
df58fc94
RS
6117 /* The microMIPS .cpload sequence uses the same assembly
6118 instructions as the traditional psABI version, but the
6119 incoming $t9 has the low bit set. */
6120 else if (r_type == R_MICROMIPS_HI16)
6121 value = mips_elf_high (addend + gp - p - 1);
d6f16593
MR
6122 else
6123 value = mips_elf_high (addend + gp - p);
b49e97c9
TS
6124 }
6125 break;
6126
6127 case R_MIPS_LO16:
d6f16593 6128 case R_MIPS16_LO16:
df58fc94
RS
6129 case R_MICROMIPS_LO16:
6130 case R_MICROMIPS_HI0_LO16:
b49e97c9
TS
6131 if (!gp_disp_p)
6132 value = (symbol + addend) & howto->dst_mask;
6133 else
6134 {
d6f16593
MR
6135 /* See the comment for R_MIPS16_HI16 above for the reason
6136 for this conditional. */
6137 if (r_type == R_MIPS16_LO16)
888b9c01 6138 value = addend + gp - (p & ~(bfd_vma) 0x3);
df58fc94
RS
6139 else if (r_type == R_MICROMIPS_LO16
6140 || r_type == R_MICROMIPS_HI0_LO16)
6141 value = addend + gp - p + 3;
d6f16593
MR
6142 else
6143 value = addend + gp - p + 4;
b49e97c9 6144 /* The MIPS ABI requires checking the R_MIPS_LO16 relocation
8dc1a139 6145 for overflow. But, on, say, IRIX5, relocations against
b49e97c9
TS
6146 _gp_disp are normally generated from the .cpload
6147 pseudo-op. It generates code that normally looks like
6148 this:
6149
6150 lui $gp,%hi(_gp_disp)
6151 addiu $gp,$gp,%lo(_gp_disp)
6152 addu $gp,$gp,$t9
6153
6154 Here $t9 holds the address of the function being called,
6155 as required by the MIPS ELF ABI. The R_MIPS_LO16
6156 relocation can easily overflow in this situation, but the
6157 R_MIPS_HI16 relocation will handle the overflow.
6158 Therefore, we consider this a bug in the MIPS ABI, and do
6159 not check for overflow here. */
6160 }
6161 break;
6162
6163 case R_MIPS_LITERAL:
df58fc94 6164 case R_MICROMIPS_LITERAL:
b49e97c9
TS
6165 /* Because we don't merge literal sections, we can handle this
6166 just like R_MIPS_GPREL16. In the long run, we should merge
6167 shared literals, and then we will need to additional work
6168 here. */
6169
6170 /* Fall through. */
6171
6172 case R_MIPS16_GPREL:
6173 /* The R_MIPS16_GPREL performs the same calculation as
6174 R_MIPS_GPREL16, but stores the relocated bits in a different
6175 order. We don't need to do anything special here; the
6176 differences are handled in mips_elf_perform_relocation. */
6177 case R_MIPS_GPREL16:
df58fc94
RS
6178 case R_MICROMIPS_GPREL7_S2:
6179 case R_MICROMIPS_GPREL16:
bce03d3d
AO
6180 /* Only sign-extend the addend if it was extracted from the
6181 instruction. If the addend was separate, leave it alone,
6182 otherwise we may lose significant bits. */
6183 if (howto->partial_inplace)
a7ebbfdf 6184 addend = _bfd_mips_elf_sign_extend (addend, 16);
bce03d3d
AO
6185 value = symbol + addend - gp;
6186 /* If the symbol was local, any earlier relocatable links will
6187 have adjusted its addend with the gp offset, so compensate
6188 for that now. Don't do it for symbols forced local in this
6189 link, though, since they won't have had the gp offset applied
6190 to them before. */
6191 if (was_local_p)
6192 value += gp0;
538baf8b
AB
6193 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6194 overflowed_p = mips_elf_overflow_p (value, 16);
b49e97c9
TS
6195 break;
6196
738e5348
RS
6197 case R_MIPS16_GOT16:
6198 case R_MIPS16_CALL16:
b49e97c9
TS
6199 case R_MIPS_GOT16:
6200 case R_MIPS_CALL16:
df58fc94
RS
6201 case R_MICROMIPS_GOT16:
6202 case R_MICROMIPS_CALL16:
0a44bf69 6203 /* VxWorks does not have separate local and global semantics for
738e5348 6204 R_MIPS*_GOT16; every relocation evaluates to "G". */
90c14f0c 6205 if (htab->root.target_os != is_vxworks && local_p)
b49e97c9 6206 {
5c18022e 6207 value = mips_elf_got16_entry (abfd, input_bfd, info,
020d7251 6208 symbol + addend, !was_local_p);
b49e97c9
TS
6209 if (value == MINUS_ONE)
6210 return bfd_reloc_outofrange;
6211 value
a8028dd0 6212 = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
6213 overflowed_p = mips_elf_overflow_p (value, 16);
6214 break;
6215 }
6216
6217 /* Fall through. */
6218
0f20cc35
DJ
6219 case R_MIPS_TLS_GD:
6220 case R_MIPS_TLS_GOTTPREL:
6221 case R_MIPS_TLS_LDM:
b49e97c9 6222 case R_MIPS_GOT_DISP:
d0f13682
CLT
6223 case R_MIPS16_TLS_GD:
6224 case R_MIPS16_TLS_GOTTPREL:
6225 case R_MIPS16_TLS_LDM:
df58fc94
RS
6226 case R_MICROMIPS_TLS_GD:
6227 case R_MICROMIPS_TLS_GOTTPREL:
6228 case R_MICROMIPS_TLS_LDM:
6229 case R_MICROMIPS_GOT_DISP:
b49e97c9
TS
6230 value = g;
6231 overflowed_p = mips_elf_overflow_p (value, 16);
6232 break;
6233
6234 case R_MIPS_GPREL32:
bce03d3d
AO
6235 value = (addend + symbol + gp0 - gp);
6236 if (!save_addend)
6237 value &= howto->dst_mask;
b49e97c9
TS
6238 break;
6239
6240 case R_MIPS_PC16:
bad36eac 6241 case R_MIPS_GNU_REL16_S2:
c3eb94b4
MF
6242 if (howto->partial_inplace)
6243 addend = _bfd_mips_elf_sign_extend (addend, 18);
6244
9d862524 6245 /* No need to exclude weak undefined symbols here as they resolve
07d6d2b8
AM
6246 to 0 and never set `*cross_mode_jump_p', so this alignment check
6247 will never trigger for them. */
9d862524
MR
6248 if (*cross_mode_jump_p
6249 ? ((symbol + addend) & 3) != 1
6250 : ((symbol + addend) & 3) != 0)
c3eb94b4
MF
6251 return bfd_reloc_outofrange;
6252
6253 value = symbol + addend - p;
538baf8b
AB
6254 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6255 overflowed_p = mips_elf_overflow_p (value, 18);
37caec6b
TS
6256 value >>= howto->rightshift;
6257 value &= howto->dst_mask;
b49e97c9
TS
6258 break;
6259
c9775dde
MR
6260 case R_MIPS16_PC16_S1:
6261 if (howto->partial_inplace)
6262 addend = _bfd_mips_elf_sign_extend (addend, 17);
6263
6264 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
9d862524
MR
6265 && (*cross_mode_jump_p
6266 ? ((symbol + addend) & 3) != 0
6267 : ((symbol + addend) & 1) == 0))
c9775dde
MR
6268 return bfd_reloc_outofrange;
6269
6270 value = symbol + addend - p;
6271 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6272 overflowed_p = mips_elf_overflow_p (value, 17);
6273 value >>= howto->rightshift;
6274 value &= howto->dst_mask;
6275 break;
6276
7361da2c
AB
6277 case R_MIPS_PC21_S2:
6278 if (howto->partial_inplace)
6279 addend = _bfd_mips_elf_sign_extend (addend, 23);
6280
6281 if ((symbol + addend) & 3)
6282 return bfd_reloc_outofrange;
6283
6284 value = symbol + addend - p;
538baf8b
AB
6285 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6286 overflowed_p = mips_elf_overflow_p (value, 23);
7361da2c
AB
6287 value >>= howto->rightshift;
6288 value &= howto->dst_mask;
6289 break;
6290
6291 case R_MIPS_PC26_S2:
6292 if (howto->partial_inplace)
6293 addend = _bfd_mips_elf_sign_extend (addend, 28);
6294
6295 if ((symbol + addend) & 3)
6296 return bfd_reloc_outofrange;
6297
6298 value = symbol + addend - p;
538baf8b
AB
6299 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6300 overflowed_p = mips_elf_overflow_p (value, 28);
7361da2c
AB
6301 value >>= howto->rightshift;
6302 value &= howto->dst_mask;
6303 break;
6304
6305 case R_MIPS_PC18_S3:
6306 if (howto->partial_inplace)
6307 addend = _bfd_mips_elf_sign_extend (addend, 21);
6308
6309 if ((symbol + addend) & 7)
6310 return bfd_reloc_outofrange;
6311
6312 value = symbol + addend - ((p | 7) ^ 7);
538baf8b
AB
6313 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6314 overflowed_p = mips_elf_overflow_p (value, 21);
7361da2c
AB
6315 value >>= howto->rightshift;
6316 value &= howto->dst_mask;
6317 break;
6318
6319 case R_MIPS_PC19_S2:
6320 if (howto->partial_inplace)
6321 addend = _bfd_mips_elf_sign_extend (addend, 21);
6322
6323 if ((symbol + addend) & 3)
6324 return bfd_reloc_outofrange;
6325
6326 value = symbol + addend - p;
538baf8b
AB
6327 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6328 overflowed_p = mips_elf_overflow_p (value, 21);
7361da2c
AB
6329 value >>= howto->rightshift;
6330 value &= howto->dst_mask;
6331 break;
6332
6333 case R_MIPS_PCHI16:
6334 value = mips_elf_high (symbol + addend - p);
7361da2c
AB
6335 value &= howto->dst_mask;
6336 break;
6337
6338 case R_MIPS_PCLO16:
6339 if (howto->partial_inplace)
6340 addend = _bfd_mips_elf_sign_extend (addend, 16);
6341 value = symbol + addend - p;
6342 value &= howto->dst_mask;
6343 break;
6344
df58fc94 6345 case R_MICROMIPS_PC7_S1:
c3eb94b4
MF
6346 if (howto->partial_inplace)
6347 addend = _bfd_mips_elf_sign_extend (addend, 8);
9d862524
MR
6348
6349 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6350 && (*cross_mode_jump_p
6351 ? ((symbol + addend + 2) & 3) != 0
6352 : ((symbol + addend + 2) & 1) == 0))
6353 return bfd_reloc_outofrange;
6354
c3eb94b4 6355 value = symbol + addend - p;
538baf8b
AB
6356 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6357 overflowed_p = mips_elf_overflow_p (value, 8);
df58fc94
RS
6358 value >>= howto->rightshift;
6359 value &= howto->dst_mask;
6360 break;
6361
6362 case R_MICROMIPS_PC10_S1:
c3eb94b4
MF
6363 if (howto->partial_inplace)
6364 addend = _bfd_mips_elf_sign_extend (addend, 11);
9d862524
MR
6365
6366 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6367 && (*cross_mode_jump_p
6368 ? ((symbol + addend + 2) & 3) != 0
6369 : ((symbol + addend + 2) & 1) == 0))
6370 return bfd_reloc_outofrange;
6371
c3eb94b4 6372 value = symbol + addend - p;
538baf8b
AB
6373 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6374 overflowed_p = mips_elf_overflow_p (value, 11);
df58fc94
RS
6375 value >>= howto->rightshift;
6376 value &= howto->dst_mask;
6377 break;
6378
6379 case R_MICROMIPS_PC16_S1:
c3eb94b4
MF
6380 if (howto->partial_inplace)
6381 addend = _bfd_mips_elf_sign_extend (addend, 17);
9d862524
MR
6382
6383 if ((was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6384 && (*cross_mode_jump_p
6385 ? ((symbol + addend) & 3) != 0
6386 : ((symbol + addend) & 1) == 0))
6387 return bfd_reloc_outofrange;
6388
c3eb94b4 6389 value = symbol + addend - p;
538baf8b
AB
6390 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6391 overflowed_p = mips_elf_overflow_p (value, 17);
df58fc94
RS
6392 value >>= howto->rightshift;
6393 value &= howto->dst_mask;
6394 break;
6395
6396 case R_MICROMIPS_PC23_S2:
c3eb94b4
MF
6397 if (howto->partial_inplace)
6398 addend = _bfd_mips_elf_sign_extend (addend, 25);
6399 value = symbol + addend - ((p | 3) ^ 3);
538baf8b
AB
6400 if (was_local_p || h->root.root.type != bfd_link_hash_undefweak)
6401 overflowed_p = mips_elf_overflow_p (value, 25);
df58fc94
RS
6402 value >>= howto->rightshift;
6403 value &= howto->dst_mask;
6404 break;
6405
b49e97c9
TS
6406 case R_MIPS_GOT_HI16:
6407 case R_MIPS_CALL_HI16:
df58fc94
RS
6408 case R_MICROMIPS_GOT_HI16:
6409 case R_MICROMIPS_CALL_HI16:
b49e97c9
TS
6410 /* We're allowed to handle these two relocations identically.
6411 The dynamic linker is allowed to handle the CALL relocations
6412 differently by creating a lazy evaluation stub. */
6413 value = g;
6414 value = mips_elf_high (value);
6415 value &= howto->dst_mask;
6416 break;
6417
6418 case R_MIPS_GOT_LO16:
6419 case R_MIPS_CALL_LO16:
df58fc94
RS
6420 case R_MICROMIPS_GOT_LO16:
6421 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
6422 value = g & howto->dst_mask;
6423 break;
6424
6425 case R_MIPS_GOT_PAGE:
df58fc94 6426 case R_MICROMIPS_GOT_PAGE:
5c18022e 6427 value = mips_elf_got_page (abfd, input_bfd, info, symbol + addend, NULL);
b49e97c9
TS
6428 if (value == MINUS_ONE)
6429 return bfd_reloc_outofrange;
a8028dd0 6430 value = mips_elf_got_offset_from_index (info, abfd, input_bfd, value);
b49e97c9
TS
6431 overflowed_p = mips_elf_overflow_p (value, 16);
6432 break;
6433
6434 case R_MIPS_GOT_OFST:
df58fc94 6435 case R_MICROMIPS_GOT_OFST:
93a2b7ae 6436 if (local_p)
5c18022e 6437 mips_elf_got_page (abfd, input_bfd, info, symbol + addend, &value);
0fdc1bf1
AO
6438 else
6439 value = addend;
b49e97c9
TS
6440 overflowed_p = mips_elf_overflow_p (value, 16);
6441 break;
6442
6443 case R_MIPS_SUB:
df58fc94 6444 case R_MICROMIPS_SUB:
b49e97c9
TS
6445 value = symbol - addend;
6446 value &= howto->dst_mask;
6447 break;
6448
6449 case R_MIPS_HIGHER:
df58fc94 6450 case R_MICROMIPS_HIGHER:
b49e97c9
TS
6451 value = mips_elf_higher (addend + symbol);
6452 value &= howto->dst_mask;
6453 break;
6454
6455 case R_MIPS_HIGHEST:
df58fc94 6456 case R_MICROMIPS_HIGHEST:
b49e97c9
TS
6457 value = mips_elf_highest (addend + symbol);
6458 value &= howto->dst_mask;
6459 break;
6460
6461 case R_MIPS_SCN_DISP:
df58fc94 6462 case R_MICROMIPS_SCN_DISP:
b49e97c9
TS
6463 value = symbol + addend - sec->output_offset;
6464 value &= howto->dst_mask;
6465 break;
6466
b49e97c9 6467 case R_MIPS_JALR:
df58fc94 6468 case R_MICROMIPS_JALR:
1367d393
ILT
6469 /* This relocation is only a hint. In some cases, we optimize
6470 it into a bal instruction. But we don't try to optimize
5bbc5ae7
AN
6471 when the symbol does not resolve locally. */
6472 if (h != NULL && !SYMBOL_CALLS_LOCAL (info, &h->root))
1367d393 6473 return bfd_reloc_continue;
c1556ecd
MR
6474 /* We can't optimize cross-mode jumps either. */
6475 if (*cross_mode_jump_p)
6476 return bfd_reloc_continue;
1367d393 6477 value = symbol + addend;
c1556ecd
MR
6478 /* Neither we can non-instruction-aligned targets. */
6479 if (r_type == R_MIPS_JALR ? (value & 3) != 0 : (value & 1) == 0)
6480 return bfd_reloc_continue;
1367d393 6481 break;
b49e97c9 6482
1367d393 6483 case R_MIPS_PJUMP:
b49e97c9
TS
6484 case R_MIPS_GNU_VTINHERIT:
6485 case R_MIPS_GNU_VTENTRY:
6486 /* We don't do anything with these at present. */
6487 return bfd_reloc_continue;
6488
6489 default:
6490 /* An unrecognized relocation type. */
6491 return bfd_reloc_notsupported;
6492 }
6493
6494 /* Store the VALUE for our caller. */
6495 *valuep = value;
6496 return overflowed_p ? bfd_reloc_overflow : bfd_reloc_ok;
6497}
6498
b49e97c9
TS
6499/* It has been determined that the result of the RELOCATION is the
6500 VALUE. Use HOWTO to place VALUE into the output file at the
6501 appropriate position. The SECTION is the section to which the
68ffbac6 6502 relocation applies.
38a7df63 6503 CROSS_MODE_JUMP_P is true if the relocation field
df58fc94 6504 is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
b49e97c9 6505
b34976b6 6506 Returns FALSE if anything goes wrong. */
b49e97c9 6507
b34976b6 6508static bfd_boolean
9719ad41
RS
6509mips_elf_perform_relocation (struct bfd_link_info *info,
6510 reloc_howto_type *howto,
6511 const Elf_Internal_Rela *relocation,
6512 bfd_vma value, bfd *input_bfd,
6513 asection *input_section, bfd_byte *contents,
38a7df63 6514 bfd_boolean cross_mode_jump_p)
b49e97c9
TS
6515{
6516 bfd_vma x;
6517 bfd_byte *location;
6518 int r_type = ELF_R_TYPE (input_bfd, relocation->r_info);
6519
6520 /* Figure out where the relocation is occurring. */
6521 location = contents + relocation->r_offset;
6522
df58fc94 6523 _bfd_mips_elf_reloc_unshuffle (input_bfd, r_type, FALSE, location);
d6f16593 6524
b49e97c9
TS
6525 /* Obtain the current value. */
6526 x = mips_elf_obtain_contents (howto, relocation, input_bfd, contents);
6527
6528 /* Clear the field we are setting. */
6529 x &= ~howto->dst_mask;
6530
b49e97c9
TS
6531 /* Set the field. */
6532 x |= (value & howto->dst_mask);
6533
a6ebf616 6534 /* Detect incorrect JALX usage. If required, turn JAL or BAL into JALX. */
9d862524
MR
6535 if (!cross_mode_jump_p && jal_reloc_p (r_type))
6536 {
6537 bfd_vma opcode = x >> 26;
6538
6539 if (r_type == R_MIPS16_26 ? opcode == 0x7
6540 : r_type == R_MICROMIPS_26_S1 ? opcode == 0x3c
6541 : opcode == 0x1d)
6542 {
6543 info->callbacks->einfo
2c1c9679 6544 (_("%X%H: unsupported JALX to the same ISA mode\n"),
9d862524
MR
6545 input_bfd, input_section, relocation->r_offset);
6546 return TRUE;
6547 }
6548 }
38a7df63 6549 if (cross_mode_jump_p && jal_reloc_p (r_type))
b49e97c9 6550 {
b34976b6 6551 bfd_boolean ok;
b49e97c9
TS
6552 bfd_vma opcode = x >> 26;
6553 bfd_vma jalx_opcode;
6554
6555 /* Check to see if the opcode is already JAL or JALX. */
6556 if (r_type == R_MIPS16_26)
6557 {
6558 ok = ((opcode == 0x6) || (opcode == 0x7));
6559 jalx_opcode = 0x7;
6560 }
df58fc94
RS
6561 else if (r_type == R_MICROMIPS_26_S1)
6562 {
6563 ok = ((opcode == 0x3d) || (opcode == 0x3c));
6564 jalx_opcode = 0x3c;
6565 }
b49e97c9
TS
6566 else
6567 {
6568 ok = ((opcode == 0x3) || (opcode == 0x1d));
6569 jalx_opcode = 0x1d;
6570 }
6571
3bdf9505 6572 /* If the opcode is not JAL or JALX, there's a problem. We cannot
07d6d2b8 6573 convert J or JALS to JALX. */
b49e97c9
TS
6574 if (!ok)
6575 {
5f68df25 6576 info->callbacks->einfo
2c1c9679 6577 (_("%X%H: unsupported jump between ISA modes; "
5f68df25
MR
6578 "consider recompiling with interlinking enabled\n"),
6579 input_bfd, input_section, relocation->r_offset);
6580 return TRUE;
b49e97c9
TS
6581 }
6582
6583 /* Make this the JALX opcode. */
2365f8d7 6584 x = (x & ~(0x3fu << 26)) | (jalx_opcode << 26);
b49e97c9 6585 }
9d862524
MR
6586 else if (cross_mode_jump_p && b_reloc_p (r_type))
6587 {
a6ebf616
MR
6588 bfd_boolean ok = FALSE;
6589 bfd_vma opcode = x >> 16;
6590 bfd_vma jalx_opcode = 0;
70e65ca8 6591 bfd_vma sign_bit = 0;
a6ebf616
MR
6592 bfd_vma addr;
6593 bfd_vma dest;
6594
6595 if (r_type == R_MICROMIPS_PC16_S1)
6596 {
6597 ok = opcode == 0x4060;
6598 jalx_opcode = 0x3c;
70e65ca8 6599 sign_bit = 0x10000;
a6ebf616
MR
6600 value <<= 1;
6601 }
6602 else if (r_type == R_MIPS_PC16 || r_type == R_MIPS_GNU_REL16_S2)
6603 {
6604 ok = opcode == 0x411;
6605 jalx_opcode = 0x1d;
70e65ca8 6606 sign_bit = 0x20000;
a6ebf616
MR
6607 value <<= 2;
6608 }
6609
8b10b0b3 6610 if (ok && !bfd_link_pic (info))
a6ebf616 6611 {
8b10b0b3
MR
6612 addr = (input_section->output_section->vma
6613 + input_section->output_offset
6614 + relocation->r_offset
6615 + 4);
70e65ca8
MR
6616 dest = (addr
6617 + (((value & ((sign_bit << 1) - 1)) ^ sign_bit) - sign_bit));
a6ebf616 6618
8b10b0b3
MR
6619 if ((addr >> 28) << 28 != (dest >> 28) << 28)
6620 {
6621 info->callbacks->einfo
2c1c9679 6622 (_("%X%H: cannot convert branch between ISA modes "
8b10b0b3
MR
6623 "to JALX: relocation out of range\n"),
6624 input_bfd, input_section, relocation->r_offset);
6625 return TRUE;
6626 }
a6ebf616 6627
8b10b0b3
MR
6628 /* Make this the JALX opcode. */
6629 x = ((dest >> 2) & 0x3ffffff) | jalx_opcode << 26;
6630 }
6631 else if (!mips_elf_hash_table (info)->ignore_branch_isa)
a6ebf616
MR
6632 {
6633 info->callbacks->einfo
2c1c9679 6634 (_("%X%H: unsupported branch between ISA modes\n"),
a6ebf616
MR
6635 input_bfd, input_section, relocation->r_offset);
6636 return TRUE;
6637 }
9d862524 6638 }
b49e97c9 6639
38a7df63
CF
6640 /* Try converting JAL to BAL and J(AL)R to B(AL), if the target is in
6641 range. */
0e1862bb 6642 if (!bfd_link_relocatable (info)
38a7df63 6643 && !cross_mode_jump_p
cd8d5a82
CF
6644 && ((JAL_TO_BAL_P (input_bfd)
6645 && r_type == R_MIPS_26
0e392101 6646 && (x >> 26) == 0x3) /* jal addr */
cd8d5a82
CF
6647 || (JALR_TO_BAL_P (input_bfd)
6648 && r_type == R_MIPS_JALR
0e392101 6649 && x == 0x0320f809) /* jalr t9 */
38a7df63
CF
6650 || (JR_TO_B_P (input_bfd)
6651 && r_type == R_MIPS_JALR
0e392101 6652 && (x & ~1) == 0x03200008))) /* jr t9 / jalr zero, t9 */
1367d393
ILT
6653 {
6654 bfd_vma addr;
6655 bfd_vma dest;
6656 bfd_signed_vma off;
6657
6658 addr = (input_section->output_section->vma
6659 + input_section->output_offset
6660 + relocation->r_offset
6661 + 4);
6662 if (r_type == R_MIPS_26)
6663 dest = (value << 2) | ((addr >> 28) << 28);
6664 else
6665 dest = value;
6666 off = dest - addr;
6667 if (off <= 0x1ffff && off >= -0x20000)
38a7df63 6668 {
0e392101 6669 if ((x & ~1) == 0x03200008) /* jr t9 / jalr zero, t9 */
38a7df63
CF
6670 x = 0x10000000 | (((bfd_vma) off >> 2) & 0xffff); /* b addr */
6671 else
6672 x = 0x04110000 | (((bfd_vma) off >> 2) & 0xffff); /* bal addr */
6673 }
1367d393
ILT
6674 }
6675
b49e97c9 6676 /* Put the value into the output. */
98e10ffa 6677 mips_elf_store_contents (howto, relocation, input_bfd, contents, x);
d6f16593 6678
0e1862bb 6679 _bfd_mips_elf_reloc_shuffle (input_bfd, r_type, !bfd_link_relocatable (info),
df58fc94 6680 location);
d6f16593 6681
b34976b6 6682 return TRUE;
b49e97c9 6683}
b49e97c9 6684\f
b49e97c9
TS
6685/* Create a rel.dyn relocation for the dynamic linker to resolve. REL
6686 is the original relocation, which is now being transformed into a
6687 dynamic relocation. The ADDENDP is adjusted if necessary; the
6688 caller should store the result in place of the original addend. */
6689
b34976b6 6690static bfd_boolean
9719ad41
RS
6691mips_elf_create_dynamic_relocation (bfd *output_bfd,
6692 struct bfd_link_info *info,
6693 const Elf_Internal_Rela *rel,
6694 struct mips_elf_link_hash_entry *h,
6695 asection *sec, bfd_vma symbol,
6696 bfd_vma *addendp, asection *input_section)
b49e97c9 6697{
947216bf 6698 Elf_Internal_Rela outrel[3];
b49e97c9
TS
6699 asection *sreloc;
6700 bfd *dynobj;
6701 int r_type;
5d41f0b6
RS
6702 long indx;
6703 bfd_boolean defined_p;
0a44bf69 6704 struct mips_elf_link_hash_table *htab;
b49e97c9 6705
0a44bf69 6706 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
6707 BFD_ASSERT (htab != NULL);
6708
b49e97c9
TS
6709 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
6710 dynobj = elf_hash_table (info)->dynobj;
0a44bf69 6711 sreloc = mips_elf_rel_dyn_section (info, FALSE);
b49e97c9
TS
6712 BFD_ASSERT (sreloc != NULL);
6713 BFD_ASSERT (sreloc->contents != NULL);
6714 BFD_ASSERT (sreloc->reloc_count * MIPS_ELF_REL_SIZE (output_bfd)
eea6121a 6715 < sreloc->size);
b49e97c9 6716
b49e97c9
TS
6717 outrel[0].r_offset =
6718 _bfd_elf_section_offset (output_bfd, info, input_section, rel[0].r_offset);
9ddf8309
TS
6719 if (ABI_64_P (output_bfd))
6720 {
6721 outrel[1].r_offset =
6722 _bfd_elf_section_offset (output_bfd, info, input_section, rel[1].r_offset);
6723 outrel[2].r_offset =
6724 _bfd_elf_section_offset (output_bfd, info, input_section, rel[2].r_offset);
6725 }
b49e97c9 6726
c5ae1840 6727 if (outrel[0].r_offset == MINUS_ONE)
0d591ff7 6728 /* The relocation field has been deleted. */
5d41f0b6
RS
6729 return TRUE;
6730
6731 if (outrel[0].r_offset == MINUS_TWO)
0d591ff7
RS
6732 {
6733 /* The relocation field has been converted into a relative value of
6734 some sort. Functions like _bfd_elf_write_section_eh_frame expect
6735 the field to be fully relocated, so add in the symbol's value. */
0d591ff7 6736 *addendp += symbol;
5d41f0b6 6737 return TRUE;
0d591ff7 6738 }
b49e97c9 6739
5d41f0b6
RS
6740 /* We must now calculate the dynamic symbol table index to use
6741 in the relocation. */
d4a77f3f 6742 if (h != NULL && ! SYMBOL_REFERENCES_LOCAL (info, &h->root))
5d41f0b6 6743 {
90c14f0c
L
6744 BFD_ASSERT (htab->root.target_os == is_vxworks
6745 || h->global_got_area != GGA_NONE);
5d41f0b6
RS
6746 indx = h->root.dynindx;
6747 if (SGI_COMPAT (output_bfd))
6748 defined_p = h->root.def_regular;
6749 else
6750 /* ??? glibc's ld.so just adds the final GOT entry to the
6751 relocation field. It therefore treats relocs against
6752 defined symbols in the same way as relocs against
6753 undefined symbols. */
6754 defined_p = FALSE;
6755 }
b49e97c9
TS
6756 else
6757 {
5d41f0b6
RS
6758 if (sec != NULL && bfd_is_abs_section (sec))
6759 indx = 0;
6760 else if (sec == NULL || sec->owner == NULL)
fdd07405 6761 {
5d41f0b6
RS
6762 bfd_set_error (bfd_error_bad_value);
6763 return FALSE;
b49e97c9
TS
6764 }
6765 else
6766 {
5d41f0b6 6767 indx = elf_section_data (sec->output_section)->dynindx;
74541ad4
AM
6768 if (indx == 0)
6769 {
6770 asection *osec = htab->root.text_index_section;
6771 indx = elf_section_data (osec)->dynindx;
6772 }
5d41f0b6
RS
6773 if (indx == 0)
6774 abort ();
b49e97c9
TS
6775 }
6776
5d41f0b6
RS
6777 /* Instead of generating a relocation using the section
6778 symbol, we may as well make it a fully relative
6779 relocation. We want to avoid generating relocations to
6780 local symbols because we used to generate them
6781 incorrectly, without adding the original symbol value,
6782 which is mandated by the ABI for section symbols. In
6783 order to give dynamic loaders and applications time to
6784 phase out the incorrect use, we refrain from emitting
6785 section-relative relocations. It's not like they're
6786 useful, after all. This should be a bit more efficient
6787 as well. */
6788 /* ??? Although this behavior is compatible with glibc's ld.so,
6789 the ABI says that relocations against STN_UNDEF should have
6790 a symbol value of 0. Irix rld honors this, so relocations
6791 against STN_UNDEF have no effect. */
6792 if (!SGI_COMPAT (output_bfd))
6793 indx = 0;
6794 defined_p = TRUE;
b49e97c9
TS
6795 }
6796
5d41f0b6
RS
6797 /* If the relocation was previously an absolute relocation and
6798 this symbol will not be referred to by the relocation, we must
6799 adjust it by the value we give it in the dynamic symbol table.
6800 Otherwise leave the job up to the dynamic linker. */
6801 if (defined_p && r_type != R_MIPS_REL32)
6802 *addendp += symbol;
6803
90c14f0c 6804 if (htab->root.target_os == is_vxworks)
0a44bf69
RS
6805 /* VxWorks uses non-relative relocations for this. */
6806 outrel[0].r_info = ELF32_R_INFO (indx, R_MIPS_32);
6807 else
6808 /* The relocation is always an REL32 relocation because we don't
6809 know where the shared library will wind up at load-time. */
6810 outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
6811 R_MIPS_REL32);
6812
5d41f0b6
RS
6813 /* For strict adherence to the ABI specification, we should
6814 generate a R_MIPS_64 relocation record by itself before the
6815 _REL32/_64 record as well, such that the addend is read in as
6816 a 64-bit value (REL32 is a 32-bit relocation, after all).
6817 However, since none of the existing ELF64 MIPS dynamic
6818 loaders seems to care, we don't waste space with these
6819 artificial relocations. If this turns out to not be true,
6820 mips_elf_allocate_dynamic_relocation() should be tweaked so
6821 as to make room for a pair of dynamic relocations per
6822 invocation if ABI_64_P, and here we should generate an
6823 additional relocation record with R_MIPS_64 by itself for a
6824 NULL symbol before this relocation record. */
6825 outrel[1].r_info = ELF_R_INFO (output_bfd, 0,
6826 ABI_64_P (output_bfd)
6827 ? R_MIPS_64
6828 : R_MIPS_NONE);
6829 outrel[2].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_NONE);
6830
6831 /* Adjust the output offset of the relocation to reference the
6832 correct location in the output file. */
6833 outrel[0].r_offset += (input_section->output_section->vma
6834 + input_section->output_offset);
6835 outrel[1].r_offset += (input_section->output_section->vma
6836 + input_section->output_offset);
6837 outrel[2].r_offset += (input_section->output_section->vma
6838 + input_section->output_offset);
6839
b49e97c9
TS
6840 /* Put the relocation back out. We have to use the special
6841 relocation outputter in the 64-bit case since the 64-bit
6842 relocation format is non-standard. */
6843 if (ABI_64_P (output_bfd))
6844 {
6845 (*get_elf_backend_data (output_bfd)->s->swap_reloc_out)
6846 (output_bfd, &outrel[0],
6847 (sreloc->contents
6848 + sreloc->reloc_count * sizeof (Elf64_Mips_External_Rel)));
6849 }
90c14f0c 6850 else if (htab->root.target_os == is_vxworks)
0a44bf69
RS
6851 {
6852 /* VxWorks uses RELA rather than REL dynamic relocations. */
6853 outrel[0].r_addend = *addendp;
6854 bfd_elf32_swap_reloca_out
6855 (output_bfd, &outrel[0],
6856 (sreloc->contents
6857 + sreloc->reloc_count * sizeof (Elf32_External_Rela)));
6858 }
b49e97c9 6859 else
947216bf
AM
6860 bfd_elf32_swap_reloc_out
6861 (output_bfd, &outrel[0],
6862 (sreloc->contents + sreloc->reloc_count * sizeof (Elf32_External_Rel)));
b49e97c9 6863
b49e97c9
TS
6864 /* We've now added another relocation. */
6865 ++sreloc->reloc_count;
6866
6867 /* Make sure the output section is writable. The dynamic linker
6868 will be writing to it. */
6869 elf_section_data (input_section->output_section)->this_hdr.sh_flags
6870 |= SHF_WRITE;
6871
6872 /* On IRIX5, make an entry of compact relocation info. */
5d41f0b6 6873 if (IRIX_COMPAT (output_bfd) == ict_irix5)
b49e97c9 6874 {
3d4d4302 6875 asection *scpt = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
6876 bfd_byte *cr;
6877
6878 if (scpt)
6879 {
6880 Elf32_crinfo cptrel;
6881
6882 mips_elf_set_cr_format (cptrel, CRF_MIPS_LONG);
6883 cptrel.vaddr = (rel->r_offset
6884 + input_section->output_section->vma
6885 + input_section->output_offset);
6886 if (r_type == R_MIPS_REL32)
6887 mips_elf_set_cr_type (cptrel, CRT_MIPS_REL32);
6888 else
6889 mips_elf_set_cr_type (cptrel, CRT_MIPS_WORD);
6890 mips_elf_set_cr_dist2to (cptrel, 0);
6891 cptrel.konst = *addendp;
6892
6893 cr = (scpt->contents
6894 + sizeof (Elf32_External_compact_rel));
abc0f8d0 6895 mips_elf_set_cr_relvaddr (cptrel, 0);
b49e97c9
TS
6896 bfd_elf32_swap_crinfo_out (output_bfd, &cptrel,
6897 ((Elf32_External_crinfo *) cr
6898 + scpt->reloc_count));
6899 ++scpt->reloc_count;
6900 }
6901 }
6902
943284cc
DJ
6903 /* If we've written this relocation for a readonly section,
6904 we need to set DF_TEXTREL again, so that we do not delete the
6905 DT_TEXTREL tag. */
6906 if (MIPS_ELF_READONLY_SECTION (input_section))
6907 info->flags |= DF_TEXTREL;
6908
b34976b6 6909 return TRUE;
b49e97c9
TS
6910}
6911\f
b49e97c9
TS
6912/* Return the MACH for a MIPS e_flags value. */
6913
6914unsigned long
9719ad41 6915_bfd_elf_mips_mach (flagword flags)
b49e97c9
TS
6916{
6917 switch (flags & EF_MIPS_MACH)
6918 {
6919 case E_MIPS_MACH_3900:
6920 return bfd_mach_mips3900;
6921
6922 case E_MIPS_MACH_4010:
6923 return bfd_mach_mips4010;
6924
6925 case E_MIPS_MACH_4100:
6926 return bfd_mach_mips4100;
6927
6928 case E_MIPS_MACH_4111:
6929 return bfd_mach_mips4111;
6930
00707a0e
RS
6931 case E_MIPS_MACH_4120:
6932 return bfd_mach_mips4120;
6933
b49e97c9
TS
6934 case E_MIPS_MACH_4650:
6935 return bfd_mach_mips4650;
6936
00707a0e
RS
6937 case E_MIPS_MACH_5400:
6938 return bfd_mach_mips5400;
6939
6940 case E_MIPS_MACH_5500:
6941 return bfd_mach_mips5500;
6942
e407c74b
NC
6943 case E_MIPS_MACH_5900:
6944 return bfd_mach_mips5900;
6945
0d2e43ed
ILT
6946 case E_MIPS_MACH_9000:
6947 return bfd_mach_mips9000;
6948
b49e97c9
TS
6949 case E_MIPS_MACH_SB1:
6950 return bfd_mach_mips_sb1;
6951
350cc38d
MS
6952 case E_MIPS_MACH_LS2E:
6953 return bfd_mach_mips_loongson_2e;
6954
6955 case E_MIPS_MACH_LS2F:
6956 return bfd_mach_mips_loongson_2f;
6957
ac8cb70f
CX
6958 case E_MIPS_MACH_GS464:
6959 return bfd_mach_mips_gs464;
fd503541 6960
bd782c07
CX
6961 case E_MIPS_MACH_GS464E:
6962 return bfd_mach_mips_gs464e;
6963
9108bc33
CX
6964 case E_MIPS_MACH_GS264E:
6965 return bfd_mach_mips_gs264e;
6966
2c629856
N
6967 case E_MIPS_MACH_OCTEON3:
6968 return bfd_mach_mips_octeon3;
6969
432233b3
AP
6970 case E_MIPS_MACH_OCTEON2:
6971 return bfd_mach_mips_octeon2;
6972
6f179bd0
AN
6973 case E_MIPS_MACH_OCTEON:
6974 return bfd_mach_mips_octeon;
6975
52b6b6b9
JM
6976 case E_MIPS_MACH_XLR:
6977 return bfd_mach_mips_xlr;
6978
38bf472a
MR
6979 case E_MIPS_MACH_IAMR2:
6980 return bfd_mach_mips_interaptiv_mr2;
6981
b49e97c9
TS
6982 default:
6983 switch (flags & EF_MIPS_ARCH)
6984 {
6985 default:
6986 case E_MIPS_ARCH_1:
6987 return bfd_mach_mips3000;
b49e97c9
TS
6988
6989 case E_MIPS_ARCH_2:
6990 return bfd_mach_mips6000;
b49e97c9
TS
6991
6992 case E_MIPS_ARCH_3:
6993 return bfd_mach_mips4000;
b49e97c9
TS
6994
6995 case E_MIPS_ARCH_4:
6996 return bfd_mach_mips8000;
b49e97c9
TS
6997
6998 case E_MIPS_ARCH_5:
6999 return bfd_mach_mips5;
b49e97c9
TS
7000
7001 case E_MIPS_ARCH_32:
7002 return bfd_mach_mipsisa32;
b49e97c9
TS
7003
7004 case E_MIPS_ARCH_64:
7005 return bfd_mach_mipsisa64;
af7ee8bf
CD
7006
7007 case E_MIPS_ARCH_32R2:
7008 return bfd_mach_mipsisa32r2;
5f74bc13
CD
7009
7010 case E_MIPS_ARCH_64R2:
7011 return bfd_mach_mipsisa64r2;
7361da2c
AB
7012
7013 case E_MIPS_ARCH_32R6:
7014 return bfd_mach_mipsisa32r6;
7015
7016 case E_MIPS_ARCH_64R6:
7017 return bfd_mach_mipsisa64r6;
b49e97c9
TS
7018 }
7019 }
7020
7021 return 0;
7022}
7023
7024/* Return printable name for ABI. */
7025
7026static INLINE char *
9719ad41 7027elf_mips_abi_name (bfd *abfd)
b49e97c9
TS
7028{
7029 flagword flags;
7030
7031 flags = elf_elfheader (abfd)->e_flags;
7032 switch (flags & EF_MIPS_ABI)
7033 {
7034 case 0:
7035 if (ABI_N32_P (abfd))
7036 return "N32";
7037 else if (ABI_64_P (abfd))
7038 return "64";
7039 else
7040 return "none";
7041 case E_MIPS_ABI_O32:
7042 return "O32";
7043 case E_MIPS_ABI_O64:
7044 return "O64";
7045 case E_MIPS_ABI_EABI32:
7046 return "EABI32";
7047 case E_MIPS_ABI_EABI64:
7048 return "EABI64";
7049 default:
7050 return "unknown abi";
7051 }
7052}
7053\f
7054/* MIPS ELF uses two common sections. One is the usual one, and the
7055 other is for small objects. All the small objects are kept
7056 together, and then referenced via the gp pointer, which yields
7057 faster assembler code. This is what we use for the small common
7058 section. This approach is copied from ecoff.c. */
7059static asection mips_elf_scom_section;
7060static asymbol mips_elf_scom_symbol;
7061static asymbol *mips_elf_scom_symbol_ptr;
7062
7063/* MIPS ELF also uses an acommon section, which represents an
7064 allocated common symbol which may be overridden by a
7065 definition in a shared library. */
7066static asection mips_elf_acom_section;
7067static asymbol mips_elf_acom_symbol;
7068static asymbol *mips_elf_acom_symbol_ptr;
7069
738e5348 7070/* This is used for both the 32-bit and the 64-bit ABI. */
b49e97c9
TS
7071
7072void
9719ad41 7073_bfd_mips_elf_symbol_processing (bfd *abfd, asymbol *asym)
b49e97c9
TS
7074{
7075 elf_symbol_type *elfsym;
7076
738e5348 7077 /* Handle the special MIPS section numbers that a symbol may use. */
b49e97c9
TS
7078 elfsym = (elf_symbol_type *) asym;
7079 switch (elfsym->internal_elf_sym.st_shndx)
7080 {
7081 case SHN_MIPS_ACOMMON:
7082 /* This section is used in a dynamically linked executable file.
7083 It is an allocated common section. The dynamic linker can
7084 either resolve these symbols to something in a shared
7085 library, or it can just leave them here. For our purposes,
7086 we can consider these symbols to be in a new section. */
7087 if (mips_elf_acom_section.name == NULL)
7088 {
7089 /* Initialize the acommon section. */
7090 mips_elf_acom_section.name = ".acommon";
7091 mips_elf_acom_section.flags = SEC_ALLOC;
7092 mips_elf_acom_section.output_section = &mips_elf_acom_section;
7093 mips_elf_acom_section.symbol = &mips_elf_acom_symbol;
7094 mips_elf_acom_section.symbol_ptr_ptr = &mips_elf_acom_symbol_ptr;
7095 mips_elf_acom_symbol.name = ".acommon";
7096 mips_elf_acom_symbol.flags = BSF_SECTION_SYM;
7097 mips_elf_acom_symbol.section = &mips_elf_acom_section;
7098 mips_elf_acom_symbol_ptr = &mips_elf_acom_symbol;
7099 }
7100 asym->section = &mips_elf_acom_section;
7101 break;
7102
7103 case SHN_COMMON:
7104 /* Common symbols less than the GP size are automatically
7105 treated as SHN_MIPS_SCOMMON symbols on IRIX5. */
7106 if (asym->value > elf_gp_size (abfd)
b59eed79 7107 || ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_TLS
b49e97c9
TS
7108 || IRIX_COMPAT (abfd) == ict_irix6)
7109 break;
7110 /* Fall through. */
7111 case SHN_MIPS_SCOMMON:
7112 if (mips_elf_scom_section.name == NULL)
7113 {
7114 /* Initialize the small common section. */
7115 mips_elf_scom_section.name = ".scommon";
7116 mips_elf_scom_section.flags = SEC_IS_COMMON;
7117 mips_elf_scom_section.output_section = &mips_elf_scom_section;
7118 mips_elf_scom_section.symbol = &mips_elf_scom_symbol;
7119 mips_elf_scom_section.symbol_ptr_ptr = &mips_elf_scom_symbol_ptr;
7120 mips_elf_scom_symbol.name = ".scommon";
7121 mips_elf_scom_symbol.flags = BSF_SECTION_SYM;
7122 mips_elf_scom_symbol.section = &mips_elf_scom_section;
7123 mips_elf_scom_symbol_ptr = &mips_elf_scom_symbol;
7124 }
7125 asym->section = &mips_elf_scom_section;
7126 asym->value = elfsym->internal_elf_sym.st_size;
7127 break;
7128
7129 case SHN_MIPS_SUNDEFINED:
7130 asym->section = bfd_und_section_ptr;
7131 break;
7132
b49e97c9 7133 case SHN_MIPS_TEXT:
00b4930b
TS
7134 {
7135 asection *section = bfd_get_section_by_name (abfd, ".text");
7136
00b4930b
TS
7137 if (section != NULL)
7138 {
7139 asym->section = section;
7140 /* MIPS_TEXT is a bit special, the address is not an offset
de194d85 7141 to the base of the .text section. So subtract the section
00b4930b
TS
7142 base address to make it an offset. */
7143 asym->value -= section->vma;
7144 }
7145 }
b49e97c9
TS
7146 break;
7147
7148 case SHN_MIPS_DATA:
00b4930b
TS
7149 {
7150 asection *section = bfd_get_section_by_name (abfd, ".data");
7151
00b4930b
TS
7152 if (section != NULL)
7153 {
7154 asym->section = section;
7155 /* MIPS_DATA is a bit special, the address is not an offset
de194d85 7156 to the base of the .data section. So subtract the section
00b4930b
TS
7157 base address to make it an offset. */
7158 asym->value -= section->vma;
7159 }
7160 }
b49e97c9 7161 break;
b49e97c9 7162 }
738e5348 7163
df58fc94
RS
7164 /* If this is an odd-valued function symbol, assume it's a MIPS16
7165 or microMIPS one. */
738e5348
RS
7166 if (ELF_ST_TYPE (elfsym->internal_elf_sym.st_info) == STT_FUNC
7167 && (asym->value & 1) != 0)
7168 {
7169 asym->value--;
e8faf7d1 7170 if (MICROMIPS_P (abfd))
df58fc94
RS
7171 elfsym->internal_elf_sym.st_other
7172 = ELF_ST_SET_MICROMIPS (elfsym->internal_elf_sym.st_other);
7173 else
7174 elfsym->internal_elf_sym.st_other
7175 = ELF_ST_SET_MIPS16 (elfsym->internal_elf_sym.st_other);
738e5348 7176 }
b49e97c9
TS
7177}
7178\f
8c946ed5
RS
7179/* Implement elf_backend_eh_frame_address_size. This differs from
7180 the default in the way it handles EABI64.
7181
7182 EABI64 was originally specified as an LP64 ABI, and that is what
7183 -mabi=eabi normally gives on a 64-bit target. However, gcc has
7184 historically accepted the combination of -mabi=eabi and -mlong32,
7185 and this ILP32 variation has become semi-official over time.
7186 Both forms use elf32 and have pointer-sized FDE addresses.
7187
7188 If an EABI object was generated by GCC 4.0 or above, it will have
7189 an empty .gcc_compiled_longXX section, where XX is the size of longs
7190 in bits. Unfortunately, ILP32 objects generated by earlier compilers
7191 have no special marking to distinguish them from LP64 objects.
7192
7193 We don't want users of the official LP64 ABI to be punished for the
7194 existence of the ILP32 variant, but at the same time, we don't want
7195 to mistakenly interpret pre-4.0 ILP32 objects as being LP64 objects.
7196 We therefore take the following approach:
7197
7198 - If ABFD contains a .gcc_compiled_longXX section, use it to
07d6d2b8 7199 determine the pointer size.
8c946ed5
RS
7200
7201 - Otherwise check the type of the first relocation. Assume that
07d6d2b8 7202 the LP64 ABI is being used if the relocation is of type R_MIPS_64.
8c946ed5
RS
7203
7204 - Otherwise punt.
7205
7206 The second check is enough to detect LP64 objects generated by pre-4.0
7207 compilers because, in the kind of output generated by those compilers,
7208 the first relocation will be associated with either a CIE personality
7209 routine or an FDE start address. Furthermore, the compilers never
7210 used a special (non-pointer) encoding for this ABI.
7211
7212 Checking the relocation type should also be safe because there is no
7213 reason to use R_MIPS_64 in an ILP32 object. Pre-4.0 compilers never
7214 did so. */
7215
7216unsigned int
76c20d54 7217_bfd_mips_elf_eh_frame_address_size (bfd *abfd, const asection *sec)
8c946ed5
RS
7218{
7219 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
7220 return 8;
7221 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
7222 {
7223 bfd_boolean long32_p, long64_p;
7224
7225 long32_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long32") != 0;
7226 long64_p = bfd_get_section_by_name (abfd, ".gcc_compiled_long64") != 0;
7227 if (long32_p && long64_p)
7228 return 0;
7229 if (long32_p)
7230 return 4;
7231 if (long64_p)
7232 return 8;
7233
7234 if (sec->reloc_count > 0
7235 && elf_section_data (sec)->relocs != NULL
7236 && (ELF32_R_TYPE (elf_section_data (sec)->relocs[0].r_info)
7237 == R_MIPS_64))
7238 return 8;
7239
7240 return 0;
7241 }
7242 return 4;
7243}
7244\f
174fd7f9
RS
7245/* There appears to be a bug in the MIPSpro linker that causes GOT_DISP
7246 relocations against two unnamed section symbols to resolve to the
7247 same address. For example, if we have code like:
7248
7249 lw $4,%got_disp(.data)($gp)
7250 lw $25,%got_disp(.text)($gp)
7251 jalr $25
7252
7253 then the linker will resolve both relocations to .data and the program
7254 will jump there rather than to .text.
7255
7256 We can work around this problem by giving names to local section symbols.
7257 This is also what the MIPSpro tools do. */
7258
7259bfd_boolean
7260_bfd_mips_elf_name_local_section_symbols (bfd *abfd)
7261{
c77cb2a0 7262 return elf_elfheader (abfd)->e_type == ET_REL && SGI_COMPAT (abfd);
174fd7f9
RS
7263}
7264\f
b49e97c9
TS
7265/* Work over a section just before writing it out. This routine is
7266 used by both the 32-bit and the 64-bit ABI. FIXME: We recognize
7267 sections that need the SHF_MIPS_GPREL flag by name; there has to be
7268 a better way. */
7269
b34976b6 7270bfd_boolean
9719ad41 7271_bfd_mips_elf_section_processing (bfd *abfd, Elf_Internal_Shdr *hdr)
b49e97c9
TS
7272{
7273 if (hdr->sh_type == SHT_MIPS_REGINFO
7274 && hdr->sh_size > 0)
7275 {
7276 bfd_byte buf[4];
7277
b49e97c9
TS
7278 BFD_ASSERT (hdr->contents == NULL);
7279
2d6dda71
MR
7280 if (hdr->sh_size != sizeof (Elf32_External_RegInfo))
7281 {
7282 _bfd_error_handler
2c1c9679 7283 (_("%pB: incorrect `.reginfo' section size; "
2dcf00ce
AM
7284 "expected %" PRIu64 ", got %" PRIu64),
7285 abfd, (uint64_t) sizeof (Elf32_External_RegInfo),
7286 (uint64_t) hdr->sh_size);
2d6dda71
MR
7287 bfd_set_error (bfd_error_bad_value);
7288 return FALSE;
7289 }
7290
b49e97c9
TS
7291 if (bfd_seek (abfd,
7292 hdr->sh_offset + sizeof (Elf32_External_RegInfo) - 4,
7293 SEEK_SET) != 0)
b34976b6 7294 return FALSE;
b49e97c9 7295 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 7296 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 7297 return FALSE;
b49e97c9
TS
7298 }
7299
7300 if (hdr->sh_type == SHT_MIPS_OPTIONS
7301 && hdr->bfd_section != NULL
f0abc2a1
AM
7302 && mips_elf_section_data (hdr->bfd_section) != NULL
7303 && mips_elf_section_data (hdr->bfd_section)->u.tdata != NULL)
b49e97c9
TS
7304 {
7305 bfd_byte *contents, *l, *lend;
7306
f0abc2a1
AM
7307 /* We stored the section contents in the tdata field in the
7308 set_section_contents routine. We save the section contents
7309 so that we don't have to read them again.
b49e97c9
TS
7310 At this point we know that elf_gp is set, so we can look
7311 through the section contents to see if there is an
7312 ODK_REGINFO structure. */
7313
f0abc2a1 7314 contents = mips_elf_section_data (hdr->bfd_section)->u.tdata;
b49e97c9
TS
7315 l = contents;
7316 lend = contents + hdr->sh_size;
7317 while (l + sizeof (Elf_External_Options) <= lend)
7318 {
7319 Elf_Internal_Options intopt;
7320
7321 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7322 &intopt);
1bc8074d
MR
7323 if (intopt.size < sizeof (Elf_External_Options))
7324 {
4eca0228 7325 _bfd_error_handler
695344c0 7326 /* xgettext:c-format */
2c1c9679 7327 (_("%pB: warning: bad `%s' option size %u smaller than"
63a5468a 7328 " its header"),
1bc8074d
MR
7329 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7330 break;
7331 }
b49e97c9
TS
7332 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7333 {
7334 bfd_byte buf[8];
7335
7336 if (bfd_seek (abfd,
7337 (hdr->sh_offset
7338 + (l - contents)
7339 + sizeof (Elf_External_Options)
7340 + (sizeof (Elf64_External_RegInfo) - 8)),
7341 SEEK_SET) != 0)
b34976b6 7342 return FALSE;
b49e97c9 7343 H_PUT_64 (abfd, elf_gp (abfd), buf);
9719ad41 7344 if (bfd_bwrite (buf, 8, abfd) != 8)
b34976b6 7345 return FALSE;
b49e97c9
TS
7346 }
7347 else if (intopt.kind == ODK_REGINFO)
7348 {
7349 bfd_byte buf[4];
7350
7351 if (bfd_seek (abfd,
7352 (hdr->sh_offset
7353 + (l - contents)
7354 + sizeof (Elf_External_Options)
7355 + (sizeof (Elf32_External_RegInfo) - 4)),
7356 SEEK_SET) != 0)
b34976b6 7357 return FALSE;
b49e97c9 7358 H_PUT_32 (abfd, elf_gp (abfd), buf);
9719ad41 7359 if (bfd_bwrite (buf, 4, abfd) != 4)
b34976b6 7360 return FALSE;
b49e97c9
TS
7361 }
7362 l += intopt.size;
7363 }
7364 }
7365
7366 if (hdr->bfd_section != NULL)
7367 {
fd361982 7368 const char *name = bfd_section_name (hdr->bfd_section);
b49e97c9 7369
2d0f9ad9
JM
7370 /* .sbss is not handled specially here because the GNU/Linux
7371 prelinker can convert .sbss from NOBITS to PROGBITS and
7372 changing it back to NOBITS breaks the binary. The entry in
7373 _bfd_mips_elf_special_sections will ensure the correct flags
7374 are set on .sbss if BFD creates it without reading it from an
7375 input file, and without special handling here the flags set
7376 on it in an input file will be followed. */
b49e97c9
TS
7377 if (strcmp (name, ".sdata") == 0
7378 || strcmp (name, ".lit8") == 0
7379 || strcmp (name, ".lit4") == 0)
fd6f9d17 7380 hdr->sh_flags |= SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL;
b49e97c9 7381 else if (strcmp (name, ".srdata") == 0)
fd6f9d17 7382 hdr->sh_flags |= SHF_ALLOC | SHF_MIPS_GPREL;
b49e97c9 7383 else if (strcmp (name, ".compact_rel") == 0)
fd6f9d17 7384 hdr->sh_flags = 0;
b49e97c9
TS
7385 else if (strcmp (name, ".rtproc") == 0)
7386 {
7387 if (hdr->sh_addralign != 0 && hdr->sh_entsize == 0)
7388 {
7389 unsigned int adjust;
7390
7391 adjust = hdr->sh_size % hdr->sh_addralign;
7392 if (adjust != 0)
7393 hdr->sh_size += hdr->sh_addralign - adjust;
7394 }
7395 }
7396 }
7397
b34976b6 7398 return TRUE;
b49e97c9
TS
7399}
7400
7401/* Handle a MIPS specific section when reading an object file. This
7402 is called when elfcode.h finds a section with an unknown type.
bf577467 7403 This routine supports both the 32-bit and 64-bit ELF ABI. */
b49e97c9 7404
b34976b6 7405bfd_boolean
6dc132d9
L
7406_bfd_mips_elf_section_from_shdr (bfd *abfd,
7407 Elf_Internal_Shdr *hdr,
7408 const char *name,
7409 int shindex)
b49e97c9
TS
7410{
7411 flagword flags = 0;
7412
7413 /* There ought to be a place to keep ELF backend specific flags, but
7414 at the moment there isn't one. We just keep track of the
7415 sections by their name, instead. Fortunately, the ABI gives
7416 suggested names for all the MIPS specific sections, so we will
7417 probably get away with this. */
7418 switch (hdr->sh_type)
7419 {
7420 case SHT_MIPS_LIBLIST:
7421 if (strcmp (name, ".liblist") != 0)
b34976b6 7422 return FALSE;
b49e97c9
TS
7423 break;
7424 case SHT_MIPS_MSYM:
7425 if (strcmp (name, ".msym") != 0)
b34976b6 7426 return FALSE;
b49e97c9
TS
7427 break;
7428 case SHT_MIPS_CONFLICT:
7429 if (strcmp (name, ".conflict") != 0)
b34976b6 7430 return FALSE;
b49e97c9
TS
7431 break;
7432 case SHT_MIPS_GPTAB:
0112cd26 7433 if (! CONST_STRNEQ (name, ".gptab."))
b34976b6 7434 return FALSE;
b49e97c9
TS
7435 break;
7436 case SHT_MIPS_UCODE:
7437 if (strcmp (name, ".ucode") != 0)
b34976b6 7438 return FALSE;
b49e97c9
TS
7439 break;
7440 case SHT_MIPS_DEBUG:
7441 if (strcmp (name, ".mdebug") != 0)
b34976b6 7442 return FALSE;
b49e97c9
TS
7443 flags = SEC_DEBUGGING;
7444 break;
7445 case SHT_MIPS_REGINFO:
7446 if (strcmp (name, ".reginfo") != 0
7447 || hdr->sh_size != sizeof (Elf32_External_RegInfo))
b34976b6 7448 return FALSE;
b49e97c9
TS
7449 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7450 break;
7451 case SHT_MIPS_IFACE:
7452 if (strcmp (name, ".MIPS.interfaces") != 0)
b34976b6 7453 return FALSE;
b49e97c9
TS
7454 break;
7455 case SHT_MIPS_CONTENT:
0112cd26 7456 if (! CONST_STRNEQ (name, ".MIPS.content"))
b34976b6 7457 return FALSE;
b49e97c9
TS
7458 break;
7459 case SHT_MIPS_OPTIONS:
cc2e31b9 7460 if (!MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b34976b6 7461 return FALSE;
b49e97c9 7462 break;
351cdf24
MF
7463 case SHT_MIPS_ABIFLAGS:
7464 if (!MIPS_ELF_ABIFLAGS_SECTION_NAME_P (name))
7465 return FALSE;
7466 flags = (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_SAME_SIZE);
7467 break;
b49e97c9 7468 case SHT_MIPS_DWARF:
1b315056 7469 if (! CONST_STRNEQ (name, ".debug_")
07d6d2b8 7470 && ! CONST_STRNEQ (name, ".zdebug_"))
b34976b6 7471 return FALSE;
b49e97c9
TS
7472 break;
7473 case SHT_MIPS_SYMBOL_LIB:
7474 if (strcmp (name, ".MIPS.symlib") != 0)
b34976b6 7475 return FALSE;
b49e97c9
TS
7476 break;
7477 case SHT_MIPS_EVENTS:
0112cd26
NC
7478 if (! CONST_STRNEQ (name, ".MIPS.events")
7479 && ! CONST_STRNEQ (name, ".MIPS.post_rel"))
b34976b6 7480 return FALSE;
b49e97c9 7481 break;
f16a9783
MS
7482 case SHT_MIPS_XHASH:
7483 if (strcmp (name, ".MIPS.xhash") != 0)
7484 return FALSE;
b49e97c9 7485 default:
cc2e31b9 7486 break;
b49e97c9
TS
7487 }
7488
6dc132d9 7489 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
b34976b6 7490 return FALSE;
b49e97c9 7491
bf577467
AM
7492 if (hdr->sh_flags & SHF_MIPS_GPREL)
7493 flags |= SEC_SMALL_DATA;
7494
b49e97c9
TS
7495 if (flags)
7496 {
fd361982
AM
7497 if (!bfd_set_section_flags (hdr->bfd_section,
7498 (bfd_section_flags (hdr->bfd_section)
7499 | flags)))
b34976b6 7500 return FALSE;
b49e97c9
TS
7501 }
7502
351cdf24
MF
7503 if (hdr->sh_type == SHT_MIPS_ABIFLAGS)
7504 {
7505 Elf_External_ABIFlags_v0 ext;
7506
7507 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7508 &ext, 0, sizeof ext))
7509 return FALSE;
7510 bfd_mips_elf_swap_abiflags_v0_in (abfd, &ext,
7511 &mips_elf_tdata (abfd)->abiflags);
7512 if (mips_elf_tdata (abfd)->abiflags.version != 0)
7513 return FALSE;
7514 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
7515 }
7516
b49e97c9
TS
7517 /* FIXME: We should record sh_info for a .gptab section. */
7518
7519 /* For a .reginfo section, set the gp value in the tdata information
7520 from the contents of this section. We need the gp value while
7521 processing relocs, so we just get it now. The .reginfo section
7522 is not used in the 64-bit MIPS ELF ABI. */
7523 if (hdr->sh_type == SHT_MIPS_REGINFO)
7524 {
7525 Elf32_External_RegInfo ext;
7526 Elf32_RegInfo s;
7527
9719ad41
RS
7528 if (! bfd_get_section_contents (abfd, hdr->bfd_section,
7529 &ext, 0, sizeof ext))
b34976b6 7530 return FALSE;
b49e97c9
TS
7531 bfd_mips_elf32_swap_reginfo_in (abfd, &ext, &s);
7532 elf_gp (abfd) = s.ri_gp_value;
7533 }
7534
7535 /* For a SHT_MIPS_OPTIONS section, look for a ODK_REGINFO entry, and
7536 set the gp value based on what we find. We may see both
7537 SHT_MIPS_REGINFO and SHT_MIPS_OPTIONS/ODK_REGINFO; in that case,
7538 they should agree. */
7539 if (hdr->sh_type == SHT_MIPS_OPTIONS)
7540 {
7541 bfd_byte *contents, *l, *lend;
7542
9719ad41 7543 contents = bfd_malloc (hdr->sh_size);
b49e97c9 7544 if (contents == NULL)
b34976b6 7545 return FALSE;
b49e97c9 7546 if (! bfd_get_section_contents (abfd, hdr->bfd_section, contents,
9719ad41 7547 0, hdr->sh_size))
b49e97c9
TS
7548 {
7549 free (contents);
b34976b6 7550 return FALSE;
b49e97c9
TS
7551 }
7552 l = contents;
7553 lend = contents + hdr->sh_size;
7554 while (l + sizeof (Elf_External_Options) <= lend)
7555 {
7556 Elf_Internal_Options intopt;
7557
7558 bfd_mips_elf_swap_options_in (abfd, (Elf_External_Options *) l,
7559 &intopt);
1bc8074d
MR
7560 if (intopt.size < sizeof (Elf_External_Options))
7561 {
4eca0228 7562 _bfd_error_handler
695344c0 7563 /* xgettext:c-format */
2c1c9679 7564 (_("%pB: warning: bad `%s' option size %u smaller than"
63a5468a 7565 " its header"),
1bc8074d
MR
7566 abfd, MIPS_ELF_OPTIONS_SECTION_NAME (abfd), intopt.size);
7567 break;
7568 }
b49e97c9
TS
7569 if (ABI_64_P (abfd) && intopt.kind == ODK_REGINFO)
7570 {
7571 Elf64_Internal_RegInfo intreg;
7572
7573 bfd_mips_elf64_swap_reginfo_in
7574 (abfd,
7575 ((Elf64_External_RegInfo *)
7576 (l + sizeof (Elf_External_Options))),
7577 &intreg);
7578 elf_gp (abfd) = intreg.ri_gp_value;
7579 }
7580 else if (intopt.kind == ODK_REGINFO)
7581 {
7582 Elf32_RegInfo intreg;
7583
7584 bfd_mips_elf32_swap_reginfo_in
7585 (abfd,
7586 ((Elf32_External_RegInfo *)
7587 (l + sizeof (Elf_External_Options))),
7588 &intreg);
7589 elf_gp (abfd) = intreg.ri_gp_value;
7590 }
7591 l += intopt.size;
7592 }
7593 free (contents);
7594 }
7595
b34976b6 7596 return TRUE;
b49e97c9
TS
7597}
7598
7599/* Set the correct type for a MIPS ELF section. We do this by the
7600 section name, which is a hack, but ought to work. This routine is
7601 used by both the 32-bit and the 64-bit ABI. */
7602
b34976b6 7603bfd_boolean
9719ad41 7604_bfd_mips_elf_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
b49e97c9 7605{
fd361982 7606 const char *name = bfd_section_name (sec);
b49e97c9
TS
7607
7608 if (strcmp (name, ".liblist") == 0)
7609 {
7610 hdr->sh_type = SHT_MIPS_LIBLIST;
eea6121a 7611 hdr->sh_info = sec->size / sizeof (Elf32_Lib);
b49e97c9
TS
7612 /* The sh_link field is set in final_write_processing. */
7613 }
7614 else if (strcmp (name, ".conflict") == 0)
7615 hdr->sh_type = SHT_MIPS_CONFLICT;
0112cd26 7616 else if (CONST_STRNEQ (name, ".gptab."))
b49e97c9
TS
7617 {
7618 hdr->sh_type = SHT_MIPS_GPTAB;
7619 hdr->sh_entsize = sizeof (Elf32_External_gptab);
7620 /* The sh_info field is set in final_write_processing. */
7621 }
7622 else if (strcmp (name, ".ucode") == 0)
7623 hdr->sh_type = SHT_MIPS_UCODE;
7624 else if (strcmp (name, ".mdebug") == 0)
7625 {
7626 hdr->sh_type = SHT_MIPS_DEBUG;
8dc1a139 7627 /* In a shared object on IRIX 5.3, the .mdebug section has an
07d6d2b8 7628 entsize of 0. FIXME: Does this matter? */
b49e97c9
TS
7629 if (SGI_COMPAT (abfd) && (abfd->flags & DYNAMIC) != 0)
7630 hdr->sh_entsize = 0;
7631 else
7632 hdr->sh_entsize = 1;
7633 }
7634 else if (strcmp (name, ".reginfo") == 0)
7635 {
7636 hdr->sh_type = SHT_MIPS_REGINFO;
8dc1a139 7637 /* In a shared object on IRIX 5.3, the .reginfo section has an
07d6d2b8 7638 entsize of 0x18. FIXME: Does this matter? */
b49e97c9
TS
7639 if (SGI_COMPAT (abfd))
7640 {
7641 if ((abfd->flags & DYNAMIC) != 0)
7642 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7643 else
7644 hdr->sh_entsize = 1;
7645 }
7646 else
7647 hdr->sh_entsize = sizeof (Elf32_External_RegInfo);
7648 }
7649 else if (SGI_COMPAT (abfd)
7650 && (strcmp (name, ".hash") == 0
7651 || strcmp (name, ".dynamic") == 0
7652 || strcmp (name, ".dynstr") == 0))
7653 {
7654 if (SGI_COMPAT (abfd))
7655 hdr->sh_entsize = 0;
7656#if 0
8dc1a139 7657 /* This isn't how the IRIX6 linker behaves. */
b49e97c9
TS
7658 hdr->sh_info = SIZEOF_MIPS_DYNSYM_SECNAMES;
7659#endif
7660 }
7661 else if (strcmp (name, ".got") == 0
7662 || strcmp (name, ".srdata") == 0
7663 || strcmp (name, ".sdata") == 0
7664 || strcmp (name, ".sbss") == 0
7665 || strcmp (name, ".lit4") == 0
7666 || strcmp (name, ".lit8") == 0)
7667 hdr->sh_flags |= SHF_MIPS_GPREL;
7668 else if (strcmp (name, ".MIPS.interfaces") == 0)
7669 {
7670 hdr->sh_type = SHT_MIPS_IFACE;
7671 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7672 }
0112cd26 7673 else if (CONST_STRNEQ (name, ".MIPS.content"))
b49e97c9
TS
7674 {
7675 hdr->sh_type = SHT_MIPS_CONTENT;
7676 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7677 /* The sh_info field is set in final_write_processing. */
7678 }
cc2e31b9 7679 else if (MIPS_ELF_OPTIONS_SECTION_NAME_P (name))
b49e97c9
TS
7680 {
7681 hdr->sh_type = SHT_MIPS_OPTIONS;
7682 hdr->sh_entsize = 1;
7683 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7684 }
351cdf24
MF
7685 else if (CONST_STRNEQ (name, ".MIPS.abiflags"))
7686 {
7687 hdr->sh_type = SHT_MIPS_ABIFLAGS;
7688 hdr->sh_entsize = sizeof (Elf_External_ABIFlags_v0);
7689 }
1b315056 7690 else if (CONST_STRNEQ (name, ".debug_")
07d6d2b8 7691 || CONST_STRNEQ (name, ".zdebug_"))
b5482f21
NC
7692 {
7693 hdr->sh_type = SHT_MIPS_DWARF;
7694
7695 /* Irix facilities such as libexc expect a single .debug_frame
7696 per executable, the system ones have NOSTRIP set and the linker
7697 doesn't merge sections with different flags so ... */
7698 if (SGI_COMPAT (abfd) && CONST_STRNEQ (name, ".debug_frame"))
7699 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7700 }
b49e97c9
TS
7701 else if (strcmp (name, ".MIPS.symlib") == 0)
7702 {
7703 hdr->sh_type = SHT_MIPS_SYMBOL_LIB;
7704 /* The sh_link and sh_info fields are set in
07d6d2b8 7705 final_write_processing. */
b49e97c9 7706 }
0112cd26
NC
7707 else if (CONST_STRNEQ (name, ".MIPS.events")
7708 || CONST_STRNEQ (name, ".MIPS.post_rel"))
b49e97c9
TS
7709 {
7710 hdr->sh_type = SHT_MIPS_EVENTS;
7711 hdr->sh_flags |= SHF_MIPS_NOSTRIP;
7712 /* The sh_link field is set in final_write_processing. */
7713 }
7714 else if (strcmp (name, ".msym") == 0)
7715 {
7716 hdr->sh_type = SHT_MIPS_MSYM;
7717 hdr->sh_flags |= SHF_ALLOC;
7718 hdr->sh_entsize = 8;
7719 }
f16a9783
MS
7720 else if (strcmp (name, ".MIPS.xhash") == 0)
7721 {
7722 hdr->sh_type = SHT_MIPS_XHASH;
7723 hdr->sh_flags |= SHF_ALLOC;
7724 hdr->sh_entsize = get_elf_backend_data(abfd)->s->arch_size == 64 ? 0 : 4;
7725 }
b49e97c9 7726
7a79a000
TS
7727 /* The generic elf_fake_sections will set up REL_HDR using the default
7728 kind of relocations. We used to set up a second header for the
7729 non-default kind of relocations here, but only NewABI would use
7730 these, and the IRIX ld doesn't like resulting empty RELA sections.
7731 Thus we create those header only on demand now. */
b49e97c9 7732
b34976b6 7733 return TRUE;
b49e97c9
TS
7734}
7735
7736/* Given a BFD section, try to locate the corresponding ELF section
7737 index. This is used by both the 32-bit and the 64-bit ABI.
7738 Actually, it's not clear to me that the 64-bit ABI supports these,
7739 but for non-PIC objects we will certainly want support for at least
7740 the .scommon section. */
7741
b34976b6 7742bfd_boolean
9719ad41
RS
7743_bfd_mips_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
7744 asection *sec, int *retval)
b49e97c9 7745{
fd361982 7746 if (strcmp (bfd_section_name (sec), ".scommon") == 0)
b49e97c9
TS
7747 {
7748 *retval = SHN_MIPS_SCOMMON;
b34976b6 7749 return TRUE;
b49e97c9 7750 }
fd361982 7751 if (strcmp (bfd_section_name (sec), ".acommon") == 0)
b49e97c9
TS
7752 {
7753 *retval = SHN_MIPS_ACOMMON;
b34976b6 7754 return TRUE;
b49e97c9 7755 }
b34976b6 7756 return FALSE;
b49e97c9
TS
7757}
7758\f
7759/* Hook called by the linker routine which adds symbols from an object
7760 file. We must handle the special MIPS section numbers here. */
7761
b34976b6 7762bfd_boolean
9719ad41 7763_bfd_mips_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 7764 Elf_Internal_Sym *sym, const char **namep,
9719ad41
RS
7765 flagword *flagsp ATTRIBUTE_UNUSED,
7766 asection **secp, bfd_vma *valp)
b49e97c9
TS
7767{
7768 if (SGI_COMPAT (abfd)
7769 && (abfd->flags & DYNAMIC) != 0
7770 && strcmp (*namep, "_rld_new_interface") == 0)
7771 {
8dc1a139 7772 /* Skip IRIX5 rld entry name. */
b49e97c9 7773 *namep = NULL;
b34976b6 7774 return TRUE;
b49e97c9
TS
7775 }
7776
eedecc07
DD
7777 /* Shared objects may have a dynamic symbol '_gp_disp' defined as
7778 a SECTION *ABS*. This causes ld to think it can resolve _gp_disp
7779 by setting a DT_NEEDED for the shared object. Since _gp_disp is
7780 a magic symbol resolved by the linker, we ignore this bogus definition
7781 of _gp_disp. New ABI objects do not suffer from this problem so this
7782 is not done for them. */
7783 if (!NEWABI_P(abfd)
7784 && (sym->st_shndx == SHN_ABS)
7785 && (strcmp (*namep, "_gp_disp") == 0))
7786 {
7787 *namep = NULL;
7788 return TRUE;
7789 }
7790
b49e97c9
TS
7791 switch (sym->st_shndx)
7792 {
7793 case SHN_COMMON:
7794 /* Common symbols less than the GP size are automatically
7795 treated as SHN_MIPS_SCOMMON symbols. */
7796 if (sym->st_size > elf_gp_size (abfd)
b59eed79 7797 || ELF_ST_TYPE (sym->st_info) == STT_TLS
b49e97c9
TS
7798 || IRIX_COMPAT (abfd) == ict_irix6)
7799 break;
7800 /* Fall through. */
7801 case SHN_MIPS_SCOMMON:
7802 *secp = bfd_make_section_old_way (abfd, ".scommon");
7803 (*secp)->flags |= SEC_IS_COMMON;
7804 *valp = sym->st_size;
7805 break;
7806
7807 case SHN_MIPS_TEXT:
7808 /* This section is used in a shared object. */
698600e4 7809 if (mips_elf_tdata (abfd)->elf_text_section == NULL)
b49e97c9
TS
7810 {
7811 asymbol *elf_text_symbol;
7812 asection *elf_text_section;
986f0783 7813 size_t amt = sizeof (asection);
b49e97c9
TS
7814
7815 elf_text_section = bfd_zalloc (abfd, amt);
7816 if (elf_text_section == NULL)
b34976b6 7817 return FALSE;
b49e97c9
TS
7818
7819 amt = sizeof (asymbol);
7820 elf_text_symbol = bfd_zalloc (abfd, amt);
7821 if (elf_text_symbol == NULL)
b34976b6 7822 return FALSE;
b49e97c9
TS
7823
7824 /* Initialize the section. */
7825
698600e4
AM
7826 mips_elf_tdata (abfd)->elf_text_section = elf_text_section;
7827 mips_elf_tdata (abfd)->elf_text_symbol = elf_text_symbol;
b49e97c9
TS
7828
7829 elf_text_section->symbol = elf_text_symbol;
698600e4 7830 elf_text_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_text_symbol;
b49e97c9
TS
7831
7832 elf_text_section->name = ".text";
7833 elf_text_section->flags = SEC_NO_FLAGS;
7834 elf_text_section->output_section = NULL;
7835 elf_text_section->owner = abfd;
7836 elf_text_symbol->name = ".text";
7837 elf_text_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7838 elf_text_symbol->section = elf_text_section;
7839 }
7840 /* This code used to do *secp = bfd_und_section_ptr if
07d6d2b8
AM
7841 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7842 so I took it out. */
698600e4 7843 *secp = mips_elf_tdata (abfd)->elf_text_section;
b49e97c9
TS
7844 break;
7845
7846 case SHN_MIPS_ACOMMON:
7847 /* Fall through. XXX Can we treat this as allocated data? */
7848 case SHN_MIPS_DATA:
7849 /* This section is used in a shared object. */
698600e4 7850 if (mips_elf_tdata (abfd)->elf_data_section == NULL)
b49e97c9
TS
7851 {
7852 asymbol *elf_data_symbol;
7853 asection *elf_data_section;
986f0783 7854 size_t amt = sizeof (asection);
b49e97c9
TS
7855
7856 elf_data_section = bfd_zalloc (abfd, amt);
7857 if (elf_data_section == NULL)
b34976b6 7858 return FALSE;
b49e97c9
TS
7859
7860 amt = sizeof (asymbol);
7861 elf_data_symbol = bfd_zalloc (abfd, amt);
7862 if (elf_data_symbol == NULL)
b34976b6 7863 return FALSE;
b49e97c9
TS
7864
7865 /* Initialize the section. */
7866
698600e4
AM
7867 mips_elf_tdata (abfd)->elf_data_section = elf_data_section;
7868 mips_elf_tdata (abfd)->elf_data_symbol = elf_data_symbol;
b49e97c9
TS
7869
7870 elf_data_section->symbol = elf_data_symbol;
698600e4 7871 elf_data_section->symbol_ptr_ptr = &mips_elf_tdata (abfd)->elf_data_symbol;
b49e97c9
TS
7872
7873 elf_data_section->name = ".data";
7874 elf_data_section->flags = SEC_NO_FLAGS;
7875 elf_data_section->output_section = NULL;
7876 elf_data_section->owner = abfd;
7877 elf_data_symbol->name = ".data";
7878 elf_data_symbol->flags = BSF_SECTION_SYM | BSF_DYNAMIC;
7879 elf_data_symbol->section = elf_data_section;
7880 }
7881 /* This code used to do *secp = bfd_und_section_ptr if
07d6d2b8
AM
7882 bfd_link_pic (info). I don't know why, and that doesn't make sense,
7883 so I took it out. */
698600e4 7884 *secp = mips_elf_tdata (abfd)->elf_data_section;
b49e97c9
TS
7885 break;
7886
7887 case SHN_MIPS_SUNDEFINED:
7888 *secp = bfd_und_section_ptr;
7889 break;
7890 }
7891
7892 if (SGI_COMPAT (abfd)
0e1862bb 7893 && ! bfd_link_pic (info)
f13a99db 7894 && info->output_bfd->xvec == abfd->xvec
b49e97c9
TS
7895 && strcmp (*namep, "__rld_obj_head") == 0)
7896 {
7897 struct elf_link_hash_entry *h;
14a793b2 7898 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7899
7900 /* Mark __rld_obj_head as dynamic. */
14a793b2 7901 bh = NULL;
b49e97c9 7902 if (! (_bfd_generic_link_add_one_symbol
9719ad41 7903 (info, abfd, *namep, BSF_GLOBAL, *secp, *valp, NULL, FALSE,
14a793b2 7904 get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 7905 return FALSE;
14a793b2
AM
7906
7907 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
7908 h->non_elf = 0;
7909 h->def_regular = 1;
b49e97c9
TS
7910 h->type = STT_OBJECT;
7911
c152c796 7912 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7913 return FALSE;
b49e97c9 7914
b34976b6 7915 mips_elf_hash_table (info)->use_rld_obj_head = TRUE;
b4082c70 7916 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
7917 }
7918
7919 /* If this is a mips16 text symbol, add 1 to the value to make it
7920 odd. This will cause something like .word SYM to come up with
7921 the right value when it is loaded into the PC. */
df58fc94 7922 if (ELF_ST_IS_COMPRESSED (sym->st_other))
b49e97c9
TS
7923 ++*valp;
7924
b34976b6 7925 return TRUE;
b49e97c9
TS
7926}
7927
7928/* This hook function is called before the linker writes out a global
7929 symbol. We mark symbols as small common if appropriate. This is
7930 also where we undo the increment of the value for a mips16 symbol. */
7931
6e0b88f1 7932int
9719ad41
RS
7933_bfd_mips_elf_link_output_symbol_hook
7934 (struct bfd_link_info *info ATTRIBUTE_UNUSED,
7935 const char *name ATTRIBUTE_UNUSED, Elf_Internal_Sym *sym,
7936 asection *input_sec, struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
b49e97c9
TS
7937{
7938 /* If we see a common symbol, which implies a relocatable link, then
7939 if a symbol was small common in an input file, mark it as small
7940 common in the output file. */
7941 if (sym->st_shndx == SHN_COMMON
7942 && strcmp (input_sec->name, ".scommon") == 0)
7943 sym->st_shndx = SHN_MIPS_SCOMMON;
7944
df58fc94 7945 if (ELF_ST_IS_COMPRESSED (sym->st_other))
79cda7cf 7946 sym->st_value &= ~1;
b49e97c9 7947
6e0b88f1 7948 return 1;
b49e97c9
TS
7949}
7950\f
7951/* Functions for the dynamic linker. */
7952
7953/* Create dynamic sections when linking against a dynamic object. */
7954
b34976b6 7955bfd_boolean
9719ad41 7956_bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
b49e97c9
TS
7957{
7958 struct elf_link_hash_entry *h;
14a793b2 7959 struct bfd_link_hash_entry *bh;
b49e97c9
TS
7960 flagword flags;
7961 register asection *s;
7962 const char * const *namep;
0a44bf69 7963 struct mips_elf_link_hash_table *htab;
b49e97c9 7964
0a44bf69 7965 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
7966 BFD_ASSERT (htab != NULL);
7967
b49e97c9
TS
7968 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7969 | SEC_LINKER_CREATED | SEC_READONLY);
7970
0a44bf69
RS
7971 /* The psABI requires a read-only .dynamic section, but the VxWorks
7972 EABI doesn't. */
90c14f0c 7973 if (htab->root.target_os != is_vxworks)
b49e97c9 7974 {
3d4d4302 7975 s = bfd_get_linker_section (abfd, ".dynamic");
0a44bf69
RS
7976 if (s != NULL)
7977 {
fd361982 7978 if (!bfd_set_section_flags (s, flags))
0a44bf69
RS
7979 return FALSE;
7980 }
b49e97c9
TS
7981 }
7982
7983 /* We need to create .got section. */
23cc69b6 7984 if (!mips_elf_create_got_section (abfd, info))
f4416af6
AO
7985 return FALSE;
7986
0a44bf69 7987 if (! mips_elf_rel_dyn_section (info, TRUE))
b34976b6 7988 return FALSE;
b49e97c9 7989
b49e97c9 7990 /* Create .stub section. */
3d4d4302
AM
7991 s = bfd_make_section_anyway_with_flags (abfd,
7992 MIPS_ELF_STUB_SECTION_NAME (abfd),
7993 flags | SEC_CODE);
4e41d0d7 7994 if (s == NULL
fd361982 7995 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
4e41d0d7
RS
7996 return FALSE;
7997 htab->sstubs = s;
b49e97c9 7998
e6aea42d 7999 if (!mips_elf_hash_table (info)->use_rld_obj_head
0e1862bb 8000 && bfd_link_executable (info)
3d4d4302 8001 && bfd_get_linker_section (abfd, ".rld_map") == NULL)
b49e97c9 8002 {
3d4d4302
AM
8003 s = bfd_make_section_anyway_with_flags (abfd, ".rld_map",
8004 flags &~ (flagword) SEC_READONLY);
b49e97c9 8005 if (s == NULL
fd361982 8006 || !bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd)))
b34976b6 8007 return FALSE;
b49e97c9
TS
8008 }
8009
f16a9783
MS
8010 /* Create .MIPS.xhash section. */
8011 if (info->emit_gnu_hash)
8012 s = bfd_make_section_anyway_with_flags (abfd, ".MIPS.xhash",
8013 flags | SEC_READONLY);
8014
b49e97c9
TS
8015 /* On IRIX5, we adjust add some additional symbols and change the
8016 alignments of several sections. There is no ABI documentation
8017 indicating that this is necessary on IRIX6, nor any evidence that
8018 the linker takes such action. */
8019 if (IRIX_COMPAT (abfd) == ict_irix5)
8020 {
8021 for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
8022 {
14a793b2 8023 bh = NULL;
b49e97c9 8024 if (! (_bfd_generic_link_add_one_symbol
9719ad41
RS
8025 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr, 0,
8026 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 8027 return FALSE;
14a793b2
AM
8028
8029 h = (struct elf_link_hash_entry *) bh;
12f09816 8030 h->mark = 1;
f5385ebf
AM
8031 h->non_elf = 0;
8032 h->def_regular = 1;
b49e97c9
TS
8033 h->type = STT_SECTION;
8034
c152c796 8035 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 8036 return FALSE;
b49e97c9
TS
8037 }
8038
8039 /* We need to create a .compact_rel section. */
8040 if (SGI_COMPAT (abfd))
8041 {
8042 if (!mips_elf_create_compact_rel_section (abfd, info))
b34976b6 8043 return FALSE;
b49e97c9
TS
8044 }
8045
44c410de 8046 /* Change alignments of some sections. */
3d4d4302 8047 s = bfd_get_linker_section (abfd, ".hash");
b49e97c9 8048 if (s != NULL)
fd361982 8049 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
a253d456 8050
3d4d4302 8051 s = bfd_get_linker_section (abfd, ".dynsym");
b49e97c9 8052 if (s != NULL)
fd361982 8053 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
a253d456 8054
3d4d4302 8055 s = bfd_get_linker_section (abfd, ".dynstr");
b49e97c9 8056 if (s != NULL)
fd361982 8057 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
a253d456 8058
3d4d4302 8059 /* ??? */
b49e97c9
TS
8060 s = bfd_get_section_by_name (abfd, ".reginfo");
8061 if (s != NULL)
fd361982 8062 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
a253d456 8063
3d4d4302 8064 s = bfd_get_linker_section (abfd, ".dynamic");
b49e97c9 8065 if (s != NULL)
fd361982 8066 bfd_set_section_alignment (s, MIPS_ELF_LOG_FILE_ALIGN (abfd));
b49e97c9
TS
8067 }
8068
0e1862bb 8069 if (bfd_link_executable (info))
b49e97c9 8070 {
14a793b2
AM
8071 const char *name;
8072
8073 name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
8074 bh = NULL;
8075 if (!(_bfd_generic_link_add_one_symbol
9719ad41
RS
8076 (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr, 0,
8077 NULL, FALSE, get_elf_backend_data (abfd)->collect, &bh)))
b34976b6 8078 return FALSE;
14a793b2
AM
8079
8080 h = (struct elf_link_hash_entry *) bh;
f5385ebf
AM
8081 h->non_elf = 0;
8082 h->def_regular = 1;
b49e97c9
TS
8083 h->type = STT_SECTION;
8084
c152c796 8085 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 8086 return FALSE;
b49e97c9
TS
8087
8088 if (! mips_elf_hash_table (info)->use_rld_obj_head)
8089 {
8090 /* __rld_map is a four byte word located in the .data section
8091 and is filled in by the rtld to contain a pointer to
8092 the _r_debug structure. Its symbol value will be set in
8093 _bfd_mips_elf_finish_dynamic_symbol. */
3d4d4302 8094 s = bfd_get_linker_section (abfd, ".rld_map");
0abfb97a 8095 BFD_ASSERT (s != NULL);
14a793b2 8096
0abfb97a
L
8097 name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
8098 bh = NULL;
8099 if (!(_bfd_generic_link_add_one_symbol
8100 (info, abfd, name, BSF_GLOBAL, s, 0, NULL, FALSE,
8101 get_elf_backend_data (abfd)->collect, &bh)))
8102 return FALSE;
b49e97c9 8103
0abfb97a
L
8104 h = (struct elf_link_hash_entry *) bh;
8105 h->non_elf = 0;
8106 h->def_regular = 1;
8107 h->type = STT_OBJECT;
8108
8109 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8110 return FALSE;
b4082c70 8111 mips_elf_hash_table (info)->rld_symbol = h;
b49e97c9
TS
8112 }
8113 }
8114
861fb55a 8115 /* Create the .plt, .rel(a).plt, .dynbss and .rel(a).bss sections.
c164a95d 8116 Also, on VxWorks, create the _PROCEDURE_LINKAGE_TABLE_ symbol. */
861fb55a
DJ
8117 if (!_bfd_elf_create_dynamic_sections (abfd, info))
8118 return FALSE;
8119
1bbce132 8120 /* Do the usual VxWorks handling. */
90c14f0c 8121 if (htab->root.target_os == is_vxworks
1bbce132
MR
8122 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
8123 return FALSE;
0a44bf69 8124
b34976b6 8125 return TRUE;
b49e97c9
TS
8126}
8127\f
c224138d
RS
8128/* Return true if relocation REL against section SEC is a REL rather than
8129 RELA relocation. RELOCS is the first relocation in the section and
8130 ABFD is the bfd that contains SEC. */
8131
8132static bfd_boolean
8133mips_elf_rel_relocation_p (bfd *abfd, asection *sec,
8134 const Elf_Internal_Rela *relocs,
8135 const Elf_Internal_Rela *rel)
8136{
8137 Elf_Internal_Shdr *rel_hdr;
8138 const struct elf_backend_data *bed;
8139
d4730f92
BS
8140 /* To determine which flavor of relocation this is, we depend on the
8141 fact that the INPUT_SECTION's REL_HDR is read before RELA_HDR. */
8142 rel_hdr = elf_section_data (sec)->rel.hdr;
8143 if (rel_hdr == NULL)
8144 return FALSE;
c224138d 8145 bed = get_elf_backend_data (abfd);
d4730f92
BS
8146 return ((size_t) (rel - relocs)
8147 < NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel);
c224138d
RS
8148}
8149
8150/* Read the addend for REL relocation REL, which belongs to bfd ABFD.
8151 HOWTO is the relocation's howto and CONTENTS points to the contents
8152 of the section that REL is against. */
8153
8154static bfd_vma
8155mips_elf_read_rel_addend (bfd *abfd, const Elf_Internal_Rela *rel,
8156 reloc_howto_type *howto, bfd_byte *contents)
8157{
8158 bfd_byte *location;
8159 unsigned int r_type;
8160 bfd_vma addend;
17c6c9d9 8161 bfd_vma bytes;
c224138d
RS
8162
8163 r_type = ELF_R_TYPE (abfd, rel->r_info);
8164 location = contents + rel->r_offset;
8165
8166 /* Get the addend, which is stored in the input file. */
df58fc94 8167 _bfd_mips_elf_reloc_unshuffle (abfd, r_type, FALSE, location);
17c6c9d9 8168 bytes = mips_elf_obtain_contents (howto, rel, abfd, contents);
df58fc94 8169 _bfd_mips_elf_reloc_shuffle (abfd, r_type, FALSE, location);
c224138d 8170
17c6c9d9
MR
8171 addend = bytes & howto->src_mask;
8172
8173 /* Shift is 2, unusually, for microMIPS JALX. Adjust the addend
8174 accordingly. */
8175 if (r_type == R_MICROMIPS_26_S1 && (bytes >> 26) == 0x3c)
8176 addend <<= 1;
8177
8178 return addend;
c224138d
RS
8179}
8180
8181/* REL is a relocation in ABFD that needs a partnering LO16 relocation
8182 and *ADDEND is the addend for REL itself. Look for the LO16 relocation
8183 and update *ADDEND with the final addend. Return true on success
8184 or false if the LO16 could not be found. RELEND is the exclusive
8185 upper bound on the relocations for REL's section. */
8186
8187static bfd_boolean
8188mips_elf_add_lo16_rel_addend (bfd *abfd,
8189 const Elf_Internal_Rela *rel,
8190 const Elf_Internal_Rela *relend,
8191 bfd_byte *contents, bfd_vma *addend)
8192{
8193 unsigned int r_type, lo16_type;
8194 const Elf_Internal_Rela *lo16_relocation;
8195 reloc_howto_type *lo16_howto;
8196 bfd_vma l;
8197
8198 r_type = ELF_R_TYPE (abfd, rel->r_info);
738e5348 8199 if (mips16_reloc_p (r_type))
c224138d 8200 lo16_type = R_MIPS16_LO16;
df58fc94
RS
8201 else if (micromips_reloc_p (r_type))
8202 lo16_type = R_MICROMIPS_LO16;
7361da2c
AB
8203 else if (r_type == R_MIPS_PCHI16)
8204 lo16_type = R_MIPS_PCLO16;
c224138d
RS
8205 else
8206 lo16_type = R_MIPS_LO16;
8207
8208 /* The combined value is the sum of the HI16 addend, left-shifted by
8209 sixteen bits, and the LO16 addend, sign extended. (Usually, the
8210 code does a `lui' of the HI16 value, and then an `addiu' of the
8211 LO16 value.)
8212
8213 Scan ahead to find a matching LO16 relocation.
8214
8215 According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
8216 be immediately following. However, for the IRIX6 ABI, the next
8217 relocation may be a composed relocation consisting of several
8218 relocations for the same address. In that case, the R_MIPS_LO16
8219 relocation may occur as one of these. We permit a similar
8220 extension in general, as that is useful for GCC.
8221
8222 In some cases GCC dead code elimination removes the LO16 but keeps
8223 the corresponding HI16. This is strictly speaking a violation of
8224 the ABI but not immediately harmful. */
8225 lo16_relocation = mips_elf_next_relocation (abfd, lo16_type, rel, relend);
8226 if (lo16_relocation == NULL)
8227 return FALSE;
8228
8229 /* Obtain the addend kept there. */
8230 lo16_howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, lo16_type, FALSE);
8231 l = mips_elf_read_rel_addend (abfd, lo16_relocation, lo16_howto, contents);
8232
8233 l <<= lo16_howto->rightshift;
8234 l = _bfd_mips_elf_sign_extend (l, 16);
8235
8236 *addend <<= 16;
8237 *addend += l;
8238 return TRUE;
8239}
8240
8241/* Try to read the contents of section SEC in bfd ABFD. Return true and
8242 store the contents in *CONTENTS on success. Assume that *CONTENTS
8243 already holds the contents if it is nonull on entry. */
8244
8245static bfd_boolean
8246mips_elf_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents)
8247{
8248 if (*contents)
8249 return TRUE;
8250
8251 /* Get cached copy if it exists. */
8252 if (elf_section_data (sec)->this_hdr.contents != NULL)
8253 {
8254 *contents = elf_section_data (sec)->this_hdr.contents;
8255 return TRUE;
8256 }
8257
8258 return bfd_malloc_and_get_section (abfd, sec, contents);
8259}
8260
1bbce132
MR
8261/* Make a new PLT record to keep internal data. */
8262
8263static struct plt_entry *
8264mips_elf_make_plt_record (bfd *abfd)
8265{
8266 struct plt_entry *entry;
8267
8268 entry = bfd_zalloc (abfd, sizeof (*entry));
8269 if (entry == NULL)
8270 return NULL;
8271
8272 entry->stub_offset = MINUS_ONE;
8273 entry->mips_offset = MINUS_ONE;
8274 entry->comp_offset = MINUS_ONE;
8275 entry->gotplt_index = MINUS_ONE;
8276 return entry;
8277}
8278
47275900
MR
8279/* Define the special `__gnu_absolute_zero' symbol. We only need this
8280 for PIC code, as otherwise there is no load-time relocation involved
8281 and local GOT entries whose value is zero at static link time will
8282 retain their value at load time. */
8283
8284static bfd_boolean
8285mips_elf_define_absolute_zero (bfd *abfd, struct bfd_link_info *info,
8286 struct mips_elf_link_hash_table *htab,
8287 unsigned int r_type)
8288{
8289 union
8290 {
8291 struct elf_link_hash_entry *eh;
8292 struct bfd_link_hash_entry *bh;
8293 }
8294 hzero;
8295
8296 BFD_ASSERT (!htab->use_absolute_zero);
8297 BFD_ASSERT (bfd_link_pic (info));
8298
8299 hzero.bh = NULL;
8300 if (!_bfd_generic_link_add_one_symbol (info, abfd, "__gnu_absolute_zero",
8301 BSF_GLOBAL, bfd_abs_section_ptr, 0,
8302 NULL, FALSE, FALSE, &hzero.bh))
8303 return FALSE;
8304
8305 BFD_ASSERT (hzero.bh != NULL);
8306 hzero.eh->size = 0;
8307 hzero.eh->type = STT_NOTYPE;
8308 hzero.eh->other = STV_PROTECTED;
8309 hzero.eh->def_regular = 1;
8310 hzero.eh->non_elf = 0;
8311
8312 if (!mips_elf_record_global_got_symbol (hzero.eh, abfd, info, TRUE, r_type))
8313 return FALSE;
8314
8315 htab->use_absolute_zero = TRUE;
8316
8317 return TRUE;
8318}
8319
b49e97c9 8320/* Look through the relocs for a section during the first phase, and
1bbce132
MR
8321 allocate space in the global offset table and record the need for
8322 standard MIPS and compressed procedure linkage table entries. */
b49e97c9 8323
b34976b6 8324bfd_boolean
9719ad41
RS
8325_bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
8326 asection *sec, const Elf_Internal_Rela *relocs)
b49e97c9
TS
8327{
8328 const char *name;
8329 bfd *dynobj;
8330 Elf_Internal_Shdr *symtab_hdr;
8331 struct elf_link_hash_entry **sym_hashes;
b49e97c9
TS
8332 size_t extsymoff;
8333 const Elf_Internal_Rela *rel;
8334 const Elf_Internal_Rela *rel_end;
b49e97c9 8335 asection *sreloc;
9c5bfbb7 8336 const struct elf_backend_data *bed;
0a44bf69 8337 struct mips_elf_link_hash_table *htab;
c224138d
RS
8338 bfd_byte *contents;
8339 bfd_vma addend;
8340 reloc_howto_type *howto;
b49e97c9 8341
0e1862bb 8342 if (bfd_link_relocatable (info))
b34976b6 8343 return TRUE;
b49e97c9 8344
0a44bf69 8345 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
8346 BFD_ASSERT (htab != NULL);
8347
b49e97c9
TS
8348 dynobj = elf_hash_table (info)->dynobj;
8349 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8350 sym_hashes = elf_sym_hashes (abfd);
8351 extsymoff = (elf_bad_symtab (abfd)) ? 0 : symtab_hdr->sh_info;
8352
738e5348 8353 bed = get_elf_backend_data (abfd);
056bafd4 8354 rel_end = relocs + sec->reloc_count;
738e5348 8355
b49e97c9
TS
8356 /* Check for the mips16 stub sections. */
8357
fd361982 8358 name = bfd_section_name (sec);
b9d58d71 8359 if (FN_STUB_P (name))
b49e97c9
TS
8360 {
8361 unsigned long r_symndx;
8362
8363 /* Look at the relocation information to figure out which symbol
07d6d2b8 8364 this is for. */
b49e97c9 8365
cb4437b8 8366 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
8367 if (r_symndx == 0)
8368 {
4eca0228 8369 _bfd_error_handler
695344c0 8370 /* xgettext:c-format */
2c1c9679 8371 (_("%pB: warning: cannot determine the target function for"
738e5348
RS
8372 " stub section `%s'"),
8373 abfd, name);
8374 bfd_set_error (bfd_error_bad_value);
8375 return FALSE;
8376 }
b49e97c9
TS
8377
8378 if (r_symndx < extsymoff
8379 || sym_hashes[r_symndx - extsymoff] == NULL)
8380 {
8381 asection *o;
8382
8383 /* This stub is for a local symbol. This stub will only be
07d6d2b8
AM
8384 needed if there is some relocation in this BFD, other
8385 than a 16 bit function call, which refers to this symbol. */
b49e97c9
TS
8386 for (o = abfd->sections; o != NULL; o = o->next)
8387 {
8388 Elf_Internal_Rela *sec_relocs;
8389 const Elf_Internal_Rela *r, *rend;
8390
8391 /* We can ignore stub sections when looking for relocs. */
8392 if ((o->flags & SEC_RELOC) == 0
8393 || o->reloc_count == 0
738e5348 8394 || section_allows_mips16_refs_p (o))
b49e97c9
TS
8395 continue;
8396
45d6a902 8397 sec_relocs
9719ad41 8398 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 8399 info->keep_memory);
b49e97c9 8400 if (sec_relocs == NULL)
b34976b6 8401 return FALSE;
b49e97c9
TS
8402
8403 rend = sec_relocs + o->reloc_count;
8404 for (r = sec_relocs; r < rend; r++)
8405 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
738e5348 8406 && !mips16_call_reloc_p (ELF_R_TYPE (abfd, r->r_info)))
b49e97c9
TS
8407 break;
8408
6cdc0ccc 8409 if (elf_section_data (o)->relocs != sec_relocs)
b49e97c9
TS
8410 free (sec_relocs);
8411
8412 if (r < rend)
8413 break;
8414 }
8415
8416 if (o == NULL)
8417 {
8418 /* There is no non-call reloc for this stub, so we do
07d6d2b8
AM
8419 not need it. Since this function is called before
8420 the linker maps input sections to output sections, we
8421 can easily discard it by setting the SEC_EXCLUDE
8422 flag. */
b49e97c9 8423 sec->flags |= SEC_EXCLUDE;
b34976b6 8424 return TRUE;
b49e97c9
TS
8425 }
8426
8427 /* Record this stub in an array of local symbol stubs for
07d6d2b8 8428 this BFD. */
698600e4 8429 if (mips_elf_tdata (abfd)->local_stubs == NULL)
b49e97c9
TS
8430 {
8431 unsigned long symcount;
8432 asection **n;
8433 bfd_size_type amt;
8434
8435 if (elf_bad_symtab (abfd))
8436 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8437 else
8438 symcount = symtab_hdr->sh_info;
8439 amt = symcount * sizeof (asection *);
9719ad41 8440 n = bfd_zalloc (abfd, amt);
b49e97c9 8441 if (n == NULL)
b34976b6 8442 return FALSE;
698600e4 8443 mips_elf_tdata (abfd)->local_stubs = n;
b49e97c9
TS
8444 }
8445
b9d58d71 8446 sec->flags |= SEC_KEEP;
698600e4 8447 mips_elf_tdata (abfd)->local_stubs[r_symndx] = sec;
b49e97c9
TS
8448
8449 /* We don't need to set mips16_stubs_seen in this case.
07d6d2b8
AM
8450 That flag is used to see whether we need to look through
8451 the global symbol table for stubs. We don't need to set
8452 it here, because we just have a local stub. */
b49e97c9
TS
8453 }
8454 else
8455 {
8456 struct mips_elf_link_hash_entry *h;
8457
8458 h = ((struct mips_elf_link_hash_entry *)
8459 sym_hashes[r_symndx - extsymoff]);
8460
973a3492
L
8461 while (h->root.root.type == bfd_link_hash_indirect
8462 || h->root.root.type == bfd_link_hash_warning)
8463 h = (struct mips_elf_link_hash_entry *) h->root.root.u.i.link;
8464
b49e97c9
TS
8465 /* H is the symbol this stub is for. */
8466
b9d58d71
TS
8467 /* If we already have an appropriate stub for this function, we
8468 don't need another one, so we can discard this one. Since
8469 this function is called before the linker maps input sections
8470 to output sections, we can easily discard it by setting the
8471 SEC_EXCLUDE flag. */
8472 if (h->fn_stub != NULL)
8473 {
8474 sec->flags |= SEC_EXCLUDE;
8475 return TRUE;
8476 }
8477
8478 sec->flags |= SEC_KEEP;
b49e97c9 8479 h->fn_stub = sec;
b34976b6 8480 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
b49e97c9
TS
8481 }
8482 }
b9d58d71 8483 else if (CALL_STUB_P (name) || CALL_FP_STUB_P (name))
b49e97c9
TS
8484 {
8485 unsigned long r_symndx;
8486 struct mips_elf_link_hash_entry *h;
8487 asection **loc;
8488
8489 /* Look at the relocation information to figure out which symbol
07d6d2b8 8490 this is for. */
b49e97c9 8491
cb4437b8 8492 r_symndx = mips16_stub_symndx (bed, sec, relocs, rel_end);
738e5348
RS
8493 if (r_symndx == 0)
8494 {
4eca0228 8495 _bfd_error_handler
695344c0 8496 /* xgettext:c-format */
2c1c9679 8497 (_("%pB: warning: cannot determine the target function for"
738e5348
RS
8498 " stub section `%s'"),
8499 abfd, name);
8500 bfd_set_error (bfd_error_bad_value);
8501 return FALSE;
8502 }
b49e97c9
TS
8503
8504 if (r_symndx < extsymoff
8505 || sym_hashes[r_symndx - extsymoff] == NULL)
8506 {
b9d58d71 8507 asection *o;
b49e97c9 8508
b9d58d71 8509 /* This stub is for a local symbol. This stub will only be
07d6d2b8
AM
8510 needed if there is some relocation (R_MIPS16_26) in this BFD
8511 that refers to this symbol. */
b9d58d71
TS
8512 for (o = abfd->sections; o != NULL; o = o->next)
8513 {
8514 Elf_Internal_Rela *sec_relocs;
8515 const Elf_Internal_Rela *r, *rend;
8516
8517 /* We can ignore stub sections when looking for relocs. */
8518 if ((o->flags & SEC_RELOC) == 0
8519 || o->reloc_count == 0
738e5348 8520 || section_allows_mips16_refs_p (o))
b9d58d71
TS
8521 continue;
8522
8523 sec_relocs
8524 = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
8525 info->keep_memory);
8526 if (sec_relocs == NULL)
8527 return FALSE;
8528
8529 rend = sec_relocs + o->reloc_count;
8530 for (r = sec_relocs; r < rend; r++)
8531 if (ELF_R_SYM (abfd, r->r_info) == r_symndx
8532 && ELF_R_TYPE (abfd, r->r_info) == R_MIPS16_26)
8533 break;
8534
8535 if (elf_section_data (o)->relocs != sec_relocs)
8536 free (sec_relocs);
8537
8538 if (r < rend)
8539 break;
8540 }
8541
8542 if (o == NULL)
8543 {
8544 /* There is no non-call reloc for this stub, so we do
07d6d2b8
AM
8545 not need it. Since this function is called before
8546 the linker maps input sections to output sections, we
8547 can easily discard it by setting the SEC_EXCLUDE
8548 flag. */
b9d58d71
TS
8549 sec->flags |= SEC_EXCLUDE;
8550 return TRUE;
8551 }
8552
8553 /* Record this stub in an array of local symbol call_stubs for
07d6d2b8 8554 this BFD. */
698600e4 8555 if (mips_elf_tdata (abfd)->local_call_stubs == NULL)
b9d58d71
TS
8556 {
8557 unsigned long symcount;
8558 asection **n;
8559 bfd_size_type amt;
8560
8561 if (elf_bad_symtab (abfd))
8562 symcount = NUM_SHDR_ENTRIES (symtab_hdr);
8563 else
8564 symcount = symtab_hdr->sh_info;
8565 amt = symcount * sizeof (asection *);
8566 n = bfd_zalloc (abfd, amt);
8567 if (n == NULL)
8568 return FALSE;
698600e4 8569 mips_elf_tdata (abfd)->local_call_stubs = n;
b9d58d71 8570 }
b49e97c9 8571
b9d58d71 8572 sec->flags |= SEC_KEEP;
698600e4 8573 mips_elf_tdata (abfd)->local_call_stubs[r_symndx] = sec;
b49e97c9 8574
b9d58d71 8575 /* We don't need to set mips16_stubs_seen in this case.
07d6d2b8
AM
8576 That flag is used to see whether we need to look through
8577 the global symbol table for stubs. We don't need to set
8578 it here, because we just have a local stub. */
b9d58d71 8579 }
b49e97c9 8580 else
b49e97c9 8581 {
b9d58d71
TS
8582 h = ((struct mips_elf_link_hash_entry *)
8583 sym_hashes[r_symndx - extsymoff]);
68ffbac6 8584
b9d58d71 8585 /* H is the symbol this stub is for. */
68ffbac6 8586
b9d58d71
TS
8587 if (CALL_FP_STUB_P (name))
8588 loc = &h->call_fp_stub;
8589 else
8590 loc = &h->call_stub;
68ffbac6 8591
b9d58d71
TS
8592 /* If we already have an appropriate stub for this function, we
8593 don't need another one, so we can discard this one. Since
8594 this function is called before the linker maps input sections
8595 to output sections, we can easily discard it by setting the
8596 SEC_EXCLUDE flag. */
8597 if (*loc != NULL)
8598 {
8599 sec->flags |= SEC_EXCLUDE;
8600 return TRUE;
8601 }
b49e97c9 8602
b9d58d71
TS
8603 sec->flags |= SEC_KEEP;
8604 *loc = sec;
8605 mips_elf_hash_table (info)->mips16_stubs_seen = TRUE;
8606 }
b49e97c9
TS
8607 }
8608
b49e97c9 8609 sreloc = NULL;
c224138d 8610 contents = NULL;
b49e97c9
TS
8611 for (rel = relocs; rel < rel_end; ++rel)
8612 {
8613 unsigned long r_symndx;
8614 unsigned int r_type;
8615 struct elf_link_hash_entry *h;
861fb55a 8616 bfd_boolean can_make_dynamic_p;
c5d6fa44
RS
8617 bfd_boolean call_reloc_p;
8618 bfd_boolean constrain_symbol_p;
b49e97c9
TS
8619
8620 r_symndx = ELF_R_SYM (abfd, rel->r_info);
8621 r_type = ELF_R_TYPE (abfd, rel->r_info);
8622
8623 if (r_symndx < extsymoff)
8624 h = NULL;
8625 else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
8626 {
4eca0228 8627 _bfd_error_handler
695344c0 8628 /* xgettext:c-format */
2c1c9679 8629 (_("%pB: malformed reloc detected for section %s"),
d003868e 8630 abfd, name);
b49e97c9 8631 bfd_set_error (bfd_error_bad_value);
b34976b6 8632 return FALSE;
b49e97c9
TS
8633 }
8634 else
8635 {
8636 h = sym_hashes[r_symndx - extsymoff];
81fbe831
AM
8637 if (h != NULL)
8638 {
8639 while (h->root.type == bfd_link_hash_indirect
8640 || h->root.type == bfd_link_hash_warning)
8641 h = (struct elf_link_hash_entry *) h->root.u.i.link;
81fbe831 8642 }
861fb55a 8643 }
b49e97c9 8644
861fb55a
DJ
8645 /* Set CAN_MAKE_DYNAMIC_P to true if we can convert this
8646 relocation into a dynamic one. */
8647 can_make_dynamic_p = FALSE;
c5d6fa44
RS
8648
8649 /* Set CALL_RELOC_P to true if the relocation is for a call,
8650 and if pointer equality therefore doesn't matter. */
8651 call_reloc_p = FALSE;
8652
8653 /* Set CONSTRAIN_SYMBOL_P if we need to take the relocation
c4b126b8
L
8654 into account when deciding how to define the symbol. */
8655 constrain_symbol_p = TRUE;
c5d6fa44 8656
861fb55a
DJ
8657 switch (r_type)
8658 {
861fb55a
DJ
8659 case R_MIPS_CALL16:
8660 case R_MIPS_CALL_HI16:
8661 case R_MIPS_CALL_LO16:
c5d6fa44
RS
8662 case R_MIPS16_CALL16:
8663 case R_MICROMIPS_CALL16:
8664 case R_MICROMIPS_CALL_HI16:
8665 case R_MICROMIPS_CALL_LO16:
8666 call_reloc_p = TRUE;
8667 /* Fall through. */
8668
8669 case R_MIPS_GOT16:
861fb55a
DJ
8670 case R_MIPS_GOT_LO16:
8671 case R_MIPS_GOT_PAGE:
861fb55a 8672 case R_MIPS_GOT_DISP:
47275900
MR
8673 case R_MIPS16_GOT16:
8674 case R_MICROMIPS_GOT16:
8675 case R_MICROMIPS_GOT_LO16:
8676 case R_MICROMIPS_GOT_PAGE:
8677 case R_MICROMIPS_GOT_DISP:
8678 /* If we have a symbol that will resolve to zero at static link
8679 time and it is used by a GOT relocation applied to code we
8680 cannot relax to an immediate zero load, then we will be using
8681 the special `__gnu_absolute_zero' symbol whose value is zero
8682 at dynamic load time. We ignore HI16-type GOT relocations at
8683 this stage, because their handling will depend entirely on
8684 the corresponding LO16-type GOT relocation. */
8685 if (!call_hi16_reloc_p (r_type)
8686 && h != NULL
8687 && bfd_link_pic (info)
8688 && !htab->use_absolute_zero
8689 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
8690 {
8691 bfd_boolean rel_reloc;
8692
8693 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8694 return FALSE;
8695
8696 rel_reloc = mips_elf_rel_relocation_p (abfd, sec, relocs, rel);
8697 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, !rel_reloc);
8698
8699 if (!mips_elf_nullify_got_load (abfd, contents, rel, howto,
8700 FALSE))
8701 if (!mips_elf_define_absolute_zero (abfd, info, htab, r_type))
8702 return FALSE;
8703 }
8704
8705 /* Fall through. */
8706 case R_MIPS_GOT_HI16:
8707 case R_MIPS_GOT_OFST:
861fb55a
DJ
8708 case R_MIPS_TLS_GOTTPREL:
8709 case R_MIPS_TLS_GD:
8710 case R_MIPS_TLS_LDM:
d0f13682
CLT
8711 case R_MIPS16_TLS_GOTTPREL:
8712 case R_MIPS16_TLS_GD:
8713 case R_MIPS16_TLS_LDM:
df58fc94 8714 case R_MICROMIPS_GOT_HI16:
df58fc94 8715 case R_MICROMIPS_GOT_OFST:
df58fc94
RS
8716 case R_MICROMIPS_TLS_GOTTPREL:
8717 case R_MICROMIPS_TLS_GD:
8718 case R_MICROMIPS_TLS_LDM:
861fb55a
DJ
8719 if (dynobj == NULL)
8720 elf_hash_table (info)->dynobj = dynobj = abfd;
8721 if (!mips_elf_create_got_section (dynobj, info))
8722 return FALSE;
90c14f0c
L
8723 if (htab->root.target_os == is_vxworks
8724 && !bfd_link_pic (info))
b49e97c9 8725 {
4eca0228 8726 _bfd_error_handler
695344c0 8727 /* xgettext:c-format */
2dcf00ce
AM
8728 (_("%pB: GOT reloc at %#" PRIx64 " not expected in executables"),
8729 abfd, (uint64_t) rel->r_offset);
861fb55a
DJ
8730 bfd_set_error (bfd_error_bad_value);
8731 return FALSE;
b49e97c9 8732 }
c5d6fa44 8733 can_make_dynamic_p = TRUE;
861fb55a 8734 break;
b49e97c9 8735
c5d6fa44 8736 case R_MIPS_NONE:
99da6b5f 8737 case R_MIPS_JALR:
df58fc94 8738 case R_MICROMIPS_JALR:
c5d6fa44
RS
8739 /* These relocations have empty fields and are purely there to
8740 provide link information. The symbol value doesn't matter. */
8741 constrain_symbol_p = FALSE;
8742 break;
8743
8744 case R_MIPS_GPREL16:
8745 case R_MIPS_GPREL32:
8746 case R_MIPS16_GPREL:
8747 case R_MICROMIPS_GPREL16:
8748 /* GP-relative relocations always resolve to a definition in a
8749 regular input file, ignoring the one-definition rule. This is
8750 important for the GP setup sequence in NewABI code, which
8751 always resolves to a local function even if other relocations
8752 against the symbol wouldn't. */
8753 constrain_symbol_p = FALSE;
99da6b5f
AN
8754 break;
8755
861fb55a
DJ
8756 case R_MIPS_32:
8757 case R_MIPS_REL32:
8758 case R_MIPS_64:
8759 /* In VxWorks executables, references to external symbols
8760 must be handled using copy relocs or PLT entries; it is not
8761 possible to convert this relocation into a dynamic one.
8762
8763 For executables that use PLTs and copy-relocs, we have a
8764 choice between converting the relocation into a dynamic
8765 one or using copy relocations or PLT entries. It is
8766 usually better to do the former, unless the relocation is
8767 against a read-only section. */
0e1862bb 8768 if ((bfd_link_pic (info)
861fb55a 8769 || (h != NULL
90c14f0c 8770 && htab->root.target_os != is_vxworks
861fb55a
DJ
8771 && strcmp (h->root.root.string, "__gnu_local_gp") != 0
8772 && !(!info->nocopyreloc
8773 && !PIC_OBJECT_P (abfd)
8774 && MIPS_ELF_READONLY_SECTION (sec))))
8775 && (sec->flags & SEC_ALLOC) != 0)
b49e97c9 8776 {
861fb55a 8777 can_make_dynamic_p = TRUE;
b49e97c9
TS
8778 if (dynobj == NULL)
8779 elf_hash_table (info)->dynobj = dynobj = abfd;
861fb55a 8780 }
c5d6fa44 8781 break;
b49e97c9 8782
861fb55a
DJ
8783 case R_MIPS_26:
8784 case R_MIPS_PC16:
7361da2c
AB
8785 case R_MIPS_PC21_S2:
8786 case R_MIPS_PC26_S2:
861fb55a 8787 case R_MIPS16_26:
c9775dde 8788 case R_MIPS16_PC16_S1:
df58fc94
RS
8789 case R_MICROMIPS_26_S1:
8790 case R_MICROMIPS_PC7_S1:
8791 case R_MICROMIPS_PC10_S1:
8792 case R_MICROMIPS_PC16_S1:
8793 case R_MICROMIPS_PC23_S2:
c5d6fa44 8794 call_reloc_p = TRUE;
861fb55a 8795 break;
b49e97c9
TS
8796 }
8797
0a44bf69
RS
8798 if (h)
8799 {
c5d6fa44
RS
8800 if (constrain_symbol_p)
8801 {
8802 if (!can_make_dynamic_p)
8803 ((struct mips_elf_link_hash_entry *) h)->has_static_relocs = 1;
8804
8805 if (!call_reloc_p)
8806 h->pointer_equality_needed = 1;
8807
8808 /* We must not create a stub for a symbol that has
8809 relocations related to taking the function's address.
8810 This doesn't apply to VxWorks, where CALL relocs refer
8811 to a .got.plt entry instead of a normal .got entry. */
90c14f0c
L
8812 if (htab->root.target_os != is_vxworks
8813 && (!can_make_dynamic_p || !call_reloc_p))
c5d6fa44
RS
8814 ((struct mips_elf_link_hash_entry *) h)->no_fn_stub = TRUE;
8815 }
8816
0a44bf69
RS
8817 /* Relocations against the special VxWorks __GOTT_BASE__ and
8818 __GOTT_INDEX__ symbols must be left to the loader. Allocate
8819 room for them in .rela.dyn. */
8820 if (is_gott_symbol (info, h))
8821 {
8822 if (sreloc == NULL)
8823 {
8824 sreloc = mips_elf_rel_dyn_section (info, TRUE);
8825 if (sreloc == NULL)
8826 return FALSE;
8827 }
8828 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
9e3313ae
RS
8829 if (MIPS_ELF_READONLY_SECTION (sec))
8830 /* We tell the dynamic linker that there are
8831 relocations against the text segment. */
8832 info->flags |= DF_TEXTREL;
0a44bf69
RS
8833 }
8834 }
df58fc94
RS
8835 else if (call_lo16_reloc_p (r_type)
8836 || got_lo16_reloc_p (r_type)
8837 || got_disp_reloc_p (r_type)
90c14f0c
L
8838 || (got16_reloc_p (r_type)
8839 && htab->root.target_os == is_vxworks))
b49e97c9
TS
8840 {
8841 /* We may need a local GOT entry for this relocation. We
8842 don't count R_MIPS_GOT_PAGE because we can estimate the
8843 maximum number of pages needed by looking at the size of
738e5348
RS
8844 the segment. Similar comments apply to R_MIPS*_GOT16 and
8845 R_MIPS*_CALL16, except on VxWorks, where GOT relocations
0a44bf69 8846 always evaluate to "G". We don't count R_MIPS_GOT_HI16, or
b49e97c9 8847 R_MIPS_CALL_HI16 because these are always followed by an
b15e6682 8848 R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16. */
a8028dd0 8849 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
e641e783 8850 rel->r_addend, info, r_type))
f4416af6 8851 return FALSE;
b49e97c9
TS
8852 }
8853
8f0c309a
CLT
8854 if (h != NULL
8855 && mips_elf_relocation_needs_la25_stub (abfd, r_type,
8856 ELF_ST_IS_MIPS16 (h->other)))
861fb55a
DJ
8857 ((struct mips_elf_link_hash_entry *) h)->has_nonpic_branches = TRUE;
8858
b49e97c9
TS
8859 switch (r_type)
8860 {
8861 case R_MIPS_CALL16:
738e5348 8862 case R_MIPS16_CALL16:
df58fc94 8863 case R_MICROMIPS_CALL16:
b49e97c9
TS
8864 if (h == NULL)
8865 {
4eca0228 8866 _bfd_error_handler
695344c0 8867 /* xgettext:c-format */
2dcf00ce
AM
8868 (_("%pB: CALL16 reloc at %#" PRIx64 " not against global symbol"),
8869 abfd, (uint64_t) rel->r_offset);
b49e97c9 8870 bfd_set_error (bfd_error_bad_value);
b34976b6 8871 return FALSE;
b49e97c9
TS
8872 }
8873 /* Fall through. */
8874
8875 case R_MIPS_CALL_HI16:
8876 case R_MIPS_CALL_LO16:
df58fc94
RS
8877 case R_MICROMIPS_CALL_HI16:
8878 case R_MICROMIPS_CALL_LO16:
b49e97c9
TS
8879 if (h != NULL)
8880 {
6ccf4795
RS
8881 /* Make sure there is room in the regular GOT to hold the
8882 function's address. We may eliminate it in favour of
8883 a .got.plt entry later; see mips_elf_count_got_symbols. */
e641e783
RS
8884 if (!mips_elf_record_global_got_symbol (h, abfd, info, TRUE,
8885 r_type))
b34976b6 8886 return FALSE;
b49e97c9
TS
8887
8888 /* We need a stub, not a plt entry for the undefined
8889 function. But we record it as if it needs plt. See
c152c796 8890 _bfd_elf_adjust_dynamic_symbol. */
f5385ebf 8891 h->needs_plt = 1;
b49e97c9
TS
8892 h->type = STT_FUNC;
8893 }
8894 break;
8895
0fdc1bf1 8896 case R_MIPS_GOT_PAGE:
df58fc94 8897 case R_MICROMIPS_GOT_PAGE:
738e5348 8898 case R_MIPS16_GOT16:
b49e97c9
TS
8899 case R_MIPS_GOT16:
8900 case R_MIPS_GOT_HI16:
8901 case R_MIPS_GOT_LO16:
df58fc94
RS
8902 case R_MICROMIPS_GOT16:
8903 case R_MICROMIPS_GOT_HI16:
8904 case R_MICROMIPS_GOT_LO16:
8905 if (!h || got_page_reloc_p (r_type))
c224138d 8906 {
3a3b6725
DJ
8907 /* This relocation needs (or may need, if h != NULL) a
8908 page entry in the GOT. For R_MIPS_GOT_PAGE we do not
8909 know for sure until we know whether the symbol is
8910 preemptible. */
c224138d
RS
8911 if (mips_elf_rel_relocation_p (abfd, sec, relocs, rel))
8912 {
8913 if (!mips_elf_get_section_contents (abfd, sec, &contents))
8914 return FALSE;
8915 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
8916 addend = mips_elf_read_rel_addend (abfd, rel,
8917 howto, contents);
9684f078 8918 if (got16_reloc_p (r_type))
c224138d
RS
8919 mips_elf_add_lo16_rel_addend (abfd, rel, rel_end,
8920 contents, &addend);
8921 else
8922 addend <<= howto->rightshift;
8923 }
8924 else
8925 addend = rel->r_addend;
13db6b44
RS
8926 if (!mips_elf_record_got_page_ref (info, abfd, r_symndx,
8927 h, addend))
c224138d 8928 return FALSE;
13db6b44
RS
8929
8930 if (h)
8931 {
8932 struct mips_elf_link_hash_entry *hmips =
8933 (struct mips_elf_link_hash_entry *) h;
8934
8935 /* This symbol is definitely not overridable. */
8936 if (hmips->root.def_regular
0e1862bb 8937 && ! (bfd_link_pic (info) && ! info->symbolic
13db6b44
RS
8938 && ! hmips->root.forced_local))
8939 h = NULL;
8940 }
c224138d 8941 }
13db6b44
RS
8942 /* If this is a global, overridable symbol, GOT_PAGE will
8943 decay to GOT_DISP, so we'll need a GOT entry for it. */
c224138d
RS
8944 /* Fall through. */
8945
b49e97c9 8946 case R_MIPS_GOT_DISP:
df58fc94 8947 case R_MICROMIPS_GOT_DISP:
6ccf4795 8948 if (h && !mips_elf_record_global_got_symbol (h, abfd, info,
e641e783 8949 FALSE, r_type))
b34976b6 8950 return FALSE;
b49e97c9
TS
8951 break;
8952
0f20cc35 8953 case R_MIPS_TLS_GOTTPREL:
d0f13682 8954 case R_MIPS16_TLS_GOTTPREL:
df58fc94 8955 case R_MICROMIPS_TLS_GOTTPREL:
0e1862bb 8956 if (bfd_link_pic (info))
0f20cc35
DJ
8957 info->flags |= DF_STATIC_TLS;
8958 /* Fall through */
8959
8960 case R_MIPS_TLS_LDM:
d0f13682 8961 case R_MIPS16_TLS_LDM:
df58fc94
RS
8962 case R_MICROMIPS_TLS_LDM:
8963 if (tls_ldm_reloc_p (r_type))
0f20cc35 8964 {
cf35638d 8965 r_symndx = STN_UNDEF;
0f20cc35
DJ
8966 h = NULL;
8967 }
8968 /* Fall through */
8969
8970 case R_MIPS_TLS_GD:
d0f13682 8971 case R_MIPS16_TLS_GD:
df58fc94 8972 case R_MICROMIPS_TLS_GD:
0f20cc35
DJ
8973 /* This symbol requires a global offset table entry, or two
8974 for TLS GD relocations. */
e641e783
RS
8975 if (h != NULL)
8976 {
8977 if (!mips_elf_record_global_got_symbol (h, abfd, info,
8978 FALSE, r_type))
8979 return FALSE;
8980 }
8981 else
8982 {
8983 if (!mips_elf_record_local_got_symbol (abfd, r_symndx,
8984 rel->r_addend,
8985 info, r_type))
8986 return FALSE;
8987 }
0f20cc35
DJ
8988 break;
8989
b49e97c9
TS
8990 case R_MIPS_32:
8991 case R_MIPS_REL32:
8992 case R_MIPS_64:
0a44bf69
RS
8993 /* In VxWorks executables, references to external symbols
8994 are handled using copy relocs or PLT stubs, so there's
8995 no need to add a .rela.dyn entry for this relocation. */
861fb55a 8996 if (can_make_dynamic_p)
b49e97c9
TS
8997 {
8998 if (sreloc == NULL)
8999 {
0a44bf69 9000 sreloc = mips_elf_rel_dyn_section (info, TRUE);
b49e97c9 9001 if (sreloc == NULL)
f4416af6 9002 return FALSE;
b49e97c9 9003 }
0e1862bb 9004 if (bfd_link_pic (info) && h == NULL)
82f0cfbd
EC
9005 {
9006 /* When creating a shared object, we must copy these
9007 reloc types into the output file as R_MIPS_REL32
0a44bf69
RS
9008 relocs. Make room for this reloc in .rel(a).dyn. */
9009 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
943284cc 9010 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
9011 /* We tell the dynamic linker that there are
9012 relocations against the text segment. */
9013 info->flags |= DF_TEXTREL;
9014 }
b49e97c9
TS
9015 else
9016 {
9017 struct mips_elf_link_hash_entry *hmips;
82f0cfbd 9018
9a59ad6b
DJ
9019 /* For a shared object, we must copy this relocation
9020 unless the symbol turns out to be undefined and
9021 weak with non-default visibility, in which case
9022 it will be left as zero.
9023
9024 We could elide R_MIPS_REL32 for locally binding symbols
9025 in shared libraries, but do not yet do so.
9026
9027 For an executable, we only need to copy this
9028 reloc if the symbol is defined in a dynamic
9029 object. */
b49e97c9
TS
9030 hmips = (struct mips_elf_link_hash_entry *) h;
9031 ++hmips->possibly_dynamic_relocs;
943284cc 9032 if (MIPS_ELF_READONLY_SECTION (sec))
82f0cfbd
EC
9033 /* We need it to tell the dynamic linker if there
9034 are relocations against the text segment. */
9035 hmips->readonly_reloc = TRUE;
b49e97c9 9036 }
b49e97c9
TS
9037 }
9038
9039 if (SGI_COMPAT (abfd))
9040 mips_elf_hash_table (info)->compact_rel_size +=
9041 sizeof (Elf32_External_crinfo);
9042 break;
9043
9044 case R_MIPS_26:
9045 case R_MIPS_GPREL16:
9046 case R_MIPS_LITERAL:
9047 case R_MIPS_GPREL32:
df58fc94
RS
9048 case R_MICROMIPS_26_S1:
9049 case R_MICROMIPS_GPREL16:
9050 case R_MICROMIPS_LITERAL:
9051 case R_MICROMIPS_GPREL7_S2:
b49e97c9
TS
9052 if (SGI_COMPAT (abfd))
9053 mips_elf_hash_table (info)->compact_rel_size +=
9054 sizeof (Elf32_External_crinfo);
9055 break;
9056
9057 /* This relocation describes the C++ object vtable hierarchy.
9058 Reconstruct it for later use during GC. */
9059 case R_MIPS_GNU_VTINHERIT:
c152c796 9060 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 9061 return FALSE;
b49e97c9
TS
9062 break;
9063
9064 /* This relocation describes which C++ vtable entries are actually
9065 used. Record for later use during GC. */
9066 case R_MIPS_GNU_VTENTRY:
a0ea3a14 9067 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
b34976b6 9068 return FALSE;
b49e97c9
TS
9069 break;
9070
9071 default:
9072 break;
9073 }
9074
1bbce132 9075 /* Record the need for a PLT entry. At this point we don't know
07d6d2b8
AM
9076 yet if we are going to create a PLT in the first place, but
9077 we only record whether the relocation requires a standard MIPS
9078 or a compressed code entry anyway. If we don't make a PLT after
9079 all, then we'll just ignore these arrangements. Likewise if
9080 a PLT entry is not created because the symbol is satisfied
9081 locally. */
1bbce132 9082 if (h != NULL
54806ffa
MR
9083 && (branch_reloc_p (r_type)
9084 || mips16_branch_reloc_p (r_type)
9085 || micromips_branch_reloc_p (r_type))
1bbce132
MR
9086 && !SYMBOL_CALLS_LOCAL (info, h))
9087 {
9088 if (h->plt.plist == NULL)
9089 h->plt.plist = mips_elf_make_plt_record (abfd);
9090 if (h->plt.plist == NULL)
9091 return FALSE;
9092
54806ffa 9093 if (branch_reloc_p (r_type))
1bbce132
MR
9094 h->plt.plist->need_mips = TRUE;
9095 else
9096 h->plt.plist->need_comp = TRUE;
9097 }
9098
738e5348
RS
9099 /* See if this reloc would need to refer to a MIPS16 hard-float stub,
9100 if there is one. We only need to handle global symbols here;
9101 we decide whether to keep or delete stubs for local symbols
9102 when processing the stub's relocations. */
b49e97c9 9103 if (h != NULL
738e5348
RS
9104 && !mips16_call_reloc_p (r_type)
9105 && !section_allows_mips16_refs_p (sec))
b49e97c9
TS
9106 {
9107 struct mips_elf_link_hash_entry *mh;
9108
9109 mh = (struct mips_elf_link_hash_entry *) h;
b34976b6 9110 mh->need_fn_stub = TRUE;
b49e97c9 9111 }
861fb55a
DJ
9112
9113 /* Refuse some position-dependent relocations when creating a
9114 shared library. Do not refuse R_MIPS_32 / R_MIPS_64; they're
9115 not PIC, but we can create dynamic relocations and the result
9116 will be fine. Also do not refuse R_MIPS_LO16, which can be
9117 combined with R_MIPS_GOT16. */
0e1862bb 9118 if (bfd_link_pic (info))
861fb55a
DJ
9119 {
9120 switch (r_type)
9121 {
b474a202
FS
9122 case R_MIPS_TLS_TPREL_HI16:
9123 case R_MIPS16_TLS_TPREL_HI16:
9124 case R_MICROMIPS_TLS_TPREL_HI16:
9125 case R_MIPS_TLS_TPREL_LO16:
9126 case R_MIPS16_TLS_TPREL_LO16:
9127 case R_MICROMIPS_TLS_TPREL_LO16:
9128 /* These are okay in PIE, but not in a shared library. */
9129 if (bfd_link_executable (info))
9130 break;
9131
9132 /* FALLTHROUGH */
9133
861fb55a
DJ
9134 case R_MIPS16_HI16:
9135 case R_MIPS_HI16:
9136 case R_MIPS_HIGHER:
9137 case R_MIPS_HIGHEST:
df58fc94
RS
9138 case R_MICROMIPS_HI16:
9139 case R_MICROMIPS_HIGHER:
9140 case R_MICROMIPS_HIGHEST:
861fb55a
DJ
9141 /* Don't refuse a high part relocation if it's against
9142 no symbol (e.g. part of a compound relocation). */
cf35638d 9143 if (r_symndx == STN_UNDEF)
861fb55a
DJ
9144 break;
9145
3c7687b9 9146 /* Likewise an absolute symbol. */
304f09d0 9147 if (h != NULL && bfd_is_abs_symbol (&h->root))
3c7687b9
MR
9148 break;
9149
861fb55a
DJ
9150 /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
9151 and has a special meaning. */
9152 if (!NEWABI_P (abfd) && h != NULL
9153 && strcmp (h->root.root.string, "_gp_disp") == 0)
9154 break;
9155
0fc1eb3c
RS
9156 /* Likewise __GOTT_BASE__ and __GOTT_INDEX__ on VxWorks. */
9157 if (is_gott_symbol (info, h))
9158 break;
9159
861fb55a
DJ
9160 /* FALLTHROUGH */
9161
9162 case R_MIPS16_26:
9163 case R_MIPS_26:
df58fc94 9164 case R_MICROMIPS_26_S1:
304f09d0
FS
9165 howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
9166 /* An error for unsupported relocations is raised as part
9167 of the above search, so we can skip the following. */
9168 if (howto != NULL)
9169 info->callbacks->einfo
9170 /* xgettext:c-format */
9171 (_("%X%H: relocation %s against `%s' cannot be used"
9172 " when making a shared object; recompile with -fPIC\n"),
9173 abfd, sec, rel->r_offset, howto->name,
9174 (h) ? h->root.root.string : "a local symbol");
aff68bd0 9175 break;
861fb55a
DJ
9176 default:
9177 break;
9178 }
9179 }
b49e97c9
TS
9180 }
9181
b34976b6 9182 return TRUE;
b49e97c9
TS
9183}
9184\f
9a59ad6b
DJ
9185/* Allocate space for global sym dynamic relocs. */
9186
9187static bfd_boolean
9188allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9189{
9190 struct bfd_link_info *info = inf;
9191 bfd *dynobj;
9192 struct mips_elf_link_hash_entry *hmips;
9193 struct mips_elf_link_hash_table *htab;
9194
9195 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9196 BFD_ASSERT (htab != NULL);
9197
9a59ad6b
DJ
9198 dynobj = elf_hash_table (info)->dynobj;
9199 hmips = (struct mips_elf_link_hash_entry *) h;
9200
9201 /* VxWorks executables are handled elsewhere; we only need to
9202 allocate relocations in shared objects. */
90c14f0c 9203 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
9a59ad6b
DJ
9204 return TRUE;
9205
7686d77d
AM
9206 /* Ignore indirect symbols. All relocations against such symbols
9207 will be redirected to the target symbol. */
9208 if (h->root.type == bfd_link_hash_indirect)
63897e2c
RS
9209 return TRUE;
9210
9a59ad6b
DJ
9211 /* If this symbol is defined in a dynamic object, or we are creating
9212 a shared library, we will need to copy any R_MIPS_32 or
9213 R_MIPS_REL32 relocs against it into the output file. */
0e1862bb 9214 if (! bfd_link_relocatable (info)
9a59ad6b
DJ
9215 && hmips->possibly_dynamic_relocs != 0
9216 && (h->root.type == bfd_link_hash_defweak
625ef6dc 9217 || (!h->def_regular && !ELF_COMMON_DEF_P (h))
0e1862bb 9218 || bfd_link_pic (info)))
9a59ad6b
DJ
9219 {
9220 bfd_boolean do_copy = TRUE;
9221
9222 if (h->root.type == bfd_link_hash_undefweak)
9223 {
262e07d0
MR
9224 /* Do not copy relocations for undefined weak symbols that
9225 we are not going to export. */
9226 if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9a59ad6b
DJ
9227 do_copy = FALSE;
9228
9229 /* Make sure undefined weak symbols are output as a dynamic
9230 symbol in PIEs. */
9231 else if (h->dynindx == -1 && !h->forced_local)
9232 {
9233 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9234 return FALSE;
9235 }
9236 }
9237
9238 if (do_copy)
9239 {
aff469fa 9240 /* Even though we don't directly need a GOT entry for this symbol,
f7ff1106
RS
9241 the SVR4 psABI requires it to have a dynamic symbol table
9242 index greater that DT_MIPS_GOTSYM if there are dynamic
9243 relocations against it.
9244
9245 VxWorks does not enforce the same mapping between the GOT
9246 and the symbol table, so the same requirement does not
9247 apply there. */
90c14f0c 9248 if (htab->root.target_os != is_vxworks)
6ccf4795
RS
9249 {
9250 if (hmips->global_got_area > GGA_RELOC_ONLY)
9251 hmips->global_got_area = GGA_RELOC_ONLY;
9252 hmips->got_only_for_calls = FALSE;
9253 }
aff469fa 9254
9a59ad6b
DJ
9255 mips_elf_allocate_dynamic_relocations
9256 (dynobj, info, hmips->possibly_dynamic_relocs);
9257 if (hmips->readonly_reloc)
9258 /* We tell the dynamic linker that there are relocations
9259 against the text segment. */
9260 info->flags |= DF_TEXTREL;
9261 }
9262 }
9263
9264 return TRUE;
9265}
9266
b49e97c9
TS
9267/* Adjust a symbol defined by a dynamic object and referenced by a
9268 regular object. The current definition is in some section of the
9269 dynamic object, but we're not including those sections. We have to
9270 change the definition to something the rest of the link can
9271 understand. */
9272
b34976b6 9273bfd_boolean
9719ad41
RS
9274_bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
9275 struct elf_link_hash_entry *h)
b49e97c9
TS
9276{
9277 bfd *dynobj;
9278 struct mips_elf_link_hash_entry *hmips;
5108fc1b 9279 struct mips_elf_link_hash_table *htab;
5474d94f 9280 asection *s, *srel;
b49e97c9 9281
5108fc1b 9282 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9283 BFD_ASSERT (htab != NULL);
9284
b49e97c9 9285 dynobj = elf_hash_table (info)->dynobj;
861fb55a 9286 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9
TS
9287
9288 /* Make sure we know what is going on here. */
8e4979ac
NC
9289 if (dynobj == NULL
9290 || (! h->needs_plt
9291 && ! h->is_weakalias
9292 && (! h->def_dynamic
9293 || ! h->ref_regular
9294 || h->def_regular)))
9295 {
9296 if (h->type == STT_GNU_IFUNC)
9297 _bfd_error_handler (_("IFUNC symbol %s in dynamic symbol table - IFUNCS are not supported"),
9298 h->root.root.string);
9299 else
9300 _bfd_error_handler (_("non-dynamic symbol %s in dynamic symbol table"),
9301 h->root.root.string);
9302 return TRUE;
9303 }
b49e97c9 9304
b49e97c9 9305 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 9306
861fb55a
DJ
9307 /* If there are call relocations against an externally-defined symbol,
9308 see whether we can create a MIPS lazy-binding stub for it. We can
9309 only do this if all references to the function are through call
9310 relocations, and in that case, the traditional lazy-binding stubs
9311 are much more efficient than PLT entries.
9312
9313 Traditional stubs are only available on SVR4 psABI-based systems;
9314 VxWorks always uses PLTs instead. */
90c14f0c
L
9315 if (htab->root.target_os != is_vxworks
9316 && h->needs_plt
9317 && !hmips->no_fn_stub)
b49e97c9
TS
9318 {
9319 if (! elf_hash_table (info)->dynamic_sections_created)
b34976b6 9320 return TRUE;
b49e97c9
TS
9321
9322 /* If this symbol is not defined in a regular file, then set
9323 the symbol to the stub location. This is required to make
9324 function pointers compare as equal between the normal
9325 executable and the shared library. */
4b8377e7
MR
9326 if (!h->def_regular
9327 && !bfd_is_abs_section (htab->sstubs->output_section))
b49e97c9 9328 {
33bb52fb
RS
9329 hmips->needs_lazy_stub = TRUE;
9330 htab->lazy_stub_count++;
b34976b6 9331 return TRUE;
b49e97c9
TS
9332 }
9333 }
861fb55a
DJ
9334 /* As above, VxWorks requires PLT entries for externally-defined
9335 functions that are only accessed through call relocations.
b49e97c9 9336
861fb55a
DJ
9337 Both VxWorks and non-VxWorks targets also need PLT entries if there
9338 are static-only relocations against an externally-defined function.
9339 This can technically occur for shared libraries if there are
9340 branches to the symbol, although it is unlikely that this will be
9341 used in practice due to the short ranges involved. It can occur
9342 for any relative or absolute relocation in executables; in that
9343 case, the PLT entry becomes the function's canonical address. */
9344 else if (((h->needs_plt && !hmips->no_fn_stub)
9345 || (h->type == STT_FUNC && hmips->has_static_relocs))
9346 && htab->use_plts_and_copy_relocs
9347 && !SYMBOL_CALLS_LOCAL (info, h)
9348 && !(ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
9349 && h->root.type == bfd_link_hash_undefweak))
b49e97c9 9350 {
1bbce132
MR
9351 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9352 bfd_boolean newabi_p = NEWABI_P (info->output_bfd);
9353
9354 /* If this is the first symbol to need a PLT entry, then make some
07d6d2b8
AM
9355 basic setup. Also work out PLT entry sizes. We'll need them
9356 for PLT offset calculations. */
1bbce132 9357 if (htab->plt_mips_offset + htab->plt_comp_offset == 0)
861fb55a 9358 {
ce558b89 9359 BFD_ASSERT (htab->root.sgotplt->size == 0);
1bbce132 9360 BFD_ASSERT (htab->plt_got_index == 0);
0a44bf69 9361
861fb55a
DJ
9362 /* If we're using the PLT additions to the psABI, each PLT
9363 entry is 16 bytes and the PLT0 entry is 32 bytes.
9364 Encourage better cache usage by aligning. We do this
9365 lazily to avoid pessimizing traditional objects. */
90c14f0c 9366 if (htab->root.target_os != is_vxworks
fd361982 9367 && !bfd_set_section_alignment (htab->root.splt, 5))
861fb55a 9368 return FALSE;
0a44bf69 9369
861fb55a
DJ
9370 /* Make sure that .got.plt is word-aligned. We do this lazily
9371 for the same reason as above. */
fd361982 9372 if (!bfd_set_section_alignment (htab->root.sgotplt,
861fb55a
DJ
9373 MIPS_ELF_LOG_FILE_ALIGN (dynobj)))
9374 return FALSE;
0a44bf69 9375
861fb55a
DJ
9376 /* On non-VxWorks targets, the first two entries in .got.plt
9377 are reserved. */
90c14f0c 9378 if (htab->root.target_os != is_vxworks)
1bbce132
MR
9379 htab->plt_got_index
9380 += (get_elf_backend_data (dynobj)->got_header_size
9381 / MIPS_ELF_GOT_SIZE (dynobj));
0a44bf69 9382
861fb55a
DJ
9383 /* On VxWorks, also allocate room for the header's
9384 .rela.plt.unloaded entries. */
90c14f0c
L
9385 if (htab->root.target_os == is_vxworks
9386 && !bfd_link_pic (info))
0a44bf69 9387 htab->srelplt2->size += 2 * sizeof (Elf32_External_Rela);
1bbce132
MR
9388
9389 /* Now work out the sizes of individual PLT entries. */
90c14f0c
L
9390 if (htab->root.target_os == is_vxworks
9391 && bfd_link_pic (info))
1bbce132
MR
9392 htab->plt_mips_entry_size
9393 = 4 * ARRAY_SIZE (mips_vxworks_shared_plt_entry);
90c14f0c 9394 else if (htab->root.target_os == is_vxworks)
1bbce132
MR
9395 htab->plt_mips_entry_size
9396 = 4 * ARRAY_SIZE (mips_vxworks_exec_plt_entry);
9397 else if (newabi_p)
9398 htab->plt_mips_entry_size
9399 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
833794fc 9400 else if (!micromips_p)
1bbce132
MR
9401 {
9402 htab->plt_mips_entry_size
9403 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9404 htab->plt_comp_entry_size
833794fc
MR
9405 = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
9406 }
9407 else if (htab->insn32)
9408 {
9409 htab->plt_mips_entry_size
9410 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9411 htab->plt_comp_entry_size
9412 = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
1bbce132
MR
9413 }
9414 else
9415 {
9416 htab->plt_mips_entry_size
9417 = 4 * ARRAY_SIZE (mips_exec_plt_entry);
9418 htab->plt_comp_entry_size
833794fc 9419 = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
1bbce132 9420 }
0a44bf69
RS
9421 }
9422
1bbce132
MR
9423 if (h->plt.plist == NULL)
9424 h->plt.plist = mips_elf_make_plt_record (dynobj);
9425 if (h->plt.plist == NULL)
9426 return FALSE;
9427
9428 /* There are no defined MIPS16 or microMIPS PLT entries for VxWorks,
07d6d2b8 9429 n32 or n64, so always use a standard entry there.
1bbce132 9430
07d6d2b8
AM
9431 If the symbol has a MIPS16 call stub and gets a PLT entry, then
9432 all MIPS16 calls will go via that stub, and there is no benefit
9433 to having a MIPS16 entry. And in the case of call_stub a
9434 standard entry actually has to be used as the stub ends with a J
9435 instruction. */
1bbce132 9436 if (newabi_p
90c14f0c 9437 || htab->root.target_os == is_vxworks
1bbce132
MR
9438 || hmips->call_stub
9439 || hmips->call_fp_stub)
9440 {
9441 h->plt.plist->need_mips = TRUE;
9442 h->plt.plist->need_comp = FALSE;
9443 }
9444
9445 /* Otherwise, if there are no direct calls to the function, we
07d6d2b8
AM
9446 have a free choice of whether to use standard or compressed
9447 entries. Prefer microMIPS entries if the object is known to
9448 contain microMIPS code, so that it becomes possible to create
9449 pure microMIPS binaries. Prefer standard entries otherwise,
9450 because MIPS16 ones are no smaller and are usually slower. */
1bbce132
MR
9451 if (!h->plt.plist->need_mips && !h->plt.plist->need_comp)
9452 {
9453 if (micromips_p)
9454 h->plt.plist->need_comp = TRUE;
9455 else
9456 h->plt.plist->need_mips = TRUE;
9457 }
9458
9459 if (h->plt.plist->need_mips)
9460 {
9461 h->plt.plist->mips_offset = htab->plt_mips_offset;
9462 htab->plt_mips_offset += htab->plt_mips_entry_size;
9463 }
9464 if (h->plt.plist->need_comp)
9465 {
9466 h->plt.plist->comp_offset = htab->plt_comp_offset;
9467 htab->plt_comp_offset += htab->plt_comp_entry_size;
9468 }
9469
9470 /* Reserve the corresponding .got.plt entry now too. */
9471 h->plt.plist->gotplt_index = htab->plt_got_index++;
0a44bf69
RS
9472
9473 /* If the output file has no definition of the symbol, set the
861fb55a 9474 symbol's value to the address of the stub. */
0e1862bb 9475 if (!bfd_link_pic (info) && !h->def_regular)
1bbce132 9476 hmips->use_plt_entry = TRUE;
0a44bf69 9477
1bbce132 9478 /* Make room for the R_MIPS_JUMP_SLOT relocation. */
90c14f0c 9479 htab->root.srelplt->size += (htab->root.target_os == is_vxworks
ce558b89
AM
9480 ? MIPS_ELF_RELA_SIZE (dynobj)
9481 : MIPS_ELF_REL_SIZE (dynobj));
0a44bf69
RS
9482
9483 /* Make room for the .rela.plt.unloaded relocations. */
90c14f0c 9484 if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
0a44bf69
RS
9485 htab->srelplt2->size += 3 * sizeof (Elf32_External_Rela);
9486
861fb55a
DJ
9487 /* All relocations against this symbol that could have been made
9488 dynamic will now refer to the PLT entry instead. */
9489 hmips->possibly_dynamic_relocs = 0;
0a44bf69 9490
0a44bf69
RS
9491 return TRUE;
9492 }
9493
9494 /* If this is a weak symbol, and there is a real definition, the
9495 processor independent code will have arranged for us to see the
9496 real definition first, and we can just use the same value. */
60d67dc8 9497 if (h->is_weakalias)
0a44bf69 9498 {
60d67dc8
AM
9499 struct elf_link_hash_entry *def = weakdef (h);
9500 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
9501 h->root.u.def.section = def->root.u.def.section;
9502 h->root.u.def.value = def->root.u.def.value;
0a44bf69
RS
9503 return TRUE;
9504 }
9505
861fb55a
DJ
9506 /* Otherwise, there is nothing further to do for symbols defined
9507 in regular objects. */
9508 if (h->def_regular)
0a44bf69
RS
9509 return TRUE;
9510
861fb55a
DJ
9511 /* There's also nothing more to do if we'll convert all relocations
9512 against this symbol into dynamic relocations. */
9513 if (!hmips->has_static_relocs)
9514 return TRUE;
9515
9516 /* We're now relying on copy relocations. Complain if we have
9517 some that we can't convert. */
0e1862bb 9518 if (!htab->use_plts_and_copy_relocs || bfd_link_pic (info))
861fb55a 9519 {
4eca0228
AM
9520 _bfd_error_handler (_("non-dynamic relocations refer to "
9521 "dynamic symbol %s"),
9522 h->root.root.string);
861fb55a
DJ
9523 bfd_set_error (bfd_error_bad_value);
9524 return FALSE;
9525 }
9526
0a44bf69
RS
9527 /* We must allocate the symbol in our .dynbss section, which will
9528 become part of the .bss section of the executable. There will be
9529 an entry for this symbol in the .dynsym section. The dynamic
9530 object will contain position independent code, so all references
9531 from the dynamic object to this symbol will go through the global
9532 offset table. The dynamic linker will use the .dynsym entry to
9533 determine the address it must put in the global offset table, so
9534 both the dynamic object and the regular object will refer to the
9535 same memory location for the variable. */
9536
5474d94f
AM
9537 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
9538 {
9539 s = htab->root.sdynrelro;
9540 srel = htab->root.sreldynrelro;
9541 }
9542 else
9543 {
9544 s = htab->root.sdynbss;
9545 srel = htab->root.srelbss;
9546 }
0a44bf69
RS
9547 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
9548 {
90c14f0c 9549 if (htab->root.target_os == is_vxworks)
5474d94f 9550 srel->size += sizeof (Elf32_External_Rela);
861fb55a
DJ
9551 else
9552 mips_elf_allocate_dynamic_relocations (dynobj, info, 1);
0a44bf69
RS
9553 h->needs_copy = 1;
9554 }
9555
861fb55a
DJ
9556 /* All relocations against this symbol that could have been made
9557 dynamic will now refer to the local copy instead. */
9558 hmips->possibly_dynamic_relocs = 0;
9559
5474d94f 9560 return _bfd_elf_adjust_dynamic_copy (info, h, s);
0a44bf69 9561}
b49e97c9
TS
9562\f
9563/* This function is called after all the input files have been read,
9564 and the input sections have been assigned to output sections. We
9565 check for any mips16 stub sections that we can discard. */
9566
b34976b6 9567bfd_boolean
9719ad41
RS
9568_bfd_mips_elf_always_size_sections (bfd *output_bfd,
9569 struct bfd_link_info *info)
b49e97c9 9570{
351cdf24 9571 asection *sect;
0a44bf69 9572 struct mips_elf_link_hash_table *htab;
861fb55a 9573 struct mips_htab_traverse_info hti;
0a44bf69
RS
9574
9575 htab = mips_elf_hash_table (info);
4dfe6ac6 9576 BFD_ASSERT (htab != NULL);
f4416af6 9577
b49e97c9 9578 /* The .reginfo section has a fixed size. */
351cdf24
MF
9579 sect = bfd_get_section_by_name (output_bfd, ".reginfo");
9580 if (sect != NULL)
6798f8bf 9581 {
fd361982 9582 bfd_set_section_size (sect, sizeof (Elf32_External_RegInfo));
6798f8bf
MR
9583 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9584 }
351cdf24
MF
9585
9586 /* The .MIPS.abiflags section has a fixed size. */
9587 sect = bfd_get_section_by_name (output_bfd, ".MIPS.abiflags");
9588 if (sect != NULL)
6798f8bf 9589 {
fd361982 9590 bfd_set_section_size (sect, sizeof (Elf_External_ABIFlags_v0));
6798f8bf
MR
9591 sect->flags |= SEC_FIXED_SIZE | SEC_HAS_CONTENTS;
9592 }
b49e97c9 9593
861fb55a
DJ
9594 hti.info = info;
9595 hti.output_bfd = output_bfd;
9596 hti.error = FALSE;
9597 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9598 mips_elf_check_symbols, &hti);
9599 if (hti.error)
9600 return FALSE;
f4416af6 9601
33bb52fb
RS
9602 return TRUE;
9603}
9604
9605/* If the link uses a GOT, lay it out and work out its size. */
9606
9607static bfd_boolean
9608mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
9609{
9610 bfd *dynobj;
9611 asection *s;
9612 struct mips_got_info *g;
33bb52fb
RS
9613 bfd_size_type loadable_size = 0;
9614 bfd_size_type page_gotno;
d7206569 9615 bfd *ibfd;
ab361d49 9616 struct mips_elf_traverse_got_arg tga;
33bb52fb
RS
9617 struct mips_elf_link_hash_table *htab;
9618
9619 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9620 BFD_ASSERT (htab != NULL);
9621
ce558b89 9622 s = htab->root.sgot;
f4416af6 9623 if (s == NULL)
b34976b6 9624 return TRUE;
b49e97c9 9625
33bb52fb 9626 dynobj = elf_hash_table (info)->dynobj;
a8028dd0
RS
9627 g = htab->got_info;
9628
861fb55a
DJ
9629 /* Allocate room for the reserved entries. VxWorks always reserves
9630 3 entries; other objects only reserve 2 entries. */
cb22ccf4 9631 BFD_ASSERT (g->assigned_low_gotno == 0);
90c14f0c 9632 if (htab->root.target_os == is_vxworks)
861fb55a
DJ
9633 htab->reserved_gotno = 3;
9634 else
9635 htab->reserved_gotno = 2;
9636 g->local_gotno += htab->reserved_gotno;
cb22ccf4 9637 g->assigned_low_gotno = htab->reserved_gotno;
861fb55a 9638
6c42ddb9
RS
9639 /* Decide which symbols need to go in the global part of the GOT and
9640 count the number of reloc-only GOT symbols. */
020d7251 9641 mips_elf_link_hash_traverse (htab, mips_elf_count_got_symbols, info);
f4416af6 9642
13db6b44
RS
9643 if (!mips_elf_resolve_final_got_entries (info, g))
9644 return FALSE;
9645
33bb52fb
RS
9646 /* Calculate the total loadable size of the output. That
9647 will give us the maximum number of GOT_PAGE entries
9648 required. */
c72f2fb2 9649 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
33bb52fb
RS
9650 {
9651 asection *subsection;
5108fc1b 9652
d7206569 9653 for (subsection = ibfd->sections;
33bb52fb
RS
9654 subsection;
9655 subsection = subsection->next)
9656 {
9657 if ((subsection->flags & SEC_ALLOC) == 0)
9658 continue;
9659 loadable_size += ((subsection->size + 0xf)
9660 &~ (bfd_size_type) 0xf);
9661 }
9662 }
f4416af6 9663
90c14f0c 9664 if (htab->root.target_os == is_vxworks)
738e5348 9665 /* There's no need to allocate page entries for VxWorks; R_MIPS*_GOT16
0a44bf69
RS
9666 relocations against local symbols evaluate to "G", and the EABI does
9667 not include R_MIPS_GOT_PAGE. */
c224138d 9668 page_gotno = 0;
0a44bf69
RS
9669 else
9670 /* Assume there are two loadable segments consisting of contiguous
9671 sections. Is 5 enough? */
c224138d
RS
9672 page_gotno = (loadable_size >> 16) + 5;
9673
13db6b44 9674 /* Choose the smaller of the two page estimates; both are intended to be
c224138d
RS
9675 conservative. */
9676 if (page_gotno > g->page_gotno)
9677 page_gotno = g->page_gotno;
f4416af6 9678
c224138d 9679 g->local_gotno += page_gotno;
cb22ccf4 9680 g->assigned_high_gotno = g->local_gotno - 1;
ab361d49 9681
ab361d49
RS
9682 s->size += g->local_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9683 s->size += g->global_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
0f20cc35
DJ
9684 s->size += g->tls_gotno * MIPS_ELF_GOT_SIZE (output_bfd);
9685
0a44bf69
RS
9686 /* VxWorks does not support multiple GOTs. It initializes $gp to
9687 __GOTT_BASE__[__GOTT_INDEX__], the value of which is set by the
9688 dynamic loader. */
90c14f0c
L
9689 if (htab->root.target_os != is_vxworks
9690 && s->size > MIPS_ELF_GOT_MAX_SIZE (info))
0f20cc35 9691 {
a8028dd0 9692 if (!mips_elf_multi_got (output_bfd, info, s, page_gotno))
0f20cc35
DJ
9693 return FALSE;
9694 }
9695 else
9696 {
d7206569
RS
9697 /* Record that all bfds use G. This also has the effect of freeing
9698 the per-bfd GOTs, which we no longer need. */
c72f2fb2 9699 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
d7206569
RS
9700 if (mips_elf_bfd_got (ibfd, FALSE))
9701 mips_elf_replace_bfd_got (ibfd, g);
9702 mips_elf_replace_bfd_got (output_bfd, g);
9703
33bb52fb 9704 /* Set up TLS entries. */
0f20cc35 9705 g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
72e7511a
RS
9706 tga.info = info;
9707 tga.g = g;
9708 tga.value = MIPS_ELF_GOT_SIZE (output_bfd);
9709 htab_traverse (g->got_entries, mips_elf_initialize_tls_index, &tga);
9710 if (!tga.g)
9711 return FALSE;
1fd20d70
RS
9712 BFD_ASSERT (g->tls_assigned_gotno
9713 == g->global_gotno + g->local_gotno + g->tls_gotno);
33bb52fb 9714
57093f5e 9715 /* Each VxWorks GOT entry needs an explicit relocation. */
90c14f0c 9716 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
57093f5e
RS
9717 g->relocs += g->global_gotno + g->local_gotno - htab->reserved_gotno;
9718
33bb52fb 9719 /* Allocate room for the TLS relocations. */
ab361d49
RS
9720 if (g->relocs)
9721 mips_elf_allocate_dynamic_relocations (dynobj, info, g->relocs);
0f20cc35 9722 }
b49e97c9 9723
b34976b6 9724 return TRUE;
b49e97c9
TS
9725}
9726
33bb52fb
RS
9727/* Estimate the size of the .MIPS.stubs section. */
9728
9729static void
9730mips_elf_estimate_stub_size (bfd *output_bfd, struct bfd_link_info *info)
9731{
9732 struct mips_elf_link_hash_table *htab;
9733 bfd_size_type dynsymcount;
9734
9735 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9736 BFD_ASSERT (htab != NULL);
9737
33bb52fb
RS
9738 if (htab->lazy_stub_count == 0)
9739 return;
9740
9741 /* IRIX rld assumes that a function stub isn't at the end of the .text
9742 section, so add a dummy entry to the end. */
9743 htab->lazy_stub_count++;
9744
9745 /* Get a worst-case estimate of the number of dynamic symbols needed.
9746 At this point, dynsymcount does not account for section symbols
9747 and count_section_dynsyms may overestimate the number that will
9748 be needed. */
9749 dynsymcount = (elf_hash_table (info)->dynsymcount
9750 + count_section_dynsyms (output_bfd, info));
9751
1bbce132
MR
9752 /* Determine the size of one stub entry. There's no disadvantage
9753 from using microMIPS code here, so for the sake of pure-microMIPS
9754 binaries we prefer it whenever there's any microMIPS code in
9755 output produced at all. This has a benefit of stubs being
833794fc
MR
9756 shorter by 4 bytes each too, unless in the insn32 mode. */
9757 if (!MICROMIPS_P (output_bfd))
1bbce132
MR
9758 htab->function_stub_size = (dynsymcount > 0x10000
9759 ? MIPS_FUNCTION_STUB_BIG_SIZE
9760 : MIPS_FUNCTION_STUB_NORMAL_SIZE);
833794fc
MR
9761 else if (htab->insn32)
9762 htab->function_stub_size = (dynsymcount > 0x10000
9763 ? MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE
9764 : MICROMIPS_INSN32_FUNCTION_STUB_NORMAL_SIZE);
9765 else
9766 htab->function_stub_size = (dynsymcount > 0x10000
9767 ? MICROMIPS_FUNCTION_STUB_BIG_SIZE
9768 : MICROMIPS_FUNCTION_STUB_NORMAL_SIZE);
33bb52fb
RS
9769
9770 htab->sstubs->size = htab->lazy_stub_count * htab->function_stub_size;
9771}
9772
1bbce132
MR
9773/* A mips_elf_link_hash_traverse callback for which DATA points to a
9774 mips_htab_traverse_info. If H needs a traditional MIPS lazy-binding
9775 stub, allocate an entry in the stubs section. */
33bb52fb
RS
9776
9777static bfd_boolean
af924177 9778mips_elf_allocate_lazy_stub (struct mips_elf_link_hash_entry *h, void *data)
33bb52fb 9779{
1bbce132 9780 struct mips_htab_traverse_info *hti = data;
33bb52fb 9781 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9782 struct bfd_link_info *info;
9783 bfd *output_bfd;
9784
9785 info = hti->info;
9786 output_bfd = hti->output_bfd;
9787 htab = mips_elf_hash_table (info);
9788 BFD_ASSERT (htab != NULL);
33bb52fb 9789
33bb52fb
RS
9790 if (h->needs_lazy_stub)
9791 {
1bbce132
MR
9792 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9793 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9794 bfd_vma isa_bit = micromips_p;
9795
9796 BFD_ASSERT (htab->root.dynobj != NULL);
9797 if (h->root.plt.plist == NULL)
9798 h->root.plt.plist = mips_elf_make_plt_record (htab->sstubs->owner);
9799 if (h->root.plt.plist == NULL)
9800 {
9801 hti->error = TRUE;
9802 return FALSE;
9803 }
33bb52fb 9804 h->root.root.u.def.section = htab->sstubs;
1bbce132
MR
9805 h->root.root.u.def.value = htab->sstubs->size + isa_bit;
9806 h->root.plt.plist->stub_offset = htab->sstubs->size;
9807 h->root.other = other;
33bb52fb
RS
9808 htab->sstubs->size += htab->function_stub_size;
9809 }
9810 return TRUE;
9811}
9812
9813/* Allocate offsets in the stubs section to each symbol that needs one.
9814 Set the final size of the .MIPS.stub section. */
9815
1bbce132 9816static bfd_boolean
33bb52fb
RS
9817mips_elf_lay_out_lazy_stubs (struct bfd_link_info *info)
9818{
1bbce132
MR
9819 bfd *output_bfd = info->output_bfd;
9820 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
9821 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9822 bfd_vma isa_bit = micromips_p;
33bb52fb 9823 struct mips_elf_link_hash_table *htab;
1bbce132
MR
9824 struct mips_htab_traverse_info hti;
9825 struct elf_link_hash_entry *h;
9826 bfd *dynobj;
33bb52fb
RS
9827
9828 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
9829 BFD_ASSERT (htab != NULL);
9830
33bb52fb 9831 if (htab->lazy_stub_count == 0)
1bbce132 9832 return TRUE;
33bb52fb
RS
9833
9834 htab->sstubs->size = 0;
1bbce132
MR
9835 hti.info = info;
9836 hti.output_bfd = output_bfd;
9837 hti.error = FALSE;
9838 mips_elf_link_hash_traverse (htab, mips_elf_allocate_lazy_stub, &hti);
9839 if (hti.error)
9840 return FALSE;
33bb52fb
RS
9841 htab->sstubs->size += htab->function_stub_size;
9842 BFD_ASSERT (htab->sstubs->size
9843 == htab->lazy_stub_count * htab->function_stub_size);
1bbce132
MR
9844
9845 dynobj = elf_hash_table (info)->dynobj;
9846 BFD_ASSERT (dynobj != NULL);
9847 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->sstubs, "_MIPS_STUBS_");
9848 if (h == NULL)
9849 return FALSE;
9850 h->root.u.def.value = isa_bit;
9851 h->other = other;
9852 h->type = STT_FUNC;
9853
9854 return TRUE;
9855}
9856
9857/* A mips_elf_link_hash_traverse callback for which DATA points to a
9858 bfd_link_info. If H uses the address of a PLT entry as the value
9859 of the symbol, then set the entry in the symbol table now. Prefer
9860 a standard MIPS PLT entry. */
9861
9862static bfd_boolean
9863mips_elf_set_plt_sym_value (struct mips_elf_link_hash_entry *h, void *data)
9864{
9865 struct bfd_link_info *info = data;
9866 bfd_boolean micromips_p = MICROMIPS_P (info->output_bfd);
9867 struct mips_elf_link_hash_table *htab;
9868 unsigned int other;
9869 bfd_vma isa_bit;
9870 bfd_vma val;
9871
9872 htab = mips_elf_hash_table (info);
9873 BFD_ASSERT (htab != NULL);
9874
9875 if (h->use_plt_entry)
9876 {
9877 BFD_ASSERT (h->root.plt.plist != NULL);
9878 BFD_ASSERT (h->root.plt.plist->mips_offset != MINUS_ONE
9879 || h->root.plt.plist->comp_offset != MINUS_ONE);
9880
9881 val = htab->plt_header_size;
9882 if (h->root.plt.plist->mips_offset != MINUS_ONE)
9883 {
9884 isa_bit = 0;
9885 val += h->root.plt.plist->mips_offset;
9886 other = 0;
9887 }
9888 else
9889 {
9890 isa_bit = 1;
9891 val += htab->plt_mips_offset + h->root.plt.plist->comp_offset;
9892 other = micromips_p ? STO_MICROMIPS : STO_MIPS16;
9893 }
9894 val += isa_bit;
9895 /* For VxWorks, point at the PLT load stub rather than the lazy
07d6d2b8
AM
9896 resolution stub; this stub will become the canonical function
9897 address. */
90c14f0c 9898 if (htab->root.target_os == is_vxworks)
1bbce132
MR
9899 val += 8;
9900
ce558b89 9901 h->root.root.u.def.section = htab->root.splt;
1bbce132
MR
9902 h->root.root.u.def.value = val;
9903 h->root.other = other;
9904 }
9905
9906 return TRUE;
33bb52fb
RS
9907}
9908
b49e97c9
TS
9909/* Set the sizes of the dynamic sections. */
9910
b34976b6 9911bfd_boolean
9719ad41
RS
9912_bfd_mips_elf_size_dynamic_sections (bfd *output_bfd,
9913 struct bfd_link_info *info)
b49e97c9
TS
9914{
9915 bfd *dynobj;
861fb55a 9916 asection *s, *sreldyn;
b34976b6 9917 bfd_boolean reltext;
0a44bf69 9918 struct mips_elf_link_hash_table *htab;
b49e97c9 9919
0a44bf69 9920 htab = mips_elf_hash_table (info);
4dfe6ac6 9921 BFD_ASSERT (htab != NULL);
b49e97c9
TS
9922 dynobj = elf_hash_table (info)->dynobj;
9923 BFD_ASSERT (dynobj != NULL);
9924
9925 if (elf_hash_table (info)->dynamic_sections_created)
9926 {
9927 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 9928 if (bfd_link_executable (info) && !info->nointerp)
b49e97c9 9929 {
3d4d4302 9930 s = bfd_get_linker_section (dynobj, ".interp");
b49e97c9 9931 BFD_ASSERT (s != NULL);
eea6121a 9932 s->size
b49e97c9
TS
9933 = strlen (ELF_DYNAMIC_INTERPRETER (output_bfd)) + 1;
9934 s->contents
9935 = (bfd_byte *) ELF_DYNAMIC_INTERPRETER (output_bfd);
9936 }
861fb55a 9937
1bbce132 9938 /* Figure out the size of the PLT header if we know that we
07d6d2b8
AM
9939 are using it. For the sake of cache alignment always use
9940 a standard header whenever any standard entries are present
9941 even if microMIPS entries are present as well. This also
9942 lets the microMIPS header rely on the value of $v0 only set
9943 by microMIPS entries, for a small size reduction.
1bbce132 9944
07d6d2b8
AM
9945 Set symbol table entry values for symbols that use the
9946 address of their PLT entry now that we can calculate it.
1bbce132 9947
07d6d2b8
AM
9948 Also create the _PROCEDURE_LINKAGE_TABLE_ symbol if we
9949 haven't already in _bfd_elf_create_dynamic_sections. */
ce558b89 9950 if (htab->root.splt && htab->plt_mips_offset + htab->plt_comp_offset != 0)
861fb55a 9951 {
1bbce132
MR
9952 bfd_boolean micromips_p = (MICROMIPS_P (output_bfd)
9953 && !htab->plt_mips_offset);
9954 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
9955 bfd_vma isa_bit = micromips_p;
861fb55a 9956 struct elf_link_hash_entry *h;
1bbce132 9957 bfd_vma size;
861fb55a
DJ
9958
9959 BFD_ASSERT (htab->use_plts_and_copy_relocs);
ce558b89
AM
9960 BFD_ASSERT (htab->root.sgotplt->size == 0);
9961 BFD_ASSERT (htab->root.splt->size == 0);
1bbce132 9962
90c14f0c 9963 if (htab->root.target_os == is_vxworks && bfd_link_pic (info))
1bbce132 9964 size = 4 * ARRAY_SIZE (mips_vxworks_shared_plt0_entry);
90c14f0c 9965 else if (htab->root.target_os == is_vxworks)
1bbce132
MR
9966 size = 4 * ARRAY_SIZE (mips_vxworks_exec_plt0_entry);
9967 else if (ABI_64_P (output_bfd))
9968 size = 4 * ARRAY_SIZE (mips_n64_exec_plt0_entry);
9969 else if (ABI_N32_P (output_bfd))
9970 size = 4 * ARRAY_SIZE (mips_n32_exec_plt0_entry);
9971 else if (!micromips_p)
9972 size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
833794fc
MR
9973 else if (htab->insn32)
9974 size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
1bbce132
MR
9975 else
9976 size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
861fb55a 9977
1bbce132
MR
9978 htab->plt_header_is_comp = micromips_p;
9979 htab->plt_header_size = size;
ce558b89
AM
9980 htab->root.splt->size = (size
9981 + htab->plt_mips_offset
9982 + htab->plt_comp_offset);
9983 htab->root.sgotplt->size = (htab->plt_got_index
9984 * MIPS_ELF_GOT_SIZE (dynobj));
1bbce132
MR
9985
9986 mips_elf_link_hash_traverse (htab, mips_elf_set_plt_sym_value, info);
9987
9988 if (htab->root.hplt == NULL)
9989 {
ce558b89 9990 h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.splt,
1bbce132
MR
9991 "_PROCEDURE_LINKAGE_TABLE_");
9992 htab->root.hplt = h;
9993 if (h == NULL)
9994 return FALSE;
9995 }
9996
9997 h = htab->root.hplt;
9998 h->root.u.def.value = isa_bit;
9999 h->other = other;
861fb55a
DJ
10000 h->type = STT_FUNC;
10001 }
10002 }
4e41d0d7 10003
9a59ad6b 10004 /* Allocate space for global sym dynamic relocs. */
2c3fc389 10005 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
9a59ad6b 10006
33bb52fb
RS
10007 mips_elf_estimate_stub_size (output_bfd, info);
10008
10009 if (!mips_elf_lay_out_got (output_bfd, info))
10010 return FALSE;
10011
10012 mips_elf_lay_out_lazy_stubs (info);
10013
b49e97c9
TS
10014 /* The check_relocs and adjust_dynamic_symbol entry points have
10015 determined the sizes of the various dynamic sections. Allocate
10016 memory for them. */
b34976b6 10017 reltext = FALSE;
b49e97c9
TS
10018 for (s = dynobj->sections; s != NULL; s = s->next)
10019 {
10020 const char *name;
b49e97c9
TS
10021
10022 /* It's OK to base decisions on the section name, because none
10023 of the dynobj section names depend upon the input files. */
fd361982 10024 name = bfd_section_name (s);
b49e97c9
TS
10025
10026 if ((s->flags & SEC_LINKER_CREATED) == 0)
10027 continue;
10028
0112cd26 10029 if (CONST_STRNEQ (name, ".rel"))
b49e97c9 10030 {
c456f082 10031 if (s->size != 0)
b49e97c9
TS
10032 {
10033 const char *outname;
10034 asection *target;
10035
10036 /* If this relocation section applies to a read only
07d6d2b8
AM
10037 section, then we probably need a DT_TEXTREL entry.
10038 If the relocation section is .rel(a).dyn, we always
10039 assert a DT_TEXTREL entry rather than testing whether
10040 there exists a relocation to a read only section or
10041 not. */
fd361982 10042 outname = bfd_section_name (s->output_section);
b49e97c9
TS
10043 target = bfd_get_section_by_name (output_bfd, outname + 4);
10044 if ((target != NULL
10045 && (target->flags & SEC_READONLY) != 0
10046 && (target->flags & SEC_ALLOC) != 0)
0a44bf69 10047 || strcmp (outname, MIPS_ELF_REL_DYN_NAME (info)) == 0)
b34976b6 10048 reltext = TRUE;
b49e97c9
TS
10049
10050 /* We use the reloc_count field as a counter if we need
10051 to copy relocs into the output file. */
0a44bf69 10052 if (strcmp (name, MIPS_ELF_REL_DYN_NAME (info)) != 0)
b49e97c9 10053 s->reloc_count = 0;
f4416af6
AO
10054
10055 /* If combreloc is enabled, elf_link_sort_relocs() will
10056 sort relocations, but in a different way than we do,
10057 and before we're done creating relocations. Also, it
10058 will move them around between input sections'
10059 relocation's contents, so our sorting would be
10060 broken, so don't let it run. */
10061 info->combreloc = 0;
b49e97c9
TS
10062 }
10063 }
0e1862bb 10064 else if (bfd_link_executable (info)
b49e97c9 10065 && ! mips_elf_hash_table (info)->use_rld_obj_head
0112cd26 10066 && CONST_STRNEQ (name, ".rld_map"))
b49e97c9 10067 {
5108fc1b 10068 /* We add a room for __rld_map. It will be filled in by the
b49e97c9 10069 rtld to contain a pointer to the _r_debug structure. */
b4082c70 10070 s->size += MIPS_ELF_RLD_MAP_SIZE (output_bfd);
b49e97c9
TS
10071 }
10072 else if (SGI_COMPAT (output_bfd)
0112cd26 10073 && CONST_STRNEQ (name, ".compact_rel"))
eea6121a 10074 s->size += mips_elf_hash_table (info)->compact_rel_size;
ce558b89 10075 else if (s == htab->root.splt)
861fb55a
DJ
10076 {
10077 /* If the last PLT entry has a branch delay slot, allocate
6d30f5b2
NC
10078 room for an extra nop to fill the delay slot. This is
10079 for CPUs without load interlocking. */
10080 if (! LOAD_INTERLOCKS_P (output_bfd)
90c14f0c
L
10081 && htab->root.target_os != is_vxworks
10082 && s->size > 0)
861fb55a
DJ
10083 s->size += 4;
10084 }
0112cd26 10085 else if (! CONST_STRNEQ (name, ".init")
ce558b89
AM
10086 && s != htab->root.sgot
10087 && s != htab->root.sgotplt
861fb55a 10088 && s != htab->sstubs
5474d94f
AM
10089 && s != htab->root.sdynbss
10090 && s != htab->root.sdynrelro)
b49e97c9
TS
10091 {
10092 /* It's not one of our sections, so don't allocate space. */
10093 continue;
10094 }
10095
c456f082 10096 if (s->size == 0)
b49e97c9 10097 {
8423293d 10098 s->flags |= SEC_EXCLUDE;
b49e97c9
TS
10099 continue;
10100 }
10101
c456f082
AM
10102 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10103 continue;
10104
b49e97c9 10105 /* Allocate memory for the section contents. */
eea6121a 10106 s->contents = bfd_zalloc (dynobj, s->size);
c456f082 10107 if (s->contents == NULL)
b49e97c9
TS
10108 {
10109 bfd_set_error (bfd_error_no_memory);
b34976b6 10110 return FALSE;
b49e97c9
TS
10111 }
10112 }
10113
10114 if (elf_hash_table (info)->dynamic_sections_created)
10115 {
10116 /* Add some entries to the .dynamic section. We fill in the
10117 values later, in _bfd_mips_elf_finish_dynamic_sections, but we
10118 must add the entries now so that we get the correct size for
5750dcec 10119 the .dynamic section. */
af5978fb
RS
10120
10121 /* SGI object has the equivalence of DT_DEBUG in the
5750dcec 10122 DT_MIPS_RLD_MAP entry. This must come first because glibc
6e6be592
MR
10123 only fills in DT_MIPS_RLD_MAP (not DT_DEBUG) and some tools
10124 may only look at the first one they see. */
0e1862bb 10125 if (!bfd_link_pic (info)
af5978fb
RS
10126 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP, 0))
10127 return FALSE;
b49e97c9 10128
0e1862bb 10129 if (bfd_link_executable (info)
a5499fa4
MF
10130 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_MAP_REL, 0))
10131 return FALSE;
10132
5750dcec
DJ
10133 /* The DT_DEBUG entry may be filled in by the dynamic linker and
10134 used by the debugger. */
0e1862bb 10135 if (bfd_link_executable (info)
5750dcec
DJ
10136 && !SGI_COMPAT (output_bfd)
10137 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_DEBUG, 0))
10138 return FALSE;
10139
90c14f0c
L
10140 if (reltext
10141 && (SGI_COMPAT (output_bfd)
10142 || htab->root.target_os == is_vxworks))
b49e97c9
TS
10143 info->flags |= DF_TEXTREL;
10144
10145 if ((info->flags & DF_TEXTREL) != 0)
10146 {
10147 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_TEXTREL, 0))
b34976b6 10148 return FALSE;
943284cc
DJ
10149
10150 /* Clear the DF_TEXTREL flag. It will be set again if we
10151 write out an actual text relocation; we may not, because
10152 at this point we do not know whether e.g. any .eh_frame
10153 absolute relocations have been converted to PC-relative. */
10154 info->flags &= ~DF_TEXTREL;
b49e97c9
TS
10155 }
10156
10157 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTGOT, 0))
b34976b6 10158 return FALSE;
b49e97c9 10159
861fb55a 10160 sreldyn = mips_elf_rel_dyn_section (info, FALSE);
90c14f0c 10161 if (htab->root.target_os == is_vxworks)
b49e97c9 10162 {
0a44bf69
RS
10163 /* VxWorks uses .rela.dyn instead of .rel.dyn. It does not
10164 use any of the DT_MIPS_* tags. */
861fb55a 10165 if (sreldyn && sreldyn->size > 0)
0a44bf69
RS
10166 {
10167 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELA, 0))
10168 return FALSE;
b49e97c9 10169
0a44bf69
RS
10170 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELASZ, 0))
10171 return FALSE;
b49e97c9 10172
0a44bf69
RS
10173 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELAENT, 0))
10174 return FALSE;
10175 }
b49e97c9 10176 }
0a44bf69
RS
10177 else
10178 {
db841b6f
MR
10179 if (sreldyn && sreldyn->size > 0
10180 && !bfd_is_abs_section (sreldyn->output_section))
0a44bf69
RS
10181 {
10182 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_REL, 0))
10183 return FALSE;
b49e97c9 10184
0a44bf69
RS
10185 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELSZ, 0))
10186 return FALSE;
b49e97c9 10187
0a44bf69
RS
10188 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_RELENT, 0))
10189 return FALSE;
10190 }
b49e97c9 10191
0a44bf69
RS
10192 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_RLD_VERSION, 0))
10193 return FALSE;
b49e97c9 10194
0a44bf69
RS
10195 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_FLAGS, 0))
10196 return FALSE;
b49e97c9 10197
0a44bf69
RS
10198 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_BASE_ADDRESS, 0))
10199 return FALSE;
b49e97c9 10200
0a44bf69
RS
10201 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_LOCAL_GOTNO, 0))
10202 return FALSE;
b49e97c9 10203
0a44bf69
RS
10204 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_SYMTABNO, 0))
10205 return FALSE;
b49e97c9 10206
0a44bf69
RS
10207 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_UNREFEXTNO, 0))
10208 return FALSE;
b49e97c9 10209
0a44bf69
RS
10210 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_GOTSYM, 0))
10211 return FALSE;
10212
f16a9783
MS
10213 if (info->emit_gnu_hash
10214 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_XHASH, 0))
10215 return FALSE;
10216
0a44bf69
RS
10217 if (IRIX_COMPAT (dynobj) == ict_irix5
10218 && ! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_HIPAGENO, 0))
10219 return FALSE;
10220
10221 if (IRIX_COMPAT (dynobj) == ict_irix6
10222 && (bfd_get_section_by_name
af0edeb8 10223 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (dynobj)))
0a44bf69
RS
10224 && !MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_OPTIONS, 0))
10225 return FALSE;
10226 }
ce558b89 10227 if (htab->root.splt->size > 0)
861fb55a
DJ
10228 {
10229 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTREL, 0))
10230 return FALSE;
10231
10232 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_JMPREL, 0))
10233 return FALSE;
10234
10235 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_PLTRELSZ, 0))
10236 return FALSE;
10237
10238 if (! MIPS_ELF_ADD_DYNAMIC_ENTRY (info, DT_MIPS_PLTGOT, 0))
10239 return FALSE;
10240 }
90c14f0c 10241 if (htab->root.target_os == is_vxworks
7a2b07ff
NS
10242 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
10243 return FALSE;
b49e97c9
TS
10244 }
10245
b34976b6 10246 return TRUE;
b49e97c9
TS
10247}
10248\f
81d43bff
RS
10249/* REL is a relocation in INPUT_BFD that is being copied to OUTPUT_BFD.
10250 Adjust its R_ADDEND field so that it is correct for the output file.
10251 LOCAL_SYMS and LOCAL_SECTIONS are arrays of INPUT_BFD's local symbols
10252 and sections respectively; both use symbol indexes. */
10253
10254static void
10255mips_elf_adjust_addend (bfd *output_bfd, struct bfd_link_info *info,
10256 bfd *input_bfd, Elf_Internal_Sym *local_syms,
10257 asection **local_sections, Elf_Internal_Rela *rel)
10258{
10259 unsigned int r_type, r_symndx;
10260 Elf_Internal_Sym *sym;
10261 asection *sec;
10262
020d7251 10263 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
81d43bff
RS
10264 {
10265 r_type = ELF_R_TYPE (output_bfd, rel->r_info);
df58fc94 10266 if (gprel16_reloc_p (r_type)
81d43bff 10267 || r_type == R_MIPS_GPREL32
df58fc94 10268 || literal_reloc_p (r_type))
81d43bff
RS
10269 {
10270 rel->r_addend += _bfd_get_gp_value (input_bfd);
10271 rel->r_addend -= _bfd_get_gp_value (output_bfd);
10272 }
10273
10274 r_symndx = ELF_R_SYM (output_bfd, rel->r_info);
10275 sym = local_syms + r_symndx;
10276
10277 /* Adjust REL's addend to account for section merging. */
0e1862bb 10278 if (!bfd_link_relocatable (info))
81d43bff
RS
10279 {
10280 sec = local_sections[r_symndx];
10281 _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
10282 }
10283
10284 /* This would normally be done by the rela_normal code in elflink.c. */
10285 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
10286 rel->r_addend += local_sections[r_symndx]->output_offset;
10287 }
10288}
10289
545fd46b
MR
10290/* Handle relocations against symbols from removed linkonce sections,
10291 or sections discarded by a linker script. We use this wrapper around
10292 RELOC_AGAINST_DISCARDED_SECTION to handle triplets of compound relocs
10293 on 64-bit ELF targets. In this case for any relocation handled, which
10294 always be the first in a triplet, the remaining two have to be processed
10295 together with the first, even if they are R_MIPS_NONE. It is the symbol
10296 index referred by the first reloc that applies to all the three and the
10297 remaining two never refer to an object symbol. And it is the final
10298 relocation (the last non-null one) that determines the output field of
10299 the whole relocation so retrieve the corresponding howto structure for
10300 the relocatable field to be cleared by RELOC_AGAINST_DISCARDED_SECTION.
10301
10302 Note that RELOC_AGAINST_DISCARDED_SECTION is a macro that uses "continue"
10303 and therefore requires to be pasted in a loop. It also defines a block
10304 and does not protect any of its arguments, hence the extra brackets. */
10305
10306static void
10307mips_reloc_against_discarded_section (bfd *output_bfd,
10308 struct bfd_link_info *info,
10309 bfd *input_bfd, asection *input_section,
10310 Elf_Internal_Rela **rel,
10311 const Elf_Internal_Rela **relend,
10312 bfd_boolean rel_reloc,
10313 reloc_howto_type *howto,
10314 bfd_byte *contents)
10315{
10316 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10317 int count = bed->s->int_rels_per_ext_rel;
10318 unsigned int r_type;
10319 int i;
10320
10321 for (i = count - 1; i > 0; i--)
10322 {
10323 r_type = ELF_R_TYPE (output_bfd, (*rel)[i].r_info);
10324 if (r_type != R_MIPS_NONE)
10325 {
10326 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
10327 break;
10328 }
10329 }
10330 do
10331 {
10332 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
10333 (*rel), count, (*relend),
10334 howto, i, contents);
10335 }
10336 while (0);
10337}
10338
b49e97c9
TS
10339/* Relocate a MIPS ELF section. */
10340
b34976b6 10341bfd_boolean
9719ad41
RS
10342_bfd_mips_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
10343 bfd *input_bfd, asection *input_section,
10344 bfd_byte *contents, Elf_Internal_Rela *relocs,
10345 Elf_Internal_Sym *local_syms,
10346 asection **local_sections)
b49e97c9
TS
10347{
10348 Elf_Internal_Rela *rel;
10349 const Elf_Internal_Rela *relend;
10350 bfd_vma addend = 0;
b34976b6 10351 bfd_boolean use_saved_addend_p = FALSE;
b49e97c9 10352
056bafd4 10353 relend = relocs + input_section->reloc_count;
b49e97c9
TS
10354 for (rel = relocs; rel < relend; ++rel)
10355 {
10356 const char *name;
c9adbffe 10357 bfd_vma value = 0;
b49e97c9 10358 reloc_howto_type *howto;
ad3d9127 10359 bfd_boolean cross_mode_jump_p = FALSE;
b34976b6 10360 /* TRUE if the relocation is a RELA relocation, rather than a
07d6d2b8 10361 REL relocation. */
b34976b6 10362 bfd_boolean rela_relocation_p = TRUE;
b49e97c9 10363 unsigned int r_type = ELF_R_TYPE (output_bfd, rel->r_info);
9719ad41 10364 const char *msg;
ab96bf03
AM
10365 unsigned long r_symndx;
10366 asection *sec;
749b8d9d
L
10367 Elf_Internal_Shdr *symtab_hdr;
10368 struct elf_link_hash_entry *h;
d4730f92 10369 bfd_boolean rel_reloc;
b49e97c9 10370
d4730f92
BS
10371 rel_reloc = (NEWABI_P (input_bfd)
10372 && mips_elf_rel_relocation_p (input_bfd, input_section,
10373 relocs, rel));
b49e97c9 10374 /* Find the relocation howto for this relocation. */
d4730f92 10375 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type, !rel_reloc);
ab96bf03
AM
10376
10377 r_symndx = ELF_R_SYM (input_bfd, rel->r_info);
749b8d9d 10378 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
020d7251 10379 if (mips_elf_local_relocation_p (input_bfd, rel, local_sections))
749b8d9d
L
10380 {
10381 sec = local_sections[r_symndx];
10382 h = NULL;
10383 }
ab96bf03
AM
10384 else
10385 {
ab96bf03 10386 unsigned long extsymoff;
ab96bf03 10387
ab96bf03
AM
10388 extsymoff = 0;
10389 if (!elf_bad_symtab (input_bfd))
10390 extsymoff = symtab_hdr->sh_info;
10391 h = elf_sym_hashes (input_bfd) [r_symndx - extsymoff];
10392 while (h->root.type == bfd_link_hash_indirect
10393 || h->root.type == bfd_link_hash_warning)
10394 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10395
10396 sec = NULL;
10397 if (h->root.type == bfd_link_hash_defined
10398 || h->root.type == bfd_link_hash_defweak)
10399 sec = h->root.u.def.section;
10400 }
10401
dbaa2011 10402 if (sec != NULL && discarded_section (sec))
545fd46b
MR
10403 {
10404 mips_reloc_against_discarded_section (output_bfd, info, input_bfd,
10405 input_section, &rel, &relend,
10406 rel_reloc, howto, contents);
10407 continue;
10408 }
ab96bf03 10409
4a14403c 10410 if (r_type == R_MIPS_64 && ! NEWABI_P (input_bfd))
b49e97c9
TS
10411 {
10412 /* Some 32-bit code uses R_MIPS_64. In particular, people use
10413 64-bit code, but make sure all their addresses are in the
10414 lowermost or uppermost 32-bit section of the 64-bit address
10415 space. Thus, when they use an R_MIPS_64 they mean what is
10416 usually meant by R_MIPS_32, with the exception that the
10417 stored value is sign-extended to 64 bits. */
b34976b6 10418 howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, R_MIPS_32, FALSE);
b49e97c9
TS
10419
10420 /* On big-endian systems, we need to lie about the position
10421 of the reloc. */
10422 if (bfd_big_endian (input_bfd))
10423 rel->r_offset += 4;
10424 }
b49e97c9
TS
10425
10426 if (!use_saved_addend_p)
10427 {
b49e97c9
TS
10428 /* If these relocations were originally of the REL variety,
10429 we must pull the addend out of the field that will be
10430 relocated. Otherwise, we simply use the contents of the
c224138d
RS
10431 RELA relocation. */
10432 if (mips_elf_rel_relocation_p (input_bfd, input_section,
10433 relocs, rel))
b49e97c9 10434 {
b34976b6 10435 rela_relocation_p = FALSE;
c224138d
RS
10436 addend = mips_elf_read_rel_addend (input_bfd, rel,
10437 howto, contents);
738e5348
RS
10438 if (hi16_reloc_p (r_type)
10439 || (got16_reloc_p (r_type)
b49e97c9 10440 && mips_elf_local_relocation_p (input_bfd, rel,
020d7251 10441 local_sections)))
b49e97c9 10442 {
c224138d
RS
10443 if (!mips_elf_add_lo16_rel_addend (input_bfd, rel, relend,
10444 contents, &addend))
749b8d9d 10445 {
749b8d9d
L
10446 if (h)
10447 name = h->root.root.string;
10448 else
10449 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10450 local_syms + r_symndx,
10451 sec);
4eca0228 10452 _bfd_error_handler
695344c0 10453 /* xgettext:c-format */
2c1c9679 10454 (_("%pB: can't find matching LO16 reloc against `%s'"
2dcf00ce 10455 " for %s at %#" PRIx64 " in section `%pA'"),
c08bb8dd 10456 input_bfd, name,
2dcf00ce 10457 howto->name, (uint64_t) rel->r_offset, input_section);
749b8d9d 10458 }
b49e97c9 10459 }
30ac9238
RS
10460 else
10461 addend <<= howto->rightshift;
b49e97c9
TS
10462 }
10463 else
10464 addend = rel->r_addend;
81d43bff
RS
10465 mips_elf_adjust_addend (output_bfd, info, input_bfd,
10466 local_syms, local_sections, rel);
b49e97c9
TS
10467 }
10468
0e1862bb 10469 if (bfd_link_relocatable (info))
b49e97c9 10470 {
4a14403c 10471 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd)
b49e97c9
TS
10472 && bfd_big_endian (input_bfd))
10473 rel->r_offset -= 4;
10474
81d43bff 10475 if (!rela_relocation_p && rel->r_addend)
5a659663 10476 {
81d43bff 10477 addend += rel->r_addend;
738e5348 10478 if (hi16_reloc_p (r_type) || got16_reloc_p (r_type))
5a659663
TS
10479 addend = mips_elf_high (addend);
10480 else if (r_type == R_MIPS_HIGHER)
10481 addend = mips_elf_higher (addend);
10482 else if (r_type == R_MIPS_HIGHEST)
10483 addend = mips_elf_highest (addend);
30ac9238
RS
10484 else
10485 addend >>= howto->rightshift;
b49e97c9 10486
30ac9238
RS
10487 /* We use the source mask, rather than the destination
10488 mask because the place to which we are writing will be
10489 source of the addend in the final link. */
b49e97c9
TS
10490 addend &= howto->src_mask;
10491
5a659663 10492 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10493 /* See the comment above about using R_MIPS_64 in the 32-bit
10494 ABI. Here, we need to update the addend. It would be
10495 possible to get away with just using the R_MIPS_32 reloc
10496 but for endianness. */
10497 {
10498 bfd_vma sign_bits;
10499 bfd_vma low_bits;
10500 bfd_vma high_bits;
10501
10502 if (addend & ((bfd_vma) 1 << 31))
10503#ifdef BFD64
10504 sign_bits = ((bfd_vma) 1 << 32) - 1;
10505#else
10506 sign_bits = -1;
10507#endif
10508 else
10509 sign_bits = 0;
10510
10511 /* If we don't know that we have a 64-bit type,
10512 do two separate stores. */
10513 if (bfd_big_endian (input_bfd))
10514 {
10515 /* Store the sign-bits (which are most significant)
10516 first. */
10517 low_bits = sign_bits;
10518 high_bits = addend;
10519 }
10520 else
10521 {
10522 low_bits = addend;
10523 high_bits = sign_bits;
10524 }
10525 bfd_put_32 (input_bfd, low_bits,
10526 contents + rel->r_offset);
10527 bfd_put_32 (input_bfd, high_bits,
10528 contents + rel->r_offset + 4);
10529 continue;
10530 }
10531
10532 if (! mips_elf_perform_relocation (info, howto, rel, addend,
10533 input_bfd, input_section,
b34976b6
AM
10534 contents, FALSE))
10535 return FALSE;
b49e97c9
TS
10536 }
10537
10538 /* Go on to the next relocation. */
10539 continue;
10540 }
10541
10542 /* In the N32 and 64-bit ABIs there may be multiple consecutive
10543 relocations for the same offset. In that case we are
10544 supposed to treat the output of each relocation as the addend
10545 for the next. */
10546 if (rel + 1 < relend
10547 && rel->r_offset == rel[1].r_offset
10548 && ELF_R_TYPE (input_bfd, rel[1].r_info) != R_MIPS_NONE)
b34976b6 10549 use_saved_addend_p = TRUE;
b49e97c9 10550 else
b34976b6 10551 use_saved_addend_p = FALSE;
b49e97c9
TS
10552
10553 /* Figure out what value we are supposed to relocate. */
10554 switch (mips_elf_calculate_relocation (output_bfd, input_bfd,
47275900
MR
10555 input_section, contents,
10556 info, rel, addend, howto,
10557 local_syms, local_sections,
10558 &value, &name, &cross_mode_jump_p,
bce03d3d 10559 use_saved_addend_p))
b49e97c9
TS
10560 {
10561 case bfd_reloc_continue:
10562 /* There's nothing to do. */
10563 continue;
10564
10565 case bfd_reloc_undefined:
10566 /* mips_elf_calculate_relocation already called the
10567 undefined_symbol callback. There's no real point in
10568 trying to perform the relocation at this point, so we
10569 just skip ahead to the next relocation. */
10570 continue;
10571
10572 case bfd_reloc_notsupported:
10573 msg = _("internal error: unsupported relocation error");
10574 info->callbacks->warning
10575 (info, msg, name, input_bfd, input_section, rel->r_offset);
b34976b6 10576 return FALSE;
b49e97c9
TS
10577
10578 case bfd_reloc_overflow:
10579 if (use_saved_addend_p)
10580 /* Ignore overflow until we reach the last relocation for
10581 a given location. */
10582 ;
10583 else
10584 {
0e53d9da
AN
10585 struct mips_elf_link_hash_table *htab;
10586
10587 htab = mips_elf_hash_table (info);
4dfe6ac6 10588 BFD_ASSERT (htab != NULL);
b49e97c9 10589 BFD_ASSERT (name != NULL);
0e53d9da 10590 if (!htab->small_data_overflow_reported
9684f078 10591 && (gprel16_reloc_p (howto->type)
df58fc94 10592 || literal_reloc_p (howto->type)))
0e53d9da 10593 {
91d6fa6a
NC
10594 msg = _("small-data section exceeds 64KB;"
10595 " lower small-data size limit (see option -G)");
0e53d9da
AN
10596
10597 htab->small_data_overflow_reported = TRUE;
10598 (*info->callbacks->einfo) ("%P: %s\n", msg);
10599 }
1a72702b
AM
10600 (*info->callbacks->reloc_overflow)
10601 (info, NULL, name, howto->name, (bfd_vma) 0,
10602 input_bfd, input_section, rel->r_offset);
b49e97c9
TS
10603 }
10604 break;
10605
10606 case bfd_reloc_ok:
10607 break;
10608
df58fc94 10609 case bfd_reloc_outofrange:
7db9a74e 10610 msg = NULL;
df58fc94 10611 if (jal_reloc_p (howto->type))
9d862524 10612 msg = (cross_mode_jump_p
2c1c9679 10613 ? _("cannot convert a jump to JALX "
9d862524
MR
10614 "for a non-word-aligned address")
10615 : (howto->type == R_MIPS16_26
2c1c9679
AM
10616 ? _("jump to a non-word-aligned address")
10617 : _("jump to a non-instruction-aligned address")));
99aefae6 10618 else if (b_reloc_p (howto->type))
a6ebf616 10619 msg = (cross_mode_jump_p
2c1c9679 10620 ? _("cannot convert a branch to JALX "
a6ebf616 10621 "for a non-word-aligned address")
2c1c9679 10622 : _("branch to a non-instruction-aligned address"));
7db9a74e
MR
10623 else if (aligned_pcrel_reloc_p (howto->type))
10624 msg = _("PC-relative load from unaligned address");
10625 if (msg)
df58fc94 10626 {
de341542 10627 info->callbacks->einfo
ed53407e
MR
10628 ("%X%H: %s\n", input_bfd, input_section, rel->r_offset, msg);
10629 break;
7361da2c 10630 }
df58fc94
RS
10631 /* Fall through. */
10632
b49e97c9
TS
10633 default:
10634 abort ();
10635 break;
10636 }
10637
10638 /* If we've got another relocation for the address, keep going
10639 until we reach the last one. */
10640 if (use_saved_addend_p)
10641 {
10642 addend = value;
10643 continue;
10644 }
10645
4a14403c 10646 if (r_type == R_MIPS_64 && ! NEWABI_P (output_bfd))
b49e97c9
TS
10647 /* See the comment above about using R_MIPS_64 in the 32-bit
10648 ABI. Until now, we've been using the HOWTO for R_MIPS_32;
10649 that calculated the right value. Now, however, we
10650 sign-extend the 32-bit result to 64-bits, and store it as a
10651 64-bit value. We are especially generous here in that we
10652 go to extreme lengths to support this usage on systems with
10653 only a 32-bit VMA. */
10654 {
10655 bfd_vma sign_bits;
10656 bfd_vma low_bits;
10657 bfd_vma high_bits;
10658
10659 if (value & ((bfd_vma) 1 << 31))
10660#ifdef BFD64
10661 sign_bits = ((bfd_vma) 1 << 32) - 1;
10662#else
10663 sign_bits = -1;
10664#endif
10665 else
10666 sign_bits = 0;
10667
10668 /* If we don't know that we have a 64-bit type,
10669 do two separate stores. */
10670 if (bfd_big_endian (input_bfd))
10671 {
10672 /* Undo what we did above. */
10673 rel->r_offset -= 4;
10674 /* Store the sign-bits (which are most significant)
10675 first. */
10676 low_bits = sign_bits;
10677 high_bits = value;
10678 }
10679 else
10680 {
10681 low_bits = value;
10682 high_bits = sign_bits;
10683 }
10684 bfd_put_32 (input_bfd, low_bits,
10685 contents + rel->r_offset);
10686 bfd_put_32 (input_bfd, high_bits,
10687 contents + rel->r_offset + 4);
10688 continue;
10689 }
10690
10691 /* Actually perform the relocation. */
10692 if (! mips_elf_perform_relocation (info, howto, rel, value,
10693 input_bfd, input_section,
38a7df63 10694 contents, cross_mode_jump_p))
b34976b6 10695 return FALSE;
b49e97c9
TS
10696 }
10697
b34976b6 10698 return TRUE;
b49e97c9
TS
10699}
10700\f
861fb55a
DJ
10701/* A function that iterates over each entry in la25_stubs and fills
10702 in the code for each one. DATA points to a mips_htab_traverse_info. */
10703
10704static int
10705mips_elf_create_la25_stub (void **slot, void *data)
10706{
10707 struct mips_htab_traverse_info *hti;
10708 struct mips_elf_link_hash_table *htab;
10709 struct mips_elf_la25_stub *stub;
10710 asection *s;
10711 bfd_byte *loc;
10712 bfd_vma offset, target, target_high, target_low;
3734320d
MF
10713 bfd_vma branch_pc;
10714 bfd_signed_vma pcrel_offset = 0;
861fb55a
DJ
10715
10716 stub = (struct mips_elf_la25_stub *) *slot;
10717 hti = (struct mips_htab_traverse_info *) data;
10718 htab = mips_elf_hash_table (hti->info);
4dfe6ac6 10719 BFD_ASSERT (htab != NULL);
861fb55a
DJ
10720
10721 /* Create the section contents, if we haven't already. */
10722 s = stub->stub_section;
10723 loc = s->contents;
10724 if (loc == NULL)
10725 {
10726 loc = bfd_malloc (s->size);
10727 if (loc == NULL)
10728 {
10729 hti->error = TRUE;
10730 return FALSE;
10731 }
10732 s->contents = loc;
10733 }
10734
10735 /* Work out where in the section this stub should go. */
10736 offset = stub->offset;
10737
3734320d
MF
10738 /* We add 8 here to account for the LUI/ADDIU instructions
10739 before the branch instruction. This cannot be moved down to
10740 where pcrel_offset is calculated as 's' is updated in
10741 mips_elf_get_la25_target. */
10742 branch_pc = s->output_section->vma + s->output_offset + offset + 8;
10743
861fb55a 10744 /* Work out the target address. */
8f0c309a
CLT
10745 target = mips_elf_get_la25_target (stub, &s);
10746 target += s->output_section->vma + s->output_offset;
10747
861fb55a
DJ
10748 target_high = ((target + 0x8000) >> 16) & 0xffff;
10749 target_low = (target & 0xffff);
10750
3734320d
MF
10751 /* Calculate the PC of the compact branch instruction (for the case where
10752 compact branches are used for either microMIPSR6 or MIPSR6 with
10753 compact branches. Add 4-bytes to account for BC using the PC of the
10754 next instruction as the base. */
10755 pcrel_offset = target - (branch_pc + 4);
10756
861fb55a
DJ
10757 if (stub->stub_section != htab->strampoline)
10758 {
df58fc94 10759 /* This is a simple LUI/ADDIU stub. Zero out the beginning
861fb55a
DJ
10760 of the section and write the two instructions at the end. */
10761 memset (loc, 0, offset);
10762 loc += offset;
df58fc94
RS
10763 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10764 {
d21911ea
MR
10765 bfd_put_micromips_32 (hti->output_bfd,
10766 LA25_LUI_MICROMIPS (target_high),
10767 loc);
10768 bfd_put_micromips_32 (hti->output_bfd,
10769 LA25_ADDIU_MICROMIPS (target_low),
10770 loc + 4);
df58fc94
RS
10771 }
10772 else
10773 {
10774 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
10775 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10776 }
861fb55a
DJ
10777 }
10778 else
10779 {
10780 /* This is trampoline. */
10781 loc += offset;
df58fc94
RS
10782 if (ELF_ST_IS_MICROMIPS (stub->h->root.other))
10783 {
d21911ea
MR
10784 bfd_put_micromips_32 (hti->output_bfd,
10785 LA25_LUI_MICROMIPS (target_high), loc);
10786 bfd_put_micromips_32 (hti->output_bfd,
10787 LA25_J_MICROMIPS (target), loc + 4);
10788 bfd_put_micromips_32 (hti->output_bfd,
10789 LA25_ADDIU_MICROMIPS (target_low), loc + 8);
df58fc94
RS
10790 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10791 }
10792 else
10793 {
10794 bfd_put_32 (hti->output_bfd, LA25_LUI (target_high), loc);
3734320d
MF
10795 if (MIPSR6_P (hti->output_bfd) && htab->compact_branches)
10796 {
10797 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 4);
10798 bfd_put_32 (hti->output_bfd, LA25_BC (pcrel_offset), loc + 8);
10799 }
10800 else
10801 {
10802 bfd_put_32 (hti->output_bfd, LA25_J (target), loc + 4);
10803 bfd_put_32 (hti->output_bfd, LA25_ADDIU (target_low), loc + 8);
10804 }
df58fc94
RS
10805 bfd_put_32 (hti->output_bfd, 0, loc + 12);
10806 }
861fb55a
DJ
10807 }
10808 return TRUE;
10809}
10810
b49e97c9
TS
10811/* If NAME is one of the special IRIX6 symbols defined by the linker,
10812 adjust it appropriately now. */
10813
10814static void
9719ad41
RS
10815mips_elf_irix6_finish_dynamic_symbol (bfd *abfd ATTRIBUTE_UNUSED,
10816 const char *name, Elf_Internal_Sym *sym)
b49e97c9
TS
10817{
10818 /* The linker script takes care of providing names and values for
10819 these, but we must place them into the right sections. */
10820 static const char* const text_section_symbols[] = {
10821 "_ftext",
10822 "_etext",
10823 "__dso_displacement",
10824 "__elf_header",
10825 "__program_header_table",
10826 NULL
10827 };
10828
10829 static const char* const data_section_symbols[] = {
10830 "_fdata",
10831 "_edata",
10832 "_end",
10833 "_fbss",
10834 NULL
10835 };
10836
10837 const char* const *p;
10838 int i;
10839
10840 for (i = 0; i < 2; ++i)
10841 for (p = (i == 0) ? text_section_symbols : data_section_symbols;
10842 *p;
10843 ++p)
10844 if (strcmp (*p, name) == 0)
10845 {
10846 /* All of these symbols are given type STT_SECTION by the
10847 IRIX6 linker. */
10848 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
e10609d3 10849 sym->st_other = STO_PROTECTED;
b49e97c9
TS
10850
10851 /* The IRIX linker puts these symbols in special sections. */
10852 if (i == 0)
10853 sym->st_shndx = SHN_MIPS_TEXT;
10854 else
10855 sym->st_shndx = SHN_MIPS_DATA;
10856
10857 break;
10858 }
10859}
10860
10861/* Finish up dynamic symbol handling. We set the contents of various
10862 dynamic sections here. */
10863
b34976b6 10864bfd_boolean
9719ad41
RS
10865_bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
10866 struct bfd_link_info *info,
10867 struct elf_link_hash_entry *h,
10868 Elf_Internal_Sym *sym)
b49e97c9
TS
10869{
10870 bfd *dynobj;
b49e97c9 10871 asection *sgot;
f4416af6 10872 struct mips_got_info *g, *gg;
b49e97c9 10873 const char *name;
3d6746ca 10874 int idx;
5108fc1b 10875 struct mips_elf_link_hash_table *htab;
738e5348 10876 struct mips_elf_link_hash_entry *hmips;
b49e97c9 10877
5108fc1b 10878 htab = mips_elf_hash_table (info);
4dfe6ac6 10879 BFD_ASSERT (htab != NULL);
b49e97c9 10880 dynobj = elf_hash_table (info)->dynobj;
738e5348 10881 hmips = (struct mips_elf_link_hash_entry *) h;
b49e97c9 10882
90c14f0c 10883 BFD_ASSERT (htab->root.target_os != is_vxworks);
861fb55a 10884
1bbce132
MR
10885 if (h->plt.plist != NULL
10886 && (h->plt.plist->mips_offset != MINUS_ONE
10887 || h->plt.plist->comp_offset != MINUS_ONE))
861fb55a
DJ
10888 {
10889 /* We've decided to create a PLT entry for this symbol. */
10890 bfd_byte *loc;
1bbce132 10891 bfd_vma header_address, got_address;
861fb55a 10892 bfd_vma got_address_high, got_address_low, load;
1bbce132
MR
10893 bfd_vma got_index;
10894 bfd_vma isa_bit;
10895
10896 got_index = h->plt.plist->gotplt_index;
861fb55a
DJ
10897
10898 BFD_ASSERT (htab->use_plts_and_copy_relocs);
10899 BFD_ASSERT (h->dynindx != -1);
ce558b89 10900 BFD_ASSERT (htab->root.splt != NULL);
1bbce132 10901 BFD_ASSERT (got_index != MINUS_ONE);
861fb55a
DJ
10902 BFD_ASSERT (!h->def_regular);
10903
10904 /* Calculate the address of the PLT header. */
1bbce132 10905 isa_bit = htab->plt_header_is_comp;
ce558b89
AM
10906 header_address = (htab->root.splt->output_section->vma
10907 + htab->root.splt->output_offset + isa_bit);
861fb55a
DJ
10908
10909 /* Calculate the address of the .got.plt entry. */
ce558b89
AM
10910 got_address = (htab->root.sgotplt->output_section->vma
10911 + htab->root.sgotplt->output_offset
1bbce132
MR
10912 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
10913
861fb55a
DJ
10914 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
10915 got_address_low = got_address & 0xffff;
10916
789ff5b6
MR
10917 /* The PLT sequence is not safe for N64 if .got.plt entry's address
10918 cannot be loaded in two instructions. */
10919 if (ABI_64_P (output_bfd)
10920 && ((got_address + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
10921 {
10922 _bfd_error_handler
10923 /* xgettext:c-format */
10924 (_("%pB: `%pA' entry VMA of %#" PRIx64 " outside the 32-bit range "
10925 "supported; consider using `-Ttext-segment=...'"),
10926 output_bfd,
10927 htab->root.sgotplt->output_section,
10928 (int64_t) got_address);
10929 bfd_set_error (bfd_error_no_error);
10930 return FALSE;
10931 }
10932
861fb55a 10933 /* Initially point the .got.plt entry at the PLT header. */
6a382bce
MR
10934 loc = (htab->root.sgotplt->contents
10935 + got_index * MIPS_ELF_GOT_SIZE (dynobj));
861fb55a
DJ
10936 if (ABI_64_P (output_bfd))
10937 bfd_put_64 (output_bfd, header_address, loc);
10938 else
10939 bfd_put_32 (output_bfd, header_address, loc);
10940
1bbce132 10941 /* Now handle the PLT itself. First the standard entry (the order
07d6d2b8 10942 does not matter, we just have to pick one). */
1bbce132
MR
10943 if (h->plt.plist->mips_offset != MINUS_ONE)
10944 {
10945 const bfd_vma *plt_entry;
10946 bfd_vma plt_offset;
861fb55a 10947
1bbce132 10948 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
861fb55a 10949
ce558b89 10950 BFD_ASSERT (plt_offset <= htab->root.splt->size);
6d30f5b2 10951
1bbce132 10952 /* Find out where the .plt entry should go. */
ce558b89 10953 loc = htab->root.splt->contents + plt_offset;
1bbce132
MR
10954
10955 /* Pick the load opcode. */
10956 load = MIPS_ELF_LOAD_WORD (output_bfd);
10957
10958 /* Fill in the PLT entry itself. */
7361da2c
AB
10959
10960 if (MIPSR6_P (output_bfd))
3734320d
MF
10961 plt_entry = htab->compact_branches ? mipsr6_exec_plt_entry_compact
10962 : mipsr6_exec_plt_entry;
7361da2c
AB
10963 else
10964 plt_entry = mips_exec_plt_entry;
1bbce132
MR
10965 bfd_put_32 (output_bfd, plt_entry[0] | got_address_high, loc);
10966 bfd_put_32 (output_bfd, plt_entry[1] | got_address_low | load,
10967 loc + 4);
10968
3734320d
MF
10969 if (! LOAD_INTERLOCKS_P (output_bfd)
10970 || (MIPSR6_P (output_bfd) && htab->compact_branches))
1bbce132
MR
10971 {
10972 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low, loc + 8);
10973 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
10974 }
10975 else
10976 {
10977 bfd_put_32 (output_bfd, plt_entry[3], loc + 8);
10978 bfd_put_32 (output_bfd, plt_entry[2] | got_address_low,
10979 loc + 12);
10980 }
6d30f5b2 10981 }
1bbce132
MR
10982
10983 /* Now the compressed entry. They come after any standard ones. */
10984 if (h->plt.plist->comp_offset != MINUS_ONE)
6d30f5b2 10985 {
1bbce132
MR
10986 bfd_vma plt_offset;
10987
10988 plt_offset = (htab->plt_header_size + htab->plt_mips_offset
10989 + h->plt.plist->comp_offset);
10990
ce558b89 10991 BFD_ASSERT (plt_offset <= htab->root.splt->size);
1bbce132
MR
10992
10993 /* Find out where the .plt entry should go. */
ce558b89 10994 loc = htab->root.splt->contents + plt_offset;
1bbce132
MR
10995
10996 /* Fill in the PLT entry itself. */
833794fc
MR
10997 if (!MICROMIPS_P (output_bfd))
10998 {
10999 const bfd_vma *plt_entry = mips16_o32_exec_plt_entry;
11000
11001 bfd_put_16 (output_bfd, plt_entry[0], loc);
11002 bfd_put_16 (output_bfd, plt_entry[1], loc + 2);
11003 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11004 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11005 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11006 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11007 bfd_put_32 (output_bfd, got_address, loc + 12);
11008 }
11009 else if (htab->insn32)
11010 {
11011 const bfd_vma *plt_entry = micromips_insn32_o32_exec_plt_entry;
11012
11013 bfd_put_16 (output_bfd, plt_entry[0], loc);
11014 bfd_put_16 (output_bfd, got_address_high, loc + 2);
11015 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11016 bfd_put_16 (output_bfd, got_address_low, loc + 6);
11017 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11018 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11019 bfd_put_16 (output_bfd, plt_entry[6], loc + 12);
11020 bfd_put_16 (output_bfd, got_address_low, loc + 14);
11021 }
11022 else
1bbce132
MR
11023 {
11024 const bfd_vma *plt_entry = micromips_o32_exec_plt_entry;
11025 bfd_signed_vma gotpc_offset;
11026 bfd_vma loc_address;
11027
11028 BFD_ASSERT (got_address % 4 == 0);
11029
ce558b89
AM
11030 loc_address = (htab->root.splt->output_section->vma
11031 + htab->root.splt->output_offset + plt_offset);
1bbce132
MR
11032 gotpc_offset = got_address - ((loc_address | 3) ^ 3);
11033
11034 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11035 if (gotpc_offset + 0x1000000 >= 0x2000000)
11036 {
4eca0228 11037 _bfd_error_handler
695344c0 11038 /* xgettext:c-format */
2dcf00ce 11039 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
1bbce132
MR
11040 "beyond the range of ADDIUPC"),
11041 output_bfd,
ce558b89 11042 htab->root.sgotplt->output_section,
2dcf00ce 11043 (int64_t) gotpc_offset,
c08bb8dd 11044 htab->root.splt->output_section);
1bbce132
MR
11045 bfd_set_error (bfd_error_no_error);
11046 return FALSE;
11047 }
11048 bfd_put_16 (output_bfd,
11049 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11050 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11051 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11052 bfd_put_16 (output_bfd, plt_entry[3], loc + 6);
11053 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11054 bfd_put_16 (output_bfd, plt_entry[5], loc + 10);
11055 }
6d30f5b2 11056 }
861fb55a
DJ
11057
11058 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
ce558b89 11059 mips_elf_output_dynamic_relocation (output_bfd, htab->root.srelplt,
1bbce132 11060 got_index - 2, h->dynindx,
861fb55a
DJ
11061 R_MIPS_JUMP_SLOT, got_address);
11062
11063 /* We distinguish between PLT entries and lazy-binding stubs by
11064 giving the former an st_other value of STO_MIPS_PLT. Set the
11065 flag and leave the value if there are any relocations in the
11066 binary where pointer equality matters. */
11067 sym->st_shndx = SHN_UNDEF;
11068 if (h->pointer_equality_needed)
1bbce132 11069 sym->st_other = ELF_ST_SET_MIPS_PLT (sym->st_other);
861fb55a 11070 else
1bbce132
MR
11071 {
11072 sym->st_value = 0;
11073 sym->st_other = 0;
11074 }
861fb55a 11075 }
1bbce132
MR
11076
11077 if (h->plt.plist != NULL && h->plt.plist->stub_offset != MINUS_ONE)
b49e97c9 11078 {
861fb55a 11079 /* We've decided to create a lazy-binding stub. */
1bbce132
MR
11080 bfd_boolean micromips_p = MICROMIPS_P (output_bfd);
11081 unsigned int other = micromips_p ? STO_MICROMIPS : 0;
11082 bfd_vma stub_size = htab->function_stub_size;
5108fc1b 11083 bfd_byte stub[MIPS_FUNCTION_STUB_BIG_SIZE];
1bbce132
MR
11084 bfd_vma isa_bit = micromips_p;
11085 bfd_vma stub_big_size;
11086
833794fc 11087 if (!micromips_p)
1bbce132 11088 stub_big_size = MIPS_FUNCTION_STUB_BIG_SIZE;
833794fc
MR
11089 else if (htab->insn32)
11090 stub_big_size = MICROMIPS_INSN32_FUNCTION_STUB_BIG_SIZE;
11091 else
11092 stub_big_size = MICROMIPS_FUNCTION_STUB_BIG_SIZE;
b49e97c9
TS
11093
11094 /* This symbol has a stub. Set it up. */
11095
11096 BFD_ASSERT (h->dynindx != -1);
11097
1bbce132 11098 BFD_ASSERT (stub_size == stub_big_size || h->dynindx <= 0xffff);
3d6746ca
DD
11099
11100 /* Values up to 2^31 - 1 are allowed. Larger values would cause
5108fc1b
RS
11101 sign extension at runtime in the stub, resulting in a negative
11102 index value. */
11103 if (h->dynindx & ~0x7fffffff)
b34976b6 11104 return FALSE;
b49e97c9
TS
11105
11106 /* Fill the stub. */
1bbce132
MR
11107 if (micromips_p)
11108 {
11109 idx = 0;
11110 bfd_put_micromips_32 (output_bfd, STUB_LW_MICROMIPS (output_bfd),
11111 stub + idx);
11112 idx += 4;
833794fc
MR
11113 if (htab->insn32)
11114 {
11115 bfd_put_micromips_32 (output_bfd,
40fc1451 11116 STUB_MOVE32_MICROMIPS, stub + idx);
833794fc
MR
11117 idx += 4;
11118 }
11119 else
11120 {
11121 bfd_put_16 (output_bfd, STUB_MOVE_MICROMIPS, stub + idx);
11122 idx += 2;
11123 }
1bbce132
MR
11124 if (stub_size == stub_big_size)
11125 {
11126 long dynindx_hi = (h->dynindx >> 16) & 0x7fff;
11127
11128 bfd_put_micromips_32 (output_bfd,
11129 STUB_LUI_MICROMIPS (dynindx_hi),
11130 stub + idx);
11131 idx += 4;
11132 }
833794fc
MR
11133 if (htab->insn32)
11134 {
11135 bfd_put_micromips_32 (output_bfd, STUB_JALR32_MICROMIPS,
11136 stub + idx);
11137 idx += 4;
11138 }
11139 else
11140 {
11141 bfd_put_16 (output_bfd, STUB_JALR_MICROMIPS, stub + idx);
11142 idx += 2;
11143 }
1bbce132
MR
11144
11145 /* If a large stub is not required and sign extension is not a
11146 problem, then use legacy code in the stub. */
11147 if (stub_size == stub_big_size)
11148 bfd_put_micromips_32 (output_bfd,
11149 STUB_ORI_MICROMIPS (h->dynindx & 0xffff),
11150 stub + idx);
11151 else if (h->dynindx & ~0x7fff)
11152 bfd_put_micromips_32 (output_bfd,
11153 STUB_LI16U_MICROMIPS (h->dynindx & 0xffff),
11154 stub + idx);
11155 else
11156 bfd_put_micromips_32 (output_bfd,
11157 STUB_LI16S_MICROMIPS (output_bfd,
11158 h->dynindx),
11159 stub + idx);
11160 }
3d6746ca 11161 else
1bbce132
MR
11162 {
11163 idx = 0;
11164 bfd_put_32 (output_bfd, STUB_LW (output_bfd), stub + idx);
11165 idx += 4;
40fc1451 11166 bfd_put_32 (output_bfd, STUB_MOVE, stub + idx);
1bbce132
MR
11167 idx += 4;
11168 if (stub_size == stub_big_size)
11169 {
11170 bfd_put_32 (output_bfd, STUB_LUI ((h->dynindx >> 16) & 0x7fff),
11171 stub + idx);
11172 idx += 4;
11173 }
3734320d
MF
11174
11175 if (!(MIPSR6_P (output_bfd) && htab->compact_branches))
11176 {
11177 bfd_put_32 (output_bfd, STUB_JALR, stub + idx);
11178 idx += 4;
11179 }
1bbce132
MR
11180
11181 /* If a large stub is not required and sign extension is not a
11182 problem, then use legacy code in the stub. */
11183 if (stub_size == stub_big_size)
11184 bfd_put_32 (output_bfd, STUB_ORI (h->dynindx & 0xffff),
11185 stub + idx);
11186 else if (h->dynindx & ~0x7fff)
11187 bfd_put_32 (output_bfd, STUB_LI16U (h->dynindx & 0xffff),
11188 stub + idx);
11189 else
11190 bfd_put_32 (output_bfd, STUB_LI16S (output_bfd, h->dynindx),
11191 stub + idx);
3734320d
MF
11192 idx += 4;
11193
11194 if (MIPSR6_P (output_bfd) && htab->compact_branches)
11195 bfd_put_32 (output_bfd, STUB_JALRC, stub + idx);
1bbce132 11196 }
5108fc1b 11197
1bbce132
MR
11198 BFD_ASSERT (h->plt.plist->stub_offset <= htab->sstubs->size);
11199 memcpy (htab->sstubs->contents + h->plt.plist->stub_offset,
11200 stub, stub_size);
b49e97c9 11201
1bbce132 11202 /* Mark the symbol as undefined. stub_offset != -1 occurs
b49e97c9
TS
11203 only for the referenced symbol. */
11204 sym->st_shndx = SHN_UNDEF;
11205
11206 /* The run-time linker uses the st_value field of the symbol
11207 to reset the global offset table entry for this external
11208 to its stub address when unlinking a shared object. */
4e41d0d7
RS
11209 sym->st_value = (htab->sstubs->output_section->vma
11210 + htab->sstubs->output_offset
1bbce132
MR
11211 + h->plt.plist->stub_offset
11212 + isa_bit);
11213 sym->st_other = other;
b49e97c9
TS
11214 }
11215
738e5348
RS
11216 /* If we have a MIPS16 function with a stub, the dynamic symbol must
11217 refer to the stub, since only the stub uses the standard calling
11218 conventions. */
11219 if (h->dynindx != -1 && hmips->fn_stub != NULL)
11220 {
11221 BFD_ASSERT (hmips->need_fn_stub);
11222 sym->st_value = (hmips->fn_stub->output_section->vma
11223 + hmips->fn_stub->output_offset);
11224 sym->st_size = hmips->fn_stub->size;
11225 sym->st_other = ELF_ST_VISIBILITY (sym->st_other);
11226 }
11227
b49e97c9 11228 BFD_ASSERT (h->dynindx != -1
f5385ebf 11229 || h->forced_local);
b49e97c9 11230
ce558b89 11231 sgot = htab->root.sgot;
a8028dd0 11232 g = htab->got_info;
b49e97c9
TS
11233 BFD_ASSERT (g != NULL);
11234
11235 /* Run through the global symbol table, creating GOT entries for all
11236 the symbols that need them. */
020d7251 11237 if (hmips->global_got_area != GGA_NONE)
b49e97c9
TS
11238 {
11239 bfd_vma offset;
11240 bfd_vma value;
11241
6eaa6adc 11242 value = sym->st_value;
13fbec83 11243 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
b49e97c9
TS
11244 MIPS_ELF_PUT_WORD (output_bfd, value, sgot->contents + offset);
11245 }
11246
e641e783 11247 if (hmips->global_got_area != GGA_NONE && g->next)
f4416af6
AO
11248 {
11249 struct mips_got_entry e, *p;
0626d451 11250 bfd_vma entry;
f4416af6 11251 bfd_vma offset;
f4416af6
AO
11252
11253 gg = g;
11254
11255 e.abfd = output_bfd;
11256 e.symndx = -1;
738e5348 11257 e.d.h = hmips;
9ab066b4 11258 e.tls_type = GOT_TLS_NONE;
143d77c5 11259
f4416af6
AO
11260 for (g = g->next; g->next != gg; g = g->next)
11261 {
11262 if (g->got_entries
11263 && (p = (struct mips_got_entry *) htab_find (g->got_entries,
11264 &e)))
11265 {
11266 offset = p->gotidx;
ce558b89 11267 BFD_ASSERT (offset > 0 && offset < htab->root.sgot->size);
0e1862bb 11268 if (bfd_link_pic (info)
0626d451
RS
11269 || (elf_hash_table (info)->dynamic_sections_created
11270 && p->d.h != NULL
f5385ebf
AM
11271 && p->d.h->root.def_dynamic
11272 && !p->d.h->root.def_regular))
0626d451
RS
11273 {
11274 /* Create an R_MIPS_REL32 relocation for this entry. Due to
11275 the various compatibility problems, it's easier to mock
11276 up an R_MIPS_32 or R_MIPS_64 relocation and leave
11277 mips_elf_create_dynamic_relocation to calculate the
11278 appropriate addend. */
11279 Elf_Internal_Rela rel[3];
11280
11281 memset (rel, 0, sizeof (rel));
11282 if (ABI_64_P (output_bfd))
11283 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_64);
11284 else
11285 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_32);
11286 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset = offset;
11287
11288 entry = 0;
11289 if (! (mips_elf_create_dynamic_relocation
11290 (output_bfd, info, rel,
11291 e.d.h, NULL, sym->st_value, &entry, sgot)))
11292 return FALSE;
11293 }
11294 else
11295 entry = sym->st_value;
11296 MIPS_ELF_PUT_WORD (output_bfd, entry, sgot->contents + offset);
f4416af6
AO
11297 }
11298 }
11299 }
11300
b49e97c9
TS
11301 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
11302 name = h->root.root.string;
9637f6ef 11303 if (h == elf_hash_table (info)->hdynamic
22edb2f1 11304 || h == elf_hash_table (info)->hgot)
b49e97c9
TS
11305 sym->st_shndx = SHN_ABS;
11306 else if (strcmp (name, "_DYNAMIC_LINK") == 0
11307 || strcmp (name, "_DYNAMIC_LINKING") == 0)
11308 {
11309 sym->st_shndx = SHN_ABS;
11310 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11311 sym->st_value = 1;
11312 }
b49e97c9
TS
11313 else if (SGI_COMPAT (output_bfd))
11314 {
11315 if (strcmp (name, mips_elf_dynsym_rtproc_names[0]) == 0
11316 || strcmp (name, mips_elf_dynsym_rtproc_names[1]) == 0)
11317 {
11318 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11319 sym->st_other = STO_PROTECTED;
11320 sym->st_value = 0;
11321 sym->st_shndx = SHN_MIPS_DATA;
11322 }
11323 else if (strcmp (name, mips_elf_dynsym_rtproc_names[2]) == 0)
11324 {
11325 sym->st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
11326 sym->st_other = STO_PROTECTED;
11327 sym->st_value = mips_elf_hash_table (info)->procedure_count;
11328 sym->st_shndx = SHN_ABS;
11329 }
11330 else if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS)
11331 {
11332 if (h->type == STT_FUNC)
11333 sym->st_shndx = SHN_MIPS_TEXT;
11334 else if (h->type == STT_OBJECT)
11335 sym->st_shndx = SHN_MIPS_DATA;
11336 }
11337 }
11338
861fb55a
DJ
11339 /* Emit a copy reloc, if needed. */
11340 if (h->needs_copy)
11341 {
11342 asection *s;
11343 bfd_vma symval;
11344
11345 BFD_ASSERT (h->dynindx != -1);
11346 BFD_ASSERT (htab->use_plts_and_copy_relocs);
11347
11348 s = mips_elf_rel_dyn_section (info, FALSE);
11349 symval = (h->root.u.def.section->output_section->vma
11350 + h->root.u.def.section->output_offset
11351 + h->root.u.def.value);
11352 mips_elf_output_dynamic_relocation (output_bfd, s, s->reloc_count++,
11353 h->dynindx, R_MIPS_COPY, symval);
11354 }
11355
b49e97c9
TS
11356 /* Handle the IRIX6-specific symbols. */
11357 if (IRIX_COMPAT (output_bfd) == ict_irix6)
11358 mips_elf_irix6_finish_dynamic_symbol (output_bfd, name, sym);
11359
cbf8d970
MR
11360 /* Keep dynamic compressed symbols odd. This allows the dynamic linker
11361 to treat compressed symbols like any other. */
30c09090 11362 if (ELF_ST_IS_MIPS16 (sym->st_other))
738e5348
RS
11363 {
11364 BFD_ASSERT (sym->st_value & 1);
11365 sym->st_other -= STO_MIPS16;
11366 }
cbf8d970
MR
11367 else if (ELF_ST_IS_MICROMIPS (sym->st_other))
11368 {
11369 BFD_ASSERT (sym->st_value & 1);
11370 sym->st_other -= STO_MICROMIPS;
11371 }
b49e97c9 11372
b34976b6 11373 return TRUE;
b49e97c9
TS
11374}
11375
0a44bf69
RS
11376/* Likewise, for VxWorks. */
11377
11378bfd_boolean
11379_bfd_mips_vxworks_finish_dynamic_symbol (bfd *output_bfd,
11380 struct bfd_link_info *info,
11381 struct elf_link_hash_entry *h,
11382 Elf_Internal_Sym *sym)
11383{
11384 bfd *dynobj;
11385 asection *sgot;
11386 struct mips_got_info *g;
11387 struct mips_elf_link_hash_table *htab;
020d7251 11388 struct mips_elf_link_hash_entry *hmips;
0a44bf69
RS
11389
11390 htab = mips_elf_hash_table (info);
4dfe6ac6 11391 BFD_ASSERT (htab != NULL);
0a44bf69 11392 dynobj = elf_hash_table (info)->dynobj;
020d7251 11393 hmips = (struct mips_elf_link_hash_entry *) h;
0a44bf69 11394
1bbce132 11395 if (h->plt.plist != NULL && h->plt.plist->mips_offset != MINUS_ONE)
0a44bf69 11396 {
6d79d2ed 11397 bfd_byte *loc;
1bbce132 11398 bfd_vma plt_address, got_address, got_offset, branch_offset;
0a44bf69
RS
11399 Elf_Internal_Rela rel;
11400 static const bfd_vma *plt_entry;
1bbce132
MR
11401 bfd_vma gotplt_index;
11402 bfd_vma plt_offset;
11403
11404 plt_offset = htab->plt_header_size + h->plt.plist->mips_offset;
11405 gotplt_index = h->plt.plist->gotplt_index;
0a44bf69
RS
11406
11407 BFD_ASSERT (h->dynindx != -1);
ce558b89 11408 BFD_ASSERT (htab->root.splt != NULL);
1bbce132 11409 BFD_ASSERT (gotplt_index != MINUS_ONE);
ce558b89 11410 BFD_ASSERT (plt_offset <= htab->root.splt->size);
0a44bf69
RS
11411
11412 /* Calculate the address of the .plt entry. */
ce558b89
AM
11413 plt_address = (htab->root.splt->output_section->vma
11414 + htab->root.splt->output_offset
1bbce132 11415 + plt_offset);
0a44bf69
RS
11416
11417 /* Calculate the address of the .got.plt entry. */
ce558b89
AM
11418 got_address = (htab->root.sgotplt->output_section->vma
11419 + htab->root.sgotplt->output_offset
1bbce132 11420 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd));
0a44bf69
RS
11421
11422 /* Calculate the offset of the .got.plt entry from
11423 _GLOBAL_OFFSET_TABLE_. */
11424 got_offset = mips_elf_gotplt_index (info, h);
11425
11426 /* Calculate the offset for the branch at the start of the PLT
11427 entry. The branch jumps to the beginning of .plt. */
1bbce132 11428 branch_offset = -(plt_offset / 4 + 1) & 0xffff;
0a44bf69
RS
11429
11430 /* Fill in the initial value of the .got.plt entry. */
11431 bfd_put_32 (output_bfd, plt_address,
ce558b89 11432 (htab->root.sgotplt->contents
1bbce132 11433 + gotplt_index * MIPS_ELF_GOT_SIZE (output_bfd)));
0a44bf69
RS
11434
11435 /* Find out where the .plt entry should go. */
ce558b89 11436 loc = htab->root.splt->contents + plt_offset;
0a44bf69 11437
0e1862bb 11438 if (bfd_link_pic (info))
0a44bf69
RS
11439 {
11440 plt_entry = mips_vxworks_shared_plt_entry;
11441 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 11442 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
11443 }
11444 else
11445 {
11446 bfd_vma got_address_high, got_address_low;
11447
11448 plt_entry = mips_vxworks_exec_plt_entry;
11449 got_address_high = ((got_address + 0x8000) >> 16) & 0xffff;
11450 got_address_low = got_address & 0xffff;
11451
11452 bfd_put_32 (output_bfd, plt_entry[0] | branch_offset, loc);
1bbce132 11453 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_index, loc + 4);
0a44bf69
RS
11454 bfd_put_32 (output_bfd, plt_entry[2] | got_address_high, loc + 8);
11455 bfd_put_32 (output_bfd, plt_entry[3] | got_address_low, loc + 12);
11456 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11457 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11458 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11459 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11460
11461 loc = (htab->srelplt2->contents
1bbce132 11462 + (gotplt_index * 3 + 2) * sizeof (Elf32_External_Rela));
0a44bf69
RS
11463
11464 /* Emit a relocation for the .got.plt entry. */
11465 rel.r_offset = got_address;
11466 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
1bbce132 11467 rel.r_addend = plt_offset;
0a44bf69
RS
11468 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11469
11470 /* Emit a relocation for the lui of %hi(<.got.plt slot>). */
11471 loc += sizeof (Elf32_External_Rela);
11472 rel.r_offset = plt_address + 8;
11473 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11474 rel.r_addend = got_offset;
11475 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11476
11477 /* Emit a relocation for the addiu of %lo(<.got.plt slot>). */
11478 loc += sizeof (Elf32_External_Rela);
11479 rel.r_offset += 4;
11480 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11481 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11482 }
11483
11484 /* Emit an R_MIPS_JUMP_SLOT relocation against the .got.plt entry. */
ce558b89 11485 loc = (htab->root.srelplt->contents
1bbce132 11486 + gotplt_index * sizeof (Elf32_External_Rela));
0a44bf69
RS
11487 rel.r_offset = got_address;
11488 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_JUMP_SLOT);
11489 rel.r_addend = 0;
11490 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11491
11492 if (!h->def_regular)
11493 sym->st_shndx = SHN_UNDEF;
11494 }
11495
11496 BFD_ASSERT (h->dynindx != -1 || h->forced_local);
11497
ce558b89 11498 sgot = htab->root.sgot;
a8028dd0 11499 g = htab->got_info;
0a44bf69
RS
11500 BFD_ASSERT (g != NULL);
11501
11502 /* See if this symbol has an entry in the GOT. */
020d7251 11503 if (hmips->global_got_area != GGA_NONE)
0a44bf69
RS
11504 {
11505 bfd_vma offset;
11506 Elf_Internal_Rela outrel;
11507 bfd_byte *loc;
11508 asection *s;
11509
11510 /* Install the symbol value in the GOT. */
13fbec83 11511 offset = mips_elf_primary_global_got_index (output_bfd, info, h);
0a44bf69
RS
11512 MIPS_ELF_PUT_WORD (output_bfd, sym->st_value, sgot->contents + offset);
11513
11514 /* Add a dynamic relocation for it. */
11515 s = mips_elf_rel_dyn_section (info, FALSE);
11516 loc = s->contents + (s->reloc_count++ * sizeof (Elf32_External_Rela));
11517 outrel.r_offset = (sgot->output_section->vma
11518 + sgot->output_offset
11519 + offset);
11520 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_32);
11521 outrel.r_addend = 0;
11522 bfd_elf32_swap_reloca_out (dynobj, &outrel, loc);
11523 }
11524
11525 /* Emit a copy reloc, if needed. */
11526 if (h->needs_copy)
11527 {
11528 Elf_Internal_Rela rel;
5474d94f
AM
11529 asection *srel;
11530 bfd_byte *loc;
0a44bf69
RS
11531
11532 BFD_ASSERT (h->dynindx != -1);
11533
11534 rel.r_offset = (h->root.u.def.section->output_section->vma
11535 + h->root.u.def.section->output_offset
11536 + h->root.u.def.value);
11537 rel.r_info = ELF32_R_INFO (h->dynindx, R_MIPS_COPY);
11538 rel.r_addend = 0;
afbf7e8e 11539 if (h->root.u.def.section == htab->root.sdynrelro)
5474d94f
AM
11540 srel = htab->root.sreldynrelro;
11541 else
11542 srel = htab->root.srelbss;
11543 loc = srel->contents + srel->reloc_count * sizeof (Elf32_External_Rela);
11544 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11545 ++srel->reloc_count;
0a44bf69
RS
11546 }
11547
df58fc94
RS
11548 /* If this is a mips16/microMIPS symbol, force the value to be even. */
11549 if (ELF_ST_IS_COMPRESSED (sym->st_other))
0a44bf69
RS
11550 sym->st_value &= ~1;
11551
11552 return TRUE;
11553}
11554
861fb55a
DJ
11555/* Write out a plt0 entry to the beginning of .plt. */
11556
1bbce132 11557static bfd_boolean
861fb55a
DJ
11558mips_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11559{
11560 bfd_byte *loc;
11561 bfd_vma gotplt_value, gotplt_value_high, gotplt_value_low;
11562 static const bfd_vma *plt_entry;
11563 struct mips_elf_link_hash_table *htab;
11564
11565 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11566 BFD_ASSERT (htab != NULL);
11567
861fb55a 11568 if (ABI_64_P (output_bfd))
3734320d
MF
11569 plt_entry = (htab->compact_branches
11570 ? mipsr6_n64_exec_plt0_entry_compact
11571 : mips_n64_exec_plt0_entry);
861fb55a 11572 else if (ABI_N32_P (output_bfd))
3734320d
MF
11573 plt_entry = (htab->compact_branches
11574 ? mipsr6_n32_exec_plt0_entry_compact
11575 : mips_n32_exec_plt0_entry);
833794fc 11576 else if (!htab->plt_header_is_comp)
3734320d
MF
11577 plt_entry = (htab->compact_branches
11578 ? mipsr6_o32_exec_plt0_entry_compact
11579 : mips_o32_exec_plt0_entry);
833794fc
MR
11580 else if (htab->insn32)
11581 plt_entry = micromips_insn32_o32_exec_plt0_entry;
11582 else
11583 plt_entry = micromips_o32_exec_plt0_entry;
861fb55a
DJ
11584
11585 /* Calculate the value of .got.plt. */
ce558b89
AM
11586 gotplt_value = (htab->root.sgotplt->output_section->vma
11587 + htab->root.sgotplt->output_offset);
861fb55a
DJ
11588 gotplt_value_high = ((gotplt_value + 0x8000) >> 16) & 0xffff;
11589 gotplt_value_low = gotplt_value & 0xffff;
11590
11591 /* The PLT sequence is not safe for N64 if .got.plt's address can
11592 not be loaded in two instructions. */
789ff5b6
MR
11593 if (ABI_64_P (output_bfd)
11594 && ((gotplt_value + 0x80008000) & ~(bfd_vma) 0xffffffff) != 0)
11595 {
11596 _bfd_error_handler
11597 /* xgettext:c-format */
11598 (_("%pB: `%pA' start VMA of %#" PRIx64 " outside the 32-bit range "
11599 "supported; consider using `-Ttext-segment=...'"),
11600 output_bfd,
11601 htab->root.sgotplt->output_section,
11602 (int64_t) gotplt_value);
11603 bfd_set_error (bfd_error_no_error);
11604 return FALSE;
11605 }
861fb55a
DJ
11606
11607 /* Install the PLT header. */
ce558b89 11608 loc = htab->root.splt->contents;
1bbce132
MR
11609 if (plt_entry == micromips_o32_exec_plt0_entry)
11610 {
11611 bfd_vma gotpc_offset;
11612 bfd_vma loc_address;
11613 size_t i;
11614
11615 BFD_ASSERT (gotplt_value % 4 == 0);
11616
ce558b89
AM
11617 loc_address = (htab->root.splt->output_section->vma
11618 + htab->root.splt->output_offset);
1bbce132
MR
11619 gotpc_offset = gotplt_value - ((loc_address | 3) ^ 3);
11620
11621 /* ADDIUPC has a span of +/-16MB, check we're in range. */
11622 if (gotpc_offset + 0x1000000 >= 0x2000000)
11623 {
4eca0228 11624 _bfd_error_handler
695344c0 11625 /* xgettext:c-format */
2dcf00ce
AM
11626 (_("%pB: `%pA' offset of %" PRId64 " from `%pA' "
11627 "beyond the range of ADDIUPC"),
1bbce132 11628 output_bfd,
ce558b89 11629 htab->root.sgotplt->output_section,
2dcf00ce 11630 (int64_t) gotpc_offset,
c08bb8dd 11631 htab->root.splt->output_section);
1bbce132
MR
11632 bfd_set_error (bfd_error_no_error);
11633 return FALSE;
11634 }
11635 bfd_put_16 (output_bfd,
11636 plt_entry[0] | ((gotpc_offset >> 18) & 0x7f), loc);
11637 bfd_put_16 (output_bfd, (gotpc_offset >> 2) & 0xffff, loc + 2);
11638 for (i = 2; i < ARRAY_SIZE (micromips_o32_exec_plt0_entry); i++)
11639 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11640 }
833794fc
MR
11641 else if (plt_entry == micromips_insn32_o32_exec_plt0_entry)
11642 {
11643 size_t i;
11644
11645 bfd_put_16 (output_bfd, plt_entry[0], loc);
11646 bfd_put_16 (output_bfd, gotplt_value_high, loc + 2);
11647 bfd_put_16 (output_bfd, plt_entry[2], loc + 4);
11648 bfd_put_16 (output_bfd, gotplt_value_low, loc + 6);
11649 bfd_put_16 (output_bfd, plt_entry[4], loc + 8);
11650 bfd_put_16 (output_bfd, gotplt_value_low, loc + 10);
11651 for (i = 6; i < ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry); i++)
11652 bfd_put_16 (output_bfd, plt_entry[i], loc + (i * 2));
11653 }
1bbce132
MR
11654 else
11655 {
11656 bfd_put_32 (output_bfd, plt_entry[0] | gotplt_value_high, loc);
11657 bfd_put_32 (output_bfd, plt_entry[1] | gotplt_value_low, loc + 4);
11658 bfd_put_32 (output_bfd, plt_entry[2] | gotplt_value_low, loc + 8);
11659 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11660 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11661 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11662 bfd_put_32 (output_bfd, plt_entry[6], loc + 24);
11663 bfd_put_32 (output_bfd, plt_entry[7], loc + 28);
11664 }
11665
11666 return TRUE;
861fb55a
DJ
11667}
11668
0a44bf69
RS
11669/* Install the PLT header for a VxWorks executable and finalize the
11670 contents of .rela.plt.unloaded. */
11671
11672static void
11673mips_vxworks_finish_exec_plt (bfd *output_bfd, struct bfd_link_info *info)
11674{
11675 Elf_Internal_Rela rela;
11676 bfd_byte *loc;
11677 bfd_vma got_value, got_value_high, got_value_low, plt_address;
11678 static const bfd_vma *plt_entry;
11679 struct mips_elf_link_hash_table *htab;
11680
11681 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11682 BFD_ASSERT (htab != NULL);
11683
0a44bf69
RS
11684 plt_entry = mips_vxworks_exec_plt0_entry;
11685
11686 /* Calculate the value of _GLOBAL_OFFSET_TABLE_. */
11687 got_value = (htab->root.hgot->root.u.def.section->output_section->vma
11688 + htab->root.hgot->root.u.def.section->output_offset
11689 + htab->root.hgot->root.u.def.value);
11690
11691 got_value_high = ((got_value + 0x8000) >> 16) & 0xffff;
11692 got_value_low = got_value & 0xffff;
11693
11694 /* Calculate the address of the PLT header. */
ce558b89
AM
11695 plt_address = (htab->root.splt->output_section->vma
11696 + htab->root.splt->output_offset);
0a44bf69
RS
11697
11698 /* Install the PLT header. */
ce558b89 11699 loc = htab->root.splt->contents;
0a44bf69
RS
11700 bfd_put_32 (output_bfd, plt_entry[0] | got_value_high, loc);
11701 bfd_put_32 (output_bfd, plt_entry[1] | got_value_low, loc + 4);
11702 bfd_put_32 (output_bfd, plt_entry[2], loc + 8);
11703 bfd_put_32 (output_bfd, plt_entry[3], loc + 12);
11704 bfd_put_32 (output_bfd, plt_entry[4], loc + 16);
11705 bfd_put_32 (output_bfd, plt_entry[5], loc + 20);
11706
11707 /* Output the relocation for the lui of %hi(_GLOBAL_OFFSET_TABLE_). */
11708 loc = htab->srelplt2->contents;
11709 rela.r_offset = plt_address;
11710 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11711 rela.r_addend = 0;
11712 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11713 loc += sizeof (Elf32_External_Rela);
11714
11715 /* Output the relocation for the following addiu of
11716 %lo(_GLOBAL_OFFSET_TABLE_). */
11717 rela.r_offset += 4;
11718 rela.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11719 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
11720 loc += sizeof (Elf32_External_Rela);
11721
11722 /* Fix up the remaining relocations. They may have the wrong
11723 symbol index for _G_O_T_ or _P_L_T_ depending on the order
11724 in which symbols were output. */
11725 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
11726 {
11727 Elf_Internal_Rela rel;
11728
11729 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11730 rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_MIPS_32);
11731 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11732 loc += sizeof (Elf32_External_Rela);
11733
11734 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11735 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_HI16);
11736 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11737 loc += sizeof (Elf32_External_Rela);
11738
11739 bfd_elf32_swap_reloca_in (output_bfd, loc, &rel);
11740 rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_MIPS_LO16);
11741 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
11742 loc += sizeof (Elf32_External_Rela);
11743 }
11744}
11745
11746/* Install the PLT header for a VxWorks shared library. */
11747
11748static void
11749mips_vxworks_finish_shared_plt (bfd *output_bfd, struct bfd_link_info *info)
11750{
11751 unsigned int i;
11752 struct mips_elf_link_hash_table *htab;
11753
11754 htab = mips_elf_hash_table (info);
4dfe6ac6 11755 BFD_ASSERT (htab != NULL);
0a44bf69
RS
11756
11757 /* We just need to copy the entry byte-by-byte. */
11758 for (i = 0; i < ARRAY_SIZE (mips_vxworks_shared_plt0_entry); i++)
11759 bfd_put_32 (output_bfd, mips_vxworks_shared_plt0_entry[i],
ce558b89 11760 htab->root.splt->contents + i * 4);
0a44bf69
RS
11761}
11762
b49e97c9
TS
11763/* Finish up the dynamic sections. */
11764
b34976b6 11765bfd_boolean
9719ad41
RS
11766_bfd_mips_elf_finish_dynamic_sections (bfd *output_bfd,
11767 struct bfd_link_info *info)
b49e97c9
TS
11768{
11769 bfd *dynobj;
11770 asection *sdyn;
11771 asection *sgot;
f4416af6 11772 struct mips_got_info *gg, *g;
0a44bf69 11773 struct mips_elf_link_hash_table *htab;
b49e97c9 11774
0a44bf69 11775 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
11776 BFD_ASSERT (htab != NULL);
11777
b49e97c9
TS
11778 dynobj = elf_hash_table (info)->dynobj;
11779
3d4d4302 11780 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
b49e97c9 11781
ce558b89 11782 sgot = htab->root.sgot;
23cc69b6 11783 gg = htab->got_info;
b49e97c9
TS
11784
11785 if (elf_hash_table (info)->dynamic_sections_created)
11786 {
11787 bfd_byte *b;
943284cc 11788 int dyn_to_skip = 0, dyn_skipped = 0;
b49e97c9
TS
11789
11790 BFD_ASSERT (sdyn != NULL);
23cc69b6
RS
11791 BFD_ASSERT (gg != NULL);
11792
d7206569 11793 g = mips_elf_bfd_got (output_bfd, FALSE);
b49e97c9
TS
11794 BFD_ASSERT (g != NULL);
11795
11796 for (b = sdyn->contents;
eea6121a 11797 b < sdyn->contents + sdyn->size;
b49e97c9
TS
11798 b += MIPS_ELF_DYN_SIZE (dynobj))
11799 {
11800 Elf_Internal_Dyn dyn;
11801 const char *name;
11802 size_t elemsize;
11803 asection *s;
b34976b6 11804 bfd_boolean swap_out_p;
b49e97c9
TS
11805
11806 /* Read in the current dynamic entry. */
11807 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
11808
11809 /* Assume that we're going to modify it and write it out. */
b34976b6 11810 swap_out_p = TRUE;
b49e97c9
TS
11811
11812 switch (dyn.d_tag)
11813 {
11814 case DT_RELENT:
b49e97c9
TS
11815 dyn.d_un.d_val = MIPS_ELF_REL_SIZE (dynobj);
11816 break;
11817
0a44bf69 11818 case DT_RELAENT:
90c14f0c 11819 BFD_ASSERT (htab->root.target_os == is_vxworks);
0a44bf69
RS
11820 dyn.d_un.d_val = MIPS_ELF_RELA_SIZE (dynobj);
11821 break;
11822
b49e97c9
TS
11823 case DT_STRSZ:
11824 /* Rewrite DT_STRSZ. */
11825 dyn.d_un.d_val =
11826 _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
11827 break;
11828
11829 case DT_PLTGOT:
ce558b89 11830 s = htab->root.sgot;
861fb55a
DJ
11831 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11832 break;
11833
11834 case DT_MIPS_PLTGOT:
ce558b89 11835 s = htab->root.sgotplt;
861fb55a 11836 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
b49e97c9
TS
11837 break;
11838
11839 case DT_MIPS_RLD_VERSION:
11840 dyn.d_un.d_val = 1; /* XXX */
11841 break;
11842
11843 case DT_MIPS_FLAGS:
11844 dyn.d_un.d_val = RHF_NOTPOT; /* XXX */
11845 break;
11846
b49e97c9 11847 case DT_MIPS_TIME_STAMP:
6edfbbad
DJ
11848 {
11849 time_t t;
11850 time (&t);
11851 dyn.d_un.d_val = t;
11852 }
b49e97c9
TS
11853 break;
11854
11855 case DT_MIPS_ICHECKSUM:
11856 /* XXX FIXME: */
b34976b6 11857 swap_out_p = FALSE;
b49e97c9
TS
11858 break;
11859
11860 case DT_MIPS_IVERSION:
11861 /* XXX FIXME: */
b34976b6 11862 swap_out_p = FALSE;
b49e97c9
TS
11863 break;
11864
11865 case DT_MIPS_BASE_ADDRESS:
11866 s = output_bfd->sections;
11867 BFD_ASSERT (s != NULL);
11868 dyn.d_un.d_ptr = s->vma & ~(bfd_vma) 0xffff;
11869 break;
11870
11871 case DT_MIPS_LOCAL_GOTNO:
11872 dyn.d_un.d_val = g->local_gotno;
11873 break;
11874
11875 case DT_MIPS_UNREFEXTNO:
11876 /* The index into the dynamic symbol table which is the
11877 entry of the first external symbol that is not
11878 referenced within the same object. */
11879 dyn.d_un.d_val = bfd_count_sections (output_bfd) + 1;
11880 break;
11881
11882 case DT_MIPS_GOTSYM:
d222d210 11883 if (htab->global_gotsym)
b49e97c9 11884 {
d222d210 11885 dyn.d_un.d_val = htab->global_gotsym->dynindx;
b49e97c9
TS
11886 break;
11887 }
11888 /* In case if we don't have global got symbols we default
11889 to setting DT_MIPS_GOTSYM to the same value as
1a0670f3
AM
11890 DT_MIPS_SYMTABNO. */
11891 /* Fall through. */
b49e97c9
TS
11892
11893 case DT_MIPS_SYMTABNO:
11894 name = ".dynsym";
11895 elemsize = MIPS_ELF_SYM_SIZE (output_bfd);
4ade44b7 11896 s = bfd_get_linker_section (dynobj, name);
b49e97c9 11897
131e2f8e
MF
11898 if (s != NULL)
11899 dyn.d_un.d_val = s->size / elemsize;
11900 else
11901 dyn.d_un.d_val = 0;
b49e97c9
TS
11902 break;
11903
11904 case DT_MIPS_HIPAGENO:
861fb55a 11905 dyn.d_un.d_val = g->local_gotno - htab->reserved_gotno;
b49e97c9
TS
11906 break;
11907
11908 case DT_MIPS_RLD_MAP:
b4082c70
DD
11909 {
11910 struct elf_link_hash_entry *h;
11911 h = mips_elf_hash_table (info)->rld_symbol;
11912 if (!h)
11913 {
11914 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11915 swap_out_p = FALSE;
11916 break;
11917 }
11918 s = h->root.u.def.section;
a5499fa4
MF
11919
11920 /* The MIPS_RLD_MAP tag stores the absolute address of the
11921 debug pointer. */
b4082c70
DD
11922 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
11923 + h->root.u.def.value);
11924 }
b49e97c9
TS
11925 break;
11926
a5499fa4
MF
11927 case DT_MIPS_RLD_MAP_REL:
11928 {
11929 struct elf_link_hash_entry *h;
11930 bfd_vma dt_addr, rld_addr;
11931 h = mips_elf_hash_table (info)->rld_symbol;
11932 if (!h)
11933 {
11934 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11935 swap_out_p = FALSE;
11936 break;
11937 }
11938 s = h->root.u.def.section;
11939
11940 /* The MIPS_RLD_MAP_REL tag stores the offset to the debug
11941 pointer, relative to the address of the tag. */
11942 dt_addr = (sdyn->output_section->vma + sdyn->output_offset
d5cff5df 11943 + (b - sdyn->contents));
a5499fa4
MF
11944 rld_addr = (s->output_section->vma + s->output_offset
11945 + h->root.u.def.value);
11946 dyn.d_un.d_ptr = rld_addr - dt_addr;
11947 }
11948 break;
11949
b49e97c9
TS
11950 case DT_MIPS_OPTIONS:
11951 s = (bfd_get_section_by_name
11952 (output_bfd, MIPS_ELF_OPTIONS_SECTION_NAME (output_bfd)));
11953 dyn.d_un.d_ptr = s->vma;
11954 break;
11955
0a44bf69 11956 case DT_PLTREL:
861fb55a 11957 BFD_ASSERT (htab->use_plts_and_copy_relocs);
90c14f0c 11958 if (htab->root.target_os == is_vxworks)
861fb55a
DJ
11959 dyn.d_un.d_val = DT_RELA;
11960 else
11961 dyn.d_un.d_val = DT_REL;
0a44bf69
RS
11962 break;
11963
11964 case DT_PLTRELSZ:
861fb55a 11965 BFD_ASSERT (htab->use_plts_and_copy_relocs);
ce558b89 11966 dyn.d_un.d_val = htab->root.srelplt->size;
0a44bf69
RS
11967 break;
11968
11969 case DT_JMPREL:
861fb55a 11970 BFD_ASSERT (htab->use_plts_and_copy_relocs);
ce558b89
AM
11971 dyn.d_un.d_ptr = (htab->root.srelplt->output_section->vma
11972 + htab->root.srelplt->output_offset);
0a44bf69
RS
11973 break;
11974
943284cc
DJ
11975 case DT_TEXTREL:
11976 /* If we didn't need any text relocations after all, delete
11977 the dynamic tag. */
11978 if (!(info->flags & DF_TEXTREL))
11979 {
11980 dyn_to_skip = MIPS_ELF_DYN_SIZE (dynobj);
11981 swap_out_p = FALSE;
11982 }
11983 break;
11984
11985 case DT_FLAGS:
11986 /* If we didn't need any text relocations after all, clear
11987 DF_TEXTREL from DT_FLAGS. */
11988 if (!(info->flags & DF_TEXTREL))
11989 dyn.d_un.d_val &= ~DF_TEXTREL;
11990 else
11991 swap_out_p = FALSE;
11992 break;
11993
f16a9783
MS
11994 case DT_MIPS_XHASH:
11995 name = ".MIPS.xhash";
11996 s = bfd_get_linker_section (dynobj, name);
11997 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11998 break;
11999
b49e97c9 12000 default:
b34976b6 12001 swap_out_p = FALSE;
90c14f0c 12002 if (htab->root.target_os == is_vxworks
7a2b07ff
NS
12003 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
12004 swap_out_p = TRUE;
b49e97c9
TS
12005 break;
12006 }
12007
943284cc 12008 if (swap_out_p || dyn_skipped)
b49e97c9 12009 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
943284cc
DJ
12010 (dynobj, &dyn, b - dyn_skipped);
12011
12012 if (dyn_to_skip)
12013 {
12014 dyn_skipped += dyn_to_skip;
12015 dyn_to_skip = 0;
12016 }
b49e97c9 12017 }
943284cc
DJ
12018
12019 /* Wipe out any trailing entries if we shifted down a dynamic tag. */
12020 if (dyn_skipped > 0)
12021 memset (b - dyn_skipped, 0, dyn_skipped);
b49e97c9
TS
12022 }
12023
b55fd4d4
DJ
12024 if (sgot != NULL && sgot->size > 0
12025 && !bfd_is_abs_section (sgot->output_section))
b49e97c9 12026 {
90c14f0c 12027 if (htab->root.target_os == is_vxworks)
0a44bf69
RS
12028 {
12029 /* The first entry of the global offset table points to the
12030 ".dynamic" section. The second is initialized by the
12031 loader and contains the shared library identifier.
12032 The third is also initialized by the loader and points
12033 to the lazy resolution stub. */
12034 MIPS_ELF_PUT_WORD (output_bfd,
12035 sdyn->output_offset + sdyn->output_section->vma,
12036 sgot->contents);
12037 MIPS_ELF_PUT_WORD (output_bfd, 0,
12038 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12039 MIPS_ELF_PUT_WORD (output_bfd, 0,
12040 sgot->contents
12041 + 2 * MIPS_ELF_GOT_SIZE (output_bfd));
12042 }
12043 else
12044 {
12045 /* The first entry of the global offset table will be filled at
12046 runtime. The second entry will be used by some runtime loaders.
12047 This isn't the case of IRIX rld. */
12048 MIPS_ELF_PUT_WORD (output_bfd, (bfd_vma) 0, sgot->contents);
51e38d68 12049 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
0a44bf69
RS
12050 sgot->contents + MIPS_ELF_GOT_SIZE (output_bfd));
12051 }
b49e97c9 12052
54938e2a
TS
12053 elf_section_data (sgot->output_section)->this_hdr.sh_entsize
12054 = MIPS_ELF_GOT_SIZE (output_bfd);
12055 }
b49e97c9 12056
f4416af6
AO
12057 /* Generate dynamic relocations for the non-primary gots. */
12058 if (gg != NULL && gg->next)
12059 {
12060 Elf_Internal_Rela rel[3];
12061 bfd_vma addend = 0;
12062
12063 memset (rel, 0, sizeof (rel));
12064 rel[0].r_info = ELF_R_INFO (output_bfd, 0, R_MIPS_REL32);
12065
12066 for (g = gg->next; g->next != gg; g = g->next)
12067 {
91d6fa6a 12068 bfd_vma got_index = g->next->local_gotno + g->next->global_gotno
0f20cc35 12069 + g->next->tls_gotno;
f4416af6 12070
9719ad41 12071 MIPS_ELF_PUT_WORD (output_bfd, 0, sgot->contents
91d6fa6a 12072 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
51e38d68
RS
12073 MIPS_ELF_PUT_WORD (output_bfd, MIPS_ELF_GNU_GOT1_MASK (output_bfd),
12074 sgot->contents
91d6fa6a 12075 + got_index++ * MIPS_ELF_GOT_SIZE (output_bfd));
f4416af6 12076
0e1862bb 12077 if (! bfd_link_pic (info))
f4416af6
AO
12078 continue;
12079
cb22ccf4 12080 for (; got_index < g->local_gotno; got_index++)
f4416af6 12081 {
cb22ccf4
KCY
12082 if (got_index >= g->assigned_low_gotno
12083 && got_index <= g->assigned_high_gotno)
12084 continue;
12085
f4416af6 12086 rel[0].r_offset = rel[1].r_offset = rel[2].r_offset
cb22ccf4 12087 = got_index * MIPS_ELF_GOT_SIZE (output_bfd);
f4416af6
AO
12088 if (!(mips_elf_create_dynamic_relocation
12089 (output_bfd, info, rel, NULL,
12090 bfd_abs_section_ptr,
12091 0, &addend, sgot)))
12092 return FALSE;
12093 BFD_ASSERT (addend == 0);
12094 }
12095 }
12096 }
12097
3133ddbf
DJ
12098 /* The generation of dynamic relocations for the non-primary gots
12099 adds more dynamic relocations. We cannot count them until
12100 here. */
12101
12102 if (elf_hash_table (info)->dynamic_sections_created)
12103 {
12104 bfd_byte *b;
12105 bfd_boolean swap_out_p;
12106
12107 BFD_ASSERT (sdyn != NULL);
12108
12109 for (b = sdyn->contents;
12110 b < sdyn->contents + sdyn->size;
12111 b += MIPS_ELF_DYN_SIZE (dynobj))
12112 {
12113 Elf_Internal_Dyn dyn;
12114 asection *s;
12115
12116 /* Read in the current dynamic entry. */
12117 (*get_elf_backend_data (dynobj)->s->swap_dyn_in) (dynobj, b, &dyn);
12118
12119 /* Assume that we're going to modify it and write it out. */
12120 swap_out_p = TRUE;
12121
12122 switch (dyn.d_tag)
12123 {
12124 case DT_RELSZ:
12125 /* Reduce DT_RELSZ to account for any relocations we
12126 decided not to make. This is for the n64 irix rld,
12127 which doesn't seem to apply any relocations if there
12128 are trailing null entries. */
0a44bf69 12129 s = mips_elf_rel_dyn_section (info, FALSE);
3133ddbf
DJ
12130 dyn.d_un.d_val = (s->reloc_count
12131 * (ABI_64_P (output_bfd)
12132 ? sizeof (Elf64_Mips_External_Rel)
12133 : sizeof (Elf32_External_Rel)));
bcfdf036
RS
12134 /* Adjust the section size too. Tools like the prelinker
12135 can reasonably expect the values to the same. */
db841b6f 12136 BFD_ASSERT (!bfd_is_abs_section (s->output_section));
bcfdf036
RS
12137 elf_section_data (s->output_section)->this_hdr.sh_size
12138 = dyn.d_un.d_val;
3133ddbf
DJ
12139 break;
12140
12141 default:
12142 swap_out_p = FALSE;
12143 break;
12144 }
12145
12146 if (swap_out_p)
12147 (*get_elf_backend_data (dynobj)->s->swap_dyn_out)
12148 (dynobj, &dyn, b);
12149 }
12150 }
12151
b49e97c9 12152 {
b49e97c9
TS
12153 asection *s;
12154 Elf32_compact_rel cpt;
12155
b49e97c9
TS
12156 if (SGI_COMPAT (output_bfd))
12157 {
12158 /* Write .compact_rel section out. */
3d4d4302 12159 s = bfd_get_linker_section (dynobj, ".compact_rel");
b49e97c9
TS
12160 if (s != NULL)
12161 {
12162 cpt.id1 = 1;
12163 cpt.num = s->reloc_count;
12164 cpt.id2 = 2;
12165 cpt.offset = (s->output_section->filepos
12166 + sizeof (Elf32_External_compact_rel));
12167 cpt.reserved0 = 0;
12168 cpt.reserved1 = 0;
12169 bfd_elf32_swap_compact_rel_out (output_bfd, &cpt,
12170 ((Elf32_External_compact_rel *)
12171 s->contents));
12172
12173 /* Clean up a dummy stub function entry in .text. */
4e41d0d7 12174 if (htab->sstubs != NULL)
b49e97c9
TS
12175 {
12176 file_ptr dummy_offset;
12177
4e41d0d7
RS
12178 BFD_ASSERT (htab->sstubs->size >= htab->function_stub_size);
12179 dummy_offset = htab->sstubs->size - htab->function_stub_size;
12180 memset (htab->sstubs->contents + dummy_offset, 0,
5108fc1b 12181 htab->function_stub_size);
b49e97c9
TS
12182 }
12183 }
12184 }
12185
0a44bf69
RS
12186 /* The psABI says that the dynamic relocations must be sorted in
12187 increasing order of r_symndx. The VxWorks EABI doesn't require
12188 this, and because the code below handles REL rather than RELA
12189 relocations, using it for VxWorks would be outright harmful. */
90c14f0c 12190 if (htab->root.target_os != is_vxworks)
b49e97c9 12191 {
0a44bf69
RS
12192 s = mips_elf_rel_dyn_section (info, FALSE);
12193 if (s != NULL
12194 && s->size > (bfd_vma)2 * MIPS_ELF_REL_SIZE (output_bfd))
12195 {
12196 reldyn_sorting_bfd = output_bfd;
b49e97c9 12197
0a44bf69
RS
12198 if (ABI_64_P (output_bfd))
12199 qsort ((Elf64_External_Rel *) s->contents + 1,
12200 s->reloc_count - 1, sizeof (Elf64_Mips_External_Rel),
12201 sort_dynamic_relocs_64);
12202 else
12203 qsort ((Elf32_External_Rel *) s->contents + 1,
12204 s->reloc_count - 1, sizeof (Elf32_External_Rel),
12205 sort_dynamic_relocs);
12206 }
b49e97c9 12207 }
b49e97c9
TS
12208 }
12209
ce558b89 12210 if (htab->root.splt && htab->root.splt->size > 0)
0a44bf69 12211 {
90c14f0c 12212 if (htab->root.target_os == is_vxworks)
861fb55a 12213 {
0e1862bb 12214 if (bfd_link_pic (info))
861fb55a
DJ
12215 mips_vxworks_finish_shared_plt (output_bfd, info);
12216 else
12217 mips_vxworks_finish_exec_plt (output_bfd, info);
12218 }
0a44bf69 12219 else
861fb55a 12220 {
0e1862bb 12221 BFD_ASSERT (!bfd_link_pic (info));
1bbce132
MR
12222 if (!mips_finish_exec_plt (output_bfd, info))
12223 return FALSE;
861fb55a 12224 }
0a44bf69 12225 }
b34976b6 12226 return TRUE;
b49e97c9
TS
12227}
12228
b49e97c9 12229
64543e1a
RS
12230/* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
12231
12232static void
9719ad41 12233mips_set_isa_flags (bfd *abfd)
b49e97c9 12234{
64543e1a 12235 flagword val;
b49e97c9
TS
12236
12237 switch (bfd_get_mach (abfd))
12238 {
12239 default:
c7c860d2
YS
12240 if (ABI_N32_P (abfd) || ABI_64_P (abfd))
12241 val = E_MIPS_ARCH_3;
12242 else
12243 val = E_MIPS_ARCH_1;
12244 break;
12245
b49e97c9
TS
12246 case bfd_mach_mips3000:
12247 val = E_MIPS_ARCH_1;
12248 break;
12249
12250 case bfd_mach_mips3900:
12251 val = E_MIPS_ARCH_1 | E_MIPS_MACH_3900;
12252 break;
12253
12254 case bfd_mach_mips6000:
12255 val = E_MIPS_ARCH_2;
12256 break;
12257
b417536f
MR
12258 case bfd_mach_mips4010:
12259 val = E_MIPS_ARCH_2 | E_MIPS_MACH_4010;
12260 break;
12261
b49e97c9
TS
12262 case bfd_mach_mips4000:
12263 case bfd_mach_mips4300:
12264 case bfd_mach_mips4400:
12265 case bfd_mach_mips4600:
12266 val = E_MIPS_ARCH_3;
12267 break;
12268
b49e97c9
TS
12269 case bfd_mach_mips4100:
12270 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4100;
12271 break;
12272
12273 case bfd_mach_mips4111:
12274 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4111;
12275 break;
12276
00707a0e
RS
12277 case bfd_mach_mips4120:
12278 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4120;
12279 break;
12280
b49e97c9
TS
12281 case bfd_mach_mips4650:
12282 val = E_MIPS_ARCH_3 | E_MIPS_MACH_4650;
12283 break;
12284
00707a0e
RS
12285 case bfd_mach_mips5400:
12286 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5400;
12287 break;
12288
12289 case bfd_mach_mips5500:
12290 val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
12291 break;
12292
e407c74b
NC
12293 case bfd_mach_mips5900:
12294 val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
12295 break;
12296
0d2e43ed
ILT
12297 case bfd_mach_mips9000:
12298 val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
12299 break;
12300
b49e97c9 12301 case bfd_mach_mips5000:
5a7ea749 12302 case bfd_mach_mips7000:
b49e97c9
TS
12303 case bfd_mach_mips8000:
12304 case bfd_mach_mips10000:
12305 case bfd_mach_mips12000:
3aa3176b
TS
12306 case bfd_mach_mips14000:
12307 case bfd_mach_mips16000:
b49e97c9
TS
12308 val = E_MIPS_ARCH_4;
12309 break;
12310
12311 case bfd_mach_mips5:
12312 val = E_MIPS_ARCH_5;
12313 break;
12314
350cc38d
MS
12315 case bfd_mach_mips_loongson_2e:
12316 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2E;
12317 break;
12318
12319 case bfd_mach_mips_loongson_2f:
12320 val = E_MIPS_ARCH_3 | E_MIPS_MACH_LS2F;
12321 break;
12322
b49e97c9
TS
12323 case bfd_mach_mips_sb1:
12324 val = E_MIPS_ARCH_64 | E_MIPS_MACH_SB1;
12325 break;
12326
ac8cb70f
CX
12327 case bfd_mach_mips_gs464:
12328 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464;
d051516a
NC
12329 break;
12330
bd782c07
CX
12331 case bfd_mach_mips_gs464e:
12332 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS464E;
12333 break;
12334
9108bc33
CX
12335 case bfd_mach_mips_gs264e:
12336 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_GS264E;
12337 break;
12338
6f179bd0 12339 case bfd_mach_mips_octeon:
dd6a37e7 12340 case bfd_mach_mips_octeonp:
6f179bd0
AN
12341 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON;
12342 break;
12343
2c629856
N
12344 case bfd_mach_mips_octeon3:
12345 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON3;
12346 break;
12347
52b6b6b9
JM
12348 case bfd_mach_mips_xlr:
12349 val = E_MIPS_ARCH_64 | E_MIPS_MACH_XLR;
12350 break;
12351
432233b3
AP
12352 case bfd_mach_mips_octeon2:
12353 val = E_MIPS_ARCH_64R2 | E_MIPS_MACH_OCTEON2;
12354 break;
12355
b49e97c9
TS
12356 case bfd_mach_mipsisa32:
12357 val = E_MIPS_ARCH_32;
12358 break;
12359
12360 case bfd_mach_mipsisa64:
12361 val = E_MIPS_ARCH_64;
af7ee8bf
CD
12362 break;
12363
12364 case bfd_mach_mipsisa32r2:
ae52f483
AB
12365 case bfd_mach_mipsisa32r3:
12366 case bfd_mach_mipsisa32r5:
af7ee8bf
CD
12367 val = E_MIPS_ARCH_32R2;
12368 break;
5f74bc13 12369
38bf472a
MR
12370 case bfd_mach_mips_interaptiv_mr2:
12371 val = E_MIPS_ARCH_32R2 | E_MIPS_MACH_IAMR2;
12372 break;
12373
5f74bc13 12374 case bfd_mach_mipsisa64r2:
ae52f483
AB
12375 case bfd_mach_mipsisa64r3:
12376 case bfd_mach_mipsisa64r5:
5f74bc13
CD
12377 val = E_MIPS_ARCH_64R2;
12378 break;
7361da2c
AB
12379
12380 case bfd_mach_mipsisa32r6:
12381 val = E_MIPS_ARCH_32R6;
12382 break;
12383
12384 case bfd_mach_mipsisa64r6:
12385 val = E_MIPS_ARCH_64R6;
12386 break;
b49e97c9 12387 }
b49e97c9
TS
12388 elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
12389 elf_elfheader (abfd)->e_flags |= val;
12390
64543e1a
RS
12391}
12392
12393
28dbcedc
AM
12394/* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
12395 Don't do so for code sections. We want to keep ordering of HI16/LO16
12396 as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
12397 relocs to be sorted. */
12398
12399bfd_boolean
12400_bfd_mips_elf_sort_relocs_p (asection *sec)
12401{
12402 return (sec->flags & SEC_CODE) == 0;
12403}
12404
12405
64543e1a
RS
12406/* The final processing done just before writing out a MIPS ELF object
12407 file. This gets the MIPS architecture right based on the machine
12408 number. This is used by both the 32-bit and the 64-bit ABI. */
12409
12410void
cc364be6 12411_bfd_mips_final_write_processing (bfd *abfd)
64543e1a
RS
12412{
12413 unsigned int i;
12414 Elf_Internal_Shdr **hdrpp;
12415 const char *name;
12416 asection *sec;
12417
12418 /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
12419 is nonzero. This is for compatibility with old objects, which used
12420 a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
12421 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
12422 mips_set_isa_flags (abfd);
12423
b49e97c9
TS
12424 /* Set the sh_info field for .gptab sections and other appropriate
12425 info for each special section. */
12426 for (i = 1, hdrpp = elf_elfsections (abfd) + 1;
12427 i < elf_numsections (abfd);
12428 i++, hdrpp++)
12429 {
12430 switch ((*hdrpp)->sh_type)
12431 {
12432 case SHT_MIPS_MSYM:
12433 case SHT_MIPS_LIBLIST:
12434 sec = bfd_get_section_by_name (abfd, ".dynstr");
12435 if (sec != NULL)
12436 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12437 break;
12438
12439 case SHT_MIPS_GPTAB:
12440 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
fd361982 12441 name = bfd_section_name ((*hdrpp)->bfd_section);
b49e97c9 12442 BFD_ASSERT (name != NULL
0112cd26 12443 && CONST_STRNEQ (name, ".gptab."));
b49e97c9
TS
12444 sec = bfd_get_section_by_name (abfd, name + sizeof ".gptab" - 1);
12445 BFD_ASSERT (sec != NULL);
12446 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12447 break;
12448
12449 case SHT_MIPS_CONTENT:
12450 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
fd361982 12451 name = bfd_section_name ((*hdrpp)->bfd_section);
b49e97c9 12452 BFD_ASSERT (name != NULL
0112cd26 12453 && CONST_STRNEQ (name, ".MIPS.content"));
b49e97c9
TS
12454 sec = bfd_get_section_by_name (abfd,
12455 name + sizeof ".MIPS.content" - 1);
12456 BFD_ASSERT (sec != NULL);
12457 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12458 break;
12459
12460 case SHT_MIPS_SYMBOL_LIB:
12461 sec = bfd_get_section_by_name (abfd, ".dynsym");
12462 if (sec != NULL)
12463 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12464 sec = bfd_get_section_by_name (abfd, ".liblist");
12465 if (sec != NULL)
12466 (*hdrpp)->sh_info = elf_section_data (sec)->this_idx;
12467 break;
12468
12469 case SHT_MIPS_EVENTS:
12470 BFD_ASSERT ((*hdrpp)->bfd_section != NULL);
fd361982 12471 name = bfd_section_name ((*hdrpp)->bfd_section);
b49e97c9 12472 BFD_ASSERT (name != NULL);
0112cd26 12473 if (CONST_STRNEQ (name, ".MIPS.events"))
b49e97c9
TS
12474 sec = bfd_get_section_by_name (abfd,
12475 name + sizeof ".MIPS.events" - 1);
12476 else
12477 {
0112cd26 12478 BFD_ASSERT (CONST_STRNEQ (name, ".MIPS.post_rel"));
b49e97c9
TS
12479 sec = bfd_get_section_by_name (abfd,
12480 (name
12481 + sizeof ".MIPS.post_rel" - 1));
12482 }
12483 BFD_ASSERT (sec != NULL);
12484 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
12485 break;
12486
f16a9783
MS
12487 case SHT_MIPS_XHASH:
12488 sec = bfd_get_section_by_name (abfd, ".dynsym");
12489 if (sec != NULL)
12490 (*hdrpp)->sh_link = elf_section_data (sec)->this_idx;
b49e97c9
TS
12491 }
12492 }
12493}
06f44071 12494
cc364be6
AM
12495bfd_boolean
12496_bfd_mips_elf_final_write_processing (bfd *abfd)
06f44071 12497{
cc364be6
AM
12498 _bfd_mips_final_write_processing (abfd);
12499 return _bfd_elf_final_write_processing (abfd);
06f44071 12500}
b49e97c9 12501\f
8dc1a139 12502/* When creating an IRIX5 executable, we need REGINFO and RTPROC
b49e97c9
TS
12503 segments. */
12504
12505int
a6b96beb
AM
12506_bfd_mips_elf_additional_program_headers (bfd *abfd,
12507 struct bfd_link_info *info ATTRIBUTE_UNUSED)
b49e97c9
TS
12508{
12509 asection *s;
12510 int ret = 0;
12511
12512 /* See if we need a PT_MIPS_REGINFO segment. */
12513 s = bfd_get_section_by_name (abfd, ".reginfo");
12514 if (s && (s->flags & SEC_LOAD))
12515 ++ret;
12516
351cdf24
MF
12517 /* See if we need a PT_MIPS_ABIFLAGS segment. */
12518 if (bfd_get_section_by_name (abfd, ".MIPS.abiflags"))
12519 ++ret;
12520
b49e97c9
TS
12521 /* See if we need a PT_MIPS_OPTIONS segment. */
12522 if (IRIX_COMPAT (abfd) == ict_irix6
12523 && bfd_get_section_by_name (abfd,
12524 MIPS_ELF_OPTIONS_SECTION_NAME (abfd)))
12525 ++ret;
12526
12527 /* See if we need a PT_MIPS_RTPROC segment. */
12528 if (IRIX_COMPAT (abfd) == ict_irix5
12529 && bfd_get_section_by_name (abfd, ".dynamic")
12530 && bfd_get_section_by_name (abfd, ".mdebug"))
12531 ++ret;
12532
98c904a8
RS
12533 /* Allocate a PT_NULL header in dynamic objects. See
12534 _bfd_mips_elf_modify_segment_map for details. */
12535 if (!SGI_COMPAT (abfd)
12536 && bfd_get_section_by_name (abfd, ".dynamic"))
12537 ++ret;
12538
b49e97c9
TS
12539 return ret;
12540}
12541
8dc1a139 12542/* Modify the segment map for an IRIX5 executable. */
b49e97c9 12543
b34976b6 12544bfd_boolean
9719ad41 12545_bfd_mips_elf_modify_segment_map (bfd *abfd,
7c8b76cc 12546 struct bfd_link_info *info)
b49e97c9
TS
12547{
12548 asection *s;
12549 struct elf_segment_map *m, **pm;
986f0783 12550 size_t amt;
b49e97c9
TS
12551
12552 /* If there is a .reginfo section, we need a PT_MIPS_REGINFO
12553 segment. */
12554 s = bfd_get_section_by_name (abfd, ".reginfo");
12555 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12556 {
12bd6957 12557 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
12558 if (m->p_type == PT_MIPS_REGINFO)
12559 break;
12560 if (m == NULL)
12561 {
12562 amt = sizeof *m;
9719ad41 12563 m = bfd_zalloc (abfd, amt);
b49e97c9 12564 if (m == NULL)
b34976b6 12565 return FALSE;
b49e97c9
TS
12566
12567 m->p_type = PT_MIPS_REGINFO;
12568 m->count = 1;
12569 m->sections[0] = s;
12570
12571 /* We want to put it after the PHDR and INTERP segments. */
12bd6957 12572 pm = &elf_seg_map (abfd);
b49e97c9
TS
12573 while (*pm != NULL
12574 && ((*pm)->p_type == PT_PHDR
12575 || (*pm)->p_type == PT_INTERP))
12576 pm = &(*pm)->next;
12577
12578 m->next = *pm;
12579 *pm = m;
12580 }
12581 }
12582
351cdf24
MF
12583 /* If there is a .MIPS.abiflags section, we need a PT_MIPS_ABIFLAGS
12584 segment. */
12585 s = bfd_get_section_by_name (abfd, ".MIPS.abiflags");
12586 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12587 {
12588 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
12589 if (m->p_type == PT_MIPS_ABIFLAGS)
12590 break;
12591 if (m == NULL)
12592 {
12593 amt = sizeof *m;
12594 m = bfd_zalloc (abfd, amt);
12595 if (m == NULL)
12596 return FALSE;
12597
12598 m->p_type = PT_MIPS_ABIFLAGS;
12599 m->count = 1;
12600 m->sections[0] = s;
12601
12602 /* We want to put it after the PHDR and INTERP segments. */
12603 pm = &elf_seg_map (abfd);
12604 while (*pm != NULL
12605 && ((*pm)->p_type == PT_PHDR
12606 || (*pm)->p_type == PT_INTERP))
12607 pm = &(*pm)->next;
12608
12609 m->next = *pm;
12610 *pm = m;
12611 }
12612 }
12613
b49e97c9
TS
12614 /* For IRIX 6, we don't have .mdebug sections, nor does anything but
12615 .dynamic end up in PT_DYNAMIC. However, we do have to insert a
98a8deaf 12616 PT_MIPS_OPTIONS segment immediately following the program header
b49e97c9 12617 table. */
c1fd6598
AO
12618 if (NEWABI_P (abfd)
12619 /* On non-IRIX6 new abi, we'll have already created a segment
12620 for this section, so don't create another. I'm not sure this
12621 is not also the case for IRIX 6, but I can't test it right
12622 now. */
12623 && IRIX_COMPAT (abfd) == ict_irix6)
b49e97c9
TS
12624 {
12625 for (s = abfd->sections; s; s = s->next)
12626 if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
12627 break;
12628
12629 if (s)
12630 {
12631 struct elf_segment_map *options_segment;
12632
12bd6957 12633 pm = &elf_seg_map (abfd);
98a8deaf
RS
12634 while (*pm != NULL
12635 && ((*pm)->p_type == PT_PHDR
12636 || (*pm)->p_type == PT_INTERP))
12637 pm = &(*pm)->next;
b49e97c9 12638
8ded5a0f
AM
12639 if (*pm == NULL || (*pm)->p_type != PT_MIPS_OPTIONS)
12640 {
12641 amt = sizeof (struct elf_segment_map);
12642 options_segment = bfd_zalloc (abfd, amt);
12643 options_segment->next = *pm;
12644 options_segment->p_type = PT_MIPS_OPTIONS;
12645 options_segment->p_flags = PF_R;
12646 options_segment->p_flags_valid = TRUE;
12647 options_segment->count = 1;
12648 options_segment->sections[0] = s;
12649 *pm = options_segment;
12650 }
b49e97c9
TS
12651 }
12652 }
12653 else
12654 {
12655 if (IRIX_COMPAT (abfd) == ict_irix5)
12656 {
12657 /* If there are .dynamic and .mdebug sections, we make a room
12658 for the RTPROC header. FIXME: Rewrite without section names. */
12659 if (bfd_get_section_by_name (abfd, ".interp") == NULL
12660 && bfd_get_section_by_name (abfd, ".dynamic") != NULL
12661 && bfd_get_section_by_name (abfd, ".mdebug") != NULL)
12662 {
12bd6957 12663 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
b49e97c9
TS
12664 if (m->p_type == PT_MIPS_RTPROC)
12665 break;
12666 if (m == NULL)
12667 {
12668 amt = sizeof *m;
9719ad41 12669 m = bfd_zalloc (abfd, amt);
b49e97c9 12670 if (m == NULL)
b34976b6 12671 return FALSE;
b49e97c9
TS
12672
12673 m->p_type = PT_MIPS_RTPROC;
12674
12675 s = bfd_get_section_by_name (abfd, ".rtproc");
12676 if (s == NULL)
12677 {
12678 m->count = 0;
12679 m->p_flags = 0;
12680 m->p_flags_valid = 1;
12681 }
12682 else
12683 {
12684 m->count = 1;
12685 m->sections[0] = s;
12686 }
12687
12688 /* We want to put it after the DYNAMIC segment. */
12bd6957 12689 pm = &elf_seg_map (abfd);
b49e97c9
TS
12690 while (*pm != NULL && (*pm)->p_type != PT_DYNAMIC)
12691 pm = &(*pm)->next;
12692 if (*pm != NULL)
12693 pm = &(*pm)->next;
12694
12695 m->next = *pm;
12696 *pm = m;
12697 }
12698 }
12699 }
8dc1a139 12700 /* On IRIX5, the PT_DYNAMIC segment includes the .dynamic,
b49e97c9
TS
12701 .dynstr, .dynsym, and .hash sections, and everything in
12702 between. */
12bd6957 12703 for (pm = &elf_seg_map (abfd); *pm != NULL;
b49e97c9
TS
12704 pm = &(*pm)->next)
12705 if ((*pm)->p_type == PT_DYNAMIC)
12706 break;
12707 m = *pm;
f6f62d6f
RS
12708 /* GNU/Linux binaries do not need the extended PT_DYNAMIC section.
12709 glibc's dynamic linker has traditionally derived the number of
12710 tags from the p_filesz field, and sometimes allocates stack
12711 arrays of that size. An overly-big PT_DYNAMIC segment can
12712 be actively harmful in such cases. Making PT_DYNAMIC contain
12713 other sections can also make life hard for the prelinker,
12714 which might move one of the other sections to a different
12715 PT_LOAD segment. */
12716 if (SGI_COMPAT (abfd)
12717 && m != NULL
12718 && m->count == 1
12719 && strcmp (m->sections[0]->name, ".dynamic") == 0)
b49e97c9
TS
12720 {
12721 static const char *sec_names[] =
12722 {
12723 ".dynamic", ".dynstr", ".dynsym", ".hash"
12724 };
12725 bfd_vma low, high;
12726 unsigned int i, c;
12727 struct elf_segment_map *n;
12728
792b4a53 12729 low = ~(bfd_vma) 0;
b49e97c9
TS
12730 high = 0;
12731 for (i = 0; i < sizeof sec_names / sizeof sec_names[0]; i++)
12732 {
12733 s = bfd_get_section_by_name (abfd, sec_names[i]);
12734 if (s != NULL && (s->flags & SEC_LOAD) != 0)
12735 {
12736 bfd_size_type sz;
12737
12738 if (low > s->vma)
12739 low = s->vma;
eea6121a 12740 sz = s->size;
b49e97c9
TS
12741 if (high < s->vma + sz)
12742 high = s->vma + sz;
12743 }
12744 }
12745
12746 c = 0;
12747 for (s = abfd->sections; s != NULL; s = s->next)
12748 if ((s->flags & SEC_LOAD) != 0
12749 && s->vma >= low
eea6121a 12750 && s->vma + s->size <= high)
b49e97c9
TS
12751 ++c;
12752
986f0783 12753 amt = sizeof *n - sizeof (asection *) + c * sizeof (asection *);
9719ad41 12754 n = bfd_zalloc (abfd, amt);
b49e97c9 12755 if (n == NULL)
b34976b6 12756 return FALSE;
b49e97c9
TS
12757 *n = *m;
12758 n->count = c;
12759
12760 i = 0;
12761 for (s = abfd->sections; s != NULL; s = s->next)
12762 {
12763 if ((s->flags & SEC_LOAD) != 0
12764 && s->vma >= low
eea6121a 12765 && s->vma + s->size <= high)
b49e97c9
TS
12766 {
12767 n->sections[i] = s;
12768 ++i;
12769 }
12770 }
12771
12772 *pm = n;
12773 }
12774 }
12775
98c904a8
RS
12776 /* Allocate a spare program header in dynamic objects so that tools
12777 like the prelinker can add an extra PT_LOAD entry.
12778
12779 If the prelinker needs to make room for a new PT_LOAD entry, its
12780 standard procedure is to move the first (read-only) sections into
12781 the new (writable) segment. However, the MIPS ABI requires
12782 .dynamic to be in a read-only segment, and the section will often
12783 start within sizeof (ElfNN_Phdr) bytes of the last program header.
12784
12785 Although the prelinker could in principle move .dynamic to a
12786 writable segment, it seems better to allocate a spare program
12787 header instead, and avoid the need to move any sections.
12788 There is a long tradition of allocating spare dynamic tags,
12789 so allocating a spare program header seems like a natural
7c8b76cc
JM
12790 extension.
12791
12792 If INFO is NULL, we may be copying an already prelinked binary
12793 with objcopy or strip, so do not add this header. */
12794 if (info != NULL
12795 && !SGI_COMPAT (abfd)
98c904a8
RS
12796 && bfd_get_section_by_name (abfd, ".dynamic"))
12797 {
12bd6957 12798 for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
98c904a8
RS
12799 if ((*pm)->p_type == PT_NULL)
12800 break;
12801 if (*pm == NULL)
12802 {
12803 m = bfd_zalloc (abfd, sizeof (*m));
12804 if (m == NULL)
12805 return FALSE;
12806
12807 m->p_type = PT_NULL;
12808 *pm = m;
12809 }
12810 }
12811
b34976b6 12812 return TRUE;
b49e97c9
TS
12813}
12814\f
12815/* Return the section that should be marked against GC for a given
12816 relocation. */
12817
12818asection *
9719ad41 12819_bfd_mips_elf_gc_mark_hook (asection *sec,
07adf181 12820 struct bfd_link_info *info,
9719ad41
RS
12821 Elf_Internal_Rela *rel,
12822 struct elf_link_hash_entry *h,
12823 Elf_Internal_Sym *sym)
b49e97c9
TS
12824{
12825 /* ??? Do mips16 stub sections need to be handled special? */
12826
12827 if (h != NULL)
07adf181
AM
12828 switch (ELF_R_TYPE (sec->owner, rel->r_info))
12829 {
12830 case R_MIPS_GNU_VTINHERIT:
12831 case R_MIPS_GNU_VTENTRY:
12832 return NULL;
12833 }
b49e97c9 12834
07adf181 12835 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
b49e97c9
TS
12836}
12837
351cdf24
MF
12838/* Prevent .MIPS.abiflags from being discarded with --gc-sections. */
12839
12840bfd_boolean
12841_bfd_mips_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12842 elf_gc_mark_hook_fn gc_mark_hook)
12843{
12844 bfd *sub;
12845
12846 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
12847
12848 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12849 {
12850 asection *o;
12851
12852 if (! is_mips_elf (sub))
12853 continue;
12854
12855 for (o = sub->sections; o != NULL; o = o->next)
12856 if (!o->gc_mark
fd361982 12857 && MIPS_ELF_ABIFLAGS_SECTION_NAME_P (bfd_section_name (o)))
351cdf24
MF
12858 {
12859 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12860 return FALSE;
12861 }
12862 }
12863
12864 return TRUE;
12865}
b49e97c9
TS
12866\f
12867/* Copy data from a MIPS ELF indirect symbol to its direct symbol,
12868 hiding the old indirect symbol. Process additional relocation
12869 information. Also called for weakdefs, in which case we just let
12870 _bfd_elf_link_hash_copy_indirect copy the flags for us. */
12871
12872void
fcfa13d2 12873_bfd_mips_elf_copy_indirect_symbol (struct bfd_link_info *info,
9719ad41
RS
12874 struct elf_link_hash_entry *dir,
12875 struct elf_link_hash_entry *ind)
b49e97c9
TS
12876{
12877 struct mips_elf_link_hash_entry *dirmips, *indmips;
12878
fcfa13d2 12879 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
b49e97c9 12880
861fb55a
DJ
12881 dirmips = (struct mips_elf_link_hash_entry *) dir;
12882 indmips = (struct mips_elf_link_hash_entry *) ind;
12883 /* Any absolute non-dynamic relocations against an indirect or weak
12884 definition will be against the target symbol. */
12885 if (indmips->has_static_relocs)
12886 dirmips->has_static_relocs = TRUE;
12887
b49e97c9
TS
12888 if (ind->root.type != bfd_link_hash_indirect)
12889 return;
12890
b49e97c9
TS
12891 dirmips->possibly_dynamic_relocs += indmips->possibly_dynamic_relocs;
12892 if (indmips->readonly_reloc)
b34976b6 12893 dirmips->readonly_reloc = TRUE;
b49e97c9 12894 if (indmips->no_fn_stub)
b34976b6 12895 dirmips->no_fn_stub = TRUE;
61b0a4af
RS
12896 if (indmips->fn_stub)
12897 {
12898 dirmips->fn_stub = indmips->fn_stub;
12899 indmips->fn_stub = NULL;
12900 }
12901 if (indmips->need_fn_stub)
12902 {
12903 dirmips->need_fn_stub = TRUE;
12904 indmips->need_fn_stub = FALSE;
12905 }
12906 if (indmips->call_stub)
12907 {
12908 dirmips->call_stub = indmips->call_stub;
12909 indmips->call_stub = NULL;
12910 }
12911 if (indmips->call_fp_stub)
12912 {
12913 dirmips->call_fp_stub = indmips->call_fp_stub;
12914 indmips->call_fp_stub = NULL;
12915 }
634835ae
RS
12916 if (indmips->global_got_area < dirmips->global_got_area)
12917 dirmips->global_got_area = indmips->global_got_area;
12918 if (indmips->global_got_area < GGA_NONE)
12919 indmips->global_got_area = GGA_NONE;
861fb55a
DJ
12920 if (indmips->has_nonpic_branches)
12921 dirmips->has_nonpic_branches = TRUE;
b49e97c9 12922}
47275900
MR
12923
12924/* Take care of the special `__gnu_absolute_zero' symbol and ignore attempts
12925 to hide it. It has to remain global (it will also be protected) so as to
12926 be assigned a global GOT entry, which will then remain unchanged at load
12927 time. */
12928
12929void
12930_bfd_mips_elf_hide_symbol (struct bfd_link_info *info,
12931 struct elf_link_hash_entry *entry,
12932 bfd_boolean force_local)
12933{
12934 struct mips_elf_link_hash_table *htab;
12935
12936 htab = mips_elf_hash_table (info);
12937 BFD_ASSERT (htab != NULL);
12938 if (htab->use_absolute_zero
12939 && strcmp (entry->root.root.string, "__gnu_absolute_zero") == 0)
12940 return;
12941
12942 _bfd_elf_link_hash_hide_symbol (info, entry, force_local);
12943}
b49e97c9 12944\f
d01414a5
TS
12945#define PDR_SIZE 32
12946
b34976b6 12947bfd_boolean
9719ad41
RS
12948_bfd_mips_elf_discard_info (bfd *abfd, struct elf_reloc_cookie *cookie,
12949 struct bfd_link_info *info)
d01414a5
TS
12950{
12951 asection *o;
b34976b6 12952 bfd_boolean ret = FALSE;
d01414a5
TS
12953 unsigned char *tdata;
12954 size_t i, skip;
12955
12956 o = bfd_get_section_by_name (abfd, ".pdr");
12957 if (! o)
b34976b6 12958 return FALSE;
eea6121a 12959 if (o->size == 0)
b34976b6 12960 return FALSE;
eea6121a 12961 if (o->size % PDR_SIZE != 0)
b34976b6 12962 return FALSE;
d01414a5
TS
12963 if (o->output_section != NULL
12964 && bfd_is_abs_section (o->output_section))
b34976b6 12965 return FALSE;
d01414a5 12966
eea6121a 12967 tdata = bfd_zmalloc (o->size / PDR_SIZE);
d01414a5 12968 if (! tdata)
b34976b6 12969 return FALSE;
d01414a5 12970
9719ad41 12971 cookie->rels = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
45d6a902 12972 info->keep_memory);
d01414a5
TS
12973 if (!cookie->rels)
12974 {
12975 free (tdata);
b34976b6 12976 return FALSE;
d01414a5
TS
12977 }
12978
12979 cookie->rel = cookie->rels;
12980 cookie->relend = cookie->rels + o->reloc_count;
12981
eea6121a 12982 for (i = 0, skip = 0; i < o->size / PDR_SIZE; i ++)
d01414a5 12983 {
c152c796 12984 if (bfd_elf_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
d01414a5
TS
12985 {
12986 tdata[i] = 1;
12987 skip ++;
12988 }
12989 }
12990
12991 if (skip != 0)
12992 {
f0abc2a1 12993 mips_elf_section_data (o)->u.tdata = tdata;
e034b2cc
MR
12994 if (o->rawsize == 0)
12995 o->rawsize = o->size;
eea6121a 12996 o->size -= skip * PDR_SIZE;
b34976b6 12997 ret = TRUE;
d01414a5
TS
12998 }
12999 else
13000 free (tdata);
13001
13002 if (! info->keep_memory)
13003 free (cookie->rels);
13004
13005 return ret;
13006}
13007
b34976b6 13008bfd_boolean
9719ad41 13009_bfd_mips_elf_ignore_discarded_relocs (asection *sec)
53bfd6b4
MR
13010{
13011 if (strcmp (sec->name, ".pdr") == 0)
b34976b6
AM
13012 return TRUE;
13013 return FALSE;
53bfd6b4 13014}
d01414a5 13015
b34976b6 13016bfd_boolean
c7b8f16e
JB
13017_bfd_mips_elf_write_section (bfd *output_bfd,
13018 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
07d6d2b8 13019 asection *sec, bfd_byte *contents)
d01414a5
TS
13020{
13021 bfd_byte *to, *from, *end;
13022 int i;
13023
13024 if (strcmp (sec->name, ".pdr") != 0)
b34976b6 13025 return FALSE;
d01414a5 13026
f0abc2a1 13027 if (mips_elf_section_data (sec)->u.tdata == NULL)
b34976b6 13028 return FALSE;
d01414a5
TS
13029
13030 to = contents;
eea6121a 13031 end = contents + sec->size;
d01414a5
TS
13032 for (from = contents, i = 0;
13033 from < end;
13034 from += PDR_SIZE, i++)
13035 {
f0abc2a1 13036 if ((mips_elf_section_data (sec)->u.tdata)[i] == 1)
d01414a5
TS
13037 continue;
13038 if (to != from)
13039 memcpy (to, from, PDR_SIZE);
13040 to += PDR_SIZE;
13041 }
13042 bfd_set_section_contents (output_bfd, sec->output_section, contents,
eea6121a 13043 sec->output_offset, sec->size);
b34976b6 13044 return TRUE;
d01414a5 13045}
53bfd6b4 13046\f
df58fc94
RS
13047/* microMIPS code retains local labels for linker relaxation. Omit them
13048 from output by default for clarity. */
13049
13050bfd_boolean
13051_bfd_mips_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
13052{
13053 return _bfd_elf_is_local_label_name (abfd, sym->name);
13054}
13055
b49e97c9
TS
13056/* MIPS ELF uses a special find_nearest_line routine in order the
13057 handle the ECOFF debugging information. */
13058
13059struct mips_elf_find_line
13060{
13061 struct ecoff_debug_info d;
13062 struct ecoff_find_line i;
13063};
13064
b34976b6 13065bfd_boolean
fb167eb2
AM
13066_bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
13067 asection *section, bfd_vma offset,
9719ad41
RS
13068 const char **filename_ptr,
13069 const char **functionname_ptr,
fb167eb2
AM
13070 unsigned int *line_ptr,
13071 unsigned int *discriminator_ptr)
b49e97c9
TS
13072{
13073 asection *msec;
13074
fb167eb2 13075 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
b49e97c9 13076 filename_ptr, functionname_ptr,
fb167eb2
AM
13077 line_ptr, discriminator_ptr,
13078 dwarf_debug_sections,
7f3bf384
AM
13079 &elf_tdata (abfd)->dwarf2_find_line_info)
13080 == 1)
e7679060 13081 return TRUE;
46d09186 13082
e7679060
AM
13083 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
13084 filename_ptr, functionname_ptr,
13085 line_ptr))
13086 {
13087 if (!*functionname_ptr)
13088 _bfd_elf_find_function (abfd, symbols, section, offset,
13089 *filename_ptr ? NULL : filename_ptr,
13090 functionname_ptr);
46d09186
NC
13091 return TRUE;
13092 }
b49e97c9
TS
13093
13094 msec = bfd_get_section_by_name (abfd, ".mdebug");
13095 if (msec != NULL)
13096 {
13097 flagword origflags;
13098 struct mips_elf_find_line *fi;
13099 const struct ecoff_debug_swap * const swap =
13100 get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
13101
13102 /* If we are called during a link, mips_elf_final_link may have
13103 cleared the SEC_HAS_CONTENTS field. We force it back on here
13104 if appropriate (which it normally will be). */
13105 origflags = msec->flags;
13106 if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
13107 msec->flags |= SEC_HAS_CONTENTS;
13108
698600e4 13109 fi = mips_elf_tdata (abfd)->find_line_info;
b49e97c9
TS
13110 if (fi == NULL)
13111 {
13112 bfd_size_type external_fdr_size;
13113 char *fraw_src;
13114 char *fraw_end;
13115 struct fdr *fdr_ptr;
13116 bfd_size_type amt = sizeof (struct mips_elf_find_line);
13117
9719ad41 13118 fi = bfd_zalloc (abfd, amt);
b49e97c9
TS
13119 if (fi == NULL)
13120 {
13121 msec->flags = origflags;
b34976b6 13122 return FALSE;
b49e97c9
TS
13123 }
13124
13125 if (! _bfd_mips_elf_read_ecoff_info (abfd, msec, &fi->d))
13126 {
13127 msec->flags = origflags;
b34976b6 13128 return FALSE;
b49e97c9
TS
13129 }
13130
13131 /* Swap in the FDR information. */
13132 amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
9719ad41 13133 fi->d.fdr = bfd_alloc (abfd, amt);
b49e97c9
TS
13134 if (fi->d.fdr == NULL)
13135 {
13136 msec->flags = origflags;
b34976b6 13137 return FALSE;
b49e97c9
TS
13138 }
13139 external_fdr_size = swap->external_fdr_size;
13140 fdr_ptr = fi->d.fdr;
13141 fraw_src = (char *) fi->d.external_fdr;
13142 fraw_end = (fraw_src
13143 + fi->d.symbolic_header.ifdMax * external_fdr_size);
13144 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
9719ad41 13145 (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
b49e97c9 13146
698600e4 13147 mips_elf_tdata (abfd)->find_line_info = fi;
b49e97c9
TS
13148
13149 /* Note that we don't bother to ever free this information.
07d6d2b8
AM
13150 find_nearest_line is either called all the time, as in
13151 objdump -l, so the information should be saved, or it is
13152 rarely called, as in ld error messages, so the memory
13153 wasted is unimportant. Still, it would probably be a
13154 good idea for free_cached_info to throw it away. */
b49e97c9
TS
13155 }
13156
13157 if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
13158 &fi->i, filename_ptr, functionname_ptr,
13159 line_ptr))
13160 {
13161 msec->flags = origflags;
b34976b6 13162 return TRUE;
b49e97c9
TS
13163 }
13164
13165 msec->flags = origflags;
13166 }
13167
13168 /* Fall back on the generic ELF find_nearest_line routine. */
13169
fb167eb2 13170 return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
b49e97c9 13171 filename_ptr, functionname_ptr,
fb167eb2 13172 line_ptr, discriminator_ptr);
b49e97c9 13173}
4ab527b0
FF
13174
13175bfd_boolean
13176_bfd_mips_elf_find_inliner_info (bfd *abfd,
13177 const char **filename_ptr,
13178 const char **functionname_ptr,
13179 unsigned int *line_ptr)
13180{
13181 bfd_boolean found;
13182 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
13183 functionname_ptr, line_ptr,
13184 & elf_tdata (abfd)->dwarf2_find_line_info);
13185 return found;
13186}
13187
b49e97c9
TS
13188\f
13189/* When are writing out the .options or .MIPS.options section,
13190 remember the bytes we are writing out, so that we can install the
13191 GP value in the section_processing routine. */
13192
b34976b6 13193bfd_boolean
9719ad41
RS
13194_bfd_mips_elf_set_section_contents (bfd *abfd, sec_ptr section,
13195 const void *location,
13196 file_ptr offset, bfd_size_type count)
b49e97c9 13197{
cc2e31b9 13198 if (MIPS_ELF_OPTIONS_SECTION_NAME_P (section->name))
b49e97c9
TS
13199 {
13200 bfd_byte *c;
13201
13202 if (elf_section_data (section) == NULL)
13203 {
986f0783 13204 size_t amt = sizeof (struct bfd_elf_section_data);
9719ad41 13205 section->used_by_bfd = bfd_zalloc (abfd, amt);
b49e97c9 13206 if (elf_section_data (section) == NULL)
b34976b6 13207 return FALSE;
b49e97c9 13208 }
f0abc2a1 13209 c = mips_elf_section_data (section)->u.tdata;
b49e97c9
TS
13210 if (c == NULL)
13211 {
eea6121a 13212 c = bfd_zalloc (abfd, section->size);
b49e97c9 13213 if (c == NULL)
b34976b6 13214 return FALSE;
f0abc2a1 13215 mips_elf_section_data (section)->u.tdata = c;
b49e97c9
TS
13216 }
13217
9719ad41 13218 memcpy (c + offset, location, count);
b49e97c9
TS
13219 }
13220
13221 return _bfd_elf_set_section_contents (abfd, section, location, offset,
13222 count);
13223}
13224
13225/* This is almost identical to bfd_generic_get_... except that some
13226 MIPS relocations need to be handled specially. Sigh. */
13227
13228bfd_byte *
9719ad41
RS
13229_bfd_elf_mips_get_relocated_section_contents
13230 (bfd *abfd,
13231 struct bfd_link_info *link_info,
13232 struct bfd_link_order *link_order,
13233 bfd_byte *data,
13234 bfd_boolean relocatable,
13235 asymbol **symbols)
b49e97c9
TS
13236{
13237 /* Get enough memory to hold the stuff */
13238 bfd *input_bfd = link_order->u.indirect.section->owner;
13239 asection *input_section = link_order->u.indirect.section;
eea6121a 13240 bfd_size_type sz;
b49e97c9
TS
13241
13242 long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13243 arelent **reloc_vector = NULL;
13244 long reloc_count;
13245
13246 if (reloc_size < 0)
13247 goto error_return;
13248
9719ad41 13249 reloc_vector = bfd_malloc (reloc_size);
b49e97c9
TS
13250 if (reloc_vector == NULL && reloc_size != 0)
13251 goto error_return;
13252
13253 /* read in the section */
eea6121a
AM
13254 sz = input_section->rawsize ? input_section->rawsize : input_section->size;
13255 if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
b49e97c9
TS
13256 goto error_return;
13257
b49e97c9
TS
13258 reloc_count = bfd_canonicalize_reloc (input_bfd,
13259 input_section,
13260 reloc_vector,
13261 symbols);
13262 if (reloc_count < 0)
13263 goto error_return;
13264
13265 if (reloc_count > 0)
13266 {
13267 arelent **parent;
13268 /* for mips */
13269 int gp_found;
13270 bfd_vma gp = 0x12345678; /* initialize just to shut gcc up */
13271
13272 {
13273 struct bfd_hash_entry *h;
13274 struct bfd_link_hash_entry *lh;
13275 /* Skip all this stuff if we aren't mixing formats. */
13276 if (abfd && input_bfd
13277 && abfd->xvec == input_bfd->xvec)
13278 lh = 0;
13279 else
13280 {
b34976b6 13281 h = bfd_hash_lookup (&link_info->hash->table, "_gp", FALSE, FALSE);
b49e97c9
TS
13282 lh = (struct bfd_link_hash_entry *) h;
13283 }
13284 lookup:
13285 if (lh)
13286 {
13287 switch (lh->type)
13288 {
13289 case bfd_link_hash_undefined:
13290 case bfd_link_hash_undefweak:
13291 case bfd_link_hash_common:
13292 gp_found = 0;
13293 break;
13294 case bfd_link_hash_defined:
13295 case bfd_link_hash_defweak:
13296 gp_found = 1;
13297 gp = lh->u.def.value;
13298 break;
13299 case bfd_link_hash_indirect:
13300 case bfd_link_hash_warning:
13301 lh = lh->u.i.link;
13302 /* @@FIXME ignoring warning for now */
13303 goto lookup;
13304 case bfd_link_hash_new:
13305 default:
13306 abort ();
13307 }
13308 }
13309 else
13310 gp_found = 0;
13311 }
13312 /* end mips */
9719ad41 13313 for (parent = reloc_vector; *parent != NULL; parent++)
b49e97c9 13314 {
9719ad41 13315 char *error_message = NULL;
b49e97c9
TS
13316 bfd_reloc_status_type r;
13317
13318 /* Specific to MIPS: Deal with relocation types that require
13319 knowing the gp of the output bfd. */
13320 asymbol *sym = *(*parent)->sym_ptr_ptr;
b49e97c9 13321
8236346f
EC
13322 /* If we've managed to find the gp and have a special
13323 function for the relocation then go ahead, else default
13324 to the generic handling. */
13325 if (gp_found
13326 && (*parent)->howto->special_function
13327 == _bfd_mips_elf32_gprel16_reloc)
13328 r = _bfd_mips_elf_gprel16_with_gp (input_bfd, sym, *parent,
13329 input_section, relocatable,
13330 data, gp);
13331 else
86324f90 13332 r = bfd_perform_relocation (input_bfd, *parent, data,
8236346f
EC
13333 input_section,
13334 relocatable ? abfd : NULL,
13335 &error_message);
b49e97c9 13336
1049f94e 13337 if (relocatable)
b49e97c9
TS
13338 {
13339 asection *os = input_section->output_section;
13340
13341 /* A partial link, so keep the relocs */
13342 os->orelocation[os->reloc_count] = *parent;
13343 os->reloc_count++;
13344 }
13345
13346 if (r != bfd_reloc_ok)
13347 {
13348 switch (r)
13349 {
13350 case bfd_reloc_undefined:
1a72702b
AM
13351 (*link_info->callbacks->undefined_symbol)
13352 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13353 input_bfd, input_section, (*parent)->address, TRUE);
b49e97c9
TS
13354 break;
13355 case bfd_reloc_dangerous:
9719ad41 13356 BFD_ASSERT (error_message != NULL);
1a72702b
AM
13357 (*link_info->callbacks->reloc_dangerous)
13358 (link_info, error_message,
13359 input_bfd, input_section, (*parent)->address);
b49e97c9
TS
13360 break;
13361 case bfd_reloc_overflow:
1a72702b
AM
13362 (*link_info->callbacks->reloc_overflow)
13363 (link_info, NULL,
13364 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13365 (*parent)->howto->name, (*parent)->addend,
13366 input_bfd, input_section, (*parent)->address);
b49e97c9
TS
13367 break;
13368 case bfd_reloc_outofrange:
13369 default:
13370 abort ();
13371 break;
13372 }
13373
13374 }
13375 }
13376 }
c9594989 13377 free (reloc_vector);
b49e97c9
TS
13378 return data;
13379
dc1e8a47 13380 error_return:
c9594989 13381 free (reloc_vector);
b49e97c9
TS
13382 return NULL;
13383}
13384\f
df58fc94
RS
13385static bfd_boolean
13386mips_elf_relax_delete_bytes (bfd *abfd,
13387 asection *sec, bfd_vma addr, int count)
13388{
13389 Elf_Internal_Shdr *symtab_hdr;
13390 unsigned int sec_shndx;
13391 bfd_byte *contents;
13392 Elf_Internal_Rela *irel, *irelend;
13393 Elf_Internal_Sym *isym;
13394 Elf_Internal_Sym *isymend;
13395 struct elf_link_hash_entry **sym_hashes;
13396 struct elf_link_hash_entry **end_hashes;
13397 struct elf_link_hash_entry **start_hashes;
13398 unsigned int symcount;
13399
13400 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
13401 contents = elf_section_data (sec)->this_hdr.contents;
13402
13403 irel = elf_section_data (sec)->relocs;
13404 irelend = irel + sec->reloc_count;
13405
13406 /* Actually delete the bytes. */
13407 memmove (contents + addr, contents + addr + count,
13408 (size_t) (sec->size - addr - count));
13409 sec->size -= count;
13410
13411 /* Adjust all the relocs. */
13412 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
13413 {
13414 /* Get the new reloc address. */
13415 if (irel->r_offset > addr)
13416 irel->r_offset -= count;
13417 }
13418
13419 BFD_ASSERT (addr % 2 == 0);
13420 BFD_ASSERT (count % 2 == 0);
13421
13422 /* Adjust the local symbols defined in this section. */
13423 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13424 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
13425 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2309ddf2 13426 if (isym->st_shndx == sec_shndx && isym->st_value > addr)
df58fc94
RS
13427 isym->st_value -= count;
13428
13429 /* Now adjust the global symbols defined in this section. */
13430 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
13431 - symtab_hdr->sh_info);
13432 sym_hashes = start_hashes = elf_sym_hashes (abfd);
13433 end_hashes = sym_hashes + symcount;
13434
13435 for (; sym_hashes < end_hashes; sym_hashes++)
13436 {
13437 struct elf_link_hash_entry *sym_hash = *sym_hashes;
13438
13439 if ((sym_hash->root.type == bfd_link_hash_defined
13440 || sym_hash->root.type == bfd_link_hash_defweak)
13441 && sym_hash->root.u.def.section == sec)
13442 {
2309ddf2 13443 bfd_vma value = sym_hash->root.u.def.value;
df58fc94 13444
df58fc94
RS
13445 if (ELF_ST_IS_MICROMIPS (sym_hash->other))
13446 value &= MINUS_TWO;
13447 if (value > addr)
13448 sym_hash->root.u.def.value -= count;
13449 }
13450 }
13451
13452 return TRUE;
13453}
13454
13455
13456/* Opcodes needed for microMIPS relaxation as found in
13457 opcodes/micromips-opc.c. */
13458
13459struct opcode_descriptor {
13460 unsigned long match;
13461 unsigned long mask;
13462};
13463
13464/* The $ra register aka $31. */
13465
13466#define RA 31
13467
13468/* 32-bit instruction format register fields. */
13469
13470#define OP32_SREG(opcode) (((opcode) >> 16) & 0x1f)
13471#define OP32_TREG(opcode) (((opcode) >> 21) & 0x1f)
13472
13473/* Check if a 5-bit register index can be abbreviated to 3 bits. */
13474
13475#define OP16_VALID_REG(r) \
13476 ((2 <= (r) && (r) <= 7) || (16 <= (r) && (r) <= 17))
13477
13478
13479/* 32-bit and 16-bit branches. */
13480
13481static const struct opcode_descriptor b_insns_32[] = {
13482 { /* "b", "p", */ 0x40400000, 0xffff0000 }, /* bgez 0 */
13483 { /* "b", "p", */ 0x94000000, 0xffff0000 }, /* beq 0, 0 */
13484 { 0, 0 } /* End marker for find_match(). */
13485};
13486
13487static const struct opcode_descriptor bc_insn_32 =
13488 { /* "bc(1|2)(ft)", "N,p", */ 0x42800000, 0xfec30000 };
13489
13490static const struct opcode_descriptor bz_insn_32 =
13491 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 };
13492
13493static const struct opcode_descriptor bzal_insn_32 =
13494 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 };
13495
13496static const struct opcode_descriptor beq_insn_32 =
13497 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 };
13498
13499static const struct opcode_descriptor b_insn_16 =
13500 { /* "b", "mD", */ 0xcc00, 0xfc00 };
13501
13502static const struct opcode_descriptor bz_insn_16 =
c088dedf 13503 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 };
df58fc94
RS
13504
13505
13506/* 32-bit and 16-bit branch EQ and NE zero. */
13507
13508/* NOTE: All opcode tables have BEQ/BNE in the same order: first the
13509 eq and second the ne. This convention is used when replacing a
13510 32-bit BEQ/BNE with the 16-bit version. */
13511
13512#define BZC32_REG_FIELD(r) (((r) & 0x1f) << 16)
13513
13514static const struct opcode_descriptor bz_rs_insns_32[] = {
13515 { /* "beqz", "s,p", */ 0x94000000, 0xffe00000 },
13516 { /* "bnez", "s,p", */ 0xb4000000, 0xffe00000 },
13517 { 0, 0 } /* End marker for find_match(). */
13518};
13519
13520static const struct opcode_descriptor bz_rt_insns_32[] = {
13521 { /* "beqz", "t,p", */ 0x94000000, 0xfc01f000 },
13522 { /* "bnez", "t,p", */ 0xb4000000, 0xfc01f000 },
13523 { 0, 0 } /* End marker for find_match(). */
13524};
13525
13526static const struct opcode_descriptor bzc_insns_32[] = {
13527 { /* "beqzc", "s,p", */ 0x40e00000, 0xffe00000 },
13528 { /* "bnezc", "s,p", */ 0x40a00000, 0xffe00000 },
13529 { 0, 0 } /* End marker for find_match(). */
13530};
13531
13532static const struct opcode_descriptor bz_insns_16[] = {
13533 { /* "beqz", "md,mE", */ 0x8c00, 0xfc00 },
13534 { /* "bnez", "md,mE", */ 0xac00, 0xfc00 },
13535 { 0, 0 } /* End marker for find_match(). */
13536};
13537
13538/* Switch between a 5-bit register index and its 3-bit shorthand. */
13539
e67f83e5 13540#define BZ16_REG(opcode) ((((((opcode) >> 7) & 7) + 0x1e) & 0xf) + 2)
eb6b0cf4 13541#define BZ16_REG_FIELD(r) (((r) & 7) << 7)
df58fc94
RS
13542
13543
13544/* 32-bit instructions with a delay slot. */
13545
13546static const struct opcode_descriptor jal_insn_32_bd16 =
13547 { /* "jals", "a", */ 0x74000000, 0xfc000000 };
13548
13549static const struct opcode_descriptor jal_insn_32_bd32 =
13550 { /* "jal", "a", */ 0xf4000000, 0xfc000000 };
13551
13552static const struct opcode_descriptor jal_x_insn_32_bd32 =
13553 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 };
13554
13555static const struct opcode_descriptor j_insn_32 =
13556 { /* "j", "a", */ 0xd4000000, 0xfc000000 };
13557
13558static const struct opcode_descriptor jalr_insn_32 =
13559 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff };
13560
13561/* This table can be compacted, because no opcode replacement is made. */
13562
13563static const struct opcode_descriptor ds_insns_32_bd16[] = {
13564 { /* "jals", "a", */ 0x74000000, 0xfc000000 },
13565
13566 { /* "jalrs[.hb]", "t,s", */ 0x00004f3c, 0xfc00efff },
13567 { /* "b(ge|lt)zals", "s,p", */ 0x42200000, 0xffa00000 },
13568
13569 { /* "b(g|l)(e|t)z", "s,p", */ 0x40000000, 0xff200000 },
13570 { /* "b(eq|ne)", "s,t,p", */ 0x94000000, 0xdc000000 },
13571 { /* "j", "a", */ 0xd4000000, 0xfc000000 },
13572 { 0, 0 } /* End marker for find_match(). */
13573};
13574
13575/* This table can be compacted, because no opcode replacement is made. */
13576
13577static const struct opcode_descriptor ds_insns_32_bd32[] = {
13578 { /* "jal[x]", "a", */ 0xf0000000, 0xf8000000 },
13579
13580 { /* "jalr[.hb]", "t,s", */ 0x00000f3c, 0xfc00efff },
13581 { /* "b(ge|lt)zal", "s,p", */ 0x40200000, 0xffa00000 },
13582 { 0, 0 } /* End marker for find_match(). */
13583};
13584
13585
13586/* 16-bit instructions with a delay slot. */
13587
13588static const struct opcode_descriptor jalr_insn_16_bd16 =
13589 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 };
13590
13591static const struct opcode_descriptor jalr_insn_16_bd32 =
13592 { /* "jalr", "my,mj", */ 0x45c0, 0xffe0 };
13593
13594static const struct opcode_descriptor jr_insn_16 =
13595 { /* "jr", "mj", */ 0x4580, 0xffe0 };
13596
13597#define JR16_REG(opcode) ((opcode) & 0x1f)
13598
13599/* This table can be compacted, because no opcode replacement is made. */
13600
13601static const struct opcode_descriptor ds_insns_16_bd16[] = {
13602 { /* "jalrs", "my,mj", */ 0x45e0, 0xffe0 },
13603
13604 { /* "b", "mD", */ 0xcc00, 0xfc00 },
13605 { /* "b(eq|ne)z", "md,mE", */ 0x8c00, 0xdc00 },
13606 { /* "jr", "mj", */ 0x4580, 0xffe0 },
13607 { 0, 0 } /* End marker for find_match(). */
13608};
13609
13610
13611/* LUI instruction. */
13612
13613static const struct opcode_descriptor lui_insn =
13614 { /* "lui", "s,u", */ 0x41a00000, 0xffe00000 };
13615
13616
13617/* ADDIU instruction. */
13618
13619static const struct opcode_descriptor addiu_insn =
13620 { /* "addiu", "t,r,j", */ 0x30000000, 0xfc000000 };
13621
13622static const struct opcode_descriptor addiupc_insn =
13623 { /* "addiu", "mb,$pc,mQ", */ 0x78000000, 0xfc000000 };
13624
13625#define ADDIUPC_REG_FIELD(r) \
13626 (((2 <= (r) && (r) <= 7) ? (r) : ((r) - 16)) << 23)
13627
13628
13629/* Relaxable instructions in a JAL delay slot: MOVE. */
13630
13631/* The 16-bit move has rd in 9:5 and rs in 4:0. The 32-bit moves
13632 (ADDU, OR) have rd in 15:11 and rs in 10:16. */
13633#define MOVE32_RD(opcode) (((opcode) >> 11) & 0x1f)
13634#define MOVE32_RS(opcode) (((opcode) >> 16) & 0x1f)
13635
13636#define MOVE16_RD_FIELD(r) (((r) & 0x1f) << 5)
13637#define MOVE16_RS_FIELD(r) (((r) & 0x1f) )
13638
13639static const struct opcode_descriptor move_insns_32[] = {
df58fc94 13640 { /* "move", "d,s", */ 0x00000290, 0xffe007ff }, /* or d,s,$0 */
40fc1451 13641 { /* "move", "d,s", */ 0x00000150, 0xffe007ff }, /* addu d,s,$0 */
df58fc94
RS
13642 { 0, 0 } /* End marker for find_match(). */
13643};
13644
13645static const struct opcode_descriptor move_insn_16 =
13646 { /* "move", "mp,mj", */ 0x0c00, 0xfc00 };
13647
13648
13649/* NOP instructions. */
13650
13651static const struct opcode_descriptor nop_insn_32 =
13652 { /* "nop", "", */ 0x00000000, 0xffffffff };
13653
13654static const struct opcode_descriptor nop_insn_16 =
13655 { /* "nop", "", */ 0x0c00, 0xffff };
13656
13657
13658/* Instruction match support. */
13659
13660#define MATCH(opcode, insn) ((opcode & insn.mask) == insn.match)
13661
13662static int
13663find_match (unsigned long opcode, const struct opcode_descriptor insn[])
13664{
13665 unsigned long indx;
13666
13667 for (indx = 0; insn[indx].mask != 0; indx++)
13668 if (MATCH (opcode, insn[indx]))
13669 return indx;
13670
13671 return -1;
13672}
13673
13674
13675/* Branch and delay slot decoding support. */
13676
13677/* If PTR points to what *might* be a 16-bit branch or jump, then
13678 return the minimum length of its delay slot, otherwise return 0.
13679 Non-zero results are not definitive as we might be checking against
13680 the second half of another instruction. */
13681
13682static int
13683check_br16_dslot (bfd *abfd, bfd_byte *ptr)
13684{
13685 unsigned long opcode;
13686 int bdsize;
13687
13688 opcode = bfd_get_16 (abfd, ptr);
13689 if (MATCH (opcode, jalr_insn_16_bd32) != 0)
13690 /* 16-bit branch/jump with a 32-bit delay slot. */
13691 bdsize = 4;
13692 else if (MATCH (opcode, jalr_insn_16_bd16) != 0
13693 || find_match (opcode, ds_insns_16_bd16) >= 0)
13694 /* 16-bit branch/jump with a 16-bit delay slot. */
13695 bdsize = 2;
13696 else
13697 /* No delay slot. */
13698 bdsize = 0;
13699
13700 return bdsize;
13701}
13702
13703/* If PTR points to what *might* be a 32-bit branch or jump, then
13704 return the minimum length of its delay slot, otherwise return 0.
13705 Non-zero results are not definitive as we might be checking against
13706 the second half of another instruction. */
13707
13708static int
13709check_br32_dslot (bfd *abfd, bfd_byte *ptr)
13710{
13711 unsigned long opcode;
13712 int bdsize;
13713
d21911ea 13714 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13715 if (find_match (opcode, ds_insns_32_bd32) >= 0)
13716 /* 32-bit branch/jump with a 32-bit delay slot. */
13717 bdsize = 4;
13718 else if (find_match (opcode, ds_insns_32_bd16) >= 0)
13719 /* 32-bit branch/jump with a 16-bit delay slot. */
13720 bdsize = 2;
13721 else
13722 /* No delay slot. */
13723 bdsize = 0;
13724
13725 return bdsize;
13726}
13727
13728/* If PTR points to a 16-bit branch or jump with a 32-bit delay slot
13729 that doesn't fiddle with REG, then return TRUE, otherwise FALSE. */
13730
13731static bfd_boolean
13732check_br16 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13733{
13734 unsigned long opcode;
13735
13736 opcode = bfd_get_16 (abfd, ptr);
13737 if (MATCH (opcode, b_insn_16)
13738 /* B16 */
13739 || (MATCH (opcode, jr_insn_16) && reg != JR16_REG (opcode))
13740 /* JR16 */
13741 || (MATCH (opcode, bz_insn_16) && reg != BZ16_REG (opcode))
13742 /* BEQZ16, BNEZ16 */
13743 || (MATCH (opcode, jalr_insn_16_bd32)
13744 /* JALR16 */
13745 && reg != JR16_REG (opcode) && reg != RA))
13746 return TRUE;
13747
13748 return FALSE;
13749}
13750
13751/* If PTR points to a 32-bit branch or jump that doesn't fiddle with REG,
13752 then return TRUE, otherwise FALSE. */
13753
f41e5fcc 13754static bfd_boolean
df58fc94
RS
13755check_br32 (bfd *abfd, bfd_byte *ptr, unsigned long reg)
13756{
13757 unsigned long opcode;
13758
d21911ea 13759 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13760 if (MATCH (opcode, j_insn_32)
13761 /* J */
13762 || MATCH (opcode, bc_insn_32)
13763 /* BC1F, BC1T, BC2F, BC2T */
13764 || (MATCH (opcode, jal_x_insn_32_bd32) && reg != RA)
13765 /* JAL, JALX */
13766 || (MATCH (opcode, bz_insn_32) && reg != OP32_SREG (opcode))
13767 /* BGEZ, BGTZ, BLEZ, BLTZ */
13768 || (MATCH (opcode, bzal_insn_32)
13769 /* BGEZAL, BLTZAL */
13770 && reg != OP32_SREG (opcode) && reg != RA)
13771 || ((MATCH (opcode, jalr_insn_32) || MATCH (opcode, beq_insn_32))
13772 /* JALR, JALR.HB, BEQ, BNE */
13773 && reg != OP32_SREG (opcode) && reg != OP32_TREG (opcode)))
13774 return TRUE;
13775
13776 return FALSE;
13777}
13778
80cab405
MR
13779/* If the instruction encoding at PTR and relocations [INTERNAL_RELOCS,
13780 IRELEND) at OFFSET indicate that there must be a compact branch there,
13781 then return TRUE, otherwise FALSE. */
df58fc94
RS
13782
13783static bfd_boolean
80cab405
MR
13784check_relocated_bzc (bfd *abfd, const bfd_byte *ptr, bfd_vma offset,
13785 const Elf_Internal_Rela *internal_relocs,
13786 const Elf_Internal_Rela *irelend)
df58fc94 13787{
80cab405
MR
13788 const Elf_Internal_Rela *irel;
13789 unsigned long opcode;
13790
d21911ea 13791 opcode = bfd_get_micromips_32 (abfd, ptr);
80cab405
MR
13792 if (find_match (opcode, bzc_insns_32) < 0)
13793 return FALSE;
df58fc94
RS
13794
13795 for (irel = internal_relocs; irel < irelend; irel++)
80cab405
MR
13796 if (irel->r_offset == offset
13797 && ELF32_R_TYPE (irel->r_info) == R_MICROMIPS_PC16_S1)
13798 return TRUE;
13799
df58fc94
RS
13800 return FALSE;
13801}
80cab405
MR
13802
13803/* Bitsize checking. */
13804#define IS_BITSIZE(val, N) \
13805 (((((val) & ((1ULL << (N)) - 1)) ^ (1ULL << ((N) - 1))) \
13806 - (1ULL << ((N) - 1))) == (val))
13807
df58fc94
RS
13808\f
13809bfd_boolean
13810_bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
13811 struct bfd_link_info *link_info,
13812 bfd_boolean *again)
13813{
833794fc 13814 bfd_boolean insn32 = mips_elf_hash_table (link_info)->insn32;
df58fc94
RS
13815 Elf_Internal_Shdr *symtab_hdr;
13816 Elf_Internal_Rela *internal_relocs;
13817 Elf_Internal_Rela *irel, *irelend;
13818 bfd_byte *contents = NULL;
13819 Elf_Internal_Sym *isymbuf = NULL;
13820
13821 /* Assume nothing changes. */
13822 *again = FALSE;
13823
13824 /* We don't have to do anything for a relocatable link, if
13825 this section does not have relocs, or if this is not a
13826 code section. */
13827
0e1862bb 13828 if (bfd_link_relocatable (link_info)
df58fc94
RS
13829 || (sec->flags & SEC_RELOC) == 0
13830 || sec->reloc_count == 0
13831 || (sec->flags & SEC_CODE) == 0)
13832 return TRUE;
13833
13834 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13835
13836 /* Get a copy of the native relocations. */
13837 internal_relocs = (_bfd_elf_link_read_relocs
2c3fc389 13838 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
df58fc94
RS
13839 link_info->keep_memory));
13840 if (internal_relocs == NULL)
13841 goto error_return;
13842
13843 /* Walk through them looking for relaxing opportunities. */
13844 irelend = internal_relocs + sec->reloc_count;
13845 for (irel = internal_relocs; irel < irelend; irel++)
13846 {
13847 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
13848 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
13849 bfd_boolean target_is_micromips_code_p;
13850 unsigned long opcode;
13851 bfd_vma symval;
13852 bfd_vma pcrval;
2309ddf2 13853 bfd_byte *ptr;
df58fc94
RS
13854 int fndopc;
13855
13856 /* The number of bytes to delete for relaxation and from where
07d6d2b8 13857 to delete these bytes starting at irel->r_offset. */
df58fc94
RS
13858 int delcnt = 0;
13859 int deloff = 0;
13860
13861 /* If this isn't something that can be relaxed, then ignore
07d6d2b8 13862 this reloc. */
df58fc94
RS
13863 if (r_type != R_MICROMIPS_HI16
13864 && r_type != R_MICROMIPS_PC16_S1
2309ddf2 13865 && r_type != R_MICROMIPS_26_S1)
df58fc94
RS
13866 continue;
13867
13868 /* Get the section contents if we haven't done so already. */
13869 if (contents == NULL)
13870 {
13871 /* Get cached copy if it exists. */
13872 if (elf_section_data (sec)->this_hdr.contents != NULL)
13873 contents = elf_section_data (sec)->this_hdr.contents;
13874 /* Go get them off disk. */
13875 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
13876 goto error_return;
13877 }
2309ddf2 13878 ptr = contents + irel->r_offset;
df58fc94
RS
13879
13880 /* Read this BFD's local symbols if we haven't done so already. */
13881 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
13882 {
13883 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
13884 if (isymbuf == NULL)
13885 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
13886 symtab_hdr->sh_info, 0,
13887 NULL, NULL, NULL);
13888 if (isymbuf == NULL)
13889 goto error_return;
13890 }
13891
13892 /* Get the value of the symbol referred to by the reloc. */
13893 if (r_symndx < symtab_hdr->sh_info)
13894 {
13895 /* A local symbol. */
13896 Elf_Internal_Sym *isym;
13897 asection *sym_sec;
13898
13899 isym = isymbuf + r_symndx;
13900 if (isym->st_shndx == SHN_UNDEF)
13901 sym_sec = bfd_und_section_ptr;
13902 else if (isym->st_shndx == SHN_ABS)
13903 sym_sec = bfd_abs_section_ptr;
13904 else if (isym->st_shndx == SHN_COMMON)
13905 sym_sec = bfd_com_section_ptr;
13906 else
13907 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
13908 symval = (isym->st_value
13909 + sym_sec->output_section->vma
13910 + sym_sec->output_offset);
13911 target_is_micromips_code_p = ELF_ST_IS_MICROMIPS (isym->st_other);
13912 }
13913 else
13914 {
13915 unsigned long indx;
13916 struct elf_link_hash_entry *h;
13917
13918 /* An external symbol. */
13919 indx = r_symndx - symtab_hdr->sh_info;
13920 h = elf_sym_hashes (abfd)[indx];
13921 BFD_ASSERT (h != NULL);
13922
13923 if (h->root.type != bfd_link_hash_defined
13924 && h->root.type != bfd_link_hash_defweak)
13925 /* This appears to be a reference to an undefined
13926 symbol. Just ignore it -- it will be caught by the
13927 regular reloc processing. */
13928 continue;
13929
13930 symval = (h->root.u.def.value
13931 + h->root.u.def.section->output_section->vma
13932 + h->root.u.def.section->output_offset);
13933 target_is_micromips_code_p = (!h->needs_plt
13934 && ELF_ST_IS_MICROMIPS (h->other));
13935 }
13936
13937
13938 /* For simplicity of coding, we are going to modify the
07d6d2b8
AM
13939 section contents, the section relocs, and the BFD symbol
13940 table. We must tell the rest of the code not to free up this
13941 information. It would be possible to instead create a table
13942 of changes which have to be made, as is done in coff-mips.c;
13943 that would be more work, but would require less memory when
13944 the linker is run. */
df58fc94
RS
13945
13946 /* Only 32-bit instructions relaxed. */
13947 if (irel->r_offset + 4 > sec->size)
13948 continue;
13949
d21911ea 13950 opcode = bfd_get_micromips_32 (abfd, ptr);
df58fc94
RS
13951
13952 /* This is the pc-relative distance from the instruction the
07d6d2b8 13953 relocation is applied to, to the symbol referred. */
df58fc94
RS
13954 pcrval = (symval
13955 - (sec->output_section->vma + sec->output_offset)
13956 - irel->r_offset);
13957
13958 /* R_MICROMIPS_HI16 / LUI relaxation to nil, performing relaxation
07d6d2b8
AM
13959 of corresponding R_MICROMIPS_LO16 to R_MICROMIPS_HI0_LO16 or
13960 R_MICROMIPS_PC23_S2. The R_MICROMIPS_PC23_S2 condition is
df58fc94 13961
07d6d2b8 13962 (symval % 4 == 0 && IS_BITSIZE (pcrval, 25))
df58fc94 13963
07d6d2b8
AM
13964 where pcrval has first to be adjusted to apply against the LO16
13965 location (we make the adjustment later on, when we have figured
13966 out the offset). */
df58fc94
RS
13967 if (r_type == R_MICROMIPS_HI16 && MATCH (opcode, lui_insn))
13968 {
80cab405 13969 bfd_boolean bzc = FALSE;
df58fc94
RS
13970 unsigned long nextopc;
13971 unsigned long reg;
13972 bfd_vma offset;
13973
13974 /* Give up if the previous reloc was a HI16 against this symbol
13975 too. */
13976 if (irel > internal_relocs
13977 && ELF32_R_TYPE (irel[-1].r_info) == R_MICROMIPS_HI16
13978 && ELF32_R_SYM (irel[-1].r_info) == r_symndx)
13979 continue;
13980
13981 /* Or if the next reloc is not a LO16 against this symbol. */
13982 if (irel + 1 >= irelend
13983 || ELF32_R_TYPE (irel[1].r_info) != R_MICROMIPS_LO16
13984 || ELF32_R_SYM (irel[1].r_info) != r_symndx)
13985 continue;
13986
13987 /* Or if the second next reloc is a LO16 against this symbol too. */
13988 if (irel + 2 >= irelend
13989 && ELF32_R_TYPE (irel[2].r_info) == R_MICROMIPS_LO16
13990 && ELF32_R_SYM (irel[2].r_info) == r_symndx)
13991 continue;
13992
80cab405
MR
13993 /* See if the LUI instruction *might* be in a branch delay slot.
13994 We check whether what looks like a 16-bit branch or jump is
13995 actually an immediate argument to a compact branch, and let
13996 it through if so. */
df58fc94 13997 if (irel->r_offset >= 2
2309ddf2 13998 && check_br16_dslot (abfd, ptr - 2)
df58fc94 13999 && !(irel->r_offset >= 4
80cab405
MR
14000 && (bzc = check_relocated_bzc (abfd,
14001 ptr - 4, irel->r_offset - 4,
14002 internal_relocs, irelend))))
df58fc94
RS
14003 continue;
14004 if (irel->r_offset >= 4
80cab405 14005 && !bzc
2309ddf2 14006 && check_br32_dslot (abfd, ptr - 4))
df58fc94
RS
14007 continue;
14008
14009 reg = OP32_SREG (opcode);
14010
14011 /* We only relax adjacent instructions or ones separated with
14012 a branch or jump that has a delay slot. The branch or jump
14013 must not fiddle with the register used to hold the address.
14014 Subtract 4 for the LUI itself. */
14015 offset = irel[1].r_offset - irel[0].r_offset;
14016 switch (offset - 4)
14017 {
14018 case 0:
14019 break;
14020 case 2:
2309ddf2 14021 if (check_br16 (abfd, ptr + 4, reg))
df58fc94
RS
14022 break;
14023 continue;
14024 case 4:
2309ddf2 14025 if (check_br32 (abfd, ptr + 4, reg))
df58fc94
RS
14026 break;
14027 continue;
14028 default:
14029 continue;
14030 }
14031
d21911ea 14032 nextopc = bfd_get_micromips_32 (abfd, contents + irel[1].r_offset);
df58fc94
RS
14033
14034 /* Give up unless the same register is used with both
14035 relocations. */
14036 if (OP32_SREG (nextopc) != reg)
14037 continue;
14038
14039 /* Now adjust pcrval, subtracting the offset to the LO16 reloc
14040 and rounding up to take masking of the two LSBs into account. */
14041 pcrval = ((pcrval - offset + 3) | 3) ^ 3;
14042
14043 /* R_MICROMIPS_LO16 relaxation to R_MICROMIPS_HI0_LO16. */
14044 if (IS_BITSIZE (symval, 16))
14045 {
14046 /* Fix the relocation's type. */
14047 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_HI0_LO16);
14048
14049 /* Instructions using R_MICROMIPS_LO16 have the base or
07d6d2b8
AM
14050 source register in bits 20:16. This register becomes $0
14051 (zero) as the result of the R_MICROMIPS_HI16 being 0. */
df58fc94
RS
14052 nextopc &= ~0x001f0000;
14053 bfd_put_16 (abfd, (nextopc >> 16) & 0xffff,
14054 contents + irel[1].r_offset);
14055 }
14056
14057 /* R_MICROMIPS_LO16 / ADDIU relaxation to R_MICROMIPS_PC23_S2.
14058 We add 4 to take LUI deletion into account while checking
14059 the PC-relative distance. */
14060 else if (symval % 4 == 0
14061 && IS_BITSIZE (pcrval + 4, 25)
14062 && MATCH (nextopc, addiu_insn)
14063 && OP32_TREG (nextopc) == OP32_SREG (nextopc)
14064 && OP16_VALID_REG (OP32_TREG (nextopc)))
14065 {
14066 /* Fix the relocation's type. */
14067 irel[1].r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC23_S2);
14068
14069 /* Replace ADDIU with the ADDIUPC version. */
14070 nextopc = (addiupc_insn.match
14071 | ADDIUPC_REG_FIELD (OP32_TREG (nextopc)));
14072
d21911ea
MR
14073 bfd_put_micromips_32 (abfd, nextopc,
14074 contents + irel[1].r_offset);
df58fc94
RS
14075 }
14076
14077 /* Can't do anything, give up, sigh... */
14078 else
14079 continue;
14080
14081 /* Fix the relocation's type. */
14082 irel->r_info = ELF32_R_INFO (r_symndx, R_MIPS_NONE);
14083
14084 /* Delete the LUI instruction: 4 bytes at irel->r_offset. */
14085 delcnt = 4;
14086 deloff = 0;
14087 }
14088
14089 /* Compact branch relaxation -- due to the multitude of macros
07d6d2b8
AM
14090 employed by the compiler/assembler, compact branches are not
14091 always generated. Obviously, this can/will be fixed elsewhere,
14092 but there is no drawback in double checking it here. */
df58fc94
RS
14093 else if (r_type == R_MICROMIPS_PC16_S1
14094 && irel->r_offset + 5 < sec->size
14095 && ((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14096 || (fndopc = find_match (opcode, bz_rt_insns_32)) >= 0)
833794fc
MR
14097 && ((!insn32
14098 && (delcnt = MATCH (bfd_get_16 (abfd, ptr + 4),
14099 nop_insn_16) ? 2 : 0))
14100 || (irel->r_offset + 7 < sec->size
14101 && (delcnt = MATCH (bfd_get_micromips_32 (abfd,
14102 ptr + 4),
14103 nop_insn_32) ? 4 : 0))))
df58fc94
RS
14104 {
14105 unsigned long reg;
14106
14107 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14108
14109 /* Replace BEQZ/BNEZ with the compact version. */
14110 opcode = (bzc_insns_32[fndopc].match
14111 | BZC32_REG_FIELD (reg)
14112 | (opcode & 0xffff)); /* Addend value. */
14113
d21911ea 14114 bfd_put_micromips_32 (abfd, opcode, ptr);
df58fc94 14115
833794fc
MR
14116 /* Delete the delay slot NOP: two or four bytes from
14117 irel->offset + 4; delcnt has already been set above. */
df58fc94
RS
14118 deloff = 4;
14119 }
14120
14121 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC10_S1. We need
07d6d2b8 14122 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
14123 else if (!insn32
14124 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
14125 && IS_BITSIZE (pcrval - 2, 11)
14126 && find_match (opcode, b_insns_32) >= 0)
14127 {
14128 /* Fix the relocation's type. */
14129 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
14130
a8685210 14131 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
14132 bfd_put_16 (abfd,
14133 (b_insn_16.match
14134 | (opcode & 0x3ff)), /* Addend value. */
2309ddf2 14135 ptr);
df58fc94
RS
14136
14137 /* Delete 2 bytes from irel->r_offset + 2. */
14138 delcnt = 2;
14139 deloff = 2;
14140 }
14141
14142 /* R_MICROMIPS_PC16_S1 relaxation to R_MICROMIPS_PC7_S1. We need
07d6d2b8 14143 to check the distance from the next instruction, so subtract 2. */
833794fc
MR
14144 else if (!insn32
14145 && r_type == R_MICROMIPS_PC16_S1
df58fc94
RS
14146 && IS_BITSIZE (pcrval - 2, 8)
14147 && (((fndopc = find_match (opcode, bz_rs_insns_32)) >= 0
14148 && OP16_VALID_REG (OP32_SREG (opcode)))
14149 || ((fndopc = find_match (opcode, bz_rt_insns_32)) >= 0
14150 && OP16_VALID_REG (OP32_TREG (opcode)))))
14151 {
14152 unsigned long reg;
14153
14154 reg = OP32_SREG (opcode) ? OP32_SREG (opcode) : OP32_TREG (opcode);
14155
14156 /* Fix the relocation's type. */
14157 irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
14158
a8685210 14159 /* Replace the 32-bit opcode with a 16-bit opcode. */
df58fc94
RS
14160 bfd_put_16 (abfd,
14161 (bz_insns_16[fndopc].match
14162 | BZ16_REG_FIELD (reg)
14163 | (opcode & 0x7f)), /* Addend value. */
2309ddf2 14164 ptr);
df58fc94
RS
14165
14166 /* Delete 2 bytes from irel->r_offset + 2. */
14167 delcnt = 2;
14168 deloff = 2;
14169 }
14170
14171 /* R_MICROMIPS_26_S1 -- JAL to JALS relaxation for microMIPS targets. */
833794fc
MR
14172 else if (!insn32
14173 && r_type == R_MICROMIPS_26_S1
df58fc94
RS
14174 && target_is_micromips_code_p
14175 && irel->r_offset + 7 < sec->size
14176 && MATCH (opcode, jal_insn_32_bd32))
14177 {
14178 unsigned long n32opc;
14179 bfd_boolean relaxed = FALSE;
14180
d21911ea 14181 n32opc = bfd_get_micromips_32 (abfd, ptr + 4);
df58fc94
RS
14182
14183 if (MATCH (n32opc, nop_insn_32))
14184 {
14185 /* Replace delay slot 32-bit NOP with a 16-bit NOP. */
2309ddf2 14186 bfd_put_16 (abfd, nop_insn_16.match, ptr + 4);
df58fc94
RS
14187
14188 relaxed = TRUE;
14189 }
14190 else if (find_match (n32opc, move_insns_32) >= 0)
14191 {
14192 /* Replace delay slot 32-bit MOVE with 16-bit MOVE. */
14193 bfd_put_16 (abfd,
14194 (move_insn_16.match
14195 | MOVE16_RD_FIELD (MOVE32_RD (n32opc))
14196 | MOVE16_RS_FIELD (MOVE32_RS (n32opc))),
2309ddf2 14197 ptr + 4);
df58fc94
RS
14198
14199 relaxed = TRUE;
14200 }
14201 /* Other 32-bit instructions relaxable to 16-bit
14202 instructions will be handled here later. */
14203
14204 if (relaxed)
14205 {
14206 /* JAL with 32-bit delay slot that is changed to a JALS
07d6d2b8 14207 with 16-bit delay slot. */
d21911ea 14208 bfd_put_micromips_32 (abfd, jal_insn_32_bd16.match, ptr);
df58fc94
RS
14209
14210 /* Delete 2 bytes from irel->r_offset + 6. */
14211 delcnt = 2;
14212 deloff = 6;
14213 }
14214 }
14215
14216 if (delcnt != 0)
14217 {
14218 /* Note that we've changed the relocs, section contents, etc. */
14219 elf_section_data (sec)->relocs = internal_relocs;
14220 elf_section_data (sec)->this_hdr.contents = contents;
14221 symtab_hdr->contents = (unsigned char *) isymbuf;
14222
14223 /* Delete bytes depending on the delcnt and deloff. */
14224 if (!mips_elf_relax_delete_bytes (abfd, sec,
14225 irel->r_offset + deloff, delcnt))
14226 goto error_return;
14227
14228 /* That will change things, so we should relax again.
14229 Note that this is not required, and it may be slow. */
14230 *again = TRUE;
14231 }
14232 }
14233
14234 if (isymbuf != NULL
14235 && symtab_hdr->contents != (unsigned char *) isymbuf)
14236 {
14237 if (! link_info->keep_memory)
14238 free (isymbuf);
14239 else
14240 {
14241 /* Cache the symbols for elf_link_input_bfd. */
14242 symtab_hdr->contents = (unsigned char *) isymbuf;
14243 }
14244 }
14245
14246 if (contents != NULL
14247 && elf_section_data (sec)->this_hdr.contents != contents)
14248 {
14249 if (! link_info->keep_memory)
14250 free (contents);
14251 else
14252 {
14253 /* Cache the section contents for elf_link_input_bfd. */
14254 elf_section_data (sec)->this_hdr.contents = contents;
14255 }
14256 }
14257
c9594989 14258 if (elf_section_data (sec)->relocs != internal_relocs)
df58fc94
RS
14259 free (internal_relocs);
14260
14261 return TRUE;
14262
14263 error_return:
c9594989 14264 if (symtab_hdr->contents != (unsigned char *) isymbuf)
df58fc94 14265 free (isymbuf);
c9594989 14266 if (elf_section_data (sec)->this_hdr.contents != contents)
df58fc94 14267 free (contents);
c9594989 14268 if (elf_section_data (sec)->relocs != internal_relocs)
df58fc94
RS
14269 free (internal_relocs);
14270
14271 return FALSE;
14272}
14273\f
b49e97c9
TS
14274/* Create a MIPS ELF linker hash table. */
14275
14276struct bfd_link_hash_table *
9719ad41 14277_bfd_mips_elf_link_hash_table_create (bfd *abfd)
b49e97c9
TS
14278{
14279 struct mips_elf_link_hash_table *ret;
986f0783 14280 size_t amt = sizeof (struct mips_elf_link_hash_table);
b49e97c9 14281
7bf52ea2 14282 ret = bfd_zmalloc (amt);
9719ad41 14283 if (ret == NULL)
b49e97c9
TS
14284 return NULL;
14285
66eb6687
AM
14286 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
14287 mips_elf_link_hash_newfunc,
4dfe6ac6
NC
14288 sizeof (struct mips_elf_link_hash_entry),
14289 MIPS_ELF_DATA))
b49e97c9 14290 {
e2d34d7d 14291 free (ret);
b49e97c9
TS
14292 return NULL;
14293 }
1bbce132
MR
14294 ret->root.init_plt_refcount.plist = NULL;
14295 ret->root.init_plt_offset.plist = NULL;
b49e97c9 14296
b49e97c9
TS
14297 return &ret->root.root;
14298}
0a44bf69
RS
14299
14300/* Likewise, but indicate that the target is VxWorks. */
14301
14302struct bfd_link_hash_table *
14303_bfd_mips_vxworks_link_hash_table_create (bfd *abfd)
14304{
14305 struct bfd_link_hash_table *ret;
14306
14307 ret = _bfd_mips_elf_link_hash_table_create (abfd);
14308 if (ret)
14309 {
14310 struct mips_elf_link_hash_table *htab;
14311
14312 htab = (struct mips_elf_link_hash_table *) ret;
861fb55a 14313 htab->use_plts_and_copy_relocs = TRUE;
0a44bf69
RS
14314 }
14315 return ret;
14316}
861fb55a
DJ
14317
14318/* A function that the linker calls if we are allowed to use PLTs
14319 and copy relocs. */
14320
14321void
14322_bfd_mips_elf_use_plts_and_copy_relocs (struct bfd_link_info *info)
14323{
14324 mips_elf_hash_table (info)->use_plts_and_copy_relocs = TRUE;
14325}
833794fc
MR
14326
14327/* A function that the linker calls to select between all or only
8b10b0b3 14328 32-bit microMIPS instructions, and between making or ignoring
47275900
MR
14329 branch relocation checks for invalid transitions between ISA modes.
14330 Also record whether we have been configured for a GNU target. */
833794fc
MR
14331
14332void
8b10b0b3 14333_bfd_mips_elf_linker_flags (struct bfd_link_info *info, bfd_boolean insn32,
47275900
MR
14334 bfd_boolean ignore_branch_isa,
14335 bfd_boolean gnu_target)
833794fc 14336{
8b10b0b3
MR
14337 mips_elf_hash_table (info)->insn32 = insn32;
14338 mips_elf_hash_table (info)->ignore_branch_isa = ignore_branch_isa;
47275900 14339 mips_elf_hash_table (info)->gnu_target = gnu_target;
833794fc 14340}
3734320d
MF
14341
14342/* A function that the linker calls to enable use of compact branches in
14343 linker generated code for MIPSR6. */
14344
14345void
14346_bfd_mips_elf_compact_branches (struct bfd_link_info *info, bfd_boolean on)
14347{
14348 mips_elf_hash_table (info)->compact_branches = on;
14349}
14350
b49e97c9 14351\f
c97c330b
MF
14352/* Structure for saying that BFD machine EXTENSION extends BASE. */
14353
14354struct mips_mach_extension
14355{
14356 unsigned long extension, base;
14357};
14358
14359
14360/* An array describing how BFD machines relate to one another. The entries
14361 are ordered topologically with MIPS I extensions listed last. */
14362
14363static const struct mips_mach_extension mips_mach_extensions[] =
14364{
14365 /* MIPS64r2 extensions. */
14366 { bfd_mach_mips_octeon3, bfd_mach_mips_octeon2 },
14367 { bfd_mach_mips_octeon2, bfd_mach_mips_octeonp },
14368 { bfd_mach_mips_octeonp, bfd_mach_mips_octeon },
14369 { bfd_mach_mips_octeon, bfd_mach_mipsisa64r2 },
9108bc33 14370 { bfd_mach_mips_gs264e, bfd_mach_mips_gs464e },
bd782c07 14371 { bfd_mach_mips_gs464e, bfd_mach_mips_gs464 },
ac8cb70f 14372 { bfd_mach_mips_gs464, bfd_mach_mipsisa64r2 },
c97c330b
MF
14373
14374 /* MIPS64 extensions. */
14375 { bfd_mach_mipsisa64r2, bfd_mach_mipsisa64 },
14376 { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
14377 { bfd_mach_mips_xlr, bfd_mach_mipsisa64 },
14378
14379 /* MIPS V extensions. */
14380 { bfd_mach_mipsisa64, bfd_mach_mips5 },
14381
14382 /* R10000 extensions. */
14383 { bfd_mach_mips12000, bfd_mach_mips10000 },
14384 { bfd_mach_mips14000, bfd_mach_mips10000 },
14385 { bfd_mach_mips16000, bfd_mach_mips10000 },
14386
14387 /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
14388 vr5400 ISA, but doesn't include the multimedia stuff. It seems
14389 better to allow vr5400 and vr5500 code to be merged anyway, since
14390 many libraries will just use the core ISA. Perhaps we could add
14391 some sort of ASE flag if this ever proves a problem. */
14392 { bfd_mach_mips5500, bfd_mach_mips5400 },
14393 { bfd_mach_mips5400, bfd_mach_mips5000 },
14394
14395 /* MIPS IV extensions. */
14396 { bfd_mach_mips5, bfd_mach_mips8000 },
14397 { bfd_mach_mips10000, bfd_mach_mips8000 },
14398 { bfd_mach_mips5000, bfd_mach_mips8000 },
14399 { bfd_mach_mips7000, bfd_mach_mips8000 },
14400 { bfd_mach_mips9000, bfd_mach_mips8000 },
14401
14402 /* VR4100 extensions. */
14403 { bfd_mach_mips4120, bfd_mach_mips4100 },
14404 { bfd_mach_mips4111, bfd_mach_mips4100 },
14405
14406 /* MIPS III extensions. */
14407 { bfd_mach_mips_loongson_2e, bfd_mach_mips4000 },
14408 { bfd_mach_mips_loongson_2f, bfd_mach_mips4000 },
14409 { bfd_mach_mips8000, bfd_mach_mips4000 },
14410 { bfd_mach_mips4650, bfd_mach_mips4000 },
14411 { bfd_mach_mips4600, bfd_mach_mips4000 },
14412 { bfd_mach_mips4400, bfd_mach_mips4000 },
14413 { bfd_mach_mips4300, bfd_mach_mips4000 },
14414 { bfd_mach_mips4100, bfd_mach_mips4000 },
c97c330b
MF
14415 { bfd_mach_mips5900, bfd_mach_mips4000 },
14416
38bf472a
MR
14417 /* MIPS32r3 extensions. */
14418 { bfd_mach_mips_interaptiv_mr2, bfd_mach_mipsisa32r3 },
14419
14420 /* MIPS32r2 extensions. */
14421 { bfd_mach_mipsisa32r3, bfd_mach_mipsisa32r2 },
14422
c97c330b
MF
14423 /* MIPS32 extensions. */
14424 { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
14425
14426 /* MIPS II extensions. */
14427 { bfd_mach_mips4000, bfd_mach_mips6000 },
14428 { bfd_mach_mipsisa32, bfd_mach_mips6000 },
b417536f 14429 { bfd_mach_mips4010, bfd_mach_mips6000 },
c97c330b
MF
14430
14431 /* MIPS I extensions. */
14432 { bfd_mach_mips6000, bfd_mach_mips3000 },
14433 { bfd_mach_mips3900, bfd_mach_mips3000 }
14434};
14435
14436/* Return true if bfd machine EXTENSION is an extension of machine BASE. */
14437
14438static bfd_boolean
14439mips_mach_extends_p (unsigned long base, unsigned long extension)
14440{
14441 size_t i;
14442
14443 if (extension == base)
14444 return TRUE;
14445
14446 if (base == bfd_mach_mipsisa32
14447 && mips_mach_extends_p (bfd_mach_mipsisa64, extension))
14448 return TRUE;
14449
14450 if (base == bfd_mach_mipsisa32r2
14451 && mips_mach_extends_p (bfd_mach_mipsisa64r2, extension))
14452 return TRUE;
14453
14454 for (i = 0; i < ARRAY_SIZE (mips_mach_extensions); i++)
14455 if (extension == mips_mach_extensions[i].extension)
14456 {
14457 extension = mips_mach_extensions[i].base;
14458 if (extension == base)
14459 return TRUE;
14460 }
14461
14462 return FALSE;
14463}
14464
14465/* Return the BFD mach for each .MIPS.abiflags ISA Extension. */
14466
14467static unsigned long
14468bfd_mips_isa_ext_mach (unsigned int isa_ext)
14469{
14470 switch (isa_ext)
14471 {
07d6d2b8
AM
14472 case AFL_EXT_3900: return bfd_mach_mips3900;
14473 case AFL_EXT_4010: return bfd_mach_mips4010;
14474 case AFL_EXT_4100: return bfd_mach_mips4100;
14475 case AFL_EXT_4111: return bfd_mach_mips4111;
14476 case AFL_EXT_4120: return bfd_mach_mips4120;
14477 case AFL_EXT_4650: return bfd_mach_mips4650;
14478 case AFL_EXT_5400: return bfd_mach_mips5400;
14479 case AFL_EXT_5500: return bfd_mach_mips5500;
14480 case AFL_EXT_5900: return bfd_mach_mips5900;
14481 case AFL_EXT_10000: return bfd_mach_mips10000;
c97c330b
MF
14482 case AFL_EXT_LOONGSON_2E: return bfd_mach_mips_loongson_2e;
14483 case AFL_EXT_LOONGSON_2F: return bfd_mach_mips_loongson_2f;
07d6d2b8 14484 case AFL_EXT_SB1: return bfd_mach_mips_sb1;
c97c330b
MF
14485 case AFL_EXT_OCTEON: return bfd_mach_mips_octeon;
14486 case AFL_EXT_OCTEONP: return bfd_mach_mips_octeonp;
14487 case AFL_EXT_OCTEON2: return bfd_mach_mips_octeon2;
07d6d2b8
AM
14488 case AFL_EXT_XLR: return bfd_mach_mips_xlr;
14489 default: return bfd_mach_mips3000;
c97c330b
MF
14490 }
14491}
14492
351cdf24
MF
14493/* Return the .MIPS.abiflags value representing each ISA Extension. */
14494
14495unsigned int
14496bfd_mips_isa_ext (bfd *abfd)
14497{
14498 switch (bfd_get_mach (abfd))
14499 {
07d6d2b8
AM
14500 case bfd_mach_mips3900: return AFL_EXT_3900;
14501 case bfd_mach_mips4010: return AFL_EXT_4010;
14502 case bfd_mach_mips4100: return AFL_EXT_4100;
14503 case bfd_mach_mips4111: return AFL_EXT_4111;
14504 case bfd_mach_mips4120: return AFL_EXT_4120;
14505 case bfd_mach_mips4650: return AFL_EXT_4650;
14506 case bfd_mach_mips5400: return AFL_EXT_5400;
14507 case bfd_mach_mips5500: return AFL_EXT_5500;
14508 case bfd_mach_mips5900: return AFL_EXT_5900;
14509 case bfd_mach_mips10000: return AFL_EXT_10000;
c97c330b
MF
14510 case bfd_mach_mips_loongson_2e: return AFL_EXT_LOONGSON_2E;
14511 case bfd_mach_mips_loongson_2f: return AFL_EXT_LOONGSON_2F;
07d6d2b8
AM
14512 case bfd_mach_mips_sb1: return AFL_EXT_SB1;
14513 case bfd_mach_mips_octeon: return AFL_EXT_OCTEON;
14514 case bfd_mach_mips_octeonp: return AFL_EXT_OCTEONP;
14515 case bfd_mach_mips_octeon3: return AFL_EXT_OCTEON3;
14516 case bfd_mach_mips_octeon2: return AFL_EXT_OCTEON2;
14517 case bfd_mach_mips_xlr: return AFL_EXT_XLR;
38bf472a
MR
14518 case bfd_mach_mips_interaptiv_mr2:
14519 return AFL_EXT_INTERAPTIV_MR2;
07d6d2b8 14520 default: return 0;
c97c330b
MF
14521 }
14522}
14523
14524/* Encode ISA level and revision as a single value. */
14525#define LEVEL_REV(LEV,REV) ((LEV) << 3 | (REV))
14526
14527/* Decode a single value into level and revision. */
14528#define ISA_LEVEL(LEVREV) ((LEVREV) >> 3)
14529#define ISA_REV(LEVREV) ((LEVREV) & 0x7)
351cdf24
MF
14530
14531/* Update the isa_level, isa_rev, isa_ext fields of abiflags. */
14532
14533static void
14534update_mips_abiflags_isa (bfd *abfd, Elf_Internal_ABIFlags_v0 *abiflags)
14535{
c97c330b 14536 int new_isa = 0;
351cdf24
MF
14537 switch (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH)
14538 {
c97c330b
MF
14539 case E_MIPS_ARCH_1: new_isa = LEVEL_REV (1, 0); break;
14540 case E_MIPS_ARCH_2: new_isa = LEVEL_REV (2, 0); break;
14541 case E_MIPS_ARCH_3: new_isa = LEVEL_REV (3, 0); break;
14542 case E_MIPS_ARCH_4: new_isa = LEVEL_REV (4, 0); break;
14543 case E_MIPS_ARCH_5: new_isa = LEVEL_REV (5, 0); break;
14544 case E_MIPS_ARCH_32: new_isa = LEVEL_REV (32, 1); break;
14545 case E_MIPS_ARCH_32R2: new_isa = LEVEL_REV (32, 2); break;
14546 case E_MIPS_ARCH_32R6: new_isa = LEVEL_REV (32, 6); break;
14547 case E_MIPS_ARCH_64: new_isa = LEVEL_REV (64, 1); break;
14548 case E_MIPS_ARCH_64R2: new_isa = LEVEL_REV (64, 2); break;
14549 case E_MIPS_ARCH_64R6: new_isa = LEVEL_REV (64, 6); break;
351cdf24 14550 default:
4eca0228 14551 _bfd_error_handler
695344c0 14552 /* xgettext:c-format */
2c1c9679 14553 (_("%pB: unknown architecture %s"),
351cdf24
MF
14554 abfd, bfd_printable_name (abfd));
14555 }
14556
c97c330b
MF
14557 if (new_isa > LEVEL_REV (abiflags->isa_level, abiflags->isa_rev))
14558 {
14559 abiflags->isa_level = ISA_LEVEL (new_isa);
14560 abiflags->isa_rev = ISA_REV (new_isa);
14561 }
14562
14563 /* Update the isa_ext if ABFD describes a further extension. */
14564 if (mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags->isa_ext),
14565 bfd_get_mach (abfd)))
14566 abiflags->isa_ext = bfd_mips_isa_ext (abfd);
351cdf24
MF
14567}
14568
14569/* Return true if the given ELF header flags describe a 32-bit binary. */
14570
14571static bfd_boolean
14572mips_32bit_flags_p (flagword flags)
14573{
14574 return ((flags & EF_MIPS_32BITMODE) != 0
14575 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
14576 || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
14577 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
14578 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
14579 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32
7361da2c
AB
14580 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2
14581 || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6);
351cdf24
MF
14582}
14583
14584/* Infer the content of the ABI flags based on the elf header. */
14585
14586static void
14587infer_mips_abiflags (bfd *abfd, Elf_Internal_ABIFlags_v0* abiflags)
14588{
14589 obj_attribute *in_attr;
14590
14591 memset (abiflags, 0, sizeof (Elf_Internal_ABIFlags_v0));
14592 update_mips_abiflags_isa (abfd, abiflags);
14593
14594 if (mips_32bit_flags_p (elf_elfheader (abfd)->e_flags))
14595 abiflags->gpr_size = AFL_REG_32;
14596 else
14597 abiflags->gpr_size = AFL_REG_64;
14598
14599 abiflags->cpr1_size = AFL_REG_NONE;
14600
14601 in_attr = elf_known_obj_attributes (abfd)[OBJ_ATTR_GNU];
14602 abiflags->fp_abi = in_attr[Tag_GNU_MIPS_ABI_FP].i;
14603
14604 if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_SINGLE
14605 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_XX
14606 || (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14607 && abiflags->gpr_size == AFL_REG_32))
14608 abiflags->cpr1_size = AFL_REG_32;
14609 else if (abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_DOUBLE
14610 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64
14611 || abiflags->fp_abi == Val_GNU_MIPS_ABI_FP_64A)
14612 abiflags->cpr1_size = AFL_REG_64;
14613
14614 abiflags->cpr2_size = AFL_REG_NONE;
14615
14616 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
14617 abiflags->ases |= AFL_ASE_MDMX;
14618 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
14619 abiflags->ases |= AFL_ASE_MIPS16;
14620 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
14621 abiflags->ases |= AFL_ASE_MICROMIPS;
14622
14623 if (abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_ANY
14624 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_SOFT
14625 && abiflags->fp_abi != Val_GNU_MIPS_ABI_FP_64A
14626 && abiflags->isa_level >= 32
bdc6c06e 14627 && abiflags->ases != AFL_ASE_LOONGSON_EXT)
351cdf24
MF
14628 abiflags->flags1 |= AFL_FLAGS1_ODDSPREG;
14629}
14630
b49e97c9
TS
14631/* We need to use a special link routine to handle the .reginfo and
14632 the .mdebug sections. We need to merge all instances of these
14633 sections together, not write them all out sequentially. */
14634
b34976b6 14635bfd_boolean
9719ad41 14636_bfd_mips_elf_final_link (bfd *abfd, struct bfd_link_info *info)
b49e97c9 14637{
b49e97c9
TS
14638 asection *o;
14639 struct bfd_link_order *p;
14640 asection *reginfo_sec, *mdebug_sec, *gptab_data_sec, *gptab_bss_sec;
351cdf24 14641 asection *rtproc_sec, *abiflags_sec;
b49e97c9
TS
14642 Elf32_RegInfo reginfo;
14643 struct ecoff_debug_info debug;
861fb55a 14644 struct mips_htab_traverse_info hti;
7a2a6943
NC
14645 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14646 const struct ecoff_debug_swap *swap = bed->elf_backend_ecoff_debug_swap;
b49e97c9 14647 HDRR *symhdr = &debug.symbolic_header;
9719ad41 14648 void *mdebug_handle = NULL;
b49e97c9
TS
14649 asection *s;
14650 EXTR esym;
14651 unsigned int i;
14652 bfd_size_type amt;
0a44bf69 14653 struct mips_elf_link_hash_table *htab;
b49e97c9
TS
14654
14655 static const char * const secname[] =
14656 {
14657 ".text", ".init", ".fini", ".data",
14658 ".rodata", ".sdata", ".sbss", ".bss"
14659 };
14660 static const int sc[] =
14661 {
14662 scText, scInit, scFini, scData,
14663 scRData, scSData, scSBss, scBss
14664 };
14665
0a44bf69 14666 htab = mips_elf_hash_table (info);
4dfe6ac6
NC
14667 BFD_ASSERT (htab != NULL);
14668
64575f78
MR
14669 /* Sort the dynamic symbols so that those with GOT entries come after
14670 those without. */
d4596a51
RS
14671 if (!mips_elf_sort_hash_table (abfd, info))
14672 return FALSE;
b49e97c9 14673
861fb55a
DJ
14674 /* Create any scheduled LA25 stubs. */
14675 hti.info = info;
14676 hti.output_bfd = abfd;
14677 hti.error = FALSE;
14678 htab_traverse (htab->la25_stubs, mips_elf_create_la25_stub, &hti);
14679 if (hti.error)
14680 return FALSE;
14681
b49e97c9
TS
14682 /* Get a value for the GP register. */
14683 if (elf_gp (abfd) == 0)
14684 {
14685 struct bfd_link_hash_entry *h;
14686
b34976b6 14687 h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
9719ad41 14688 if (h != NULL && h->type == bfd_link_hash_defined)
b49e97c9
TS
14689 elf_gp (abfd) = (h->u.def.value
14690 + h->u.def.section->output_section->vma
14691 + h->u.def.section->output_offset);
90c14f0c 14692 else if (htab->root.target_os == is_vxworks
0a44bf69
RS
14693 && (h = bfd_link_hash_lookup (info->hash,
14694 "_GLOBAL_OFFSET_TABLE_",
14695 FALSE, FALSE, TRUE))
14696 && h->type == bfd_link_hash_defined)
14697 elf_gp (abfd) = (h->u.def.section->output_section->vma
14698 + h->u.def.section->output_offset
14699 + h->u.def.value);
0e1862bb 14700 else if (bfd_link_relocatable (info))
b49e97c9
TS
14701 {
14702 bfd_vma lo = MINUS_ONE;
14703
14704 /* Find the GP-relative section with the lowest offset. */
9719ad41 14705 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9
TS
14706 if (o->vma < lo
14707 && (elf_section_data (o)->this_hdr.sh_flags & SHF_MIPS_GPREL))
14708 lo = o->vma;
14709
14710 /* And calculate GP relative to that. */
0a44bf69 14711 elf_gp (abfd) = lo + ELF_MIPS_GP_OFFSET (info);
b49e97c9
TS
14712 }
14713 else
14714 {
14715 /* If the relocate_section function needs to do a reloc
14716 involving the GP value, it should make a reloc_dangerous
14717 callback to warn that GP is not defined. */
14718 }
14719 }
14720
14721 /* Go through the sections and collect the .reginfo and .mdebug
14722 information. */
351cdf24 14723 abiflags_sec = NULL;
b49e97c9
TS
14724 reginfo_sec = NULL;
14725 mdebug_sec = NULL;
14726 gptab_data_sec = NULL;
14727 gptab_bss_sec = NULL;
9719ad41 14728 for (o = abfd->sections; o != NULL; o = o->next)
b49e97c9 14729 {
351cdf24
MF
14730 if (strcmp (o->name, ".MIPS.abiflags") == 0)
14731 {
14732 /* We have found the .MIPS.abiflags section in the output file.
14733 Look through all the link_orders comprising it and remove them.
14734 The data is merged in _bfd_mips_elf_merge_private_bfd_data. */
14735 for (p = o->map_head.link_order; p != NULL; p = p->next)
14736 {
14737 asection *input_section;
14738
14739 if (p->type != bfd_indirect_link_order)
14740 {
14741 if (p->type == bfd_data_link_order)
14742 continue;
14743 abort ();
14744 }
14745
14746 input_section = p->u.indirect.section;
14747
14748 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14749 elf_link_input_bfd ignores this section. */
14750 input_section->flags &= ~SEC_HAS_CONTENTS;
14751 }
14752
14753 /* Size has been set in _bfd_mips_elf_always_size_sections. */
14754 BFD_ASSERT(o->size == sizeof (Elf_External_ABIFlags_v0));
14755
14756 /* Skip this section later on (I don't think this currently
14757 matters, but someday it might). */
14758 o->map_head.link_order = NULL;
14759
14760 abiflags_sec = o;
14761 }
14762
b49e97c9
TS
14763 if (strcmp (o->name, ".reginfo") == 0)
14764 {
14765 memset (&reginfo, 0, sizeof reginfo);
14766
14767 /* We have found the .reginfo section in the output file.
14768 Look through all the link_orders comprising it and merge
14769 the information together. */
8423293d 14770 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14771 {
14772 asection *input_section;
14773 bfd *input_bfd;
14774 Elf32_External_RegInfo ext;
14775 Elf32_RegInfo sub;
6798f8bf 14776 bfd_size_type sz;
b49e97c9
TS
14777
14778 if (p->type != bfd_indirect_link_order)
14779 {
14780 if (p->type == bfd_data_link_order)
14781 continue;
14782 abort ();
14783 }
14784
14785 input_section = p->u.indirect.section;
14786 input_bfd = input_section->owner;
14787
6798f8bf
MR
14788 sz = (input_section->size < sizeof (ext)
14789 ? input_section->size : sizeof (ext));
14790 memset (&ext, 0, sizeof (ext));
b49e97c9 14791 if (! bfd_get_section_contents (input_bfd, input_section,
6798f8bf 14792 &ext, 0, sz))
b34976b6 14793 return FALSE;
b49e97c9
TS
14794
14795 bfd_mips_elf32_swap_reginfo_in (input_bfd, &ext, &sub);
14796
14797 reginfo.ri_gprmask |= sub.ri_gprmask;
14798 reginfo.ri_cprmask[0] |= sub.ri_cprmask[0];
14799 reginfo.ri_cprmask[1] |= sub.ri_cprmask[1];
14800 reginfo.ri_cprmask[2] |= sub.ri_cprmask[2];
14801 reginfo.ri_cprmask[3] |= sub.ri_cprmask[3];
14802
14803 /* ri_gp_value is set by the function
1c5e4ee9 14804 `_bfd_mips_elf_section_processing' when the section is
b49e97c9
TS
14805 finally written out. */
14806
14807 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14808 elf_link_input_bfd ignores this section. */
14809 input_section->flags &= ~SEC_HAS_CONTENTS;
14810 }
14811
14812 /* Size has been set in _bfd_mips_elf_always_size_sections. */
b248d650 14813 BFD_ASSERT(o->size == sizeof (Elf32_External_RegInfo));
b49e97c9
TS
14814
14815 /* Skip this section later on (I don't think this currently
14816 matters, but someday it might). */
8423293d 14817 o->map_head.link_order = NULL;
b49e97c9
TS
14818
14819 reginfo_sec = o;
14820 }
14821
14822 if (strcmp (o->name, ".mdebug") == 0)
14823 {
14824 struct extsym_info einfo;
14825 bfd_vma last;
14826
14827 /* We have found the .mdebug section in the output file.
14828 Look through all the link_orders comprising it and merge
14829 the information together. */
14830 symhdr->magic = swap->sym_magic;
14831 /* FIXME: What should the version stamp be? */
14832 symhdr->vstamp = 0;
14833 symhdr->ilineMax = 0;
14834 symhdr->cbLine = 0;
14835 symhdr->idnMax = 0;
14836 symhdr->ipdMax = 0;
14837 symhdr->isymMax = 0;
14838 symhdr->ioptMax = 0;
14839 symhdr->iauxMax = 0;
14840 symhdr->issMax = 0;
14841 symhdr->issExtMax = 0;
14842 symhdr->ifdMax = 0;
14843 symhdr->crfd = 0;
14844 symhdr->iextMax = 0;
14845
14846 /* We accumulate the debugging information itself in the
14847 debug_info structure. */
14848 debug.line = NULL;
14849 debug.external_dnr = NULL;
14850 debug.external_pdr = NULL;
14851 debug.external_sym = NULL;
14852 debug.external_opt = NULL;
14853 debug.external_aux = NULL;
14854 debug.ss = NULL;
14855 debug.ssext = debug.ssext_end = NULL;
14856 debug.external_fdr = NULL;
14857 debug.external_rfd = NULL;
14858 debug.external_ext = debug.external_ext_end = NULL;
14859
14860 mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
9719ad41 14861 if (mdebug_handle == NULL)
b34976b6 14862 return FALSE;
b49e97c9
TS
14863
14864 esym.jmptbl = 0;
14865 esym.cobol_main = 0;
14866 esym.weakext = 0;
14867 esym.reserved = 0;
14868 esym.ifd = ifdNil;
14869 esym.asym.iss = issNil;
14870 esym.asym.st = stLocal;
14871 esym.asym.reserved = 0;
14872 esym.asym.index = indexNil;
14873 last = 0;
14874 for (i = 0; i < sizeof (secname) / sizeof (secname[0]); i++)
14875 {
14876 esym.asym.sc = sc[i];
14877 s = bfd_get_section_by_name (abfd, secname[i]);
14878 if (s != NULL)
14879 {
14880 esym.asym.value = s->vma;
eea6121a 14881 last = s->vma + s->size;
b49e97c9
TS
14882 }
14883 else
14884 esym.asym.value = last;
14885 if (!bfd_ecoff_debug_one_external (abfd, &debug, swap,
14886 secname[i], &esym))
b34976b6 14887 return FALSE;
b49e97c9
TS
14888 }
14889
8423293d 14890 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
14891 {
14892 asection *input_section;
14893 bfd *input_bfd;
14894 const struct ecoff_debug_swap *input_swap;
14895 struct ecoff_debug_info input_debug;
14896 char *eraw_src;
14897 char *eraw_end;
14898
14899 if (p->type != bfd_indirect_link_order)
14900 {
14901 if (p->type == bfd_data_link_order)
14902 continue;
14903 abort ();
14904 }
14905
14906 input_section = p->u.indirect.section;
14907 input_bfd = input_section->owner;
14908
d5eaccd7 14909 if (!is_mips_elf (input_bfd))
b49e97c9
TS
14910 {
14911 /* I don't know what a non MIPS ELF bfd would be
14912 doing with a .mdebug section, but I don't really
14913 want to deal with it. */
14914 continue;
14915 }
14916
14917 input_swap = (get_elf_backend_data (input_bfd)
14918 ->elf_backend_ecoff_debug_swap);
14919
eea6121a 14920 BFD_ASSERT (p->size == input_section->size);
b49e97c9
TS
14921
14922 /* The ECOFF linking code expects that we have already
14923 read in the debugging information and set up an
14924 ecoff_debug_info structure, so we do that now. */
14925 if (! _bfd_mips_elf_read_ecoff_info (input_bfd, input_section,
14926 &input_debug))
b34976b6 14927 return FALSE;
b49e97c9
TS
14928
14929 if (! (bfd_ecoff_debug_accumulate
14930 (mdebug_handle, abfd, &debug, swap, input_bfd,
14931 &input_debug, input_swap, info)))
b34976b6 14932 return FALSE;
b49e97c9
TS
14933
14934 /* Loop through the external symbols. For each one with
14935 interesting information, try to find the symbol in
14936 the linker global hash table and save the information
14937 for the output external symbols. */
14938 eraw_src = input_debug.external_ext;
14939 eraw_end = (eraw_src
14940 + (input_debug.symbolic_header.iextMax
14941 * input_swap->external_ext_size));
14942 for (;
14943 eraw_src < eraw_end;
14944 eraw_src += input_swap->external_ext_size)
14945 {
14946 EXTR ext;
14947 const char *name;
14948 struct mips_elf_link_hash_entry *h;
14949
9719ad41 14950 (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
b49e97c9
TS
14951 if (ext.asym.sc == scNil
14952 || ext.asym.sc == scUndefined
14953 || ext.asym.sc == scSUndefined)
14954 continue;
14955
14956 name = input_debug.ssext + ext.asym.iss;
14957 h = mips_elf_link_hash_lookup (mips_elf_hash_table (info),
b34976b6 14958 name, FALSE, FALSE, TRUE);
b49e97c9
TS
14959 if (h == NULL || h->esym.ifd != -2)
14960 continue;
14961
14962 if (ext.ifd != -1)
14963 {
14964 BFD_ASSERT (ext.ifd
14965 < input_debug.symbolic_header.ifdMax);
14966 ext.ifd = input_debug.ifdmap[ext.ifd];
14967 }
14968
14969 h->esym = ext;
14970 }
14971
14972 /* Free up the information we just read. */
14973 free (input_debug.line);
14974 free (input_debug.external_dnr);
14975 free (input_debug.external_pdr);
14976 free (input_debug.external_sym);
14977 free (input_debug.external_opt);
14978 free (input_debug.external_aux);
14979 free (input_debug.ss);
14980 free (input_debug.ssext);
14981 free (input_debug.external_fdr);
14982 free (input_debug.external_rfd);
14983 free (input_debug.external_ext);
14984
14985 /* Hack: reset the SEC_HAS_CONTENTS flag so that
14986 elf_link_input_bfd ignores this section. */
14987 input_section->flags &= ~SEC_HAS_CONTENTS;
14988 }
14989
0e1862bb 14990 if (SGI_COMPAT (abfd) && bfd_link_pic (info))
b49e97c9
TS
14991 {
14992 /* Create .rtproc section. */
87e0a731 14993 rtproc_sec = bfd_get_linker_section (abfd, ".rtproc");
b49e97c9
TS
14994 if (rtproc_sec == NULL)
14995 {
14996 flagword flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY
14997 | SEC_LINKER_CREATED | SEC_READONLY);
14998
87e0a731
AM
14999 rtproc_sec = bfd_make_section_anyway_with_flags (abfd,
15000 ".rtproc",
15001 flags);
b49e97c9 15002 if (rtproc_sec == NULL
fd361982 15003 || !bfd_set_section_alignment (rtproc_sec, 4))
b34976b6 15004 return FALSE;
b49e97c9
TS
15005 }
15006
15007 if (! mips_elf_create_procedure_table (mdebug_handle, abfd,
15008 info, rtproc_sec,
15009 &debug))
b34976b6 15010 return FALSE;
b49e97c9
TS
15011 }
15012
15013 /* Build the external symbol information. */
15014 einfo.abfd = abfd;
15015 einfo.info = info;
15016 einfo.debug = &debug;
15017 einfo.swap = swap;
b34976b6 15018 einfo.failed = FALSE;
b49e97c9 15019 mips_elf_link_hash_traverse (mips_elf_hash_table (info),
9719ad41 15020 mips_elf_output_extsym, &einfo);
b49e97c9 15021 if (einfo.failed)
b34976b6 15022 return FALSE;
b49e97c9
TS
15023
15024 /* Set the size of the .mdebug section. */
eea6121a 15025 o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
b49e97c9
TS
15026
15027 /* Skip this section later on (I don't think this currently
15028 matters, but someday it might). */
8423293d 15029 o->map_head.link_order = NULL;
b49e97c9
TS
15030
15031 mdebug_sec = o;
15032 }
15033
0112cd26 15034 if (CONST_STRNEQ (o->name, ".gptab."))
b49e97c9
TS
15035 {
15036 const char *subname;
15037 unsigned int c;
15038 Elf32_gptab *tab;
15039 Elf32_External_gptab *ext_tab;
15040 unsigned int j;
15041
15042 /* The .gptab.sdata and .gptab.sbss sections hold
15043 information describing how the small data area would
15044 change depending upon the -G switch. These sections
15045 not used in executables files. */
0e1862bb 15046 if (! bfd_link_relocatable (info))
b49e97c9 15047 {
8423293d 15048 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
15049 {
15050 asection *input_section;
15051
15052 if (p->type != bfd_indirect_link_order)
15053 {
15054 if (p->type == bfd_data_link_order)
15055 continue;
15056 abort ();
15057 }
15058
15059 input_section = p->u.indirect.section;
15060
15061 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15062 elf_link_input_bfd ignores this section. */
15063 input_section->flags &= ~SEC_HAS_CONTENTS;
15064 }
15065
15066 /* Skip this section later on (I don't think this
15067 currently matters, but someday it might). */
8423293d 15068 o->map_head.link_order = NULL;
b49e97c9
TS
15069
15070 /* Really remove the section. */
5daa8fe7 15071 bfd_section_list_remove (abfd, o);
b49e97c9
TS
15072 --abfd->section_count;
15073
15074 continue;
15075 }
15076
15077 /* There is one gptab for initialized data, and one for
15078 uninitialized data. */
15079 if (strcmp (o->name, ".gptab.sdata") == 0)
15080 gptab_data_sec = o;
15081 else if (strcmp (o->name, ".gptab.sbss") == 0)
15082 gptab_bss_sec = o;
15083 else
15084 {
4eca0228 15085 _bfd_error_handler
695344c0 15086 /* xgettext:c-format */
871b3ab2 15087 (_("%pB: illegal section name `%pA'"), abfd, o);
b49e97c9 15088 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 15089 return FALSE;
b49e97c9
TS
15090 }
15091
15092 /* The linker script always combines .gptab.data and
15093 .gptab.sdata into .gptab.sdata, and likewise for
15094 .gptab.bss and .gptab.sbss. It is possible that there is
15095 no .sdata or .sbss section in the output file, in which
15096 case we must change the name of the output section. */
15097 subname = o->name + sizeof ".gptab" - 1;
15098 if (bfd_get_section_by_name (abfd, subname) == NULL)
15099 {
15100 if (o == gptab_data_sec)
15101 o->name = ".gptab.data";
15102 else
15103 o->name = ".gptab.bss";
15104 subname = o->name + sizeof ".gptab" - 1;
15105 BFD_ASSERT (bfd_get_section_by_name (abfd, subname) != NULL);
15106 }
15107
15108 /* Set up the first entry. */
15109 c = 1;
15110 amt = c * sizeof (Elf32_gptab);
9719ad41 15111 tab = bfd_malloc (amt);
b49e97c9 15112 if (tab == NULL)
b34976b6 15113 return FALSE;
b49e97c9
TS
15114 tab[0].gt_header.gt_current_g_value = elf_gp_size (abfd);
15115 tab[0].gt_header.gt_unused = 0;
15116
15117 /* Combine the input sections. */
8423293d 15118 for (p = o->map_head.link_order; p != NULL; p = p->next)
b49e97c9
TS
15119 {
15120 asection *input_section;
15121 bfd *input_bfd;
15122 bfd_size_type size;
15123 unsigned long last;
15124 bfd_size_type gpentry;
15125
15126 if (p->type != bfd_indirect_link_order)
15127 {
15128 if (p->type == bfd_data_link_order)
15129 continue;
15130 abort ();
15131 }
15132
15133 input_section = p->u.indirect.section;
15134 input_bfd = input_section->owner;
15135
15136 /* Combine the gptab entries for this input section one
15137 by one. We know that the input gptab entries are
15138 sorted by ascending -G value. */
eea6121a 15139 size = input_section->size;
b49e97c9
TS
15140 last = 0;
15141 for (gpentry = sizeof (Elf32_External_gptab);
15142 gpentry < size;
15143 gpentry += sizeof (Elf32_External_gptab))
15144 {
15145 Elf32_External_gptab ext_gptab;
15146 Elf32_gptab int_gptab;
15147 unsigned long val;
15148 unsigned long add;
b34976b6 15149 bfd_boolean exact;
b49e97c9
TS
15150 unsigned int look;
15151
15152 if (! (bfd_get_section_contents
9719ad41
RS
15153 (input_bfd, input_section, &ext_gptab, gpentry,
15154 sizeof (Elf32_External_gptab))))
b49e97c9
TS
15155 {
15156 free (tab);
b34976b6 15157 return FALSE;
b49e97c9
TS
15158 }
15159
15160 bfd_mips_elf32_swap_gptab_in (input_bfd, &ext_gptab,
15161 &int_gptab);
15162 val = int_gptab.gt_entry.gt_g_value;
15163 add = int_gptab.gt_entry.gt_bytes - last;
15164
b34976b6 15165 exact = FALSE;
b49e97c9
TS
15166 for (look = 1; look < c; look++)
15167 {
15168 if (tab[look].gt_entry.gt_g_value >= val)
15169 tab[look].gt_entry.gt_bytes += add;
15170
15171 if (tab[look].gt_entry.gt_g_value == val)
b34976b6 15172 exact = TRUE;
b49e97c9
TS
15173 }
15174
15175 if (! exact)
15176 {
15177 Elf32_gptab *new_tab;
15178 unsigned int max;
15179
15180 /* We need a new table entry. */
15181 amt = (bfd_size_type) (c + 1) * sizeof (Elf32_gptab);
9719ad41 15182 new_tab = bfd_realloc (tab, amt);
b49e97c9
TS
15183 if (new_tab == NULL)
15184 {
15185 free (tab);
b34976b6 15186 return FALSE;
b49e97c9
TS
15187 }
15188 tab = new_tab;
15189 tab[c].gt_entry.gt_g_value = val;
15190 tab[c].gt_entry.gt_bytes = add;
15191
15192 /* Merge in the size for the next smallest -G
15193 value, since that will be implied by this new
15194 value. */
15195 max = 0;
15196 for (look = 1; look < c; look++)
15197 {
15198 if (tab[look].gt_entry.gt_g_value < val
15199 && (max == 0
15200 || (tab[look].gt_entry.gt_g_value
15201 > tab[max].gt_entry.gt_g_value)))
15202 max = look;
15203 }
15204 if (max != 0)
15205 tab[c].gt_entry.gt_bytes +=
15206 tab[max].gt_entry.gt_bytes;
15207
15208 ++c;
15209 }
15210
15211 last = int_gptab.gt_entry.gt_bytes;
15212 }
15213
15214 /* Hack: reset the SEC_HAS_CONTENTS flag so that
15215 elf_link_input_bfd ignores this section. */
15216 input_section->flags &= ~SEC_HAS_CONTENTS;
15217 }
15218
15219 /* The table must be sorted by -G value. */
15220 if (c > 2)
15221 qsort (tab + 1, c - 1, sizeof (tab[0]), gptab_compare);
15222
15223 /* Swap out the table. */
15224 amt = (bfd_size_type) c * sizeof (Elf32_External_gptab);
9719ad41 15225 ext_tab = bfd_alloc (abfd, amt);
b49e97c9
TS
15226 if (ext_tab == NULL)
15227 {
15228 free (tab);
b34976b6 15229 return FALSE;
b49e97c9
TS
15230 }
15231
15232 for (j = 0; j < c; j++)
15233 bfd_mips_elf32_swap_gptab_out (abfd, tab + j, ext_tab + j);
15234 free (tab);
15235
eea6121a 15236 o->size = c * sizeof (Elf32_External_gptab);
b49e97c9
TS
15237 o->contents = (bfd_byte *) ext_tab;
15238
15239 /* Skip this section later on (I don't think this currently
15240 matters, but someday it might). */
8423293d 15241 o->map_head.link_order = NULL;
b49e97c9
TS
15242 }
15243 }
15244
15245 /* Invoke the regular ELF backend linker to do all the work. */
c152c796 15246 if (!bfd_elf_final_link (abfd, info))
b34976b6 15247 return FALSE;
b49e97c9
TS
15248
15249 /* Now write out the computed sections. */
15250
351cdf24
MF
15251 if (abiflags_sec != NULL)
15252 {
15253 Elf_External_ABIFlags_v0 ext;
15254 Elf_Internal_ABIFlags_v0 *abiflags;
15255
15256 abiflags = &mips_elf_tdata (abfd)->abiflags;
15257
15258 /* Set up the abiflags if no valid input sections were found. */
15259 if (!mips_elf_tdata (abfd)->abiflags_valid)
15260 {
15261 infer_mips_abiflags (abfd, abiflags);
15262 mips_elf_tdata (abfd)->abiflags_valid = TRUE;
15263 }
15264 bfd_mips_elf_swap_abiflags_v0_out (abfd, abiflags, &ext);
15265 if (! bfd_set_section_contents (abfd, abiflags_sec, &ext, 0, sizeof ext))
15266 return FALSE;
15267 }
15268
9719ad41 15269 if (reginfo_sec != NULL)
b49e97c9
TS
15270 {
15271 Elf32_External_RegInfo ext;
15272
15273 bfd_mips_elf32_swap_reginfo_out (abfd, &reginfo, &ext);
9719ad41 15274 if (! bfd_set_section_contents (abfd, reginfo_sec, &ext, 0, sizeof ext))
b34976b6 15275 return FALSE;
b49e97c9
TS
15276 }
15277
9719ad41 15278 if (mdebug_sec != NULL)
b49e97c9
TS
15279 {
15280 BFD_ASSERT (abfd->output_has_begun);
15281 if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
15282 swap, info,
15283 mdebug_sec->filepos))
b34976b6 15284 return FALSE;
b49e97c9
TS
15285
15286 bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
15287 }
15288
9719ad41 15289 if (gptab_data_sec != NULL)
b49e97c9
TS
15290 {
15291 if (! bfd_set_section_contents (abfd, gptab_data_sec,
15292 gptab_data_sec->contents,
eea6121a 15293 0, gptab_data_sec->size))
b34976b6 15294 return FALSE;
b49e97c9
TS
15295 }
15296
9719ad41 15297 if (gptab_bss_sec != NULL)
b49e97c9
TS
15298 {
15299 if (! bfd_set_section_contents (abfd, gptab_bss_sec,
15300 gptab_bss_sec->contents,
eea6121a 15301 0, gptab_bss_sec->size))
b34976b6 15302 return FALSE;
b49e97c9
TS
15303 }
15304
15305 if (SGI_COMPAT (abfd))
15306 {
15307 rtproc_sec = bfd_get_section_by_name (abfd, ".rtproc");
15308 if (rtproc_sec != NULL)
15309 {
15310 if (! bfd_set_section_contents (abfd, rtproc_sec,
15311 rtproc_sec->contents,
eea6121a 15312 0, rtproc_sec->size))
b34976b6 15313 return FALSE;
b49e97c9
TS
15314 }
15315 }
15316
b34976b6 15317 return TRUE;
b49e97c9
TS
15318}
15319\f
b2e9744f
MR
15320/* Merge object file header flags from IBFD into OBFD. Raise an error
15321 if there are conflicting settings. */
15322
15323static bfd_boolean
50e03d47 15324mips_elf_merge_obj_e_flags (bfd *ibfd, struct bfd_link_info *info)
b2e9744f 15325{
50e03d47 15326 bfd *obfd = info->output_bfd;
b2e9744f
MR
15327 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15328 flagword old_flags;
15329 flagword new_flags;
15330 bfd_boolean ok;
15331
15332 new_flags = elf_elfheader (ibfd)->e_flags;
15333 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_NOREORDER;
15334 old_flags = elf_elfheader (obfd)->e_flags;
15335
15336 /* Check flag compatibility. */
15337
15338 new_flags &= ~EF_MIPS_NOREORDER;
15339 old_flags &= ~EF_MIPS_NOREORDER;
15340
15341 /* Some IRIX 6 BSD-compatibility objects have this bit set. It
15342 doesn't seem to matter. */
15343 new_flags &= ~EF_MIPS_XGOT;
15344 old_flags &= ~EF_MIPS_XGOT;
15345
15346 /* MIPSpro generates ucode info in n64 objects. Again, we should
15347 just be able to ignore this. */
15348 new_flags &= ~EF_MIPS_UCODE;
15349 old_flags &= ~EF_MIPS_UCODE;
15350
15351 /* DSOs should only be linked with CPIC code. */
15352 if ((ibfd->flags & DYNAMIC) != 0)
15353 new_flags |= EF_MIPS_PIC | EF_MIPS_CPIC;
15354
15355 if (new_flags == old_flags)
15356 return TRUE;
15357
15358 ok = TRUE;
15359
15360 if (((new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0)
15361 != ((old_flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) != 0))
15362 {
4eca0228 15363 _bfd_error_handler
871b3ab2 15364 (_("%pB: warning: linking abicalls files with non-abicalls files"),
b2e9744f
MR
15365 ibfd);
15366 ok = TRUE;
15367 }
15368
15369 if (new_flags & (EF_MIPS_PIC | EF_MIPS_CPIC))
15370 elf_elfheader (obfd)->e_flags |= EF_MIPS_CPIC;
15371 if (! (new_flags & EF_MIPS_PIC))
15372 elf_elfheader (obfd)->e_flags &= ~EF_MIPS_PIC;
15373
15374 new_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15375 old_flags &= ~ (EF_MIPS_PIC | EF_MIPS_CPIC);
15376
15377 /* Compare the ISAs. */
15378 if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
15379 {
4eca0228 15380 _bfd_error_handler
871b3ab2 15381 (_("%pB: linking 32-bit code with 64-bit code"),
b2e9744f
MR
15382 ibfd);
15383 ok = FALSE;
15384 }
15385 else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
15386 {
15387 /* OBFD's ISA isn't the same as, or an extension of, IBFD's. */
15388 if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
15389 {
15390 /* Copy the architecture info from IBFD to OBFD. Also copy
15391 the 32-bit flag (if set) so that we continue to recognise
15392 OBFD as a 32-bit binary. */
15393 bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
15394 elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
15395 elf_elfheader (obfd)->e_flags
15396 |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15397
15398 /* Update the ABI flags isa_level, isa_rev, isa_ext fields. */
15399 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
15400
15401 /* Copy across the ABI flags if OBFD doesn't use them
15402 and if that was what caused us to treat IBFD as 32-bit. */
15403 if ((old_flags & EF_MIPS_ABI) == 0
15404 && mips_32bit_flags_p (new_flags)
15405 && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
15406 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
15407 }
15408 else
15409 {
15410 /* The ISAs aren't compatible. */
4eca0228 15411 _bfd_error_handler
695344c0 15412 /* xgettext:c-format */
871b3ab2 15413 (_("%pB: linking %s module with previous %s modules"),
b2e9744f
MR
15414 ibfd,
15415 bfd_printable_name (ibfd),
15416 bfd_printable_name (obfd));
15417 ok = FALSE;
15418 }
15419 }
15420
15421 new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15422 old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
15423
15424 /* Compare ABIs. The 64-bit ABI does not use EF_MIPS_ABI. But, it
15425 does set EI_CLASS differently from any 32-bit ABI. */
15426 if ((new_flags & EF_MIPS_ABI) != (old_flags & EF_MIPS_ABI)
15427 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15428 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15429 {
15430 /* Only error if both are set (to different values). */
15431 if (((new_flags & EF_MIPS_ABI) && (old_flags & EF_MIPS_ABI))
15432 || (elf_elfheader (ibfd)->e_ident[EI_CLASS]
15433 != elf_elfheader (obfd)->e_ident[EI_CLASS]))
15434 {
4eca0228 15435 _bfd_error_handler
695344c0 15436 /* xgettext:c-format */
871b3ab2 15437 (_("%pB: ABI mismatch: linking %s module with previous %s modules"),
b2e9744f
MR
15438 ibfd,
15439 elf_mips_abi_name (ibfd),
15440 elf_mips_abi_name (obfd));
15441 ok = FALSE;
15442 }
15443 new_flags &= ~EF_MIPS_ABI;
15444 old_flags &= ~EF_MIPS_ABI;
15445 }
15446
15447 /* Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together
15448 and allow arbitrary mixing of the remaining ASEs (retain the union). */
15449 if ((new_flags & EF_MIPS_ARCH_ASE) != (old_flags & EF_MIPS_ARCH_ASE))
15450 {
15451 int old_micro = old_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15452 int new_micro = new_flags & EF_MIPS_ARCH_ASE_MICROMIPS;
15453 int old_m16 = old_flags & EF_MIPS_ARCH_ASE_M16;
15454 int new_m16 = new_flags & EF_MIPS_ARCH_ASE_M16;
15455 int micro_mis = old_m16 && new_micro;
15456 int m16_mis = old_micro && new_m16;
15457
15458 if (m16_mis || micro_mis)
15459 {
4eca0228 15460 _bfd_error_handler
695344c0 15461 /* xgettext:c-format */
871b3ab2 15462 (_("%pB: ASE mismatch: linking %s module with previous %s modules"),
b2e9744f
MR
15463 ibfd,
15464 m16_mis ? "MIPS16" : "microMIPS",
15465 m16_mis ? "microMIPS" : "MIPS16");
15466 ok = FALSE;
15467 }
15468
15469 elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ARCH_ASE;
15470
15471 new_flags &= ~ EF_MIPS_ARCH_ASE;
15472 old_flags &= ~ EF_MIPS_ARCH_ASE;
15473 }
15474
15475 /* Compare NaN encodings. */
15476 if ((new_flags & EF_MIPS_NAN2008) != (old_flags & EF_MIPS_NAN2008))
15477 {
695344c0 15478 /* xgettext:c-format */
871b3ab2 15479 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
b2e9744f
MR
15480 ibfd,
15481 (new_flags & EF_MIPS_NAN2008
15482 ? "-mnan=2008" : "-mnan=legacy"),
15483 (old_flags & EF_MIPS_NAN2008
15484 ? "-mnan=2008" : "-mnan=legacy"));
15485 ok = FALSE;
15486 new_flags &= ~EF_MIPS_NAN2008;
15487 old_flags &= ~EF_MIPS_NAN2008;
15488 }
15489
15490 /* Compare FP64 state. */
15491 if ((new_flags & EF_MIPS_FP64) != (old_flags & EF_MIPS_FP64))
15492 {
695344c0 15493 /* xgettext:c-format */
871b3ab2 15494 _bfd_error_handler (_("%pB: linking %s module with previous %s modules"),
b2e9744f
MR
15495 ibfd,
15496 (new_flags & EF_MIPS_FP64
15497 ? "-mfp64" : "-mfp32"),
15498 (old_flags & EF_MIPS_FP64
15499 ? "-mfp64" : "-mfp32"));
15500 ok = FALSE;
15501 new_flags &= ~EF_MIPS_FP64;
15502 old_flags &= ~EF_MIPS_FP64;
15503 }
15504
15505 /* Warn about any other mismatches */
15506 if (new_flags != old_flags)
15507 {
695344c0 15508 /* xgettext:c-format */
4eca0228 15509 _bfd_error_handler
871b3ab2 15510 (_("%pB: uses different e_flags (%#x) fields than previous modules "
d42c267e
AM
15511 "(%#x)"),
15512 ibfd, new_flags, old_flags);
b2e9744f
MR
15513 ok = FALSE;
15514 }
15515
15516 return ok;
15517}
15518
2cf19d5c
JM
15519/* Merge object attributes from IBFD into OBFD. Raise an error if
15520 there are conflicting attributes. */
15521static bfd_boolean
50e03d47 15522mips_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
2cf19d5c 15523{
50e03d47 15524 bfd *obfd = info->output_bfd;
2cf19d5c
JM
15525 obj_attribute *in_attr;
15526 obj_attribute *out_attr;
6ae68ba3 15527 bfd *abi_fp_bfd;
b60bf9be 15528 bfd *abi_msa_bfd;
6ae68ba3
MR
15529
15530 abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
15531 in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
d929bc19 15532 if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != Val_GNU_MIPS_ABI_FP_ANY)
6ae68ba3 15533 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
2cf19d5c 15534
b60bf9be
CF
15535 abi_msa_bfd = mips_elf_tdata (obfd)->abi_msa_bfd;
15536 if (!abi_msa_bfd
15537 && in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15538 mips_elf_tdata (obfd)->abi_msa_bfd = ibfd;
15539
2cf19d5c
JM
15540 if (!elf_known_obj_attributes_proc (obfd)[0].i)
15541 {
15542 /* This is the first object. Copy the attributes. */
15543 _bfd_elf_copy_obj_attributes (ibfd, obfd);
15544
15545 /* Use the Tag_null value to indicate the attributes have been
15546 initialized. */
15547 elf_known_obj_attributes_proc (obfd)[0].i = 1;
15548
15549 return TRUE;
15550 }
15551
15552 /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
15553 non-conflicting ones. */
2cf19d5c
JM
15554 out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15555 if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
15556 {
757a636f 15557 int out_fp, in_fp;
6ae68ba3 15558
757a636f
RS
15559 out_fp = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15560 in_fp = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15561 out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
15562 if (out_fp == Val_GNU_MIPS_ABI_FP_ANY)
15563 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_fp;
351cdf24
MF
15564 else if (out_fp == Val_GNU_MIPS_ABI_FP_XX
15565 && (in_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15566 || in_fp == Val_GNU_MIPS_ABI_FP_64
15567 || in_fp == Val_GNU_MIPS_ABI_FP_64A))
15568 {
15569 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15570 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15571 }
15572 else if (in_fp == Val_GNU_MIPS_ABI_FP_XX
15573 && (out_fp == Val_GNU_MIPS_ABI_FP_DOUBLE
15574 || out_fp == Val_GNU_MIPS_ABI_FP_64
15575 || out_fp == Val_GNU_MIPS_ABI_FP_64A))
15576 /* Keep the current setting. */;
15577 else if (out_fp == Val_GNU_MIPS_ABI_FP_64A
15578 && in_fp == Val_GNU_MIPS_ABI_FP_64)
15579 {
15580 mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
15581 out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
15582 }
15583 else if (in_fp == Val_GNU_MIPS_ABI_FP_64A
15584 && out_fp == Val_GNU_MIPS_ABI_FP_64)
15585 /* Keep the current setting. */;
757a636f
RS
15586 else if (in_fp != Val_GNU_MIPS_ABI_FP_ANY)
15587 {
15588 const char *out_string, *in_string;
6ae68ba3 15589
757a636f
RS
15590 out_string = _bfd_mips_fp_abi_string (out_fp);
15591 in_string = _bfd_mips_fp_abi_string (in_fp);
15592 /* First warn about cases involving unrecognised ABIs. */
15593 if (!out_string && !in_string)
695344c0 15594 /* xgettext:c-format */
757a636f 15595 _bfd_error_handler
2c1c9679 15596 (_("warning: %pB uses unknown floating point ABI %d "
871b3ab2 15597 "(set by %pB), %pB uses unknown floating point ABI %d"),
c08bb8dd 15598 obfd, out_fp, abi_fp_bfd, ibfd, in_fp);
757a636f
RS
15599 else if (!out_string)
15600 _bfd_error_handler
695344c0 15601 /* xgettext:c-format */
2c1c9679 15602 (_("warning: %pB uses unknown floating point ABI %d "
871b3ab2 15603 "(set by %pB), %pB uses %s"),
c08bb8dd 15604 obfd, out_fp, abi_fp_bfd, ibfd, in_string);
757a636f
RS
15605 else if (!in_string)
15606 _bfd_error_handler
695344c0 15607 /* xgettext:c-format */
2c1c9679 15608 (_("warning: %pB uses %s (set by %pB), "
871b3ab2 15609 "%pB uses unknown floating point ABI %d"),
c08bb8dd 15610 obfd, out_string, abi_fp_bfd, ibfd, in_fp);
757a636f
RS
15611 else
15612 {
15613 /* If one of the bfds is soft-float, the other must be
15614 hard-float. The exact choice of hard-float ABI isn't
15615 really relevant to the error message. */
15616 if (in_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15617 out_string = "-mhard-float";
15618 else if (out_fp == Val_GNU_MIPS_ABI_FP_SOFT)
15619 in_string = "-mhard-float";
15620 _bfd_error_handler
695344c0 15621 /* xgettext:c-format */
2c1c9679 15622 (_("warning: %pB uses %s (set by %pB), %pB uses %s"),
c08bb8dd 15623 obfd, out_string, abi_fp_bfd, ibfd, in_string);
757a636f
RS
15624 }
15625 }
2cf19d5c
JM
15626 }
15627
b60bf9be
CF
15628 /* Check for conflicting Tag_GNU_MIPS_ABI_MSA attributes and merge
15629 non-conflicting ones. */
15630 if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15631 {
15632 out_attr[Tag_GNU_MIPS_ABI_MSA].type = 1;
15633 if (out_attr[Tag_GNU_MIPS_ABI_MSA].i == Val_GNU_MIPS_ABI_MSA_ANY)
15634 out_attr[Tag_GNU_MIPS_ABI_MSA].i = in_attr[Tag_GNU_MIPS_ABI_MSA].i;
15635 else if (in_attr[Tag_GNU_MIPS_ABI_MSA].i != Val_GNU_MIPS_ABI_MSA_ANY)
15636 switch (out_attr[Tag_GNU_MIPS_ABI_MSA].i)
15637 {
15638 case Val_GNU_MIPS_ABI_MSA_128:
15639 _bfd_error_handler
695344c0 15640 /* xgettext:c-format */
2c1c9679 15641 (_("warning: %pB uses %s (set by %pB), "
871b3ab2 15642 "%pB uses unknown MSA ABI %d"),
c08bb8dd
AM
15643 obfd, "-mmsa", abi_msa_bfd,
15644 ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
b60bf9be
CF
15645 break;
15646
15647 default:
15648 switch (in_attr[Tag_GNU_MIPS_ABI_MSA].i)
15649 {
15650 case Val_GNU_MIPS_ABI_MSA_128:
15651 _bfd_error_handler
695344c0 15652 /* xgettext:c-format */
2c1c9679 15653 (_("warning: %pB uses unknown MSA ABI %d "
871b3ab2 15654 "(set by %pB), %pB uses %s"),
c08bb8dd
AM
15655 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15656 abi_msa_bfd, ibfd, "-mmsa");
b60bf9be
CF
15657 break;
15658
15659 default:
15660 _bfd_error_handler
695344c0 15661 /* xgettext:c-format */
2c1c9679 15662 (_("warning: %pB uses unknown MSA ABI %d "
871b3ab2 15663 "(set by %pB), %pB uses unknown MSA ABI %d"),
c08bb8dd
AM
15664 obfd, out_attr[Tag_GNU_MIPS_ABI_MSA].i,
15665 abi_msa_bfd, ibfd, in_attr[Tag_GNU_MIPS_ABI_MSA].i);
b60bf9be
CF
15666 break;
15667 }
15668 }
15669 }
15670
2cf19d5c 15671 /* Merge Tag_compatibility attributes and any common GNU ones. */
50e03d47 15672 return _bfd_elf_merge_object_attributes (ibfd, info);
2cf19d5c
JM
15673}
15674
a3dc0a7f
MR
15675/* Merge object ABI flags from IBFD into OBFD. Raise an error if
15676 there are conflicting settings. */
15677
15678static bfd_boolean
15679mips_elf_merge_obj_abiflags (bfd *ibfd, bfd *obfd)
15680{
15681 obj_attribute *out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
15682 struct mips_elf_obj_tdata *out_tdata = mips_elf_tdata (obfd);
15683 struct mips_elf_obj_tdata *in_tdata = mips_elf_tdata (ibfd);
15684
15685 /* Update the output abiflags fp_abi using the computed fp_abi. */
15686 out_tdata->abiflags.fp_abi = out_attr[Tag_GNU_MIPS_ABI_FP].i;
15687
15688#define max(a, b) ((a) > (b) ? (a) : (b))
15689 /* Merge abiflags. */
15690 out_tdata->abiflags.isa_level = max (out_tdata->abiflags.isa_level,
15691 in_tdata->abiflags.isa_level);
15692 out_tdata->abiflags.isa_rev = max (out_tdata->abiflags.isa_rev,
15693 in_tdata->abiflags.isa_rev);
15694 out_tdata->abiflags.gpr_size = max (out_tdata->abiflags.gpr_size,
15695 in_tdata->abiflags.gpr_size);
15696 out_tdata->abiflags.cpr1_size = max (out_tdata->abiflags.cpr1_size,
15697 in_tdata->abiflags.cpr1_size);
15698 out_tdata->abiflags.cpr2_size = max (out_tdata->abiflags.cpr2_size,
15699 in_tdata->abiflags.cpr2_size);
15700#undef max
15701 out_tdata->abiflags.ases |= in_tdata->abiflags.ases;
15702 out_tdata->abiflags.flags1 |= in_tdata->abiflags.flags1;
15703
15704 return TRUE;
15705}
15706
b49e97c9
TS
15707/* Merge backend specific data from an object file to the output
15708 object file when linking. */
15709
b34976b6 15710bfd_boolean
50e03d47 15711_bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
b49e97c9 15712{
50e03d47 15713 bfd *obfd = info->output_bfd;
cf8502c1
MR
15714 struct mips_elf_obj_tdata *out_tdata;
15715 struct mips_elf_obj_tdata *in_tdata;
b34976b6 15716 bfd_boolean null_input_bfd = TRUE;
b49e97c9 15717 asection *sec;
d537eeb5 15718 bfd_boolean ok;
b49e97c9 15719
58238693 15720 /* Check if we have the same endianness. */
50e03d47 15721 if (! _bfd_generic_verify_endian_match (ibfd, info))
aa701218 15722 {
4eca0228 15723 _bfd_error_handler
871b3ab2 15724 (_("%pB: endianness incompatible with that of the selected emulation"),
d003868e 15725 ibfd);
aa701218
AO
15726 return FALSE;
15727 }
b49e97c9 15728
d5eaccd7 15729 if (!is_mips_elf (ibfd) || !is_mips_elf (obfd))
b34976b6 15730 return TRUE;
b49e97c9 15731
cf8502c1
MR
15732 in_tdata = mips_elf_tdata (ibfd);
15733 out_tdata = mips_elf_tdata (obfd);
15734
aa701218
AO
15735 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
15736 {
4eca0228 15737 _bfd_error_handler
871b3ab2 15738 (_("%pB: ABI is incompatible with that of the selected emulation"),
d003868e 15739 ibfd);
aa701218
AO
15740 return FALSE;
15741 }
15742
23ba6f18
MR
15743 /* Check to see if the input BFD actually contains any sections. If not,
15744 then it has no attributes, and its flags may not have been initialized
15745 either, but it cannot actually cause any incompatibility. */
6b728d32 15746 /* FIXME: This excludes any input shared library from consideration. */
351cdf24
MF
15747 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
15748 {
15749 /* Ignore synthetic sections and empty .text, .data and .bss sections
15750 which are automatically generated by gas. Also ignore fake
15751 (s)common sections, since merely defining a common symbol does
15752 not affect compatibility. */
15753 if ((sec->flags & SEC_IS_COMMON) == 0
15754 && strcmp (sec->name, ".reginfo")
15755 && strcmp (sec->name, ".mdebug")
15756 && (sec->size != 0
15757 || (strcmp (sec->name, ".text")
15758 && strcmp (sec->name, ".data")
15759 && strcmp (sec->name, ".bss"))))
15760 {
15761 null_input_bfd = FALSE;
15762 break;
15763 }
15764 }
15765 if (null_input_bfd)
15766 return TRUE;
15767
28d45e28 15768 /* Populate abiflags using existing information. */
23ba6f18
MR
15769 if (in_tdata->abiflags_valid)
15770 {
15771 obj_attribute *in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
28d45e28
MR
15772 Elf_Internal_ABIFlags_v0 in_abiflags;
15773 Elf_Internal_ABIFlags_v0 abiflags;
15774
15775 /* Set up the FP ABI attribute from the abiflags if it is not already
07d6d2b8 15776 set. */
23ba6f18 15777 if (in_attr[Tag_GNU_MIPS_ABI_FP].i == Val_GNU_MIPS_ABI_FP_ANY)
07d6d2b8 15778 in_attr[Tag_GNU_MIPS_ABI_FP].i = in_tdata->abiflags.fp_abi;
23ba6f18 15779
351cdf24 15780 infer_mips_abiflags (ibfd, &abiflags);
cf8502c1 15781 in_abiflags = in_tdata->abiflags;
351cdf24
MF
15782
15783 /* It is not possible to infer the correct ISA revision
07d6d2b8 15784 for R3 or R5 so drop down to R2 for the checks. */
351cdf24
MF
15785 if (in_abiflags.isa_rev == 3 || in_abiflags.isa_rev == 5)
15786 in_abiflags.isa_rev = 2;
15787
c97c330b
MF
15788 if (LEVEL_REV (in_abiflags.isa_level, in_abiflags.isa_rev)
15789 < LEVEL_REV (abiflags.isa_level, abiflags.isa_rev))
4eca0228 15790 _bfd_error_handler
2c1c9679 15791 (_("%pB: warning: inconsistent ISA between e_flags and "
351cdf24
MF
15792 ".MIPS.abiflags"), ibfd);
15793 if (abiflags.fp_abi != Val_GNU_MIPS_ABI_FP_ANY
15794 && in_abiflags.fp_abi != abiflags.fp_abi)
4eca0228 15795 _bfd_error_handler
2c1c9679 15796 (_("%pB: warning: inconsistent FP ABI between .gnu.attributes and "
351cdf24
MF
15797 ".MIPS.abiflags"), ibfd);
15798 if ((in_abiflags.ases & abiflags.ases) != abiflags.ases)
4eca0228 15799 _bfd_error_handler
2c1c9679 15800 (_("%pB: warning: inconsistent ASEs between e_flags and "
351cdf24 15801 ".MIPS.abiflags"), ibfd);
c97c330b
MF
15802 /* The isa_ext is allowed to be an extension of what can be inferred
15803 from e_flags. */
15804 if (!mips_mach_extends_p (bfd_mips_isa_ext_mach (abiflags.isa_ext),
15805 bfd_mips_isa_ext_mach (in_abiflags.isa_ext)))
4eca0228 15806 _bfd_error_handler
2c1c9679 15807 (_("%pB: warning: inconsistent ISA extensions between e_flags and "
351cdf24
MF
15808 ".MIPS.abiflags"), ibfd);
15809 if (in_abiflags.flags2 != 0)
4eca0228 15810 _bfd_error_handler
2c1c9679 15811 (_("%pB: warning: unexpected flag in the flags2 field of "
351cdf24 15812 ".MIPS.abiflags (0x%lx)"), ibfd,
d42c267e 15813 in_abiflags.flags2);
351cdf24 15814 }
28d45e28
MR
15815 else
15816 {
15817 infer_mips_abiflags (ibfd, &in_tdata->abiflags);
15818 in_tdata->abiflags_valid = TRUE;
15819 }
15820
cf8502c1 15821 if (!out_tdata->abiflags_valid)
351cdf24
MF
15822 {
15823 /* Copy input abiflags if output abiflags are not already valid. */
cf8502c1
MR
15824 out_tdata->abiflags = in_tdata->abiflags;
15825 out_tdata->abiflags_valid = TRUE;
351cdf24 15826 }
b49e97c9
TS
15827
15828 if (! elf_flags_init (obfd))
15829 {
b34976b6 15830 elf_flags_init (obfd) = TRUE;
351cdf24 15831 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
b49e97c9
TS
15832 elf_elfheader (obfd)->e_ident[EI_CLASS]
15833 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
15834
15835 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2907b861 15836 && (bfd_get_arch_info (obfd)->the_default
68ffbac6 15837 || mips_mach_extends_p (bfd_get_mach (obfd),
2907b861 15838 bfd_get_mach (ibfd))))
b49e97c9
TS
15839 {
15840 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
15841 bfd_get_mach (ibfd)))
b34976b6 15842 return FALSE;
351cdf24
MF
15843
15844 /* Update the ABI flags isa_level, isa_rev and isa_ext fields. */
cf8502c1 15845 update_mips_abiflags_isa (obfd, &out_tdata->abiflags);
b49e97c9
TS
15846 }
15847
d537eeb5 15848 ok = TRUE;
b49e97c9 15849 }
d537eeb5 15850 else
50e03d47 15851 ok = mips_elf_merge_obj_e_flags (ibfd, info);
d537eeb5 15852
50e03d47 15853 ok = mips_elf_merge_obj_attributes (ibfd, info) && ok;
b49e97c9 15854
a3dc0a7f 15855 ok = mips_elf_merge_obj_abiflags (ibfd, obfd) && ok;
351cdf24 15856
d537eeb5 15857 if (!ok)
b49e97c9
TS
15858 {
15859 bfd_set_error (bfd_error_bad_value);
b34976b6 15860 return FALSE;
b49e97c9
TS
15861 }
15862
b34976b6 15863 return TRUE;
b49e97c9
TS
15864}
15865
15866/* Function to keep MIPS specific file flags like as EF_MIPS_PIC. */
15867
b34976b6 15868bfd_boolean
9719ad41 15869_bfd_mips_elf_set_private_flags (bfd *abfd, flagword flags)
b49e97c9
TS
15870{
15871 BFD_ASSERT (!elf_flags_init (abfd)
15872 || elf_elfheader (abfd)->e_flags == flags);
15873
15874 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
15875 elf_flags_init (abfd) = TRUE;
15876 return TRUE;
b49e97c9
TS
15877}
15878
ad9563d6
CM
15879char *
15880_bfd_mips_elf_get_target_dtag (bfd_vma dtag)
15881{
15882 switch (dtag)
15883 {
15884 default: return "";
15885 case DT_MIPS_RLD_VERSION:
15886 return "MIPS_RLD_VERSION";
15887 case DT_MIPS_TIME_STAMP:
15888 return "MIPS_TIME_STAMP";
15889 case DT_MIPS_ICHECKSUM:
15890 return "MIPS_ICHECKSUM";
15891 case DT_MIPS_IVERSION:
15892 return "MIPS_IVERSION";
15893 case DT_MIPS_FLAGS:
15894 return "MIPS_FLAGS";
15895 case DT_MIPS_BASE_ADDRESS:
15896 return "MIPS_BASE_ADDRESS";
15897 case DT_MIPS_MSYM:
15898 return "MIPS_MSYM";
15899 case DT_MIPS_CONFLICT:
15900 return "MIPS_CONFLICT";
15901 case DT_MIPS_LIBLIST:
15902 return "MIPS_LIBLIST";
15903 case DT_MIPS_LOCAL_GOTNO:
15904 return "MIPS_LOCAL_GOTNO";
15905 case DT_MIPS_CONFLICTNO:
15906 return "MIPS_CONFLICTNO";
15907 case DT_MIPS_LIBLISTNO:
15908 return "MIPS_LIBLISTNO";
15909 case DT_MIPS_SYMTABNO:
15910 return "MIPS_SYMTABNO";
15911 case DT_MIPS_UNREFEXTNO:
15912 return "MIPS_UNREFEXTNO";
15913 case DT_MIPS_GOTSYM:
15914 return "MIPS_GOTSYM";
15915 case DT_MIPS_HIPAGENO:
15916 return "MIPS_HIPAGENO";
15917 case DT_MIPS_RLD_MAP:
15918 return "MIPS_RLD_MAP";
a5499fa4
MF
15919 case DT_MIPS_RLD_MAP_REL:
15920 return "MIPS_RLD_MAP_REL";
ad9563d6
CM
15921 case DT_MIPS_DELTA_CLASS:
15922 return "MIPS_DELTA_CLASS";
15923 case DT_MIPS_DELTA_CLASS_NO:
15924 return "MIPS_DELTA_CLASS_NO";
15925 case DT_MIPS_DELTA_INSTANCE:
15926 return "MIPS_DELTA_INSTANCE";
15927 case DT_MIPS_DELTA_INSTANCE_NO:
15928 return "MIPS_DELTA_INSTANCE_NO";
15929 case DT_MIPS_DELTA_RELOC:
15930 return "MIPS_DELTA_RELOC";
15931 case DT_MIPS_DELTA_RELOC_NO:
15932 return "MIPS_DELTA_RELOC_NO";
15933 case DT_MIPS_DELTA_SYM:
15934 return "MIPS_DELTA_SYM";
15935 case DT_MIPS_DELTA_SYM_NO:
15936 return "MIPS_DELTA_SYM_NO";
15937 case DT_MIPS_DELTA_CLASSSYM:
15938 return "MIPS_DELTA_CLASSSYM";
15939 case DT_MIPS_DELTA_CLASSSYM_NO:
15940 return "MIPS_DELTA_CLASSSYM_NO";
15941 case DT_MIPS_CXX_FLAGS:
15942 return "MIPS_CXX_FLAGS";
15943 case DT_MIPS_PIXIE_INIT:
15944 return "MIPS_PIXIE_INIT";
15945 case DT_MIPS_SYMBOL_LIB:
15946 return "MIPS_SYMBOL_LIB";
15947 case DT_MIPS_LOCALPAGE_GOTIDX:
15948 return "MIPS_LOCALPAGE_GOTIDX";
15949 case DT_MIPS_LOCAL_GOTIDX:
15950 return "MIPS_LOCAL_GOTIDX";
15951 case DT_MIPS_HIDDEN_GOTIDX:
15952 return "MIPS_HIDDEN_GOTIDX";
15953 case DT_MIPS_PROTECTED_GOTIDX:
15954 return "MIPS_PROTECTED_GOT_IDX";
15955 case DT_MIPS_OPTIONS:
15956 return "MIPS_OPTIONS";
15957 case DT_MIPS_INTERFACE:
15958 return "MIPS_INTERFACE";
15959 case DT_MIPS_DYNSTR_ALIGN:
15960 return "DT_MIPS_DYNSTR_ALIGN";
15961 case DT_MIPS_INTERFACE_SIZE:
15962 return "DT_MIPS_INTERFACE_SIZE";
15963 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
15964 return "DT_MIPS_RLD_TEXT_RESOLVE_ADDR";
15965 case DT_MIPS_PERF_SUFFIX:
15966 return "DT_MIPS_PERF_SUFFIX";
15967 case DT_MIPS_COMPACT_SIZE:
15968 return "DT_MIPS_COMPACT_SIZE";
15969 case DT_MIPS_GP_VALUE:
15970 return "DT_MIPS_GP_VALUE";
15971 case DT_MIPS_AUX_DYNAMIC:
15972 return "DT_MIPS_AUX_DYNAMIC";
861fb55a
DJ
15973 case DT_MIPS_PLTGOT:
15974 return "DT_MIPS_PLTGOT";
15975 case DT_MIPS_RWPLT:
15976 return "DT_MIPS_RWPLT";
f16a9783
MS
15977 case DT_MIPS_XHASH:
15978 return "DT_MIPS_XHASH";
ad9563d6
CM
15979 }
15980}
15981
757a636f
RS
15982/* Return the meaning of Tag_GNU_MIPS_ABI_FP value FP, or null if
15983 not known. */
15984
15985const char *
15986_bfd_mips_fp_abi_string (int fp)
15987{
15988 switch (fp)
15989 {
15990 /* These strings aren't translated because they're simply
15991 option lists. */
15992 case Val_GNU_MIPS_ABI_FP_DOUBLE:
15993 return "-mdouble-float";
15994
15995 case Val_GNU_MIPS_ABI_FP_SINGLE:
15996 return "-msingle-float";
15997
15998 case Val_GNU_MIPS_ABI_FP_SOFT:
15999 return "-msoft-float";
16000
351cdf24
MF
16001 case Val_GNU_MIPS_ABI_FP_OLD_64:
16002 return _("-mips32r2 -mfp64 (12 callee-saved)");
16003
16004 case Val_GNU_MIPS_ABI_FP_XX:
16005 return "-mfpxx";
16006
757a636f 16007 case Val_GNU_MIPS_ABI_FP_64:
351cdf24
MF
16008 return "-mgp32 -mfp64";
16009
16010 case Val_GNU_MIPS_ABI_FP_64A:
16011 return "-mgp32 -mfp64 -mno-odd-spreg";
757a636f
RS
16012
16013 default:
16014 return 0;
16015 }
16016}
16017
351cdf24
MF
16018static void
16019print_mips_ases (FILE *file, unsigned int mask)
16020{
16021 if (mask & AFL_ASE_DSP)
16022 fputs ("\n\tDSP ASE", file);
16023 if (mask & AFL_ASE_DSPR2)
16024 fputs ("\n\tDSP R2 ASE", file);
8f4f9071
MF
16025 if (mask & AFL_ASE_DSPR3)
16026 fputs ("\n\tDSP R3 ASE", file);
351cdf24
MF
16027 if (mask & AFL_ASE_EVA)
16028 fputs ("\n\tEnhanced VA Scheme", file);
16029 if (mask & AFL_ASE_MCU)
16030 fputs ("\n\tMCU (MicroController) ASE", file);
16031 if (mask & AFL_ASE_MDMX)
16032 fputs ("\n\tMDMX ASE", file);
16033 if (mask & AFL_ASE_MIPS3D)
16034 fputs ("\n\tMIPS-3D ASE", file);
16035 if (mask & AFL_ASE_MT)
16036 fputs ("\n\tMT ASE", file);
16037 if (mask & AFL_ASE_SMARTMIPS)
16038 fputs ("\n\tSmartMIPS ASE", file);
16039 if (mask & AFL_ASE_VIRT)
16040 fputs ("\n\tVZ ASE", file);
16041 if (mask & AFL_ASE_MSA)
16042 fputs ("\n\tMSA ASE", file);
16043 if (mask & AFL_ASE_MIPS16)
16044 fputs ("\n\tMIPS16 ASE", file);
16045 if (mask & AFL_ASE_MICROMIPS)
16046 fputs ("\n\tMICROMIPS ASE", file);
16047 if (mask & AFL_ASE_XPA)
16048 fputs ("\n\tXPA ASE", file);
25499ac7
MR
16049 if (mask & AFL_ASE_MIPS16E2)
16050 fputs ("\n\tMIPS16e2 ASE", file);
730c3174
SE
16051 if (mask & AFL_ASE_CRC)
16052 fputs ("\n\tCRC ASE", file);
6f20c942
FS
16053 if (mask & AFL_ASE_GINV)
16054 fputs ("\n\tGINV ASE", file);
8095d2f7
CX
16055 if (mask & AFL_ASE_LOONGSON_MMI)
16056 fputs ("\n\tLoongson MMI ASE", file);
716c08de
CX
16057 if (mask & AFL_ASE_LOONGSON_CAM)
16058 fputs ("\n\tLoongson CAM ASE", file);
bdc6c06e
CX
16059 if (mask & AFL_ASE_LOONGSON_EXT)
16060 fputs ("\n\tLoongson EXT ASE", file);
a693765e
CX
16061 if (mask & AFL_ASE_LOONGSON_EXT2)
16062 fputs ("\n\tLoongson EXT2 ASE", file);
351cdf24
MF
16063 if (mask == 0)
16064 fprintf (file, "\n\t%s", _("None"));
00ac7aa0
MF
16065 else if ((mask & ~AFL_ASE_MASK) != 0)
16066 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
351cdf24
MF
16067}
16068
16069static void
16070print_mips_isa_ext (FILE *file, unsigned int isa_ext)
16071{
16072 switch (isa_ext)
16073 {
16074 case 0:
16075 fputs (_("None"), file);
16076 break;
16077 case AFL_EXT_XLR:
16078 fputs ("RMI XLR", file);
16079 break;
2c629856
N
16080 case AFL_EXT_OCTEON3:
16081 fputs ("Cavium Networks Octeon3", file);
16082 break;
351cdf24
MF
16083 case AFL_EXT_OCTEON2:
16084 fputs ("Cavium Networks Octeon2", file);
16085 break;
16086 case AFL_EXT_OCTEONP:
16087 fputs ("Cavium Networks OcteonP", file);
16088 break;
351cdf24
MF
16089 case AFL_EXT_OCTEON:
16090 fputs ("Cavium Networks Octeon", file);
16091 break;
16092 case AFL_EXT_5900:
16093 fputs ("Toshiba R5900", file);
16094 break;
16095 case AFL_EXT_4650:
16096 fputs ("MIPS R4650", file);
16097 break;
16098 case AFL_EXT_4010:
16099 fputs ("LSI R4010", file);
16100 break;
16101 case AFL_EXT_4100:
16102 fputs ("NEC VR4100", file);
16103 break;
16104 case AFL_EXT_3900:
16105 fputs ("Toshiba R3900", file);
16106 break;
16107 case AFL_EXT_10000:
16108 fputs ("MIPS R10000", file);
16109 break;
16110 case AFL_EXT_SB1:
16111 fputs ("Broadcom SB-1", file);
16112 break;
16113 case AFL_EXT_4111:
16114 fputs ("NEC VR4111/VR4181", file);
16115 break;
16116 case AFL_EXT_4120:
16117 fputs ("NEC VR4120", file);
16118 break;
16119 case AFL_EXT_5400:
16120 fputs ("NEC VR5400", file);
16121 break;
16122 case AFL_EXT_5500:
16123 fputs ("NEC VR5500", file);
16124 break;
16125 case AFL_EXT_LOONGSON_2E:
16126 fputs ("ST Microelectronics Loongson 2E", file);
16127 break;
16128 case AFL_EXT_LOONGSON_2F:
16129 fputs ("ST Microelectronics Loongson 2F", file);
16130 break;
38bf472a
MR
16131 case AFL_EXT_INTERAPTIV_MR2:
16132 fputs ("Imagination interAptiv MR2", file);
16133 break;
351cdf24 16134 default:
00ac7aa0 16135 fprintf (file, "%s (%d)", _("Unknown"), isa_ext);
351cdf24
MF
16136 break;
16137 }
16138}
16139
16140static void
16141print_mips_fp_abi_value (FILE *file, int val)
16142{
16143 switch (val)
16144 {
16145 case Val_GNU_MIPS_ABI_FP_ANY:
16146 fprintf (file, _("Hard or soft float\n"));
16147 break;
16148 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16149 fprintf (file, _("Hard float (double precision)\n"));
16150 break;
16151 case Val_GNU_MIPS_ABI_FP_SINGLE:
16152 fprintf (file, _("Hard float (single precision)\n"));
16153 break;
16154 case Val_GNU_MIPS_ABI_FP_SOFT:
16155 fprintf (file, _("Soft float\n"));
16156 break;
16157 case Val_GNU_MIPS_ABI_FP_OLD_64:
16158 fprintf (file, _("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16159 break;
16160 case Val_GNU_MIPS_ABI_FP_XX:
16161 fprintf (file, _("Hard float (32-bit CPU, Any FPU)\n"));
16162 break;
16163 case Val_GNU_MIPS_ABI_FP_64:
16164 fprintf (file, _("Hard float (32-bit CPU, 64-bit FPU)\n"));
16165 break;
16166 case Val_GNU_MIPS_ABI_FP_64A:
16167 fprintf (file, _("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16168 break;
16169 default:
16170 fprintf (file, "??? (%d)\n", val);
16171 break;
16172 }
16173}
16174
16175static int
16176get_mips_reg_size (int reg_size)
16177{
16178 return (reg_size == AFL_REG_NONE) ? 0
16179 : (reg_size == AFL_REG_32) ? 32
16180 : (reg_size == AFL_REG_64) ? 64
16181 : (reg_size == AFL_REG_128) ? 128
16182 : -1;
16183}
16184
b34976b6 16185bfd_boolean
9719ad41 16186_bfd_mips_elf_print_private_bfd_data (bfd *abfd, void *ptr)
b49e97c9 16187{
9719ad41 16188 FILE *file = ptr;
b49e97c9
TS
16189
16190 BFD_ASSERT (abfd != NULL && ptr != NULL);
16191
16192 /* Print normal ELF private data. */
16193 _bfd_elf_print_private_bfd_data (abfd, ptr);
16194
16195 /* xgettext:c-format */
16196 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
16197
16198 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
16199 fprintf (file, _(" [abi=O32]"));
16200 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O64)
16201 fprintf (file, _(" [abi=O64]"));
16202 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32)
16203 fprintf (file, _(" [abi=EABI32]"));
16204 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64)
16205 fprintf (file, _(" [abi=EABI64]"));
16206 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ABI))
16207 fprintf (file, _(" [abi unknown]"));
16208 else if (ABI_N32_P (abfd))
16209 fprintf (file, _(" [abi=N32]"));
16210 else if (ABI_64_P (abfd))
16211 fprintf (file, _(" [abi=64]"));
16212 else
16213 fprintf (file, _(" [no abi set]"));
16214
16215 if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
ae0d2616 16216 fprintf (file, " [mips1]");
b49e97c9 16217 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
ae0d2616 16218 fprintf (file, " [mips2]");
b49e97c9 16219 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
ae0d2616 16220 fprintf (file, " [mips3]");
b49e97c9 16221 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
ae0d2616 16222 fprintf (file, " [mips4]");
b49e97c9 16223 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_5)
ae0d2616 16224 fprintf (file, " [mips5]");
b49e97c9 16225 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32)
ae0d2616 16226 fprintf (file, " [mips32]");
b49e97c9 16227 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64)
ae0d2616 16228 fprintf (file, " [mips64]");
af7ee8bf 16229 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R2)
ae0d2616 16230 fprintf (file, " [mips32r2]");
5f74bc13 16231 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R2)
ae0d2616 16232 fprintf (file, " [mips64r2]");
7361da2c
AB
16233 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32R6)
16234 fprintf (file, " [mips32r6]");
16235 else if ((elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_64R6)
16236 fprintf (file, " [mips64r6]");
b49e97c9
TS
16237 else
16238 fprintf (file, _(" [unknown ISA]"));
16239
40d32fc6 16240 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MDMX)
ae0d2616 16241 fprintf (file, " [mdmx]");
40d32fc6
CD
16242
16243 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_M16)
ae0d2616 16244 fprintf (file, " [mips16]");
40d32fc6 16245
df58fc94
RS
16246 if (elf_elfheader (abfd)->e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
16247 fprintf (file, " [micromips]");
16248
ba92f887
MR
16249 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NAN2008)
16250 fprintf (file, " [nan2008]");
16251
5baf5e34 16252 if (elf_elfheader (abfd)->e_flags & EF_MIPS_FP64)
351cdf24 16253 fprintf (file, " [old fp64]");
5baf5e34 16254
b49e97c9 16255 if (elf_elfheader (abfd)->e_flags & EF_MIPS_32BITMODE)
ae0d2616 16256 fprintf (file, " [32bitmode]");
b49e97c9
TS
16257 else
16258 fprintf (file, _(" [not 32bitmode]"));
16259
c0e3f241 16260 if (elf_elfheader (abfd)->e_flags & EF_MIPS_NOREORDER)
ae0d2616 16261 fprintf (file, " [noreorder]");
c0e3f241
CD
16262
16263 if (elf_elfheader (abfd)->e_flags & EF_MIPS_PIC)
ae0d2616 16264 fprintf (file, " [PIC]");
c0e3f241
CD
16265
16266 if (elf_elfheader (abfd)->e_flags & EF_MIPS_CPIC)
ae0d2616 16267 fprintf (file, " [CPIC]");
c0e3f241
CD
16268
16269 if (elf_elfheader (abfd)->e_flags & EF_MIPS_XGOT)
ae0d2616 16270 fprintf (file, " [XGOT]");
c0e3f241
CD
16271
16272 if (elf_elfheader (abfd)->e_flags & EF_MIPS_UCODE)
ae0d2616 16273 fprintf (file, " [UCODE]");
c0e3f241 16274
b49e97c9
TS
16275 fputc ('\n', file);
16276
351cdf24
MF
16277 if (mips_elf_tdata (abfd)->abiflags_valid)
16278 {
16279 Elf_Internal_ABIFlags_v0 *abiflags = &mips_elf_tdata (abfd)->abiflags;
16280 fprintf (file, "\nMIPS ABI Flags Version: %d\n", abiflags->version);
16281 fprintf (file, "\nISA: MIPS%d", abiflags->isa_level);
16282 if (abiflags->isa_rev > 1)
16283 fprintf (file, "r%d", abiflags->isa_rev);
16284 fprintf (file, "\nGPR size: %d",
16285 get_mips_reg_size (abiflags->gpr_size));
16286 fprintf (file, "\nCPR1 size: %d",
16287 get_mips_reg_size (abiflags->cpr1_size));
16288 fprintf (file, "\nCPR2 size: %d",
16289 get_mips_reg_size (abiflags->cpr2_size));
16290 fputs ("\nFP ABI: ", file);
16291 print_mips_fp_abi_value (file, abiflags->fp_abi);
16292 fputs ("ISA Extension: ", file);
16293 print_mips_isa_ext (file, abiflags->isa_ext);
16294 fputs ("\nASEs:", file);
16295 print_mips_ases (file, abiflags->ases);
16296 fprintf (file, "\nFLAGS 1: %8.8lx", abiflags->flags1);
16297 fprintf (file, "\nFLAGS 2: %8.8lx", abiflags->flags2);
16298 fputc ('\n', file);
16299 }
16300
b34976b6 16301 return TRUE;
b49e97c9 16302}
2f89ff8d 16303
b35d266b 16304const struct bfd_elf_special_section _bfd_mips_elf_special_sections[] =
2f89ff8d 16305{
07d6d2b8
AM
16306 { STRING_COMMA_LEN (".lit4"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16307 { STRING_COMMA_LEN (".lit8"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
0112cd26 16308 { STRING_COMMA_LEN (".mdebug"), 0, SHT_MIPS_DEBUG, 0 },
07d6d2b8 16309 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
0112cd26
NC
16310 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_MIPS_GPREL },
16311 { STRING_COMMA_LEN (".ucode"), 0, SHT_MIPS_UCODE, 0 },
f16a9783 16312 { STRING_COMMA_LEN (".MIPS.xhash"), 0, SHT_MIPS_XHASH, SHF_ALLOC },
07d6d2b8 16313 { NULL, 0, 0, 0, 0 }
2f89ff8d 16314};
5e2b0d47 16315
8992f0d7
TS
16316/* Merge non visibility st_other attributes. Ensure that the
16317 STO_OPTIONAL flag is copied into h->other, even if this is not a
16318 definiton of the symbol. */
5e2b0d47
NC
16319void
16320_bfd_mips_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
16321 const Elf_Internal_Sym *isym,
16322 bfd_boolean definition,
16323 bfd_boolean dynamic ATTRIBUTE_UNUSED)
16324{
8992f0d7
TS
16325 if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
16326 {
16327 unsigned char other;
16328
16329 other = (definition ? isym->st_other : h->other);
16330 other &= ~ELF_ST_VISIBILITY (-1);
16331 h->other = other | ELF_ST_VISIBILITY (h->other);
16332 }
16333
16334 if (!definition
5e2b0d47
NC
16335 && ELF_MIPS_IS_OPTIONAL (isym->st_other))
16336 h->other |= STO_OPTIONAL;
16337}
12ac1cf5
NC
16338
16339/* Decide whether an undefined symbol is special and can be ignored.
16340 This is the case for OPTIONAL symbols on IRIX. */
16341bfd_boolean
16342_bfd_mips_elf_ignore_undef_symbol (struct elf_link_hash_entry *h)
16343{
16344 return ELF_MIPS_IS_OPTIONAL (h->other) ? TRUE : FALSE;
16345}
e0764319
NC
16346
16347bfd_boolean
16348_bfd_mips_elf_common_definition (Elf_Internal_Sym *sym)
16349{
16350 return (sym->st_shndx == SHN_COMMON
16351 || sym->st_shndx == SHN_MIPS_ACOMMON
16352 || sym->st_shndx == SHN_MIPS_SCOMMON);
16353}
861fb55a
DJ
16354
16355/* Return address for Ith PLT stub in section PLT, for relocation REL
16356 or (bfd_vma) -1 if it should not be included. */
16357
16358bfd_vma
16359_bfd_mips_elf_plt_sym_val (bfd_vma i, const asection *plt,
16360 const arelent *rel ATTRIBUTE_UNUSED)
16361{
16362 return (plt->vma
16363 + 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry)
16364 + i * 4 * ARRAY_SIZE (mips_exec_plt_entry));
16365}
16366
1bbce132
MR
16367/* Build a table of synthetic symbols to represent the PLT. As with MIPS16
16368 and microMIPS PLT slots we may have a many-to-one mapping between .plt
16369 and .got.plt and also the slots may be of a different size each we walk
16370 the PLT manually fetching instructions and matching them against known
16371 patterns. To make things easier standard MIPS slots, if any, always come
16372 first. As we don't create proper ELF symbols we use the UDATA.I member
16373 of ASYMBOL to carry ISA annotation. The encoding used is the same as
16374 with the ST_OTHER member of the ELF symbol. */
16375
16376long
16377_bfd_mips_elf_get_synthetic_symtab (bfd *abfd,
16378 long symcount ATTRIBUTE_UNUSED,
16379 asymbol **syms ATTRIBUTE_UNUSED,
16380 long dynsymcount, asymbol **dynsyms,
16381 asymbol **ret)
16382{
16383 static const char pltname[] = "_PROCEDURE_LINKAGE_TABLE_";
16384 static const char microsuffix[] = "@micromipsplt";
16385 static const char m16suffix[] = "@mips16plt";
16386 static const char mipssuffix[] = "@plt";
16387
16388 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
16389 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
16390 bfd_boolean micromips_p = MICROMIPS_P (abfd);
16391 Elf_Internal_Shdr *hdr;
16392 bfd_byte *plt_data;
16393 bfd_vma plt_offset;
16394 unsigned int other;
16395 bfd_vma entry_size;
16396 bfd_vma plt0_size;
16397 asection *relplt;
16398 bfd_vma opcode;
16399 asection *plt;
16400 asymbol *send;
16401 size_t size;
16402 char *names;
16403 long counti;
16404 arelent *p;
16405 asymbol *s;
16406 char *nend;
16407 long count;
16408 long pi;
16409 long i;
16410 long n;
16411
16412 *ret = NULL;
16413
16414 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 || dynsymcount <= 0)
16415 return 0;
16416
16417 relplt = bfd_get_section_by_name (abfd, ".rel.plt");
16418 if (relplt == NULL)
16419 return 0;
16420
16421 hdr = &elf_section_data (relplt)->this_hdr;
16422 if (hdr->sh_link != elf_dynsymtab (abfd) || hdr->sh_type != SHT_REL)
16423 return 0;
16424
16425 plt = bfd_get_section_by_name (abfd, ".plt");
16426 if (plt == NULL)
16427 return 0;
16428
16429 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
16430 if (!(*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
16431 return -1;
16432 p = relplt->relocation;
16433
16434 /* Calculating the exact amount of space required for symbols would
16435 require two passes over the PLT, so just pessimise assuming two
16436 PLT slots per relocation. */
16437 count = relplt->size / hdr->sh_entsize;
16438 counti = count * bed->s->int_rels_per_ext_rel;
16439 size = 2 * count * sizeof (asymbol);
16440 size += count * (sizeof (mipssuffix) +
16441 (micromips_p ? sizeof (microsuffix) : sizeof (m16suffix)));
16442 for (pi = 0; pi < counti; pi += bed->s->int_rels_per_ext_rel)
16443 size += 2 * strlen ((*p[pi].sym_ptr_ptr)->name);
16444
16445 /* Add the size of "_PROCEDURE_LINKAGE_TABLE_" too. */
16446 size += sizeof (asymbol) + sizeof (pltname);
16447
16448 if (!bfd_malloc_and_get_section (abfd, plt, &plt_data))
16449 return -1;
16450
16451 if (plt->size < 16)
16452 return -1;
16453
16454 s = *ret = bfd_malloc (size);
16455 if (s == NULL)
16456 return -1;
16457 send = s + 2 * count + 1;
16458
16459 names = (char *) send;
16460 nend = (char *) s + size;
16461 n = 0;
16462
16463 opcode = bfd_get_micromips_32 (abfd, plt_data + 12);
16464 if (opcode == 0x3302fffe)
16465 {
16466 if (!micromips_p)
16467 return -1;
16468 plt0_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt0_entry);
16469 other = STO_MICROMIPS;
16470 }
833794fc
MR
16471 else if (opcode == 0x0398c1d0)
16472 {
16473 if (!micromips_p)
16474 return -1;
16475 plt0_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt0_entry);
16476 other = STO_MICROMIPS;
16477 }
1bbce132
MR
16478 else
16479 {
16480 plt0_size = 4 * ARRAY_SIZE (mips_o32_exec_plt0_entry);
16481 other = 0;
16482 }
16483
16484 s->the_bfd = abfd;
16485 s->flags = BSF_SYNTHETIC | BSF_FUNCTION | BSF_LOCAL;
16486 s->section = plt;
16487 s->value = 0;
16488 s->name = names;
16489 s->udata.i = other;
16490 memcpy (names, pltname, sizeof (pltname));
16491 names += sizeof (pltname);
16492 ++s, ++n;
16493
16494 pi = 0;
16495 for (plt_offset = plt0_size;
16496 plt_offset + 8 <= plt->size && s < send;
16497 plt_offset += entry_size)
16498 {
16499 bfd_vma gotplt_addr;
16500 const char *suffix;
16501 bfd_vma gotplt_hi;
16502 bfd_vma gotplt_lo;
16503 size_t suffixlen;
16504
16505 opcode = bfd_get_micromips_32 (abfd, plt_data + plt_offset + 4);
16506
16507 /* Check if the second word matches the expected MIPS16 instruction. */
16508 if (opcode == 0x651aeb00)
16509 {
16510 if (micromips_p)
16511 return -1;
16512 /* Truncated table??? */
16513 if (plt_offset + 16 > plt->size)
16514 break;
16515 gotplt_addr = bfd_get_32 (abfd, plt_data + plt_offset + 12);
16516 entry_size = 2 * ARRAY_SIZE (mips16_o32_exec_plt_entry);
16517 suffixlen = sizeof (m16suffix);
16518 suffix = m16suffix;
16519 other = STO_MIPS16;
16520 }
833794fc 16521 /* Likewise the expected microMIPS instruction (no insn32 mode). */
1bbce132
MR
16522 else if (opcode == 0xff220000)
16523 {
16524 if (!micromips_p)
16525 return -1;
16526 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset) & 0x7f;
16527 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16528 gotplt_hi = ((gotplt_hi ^ 0x40) - 0x40) << 18;
16529 gotplt_lo <<= 2;
16530 gotplt_addr = gotplt_hi + gotplt_lo;
16531 gotplt_addr += ((plt->vma + plt_offset) | 3) ^ 3;
16532 entry_size = 2 * ARRAY_SIZE (micromips_o32_exec_plt_entry);
16533 suffixlen = sizeof (microsuffix);
16534 suffix = microsuffix;
16535 other = STO_MICROMIPS;
16536 }
833794fc
MR
16537 /* Likewise the expected microMIPS instruction (insn32 mode). */
16538 else if ((opcode & 0xffff0000) == 0xff2f0000)
16539 {
16540 gotplt_hi = bfd_get_16 (abfd, plt_data + plt_offset + 2) & 0xffff;
16541 gotplt_lo = bfd_get_16 (abfd, plt_data + plt_offset + 6) & 0xffff;
16542 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16543 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16544 gotplt_addr = gotplt_hi + gotplt_lo;
16545 entry_size = 2 * ARRAY_SIZE (micromips_insn32_o32_exec_plt_entry);
16546 suffixlen = sizeof (microsuffix);
16547 suffix = microsuffix;
16548 other = STO_MICROMIPS;
16549 }
1bbce132
MR
16550 /* Otherwise assume standard MIPS code. */
16551 else
16552 {
16553 gotplt_hi = bfd_get_32 (abfd, plt_data + plt_offset) & 0xffff;
16554 gotplt_lo = bfd_get_32 (abfd, plt_data + plt_offset + 4) & 0xffff;
16555 gotplt_hi = ((gotplt_hi ^ 0x8000) - 0x8000) << 16;
16556 gotplt_lo = (gotplt_lo ^ 0x8000) - 0x8000;
16557 gotplt_addr = gotplt_hi + gotplt_lo;
16558 entry_size = 4 * ARRAY_SIZE (mips_exec_plt_entry);
16559 suffixlen = sizeof (mipssuffix);
16560 suffix = mipssuffix;
16561 other = 0;
16562 }
16563 /* Truncated table??? */
16564 if (plt_offset + entry_size > plt->size)
16565 break;
16566
16567 for (i = 0;
16568 i < count && p[pi].address != gotplt_addr;
16569 i++, pi = (pi + bed->s->int_rels_per_ext_rel) % counti);
16570
16571 if (i < count)
16572 {
16573 size_t namelen;
16574 size_t len;
16575
16576 *s = **p[pi].sym_ptr_ptr;
16577 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
16578 we are defining a symbol, ensure one of them is set. */
16579 if ((s->flags & BSF_LOCAL) == 0)
16580 s->flags |= BSF_GLOBAL;
16581 s->flags |= BSF_SYNTHETIC;
16582 s->section = plt;
16583 s->value = plt_offset;
16584 s->name = names;
16585 s->udata.i = other;
16586
16587 len = strlen ((*p[pi].sym_ptr_ptr)->name);
16588 namelen = len + suffixlen;
16589 if (names + namelen > nend)
16590 break;
16591
16592 memcpy (names, (*p[pi].sym_ptr_ptr)->name, len);
16593 names += len;
16594 memcpy (names, suffix, suffixlen);
16595 names += suffixlen;
16596
16597 ++s, ++n;
16598 pi = (pi + bed->s->int_rels_per_ext_rel) % counti;
16599 }
16600 }
16601
16602 free (plt_data);
16603
16604 return n;
16605}
16606
5e7fc731
MR
16607/* Return the ABI flags associated with ABFD if available. */
16608
16609Elf_Internal_ABIFlags_v0 *
16610bfd_mips_elf_get_abiflags (bfd *abfd)
16611{
16612 struct mips_elf_obj_tdata *tdata = mips_elf_tdata (abfd);
16613
16614 return tdata->abiflags_valid ? &tdata->abiflags : NULL;
16615}
16616
bb29b84d
MR
16617/* MIPS libc ABI versions, used with the EI_ABIVERSION ELF file header
16618 field. Taken from `libc-abis.h' generated at GNU libc build time.
16619 Using a MIPS_ prefix as other libc targets use different values. */
16620enum
16621{
16622 MIPS_LIBC_ABI_DEFAULT = 0,
16623 MIPS_LIBC_ABI_MIPS_PLT,
16624 MIPS_LIBC_ABI_UNIQUE,
16625 MIPS_LIBC_ABI_MIPS_O32_FP64,
47275900 16626 MIPS_LIBC_ABI_ABSOLUTE,
f16a9783 16627 MIPS_LIBC_ABI_XHASH,
bb29b84d
MR
16628 MIPS_LIBC_ABI_MAX
16629};
16630
ed7e9d0b
AM
16631bfd_boolean
16632_bfd_mips_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
861fb55a 16633{
47275900 16634 struct mips_elf_link_hash_table *htab = NULL;
861fb55a
DJ
16635 Elf_Internal_Ehdr *i_ehdrp;
16636
ed7e9d0b
AM
16637 if (!_bfd_elf_init_file_header (abfd, link_info))
16638 return FALSE;
16639
861fb55a
DJ
16640 i_ehdrp = elf_elfheader (abfd);
16641 if (link_info)
16642 {
16643 htab = mips_elf_hash_table (link_info);
4dfe6ac6 16644 BFD_ASSERT (htab != NULL);
861fb55a 16645 }
0af03126 16646
90c14f0c
L
16647 if (htab != NULL
16648 && htab->use_plts_and_copy_relocs
16649 && htab->root.target_os != is_vxworks)
47275900
MR
16650 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_PLT;
16651
351cdf24
MF
16652 if (mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64
16653 || mips_elf_tdata (abfd)->abiflags.fp_abi == Val_GNU_MIPS_ABI_FP_64A)
bb29b84d 16654 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_MIPS_O32_FP64;
334cd8a7 16655
47275900
MR
16656 /* Mark that we need support for absolute symbols in the dynamic loader. */
16657 if (htab != NULL && htab->use_absolute_zero && htab->gnu_target)
16658 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_ABSOLUTE;
16659
f16a9783
MS
16660 /* Mark that we need support for .MIPS.xhash in the dynamic linker,
16661 if it is the only hash section that will be created. */
16662 if (link_info && link_info->emit_gnu_hash && !link_info->emit_hash)
16663 i_ehdrp->e_ident[EI_ABIVERSION] = MIPS_LIBC_ABI_XHASH;
ed7e9d0b 16664 return TRUE;
861fb55a 16665}
2f0c68f2
CM
16666
16667int
1ced1a5f
MR
16668_bfd_mips_elf_compact_eh_encoding
16669 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
2f0c68f2
CM
16670{
16671 return DW_EH_PE_pcrel | DW_EH_PE_sdata4;
16672}
16673
16674/* Return the opcode for can't unwind. */
16675
16676int
1ced1a5f
MR
16677_bfd_mips_elf_cant_unwind_opcode
16678 (struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
2f0c68f2
CM
16679{
16680 return COMPACT_EH_CANT_UNWIND_OPCODE;
16681}
f16a9783
MS
16682
16683/* Record a position XLAT_LOC in the xlat translation table, associated with
16684 the hash entry H. The entry in the translation table will later be
16685 populated with the real symbol dynindx. */
16686
16687void
16688_bfd_mips_elf_record_xhash_symbol (struct elf_link_hash_entry *h,
16689 bfd_vma xlat_loc)
16690{
16691 struct mips_elf_link_hash_entry *hmips;
16692
16693 hmips = (struct mips_elf_link_hash_entry *) h;
16694 hmips->mipsxhash_loc = xlat_loc;
16695}